123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578 |
- #include "unifdef.h"
- static const char copyright[] =
- #include "version.h"
- "@(#) $Author: Tony Finch (dot@dotat.at) $\n"
- "@(#) $URL: http://dotat.at/prog/unifdef $\n"
- ;
- typedef enum {
- LT_TRUEI,
- LT_FALSEI,
- LT_IF,
- LT_TRUE,
- LT_FALSE,
- LT_ELIF,
- LT_ELTRUE,
- LT_ELFALSE,
- LT_ELSE,
- LT_ENDIF,
- LT_DODGY,
- LT_DODGY_LAST = LT_DODGY + LT_ENDIF,
- LT_PLAIN,
- LT_EOF,
- LT_ERROR,
- LT_COUNT
- } Linetype;
- static char const * const linetype_name[] = {
- "TRUEI", "FALSEI", "IF", "TRUE", "FALSE",
- "ELIF", "ELTRUE", "ELFALSE", "ELSE", "ENDIF",
- "DODGY TRUEI", "DODGY FALSEI",
- "DODGY IF", "DODGY TRUE", "DODGY FALSE",
- "DODGY ELIF", "DODGY ELTRUE", "DODGY ELFALSE",
- "DODGY ELSE", "DODGY ENDIF",
- "PLAIN", "EOF", "ERROR"
- };
- #define linetype_if2elif(lt) ((Linetype)(lt - LT_IF + LT_ELIF))
- #define linetype_2dodgy(lt) ((Linetype)(lt + LT_DODGY))
- typedef enum {
- IS_OUTSIDE,
- IS_FALSE_PREFIX,
- IS_TRUE_PREFIX,
- IS_PASS_MIDDLE,
- IS_FALSE_MIDDLE,
- IS_TRUE_MIDDLE,
- IS_PASS_ELSE,
- IS_FALSE_ELSE,
- IS_TRUE_ELSE,
- IS_FALSE_TRAILER,
- IS_COUNT
- } Ifstate;
- static char const * const ifstate_name[] = {
- "OUTSIDE", "FALSE_PREFIX", "TRUE_PREFIX",
- "PASS_MIDDLE", "FALSE_MIDDLE", "TRUE_MIDDLE",
- "PASS_ELSE", "FALSE_ELSE", "TRUE_ELSE",
- "FALSE_TRAILER"
- };
- typedef enum {
- NO_COMMENT = false,
- C_COMMENT,
- CXX_COMMENT,
- STARTING_COMMENT,
- FINISHING_COMMENT,
- CHAR_LITERAL,
- STRING_LITERAL
- } Comment_state;
- static char const * const comment_name[] = {
- "NO", "C", "CXX", "STARTING", "FINISHING", "CHAR", "STRING"
- };
- typedef enum {
- LS_START,
- LS_HASH,
- LS_DIRTY
- } Line_state;
- static char const * const linestate_name[] = {
- "START", "HASH", "DIRTY"
- };
- #define MAXDEPTH 64
- #define MAXLINE 4096
- #define MAXSYMS 16384
- #define EDITSLOP 10
- static bool compblank;
- static bool lnblank;
- static bool complement;
- static bool debugging;
- static bool inplace;
- static bool iocccok;
- static bool strictlogic;
- static bool killconsts;
- static bool lnnum;
- static bool symlist;
- static bool symdepth;
- static bool text;
- static const char *symname[MAXSYMS];
- static const char *value[MAXSYMS];
- static bool ignore[MAXSYMS];
- static int nsyms;
- static FILE *input;
- static const char *filename;
- static int linenum;
- static const char *linefile;
- static FILE *output;
- static const char *ofilename;
- static const char *backext;
- static char *tempname;
- static char tline[MAXLINE+EDITSLOP];
- static char *keyword;
- static const char *newline;
- static const char newline_unix[] = "\n";
- static const char newline_crlf[] = "\r\n";
- static Comment_state incomment;
- static Line_state linestate;
- static Ifstate ifstate[MAXDEPTH];
- static bool ignoring[MAXDEPTH];
- static int stifline[MAXDEPTH];
- static int depth;
- static int delcount;
- static unsigned blankcount;
- static unsigned blankmax;
- static bool constexpr;
- static bool zerosyms;
- static bool firstsym;
- static int exitmode;
- static int exitstat;
- static bool altered;
- static void addsym1(bool, bool, char *);
- static void addsym2(bool, const char *, const char *);
- static char *astrcat(const char *, const char *);
- static void cleantemp(void);
- static void closeio(void);
- static void debug(const char *, ...);
- static void debugsym(const char *, int);
- static bool defundef(void);
- static void defundefile(const char *);
- static void done(void);
- static void error(const char *);
- static int findsym(const char **);
- static void flushline(bool);
- static void hashline(void);
- static void help(void);
- static Linetype ifeval(const char **);
- static void ignoreoff(void);
- static void ignoreon(void);
- static void indirectsym(void);
- static void keywordedit(const char *);
- static const char *matchsym(const char *, const char *);
- static void nest(void);
- static Linetype parseline(void);
- static void process(void);
- static void processinout(const char *, const char *);
- static const char *skipargs(const char *);
- static const char *skipcomment(const char *);
- static const char *skiphash(void);
- static const char *skipline(const char *);
- static const char *skipsym(const char *);
- static void state(Ifstate);
- static void unnest(void);
- static void usage(void);
- static void version(void);
- static const char *xstrdup(const char *, const char *);
- #define endsym(c) (!isalnum((unsigned char)c) && c != '_')
- int
- main(int argc, char *argv[])
- {
- int opt;
- while ((opt = getopt(argc, argv, "i:D:U:f:I:M:o:x:bBcdehKklmnsStV")) != -1)
- switch (opt) {
- case 'i':
-
- opt = *optarg++;
- if (opt == 'D')
- addsym1(true, true, optarg);
- else if (opt == 'U')
- addsym1(true, false, optarg);
- else
- usage();
- break;
- case 'D':
- addsym1(false, true, optarg);
- break;
- case 'U':
- addsym1(false, false, optarg);
- break;
- case 'I':
- break;
- case 'b':
- case 'l':
- lnblank = true;
- break;
- case 'B':
- compblank = true;
- break;
- case 'c':
- complement = true;
- break;
- case 'd':
- debugging = true;
- break;
- case 'e':
- iocccok = true;
- break;
- case 'f':
- defundefile(optarg);
- break;
- case 'h':
- help();
- break;
- case 'K':
- strictlogic = true;
- break;
- case 'k':
- killconsts = true;
- break;
- case 'm':
- inplace = true;
- break;
- case 'M':
- inplace = true;
- backext = optarg;
- break;
- case 'n':
- lnnum = true;
- break;
- case 'o':
- ofilename = optarg;
- break;
- case 's':
- symlist = true;
- break;
- case 'S':
- symlist = symdepth = true;
- break;
- case 't':
- text = true;
- break;
- case 'V':
- version();
- break;
- case 'x':
- exitmode = atoi(optarg);
- if(exitmode < 0 || exitmode > 2)
- usage();
- break;
- default:
- usage();
- }
- argc -= optind;
- argv += optind;
- if (compblank && lnblank)
- errx(2, "-B and -b are mutually exclusive");
- if (symlist && (ofilename != NULL || inplace || argc > 1))
- errx(2, "-s only works with one input file");
- if (argc > 1 && ofilename != NULL)
- errx(2, "-o cannot be used with multiple input files");
- if (argc > 1 && !inplace)
- errx(2, "multiple input files require -m or -M");
- if (argc == 0)
- argc = 1;
- if (argc == 1 && !inplace && ofilename == NULL)
- ofilename = "-";
- indirectsym();
- atexit(cleantemp);
- if (ofilename != NULL)
- processinout(*argv, ofilename);
- else while (argc-- > 0) {
- processinout(*argv, *argv);
- argv++;
- }
- switch(exitmode) {
- case(0): exit(exitstat);
- case(1): exit(!exitstat);
- case(2): exit(0);
- default: abort();
- }
- }
- static void
- processinout(const char *ifn, const char *ofn)
- {
- struct stat st;
- if (ifn == NULL || strcmp(ifn, "-") == 0) {
- filename = "[stdin]";
- linefile = NULL;
- input = fbinmode(stdin);
- } else {
- filename = ifn;
- linefile = ifn;
- input = fopen(ifn, "rb");
- if (input == NULL)
- err(2, "can't open %s", ifn);
- }
- if (strcmp(ofn, "-") == 0) {
- output = fbinmode(stdout);
- process();
- return;
- }
- if (stat(ofn, &st) < 0) {
- output = fopen(ofn, "wb");
- if (output == NULL)
- err(2, "can't create %s", ofn);
- process();
- return;
- }
- tempname = astrcat(ofn, ".XXXXXX");
- output = mktempmode(tempname, st.st_mode);
- if (output == NULL)
- err(2, "can't create %s", tempname);
- process();
- if (backext != NULL) {
- char *backname = astrcat(ofn, backext);
- if (rename(ofn, backname) < 0)
- err(2, "can't rename \"%s\" to \"%s\"", ofn, backname);
- free(backname);
- }
-
- if (!altered && backext == NULL) {
- if (remove(tempname) < 0)
- warn("can't remove \"%s\"", tempname);
- } else if (replace(tempname, ofn) < 0)
- err(2, "can't rename \"%s\" to \"%s\"", tempname, ofn);
- free(tempname);
- tempname = NULL;
- }
- static void
- cleantemp(void)
- {
- if (tempname != NULL)
- remove(tempname);
- }
- static void
- version(void)
- {
- const char *c = copyright;
- for (;;) {
- while (*++c != '$')
- if (*c == '\0')
- exit(0);
- while (*++c != '$')
- putc(*c, stderr);
- putc('\n', stderr);
- }
- }
- static void
- synopsis(FILE *fp)
- {
- fprintf(fp,
- "usage: unifdef [-bBcdehKkmnsStV] [-x{012}] [-Mext] [-opath] \\\n"
- " [-[i]Dsym[=val]] [-[i]Usym] [-fpath] ... [file] ...\n");
- }
- static void
- usage(void)
- {
- synopsis(stderr);
- exit(2);
- }
- static void
- help(void)
- {
- synopsis(stdout);
- printf(
- " -Dsym=val define preprocessor symbol with given value\n"
- " -Dsym define preprocessor symbol with value 1\n"
- " -Usym preprocessor symbol is undefined\n"
- " -iDsym=val \\ ignore C strings and comments\n"
- " -iDsym ) in sections controlled by these\n"
- " -iUsym / preprocessor symbols\n"
- " -fpath file containing #define and #undef directives\n"
- " -b blank lines instead of deleting them\n"
- " -B compress blank lines around deleted section\n"
- " -c complement (invert) keep vs. delete\n"
- " -d debugging mode\n"
- " -e ignore multiline preprocessor directives\n"
- " -h print help\n"
- " -Ipath extra include file path (ignored)\n"
- " -K disable && and || short-circuiting\n"
- " -k process constant #if expressions\n"
- " -Mext modify in place and keep backups\n"
- " -m modify input files in place\n"
- " -n add #line directives to output\n"
- " -opath output file name\n"
- " -S list #if control symbols with nesting\n"
- " -s list #if control symbols\n"
- " -t ignore C strings and comments\n"
- " -V print version\n"
- " -x{012} exit status mode\n"
- );
- exit(0);
- }
- typedef void state_fn(void);
- static void Eelif (void) { error("Inappropriate #elif"); }
- static void Eelse (void) { error("Inappropriate #else"); }
- static void Eendif(void) { error("Inappropriate #endif"); }
- static void Eeof (void) { error("Premature EOF"); }
- static void Eioccc(void) { error("Obfuscated preprocessor control line"); }
- static void print (void) { flushline(true); }
- static void drop (void) { flushline(false); }
- static void Strue (void) { drop(); ignoreoff(); state(IS_TRUE_PREFIX); }
- static void Sfalse(void) { drop(); ignoreoff(); state(IS_FALSE_PREFIX); }
- static void Selse (void) { drop(); state(IS_TRUE_ELSE); }
- static void Pelif (void) { print(); ignoreoff(); state(IS_PASS_MIDDLE); }
- static void Pelse (void) { print(); state(IS_PASS_ELSE); }
- static void Pendif(void) { print(); unnest(); }
- static void Dfalse(void) { drop(); ignoreoff(); state(IS_FALSE_TRAILER); }
- static void Delif (void) { drop(); ignoreoff(); state(IS_FALSE_MIDDLE); }
- static void Delse (void) { drop(); state(IS_FALSE_ELSE); }
- static void Dendif(void) { drop(); unnest(); }
- static void Fdrop (void) { nest(); Dfalse(); }
- static void Fpass (void) { nest(); Pelif(); }
- static void Ftrue (void) { nest(); Strue(); }
- static void Ffalse(void) { nest(); Sfalse(); }
- static void Oiffy (void) { if (!iocccok) Eioccc(); Fpass(); ignoreon(); }
- static void Oif (void) { if (!iocccok) Eioccc(); Fpass(); }
- static void Oelif (void) { if (!iocccok) Eioccc(); Pelif(); }
- static void Idrop (void) { Fdrop(); ignoreon(); }
- static void Itrue (void) { Ftrue(); ignoreon(); }
- static void Ifalse(void) { Ffalse(); ignoreon(); }
- static void Mpass (void) { memcpy(keyword, "if ", 4); Pelif(); }
- static void Mtrue (void) { keywordedit("else"); state(IS_TRUE_MIDDLE); }
- static void Melif (void) { keywordedit("endif"); state(IS_FALSE_TRAILER); }
- static void Melse (void) { keywordedit("endif"); state(IS_FALSE_ELSE); }
- static state_fn * const trans_table[IS_COUNT][LT_COUNT] = {
- { Itrue, Ifalse,Fpass, Ftrue, Ffalse,Eelif, Eelif, Eelif, Eelse, Eendif,
- Oiffy, Oiffy, Fpass, Oif, Oif, Eelif, Eelif, Eelif, Eelse, Eendif,
- print, done, abort },
- { Idrop, Idrop, Fdrop, Fdrop, Fdrop, Mpass, Strue, Sfalse,Selse, Dendif,
- Idrop, Idrop, Fdrop, Fdrop, Fdrop, Mpass, Eioccc,Eioccc,Eioccc,Eioccc,
- drop, Eeof, abort },
- { Itrue, Ifalse,Fpass, Ftrue, Ffalse,Dfalse,Dfalse,Dfalse,Delse, Dendif,
- Oiffy, Oiffy, Fpass, Oif, Oif, Eioccc,Eioccc,Eioccc,Eioccc,Eioccc,
- print, Eeof, abort },
- { Itrue, Ifalse,Fpass, Ftrue, Ffalse,Pelif, Mtrue, Delif, Pelse, Pendif,
- Oiffy, Oiffy, Fpass, Oif, Oif, Pelif, Oelif, Oelif, Pelse, Pendif,
- print, Eeof, abort },
- { Idrop, Idrop, Fdrop, Fdrop, Fdrop, Pelif, Mtrue, Delif, Pelse, Pendif,
- Idrop, Idrop, Fdrop, Fdrop, Fdrop, Eioccc,Eioccc,Eioccc,Eioccc,Eioccc,
- drop, Eeof, abort },
- { Itrue, Ifalse,Fpass, Ftrue, Ffalse,Melif, Melif, Melif, Melse, Pendif,
- Oiffy, Oiffy, Fpass, Oif, Oif, Eioccc,Eioccc,Eioccc,Eioccc,Pendif,
- print, Eeof, abort },
- { Itrue, Ifalse,Fpass, Ftrue, Ffalse,Eelif, Eelif, Eelif, Eelse, Pendif,
- Oiffy, Oiffy, Fpass, Oif, Oif, Eelif, Eelif, Eelif, Eelse, Pendif,
- print, Eeof, abort },
- { Idrop, Idrop, Fdrop, Fdrop, Fdrop, Eelif, Eelif, Eelif, Eelse, Dendif,
- Idrop, Idrop, Fdrop, Fdrop, Fdrop, Eelif, Eelif, Eelif, Eelse, Eioccc,
- drop, Eeof, abort },
- { Itrue, Ifalse,Fpass, Ftrue, Ffalse,Eelif, Eelif, Eelif, Eelse, Dendif,
- Oiffy, Oiffy, Fpass, Oif, Oif, Eelif, Eelif, Eelif, Eelse, Eioccc,
- print, Eeof, abort },
- { Idrop, Idrop, Fdrop, Fdrop, Fdrop, Dfalse,Dfalse,Dfalse,Delse, Dendif,
- Idrop, Idrop, Fdrop, Fdrop, Fdrop, Dfalse,Dfalse,Dfalse,Delse, Eioccc,
- drop, Eeof, abort }
- };
- static void
- ignoreoff(void)
- {
- if (depth == 0)
- abort();
- ignoring[depth] = ignoring[depth-1];
- }
- static void
- ignoreon(void)
- {
- ignoring[depth] = true;
- }
- static void
- keywordedit(const char *replacement)
- {
- snprintf(keyword, tline + sizeof(tline) - keyword,
- "%s%s", replacement, newline);
- altered = true;
- print();
- }
- static void
- nest(void)
- {
- if (depth > MAXDEPTH-1)
- abort();
- if (depth == MAXDEPTH-1)
- error("Too many levels of nesting");
- depth += 1;
- stifline[depth] = linenum;
- }
- static void
- unnest(void)
- {
- if (depth == 0)
- abort();
- depth -= 1;
- }
- static void
- state(Ifstate is)
- {
- ifstate[depth] = is;
- }
- static void
- done(void)
- {
- if (incomment)
- error("EOF in comment");
- closeio();
- }
- static void
- flushline(bool keep)
- {
- if (symlist)
- return;
- if (keep ^ complement) {
- bool blankline = tline[strspn(tline, " \t\r\n")] == '\0';
- if (blankline && compblank && blankcount != blankmax) {
- delcount += 1;
- blankcount += 1;
- } else {
- if (lnnum && delcount > 0)
- hashline();
- if (fputs(tline, output) == EOF)
- closeio();
- delcount = 0;
- blankmax = blankcount = blankline ? blankcount + 1 : 0;
- }
- } else {
- if (lnblank && fputs(newline, output) == EOF)
- closeio();
- altered = true;
- delcount += 1;
- blankcount = 0;
- }
- if (debugging && fflush(output) == EOF)
- closeio();
- }
- static void
- hashline(void)
- {
- int e;
- if (linefile == NULL)
- e = fprintf(output, "#line %d%s", linenum, newline);
- else
- e = fprintf(output, "#line %d \"%s\"%s",
- linenum, linefile, newline);
- if (e < 0)
- closeio();
- }
- static void
- closeio(void)
- {
-
- if (symdepth && !zerosyms)
- printf("\n");
- if (output != NULL && (ferror(output) || fclose(output) == EOF))
- err(2, "%s: can't write to output", filename);
- fclose(input);
- }
- static void
- process(void)
- {
- Linetype lineval = LT_PLAIN;
-
- blankmax = blankcount = 1000;
- zerosyms = true;
- newline = NULL;
- linenum = 0;
- altered = false;
- while (lineval != LT_EOF) {
- lineval = parseline();
- trans_table[ifstate[depth]][lineval]();
- debug("process line %d %s -> %s depth %d",
- linenum, linetype_name[lineval],
- ifstate_name[ifstate[depth]], depth);
- }
- exitstat |= altered;
- }
- static Linetype
- parseline(void)
- {
- const char *cp;
- int cursym;
- Linetype retval;
- Comment_state wascomment;
- wascomment = incomment;
- cp = skiphash();
- if (cp == NULL)
- return (LT_EOF);
- if (newline == NULL) {
- if (strrchr(tline, '\n') == strrchr(tline, '\r') + 1)
- newline = newline_crlf;
- else
- newline = newline_unix;
- }
- if (*cp == '\0') {
- retval = LT_PLAIN;
- goto done;
- }
- keyword = tline + (cp - tline);
- if ((cp = matchsym("ifdef", keyword)) != NULL ||
- (cp = matchsym("ifndef", keyword)) != NULL) {
- cp = skipcomment(cp);
- if ((cursym = findsym(&cp)) < 0)
- retval = LT_IF;
- else {
- retval = (keyword[2] == 'n')
- ? LT_FALSE : LT_TRUE;
- if (value[cursym] == NULL)
- retval = (retval == LT_TRUE)
- ? LT_FALSE : LT_TRUE;
- if (ignore[cursym])
- retval = (retval == LT_TRUE)
- ? LT_TRUEI : LT_FALSEI;
- }
- } else if ((cp = matchsym("if", keyword)) != NULL)
- retval = ifeval(&cp);
- else if ((cp = matchsym("elif", keyword)) != NULL)
- retval = linetype_if2elif(ifeval(&cp));
- else if ((cp = matchsym("else", keyword)) != NULL)
- retval = LT_ELSE;
- else if ((cp = matchsym("endif", keyword)) != NULL)
- retval = LT_ENDIF;
- else {
- cp = skipsym(keyword);
-
- if (strncmp(cp, "\\\r\n", 3) == 0 ||
- strncmp(cp, "\\\n", 2) == 0)
- Eioccc();
- cp = skipline(cp);
- retval = LT_PLAIN;
- goto done;
- }
- cp = skipcomment(cp);
- if (*cp != '\0') {
- cp = skipline(cp);
- if (retval == LT_TRUE || retval == LT_FALSE ||
- retval == LT_TRUEI || retval == LT_FALSEI)
- retval = LT_IF;
- if (retval == LT_ELTRUE || retval == LT_ELFALSE)
- retval = LT_ELIF;
- }
-
- if (linestate == LS_HASH) {
- long len = cp - tline;
- if (fgets(tline + len, MAXLINE - len, input) == NULL) {
- if (ferror(input))
- err(2, "can't read %s", filename);
-
- strcpy(tline + len, newline);
- cp += strlen(newline);
- linestate = LS_START;
- } else {
- linestate = LS_DIRTY;
- }
- }
- if (retval != LT_PLAIN && (wascomment || linestate != LS_START)) {
- retval = linetype_2dodgy(retval);
- linestate = LS_DIRTY;
- }
- done:
- debug("parser line %d state %s comment %s line", linenum,
- comment_name[incomment], linestate_name[linestate]);
- return (retval);
- }
- static Linetype op_strict(long *p, long v, Linetype at, Linetype bt) {
- if(at == LT_IF || bt == LT_IF) return (LT_IF);
- return (*p = v, v ? LT_TRUE : LT_FALSE);
- }
- static Linetype op_lt(long *p, Linetype at, long a, Linetype bt, long b) {
- return op_strict(p, a < b, at, bt);
- }
- static Linetype op_gt(long *p, Linetype at, long a, Linetype bt, long b) {
- return op_strict(p, a > b, at, bt);
- }
- static Linetype op_le(long *p, Linetype at, long a, Linetype bt, long b) {
- return op_strict(p, a <= b, at, bt);
- }
- static Linetype op_ge(long *p, Linetype at, long a, Linetype bt, long b) {
- return op_strict(p, a >= b, at, bt);
- }
- static Linetype op_eq(long *p, Linetype at, long a, Linetype bt, long b) {
- return op_strict(p, a == b, at, bt);
- }
- static Linetype op_ne(long *p, Linetype at, long a, Linetype bt, long b) {
- return op_strict(p, a != b, at, bt);
- }
- static Linetype op_or(long *p, Linetype at, long a, Linetype bt, long b) {
- if (!strictlogic && (at == LT_TRUE || bt == LT_TRUE))
- return (*p = 1, LT_TRUE);
- return op_strict(p, a || b, at, bt);
- }
- static Linetype op_and(long *p, Linetype at, long a, Linetype bt, long b) {
- if (!strictlogic && (at == LT_FALSE || bt == LT_FALSE))
- return (*p = 0, LT_FALSE);
- return op_strict(p, a && b, at, bt);
- }
- struct ops;
- typedef Linetype eval_fn(const struct ops *, long *, const char **);
- static eval_fn eval_table, eval_unary;
- struct op {
- const char *str;
- Linetype (*fn)(long *, Linetype, long, Linetype, long);
- };
- struct ops {
- eval_fn *inner;
- struct op op[5];
- };
- static const struct ops eval_ops[] = {
- { eval_table, { { "||", op_or } } },
- { eval_table, { { "&&", op_and } } },
- { eval_table, { { "==", op_eq },
- { "!=", op_ne } } },
- { eval_unary, { { "<=", op_le },
- { ">=", op_ge },
- { "<", op_lt },
- { ">", op_gt } } }
- };
- static long prec(const struct ops *ops)
- {
- return (ops - eval_ops);
- }
- static Linetype
- eval_unary(const struct ops *ops, long *valp, const char **cpp)
- {
- const char *cp;
- char *ep;
- int sym;
- bool defparen;
- Linetype lt;
- cp = skipcomment(*cpp);
- if (*cp == '!') {
- debug("eval%d !", prec(ops));
- cp++;
- lt = eval_unary(ops, valp, &cp);
- if (lt == LT_ERROR)
- return (LT_ERROR);
- if (lt != LT_IF) {
- *valp = !*valp;
- lt = *valp ? LT_TRUE : LT_FALSE;
- }
- } else if (*cp == '(') {
- cp++;
- debug("eval%d (", prec(ops));
- lt = eval_table(eval_ops, valp, &cp);
- if (lt == LT_ERROR)
- return (LT_ERROR);
- cp = skipcomment(cp);
- if (*cp++ != ')')
- return (LT_ERROR);
- } else if (isdigit((unsigned char)*cp)) {
- debug("eval%d number", prec(ops));
- *valp = strtol(cp, &ep, 0);
- if (ep == cp)
- return (LT_ERROR);
- lt = *valp ? LT_TRUE : LT_FALSE;
- cp = ep;
- } else if (matchsym("defined", cp) != NULL) {
- cp = skipcomment(cp+7);
- if (*cp == '(') {
- cp = skipcomment(cp+1);
- defparen = true;
- } else {
- defparen = false;
- }
- sym = findsym(&cp);
- cp = skipcomment(cp);
- if (defparen && *cp++ != ')') {
- debug("eval%d defined missing ')'", prec(ops));
- return (LT_ERROR);
- }
- if (sym < 0) {
- debug("eval%d defined unknown", prec(ops));
- lt = LT_IF;
- } else {
- debug("eval%d defined %s", prec(ops), symname[sym]);
- *valp = (value[sym] != NULL);
- lt = *valp ? LT_TRUE : LT_FALSE;
- }
- constexpr = false;
- } else if (!endsym(*cp)) {
- debug("eval%d symbol", prec(ops));
- sym = findsym(&cp);
- if (sym < 0) {
- lt = LT_IF;
- cp = skipargs(cp);
- } else if (value[sym] == NULL) {
- *valp = 0;
- lt = LT_FALSE;
- } else {
- *valp = strtol(value[sym], &ep, 0);
- if (*ep != '\0' || ep == value[sym])
- return (LT_ERROR);
- lt = *valp ? LT_TRUE : LT_FALSE;
- cp = skipargs(cp);
- }
- constexpr = false;
- } else {
- debug("eval%d bad expr", prec(ops));
- return (LT_ERROR);
- }
- *cpp = cp;
- debug("eval%d = %d", prec(ops), *valp);
- return (lt);
- }
- static Linetype
- eval_table(const struct ops *ops, long *valp, const char **cpp)
- {
- const struct op *op;
- const char *cp;
- long val;
- Linetype lt, rt;
- debug("eval%d", prec(ops));
- cp = *cpp;
- lt = ops->inner(ops+1, valp, &cp);
- if (lt == LT_ERROR)
- return (LT_ERROR);
- for (;;) {
- cp = skipcomment(cp);
- for (op = ops->op; op->str != NULL; op++)
- if (strncmp(cp, op->str, strlen(op->str)) == 0)
- break;
- if (op->str == NULL)
- break;
- cp += strlen(op->str);
- debug("eval%d %s", prec(ops), op->str);
- rt = ops->inner(ops+1, &val, &cp);
- if (rt == LT_ERROR)
- return (LT_ERROR);
- lt = op->fn(valp, lt, *valp, rt, val);
- }
- *cpp = cp;
- debug("eval%d = %d", prec(ops), *valp);
- debug("eval%d lt = %s", prec(ops), linetype_name[lt]);
- return (lt);
- }
- static Linetype
- ifeval(const char **cpp)
- {
- Linetype ret;
- long val = 0;
- debug("eval %s", *cpp);
- constexpr = killconsts ? false : true;
- ret = eval_table(eval_ops, &val, cpp);
- debug("eval = %d", val);
- return (constexpr ? LT_IF : ret == LT_ERROR ? LT_IF : ret);
- }
- static const char *
- skiphash(void)
- {
- const char *cp;
- linenum++;
- if (fgets(tline, MAXLINE, input) == NULL) {
- if (ferror(input))
- err(2, "can't read %s", filename);
- else
- return (NULL);
- }
- cp = skipcomment(tline);
- if (linestate == LS_START && *cp == '#') {
- linestate = LS_HASH;
- return (skipcomment(cp + 1));
- } else if (*cp == '\0') {
- return (cp);
- } else {
- return (skipline(cp));
- }
- }
- static const char *
- skipline(const char *cp)
- {
- if (*cp != '\0')
- linestate = LS_DIRTY;
- while (*cp != '\0')
- cp = skipcomment(cp + 1);
- return (cp);
- }
- static const char *
- skipcomment(const char *cp)
- {
- if (text || ignoring[depth]) {
- for (; isspace((unsigned char)*cp); cp++)
- if (*cp == '\n')
- linestate = LS_START;
- return (cp);
- }
- while (*cp != '\0')
-
- if (strncmp(cp, "\\\r\n", 3) == 0)
- cp += 3;
- else if (strncmp(cp, "\\\n", 2) == 0)
- cp += 2;
- else switch (incomment) {
- case NO_COMMENT:
- if (strncmp(cp, "/\\\r\n", 4) == 0) {
- incomment = STARTING_COMMENT;
- cp += 4;
- } else if (strncmp(cp, "/\\\n", 3) == 0) {
- incomment = STARTING_COMMENT;
- cp += 3;
- } else if (strncmp(cp, "/*", 2) == 0) {
- incomment = C_COMMENT;
- cp += 2;
- } else if (strncmp(cp, "//", 2) == 0) {
- incomment = CXX_COMMENT;
- cp += 2;
- } else if (strncmp(cp, "\'", 1) == 0) {
- incomment = CHAR_LITERAL;
- linestate = LS_DIRTY;
- cp += 1;
- } else if (strncmp(cp, "\"", 1) == 0) {
- incomment = STRING_LITERAL;
- linestate = LS_DIRTY;
- cp += 1;
- } else if (strncmp(cp, "\n", 1) == 0) {
- linestate = LS_START;
- cp += 1;
- } else if (strchr(" \r\t", *cp) != NULL) {
- cp += 1;
- } else
- return (cp);
- continue;
- case CXX_COMMENT:
- if (strncmp(cp, "\n", 1) == 0) {
- incomment = NO_COMMENT;
- linestate = LS_START;
- }
- cp += 1;
- continue;
- case CHAR_LITERAL:
- case STRING_LITERAL:
- if ((incomment == CHAR_LITERAL && cp[0] == '\'') ||
- (incomment == STRING_LITERAL && cp[0] == '\"')) {
- incomment = NO_COMMENT;
- cp += 1;
- } else if (cp[0] == '\\') {
- if (cp[1] == '\0')
- cp += 1;
- else
- cp += 2;
- } else if (strncmp(cp, "\n", 1) == 0) {
- if (incomment == CHAR_LITERAL)
- error("unterminated char literal");
- else
- error("unterminated string literal");
- } else
- cp += 1;
- continue;
- case C_COMMENT:
- if (strncmp(cp, "*\\\r\n", 4) == 0) {
- incomment = FINISHING_COMMENT;
- cp += 4;
- } else if (strncmp(cp, "*\\\n", 3) == 0) {
- incomment = FINISHING_COMMENT;
- cp += 3;
- } else if (strncmp(cp, "*/", 2) == 0) {
- incomment = NO_COMMENT;
- cp += 2;
- } else
- cp += 1;
- continue;
- case STARTING_COMMENT:
- if (*cp == '*') {
- incomment = C_COMMENT;
- cp += 1;
- } else if (*cp == '/') {
- incomment = CXX_COMMENT;
- cp += 1;
- } else {
- incomment = NO_COMMENT;
- linestate = LS_DIRTY;
- }
- continue;
- case FINISHING_COMMENT:
- if (*cp == '/') {
- incomment = NO_COMMENT;
- cp += 1;
- } else
- incomment = C_COMMENT;
- continue;
- default:
- abort();
- }
- return (cp);
- }
- static const char *
- skipargs(const char *cp)
- {
- const char *ocp = cp;
- int level = 0;
- cp = skipcomment(cp);
- if (*cp != '(')
- return (cp);
- do {
- if (*cp == '(')
- level++;
- if (*cp == ')')
- level--;
- cp = skipcomment(cp+1);
- } while (level != 0 && *cp != '\0');
- if (level == 0)
- return (cp);
- else
-
- return (ocp);
- }
- static const char *
- skipsym(const char *cp)
- {
- while (!endsym(*cp))
- ++cp;
- return (cp);
- }
- static const char *
- getsym(const char **cpp)
- {
- const char *cp = *cpp, *sym;
- cp = skipcomment(cp);
- cp = skipsym(sym = cp);
- if (cp == sym)
- return NULL;
- *cpp = cp;
- return (xstrdup(sym, cp));
- }
- static const char *
- matchsym(const char *s, const char *t)
- {
- while (*s != '\0' && *t != '\0')
- if (*s != *t)
- return (NULL);
- else
- ++s, ++t;
- if (*s == '\0' && endsym(*t))
- return(t);
- else
- return(NULL);
- }
- static int
- findsym(const char **strp)
- {
- const char *str;
- int symind;
- str = *strp;
- *strp = skipsym(str);
- if (symlist) {
- if (*strp == str)
- return (-1);
- if (symdepth && firstsym)
- printf("%s%3d", zerosyms ? "" : "\n", depth);
- firstsym = zerosyms = false;
- printf("%s%.*s%s",
- symdepth ? " " : "",
- (int)(*strp-str), str,
- symdepth ? "" : "\n");
-
- return (0);
- }
- for (symind = 0; symind < nsyms; ++symind) {
- if (matchsym(symname[symind], str) != NULL) {
- debugsym("findsym", symind);
- return (symind);
- }
- }
- return (-1);
- }
- static void
- indirectsym(void)
- {
- const char *cp;
- int changed, sym, ind;
- do {
- changed = 0;
- for (sym = 0; sym < nsyms; ++sym) {
- if (value[sym] == NULL)
- continue;
- cp = value[sym];
- ind = findsym(&cp);
- if (ind == -1 || ind == sym ||
- *cp != '\0' ||
- value[ind] == NULL ||
- value[ind] == value[sym])
- continue;
- debugsym("indir...", sym);
- value[sym] = value[ind];
- debugsym("...ectsym", sym);
- changed++;
- }
- } while (changed);
- }
- static void
- addsym1(bool ignorethis, bool definethis, char *symval)
- {
- const char *sym, *val;
- sym = symval;
- val = skipsym(sym);
- if (definethis && *val == '=') {
- symval[val - sym] = '\0';
- val = val + 1;
- } else if (*val == '\0') {
- val = definethis ? "1" : NULL;
- } else {
- usage();
- }
- addsym2(ignorethis, sym, val);
- }
- static void
- addsym2(bool ignorethis, const char *sym, const char *val)
- {
- const char *cp = sym;
- int symind;
- symind = findsym(&cp);
- if (symind < 0) {
- if (nsyms >= MAXSYMS)
- errx(2, "too many symbols");
- symind = nsyms++;
- }
- ignore[symind] = ignorethis;
- symname[symind] = sym;
- value[symind] = val;
- debugsym("addsym", symind);
- }
- static void
- debugsym(const char *why, int symind)
- {
- debug("%s %s%c%s", why, symname[symind],
- value[symind] ? '=' : ' ',
- value[symind] ? value[symind] : "undef");
- }
- static void
- defundefile(const char *fn)
- {
- filename = fn;
- input = fopen(fn, "rb");
- if (input == NULL)
- err(2, "can't open %s", fn);
- linenum = 0;
- while (defundef())
- ;
- if (ferror(input))
- err(2, "can't read %s", filename);
- else
- fclose(input);
- if (incomment)
- error("EOF in comment");
- }
- static bool
- defundef(void)
- {
- const char *cp, *kw, *sym, *val, *end;
- cp = skiphash();
- if (cp == NULL)
- return (false);
- if (*cp == '\0')
- goto done;
-
- end = cp + strlen(cp);
- while (end > tline && strchr(" \t\n\r", end[-1]) != NULL)
- --end;
- if (end > tline && end[-1] == '\\')
- Eioccc();
- kw = cp;
- if ((cp = matchsym("define", kw)) != NULL) {
- sym = getsym(&cp);
- if (sym == NULL)
- error("missing macro name in #define");
- if (*cp == '(') {
- val = "1";
- } else {
- cp = skipcomment(cp);
- val = (cp < end) ? xstrdup(cp, end) : "";
- }
- debug("#define");
- addsym2(false, sym, val);
- } else if ((cp = matchsym("undef", kw)) != NULL) {
- sym = getsym(&cp);
- if (sym == NULL)
- error("missing macro name in #undef");
- cp = skipcomment(cp);
- debug("#undef");
- addsym2(false, sym, NULL);
- } else {
- error("unrecognized preprocessor directive");
- }
- skipline(cp);
- done:
- debug("parser line %d state %s comment %s line", linenum,
- comment_name[incomment], linestate_name[linestate]);
- return (true);
- }
- static char *
- astrcat(const char *s1, const char *s2)
- {
- char *s;
- int len;
- size_t size;
- len = snprintf(NULL, 0, "%s%s", s1, s2);
- if (len < 0)
- err(2, "snprintf");
- size = (size_t)len + 1;
- s = (char *)malloc(size);
- if (s == NULL)
- err(2, "malloc");
- snprintf(s, size, "%s%s", s1, s2);
- return (s);
- }
- static const char *
- xstrdup(const char *start, const char *end)
- {
- size_t n;
- char *s;
- if (end < start) abort();
- n = (size_t)(end - start) + 1;
- s = malloc(n);
- if (s == NULL)
- err(2, "malloc");
- snprintf(s, n, "%s", start);
- return (s);
- }
- static void
- debug(const char *msg, ...)
- {
- va_list ap;
- if (debugging) {
- va_start(ap, msg);
- vwarnx(msg, ap);
- va_end(ap);
- }
- }
- static void
- error(const char *msg)
- {
- if (depth == 0)
- warnx("%s: %d: %s", filename, linenum, msg);
- else
- warnx("%s: %d: %s (#if line %d depth %d)",
- filename, linenum, msg, stifline[depth], depth);
- closeio();
- errx(2, "output may be truncated");
- }
|