Lines Matching +full:input +full:- +full:depth
2 * Copyright (c) 2002 - 2020 Tony Finch <dot@dotat.at>
27 * unifdef - remove ifdef'ed lines
31 * of unifdef carried the 4-clause BSD copyright licence. None of its code
54 /* types of input lines: */
84 #define linetype_if2elif(lt) ((Linetype)(lt - LT_IF + LT_ELIF))
91 IS_TRUE_PREFIX, /* first non-false #(el)if is true */
92 IS_PASS_MIDDLE, /* first non-false #(el)if is unknown */
114 STARTING_COMMENT, /* just after slash-backslash-newline */
115 FINISHING_COMMENT, /* star-backslash-newline in a C comment */
156 bool ignore; /* -iDsym or -iUsym */
162 return (strcmp(a->name, b->name)); in macro_cmp()
172 static bool compblank; /* -B: compress blank lines */
173 static bool lnblank; /* -b: blank deleted lines */
174 static bool complement; /* -c: do the complement */
175 static bool debugging; /* -d: debugging reports */
176 static bool inplace; /* -m: modify in place */
177 static bool iocccok; /* -e: fewer IOCCC errors */
178 static bool strictlogic; /* -K: keep ambiguous #ifs */
179 static bool killconsts; /* -k: eval constant #ifs */
180 static bool lnnum; /* -n: add #line directives */
181 static bool symlist; /* -s: output symbol list */
182 static bool symdepth; /* -S: output symbol depth */
183 static bool text; /* -t: this is a text file */
185 static FILE *input; /* input file pointer */ variable
186 static const char *filename; /* input file name */
192 static char *tempname; /* avoid splatting input */
194 static char tline[MAXLINE+EDITSLOP];/* input buffer plus space */
199 * input's, and unifdef correctly handles CRLF or LF endings whatever
202 * When the output isn't a processed input file (when it is error /
206 static const char *newline; /* input file format */
215 static int depth; /* current #if nesting */ variable
273 while ((opt = getopt(argc, argv, "i:D:U:f:I:M:o:x:bBcdehKklmnsStV")) != -1) in main()
277 * For strict backwards-compatibility the U or D in main()
278 * should be immediately after the -i but it doesn't in main()
295 case 'I': /* no-op for compatibility with cpp */ in main()
304 case 'c': /* treat -D as -U and vice versa */ in main()
342 case 'S': /* list symbols with their nesting depth */ in main()
359 argc -= optind; in main()
362 errx(2, "-B and -b are mutually exclusive"); in main()
364 errx(2, "-s only works with one input file"); in main()
366 errx(2, "-o cannot be used with multiple input files"); in main()
368 errx(2, "multiple input files require -m or -M"); in main()
370 errx(2, "-m requires an input file"); in main()
374 ofilename = "-"; in main()
380 else while (argc-- > 0) { in main()
400 if (ifn == NULL || strcmp(ifn, "-") == 0) { in processinout()
403 input = fbinmode(stdin); in processinout()
407 input = fopen(ifn, "rb"); in processinout()
408 if (input == NULL) in processinout()
411 if (strcmp(ofn, "-") == 0) { in processinout()
458 * Self-identification functions.
479 "usage: unifdef [-bBcdehKkmnsStV] [-x{012}] [-Mext] [-opath] \\\n" in synopsis()
480 " [-[i]Dsym[=val]] [-[i]Usym] [-fpath] ... [file] ...\n"); in synopsis()
495 " -Dsym=val define preprocessor symbol with given value\n" in help()
496 " -Dsym define preprocessor symbol with value 1\n" in help()
497 " -Usym preprocessor symbol is undefined\n" in help()
498 " -iDsym=val \\ ignore C strings and comments\n" in help()
499 " -iDsym ) in sections controlled by these\n" in help()
500 " -iUsym / preprocessor symbols\n" in help()
501 " -fpath file containing #define and #undef directives\n" in help()
502 " -b blank lines instead of deleting them\n" in help()
503 " -B compress blank lines around deleted section\n" in help()
504 " -c complement (invert) keep vs. delete\n" in help()
505 " -d debugging mode\n" in help()
506 " -e ignore multiline preprocessor directives\n" in help()
507 " -h print help\n" in help()
508 " -Ipath extra include file path (ignored)\n" in help()
509 " -K disable && and || short-circuiting\n" in help()
510 " -k process constant #if expressions\n" in help()
511 " -Mext modify in place and keep backups\n" in help()
512 " -m modify input files in place\n" in help()
513 " -n add #line directives to output\n" in help()
514 " -opath output file name\n" in help()
515 " -S list #if control symbols with nesting\n" in help()
516 " -s list #if control symbols\n" in help()
517 " -t ignore C strings and comments\n" in help()
518 " -V print version\n" in help()
519 " -x{012} exit status mode\n" in help()
530 * functions increase or decrease the depth. They also maintain the
534 * When we have processed a group that starts off with a known-false
546 * example being a multi-line comment hanging off the right of the
649 if (depth == 0) in ignoreoff()
651 ignoring[depth] = ignoring[depth-1]; in ignoreoff()
656 ignoring[depth] = true; in ignoreon()
661 snprintf(keyword, tline + sizeof(tline) - keyword, in keywordedit()
669 if (depth > MAXDEPTH-1) in nest()
671 if (depth == MAXDEPTH-1) in nest()
673 depth += 1; in nest()
674 stifline[depth] = linenum; in nest()
679 if (depth == 0) in unnest()
681 depth -= 1; in unnest()
686 ifstate[depth] = is; in state()
735 * Format of #line directives depends on whether we know the input filename.
762 fclose(input); in closeio()
781 trans_table[ifstate[depth]][lineval](); in process()
782 debug("process line %d %s -> %s depth %d", in process()
784 ifstate_name[ifstate[depth]], depth); in process()
816 keyword = tline + (cp - tline); in parseline()
825 if (cursym->value == NULL) in parseline()
828 if (cursym->ignore) in parseline()
862 long len = cp - tline; in parseline()
863 if (fgets(tline + len, MAXLINE - len, input) == NULL) { in parseline()
864 if (ferror(input)) in parseline()
932 return op_strict(p, a - b, at, bt); in op_sub()
964 * the expression is zero, LT_TRUE if it is non-zero, LT_IF if the expression
975 * in a table-driven way by eval_table. When it evaluates a subexpression it
977 * element of the table. Innermost expressions have special non-table-driven
1008 { "-", op_sub, NULL } } },
1017 return (ops - eval_ops); in prec()
1055 } else if (*cp == '-') { in eval_unary()
1056 debug("eval%d -", prec(ops)); in eval_unary()
1062 *valp = -(*valp); in eval_unary()
1099 debug("eval%d defined %s", prec(ops), sym->name); in eval_unary()
1100 *valp = (sym->value != NULL); in eval_unary()
1110 } else if (sym->value == NULL) { in eval_unary()
1114 *valp = strtol(sym->value, &ep, 0); in eval_unary()
1115 if (*ep != '\0' || ep == sym->value) in eval_unary()
1132 * Table-driven evaluation of binary operators.
1144 lt = ops->inner(ops+1, valp, &cp); in eval_table()
1149 for (op = ops->op; op->str != NULL; op++) { in eval_table()
1150 if (strncmp(cp, op->str, strlen(op->str)) == 0) { in eval_table()
1151 /* assume only one-char operators have stop chars */ in eval_table()
1152 if (op->stop != NULL && cp[1] != '\0' && in eval_table()
1153 strchr(op->stop, cp[1]) != NULL) in eval_table()
1159 if (op->str == NULL) in eval_table()
1161 cp += strlen(op->str); in eval_table()
1162 debug("eval%d %s", prec(ops), op->str); in eval_table()
1163 rt = ops->inner(ops+1, &val, &cp); in eval_table()
1166 lt = op->fn(valp, lt, *valp, rt, val); in eval_table()
1205 if (fgets(tline, MAXLINE, input) == NULL) { in skiphash()
1206 if (ferror(input)) in skiphash()
1250 if (text || ignoring[depth]) { in skipcomment()
1382 level--; in skipargs()
1388 /* Rewind and re-detect the syntax error later. */ in skipargs()
1441 * the symbol table index, else we return -1.
1456 printf("%s%3d", zerosyms ? "" : "\n", depth); in findsym()
1460 (int)(*strp-str), str, in findsym()
1467 * 'str' just points into the current mid-parse input and is not in findsym()
1468 * nul-terminated. We know the length of the symbol, *strp - str, but in findsym()
1469 * need to provide a nul-terminated lookup key for RB_FIND's comparison in findsym()
1472 strkey = malloc(*strp - str + 1); in findsym()
1473 memcpy(strkey, str, *strp - str); in findsym()
1474 strkey[*strp - str] = 0; in findsym()
1498 if (sym->value == NULL) in indirectsym()
1500 key.name = sym->value; in indirectsym()
1503 ind->value == NULL || in indirectsym()
1504 ind->value == sym->value) in indirectsym()
1507 sym->value = ind->value; in indirectsym()
1525 symval[val - sym] = '\0'; in addsym1()
1547 sym->ignore = ignorethis; in addsym2()
1548 sym->name = symname; in addsym2()
1549 sym->value = val; in addsym2()
1554 sym->ignore = ignorethis; in addsym2()
1555 sym->value = val; in addsym2()
1563 debug("%s %s%c%s", why, sym->name, in debugsym()
1564 sym->value ? '=' : ' ', in debugsym()
1565 sym->value ? sym->value : "undef"); in debugsym()
1576 input = fopen(fn, "rb"); in defundefile()
1577 if (input == NULL) in defundefile()
1582 if (ferror(input)) in defundefile()
1585 fclose(input); in defundefile()
1604 avoid unsupported multi-line preprocessor directives */ in defundef()
1606 while (end > tline && strchr(" \t\n\r", end[-1]) != NULL) in defundef()
1607 --end; in defundef()
1608 if (end > tline && end[-1] == '\\') in defundef()
1672 n = (size_t)(end - start) + 1; in xstrdup()
1698 if (depth == 0) in error()
1701 warnx("%s: %d: %s (#if line %d depth %d)", in error()
1702 filename, linenum, msg, stifline[depth], depth); in error()