Commit | Line | Data |
---|---|---|
dad19963 ML |
1 | /* Copyright (c) 1982 Regents of the University of California */ |
2 | ||
76eb7cef | 3 | static char sccsid[] = "@(#)main.c 1.4 %G%"; |
dad19963 ML |
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 | ||
76eb7cef | 17 | #define FIRST_TIME 0 /* initial value setjmp returns */ |
dad19963 | 18 | |
b7eb7857 | 19 | LOCAL int firstarg; |
dad19963 ML |
20 | LOCAL jmp_buf env; |
21 | LOCAL catchintr(); | |
22 | ||
23 | main(argc, argv) | |
24 | int argc; | |
25 | char **argv; | |
26 | { | |
76eb7cef ML |
27 | FILE *fp; |
28 | int i; | |
29 | ||
30 | catcherrs(); | |
31 | catchsigs(); | |
32 | scanargs(argc, argv); | |
33 | cmdname = argv[0]; | |
34 | if ((fp = fopen(objname, "r")) == NIL) { | |
35 | panic("can't read %s", objname); | |
36 | } else { | |
37 | fclose(fp); | |
38 | } | |
39 | if (option('r')) { | |
40 | if (setjmp(env) == FIRST_TIME) { | |
41 | arginit(); | |
42 | for (i = firstarg; i < argc; i++) { | |
43 | newarg(argv[i]); | |
44 | } | |
45 | run(); | |
46 | /* NOTREACHED */ | |
dad19963 | 47 | } else { |
76eb7cef | 48 | option('r') = FALSE; |
dad19963 | 49 | } |
76eb7cef ML |
50 | } else { |
51 | initstart(); | |
52 | prompt(); | |
53 | init(); | |
54 | } | |
55 | setjmp(env); | |
56 | signal(SIGINT, catchintr); | |
57 | yyparse(); | |
58 | putchar('\n'); | |
59 | quit(0); | |
dad19963 ML |
60 | } |
61 | ||
62 | /* | |
63 | * Initialize the world, including setting initial input file | |
64 | * if the file exists. | |
65 | */ | |
66 | ||
67 | init() | |
68 | { | |
76eb7cef ML |
69 | initinput(); |
70 | readobj(objname); | |
71 | lexinit(); | |
dad19963 ML |
72 | } |
73 | ||
74 | /* | |
75 | * After a non-fatal error we jump back to command parsing. | |
76 | */ | |
77 | ||
78 | erecover() | |
79 | { | |
76eb7cef ML |
80 | gobble(); |
81 | prompt(); | |
82 | longjmp(env, 1); | |
dad19963 ML |
83 | } |
84 | ||
85 | /* | |
86 | * This routine is called when an interrupt occurs. | |
87 | */ | |
88 | ||
89 | LOCAL catchintr() | |
90 | { | |
76eb7cef ML |
91 | putchar('\n'); |
92 | prompt(); | |
93 | longjmp(env, 1); | |
dad19963 ML |
94 | } |
95 | ||
96 | /* | |
97 | * scan the argument list | |
98 | */ | |
99 | ||
100 | LOCAL scanargs(argc, argv) | |
101 | int argc; | |
102 | char **argv; | |
103 | { | |
76eb7cef ML |
104 | register int i, j; |
105 | BOOLEAN done; | |
106 | ||
107 | if (streq(argv[0], "pxhdr") || streq(argv[0], "pix")) { | |
108 | objname = argv[1]; | |
109 | option('r') = TRUE; | |
110 | option('t') = TRUE; | |
111 | if (streq(argv[0], "pxhdr")) { | |
112 | setargs("pdx", argv[2]); | |
113 | firstarg = 3; | |
b7eb7857 | 114 | } else { |
76eb7cef ML |
115 | setargs("pix", NIL); |
116 | firstarg = 2; | |
117 | } | |
118 | argv[0] = "pdx"; | |
119 | } else { | |
120 | done = FALSE; | |
121 | i = 1; | |
122 | while (i < argc && !done) { | |
123 | if (argv[i][0] == '-') { | |
124 | for (j = 1; argv[i][j] != '\0'; j++) { | |
125 | switch (argv[i][j]) { | |
126 | case 'r': /* run program before accepting commands */ | |
127 | case 'i': /* assume input is a terminal */ | |
128 | case 'b': /* (internal) trace breakpoints */ | |
129 | case 'e': /* (internal) trace execution */ | |
130 | case 'h': /* (internal) display header information */ | |
131 | option(argv[i][j]) = TRUE; | |
132 | break; | |
133 | ||
134 | default: | |
135 | panic("bad option \"%c\"", argv[i]); | |
136 | } | |
b7eb7857 | 137 | } |
76eb7cef ML |
138 | } else { |
139 | objname = argv[i]; | |
140 | done = TRUE; | |
141 | } | |
142 | i++; | |
dad19963 | 143 | } |
76eb7cef ML |
144 | firstarg = i; |
145 | setargs("pdx", objname); | |
146 | } | |
dad19963 ML |
147 | } |
148 | ||
149 | /* | |
b7eb7857 ML |
150 | * Terminate program. In the case of the -t option, we must remove |
151 | * the object file because it's a tmp file. | |
dad19963 ML |
152 | */ |
153 | ||
b7eb7857 ML |
154 | quit(r) |
155 | int r; | |
dad19963 | 156 | { |
76eb7cef ML |
157 | if (option('t')) { |
158 | unlink(objname); | |
159 | } | |
160 | exit(r); | |
b7eb7857 ML |
161 | } |
162 | ||
163 | LOCAL catchsigs() | |
164 | { | |
76eb7cef ML |
165 | signal(SIGHUP, quit); |
166 | signal(SIGQUIT, quit); | |
dad19963 | 167 | } |