xref: /original-bsd/usr.bin/pascal/pdx/main/main.c (revision fbed46ce)
1 /* Copyright (c) 1982 Regents of the University of California */
2 
3 static char sccsid[] = "@(#)main.c 1.3 02/02/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 
19 LOCAL int firstarg;
20 LOCAL jmp_buf env;
21 LOCAL catchintr();
22 
23 main(argc, argv)
24 int argc;
25 char **argv;
26 {
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 */
47 		} else {
48 			option('r') = FALSE;
49 		}
50 	} else {
51 		initstart();
52 		prompt();
53 		init();
54 	}
55 	setjmp(env);
56 	signal(SIGINT, catchintr);
57 	yyparse();
58 	putchar('\n');
59 	quit(0);
60 }
61 
62 /*
63  * Initialize the world, including setting initial input file
64  * if the file exists.
65  */
66 
67 init()
68 {
69 	initinput();
70 	readobj(objname);
71 	lexinit();
72 }
73 
74 /*
75  * After a non-fatal error we jump back to command parsing.
76  */
77 
78 erecover()
79 {
80 	gobble();
81 	prompt();
82 	longjmp(env, 1);
83 }
84 
85 /*
86  * This routine is called when an interrupt occurs.
87  */
88 
89 LOCAL catchintr()
90 {
91 	putchar('\n');
92 	prompt();
93 	longjmp(env, 1);
94 }
95 
96 /*
97  * scan the argument list
98  */
99 
100 LOCAL scanargs(argc, argv)
101 int argc;
102 char **argv;
103 {
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;
114 		} else {
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 'b':	/* (internal) trace breakpoints */
128 						case 'e':	/* (internal) trace execution */
129 						case 'h':	/* (internal) display header information */
130 							option(argv[i][j]) = TRUE;
131 							break;
132 
133 					default:
134 						panic("bad option \"%c\"", argv[i]);
135 					}
136 				}
137 			} else {
138 				objname = argv[i];
139 				done = TRUE;
140 			}
141 			i++;
142 		}
143 		firstarg = i;
144 		setargs("pdx", objname);
145 	}
146 }
147 
148 /*
149  * Terminate program.  In the case of the -t option, we must remove
150  * the object file because it's a tmp file.
151  */
152 
153 quit(r)
154 int r;
155 {
156 	if (option('t')) {
157 		unlink(objname);
158 	}
159 	exit(r);
160 }
161 
162 LOCAL catchsigs()
163 {
164 	signal(SIGHUP, quit);
165 	signal(SIGQUIT, quit);
166 }
167