| 1 | /* Copyright (c) 1979 Regents of the University of California */ |
| 2 | |
| 3 | static char sccsid[] = "@(#)yymain.c 1.1 8/27/80"; |
| 4 | |
| 5 | #include "whoami.h" |
| 6 | #include "0.h" |
| 7 | #include "yy.h" |
| 8 | #include <a.out.h> |
| 9 | #include "objfmt.h" |
| 10 | #include <signal.h> |
| 11 | |
| 12 | short line = 1; |
| 13 | |
| 14 | /* |
| 15 | * Yymain initializes each of the utility |
| 16 | * clusters and then starts the processing |
| 17 | * by calling yyparse. |
| 18 | */ |
| 19 | yymain() |
| 20 | { |
| 21 | |
| 22 | /* |
| 23 | * Initialize the scanner |
| 24 | */ |
| 25 | #ifdef PXP |
| 26 | if (bracket == 0) { |
| 27 | #endif |
| 28 | if (getline() == -1) { |
| 29 | Perror(filename, "No lines in file"); |
| 30 | pexit(NOSTART); |
| 31 | } |
| 32 | #ifdef PXP |
| 33 | } else |
| 34 | yyline = 0; |
| 35 | #endif |
| 36 | |
| 37 | #ifdef PI |
| 38 | # ifdef OBJ |
| 39 | magic(); |
| 40 | # endif OBJ |
| 41 | #endif |
| 42 | /* |
| 43 | * Initialize the clusters |
| 44 | * |
| 45 | initstring(); |
| 46 | */ |
| 47 | inithash(); |
| 48 | inittree(); |
| 49 | #ifdef PI |
| 50 | initnl(); |
| 51 | #endif |
| 52 | |
| 53 | /* |
| 54 | * Process the input |
| 55 | */ |
| 56 | yyparse(); |
| 57 | #ifdef PI |
| 58 | # ifdef OBJ |
| 59 | magic2(); |
| 60 | # endif OBJ |
| 61 | # ifdef DEBUG |
| 62 | dumpnl(0); |
| 63 | # endif |
| 64 | #endif |
| 65 | #ifdef PXP |
| 66 | prttab(); |
| 67 | if (onefile) { |
| 68 | extern int outcol; |
| 69 | |
| 70 | if (outcol) |
| 71 | pchr('\n'); |
| 72 | flush(); |
| 73 | if (eflg) { |
| 74 | writef(2, "File not rewritten because of errors\n"); |
| 75 | pexit(ERRS); |
| 76 | } |
| 77 | signal(SIGHUP, SIG_IGN); |
| 78 | signal(SIGINT, SIG_IGN); |
| 79 | copyfile(); |
| 80 | } |
| 81 | #endif |
| 82 | pexit(eflg ? ERRS : AOK); |
| 83 | } |
| 84 | |
| 85 | #ifdef PXP |
| 86 | copyfile() |
| 87 | { |
| 88 | extern int fout[]; |
| 89 | register int c; |
| 90 | |
| 91 | close(1); |
| 92 | if (creat(firstname, 0644) != 1) { |
| 93 | perror(firstname); |
| 94 | pexit(ERRS); |
| 95 | } |
| 96 | lseek(fout[0], 0l, 0); |
| 97 | while ((c = read(fout[0], &fout[3], 512)) > 0) { |
| 98 | if (write(1, &fout[3], c) != c) { |
| 99 | perror(firstname); |
| 100 | pexit(ERRS); |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | #endif |
| 105 | |
| 106 | static |
| 107 | struct exec magichdr; |
| 108 | |
| 109 | #ifdef PI |
| 110 | #ifdef OBJ |
| 111 | magic() |
| 112 | { |
| 113 | |
| 114 | short buf[HEADER_BYTES / sizeof ( short )]; |
| 115 | unsigned *ubuf = buf; |
| 116 | register int hf, i; |
| 117 | |
| 118 | hf = open(PX_HEADER,0); |
| 119 | if (hf >= 0 && read(hf, buf, HEADER_BYTES) > sizeof(struct exec)) { |
| 120 | magichdr.a_magic = ubuf[0]; |
| 121 | magichdr.a_text = ubuf[1]; |
| 122 | magichdr.a_data = ubuf[2]; |
| 123 | magichdr.a_bss = ubuf[3]; |
| 124 | magichdr.a_syms = ubuf[4]; |
| 125 | magichdr.a_entry = ubuf[5]; |
| 126 | magichdr.a_trsize = ubuf[6]; |
| 127 | magichdr.a_drsize = ubuf[7]; |
| 128 | for (i = 0; i < HEADER_BYTES / sizeof ( short ); i++) |
| 129 | word(buf[i]); |
| 130 | } |
| 131 | close(hf); |
| 132 | } |
| 133 | #endif OBJ |
| 134 | |
| 135 | #ifdef OBJ |
| 136 | magic2() |
| 137 | { |
| 138 | struct pxhdr pxhd; |
| 139 | |
| 140 | if (magichdr.a_magic != 0407) |
| 141 | panic ( "magic2" ); |
| 142 | pflush(); |
| 143 | lseek(ofil, 0l, 0); |
| 144 | magichdr.a_data = ( unsigned ) lc - magichdr.a_text; |
| 145 | magichdr.a_data -= sizeof (struct exec); |
| 146 | write(ofil, &magichdr, sizeof(struct exec)); |
| 147 | pxhd.objsize = ( ( unsigned ) lc) - HEADER_BYTES; |
| 148 | pxhd.maketime = time(0); |
| 149 | pxhd.magicnum = MAGICNUM; |
| 150 | lseek(ofil, ( long ) ( HEADER_BYTES - sizeof ( pxhd ) ) , 0); |
| 151 | write(ofil, &pxhd, sizeof (pxhd)); |
| 152 | } |
| 153 | #endif OBJ |
| 154 | #endif |
| 155 | |
| 156 | #ifdef PXP |
| 157 | writef(i, cp) |
| 158 | { |
| 159 | |
| 160 | write(i, cp, strlen(cp)); |
| 161 | } |
| 162 | #endif |