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