/usr.bin/unifdef/unifdef.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 1235 lines · 959 code · 64 blank · 212 comment · 344 complexity · 97a055ce30e7cd0d8edb00179d9bc61a MD5 · raw file

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