xref: /csrg-svn/usr.bin/pascal/pdx/main/main.c (revision 5566)
1 /* Copyright (c) 1982 Regents of the University of California */
2 
3 static char sccsid[] = "@(#)main.c 1.2 01/19/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 #define isinteractive()		(isatty(fileno(stdin)))
19 
20 LOCAL jmp_buf env;
21 LOCAL catchintr();
22 
23 main(argc, argv)
24 int argc;
25 char **argv;
26 {
27 	FILE *fp;
28 
29 	cmdname = argv[0];
30 	catcherrs();
31 	scanargs(argc, argv);
32 	if ((fp = fopen(objname, "r")) == NIL) {
33 		panic("can't read %s", objname);
34 	} else {
35 		fclose(fp);
36 	}
37 	if (option('r')) {
38 		if (setjmp(env) == FIRST_TIME) {
39 			arginit();
40 			run();
41 			/* NOTREACHED */
42 		} else {
43 			option('r') = FALSE;
44 		}
45 	} else {
46 		start(NIL, NIL, NIL);
47 		prompt();
48 		init();
49 	}
50 	setjmp(env);
51 	signal(SIGINT, &catchintr);
52 	yyparse();
53 	putchar('\n');
54 }
55 
56 /*
57  * Initialize the world, including setting initial input file
58  * if the file exists.
59  */
60 
61 init()
62 {
63 	initinput();
64 	readobj(objname);
65 	lexinit();
66 }
67 
68 /*
69  * After a non-fatal error we jump back to command parsing.
70  */
71 
72 erecover()
73 {
74 	gobble();
75 	prompt();
76 	longjmp(env, 1);
77 }
78 
79 /*
80  * This routine is called when an interrupt occurs.
81  */
82 
83 LOCAL catchintr()
84 {
85 	putchar('\n');
86 	prompt();
87 	longjmp(env, 1);
88 }
89 
90 /*
91  * scan the argument list
92  */
93 
94 LOCAL scanargs(argc, argv)
95 int argc;
96 char **argv;
97 {
98 	register int i, j;
99 	BOOLEAN foundfile;
100 
101 	foundfile = FALSE;
102 	for (i = 1; i < argc; i++) {
103 		if (argv[i][0] == '-') {
104 			for (j = 1; argv[i][j] != '\0'; j++) {
105 				setoption(argv[i][j]);
106 			}
107 		} else if (!foundfile) {
108 			objname = argv[i];
109 		} else {
110 			panic("extraneous argument %s", argv[i]);
111 		}
112 	}
113 }
114 
115 /*
116  * take appropriate action for recognized command argument
117  */
118 
119 LOCAL setoption(c)
120 register char c;
121 {
122 	switch(c) {
123 		case 'r':	/* run program before accepting commands */
124 		case 'b':	/* trace internal breakpoints (for debugging) */
125 		case 'e':	/* trace execution (for debugging) */
126 			option(c) = TRUE;
127 			break;
128 
129 		default:
130 			panic("unknown option '%c'", c);
131 	}
132 }
133