|
|
1.1 ! root 1: /* ! 2: * lexio.c 1.4 ! 3: * ! 4: * Modified Lexical Analyzer I/O Package for Spreadsheet Program `vis' ! 5: * ! 6: * A. F. Gettier ! 7: * Bell Laboratories ! 8: * Update made 11/1/82 11:12:16 ! 9: * Retrieved 11/15/82 13:22:30 ! 10: */ ! 11: #include <stdio.h> ! 12: #include "curses.h" ! 13: #include "vis.h" ! 14: ! 15: extern struct qheader Fixup, Depend; ! 16: extern int COLS, LINES; ! 17: ! 18: enum states { FIRST, FFILE, FPEND, OTHER }; ! 19: ! 20: char Inline[128]; ! 21: ! 22: static enum states lstate; ! 23: static char *line=0, unbuf[128]; ! 24: static char buffer[128]; ! 25: static int uncnt=0, first=TRUE; ! 26: static FILE *fp; ! 27: ! 28: /* ! 29: * Start lex up for this input line ! 30: * This includes passing the initial line to the ! 31: * lexical analyzer ! 32: */ ! 33: void ! 34: startlex( inbuf ) ! 35: char *inbuf; ! 36: { ! 37: lexinit(); ! 38: (void)strcpy( buffer, inbuf ); ! 39: lstate = FIRST; ! 40: } ! 41: ! 42: /* ! 43: * Reset lex for next line ! 44: * This routine allows lex to restart as though ! 45: * it had never been run ! 46: */ ! 47: void ! 48: lexinit() ! 49: { ! 50: /* set the current line as empty */ ! 51: ! 52: Inline[0] = '\0'; ! 53: line = 0; ! 54: ! 55: /* empty the unput buffer */ ! 56: ! 57: uncnt = 0; ! 58: ! 59: /* flush any fixups */ ! 60: ! 61: while ( qread( &Fixup ) != 0 ); ! 62: ! 63: /* turn off the file */ ! 64: ! 65: /* lstate = OTHER; */ ! 66: } ! 67: ! 68: readfile( file ) ! 69: FILE *file; ! 70: { ! 71: fp = file; ! 72: lstate = FPEND; ! 73: } ! 74: ! 75: ! 76: /* ! 77: * New input routine for the lexical analyzer ! 78: */ ! 79: char input() ! 80: { ! 81: int i; ! 82: struct node *t; ! 83: /* ! 84: * return stored up characters if present ! 85: */ ! 86: if ( uncnt > 0 ) { ! 87: return( unbuf[ --uncnt ] ); ! 88: } ! 89: /* ! 90: * Do we need a new buffer? ! 91: */ ! 92: if ( line == 0 ) { ! 93: switch( lstate ) { ! 94: /* ! 95: * First time in, get the input buffer ! 96: */ ! 97: case FIRST: ! 98: (void)strcpy( Inline, cannon( buffer ) ); ! 99: line = buffer; ! 100: lstate = OTHER; ! 101: break; ! 102: /* ! 103: * Get a line from the file ! 104: */ ! 105: case FFILE: ! 106: tryfile: ! 107: if ( fgets( buffer, 128, fp ) == NULL ) { ! 108: lstate = OTHER; ! 109: goto tryother; ! 110: } ! 111: for ( i=0; buffer[i]!='\0'; i++ ) ! 112: if ( buffer[i] == '\n' ) { ! 113: buffer[i] = '\0'; ! 114: break; ! 115: } ! 116: (void)cannon( buffer ); ! 117: (void)strcpy( Inline, buffer ); ! 118: line = buffer; ! 119: lstate = FPEND; ! 120: break; ! 121: case FPEND: ! 122: loop { ! 123: if ( (t = qread( &Fixup )) == 0 ) { ! 124: lstate = FFILE; ! 125: goto tryfile; ! 126: } ! 127: if ( t->def == 0 ) continue; ! 128: strcpy( buffer, t->def ); ! 129: (void)strcpy( Inline, buffer ); ! 130: line = buffer; ! 131: break; ! 132: } ! 133: break; ! 134: case OTHER: ! 135: tryother: ! 136: loop { ! 137: if ( (t = qread( &Fixup )) == 0 ) { ! 138: first = TRUE; ! 139: return( '\0' ); ! 140: } ! 141: if ( t->def == 0 ) continue; ! 142: strcpy( buffer, t->def ); ! 143: (void)strcpy( Inline, buffer ); ! 144: line = buffer; ! 145: break; ! 146: } ! 147: } ! 148: } ! 149: if ( *line == '#' || *line == ';' || *line == '\0' || *line == '\n' ) { ! 150: line = 0; ! 151: return( '\n' ); ! 152: } ! 153: return( *line++ ); ! 154: } ! 155: /* ! 156: * Unput the character ! 157: */ ! 158: unput( chr ) ! 159: char chr; ! 160: { ! 161: unbuf[ uncnt++ ] = chr; ! 162: } ! 163: /* ! 164: * Fold all lower case to upper case ! 165: */ ! 166: char *foldup( string ) ! 167: char *string; ! 168: { ! 169: char *ts; ! 170: /* ! 171: * Fold up ! 172: */ ! 173: ts = string; ! 174: while ( *ts != '\0' ) { ! 175: if ( *ts >= 'a' && *ts <= 'z' ) *ts += 'A' - 'a'; ! 176: ts++; ! 177: } ! 178: return( string ); ! 179: } ! 180: /* ! 181: * Remove all backspaces, general canonical processesing ! 182: */ ! 183: char *cannon( string ) ! 184: char *string; ! 185: { ! 186: char *ts, *ts2; ! 187: /* ! 188: * Take out all backspaces ! 189: */ ! 190: ts2 = ts = string; ! 191: while ( *ts != '\0' ) { ! 192: if ( *ts == '\b' ) { ! 193: if ( ts != string ) ts2--; ! 194: } ! 195: else { ! 196: if ( ts != ts2 ) *ts2 = *ts; ! 197: ts2++; ! 198: } ! 199: ts++; ! 200: } ! 201: if ( ts != ts2 ) *ts2 = '\0'; ! 202: return( string ); ! 203: } ! 204: /* ! 205: * Get the next token and treat it as a value for the previous ! 206: * token, such as the file name for a read ! 207: */ ! 208: char * ! 209: collect() ! 210: { ! 211: static char buff[64]; ! 212: char c; ! 213: int i; ! 214: while ( (c=input()) == ' ' || c == '\t' ); ! 215: if ( c == '\n' ) { ! 216: unput( c ); ! 217: return( 0 ); ! 218: } ! 219: i = 1; ! 220: buff[0] = c; ! 221: while ( (c=input()) > ' ' ) buff[i++] = c; ! 222: buff[i] = '\0'; ! 223: unput( c ); ! 224: return( buff ); ! 225: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.