summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBernhard Reutner-Fischer <rep.dot.nop@gmail.com>2013-11-19 12:43:49 +0100
committerBernhard Reutner-Fischer <rep.dot.nop@gmail.com>2013-11-19 12:43:49 +0100
commit2a021ae81c36f4281883a3195f7ce81504edf978 (patch)
treeb851240ca72301ebb42a85fed6e2bfd640f1e21a
parent32ef09dd30bb2b8e1878afff755fc5b7000f24c7 (diff)
buildsys: update unifdef
sync up to 55501a61dd257e941d53f175350ee52fc6ff2866 (2.9 + refactor keyword and symbol matching) Signed-off-by: Bernhard Reutner-Fischer <rep.dot.nop@gmail.com>
-rw-r--r--Makefile.in8
-rwxr-xr-xextra/scripts/install_headers.sh4
-rw-r--r--extra/scripts/unifdef.c1235
-rw-r--r--extra/scripts/unifdef.h56
-rw-r--r--extra/scripts/version.h2
5 files changed, 934 insertions, 371 deletions
diff --git a/Makefile.in b/Makefile.in
index 500b8f37d..946e14e9f 100644
--- a/Makefile.in
+++ b/Makefile.in
@@ -67,6 +67,10 @@ MAKEFLAGS += -L
$(top_builddir)include/config/linuxthreads/old.h $(top_builddir)include/config/linuxthreads/new.h:
@true
+$(top_builddir)include/generated/unifdef_config.h: $(top_builddir)include/bits/uClibc_config.h| $(top_builddir)include/generated
+ @$(disp_gen)
+ $(Q)$(SED) -e '1,3d' $^ > $@
+
# For the moment, we have to keep re-running this target
# because the fix includes scripts rely on pre-processers
# in order to generate the headers correctly :(. That
@@ -331,7 +335,9 @@ $(PREFIX)$(RUNTIME_PREFIX)$(MULTILIB_DIR):
$(do_mkdir)
endif
endif
-install_headers: headers $(top_builddir)extra/scripts/unifdef | $(PREFIX)$(DEVEL_PREFIX)include
+
+
+install_headers: headers $(top_builddir)extra/scripts/unifdef $(top_builddir)include/generated/unifdef_config.h | $(PREFIX)$(DEVEL_PREFIX)include
@$(call disp_install,"include -> $(PREFIX)$(DEVEL_PREFIX)include")
$(Q)top_builddir=$(top_builddir) \
$(top_srcdir)extra/scripts/install_headers.sh \
diff --git a/extra/scripts/install_headers.sh b/extra/scripts/install_headers.sh
index 14d64dc9d..5a966066d 100755
--- a/extra/scripts/install_headers.sh
+++ b/extra/scripts/install_headers.sh
@@ -32,7 +32,6 @@ if ! test -x "$top_builddir/extra/scripts/unifdef"; then
exit 1
fi
-
# Sanitize and copy uclibc headers
(
# We must cd, or else we'll prepend "${srcdir}" to filenames!
@@ -56,6 +55,9 @@ while read -r filename; do
# Do not abort the script if unifdef "fails"!
# NB2: careful with sed command arguments, they contain tab character
"$top_builddir/extra/scripts/unifdef" \
+ -B \
+ -t \
+ -f "$top_builddir/include/generated/unifdef_config.h" \
-U_LIBC \
-U__UCLIBC_GEN_LOCALE \
-U__NO_CTYPE \
diff --git a/extra/scripts/unifdef.c b/extra/scripts/unifdef.c
index abc39966c..b159df0a6 100644
--- a/extra/scripts/unifdef.c
+++ b/extra/scripts/unifdef.c
@@ -1,13 +1,5 @@
/*
- * Copyright (c) 2002 - 2005 Tony Finch <dot@dotat.at>. All rights reserved.
- *
- * This code is derived from software contributed to Berkeley by Dave Yost.
- * It was rewritten to support ANSI C by Tony Finch. The original version of
- * unifdef carried the following copyright notice. None of its code remains
- * in this version (though some of the names remain).
- *
- * Copyright (c) 1985, 1993
- * The Regents of the University of California. All rights reserved.
+ * Copyright (c) 2002 - 2013 Tony Finch <dot@dotat.at>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -31,27 +23,15 @@
* SUCH DAMAGE.
*/
-#include <sys/cdefs.h>
-
-#ifndef lint
-#if 0
-static const char copyright[] =
-"@(#) Copyright (c) 1985, 1993\n\
- The Regents of the University of California. All rights reserved.\n";
-#endif
-#ifdef __IDSTRING
-__IDSTRING(Berkeley, "@(#)unifdef.c 8.1 (Berkeley) 6/6/93");
-__IDSTRING(NetBSD, "$NetBSD: unifdef.c,v 1.8 2000/07/03 02:51:36 matt Exp $");
-__IDSTRING(dotat, "$dotat: things/unifdef.c,v 1.171 2005/03/08 12:38:48 fanf2 Exp $");
-#endif
-#endif /* not lint */
-#ifdef __FBSDID
-__FBSDID("$FreeBSD: /repoman/r/ncvs/src/usr.bin/unifdef/unifdef.c,v 1.20 2005/05/21 09:55:09 ru Exp $");
-#endif
-
/*
* unifdef - remove ifdef'ed lines
*
+ * This code was derived from software contributed to Berkeley by Dave Yost.
+ * It was rewritten to support ANSI C by Tony Finch. The original version
+ * of unifdef carried the 4-clause BSD copyright licence. None of its code
+ * remains in this version (though some of the names remain) so it now
+ * carries a more liberal licence.
+ *
* Wishlist:
* provide an option which will append the name of the
* appropriate symbol after #else's and #endif's
@@ -59,26 +39,17 @@ __FBSDID("$FreeBSD: /repoman/r/ncvs/src/usr.bin/unifdef/unifdef.c,v 1.20 2005/05
* #else's and #endif's to see that they match their
* corresponding #ifdef or #ifndef
*
- * The first two items above require better buffer handling, which would
- * also make it possible to handle all "dodgy" directives correctly.
+ * These require better buffer handling, which would also make
+ * it possible to handle all "dodgy" directives correctly.
*/
-#include <errno.h>
-#include <ctype.h>
-#include <stdarg.h>
-#include <stdbool.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-
-/* Avoid err.h since uClibc can disable these things */
-#define vwarnx(fmt, args) ({ fprintf(stderr, "unifdef: "); vfprintf(stderr, fmt, args); fprintf(stderr, "\n"); })
-#define warnx(fmt, args...) fprintf(stderr, "unifdef: " fmt "\n", ## args)
-#define errx(exit_code, fmt, args...) ({ warnx(fmt, ## args); exit(exit_code); })
-#define err(exit_code, fmt, args...) errx(exit_code, fmt ": %s", ## args, strerror(errno))
+#include "unifdef.h"
-size_t strlcpy(char *dst, const char *src, size_t siz);
+static const char copyright[] =
+ #include "version.h"
+ "@(#) $Author: Tony Finch (dot@dotat.at) $\n"
+ "@(#) $URL: http://dotat.at/prog/unifdef $\n"
+;
/* types of input lines: */
typedef enum {
@@ -96,6 +67,7 @@ typedef enum {
LT_DODGY_LAST = LT_DODGY + LT_ENDIF,
LT_PLAIN, /* ordinary line */
LT_EOF, /* end of file */
+ LT_ERROR, /* unevaluable #if */
LT_COUNT
} Linetype;
@@ -106,9 +78,12 @@ static char const * const linetype_name[] = {
"DODGY IF", "DODGY TRUE", "DODGY FALSE",
"DODGY ELIF", "DODGY ELTRUE", "DODGY ELFALSE",
"DODGY ELSE", "DODGY ENDIF",
- "PLAIN", "EOF"
+ "PLAIN", "EOF", "ERROR"
};
+#define linetype_if2elif(lt) ((Linetype)(lt - LT_IF + LT_ELIF))
+#define linetype_2dodgy(lt) ((Linetype)(lt + LT_DODGY))
+
/* state of #if processing */
typedef enum {
IS_OUTSIDE,
@@ -162,7 +137,7 @@ static char const * const linestate_name[] = {
*/
#define MAXDEPTH 64 /* maximum #if nesting */
#define MAXLINE 4096 /* maximum length of line */
-#define MAXSYMS 4096 /* maximum number of symbols */
+#define MAXSYMS 16384 /* maximum number of symbols */
/*
* Sometimes when editing a keyword the replacement text is longer, so
@@ -174,13 +149,17 @@ static char const * const linestate_name[] = {
* Globals.
*/
+static bool compblank; /* -B: compress blank lines */
+static bool lnblank; /* -b: blank deleted lines */
static bool complement; /* -c: do the complement */
static bool debugging; /* -d: debugging reports */
+static bool inplace; /* -m: modify in place */
static bool iocccok; /* -e: fewer IOCCC errors */
+static bool strictlogic; /* -K: keep ambiguous #ifs */
static bool killconsts; /* -k: eval constant #ifs */
-static bool lnblank; /* -l: blank deleted lines */
static bool lnnum; /* -n: add #line directives */
static bool symlist; /* -s: output symbol list */
+static bool symdepth; /* -S: output symbol depth */
static bool text; /* -t: this is a text file */
static const char *symname[MAXSYMS]; /* symbol name */
@@ -191,10 +170,28 @@ static int nsyms; /* number of symbols */
static FILE *input; /* input file pointer */
static const char *filename; /* input file name */
static int linenum; /* current line number */
+static const char *linefile; /* file name for #line */
+static FILE *output; /* output file pointer */
+static const char *ofilename; /* output file name */
+static const char *backext; /* backup extension */
+static char *tempname; /* avoid splatting input */
static char tline[MAXLINE+EDITSLOP];/* input buffer plus space */
static char *keyword; /* used for editing #elif's */
+/*
+ * When processing a file, the output's newline style will match the
+ * input's, and unifdef correctly handles CRLF or LF endings whatever
+ * the platform's native style. The stdio streams are opened in binary
+ * mode to accommodate platforms whose native newline style is CRLF.
+ * When the output isn't a processed input file (when it is error /
+ * debug / diagnostic messages) then unifdef uses native line endings.
+ */
+
+static const char *newline; /* input file format */
+static const char newline_unix[] = "\n";
+static const char newline_crlf[] = "\r\n";
+
static Comment_state incomment; /* comment parser state */
static Line_state linestate; /* #if line parser state */
static Ifstate ifstate[MAXDEPTH]; /* #if processor state */
@@ -202,31 +199,52 @@ static bool ignoring[MAXDEPTH]; /* ignore comments state */
static int stifline[MAXDEPTH]; /* start of current #if */
static int depth; /* current #if nesting */
static int delcount; /* count of deleted lines */
-static bool keepthis; /* don't delete constant #if */
+static unsigned blankcount; /* count of blank lines */
+static unsigned blankmax; /* maximum recent blankcount */
+static bool constexpr; /* constant #if expression */
+static bool zerosyms; /* to format symdepth output */
+static bool firstsym; /* ditto */
+static int exitmode; /* exit status mode */
static int exitstat; /* program exit status */
-static void addsym(bool, bool, char *);
+static void addsym1(bool, bool, char *);
+static void addsym2(bool, const char *, const char *);
+static char *astrcat(const char *, const char *);
+static void cleantemp(void);
+static void closeio(void);
static void debug(const char *, ...);
+static void debugsym(const char *, int);
+static bool defundef(void);
+static void defundefile(const char *);
static void done(void);
static void error(const char *);
-static int findsym(const char *);
+static int findsym(const char **);
static void flushline(bool);
-static Linetype get_line(void);
+static void hashline(void);
+static void help(void);
static Linetype ifeval(const char **);
static void ignoreoff(void);
static void ignoreon(void);
+static void indirectsym(void);
static void keywordedit(const char *);
+static const char *matchsym(const char *, const char *);
static void nest(void);
+static Linetype parseline(void);
static void process(void);
+static void processinout(const char *, const char *);
+static const char *skipargs(const char *);
static const char *skipcomment(const char *);
+static const char *skiphash(void);
+static const char *skipline(const char *);
static const char *skipsym(const char *);
static void state(Ifstate);
-static int strlcmp(const char *, const char *, size_t);
static void unnest(void);
static void usage(void);
+static void version(void);
+static const char *xstrdup(const char *, const char *);
-#define endsym(c) (!isalpha((unsigned char)c) && !isdigit((unsigned char)c) && c != '_')
+#define endsym(c) (!isalnum((unsigned char)c) && c != '_')
/*
* The main program.
@@ -236,7 +254,7 @@ main(int argc, char *argv[])
{
int opt;
- while ((opt = getopt(argc, argv, "i:D:U:I:cdeklnst")) != -1)
+ while ((opt = getopt(argc, argv, "i:D:U:f:I:M:o:x:bBcdehKklmnsStV")) != -1)
switch (opt) {
case 'i': /* treat stuff controlled by these symbols as text */
/*
@@ -246,20 +264,26 @@ main(int argc, char *argv[])
*/
opt = *optarg++;
if (opt == 'D')
- addsym(true, true, optarg);
+ addsym1(true, true, optarg);
else if (opt == 'U')
- addsym(true, false, optarg);
+ addsym1(true, false, optarg);
else
usage();
break;
case 'D': /* define a symbol */
- addsym(false, true, optarg);
+ addsym1(false, true, optarg);
break;
case 'U': /* undef a symbol */
- addsym(false, false, optarg);
+ addsym1(false, false, optarg);
break;
- case 'I':
- /* no-op for compatibility with cpp */
+ case 'I': /* no-op for compatibility with cpp */
+ break;
+ case 'b': /* blank deleted lines instead of omitting them */
+ case 'l': /* backwards compatibility */
+ lnblank = true;
+ break;
+ case 'B': /* compress blank lines around removed section */
+ compblank = true;
break;
case 'c': /* treat -D as -U and vice versa */
complement = true;
@@ -270,50 +294,210 @@ main(int argc, char *argv[])
case 'e': /* fewer errors from dodgy lines */
iocccok = true;
break;
+ case 'f': /* definitions file */
+ defundefile(optarg);
+ break;
+ case 'h':
+ help();
+ break;
+ case 'K': /* keep ambiguous #ifs */
+ strictlogic = true;
+ break;
case 'k': /* process constant #ifs */
killconsts = true;
break;
- case 'l': /* blank deleted lines instead of omitting them */
- lnblank = true;
+ case 'm': /* modify in place */
+ inplace = true;
+ break;
+ case 'M': /* modify in place and keep backup */
+ inplace = true;
+ backext = optarg;
break;
case 'n': /* add #line directive after deleted lines */
lnnum = true;
break;
+ case 'o': /* output to a file */
+ ofilename = optarg;
+ break;
case 's': /* only output list of symbols that control #ifs */
symlist = true;
break;
+ case 'S': /* list symbols with their nesting depth */
+ symlist = symdepth = true;
+ break;
case 't': /* don't parse C comments */
text = true;
break;
+ case 'V':
+ version();
+ break;
+ case 'x':
+ exitmode = atoi(optarg);
+ if(exitmode < 0 || exitmode > 2)
+ usage();
+ break;
default:
usage();
}
argc -= optind;
argv += optind;
- if (argc > 1) {
- errx(2, "can only do one file");
- } else if (argc == 1 && strcmp(*argv, "-") != 0) {
- filename = *argv;
- input = fopen(filename, "r");
- if (input == NULL)
- err(2, "can't open %s", filename);
- } else {
+ if (compblank && lnblank)
+ errx(2, "-B and -b are mutually exclusive");
+ if (symlist && (ofilename != NULL || inplace || argc > 1))
+ errx(2, "-s only works with one input file");
+ if (argc > 1 && ofilename != NULL)
+ errx(2, "-o cannot be used with multiple input files");
+ if (argc > 1 && !inplace)
+ errx(2, "multiple input files require -m or -M");
+ if (argc == 0)
+ argc = 1;
+ if (argc == 1 && !inplace && ofilename == NULL)
+ ofilename = "-";
+ indirectsym();
+
+ atexit(cleantemp);
+ if (ofilename != NULL)
+ processinout(*argv, ofilename);
+ else while (argc-- > 0) {
+ processinout(*argv, *argv);
+ argv++;
+ }
+ switch(exitmode) {
+ case(0): exit(exitstat);
+ case(1): exit(!exitstat);
+ case(2): exit(0);
+ default: abort(); /* bug */
+ }
+}
+
+/*
+ * File logistics.
+ */
+static void
+processinout(const char *ifn, const char *ofn)
+{
+ struct stat st;
+
+ if (ifn == NULL || strcmp(ifn, "-") == 0) {
filename = "[stdin]";
- input = stdin;
+ linefile = NULL;
+ input = fbinmode(stdin);
+ } else {
+ filename = ifn;
+ linefile = ifn;
+ input = fopen(ifn, "rb");
+ if (input == NULL)
+ err(2, "can't open %s", ifn);
+ }
+ if (strcmp(ofn, "-") == 0) {
+ output = fbinmode(stdout);
+ process();
+ return;
+ }
+ if (stat(ofn, &st) < 0) {
+ output = fopen(ofn, "wb");
+ if (output == NULL)
+ err(2, "can't create %s", ofn);
+ process();
+ return;
}
+
+ tempname = astrcat(ofn, ".XXXXXX");
+ output = mktempmode(tempname, st.st_mode);
+ if (output == NULL)
+ err(2, "can't create %s", tempname);
+
process();
- debug("bug at line %d", __LINE__);
- abort(); /* bug */
+
+ if (backext != NULL) {
+ char *backname = astrcat(ofn, backext);
+ if (rename(ofn, backname) < 0)
+ err(2, "can't rename \"%s\" to \"%s\"", ofn, backname);
+ free(backname);
+ }
+ if (replace(tempname, ofn) < 0)
+ err(2, "can't rename \"%s\" to \"%s\"", tempname, ofn);
+ free(tempname);
+ tempname = NULL;
+}
+
+/*
+ * For cleaning up if there is an error.
+ */
+static void
+cleantemp(void)
+{
+ if (tempname != NULL)
+ remove(tempname);
+}
+
+/*
+ * Self-identification functions.
+ */
+
+static void
+version(void)
+{
+ const char *c = copyright;
+ for (;;) {
+ while (*++c != '$')
+ if (*c == '\0')
+ exit(0);
+ while (*++c != '$')
+ putc(*c, stderr);
+ putc('\n', stderr);
+ }
+}
+
+static void
+synopsis(FILE *fp)
+{
+ fprintf(fp,
+ "usage: unifdef [-bBcdehKkmnsStV] [-x{012}] [-Mext] [-opath] \\\n"
+ " [-[i]Dsym[=val]] [-[i]Usym] [-fpath] ... [file] ...\n");
}
static void
usage(void)
{
- fprintf(stderr, "usage: unifdef [-cdeklnst] [-Ipath]"
- " [-Dsym[=val]] [-Usym] [-iDsym[=val]] [-iUsym] ... [file]\n");
+ synopsis(stderr);
exit(2);
}
+static void
+help(void)
+{
+ synopsis(stdout);
+ printf(
+ " -Dsym=val define preprocessor symbol with given value\n"
+ " -Dsym define preprocessor symbol with value 1\n"
+ " -Usym preprocessor symbol is undefined\n"
+ " -iDsym=val \\ ignore C strings and comments\n"
+ " -iDsym ) in sections controlled by these\n"
+ " -iUsym / preprocessor symbols\n"
+ " -fpath file containing #define and #undef directives\n"
+ " -b blank lines instead of deleting them\n"
+ " -B compress blank lines around deleted section\n"
+ " -c complement (invert) keep vs. delete\n"
+ " -d debugging mode\n"
+ " -e ignore multiline preprocessor directives\n"
+ " -h print help\n"
+ " -Ipath extra include file path (ignored)\n"
+ " -K disable && and || short-circuiting\n"
+ " -k process constant #if expressions\n"
+ " -Mext modify in place and keep backups\n"
+ " -m modify input files in place\n"
+ " -n add #line directives to output\n"
+ " -opath output file name\n"
+ " -S list #if control symbols with nesting\n"
+ " -s list #if control symbols\n"
+ " -t ignore C strings and comments\n"
+ " -V print version\n"
+ " -x{012} exit status mode\n"
+ );
+ exit(0);
+}
+
/*
* A state transition function alters the global #if processing state
* in a particular way. The table below is indexed by the current
@@ -327,7 +511,8 @@ usage(void)
* When we have processed a group that starts off with a known-false
* #if/#elif sequence (which has therefore been deleted) followed by a
* #elif that we don't understand and therefore must keep, we edit the
- * latter into a #if to keep the nesting correct.
+ * latter into a #if to keep the nesting correct. We use memcpy() to
+ * overwrite the 4 byte token "elif" with "if " without a '\0' byte.
*
* When we find a true #elif in a group, the following block will
* always be kept and the rest of the sequence after the next #elif or
@@ -380,75 +565,66 @@ static void Oelif (void) { if (!iocccok) Eioccc(); Pelif(); }
static void Idrop (void) { Fdrop(); ignoreon(); }
static void Itrue (void) { Ftrue(); ignoreon(); }
static void Ifalse(void) { Ffalse(); ignoreon(); }
-/* edit this line */
-static void Mpass (void) { strncpy(keyword, "if ", 4); Pelif(); }
-static void Mtrue (void) { keywordedit("else\n"); state(IS_TRUE_MIDDLE); }
-static void Melif (void) { keywordedit("endif\n"); state(IS_FALSE_TRAILER); }
-static void Melse (void) { keywordedit("endif\n"); state(IS_FALSE_ELSE); }
+/* modify this line */
+static void Mpass (void) { memcpy(keyword, "if ", 4); Pelif(); }
+static void Mtrue (void) { keywordedit("else"); state(IS_TRUE_MIDDLE); }
+static void Melif (void) { keywordedit("endif"); state(IS_FALSE_TRAILER); }
+static void Melse (void) { keywordedit("endif"); state(IS_FALSE_ELSE); }
static state_fn * const trans_table[IS_COUNT][LT_COUNT] = {
/* IS_OUTSIDE */
{ Itrue, Ifalse,Fpass, Ftrue, Ffalse,Eelif, Eelif, Eelif, Eelse, Eendif,
Oiffy, Oiffy, Fpass, Oif, Oif, Eelif, Eelif, Eelif, Eelse, Eendif,
- print, done },
+ print, done, abort },
/* IS_FALSE_PREFIX */
{ Idrop, Idrop, Fdrop, Fdrop, Fdrop, Mpass, Strue, Sfalse,Selse, Dendif,
Idrop, Idrop, Fdrop, Fdrop, Fdrop, Mpass, Eioccc,Eioccc,Eioccc,Eioccc,
- drop, Eeof },
+ drop, Eeof, abort },
/* IS_TRUE_PREFIX */
{ Itrue, Ifalse,Fpass, Ftrue, Ffalse,Dfalse,Dfalse,Dfalse,Delse, Dendif,
Oiffy, Oiffy, Fpass, Oif, Oif, Eioccc,Eioccc,Eioccc,Eioccc,Eioccc,
- print, Eeof },
+ print, Eeof, abort },
/* IS_PASS_MIDDLE */
{ Itrue, Ifalse,Fpass, Ftrue, Ffalse,Pelif, Mtrue, Delif, Pelse, Pendif,
Oiffy, Oiffy, Fpass, Oif, Oif, Pelif, Oelif, Oelif, Pelse, Pendif,
- print, Eeof },
+ print, Eeof, abort },
/* IS_FALSE_MIDDLE */
{ Idrop, Idrop, Fdrop, Fdrop, Fdrop, Pelif, Mtrue, Delif, Pelse, Pendif,
Idrop, Idrop, Fdrop, Fdrop, Fdrop, Eioccc,Eioccc,Eioccc,Eioccc,Eioccc,
- drop, Eeof },
+ drop, Eeof, abort },
/* IS_TRUE_MIDDLE */
{ Itrue, Ifalse,Fpass, Ftrue, Ffalse,Melif, Melif, Melif, Melse, Pendif,
Oiffy, Oiffy, Fpass, Oif, Oif, Eioccc,Eioccc,Eioccc,Eioccc,Pendif,
- print, Eeof },
+ print, Eeof, abort },
/* IS_PASS_ELSE */
{ Itrue, Ifalse,Fpass, Ftrue, Ffalse,Eelif, Eelif, Eelif, Eelse, Pendif,
Oiffy, Oiffy, Fpass, Oif, Oif, Eelif, Eelif, Eelif, Eelse, Pendif,
- print, Eeof },
+ print, Eeof, abort },
/* IS_FALSE_ELSE */
{ Idrop, Idrop, Fdrop, Fdrop, Fdrop, Eelif, Eelif, Eelif, Eelse, Dendif,
Idrop, Idrop, Fdrop, Fdrop, Fdrop, Eelif, Eelif, Eelif, Eelse, Eioccc,
- drop, Eeof },
+ drop, Eeof, abort },
/* IS_TRUE_ELSE */
{ Itrue, Ifalse,Fpass, Ftrue, Ffalse,Eelif, Eelif, Eelif, Eelse, Dendif,
Oiffy, Oiffy, Fpass, Oif, Oif, Eelif, Eelif, Eelif, Eelse, Eioccc,
- print, Eeof },
+ print, Eeof, abort },
/* IS_FALSE_TRAILER */
{ Idrop, Idrop, Fdrop, Fdrop, Fdrop, Dfalse,Dfalse,Dfalse,Delse, Dendif,
Idrop, Idrop, Fdrop, Fdrop, Fdrop, Dfalse,Dfalse,Dfalse,Delse, Eioccc,
- drop, Eeof }
+ drop, Eeof, abort }
/*TRUEI FALSEI IF TRUE FALSE ELIF ELTRUE ELFALSE ELSE ENDIF
TRUEI FALSEI IF TRUE FALSE ELIF ELTRUE ELFALSE ELSE ENDIF (DODGY)
- PLAIN EOF */
+ PLAIN EOF ERROR */
};
/*
* State machine utility functions
*/
static void
-done(void)
-{
- if (incomment)
- error("EOF in comment");
- exit(exitstat);
-}
-static void
ignoreoff(void)
{
- if (depth == 0) {
- debug("bug at line %d", __LINE__);
+ if (depth == 0)
abort(); /* bug */
- }
ignoring[depth] = ignoring[depth-1];
}
static void
@@ -459,31 +635,25 @@ ignoreon(void)
static void
keywordedit(const char *replacement)
{
- size_t size = tline + sizeof(tline) - keyword;
- char *dst = keyword;
- const char *src = replacement;
- if (size != 0) {
- while ((--size != 0) && (*src != '\0'))
- *dst++ = *src++;
- *dst = '\0';
- }
+ snprintf(keyword, tline + sizeof(tline) - keyword,
+ "%s%s", replacement, newline);
print();
}
static void
nest(void)
{
- depth += 1;
- if (depth >= MAXDEPTH)
+ if (depth > MAXDEPTH-1)
+ abort(); /* bug */
+ if (depth == MAXDEPTH-1)
error("Too many levels of nesting");
+ depth += 1;
stifline[depth] = linenum;
}
static void
unnest(void)
{
- if (depth == 0) {
- debug("bug at line %d", __LINE__);
+ if (depth == 0)
abort(); /* bug */
- }
depth -= 1;
}
static void
@@ -493,7 +663,20 @@ state(Ifstate is)
}
/*
+ * The last state transition function. When this is called,
+ * lineval == LT_EOF, so the process() loop will terminate.
+ */
+static void
+done(void)
+{
+ if (incomment)
+ error("EOF in comment");
+ closeio();
+}
+
+/*
* Write a line to the output or not, according to command line options.
+ * If writing fails, closeio() will print the error and exit.
*/
static void
flushline(bool keep)
@@ -501,16 +684,58 @@ flushline(bool keep)
if (symlist)
return;
if (keep ^ complement) {
- if (lnnum && delcount > 0)
- printf("#line %d\n", linenum);
- fputs(tline, stdout);
- delcount = 0;
+ bool blankline = tline[strspn(tline, " \t\r\n")] == '\0';
+ if (blankline && compblank && blankcount != blankmax) {
+ delcount += 1;
+ blankcount += 1;
+ } else {
+ if (lnnum && delcount > 0)
+ hashline();
+ if (fputs(tline, output) == EOF)
+ closeio();
+ delcount = 0;
+ blankmax = blankcount = blankline ? blankcount + 1 : 0;
+ }
} else {
- if (lnblank)
- putc('\n', stdout);
+ if (lnblank && fputs(newline, output) == EOF)
+ closeio();
exitstat = 1;
delcount += 1;
+ blankcount = 0;
}
+ if (debugging && fflush(output) == EOF)
+ closeio();
+}
+
+/*
+ * Format of #line directives depends on whether we know the input filename.
+ */
+static void
+hashline(void)
+{
+ int e;
+
+ if (linefile == NULL)
+ e = fprintf(output, "#line %d%s", linenum, newline);
+ else
+ e = fprintf(output, "#line %d \"%s\"%s",
+ linenum, linefile, newline);
+ if (e < 0)
+ closeio();
+}
+
+/*
+ * Flush the output and handle errors.
+ */
+static void
+closeio(void)
+{
+ /* Tidy up after findsym(). */
+ if (symdepth && !zerosyms)
+ printf("\n");
+ if (output != NULL && (ferror(output) || fclose(output) == EOF))
+ err(2, "%s: can't write to output", filename);
+ fclose(input);
}
/*
@@ -519,14 +744,18 @@ flushline(bool keep)
static void
process(void)
{
- Linetype lineval;
-
- for (;;) {
- linenum++;
- lineval = get_line();
+ Linetype lineval = LT_PLAIN;
+ /* When compressing blank lines, act as if the file
+ is preceded by a large number of blank lines. */
+ blankmax = blankcount = 1000;
+ zerosyms = true;
+ newline = NULL;
+ linenum = 0;
+ while (lineval != LT_EOF) {
+ lineval = parseline();
trans_table[ifstate[depth]][lineval]();
- debug("process %s -> %s depth %d",
- linetype_name[lineval],
+ debug("process line %d %s -> %s depth %d",
+ linenum, linetype_name[lineval],
ifstate_name[ifstate[depth]], depth);
}
}
@@ -537,104 +766,131 @@ process(void)
* help from skipcomment().
*/
static Linetype
-get_line(void)
+parseline(void)
{
const char *cp;
int cursym;
- int kwlen;
Linetype retval;
Comment_state wascomment;
- if (fgets(tline, MAXLINE, input) == NULL)
- return (LT_EOF);
- retval = LT_PLAIN;
wascomment = incomment;
- cp = skipcomment(tline);
- if (linestate == LS_START) {
- if (*cp == '#') {
- linestate = LS_HASH;
- cp = skipcomment(cp + 1);
- } else if (*cp != '\0')
- linestate = LS_DIRTY;
+ cp = skiphash();
+ if (cp == NULL)
+ return (LT_EOF);
+ if (newline == NULL) {
+ if (strrchr(tline, '\n') == strrchr(tline, '\r') + 1)
+ newline = newline_crlf;
+ else
+ newline = newline_unix;
}
- if (!incomment && linestate == LS_HASH) {
- keyword = tline + (cp - tline);
- cp = skipsym(cp);
- kwlen = cp - keyword;
- /* no way can we deal with a continuation inside a keyword */
- if (strncmp(cp, "\\\n", 2) == 0)
- Eioccc();
- if (strlcmp("ifdef", keyword, kwlen) == 0 ||
- strlcmp("ifndef", keyword, kwlen) == 0) {
- cp = skipcomment(cp);
- if ((cursym = findsym(cp)) < 0)
- retval = LT_IF;
- else {
- retval = (keyword[2] == 'n')
- ? LT_FALSE : LT_TRUE;
- if (value[cursym] == NULL)
- retval = (retval == LT_TRUE)
- ? LT_FALSE : LT_TRUE;
- if (ignore[cursym])
- retval = (retval == LT_TRUE)
- ? LT_TRUEI : LT_FALSEI;
- }
- cp = skipsym(cp);
- } else if (strlcmp("if", keyword, kwlen) == 0)
- retval = ifeval(&cp);
- else if (strlcmp("elif", keyword, kwlen) == 0)
- retval = ifeval(&cp) - LT_IF + LT_ELIF;
- else if (strlcmp("else", keyword, kwlen) == 0)
- retval = LT_ELSE;
- else if (strlcmp("endif", keyword, kwlen) == 0)
- retval = LT_ENDIF;
+ if (*cp == '\0') {
+ retval = LT_PLAIN;
+ goto done;
+ }
+ keyword = tline + (cp - tline);
+ if ((cp = matchsym("ifdef", keyword)) != NULL ||
+ (cp = matchsym("ifndef", keyword)) != NULL) {
+ cp = skipcomment(cp);
+ if ((cursym = findsym(&cp)) < 0)
+ retval = LT_IF;
else {
- linestate = LS_DIRTY;
- retval = LT_PLAIN;
+ retval = (keyword[2] == 'n')
+ ? LT_FALSE : LT_TRUE;
+ if (value[cursym] == NULL)
+ retval = (retval == LT_TRUE)
+ ? LT_FALSE : LT_TRUE;
+ if (ignore[cursym])
+ retval = (retval == LT_TRUE)
+ ? LT_TRUEI : LT_FALSEI;
}
- cp = skipcomment(cp);
- if (*cp != '\0') {
+ } else if ((cp = matchsym("if", keyword)) != NULL)
+ retval = ifeval(&cp);
+ else if ((cp = matchsym("elif", keyword)) != NULL)
+ retval = linetype_if2elif(ifeval(&cp));
+ else if ((cp = matchsym("else", keyword)) != NULL)
+ retval = LT_ELSE;
+ else if ((cp = matchsym("endif", keyword)) != NULL)
+ retval = LT_ENDIF;
+ else {
+ cp = skipsym(keyword);
+ /* no way can we deal with a continuation inside a keyword */
+ if (strncmp(cp, "\\\r\n", 3) == 0 ||
+ strncmp(cp, "\\\n", 2) == 0)
+ Eioccc();
+ cp = skipline(cp);
+ retval = LT_PLAIN;
+ goto done;
+ }
+ cp = skipcomment(cp);
+ if (*cp != '\0') {
+ cp = skipline(cp);
+ if (retval == LT_TRUE || retval == LT_FALSE ||
+ retval == LT_TRUEI || retval == LT_FALSEI)
+ retval = LT_IF;
+ if (retval == LT_ELTRUE || retval == LT_ELFALSE)
+ retval = LT_ELIF;
+ }
+ /* the following can happen if the last line of the file lacks a
+ newline or if there is too much whitespace in a directive */
+ if (linestate == LS_HASH) {
+ long len = cp - tline;
+ if (fgets(tline + len, MAXLINE - len, input) == NULL) {
+ if (ferror(input))
+ err(2, "can't read %s", filename);
+ /* append the missing newline at eof */
+ strcpy(tline + len, newline);
+ cp += strlen(newline);
+ linestate = LS_START;
+ } else {
linestate = LS_DIRTY;
- if (retval == LT_TRUE || retval == LT_FALSE ||
- retval == LT_TRUEI || retval == LT_FALSEI)
- retval = LT_IF;
- if (retval == LT_ELTRUE || retval == LT_ELFALSE)
- retval = LT_ELIF;
- }
- if (retval != LT_PLAIN && (wascomment || incomment)) {
- retval += LT_DODGY;
- if (incomment)
- linestate = LS_DIRTY;
}
- /* skipcomment should have changed the state */
-// Hmm hppens sometimes on valid files
-// if (linestate == LS_HASH) {
-// debug("bug at line %d", __LINE__);
-// abort(); /* bug */
-// }
}
- if (linestate == LS_DIRTY) {
- while (*cp != '\0')
- cp = skipcomment(cp + 1);
+ if (retval != LT_PLAIN && (wascomment || linestate != LS_START)) {
+ retval = linetype_2dodgy(retval);
+ linestate = LS_DIRTY;
}
- debug("parser %s comment %s line",
+done:
+ debug("parser line %d state %s comment %s line", linenum,
comment_name[incomment], linestate_name[linestate]);
return (retval);
}
/*
* These are the binary operators that are supported by the expression
- * evaluator. Note that if support for division is added then we also
- * need short-circuiting booleans because of divide-by-zero.
+ * evaluator.
*/
-static int op_lt(int a, int b) { return (a < b); }
-static int op_gt(int a, int b) { return (a > b); }
-static int op_le(int a, int b) { return (a <= b); }
-static int op_ge(int a, int b) { return (a >= b); }
-static int op_eq(int a, int b) { return (a == b); }
-static int op_ne(int a, int b) { return (a != b); }
-static int op_or(int a, int b) { return (a || b); }
-static int op_and(int a, int b) { return (a && b); }
+static Linetype op_strict(long *p, long v, Linetype at, Linetype bt) {
+ if(at == LT_IF || bt == LT_IF) return (LT_IF);
+ return (*p = v, v ? LT_TRUE : LT_FALSE);
+}
+static Linetype op_lt(long *p, Linetype at, long a, Linetype bt, long b) {
+ return op_strict(p, a < b, at, bt);
+}
+static Linetype op_gt(long *p, Linetype at, long a, Linetype bt, long b) {
+ return op_strict(p, a > b, at, bt);
+}
+static Linetype op_le(long *p, Linetype at, long a, Linetype bt, long b) {
+ return op_strict(p, a <= b, at, bt);
+}
+static Linetype op_ge(long *p, Linetype at, long a, Linetype bt, long b) {
+ return op_strict(p, a >= b, at, bt);
+}
+static Linetype op_eq(long *p, Linetype at, long a, Linetype bt, long b) {
+ return op_strict(p, a == b, at, bt);
+}
+static Linetype op_ne(long *p, Linetype at, long a, Linetype bt, long b) {
+ return op_strict(p, a != b, at, bt);
+}
+static Linetype op_or(long *p, Linetype at, long a, Linetype bt, long b) {
+ if (!strictlogic && (at == LT_TRUE || bt == LT_TRUE))
+ return (*p = 1, LT_TRUE);
+ return op_strict(p, a || b, at, bt);
+}
+static Linetype op_and(long *p, Linetype at, long a, Linetype bt, long b) {
+ if (!strictlogic && (at == LT_FALSE || bt == LT_FALSE))
+ return (*p = 0, LT_FALSE);
+ return op_strict(p, a && b, at, bt);
+}
/*
* An evaluation function takes three arguments, as follows: (1) a pointer to
@@ -643,12 +899,12 @@ static int op_and(int a, int b) { return (a && b); }
* value of the expression; and (3) a pointer to a char* that points to the
* expression to be evaluated and that is updated to the end of the expression
* when evaluation is complete. The function returns LT_FALSE if the value of
- * the expression is zero, LT_TRUE if it is non-zero, or LT_IF if the
- * expression could not be evaluated.
+ * the expression is zero, LT_TRUE if it is non-zero, LT_IF if the expression
+ * depends on an unknown symbol, or LT_ERROR if there is a parse failure.
*/
struct ops;
-typedef Linetype eval_fn(const struct ops *, int *, const char **);
+typedef Linetype eval_fn(const struct ops *, long *, const char **);
static eval_fn eval_table, eval_unary;
@@ -659,139 +915,139 @@ static eval_fn eval_table, eval_unary;
* element of the table. Innermost expressions have special non-table-driven
* handling.
*/
-static const struct ops {
+struct op {
+ const char *str;
+ Linetype (*fn)(long *, Linetype, long, Linetype, long);
+};
+struct ops {
eval_fn *inner;
- struct op {
- const char *str;
- int short_circuit_val;
- int (*fn)(int, int);