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