BSD 4_1_snap release
[unix-history] / usr / src / cmd / pi / main.c
index 0f130e2..c9167fc 100644 (file)
@@ -1,20 +1,19 @@
 /* Copyright (c) 1979 Regents of the University of California */
 /* Copyright (c) 1979 Regents of the University of California */
-#
-/*
- * pi - Pascal interpreter code translator
- *
- * Charles Haley, Bill Joy UCB
- * Version 1.2 November 1978
- */
 
 
-#include "whoami"
+static char copyright[] =
+           "@(#)Copyright (c) 1979 Regents of the University of California";
+
+static char sccsid[] = "@(#)main.c 1.4 3/9/81";
+
+#include "whoami.h"
 #include "0.h"
 #include "yy.h"
 #include "0.h"
 #include "yy.h"
+#include <signal.h>
+#include "objfmt.h"
 
 /*
  * This version of pi has been in use at Berkeley since May 1977
 
 /*
  * This version of pi has been in use at Berkeley since May 1977
- * and is very stable, except for the syntactic error recovery which
- * has just been written.  Please report any problems with the error
+ * and is very stable. Please report any problems with the error
  * recovery to the second author at the address given in the file
  * READ_ME.  The second author takes full responsibility for any bugs
  * in the syntactic error recovery.
  * recovery to the second author at the address given in the file
  * READ_ME.  The second author takes full responsibility for any bugs
  * in the syntactic error recovery.
 
 char   piusage[]       = "pi [ -blnpstuw ] [ -i file ... ] name.p";
 char   pixusage[]      = "pix [ -blnpstuw ] [ -i file ... ] name.p [ arg ... ]";
 
 char   piusage[]       = "pi [ -blnpstuw ] [ -i file ... ] name.p";
 char   pixusage[]      = "pix [ -blnpstuw ] [ -i file ... ] name.p [ arg ... ]";
+char   pcusage[]       = "pc [ options ] [ -o file ] [ -i file ... ] name.p";
 
 char   *usageis        = piusage;
 
 char   *usageis        = piusage;
-char   *obj            = "obj";
 
 
-#ifdef PPC
-    char       *ppcname = "ppc.p1";
-#   ifdef DEBUG
-       char    *ppcdname = "ppcd.p1";
-#   endif
-#endif
+char   *errfile = ERR_STRNGS;
+
+#ifdef OBJ
+    char       *obj    = "obj";
+#endif OBJ
+#ifdef PC
+    char       *pcname = "pc.pc1";
+#endif PC
 #ifdef PTREE
     char       *pTreeName = "pi.pTree";
 #ifdef PTREE
     char       *pTreeName = "pi.pTree";
-#endif
+#endif PTREE
 
 /*
  * Be careful changing errfile and howfile.
  * There are the "magic" constants 9 and 15 immediately below.
 
 /*
  * Be careful changing errfile and howfile.
  * There are the "magic" constants 9 and 15 immediately below.
+ * errfile is now defined by ERR_STRNGS, in objfmt.h,
+ * and its leading path name length is ERR_PATHLEN long.
+ * this for executing out of the current directory if running as `a.something'.
  */
  */
-char   *errfile        = "/usr/lib/pi1.2strings";
-char   *howfile        = "/usr/lib/how_pi\0";
+#ifdef OBJ
+char   *howfile        = HOW_STRNGS;
+#endif OBJ
+#ifdef PC
+char   *howfile        = HOW_STRNGS;
+#endif PC
 
 int    onintr();
 
 extern char *lastname;
 
 FILE   *ibuf;
 
 int    onintr();
 
 extern char *lastname;
 
 FILE   *ibuf;
