"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 1 | /* |
"Vladimir N. Oleynik" | 6c0642d | 2005-10-07 15:36:26 +0000 | [diff] [blame] | 2 | * Another fast dependencies generator for Makefiles, Version 2.5 |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2005 by Vladimir Oleynik <dzo@simtreas.ru> |
"Vladimir N. Oleynik" | af0dd59 | 2005-09-16 13:57:33 +0000 | [diff] [blame] | 5 | * mmaping file may be originally by Linus Torvalds. |
| 6 | * |
| 7 | * (c) 2005 Bernhard Fischer: |
| 8 | * - commentary typos, |
| 9 | * - move "memory exhausted" into msg_enomem, |
| 10 | * - more verbose --help output. |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 11 | * |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 12 | * This program does: |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 13 | * 1) find #define KEY VALUE or #undef KEY from include/config.h |
| 14 | * 2) save include/config/key*.h if changed after previous usage |
"Vladimir N. Oleynik" | 664c6e7 | 2005-10-06 14:53:43 +0000 | [diff] [blame] | 15 | * 3) recursive find and scan *.[ch] files, but skips scan of include/config/ |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 16 | * 4) find #include "*.h" and KEYs using, if not as #define and #undef |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 17 | * 5) generate dependencies to stdout |
"Vladimir N. Oleynik" | 664c6e7 | 2005-10-06 14:53:43 +0000 | [diff] [blame] | 18 | * pwd/file.o: include/config/key*.h found_include_*.h |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 19 | * path/inc.h: include/config/key*.h found_included_include_*.h |
"Vladimir N. Oleynik" | af0dd59 | 2005-09-16 13:57:33 +0000 | [diff] [blame] | 20 | * This program does not generate dependencies for #include <...> |
"Vladimir N. Oleynik" | d128b71 | 2005-10-03 10:08:46 +0000 | [diff] [blame] | 21 | * BUG: all includes name must unique |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 22 | */ |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 23 | |
| 24 | #define LOCAL_INCLUDE_PATH "include" |
| 25 | #define INCLUDE_CONFIG_PATH LOCAL_INCLUDE_PATH"/config" |
| 26 | #define INCLUDE_CONFIG_KEYS_PATH LOCAL_INCLUDE_PATH"/config.h" |
| 27 | |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 28 | #define bb_mkdep_full_options \ |
| 29 | "\nOptions:" \ |
| 30 | "\n\t-I local_include_path include paths, default: \"" LOCAL_INCLUDE_PATH "\"" \ |
| 31 | "\n\t-d don't generate depend" \ |
| 32 | "\n\t-w show warning if include files not found" \ |
| 33 | "\n\t-k include/config default: \"" INCLUDE_CONFIG_PATH "\"" \ |
| 34 | "\n\t-c include/config.h configs, default: \"" INCLUDE_CONFIG_KEYS_PATH "\"" \ |
| 35 | "\n\tdirs_to_scan default \".\"" |
| 36 | |
| 37 | #define bb_mkdep_terse_options "Usage: [-I local_include_paths] [-dw] " \ |
| 38 | "[-k path_for_stored_keys] [dirs]" |
| 39 | |
| 40 | |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 41 | #define _GNU_SOURCE |
| 42 | #include <sys/types.h> |
| 43 | #include <sys/stat.h> |
| 44 | #include <sys/mman.h> |
| 45 | #include <getopt.h> |
| 46 | #include <dirent.h> |
| 47 | #include <stdio.h> |
| 48 | #include <stdlib.h> |
| 49 | #include <string.h> |
| 50 | #include <stdarg.h> |
| 51 | #include <unistd.h> |
| 52 | #include <errno.h> |
| 53 | #include <fcntl.h> |
| 54 | |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 55 | |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 56 | typedef struct BB_KEYS { |
| 57 | char *keyname; |
"Vladimir N. Oleynik" | d128b71 | 2005-10-03 10:08:46 +0000 | [diff] [blame] | 58 | size_t key_sz; |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 59 | const char *value; |
| 60 | char *stored_path; |
"Vladimir N. Oleynik" | d128b71 | 2005-10-03 10:08:46 +0000 | [diff] [blame] | 61 | char *checked; |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 62 | struct BB_KEYS *next; |
| 63 | } bb_key_t; |
| 64 | |
"Vladimir N. Oleynik" | d128b71 | 2005-10-03 10:08:46 +0000 | [diff] [blame] | 65 | static bb_key_t *check_key(bb_key_t *k, const char *nk, size_t key_sz); |
| 66 | static bb_key_t *make_new_key(bb_key_t *k, const char *nk, size_t key_sz); |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 67 | |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 68 | /* partial and simplified libbb routine */ |
"Vladimir N. Oleynik" | b1fe462 | 2005-09-12 16:39:47 +0000 | [diff] [blame] | 69 | static void bb_error_d(const char *s, ...) __attribute__ ((noreturn, format (printf, 1, 2))); |
| 70 | static char * bb_asprint(const char *format, ...) __attribute__ ((format (printf, 1, 2))); |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 71 | |
| 72 | /* stolen from libbb as is */ |
| 73 | typedef struct llist_s { |
| 74 | char *data; |
| 75 | struct llist_s *link; |
| 76 | } llist_t; |
"Vladimir N. Oleynik" | b1fe462 | 2005-09-12 16:39:47 +0000 | [diff] [blame] | 77 | static void *xrealloc(void *p, size_t size); |
| 78 | static void *xmalloc(size_t size); |
| 79 | static char *bb_xstrdup(const char *s); |
| 80 | static char *bb_simplify_path(const char *path); |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 81 | /* error messages */ |
| 82 | static const char msg_enomem[] = "memory exhausted"; |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 83 | |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 84 | /* for lexical analyser */ |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 85 | static bb_key_t *key_top; |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 86 | static llist_t *configs; |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 87 | |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 88 | static int mode; |
| 89 | #define CONFIG_MODE 0 |
| 90 | #define SOURCES_MODE 1 |
| 91 | |
"Vladimir N. Oleynik" | d128b71 | 2005-10-03 10:08:46 +0000 | [diff] [blame] | 92 | static void parse_inc(const char *include, const char *fname, size_t key_sz); |
| 93 | static void parse_conf_opt(const char *opt, const char *val, |
| 94 | size_t rsz, size_t key_sz); |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 95 | |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 96 | /* for speed tricks */ |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 97 | static char first_chars[257]; /* + L_EOF */ |
"Vladimir N. Oleynik" | af0dd59 | 2005-09-16 13:57:33 +0000 | [diff] [blame] | 98 | static char isalnums[257]; /* + L_EOF */ |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 99 | /* trick for fast find "define", "include", "undef" */ |
"Vladimir N. Oleynik" | 6c0642d | 2005-10-07 15:36:26 +0000 | [diff] [blame] | 100 | static char first_chars_diu[256] = { |
| 101 | [(int)'d'] = (char)5, /* strlen("define") - 1; */ |
| 102 | [(int)'i'] = (char)6, /* strlen("include") - 1; */ |
| 103 | [(int)'u'] = (char)4, /* strlen("undef") - 1; */ |
| 104 | }; |
"Vladimir N. Oleynik" | b1fe462 | 2005-09-12 16:39:47 +0000 | [diff] [blame] | 105 | |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 106 | static int pagesizem1; |
| 107 | static size_t mema_id = 128; /* first allocated for id */ |
| 108 | static char *id_s; |
| 109 | |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 110 | |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 111 | #define yy_error_d(s) bb_error_d("%s:%d hmm, %s", fname, line, s) |
| 112 | |
| 113 | /* state */ |
| 114 | #define S 0 /* start state */ |
| 115 | #define STR '"' /* string */ |
| 116 | #define CHR '\'' /* char */ |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 117 | #define REM '/' /* block comment */ |
| 118 | #define BS '\\' /* back slash */ |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 119 | #define POUND '#' /* # */ |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 120 | #define I 'i' /* #include preprocessor's directive */ |
| 121 | #define D 'd' /* #define preprocessor's directive */ |
| 122 | #define U 'u' /* #undef preprocessor's directive */ |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 123 | #define LI 'I' /* #include "... */ |
| 124 | #define DK 'K' /* #define KEY... (config mode) */ |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 125 | #define DV 'V' /* #define KEY "VALUE or #define KEY 'VALUE */ |
| 126 | #define NLC 'n' /* \ and \n */ |
| 127 | #define ANY '*' /* any unparsed chars */ |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 128 | |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 129 | #define L_EOF 256 |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 130 | /* [A-Z_a-z] */ |
| 131 | #define ID(c) ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c == '_') |
| 132 | /* [A-Z_a-z0-9] */ |
| 133 | #define ISALNUM(c) (ID(c) || (c >= '0' && c <= '9')) |
| 134 | |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 135 | #define getc1() do { c = (optr >= oend) ? L_EOF : *optr++; } while(0) |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 136 | #define ungetc1() optr-- |
| 137 | |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 138 | #define put_id(c) do { if(id_len == local_mema_id) \ |
| 139 | id = xrealloc(id, local_mema_id += 16); \ |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 140 | id[id_len++] = c; } while(0) |
| 141 | |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 142 | |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 143 | /* stupid C lexical analyser */ |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 144 | static void c_lex(const char *fname, long fsize) |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 145 | { |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 146 | int c = L_EOF; /* stupid initialize */ |
| 147 | int prev_state = L_EOF; |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 148 | int called; |
| 149 | int state; |
| 150 | int line; |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 151 | char *id = id_s; |
| 152 | size_t local_mema_id = mema_id; |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 153 | size_t id_len = 0; /* stupid initialize */ |
| 154 | char *val = NULL; |
| 155 | unsigned char *optr, *oend; |
| 156 | unsigned char *start = NULL; /* stupid initialize */ |
"Vladimir N. Oleynik" | d128b71 | 2005-10-03 10:08:46 +0000 | [diff] [blame] | 157 | size_t opt_len = 0; /* stupid initialize */ |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 158 | |
| 159 | int fd; |
| 160 | char *map; |
| 161 | int mapsize; |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 162 | |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 163 | if(fsize == 0) { |
| 164 | fprintf(stderr, "Warning: %s is empty\n", fname); |
| 165 | return; |
| 166 | } |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 167 | fd = open(fname, O_RDONLY); |
| 168 | if(fd < 0) { |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 169 | perror(fname); |
| 170 | return; |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 171 | } |
| 172 | mapsize = (fsize+pagesizem1) & ~pagesizem1; |
| 173 | map = mmap(NULL, mapsize, PROT_READ, MAP_PRIVATE, fd, 0); |
| 174 | if ((long) map == -1) |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 175 | bb_error_d("%s: mmap: %m", fname); |
| 176 | |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 177 | optr = (unsigned char *)map; |
| 178 | oend = optr + fsize; |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 179 | |
| 180 | line = 1; |
| 181 | called = state = S; |
| 182 | |
| 183 | for(;;) { |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 184 | if(prev_state != state) { |
| 185 | prev_state = state; |
| 186 | getc1(); |
| 187 | } |
| 188 | |
| 189 | /* [ \t]+ eat first space */ |
| 190 | while(c == ' ' || c == '\t') |
| 191 | getc1(); |
| 192 | |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 193 | if(c == BS) { |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 194 | getc1(); |
| 195 | if(c == '\n') { |
| 196 | /* \\\n eat continued */ |
| 197 | line++; |
| 198 | prev_state = NLC; |
| 199 | continue; |
| 200 | } |
| 201 | ungetc1(); |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 202 | c = BS; |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | if(state == S) { |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 206 | while(first_chars[c] == ANY) { |
| 207 | /* <S>unparsed */ |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 208 | if(c == '\n') |
| 209 | line++; |
| 210 | getc1(); |
| 211 | } |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 212 | if(c == L_EOF) { |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 213 | /* <S><<EOF>> */ |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 214 | id_s = id; |
| 215 | mema_id = local_mema_id; |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 216 | munmap(map, mapsize); |
| 217 | close(fd); |
| 218 | return; |
| 219 | } |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 220 | if(c == REM) { |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 221 | /* <S>/ */ |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 222 | getc1(); /* eat <S>/ */ |
| 223 | if(c == REM) { |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 224 | /* <S>"//"[^\n]* */ |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 225 | do getc1(); while(c != '\n' && c != L_EOF); |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 226 | } else if(c == '*') { |
| 227 | /* <S>[/][*] */ |
| 228 | called = S; |
| 229 | state = REM; |
| 230 | } |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 231 | } else if(c == POUND) { |
| 232 | /* <S># */ |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 233 | start = optr - 1; |
| 234 | state = c; |
| 235 | } else if(c == STR || c == CHR) { |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 236 | /* <S>\"|\' */ |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 237 | val = NULL; |
| 238 | called = S; |
| 239 | state = c; |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 240 | } else if(c != BS) { |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 241 | /* <S>[A-Z_a-z0-9] */ |
"Vladimir N. Oleynik" | b1fe462 | 2005-09-12 16:39:47 +0000 | [diff] [blame] | 242 | |
| 243 | /* trick for fast drop id |
| 244 | if key with this first char undefined */ |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 245 | if(first_chars[c] == 0) { |
"Vladimir N. Oleynik" | b1fe462 | 2005-09-12 16:39:47 +0000 | [diff] [blame] | 246 | /* skip <S>[A-Z_a-z0-9]+ */ |
"Vladimir N. Oleynik" | af0dd59 | 2005-09-16 13:57:33 +0000 | [diff] [blame] | 247 | do getc1(); while(isalnums[c]); |
"Vladimir N. Oleynik" | b1fe462 | 2005-09-12 16:39:47 +0000 | [diff] [blame] | 248 | } else { |
| 249 | id_len = 0; |
| 250 | do { |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 251 | /* <S>[A-Z_a-z0-9]+ */ |
| 252 | put_id(c); |
| 253 | getc1(); |
"Vladimir N. Oleynik" | af0dd59 | 2005-09-16 13:57:33 +0000 | [diff] [blame] | 254 | } while(isalnums[c]); |
"Vladimir N. Oleynik" | d128b71 | 2005-10-03 10:08:46 +0000 | [diff] [blame] | 255 | check_key(key_top, id, id_len); |
"Vladimir N. Oleynik" | b1fe462 | 2005-09-12 16:39:47 +0000 | [diff] [blame] | 256 | } |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 257 | } else { |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 258 | /* <S>\\ */ |
| 259 | prev_state = c; |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 260 | } |
| 261 | continue; |
| 262 | } |
| 263 | if(state == REM) { |
| 264 | for(;;) { |
| 265 | /* <REM>[^*]+ */ |
| 266 | while(c != '*') { |
| 267 | if(c == '\n') { |
| 268 | /* <REM>\n */ |
| 269 | if(called != S) |
| 270 | yy_error_d("unexpected newline"); |
| 271 | line++; |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 272 | } else if(c == L_EOF) |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 273 | yy_error_d("unexpected EOF"); |
| 274 | getc1(); |
| 275 | } |
| 276 | /* <REM>[*] */ |
| 277 | getc1(); |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 278 | if(c == REM) { |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 279 | /* <REM>[*][/] */ |
| 280 | state = called; |
| 281 | break; |
| 282 | } |
| 283 | } |
| 284 | continue; |
| 285 | } |
| 286 | if(state == STR || state == CHR) { |
| 287 | for(;;) { |
| 288 | /* <STR,CHR>\n|<<EOF>> */ |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 289 | if(c == '\n' || c == L_EOF) |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 290 | yy_error_d("unterminated"); |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 291 | if(c == BS) { |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 292 | /* <STR,CHR>\\ */ |
| 293 | getc1(); |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 294 | if(c != BS && c != '\n' && c != state) { |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 295 | /* another usage \ in str or char */ |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 296 | if(c == L_EOF) |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 297 | yy_error_d("unexpected EOF"); |
| 298 | if(val) |
| 299 | put_id(c); |
| 300 | continue; |
| 301 | } |
| 302 | /* <STR,CHR>\\[\\\n] or <STR>\\\" or <CHR>\\\' */ |
| 303 | /* eat 2 char */ |
| 304 | if(c == '\n') |
| 305 | line++; |
| 306 | else if(val) |
| 307 | put_id(c); |
| 308 | } else if(c == state) { |
| 309 | /* <STR>\" or <CHR>\' */ |
"Vladimir N. Oleynik" | d128b71 | 2005-10-03 10:08:46 +0000 | [diff] [blame] | 310 | if(called == LI) { |
| 311 | /* store "include.h" */ |
| 312 | parse_inc(id, fname, id_len); |
| 313 | } else if(called == DV) { |
| 314 | put_id(c); /* config mode #define KEY "VAL"<- */ |
| 315 | put_id(0); |
| 316 | parse_conf_opt(id, val, (optr - start), opt_len); |
| 317 | } |
| 318 | state = S; |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 319 | break; |
| 320 | } else if(val) |
| 321 | put_id(c); |
| 322 | /* <STR,CHR>. */ |
| 323 | getc1(); |
| 324 | } |
| 325 | continue; |
| 326 | } |
| 327 | |
| 328 | /* begin preprocessor states */ |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 329 | if(c == L_EOF) |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 330 | yy_error_d("unexpected EOF"); |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 331 | if(c == REM) { |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 332 | /* <#.*>/ */ |
| 333 | getc1(); |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 334 | if(c == REM) |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 335 | yy_error_d("detected // in preprocessor line"); |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 336 | if(c == '*') { |
| 337 | /* <#.*>[/][*] */ |
| 338 | called = state; |
| 339 | state = REM; |
| 340 | continue; |
| 341 | } |
| 342 | /* hmm, #.*[/] */ |
| 343 | yy_error_d("strange preprocessor line"); |
| 344 | } |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 345 | if(state == POUND) { |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 346 | /* tricks */ |
| 347 | static const char * const preproc[] = { |
"Vladimir N. Oleynik" | d128b71 | 2005-10-03 10:08:46 +0000 | [diff] [blame] | 348 | /* 0-3 */ |
| 349 | "", "", "", "", |
| 350 | /* 4 */ /* 5 */ /* 6 */ |
| 351 | "ndef", "efine", "nclude", |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 352 | }; |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 353 | size_t diu = first_chars_diu[c]; /* strlen and preproc ptr */ |
"Vladimir N. Oleynik" | d128b71 | 2005-10-03 10:08:46 +0000 | [diff] [blame] | 354 | const unsigned char *p = optr; |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 355 | |
"Vladimir N. Oleynik" | af0dd59 | 2005-09-16 13:57:33 +0000 | [diff] [blame] | 356 | while(isalnums[c]) getc1(); |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 357 | /* have str begined with c, readed == strlen key and compared */ |
"Vladimir N. Oleynik" | af0dd59 | 2005-09-16 13:57:33 +0000 | [diff] [blame] | 358 | if(diu != S && diu == (optr-p-1) && !memcmp(p, preproc[diu], diu)) { |
"Vladimir N. Oleynik" | d128b71 | 2005-10-03 10:08:46 +0000 | [diff] [blame] | 359 | state = p[-1]; |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 360 | id_len = 0; /* common for save */ |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 361 | } else { |
| 362 | state = S; |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 363 | } |
| 364 | ungetc1(); |
| 365 | continue; |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 366 | } |
| 367 | if(state == I) { |
| 368 | if(c == STR) { |
| 369 | /* <I>\" */ |
| 370 | val = id; |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 371 | called = LI; |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 372 | state = STR; |
| 373 | } else { |
| 374 | /* another (may be wrong) #include ... */ |
| 375 | ungetc1(); |
| 376 | state = S; |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 377 | } |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 378 | continue; |
| 379 | } |
| 380 | if(state == D || state == U) { |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 381 | if(mode == SOURCES_MODE) { |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 382 | /* ignore depend with #define or #undef KEY */ |
"Vladimir N. Oleynik" | af0dd59 | 2005-09-16 13:57:33 +0000 | [diff] [blame] | 383 | while(isalnums[c]) |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 384 | getc1(); |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 385 | state = S; |
| 386 | } else { |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 387 | /* save KEY from #"define"|"undef" ... */ |
"Vladimir N. Oleynik" | af0dd59 | 2005-09-16 13:57:33 +0000 | [diff] [blame] | 388 | while(isalnums[c]) { |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 389 | put_id(c); |
| 390 | getc1(); |
| 391 | } |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 392 | if(!id_len) |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 393 | yy_error_d("expected identifier"); |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 394 | if(state == U) { |
"Vladimir N. Oleynik" | d128b71 | 2005-10-03 10:08:46 +0000 | [diff] [blame] | 395 | parse_conf_opt(id, NULL, (optr - start), id_len); |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 396 | state = S; |
| 397 | } else { |
| 398 | /* D -> DK */ |
"Vladimir N. Oleynik" | d128b71 | 2005-10-03 10:08:46 +0000 | [diff] [blame] | 399 | opt_len = id_len; |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 400 | state = DK; |
| 401 | } |
| 402 | } |
| 403 | ungetc1(); |
| 404 | continue; |
| 405 | } |
| 406 | if(state == DK) { |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 407 | /* #define KEY[ ] (config mode) */ |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 408 | val = id + id_len; |
| 409 | if(c == STR || c == CHR) { |
| 410 | /* define KEY "... or define KEY '... */ |
| 411 | put_id(c); |
| 412 | called = DV; |
| 413 | state = c; |
| 414 | continue; |
| 415 | } |
"Vladimir N. Oleynik" | af0dd59 | 2005-09-16 13:57:33 +0000 | [diff] [blame] | 416 | while(isalnums[c]) { |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 417 | /* VALUE */ |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 418 | put_id(c); |
| 419 | getc1(); |
| 420 | } |
"Vladimir N. Oleynik" | d128b71 | 2005-10-03 10:08:46 +0000 | [diff] [blame] | 421 | put_id(0); |
| 422 | parse_conf_opt(id, val, (optr - start), opt_len); |
| 423 | state = S; |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 424 | ungetc1(); |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 425 | continue; |
| 426 | } |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | |
| 431 | static void show_usage(void) __attribute__ ((noreturn)); |
| 432 | static void show_usage(void) |
| 433 | { |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 434 | bb_error_d("%s\n%s\n", bb_mkdep_terse_options, bb_mkdep_full_options); |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | static const char *kp; |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 438 | static size_t kp_len; |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 439 | static llist_t *Iop; |
| 440 | static bb_key_t *Ifound; |
| 441 | static int noiwarning; |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 442 | |
"Vladimir N. Oleynik" | d128b71 | 2005-10-03 10:08:46 +0000 | [diff] [blame] | 443 | static bb_key_t *check_key(bb_key_t *k, const char *nk, size_t key_sz) |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 444 | { |
| 445 | bb_key_t *cur; |
| 446 | |
| 447 | for(cur = k; cur; cur = cur->next) { |
"Vladimir N. Oleynik" | d128b71 | 2005-10-03 10:08:46 +0000 | [diff] [blame] | 448 | if(key_sz == cur->key_sz && memcmp(cur->keyname, nk, key_sz) == 0) { |
| 449 | cur->checked = cur->stored_path; |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 450 | return cur; |
| 451 | } |
| 452 | } |
| 453 | return NULL; |
| 454 | } |
| 455 | |
"Vladimir N. Oleynik" | d128b71 | 2005-10-03 10:08:46 +0000 | [diff] [blame] | 456 | static bb_key_t *make_new_key(bb_key_t *k, const char *nk, size_t key_sz) |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 457 | { |
| 458 | bb_key_t *cur; |
| 459 | |
"Vladimir N. Oleynik" | d128b71 | 2005-10-03 10:08:46 +0000 | [diff] [blame] | 460 | cur = xmalloc(sizeof(bb_key_t) + key_sz + 1); |
| 461 | cur->keyname = memcpy(cur + 1, nk, key_sz); |
| 462 | cur->keyname[key_sz] = '\0'; |
| 463 | cur->key_sz = key_sz; |
| 464 | cur->checked = NULL; |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 465 | cur->next = k; |
| 466 | return cur; |
| 467 | } |
| 468 | |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 469 | static inline char *store_include_fullpath(char *p_i, bb_key_t *li) |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 470 | { |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 471 | char *ok; |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 472 | |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 473 | if(access(p_i, F_OK) == 0) { |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 474 | ok = li->stored_path = bb_simplify_path(p_i); |
"Vladimir N. Oleynik" | d128b71 | 2005-10-03 10:08:46 +0000 | [diff] [blame] | 475 | li->checked = ok; |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 476 | } else { |
| 477 | ok = NULL; |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 478 | } |
| 479 | free(p_i); |
| 480 | return ok; |
| 481 | } |
| 482 | |
"Vladimir N. Oleynik" | d128b71 | 2005-10-03 10:08:46 +0000 | [diff] [blame] | 483 | static void parse_inc(const char *include, const char *fname, size_t key_sz) |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 484 | { |
| 485 | bb_key_t *li; |
| 486 | char *p_i; |
| 487 | llist_t *lo; |
| 488 | |
"Vladimir N. Oleynik" | d128b71 | 2005-10-03 10:08:46 +0000 | [diff] [blame] | 489 | li = check_key(Ifound, include, key_sz); |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 490 | if(li) |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 491 | return; |
"Vladimir N. Oleynik" | d128b71 | 2005-10-03 10:08:46 +0000 | [diff] [blame] | 492 | Ifound = li = make_new_key(Ifound, include, key_sz); |
| 493 | include = li->keyname; |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 494 | if(include[0] != '/') { |
| 495 | /* relative */ |
| 496 | int w; |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 497 | |
"Vladimir N. Oleynik" | d128b71 | 2005-10-03 10:08:46 +0000 | [diff] [blame] | 498 | p_i = strrchr(fname, '/'); /* fname have absolute pathname */ |
| 499 | w = (p_i-fname); |
| 500 | /* find from current directory of source file */ |
| 501 | p_i = bb_asprint("%.*s/%s", w, fname, include); |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 502 | if(store_include_fullpath(p_i, li)) |
| 503 | return; |
"Vladimir N. Oleynik" | d128b71 | 2005-10-03 10:08:46 +0000 | [diff] [blame] | 504 | /* find from "-I include" specified directories */ |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 505 | for(lo = Iop; lo; lo = lo->link) { |
| 506 | p_i = bb_asprint("%s/%s", lo->data, include); |
| 507 | if(store_include_fullpath(p_i, li)) |
| 508 | return; |
| 509 | } |
| 510 | } else { |
| 511 | /* absolute include pathname */ |
| 512 | if(access(include, F_OK) == 0) { |
"Vladimir N. Oleynik" | d128b71 | 2005-10-03 10:08:46 +0000 | [diff] [blame] | 513 | li->checked = li->stored_path = bb_xstrdup(include); |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 514 | return; |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 515 | } |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 516 | } |
| 517 | li->stored_path = NULL; |
| 518 | if(noiwarning) |
| 519 | fprintf(stderr, "%s: Warning: #include \"%s\" not found in specified paths\n", fname, include); |
| 520 | } |
| 521 | |
"Vladimir N. Oleynik" | d128b71 | 2005-10-03 10:08:46 +0000 | [diff] [blame] | 522 | static void parse_conf_opt(const char *opt, const char *val, |
| 523 | size_t recordsz, size_t key_sz) |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 524 | { |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 525 | bb_key_t *cur; |
"Vladimir N. Oleynik" | d128b71 | 2005-10-03 10:08:46 +0000 | [diff] [blame] | 526 | char *k; |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 527 | char *s, *p; |
| 528 | struct stat st; |
| 529 | int fd; |
| 530 | int cmp_ok = 0; |
| 531 | static char *record_buf; |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 532 | static size_t r_sz; |
| 533 | ssize_t rw_ret; |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 534 | |
"Vladimir N. Oleynik" | d128b71 | 2005-10-03 10:08:46 +0000 | [diff] [blame] | 535 | cur = check_key(key_top, opt, key_sz); |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 536 | if(cur != NULL) { |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 537 | /* present already */ |
"Vladimir N. Oleynik" | d128b71 | 2005-10-03 10:08:46 +0000 | [diff] [blame] | 538 | cur->checked = NULL; /* store only */ |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 539 | if(cur->value == NULL && val == NULL) |
| 540 | return; |
"Vladimir N. Oleynik" | d128b71 | 2005-10-03 10:08:46 +0000 | [diff] [blame] | 541 | if(cur->value != NULL && val != NULL && !strcmp(cur->value, val)) |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 542 | return; |
"Vladimir N. Oleynik" | d128b71 | 2005-10-03 10:08:46 +0000 | [diff] [blame] | 543 | k = cur->keyname; |
| 544 | fprintf(stderr, "Warning: redefined %s\n", k); |
"Vladimir N. Oleynik" | af0dd59 | 2005-09-16 13:57:33 +0000 | [diff] [blame] | 545 | } else { |
"Vladimir N. Oleynik" | d128b71 | 2005-10-03 10:08:46 +0000 | [diff] [blame] | 546 | key_top = cur = make_new_key(key_top, opt, key_sz); |
| 547 | k = cur->keyname; |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 548 | } |
"Vladimir N. Oleynik" | af0dd59 | 2005-09-16 13:57:33 +0000 | [diff] [blame] | 549 | /* do generate record */ |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 550 | recordsz += 2; /* \n\0 */ |
"Vladimir N. Oleynik" | d128b71 | 2005-10-03 10:08:46 +0000 | [diff] [blame] | 551 | if(recordsz > r_sz) |
| 552 | record_buf = xrealloc(record_buf, (r_sz = recordsz) * 2); |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 553 | s = record_buf; |
| 554 | /* may be short count " " */ |
| 555 | if(val) { |
| 556 | if(*val == '\0') { |
| 557 | cur->value = ""; |
"Vladimir N. Oleynik" | d128b71 | 2005-10-03 10:08:46 +0000 | [diff] [blame] | 558 | recordsz = sprintf(s, "#define %s\n", k); |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 559 | } else { |
| 560 | cur->value = bb_xstrdup(val); |
"Vladimir N. Oleynik" | d128b71 | 2005-10-03 10:08:46 +0000 | [diff] [blame] | 561 | recordsz = sprintf(s, "#define %s %s\n", k, val); |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 562 | } |
| 563 | } else { |
| 564 | cur->value = NULL; |
"Vladimir N. Oleynik" | d128b71 | 2005-10-03 10:08:46 +0000 | [diff] [blame] | 565 | recordsz = sprintf(s, "#undef %s\n", k); |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 566 | } |
| 567 | /* size_t -> ssize_t :( */ |
| 568 | rw_ret = (ssize_t)recordsz; |
| 569 | /* trick, save first char KEY for do fast identify id */ |
"Vladimir N. Oleynik" | d128b71 | 2005-10-03 10:08:46 +0000 | [diff] [blame] | 570 | first_chars[(int)*k] = *k; |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 571 | |
"Vladimir N. Oleynik" | d128b71 | 2005-10-03 10:08:46 +0000 | [diff] [blame] | 572 | cur->stored_path = k = bb_asprint("%s/%s.h", kp, k); |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 573 | /* key converting [A-Z_] -> [a-z/] */ |
"Vladimir N. Oleynik" | d128b71 | 2005-10-03 10:08:46 +0000 | [diff] [blame] | 574 | for(p = k + kp_len + 1; *p; p++) { |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 575 | if(*p >= 'A' && *p <= 'Z') |
| 576 | *p = *p - 'A' + 'a'; |
"Vladimir N. Oleynik" | af0dd59 | 2005-09-16 13:57:33 +0000 | [diff] [blame] | 577 | else if(*p == '_' && p[1] > '9') /* do not change A_1 to A/1 */ |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 578 | *p = '/'; |
| 579 | } |
"Vladimir N. Oleynik" | af0dd59 | 2005-09-16 13:57:33 +0000 | [diff] [blame] | 580 | /* check kp/key.h if present after previous usage */ |
"Vladimir N. Oleynik" | d128b71 | 2005-10-03 10:08:46 +0000 | [diff] [blame] | 581 | if(stat(k, &st)) { |
| 582 | for(p = k + kp_len + 1; *p; p++) { |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 583 | /* Auto-create directories. */ |
| 584 | if (*p == '/') { |
| 585 | *p = '\0'; |
"Vladimir N. Oleynik" | d128b71 | 2005-10-03 10:08:46 +0000 | [diff] [blame] | 586 | if (access(k, F_OK) != 0 && mkdir(k, 0755) != 0) |
| 587 | bb_error_d("mkdir(%s): %m", k); |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 588 | *p = '/'; |
| 589 | } |
| 590 | } |
| 591 | } else { |
| 592 | /* found */ |
| 593 | if(st.st_size == (off_t)recordsz) { |
"Vladimir N. Oleynik" | d128b71 | 2005-10-03 10:08:46 +0000 | [diff] [blame] | 594 | char *r_cmp = s + recordsz; |
| 595 | |
| 596 | fd = open(k, O_RDONLY); |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 597 | if(fd < 0 || read(fd, r_cmp, recordsz) < rw_ret) |
"Vladimir N. Oleynik" | d128b71 | 2005-10-03 10:08:46 +0000 | [diff] [blame] | 598 | bb_error_d("%s: %m", k); |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 599 | close(fd); |
| 600 | cmp_ok = memcmp(s, r_cmp, recordsz) == 0; |
| 601 | } |
| 602 | } |
| 603 | if(!cmp_ok) { |
"Vladimir N. Oleynik" | d128b71 | 2005-10-03 10:08:46 +0000 | [diff] [blame] | 604 | fd = open(k, O_WRONLY|O_CREAT|O_TRUNC, 0644); |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 605 | if(fd < 0 || write(fd, s, recordsz) < rw_ret) |
"Vladimir N. Oleynik" | d128b71 | 2005-10-03 10:08:46 +0000 | [diff] [blame] | 606 | bb_error_d("%s: %m", k); |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 607 | close(fd); |
| 608 | } |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 609 | } |
| 610 | |
"Vladimir N. Oleynik" | 6c0642d | 2005-10-07 15:36:26 +0000 | [diff] [blame] | 611 | static char *pwd; |
| 612 | |
| 613 | static int show_dep(int first, bb_key_t *k, const char *name, const char *f) |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 614 | { |
| 615 | bb_key_t *cur; |
| 616 | |
| 617 | for(cur = k; cur; cur = cur->next) { |
"Vladimir N. Oleynik" | d128b71 | 2005-10-03 10:08:46 +0000 | [diff] [blame] | 618 | if(cur->checked) { |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 619 | if(first) { |
"Vladimir N. Oleynik" | 6c0642d | 2005-10-07 15:36:26 +0000 | [diff] [blame] | 620 | if(f == NULL) |
| 621 | printf("\n%s:", name); |
| 622 | else |
| 623 | printf("\n%s/%s:", pwd, name); |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 624 | first = 0; |
| 625 | } else { |
| 626 | printf(" \\\n "); |
| 627 | } |
"Vladimir N. Oleynik" | d128b71 | 2005-10-03 10:08:46 +0000 | [diff] [blame] | 628 | printf(" %s", cur->checked); |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 629 | } |
"Vladimir N. Oleynik" | d128b71 | 2005-10-03 10:08:46 +0000 | [diff] [blame] | 630 | cur->checked = NULL; |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 631 | } |
| 632 | return first; |
| 633 | } |
| 634 | |
"Vladimir N. Oleynik" | 6c0642d | 2005-10-07 15:36:26 +0000 | [diff] [blame] | 635 | static size_t replace; |
"Vladimir N. Oleynik" | d5f2a18 | 2005-10-06 14:47:16 +0000 | [diff] [blame] | 636 | |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 637 | static struct stat st_kp; |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 638 | static int dontgenerate_dep; |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 639 | |
| 640 | static char * |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 641 | parse_chd(const char *fe, const char *p, size_t dirlen) |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 642 | { |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 643 | struct stat st; |
| 644 | char *fp; |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 645 | size_t df_sz; |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 646 | static char *dir_and_entry; |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 647 | static size_t dir_and_entry_sz; |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 648 | |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 649 | df_sz = dirlen + strlen(fe) + 2; /* dir/file\0 */ |
| 650 | if(df_sz > dir_and_entry_sz) |
| 651 | dir_and_entry = xrealloc(dir_and_entry, dir_and_entry_sz = df_sz); |
| 652 | fp = dir_and_entry; |
| 653 | sprintf(fp, "%s/%s", p, fe); |
| 654 | |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 655 | if(stat(fp, &st)) { |
"Vladimir N. Oleynik" | d128b71 | 2005-10-03 10:08:46 +0000 | [diff] [blame] | 656 | fprintf(stderr, "Warning: stat(%s): %m\n", fp); |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 657 | return NULL; |
| 658 | } |
"Vladimir N. Oleynik" | b1fe462 | 2005-09-12 16:39:47 +0000 | [diff] [blame] | 659 | if(S_ISREG(st.st_mode)) { |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 660 | llist_t *cfl; |
| 661 | char *e = fp + df_sz - 3; |
| 662 | |
| 663 | if(*e++ != '.' || (*e != 'c' && *e != 'h')) { |
"Vladimir N. Oleynik" | b1fe462 | 2005-09-12 16:39:47 +0000 | [diff] [blame] | 664 | /* direntry is regular file, but is not *.[ch] */ |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 665 | return NULL; |
| 666 | } |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 667 | for(cfl = configs; cfl; cfl = cfl->link) { |
| 668 | struct stat *config = (struct stat *)cfl->data; |
| 669 | |
| 670 | if (st.st_dev == config->st_dev && st.st_ino == config->st_ino) { |
| 671 | /* skip already parsed configs.h */ |
"Vladimir N. Oleynik" | b1fe462 | 2005-09-12 16:39:47 +0000 | [diff] [blame] | 672 | return NULL; |
| 673 | } |
"Vladimir N. Oleynik" | b1fe462 | 2005-09-12 16:39:47 +0000 | [diff] [blame] | 674 | } |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 675 | /* direntry is *.[ch] regular file and is not configs */ |
| 676 | if(!dontgenerate_dep) { |
| 677 | int first; |
| 678 | |
| 679 | c_lex(fp, st.st_size); |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 680 | if(*e == 'c') { |
| 681 | /* *.c -> *.o */ |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 682 | *e = 'o'; |
"Vladimir N. Oleynik" | 6c0642d | 2005-10-07 15:36:26 +0000 | [diff] [blame] | 683 | /* /src_dir/path/file.o to path/file.o */ |
| 684 | fp += replace; |
| 685 | if(*fp == '/') |
| 686 | fp++; |
"Vladimir N. Oleynik" | d5f2a18 | 2005-10-06 14:47:16 +0000 | [diff] [blame] | 687 | } else { |
| 688 | e = NULL; |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 689 | } |
"Vladimir N. Oleynik" | 6c0642d | 2005-10-07 15:36:26 +0000 | [diff] [blame] | 690 | first = show_dep(1, Ifound, fp, e); |
| 691 | first = show_dep(first, key_top, fp, e); |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 692 | if(first == 0) |
| 693 | putchar('\n'); |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 694 | } |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 695 | return NULL; |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 696 | } else if(S_ISDIR(st.st_mode)) { |
| 697 | if (st.st_dev == st_kp.st_dev && st.st_ino == st_kp.st_ino) |
| 698 | return NULL; /* drop scan kp/ directory */ |
| 699 | /* direntry is directory. buff is returned, begin of zero allocate */ |
| 700 | dir_and_entry = NULL; |
| 701 | dir_and_entry_sz = 0; |
| 702 | return fp; |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 703 | } |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 704 | /* hmm, direntry is device! */ |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 705 | return NULL; |
| 706 | } |
| 707 | |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 708 | /* from libbb but inline for fast */ |
| 709 | static inline llist_t *llist_add_to(llist_t *old_head, char *new_item) |
| 710 | { |
| 711 | llist_t *new_head; |
| 712 | |
| 713 | new_head = xmalloc(sizeof(llist_t)); |
| 714 | new_head->data = new_item; |
| 715 | new_head->link = old_head; |
| 716 | |
| 717 | return(new_head); |
| 718 | } |
| 719 | |
"Vladimir N. Oleynik" | d128b71 | 2005-10-03 10:08:46 +0000 | [diff] [blame] | 720 | static void scan_dir_find_ch_files(const char *p) |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 721 | { |
| 722 | llist_t *dirs; |
| 723 | llist_t *d_add; |
| 724 | llist_t *d; |
| 725 | struct dirent *de; |
| 726 | DIR *dir; |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 727 | size_t dirlen; |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 728 | |
"Vladimir N. Oleynik" | d128b71 | 2005-10-03 10:08:46 +0000 | [diff] [blame] | 729 | dirs = llist_add_to(NULL, bb_simplify_path(p)); |
"Vladimir N. Oleynik" | 6c0642d | 2005-10-07 15:36:26 +0000 | [diff] [blame] | 730 | replace = strlen(dirs->data); |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 731 | /* emulate recursive */ |
| 732 | while(dirs) { |
| 733 | d_add = NULL; |
| 734 | while(dirs) { |
| 735 | dir = opendir(dirs->data); |
| 736 | if (dir == NULL) |
"Vladimir N. Oleynik" | d128b71 | 2005-10-03 10:08:46 +0000 | [diff] [blame] | 737 | fprintf(stderr, "Warning: opendir(%s): %m\n", dirs->data); |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 738 | dirlen = strlen(dirs->data); |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 739 | while ((de = readdir(dir)) != NULL) { |
"Vladimir N. Oleynik" | d128b71 | 2005-10-03 10:08:46 +0000 | [diff] [blame] | 740 | char *found_dir; |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 741 | |
"Vladimir N. Oleynik" | d128b71 | 2005-10-03 10:08:46 +0000 | [diff] [blame] | 742 | if (de->d_name[0] == '.') |
| 743 | continue; |
| 744 | found_dir = parse_chd(de->d_name, dirs->data, dirlen); |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 745 | if(found_dir) |
| 746 | d_add = llist_add_to(d_add, found_dir); |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 747 | } |
| 748 | closedir(dir); |
"Vladimir N. Oleynik" | d128b71 | 2005-10-03 10:08:46 +0000 | [diff] [blame] | 749 | free(dirs->data); |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 750 | d = dirs; |
| 751 | dirs = dirs->link; |
| 752 | free(d); |
| 753 | } |
| 754 | dirs = d_add; |
| 755 | } |
| 756 | } |
| 757 | |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 758 | |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 759 | int main(int argc, char **argv) |
| 760 | { |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 761 | char *s; |
| 762 | int i; |
| 763 | llist_t *fl; |
| 764 | |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 765 | { |
| 766 | /* for bb_simplify_path */ |
| 767 | /* libbb xgetcwd(), this program have not chdir() */ |
| 768 | unsigned path_max = 512; |
| 769 | |
| 770 | s = xmalloc (path_max); |
| 771 | #define PATH_INCR 32 |
| 772 | while (getcwd (s, path_max) == NULL) { |
| 773 | if(errno != ERANGE) |
| 774 | bb_error_d("getcwd: %m"); |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 775 | s = xrealloc (s, path_max += PATH_INCR); |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 776 | } |
| 777 | pwd = s; |
| 778 | } |
| 779 | |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 780 | while ((i = getopt(argc, argv, "I:c:dk:w")) > 0) { |
| 781 | switch(i) { |
| 782 | case 'I': |
| 783 | Iop = llist_add_to(Iop, optarg); |
| 784 | break; |
| 785 | case 'c': |
| 786 | s = bb_simplify_path(optarg); |
| 787 | configs = llist_add_to(configs, s); |
| 788 | break; |
| 789 | case 'd': |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 790 | dontgenerate_dep = 1; |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 791 | break; |
| 792 | case 'k': |
| 793 | if(kp) |
| 794 | bb_error_d("Hmm, why multiple -k?"); |
| 795 | kp = bb_simplify_path(optarg); |
| 796 | break; |
| 797 | case 'w': |
| 798 | noiwarning = 1; |
| 799 | break; |
| 800 | default: |
| 801 | show_usage(); |
| 802 | } |
| 803 | } |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 804 | /* default kp */ |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 805 | if(kp == NULL) |
| 806 | kp = bb_simplify_path(INCLUDE_CONFIG_PATH); |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 807 | /* globals initialize */ |
| 808 | kp_len = strlen(kp); |
| 809 | if(stat(kp, &st_kp)) |
| 810 | bb_error_d("stat(%s): %m", kp); |
| 811 | if(!S_ISDIR(st_kp.st_mode)) |
| 812 | bb_error_d("%s is not directory", kp); |
| 813 | /* defaults */ |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 814 | if(Iop == NULL) |
| 815 | Iop = llist_add_to(Iop, LOCAL_INCLUDE_PATH); |
| 816 | if(configs == NULL) { |
| 817 | s = bb_simplify_path(INCLUDE_CONFIG_KEYS_PATH); |
| 818 | configs = llist_add_to(configs, s); |
| 819 | } |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 820 | /* for c_lex */ |
| 821 | pagesizem1 = getpagesize() - 1; |
| 822 | id_s = xmalloc(mema_id); |
| 823 | for(i = 0; i < 256; i++) { |
"Vladimir N. Oleynik" | af0dd59 | 2005-09-16 13:57:33 +0000 | [diff] [blame] | 824 | if(ISALNUM(i)) |
| 825 | isalnums[i] = i; |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 826 | /* set unparsed chars for speed up of parser */ |
"Vladimir N. Oleynik" | af0dd59 | 2005-09-16 13:57:33 +0000 | [diff] [blame] | 827 | else if(i != CHR && i != STR && i != POUND && i != REM && i != BS) |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 828 | first_chars[i] = ANY; |
| 829 | } |
| 830 | first_chars[i] = '-'; /* L_EOF */ |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 831 | |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 832 | /* parse configs */ |
| 833 | for(fl = configs; fl; fl = fl->link) { |
| 834 | struct stat st; |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 835 | |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 836 | if(stat(fl->data, &st)) |
| 837 | bb_error_d("stat(%s): %m", fl->data); |
| 838 | c_lex(fl->data, st.st_size); |
| 839 | /* trick for fast comparing found files with configs */ |
| 840 | fl->data = xrealloc(fl->data, sizeof(struct stat)); |
| 841 | memcpy(fl->data, &st, sizeof(struct stat)); |
| 842 | } |
| 843 | |
| 844 | /* main loop */ |
| 845 | mode = SOURCES_MODE; |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 846 | argv += optind; |
| 847 | if(*argv) { |
| 848 | while(*argv) |
| 849 | scan_dir_find_ch_files(*argv++); |
| 850 | } else { |
| 851 | scan_dir_find_ch_files("."); |
| 852 | } |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 853 | return 0; |
| 854 | } |
| 855 | |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 856 | /* partial and simplified libbb routine */ |
"Vladimir N. Oleynik" | b1fe462 | 2005-09-12 16:39:47 +0000 | [diff] [blame] | 857 | static void bb_error_d(const char *s, ...) |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 858 | { |
| 859 | va_list p; |
| 860 | |
| 861 | va_start(p, s); |
| 862 | vfprintf(stderr, s, p); |
| 863 | va_end(p); |
| 864 | putc('\n', stderr); |
| 865 | exit(1); |
| 866 | } |
| 867 | |
"Vladimir N. Oleynik" | b1fe462 | 2005-09-12 16:39:47 +0000 | [diff] [blame] | 868 | static char *bb_asprint(const char *format, ...) |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 869 | { |
| 870 | va_list p; |
| 871 | int r; |
| 872 | char *out; |
| 873 | |
| 874 | va_start(p, format); |
| 875 | r = vasprintf(&out, format, p); |
| 876 | va_end(p); |
| 877 | |
| 878 | if (r < 0) |
| 879 | bb_error_d("bb_asprint: %m"); |
| 880 | return out; |
| 881 | } |
| 882 | |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 883 | /* partial libbb routine as is */ |
| 884 | static void *xmalloc(size_t size) |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 885 | { |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 886 | void *p = malloc(size); |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 887 | |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 888 | if(p == NULL) |
| 889 | bb_error_d(msg_enomem); |
| 890 | return p; |
| 891 | } |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 892 | |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 893 | static void *xrealloc(void *p, size_t size) { |
| 894 | p = realloc(p, size); |
| 895 | if(p == NULL) |
| 896 | bb_error_d(msg_enomem); |
| 897 | return p; |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 898 | } |
| 899 | |
"Vladimir N. Oleynik" | b1fe462 | 2005-09-12 16:39:47 +0000 | [diff] [blame] | 900 | static char *bb_xstrdup(const char *s) |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 901 | { |
"Vladimir N. Oleynik" | b1fe462 | 2005-09-12 16:39:47 +0000 | [diff] [blame] | 902 | char *r = strdup(s); |
| 903 | |
| 904 | if(r == NULL) |
"Vladimir N. Oleynik" | 7573ac6 | 2005-09-14 15:09:06 +0000 | [diff] [blame] | 905 | bb_error_d(msg_enomem); |
"Vladimir N. Oleynik" | b1fe462 | 2005-09-12 16:39:47 +0000 | [diff] [blame] | 906 | return r; |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 907 | } |
| 908 | |
"Vladimir N. Oleynik" | b1fe462 | 2005-09-12 16:39:47 +0000 | [diff] [blame] | 909 | static char *bb_simplify_path(const char *path) |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 910 | { |
| 911 | char *s, *start, *p; |
| 912 | |
| 913 | if (path[0] == '/') |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 914 | start = bb_xstrdup(path); |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 915 | else { |
"Vladimir N. Oleynik" | 676e95e | 2005-09-13 16:50:53 +0000 | [diff] [blame] | 916 | /* is not libbb, but this program have not chdir() */ |
| 917 | start = bb_asprint("%s/%s", pwd, path); |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 918 | } |
| 919 | p = s = start; |
| 920 | |
| 921 | do { |
"Vladimir N. Oleynik" | b1fe462 | 2005-09-12 16:39:47 +0000 | [diff] [blame] | 922 | if (*p == '/') { |
| 923 | if (*s == '/') { /* skip duplicate (or initial) slash */ |
| 924 | continue; |
| 925 | } else if (*s == '.') { |
| 926 | if (s[1] == '/' || s[1] == 0) { /* remove extra '.' */ |
| 927 | continue; |
| 928 | } else if ((s[1] == '.') && (s[2] == '/' || s[2] == 0)) { |
| 929 | ++s; |
| 930 | if (p > start) { |
| 931 | while (*--p != '/'); /* omit previous dir */ |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 932 | } |
"Vladimir N. Oleynik" | b1fe462 | 2005-09-12 16:39:47 +0000 | [diff] [blame] | 933 | continue; |
| 934 | } |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 935 | } |
"Vladimir N. Oleynik" | b1fe462 | 2005-09-12 16:39:47 +0000 | [diff] [blame] | 936 | } |
| 937 | *++p = *s; |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 938 | } while (*++s); |
| 939 | |
"Vladimir N. Oleynik" | b1fe462 | 2005-09-12 16:39:47 +0000 | [diff] [blame] | 940 | if ((p == start) || (*p != '/')) { /* not a trailing slash */ |
| 941 | ++p; /* so keep last character */ |
"Vladimir N. Oleynik" | 5e60dc4 | 2005-09-12 12:33:27 +0000 | [diff] [blame] | 942 | } |
| 943 | *p = 0; |
| 944 | |
| 945 | return start; |
| 946 | } |