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