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