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