| 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 |
if (strchr(cp, '\\') == NULL) |
| 225 |
continue; |
| 226 |
/* unescape backslashes */ |
| 227 |
tp = dp = str_save(cp); |
| 228 |
while ((ch = *cp++)) { |
| 229 |
if (ch == '\\') |
| 230 |
ch = *cp++; |
| 231 |
*tp++ = ch; |
| 232 |
} |
| 233 |
free(entry->value); |
| 234 |
entry->value = str_nsave(dp, tp - dp); |
| 235 |
free(dp); |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
struct parser_res * |
| 240 |
parse_lookup(struct parser_result *head, struct parser_res *sample) |
| 241 |
{ |
| 242 |
struct parser_res *rv; |
| 243 |
const struct parser_keywords *kw; |
| 244 |
char *name, *tmp; |
| 245 |
|
| 246 |
if (head == NULL || sample == NULL) |
| 247 |
return (NULL); |
| 248 |
if ((kw = parser_getkwbynum(sample->keyword, kwords)) == NULL) |
| 249 |
return (NULL); |
| 250 |
name = str_save(kw->kwprefix); |
| 251 |
switch (kw->kwtype) { |
| 252 |
case KWT_NORMAL: |
| 253 |
/* no additions */ |
| 254 |
break; |
| 255 |
case KWT_MULTI: |
| 256 |
name = str_add(name, "_"); |
| 257 |
name = str_add(name, sample->kw_multi); |
| 258 |
break; |
| 259 |
case KWT_ITERATED: |
| 260 |
name = str_add(name, "_"); |
| 261 |
name = str_add(name, tmp = xasprintf("%u", sample->kw_iter)); |
| 262 |
free(tmp); |
| 263 |
break; |
| 264 |
case KWT_MULTITER: |
| 265 |
case KWT_MULTITOP: |
| 266 |
name = str_add(name, "_"); |
| 267 |
name = str_add(name, tmp = xasprintf("%u", sample->kw_iter)); |
| 268 |
free(tmp); |
| 269 |
name = str_add(name, "_"); |
| 270 |
name = str_add(name, sample->kw_multi); |
| 271 |
break; |
| 272 |
default: |
| 273 |
abort(); |
| 274 |
} |
| 275 |
rv = parse_lookupbyname(head, name); |
| 276 |
free(name); |
| 277 |
return (rv); |
| 278 |
} |
| 279 |
|
| 280 |
char * |
| 281 |
varnameck(const char *func, const char *ivar) |
| 282 |
{ |
| 283 |
char *ovar, *cp; |
| 284 |
|
| 285 |
/* make “ivar” upper-case and validate */ |
| 286 |
cp = ovar = str_save(ivar); |
| 287 |
if (*cp >= '0' && *cp <= '9') |
| 288 |
errx(1, "%s: variable name '%s' must not begin with a digit!", |
| 289 |
func, ivar); |
| 290 |
while (*cp) { |
| 291 |
if (*cp >= 'a' && *cp <= 'z') |
| 292 |
*cp = *cp - 0x20; |
| 293 |
else if ((*cp < 'A' || *cp > 'Z') && *cp != '_' && |
| 294 |
(*cp < '0' || *cp > '9')) |
| 295 |
errx(1, "%s: variable name '%s' contains invalid" |
| 296 |
" char %02X!", func, ivar, (uint8_t)(*cp)); |
| 297 |
++cp; |
| 298 |
} |
| 299 |
return (ovar); |
| 300 |
} |
| 301 |
|
| 302 |
struct parser_res * |
| 303 |
parse_lookupbyname(struct parser_result *head, const char *name) |
| 304 |
{ |
| 305 |
struct parser_res *entry; |
| 306 |
const struct parser_keywords *kw; |
| 307 |
char *iname, *ename, *cp; |
| 308 |
bool found = false; |
| 309 |
|
| 310 |
if (head == NULL || name == NULL || *name == '\0') |
| 311 |
return (NULL); |
| 312 |
|
| 313 |
iname = varnameck(__func__, name); |
| 314 |
|
| 315 |
D(2, "parse_lookupbyname: try '%s' (orig '%s')\n", iname, name); |
| 316 |
|
| 317 |
CIRCLEQ_FOREACH(entry, head, e) { |
| 318 |
if ((kw = parser_getkwbynum(entry->keyword, kwords)) == NULL) |
| 319 |
continue; |
| 320 |
/* recreate varname */ |
| 321 |
ename = str_save(kw->kwprefix); |
| 322 |
if (entry->itype == KWT_ITERATED || |
| 323 |
entry->itype == KWT_MULTITER) { |
| 324 |
ename = str_add(ename, "_"); |
| 325 |
ename = str_add(ename, |
| 326 |
cp = xasprintf("%u", entry->kw_iter)); |
| 327 |
free(cp); |
| 328 |
} |
| 329 |
if (entry->itype == KWT_MULTI || |
| 330 |
entry->itype == KWT_MULTITER) { |
| 331 |
ename = str_add(ename, "_"); |
| 332 |
ename = str_add(ename, entry->kw_multi); |
| 333 |
} |
| 334 |
/* upper-case, normalise */ |
| 335 |
ename = varnameck(__func__, cp = ename); |
| 336 |
free(cp); |
| 337 |
if (!strcmp(iname, ename)) |
| 338 |
found = true; |
| 339 |
D(2, "parse_lookupbyname: cmp '%s', %smatch\n", ename, |
| 340 |
found ? "" : "no "); |
| 341 |
free(ename); |
| 342 |
if (found) |
| 343 |
break; |
| 344 |
} |
| 345 |
free(iname); |
| 346 |
return (found ? entry : NULL); |
| 347 |
} |