PageRenderTime 91ms CodeModel.GetById 11ms app.highlight 70ms RepoModel.GetById 1ms app.codeStats 1ms

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