diff options
| author | Tony Finch <dot@dotat.at> | 2011-01-18 15:12:49 -0500 |
|---|---|---|
| committer | Michal Marek <mmarek@suse.cz> | 2011-01-22 09:50:59 -0500 |
| commit | 3cbea4366f17dcb22f3bf5f253eeb86b622d24d0 (patch) | |
| tree | 8c0801567380899d7b2fe30cef39b1cf6a7cc32a /scripts | |
| parent | c56eb8fb6dccb83d9fe62fd4dc00c834de9bc470 (diff) | |
unifdef: update to upstream version 2.5
Fix a long-standing cpp compatibility bug. The -DFOO argument
(without an explicit value) should define FOO to 1 not to the empty
string.
Add a -o option to support overwriting a file in place, and a -S
option to list the nesting depth of symbols. Include line numbers
in debugging output. Support CRLF newlines.
Signed-off-by: Tony Finch <dot@dotat.at>
Signed-off-by: Michal Marek <mmarek@suse.cz>
Diffstat (limited to 'scripts')
| -rw-r--r-- | scripts/unifdef.c | 247 |
1 files changed, 184 insertions, 63 deletions
diff --git a/scripts/unifdef.c b/scripts/unifdef.c index 44d39785e50d..7493c0ee51cc 100644 --- a/scripts/unifdef.c +++ b/scripts/unifdef.c | |||
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | * Copyright (c) 2002 - 2009 Tony Finch <dot@dotat.at> | 2 | * Copyright (c) 2002 - 2011 Tony Finch <dot@dotat.at> |
| 3 | * | 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without | 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions | 5 | * modification, are permitted provided that the following conditions |
| @@ -24,23 +24,14 @@ | |||
| 24 | */ | 24 | */ |
| 25 | 25 | ||
| 26 | /* | 26 | /* |
| 27 | * unifdef - remove ifdef'ed lines | ||
| 28 | * | ||
| 27 | * This code was derived from software contributed to Berkeley by Dave Yost. | 29 | * This code was derived from software contributed to Berkeley by Dave Yost. |
| 28 | * It was rewritten to support ANSI C by Tony Finch. The original version | 30 | * It was rewritten to support ANSI C by Tony Finch. The original version |
| 29 | * of unifdef carried the 4-clause BSD copyright licence. None of its code | 31 | * of unifdef carried the 4-clause BSD copyright licence. None of its code |
| 30 | * remains in this version (though some of the names remain) so it now | 32 | * remains in this version (though some of the names remain) so it now |
| 31 | * carries a more liberal licence. | 33 | * carries a more liberal licence. |
| 32 | * | 34 | * |
| 33 | * The latest version is available from http://dotat.at/prog/unifdef | ||
| 34 | */ | ||
| 35 | |||
| 36 | static const char * const copyright[] = { | ||
| 37 | "@(#) Copyright (c) 2002 - 2009 Tony Finch <dot@dotat.at>\n", | ||
| 38 | "$dotat: unifdef/unifdef.c,v 1.190 2009/11/27 17:21:26 fanf2 Exp $", | ||
| 39 | }; | ||
| 40 | |||
| 41 | /* | ||
| 42 | * unifdef - remove ifdef'ed lines | ||
| 43 | * | ||
| 44 | * Wishlist: | 35 | * Wishlist: |
| 45 | * provide an option which will append the name of the | 36 | * provide an option which will append the name of the |
| 46 | * appropriate symbol after #else's and #endif's | 37 | * appropriate symbol after #else's and #endif's |
| @@ -48,12 +39,16 @@ static const char * const copyright[] = { | |||
| 48 | * #else's and #endif's to see that they match their | 39 | * #else's and #endif's to see that they match their |
| 49 | * corresponding #ifdef or #ifndef | 40 | * corresponding #ifdef or #ifndef |
| 50 | * | 41 | * |
| 51 | * The first two items above require better buffer handling, which would | 42 | * These require better buffer handling, which would also make |
| 52 | * also make it possible to handle all "dodgy" directives correctly. | 43 | * it possible to handle all "dodgy" directives correctly. |
| 53 | */ | 44 | */ |
| 54 | 45 | ||
| 46 | #include <sys/types.h> | ||
| 47 | #include <sys/stat.h> | ||
| 48 | |||
| 55 | #include <ctype.h> | 49 | #include <ctype.h> |
| 56 | #include <err.h> | 50 | #include <err.h> |
| 51 | #include <errno.h> | ||
| 57 | #include <stdarg.h> | 52 | #include <stdarg.h> |
| 58 | #include <stdbool.h> | 53 | #include <stdbool.h> |
| 59 | #include <stdio.h> | 54 | #include <stdio.h> |
| @@ -61,6 +56,12 @@ static const char * const copyright[] = { | |||
| 61 | #include <string.h> | 56 | #include <string.h> |
| 62 | #include <unistd.h> | 57 | #include <unistd.h> |
| 63 | 58 | ||
| 59 | const char copyright[] = | ||
| 60 | "@(#) $Version: unifdef-2.5 $\n" | ||
| 61 | "@(#) $Author: Tony Finch (dot@dotat.at) $\n" | ||
| 62 | "@(#) $URL: http://dotat.at/prog/unifdef $\n" | ||
| 63 | ; | ||
| 64 | |||
| 64 | /* types of input lines: */ | 65 | /* types of input lines: */ |
| 65 | typedef enum { | 66 | typedef enum { |
| 66 | LT_TRUEI, /* a true #if with ignore flag */ | 67 | LT_TRUEI, /* a true #if with ignore flag */ |
| @@ -153,6 +154,11 @@ static char const * const linestate_name[] = { | |||
| 153 | #define EDITSLOP 10 | 154 | #define EDITSLOP 10 |
| 154 | 155 | ||
| 155 | /* | 156 | /* |
| 157 | * For temporary filenames | ||
| 158 | */ | ||
| 159 | #define TEMPLATE "unifdef.XXXXXX" | ||
| 160 | |||
| 161 | /* | ||
| 156 | * Globals. | 162 | * Globals. |
| 157 | */ | 163 | */ |
| 158 | 164 | ||
| @@ -165,6 +171,7 @@ static bool strictlogic; /* -K: keep ambiguous #ifs */ | |||
| 165 | static bool killconsts; /* -k: eval constant #ifs */ | 171 | static bool killconsts; /* -k: eval constant #ifs */ |
| 166 | static bool lnnum; /* -n: add #line directives */ | 172 | static bool lnnum; /* -n: add #line directives */ |
| 167 | static bool symlist; /* -s: output symbol list */ | 173 | static bool symlist; /* -s: output symbol list */ |
| 174 | static bool symdepth; /* -S: output symbol depth */ | ||
| 168 | static bool text; /* -t: this is a text file */ | 175 | static bool text; /* -t: this is a text file */ |
| 169 | 176 | ||
| 170 | static const char *symname[MAXSYMS]; /* symbol name */ | 177 | static const char *symname[MAXSYMS]; /* symbol name */ |
| @@ -175,10 +182,18 @@ static int nsyms; /* number of symbols */ | |||
| 175 | static FILE *input; /* input file pointer */ | 182 | static FILE *input; /* input file pointer */ |
| 176 | static const char *filename; /* input file name */ | 183 | static const char *filename; /* input file name */ |
| 177 | static int linenum; /* current line number */ | 184 | static int linenum; /* current line number */ |
| 185 | static FILE *output; /* output file pointer */ | ||
| 186 | static const char *ofilename; /* output file name */ | ||
| 187 | static bool overwriting; /* output overwrites input */ | ||
| 188 | static char tempname[FILENAME_MAX]; /* used when overwriting */ | ||
| 178 | 189 | ||
| 179 | static char tline[MAXLINE+EDITSLOP];/* input buffer plus space */ | 190 | static char tline[MAXLINE+EDITSLOP];/* input buffer plus space */ |
| 180 | static char *keyword; /* used for editing #elif's */ | 191 | static char *keyword; /* used for editing #elif's */ |
| 181 | 192 | ||
| 193 | static const char *newline; /* input file format */ | ||
| 194 | static const char newline_unix[] = "\n"; | ||
| 195 | static const char newline_crlf[] = "\r\n"; | ||
| 196 | |||
| 182 | static Comment_state incomment; /* comment parser state */ | 197 | static Comment_state incomment; /* comment parser state */ |
| 183 | static Line_state linestate; /* #if line parser state */ | 198 | static Line_state linestate; /* #if line parser state */ |
| 184 | static Ifstate ifstate[MAXDEPTH]; /* #if processor state */ | 199 | static Ifstate ifstate[MAXDEPTH]; /* #if processor state */ |
| @@ -189,10 +204,13 @@ static int delcount; /* count of deleted lines */ | |||
| 189 | static unsigned blankcount; /* count of blank lines */ | 204 | static unsigned blankcount; /* count of blank lines */ |
| 190 | static unsigned blankmax; /* maximum recent blankcount */ | 205 | static unsigned blankmax; /* maximum recent blankcount */ |
| 191 | static bool constexpr; /* constant #if expression */ | 206 | static bool constexpr; /* constant #if expression */ |
| 207 | static bool zerosyms = true; /* to format symdepth output */ | ||
| 208 | static bool firstsym; /* ditto */ | ||
| 192 | 209 | ||
| 193 | static int exitstat; /* program exit status */ | 210 | static int exitstat; /* program exit status */ |
| 194 | 211 | ||
| 195 | static void addsym(bool, bool, char *); | 212 | static void addsym(bool, bool, char *); |
| 213 | static void closeout(void); | ||
| 196 | static void debug(const char *, ...); | 214 | static void debug(const char *, ...); |
| 197 | static void done(void); | 215 | static void done(void); |
| 198 | static void error(const char *); | 216 | static void error(const char *); |
| @@ -212,6 +230,7 @@ static void state(Ifstate); | |||
| 212 | static int strlcmp(const char *, const char *, size_t); | 230 | static int strlcmp(const char *, const char *, size_t); |
| 213 | static void unnest(void); | 231 | static void unnest(void); |
| 214 | static void usage(void); | 232 | static void usage(void); |
| 233 | static void version(void); | ||
| 215 | 234 | ||
| 216 | #define endsym(c) (!isalnum((unsigned char)c) && c != '_') | 235 | #define endsym(c) (!isalnum((unsigned char)c) && c != '_') |
| 217 | 236 | ||
| @@ -223,7 +242,7 @@ main(int argc, char *argv[]) | |||
| 223 | { | 242 | { |
| 224 | int opt; | 243 | int opt; |
| 225 | 244 | ||
| 226 | while ((opt = getopt(argc, argv, "i:D:U:I:BbcdeKklnst")) != -1) | 245 | while ((opt = getopt(argc, argv, "i:D:U:I:o:bBcdeKklnsStV")) != -1) |
| 227 | switch (opt) { | 246 | switch (opt) { |
| 228 | case 'i': /* treat stuff controlled by these symbols as text */ | 247 | case 'i': /* treat stuff controlled by these symbols as text */ |
| 229 | /* | 248 | /* |
| @@ -245,16 +264,15 @@ main(int argc, char *argv[]) | |||
| 245 | case 'U': /* undef a symbol */ | 264 | case 'U': /* undef a symbol */ |
| 246 | addsym(false, false, optarg); | 265 | addsym(false, false, optarg); |
| 247 | break; | 266 | break; |
| 248 | case 'I': | 267 | case 'I': /* no-op for compatibility with cpp */ |
| 249 | /* no-op for compatibility with cpp */ | ||
| 250 | break; | ||
| 251 | case 'B': /* compress blank lines around removed section */ | ||
| 252 | compblank = true; | ||
| 253 | break; | 268 | break; |
| 254 | case 'b': /* blank deleted lines instead of omitting them */ | 269 | case 'b': /* blank deleted lines instead of omitting them */ |
| 255 | case 'l': /* backwards compatibility */ | 270 | case 'l': /* backwards compatibility */ |
| 256 | lnblank = true; | 271 | lnblank = true; |
| 257 | break; | 272 | break; |
| 273 | case 'B': /* compress blank lines around removed section */ | ||
| 274 | compblank = true; | ||
| 275 | break; | ||
| 258 | case 'c': /* treat -D as -U and vice versa */ | 276 | case 'c': /* treat -D as -U and vice versa */ |
| 259 | complement = true; | 277 | complement = true; |
| 260 | break; | 278 | |
