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