1994-05-27 12:33:43 +00:00
|
|
|
/*
|
2005-03-08 12:52:00 +00:00
|
|
|
* 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).
|
|
|
|
*
|
1994-05-27 12:33:43 +00:00
|
|
|
* Copyright (c) 1985, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2001-12-11 23:20:23 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
#ifndef lint
|
2003-05-03 19:44:46 +00:00
|
|
|
#if 0
|
1997-08-20 11:07:53 +00:00
|
|
|
static const char copyright[] =
|
1994-05-27 12:33:43 +00:00
|
|
|
"@(#) Copyright (c) 1985, 1993\n\
|
|
|
|
The Regents of the University of California. All rights reserved.\n";
|
2003-05-03 19:44:46 +00:00
|
|
|
#endif
|
2002-05-15 16:30:28 +00:00
|
|
|
#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 $");
|
2005-03-08 12:52:00 +00:00
|
|
|
__IDSTRING(dotat, "$dotat: things/unifdef.c,v 1.171 2005/03/08 12:38:48 fanf2 Exp $");
|
2002-05-15 16:30:28 +00:00
|
|
|
#endif
|
2003-05-03 19:44:46 +00:00
|
|
|
#endif /* not lint */
|
2003-06-30 14:46:25 +00:00
|
|
|
#ifdef __FBSDID
|
2002-05-15 16:30:28 +00:00
|
|
|
__FBSDID("$FreeBSD$");
|
2003-06-30 14:46:25 +00:00
|
|
|
#endif
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* unifdef - remove ifdef'ed lines
|
|
|
|
*
|
|
|
|
* 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
|
|
|
*
|
|
|
|
* The first two items above require better buffer handling, which would
|
|
|
|
* also make it possible to handle all "dodgy" directives correctly.
|
1994-05-27 12:33:43 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <ctype.h>
|
1997-08-20 11:07:53 +00:00
|
|
|
#include <err.h>
|
2002-05-15 16:30:28 +00:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdbool.h>
|
1997-08-20 11:07:53 +00:00
|
|
|
#include <stdio.h>
|
2001-06-24 19:50:42 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2002-05-23 16:50:41 +00:00
|
|
|
#include <unistd.h>
|
1997-08-20 11:07:53 +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 */
|
|
|
|
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",
|
|
|
|
"PLAIN", "EOF"
|
2002-12-18 20:50:44 +00:00
|
|
|
};
|
2002-05-15 16:30:28 +00:00
|
|
|
|
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 */
|
|
|
|
#define MAXSYMS 4096 /* maximum number of symbols */
|
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
|
|
|
|
|
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
|
|
|
*/
|
|
|
|
|
2002-12-18 20:50:44 +00:00
|
|
|
static bool complement; /* -c: do the complement */
|
|
|
|
static bool debugging; /* -d: debugging reports */
|
2003-01-20 12:41:41 +00:00
|
|
|
static bool iocccok; /* -e: fewer IOCCC errors */
|
2002-12-18 20:50:44 +00:00
|
|
|
static bool killconsts; /* -k: eval constant #ifs */
|
|
|
|
static bool lnblank; /* -l: blank deleted lines */
|
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 */
|
|
|
|
static bool text; /* -t: this is a text file */
|
|
|
|
|
|
|
|
static const char *symname[MAXSYMS]; /* symbol name */
|
|
|
|
static const char *value[MAXSYMS]; /* -Dsym=value */
|
|
|
|
static bool ignore[MAXSYMS]; /* -iDsym or -iUsym */
|
|
|
|
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 */
|
|
|
|
|
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
|
|
|
|
|
|
|
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 */
|
2002-12-18 20:50:44 +00:00
|
|
|
static bool keepthis; /* don't delete constant #if */
|
|
|
|
|
|
|
|
static int exitstat; /* program exit status */
|
|
|
|
|
|
|
|
static void addsym(bool, bool, char *);
|
|
|
|
static void debug(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 *);
|
|
|
|
static int findsym(const char *);
|
|
|
|
static void flushline(bool);
|
|
|
|
static Linetype getline(void);
|
|
|
|
static Linetype ifeval(const char **);
|
2003-01-20 12:41:41 +00:00
|
|
|
static void ignoreoff(void);
|
|
|
|
static void ignoreon(void);
|
|
|
|
static void keywordedit(const char *);
|
2002-12-18 20:50:44 +00:00
|
|
|
static void nest(void);
|
|
|
|
static void process(void);
|
|
|
|
static const char *skipcomment(const char *);
|
|
|
|
static const char *skipsym(const char *);
|
|
|
|
static void state(Ifstate);
|
|
|
|
static int strlcmp(const char *, const char *, size_t);
|
2005-03-08 12:52:00 +00:00
|
|
|
static void unnest(void);
|
2002-12-18 20:50:44 +00:00
|
|
|
static void usage(void);
|
2002-05-15 16:30:28 +00:00
|
|
|
|
|
|
|
#define endsym(c) (!isalpha((unsigned char)c) && !isdigit((unsigned char)c) && c != '_')
|
1997-08-20 11:07:53 +00:00
|
|
|
|
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;
|
|
|
|
|
2005-03-08 12:52:00 +00:00
|
|
|
while ((opt = getopt(argc, argv, "i:D:U:I:cdeklnst")) != -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')
|
|
|
|
addsym(true, true, optarg);
|
|
|
|
else if (opt == 'U')
|
|
|
|
addsym(true, false, optarg);
|
|
|
|
else
|
|
|
|
usage();
|
2002-05-15 16:30:28 +00:00
|
|
|
break;
|
2002-05-23 16:50:41 +00:00
|
|
|
case 'D': /* define a symbol */
|
|
|
|
addsym(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 */
|
|
|
|
addsym(false, false, optarg);
|
|
|
|
break;
|
|
|
|
case 'I':
|
2002-12-18 20:50:44 +00:00
|
|
|
/* no-op for compatibility with cpp */
|
2002-05-23 16:50:41 +00:00
|
|
|
break;
|
|
|
|
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;
|
2002-12-18 20:50:44 +00:00
|
|
|
case 'k': /* process constant #ifs */
|
|
|
|
killconsts = true;
|
|
|
|
break;
|
2002-05-23 16:50:41 +00:00
|
|
|
case 'l': /* blank deleted lines instead of omitting them */
|
2002-05-15 16:30:28 +00:00
|
|
|
lnblank = true;
|
2002-05-23 16:50:41 +00:00
|
|
|
break;
|
2005-03-08 12:52:00 +00:00
|
|
|
case 'n': /* add #line directive after deleted lines */
|
|
|
|
lnnum = true;
|
|
|
|
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;
|
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;
|
|
|
|
default:
|
2002-05-15 16:30:28 +00:00
|
|
|
usage();
|
|
|
|
}
|
2002-05-23 16:50:41 +00:00
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
2002-05-15 16:30:28 +00:00
|
|
|
if (argc > 1) {
|
|
|
|
errx(2, "can only do one file");
|
2002-05-23 16:50:41 +00:00
|
|
|
} else if (argc == 1 && strcmp(*argv, "-") != 0) {
|
|
|
|
filename = *argv;
|
2003-07-01 15:30:43 +00:00
|
|
|
input = fopen(filename, "r");
|
|
|
|
if (input == NULL)
|
|
|
|
err(2, "can't open %s", filename);
|
1994-05-27 12:33:43 +00:00
|
|
|
} else {
|
2002-05-15 16:30:28 +00:00
|
|
|
filename = "[stdin]";
|
|
|
|
input = stdin;
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
2003-07-01 15:30:43 +00:00
|
|
|
process();
|
|
|
|
abort(); /* bug */
|
1994-05-27 12:33:43 +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
|
|
|
{
|
2005-05-21 09:55:10 +00:00
|
|
|
fprintf(stderr, "usage: unifdef [-cdeklnst] [-Ipath]"
|
2003-06-30 14:46:25 +00:00
|
|
|
" [-Dsym[=val]] [-Usym] [-iDsym[=val]] [-iUsym] ... [file]\n");
|
2003-01-20 12:41:41 +00:00
|
|
|
exit(2);
|
1997-08-20 11:07:53 +00:00
|
|
|
}
|
|
|
|
|
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
|
2002-12-18 20:50:44 +00:00
|
|
|
* latter into a #if to keep the nesting correct.
|
|
|
|
*
|
|
|
|
* 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(); }
|
|
|
|
/* 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); }
|
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,
|
2003-07-01 15:30:43 +00:00
|
|
|
print, done },
|
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,
|
|
|
|
drop, Eeof },
|
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,
|
|
|
|
print, Eeof },
|
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,
|
|
|
|
print, Eeof },
|
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,
|
|
|
|
drop, Eeof },
|
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,
|
|
|
|
print, Eeof },
|
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,
|
|
|
|
print, Eeof },
|
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,
|
|
|
|
drop, Eeof },
|
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,
|
|
|
|
print, Eeof },
|
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,
|
|
|
|
drop, Eeof }
|
|
|
|
/*TRUEI FALSEI IF TRUE FALSE ELIF ELTRUE ELFALSE ELSE ENDIF
|
|
|
|
TRUEI FALSEI IF TRUE FALSE ELIF ELTRUE ELFALSE ELSE ENDIF (DODGY)
|
|
|
|
PLAIN EOF */
|
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-07-01 15:30:43 +00:00
|
|
|
done(void)
|
|
|
|
{
|
|
|
|
if (incomment)
|
|
|
|
error("EOF in comment");
|
|
|
|
exit(exitstat);
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
strlcpy(keyword, replacement, tline + sizeof(tline) - keyword);
|
|
|
|
print();
|
|
|
|
}
|
|
|
|
static void
|
2002-12-18 20:50:44 +00:00
|
|
|
nest(void)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2002-12-18 20:50:44 +00:00
|
|
|
depth += 1;
|
|
|
|
if (depth >= MAXDEPTH)
|
|
|
|
error("Too many levels of nesting");
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Write a line to the output or not, according to command line options.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
flushline(bool keep)
|
|
|
|
{
|
|
|
|
if (symlist)
|
|
|
|
return;
|
2005-03-08 12:52:00 +00:00
|
|
|
if (keep ^ complement) {
|
|
|
|
if (lnnum && delcount > 0)
|
|
|
|
printf("#line %d\n", linenum);
|
2002-12-18 20:50:44 +00:00
|
|
|
fputs(tline, stdout);
|
2005-03-08 12:52:00 +00:00
|
|
|
delcount = 0;
|
|
|
|
} else {
|
2002-12-18 20:50:44 +00:00
|
|
|
if (lnblank)
|
|
|
|
putc('\n', stdout);
|
|
|
|
exitstat = 1;
|
2005-03-08 12:52:00 +00:00
|
|
|
delcount += 1;
|
2002-05-15 16:30:28 +00:00
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
2002-05-15 16:30:28 +00:00
|
|
|
/*
|
2002-12-18 20:50:44 +00:00
|
|
|
* The driver for the state machine.
|
2002-05-15 16:30:28 +00:00
|
|
|
*/
|
2002-12-18 20:50:44 +00:00
|
|
|
static void
|
|
|
|
process(void)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2002-05-15 16:30:28 +00:00
|
|
|
Linetype lineval;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
linenum++;
|
2002-12-18 20:50:44 +00:00
|
|
|
lineval = getline();
|
2003-07-01 15:30:43 +00:00
|
|
|
trans_table[ifstate[depth]][lineval]();
|
2002-12-18 20:50:44 +00:00
|
|
|
debug("process %s -> %s depth %d",
|
|
|
|
linetype_name[lineval],
|
|
|
|
ifstate_name[ifstate[depth]], depth);
|
1994-05-27 12:33:43 +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
|
|
|
|
getline(void)
|
2002-05-15 16:30:28 +00:00
|
|
|
{
|
|
|
|
const char *cp;
|
2002-12-18 20:50:44 +00:00
|
|
|
int cursym;
|
|
|
|
int kwlen;
|
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
|
|
|
|
2002-12-18 20:50:44 +00:00
|
|
|
if (fgets(tline, MAXLINE, input) == NULL)
|
2003-01-17 19:12:02 +00:00
|
|
|
return (LT_EOF);
|
2002-05-15 16:30:28 +00:00
|
|
|
retval = LT_PLAIN;
|
2002-12-18 20:50:44 +00:00
|
|
|
wascomment = incomment;
|
2002-05-15 16:30:28 +00:00
|
|
|
cp = skipcomment(tline);
|
2002-12-18 20:50:44 +00:00
|
|
|
if (linestate == LS_START) {
|
|
|
|
if (*cp == '#') {
|
|
|
|
linestate = LS_HASH;
|
|
|
|
cp = skipcomment(cp + 1);
|
|
|
|
} else if (*cp != '\0')
|
|
|
|
linestate = LS_DIRTY;
|
2002-05-15 16:30:28 +00:00
|
|
|
}
|
2002-12-18 20:50:44 +00:00
|
|
|
if (!incomment && linestate == LS_HASH) {
|
|
|
|
keyword = tline + (cp - tline);
|
|
|
|
cp = skipsym(cp);
|
|
|
|
kwlen = cp - keyword;
|
2003-01-20 12:41:41 +00:00
|
|
|
/* no way can we deal with a continuation inside a keyword */
|
2002-12-18 20:50:44 +00:00
|
|
|
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;
|
|
|
|
else {
|
|
|
|
linestate = LS_DIRTY;
|
2002-05-15 16:30:28 +00:00
|
|
|
retval = LT_PLAIN;
|
|
|
|
}
|
|
|
|
cp = skipcomment(cp);
|
2002-12-18 20:50:44 +00:00
|
|
|
if (*cp != '\0') {
|
|
|
|
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;
|
2002-05-15 16:30:28 +00:00
|
|
|
}
|
2003-01-20 12:41:41 +00:00
|
|
|
if (retval != LT_PLAIN && (wascomment || incomment)) {
|
|
|
|
retval += LT_DODGY;
|
|
|
|
if (incomment)
|
|
|
|
linestate = LS_DIRTY;
|
|
|
|
}
|
|
|
|
/* skipcomment should have changed the state */
|
2002-12-18 20:50:44 +00:00
|
|
|
if (linestate == LS_HASH)
|
|
|
|
abort(); /* bug */
|
|
|
|
}
|
|
|
|
if (linestate == LS_DIRTY) {
|
|
|
|
while (*cp != '\0')
|
|
|
|
cp = skipcomment(cp + 1);
|
|
|
|
}
|
|
|
|
debug("parser %s comment %s line",
|
|
|
|
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
|
|
|
|
* evaluator. Note that if support for division is added then we also
|
|
|
|
* need short-circuiting booleans because of divide-by-zero.
|
2002-05-15 16:30:28 +00:00
|
|
|
*/
|
2003-01-17 19:12:02 +00:00
|
|
|
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); }
|
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
|
|
|
|
* the expression is zero, LT_TRUE if it is non-zero, or LT_IF if the
|
|
|
|
* expression could not be evaluated.
|
2002-05-15 16:30:28 +00:00
|
|
|
*/
|
2002-12-18 20:50:44 +00:00
|
|
|
struct ops;
|
|
|
|
|
|
|
|
typedef Linetype eval_fn(const struct ops *, int *, const char **);
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
static const struct ops {
|
|
|
|
eval_fn *inner;
|
|
|
|
struct op {
|
|
|
|
const char *str;
|
|
|
|
int (*fn)(int, int);
|
|
|
|
} op[5];
|
|
|
|
} eval_ops[] = {
|
|
|
|
{ eval_table, { { "||", op_or } } },
|
|
|
|
{ eval_table, { { "&&", op_and } } },
|
|
|
|
{ eval_table, { { "==", op_eq },
|
|
|
|
{ "!=", op_ne } } },
|
|
|
|
{ eval_unary, { { "<=", op_le },
|
|
|
|
{ ">=", op_ge },
|
|
|
|
{ "<", op_lt },
|
|
|
|
{ ">", op_gt } } }
|
|
|
|
};
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2002-05-15 16:30:28 +00:00
|
|
|
/*
|
|
|
|
* Function for evaluating the innermost parts of expressions,
|
|
|
|
* viz. !expr (expr) defined(symbol) symbol number
|
2002-09-24 19:27:44 +00:00
|
|
|
* We reset the keepthis flag when we find a non-constant subexpression.
|
2002-05-15 16:30:28 +00:00
|
|
|
*/
|
2002-12-18 20:50:44 +00:00
|
|
|
static Linetype
|
|
|
|
eval_unary(const struct ops *ops, int *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;
|
|
|
|
int sym;
|
|
|
|
|
|
|
|
cp = skipcomment(*cpp);
|
2003-01-20 12:41:41 +00:00
|
|
|
if (*cp == '!') {
|
2002-05-15 16:30:28 +00:00
|
|
|
debug("eval%d !", ops - eval_ops);
|
1994-05-27 12:33:43 +00:00
|
|
|
cp++;
|
2002-05-15 16:30:28 +00:00
|
|
|
if (eval_unary(ops, valp, &cp) == LT_IF)
|
2003-01-17 19:12:02 +00:00
|
|
|
return (LT_IF);
|
2002-05-15 16:30:28 +00:00
|
|
|
*valp = !*valp;
|
|
|
|
} else if (*cp == '(') {
|
|
|
|
cp++;
|
|
|
|
debug("eval%d (", ops - eval_ops);
|
|
|
|
if (eval_table(eval_ops, valp, &cp) == LT_IF)
|
2003-01-17 19:12:02 +00:00
|
|
|
return (LT_IF);
|
2002-05-15 16:30:28 +00:00
|
|
|
cp = skipcomment(cp);
|
|
|
|
if (*cp++ != ')')
|
2003-01-17 19:12:02 +00:00
|
|
|
return (LT_IF);
|
2002-05-15 16:30:28 +00:00
|
|
|
} else if (isdigit((unsigned char)*cp)) {
|
|
|
|
debug("eval%d number", ops - eval_ops);
|
|
|
|
*valp = strtol(cp, &ep, 0);
|
|
|
|
cp = skipsym(cp);
|
|
|
|
} else if (strncmp(cp, "defined", 7) == 0 && endsym(cp[7])) {
|
|
|
|
cp = skipcomment(cp+7);
|
|
|
|
debug("eval%d defined", ops - eval_ops);
|
|
|
|
if (*cp++ != '(')
|
2003-01-17 19:12:02 +00:00
|
|
|
return (LT_IF);
|
2002-05-15 16:30:28 +00:00
|
|
|
cp = skipcomment(cp);
|
|
|
|
sym = findsym(cp);
|
2005-03-08 12:52:00 +00:00
|
|
|
if (sym < 0)
|
2003-01-17 19:12:02 +00:00
|
|
|
return (LT_IF);
|
2002-05-15 16:30:28 +00:00
|
|
|
*valp = (value[sym] != NULL);
|
|
|
|
cp = skipsym(cp);
|
|
|
|
cp = skipcomment(cp);
|
|
|
|
if (*cp++ != ')')
|
2003-01-17 19:12:02 +00:00
|
|
|
return (LT_IF);
|
2002-09-24 19:27:44 +00:00
|
|
|
keepthis = false;
|
2002-05-15 16:30:28 +00:00
|
|
|
} else if (!endsym(*cp)) {
|
|
|
|
debug("eval%d symbol", ops - eval_ops);
|
|
|
|
sym = findsym(cp);
|
2005-03-08 12:52:00 +00:00
|
|
|
if (sym < 0)
|
2003-01-17 19:12:02 +00:00
|
|
|
return (LT_IF);
|
2002-05-15 16:30:28 +00:00
|
|
|
if (value[sym] == NULL)
|
|
|
|
*valp = 0;
|
|
|
|
else {
|
|
|
|
*valp = strtol(value[sym], &ep, 0);
|
|
|
|
if (*ep != '\0' || ep == value[sym])
|
2003-01-17 19:12:02 +00:00
|
|
|
return (LT_IF);
|
2002-05-15 16:30:28 +00:00
|
|
|
}
|
|
|
|
cp = skipsym(cp);
|
2002-09-24 19:27:44 +00:00
|
|
|
keepthis = false;
|
2003-07-01 15:30:43 +00:00
|
|
|
} else {
|
|
|
|
debug("eval%d bad expr", ops - eval_ops);
|
2003-01-17 19:12:02 +00:00
|
|
|
return (LT_IF);
|
2003-07-01 15:30:43 +00:00
|
|
|
}
|
2002-05-15 16:30:28 +00:00
|
|
|
|
|
|
|
*cpp = cp;
|
|
|
|
debug("eval%d = %d", ops - eval_ops, *valp);
|
2003-01-17 19:12:02 +00:00
|
|
|
return (*valp ? LT_TRUE : LT_FALSE);
|
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
|
|
|
|
eval_table(const struct ops *ops, int *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;
|
|
|
|
int val;
|
|
|
|
|
|
|
|
debug("eval%d", ops - eval_ops);
|
|
|
|
cp = *cpp;
|
|
|
|
if (ops->inner(ops+1, valp, &cp) == LT_IF)
|
2003-01-17 19:12:02 +00:00
|
|
|
return (LT_IF);
|
1994-05-27 12:33:43 +00:00
|
|
|
for (;;) {
|
2002-05-15 16:30:28 +00:00
|
|
|
cp = skipcomment(cp);
|
|
|
|
for (op = ops->op; op->str != NULL; op++)
|
|
|
|
if (strncmp(cp, op->str, strlen(op->str)) == 0)
|
|
|
|
break;
|
|
|
|
if (op->str == NULL)
|
|
|
|
break;
|
|
|
|
cp += strlen(op->str);
|
|
|
|
debug("eval%d %s", ops - eval_ops, op->str);
|
|
|
|
if (ops->inner(ops+1, &val, &cp) == LT_IF)
|
2003-01-17 19:12:02 +00:00
|
|
|
return (LT_IF);
|
2002-05-15 16:30:28 +00:00
|
|
|
*valp = op->fn(*valp, val);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
2002-05-15 16:30:28 +00:00
|
|
|
|
|
|
|
*cpp = cp;
|
|
|
|
debug("eval%d = %d", ops - eval_ops, *valp);
|
2003-01-17 19:12:02 +00:00
|
|
|
return (*valp ? LT_TRUE : LT_FALSE);
|
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)
|
|
|
|
{
|
2002-12-18 20:50:44 +00:00
|
|
|
int ret;
|
2002-05-15 16:30:28 +00:00
|
|
|
int val;
|
2002-12-18 20:50:44 +00:00
|
|
|
|
2002-05-15 16:30:28 +00:00
|
|
|
debug("eval %s", *cpp);
|
2002-09-24 19:27:44 +00:00
|
|
|
keepthis = 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);
|
2003-01-17 19:12:02 +00:00
|
|
|
return (keepthis ? LT_IF : ret);
|
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 */
|
2002-12-18 20:50:44 +00:00
|
|
|
if (strncmp(cp, "\\\n", 2) == 0)
|
|
|
|
cp += 2;
|
|
|
|
else switch (incomment) {
|
|
|
|
case NO_COMMENT:
|
|
|
|
if (strncmp(cp, "/\\\n", 3) == 0) {
|
|
|
|
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;
|
|
|
|
} else if (strchr(" \t", *cp) != NULL) {
|
|
|
|
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)
|
|
|
|
error("unterminated char literal");
|
|
|
|
else
|
|
|
|
error("unterminated string literal");
|
|
|
|
} else
|
|
|
|
cp += 1;
|
|
|
|
continue;
|
2002-12-18 20:50:44 +00:00
|
|
|
case C_COMMENT:
|
|
|
|
if (strncmp(cp, "*\\\n", 3) == 0) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Look for the symbol in the symbol table. If is 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
|
|
|
*/
|
2002-12-18 20:50:44 +00:00
|
|
|
static int
|
2002-05-15 16:30:28 +00:00
|
|
|
findsym(const char *str)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2002-05-15 16:30:28 +00:00
|
|
|
const char *cp;
|
|
|
|
int symind;
|
|
|
|
|
2002-12-18 20:50:44 +00:00
|
|
|
cp = skipsym(str);
|
|
|
|
if (cp == str)
|
2003-01-17 19:12:02 +00:00
|
|
|
return (-1);
|
2005-03-08 12:52:00 +00:00
|
|
|
if (symlist) {
|
2003-01-17 19:10:18 +00:00
|
|
|
printf("%.*s\n", (int)(cp-str), str);
|
2005-03-08 12:52:00 +00:00
|
|
|
/* we don't care about the value of the symbol */
|
|
|
|
return (0);
|
|
|
|
}
|
2002-12-18 20:50:44 +00:00
|
|
|
for (symind = 0; symind < nsyms; ++symind) {
|
|
|
|
if (strlcmp(symname[symind], str, cp-str) == 0) {
|
2002-05-15 16:30:28 +00:00
|
|
|
debug("findsym %s %s", symname[symind],
|
|
|
|
value[symind] ? value[symind] : "");
|
2003-01-17 19:12:02 +00:00
|
|
|
return (symind);
|
2002-05-15 16:30:28 +00:00
|
|
|
}
|
|
|
|
}
|
2003-01-17 19:12:02 +00:00
|
|
|
return (-1);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
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
|
2002-05-23 16:50:41 +00:00
|
|
|
addsym(bool ignorethis, bool definethis, char *sym)
|
|
|
|
{
|
|
|
|
int symind;
|
|
|
|
char *val;
|
|
|
|
|
|
|
|
symind = findsym(sym);
|
2002-12-18 20:50:44 +00:00
|
|
|
if (symind < 0) {
|
2002-05-23 16:50:41 +00:00
|
|
|
if (nsyms >= MAXSYMS)
|
|
|
|
errx(2, "too many symbols");
|
|
|
|
symind = nsyms++;
|
|
|
|
}
|
|
|
|
symname[symind] = sym;
|
|
|
|
ignore[symind] = ignorethis;
|
2002-12-18 20:50:44 +00:00
|
|
|
val = sym + (skipsym(sym) - sym);
|
2002-05-23 16:50:41 +00:00
|
|
|
if (definethis) {
|
|
|
|
if (*val == '=') {
|
|
|
|
value[symind] = val+1;
|
|
|
|
*val = '\0';
|
|
|
|
} else if (*val == '\0')
|
|
|
|
value[symind] = "";
|
|
|
|
else
|
|
|
|
usage();
|
|
|
|
} else {
|
|
|
|
if (*val != '\0')
|
|
|
|
usage();
|
|
|
|
value[symind] = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
/*
|
2002-12-18 20:50:44 +00:00
|
|
|
* Compare s with n characters of t.
|
|
|
|
* The same as strncmp() except that it checks that s[n] == '\0'.
|
1994-05-27 12:33:43 +00:00
|
|
|
*/
|
2002-12-18 20:50:44 +00:00
|
|
|
static int
|
|
|
|
strlcmp(const char *s, const char *t, size_t n)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2002-12-18 20:50:44 +00:00
|
|
|
while (n-- && *t != '\0')
|
|
|
|
if (*s != *t)
|
2003-01-17 19:12:02 +00:00
|
|
|
return ((unsigned char)*s - (unsigned char)*t);
|
2002-12-18 20:50:44 +00:00
|
|
|
else
|
|
|
|
++s, ++t;
|
2003-01-17 19:12:02 +00:00
|
|
|
return ((unsigned char)*s);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
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);
|
2003-01-20 12:41:41 +00:00
|
|
|
errx(2, "output may be truncated");
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|