unifdef.c 43 KB

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