"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 2 | /* |
"Vladimir N. Oleynik" | dd1ccdd | 2006-02-16 15:40:24 +0000 | [diff] [blame] | 3 | * Another fast dependencies generator for Makefiles, Version 4.2 |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 4 | * |
Rob Landley | 7bfa88f | 2006-02-13 19:16:41 +0000 | [diff] [blame] | 5 | * Copyright (C) 2005,2006 by Vladimir Oleynik <dzo@simtreas.ru> |
"Vladimir N. Oleynik" | 083d3f4 | 2005-10-10 11:35:17 +0000 | [diff] [blame] | 6 | * |
"Vladimir N. Oleynik" | af0dd59 | 2005-09-16 13:57:33 +0000 | [diff] [blame] | 7 | * mmaping file may be originally by Linus Torvalds. |
| 8 | * |
Rob Landley | 7bfa88f | 2006-02-13 19:16:41 +0000 | [diff] [blame] | 9 | * infix parser/evaluator for #if expression |
| 10 | * Copyright (c) 2001 Aaron Lehmann <aaronl@vitelus.com> |
| 11 | * Copyright (c) 2001 Manuel Novoa III <mjn3@codepoet.org> |
| 12 | * Copyright (c) 2003 Vladimir Oleynik <dzo@simtreas.ru> |
| 13 | * |
"Vladimir N. Oleynik" | 083d3f4 | 2005-10-10 11:35:17 +0000 | [diff] [blame] | 14 | * bb_simplify_path() |
| 15 | * Copyright (C) 2005 Manuel Novoa III <mjn3@codepoet.org> |
| 16 | * |
| 17 | * xmalloc() bb_xstrdup() bb_error_d(): |
| 18 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> |
| 19 | * |
| 20 | * llist routine |
| 21 | * Copyright (C) 2003 Glenn McGrath |
| 22 | * Copyright (C) Vladimir Oleynik <dzo@simtreas.ru> |
| 23 | * |
Rob Landley | 7bfa88f | 2006-02-13 19:16:41 +0000 | [diff] [blame] | 24 | * (c) 2005,2006 Bernhard Fischer: |
"Vladimir N. Oleynik" | af0dd59 | 2005-09-16 13:57:33 +0000 | [diff] [blame] | 25 | * - commentary typos, |
| 26 | * - move "memory exhausted" into msg_enomem, |
| 27 | * - more verbose --help output. |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 28 | * |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 29 | * This program does: |
"Vladimir N. Oleynik" | 6732af2 | 2006-02-15 12:29:37 +0000 | [diff] [blame] | 30 | * 1) find #define KEY VALUE or #undef KEY from include/bb_config.h |
"Vladimir N. Oleynik" | 083d3f4 | 2005-10-10 11:35:17 +0000 | [diff] [blame] | 31 | * 2) recursive find and scan *.[ch] files, but skips scan of include/config/ |
| 32 | * 3) find #include "*.h" and KEYs using, if not as #define and #undef |
| 33 | * 4) generate dependencies to stdout |
"Vladimir N. Oleynik" | 664c6e7 | 2005-10-06 14:53:43 +0000 | [diff] [blame] | 34 | * pwd/file.o: include/config/key*.h found_include_*.h |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 35 | * path/inc.h: include/config/key*.h found_included_include_*.h |
"Vladimir N. Oleynik" | 083d3f4 | 2005-10-10 11:35:17 +0000 | [diff] [blame] | 36 | * 5) save include/config/key*.h if changed after previous usage |
"Vladimir N. Oleynik" | af0dd59 | 2005-09-16 13:57:33 +0000 | [diff] [blame] | 37 | * This program does not generate dependencies for #include <...> |
Rob Landley | 7bfa88f | 2006-02-13 19:16:41 +0000 | [diff] [blame] | 38 | * Config file can have #if #elif #else #ifdef #ifndef #endif lines |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 39 | */ |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 40 | |
| 41 | #define LOCAL_INCLUDE_PATH "include" |
| 42 | #define INCLUDE_CONFIG_PATH LOCAL_INCLUDE_PATH"/config" |
"Vladimir N. Oleynik" | 6732af2 | 2006-02-15 12:29:37 +0000 | [diff] [blame] | 43 | #define INCLUDE_CONFIG_KEYS_PATH LOCAL_INCLUDE_PATH"/bb_config.h" |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 44 | |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 45 | #define bb_mkdep_full_options \ |
| 46 | "\nOptions:" \ |
| 47 | "\n\t-I local_include_path include paths, default: \"" LOCAL_INCLUDE_PATH "\"" \ |
| 48 | "\n\t-d don't generate depend" \ |
| 49 | "\n\t-w show warning if include files not found" \ |
| 50 | "\n\t-k include/config default: \"" INCLUDE_CONFIG_PATH "\"" \ |
| 51 | "\n\t-c include/config.h configs, default: \"" INCLUDE_CONFIG_KEYS_PATH "\"" \ |
| 52 | "\n\tdirs_to_scan default \".\"" |
| 53 | |
| 54 | #define bb_mkdep_terse_options "Usage: [-I local_include_paths] [-dw] " \ |
| 55 | "[-k path_for_stored_keys] [dirs]" |
| 56 | |
| 57 | |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 58 | #define _GNU_SOURCE |
| 59 | #include <sys/types.h> |
| 60 | #include <sys/stat.h> |
| 61 | #include <sys/mman.h> |
| 62 | #include <getopt.h> |
| 63 | #include <dirent.h> |
| 64 | #include <stdio.h> |
| 65 | #include <stdlib.h> |
| 66 | #include <string.h> |
| 67 | #include <stdarg.h> |
| 68 | #include <unistd.h> |
| 69 | #include <errno.h> |
| 70 | #include <fcntl.h> |
"Vladimir N. Oleynik" | 083d3f4 | 2005-10-10 11:35:17 +0000 | [diff] [blame] | 71 | #include <limits.h> |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 72 | |
Bernhard Reutner-Fischer | d293375 | 2006-05-26 19:58:58 +0000 | [diff] [blame] | 73 | #ifdef __GNUC__ |
| 74 | #define ATTRIBUTE __attribute__ |
| 75 | #else |
| 76 | #define ATTRIBUTE(a) /* nothing */ |
| 77 | #endif |
| 78 | |
| 79 | #if !(defined __USE_ISOC99 || (defined __GLIBC_HAVE_LONG_LONG && defined __USE_MISC)) |
| 80 | #define strtoll strtol |
| 81 | #endif |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 82 | |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 83 | /* partial and simplified libbb routine */ |
Bernhard Reutner-Fischer | d293375 | 2006-05-26 19:58:58 +0000 | [diff] [blame] | 84 | static void bb_error_d(const char *s, ...) ATTRIBUTE ((noreturn, format (printf, 1, 2))); |
| 85 | static char * bb_asprint(const char *format, ...) ATTRIBUTE ((format (printf, 1, 2))); |
"Vladimir N. Oleynik" | 083d3f4 | 2005-10-10 11:35:17 +0000 | [diff] [blame] | 86 | static char *bb_simplify_path(const char *path); |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 87 | |
| 88 | /* stolen from libbb as is */ |
| 89 | typedef struct llist_s { |
| 90 | char *data; |
| 91 | struct llist_s *link; |
| 92 | } llist_t; |
"Vladimir N. Oleynik" | 083d3f4 | 2005-10-10 11:35:17 +0000 | [diff] [blame] | 93 | |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 94 | static const char msg_enomem[] = "memory exhausted"; |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 95 | |
"Vladimir N. Oleynik" | 083d3f4 | 2005-10-10 11:35:17 +0000 | [diff] [blame] | 96 | /* inline realization for fast works */ |
| 97 | static inline void *xmalloc(size_t size) |
| 98 | { |
| 99 | void *p = malloc(size); |
| 100 | |
| 101 | if(p == NULL) |
| 102 | bb_error_d(msg_enomem); |
| 103 | return p; |
| 104 | } |
| 105 | |
| 106 | static inline char *bb_xstrdup(const char *s) |
| 107 | { |
| 108 | char *r = strdup(s); |
| 109 | |
| 110 | if(r == NULL) |
| 111 | bb_error_d(msg_enomem); |
| 112 | return r; |
| 113 | } |
| 114 | |
| 115 | |
| 116 | static int dontgenerate_dep; /* flag -d usaged */ |
| 117 | static int noiwarning; /* flag -w usaged */ |
| 118 | static llist_t *configs; /* list of -c usaged and them stat() after parsed */ |
| 119 | static llist_t *Iop; /* list of -I include usaged */ |
| 120 | |
| 121 | static char *pwd; /* current work directory */ |
Bernhard Reutner-Fischer | 5ba53c0 | 2006-02-14 10:43:40 +0000 | [diff] [blame] | 122 | static size_t replace; /* replace current work directory with build dir */ |
"Vladimir N. Oleynik" | 083d3f4 | 2005-10-10 11:35:17 +0000 | [diff] [blame] | 123 | |
"Vladimir N. Oleynik" | 63ca3bf | 2006-02-14 09:23:25 +0000 | [diff] [blame] | 124 | static const char *kp; /* KEY path, argument of -k used */ |
"Vladimir N. Oleynik" | 083d3f4 | 2005-10-10 11:35:17 +0000 | [diff] [blame] | 125 | static size_t kp_len; |
| 126 | static struct stat st_kp; /* stat(kp) */ |
| 127 | |
| 128 | typedef struct BB_KEYS { |
| 129 | char *keyname; |
| 130 | size_t key_sz; |
| 131 | const char *value; |
| 132 | const char *checked; |
| 133 | char *stored_path; |
| 134 | const char *src_have_this_key; |
| 135 | struct BB_KEYS *next; |
| 136 | } bb_key_t; |
| 137 | |
| 138 | static bb_key_t *key_top; /* list of found KEYs */ |
| 139 | static bb_key_t *Ifound; /* list of parsed includes */ |
| 140 | |
| 141 | |
| 142 | static void parse_conf_opt(const char *opt, const char *val, size_t key_sz); |
| 143 | static void parse_inc(const char *include, const char *fname); |
| 144 | |
| 145 | static inline bb_key_t *check_key(bb_key_t *k, const char *nk, size_t key_sz) |
| 146 | { |
| 147 | bb_key_t *cur; |
| 148 | |
| 149 | for(cur = k; cur; cur = cur->next) { |
| 150 | if(key_sz == cur->key_sz && memcmp(cur->keyname, nk, key_sz) == 0) { |
| 151 | cur->checked = cur->stored_path; |
| 152 | return cur; |
| 153 | } |
| 154 | } |
| 155 | return NULL; |
| 156 | } |
| 157 | |
Rob Landley | 7bfa88f | 2006-02-13 19:16:41 +0000 | [diff] [blame] | 158 | static inline const char *lookup_key(const char *nk, size_t key_sz) |
| 159 | { |
| 160 | bb_key_t *cur; |
| 161 | |
| 162 | for(cur = key_top; cur; cur = cur->next) { |
| 163 | if(key_sz == cur->key_sz && memcmp(cur->keyname, nk, key_sz) == 0) { |
| 164 | return cur->value; |
| 165 | } |
| 166 | } |
| 167 | return NULL; |
| 168 | } |
| 169 | |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 170 | /* for lexical analyser */ |
"Vladimir N. Oleynik" | 083d3f4 | 2005-10-10 11:35:17 +0000 | [diff] [blame] | 171 | static int pagesizem1; /* padding mask = getpagesize() - 1 */ |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 172 | |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 173 | /* for speed tricks */ |
"Vladimir N. Oleynik" | 083d3f4 | 2005-10-10 11:35:17 +0000 | [diff] [blame] | 174 | static char first_chars[1+UCHAR_MAX]; /* + L_EOF */ |
| 175 | static char isalnums[1+UCHAR_MAX]; /* + L_EOF */ |
Rob Landley | 7bfa88f | 2006-02-13 19:16:41 +0000 | [diff] [blame] | 176 | |
| 177 | /* trick for fast find "define", "include", "undef", |
| 178 | "if((n)def)" "else", "endif" */ |
| 179 | static const char * const preproc[] = { |
| 180 | /* 0 1 2 3 4 5 6 7 8 9 */ |
| 181 | "", "efine", "lif", "lse", "ndif", "f", "fdef", "fndef", "nclude", "ndef" }; |
| 182 | static const unsigned char first_chars_deiu[UCHAR_MAX] = { |
| 183 | [(int)'d'] = (unsigned char)(1|0x10), /* define */ |
| 184 | [(int)'e'] = (unsigned char)(2|0x40), /* elif, else, endif */ |
| 185 | [(int)'i'] = (unsigned char)(5|0x80), /* if ifdef ifndef include */ |
| 186 | [(int)'u'] = (unsigned char)(9|0x90), /* undef */ |
"Vladimir N. Oleynik" | 6c0642d | 2005-10-07 15:36:26 +0000 | [diff] [blame] | 187 | }; |
"Vladimir N. Oleynik" | b1fe462 | 2005-09-12 16:39:47 +0000 | [diff] [blame] | 188 | |
"Vladimir N. Oleynik" | 083d3f4 | 2005-10-10 11:35:17 +0000 | [diff] [blame] | 189 | #define CONFIG_MODE 0 |
Rob Landley | 7bfa88f | 2006-02-13 19:16:41 +0000 | [diff] [blame] | 190 | #define IF0_MODE 1 /* #if 0 */ |
| 191 | #define IF1_MODE 2 /* #if 1 */ |
| 192 | #define ELSE0_MODE 4 /* #else found after #if 0 */ |
| 193 | #define ELSE1_MODE 8 /* #else found after #if 1 */ |
| 194 | #define ELIF1_MODE 16 /* #elif found after #if 1 */ |
| 195 | #define FALSE_MODES (IF0_MODE|ELSE1_MODE|ELIF1_MODE) |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 196 | |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 197 | #define yy_error_d(s) bb_error_d("%s:%d hmm, %s", fname, line, s) |
| 198 | |
| 199 | /* state */ |
| 200 | #define S 0 /* start state */ |
| 201 | #define STR '"' /* string */ |
| 202 | #define CHR '\'' /* char */ |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 203 | #define REM '/' /* block comment */ |
| 204 | #define BS '\\' /* back slash */ |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 205 | #define POUND '#' /* # */ |
Rob Landley | 7bfa88f | 2006-02-13 19:16:41 +0000 | [diff] [blame] | 206 | #define D '1' /* #define preprocessor's directive */ |
| 207 | #define EI '2' /* #elif preprocessor's directive */ |
| 208 | #define E '3' /* #else preprocessor's directive */ |
| 209 | #define EF '4' /* #endif preprocessor's directive */ |
| 210 | #define F '5' /* #if preprocessor's directive */ |
| 211 | #define IFD '6' /* #ifdef preprocessor's directive */ |
| 212 | #define IFND '7' /* #ifndef preprocessor's directive */ |
| 213 | #define I '8' /* #include preprocessor's directive */ |
| 214 | #define U '9' /* #undef preprocessor's directive */ |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 215 | #define DK 'K' /* #define KEY... (config mode) */ |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 216 | #define ANY '*' /* any unparsed chars */ |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 217 | |
| 218 | /* [A-Z_a-z] */ |
| 219 | #define ID(c) ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c == '_') |
| 220 | /* [A-Z_a-z0-9] */ |
| 221 | #define ISALNUM(c) (ID(c) || (c >= '0' && c <= '9')) |
| 222 | |
"Vladimir N. Oleynik" | 083d3f4 | 2005-10-10 11:35:17 +0000 | [diff] [blame] | 223 | #define L_EOF (1+UCHAR_MAX) |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 224 | |
"Vladimir N. Oleynik" | 083d3f4 | 2005-10-10 11:35:17 +0000 | [diff] [blame] | 225 | #define getc0() do { c = (optr >= oend) ? L_EOF : *optr++; } while(0) |
| 226 | |
| 227 | #define getc1() do { getc0(); if(c == BS) { getc0(); \ |
| 228 | if(c == '\n') { line++; continue; } \ |
| 229 | else { optr--; c = BS; } \ |
| 230 | } break; } while(1) |
| 231 | |
| 232 | static char id_s[4096]; |
| 233 | #define put_id(ic) do { if(id_len == sizeof(id_s)) goto too_long; \ |
| 234 | id[id_len++] = ic; } while(0) |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 235 | |
Rob Landley | 7bfa88f | 2006-02-13 19:16:41 +0000 | [diff] [blame] | 236 | static char ifcpp_stack[1024]; |
| 237 | static int ptr_ifcpp_stack; |
| 238 | #define push_mode() do { \ |
| 239 | if(ptr_ifcpp_stack == (int)sizeof(ifcpp_stack)) \ |
| 240 | yy_error_d("#if* stack overflow"); \ |
| 241 | ifcpp_stack[ptr_ifcpp_stack++] = (char)mode; \ |
| 242 | } while(0) |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 243 | |
Rob Landley | 7bfa88f | 2006-02-13 19:16:41 +0000 | [diff] [blame] | 244 | #define pop_mode() do { \ |
| 245 | if(ptr_ifcpp_stack == 0) \ |
| 246 | yy_error_d("unexpected #endif"); \ |
| 247 | mode = ifcpp_stack[--ptr_ifcpp_stack]; \ |
| 248 | } while(0) |
| 249 | |
| 250 | /* #if expression */ |
| 251 | typedef long long arith_t; |
| 252 | |
| 253 | static arith_t arith (const char *expr, int *perrcode); |
| 254 | |
| 255 | /* The code uses a simple two-stack algorithm. See |
| 256 | * http://www.onthenet.com.au/~grahamis/int2008/week02/lect02.html |
| 257 | * for a detailed explanation of the infix-to-postfix algorithm on which |
| 258 | * this is based (this code differs in that it applies operators immediately |
| 259 | * to the stack instead of adding them to a queue to end up with an |
| 260 | * expression). */ |
| 261 | |
| 262 | #define arith_isspace(arithval) \ |
| 263 | (arithval == ' ' || arithval == '\v' || arithval == '\t' || arithval == '\f') |
| 264 | |
| 265 | |
| 266 | typedef unsigned char operator; |
| 267 | |
| 268 | /* An operator's token id is a bit of a bitfield. The lower 5 bits are the |
| 269 | * precedence, and 3 high bits are an ID unique across operators of that |
| 270 | * precedence. The ID portion is so that multiple operators can have the |
| 271 | * same precedence, ensuring that the leftmost one is evaluated first. |
| 272 | * Consider * and /. */ |
| 273 | |
| 274 | #define tok_decl(prec,id) (((id)<<5)|(prec)) |
| 275 | #define PREC(op) ((op) & 0x1F) |
| 276 | |
| 277 | #define TOK_LPAREN tok_decl(0,0) |
| 278 | |
| 279 | #define TOK_COMMA tok_decl(1,0) |
| 280 | |
| 281 | /* conditional is right associativity too */ |
| 282 | #define TOK_CONDITIONAL tok_decl(2,0) |
| 283 | #define TOK_CONDITIONAL_SEP tok_decl(2,1) |
| 284 | |
| 285 | #define TOK_OR tok_decl(3,0) |
| 286 | |
| 287 | #define TOK_AND tok_decl(4,0) |
| 288 | |
| 289 | #define TOK_BOR tok_decl(5,0) |
| 290 | |
| 291 | #define TOK_BXOR tok_decl(6,0) |
| 292 | |
| 293 | #define TOK_BAND tok_decl(7,0) |
| 294 | |
| 295 | #define TOK_EQ tok_decl(8,0) |
| 296 | #define TOK_NE tok_decl(8,1) |
| 297 | |
| 298 | #define TOK_LT tok_decl(9,0) |
| 299 | #define TOK_GT tok_decl(9,1) |
| 300 | #define TOK_GE tok_decl(9,2) |
| 301 | #define TOK_LE tok_decl(9,3) |
| 302 | |
| 303 | #define TOK_LSHIFT tok_decl(10,0) |
| 304 | #define TOK_RSHIFT tok_decl(10,1) |
| 305 | |
| 306 | #define TOK_ADD tok_decl(11,0) |
| 307 | #define TOK_SUB tok_decl(11,1) |
| 308 | |
| 309 | #define TOK_MUL tok_decl(12,0) |
| 310 | #define TOK_DIV tok_decl(12,1) |
| 311 | #define TOK_REM tok_decl(12,2) |
| 312 | |
| 313 | /* For now unary operators. */ |
| 314 | #define UNARYPREC 13 |
| 315 | #define TOK_BNOT tok_decl(UNARYPREC,0) |
| 316 | #define TOK_NOT tok_decl(UNARYPREC,1) |
| 317 | |
| 318 | #define TOK_UMINUS tok_decl(UNARYPREC+1,0) |
| 319 | #define TOK_UPLUS tok_decl(UNARYPREC+1,1) |
| 320 | |
"Vladimir N. Oleynik" | 6732af2 | 2006-02-15 12:29:37 +0000 | [diff] [blame] | 321 | #define SPEC_PREC (UNARYPREC+2) |
Rob Landley | 7bfa88f | 2006-02-13 19:16:41 +0000 | [diff] [blame] | 322 | |
| 323 | #define TOK_NUM tok_decl(SPEC_PREC, 0) |
| 324 | #define TOK_RPAREN tok_decl(SPEC_PREC, 1) |
| 325 | |
| 326 | #define NUMPTR (*numstackptr) |
| 327 | |
| 328 | typedef struct ARITCH_VAR_NUM { |
| 329 | arith_t val; |
| 330 | arith_t contidional_second_val; |
| 331 | char contidional_second_val_initialized; |
| 332 | } v_n_t; |
| 333 | |
| 334 | |
| 335 | typedef struct CHK_VAR_RECURSIVE_LOOPED { |
| 336 | const char *var; |
| 337 | size_t var_sz; |
| 338 | struct CHK_VAR_RECURSIVE_LOOPED *next; |
| 339 | } chk_var_recursive_looped_t; |
| 340 | |
| 341 | static chk_var_recursive_looped_t *prev_chk_var_recursive; |
| 342 | |
| 343 | |
| 344 | static int arith_lookup_val(const char *var, size_t key_sz, arith_t *pval) |
| 345 | { |
| 346 | const char * p = lookup_key(var, key_sz); |
| 347 | |
| 348 | if(p) { |
| 349 | int errcode; |
| 350 | |
| 351 | /* recursive try as expression */ |
| 352 | chk_var_recursive_looped_t *cur; |
| 353 | chk_var_recursive_looped_t cur_save; |
| 354 | |
| 355 | for(cur = prev_chk_var_recursive; cur; cur = cur->next) { |
| 356 | if(cur->var_sz == key_sz && memcmp(cur->var, var, key_sz) == 0) { |
| 357 | /* expression recursion loop detected */ |
| 358 | return -5; |
| 359 | } |
| 360 | } |
| 361 | /* save current lookuped var name */ |
| 362 | cur = prev_chk_var_recursive; |
| 363 | cur_save.var = var; |
| 364 | cur_save.var_sz = key_sz; |
| 365 | cur_save.next = cur; |
| 366 | prev_chk_var_recursive = &cur_save; |
| 367 | |
| 368 | *pval = arith (p, &errcode); |
| 369 | /* restore previous ptr after recursiving */ |
| 370 | prev_chk_var_recursive = cur; |
| 371 | return errcode; |
| 372 | } else { |
| 373 | /* disallow undefined var */ |
| 374 | fprintf(stderr, "%.*s ", (int)key_sz, var); |
| 375 | return -4; |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | /* "applying" a token means performing it on the top elements on the integer |
| 380 | * stack. For a unary operator it will only change the top element, but a |
| 381 | * binary operator will pop two arguments and push a result */ |
| 382 | static inline int |
| 383 | arith_apply(operator op, v_n_t *numstack, v_n_t **numstackptr) |
| 384 | { |
| 385 | v_n_t *numptr_m1; |
| 386 | arith_t numptr_val, rez; |
| 387 | |
| 388 | if (NUMPTR == numstack) goto err; /* There is no operator that can work |
| 389 | without arguments */ |
| 390 | numptr_m1 = NUMPTR - 1; |
| 391 | |
| 392 | rez = numptr_m1->val; |
| 393 | if (op == TOK_UMINUS) |
| 394 | rez *= -1; |
| 395 | else if (op == TOK_NOT) |
| 396 | rez = !rez; |
| 397 | else if (op == TOK_BNOT) |
| 398 | rez = ~rez; |
| 399 | else if (op != TOK_UPLUS) { |
| 400 | /* Binary operators */ |
| 401 | |
| 402 | /* check and binary operators need two arguments */ |
| 403 | if (numptr_m1 == numstack) goto err; |
| 404 | |
| 405 | /* ... and they pop one */ |
| 406 | --NUMPTR; |
| 407 | numptr_val = rez; |
| 408 | if (op == TOK_CONDITIONAL) { |
| 409 | if(! numptr_m1->contidional_second_val_initialized) { |
| 410 | /* protect $((expr1 ? expr2)) without ": expr" */ |
| 411 | goto err; |
| 412 | } |
| 413 | rez = numptr_m1->contidional_second_val; |
| 414 | } else if(numptr_m1->contidional_second_val_initialized) { |
| 415 | /* protect $((expr1 : expr2)) without "expr ? " */ |
| 416 | goto err; |
| 417 | } |
| 418 | numptr_m1 = NUMPTR - 1; |
| 419 | if (op == TOK_CONDITIONAL) { |
| 420 | numptr_m1->contidional_second_val = rez; |
| 421 | } |
| 422 | rez = numptr_m1->val; |
| 423 | if (op == TOK_BOR) |
| 424 | rez |= numptr_val; |
| 425 | else if (op == TOK_OR) |
| 426 | rez = numptr_val || rez; |
| 427 | else if (op == TOK_BAND) |
| 428 | rez &= numptr_val; |
| 429 | else if (op == TOK_BXOR) |
| 430 | rez ^= numptr_val; |
| 431 | else if (op == TOK_AND) |
| 432 | rez = rez && numptr_val; |
| 433 | else if (op == TOK_EQ) |
| 434 | rez = (rez == numptr_val); |
| 435 | else if (op == TOK_NE) |
| 436 | rez = (rez != numptr_val); |
| 437 | else if (op == TOK_GE) |
| 438 | rez = (rez >= numptr_val); |
| 439 | else if (op == TOK_RSHIFT) |
| 440 | rez >>= numptr_val; |
| 441 | else if (op == TOK_LSHIFT) |
| 442 | rez <<= numptr_val; |
| 443 | else if (op == TOK_GT) |
| 444 | rez = (rez > numptr_val); |
| 445 | else if (op == TOK_LT) |
| 446 | rez = (rez < numptr_val); |
| 447 | else if (op == TOK_LE) |
| 448 | rez = (rez <= numptr_val); |
| 449 | else if (op == TOK_MUL) |
| 450 | rez *= numptr_val; |
| 451 | else if (op == TOK_ADD) |
| 452 | rez += numptr_val; |
| 453 | else if (op == TOK_SUB) |
| 454 | rez -= numptr_val; |
| 455 | else if (op == TOK_COMMA) |
| 456 | rez = numptr_val; |
| 457 | else if (op == TOK_CONDITIONAL_SEP) { |
| 458 | if (numptr_m1 == numstack) { |
| 459 | /* protect $((expr : expr)) without "expr ? " */ |
| 460 | goto err; |
| 461 | } |
| 462 | numptr_m1->contidional_second_val_initialized = op; |
| 463 | numptr_m1->contidional_second_val = numptr_val; |
| 464 | } |
| 465 | else if (op == TOK_CONDITIONAL) { |
| 466 | rez = rez ? |
| 467 | numptr_val : numptr_m1->contidional_second_val; |
| 468 | } |
| 469 | else if(numptr_val==0) /* zero divisor check */ |
| 470 | return -2; |
| 471 | else if (op == TOK_DIV) |
| 472 | rez /= numptr_val; |
| 473 | else if (op == TOK_REM) |
| 474 | rez %= numptr_val; |
| 475 | } |
| 476 | numptr_m1->val = rez; |
| 477 | return 0; |
| 478 | err: return(-1); |
| 479 | } |
| 480 | |
| 481 | /* longest must first */ |
| 482 | static const char op_tokens[] = { |
| 483 | '<','<', 0, TOK_LSHIFT, |
| 484 | '>','>', 0, TOK_RSHIFT, |
| 485 | '|','|', 0, TOK_OR, |
| 486 | '&','&', 0, TOK_AND, |
| 487 | '!','=', 0, TOK_NE, |
| 488 | '<','=', 0, TOK_LE, |
| 489 | '>','=', 0, TOK_GE, |
| 490 | '=','=', 0, TOK_EQ, |
| 491 | '!', 0, TOK_NOT, |
| 492 | '<', 0, TOK_LT, |
| 493 | '>', 0, TOK_GT, |
| 494 | '|', 0, TOK_BOR, |
| 495 | '&', 0, TOK_BAND, |
| 496 | '*', 0, TOK_MUL, |
| 497 | '/', 0, TOK_DIV, |
| 498 | '%', 0, TOK_REM, |
| 499 | '+', 0, TOK_ADD, |
| 500 | '-', 0, TOK_SUB, |
| 501 | '^', 0, TOK_BXOR, |
| 502 | '~', 0, TOK_BNOT, |
| 503 | ',', 0, TOK_COMMA, |
| 504 | '?', 0, TOK_CONDITIONAL, |
| 505 | ':', 0, TOK_CONDITIONAL_SEP, |
| 506 | ')', 0, TOK_RPAREN, |
| 507 | '(', 0, TOK_LPAREN, |
| 508 | 0 |
| 509 | }; |
| 510 | /* ptr to ")" */ |
| 511 | #define endexpression &op_tokens[sizeof(op_tokens)-7] |
| 512 | |
| 513 | /* |
| 514 | * Return of a legal variable name (a letter or underscore followed by zero or |
| 515 | * more letters, underscores, and digits). |
| 516 | */ |
| 517 | |
| 518 | static inline char * |
| 519 | endofname(const char *name) |
| 520 | { |
| 521 | char *p; |
| 522 | |
| 523 | p = (char *) name; |
| 524 | if (! ID(*p)) |
| 525 | return p; |
| 526 | while (*++p) { |
| 527 | if (! ISALNUM(*p)) |
| 528 | break; |
| 529 | } |
| 530 | return p; |
| 531 | } |
| 532 | |
| 533 | |
Rob Landley | 7bfa88f | 2006-02-13 19:16:41 +0000 | [diff] [blame] | 534 | static arith_t arith (const char *expr, int *perrcode) |
| 535 | { |
| 536 | char arithval; /* Current character under analysis */ |
| 537 | operator lasttok, op; |
| 538 | operator prec; |
| 539 | |
| 540 | const char *p = endexpression; |
| 541 | int errcode; |
| 542 | |
| 543 | size_t datasizes = strlen(expr) + 2; |
| 544 | |
| 545 | /* Stack of integers */ |
| 546 | /* The proof that there can be no more than strlen(startbuf)/2+1 integers |
| 547 | * in any given correct or incorrect expression is left as an exercise to |
| 548 | * the reader. */ |
| 549 | v_n_t *numstack = alloca(((datasizes)/2)*sizeof(v_n_t)), |
| 550 | *numstackptr = numstack; |
| 551 | /* Stack of operator tokens */ |
| 552 | operator *stack = alloca((datasizes) * sizeof(operator)), |
| 553 | *stackptr = stack; |
| 554 | |
| 555 | *stackptr++ = lasttok = TOK_LPAREN; /* start off with a left paren */ |
| 556 | *perrcode = errcode = 0; |
| 557 | |
| 558 | while(1) { |
| 559 | if ((arithval = *expr) == 0) { |
| 560 | if (p == endexpression) { |
| 561 | /* Null expression. */ |
| 562 | err: |
| 563 | return (*perrcode = -1); |
| 564 | } |
| 565 | |
| 566 | /* This is only reached after all tokens have been extracted from the |
| 567 | * input stream. If there are still tokens on the operator stack, they |
| 568 | * are to be applied in order. At the end, there should be a final |
| 569 | * result on the integer stack */ |
| 570 | |
| 571 | if (expr != endexpression + 1) { |
| 572 | /* If we haven't done so already, */ |
| 573 | /* append a closing right paren */ |
| 574 | expr = endexpression; |
| 575 | /* and let the loop process it. */ |
| 576 | continue; |
| 577 | } |
| 578 | /* At this point, we're done with the expression. */ |
| 579 | if (numstackptr != numstack+1) { |
| 580 | /* ... but if there isn't, it's bad */ |
| 581 | goto err; |
| 582 | } |
| 583 | ret: |
| 584 | *perrcode = errcode; |
| 585 | return numstack->val; |
| 586 | } else { |
| 587 | /* Continue processing the expression. */ |
| 588 | if (arith_isspace(arithval)) { |
| 589 | /* Skip whitespace */ |
| 590 | goto prologue; |
| 591 | } |
| 592 | if((p = endofname(expr)) != expr) { |
| 593 | size_t var_name_size = (p-expr); |
| 594 | |
| 595 | if(var_name_size == 7 && |
| 596 | strncmp(expr, "defined", var_name_size) == 0) { |
| 597 | int brace_form = 0; |
| 598 | const char *v; |
| 599 | |
| 600 | while(arith_isspace(*p)) p++; |
| 601 | if(*p == '(') { |
| 602 | p++; |
| 603 | while(arith_isspace(*p)) p++; |
| 604 | brace_form = 1; |
| 605 | } |
| 606 | expr = p; |
| 607 | if((p = endofname(expr)) == expr) |
| 608 | goto err; |
| 609 | var_name_size = (p-expr); |
| 610 | while(arith_isspace(*p)) p++; |
| 611 | if(brace_form && *p++ != ')') |
| 612 | goto err; |
| 613 | v = lookup_key(expr, var_name_size); |
| 614 | numstackptr->val = (v != NULL) ? 1 : 0; |
| 615 | } else { |
| 616 | errcode = arith_lookup_val(expr, var_name_size, |
| 617 | &(numstackptr->val)); |
| 618 | if(errcode) goto ret; |
| 619 | } |
| 620 | expr = p; |
| 621 | num: |
| 622 | numstackptr->contidional_second_val_initialized = 0; |
| 623 | numstackptr++; |
| 624 | lasttok = TOK_NUM; |
| 625 | continue; |
| 626 | } else if (arithval >= '0' && arithval <= '9') { |
| 627 | numstackptr->val = strtoll(expr, (char **) &expr, 0); |
| 628 | while(*expr == 'l' || *expr == 'L' || *expr == 'u' || |
| 629 | *expr == 'U') |
| 630 | expr++; |
| 631 | goto num; |
| 632 | } |
| 633 | for(p = op_tokens; ; p++) { |
| 634 | const char *o; |
| 635 | |
| 636 | if(*p == 0) { |
| 637 | /* strange operator not found */ |
| 638 | goto err; |
| 639 | } |
| 640 | for(o = expr; *p && *o == *p; p++) |
| 641 | o++; |
| 642 | if(! *p) { |
| 643 | /* found */ |
| 644 | expr = o - 1; |
| 645 | break; |
| 646 | } |
| 647 | /* skip tail uncompared token */ |
| 648 | while(*p) |
| 649 | p++; |
| 650 | /* skip zero delim */ |
| 651 | p++; |
| 652 | } |
| 653 | op = p[1]; |
| 654 | |
| 655 | /* Plus and minus are binary (not unary) _only_ if the last |
| 656 | * token was as number, or a right paren (which pretends to be |
| 657 | * a number, since it evaluates to one). Think about it. |
| 658 | * It makes sense. */ |
| 659 | if (lasttok != TOK_NUM) { |
| 660 | if(op == TOK_ADD) |
| 661 | op = TOK_UPLUS; |
| 662 | else if(op == TOK_SUB) |
| 663 | op = TOK_UMINUS; |
| 664 | } |
| 665 | /* We don't want a unary operator to cause recursive descent on the |
| 666 | * stack, because there can be many in a row and it could cause an |
| 667 | * operator to be evaluated before its argument is pushed onto the |
| 668 | * integer stack. */ |
| 669 | /* But for binary operators, "apply" everything on the operator |
| 670 | * stack until we find an operator with a lesser priority than the |
| 671 | * one we have just extracted. */ |
| 672 | /* Left paren is given the lowest priority so it will never be |
| 673 | * "applied" in this way. |
| 674 | * if associativity is right and priority eq, applied also skip |
| 675 | */ |
| 676 | prec = PREC(op); |
| 677 | if ((prec > 0 && prec < UNARYPREC) || prec == SPEC_PREC) { |
| 678 | /* not left paren or unary */ |
| 679 | if (lasttok != TOK_NUM) { |
| 680 | /* binary op must be preceded by a num */ |
| 681 | goto err; |
| 682 | } |
| 683 | while (stackptr != stack) { |
| 684 | if (op == TOK_RPAREN) { |
| 685 | /* The algorithm employed here is simple: while we don't |
| 686 | * hit an open paren nor the bottom of the stack, pop |
| 687 | * tokens and apply them */ |
| 688 | if (stackptr[-1] == TOK_LPAREN) { |
| 689 | --stackptr; |
| 690 | /* Any operator directly after a */ |
| 691 | lasttok = TOK_NUM; |
| 692 | /* close paren should consider itself binary */ |
| 693 | goto prologue; |
| 694 | } |
| 695 | } else { |
| 696 | operator prev_prec = PREC(stackptr[-1]); |
| 697 | |
| 698 | if (prev_prec < prec) |
| 699 | break; |
| 700 | /* check right assoc */ |
| 701 | if(prev_prec == prec && prec == PREC(TOK_CONDITIONAL)) |
| 702 | break; |
| 703 | } |
| 704 | errcode = arith_apply(*--stackptr, numstack, &numstackptr); |
| 705 | if(errcode) goto ret; |
| 706 | } |
| 707 | if (op == TOK_RPAREN) { |
| 708 | goto err; |
| 709 | } |
| 710 | } |
| 711 | |
| 712 | /* Push this operator to the stack and remember it. */ |
| 713 | *stackptr++ = lasttok = op; |
| 714 | |
| 715 | prologue: |
| 716 | ++expr; |
| 717 | } |
| 718 | } |
| 719 | } |
| 720 | |
| 721 | /* stupid C lexical analyser for configs.h */ |
| 722 | static void c_lex_config(const char *fname, long fsize) |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 723 | { |
"Vladimir N. Oleynik" | 083d3f4 | 2005-10-10 11:35:17 +0000 | [diff] [blame] | 724 | int c; |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 725 | int state; |
| 726 | int line; |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 727 | char *id = id_s; |
"Vladimir N. Oleynik" | 63ca3bf | 2006-02-14 09:23:25 +0000 | [diff] [blame] | 728 | size_t id_len = 0; /* stupid initialization */ |
Rob Landley | 7bfa88f | 2006-02-13 19:16:41 +0000 | [diff] [blame] | 729 | unsigned char *optr, *oend; |
| 730 | int mode = CONFIG_MODE; |
| 731 | |
| 732 | int fd; |
| 733 | char *map; |
| 734 | int mapsize; |
| 735 | |
| 736 | if(fsize == 0) { |
| 737 | fprintf(stderr, "Warning: %s is empty\n", fname); |
| 738 | return; |
| 739 | } |
| 740 | fd = open(fname, O_RDONLY); |
| 741 | if(fd < 0) { |
| 742 | perror(fname); |
| 743 | return; |
| 744 | } |
| 745 | mapsize = (fsize+pagesizem1) & ~pagesizem1; |
| 746 | map = mmap(NULL, mapsize, PROT_READ, MAP_PRIVATE, fd, 0); |
| 747 | if ((long) map == -1) |
| 748 | bb_error_d("%s: mmap: %m", fname); |
| 749 | |
| 750 | optr = (unsigned char *)map; |
| 751 | oend = optr + fsize; |
| 752 | |
| 753 | line = 1; |
| 754 | state = S; |
| 755 | |
| 756 | for(;;) { |
| 757 | getc1(); |
| 758 | for(;;) { |
| 759 | /* [ \t]+ eat first space */ |
| 760 | while(c == ' ' || c == '\t') |
| 761 | getc1(); |
| 762 | |
| 763 | if(state == S) { |
| 764 | while(first_chars[c] == ANY) { |
| 765 | /* <S>unparsed */ |
| 766 | if(c == '\n') |
| 767 | line++; |
| 768 | getc1(); |
| 769 | } |
| 770 | if(c == L_EOF) { |
| 771 | /* <S><<EOF>> */ |
| 772 | munmap(map, mapsize); |
| 773 | close(fd); |
| 774 | if(mode != CONFIG_MODE) |
| 775 | yy_error_d("expected #endif"); |
| 776 | return; |
| 777 | } |
| 778 | if(c == REM) { |
| 779 | /* <S>/ */ |
| 780 | getc0(); |
| 781 | if(c == REM) { |
| 782 | /* <S>"//"[^\n]* */ |
| 783 | do getc0(); while(c != '\n' && c != L_EOF); |
| 784 | } else if(c == '*') { |
| 785 | /* <S>[/][*] goto parse block comments */ |
| 786 | break; |
| 787 | } |
| 788 | } else if(c == POUND) { |
| 789 | /* <S># */ |
| 790 | state = c; |
| 791 | getc1(); |
| 792 | } else if(c == STR || c == CHR) { |
| 793 | /* <S>\"|\' */ |
| 794 | int qc = c; |
| 795 | |
| 796 | for(;;) { |
| 797 | /* <STR,CHR>. */ |
| 798 | getc1(); |
| 799 | if(c == qc) { |
| 800 | /* <STR>\" or <CHR>\' */ |
| 801 | break; |
| 802 | } |
| 803 | if(c == BS) { |
| 804 | /* <STR,CHR>\\ but is not <STR,CHR>\\\n */ |
| 805 | getc0(); |
| 806 | } |
| 807 | if(c == '\n' || c == L_EOF) |
| 808 | yy_error_d("unterminated"); |
| 809 | } |
| 810 | getc1(); |
| 811 | } else { |
| 812 | /* <S>[A-Z_a-z0-9] */ |
| 813 | |
| 814 | /* trick for fast drop id |
| 815 | if key with this first char undefined */ |
| 816 | if(first_chars[c] == 0 || (mode & FALSE_MODES) != 0) { |
| 817 | /* skip <S>[A-Z_a-z0-9]+ */ |
| 818 | do getc1(); while(isalnums[c]); |
| 819 | } else { |
| 820 | id_len = 0; |
| 821 | do { |
| 822 | /* <S>[A-Z_a-z0-9]+ */ |
| 823 | put_id(c); |
| 824 | getc1(); |
| 825 | } while(isalnums[c]); |
| 826 | check_key(key_top, id, id_len); |
| 827 | } |
| 828 | } |
| 829 | continue; |
| 830 | } |
| 831 | /* begin preprocessor states */ |
| 832 | if(c == L_EOF) |
| 833 | yy_error_d("unexpected EOF"); |
| 834 | if(c == REM) { |
| 835 | /* <#.*>/ */ |
| 836 | getc0(); |
| 837 | if(c == REM) |
| 838 | yy_error_d("detected // in preprocessor line"); |
| 839 | if(c == '*') { |
| 840 | /* <#.*>[/][*] goto parse block comments */ |
| 841 | break; |
| 842 | } |
| 843 | /* hmm, #.*[/] */ |
| 844 | yy_error_d("strange preprocessor line"); |
| 845 | } |
| 846 | if(state == POUND) { |
| 847 | /* tricks */ |
| 848 | int diu = (int)first_chars_deiu[c]; /* preproc ptr */ |
| 849 | |
| 850 | state = S; |
| 851 | if(diu != S) { |
| 852 | int p_num_str, p_num_max; |
| 853 | |
| 854 | getc1(); |
| 855 | id_len = 0; |
| 856 | while(isalnums[c]) { |
| 857 | put_id(c); |
| 858 | getc1(); |
| 859 | } |
| 860 | put_id(0); |
| 861 | p_num_str = diu & 0xf; |
| 862 | p_num_max = diu >> 4; |
| 863 | for(diu = p_num_str; diu <= p_num_max; diu++) |
| 864 | if(!strcmp(id, preproc[diu])) { |
| 865 | state = (diu + '0'); |
| 866 | /* common */ |
| 867 | id_len = 0; |
| 868 | break; |
| 869 | } |
| 870 | } else { |
| 871 | while(isalnums[c]) getc1(); |
| 872 | } |
| 873 | } else if(state == EF) { |
| 874 | /* #endif */ |
| 875 | pop_mode(); |
| 876 | state = S; |
| 877 | } else if(state == I) { |
| 878 | if(c == STR && (mode & FALSE_MODES) == 0) { |
| 879 | /* <I>\" */ |
| 880 | for(;;) { |
| 881 | getc1(); |
| 882 | if(c == STR) |
| 883 | break; |
| 884 | if(c == L_EOF) |
| 885 | yy_error_d("unexpected EOF"); |
| 886 | put_id(c); |
| 887 | } |
| 888 | put_id(0); |
| 889 | /* store "include.h" */ |
| 890 | parse_inc(id, fname); |
| 891 | getc1(); |
| 892 | } |
| 893 | /* else another (may be wrong) #include ... */ |
| 894 | state = S; |
| 895 | } else if(state == F) { |
| 896 | arith_t t; |
| 897 | int errcode; |
| 898 | |
| 899 | while(c != '\n' && c != L_EOF) { |
| 900 | put_id(c); |
| 901 | getc1(); |
| 902 | } |
| 903 | put_id(0); |
| 904 | t = arith(id, &errcode); |
| 905 | if (errcode < 0) { |
| 906 | if (errcode == -2) |
| 907 | yy_error_d("divide by zero"); |
| 908 | else if (errcode == -4) |
| 909 | yy_error_d("undefined"); |
| 910 | else if (errcode == -5) |
| 911 | yy_error_d("expression recursion loop detected"); |
| 912 | else |
| 913 | yy_error_d("syntax error"); |
| 914 | } |
| 915 | push_mode(); |
| 916 | mode = t != 0 ? IF1_MODE : IF0_MODE; |
| 917 | state = S; |
| 918 | } else if(state == IFD || state == IFND) { |
| 919 | /* save KEY from #if(n)def KEY ... */ |
| 920 | const char *v; |
| 921 | |
| 922 | push_mode(); |
| 923 | while(isalnums[c]) { |
| 924 | put_id(c); |
| 925 | getc1(); |
| 926 | } |
| 927 | if(!id_len) |
| 928 | yy_error_d("expected identifier"); |
| 929 | v = lookup_key(id, id_len); |
| 930 | mode = IF1_MODE; |
| 931 | if(state == IFD && v == NULL) |
| 932 | mode = IF0_MODE; |
| 933 | else if(state == IFND && v != NULL) |
| 934 | mode = IF0_MODE; |
| 935 | state = S; |
| 936 | } else if(state == EI) { |
| 937 | /* #elif */ |
| 938 | if(mode == CONFIG_MODE || mode == ELSE0_MODE || mode == ELSE1_MODE) |
| 939 | yy_error_d("unexpected #elif"); |
| 940 | if(mode == IF0_MODE) { |
| 941 | pop_mode(); |
| 942 | state = F; |
| 943 | } else { |
| 944 | mode = ELIF1_MODE; |
| 945 | state = S; |
| 946 | } |
| 947 | } else if(state == E) { |
| 948 | if(mode == CONFIG_MODE || mode == ELSE0_MODE || mode == ELSE1_MODE) |
| 949 | yy_error_d("unexpected #else"); |
| 950 | if(mode == IF0_MODE) |
| 951 | mode = ELSE0_MODE; |
| 952 | else if(mode == IF1_MODE) |
| 953 | mode = ELSE1_MODE; |
| 954 | state = S; |
| 955 | } else if(state == D || state == U) { |
| 956 | /* save KEY from #"define"|"undef" ... */ |
| 957 | while(isalnums[c]) { |
| 958 | put_id(c); |
| 959 | getc1(); |
| 960 | } |
| 961 | if(!id_len) |
| 962 | yy_error_d("expected identifier"); |
| 963 | if(state == U) { |
| 964 | if((mode & FALSE_MODES) == 0) |
| 965 | parse_conf_opt(id, NULL, id_len); |
| 966 | state = S; |
| 967 | } else { |
| 968 | /* D -> DK */ |
| 969 | state = DK; |
| 970 | } |
| 971 | } else { |
| 972 | /* state==<DK> #define KEY[ ] */ |
| 973 | size_t opt_len = id_len; |
| 974 | char *val = id + opt_len; |
| 975 | char *sp; |
| 976 | |
| 977 | for(;;) { |
| 978 | if(c == L_EOF || c == '\n') |
| 979 | break; |
| 980 | put_id(c); |
| 981 | getc1(); |
| 982 | } |
| 983 | sp = id + id_len; |
| 984 | put_id(0); |
| 985 | /* trim tail spaces */ |
| 986 | while(--sp >= val && (*sp == ' ' || *sp == '\t' |
| 987 | || *sp == '\f' || *sp == '\v')) |
| 988 | *sp = '\0'; |
| 989 | if((mode & FALSE_MODES) == 0) |
| 990 | parse_conf_opt(id, val, opt_len); |
| 991 | state = S; |
| 992 | } |
| 993 | } |
| 994 | |
| 995 | /* <REM> */ |
| 996 | getc0(); |
| 997 | for(;;) { |
| 998 | /* <REM>[^*]+ */ |
| 999 | while(c != '*') { |
| 1000 | if(c == '\n') { |
| 1001 | /* <REM>\n */ |
| 1002 | if(state != S) |
| 1003 | yy_error_d("unexpected newline"); |
| 1004 | line++; |
| 1005 | } else if(c == L_EOF) |
| 1006 | yy_error_d("unexpected EOF"); |
| 1007 | getc0(); |
| 1008 | } |
| 1009 | /* <REM>[*] */ |
| 1010 | getc0(); |
| 1011 | if(c == REM) { |
| 1012 | /* <REM>[*][/] */ |
| 1013 | break; |
| 1014 | } |
| 1015 | } |
| 1016 | } |
| 1017 | too_long: |
| 1018 | yy_error_d("phrase too long"); |
| 1019 | } |
| 1020 | |
| 1021 | /* trick for fast find "define", "include", "undef" */ |
| 1022 | static const char first_chars_diu[UCHAR_MAX] = { |
| 1023 | [(int)'d'] = (char)5, /* strlen("define") - 1; */ |
| 1024 | [(int)'i'] = (char)6, /* strlen("include") - 1; */ |
| 1025 | [(int)'u'] = (char)4, /* strlen("undef") - 1; */ |
| 1026 | }; |
| 1027 | |
| 1028 | #undef D |
| 1029 | #undef I |
| 1030 | #undef U |
| 1031 | #define D '5' /* #define preprocessor's directive */ |
| 1032 | #define I '6' /* #include preprocessor's directive */ |
| 1033 | #define U '4' /* #undef preprocessor's directive */ |
| 1034 | |
| 1035 | /* stupid C lexical analyser for sources */ |
| 1036 | static void c_lex_src(const char *fname, long fsize) |
| 1037 | { |
| 1038 | int c; |
| 1039 | int state; |
| 1040 | int line; |
| 1041 | char *id = id_s; |
"Vladimir N. Oleynik" | 63ca3bf | 2006-02-14 09:23:25 +0000 | [diff] [blame] | 1042 | size_t id_len = 0; /* stupid initialization */ |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 1043 | unsigned char *optr, *oend; |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 1044 | |
| 1045 | int fd; |
| 1046 | char *map; |
| 1047 | int mapsize; |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 1048 | |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 1049 | if(fsize == 0) { |
| 1050 | fprintf(stderr, "Warning: %s is empty\n", fname); |
| 1051 | return; |
| 1052 | } |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 1053 | fd = open(fname, O_RDONLY); |
| 1054 | if(fd < 0) { |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 1055 | perror(fname); |
| 1056 | return; |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 1057 | } |
| 1058 | mapsize = (fsize+pagesizem1) & ~pagesizem1; |
| 1059 | map = mmap(NULL, mapsize, PROT_READ, MAP_PRIVATE, fd, 0); |
| 1060 | if ((long) map == -1) |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 1061 | bb_error_d("%s: mmap: %m", fname); |
| 1062 | |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 1063 | optr = (unsigned char *)map; |
| 1064 | oend = optr + fsize; |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 1065 | |
| 1066 | line = 1; |
"Vladimir N. Oleynik" | 083d3f4 | 2005-10-10 11:35:17 +0000 | [diff] [blame] | 1067 | state = S; |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 1068 | |
| 1069 | for(;;) { |
"Vladimir N. Oleynik" | 083d3f4 | 2005-10-10 11:35:17 +0000 | [diff] [blame] | 1070 | getc1(); |
| 1071 | for(;;) { |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 1072 | /* [ \t]+ eat first space */ |
| 1073 | while(c == ' ' || c == '\t') |
| 1074 | getc1(); |
| 1075 | |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 1076 | if(state == S) { |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 1077 | while(first_chars[c] == ANY) { |
| 1078 | /* <S>unparsed */ |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 1079 | if(c == '\n') |
| 1080 | line++; |
| 1081 | getc1(); |
| 1082 | } |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 1083 | if(c == L_EOF) { |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 1084 | /* <S><<EOF>> */ |
| 1085 | munmap(map, mapsize); |
| 1086 | close(fd); |
| 1087 | return; |
| 1088 | } |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 1089 | if(c == REM) { |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 1090 | /* <S>/ */ |
"Vladimir N. Oleynik" | 083d3f4 | 2005-10-10 11:35:17 +0000 | [diff] [blame] | 1091 | getc0(); |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 1092 | if(c == REM) { |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 1093 | /* <S>"//"[^\n]* */ |
"Vladimir N. Oleynik" | 083d3f4 | 2005-10-10 11:35:17 +0000 | [diff] [blame] | 1094 | do getc0(); while(c != '\n' && c != L_EOF); |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 1095 | } else if(c == '*') { |
"Vladimir N. Oleynik" | 083d3f4 | 2005-10-10 11:35:17 +0000 | [diff] [blame] | 1096 | /* <S>[/][*] goto parse block comments */ |
| 1097 | break; |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 1098 | } |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 1099 | } else if(c == POUND) { |
| 1100 | /* <S># */ |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 1101 | state = c; |
"Vladimir N. Oleynik" | 083d3f4 | 2005-10-10 11:35:17 +0000 | [diff] [blame] | 1102 | getc1(); |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 1103 | } else if(c == STR || c == CHR) { |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 1104 | /* <S>\"|\' */ |
"Vladimir N. Oleynik" | 083d3f4 | 2005-10-10 11:35:17 +0000 | [diff] [blame] | 1105 | int qc = c; |
| 1106 | |
| 1107 | for(;;) { |
| 1108 | /* <STR,CHR>. */ |
| 1109 | getc1(); |
| 1110 | if(c == qc) { |
| 1111 | /* <STR>\" or <CHR>\' */ |
| 1112 | break; |
| 1113 | } |
| 1114 | if(c == BS) { |
| 1115 | /* <STR,CHR>\\ but is not <STR,CHR>\\\n */ |
| 1116 | getc0(); |
| 1117 | } |
| 1118 | if(c == '\n' || c == L_EOF) |
| 1119 | yy_error_d("unterminated"); |
| 1120 | } |
| 1121 | getc1(); |
| 1122 | } else { |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 1123 | /* <S>[A-Z_a-z0-9] */ |
"Vladimir N. Oleynik" | b1fe462 | 2005-09-12 16:39:47 +0000 | [diff] [blame] | 1124 | |
| 1125 | /* trick for fast drop id |
| 1126 | if key with this first char undefined */ |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 1127 | if(first_chars[c] == 0) { |
"Vladimir N. Oleynik" | b1fe462 | 2005-09-12 16:39:47 +0000 | [diff] [blame] | 1128 | /* skip <S>[A-Z_a-z0-9]+ */ |
"Vladimir N. Oleynik" | af0dd59 | 2005-09-16 13:57:33 +0000 | [diff] [blame] | 1129 | do getc1(); while(isalnums[c]); |
"Vladimir N. Oleynik" | b1fe462 | 2005-09-12 16:39:47 +0000 | [diff] [blame] | 1130 | } else { |
| 1131 | id_len = 0; |
| 1132 | do { |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 1133 | /* <S>[A-Z_a-z0-9]+ */ |
| 1134 | put_id(c); |
| 1135 | getc1(); |
"Vladimir N. Oleynik" | af0dd59 | 2005-09-16 13:57:33 +0000 | [diff] [blame] | 1136 | } while(isalnums[c]); |
"Vladimir N. Oleynik" | d128b71 | 2005-10-03 10:08:46 +0000 | [diff] [blame] | 1137 | check_key(key_top, id, id_len); |
"Vladimir N. Oleynik" | b1fe462 | 2005-09-12 16:39:47 +0000 | [diff] [blame] | 1138 | } |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 1139 | } |
| 1140 | continue; |
| 1141 | } |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 1142 | /* begin preprocessor states */ |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 1143 | if(c == L_EOF) |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 1144 | yy_error_d("unexpected EOF"); |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 1145 | if(c == REM) { |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 1146 | /* <#.*>/ */ |
"Vladimir N. Oleynik" | 083d3f4 | 2005-10-10 11:35:17 +0000 | [diff] [blame] | 1147 | getc0(); |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 1148 | if(c == REM) |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 1149 | yy_error_d("detected // in preprocessor line"); |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 1150 | if(c == '*') { |
"Vladimir N. Oleynik" | 083d3f4 | 2005-10-10 11:35:17 +0000 | [diff] [blame] | 1151 | /* <#.*>[/][*] goto parse block comments */ |
| 1152 | break; |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 1153 | } |
| 1154 | /* hmm, #.*[/] */ |
| 1155 | yy_error_d("strange preprocessor line"); |
| 1156 | } |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 1157 | if(state == POUND) { |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 1158 | /* tricks */ |
Rob Landley | 7bfa88f | 2006-02-13 19:16:41 +0000 | [diff] [blame] | 1159 | static const char * const p_preproc[] = { |
"Vladimir N. Oleynik" | 083d3f4 | 2005-10-10 11:35:17 +0000 | [diff] [blame] | 1160 | /* 0 1 2 3 4 5 6 */ |
| 1161 | "", "", "", "", "ndef", "efine", "nclude" |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 1162 | }; |
Rob Landley | 7bfa88f | 2006-02-13 19:16:41 +0000 | [diff] [blame] | 1163 | size_t diu = first_chars_diu[c]; /* strlen and p_preproc ptr */ |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 1164 | |
"Vladimir N. Oleynik" | 083d3f4 | 2005-10-10 11:35:17 +0000 | [diff] [blame] | 1165 | state = S; |
| 1166 | if(diu != S) { |
| 1167 | getc1(); |
| 1168 | id_len = 0; |
| 1169 | while(isalnums[c]) { |
| 1170 | put_id(c); |
| 1171 | getc1(); |
| 1172 | } |
"Vladimir N. Oleynik" | 63ca3bf | 2006-02-14 09:23:25 +0000 | [diff] [blame] | 1173 | /* str begins with c, read == strlen key and compared */ |
Rob Landley | 7bfa88f | 2006-02-13 19:16:41 +0000 | [diff] [blame] | 1174 | if(diu == id_len && !memcmp(id, p_preproc[diu], diu)) { |
"Vladimir N. Oleynik" | 083d3f4 | 2005-10-10 11:35:17 +0000 | [diff] [blame] | 1175 | state = diu + '0'; |
| 1176 | id_len = 0; /* common for save */ |
| 1177 | } |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 1178 | } else { |
"Vladimir N. Oleynik" | 083d3f4 | 2005-10-10 11:35:17 +0000 | [diff] [blame] | 1179 | while(isalnums[c]) getc1(); |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 1180 | } |
"Vladimir N. Oleynik" | 083d3f4 | 2005-10-10 11:35:17 +0000 | [diff] [blame] | 1181 | } else if(state == I) { |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 1182 | if(c == STR) { |
| 1183 | /* <I>\" */ |
"Vladimir N. Oleynik" | 083d3f4 | 2005-10-10 11:35:17 +0000 | [diff] [blame] | 1184 | for(;;) { |
| 1185 | getc1(); |
| 1186 | if(c == STR) |
| 1187 | break; |
| 1188 | if(c == L_EOF) |
| 1189 | yy_error_d("unexpected EOF"); |
| 1190 | put_id(c); |
| 1191 | } |
| 1192 | put_id(0); |
| 1193 | /* store "include.h" */ |
| 1194 | parse_inc(id, fname); |
| 1195 | getc1(); |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 1196 | } |
"Vladimir N. Oleynik" | 083d3f4 | 2005-10-10 11:35:17 +0000 | [diff] [blame] | 1197 | /* else another (may be wrong) #include ... */ |
| 1198 | state = S; |
Rob Landley | 7bfa88f | 2006-02-13 19:16:41 +0000 | [diff] [blame] | 1199 | } else /* if(state == D || state == U) */ { |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 1200 | /* ignore depend with #define or #undef KEY */ |
"Vladimir N. Oleynik" | 083d3f4 | 2005-10-10 11:35:17 +0000 | [diff] [blame] | 1201 | while(isalnums[c]) getc1(); |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 1202 | state = S; |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 1203 | } |
| 1204 | } |
"Vladimir N. Oleynik" | 083d3f4 | 2005-10-10 11:35:17 +0000 | [diff] [blame] | 1205 | |
| 1206 | /* <REM> */ |
| 1207 | getc0(); |
| 1208 | for(;;) { |
| 1209 | /* <REM>[^*]+ */ |
| 1210 | while(c != '*') { |
| 1211 | if(c == '\n') { |
| 1212 | /* <REM>\n */ |
| 1213 | if(state != S) |
| 1214 | yy_error_d("unexpected newline"); |
| 1215 | line++; |
| 1216 | } else if(c == L_EOF) |
| 1217 | yy_error_d("unexpected EOF"); |
| 1218 | getc0(); |
| 1219 | } |
| 1220 | /* <REM>[*] */ |
| 1221 | getc0(); |
| 1222 | if(c == REM) { |
| 1223 | /* <REM>[*][/] */ |
| 1224 | break; |
| 1225 | } |
| 1226 | } |
| 1227 | } |
| 1228 | too_long: |
| 1229 | yy_error_d("phrase too long"); |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 1230 | } |
| 1231 | |
| 1232 | |
"Vladimir N. Oleynik" | 63ca3bf | 2006-02-14 09:23:25 +0000 | [diff] [blame] | 1233 | /* bb_simplify_path special variant for absolute pathname */ |
"Vladimir N. Oleynik" | 083d3f4 | 2005-10-10 11:35:17 +0000 | [diff] [blame] | 1234 | static size_t bb_qa_simplify_path(char *path) |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 1235 | { |
"Vladimir N. Oleynik" | 083d3f4 | 2005-10-10 11:35:17 +0000 | [diff] [blame] | 1236 | char *s, *p; |
| 1237 | |
| 1238 | p = s = path; |
| 1239 | |
| 1240 | do { |
| 1241 | if (*p == '/') { |
| 1242 | if (*s == '/') { /* skip duplicate (or initial) slash */ |
| 1243 | continue; |
| 1244 | } else if (*s == '.') { |
| 1245 | if (s[1] == '/' || s[1] == 0) { /* remove extra '.' */ |
| 1246 | continue; |
| 1247 | } else if ((s[1] == '.') && (s[2] == '/' || s[2] == 0)) { |
| 1248 | ++s; |
| 1249 | if (p > path) { |
| 1250 | while (*--p != '/'); /* omit previous dir */ |
| 1251 | } |
| 1252 | continue; |
| 1253 | } |
| 1254 | } |
| 1255 | } |
| 1256 | *++p = *s; |
| 1257 | } while (*++s); |
| 1258 | |
| 1259 | if ((p == path) || (*p != '/')) { /* not a trailing slash */ |
| 1260 | ++p; /* so keep last character */ |
| 1261 | } |
| 1262 | *p = 0; |
| 1263 | |
| 1264 | return (p - path); |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 1265 | } |
| 1266 | |
"Vladimir N. Oleynik" | 083d3f4 | 2005-10-10 11:35:17 +0000 | [diff] [blame] | 1267 | static void parse_inc(const char *include, const char *fname) |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 1268 | { |
| 1269 | bb_key_t *cur; |
"Vladimir N. Oleynik" | 083d3f4 | 2005-10-10 11:35:17 +0000 | [diff] [blame] | 1270 | const char *p_i; |
| 1271 | llist_t *lo; |
| 1272 | char *ap; |
| 1273 | size_t key_sz; |
"Vladimir N. Oleynik" | dd1ccdd | 2006-02-16 15:40:24 +0000 | [diff] [blame] | 1274 | struct stat st; |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 1275 | |
"Vladimir N. Oleynik" | 083d3f4 | 2005-10-10 11:35:17 +0000 | [diff] [blame] | 1276 | if(*include == '/') { |
| 1277 | lo = NULL; |
| 1278 | ap = bb_xstrdup(include); |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 1279 | } else { |
"Vladimir N. Oleynik" | 083d3f4 | 2005-10-10 11:35:17 +0000 | [diff] [blame] | 1280 | int w; |
| 1281 | const char *p; |
| 1282 | |
| 1283 | lo = Iop; |
"Vladimir N. Oleynik" | 63ca3bf | 2006-02-14 09:23:25 +0000 | [diff] [blame] | 1284 | p = strrchr(fname, '/'); /* fname has absolute pathname */ |
"Vladimir N. Oleynik" | 083d3f4 | 2005-10-10 11:35:17 +0000 | [diff] [blame] | 1285 | w = (p-fname); |
| 1286 | /* find from current directory of source file */ |
| 1287 | ap = bb_asprint("%.*s/%s", w, fname, include); |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 1288 | } |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 1289 | |
"Vladimir N. Oleynik" | 083d3f4 | 2005-10-10 11:35:17 +0000 | [diff] [blame] | 1290 | for(;;) { |
| 1291 | key_sz = bb_qa_simplify_path(ap); |
| 1292 | cur = check_key(Ifound, ap, key_sz); |
| 1293 | if(cur) { |
| 1294 | cur->checked = cur->value; |
| 1295 | free(ap); |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 1296 | return; |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 1297 | } |
"Vladimir N. Oleynik" | dd1ccdd | 2006-02-16 15:40:24 +0000 | [diff] [blame] | 1298 | if(stat(ap, &st) == 0) { |
"Vladimir N. Oleynik" | 083d3f4 | 2005-10-10 11:35:17 +0000 | [diff] [blame] | 1299 | /* found */ |
"Vladimir N. Oleynik" | dd1ccdd | 2006-02-16 15:40:24 +0000 | [diff] [blame] | 1300 | llist_t *cfl; |
| 1301 | |
| 1302 | for(cfl = configs; cfl; cfl = cfl->link) { |
| 1303 | struct stat *config = (struct stat *)cfl->data; |
| 1304 | |
| 1305 | if (st.st_dev == config->st_dev && st.st_ino == config->st_ino) { |
| 1306 | /* skip depend with bb_configs.h */ |
"Vladimir N. Oleynik" | b267f13 | 2006-02-17 09:05:41 +0000 | [diff] [blame] | 1307 | return; |
"Vladimir N. Oleynik" | dd1ccdd | 2006-02-16 15:40:24 +0000 | [diff] [blame] | 1308 | } |
| 1309 | } |
"Vladimir N. Oleynik" | 083d3f4 | 2005-10-10 11:35:17 +0000 | [diff] [blame] | 1310 | p_i = ap; |
| 1311 | break; |
| 1312 | } else if(lo == NULL) { |
| 1313 | p_i = NULL; |
| 1314 | break; |
| 1315 | } |
| 1316 | |
| 1317 | /* find from "-I include" specified directories */ |
| 1318 | free(ap); |
"Vladimir N. Oleynik" | 63ca3bf | 2006-02-14 09:23:25 +0000 | [diff] [blame] | 1319 | /* lo->data has absolute pathname */ |
"Vladimir N. Oleynik" | 083d3f4 | 2005-10-10 11:35:17 +0000 | [diff] [blame] | 1320 | ap = bb_asprint("%s/%s", lo->data, include); |
| 1321 | lo = lo->link; |
| 1322 | } |
| 1323 | |
| 1324 | cur = xmalloc(sizeof(bb_key_t)); |
Rob Landley | 7bfa88f | 2006-02-13 19:16:41 +0000 | [diff] [blame] | 1325 | cur->keyname = ap; |
"Vladimir N. Oleynik" | 083d3f4 | 2005-10-10 11:35:17 +0000 | [diff] [blame] | 1326 | cur->key_sz = key_sz; |
Rob Landley | 7bfa88f | 2006-02-13 19:16:41 +0000 | [diff] [blame] | 1327 | cur->stored_path = ap; |
"Vladimir N. Oleynik" | 083d3f4 | 2005-10-10 11:35:17 +0000 | [diff] [blame] | 1328 | cur->value = cur->checked = p_i; |
| 1329 | if(p_i == NULL && noiwarning) |
| 1330 | fprintf(stderr, "%s: Warning: #include \"%s\" not found\n", fname, include); |
| 1331 | cur->next = Ifound; |
| 1332 | Ifound = cur; |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 1333 | } |
| 1334 | |
"Vladimir N. Oleynik" | 083d3f4 | 2005-10-10 11:35:17 +0000 | [diff] [blame] | 1335 | static size_t max_rec_sz; |
| 1336 | |
| 1337 | static void parse_conf_opt(const char *opt, const char *val, size_t key_sz) |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 1338 | { |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 1339 | bb_key_t *cur; |
"Vladimir N. Oleynik" | d128b71 | 2005-10-03 10:08:46 +0000 | [diff] [blame] | 1340 | char *k; |
"Vladimir N. Oleynik" | 083d3f4 | 2005-10-10 11:35:17 +0000 | [diff] [blame] | 1341 | char *p; |
| 1342 | size_t val_sz = 0; |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 1343 | |
"Vladimir N. Oleynik" | d128b71 | 2005-10-03 10:08:46 +0000 | [diff] [blame] | 1344 | cur = check_key(key_top, opt, key_sz); |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 1345 | if(cur != NULL) { |
"Vladimir N. Oleynik" | 63ca3bf | 2006-02-14 09:23:25 +0000 | [diff] [blame] | 1346 | /* already present */ |
"Vladimir N. Oleynik" | d128b71 | 2005-10-03 10:08:46 +0000 | [diff] [blame] | 1347 | cur->checked = NULL; /* store only */ |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 1348 | if(cur->value == NULL && val == NULL) |
| 1349 | return; |
"Vladimir N. Oleynik" | d128b71 | 2005-10-03 10:08:46 +0000 | [diff] [blame] | 1350 | if(cur->value != NULL && val != NULL && !strcmp(cur->value, val)) |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 1351 | return; |
"Vladimir N. Oleynik" | d128b71 | 2005-10-03 10:08:46 +0000 | [diff] [blame] | 1352 | k = cur->keyname; |
| 1353 | fprintf(stderr, "Warning: redefined %s\n", k); |
"Vladimir N. Oleynik" | af0dd59 | 2005-09-16 13:57:33 +0000 | [diff] [blame] | 1354 | } else { |
"Vladimir N. Oleynik" | 083d3f4 | 2005-10-10 11:35:17 +0000 | [diff] [blame] | 1355 | size_t recordsz; |
| 1356 | |
| 1357 | if(val && *val) |
| 1358 | val_sz = strlen(val) + 1; |
| 1359 | recordsz = key_sz + val_sz + 1; |
| 1360 | if(max_rec_sz < recordsz) |
| 1361 | max_rec_sz = recordsz; |
| 1362 | cur = xmalloc(sizeof(bb_key_t) + recordsz); |
| 1363 | k = cur->keyname = memcpy(cur + 1, opt, key_sz); |
| 1364 | cur->keyname[key_sz] = '\0'; |
| 1365 | cur->key_sz = key_sz; |
| 1366 | cur->checked = NULL; |
| 1367 | cur->src_have_this_key = NULL; |
| 1368 | cur->next = key_top; |
| 1369 | key_top = cur; |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 1370 | } |
"Vladimir N. Oleynik" | 083d3f4 | 2005-10-10 11:35:17 +0000 | [diff] [blame] | 1371 | /* save VAL */ |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 1372 | if(val) { |
| 1373 | if(*val == '\0') { |
| 1374 | cur->value = ""; |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 1375 | } else { |
"Vladimir N. Oleynik" | 083d3f4 | 2005-10-10 11:35:17 +0000 | [diff] [blame] | 1376 | cur->value = p = cur->keyname + key_sz + 1; |
| 1377 | memcpy(p, val, val_sz); |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 1378 | } |
| 1379 | } else { |
| 1380 | cur->value = NULL; |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 1381 | } |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 1382 | /* trick, save first char KEY for do fast identify id */ |
"Vladimir N. Oleynik" | d128b71 | 2005-10-03 10:08:46 +0000 | [diff] [blame] | 1383 | first_chars[(int)*k] = *k; |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 1384 | |
"Vladimir N. Oleynik" | d128b71 | 2005-10-03 10:08:46 +0000 | [diff] [blame] | 1385 | cur->stored_path = k = bb_asprint("%s/%s.h", kp, k); |
"Vladimir N. Oleynik" | 63ca3bf | 2006-02-14 09:23:25 +0000 | [diff] [blame] | 1386 | /* key conversion [A-Z_] -> [a-z/] */ |
"Vladimir N. Oleynik" | d128b71 | 2005-10-03 10:08:46 +0000 | [diff] [blame] | 1387 | for(p = k + kp_len + 1; *p; p++) { |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 1388 | if(*p >= 'A' && *p <= 'Z') |
| 1389 | *p = *p - 'A' + 'a'; |
"Vladimir N. Oleynik" | af0dd59 | 2005-09-16 13:57:33 +0000 | [diff] [blame] | 1390 | else if(*p == '_' && p[1] > '9') /* do not change A_1 to A/1 */ |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 1391 | *p = '/'; |
| 1392 | } |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 1393 | } |
| 1394 | |
"Vladimir N. Oleynik" | 083d3f4 | 2005-10-10 11:35:17 +0000 | [diff] [blame] | 1395 | static void store_keys(void) |
| 1396 | { |
| 1397 | bb_key_t *cur; |
| 1398 | char *k; |
| 1399 | struct stat st; |
| 1400 | int cmp_ok; |
| 1401 | ssize_t rw_ret; |
| 1402 | size_t recordsz = max_rec_sz * 2 + 10 * 2 + 16; |
| 1403 | /* buffer for double "#define KEY VAL\n" */ |
| 1404 | char *record_buf = xmalloc(recordsz); |
| 1405 | |
| 1406 | for(cur = key_top; cur; cur = cur->next) { |
| 1407 | if(cur->src_have_this_key) { |
| 1408 | /* do generate record */ |
| 1409 | k = cur->keyname; |
| 1410 | if(cur->value == NULL) { |
| 1411 | recordsz = sprintf(record_buf, "#undef %s\n", k); |
| 1412 | } else { |
| 1413 | const char *val = cur->value; |
| 1414 | if(*val == '\0') { |
| 1415 | recordsz = sprintf(record_buf, "#define %s\n", k); |
| 1416 | } else { |
"Vladimir N. Oleynik" | 6732af2 | 2006-02-15 12:29:37 +0000 | [diff] [blame] | 1417 | if(val[0] != '(') |
| 1418 | recordsz = sprintf(record_buf, "#define %s %s\n", k, val); |
| 1419 | else |
| 1420 | recordsz = sprintf(record_buf, "#define %s%s\n", k, val); |
"Vladimir N. Oleynik" | 083d3f4 | 2005-10-10 11:35:17 +0000 | [diff] [blame] | 1421 | } |
| 1422 | } |
| 1423 | /* size_t -> ssize_t :( */ |
| 1424 | rw_ret = (ssize_t)recordsz; |
"Vladimir N. Oleynik" | 63ca3bf | 2006-02-14 09:23:25 +0000 | [diff] [blame] | 1425 | /* check kp/key.h, compare after previous use */ |
"Vladimir N. Oleynik" | 083d3f4 | 2005-10-10 11:35:17 +0000 | [diff] [blame] | 1426 | cmp_ok = 0; |
| 1427 | k = cur->stored_path; |
| 1428 | if(stat(k, &st)) { |
| 1429 | char *p; |
| 1430 | for(p = k + kp_len + 1; *p; p++) { |
| 1431 | /* Auto-create directories. */ |
| 1432 | if (*p == '/') { |
| 1433 | *p = '\0'; |
| 1434 | if (access(k, F_OK) != 0 && mkdir(k, 0755) != 0) |
| 1435 | bb_error_d("mkdir(%s): %m", k); |
| 1436 | *p = '/'; |
| 1437 | } |
| 1438 | } |
| 1439 | } else { |
| 1440 | /* found */ |
| 1441 | if(st.st_size == (off_t)recordsz) { |
| 1442 | char *r_cmp; |
| 1443 | int fd; |
| 1444 | size_t padded = recordsz; |
| 1445 | |
| 1446 | /* 16-byte padding for read(2) and memcmp(3) */ |
| 1447 | padded = (padded+15) & ~15; |
| 1448 | r_cmp = record_buf + padded; |
| 1449 | fd = open(k, O_RDONLY); |
| 1450 | if(fd < 0 || read(fd, r_cmp, recordsz) < rw_ret) |
| 1451 | bb_error_d("%s: %m", k); |
| 1452 | close(fd); |
| 1453 | cmp_ok = memcmp(record_buf, r_cmp, recordsz) == 0; |
| 1454 | } |
| 1455 | } |
| 1456 | if(!cmp_ok) { |
| 1457 | int fd = open(k, O_WRONLY|O_CREAT|O_TRUNC, 0644); |
| 1458 | if(fd < 0 || write(fd, record_buf, recordsz) < rw_ret) |
| 1459 | bb_error_d("%s: %m", k); |
| 1460 | close(fd); |
| 1461 | } |
| 1462 | } |
| 1463 | } |
| 1464 | } |
"Vladimir N. Oleynik" | 6c0642d | 2005-10-07 15:36:26 +0000 | [diff] [blame] | 1465 | |
| 1466 | static int show_dep(int first, bb_key_t *k, const char *name, const char *f) |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 1467 | { |
| 1468 | bb_key_t *cur; |
| 1469 | |
| 1470 | for(cur = k; cur; cur = cur->next) { |
"Vladimir N. Oleynik" | d128b71 | 2005-10-03 10:08:46 +0000 | [diff] [blame] | 1471 | if(cur->checked) { |
"Vladimir N. Oleynik" | 083d3f4 | 2005-10-10 11:35:17 +0000 | [diff] [blame] | 1472 | if(first >= 0) { |
| 1473 | if(first) { |
| 1474 | if(f == NULL) |
"Vladimir N. Oleynik" | 6c0642d | 2005-10-07 15:36:26 +0000 | [diff] [blame] | 1475 | printf("\n%s:", name); |
"Vladimir N. Oleynik" | 083d3f4 | 2005-10-10 11:35:17 +0000 | [diff] [blame] | 1476 | else |
"Vladimir N. Oleynik" | 6c0642d | 2005-10-07 15:36:26 +0000 | [diff] [blame] | 1477 | printf("\n%s/%s:", pwd, name); |
"Vladimir N. Oleynik" | 083d3f4 | 2005-10-10 11:35:17 +0000 | [diff] [blame] | 1478 | first = 0; |
| 1479 | } else { |
| 1480 | printf(" \\\n "); |
| 1481 | } |
| 1482 | printf(" %s", cur->checked); |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 1483 | } |
"Vladimir N. Oleynik" | 083d3f4 | 2005-10-10 11:35:17 +0000 | [diff] [blame] | 1484 | cur->src_have_this_key = cur->checked; |
| 1485 | cur->checked = NULL; |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 1486 | } |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 1487 | } |
| 1488 | return first; |
| 1489 | } |
| 1490 | |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 1491 | static char * |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 1492 | parse_chd(const char *fe, const char *p, size_t dirlen) |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 1493 | { |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 1494 | struct stat st; |
| 1495 | char *fp; |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 1496 | size_t df_sz; |
"Vladimir N. Oleynik" | 083d3f4 | 2005-10-10 11:35:17 +0000 | [diff] [blame] | 1497 | static char dir_and_entry[4096]; |
| 1498 | size_t fe_sz = strlen(fe) + 1; |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 1499 | |
"Vladimir N. Oleynik" | 083d3f4 | 2005-10-10 11:35:17 +0000 | [diff] [blame] | 1500 | df_sz = dirlen + fe_sz + 1; /* dir/file\0 */ |
| 1501 | if(df_sz > sizeof(dir_and_entry)) |
| 1502 | bb_error_d("%s: file name too long", fe); |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 1503 | fp = dir_and_entry; |
"Vladimir N. Oleynik" | 083d3f4 | 2005-10-10 11:35:17 +0000 | [diff] [blame] | 1504 | /* sprintf(fp, "%s/%s", p, fe); */ |
| 1505 | memcpy(fp, p, dirlen); |
| 1506 | fp[dirlen] = '/'; |
| 1507 | memcpy(fp + dirlen + 1, fe, fe_sz); |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 1508 | |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 1509 | if(stat(fp, &st)) { |
"Vladimir N. Oleynik" | d128b71 | 2005-10-03 10:08:46 +0000 | [diff] [blame] | 1510 | fprintf(stderr, "Warning: stat(%s): %m\n", fp); |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 1511 | return NULL; |
| 1512 | } |
"Vladimir N. Oleynik" | b1fe462 | 2005-09-12 16:39:47 +0000 | [diff] [blame] | 1513 | if(S_ISREG(st.st_mode)) { |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 1514 | llist_t *cfl; |
| 1515 | char *e = fp + df_sz - 3; |
| 1516 | |
| 1517 | if(*e++ != '.' || (*e != 'c' && *e != 'h')) { |
"Vladimir N. Oleynik" | b1fe462 | 2005-09-12 16:39:47 +0000 | [diff] [blame] | 1518 | /* direntry is regular file, but is not *.[ch] */ |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 1519 | return NULL; |
| 1520 | } |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 1521 | for(cfl = configs; cfl; cfl = cfl->link) { |
| 1522 | struct stat *config = (struct stat *)cfl->data; |
| 1523 | |
| 1524 | if (st.st_dev == config->st_dev && st.st_ino == config->st_ino) { |
"Vladimir N. Oleynik" | dd1ccdd | 2006-02-16 15:40:24 +0000 | [diff] [blame] | 1525 | /* skip already parsed bb_configs.h */ |
"Vladimir N. Oleynik" | b1fe462 | 2005-09-12 16:39:47 +0000 | [diff] [blame] | 1526 | return NULL; |
| 1527 | } |
"Vladimir N. Oleynik" | b1fe462 | 2005-09-12 16:39:47 +0000 | [diff] [blame] | 1528 | } |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 1529 | /* direntry is *.[ch] regular file and is not configs */ |
Rob Landley | 7bfa88f | 2006-02-13 19:16:41 +0000 | [diff] [blame] | 1530 | c_lex_src(fp, st.st_size); |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 1531 | if(!dontgenerate_dep) { |
| 1532 | int first; |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 1533 | if(*e == 'c') { |
| 1534 | /* *.c -> *.o */ |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 1535 | *e = 'o'; |
"Vladimir N. Oleynik" | 6c0642d | 2005-10-07 15:36:26 +0000 | [diff] [blame] | 1536 | /* /src_dir/path/file.o to path/file.o */ |
| 1537 | fp += replace; |
| 1538 | if(*fp == '/') |
"Vladimir N. Oleynik" | 083d3f4 | 2005-10-10 11:35:17 +0000 | [diff] [blame] | 1539 | fp++; |
"Vladimir N. Oleynik" | d5f2a18 | 2005-10-06 14:47:16 +0000 | [diff] [blame] | 1540 | } else { |
| 1541 | e = NULL; |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 1542 | } |
"Vladimir N. Oleynik" | 6c0642d | 2005-10-07 15:36:26 +0000 | [diff] [blame] | 1543 | first = show_dep(1, Ifound, fp, e); |
| 1544 | first = show_dep(first, key_top, fp, e); |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 1545 | if(first == 0) |
| 1546 | putchar('\n'); |
"Vladimir N. Oleynik" | 083d3f4 | 2005-10-10 11:35:17 +0000 | [diff] [blame] | 1547 | } else { |
| 1548 | show_dep(-1, key_top, NULL, NULL); |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 1549 | } |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 1550 | return NULL; |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 1551 | } else if(S_ISDIR(st.st_mode)) { |
| 1552 | if (st.st_dev == st_kp.st_dev && st.st_ino == st_kp.st_ino) |
| 1553 | return NULL; /* drop scan kp/ directory */ |
"Vladimir N. Oleynik" | 083d3f4 | 2005-10-10 11:35:17 +0000 | [diff] [blame] | 1554 | /* direntry is directory. buff is returned */ |
| 1555 | return bb_xstrdup(fp); |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 1556 | } |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 1557 | /* hmm, direntry is device! */ |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 1558 | return NULL; |
| 1559 | } |
| 1560 | |
"Vladimir N. Oleynik" | 63ca3bf | 2006-02-14 09:23:25 +0000 | [diff] [blame] | 1561 | /* from libbb but inlined for speed considerations */ |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 1562 | static inline llist_t *llist_add_to(llist_t *old_head, char *new_item) |
| 1563 | { |
| 1564 | llist_t *new_head; |
| 1565 | |
| 1566 | new_head = xmalloc(sizeof(llist_t)); |
| 1567 | new_head->data = new_item; |
| 1568 | new_head->link = old_head; |
| 1569 | |
| 1570 | return(new_head); |
| 1571 | } |
| 1572 | |
"Vladimir N. Oleynik" | d128b71 | 2005-10-03 10:08:46 +0000 | [diff] [blame] | 1573 | static void scan_dir_find_ch_files(const char *p) |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 1574 | { |
| 1575 | llist_t *dirs; |
| 1576 | llist_t *d_add; |
| 1577 | llist_t *d; |
| 1578 | struct dirent *de; |
| 1579 | DIR *dir; |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 1580 | size_t dirlen; |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 1581 | |
"Vladimir N. Oleynik" | d128b71 | 2005-10-03 10:08:46 +0000 | [diff] [blame] | 1582 | dirs = llist_add_to(NULL, bb_simplify_path(p)); |
"Vladimir N. Oleynik" | 6c0642d | 2005-10-07 15:36:26 +0000 | [diff] [blame] | 1583 | replace = strlen(dirs->data); |
"Vladimir N. Oleynik" | 63ca3bf | 2006-02-14 09:23:25 +0000 | [diff] [blame] | 1584 | /* emulate recursion */ |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 1585 | while(dirs) { |
| 1586 | d_add = NULL; |
| 1587 | while(dirs) { |
| 1588 | dir = opendir(dirs->data); |
| 1589 | if (dir == NULL) |
"Vladimir N. Oleynik" | d128b71 | 2005-10-03 10:08:46 +0000 | [diff] [blame] | 1590 | fprintf(stderr, "Warning: opendir(%s): %m\n", dirs->data); |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 1591 | dirlen = strlen(dirs->data); |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 1592 | while ((de = readdir(dir)) != NULL) { |
"Vladimir N. Oleynik" | d128b71 | 2005-10-03 10:08:46 +0000 | [diff] [blame] | 1593 | char *found_dir; |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 1594 | |
"Vladimir N. Oleynik" | d128b71 | 2005-10-03 10:08:46 +0000 | [diff] [blame] | 1595 | if (de->d_name[0] == '.') |
| 1596 | continue; |
| 1597 | found_dir = parse_chd(de->d_name, dirs->data, dirlen); |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 1598 | if(found_dir) |
| 1599 | d_add = llist_add_to(d_add, found_dir); |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 1600 | } |
| 1601 | closedir(dir); |
"Vladimir N. Oleynik" | d128b71 | 2005-10-03 10:08:46 +0000 | [diff] [blame] | 1602 | free(dirs->data); |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 1603 | d = dirs; |
| 1604 | dirs = dirs->link; |
| 1605 | free(d); |
| 1606 | } |
| 1607 | dirs = d_add; |
| 1608 | } |
| 1609 | } |
| 1610 | |
Bernhard Reutner-Fischer | d293375 | 2006-05-26 19:58:58 +0000 | [diff] [blame] | 1611 | static void show_usage(void) ATTRIBUTE ((noreturn)); |
"Vladimir N. Oleynik" | 083d3f4 | 2005-10-10 11:35:17 +0000 | [diff] [blame] | 1612 | static void show_usage(void) |
| 1613 | { |
| 1614 | bb_error_d("%s\n%s\n", bb_mkdep_terse_options, bb_mkdep_full_options); |
| 1615 | } |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 1616 | |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 1617 | int main(int argc, char **argv) |
| 1618 | { |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 1619 | char *s; |
| 1620 | int i; |
| 1621 | llist_t *fl; |
| 1622 | |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 1623 | { |
Bernhard Reutner-Fischer | 5ba53c0 | 2006-02-14 10:43:40 +0000 | [diff] [blame] | 1624 | /* for bb_simplify_path, this program has no chdir() */ |
"Vladimir N. Oleynik" | 083d3f4 | 2005-10-10 11:35:17 +0000 | [diff] [blame] | 1625 | /* libbb-like my xgetcwd() */ |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 1626 | unsigned path_max = 512; |
| 1627 | |
| 1628 | s = xmalloc (path_max); |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 1629 | while (getcwd (s, path_max) == NULL) { |
| 1630 | if(errno != ERANGE) |
| 1631 | bb_error_d("getcwd: %m"); |
"Vladimir N. Oleynik" | 083d3f4 | 2005-10-10 11:35:17 +0000 | [diff] [blame] | 1632 | free(s); |
| 1633 | s = xmalloc(path_max *= 2); |
| 1634 | } |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 1635 | pwd = s; |
| 1636 | } |
| 1637 | |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 1638 | while ((i = getopt(argc, argv, "I:c:dk:w")) > 0) { |
| 1639 | switch(i) { |
| 1640 | case 'I': |
"Vladimir N. Oleynik" | 083d3f4 | 2005-10-10 11:35:17 +0000 | [diff] [blame] | 1641 | s = bb_simplify_path(optarg); |
| 1642 | Iop = llist_add_to(Iop, s); |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 1643 | break; |
| 1644 | case 'c': |
| 1645 | s = bb_simplify_path(optarg); |
| 1646 | configs = llist_add_to(configs, s); |
| 1647 | break; |
| 1648 | case 'd': |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 1649 | dontgenerate_dep = 1; |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 1650 | break; |
| 1651 | case 'k': |
| 1652 | if(kp) |
| 1653 | bb_error_d("Hmm, why multiple -k?"); |
| 1654 | kp = bb_simplify_path(optarg); |
| 1655 | break; |
| 1656 | case 'w': |
| 1657 | noiwarning = 1; |
| 1658 | break; |
| 1659 | default: |
| 1660 | show_usage(); |
| 1661 | } |
| 1662 | } |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 1663 | /* default kp */ |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 1664 | if(kp == NULL) |
| 1665 | kp = bb_simplify_path(INCLUDE_CONFIG_PATH); |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 1666 | /* globals initialize */ |
| 1667 | kp_len = strlen(kp); |
| 1668 | if(stat(kp, &st_kp)) |
| 1669 | bb_error_d("stat(%s): %m", kp); |
| 1670 | if(!S_ISDIR(st_kp.st_mode)) |
| 1671 | bb_error_d("%s is not directory", kp); |
| 1672 | /* defaults */ |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 1673 | if(Iop == NULL) |
"Vladimir N. Oleynik" | 083d3f4 | 2005-10-10 11:35:17 +0000 | [diff] [blame] | 1674 | Iop = llist_add_to(Iop, bb_simplify_path(LOCAL_INCLUDE_PATH)); |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 1675 | if(configs == NULL) { |
| 1676 | s = bb_simplify_path(INCLUDE_CONFIG_KEYS_PATH); |
| 1677 | configs = llist_add_to(configs, s); |
| 1678 | } |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 1679 | /* for c_lex */ |
| 1680 | pagesizem1 = getpagesize() - 1; |
"Vladimir N. Oleynik" | 083d3f4 | 2005-10-10 11:35:17 +0000 | [diff] [blame] | 1681 | for(i = 0; i < UCHAR_MAX; i++) { |
"Vladimir N. Oleynik" | af0dd59 | 2005-09-16 13:57:33 +0000 | [diff] [blame] | 1682 | if(ISALNUM(i)) |
| 1683 | isalnums[i] = i; |
"Vladimir N. Oleynik" | 63ca3bf | 2006-02-14 09:23:25 +0000 | [diff] [blame] | 1684 | /* set unparsed chars to speed up the parser */ |
"Vladimir N. Oleynik" | 083d3f4 | 2005-10-10 11:35:17 +0000 | [diff] [blame] | 1685 | else if(i != CHR && i != STR && i != POUND && i != REM) |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 1686 | first_chars[i] = ANY; |
| 1687 | } |
| 1688 | first_chars[i] = '-'; /* L_EOF */ |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 1689 | |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 1690 | /* parse configs */ |
| 1691 | for(fl = configs; fl; fl = fl->link) { |
| 1692 | struct stat st; |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 1693 | |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 1694 | if(stat(fl->data, &st)) |
| 1695 | bb_error_d("stat(%s): %m", fl->data); |
Rob Landley | 7bfa88f | 2006-02-13 19:16:41 +0000 | [diff] [blame] | 1696 | c_lex_config(fl->data, st.st_size); |
"Vladimir N. Oleynik" | 083d3f4 | 2005-10-10 11:35:17 +0000 | [diff] [blame] | 1697 | free(fl->data); |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 1698 | /* trick for fast comparing found files with configs */ |
"Vladimir N. Oleynik" | 083d3f4 | 2005-10-10 11:35:17 +0000 | [diff] [blame] | 1699 | fl->data = xmalloc(sizeof(struct stat)); |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 1700 | memcpy(fl->data, &st, sizeof(struct stat)); |
| 1701 | } |
| 1702 | |
| 1703 | /* main loop */ |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 1704 | argv += optind; |
| 1705 | if(*argv) { |
| 1706 | while(*argv) |
| 1707 | scan_dir_find_ch_files(*argv++); |
| 1708 | } else { |
| 1709 | scan_dir_find_ch_files("."); |
| 1710 | } |
"Vladimir N. Oleynik" | 083d3f4 | 2005-10-10 11:35:17 +0000 | [diff] [blame] | 1711 | store_keys(); |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 1712 | return 0; |
| 1713 | } |
| 1714 | |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 1715 | /* partial and simplified libbb routine */ |
"Vladimir N. Oleynik" | b1fe462 | 2005-09-12 16:39:47 +0000 | [diff] [blame] | 1716 | static void bb_error_d(const char *s, ...) |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 1717 | { |
| 1718 | va_list p; |
| 1719 | |
| 1720 | va_start(p, s); |
| 1721 | vfprintf(stderr, s, p); |
| 1722 | va_end(p); |
| 1723 | putc('\n', stderr); |
| 1724 | exit(1); |
| 1725 | } |
| 1726 | |
"Vladimir N. Oleynik" | b1fe462 | 2005-09-12 16:39:47 +0000 | [diff] [blame] | 1727 | static char *bb_asprint(const char *format, ...) |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 1728 | { |
| 1729 | va_list p; |
| 1730 | int r; |
| 1731 | char *out; |
| 1732 | |
Bernhard Reutner-Fischer | d293375 | 2006-05-26 19:58:58 +0000 | [diff] [blame] | 1733 | #ifdef __USE_GNU |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 1734 | va_start(p, format); |
| 1735 | r = vasprintf(&out, format, p); |
| 1736 | va_end(p); |
Bernhard Reutner-Fischer | d293375 | 2006-05-26 19:58:58 +0000 | [diff] [blame] | 1737 | #else |
| 1738 | out = xmalloc(BUFSIZ); |
| 1739 | va_start(p, format); |
| 1740 | r = vsprintf(out, format, p); |
| 1741 | va_end(p); |
| 1742 | #endif |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 1743 | |
| 1744 | if (r < 0) |
| 1745 | bb_error_d("bb_asprint: %m"); |
| 1746 | return out; |
| 1747 | } |
| 1748 | |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 1749 | /* partial libbb routine as is */ |
Rob Landley | 7bfa88f | 2006-02-13 19:16:41 +0000 | [diff] [blame] | 1750 | |
"Vladimir N. Oleynik" | b1fe462 | 2005-09-12 16:39:47 +0000 | [diff] [blame] | 1751 | static char *bb_simplify_path(const char *path) |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 1752 | { |
| 1753 | char *s, *start, *p; |
| 1754 | |
| 1755 | if (path[0] == '/') |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 1756 | start = bb_xstrdup(path); |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 1757 | else { |
Bernhard Reutner-Fischer | 5ba53c0 | 2006-02-14 10:43:40 +0000 | [diff] [blame] | 1758 | /* is not libbb, but this program has no chdir() */ |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 1759 | start = bb_asprint("%s/%s", pwd, path); |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 1760 | } |
| 1761 | p = s = start; |
| 1762 | |
| 1763 | do { |
"Vladimir N. Oleynik" | b1fe462 | 2005-09-12 16:39:47 +0000 | [diff] [blame] | 1764 | if (*p == '/') { |
| 1765 | if (*s == '/') { /* skip duplicate (or initial) slash */ |
| 1766 | continue; |
| 1767 | } else if (*s == '.') { |
| 1768 | if (s[1] == '/' || s[1] == 0) { /* remove extra '.' */ |
| 1769 | continue; |
| 1770 | } else if ((s[1] == '.') && (s[2] == '/' || s[2] == 0)) { |
| 1771 | ++s; |
| 1772 | if (p > start) { |
| 1773 | while (*--p != '/'); /* omit previous dir */ |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 1774 | } |
"Vladimir N. Oleynik" | b1fe462 | 2005-09-12 16:39:47 +0000 | [diff] [blame] | 1775 | continue; |
| 1776 | } |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 1777 | } |
"Vladimir N. Oleynik" | b1fe462 | 2005-09-12 16:39:47 +0000 | [diff] [blame] | 1778 | } |
| 1779 | *++p = *s; |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 1780 | } while (*++s); |
| 1781 | |
"Vladimir N. Oleynik" | b1fe462 | 2005-09-12 16:39:47 +0000 | [diff] [blame] | 1782 | if ((p == start) || (*p != '/')) { /* not a trailing slash */ |
| 1783 | ++p; /* so keep last character */ |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 1784 | } |
| 1785 | *p = 0; |
| 1786 | |
| 1787 | return start; |
| 1788 | } |