From: Rusty Russell Date: Mon, 19 Mar 2012 05:36:27 +0000 (+1030) Subject: tools: move config.h reading to its own file X-Git-Url: http://git.ozlabs.org/?p=ccan;a=commitdiff_plain;h=624871f35e94510b9924ba733f2b878ecf2cc6cc;ds=sidebyside tools: move config.h reading to its own file Move ccan_dir determination out to its own function, rather than implying it by the first time we get the manifest of a module. --- diff --git a/tools/Makefile b/tools/Makefile index c942bdee..b8871be0 100644 --- a/tools/Makefile +++ b/tools/Makefile @@ -8,6 +8,7 @@ DEP_OBJS = ccan/grab_file/grab_file.o \ ccan/str_talloc/str_talloc.o \ ccan/talloc/talloc.o \ ccan/time/time.o \ + tools/read_config_header.o \ tools/compile.o \ tools/depends.o \ tools/tools.o diff --git a/tools/ccanlint/Makefile b/tools/ccanlint/Makefile index cf450f27..68246700 100644 --- a/tools/ccanlint/Makefile +++ b/tools/ccanlint/Makefile @@ -32,6 +32,7 @@ CORE_OBJS := \ tools/depends.o \ tools/doc_extract-core.o \ tools/manifest.o \ + tools/read_config_header.o \ tools/tools.o OBJS := $(CORE_OBJS) $(TEST_OBJS) diff --git a/tools/ccanlint/ccanlint.c b/tools/ccanlint/ccanlint.c index 931ed41a..4ec8bb8e 100644 --- a/tools/ccanlint/ccanlint.c +++ b/tools/ccanlint/ccanlint.c @@ -19,6 +19,7 @@ */ #include "ccanlint.h" #include "../tools.h" +#include "../read_config_header.h" #include #include #include @@ -559,100 +560,6 @@ char **per_file_options(const struct ccanlint *test, struct ccan_file *f) return talloc_realloc(NULL, ret, char *, j + 1); } -static char *demangle_string(char *string) -{ - unsigned int i; - const char mapfrom[] = "abfnrtv"; - const char mapto[] = "\a\b\f\n\r\t\v"; - - if (!strchr(string, '"')) - return NULL; - string = strchr(string, '"') + 1; - if (!strrchr(string, '"')) - return NULL; - *strrchr(string, '"') = '\0'; - - for (i = 0; i < strlen(string); i++) { - if (string[i] == '\\') { - char repl; - unsigned len = 0; - const char *p = strchr(mapfrom, string[i+1]); - if (p) { - repl = mapto[p - mapfrom]; - len = 1; - } else if (strlen(string+i+1) >= 3) { - if (string[i+1] == 'x') { - repl = (string[i+2]-'0')*16 - + string[i+3]-'0'; - len = 3; - } else if (cisdigit(string[i+1])) { - repl = (string[i+2]-'0')*8*8 - + (string[i+3]-'0')*8 - + (string[i+4]-'0'); - len = 3; - } - } - if (len == 0) { - repl = string[i+1]; - len = 1; - } - - string[i] = repl; - memmove(string + i + 1, string + i + len + 1, - strlen(string + i + len + 1) + 1); - } - } - - return string; -} - - -static void read_config_header(void) -{ - char *fname = talloc_asprintf(NULL, "%s/config.h", ccan_dir); - char **lines; - unsigned int i; - - config_header = grab_file(NULL, fname, NULL); - if (!config_header) { - talloc_free(fname); - return; - } - - lines = strsplit(config_header, config_header, "\n"); - for (i = 0; i < talloc_array_length(lines) - 1; i++) { - char *sym; - const char **line = (const char **)&lines[i]; - - if (!get_token(line, "#")) - continue; - if (!get_token(line, "define")) - continue; - sym = get_symbol_token(lines, line); - if (streq(sym, "CCAN_COMPILER") && !compiler) { - compiler = demangle_string(lines[i]); - if (!compiler) - errx(1, "%s:%u:could not parse CCAN_COMPILER", - fname, i+1); - if (verbose > 1) - printf("%s: compiler set to '%s'\n", - fname, compiler); - } else if (streq(sym, "CCAN_CFLAGS") && !cflags) { - cflags = demangle_string(lines[i]); - if (!cflags) - errx(1, "%s:%u:could not parse CCAN_CFLAGS", - fname, i+1); - if (verbose > 1) - printf("%s: compiler flags set to '%s'\n", - fname, cflags); - } - } - if (!compiler) - compiler = CCAN_COMPILER; - if (!cflags) - compiler = CCAN_CFLAGS; -} - static char *opt_set_const_charp(const char *arg, const char **p) { return opt_set_charp(arg, cast_const2(char **, p)); @@ -785,7 +692,9 @@ int main(int argc, char *argv[]) if (!ccan_dir) errx(1, "Cannot find ccan/ base directory in %s", dir); - read_config_header(); + config_header = read_config_header(ccan_dir, + &compiler, &cflags, + verbose > 1); } if (dir != base_dir) diff --git a/tools/ccanlint/file_analysis.c b/tools/ccanlint/file_analysis.c index d915b7ed..8760630a 100644 --- a/tools/ccanlint/file_analysis.c +++ b/tools/ccanlint/file_analysis.c @@ -105,40 +105,6 @@ static bool continues(const char *line) return strends(line, "\\"); } -/* Get token if it's equal to token. */ -bool get_token(const char **line, const char *token) -{ - unsigned int toklen; - - *line += strspn(*line, " \t"); - if (cisalnum(token[0]) || token[0] == '_') - toklen = strspn(*line, IDENT_CHARS); - else { - /* FIXME: real tokenizer handles ++ and other multi-chars. */ - toklen = strlen(token); - } - - if (toklen == strlen(token) && !strncmp(*line, token, toklen)) { - *line += toklen; - return true; - } - return false; -} - -char *get_symbol_token(void *ctx, const char **line) -{ - unsigned int toklen; - char *ret; - - *line += strspn(*line, " \t"); - toklen = strspn(*line, IDENT_CHARS); - if (!toklen) - return NULL; - ret = talloc_strndup(ctx, *line, toklen); - *line += toklen; - return ret; -} - static bool parse_hash_if(struct pp_conditions *cond, const char **line) { bool brackets, defined; diff --git a/tools/read_config_header.c b/tools/read_config_header.c new file mode 100644 index 00000000..b168a2f9 --- /dev/null +++ b/tools/read_config_header.c @@ -0,0 +1,143 @@ +#include +#include +#include +#include +#include "read_config_header.h" +#include "tools.h" +#include +#include + +/* Get an identifier token. */ +char *get_symbol_token(void *ctx, const char **line) +{ + unsigned int toklen; + char *ret; + + *line += strspn(*line, " \t"); + toklen = strspn(*line, IDENT_CHARS); + if (!toklen) + return NULL; + ret = talloc_strndup(ctx, *line, toklen); + *line += toklen; + return ret; +} + +/* Get token if it's equal to token. */ +bool get_token(const char **line, const char *token) +{ + unsigned int toklen; + + *line += strspn(*line, " \t"); + if (cisalnum(token[0]) || token[0] == '_') + toklen = strspn(*line, IDENT_CHARS); + else { + /* FIXME: real tokenizer handles ++ and other multi-chars. */ + toklen = strlen(token); + } + + if (toklen == strlen(token) && !strncmp(*line, token, toklen)) { + *line += toklen; + return true; + } + return false; +} + +static char *demangle_string(char *string) +{ + unsigned int i; + const char mapfrom[] = "abfnrtv"; + const char mapto[] = "\a\b\f\n\r\t\v"; + + if (!strchr(string, '"')) + return NULL; + string = strchr(string, '"') + 1; + if (!strrchr(string, '"')) + return NULL; + *strrchr(string, '"') = '\0'; + + for (i = 0; i < strlen(string); i++) { + if (string[i] == '\\') { + char repl; + unsigned len = 0; + const char *p = strchr(mapfrom, string[i+1]); + if (p) { + repl = mapto[p - mapfrom]; + len = 1; + } else if (strlen(string+i+1) >= 3) { + if (string[i+1] == 'x') { + repl = (string[i+2]-'0')*16 + + string[i+3]-'0'; + len = 3; + } else if (cisdigit(string[i+1])) { + repl = (string[i+2]-'0')*8*8 + + (string[i+3]-'0')*8 + + (string[i+4]-'0'); + len = 3; + } + } + if (len == 0) { + repl = string[i+1]; + len = 1; + } + + string[i] = repl; + memmove(string + i + 1, string + i + len + 1, + strlen(string + i + len + 1) + 1); + } + } + + return string; +} + +char *read_config_header(const char *ccan_dir, + const char **compiler, const char **cflags, + bool verbose) +{ + char *fname = talloc_asprintf(NULL, "%s/config.h", ccan_dir); + char **lines; + unsigned int i; + char *config_header; + + config_header = grab_file(NULL, fname, NULL); + talloc_free(fname); + + if (!config_header) + goto out; + + lines = strsplit(config_header, config_header, "\n"); + for (i = 0; i < talloc_array_length(lines) - 1; i++) { + char *sym; + const char **line = (const char **)&lines[i]; + + if (!get_token(line, "#")) + continue; + if (!get_token(line, "define")) + continue; + sym = get_symbol_token(lines, line); + if (streq(sym, "CCAN_COMPILER") && !compiler) { + *compiler = demangle_string(lines[i]); + if (!*compiler) + errx(1, "%s:%u:could not parse CCAN_COMPILER", + fname, i+1); + if (verbose) + printf("%s: compiler set to '%s'\n", + fname, *compiler); + } else if (streq(sym, "CCAN_CFLAGS") && !cflags) { + *cflags = demangle_string(lines[i]); + if (!*cflags) + errx(1, "%s:%u:could not parse CCAN_CFLAGS", + fname, i+1); + if (verbose) + printf("%s: compiler flags set to '%s'\n", + fname, *cflags); + } + } + +out: + if (!*compiler) + *compiler = CCAN_COMPILER; + if (!*cflags) + *cflags = CCAN_CFLAGS; + + return config_header; +} diff --git a/tools/read_config_header.h b/tools/read_config_header.h new file mode 100644 index 00000000..62eb152e --- /dev/null +++ b/tools/read_config_header.h @@ -0,0 +1,16 @@ +#ifndef CCAN_TOOLS_READ_CONFIG_HEADER_H +#define CCAN_TOOLS_READ_CONFIG_HEADER_H +#include + +/* Get token if it's equal to token. */ +bool get_token(const char **line, const char *token); + +/* Get an identifier token. */ +char *get_symbol_token(void *ctx, const char **line); + +/* Read config header from config_dir/config.h: set compiler/cflags. */ +char *read_config_header(const char *config_dir, + const char **compiler, const char **cflags, + bool verbose); + +#endif /* CCAN_TOOLS_READ_CONFIG_HEADER_H */