1 /* Copyright (c) 1982 Regents of the University of California */ 2 3 static char sccsid[] = "@(#)main.c 1.1 01/18/82"; 4 5 /* 6 * Debugger main routine. 7 */ 8 9 #include "defs.h" 10 #include <setjmp.h> 11 #include <signal.h> 12 #include "main.h" 13 #include "command.h" 14 #include "process.h" 15 #include "object.h" 16 17 #define FIRST_TIME 0 /* initial value setjmp returns */ 18 #define isinteractive() (isatty(fileno(stdin))) 19 20 LOCAL jmp_buf env; 21 LOCAL catchintr(); 22 23 main(argc, argv) 24 int argc; 25 char **argv; 26 { 27 FILE *fp; 28 29 cmdname = argv[0]; 30 catcherrs(); 31 scanargs(argc, argv); 32 if ((fp = fopen(objname, "r")) == NIL) { 33 panic("can't read %s", objname); 34 } else { 35 fclose(fp); 36 } 37 if (option('r')) { 38 if (setjmp(env) == FIRST_TIME) { 39 arginit(); 40 run(); 41 /* NOTREACHED */ 42 } else { 43 option('r') = FALSE; 44 if (isinteractive()) { 45 printf("> "); 46 fflush(stdout); 47 } 48 } 49 } else { 50 start(NIL, NIL, NIL); 51 prompt(); 52 init(); 53 } 54 setjmp(env); 55 signal(SIGINT, &catchintr); 56 yyparse(); 57 putchar('\n'); 58 } 59 60 /* 61 * Initialize the world, including setting initial input file 62 * if the file exists. 63 */ 64 65 init() 66 { 67 initinput(); 68 readobj(objname); 69 lexinit(); 70 } 71 72 /* 73 * After a non-fatal error we jump back to command parsing. 74 */ 75 76 erecover() 77 { 78 gobble(); 79 prompt(); 80 longjmp(env, 1); 81 } 82 83 /* 84 * This routine is called when an interrupt occurs. 85 */ 86 87 LOCAL catchintr() 88 { 89 putchar('\n'); 90 prompt(); 91 longjmp(env, 1); 92 } 93 94 /* 95 * scan the argument list 96 */ 97 98 LOCAL scanargs(argc, argv) 99 int argc; 100 char **argv; 101 { 102 register int i, j; 103 BOOLEAN foundfile; 104 105 foundfile = FALSE; 106 for (i = 1; i < argc; i++) { 107 if (argv[i][0] == '-') { 108 for (j = 1; argv[i][j] != '\0'; j++) { 109 setoption(argv[i][j]); 110 } 111 } else if (!foundfile) { 112 objname = argv[i]; 113 } else { 114 panic("extraneous argument %s", argv[i]); 115 } 116 } 117 } 118 119 /* 120 * take appropriate action for recognized command argument 121 */ 122 123 LOCAL setoption(c) 124 register char c; 125 { 126 switch(c) { 127 case 'r': /* run program before accepting commands */ 128 case 'b': /* trace internal breakpoints (for debugging) */ 129 case 'e': /* trace execution (for debugging) */ 130 option(c) = TRUE; 131 break; 132 133 default: 134 panic("unknown option '%c'", c); 135 } 136 } 137