English Language flag
// Log In
// CVSweb
Project: FreeWRT
// Summary // Activity // Search // Tracker // Lists // News // SCM // Wiki

SCM Repository

ViewVC logotype

Contents of /branches/common-nfo/tools/nfotiser/parser.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3464 - (show annotations) (download)
Thu Aug 9 15:08:25 2007 UTC (6 years, 6 months ago) by tg
File MIME type: text/plain
File size: 10571 byte(s)
that one can now be omitted
we'll have to make sure that a kw_iter of 0 is thought of as 1

1 /* $FreeWRT: src/share/misc/licence.template,v 1.20 2006/12/11 21:04:56 tg Rel $ */
2
3 /*-
4 * Copyright (c) 2007
5 * Thorsten Glaser <tg@mirbsd.de>
6 *
7 * Provided that these terms and disclaimer and all copyright notices
8 * are retained or reproduced in an accompanying document, permission
9 * is granted to deal in this work without restriction, including un-
10 * limited rights to use, publicly perform, distribute, sell, modify,
11 * merge, give away, or sublicence.
12 *
13 * Advertising materials mentioning features or use of this work must
14 * display the following acknowledgement:
15 * This product includes material provided by Thorsten Glaser.
16 * This acknowledgement does not need to be reprinted if this work is
17 * linked into a bigger work whose licence does not allow such clause
18 * and the author of this work is given due credit in the bigger work
19 * or its accompanying documents, where such information is generally
20 * kept, provided that said credits are retained.
21 *
22 * This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to
23 * the utmost extent permitted by applicable law, neither express nor
24 * implied; without malicious intent or gross negligence. In no event
25 * may a licensor, author or contributor be held liable for indirect,
26 * direct, other damage, loss, or other issues arising in any way out
27 * of dealing in the work, even if advised of the possibility of such
28 * damage or existence of a defect, except proven that it results out
29 * of said person's immediate fault when using the work as intended.
30 */
31
32 #include <sys/param.h>
33 #include <sys/mman.h>
34 #include <sys/stat.h>
35
36 #include <err.h>
37 #include <errno.h>
38 #include <stdarg.h>
39 #include <stdint.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43
44 #include "nfotiser.h"
45
46 static void syntaxerr_(size_t, const char *, ...)
47 __attribute__((format (printf, 2, 3)))
48 __attribute__((noreturn));
49 #define syntaxerr(fmt, ...) syntaxerr_(lineno, (fmt), ##__VA_ARGS__)
50
51 /*
52 * Parsing works as follows:
53 *
54 * - strip completely empty line
55 * - strip line beginning with hash mark
56 * - if line ends with backslash, get next line that is not
57 * + empty
58 * + beginning with a hash mark
59 * and look if it begins with a tab (if not: syntax error)
60 * if so, strip backslash + newline + tab and repeat
61 * - if line begins with a tab, strip it and append line to
62 * the last line, including the newline separator
63 * - match line with '([A-Za-z_][A-Za-z0-9_]*)\t(.*)$' and
64 * call \1 the key and \2 the value (else: syntax error)
65 * - uppercase the key
66 * - enter the key/value pair in the system
67 *
68 * The following is also part of parsing, but left to the caller:
69 * - replace ${foo} with the value of key "foo"
70 * - undouble all backslashes
71 *
72 * We enter the key into the system up to three-fold:
73 * - KWT_NORMAL => ([A-Za-z_][A-Za-z0-9_]*)
74 * + \1 = keyword (toupper'd)
75 * - KWT_MULTI => ([A-Za-z_][A-Za-z0-9_]*)_([A-Za-z0-9_]*)
76 * + \1 = keyword (toupper'd)
77 * + \2 = kw_multi (case preserving)
78 * - KWT_ITERATED => ([A-Za-z_][A-Za-z0-9_]*)_([0-9]*)
79 * + \1 = keyword (toupper'd)
80 * + \2 = kw_iter (unsigned integer value)
81 * - KWT_MULTITER => ([A-Za-z_][A-Za-z0-9_]*)_([0-9]*)_([A-Za-z0-9_]*)
82 * + \1 = keyword (toupper'd)
83 * + \2 = kw_iter (unsigned integer value)
84 * + \3 = kw_multi (case preserving)
85 * - KWT_MULTITOP => ([A-Za-z_][A-Za-z0-9_]*)_(([0-9]*)_)?([A-Za-z0-9_]*)
86 * + \1 = keyword (toupper'd)
87 * + \2 = kw_iter (unsigned integer value), 0 if not set
88 * + \3 = kw_multi (case preserving)
89 * All KWT_* can match as if they were KWT_NORMAL (if we have a perfect
90 * first match); kw_iter=0 and kw_multi=NULL in that case.
91 *
92 * Cf. https://www.freewrt.org/trac/wiki/Documentation/Specs/Freewrt_info_files
93 * for more examples and a more human-readable version of this specification.
94 */
95
96 struct parser_result *
97 nfo_parse(int fd, const struct parser_keywords *kws)
98 {
99 struct parser_result *res;
100 struct parser_res *entry;
101 const struct parser_keywords *kwp;
102 char *cp, *t, *tp, *buf, *buf_base;
103 size_t len, n, lineno = 0;
104 struct stat sb;
105 char *entry_multi;
106 unsigned entry_iter;
107
108 res = xmalloc(sizeof (struct parser_result));
109 CIRCLEQ_INIT(res);
110
111 if (fstat(fd, &sb))
112 err(255, "cannot stat");
113
114 /* slurp whole file into mapped memory */
115 len = sb.st_size;
116 D(2, "trying to mmap %zu bytes...", len);
117 if ((cp = mmap(NULL, len, PROT_READ, MAP_FILE, fd, 0)) == MAP_FAILED)
118 err(255, "cannot mmap %zu bytes", len);
119 D(2, "ok\n");
120 /* make a nice NUL-terminated copy (malloc'd) */
121 D(2, "copying %zu bytes...", len);
122 buf = buf_base = str_nsave(cp, len);
123 D(2, " munmap...");
124 if (munmap(cp, len))
125 err(255, "cannot munmap");
126 D(2, "ok\n");
127 /* don't need the file any more */
128
129 /* now we can operate on the NUL-terminated R/W string “buf” */
130 if (buf[len - 1] != '\n')
131 syntaxerr("file does not end with a newline!");
132
133 D(2, "entire string: «%s» (%zu)\n", buf, strlen(buf));
134 lineno = 1;
135
136 loop_newline:
137 /* completely new line buffer */
138 cp = NULL;
139
140 loop_getline:
141 /* get a line and add it to line buffer */
142 if (*buf == '\0') {
143 D(2, "D: [%4zu] read EOF\n", lineno);
144 goto loop_eof;
145 }
146 if (*buf == '\n') {
147 D(2, "D: [%4zu] read newline\n", lineno);
148 ++buf;
149 ++lineno;
150 goto loop_getline;
151 }
152 if (*buf == '#') {
153 D(2, "D: [%4zu] read comment ", lineno);
154 t = buf;
155 while (*t != '\n')
156 ++t;
157 *t++ = '\0';
158 D(2, "'%s'\n", buf);
159 buf = t;
160 ++lineno;
161 goto loop_getline;
162 }
163 if (*buf == '\t') {
164 D(2, "D: [%4zu] read trail line\n", lineno);
165 if (cp == NULL)
166 syntaxerr("expected lead line, got trail line!");
167 t = ++buf;
168 goto loop_storeline;
169 } else {
170 D(2, "D: [%4zu] read head line (%02X)\n", lineno, *buf);
171 }
172 if (cp != NULL) {
173 --lineno;
174 goto process_line;
175 }
176 if ((*buf >= 'A' && *buf <= 'Z') ||
177 (*buf >= 'a' && *buf <= 'z') || *buf == '_')
178 t = buf;
179 else
180 syntaxerr("line must begin with a letter or an underscore!");
181 loop_storeline:
182 while (*t++ != '\n')
183 ;
184 t = str_nsave(buf, (tp = t) - buf);
185 buf = tp;
186 if (cp != NULL) {
187 if (*(tp = cp + strlen(cp) - 2) == '\\')
188 *tp = '\0';
189 }
190 tp = t + strlen(t) - 1;
191 *tp = '\0';
192 D(2, "D: [%4zu] storing string '%s'\n", lineno, t);
193 *tp = '\n';
194 tp = str_add(cp, t);
195 free(t);
196 cp = tp;
197 ++lineno;
198 goto loop_getline;
199 process_line:
200 /* cp points to <line>[<nl><line>…][\]<nl> */
201 /* buf points to first byte of next line */
202 if (*(tp = cp + strlen(cp) - 2) == '\\')
203 syntaxerr("expected trail line, got lead line!");
204 process_lastline:
205 /* cut off final newline */
206 *++tp = '\0';
207 D(2, "D: [%4zu] processing «%s»\n", lineno, cp);
208
209 /* parse the meat out of there */
210 if ((tp = strchr(cp, '\t')) == NULL)
211 syntaxerr("expected keyword + tab + value!");
212 *tp++ = '\0';
213 /* cp points to keyword, tp points to value */
214 entry_multi = NULL;
215 entry_iter = 0;
216 for (kwp = kws; kwp->kwprefix != NULL; ++kwp) {
217 char *np;
218
219 /* exact match? */
220 if (!strcasecmp(cp, kwp->kwprefix))
221 /* yepp */ break;
222 /* prefix match allowed? */
223 if (kwp->kwtype == KWT_NORMAL)
224 /* nope */ continue;
225 /* prefix match? */
226 if (strncasecmp(cp, kwp->kwprefix, n = strlen(kwp->kwprefix)))
227 /* nope */ continue;
228 if (cp[n] != '_')
229 /* same */ continue;
230 /* okay, we got a prefix match, get args */
231 np = cp + n + 1;
232 if (kwp->kwtype == KWT_ITERATED ||
233 (kwp->kwtype == KWT_MULTITOP &&
234 (*np >= '0' && *np <= '9')) ||
235 kwp->kwtype == KWT_MULTITER) {
236 char *zp = np;
237
238 if (zp[0] == '0' && (zp[1] == 'x' || zp[1] == 'X'))
239 zp += 2;
240 while (*zp >= '0' && *zp <= '9')
241 ++zp;
242 if (zp == np)
243 syntaxerr("iterator expected");
244 if (*zp != (char)(kwp->kwtype == KWT_ITERATED ?
245 '\0' : '_'))
246 syntaxerr("%s expected, got 0x%02X",
247 kwp->kwtype == KWT_ITERATED ?
248 "tab" : "underscore", *zp);
249 *zp++ = '\0';
250 entry_iter = (unsigned)strtoul(np, NULL, 0);
251 np = zp;
252 }
253 if (kwp->kwtype == KWT_MULTI ||
254 kwp->kwtype == KWT_MULTITOP ||
255 kwp->kwtype == KWT_MULTITER)
256 entry_multi = str_save(np);
257 /* values filled out */
258 break;
259 }
260 if (kwp->kwprefix == NULL)
261 errx(1, "unknown keyword '%s'", cp);
262 entry = xmalloc(sizeof (struct parser_res));
263 bzero(entry, sizeof (struct parser_res));
264 entry->keyword = kwp->kwnum;
265 entry->kw_multi = entry_multi;
266 entry->kw_iter = entry_iter;
267 entry->value = str_save(tp);
268 CIRCLEQ_INSERT_TAIL(res, entry, e);
269 free(cp);
270 ++lineno;
271 goto loop_newline;
272 loop_eof:
273 if (cp != NULL) {
274 if (*(tp = cp + strlen(cp) - 2) == '\\')
275 syntaxerr("expected trail line, read end of file!");
276 goto process_lastline;
277 }
278 free(buf_base);
279 return (res);
280 }
281
282 const struct parser_keywords *
283 parser_getkwbynum(parser_kwords num, const struct parser_keywords *kws)
284 {
285 const struct parser_keywords *kwp;
286
287 for (kwp = kws; kwp->kwprefix != NULL; ++kwp)
288 if (kwp->kwnum == num)
289 break;
290
291 return (kwp->kwprefix == NULL ? NULL : kwp);
292 }
293
294 void
295 parser_dump(struct parser_res *entry, const struct parser_keywords *kws)
296 {
297 const struct parser_keywords *kwp;
298
299 kwp = parser_getkwbynum(entry->keyword, kws);
300 printf("keyword %s (type %s)",
301 kwp == NULL ? "<unknown>" : kwp->kwprefix,
302 kwp == NULL ? "invalid" :
303 kwp->kwtype == KWT_NORMAL ? "normal" :
304 kwp->kwtype == KWT_MULTI ? "multi" :
305 kwp->kwtype == KWT_ITERATED ? "iterated" :
306 kwp->kwtype == KWT_MULTITER ? "multiter" :
307 kwp->kwtype == KWT_MULTITOP ? "multitop" : "unknown");
308 if (kwp) {
309 if (kwp->kwtype == KWT_ITERATED ||
310 kwp->kwtype == KWT_MULTITOP ||
311 kwp->kwtype == KWT_MULTITER)
312 printf(", iterator %u", entry->kw_iter);
313 if (kwp->kwtype == KWT_MULTI ||
314 kwp->kwtype == KWT_MULTITOP ||
315 kwp->kwtype == KWT_MULTITER) {
316 if (entry->kw_multi)
317 printf(", multi '%s'", entry->kw_multi);
318 else
319 fputs(", multibase", stdout);
320 }
321 }
322 if (entry->value) {
323 const uint8_t *cp = entry->value;
324
325 fputs(", value\n\t『", stdout);
326 while (*cp) {
327 while (*cp && *cp != '\n')
328 fputc(*cp++, stdout);
329 fputs(*cp ? (cp++, "\n\t ") : "』\n", stdout);
330 }
331 } else
332 fputs(", no value\n", stdout);
333 }
334
335 void
336 parser_free(struct parser_result *head)
337 {
338 struct parser_res *entry;
339
340 if (head == NULL)
341 return;
342
343 while (!CIRCLEQ_EMPTY(head)) {
344 entry = CIRCLEQ_FIRST(head);
345 CIRCLEQ_REMOVE(head, entry, e);
346 if (entry->kw_multi != NULL)
347 free(entry->kw_multi);
348 if (entry->value != NULL)
349 free(entry->value);
350 free(entry);
351 }
352
353 free(head);
354 }
355
356 static void
357 syntaxerr_(size_t lno, const char *fmt, ...)
358 {
359 va_list args;
360
361 va_start(args, fmt);
362 fflush(NULL);
363 fprintf(stderr, "syntax error [%4zu]: ", lno);
364 fflush(NULL);
365 verrx(1, fmt, args);
366 va_end(args);
367 }

root@freewrt.org:443
ViewVC Help
Powered by ViewVC 1.1.20