2005-09-12 18:03:27 +05:30
|
|
|
/*
|
2005-10-03 15:38:46 +05:30
|
|
|
* Another fast dependencies generator for Makefiles, Version 2.3
|
2005-09-12 18:03:27 +05:30
|
|
|
*
|
|
|
|
* Copyright (C) 2005 by Vladimir Oleynik <dzo@simtreas.ru>
|
2005-09-16 19:27:33 +05:30
|
|
|
* mmaping file may be originally by Linus Torvalds.
|
|
|
|
*
|
|
|
|
* (c) 2005 Bernhard Fischer:
|
|
|
|
* - commentary typos,
|
|
|
|
* - move "memory exhausted" into msg_enomem,
|
|
|
|
* - more verbose --help output.
|
2005-09-12 18:03:27 +05:30
|
|
|
*
|
2005-09-14 20:39:06 +05:30
|
|
|
* This program does:
|
2005-09-12 18:03:27 +05:30
|
|
|
* 1) find #define KEY VALUE or #undef KEY from include/config.h
|
|
|
|
* 2) save include/config/key*.h if changed after previous usage
|
2005-09-14 20:39:06 +05:30
|
|
|
* 3) recursive scan from "./" *.[ch] files, but skips scan of include/config/
|
2005-09-12 18:03:27 +05:30
|
|
|
* 4) find #include "*.h" and KEYs using, if not as #define and #undef
|
2005-09-14 20:39:06 +05:30
|
|
|
* 5) generate dependencies to stdout
|
2005-09-12 18:03:27 +05:30
|
|
|
* path/file.o: include/config/key*.h found_include_*.h
|
|
|
|
* path/inc.h: include/config/key*.h found_included_include_*.h
|
2005-09-16 19:27:33 +05:30
|
|
|
* This program does not generate dependencies for #include <...>
|
2005-10-03 15:38:46 +05:30
|
|
|
* BUG: all includes name must unique
|
2005-09-14 20:39:06 +05:30
|
|
|
*/
|
2005-09-12 18:03:27 +05:30
|
|
|
|
|
|
|
#define LOCAL_INCLUDE_PATH "include"
|
|
|
|
#define INCLUDE_CONFIG_PATH LOCAL_INCLUDE_PATH"/config"
|
|
|
|
#define INCLUDE_CONFIG_KEYS_PATH LOCAL_INCLUDE_PATH"/config.h"
|
|
|
|
|
2005-09-14 20:39:06 +05:30
|
|
|
#define bb_mkdep_full_options \
|
|
|
|
"\nOptions:" \
|
|
|
|
"\n\t-I local_include_path include paths, default: \"" LOCAL_INCLUDE_PATH "\"" \
|
|
|
|
"\n\t-d don't generate depend" \
|
|
|
|
"\n\t-w show warning if include files not found" \
|
|
|
|
"\n\t-k include/config default: \"" INCLUDE_CONFIG_PATH "\"" \
|
|
|
|
"\n\t-c include/config.h configs, default: \"" INCLUDE_CONFIG_KEYS_PATH "\"" \
|
|
|
|
"\n\tdirs_to_scan default \".\""
|
|
|
|
|
|
|
|
#define bb_mkdep_terse_options "Usage: [-I local_include_paths] [-dw] " \
|
|
|
|
"[-k path_for_stored_keys] [dirs]"
|
|
|
|
|
|
|
|
|
2005-09-12 18:03:27 +05:30
|
|
|
#define _GNU_SOURCE
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
2005-09-14 20:39:06 +05:30
|
|
|
|
2005-09-12 18:03:27 +05:30
|
|
|
typedef struct BB_KEYS {
|
|
|
|
char *keyname;
|
2005-10-03 15:38:46 +05:30
|
|
|
size_t key_sz;
|
2005-09-12 18:03:27 +05:30
|
|
|
const char *value;
|
|
|
|
char *stored_path;
|
2005-10-03 15:38:46 +05:30
|
|
|
char *checked;
|
2005-09-12 18:03:27 +05:30
|
|
|
struct BB_KEYS *next;
|
|
|
|
} bb_key_t;
|
|
|
|
|
2005-10-03 15:38:46 +05:30
|
|
|
static bb_key_t *check_key(bb_key_t *k, const char *nk, size_t key_sz);
|
|
|
|
static bb_key_t *make_new_key(bb_key_t *k, const char *nk, size_t key_sz);
|
2005-09-12 18:03:27 +05:30
|
|
|
|
2005-09-14 20:39:06 +05:30
|
|
|
/* partial and simplified libbb routine */
|
2005-09-12 22:09:47 +05:30
|
|
|
static void bb_error_d(const char *s, ...) __attribute__ ((noreturn, format (printf, 1, 2)));
|
|
|
|
static char * bb_asprint(const char *format, ...) __attribute__ ((format (printf, 1, 2)));
|
2005-09-12 18:03:27 +05:30
|
|
|
|
|
|
|
/* stolen from libbb as is */
|
|
|
|
typedef struct llist_s {
|
|
|
|
char *data;
|
|
|
|
struct llist_s *link;
|
|
|
|
} llist_t;
|
2005-09-12 22:09:47 +05:30
|
|
|
static void *xrealloc(void *p, size_t size);
|
|
|
|
static void *xmalloc(size_t size);
|
|
|
|
static char *bb_xstrdup(const char *s);
|
|
|
|
static char *bb_simplify_path(const char *path);
|
2005-09-14 20:39:06 +05:30
|
|
|
/* error messages */
|
|
|
|
static const char msg_enomem[] = "memory exhausted";
|
2005-09-12 18:03:27 +05:30
|
|
|
|
2005-09-14 20:39:06 +05:30
|
|
|
/* for lexical analyser */
|
2005-09-12 18:03:27 +05:30
|
|
|
static bb_key_t *key_top;
|
2005-09-13 22:20:53 +05:30
|
|
|
static llist_t *configs;
|
2005-09-12 18:03:27 +05:30
|
|
|
|
2005-09-14 20:39:06 +05:30
|
|
|
static int mode;
|
|
|
|
#define CONFIG_MODE 0
|
|
|
|
#define SOURCES_MODE 1
|
|
|
|
|
2005-10-03 15:38:46 +05:30
|
|
|
static void parse_inc(const char *include, const char *fname, size_t key_sz);
|
|
|
|
static void parse_conf_opt(const char *opt, const char *val,
|
|
|
|
size_t rsz, size_t key_sz);
|
2005-09-12 18:03:27 +05:30
|
|
|
|
2005-09-14 20:39:06 +05:30
|
|
|
/* for speed tricks */
|
2005-09-13 22:20:53 +05:30
|
|
|
static char first_chars[257]; /* + L_EOF */
|
2005-09-16 19:27:33 +05:30
|
|
|
static char isalnums[257]; /* + L_EOF */
|
2005-09-14 20:39:06 +05:30
|
|
|
/* trick for fast find "define", "include", "undef" */
|
|
|
|
static char first_chars_diu[256];
|
2005-09-12 22:09:47 +05:30
|
|
|
|
2005-09-13 22:20:53 +05:30
|
|
|
static int pagesizem1;
|
|
|
|
static size_t mema_id = 128; /* first allocated for id */
|
|
|
|
static char *id_s;
|
|
|
|
|
2005-09-14 20:39:06 +05:30
|
|
|
|
2005-09-12 18:03:27 +05:30
|
|
|
#define yy_error_d(s) bb_error_d("%s:%d hmm, %s", fname, line, s)
|
|
|
|
|
|
|
|
/* state */
|
|
|
|
#define S 0 /* start state */
|
|
|
|
#define STR '"' /* string */
|
|
|
|
#define CHR '\'' /* char */
|
2005-09-13 22:20:53 +05:30
|
|
|
#define REM '/' /* block comment */
|
|
|
|
#define BS '\\' /* back slash */
|
2005-09-12 18:03:27 +05:30
|
|
|
#define POUND '#' /* # */
|
2005-09-14 20:39:06 +05:30
|
|
|
#define I 'i' /* #include preprocessor's directive */
|
|
|
|
#define D 'd' /* #define preprocessor's directive */
|
|
|
|
#define U 'u' /* #undef preprocessor's directive */
|
2005-09-12 18:03:27 +05:30
|
|
|
#define LI 'I' /* #include "... */
|
|
|
|
#define DK 'K' /* #define KEY... (config mode) */
|
2005-09-13 22:20:53 +05:30
|
|
|
#define DV 'V' /* #define KEY "VALUE or #define KEY 'VALUE */
|
|
|
|
#define NLC 'n' /* \ and \n */
|
|
|
|
#define ANY '*' /* any unparsed chars */
|
2005-09-12 18:03:27 +05:30
|
|
|
|
2005-09-13 22:20:53 +05:30
|
|
|
#define L_EOF 256
|
2005-09-12 18:03:27 +05:30
|
|
|
/* [A-Z_a-z] */
|
|
|
|
#define ID(c) ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c == '_')
|
|
|
|
/* [A-Z_a-z0-9] */
|
|
|
|
#define ISALNUM(c) (ID(c) || (c >= '0' && c <= '9'))
|
|
|
|
|
2005-09-13 22:20:53 +05:30
|
|
|
#define getc1() do { c = (optr >= oend) ? L_EOF : *optr++; } while(0)
|
2005-09-12 18:03:27 +05:30
|
|
|
#define ungetc1() optr--
|
|
|
|
|
2005-09-13 22:20:53 +05:30
|
|
|
#define put_id(c) do { if(id_len == local_mema_id) \
|
|
|
|
id = xrealloc(id, local_mema_id += 16); \
|
2005-09-12 18:03:27 +05:30
|
|
|
id[id_len++] = c; } while(0)
|
|
|
|
|
2005-09-14 20:39:06 +05:30
|
|
|
|
|
|
|
|
|
|
|
/* stupid C lexical analyser */
|
2005-09-13 22:20:53 +05:30
|
|
|
static void c_lex(const char *fname, long fsize)
|
2005-09-12 18:03:27 +05:30
|
|
|
{
|
2005-09-13 22:20:53 +05:30
|
|
|
int c = L_EOF; /* stupid initialize */
|
|
|
|
int prev_state = L_EOF;
|
2005-09-12 18:03:27 +05:30
|
|
|
int called;
|
|
|
|
int state;
|
|
|
|
int line;
|
2005-09-13 22:20:53 +05:30
|
|
|
char *id = id_s;
|
|
|
|
size_t local_mema_id = mema_id;
|
2005-09-12 18:03:27 +05:30
|
|
|
size_t id_len = 0; /* stupid initialize */
|
|
|
|
char *val = NULL;
|
|
|
|
unsigned char *optr, *oend;
|
|
|
|
unsigned char *start = NULL; /* stupid initialize */
|
2005-10-03 15:38:46 +05:30
|
|
|
size_t opt_len = 0; /* stupid initialize */
|
2005-09-12 18:03:27 +05:30
|
|
|
|
|
|
|
int fd;
|
|
|
|
char *map;
|
|
|
|
int mapsize;
|
|
|
|
|
2005-09-14 20:39:06 +05:30
|
|
|
if(fsize == 0) {
|
|
|
|
fprintf(stderr, "Warning: %s is empty\n", fname);
|
|
|
|
return;
|
|
|
|
}
|
2005-09-13 22:20:53 +05:30
|
|
|
fd = open(fname, O_RDONLY);
|
|
|
|
if(fd < 0) {
|
2005-09-12 18:03:27 +05:30
|
|
|
perror(fname);
|
|
|
|
return;
|
2005-09-13 22:20:53 +05:30
|
|
|
}
|
|
|
|
mapsize = (fsize+pagesizem1) & ~pagesizem1;
|
|
|
|
map = mmap(NULL, mapsize, PROT_READ, MAP_PRIVATE, fd, 0);
|
|
|
|
if ((long) map == -1)
|
2005-09-12 18:03:27 +05:30
|
|
|
bb_error_d("%s: mmap: %m", fname);
|
|
|
|
|
2005-09-13 22:20:53 +05:30
|
|
|
optr = (unsigned char *)map;
|
|
|
|
oend = optr + fsize;
|
2005-09-12 18:03:27 +05:30
|
|
|
|
|
|
|
line = 1;
|
|
|
|
called = state = S;
|
|
|
|
|
|
|
|
for(;;) {
|
|
|
|
if(prev_state != state) {
|
|
|
|
prev_state = state;
|
|
|
|
getc1();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* [ \t]+ eat first space */
|
|
|
|
while(c == ' ' || c == '\t')
|
|
|
|
getc1();
|
|
|
|
|
2005-09-13 22:20:53 +05:30
|
|
|
if(c == BS) {
|
2005-09-12 18:03:27 +05:30
|
|
|
getc1();
|
|
|
|
if(c == '\n') {
|
|
|
|
/* \\\n eat continued */
|
|
|
|
line++;
|
|
|
|
prev_state = NLC;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
ungetc1();
|
2005-09-13 22:20:53 +05:30
|
|
|
c = BS;
|
2005-09-12 18:03:27 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if(state == S) {
|
2005-09-13 22:20:53 +05:30
|
|
|
while(first_chars[c] == ANY) {
|
|
|
|
/* <S>unparsed */
|
2005-09-12 18:03:27 +05:30
|
|
|
if(c == '\n')
|
|
|
|
line++;
|
|
|
|
getc1();
|
|
|
|
}
|
2005-09-13 22:20:53 +05:30
|
|
|
if(c == L_EOF) {
|
2005-09-12 18:03:27 +05:30
|
|
|
/* <S><<EOF>> */
|
2005-09-13 22:20:53 +05:30
|
|
|
id_s = id;
|
|
|
|
mema_id = local_mema_id;
|
2005-09-12 18:03:27 +05:30
|
|
|
munmap(map, mapsize);
|
|
|
|
close(fd);
|
|
|
|
return;
|
|
|
|
}
|
2005-09-13 22:20:53 +05:30
|
|
|
if(c == REM) {
|
2005-09-12 18:03:27 +05:30
|
|
|
/* <S>/ */
|
2005-09-13 22:20:53 +05:30
|
|
|
getc1(); /* eat <S>/ */
|
|
|
|
if(c == REM) {
|
2005-09-12 18:03:27 +05:30
|
|
|
/* <S>"//"[^\n]* */
|
2005-09-13 22:20:53 +05:30
|
|
|
do getc1(); while(c != '\n' && c != L_EOF);
|
2005-09-12 18:03:27 +05:30
|
|
|
} else if(c == '*') {
|
|
|
|
/* <S>[/][*] */
|
|
|
|
called = S;
|
|
|
|
state = REM;
|
|
|
|
}
|
2005-09-13 22:20:53 +05:30
|
|
|
} else if(c == POUND) {
|
|
|
|
/* <S># */
|
2005-09-12 18:03:27 +05:30
|
|
|
start = optr - 1;
|
|
|
|
state = c;
|
|
|
|
} else if(c == STR || c == CHR) {
|
2005-09-13 22:20:53 +05:30
|
|
|
/* <S>\"|\' */
|
2005-09-12 18:03:27 +05:30
|
|
|
val = NULL;
|
|
|
|
called = S;
|
|
|
|
state = c;
|
2005-09-13 22:20:53 +05:30
|
|
|
} else if(c != BS) {
|
2005-09-12 18:03:27 +05:30
|
|
|
/* <S>[A-Z_a-z0-9] */
|
2005-09-12 22:09:47 +05:30
|
|
|
|
|
|
|
/* trick for fast drop id
|
|
|
|
if key with this first char undefined */
|
2005-09-13 22:20:53 +05:30
|
|
|
if(first_chars[c] == 0) {
|
2005-09-12 22:09:47 +05:30
|
|
|
/* skip <S>[A-Z_a-z0-9]+ */
|
2005-09-16 19:27:33 +05:30
|
|
|
do getc1(); while(isalnums[c]);
|
2005-09-12 22:09:47 +05:30
|
|
|
} else {
|
|
|
|
id_len = 0;
|
|
|
|
do {
|
2005-09-12 18:03:27 +05:30
|
|
|
/* <S>[A-Z_a-z0-9]+ */
|
|
|
|
put_id(c);
|
|
|
|
getc1();
|
2005-09-16 19:27:33 +05:30
|
|
|
} while(isalnums[c]);
|
2005-10-03 15:38:46 +05:30
|
|
|
check_key(key_top, id, id_len);
|
2005-09-12 22:09:47 +05:30
|
|
|
}
|
2005-09-12 18:03:27 +05:30
|
|
|
} else {
|
2005-09-13 22:20:53 +05:30
|
|
|
/* <S>\\ */
|
|
|
|
prev_state = c;
|
2005-09-12 18:03:27 +05:30
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if(state == REM) {
|
|
|
|
for(;;) {
|
|
|
|
/* <REM>[^*]+ */
|
|
|
|
while(c != '*') {
|
|
|
|
if(c == '\n') {
|
|
|
|
/* <REM>\n */
|
|
|
|
if(called != S)
|
|
|
|
yy_error_d("unexpected newline");
|
|
|
|
line++;
|
2005-09-13 22:20:53 +05:30
|
|
|
} else if(c == L_EOF)
|
2005-09-12 18:03:27 +05:30
|
|
|
yy_error_d("unexpected EOF");
|
|
|
|
getc1();
|
|
|
|
}
|
|
|
|
/* <REM>[*] */
|
|
|
|
getc1();
|
2005-09-13 22:20:53 +05:30
|
|
|
if(c == REM) {
|
2005-09-12 18:03:27 +05:30
|
|
|
/* <REM>[*][/] */
|
|
|
|
state = called;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if(state == STR || state == CHR) {
|
|
|
|
for(;;) {
|
|
|
|
/* <STR,CHR>\n|<<EOF>> */
|
2005-09-13 22:20:53 +05:30
|
|
|
if(c == '\n' || c == L_EOF)
|
2005-09-14 20:39:06 +05:30
|
|
|
yy_error_d("unterminated");
|
2005-09-13 22:20:53 +05:30
|
|
|
if(c == BS) {
|
2005-09-12 18:03:27 +05:30
|
|
|
/* <STR,CHR>\\ */
|
|
|
|
getc1();
|
2005-09-13 22:20:53 +05:30
|
|
|
if(c != BS && c != '\n' && c != state) {
|
2005-09-12 18:03:27 +05:30
|
|
|
/* another usage \ in str or char */
|
2005-09-13 22:20:53 +05:30
|
|
|
if(c == L_EOF)
|
2005-09-12 18:03:27 +05:30
|
|
|
yy_error_d("unexpected EOF");
|
|
|
|
if(val)
|
|
|
|
put_id(c);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* <STR,CHR>\\[\\\n] or <STR>\\\" or <CHR>\\\' */
|
|
|
|
/* eat 2 char */
|
|
|
|
if(c == '\n')
|
|
|
|
line++;
|
|
|
|
else if(val)
|
|
|
|
put_id(c);
|
|
|
|
} else if(c == state) {
|
|
|
|
/* <STR>\" or <CHR>\' */
|
2005-10-03 15:38:46 +05:30
|
|
|
if(called == LI) {
|
|
|
|
/* store "include.h" */
|
|
|
|
parse_inc(id, fname, id_len);
|
|
|
|
} else if(called == DV) {
|
|
|
|
put_id(c); /* config mode #define KEY "VAL"<- */
|
|
|
|
put_id(0);
|
|
|
|
parse_conf_opt(id, val, (optr - start), opt_len);
|
|
|
|
}
|
|
|
|
state = S;
|
2005-09-12 18:03:27 +05:30
|
|
|
break;
|
|
|
|
} else if(val)
|
|
|
|
put_id(c);
|
|
|
|
/* <STR,CHR>. */
|
|
|
|
getc1();
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* begin preprocessor states */
|
2005-09-13 22:20:53 +05:30
|
|
|
if(c == L_EOF)
|
2005-09-12 18:03:27 +05:30
|
|
|
yy_error_d("unexpected EOF");
|
2005-09-13 22:20:53 +05:30
|
|
|
if(c == REM) {
|
2005-09-12 18:03:27 +05:30
|
|
|
/* <#.*>/ */
|
|
|
|
getc1();
|
2005-09-13 22:20:53 +05:30
|
|
|
if(c == REM)
|
2005-09-14 20:39:06 +05:30
|
|
|
yy_error_d("detected // in preprocessor line");
|
2005-09-12 18:03:27 +05:30
|
|
|
if(c == '*') {
|
|
|
|
/* <#.*>[/][*] */
|
|
|
|
called = state;
|
|
|
|
state = REM;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* hmm, #.*[/] */
|
|
|
|
yy_error_d("strange preprocessor line");
|
|
|
|
}
|
2005-09-13 22:20:53 +05:30
|
|
|
if(state == POUND) {
|
2005-09-14 20:39:06 +05:30
|
|
|
/* tricks */
|
|
|
|
static const char * const preproc[] = {
|
2005-10-03 15:38:46 +05:30
|
|
|
/* 0-3 */
|
|
|
|
"", "", "", "",
|
|
|
|
/* 4 */ /* 5 */ /* 6 */
|
|
|
|
"ndef", "efine", "nclude",
|
2005-09-14 20:39:06 +05:30
|
|
|
};
|
|
|
|
size_t diu = first_chars_diu[c]; /* strlen and preproc ptr */
|
2005-10-03 15:38:46 +05:30
|
|
|
const unsigned char *p = optr;
|
2005-09-12 18:03:27 +05:30
|
|
|
|
2005-09-16 19:27:33 +05:30
|
|
|
while(isalnums[c]) getc1();
|
2005-09-14 20:39:06 +05:30
|
|
|
/* have str begined with c, readed == strlen key and compared */
|
2005-09-16 19:27:33 +05:30
|
|
|
if(diu != S && diu == (optr-p-1) && !memcmp(p, preproc[diu], diu)) {
|
2005-10-03 15:38:46 +05:30
|
|
|
state = p[-1];
|
2005-09-12 18:03:27 +05:30
|
|
|
id_len = 0; /* common for save */
|
2005-09-14 20:39:06 +05:30
|
|
|
} else {
|
|
|
|
state = S;
|
2005-09-13 22:20:53 +05:30
|
|
|
}
|
|
|
|
ungetc1();
|
|
|
|
continue;
|
2005-09-12 18:03:27 +05:30
|
|
|
}
|
|
|
|
if(state == I) {
|
|
|
|
if(c == STR) {
|
|
|
|
/* <I>\" */
|
|
|
|
val = id;
|
|
|
|
called = LI;
|
2005-09-13 22:20:53 +05:30
|
|
|
state = STR;
|
|
|
|
} else {
|
|
|
|
/* another (may be wrong) #include ... */
|
|
|
|
ungetc1();
|
|
|
|
state = S;
|
2005-09-12 18:03:27 +05:30
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if(state == D || state == U) {
|
2005-09-14 20:39:06 +05:30
|
|
|
if(mode == SOURCES_MODE) {
|
2005-09-13 22:20:53 +05:30
|
|
|
/* ignore depend with #define or #undef KEY */
|
2005-09-16 19:27:33 +05:30
|
|
|
while(isalnums[c])
|
2005-09-13 22:20:53 +05:30
|
|
|
getc1();
|
2005-09-12 18:03:27 +05:30
|
|
|
state = S;
|
|
|
|
} else {
|
2005-09-13 22:20:53 +05:30
|
|
|
/* save KEY from #"define"|"undef" ... */
|
2005-09-16 19:27:33 +05:30
|
|
|
while(isalnums[c]) {
|
2005-09-13 22:20:53 +05:30
|
|
|
put_id(c);
|
|
|
|
getc1();
|
|
|
|
}
|
2005-09-12 18:03:27 +05:30
|
|
|
if(!id_len)
|
2005-09-14 20:39:06 +05:30
|
|
|
yy_error_d("expected identifier");
|
2005-09-12 18:03:27 +05:30
|
|
|
if(state == U) {
|
2005-10-03 15:38:46 +05:30
|
|
|
parse_conf_opt(id, NULL, (optr - start), id_len);
|
2005-09-12 18:03:27 +05:30
|
|
|
state = S;
|
|
|
|
} else {
|
|
|
|
/* D -> DK */
|
2005-10-03 15:38:46 +05:30
|
|
|
opt_len = id_len;
|
2005-09-12 18:03:27 +05:30
|
|
|
state = DK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ungetc1();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if(state == DK) {
|
2005-09-13 22:20:53 +05:30
|
|
|
/* #define KEY[ ] (config mode) */
|
2005-09-12 18:03:27 +05:30
|
|
|
val = id + id_len;
|
|
|
|
if(c == STR || c == CHR) {
|
|
|
|
/* define KEY "... or define KEY '... */
|
|
|
|
put_id(c);
|
|
|
|
called = DV;
|
|
|
|
state = c;
|
|
|
|
continue;
|
|
|
|
}
|
2005-09-16 19:27:33 +05:30
|
|
|
while(isalnums[c]) {
|
2005-09-13 22:20:53 +05:30
|
|
|
/* VALUE */
|
2005-09-12 18:03:27 +05:30
|
|
|
put_id(c);
|
|
|
|
getc1();
|
|
|
|
}
|
2005-10-03 15:38:46 +05:30
|
|
|
put_id(0);
|
|
|
|
parse_conf_opt(id, val, (optr - start), opt_len);
|
|
|
|
state = S;
|
2005-09-12 18:03:27 +05:30
|
|
|
ungetc1();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void show_usage(void) __attribute__ ((noreturn));
|
|
|
|
static void show_usage(void)
|
|
|
|
{
|
2005-09-14 20:39:06 +05:30
|
|
|
bb_error_d("%s\n%s\n", bb_mkdep_terse_options, bb_mkdep_full_options);
|
2005-09-12 18:03:27 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
static const char *kp;
|
2005-09-13 22:20:53 +05:30
|
|
|
static size_t kp_len;
|
2005-09-12 18:03:27 +05:30
|
|
|
static llist_t *Iop;
|
|
|
|
static bb_key_t *Ifound;
|
|
|
|
static int noiwarning;
|
|
|
|
|
2005-10-03 15:38:46 +05:30
|
|
|
static bb_key_t *check_key(bb_key_t *k, const char *nk, size_t key_sz)
|
2005-09-12 18:03:27 +05:30
|
|
|
{
|
2005-09-13 22:20:53 +05:30
|
|
|
bb_key_t *cur;
|
2005-09-12 18:03:27 +05:30
|
|
|
|
2005-09-13 22:20:53 +05:30
|
|
|
for(cur = k; cur; cur = cur->next) {
|
2005-10-03 15:38:46 +05:30
|
|
|
if(key_sz == cur->key_sz && memcmp(cur->keyname, nk, key_sz) == 0) {
|
|
|
|
cur->checked = cur->stored_path;
|
2005-09-13 22:20:53 +05:30
|
|
|
return cur;
|
2005-09-12 18:03:27 +05:30
|
|
|
}
|
2005-09-13 22:20:53 +05:30
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2005-10-03 15:38:46 +05:30
|
|
|
static bb_key_t *make_new_key(bb_key_t *k, const char *nk, size_t key_sz)
|
2005-09-13 22:20:53 +05:30
|
|
|
{
|
|
|
|
bb_key_t *cur;
|
|
|
|
|
2005-10-03 15:38:46 +05:30
|
|
|
cur = xmalloc(sizeof(bb_key_t) + key_sz + 1);
|
|
|
|
cur->keyname = memcpy(cur + 1, nk, key_sz);
|
|
|
|
cur->keyname[key_sz] = '\0';
|
|
|
|
cur->key_sz = key_sz;
|
|
|
|
cur->checked = NULL;
|
2005-09-12 18:03:27 +05:30
|
|
|
cur->next = k;
|
|
|
|
return cur;
|
|
|
|
}
|
|
|
|
|
2005-09-13 22:20:53 +05:30
|
|
|
static inline char *store_include_fullpath(char *p_i, bb_key_t *li)
|
2005-09-12 18:03:27 +05:30
|
|
|
{
|
2005-09-13 22:20:53 +05:30
|
|
|
char *ok;
|
2005-09-12 18:03:27 +05:30
|
|
|
|
2005-09-14 20:39:06 +05:30
|
|
|
if(access(p_i, F_OK) == 0) {
|
2005-09-13 22:20:53 +05:30
|
|
|
ok = li->stored_path = bb_simplify_path(p_i);
|
2005-10-03 15:38:46 +05:30
|
|
|
li->checked = ok;
|
2005-09-13 22:20:53 +05:30
|
|
|
} else {
|
|
|
|
ok = NULL;
|
2005-09-12 18:03:27 +05:30
|
|
|
}
|
|
|
|
free(p_i);
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
2005-10-03 15:38:46 +05:30
|
|
|
static void parse_inc(const char *include, const char *fname, size_t key_sz)
|
2005-09-12 18:03:27 +05:30
|
|
|
{
|
|
|
|
bb_key_t *li;
|
|
|
|
char *p_i;
|
|
|
|
llist_t *lo;
|
|
|
|
|
2005-10-03 15:38:46 +05:30
|
|
|
li = check_key(Ifound, include, key_sz);
|
2005-09-13 22:20:53 +05:30
|
|
|
if(li)
|
2005-09-12 18:03:27 +05:30
|
|
|
return;
|
2005-10-03 15:38:46 +05:30
|
|
|
Ifound = li = make_new_key(Ifound, include, key_sz);
|
|
|
|
include = li->keyname;
|
2005-09-12 18:03:27 +05:30
|
|
|
if(include[0] != '/') {
|
|
|
|
/* relative */
|
|
|
|
int w;
|
|
|
|
|
2005-10-03 15:38:46 +05:30
|
|
|
p_i = strrchr(fname, '/'); /* fname have absolute pathname */
|
|
|
|
w = (p_i-fname);
|
|
|
|
/* find from current directory of source file */
|
|
|
|
p_i = bb_asprint("%.*s/%s", w, fname, include);
|
2005-09-12 18:03:27 +05:30
|
|
|
if(store_include_fullpath(p_i, li))
|
|
|
|
return;
|
2005-10-03 15:38:46 +05:30
|
|
|
/* find from "-I include" specified directories */
|
2005-09-14 20:39:06 +05:30
|
|
|
for(lo = Iop; lo; lo = lo->link) {
|
|
|
|
p_i = bb_asprint("%s/%s", lo->data, include);
|
|
|
|
if(store_include_fullpath(p_i, li))
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* absolute include pathname */
|
|
|
|
if(access(include, F_OK) == 0) {
|
2005-10-03 15:38:46 +05:30
|
|
|
li->checked = li->stored_path = bb_xstrdup(include);
|
2005-09-12 18:03:27 +05:30
|
|
|
return;
|
2005-09-14 20:39:06 +05:30
|
|
|
}
|
2005-09-12 18:03:27 +05:30
|
|
|
}
|
|
|
|
li->stored_path = NULL;
|
|
|
|
if(noiwarning)
|
|
|
|
fprintf(stderr, "%s: Warning: #include \"%s\" not found in specified paths\n", fname, include);
|
|
|
|
}
|
|
|
|
|
2005-10-03 15:38:46 +05:30
|
|
|
static void parse_conf_opt(const char *opt, const char *val,
|
|
|
|
size_t recordsz, size_t key_sz)
|
2005-09-12 18:03:27 +05:30
|
|
|
{
|
2005-09-13 22:20:53 +05:30
|
|
|
bb_key_t *cur;
|
2005-10-03 15:38:46 +05:30
|
|
|
char *k;
|
2005-09-14 20:39:06 +05:30
|
|
|
char *s, *p;
|
|
|
|
struct stat st;
|
|
|
|
int fd;
|
|
|
|
int cmp_ok = 0;
|
|
|
|
static char *record_buf;
|
|
|
|
static size_t r_sz;
|
|
|
|
ssize_t rw_ret;
|
2005-09-12 18:03:27 +05:30
|
|
|
|
2005-10-03 15:38:46 +05:30
|
|
|
cur = check_key(key_top, opt, key_sz);
|
2005-09-14 20:39:06 +05:30
|
|
|
if(cur != NULL) {
|
|
|
|
/* present already */
|
2005-10-03 15:38:46 +05:30
|
|
|
cur->checked = NULL; /* store only */
|
2005-09-14 20:39:06 +05:30
|
|
|
if(cur->value == NULL && val == NULL)
|
|
|
|
return;
|
2005-10-03 15:38:46 +05:30
|
|
|
if(cur->value != NULL && val != NULL && !strcmp(cur->value, val))
|
2005-09-14 20:39:06 +05:30
|
|
|
return;
|
2005-10-03 15:38:46 +05:30
|
|
|
k = cur->keyname;
|
|
|
|
fprintf(stderr, "Warning: redefined %s\n", k);
|
2005-09-16 19:27:33 +05:30
|
|
|
} else {
|
2005-10-03 15:38:46 +05:30
|
|
|
key_top = cur = make_new_key(key_top, opt, key_sz);
|
|
|
|
k = cur->keyname;
|
2005-09-14 20:39:06 +05:30
|
|
|
}
|
2005-09-16 19:27:33 +05:30
|
|
|
/* do generate record */
|
2005-09-14 20:39:06 +05:30
|
|
|
recordsz += 2; /* \n\0 */
|
2005-10-03 15:38:46 +05:30
|
|
|
if(recordsz > r_sz)
|
|
|
|
record_buf = xrealloc(record_buf, (r_sz = recordsz) * 2);
|
2005-09-14 20:39:06 +05:30
|
|
|
s = record_buf;
|
|
|
|
/* may be short count " " */
|
|
|
|
if(val) {
|
|
|
|
if(*val == '\0') {
|
|
|
|
cur->value = "";
|
2005-10-03 15:38:46 +05:30
|
|
|
recordsz = sprintf(s, "#define %s\n", k);
|
2005-09-13 22:20:53 +05:30
|
|
|
} else {
|
2005-09-14 20:39:06 +05:30
|
|
|
cur->value = bb_xstrdup(val);
|
2005-10-03 15:38:46 +05:30
|
|
|
recordsz = sprintf(s, "#define %s %s\n", k, val);
|
2005-09-12 18:03:27 +05:30
|
|
|
}
|
2005-09-14 20:39:06 +05:30
|
|
|
} else {
|
|
|
|
cur->value = NULL;
|
2005-10-03 15:38:46 +05:30
|
|
|
recordsz = sprintf(s, "#undef %s\n", k);
|
2005-09-14 20:39:06 +05:30
|
|
|
}
|
|
|
|
/* size_t -> ssize_t :( */
|
|
|
|
rw_ret = (ssize_t)recordsz;
|
|
|
|
/* trick, save first char KEY for do fast identify id */
|
2005-10-03 15:38:46 +05:30
|
|
|
first_chars[(int)*k] = *k;
|
2005-09-14 20:39:06 +05:30
|
|
|
|
2005-10-03 15:38:46 +05:30
|
|
|
cur->stored_path = k = bb_asprint("%s/%s.h", kp, k);
|
2005-09-14 20:39:06 +05:30
|
|
|
/* key converting [A-Z_] -> [a-z/] */
|
2005-10-03 15:38:46 +05:30
|
|
|
for(p = k + kp_len + 1; *p; p++) {
|
2005-09-14 20:39:06 +05:30
|
|
|
if(*p >= 'A' && *p <= 'Z')
|
|
|
|
*p = *p - 'A' + 'a';
|
2005-09-16 19:27:33 +05:30
|
|
|
else if(*p == '_' && p[1] > '9') /* do not change A_1 to A/1 */
|
2005-09-14 20:39:06 +05:30
|
|
|
*p = '/';
|
|
|
|
}
|
2005-09-16 19:27:33 +05:30
|
|
|
/* check kp/key.h if present after previous usage */
|
2005-10-03 15:38:46 +05:30
|
|
|
if(stat(k, &st)) {
|
|
|
|
for(p = k + kp_len + 1; *p; p++) {
|
2005-09-14 20:39:06 +05:30
|
|
|
/* Auto-create directories. */
|
|
|
|
if (*p == '/') {
|
|
|
|
*p = '\0';
|
2005-10-03 15:38:46 +05:30
|
|
|
if (access(k, F_OK) != 0 && mkdir(k, 0755) != 0)
|
|
|
|
bb_error_d("mkdir(%s): %m", k);
|
2005-09-14 20:39:06 +05:30
|
|
|
*p = '/';
|
2005-09-12 18:03:27 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2005-09-14 20:39:06 +05:30
|
|
|
/* found */
|
|
|
|
if(st.st_size == (off_t)recordsz) {
|
2005-10-03 15:38:46 +05:30
|
|
|
char *r_cmp = s + recordsz;
|
|
|
|
|
|
|
|
fd = open(k, O_RDONLY);
|
2005-09-14 20:39:06 +05:30
|
|
|
if(fd < 0 || read(fd, r_cmp, recordsz) < rw_ret)
|
2005-10-03 15:38:46 +05:30
|
|
|
bb_error_d("%s: %m", k);
|
2005-09-14 20:39:06 +05:30
|
|
|
close(fd);
|
|
|
|
cmp_ok = memcmp(s, r_cmp, recordsz) == 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(!cmp_ok) {
|
2005-10-03 15:38:46 +05:30
|
|
|
fd = open(k, O_WRONLY|O_CREAT|O_TRUNC, 0644);
|
2005-09-14 20:39:06 +05:30
|
|
|
if(fd < 0 || write(fd, s, recordsz) < rw_ret)
|
2005-10-03 15:38:46 +05:30
|
|
|
bb_error_d("%s: %m", k);
|
2005-09-14 20:39:06 +05:30
|
|
|
close(fd);
|
2005-09-12 18:03:27 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-09-13 22:20:53 +05:30
|
|
|
static int show_dep(int first, bb_key_t *k, const char *name)
|
2005-09-12 18:03:27 +05:30
|
|
|
{
|
|
|
|
bb_key_t *cur;
|
|
|
|
|
|
|
|
for(cur = k; cur; cur = cur->next) {
|
2005-10-03 15:38:46 +05:30
|
|
|
if(cur->checked) {
|
2005-09-12 18:03:27 +05:30
|
|
|
if(first) {
|
2005-09-13 22:20:53 +05:30
|
|
|
printf("\n%s:", name);
|
2005-09-12 18:03:27 +05:30
|
|
|
first = 0;
|
|
|
|
} else {
|
|
|
|
printf(" \\\n ");
|
|
|
|
}
|
2005-10-03 15:38:46 +05:30
|
|
|
printf(" %s", cur->checked);
|
2005-09-12 18:03:27 +05:30
|
|
|
}
|
2005-10-03 15:38:46 +05:30
|
|
|
cur->checked = NULL;
|
2005-09-12 18:03:27 +05:30
|
|
|
}
|
|
|
|
return first;
|
|
|
|
}
|
|
|
|
|
2005-10-06 20:17:16 +05:30
|
|
|
static char *pwd;
|
|
|
|
static char *replace;
|
|
|
|
|
2005-09-13 22:20:53 +05:30
|
|
|
static struct stat st_kp;
|
2005-09-14 20:39:06 +05:30
|
|
|
static int dontgenerate_dep;
|
2005-09-12 18:03:27 +05:30
|
|
|
|
2005-09-13 22:20:53 +05:30
|
|
|
static char *
|
2005-09-14 20:39:06 +05:30
|
|
|
parse_chd(const char *fe, const char *p, size_t dirlen)
|
2005-09-12 18:03:27 +05:30
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
char *fp;
|
2005-09-13 22:20:53 +05:30
|
|
|
size_t df_sz;
|
2005-09-14 20:39:06 +05:30
|
|
|
static char *dir_and_entry;
|
2005-09-13 22:20:53 +05:30
|
|
|
static size_t dir_and_entry_sz;
|
2005-09-12 18:03:27 +05:30
|
|
|
|
2005-09-13 22:20:53 +05:30
|
|
|
df_sz = dirlen + strlen(fe) + 2; /* dir/file\0 */
|
|
|
|
if(df_sz > dir_and_entry_sz)
|
|
|
|
dir_and_entry = xrealloc(dir_and_entry, dir_and_entry_sz = df_sz);
|
|
|
|
fp = dir_and_entry;
|
|
|
|
sprintf(fp, "%s/%s", p, fe);
|
|
|
|
|
2005-09-12 18:03:27 +05:30
|
|
|
if(stat(fp, &st)) {
|
2005-10-03 15:38:46 +05:30
|
|
|
fprintf(stderr, "Warning: stat(%s): %m\n", fp);
|
2005-09-12 18:03:27 +05:30
|
|
|
return NULL;
|
|
|
|
}
|
2005-09-12 22:09:47 +05:30
|
|
|
if(S_ISREG(st.st_mode)) {
|
2005-09-14 20:39:06 +05:30
|
|
|
llist_t *cfl;
|
|
|
|
char *e = fp + df_sz - 3;
|
|
|
|
|
|
|
|
if(*e++ != '.' || (*e != 'c' && *e != 'h')) {
|
2005-09-12 22:09:47 +05:30
|
|
|
/* direntry is regular file, but is not *.[ch] */
|
2005-09-12 18:03:27 +05:30
|
|
|
return NULL;
|
|
|
|
}
|
2005-09-14 20:39:06 +05:30
|
|
|
for(cfl = configs; cfl; cfl = cfl->link) {
|
|
|
|
struct stat *config = (struct stat *)cfl->data;
|
|
|
|
|
|
|
|
if (st.st_dev == config->st_dev && st.st_ino == config->st_ino) {
|
|
|
|
/* skip already parsed configs.h */
|
2005-09-12 22:09:47 +05:30
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
2005-09-14 20:39:06 +05:30
|
|
|
/* direntry is *.[ch] regular file and is not configs */
|
|
|
|
if(!dontgenerate_dep) {
|
|
|
|
int first;
|
|
|
|
|
|
|
|
c_lex(fp, st.st_size);
|
|
|
|
if(*e == 'c') {
|
|
|
|
/* *.c -> *.o */
|
|
|
|
*e = 'o';
|
2005-10-06 20:17:16 +05:30
|
|
|
if(replace) {
|
|
|
|
/* /src_dir/path/file.o to path/file.o */
|
|
|
|
e = fp + strlen(replace);
|
|
|
|
while(*e == '/')
|
|
|
|
e++;
|
|
|
|
/* path/file.o to pwd/path/file.o */
|
|
|
|
e = fp = bb_asprint("%s/%s", pwd, e);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
e = NULL;
|
2005-09-14 20:39:06 +05:30
|
|
|
}
|
|
|
|
first = show_dep(1, Ifound, fp);
|
|
|
|
first = show_dep(first, key_top, fp);
|
|
|
|
if(first == 0)
|
|
|
|
putchar('\n');
|
2005-10-06 20:17:16 +05:30
|
|
|
if(replace && e)
|
|
|
|
free(e);
|
2005-09-12 18:03:27 +05:30
|
|
|
}
|
2005-09-14 20:39:06 +05:30
|
|
|
return NULL;
|
|
|
|
} else if(S_ISDIR(st.st_mode)) {
|
|
|
|
if (st.st_dev == st_kp.st_dev && st.st_ino == st_kp.st_ino)
|
|
|
|
return NULL; /* drop scan kp/ directory */
|
|
|
|
/* direntry is directory. buff is returned, begin of zero allocate */
|
|
|
|
dir_and_entry = NULL;
|
|
|
|
dir_and_entry_sz = 0;
|
|
|
|
return fp;
|
2005-09-12 18:03:27 +05:30
|
|
|
}
|
2005-09-14 20:39:06 +05:30
|
|
|
/* hmm, direntry is device! */
|
2005-09-12 18:03:27 +05:30
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2005-09-14 20:39:06 +05:30
|
|
|
/* from libbb but inline for fast */
|
|
|
|
static inline llist_t *llist_add_to(llist_t *old_head, char *new_item)
|
|
|
|
{
|
|
|
|
llist_t *new_head;
|
|
|
|
|
|
|
|
new_head = xmalloc(sizeof(llist_t));
|
|
|
|
new_head->data = new_item;
|
|
|
|
new_head->link = old_head;
|
|
|
|
|
|
|
|
return(new_head);
|
|
|
|
}
|
|
|
|
|
2005-10-03 15:38:46 +05:30
|
|
|
static void scan_dir_find_ch_files(const char *p)
|
2005-09-12 18:03:27 +05:30
|
|
|
{
|
|
|
|
llist_t *dirs;
|
|
|
|
llist_t *d_add;
|
|
|
|
llist_t *d;
|
|
|
|
struct dirent *de;
|
|
|
|
DIR *dir;
|
2005-09-13 22:20:53 +05:30
|
|
|
size_t dirlen;
|
2005-09-12 18:03:27 +05:30
|
|
|
|
2005-10-03 15:38:46 +05:30
|
|
|
dirs = llist_add_to(NULL, bb_simplify_path(p));
|
2005-10-06 20:17:16 +05:30
|
|
|
if(strcmp(dirs->data, pwd))
|
|
|
|
replace = bb_xstrdup(dirs->data);
|
|
|
|
else
|
|
|
|
replace = NULL;
|
2005-09-12 18:03:27 +05:30
|
|
|
/* emulate recursive */
|
|
|
|
while(dirs) {
|
|
|
|
d_add = NULL;
|
|
|
|
while(dirs) {
|
|
|
|
dir = opendir(dirs->data);
|
|
|
|
if (dir == NULL)
|
2005-10-03 15:38:46 +05:30
|
|
|
fprintf(stderr, "Warning: opendir(%s): %m\n", dirs->data);
|
2005-09-13 22:20:53 +05:30
|
|
|
dirlen = strlen(dirs->data);
|
2005-09-12 18:03:27 +05:30
|
|
|
while ((de = readdir(dir)) != NULL) {
|
2005-10-03 15:38:46 +05:30
|
|
|
char *found_dir;
|
2005-09-13 22:20:53 +05:30
|
|
|
|
2005-10-03 15:38:46 +05:30
|
|
|
if (de->d_name[0] == '.')
|
|
|
|
continue;
|
|
|
|
found_dir = parse_chd(de->d_name, dirs->data, dirlen);
|
2005-09-13 22:20:53 +05:30
|
|
|
if(found_dir)
|
|
|
|
d_add = llist_add_to(d_add, found_dir);
|
2005-09-12 18:03:27 +05:30
|
|
|
}
|
|
|
|
closedir(dir);
|
2005-10-03 15:38:46 +05:30
|
|
|
free(dirs->data);
|
2005-09-12 18:03:27 +05:30
|
|
|
d = dirs;
|
|
|
|
dirs = dirs->link;
|
|
|
|
free(d);
|
|
|
|
}
|
|
|
|
dirs = d_add;
|
|
|
|
}
|
2005-10-06 20:17:16 +05:30
|
|
|
free(replace);
|
|
|
|
replace = NULL;
|
2005-09-12 18:03:27 +05:30
|
|
|
}
|
|
|
|
|
2005-09-13 22:20:53 +05:30
|
|
|
|
2005-09-12 18:03:27 +05:30
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
char *s;
|
|
|
|
int i;
|
|
|
|
llist_t *fl;
|
|
|
|
|
2005-09-13 22:20:53 +05:30
|
|
|
{
|
|
|
|
/* for bb_simplify_path */
|
|
|
|
/* libbb xgetcwd(), this program have not chdir() */
|
|
|
|
unsigned path_max = 512;
|
|
|
|
|
|
|
|
s = xmalloc (path_max);
|
|
|
|
#define PATH_INCR 32
|
|
|
|
while (getcwd (s, path_max) == NULL) {
|
|
|
|
if(errno != ERANGE)
|
|
|
|
bb_error_d("getcwd: %m");
|
2005-09-14 20:39:06 +05:30
|
|
|
s = xrealloc (s, path_max += PATH_INCR);
|
2005-09-13 22:20:53 +05:30
|
|
|
}
|
|
|
|
pwd = s;
|
|
|
|
}
|
|
|
|
|
2005-09-12 18:03:27 +05:30
|
|
|
while ((i = getopt(argc, argv, "I:c:dk:w")) > 0) {
|
|
|
|
switch(i) {
|
|
|
|
case 'I':
|
|
|
|
Iop = llist_add_to(Iop, optarg);
|
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
s = bb_simplify_path(optarg);
|
|
|
|
configs = llist_add_to(configs, s);
|
|
|
|
break;
|
|
|
|
case 'd':
|
2005-09-14 20:39:06 +05:30
|
|
|
dontgenerate_dep = 1;
|
2005-09-12 18:03:27 +05:30
|
|
|
break;
|
|
|
|
case 'k':
|
|
|
|
if(kp)
|
|
|
|
bb_error_d("Hmm, why multiple -k?");
|
|
|
|
kp = bb_simplify_path(optarg);
|
|
|
|
break;
|
|
|
|
case 'w':
|
|
|
|
noiwarning = 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
show_usage();
|
|
|
|
}
|
|
|
|
}
|
2005-09-14 20:39:06 +05:30
|
|
|
/* default kp */
|
2005-09-12 18:03:27 +05:30
|
|
|
if(kp == NULL)
|
|
|
|
kp = bb_simplify_path(INCLUDE_CONFIG_PATH);
|
2005-09-14 20:39:06 +05:30
|
|
|
/* globals initialize */
|
|
|
|
kp_len = strlen(kp);
|
|
|
|
if(stat(kp, &st_kp))
|
|
|
|
bb_error_d("stat(%s): %m", kp);
|
|
|
|
if(!S_ISDIR(st_kp.st_mode))
|
|
|
|
bb_error_d("%s is not directory", kp);
|
|
|
|
/* defaults */
|
2005-09-12 18:03:27 +05:30
|
|
|
if(Iop == NULL)
|
|
|
|
Iop = llist_add_to(Iop, LOCAL_INCLUDE_PATH);
|
|
|
|
if(configs == NULL) {
|
|
|
|
s = bb_simplify_path(INCLUDE_CONFIG_KEYS_PATH);
|
|
|
|
configs = llist_add_to(configs, s);
|
|
|
|
}
|
2005-09-13 22:20:53 +05:30
|
|
|
/* for c_lex */
|
|
|
|
pagesizem1 = getpagesize() - 1;
|
|
|
|
id_s = xmalloc(mema_id);
|
|
|
|
for(i = 0; i < 256; i++) {
|
2005-09-16 19:27:33 +05:30
|
|
|
if(ISALNUM(i))
|
|
|
|
isalnums[i] = i;
|
2005-09-13 22:20:53 +05:30
|
|
|
/* set unparsed chars for speed up of parser */
|
2005-09-16 19:27:33 +05:30
|
|
|
else if(i != CHR && i != STR && i != POUND && i != REM && i != BS)
|
2005-09-13 22:20:53 +05:30
|
|
|
first_chars[i] = ANY;
|
|
|
|
}
|
|
|
|
first_chars[i] = '-'; /* L_EOF */
|
2005-09-14 20:39:06 +05:30
|
|
|
/* trick for fast find "define", "include", "undef" */
|
2005-10-03 15:38:46 +05:30
|
|
|
first_chars_diu[(int)'d'] = (char)5; /* strlen("define") - 1; */
|
|
|
|
first_chars_diu[(int)'i'] = (char)6; /* strlen("include") - 1; */
|
|
|
|
first_chars_diu[(int)'u'] = (char)4; /* strlen("undef") - 1; */
|
2005-09-13 22:20:53 +05:30
|
|
|
|
2005-09-14 20:39:06 +05:30
|
|
|
/* parse configs */
|
|
|
|
for(fl = configs; fl; fl = fl->link) {
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
if(stat(fl->data, &st))
|
|
|
|
bb_error_d("stat(%s): %m", fl->data);
|
|
|
|
c_lex(fl->data, st.st_size);
|
|
|
|
/* trick for fast comparing found files with configs */
|
|
|
|
fl->data = xrealloc(fl->data, sizeof(struct stat));
|
|
|
|
memcpy(fl->data, &st, sizeof(struct stat));
|
|
|
|
}
|
2005-09-13 22:20:53 +05:30
|
|
|
|
2005-09-14 20:39:06 +05:30
|
|
|
/* main loop */
|
|
|
|
mode = SOURCES_MODE;
|
2005-09-13 22:20:53 +05:30
|
|
|
argv += optind;
|
|
|
|
if(*argv) {
|
|
|
|
while(*argv)
|
|
|
|
scan_dir_find_ch_files(*argv++);
|
|
|
|
} else {
|
|
|
|
scan_dir_find_ch_files(".");
|
|
|
|
}
|
2005-09-12 18:03:27 +05:30
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-09-14 20:39:06 +05:30
|
|
|
/* partial and simplified libbb routine */
|
2005-09-12 22:09:47 +05:30
|
|
|
static void bb_error_d(const char *s, ...)
|
2005-09-12 18:03:27 +05:30
|
|
|
{
|
|
|
|
va_list p;
|
|
|
|
|
|
|
|
va_start(p, s);
|
|
|
|
vfprintf(stderr, s, p);
|
|
|
|
va_end(p);
|
|
|
|
putc('\n', stderr);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2005-09-12 22:09:47 +05:30
|
|
|
static char *bb_asprint(const char *format, ...)
|
2005-09-12 18:03:27 +05:30
|
|
|
{
|
|
|
|
va_list p;
|
|
|
|
int r;
|
|
|
|
char *out;
|
|
|
|
|
|
|
|
va_start(p, format);
|
|
|
|
r = vasprintf(&out, format, p);
|
|
|
|
va_end(p);
|
|
|
|
|
|
|
|
if (r < 0)
|
|
|
|
bb_error_d("bb_asprint: %m");
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2005-09-14 20:39:06 +05:30
|
|
|
/* partial libbb routine as is */
|
|
|
|
static void *xmalloc(size_t size)
|
2005-09-12 18:03:27 +05:30
|
|
|
{
|
2005-09-14 20:39:06 +05:30
|
|
|
void *p = malloc(size);
|
2005-09-12 18:03:27 +05:30
|
|
|
|
2005-09-14 20:39:06 +05:30
|
|
|
if(p == NULL)
|
|
|
|
bb_error_d(msg_enomem);
|
|
|
|
return p;
|
|
|
|
}
|
2005-09-12 18:03:27 +05:30
|
|
|
|
2005-09-14 20:39:06 +05:30
|
|
|
static void *xrealloc(void *p, size_t size) {
|
|
|
|
p = realloc(p, size);
|
|
|
|
if(p == NULL)
|
|
|
|
bb_error_d(msg_enomem);
|
|
|
|
return p;
|
2005-09-12 18:03:27 +05:30
|
|
|
}
|
|
|
|
|
2005-09-12 22:09:47 +05:30
|
|
|
static char *bb_xstrdup(const char *s)
|
2005-09-12 18:03:27 +05:30
|
|
|
{
|
2005-09-12 22:09:47 +05:30
|
|
|
char *r = strdup(s);
|
|
|
|
|
|
|
|
if(r == NULL)
|
2005-09-14 20:39:06 +05:30
|
|
|
bb_error_d(msg_enomem);
|
2005-09-12 22:09:47 +05:30
|
|
|
return r;
|
2005-09-12 18:03:27 +05:30
|
|
|
}
|
|
|
|
|
2005-09-12 22:09:47 +05:30
|
|
|
static char *bb_simplify_path(const char *path)
|
2005-09-12 18:03:27 +05:30
|
|
|
{
|
|
|
|
char *s, *start, *p;
|
|
|
|
|
|
|
|
if (path[0] == '/')
|
2005-09-13 22:20:53 +05:30
|
|
|
start = bb_xstrdup(path);
|
2005-09-12 18:03:27 +05:30
|
|
|
else {
|
2005-09-13 22:20:53 +05:30
|
|
|
/* is not libbb, but this program have not chdir() */
|
|
|
|
start = bb_asprint("%s/%s", pwd, path);
|
2005-09-12 18:03:27 +05:30
|
|
|
}
|
|
|
|
p = s = start;
|
|
|
|
|
|
|
|
do {
|
2005-09-12 22:09:47 +05:30
|
|
|
if (*p == '/') {
|
|
|
|
if (*s == '/') { /* skip duplicate (or initial) slash */
|
|
|
|
continue;
|
|
|
|
} else if (*s == '.') {
|
|
|
|
if (s[1] == '/' || s[1] == 0) { /* remove extra '.' */
|
|
|
|
continue;
|
|
|
|
} else if ((s[1] == '.') && (s[2] == '/' || s[2] == 0)) {
|
|
|
|
++s;
|
|
|
|
if (p > start) {
|
|
|
|
while (*--p != '/'); /* omit previous dir */
|
2005-09-12 18:03:27 +05:30
|
|
|
}
|
2005-09-12 22:09:47 +05:30
|
|
|
continue;
|
|
|
|
}
|
2005-09-12 18:03:27 +05:30
|
|
|
}
|
2005-09-12 22:09:47 +05:30
|
|
|
}
|
|
|
|
*++p = *s;
|
2005-09-12 18:03:27 +05:30
|
|
|
} while (*++s);
|
|
|
|
|
2005-09-12 22:09:47 +05:30
|
|
|
if ((p == start) || (*p != '/')) { /* not a trailing slash */
|
|
|
|
++p; /* so keep last character */
|
2005-09-12 18:03:27 +05:30
|
|
|
}
|
|
|
|
*p = 0;
|
|
|
|
|
|
|
|
return start;
|
|
|
|
}
|