1: # include <stdio.h>
2: # include "constants.h"
3: # include "globals.h"
4: # include <sccs.h>
5:
6: SCCSID(@(#)getch.c 8.2 1/16/85)
7:
8:
9: /*
10: ** GETCH -- Get a character from the input stream
11: **
12: ** Parameters:
13: ** none
14: **
15: ** Returns:
16: ** the next character on the input stream, or
17: ** the backed up character if there is one.
18: ** EOF_TOK is returned on EOF.
19: **
20: ** Side Effects:
21: ** If a backed up character exists, unloads it.
22: ** *Line_pos is returned if Line_pos != 0
23: ** and no backup character exists. Line_buf
24: ** contains the line from which characters are being
25: ** returned. A call with Line_pos == 0 forces
26: ** reading of the next input line. Yyline is
27: ** incremented when a newline is returned.
28: ** If an EOF is in the middle of a line,
29: ** then a newline will be appended.
30: **
31: ** Compilation Flags:
32: ** xDEBUG -- to test Chardebug for
33: ** returning activity of getch() and backup().
34: */
35:
36:
37: /* initializes peek buffer to be empty */
38: int Peekc [2] = {-1, -1};
39:
40: getch()
41: {
42: register char *cp;
43: register char ch;
44: static eoflag;
45: extern int yyline;
46: extern FILE *In_file;
47:
48: if (Peekc [0] >= 0)
49: {
50: /* have a backed up character */
51: ch = Peekc [0];
52: if (ch == '\n')
53: yyline += 1;
54: Peekc [0] = Peekc [1];
55: Peekc [1] = -1;
56: }
57: else
58: {
59: for ( ; ; )
60: {
61: /* no lookahead character */
62: if (!Line_pos)
63: {
64: if (eoflag)
65: {
66: eoflag = 0;
67:
68: /* try to restore previous file */
69: if (!restoref())
70: return (0);
71: # ifdef xDEBUG
72: if (Chardebug || Lex_debug)
73: printf("include file - pop\n");
74: # endif
75: }
76: for (cp = Line_buf; (*cp = getc(In_file)) != '\n'
77: && *cp != EOF; cp++)
78: {
79: if (cp - Line_buf > sizeof Line_buf - 1)
80: {
81: yysemerr("WARNING : line too long, broken in two\n",
82: 0);
83: break;
84: }
85: }
86: if (*cp == EOF)
87: {
88: eoflag++;
89: if (cp == Line_buf)
90: /* EOF after '\n' */
91: continue;
92: /* EOF in middle of line */
93: *cp = '\n';
94: }
95: Line_pos = Line_buf;
96:
97: /* test for a "#include" line */
98: if (tst_include())
99: {
100: /* Force reloading Line_buf */
101: Line_pos = 0;
102: eoflag = 0;
103: # ifdef xDEBUG
104: if (Chardebug || Lex_debug)
105: printf("include file - push\n");
106: # endif
107: continue;
108: }
109: }
110: cp = Line_pos;
111: if (*cp == '\n')
112: {
113: Line_pos = 0;
114: yyline += 1;
115: }
116: else
117: Line_pos++;
118: ch = *cp;
119: break;
120: }
121: }
122: ch &= I1MASK;
123:
124: # ifdef xDEBUG
125: if (Chardebug)
126: printf("getch - returning '%c'.\n", ch);
127: # endif
128:
129: return (ch);
130: }
131:
132: /*
133: ** BACKUP -- Back up a character on the input stream.
134: ** Backs up a single character into Peekc.
135: **
136: ** Parameters:
137: ** ch - character to back up
138: **
139: ** Returns:
140: ** none
141: **
142: ** Side Effects:
143: ** pushes Peekc [0] to Peekc [1].
144: ** sets Peekc [0] to backed up character.
145: */
146:
147: backup(ch)
148: char ch;
149: {
150: extern int yyline;
151:
152:
153: # ifdef xDEBUG
154: if (Chardebug)
155: printf("backed up : '%c'\n", ch);
156: # endif
157:
158: if (Peekc [1] >= 0)
159: syserr("backup buffer overflow on line %d, \"%c%c%c\".",
160: yyline, Peekc [0], Peekc [1], ch);
161:
162: Peekc [1] = Peekc [0];
163: if ((Peekc [0] = ch & I1MASK) == '\n')
164: --yyline;
165: }
Defined functions
getch
defined in line
40; used 16 times
Defined variables
Peekc
defined in line
38; used 11 times