blob: ca48c6a2891a2c56caf36d21a88e3adb4a4b075f [file] [log] [blame]
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001/*
Rob Landley7bfa88f2006-02-13 19:16:41 +00002 * Another fast dependencies generator for Makefiles, Version 4.0
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00003 *
Rob Landley7bfa88f2006-02-13 19:16:41 +00004 * Copyright (C) 2005,2006 by Vladimir Oleynik <dzo@simtreas.ru>
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00005 *
"Vladimir N. Oleynik"af0dd592005-09-16 13:57:33 +00006 * mmaping file may be originally by Linus Torvalds.
7 *
Rob Landley7bfa88f2006-02-13 19:16:41 +00008 * infix parser/evaluator for #if expression
9 * Copyright (c) 2001 Aaron Lehmann <aaronl@vitelus.com>
10 * Copyright (c) 2001 Manuel Novoa III <mjn3@codepoet.org>
11 * Copyright (c) 2003 Vladimir Oleynik <dzo@simtreas.ru>
12 *
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +000013 * bb_simplify_path()
14 * Copyright (C) 2005 Manuel Novoa III <mjn3@codepoet.org>
15 *
16 * xmalloc() bb_xstrdup() bb_error_d():
17 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
18 *
19 * llist routine
20 * Copyright (C) 2003 Glenn McGrath
21 * Copyright (C) Vladimir Oleynik <dzo@simtreas.ru>
22 *
Rob Landley7bfa88f2006-02-13 19:16:41 +000023 * (c) 2005,2006 Bernhard Fischer:
"Vladimir N. Oleynik"af0dd592005-09-16 13:57:33 +000024 * - commentary typos,
25 * - move "memory exhausted" into msg_enomem,
26 * - more verbose --help output.
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +000027 *
"Vladimir N. Oleynik"7573ac62005-09-14 15:09:06 +000028 * This program does:
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +000029 * 1) find #define KEY VALUE or #undef KEY from include/config.h
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +000030 * 2) recursive find and scan *.[ch] files, but skips scan of include/config/
31 * 3) find #include "*.h" and KEYs using, if not as #define and #undef
32 * 4) generate dependencies to stdout
"Vladimir N. Oleynik"664c6e72005-10-06 14:53:43 +000033 * pwd/file.o: include/config/key*.h found_include_*.h
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +000034 * path/inc.h: include/config/key*.h found_included_include_*.h
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +000035 * 5) save include/config/key*.h if changed after previous usage
"Vladimir N. Oleynik"af0dd592005-09-16 13:57:33 +000036 * This program does not generate dependencies for #include <...>
Rob Landley7bfa88f2006-02-13 19:16:41 +000037 * Config file can have #if #elif #else #ifdef #ifndef #endif lines
"Vladimir N. Oleynik"7573ac62005-09-14 15:09:06 +000038 */
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +000039
40#define LOCAL_INCLUDE_PATH "include"
41#define INCLUDE_CONFIG_PATH LOCAL_INCLUDE_PATH"/config"
42#define INCLUDE_CONFIG_KEYS_PATH LOCAL_INCLUDE_PATH"/config.h"
43
"Vladimir N. Oleynik"7573ac62005-09-14 15:09:06 +000044#define bb_mkdep_full_options \
45"\nOptions:" \
46"\n\t-I local_include_path include paths, default: \"" LOCAL_INCLUDE_PATH "\"" \
47"\n\t-d don't generate depend" \
48"\n\t-w show warning if include files not found" \
49"\n\t-k include/config default: \"" INCLUDE_CONFIG_PATH "\"" \
50"\n\t-c include/config.h configs, default: \"" INCLUDE_CONFIG_KEYS_PATH "\"" \
51"\n\tdirs_to_scan default \".\""
52
53#define bb_mkdep_terse_options "Usage: [-I local_include_paths] [-dw] " \
54 "[-k path_for_stored_keys] [dirs]"
55
56
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +000057#define _GNU_SOURCE
58#include <sys/types.h>
59#include <sys/stat.h>
60#include <sys/mman.h>
61#include <getopt.h>
62#include <dirent.h>
63#include <stdio.h>
64#include <stdlib.h>
65#include <string.h>
66#include <stdarg.h>
67#include <unistd.h>
68#include <errno.h>
69#include <fcntl.h>
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +000070#include <limits.h>
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +000071
"Vladimir N. Oleynik"7573ac62005-09-14 15:09:06 +000072
"Vladimir N. Oleynik"7573ac62005-09-14 15:09:06 +000073/* partial and simplified libbb routine */
"Vladimir N. Oleynik"b1fe4622005-09-12 16:39:47 +000074static void bb_error_d(const char *s, ...) __attribute__ ((noreturn, format (printf, 1, 2)));
75static char * bb_asprint(const char *format, ...) __attribute__ ((format (printf, 1, 2)));
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +000076static char *bb_simplify_path(const char *path);
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +000077
78/* stolen from libbb as is */
79typedef struct llist_s {
80 char *data;
81 struct llist_s *link;
82} llist_t;
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +000083
"Vladimir N. Oleynik"7573ac62005-09-14 15:09:06 +000084static const char msg_enomem[] = "memory exhausted";
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +000085
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +000086/* inline realization for fast works */
87static inline void *xmalloc(size_t size)
88{
89 void *p = malloc(size);
90
91 if(p == NULL)
92 bb_error_d(msg_enomem);
93 return p;
94}
95
96static inline char *bb_xstrdup(const char *s)
97{
98 char *r = strdup(s);
99
100 if(r == NULL)
101 bb_error_d(msg_enomem);
102 return r;
103}
104
105
106static int dontgenerate_dep; /* flag -d usaged */
107static int noiwarning; /* flag -w usaged */
108static llist_t *configs; /* list of -c usaged and them stat() after parsed */
109static llist_t *Iop; /* list of -I include usaged */
110
111static char *pwd; /* current work directory */
Bernhard Reutner-Fischer5ba53c02006-02-14 10:43:40 +0000112static size_t replace; /* replace current work directory with build dir */
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +0000113
"Vladimir N. Oleynik"63ca3bf2006-02-14 09:23:25 +0000114static const char *kp; /* KEY path, argument of -k used */
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +0000115static size_t kp_len;
116static struct stat st_kp; /* stat(kp) */
117
118typedef struct BB_KEYS {
119 char *keyname;
120 size_t key_sz;
121 const char *value;
122 const char *checked;
123 char *stored_path;
124 const char *src_have_this_key;
125 struct BB_KEYS *next;
126} bb_key_t;
127
128static bb_key_t *key_top; /* list of found KEYs */
129static bb_key_t *Ifound; /* list of parsed includes */
130
131
132static void parse_conf_opt(const char *opt, const char *val, size_t key_sz);
133static void parse_inc(const char *include, const char *fname);
134
135static inline bb_key_t *check_key(bb_key_t *k, const char *nk, size_t key_sz)
136{
137 bb_key_t *cur;
138
139 for(cur = k; cur; cur = cur->next) {
140 if(key_sz == cur->key_sz && memcmp(cur->keyname, nk, key_sz) == 0) {
141 cur->checked = cur->stored_path;
142 return cur;
143 }
144 }
145 return NULL;
146}
147
Rob Landley7bfa88f2006-02-13 19:16:41 +0000148static inline const char *lookup_key(const char *nk, size_t key_sz)
149{
150 bb_key_t *cur;
151
152 for(cur = key_top; cur; cur = cur->next) {
153 if(key_sz == cur->key_sz && memcmp(cur->keyname, nk, key_sz) == 0) {
154 return cur->value;
155 }
156 }
157 return NULL;
158}
159
"Vladimir N. Oleynik"7573ac62005-09-14 15:09:06 +0000160/* for lexical analyser */
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +0000161static int pagesizem1; /* padding mask = getpagesize() - 1 */
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +0000162
"Vladimir N. Oleynik"7573ac62005-09-14 15:09:06 +0000163/* for speed tricks */
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +0000164static char first_chars[1+UCHAR_MAX]; /* + L_EOF */
165static char isalnums[1+UCHAR_MAX]; /* + L_EOF */
Rob Landley7bfa88f2006-02-13 19:16:41 +0000166
167/* trick for fast find "define", "include", "undef",
168"if((n)def)" "else", "endif" */
169static const char * const preproc[] = {
170/* 0 1 2 3 4 5 6 7 8 9 */
171"", "efine", "lif", "lse", "ndif", "f", "fdef", "fndef", "nclude", "ndef" };
172static const unsigned char first_chars_deiu[UCHAR_MAX] = {
173 [(int)'d'] = (unsigned char)(1|0x10), /* define */
174 [(int)'e'] = (unsigned char)(2|0x40), /* elif, else, endif */
175 [(int)'i'] = (unsigned char)(5|0x80), /* if ifdef ifndef include */
176 [(int)'u'] = (unsigned char)(9|0x90), /* undef */
"Vladimir N. Oleynik"6c0642d2005-10-07 15:36:26 +0000177};
"Vladimir N. Oleynik"b1fe4622005-09-12 16:39:47 +0000178
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +0000179#define CONFIG_MODE 0
Rob Landley7bfa88f2006-02-13 19:16:41 +0000180#define IF0_MODE 1 /* #if 0 */
181#define IF1_MODE 2 /* #if 1 */
182#define ELSE0_MODE 4 /* #else found after #if 0 */
183#define ELSE1_MODE 8 /* #else found after #if 1 */
184#define ELIF1_MODE 16 /* #elif found after #if 1 */
185#define FALSE_MODES (IF0_MODE|ELSE1_MODE|ELIF1_MODE)
"Vladimir N. Oleynik"7573ac62005-09-14 15:09:06 +0000186
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +0000187#define yy_error_d(s) bb_error_d("%s:%d hmm, %s", fname, line, s)
188
189/* state */
190#define S 0 /* start state */
191#define STR '"' /* string */
192#define CHR '\'' /* char */
"Vladimir N. Oleynik"676e95e2005-09-13 16:50:53 +0000193#define REM '/' /* block comment */
194#define BS '\\' /* back slash */
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +0000195#define POUND '#' /* # */
Rob Landley7bfa88f2006-02-13 19:16:41 +0000196#define D '1' /* #define preprocessor's directive */
197#define EI '2' /* #elif preprocessor's directive */
198#define E '3' /* #else preprocessor's directive */
199#define EF '4' /* #endif preprocessor's directive */
200#define F '5' /* #if preprocessor's directive */
201#define IFD '6' /* #ifdef preprocessor's directive */
202#define IFND '7' /* #ifndef preprocessor's directive */
203#define I '8' /* #include preprocessor's directive */
204#define U '9' /* #undef preprocessor's directive */
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +0000205#define DK 'K' /* #define KEY... (config mode) */
"Vladimir N. Oleynik"676e95e2005-09-13 16:50:53 +0000206#define ANY '*' /* any unparsed chars */
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +0000207
208/* [A-Z_a-z] */
209#define ID(c) ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c == '_')
210/* [A-Z_a-z0-9] */
211#define ISALNUM(c) (ID(c) || (c >= '0' && c <= '9'))
212
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +0000213#define L_EOF (1+UCHAR_MAX)
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +0000214
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +0000215#define getc0() do { c = (optr >= oend) ? L_EOF : *optr++; } while(0)
216
217#define getc1() do { getc0(); if(c == BS) { getc0(); \
218 if(c == '\n') { line++; continue; } \
219 else { optr--; c = BS; } \
220 } break; } while(1)
221
222static char id_s[4096];
223#define put_id(ic) do { if(id_len == sizeof(id_s)) goto too_long; \
224 id[id_len++] = ic; } while(0)
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +0000225
Rob Landley7bfa88f2006-02-13 19:16:41 +0000226static char ifcpp_stack[1024];
227static int ptr_ifcpp_stack;
228#define push_mode() do { \
229 if(ptr_ifcpp_stack == (int)sizeof(ifcpp_stack)) \
230 yy_error_d("#if* stack overflow"); \
231 ifcpp_stack[ptr_ifcpp_stack++] = (char)mode; \
232 } while(0)
"Vladimir N. Oleynik"7573ac62005-09-14 15:09:06 +0000233
Rob Landley7bfa88f2006-02-13 19:16:41 +0000234#define pop_mode() do { \
235 if(ptr_ifcpp_stack == 0) \
236 yy_error_d("unexpected #endif"); \
237 mode = ifcpp_stack[--ptr_ifcpp_stack]; \
238 } while(0)
239
240/* #if expression */
241typedef long long arith_t;
242
243static arith_t arith (const char *expr, int *perrcode);
244
245/* The code uses a simple two-stack algorithm. See
246 * http://www.onthenet.com.au/~grahamis/int2008/week02/lect02.html
247 * for a detailed explanation of the infix-to-postfix algorithm on which
248 * this is based (this code differs in that it applies operators immediately
249 * to the stack instead of adding them to a queue to end up with an
250 * expression). */
251
252#define arith_isspace(arithval) \
253 (arithval == ' ' || arithval == '\v' || arithval == '\t' || arithval == '\f')
254
255
256typedef unsigned char operator;
257
258/* An operator's token id is a bit of a bitfield. The lower 5 bits are the
259 * precedence, and 3 high bits are an ID unique across operators of that
260 * precedence. The ID portion is so that multiple operators can have the
261 * same precedence, ensuring that the leftmost one is evaluated first.
262 * Consider * and /. */
263
264#define tok_decl(prec,id) (((id)<<5)|(prec))
265#define PREC(op) ((op) & 0x1F)
266
267#define TOK_LPAREN tok_decl(0,0)
268
269#define TOK_COMMA tok_decl(1,0)
270
271/* conditional is right associativity too */
272#define TOK_CONDITIONAL tok_decl(2,0)
273#define TOK_CONDITIONAL_SEP tok_decl(2,1)
274
275#define TOK_OR tok_decl(3,0)
276
277#define TOK_AND tok_decl(4,0)
278
279#define TOK_BOR tok_decl(5,0)
280
281#define TOK_BXOR tok_decl(6,0)
282
283#define TOK_BAND tok_decl(7,0)
284
285#define TOK_EQ tok_decl(8,0)
286#define TOK_NE tok_decl(8,1)
287
288#define TOK_LT tok_decl(9,0)
289#define TOK_GT tok_decl(9,1)
290#define TOK_GE tok_decl(9,2)
291#define TOK_LE tok_decl(9,3)
292
293#define TOK_LSHIFT tok_decl(10,0)
294#define TOK_RSHIFT tok_decl(10,1)
295
296#define TOK_ADD tok_decl(11,0)
297#define TOK_SUB tok_decl(11,1)
298
299#define TOK_MUL tok_decl(12,0)
300#define TOK_DIV tok_decl(12,1)
301#define TOK_REM tok_decl(12,2)
302
303/* For now unary operators. */
304#define UNARYPREC 13
305#define TOK_BNOT tok_decl(UNARYPREC,0)
306#define TOK_NOT tok_decl(UNARYPREC,1)
307
308#define TOK_UMINUS tok_decl(UNARYPREC+1,0)
309#define TOK_UPLUS tok_decl(UNARYPREC+1,1)
310
311#define SPEC_PREC (UNARYPREC+1)
312
313#define TOK_NUM tok_decl(SPEC_PREC, 0)
314#define TOK_RPAREN tok_decl(SPEC_PREC, 1)
315
316#define NUMPTR (*numstackptr)
317
318typedef struct ARITCH_VAR_NUM {
319 arith_t val;
320 arith_t contidional_second_val;
321 char contidional_second_val_initialized;
322} v_n_t;
323
324
325typedef struct CHK_VAR_RECURSIVE_LOOPED {
326 const char *var;
327 size_t var_sz;
328 struct CHK_VAR_RECURSIVE_LOOPED *next;
329} chk_var_recursive_looped_t;
330
331static chk_var_recursive_looped_t *prev_chk_var_recursive;
332
333
334static int arith_lookup_val(const char *var, size_t key_sz, arith_t *pval)
335{
336 const char * p = lookup_key(var, key_sz);
337
338 if(p) {
339 int errcode;
340
341 /* recursive try as expression */
342 chk_var_recursive_looped_t *cur;
343 chk_var_recursive_looped_t cur_save;
344
345 for(cur = prev_chk_var_recursive; cur; cur = cur->next) {
346 if(cur->var_sz == key_sz && memcmp(cur->var, var, key_sz) == 0) {
347 /* expression recursion loop detected */
348 return -5;
349 }
350 }
351 /* save current lookuped var name */
352 cur = prev_chk_var_recursive;
353 cur_save.var = var;
354 cur_save.var_sz = key_sz;
355 cur_save.next = cur;
356 prev_chk_var_recursive = &cur_save;
357
358 *pval = arith (p, &errcode);
359 /* restore previous ptr after recursiving */
360 prev_chk_var_recursive = cur;
361 return errcode;
362 } else {
363 /* disallow undefined var */
364 fprintf(stderr, "%.*s ", (int)key_sz, var);
365 return -4;
366 }
367}
368
369/* "applying" a token means performing it on the top elements on the integer
370 * stack. For a unary operator it will only change the top element, but a
371 * binary operator will pop two arguments and push a result */
372static inline int
373arith_apply(operator op, v_n_t *numstack, v_n_t **numstackptr)
374{
375 v_n_t *numptr_m1;
376 arith_t numptr_val, rez;
377
378 if (NUMPTR == numstack) goto err; /* There is no operator that can work
379 without arguments */
380 numptr_m1 = NUMPTR - 1;
381
382 rez = numptr_m1->val;
383 if (op == TOK_UMINUS)
384 rez *= -1;
385 else if (op == TOK_NOT)
386 rez = !rez;
387 else if (op == TOK_BNOT)
388 rez = ~rez;
389 else if (op != TOK_UPLUS) {
390 /* Binary operators */
391
392 /* check and binary operators need two arguments */
393 if (numptr_m1 == numstack) goto err;
394
395 /* ... and they pop one */
396 --NUMPTR;
397 numptr_val = rez;
398 if (op == TOK_CONDITIONAL) {
399 if(! numptr_m1->contidional_second_val_initialized) {
400 /* protect $((expr1 ? expr2)) without ": expr" */
401 goto err;
402 }
403 rez = numptr_m1->contidional_second_val;
404 } else if(numptr_m1->contidional_second_val_initialized) {
405 /* protect $((expr1 : expr2)) without "expr ? " */
406 goto err;
407 }
408 numptr_m1 = NUMPTR - 1;
409 if (op == TOK_CONDITIONAL) {
410 numptr_m1->contidional_second_val = rez;
411 }
412 rez = numptr_m1->val;
413 if (op == TOK_BOR)
414 rez |= numptr_val;
415 else if (op == TOK_OR)
416 rez = numptr_val || rez;
417 else if (op == TOK_BAND)
418 rez &= numptr_val;
419 else if (op == TOK_BXOR)
420 rez ^= numptr_val;
421 else if (op == TOK_AND)
422 rez = rez && numptr_val;
423 else if (op == TOK_EQ)
424 rez = (rez == numptr_val);
425 else if (op == TOK_NE)
426 rez = (rez != numptr_val);
427 else if (op == TOK_GE)
428 rez = (rez >= numptr_val);
429 else if (op == TOK_RSHIFT)
430 rez >>= numptr_val;
431 else if (op == TOK_LSHIFT)
432 rez <<= numptr_val;
433 else if (op == TOK_GT)
434 rez = (rez > numptr_val);
435 else if (op == TOK_LT)
436 rez = (rez < numptr_val);
437 else if (op == TOK_LE)
438 rez = (rez <= numptr_val);
439 else if (op == TOK_MUL)
440 rez *= numptr_val;
441 else if (op == TOK_ADD)
442 rez += numptr_val;
443 else if (op == TOK_SUB)
444 rez -= numptr_val;
445 else if (op == TOK_COMMA)
446 rez = numptr_val;
447 else if (op == TOK_CONDITIONAL_SEP) {
448 if (numptr_m1 == numstack) {
449 /* protect $((expr : expr)) without "expr ? " */
450 goto err;
451 }
452 numptr_m1->contidional_second_val_initialized = op;
453 numptr_m1->contidional_second_val = numptr_val;
454 }
455 else if (op == TOK_CONDITIONAL) {
456 rez = rez ?
457 numptr_val : numptr_m1->contidional_second_val;
458 }
459 else if(numptr_val==0) /* zero divisor check */
460 return -2;
461 else if (op == TOK_DIV)
462 rez /= numptr_val;
463 else if (op == TOK_REM)
464 rez %= numptr_val;
465 }
466 numptr_m1->val = rez;
467 return 0;
468err: return(-1);
469}
470
471/* longest must first */
472static const char op_tokens[] = {
473 '<','<', 0, TOK_LSHIFT,
474 '>','>', 0, TOK_RSHIFT,
475 '|','|', 0, TOK_OR,
476 '&','&', 0, TOK_AND,
477 '!','=', 0, TOK_NE,
478 '<','=', 0, TOK_LE,
479 '>','=', 0, TOK_GE,
480 '=','=', 0, TOK_EQ,
481 '!', 0, TOK_NOT,
482 '<', 0, TOK_LT,
483 '>', 0, TOK_GT,
484 '|', 0, TOK_BOR,
485 '&', 0, TOK_BAND,
486 '*', 0, TOK_MUL,
487 '/', 0, TOK_DIV,
488 '%', 0, TOK_REM,
489 '+', 0, TOK_ADD,
490 '-', 0, TOK_SUB,
491 '^', 0, TOK_BXOR,
492 '~', 0, TOK_BNOT,
493 ',', 0, TOK_COMMA,
494 '?', 0, TOK_CONDITIONAL,
495 ':', 0, TOK_CONDITIONAL_SEP,
496 ')', 0, TOK_RPAREN,
497 '(', 0, TOK_LPAREN,
498 0
499};
500/* ptr to ")" */
501#define endexpression &op_tokens[sizeof(op_tokens)-7]
502
503/*
504 * Return of a legal variable name (a letter or underscore followed by zero or
505 * more letters, underscores, and digits).
506 */
507
508static inline char *
509endofname(const char *name)
510{
511 char *p;
512
513 p = (char *) name;
514 if (! ID(*p))
515 return p;
516 while (*++p) {
517 if (! ISALNUM(*p))
518 break;
519 }
520 return p;
521}
522
523
524/* Like strncpy but make sure the resulting string is always 0 terminated. */
525static inline char * safe_strncpy(char *dst, const char *src, size_t size)
526{
527 dst[size-1] = '\0';
528 return strncpy(dst, src, size-1);
529}
530
531static arith_t arith (const char *expr, int *perrcode)
532{
533 char arithval; /* Current character under analysis */
534 operator lasttok, op;
535 operator prec;
536
537 const char *p = endexpression;
538 int errcode;
539
540 size_t datasizes = strlen(expr) + 2;
541
542 /* Stack of integers */
543 /* The proof that there can be no more than strlen(startbuf)/2+1 integers
544 * in any given correct or incorrect expression is left as an exercise to
545 * the reader. */
546 v_n_t *numstack = alloca(((datasizes)/2)*sizeof(v_n_t)),
547 *numstackptr = numstack;
548 /* Stack of operator tokens */
549 operator *stack = alloca((datasizes) * sizeof(operator)),
550 *stackptr = stack;
551
552 *stackptr++ = lasttok = TOK_LPAREN; /* start off with a left paren */
553 *perrcode = errcode = 0;
554
555 while(1) {
556 if ((arithval = *expr) == 0) {
557 if (p == endexpression) {
558 /* Null expression. */
559err:
560 return (*perrcode = -1);
561 }
562
563 /* This is only reached after all tokens have been extracted from the
564 * input stream. If there are still tokens on the operator stack, they
565 * are to be applied in order. At the end, there should be a final
566 * result on the integer stack */
567
568 if (expr != endexpression + 1) {
569 /* If we haven't done so already, */
570 /* append a closing right paren */
571 expr = endexpression;
572 /* and let the loop process it. */
573 continue;
574 }
575 /* At this point, we're done with the expression. */
576 if (numstackptr != numstack+1) {
577 /* ... but if there isn't, it's bad */
578 goto err;
579 }
580 ret:
581 *perrcode = errcode;
582 return numstack->val;
583 } else {
584 /* Continue processing the expression. */
585 if (arith_isspace(arithval)) {
586 /* Skip whitespace */
587 goto prologue;
588 }
589 if((p = endofname(expr)) != expr) {
590 size_t var_name_size = (p-expr);
591
592 if(var_name_size == 7 &&
593 strncmp(expr, "defined", var_name_size) == 0) {
594 int brace_form = 0;
595 const char *v;
596
597 while(arith_isspace(*p)) p++;
598 if(*p == '(') {
599 p++;
600 while(arith_isspace(*p)) p++;
601 brace_form = 1;
602 }
603 expr = p;
604 if((p = endofname(expr)) == expr)
605 goto err;
606 var_name_size = (p-expr);
607 while(arith_isspace(*p)) p++;
608 if(brace_form && *p++ != ')')
609 goto err;
610 v = lookup_key(expr, var_name_size);
611 numstackptr->val = (v != NULL) ? 1 : 0;
612 } else {
613 errcode = arith_lookup_val(expr, var_name_size,
614 &(numstackptr->val));
615 if(errcode) goto ret;
616 }
617 expr = p;
618 num:
619 numstackptr->contidional_second_val_initialized = 0;
620 numstackptr++;
621 lasttok = TOK_NUM;
622 continue;
623 } else if (arithval >= '0' && arithval <= '9') {
624 numstackptr->val = strtoll(expr, (char **) &expr, 0);
625 while(*expr == 'l' || *expr == 'L' || *expr == 'u' ||
626 *expr == 'U')
627 expr++;
628 goto num;
629 }
630 for(p = op_tokens; ; p++) {
631 const char *o;
632
633 if(*p == 0) {
634 /* strange operator not found */
635 goto err;
636 }
637 for(o = expr; *p && *o == *p; p++)
638 o++;
639 if(! *p) {
640 /* found */
641 expr = o - 1;
642 break;
643 }
644 /* skip tail uncompared token */
645 while(*p)
646 p++;
647 /* skip zero delim */
648 p++;
649 }
650 op = p[1];
651
652 /* Plus and minus are binary (not unary) _only_ if the last
653 * token was as number, or a right paren (which pretends to be
654 * a number, since it evaluates to one). Think about it.
655 * It makes sense. */
656 if (lasttok != TOK_NUM) {
657 if(op == TOK_ADD)
658 op = TOK_UPLUS;
659 else if(op == TOK_SUB)
660 op = TOK_UMINUS;
661 }
662 /* We don't want a unary operator to cause recursive descent on the
663 * stack, because there can be many in a row and it could cause an
664 * operator to be evaluated before its argument is pushed onto the
665 * integer stack. */
666 /* But for binary operators, "apply" everything on the operator
667 * stack until we find an operator with a lesser priority than the
668 * one we have just extracted. */
669 /* Left paren is given the lowest priority so it will never be
670 * "applied" in this way.
671 * if associativity is right and priority eq, applied also skip
672 */
673 prec = PREC(op);
674 if ((prec > 0 && prec < UNARYPREC) || prec == SPEC_PREC) {
675 /* not left paren or unary */
676 if (lasttok != TOK_NUM) {
677 /* binary op must be preceded by a num */
678 goto err;
679 }
680 while (stackptr != stack) {
681 if (op == TOK_RPAREN) {
682 /* The algorithm employed here is simple: while we don't
683 * hit an open paren nor the bottom of the stack, pop
684 * tokens and apply them */
685 if (stackptr[-1] == TOK_LPAREN) {
686 --stackptr;
687 /* Any operator directly after a */
688 lasttok = TOK_NUM;
689 /* close paren should consider itself binary */
690 goto prologue;
691 }
692 } else {
693 operator prev_prec = PREC(stackptr[-1]);
694
695 if (prev_prec < prec)
696 break;
697 /* check right assoc */
698 if(prev_prec == prec && prec == PREC(TOK_CONDITIONAL))
699 break;
700 }
701 errcode = arith_apply(*--stackptr, numstack, &numstackptr);
702 if(errcode) goto ret;
703 }
704 if (op == TOK_RPAREN) {
705 goto err;
706 }
707 }
708
709 /* Push this operator to the stack and remember it. */
710 *stackptr++ = lasttok = op;
711
712 prologue:
713 ++expr;
714 }
715 }
716}
717
718/* stupid C lexical analyser for configs.h */
719static void c_lex_config(const char *fname, long fsize)
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +0000720{
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +0000721 int c;
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +0000722 int state;
723 int line;
"Vladimir N. Oleynik"676e95e2005-09-13 16:50:53 +0000724 char *id = id_s;
"Vladimir N. Oleynik"63ca3bf2006-02-14 09:23:25 +0000725 size_t id_len = 0; /* stupid initialization */
Rob Landley7bfa88f2006-02-13 19:16:41 +0000726 unsigned char *optr, *oend;
727 int mode = CONFIG_MODE;
728
729 int fd;
730 char *map;
731 int mapsize;
732
733 if(fsize == 0) {
734 fprintf(stderr, "Warning: %s is empty\n", fname);
735 return;
736 }
737 fd = open(fname, O_RDONLY);
738 if(fd < 0) {
739 perror(fname);
740 return;
741 }
742 mapsize = (fsize+pagesizem1) & ~pagesizem1;
743 map = mmap(NULL, mapsize, PROT_READ, MAP_PRIVATE, fd, 0);
744 if ((long) map == -1)
745 bb_error_d("%s: mmap: %m", fname);
746
747 optr = (unsigned char *)map;
748 oend = optr + fsize;
749
750 line = 1;
751 state = S;
752
753 for(;;) {
754 getc1();
755 for(;;) {
756 /* [ \t]+ eat first space */
757 while(c == ' ' || c == '\t')
758 getc1();
759
760 if(state == S) {
761 while(first_chars[c] == ANY) {
762 /* <S>unparsed */
763 if(c == '\n')
764 line++;
765 getc1();
766 }
767 if(c == L_EOF) {
768 /* <S><<EOF>> */
769 munmap(map, mapsize);
770 close(fd);
771 if(mode != CONFIG_MODE)
772 yy_error_d("expected #endif");
773 return;
774 }
775 if(c == REM) {
776 /* <S>/ */
777 getc0();
778 if(c == REM) {
779 /* <S>"//"[^\n]* */
780 do getc0(); while(c != '\n' && c != L_EOF);
781 } else if(c == '*') {
782 /* <S>[/][*] goto parse block comments */
783 break;
784 }
785 } else if(c == POUND) {
786 /* <S># */
787 state = c;
788 getc1();
789 } else if(c == STR || c == CHR) {
790 /* <S>\"|\' */
791 int qc = c;
792
793 for(;;) {
794 /* <STR,CHR>. */
795 getc1();
796 if(c == qc) {
797 /* <STR>\" or <CHR>\' */
798 break;
799 }
800 if(c == BS) {
801 /* <STR,CHR>\\ but is not <STR,CHR>\\\n */
802 getc0();
803 }
804 if(c == '\n' || c == L_EOF)
805 yy_error_d("unterminated");
806 }
807 getc1();
808 } else {
809 /* <S>[A-Z_a-z0-9] */
810
811 /* trick for fast drop id
812 if key with this first char undefined */
813 if(first_chars[c] == 0 || (mode & FALSE_MODES) != 0) {
814 /* skip <S>[A-Z_a-z0-9]+ */
815 do getc1(); while(isalnums[c]);
816 } else {
817 id_len = 0;
818 do {
819 /* <S>[A-Z_a-z0-9]+ */
820 put_id(c);
821 getc1();
822 } while(isalnums[c]);
823 check_key(key_top, id, id_len);
824 }
825 }
826 continue;
827 }
828 /* begin preprocessor states */
829 if(c == L_EOF)
830 yy_error_d("unexpected EOF");
831 if(c == REM) {
832 /* <#.*>/ */
833 getc0();
834 if(c == REM)
835 yy_error_d("detected // in preprocessor line");
836 if(c == '*') {
837 /* <#.*>[/][*] goto parse block comments */
838 break;
839 }
840 /* hmm, #.*[/] */
841 yy_error_d("strange preprocessor line");
842 }
843 if(state == POUND) {
844 /* tricks */
845 int diu = (int)first_chars_deiu[c]; /* preproc ptr */
846
847 state = S;
848 if(diu != S) {
849 int p_num_str, p_num_max;
850
851 getc1();
852 id_len = 0;
853 while(isalnums[c]) {
854 put_id(c);
855 getc1();
856 }
857 put_id(0);
858 p_num_str = diu & 0xf;
859 p_num_max = diu >> 4;
860 for(diu = p_num_str; diu <= p_num_max; diu++)
861 if(!strcmp(id, preproc[diu])) {
862 state = (diu + '0');
863 /* common */
864 id_len = 0;
865 break;
866 }
867 } else {
868 while(isalnums[c]) getc1();
869 }
870 } else if(state == EF) {
871 /* #endif */
872 pop_mode();
873 state = S;
874 } else if(state == I) {
875 if(c == STR && (mode & FALSE_MODES) == 0) {
876 /* <I>\" */
877 for(;;) {
878 getc1();
879 if(c == STR)
880 break;
881 if(c == L_EOF)
882 yy_error_d("unexpected EOF");
883 put_id(c);
884 }
885 put_id(0);
886 /* store "include.h" */
887 parse_inc(id, fname);
888 getc1();
889 }
890 /* else another (may be wrong) #include ... */
891 state = S;
892 } else if(state == F) {
893 arith_t t;
894 int errcode;
895
896 while(c != '\n' && c != L_EOF) {
897 put_id(c);
898 getc1();
899 }
900 put_id(0);
901 t = arith(id, &errcode);
902 if (errcode < 0) {
903 if (errcode == -2)
904 yy_error_d("divide by zero");
905 else if (errcode == -4)
906 yy_error_d("undefined");
907 else if (errcode == -5)
908 yy_error_d("expression recursion loop detected");
909 else
910 yy_error_d("syntax error");
911 }
912 push_mode();
913 mode = t != 0 ? IF1_MODE : IF0_MODE;
914 state = S;
915 } else if(state == IFD || state == IFND) {
916 /* save KEY from #if(n)def KEY ... */
917 const char *v;
918
919 push_mode();
920 while(isalnums[c]) {
921 put_id(c);
922 getc1();
923 }
924 if(!id_len)
925 yy_error_d("expected identifier");
926 v = lookup_key(id, id_len);
927 mode = IF1_MODE;
928 if(state == IFD && v == NULL)
929 mode = IF0_MODE;
930 else if(state == IFND && v != NULL)
931 mode = IF0_MODE;
932 state = S;
933 } else if(state == EI) {
934 /* #elif */
935 if(mode == CONFIG_MODE || mode == ELSE0_MODE || mode == ELSE1_MODE)
936 yy_error_d("unexpected #elif");
937 if(mode == IF0_MODE) {
938 pop_mode();
939 state = F;
940 } else {
941 mode = ELIF1_MODE;
942 state = S;
943 }
944 } else if(state == E) {
945 if(mode == CONFIG_MODE || mode == ELSE0_MODE || mode == ELSE1_MODE)
946 yy_error_d("unexpected #else");
947 if(mode == IF0_MODE)
948 mode = ELSE0_MODE;
949 else if(mode == IF1_MODE)
950 mode = ELSE1_MODE;
951 state = S;
952 } else if(state == D || state == U) {
953 /* save KEY from #"define"|"undef" ... */
954 while(isalnums[c]) {
955 put_id(c);
956 getc1();
957 }
958 if(!id_len)
959 yy_error_d("expected identifier");
960 if(state == U) {
961 if((mode & FALSE_MODES) == 0)
962 parse_conf_opt(id, NULL, id_len);
963 state = S;
964 } else {
965 /* D -> DK */
966 state = DK;
967 }
968 } else {
969 /* state==<DK> #define KEY[ ] */
970 size_t opt_len = id_len;
971 char *val = id + opt_len;
972 char *sp;
973
974 for(;;) {
975 if(c == L_EOF || c == '\n')
976 break;
977 put_id(c);
978 getc1();
979 }
980 sp = id + id_len;
981 put_id(0);
982 /* trim tail spaces */
983 while(--sp >= val && (*sp == ' ' || *sp == '\t'
984 || *sp == '\f' || *sp == '\v'))
985 *sp = '\0';
986 if((mode & FALSE_MODES) == 0)
987 parse_conf_opt(id, val, opt_len);
988 state = S;
989 }
990 }
991
992 /* <REM> */
993 getc0();
994 for(;;) {
995 /* <REM>[^*]+ */
996 while(c != '*') {
997 if(c == '\n') {
998 /* <REM>\n */
999 if(state != S)
1000 yy_error_d("unexpected newline");
1001 line++;
1002 } else if(c == L_EOF)
1003 yy_error_d("unexpected EOF");
1004 getc0();
1005 }
1006 /* <REM>[*] */
1007 getc0();
1008 if(c == REM) {
1009 /* <REM>[*][/] */
1010 break;
1011 }
1012 }
1013 }
1014too_long:
1015 yy_error_d("phrase too long");
1016}
1017
1018/* trick for fast find "define", "include", "undef" */
1019static const char first_chars_diu[UCHAR_MAX] = {
1020 [(int)'d'] = (char)5, /* strlen("define") - 1; */
1021 [(int)'i'] = (char)6, /* strlen("include") - 1; */
1022 [(int)'u'] = (char)4, /* strlen("undef") - 1; */
1023};
1024
1025#undef D
1026#undef I
1027#undef U
1028#define D '5' /* #define preprocessor's directive */
1029#define I '6' /* #include preprocessor's directive */
1030#define U '4' /* #undef preprocessor's directive */
1031
1032/* stupid C lexical analyser for sources */
1033static void c_lex_src(const char *fname, long fsize)
1034{
1035 int c;
1036 int state;
1037 int line;
1038 char *id = id_s;
"Vladimir N. Oleynik"63ca3bf2006-02-14 09:23:25 +00001039 size_t id_len = 0; /* stupid initialization */
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001040 unsigned char *optr, *oend;
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001041
1042 int fd;
1043 char *map;
1044 int mapsize;
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001045
"Vladimir N. Oleynik"7573ac62005-09-14 15:09:06 +00001046 if(fsize == 0) {
1047 fprintf(stderr, "Warning: %s is empty\n", fname);
1048 return;
1049 }
"Vladimir N. Oleynik"676e95e2005-09-13 16:50:53 +00001050 fd = open(fname, O_RDONLY);
1051 if(fd < 0) {
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001052 perror(fname);
1053 return;
"Vladimir N. Oleynik"676e95e2005-09-13 16:50:53 +00001054 }
1055 mapsize = (fsize+pagesizem1) & ~pagesizem1;
1056 map = mmap(NULL, mapsize, PROT_READ, MAP_PRIVATE, fd, 0);
1057 if ((long) map == -1)
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001058 bb_error_d("%s: mmap: %m", fname);
1059
"Vladimir N. Oleynik"676e95e2005-09-13 16:50:53 +00001060 optr = (unsigned char *)map;
1061 oend = optr + fsize;
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001062
1063 line = 1;
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001064 state = S;
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001065
1066 for(;;) {
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001067 getc1();
1068 for(;;) {
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001069 /* [ \t]+ eat first space */
1070 while(c == ' ' || c == '\t')
1071 getc1();
1072
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001073 if(state == S) {
"Vladimir N. Oleynik"676e95e2005-09-13 16:50:53 +00001074 while(first_chars[c] == ANY) {
1075 /* <S>unparsed */
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001076 if(c == '\n')
1077 line++;
1078 getc1();
1079 }
"Vladimir N. Oleynik"676e95e2005-09-13 16:50:53 +00001080 if(c == L_EOF) {
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001081 /* <S><<EOF>> */
1082 munmap(map, mapsize);
1083 close(fd);
1084 return;
1085 }
"Vladimir N. Oleynik"676e95e2005-09-13 16:50:53 +00001086 if(c == REM) {
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001087 /* <S>/ */
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001088 getc0();
"Vladimir N. Oleynik"676e95e2005-09-13 16:50:53 +00001089 if(c == REM) {
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001090 /* <S>"//"[^\n]* */
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001091 do getc0(); while(c != '\n' && c != L_EOF);
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001092 } else if(c == '*') {
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001093 /* <S>[/][*] goto parse block comments */
1094 break;
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001095 }
"Vladimir N. Oleynik"676e95e2005-09-13 16:50:53 +00001096 } else if(c == POUND) {
1097 /* <S># */
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001098 state = c;
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001099 getc1();
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001100 } else if(c == STR || c == CHR) {
"Vladimir N. Oleynik"676e95e2005-09-13 16:50:53 +00001101 /* <S>\"|\' */
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001102 int qc = c;
1103
1104 for(;;) {
1105 /* <STR,CHR>. */
1106 getc1();
1107 if(c == qc) {
1108 /* <STR>\" or <CHR>\' */
1109 break;
1110 }
1111 if(c == BS) {
1112 /* <STR,CHR>\\ but is not <STR,CHR>\\\n */
1113 getc0();
1114 }
1115 if(c == '\n' || c == L_EOF)
1116 yy_error_d("unterminated");
1117 }
1118 getc1();
1119 } else {
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001120 /* <S>[A-Z_a-z0-9] */
"Vladimir N. Oleynik"b1fe4622005-09-12 16:39:47 +00001121
1122 /* trick for fast drop id
1123 if key with this first char undefined */
"Vladimir N. Oleynik"676e95e2005-09-13 16:50:53 +00001124 if(first_chars[c] == 0) {
"Vladimir N. Oleynik"b1fe4622005-09-12 16:39:47 +00001125 /* skip <S>[A-Z_a-z0-9]+ */
"Vladimir N. Oleynik"af0dd592005-09-16 13:57:33 +00001126 do getc1(); while(isalnums[c]);
"Vladimir N. Oleynik"b1fe4622005-09-12 16:39:47 +00001127 } else {
1128 id_len = 0;
1129 do {
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001130 /* <S>[A-Z_a-z0-9]+ */
1131 put_id(c);
1132 getc1();
"Vladimir N. Oleynik"af0dd592005-09-16 13:57:33 +00001133 } while(isalnums[c]);
"Vladimir N. Oleynik"d128b712005-10-03 10:08:46 +00001134 check_key(key_top, id, id_len);
"Vladimir N. Oleynik"b1fe4622005-09-12 16:39:47 +00001135 }
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001136 }
1137 continue;
1138 }
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001139 /* begin preprocessor states */
"Vladimir N. Oleynik"676e95e2005-09-13 16:50:53 +00001140 if(c == L_EOF)
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001141 yy_error_d("unexpected EOF");
"Vladimir N. Oleynik"676e95e2005-09-13 16:50:53 +00001142 if(c == REM) {
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001143 /* <#.*>/ */
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001144 getc0();
"Vladimir N. Oleynik"676e95e2005-09-13 16:50:53 +00001145 if(c == REM)
"Vladimir N. Oleynik"7573ac62005-09-14 15:09:06 +00001146 yy_error_d("detected // in preprocessor line");
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001147 if(c == '*') {
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001148 /* <#.*>[/][*] goto parse block comments */
1149 break;
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001150 }
1151 /* hmm, #.*[/] */
1152 yy_error_d("strange preprocessor line");
1153 }
"Vladimir N. Oleynik"676e95e2005-09-13 16:50:53 +00001154 if(state == POUND) {
"Vladimir N. Oleynik"7573ac62005-09-14 15:09:06 +00001155 /* tricks */
Rob Landley7bfa88f2006-02-13 19:16:41 +00001156 static const char * const p_preproc[] = {
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001157 /* 0 1 2 3 4 5 6 */
1158 "", "", "", "", "ndef", "efine", "nclude"
"Vladimir N. Oleynik"7573ac62005-09-14 15:09:06 +00001159 };
Rob Landley7bfa88f2006-02-13 19:16:41 +00001160 size_t diu = first_chars_diu[c]; /* strlen and p_preproc ptr */
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001161
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001162 state = S;
1163 if(diu != S) {
1164 getc1();
1165 id_len = 0;
1166 while(isalnums[c]) {
1167 put_id(c);
1168 getc1();
1169 }
"Vladimir N. Oleynik"63ca3bf2006-02-14 09:23:25 +00001170 /* str begins with c, read == strlen key and compared */
Rob Landley7bfa88f2006-02-13 19:16:41 +00001171 if(diu == id_len && !memcmp(id, p_preproc[diu], diu)) {
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001172 state = diu + '0';
1173 id_len = 0; /* common for save */
1174 }
"Vladimir N. Oleynik"7573ac62005-09-14 15:09:06 +00001175 } else {
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001176 while(isalnums[c]) getc1();
"Vladimir N. Oleynik"676e95e2005-09-13 16:50:53 +00001177 }
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001178 } else if(state == I) {
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001179 if(c == STR) {
1180 /* <I>\" */
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001181 for(;;) {
1182 getc1();
1183 if(c == STR)
1184 break;
1185 if(c == L_EOF)
1186 yy_error_d("unexpected EOF");
1187 put_id(c);
1188 }
1189 put_id(0);
1190 /* store "include.h" */
1191 parse_inc(id, fname);
1192 getc1();
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001193 }
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001194 /* else another (may be wrong) #include ... */
1195 state = S;
Rob Landley7bfa88f2006-02-13 19:16:41 +00001196 } else /* if(state == D || state == U) */ {
"Vladimir N. Oleynik"676e95e2005-09-13 16:50:53 +00001197 /* ignore depend with #define or #undef KEY */
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001198 while(isalnums[c]) getc1();
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001199 state = S;
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001200 }
1201 }
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001202
1203 /* <REM> */
1204 getc0();
1205 for(;;) {
1206 /* <REM>[^*]+ */
1207 while(c != '*') {
1208 if(c == '\n') {
1209 /* <REM>\n */
1210 if(state != S)
1211 yy_error_d("unexpected newline");
1212 line++;
1213 } else if(c == L_EOF)
1214 yy_error_d("unexpected EOF");
1215 getc0();
1216 }
1217 /* <REM>[*] */
1218 getc0();
1219 if(c == REM) {
1220 /* <REM>[*][/] */
1221 break;
1222 }
1223 }
1224 }
1225too_long:
1226 yy_error_d("phrase too long");
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001227}
1228
1229
"Vladimir N. Oleynik"63ca3bf2006-02-14 09:23:25 +00001230/* bb_simplify_path special variant for absolute pathname */
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001231static size_t bb_qa_simplify_path(char *path)
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001232{
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001233 char *s, *p;
1234
1235 p = s = path;
1236
1237 do {
1238 if (*p == '/') {
1239 if (*s == '/') { /* skip duplicate (or initial) slash */
1240 continue;
1241 } else if (*s == '.') {
1242 if (s[1] == '/' || s[1] == 0) { /* remove extra '.' */
1243 continue;
1244 } else if ((s[1] == '.') && (s[2] == '/' || s[2] == 0)) {
1245 ++s;
1246 if (p > path) {
1247 while (*--p != '/'); /* omit previous dir */
1248 }
1249 continue;
1250 }
1251 }
1252 }
1253 *++p = *s;
1254 } while (*++s);
1255
1256 if ((p == path) || (*p != '/')) { /* not a trailing slash */
1257 ++p; /* so keep last character */
1258 }
1259 *p = 0;
1260
1261 return (p - path);
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001262}
1263
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001264static void parse_inc(const char *include, const char *fname)
"Vladimir N. Oleynik"676e95e2005-09-13 16:50:53 +00001265{
1266 bb_key_t *cur;
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001267 const char *p_i;
1268 llist_t *lo;
1269 char *ap;
1270 size_t key_sz;
"Vladimir N. Oleynik"676e95e2005-09-13 16:50:53 +00001271
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001272 if(*include == '/') {
1273 lo = NULL;
1274 ap = bb_xstrdup(include);
"Vladimir N. Oleynik"676e95e2005-09-13 16:50:53 +00001275 } else {
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001276 int w;
1277 const char *p;
1278
1279 lo = Iop;
"Vladimir N. Oleynik"63ca3bf2006-02-14 09:23:25 +00001280 p = strrchr(fname, '/'); /* fname has absolute pathname */
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001281 w = (p-fname);
1282 /* find from current directory of source file */
1283 ap = bb_asprint("%.*s/%s", w, fname, include);
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001284 }
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001285
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001286 for(;;) {
1287 key_sz = bb_qa_simplify_path(ap);
1288 cur = check_key(Ifound, ap, key_sz);
1289 if(cur) {
1290 cur->checked = cur->value;
1291 free(ap);
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001292 return;
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001293 }
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001294 if(access(ap, F_OK) == 0) {
1295 /* found */
1296 p_i = ap;
1297 break;
1298 } else if(lo == NULL) {
1299 p_i = NULL;
1300 break;
1301 }
1302
1303 /* find from "-I include" specified directories */
1304 free(ap);
"Vladimir N. Oleynik"63ca3bf2006-02-14 09:23:25 +00001305 /* lo->data has absolute pathname */
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001306 ap = bb_asprint("%s/%s", lo->data, include);
1307 lo = lo->link;
1308 }
1309
1310 cur = xmalloc(sizeof(bb_key_t));
Rob Landley7bfa88f2006-02-13 19:16:41 +00001311 cur->keyname = ap;
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001312 cur->key_sz = key_sz;
Rob Landley7bfa88f2006-02-13 19:16:41 +00001313 cur->stored_path = ap;
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001314 cur->value = cur->checked = p_i;
1315 if(p_i == NULL && noiwarning)
1316 fprintf(stderr, "%s: Warning: #include \"%s\" not found\n", fname, include);
1317 cur->next = Ifound;
1318 Ifound = cur;
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001319}
1320
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001321static size_t max_rec_sz;
1322
1323static void parse_conf_opt(const char *opt, const char *val, size_t key_sz)
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001324{
"Vladimir N. Oleynik"676e95e2005-09-13 16:50:53 +00001325 bb_key_t *cur;
"Vladimir N. Oleynik"d128b712005-10-03 10:08:46 +00001326 char *k;
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001327 char *p;
1328 size_t val_sz = 0;
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001329
"Vladimir N. Oleynik"d128b712005-10-03 10:08:46 +00001330 cur = check_key(key_top, opt, key_sz);
"Vladimir N. Oleynik"7573ac62005-09-14 15:09:06 +00001331 if(cur != NULL) {
"Vladimir N. Oleynik"63ca3bf2006-02-14 09:23:25 +00001332 /* already present */
"Vladimir N. Oleynik"d128b712005-10-03 10:08:46 +00001333 cur->checked = NULL; /* store only */
"Vladimir N. Oleynik"7573ac62005-09-14 15:09:06 +00001334 if(cur->value == NULL && val == NULL)
1335 return;
"Vladimir N. Oleynik"d128b712005-10-03 10:08:46 +00001336 if(cur->value != NULL && val != NULL && !strcmp(cur->value, val))
"Vladimir N. Oleynik"7573ac62005-09-14 15:09:06 +00001337 return;
"Vladimir N. Oleynik"d128b712005-10-03 10:08:46 +00001338 k = cur->keyname;
1339 fprintf(stderr, "Warning: redefined %s\n", k);
"Vladimir N. Oleynik"af0dd592005-09-16 13:57:33 +00001340 } else {
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001341 size_t recordsz;
1342
1343 if(val && *val)
1344 val_sz = strlen(val) + 1;
1345 recordsz = key_sz + val_sz + 1;
1346 if(max_rec_sz < recordsz)
1347 max_rec_sz = recordsz;
1348 cur = xmalloc(sizeof(bb_key_t) + recordsz);
1349 k = cur->keyname = memcpy(cur + 1, opt, key_sz);
1350 cur->keyname[key_sz] = '\0';
1351 cur->key_sz = key_sz;
1352 cur->checked = NULL;
1353 cur->src_have_this_key = NULL;
1354 cur->next = key_top;
1355 key_top = cur;
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001356 }
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001357 /* save VAL */
"Vladimir N. Oleynik"7573ac62005-09-14 15:09:06 +00001358 if(val) {
1359 if(*val == '\0') {
1360 cur->value = "";
"Vladimir N. Oleynik"7573ac62005-09-14 15:09:06 +00001361 } else {
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001362 cur->value = p = cur->keyname + key_sz + 1;
1363 memcpy(p, val, val_sz);
"Vladimir N. Oleynik"7573ac62005-09-14 15:09:06 +00001364 }
1365 } else {
1366 cur->value = NULL;
"Vladimir N. Oleynik"7573ac62005-09-14 15:09:06 +00001367 }
"Vladimir N. Oleynik"7573ac62005-09-14 15:09:06 +00001368 /* trick, save first char KEY for do fast identify id */
"Vladimir N. Oleynik"d128b712005-10-03 10:08:46 +00001369 first_chars[(int)*k] = *k;
"Vladimir N. Oleynik"7573ac62005-09-14 15:09:06 +00001370
"Vladimir N. Oleynik"d128b712005-10-03 10:08:46 +00001371 cur->stored_path = k = bb_asprint("%s/%s.h", kp, k);
"Vladimir N. Oleynik"63ca3bf2006-02-14 09:23:25 +00001372 /* key conversion [A-Z_] -> [a-z/] */
"Vladimir N. Oleynik"d128b712005-10-03 10:08:46 +00001373 for(p = k + kp_len + 1; *p; p++) {
"Vladimir N. Oleynik"7573ac62005-09-14 15:09:06 +00001374 if(*p >= 'A' && *p <= 'Z')
1375 *p = *p - 'A' + 'a';
"Vladimir N. Oleynik"af0dd592005-09-16 13:57:33 +00001376 else if(*p == '_' && p[1] > '9') /* do not change A_1 to A/1 */
"Vladimir N. Oleynik"7573ac62005-09-14 15:09:06 +00001377 *p = '/';
1378 }
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001379}
1380
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001381static void store_keys(void)
1382{
1383 bb_key_t *cur;
1384 char *k;
1385 struct stat st;
1386 int cmp_ok;
1387 ssize_t rw_ret;
1388 size_t recordsz = max_rec_sz * 2 + 10 * 2 + 16;
1389 /* buffer for double "#define KEY VAL\n" */
1390 char *record_buf = xmalloc(recordsz);
1391
1392 for(cur = key_top; cur; cur = cur->next) {
1393 if(cur->src_have_this_key) {
1394 /* do generate record */
1395 k = cur->keyname;
1396 if(cur->value == NULL) {
1397 recordsz = sprintf(record_buf, "#undef %s\n", k);
1398 } else {
1399 const char *val = cur->value;
1400 if(*val == '\0') {
1401 recordsz = sprintf(record_buf, "#define %s\n", k);
1402 } else {
1403 recordsz = sprintf(record_buf, "#define %s %s\n", k, val);
1404 }
1405 }
1406 /* size_t -> ssize_t :( */
1407 rw_ret = (ssize_t)recordsz;
"Vladimir N. Oleynik"63ca3bf2006-02-14 09:23:25 +00001408 /* check kp/key.h, compare after previous use */
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001409 cmp_ok = 0;
1410 k = cur->stored_path;
1411 if(stat(k, &st)) {
1412 char *p;
1413 for(p = k + kp_len + 1; *p; p++) {
1414 /* Auto-create directories. */
1415 if (*p == '/') {
1416 *p = '\0';
1417 if (access(k, F_OK) != 0 && mkdir(k, 0755) != 0)
1418 bb_error_d("mkdir(%s): %m", k);
1419 *p = '/';
1420 }
1421 }
1422 } else {
1423 /* found */
1424 if(st.st_size == (off_t)recordsz) {
1425 char *r_cmp;
1426 int fd;
1427 size_t padded = recordsz;
1428
1429 /* 16-byte padding for read(2) and memcmp(3) */
1430 padded = (padded+15) & ~15;
1431 r_cmp = record_buf + padded;
1432 fd = open(k, O_RDONLY);
1433 if(fd < 0 || read(fd, r_cmp, recordsz) < rw_ret)
1434 bb_error_d("%s: %m", k);
1435 close(fd);
1436 cmp_ok = memcmp(record_buf, r_cmp, recordsz) == 0;
1437 }
1438 }
1439 if(!cmp_ok) {
1440 int fd = open(k, O_WRONLY|O_CREAT|O_TRUNC, 0644);
1441 if(fd < 0 || write(fd, record_buf, recordsz) < rw_ret)
1442 bb_error_d("%s: %m", k);
1443 close(fd);
1444 }
1445 }
1446 }
1447}
"Vladimir N. Oleynik"6c0642d2005-10-07 15:36:26 +00001448
1449static int show_dep(int first, bb_key_t *k, const char *name, const char *f)
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001450{
1451 bb_key_t *cur;
1452
1453 for(cur = k; cur; cur = cur->next) {
"Vladimir N. Oleynik"d128b712005-10-03 10:08:46 +00001454 if(cur->checked) {
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001455 if(first >= 0) {
1456 if(first) {
1457 if(f == NULL)
"Vladimir N. Oleynik"6c0642d2005-10-07 15:36:26 +00001458 printf("\n%s:", name);
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001459 else
"Vladimir N. Oleynik"6c0642d2005-10-07 15:36:26 +00001460 printf("\n%s/%s:", pwd, name);
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001461 first = 0;
1462 } else {
1463 printf(" \\\n ");
1464 }
1465 printf(" %s", cur->checked);
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001466 }
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001467 cur->src_have_this_key = cur->checked;
1468 cur->checked = NULL;
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001469 }
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001470 }
1471 return first;
1472}
1473
"Vladimir N. Oleynik"676e95e2005-09-13 16:50:53 +00001474static char *
"Vladimir N. Oleynik"7573ac62005-09-14 15:09:06 +00001475parse_chd(const char *fe, const char *p, size_t dirlen)
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001476{
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001477 struct stat st;
1478 char *fp;
"Vladimir N. Oleynik"676e95e2005-09-13 16:50:53 +00001479 size_t df_sz;
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001480 static char dir_and_entry[4096];
1481 size_t fe_sz = strlen(fe) + 1;
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001482
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001483 df_sz = dirlen + fe_sz + 1; /* dir/file\0 */
1484 if(df_sz > sizeof(dir_and_entry))
1485 bb_error_d("%s: file name too long", fe);
"Vladimir N. Oleynik"676e95e2005-09-13 16:50:53 +00001486 fp = dir_and_entry;
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001487 /* sprintf(fp, "%s/%s", p, fe); */
1488 memcpy(fp, p, dirlen);
1489 fp[dirlen] = '/';
1490 memcpy(fp + dirlen + 1, fe, fe_sz);
"Vladimir N. Oleynik"676e95e2005-09-13 16:50:53 +00001491
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001492 if(stat(fp, &st)) {
"Vladimir N. Oleynik"d128b712005-10-03 10:08:46 +00001493 fprintf(stderr, "Warning: stat(%s): %m\n", fp);
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001494 return NULL;
1495 }
"Vladimir N. Oleynik"b1fe4622005-09-12 16:39:47 +00001496 if(S_ISREG(st.st_mode)) {
"Vladimir N. Oleynik"7573ac62005-09-14 15:09:06 +00001497 llist_t *cfl;
1498 char *e = fp + df_sz - 3;
1499
1500 if(*e++ != '.' || (*e != 'c' && *e != 'h')) {
"Vladimir N. Oleynik"b1fe4622005-09-12 16:39:47 +00001501 /* direntry is regular file, but is not *.[ch] */
"Vladimir N. Oleynik"676e95e2005-09-13 16:50:53 +00001502 return NULL;
1503 }
"Vladimir N. Oleynik"7573ac62005-09-14 15:09:06 +00001504 for(cfl = configs; cfl; cfl = cfl->link) {
1505 struct stat *config = (struct stat *)cfl->data;
1506
1507 if (st.st_dev == config->st_dev && st.st_ino == config->st_ino) {
1508 /* skip already parsed configs.h */
"Vladimir N. Oleynik"b1fe4622005-09-12 16:39:47 +00001509 return NULL;
1510 }
"Vladimir N. Oleynik"b1fe4622005-09-12 16:39:47 +00001511 }
"Vladimir N. Oleynik"7573ac62005-09-14 15:09:06 +00001512 /* direntry is *.[ch] regular file and is not configs */
Rob Landley7bfa88f2006-02-13 19:16:41 +00001513 c_lex_src(fp, st.st_size);
"Vladimir N. Oleynik"7573ac62005-09-14 15:09:06 +00001514 if(!dontgenerate_dep) {
1515 int first;
"Vladimir N. Oleynik"7573ac62005-09-14 15:09:06 +00001516 if(*e == 'c') {
1517 /* *.c -> *.o */
"Vladimir N. Oleynik"7573ac62005-09-14 15:09:06 +00001518 *e = 'o';
"Vladimir N. Oleynik"6c0642d2005-10-07 15:36:26 +00001519 /* /src_dir/path/file.o to path/file.o */
1520 fp += replace;
1521 if(*fp == '/')
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001522 fp++;
"Vladimir N. Oleynik"d5f2a182005-10-06 14:47:16 +00001523 } else {
1524 e = NULL;
"Vladimir N. Oleynik"7573ac62005-09-14 15:09:06 +00001525 }
"Vladimir N. Oleynik"6c0642d2005-10-07 15:36:26 +00001526 first = show_dep(1, Ifound, fp, e);
1527 first = show_dep(first, key_top, fp, e);
"Vladimir N. Oleynik"7573ac62005-09-14 15:09:06 +00001528 if(first == 0)
1529 putchar('\n');
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001530 } else {
1531 show_dep(-1, key_top, NULL, NULL);
"Vladimir N. Oleynik"7573ac62005-09-14 15:09:06 +00001532 }
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001533 return NULL;
"Vladimir N. Oleynik"7573ac62005-09-14 15:09:06 +00001534 } else if(S_ISDIR(st.st_mode)) {
1535 if (st.st_dev == st_kp.st_dev && st.st_ino == st_kp.st_ino)
1536 return NULL; /* drop scan kp/ directory */
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001537 /* direntry is directory. buff is returned */
1538 return bb_xstrdup(fp);
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001539 }
"Vladimir N. Oleynik"7573ac62005-09-14 15:09:06 +00001540 /* hmm, direntry is device! */
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001541 return NULL;
1542}
1543
"Vladimir N. Oleynik"63ca3bf2006-02-14 09:23:25 +00001544/* from libbb but inlined for speed considerations */
"Vladimir N. Oleynik"7573ac62005-09-14 15:09:06 +00001545static inline llist_t *llist_add_to(llist_t *old_head, char *new_item)
1546{
1547 llist_t *new_head;
1548
1549 new_head = xmalloc(sizeof(llist_t));
1550 new_head->data = new_item;
1551 new_head->link = old_head;
1552
1553 return(new_head);
1554}
1555
"Vladimir N. Oleynik"d128b712005-10-03 10:08:46 +00001556static void scan_dir_find_ch_files(const char *p)
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001557{
1558 llist_t *dirs;
1559 llist_t *d_add;
1560 llist_t *d;
1561 struct dirent *de;
1562 DIR *dir;
"Vladimir N. Oleynik"676e95e2005-09-13 16:50:53 +00001563 size_t dirlen;
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001564
"Vladimir N. Oleynik"d128b712005-10-03 10:08:46 +00001565 dirs = llist_add_to(NULL, bb_simplify_path(p));
"Vladimir N. Oleynik"6c0642d2005-10-07 15:36:26 +00001566 replace = strlen(dirs->data);
"Vladimir N. Oleynik"63ca3bf2006-02-14 09:23:25 +00001567 /* emulate recursion */
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001568 while(dirs) {
1569 d_add = NULL;
1570 while(dirs) {
1571 dir = opendir(dirs->data);
1572 if (dir == NULL)
"Vladimir N. Oleynik"d128b712005-10-03 10:08:46 +00001573 fprintf(stderr, "Warning: opendir(%s): %m\n", dirs->data);
"Vladimir N. Oleynik"676e95e2005-09-13 16:50:53 +00001574 dirlen = strlen(dirs->data);
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001575 while ((de = readdir(dir)) != NULL) {
"Vladimir N. Oleynik"d128b712005-10-03 10:08:46 +00001576 char *found_dir;
"Vladimir N. Oleynik"676e95e2005-09-13 16:50:53 +00001577
"Vladimir N. Oleynik"d128b712005-10-03 10:08:46 +00001578 if (de->d_name[0] == '.')
1579 continue;
1580 found_dir = parse_chd(de->d_name, dirs->data, dirlen);
"Vladimir N. Oleynik"676e95e2005-09-13 16:50:53 +00001581 if(found_dir)
1582 d_add = llist_add_to(d_add, found_dir);
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001583 }
1584 closedir(dir);
"Vladimir N. Oleynik"d128b712005-10-03 10:08:46 +00001585 free(dirs->data);
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001586 d = dirs;
1587 dirs = dirs->link;
1588 free(d);
1589 }
1590 dirs = d_add;
1591 }
1592}
1593
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001594static void show_usage(void) __attribute__ ((noreturn));
1595static void show_usage(void)
1596{
1597 bb_error_d("%s\n%s\n", bb_mkdep_terse_options, bb_mkdep_full_options);
1598}
"Vladimir N. Oleynik"676e95e2005-09-13 16:50:53 +00001599
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001600int main(int argc, char **argv)
1601{
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001602 char *s;
1603 int i;
1604 llist_t *fl;
1605
"Vladimir N. Oleynik"676e95e2005-09-13 16:50:53 +00001606 {
Bernhard Reutner-Fischer5ba53c02006-02-14 10:43:40 +00001607 /* for bb_simplify_path, this program has no chdir() */
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001608 /* libbb-like my xgetcwd() */
"Vladimir N. Oleynik"676e95e2005-09-13 16:50:53 +00001609 unsigned path_max = 512;
1610
1611 s = xmalloc (path_max);
"Vladimir N. Oleynik"676e95e2005-09-13 16:50:53 +00001612 while (getcwd (s, path_max) == NULL) {
1613 if(errno != ERANGE)
1614 bb_error_d("getcwd: %m");
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001615 free(s);
1616 s = xmalloc(path_max *= 2);
1617 }
"Vladimir N. Oleynik"676e95e2005-09-13 16:50:53 +00001618 pwd = s;
1619 }
1620
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001621 while ((i = getopt(argc, argv, "I:c:dk:w")) > 0) {
1622 switch(i) {
1623 case 'I':
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001624 s = bb_simplify_path(optarg);
1625 Iop = llist_add_to(Iop, s);
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001626 break;
1627 case 'c':
1628 s = bb_simplify_path(optarg);
1629 configs = llist_add_to(configs, s);
1630 break;
1631 case 'd':
"Vladimir N. Oleynik"7573ac62005-09-14 15:09:06 +00001632 dontgenerate_dep = 1;
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001633 break;
1634 case 'k':
1635 if(kp)
1636 bb_error_d("Hmm, why multiple -k?");
1637 kp = bb_simplify_path(optarg);
1638 break;
1639 case 'w':
1640 noiwarning = 1;
1641 break;
1642 default:
1643 show_usage();
1644 }
1645 }
"Vladimir N. Oleynik"7573ac62005-09-14 15:09:06 +00001646 /* default kp */
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001647 if(kp == NULL)
1648 kp = bb_simplify_path(INCLUDE_CONFIG_PATH);
"Vladimir N. Oleynik"7573ac62005-09-14 15:09:06 +00001649 /* globals initialize */
1650 kp_len = strlen(kp);
1651 if(stat(kp, &st_kp))
1652 bb_error_d("stat(%s): %m", kp);
1653 if(!S_ISDIR(st_kp.st_mode))
1654 bb_error_d("%s is not directory", kp);
1655 /* defaults */
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001656 if(Iop == NULL)
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001657 Iop = llist_add_to(Iop, bb_simplify_path(LOCAL_INCLUDE_PATH));
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001658 if(configs == NULL) {
1659 s = bb_simplify_path(INCLUDE_CONFIG_KEYS_PATH);
1660 configs = llist_add_to(configs, s);
1661 }
"Vladimir N. Oleynik"676e95e2005-09-13 16:50:53 +00001662 /* for c_lex */
1663 pagesizem1 = getpagesize() - 1;
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001664 for(i = 0; i < UCHAR_MAX; i++) {
"Vladimir N. Oleynik"af0dd592005-09-16 13:57:33 +00001665 if(ISALNUM(i))
1666 isalnums[i] = i;
"Vladimir N. Oleynik"63ca3bf2006-02-14 09:23:25 +00001667 /* set unparsed chars to speed up the parser */
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001668 else if(i != CHR && i != STR && i != POUND && i != REM)
"Vladimir N. Oleynik"676e95e2005-09-13 16:50:53 +00001669 first_chars[i] = ANY;
1670 }
1671 first_chars[i] = '-'; /* L_EOF */
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001672
"Vladimir N. Oleynik"7573ac62005-09-14 15:09:06 +00001673 /* parse configs */
1674 for(fl = configs; fl; fl = fl->link) {
1675 struct stat st;
"Vladimir N. Oleynik"676e95e2005-09-13 16:50:53 +00001676
"Vladimir N. Oleynik"7573ac62005-09-14 15:09:06 +00001677 if(stat(fl->data, &st))
1678 bb_error_d("stat(%s): %m", fl->data);
Rob Landley7bfa88f2006-02-13 19:16:41 +00001679 c_lex_config(fl->data, st.st_size);
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001680 free(fl->data);
"Vladimir N. Oleynik"7573ac62005-09-14 15:09:06 +00001681 /* trick for fast comparing found files with configs */
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001682 fl->data = xmalloc(sizeof(struct stat));
"Vladimir N. Oleynik"7573ac62005-09-14 15:09:06 +00001683 memcpy(fl->data, &st, sizeof(struct stat));
1684 }
1685
1686 /* main loop */
"Vladimir N. Oleynik"676e95e2005-09-13 16:50:53 +00001687 argv += optind;
1688 if(*argv) {
1689 while(*argv)
1690 scan_dir_find_ch_files(*argv++);
1691 } else {
1692 scan_dir_find_ch_files(".");
1693 }
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +00001694 store_keys();
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001695 return 0;
1696}
1697
"Vladimir N. Oleynik"7573ac62005-09-14 15:09:06 +00001698/* partial and simplified libbb routine */
"Vladimir N. Oleynik"b1fe4622005-09-12 16:39:47 +00001699static void bb_error_d(const char *s, ...)
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001700{
1701 va_list p;
1702
1703 va_start(p, s);
1704 vfprintf(stderr, s, p);
1705 va_end(p);
1706 putc('\n', stderr);
1707 exit(1);
1708}
1709
"Vladimir N. Oleynik"b1fe4622005-09-12 16:39:47 +00001710static char *bb_asprint(const char *format, ...)
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001711{
1712 va_list p;
1713 int r;
1714 char *out;
1715
1716 va_start(p, format);
1717 r = vasprintf(&out, format, p);
1718 va_end(p);
1719
1720 if (r < 0)
1721 bb_error_d("bb_asprint: %m");
1722 return out;
1723}
1724
"Vladimir N. Oleynik"7573ac62005-09-14 15:09:06 +00001725/* partial libbb routine as is */
Rob Landley7bfa88f2006-02-13 19:16:41 +00001726
"Vladimir N. Oleynik"b1fe4622005-09-12 16:39:47 +00001727static char *bb_simplify_path(const char *path)
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001728{
1729 char *s, *start, *p;
1730
1731 if (path[0] == '/')
"Vladimir N. Oleynik"676e95e2005-09-13 16:50:53 +00001732 start = bb_xstrdup(path);
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001733 else {
Bernhard Reutner-Fischer5ba53c02006-02-14 10:43:40 +00001734 /* is not libbb, but this program has no chdir() */
"Vladimir N. Oleynik"676e95e2005-09-13 16:50:53 +00001735 start = bb_asprint("%s/%s", pwd, path);
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001736 }
1737 p = s = start;
1738
1739 do {
"Vladimir N. Oleynik"b1fe4622005-09-12 16:39:47 +00001740 if (*p == '/') {
1741 if (*s == '/') { /* skip duplicate (or initial) slash */
1742 continue;
1743 } else if (*s == '.') {
1744 if (s[1] == '/' || s[1] == 0) { /* remove extra '.' */
1745 continue;
1746 } else if ((s[1] == '.') && (s[2] == '/' || s[2] == 0)) {
1747 ++s;
1748 if (p > start) {
1749 while (*--p != '/'); /* omit previous dir */
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001750 }
"Vladimir N. Oleynik"b1fe4622005-09-12 16:39:47 +00001751 continue;
1752 }
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001753 }
"Vladimir N. Oleynik"b1fe4622005-09-12 16:39:47 +00001754 }
1755 *++p = *s;
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001756 } while (*++s);
1757
"Vladimir N. Oleynik"b1fe4622005-09-12 16:39:47 +00001758 if ((p == start) || (*p != '/')) { /* not a trailing slash */
1759 ++p; /* so keep last character */
"Vladimir N. Oleynik"5e60dc42005-09-12 12:33:27 +00001760 }
1761 *p = 0;
1762
1763 return start;
1764}