[comp.os.vms] Signal Handling in VAX ^C

BJH@V3.INFERENCE.COM.UUCP (07/01/88)

HELP! -

Here is a simple little C program that is supposed to have a top level
command loop and a debug command loop.  Each loop is supposed to scanf
for commands and exit when you type 'X'.  To get to the debug command
loop from the top level you type ^C.  Sure enough, that works.  But once
in debugger() it seems that stdin gets wedged, ie. scanf from stdin
returns 0 tokens.  In fact, debugger() never stops at scanf and asks for
input; it just loops endlessly.  I have tried all sorts of clearerr(),
rewind(), open(), close(), freopen(), setbuf() operations on stdin to no
avail.  What am I missing?  Is VAX C broken?  Am I broken? 

PS. I'm using VMS 4.7, VAX C 2.3.  And please don't respond with 'Try
this' 'Try that' unless you've tried it and it works, because I've
already tried a hundred billion things.

=> Brian Hagerty [sic]	     BJH @ V3.INFERENCE.COM
=> Network Manager	     "If I had a penny for every solution I've tried!"
=> Inference Corporation
=> Lost Angeles, UT

8< ...snip... 8< ...snip... 8< ...snip... 8< ...snip... 8< ...snip... 

#include stdio
#include signal
#include setjmp

jmp_buf env;				/* set/longjmp environment buffer */

main()
{
  char buf[80];
  int debugger(), n;

  signal( SIGINT, debugger);		/* Set the trap for ^C */

  while( setjmp(env) != 0 ) {		/* False first time thru */
    printf( "Long jump!\n");		/* True when returning from debugger */
  }

  while( *buf != 'X' ) {	   	/* TOPLEVEL command loop */
    printf( "toplevel> ");  	   	/*   Loop works fine. */
    n = fscanf( stdin, "%s", &buf);	/*   Try ^C when you're bored */
    printf( "%d toplevel tokens\n", n);
  }
}

debugger( sigint, code, scp)
int sigint, code;
struct sigcontext *scp;
{
  char buf[80];
  int n;

  printf( "Signal trapped!\n");		/* ^C from TOPLEVEL takes you here */
  signal( SIGINT, debugger);		/* Reset the trap for ^C */

  while( *buf != 'X' ) {
    printf( "debug> ");			/* This loop printf's ... */
    n = fscanf( stdin, "%s", &buf);	/*   but won't accept input */
    printf( "%d debug tokens\n", n);	/*   and loops forever */
  }

  longjmp( env, sigint);		/* Jump to main (if u ever get here!) */
}