blob: 39d4605afe558d60dbec9e13abd8b8e963403488 [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00002/*
"Vladimir N. Oleynik"dd1ccdd2006-02-16 15:40:24 +00003 * Another fast dependencies generator for Makefiles, Version 4.2
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00004 *
Rob Landley7bfa88f2006-02-13 19:16:41 +00005 * Copyright (C) 2005,2006 by Vladimir Oleynik <dzo@simtreas.ru>
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00006 *
"Vladimir N. Oleynik"af0dd592005-09-16 13:57:33 +00007 * mmaping file may be originally by Linus Torvalds.
8 *
Rob Landley7bfa88f2006-02-13 19:16:41 +00009 * 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"083d3f42005-10-10 11:35:17 +000014 * 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 Landley7bfa88f2006-02-13 19:16:41 +000024 * (c) 2005,2006 Bernhard Fischer:
"Vladimir N. Oleynik"af0dd592005-09-16 13:57:33 +000025 * - commentary typos,
26 * - move "memory exhausted" into msg_enomem,
27 * - more verbose --help output.
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +000028 *
"Vladimir N. Oleynik"7573ac62005-09-14 15:09:06 +000029 * This program does:
"Vladimir N. Oleynik"6732af22006-02-15 12:29:37 +000030 * 1) find #define KEY VALUE or #undef KEY from include/bb_config.h
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +000031 * 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"664c6e72005-10-06 14:53:43 +000034 * pwd/file.o: include/config/key*.h found_include_*.h
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +000035 * path/inc.h: include/config/key*.h found_included_include_*.h
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +000036 * 5) save include/config/key*.h if changed after previous usage
"Vladimir N. Oleynik"af0dd592005-09-16 13:57:33 +000037 * This program does not generate dependencies for #include <...>
Rob Landley7bfa88f2006-02-13 19:16:41 +000038 * Config file can have #if #elif #else #ifdef #ifndef #endif lines
"Vladimir N. Oleynik"7573ac62005-09-14 15:09:06 +000039 */
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +000040
41#define LOCAL_INCLUDE_PATH "include"
42#define INCLUDE_CONFIG_PATH LOCAL_INCLUDE_PATH"/config"
"Vladimir N. Oleynik"6732af22006-02-15 12:29:37 +000043#define INCLUDE_CONFIG_KEYS_PATH LOCAL_INCLUDE_PATH"/bb_config.h"
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +000044
"Vladimir N. Oleynik"7573ac62005-09-14 15:09:06 +000045#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"5e60dc42005-09-12 12:33:27 +000058#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"083d3f42005-10-10 11:35:17 +000071#include <limits.h>
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +000072
Bernhard Reutner-Fischerd2933752006-05-26 19:58:58 +000073#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"7573ac62005-09-14 15:09:06 +000082
"Vladimir N. Oleynik"7573ac62005-09-14 15:09:06 +000083/* partial and simplified libbb routine */
Bernhard Reutner-Fischerd2933752006-05-26 19:58:58 +000084static void bb_error_d(const char *s, ...) ATTRIBUTE ((noreturn, format (printf, 1, 2)));
85static char * bb_asprint(const char *format, ...) ATTRIBUTE ((format (printf, 1, 2)));
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +000086static char *bb_simplify_path(const char *path);
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +000087
88/* stolen from libbb as is */
89typedef struct llist_s {
90 char *data;
91 struct llist_s *link;
92} llist_t;
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +000093
"Vladimir N. Oleynik"7573ac62005-09-14 15:09:06 +000094static const char msg_enomem[] = "memory exhausted";
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +000095
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +000096/* inline realization for fast works */
97static 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
106static 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
116static int dontgenerate_dep; /* flag -d usaged */
117static int noiwarning; /* flag -w usaged */
118static llist_t *configs; /* list of -c usaged and them stat() after parsed */
119static llist_t *Iop; /* list of -I include usaged */
120
121static char *pwd; /* current work directory */
Bernhard Reutner-Fischer5ba53c02006-02-14 10:43:40 +0000122static size_t replace; /* replace current work directory with build dir */
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +0000123
"Vladimir N. Oleynik"63ca3bf2006-02-14 09:23:25 +0000124static const char *kp; /* KEY path, argument of -k used */
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +0000125static size_t kp_len;
126static struct stat st_kp; /* stat(kp) */
127
128typedef 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
138static bb_key_t *key_top; /* list of found KEYs */
139static bb_key_t *Ifound; /* list of parsed includes */
140
141
142static void parse_conf_opt(const char *opt, const char *val, size_t key_sz);
143static void parse_inc(const char *include, const char *fname);
144
145static 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 Landley7bfa88f2006-02-13 19:16:41 +0000158static 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"7573ac62005-09-14 15:09:06 +0000170/* for lexical analyser */
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +0000171static int pagesizem1; /* padding mask = getpagesize() - 1 */
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +0000172
"Vladimir N. Oleynik"7573ac62005-09-14 15:09:06 +0000173/* for speed tricks */
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +0000174static char first_chars[1+UCHAR_MAX]; /* + L_EOF */
175static char isalnums[1+UCHAR_MAX]; /* + L_EOF */
Rob Landley7bfa88f2006-02-13 19:16:41 +0000176
177/* trick for fast find "define", "include", "undef",
178"if((n)def)" "else", "endif" */
179static 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" };
182static 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"6c0642d2005-10-07 15:36:26 +0000187};
"Vladimir N. Oleynik"b1fe4622005-09-12 16:39:47 +0000188
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +0000189#define CONFIG_MODE 0
Rob Landley7bfa88f2006-02-13 19:16:41 +0000190#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"7573ac62005-09-14 15:09:06 +0000196
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +0000197#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"676e95e2005-09-13 16:50:53 +0000203#define REM '/' /* block comment */
204#define BS '\\' /* back slash */
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +0000205#define POUND '#' /* # */
Rob Landley7bfa88f2006-02-13 19:16:41 +0000206#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"5e60dc42005-09-12 12:33:27 +0000215#define DK 'K' /* #define KEY... (config mode) */
"Vladimir N. Oleynik"676e95e2005-09-13 16:50:53 +0000216#define ANY '*' /* any unparsed chars */
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +0000217
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"083d3f42005-10-10 11:35:17 +0000223#define L_EOF (1+UCHAR_MAX)
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +0000224
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +0000225#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
232static 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"5e60dc42005-09-12 12:33:27 +0000235
Rob Landley7bfa88f2006-02-13 19:16:41 +0000236static char ifcpp_stack[1024];
237static 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"7573ac62005-09-14 15:09:06 +0000243
Rob Landley7bfa88f2006-02-13 19:16:41 +0000244#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 */
251typedef long long arith_t;
252
253static 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
266typedef 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"6732af22006-02-15 12:29:37 +0000321#define SPEC_PREC (UNARYPREC+2)
Rob Landley7bfa88f2006-02-13 19:16:41 +0000322
323#define TOK_NUM tok_decl(SPEC_PREC, 0)
324#define TOK_RPAREN tok_decl(SPEC_PREC, 1)
325
326#define NUMPTR (*numstackptr)
327
328typedef 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
335typedef 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
341static chk_var_recursive_looped_t *prev_chk_var_recursive;
342
343
344static 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 */
382static inline int
383arith_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;
478err: return(-1);
479}
480
481/* longest must first */
482static 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
518static inline char *
519endofname(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 Landley7bfa88f2006-02-13 19:16:41 +0000534static 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. */
562err:
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 */
722static void c_lex_config(const char *fname, long fsize)
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +0000723{
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +0000724 int c;
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +0000725 int state;
726 int line;
"Vladimir N. Oleynik"676e95e2005-09-13 16:50:53 +0000727 char *id = id_s;
"Vladimir N. Oleynik"63ca3bf2006-02-14 09:23:25 +0000728 size_t id_len = 0; /* stupid initialization */
Rob Landley7bfa88f2006-02-13 19:16:41 +0000729 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 }
1017too_long:
1018 yy_error_d("phrase too long");
1019}
1020
1021/* trick for fast find "define", "include", "undef" */
1022static 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 */
1036static 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"63ca3bf2006-02-14 09:23:25 +00001042 size_t id_len = 0; /* stupid initialization */
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001043 unsigned char *optr, *oend;
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001044
1045 int fd;
1046 char *map;
1047 int mapsize;
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001048
"Vladimir N. Oleynik"7573ac62005-09-14 15:09:06 +00001049 if(fsize == 0) {
1050 fprintf(stderr, "Warning: %s is empty\n", fname);
1051 return;
1052 }
"Vladimir N. Oleynik"676e95e2005-09-13 16:50:53 +00001053 fd = open(fname, O_RDONLY);
1054 if(fd < 0) {
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001055 perror(fname);
1056 return;
"Vladimir N. Oleynik"676e95e2005-09-13 16:50:53 +00001057 }
1058 mapsize = (fsize+pagesizem1) & ~pagesizem1;
1059 map = mmap(NULL, mapsize, PROT_READ, MAP_PRIVATE, fd, 0);
1060 if ((long) map == -1)
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001061 bb_error_d("%s: mmap: %m", fname);
1062
"Vladimir N. Oleynik"676e95e2005-09-13 16:50:53 +00001063 optr = (unsigned char *)map;
1064 oend = optr + fsize;
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001065
1066 line = 1;
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001067 state = S;
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001068
1069 for(;;) {
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001070 getc1();
1071 for(;;) {
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001072 /* [ \t]+ eat first space */
1073 while(c == ' ' || c == '\t')
1074 getc1();
1075
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001076 if(state == S) {
"Vladimir N. Oleynik"676e95e2005-09-13 16:50:53 +00001077 while(first_chars[c] == ANY) {
1078 /* <S>unparsed */
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001079 if(c == '\n')
1080 line++;
1081 getc1();
1082 }
"Vladimir N. Oleynik"676e95e2005-09-13 16:50:53 +00001083 if(c == L_EOF) {
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001084 /* <S><<EOF>> */
1085 munmap(map, mapsize);
1086 close(fd);
1087 return;
1088 }
"Vladimir N. Oleynik"676e95e2005-09-13 16:50:53 +00001089 if(c == REM) {
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001090 /* <S>/ */
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001091 getc0();
"Vladimir N. Oleynik"676e95e2005-09-13 16:50:53 +00001092 if(c == REM) {
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001093 /* <S>"//"[^\n]* */
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001094 do getc0(); while(c != '\n' && c != L_EOF);
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001095 } else if(c == '*') {
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001096 /* <S>[/][*] goto parse block comments */
1097 break;
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001098 }
"Vladimir N. Oleynik"676e95e2005-09-13 16:50:53 +00001099 } else if(c == POUND) {
1100 /* <S># */
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001101 state = c;
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001102 getc1();
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001103 } else if(c == STR || c == CHR) {
"Vladimir N. Oleynik"676e95e2005-09-13 16:50:53 +00001104 /* <S>\"|\' */
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001105 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"5e60dc42005-09-12 12:33:27 +00001123 /* <S>[A-Z_a-z0-9] */
"Vladimir N. Oleynik"b1fe4622005-09-12 16:39:47 +00001124
1125 /* trick for fast drop id
1126 if key with this first char undefined */
"Vladimir N. Oleynik"676e95e2005-09-13 16:50:53 +00001127 if(first_chars[c] == 0) {
"Vladimir N. Oleynik"b1fe4622005-09-12 16:39:47 +00001128 /* skip <S>[A-Z_a-z0-9]+ */
"Vladimir N. Oleynik"af0dd592005-09-16 13:57:33 +00001129 do getc1(); while(isalnums[c]);
"Vladimir N. Oleynik"b1fe4622005-09-12 16:39:47 +00001130 } else {
1131 id_len = 0;
1132 do {
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001133 /* <S>[A-Z_a-z0-9]+ */
1134 put_id(c);
1135 getc1();
"Vladimir N. Oleynik"af0dd592005-09-16 13:57:33 +00001136 } while(isalnums[c]);
"Vladimir N. Oleynik"d128b712005-10-03 10:08:46 +00001137 check_key(key_top, id, id_len);
"Vladimir N. Oleynik"b1fe4622005-09-12 16:39:47 +00001138 }
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001139 }
1140 continue;
1141 }
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001142 /* begin preprocessor states */
"Vladimir N. Oleynik"676e95e2005-09-13 16:50:53 +00001143 if(c == L_EOF)
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001144 yy_error_d("unexpected EOF");
"Vladimir N. Oleynik"676e95e2005-09-13 16:50:53 +00001145 if(c == REM) {
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001146 /* <#.*>/ */
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001147 getc0();
"Vladimir N. Oleynik"676e95e2005-09-13 16:50:53 +00001148 if(c == REM)
"Vladimir N. Oleynik"7573ac62005-09-14 15:09:06 +00001149 yy_error_d("detected // in preprocessor line");
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001150 if(c == '*') {
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001151 /* <#.*>[/][*] goto parse block comments */
1152 break;
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001153 }
1154 /* hmm, #.*[/] */
1155 yy_error_d("strange preprocessor line");
1156 }
"Vladimir N. Oleynik"676e95e2005-09-13 16:50:53 +00001157 if(state == POUND) {
"Vladimir N. Oleynik"7573ac62005-09-14 15:09:06 +00001158 /* tricks */
Rob Landley7bfa88f2006-02-13 19:16:41 +00001159 static const char * const p_preproc[] = {
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001160 /* 0 1 2 3 4 5 6 */
1161 "", "", "", "", "ndef", "efine", "nclude"
"Vladimir N. Oleynik"7573ac62005-09-14 15:09:06 +00001162 };
Rob Landley7bfa88f2006-02-13 19:16:41 +00001163 size_t diu = first_chars_diu[c]; /* strlen and p_preproc ptr */
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001164
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001165 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"63ca3bf2006-02-14 09:23:25 +00001173 /* str begins with c, read == strlen key and compared */
Rob Landley7bfa88f2006-02-13 19:16:41 +00001174 if(diu == id_len && !memcmp(id, p_preproc[diu], diu)) {
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001175 state = diu + '0';
1176 id_len = 0; /* common for save */
1177 }
"Vladimir N. Oleynik"7573ac62005-09-14 15:09:06 +00001178 } else {
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001179 while(isalnums[c]) getc1();
"Vladimir N. Oleynik"676e95e2005-09-13 16:50:53 +00001180 }
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001181 } else if(state == I) {
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001182 if(c == STR) {
1183 /* <I>\" */
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001184 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"5e60dc42005-09-12 12:33:27 +00001196 }
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001197 /* else another (may be wrong) #include ... */
1198 state = S;
Rob Landley7bfa88f2006-02-13 19:16:41 +00001199 } else /* if(state == D || state == U) */ {
"Vladimir N. Oleynik"676e95e2005-09-13 16:50:53 +00001200 /* ignore depend with #define or #undef KEY */
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001201 while(isalnums[c]) getc1();
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001202 state = S;
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001203 }
1204 }
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001205
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 }
1228too_long:
1229 yy_error_d("phrase too long");
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001230}
1231
1232
"Vladimir N. Oleynik"63ca3bf2006-02-14 09:23:25 +00001233/* bb_simplify_path special variant for absolute pathname */
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001234static size_t bb_qa_simplify_path(char *path)
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001235{
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001236 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"5e60dc42005-09-12 12:33:27 +00001265}
1266
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001267static void parse_inc(const char *include, const char *fname)
"Vladimir N. Oleynik"676e95e2005-09-13 16:50:53 +00001268{
1269 bb_key_t *cur;
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001270 const char *p_i;
1271 llist_t *lo;
1272 char *ap;
1273 size_t key_sz;
"Vladimir N. Oleynik"dd1ccdd2006-02-16 15:40:24 +00001274 struct stat st;
"Vladimir N. Oleynik"676e95e2005-09-13 16:50:53 +00001275
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001276 if(*include == '/') {
1277 lo = NULL;
1278 ap = bb_xstrdup(include);
"Vladimir N. Oleynik"676e95e2005-09-13 16:50:53 +00001279 } else {
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001280 int w;
1281 const char *p;
1282
1283 lo = Iop;
"Vladimir N. Oleynik"63ca3bf2006-02-14 09:23:25 +00001284 p = strrchr(fname, '/'); /* fname has absolute pathname */
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001285 w = (p-fname);
1286 /* find from current directory of source file */
1287 ap = bb_asprint("%.*s/%s", w, fname, include);
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001288 }
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001289
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001290 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"5e60dc42005-09-12 12:33:27 +00001296 return;
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001297 }
"Vladimir N. Oleynik"dd1ccdd2006-02-16 15:40:24 +00001298 if(stat(ap, &st) == 0) {
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001299 /* found */
"Vladimir N. Oleynik"dd1ccdd2006-02-16 15:40:24 +00001300 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"b267f132006-02-17 09:05:41 +00001307 return;
"Vladimir N. Oleynik"dd1ccdd2006-02-16 15:40:24 +00001308 }
1309 }
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001310 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"63ca3bf2006-02-14 09:23:25 +00001319 /* lo->data has absolute pathname */
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001320 ap = bb_asprint("%s/%s", lo->data, include);
1321 lo = lo->link;
1322 }
1323
1324 cur = xmalloc(sizeof(bb_key_t));
Rob Landley7bfa88f2006-02-13 19:16:41 +00001325 cur->keyname = ap;
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001326 cur->key_sz = key_sz;
Rob Landley7bfa88f2006-02-13 19:16:41 +00001327 cur->stored_path = ap;
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001328 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"5e60dc42005-09-12 12:33:27 +00001333}
1334
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001335static size_t max_rec_sz;
1336
1337static void parse_conf_opt(const char *opt, const char *val, size_t key_sz)
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001338{
"Vladimir N. Oleynik"676e95e2005-09-13 16:50:53 +00001339 bb_key_t *cur;
"Vladimir N. Oleynik"d128b712005-10-03 10:08:46 +00001340 char *k;
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001341 char *p;
1342 size_t val_sz = 0;
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001343
"Vladimir N. Oleynik"d128b712005-10-03 10:08:46 +00001344 cur = check_key(key_top, opt, key_sz);
"Vladimir N. Oleynik"7573ac62005-09-14 15:09:06 +00001345 if(cur != NULL) {
"Vladimir N. Oleynik"63ca3bf2006-02-14 09:23:25 +00001346 /* already present */
"Vladimir N. Oleynik"d128b712005-10-03 10:08:46 +00001347 cur->checked = NULL; /* store only */
"Vladimir N. Oleynik"7573ac62005-09-14 15:09:06 +00001348 if(cur->value == NULL && val == NULL)
1349 return;
"Vladimir N. Oleynik"d128b712005-10-03 10:08:46 +00001350 if(cur->value != NULL && val != NULL && !strcmp(cur->value, val))
"Vladimir N. Oleynik"7573ac62005-09-14 15:09:06 +00001351 return;
"Vladimir N. Oleynik"d128b712005-10-03 10:08:46 +00001352 k = cur->keyname;
1353 fprintf(stderr, "Warning: redefined %s\n", k);
"Vladimir N. Oleynik"af0dd592005-09-16 13:57:33 +00001354 } else {
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001355 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"5e60dc42005-09-12 12:33:27 +00001370 }
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001371 /* save VAL */
"Vladimir N. Oleynik"7573ac62005-09-14 15:09:06 +00001372 if(val) {
1373 if(*val == '\0') {
1374 cur->value = "";
"Vladimir N. Oleynik"7573ac62005-09-14 15:09:06 +00001375 } else {
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001376 cur->value = p = cur->keyname + key_sz + 1;
1377 memcpy(p, val, val_sz);
"Vladimir N. Oleynik"7573ac62005-09-14 15:09:06 +00001378 }
1379 } else {
1380 cur->value = NULL;
"Vladimir N. Oleynik"7573ac62005-09-14 15:09:06 +00001381 }
"Vladimir N. Oleynik"7573ac62005-09-14 15:09:06 +00001382 /* trick, save first char KEY for do fast identify id */
"Vladimir N. Oleynik"d128b712005-10-03 10:08:46 +00001383 first_chars[(int)*k] = *k;
"Vladimir N. Oleynik"7573ac62005-09-14 15:09:06 +00001384
"Vladimir N. Oleynik"d128b712005-10-03 10:08:46 +00001385 cur->stored_path = k = bb_asprint("%s/%s.h", kp, k);
"Vladimir N. Oleynik"63ca3bf2006-02-14 09:23:25 +00001386 /* key conversion [A-Z_] -> [a-z/] */
"Vladimir N. Oleynik"d128b712005-10-03 10:08:46 +00001387 for(p = k + kp_len + 1; *p; p++) {
"Vladimir N. Oleynik"7573ac62005-09-14 15:09:06 +00001388 if(*p >= 'A' && *p <= 'Z')
1389 *p = *p - 'A' + 'a';
"Vladimir N. Oleynik"af0dd592005-09-16 13:57:33 +00001390 else if(*p == '_' && p[1] > '9') /* do not change A_1 to A/1 */
"Vladimir N. Oleynik"7573ac62005-09-14 15:09:06 +00001391 *p = '/';
1392 }
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001393}
1394
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001395static 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"6732af22006-02-15 12:29:37 +00001417 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"083d3f42005-10-10 11:35:17 +00001421 }
1422 }
1423 /* size_t -> ssize_t :( */
1424 rw_ret = (ssize_t)recordsz;
"Vladimir N. Oleynik"63ca3bf2006-02-14 09:23:25 +00001425 /* check kp/key.h, compare after previous use */
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001426 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"6c0642d2005-10-07 15:36:26 +00001465
1466static int show_dep(int first, bb_key_t *k, const char *name, const char *f)
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001467{
1468 bb_key_t *cur;
1469
1470 for(cur = k; cur; cur = cur->next) {
"Vladimir N. Oleynik"d128b712005-10-03 10:08:46 +00001471 if(cur->checked) {
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001472 if(first >= 0) {
1473 if(first) {
1474 if(f == NULL)
"Vladimir N. Oleynik"6c0642d2005-10-07 15:36:26 +00001475 printf("\n%s:", name);
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001476 else
"Vladimir N. Oleynik"6c0642d2005-10-07 15:36:26 +00001477 printf("\n%s/%s:", pwd, name);
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001478 first = 0;
1479 } else {
1480 printf(" \\\n ");
1481 }
1482 printf(" %s", cur->checked);
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001483 }
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001484 cur->src_have_this_key = cur->checked;
1485 cur->checked = NULL;
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001486 }
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001487 }
1488 return first;
1489}
1490
"Vladimir N. Oleynik"676e95e2005-09-13 16:50:53 +00001491static char *
"Vladimir N. Oleynik"7573ac62005-09-14 15:09:06 +00001492parse_chd(const char *fe, const char *p, size_t dirlen)
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001493{
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001494 struct stat st;
1495 char *fp;
"Vladimir N. Oleynik"676e95e2005-09-13 16:50:53 +00001496 size_t df_sz;
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001497 static char dir_and_entry[4096];
1498 size_t fe_sz = strlen(fe) + 1;
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001499
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001500 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"676e95e2005-09-13 16:50:53 +00001503 fp = dir_and_entry;
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001504 /* 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"676e95e2005-09-13 16:50:53 +00001508
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001509 if(stat(fp, &st)) {
"Vladimir N. Oleynik"d128b712005-10-03 10:08:46 +00001510 fprintf(stderr, "Warning: stat(%s): %m\n", fp);
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001511 return NULL;
1512 }
"Vladimir N. Oleynik"b1fe4622005-09-12 16:39:47 +00001513 if(S_ISREG(st.st_mode)) {
"Vladimir N. Oleynik"7573ac62005-09-14 15:09:06 +00001514 llist_t *cfl;
1515 char *e = fp + df_sz - 3;
1516
1517 if(*e++ != '.' || (*e != 'c' && *e != 'h')) {
"Vladimir N. Oleynik"b1fe4622005-09-12 16:39:47 +00001518 /* direntry is regular file, but is not *.[ch] */
"Vladimir N. Oleynik"676e95e2005-09-13 16:50:53 +00001519 return NULL;
1520 }
"Vladimir N. Oleynik"7573ac62005-09-14 15:09:06 +00001521 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"dd1ccdd2006-02-16 15:40:24 +00001525 /* skip already parsed bb_configs.h */
"Vladimir N. Oleynik"b1fe4622005-09-12 16:39:47 +00001526 return NULL;
1527 }
"Vladimir N. Oleynik"b1fe4622005-09-12 16:39:47 +00001528 }
"Vladimir N. Oleynik"7573ac62005-09-14 15:09:06 +00001529 /* direntry is *.[ch] regular file and is not configs */
Rob Landley7bfa88f2006-02-13 19:16:41 +00001530 c_lex_src(fp, st.st_size);
"Vladimir N. Oleynik"7573ac62005-09-14 15:09:06 +00001531 if(!dontgenerate_dep) {
1532 int first;
"Vladimir N. Oleynik"7573ac62005-09-14 15:09:06 +00001533 if(*e == 'c') {
1534 /* *.c -> *.o */
"Vladimir N. Oleynik"7573ac62005-09-14 15:09:06 +00001535 *e = 'o';
"Vladimir N. Oleynik"6c0642d2005-10-07 15:36:26 +00001536 /* /src_dir/path/file.o to path/file.o */
1537 fp += replace;
1538 if(*fp == '/')
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001539 fp++;
"Vladimir N. Oleynik"d5f2a182005-10-06 14:47:16 +00001540 } else {
1541 e = NULL;
"Vladimir N. Oleynik"7573ac62005-09-14 15:09:06 +00001542 }
"Vladimir N. Oleynik"6c0642d2005-10-07 15:36:26 +00001543 first = show_dep(1, Ifound, fp, e);
1544 first = show_dep(first, key_top, fp, e);
"Vladimir N. Oleynik"7573ac62005-09-14 15:09:06 +00001545 if(first == 0)
1546 putchar('\n');
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001547 } else {
1548 show_dep(-1, key_top, NULL, NULL);
"Vladimir N. Oleynik"7573ac62005-09-14 15:09:06 +00001549 }
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001550 return NULL;
"Vladimir N. Oleynik"7573ac62005-09-14 15:09:06 +00001551 } 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"083d3f42005-10-10 11:35:17 +00001554 /* direntry is directory. buff is returned */
1555 return bb_xstrdup(fp);
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001556 }
"Vladimir N. Oleynik"7573ac62005-09-14 15:09:06 +00001557 /* hmm, direntry is device! */
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001558 return NULL;
1559}
1560
"Vladimir N. Oleynik"63ca3bf2006-02-14 09:23:25 +00001561/* from libbb but inlined for speed considerations */
"Vladimir N. Oleynik"7573ac62005-09-14 15:09:06 +00001562static 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"d128b712005-10-03 10:08:46 +00001573static void scan_dir_find_ch_files(const char *p)
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001574{
1575 llist_t *dirs;
1576 llist_t *d_add;
1577 llist_t *d;
1578 struct dirent *de;
1579 DIR *dir;
"Vladimir N. Oleynik"676e95e2005-09-13 16:50:53 +00001580 size_t dirlen;
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001581
"Vladimir N. Oleynik"d128b712005-10-03 10:08:46 +00001582 dirs = llist_add_to(NULL, bb_simplify_path(p));
"Vladimir N. Oleynik"6c0642d2005-10-07 15:36:26 +00001583 replace = strlen(dirs->data);
"Vladimir N. Oleynik"63ca3bf2006-02-14 09:23:25 +00001584 /* emulate recursion */
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001585 while(dirs) {
1586 d_add = NULL;
1587 while(dirs) {
1588 dir = opendir(dirs->data);
1589 if (dir == NULL)
"Vladimir N. Oleynik"d128b712005-10-03 10:08:46 +00001590 fprintf(stderr, "Warning: opendir(%s): %m\n", dirs->data);
"Vladimir N. Oleynik"676e95e2005-09-13 16:50:53 +00001591 dirlen = strlen(dirs->data);
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001592 while ((de = readdir(dir)) != NULL) {
"Vladimir N. Oleynik"d128b712005-10-03 10:08:46 +00001593 char *found_dir;
"Vladimir N. Oleynik"676e95e2005-09-13 16:50:53 +00001594
"Vladimir N. Oleynik"d128b712005-10-03 10:08:46 +00001595 if (de->d_name[0] == '.')
1596 continue;
1597 found_dir = parse_chd(de->d_name, dirs->data, dirlen);
"Vladimir N. Oleynik"676e95e2005-09-13 16:50:53 +00001598 if(found_dir)
1599 d_add = llist_add_to(d_add, found_dir);
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001600 }
1601 closedir(dir);
"Vladimir N. Oleynik"d128b712005-10-03 10:08:46 +00001602 free(dirs->data);
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001603 d = dirs;
1604 dirs = dirs->link;
1605 free(d);
1606 }
1607 dirs = d_add;
1608 }
1609}
1610
Bernhard Reutner-Fischerd2933752006-05-26 19:58:58 +00001611static void show_usage(void) ATTRIBUTE ((noreturn));
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001612static 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"676e95e2005-09-13 16:50:53 +00001616
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001617int main(int argc, char **argv)
1618{
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001619 char *s;
1620 int i;
1621 llist_t *fl;
1622
"Vladimir N. Oleynik"676e95e2005-09-13 16:50:53 +00001623 {
Bernhard Reutner-Fischer5ba53c02006-02-14 10:43:40 +00001624 /* for bb_simplify_path, this program has no chdir() */
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001625 /* libbb-like my xgetcwd() */
"Vladimir N. Oleynik"676e95e2005-09-13 16:50:53 +00001626 unsigned path_max = 512;
1627
1628 s = xmalloc (path_max);
"Vladimir N. Oleynik"676e95e2005-09-13 16:50:53 +00001629 while (getcwd (s, path_max) == NULL) {
1630 if(errno != ERANGE)
1631 bb_error_d("getcwd: %m");
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001632 free(s);
1633 s = xmalloc(path_max *= 2);
1634 }
"Vladimir N. Oleynik"676e95e2005-09-13 16:50:53 +00001635 pwd = s;
1636 }
1637
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001638 while ((i = getopt(argc, argv, "I:c:dk:w")) > 0) {
1639 switch(i) {
1640 case 'I':
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001641 s = bb_simplify_path(optarg);
1642 Iop = llist_add_to(Iop, s);
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001643 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"7573ac62005-09-14 15:09:06 +00001649 dontgenerate_dep = 1;
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001650 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"7573ac62005-09-14 15:09:06 +00001663 /* default kp */
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001664 if(kp == NULL)
1665 kp = bb_simplify_path(INCLUDE_CONFIG_PATH);
"Vladimir N. Oleynik"7573ac62005-09-14 15:09:06 +00001666 /* 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"5e60dc42005-09-12 12:33:27 +00001673 if(Iop == NULL)
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001674 Iop = llist_add_to(Iop, bb_simplify_path(LOCAL_INCLUDE_PATH));
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001675 if(configs == NULL) {
1676 s = bb_simplify_path(INCLUDE_CONFIG_KEYS_PATH);
1677 configs = llist_add_to(configs, s);
1678 }
"Vladimir N. Oleynik"676e95e2005-09-13 16:50:53 +00001679 /* for c_lex */
1680 pagesizem1 = getpagesize() - 1;
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001681 for(i = 0; i < UCHAR_MAX; i++) {
"Vladimir N. Oleynik"af0dd592005-09-16 13:57:33 +00001682 if(ISALNUM(i))
1683 isalnums[i] = i;
"Vladimir N. Oleynik"63ca3bf2006-02-14 09:23:25 +00001684 /* set unparsed chars to speed up the parser */
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001685 else if(i != CHR && i != STR && i != POUND && i != REM)
"Vladimir N. Oleynik"676e95e2005-09-13 16:50:53 +00001686 first_chars[i] = ANY;
1687 }
1688 first_chars[i] = '-'; /* L_EOF */
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001689
"Vladimir N. Oleynik"7573ac62005-09-14 15:09:06 +00001690 /* parse configs */
1691 for(fl = configs; fl; fl = fl->link) {
1692 struct stat st;
"Vladimir N. Oleynik"676e95e2005-09-13 16:50:53 +00001693
"Vladimir N. Oleynik"7573ac62005-09-14 15:09:06 +00001694 if(stat(fl->data, &st))
1695 bb_error_d("stat(%s): %m", fl->data);
Rob Landley7bfa88f2006-02-13 19:16:41 +00001696 c_lex_config(fl->data, st.st_size);
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001697 free(fl->data);
"Vladimir N. Oleynik"7573ac62005-09-14 15:09:06 +00001698 /* trick for fast comparing found files with configs */
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001699 fl->data = xmalloc(sizeof(struct stat));
"Vladimir N. Oleynik"7573ac62005-09-14 15:09:06 +00001700 memcpy(fl->data, &st, sizeof(struct stat));
1701 }
1702
1703 /* main loop */
"Vladimir N. Oleynik"676e95e2005-09-13 16:50:53 +00001704 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"083d3f42005-10-10 11:35:17 +00001711 store_keys();
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001712 return 0;
1713}
1714
"Vladimir N. Oleynik"7573ac62005-09-14 15:09:06 +00001715/* partial and simplified libbb routine */
"Vladimir N. Oleynik"b1fe4622005-09-12 16:39:47 +00001716static void bb_error_d(const char *s, ...)
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001717{
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"b1fe4622005-09-12 16:39:47 +00001727static char *bb_asprint(const char *format, ...)
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001728{
1729 va_list p;
1730 int r;
1731 char *out;
1732
Bernhard Reutner-Fischerd2933752006-05-26 19:58:58 +00001733#ifdef __USE_GNU
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001734 va_start(p, format);
1735 r = vasprintf(&out, format, p);
1736 va_end(p);
Bernhard Reutner-Fischerd2933752006-05-26 19:58:58 +00001737#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"5e60dc42005-09-12 12:33:27 +00001743
1744 if (r < 0)
1745 bb_error_d("bb_asprint: %m");
1746 return out;
1747}
1748
"Vladimir N. Oleynik"7573ac62005-09-14 15:09:06 +00001749/* partial libbb routine as is */
Rob Landley7bfa88f2006-02-13 19:16:41 +00001750
"Vladimir N. Oleynik"b1fe4622005-09-12 16:39:47 +00001751static char *bb_simplify_path(const char *path)
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001752{
1753 char *s, *start, *p;
1754
1755 if (path[0] == '/')
"Vladimir N. Oleynik"676e95e2005-09-13 16:50:53 +00001756 start = bb_xstrdup(path);
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001757 else {
Bernhard Reutner-Fischer5ba53c02006-02-14 10:43:40 +00001758 /* is not libbb, but this program has no chdir() */
"Vladimir N. Oleynik"676e95e2005-09-13 16:50:53 +00001759 start = bb_asprint("%s/%s", pwd, path);
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001760 }
1761 p = s = start;
1762
1763 do {
"Vladimir N. Oleynik"b1fe4622005-09-12 16:39:47 +00001764 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"5e60dc42005-09-12 12:33:27 +00001774 }
"Vladimir N. Oleynik"b1fe4622005-09-12 16:39:47 +00001775 continue;
1776 }
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001777 }
"Vladimir N. Oleynik"b1fe4622005-09-12 16:39:47 +00001778 }
1779 *++p = *s;
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001780 } while (*++s);
1781
"Vladimir N. Oleynik"b1fe4622005-09-12 16:39:47 +00001782 if ((p == start) || (*p != '/')) { /* not a trailing slash */
1783 ++p; /* so keep last character */
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001784 }
1785 *p = 0;
1786
1787 return start;
1788}