1: #
   2: # include   <stdio.h>
   3: 
   4: # include   "constants.h"
   5: # include       "globals.h"
   6: 
   7: /*
   8: **  GETCH.C -- I/O manipulation routines
   9: **
  10: **	Defines:
  11: **		getch()
  12: **		backup()
  13: **		int Peekc[2]  -- 2 char stack, if [0]  != -1, the character to
  14: **			be returned on the next call to getch().
  15: **		char *Line_pos -- if != 0 *Line_pos is next
  16: **			character that should be returned.
  17: **		char Line_buf [MAXSTRING + 1] -- holds current
  18: **			input line's text.
  19: **
  20: **	Files:
  21: **		globals.h
  22: **
  23: **	Compilation Flags:
  24: **		xDEBUG -- to test Chardebug for
  25: **			returning activity of getch() and backup().
  26: **
  27: **	History:
  28: **		4/20/78 - written (marc)
  29: **		9/5/78 -- Peekc made Peekc [2] to allow correct
  30: **			processing in eat_display() [display.c]
  31: **			Also include processing put below this
  32: **			abstraction level
  33: **
  34: */
  35: 
  36: /*
  37: **  GETCH -- Get a character from the input stream
  38: **
  39: **	Parameters:
  40: **		none
  41: **
  42: **	Returns:
  43: **		the next character on the input stream, or
  44: **		the backed up character if there is one.
  45: **		EOF_TOK is returned on EOF.
  46: **
  47: **	Side Effects:
  48: **		If a backed up character exists, unloads it.
  49: **		*Line_pos is returned if Line_pos != 0
  50: **		and no backup character exists. Line_buf
  51: **		contains the line from which characters are being
  52: **		returned. A call with Line_pos == 0 forces
  53: **		reading of the next input line. Yyline is
  54: **		incremented when a newline is returned.
  55: **		If an EOF is in the middle of a line,
  56: **		then a newline will be appended.
  57: **
  58: **	Requires:
  59: **		getc() - from utility library
  60: **		tst_include() - [include.c]
  61: **
  62: **	Diagnostics:
  63: **		"WARNING : line %d too long, broken in two\n" --
  64: **			a input line may contain at most MAXSTRING
  65: **			characters, including the terminating
  66: **			newline.
  67: **
  68: **	Called By:
  69: **		all lexical analisys routines
  70: **		include() looks at Line_buf and manipulates
  71: **		Line_pos to skip the "#include"d lines.
  72: **
  73: **	History:
  74: **		4/20/78 - written (marc)
  75: **		9/5/78 -- modified (marc)
  76: */
  77: 
  78: 
  79: /* initializes peek buffer to be empty */
  80: int     Peekc [2]   = {-1, -1};
  81: 
  82: getch()
  83: {
  84:     register char   *cp;
  85:     register char   ch;
  86:     static      eoflag;
  87:     extern int  yyline;
  88:     extern FILE     *In_file;
  89: 
  90:     if (Peekc [0] >= 0)
  91:     {
  92:         /* have a backed up character */
  93:         ch = Peekc [0];
  94:         if (ch == '\n')
  95:             yyline += 1;
  96:         Peekc [0] = Peekc [1];
  97:         Peekc [1] = -1;
  98:     }
  99:     else
 100:     {
 101:         for ( ; ; )
 102:         {
 103:             /* no lookahead character */
 104:             if (!Line_pos)
 105:             {
 106:                 if (eoflag)
 107:                 {
 108:                     eoflag = 0;
 109: 
 110:                     /* try to restore previous file */
 111:                     if (!restoref())
 112:                         return (0);
 113: #					ifdef xDEBUG
 114:                     if (Chardebug || Lex_debug)
 115:                         printf("include file - pop\n");
 116: #					endif
 117:                 }
 118:                 for (cp = Line_buf; (*cp = getc(In_file)) != '\n'
 119:                             && *cp != EOF; cp++)
 120:                 {
 121:                     if (cp - Line_buf > sizeof Line_buf - 1)
 122:                     {
 123:                         printf("WARNING : line %d too long, broken in two\n",
 124:                         yyline);
 125:                         break;
 126:                     }
 127:                 }
 128:                 if (*cp == EOF)
 129:                 {
 130:                     eoflag++;
 131:                     if (cp == Line_buf)
 132:                         /* EOF after '\n' */
 133:                         continue;
 134:                     /* EOF in middle of line */
 135:                     *cp = '\n';
 136:                 }
 137:                 Line_pos = Line_buf;
 138: 
 139:                 /* test for a "#include" line */
 140:                 if (tst_include())
 141:                 {
 142:                     /* Force reloading Line_buf */
 143:                     Line_pos = 0;
 144:                     eoflag = 0;
 145: #					ifdef xDEBUG
 146:                     if (Chardebug || Lex_debug)
 147:                         printf("include file - push\n");
 148: #					endif
 149:                     continue;
 150:                 }
 151:             }
 152:             cp =  Line_pos;
 153:             if (*cp == '\n')
 154:             {
 155:                 Line_pos = 0;
 156:                 yyline += 1;
 157:             }
 158:             else
 159:                 Line_pos++;
 160:             ch = *cp;
 161:             break;
 162:         }
 163:     }
 164:     ch &= 0377;
 165: 
 166: #	ifdef xDEBUG
 167:     if (Chardebug)
 168:         printf("getch - returning '%c'.\n", ch);
 169: #	endif
 170: 
 171:     return (ch);
 172: }
 173: 
 174: /*
 175: **  BACKUP -- Back up a character on the input stream.
 176: **	Backs up a single character into Peekc.
 177: **
 178: **	Parameters:
 179: **		ch - character to back up
 180: **
 181: **	Returns:
 182: **		none
 183: **
 184: **	Side Effects:
 185: **		pushes Peekc [0] to Peekc [1].
 186: **		sets Peekc [0] to backed up character.
 187: **
 188: **	Requires:
 189: **		Peekc [2]  -- integers.
 190: **
 191: **	Called By:
 192: **		most lexical analysis routines.
 193: **
 194: **	Syserrs:
 195: **		"backup buffer overflow on line %d, \"%c%c\"." -
 196: **			can only back up two character.
 197: **	History:
 198: **		4/20/78 - written (marc)
 199: **		9/5/78 -- added on backup character (marc)
 200: **
 201: */
 202: 
 203: backup(ch)
 204: char        ch;
 205: {
 206:     extern int  yyline;
 207: 
 208: 
 209: #	ifdef xDEBUG
 210:     if (Chardebug)
 211:         printf("backed up : '%c'\n", ch);
 212: #	endif
 213: 
 214:     if (Peekc [1] >= 0)
 215:         syserr("backup buffer overflow on line %d, \"%c%c%c\".",
 216:         yyline, Peekc [0], Peekc [1], ch);
 217: 
 218:     Peekc [1] = Peekc [0];
 219:     if ((Peekc [0] = ch & 0377) == '\n')
 220:         --yyline;
 221: }
Last modified: 1995-02-05
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 3246
Valid CSS Valid XHTML 1.0 Strict