xref: /csrg-svn/old/adb/adb.vax/main.c (revision 3758)
1 static	char sccsid[] = "@(#)main.c 4.1 05/14/81";
2 /*
3  * adb - main command loop and error/interrupt handling
4  */
5 #include "defs.h"
6 
7 MSG		NOEOR;
8 
9 INT		mkfault;
10 INT		executing;
11 INT		infile;
12 CHAR		*lp;
13 L_INT		maxoff;
14 L_INT		maxpos;
15 ADDR		sigint;
16 ADDR		sigqit;
17 INT		wtflag;
18 L_INT		maxfile;
19 STRING		errflg;
20 L_INT		exitflg;
21 
22 CHAR		lastc;
23 INT		eof;
24 
25 INT		lastcom;
26 
27 long	maxoff = MAXOFF;
28 long	maxpos = MAXPOS;
29 char	*Ipath = "/usr/lib/adb";
30 
31 main(argc, argv)
32 	register char **argv;
33 	int argc;
34 {
35 
36 	mkioptab();
37 another:
38 	if (argc>1) {
39 		if (eqstr("-w", argv[1])) {
40 			wtflag = 2;		/* suitable for open() */
41 			argc--, argv++;
42 			goto another;
43 		}
44 		if (argv[1][0] == '-' && argv[1][1] == 'I') {
45 			Ipath = argv[1]+2;
46 			argc--, argv++;
47 		}
48 	}
49 	if (argc > 1)
50 		symfil = argv[1];
51 	if (argc > 2)
52 		corfil = argv[2];
53 	xargc = argc;
54 	setsym(); setcor(); setvar();
55 
56 	if ((sigint=signal(SIGINT,SIG_IGN)) != SIG_IGN) {
57 		sigint = fault;
58 		signal(SIGINT, fault);
59 	}
60 	sigqit = signal(SIGQUIT, SIG_IGN);
61 	setexit();
62 	if (executing)
63 		delbp();
64 	executing = 0;
65 	for (;;) {
66 		flushbuf();
67 		if (errflg) {
68 			printf("%s\n", errflg);
69 			exitflg = errflg;
70 			errflg = 0;
71 		}
72 		if (mkfault) {
73 			mkfault=0;
74 			printc('\n');
75 			prints(DBNAME);
76 		}
77 		lp=0; rdc(); lp--;
78 		if (eof) {
79 			if (infile) {
80 				iclose(-1, 0); eof=0; reset();
81 			} else
82 				done();
83 		} else
84 			exitflg = 0;
85 		command(0, lastcom);
86 		if (lp && lastc!='\n')
87 			error(NOEOR);
88 	}
89 }
90 
91 done()
92 {
93 	endpcs();
94 	exit(exitflg);
95 }
96 
97 L_INT
98 round(a,b)
99 REG L_INT a, b;
100 {
101 	REG L_INT w;
102 	w = (a/b)*b;
103 	IF a!=w THEN w += b; FI
104 	return(w);
105 }
106 
107 /*
108  * If there has been an error or a fault, take the error.
109  */
110 chkerr()
111 {
112 	if (errflg || mkfault)
113 		error(errflg);
114 }
115 
116 /*
117  * An error occurred; save the message for later printing,
118  * close open files, and reset to main command loop.
119  */
120 error(n)
121 	char *n;
122 {
123 	errflg = n;
124 	iclose(0, 1); oclose();
125 	reset();
126 }
127 
128 /*
129  * An interrupt occurred; reset the interrupt
130  * catch, seek to the end of the current file
131  * and remember that there was a fault.
132  */
133 fault(a)
134 {
135 	signal(a, fault);
136 	lseek(infile, 0L, 2);
137 	mkfault++;
138 }
139