Quantcast
Channel: Icepack-linux and Neck Pads » linux scripts code
Viewing all articles
Browse latest Browse all 8

linux kernel code scripts unifdef.c 1

$
0
0

/*
* Copyright (c) 2002 – 2011 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.
*/

/*
* unifdef – remove ifdef’ed lines
*
* This code was derived from software contributed to Berkeley by Dave Yost.
* It was rewritten to support ANSI C by Tony Finch. The original version
* of unifdef carried the 4-clause BSD copyright licence. None of its code
* remains in this version (though some of the names remain) so it now
* carries a more liberal licence.
*
*  Wishlist:
*      provide an option which will append the name of the
*        appropriate symbol after #else’s and #endif’s
*      provide an option which will check symbols after
*        #else’s and #endif’s to see that they match their
*        corresponding #ifdef or #ifndef
*
*   These require better buffer handling, which would also make
*   it possible to handle all “dodgy” directives correctly.
*/

#include <sys/types.h>
#include <sys/stat.h>

#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

const char copyright[] =
“@(#) $Version: unifdef-2.5 $\n”
“@(#) $Author: Tony Finch (dot@dotat.at) $\n”
“@(#) $URL: http://dotat.at/prog/unifdef $\n”
;

/* 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

/*
* For temporary filenames
*/
#define TEMPLATE        “unifdef.XXXXXX”

/*
* 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             iocccok;        /* -e: fewer IOCCC errors */
static bool             strictlogic;        /* -K: keep ambiguous #ifs */
static bool             killconsts;        /* -k: eval constant #ifs */
static bool             lnnum;            /* -n: add #line directives */
static bool             symlist;        /* -s: output symbol list */
static bool             symdepth;        /* -S: output symbol depth */
static bool             text;            /* -t: this is a text file */

static const char      *symname[MAXSYMS];    /* symbol name */
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 */
static FILE            *output;            /* output file pointer */
static const char      *ofilename;        /* output file name */
static bool             overwriting;        /* output overwrites input */
static char             tempname[FILENAME_MAX];    /* used when overwriting */

static char             tline[MAXLINE+EDITSLOP];/* input buffer plus space */
static char            *keyword;        /* used for editing #elif’s */

static const char      *newline;        /* input file format */
static const char       newline_unix[] = “\n”;
static const char       newline_crlf[] = “\r\n”;

static Comment_state    incomment;        /* comment parser state */
static Line_state       linestate;        /* #if line parser state */
static Ifstate          ifstate[MAXDEPTH];    /* #if processor state */
static bool             ignoring[MAXDEPTH];    /* ignore comments state */
static int              stifline[MAXDEPTH];    /* start of current #if */
static int              depth;            /* current #if nesting */
static int              delcount;        /* count of deleted lines */
static unsigned         blankcount;        /* count of blank lines */
static unsigned         blankmax;        /* maximum recent blankcount */
static bool             constexpr;        /* constant #if expression */
static bool             zerosyms = true;    /* to format symdepth output */
static bool             firstsym;        /* ditto */

static int              exitstat;        /* program exit status */

static void             addsym(bool, bool, char *);
static void             closeout(void);
static void             debug(const char *, …);
static void             done(void);
static void             error(const char *);
static int              findsym(const char *);
static void             flushline(bool);
static Linetype         parseline(void);
static Linetype         ifeval(const char **);
static void             ignoreoff(void);
static void             ignoreon(void);
static void             keywordedit(const char *);
static void             nest(void);
static void             process(void);
static const char      *skipargs(const char *);
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);
static void             unnest(void);
static void             usage(void);
static void             version(void);

#define endsym(c) (!isalnum((unsigned char)c) && c != ‘_’)


Viewing all articles
Browse latest Browse all 8

Trending Articles