reader.c (3ffd35307077b69e8e71772d0e2603f8ffbc4804) reader.c (3e794565fce4e0901d6596368adacc38558278c7)
1/* $Id: reader.c,v 1.66 2016/12/02 20:14:34 tom Exp $ */
1/* $Id: reader.c,v 1.68 2017/02/02 01:05:36 tom Exp $ */
2
3#include "defs.h"
4
5/* The line size must be a positive integer. One hundred was chosen */
6/* because few lines in Yacc input grammars exceed 100 characters. */
7/* Note that if a line exceeds LINESIZE characters, the line buffer */
8/* will be expanded to accomodate it. */
9

--- 93 unchanged lines hidden (view full) ---

103 cache_size += CACHE_SIZE;
104 cache = TREALLOC(char, cache, cache_size);
105 NO_SPACE(cache);
106 }
107 cache[cinc] = (char)c;
108 ++cinc;
109}
110
2
3#include "defs.h"
4
5/* The line size must be a positive integer. One hundred was chosen */
6/* because few lines in Yacc input grammars exceed 100 characters. */
7/* Note that if a line exceeds LINESIZE characters, the line buffer */
8/* will be expanded to accomodate it. */
9

--- 93 unchanged lines hidden (view full) ---

103 cache_size += CACHE_SIZE;
104 cache = TREALLOC(char, cache, cache_size);
105 NO_SPACE(cache);
106 }
107 cache[cinc] = (char)c;
108 ++cinc;
109}
110
111static void
112get_line(void)
111typedef enum
113{
112{
114 FILE *f = input_file;
115 int c;
116 int i;
113 ldSPC1,
114 ldSPC2,
115 ldNAME,
116 ldSPC3,
117 ldNUM,
118 ldSPC4,
119 ldFILE,
120 ldOK,
121 ldERR
122}
123LINE_DIR;
117
124
118 if (saw_eof || (c = getc(f)) == EOF)
125/*
126 * Expect this pattern:
127 * /^[[:space:]]*#[[:space:]]*
128 * line[[:space:]]+
129 * [[:digit:]]+
130 * ([[:space:]]*|[[:space:]]+"[^"]+")/
131 */
132static int
133line_directive(void)
134{
135#define UNLESS(what) if (what) { ld = ldERR; break; }
136 int n;
137 int line_1st = -1;
138 int name_1st = -1;
139 int name_end = -1;
140 LINE_DIR ld = ldSPC1;
141 for (n = 0; (ld <= ldOK) && (line[n] != '\0'); ++n)
119 {
142 {
120 if (line)
143 int ch = UCH(line[n]);
144 switch (ld)
121 {
145 {
122 FREE(line);
123 line = 0;
146 case ldSPC1:
147 if (isspace(ch))
148 {
149 break;
150 }
151 else
152 UNLESS(ch != '#');
153 ld = ldSPC2;
154 break;
155 case ldSPC2:
156 if (isspace(ch))
157 {
158 break;
159 }
160 /* FALLTHRU */
161 case ldNAME:
162 UNLESS(strncmp(line + n, "line", 4));
163 n += 4;
164 if (line[n] == '\0')
165 {
166 ld = ldOK;
167 break;
168 }
169 else
170 UNLESS(!isspace(UCH(line[n])));
171 ld = ldSPC3;
172 break;
173 case ldSPC3:
174 if (isspace(ch))
175 {
176 break;
177 }
178 else
179 UNLESS(!isdigit(ch));
180 line_1st = n;
181 ld = ldNUM;
182 /* FALLTHRU */
183 case ldNUM:
184 if (isdigit(ch))
185 {
186 break;
187 }
188 else
189 UNLESS(!isspace(ch));
190 ld = ldSPC4;
191 break;
192 case ldSPC4:
193 if (isspace(ch))
194 {
195 break;
196 }
197 else
198 UNLESS(ch != '"');
199 UNLESS(line[n + 1] == '"');
200 ld = ldFILE;
201 name_1st = n;
202 break;
203 case ldFILE:
204 if (ch != '"')
205 {
206 break;
207 }
208 ld = ldOK;
209 name_end = n;
210 /* FALLTHRU */
211 case ldERR:
212 case ldOK:
213 break;
124 }
214 }
125 cptr = 0;
126 saw_eof = 1;
127 return;
128 }
129
215 }
216
130 if (line == NULL || linesize != (LINESIZE + 1))
217 if (ld == ldOK)
131 {
218 {
132 if (line)
133 FREE(line);
134 linesize = LINESIZE + 1;
135 line = TMALLOC(char, linesize);
136 NO_SPACE(line);
219 size_t need = (size_t) (name_end - name_1st);
220 if (need > input_file_name_len)
221 {
222 input_file_name_len = need;
223 input_file_name = TREALLOC(char, input_file_name, need + 1);
224 NO_SPACE(input_file_name);
225 }
226 memcpy(input_file_name, line + name_1st + 1, need - 1);
227 input_file_name[need - 1] = '\0';
137 }
138
228 }
229
139 i = 0;
140 ++lineno;
141 for (;;)
230 if (ld >= ldNUM && ld < ldERR)
142 {
231 {
143 line[i++] = (char)c;
144 if (c == '\n')
145 break;
146 if ((i + 3) >= linesize)
232 lineno = (int)strtol(line + line_1st, NULL, 10) - 1;
233 }
234
235 return (ld == ldOK);
236#undef UNLESS
237}
238
239static void
240get_line(void)
241{
242 FILE *f = input_file;
243 int c;
244 int i;
245
246 do
247 {
248 if (saw_eof || (c = getc(f)) == EOF)
147 {
249 {
148 linesize += LINESIZE;
149 line = TREALLOC(char, line, linesize);
250 if (line)
251 {
252 FREE(line);
253 line = 0;
254 }
255 cptr = 0;
256 saw_eof = 1;
257 return;
258 }
259
260 if (line == NULL || linesize != (LINESIZE + 1))
261 {
262 if (line)
263 FREE(line);
264 linesize = LINESIZE + 1;
265 line = TMALLOC(char, linesize);
150 NO_SPACE(line);
151 }
266 NO_SPACE(line);
267 }
152 c = getc(f);
153 if (c == EOF)
268
269 i = 0;
270 ++lineno;
271 for (;;)
154 {
272 {
155 line[i++] = '\n';
156 saw_eof = 1;
157 break;
273 line[i++] = (char)c;
274 if (c == '\n')
275 break;
276 if ((i + 3) >= linesize)
277 {
278 linesize += LINESIZE;
279 line = TREALLOC(char, line, linesize);
280 NO_SPACE(line);
281 }
282 c = getc(f);
283 if (c == EOF)
284 {
285 line[i++] = '\n';
286 saw_eof = 1;
287 break;
288 }
158 }
289 }
290 line[i] = '\0';
159 }
291 }
160 line[i] = '\0';
292 while (line_directive());
161 cptr = line;
162 return;
163}
164
165static char *
166dup_line(void)
167{
168 char *p, *s, *t;

--- 3487 unchanged lines hidden ---
293 cptr = line;
294 return;
295}
296
297static char *
298dup_line(void)
299{
300 char *p, *s, *t;

--- 3487 unchanged lines hidden ---