1 #include <ccan/err/err.h>
2 #include <ccan/str/str.h>
3 #include <ccan/tal/path/path.h>
4 #include "read_config_header.h"
8 /* Get an identifier token. */
9 char *get_symbol_token(void *ctx, const char **line)
14 *line += strspn(*line, " \t");
15 toklen = strspn(*line, IDENT_CHARS);
18 ret = tal_strndup(ctx, *line, toklen);
23 /* Get token if it's equal to token. */
24 bool get_token(const char **line, const char *token)
28 *line += strspn(*line, " \t");
29 if (cisalnum(token[0]) || token[0] == '_')
30 toklen = strspn(*line, IDENT_CHARS);
32 /* FIXME: real tokenizer handles ++ and other multi-chars. */
33 toklen = strlen(token);
36 if (toklen == strlen(token) && !strncmp(*line, token, toklen)) {
43 static char *demangle_string(char *string)
46 const char mapfrom[] = "abfnrtv";
47 const char mapto[] = "\a\b\f\n\r\t\v";
49 if (!strchr(string, '"'))
51 string = strchr(string, '"') + 1;
52 if (!strrchr(string, '"'))
54 *strrchr(string, '"') = '\0';
56 for (i = 0; i < strlen(string); i++) {
57 if (string[i] == '\\') {
60 const char *p = strchr(mapfrom, string[i+1]);
62 repl = mapto[p - mapfrom];
64 } else if (strlen(string+i+1) >= 3) {
65 if (string[i+1] == 'x') {
66 repl = (string[i+2]-'0')*16
69 } else if (cisdigit(string[i+1])) {
70 repl = (string[i+2]-'0')*8*8
82 memmove(string + i + 1, string + i + len + 1,
83 strlen(string + i + len + 1) + 1);
90 char *read_config_header(const char *ccan_dir, bool verbose)
92 char *fname = path_join(NULL, ccan_dir, "config.h");
97 config_header = tal_grab_file(NULL, fname, NULL);
103 lines = tal_strsplit(config_header, config_header, "\n", STR_EMPTY_OK);
104 for (i = 0; i < tal_count(lines) - 1; i++) {
106 const char **line = (const char **)&lines[i];
108 if (!get_token(line, "#"))
110 if (!get_token(line, "define"))
112 sym = get_symbol_token(lines, line);
113 if (streq(sym, "CCAN_COMPILER")) {
114 compiler = demangle_string(lines[i]);
116 errx(1, "%s:%u:could not parse CCAN_COMPILER",
119 printf("%s: compiler set to '%s'\n",
121 } else if (streq(sym, "CCAN_CFLAGS")) {
122 cflags = demangle_string(lines[i]);
124 errx(1, "%s:%u:could not parse CCAN_CFLAGS",
127 printf("%s: compiler flags set to '%s'\n",
131 return config_header;