^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) * Copyright (c) 2002 - 2011 Tony Finch <dot@dotat.at>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Redistribution and use in source and binary forms, with or without
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * modification, are permitted provided that the following conditions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * are met:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * 1. Redistributions of source code must retain the above copyright
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * notice, this list of conditions and the following disclaimer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * 2. Redistributions in binary form must reproduce the above copyright
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * notice, this list of conditions and the following disclaimer in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * documentation and/or other materials provided with the distribution.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * SUCH DAMAGE.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * unifdef - remove ifdef'ed lines
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * This code was derived from software contributed to Berkeley by Dave Yost.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * It was rewritten to support ANSI C by Tony Finch. The original version
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) * of unifdef carried the 4-clause BSD copyright licence. None of its code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) * remains in this version (though some of the names remain) so it now
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) * carries a more liberal licence.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * Wishlist:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) * provide an option which will append the name of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) * appropriate symbol after #else's and #endif's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) * provide an option which will check symbols after
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) * #else's and #endif's to see that they match their
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) * corresponding #ifdef or #ifndef
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) * These require better buffer handling, which would also make
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) * it possible to handle all "dodgy" directives correctly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) #include <sys/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) #include <sys/stat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) #include <ctype.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) #include <err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) #include <stdarg.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) #include <stdbool.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) const char copyright[] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) "@(#) $Version: unifdef-2.5 $\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) "@(#) $Author: Tony Finch (dot@dotat.at) $\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) "@(#) $URL: http://dotat.at/prog/unifdef $\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) /* types of input lines: */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) typedef enum {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) LT_TRUEI, /* a true #if with ignore flag */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) LT_FALSEI, /* a false #if with ignore flag */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) LT_IF, /* an unknown #if */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) LT_TRUE, /* a true #if */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) LT_FALSE, /* a false #if */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) LT_ELIF, /* an unknown #elif */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) LT_ELTRUE, /* a true #elif */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) LT_ELFALSE, /* a false #elif */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) LT_ELSE, /* #else */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) LT_ENDIF, /* #endif */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) LT_DODGY, /* flag: directive is not on one line */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) LT_DODGY_LAST = LT_DODGY + LT_ENDIF,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) LT_PLAIN, /* ordinary line */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) LT_EOF, /* end of file */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) LT_ERROR, /* unevaluable #if */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) LT_COUNT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) } Linetype;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) static char const * const linetype_name[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) "TRUEI", "FALSEI", "IF", "TRUE", "FALSE",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) "ELIF", "ELTRUE", "ELFALSE", "ELSE", "ENDIF",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) "DODGY TRUEI", "DODGY FALSEI",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) "DODGY IF", "DODGY TRUE", "DODGY FALSE",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) "DODGY ELIF", "DODGY ELTRUE", "DODGY ELFALSE",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) "DODGY ELSE", "DODGY ENDIF",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) "PLAIN", "EOF", "ERROR"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) /* state of #if processing */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) typedef enum {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) IS_OUTSIDE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) IS_FALSE_PREFIX, /* false #if followed by false #elifs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) IS_TRUE_PREFIX, /* first non-false #(el)if is true */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) IS_PASS_MIDDLE, /* first non-false #(el)if is unknown */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) IS_FALSE_MIDDLE, /* a false #elif after a pass state */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) IS_TRUE_MIDDLE, /* a true #elif after a pass state */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) IS_PASS_ELSE, /* an else after a pass state */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) IS_FALSE_ELSE, /* an else after a true state */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) IS_TRUE_ELSE, /* an else after only false states */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) IS_FALSE_TRAILER, /* #elifs after a true are false */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) IS_COUNT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) } Ifstate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) static char const * const ifstate_name[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) "OUTSIDE", "FALSE_PREFIX", "TRUE_PREFIX",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) "PASS_MIDDLE", "FALSE_MIDDLE", "TRUE_MIDDLE",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) "PASS_ELSE", "FALSE_ELSE", "TRUE_ELSE",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) "FALSE_TRAILER"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) /* state of comment parser */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) typedef enum {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) NO_COMMENT = false, /* outside a comment */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) C_COMMENT, /* in a comment like this one */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) CXX_COMMENT, /* between // and end of line */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) STARTING_COMMENT, /* just after slash-backslash-newline */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) FINISHING_COMMENT, /* star-backslash-newline in a C comment */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) CHAR_LITERAL, /* inside '' */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) STRING_LITERAL /* inside "" */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) } Comment_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) static char const * const comment_name[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) "NO", "C", "CXX", "STARTING", "FINISHING", "CHAR", "STRING"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) /* state of preprocessor line parser */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) typedef enum {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) LS_START, /* only space and comments on this line */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) LS_HASH, /* only space, comments, and a hash */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) LS_DIRTY /* this line can't be a preprocessor line */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) } Line_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) static char const * const linestate_name[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) "START", "HASH", "DIRTY"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) * Minimum translation limits from ISO/IEC 9899:1999 5.2.4.1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) #define MAXDEPTH 64 /* maximum #if nesting */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) #define MAXLINE 4096 /* maximum length of line */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) #define MAXSYMS 4096 /* maximum number of symbols */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) * Sometimes when editing a keyword the replacement text is longer, so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) * we leave some space at the end of the tline buffer to accommodate this.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) #define EDITSLOP 10
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) * For temporary filenames
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) #define TEMPLATE "unifdef.XXXXXX"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) * Globals.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) static bool compblank; /* -B: compress blank lines */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) static bool lnblank; /* -b: blank deleted lines */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) static bool complement; /* -c: do the complement */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) static bool debugging; /* -d: debugging reports */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) static bool iocccok; /* -e: fewer IOCCC errors */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) static bool strictlogic; /* -K: keep ambiguous #ifs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) static bool killconsts; /* -k: eval constant #ifs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) static bool lnnum; /* -n: add #line directives */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) static bool symlist; /* -s: output symbol list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) static bool symdepth; /* -S: output symbol depth */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) static bool text; /* -t: this is a text file */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) static const char *symname[MAXSYMS]; /* symbol name */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) static const char *value[MAXSYMS]; /* -Dsym=value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) static bool ignore[MAXSYMS]; /* -iDsym or -iUsym */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) static int nsyms; /* number of symbols */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) static FILE *input; /* input file pointer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) static const char *filename; /* input file name */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) static int linenum; /* current line number */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) static FILE *output; /* output file pointer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) static const char *ofilename; /* output file name */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) static bool overwriting; /* output overwrites input */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) static char tempname[FILENAME_MAX]; /* used when overwriting */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) static char tline[MAXLINE+EDITSLOP];/* input buffer plus space */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) static char *keyword; /* used for editing #elif's */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) static const char *newline; /* input file format */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) static const char newline_unix[] = "\n";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) static const char newline_crlf[] = "\r\n";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) static Comment_state incomment; /* comment parser state */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) static Line_state linestate; /* #if line parser state */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) static Ifstate ifstate[MAXDEPTH]; /* #if processor state */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) static bool ignoring[MAXDEPTH]; /* ignore comments state */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) static int stifline[MAXDEPTH]; /* start of current #if */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) static int depth; /* current #if nesting */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) static int delcount; /* count of deleted lines */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) static unsigned blankcount; /* count of blank lines */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) static unsigned blankmax; /* maximum recent blankcount */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) static bool constexpr; /* constant #if expression */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) static bool zerosyms = true; /* to format symdepth output */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) static bool firstsym; /* ditto */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) static int exitstat; /* program exit status */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) static void addsym(bool, bool, char *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) static void closeout(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) static void debug(const char *, ...);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) static void done(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) static void error(const char *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) static int findsym(const char *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) static void flushline(bool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) static Linetype parseline(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) static Linetype ifeval(const char **);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) static void ignoreoff(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) static void ignoreon(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) static void keywordedit(const char *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) static void nest(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) static void process(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) static const char *skipargs(const char *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) static const char *skipcomment(const char *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) static const char *skipsym(const char *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) static void state(Ifstate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) static int strlcmp(const char *, const char *, size_t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) static void unnest(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) static void usage(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) static void version(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) #define endsym(c) (!isalnum((unsigned char)c) && c != '_')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) * The main program.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) main(int argc, char *argv[])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) int opt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) while ((opt = getopt(argc, argv, "i:D:U:I:o:bBcdeKklnsStV")) != -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) switch (opt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) case 'i': /* treat stuff controlled by these symbols as text */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) * For strict backwards-compatibility the U or D
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) * should be immediately after the -i but it doesn't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) * matter much if we relax that requirement.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) opt = *optarg++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) if (opt == 'D')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) addsym(true, true, optarg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) else if (opt == 'U')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) addsym(true, false, optarg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) usage();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) case 'D': /* define a symbol */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) addsym(false, true, optarg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) case 'U': /* undef a symbol */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) addsym(false, false, optarg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) case 'I': /* no-op for compatibility with cpp */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) case 'b': /* blank deleted lines instead of omitting them */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) case 'l': /* backwards compatibility */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) lnblank = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) case 'B': /* compress blank lines around removed section */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) compblank = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) case 'c': /* treat -D as -U and vice versa */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) complement = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) case 'd':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) debugging = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) case 'e': /* fewer errors from dodgy lines */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) iocccok = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) case 'K': /* keep ambiguous #ifs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) strictlogic = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) case 'k': /* process constant #ifs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) killconsts = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) case 'n': /* add #line directive after deleted lines */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) lnnum = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) case 'o': /* output to a file */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) ofilename = optarg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) case 's': /* only output list of symbols that control #ifs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) symlist = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) case 'S': /* list symbols with their nesting depth */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) symlist = symdepth = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) case 't': /* don't parse C comments */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) text = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) case 'V': /* print version */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) version();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) usage();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) argc -= optind;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) argv += optind;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) if (compblank && lnblank)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) errx(2, "-B and -b are mutually exclusive");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) if (argc > 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) errx(2, "can only do one file");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) } else if (argc == 1 && strcmp(*argv, "-") != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) filename = *argv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) input = fopen(filename, "rb");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) if (input == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) err(2, "can't open %s", filename);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) filename = "[stdin]";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) input = stdin;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) if (ofilename == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) ofilename = "[stdout]";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) output = stdout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) struct stat ist, ost;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) if (stat(ofilename, &ost) == 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) fstat(fileno(input), &ist) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) overwriting = (ist.st_dev == ost.st_dev
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) && ist.st_ino == ost.st_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) if (overwriting) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) const char *dirsep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) int ofd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) dirsep = strrchr(ofilename, '/');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) if (dirsep != NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) snprintf(tempname, sizeof(tempname),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) "%.*s/" TEMPLATE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) (int)(dirsep - ofilename), ofilename);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) snprintf(tempname, sizeof(tempname),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) TEMPLATE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) ofd = mkstemp(tempname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) if (ofd != -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) output = fdopen(ofd, "wb+");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) if (output == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) err(2, "can't create temporary file");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) fchmod(ofd, ist.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) output = fopen(ofilename, "wb");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) if (output == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) err(2, "can't open %s", ofilename);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) process();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) abort(); /* bug */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) version(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) const char *c = copyright;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) while (*++c != '$')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) if (*c == '\0')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) exit(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) while (*++c != '$')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) putc(*c, stderr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) putc('\n', stderr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) usage(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) fprintf(stderr, "usage: unifdef [-bBcdeKknsStV] [-Ipath]"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) " [-Dsym[=val]] [-Usym] [-iDsym[=val]] [-iUsym] ... [file]\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) exit(2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) * A state transition function alters the global #if processing state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) * in a particular way. The table below is indexed by the current
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) * processing state and the type of the current line.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) * Nesting is handled by keeping a stack of states; some transition
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) * functions increase or decrease the depth. They also maintain the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) * ignore state on a stack. In some complicated cases they have to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) * alter the preprocessor directive, as follows.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) * When we have processed a group that starts off with a known-false
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) * #if/#elif sequence (which has therefore been deleted) followed by a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) * #elif that we don't understand and therefore must keep, we edit the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) * latter into a #if to keep the nesting correct. We use memcpy() to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) * overwrite the 4 byte token "elif" with "if " without a '\0' byte.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) * When we find a true #elif in a group, the following block will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) * always be kept and the rest of the sequence after the next #elif or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) * #else will be discarded. We edit the #elif into a #else and the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) * following directive to #endif since this has the desired behaviour.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) * "Dodgy" directives are split across multiple lines, the most common
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) * example being a multi-line comment hanging off the right of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) * directive. We can handle them correctly only if there is no change
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) * from printing to dropping (or vice versa) caused by that directive.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) * If the directive is the first of a group we have a choice between
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) * failing with an error, or passing it through unchanged instead of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) * evaluating it. The latter is not the default to avoid questions from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) * users about unifdef unexpectedly leaving behind preprocessor directives.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) typedef void state_fn(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) /* report an error */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) static void Eelif (void) { error("Inappropriate #elif"); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) static void Eelse (void) { error("Inappropriate #else"); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) static void Eendif(void) { error("Inappropriate #endif"); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) static void Eeof (void) { error("Premature EOF"); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) static void Eioccc(void) { error("Obfuscated preprocessor control line"); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) /* plain line handling */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) static void print (void) { flushline(true); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) static void drop (void) { flushline(false); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) /* output lacks group's start line */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) static void Strue (void) { drop(); ignoreoff(); state(IS_TRUE_PREFIX); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) static void Sfalse(void) { drop(); ignoreoff(); state(IS_FALSE_PREFIX); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) static void Selse (void) { drop(); state(IS_TRUE_ELSE); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) /* print/pass this block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) static void Pelif (void) { print(); ignoreoff(); state(IS_PASS_MIDDLE); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) static void Pelse (void) { print(); state(IS_PASS_ELSE); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) static void Pendif(void) { print(); unnest(); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) /* discard this block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) static void Dfalse(void) { drop(); ignoreoff(); state(IS_FALSE_TRAILER); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) static void Delif (void) { drop(); ignoreoff(); state(IS_FALSE_MIDDLE); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) static void Delse (void) { drop(); state(IS_FALSE_ELSE); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) static void Dendif(void) { drop(); unnest(); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) /* first line of group */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) static void Fdrop (void) { nest(); Dfalse(); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) static void Fpass (void) { nest(); Pelif(); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) static void Ftrue (void) { nest(); Strue(); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) static void Ffalse(void) { nest(); Sfalse(); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) /* variable pedantry for obfuscated lines */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) static void Oiffy (void) { if (!iocccok) Eioccc(); Fpass(); ignoreon(); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) static void Oif (void) { if (!iocccok) Eioccc(); Fpass(); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) static void Oelif (void) { if (!iocccok) Eioccc(); Pelif(); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) /* ignore comments in this block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) static void Idrop (void) { Fdrop(); ignoreon(); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) static void Itrue (void) { Ftrue(); ignoreon(); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) static void Ifalse(void) { Ffalse(); ignoreon(); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) /* modify this line */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) static void Mpass (void) { memcpy(keyword, "if ", 4); Pelif(); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) static void Mtrue (void) { keywordedit("else"); state(IS_TRUE_MIDDLE); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) static void Melif (void) { keywordedit("endif"); state(IS_FALSE_TRAILER); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) static void Melse (void) { keywordedit("endif"); state(IS_FALSE_ELSE); }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) static state_fn * const trans_table[IS_COUNT][LT_COUNT] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) /* IS_OUTSIDE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) { Itrue, Ifalse,Fpass, Ftrue, Ffalse,Eelif, Eelif, Eelif, Eelse, Eendif,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) Oiffy, Oiffy, Fpass, Oif, Oif, Eelif, Eelif, Eelif, Eelse, Eendif,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) print, done, abort },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) /* IS_FALSE_PREFIX */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) { Idrop, Idrop, Fdrop, Fdrop, Fdrop, Mpass, Strue, Sfalse,Selse, Dendif,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) Idrop, Idrop, Fdrop, Fdrop, Fdrop, Mpass, Eioccc,Eioccc,Eioccc,Eioccc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) drop, Eeof, abort },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) /* IS_TRUE_PREFIX */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) { Itrue, Ifalse,Fpass, Ftrue, Ffalse,Dfalse,Dfalse,Dfalse,Delse, Dendif,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) Oiffy, Oiffy, Fpass, Oif, Oif, Eioccc,Eioccc,Eioccc,Eioccc,Eioccc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) print, Eeof, abort },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) /* IS_PASS_MIDDLE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) { Itrue, Ifalse,Fpass, Ftrue, Ffalse,Pelif, Mtrue, Delif, Pelse, Pendif,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) Oiffy, Oiffy, Fpass, Oif, Oif, Pelif, Oelif, Oelif, Pelse, Pendif,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) print, Eeof, abort },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) /* IS_FALSE_MIDDLE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) { Idrop, Idrop, Fdrop, Fdrop, Fdrop, Pelif, Mtrue, Delif, Pelse, Pendif,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) Idrop, Idrop, Fdrop, Fdrop, Fdrop, Eioccc,Eioccc,Eioccc,Eioccc,Eioccc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) drop, Eeof, abort },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) /* IS_TRUE_MIDDLE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) { Itrue, Ifalse,Fpass, Ftrue, Ffalse,Melif, Melif, Melif, Melse, Pendif,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) Oiffy, Oiffy, Fpass, Oif, Oif, Eioccc,Eioccc,Eioccc,Eioccc,Pendif,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) print, Eeof, abort },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) /* IS_PASS_ELSE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) { Itrue, Ifalse,Fpass, Ftrue, Ffalse,Eelif, Eelif, Eelif, Eelse, Pendif,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) Oiffy, Oiffy, Fpass, Oif, Oif, Eelif, Eelif, Eelif, Eelse, Pendif,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) print, Eeof, abort },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) /* IS_FALSE_ELSE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) { Idrop, Idrop, Fdrop, Fdrop, Fdrop, Eelif, Eelif, Eelif, Eelse, Dendif,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) Idrop, Idrop, Fdrop, Fdrop, Fdrop, Eelif, Eelif, Eelif, Eelse, Eioccc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) drop, Eeof, abort },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) /* IS_TRUE_ELSE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) { Itrue, Ifalse,Fpass, Ftrue, Ffalse,Eelif, Eelif, Eelif, Eelse, Dendif,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) Oiffy, Oiffy, Fpass, Oif, Oif, Eelif, Eelif, Eelif, Eelse, Eioccc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) print, Eeof, abort },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) /* IS_FALSE_TRAILER */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) { Idrop, Idrop, Fdrop, Fdrop, Fdrop, Dfalse,Dfalse,Dfalse,Delse, Dendif,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) Idrop, Idrop, Fdrop, Fdrop, Fdrop, Dfalse,Dfalse,Dfalse,Delse, Eioccc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) drop, Eeof, abort }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) /*TRUEI FALSEI IF TRUE FALSE ELIF ELTRUE ELFALSE ELSE ENDIF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) TRUEI FALSEI IF TRUE FALSE ELIF ELTRUE ELFALSE ELSE ENDIF (DODGY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) PLAIN EOF ERROR */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) * State machine utility functions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) ignoreoff(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) if (depth == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) abort(); /* bug */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) ignoring[depth] = ignoring[depth-1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) ignoreon(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) ignoring[depth] = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) keywordedit(const char *replacement)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) snprintf(keyword, tline + sizeof(tline) - keyword,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) "%s%s", replacement, newline);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) print();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) nest(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) if (depth > MAXDEPTH-1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) abort(); /* bug */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) if (depth == MAXDEPTH-1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) error("Too many levels of nesting");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) depth += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) stifline[depth] = linenum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) unnest(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) if (depth == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) abort(); /* bug */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) depth -= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) state(Ifstate is)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) ifstate[depth] = is;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) * Write a line to the output or not, according to command line options.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) flushline(bool keep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) if (symlist)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) if (keep ^ complement) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) bool blankline = tline[strspn(tline, " \t\r\n")] == '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) if (blankline && compblank && blankcount != blankmax) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) delcount += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) blankcount += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) if (lnnum && delcount > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) printf("#line %d%s", linenum, newline);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) fputs(tline, output);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) delcount = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) blankmax = blankcount = blankline ? blankcount + 1 : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) if (lnblank)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) fputs(newline, output);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) exitstat = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) delcount += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) blankcount = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) if (debugging)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) fflush(output);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) * The driver for the state machine.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) process(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) /* When compressing blank lines, act as if the file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) is preceded by a large number of blank lines. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) blankmax = blankcount = 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) Linetype lineval = parseline();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) trans_table[ifstate[depth]][lineval]();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) debug("process line %d %s -> %s depth %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) linenum, linetype_name[lineval],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) ifstate_name[ifstate[depth]], depth);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) * Flush the output and handle errors.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) closeout(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) if (symdepth && !zerosyms)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) printf("\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) if (fclose(output) == EOF) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) warn("couldn't write to %s", ofilename);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) if (overwriting) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) unlink(tempname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) errx(2, "%s unchanged", filename);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) exit(2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) * Clean up and exit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) done(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) if (incomment)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) error("EOF in comment");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) closeout();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) if (overwriting && rename(tempname, ofilename) == -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) warn("couldn't rename temporary file");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) unlink(tempname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) errx(2, "%s unchanged", ofilename);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) exit(exitstat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) * Parse a line and determine its type. We keep the preprocessor line
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) * parser state between calls in the global variable linestate, with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) * help from skipcomment().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) static Linetype
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) parseline(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) const char *cp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) int cursym;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) int kwlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) Linetype retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) Comment_state wascomment;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) linenum++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) if (fgets(tline, MAXLINE, input) == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) return (LT_EOF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) if (newline == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) if (strrchr(tline, '\n') == strrchr(tline, '\r') + 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) newline = newline_crlf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) newline = newline_unix;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) retval = LT_PLAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) wascomment = incomment;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) cp = skipcomment(tline);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) if (linestate == LS_START) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) if (*cp == '#') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) linestate = LS_HASH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) firstsym = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) cp = skipcomment(cp + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) } else if (*cp != '\0')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) linestate = LS_DIRTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) if (!incomment && linestate == LS_HASH) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) keyword = tline + (cp - tline);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) cp = skipsym(cp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) kwlen = cp - keyword;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) /* no way can we deal with a continuation inside a keyword */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) if (strncmp(cp, "\\\r\n", 3) == 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) strncmp(cp, "\\\n", 2) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) Eioccc();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) if (strlcmp("ifdef", keyword, kwlen) == 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) strlcmp("ifndef", keyword, kwlen) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) cp = skipcomment(cp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) if ((cursym = findsym(cp)) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) retval = LT_IF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) retval = (keyword[2] == 'n')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) ? LT_FALSE : LT_TRUE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) if (value[cursym] == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) retval = (retval == LT_TRUE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) ? LT_FALSE : LT_TRUE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) if (ignore[cursym])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) retval = (retval == LT_TRUE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) ? LT_TRUEI : LT_FALSEI;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) cp = skipsym(cp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) } else if (strlcmp("if", keyword, kwlen) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) retval = ifeval(&cp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) else if (strlcmp("elif", keyword, kwlen) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) retval = ifeval(&cp) - LT_IF + LT_ELIF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) else if (strlcmp("else", keyword, kwlen) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) retval = LT_ELSE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) else if (strlcmp("endif", keyword, kwlen) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) retval = LT_ENDIF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) linestate = LS_DIRTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) retval = LT_PLAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) cp = skipcomment(cp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) if (*cp != '\0') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) linestate = LS_DIRTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) if (retval == LT_TRUE || retval == LT_FALSE ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) retval == LT_TRUEI || retval == LT_FALSEI)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) retval = LT_IF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) if (retval == LT_ELTRUE || retval == LT_ELFALSE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) retval = LT_ELIF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) if (retval != LT_PLAIN && (wascomment || incomment)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) retval += LT_DODGY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) if (incomment)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) linestate = LS_DIRTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) /* skipcomment normally changes the state, except
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) if the last line of the file lacks a newline, or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) if there is too much whitespace in a directive */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) if (linestate == LS_HASH) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) size_t len = cp - tline;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) if (fgets(tline + len, MAXLINE - len, input) == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) /* append the missing newline */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) strcpy(tline + len, newline);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) cp += strlen(newline);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) linestate = LS_START;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) linestate = LS_DIRTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) if (linestate == LS_DIRTY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) while (*cp != '\0')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) cp = skipcomment(cp + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) debug("parser line %d state %s comment %s line", linenum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) comment_name[incomment], linestate_name[linestate]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) return (retval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) * These are the binary operators that are supported by the expression
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) * evaluator.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) static Linetype op_strict(int *p, int v, Linetype at, Linetype bt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) if(at == LT_IF || bt == LT_IF) return (LT_IF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) return (*p = v, v ? LT_TRUE : LT_FALSE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) static Linetype op_lt(int *p, Linetype at, int a, Linetype bt, int b) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) return op_strict(p, a < b, at, bt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) static Linetype op_gt(int *p, Linetype at, int a, Linetype bt, int b) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) return op_strict(p, a > b, at, bt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) static Linetype op_le(int *p, Linetype at, int a, Linetype bt, int b) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) return op_strict(p, a <= b, at, bt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) static Linetype op_ge(int *p, Linetype at, int a, Linetype bt, int b) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) return op_strict(p, a >= b, at, bt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) static Linetype op_eq(int *p, Linetype at, int a, Linetype bt, int b) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) return op_strict(p, a == b, at, bt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) static Linetype op_ne(int *p, Linetype at, int a, Linetype bt, int b) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) return op_strict(p, a != b, at, bt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) static Linetype op_or(int *p, Linetype at, int a, Linetype bt, int b) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) if (!strictlogic && (at == LT_TRUE || bt == LT_TRUE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) return (*p = 1, LT_TRUE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) return op_strict(p, a || b, at, bt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) static Linetype op_and(int *p, Linetype at, int a, Linetype bt, int b) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) if (!strictlogic && (at == LT_FALSE || bt == LT_FALSE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) return (*p = 0, LT_FALSE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) return op_strict(p, a && b, at, bt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) * An evaluation function takes three arguments, as follows: (1) a pointer to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) * an element of the precedence table which lists the operators at the current
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) * level of precedence; (2) a pointer to an integer which will receive the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) * value of the expression; and (3) a pointer to a char* that points to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) * expression to be evaluated and that is updated to the end of the expression
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) * when evaluation is complete. The function returns LT_FALSE if the value of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) * the expression is zero, LT_TRUE if it is non-zero, LT_IF if the expression
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) * depends on an unknown symbol, or LT_ERROR if there is a parse failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) struct ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) typedef Linetype eval_fn(const struct ops *, int *, const char **);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) static eval_fn eval_table, eval_unary;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) * The precedence table. Expressions involving binary operators are evaluated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) * in a table-driven way by eval_table. When it evaluates a subexpression it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) * calls the inner function with its first argument pointing to the next
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) * element of the table. Innermost expressions have special non-table-driven
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) * handling.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) static const struct ops {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) eval_fn *inner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) struct op {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) const char *str;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) Linetype (*fn)(int *, Linetype, int, Linetype, int);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) } op[5];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) } eval_ops[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) { eval_table, { { "||", op_or } } },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) { eval_table, { { "&&", op_and } } },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) { eval_table, { { "==", op_eq },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) { "!=", op_ne } } },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) { eval_unary, { { "<=", op_le },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) { ">=", op_ge },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) { "<", op_lt },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) { ">", op_gt } } }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) * Function for evaluating the innermost parts of expressions,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) * viz. !expr (expr) number defined(symbol) symbol
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) * We reset the constexpr flag in the last two cases.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) static Linetype
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) eval_unary(const struct ops *ops, int *valp, const char **cpp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) const char *cp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) char *ep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) int sym;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) bool defparen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) Linetype lt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) cp = skipcomment(*cpp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) if (*cp == '!') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) debug("eval%d !", ops - eval_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) cp++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) lt = eval_unary(ops, valp, &cp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) if (lt == LT_ERROR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) return (LT_ERROR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) if (lt != LT_IF) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) *valp = !*valp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) lt = *valp ? LT_TRUE : LT_FALSE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) } else if (*cp == '(') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) cp++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) debug("eval%d (", ops - eval_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) lt = eval_table(eval_ops, valp, &cp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) if (lt == LT_ERROR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) return (LT_ERROR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) cp = skipcomment(cp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) if (*cp++ != ')')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) return (LT_ERROR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) } else if (isdigit((unsigned char)*cp)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) debug("eval%d number", ops - eval_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) *valp = strtol(cp, &ep, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) if (ep == cp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) return (LT_ERROR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) lt = *valp ? LT_TRUE : LT_FALSE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) cp = skipsym(cp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) } else if (strncmp(cp, "defined", 7) == 0 && endsym(cp[7])) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) cp = skipcomment(cp+7);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) debug("eval%d defined", ops - eval_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) if (*cp == '(') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) cp = skipcomment(cp+1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) defparen = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) defparen = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) sym = findsym(cp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) if (sym < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) lt = LT_IF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) *valp = (value[sym] != NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) lt = *valp ? LT_TRUE : LT_FALSE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) cp = skipsym(cp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) cp = skipcomment(cp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) if (defparen && *cp++ != ')')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) return (LT_ERROR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) constexpr = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) } else if (!endsym(*cp)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) debug("eval%d symbol", ops - eval_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) sym = findsym(cp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) cp = skipsym(cp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) if (sym < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) lt = LT_IF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) cp = skipargs(cp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) } else if (value[sym] == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) *valp = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) lt = LT_FALSE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) *valp = strtol(value[sym], &ep, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) if (*ep != '\0' || ep == value[sym])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) return (LT_ERROR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) lt = *valp ? LT_TRUE : LT_FALSE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) cp = skipargs(cp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) constexpr = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) debug("eval%d bad expr", ops - eval_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) return (LT_ERROR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) *cpp = cp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) debug("eval%d = %d", ops - eval_ops, *valp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) return (lt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) * Table-driven evaluation of binary operators.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) static Linetype
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) eval_table(const struct ops *ops, int *valp, const char **cpp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) const struct op *op;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) const char *cp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) int val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) Linetype lt, rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) debug("eval%d", ops - eval_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) cp = *cpp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) lt = ops->inner(ops+1, valp, &cp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) if (lt == LT_ERROR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) return (LT_ERROR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) cp = skipcomment(cp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) for (op = ops->op; op->str != NULL; op++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) if (strncmp(cp, op->str, strlen(op->str)) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) if (op->str == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) cp += strlen(op->str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) debug("eval%d %s", ops - eval_ops, op->str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) rt = ops->inner(ops+1, &val, &cp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) if (rt == LT_ERROR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) return (LT_ERROR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) lt = op->fn(valp, lt, *valp, rt, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) *cpp = cp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) debug("eval%d = %d", ops - eval_ops, *valp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) debug("eval%d lt = %s", ops - eval_ops, linetype_name[lt]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) return (lt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) * Evaluate the expression on a #if or #elif line. If we can work out
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) * the result we return LT_TRUE or LT_FALSE accordingly, otherwise we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) * return just a generic LT_IF.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) static Linetype
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) ifeval(const char **cpp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) int val = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) debug("eval %s", *cpp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) constexpr = killconsts ? false : true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) ret = eval_table(eval_ops, &val, cpp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960) debug("eval = %d", val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) return (constexpr ? LT_IF : ret == LT_ERROR ? LT_IF : ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965) * Skip over comments, strings, and character literals and stop at the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966) * next character position that is not whitespace. Between calls we keep
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967) * the comment state in the global variable incomment, and we also adjust
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968) * the global variable linestate when we see a newline.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969) * XXX: doesn't cope with the buffer splitting inside a state transition.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) static const char *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972) skipcomment(const char *cp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974) if (text || ignoring[depth]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975) for (; isspace((unsigned char)*cp); cp++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976) if (*cp == '\n')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977) linestate = LS_START;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978) return (cp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980) while (*cp != '\0')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981) /* don't reset to LS_START after a line continuation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982) if (strncmp(cp, "\\\r\n", 3) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983) cp += 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984) else if (strncmp(cp, "\\\n", 2) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985) cp += 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986) else switch (incomment) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987) case NO_COMMENT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988) if (strncmp(cp, "/\\\r\n", 4) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989) incomment = STARTING_COMMENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990) cp += 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991) } else if (strncmp(cp, "/\\\n", 3) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992) incomment = STARTING_COMMENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993) cp += 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994) } else if (strncmp(cp, "/*", 2) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995) incomment = C_COMMENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996) cp += 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997) } else if (strncmp(cp, "//", 2) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998) incomment = CXX_COMMENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999) cp += 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) } else if (strncmp(cp, "\'", 1) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) incomment = CHAR_LITERAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) linestate = LS_DIRTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) cp += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) } else if (strncmp(cp, "\"", 1) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) incomment = STRING_LITERAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) linestate = LS_DIRTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) cp += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) } else if (strncmp(cp, "\n", 1) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) linestate = LS_START;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) cp += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) } else if (strchr(" \r\t", *cp) != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) cp += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) return (cp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) case CXX_COMMENT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) if (strncmp(cp, "\n", 1) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) incomment = NO_COMMENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) linestate = LS_START;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) cp += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) case CHAR_LITERAL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) case STRING_LITERAL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) if ((incomment == CHAR_LITERAL && cp[0] == '\'') ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) (incomment == STRING_LITERAL && cp[0] == '\"')) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) incomment = NO_COMMENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) cp += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) } else if (cp[0] == '\\') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) if (cp[1] == '\0')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) cp += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) cp += 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) } else if (strncmp(cp, "\n", 1) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) if (incomment == CHAR_LITERAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) error("unterminated char literal");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) error("unterminated string literal");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) cp += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) case C_COMMENT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) if (strncmp(cp, "*\\\r\n", 4) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) incomment = FINISHING_COMMENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) cp += 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) } else if (strncmp(cp, "*\\\n", 3) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) incomment = FINISHING_COMMENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) cp += 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) } else if (strncmp(cp, "*/", 2) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) incomment = NO_COMMENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) cp += 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) cp += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) case STARTING_COMMENT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) if (*cp == '*') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) incomment = C_COMMENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) cp += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) } else if (*cp == '/') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) incomment = CXX_COMMENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) cp += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) incomment = NO_COMMENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) linestate = LS_DIRTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) case FINISHING_COMMENT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) if (*cp == '/') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) incomment = NO_COMMENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) cp += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) incomment = C_COMMENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) abort(); /* bug */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) return (cp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) * Skip macro arguments.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) static const char *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) skipargs(const char *cp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) const char *ocp = cp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) int level = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) cp = skipcomment(cp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) if (*cp != '(')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) return (cp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) if (*cp == '(')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) level++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) if (*cp == ')')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) level--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) cp = skipcomment(cp+1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) } while (level != 0 && *cp != '\0');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) if (level == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) return (cp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) /* Rewind and re-detect the syntax error later. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) return (ocp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) * Skip over an identifier.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) static const char *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) skipsym(const char *cp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) while (!endsym(*cp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) ++cp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) return (cp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) * Look for the symbol in the symbol table. If it is found, we return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) * the symbol table index, else we return -1.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) findsym(const char *str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) const char *cp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) int symind;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) cp = skipsym(str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) if (cp == str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) return (-1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) if (symlist) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) if (symdepth && firstsym)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) printf("%s%3d", zerosyms ? "" : "\n", depth);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) firstsym = zerosyms = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) printf("%s%.*s%s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) symdepth ? " " : "",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) (int)(cp-str), str,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) symdepth ? "" : "\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) /* we don't care about the value of the symbol */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) return (0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) for (symind = 0; symind < nsyms; ++symind) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) if (strlcmp(symname[symind], str, cp-str) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) debug("findsym %s %s", symname[symind],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) value[symind] ? value[symind] : "");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) return (symind);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) return (-1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) * Add a symbol to the symbol table.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) addsym(bool ignorethis, bool definethis, char *sym)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) int symind;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) char *val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) symind = findsym(sym);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) if (symind < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) if (nsyms >= MAXSYMS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) errx(2, "too many symbols");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) symind = nsyms++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) symname[symind] = sym;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) ignore[symind] = ignorethis;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) val = sym + (skipsym(sym) - sym);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) if (definethis) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) if (*val == '=') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) value[symind] = val+1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) *val = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) } else if (*val == '\0')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) value[symind] = "1";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) usage();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) if (*val != '\0')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) usage();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) value[symind] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) debug("addsym %s=%s", symname[symind],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) value[symind] ? value[symind] : "undef");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) * Compare s with n characters of t.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) * The same as strncmp() except that it checks that s[n] == '\0'.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) strlcmp(const char *s, const char *t, size_t n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) while (n-- && *t != '\0')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) if (*s != *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) return ((unsigned char)*s - (unsigned char)*t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) ++s, ++t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) return ((unsigned char)*s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) * Diagnostics.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) debug(const char *msg, ...)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) va_list ap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) if (debugging) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) va_start(ap, msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) vwarnx(msg, ap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) va_end(ap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) error(const char *msg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) if (depth == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) warnx("%s: %d: %s", filename, linenum, msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) warnx("%s: %d: %s (#if line %d depth %d)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) filename, linenum, msg, stifline[depth], depth);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) closeout();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) errx(2, "output may be truncated");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) }