main.c (2574B)
1 /* 2 * db - main command loop and error/interrupt handling 3 */ 4 #include "defs.h" 5 #include "fns.h" 6 7 int wtflag = OREAD; 8 BOOL kflag; 9 10 BOOL mkfault; 11 ADDR maxoff; 12 13 int xargc; /* bullshit */ 14 15 extern BOOL executing; 16 extern int infile; 17 int exitflg; 18 extern int eof; 19 20 int alldigs(char*); 21 void fault(void*, char*); 22 23 extern char *Ipath; 24 jmp_buf env; 25 static char *errmsg; 26 27 void 28 usage(void) 29 { 30 fprint(2, "usage: db [-kw] [-m machine] [-I dir] [symfile] [pid]\n"); 31 exits("usage"); 32 } 33 34 void 35 main(int argc, char **argv) 36 { 37 int omode; 38 volatile int quiet; 39 char *s; 40 char *name; 41 42 quiet = 0; 43 name = 0; 44 outputinit(); 45 maxoff = MAXOFF; 46 omode = OREAD; 47 ARGBEGIN{ 48 default: 49 usage(); 50 case 'A': 51 abort(); 52 case 'k': 53 kflag = 1; 54 break; 55 case 'w': 56 omode = ORDWR; 57 break; 58 case 'I': 59 s = ARGF(); 60 if(s == 0) 61 dprint("missing -I argument\n"); 62 else 63 Ipath = s; 64 break; 65 case 'm': 66 name = ARGF(); 67 if(name == 0) 68 dprint("missing -m argument\n"); 69 break; 70 case 'q': 71 quiet = 1; 72 break; 73 }ARGEND 74 75 attachargs(argc, argv, omode, !quiet); 76 77 dotmap = dumbmap(-1); 78 79 /* 80 * show initial state and drop into the execution loop. 81 */ 82 notify(fault); 83 setsym(); 84 if(setjmp(env) == 0){ 85 if (pid || corhdr) 86 setcor(); /* could get error */ 87 if (correg && !quiet) { 88 dprint("%s\n", mach->exc(cormap, correg)); 89 printpc(); 90 } 91 } 92 93 setjmp(env); 94 if (executing) 95 delbp(); 96 executing = FALSE; 97 for (;;) { 98 flushbuf(); 99 if (errmsg) { 100 dprint(errmsg); 101 printc('\n'); 102 errmsg = 0; 103 exitflg = 0; 104 } 105 if (mkfault) { 106 mkfault=0; 107 printc('\n'); 108 prints(DBNAME); 109 } 110 clrinp(); 111 rdc(); 112 reread(); 113 if (eof) { 114 if (infile == STDIN) 115 done(); 116 iclose(-1, 0); 117 eof = 0; 118 longjmp(env, 1); 119 } 120 exitflg = 0; 121 command(0, 0); 122 reread(); 123 if (rdc() != '\n') 124 error("newline expected"); 125 } 126 } 127 128 int 129 alldigs(char *s) 130 { 131 while(*s){ 132 if(*s<'0' || '9'<*s) 133 return 0; 134 s++; 135 } 136 return 1; 137 } 138 139 void 140 done(void) 141 { 142 if (pid) 143 endpcs(); 144 exits(exitflg? "error": 0); 145 } 146 147 /* 148 * An error occurred; save the message for later printing, 149 * close open files, and reset to main command loop. 150 */ 151 void 152 error(char *n) 153 { 154 errmsg = n; 155 iclose(0, 1); 156 oclose(); 157 flush(); 158 delbp(); 159 ending = 0; 160 longjmp(env, 1); 161 } 162 163 void 164 errors(char *m, char *n) 165 { 166 static char buf[128]; 167 168 sprint(buf, "%s: %s", m, n); 169 error(buf); 170 } 171 172 /* 173 * An interrupt occurred; 174 * seek to the end of the current file 175 * and remember that there was a fault. 176 */ 177 void 178 fault(void *a, char *s) 179 { 180 USED(a); 181 if(strncmp(s, "interrupt", 9) == 0){ 182 seek(infile, 0L, 2); 183 mkfault++; 184 noted(NCONT); 185 } 186 noted(NDFLT); 187 }