1 diff -purN bb.old/editors/awk.c bb.dev/editors/awk.c
2 --- bb.old/editors/awk.c 2007-03-06 19:38:07.278092000 +0100
3 +++ bb.dev/editors/awk.c 2007-03-11 05:14:11.776304544 +0100
5 /* these flags are static, don't change them when value is changed */
6 #define VF_DONTTOUCH (VF_ARRAY | VF_SPECIAL | VF_WALK | VF_CHILD | VF_DIRTY)
9 +#define fputs(s, stream) fputs_hook(s, stream)
10 +static inline int fputs_hook (__const char *__restrict __s, FILE *__restrict __stream);
14 typedef struct var_s {
15 unsigned short type; /* flags */
16 @@ -50,10 +55,15 @@ typedef struct chain_s {
20 +typedef var *(*awk_cfunc)(var *res, var *args, int nargs);
22 typedef struct func_s {
24 - struct chain_s body;
25 + enum { AWKFUNC, CFUNC } type;
28 + struct chain_s body;
33 @@ -1312,7 +1322,8 @@ static void parse_program(char *p)
34 next_token(TC_FUNCTION);
36 f = newfunc(t.string);
37 - f->body.first = NULL;
39 + f->x.body.first = NULL;
41 while (next_token(TC_VARIABLE | TC_SEQTERM) & TC_VARIABLE) {
42 v = findvar(ahash, t.string);
43 @@ -1321,7 +1332,7 @@ static void parse_program(char *p)
44 if (next_token(TC_COMMA | TC_SEQTERM) & TC_SEQTERM)
52 @@ -2260,7 +2271,8 @@ static var *evaluate(node *op, var *res)
56 - if (! op->r.f->body.first)
57 + if ((op->r.f->type == AWKFUNC) &&
58 + !op->r.f->x.body.first)
59 runtime_error(EMSG_UNDEF_FUNC);
61 X.v = R.v = nvalloc(op->r.f->nargs+1);
62 @@ -2277,7 +2289,11 @@ static var *evaluate(node *op, var *res)
66 - res = evaluate(op->r.f->body.first, res);
67 + if (op->r.f->type == AWKFUNC)
68 + res = evaluate(op->r.f->x.body.first, res);
69 + else if (op->r.f->type == CFUNC)
70 + res = op->r.f->x.cfunc(res, fnargs, op->r.f->nargs);
75 @@ -2637,6 +2653,11 @@ static rstream *next_input_file(void)
80 +static int is_awx = 0;
84 int awk_main(int argc, char **argv)
87 @@ -2693,6 +2714,10 @@ int awk_main(int argc, char **argv)
96 while((c = getopt(argc, argv, "F:v:f:W:")) != EOF) {
98 diff -purN bb.old/editors/awx.c bb.dev/editors/awx.c
99 --- bb.old/editors/awx.c 1970-01-01 01:00:00.000000000 +0100
100 +++ bb.dev/editors/awx.c 2007-03-11 06:25:48.552095336 +0100
103 + * awk web extension
105 + * Copyright (C) 2007 by Felix Fietkau <nbd@openwrt.org>
107 + * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
113 +#define LINE_BUF 2048
114 +#define HASH_MAX 1536
115 +#define TR_START "@TR<<"
118 +#define SSI_START "<%"
119 +#define SSI_END "%>"
123 +static xhash *lstr = NULL;
124 +static int lang_inuse = 0;
126 +/* look up a translation symbol from the hash */
127 +static inline char *translate_lookup(char *str)
129 + char *name, *def, *p;
134 + if (((p = strchr(str, '|')) != NULL)
135 + || ((p = strchr(str, '#')) != NULL)) {
140 + hi = hash_search(lstr, name);
146 + return getvar_s(v);
149 +/* look for translation markers in the line and return the translated string */
150 +static char *translate_line(char *line)
152 + char *tok[MAX_TR * 3];
153 + char *l, *p, *p2, *res;
154 + int len = 0, _pos = 0, i;
157 + while (l != NULL) {
158 + if ((p = strstr(l, TR_START)) == NULL) {
159 + len += strlen((tok[_pos++] = l));
163 + p2 = strstr(p, TR_END);
169 + len += strlen((tok[_pos++] = l));
170 + len += strlen((tok[_pos++] = translate_lookup(p + strlen(TR_START))));
173 + l += strlen(TR_END);
177 + p = xmalloc(len + 1);
180 + for (i = 0; i < _pos; i++) {
182 + p += strlen(tok[i]);
188 +/* hook for intercepting awk's use of puts. used for running all printed strings
189 + * through the translation system */
190 +static inline int fputs_hook (__const char *__restrict __s, FILE *__restrict __stream)
192 + if (lang_inuse && (__stream == stdout)) {
196 + str = translate_line((char *) __s);
197 + ret = fputs(str, __stream);
203 + return fputs(__s, __stream);
206 +static var *init_lang(var *res, var *args, int nargs)
209 + lstr = hash_init();
216 +/* load and parse language file */
217 +static void load_lang_file(char *file)
220 + char *b, *name, *value;
221 + char buf1[LINE_BUF];
223 + if ((f = fopen(file, "r")) == NULL)
226 + while (!feof(f) && (fgets(buf1, LINE_BUF - 1, f) != NULL)) {
229 + continue; /* skip comments */
231 + while (isspace(*b))
232 + b++; /* skip leading spaces */
237 + if ((b = strstr(name, "=>")) == NULL)
238 + continue; /* separator not found */
245 + for (b--; isspace(*b); b--)
246 + *b = 0; /* remove trailing spaces */
248 + while (isspace(*value))
249 + value++; /* skip leading spaces */
251 + for (b = value + strlen(value) - 1; isspace(*b); b--)
252 + *b = 0; /* remove trailing spaces */
257 + setvar_s(findvar(lstr,name), value);
263 +static var *load_lang(var *res, var *args, int nargs)
265 + const char *langfmt = "/usr/lib/webif/lang/%s.txt";
266 + char lbuf[LINE_BUF];
270 + init_lang(res, args, nargs);
272 + lang = getvar_s(args);
273 + if (!lang || !strcmp(lang, ""))
276 + sprintf(lbuf, langfmt, lang);
277 + load_lang_file(lbuf);
282 +/* read the contents of an entire file */
283 +static char *get_file(char *fname)
289 + F = fopen(fname, "r");
294 + if (fseek(F, 0, SEEK_END) == 0) {
296 + s = (char *)xmalloc(flen+4);
297 + fseek(F, 0, SEEK_SET);
298 + i = 1 + fread(s+1, 1, flen, F);
300 + for (i=j=1; j>0; i+=j) {
301 + s = (char *)xrealloc(s, i+4096);
302 + j = fread(s+i, 1, 4094, F);
314 + * taken from parse_program from awk.c
315 + * END{} is not parsed here, and BEGIN{} is executed immediately
317 +static void parse_include(char *p)
327 + memset(&initseq, 0, sizeof(initseq));
328 + while ((tclass = next_token(TC_EOF | TC_OPSEQ |
329 + TC_OPTERM | TC_BEGIN | TC_FUNCDECL)) != TC_EOF) {
330 + if (tclass & TC_OPTERM)
334 + if (tclass & TC_BEGIN) {
337 + } else if (tclass & TC_FUNCDECL) {
338 + next_token(TC_FUNCTION);
340 + f = newfunc(t.string);
342 + f->x.body.first = NULL;
344 + while (next_token(TC_VARIABLE | TC_SEQTERM) & TC_VARIABLE) {
345 + v = findvar(ahash, t.string);
346 + v->x.aidx = (f->nargs)++;
348 + if (next_token(TC_COMMA | TC_SEQTERM) & TC_SEQTERM)
351 + seq = &(f->x.body);
353 + clear_array(ahash);
357 + evaluate(initseq.first, &tv);
360 +/* include an awk file and run its BEGIN{} section */
361 +static var *include(var *res, var *args, int nargs)
363 + static xhash *includes = NULL;
367 + s = getvar_s(args);
372 + includes = hash_init();
374 + /* find out if the file has been included already */
375 + v = findvar(includes, s);
380 + /* read include file */
383 + fprintf(stderr, "Could not open file.\n");
386 + parse_include(s+1);
393 +/* parse and evaluate an awk expression and return the result as string */
394 +static char *render_lookup(char *fname, int lnr, char *str)
399 + memset(&body, 0, sizeof(body));
404 + /* end of expression, assume that there's going to be a free byte
405 + * at the end of the string that can be used for the ')' */
406 + strcat(str + strlen(str), ")");
407 + return getvar_s(evaluate(parse_expr(TC_SEQTERM), &tv));
410 +static inline void print_translate(char *s)
414 + str = translate_line(s);
415 + fputs(str, stdout);
421 +/* process awk calls in a template line and print the output to stdout */
422 +static void render_line(char *fname, int lnr, char *line)
424 + char *tok[MAX_TR * 3];
425 + char *l, *p, *p2, *res;
426 + int len = 0, _pos = 0, i;
429 + while (l != NULL) {
430 + if ((p = strstr(l, SSI_START)) == NULL) {
431 + len += strlen((tok[_pos++] = l));
435 + p2 = strstr(p, SSI_END);
437 + fprintf(stderr, "Parse error in '%s', line '%d', unmatched %s\n", fname, lnr, SSI_END);
444 + len += strlen((tok[_pos++] = l));
445 + len += strlen((tok[_pos++] = render_lookup(fname, lnr, p + strlen(SSI_START))));
448 + l += strlen(SSI_END);
452 + p = xmalloc(len + 1);
455 + for (i = 0; i < _pos; i++) {
457 + p += strlen(tok[i]);
459 + print_translate(res);
463 +/* awk method render(), which opens a template file and processes all awk ssi calls */
464 +static var *render(var *res, var *args, int nargs)
471 + buf1 = xmalloc(LINE_BUF);
472 + s = getvar_s(args);
480 + while (!feof(f) && (fgets(buf1, LINE_BUF - 1, f) != NULL)) {
481 + render_line(s, ++lnr, buf1);
489 +/* Call render, but only if this function hasn't been called already */
490 +static int layout_rendered = 0;
491 +static var *render_layout(var *res, var *args, int nargs)
493 + if (layout_rendered)
495 + layout_rendered = 1;
496 + return render(res, args, nargs);
499 +/* registers a global c function for the awk interpreter */
500 +static void register_cfunc(char *name, awk_cfunc cfunc, int nargs)
506 + f->x.cfunc = cfunc;
510 +/* function call for accessing cgi form variables */
511 +static var *getvar(var *res, var *args, int nargs)
515 + s = getvar_s(args);
517 + setvar_s(res, cgi_param(s) ?: "");
522 +/* call an awk function without arguments by string reference */
523 +static var *call(var *res, var *args, int nargs)
525 + char *s = getvar_s(args);
532 + if (f && f->type == AWKFUNC && f->x.body.first)
533 + return evaluate(f->x.body.first, res);
540 +/* main awx processing function. called from awk_main() */
541 +static int do_awx(int argc, char **argv)
553 + /* register awk C callbacks */
554 + register_cfunc("getvar", getvar, 1);
555 + register_cfunc("render", render, 1);
556 + register_cfunc("render_layout", render_layout, 1);
557 + register_cfunc("call", call, 1);
558 + register_cfunc("include", include, 1);
559 + register_cfunc("init_lang", init_lang, 1);
560 + register_cfunc("load_lang", load_lang, 1);
565 + /* fill in ARGV array */
566 + programname = argv[1];
567 + setvar_i(V[ARGC], argc + 1);
568 + setari_u(V[ARGV], 0, "awx");
571 + setari_u(V[ARGV], ++i, *argv++);
575 + fprintf(stderr, "Invalid argument.\n");
579 + /* read the main controller source */
580 + s = get_file(programname);
582 + fprintf(stderr, "Could not open file\n");
585 + parse_program(s+1);
588 + /* set some defaults for ACTION and LAYOUT, which will have special meaning */
589 + cgi_process_form();
591 + action = newvar("ACTION");
592 + setvar_s(action, cgi_param("action") ?: "default");
593 + layout = newvar("LAYOUT");
594 + setvar_s(layout, "views/layout.ahtml");
596 + /* run the BEGIN {} block */
597 + evaluate(beginseq.first, &tv);
599 + /* call the action (precedence: begin block override > cgi parameter > "default") */
600 + tmp = xmalloc(strlen(getvar_s(action)) + 7);
601 + sprintf(tmp, "handle_%s", getvar_s(action));
602 + setvar_s(action, tmp);
603 + call(&tv, action, 1);
606 + /* render the selected layout, will do nothing if render_layout has been called from awk */
607 + render_layout(&tv, layout, 1);
616 +/* entry point for awx applet */
617 +int awx_main(int argc, char **argv)
620 + return awk_main(argc, argv);
623 diff -purN bb.old/editors/Config.in bb.dev/editors/Config.in
624 --- bb.old/editors/Config.in 2007-01-24 22:34:50.000000000 +0100
625 +++ bb.dev/editors/Config.in 2007-03-11 06:19:51.469380160 +0100
626 @@ -12,6 +12,13 @@ config AWK
627 Awk is used as a pattern scanning and processing language. This is
628 the BusyBox implementation of that programming language.
631 + bool "Enable awx (awk web extension)"
635 + awx - awk web extension
637 config FEATURE_AWK_MATH
638 bool "Enable math functions (requires libm)"
640 diff -purN bb.old/include/applets.h bb.dev/include/applets.h
641 --- bb.old/include/applets.h 2007-03-06 19:38:07.355081000 +0100
642 +++ bb.dev/include/applets.h 2007-03-07 02:12:24.280681880 +0100
643 @@ -60,6 +60,7 @@ USE_ARP(APPLET(arp, _BB_DIR_SBIN, _BB_SU
644 USE_ARPING(APPLET(arping, _BB_DIR_USR_BIN, _BB_SUID_NEVER))
645 USE_ASH(APPLET_NOUSAGE(ash, ash, _BB_DIR_BIN, _BB_SUID_NEVER))
646 USE_AWK(APPLET(awk, _BB_DIR_USR_BIN, _BB_SUID_NEVER))
647 +USE_AWX(APPLET_NOUSAGE(awx, awx, _BB_DIR_USR_BIN, _BB_SUID_NEVER))
648 USE_BASENAME(APPLET(basename, _BB_DIR_USR_BIN, _BB_SUID_NEVER))
649 USE_BBCONFIG(APPLET(bbconfig, _BB_DIR_BIN, _BB_SUID_NEVER))
650 //USE_BBSH(APPLET(bbsh, _BB_DIR_BIN, _BB_SUID_NEVER))
651 diff -purN bb.old/Makefile bb.dev/Makefile
652 --- bb.old/Makefile 2007-03-06 19:38:07.358080000 +0100
653 +++ bb.dev/Makefile 2007-03-07 02:28:22.844957960 +0100
654 @@ -565,7 +565,7 @@ quiet_cmd_busybox__ ?= LINK $@
655 cmd_busybox__ ?= $(srctree)/scripts/trylink $(CC) $(LDFLAGS) \
657 -Wl,--warn-common -Wl,--sort-common -Wl,--gc-sections \
658 - -Wl,--start-group $(busybox-all) -Wl,--end-group
659 + -Wl,--start-group $(busybox-all) $(EXTRA_LIBS) -Wl,--end-group
661 # Generate System.map
662 quiet_cmd_sysmap = SYSMAP