unifdef.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074
  1. /*
  2. * Copyright (c) 2002 - 2005 Tony Finch <dot@dotat.at>. All rights reserved.
  3. *
  4. * This code is derived from software contributed to Berkeley by Dave Yost.
  5. * It was rewritten to support ANSI C by Tony Finch. The original version of
  6. * unifdef carried the following copyright notice. None of its code remains
  7. * in this version (though some of the names remain).
  8. *
  9. * Copyright (c) 1985, 1993
  10. * The Regents of the University of California. All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions
  14. * are met:
  15. * 1. Redistributions of source code must retain the above copyright
  16. * notice, this list of conditions and the following disclaimer.
  17. * 2. Redistributions in binary form must reproduce the above copyright
  18. * notice, this list of conditions and the following disclaimer in the
  19. * documentation and/or other materials provided with the distribution.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  22. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  25. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. */
  33. #include <sys/cdefs.h>
  34. #ifndef lint
  35. #if 0
  36. static const char copyright[] =
  37. "@(#) Copyright (c) 1985, 1993\n\
  38. The Regents of the University of California. All rights reserved.\n";
  39. #endif
  40. #ifdef __IDSTRING
  41. __IDSTRING(Berkeley, "@(#)unifdef.c 8.1 (Berkeley) 6/6/93");
  42. __IDSTRING(NetBSD, "$NetBSD: unifdef.c,v 1.8 2000/07/03 02:51:36 matt Exp $");
  43. __IDSTRING(dotat, "$dotat: things/unifdef.c,v 1.171 2005/03/08 12:38:48 fanf2 Exp $");
  44. #endif
  45. #endif /* not lint */
  46. #ifdef __FBSDID
  47. __FBSDID("$FreeBSD: /repoman/r/ncvs/src/usr.bin/unifdef/unifdef.c,v 1.20 2005/05/21 09:55:09 ru Exp $");
  48. #endif
  49. /*
  50. * unifdef - remove ifdef'ed lines
  51. *
  52. * Wishlist:
  53. * provide an option which will append the name of the
  54. * appropriate symbol after #else's and #endif's
  55. * provide an option which will check symbols after
  56. * #else's and #endif's to see that they match their
  57. * corresponding #ifdef or #ifndef
  58. *
  59. * The first two items above require better buffer handling, which would
  60. * also make it possible to handle all "dodgy" directives correctly.
  61. */
  62. #include <errno.h>
  63. #include <ctype.h>
  64. #include <stdarg.h>
  65. #include <stdbool.h>
  66. #include <stdio.h>
  67. #include <stdlib.h>
  68. #include <string.h>
  69. #include <unistd.h>
  70. /* Avoid err.h since uClibc can disable these things */
  71. #define vwarnx(fmt, args) ({ fprintf(stderr, "unifdef: "); vfprintf(stderr, fmt, args); fprintf(stderr, "\n"); })
  72. #define warnx(fmt, args...) fprintf(stderr, "unifdef: " fmt "\n", ## args)
  73. #define errx(exit_code, fmt, args...) ({ warnx(fmt, ## args); exit(exit_code); })
  74. #define err(exit_code, fmt, args...) errx(exit_code, fmt ": %s", ## args, strerror(errno))
  75. size_t strlcpy(char *dst, const char *src, size_t siz);
  76. /* types of input lines: */
  77. typedef enum {
  78. LT_TRUEI, /* a true #if with ignore flag */
  79. LT_FALSEI, /* a false #if with ignore flag */
  80. LT_IF, /* an unknown #if */
  81. LT_TRUE, /* a true #if */
  82. LT_FALSE, /* a false #if */
  83. LT_ELIF, /* an unknown #elif */
  84. LT_ELTRUE, /* a true #elif */
  85. LT_ELFALSE, /* a false #elif */
  86. LT_ELSE, /* #else */
  87. LT_ENDIF, /* #endif */
  88. LT_DODGY, /* flag: directive is not on one line */
  89. LT_DODGY_LAST = LT_DODGY + LT_ENDIF,
  90. LT_PLAIN, /* ordinary line */
  91. LT_EOF, /* end of file */
  92. LT_COUNT
  93. } Linetype;
  94. static char const * const linetype_name[] = {
  95. "TRUEI", "FALSEI", "IF", "TRUE", "FALSE",
  96. "ELIF", "ELTRUE", "ELFALSE", "ELSE", "ENDIF",
  97. "DODGY TRUEI", "DODGY FALSEI",
  98. "DODGY IF", "DODGY TRUE", "DODGY FALSE",
  99. "DODGY ELIF", "DODGY ELTRUE", "DODGY ELFALSE",
  100. "DODGY ELSE", "DODGY ENDIF",
  101. "PLAIN", "EOF"
  102. };
  103. /* state of #if processing */
  104. typedef enum {
  105. IS_OUTSIDE,
  106. IS_FALSE_PREFIX, /* false #if followed by false #elifs */
  107. IS_TRUE_PREFIX, /* first non-false #(el)if is true */
  108. IS_PASS_MIDDLE, /* first non-false #(el)if is unknown */
  109. IS_FALSE_MIDDLE, /* a false #elif after a pass state */
  110. IS_TRUE_MIDDLE, /* a true #elif after a pass state */
  111. IS_PASS_ELSE, /* an else after a pass state */
  112. IS_FALSE_ELSE, /* an else after a true state */
  113. IS_TRUE_ELSE, /* an else after only false states */
  114. IS_FALSE_TRAILER, /* #elifs after a true are false */
  115. IS_COUNT
  116. } Ifstate;
  117. static char const * const ifstate_name[] = {
  118. "OUTSIDE", "FALSE_PREFIX", "TRUE_PREFIX",
  119. "PASS_MIDDLE", "FALSE_MIDDLE", "TRUE_MIDDLE",
  120. "PASS_ELSE", "FALSE_ELSE", "TRUE_ELSE",
  121. "FALSE_TRAILER"
  122. };
  123. /* state of comment parser */
  124. typedef enum {
  125. NO_COMMENT = false, /* outside a comment */
  126. C_COMMENT, /* in a comment like this one */
  127. CXX_COMMENT, /* between // and end of line */
  128. STARTING_COMMENT, /* just after slash-backslash-newline */
  129. FINISHING_COMMENT, /* star-backslash-newline in a C comment */
  130. CHAR_LITERAL, /* inside '' */
  131. STRING_LITERAL /* inside "" */
  132. } Comment_state;
  133. static char const * const comment_name[] = {
  134. "NO", "C", "CXX", "STARTING", "FINISHING", "CHAR", "STRING"
  135. };
  136. /* state of preprocessor line parser */
  137. typedef enum {
  138. LS_START, /* only space and comments on this line */
  139. LS_HASH, /* only space, comments, and a hash */
  140. LS_DIRTY /* this line can't be a preprocessor line */
  141. } Line_state;
  142. static char const * const linestate_name[] = {
  143. "START", "HASH", "DIRTY"
  144. };
  145. /*
  146. * Minimum translation limits from ISO/IEC 9899:1999 5.2.4.1
  147. */
  148. #define MAXDEPTH 64 /* maximum #if nesting */
  149. #define MAXLINE 4096 /* maximum length of line */
  150. #define MAXSYMS 4096 /* maximum number of symbols */
  151. /*
  152. * Sometimes when editing a keyword the replacement text is longer, so
  153. * we leave some space at the end of the tline buffer to accommodate this.
  154. */
  155. #define EDITSLOP 10
  156. /*
  157. * Globals.
  158. */
  159. static bool complement; /* -c: do the complement */
  160. static bool debugging; /* -d: debugging reports */
  161. static bool iocccok; /* -e: fewer IOCCC errors */
  162. static bool killconsts; /* -k: eval constant #ifs */
  163. static bool lnblank; /* -l: blank deleted lines */
  164. static bool lnnum; /* -n: add #line directives */
  165. static bool symlist; /* -s: output symbol list */
  166. static bool text; /* -t: this is a text file */
  167. static const char *symname[MAXSYMS]; /* symbol name */
  168. static const char *value[MAXSYMS]; /* -Dsym=value */
  169. static bool ignore[MAXSYMS]; /* -iDsym or -iUsym */
  170. static int nsyms; /* number of symbols */
  171. static FILE *input; /* input file pointer */
  172. static const char *filename; /* input file name */
  173. static int linenum; /* current line number */
  174. static char tline[MAXLINE+EDITSLOP];/* input buffer plus space */
  175. static char *keyword; /* used for editing #elif's */
  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 bool keepthis; /* don't delete constant #if */
  184. static int exitstat; /* program exit status */
  185. static void addsym(bool, bool, char *);
  186. static void debug(const char *, ...);
  187. static void done(void);
  188. static void error(const char *);
  189. static int findsym(const char *);
  190. static void flushline(bool);
  191. static Linetype get_line(void);
  192. static Linetype ifeval(const char **);
  193. static void ignoreoff(void);
  194. static void ignoreon(void);
  195. static void keywordedit(const char *);
  196. static void nest(void);
  197. static void process(void);
  198. static const char *skipcomment(const char *);
  199. static const char *skipsym(const char *);
  200. static void state(Ifstate);
  201. static int strlcmp(const char *, const char *, size_t);
  202. static void unnest(void);
  203. static void usage(void);
  204. #define endsym(c) (!isalpha((unsigned char)c) && !isdigit((unsigned char)c) && c != '_')
  205. /*
  206. * The main program.
  207. */
  208. int
  209. main(int argc, char *argv[])
  210. {
  211. int opt;
  212. while ((opt = getopt(argc, argv, "i:D:U:I:cdeklnst")) != -1)
  213. switch (opt) {
  214. case 'i': /* treat stuff controlled by these symbols as text */
  215. /*
  216. * For strict backwards-compatibility the U or D
  217. * should be immediately after the -i but it doesn't
  218. * matter much if we relax that requirement.
  219. */
  220. opt = *optarg++;
  221. if (opt == 'D')
  222. addsym(true, true, optarg);
  223. else if (opt == 'U')
  224. addsym(true, false, optarg);
  225. else
  226. usage();
  227. break;
  228. case 'D': /* define a symbol */
  229. addsym(false, true, optarg);
  230. break;
  231. case 'U': /* undef a symbol */
  232. addsym(false, false, optarg);
  233. break;
  234. case 'I':
  235. /* no-op for compatibility with cpp */
  236. break;
  237. case 'c': /* treat -D as -U and vice versa */
  238. complement = true;
  239. break;
  240. case 'd':
  241. debugging = true;
  242. break;
  243. case 'e': /* fewer errors from dodgy lines */
  244. iocccok = true;
  245. break;
  246. case 'k': /* process constant #ifs */
  247. killconsts = true;
  248. break;
  249. case 'l': /* blank deleted lines instead of omitting them */
  250. lnblank = true;
  251. break;
  252. case 'n': /* add #line directive after deleted lines */
  253. lnnum = true;
  254. break;
  255. case 's': /* only output list of symbols that control #ifs */
  256. symlist = true;
  257. break;
  258. case 't': /* don't parse C comments */
  259. text = true;
  260. break;
  261. default:
  262. usage();
  263. }
  264. argc -= optind;
  265. argv += optind;
  266. if (argc > 1) {
  267. errx(2, "can only do one file");
  268. } else if (argc == 1 && strcmp(*argv, "-") != 0) {
  269. filename = *argv;
  270. input = fopen(filename, "r");
  271. if (input == NULL)
  272. err(2, "can't open %s", filename);
  273. } else {
  274. filename = "[stdin]";
  275. input = stdin;
  276. }
  277. process();
  278. debug("bug at line %d", __LINE__);
  279. abort(); /* bug */
  280. }
  281. static void
  282. usage(void)
  283. {
  284. fprintf(stderr, "usage: unifdef [-cdeklnst] [-Ipath]"
  285. " [-Dsym[=val]] [-Usym] [-iDsym[=val]] [-iUsym] ... [file]\n");
  286. exit(2);
  287. }
  288. /*
  289. * A state transition function alters the global #if processing state
  290. * in a particular way. The table below is indexed by the current
  291. * processing state and the type of the current line.
  292. *
  293. * Nesting is handled by keeping a stack of states; some transition
  294. * functions increase or decrease the depth. They also maintain the
  295. * ignore state on a stack. In some complicated cases they have to
  296. * alter the preprocessor directive, as follows.
  297. *
  298. * When we have processed a group that starts off with a known-false
  299. * #if/#elif sequence (which has therefore been deleted) followed by a
  300. * #elif that we don't understand and therefore must keep, we edit the
  301. * latter into a #if to keep the nesting correct.
  302. *
  303. * When we find a true #elif in a group, the following block will
  304. * always be kept and the rest of the sequence after the next #elif or
  305. * #else will be discarded. We edit the #elif into a #else and the
  306. * following directive to #endif since this has the desired behaviour.
  307. *
  308. * "Dodgy" directives are split across multiple lines, the most common
  309. * example being a multi-line comment hanging off the right of the
  310. * directive. We can handle them correctly only if there is no change
  311. * from printing to dropping (or vice versa) caused by that directive.
  312. * If the directive is the first of a group we have a choice between
  313. * failing with an error, or passing it through unchanged instead of
  314. * evaluating it. The latter is not the default to avoid questions from
  315. * users about unifdef unexpectedly leaving behind preprocessor directives.
  316. */
  317. typedef void state_fn(void);
  318. /* report an error */
  319. static void Eelif (void) { error("Inappropriate #elif"); }
  320. static void Eelse (void) { error("Inappropriate #else"); }
  321. static void Eendif(void) { error("Inappropriate #endif"); }
  322. static void Eeof (void) { error("Premature EOF"); }
  323. static void Eioccc(void) { error("Obfuscated preprocessor control line"); }
  324. /* plain line handling */
  325. static void print (void) { flushline(true); }
  326. static void drop (void) { flushline(false); }
  327. /* output lacks group's start line */
  328. static void Strue (void) { drop(); ignoreoff(); state(IS_TRUE_PREFIX); }
  329. static void Sfalse(void) { drop(); ignoreoff(); state(IS_FALSE_PREFIX); }
  330. static void Selse (void) { drop(); state(IS_TRUE_ELSE); }
  331. /* print/pass this block */
  332. static void Pelif (void) { print(); ignoreoff(); state(IS_PASS_MIDDLE); }
  333. static void Pelse (void) { print(); state(IS_PASS_ELSE); }
  334. static void Pendif(void) { print(); unnest(); }
  335. /* discard this block */
  336. static void Dfalse(void) { drop(); ignoreoff(); state(IS_FALSE_TRAILER); }
  337. static void Delif (void) { drop(); ignoreoff(); state(IS_FALSE_MIDDLE); }
  338. static void Delse (void) { drop(); state(IS_FALSE_ELSE); }
  339. static void Dendif(void) { drop(); unnest(); }
  340. /* first line of group */
  341. static void Fdrop (void) { nest(); Dfalse(); }
  342. static void Fpass (void) { nest(); Pelif(); }
  343. static void Ftrue (void) { nest(); Strue(); }
  344. static void Ffalse(void) { nest(); Sfalse(); }
  345. /* variable pedantry for obfuscated lines */
  346. static void Oiffy (void) { if (!iocccok) Eioccc(); Fpass(); ignoreon(); }
  347. static void Oif (void) { if (!iocccok) Eioccc(); Fpass(); }
  348. static void Oelif (void) { if (!iocccok) Eioccc(); Pelif(); }
  349. /* ignore comments in this block */
  350. static void Idrop (void) { Fdrop(); ignoreon(); }
  351. static void Itrue (void) { Ftrue(); ignoreon(); }
  352. static void Ifalse(void) { Ffalse(); ignoreon(); }
  353. /* edit this line */
  354. static void Mpass (void) { strncpy(keyword, "if ", 4); Pelif(); }
  355. static void Mtrue (void) { keywordedit("else\n"); state(IS_TRUE_MIDDLE); }
  356. static void Melif (void) { keywordedit("endif\n"); state(IS_FALSE_TRAILER); }
  357. static void Melse (void) { keywordedit("endif\n"); state(IS_FALSE_ELSE); }
  358. static state_fn * const trans_table[IS_COUNT][LT_COUNT] = {
  359. /* IS_OUTSIDE */
  360. { Itrue, Ifalse,Fpass, Ftrue, Ffalse,Eelif, Eelif, Eelif, Eelse, Eendif,
  361. Oiffy, Oiffy, Fpass, Oif, Oif, Eelif, Eelif, Eelif, Eelse, Eendif,
  362. print, done },
  363. /* IS_FALSE_PREFIX */
  364. { Idrop, Idrop, Fdrop, Fdrop, Fdrop, Mpass, Strue, Sfalse,Selse, Dendif,
  365. Idrop, Idrop, Fdrop, Fdrop, Fdrop, Mpass, Eioccc,Eioccc,Eioccc,Eioccc,
  366. drop, Eeof },
  367. /* IS_TRUE_PREFIX */
  368. { Itrue, Ifalse,Fpass, Ftrue, Ffalse,Dfalse,Dfalse,Dfalse,Delse, Dendif,
  369. Oiffy, Oiffy, Fpass, Oif, Oif, Eioccc,Eioccc,Eioccc,Eioccc,Eioccc,
  370. print, Eeof },
  371. /* IS_PASS_MIDDLE */
  372. { Itrue, Ifalse,Fpass, Ftrue, Ffalse,Pelif, Mtrue, Delif, Pelse, Pendif,
  373. Oiffy, Oiffy, Fpass, Oif, Oif, Pelif, Oelif, Oelif, Pelse, Pendif,
  374. print, Eeof },
  375. /* IS_FALSE_MIDDLE */
  376. { Idrop, Idrop, Fdrop, Fdrop, Fdrop, Pelif, Mtrue, Delif, Pelse, Pendif,
  377. Idrop, Idrop, Fdrop, Fdrop, Fdrop, Eioccc,Eioccc,Eioccc,Eioccc,Eioccc,
  378. drop, Eeof },
  379. /* IS_TRUE_MIDDLE */
  380. { Itrue, Ifalse,Fpass, Ftrue, Ffalse,Melif, Melif, Melif, Melse, Pendif,
  381. Oiffy, Oiffy, Fpass, Oif, Oif, Eioccc,Eioccc,Eioccc,Eioccc,Pendif,
  382. print, Eeof },
  383. /* IS_PASS_ELSE */
  384. { Itrue, Ifalse,Fpass, Ftrue, Ffalse,Eelif, Eelif, Eelif, Eelse, Pendif,
  385. Oiffy, Oiffy, Fpass, Oif, Oif, Eelif, Eelif, Eelif, Eelse, Pendif,
  386. print, Eeof },
  387. /* IS_FALSE_ELSE */
  388. { Idrop, Idrop, Fdrop, Fdrop, Fdrop, Eelif, Eelif, Eelif, Eelse, Dendif,
  389. Idrop, Idrop, Fdrop, Fdrop, Fdrop, Eelif, Eelif, Eelif, Eelse, Eioccc,
  390. drop, Eeof },
  391. /* IS_TRUE_ELSE */
  392. { Itrue, Ifalse,Fpass, Ftrue, Ffalse,Eelif, Eelif, Eelif, Eelse, Dendif,
  393. Oiffy, Oiffy, Fpass, Oif, Oif, Eelif, Eelif, Eelif, Eelse, Eioccc,
  394. print, Eeof },
  395. /* IS_FALSE_TRAILER */
  396. { Idrop, Idrop, Fdrop, Fdrop, Fdrop, Dfalse,Dfalse,Dfalse,Delse, Dendif,
  397. Idrop, Idrop, Fdrop, Fdrop, Fdrop, Dfalse,Dfalse,Dfalse,Delse, Eioccc,
  398. drop, Eeof }
  399. /*TRUEI FALSEI IF TRUE FALSE ELIF ELTRUE ELFALSE ELSE ENDIF
  400. TRUEI FALSEI IF TRUE FALSE ELIF ELTRUE ELFALSE ELSE ENDIF (DODGY)
  401. PLAIN EOF */
  402. };
  403. /*
  404. * State machine utility functions
  405. */
  406. static void
  407. done(void)
  408. {
  409. if (incomment)
  410. error("EOF in comment");
  411. exit(exitstat);
  412. }
  413. static void
  414. ignoreoff(void)
  415. {
  416. if (depth == 0) {
  417. debug("bug at line %d", __LINE__);
  418. abort(); /* bug */
  419. }
  420. ignoring[depth] = ignoring[depth-1];
  421. }
  422. static void
  423. ignoreon(void)
  424. {
  425. ignoring[depth] = true;
  426. }
  427. static void
  428. keywordedit(const char *replacement)
  429. {
  430. size_t size = tline + sizeof(tline) - keyword;
  431. char *dst = keyword;
  432. const char *src = replacement;
  433. if (size != 0) {
  434. while ((--size != 0) && (*src != '\0'))
  435. *dst++ = *src++;
  436. *dst = '\0';
  437. }
  438. print();
  439. }
  440. static void
  441. nest(void)
  442. {
  443. depth += 1;
  444. if (depth >= MAXDEPTH)
  445. error("Too many levels of nesting");
  446. stifline[depth] = linenum;
  447. }
  448. static void
  449. unnest(void)
  450. {
  451. if (depth == 0) {
  452. debug("bug at line %d", __LINE__);
  453. abort(); /* bug */
  454. }
  455. depth -= 1;
  456. }
  457. static void
  458. state(Ifstate is)
  459. {
  460. ifstate[depth] = is;
  461. }
  462. /*
  463. * Write a line to the output or not, according to command line options.
  464. */
  465. static void
  466. flushline(bool keep)
  467. {
  468. if (symlist)
  469. return;
  470. if (keep ^ complement) {
  471. if (lnnum && delcount > 0)
  472. printf("#line %d\n", linenum);
  473. fputs(tline, stdout);
  474. delcount = 0;
  475. } else {
  476. if (lnblank)
  477. putc('\n', stdout);
  478. exitstat = 1;
  479. delcount += 1;
  480. }
  481. }
  482. /*
  483. * The driver for the state machine.
  484. */
  485. static void
  486. process(void)
  487. {
  488. Linetype lineval;
  489. for (;;) {
  490. linenum++;
  491. lineval = get_line();
  492. trans_table[ifstate[depth]][lineval]();
  493. debug("process %s -> %s depth %d",
  494. linetype_name[lineval],
  495. ifstate_name[ifstate[depth]], depth);
  496. }
  497. }
  498. /*
  499. * Parse a line and determine its type. We keep the preprocessor line
  500. * parser state between calls in the global variable linestate, with
  501. * help from skipcomment().
  502. */
  503. static Linetype
  504. get_line(void)
  505. {
  506. const char *cp;
  507. int cursym;
  508. int kwlen;
  509. Linetype retval;
  510. Comment_state wascomment;
  511. if (fgets(tline, MAXLINE, input) == NULL)
  512. return (LT_EOF);
  513. retval = LT_PLAIN;
  514. wascomment = incomment;
  515. cp = skipcomment(tline);
  516. if (linestate == LS_START) {
  517. if (*cp == '#') {
  518. linestate = LS_HASH;
  519. cp = skipcomment(cp + 1);
  520. } else if (*cp != '\0')
  521. linestate = LS_DIRTY;
  522. }
  523. if (!incomment && linestate == LS_HASH) {
  524. keyword = tline + (cp - tline);
  525. cp = skipsym(cp);
  526. kwlen = cp - keyword;
  527. /* no way can we deal with a continuation inside a keyword */
  528. if (strncmp(cp, "\\\n", 2) == 0)
  529. Eioccc();
  530. if (strlcmp("ifdef", keyword, kwlen) == 0 ||
  531. strlcmp("ifndef", keyword, kwlen) == 0) {
  532. cp = skipcomment(cp);
  533. if ((cursym = findsym(cp)) < 0)
  534. retval = LT_IF;
  535. else {
  536. retval = (keyword[2] == 'n')
  537. ? LT_FALSE : LT_TRUE;
  538. if (value[cursym] == NULL)
  539. retval = (retval == LT_TRUE)
  540. ? LT_FALSE : LT_TRUE;
  541. if (ignore[cursym])
  542. retval = (retval == LT_TRUE)
  543. ? LT_TRUEI : LT_FALSEI;
  544. }
  545. cp = skipsym(cp);
  546. } else if (strlcmp("if", keyword, kwlen) == 0)
  547. retval = ifeval(&cp);
  548. else if (strlcmp("elif", keyword, kwlen) == 0)
  549. retval = ifeval(&cp) - LT_IF + LT_ELIF;
  550. else if (strlcmp("else", keyword, kwlen) == 0)
  551. retval = LT_ELSE;
  552. else if (strlcmp("endif", keyword, kwlen) == 0)
  553. retval = LT_ENDIF;
  554. else {
  555. linestate = LS_DIRTY;
  556. retval = LT_PLAIN;
  557. }
  558. cp = skipcomment(cp);
  559. if (*cp != '\0') {
  560. linestate = LS_DIRTY;
  561. if (retval == LT_TRUE || retval == LT_FALSE ||
  562. retval == LT_TRUEI || retval == LT_FALSEI)
  563. retval = LT_IF;
  564. if (retval == LT_ELTRUE || retval == LT_ELFALSE)
  565. retval = LT_ELIF;
  566. }
  567. if (retval != LT_PLAIN && (wascomment || incomment)) {
  568. retval += LT_DODGY;
  569. if (incomment)
  570. linestate = LS_DIRTY;
  571. }
  572. /* skipcomment should have changed the state */
  573. // Hmm hppens sometimes on valid files
  574. // if (linestate == LS_HASH) {
  575. // debug("bug at line %d", __LINE__);
  576. // abort(); /* bug */
  577. // }
  578. }
  579. if (linestate == LS_DIRTY) {
  580. while (*cp != '\0')
  581. cp = skipcomment(cp + 1);
  582. }
  583. debug("parser %s comment %s line",
  584. comment_name[incomment], linestate_name[linestate]);
  585. return (retval);
  586. }
  587. /*
  588. * These are the binary operators that are supported by the expression
  589. * evaluator. Note that if support for division is added then we also
  590. * need short-circuiting booleans because of divide-by-zero.
  591. */
  592. static int op_lt(int a, int b) { return (a < b); }
  593. static int op_gt(int a, int b) { return (a > b); }
  594. static int op_le(int a, int b) { return (a <= b); }
  595. static int op_ge(int a, int b) { return (a >= b); }
  596. static int op_eq(int a, int b) { return (a == b); }
  597. static int op_ne(int a, int b) { return (a != b); }
  598. static int op_or(int a, int b) { return (a || b); }
  599. static int op_and(int a, int b) { return (a && b); }
  600. /*
  601. * An evaluation function takes three arguments, as follows: (1) a pointer to
  602. * an element of the precedence table which lists the operators at the current
  603. * level of precedence; (2) a pointer to an integer which will receive the
  604. * value of the expression; and (3) a pointer to a char* that points to the
  605. * expression to be evaluated and that is updated to the end of the expression
  606. * when evaluation is complete. The function returns LT_FALSE if the value of
  607. * the expression is zero, LT_TRUE if it is non-zero, or LT_IF if the
  608. * expression could not be evaluated.
  609. */
  610. struct ops;
  611. typedef Linetype eval_fn(const struct ops *, int *, const char **);
  612. static eval_fn eval_table, eval_unary;
  613. /*
  614. * The precedence table. Expressions involving binary operators are evaluated
  615. * in a table-driven way by eval_table. When it evaluates a subexpression it
  616. * calls the inner function with its first argument pointing to the next
  617. * element of the table. Innermost expressions have special non-table-driven
  618. * handling.
  619. */
  620. static const struct ops {
  621. eval_fn *inner;
  622. struct op {
  623. const char *str;
  624. int short_circuit_val;
  625. int (*fn)(int, int);
  626. } op[5];
  627. } eval_ops[] = {
  628. { eval_table, { { "||", 1, op_or } } },
  629. { eval_table, { { "&&", 0, op_and } } },
  630. { eval_table, { { "==", -1, op_eq },
  631. { "!=", -1, op_ne } } },
  632. { eval_unary, { { "<=", -1, op_le },
  633. { ">=", -1, op_ge },
  634. { "<", -1, op_lt },
  635. { ">", -1, op_gt } } }
  636. };
  637. /*
  638. * Function for evaluating the innermost parts of expressions, viz.
  639. * "!expr", "(expr)", "defined(symbol)", "defined symbol", "symbol", "number".
  640. * We reset the keepthis flag when we find a non-constant subexpression.
  641. */
  642. // TODO: we use LT_IF both as "I don't know whether it's false or true"
  643. // (example: "#if defined FOO") and when we see syntax error
  644. // (example: "#if (1 || 2" - no closing paren!), but this is wrong.
  645. // Binary && and || need to distinguish these cases in order to handle this:
  646. // "#if defined KNOWN_UNDEFINED && FOO" - discard
  647. // "#if defined KNOWN_UNDEFINED && (syntax_error_here" - do not discard!
  648. static Linetype
  649. eval_unary(const struct ops *ops, int *valp, const char **cpp)
  650. {
  651. const char *cp;
  652. char *ep;
  653. int sym;
  654. cp = skipcomment(*cpp);
  655. if (*cp == '!') {
  656. debug("eval%d !", ops - eval_ops);
  657. cp++;
  658. if (eval_unary(ops, valp, &cp) == LT_IF) {
  659. *cpp = cp;
  660. return (LT_IF);
  661. }
  662. *valp = !*valp;
  663. } else if (*cp == '(') {
  664. Linetype expr_res;
  665. cp++;
  666. debug("eval%d (%s", ops - eval_ops, cp);
  667. expr_res = eval_table(eval_ops, valp, &cp);
  668. cp = skipcomment(cp);
  669. *cpp = cp;
  670. if (*cp++ != ')')
  671. return (LT_IF);
  672. *cpp = cp;
  673. if (expr_res == LT_IF)
  674. return (LT_IF);
  675. } else if (isdigit((unsigned char)*cp)) {
  676. debug("eval%d number", ops - eval_ops);
  677. *valp = strtol(cp, &ep, 0);
  678. cp = skipsym(cp);
  679. } else if (strncmp(cp, "defined", 7) == 0 && endsym(cp[7])) {
  680. bool parens;
  681. cp = skipcomment(cp+7);
  682. debug("eval%d defined '%s'", ops - eval_ops, cp);
  683. parens = (*cp == '(');
  684. if (parens)
  685. cp = skipcomment(cp+1);
  686. sym = findsym(cp);
  687. cp = skipsym(cp);
  688. cp = skipcomment(cp);
  689. if (parens) {
  690. if (*cp != ')')
  691. return (LT_IF);
  692. cp = skipcomment(cp+1);
  693. }
  694. *cpp = cp;
  695. if (sym < 0) {
  696. debug("sym not found, returning LT_IF");
  697. return (LT_IF);
  698. }
  699. *valp = (value[sym] != NULL);
  700. keepthis = false;
  701. } else if (!endsym(*cp)) {
  702. debug("eval%d symbol", ops - eval_ops);
  703. sym = findsym(cp);
  704. cp = skipsym(cp);
  705. *cpp = cp;
  706. if (sym < 0)
  707. return (LT_IF);
  708. if (value[sym] == NULL)
  709. *valp = 0;
  710. else {
  711. *valp = strtol(value[sym], &ep, 0);
  712. if (*ep != '\0' || ep == value[sym])
  713. return (LT_IF);
  714. }
  715. keepthis = false;
  716. } else {
  717. debug("eval%d bad expr", ops - eval_ops);
  718. return (LT_IF);
  719. }
  720. *cpp = cp;
  721. debug("eval%d = %d", ops - eval_ops, *valp);
  722. return (*valp ? LT_TRUE : LT_FALSE);
  723. }
  724. /*
  725. * Table-driven evaluation of binary operators.
  726. */
  727. static Linetype
  728. eval_table(const struct ops *ops, int *valp, const char **cpp)
  729. {
  730. Linetype left_side;
  731. const struct op *op;
  732. const char *cp;
  733. int val;
  734. debug("eval%d '%s'", ops - eval_ops, *cpp);
  735. left_side = ops->inner(ops+1, valp, cpp);
  736. cp = *cpp;
  737. for (;;) {
  738. Linetype right_side;
  739. cp = skipcomment(cp);
  740. for (op = ops->op; op->str != NULL; op++)
  741. if (strncmp(cp, op->str, strlen(op->str)) == 0)
  742. break;
  743. if (op->str == NULL)
  744. break;
  745. cp += strlen(op->str);
  746. debug("eval%d '%s'", ops - eval_ops, op->str);
  747. right_side = ops->inner(ops+1, &val, &cp);
  748. *cpp = cp;
  749. /* If short_circuit_val is 0 or 1, we can ignore
  750. * right side if left size is known, and its value
  751. * (i.e., *valp) is 0 or !0, respectively */
  752. if (left_side != LT_IF && op->short_circuit_val == !!*valp) {
  753. debug("op->short_circuit_val:%d *valp:%d cp:'%s'",
  754. op->short_circuit_val, *valp, cp);
  755. *valp = !!*valp;
  756. break;
  757. }
  758. /* Same for the right side */
  759. if (right_side != LT_IF && op->short_circuit_val == !!val) {
  760. debug("op->short_circuit_val:%d val:%d cp:'%s'",
  761. op->short_circuit_val, val, cp);
  762. left_side = right_side;
  763. *valp = !!val;
  764. break;
  765. }
  766. if (left_side == LT_IF || right_side == LT_IF)
  767. return (LT_IF);
  768. *valp = op->fn(*valp, val);
  769. left_side = right_side;
  770. }
  771. debug("eval%d = %d LT_IF:%d", ops - eval_ops, *valp, (left_side == LT_IF));
  772. if (left_side == LT_IF)
  773. return (LT_IF);
  774. return (*valp ? LT_TRUE : LT_FALSE);
  775. }
  776. /*
  777. * Evaluate the expression on a #if or #elif line. If we can work out
  778. * the result we return LT_TRUE or LT_FALSE accordingly, otherwise we
  779. * return just a generic LT_IF.
  780. */
  781. static Linetype
  782. ifeval(const char **cpp)
  783. {
  784. int ret;
  785. int val;
  786. debug("eval %s", *cpp);
  787. keepthis = killconsts ? false : true;
  788. ret = eval_table(eval_ops, &val, cpp);
  789. debug("val:%d ret:%d keepthis:%d", val, ret, keepthis);
  790. return (keepthis ? LT_IF : ret);
  791. }
  792. /*
  793. * Skip over comments, strings, and character literals and stop at the
  794. * next character position that is not whitespace. Between calls we keep
  795. * the comment state in the global variable incomment, and we also adjust
  796. * the global variable linestate when we see a newline.
  797. * XXX: doesn't cope with the buffer splitting inside a state transition.
  798. */
  799. static const char *
  800. skipcomment(const char *cp)
  801. {
  802. if (text || ignoring[depth]) {
  803. for (; isspace((unsigned char)*cp); cp++)
  804. if (*cp == '\n')
  805. linestate = LS_START;
  806. return (cp);
  807. }
  808. while (*cp != '\0')
  809. /* don't reset to LS_START after a line continuation */
  810. if (strncmp(cp, "\\\n", 2) == 0)
  811. cp += 2;
  812. else switch (incomment) {
  813. case NO_COMMENT:
  814. if (strncmp(cp, "/\\\n", 3) == 0) {
  815. incomment = STARTING_COMMENT;
  816. cp += 3;
  817. } else if (strncmp(cp, "/*", 2) == 0) {
  818. incomment = C_COMMENT;
  819. cp += 2;
  820. } else if (strncmp(cp, "//", 2) == 0) {
  821. incomment = CXX_COMMENT;
  822. cp += 2;
  823. } else if (strncmp(cp, "\'", 1) == 0) {
  824. incomment = CHAR_LITERAL;
  825. linestate = LS_DIRTY;
  826. cp += 1;
  827. } else if (strncmp(cp, "\"", 1) == 0) {
  828. incomment = STRING_LITERAL;
  829. linestate = LS_DIRTY;
  830. cp += 1;
  831. } else if (strncmp(cp, "\n", 1) == 0) {
  832. linestate = LS_START;
  833. cp += 1;
  834. } else if (strchr(" \t", *cp) != NULL) {
  835. cp += 1;
  836. } else
  837. return (cp);
  838. continue;
  839. case CXX_COMMENT:
  840. if (strncmp(cp, "\n", 1) == 0) {
  841. incomment = NO_COMMENT;
  842. linestate = LS_START;
  843. }
  844. cp += 1;
  845. continue;
  846. case CHAR_LITERAL:
  847. case STRING_LITERAL:
  848. if ((incomment == CHAR_LITERAL && cp[0] == '\'') ||
  849. (incomment == STRING_LITERAL && cp[0] == '\"')) {
  850. incomment = NO_COMMENT;
  851. cp += 1;
  852. } else if (cp[0] == '\\') {
  853. if (cp[1] == '\0')
  854. cp += 1;
  855. else
  856. cp += 2;
  857. } else if (strncmp(cp, "\n", 1) == 0) {
  858. if (incomment == CHAR_LITERAL)
  859. error("unterminated char literal");
  860. else
  861. error("unterminated string literal");
  862. } else
  863. cp += 1;
  864. continue;
  865. case C_COMMENT:
  866. if (strncmp(cp, "*\\\n", 3) == 0) {
  867. incomment = FINISHING_COMMENT;
  868. cp += 3;
  869. } else if (strncmp(cp, "*/", 2) == 0) {
  870. incomment = NO_COMMENT;
  871. cp += 2;
  872. } else
  873. cp += 1;
  874. continue;
  875. case STARTING_COMMENT:
  876. if (*cp == '*') {
  877. incomment = C_COMMENT;
  878. cp += 1;
  879. } else if (*cp == '/') {
  880. incomment = CXX_COMMENT;
  881. cp += 1;
  882. } else {
  883. incomment = NO_COMMENT;
  884. linestate = LS_DIRTY;
  885. }
  886. continue;
  887. case FINISHING_COMMENT:
  888. if (*cp == '/') {
  889. incomment = NO_COMMENT;
  890. cp += 1;
  891. } else
  892. incomment = C_COMMENT;
  893. continue;
  894. default:
  895. debug("bug at line %d", __LINE__);
  896. abort(); /* bug */
  897. }
  898. return (cp);
  899. }
  900. /*
  901. * Skip over an identifier.
  902. */
  903. static const char *
  904. skipsym(const char *cp)
  905. {
  906. while (!endsym(*cp))
  907. ++cp;
  908. return (cp);
  909. }
  910. /*
  911. * Look for the symbol in the symbol table. If is is found, we return
  912. * the symbol table index, else we return -1.
  913. */
  914. static int
  915. findsym(const char *str)
  916. {
  917. const char *cp;
  918. int symind;
  919. cp = skipsym(str);
  920. if (cp == str)
  921. return (-1);
  922. if (symlist) {
  923. printf("%.*s\n", (int)(cp-str), str);
  924. /* we don't care about the value of the symbol */
  925. return (0);
  926. }
  927. for (symind = 0; symind < nsyms; ++symind) {
  928. if (strlcmp(symname[symind], str, cp-str) == 0) {
  929. debug("findsym %s %s", symname[symind],
  930. value[symind] ? value[symind] : "");
  931. return (symind);
  932. }
  933. }
  934. return (-1);
  935. }
  936. /*
  937. * Add a symbol to the symbol table.
  938. */
  939. static void
  940. addsym(bool ignorethis, bool definethis, char *sym)
  941. {
  942. int symind;
  943. char *val;
  944. symind = findsym(sym);
  945. if (symind < 0) {
  946. if (nsyms >= MAXSYMS)
  947. errx(2, "too many symbols");
  948. symind = nsyms++;
  949. }
  950. symname[symind] = sym;
  951. ignore[symind] = ignorethis;
  952. val = sym + (skipsym(sym) - sym);
  953. if (definethis) {
  954. if (*val == '=') {
  955. value[symind] = val+1;
  956. *val = '\0';
  957. } else if (*val == '\0')
  958. value[symind] = "";
  959. else
  960. usage();
  961. } else {
  962. if (*val != '\0')
  963. usage();
  964. value[symind] = NULL;
  965. }
  966. }
  967. /*
  968. * Compare s with n characters of t.
  969. * The same as strncmp() except that it checks that s[n] == '\0'.
  970. */
  971. static int
  972. strlcmp(const char *s, const char *t, size_t n)
  973. {
  974. while (n-- && *t != '\0')
  975. if (*s != *t)
  976. return ((unsigned char)*s - (unsigned char)*t);
  977. else
  978. ++s, ++t;
  979. return ((unsigned char)*s);
  980. }
  981. /*
  982. * Diagnostics.
  983. */
  984. static void
  985. debug(const char *msg, ...)
  986. {
  987. va_list ap;
  988. if (debugging) {
  989. va_start(ap, msg);
  990. vwarnx(msg, ap);
  991. va_end(ap);
  992. }
  993. }
  994. static void
  995. error(const char *msg)
  996. {
  997. if (depth == 0)
  998. warnx("%s: %d: %s", filename, linenum, msg);
  999. else
  1000. warnx("%s: %d: %s (#if line %d depth %d)",
  1001. filename, linenum, msg, stifline[depth], depth);
  1002. errx(2, "output may be truncated");
  1003. }