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