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 3472 - (show annotations) (download)
Thu Aug 9 16:38:17 2007 UTC (6 years, 6 months ago) by tg
File MIME type: text/plain
File size: 9306 byte(s)
more debugging, and fix varexpand to be actually working

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

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