1 #include <ccan/err/err.h>
2 #include <ccan/str/str.h>
3 #include "read_config_header.h"
7 /* Get an identifier token. */
8 char *get_symbol_token(void *ctx, const char **line)
13 *line += strspn(*line, " \t");
14 toklen = strspn(*line, IDENT_CHARS);
17 ret = tal_strndup(ctx, *line, toklen);
22 /* Get token if it's equal to token. */
23 bool get_token(const char **line, const char *token)
27 *line += strspn(*line, " \t");
28 if (cisalnum(token[0]) || token[0] == '_')
29 toklen = strspn(*line, IDENT_CHARS);
31 /* FIXME: real tokenizer handles ++ and other multi-chars. */
32 toklen = strlen(token);
35 if (toklen == strlen(token) && !strncmp(*line, token, toklen)) {
42 static char *demangle_string(char *string)
45 const char mapfrom[] = "abfnrtv";
46 const char mapto[] = "\a\b\f\n\r\t\v";
48 if (!strchr(string, '"'))
50 string = strchr(string, '"') + 1;
51 if (!strrchr(string, '"'))
53 *strrchr(string, '"') = '\0';
55 for (i = 0; i < strlen(string); i++) {
56 if (string[i] == '\\') {
59 const char *p = strchr(mapfrom, string[i+1]);
61 repl = mapto[p - mapfrom];
63 } else if (strlen(string+i+1) >= 3) {
64 if (string[i+1] == 'x') {
65 repl = (string[i+2]-'0')*16
68 } else if (cisdigit(string[i+1])) {
69 repl = (string[i+2]-'0')*8*8
81 memmove(string + i + 1, string + i + len + 1,
82 strlen(string + i + len + 1) + 1);
89 char *read_config_header(const char *ccan_dir,
90 const char **compiler, const char **cflags,
93 char *fname = tal_fmt(NULL, "%s/config.h", ccan_dir);
98 config_header = tal_grab_file(NULL, fname, NULL);
104 lines = tal_strsplit(config_header, config_header, "\n", STR_EMPTY_OK);
105 for (i = 0; i < tal_count(lines) - 1; i++) {
107 const char **line = (const char **)&lines[i];
109 if (!get_token(line, "#"))
111 if (!get_token(line, "define"))
113 sym = get_symbol_token(lines, line);
114 if (streq(sym, "CCAN_COMPILER") && !compiler) {
115 *compiler = demangle_string(lines[i]);
117 errx(1, "%s:%u:could not parse CCAN_COMPILER",
120 printf("%s: compiler set to '%s'\n",
122 } else if (streq(sym, "CCAN_CFLAGS") && !cflags) {
123 *cflags = demangle_string(lines[i]);
125 errx(1, "%s:%u:could not parse CCAN_CFLAGS",
128 printf("%s: compiler flags set to '%s'\n",
135 *compiler = CCAN_COMPILER;
137 *cflags = CCAN_CFLAGS;
139 return config_header;