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