+FILE   *pcstream = NULL;
 
 /*
  * these are made real variables
 
 /*
  * these are made real variables
@@ -71,19 +80,21 @@ main(argc, argv)
        int i;
 
        if (argv[0][0] == 'a')
        int i;
 
        if (argv[0][0] == 'a')
-               errfile += 9, howfile += 9;
-       if (argv[0][0] == '-' && argv[0][1] == 'o') {
-               obj = &argv[0][2];
-               usageis = pixusage;
-               howfile[15] = 'x';
-               ofil = 3;
-       } else {
-               ofil = creat(obj, 0755);
-               if (ofil < 0) {
-                       perror(obj);
-                       pexit(NOSTART);
-               }
-       }
+               errfile += ERR_PATHLEN , howfile += HOW_PATHLEN;
+#      ifdef OBJ
+           if (argv[0][0] == '-' && argv[0][1] == 'o') {
+                   obj = &argv[0][2];
+                   usageis = pixusage;
+                   howfile[HOW_PATHLEN+6] = 'x';
+                   ofil = 3;
+           } else {
+                   ofil = creat(obj, 0755);
+                   if (ofil < 0) {
+                           perror(obj);
+                           pexit(NOSTART);
+                   }
+           }
+#      endif OBJ
        argv++, argc--;
        if (argc == 0) {
                i = fork();
        argv++, argc--;
        if (argc == 0) {
                i = fork();
@@ -97,71 +108,166 @@ main(argc, argv)
                        continue;
                pexit(NOSTART);
        }
                        continue;
                pexit(NOSTART);
        }
-       opt('p') = opt('t') = opt('b') = 1;
-       while (argc > 0) {
+#      ifdef OBJ
+           opt('p') = opt('t') = opt('b') = 1;
+           while (argc > 0) {
+                   cp = argv[0];
+                   if (*cp++ != '-')
+                           break;
+                   while (c = *cp++) switch (c) {
+#ifdef DEBUG
+                           case 'k':
+                           case 'r':
+                           case 'y':
+                                   togopt(c);
+                                   continue;
+                           case 'K':
+                                   yycosts();
+                                   pexit(NOSTART);
+                           case 'A':
+                                   testtrace = TRUE;
+                           case 'F':
+                                   fulltrace = TRUE;
+                           case 'E':
+                                   errtrace = TRUE;
+                                   opt('r')++;
+                                   continue;
+                           case 'U':
+                                   yyunique = 0;
+                                   continue;
+#endif
+                           case 'b':
+                                   opt('b') = 2;
+                                   continue;
+                           case 'i':
+                                   pflist = argv + 1;
+                                   pflstc = 0;
+                                   while (argc > 1) {
+                                           if (dotted(argv[1], 'p'))
+                                                   break;
+                                           pflstc++, argc--, argv++;
+                                   }
+                                   if (pflstc == 0)
+                                           goto usage;
+                                   continue;
+                           case 'l':
+                           case 'n':
+                           case 'p':
+                           case 's':
+                           case 't':
+                           case 'u':
+                           case 'w':
+                                   togopt(c);
+                                   continue;
+                           case 'z':
+                                   monflg = TRUE;
+                                   continue;
+                           default:
+    usage:
+                                   Perror( "Usage", usageis);
+                                   pexit(NOSTART);
+                   }
+                   argc--, argv++;
+           }
+#      endif OBJ
+#      ifdef PC
+           opt( 'b' ) = 1;
+           opt( 'g' ) = 0;
+           opt( 't' ) = 0;
+           opt( 'p' ) = 0;
+           usageis = pcusage;
+           while ( argc > 0 ) {
                cp = argv[0];
                cp = argv[0];
-               if (*cp++ != '-')
-                       break;
-               while (c = *cp++) switch (c) {
+               if ( *cp++ != '-' ) {
+                   break;
+               }
+               c = *cp++;
+               switch( c ) {
 #ifdef DEBUG
 #ifdef DEBUG
-                       case 'c':
-                       case 'r':
-                       case 'y':
-                               togopt(c);
-                               continue;
-                       case 'C':
-                               yycosts();
-                               pexit(NOSTART);
-                       case 'A':
-                               testtrace++;
-                       case 'F':
-                               fulltrace++;
-                       case 'E':
-                               errtrace++;
-                               opt('r')++;
-                               continue;
-                       case 'U':
-                               yyunique = 0;
-                               continue;
-#                      ifdef PPC
-                           case 'P':
-                               ppcdebug++;
-                               continue;
-#                      endif
+                   case 'k':
+                   case 'r':
+                   case 'y':
+                           togopt(c);
+                           break;
+                   case 'K':
+                           yycosts();
+                           pexit(NOSTART);
+                   case 'A':
+                           testtrace = TRUE;
+                           /* and fall through */
+                   case 'F':
+                           fulltrace = TRUE;
+                           /* and fall through */
+                   case 'E':
+                           errtrace = TRUE;
+                           opt('r')++;
+                           break;
+                   case 'U':
+                           yyunique = 0;
+                           break;
 #endif
 #endif
-                       case 'b':
-                               opt('b') = 2;
-                               continue;
-                       case 'i':
-                               pflist = argv + 1;
-                               pflstc = 0;
-                               while (argc > 1) {
-                                       if (dotted(argv[1], 'p'))
-                                               break;
-                                       pflstc++, argc--, argv++;
-                               }
-                               if (pflstc == 0)
-                                       goto usage;
-                               continue;
-                       case 'l':
-                       case 'n':
-                       case 'p':
-                       case 's':
-                       case 't':
-                       case 'u':
-                       case 'w':
-                               togopt(c);
-                               continue;
-                       case 'z':
-                               monflg++;
-                               continue;
-                       default:
+                   case 'b':
+                           opt('b') = 2;
+                           break;
+                   case 'i':
+                           pflist = argv + 1;
+                           pflstc = 0;
+                           while (argc > 1) {
+                                   if (dotted(argv[1], 'p'))
+                                           break;
+                                   pflstc++, argc--, argv++;
+                           }
+                           if (pflstc == 0)
+                                   goto usage;
+                           break;
+                       /*
+                        *      output file for the first pass
+                        */
+                   case 'o':
+                           if ( argc < 2 ) {
+                               goto usage;
+                           }
+                           argv++;
+                           argc--;
+                           pcname = argv[0];
+                           break;      
+                   case 'C':
+                               /*
+                                * since -t is an ld switch, use -C
+                                * to turn on tests
+                                */
+                           togopt( 't' );
+                           break;
+                   case 'g':
+                               /*
+                                *      sdb symbol table
+                                */
+                           togopt( 'g' );
+                           break;
+                   case 'l':
+                   case 's':
+                   case 'u':
+                   case 'w':
+                           togopt(c);
+                           break;
+                   case 'p':
+                               /*
+                                *      -p on the command line means profile
+                                */
+                           profflag = TRUE;
+                           break;
+                   case 'z':
+                           monflg = TRUE;
+                           break;
+                   default:
 usage:
 usage:
