From 2c49a8335a953206ce8603c5522153c59e1f7a0d Mon Sep 17 00:00:00 2001 From: Alan Coopersmith Date: Tue, 6 Dec 2022 16:42:35 -0800 Subject: Reformat code into X.Org standard coding style Make indentation and formatting much more consistent across this project Signed-off-by: Alan Coopersmith --- cppsetup.c | 60 ++- def.h | 186 ++++----- ifparser.c | 623 +++++++++++++++-------------- ifparser.h | 19 +- imakemdep.h | 414 ++++++++++--------- include.c | 491 ++++++++++++----------- main.c | 1280 ++++++++++++++++++++++++++++++----------------------------- parse.c | 1084 +++++++++++++++++++++++++------------------------- pr.c | 198 ++++----- 9 files changed, 2175 insertions(+), 2180 deletions(-) diff --git a/cppsetup.c b/cppsetup.c index 58e2a12..90d5eaa 100644 --- a/cppsetup.c +++ b/cppsetup.c @@ -36,7 +36,7 @@ struct _parse_data { }; static const char * -my_if_errors (IfParser *ip, const char *cp, const char *expecting) +my_if_errors(IfParser *ip, const char *cp, const char *expecting) { struct _parse_data *pd = (struct _parse_data *) ip->data; int lineno = pd->filep->f_line; @@ -45,17 +45,17 @@ my_if_errors (IfParser *ip, const char *cp, const char *expecting) int prefixlen; int i; - snprintf (prefix, sizeof(prefix), "\"%s\":%d", filename, lineno); + snprintf(prefix, sizeof(prefix), "\"%s\":%d", filename, lineno); prefixlen = strlen(prefix); - fprintf (stderr, "%s: %s", prefix, pd->line); + fprintf(stderr, "%s: %s", prefix, pd->line); i = cp - pd->line; - if (i > 0 && pd->line[i-1] != '\n') { - putc ('\n', stderr); + if (i > 0 && pd->line[i - 1] != '\n') { + putc('\n', stderr); } for (i += prefixlen + 3; i > 0; i--) { - putc (' ', stderr); + putc(' ', stderr); } - fprintf (stderr, "^--- expecting %s\n", expecting); + fprintf(stderr, "^--- expecting %s\n", expecting); return NULL; } @@ -63,57 +63,56 @@ my_if_errors (IfParser *ip, const char *cp, const char *expecting) #define MAXNAMELEN 256 static struct symtab ** -lookup_variable (IfParser *ip, const char *var, int len) +lookup_variable(IfParser *ip, const char *var, int len) { char tmpbuf[MAXNAMELEN + 1]; struct _parse_data *pd = (struct _parse_data *) ip->data; if (len > MAXNAMELEN) - return NULL; + return NULL; - strncpy (tmpbuf, var, len); + strncpy(tmpbuf, var, len); tmpbuf[len] = '\0'; - return isdefined (tmpbuf, pd->inc, NULL); + return isdefined(tmpbuf, pd->inc, NULL); } static int -my_eval_defined (IfParser *ip, const char *var, int len) +my_eval_defined(IfParser *ip, const char *var, int len) { - if (lookup_variable (ip, var, len)) - return 1; + if (lookup_variable(ip, var, len)) + return 1; else - return 0; + return 0; } #define isvarfirstletter(ccc) (isalpha(ccc) || (ccc) == '_') static long -my_eval_variable (IfParser *ip, const char *var, int len) +my_eval_variable(IfParser *ip, const char *var, int len) { long val; struct symtab **s; - s = lookup_variable (ip, var, len); + s = lookup_variable(ip, var, len); if (!s) - return 0; + return 0; do { - var = (*s)->s_value; - if (!isvarfirstletter(*var) || !strcmp((*s)->s_name, var)) - break; - s = lookup_variable (ip, var, strlen(var)); + var = (*s)->s_value; + if (!isvarfirstletter(*var) || !strcmp((*s)->s_name, var)) + break; + s = lookup_variable(ip, var, strlen(var)); } while (s); var = ParseIfExpression(ip, var, &val); - if (var && *var) debug(4, ("extraneous: '%s'\n", var)); + if (var && *var) + debug(4, ("extraneous: '%s'\n", var)); return val; } int -cppsetup(const char *filename, - const char *line, - struct filepointer *filep, - struct inclist *inc) +cppsetup(const char *filename, const char *line, + struct filepointer *filep, struct inclist *inc) { IfParser ip; struct _parse_data pd; @@ -128,10 +127,9 @@ cppsetup(const char *filename, ip.funcs.eval_variable = my_eval_variable; ip.data = (char *) &pd; - (void) ParseIfExpression (&ip, line, &val); + (void) ParseIfExpression(&ip, line, &val); if (val) - return IF; + return IF; else - return IFFALSE; + return IFFALSE; } - diff --git a/def.h b/def.h index 479adcc..676b395 100644 --- a/def.h +++ b/def.h @@ -40,10 +40,10 @@ in this Software without prior written authorization from The Open Group. #define MAXDEFINES 512 #define MAXFILES 2048 -#define MAXINCFILES 128 /* "-include" files */ -#define MAXDIRS 512 /* -I flags */ +#define MAXINCFILES 128 /* "-include" files */ +#define MAXDIRS 512 /* -I flags */ #define PATHMAX 4096 /* realpath */ -#define SYMTABINC 10 /* must be > 1 for define() to work right */ +#define SYMTABINC 10 /* must be > 1 for define() to work right */ #define TRUE 1 #define FALSE 0 @@ -65,15 +65,16 @@ in this Software without prior written authorization from The Open Group. #define EJECT 14 #define WARNING 15 #define INCLUDENEXT 16 -#define IFFALSE 17 /* pseudo value --- never matched */ -#define ELIFFALSE 18 /* pseudo value --- never matched */ -#define INCLUDEDOT 19 /* pseudo value --- never matched */ -#define IFGUESSFALSE 20 /* pseudo value --- never matched */ -#define ELIFGUESSFALSE 21 /* pseudo value --- never matched */ -#define INCLUDENEXTDOT 22 /* pseudo value --- never matched */ +#define IFFALSE 17 /* pseudo value --- never matched */ +#define ELIFFALSE 18 /* pseudo value --- never matched */ +#define INCLUDEDOT 19 /* pseudo value --- never matched */ +#define IFGUESSFALSE 20 /* pseudo value --- never matched */ +#define ELIFGUESSFALSE 21 /* pseudo value --- never matched */ +#define INCLUDENEXTDOT 22 /* pseudo value --- never matched */ #ifdef DEBUG -extern int _debugmask; +extern int _debugmask; + /* * debug levels are: * @@ -88,108 +89,97 @@ extern int _debugmask; #define debug(level,arg) do { /**/ } while (0) #endif /* DEBUG */ -typedef unsigned char boolean; +typedef unsigned char boolean; struct symtab { - char *s_name; - char *s_value; + char *s_name; + char *s_value; }; /* possible i_flag */ -#define DEFCHECKED (1<<0) /* whether defines have been checked */ -#define NOTIFIED (1<<1) /* whether we have revealed includes */ -#define MARKED (1<<2) /* whether it's in the makefile */ -#define SEARCHED (1<<3) /* whether we have read this */ -#define FINISHED (1<<4) /* whether we are done reading this */ -#define INCLUDED_SYM (1<<5) /* whether #include SYMBOL was found - Can't use i_list if TRUE */ -struct inclist { - char *i_incstring; /* string from #include line */ - char *i_file; /* path name of the include file */ - struct inclist **i_list; /* list of files it itself includes */ - struct symtab **i_defs; /* symbol table for this file and its - children when merged */ - int i_listlen; /* length of i_list */ - int i_ndefs; /* current # defines */ - boolean *i_merged; /* whether we have merged child - defines */ - unsigned char i_flags; +#define DEFCHECKED (1<<0) /* whether defines have been checked */ +#define NOTIFIED (1<<1) /* whether we have revealed includes */ +#define MARKED (1<<2) /* whether it's in the makefile */ +#define SEARCHED (1<<3) /* whether we have read this */ +#define FINISHED (1<<4) /* whether we are done reading this */ +#define INCLUDED_SYM (1<<5) /* whether #include SYMBOL was found + Can't use i_list if TRUE */ +struct inclist { + char *i_incstring; /* string from #include line */ + char *i_file; /* path name of the include file */ + struct inclist **i_list; /* list of files it itself includes */ + struct symtab **i_defs; /* symbol table for this file and its + children when merged */ + int i_listlen; /* length of i_list */ + int i_ndefs; /* current # defines */ + boolean *i_merged; /* whether we have merged child + defines */ + unsigned char i_flags; }; struct filepointer { - const char *f_name; - char *f_p; - char *f_base; - char *f_end; - long f_len; - long f_line; - long cmdinc_count; - char **cmdinc_list; - long cmdinc_line; + const char *f_name; + char *f_p; + char *f_base; + char *f_end; + long f_len; + long f_line; + long cmdinc_count; + char **cmdinc_list; + long cmdinc_line; }; #include -int match(const char *str, const char * const *list); -char *base_name(const char *file); -char *getnextline(struct filepointer *fp); -struct symtab **slookup(const char *symbol, struct inclist *file); -struct symtab **isdefined(const char *symbol, struct inclist *file, - struct inclist **srcfile); -struct symtab **fdefined(const char *symbol, struct inclist *file, - struct inclist **srcfile); -struct filepointer *getfile(const char *file); -void included_by(struct inclist *ip, - struct inclist *newfile); -struct inclist *newinclude(const char *newfile, - const char *incstring); -void inc_clean (void); -struct inclist *inc_path(const char *file, const char *include, - int type); - -void freefile(struct filepointer *fp); - -void define2(const char *name, const char *val, - struct inclist *file); -void define(char *def, struct inclist *file); -void undefine(const char *symbol, struct inclist *file); -int find_includes(struct filepointer *filep, - struct inclist *file, - struct inclist *file_red, - int recursion, boolean failOK); - -void recursive_pr_include(struct inclist *head, - const char *file, - const char *base); -void add_include(struct filepointer *filep, - struct inclist *file, - struct inclist *file_red, - const char *include, int type, - boolean failOK); - -int cppsetup(const char *filename, - const char *line, - struct filepointer *filep, - struct inclist *inc); - +int match(const char *str, const char *const *list); +char *base_name(const char *file); +char *getnextline(struct filepointer *fp); +struct symtab **slookup(const char *symbol, struct inclist *file); +struct symtab **isdefined(const char *symbol, struct inclist *file, + struct inclist **srcfile); +struct symtab **fdefined(const char *symbol, struct inclist *file, + struct inclist **srcfile); +struct filepointer *getfile(const char *file); +void included_by(struct inclist *ip, struct inclist *newfile); +struct inclist *newinclude(const char *newfile, const char *incstring); +void inc_clean(void); +struct inclist *inc_path(const char *file, const char *include, int type); + +void freefile(struct filepointer *fp); + +void define2(const char *name, const char *val, struct inclist *file); +void define(char *def, struct inclist *file); +void undefine(const char *symbol, struct inclist *file); +int find_includes(struct filepointer *filep, + struct inclist *file, struct inclist *file_red, + int recursion, boolean failOK); + +void recursive_pr_include(struct inclist *head, + const char *file, const char *base); +void add_include(struct filepointer *filep, + struct inclist *file, struct inclist *file_red, + const char *include, int type, boolean failOK); + +int cppsetup(const char *filename, const char *line, + struct filepointer *filep, struct inclist *inc); extern void fatalerr(const char *, ...) _X_ATTRIBUTE_PRINTF(1, 2) _X_NORETURN; -extern void warning(const char *, ...) _X_ATTRIBUTE_PRINTF(1, 2); +extern void warning(const char *, ...) _X_ATTRIBUTE_PRINTF(1, 2); extern void warning1(const char *, ...) _X_ATTRIBUTE_PRINTF(1, 2); -extern struct inclist inclist[ MAXFILES ]; -extern struct inclist *inclistp; -extern struct inclist *inclistnext; -extern struct inclist maininclist; -extern const char *includedirs[ ]; +extern struct inclist inclist[MAXFILES]; +extern struct inclist *inclistp; +extern struct inclist *inclistnext; +extern struct inclist maininclist; +extern const char *includedirs[]; extern const char **includedirsnext; -extern const char * const directives[]; -extern char *notdotdot[ ]; - -extern const char *objprefix; -extern const char *objsuffix; -extern int width; -extern boolean printed; -extern boolean verbose; -extern boolean show_where_not; -extern boolean warn_multiple; +extern const char *const directives[]; +extern char *notdotdot[]; + +extern const char *objprefix; +extern const char *objsuffix; +extern int width; +extern boolean printed; +extern boolean verbose; +extern boolean show_where_not; +extern boolean warn_multiple; diff --git a/ifparser.c b/ifparser.c index 4fb4d58..c53d6d1 100644 --- a/ifparser.c +++ b/ifparser.c @@ -76,465 +76,490 @@ static const char * -parse_variable (IfParser *g, const char *cp, const char **varp) +parse_variable(IfParser *g, const char *cp, const char **varp) { - SKIPSPACE (cp); + SKIPSPACE(cp); - if (!isvarfirstletter (*cp)) - return CALLFUNC(g, handle_error) (g, cp, "variable name"); + if (!isvarfirstletter(*cp)) + return CALLFUNC(g, handle_error) (g, cp, "variable name"); *varp = cp; /* EMPTY */ - for (cp++; isalnum(*cp) || *cp == '_'; cp++) ; + for (cp++; isalnum(*cp) || *cp == '_'; cp++); return cp; } - static const char * -parse_number (IfParser *g, const char *cp, long *valp) +parse_number(IfParser *g, const char *cp, long *valp) { long base = 10; - SKIPSPACE (cp); + + SKIPSPACE(cp); if (!isdigit(*cp)) - return CALLFUNC(g, handle_error) (g, cp, "number"); + return CALLFUNC(g, handle_error) (g, cp, "number"); *valp = 0; if (*cp == '0') { - cp++; - if ((*cp == 'x') || (*cp == 'X')) { - base = 16; - cp++; - } else { - base = 8; - } + cp++; + if ((*cp == 'x') || (*cp == 'X')) { + base = 16; + cp++; + } + else { + base = 8; + } } /* Ignore overflows and assume ASCII, what source is usually written in */ while (1) { - int increment = -1; - if (base == 8) { - if ((*cp >= '0') && (*cp <= '7')) - increment = *cp++ - '0'; - } else if (base == 16) { - if ((*cp >= '0') && (*cp <= '9')) - increment = *cp++ - '0'; - else if ((*cp >= 'A') && (*cp <= 'F')) - increment = *cp++ - ('A' - 10); - else if ((*cp >= 'a') && (*cp <= 'f')) - increment = *cp++ - ('a' - 10); - } else { /* Decimal */ - if ((*cp >= '0') && (*cp <= '9')) - increment = *cp++ - '0'; - } - if (increment < 0) - break; - *valp = (*valp * base) + increment; + int increment = -1; + + if (base == 8) { + if ((*cp >= '0') && (*cp <= '7')) + increment = *cp++ - '0'; + } + else if (base == 16) { + if ((*cp >= '0') && (*cp <= '9')) + increment = *cp++ - '0'; + else if ((*cp >= 'A') && (*cp <= 'F')) + increment = *cp++ - ('A' - 10); + else if ((*cp >= 'a') && (*cp <= 'f')) + increment = *cp++ - ('a' - 10); + } + else { /* Decimal */ + if ((*cp >= '0') && (*cp <= '9')) + increment = *cp++ - '0'; + } + if (increment < 0) + break; + *valp = (*valp * base) + increment; } /* Skip trailing qualifiers */ - while (*cp == 'U' || *cp == 'u' || *cp == 'L' || *cp == 'l') cp++; + while (*cp == 'U' || *cp == 'u' || *cp == 'L' || *cp == 'l') + cp++; return cp; } static const char * -parse_character (IfParser *g, const char *cp, long *valp) +parse_character(IfParser *g, const char *cp, long *valp) { char val; - SKIPSPACE (cp); + SKIPSPACE(cp); if (*cp == '\\') - switch (cp[1]) { - case 'n': val = '\n'; break; - case 't': val = '\t'; break; - case 'v': val = '\v'; break; - case 'b': val = '\b'; break; - case 'r': val = '\r'; break; - case 'f': val = '\f'; break; - case 'a': val = '\a'; break; - case '\\': val = '\\'; break; - case '?': val = '\?'; break; - case '\'': val = '\''; break; - case '\"': val = '\"'; break; - case 'x': val = (char) strtol (cp + 2, NULL, 16); break; - default: val = (char) strtol (cp + 1, NULL, 8); break; - } + switch (cp[1]) { + case 'n': + val = '\n'; + break; + case 't': + val = '\t'; + break; + case 'v': + val = '\v'; + break; + case 'b': + val = '\b'; + break; + case 'r': + val = '\r'; + break; + case 'f': + val = '\f'; + break; + case 'a': + val = '\a'; + break; + case '\\': + val = '\\'; + break; + case '?': + val = '\?'; + break; + case '\'': + val = '\''; + break; + case '\"': + val = '\"'; + break; + case 'x': + val = (char) strtol(cp + 2, NULL, 16); + break; + default: + val = (char) strtol(cp + 1, NULL, 8); + break; + } else - val = *cp; - while (*cp != '\'') cp++; + val = *cp; + while (*cp != '\'') + cp++; *valp = (long) val; return cp; } static const char * -parse_value (IfParser *g, const char *cp, long *valp) +parse_value(IfParser *g, const char *cp, long *valp) { const char *var, *varend; *valp = 0; - SKIPSPACE (cp); + SKIPSPACE(cp); if (!*cp) - return cp; + return cp; switch (*cp) { - case '(': - DO (cp = ParseIfExpression (g, cp + 1, valp)); - SKIPSPACE (cp); - if (*cp != ')') - return CALLFUNC(g, handle_error) (g, cp, ")"); - - return cp + 1; /* skip the right paren */ - - case '!': - DO (cp = parse_value (g, cp + 1, valp)); - *valp = !(*valp); - return cp; - - case '-': - DO (cp = parse_value (g, cp + 1, valp)); - *valp = -(*valp); - return cp; - - case '+': - DO (cp = parse_value (g, cp + 1, valp)); - return cp; - - case '~': - DO (cp = parse_value (g, cp + 1, valp)); - *valp = ~(*valp); - return cp; - - case '#': - DO (cp = parse_variable (g, cp + 1, &var)); - SKIPSPACE (cp); - if (*cp != '(') - return CALLFUNC(g, handle_error) (g, cp, "("); - do { - DO (cp = parse_variable (g, cp + 1, &var)); - SKIPSPACE (cp); - } while (*cp && *cp != ')'); - if (*cp != ')') - return CALLFUNC(g, handle_error) (g, cp, ")"); - *valp = 1; /* XXX */ - return cp + 1; - - case '\'': - DO (cp = parse_character (g, cp + 1, valp)); - if (*cp != '\'') - return CALLFUNC(g, handle_error) (g, cp, "'"); - return cp + 1; - - case 'd': - if (strncmp (cp, "defined", 7) == 0 && !isalnum(cp[7])) { - int paren = 0; - int len; - - cp += 7; - SKIPSPACE (cp); - if (*cp == '(') { - paren = 1; - cp++; - } - DO (cp = parse_variable (g, cp, &var)); - len = cp - var; - SKIPSPACE (cp); - if (paren && *cp != ')') - return CALLFUNC(g, handle_error) (g, cp, ")"); - *valp = (*(g->funcs.eval_defined)) (g, var, len); - return cp + paren; /* skip the right paren */ - } - /* fall out */ + case '(': + DO(cp = ParseIfExpression(g, cp + 1, valp)); + SKIPSPACE(cp); + if (*cp != ')') + return CALLFUNC(g, handle_error) (g, cp, ")"); + + return cp + 1; /* skip the right paren */ + + case '!': + DO(cp = parse_value(g, cp + 1, valp)); + *valp = !(*valp); + return cp; + + case '-': + DO(cp = parse_value(g, cp + 1, valp)); + *valp = -(*valp); + return cp; + + case '+': + DO(cp = parse_value(g, cp + 1, valp)); + return cp; + + case '~': + DO(cp = parse_value(g, cp + 1, valp)); + *valp = ~(*valp); + return cp; + + case '#': + DO(cp = parse_variable(g, cp + 1, &var)); + SKIPSPACE(cp); + if (*cp != '(') + return CALLFUNC(g, handle_error) (g, cp, "("); + do { + DO(cp = parse_variable(g, cp + 1, &var)); + SKIPSPACE(cp); + } while (*cp && *cp != ')'); + if (*cp != ')') + return CALLFUNC(g, handle_error) (g, cp, ")"); + *valp = 1; /* XXX */ + return cp + 1; + + case '\'': + DO(cp = parse_character(g, cp + 1, valp)); + if (*cp != '\'') + return CALLFUNC(g, handle_error) (g, cp, "'"); + return cp + 1; + + case 'd': + if (strncmp(cp, "defined", 7) == 0 && !isalnum(cp[7])) { + int paren = 0; + int len; + + cp += 7; + SKIPSPACE(cp); + if (*cp == '(') { + paren = 1; + cp++; + } + DO(cp = parse_variable(g, cp, &var)); + len = cp - var; + SKIPSPACE(cp); + if (paren && *cp != ')') + return CALLFUNC(g, handle_error) (g, cp, ")"); + *valp = (*(g->funcs.eval_defined)) (g, var, len); + return cp + paren; /* skip the right paren */ + } + /* fall out */ } if (isdigit(*cp)) { - DO (cp = parse_number (g, cp, valp)); - } else if (!isvarfirstletter(*cp)) - return CALLFUNC(g, handle_error) (g, cp, "variable or number"); + DO(cp = parse_number(g, cp, valp)); + } + else if (!isvarfirstletter(*cp)) + return CALLFUNC(g, handle_error) (g, cp, "variable or number"); else { - DO (cp = parse_variable (g, cp, &var)); - varend = cp; - SKIPSPACE(cp); - if (*cp != '(') { - *valp = (*(g->funcs.eval_variable)) (g, var, varend - var); - } else { - do { - long dummy; - DO (cp = ParseIfExpression (g, cp + 1, &dummy)); - SKIPSPACE(cp); - if (*cp == ')') - break; - if (*cp != ',') - return CALLFUNC(g, handle_error) (g, cp, ","); - } while (1); - - *valp = 1; /* XXX */ - cp++; - } + DO(cp = parse_variable(g, cp, &var)); + varend = cp; + SKIPSPACE(cp); + if (*cp != '(') { + *valp = (*(g->funcs.eval_variable)) (g, var, varend - var); + } + else { + do { + long dummy; + + DO(cp = ParseIfExpression(g, cp + 1, &dummy)); + SKIPSPACE(cp); + if (*cp == ')') + break; + if (*cp != ',') + return CALLFUNC(g, handle_error) (g, cp, ","); + } while (1); + + *valp = 1; /* XXX */ + cp++; + } } return cp; } - - static const char * -parse_product (IfParser *g, const char *cp, long *valp) +parse_product(IfParser *g, const char *cp, long *valp) { long rightval; - DO (cp = parse_value (g, cp, valp)); - SKIPSPACE (cp); + DO(cp = parse_value(g, cp, valp)); + SKIPSPACE(cp); switch (*cp) { - case '*': - DO (cp = parse_product (g, cp + 1, &rightval)); - *valp = (*valp * rightval); - break; - - case '/': - DO (cp = parse_product (g, cp + 1, &rightval)); - if (rightval) - *valp = (*valp / rightval); - else - *valp = LONG_MAX; - break; - - case '%': - DO (cp = parse_product (g, cp + 1, &rightval)); - *valp = (*valp % rightval); - break; + case '*': + DO(cp = parse_product(g, cp + 1, &rightval)); + *valp = (*valp * rightval); + break; + + case '/': + DO(cp = parse_product(g, cp + 1, &rightval)); + if (rightval) + *valp = (*valp / rightval); + else + *valp = LONG_MAX; + break; + + case '%': + DO(cp = parse_product(g, cp + 1, &rightval)); + *valp = (*valp % rightval); + break; } return cp; } - static const char * -parse_sum (IfParser *g, const char *cp, long *valp) +parse_sum(IfParser *g, const char *cp, long *valp) { long rightval; - DO (cp = parse_product (g, cp, valp)); - SKIPSPACE (cp); + DO(cp = parse_product(g, cp, valp)); + SKIPSPACE(cp); switch (*cp) { - case '+': - DO (cp = parse_sum (g, cp + 1, &rightval)); - *valp = (*valp + rightval); - break; - - case '-': - DO (cp = parse_sum (g, cp + 1, &rightval)); - *valp = (*valp - rightval); - break; + case '+': + DO(cp = parse_sum(g, cp + 1, &rightval)); + *valp = (*valp + rightval); + break; + + case '-': + DO(cp = parse_sum(g, cp + 1, &rightval)); + *valp = (*valp - rightval); + break; } return cp; } - static const char * -parse_shift (IfParser *g, const char *cp, long *valp) +parse_shift(IfParser *g, const char *cp, long *valp) { long rightval; - DO (cp = parse_sum (g, cp, valp)); - SKIPSPACE (cp); + DO(cp = parse_sum(g, cp, valp)); + SKIPSPACE(cp); switch (*cp) { - case '<': - if (cp[1] == '<') { - DO (cp = parse_shift (g, cp + 2, &rightval)); - *valp = (*valp << rightval); - } - break; - - case '>': - if (cp[1] == '>') { - DO (cp = parse_shift (g, cp + 2, &rightval)); - *valp = (*valp >> rightval); - } - break; + case '<': + if (cp[1] == '<') { + DO(cp = parse_shift(g, cp + 2, &rightval)); + *valp = (*valp << rightval); + } + break; + + case '>': + if (cp[1] == '>') { + DO(cp = parse_shift(g, cp + 2, &rightval)); + *valp = (*valp >> rightval); + } + break; } return cp; } - static const char * -parse_inequality (IfParser *g, const char *cp, long *valp) +parse_inequality(IfParser *g, const char *cp, long *valp) { long rightval; - DO (cp = parse_shift (g, cp, valp)); - SKIPSPACE (cp); + DO(cp = parse_shift(g, cp, valp)); + SKIPSPACE(cp); switch (*cp) { - case '<': - if (cp[1] == '=') { - DO (cp = parse_inequality (g, cp + 2, &rightval)); - *valp = (*valp <= rightval); - } else { - DO (cp = parse_inequality (g, cp + 1, &rightval)); - *valp = (*valp < rightval); - } - break; - - case '>': - if (cp[1] == '=') { - DO (cp = parse_inequality (g, cp + 2, &rightval)); - *valp = (*valp >= rightval); - } else { - DO (cp = parse_inequality (g, cp + 1, &rightval)); - *valp = (*valp > rightval); - } - break; + case '<': + if (cp[1] == '=') { + DO(cp = parse_inequality(g, cp + 2, &rightval)); + *valp = (*valp <= rightval); + } + else { + DO(cp = parse_inequality(g, cp + 1, &rightval)); + *valp = (*valp < rightval); + } + break; + + case '>': + if (cp[1] == '=') { + DO(cp = parse_inequality(g, cp + 2, &rightval)); + *valp = (*valp >= rightval); + } + else { + DO(cp = parse_inequality(g, cp + 1, &rightval)); + *valp = (*valp > rightval); + } + break; } return cp; } - static const char * -parse_equality (IfParser *g, const char *cp, long *valp) +parse_equality(IfParser *g, const char *cp, long *valp) { long rightval; - DO (cp = parse_inequality (g, cp, valp)); - SKIPSPACE (cp); + DO(cp = parse_inequality(g, cp, valp)); + SKIPSPACE(cp); switch (*cp) { - case '=': - if (cp[1] == '=') - cp++; - DO (cp = parse_equality (g, cp + 1, &rightval)); - *valp = (*valp == rightval); - break; - - case '!': - if (cp[1] != '=') - break; - DO (cp = parse_equality (g, cp + 2, &rightval)); - *valp = (*valp != rightval); - break; + case '=': + if (cp[1] == '=') + cp++; + DO(cp = parse_equality(g, cp + 1, &rightval)); + *valp = (*valp == rightval); + break; + + case '!': + if (cp[1] != '=') + break; + DO(cp = parse_equality(g, cp + 2, &rightval)); + *valp = (*valp != rightval); + break; } return cp; } - static const char * -parse_band (IfParser *g, const char *cp, long *valp) +parse_band(IfParser *g, const char *cp, long *valp) { long rightval; - DO (cp = parse_equality (g, cp, valp)); - SKIPSPACE (cp); + DO(cp = parse_equality(g, cp, valp)); + SKIPSPACE(cp); switch (*cp) { - case '&': - if (cp[1] != '&') { - DO (cp = parse_band (g, cp + 1, &rightval)); - *valp = (*valp & rightval); - } - break; + case '&': + if (cp[1] != '&') { + DO(cp = parse_band(g, cp + 1, &rightval)); + *valp = (*valp & rightval); + } + break; } return cp; } - static const char * -parse_bxor (IfParser *g, const char *cp, long *valp) +parse_bxor(IfParser *g, const char *cp, long *valp) { long rightval; - DO (cp = parse_band (g, cp, valp)); - SKIPSPACE (cp); + DO(cp = parse_band(g, cp, valp)); + SKIPSPACE(cp); switch (*cp) { - case '^': - DO (cp = parse_bxor (g, cp + 1, &rightval)); - *valp = (*valp ^ rightval); - break; + case '^': + DO(cp = parse_bxor(g, cp + 1, &rightval)); + *valp = (*valp ^ rightval); + break; } return cp; } - static const char * -parse_bor (IfParser *g, const char *cp, long *valp) +parse_bor(IfParser *g, const char *cp, long *valp) { long rightval; - DO (cp = parse_bxor (g, cp, valp)); - SKIPSPACE (cp); + DO(cp = parse_bxor(g, cp, valp)); + SKIPSPACE(cp); switch (*cp) { - case '|': - if (cp[1] != '|') { - DO (cp = parse_bor (g, cp + 1, &rightval)); - *valp = (*valp | rightval); - } - break; + case '|': + if (cp[1] != '|') { + DO(cp = parse_bor(g, cp + 1, &rightval)); + *valp = (*valp | rightval); + } + break; } return cp; } - static const char * -parse_land (IfParser *g, const char *cp, long *valp) +parse_land(IfParser *g, const char *cp, long *valp) { long rightval; - DO (cp = parse_bor (g, cp, valp)); - SKIPSPACE (cp); + DO(cp = parse_bor(g, cp, valp)); + SKIPSPACE(cp); switch (*cp) { - case '&': - if (cp[1] != '&') - return CALLFUNC(g, handle_error) (g, cp, "&&"); - DO (cp = parse_land (g, cp + 2, &rightval)); - *valp = (*valp && rightval); - break; + case '&': + if (cp[1] != '&') + return CALLFUNC(g, handle_error) (g, cp, "&&"); + DO(cp = parse_land(g, cp + 2, &rightval)); + *valp = (*valp && rightval); + break; } return cp; } - static const char * -parse_lor (IfParser *g, const char *cp, long *valp) +parse_lor(IfParser *g, const char *cp, long *valp) { long rightval; - DO (cp = parse_land (g, cp, valp)); - SKIPSPACE (cp); + DO(cp = parse_land(g, cp, valp)); + SKIPSPACE(cp); switch (*cp) { - case '|': - if (cp[1] != '|') - return CALLFUNC(g, handle_error) (g, cp, "||"); - DO (cp = parse_lor (g, cp + 2, &rightval)); - *valp = (*valp || rightval); - break; + case '|': + if (cp[1] != '|') + return CALLFUNC(g, handle_error) (g, cp, "||"); + DO(cp = parse_lor(g, cp + 2, &rightval)); + *valp = (*valp || rightval); + break; } return cp; } - static const char * parse_cond(IfParser *g, const char *cp, long *valp) { long trueval, falseval; - DO (cp = parse_lor (g, cp, valp)); - SKIPSPACE (cp); + DO(cp = parse_lor(g, cp, valp)); + SKIPSPACE(cp); switch (*cp) { - case '?': - DO (cp = parse_cond (g, cp + 1, &trueval)); - SKIPSPACE (cp); - if (*cp != ':') - return CALLFUNC(g, handle_error) (g, cp, ":"); - DO (cp = parse_cond (g, cp + 1, &falseval)); - *valp = (*valp ? trueval : falseval); - break; + case '?': + DO(cp = parse_cond(g, cp + 1, &trueval)); + SKIPSPACE(cp); + if (*cp != ':') + return CALLFUNC(g, handle_error) (g, cp, ":"); + DO(cp = parse_cond(g, cp + 1, &falseval)); + *valp = (*valp ? trueval : falseval); + break; } return cp; } @@ -545,7 +570,7 @@ parse_cond(IfParser *g, const char *cp, long *valp) ****************************************************************************/ const char * -ParseIfExpression (IfParser *g, const char *cp, long *valp) +ParseIfExpression(IfParser *g, const char *cp, long *valp) { - return parse_cond (g, cp, valp); + return parse_cond(g, cp, valp); } diff --git a/ifparser.h b/ifparser.h index ad809d1..5d88d05 100644 --- a/ifparser.h +++ b/ifparser.h @@ -56,26 +56,21 @@ * ParseIfExpression parse a string for #if */ - #include typedef int Bool; + #define False 0 #define True 1 typedef struct _if_parser { - struct { /* functions */ - const char *(*handle_error) (struct _if_parser *, const char *, - const char *); - long (*eval_variable) (struct _if_parser *, const char *, int); - int (*eval_defined) (struct _if_parser *, const char *, int); + struct { /* functions */ + const char *(*handle_error)(struct _if_parser *, const char *, + const char *); + long (*eval_variable)(struct _if_parser *, const char *, int); + int (*eval_defined)(struct _if_parser *, const char *, int); } funcs; char *data; } IfParser; -const char *ParseIfExpression ( - IfParser *, - const char *, - long * -); - +const char *ParseIfExpression(IfParser *, const char *, long *); diff --git a/imakemdep.h b/imakemdep.h index 268e837..f1b8a8f 100644 --- a/imakemdep.h +++ b/imakemdep.h @@ -42,425 +42,423 @@ in this Software without prior written authorization from The Open Group. #undef DEF_STRINGIFY #define DEF_EVALUATE(__x) #__x #define DEF_STRINGIFY(_x) DEF_EVALUATE(_x) -static const struct symtab predefs[] = { +static const struct symtab predefs[] = { #ifdef apollo - {"apollo", "1"}, + {"apollo", "1"}, #endif #if defined(clipper) || defined(__clipper__) - {"clipper", "1"}, - {"__clipper__", "1"}, - {"clix", "1"}, - {"__clix__", "1"}, + {"clipper", "1"}, + {"__clipper__", "1"}, + {"clix", "1"}, + {"__clix__", "1"}, #endif #ifdef ibm032 - {"ibm032", "1"}, + {"ibm032", "1"}, #endif #ifdef ibm - {"ibm", "1"}, + {"ibm", "1"}, #endif #ifdef aix - {"aix", "1"}, + {"aix", "1"}, #endif #ifdef sun - {"sun", "1"}, + {"sun", "1"}, #endif #ifdef sun2 - {"sun2", "1"}, + {"sun2", "1"}, #endif #ifdef sun3 - {"sun3", "1"}, + {"sun3", "1"}, #endif #ifdef sun4 - {"sun4", "1"}, + {"sun4", "1"}, #endif #ifdef __sun - {"__sun", "1"}, + {"__sun", "1"}, #endif #ifdef __SunOS - {"__SunOS", "1"}, + {"__SunOS", "1"}, #endif #ifdef __SVR4 - {"__SVR4", "1"}, + {"__SVR4", "1"}, #endif #ifdef sparc - {"sparc", "1"}, + {"sparc", "1"}, #endif #ifdef __sparc - {"__sparc", "1"}, + {"__sparc", "1"}, #endif #ifdef __sparcv9 - {"__sparcv9", "1"}, + {"__sparcv9", "1"}, #endif #ifdef __sparc__ - {"__sparc__", "1"}, + {"__sparc__", "1"}, #endif #ifdef __sparcv9__ - {"__sparcv9__", "1"}, + {"__sparcv9__", "1"}, #endif #ifdef hpux - {"hpux", "1"}, + {"hpux", "1"}, #endif #ifdef __hpux - {"__hpux", "1"}, + {"__hpux", "1"}, #endif #ifdef __hp9000s800 - {"__hp9000s800", "1"}, + {"__hp9000s800", "1"}, #endif #ifdef __hp9000s700 - {"__hp9000s700", "1"}, + {"__hp9000s700", "1"}, #endif #ifdef vax - {"vax", "1"}, + {"vax", "1"}, #endif #ifdef VMS - {"VMS", "1"}, + {"VMS", "1"}, #endif #ifdef cray - {"cray", "1"}, + {"cray", "1"}, #endif #ifdef CRAY - {"CRAY", "1"}, + {"CRAY", "1"}, #endif #ifdef _CRAY - {"_CRAY", "1"}, + {"_CRAY", "1"}, #endif #ifdef att - {"att", "1"}, + {"att", "1"}, #endif #ifdef mips - {"mips", "1"}, + {"mips", "1"}, #endif #ifdef __mips__ - {"__mips__", "1"}, + {"__mips__", "1"}, #endif #ifdef ultrix - {"ultrix", "1"}, + {"ultrix", "1"}, #endif #ifdef stellar - {"stellar", "1"}, + {"stellar", "1"}, #endif #ifdef mc68000 - {"mc68000", "1"}, + {"mc68000", "1"}, #endif #ifdef mc68020 - {"mc68020", "1"}, + {"mc68020", "1"}, #endif #ifdef __GNUC__ - {"__GNUC__", DEF_STRINGIFY(__GNUC__)}, + {"__GNUC__", DEF_STRINGIFY(__GNUC__)}, #endif #ifdef __STRICT_ANSI__ - {"__STRICT_ANSI__", "1"}, + {"__STRICT_ANSI__", "1"}, #endif #ifdef __STDC__ - {"__STDC__", DEF_STRINGIFY(__STDC__)}, + {"__STDC__", DEF_STRINGIFY(__STDC__)}, #endif #ifdef __HIGHC__ - {"__HIGHC__", "1"}, + {"__HIGHC__", "1"}, #endif #ifdef __OPENSERVER__ - {"__OPENSERVER__", DEF_STRINGIFY(__OPENSERVER__)}, + {"__OPENSERVER__", DEF_STRINGIFY(__OPENSERVER__)}, #endif #ifdef _SCO_DS - {"_SCO_DS", DEF_STRINGIFY(_SCO_DS)}, + {"_SCO_DS", DEF_STRINGIFY(_SCO_DS)}, #endif #ifdef _SCO_DS_LL - {"_SCO_DS_LL", DEF_STRINGIFY(_SCO_DS_LL)}, + {"_SCO_DS_LL", DEF_STRINGIFY(_SCO_DS_LL)}, #endif #ifdef __SCO_VERSION__ - {"__SCO_VERSION__", DEF_STRINGIFY(__SCO_VERSION__)}, + {"__SCO_VERSION__", DEF_STRINGIFY(__SCO_VERSION__)}, #endif #ifdef __UNIXWARE__ - {"__UNIXWARE__", DEF_STRINGIFY(__UNIXWARE__)}, + {"__UNIXWARE__", DEF_STRINGIFY(__UNIXWARE__)}, #endif #ifdef __USLC__ - {"__USLC__", DEF_STRINGIFY(__USLC__)}, + {"__USLC__", DEF_STRINGIFY(__USLC__)}, #endif #ifdef CMU - {"CMU", "1"}, + {"CMU", "1"}, #endif #ifdef luna - {"luna", "1"}, + {"luna", "1"}, #ifdef luna1 - {"luna1", "1"}, + {"luna1", "1"}, #endif #ifdef luna2 - {"luna2", "1"}, + {"luna2", "1"}, #endif #ifdef luna88k - {"luna88k", "1"}, + {"luna88k", "1"}, #endif #ifdef uniosb - {"uniosb", "1"}, + {"uniosb", "1"}, #endif #ifdef uniosu - {"uniosu", "1"}, + {"uniosu", "1"}, #endif #endif #ifdef ieeep754 - {"ieeep754", "1"}, + {"ieeep754", "1"}, #endif #ifdef is68k - {"is68k", "1"}, + {"is68k", "1"}, #endif #ifdef m68k - {"m68k", "1"}, + {"m68k", "1"}, #endif #ifdef M68k - {"M68k", "1"}, + {"M68k", "1"}, #endif #ifdef __m68k__ - {"__m68k__", "1"}, + {"__m68k__", "1"}, #endif #ifdef m88k - {"m88k", "1"}, + {"m88k", "1"}, #endif #ifdef __m88k__ - {"__m88k__", "1"}, + {"__m88k__", "1"}, #endif #ifdef bsd43 - {"bsd43", "1"}, + {"bsd43", "1"}, #endif #ifdef hcx - {"hcx", "1"}, + {"hcx", "1"}, #endif #ifdef sony - {"sony", "1"}, + {"sony", "1"}, #ifdef SYSTYPE_SYSV - {"SYSTYPE_SYSV", "1"}, + {"SYSTYPE_SYSV", "1"}, #endif #ifdef _SYSTYPE_SYSV - {"_SYSTYPE_SYSV", "1"}, + {"_SYSTYPE_SYSV", "1"}, #endif #endif #ifdef __OSF__ - {"__OSF__", "1"}, + {"__OSF__", "1"}, #endif #ifdef __osf__ - {"__osf__", "1"}, + {"__osf__", "1"}, #endif #ifdef __amiga__ - {"__amiga__", "1"}, + {"__amiga__", "1"}, #endif #ifdef __alpha - {"__alpha", "1"}, + {"__alpha", "1"}, #endif #ifdef __alpha__ - {"__alpha__", "1"}, + {"__alpha__", "1"}, #endif #ifdef __DECC - {"__DECC", "1"}, + {"__DECC", "1"}, #endif #ifdef __decc - {"__decc", "1"}, + {"__decc", "1"}, #endif #ifdef __unix - {"__unix", "1"}, + {"__unix", "1"}, #endif #ifdef __unix__ - {"__unix__", "1"}, + {"__unix__", "1"}, #endif #ifdef __uxp__ - {"__uxp__", "1"}, + {"__uxp__", "1"}, #endif #ifdef __sxg__ - {"__sxg__", "1"}, + {"__sxg__", "1"}, #endif #ifdef _SEQUENT_ - {"_SEQUENT_", "1"}, - {"__STDC__", "1"}, + {"_SEQUENT_", "1"}, + {"__STDC__", "1"}, #endif #ifdef __bsdi__ - {"__bsdi__", "1"}, + {"__bsdi__", "1"}, #endif #ifdef nec_ews_svr2 - {"nec_ews_svr2", "1"}, + {"nec_ews_svr2", "1"}, #endif #ifdef nec_ews_svr4 - {"nec_ews_svr4", "1"}, + {"nec_ews_svr4", "1"}, #endif #ifdef _nec_ews_svr4 - {"_nec_ews_svr4", "1"}, + {"_nec_ews_svr4", "1"}, #endif #ifdef _nec_up - {"_nec_up", "1"}, + {"_nec_up", "1"}, #endif #ifdef SX - {"SX", "1"}, + {"SX", "1"}, #endif #ifdef nec - {"nec", "1"}, + {"nec", "1"}, #endif #ifdef _nec_ft - {"_nec_ft", "1"}, + {"_nec_ft", "1"}, #endif #ifdef PC_UX - {"PC_UX", "1"}, + {"PC_UX", "1"}, #endif #ifdef sgi - {"sgi", "1"}, + {"sgi", "1"}, #endif #ifdef __sgi - {"__sgi", "1"}, + {"__sgi", "1"}, #endif #ifdef _MIPS_FPSET - {"_MIPS_FPSET", DEF_STRINGIFY(_MIPS_FPSET)}, + {"_MIPS_FPSET", DEF_STRINGIFY(_MIPS_FPSET)}, #endif #ifdef _MIPS_ISA - {"_MIPS_ISA", DEF_STRINGIFY(_MIPS_ISA)}, + {"_MIPS_ISA", DEF_STRINGIFY(_MIPS_ISA)}, #endif #ifdef _MIPS_SIM - {"_MIPS_SIM", DEF_STRINGIFY(_MIPS_SIM)}, + {"_MIPS_SIM", DEF_STRINGIFY(_MIPS_SIM)}, #endif #ifdef _MIPS_SZINT - {"_MIPS_SZINT", DEF_STRINGIFY(_MIPS_SZINT)}, + {"_MIPS_SZINT", DEF_STRINGIFY(_MIPS_SZINT)}, #endif #ifdef _MIPS_SZLONG - {"_MIPS_SZLONG", DEF_STRINGIFY(_MIPS_SZLONG)}, + {"_MIPS_SZLONG", DEF_STRINGIFY(_MIPS_SZLONG)}, #endif #ifdef _MIPS_SZPTR - {"_MIPS_SZPTR", DEF_STRINGIFY(_MIPS_SZPTR)}, + {"_MIPS_SZPTR", DEF_STRINGIFY(_MIPS_SZPTR)}, #endif #ifdef __DragonFly__ - {"__DragonFly__", "1"}, + {"__DragonFly__", "1"}, #endif #ifdef __FreeBSD__ - {"__FreeBSD__", "1"}, + {"__FreeBSD__", "1"}, #endif #ifdef __OpenBSD__ - {"__OpenBSD__", "1"}, + {"__OpenBSD__", "1"}, #endif #ifdef __NetBSD__ - {"__NetBSD__", "1"}, + {"__NetBSD__", "1"}, #endif #ifdef __GNU__ - {"__GNU__", "1"}, + {"__GNU__", "1"}, #endif #ifdef __ELF__ - {"__ELF__", "1"}, + {"__ELF__", "1"}, #endif #if defined(__QNX__) - {"__QNX__", "1"}, + {"__QNX__", "1"}, #endif #ifdef __QNXNTO__ - {"__QNXNTO__", "1"}, -#endif -# ifdef __powerpc__ - {"__powerpc__", "1"}, -# endif -# ifdef __powerpc64__ - {"__powerpc64__", "1"}, -# endif -# ifdef PowerMAX_OS - {"PowerMAX_OS", "1"}, -# endif -# ifdef ia64 - {"ia64", "1"}, -# endif -# ifdef __ia64__ - {"__ia64__", "1"}, -# endif -# if defined (amd64) || defined (x86_64) - {"amd64", "1"}, - {"x86_64", "1"}, -# endif -# if defined (__amd64__) || defined (__x86_64__) - {"__amd64__", "1"}, - {"__x86_64__", "1"}, -# endif -# if defined (__amd64) || defined(__x86_64) - {"__amd64", "1"}, - {"__x86_64", "1"}, -# endif -# ifdef __x86 - {"__x86", "1"}, -# endif -# ifdef __i386 - {"__i386", "1"}, -# endif -# ifdef __i386__ - {"__i386__", "1"}, -# endif -# ifdef __i486__ - {"__i486__", "1"}, -# endif -# ifdef __i586__ - {"__i586__", "1"}, -# endif -# ifdef __i686__ - {"__i686__", "1"}, -# endif -# ifdef __k6__ - {"__k6__", "1"}, -# endif -# ifdef i386 - {"i386", "1"}, -# endif -# ifdef i486 - {"i486", "1"}, -# endif -# ifdef i586 - {"i586", "1"}, -# endif -# ifdef i686 - { "i686", "1"}, -# endif -# ifdef k6 - {"k6", "1"}, -# endif -# ifdef sparc - {"sparc", "1"}, -# endif -# ifdef __sparc__ - {"__sparc__", "1"}, -# endif -# ifdef __s390__ - {"__s390__", "1"}, -# endif -# ifdef __hppa__ - {"__hppa__", "1"}, -# endif -# ifdef __sh__ - {"__sh__", "1"}, -# endif -# ifdef __sh3_ - {"__sh3__", "1"}, -# endif -# ifdef __SH3__ - {"__SH3__", "1"}, -# endif -# ifdef __SH4__ - {"__SH4__", "1"}, -# endif -# ifdef __SH4NOFPU__ - {"__SH4NOFPU__", "1"}, -# endif + {"__QNXNTO__", "1"}, +#endif +#ifdef __powerpc__ + {"__powerpc__", "1"}, +#endif +#ifdef __powerpc64__ + {"__powerpc64__", "1"}, +#endif +#ifdef PowerMAX_OS + {"PowerMAX_OS", "1"}, +#endif +#ifdef ia64 + {"ia64", "1"}, +#endif +#ifdef __ia64__ + {"__ia64__", "1"}, +#endif +#if defined (amd64) || defined (x86_64) + {"amd64", "1"}, + {"x86_64", "1"}, +#endif +#if defined (__amd64__) || defined (__x86_64__) + {"__amd64__", "1"}, + {"__x86_64__", "1"}, +#endif +#if defined (__amd64) || defined(__x86_64) + {"__amd64", "1"}, + {"__x86_64", "1"}, +#endif +#ifdef __x86 + {"__x86", "1"}, +#endif +#ifdef __i386 + {"__i386", "1"}, +#endif +#ifdef __i386__ + {"__i386__", "1"}, +#endif +#ifdef __i486__ + {"__i486__", "1"}, +#endif +#ifdef __i586__ + {"__i586__", "1"}, +#endif +#ifdef __i686__ + {"__i686__", "1"}, +#endif +#ifdef __k6__ + {"__k6__", "1"}, +#endif +#ifdef i386 + {"i386", "1"}, +#endif +#ifdef i486 + {"i486", "1"}, +#endif +#ifdef i586 + {"i586", "1"}, +#endif +#ifdef i686 + {"i686", "1"}, +#endif +#ifdef k6 + {"k6", "1"}, +#endif +#ifdef sparc + {"sparc", "1"}, +#endif +#ifdef __sparc__ + {"__sparc__", "1"}, +#endif +#ifdef __s390__ + {"__s390__", "1"}, +#endif +#ifdef __hppa__ + {"__hppa__", "1"}, +#endif +#ifdef __sh__ + {"__sh__", "1"}, +#endif +#ifdef __sh3_ + {"__sh3__", "1"}, +#endif +#ifdef __SH3__ + {"__SH3__", "1"}, +#endif +#ifdef __SH4__ + {"__SH4__", "1"}, +#endif +#ifdef __SH4NOFPU__ + {"__SH4NOFPU__", "1"}, +#endif #if defined(__ppc__) - {"__ppc__", "1"}, + {"__ppc__", "1"}, #endif #if defined(__ppc64__) - {"__ppc64__", "1"}, + {"__ppc64__", "1"}, #endif #if defined(__BIG_ENDIAN__) - {"__BIG_ENDIAN__", "1"}, + {"__BIG_ENDIAN__", "1"}, #endif #if defined(__LITTLE_ENDIAN__) - {"__LITTLE_ENDIAN__", "1"}, + {"__LITTLE_ENDIAN__", "1"}, #endif #if defined (__CHAR_BIT__) - {"__CHAR_BIT__", DEF_STRINGIFY(__CHAR_BIT__)}, + {"__CHAR_BIT__", DEF_STRINGIFY(__CHAR_BIT__)}, #endif #if defined (__BUILTIN_VA_STRUCT) - {"__BUILTIN_VA_STRUCT", "1"}, + {"__BUILTIN_VA_STRUCT", "1"}, #endif #if defined (__BUILTIN_VA_ARG_INCR) - {"__BUILTIN_VA_ARG_INCR", "1"}, + {"__BUILTIN_VA_ARG_INCR", "1"}, #endif - /* add any additional symbols before this line */ - {NULL, NULL} + /* add any additional symbols before this line */ + {NULL, NULL} }; + #undef DEF_EVALUATE #undef DEF_STRINGIFY -#endif /* CROSSCOMPILE */ - - - +#endif /* CROSSCOMPILE */ diff --git a/include.c b/include.c index 57e2b24..9cc97f8 100644 --- a/include.c +++ b/include.c @@ -24,45 +24,43 @@ in this Software without prior written authorization from The Open Group. */ - #include "def.h" static boolean isdot(const char *p) { - if(p && *p++ == '.' && *p++ == '\0') - return(TRUE); - return(FALSE); + if (p && *p++ == '.' && *p++ == '\0') + return (TRUE); + return (FALSE); } static boolean isdotdot(const char *p) { - if(p && *p++ == '.' && *p++ == '.' && *p++ == '\0') - return(TRUE); - return(FALSE); + if (p && *p++ == '.' && *p++ == '.' && *p++ == '\0') + return (TRUE); + return (FALSE); } static boolean issymbolic(const char *dir, const char *component) { #ifdef S_IFLNK - struct stat st; - char buf[ BUFSIZ ], **pp; - - snprintf(buf, sizeof(buf), "%s%s%s", dir, *dir ? "/" : "", component); - for (pp=notdotdot; *pp; pp++) - if (strcmp(*pp, buf) == 0) - return (TRUE); - if (lstat(buf, &st) == 0 - && (st.st_mode & S_IFMT) == S_IFLNK) { - *pp++ = strdup(buf); - if (pp >= ¬dotdot[ MAXDIRS ]) - fatalerr("out of .. dirs, increase MAXDIRS\n"); - return(TRUE); - } + struct stat st; + char buf[BUFSIZ], **pp; + + snprintf(buf, sizeof(buf), "%s%s%s", dir, *dir ? "/" : "", component); + for (pp = notdotdot; *pp; pp++) + if (strcmp(*pp, buf) == 0) + return (TRUE); + if (lstat(buf, &st) == 0 && (st.st_mode & S_IFMT) == S_IFLNK) { + *pp++ = strdup(buf); + if (pp >= ¬dotdot[MAXDIRS]) + fatalerr("out of .. dirs, increase MAXDIRS\n"); + return (TRUE); + } #endif - return(FALSE); + return (FALSE); } /* @@ -73,70 +71,69 @@ issymbolic(const char *dir, const char *component) static void remove_dotdot(char *path) { - register char *end, *from, *to, **cp; - char *components[ MAXFILES ], - newpath[ BUFSIZ ]; - boolean component_copied; - - /* - * slice path up into components. - */ - to = newpath; - if (*path == '/') - *to++ = '/'; - *to = '\0'; - cp = components; - for (from=end=path; *end; end++) - if (*end == '/') { - while (*end == '/') - *end++ = '\0'; - if (*from) - *cp++ = from; - from = end; - } - *cp++ = from; - *cp = NULL; - - /* - * Recursively remove all 'x/..' component pairs. - */ - cp = components; - while(*cp) { - if (!isdot(*cp) && !isdotdot(*cp) && isdotdot(*(cp+1)) - && !issymbolic(newpath, *cp)) - { - char **fp = cp + 2; - char **tp = cp; - - do - *tp++ = *fp; /* move all the pointers down */ - while (*fp++); - if (cp != components) - cp--; /* go back and check for nested ".." */ - } else { - cp++; - } - } - /* - * Concatenate the remaining path elements. - */ - cp = components; - component_copied = FALSE; - while(*cp) { - if (component_copied) - *to++ = '/'; - component_copied = TRUE; - for (from = *cp; *from; ) - *to++ = *from++; - *to = '\0'; - cp++; - } - *to++ = '\0'; - - /* - * copy the reconstituted path back to our pointer. - */ - strcpy(path, newpath); + register char *end, *from, *to, **cp; + char *components[MAXFILES], newpath[BUFSIZ]; + boolean component_copied; + + /* + * slice path up into components. + */ + to = newpath; + if (*path == '/') + *to++ = '/'; + *to = '\0'; + cp = components; + for (from = end = path; *end; end++) + if (*end == '/') { + while (*end == '/') + *end++ = '\0'; + if (*from) + *cp++ = from; + from = end; + } + *cp++ = from; + *cp = NULL; + + /* + * Recursively remove all 'x/..' component pairs. + */ + cp = components; + while (*cp) { + if (!isdot(*cp) && !isdotdot(*cp) && isdotdot(*(cp + 1)) + && !issymbolic(newpath, *cp)) { + char **fp = cp + 2; + char **tp = cp; + + do + *tp++ = *fp; /* move all the pointers down */ + while (*fp++); + if (cp != components) + cp--; /* go back and check for nested ".." */ + } + else { + cp++; + } + } + /* + * Concatenate the remaining path elements. + */ + cp = components; + component_copied = FALSE; + while (*cp) { + if (component_copied) + *to++ = '/'; + component_copied = TRUE; + for (from = *cp; *from;) + *to++ = *from++; + *to = '\0'; + cp++; + } + *to++ = '\0'; + + /* + * copy the reconstituted path back to our pointer. + */ + strcpy(path, newpath); } /* @@ -145,81 +142,79 @@ remove_dotdot(char *path) struct inclist * newinclude(const char *newfile, const char *incstring) { - register struct inclist *ip; - - /* - * First, put this file on the global list of include files. - */ - ip = inclistp++; - if (inclistp == inclist + MAXFILES - 1) - fatalerr("out of space: increase MAXFILES\n"); - ip->i_file = strdup(newfile); - - if (incstring == NULL) - ip->i_incstring = ip->i_file; - else - ip->i_incstring = strdup(incstring); - - inclistnext = inclistp; - return(ip); + register struct inclist *ip; + + /* + * First, put this file on the global list of include files. + */ + ip = inclistp++; + if (inclistp == inclist + MAXFILES - 1) + fatalerr("out of space: increase MAXFILES\n"); + ip->i_file = strdup(newfile); + + if (incstring == NULL) + ip->i_incstring = ip->i_file; + else + ip->i_incstring = strdup(incstring); + + inclistnext = inclistp; + return (ip); } void included_by(struct inclist *ip, struct inclist *newfile) { - register int i; - - if (ip == NULL) - return; - /* - * Put this include file (newfile) on the list of files included - * by 'file'. If 'file' is NULL, then it is not an include - * file itself (i.e. was probably mentioned on the command line). - * If it is already on the list, don't stick it on again. - */ - if (ip->i_list == NULL) { - ip->i_list = malloc(sizeof(struct inclist *) * ++ip->i_listlen); - ip->i_merged = malloc(sizeof(boolean) * ip->i_listlen); - } else { - for (i=0; ii_listlen; i++) - if (ip->i_list[ i ] == newfile) { - i = strlen(newfile->i_file); - if (!(ip->i_flags & INCLUDED_SYM) && - !(i > 2 && - newfile->i_file[i-1] == 'c' && - newfile->i_file[i-2] == '.')) - { - /* only bitch if ip has */ - /* no #include SYMBOL lines */ - /* and is not a .c file */ - if (warn_multiple) - { - warning("%s includes %s more than once!\n", - ip->i_file, newfile->i_file); - warning1("Already have\n"); - for (i=0; ii_listlen; i++) - warning1("\t%s\n", ip->i_list[i]->i_file); - } - } - return; - } - ip->i_list = realloc(ip->i_list, - sizeof(struct inclist *) * ++ip->i_listlen); - ip->i_merged = - realloc(ip->i_merged, sizeof(boolean) * ip->i_listlen); - } - ip->i_list[ ip->i_listlen-1 ] = newfile; - ip->i_merged[ ip->i_listlen-1 ] = FALSE; + register int i; + + if (ip == NULL) + return; + /* + * Put this include file (newfile) on the list of files included + * by 'file'. If 'file' is NULL, then it is not an include + * file itself (i.e. was probably mentioned on the command line). + * If it is already on the list, don't stick it on again. + */ + if (ip->i_list == NULL) { + ip->i_list = malloc(sizeof(struct inclist *) * ++ip->i_listlen); + ip->i_merged = malloc(sizeof(boolean) * ip->i_listlen); + } + else { + for (i = 0; i < ip->i_listlen; i++) + if (ip->i_list[i] == newfile) { + i = strlen(newfile->i_file); + if (!(ip->i_flags & INCLUDED_SYM) && + !(i > 2 && + newfile->i_file[i - 1] == 'c' && + newfile->i_file[i - 2] == '.')) { + /* only bitch if ip has */ + /* no #include SYMBOL lines */ + /* and is not a .c file */ + if (warn_multiple) { + warning("%s includes %s more than once!\n", + ip->i_file, newfile->i_file); + warning1("Already have\n"); + for (i = 0; i < ip->i_listlen; i++) + warning1("\t%s\n", ip->i_list[i]->i_file); + } + } + return; + } + ip->i_list = realloc(ip->i_list, + sizeof(struct inclist *) * ++ip->i_listlen); + ip->i_merged = realloc(ip->i_merged, sizeof(boolean) * ip->i_listlen); + } + ip->i_list[ip->i_listlen - 1] = newfile; + ip->i_merged[ip->i_listlen - 1] = FALSE; } void -inc_clean (void) +inc_clean(void) { - register struct inclist *ip; + register struct inclist *ip; - for (ip = inclist; ip < inclistp; ip++) { - ip->i_flags &= ~MARKED; - } + for (ip = inclist; ip < inclistp; ip++) { + ip->i_flags &= ~MARKED; + } } /* @@ -229,105 +224,107 @@ inc_clean (void) static const char * find_full_inc_path(const char *file, const char *include, int type) { - static char path[ BUFSIZ ]; - register const char **pp, *p; - struct stat st; - - if (inclistnext == inclist) { - /* - * If the path was surrounded by "" or is an absolute path, - * then check the exact path provided. - */ - if ((type == INCLUDEDOT) || - (type == INCLUDENEXTDOT) || - (*include == '/')) { - if (stat(include, &st) == 0 && !S_ISDIR(st.st_mode)) - return include; - if (show_where_not) - warning1("\tnot in %s\n", include); - } - - /* - * If the path was surrounded by "" see if this include file is - * in the directory of the file being parsed. - */ - if ((type == INCLUDEDOT) || (type == INCLUDENEXTDOT)) { - for (p=file+strlen(file); p>file; p--) - if (*p == '/') - break; - if (p == file) { - strcpy(path, include); - } else { - strncpy(path, file, (p-file) + 1); - path[ (p-file) + 1 ] = '\0'; - strcpy(path + (p-file) + 1, include); - } - remove_dotdot(path); - if (stat(path, &st) == 0 && !S_ISDIR(st.st_mode)) - return path; - if (show_where_not) - warning1("\tnot in %s\n", path); - } - } - - /* - * Check the include directories specified. Standard include dirs - * should be at the end. - */ - if ((type == INCLUDE) || (type == INCLUDEDOT)) - includedirsnext = includedirs; - pp = includedirsnext; - - for (; *pp; pp++) { - snprintf(path, sizeof(path), "%s/%s", *pp, include); - remove_dotdot(path); - if (stat(path, &st) == 0 && !S_ISDIR(st.st_mode)) { - includedirsnext = pp + 1; - return path; - } - if (show_where_not) - warning1("\tnot in %s\n", path); - } - - return NULL; + static char path[BUFSIZ]; + register const char **pp, *p; + struct stat st; + + if (inclistnext == inclist) { + /* + * If the path was surrounded by "" or is an absolute path, + * then check the exact path provided. + */ + if ((type == INCLUDEDOT) || + (type == INCLUDENEXTDOT) || + (*include == '/')) { + if (stat(include, &st) == 0 && !S_ISDIR(st.st_mode)) + return include; + if (show_where_not) + warning1("\tnot in %s\n", include); + } + + /* + * If the path was surrounded by "" see if this include file is + * in the directory of the file being parsed. + */ + if ((type == INCLUDEDOT) || (type == INCLUDENEXTDOT)) { + for (p = file + strlen(file); p > file; p--) + if (*p == '/') + break; + if (p == file) { + strcpy(path, include); + } + else { + strncpy(path, file, (p - file) + 1); + path[(p - file) + 1] = '\0'; + strcpy(path + (p - file) + 1, include); + } + remove_dotdot(path); + if (stat(path, &st) == 0 && !S_ISDIR(st.st_mode)) + return path; + if (show_where_not) + warning1("\tnot in %s\n", path); + } + } + + /* + * Check the include directories specified. Standard include dirs + * should be at the end. + */ + if ((type == INCLUDE) || (type == INCLUDEDOT)) + includedirsnext = includedirs; + pp = includedirsnext; + + for (; *pp; pp++) { + snprintf(path, sizeof(path), "%s/%s", *pp, include); + remove_dotdot(path); + if (stat(path, &st) == 0 && !S_ISDIR(st.st_mode)) { + includedirsnext = pp + 1; + return path; + } + if (show_where_not) + warning1("\tnot in %s\n", path); + } + + return NULL; } struct inclist * inc_path(const char *file, const char *include, int type) { - const char *fp; - struct inclist *ip; - char r_include[PATHMAX+1]; - - /* - * Check all previously found include files for a path that - * has already been expanded. - */ - if ((type == INCLUDE) || (type == INCLUDEDOT)) - inclistnext = inclist; - ip = inclistnext; - - fp = find_full_inc_path(file, include, type); - if (fp == NULL) - return NULL; - if (realpath(fp, r_include) == NULL) - return NULL; - - for (; ip->i_file; ip++) { - if ((strcmp(ip->i_incstring, include) == 0) && - !(ip->i_flags & INCLUDED_SYM)) { - /* - * Same filename but same file ? - */ - char r_saved_path[PATHMAX+1]; - if (realpath(ip->i_file, r_saved_path) == NULL) - continue; - if (!strcmp(r_include, r_saved_path)) { - inclistnext = ip + 1; - return ip; - } - } - } - - return newinclude(fp, include); + const char *fp; + struct inclist *ip; + char r_include[PATHMAX + 1]; + + /* + * Check all previously found include files for a path that + * has already been expanded. + */ + if ((type == INCLUDE) || (type == INCLUDEDOT)) + inclistnext = inclist; + ip = inclistnext; + + fp = find_full_inc_path(file, include, type); + if (fp == NULL) + return NULL; + if (realpath(fp, r_include) == NULL) + return NULL; + + for (; ip->i_file; ip++) { + if ((strcmp(ip->i_incstring, include) == 0) && + !(ip->i_flags & INCLUDED_SYM)) { + /* + * Same filename but same file ? + */ + char r_saved_path[PATHMAX + 1]; + + if (realpath(ip->i_file, r_saved_path) == NULL) + continue; + if (!strcmp(r_include, r_saved_path)) { + inclistnext = ip + 1; + return ip; + } + } + } + + return newinclude(fp, include); } diff --git a/main.c b/main.c index 816243f..c125e99 100644 --- a/main.c +++ b/main.c @@ -46,11 +46,11 @@ in this Software without prior written authorization from The Open Group. #include #ifdef __sun -# include +#include #endif #ifdef DEBUG -int _debugmask; +int _debugmask; #endif /* #define DEBUG_DUMP */ @@ -65,59 +65,60 @@ int _debugmask; const char *ProgramName; -const char * const directives[] = { - "if", - "ifdef", - "ifndef", - "else", - "endif", - "define", - "undef", - "include", - "line", - "pragma", - "error", - "ident", - "sccs", - "elif", - "eject", - "warning", - "include_next", - NULL +const char *const directives[] = { + "if", + "ifdef", + "ifndef", + "else", + "endif", + "define", + "undef", + "include", + "line", + "pragma", + "error", + "ident", + "sccs", + "elif", + "eject", + "warning", + "include_next", + NULL }; #include "imakemdep.h" -struct inclist inclist[ MAXFILES ], - *inclistp = inclist, - *inclistnext = inclist, - maininclist; - -static char *filelist[ MAXFILES ]; -const char *includedirs[ MAXDIRS + 1 ], - **includedirsnext = includedirs; -char *notdotdot[ MAXDIRS ]; -static int cmdinc_count = 0; -static char *cmdinc_list[ 2 * MAXINCFILES ]; -const char *objprefix = ""; -const char *objsuffix = OBJSUFFIX; -static const char *startat = "# DO NOT DELETE"; -int width = 78; -static boolean append = FALSE; -boolean printed = FALSE; -boolean verbose = FALSE; -boolean show_where_not = FALSE; +struct inclist inclist[MAXFILES], + *inclistp = inclist, + *inclistnext = inclist, + maininclist; + +static char *filelist[MAXFILES]; +const char *includedirs[MAXDIRS + 1], + **includedirsnext = includedirs; +char *notdotdot[MAXDIRS]; +static int cmdinc_count = 0; +static char *cmdinc_list[2 * MAXINCFILES]; +const char *objprefix = ""; +const char *objsuffix = OBJSUFFIX; +static const char *startat = "# DO NOT DELETE"; +int width = 78; +static boolean append = FALSE; +boolean printed = FALSE; +boolean verbose = FALSE; +boolean show_where_not = FALSE; + /* Warn on multiple includes of same file */ -boolean warn_multiple = FALSE; +boolean warn_multiple = FALSE; static void setfile_cmdinc(struct filepointer *filep, long count, char **list); static void redirect(const char *line, const char *makefile); static void _X_NORETURN -catch (int sig) +catch(int sig) { - fflush (stdout); - fatalerr ("got signal %d\n", sig); + fflush(stdout); + fatalerr("got signal %d\n", sig); } #if defined(USG) || (defined(i386) && defined(SYSV)) || defined(WIN32) || defined(Lynx_22) || defined(__CYGWIN__) @@ -137,694 +138,709 @@ static struct sigaction sig_act; int main(int argc, char *argv[]) { - char **fp = filelist; - const char **incp = includedirs; - char *p; - struct inclist *ip; - char *makefile = NULL; - struct filepointer *filecontent; - const struct symtab *psymp = predefs; - const char *endmarker = NULL; - char *defincdir = NULL; - char **undeflist = NULL; - int numundefs = 0, i; - - ProgramName = argv[0]; - - while (psymp->s_name) - { - define2(psymp->s_name, psymp->s_value, &maininclist); - psymp++; - } + char **fp = filelist; + const char **incp = includedirs; + char *p; + struct inclist *ip; + char *makefile = NULL; + struct filepointer *filecontent; + const struct symtab *psymp = predefs; + const char *endmarker = NULL; + char *defincdir = NULL; + char **undeflist = NULL; + int numundefs = 0, i; + + ProgramName = argv[0]; + + while (psymp->s_name) { + define2(psymp->s_name, psymp->s_value, &maininclist); + psymp++; + } #ifdef __sun - /* Solaris predefined values that are computed, not hardcoded */ - { - struct utsname name; - - if (uname(&name) >= 0) { - char osrevdef[SYS_NMLN + SYS_NMLN + 5]; - snprintf(osrevdef, sizeof(osrevdef), "__%s_%s", - name.sysname, name.release); - - for (p = osrevdef; *p != '\0'; p++) { - if (!isalnum(*p)) { - *p = '_'; - } - } - define2(osrevdef, "1", &maininclist); - } - } + /* Solaris predefined values that are computed, not hardcoded */ + { + struct utsname name; + + if (uname(&name) >= 0) { + char osrevdef[SYS_NMLN + SYS_NMLN + 5]; + + snprintf(osrevdef, sizeof(osrevdef), "__%s_%s", + name.sysname, name.release); + + for (p = osrevdef; *p != '\0'; p++) { + if (!isalnum(*p)) { + *p = '_'; + } + } + define2(osrevdef, "1", &maininclist); + } + } #endif - if (argc == 2 && argv[1][0] == '@') { - struct stat ast; - int afd; - char *args; - char **nargv; - int nargc; - char quotechar = '\0'; - - nargc = 1; - if ((afd = open(argv[1]+1, O_RDONLY)) < 0) - fatalerr("cannot open \"%s\"\n", argv[1]+1); - fstat(afd, &ast); - args = malloc(ast.st_size + 1); - if ((ast.st_size = read(afd, args, ast.st_size)) < 0) - fatalerr("failed to read %s\n", argv[1]+1); - args[ast.st_size] = '\0'; - close(afd); - for (p = args; *p; p++) { - if (quotechar) { - if (quotechar == '\\' || - (*p == quotechar && p[-1] != '\\')) - quotechar = '\0'; - continue; - } - switch (*p) { - case '\\': - case '"': - case '\'': - quotechar = *p; - break; - case ' ': - case '\n': - *p = '\0'; - if (p > args && p[-1]) - nargc++; - break; - } - } - if (p[-1]) - nargc++; - nargv = malloc(nargc * sizeof(char *)); - nargv[0] = argv[0]; - argc = 1; - for (p = args; argc < nargc; p += strlen(p) + 1) - if (*p) nargv[argc++] = p; - argv = nargv; - } - for(argc--, argv++; argc; argc--, argv++) { - /* if looking for endmarker then check before parsing */ - if (endmarker && strcmp (endmarker, *argv) == 0) { - endmarker = NULL; - continue; - } - if (**argv != '-') { - /* treat +thing as an option for C++ */ - if (endmarker && **argv == '+') - continue; - if (fp >= filelist + MAXFILES) { - fatalerr("Too many source files. Limit is %i files.\n", MAXFILES); - } - *fp++ = argv[0]; - continue; - } - switch(argv[0][1]) { - case '-': - endmarker = &argv[0][2]; - if (endmarker[0] == '\0') endmarker = "--"; - break; - case 'D': - if (argv[0][2] == '\0') { - if (argc < 2) - fatalerr("Missing argument for -D\n"); - argv++; - argc--; - } - for (p=argv[0] + 2; *p ; p++) - if (*p == '=') { - *p = ' '; - break; - } - define(argv[0] + 2, &maininclist); - break; - case 'I': - if (incp >= includedirs + MAXDIRS) - fatalerr("Too many -I flags.\n"); - *incp++ = argv[0]+2; - if (**(incp-1) == '\0') { - if (argc < 2) - fatalerr("Missing argument for -I\n"); - *(incp-1) = *(++argv); - argc--; - } - break; - case 'U': - /* Undef's override all -D's so save them up */ - numundefs++; - if (numundefs == 1) - undeflist = malloc(sizeof(char *)); - else - undeflist = realloc(undeflist, - numundefs * sizeof(char *)); - if (argv[0][2] == '\0') { - if (argc < 2) - fatalerr("Missing argument for -U\n"); - argv++; - argc--; - } - undeflist[numundefs - 1] = argv[0] + 2; - break; - case 'Y': - defincdir = argv[0]+2; - break; - /* do not use if endmarker processing */ - case 'a': - if (endmarker) break; - append = TRUE; - break; - case 'w': - if (endmarker) break; - if (argv[0][2] == '\0') { - if (argc < 2) - fatalerr("Missing argument for -w\n"); - argv++; - argc--; - width = atoi(argv[0]); - } else - width = atoi(argv[0]+2); - break; - case 'o': - if (endmarker) break; - if (argv[0][2] == '\0') { - if (argc < 2) - fatalerr("Missing argument for -o\n"); - argv++; - argc--; - objsuffix = argv[0]; - } else - objsuffix = argv[0]+2; - break; - case 'p': - if (endmarker) break; - if (argv[0][2] == '\0') { - if (argc < 2) - fatalerr("Missing argument for -p\n"); - argv++; - argc--; - objprefix = argv[0]; - } else - objprefix = argv[0]+2; - break; - case 'v': - if (endmarker) break; - verbose = TRUE; + if (argc == 2 && argv[1][0] == '@') { + struct stat ast; + int afd; + char *args; + char **nargv; + int nargc; + char quotechar = '\0'; + + nargc = 1; + if ((afd = open(argv[1] + 1, O_RDONLY)) < 0) + fatalerr("cannot open \"%s\"\n", argv[1] + 1); + fstat(afd, &ast); + args = malloc(ast.st_size + 1); + if ((ast.st_size = read(afd, args, ast.st_size)) < 0) + fatalerr("failed to read %s\n", argv[1] + 1); + args[ast.st_size] = '\0'; + close(afd); + for (p = args; *p; p++) { + if (quotechar) { + if (quotechar == '\\' || (*p == quotechar && p[-1] != '\\')) + quotechar = '\0'; + continue; + } + switch (*p) { + case '\\': + case '"': + case '\'': + quotechar = *p; + break; + case ' ': + case '\n': + *p = '\0'; + if (p > args && p[-1]) + nargc++; + break; + } + } + if (p[-1]) + nargc++; + nargv = malloc(nargc * sizeof(char *)); + nargv[0] = argv[0]; + argc = 1; + for (p = args; argc < nargc; p += strlen(p) + 1) + if (*p) + nargv[argc++] = p; + argv = nargv; + } + for (argc--, argv++; argc; argc--, argv++) { + /* if looking for endmarker then check before parsing */ + if (endmarker && strcmp(endmarker, *argv) == 0) { + endmarker = NULL; + continue; + } + if (**argv != '-') { + /* treat +thing as an option for C++ */ + if (endmarker && **argv == '+') + continue; + if (fp >= filelist + MAXFILES) { + fatalerr("Too many source files. Limit is %i files.\n", + MAXFILES); + } + *fp++ = argv[0]; + continue; + } + switch (argv[0][1]) { + case '-': + endmarker = &argv[0][2]; + if (endmarker[0] == '\0') + endmarker = "--"; + break; + case 'D': + if (argv[0][2] == '\0') { + if (argc < 2) + fatalerr("Missing argument for -D\n"); + argv++; + argc--; + } + for (p = argv[0] + 2; *p; p++) + if (*p == '=') { + *p = ' '; + break; + } + define(argv[0] + 2, &maininclist); + break; + case 'I': + if (incp >= includedirs + MAXDIRS) + fatalerr("Too many -I flags.\n"); + *incp++ = argv[0] + 2; + if (**(incp - 1) == '\0') { + if (argc < 2) + fatalerr("Missing argument for -I\n"); + *(incp - 1) = *(++argv); + argc--; + } + break; + case 'U': + /* Undef's override all -D's so save them up */ + numundefs++; + if (numundefs == 1) + undeflist = malloc(sizeof(char *)); + else + undeflist = realloc(undeflist, numundefs * sizeof(char *)); + if (argv[0][2] == '\0') { + if (argc < 2) + fatalerr("Missing argument for -U\n"); + argv++; + argc--; + } + undeflist[numundefs - 1] = argv[0] + 2; + break; + case 'Y': + defincdir = argv[0] + 2; + break; + /* do not use if endmarker processing */ + case 'a': + if (endmarker) + break; + append = TRUE; + break; + case 'w': + if (endmarker) + break; + if (argv[0][2] == '\0') { + if (argc < 2) + fatalerr("Missing argument for -w\n"); + argv++; + argc--; + width = atoi(argv[0]); + } + else + width = atoi(argv[0] + 2); + break; + case 'o': + if (endmarker) + break; + if (argv[0][2] == '\0') { + if (argc < 2) + fatalerr("Missing argument for -o\n"); + argv++; + argc--; + objsuffix = argv[0]; + } + else + objsuffix = argv[0] + 2; + break; + case 'p': + if (endmarker) + break; + if (argv[0][2] == '\0') { + if (argc < 2) + fatalerr("Missing argument for -p\n"); + argv++; + argc--; + objprefix = argv[0]; + } + else + objprefix = argv[0] + 2; + break; + case 'v': + if (endmarker) + break; + verbose = TRUE; #ifdef DEBUG - if (argv[0][2]) - _debugmask = atoi(argv[0]+2); + if (argv[0][2]) + _debugmask = atoi(argv[0] + 2); #endif - break; - case 's': - if (endmarker) break; - startat = argv[0]+2; - if (*startat == '\0') { - if (argc < 2) - fatalerr("Missing argument for -s\n"); - startat = *(++argv); - argc--; - } - if (*startat != '#') - fatalerr("-s flag's value should start %s\n", - "with '#'."); - break; - case 'f': - if (endmarker) break; - makefile = argv[0]+2; - if (*makefile == '\0') { - if (argc < 2) - fatalerr("Missing argument for -f\n"); - makefile = *(++argv); - argc--; - } - break; - - case 'm': - warn_multiple = TRUE; - break; - - /* Ignore -O, -g so we can just pass ${CFLAGS} to - makedepend - */ - case 'O': - case 'g': - break; - case 'i': - if (strcmp(&argv[0][1],"include") == 0) { - char *buf; - if (argc<2) - fatalerr("option -include is a " - "missing its parameter\n"); - if (cmdinc_count >= MAXINCFILES) - fatalerr("Too many -include flags.\n"); - argc--; - argv++; - buf = malloc(strlen(DASH_INC_PRE) + - strlen(argv[0]) + - strlen(DASH_INC_POST) + 1); - if(!buf) - fatalerr("out of memory at " - "-include string\n"); - cmdinc_list[2 * cmdinc_count + 0] = argv[0]; - cmdinc_list[2 * cmdinc_count + 1] = buf; - cmdinc_count++; - break; - } - /* intentional fall through */ - default: - if (endmarker) break; - /* fatalerr("unknown opt = %s\n", argv[0]); */ - warning("ignoring option %s\n", argv[0]); - } - } - /* Now do the undefs from the command line */ - for (i = 0; i < numundefs; i++) - undefine(undeflist[i], &maininclist); - if (numundefs > 0) - free(undeflist); - - if (!defincdir) { + break; + case 's': + if (endmarker) + break; + startat = argv[0] + 2; + if (*startat == '\0') { + if (argc < 2) + fatalerr("Missing argument for -s\n"); + startat = *(++argv); + argc--; + } + if (*startat != '#') + fatalerr("-s flag's value should start %s\n", "with '#'."); + break; + case 'f': + if (endmarker) + break; + makefile = argv[0] + 2; + if (*makefile == '\0') { + if (argc < 2) + fatalerr("Missing argument for -f\n"); + makefile = *(++argv); + argc--; + } + break; + + case 'm': + warn_multiple = TRUE; + break; + + /* Ignore -O, -g so we can just pass ${CFLAGS} to + makedepend + */ + case 'O': + case 'g': + break; + case 'i': + if (strcmp(&argv[0][1], "include") == 0) { + char *buf; + + if (argc < 2) + fatalerr("option -include is a " "missing its parameter\n"); + if (cmdinc_count >= MAXINCFILES) + fatalerr("Too many -include flags.\n"); + argc--; + argv++; + buf = malloc(strlen(DASH_INC_PRE) + + strlen(argv[0]) + strlen(DASH_INC_POST) + 1); + if (!buf) + fatalerr("out of memory at " "-include string\n"); + cmdinc_list[2 * cmdinc_count + 0] = argv[0]; + cmdinc_list[2 * cmdinc_count + 1] = buf; + cmdinc_count++; + break; + } + /* intentional fall through */ + default: + if (endmarker) + break; + /* fatalerr("unknown opt = %s\n", argv[0]); */ + warning("ignoring option %s\n", argv[0]); + } + } + /* Now do the undefs from the command line */ + for (i = 0; i < numundefs; i++) + undefine(undeflist[i], &maininclist); + if (numundefs > 0) + free(undeflist); + + if (!defincdir) { #ifdef PREINCDIR - if (incp >= includedirs + MAXDIRS) - fatalerr("Too many -I flags.\n"); - *incp++ = PREINCDIR; + if (incp >= includedirs + MAXDIRS) + fatalerr("Too many -I flags.\n"); + *incp++ = PREINCDIR; #endif - if (incp >= includedirs + MAXDIRS) - fatalerr("Too many -I flags.\n"); - *incp++ = INCLUDEDIR; + if (incp >= includedirs + MAXDIRS) + fatalerr("Too many -I flags.\n"); + *incp++ = INCLUDEDIR; #ifdef EXTRAINCDIR - if (incp >= includedirs + MAXDIRS) - fatalerr("Too many -I flags.\n"); - *incp++ = EXTRAINCDIR; + if (incp >= includedirs + MAXDIRS) + fatalerr("Too many -I flags.\n"); + *incp++ = EXTRAINCDIR; #endif #ifdef POSTINCDIR - if (incp >= includedirs + MAXDIRS) - fatalerr("Too many -I flags.\n"); - *incp++ = POSTINCDIR; + if (incp >= includedirs + MAXDIRS) + fatalerr("Too many -I flags.\n"); + *incp++ = POSTINCDIR; #endif - } else if (*defincdir) { - if (incp >= includedirs + MAXDIRS) - fatalerr("Too many -I flags.\n"); - *incp++ = defincdir; - } + } + else if (*defincdir) { + if (incp >= includedirs + MAXDIRS) + fatalerr("Too many -I flags.\n"); + *incp++ = defincdir; + } - redirect(startat, makefile); + redirect(startat, makefile); - /* - * catch signals. - */ + /* + * catch signals. + */ #ifdef USGISH /* should really reset SIGINT to SIG_IGN if it was. */ #ifdef SIGHUP - signal (SIGHUP, catch); + signal(SIGHUP, catch); #endif - signal (SIGINT, catch); + signal(SIGINT, catch); #ifdef SIGQUIT - signal (SIGQUIT, catch); + signal(SIGQUIT, catch); #endif - signal (SIGILL, catch); + signal(SIGILL, catch); #ifdef SIGBUS - signal (SIGBUS, catch); + signal(SIGBUS, catch); #endif - signal (SIGSEGV, catch); + signal(SIGSEGV, catch); #ifdef SIGSYS - signal (SIGSYS, catch); + signal(SIGSYS, catch); #endif #else - sig_act.sa_handler = catch; + sig_act.sa_handler = catch; #if defined(_POSIX_SOURCE) || !defined(X_NOT_POSIX) - sigemptyset(&sig_act.sa_mask); - sigaddset(&sig_act.sa_mask, SIGINT); - sigaddset(&sig_act.sa_mask, SIGQUIT); + sigemptyset(&sig_act.sa_mask); + sigaddset(&sig_act.sa_mask, SIGINT); + sigaddset(&sig_act.sa_mask, SIGQUIT); #ifdef SIGBUS - sigaddset(&sig_act.sa_mask, SIGBUS); + sigaddset(&sig_act.sa_mask, SIGBUS); #endif - sigaddset(&sig_act.sa_mask, SIGILL); - sigaddset(&sig_act.sa_mask, SIGSEGV); - sigaddset(&sig_act.sa_mask, SIGHUP); - sigaddset(&sig_act.sa_mask, SIGPIPE); + sigaddset(&sig_act.sa_mask, SIGILL); + sigaddset(&sig_act.sa_mask, SIGSEGV); + sigaddset(&sig_act.sa_mask, SIGHUP); + sigaddset(&sig_act.sa_mask, SIGPIPE); #ifdef SIGSYS - sigaddset(&sig_act.sa_mask, SIGSYS); + sigaddset(&sig_act.sa_mask, SIGSYS); #endif #else - sig_act.sa_mask = ((1<<(SIGINT -1)) - |(1<<(SIGQUIT-1)) + sig_act.sa_mask = ((1 << (SIGINT - 1)) + | (1 << (SIGQUIT - 1)) #ifdef SIGBUS - |(1<<(SIGBUS-1)) + | (1 << (SIGBUS - 1)) #endif - |(1<<(SIGILL-1)) - |(1<<(SIGSEGV-1)) - |(1<<(SIGHUP-1)) - |(1<<(SIGPIPE-1)) + | (1 << (SIGILL - 1)) + | (1 << (SIGSEGV - 1)) + | (1 << (SIGHUP - 1)) + | (1 << (SIGPIPE - 1)) #ifdef SIGSYS - |(1<<(SIGSYS-1)) + | (1 << (SIGSYS - 1)) #endif - ); -#endif /* _POSIX_SOURCE */ - sig_act.sa_flags = 0; - sigaction(SIGHUP, &sig_act, (struct sigaction *)0); - sigaction(SIGINT, &sig_act, (struct sigaction *)0); - sigaction(SIGQUIT, &sig_act, (struct sigaction *)0); - sigaction(SIGILL, &sig_act, (struct sigaction *)0); + ); +#endif /* _POSIX_SOURCE */ + sig_act.sa_flags = 0; + sigaction(SIGHUP, &sig_act, (struct sigaction *) 0); + sigaction(SIGINT, &sig_act, (struct sigaction *) 0); + sigaction(SIGQUIT, &sig_act, (struct sigaction *) 0); + sigaction(SIGILL, &sig_act, (struct sigaction *) 0); #ifdef SIGBUS - sigaction(SIGBUS, &sig_act, (struct sigaction *)0); + sigaction(SIGBUS, &sig_act, (struct sigaction *) 0); #endif - sigaction(SIGSEGV, &sig_act, (struct sigaction *)0); + sigaction(SIGSEGV, &sig_act, (struct sigaction *) 0); #ifdef SIGSYS - sigaction(SIGSYS, &sig_act, (struct sigaction *)0); + sigaction(SIGSYS, &sig_act, (struct sigaction *) 0); #endif -#endif /* USGISH */ - - /* - * now peruse through the list of files. - */ - for(fp=filelist; *fp; fp++) { - DBG_PRINT(stderr,"file: %s\n",*fp); - filecontent = getfile(*fp); - setfile_cmdinc(filecontent, cmdinc_count, cmdinc_list); - ip = newinclude(*fp, (char *)NULL); - - find_includes(filecontent, ip, ip, 0, FALSE); - freefile(filecontent); - recursive_pr_include(ip, ip->i_file, base_name(*fp)); - inc_clean(); - } - if (printed) - printf("\n"); - return 0; +#endif /* USGISH */ + + /* + * now peruse through the list of files. + */ + for (fp = filelist; *fp; fp++) { + DBG_PRINT(stderr, "file: %s\n", *fp); + filecontent = getfile(*fp); + setfile_cmdinc(filecontent, cmdinc_count, cmdinc_list); + ip = newinclude(*fp, (char *) NULL); + + find_includes(filecontent, ip, ip, 0, FALSE); + freefile(filecontent); + recursive_pr_include(ip, ip->i_file, base_name(*fp)); + inc_clean(); + } + if (printed) + printf("\n"); + return 0; } - struct filepointer * getfile(const char *file) { - int fd; - struct filepointer *content; - struct stat st; - - content = malloc(sizeof(struct filepointer)); - content->f_name = file; - if ((fd = open(file, O_RDONLY)) < 0) { - warning("cannot open \"%s\"\n", file); - content->f_p = content->f_base = content->f_end = malloc(1); - *content->f_p = '\0'; - return(content); - } - fstat(fd, &st); - content->f_base = malloc(st.st_size+1); - if (content->f_base == NULL) - fatalerr("cannot allocate mem\n"); - if ((st.st_size = read(fd, content->f_base, st.st_size)) < 0) - fatalerr("failed to read %s\n", file); - close(fd); - content->f_len = st.st_size+1; - content->f_p = content->f_base; - content->f_end = content->f_base + st.st_size; - *content->f_end = '\0'; - content->f_line = 0; - content->cmdinc_count = 0; - content->cmdinc_list = NULL; - content->cmdinc_line = 0; - return(content); + int fd; + struct filepointer *content; + struct stat st; + + content = malloc(sizeof(struct filepointer)); + content->f_name = file; + if ((fd = open(file, O_RDONLY)) < 0) { + warning("cannot open \"%s\"\n", file); + content->f_p = content->f_base = content->f_end = malloc(1); + *content->f_p = '\0'; + return (content); + } + fstat(fd, &st); + content->f_base = malloc(st.st_size + 1); + if (content->f_base == NULL) + fatalerr("cannot allocate mem\n"); + if ((st.st_size = read(fd, content->f_base, st.st_size)) < 0) + fatalerr("failed to read %s\n", file); + close(fd); + content->f_len = st.st_size + 1; + content->f_p = content->f_base; + content->f_end = content->f_base + st.st_size; + *content->f_end = '\0'; + content->f_line = 0; + content->cmdinc_count = 0; + content->cmdinc_list = NULL; + content->cmdinc_line = 0; + return (content); } void -setfile_cmdinc(struct filepointer* filep, long count, char** list) +setfile_cmdinc(struct filepointer *filep, long count, char **list) { - filep->cmdinc_count = count; - filep->cmdinc_list = list; - filep->cmdinc_line = 0; + filep->cmdinc_count = count; + filep->cmdinc_list = list; + filep->cmdinc_line = 0; } void freefile(struct filepointer *fp) { - free(fp->f_base); - free(fp); + free(fp->f_base); + free(fp); } int -match(const char *str, const char * const *list) +match(const char *str, const char *const *list) { - int i; + int i; - for (i=0; *list; i++, list++) - if (strcmp(str, *list) == 0) - return(i); - return(-1); + for (i = 0; *list; i++, list++) + if (strcmp(str, *list) == 0) + return (i); + return (-1); } /* * Get the next line. We only return lines beginning with '#' since that * is all this program is ever interested in. */ -char *getnextline(struct filepointer *filep) +char * +getnextline(struct filepointer *filep) { - char *p, /* walking pointer */ - *eof, /* end of file pointer */ - *bol; /* beginning of line pointer */ - int lineno; /* line number */ - - /* - * Fake the "-include" line files in form of #include to the - * start of each file. - */ - if (filep->cmdinc_line < filep->cmdinc_count) { - char *inc = filep->cmdinc_list[2 * filep->cmdinc_line + 0]; - char *buf = filep->cmdinc_list[2 * filep->cmdinc_line + 1]; - filep->cmdinc_line++; - sprintf(buf,"%s%s%s",DASH_INC_PRE,inc,DASH_INC_POST); - DBG_PRINT(stderr,"%s\n",buf); - return(buf); - } - - p = filep->f_p; - eof = filep->f_end; - if (p >= eof) - return((char *)NULL); - lineno = filep->f_line; - - for (bol = p--; ++p < eof; ) { - if ((bol == p) && ((*p == ' ') || (*p == '\t'))) - { - /* Consume leading white-spaces for this line */ - while (((p+1) < eof) && ((*p == ' ') || (*p == '\t'))) - { - p++; - bol++; - } - } - - if (*p == '/' && (p+1) < eof && *(p+1) == '*') { - /* Consume C comments */ - *(p++) = ' '; - *(p++) = ' '; - while (p < eof && *p) { - if (*p == '*' && (p+1) < eof && *(p+1) == '/') { - *(p++) = ' '; - *(p++) = ' '; - break; - } - if (*p == '\n') - lineno++; - *(p++) = ' '; - } - --p; - } - else if (*p == '/' && (p+1) < eof && *(p+1) == '/') { - /* Consume C++ comments */ - *(p++) = ' '; - *(p++) = ' '; - while (p < eof && *p) { - if (*p == '\\' && (p+1) < eof && - *(p+1) == '\n') { - *(p++) = ' '; - lineno++; - } - else if (*p == '?' && (p+3) < eof && - *(p+1) == '?' && - *(p+2) == '/' && - *(p+3) == '\n') { - *(p++) = ' '; - *(p++) = ' '; - *(p++) = ' '; - lineno++; - } - else if (*p == '\n') - break; /* to process end of line */ - *(p++) = ' '; - } - --p; - } - else if (*p == '\\' && (p+1) < eof && *(p+1) == '\n') { - /* Consume backslash line terminations */ - *(p++) = ' '; - *p = ' '; - lineno++; - } - else if (*p == '?' && (p+3) < eof && - *(p+1) == '?' && *(p+2) == '/' && *(p+3) == '\n') { - /* Consume trigraph'ed backslash line terminations */ - *(p++) = ' '; - *(p++) = ' '; - *(p++) = ' '; - *p = ' '; - lineno++; - } - else if (*p == '\n') { - lineno++; - if (*bol == '#') { - char *cp; - - *(p++) = '\0'; - /* punt lines with just # (yacc generated) */ - for (cp = bol+1; - *cp && (*cp == ' ' || *cp == '\t'); cp++); - if (*cp) goto done; - --p; - } - bol = p+1; - } - } - if (*bol != '#') - bol = NULL; -done: - filep->f_p = p; - filep->f_line = lineno; + char *p, /* walking pointer */ + *eof, /* end of file pointer */ + *bol; /* beginning of line pointer */ + int lineno; /* line number */ + + /* + * Fake the "-include" line files in form of #include to the + * start of each file. + */ + if (filep->cmdinc_line < filep->cmdinc_count) { + char *inc = filep->cmdinc_list[2 * filep->cmdinc_line + 0]; + char *buf = filep->cmdinc_list[2 * filep->cmdinc_line + 1]; + + filep->cmdinc_line++; + sprintf(buf, "%s%s%s", DASH_INC_PRE, inc, DASH_INC_POST); + DBG_PRINT(stderr, "%s\n", buf); + return (buf); + } + + p = filep->f_p; + eof = filep->f_end; + if (p >= eof) + return ((char *) NULL); + lineno = filep->f_line; + + for (bol = p--; ++p < eof;) { + if ((bol == p) && ((*p == ' ') || (*p == '\t'))) { + /* Consume leading white-spaces for this line */ + while (((p + 1) < eof) && ((*p == ' ') || (*p == '\t'))) { + p++; + bol++; + } + } + + if (*p == '/' && (p + 1) < eof && *(p + 1) == '*') { + /* Consume C comments */ + *(p++) = ' '; + *(p++) = ' '; + while (p < eof && *p) { + if (*p == '*' && (p + 1) < eof && *(p + 1) == '/') { + *(p++) = ' '; + *(p++) = ' '; + break; + } + if (*p == '\n') + lineno++; + *(p++) = ' '; + } + --p; + } + else if (*p == '/' && (p + 1) < eof && *(p + 1) == '/') { + /* Consume C++ comments */ + *(p++) = ' '; + *(p++) = ' '; + while (p < eof && *p) { + if (*p == '\\' && (p + 1) < eof && *(p + 1) == '\n') { + *(p++) = ' '; + lineno++; + } + else if (*p == '?' && (p + 3) < eof && + *(p + 1) == '?' && + *(p + 2) == '/' && *(p + 3) == '\n') { + *(p++) = ' '; + *(p++) = ' '; + *(p++) = ' '; + lineno++; + } + else if (*p == '\n') + break; /* to process end of line */ + *(p++) = ' '; + } + --p; + } + else if (*p == '\\' && (p + 1) < eof && *(p + 1) == '\n') { + /* Consume backslash line terminations */ + *(p++) = ' '; + *p = ' '; + lineno++; + } + else if (*p == '?' && (p + 3) < eof && + *(p + 1) == '?' && *(p + 2) == '/' && *(p + 3) == '\n') { + /* Consume trigraph'ed backslash line terminations */ + *(p++) = ' '; + *(p++) = ' '; + *(p++) = ' '; + *p = ' '; + lineno++; + } + else if (*p == '\n') { + lineno++; + if (*bol == '#') { + char *cp; + + *(p++) = '\0'; + /* punt lines with just # (yacc generated) */ + for (cp = bol + 1; *cp && (*cp == ' ' || *cp == '\t'); cp++); + if (*cp) + goto done; + --p; + } + bol = p + 1; + } + } + if (*bol != '#') + bol = NULL; + done: + filep->f_p = p; + filep->f_line = lineno; #ifdef DEBUG_DUMP - if (bol) - DBG_PRINT(stderr,"%s\n",bol); + if (bol) + DBG_PRINT(stderr, "%s\n", bol); #endif - return(bol); + return (bol); } /* * Strip the file name down to what we want to see in the Makefile. * It will have objprefix and objsuffix around it. */ -char *base_name(const char *in_file) +char * +base_name(const char *in_file) { - char *p; - char *file = strdup(in_file); - for(p=file+strlen(file); p>file && *p != '.'; p--) ; + char *p; + char *file = strdup(in_file); - if (*p == '.') - *p = '\0'; - return(file); + for (p = file + strlen(file); p > file && *p != '.'; p--); + + if (*p == '.') + *p = '\0'; + return (file); } #ifndef HAVE_RENAME -int rename (char *from, char *to) +int +rename(char *from, char *to) { - (void) unlink (to); - if (link (from, to) == 0) { - unlink (from); - return 0; - } else { - return -1; + (void) unlink(to); + if (link(from, to) == 0) { + unlink(from); + return 0; + } + else { + return -1; } } -#endif /* !HAVE_RENAME */ +#endif /* !HAVE_RENAME */ static void redirect(const char *line, const char *makefile) { - struct stat st; - FILE *fdin, *fdout; - char backup[ BUFSIZ ], - buf[ BUFSIZ ]; - boolean found = FALSE; - size_t len; - - /* - * if makefile is "-" then let it pour onto stdout. - */ - if (makefile && *makefile == '-' && *(makefile+1) == '\0') { - puts(line); - return; - } - - /* - * use a default if makefile is not specified. - */ - if (!makefile) { - if (stat("Makefile", &st) == 0) - makefile = "Makefile"; - else if (stat("makefile", &st) == 0) - makefile = "makefile"; - else - fatalerr("[mM]akefile is not present\n"); - } - else { - if (stat(makefile, &st) != 0) - fatalerr("\"%s\" is not present\n", makefile); - } - - snprintf(backup, sizeof(backup), "%s.bak", makefile); - unlink(backup); - - /* rename() won't work on WIN32, CYGWIN, or CIFS if src file is open */ - if (rename(makefile, backup) < 0) - fatalerr("cannot rename %s to %s\n", makefile, backup); - if ((fdin = fopen(backup, "r")) == NULL) { - if (rename(backup, makefile) < 0) - warning("renamed %s to %s, but can't move it back\n", - makefile, backup); - fatalerr("cannot open \"%s\"\n", makefile); - } - if ((fdout = freopen(makefile, "w", stdout)) == NULL) - fatalerr("cannot open \"%s\"\n", backup); - len = strlen(line); - while (!found && fgets(buf, BUFSIZ, fdin)) { - if (*buf == '#' && strncmp(line, buf, len) == 0) - found = TRUE; - fputs(buf, fdout); - } - if (!found) { - if (verbose) - warning("Adding new delimiting line \"%s\" and dependencies...\n", - line); - puts(line); /* same as fputs(fdout); but with newline */ - } else if (append) { - while (fgets(buf, BUFSIZ, fdin)) { - fputs(buf, fdout); - } - } - fclose(fdin); - fflush(fdout); + struct stat st; + FILE *fdin, *fdout; + char backup[BUFSIZ], + buf[BUFSIZ]; + boolean found = FALSE; + size_t len; + + /* + * if makefile is "-" then let it pour onto stdout. + */ + if (makefile && *makefile == '-' && *(makefile + 1) == '\0') { + puts(line); + return; + } + + /* + * use a default if makefile is not specified. + */ + if (!makefile) { + if (stat("Makefile", &st) == 0) + makefile = "Makefile"; + else if (stat("makefile", &st) == 0) + makefile = "makefile"; + else + fatalerr("[mM]akefile is not present\n"); + } + else { + if (stat(makefile, &st) != 0) + fatalerr("\"%s\" is not present\n", makefile); + } + + snprintf(backup, sizeof(backup), "%s.bak", makefile); + unlink(backup); + + /* rename() won't work on WIN32, CYGWIN, or CIFS if src file is open */ + if (rename(makefile, backup) < 0) + fatalerr("cannot rename %s to %s\n", makefile, backup); + if ((fdin = fopen(backup, "r")) == NULL) { + if (rename(backup, makefile) < 0) + warning("renamed %s to %s, but can't move it back\n", + makefile, backup); + fatalerr("cannot open \"%s\"\n", makefile); + } + if ((fdout = freopen(makefile, "w", stdout)) == NULL) + fatalerr("cannot open \"%s\"\n", backup); + len = strlen(line); + while (!found && fgets(buf, BUFSIZ, fdin)) { + if (*buf == '#' && strncmp(line, buf, len) == 0) + found = TRUE; + fputs(buf, fdout); + } + if (!found) { + if (verbose) + warning("Adding new delimiting line \"%s\" and dependencies...\n", + line); + puts(line); /* same as fputs(fdout); but with newline */ + } + else if (append) { + while (fgets(buf, BUFSIZ, fdin)) { + fputs(buf, fdout); + } + } + fclose(fdin); + fflush(fdout); #ifndef HAVE_FCHMOD - chmod(makefile, st.st_mode); + chmod(makefile, st.st_mode); #else - fchmod(fileno(fdout), st.st_mode); -#endif /* HAVE_FCHMOD */ + fchmod(fileno(fdout), st.st_mode); +#endif /* HAVE_FCHMOD */ } void fatalerr(const char *msg, ...) { - va_list args; - fprintf(stderr, "%s: error: ", ProgramName); - va_start(args, msg); - vfprintf(stderr, msg, args); - va_end(args); - exit (1); + va_list args; + + fprintf(stderr, "%s: error: ", ProgramName); + va_start(args, msg); + vfprintf(stderr, msg, args); + va_end(args); + exit(1); } void warning(const char *msg, ...) { - va_list args; - fprintf(stderr, "%s: warning: ", ProgramName); - va_start(args, msg); - vfprintf(stderr, msg, args); - va_end(args); + va_list args; + + fprintf(stderr, "%s: warning: ", ProgramName); + va_start(args, msg); + vfprintf(stderr, msg, args); + va_end(args); } void warning1(const char *msg, ...) { - va_list args; - va_start(args, msg); - vfprintf(stderr, msg, args); - va_end(args); + va_list args; + + va_start(args, msg); + vfprintf(stderr, msg, args); + va_end(args); } diff --git a/parse.c b/parse.c index 1e2f263..9342dcf 100644 --- a/parse.c +++ b/parse.c @@ -26,290 +26,284 @@ in this Software without prior written authorization from The Open Group. #include "def.h" -static int deftype (char *line, struct filepointer *filep, - struct inclist *file_red, struct inclist *file, - int parse_it); +static int deftype(char *line, struct filepointer *filep, + struct inclist *file_red, struct inclist *file, + int parse_it); static int zero_value(char *filename, char *exp, struct filepointer *filep, - struct inclist *file_red); + struct inclist *file_red); static int merge2defines(struct inclist *file1, struct inclist *file2); static int gobble(struct filepointer *filep, struct inclist *file, struct inclist *file_red) { - char *line; - int type; - - while ((line = getnextline(filep))) { - switch(type = deftype(line, filep, file_red, file, FALSE)) { - case IF: - case IFFALSE: - case IFGUESSFALSE: - case IFDEF: - case IFNDEF: - type = gobble(filep, file, file_red); - while ((type == ELIF) || (type == ELIFFALSE) || - (type == ELIFGUESSFALSE)) - type = gobble(filep, file, file_red); - if (type == ELSE) - (void)gobble(filep, file, file_red); - break; - case ELSE: - case ENDIF: - debug(0,("%s, line %d: #%s\n", - file->i_file, filep->f_line, - directives[type])); - return(type); - case DEFINE: - case UNDEF: - case INCLUDE: - case INCLUDEDOT: - case PRAGMA: - case ERROR: - case IDENT: - case SCCS: - case EJECT: - case WARNING: - case INCLUDENEXT: - case INCLUDENEXTDOT: - break; - case ELIF: - case ELIFFALSE: - case ELIFGUESSFALSE: - return(type); - case -1: - warning("%s", file_red->i_file); - if (file_red != file) - warning1(" (reading %s)", file->i_file); - warning1(", line %ld: unknown directive == \"%s\"\n", - filep->f_line, line); - break; - } - } - return(-1); + char *line; + int type; + + while ((line = getnextline(filep))) { + switch (type = deftype(line, filep, file_red, file, FALSE)) { + case IF: + case IFFALSE: + case IFGUESSFALSE: + case IFDEF: + case IFNDEF: + type = gobble(filep, file, file_red); + while ((type == ELIF) || (type == ELIFFALSE) || + (type == ELIFGUESSFALSE)) + type = gobble(filep, file, file_red); + if (type == ELSE) + (void) gobble(filep, file, file_red); + break; + case ELSE: + case ENDIF: + debug(0, ("%s, line %d: #%s\n", + file->i_file, filep->f_line, directives[type])); + return (type); + case DEFINE: + case UNDEF: + case INCLUDE: + case INCLUDEDOT: + case PRAGMA: + case ERROR: + case IDENT: + case SCCS: + case EJECT: + case WARNING: + case INCLUDENEXT: + case INCLUDENEXTDOT: + break; + case ELIF: + case ELIFFALSE: + case ELIFGUESSFALSE: + return (type); + case -1: + warning("%s", file_red->i_file); + if (file_red != file) + warning1(" (reading %s)", file->i_file); + warning1(", line %ld: unknown directive == \"%s\"\n", + filep->f_line, line); + break; + } + } + return (-1); } /* * Decide what type of # directive this line is. */ static int -deftype (char *line, struct filepointer *filep, - struct inclist *file_red, struct inclist *file, int parse_it) +deftype(char *line, struct filepointer *filep, + struct inclist *file_red, struct inclist *file, int parse_it) { - register char *p; - char *directive, savechar, *q; - register int ret; - - /* - * Parse the directive... - */ - directive=line+1; - while (*directive == ' ' || *directive == '\t') - directive++; - - p = directive; - while ((*p == '_') || (*p >= 'a' && *p <= 'z')) - p++; - savechar = *p; - *p = '\0'; - ret = match(directive, directives); - *p = savechar; - - /* If we don't recognize this compiler directive or we happen to just - * be gobbling up text while waiting for an #endif or #elif or #else - * in the case of an #elif we must check the zero_value and return an - * ELIF or an ELIFFALSE. - */ - - if (ret == ELIF && !parse_it) - { - while (*p == ' ' || *p == '\t') - p++; - /* - * parse an expression. - */ - debug(0,("%s, line %d: #elif %s ", - file->i_file, filep->f_line, p)); - ret = zero_value(file->i_file, p, filep, file_red); - if (ret != IF) - { - debug(0,("false...\n")); - if (ret == IFFALSE) - return(ELIFFALSE); - else - return(ELIFGUESSFALSE); - } - else - { - debug(0,("true...\n")); - return(ELIF); - } - } - - if (ret < 0 || ! parse_it) - return(ret); - - /* - * now decide how to parse the directive, and do it. - */ - while (*p == ' ' || *p == '\t') - p++; - q = p + strlen(p); - do { - q--; - } while (*q == ' ' || *q == '\t'); - q[1] = '\0'; - switch (ret) { - case IF: - /* - * parse an expression. - */ - ret = zero_value(file->i_file, p, filep, file_red); - debug(0,("%s, line %d: %s #if %s\n", - file->i_file, filep->f_line, ret?"false":"true", p)); - break; - case IFDEF: - case IFNDEF: - debug(0,("%s, line %d: #%s %s\n", - file->i_file, filep->f_line, directives[ret], p)); - case UNDEF: - /* - * separate the name of a single symbol. - */ - while (isalnum(*p) || *p == '_') - *line++ = *p++; - *line = '\0'; - break; - case INCLUDE: - case INCLUDENEXT: - debug(2,("%s, line %d: #include%s %s\n", - file->i_file, filep->f_line, - (ret == INCLUDE) ? "" : "_next", p)); - - /* Support ANSI macro substitution */ - while (1) { - struct symtab **sym; - - if (!*p || *p == '"' || *p == '<') - break; - - sym = isdefined(p, file_red, NULL); - if (!sym) - break; - - p = (*sym)->s_value; - debug(3,("%s : #includes SYMBOL %s = %s\n", - file->i_incstring, - (*sym) -> s_name, - (*sym) -> s_value)); - /* mark file as having included a 'soft include' */ - file->i_flags |= INCLUDED_SYM; - } - - /* - * Separate the name of the include file. - */ - while (*p && *p != '"' && *p != '<') - p++; - if (! *p) - return(-2); - if (*p++ == '"') { - if (ret == INCLUDE) - ret = INCLUDEDOT; - else - ret = INCLUDENEXTDOT; - while (*p && *p != '"') - *line++ = *p++; - } else - while (*p && *p != '>') - *line++ = *p++; - *line = '\0'; - break; - case DEFINE: - /* - * copy the definition back to the beginning of the line. - */ - memmove (line, p, strlen(p) + 1); - break; - case ELSE: - case ENDIF: - case ELIF: - case PRAGMA: - case ERROR: - case IDENT: - case SCCS: - case EJECT: - case WARNING: - debug(0,("%s, line %d: #%s\n", - file->i_file, filep->f_line, directives[ret])); - /* - * nothing to do. - */ - break; - } - return(ret); + register char *p; + char *directive, savechar, *q; + register int ret; + + /* + * Parse the directive... + */ + directive = line + 1; + while (*directive == ' ' || *directive == '\t') + directive++; + + p = directive; + while ((*p == '_') || (*p >= 'a' && *p <= 'z')) + p++; + savechar = *p; + *p = '\0'; + ret = match(directive, directives); + *p = savechar; + + /* If we don't recognize this compiler directive or we happen to just + * be gobbling up text while waiting for an #endif or #elif or #else + * in the case of an #elif we must check the zero_value and return an + * ELIF or an ELIFFALSE. + */ + + if (ret == ELIF && !parse_it) { + while (*p == ' ' || *p == '\t') + p++; + /* + * parse an expression. + */ + debug(0, ("%s, line %d: #elif %s ", file->i_file, filep->f_line, p)); + ret = zero_value(file->i_file, p, filep, file_red); + if (ret != IF) { + debug(0, ("false...\n")); + if (ret == IFFALSE) + return (ELIFFALSE); + else + return (ELIFGUESSFALSE); + } + else { + debug(0, ("true...\n")); + return (ELIF); + } + } + + if (ret < 0 || !parse_it) + return (ret); + + /* + * now decide how to parse the directive, and do it. + */ + while (*p == ' ' || *p == '\t') + p++; + q = p + strlen(p); + do { + q--; + } while (*q == ' ' || *q == '\t'); + q[1] = '\0'; + switch (ret) { + case IF: + /* + * parse an expression. + */ + ret = zero_value(file->i_file, p, filep, file_red); + debug(0, ("%s, line %d: %s #if %s\n", + file->i_file, filep->f_line, ret ? "false" : "true", p)); + break; + case IFDEF: + case IFNDEF: + debug(0, ("%s, line %d: #%s %s\n", + file->i_file, filep->f_line, directives[ret], p)); + case UNDEF: + /* + * separate the name of a single symbol. + */ + while (isalnum(*p) || *p == '_') + *line++ = *p++; + *line = '\0'; + break; + case INCLUDE: + case INCLUDENEXT: + debug(2, ("%s, line %d: #include%s %s\n", + file->i_file, filep->f_line, + (ret == INCLUDE) ? "" : "_next", p)); + + /* Support ANSI macro substitution */ + while (1) { + struct symtab **sym; + + if (!*p || *p == '"' || *p == '<') + break; + + sym = isdefined(p, file_red, NULL); + if (!sym) + break; + + p = (*sym)->s_value; + debug(3, ("%s : #includes SYMBOL %s = %s\n", + file->i_incstring, (*sym)->s_name, (*sym)->s_value)); + /* mark file as having included a 'soft include' */ + file->i_flags |= INCLUDED_SYM; + } + + /* + * Separate the name of the include file. + */ + while (*p && *p != '"' && *p != '<') + p++; + if (!*p) + return (-2); + if (*p++ == '"') { + if (ret == INCLUDE) + ret = INCLUDEDOT; + else + ret = INCLUDENEXTDOT; + while (*p && *p != '"') + *line++ = *p++; + } + else + while (*p && *p != '>') + *line++ = *p++; + *line = '\0'; + break; + case DEFINE: + /* + * copy the definition back to the beginning of the line. + */ + memmove(line, p, strlen(p) + 1); + break; + case ELSE: + case ENDIF: + case ELIF: + case PRAGMA: + case ERROR: + case IDENT: + case SCCS: + case EJECT: + case WARNING: + debug(0, ("%s, line %d: #%s\n", + file->i_file, filep->f_line, directives[ret])); + /* + * nothing to do. + */ + break; + } + return (ret); } struct symtab ** fdefined(const char *symbol, struct inclist *file, struct inclist **srcfile) { - struct inclist **ip; - struct symtab **val; - int i; - static int recurse_lvl = 0; - - if (file->i_flags & DEFCHECKED) - return(NULL); - debug(2,("Looking for %s in %s\n", symbol, file->i_file)); - file->i_flags |= DEFCHECKED; - if ((val = slookup(symbol, file))) - debug(1,("%s defined in %s as %s\n", - symbol, file->i_file, (*val)->s_value)); - if (val == NULL && file->i_list) - { - for (ip = file->i_list, i=0; i < file->i_listlen; i++, ip++) - if (file->i_merged[i]==FALSE) { - val = fdefined(symbol, *ip, srcfile); - file->i_merged[i]=merge2defines(file,*ip); - if (val!=NULL) break; - } - } - else if (val != NULL && srcfile != NULL) *srcfile = file; - recurse_lvl--; - file->i_flags &= ~DEFCHECKED; - - return(val); + struct inclist **ip; + struct symtab **val; + int i; + static int recurse_lvl = 0; + + if (file->i_flags & DEFCHECKED) + return (NULL); + debug(2, ("Looking for %s in %s\n", symbol, file->i_file)); + file->i_flags |= DEFCHECKED; + if ((val = slookup(symbol, file))) + debug(1, ("%s defined in %s as %s\n", + symbol, file->i_file, (*val)->s_value)); + if (val == NULL && file->i_list) { + for (ip = file->i_list, i = 0; i < file->i_listlen; i++, ip++) + if (file->i_merged[i] == FALSE) { + val = fdefined(symbol, *ip, srcfile); + file->i_merged[i] = merge2defines(file, *ip); + if (val != NULL) + break; + } + } + else if (val != NULL && srcfile != NULL) + *srcfile = file; + recurse_lvl--; + file->i_flags &= ~DEFCHECKED; + + return (val); } struct symtab ** isdefined(const char *symbol, struct inclist *file, struct inclist **srcfile) { - struct symtab **val; - - if ((val = slookup(symbol, &maininclist))) { - debug(1,("%s defined on command line\n", symbol)); - if (srcfile != NULL) *srcfile = &maininclist; - return(val); - } - if ((val = fdefined(symbol, file, srcfile))) - return(val); - debug(1,("%s not defined in %s\n", symbol, file->i_file)); - return(NULL); + struct symtab **val; + + if ((val = slookup(symbol, &maininclist))) { + debug(1, ("%s defined on command line\n", symbol)); + if (srcfile != NULL) + *srcfile = &maininclist; + return (val); + } + if ((val = fdefined(symbol, file, srcfile))) + return (val); + debug(1, ("%s not defined in %s\n", symbol, file->i_file)); + return (NULL); } /* * Return type based on if the #if expression evaluates to 0 */ static int -zero_value(char *filename, - char *exp, - struct filepointer *filep, - struct inclist *file_red) +zero_value(char *filename, char *exp, + struct filepointer *filep, struct inclist *file_red) { - if (cppsetup(filename, exp, filep, file_red)) - return(IFFALSE); - else - return(IF); + if (cppsetup(filename, exp, filep, file_red)) + return (IFFALSE); + else + return (IF); } void @@ -320,76 +314,71 @@ define2(const char *name, const char *val, struct inclist *file) struct symtab *stab; /* Make space if it's needed */ - if (file->i_defs == NULL) - { - file->i_defs = malloc(sizeof (struct symtab*) * SYMTABINC); - file->i_ndefs = 0; + if (file->i_defs == NULL) { + file->i_defs = malloc(sizeof(struct symtab *) * SYMTABINC); + file->i_ndefs = 0; } else if (!(file->i_ndefs % SYMTABINC)) - file->i_defs = realloc(file->i_defs, - sizeof(struct symtab*)*(file->i_ndefs+SYMTABINC)); + file->i_defs = realloc(file->i_defs, + sizeof(struct symtab *) * (file->i_ndefs + + SYMTABINC)); if (file->i_defs == NULL) - fatalerr("malloc()/realloc() failure in insert_defn()\n"); + fatalerr("malloc()/realloc() failure in insert_defn()\n"); below = first = 0; last = file->i_ndefs - 1; - while (last >= first) - { - /* Fast inline binary search */ - register const char *s1; - register const char *s2; - register int middle = (first + last) / 2; - - /* Fast inline strchr() */ - s1 = name; - s2 = file->i_defs[middle]->s_name; - while (*s1++ == *s2++) - if (s2[-1] == '\0') break; - - /* If exact match, set sp and break */ - if (*--s1 == *--s2) - { - sp = file->i_defs + middle; - break; - } - - /* If name > i_defs[middle] ... */ - if (*s1 > *s2) - { - below = first; - first = middle + 1; - } - /* else ... */ - else - { - below = last = middle - 1; - } + while (last >= first) { + /* Fast inline binary search */ + register const char *s1; + register const char *s2; + register int middle = (first + last) / 2; + + /* Fast inline strchr() */ + s1 = name; + s2 = file->i_defs[middle]->s_name; + while (*s1++ == *s2++) + if (s2[-1] == '\0') + break; + + /* If exact match, set sp and break */ + if (*--s1 == *--s2) { + sp = file->i_defs + middle; + break; + } + + /* If name > i_defs[middle] ... */ + if (*s1 > *s2) { + below = first; + first = middle + 1; + } + /* else ... */ + else { + below = last = middle - 1; + } } /* Search is done. If we found an exact match to the symbol name, just replace its s_value */ - if (sp != NULL) - { - debug(1,("redefining %s from %s to %s in file %s\n", - name, (*sp)->s_value, val, file->i_file)); - free((*sp)->s_value); - (*sp)->s_value = strdup(val); - return; + if (sp != NULL) { + debug(1, ("redefining %s from %s to %s in file %s\n", + name, (*sp)->s_value, val, file->i_file)); + free((*sp)->s_value); + (*sp)->s_value = strdup(val); + return; } sp = file->i_defs + file->i_ndefs++; dest = file->i_defs + below + 1; - while (sp > dest) - { - *sp = sp[-1]; - sp--; + while (sp > dest) { + *sp = sp[-1]; + sp--; } - stab = malloc(sizeof (struct symtab)); + stab = malloc(sizeof(struct symtab)); if (stab == NULL) - fatalerr("malloc()/realloc() failure in insert_defn()\n"); + fatalerr("malloc()/realloc() failure in insert_defn()\n"); - debug(1,("defining %s to %s in file %s\n", name, val, file->i_file)); + debug(1, ("defining %s to %s in file %s\n", name, val, file->i_file)); stab->s_name = strdup(name); stab->s_value = strdup(val); *sp = stab; @@ -403,278 +392,265 @@ define(char *def, struct inclist *file) /* Separate symbol name and its value */ val = def; while (isalnum(*val) || *val == '_') - val++; + val++; if (*val) - *val++ = '\0'; + *val++ = '\0'; while (*val == ' ' || *val == '\t') - val++; + val++; if (!*val) - define2(def, "1", file); + define2(def, "1", file); else - define2(def, val, file); + define2(def, val, file); } struct symtab ** slookup(const char *symbol, struct inclist *file) { - register int first = 0; - register int last; - - if (file == NULL) - return NULL; - - last = file->i_ndefs - 1; - - while (last >= first) - { - /* Fast inline binary search */ - register const char *s1; - register const char *s2; - register int middle = (first + last) / 2; - - /* Fast inline strchr() */ - s1 = symbol; - s2 = file->i_defs[middle]->s_name; - while (*s1++ == *s2++) - if (s2[-1] == '\0') break; - - /* If exact match, we're done */ - if (*--s1 == *--s2) - { - return file->i_defs + middle; - } - - /* If symbol > i_defs[middle] ... */ - if (*s1 > *s2) - { - first = middle + 1; - } - /* else ... */ - else - { - last = middle - 1; - } - } - return(NULL); + register int first = 0; + register int last; + + if (file == NULL) + return NULL; + + last = file->i_ndefs - 1; + + while (last >= first) { + /* Fast inline binary search */ + register const char *s1; + register const char *s2; + register int middle = (first + last) / 2; + + /* Fast inline strchr() */ + s1 = symbol; + s2 = file->i_defs[middle]->s_name; + while (*s1++ == *s2++) + if (s2[-1] == '\0') + break; + + /* If exact match, we're done */ + if (*--s1 == *--s2) { + return file->i_defs + middle; + } + + /* If symbol > i_defs[middle] ... */ + if (*s1 > *s2) { + first = middle + 1; + } + /* else ... */ + else { + last = middle - 1; + } + } + return (NULL); } static int merge2defines(struct inclist *file1, struct inclist *file2) { - int i; - - if ((file1==NULL) || (file2==NULL) || - !(file2->i_flags & FINISHED)) - return 0; - - for (i=0; i < file2->i_listlen; i++) - if (file2->i_merged[i]==FALSE) - return 0; - - { - int first1 = 0; - int last1 = file1->i_ndefs - 1; - - int first2 = 0; - int last2 = file2->i_ndefs - 1; - - int first=0; - struct symtab** i_defs = NULL; - int deflen=file1->i_ndefs+file2->i_ndefs; - - debug(2,("merging %s into %s\n", - file2->i_file, file1->i_file)); - - if (deflen>0) - { - /* make sure deflen % SYMTABINC == 0 is still true */ - deflen += (SYMTABINC - deflen % SYMTABINC) % SYMTABINC; - i_defs = malloc(deflen*sizeof(struct symtab*)); - if (i_defs==NULL) return 0; - } - - while ((last1 >= first1) && (last2 >= first2)) - { - const char *s1 = file1->i_defs[first1]->s_name; - const char *s2 = file2->i_defs[first2]->s_name; - - if (strcmp(s1,s2) < 0) - i_defs[first++]=file1->i_defs[first1++]; - else if (strcmp(s1,s2) > 0) - i_defs[first++]=file2->i_defs[first2++]; - else /* equal */ - { - i_defs[first++]=file2->i_defs[first2++]; - first1++; - } - } - while (last1 >= first1) - { - i_defs[first++]=file1->i_defs[first1++]; - } - while (last2 >= first2) - { - i_defs[first++]=file2->i_defs[first2++]; - } - - if (file1->i_defs) free(file1->i_defs); - file1->i_defs=i_defs; - file1->i_ndefs=first; - - return 1; - } + int i; + + if ((file1 == NULL) || (file2 == NULL) || !(file2->i_flags & FINISHED)) + return 0; + + for (i = 0; i < file2->i_listlen; i++) + if (file2->i_merged[i] == FALSE) + return 0; + + { + int first1 = 0; + int last1 = file1->i_ndefs - 1; + + int first2 = 0; + int last2 = file2->i_ndefs - 1; + + int first = 0; + struct symtab **i_defs = NULL; + int deflen = file1->i_ndefs + file2->i_ndefs; + + debug(2, ("merging %s into %s\n", file2->i_file, file1->i_file)); + + if (deflen > 0) { + /* make sure deflen % SYMTABINC == 0 is still true */ + deflen += (SYMTABINC - deflen % SYMTABINC) % SYMTABINC; + i_defs = malloc(deflen * sizeof(struct symtab *)); + if (i_defs == NULL) + return 0; + } + + while ((last1 >= first1) && (last2 >= first2)) { + const char *s1 = file1->i_defs[first1]->s_name; + const char *s2 = file2->i_defs[first2]->s_name; + + if (strcmp(s1, s2) < 0) + i_defs[first++] = file1->i_defs[first1++]; + else if (strcmp(s1, s2) > 0) + i_defs[first++] = file2->i_defs[first2++]; + else { /* equal */ + i_defs[first++] = file2->i_defs[first2++]; + first1++; + } + } + while (last1 >= first1) { + i_defs[first++] = file1->i_defs[first1++]; + } + while (last2 >= first2) { + i_defs[first++] = file2->i_defs[first2++]; + } + + if (file1->i_defs) + free(file1->i_defs); + file1->i_defs = i_defs; + file1->i_ndefs = first; + + return 1; + } } void undefine(const char *symbol, struct inclist *file) { - register struct symtab **ptr; - struct inclist *srcfile; - while ((ptr = isdefined(symbol, file, &srcfile)) != NULL) - { - srcfile->i_ndefs--; - for (; ptr < srcfile->i_defs + srcfile->i_ndefs; ptr++) - *ptr = ptr[1]; - } + register struct symtab **ptr; + struct inclist *srcfile; + + while ((ptr = isdefined(symbol, file, &srcfile)) != NULL) { + srcfile->i_ndefs--; + for (; ptr < srcfile->i_defs + srcfile->i_ndefs; ptr++) + *ptr = ptr[1]; + } } int find_includes(struct filepointer *filep, struct inclist *file, - struct inclist *file_red, int recursion, boolean failOK) + struct inclist *file_red, int recursion, boolean failOK) { - struct inclist *inclistp; - const char **includedirsp; - register char *line; - register int type; - boolean recfailOK; - - while ((line = getnextline(filep))) { - switch(type = deftype(line, filep, file_red, file, TRUE)) { - case IF: - doif: - type = find_includes(filep, file, - file_red, recursion+1, failOK); - while ((type == ELIF) || (type == ELIFFALSE) || - (type == ELIFGUESSFALSE)) - type = gobble(filep, file, file_red); - if (type == ELSE) - gobble(filep, file, file_red); - break; - case IFFALSE: - case IFGUESSFALSE: - doiffalse: - if (type == IFGUESSFALSE || type == ELIFGUESSFALSE) - recfailOK = TRUE; - else - recfailOK = failOK; - type = gobble(filep, file, file_red); - if (type == ELSE) - find_includes(filep, file, - file_red, recursion+1, recfailOK); - else - if (type == ELIF) - goto doif; - else - if ((type == ELIFFALSE) || (type == ELIFGUESSFALSE)) - goto doiffalse; - break; - case IFDEF: - case IFNDEF: - if ((type == IFDEF && isdefined(line, file_red, NULL)) - || (type == IFNDEF && !isdefined(line, file_red, NULL))) { - debug(1,(type == IFNDEF ? - "line %d: %s !def'd in %s via %s%s\n" : "", - filep->f_line, line, - file->i_file, file_red->i_file, ": doit")); - type = find_includes(filep, file, - file_red, recursion+1, failOK); - while (type == ELIF || type == ELIFFALSE || type == ELIFGUESSFALSE) - type = gobble(filep, file, file_red); - if (type == ELSE) - gobble(filep, file, file_red); - } - else { - debug(1,(type == IFDEF ? - "line %d: %s !def'd in %s via %s%s\n" : "", - filep->f_line, line, - file->i_file, file_red->i_file, ": gobble")); - type = gobble(filep, file, file_red); - if (type == ELSE) - find_includes(filep, file, - file_red, recursion+1, failOK); - else if (type == ELIF) - goto doif; - else if (type == ELIFFALSE || type == ELIFGUESSFALSE) - goto doiffalse; - } - break; - case ELSE: - case ELIFFALSE: - case ELIFGUESSFALSE: - case ELIF: - if (!recursion) - gobble(filep, file, file_red); - case ENDIF: - if (recursion) - return(type); - case DEFINE: - define(line, file); - break; - case UNDEF: - if (!*line) { - warning("%s", file_red->i_file); - if (file_red != file) - warning1(" (reading %s)", file->i_file); - warning1(", line %ld: incomplete undef == \"%s\"\n", - filep->f_line, line); - break; - } - undefine(line, file_red); - break; - case INCLUDE: - case INCLUDEDOT: - case INCLUDENEXT: - case INCLUDENEXTDOT: - inclistp = inclistnext; - includedirsp = includedirsnext; - debug(2,("%s, reading %s, includes %s\n", - file_red->i_file, file->i_file, line)); - add_include(filep, file, file_red, line, type, failOK); - inclistnext = inclistp; - includedirsnext = includedirsp; - break; - case ERROR: - case WARNING: - warning("%s", file_red->i_file); - if (file_red != file) - warning1(" (reading %s)", file->i_file); - warning1(", line %ld: %s\n", - filep->f_line, line); - break; - - case PRAGMA: - case IDENT: - case SCCS: - case EJECT: - break; - case -1: - warning("%s", file_red->i_file); - if (file_red != file) - warning1(" (reading %s)", file->i_file); - warning1(", line %ld: unknown directive == \"%s\"\n", - filep->f_line, line); - break; - case -2: - warning("%s", file_red->i_file); - if (file_red != file) - warning1(" (reading %s)", file->i_file); - warning1(", line %ld: incomplete include == \"%s\"\n", - filep->f_line, line); - break; - } - } - file->i_flags |= FINISHED; - debug(2,("finished with %s\n", file->i_file)); - return(-1); + struct inclist *inclistp; + const char **includedirsp; + register char *line; + register int type; + boolean recfailOK; + + while ((line = getnextline(filep))) { + switch (type = deftype(line, filep, file_red, file, TRUE)) { + case IF: + doif: + type = find_includes(filep, file, file_red, recursion + 1, failOK); + while ((type == ELIF) || (type == ELIFFALSE) || + (type == ELIFGUESSFALSE)) + type = gobble(filep, file, file_red); + if (type == ELSE) + gobble(filep, file, file_red); + break; + case IFFALSE: + case IFGUESSFALSE: + doiffalse: + if (type == IFGUESSFALSE || type == ELIFGUESSFALSE) + recfailOK = TRUE; + else + recfailOK = failOK; + type = gobble(filep, file, file_red); + if (type == ELSE) + find_includes(filep, file, file_red, recursion + 1, recfailOK); + else if (type == ELIF) + goto doif; + else if ((type == ELIFFALSE) || (type == ELIFGUESSFALSE)) + goto doiffalse; + break; + case IFDEF: + case IFNDEF: + if ((type == IFDEF && isdefined(line, file_red, NULL)) + || (type == IFNDEF && !isdefined(line, file_red, NULL))) { + debug(1, (type == IFNDEF ? + "line %d: %s !def'd in %s via %s%s\n" : "", + filep->f_line, line, + file->i_file, file_red->i_file, ": doit")); + type = find_includes(filep, file, + file_red, recursion + 1, failOK); + while (type == ELIF || type == ELIFFALSE || + type == ELIFGUESSFALSE) + type = gobble(filep, file, file_red); + if (type == ELSE) + gobble(filep, file, file_red); + } + else { + debug(1, (type == IFDEF ? + "line %d: %s !def'd in %s via %s%s\n" : "", + filep->f_line, line, + file->i_file, file_red->i_file, ": gobble")); + type = gobble(filep, file, file_red); + if (type == ELSE) + find_includes(filep, file, file_red, recursion + 1, failOK); + else if (type == ELIF) + goto doif; + else if (type == ELIFFALSE || type == ELIFGUESSFALSE) + goto doiffalse; + } + break; + case ELSE: + case ELIFFALSE: + case ELIFGUESSFALSE: + case ELIF: + if (!recursion) + gobble(filep, file, file_red); + case ENDIF: + if (recursion) + return (type); + case DEFINE: + define(line, file); + break; + case UNDEF: + if (!*line) { + warning("%s", file_red->i_file); + if (file_red != file) + warning1(" (reading %s)", file->i_file); + warning1(", line %ld: incomplete undef == \"%s\"\n", + filep->f_line, line); + break; + } + undefine(line, file_red); + break; + case INCLUDE: + case INCLUDEDOT: + case INCLUDENEXT: + case INCLUDENEXTDOT: + inclistp = inclistnext; + includedirsp = includedirsnext; + debug(2, ("%s, reading %s, includes %s\n", + file_red->i_file, file->i_file, line)); + add_include(filep, file, file_red, line, type, failOK); + inclistnext = inclistp; + includedirsnext = includedirsp; + break; + case ERROR: + case WARNING: + warning("%s", file_red->i_file); + if (file_red != file) + warning1(" (reading %s)", file->i_file); + warning1(", line %ld: %s\n", filep->f_line, line); + break; + + case PRAGMA: + case IDENT: + case SCCS: + case EJECT: + break; + case -1: + warning("%s", file_red->i_file); + if (file_red != file) + warning1(" (reading %s)", file->i_file); + warning1(", line %ld: unknown directive == \"%s\"\n", + filep->f_line, line); + break; + case -2: + warning("%s", file_red->i_file); + if (file_red != file) + warning1(" (reading %s)", file->i_file); + warning1(", line %ld: incomplete include == \"%s\"\n", + filep->f_line, line); + break; + } + } + file->i_flags |= FINISHED; + debug(2, ("finished with %s\n", file->i_file)); + return (-1); } diff --git a/pr.c b/pr.c index 4744d56..f3a09c2 100644 --- a/pr.c +++ b/pr.c @@ -28,39 +28,39 @@ in this Software without prior written authorization from The Open Group. void add_include(struct filepointer *filep, struct inclist *file, - struct inclist *file_red, const char *include, int type, - boolean failOK) + struct inclist *file_red, const char *include, int type, + boolean failOK) { - register struct inclist *newfile; - register struct filepointer *content; - - /* - * First decide what the pathname of this include file really is. - */ - newfile = inc_path(file->i_file, include, type); - if (newfile == NULL) { - if (failOK) - return; - if (file != file_red) - warning("%s (reading %s, line %ld): ", - file_red->i_file, file->i_file, filep->f_line); - else - warning("%s, line %ld: ", file->i_file, filep->f_line); - warning1("cannot find include file \"%s\"\n", include); - show_where_not = TRUE; - newfile = inc_path(file->i_file, include, type); - show_where_not = FALSE; - } - - if (newfile) { - included_by(file, newfile); - if (!(newfile->i_flags & SEARCHED)) { - newfile->i_flags |= SEARCHED; - content = getfile(newfile->i_file); - find_includes(content, newfile, file_red, 0, failOK); - freefile(content); - } - } + register struct inclist *newfile; + register struct filepointer *content; + + /* + * First decide what the pathname of this include file really is. + */ + newfile = inc_path(file->i_file, include, type); + if (newfile == NULL) { + if (failOK) + return; + if (file != file_red) + warning("%s (reading %s, line %ld): ", + file_red->i_file, file->i_file, filep->f_line); + else + warning("%s, line %ld: ", file->i_file, filep->f_line); + warning1("cannot find include file \"%s\"\n", include); + show_where_not = TRUE; + newfile = inc_path(file->i_file, include, type); + show_where_not = FALSE; + } + + if (newfile) { + included_by(file, newfile); + if (!(newfile->i_flags & SEARCHED)) { + newfile->i_flags |= SEARCHED; + content = getfile(newfile->i_file); + find_includes(content, newfile, file_red, 0, failOK); + freefile(content); + } + } } /** @@ -70,81 +70,81 @@ add_include(struct filepointer *filep, struct inclist *file, static const char * quoteColons(const char *input, char *outputbuffer, size_t bufsize) { - const char *tmp=input; - const char *loc; - char *output=outputbuffer; - - loc = strchr(input, ':'); - if (loc == NULL) { - return input; - } - - tmp=input; - while (loc != NULL && bufsize > loc-tmp+2 ) { - memcpy(output, tmp, loc-tmp); - output+=loc-tmp; - bufsize-=loc-tmp+2; - tmp=loc+1; - *output='\\'; - output++; - *output=':'; - output++; - loc = strchr(tmp, ':'); - } - - if (strlen(tmp) <= bufsize) - strcpy(output, tmp); - else { - strncpy(output, tmp, bufsize-1); - output[bufsize]=0; - } - return outputbuffer; + const char *tmp = input; + const char *loc; + char *output = outputbuffer; + + loc = strchr(input, ':'); + if (loc == NULL) { + return input; + } + + tmp = input; + while (loc != NULL && bufsize > loc - tmp + 2) { + memcpy(output, tmp, loc - tmp); + output += loc - tmp; + bufsize -= loc - tmp + 2; + tmp = loc + 1; + *output = '\\'; + output++; + *output = ':'; + output++; + loc = strchr(tmp, ':'); + } + + if (strlen(tmp) <= bufsize) + strcpy(output, tmp); + else { + strncpy(output, tmp, bufsize - 1); + output[bufsize] = 0; + } + return outputbuffer; } static void pr(struct inclist *ip, const char *file, const char *base) { - static const char *lastfile; - static int current_len; - register int len, i; - const char * quoted; - char quotebuf[ BUFSIZ ]; - - printed = TRUE; - quoted = quoteColons(ip->i_file, quotebuf, sizeof(quotebuf)); - len = strlen(quoted)+1; - if (current_len + len > width || file != lastfile) { - lastfile = file; - current_len = fprintf(stdout, "\n%s%s%s: %s", - objprefix, base, objsuffix, quoted); - } - else { - fprintf(stdout, " %s", quoted); - current_len += len; - } - - /* - * If verbose is set, then print out what this file includes. - */ - if (! verbose || ip->i_list == NULL || ip->i_flags & NOTIFIED) - return; - ip->i_flags |= NOTIFIED; - lastfile = NULL; - printf("\n# %s includes:", ip->i_file); - for (i=0; ii_listlen; i++) - printf("\n#\t%s", ip->i_list[ i ]->i_incstring); + static const char *lastfile; + static int current_len; + register int len, i; + const char *quoted; + char quotebuf[BUFSIZ]; + + printed = TRUE; + quoted = quoteColons(ip->i_file, quotebuf, sizeof(quotebuf)); + len = strlen(quoted) + 1; + if (current_len + len > width || file != lastfile) { + lastfile = file; + current_len = fprintf(stdout, "\n%s%s%s: %s", + objprefix, base, objsuffix, quoted); + } + else { + fprintf(stdout, " %s", quoted); + current_len += len; + } + + /* + * If verbose is set, then print out what this file includes. + */ + if (!verbose || ip->i_list == NULL || ip->i_flags & NOTIFIED) + return; + ip->i_flags |= NOTIFIED; + lastfile = NULL; + printf("\n# %s includes:", ip->i_file); + for (i = 0; i < ip->i_listlen; i++) + printf("\n#\t%s", ip->i_list[i]->i_incstring); } void recursive_pr_include(struct inclist *head, const char *file, const char *base) { - int i; - - if (head->i_flags & MARKED) - return; - head->i_flags |= MARKED; - if (head->i_file != file) - pr(head, file, base); - for (i=0; ii_listlen; i++) - recursive_pr_include(head->i_list[ i ], file, base); + int i; + + if (head->i_flags & MARKED) + return; + head->i_flags |= MARKED; + if (head->i_file != file) + pr(head, file, base); + for (i = 0; i < head->i_listlen; i++) + recursive_pr_include(head->i_list[i], file, base); } -- cgit v1.2.1