home *** CD-ROM | disk | FTP | other *** search
/ minnie.tuhs.org / unixen.tar / unixen / PDP-11 / Distributions / ucb / spencer_2bsd.tar.gz / 2bsd.tar / src / pi0 / main.c < prev    next >
C/C++ Source or Header  |  1980-02-17  |  5KB  |  260 lines

  1. /* Copyright (c) 1979 Regents of the University of California */
  2. #
  3. /*
  4.  * pi - Pascal interpreter code translator
  5.  *
  6.  * Charles Haley, Bill Joy UCB
  7.  * Version 1.2 January 1979
  8.  */
  9.  
  10. #include "0.h"
  11.  
  12. /*
  13.  * This program is described in detail in the "PI 1.0 Implementation Notes"
  14.  *
  15.  * This version of pi has been in use at Berkeley since May 1977
  16.  * and is very stable, except for the syntactic error recovery which
  17.  * has just been written.  Please report any problems with the error
  18.  * recovery to the second author at the address given in the file
  19.  * READ_ME.  The second author takes full responsibility for any bugs
  20.  * in the syntactic error recovery.
  21.  */
  22.  
  23. char    piusage[]    "pi [ -blnpstuw ] [ -i file ... ] name.p";
  24. char    pixusage[]    "pix [ -blnpstuw ] [ -i file ... ] name.p [ arg ... ]";
  25.  
  26. char    *usageis    piusage;
  27. char    *obj        "obj";
  28. /*
  29.  * Be careful changing errfile and howfile.
  30.  * There are the "magic" constants 9 and 15 immediately below.
  31.  */
  32. char    *errfile    "/usr/lib/pi1:2strings";
  33. char    *howfile    "/usr/lib/how_pi\0";
  34.  
  35. int    onintr();
  36.  
  37. extern    int ibuf[259];
  38.  
  39. extern    char *lastname;
  40.  
  41. /*
  42.  * Main program for pi.
  43.  * Process options, then call yymain
  44.  * to do all the real work.
  45.  */
  46. main(argc, argv)
  47.     int argc;
  48.     char *argv[];
  49. {
  50.     extern char *PI1;
  51.     register char *cp;
  52.     register c;
  53.     int i;
  54.  
  55.     cp = argv[0];
  56.     if (cp[0] == 'a')
  57.         errfile =+ 9, howfile =+ 9;
  58. #ifdef PC0
  59.     if (cp[1] == 'c')
  60.         PI1 = "/usr/lib/pc1";
  61. #endif
  62.     if (cp[0] == 'x')
  63.         errfile = "/usr/lib/xpi_strings";
  64.     if (argv[0][0] == '-' && argv[0][1] == 'o') {
  65.         obj = &argv[0][2];
  66.         usageis = pixusage;
  67.         howfile[15] = 'x';
  68.         ofil = 3;
  69.     } else {
  70.         ofil = creat(obj, 0755);
  71.         if (ofil < 0) {
  72.             perror(obj);
  73.             pexit(NOSTART);
  74.         }
  75.     }
  76.     argv++, argc--;
  77.     if (argc == 0) {
  78.         i = fork();
  79.         if (i == -1)
  80.             goto usage;
  81.         if (i == 0) {
  82.             execl("/bin/cat", "cat", howfile, 0);
  83.             goto usage;
  84.         }
  85.         while (wait(&i) != -1)
  86.             continue;
  87.         pexit(NOSTART);
  88.     }
  89.     opt('p') = opt('t') = opt('b') = 1;
  90.     while (argc > 0) {
  91.         cp = argv[0];
  92.         if (*cp++ != '-')
  93.             break;
  94.         while (c = *cp++) switch (c) {
  95. #ifdef DEBUG
  96.             case 'r':
  97.                 togopt(c);
  98.                 continue;
  99.             case 'C':
  100.                 yycosts();
  101.                 pexit(NOSTART);
  102.             case 'A':
  103.                 testtrace++;
  104.             case 'F':
  105.                 fulltrace++;
  106.             case 'E':
  107.                 errtrace++;
  108.                 opt('r')++;
  109.                 continue;
  110.             case 'U':
  111.                 yyunique = 0;
  112.                 continue;
  113.             case 'h':
  114.                 hp21mx++;
  115.                 continue;
  116. #endif
  117.             case 'b':
  118.                 opt('b') = 2;
  119.                 continue;
  120.             case 'i':
  121.                 pflist = argv + 1;
  122.                 pflstc = 0;
  123.                 while (argc > 1) {
  124.                     if (dotted(argv[1], 'p'))
  125.                         break;
  126.                     pflstc++, argc--, argv++;
  127.                 }
  128.                 if (pflstc == 0)
  129.                     goto usage;
  130.                 continue;
  131.             case 'c':
  132.             case 'd':
  133. #ifdef PC0
  134.             case 'f':
  135. #endif
  136.             case 'l':
  137.             case 'n':
  138.             case 'p':
  139.             case 's':
  140.             case 't':
  141.             case 'u':
  142.             case 'w':
  143.             case 'y':
  144.             case 'z':
  145.                 togopt(c);
  146.                 continue;
  147.             default:
  148. usage:
  149.                 Perror( "Usage", usageis);
  150.                 pexit(NOSTART);
  151.         }
  152.         argc--, argv++;
  153.     }
  154.     if (argc != 1)
  155.         goto usage;
  156.     efil = open(errfile, 0);
  157.     if (efil < 0)
  158.         perror(errfile), pexit(NOSTART);
  159.     filename = argv[0];
  160.     if (!dotted(filename, 'p')) {
  161.         Perror(filename, "Name must end in '.p'");
  162.         pexit(NOSTART);
  163.     }
  164.     close(0);
  165.     if (fopen(filename, ibuf) < 0)
  166.         perror(filename), pexit(NOSTART);
  167.     if ((signal(2, 1) & 01) == 0)
  168.         signal(2, onintr);
  169.     if (opt('l')) {
  170.         opt('n')++;
  171.         yysetfile(filename);
  172.         opt('n')--;
  173.     } else
  174.         lastname = filename;
  175.     yymain();
  176.     /* No return */
  177. }
  178.  
  179. /*
  180.  * Buffer for putchar
  181.  */
  182. char    pcbuf[128];
  183. char    *pcbp pcbuf;
  184.  
  185. /*
  186.  * Line buffered putchar for pi.
  187.  */
  188. putchar(c)
  189.     char c;
  190. {
  191.  
  192.     *pcbp++ = c;
  193.     if (c == '\n' || pcbp == &pcbuf[sizeof pcbuf-1]) {
  194.         write(1, &pcbuf, pcbp-pcbuf);
  195.         pcbp = pcbuf;
  196.     }
  197. }
  198.  
  199. char    ugh[]    "Fatal error in pi\n";
  200. /*
  201.  * Exit from the Pascal system.
  202.  * We throw in an ungraceful termination
  203.  * message if c > 1 indicating a severe
  204.  * error such as running out of memory
  205.  * or an internal inconsistency.
  206.  */
  207. pexit(c)
  208.     int c;
  209. {
  210.  
  211.     if (opt('l') && c != DIED && c != NOSTART)
  212.         while (getline() != -1)
  213.             continue;
  214.     yyflush();
  215.     switch (c) {
  216.         case DIED:
  217.             write(2, ugh, sizeof ugh);
  218.         case NOSTART:
  219.         case ERRS:
  220.             if (ofil > 0)
  221.                 unlink(obj);
  222.             send(RKILL);
  223.             break;
  224.         case AOK:
  225.             break;
  226.     }
  227.     exit(c);
  228. }
  229.  
  230. onintr()
  231. {
  232.  
  233.     signal(2, 1);
  234.     pexit(NOSTART);
  235. }
  236.  
  237. /*
  238.  * Get an error message from the error message file
  239.  */
  240. geterr(seekpt, buf)
  241.     int seekpt;
  242.     char *buf;
  243. {
  244.  
  245.     if (seek(efil, seekpt, 0) || read(efil, buf, 256) <= 0)
  246.         perror(errfile), pexit(DIED);
  247. }
  248.  
  249. header()
  250. {
  251.     extern char version[];
  252.     static char anyheaders;
  253.  
  254.     gettime();
  255.     if (anyheaders && opt('n'))
  256.         putchar('\f');
  257.     anyheaders++;
  258.     printf("Berkeley Pascal PI -- Version 1.2 (%s)\n\n%s  %s\n\n", version, myctime(tvec), filename);
  259. }
  260.