148100Sbostic /*-
2*62137Sbostic * Copyright (c) 1980, 1993
3*62137Sbostic * The Regents of the University of California. All rights reserved.
448100Sbostic *
548100Sbostic * %sccs.include.redist.c%
622650Sdist */
75489Slinton
822650Sdist #ifndef lint
9*62137Sbostic static char copyright[] =
10*62137Sbostic "@(#) Copyright (c) 1980, 1993\n\
11*62137Sbostic The Regents of the University of California. All rights reserved.\n";
1248100Sbostic #endif /* not lint */
135489Slinton
1422650Sdist #ifndef lint
15*62137Sbostic static char sccsid[] = "@(#)main.c 8.1 (Berkeley) 06/06/93";
1648100Sbostic #endif /* not lint */
1722650Sdist
185489Slinton /*
195489Slinton * Debugger main routine.
205489Slinton */
215489Slinton
225489Slinton #include "defs.h"
235489Slinton #include <setjmp.h>
245489Slinton #include <signal.h>
255489Slinton #include "main.h"
265489Slinton #include "command.h"
275489Slinton #include "process.h"
285489Slinton #include "object.h"
295489Slinton
306875Slinton #define FIRST_TIME 0 /* initial value setjmp returns */
315489Slinton
325648Slinton LOCAL int firstarg;
335489Slinton LOCAL jmp_buf env;
345489Slinton LOCAL catchintr();
355489Slinton
main(argc,argv)365489Slinton main(argc, argv)
375489Slinton int argc;
385489Slinton char **argv;
395489Slinton {
406875Slinton FILE *fp;
416875Slinton int i;
425489Slinton
4330846Smckusick #ifdef lint
4430846Smckusick syserr();
4530846Smckusick #endif
466875Slinton catchsigs();
476875Slinton scanargs(argc, argv);
486875Slinton cmdname = argv[0];
496875Slinton if ((fp = fopen(objname, "r")) == NIL) {
506875Slinton panic("can't read %s", objname);
516875Slinton } else {
526875Slinton fclose(fp);
536875Slinton }
546875Slinton if (option('r')) {
556875Slinton if (setjmp(env) == FIRST_TIME) {
566875Slinton arginit();
576875Slinton for (i = firstarg; i < argc; i++) {
586875Slinton newarg(argv[i]);
596875Slinton }
606875Slinton run();
616875Slinton /* NOTREACHED */
625489Slinton } else {
636875Slinton option('r') = FALSE;
645489Slinton }
656875Slinton } else {
666875Slinton initstart();
676875Slinton prompt();
686875Slinton init();
696875Slinton }
706875Slinton setjmp(env);
716875Slinton signal(SIGINT, catchintr);
726875Slinton yyparse();
736875Slinton putchar('\n');
746875Slinton quit(0);
755489Slinton }
765489Slinton
775489Slinton /*
785489Slinton * Initialize the world, including setting initial input file
795489Slinton * if the file exists.
805489Slinton */
815489Slinton
init()825489Slinton init()
835489Slinton {
846875Slinton initinput();
856875Slinton readobj(objname);
866875Slinton lexinit();
875489Slinton }
885489Slinton
895489Slinton /*
905489Slinton * After a non-fatal error we jump back to command parsing.
915489Slinton */
925489Slinton
erecover()935489Slinton erecover()
945489Slinton {
956875Slinton gobble();
966875Slinton prompt();
976875Slinton longjmp(env, 1);
985489Slinton }
995489Slinton
1005489Slinton /*
1015489Slinton * This routine is called when an interrupt occurs.
1025489Slinton */
1035489Slinton
catchintr()1045489Slinton LOCAL catchintr()
1055489Slinton {
1066875Slinton putchar('\n');
1076875Slinton prompt();
1086875Slinton longjmp(env, 1);
1095489Slinton }
1105489Slinton
1115489Slinton /*
1125489Slinton * scan the argument list
1135489Slinton */
1145489Slinton
scanargs(argc,argv)1155489Slinton LOCAL scanargs(argc, argv)
1165489Slinton int argc;
1175489Slinton char **argv;
1185489Slinton {
1196875Slinton register int i, j;
1206875Slinton BOOLEAN done;
1215489Slinton
1226875Slinton if (streq(argv[0], "pxhdr") || streq(argv[0], "pix")) {
1236875Slinton objname = argv[1];
1246875Slinton option('r') = TRUE;
1256875Slinton option('t') = TRUE;
1266875Slinton if (streq(argv[0], "pxhdr")) {
1276875Slinton setargs("pdx", argv[2]);
1286875Slinton firstarg = 3;
1295648Slinton } else {
1306875Slinton setargs("pix", NIL);
1316875Slinton firstarg = 2;
1326875Slinton }
1336875Slinton argv[0] = "pdx";
1346875Slinton } else {
1356875Slinton done = FALSE;
1366875Slinton i = 1;
1376875Slinton while (i < argc && !done) {
1386875Slinton if (argv[i][0] == '-') {
1396875Slinton for (j = 1; argv[i][j] != '\0'; j++) {
1406875Slinton switch (argv[i][j]) {
1416875Slinton case 'r': /* run program before accepting commands */
1426875Slinton case 'i': /* assume input is a terminal */
1436875Slinton case 'b': /* (internal) trace breakpoints */
1446875Slinton case 'e': /* (internal) trace execution */
1456875Slinton case 'h': /* (internal) display header information */
1466875Slinton option(argv[i][j]) = TRUE;
1476875Slinton break;
1485648Slinton
1496875Slinton default:
1506875Slinton panic("bad option \"%c\"", argv[i]);
1516875Slinton }
1525648Slinton }
1536875Slinton } else {
1546875Slinton objname = argv[i];
1556875Slinton done = TRUE;
1566875Slinton }
1576875Slinton i++;
1585489Slinton }
1596875Slinton firstarg = i;
1606875Slinton setargs("pdx", objname);
1616875Slinton }
1625489Slinton }
1635489Slinton
1645489Slinton /*
1655648Slinton * Terminate program. In the case of the -t option, we must remove
1665648Slinton * the object file because it's a tmp file.
1675489Slinton */
1685489Slinton
quit(r)1695648Slinton quit(r)
1705648Slinton int r;
1715489Slinton {
1726875Slinton if (option('t')) {
1736875Slinton unlink(objname);
1746875Slinton }
1756875Slinton exit(r);
1765489Slinton }
1775648Slinton
catchsigs()1785648Slinton LOCAL catchsigs()
1795648Slinton {
1806875Slinton signal(SIGHUP, quit);
1816875Slinton signal(SIGQUIT, quit);
1825648Slinton }
183