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/pfile.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3476 - (show annotations) (download)
Fri Aug 10 11:52:23 2007 UTC (6 years, 6 months ago) by tg
File MIME type: text/plain
File size: 9593 byte(s)
add some data for input validation, to be used later

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
34 #include <err.h>
35 #include <errno.h>
36 #include <fcntl.h>
37 #include <stdbool.h>
38 #include <stdint.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
43
44 enum kwords {
45 KA_DISTFILE,
46 KA_HASH,
47 KA_RCCONF,
48 KN_DISTFILES,
49 K_BDEPS,
50 K_BROKEN,
51 K_CATEGORY,
52 K_CITEM,
53 K_CONFFILES,
54 K_CONFSYMS,
55 K_CSYM,
56 K_DASHVER,
57 K_DEFLT,
58 K_DESC,
59 K_FLAVOURS,
60 K_GROUPS,
61 K_HASHES,
62 K_HELP,
63 K_MDEPS,
64 K_MENUITEM,
65 K_MULTIPKGS,
66 K_NAME,
67 K_PERMIT_BIN,
68 K_PERMIT_SRC,
69 K_PKGDEPS,
70 K_PKGNAME,
71 K_RCCONF_SYMS,
72 K_SDEPS,
73 K_SITES,
74 K_URLS,
75 K_USERS,
76 K_VDEPS,
77 K_VERSION,
78 KX_INVALID
79 };
80 #define parser_kwords enum kwords
81
82 #include "extern.h"
83
84 static const struct parser_keywords kwords[] = {
85 /* sorted but shortest-match aware */
86 { "BROKEN", K_BROKEN, KWT_NORMAL, ARGT_STRING },
87 { "BUILDDEPS", K_BDEPS, KWT_MULTI, ARGT_STRING },
88 { "CATEGORY", K_CATEGORY, KWT_MULTI, ARGT_STRING },
89 { "CITEM", K_CITEM, KWT_MULTI, ARGT_STRING },
90 { "CONFFILES", K_CONFFILES, KWT_MULTI, ARGT_STRING },
91 { "CONFSYMS", K_CONFSYMS, KWT_NORMAL, ARGT_STRING },
92 { "CSYMDEPS", K_SDEPS, KWT_MULTI, ARGT_STRING },
93 { "CSYM", K_CSYM, KWT_MULTI, ARGT_STRING },
94 { "CVALDEPS", K_VDEPS, KWT_MULTI, ARGT_STRING },
95 { "DASHVER", K_DASHVER, KWT_NORMAL, ARGT_INTEGER },
96 { "DEFAULT", K_DEFLT, KWT_MULTI, ARGT_STRING },
97 { "DESCRIPTION", K_DESC, KWT_MULTI, ARGT_STRING },
98 { "DISTFILES", KN_DISTFILES, KWT_NORMAL, ARGT_INTEGER },
99 { "DISTFILE", KA_DISTFILE, KWT_ITERATED, ARGT_STRING },
100 { "FLAVOURS", K_FLAVOURS, KWT_MULTI, ARGT_STRING },
101 { "GROUPS", K_GROUPS, KWT_NORMAL, ARGT_STRING },
102 { "HASHES", K_HASHES, KWT_NORMAL, ARGT_STRING },
103 { "HASH", KA_HASH, KWT_MULTITOP, ARGT_STRING },
104 { "HELPTEXT", K_HELP, KWT_MULTI, ARGT_STRING },
105 { "MASTER_SITES", K_SITES, KWT_ITERATED, ARGT_STRING },
106 { "MENUDEPS", K_MDEPS, KWT_MULTI, ARGT_STRING },
107 { "MENUITEM", K_MENUITEM, KWT_MULTI, ARGT_STRING },
108 { "MULTIPKGS", K_MULTIPKGS, KWT_NORMAL, ARGT_STRING },
109 { "NAME", K_NAME, KWT_NORMAL, ARGT_STRING },
110 { "PERMIT_BIN", K_PERMIT_BIN, KWT_MULTI, ARGT_STRING },
111 { "PERMIT_SRC", K_PERMIT_SRC, KWT_NORMAL, ARGT_STRING },
112 { "PKGDEPS", K_PKGDEPS, KWT_MULTI, ARGT_STRING },
113 { "PKGNAME", K_PKGNAME, KWT_NORMAL, ARGT_STRING },
114 { "RCCONF_SYMS", K_RCCONF_SYMS, KWT_NORMAL, ARGT_INTEGER },
115 { "RCCONF", KA_RCCONF, KWT_MULTITER, ARGT_STRING },
116 { "URLS", K_URLS, KWT_NORMAL, ARGT_STRING },
117 { "USERS", K_USERS, KWT_NORMAL, ARGT_STRING },
118 { "VERSION", K_VERSION, KWT_NORMAL, ARGT_STRING },
119 { NULL, 0, 0, 0 }
120 };
121
122 static const enum kwords zeroiter[] = {
123 /* for these, an iter of 0 is set to 1 later */
124 KA_DISTFILE,
125 K_SITES,
126 KX_INVALID,
127 /* for these, an iter of 0 is an error */
128 KA_RCCONF,
129 KX_INVALID
130 };
131
132 static void do_defaultvalues(struct parser_result *);
133 static void do_varexpand(struct parser_result *);
134
135 void
136 pfile(const char *fn)
137 {
138 struct parser_result *parsed;
139 struct parser_res *entry;
140 int fd;
141 size_t nument;
142
143 if ((fd = open(fn, O_RDONLY)) < 0)
144 err(255, "cannot open input file '%s'", fn);
145 printf("parsing %s…", fn);
146 if (parser_errpfx != NULL)
147 free(parser_errpfx);
148 parser_errpfx = strdup(fn);
149 parsed = nfo_parse(fd, kwords);
150 free(parser_errpfx);
151 parser_errpfx = NULL;
152 close(fd);
153 if (CIRCLEQ_EMPTY(parsed))
154 errx(1, "error, no entries in the file!");
155 nument = 0;
156 CIRCLEQ_FOREACH(entry, parsed, e)
157 ++nument;
158 printf("ok, %zu entries\n", nument);
159
160 do_defaultvalues(parsed);
161 do_varexpand(parsed);
162
163 /* do something with ‘parsed’ */
164 nument = 0;
165 CIRCLEQ_FOREACH(entry, parsed, e) {
166 printf("ent #%03zu: ", nument++);
167 parser_dump(entry, kwords);
168 }
169
170 parser_free(parsed);
171 }
172
173 static void
174 do_defaultvalues(struct parser_result *head)
175 {
176 /* ENOCOFFEE */;
177 }
178
179 static void
180 do_varexpand(struct parser_result *head)
181 {
182 struct parser_res *entry, *evar;
183 char ch, *cp, *tp, *dp;
184 bool do_again;
185
186 expand_loop:
187 do_again = false;
188 CIRCLEQ_FOREACH(entry, head, e) {
189 if ((cp = entry->value) == NULL)
190 continue;
191 /* scan the value for variable references */
192 while ((ch = *cp++))
193 if (ch == '\\')
194 cp++;
195 else if (ch == '$')
196 break;
197 if (!ch)
198 continue;
199 /* cp points past a dollar sign, copy head */
200 dp = str_nsave(entry->value, cp - entry->value - 1);
201 /* read variable name */
202 if (*cp != '{' /*}*/)
203 goto copy_rest;
204 tp = ++cp;
205 while (*tp && (*tp != /*{*/ '}'))
206 ++tp;
207 if (*tp != /*{*/ '}')
208 goto copy_rest;
209 *tp++ = '\0';
210 /* variable name in cp, rest of string in tp */
211 evar = parse_lookupbyname(head, cp);
212 tp[-1] = /*{*/ '}';
213 cp = tp;
214 /* variable content in evar, rest of string in cp */
215 if (evar && evar->value)
216 dp = str_add(dp, evar->value);
217 else if (!evar)
218 D(1, "warning: expansion in '%s' undefined for '%s'\n",
219 entry->value, cp);
220 copy_rest:
221 dp = str_add(dp, cp);
222 D(2, "do_varexpand: '%s' -> '%s'\n", entry->value, dp);
223 free(entry->value);
224 entry->value = dp;
225 /* string was modified, so reloop */
226 do_again = true;
227 break;
228 }
229 if (do_again)
230 goto expand_loop;
231 /* all variable references have been expanded */
232 CIRCLEQ_FOREACH(entry, head, e) {
233 if ((cp = entry->value) == NULL)
234 continue;
235 if (strchr(cp, '\\') == NULL)
236 continue;
237 /* unescape backslashes */
238 tp = dp = str_save(cp);
239 while ((ch = *cp++)) {
240 if (ch == '\\')
241 ch = *cp++;
242 *tp++ = ch;
243 }
244 free(entry->value);
245 entry->value = str_nsave(dp, tp - dp);
246 free(dp);
247 }
248 }
249
250 struct parser_res *
251 parse_lookup(struct parser_result *head, struct parser_res *sample)
252 {
253 struct parser_res *rv;
254 const struct parser_keywords *kw;
255 char *name, *tmp;
256
257 if (head == NULL || sample == NULL)
258 return (NULL);
259 if ((kw = parser_getkwbynum(sample->keyword, kwords)) == NULL)
260 return (NULL);
261 name = str_save(kw->kwprefix);
262 switch (kw->kwtype) {
263 case KWT_NORMAL:
264 /* no additions */
265 break;
266 case KWT_MULTI:
267 name = str_add(name, "_");
268 name = str_add(name, sample->kw_multi);
269 break;
270 case KWT_ITERATED:
271 name = str_add(name, "_");
272 name = str_add(name, tmp = xasprintf("%u", sample->kw_iter));
273 free(tmp);
274 break;
275 case KWT_MULTITER:
276 case KWT_MULTITOP:
277 name = str_add(name, "_");
278 name = str_add(name, tmp = xasprintf("%u", sample->kw_iter));
279 free(tmp);
280 name = str_add(name, "_");
281 name = str_add(name, sample->kw_multi);
282 break;
283 default:
284 abort();
285 }
286 rv = parse_lookupbyname(head, name);
287 free(name);
288 return (rv);
289 }
290
291 char *
292 varnameck(const char *func, const char *ivar)
293 {
294 char *ovar, *cp;
295
296 /* make “ivar” upper-case and validate */
297 cp = ovar = str_save(ivar);
298 if (*cp >= '0' && *cp <= '9')
299 errx(1, "%s: variable name '%s' must not begin with a digit!",
300 func, ivar);
301 while (*cp) {
302 if (*cp >= 'a' && *cp <= 'z')
303 *cp = *cp - 0x20;
304 else if ((*cp < 'A' || *cp > 'Z') && *cp != '_' &&
305 (*cp < '0' || *cp > '9'))
306 errx(1, "%s: variable name '%s' contains invalid"
307 " char %02X!", func, ivar, (uint8_t)(*cp));
308 ++cp;
309 }
310 return (ovar);
311 }
312
313 struct parser_res *
314 parse_lookupbyname(struct parser_result *head, const char *name)
315 {
316 struct parser_res *entry;
317 const struct parser_keywords *kw;
318 char *iname, *ename, *cp;
319 bool found = false;
320
321 if (head == NULL || name == NULL || *name == '\0')
322 return (NULL);
323
324 iname = varnameck(__func__, name);
325
326 D(2, "parse_lookupbyname: try '%s' (orig '%s')\n", iname, name);
327
328 CIRCLEQ_FOREACH(entry, head, e) {
329 if ((kw = parser_getkwbynum(entry->keyword, kwords)) == NULL)
330 continue;
331 /* recreate varname */
332 ename = str_save(kw->kwprefix);
333 if (entry->itype == KWT_ITERATED ||
334 entry->itype == KWT_MULTITER) {
335 ename = str_add(ename, "_");
336 ename = str_add(ename,
337 cp = xasprintf("%u", entry->kw_iter));
338 free(cp);
339 }
340 if (entry->itype == KWT_MULTI ||
341 entry->itype == KWT_MULTITER) {
342 ename = str_add(ename, "_");
343 ename = str_add(ename, entry->kw_multi);
344 }
345 /* upper-case, normalise */
346 ename = varnameck(__func__, cp = ename);
347 free(cp);
348 if (!strcmp(iname, ename))
349 found = true;
350 D(2, "parse_lookupbyname: cmp '%s', %smatch\n", ename,
351 found ? "" : "no ");
352 free(ename);
353 if (found)
354 break;
355 }
356 free(iname);
357 return (found ? entry : NULL);
358 }

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