unifdef.c revision 1.7 1 1.7 christos /* $NetBSD: unifdef.c,v 1.7 1998/12/19 23:22:51 christos Exp $ */
2 1.3 jtc
3 1.1 cgd /*
4 1.3 jtc * Copyright (c) 1985, 1993
5 1.3 jtc * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * This code is derived from software contributed to Berkeley by
8 1.1 cgd * Dave Yost.
9 1.1 cgd *
10 1.1 cgd * Redistribution and use in source and binary forms, with or without
11 1.1 cgd * modification, are permitted provided that the following conditions
12 1.1 cgd * are met:
13 1.1 cgd * 1. Redistributions of source code must retain the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer.
15 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 cgd * notice, this list of conditions and the following disclaimer in the
17 1.1 cgd * documentation and/or other materials provided with the distribution.
18 1.1 cgd * 3. All advertising materials mentioning features or use of this software
19 1.1 cgd * must display the following acknowledgement:
20 1.1 cgd * This product includes software developed by the University of
21 1.1 cgd * California, Berkeley and its contributors.
22 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
23 1.1 cgd * may be used to endorse or promote products derived from this software
24 1.1 cgd * without specific prior written permission.
25 1.1 cgd *
26 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 1.1 cgd * SUCH DAMAGE.
37 1.1 cgd */
38 1.1 cgd
39 1.5 lukem #include <sys/cdefs.h>
40 1.1 cgd #ifndef lint
41 1.5 lukem __COPYRIGHT("@(#) Copyright (c) 1985, 1993\n\
42 1.5 lukem The Regents of the University of California. All rights reserved.\n");
43 1.5 lukem #endif /* not lint */
44 1.1 cgd
45 1.1 cgd #ifndef lint
46 1.3 jtc #if 0
47 1.3 jtc static char sccsid[] = "@(#)unifdef.c 8.1 (Berkeley) 6/6/93";
48 1.3 jtc #endif
49 1.7 christos __RCSID("$NetBSD: unifdef.c,v 1.7 1998/12/19 23:22:51 christos Exp $");
50 1.5 lukem #endif /* not lint */
51 1.1 cgd
52 1.1 cgd /*
53 1.1 cgd * unifdef - remove ifdef'ed lines
54 1.1 cgd *
55 1.1 cgd * Warning: will not work correctly if input contains null characters.
56 1.1 cgd *
57 1.1 cgd * Wishlist:
58 1.1 cgd * provide an option which will append the name of the
59 1.1 cgd * appropriate symbol after #else's and #endif's
60 1.1 cgd * provide an option which will check symbols after
61 1.1 cgd * #else's and #endif's to see that they match their
62 1.1 cgd * corresponding #ifdef or #ifndef
63 1.1 cgd */
64 1.1 cgd
65 1.1 cgd #include <stdio.h>
66 1.1 cgd #include <ctype.h>
67 1.1 cgd
68 1.1 cgd #define BSS
69 1.5 lukem FILE *input;
70 1.1 cgd #ifndef YES
71 1.1 cgd #define YES 1
72 1.1 cgd #define NO 0
73 1.5 lukem #endif /* YES */
74 1.6 wsanchez #define C_COMMENT 1
75 1.6 wsanchez #define CXX_COMMENT 2
76 1.1 cgd typedef int Bool;
77 1.1 cgd
78 1.5 lukem char *progname BSS;
79 1.5 lukem char *filename BSS;
80 1.5 lukem char text BSS; /* -t option in effect: this is a text file */
81 1.5 lukem char lnblank BSS; /* -l option in effect: blank deleted lines */
82 1.5 lukem char complement BSS; /* -c option in effect: complement the
83 1.5 lukem * operation */
84 1.1 cgd
85 1.1 cgd #define MAXSYMS 100
86 1.5 lukem char *symname[MAXSYMS] BSS; /* symbol name */
87 1.5 lukem char true[MAXSYMS] BSS; /* -Dsym */
88 1.5 lukem char ignore[MAXSYMS] BSS; /* -iDsym or -iUsym */
89 1.5 lukem char insym[MAXSYMS] BSS; /* state: false, inactive, true */
90 1.5 lukem #define SYM_INACTIVE 0 /* symbol is currently inactive */
91 1.5 lukem #define SYM_FALSE 1 /* symbol is currently false */
92 1.5 lukem #define SYM_TRUE 2 /* symbol is currently true */
93 1.1 cgd
94 1.1 cgd char nsyms BSS;
95 1.5 lukem char incomment BSS; /* inside C comment */
96 1.1 cgd
97 1.1 cgd #define QUOTE_NONE 0
98 1.1 cgd #define QUOTE_SINGLE 1
99 1.1 cgd #define QUOTE_DOUBLE 2
100 1.5 lukem char inquote BSS; /* inside single or double quotes */
101 1.4 jtc int exitstat BSS;
102 1.1 cgd
103 1.5 lukem int error __P((int, int, int));
104 1.5 lukem int findsym __P((char *));
105 1.5 lukem void flushline __P((Bool));
106 1.5 lukem int getlin __P((char *, int, FILE *, int));
107 1.5 lukem int main __P((int, char **));
108 1.5 lukem void pfile __P((void));
109 1.5 lukem void prname __P((void));
110 1.4 jtc char *skipcomment __P((char *));
111 1.4 jtc char *skipquote __P((char *, int));
112 1.1 cgd
113 1.4 jtc int
114 1.5 lukem main(argc, argv)
115 1.5 lukem int argc;
116 1.5 lukem char **argv;
117 1.5 lukem {
118 1.5 lukem char **curarg;
119 1.5 lukem char *cp;
120 1.5 lukem char *cp1;
121 1.5 lukem char ignorethis;
122 1.5 lukem
123 1.5 lukem progname = argv[0][0] ? argv[0] : "unifdef";
124 1.5 lukem
125 1.5 lukem for (curarg = &argv[1]; --argc > 0; curarg++) {
126 1.5 lukem if (*(cp1 = cp = *curarg) != '-')
127 1.5 lukem break;
128 1.5 lukem if (*++cp1 == 'i') {
129 1.5 lukem ignorethis = YES;
130 1.5 lukem cp1++;
131 1.5 lukem } else
132 1.5 lukem ignorethis = NO;
133 1.5 lukem if ((*cp1 == 'D'
134 1.5 lukem || *cp1 == 'U'
135 1.5 lukem )
136 1.5 lukem && cp1[1] != '\0'
137 1.5 lukem ) {
138 1.5 lukem int symind;
139 1.5 lukem
140 1.5 lukem if ((symind = findsym(&cp1[1])) < 0) {
141 1.5 lukem if (nsyms >= MAXSYMS) {
142 1.5 lukem prname();
143 1.5 lukem fprintf(stderr, "too many symbols.\n");
144 1.5 lukem exit(2);
145 1.5 lukem }
146 1.5 lukem symind = nsyms++;
147 1.5 lukem symname[symind] = &cp1[1];
148 1.5 lukem insym[symind] = SYM_INACTIVE;
149 1.5 lukem }
150 1.5 lukem ignore[symind] = ignorethis;
151 1.5 lukem true[symind] = *cp1 == 'D' ? YES : NO;
152 1.5 lukem } else
153 1.5 lukem if (ignorethis)
154 1.5 lukem goto unrec;
155 1.5 lukem else
156 1.5 lukem if (strcmp(&cp[1], "t") == 0)
157 1.5 lukem text = YES;
158 1.5 lukem else
159 1.5 lukem if (strcmp(&cp[1], "l") == 0)
160 1.5 lukem lnblank = YES;
161 1.5 lukem else
162 1.5 lukem if (strcmp(&cp[1], "c") == 0)
163 1.5 lukem complement = YES;
164 1.5 lukem else {
165 1.5 lukem unrec:
166 1.5 lukem prname();
167 1.5 lukem fprintf(stderr, "unrecognized option: %s\n", cp);
168 1.5 lukem goto usage;
169 1.5 lukem }
170 1.1 cgd }
171 1.5 lukem if (nsyms == 0) {
172 1.5 lukem usage:
173 1.5 lukem fprintf(stderr, "\
174 1.1 cgd Usage: %s [-l] [-t] [-c] [[-Dsym] [-Usym] [-iDsym] [-iUsym]]... [file]\n\
175 1.1 cgd At least one arg from [-D -U -iD -iU] is required\n", progname);
176 1.5 lukem exit(2);
177 1.1 cgd }
178 1.5 lukem if (argc > 1) {
179 1.5 lukem prname();
180 1.5 lukem fprintf(stderr, "can only do one file.\n");
181 1.5 lukem } else
182 1.5 lukem if (argc == 1) {
183 1.5 lukem filename = *curarg;
184 1.5 lukem if ((input = fopen(filename, "r")) != NULL) {
185 1.5 lukem pfile();
186 1.5 lukem (void) fclose(input);
187 1.5 lukem } else {
188 1.5 lukem prname();
189 1.5 lukem fprintf(stderr, "can't open ");
190 1.5 lukem perror(*curarg);
191 1.5 lukem }
192 1.5 lukem } else {
193 1.5 lukem filename = "[stdin]";
194 1.5 lukem input = stdin;
195 1.5 lukem pfile();
196 1.5 lukem }
197 1.1 cgd
198 1.5 lukem (void) fflush(stdout);
199 1.5 lukem exit(exitstat);
200 1.1 cgd }
201 1.1 cgd /* types of input lines: */
202 1.1 cgd typedef int Linetype;
203 1.5 lukem #define LT_PLAIN 0 /* ordinary line */
204 1.5 lukem #define LT_TRUE 1 /* a true #ifdef of a symbol known to us */
205 1.5 lukem #define LT_FALSE 2 /* a false #ifdef of a symbol known to us */
206 1.5 lukem #define LT_OTHER 3 /* an #ifdef of a symbol not known to us */
207 1.5 lukem #define LT_IF 4 /* an #ifdef of a symbol not known to us */
208 1.5 lukem #define LT_ELSE 5 /* #else */
209 1.5 lukem #define LT_ENDIF 6 /* #endif */
210 1.5 lukem #define LT_LEOF 7 /* end of file */
211 1.4 jtc Linetype checkline __P((int *));
212 1.1 cgd
213 1.1 cgd typedef int Reject_level;
214 1.5 lukem Reject_level reject BSS; /* 0 or 1: pass thru; 1 or 2: ignore comments */
215 1.1 cgd #define REJ_NO 0
216 1.1 cgd #define REJ_IGNORE 1
217 1.1 cgd #define REJ_YES 2
218 1.4 jtc int doif __P((int, int, Reject_level, int));
219 1.1 cgd
220 1.5 lukem int linenum BSS; /* current line number */
221 1.5 lukem int stqcline BSS; /* start of current coment or quote */
222 1.5 lukem char *errs[] = {
223 1.1 cgd #define NO_ERR 0
224 1.5 lukem "",
225 1.1 cgd #define END_ERR 1
226 1.5 lukem "",
227 1.1 cgd #define ELSE_ERR 2
228 1.5 lukem "Inappropriate else",
229 1.1 cgd #define ENDIF_ERR 3
230 1.5 lukem "Inappropriate endif",
231 1.1 cgd #define IEOF_ERR 4
232 1.5 lukem "Premature EOF in ifdef",
233 1.1 cgd #define CEOF_ERR 5
234 1.5 lukem "Premature EOF in comment",
235 1.1 cgd #define Q1EOF_ERR 6
236 1.5 lukem "Premature EOF in quoted character",
237 1.1 cgd #define Q2EOF_ERR 7
238 1.5 lukem "Premature EOF in quoted string"
239 1.1 cgd };
240 1.1 cgd /* States for inif arg to doif */
241 1.1 cgd #define IN_NONE 0
242 1.1 cgd #define IN_IF 1
243 1.1 cgd #define IN_ELSE 2
244 1.1 cgd
245 1.4 jtc void
246 1.5 lukem pfile()
247 1.1 cgd {
248 1.5 lukem reject = REJ_NO;
249 1.5 lukem (void) doif(-1, IN_NONE, reject, 0);
250 1.5 lukem return;
251 1.1 cgd }
252 1.1 cgd
253 1.1 cgd int
254 1.5 lukem doif(thissym, inif, prevreject, depth)
255 1.5 lukem int thissym; /* index of the symbol who was last ifdef'ed */
256 1.5 lukem int inif; /* YES or NO we are inside an ifdef */
257 1.5 lukem Reject_level prevreject;/* previous value of reject */
258 1.5 lukem int depth; /* depth of ifdef's */
259 1.5 lukem {
260 1.5 lukem Linetype lineval;
261 1.5 lukem Reject_level thisreject;
262 1.5 lukem int doret; /* tmp return value of doif */
263 1.5 lukem int cursym; /* index of the symbol returned by checkline */
264 1.5 lukem int stline; /* line number when called this time */
265 1.5 lukem
266 1.5 lukem stline = linenum;
267 1.5 lukem for (;;) {
268 1.5 lukem switch (lineval = checkline(&cursym)) {
269 1.5 lukem case LT_PLAIN:
270 1.5 lukem flushline(YES);
271 1.5 lukem break;
272 1.5 lukem
273 1.5 lukem case LT_TRUE:
274 1.5 lukem case LT_FALSE:
275 1.5 lukem thisreject = reject;
276 1.5 lukem if (lineval == LT_TRUE)
277 1.5 lukem insym[cursym] = SYM_TRUE;
278 1.5 lukem else {
279 1.5 lukem if (reject != REJ_YES)
280 1.5 lukem reject = ignore[cursym] ? REJ_IGNORE : REJ_YES;
281 1.5 lukem insym[cursym] = SYM_FALSE;
282 1.5 lukem }
283 1.5 lukem if (ignore[cursym])
284 1.5 lukem flushline(YES);
285 1.5 lukem else {
286 1.5 lukem exitstat = 1;
287 1.5 lukem flushline(NO);
288 1.5 lukem }
289 1.5 lukem if ((doret = doif(cursym, IN_IF, thisreject, depth + 1)) != NO_ERR)
290 1.5 lukem return error(doret, stline, depth);
291 1.5 lukem break;
292 1.5 lukem
293 1.5 lukem case LT_IF:
294 1.5 lukem case LT_OTHER:
295 1.5 lukem flushline(YES);
296 1.5 lukem if ((doret = doif(-1, IN_IF, reject, depth + 1)) != NO_ERR)
297 1.5 lukem return error(doret, stline, depth);
298 1.5 lukem break;
299 1.5 lukem
300 1.5 lukem case LT_ELSE:
301 1.5 lukem if (inif != IN_IF)
302 1.5 lukem return error(ELSE_ERR, linenum, depth);
303 1.5 lukem inif = IN_ELSE;
304 1.5 lukem if (thissym >= 0) {
305 1.5 lukem if (insym[thissym] == SYM_TRUE) {
306 1.5 lukem reject = ignore[thissym] ? REJ_IGNORE : REJ_YES;
307 1.5 lukem insym[thissym] = SYM_FALSE;
308 1.5 lukem } else { /* (insym[thissym] ==
309 1.5 lukem * SYM_FALSE) */
310 1.5 lukem reject = prevreject;
311 1.5 lukem insym[thissym] = SYM_TRUE;
312 1.5 lukem }
313 1.5 lukem if (!ignore[thissym]) {
314 1.5 lukem flushline(NO);
315 1.5 lukem break;
316 1.5 lukem }
317 1.5 lukem }
318 1.5 lukem flushline(YES);
319 1.5 lukem break;
320 1.5 lukem
321 1.5 lukem case LT_ENDIF:
322 1.5 lukem if (inif == IN_NONE)
323 1.5 lukem return error(ENDIF_ERR, linenum, depth);
324 1.5 lukem if (thissym >= 0) {
325 1.5 lukem insym[thissym] = SYM_INACTIVE;
326 1.5 lukem reject = prevreject;
327 1.5 lukem if (!ignore[thissym]) {
328 1.5 lukem flushline(NO);
329 1.5 lukem return NO_ERR;
330 1.5 lukem }
331 1.5 lukem }
332 1.5 lukem flushline(YES);
333 1.5 lukem return NO_ERR;
334 1.5 lukem
335 1.5 lukem case LT_LEOF:{
336 1.5 lukem int err;
337 1.5 lukem err = incomment
338 1.5 lukem ? CEOF_ERR
339 1.5 lukem : inquote == QUOTE_SINGLE
340 1.5 lukem ? Q1EOF_ERR
341 1.5 lukem : inquote == QUOTE_DOUBLE
342 1.5 lukem ? Q2EOF_ERR
343 1.5 lukem : NO_ERR;
344 1.5 lukem if (inif != IN_NONE) {
345 1.5 lukem if (err != NO_ERR)
346 1.5 lukem (void) error(err, stqcline, depth);
347 1.5 lukem return error(IEOF_ERR, stline, depth);
348 1.5 lukem } else
349 1.5 lukem if (err != NO_ERR)
350 1.5 lukem return error(err, stqcline, depth);
351 1.5 lukem else
352 1.5 lukem return NO_ERR;
353 1.5 lukem }
354 1.1 cgd }
355 1.1 cgd }
356 1.1 cgd }
357 1.7 christos #define endsym(c) (!isalpha ((unsigned char)c) && !isdigit ((unsigned char)c) && c != '_')
358 1.1 cgd
359 1.1 cgd #define MAXLINE 256
360 1.5 lukem char tline[MAXLINE] BSS;
361 1.1 cgd
362 1.1 cgd Linetype
363 1.5 lukem checkline(cursym)
364 1.5 lukem int *cursym; /* if LT_TRUE or LT_FALSE returned, set this
365 1.5 lukem * to sym index */
366 1.5 lukem {
367 1.5 lukem char *cp;
368 1.5 lukem char *symp;
369 1.5 lukem char *scp;
370 1.5 lukem Linetype retval;
371 1.5 lukem #define KWSIZE 8
372 1.5 lukem char keyword[KWSIZE];
373 1.5 lukem
374 1.5 lukem linenum++;
375 1.5 lukem if (getlin(tline, sizeof tline, input, NO) == EOF)
376 1.5 lukem return LT_LEOF;
377 1.5 lukem
378 1.5 lukem retval = LT_PLAIN;
379 1.5 lukem if (*(cp = tline) != '#'
380 1.5 lukem || incomment
381 1.5 lukem || inquote == QUOTE_SINGLE
382 1.5 lukem || inquote == QUOTE_DOUBLE
383 1.5 lukem )
384 1.5 lukem goto eol;
385 1.5 lukem
386 1.5 lukem cp = skipcomment(++cp);
387 1.5 lukem symp = keyword;
388 1.5 lukem while (!endsym(*cp)) {
389 1.5 lukem *symp = *cp++;
390 1.5 lukem if (++symp >= &keyword[KWSIZE])
391 1.5 lukem goto eol;
392 1.1 cgd }
393 1.5 lukem *symp = '\0';
394 1.1 cgd
395 1.5 lukem if (strcmp(keyword, "ifdef") == 0) {
396 1.5 lukem retval = YES;
397 1.5 lukem goto ifdef;
398 1.5 lukem } else
399 1.5 lukem if (strcmp(keyword, "ifndef") == 0) {
400 1.5 lukem retval = NO;
401 1.5 lukem ifdef:
402 1.5 lukem scp = cp = skipcomment(++cp);
403 1.5 lukem if (incomment) {
404 1.5 lukem retval = LT_PLAIN;
405 1.5 lukem goto eol;
406 1.5 lukem } {
407 1.5 lukem int symind;
408 1.5 lukem
409 1.5 lukem if ((symind = findsym(scp)) >= 0)
410 1.5 lukem retval = (retval ^ true[*cursym = symind])
411 1.5 lukem ? LT_FALSE : LT_TRUE;
412 1.5 lukem else
413 1.5 lukem retval = LT_OTHER;
414 1.5 lukem }
415 1.5 lukem } else
416 1.5 lukem if (strcmp(keyword, "if") == 0)
417 1.5 lukem retval = LT_IF;
418 1.5 lukem else
419 1.5 lukem if (strcmp(keyword, "else") == 0)
420 1.5 lukem retval = LT_ELSE;
421 1.5 lukem else
422 1.5 lukem if (strcmp(keyword, "endif") == 0)
423 1.5 lukem retval = LT_ENDIF;
424 1.5 lukem
425 1.5 lukem eol:
426 1.5 lukem if (!text && reject != REJ_IGNORE)
427 1.5 lukem for (; *cp;) {
428 1.5 lukem if (incomment)
429 1.5 lukem cp = skipcomment(cp);
430 1.5 lukem else
431 1.5 lukem if (inquote == QUOTE_SINGLE)
432 1.5 lukem cp = skipquote(cp, QUOTE_SINGLE);
433 1.5 lukem else
434 1.5 lukem if (inquote == QUOTE_DOUBLE)
435 1.5 lukem cp = skipquote(cp, QUOTE_DOUBLE);
436 1.5 lukem else
437 1.6 wsanchez if (*cp == '/' && (cp[1] == '*' || cp[1] == '/'))
438 1.5 lukem cp = skipcomment(cp);
439 1.5 lukem else
440 1.5 lukem if (*cp == '\'')
441 1.5 lukem cp = skipquote(cp, QUOTE_SINGLE);
442 1.5 lukem else
443 1.5 lukem if (*cp == '"')
444 1.5 lukem cp = skipquote(cp, QUOTE_DOUBLE);
445 1.5 lukem else
446 1.5 lukem cp++;
447 1.5 lukem }
448 1.5 lukem return retval;
449 1.1 cgd }
450 1.1 cgd /*
451 1.1 cgd * Skip over comments and stop at the next charaacter
452 1.1 cgd * position that is not whitespace.
453 1.1 cgd */
454 1.5 lukem char *
455 1.5 lukem skipcomment(cp)
456 1.5 lukem char *cp;
457 1.5 lukem {
458 1.5 lukem if (incomment)
459 1.5 lukem goto inside;
460 1.5 lukem for (;; cp++) {
461 1.5 lukem while (*cp == ' ' || *cp == '\t')
462 1.5 lukem cp++;
463 1.5 lukem if (text)
464 1.5 lukem return cp;
465 1.6 wsanchez if (cp[0] != '/')
466 1.6 wsanchez return cp;
467 1.6 wsanchez
468 1.6 wsanchez if (cp[1] == '*') {
469 1.6 wsanchez if (!incomment) {
470 1.6 wsanchez incomment = C_COMMENT;
471 1.6 wsanchez stqcline = linenum;
472 1.6 wsanchez }
473 1.6 wsanchez } else if (cp[1] == '/') {
474 1.6 wsanchez if (!incomment) {
475 1.6 wsanchez incomment = CXX_COMMENT;
476 1.6 wsanchez stqcline = linenum;
477 1.6 wsanchez }
478 1.6 wsanchez } else
479 1.5 lukem return cp;
480 1.6 wsanchez
481 1.5 lukem cp += 2;
482 1.6 wsanchez inside:
483 1.6 wsanchez if (incomment == C_COMMENT) {
484 1.6 wsanchez for (;;) {
485 1.6 wsanchez for (; *cp != '*'; cp++)
486 1.6 wsanchez if (*cp == '\0')
487 1.6 wsanchez return cp;
488 1.6 wsanchez if (*++cp == '/') {
489 1.6 wsanchez incomment = NO;
490 1.6 wsanchez break;
491 1.6 wsanchez }
492 1.6 wsanchez }
493 1.5 lukem }
494 1.6 wsanchez else if (incomment == CXX_COMMENT) {
495 1.6 wsanchez for (; *cp != '\n'; cp++)
496 1.5 lukem if (*cp == '\0')
497 1.5 lukem return cp;
498 1.6 wsanchez incomment = NO;
499 1.5 lukem }
500 1.1 cgd }
501 1.1 cgd }
502 1.1 cgd /*
503 1.1 cgd * Skip over a quoted string or character and stop at the next charaacter
504 1.1 cgd * position that is not whitespace.
505 1.1 cgd */
506 1.5 lukem char *
507 1.5 lukem skipquote(cp, type)
508 1.5 lukem char *cp;
509 1.5 lukem int type;
510 1.5 lukem {
511 1.5 lukem char qchar;
512 1.5 lukem
513 1.5 lukem qchar = type == QUOTE_SINGLE ? '\'' : '"';
514 1.5 lukem
515 1.5 lukem if (inquote == type)
516 1.5 lukem goto inside;
517 1.5 lukem for (;; cp++) {
518 1.5 lukem if (*cp != qchar)
519 1.5 lukem return cp;
520 1.5 lukem cp++;
521 1.5 lukem inquote = type;
522 1.5 lukem stqcline = linenum;
523 1.5 lukem inside:
524 1.5 lukem for (;; cp++) {
525 1.5 lukem if (*cp == qchar)
526 1.5 lukem break;
527 1.5 lukem if (*cp == '\0' || (*cp == '\\' && *++cp == '\0'))
528 1.5 lukem return cp;
529 1.5 lukem }
530 1.5 lukem inquote = QUOTE_NONE;
531 1.1 cgd }
532 1.1 cgd }
533 1.1 cgd /*
534 1.1 cgd * findsym - look for the symbol in the symbol table.
535 1.1 cgd * if found, return symbol table index,
536 1.1 cgd * else return -1.
537 1.1 cgd */
538 1.1 cgd int
539 1.5 lukem findsym(str)
540 1.5 lukem char *str;
541 1.1 cgd {
542 1.5 lukem char *cp;
543 1.5 lukem char *symp;
544 1.5 lukem int symind;
545 1.5 lukem char chr;
546 1.5 lukem
547 1.5 lukem for (symind = 0; symind < nsyms; ++symind) {
548 1.5 lukem if (insym[symind] == SYM_INACTIVE) {
549 1.5 lukem for (symp = symname[symind], cp = str
550 1.5 lukem ; *symp && *cp == *symp
551 1.5 lukem ; cp++, symp++
552 1.5 lukem )
553 1.5 lukem continue;
554 1.5 lukem chr = *cp;
555 1.5 lukem if (*symp == '\0' && endsym(chr))
556 1.5 lukem return symind;
557 1.5 lukem }
558 1.1 cgd }
559 1.5 lukem return -1;
560 1.1 cgd }
561 1.1 cgd /*
562 1.1 cgd * getlin - expands tabs if asked for
563 1.1 cgd * and (if compiled in) treats form-feed as an end-of-line
564 1.1 cgd */
565 1.1 cgd int
566 1.5 lukem getlin(line, maxline, inp, expandtabs)
567 1.5 lukem char *line;
568 1.5 lukem int maxline;
569 1.5 lukem FILE *inp;
570 1.5 lukem int expandtabs;
571 1.5 lukem {
572 1.5 lukem int tmp;
573 1.5 lukem int num;
574 1.5 lukem int chr;
575 1.1 cgd #ifdef FFSPECIAL
576 1.5 lukem static char havechar = NO; /* have leftover char from last time */
577 1.5 lukem static char svchar BSS;
578 1.5 lukem #endif /* FFSPECIAL */
579 1.1 cgd
580 1.5 lukem num = 0;
581 1.1 cgd #ifdef FFSPECIAL
582 1.5 lukem if (havechar) {
583 1.5 lukem havechar = NO;
584 1.5 lukem chr = svchar;
585 1.5 lukem goto ent;
586 1.5 lukem }
587 1.5 lukem #endif /* FFSPECIAL */
588 1.5 lukem while (num + 8 < maxline) { /* leave room for tab */
589 1.5 lukem chr = getc(inp);
590 1.5 lukem if (isprint(chr)) {
591 1.1 cgd #ifdef FFSPECIAL
592 1.5 lukem ent:
593 1.5 lukem #endif /* FFSPECIAL */
594 1.5 lukem *line++ = chr;
595 1.5 lukem num++;
596 1.5 lukem } else
597 1.5 lukem switch (chr) {
598 1.5 lukem case EOF:
599 1.5 lukem return EOF;
600 1.5 lukem
601 1.5 lukem case '\t':
602 1.5 lukem if (expandtabs) {
603 1.5 lukem num += tmp = 8 - (num & 7);
604 1.5 lukem do
605 1.5 lukem *line++ = ' ';
606 1.5 lukem while (--tmp);
607 1.5 lukem break;
608 1.5 lukem }
609 1.5 lukem default:
610 1.5 lukem *line++ = chr;
611 1.5 lukem num++;
612 1.5 lukem break;
613 1.5 lukem
614 1.5 lukem case '\n':
615 1.5 lukem *line = '\n';
616 1.5 lukem num++;
617 1.5 lukem goto end;
618 1.1 cgd
619 1.1 cgd #ifdef FFSPECIAL
620 1.5 lukem case '\f':
621 1.5 lukem if (++num == 1)
622 1.5 lukem *line = '\f';
623 1.5 lukem else {
624 1.5 lukem *line = '\n';
625 1.5 lukem havechar = YES;
626 1.5 lukem svchar = chr;
627 1.5 lukem }
628 1.5 lukem goto end;
629 1.5 lukem #endif /* FFSPECIAL */
630 1.5 lukem }
631 1.5 lukem }
632 1.5 lukem end:
633 1.5 lukem *++line = '\0';
634 1.5 lukem return num;
635 1.1 cgd }
636 1.1 cgd
637 1.4 jtc void
638 1.5 lukem flushline(keep)
639 1.5 lukem Bool keep;
640 1.1 cgd {
641 1.5 lukem if ((keep && reject != REJ_YES) ^ complement) {
642 1.5 lukem char *line = tline;
643 1.5 lukem FILE *out = stdout;
644 1.5 lukem char chr;
645 1.5 lukem
646 1.5 lukem while ((chr = *line++) != 0)
647 1.5 lukem putc(chr, out);
648 1.5 lukem } else
649 1.5 lukem if (lnblank)
650 1.5 lukem putc('\n', stdout);
651 1.5 lukem return;
652 1.1 cgd }
653 1.1 cgd
654 1.4 jtc void
655 1.5 lukem prname()
656 1.1 cgd {
657 1.5 lukem fprintf(stderr, "%s: ", progname);
658 1.5 lukem return;
659 1.1 cgd }
660 1.1 cgd
661 1.1 cgd int
662 1.5 lukem error(err, line, depth)
663 1.5 lukem int err; /* type of error & index into error string
664 1.5 lukem * array */
665 1.5 lukem int line; /* line number */
666 1.5 lukem int depth; /* how many ifdefs we are inside */
667 1.1 cgd {
668 1.5 lukem if (err == END_ERR)
669 1.5 lukem return err;
670 1.1 cgd
671 1.5 lukem prname();
672 1.1 cgd
673 1.1 cgd #ifndef TESTING
674 1.5 lukem fprintf(stderr, "Error in %s line %d: %s.\n", filename, line, errs[err]);
675 1.5 lukem #else /* TESTING */
676 1.5 lukem fprintf(stderr, "Error in %s line %d: %s. ", filename, line, errs[err]);
677 1.5 lukem fprintf(stderr, "ifdef depth: %d\n", depth);
678 1.5 lukem #endif /* TESTING */
679 1.1 cgd
680 1.5 lukem exitstat = 2;
681 1.5 lukem return depth > 1 ? IEOF_ERR : END_ERR;
682 1.1 cgd }
683