]> git.ozlabs.org Git - ccan/blob - tools/read_config_header.c
tools: use rbuf instead of grab_file.
[ccan] / tools / read_config_header.c
1 #include <ccan/err/err.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
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 = talloc_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,
92                          const char **compiler, const char **cflags,
93                          bool verbose)
94 {
95         char *fname = talloc_asprintf(NULL, "%s/config.h", ccan_dir);
96         char **lines;
97         unsigned int i;
98         char *config_header;
99
100         config_header = talloc_grab_file(NULL, fname, NULL);
101         talloc_free(fname);
102
103         if (!config_header)
104                 goto out;
105
106         lines = strsplit(config_header, config_header, "\n");
107         for (i = 0; i < talloc_array_length(lines) - 1; i++) {
108                 char *sym;
109                 const char **line = (const char **)&lines[i];
110
111                 if (!get_token(line, "#"))
112                         continue;
113                 if (!get_token(line, "define"))
114                         continue;
115                 sym = get_symbol_token(lines, line);
116                 if (streq(sym, "CCAN_COMPILER") && !compiler) {
117                         *compiler = demangle_string(lines[i]);
118                         if (!*compiler)
119                                 errx(1, "%s:%u:could not parse CCAN_COMPILER",
120                                      fname, i+1);
121                         if (verbose)
122                                 printf("%s: compiler set to '%s'\n",
123                                        fname, *compiler);
124                 } else if (streq(sym, "CCAN_CFLAGS") && !cflags) {
125                         *cflags = demangle_string(lines[i]);
126                         if (!*cflags)
127                                 errx(1, "%s:%u:could not parse CCAN_CFLAGS",
128                                      fname, i+1);
129                         if (verbose)
130                                 printf("%s: compiler flags set to '%s'\n",
131                                        fname, *cflags);
132                 }
133         }
134
135 out:
136         if (!*compiler)
137                 *compiler = CCAN_COMPILER;
138         if (!*cflags)
139                 *cflags = CCAN_CFLAGS;
140
141         return config_header;
142 }