1: /*	@(#)yyget.c	2.2	SCCS id keyword	*/
   2: /* Copyright (c) 1979 Regents of the University of California */
   3: #
   4: /*
   5:  * pi - Pascal interpreter code translator
   6:  *
   7:  * Charles Haley, Bill Joy UCB
   8:  * Version 1.1 February 1978
   9:  *
  10:  *
  11:  * pxp - Pascal execution profiler
  12:  *
  13:  * Bill Joy UCB
  14:  * Version 1.1 February 1978
  15:  */
  16: 
  17: #include "whoami"
  18: #include "0.h"
  19: #include "yy.h"
  20: 
  21: #ifdef PXP
  22: int yytokcnt;
  23: #endif
  24: 
  25: /*
  26:  * Readch returns the next
  27:  * character from the current
  28:  * input line or -1 on end-of-file.
  29:  * It also maintains yycol for use in
  30:  * printing error messages.
  31:  */
  32: readch()
  33: {
  34:     register i, c;
  35: 
  36:     if (*bufp == '\n' && bufp >= charbuf) {
  37: #ifdef PXP
  38:         yytokcnt = 0;
  39: #endif
  40:         if (getline() < 0)
  41:             return (-1);
  42:     }
  43:     c = *++bufp;
  44:     if (c == '\t')
  45:         yycol = ((yycol + 8) & ~7);
  46:     else
  47:         yycol++;
  48:     return (c);
  49: }
  50: 
  51: /*
  52:  * Definitions of the structures used for the
  53:  * include facility.  The variable "ibp" points
  54:  * to the getc buffer of the current input file.
  55:  * There are "inclev + 1" current include files,
  56:  * and information in saved in the incs stack
  57:  * whenever a new level of include nesting occurs.
  58:  *
  59:  * Ibp in the incs structure saves the pointer
  60:  * to the previous levels input buffer;
  61:  * filename saves the previous file name;
  62:  * Printed saves whether the previous file name
  63:  * had been printed before this nesting occurred;
  64:  * and yyline is the line we were on on the previous file.
  65:  */
  66: 
  67: #define MAXINC  10
  68: 
  69: struct inc {
  70:     FILE    *ibp;
  71:     char    *filename;
  72:     int Printed;
  73:     int yyline;
  74:     int yyLinpt;
  75: } incs[MAXINC];
  76: 
  77: extern  char *printed;
  78: 
  79: int inclev  = -1;
  80: 
  81: #ifdef PXP
  82: /*
  83:  * These initializations survive only if
  84:  * pxp is asked to pretty print one file.
  85:  * Otherwise they are destroyed by the initial
  86:  * call to getline.
  87:  */
  88: char    charbuf[CBSIZE] = " program x(output);\n";
  89: int yycol = 8;
  90: char    *bufp = charbuf;
  91: 
  92: #endif
  93: /*
  94:  * YyLinpt is the seek pointer to the beginning of the
  95:  * next line in the file.
  96:  */
  97: int yyLinpt;
  98: 
  99: /*
 100:  * Getline places the next line
 101:  * from the input stream in the
 102:  * line buffer, returning -1 at YEOF.
 103:  */
 104: getline()
 105: {
 106:     register char *cp;
 107:     register CHAR c;
 108: #ifdef PXP
 109:     static char ateof;
 110: #endif
 111:     register FILE *ib;
 112:     int i;
 113: 
 114:     if (opt('l') && yyprtd == 0)
 115:         yyoutline();
 116:     yyprtd = 0;
 117: top:
 118:     yylinpt = yyLinpt;
 119:     yyline++;
 120:     yyseqid++;
 121:     cp = charbuf;
 122:     ib = ibp;
 123:     i = sizeof charbuf - 1;
 124:     for (;;) {
 125:         c = getc(ib);
 126:         if (c == EOF) {
 127:             if (uninclud())
 128:                 goto top;
 129: #ifdef PXP
 130:             if (ateof == 0 && bracket) {
 131:                 strcpy(charbuf, "begin end.\n");
 132:                 ateof = 1;
 133:                 goto out;
 134:             }
 135: #endif
 136:             bufp = "\n";
 137:             yyline--;
 138:             yyseqid--;
 139:             yyprtd = 1;
 140:             return (-1);
 141:         }
 142:         *cp++ = c;
 143:         if (c == '\n')
 144:             break;
 145:         if (--i == 0) {
 146:             line = yyline;
 147:             error("Input line too long - QUIT");
 148:             pexit(DIED);
 149:         }
 150:     }
 151:     *cp = 0;
 152:     yyLinpt = yylinpt + cp - charbuf;
 153:     if (includ())
 154:         goto top;
 155: #ifdef PXP
 156:     if (cp == &charbuf[1])
 157:         commnl();
 158:     else if (cp == &charbuf[2])
 159:         switch (charbuf[0]) {
 160:             case ' ':
 161:                 commnlbl();
 162:                 break;
 163:             case '\f':
 164:                 commform();
 165:         }
 166: #endif
 167:     if (opt('u'))
 168:         setuflg();
 169: out:
 170:     bufp = charbuf - 1;
 171:     yycol = 8;
 172:     return (1);
 173: }
 174: 
 175: /*
 176:  * Check an input line to see if it is a "#include" pseudo-statement.
 177:  * We allow arbitrary blanks in the line and the file name
 178:  * may be delimited by either 's or "s.  A single semicolon
 179:  * may be placed after the name, but nothing else is allowed
 180:  */
 181: includ()
 182: {
 183:     register char *cp, *dp;
 184:     char ch;
 185:     register struct inc *ip;
 186: 
 187:     cp = charbuf;
 188:     if (*cp++ != '#')
 189:         return (0);
 190:     cp = skipbl(cp);
 191:     for (dp = "include"; *dp; dp++)
 192:         if (*dp != *cp++)
 193:             return (0);
 194:     line = yyline;
 195:     cp = skipbl(cp);
 196:     ch = *cp++;
 197:     if (ch != '\'' && ch != '"') {
 198:         /*
 199: 		 * This should be a yerror flagging the place
 200: 		 * but its not worth figuring out the column.
 201: 		 */
 202:         line = yyline;
 203:         error("Include syntax error - expected ' or \" not found - QUIT");
 204:         pexit(DIED);
 205:     }
 206:     for (dp = cp; *dp != ch; dp++)
 207:         if (*dp == 0) {
 208:             line = yyline;
 209:             error("Missing closing %c for include file name - QUIT", ch);
 210:             pexit(DIED);
 211:         }
 212:     *dp++ = 0;
 213: /*
 214: 	if (*dp == ';')
 215: 		dp++;
 216: 	dp = skipbl(dp);
 217: 	if (*dp != '\n') {
 218: 		line = yyline;
 219: 		error("Garbage after filename in include");
 220: 		pexit(DIED);
 221: 	}
 222: */
 223:     if (!dotted(cp, 'i')) {
 224:         line = yyline;
 225:         error("Include filename must end in .i");
 226:     }
 227: #ifdef PXP
 228:     commincl(cp, ch);
 229:     if (noinclude)
 230:         return (1);
 231: #endif
 232:     inclev++;
 233:     if (inclev > MAXINC) {
 234:         line = yyline;
 235:         error("Absurdly deep include nesting - QUIT");
 236:         pexit(DIED);
 237:     }
 238:     ip = &incs[inclev];
 239:     ip->filename = filename;
 240:     filename = savestr(cp);
 241: /*
 242:  *	left over from before stdio
 243:  *
 244:  *	cp = malloc(518);
 245:  *	if (cp == -1) {
 246:  *		error("Ran out of memory (include)");
 247:  *		pexit(DIED);
 248:  *	}
 249:  *
 250:  */
 251:     ip->ibp = ibp;
 252:     if ( ( ibp = fopen(filename, "r" ) ) == NULL ) {
 253:         perror(filename);
 254:         pexit(DIED);
 255:     }
 256:     if (inpflist(filename)) {
 257: #ifdef PI
 258:         opush('l');
 259: #endif
 260: #ifdef PXP
 261:         opush('z');
 262: #endif
 263:     }
 264:     ip->Printed = printed;
 265:     printed = 0;
 266:     ip->yyline = yyline;
 267:     yyline = 0;
 268:     ip->yyLinpt = yyLinpt;
 269:     yyLinpt = 0;
 270: /*
 271:  *	left over from before stdio
 272:  *
 273:  *	ip->ibp = ibp;
 274:  *	ibp = cp;
 275:  *
 276:  */
 277:     return (1);
 278: }
 279: 
 280: skipbl(ocp)
 281:     char *ocp;
 282: {
 283:     register char *cp;
 284: 
 285:     cp = ocp;
 286:     while (*cp == ' ' || *cp == '\t')
 287:         cp++;
 288:     return (cp);
 289: }
 290: 
 291: 
 292: /*
 293:  * At the end of an include,
 294:  * close the file, free the input buffer,
 295:  * and restore the environment before
 296:  * the "push", including the value of
 297:  * the z option for pxp and the l option for pi.
 298:  */
 299: uninclud()
 300: {
 301:     register struct inc *ip;
 302: 
 303:     if (inclev < 0)
 304:         return (0);
 305: /*
 306:  *	left over from before stdio: becomes fclose ( ibp )
 307:  *
 308:  *	close(ibp[0]);
 309:  *	free(ibp);
 310:  *
 311:  */
 312:     fclose ( ibp );
 313:     ip = &incs[inclev];
 314:     ibp = ip->ibp;
 315:     yyline = ip->yyline;
 316:     if (inpflist(filename)) {
 317: #ifdef PI
 318:         opop('l');
 319: #endif
 320: #ifdef PXP
 321:         opop('z');
 322: #endif
 323:     }
 324:     filename = ip->filename;
 325:     yyLinpt = ip->yyLinpt;
 326:     /*
 327: 	 * If we printed out the nested name,
 328: 	 * then we should print all covered names again.
 329: 	 * If we didn't print out the nested name
 330: 	 * we print the uncovered name only if it
 331: 	 * has not been printed before (unstack).
 332: 	 */
 333:     if (printed) {
 334:         printed = 0;
 335:         while (ip >= incs) {
 336:             ip->Printed = 0;
 337:             ip--;
 338:         }
 339:     } else
 340:         printed = ip->Printed;
 341:     inclev--;
 342:     return (1);
 343: }

Defined functions

getline defined in line 104; used 3 times
includ defined in line 181; used 1 times
readch defined in line 32; used 30 times
skipbl defined in line 280; used 2 times
uninclud defined in line 299; used 1 times

Defined variables

bufp defined in line 90; used 5 times
charbuf defined in line 88; used 11 times
inclev defined in line 79; used 6 times
incs defined in line 75; used 3 times
yyLinpt defined in line 97; used 8 times
yycol defined in line 89; used 4 times
yytokcnt defined in line 22; used 1 times
  • in line 38

Defined struct's

inc defined in line 69; used 4 times

Defined macros

MAXINC defined in line 67; used 2 times
Last modified: 1981-07-10
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 1097
Valid CSS Valid XHTML 1.0 Strict