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