]> git.ozlabs.org Git - ccan/blob - tools/read_config_header.c
tools: use tal instead of talloc.
[ccan] / tools / read_config_header.c
1 #include <ccan/err/err.h>
2 #include <ccan/str/str.h>
3 #include "read_config_header.h"
4 #include "tools.h"
5 #include <string.h>
6
7 /* Get an identifier token. */
8 char *get_symbol_token(void *ctx, const char **line)
9 {
10         unsigned int toklen;
11         char *ret;
12
13         *line += strspn(*line, " \t");
14         toklen = strspn(*line, IDENT_CHARS);
15         if (!toklen)
16                 return NULL;
17         ret = tal_strndup(ctx, *line, toklen);
18         *line += toklen;
19         return ret;
20 }
21
22 /* Get token if it's equal to token. */
23 bool get_token(const char **line, const char *token)
24 {
25         unsigned int toklen;
26
27         *line += strspn(*line, " \t");
28         if (cisalnum(token[0]) || token[0] == '_')
29                 toklen = strspn(*line, IDENT_CHARS);
30         else {
31                 /* FIXME: real tokenizer handles ++ and other multi-chars.  */
32                 toklen = strlen(token);
33         }
34
35         if (toklen == strlen(token) && !strncmp(*line, token, toklen)) {
36                 *line += toklen;
37                 return true;
38         }
39         return false;
40 }
41
42 static char *demangle_string(char *string)
43 {
44         unsigned int i;
45         const char mapfrom[] = "abfnrtv";
46         const char mapto[] = "\a\b\f\n\r\t\v";
47
48         if (!strchr(string, '"'))
49                 return NULL;
50         string = strchr(string, '"') + 1;
51         if (!strrchr(string, '"'))
52                 return NULL;
53         *strrchr(string, '"') = '\0';
54
55         for (i = 0; i < strlen(string); i++) {
56                 if (string[i] == '\\') {
57                         char repl;
58                         unsigned len = 0;
59                         const char *p = strchr(mapfrom, string[i+1]);
60                         if (p) {
61                                 repl = mapto[p - mapfrom];
62                                 len = 1;
63                         } else if (strlen(string+i+1) >= 3) {
64                                 if (string[i+1] == 'x') {
65                                         repl = (string[i+2]-'0')*16
66                                                 + string[i+3]-'0';
67                                         len = 3;
68                                 } else if (cisdigit(string[i+1])) {
69                                         repl = (string[i+2]-'0')*8*8
70                                                 + (string[i+3]-'0')*8
71                                                 + (string[i+4]-'0');
72                                         len = 3;
73                                 }
74                         }
75                         if (len == 0) {
76                                 repl = string[i+1];
77                                 len = 1;
78                         }
79
80                         string[i] = repl;
81                         memmove(string + i + 1, string + i + len + 1,
82                                 strlen(string + i + len + 1) + 1);
83                 }
84         }
85
86         return string;
87 }
88
89 char *read_config_header(const char *ccan_dir,
90                          const char **compiler, const char **cflags,
91                          bool verbose)
92 {
93         char *fname = tal_fmt(NULL, "%s/config.h", ccan_dir);
94         char **lines;
95         unsigned int i;
96         char *config_header;
97
98         config_header = tal_grab_file(NULL, fname, NULL);
99         tal_free(fname);
100
101         if (!config_header)
102                 goto out;
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") && !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") && !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
133 out:
134         if (!*compiler)
135                 *compiler = CCAN_COMPILER;
136         if (!*cflags)
137                 *cflags = CCAN_CFLAGS;
138
139         return config_header;
140 }