xref: /csrg-svn/old/adb/adb.vax/main.c (revision 6416)
1*6416Sroot static	char sccsid[] = "@(#)main.c 4.3 04/01/82";
23758Sroot /*
33758Sroot  * adb - main command loop and error/interrupt handling
43758Sroot  */
53758Sroot #include "defs.h"
63758Sroot 
73758Sroot MSG		NOEOR;
83758Sroot 
93758Sroot INT		mkfault;
103758Sroot INT		executing;
113758Sroot INT		infile;
123758Sroot CHAR		*lp;
133758Sroot L_INT		maxoff;
143758Sroot L_INT		maxpos;
153758Sroot ADDR		sigint;
163758Sroot ADDR		sigqit;
173758Sroot INT		wtflag;
183758Sroot L_INT		maxfile;
193758Sroot STRING		errflg;
203758Sroot L_INT		exitflg;
213758Sroot 
223758Sroot CHAR		lastc;
233758Sroot INT		eof;
243758Sroot 
253758Sroot INT		lastcom;
263758Sroot 
273758Sroot long	maxoff = MAXOFF;
283758Sroot long	maxpos = MAXPOS;
293758Sroot char	*Ipath = "/usr/lib/adb";
303758Sroot 
main(argc,argv)313758Sroot main(argc, argv)
323758Sroot 	register char **argv;
333758Sroot 	int argc;
343758Sroot {
353758Sroot 
363758Sroot 	mkioptab();
373758Sroot another:
383758Sroot 	if (argc>1) {
393758Sroot 		if (eqstr("-w", argv[1])) {
403758Sroot 			wtflag = 2;		/* suitable for open() */
413758Sroot 			argc--, argv++;
423758Sroot 			goto another;
433758Sroot 		}
443778Sroot 		if (eqstr("-k", argv[1])) {
453778Sroot 			kernel = 1;
463778Sroot 			argc--, argv++;
473778Sroot 			goto another;
483778Sroot 		}
493758Sroot 		if (argv[1][0] == '-' && argv[1][1] == 'I') {
503758Sroot 			Ipath = argv[1]+2;
513758Sroot 			argc--, argv++;
523758Sroot 		}
533758Sroot 	}
543758Sroot 	if (argc > 1)
553758Sroot 		symfil = argv[1];
563758Sroot 	if (argc > 2)
573758Sroot 		corfil = argv[2];
583758Sroot 	xargc = argc;
593758Sroot 	setsym(); setcor(); setvar();
603758Sroot 
613758Sroot 	if ((sigint=signal(SIGINT,SIG_IGN)) != SIG_IGN) {
623758Sroot 		sigint = fault;
633758Sroot 		signal(SIGINT, fault);
643758Sroot 	}
653758Sroot 	sigqit = signal(SIGQUIT, SIG_IGN);
663758Sroot 	setexit();
673758Sroot 	if (executing)
683758Sroot 		delbp();
693758Sroot 	executing = 0;
703758Sroot 	for (;;) {
713758Sroot 		flushbuf();
723758Sroot 		if (errflg) {
733758Sroot 			printf("%s\n", errflg);
743758Sroot 			exitflg = errflg;
753758Sroot 			errflg = 0;
763758Sroot 		}
773758Sroot 		if (mkfault) {
783758Sroot 			mkfault=0;
793758Sroot 			printc('\n');
803758Sroot 			prints(DBNAME);
813758Sroot 		}
823758Sroot 		lp=0; rdc(); lp--;
833758Sroot 		if (eof) {
843758Sroot 			if (infile) {
853758Sroot 				iclose(-1, 0); eof=0; reset();
863758Sroot 			} else
873758Sroot 				done();
883758Sroot 		} else
893758Sroot 			exitflg = 0;
903758Sroot 		command(0, lastcom);
913758Sroot 		if (lp && lastc!='\n')
923758Sroot 			error(NOEOR);
933758Sroot 	}
943758Sroot }
953758Sroot 
done()963758Sroot done()
973758Sroot {
983758Sroot 	endpcs();
993758Sroot 	exit(exitflg);
1003758Sroot }
1013758Sroot 
1023758Sroot L_INT
round(a,b)1033758Sroot round(a,b)
1043758Sroot REG L_INT a, b;
1053758Sroot {
1063758Sroot 	REG L_INT w;
1073758Sroot 	w = (a/b)*b;
1083758Sroot 	IF a!=w THEN w += b; FI
1093758Sroot 	return(w);
1103758Sroot }
1113758Sroot 
1123758Sroot /*
1133758Sroot  * If there has been an error or a fault, take the error.
1143758Sroot  */
chkerr()1153758Sroot chkerr()
1163758Sroot {
1173758Sroot 	if (errflg || mkfault)
1183758Sroot 		error(errflg);
1193758Sroot }
1203758Sroot 
1213758Sroot /*
1223758Sroot  * An error occurred; save the message for later printing,
1233758Sroot  * close open files, and reset to main command loop.
1243758Sroot  */
error(n)1253758Sroot error(n)
1263758Sroot 	char *n;
1273758Sroot {
1283758Sroot 	errflg = n;
1293758Sroot 	iclose(0, 1); oclose();
1303758Sroot 	reset();
1313758Sroot }
1323758Sroot 
1333758Sroot /*
1343758Sroot  * An interrupt occurred; reset the interrupt
1353758Sroot  * catch, seek to the end of the current file
1363758Sroot  * and remember that there was a fault.
1373758Sroot  */
fault(a)1383758Sroot fault(a)
1393758Sroot {
1403758Sroot 	signal(a, fault);
1413758Sroot 	lseek(infile, 0L, 2);
1423758Sroot 	mkfault++;
1433758Sroot }
144