]> git.ozlabs.org Git - ccan/blob - tools/read_config_header.c
heap: new module
[ccan] / tools / read_config_header.c
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"
5 #include "tools.h"
6 #include <string.h>
7
8 /* Get an identifier token. */
9 char *get_symbol_token(void *ctx, const char **line)
10 {
11         unsigned int toklen;
12         char *ret;
13
14         *line += strspn(*line, " \t");
15         toklen = strspn(*line, IDENT_CHARS);
16         if (!toklen)
17                 return NULL;
18         ret = tal_strndup(ctx, *line, toklen);
19         *line += toklen;
20         return ret;
21 }
22
23 /* Get token if it's equal to token. */
24 bool get_token(const char **line, const char *token)
25 {
26         unsigned int toklen;
27
28         *line += strspn(*line, " \t");
29         if (cisalnum(token[0]) || token[0] == '_')
30                 toklen = strspn(*line, IDENT_CHARS);
31         else {
32                 /* FIXME: real tokenizer handles ++ and other multi-chars.  */
33                 toklen = strlen(token);
34         }
35
36         if (toklen == strlen(token) && !strncmp(*line, token, toklen)) {
37                 *line += toklen;
38                 return true;
39         }
40         return false;
41 }
42
43 static char *demangle_string(char *string)
44 {
45         unsigned int i;
46         const char mapfrom[] = "abfnrtv";
47         const char mapto[] = "\a\b\f\n\r\t\v";
48
49         if (!strchr(string, '"'))
50                 return NULL;
51         string = strchr(string, '"') + 1;
52         if (!strrchr(string, '"'))
53                 return NULL;
54         *strrchr(string, '"') = '\0';
55
56         for (i = 0; i < strlen(string); i++) {
57                 if (string[i] == '\\') {
58                         char repl;
59                         unsigned len = 0;
60                         const char *p = strchr(mapfrom, string[i+1]);
61                         if (p) {
62                                 repl = mapto[p - mapfrom];
63                                 len = 1;
64                         } else if (strlen(string+i+1) >= 3) {
65                                 if (string[i+1] == 'x') {
66                                         repl = (string[i+2]-'0')*16
67                                                 + string[i+3]-'0';
68                                         len = 3;
69                                 } else if (cisdigit(string[i+1])) {
70                                         repl = (string[i+2]-'0')*8*8
71                                                 + (string[i+3]-'0')*8
72                                                 + (string[i+4]-'0');
73                                         len = 3;
74                                 }
75                         }
76                         if (len == 0) {
77                                 repl = string[i+1];
78                                 len = 1;
79                         }
80
81                         string[i] = repl;
82                         memmove(string + i + 1, string + i + len + 1,
83                                 strlen(string + i + len + 1) + 1);
84                 }
85         }
86
87         return string;
88 }
89
90 char *read_config_header(const char *ccan_dir, bool verbose)
91 {
92         char *fname = path_join(NULL, ccan_dir, "config.h");
93         char **lines;
94         unsigned int i;
95         char *config_header;
96
97         config_header = tal_grab_file(NULL, fname, NULL);
98         tal_free(fname);
99
100         if (!config_header)
101                 return NULL;
102
103         lines = tal_strsplit(config_header, config_header, "\n", STR_EMPTY_OK);
104         for (i = 0; i < tal_count(lines) - 1; i++) {
105                 char *sym;
106                 const char **line = (const char **)&lines[i];
107
108                 if (!get_token(line, "#"))
109                         continue;
110                 if (!get_token(line, "define"))
111                         continue;
112                 sym = get_symbol_token(lines, line);
113                 if (streq(sym, "CCAN_COMPILER")) {
114                         compiler = demangle_string(lines[i]);
115                         if (!compiler)
116                                 errx(1, "%s:%u:could not parse CCAN_COMPILER",
117                                      fname, i+1);
118                         if (verbose)
119                                 printf("%s: compiler set to '%s'\n",
120                                        fname, compiler);
121                 } else if (streq(sym, "CCAN_CFLAGS")) {
122                         cflags = demangle_string(lines[i]);
123                         if (!cflags)
124                                 errx(1, "%s:%u:could not parse CCAN_CFLAGS",
125                                      fname, i+1);
126                         if (verbose)
127                                 printf("%s: compiler flags set to '%s'\n",
128                                        fname, cflags);
129                 }
130         }
131         return config_header;
132 }