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 3487 - (show annotations) (download)
Mon Aug 13 14:55:59 2007 UTC (6 years, 6 months ago) by tg
File MIME type: text/plain
File size: 9498 byte(s)
add some more helpers for important tasks

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 char *name;
255
256 if (head == NULL || sample == NULL)
257 return (NULL);
258 if ((name = parse_fmt(sample->keyword, sample->itype,
259 sample->kw_multi, sample->kw_iter, 0)) == NULL)
260 return (NULL);
261 rv = parse_lookupbyname(head, name);
262 free(name);
263 return (rv);
264 }
265
266 char *
267 varnameck(const char *func, const char *ivar)
268 {
269 char *ovar, *cp;
270
271 /* make “ivar” upper-case and validate */
272 cp = ovar = str_save(ivar);
273 if (*cp >= '0' && *cp <= '9')
274 errx(1, "%s: variable name '%s' must not begin with a digit!",
275 func, ivar);
276 while (*cp) {
277 if (*cp >= 'a' && *cp <= 'z')
278 *cp = *cp - 0x20;
279 else if ((*cp < 'A' || *cp > 'Z') && *cp != '_' &&
280 (*cp < '0' || *cp > '9'))
281 errx(1, "%s: variable name '%s' contains invalid"
282 " char %02X!", func, ivar, (uint8_t)(*cp));
283 ++cp;
284 }
285 return (ovar);
286 }
287
288 struct parser_res *
289 parse_lookupbyname(struct parser_result *head, const char *name)
290 {
291 struct parser_res *entry;
292 char *iname, *ename, *cp;
293 bool found = false;
294
295 if (head == NULL || name == NULL || *name == '\0')
296 return (NULL);
297
298 iname = varnameck(__func__, name);
299
300 D(2, "parse_lookupbyname: try '%s' (orig '%s')\n", iname, name);
301
302 CIRCLEQ_FOREACH(entry, head, e) {
303 /* recreate varname */
304 if ((ename = parse_fmt(entry->keyword, entry->itype,
305 entry->kw_multi, entry->kw_iter, 0)) == NULL)
306 continue;
307 /* upper-case, normalise */
308 ename = varnameck(__func__, cp = ename);
309 free(cp);
310 if (!strcmp(iname, ename))
311 found = true;
312 D(2, "parse_lookupbyname: cmp '%s', %smatch\n", ename,
313 found ? "" : "no ");
314 free(ename);
315 if (found)
316 break;
317 }
318 free(iname);
319 return (found ? entry : NULL);
320 }
321
322 char *
323 parse_fmt(parser_kwords kw, enum parser_kwtype kt, char *km, unsigned ki, int u)
324 {
325 char buf[64], *res;
326 const struct parser_keywords *kwp;
327
328 if (kt == KWT_INVALID)
329 return (NULL);
330 if ((kwp = parser_getkwbynum(kw, kwords)) == NULL) {
331 if (!u)
332 return (NULL);
333 snprintf(buf, sizeof (buf), "<unknown:%u>", (int)kw);
334 res = str_save(buf);
335 } else
336 res = str_save(kwp->kwprefix);
337 if (kt == KWT_NORMAL)
338 return (res);
339 if (kt == KWT_MULTITOP)
340 kt = KWT_MULTITER;
341 if (kt == KWT_ITERATED || kt == KWT_MULTITER) {
342 snprintf(buf, sizeof (buf), "_%u", ki);
343 res = str_add(res, buf);
344 }
345 if (kt == KWT_MULTI || kt == KWT_MULTITER) {
346 buf[0] = '_';
347 strlcpy(buf + 1, km, sizeof (buf) - 1);
348 res = str_add(res, buf);
349 }
350 if (kt == KWT_MULTI || kt == KWT_ITERATED || kt == KWT_MULTITER)
351 return (res);
352 /* invalid KWT */
353 free(res);
354 return (NULL);
355 }

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