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