-                               Perror( "Usage", usageis);
-                               pexit(NOSTART);
+                           Perror( "Usage", usageis);
+                           pexit(NOSTART);
                }
                }
-               argc--, argv++;
-       }
+               argc--;
+               argv++;
+           }
+#      endif PC
        if (argc != 1)
                goto usage;
        efil = open ( errfile, 0 );
        if (argc != 1)
                goto usage;
        efil = open ( errfile, 0 );
@@ -173,39 +279,30 @@ usage:
                pexit(NOSTART);
        }
        close(0);
                pexit(NOSTART);
        }
        close(0);
-       if ( ( ibuf = fopen ( filename , "r" ) ) == NULL )
+       if ( ( ibuf = fopen( filename , "r" ) ) == NULL )
                perror(filename), pexit(NOSTART);
        ibp = ibuf;
                perror(filename), pexit(NOSTART);
        ibp = ibuf;
-#      ifdef PPC
-           if ( ( ppcstream = fopen( ppcname , "w" ) ) == NULL ) {
-               perror( ppcname );
+#      ifdef PC
+           if ( ( pcstream = fopen( pcname , "w" ) ) == NULL ) {
+               perror( pcname );
                pexit( NOSTART );
            }
                pexit( NOSTART );
            }
-#          ifdef DEBUG
-               if ( ppcdebug ) {
-                   if ( ( ppcdstream = fopen( ppcdname , "w" ) ) == NULL ) {
-                       perror( ppcdname );
-                       pexit( NOSTART );
-                   }
-               }
-#          endif
-           putprintf( "#               compilation of %s" , filename );
-#      endif
+           stabsource( filename );
+#      endif PC
 #      ifdef PTREE
 #          define      MAXpPAGES       16
            if ( ! pCreate( pTreeName , MAXpPAGES ) ) {
                perror( pTreeName );
                pexit( NOSTART );
            }
 #      ifdef PTREE
 #          define      MAXpPAGES       16
            if ( ! pCreate( pTreeName , MAXpPAGES ) ) {
                perror( pTreeName );
                pexit( NOSTART );
            }
-#      endif
-       if ((signal(2, 1) & 01) == 0)
-               signal(2, onintr);
+#      endif PTREE
+       if ( signal( SIGINT , SIG_IGN ) != SIG_IGN )
+               signal( SIGINT , onintr );
        if (opt('l')) {
                opt('n')++;
                yysetfile(filename);
                opt('n')--;
        if (opt('l')) {
                opt('n')++;
                yysetfile(filename);
                opt('n')--;
-       } else
-               lastname = filename;
+       }
        yymain();
        /* No return */
 }
        yymain();
        /* No return */
 }
@@ -238,11 +335,23 @@ pexit(c)
                        write(2, ugh, sizeof ugh);
                case NOSTART:
                case ERRS:
                        write(2, ugh, sizeof ugh);
                case NOSTART:
                case ERRS:
-                       if (ofil > 0)
-                               unlink(obj);
+#                      ifdef OBJ
+                           if (ofil > 0)
+                                   unlink(obj);
+#                      endif OBJ
+#                      ifdef PC
+                           if ( pcstream != NULL ) {
+                               unlink( pcname );
+                           }
+#                      endif PC
                        break;
                case AOK:
                        break;
                case AOK:
-                       pflush();
+#                      ifdef OBJ
+                           pflush();
+#                      endif OBJ
+#                      ifdef PC
+                           puteof();
+#                      endif PC
                        break;
        }
        /*
                        break;
        }
        /*
@@ -265,7 +374,7 @@ pexit(c)
 onintr()
 {
 
 onintr()
 {
 
-       signal(2, 1);
+       signal( SIGINT , SIG_IGN );
        pexit(NOSTART);
 }
 
        pexit(NOSTART);
 }
 
@@ -291,6 +400,12 @@ header()
        if (anyheaders && opt('n'))
                putc( '\f' , stdout );
        anyheaders++;
        if (anyheaders && opt('n'))
                putc( '\f' , stdout );
        anyheaders++;
-       printf("Berkeley Pascal PI -- Version 1.2 (%s)\n\n%s  %s\n\n",
-               version, myctime(&tvec), filename);
+#      ifdef OBJ
+           printf("Berkeley Pascal PI -- Version 2.0 (%s)\n\n%s  %s\n\n",
+                   version, myctime(&tvec), filename);
+#      endif OBJ
+#      ifdef PC
+           printf("Berkeley Pascal PC -- Version 2.0 (%s)\n\n%s  %s\n\n",
+                   version, myctime(&tvec), filename);
+#      endif PC
 }
 }