2005-09-12 12:33:27 +00:00
|
|
|
/*
|
2005-10-10 11:35:17 +00:00
|
|
|
* Another fast dependencies generator for Makefiles, Version 3.0
|
2005-09-12 12:33:27 +00:00
|
|
|
*
|
|
|
|
* Copyright (C) 2005 by Vladimir Oleynik <dzo@simtreas.ru>
|
2005-10-10 11:35:17 +00:00
|
|
|
*
|
2005-09-16 13:57:33 +00:00
|
|
|
* mmaping file may be originally by Linus Torvalds.
|
|
|
|
*
|
2005-10-10 11:35:17 +00:00
|
|
|
* bb_simplify_path()
|
|
|
|
* Copyright (C) 2005 Manuel Novoa III <mjn3@codepoet.org>
|
|
|
|
*
|
|
|
|
* xmalloc() bb_xstrdup() bb_error_d():
|
|
|
|
* Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
|
|
|
|
*
|
|
|
|
* llist routine
|
|
|
|
* Copyright (C) 2003 Glenn McGrath
|
|
|
|
* Copyright (C) Vladimir Oleynik <dzo@simtreas.ru>
|
|
|
|
*
|
2005-09-16 13:57:33 +00:00
|
|
|
* (c) 2005 Bernhard Fischer:
|
|
|
|
* - commentary typos,
|
|
|
|
* - move "memory exhausted" into msg_enomem,
|
|
|
|
* - more verbose --help output.
|
2005-09-12 12:33:27 +00:00
|
|
|
*
|
2005-09-14 15:09:06 +00:00
|
|
|
* This program does:
|
2005-09-12 12:33:27 +00:00
|
|
|
* 1) find #define KEY VALUE or #undef KEY from include/config.h
|
2005-10-10 11:35:17 +00:00
|
|
|
* 2) recursive find and scan *.[ch] files, but skips scan of include/config/
|
|
|
|
* 3) find #include "*.h" and KEYs using, if not as #define and #undef
|
|
|
|
* 4) generate dependencies to stdout
|
2005-10-06 14:53:43 +00:00
|
|
|
* pwd/file.o: include/config/key*.h found_include_*.h
|
2005-09-12 12:33:27 +00:00
|
|
|
* path/inc.h: include/config/key*.h found_included_include_*.h
|
2005-10-10 11:35:17 +00:00
|
|
|
* 5) save include/config/key*.h if changed after previous usage
|
2005-09-16 13:57:33 +00:00
|
|
|
* This program does not generate dependencies for #include <...>
|
2005-09-14 15:09:06 +00:00
|
|
|
*/
|
2005-09-12 12:33:27 +00:00
|
|
|
|
|
|
|
#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 15:09:06 +00:00
|
|
|
#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 12:33:27 +00:00
|
|
|
#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-10-10 11:35:17 +00:00
|
|
|
#include <limits.h>
|
2005-09-12 12:33:27 +00:00
|
|
|
|
2005-09-14 15:09:06 +00:00
|
|
|
|
|
|
|
/* partial and simplified libbb routine */
|
2005-09-12 16:39:47 +00:00
|
|
|
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-10-10 11:35:17 +00:00
|
|
|
static char *bb_simplify_path(const char *path);
|
2005-09-12 12:33:27 +00:00
|
|
|
|
|
|
|
/* stolen from libbb as is */
|
|
|
|
typedef struct llist_s {
|
|
|
|
char *data;
|
|
|
|
struct llist_s *link;
|
|
|
|
} llist_t;
|
2005-10-10 11:35:17 +00:00
|
|
|
|
2005-09-14 15:09:06 +00:00
|
|
|
static const char msg_enomem[] = "memory exhausted";
|
2005-09-12 12:33:27 +00:00
|
|
|
|
2005-10-10 11:35:17 +00:00
|
|
|
/* inline realization for fast works */
|
|
|
|
static inline void *xmalloc(size_t size)
|
|
|
|
{
|
|
|
|
void *p = malloc(size);
|
2005-09-12 12:33:27 +00:00
|
|
|
|
2005-10-10 11:35:17 +00:00
|
|
|
if(p == NULL)
|
|
|
|
bb_error_d(msg_enomem);
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline char *bb_xstrdup(const char *s)
|
|
|
|
{
|
|
|
|
char *r = strdup(s);
|
2005-09-14 15:09:06 +00:00
|
|
|
|
2005-10-10 11:35:17 +00:00
|
|
|
if(r == NULL)
|
|
|
|
bb_error_d(msg_enomem);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int dontgenerate_dep; /* flag -d usaged */
|
|
|
|
static int noiwarning; /* flag -w usaged */
|
|
|
|
static llist_t *configs; /* list of -c usaged and them stat() after parsed */
|
|
|
|
static llist_t *Iop; /* list of -I include usaged */
|
|
|
|
|
|
|
|
static char *pwd; /* current work directory */
|
|
|
|
static size_t replace; /* replace current work derectory to build dir */
|
|
|
|
|
|
|
|
static const char *kp; /* KEY path, argument of -k usaged */
|
|
|
|
static size_t kp_len;
|
|
|
|
static struct stat st_kp; /* stat(kp) */
|
|
|
|
|
|
|
|
typedef struct BB_KEYS {
|
|
|
|
char *keyname;
|
|
|
|
size_t key_sz;
|
|
|
|
const char *value;
|
|
|
|
const char *checked;
|
|
|
|
char *stored_path;
|
|
|
|
const char *src_have_this_key;
|
|
|
|
struct BB_KEYS *next;
|
|
|
|
} bb_key_t;
|
|
|
|
|
|
|
|
static bb_key_t *key_top; /* list of found KEYs */
|
|
|
|
static bb_key_t *Ifound; /* list of parsed includes */
|
|
|
|
|
|
|
|
|
|
|
|
static void parse_conf_opt(const char *opt, const char *val, size_t key_sz);
|
|
|
|
static void parse_inc(const char *include, const char *fname);
|
|
|
|
|
|
|
|
static inline bb_key_t *check_key(bb_key_t *k, const char *nk, size_t key_sz)
|
|
|
|
{
|
|
|
|
bb_key_t *cur;
|
|
|
|
|
|
|
|
for(cur = k; cur; cur = cur->next) {
|
|
|
|
if(key_sz == cur->key_sz && memcmp(cur->keyname, nk, key_sz) == 0) {
|
|
|
|
cur->checked = cur->stored_path;
|
|
|
|
return cur;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* for lexical analyser */
|
|
|
|
static int pagesizem1; /* padding mask = getpagesize() - 1 */
|
2005-09-12 12:33:27 +00:00
|
|
|
|
2005-09-14 15:09:06 +00:00
|
|
|
/* for speed tricks */
|
2005-10-10 11:35:17 +00:00
|
|
|
static char first_chars[1+UCHAR_MAX]; /* + L_EOF */
|
|
|
|
static char isalnums[1+UCHAR_MAX]; /* + L_EOF */
|
2005-09-14 15:09:06 +00:00
|
|
|
/* trick for fast find "define", "include", "undef" */
|
2005-10-10 11:35:17 +00:00
|
|
|
static const char first_chars_diu[UCHAR_MAX] = {
|
|
|
|
[(int)'d'] = (char)5, /* strlen("define") - 1; */
|
2005-10-07 15:36:26 +00:00
|
|
|
[(int)'i'] = (char)6, /* strlen("include") - 1; */
|
2005-10-10 11:35:17 +00:00
|
|
|
[(int)'u'] = (char)4, /* strlen("undef") - 1; */
|
2005-10-07 15:36:26 +00:00
|
|
|
};
|
2005-09-12 16:39:47 +00:00
|
|
|
|
2005-10-10 11:35:17 +00:00
|
|
|
#define CONFIG_MODE 0
|
|
|
|
#define SOURCES_MODE 1
|
|
|
|
static int mode;
|
2005-09-14 15:09:06 +00:00
|
|
|
|
2005-09-12 12:33:27 +00:00
|
|
|
#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 16:50:53 +00:00
|
|
|
#define REM '/' /* block comment */
|
|
|
|
#define BS '\\' /* back slash */
|
2005-09-12 12:33:27 +00:00
|
|
|
#define POUND '#' /* # */
|
2005-10-10 11:35:17 +00:00
|
|
|
#define D '5' /* #define preprocessor's directive */
|
|
|
|
#define I '6' /* #include preprocessor's directive */
|
|
|
|
#define U '4' /* #undef preprocessor's directive */
|
2005-09-12 12:33:27 +00:00
|
|
|
#define DK 'K' /* #define KEY... (config mode) */
|
2005-09-13 16:50:53 +00:00
|
|
|
#define ANY '*' /* any unparsed chars */
|
2005-09-12 12:33:27 +00:00
|
|
|
|
|
|
|
/* [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-10-10 11:35:17 +00:00
|
|
|
#define L_EOF (1+UCHAR_MAX)
|
2005-09-12 12:33:27 +00:00
|
|
|
|
2005-10-10 11:35:17 +00:00
|
|
|
#define getc0() do { c = (optr >= oend) ? L_EOF : *optr++; } while(0)
|
|
|
|
|
|
|
|
#define getc1() do { getc0(); if(c == BS) { getc0(); \
|
|
|
|
if(c == '\n') { line++; continue; } \
|
|
|
|
else { optr--; c = BS; } \
|
|
|
|
} break; } while(1)
|
|
|
|
|
|
|
|
static char id_s[4096];
|
|
|
|
#define put_id(ic) do { if(id_len == sizeof(id_s)) goto too_long; \
|
|
|
|
id[id_len++] = ic; } while(0)
|
2005-09-12 12:33:27 +00:00
|
|
|
|
2005-09-14 15:09:06 +00:00
|
|
|
|
|
|
|
/* stupid C lexical analyser */
|
2005-09-13 16:50:53 +00:00
|
|
|
static void c_lex(const char *fname, long fsize)
|
2005-09-12 12:33:27 +00:00
|
|
|
{
|
2005-10-10 11:35:17 +00:00
|
|
|
int c;
|
2005-09-12 12:33:27 +00:00
|
|
|
int state;
|
|
|
|
int line;
|
2005-09-13 16:50:53 +00:00
|
|
|
char *id = id_s;
|
2005-09-12 12:33:27 +00:00
|
|
|
size_t id_len = 0; /* stupid initialize */
|
|
|
|
unsigned char *optr, *oend;
|
|
|
|
|
|
|
|
int fd;
|
|
|
|
char *map;
|
|
|
|
int mapsize;
|
|
|
|
|
2005-09-14 15:09:06 +00:00
|
|
|
if(fsize == 0) {
|
|
|
|
fprintf(stderr, "Warning: %s is empty\n", fname);
|
|
|
|
return;
|
|
|
|
}
|
2005-09-13 16:50:53 +00:00
|
|
|
fd = open(fname, O_RDONLY);
|
|
|
|
if(fd < 0) {
|
2005-09-12 12:33:27 +00:00
|
|
|
perror(fname);
|
|
|
|
return;
|
2005-09-13 16:50:53 +00:00
|
|
|
}
|
|
|
|
mapsize = (fsize+pagesizem1) & ~pagesizem1;
|
|
|
|
map = mmap(NULL, mapsize, PROT_READ, MAP_PRIVATE, fd, 0);
|
|
|
|
if ((long) map == -1)
|
2005-09-12 12:33:27 +00:00
|
|
|
bb_error_d("%s: mmap: %m", fname);
|
|
|
|
|
2005-09-13 16:50:53 +00:00
|
|
|
optr = (unsigned char *)map;
|
|
|
|
oend = optr + fsize;
|
2005-09-12 12:33:27 +00:00
|
|
|
|
|
|
|
line = 1;
|
2005-10-10 11:35:17 +00:00
|
|
|
state = S;
|
2005-09-12 12:33:27 +00:00
|
|
|
|
|
|
|
for(;;) {
|
2005-10-10 11:35:17 +00:00
|
|
|
getc1();
|
|
|
|
for(;;) {
|
2005-09-12 12:33:27 +00:00
|
|
|
/* [ \t]+ eat first space */
|
|
|
|
while(c == ' ' || c == '\t')
|
|
|
|
getc1();
|
|
|
|
|
|
|
|
if(state == S) {
|
2005-09-13 16:50:53 +00:00
|
|
|
while(first_chars[c] == ANY) {
|
|
|
|
/* <S>unparsed */
|
2005-09-12 12:33:27 +00:00
|
|
|
if(c == '\n')
|
|
|
|
line++;
|
|
|
|
getc1();
|
|
|
|
}
|
2005-09-13 16:50:53 +00:00
|
|
|
if(c == L_EOF) {
|
2005-09-12 12:33:27 +00:00
|
|
|
/* <S><<EOF>> */
|
|
|
|
munmap(map, mapsize);
|
|
|
|
close(fd);
|
|
|
|
return;
|
|
|
|
}
|
2005-09-13 16:50:53 +00:00
|
|
|
if(c == REM) {
|
2005-09-12 12:33:27 +00:00
|
|
|
/* <S>/ */
|
2005-10-10 11:35:17 +00:00
|
|
|
getc0();
|
2005-09-13 16:50:53 +00:00
|
|
|
if(c == REM) {
|
2005-09-12 12:33:27 +00:00
|
|
|
/* <S>"//"[^\n]* */
|
2005-10-10 11:35:17 +00:00
|
|
|
do getc0(); while(c != '\n' && c != L_EOF);
|
2005-09-12 12:33:27 +00:00
|
|
|
} else if(c == '*') {
|
2005-10-10 11:35:17 +00:00
|
|
|
/* <S>[/][*] goto parse block comments */
|
|
|
|
break;
|
2005-09-12 12:33:27 +00:00
|
|
|
}
|
2005-09-13 16:50:53 +00:00
|
|
|
} else if(c == POUND) {
|
|
|
|
/* <S># */
|
2005-09-12 12:33:27 +00:00
|
|
|
state = c;
|
2005-10-10 11:35:17 +00:00
|
|
|
getc1();
|
2005-09-12 12:33:27 +00:00
|
|
|
} else if(c == STR || c == CHR) {
|
2005-09-13 16:50:53 +00:00
|
|
|
/* <S>\"|\' */
|
2005-10-10 11:35:17 +00:00
|
|
|
int qc = c;
|
|
|
|
|
|
|
|
for(;;) {
|
|
|
|
/* <STR,CHR>. */
|
|
|
|
getc1();
|
|
|
|
if(c == qc) {
|
|
|
|
/* <STR>\" or <CHR>\' */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if(c == BS) {
|
|
|
|
/* <STR,CHR>\\ but is not <STR,CHR>\\\n */
|
|
|
|
getc0();
|
|
|
|
}
|
|
|
|
if(c == '\n' || c == L_EOF)
|
|
|
|
yy_error_d("unterminated");
|
|
|
|
}
|
|
|
|
getc1();
|
|
|
|
} else {
|
2005-09-12 12:33:27 +00:00
|
|
|
/* <S>[A-Z_a-z0-9] */
|
2005-09-12 16:39:47 +00:00
|
|
|
|
|
|
|
/* trick for fast drop id
|
|
|
|
if key with this first char undefined */
|
2005-09-13 16:50:53 +00:00
|
|
|
if(first_chars[c] == 0) {
|
2005-09-12 16:39:47 +00:00
|
|
|
/* skip <S>[A-Z_a-z0-9]+ */
|
2005-09-16 13:57:33 +00:00
|
|
|
do getc1(); while(isalnums[c]);
|
2005-09-12 16:39:47 +00:00
|
|
|
} else {
|
|
|
|
id_len = 0;
|
|
|
|
do {
|
2005-09-12 12:33:27 +00:00
|
|
|
/* <S>[A-Z_a-z0-9]+ */
|
|
|
|
put_id(c);
|
|
|
|
getc1();
|
2005-09-16 13:57:33 +00:00
|
|
|
} while(isalnums[c]);
|
2005-10-03 10:08:46 +00:00
|
|
|
check_key(key_top, id, id_len);
|
2005-09-12 16:39:47 +00:00
|
|
|
}
|
2005-09-12 12:33:27 +00:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* begin preprocessor states */
|
2005-09-13 16:50:53 +00:00
|
|
|
if(c == L_EOF)
|
2005-09-12 12:33:27 +00:00
|
|
|
yy_error_d("unexpected EOF");
|
2005-09-13 16:50:53 +00:00
|
|
|
if(c == REM) {
|
2005-09-12 12:33:27 +00:00
|
|
|
/* <#.*>/ */
|
2005-10-10 11:35:17 +00:00
|
|
|
getc0();
|
2005-09-13 16:50:53 +00:00
|
|
|
if(c == REM)
|
2005-09-14 15:09:06 +00:00
|
|
|
yy_error_d("detected // in preprocessor line");
|
2005-09-12 12:33:27 +00:00
|
|
|
if(c == '*') {
|
2005-10-10 11:35:17 +00:00
|
|
|
/* <#.*>[/][*] goto parse block comments */
|
|
|
|
break;
|
2005-09-12 12:33:27 +00:00
|
|
|
}
|
|
|
|
/* hmm, #.*[/] */
|
|
|
|
yy_error_d("strange preprocessor line");
|
|
|
|
}
|
2005-09-13 16:50:53 +00:00
|
|
|
if(state == POUND) {
|
2005-09-14 15:09:06 +00:00
|
|
|
/* tricks */
|
|
|
|
static const char * const preproc[] = {
|
2005-10-10 11:35:17 +00:00
|
|
|
/* 0 1 2 3 4 5 6 */
|
|
|
|
"", "", "", "", "ndef", "efine", "nclude"
|
2005-09-14 15:09:06 +00:00
|
|
|
};
|
|
|
|
size_t diu = first_chars_diu[c]; /* strlen and preproc ptr */
|
2005-09-12 12:33:27 +00:00
|
|
|
|
2005-10-10 11:35:17 +00:00
|
|
|
state = S;
|
|
|
|
if(diu != S) {
|
|
|
|
getc1();
|
|
|
|
id_len = 0;
|
|
|
|
while(isalnums[c]) {
|
|
|
|
put_id(c);
|
|
|
|
getc1();
|
|
|
|
}
|
|
|
|
/* have str begined with c, readed == strlen key and compared */
|
|
|
|
if(diu == id_len && !memcmp(id, preproc[diu], diu)) {
|
|
|
|
state = diu + '0';
|
|
|
|
id_len = 0; /* common for save */
|
|
|
|
}
|
2005-09-14 15:09:06 +00:00
|
|
|
} else {
|
2005-10-10 11:35:17 +00:00
|
|
|
while(isalnums[c]) getc1();
|
2005-09-13 16:50:53 +00:00
|
|
|
}
|
2005-10-10 11:35:17 +00:00
|
|
|
} else if(state == I) {
|
2005-09-12 12:33:27 +00:00
|
|
|
if(c == STR) {
|
|
|
|
/* <I>\" */
|
2005-10-10 11:35:17 +00:00
|
|
|
for(;;) {
|
|
|
|
getc1();
|
|
|
|
if(c == STR)
|
|
|
|
break;
|
|
|
|
if(c == L_EOF)
|
|
|
|
yy_error_d("unexpected EOF");
|
|
|
|
put_id(c);
|
|
|
|
}
|
|
|
|
put_id(0);
|
|
|
|
/* store "include.h" */
|
|
|
|
parse_inc(id, fname);
|
|
|
|
getc1();
|
2005-09-12 12:33:27 +00:00
|
|
|
}
|
2005-10-10 11:35:17 +00:00
|
|
|
/* else another (may be wrong) #include ... */
|
|
|
|
state = S;
|
|
|
|
} else if(state == D || state == U) {
|
2005-09-14 15:09:06 +00:00
|
|
|
if(mode == SOURCES_MODE) {
|
2005-09-13 16:50:53 +00:00
|
|
|
/* ignore depend with #define or #undef KEY */
|
2005-10-10 11:35:17 +00:00
|
|
|
while(isalnums[c]) getc1();
|
2005-09-12 12:33:27 +00:00
|
|
|
state = S;
|
|
|
|
} else {
|
2005-09-13 16:50:53 +00:00
|
|
|
/* save KEY from #"define"|"undef" ... */
|
2005-09-16 13:57:33 +00:00
|
|
|
while(isalnums[c]) {
|
2005-09-13 16:50:53 +00:00
|
|
|
put_id(c);
|
|
|
|
getc1();
|
|
|
|
}
|
2005-09-12 12:33:27 +00:00
|
|
|
if(!id_len)
|
2005-09-14 15:09:06 +00:00
|
|
|
yy_error_d("expected identifier");
|
2005-09-12 12:33:27 +00:00
|
|
|
if(state == U) {
|
2005-10-10 11:35:17 +00:00
|
|
|
parse_conf_opt(id, NULL, id_len);
|
2005-09-12 12:33:27 +00:00
|
|
|
state = S;
|
|
|
|
} else {
|
|
|
|
/* D -> DK */
|
2005-10-10 11:35:17 +00:00
|
|
|
if(c == '(')
|
|
|
|
yy_error_d("unexpected function macro");
|
2005-09-12 12:33:27 +00:00
|
|
|
state = DK;
|
|
|
|
}
|
|
|
|
}
|
2005-10-10 11:35:17 +00:00
|
|
|
} else {
|
|
|
|
/* state==<DK> #define KEY[ ] (config mode) */
|
|
|
|
size_t opt_len = id_len;
|
|
|
|
char *val = id + opt_len;
|
|
|
|
char *sp;
|
|
|
|
|
|
|
|
for(;;) {
|
|
|
|
if(c == L_EOF || c == '\n')
|
|
|
|
break;
|
2005-09-12 12:33:27 +00:00
|
|
|
put_id(c);
|
|
|
|
getc1();
|
|
|
|
}
|
2005-10-10 11:35:17 +00:00
|
|
|
sp = id + id_len;
|
2005-10-03 10:08:46 +00:00
|
|
|
put_id(0);
|
2005-10-10 11:35:17 +00:00
|
|
|
/* trim tail spaces */
|
|
|
|
while(--sp >= val && (*sp == ' ' || *sp == '\t'
|
|
|
|
|| *sp == '\f' || *sp == '\v'))
|
|
|
|
*sp = '\0';
|
|
|
|
parse_conf_opt(id, val, opt_len);
|
2005-10-03 10:08:46 +00:00
|
|
|
state = S;
|
2005-09-12 12:33:27 +00:00
|
|
|
}
|
|
|
|
}
|
2005-10-10 11:35:17 +00:00
|
|
|
|
|
|
|
/* <REM> */
|
|
|
|
getc0();
|
|
|
|
for(;;) {
|
|
|
|
/* <REM>[^*]+ */
|
|
|
|
while(c != '*') {
|
|
|
|
if(c == '\n') {
|
|
|
|
/* <REM>\n */
|
|
|
|
if(state != S)
|
|
|
|
yy_error_d("unexpected newline");
|
|
|
|
line++;
|
|
|
|
} else if(c == L_EOF)
|
|
|
|
yy_error_d("unexpected EOF");
|
|
|
|
getc0();
|
|
|
|
}
|
|
|
|
/* <REM>[*] */
|
|
|
|
getc0();
|
|
|
|
if(c == REM) {
|
|
|
|
/* <REM>[*][/] */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
too_long:
|
|
|
|
yy_error_d("phrase too long");
|
2005-09-12 12:33:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-10-10 11:35:17 +00:00
|
|
|
/* bb_simplify_path special variant for apsolute pathname */
|
|
|
|
static size_t bb_qa_simplify_path(char *path)
|
2005-09-12 12:33:27 +00:00
|
|
|
{
|
2005-10-10 11:35:17 +00:00
|
|
|
char *s, *p;
|
2005-09-12 12:33:27 +00:00
|
|
|
|
2005-10-10 11:35:17 +00:00
|
|
|
p = s = path;
|
2005-09-12 12:33:27 +00:00
|
|
|
|
2005-10-10 11:35:17 +00:00
|
|
|
do {
|
|
|
|
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 > path) {
|
|
|
|
while (*--p != '/'); /* omit previous dir */
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*++p = *s;
|
|
|
|
} while (*++s);
|
2005-09-12 12:33:27 +00:00
|
|
|
|
2005-10-10 11:35:17 +00:00
|
|
|
if ((p == path) || (*p != '/')) { /* not a trailing slash */
|
|
|
|
++p; /* so keep last character */
|
2005-09-12 12:33:27 +00:00
|
|
|
}
|
2005-10-10 11:35:17 +00:00
|
|
|
*p = 0;
|
2005-09-13 16:50:53 +00:00
|
|
|
|
2005-10-10 11:35:17 +00:00
|
|
|
return (p - path);
|
2005-09-12 12:33:27 +00:00
|
|
|
}
|
|
|
|
|
2005-10-10 11:35:17 +00:00
|
|
|
static void parse_inc(const char *include, const char *fname)
|
2005-09-12 12:33:27 +00:00
|
|
|
{
|
2005-10-10 11:35:17 +00:00
|
|
|
bb_key_t *cur;
|
|
|
|
const char *p_i;
|
|
|
|
llist_t *lo;
|
|
|
|
char *ap;
|
|
|
|
size_t key_sz;
|
|
|
|
|
|
|
|
if(*include == '/') {
|
|
|
|
lo = NULL;
|
|
|
|
ap = bb_xstrdup(include);
|
2005-09-13 16:50:53 +00:00
|
|
|
} else {
|
2005-10-10 11:35:17 +00:00
|
|
|
int w;
|
|
|
|
const char *p;
|
|
|
|
|
|
|
|
lo = Iop;
|
|
|
|
p = strrchr(fname, '/'); /* fname have absolute pathname */
|
|
|
|
w = (p-fname);
|
|
|
|
/* find from current directory of source file */
|
|
|
|
ap = bb_asprint("%.*s/%s", w, fname, include);
|
2005-09-12 12:33:27 +00:00
|
|
|
}
|
|
|
|
|
2005-10-10 11:35:17 +00:00
|
|
|
for(;;) {
|
|
|
|
key_sz = bb_qa_simplify_path(ap);
|
|
|
|
cur = check_key(Ifound, ap, key_sz);
|
|
|
|
if(cur) {
|
|
|
|
cur->checked = cur->value;
|
|
|
|
free(ap);
|
2005-09-12 12:33:27 +00:00
|
|
|
return;
|
|
|
|
}
|
2005-10-10 11:35:17 +00:00
|
|
|
if(access(ap, F_OK) == 0) {
|
|
|
|
/* found */
|
|
|
|
p_i = ap;
|
|
|
|
break;
|
|
|
|
} else if(lo == NULL) {
|
|
|
|
p_i = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* find from "-I include" specified directories */
|
|
|
|
free(ap);
|
|
|
|
/* lo->data have absolute pathname */
|
|
|
|
ap = bb_asprint("%s/%s", lo->data, include);
|
|
|
|
lo = lo->link;
|
|
|
|
}
|
|
|
|
|
|
|
|
cur = xmalloc(sizeof(bb_key_t));
|
|
|
|
cur->keyname = ap;
|
|
|
|
cur->key_sz = key_sz;
|
|
|
|
cur->stored_path = ap;
|
|
|
|
cur->value = cur->checked = p_i;
|
|
|
|
if(p_i == NULL && noiwarning)
|
|
|
|
fprintf(stderr, "%s: Warning: #include \"%s\" not found\n", fname, include);
|
|
|
|
cur->next = Ifound;
|
|
|
|
Ifound = cur;
|
2005-09-12 12:33:27 +00:00
|
|
|
}
|
|
|
|
|
2005-10-10 11:35:17 +00:00
|
|
|
static size_t max_rec_sz;
|
|
|
|
|
|
|
|
static void parse_conf_opt(const char *opt, const char *val, size_t key_sz)
|
2005-09-12 12:33:27 +00:00
|
|
|
{
|
2005-09-13 16:50:53 +00:00
|
|
|
bb_key_t *cur;
|
2005-10-03 10:08:46 +00:00
|
|
|
char *k;
|
2005-10-10 11:35:17 +00:00
|
|
|
char *p;
|
|
|
|
size_t val_sz = 0;
|
2005-09-12 12:33:27 +00:00
|
|
|
|
2005-10-03 10:08:46 +00:00
|
|
|
cur = check_key(key_top, opt, key_sz);
|
2005-09-14 15:09:06 +00:00
|
|
|
if(cur != NULL) {
|
|
|
|
/* present already */
|
2005-10-03 10:08:46 +00:00
|
|
|
cur->checked = NULL; /* store only */
|
2005-09-14 15:09:06 +00:00
|
|
|
if(cur->value == NULL && val == NULL)
|
|
|
|
return;
|
2005-10-03 10:08:46 +00:00
|
|
|
if(cur->value != NULL && val != NULL && !strcmp(cur->value, val))
|
2005-09-14 15:09:06 +00:00
|
|
|
return;
|
2005-10-03 10:08:46 +00:00
|
|
|
k = cur->keyname;
|
|
|
|
fprintf(stderr, "Warning: redefined %s\n", k);
|
2005-09-16 13:57:33 +00:00
|
|
|
} else {
|
2005-10-10 11:35:17 +00:00
|
|
|
size_t recordsz;
|
|
|
|
|
|
|
|
if(val && *val)
|
|
|
|
val_sz = strlen(val) + 1;
|
|
|
|
recordsz = key_sz + val_sz + 1;
|
|
|
|
if(max_rec_sz < recordsz)
|
|
|
|
max_rec_sz = recordsz;
|
|
|
|
cur = xmalloc(sizeof(bb_key_t) + recordsz);
|
|
|
|
k = cur->keyname = memcpy(cur + 1, opt, key_sz);
|
|
|
|
cur->keyname[key_sz] = '\0';
|
|
|
|
cur->key_sz = key_sz;
|
|
|
|
cur->checked = NULL;
|
|
|
|
cur->src_have_this_key = NULL;
|
|
|
|
cur->next = key_top;
|
|
|
|
key_top = cur;
|
2005-09-14 15:09:06 +00:00
|
|
|
}
|
2005-10-10 11:35:17 +00:00
|
|
|
/* save VAL */
|
2005-09-14 15:09:06 +00:00
|
|
|
if(val) {
|
|
|
|
if(*val == '\0') {
|
|
|
|
cur->value = "";
|
2005-09-13 16:50:53 +00:00
|
|
|
} else {
|
2005-10-10 11:35:17 +00:00
|
|
|
cur->value = p = cur->keyname + key_sz + 1;
|
|
|
|
memcpy(p, val, val_sz);
|
2005-09-12 12:33:27 +00:00
|
|
|
}
|
2005-09-14 15:09:06 +00:00
|
|
|
} else {
|
|
|
|
cur->value = NULL;
|
|
|
|
}
|
|
|
|
/* trick, save first char KEY for do fast identify id */
|
2005-10-03 10:08:46 +00:00
|
|
|
first_chars[(int)*k] = *k;
|
2005-09-14 15:09:06 +00:00
|
|
|
|
2005-10-03 10:08:46 +00:00
|
|
|
cur->stored_path = k = bb_asprint("%s/%s.h", kp, k);
|
2005-09-14 15:09:06 +00:00
|
|
|
/* key converting [A-Z_] -> [a-z/] */
|
2005-10-03 10:08:46 +00:00
|
|
|
for(p = k + kp_len + 1; *p; p++) {
|
2005-09-14 15:09:06 +00:00
|
|
|
if(*p >= 'A' && *p <= 'Z')
|
|
|
|
*p = *p - 'A' + 'a';
|
2005-09-16 13:57:33 +00:00
|
|
|
else if(*p == '_' && p[1] > '9') /* do not change A_1 to A/1 */
|
2005-09-14 15:09:06 +00:00
|
|
|
*p = '/';
|
|
|
|
}
|
2005-10-10 11:35:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void store_keys(void)
|
|
|
|
{
|
|
|
|
bb_key_t *cur;
|
|
|
|
char *k;
|
|
|
|
struct stat st;
|
|
|
|
int cmp_ok;
|
|
|
|
ssize_t rw_ret;
|
|
|
|
size_t recordsz = max_rec_sz * 2 + 10 * 2 + 16;
|
|
|
|
/* buffer for double "#define KEY VAL\n" */
|
|
|
|
char *record_buf = xmalloc(recordsz);
|
|
|
|
|
|
|
|
for(cur = key_top; cur; cur = cur->next) {
|
|
|
|
if(cur->src_have_this_key) {
|
|
|
|
/* do generate record */
|
|
|
|
k = cur->keyname;
|
|
|
|
if(cur->value == NULL) {
|
|
|
|
recordsz = sprintf(record_buf, "#undef %s\n", k);
|
|
|
|
} else {
|
|
|
|
const char *val = cur->value;
|
|
|
|
if(*val == '\0') {
|
|
|
|
recordsz = sprintf(record_buf, "#define %s\n", k);
|
|
|
|
} else {
|
|
|
|
recordsz = sprintf(record_buf, "#define %s %s\n", k, val);
|
2005-09-12 12:33:27 +00:00
|
|
|
}
|
|
|
|
}
|
2005-10-10 11:35:17 +00:00
|
|
|
/* size_t -> ssize_t :( */
|
|
|
|
rw_ret = (ssize_t)recordsz;
|
|
|
|
/* check kp/key.h, compare after previous usage */
|
|
|
|
cmp_ok = 0;
|
|
|
|
k = cur->stored_path;
|
|
|
|
if(stat(k, &st)) {
|
|
|
|
char *p;
|
|
|
|
for(p = k + kp_len + 1; *p; p++) {
|
|
|
|
/* Auto-create directories. */
|
|
|
|
if (*p == '/') {
|
|
|
|
*p = '\0';
|
|
|
|
if (access(k, F_OK) != 0 && mkdir(k, 0755) != 0)
|
|
|
|
bb_error_d("mkdir(%s): %m", k);
|
|
|
|
*p = '/';
|
|
|
|
}
|
2005-09-14 15:09:06 +00:00
|
|
|
}
|
2005-10-10 11:35:17 +00:00
|
|
|
} else {
|
|
|
|
/* found */
|
|
|
|
if(st.st_size == (off_t)recordsz) {
|
|
|
|
char *r_cmp;
|
|
|
|
int fd;
|
|
|
|
size_t padded = recordsz;
|
|
|
|
|
|
|
|
/* 16-byte padding for read(2) and memcmp(3) */
|
|
|
|
padded = (padded+15) & ~15;
|
|
|
|
r_cmp = record_buf + padded;
|
|
|
|
fd = open(k, O_RDONLY);
|
|
|
|
if(fd < 0 || read(fd, r_cmp, recordsz) < rw_ret)
|
|
|
|
bb_error_d("%s: %m", k);
|
|
|
|
close(fd);
|
|
|
|
cmp_ok = memcmp(record_buf, r_cmp, recordsz) == 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(!cmp_ok) {
|
|
|
|
int fd = open(k, O_WRONLY|O_CREAT|O_TRUNC, 0644);
|
|
|
|
if(fd < 0 || write(fd, record_buf, recordsz) < rw_ret)
|
|
|
|
bb_error_d("%s: %m", k);
|
|
|
|
close(fd);
|
|
|
|
}
|
2005-09-14 15:09:06 +00:00
|
|
|
}
|
2005-10-10 11:35:17 +00:00
|
|
|
}
|
2005-09-12 12:33:27 +00:00
|
|
|
}
|
|
|
|
|
2005-10-07 15:36:26 +00:00
|
|
|
static int show_dep(int first, bb_key_t *k, const char *name, const char *f)
|
2005-09-12 12:33:27 +00:00
|
|
|
{
|
|
|
|
bb_key_t *cur;
|
|
|
|
|
|
|
|
for(cur = k; cur; cur = cur->next) {
|
2005-10-03 10:08:46 +00:00
|
|
|
if(cur->checked) {
|
2005-10-10 11:35:17 +00:00
|
|
|
if(first >= 0) {
|
|
|
|
if(first) {
|
|
|
|
if(f == NULL)
|
2005-10-07 15:36:26 +00:00
|
|
|
printf("\n%s:", name);
|
2005-10-10 11:35:17 +00:00
|
|
|
else
|
2005-10-07 15:36:26 +00:00
|
|
|
printf("\n%s/%s:", pwd, name);
|
2005-10-10 11:35:17 +00:00
|
|
|
first = 0;
|
|
|
|
} else {
|
|
|
|
printf(" \\\n ");
|
|
|
|
}
|
|
|
|
printf(" %s", cur->checked);
|
2005-09-12 12:33:27 +00:00
|
|
|
}
|
2005-10-10 11:35:17 +00:00
|
|
|
cur->src_have_this_key = cur->checked;
|
|
|
|
cur->checked = NULL;
|
2005-09-12 12:33:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return first;
|
|
|
|
}
|
|
|
|
|
2005-09-13 16:50:53 +00:00
|
|
|
static char *
|
2005-09-14 15:09:06 +00:00
|
|
|
parse_chd(const char *fe, const char *p, size_t dirlen)
|
2005-09-12 12:33:27 +00:00
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
char *fp;
|
2005-09-13 16:50:53 +00:00
|
|
|
size_t df_sz;
|
2005-10-10 11:35:17 +00:00
|
|
|
static char dir_and_entry[4096];
|
|
|
|
size_t fe_sz = strlen(fe) + 1;
|
2005-09-12 12:33:27 +00:00
|
|
|
|
2005-10-10 11:35:17 +00:00
|
|
|
df_sz = dirlen + fe_sz + 1; /* dir/file\0 */
|
|
|
|
if(df_sz > sizeof(dir_and_entry))
|
|
|
|
bb_error_d("%s: file name too long", fe);
|
2005-09-13 16:50:53 +00:00
|
|
|
fp = dir_and_entry;
|
2005-10-10 11:35:17 +00:00
|
|
|
/* sprintf(fp, "%s/%s", p, fe); */
|
|
|
|
memcpy(fp, p, dirlen);
|
|
|
|
fp[dirlen] = '/';
|
|
|
|
memcpy(fp + dirlen + 1, fe, fe_sz);
|
2005-09-13 16:50:53 +00:00
|
|
|
|
2005-09-12 12:33:27 +00:00
|
|
|
if(stat(fp, &st)) {
|
2005-10-03 10:08:46 +00:00
|
|
|
fprintf(stderr, "Warning: stat(%s): %m\n", fp);
|
2005-09-12 12:33:27 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2005-09-12 16:39:47 +00:00
|
|
|
if(S_ISREG(st.st_mode)) {
|
2005-09-14 15:09:06 +00:00
|
|
|
llist_t *cfl;
|
|
|
|
char *e = fp + df_sz - 3;
|
|
|
|
|
|
|
|
if(*e++ != '.' || (*e != 'c' && *e != 'h')) {
|
2005-09-12 16:39:47 +00:00
|
|
|
/* direntry is regular file, but is not *.[ch] */
|
2005-09-12 12:33:27 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2005-09-14 15:09:06 +00:00
|
|
|
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 16:39:47 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
2005-09-14 15:09:06 +00:00
|
|
|
/* direntry is *.[ch] regular file and is not configs */
|
2005-10-10 11:35:17 +00:00
|
|
|
c_lex(fp, st.st_size);
|
2005-09-14 15:09:06 +00:00
|
|
|
if(!dontgenerate_dep) {
|
|
|
|
int first;
|
|
|
|
if(*e == 'c') {
|
|
|
|
/* *.c -> *.o */
|
|
|
|
*e = 'o';
|
2005-10-07 15:36:26 +00:00
|
|
|
/* /src_dir/path/file.o to path/file.o */
|
|
|
|
fp += replace;
|
|
|
|
if(*fp == '/')
|
2005-10-10 11:35:17 +00:00
|
|
|
fp++;
|
2005-10-06 14:47:16 +00:00
|
|
|
} else {
|
|
|
|
e = NULL;
|
2005-09-14 15:09:06 +00:00
|
|
|
}
|
2005-10-07 15:36:26 +00:00
|
|
|
first = show_dep(1, Ifound, fp, e);
|
|
|
|
first = show_dep(first, key_top, fp, e);
|
2005-09-14 15:09:06 +00:00
|
|
|
if(first == 0)
|
|
|
|
putchar('\n');
|
2005-10-10 11:35:17 +00:00
|
|
|
} else {
|
|
|
|
show_dep(-1, key_top, NULL, NULL);
|
2005-09-12 12:33:27 +00:00
|
|
|
}
|
2005-09-14 15:09:06 +00:00
|
|
|
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 */
|
2005-10-10 11:35:17 +00:00
|
|
|
/* direntry is directory. buff is returned */
|
|
|
|
return bb_xstrdup(fp);
|
2005-09-12 12:33:27 +00:00
|
|
|
}
|
2005-09-14 15:09:06 +00:00
|
|
|
/* hmm, direntry is device! */
|
2005-09-12 12:33:27 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2005-09-14 15:09:06 +00:00
|
|
|
/* 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 10:08:46 +00:00
|
|
|
static void scan_dir_find_ch_files(const char *p)
|
2005-09-12 12:33:27 +00:00
|
|
|
{
|
|
|
|
llist_t *dirs;
|
|
|
|
llist_t *d_add;
|
|
|
|
llist_t *d;
|
|
|
|
struct dirent *de;
|
|
|
|
DIR *dir;
|
2005-09-13 16:50:53 +00:00
|
|
|
size_t dirlen;
|
2005-09-12 12:33:27 +00:00
|
|
|
|
2005-10-03 10:08:46 +00:00
|
|
|
dirs = llist_add_to(NULL, bb_simplify_path(p));
|
2005-10-07 15:36:26 +00:00
|
|
|
replace = strlen(dirs->data);
|
2005-09-12 12:33:27 +00:00
|
|
|
/* emulate recursive */
|
|
|
|
while(dirs) {
|
|
|
|
d_add = NULL;
|
|
|
|
while(dirs) {
|
|
|
|
dir = opendir(dirs->data);
|
|
|
|
if (dir == NULL)
|
2005-10-03 10:08:46 +00:00
|
|
|
fprintf(stderr, "Warning: opendir(%s): %m\n", dirs->data);
|
2005-09-13 16:50:53 +00:00
|
|
|
dirlen = strlen(dirs->data);
|
2005-09-12 12:33:27 +00:00
|
|
|
while ((de = readdir(dir)) != NULL) {
|
2005-10-03 10:08:46 +00:00
|
|
|
char *found_dir;
|
2005-09-13 16:50:53 +00:00
|
|
|
|
2005-10-03 10:08:46 +00:00
|
|
|
if (de->d_name[0] == '.')
|
|
|
|
continue;
|
|
|
|
found_dir = parse_chd(de->d_name, dirs->data, dirlen);
|
2005-09-13 16:50:53 +00:00
|
|
|
if(found_dir)
|
|
|
|
d_add = llist_add_to(d_add, found_dir);
|
2005-09-12 12:33:27 +00:00
|
|
|
}
|
|
|
|
closedir(dir);
|
2005-10-03 10:08:46 +00:00
|
|
|
free(dirs->data);
|
2005-09-12 12:33:27 +00:00
|
|
|
d = dirs;
|
|
|
|
dirs = dirs->link;
|
|
|
|
free(d);
|
|
|
|
}
|
|
|
|
dirs = d_add;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-10-10 11:35:17 +00:00
|
|
|
static void show_usage(void) __attribute__ ((noreturn));
|
|
|
|
static void show_usage(void)
|
|
|
|
{
|
|
|
|
bb_error_d("%s\n%s\n", bb_mkdep_terse_options, bb_mkdep_full_options);
|
|
|
|
}
|
2005-09-13 16:50:53 +00:00
|
|
|
|
2005-09-12 12:33:27 +00:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
char *s;
|
|
|
|
int i;
|
|
|
|
llist_t *fl;
|
|
|
|
|
2005-09-13 16:50:53 +00:00
|
|
|
{
|
2005-10-10 11:35:17 +00:00
|
|
|
/* for bb_simplify_path, this program have not chdir() */
|
|
|
|
/* libbb-like my xgetcwd() */
|
2005-09-13 16:50:53 +00:00
|
|
|
unsigned path_max = 512;
|
|
|
|
|
|
|
|
s = xmalloc (path_max);
|
|
|
|
while (getcwd (s, path_max) == NULL) {
|
|
|
|
if(errno != ERANGE)
|
|
|
|
bb_error_d("getcwd: %m");
|
2005-10-10 11:35:17 +00:00
|
|
|
free(s);
|
|
|
|
s = xmalloc(path_max *= 2);
|
|
|
|
}
|
2005-09-13 16:50:53 +00:00
|
|
|
pwd = s;
|
|
|
|
}
|
|
|
|
|
2005-09-12 12:33:27 +00:00
|
|
|
while ((i = getopt(argc, argv, "I:c:dk:w")) > 0) {
|
|
|
|
switch(i) {
|
|
|
|
case 'I':
|
2005-10-10 11:35:17 +00:00
|
|
|
s = bb_simplify_path(optarg);
|
|
|
|
Iop = llist_add_to(Iop, s);
|
2005-09-12 12:33:27 +00:00
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
s = bb_simplify_path(optarg);
|
|
|
|
configs = llist_add_to(configs, s);
|
|
|
|
break;
|
|
|
|
case 'd':
|
2005-09-14 15:09:06 +00:00
|
|
|
dontgenerate_dep = 1;
|
2005-09-12 12:33:27 +00:00
|
|
|
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 15:09:06 +00:00
|
|
|
/* default kp */
|
2005-09-12 12:33:27 +00:00
|
|
|
if(kp == NULL)
|
|
|
|
kp = bb_simplify_path(INCLUDE_CONFIG_PATH);
|
2005-09-14 15:09:06 +00:00
|
|
|
/* 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 12:33:27 +00:00
|
|
|
if(Iop == NULL)
|
2005-10-10 11:35:17 +00:00
|
|
|
Iop = llist_add_to(Iop, bb_simplify_path(LOCAL_INCLUDE_PATH));
|
2005-09-12 12:33:27 +00:00
|
|
|
if(configs == NULL) {
|
|
|
|
s = bb_simplify_path(INCLUDE_CONFIG_KEYS_PATH);
|
|
|
|
configs = llist_add_to(configs, s);
|
|
|
|
}
|
2005-09-13 16:50:53 +00:00
|
|
|
/* for c_lex */
|
|
|
|
pagesizem1 = getpagesize() - 1;
|
2005-10-10 11:35:17 +00:00
|
|
|
for(i = 0; i < UCHAR_MAX; i++) {
|
2005-09-16 13:57:33 +00:00
|
|
|
if(ISALNUM(i))
|
|
|
|
isalnums[i] = i;
|
2005-09-13 16:50:53 +00:00
|
|
|
/* set unparsed chars for speed up of parser */
|
2005-10-10 11:35:17 +00:00
|
|
|
else if(i != CHR && i != STR && i != POUND && i != REM)
|
2005-09-13 16:50:53 +00:00
|
|
|
first_chars[i] = ANY;
|
|
|
|
}
|
|
|
|
first_chars[i] = '-'; /* L_EOF */
|
|
|
|
|
2005-09-14 15:09:06 +00:00
|
|
|
/* 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);
|
2005-10-10 11:35:17 +00:00
|
|
|
free(fl->data);
|
2005-09-14 15:09:06 +00:00
|
|
|
/* trick for fast comparing found files with configs */
|
2005-10-10 11:35:17 +00:00
|
|
|
fl->data = xmalloc(sizeof(struct stat));
|
2005-09-14 15:09:06 +00:00
|
|
|
memcpy(fl->data, &st, sizeof(struct stat));
|
|
|
|
}
|
2005-09-13 16:50:53 +00:00
|
|
|
|
2005-09-14 15:09:06 +00:00
|
|
|
/* main loop */
|
|
|
|
mode = SOURCES_MODE;
|
2005-09-13 16:50:53 +00:00
|
|
|
argv += optind;
|
|
|
|
if(*argv) {
|
|
|
|
while(*argv)
|
|
|
|
scan_dir_find_ch_files(*argv++);
|
|
|
|
} else {
|
|
|
|
scan_dir_find_ch_files(".");
|
|
|
|
}
|
2005-10-10 11:35:17 +00:00
|
|
|
store_keys();
|
2005-09-12 12:33:27 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-09-14 15:09:06 +00:00
|
|
|
/* partial and simplified libbb routine */
|
2005-09-12 16:39:47 +00:00
|
|
|
static void bb_error_d(const char *s, ...)
|
2005-09-12 12:33:27 +00:00
|
|
|
{
|
|
|
|
va_list p;
|
|
|
|
|
|
|
|
va_start(p, s);
|
|
|
|
vfprintf(stderr, s, p);
|
|
|
|
va_end(p);
|
|
|
|
putc('\n', stderr);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2005-09-12 16:39:47 +00:00
|
|
|
static char *bb_asprint(const char *format, ...)
|
2005-09-12 12:33:27 +00:00
|
|
|
{
|
|
|
|
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 15:09:06 +00:00
|
|
|
/* partial libbb routine as is */
|
2005-09-12 12:33:27 +00:00
|
|
|
|
2005-09-12 16:39:47 +00:00
|
|
|
static char *bb_simplify_path(const char *path)
|
2005-09-12 12:33:27 +00:00
|
|
|
{
|
|
|
|
char *s, *start, *p;
|
|
|
|
|
|
|
|
if (path[0] == '/')
|
2005-09-13 16:50:53 +00:00
|
|
|
start = bb_xstrdup(path);
|
2005-09-12 12:33:27 +00:00
|
|
|
else {
|
2005-09-13 16:50:53 +00:00
|
|
|
/* is not libbb, but this program have not chdir() */
|
|
|
|
start = bb_asprint("%s/%s", pwd, path);
|
2005-09-12 12:33:27 +00:00
|
|
|
}
|
|
|
|
p = s = start;
|
|
|
|
|
|
|
|
do {
|
2005-09-12 16:39:47 +00:00
|
|
|
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 12:33:27 +00:00
|
|
|
}
|
2005-09-12 16:39:47 +00:00
|
|
|
continue;
|
|
|
|
}
|
2005-09-12 12:33:27 +00:00
|
|
|
}
|
2005-09-12 16:39:47 +00:00
|
|
|
}
|
|
|
|
*++p = *s;
|
2005-09-12 12:33:27 +00:00
|
|
|
} while (*++s);
|
|
|
|
|
2005-09-12 16:39:47 +00:00
|
|
|
if ((p == start) || (*p != '/')) { /* not a trailing slash */
|
|
|
|
++p; /* so keep last character */
|
2005-09-12 12:33:27 +00:00
|
|
|
}
|
|
|
|
*p = 0;
|
|
|
|
|
|
|
|
return start;
|
|
|
|
}
|