2017-11-27 15:37:16 +00:00
|
|
|
/*-
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD
|
|
|
|
*
|
2015-12-03 14:21:55 +00:00
|
|
|
* Copyright (c) 2002 - 2015 Tony Finch <dot@dotat.at>
|
1994-05-27 12:33:43 +00:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
2005-03-08 12:52:00 +00:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
1994-05-27 12:33:43 +00:00
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
2005-03-08 12:52:00 +00:00
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
1994-05-27 12:33:43 +00:00
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2008-02-29 12:57:14 +00:00
|
|
|
/*
|
2010-01-19 18:13:54 +00:00
|
|
|
* unifdef - remove ifdef'ed lines
|
|
|
|
*
|
2009-11-27 17:53:49 +00:00
|
|
|
* This code was derived from software contributed to Berkeley by Dave Yost.
|
2008-02-29 12:57:14 +00:00
|
|
|
* 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.
|
2009-11-27 17:53:49 +00:00
|
|
|
*
|
1994-05-27 12:33:43 +00:00
|
|
|
* Wishlist:
|
|
|
|
* provide an option which will append the name of the
|
|
|
|
* appropriate symbol after #else's and #endif's
|
|
|
|
* provide an option which will check symbols after
|
|
|
|
* #else's and #endif's to see that they match their
|
|
|
|
* corresponding #ifdef or #ifndef
|
2003-01-20 12:41:41 +00:00
|
|
|
*
|
2010-02-19 16:54:51 +00:00
|
|
|
* These require better buffer handling, which would also make
|
|
|
|
* it possible to handle all "dodgy" directives correctly.
|
1994-05-27 12:33:43 +00:00
|
|
|
*/
|
|
|
|
|
2019-11-20 19:43:34 +00:00
|
|
|
#include <sys/param.h>
|
2019-11-19 03:15:06 +00:00
|
|
|
#include <sys/stat.h>
|
2019-11-20 19:43:34 +00:00
|
|
|
#include <sys/tree.h>
|
2019-11-19 03:15:06 +00:00
|
|
|
|
2019-11-20 19:43:34 +00:00
|
|
|
#include <assert.h>
|
2019-11-19 03:15:06 +00:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <err.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
1997-08-20 11:07:53 +00:00
|
|
|
|
2011-11-06 18:50:00 +00:00
|
|
|
static const char copyright[] =
|
2015-12-03 14:21:55 +00:00
|
|
|
"@(#) $Version: unifdef-2.11 $\n"
|
2010-03-12 17:55:29 +00:00
|
|
|
"@(#) $FreeBSD$\n"
|
|
|
|
"@(#) $Author: Tony Finch (dot@dotat.at) $\n"
|
2017-10-29 08:03:21 +00:00
|
|
|
"@(#) $URL: https://dotat.at/prog/unifdef $\n"
|
2010-03-12 17:55:29 +00:00
|
|
|
;
|
2010-01-19 18:13:54 +00:00
|
|
|
|
2002-05-15 16:30:28 +00:00
|
|
|
/* types of input lines: */
|
|
|
|
typedef enum {
|
2002-12-18 20:50:44 +00:00
|
|
|
LT_TRUEI, /* a true #if with ignore flag */
|
|
|
|
LT_FALSEI, /* a false #if with ignore flag */
|
|
|
|
LT_IF, /* an unknown #if */
|
2002-05-15 16:30:28 +00:00
|
|
|
LT_TRUE, /* a true #if */
|
|
|
|
LT_FALSE, /* a false #if */
|
2002-12-18 20:50:44 +00:00
|
|
|
LT_ELIF, /* an unknown #elif */
|
2002-05-15 16:30:28 +00:00
|
|
|
LT_ELTRUE, /* a true #elif */
|
|
|
|
LT_ELFALSE, /* a false #elif */
|
|
|
|
LT_ELSE, /* #else */
|
|
|
|
LT_ENDIF, /* #endif */
|
2003-01-20 12:41:41 +00:00
|
|
|
LT_DODGY, /* flag: directive is not on one line */
|
|
|
|
LT_DODGY_LAST = LT_DODGY + LT_ENDIF,
|
|
|
|
LT_PLAIN, /* ordinary line */
|
2002-12-18 20:50:44 +00:00
|
|
|
LT_EOF, /* end of file */
|
2009-11-25 20:23:18 +00:00
|
|
|
LT_ERROR, /* unevaluable #if */
|
2002-12-18 20:50:44 +00:00
|
|
|
LT_COUNT
|
2002-05-15 16:30:28 +00:00
|
|
|
} Linetype;
|
|
|
|
|
2002-12-18 20:50:44 +00:00
|
|
|
static char const * const linetype_name[] = {
|
2003-01-20 12:41:41 +00:00
|
|
|
"TRUEI", "FALSEI", "IF", "TRUE", "FALSE",
|
|
|
|
"ELIF", "ELTRUE", "ELFALSE", "ELSE", "ENDIF",
|
|
|
|
"DODGY TRUEI", "DODGY FALSEI",
|
|
|
|
"DODGY IF", "DODGY TRUE", "DODGY FALSE",
|
|
|
|
"DODGY ELIF", "DODGY ELTRUE", "DODGY ELFALSE",
|
|
|
|
"DODGY ELSE", "DODGY ENDIF",
|
2009-11-25 20:23:18 +00:00
|
|
|
"PLAIN", "EOF", "ERROR"
|
2002-12-18 20:50:44 +00:00
|
|
|
};
|
2002-05-15 16:30:28 +00:00
|
|
|
|
2013-03-28 20:33:07 +00:00
|
|
|
#define linetype_if2elif(lt) ((Linetype)(lt - LT_IF + LT_ELIF))
|
|
|
|
#define linetype_2dodgy(lt) ((Linetype)(lt + LT_DODGY))
|
|
|
|
|
2002-12-18 20:50:44 +00:00
|
|
|
/* state of #if processing */
|
2002-05-15 16:30:28 +00:00
|
|
|
typedef enum {
|
2002-12-18 20:50:44 +00:00
|
|
|
IS_OUTSIDE,
|
|
|
|
IS_FALSE_PREFIX, /* false #if followed by false #elifs */
|
|
|
|
IS_TRUE_PREFIX, /* first non-false #(el)if is true */
|
|
|
|
IS_PASS_MIDDLE, /* first non-false #(el)if is unknown */
|
|
|
|
IS_FALSE_MIDDLE, /* a false #elif after a pass state */
|
|
|
|
IS_TRUE_MIDDLE, /* a true #elif after a pass state */
|
|
|
|
IS_PASS_ELSE, /* an else after a pass state */
|
|
|
|
IS_FALSE_ELSE, /* an else after a true state */
|
|
|
|
IS_TRUE_ELSE, /* an else after only false states */
|
|
|
|
IS_FALSE_TRAILER, /* #elifs after a true are false */
|
|
|
|
IS_COUNT
|
|
|
|
} Ifstate;
|
|
|
|
|
|
|
|
static char const * const ifstate_name[] = {
|
|
|
|
"OUTSIDE", "FALSE_PREFIX", "TRUE_PREFIX",
|
2003-01-20 12:41:41 +00:00
|
|
|
"PASS_MIDDLE", "FALSE_MIDDLE", "TRUE_MIDDLE",
|
|
|
|
"PASS_ELSE", "FALSE_ELSE", "TRUE_ELSE",
|
2002-12-18 20:50:44 +00:00
|
|
|
"FALSE_TRAILER"
|
|
|
|
};
|
2002-05-15 16:30:28 +00:00
|
|
|
|
2002-12-18 20:50:44 +00:00
|
|
|
/* state of comment parser */
|
2002-05-15 16:30:28 +00:00
|
|
|
typedef enum {
|
2002-12-18 20:50:44 +00:00
|
|
|
NO_COMMENT = false, /* outside a comment */
|
|
|
|
C_COMMENT, /* in a comment like this one */
|
|
|
|
CXX_COMMENT, /* between // and end of line */
|
|
|
|
STARTING_COMMENT, /* just after slash-backslash-newline */
|
2005-03-08 12:52:00 +00:00
|
|
|
FINISHING_COMMENT, /* star-backslash-newline in a C comment */
|
|
|
|
CHAR_LITERAL, /* inside '' */
|
|
|
|
STRING_LITERAL /* inside "" */
|
2002-12-18 20:50:44 +00:00
|
|
|
} Comment_state;
|
|
|
|
|
|
|
|
static char const * const comment_name[] = {
|
2005-03-08 12:52:00 +00:00
|
|
|
"NO", "C", "CXX", "STARTING", "FINISHING", "CHAR", "STRING"
|
2002-05-15 16:30:28 +00:00
|
|
|
};
|
|
|
|
|
2002-12-18 20:50:44 +00:00
|
|
|
/* state of preprocessor line parser */
|
|
|
|
typedef enum {
|
|
|
|
LS_START, /* only space and comments on this line */
|
|
|
|
LS_HASH, /* only space, comments, and a hash */
|
|
|
|
LS_DIRTY /* this line can't be a preprocessor line */
|
|
|
|
} Line_state;
|
2002-05-15 16:30:28 +00:00
|
|
|
|
2002-12-18 20:50:44 +00:00
|
|
|
static char const * const linestate_name[] = {
|
|
|
|
"START", "HASH", "DIRTY"
|
|
|
|
};
|
2002-05-15 16:30:28 +00:00
|
|
|
|
|
|
|
/*
|
2002-12-18 20:50:44 +00:00
|
|
|
* Minimum translation limits from ISO/IEC 9899:1999 5.2.4.1
|
2002-05-15 16:30:28 +00:00
|
|
|
*/
|
2002-12-18 20:50:44 +00:00
|
|
|
#define MAXDEPTH 64 /* maximum #if nesting */
|
|
|
|
#define MAXLINE 4096 /* maximum length of line */
|
2002-05-15 16:30:28 +00:00
|
|
|
|
2003-01-20 12:41:41 +00:00
|
|
|
/*
|
|
|
|
* Sometimes when editing a keyword the replacement text is longer, so
|
|
|
|
* we leave some space at the end of the tline buffer to accommodate this.
|
|
|
|
*/
|
|
|
|
#define EDITSLOP 10
|
|
|
|
|
2019-11-20 19:43:34 +00:00
|
|
|
/*
|
|
|
|
* C17/18 allow 63 characters per macro name, but up to 127 arbitrarily large
|
|
|
|
* parameters.
|
|
|
|
*/
|
|
|
|
struct macro {
|
|
|
|
RB_ENTRY(macro) entry;
|
|
|
|
const char *name;
|
|
|
|
const char *value;
|
|
|
|
bool ignore; /* -iDsym or -iUsym */
|
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
|
|
|
macro_cmp(struct macro *a, struct macro *b)
|
|
|
|
{
|
|
|
|
return (strcmp(a->name, b->name));
|
|
|
|
}
|
|
|
|
|
|
|
|
static RB_HEAD(MACROMAP, macro) macro_tree = RB_INITIALIZER(¯o_tree);
|
|
|
|
RB_GENERATE_STATIC(MACROMAP, macro, entry, macro_cmp);
|
|
|
|
|
2002-05-15 16:30:28 +00:00
|
|
|
/*
|
2002-12-18 20:50:44 +00:00
|
|
|
* Globals.
|
2002-05-15 16:30:28 +00:00
|
|
|
*/
|
|
|
|
|
2009-11-25 20:23:18 +00:00
|
|
|
static bool compblank; /* -B: compress blank lines */
|
|
|
|
static bool lnblank; /* -b: blank deleted lines */
|
2002-12-18 20:50:44 +00:00
|
|
|
static bool complement; /* -c: do the complement */
|
|
|
|
static bool debugging; /* -d: debugging reports */
|
2013-03-28 20:33:07 +00:00
|
|
|
static bool inplace; /* -m: modify in place */
|
2003-01-20 12:41:41 +00:00
|
|
|
static bool iocccok; /* -e: fewer IOCCC errors */
|
2009-11-25 20:23:18 +00:00
|
|
|
static bool strictlogic; /* -K: keep ambiguous #ifs */
|
2002-12-18 20:50:44 +00:00
|
|
|
static bool killconsts; /* -k: eval constant #ifs */
|
2005-03-08 12:52:00 +00:00
|
|
|
static bool lnnum; /* -n: add #line directives */
|
2002-12-18 20:50:44 +00:00
|
|
|
static bool symlist; /* -s: output symbol list */
|
2010-03-12 17:55:29 +00:00
|
|
|
static bool symdepth; /* -S: output symbol depth */
|
2002-12-18 20:50:44 +00:00
|
|
|
static bool text; /* -t: this is a text file */
|
|
|
|
|
|
|
|
static FILE *input; /* input file pointer */
|
|
|
|
static const char *filename; /* input file name */
|
|
|
|
static int linenum; /* current line number */
|
2013-03-28 20:33:07 +00:00
|
|
|
static const char *linefile; /* file name for #line */
|
2010-01-19 18:13:54 +00:00
|
|
|
static FILE *output; /* output file pointer */
|
|
|
|
static const char *ofilename; /* output file name */
|
2013-03-28 20:33:07 +00:00
|
|
|
static const char *backext; /* backup extension */
|
|
|
|
static char *tempname; /* avoid splatting input */
|
2002-12-18 20:50:44 +00:00
|
|
|
|
2003-01-20 12:41:41 +00:00
|
|
|
static char tline[MAXLINE+EDITSLOP];/* input buffer plus space */
|
2003-01-17 19:12:02 +00:00
|
|
|
static char *keyword; /* used for editing #elif's */
|
2002-12-18 20:50:44 +00:00
|
|
|
|
2015-02-25 21:10:03 +00:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2010-02-19 16:54:51 +00:00
|
|
|
static const char *newline; /* input file format */
|
|
|
|
static const char newline_unix[] = "\n";
|
|
|
|
static const char newline_crlf[] = "\r\n";
|
|
|
|
|
2002-12-18 20:50:44 +00:00
|
|
|
static Comment_state incomment; /* comment parser state */
|
|
|
|
static Line_state linestate; /* #if line parser state */
|
|
|
|
static Ifstate ifstate[MAXDEPTH]; /* #if processor state */
|
|
|
|
static bool ignoring[MAXDEPTH]; /* ignore comments state */
|
|
|
|
static int stifline[MAXDEPTH]; /* start of current #if */
|
|
|
|
static int depth; /* current #if nesting */
|
2005-03-08 12:52:00 +00:00
|
|
|
static int delcount; /* count of deleted lines */
|
2009-11-25 20:23:18 +00:00
|
|
|
static unsigned blankcount; /* count of blank lines */
|
|
|
|
static unsigned blankmax; /* maximum recent blankcount */
|
|
|
|
static bool constexpr; /* constant #if expression */
|
2013-03-28 20:33:07 +00:00
|
|
|
static bool zerosyms; /* to format symdepth output */
|
2010-03-12 17:55:29 +00:00
|
|
|
static bool firstsym; /* ditto */
|
2002-12-18 20:50:44 +00:00
|
|
|
|
2013-03-28 20:33:07 +00:00
|
|
|
static int exitmode; /* exit status mode */
|
2002-12-18 20:50:44 +00:00
|
|
|
static int exitstat; /* program exit status */
|
2015-12-03 14:21:55 +00:00
|
|
|
static bool altered; /* was this file modified? */
|
2002-12-18 20:50:44 +00:00
|
|
|
|
2015-02-25 21:10:03 +00:00
|
|
|
static void addsym1(bool, bool, char *);
|
|
|
|
static void addsym2(bool, const char *, const char *);
|
2013-03-28 20:33:07 +00:00
|
|
|
static char *astrcat(const char *, const char *);
|
|
|
|
static void cleantemp(void);
|
2015-02-25 21:10:03 +00:00
|
|
|
static void closeio(void);
|
2002-12-18 20:50:44 +00:00
|
|
|
static void debug(const char *, ...);
|
2019-11-20 19:43:34 +00:00
|
|
|
static void debugsym(const char *, const struct macro *);
|
2015-02-25 21:10:03 +00:00
|
|
|
static bool defundef(void);
|
|
|
|
static void defundefile(const char *);
|
2003-07-01 15:30:43 +00:00
|
|
|
static void done(void);
|
2002-12-18 20:50:44 +00:00
|
|
|
static void error(const char *);
|
2019-11-20 19:43:34 +00:00
|
|
|
static struct macro *findsym(const char **);
|
2002-12-18 20:50:44 +00:00
|
|
|
static void flushline(bool);
|
2013-03-28 20:33:07 +00:00
|
|
|
static void hashline(void);
|
|
|
|
static void help(void);
|
2002-12-18 20:50:44 +00:00
|
|
|
static Linetype ifeval(const char **);
|
2003-01-20 12:41:41 +00:00
|
|
|
static void ignoreoff(void);
|
|
|
|
static void ignoreon(void);
|
2015-02-25 21:10:03 +00:00
|
|
|
static void indirectsym(void);
|
2003-01-20 12:41:41 +00:00
|
|
|
static void keywordedit(const char *);
|
2015-02-25 21:10:03 +00:00
|
|
|
static const char *matchsym(const char *, const char *);
|
2002-12-18 20:50:44 +00:00
|
|
|
static void nest(void);
|
2013-03-28 20:33:07 +00:00
|
|
|
static Linetype parseline(void);
|
2002-12-18 20:50:44 +00:00
|
|
|
static void process(void);
|
2013-03-28 20:33:07 +00:00
|
|
|
static void processinout(const char *, const char *);
|
2009-11-25 20:23:18 +00:00
|
|
|
static const char *skipargs(const char *);
|
2002-12-18 20:50:44 +00:00
|
|
|
static const char *skipcomment(const char *);
|
2015-02-25 21:10:03 +00:00
|
|
|
static const char *skiphash(void);
|
|
|
|
static const char *skipline(const char *);
|
2002-12-18 20:50:44 +00:00
|
|
|
static const char *skipsym(const char *);
|
|
|
|
static void state(Ifstate);
|
2005-03-08 12:52:00 +00:00
|
|
|
static void unnest(void);
|
2002-12-18 20:50:44 +00:00
|
|
|
static void usage(void);
|
2010-03-12 17:55:29 +00:00
|
|
|
static void version(void);
|
2015-02-25 21:10:03 +00:00
|
|
|
static const char *xstrdup(const char *, const char *);
|
2002-05-15 16:30:28 +00:00
|
|
|
|
2009-11-25 20:23:18 +00:00
|
|
|
#define endsym(c) (!isalnum((unsigned char)c) && c != '_')
|
1997-08-20 11:07:53 +00:00
|
|
|
|
2019-11-19 03:15:06 +00:00
|
|
|
static FILE *
|
|
|
|
mktempmode(char *tmp, int mode)
|
|
|
|
{
|
|
|
|
int rc, fd;
|
|
|
|
|
|
|
|
mode &= (S_IRWXU|S_IRWXG|S_IRWXO);
|
|
|
|
fd = mkstemp(tmp);
|
|
|
|
if (fd < 0)
|
|
|
|
err(2, "can't create %s", tmp);
|
|
|
|
rc = fchmod(fd, mode);
|
|
|
|
if (rc < 0)
|
|
|
|
err(2, "can't fchmod %s mode=0o%o", tmp, mode);
|
|
|
|
return (fdopen(fd, "wb"));
|
|
|
|
}
|
|
|
|
|
2002-12-18 20:50:44 +00:00
|
|
|
/*
|
|
|
|
* The main program.
|
|
|
|
*/
|
1997-08-26 11:08:24 +00:00
|
|
|
int
|
2002-05-15 16:30:28 +00:00
|
|
|
main(int argc, char *argv[])
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2002-05-23 16:50:41 +00:00
|
|
|
int opt;
|
|
|
|
|
2015-02-25 21:10:03 +00:00
|
|
|
while ((opt = getopt(argc, argv, "i:D:U:f:I:M:o:x:bBcdehKklmnsStV")) != -1)
|
2002-05-23 16:50:41 +00:00
|
|
|
switch (opt) {
|
|
|
|
case 'i': /* treat stuff controlled by these symbols as text */
|
|
|
|
/*
|
|
|
|
* For strict backwards-compatibility the U or D
|
|
|
|
* should be immediately after the -i but it doesn't
|
|
|
|
* matter much if we relax that requirement.
|
|
|
|
*/
|
|
|
|
opt = *optarg++;
|
|
|
|
if (opt == 'D')
|
2015-02-25 21:10:03 +00:00
|
|
|
addsym1(true, true, optarg);
|
2002-05-23 16:50:41 +00:00
|
|
|
else if (opt == 'U')
|
2015-02-25 21:10:03 +00:00
|
|
|
addsym1(true, false, optarg);
|
2002-05-23 16:50:41 +00:00
|
|
|
else
|
|
|
|
usage();
|
2002-05-15 16:30:28 +00:00
|
|
|
break;
|
2002-05-23 16:50:41 +00:00
|
|
|
case 'D': /* define a symbol */
|
2015-02-25 21:10:03 +00:00
|
|
|
addsym1(false, true, optarg);
|
2002-05-15 16:30:28 +00:00
|
|
|
break;
|
2002-05-23 16:50:41 +00:00
|
|
|
case 'U': /* undef a symbol */
|
2015-02-25 21:10:03 +00:00
|
|
|
addsym1(false, false, optarg);
|
2002-05-23 16:50:41 +00:00
|
|
|
break;
|
2010-03-12 17:55:29 +00:00
|
|
|
case 'I': /* no-op for compatibility with cpp */
|
2009-11-25 20:23:18 +00:00
|
|
|
break;
|
|
|
|
case 'b': /* blank deleted lines instead of omitting them */
|
|
|
|
case 'l': /* backwards compatibility */
|
|
|
|
lnblank = true;
|
|
|
|
break;
|
2010-03-12 17:55:29 +00:00
|
|
|
case 'B': /* compress blank lines around removed section */
|
|
|
|
compblank = true;
|
|
|
|
break;
|
2002-05-23 16:50:41 +00:00
|
|
|
case 'c': /* treat -D as -U and vice versa */
|
2002-05-15 16:30:28 +00:00
|
|
|
complement = true;
|
2002-05-23 16:50:41 +00:00
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
debugging = true;
|
|
|
|
break;
|
2003-01-20 12:41:41 +00:00
|
|
|
case 'e': /* fewer errors from dodgy lines */
|
|
|
|
iocccok = true;
|
|
|
|
break;
|
2015-02-25 21:10:03 +00:00
|
|
|
case 'f': /* definitions file */
|
|
|
|
defundefile(optarg);
|
|
|
|
break;
|
2013-03-28 20:33:07 +00:00
|
|
|
case 'h':
|
|
|
|
help();
|
|
|
|
break;
|
2009-11-25 20:23:18 +00:00
|
|
|
case 'K': /* keep ambiguous #ifs */
|
|
|
|
strictlogic = true;
|
|
|
|
break;
|
2002-12-18 20:50:44 +00:00
|
|
|
case 'k': /* process constant #ifs */
|
|
|
|
killconsts = true;
|
|
|
|
break;
|
2013-03-28 20:33:07 +00:00
|
|
|
case 'm': /* modify in place */
|
|
|
|
inplace = true;
|
|
|
|
break;
|
|
|
|
case 'M': /* modify in place and keep backup */
|
|
|
|
inplace = true;
|
2015-12-03 14:21:55 +00:00
|
|
|
if (strlen(optarg) > 0)
|
|
|
|
backext = optarg;
|
2013-03-28 20:33:07 +00:00
|
|
|
break;
|
2005-03-08 12:52:00 +00:00
|
|
|
case 'n': /* add #line directive after deleted lines */
|
|
|
|
lnnum = true;
|
|
|
|
break;
|
2010-01-19 18:13:54 +00:00
|
|
|
case 'o': /* output to a file */
|
|
|
|
ofilename = optarg;
|
|
|
|
break;
|
2002-05-23 16:50:41 +00:00
|
|
|
case 's': /* only output list of symbols that control #ifs */
|
2002-05-15 16:30:28 +00:00
|
|
|
symlist = true;
|
2002-05-23 16:50:41 +00:00
|
|
|
break;
|
2010-03-12 17:55:29 +00:00
|
|
|
case 'S': /* list symbols with their nesting depth */
|
|
|
|
symlist = symdepth = true;
|
|
|
|
break;
|
2002-12-18 20:50:44 +00:00
|
|
|
case 't': /* don't parse C comments */
|
2002-05-15 16:30:28 +00:00
|
|
|
text = true;
|
2002-05-23 16:50:41 +00:00
|
|
|
break;
|
2013-03-28 20:33:07 +00:00
|
|
|
case 'V':
|
2010-03-12 17:55:29 +00:00
|
|
|
version();
|
2013-03-28 20:33:07 +00:00
|
|
|
break;
|
|
|
|
case 'x':
|
|
|
|
exitmode = atoi(optarg);
|
|
|
|
if(exitmode < 0 || exitmode > 2)
|
|
|
|
usage();
|
|
|
|
break;
|
2002-05-23 16:50:41 +00:00
|
|
|
default:
|
2002-05-15 16:30:28 +00:00
|
|
|
usage();
|
|
|
|
}
|
2002-05-23 16:50:41 +00:00
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
2009-11-25 20:23:18 +00:00
|
|
|
if (compblank && lnblank)
|
|
|
|
errx(2, "-B and -b are mutually exclusive");
|
2013-03-28 20:33:07 +00:00
|
|
|
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");
|
2015-12-03 14:21:55 +00:00
|
|
|
if (argc == 0 && inplace)
|
|
|
|
errx(2, "-m requires an input file");
|
2013-03-28 20:33:07 +00:00
|
|
|
if (argc == 0)
|
|
|
|
argc = 1;
|
|
|
|
if (argc == 1 && !inplace && ofilename == NULL)
|
|
|
|
ofilename = "-";
|
2015-02-25 21:10:03 +00:00
|
|
|
indirectsym();
|
2013-03-28 20:33:07 +00:00
|
|
|
|
|
|
|
atexit(cleantemp);
|
|
|
|
if (ofilename != NULL)
|
|
|
|
processinout(*argv, ofilename);
|
|
|
|
else while (argc-- > 0) {
|
|
|
|
processinout(*argv, *argv);
|
|
|
|
argv++;
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
2013-03-28 20:33:07 +00:00
|
|
|
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]";
|
|
|
|
linefile = NULL;
|
2019-11-19 03:15:06 +00:00
|
|
|
input = stdin;
|
2010-01-19 18:13:54 +00:00
|
|
|
} else {
|
2013-03-28 20:33:07 +00:00
|
|
|
filename = ifn;
|
|
|
|
linefile = ifn;
|
|
|
|
input = fopen(ifn, "rb");
|
|
|
|
if (input == NULL)
|
|
|
|
err(2, "can't open %s", ifn);
|
|
|
|
}
|
|
|
|
if (strcmp(ofn, "-") == 0) {
|
2019-11-19 03:15:06 +00:00
|
|
|
output = stdout;
|
2013-03-28 20:33:07 +00:00
|
|
|
process();
|
|
|
|
return;
|
2010-01-19 18:13:54 +00:00
|
|
|
}
|
2013-03-28 20:33:07 +00:00
|
|
|
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);
|
|
|
|
|
2003-07-01 15:30:43 +00:00
|
|
|
process();
|
2013-03-28 20:33:07 +00:00
|
|
|
|
|
|
|
if (backext != NULL) {
|
|
|
|
char *backname = astrcat(ofn, backext);
|
|
|
|
if (rename(ofn, backname) < 0)
|
2015-02-25 21:10:03 +00:00
|
|
|
err(2, "can't rename \"%s\" to \"%s\"", ofn, backname);
|
2013-03-28 20:33:07 +00:00
|
|
|
free(backname);
|
|
|
|
}
|
2015-12-03 14:21:55 +00:00
|
|
|
/* leave file unmodified if unifdef made no changes */
|
|
|
|
if (!altered && backext == NULL) {
|
|
|
|
if (remove(tempname) < 0)
|
|
|
|
warn("can't remove \"%s\"", tempname);
|
2019-11-19 03:15:06 +00:00
|
|
|
} else if (rename(tempname, ofn) < 0)
|
2013-03-28 20:33:07 +00:00
|
|
|
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);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
2013-03-28 20:33:07 +00:00
|
|
|
/*
|
|
|
|
* Self-identification functions.
|
|
|
|
*/
|
|
|
|
|
2010-03-12 17:55:29 +00:00
|
|
|
static void
|
|
|
|
version(void)
|
|
|
|
{
|
|
|
|
const char *c = copyright;
|
|
|
|
for (;;) {
|
|
|
|
while (*++c != '$')
|
|
|
|
if (*c == '\0')
|
|
|
|
exit(0);
|
|
|
|
while (*++c != '$')
|
|
|
|
putc(*c, stderr);
|
|
|
|
putc('\n', stderr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-28 20:33:07 +00:00
|
|
|
static void
|
|
|
|
synopsis(FILE *fp)
|
|
|
|
{
|
|
|
|
fprintf(fp,
|
|
|
|
"usage: unifdef [-bBcdehKkmnsStV] [-x{012}] [-Mext] [-opath] \\\n"
|
2015-02-25 21:10:03 +00:00
|
|
|
" [-[i]Dsym[=val]] [-[i]Usym] [-fpath] ... [file] ...\n");
|
2013-03-28 20:33:07 +00:00
|
|
|
}
|
|
|
|
|
2002-12-18 20:50:44 +00:00
|
|
|
static void
|
2002-05-15 16:30:28 +00:00
|
|
|
usage(void)
|
1997-08-20 11:07:53 +00:00
|
|
|
{
|
2013-03-28 20:33:07 +00:00
|
|
|
synopsis(stderr);
|
2003-01-20 12:41:41 +00:00
|
|
|
exit(2);
|
1997-08-20 11:07:53 +00:00
|
|
|
}
|
|
|
|
|
2013-03-28 20:33:07 +00:00
|
|
|
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"
|
2015-02-25 21:10:03 +00:00
|
|
|
" -fpath file containing #define and #undef directives\n"
|
2013-03-28 20:33:07 +00:00
|
|
|
" -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);
|
|
|
|
}
|
|
|
|
|
2002-05-15 16:30:28 +00:00
|
|
|
/*
|
2002-12-18 20:50:44 +00:00
|
|
|
* A state transition function alters the global #if processing state
|
|
|
|
* in a particular way. The table below is indexed by the current
|
2003-07-01 15:30:43 +00:00
|
|
|
* processing state and the type of the current line.
|
2002-12-18 20:50:44 +00:00
|
|
|
*
|
|
|
|
* Nesting is handled by keeping a stack of states; some transition
|
2003-01-20 12:41:41 +00:00
|
|
|
* functions increase or decrease the depth. They also maintain the
|
2002-12-18 20:50:44 +00:00
|
|
|
* ignore state on a stack. In some complicated cases they have to
|
|
|
|
* alter the preprocessor directive, as follows.
|
|
|
|
*
|
|
|
|
* When we have processed a group that starts off with a known-false
|
|
|
|
* #if/#elif sequence (which has therefore been deleted) followed by a
|
2003-01-20 12:41:41 +00:00
|
|
|
* #elif that we don't understand and therefore must keep, we edit the
|
2013-03-28 20:33:07 +00:00
|
|
|
* latter into a #if to keep the nesting correct. We use memcpy() to
|
2011-01-18 19:13:03 +00:00
|
|
|
* overwrite the 4 byte token "elif" with "if " without a '\0' byte.
|
2002-12-18 20:50:44 +00:00
|
|
|
*
|
|
|
|
* 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
|
2003-01-20 12:41:41 +00:00
|
|
|
* #else will be discarded. We edit the #elif into a #else and the
|
2002-12-18 20:50:44 +00:00
|
|
|
* following directive to #endif since this has the desired behaviour.
|
2003-01-20 12:41:41 +00:00
|
|
|
*
|
|
|
|
* "Dodgy" directives are split across multiple lines, the most common
|
|
|
|
* example being a multi-line comment hanging off the right of the
|
|
|
|
* directive. We can handle them correctly only if there is no change
|
|
|
|
* from printing to dropping (or vice versa) caused by that directive.
|
|
|
|
* If the directive is the first of a group we have a choice between
|
|
|
|
* failing with an error, or passing it through unchanged instead of
|
|
|
|
* evaluating it. The latter is not the default to avoid questions from
|
|
|
|
* users about unifdef unexpectedly leaving behind preprocessor directives.
|
2002-12-18 20:50:44 +00:00
|
|
|
*/
|
|
|
|
typedef void state_fn(void);
|
|
|
|
|
|
|
|
/* report an error */
|
|
|
|
static void Eelif (void) { error("Inappropriate #elif"); }
|
|
|
|
static void Eelse (void) { error("Inappropriate #else"); }
|
|
|
|
static void Eendif(void) { error("Inappropriate #endif"); }
|
|
|
|
static void Eeof (void) { error("Premature EOF"); }
|
|
|
|
static void Eioccc(void) { error("Obfuscated preprocessor control line"); }
|
|
|
|
/* plain line handling */
|
|
|
|
static void print (void) { flushline(true); }
|
|
|
|
static void drop (void) { flushline(false); }
|
|
|
|
/* output lacks group's start line */
|
2003-01-20 12:41:41 +00:00
|
|
|
static void Strue (void) { drop(); ignoreoff(); state(IS_TRUE_PREFIX); }
|
|
|
|
static void Sfalse(void) { drop(); ignoreoff(); state(IS_FALSE_PREFIX); }
|
|
|
|
static void Selse (void) { drop(); state(IS_TRUE_ELSE); }
|
2002-12-18 20:50:44 +00:00
|
|
|
/* print/pass this block */
|
2003-01-20 12:41:41 +00:00
|
|
|
static void Pelif (void) { print(); ignoreoff(); state(IS_PASS_MIDDLE); }
|
|
|
|
static void Pelse (void) { print(); state(IS_PASS_ELSE); }
|
2005-03-08 12:52:00 +00:00
|
|
|
static void Pendif(void) { print(); unnest(); }
|
2002-12-18 20:50:44 +00:00
|
|
|
/* discard this block */
|
2003-01-20 12:41:41 +00:00
|
|
|
static void Dfalse(void) { drop(); ignoreoff(); state(IS_FALSE_TRAILER); }
|
|
|
|
static void Delif (void) { drop(); ignoreoff(); state(IS_FALSE_MIDDLE); }
|
|
|
|
static void Delse (void) { drop(); state(IS_FALSE_ELSE); }
|
2005-03-08 12:52:00 +00:00
|
|
|
static void Dendif(void) { drop(); unnest(); }
|
2002-12-18 20:50:44 +00:00
|
|
|
/* first line of group */
|
|
|
|
static void Fdrop (void) { nest(); Dfalse(); }
|
|
|
|
static void Fpass (void) { nest(); Pelif(); }
|
|
|
|
static void Ftrue (void) { nest(); Strue(); }
|
|
|
|
static void Ffalse(void) { nest(); Sfalse(); }
|
2003-01-20 12:41:41 +00:00
|
|
|
/* variable pedantry for obfuscated lines */
|
2005-03-08 12:52:00 +00:00
|
|
|
static void Oiffy (void) { if (!iocccok) Eioccc(); Fpass(); ignoreon(); }
|
|
|
|
static void Oif (void) { if (!iocccok) Eioccc(); Fpass(); }
|
|
|
|
static void Oelif (void) { if (!iocccok) Eioccc(); Pelif(); }
|
2002-12-18 20:50:44 +00:00
|
|
|
/* ignore comments in this block */
|
2003-01-20 12:41:41 +00:00
|
|
|
static void Idrop (void) { Fdrop(); ignoreon(); }
|
|
|
|
static void Itrue (void) { Ftrue(); ignoreon(); }
|
|
|
|
static void Ifalse(void) { Ffalse(); ignoreon(); }
|
2011-01-18 19:13:03 +00:00
|
|
|
/* modify this line */
|
2013-03-28 20:33:07 +00:00
|
|
|
static void Mpass (void) { memcpy(keyword, "if ", 4); Pelif(); }
|
2010-02-19 16:54:51 +00:00
|
|
|
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); }
|
2002-12-18 20:50:44 +00:00
|
|
|
|
|
|
|
static state_fn * const trans_table[IS_COUNT][LT_COUNT] = {
|
|
|
|
/* IS_OUTSIDE */
|
2003-01-20 12:41:41 +00:00
|
|
|
{ Itrue, Ifalse,Fpass, Ftrue, Ffalse,Eelif, Eelif, Eelif, Eelse, Eendif,
|
|
|
|
Oiffy, Oiffy, Fpass, Oif, Oif, Eelif, Eelif, Eelif, Eelse, Eendif,
|
2009-11-25 20:23:18 +00:00
|
|
|
print, done, abort },
|
2002-12-18 20:50:44 +00:00
|
|
|
/* IS_FALSE_PREFIX */
|
2003-01-20 12:41:41 +00:00
|
|
|
{ Idrop, Idrop, Fdrop, Fdrop, Fdrop, Mpass, Strue, Sfalse,Selse, Dendif,
|
|
|
|
Idrop, Idrop, Fdrop, Fdrop, Fdrop, Mpass, Eioccc,Eioccc,Eioccc,Eioccc,
|
2009-11-25 20:23:18 +00:00
|
|
|
drop, Eeof, abort },
|
2002-12-18 20:50:44 +00:00
|
|
|
/* IS_TRUE_PREFIX */
|
2003-01-20 12:41:41 +00:00
|
|
|
{ Itrue, Ifalse,Fpass, Ftrue, Ffalse,Dfalse,Dfalse,Dfalse,Delse, Dendif,
|
|
|
|
Oiffy, Oiffy, Fpass, Oif, Oif, Eioccc,Eioccc,Eioccc,Eioccc,Eioccc,
|
2009-11-25 20:23:18 +00:00
|
|
|
print, Eeof, abort },
|
2002-12-18 20:50:44 +00:00
|
|
|
/* IS_PASS_MIDDLE */
|
2003-01-20 12:41:41 +00:00
|
|
|
{ Itrue, Ifalse,Fpass, Ftrue, Ffalse,Pelif, Mtrue, Delif, Pelse, Pendif,
|
|
|
|
Oiffy, Oiffy, Fpass, Oif, Oif, Pelif, Oelif, Oelif, Pelse, Pendif,
|
2009-11-25 20:23:18 +00:00
|
|
|
print, Eeof, abort },
|
2002-12-18 20:50:44 +00:00
|
|
|
/* IS_FALSE_MIDDLE */
|
2003-01-20 12:41:41 +00:00
|
|
|
{ Idrop, Idrop, Fdrop, Fdrop, Fdrop, Pelif, Mtrue, Delif, Pelse, Pendif,
|
|
|
|
Idrop, Idrop, Fdrop, Fdrop, Fdrop, Eioccc,Eioccc,Eioccc,Eioccc,Eioccc,
|
2009-11-25 20:23:18 +00:00
|
|
|
drop, Eeof, abort },
|
2002-12-18 20:50:44 +00:00
|
|
|
/* IS_TRUE_MIDDLE */
|
2003-01-20 12:41:41 +00:00
|
|
|
{ Itrue, Ifalse,Fpass, Ftrue, Ffalse,Melif, Melif, Melif, Melse, Pendif,
|
|
|
|
Oiffy, Oiffy, Fpass, Oif, Oif, Eioccc,Eioccc,Eioccc,Eioccc,Pendif,
|
2009-11-25 20:23:18 +00:00
|
|
|
print, Eeof, abort },
|
2002-12-18 20:50:44 +00:00
|
|
|
/* IS_PASS_ELSE */
|
2003-01-20 12:41:41 +00:00
|
|
|
{ Itrue, Ifalse,Fpass, Ftrue, Ffalse,Eelif, Eelif, Eelif, Eelse, Pendif,
|
|
|
|
Oiffy, Oiffy, Fpass, Oif, Oif, Eelif, Eelif, Eelif, Eelse, Pendif,
|
2009-11-25 20:23:18 +00:00
|
|
|
print, Eeof, abort },
|
2002-12-18 20:50:44 +00:00
|
|
|
/* IS_FALSE_ELSE */
|
2003-01-20 12:41:41 +00:00
|
|
|
{ Idrop, Idrop, Fdrop, Fdrop, Fdrop, Eelif, Eelif, Eelif, Eelse, Dendif,
|
|
|
|
Idrop, Idrop, Fdrop, Fdrop, Fdrop, Eelif, Eelif, Eelif, Eelse, Eioccc,
|
2009-11-25 20:23:18 +00:00
|
|
|
drop, Eeof, abort },
|
2002-12-18 20:50:44 +00:00
|
|
|
/* IS_TRUE_ELSE */
|
2003-01-20 12:41:41 +00:00
|
|
|
{ Itrue, Ifalse,Fpass, Ftrue, Ffalse,Eelif, Eelif, Eelif, Eelse, Dendif,
|
|
|
|
Oiffy, Oiffy, Fpass, Oif, Oif, Eelif, Eelif, Eelif, Eelse, Eioccc,
|
2009-11-25 20:23:18 +00:00
|
|
|
print, Eeof, abort },
|
2002-12-18 20:50:44 +00:00
|
|
|
/* IS_FALSE_TRAILER */
|
2003-01-20 12:41:41 +00:00
|
|
|
{ Idrop, Idrop, Fdrop, Fdrop, Fdrop, Dfalse,Dfalse,Dfalse,Delse, Dendif,
|
|
|
|
Idrop, Idrop, Fdrop, Fdrop, Fdrop, Dfalse,Dfalse,Dfalse,Delse, Eioccc,
|
2009-11-25 20:23:18 +00:00
|
|
|
drop, Eeof, abort }
|
2003-01-20 12:41:41 +00:00
|
|
|
/*TRUEI FALSEI IF TRUE FALSE ELIF ELTRUE ELFALSE ELSE ENDIF
|
|
|
|
TRUEI FALSEI IF TRUE FALSE ELIF ELTRUE ELFALSE ELSE ENDIF (DODGY)
|
2009-11-25 20:23:18 +00:00
|
|
|
PLAIN EOF ERROR */
|
2002-12-18 20:50:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* State machine utility functions
|
2002-05-15 16:30:28 +00:00
|
|
|
*/
|
2002-12-18 20:50:44 +00:00
|
|
|
static void
|
2003-01-20 12:41:41 +00:00
|
|
|
ignoreoff(void)
|
|
|
|
{
|
2005-03-08 12:52:00 +00:00
|
|
|
if (depth == 0)
|
|
|
|
abort(); /* bug */
|
2003-01-20 12:41:41 +00:00
|
|
|
ignoring[depth] = ignoring[depth-1];
|
|
|
|
}
|
|
|
|
static void
|
|
|
|
ignoreon(void)
|
|
|
|
{
|
|
|
|
ignoring[depth] = true;
|
|
|
|
}
|
|
|
|
static void
|
|
|
|
keywordedit(const char *replacement)
|
|
|
|
{
|
2010-02-19 16:54:51 +00:00
|
|
|
snprintf(keyword, tline + sizeof(tline) - keyword,
|
|
|
|
"%s%s", replacement, newline);
|
2015-12-03 14:21:55 +00:00
|
|
|
altered = true;
|
2003-01-20 12:41:41 +00:00
|
|
|
print();
|
|
|
|
}
|
|
|
|
static void
|
2002-12-18 20:50:44 +00:00
|
|
|
nest(void)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2009-11-27 17:53:49 +00:00
|
|
|
if (depth > MAXDEPTH-1)
|
|
|
|
abort(); /* bug */
|
|
|
|
if (depth == MAXDEPTH-1)
|
2002-12-18 20:50:44 +00:00
|
|
|
error("Too many levels of nesting");
|
2009-11-27 17:53:49 +00:00
|
|
|
depth += 1;
|
2002-12-18 20:50:44 +00:00
|
|
|
stifline[depth] = linenum;
|
|
|
|
}
|
|
|
|
static void
|
2005-03-08 12:52:00 +00:00
|
|
|
unnest(void)
|
|
|
|
{
|
|
|
|
if (depth == 0)
|
|
|
|
abort(); /* bug */
|
|
|
|
depth -= 1;
|
|
|
|
}
|
|
|
|
static void
|
2002-12-18 20:50:44 +00:00
|
|
|
state(Ifstate is)
|
|
|
|
{
|
|
|
|
ifstate[depth] = is;
|
|
|
|
}
|
|
|
|
|
2013-03-28 20:33:07 +00:00
|
|
|
/*
|
|
|
|
* 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");
|
2015-02-25 21:10:03 +00:00
|
|
|
closeio();
|
2013-03-28 20:33:07 +00:00
|
|
|
}
|
|
|
|
|
2002-12-18 20:50:44 +00:00
|
|
|
/*
|
|
|
|
* Write a line to the output or not, according to command line options.
|
2015-02-25 21:10:03 +00:00
|
|
|
* If writing fails, closeio() will print the error and exit.
|
2002-12-18 20:50:44 +00:00
|
|
|
*/
|
|
|
|
static void
|
|
|
|
flushline(bool keep)
|
|
|
|
{
|
|
|
|
if (symlist)
|
|
|
|
return;
|
2005-03-08 12:52:00 +00:00
|
|
|
if (keep ^ complement) {
|
2010-02-19 16:54:51 +00:00
|
|
|
bool blankline = tline[strspn(tline, " \t\r\n")] == '\0';
|
2009-11-25 20:23:18 +00:00
|
|
|
if (blankline && compblank && blankcount != blankmax) {
|
|
|
|
delcount += 1;
|
|
|
|
blankcount += 1;
|
|
|
|
} else {
|
2013-03-28 20:33:07 +00:00
|
|
|
if (lnnum && delcount > 0)
|
|
|
|
hashline();
|
2011-01-21 18:10:11 +00:00
|
|
|
if (fputs(tline, output) == EOF)
|
2015-02-25 21:10:03 +00:00
|
|
|
closeio();
|
2009-11-25 20:23:18 +00:00
|
|
|
delcount = 0;
|
|
|
|
blankmax = blankcount = blankline ? blankcount + 1 : 0;
|
|
|
|
}
|
2005-03-08 12:52:00 +00:00
|
|
|
} else {
|
2011-01-21 18:10:11 +00:00
|
|
|
if (lnblank && fputs(newline, output) == EOF)
|
2015-02-25 21:10:03 +00:00
|
|
|
closeio();
|
2015-12-03 14:21:55 +00:00
|
|
|
altered = true;
|
2005-03-08 12:52:00 +00:00
|
|
|
delcount += 1;
|
2009-11-25 20:23:18 +00:00
|
|
|
blankcount = 0;
|
2002-05-15 16:30:28 +00:00
|
|
|
}
|
2011-01-21 18:10:11 +00:00
|
|
|
if (debugging && fflush(output) == EOF)
|
2015-02-25 21:10:03 +00:00
|
|
|
closeio();
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
2002-05-15 16:30:28 +00:00
|
|
|
/*
|
2013-03-28 20:33:07 +00:00
|
|
|
* Format of #line directives depends on whether we know the input filename.
|
2002-05-15 16:30:28 +00:00
|
|
|
*/
|
2002-12-18 20:50:44 +00:00
|
|
|
static void
|
2013-03-28 20:33:07 +00:00
|
|
|
hashline(void)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2013-03-28 20:33:07 +00:00
|
|
|
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)
|
2015-02-25 21:10:03 +00:00
|
|
|
closeio();
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
2010-01-19 18:13:54 +00:00
|
|
|
/*
|
|
|
|
* Flush the output and handle errors.
|
|
|
|
*/
|
|
|
|
static void
|
2015-02-25 21:10:03 +00:00
|
|
|
closeio(void)
|
2010-01-19 18:13:54 +00:00
|
|
|
{
|
2013-03-28 20:33:07 +00:00
|
|
|
/* Tidy up after findsym(). */
|
2010-03-12 17:55:29 +00:00
|
|
|
if (symdepth && !zerosyms)
|
|
|
|
printf("\n");
|
2015-02-25 21:10:03 +00:00
|
|
|
if (output != NULL && (ferror(output) || fclose(output) == EOF))
|
|
|
|
err(2, "%s: can't write to output", filename);
|
|
|
|
fclose(input);
|
2010-01-19 18:13:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2013-03-28 20:33:07 +00:00
|
|
|
* The driver for the state machine.
|
2010-01-19 18:13:54 +00:00
|
|
|
*/
|
|
|
|
static void
|
2013-03-28 20:33:07 +00:00
|
|
|
process(void)
|
2010-01-19 18:13:54 +00:00
|
|
|
{
|
2013-03-28 20:33:07 +00:00
|
|
|
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;
|
2015-02-25 21:10:03 +00:00
|
|
|
newline = NULL;
|
|
|
|
linenum = 0;
|
2015-12-03 14:21:55 +00:00
|
|
|
altered = false;
|
2013-03-28 20:33:07 +00:00
|
|
|
while (lineval != LT_EOF) {
|
|
|
|
lineval = parseline();
|
|
|
|
trans_table[ifstate[depth]][lineval]();
|
|
|
|
debug("process line %d %s -> %s depth %d",
|
|
|
|
linenum, linetype_name[lineval],
|
|
|
|
ifstate_name[ifstate[depth]], depth);
|
2010-01-19 18:13:54 +00:00
|
|
|
}
|
2015-12-03 14:21:55 +00:00
|
|
|
exitstat |= altered;
|
2010-01-19 18:13:54 +00:00
|
|
|
}
|
|
|
|
|
2002-05-15 16:30:28 +00:00
|
|
|
/*
|
2002-12-18 20:50:44 +00:00
|
|
|
* Parse a line and determine its type. We keep the preprocessor line
|
2003-06-30 14:46:25 +00:00
|
|
|
* parser state between calls in the global variable linestate, with
|
|
|
|
* help from skipcomment().
|
2002-05-15 16:30:28 +00:00
|
|
|
*/
|
2002-12-18 20:50:44 +00:00
|
|
|
static Linetype
|
2009-11-25 20:23:18 +00:00
|
|
|
parseline(void)
|
2002-05-15 16:30:28 +00:00
|
|
|
{
|
|
|
|
const char *cp;
|
2019-11-20 19:43:34 +00:00
|
|
|
struct macro *cursym;
|
2002-05-15 16:30:28 +00:00
|
|
|
Linetype retval;
|
2002-12-18 20:50:44 +00:00
|
|
|
Comment_state wascomment;
|
2002-05-15 16:30:28 +00:00
|
|
|
|
2015-02-25 21:10:03 +00:00
|
|
|
wascomment = incomment;
|
|
|
|
cp = skiphash();
|
|
|
|
if (cp == NULL)
|
|
|
|
return (LT_EOF);
|
2010-02-19 16:54:51 +00:00
|
|
|
if (newline == NULL) {
|
|
|
|
if (strrchr(tline, '\n') == strrchr(tline, '\r') + 1)
|
|
|
|
newline = newline_crlf;
|
|
|
|
else
|
|
|
|
newline = newline_unix;
|
|
|
|
}
|
2015-02-25 21:10:03 +00:00
|
|
|
if (*cp == '\0') {
|
|
|
|
retval = LT_PLAIN;
|
|
|
|
goto done;
|
2002-05-15 16:30:28 +00:00
|
|
|
}
|
2015-02-25 21:10:03 +00:00
|
|
|
keyword = tline + (cp - tline);
|
|
|
|
if ((cp = matchsym("ifdef", keyword)) != NULL ||
|
|
|
|
(cp = matchsym("ifndef", keyword)) != NULL) {
|
|
|
|
cp = skipcomment(cp);
|
2019-11-20 19:43:34 +00:00
|
|
|
if ((cursym = findsym(&cp)) == NULL)
|
2015-02-25 21:10:03 +00:00
|
|
|
retval = LT_IF;
|
|
|
|
else {
|
|
|
|
retval = (keyword[2] == 'n')
|
|
|
|
? LT_FALSE : LT_TRUE;
|
2019-11-20 19:43:34 +00:00
|
|
|
if (cursym->value == NULL)
|
2015-02-25 21:10:03 +00:00
|
|
|
retval = (retval == LT_TRUE)
|
|
|
|
? LT_FALSE : LT_TRUE;
|
2019-11-20 19:43:34 +00:00
|
|
|
if (cursym->ignore)
|
2015-02-25 21:10:03 +00:00
|
|
|
retval = (retval == LT_TRUE)
|
|
|
|
? LT_TRUEI : LT_FALSEI;
|
|
|
|
}
|
|
|
|
} 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);
|
2003-01-20 12:41:41 +00:00
|
|
|
/* no way can we deal with a continuation inside a keyword */
|
2010-02-19 16:54:51 +00:00
|
|
|
if (strncmp(cp, "\\\r\n", 3) == 0 ||
|
|
|
|
strncmp(cp, "\\\n", 2) == 0)
|
2002-12-18 20:50:44 +00:00
|
|
|
Eioccc();
|
2015-02-25 21:10:03 +00:00
|
|
|
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 {
|
2002-12-18 20:50:44 +00:00
|
|
|
linestate = LS_DIRTY;
|
2008-02-29 12:57:14 +00:00
|
|
|
}
|
2002-12-18 20:50:44 +00:00
|
|
|
}
|
2015-02-25 21:10:03 +00:00
|
|
|
if (retval != LT_PLAIN && (wascomment || linestate != LS_START)) {
|
|
|
|
retval = linetype_2dodgy(retval);
|
|
|
|
linestate = LS_DIRTY;
|
2002-12-18 20:50:44 +00:00
|
|
|
}
|
2015-02-25 21:10:03 +00:00
|
|
|
done:
|
2010-03-12 17:55:29 +00:00
|
|
|
debug("parser line %d state %s comment %s line", linenum,
|
2002-12-18 20:50:44 +00:00
|
|
|
comment_name[incomment], linestate_name[linestate]);
|
2003-01-17 19:12:02 +00:00
|
|
|
return (retval);
|
2002-05-15 16:30:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2003-06-30 14:46:25 +00:00
|
|
|
* These are the binary operators that are supported by the expression
|
2009-11-25 20:23:18 +00:00
|
|
|
* evaluator.
|
2002-05-15 16:30:28 +00:00
|
|
|
*/
|
2015-02-25 21:10:03 +00:00
|
|
|
static Linetype op_strict(long *p, long v, Linetype at, Linetype bt) {
|
2009-11-25 20:23:18 +00:00
|
|
|
if(at == LT_IF || bt == LT_IF) return (LT_IF);
|
|
|
|
return (*p = v, v ? LT_TRUE : LT_FALSE);
|
|
|
|
}
|
2015-02-25 21:10:03 +00:00
|
|
|
static Linetype op_lt(long *p, Linetype at, long a, Linetype bt, long b) {
|
2009-11-25 20:23:18 +00:00
|
|
|
return op_strict(p, a < b, at, bt);
|
|
|
|
}
|
2015-02-25 21:10:03 +00:00
|
|
|
static Linetype op_gt(long *p, Linetype at, long a, Linetype bt, long b) {
|
2009-11-25 20:23:18 +00:00
|
|
|
return op_strict(p, a > b, at, bt);
|
|
|
|
}
|
2015-02-25 21:10:03 +00:00
|
|
|
static Linetype op_le(long *p, Linetype at, long a, Linetype bt, long b) {
|
2009-11-25 20:23:18 +00:00
|
|
|
return op_strict(p, a <= b, at, bt);
|
|
|
|
}
|
2015-02-25 21:10:03 +00:00
|
|
|
static Linetype op_ge(long *p, Linetype at, long a, Linetype bt, long b) {
|
2009-11-25 20:23:18 +00:00
|
|
|
return op_strict(p, a >= b, at, bt);
|
|
|
|
}
|
2015-02-25 21:10:03 +00:00
|
|
|
static Linetype op_eq(long *p, Linetype at, long a, Linetype bt, long b) {
|
2009-11-25 20:23:18 +00:00
|
|
|
return op_strict(p, a == b, at, bt);
|
|
|
|
}
|
2015-02-25 21:10:03 +00:00
|
|
|
static Linetype op_ne(long *p, Linetype at, long a, Linetype bt, long b) {
|
2009-11-25 20:23:18 +00:00
|
|
|
return op_strict(p, a != b, at, bt);
|
|
|
|
}
|
2015-02-25 21:10:03 +00:00
|
|
|
static Linetype op_or(long *p, Linetype at, long a, Linetype bt, long b) {
|
2009-11-25 20:23:18 +00:00
|
|
|
if (!strictlogic && (at == LT_TRUE || bt == LT_TRUE))
|
|
|
|
return (*p = 1, LT_TRUE);
|
|
|
|
return op_strict(p, a || b, at, bt);
|
|
|
|
}
|
2015-02-25 21:10:03 +00:00
|
|
|
static Linetype op_and(long *p, Linetype at, long a, Linetype bt, long b) {
|
2009-11-25 20:23:18 +00:00
|
|
|
if (!strictlogic && (at == LT_FALSE || bt == LT_FALSE))
|
|
|
|
return (*p = 0, LT_FALSE);
|
|
|
|
return op_strict(p, a && b, at, bt);
|
|
|
|
}
|
2015-12-03 14:21:55 +00:00
|
|
|
static Linetype op_blsh(long *p, Linetype at, long a, Linetype bt, long b) {
|
|
|
|
return op_strict(p, a << b, at, bt);
|
|
|
|
}
|
|
|
|
static Linetype op_brsh(long *p, Linetype at, long a, Linetype bt, long b) {
|
|
|
|
return op_strict(p, a >> b, at, bt);
|
|
|
|
}
|
|
|
|
static Linetype op_add(long *p, Linetype at, long a, Linetype bt, long b) {
|
|
|
|
return op_strict(p, a + b, at, bt);
|
|
|
|
}
|
|
|
|
static Linetype op_sub(long *p, Linetype at, long a, Linetype bt, long b) {
|
|
|
|
return op_strict(p, a - b, at, bt);
|
|
|
|
}
|
|
|
|
static Linetype op_mul(long *p, Linetype at, long a, Linetype bt, long b) {
|
|
|
|
return op_strict(p, a * b, at, bt);
|
|
|
|
}
|
|
|
|
static Linetype op_div(long *p, Linetype at, long a, Linetype bt, long b) {
|
|
|
|
if (bt != LT_TRUE) {
|
|
|
|
debug("eval division by zero");
|
|
|
|
return (LT_ERROR);
|
|
|
|
}
|
|
|
|
return op_strict(p, a / b, at, bt);
|
|
|
|
}
|
|
|
|
static Linetype op_mod(long *p, Linetype at, long a, Linetype bt, long b) {
|
|
|
|
return op_strict(p, a % b, at, bt);
|
|
|
|
}
|
|
|
|
static Linetype op_bor(long *p, Linetype at, long a, Linetype bt, long b) {
|
|
|
|
return op_strict(p, a | b, at, bt);
|
|
|
|
}
|
|
|
|
static Linetype op_bxor(long *p, Linetype at, long a, Linetype bt, long b) {
|
|
|
|
return op_strict(p, a ^ b, at, bt);
|
|
|
|
}
|
|
|
|
static Linetype op_band(long *p, Linetype at, long a, Linetype bt, long b) {
|
|
|
|
return op_strict(p, a & b, at, bt);
|
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2002-05-15 16:30:28 +00:00
|
|
|
/*
|
2002-12-18 20:50:44 +00:00
|
|
|
* An evaluation function takes three arguments, as follows: (1) a pointer to
|
|
|
|
* an element of the precedence table which lists the operators at the current
|
|
|
|
* level of precedence; (2) a pointer to an integer which will receive the
|
|
|
|
* 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
|
2009-11-25 20:23:18 +00:00
|
|
|
* 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.
|
2002-05-15 16:30:28 +00:00
|
|
|
*/
|
2002-12-18 20:50:44 +00:00
|
|
|
struct ops;
|
|
|
|
|
2015-02-25 21:10:03 +00:00
|
|
|
typedef Linetype eval_fn(const struct ops *, long *, const char **);
|
2002-12-18 20:50:44 +00:00
|
|
|
|
|
|
|
static eval_fn eval_table, eval_unary;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The precedence table. Expressions involving binary operators are evaluated
|
|
|
|
* in a table-driven way by eval_table. When it evaluates a subexpression it
|
|
|
|
* calls the inner function with its first argument pointing to the next
|
|
|
|
* element of the table. Innermost expressions have special non-table-driven
|
|
|
|
* handling.
|
2015-12-03 14:21:55 +00:00
|
|
|
*
|
|
|
|
* The stop characters help with lexical analysis: an operator is not
|
|
|
|
* recognized if it is followed by one of the stop characters because
|
|
|
|
* that would make it a different operator.
|
2002-12-18 20:50:44 +00:00
|
|
|
*/
|
2013-03-28 20:33:07 +00:00
|
|
|
struct op {
|
|
|
|
const char *str;
|
2015-02-25 21:10:03 +00:00
|
|
|
Linetype (*fn)(long *, Linetype, long, Linetype, long);
|
2015-12-03 14:21:55 +00:00
|
|
|
const char *stop;
|
2013-03-28 20:33:07 +00:00
|
|
|
};
|
|
|
|
struct ops {
|
2002-12-18 20:50:44 +00:00
|
|
|
eval_fn *inner;
|
2013-03-28 20:33:07 +00:00
|
|
|
struct op op[5];
|
|
|
|
};
|
|
|
|
static const struct ops eval_ops[] = {
|
2015-12-03 14:32:54 +00:00
|
|
|
{ eval_table, { { "||", op_or, NULL } } },
|
|
|
|
{ eval_table, { { "&&", op_and, NULL } } },
|
2015-12-03 14:21:55 +00:00
|
|
|
{ eval_table, { { "|", op_bor, "|" } } },
|
2015-12-03 14:32:54 +00:00
|
|
|
{ eval_table, { { "^", op_bxor, NULL } } },
|
2015-12-03 14:21:55 +00:00
|
|
|
{ eval_table, { { "&", op_band, "&" } } },
|
2015-12-03 14:32:54 +00:00
|
|
|
{ eval_table, { { "==", op_eq, NULL },
|
|
|
|
{ "!=", op_ne, NULL } } },
|
|
|
|
{ eval_table, { { "<=", op_le, NULL },
|
|
|
|
{ ">=", op_ge, NULL },
|
2015-12-03 14:21:55 +00:00
|
|
|
{ "<", op_lt, "<=" },
|
|
|
|
{ ">", op_gt, ">=" } } },
|
2015-12-03 14:32:54 +00:00
|
|
|
{ eval_table, { { "<<", op_blsh, NULL },
|
|
|
|
{ ">>", op_brsh, NULL } } },
|
|
|
|
{ eval_table, { { "+", op_add, NULL },
|
|
|
|
{ "-", op_sub, NULL } } },
|
|
|
|
{ eval_unary, { { "*", op_mul, NULL },
|
|
|
|
{ "/", op_div, NULL },
|
|
|
|
{ "%", op_mod, NULL } } },
|
2002-12-18 20:50:44 +00:00
|
|
|
};
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2013-03-28 20:33:07 +00:00
|
|
|
/* Current operator precedence level */
|
2015-02-25 21:10:03 +00:00
|
|
|
static long prec(const struct ops *ops)
|
2013-03-28 20:33:07 +00:00
|
|
|
{
|
|
|
|
return (ops - eval_ops);
|
|
|
|
}
|
|
|
|
|
2002-05-15 16:30:28 +00:00
|
|
|
/*
|
|
|
|
* Function for evaluating the innermost parts of expressions,
|
2009-11-25 20:23:18 +00:00
|
|
|
* viz. !expr (expr) number defined(symbol) symbol
|
|
|
|
* We reset the constexpr flag in the last two cases.
|
2002-05-15 16:30:28 +00:00
|
|
|
*/
|
2002-12-18 20:50:44 +00:00
|
|
|
static Linetype
|
2015-02-25 21:10:03 +00:00
|
|
|
eval_unary(const struct ops *ops, long *valp, const char **cpp)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2002-05-15 16:30:28 +00:00
|
|
|
const char *cp;
|
|
|
|
char *ep;
|
2019-11-20 19:43:34 +00:00
|
|
|
struct macro *sym;
|
2008-02-29 12:57:14 +00:00
|
|
|
bool defparen;
|
2009-11-25 20:23:18 +00:00
|
|
|
Linetype lt;
|
2002-05-15 16:30:28 +00:00
|
|
|
|
|
|
|
cp = skipcomment(*cpp);
|
2003-01-20 12:41:41 +00:00
|
|
|
if (*cp == '!') {
|
2013-03-28 20:33:07 +00:00
|
|
|
debug("eval%d !", prec(ops));
|
1994-05-27 12:33:43 +00:00
|
|
|
cp++;
|
2009-11-25 20:23:18 +00:00
|
|
|
lt = eval_unary(ops, valp, &cp);
|
|
|
|
if (lt == LT_ERROR)
|
|
|
|
return (LT_ERROR);
|
|
|
|
if (lt != LT_IF) {
|
|
|
|
*valp = !*valp;
|
|
|
|
lt = *valp ? LT_TRUE : LT_FALSE;
|
|
|
|
}
|
2015-12-03 14:21:55 +00:00
|
|
|
} else if (*cp == '~') {
|
|
|
|
debug("eval%d ~", prec(ops));
|
|
|
|
cp++;
|
|
|
|
lt = eval_unary(ops, valp, &cp);
|
|
|
|
if (lt == LT_ERROR)
|
|
|
|
return (LT_ERROR);
|
|
|
|
if (lt != LT_IF) {
|
|
|
|
*valp = ~(*valp);
|
|
|
|
lt = *valp ? LT_TRUE : LT_FALSE;
|
|
|
|
}
|
|
|
|
} else if (*cp == '-') {
|
|
|
|
debug("eval%d -", prec(ops));
|
|
|
|
cp++;
|
|
|
|
lt = eval_unary(ops, valp, &cp);
|
|
|
|
if (lt == LT_ERROR)
|
|
|
|
return (LT_ERROR);
|
|
|
|
if (lt != LT_IF) {
|
|
|
|
*valp = -(*valp);
|
|
|
|
lt = *valp ? LT_TRUE : LT_FALSE;
|
|
|
|
}
|
2002-05-15 16:30:28 +00:00
|
|
|
} else if (*cp == '(') {
|
|
|
|
cp++;
|
2013-03-28 20:33:07 +00:00
|
|
|
debug("eval%d (", prec(ops));
|
2009-11-25 20:23:18 +00:00
|
|
|
lt = eval_table(eval_ops, valp, &cp);
|
|
|
|
if (lt == LT_ERROR)
|
|
|
|
return (LT_ERROR);
|
2002-05-15 16:30:28 +00:00
|
|
|
cp = skipcomment(cp);
|
|
|
|
if (*cp++ != ')')
|
2009-11-25 20:23:18 +00:00
|
|
|
return (LT_ERROR);
|
2002-05-15 16:30:28 +00:00
|
|
|
} else if (isdigit((unsigned char)*cp)) {
|
2013-03-28 20:33:07 +00:00
|
|
|
debug("eval%d number", prec(ops));
|
2002-05-15 16:30:28 +00:00
|
|
|
*valp = strtol(cp, &ep, 0);
|
2009-11-25 20:23:18 +00:00
|
|
|
if (ep == cp)
|
|
|
|
return (LT_ERROR);
|
|
|
|
lt = *valp ? LT_TRUE : LT_FALSE;
|
2015-02-25 21:10:03 +00:00
|
|
|
cp = ep;
|
|
|
|
} else if (matchsym("defined", cp) != NULL) {
|
2002-05-15 16:30:28 +00:00
|
|
|
cp = skipcomment(cp+7);
|
2008-02-29 12:57:14 +00:00
|
|
|
if (*cp == '(') {
|
|
|
|
cp = skipcomment(cp+1);
|
|
|
|
defparen = true;
|
|
|
|
} else {
|
|
|
|
defparen = false;
|
|
|
|
}
|
2015-02-25 21:10:03 +00:00
|
|
|
sym = findsym(&cp);
|
|
|
|
cp = skipcomment(cp);
|
|
|
|
if (defparen && *cp++ != ')') {
|
|
|
|
debug("eval%d defined missing ')'", prec(ops));
|
|
|
|
return (LT_ERROR);
|
|
|
|
}
|
2019-11-20 19:43:34 +00:00
|
|
|
if (sym == NULL) {
|
2015-02-25 21:10:03 +00:00
|
|
|
debug("eval%d defined unknown", prec(ops));
|
2009-11-25 20:23:18 +00:00
|
|
|
lt = LT_IF;
|
|
|
|
} else {
|
2019-11-20 19:43:34 +00:00
|
|
|
debug("eval%d defined %s", prec(ops), sym->name);
|
|
|
|
*valp = (sym->value != NULL);
|
2009-11-25 20:23:18 +00:00
|
|
|
lt = *valp ? LT_TRUE : LT_FALSE;
|
|
|
|
}
|
|
|
|
constexpr = false;
|
2002-05-15 16:30:28 +00:00
|
|
|
} else if (!endsym(*cp)) {
|
2013-03-28 20:33:07 +00:00
|
|
|
debug("eval%d symbol", prec(ops));
|
2015-02-25 21:10:03 +00:00
|
|
|
sym = findsym(&cp);
|
2019-11-20 19:43:34 +00:00
|
|
|
if (sym == NULL) {
|
2009-11-25 20:23:18 +00:00
|
|
|
lt = LT_IF;
|
|
|
|
cp = skipargs(cp);
|
2019-11-20 19:43:34 +00:00
|
|
|
} else if (sym->value == NULL) {
|
2002-05-15 16:30:28 +00:00
|
|
|
*valp = 0;
|
2009-11-25 20:23:18 +00:00
|
|
|
lt = LT_FALSE;
|
|
|
|
} else {
|
2019-11-20 19:43:34 +00:00
|
|
|
*valp = strtol(sym->value, &ep, 0);
|
|
|
|
if (*ep != '\0' || ep == sym->value)
|
2009-11-25 20:23:18 +00:00
|
|
|
return (LT_ERROR);
|
|
|
|
lt = *valp ? LT_TRUE : LT_FALSE;
|
|
|
|
cp = skipargs(cp);
|
2002-05-15 16:30:28 +00:00
|
|
|
}
|
2009-11-25 20:23:18 +00:00
|
|
|
constexpr = false;
|
2003-07-01 15:30:43 +00:00
|
|
|
} else {
|
2013-03-28 20:33:07 +00:00
|
|
|
debug("eval%d bad expr", prec(ops));
|
2009-11-25 20:23:18 +00:00
|
|
|
return (LT_ERROR);
|
2003-07-01 15:30:43 +00:00
|
|
|
}
|
2002-05-15 16:30:28 +00:00
|
|
|
|
|
|
|
*cpp = cp;
|
2013-03-28 20:33:07 +00:00
|
|
|
debug("eval%d = %d", prec(ops), *valp);
|
2009-11-25 20:23:18 +00:00
|
|
|
return (lt);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2002-05-15 16:30:28 +00:00
|
|
|
* Table-driven evaluation of binary operators.
|
1994-05-27 12:33:43 +00:00
|
|
|
*/
|
2002-12-18 20:50:44 +00:00
|
|
|
static Linetype
|
2015-02-25 21:10:03 +00:00
|
|
|
eval_table(const struct ops *ops, long *valp, const char **cpp)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2002-12-18 20:50:44 +00:00
|
|
|
const struct op *op;
|
2002-05-15 16:30:28 +00:00
|
|
|
const char *cp;
|
2015-12-03 14:21:55 +00:00
|
|
|
long val = 0;
|
2009-11-25 20:23:18 +00:00
|
|
|
Linetype lt, rt;
|
2002-05-15 16:30:28 +00:00
|
|
|
|
2013-03-28 20:33:07 +00:00
|
|
|
debug("eval%d", prec(ops));
|
2002-05-15 16:30:28 +00:00
|
|
|
cp = *cpp;
|
2009-11-25 20:23:18 +00:00
|
|
|
lt = ops->inner(ops+1, valp, &cp);
|
|
|
|
if (lt == LT_ERROR)
|
|
|
|
return (LT_ERROR);
|
1994-05-27 12:33:43 +00:00
|
|
|
for (;;) {
|
2002-05-15 16:30:28 +00:00
|
|
|
cp = skipcomment(cp);
|
2015-12-03 14:21:55 +00:00
|
|
|
for (op = ops->op; op->str != NULL; op++) {
|
|
|
|
if (strncmp(cp, op->str, strlen(op->str)) == 0) {
|
|
|
|
/* assume only one-char operators have stop chars */
|
|
|
|
if (op->stop != NULL && cp[1] != '\0' &&
|
|
|
|
strchr(op->stop, cp[1]) != NULL)
|
|
|
|
continue;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2002-05-15 16:30:28 +00:00
|
|
|
if (op->str == NULL)
|
|
|
|
break;
|
|
|
|
cp += strlen(op->str);
|
2013-03-28 20:33:07 +00:00
|
|
|
debug("eval%d %s", prec(ops), op->str);
|
2009-11-25 20:23:18 +00:00
|
|
|
rt = ops->inner(ops+1, &val, &cp);
|
|
|
|
if (rt == LT_ERROR)
|
|
|
|
return (LT_ERROR);
|
|
|
|
lt = op->fn(valp, lt, *valp, rt, val);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
2002-05-15 16:30:28 +00:00
|
|
|
|
|
|
|
*cpp = cp;
|
2013-03-28 20:33:07 +00:00
|
|
|
debug("eval%d = %d", prec(ops), *valp);
|
|
|
|
debug("eval%d lt = %s", prec(ops), linetype_name[lt]);
|
2009-11-25 20:23:18 +00:00
|
|
|
return (lt);
|
2002-05-15 16:30:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Evaluate the expression on a #if or #elif line. If we can work out
|
|
|
|
* the result we return LT_TRUE or LT_FALSE accordingly, otherwise we
|
2002-12-18 20:50:44 +00:00
|
|
|
* return just a generic LT_IF.
|
2002-05-15 16:30:28 +00:00
|
|
|
*/
|
2002-12-18 20:50:44 +00:00
|
|
|
static Linetype
|
2002-05-15 16:30:28 +00:00
|
|
|
ifeval(const char **cpp)
|
|
|
|
{
|
2013-03-28 20:33:07 +00:00
|
|
|
Linetype ret;
|
2015-02-25 21:10:03 +00:00
|
|
|
long val = 0;
|
2002-12-18 20:50:44 +00:00
|
|
|
|
2002-05-15 16:30:28 +00:00
|
|
|
debug("eval %s", *cpp);
|
2009-11-25 20:23:18 +00:00
|
|
|
constexpr = killconsts ? false : true;
|
2002-12-18 20:50:44 +00:00
|
|
|
ret = eval_table(eval_ops, &val, cpp);
|
2003-07-01 15:30:43 +00:00
|
|
|
debug("eval = %d", val);
|
2009-11-25 20:23:18 +00:00
|
|
|
return (constexpr ? LT_IF : ret == LT_ERROR ? LT_IF : ret);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
2015-02-25 21:10:03 +00:00
|
|
|
/*
|
|
|
|
* Read a line and examine its initial part to determine if it is a
|
|
|
|
* preprocessor directive. Returns NULL on EOF, or a pointer to a
|
|
|
|
* preprocessor directive name, or a pointer to the zero byte at the
|
|
|
|
* end of the line.
|
|
|
|
*/
|
|
|
|
static const char *
|
|
|
|
skiphash(void)
|
|
|
|
{
|
|
|
|
const char *cp;
|
|
|
|
|
|
|
|
linenum++;
|
|
|
|
if (fgets(tline, MAXLINE, input) == NULL) {
|
|
|
|
if (ferror(input))
|
|
|
|
err(2, "can't read %s", filename);
|
|
|
|
else
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
cp = skipcomment(tline);
|
|
|
|
if (linestate == LS_START && *cp == '#') {
|
|
|
|
linestate = LS_HASH;
|
|
|
|
return (skipcomment(cp + 1));
|
|
|
|
} else if (*cp == '\0') {
|
|
|
|
return (cp);
|
|
|
|
} else {
|
|
|
|
return (skipline(cp));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Mark a line dirty and consume the rest of it, keeping track of the
|
|
|
|
* lexical state.
|
|
|
|
*/
|
|
|
|
static const char *
|
|
|
|
skipline(const char *cp)
|
|
|
|
{
|
2015-12-03 14:21:55 +00:00
|
|
|
const char *pcp;
|
2015-02-25 21:10:03 +00:00
|
|
|
if (*cp != '\0')
|
|
|
|
linestate = LS_DIRTY;
|
2015-12-03 14:21:55 +00:00
|
|
|
while (*cp != '\0') {
|
|
|
|
cp = skipcomment(pcp = cp);
|
|
|
|
if (pcp == cp)
|
|
|
|
cp++;
|
|
|
|
}
|
2015-02-25 21:10:03 +00:00
|
|
|
return (cp);
|
|
|
|
}
|
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
/*
|
2005-03-08 12:52:00 +00:00
|
|
|
* Skip over comments, strings, and character literals and stop at the
|
|
|
|
* next character position that is not whitespace. Between calls we keep
|
|
|
|
* the comment state in the global variable incomment, and we also adjust
|
|
|
|
* the global variable linestate when we see a newline.
|
2002-12-18 20:50:44 +00:00
|
|
|
* XXX: doesn't cope with the buffer splitting inside a state transition.
|
1994-05-27 12:33:43 +00:00
|
|
|
*/
|
2002-12-18 20:50:44 +00:00
|
|
|
static const char *
|
2002-05-15 16:30:28 +00:00
|
|
|
skipcomment(const char *cp)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2002-12-18 20:50:44 +00:00
|
|
|
if (text || ignoring[depth]) {
|
2003-06-30 14:46:25 +00:00
|
|
|
for (; isspace((unsigned char)*cp); cp++)
|
|
|
|
if (*cp == '\n')
|
|
|
|
linestate = LS_START;
|
2003-01-17 19:12:02 +00:00
|
|
|
return (cp);
|
2002-12-18 20:50:44 +00:00
|
|
|
}
|
|
|
|
while (*cp != '\0')
|
2003-07-01 15:30:43 +00:00
|
|
|
/* don't reset to LS_START after a line continuation */
|
2010-02-19 16:54:51 +00:00
|
|
|
if (strncmp(cp, "\\\r\n", 3) == 0)
|
|
|
|
cp += 3;
|
|
|
|
else if (strncmp(cp, "\\\n", 2) == 0)
|
2002-12-18 20:50:44 +00:00
|
|
|
cp += 2;
|
|
|
|
else switch (incomment) {
|
|
|
|
case NO_COMMENT:
|
2010-02-19 16:54:51 +00:00
|
|
|
if (strncmp(cp, "/\\\r\n", 4) == 0) {
|
|
|
|
incomment = STARTING_COMMENT;
|
|
|
|
cp += 4;
|
|
|
|
} else if (strncmp(cp, "/\\\n", 3) == 0) {
|
2002-12-18 20:50:44 +00:00
|
|
|
incomment = STARTING_COMMENT;
|
|
|
|
cp += 3;
|
|
|
|
} else if (strncmp(cp, "/*", 2) == 0) {
|
2002-05-15 16:30:28 +00:00
|
|
|
incomment = C_COMMENT;
|
2002-12-18 20:50:44 +00:00
|
|
|
cp += 2;
|
|
|
|
} else if (strncmp(cp, "//", 2) == 0) {
|
2002-05-15 16:30:28 +00:00
|
|
|
incomment = CXX_COMMENT;
|
2002-12-18 20:50:44 +00:00
|
|
|
cp += 2;
|
2005-03-08 12:52:00 +00:00
|
|
|
} else if (strncmp(cp, "\'", 1) == 0) {
|
|
|
|
incomment = CHAR_LITERAL;
|
|
|
|
linestate = LS_DIRTY;
|
|
|
|
cp += 1;
|
|
|
|
} else if (strncmp(cp, "\"", 1) == 0) {
|
|
|
|
incomment = STRING_LITERAL;
|
|
|
|
linestate = LS_DIRTY;
|
|
|
|
cp += 1;
|
2002-12-18 20:50:44 +00:00
|
|
|
} else if (strncmp(cp, "\n", 1) == 0) {
|
|
|
|
linestate = LS_START;
|
|
|
|
cp += 1;
|
2010-02-19 16:54:51 +00:00
|
|
|
} else if (strchr(" \r\t", *cp) != NULL) {
|
2002-12-18 20:50:44 +00:00
|
|
|
cp += 1;
|
|
|
|
} else
|
2003-01-17 19:12:02 +00:00
|
|
|
return (cp);
|
2002-12-18 20:50:44 +00:00
|
|
|
continue;
|
|
|
|
case CXX_COMMENT:
|
|
|
|
if (strncmp(cp, "\n", 1) == 0) {
|
|
|
|
incomment = NO_COMMENT;
|
|
|
|
linestate = LS_START;
|
2002-05-15 16:30:28 +00:00
|
|
|
}
|
2002-12-18 20:50:44 +00:00
|
|
|
cp += 1;
|
|
|
|
continue;
|
2005-03-08 12:52:00 +00:00
|
|
|
case CHAR_LITERAL:
|
|
|
|
case STRING_LITERAL:
|
|
|
|
if ((incomment == CHAR_LITERAL && cp[0] == '\'') ||
|
|
|
|
(incomment == STRING_LITERAL && cp[0] == '\"')) {
|
|
|
|
incomment = NO_COMMENT;
|
|
|
|
cp += 1;
|
|
|
|
} else if (cp[0] == '\\') {
|
|
|
|
if (cp[1] == '\0')
|
|
|
|
cp += 1;
|
|
|
|
else
|
|
|
|
cp += 2;
|
|
|
|
} else if (strncmp(cp, "\n", 1) == 0) {
|
|
|
|
if (incomment == CHAR_LITERAL)
|
2015-12-03 14:21:55 +00:00
|
|
|
error("Unterminated char literal");
|
2005-03-08 12:52:00 +00:00
|
|
|
else
|
2015-12-03 14:21:55 +00:00
|
|
|
error("Unterminated string literal");
|
2005-03-08 12:52:00 +00:00
|
|
|
} else
|
|
|
|
cp += 1;
|
|
|
|
continue;
|
2002-12-18 20:50:44 +00:00
|
|
|
case C_COMMENT:
|
2010-02-19 16:54:51 +00:00
|
|
|
if (strncmp(cp, "*\\\r\n", 4) == 0) {
|
|
|
|
incomment = FINISHING_COMMENT;
|
|
|
|
cp += 4;
|
|
|
|
} else if (strncmp(cp, "*\\\n", 3) == 0) {
|
2002-12-18 20:50:44 +00:00
|
|
|
incomment = FINISHING_COMMENT;
|
|
|
|
cp += 3;
|
|
|
|
} else if (strncmp(cp, "*/", 2) == 0) {
|
|
|
|
incomment = NO_COMMENT;
|
|
|
|
cp += 2;
|
|
|
|
} else
|
|
|
|
cp += 1;
|
|
|
|
continue;
|
|
|
|
case STARTING_COMMENT:
|
|
|
|
if (*cp == '*') {
|
|
|
|
incomment = C_COMMENT;
|
|
|
|
cp += 1;
|
|
|
|
} else if (*cp == '/') {
|
|
|
|
incomment = CXX_COMMENT;
|
|
|
|
cp += 1;
|
|
|
|
} else {
|
|
|
|
incomment = NO_COMMENT;
|
|
|
|
linestate = LS_DIRTY;
|
2002-05-15 16:30:28 +00:00
|
|
|
}
|
2002-12-18 20:50:44 +00:00
|
|
|
continue;
|
|
|
|
case FINISHING_COMMENT:
|
|
|
|
if (*cp == '/') {
|
|
|
|
incomment = NO_COMMENT;
|
|
|
|
cp += 1;
|
|
|
|
} else
|
|
|
|
incomment = C_COMMENT;
|
|
|
|
continue;
|
|
|
|
default:
|
2003-07-01 15:30:43 +00:00
|
|
|
abort(); /* bug */
|
2002-05-15 16:30:28 +00:00
|
|
|
}
|
2003-01-17 19:12:02 +00:00
|
|
|
return (cp);
|
2002-05-15 16:30:28 +00:00
|
|
|
}
|
|
|
|
|
2009-11-25 20:23:18 +00:00
|
|
|
/*
|
|
|
|
* Skip macro arguments.
|
|
|
|
*/
|
|
|
|
static const char *
|
|
|
|
skipargs(const char *cp)
|
|
|
|
{
|
|
|
|
const char *ocp = cp;
|
|
|
|
int level = 0;
|
|
|
|
cp = skipcomment(cp);
|
|
|
|
if (*cp != '(')
|
|
|
|
return (cp);
|
|
|
|
do {
|
|
|
|
if (*cp == '(')
|
|
|
|
level++;
|
|
|
|
if (*cp == ')')
|
|
|
|
level--;
|
|
|
|
cp = skipcomment(cp+1);
|
|
|
|
} while (level != 0 && *cp != '\0');
|
|
|
|
if (level == 0)
|
|
|
|
return (cp);
|
|
|
|
else
|
|
|
|
/* Rewind and re-detect the syntax error later. */
|
|
|
|
return (ocp);
|
|
|
|
}
|
|
|
|
|
2002-05-15 16:30:28 +00:00
|
|
|
/*
|
|
|
|
* Skip over an identifier.
|
|
|
|
*/
|
2002-12-18 20:50:44 +00:00
|
|
|
static const char *
|
2002-05-15 16:30:28 +00:00
|
|
|
skipsym(const char *cp)
|
|
|
|
{
|
|
|
|
while (!endsym(*cp))
|
|
|
|
++cp;
|
2003-01-17 19:12:02 +00:00
|
|
|
return (cp);
|
2002-05-15 16:30:28 +00:00
|
|
|
}
|
|
|
|
|
2015-02-25 21:10:03 +00:00
|
|
|
/*
|
|
|
|
* Skip whitespace and take a copy of any following identifier.
|
|
|
|
*/
|
|
|
|
static const char *
|
|
|
|
getsym(const char **cpp)
|
|
|
|
{
|
|
|
|
const char *cp = *cpp, *sym;
|
|
|
|
|
|
|
|
cp = skipcomment(cp);
|
|
|
|
cp = skipsym(sym = cp);
|
|
|
|
if (cp == sym)
|
|
|
|
return NULL;
|
|
|
|
*cpp = cp;
|
|
|
|
return (xstrdup(sym, cp));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check that s (a symbol) matches the start of t, and that the
|
|
|
|
* following character in t is not a symbol character. Returns a
|
|
|
|
* pointer to the following character in t if there is a match,
|
|
|
|
* otherwise NULL.
|
|
|
|
*/
|
|
|
|
static const char *
|
|
|
|
matchsym(const char *s, const char *t)
|
|
|
|
{
|
|
|
|
while (*s != '\0' && *t != '\0')
|
|
|
|
if (*s != *t)
|
|
|
|
return (NULL);
|
|
|
|
else
|
|
|
|
++s, ++t;
|
|
|
|
if (*s == '\0' && endsym(*t))
|
|
|
|
return(t);
|
|
|
|
else
|
|
|
|
return(NULL);
|
|
|
|
}
|
|
|
|
|
2002-05-15 16:30:28 +00:00
|
|
|
/*
|
2008-03-02 22:27:49 +00:00
|
|
|
* Look for the symbol in the symbol table. If it is found, we return
|
2002-12-18 20:50:44 +00:00
|
|
|
* the symbol table index, else we return -1.
|
1994-05-27 12:33:43 +00:00
|
|
|
*/
|
2019-11-20 19:43:34 +00:00
|
|
|
static struct macro *
|
2015-02-25 21:10:03 +00:00
|
|
|
findsym(const char **strp)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2015-02-25 21:10:03 +00:00
|
|
|
const char *str;
|
2019-11-20 19:43:34 +00:00
|
|
|
char *strkey;
|
|
|
|
struct macro key, *res;
|
2002-05-15 16:30:28 +00:00
|
|
|
|
2015-02-25 21:10:03 +00:00
|
|
|
str = *strp;
|
|
|
|
*strp = skipsym(str);
|
2005-03-08 12:52:00 +00:00
|
|
|
if (symlist) {
|
2015-02-25 21:10:03 +00:00
|
|
|
if (*strp == str)
|
2019-11-20 19:43:34 +00:00
|
|
|
return (NULL);
|
2010-03-12 17:55:29 +00:00
|
|
|
if (symdepth && firstsym)
|
|
|
|
printf("%s%3d", zerosyms ? "" : "\n", depth);
|
|
|
|
firstsym = zerosyms = false;
|
|
|
|
printf("%s%.*s%s",
|
2015-02-25 21:10:03 +00:00
|
|
|
symdepth ? " " : "",
|
|
|
|
(int)(*strp-str), str,
|
|
|
|
symdepth ? "" : "\n");
|
2005-03-08 12:52:00 +00:00
|
|
|
/* we don't care about the value of the symbol */
|
2019-11-20 19:43:34 +00:00
|
|
|
return (NULL);
|
2005-03-08 12:52:00 +00:00
|
|
|
}
|
2019-11-20 19:43:34 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 'str' just points into the current mid-parse input and is not
|
|
|
|
* nul-terminated. We know the length of the symbol, *strp - str, but
|
|
|
|
* need to provide a nul-terminated lookup key for RB_FIND's comparison
|
|
|
|
* function. Create one here.
|
|
|
|
*/
|
|
|
|
strkey = malloc(*strp - str + 1);
|
|
|
|
memcpy(strkey, str, *strp - str);
|
|
|
|
strkey[*strp - str] = 0;
|
|
|
|
|
|
|
|
key.name = strkey;
|
|
|
|
res = RB_FIND(MACROMAP, ¯o_tree, &key);
|
|
|
|
if (res != NULL)
|
|
|
|
debugsym("findsym", res);
|
|
|
|
|
|
|
|
free(strkey);
|
|
|
|
return (res);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
2015-02-25 21:10:03 +00:00
|
|
|
/*
|
|
|
|
* Resolve indirect symbol values to their final definitions.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
indirectsym(void)
|
|
|
|
{
|
|
|
|
const char *cp;
|
2019-11-20 19:43:34 +00:00
|
|
|
int changed;
|
|
|
|
struct macro *sym, *ind;
|
2015-02-25 21:10:03 +00:00
|
|
|
|
|
|
|
do {
|
|
|
|
changed = 0;
|
2019-11-20 19:43:34 +00:00
|
|
|
RB_FOREACH(sym, MACROMAP, ¯o_tree) {
|
|
|
|
if (sym->value == NULL)
|
2015-02-25 21:10:03 +00:00
|
|
|
continue;
|
2019-11-20 19:43:34 +00:00
|
|
|
cp = sym->value;
|
2015-02-25 21:10:03 +00:00
|
|
|
ind = findsym(&cp);
|
2019-11-20 19:43:34 +00:00
|
|
|
if (ind == NULL || ind == sym ||
|
2015-02-25 21:10:03 +00:00
|
|
|
*cp != '\0' ||
|
2019-11-20 19:43:34 +00:00
|
|
|
ind->value == NULL ||
|
|
|
|
ind->value == sym->value)
|
2015-02-25 21:10:03 +00:00
|
|
|
continue;
|
|
|
|
debugsym("indir...", sym);
|
2019-11-20 19:43:34 +00:00
|
|
|
sym->value = ind->value;
|
2015-02-25 21:10:03 +00:00
|
|
|
debugsym("...ectsym", sym);
|
|
|
|
changed++;
|
|
|
|
}
|
|
|
|
} while (changed);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Add a symbol to the symbol table, specified with the format sym=val
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
addsym1(bool ignorethis, bool definethis, char *symval)
|
|
|
|
{
|
|
|
|
const char *sym, *val;
|
|
|
|
|
|
|
|
sym = symval;
|
|
|
|
val = skipsym(sym);
|
|
|
|
if (definethis && *val == '=') {
|
|
|
|
symval[val - sym] = '\0';
|
|
|
|
val = val + 1;
|
|
|
|
} else if (*val == '\0') {
|
|
|
|
val = definethis ? "1" : NULL;
|
|
|
|
} else {
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
addsym2(ignorethis, sym, val);
|
|
|
|
}
|
|
|
|
|
2002-05-23 16:50:41 +00:00
|
|
|
/*
|
|
|
|
* Add a symbol to the symbol table.
|
|
|
|
*/
|
2002-12-18 20:50:44 +00:00
|
|
|
static void
|
2019-11-20 19:43:34 +00:00
|
|
|
addsym2(bool ignorethis, const char *symname, const char *val)
|
2002-05-23 16:50:41 +00:00
|
|
|
{
|
2019-11-20 19:43:34 +00:00
|
|
|
const char *cp = symname;
|
|
|
|
struct macro *sym, *r;
|
|
|
|
|
|
|
|
sym = findsym(&cp);
|
|
|
|
if (sym == NULL) {
|
|
|
|
sym = calloc(1, sizeof(*sym));
|
|
|
|
sym->ignore = ignorethis;
|
|
|
|
sym->name = symname;
|
|
|
|
sym->value = val;
|
|
|
|
r = RB_INSERT(MACROMAP, ¯o_tree, sym);
|
|
|
|
assert(r == NULL);
|
2002-05-23 16:50:41 +00:00
|
|
|
}
|
2019-11-20 19:43:34 +00:00
|
|
|
debugsym("addsym", sym);
|
2015-02-25 21:10:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2019-11-20 19:43:34 +00:00
|
|
|
debugsym(const char *why, const struct macro *sym)
|
2015-02-25 21:10:03 +00:00
|
|
|
{
|
2019-11-20 19:43:34 +00:00
|
|
|
debug("%s %s%c%s", why, sym->name,
|
|
|
|
sym->value ? '=' : ' ',
|
|
|
|
sym->value ? sym->value : "undef");
|
2002-05-23 16:50:41 +00:00
|
|
|
}
|
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
/*
|
2015-02-25 21:10:03 +00:00
|
|
|
* Add symbols to the symbol table from a file containing
|
|
|
|
* #define and #undef preprocessor directives.
|
1994-05-27 12:33:43 +00:00
|
|
|
*/
|
2015-02-25 21:10:03 +00:00
|
|
|
static void
|
|
|
|
defundefile(const char *fn)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2015-02-25 21:10:03 +00:00
|
|
|
filename = fn;
|
|
|
|
input = fopen(fn, "rb");
|
|
|
|
if (input == NULL)
|
|
|
|
err(2, "can't open %s", fn);
|
|
|
|
linenum = 0;
|
|
|
|
while (defundef())
|
|
|
|
;
|
|
|
|
if (ferror(input))
|
|
|
|
err(2, "can't read %s", filename);
|
|
|
|
else
|
|
|
|
fclose(input);
|
|
|
|
if (incomment)
|
|
|
|
error("EOF in comment");
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read and process one #define or #undef directive
|
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
defundef(void)
|
|
|
|
{
|
|
|
|
const char *cp, *kw, *sym, *val, *end;
|
|
|
|
|
|
|
|
cp = skiphash();
|
|
|
|
if (cp == NULL)
|
|
|
|
return (false);
|
|
|
|
if (*cp == '\0')
|
|
|
|
goto done;
|
|
|
|
/* strip trailing whitespace, and do a fairly rough check to
|
|
|
|
avoid unsupported multi-line preprocessor directives */
|
|
|
|
end = cp + strlen(cp);
|
|
|
|
while (end > tline && strchr(" \t\n\r", end[-1]) != NULL)
|
|
|
|
--end;
|
|
|
|
if (end > tline && end[-1] == '\\')
|
|
|
|
Eioccc();
|
|
|
|
|
|
|
|
kw = cp;
|
|
|
|
if ((cp = matchsym("define", kw)) != NULL) {
|
|
|
|
sym = getsym(&cp);
|
|
|
|
if (sym == NULL)
|
2015-12-03 14:21:55 +00:00
|
|
|
error("Missing macro name in #define");
|
2015-02-25 21:10:03 +00:00
|
|
|
if (*cp == '(') {
|
|
|
|
val = "1";
|
|
|
|
} else {
|
|
|
|
cp = skipcomment(cp);
|
|
|
|
val = (cp < end) ? xstrdup(cp, end) : "";
|
|
|
|
}
|
|
|
|
debug("#define");
|
|
|
|
addsym2(false, sym, val);
|
|
|
|
} else if ((cp = matchsym("undef", kw)) != NULL) {
|
|
|
|
sym = getsym(&cp);
|
|
|
|
if (sym == NULL)
|
2015-12-03 14:21:55 +00:00
|
|
|
error("Missing macro name in #undef");
|
2015-02-25 21:10:03 +00:00
|
|
|
cp = skipcomment(cp);
|
|
|
|
debug("#undef");
|
|
|
|
addsym2(false, sym, NULL);
|
|
|
|
} else {
|
2015-12-03 14:21:55 +00:00
|
|
|
error("Unrecognized preprocessor directive");
|
2015-02-25 21:10:03 +00:00
|
|
|
}
|
|
|
|
skipline(cp);
|
|
|
|
done:
|
|
|
|
debug("parser line %d state %s comment %s line", linenum,
|
|
|
|
comment_name[incomment], linestate_name[linestate]);
|
|
|
|
return (true);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
2013-03-28 20:33:07 +00:00
|
|
|
/*
|
|
|
|
* Concatenate two strings into new memory, checking for failure.
|
|
|
|
*/
|
|
|
|
static char *
|
|
|
|
astrcat(const char *s1, const char *s2)
|
|
|
|
{
|
|
|
|
char *s;
|
|
|
|
int len;
|
2015-02-25 21:10:03 +00:00
|
|
|
size_t size;
|
|
|
|
|
|
|
|
len = snprintf(NULL, 0, "%s%s", s1, s2);
|
|
|
|
if (len < 0)
|
|
|
|
err(2, "snprintf");
|
|
|
|
size = (size_t)len + 1;
|
|
|
|
s = (char *)malloc(size);
|
|
|
|
if (s == NULL)
|
|
|
|
err(2, "malloc");
|
|
|
|
snprintf(s, size, "%s%s", s1, s2);
|
|
|
|
return (s);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Duplicate a segment of a string, checking for failure.
|
|
|
|
*/
|
|
|
|
static const char *
|
|
|
|
xstrdup(const char *start, const char *end)
|
|
|
|
{
|
|
|
|
size_t n;
|
|
|
|
char *s;
|
2013-03-28 20:33:07 +00:00
|
|
|
|
2015-02-25 21:10:03 +00:00
|
|
|
if (end < start) abort(); /* bug */
|
|
|
|
n = (size_t)(end - start) + 1;
|
|
|
|
s = malloc(n);
|
2013-03-28 20:33:07 +00:00
|
|
|
if (s == NULL)
|
|
|
|
err(2, "malloc");
|
2015-02-25 21:10:03 +00:00
|
|
|
snprintf(s, n, "%s", start);
|
2013-03-28 20:33:07 +00:00
|
|
|
return (s);
|
|
|
|
}
|
|
|
|
|
2002-05-15 16:30:28 +00:00
|
|
|
/*
|
2002-12-18 20:50:44 +00:00
|
|
|
* Diagnostics.
|
2002-05-15 16:30:28 +00:00
|
|
|
*/
|
2002-12-18 20:50:44 +00:00
|
|
|
static void
|
2002-05-15 16:30:28 +00:00
|
|
|
debug(const char *msg, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
if (debugging) {
|
|
|
|
va_start(ap, msg);
|
|
|
|
vwarnx(msg, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-12-18 20:50:44 +00:00
|
|
|
static void
|
|
|
|
error(const char *msg)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2002-12-18 20:50:44 +00:00
|
|
|
if (depth == 0)
|
2003-01-20 12:41:41 +00:00
|
|
|
warnx("%s: %d: %s", filename, linenum, msg);
|
2002-05-15 16:30:28 +00:00
|
|
|
else
|
2003-01-20 12:41:41 +00:00
|
|
|
warnx("%s: %d: %s (#if line %d depth %d)",
|
2002-12-18 20:50:44 +00:00
|
|
|
filename, linenum, msg, stifline[depth], depth);
|
2015-02-25 21:10:03 +00:00
|
|
|
closeio();
|
2015-12-03 14:21:55 +00:00
|
|
|
errx(2, "Output may be truncated");
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|