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