1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34#include <sys/cdefs.h>
35
36#ifndef lint
37#if 0
38static const char copyright[] =
39"@(#) Copyright (c) 1985, 1993\n\
40 The Regents of the University of California. All rights reserved.\n";
41#endif
42#ifdef __IDSTRING
43__IDSTRING(Berkeley, "@(#)unifdef.c 8.1 (Berkeley) 6/6/93");
44__IDSTRING(NetBSD, "$NetBSD: unifdef.c,v 1.8 2000/07/03 02:51:36 matt Exp $");
45__IDSTRING(dotat, "$dotat: things/unifdef.c,v 1.171 2005/03/08 12:38:48 fanf2 Exp $");
46#endif
47#endif
48#ifdef __FBSDID
49__FBSDID("$FreeBSD: /repoman/r/ncvs/src/usr.bin/unifdef/unifdef.c,v 1.20 2005/05/21 09:55:09 ru Exp $");
50#endif
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66#include <ctype.h>
67#include <err.h>
68#include <stdarg.h>
69#include <stdbool.h>
70#include <stdio.h>
71#include <stdlib.h>
72#include <string.h>
73#include <unistd.h>
74
75size_t strlcpy(char *dst, const char *src, size_t siz);
76
77
78typedef enum {
79 LT_TRUEI,
80 LT_FALSEI,
81 LT_IF,
82 LT_TRUE,
83 LT_FALSE,
84 LT_ELIF,
85 LT_ELTRUE,
86 LT_ELFALSE,
87 LT_ELSE,
88 LT_ENDIF,
89 LT_DODGY,
90 LT_DODGY_LAST = LT_DODGY + LT_ENDIF,
91 LT_PLAIN,
92 LT_EOF,
93 LT_COUNT
94} Linetype;
95
96static char const * const linetype_name[] = {
97 "TRUEI", "FALSEI", "IF", "TRUE", "FALSE",
98 "ELIF", "ELTRUE", "ELFALSE", "ELSE", "ENDIF",
99 "DODGY TRUEI", "DODGY FALSEI",
100 "DODGY IF", "DODGY TRUE", "DODGY FALSE",
101 "DODGY ELIF", "DODGY ELTRUE", "DODGY ELFALSE",
102 "DODGY ELSE", "DODGY ENDIF",
103 "PLAIN", "EOF"
104};
105
106
107typedef enum {
108 IS_OUTSIDE,
109 IS_FALSE_PREFIX,
110 IS_TRUE_PREFIX,
111 IS_PASS_MIDDLE,
112 IS_FALSE_MIDDLE,
113 IS_TRUE_MIDDLE,
114 IS_PASS_ELSE,
115 IS_FALSE_ELSE,
116 IS_TRUE_ELSE,
117 IS_FALSE_TRAILER,
118 IS_COUNT
119} Ifstate;
120
121static char const * const ifstate_name[] = {
122 "OUTSIDE", "FALSE_PREFIX", "TRUE_PREFIX",
123 "PASS_MIDDLE", "FALSE_MIDDLE", "TRUE_MIDDLE",
124 "PASS_ELSE", "FALSE_ELSE", "TRUE_ELSE",
125 "FALSE_TRAILER"
126};
127
128
129typedef enum {
130 NO_COMMENT = false,
131 C_COMMENT,
132 CXX_COMMENT,
133 STARTING_COMMENT,
134 FINISHING_COMMENT,
135 CHAR_LITERAL,
136 STRING_LITERAL
137} Comment_state;
138
139static char const * const comment_name[] = {
140 "NO", "C", "CXX", "STARTING", "FINISHING", "CHAR", "STRING"
141};
142
143
144typedef enum {
145 LS_START,
146 LS_HASH,
147 LS_DIRTY
148} Line_state;
149
150static char const * const linestate_name[] = {
151 "START", "HASH", "DIRTY"
152};
153
154
155
156
157#define MAXDEPTH 64
158#define MAXLINE 4096
159#define MAXSYMS 4096
160
161
162
163
164
165#define EDITSLOP 10
166
167
168
169
170
171static bool complement;
172static bool debugging;
173static bool iocccok;
174static bool killconsts;
175static bool lnblank;
176static bool lnnum;
177static bool symlist;
178static bool text;
179
180static const char *symname[MAXSYMS];
181static const char *value[MAXSYMS];
182static bool ignore[MAXSYMS];
183static int nsyms;
184
185static FILE *input;
186static const char *filename;
187static int linenum;
188
189static char tline[MAXLINE+EDITSLOP];
190static char *keyword;
191
192static Comment_state incomment;
193static Line_state linestate;
194static Ifstate ifstate[MAXDEPTH];
195static bool ignoring[MAXDEPTH];
196static int stifline[MAXDEPTH];
197static int depth;
198static int delcount;
199static bool keepthis;
200
201static int exitstat;
202
203static void addsym(bool, bool, char *);
204static void debug(const char *, ...);
205static void done(void);
206static void error(const char *);
207static int findsym(const char *);
208static void flushline(bool);
209static Linetype getline(void);
210static Linetype ifeval(const char **);
211static void ignoreoff(void);
212static void ignoreon(void);
213static void keywordedit(const char *);
214static void nest(void);
215static void process(void);
216static const char *skipcomment(const char *);
217static const char *skipsym(const char *);
218static void state(Ifstate);
219static int strlcmp(const char *, const char *, size_t);
220static void unnest(void);
221static void usage(void);
222
223#define endsym(c) (!isalpha((unsigned char)c) && !isdigit((unsigned char)c) && c != '_')
224
225
226
227
228int
229main(int argc, char *argv[])
230{
231 int opt;
232
233 while ((opt = getopt(argc, argv, "i:D:U:I:cdeklnst")) != -1)
234 switch (opt) {
235 case 'i':
236
237
238
239
240
241 opt = *optarg++;
242 if (opt == 'D')
243 addsym(true, true, optarg);
244 else if (opt == 'U')
245 addsym(true, false, optarg);
246 else
247 usage();
248 break;
249 case 'D':
250 addsym(false, true, optarg);
251 break;
252 case 'U':
253 addsym(false, false, optarg);
254 break;
255 case 'I':
256
257 break;
258 case 'c':
259 complement = true;
260 break;
261 case 'd':
262 debugging = true;
263 break;
264 case 'e':
265 iocccok = true;
266 break;
267 case 'k':
268 killconsts = true;
269 break;
270 case 'l':
271 lnblank = true;
272 break;
273 case 'n':
274 lnnum = true;
275 break;
276 case 's':
277 symlist = true;
278 break;
279 case 't':
280 text = true;
281 break;
282 default:
283 usage();
284 }
285 argc -= optind;
286 argv += optind;
287 if (argc > 1) {
288 errx(2, "can only do one file");
289 } else if (argc == 1 && strcmp(*argv, "-") != 0) {
290 filename = *argv;
291 input = fopen(filename, "r");
292 if (input == NULL)
293 err(2, "can't open %s", filename);
294 } else {
295 filename = "[stdin]";
296 input = stdin;
297 }
298 process();
299 abort();
300}
301
302static void
303usage(void)
304{
305 fprintf(stderr, "usage: unifdef [-cdeklnst] [-Ipath]"
306 " [-Dsym[=val]] [-Usym] [-iDsym[=val]] [-iUsym] ... [file]\n");
307 exit(2);
308}
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339typedef void state_fn(void);
340
341
342static void Eelif (void) { error("Inappropriate #elif"); }
343static void Eelse (void) { error("Inappropriate #else"); }
344static void Eendif(void) { error("Inappropriate #endif"); }
345static void Eeof (void) { error("Premature EOF"); }
346static void Eioccc(void) { error("Obfuscated preprocessor control line"); }
347
348static void print (void) { flushline(true); }
349static void drop (void) { flushline(false); }
350
351static void Strue (void) { drop(); ignoreoff(); state(IS_TRUE_PREFIX); }
352static void Sfalse(void) { drop(); ignoreoff(); state(IS_FALSE_PREFIX); }
353static void Selse (void) { drop(); state(IS_TRUE_ELSE); }
354
355static void Pelif (void) { print(); ignoreoff(); state(IS_PASS_MIDDLE); }
356static void Pelse (void) { print(); state(IS_PASS_ELSE); }
357static void Pendif(void) { print(); unnest(); }
358
359static void Dfalse(void) { drop(); ignoreoff(); state(IS_FALSE_TRAILER); }
360static void Delif (void) { drop(); ignoreoff(); state(IS_FALSE_MIDDLE); }
361static void Delse (void) { drop(); state(IS_FALSE_ELSE); }
362static void Dendif(void) { drop(); unnest(); }
363
364static void Fdrop (void) { nest(); Dfalse(); }
365static void Fpass (void) { nest(); Pelif(); }
366static void Ftrue (void) { nest(); Strue(); }
367static void Ffalse(void) { nest(); Sfalse(); }
368
369static void Oiffy (void) { if (!iocccok) Eioccc(); Fpass(); ignoreon(); }
370static void Oif (void) { if (!iocccok) Eioccc(); Fpass(); }
371static void Oelif (void) { if (!iocccok) Eioccc(); Pelif(); }
372
373static void Idrop (void) { Fdrop(); ignoreon(); }
374static void Itrue (void) { Ftrue(); ignoreon(); }
375static void Ifalse(void) { Ffalse(); ignoreon(); }
376
377static void Mpass (void) { strncpy(keyword, "if ", 4); Pelif(); }
378static void Mtrue (void) { keywordedit("else\n"); state(IS_TRUE_MIDDLE); }
379static void Melif (void) { keywordedit("endif\n"); state(IS_FALSE_TRAILER); }
380static void Melse (void) { keywordedit("endif\n"); state(IS_FALSE_ELSE); }
381
382static state_fn * const trans_table[IS_COUNT][LT_COUNT] = {
383
384{ Itrue, Ifalse,Fpass, Ftrue, Ffalse,Eelif, Eelif, Eelif, Eelse, Eendif,
385 Oiffy, Oiffy, Fpass, Oif, Oif, Eelif, Eelif, Eelif, Eelse, Eendif,
386 print, done },
387
388{ Idrop, Idrop, Fdrop, Fdrop, Fdrop, Mpass, Strue, Sfalse,Selse, Dendif,
389 Idrop, Idrop, Fdrop, Fdrop, Fdrop, Mpass, Eioccc,Eioccc,Eioccc,Eioccc,
390 drop, Eeof },
391
392{ Itrue, Ifalse,Fpass, Ftrue, Ffalse,Dfalse,Dfalse,Dfalse,Delse, Dendif,
393 Oiffy, Oiffy, Fpass, Oif, Oif, Eioccc,Eioccc,Eioccc,Eioccc,Eioccc,
394 print, Eeof },
395
396{ Itrue, Ifalse,Fpass, Ftrue, Ffalse,Pelif, Mtrue, Delif, Pelse, Pendif,
397 Oiffy, Oiffy, Fpass, Oif, Oif, Pelif, Oelif, Oelif, Pelse, Pendif,
398 print, Eeof },
399
400{ Idrop, Idrop, Fdrop, Fdrop, Fdrop, Pelif, Mtrue, Delif, Pelse, Pendif,
401 Idrop, Idrop, Fdrop, Fdrop, Fdrop, Eioccc,Eioccc,Eioccc,Eioccc,Eioccc,
402 drop, Eeof },
403
404{ Itrue, Ifalse,Fpass, Ftrue, Ffalse,Melif, Melif, Melif, Melse, Pendif,
405 Oiffy, Oiffy, Fpass, Oif, Oif, Eioccc,Eioccc,Eioccc,Eioccc,Pendif,
406 print, Eeof },
407
408{ Itrue, Ifalse,Fpass, Ftrue, Ffalse,Eelif, Eelif, Eelif, Eelse, Pendif,
409 Oiffy, Oiffy, Fpass, Oif, Oif, Eelif, Eelif, Eelif, Eelse, Pendif,
410 print, Eeof },
411
412{ Idrop, Idrop, Fdrop, Fdrop, Fdrop, Eelif, Eelif, Eelif, Eelse, Dendif,
413 Idrop, Idrop, Fdrop, Fdrop, Fdrop, Eelif, Eelif, Eelif, Eelse, Eioccc,
414 drop, Eeof },
415
416{ Itrue, Ifalse,Fpass, Ftrue, Ffalse,Eelif, Eelif, Eelif, Eelse, Dendif,
417 Oiffy, Oiffy, Fpass, Oif, Oif, Eelif, Eelif, Eelif, Eelse, Eioccc,
418 print, Eeof },
419
420{ Idrop, Idrop, Fdrop, Fdrop, Fdrop, Dfalse,Dfalse,Dfalse,Delse, Dendif,
421 Idrop, Idrop, Fdrop, Fdrop, Fdrop, Dfalse,Dfalse,Dfalse,Delse, Eioccc,
422 drop, Eeof }
423
424
425
426};
427
428
429
430
431static void
432done(void)
433{
434 if (incomment)
435 error("EOF in comment");
436 exit(exitstat);
437}
438static void
439ignoreoff(void)
440{
441 if (depth == 0)
442 abort();
443 ignoring[depth] = ignoring[depth-1];
444}
445static void
446ignoreon(void)
447{
448 ignoring[depth] = true;
449}
450static void
451keywordedit(const char *replacement)
452{
453 size_t size = tline + sizeof(tline) - keyword;
454 char *dst = keyword;
455 const char *src = replacement;
456 if (size != 0) {
457 while ((--size != 0) && (*src != '\0'))
458 *dst++ = *src++;
459 *dst = '\0';
460 }
461 print();
462}
463static void
464nest(void)
465{
466 depth += 1;
467 if (depth >= MAXDEPTH)
468 error("Too many levels of nesting");
469 stifline[depth] = linenum;
470}
471static void
472unnest(void)
473{
474 if (depth == 0)
475 abort();
476 depth -= 1;
477}
478static void
479state(Ifstate is)
480{
481 ifstate[depth] = is;
482}
483
484
485
486
487static void
488flushline(bool keep)
489{
490 if (symlist)
491 return;
492 if (keep ^ complement) {
493 if (lnnum && delcount > 0)
494 printf("#line %d\n", linenum);
495 fputs(tline, stdout);
496 delcount = 0;
497 } else {
498 if (lnblank)
499 putc('\n', stdout);
500 exitstat = 1;
501 delcount += 1;
502 }
503}
504
505
506
507
508static void
509process(void)
510{
511 Linetype lineval;
512
513 for (;;) {
514 linenum++;
515 lineval = getline();
516 trans_table[ifstate[depth]][lineval]();
517 debug("process %s -> %s depth %d",
518 linetype_name[lineval],
519 ifstate_name[ifstate[depth]], depth);
520 }
521}
522
523
524
525
526
527
528static Linetype
529getline(void)
530{
531 const char *cp;
532 int cursym;
533 int kwlen;
534 Linetype retval;
535 Comment_state wascomment;
536
537 if (fgets(tline, MAXLINE, input) == NULL)
538 return (LT_EOF);
539 retval = LT_PLAIN;
540 wascomment = incomment;
541 cp = skipcomment(tline);
542 if (linestate == LS_START) {
543 if (*cp == '#') {
544 linestate = LS_HASH;
545 cp = skipcomment(cp + 1);
546 } else if (*cp != '\0')
547 linestate = LS_DIRTY;
548 }
549 if (!incomment && linestate == LS_HASH) {
550 keyword = tline + (cp - tline);
551 cp = skipsym(cp);
552 kwlen = cp - keyword;
553
554 if (strncmp(cp, "\\\n", 2) == 0)
555 Eioccc();
556 if (strlcmp("ifdef", keyword, kwlen) == 0 ||
557 strlcmp("ifndef", keyword, kwlen) == 0) {
558 cp = skipcomment(cp);
559 if ((cursym = findsym(cp)) < 0)
560 retval = LT_IF;
561 else {
562 retval = (keyword[2] == 'n')
563 ? LT_FALSE : LT_TRUE;
564 if (value[cursym] == NULL)
565 retval = (retval == LT_TRUE)
566 ? LT_FALSE : LT_TRUE;
567 if (ignore[cursym])
568 retval = (retval == LT_TRUE)
569 ? LT_TRUEI : LT_FALSEI;
570 }
571 cp = skipsym(cp);
572 } else if (strlcmp("if", keyword, kwlen) == 0)
573 retval = ifeval(&cp);
574 else if (strlcmp("elif", keyword, kwlen) == 0)
575 retval = ifeval(&cp) - LT_IF + LT_ELIF;
576 else if (strlcmp("else", keyword, kwlen) == 0)
577 retval = LT_ELSE;
578 else if (strlcmp("endif", keyword, kwlen) == 0)
579 retval = LT_ENDIF;
580 else {
581 linestate = LS_DIRTY;
582 retval = LT_PLAIN;
583 }
584 cp = skipcomment(cp);
585 if (*cp != '\0') {
586 linestate = LS_DIRTY;
587 if (retval == LT_TRUE || retval == LT_FALSE ||
588 retval == LT_TRUEI || retval == LT_FALSEI)
589 retval = LT_IF;
590 if (retval == LT_ELTRUE || retval == LT_ELFALSE)
591 retval = LT_ELIF;
592 }
593 if (retval != LT_PLAIN && (wascomment || incomment)) {
594 retval += LT_DODGY;
595 if (incomment)
596 linestate = LS_DIRTY;
597 }
598
599 if (linestate == LS_HASH)
600 abort();
601 }
602 if (linestate == LS_DIRTY) {
603 while (*cp != '\0')
604 cp = skipcomment(cp + 1);
605 }
606 debug("parser %s comment %s line",
607 comment_name[incomment], linestate_name[linestate]);
608 return (retval);
609}
610
611
612
613
614
615
616static int op_lt(int a, int b) { return (a < b); }
617static int op_gt(int a, int b) { return (a > b); }
618static int op_le(int a, int b) { return (a <= b); }
619static int op_ge(int a, int b) { return (a >= b); }
620static int op_eq(int a, int b) { return (a == b); }
621static int op_ne(int a, int b) { return (a != b); }
622static int op_or(int a, int b) { return (a || b); }
623static int op_and(int a, int b) { return (a && b); }
624
625
626
627
628
629
630
631
632
633
634
635struct ops;
636
637typedef Linetype eval_fn(const struct ops *, int *, const char **);
638
639static eval_fn eval_table, eval_unary;
640
641
642
643
644
645
646
647
648static const struct ops {
649 eval_fn *inner;
650 struct op {
651 const char *str;
652 int (*fn)(int, int);
653 } op[5];
654} eval_ops[] = {
655 { eval_table, { { "||", op_or } } },
656 { eval_table, { { "&&", op_and } } },
657 { eval_table, { { "==", op_eq },
658 { "!=", op_ne } } },
659 { eval_unary, { { "<=", op_le },
660 { ">=", op_ge },
661 { "<", op_lt },
662 { ">", op_gt } } }
663};
664
665
666
667
668
669
670static Linetype
671eval_unary(const struct ops *ops, int *valp, const char **cpp)
672{
673 const char *cp;
674 char *ep;
675 int sym;
676
677 cp = skipcomment(*cpp);
678 if (*cp == '!') {
679 debug("eval%d !", ops - eval_ops);
680 cp++;
681 if (eval_unary(ops, valp, &cp) == LT_IF)
682 return (LT_IF);
683 *valp = !*valp;
684 } else if (*cp == '(') {
685 cp++;
686 debug("eval%d (", ops - eval_ops);
687 if (eval_table(eval_ops, valp, &cp) == LT_IF)
688 return (LT_IF);
689 cp = skipcomment(cp);
690 if (*cp++ != ')')
691 return (LT_IF);
692 } else if (isdigit((unsigned char)*cp)) {
693 debug("eval%d number", ops - eval_ops);
694 *valp = strtol(cp, &ep, 0);
695 cp = skipsym(cp);
696 } else if (strncmp(cp, "defined", 7) == 0 && endsym(cp[7])) {
697 cp = skipcomment(cp+7);
698 debug("eval%d defined", ops - eval_ops);
699 if (*cp++ != '(')
700 return (LT_IF);
701 cp = skipcomment(cp);
702 sym = findsym(cp);
703 if (sym < 0)
704 return (LT_IF);
705 *valp = (value[sym] != NULL);
706 cp = skipsym(cp);
707 cp = skipcomment(cp);
708 if (*cp++ != ')')
709 return (LT_IF);
710 keepthis = false;
711 } else if (!endsym(*cp)) {
712 debug("eval%d symbol", ops - eval_ops);
713 sym = findsym(cp);
714 if (sym < 0)
715 return (LT_IF);
716 if (value[sym] == NULL)
717 *valp = 0;
718 else {
719 *valp = strtol(value[sym], &ep, 0);
720 if (*ep != '\0' || ep == value[sym])
721 return (LT_IF);
722 }
723 cp = skipsym(cp);
724 keepthis = false;
725 } else {
726 debug("eval%d bad expr", ops - eval_ops);
727 return (LT_IF);
728 }
729
730 *cpp = cp;
731 debug("eval%d = %d", ops - eval_ops, *valp);
732 return (*valp ? LT_TRUE : LT_FALSE);
733}
734
735
736
737
738static Linetype
739eval_table(const struct ops *ops, int *valp, const char **cpp)
740{
741 const struct op *op;
742 const char *cp;
743 int val;
744
745 debug("eval%d", ops - eval_ops);
746 cp = *cpp;
747 if (ops->inner(ops+1, valp, &cp) == LT_IF)
748 return (LT_IF);
749 for (;;) {
750 cp = skipcomment(cp);
751 for (op = ops->op; op->str != NULL; op++)
752 if (strncmp(cp, op->str, strlen(op->str)) == 0)
753 break;
754 if (op->str == NULL)
755 break;
756 cp += strlen(op->str);
757 debug("eval%d %s", ops - eval_ops, op->str);
758 if (ops->inner(ops+1, &val, &cp) == LT_IF)
759 return (LT_IF);
760 *valp = op->fn(*valp, val);
761 }
762
763 *cpp = cp;
764 debug("eval%d = %d", ops - eval_ops, *valp);
765 return (*valp ? LT_TRUE : LT_FALSE);
766}
767
768
769
770
771
772
773static Linetype
774ifeval(const char **cpp)
775{
776 int ret;
777 int val;
778
779 debug("eval %s", *cpp);
780 keepthis = killconsts ? false : true;
781 ret = eval_table(eval_ops, &val, cpp);
782 debug("eval = %d", val);
783 return (keepthis ? LT_IF : ret);
784}
785
786
787
788
789
790
791
792
793static const char *
794skipcomment(const char *cp)
795{
796 if (text || ignoring[depth]) {
797 for (; isspace((unsigned char)*cp); cp++)
798 if (*cp == '\n')
799 linestate = LS_START;
800 return (cp);
801 }
802 while (*cp != '\0')
803
804 if (strncmp(cp, "\\\n", 2) == 0)
805 cp += 2;
806 else switch (incomment) {
807 case NO_COMMENT:
808 if (strncmp(cp, "/\\\n", 3) == 0) {
809 incomment = STARTING_COMMENT;
810 cp += 3;
811 } else if (strncmp(cp, "/*", 2) == 0) {
812 incomment = C_COMMENT;
813 cp += 2;
814 } else if (strncmp(cp, "//", 2) == 0) {
815 incomment = CXX_COMMENT;
816 cp += 2;
817 } else if (strncmp(cp, "\'", 1) == 0) {
818 incomment = CHAR_LITERAL;
819 linestate = LS_DIRTY;
820 cp += 1;
821 } else if (strncmp(cp, "\"", 1) == 0) {
822 incomment = STRING_LITERAL;
823 linestate = LS_DIRTY;
824 cp += 1;
825 } else if (strncmp(cp, "\n", 1) == 0) {
826 linestate = LS_START;
827 cp += 1;
828 } else if (strchr(" \t", *cp) != NULL) {
829 cp += 1;
830 } else
831 return (cp);
832 continue;
833 case CXX_COMMENT:
834 if (strncmp(cp, "\n", 1) == 0) {
835 incomment = NO_COMMENT;
836 linestate = LS_START;
837 }
838 cp += 1;
839 continue;
840 case CHAR_LITERAL:
841 case STRING_LITERAL:
842 if ((incomment == CHAR_LITERAL && cp[0] == '\'') ||
843 (incomment == STRING_LITERAL && cp[0] == '\"')) {
844 incomment = NO_COMMENT;
845 cp += 1;
846 } else if (cp[0] == '\\') {
847 if (cp[1] == '\0')
848 cp += 1;
849 else
850 cp += 2;
851 } else if (strncmp(cp, "\n", 1) == 0) {
852 if (incomment == CHAR_LITERAL)
853 error("unterminated char literal");
854 else
855 error("unterminated string literal");
856 } else
857 cp += 1;
858 continue;
859 case C_COMMENT:
860 if (strncmp(cp, "*\\\n", 3) == 0) {
861 incomment = FINISHING_COMMENT;
862 cp += 3;
863 } else if (strncmp(cp, "*/", 2) == 0) {
864 incomment = NO_COMMENT;
865 cp += 2;
866 } else
867 cp += 1;
868 continue;
869 case STARTING_COMMENT:
870 if (*cp == '*') {
871 incomment = C_COMMENT;
872 cp += 1;
873 } else if (*cp == '/') {
874 incomment = CXX_COMMENT;
875 cp += 1;
876 } else {
877 incomment = NO_COMMENT;
878 linestate = LS_DIRTY;
879 }
880 continue;
881 case FINISHING_COMMENT:
882 if (*cp == '/') {
883 incomment = NO_COMMENT;
884 cp += 1;
885 } else
886 incomment = C_COMMENT;
887 continue;
888 default:
889 abort();
890 }
891 return (cp);
892}
893
894
895
896
897static const char *
898skipsym(const char *cp)
899{
900 while (!endsym(*cp))
901 ++cp;
902 return (cp);
903}
904
905
906
907
908
909static int
910findsym(const char *str)
911{
912 const char *cp;
913 int symind;
914
915 cp = skipsym(str);
916 if (cp == str)
917 return (-1);
918 if (symlist) {
919 printf("%.*s\n", (int)(cp-str), str);
920
921 return (0);
922 }
923 for (symind = 0; symind < nsyms; ++symind) {
924 if (strlcmp(symname[symind], str, cp-str) == 0) {
925 debug("findsym %s %s", symname[symind],
926 value[symind] ? value[symind] : "");
927 return (symind);
928 }
929 }
930 return (-1);
931}
932
933
934
935
936static void
937addsym(bool ignorethis, bool definethis, char *sym)
938{
939 int symind;
940 char *val;
941
942 symind = findsym(sym);
943 if (symind < 0) {
944 if (nsyms >= MAXSYMS)
945 errx(2, "too many symbols");
946 symind = nsyms++;
947 }
948 symname[symind] = sym;
949 ignore[symind] = ignorethis;
950 val = sym + (skipsym(sym) - sym);
951 if (definethis) {
952 if (*val == '=') {
953 value[symind] = val+1;
954 *val = '\0';
955 } else if (*val == '\0')
956 value[symind] = "";
957 else
958 usage();
959 } else {
960 if (*val != '\0')
961 usage();
962 value[symind] = NULL;
963 }
964}
965
966
967
968
969
970static int
971strlcmp(const char *s, const char *t, size_t n)
972{
973 while (n-- && *t != '\0')
974 if (*s != *t)
975 return ((unsigned char)*s - (unsigned char)*t);
976 else
977 ++s, ++t;
978 return ((unsigned char)*s);
979}
980
981
982
983
984static void
985debug(const char *msg, ...)
986{
987 va_list ap;
988
989 if (debugging) {
990 va_start(ap, msg);
991 vwarnx(msg, ap);
992 va_end(ap);
993 }
994}
995
996static void
997error(const char *msg)
998{
999 if (depth == 0)
1000 warnx("%s: %d: %s", filename, linenum, msg);
1001 else
1002 warnx("%s: %d: %s (#if line %d depth %d)",
1003 filename, linenum, msg, stifline[depth], depth);
1004 errx(2, "output may be truncated");
1005}
1006