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 3468 - (show annotations) (download)
Thu Aug 9 15:46:10 2007 UTC (6 years, 6 months ago) by tg
File MIME type: text/plain
File size: 6412 byte(s)
algorithm for variable expansion, dead simple:
• first, variables are looked up:
  - ${FOO} -> content of FOO
  syntax errors are handled by eating the dollar sign and braces:
  - $x -> x
  - ${x -> x
• then, all first backslashes are removed

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 to a dollar sign, copy head */
189 dp = str_nsave(entry->value, cp - entry->value);
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 cp = tp;
202 /* variable content in evar, rest of string in cp */
203 if (evar && evar->value)
204 dp = str_add(dp, evar->value);
205 copy_rest:
206 dp = str_add(dp, cp);
207 free(entry->value);
208 entry->value = dp;
209 /* string was modified, so reloop */
210 do_again = true;
211 break;
212 }
213 if (do_again)
214 goto expand_loop;
215 /* all variable references have been expanded */
216 CIRCLEQ_FOREACH(entry, head, e) {
217 if ((cp = entry->value) == NULL)
218 continue;
219 /* unescape backslashes */
220 tp = dp = str_save(cp);
221 while ((ch = *cp++)) {
222 if (ch == '\\')
223 ch = *cp++;
224 *tp++ = ch;
225 }
226 free(entry->value);
227 entry->value = dp;
228 }
229 }

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