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