6399717e00f5de6cb43efc614a001c93a84b41cf
[openwrt/svn-archive/archive.git] / package / busybox / patches / 920-awx.patch
1 diff -urN bb.old/editors/awk.c bb.dev/editors/awk.c
2 --- bb.old/editors/awk.c 2007-05-20 04:17:05.002197784 +0200
3 +++ bb.dev/editors/awk.c 2007-05-20 22:40:48.183743936 +0200
4 @@ -30,6 +30,11 @@
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)
7
8 +#ifdef CONFIG_AWX
9 +#define fputs(s, stream) fputs_hook(s, stream)
10 +static inline int fputs_hook (__const char *__restrict __s, FILE *__restrict __stream);
11 +#endif
12 +
13 /* Variable */
14 typedef struct var_s {
15 unsigned short type; /* flags */
16 @@ -50,10 +55,15 @@
17 char *programname;
18 } chain;
19
20 +typedef var *(*awk_cfunc)(var *res, var *args, int nargs);
21 /* Function */
22 typedef struct func_s {
23 unsigned short nargs;
24 - struct chain_s body;
25 + enum { AWKFUNC, CFUNC } type;
26 + union {
27 + awk_cfunc cfunc;
28 + struct chain_s body;
29 + } x;
30 } func;
31
32 /* I/O stream */
33 @@ -1312,7 +1322,8 @@
34 next_token(TC_FUNCTION);
35 pos++;
36 f = newfunc(t.string);
37 - f->body.first = NULL;
38 + f->type = AWKFUNC;
39 + f->x.body.first = NULL;
40 f->nargs = 0;
41 while (next_token(TC_VARIABLE | TC_SEQTERM) & TC_VARIABLE) {
42 v = findvar(ahash, t.string);
43 @@ -1321,7 +1332,7 @@
44 if (next_token(TC_COMMA | TC_SEQTERM) & TC_SEQTERM)
45 break;
46 }
47 - seq = &(f->body);
48 + seq = &(f->x.body);
49 chain_group();
50 clear_array(ahash);
51
52 @@ -2260,7 +2271,8 @@
53 break;
54
55 case XC( OC_FUNC ):
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);
60
61 X.v = R.v = nvalloc(op->r.f->nargs+1);
62 @@ -2277,7 +2289,11 @@
63 fnargs = X.v;
64
65 L.s = programname;
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);
71 +
72 programname = L.s;
73
74 nvfree(fnargs);
75 @@ -2637,6 +2653,11 @@
76 return &rsm;
77 }
78
79 +#ifdef CONFIG_AWX
80 +static int is_awx = 0;
81 +#include "awx.c"
82 +#endif
83 +
84 int awk_main(int argc, char **argv)
85 {
86 int i, j, flen;
87 @@ -2693,6 +2714,10 @@
88 free(s);
89 }
90
91 +#ifdef CONFIG_AWX
92 + do_awx(argc, argv);
93 +#endif
94 +
95 programname = NULL;
96 while((c = getopt(argc, argv, "F:v:f:W:")) != EOF) {
97 switch (c) {
98 diff -urN 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-05-23 19:13:40.459655704 +0200
101 @@ -0,0 +1,632 @@
102 +/*
103 + * awk web extension
104 + *
105 + * Copyright (C) 2007 by Felix Fietkau <nbd@openwrt.org>
106 + *
107 + * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
108 + */
109 +
110 +#include <cgi.h>
111 +#include <glob.h>
112 +#include "awx_parser.h"
113 +
114 +#define LINE_BUF 2048
115 +#define HASH_MAX 1536
116 +#define TR_START "@TR<<"
117 +#define TR_END ">>"
118 +#define MAX_TR 32
119 +
120 +#undef fputs
121 +
122 +static xhash *lstr = NULL;
123 +static xhash *formvar = NULL;
124 +static int lang_inuse = 0;
125 +
126 +/* look up a translation symbol from the hash */
127 +static inline char *translate_lookup(char *str)
128 +{
129 + char *name, *def, *p;
130 + hash_item *hi;
131 + var *v;
132 +
133 + def = name = str;
134 + if (((p = strchr(str, '|')) != NULL)
135 + || ((p = strchr(str, '#')) != NULL)) {
136 + def = p + 1;
137 + *p = 0;
138 + }
139 +
140 + hi = hash_search(lstr, name);
141 + if (!hi)
142 + return def;
143 +
144 + v = &hi->data.v;
145 +
146 + return getvar_s(v);
147 +}
148 +
149 +/* look for translation markers in the line and return the translated string */
150 +static char *translate_line(char *line)
151 +{
152 + char *tok[MAX_TR * 3];
153 + char *l, *p, *p2 = NULL, *res;
154 + int len = 0, _pos = 0, i, tr_abort = 0;
155 + static char *backlog = NULL;
156 +
157 + if (backlog && line) {
158 + backlog = xrealloc(backlog, strlen(backlog) + strlen(line) + 1);
159 + sprintf(backlog + strlen(backlog), line);
160 + l = backlog;
161 + } else {
162 + l = line;
163 + }
164 +
165 + while (l != NULL) {
166 + if ((p = strstr(l, TR_START)) == NULL) {
167 + len += strlen((tok[_pos++] = l));
168 + break;
169 + }
170 +
171 + p2 = strstr(p, TR_END);
172 + if (p2 == NULL) {
173 + p2 = xstrdup(p);
174 + tr_abort = 1;
175 + break;
176 + }
177 +
178 + *p = 0;
179 + *p2 = 0;
180 + len += strlen((tok[_pos++] = l));
181 + len += strlen((tok[_pos++] = translate_lookup(p + strlen(TR_START))));
182 +
183 + l = p2;
184 + l += strlen(TR_END);
185 + }
186 + len++;
187 +
188 + p = xmalloc(len + 1);
189 + *p = 0;
190 + res = p;
191 + for (i = 0; i < _pos; i++) {
192 + strcat(p, tok[i]);
193 + p += strlen(tok[i]);
194 + }
195 + if (tr_abort && p2) {
196 + if (backlog)
197 + free(backlog);
198 + backlog = p2;
199 + }
200 +
201 + return res;
202 +}
203 +
204 +/* hook for intercepting awk's use of puts. used for running all printed strings
205 + * through the translation system */
206 +static inline int fputs_hook (__const char *__restrict __s, FILE *__restrict __stream)
207 +{
208 + if (lang_inuse && (__stream == stdout)) {
209 + int ret;
210 + char *str;
211 +
212 + str = translate_line((char *) __s);
213 + ret = fputs(str, __stream);
214 + free(str);
215 +
216 + return ret;
217 + }
218 +
219 + return fputs(__s, __stream);
220 +}
221 +
222 +static var *init_lang(var *res, var *args, int nargs)
223 +{
224 + if (!lstr)
225 + lstr = hash_init();
226 +
227 + lang_inuse = 1;
228 + return res;
229 +}
230 +
231 +
232 +/* load and parse language file */
233 +static void load_lang_file(char *file)
234 +{
235 + FILE *f;
236 + char *b, *name, *value;
237 + char buf1[LINE_BUF];
238 +
239 + if ((f = fopen(file, "r")) == NULL)
240 + return;
241 +
242 + while (!feof(f) && (fgets(buf1, LINE_BUF - 1, f) != NULL)) {
243 + b = buf1;
244 + if (*b == '#')
245 + continue; /* skip comments */
246 +
247 + while (isspace(*b))
248 + b++; /* skip leading spaces */
249 + if (!*b)
250 + continue;
251 +
252 + name = b;
253 + if ((b = strstr(name, "=>")) == NULL)
254 + continue; /* separator not found */
255 +
256 + value = b + 2;
257 + if (!*value)
258 + continue;
259 +
260 + *b = 0;
261 + for (b--; isspace(*b); b--)
262 + *b = 0; /* remove trailing spaces */
263 +
264 + while (isspace(*value))
265 + value++; /* skip leading spaces */
266 +
267 + for (b = value + strlen(value) - 1; isspace(*b); b--)
268 + *b = 0; /* remove trailing spaces */
269 +
270 + if (!*value)
271 + continue;
272 +
273 + setvar_s(findvar(lstr,name), value);
274 + }
275 +
276 + fclose(f);
277 +}
278 +
279 +static var *load_lang(var *res, var *args, int nargs)
280 +{
281 + const char *langfmt = "/usr/lib/webif/lang/%s.txt";
282 + char lbuf[LINE_BUF];
283 + char *lang;
284 +
285 + if (!lang_inuse)
286 + init_lang(res, args, nargs);
287 +
288 + lang = getvar_s(args);
289 + if (!lang || !strcmp(lang, ""))
290 + return res;
291 +
292 + sprintf(lbuf, langfmt, lang);
293 + load_lang_file(lbuf);
294 +
295 + return res;
296 +}
297 +
298 +/* read the contents of an entire file */
299 +static char *get_file(char *fname)
300 +{
301 + FILE *F;
302 + char *s = NULL;
303 + int i, j, flen;
304 +
305 + F = fopen(fname, "r");
306 + if (!F) {
307 + return NULL;
308 + }
309 +
310 + if (fseek(F, 0, SEEK_END) == 0) {
311 + flen = ftell(F);
312 + s = (char *)xmalloc(flen+4);
313 + fseek(F, 0, SEEK_SET);
314 + i = 1 + fread(s+1, 1, flen, F);
315 + } else {
316 + for (i=j=1; j>0; i+=j) {
317 + s = (char *)xrealloc(s, i+4096);
318 + j = fread(s+i, 1, 4094, F);
319 + }
320 + }
321 +
322 + s[i] = '\0';
323 + fclose(F);
324 + return s;
325 +}
326 +
327 +
328 +/* parse_include():
329 + *
330 + * taken from parse_program from awk.c
331 + * END{} is not parsed here, and BEGIN{} is executed immediately
332 + */
333 +static void parse_include(char *p)
334 +{
335 + uint32_t tclass;
336 + chain *initseq = NULL;
337 + chain tmp;
338 + func *f;
339 + var *v, *tv;
340 +
341 + tv = nvalloc(1);
342 + memset(&tmp, 0, sizeof(tmp));
343 + pos = p;
344 + t.lineno = 1;
345 + while ((tclass = next_token(TC_EOF | TC_OPSEQ |
346 + TC_OPTERM | TC_BEGIN | TC_FUNCDECL)) != TC_EOF) {
347 + if (tclass & TC_OPTERM)
348 + continue;
349 +
350 + seq = &tmp;
351 + if (tclass & TC_BEGIN) {
352 + initseq = xzalloc(sizeof(chain));
353 + seq = initseq;
354 + chain_group();
355 + } else if (tclass & TC_FUNCDECL) {
356 + next_token(TC_FUNCTION);
357 + pos++;
358 + f = newfunc(t.string);
359 + f->type = AWKFUNC;
360 + f->x.body.first = NULL;
361 + f->nargs = 0;
362 + while (next_token(TC_VARIABLE | TC_SEQTERM) & TC_VARIABLE) {
363 + v = findvar(ahash, t.string);
364 + v->x.aidx = (f->nargs)++;
365 +
366 + if (next_token(TC_COMMA | TC_SEQTERM) & TC_SEQTERM)
367 + break;
368 + }
369 + seq = &(f->x.body);
370 + chain_group();
371 + clear_array(ahash);
372 + }
373 + }
374 + if (initseq && initseq->first)
375 + tv = evaluate(initseq->first, tv);
376 + nvfree(tv);
377 +}
378 +
379 +
380 +/* include an awk file and run its BEGIN{} section */
381 +static xhash *includes = NULL;
382 +static void include_file(char *filename)
383 +{
384 + char *s;
385 + var *v;
386 +
387 + if (!includes)
388 + includes = hash_init();
389 +
390 + /* find out if the file has been included already */
391 + v = findvar(includes, filename);
392 + if (istrue(v))
393 + return;
394 + setvar_s(v, "1");
395 +
396 + /* read include file */
397 + s = get_file(filename);
398 + if (!s) {
399 + fprintf(stderr, "Could not open file.\n");
400 + return;
401 + }
402 + parse_include(s+1);
403 + free(s);
404 +}
405 +
406 +static var *include(var *res, var *args, int nargs)
407 +{
408 + char *s;
409 +
410 + s = getvar_s(args);
411 + if (s && (strlen(s) > 0))
412 + include_file(s);
413 +
414 + return res;
415 +}
416 +
417 +/* parse an awk expression */
418 +static var *parse_awk(char *str, var *tv)
419 +{
420 + chain body;
421 + node *n;
422 +
423 + memset(&body, 0, sizeof(body));
424 + pos = str;
425 + seq = &body;
426 +
427 + /* end of expression, assume that there's going to be a free byte
428 + * at the end of the string that can be used for the ')' */
429 + strcat(str + strlen(str), "}");
430 + n = parse_expr(TC_GRPTERM);
431 + if (!n)
432 + return NULL;
433 +
434 + return evaluate(n, tv);
435 +}
436 +
437 +static inline void print_translate(char *s)
438 +{
439 + char *str = s;
440 + if (lang_inuse)
441 + str = translate_line(s);
442 + fputs(str, stdout);
443 + fflush(stdout);
444 + if (lang_inuse)
445 + free(str);
446 +}
447 +
448 +static void render_element(struct template_cb *tcb, struct template_element *e)
449 +{
450 + var *v;
451 + char *s, *s2;
452 + int i;
453 +
454 + if (!e || !e->var)
455 + return;
456 + lineno = e->line;
457 + switch (e->t) {
458 + case T_TEXT:
459 + s = malloc(strlen(e->var) + 2);
460 + strcpy(s, e->var);
461 + print_translate(s);
462 + free(s);
463 + break;
464 + case T_CODE:
465 + s = malloc(strlen(e->var) + 2);
466 + strcpy(s, e->var);
467 + v = nvalloc(1);
468 + s2 = strdup(getvar_s(parse_awk(s, v)));
469 + nvfree(v);
470 + print_translate(s2);
471 + free(s);
472 + free(s2);
473 + break;
474 + case T_IF:
475 + s = malloc(strlen(e->var) + 2);
476 + strcpy(s, e->var);
477 + v = nvalloc(1);
478 + i = istrue(parse_awk(s, v));
479 + nvfree(v);
480 + free(s);
481 +
482 + if (i)
483 + execute_template(tcb, e->sub);
484 + else if (e->sub2)
485 + execute_template(tcb, e->sub2);
486 + break;
487 + case T_FOR: {
488 + v = newvar(e->var);
489 + hashwalk_init(v, iamarray(findvar(vhash, e->in)));
490 + while (hashwalk_next(v)) {
491 + execute_template(tcb, e->sub);
492 + }
493 + clrvar(v);
494 + }
495 + break;
496 + default:
497 + break;
498 + }
499 +}
500 +
501 +/* awk method render(), which opens a template file and processes all awk ssi calls */
502 +static void render_file(char *filename)
503 +{
504 + struct template_cb tcb;
505 + struct template_element *e;
506 + FILE *f;
507 + char *oldprg;
508 + int oldlnr;
509 +
510 + if (!filename)
511 + return;
512 +
513 + oldlnr = lineno;
514 + oldprg = programname;
515 + programname = filename;
516 +
517 + f = fopen(filename, "r");
518 + if (!f)
519 + return;
520 +
521 + memset(&tcb, 0, sizeof(tcb));
522 + tcb.handle_element = render_element;
523 + e = parse_template(&tcb, f);
524 + execute_template(&tcb, e);
525 + free_template(&tcb, e);
526 + fclose(f);
527 + programname = oldprg;
528 + lineno = oldlnr;
529 +}
530 +
531 +static var *render(var *res, var *args, int nargs)
532 +{
533 + char *s;
534 +
535 + s = getvar_s(args);
536 + if (!s)
537 + return res;
538 +
539 + render_file(s);
540 +
541 + return res;
542 +}
543 +
544 +/* Call render, but only if this function hasn't been called already */
545 +static int layout_rendered = 0;
546 +static var *render_layout(var *res, var *args, int nargs)
547 +{
548 + if (layout_rendered)
549 + return res;
550 + layout_rendered = 1;
551 + return render(res, args, nargs);
552 +}
553 +
554 +/* registers a global c function for the awk interpreter */
555 +static void register_cfunc(char *name, awk_cfunc cfunc, int nargs)
556 +{
557 + func *f;
558 +
559 + f = newfunc(name);
560 + f->type = CFUNC;
561 + f->x.cfunc = cfunc;
562 + f->nargs = nargs;
563 +}
564 +
565 +static void putvar(vartype type, char *name, char *value)
566 +{
567 + if (type != FORM_VAR)
568 + return;
569 +
570 + setvar_u(findvar(formvar, name), value);
571 +}
572 +
573 +static char *cgi_getvar(char *name)
574 +{
575 + if (!formvar) {
576 + formvar = hash_init();
577 + cgi_init(putvar);
578 + }
579 +
580 + if (!formvar || !name)
581 + return NULL;
582 +
583 + return getvar_s(findvar(formvar, name));
584 +}
585 +
586 +/* function call for accessing cgi form variables */
587 +static var *getvar(var *res, var *args, int nargs)
588 +{
589 + char *s;
590 + char *svar;
591 +
592 + s = getvar_s(args);
593 + if (!s)
594 + return res;
595 +
596 + svar = cgi_getvar(s);
597 + if (!svar)
598 + return res;
599 +
600 + setvar_u(res, svar);
601 +
602 + return res;
603 +}
604 +
605 +/* call an awk function without arguments by string reference */
606 +static var *call(var *res, var *args, int nargs)
607 +{
608 + char *s = getvar_s(args);
609 + func *f;
610 +
611 + if (!s)
612 + goto done;
613 +
614 + f = newfunc(s);
615 + if (f && f->type == AWKFUNC && f->x.body.first)
616 + return evaluate(f->x.body.first, res);
617 +
618 +done:
619 + return res;
620 +}
621 +
622 +
623 +static int run_awxscript(char *name)
624 +{
625 + var tv, *layout, *action;
626 + char *tmp, *s = NULL;
627 +
628 + zero_out_var(&tv);
629 + programname = name;
630 +
631 + /* read the main controller source */
632 + s = get_file(programname);
633 + if (!s) {
634 + fprintf(stderr, "Could not open file\n");
635 + return 1;
636 + }
637 + parse_program(s+1);
638 + free(s);
639 +
640 +
641 + /* set some defaults for ACTION and LAYOUT, which will have special meaning */
642 + layout = newvar("LAYOUT");
643 + setvar_s(layout, "views/layout.ahtml");
644 +
645 + /* run the BEGIN {} block */
646 + evaluate(beginseq.first, &tv);
647 +
648 + action = newvar("ACTION");
649 + if (!(strlen(getvar_s(action)) > 0)) {
650 + tmp = cgi_getvar("action");
651 + if (!tmp || (strlen(tmp) <= 0))
652 + tmp = strdup("default");
653 +
654 + setvar_p(action, tmp);
655 + }
656 +
657 + /* call the action (precedence: begin block override > cgi parameter > "default") */
658 + tmp = xmalloc(strlen(getvar_s(action)) + 7);
659 + sprintf(tmp, "handle_%s", getvar_s(action));
660 + setvar_s(action, tmp);
661 + call(&tv, action, 1);
662 + free(tmp);
663 +
664 + /* render the selected layout, will do nothing if render_layout has been called from awk */
665 + render_layout(&tv, layout, 1);
666 +
667 + return 0;
668 +}
669 +
670 +
671 +/* main awx processing function. called from awk_main() */
672 +static int do_awx(int argc, char **argv)
673 +{
674 + int ret = -1;
675 + var tv;
676 + int i, c;
677 + char **args = argv;
678 +
679 + zero_out_var(&tv);
680 +
681 + /* register awk C callbacks */
682 + register_cfunc("getvar", getvar, 1);
683 + register_cfunc("render", render, 1);
684 + register_cfunc("render_layout", render_layout, 1);
685 + register_cfunc("call", call, 1);
686 + register_cfunc("include", include, 1);
687 + register_cfunc("init_lang", init_lang, 1);
688 + register_cfunc("load_lang", load_lang, 1);
689 +
690 + if (!is_awx)
691 + return 0;
692 +
693 + /* fill in ARGV array */
694 + setvar_i(V[ARGC], argc + 1);
695 + setari_u(V[ARGV], 0, "awx");
696 + i = 0;
697 + while (*args)
698 + setari_u(V[ARGV], ++i, *args++);
699 +
700 + while((c = getopt(argc, argv, "i:f:")) != EOF) {
701 + switch(c) {
702 + case 'i':
703 + programname = optarg;
704 + include_file(optarg);
705 + break;
706 + case 'f':
707 + ret = 0;
708 + programname = optarg;
709 + render_file(optarg);
710 + goto done;
711 + }
712 + }
713 + argc -= optind;
714 + argv += optind;
715 +
716 + if (argc < 1) {
717 + fprintf(stderr, "Invalid argument.\n");
718 + goto done;
719 + }
720 +
721 + ret = run_awxscript(*argv);
722 +
723 +done:
724 + exit(ret);
725 +}
726 +
727 +/* entry point for awx applet */
728 +int awx_main(int argc, char **argv)
729 +{
730 + is_awx = 1;
731 + return awk_main(argc, argv);
732 +}
733 +
734 diff -urN bb.old/editors/awx_parser.h bb.dev/editors/awx_parser.h
735 --- bb.old/editors/awx_parser.h 1970-01-01 01:00:00.000000000 +0100
736 +++ bb.dev/editors/awx_parser.h 2007-05-20 22:30:31.380512280 +0200
737 @@ -0,0 +1,38 @@
738 +#ifndef __TEMPLATE_PARSER_H
739 +#define __TEMPLATE_PARSER_H
740 +
741 +enum type {
742 + T_TEXT,
743 + T_FOR,
744 + T_IF,
745 + T_CODE
746 +};
747 +
748 +struct template_element;
749 +struct template_cb;
750 +
751 +struct template_cb {
752 + void *(*prepare_code)(struct template_element *);
753 + void (*handle_element)(struct template_cb *, struct template_element *);
754 + void (*free_code)(struct template_element *);
755 +};
756 +
757 +struct template_element {
758 + enum type t;
759 + char *var;
760 + char *in;
761 + int line;
762 + void *priv;
763 + struct template_element *parent;
764 + struct template_element *sub;
765 + struct template_element *sub2;
766 + struct template_element *prev;
767 + struct template_element *next;
768 +};
769 +
770 +
771 +struct template_element *parse_template(struct template_cb *cb, FILE *in);
772 +void execute_template(struct template_cb *cb, struct template_element *e);
773 +void free_template(struct template_cb *cb, struct template_element *e);
774 +
775 +#endif
776 diff -urN bb.old/editors/awx_parser.l bb.dev/editors/awx_parser.l
777 --- bb.old/editors/awx_parser.l 1970-01-01 01:00:00.000000000 +0100
778 +++ bb.dev/editors/awx_parser.l 2007-05-23 19:13:40.459655704 +0200
779 @@ -0,0 +1,302 @@
780 +%{
781 +#include <stdio.h>
782 +#include <string.h>
783 +#include <stdlib.h>
784 +#include "busybox.h"
785 +#include "awx_parser.h"
786 +
787 +enum {
788 + S_INIT,
789 + S_TEXT,
790 + S_CODE,
791 + S_IF_START,
792 + S_FOR_START,
793 + S_FOR_IN,
794 + S_END,
795 + S_ELSE,
796 + S_EOF
797 +};
798 +int state;
799 +
800 +#undef DEBUG
801 +#ifdef DEBUG
802 +char *statestr[] = {
803 + [S_INIT] = "S_INIT",
804 + [S_TEXT] = "S_TEXT",
805 + [S_CODE] = "S_CODE",
806 + [S_IF_START] = "S_IF_START",
807 + [S_FOR_START] = "S_FOR_START",
808 + [S_FOR_IN] = "S_FOR_IN",
809 + [S_EOF] = "S_EOF"
810 +};
811 +
812 +char *typestr[] = {
813 + [T_TEXT] = "T_TEXT",
814 + [T_FOR] = "T_FOR",
815 + [T_IF] = "T_IF",
816 + [T_CODE] = "T_CODE"
817 +};
818 +#endif
819 +
820 +static struct template_cb *parse_cb;
821 +static struct template_element *cur, *head;
822 +static char *textbuf;
823 +static unsigned int buflen;
824 +static unsigned int buf_offset;
825 +static int _lnr = 0;
826 +
827 +static void buf_realloc(void)
828 +{
829 + buflen *= 2;
830 + textbuf = xrealloc(textbuf, buflen);
831 +}
832 +
833 +static void parse_error(char *str)
834 +{
835 + fprintf(stderr, "Parse error%s%s\n", (str ? ": " : "!"), (str ?: ""));
836 + exit(255);
837 +}
838 +
839 +
840 +static struct template_element *new_template_element(struct template_element *parent)
841 +{
842 + struct template_element *ptr;
843 +
844 + ptr = xzalloc(sizeof(struct template_element));
845 + ptr->parent = parent;
846 + return ptr;
847 +}
848 +
849 +static inline void next_template_element(void)
850 +{
851 + cur->next = new_template_element(cur->parent);
852 + cur->next->prev = cur;
853 + cur = cur->next;
854 +}
855 +
856 +static void addtext(char *text)
857 +{
858 + while(buf_offset + strlen(text) + 1 > buflen)
859 + buf_realloc();
860 +
861 + buf_offset += sprintf(&textbuf[buf_offset], "%s", text);
862 +}
863 +
864 +static void set_state(int newstate)
865 +{
866 + char *ptr;
867 +
868 +#ifdef DEBUG
869 + static int _rec = 0;
870 + fprintf(stderr, "DEBUG(%d): %s => %s: %s\n", _rec, statestr[state], statestr[newstate], textbuf);
871 +#endif
872 + ptr = xstrdup(textbuf);
873 + if (state == S_FOR_IN)
874 + cur->in = ptr;
875 + else
876 + cur->var = ptr;
877 +
878 + if (parse_cb && (cur->t == T_CODE) && parse_cb->prepare_code)
879 + parse_cb->prepare_code(cur);
880 +
881 + buf_offset = 0;
882 + *textbuf = 0;
883 +
884 + switch(newstate) {
885 +#if 0
886 + case S_EOF:
887 + if (cur->parent)
888 + parse_error();
889 + break;
890 +#endif
891 + case S_FOR_START:
892 + if (ptr || !cur->prev)
893 + next_template_element();
894 + cur->t = T_FOR;
895 + break;
896 + case S_IF_START:
897 + if (ptr || !cur->prev)
898 + next_template_element();
899 + cur->t = T_IF;
900 + break;
901 + case S_ELSE:
902 + cur = cur->parent;
903 + if (!cur)
904 + parse_error("'@else' without parent element");
905 + cur->sub2 = new_template_element(cur);
906 + cur = cur->sub2;
907 + newstate = S_TEXT;
908 + break;
909 + case S_END:
910 +#ifdef DEBUG
911 + _rec--;
912 +#endif
913 + cur = cur->parent;
914 + if (!cur)
915 + parse_error("'@end' without parent element");
916 +
917 + next_template_element();
918 + cur->t = T_TEXT;
919 + newstate = S_TEXT;
920 + break;
921 + case S_TEXT:
922 + switch (cur->t) {
923 + case T_CODE:
924 + next_template_element();
925 + break;
926 + case T_IF:
927 + case T_FOR:
928 +#ifdef DEBUG
929 + _rec++;
930 +#endif
931 + cur->sub = new_template_element(cur);
932 + cur = cur->sub;
933 + break;
934 + default:
935 + break;
936 + }
937 + cur->t = T_TEXT;
938 + break;
939 + case S_CODE:
940 + if (ptr || !cur->prev)
941 + next_template_element();
942 + cur->t = T_CODE;
943 + break;
944 + default:
945 + break;
946 + }
947 + cur->line = _lnr;
948 + state = newstate;
949 +}
950 +
951 +%}
952 +
953 +%%
954 +"<%"[ \n\t]*"@if"[ \n\t]+ {
955 + if (state == S_TEXT)
956 + set_state(S_IF_START);
957 + else
958 + REJECT;
959 +}
960 +
961 +"<%"[ \n\t]*"@for"[ \n\t]+ {
962 + if (state == S_TEXT)
963 + set_state(S_FOR_START);
964 + else
965 + REJECT;
966 +}
967 +
968 +[ \n\t]+"in"[ \n\t]+ {
969 + if (state == S_FOR_START)
970 + set_state(S_FOR_IN);
971 + else
972 + REJECT;
973 +}
974 +
975 +"<%"[ \n\t]*"@end"[ \n\t]*%> {
976 + if (state != S_TEXT)
977 + REJECT;
978 + set_state(S_END);
979 +}
980 +
981 +"<%"[ \n\t]*"@else"[ \n\t]*%> {
982 + if (state != S_TEXT)
983 + REJECT;
984 + set_state(S_ELSE);
985 +}
986 +
987 +"<%" {
988 + if (state != S_TEXT)
989 + parse_error("'<%' cannot be nested");
990 + set_state(S_CODE);
991 +}
992 +
993 +[ \n\t]"%>" {
994 + if (state == S_TEXT)
995 + REJECT;
996 + set_state(S_TEXT);
997 +}
998 +
999 +\n {
1000 + _lnr++;
1001 + if (state == S_TEXT)
1002 + addtext(yytext);
1003 +}
1004 +. {
1005 + addtext(yytext);
1006 +}
1007 +
1008 +
1009 +%%
1010 +
1011 +
1012 +void execute_template(struct template_cb *cb, struct template_element *e)
1013 +{
1014 + static int rec = 0;
1015 +
1016 + while (e) {
1017 +#ifdef DEBUG
1018 + fprintf(stderr, "DEBUG: execute(%d)\t%s\n", rec, typestr[e->t]);
1019 +#endif
1020 + rec++;
1021 + if (cb->handle_element)
1022 + cb->handle_element(cb, e);
1023 + rec--;
1024 + e = e->next;
1025 + }
1026 +}
1027 +
1028 +int yywrap()
1029 +{
1030 + set_state(S_EOF);
1031 + return 1;
1032 +}
1033 +
1034 +struct template_element *parse_template(struct template_cb *cb, FILE *in)
1035 +{
1036 + _lnr = 1;
1037 + buf_offset = 0;
1038 + state = S_TEXT;
1039 + parse_cb = cb;
1040 +
1041 + buflen = 4096;
1042 + textbuf = xzalloc(buflen);
1043 +
1044 + head = xzalloc(sizeof(struct template_element));
1045 + head->t = T_TEXT;
1046 + cur = head;
1047 +
1048 + yyin = in;
1049 + yylex();
1050 +
1051 + return head;
1052 +}
1053 +
1054 +void free_template(struct template_cb *cb, struct template_element *e)
1055 +{
1056 + struct template_element *next;
1057 + return;
1058 + if (!e)
1059 + return;
1060 +
1061 + switch (e->t) {
1062 + case T_CODE:
1063 + if (cb->free_code)
1064 + cb->free_code(e);
1065 + break;
1066 + case T_FOR:
1067 + case T_IF:
1068 + free_template(cb, e->sub);
1069 + break;
1070 + default:
1071 + break;
1072 + }
1073 + if (e->var)
1074 + free(e->var);
1075 + if (e->in)
1076 + free(e->in);
1077 +
1078 + next = e->next;
1079 + free(e);
1080 + return free_template(cb, next);
1081 +}
1082 diff -urN bb.old/editors/Config.in bb.dev/editors/Config.in
1083 --- bb.old/editors/Config.in 2007-05-20 04:17:05.003197632 +0200
1084 +++ bb.dev/editors/Config.in 2007-05-20 22:30:31.380512280 +0200
1085 @@ -12,6 +12,13 @@
1086 Awk is used as a pattern scanning and processing language. This is
1087 the BusyBox implementation of that programming language.
1088
1089 +config AWX
1090 + bool "Enable awx (awk web extension)"
1091 + default n
1092 + depends on AWK
1093 + help
1094 + awx - awk web extension
1095 +
1096 config FEATURE_AWK_MATH
1097 bool "Enable math functions (requires libm)"
1098 default y
1099 diff -urN bb.old/editors/Kbuild bb.dev/editors/Kbuild
1100 --- bb.old/editors/Kbuild 2007-03-18 17:59:37.000000000 +0100
1101 +++ bb.dev/editors/Kbuild 2007-05-20 22:30:31.381512128 +0200
1102 @@ -10,3 +10,12 @@
1103 lib-$(CONFIG_PATCH) += patch.o
1104 lib-$(CONFIG_SED) += sed.o
1105 lib-$(CONFIG_VI) += vi.o
1106 +lib-$(CONFIG_AWX) += awx_parser.o
1107 +
1108 +editors/awx_parser.c: editors/awx_parser.l editors/awx_parser.h
1109 + @flex $<
1110 + @mv lex.yy.c $@
1111 +
1112 +editors/awx_parser.o: editors/awx_parser.c FORCE
1113 + $(call cmd,force_checksrc)
1114 + $(call if_changed_rule,cc_o_c)
1115 diff -urN bb.old/include/applets.h bb.dev/include/applets.h
1116 --- bb.old/include/applets.h 2007-05-20 04:17:05.003197632 +0200
1117 +++ bb.dev/include/applets.h 2007-05-20 22:30:31.381512128 +0200
1118 @@ -60,6 +60,7 @@
1119 USE_ARPING(APPLET(arping, _BB_DIR_USR_BIN, _BB_SUID_NEVER))
1120 USE_ASH(APPLET_NOUSAGE(ash, ash, _BB_DIR_BIN, _BB_SUID_NEVER))
1121 USE_AWK(APPLET(awk, _BB_DIR_USR_BIN, _BB_SUID_NEVER))
1122 +USE_AWX(APPLET_NOUSAGE(awx, awx, _BB_DIR_USR_BIN, _BB_SUID_NEVER))
1123 USE_BASENAME(APPLET(basename, _BB_DIR_USR_BIN, _BB_SUID_NEVER))
1124 USE_BBCONFIG(APPLET(bbconfig, _BB_DIR_BIN, _BB_SUID_NEVER))
1125 //USE_BBSH(APPLET(bbsh, _BB_DIR_BIN, _BB_SUID_NEVER))
1126 diff -urN bb.old/include/cgi.h bb.dev/include/cgi.h
1127 --- bb.old/include/cgi.h 1970-01-01 01:00:00.000000000 +0100
1128 +++ bb.dev/include/cgi.h 2007-05-20 22:30:31.381512128 +0200
1129 @@ -0,0 +1,8 @@
1130 +#ifndef CGI_H
1131 +#define CGI_H
1132 +
1133 +typedef enum { FORM_VAR, COOKIE_VAR } vartype;
1134 +typedef void (*var_handler) (vartype, char *, char *);
1135 +int cgi_init(var_handler);
1136 +
1137 +#endif
1138 diff -urN bb.old/libbb/cgi.c bb.dev/libbb/cgi.c
1139 --- bb.old/libbb/cgi.c 1970-01-01 01:00:00.000000000 +0100
1140 +++ bb.dev/libbb/cgi.c 2007-05-20 22:30:31.382511976 +0200
1141 @@ -0,0 +1,457 @@
1142 +/* --------------------------------------------------------------------------
1143 + * functions for processing cgi form data
1144 + * Copyright (C) 2007 by Felix Fietkau <nbd@openwrt.org>
1145 + *
1146 + * parts taken from the core of haserl.cgi - a poor-man's php for embedded/lightweight environments
1147 + * $Id: haserl.c,v 1.13 2004/11/10 17:59:35 nangel Exp $
1148 + * Copyright (c) 2003,2004 Nathan Angelacos (nangel@users.sourceforge.net)
1149 + *
1150 + * This program is free software; you can redistribute it and/or modify
1151 + * it under the terms of the GNU General Public License as published by
1152 + * the Free Software Foundation; either version 2 of the License, or
1153 + * (at your option) any later version.
1154 + *
1155 + * This program is distributed in the hope that it will be useful,
1156 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1157 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1158 + * General Public License for more details.
1159 + *
1160 + * You should have received a copy of the GNU General Public License
1161 + * along with this program; if not, write to the Free Software
1162 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1163 + *
1164 + * -----
1165 + * The x2c() and unescape_url() routines were taken from
1166 + * http://www.jmarshall.com/easy/cgi/getcgi.c.txt
1167 + *
1168 + * The comments in that text file state:
1169 + *
1170 + *** Written in 1996 by James Marshall, james@jmarshall.com, except
1171 + *** that the x2c() and unescape_url() routines were lifted directly
1172 + *** from NCSA's sample program util.c, packaged with their HTTPD.
1173 + *** For the latest, see http://www.jmarshall.com/easy/cgi/
1174 + * -----
1175 + *
1176 + ------------------------------------------------------------------------- */
1177 +
1178 +#include <stdio.h>
1179 +#include <unistd.h>
1180 +#include <time.h>
1181 +#include <sys/mman.h>
1182 +#include <sys/types.h>
1183 +#include <sys/wait.h>
1184 +#include <sys/stat.h>
1185 +#include <sys/fcntl.h>
1186 +#include <stdlib.h>
1187 +#include <string.h>
1188 +#include <cgi.h>
1189 +
1190 +#ifndef MAX_UPLOAD_KB
1191 +#define MAX_UPLOAD_KB 2048
1192 +#endif
1193 +#define TEMPDIR "/tmp"
1194 +
1195 +static int global_upload_size = 0;
1196 +static int ReadMimeEncodedInput(char *qs);
1197 +static var_handler do_putvar = NULL;
1198 +
1199 +/*
1200 + * Convert 2 char hex string into char it represents
1201 + * (from http://www.jmarshall.com/easy/cgi)
1202 + */
1203 +static char x2c (char *what) {
1204 + char digit;
1205 +
1206 + digit=(what[0] >= 'A' ? ((what[0] & 0xdf) - 'A')+10 : (what[0] - '0'));
1207 + digit *=16;
1208 + digit+=(what[1] >= 'A' ? ((what[1] & 0xdf) - 'A')+10 : (what[1] - '0'));
1209 +
1210 + return digit;
1211 +}
1212 +
1213 +/*
1214 + * unsescape %xx to the characters they represent
1215 + */
1216 +
1217 +static void unescape_url (char *url) {
1218 + int i,j;
1219 +
1220 + for (i=0, j=0; url[j]; ++i, ++j) {
1221 + if ((url[i] = url[j]) == '%') {
1222 + url[i] = x2c(&url[j+1]);
1223 + j+=2;
1224 + }
1225 + }
1226 + url[i]='\0';
1227 +}
1228 +
1229 +static inline void put_var(vartype type, char *var)
1230 +{
1231 + char *val;
1232 +
1233 + if (!do_putvar)
1234 + return;
1235 +
1236 + val = strchr(var, '=');
1237 + if (!val)
1238 + return;
1239 +
1240 + *val = 0;
1241 + val++;
1242 + do_putvar(type, var, val);
1243 +
1244 + return;
1245 +}
1246 +
1247 +
1248 +/* CookieVars ()
1249 + * if HTTP_COOKIE is passed as an environment variable,
1250 + * attempt to parse its values into environment variables
1251 + */
1252 +static void CookieVars (void)
1253 +{
1254 + char *qs;
1255 + char *token;
1256 +
1257 + if (getenv("HTTP_COOKIE") != NULL)
1258 + qs=strdup(getenv("HTTP_COOKIE"));
1259 + else
1260 + return;
1261 +
1262 + /** split on; to extract name value pairs */
1263 + token=strtok(qs, ";");
1264 + while (token) {
1265 + // skip leading spaces
1266 + while ( token[0] == ' ' )
1267 + token++;
1268 +
1269 + put_var(COOKIE_VAR, token);
1270 +
1271 + token = strtok(NULL, ";");
1272 + }
1273 + free (qs);
1274 +}
1275 +
1276 +/*
1277 + * Read cgi variables from query string, and put in environment
1278 + */
1279 +static int ReadCGIQueryString (void)
1280 +{
1281 + char *qs;
1282 + char *token;
1283 + int i;
1284 +
1285 + if (getenv("QUERY_STRING") != NULL)
1286 + qs=strdup(getenv("QUERY_STRING"));
1287 + else
1288 + return 0;
1289 +
1290 + /* change plusses into spaces */
1291 + for (i=0; qs[i]; i++ ) { if (qs[i] == '+' ) { qs[i] = ' ';} };
1292 +
1293 + /** split on & and ; to extract name value pairs */
1294 +
1295 + token=strtok(qs, "&;");
1296 + while (token) {
1297 + unescape_url(token);
1298 + put_var(FORM_VAR, token);
1299 + token=strtok(NULL, "&;");
1300 + }
1301 + free(qs);
1302 +
1303 + return 0;
1304 +}
1305 +
1306 +
1307 +/*
1308 + * Read cgi variables from stdin (for POST queries)
1309 + * (oh... and if its mime-encoded file upload, we save the
1310 + * file to /tmp; and return the name of the tmp file
1311 + * the cgi script is responsible for disposing of the tmp file
1312 + */
1313 +
1314 +static int ReadCGIPOSTValues (void) {
1315 + char *qs;
1316 + int content_length;
1317 + int i;
1318 + char *token;
1319 +
1320 +
1321 + if (getenv("CONTENT_LENGTH") == NULL)
1322 + return(-1);
1323 + else
1324 + content_length = atoi(getenv("CONTENT_LENGTH"));
1325 +
1326 + /* protect ourselves from 20GB file uploads */
1327 + if (content_length > MAX_UPLOAD_KB * 1024 ) {
1328 + /* But we need to finish reading the content */
1329 + while ( fread( &i, sizeof(int), 1, stdin) == 1 );
1330 + return -1;
1331 + }
1332 +
1333 + if (!(qs=malloc(content_length+1)))
1334 + return -1;
1335 +
1336 + /* set the buffer to null, so that a browser messing with less
1337 + data than content_length won't buffer underrun us */
1338 + memset(qs, 0 ,content_length+1);
1339 +
1340 + if ((!fread(qs,content_length,1,stdin) &&
1341 + (content_length > 0)
1342 + && !feof(stdin))) {
1343 +
1344 + free(qs);
1345 + return -1;
1346 + }
1347 +
1348 + if (getenv("CONTENT_TYPE") && (strncasecmp(getenv("CONTENT_TYPE"), "multipart/form-data", 19) == 0)) {
1349 + /* This is a mime request, we need to go to the mime handler */
1350 + i=ReadMimeEncodedInput(qs);
1351 + free(qs);
1352 +
1353 + return i;
1354 + }
1355 +
1356 + /* change plusses into spaces */
1357 + for (i=0; qs[i]; i++ ) { if (qs[i] == '+' ) { qs[i] = ' ';} };
1358 +
1359 + /** split on & and ; to extract name value pairs */
1360 + token=strtok(qs, "&;");
1361 + while (token) {
1362 + unescape_url(token);
1363 + put_var(FORM_VAR, token);
1364 + token=strtok(NULL, "&;");
1365 + }
1366 +
1367 + free(qs);
1368 +
1369 + return 0;
1370 +}
1371 +
1372 +/*
1373 + * LineToStr - Scans char and replaces the first "\n" with a "\0";
1374 + * If it finds a "\r", it does that to; (fix DOS brokennes) returns
1375 + * the length of the string;
1376 + */
1377 +static int LineToStr (char *string, size_t max) {
1378 + size_t offset=0;
1379 +
1380 + while ((offset < max) && (string[offset] != '\n') && (string[offset] != '\r'))
1381 + offset++;
1382 +
1383 + if (string[offset] == '\r') {
1384 + string[offset]='\0';
1385 + offset++;
1386 + }
1387 + if (string[offset] == '\n') {
1388 + string[offset]='\0';
1389 + offset++;
1390 + }
1391 +
1392 + return offset;
1393 +}
1394 +
1395 +
1396 +/*
1397 + * ReadMimeEncodedInput - handles things that are mime encoded
1398 + * takes a pointer to the input; returns 0 on success
1399 + */
1400 +
1401 +static int ReadMimeEncodedInput(char *qs)
1402 +{
1403 + char *boundary;
1404 + char *ct;
1405 + int i;
1406 + int datastart;
1407 + size_t cl;
1408 + size_t offset;
1409 + char *envname;
1410 + char *filename;
1411 + char *ptr;
1412 + int line;
1413 + char tmpname[] = TEMPDIR "/XXXXXX";
1414 + int fd;
1415 + /* we should only get here if the content type was set. Segfaults happen
1416 + if Content_Type is null */
1417 +
1418 + if (getenv("CONTENT_LENGTH") == NULL)
1419 + /* No content length?! */
1420 + return(-1);
1421 +
1422 + cl=atoi(getenv("CONTENT_LENGTH"));
1423 +
1424 + /* we do this 'cause we can't mess with the real env. variable - it would
1425 + * overwrite the environment - I tried.
1426 + */
1427 + i=strlen(getenv("CONTENT_TYPE"))+1;
1428 + ct=malloc(i);
1429 + if (ct)
1430 + memcpy(ct, getenv("CONTENT_TYPE"), i);
1431 + else
1432 + return(-1);
1433 +
1434 + i=(int) NULL;
1435 + if (ct != NULL) {
1436 + while (i < strlen(ct) && (strncmp("boundary=", &ct[i], 9) != 0))
1437 + i++;
1438 + }
1439 + if (i == strlen(ct)) {
1440 + /* no boundary informaiton found */
1441 + free(ct);
1442 + return -1;
1443 + }
1444 + boundary=&ct[i+7];
1445 + /* add two leading -- to the boundary */
1446 + boundary[0]='-';
1447 + boundary[1]='-';
1448 +
1449 + /* begin the big loop. Look for:
1450 + --boundary
1451 + Content-Disposition: form-data; name="......."
1452 + ....
1453 + <blank line>
1454 + content
1455 + --boundary
1456 + Content-Disposition: form-data; name="....." filename="....."
1457 + ...
1458 + <blank line>
1459 + --boundary--
1460 + eof
1461 + */
1462 +
1463 + offset=0;
1464 + while (offset < cl) {
1465 + /* first look for boundary */
1466 + while ((offset < cl) && (memcmp(&qs[offset], boundary, strlen(boundary))))
1467 + offset++;
1468 +
1469 + /* if we got here and we ran off the end, its an error */
1470 + if (offset >= cl) {
1471 + free(ct);
1472 + return -1;
1473 + }
1474 +
1475 + /* if the two characters following the boundary are --, */
1476 + /* then we are at the end, exit */
1477 + if (memcmp(&qs[offset+strlen(boundary)], "--", 2) == 0) {
1478 + offset+=2;
1479 + break;
1480 + }
1481 + /* find where the offset should be */
1482 + line=LineToStr(&qs[offset], cl-offset);
1483 + offset+=line;
1484 +
1485 + /* Now we're going to look for content-disposition */
1486 + line=LineToStr(&qs[offset], cl-offset);
1487 + if (strncasecmp(&qs[offset], "Content-Disposition", 19) != 0) {
1488 + /* hmm... content disposition was not where we expected it */
1489 + free(ct);
1490 + return -1;
1491 + }
1492 + /* Found it, so let's go find "name=" */
1493 + if (!(envname=strstr(&qs[offset], "name="))) {
1494 + /* now name= is missing?! */
1495 + free(ct);
1496 + return(-1);
1497 + } else
1498 + envname+=6;
1499 +
1500 + /* is there a filename tag? */
1501 + if ((filename=strstr(&qs[offset], "filename="))!= NULL)
1502 + filename+=10;
1503 + else
1504 + filename=NULL;
1505 +
1506 + /* make envname and filename ASCIIZ */
1507 + for (i=0; (envname[i] != '"') && (envname[i] != '\0'); i++);
1508 +
1509 + envname[i] = '\0';
1510 + if (filename) {
1511 + for (i=0; (filename[i] != '"') && (filename[i] != '\0'); i++);
1512 + filename[i] = '\0';
1513 + }
1514 + offset+=line;
1515 +
1516 + /* Ok, by some miracle, we have the name; let's skip till we */
1517 + /* come to a blank line */
1518 + line=LineToStr(&qs[offset], cl-offset);
1519 + while (strlen(&qs[offset]) > 1) {
1520 + offset+=line;
1521 + line=LineToStr(&qs[offset], cl-offset);
1522 + }
1523 + offset+=line;
1524 + datastart=offset;
1525 + /* And we go back to looking for a boundary */
1526 + while ((offset < cl) && (memcmp(&qs[offset], boundary, strlen(boundary))))
1527 + offset++;
1528 +
1529 + /* strip [cr] lf */
1530 + if ((qs[offset-1] == '\n') && (qs[offset-2] == '\r'))
1531 + offset-=2;
1532 + else
1533 + offset-=1;
1534 +
1535 + qs[offset]=0;
1536 +
1537 + /* ok, at this point, we know where the name is, and we know */
1538 + /* where the content is... we have to do one of two things */
1539 + /* based on whether its a file or not */
1540 + if (filename==NULL) { /* its not a file, so its easy */
1541 + /* just jam the content after the name */
1542 + memcpy(&envname[strlen(envname)+1], &qs[datastart], offset-datastart+1);
1543 + envname[strlen(envname)]='=';
1544 + put_var(FORM_VAR, envname);
1545 + } else { /* handle the fileupload case */
1546 + if (offset-datastart) { /* only if they uploaded */
1547 + if ( global_upload_size == 0 ) {
1548 + return -1;
1549 + }
1550 + /* stuff in the filename */
1551 + ptr= calloc ( sizeof (char), strlen(envname)+strlen(filename)+2+5 );
1552 + sprintf (ptr, "%s_name=%s", envname, filename);
1553 + put_var(FORM_VAR, ptr);
1554 + free(ptr);
1555 +
1556 + fd=mkstemp(tmpname);
1557 +
1558 + if (fd == -1)
1559 + return(-1);
1560 +
1561 + write(fd, &qs[datastart], offset-datastart);
1562 + close(fd);
1563 + ptr= calloc (sizeof(char), strlen(envname)+strlen(tmpname)+2);
1564 + sprintf (ptr, "%s=%s", envname, tmpname);
1565 + put_var(FORM_VAR, ptr);
1566 + free(ptr);
1567 + }
1568 + }
1569 + }
1570 + free(ct);
1571 + return 0;
1572 +}
1573 +
1574 +
1575 +/*-------------------------------------------------------------------------
1576 + *
1577 + * Main
1578 + *
1579 + *------------------------------------------------------------------------*/
1580 +
1581 +int cgi_init(var_handler putvar_handler)
1582 +{
1583 + int retval = 0;
1584 +
1585 + do_putvar = putvar_handler;
1586 +
1587 + /* Read the current environment into our chain */
1588 + CookieVars();
1589 + if (getenv("REQUEST_METHOD")) {
1590 + if (strcasecmp(getenv("REQUEST_METHOD"), "GET") == 0)
1591 + retval = ReadCGIQueryString();
1592 +
1593 + if (strcasecmp(getenv("REQUEST_METHOD"), "POST") == 0)
1594 + retval = ReadCGIPOSTValues();
1595 + }
1596 +
1597 + return retval;
1598 +}
1599 diff -urN bb.old/libbb/Kbuild bb.dev/libbb/Kbuild
1600 --- bb.old/libbb/Kbuild 2007-05-20 04:17:05.004197480 +0200
1601 +++ bb.dev/libbb/Kbuild 2007-05-20 22:30:31.382511976 +0200
1602 @@ -118,3 +118,6 @@
1603 lib-$(CONFIG_MDEV) += xregcomp.o
1604 lib-$(CONFIG_LESS) += xregcomp.o
1605 lib-$(CONFIG_DEVFSD) += xregcomp.o
1606 +
1607 +lib-$(CONFIG_AWX) += cgi.o
1608 +