/*
* Copyright (c) 2002 - 2009 Tony Finch <dot@dotat.at>
*
* 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.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* 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.
*/
/*
* This code was derived from software contributed to Berkeley by Dave Yost.
* It was rewritten to support ANSI C by Tony Finch. The original version
* of unifdef carried the 4-clause BSD copyright licence. None of its code
* remains in this version (though some of the names remain) so it now
* carries a more liberal licence.
*
* The latest version is available from http://dotat.at/prog/unifdef
*/
static const char * const copyright[] = {
"@(#) Copyright (c) 2002 - 2009 Tony Finch <dot@dotat.at>\n",
"$dotat: unifdef/unifdef.c,v 1.190 2009/11/27 17:21:26 fanf2 Exp $",
};
/*
* 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
*
* The first two items above require better buffer handling, which would
* also make it possible to handle all "dodgy" directives correctly.
*/
#include <ctype.h>
#include <err.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
/* types of input lines: */
typedef enum {
LT_TRUEI, /* a true #if with ignore flag */
LT_FALSEI, /* a false #if with ignore flag */
LT_IF, /* an unknown #if */
LT_TRUE, /* a true #if */
LT_FALSE, /* a false #if */
LT_ELIF, /* an unknown #elif */
LT_ELTRUE, /* a true #elif */
LT_ELFALSE, /* a false #elif */
LT_ELSE, /* #else */
LT_ENDIF, /* #endif */
LT_DODGY, /* flag: directive is not on one line */
LT_DODGY_LAST = LT_DODGY + LT_ENDIF,
LT_PLAIN, /* ordinary line */
LT_EOF, /* end of file */
LT_ERROR, /* unevaluable #if */
LT_COUNT
} Linetype;
static char const * const linetype_name[] = {
"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", "ERROR"
};
/* state of #if processing */
typedef enum {
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",
"PASS_MIDDLE", "FALSE_MIDDLE", "TRUE_MIDDLE",
"PASS_ELSE", "FALSE_ELSE", "TRUE_ELSE",
"FALSE_TRAILER"
};
/* state of comment parser */
typedef enum {
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 */
FINISHING_COMMENT, /* star-backslash-newline in a C comment */
CHAR_LITERAL, /* inside '' */
STRING_LITERAL /* inside "" */
} Comment_state;
static char const * const comment_name[] = {
"NO", "C", "CXX", "STARTING", "FINISHING", "CHAR", "STRING"
};
/* 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;
static char const * const linestate_name[] = {
"START", "HASH", "DIRTY"
};
/*
* Minimum translation limits from ISO/IEC 9899:1999 5.2.4.1
*/
#define MAXDEPTH 64 /* maximum #if nesting */
#define MAXLINE 4096 /* maximum length of line */
#define MAXSYMS 4096 /* maximum number of symbols */
/*
* 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
/*
* Globals.
*/
static bool compblank; /* -B: compress blank lines */
static bool lnblank; /* -b: blank deleted lines */
static bool complement; /* -c: do the complement */
static bool debugging; /* -d: debugging reports */
static bool