awx: variable initialization fix
[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-03-06 19:38:07.278092000 +0100
3 +++ bb.dev/editors/awk.c 2007-03-11 05:14:11.776304544 +0100
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-03-14 02:03:50.566202928 +0100
101 @@ -0,0 +1,590 @@
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 +
113 +#define LINE_BUF 2048
114 +#define HASH_MAX 1536
115 +#define TR_START "@TR<<"
116 +#define TR_END ">>"
117 +#define MAX_TR 32
118 +#define SSI_START "<%"
119 +#define SSI_END "%>"
120 +
121 +#undef fputs
122 +
123 +static xhash *lstr = NULL;
124 +static xhash *formvar = NULL;
125 +static int lang_inuse = 0;
126 +
127 +/* look up a translation symbol from the hash */
128 +static inline char *translate_lookup(char *str)
129 +{
130 + char *name, *def, *p;
131 + hash_item *hi;
132 + var *v;
133 +
134 + def = name = str;
135 + if (((p = strchr(str, '|')) != NULL)
136 + || ((p = strchr(str, '#')) != NULL)) {
137 + def = p + 1;
138 + *p = 0;
139 + }
140 +
141 + hi = hash_search(lstr, name);
142 + if (!hi)
143 + return def;
144 +
145 + v = &hi->data.v;
146 +
147 + return getvar_s(v);
148 +}
149 +
150 +/* look for translation markers in the line and return the translated string */
151 +static char *translate_line(char *line)
152 +{
153 + char *tok[MAX_TR * 3];
154 + char *l, *p, *p2, *res;
155 + int len = 0, _pos = 0, i;
156 +
157 + l = line;
158 + while (l != NULL) {
159 + if ((p = strstr(l, TR_START)) == NULL) {
160 + len += strlen((tok[_pos++] = l));
161 + break;
162 + }
163 +
164 + p2 = strstr(p, TR_END);
165 + if (p2 == NULL)
166 + break;
167 +
168 + *p = 0;
169 + *p2 = 0;
170 + len += strlen((tok[_pos++] = l));
171 + len += strlen((tok[_pos++] = translate_lookup(p + strlen(TR_START))));
172 +
173 + l = p2;
174 + l += strlen(TR_END);
175 + }
176 + len++;
177 +
178 + p = xmalloc(len + 1);
179 + *p = 0;
180 + res = p;
181 + for (i = 0; i < _pos; i++) {
182 + strcat(p, tok[i]);
183 + p += strlen(tok[i]);
184 + }
185 +
186 + return res;
187 +}
188 +
189 +/* hook for intercepting awk's use of puts. used for running all printed strings
190 + * through the translation system */
191 +static inline int fputs_hook (__const char *__restrict __s, FILE *__restrict __stream)
192 +{
193 + if (lang_inuse && (__stream == stdout)) {
194 + int ret;
195 + char *str;
196 +
197 + str = translate_line((char *) __s);
198 + ret = fputs(str, __stream);
199 + free(str);
200 +
201 + return ret;
202 + }
203 +
204 + return fputs(__s, __stream);
205 +}
206 +
207 +static var *init_lang(var *res, var *args, int nargs)
208 +{
209 + if (!lstr)
210 + lstr = hash_init();
211 +
212 + lang_inuse = 1;
213 + return res;
214 +}
215 +
216 +
217 +/* load and parse language file */
218 +static void load_lang_file(char *file)
219 +{
220 + FILE *f;
221 + char *b, *name, *value;
222 + char buf1[LINE_BUF];
223 +
224 + if ((f = fopen(file, "r")) == NULL)
225 + return;
226 +
227 + while (!feof(f) && (fgets(buf1, LINE_BUF - 1, f) != NULL)) {
228 + b = buf1;
229 + if (*b == '#')
230 + continue; /* skip comments */
231 +
232 + while (isspace(*b))
233 + b++; /* skip leading spaces */
234 + if (!*b)
235 + continue;
236 +
237 + name = b;
238 + if ((b = strstr(name, "=>")) == NULL)
239 + continue; /* separator not found */
240 +
241 + value = b + 2;
242 + if (!*value)
243 + continue;
244 +
245 + *b = 0;
246 + for (b--; isspace(*b); b--)
247 + *b = 0; /* remove trailing spaces */
248 +
249 + while (isspace(*value))
250 + value++; /* skip leading spaces */
251 +
252 + for (b = value + strlen(value) - 1; isspace(*b); b--)
253 + *b = 0; /* remove trailing spaces */
254 +
255 + if (!*value)
256 + continue;
257 +
258 + setvar_s(findvar(lstr,name), value);
259 + }
260 +
261 + fclose(f);
262 +}
263 +
264 +static var *load_lang(var *res, var *args, int nargs)
265 +{
266 + const char *langfmt = "/usr/lib/webif/lang/%s.txt";
267 + char lbuf[LINE_BUF];
268 + char *lang;
269 +
270 + if (!lang_inuse)
271 + init_lang(res, args, nargs);
272 +
273 + lang = getvar_s(args);
274 + if (!lang || !strcmp(lang, ""))
275 + return res;
276 +
277 + sprintf(lbuf, langfmt, lang);
278 + load_lang_file(lbuf);
279 +
280 + return res;
281 +}
282 +
283 +/* read the contents of an entire file */
284 +static char *get_file(char *fname)
285 +{
286 + FILE *F;
287 + char *s = NULL;
288 + int i, j, flen;
289 +
290 + F = fopen(fname, "r");
291 + if (!F) {
292 + return NULL;
293 + }
294 +
295 + if (fseek(F, 0, SEEK_END) == 0) {
296 + flen = ftell(F);
297 + s = (char *)xmalloc(flen+4);
298 + fseek(F, 0, SEEK_SET);
299 + i = 1 + fread(s+1, 1, flen, F);
300 + } else {
301 + for (i=j=1; j>0; i+=j) {
302 + s = (char *)xrealloc(s, i+4096);
303 + j = fread(s+i, 1, 4094, F);
304 + }
305 + }
306 +
307 + s[i] = '\0';
308 + fclose(F);
309 + return s;
310 +}
311 +
312 +
313 +/* parse_include():
314 + *
315 + * taken from parse_program from awk.c
316 + * END{} is not parsed here, and BEGIN{} is executed immediately
317 + */
318 +static void parse_include(char *p)
319 +{
320 + uint32_t tclass;
321 + chain *initseq = NULL;
322 + chain tmp;
323 + func *f;
324 + var *v, tv;
325 +
326 + zero_out_var(&tv);
327 + memset(&tmp, 0, sizeof(tmp));
328 + pos = p;
329 + t.lineno = 1;
330 + while ((tclass = next_token(TC_EOF | TC_OPSEQ |
331 + TC_OPTERM | TC_BEGIN | TC_FUNCDECL)) != TC_EOF) {
332 + if (tclass & TC_OPTERM)
333 + continue;
334 +
335 + seq = &tmp;
336 + if (tclass & TC_BEGIN) {
337 + initseq = xzalloc(sizeof(chain));
338 + seq = initseq;
339 + chain_group();
340 + } else if (tclass & TC_FUNCDECL) {
341 + next_token(TC_FUNCTION);
342 + pos++;
343 + f = newfunc(t.string);
344 + f->type = AWKFUNC;
345 + f->x.body.first = NULL;
346 + f->nargs = 0;
347 + while (next_token(TC_VARIABLE | TC_SEQTERM) & TC_VARIABLE) {
348 + v = findvar(ahash, t.string);
349 + v->x.aidx = (f->nargs)++;
350 +
351 + if (next_token(TC_COMMA | TC_SEQTERM) & TC_SEQTERM)
352 + break;
353 + }
354 + seq = &(f->x.body);
355 + chain_group();
356 + clear_array(ahash);
357 + }
358 + }
359 + if (initseq && initseq->first)
360 + evaluate(initseq->first, &tv);
361 +}
362 +
363 +
364 +/* include an awk file and run its BEGIN{} section */
365 +static xhash *includes = NULL;
366 +static void include_file(char *filename)
367 +{
368 + char *s;
369 + var *v;
370 +
371 + if (!includes)
372 + includes = hash_init();
373 +
374 + /* find out if the file has been included already */
375 + v = findvar(includes, filename);
376 + if (istrue(v))
377 + return;
378 + setvar_s(v, "1");
379 +
380 + /* read include file */
381 + s = get_file(filename);
382 + if (!s) {
383 + fprintf(stderr, "Could not open file.\n");
384 + return;
385 + }
386 + parse_include(s+1);
387 + free(s);
388 +}
389 +
390 +static var *include(var *res, var *args, int nargs)
391 +{
392 + char *s;
393 +
394 + s = getvar_s(args);
395 + if (s && (strlen(s) > 0))
396 + include_file(s);
397 +
398 + return res;
399 +}
400 +
401 +
402 +/* parse and evaluate an awk expression and return the result as string */
403 +static char *render_lookup(char *fname, int lnr, char *str)
404 +{
405 + chain body;
406 + var tv;
407 +
408 + memset(&body, 0, sizeof(body));
409 + zero_out_var(&tv);
410 + pos = str;
411 + seq = &body;
412 +
413 + /* end of expression, assume that there's going to be a free byte
414 + * at the end of the string that can be used for the ')' */
415 + strcat(str + strlen(str), ")");
416 + return getvar_s(evaluate(parse_expr(TC_SEQTERM), &tv));
417 +}
418 +
419 +static inline void print_translate(char *s)
420 +{
421 + char *str = s;
422 + if (lang_inuse)
423 + str = translate_line(s);
424 + fputs(str, stdout);
425 + fflush(stdout);
426 + if (lang_inuse)
427 + free(str);
428 +}
429 +
430 +/* process awk calls in a template line and print the output to stdout */
431 +static void render_line(char *fname, int lnr, char *line)
432 +{
433 + char *tok[MAX_TR * 3];
434 + char *l, *p, *p2, *res;
435 + int len = 0, _pos = 0, i;
436 +
437 + l = line;
438 + while (l != NULL) {
439 + if ((p = strstr(l, SSI_START)) == NULL) {
440 + len += strlen((tok[_pos++] = l));
441 + break;
442 + }
443 +
444 + p2 = strstr(p, SSI_END);
445 + if (p2 == NULL) {
446 + fprintf(stderr, "Parse error in '%s', line '%d', unmatched %s\n", fname, lnr, SSI_END);
447 + break;
448 + }
449 +
450 + *p = 0;
451 + *p2 = 0;
452 +
453 + len += strlen((tok[_pos++] = l));
454 + len += strlen((tok[_pos++] = render_lookup(fname, lnr, p + strlen(SSI_START))));
455 +
456 + l = p2;
457 + l += strlen(SSI_END);
458 + }
459 + len++;
460 +
461 + p = xmalloc(len + 1);
462 + *p = 0;
463 + res = p;
464 + for (i = 0; i < _pos; i++) {
465 + strcat(p, tok[i]);
466 + p += strlen(tok[i]);
467 + }
468 + print_translate(res);
469 + free(res);
470 +}
471 +
472 +/* awk method render(), which opens a template file and processes all awk ssi calls */
473 +static void render_file(char *filename)
474 +{
475 + int lnr = 0;
476 + FILE *f;
477 + char *buf1;
478 +
479 + f = fopen(filename, "r");
480 + if (!f)
481 + return;
482 +
483 + buf1 = xmalloc(LINE_BUF);
484 + while (!feof(f) && (fgets(buf1, LINE_BUF - 1, f) != NULL)) {
485 + render_line(filename, ++lnr, buf1);
486 + }
487 +}
488 +
489 +static var *render(var *res, var *args, int nargs)
490 +{
491 + char *s;
492 +
493 + s = getvar_s(args);
494 + if (!s)
495 + return res;
496 +
497 + render_file(s);
498 +
499 + return res;
500 +}
501 +
502 +/* Call render, but only if this function hasn't been called already */
503 +static int layout_rendered = 0;
504 +static var *render_layout(var *res, var *args, int nargs)
505 +{
506 + if (layout_rendered)
507 + return res;
508 + layout_rendered = 1;
509 + return render(res, args, nargs);
510 +}
511 +
512 +/* registers a global c function for the awk interpreter */
513 +static void register_cfunc(char *name, awk_cfunc cfunc, int nargs)
514 +{
515 + func *f;
516 +
517 + f = newfunc(name);
518 + f->type = CFUNC;
519 + f->x.cfunc = cfunc;
520 + f->nargs = nargs;
521 +}
522 +
523 +static void putvar(vartype type, char *name, char *value)
524 +{
525 + if (type != FORM_VAR)
526 + return;
527 +
528 + setvar_u(findvar(formvar, name), value);
529 +}
530 +
531 +static char *cgi_getvar(char *name)
532 +{
533 + if (!formvar) {
534 + formvar = hash_init();
535 + cgi_init(putvar);
536 + }
537 +
538 + if (!formvar || !name)
539 + return NULL;
540 +
541 + return getvar_s(findvar(formvar, name));
542 +}
543 +
544 +/* function call for accessing cgi form variables */
545 +static var *getvar(var *res, var *args, int nargs)
546 +{
547 + char *s;
548 + char *svar;
549 +
550 + s = getvar_s(args);
551 + if (!s)
552 + return res;
553 +
554 + svar = cgi_getvar(s);
555 + if (!svar)
556 + return res;
557 +
558 + setvar_u(res, svar);
559 +
560 + return res;
561 +}
562 +
563 +/* call an awk function without arguments by string reference */
564 +static var *call(var *res, var *args, int nargs)
565 +{
566 + char *s = getvar_s(args);
567 + func *f;
568 +
569 + if (!s)
570 + goto done;
571 +
572 + f = newfunc(s);
573 + if (f && f->type == AWKFUNC && f->x.body.first)
574 + return evaluate(f->x.body.first, res);
575 +
576 +done:
577 + return res;
578 +}
579 +
580 +
581 +static int run_awxscript(char *name)
582 +{
583 + var tv, *layout, *action;
584 + char *tmp, *s = NULL;
585 +
586 + zero_out_var(&tv);
587 + programname = name;
588 +
589 + /* read the main controller source */
590 + s = get_file(programname);
591 + if (!s) {
592 + fprintf(stderr, "Could not open file\n");
593 + return 1;
594 + }
595 + parse_program(s+1);
596 + free(s);
597 +
598 +
599 + /* set some defaults for ACTION and LAYOUT, which will have special meaning */
600 + layout = newvar("LAYOUT");
601 + setvar_s(layout, "views/layout.ahtml");
602 +
603 + /* run the BEGIN {} block */
604 + evaluate(beginseq.first, &tv);
605 +
606 + action = newvar("ACTION");
607 + if (!(strlen(getvar_s(action)) > 0)) {
608 + tmp = cgi_getvar("action");
609 + if (!tmp || (strlen(tmp) <= 0))
610 + tmp = strdup("default");
611 +
612 + setvar_p(action, tmp);
613 + }
614 +
615 + /* call the action (precedence: begin block override > cgi parameter > "default") */
616 + tmp = xmalloc(strlen(getvar_s(action)) + 7);
617 + sprintf(tmp, "handle_%s", getvar_s(action));
618 + setvar_s(action, tmp);
619 + call(&tv, action, 1);
620 + free(tmp);
621 +
622 + /* render the selected layout, will do nothing if render_layout has been called from awk */
623 + render_layout(&tv, layout, 1);
624 +
625 + return 0;
626 +}
627 +
628 +
629 +/* main awx processing function. called from awk_main() */
630 +static int do_awx(int argc, char **argv)
631 +{
632 + int ret = -1;
633 + var tv;
634 + int i, c;
635 + char **args = argv;
636 +
637 + zero_out_var(&tv);
638 +
639 + /* register awk C callbacks */
640 + register_cfunc("getvar", getvar, 1);
641 + register_cfunc("render", render, 1);
642 + register_cfunc("render_layout", render_layout, 1);
643 + register_cfunc("call", call, 1);
644 + register_cfunc("include", include, 1);
645 + register_cfunc("init_lang", init_lang, 1);
646 + register_cfunc("load_lang", load_lang, 1);
647 +
648 + if (!is_awx)
649 + return 0;
650 +
651 + /* fill in ARGV array */
652 + setvar_i(V[ARGC], argc + 1);
653 + setari_u(V[ARGV], 0, "awx");
654 + i = 0;
655 + while (*args)
656 + setari_u(V[ARGV], ++i, *args++);
657 +
658 + while((c = getopt(argc, argv, "i:f:")) != EOF) {
659 + switch(c) {
660 + case 'i':
661 + programname = optarg;
662 + include_file(optarg);
663 + break;
664 + case 'f':
665 + ret = 0;
666 + programname = optarg;
667 + render_file(optarg);
668 + goto done;
669 + }
670 + }
671 + argc -= optind;
672 + argv += optind;
673 +
674 + if (argc < 1) {
675 + fprintf(stderr, "Invalid argument.\n");
676 + goto done;
677 + }
678 +
679 + ret = run_awxscript(*argv);
680 +
681 +done:
682 + exit(ret);
683 +}
684 +
685 +/* entry point for awx applet */
686 +int awx_main(int argc, char **argv)
687 +{
688 + is_awx = 1;
689 + return awk_main(argc, argv);
690 +}
691 +
692 diff -purN bb.old/editors/Config.in bb.dev/editors/Config.in
693 --- bb.old/editors/Config.in 2007-01-24 22:34:50.000000000 +0100
694 +++ bb.dev/editors/Config.in 2007-03-11 06:19:51.469380160 +0100
695 @@ -12,6 +12,13 @@ config AWK
696 Awk is used as a pattern scanning and processing language. This is
697 the BusyBox implementation of that programming language.
698
699 +config AWX
700 + bool "Enable awx (awk web extension)"
701 + default n
702 + depends on AWK
703 + help
704 + awx - awk web extension
705 +
706 config FEATURE_AWK_MATH
707 bool "Enable math functions (requires libm)"
708 default y
709 diff -purN bb.old/include/applets.h bb.dev/include/applets.h
710 --- bb.old/include/applets.h 2007-03-06 19:38:07.355081000 +0100
711 +++ bb.dev/include/applets.h 2007-03-07 02:12:24.280681880 +0100
712 @@ -60,6 +60,7 @@ USE_ARP(APPLET(arp, _BB_DIR_SBIN, _BB_SU
713 USE_ARPING(APPLET(arping, _BB_DIR_USR_BIN, _BB_SUID_NEVER))
714 USE_ASH(APPLET_NOUSAGE(ash, ash, _BB_DIR_BIN, _BB_SUID_NEVER))
715 USE_AWK(APPLET(awk, _BB_DIR_USR_BIN, _BB_SUID_NEVER))
716 +USE_AWX(APPLET_NOUSAGE(awx, awx, _BB_DIR_USR_BIN, _BB_SUID_NEVER))
717 USE_BASENAME(APPLET(basename, _BB_DIR_USR_BIN, _BB_SUID_NEVER))
718 USE_BBCONFIG(APPLET(bbconfig, _BB_DIR_BIN, _BB_SUID_NEVER))
719 //USE_BBSH(APPLET(bbsh, _BB_DIR_BIN, _BB_SUID_NEVER))
720 diff -purN bb.old/include/cgi.h bb.dev/include/cgi.h
721 --- bb.old/include/cgi.h 1970-01-01 01:00:00.000000000 +0100
722 +++ bb.dev/include/cgi.h 2007-03-11 18:58:10.708444448 +0100
723 @@ -0,0 +1,8 @@
724 +#ifndef CGI_H
725 +#define CGI_H
726 +
727 +typedef enum { FORM_VAR, COOKIE_VAR } vartype;
728 +typedef void (*var_handler) (vartype, char *, char *);
729 +int cgi_init(var_handler);
730 +
731 +#endif
732 diff -purN bb.old/libbb/cgi.c bb.dev/libbb/cgi.c
733 --- bb.old/libbb/cgi.c 1970-01-01 01:00:00.000000000 +0100
734 +++ bb.dev/libbb/cgi.c 2007-03-11 19:02:04.691873560 +0100
735 @@ -0,0 +1,457 @@
736 +/* --------------------------------------------------------------------------
737 + * functions for processing cgi form data
738 + * Copyright (C) 2007 by Felix Fietkau <nbd@openwrt.org>
739 + *
740 + * parts taken from the core of haserl.cgi - a poor-man's php for embedded/lightweight environments
741 + * $Id: haserl.c,v 1.13 2004/11/10 17:59:35 nangel Exp $
742 + * Copyright (c) 2003,2004 Nathan Angelacos (nangel@users.sourceforge.net)
743 + *
744 + * This program is free software; you can redistribute it and/or modify
745 + * it under the terms of the GNU General Public License as published by
746 + * the Free Software Foundation; either version 2 of the License, or
747 + * (at your option) any later version.
748 + *
749 + * This program is distributed in the hope that it will be useful,
750 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
751 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
752 + * General Public License for more details.
753 + *
754 + * You should have received a copy of the GNU General Public License
755 + * along with this program; if not, write to the Free Software
756 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
757 + *
758 + * -----
759 + * The x2c() and unescape_url() routines were taken from
760 + * http://www.jmarshall.com/easy/cgi/getcgi.c.txt
761 + *
762 + * The comments in that text file state:
763 + *
764 + *** Written in 1996 by James Marshall, james@jmarshall.com, except
765 + *** that the x2c() and unescape_url() routines were lifted directly
766 + *** from NCSA's sample program util.c, packaged with their HTTPD.
767 + *** For the latest, see http://www.jmarshall.com/easy/cgi/
768 + * -----
769 + *
770 + ------------------------------------------------------------------------- */
771 +
772 +#include <stdio.h>
773 +#include <unistd.h>
774 +#include <time.h>
775 +#include <sys/mman.h>
776 +#include <sys/types.h>
777 +#include <sys/wait.h>
778 +#include <sys/stat.h>
779 +#include <sys/fcntl.h>
780 +#include <stdlib.h>
781 +#include <string.h>
782 +#include <cgi.h>
783 +
784 +#ifndef MAX_UPLOAD_KB
785 +#define MAX_UPLOAD_KB 2048
786 +#endif
787 +#define TEMPDIR "/tmp"
788 +
789 +static int global_upload_size = 0;
790 +static int ReadMimeEncodedInput(char *qs);
791 +static var_handler do_putvar = NULL;
792 +
793 +/*
794 + * Convert 2 char hex string into char it represents
795 + * (from http://www.jmarshall.com/easy/cgi)
796 + */
797 +static char x2c (char *what) {
798 + char digit;
799 +
800 + digit=(what[0] >= 'A' ? ((what[0] & 0xdf) - 'A')+10 : (what[0] - '0'));
801 + digit *=16;
802 + digit+=(what[1] >= 'A' ? ((what[1] & 0xdf) - 'A')+10 : (what[1] - '0'));
803 +
804 + return digit;
805 +}
806 +
807 +/*
808 + * unsescape %xx to the characters they represent
809 + */
810 +
811 +static void unescape_url (char *url) {
812 + int i,j;
813 +
814 + for (i=0, j=0; url[j]; ++i, ++j) {
815 + if ((url[i] = url[j]) == '%') {
816 + url[i] = x2c(&url[j+1]);
817 + j+=2;
818 + }
819 + }
820 + url[i]='\0';
821 +}
822 +
823 +static inline void put_var(vartype type, char *var)
824 +{
825 + char *val;
826 +
827 + if (!do_putvar)
828 + return;
829 +
830 + val = strchr(var, '=');
831 + if (!val)
832 + return;
833 +
834 + *val = 0;
835 + val++;
836 + do_putvar(type, var, val);
837 +
838 + return;
839 +}
840 +
841 +
842 +/* CookieVars ()
843 + * if HTTP_COOKIE is passed as an environment variable,
844 + * attempt to parse its values into environment variables
845 + */
846 +static void CookieVars (void)
847 +{
848 + char *qs;
849 + char *token;
850 +
851 + if (getenv("HTTP_COOKIE") != NULL)
852 + qs=strdup(getenv("HTTP_COOKIE"));
853 + else
854 + return;
855 +
856 + /** split on; to extract name value pairs */
857 + token=strtok(qs, ";");
858 + while (token) {
859 + // skip leading spaces
860 + while ( token[0] == ' ' )
861 + token++;
862 +
863 + put_var(COOKIE_VAR, token);
864 +
865 + token = strtok(NULL, ";");
866 + }
867 + free (qs);
868 +}
869 +
870 +/*
871 + * Read cgi variables from query string, and put in environment
872 + */
873 +static int ReadCGIQueryString (void)
874 +{
875 + char *qs;
876 + char *token;
877 + int i;
878 +
879 + if (getenv("QUERY_STRING") != NULL)
880 + qs=strdup(getenv("QUERY_STRING"));
881 + else
882 + return 0;
883 +
884 + /* change plusses into spaces */
885 + for (i=0; qs[i]; i++ ) { if (qs[i] == '+' ) { qs[i] = ' ';} };
886 +
887 + /** split on & and ; to extract name value pairs */
888 +
889 + token=strtok(qs, "&;");
890 + while (token) {
891 + unescape_url(token);
892 + put_var(FORM_VAR, token);
893 + token=strtok(NULL, "&;");
894 + }
895 + free(qs);
896 +
897 + return 0;
898 +}
899 +
900 +
901 +/*
902 + * Read cgi variables from stdin (for POST queries)
903 + * (oh... and if its mime-encoded file upload, we save the
904 + * file to /tmp; and return the name of the tmp file
905 + * the cgi script is responsible for disposing of the tmp file
906 + */
907 +
908 +static int ReadCGIPOSTValues (void) {
909 + char *qs;
910 + int content_length;
911 + int i;
912 + char *token;
913 +
914 +
915 + if (getenv("CONTENT_LENGTH") == NULL)
916 + return(-1);
917 + else
918 + content_length = atoi(getenv("CONTENT_LENGTH"));
919 +
920 + /* protect ourselves from 20GB file uploads */
921 + if (content_length > MAX_UPLOAD_KB * 1024 ) {
922 + /* But we need to finish reading the content */
923 + while ( fread( &i, sizeof(int), 1, stdin) == 1 );
924 + return -1;
925 + }
926 +
927 + if (!(qs=malloc(content_length+1)))
928 + return -1;
929 +
930 + /* set the buffer to null, so that a browser messing with less
931 + data than content_length won't buffer underrun us */
932 + memset(qs, 0 ,content_length+1);
933 +
934 + if ((!fread(qs,content_length,1,stdin) &&
935 + (content_length > 0)
936 + && !feof(stdin))) {
937 +
938 + free(qs);
939 + return -1;
940 + }
941 +
942 + if (getenv("CONTENT_TYPE") && (strncasecmp(getenv("CONTENT_TYPE"), "multipart/form-data", 19) == 0)) {
943 + /* This is a mime request, we need to go to the mime handler */
944 + i=ReadMimeEncodedInput(qs);
945 + free(qs);
946 +
947 + return i;
948 + }
949 +
950 + /* change plusses into spaces */
951 + for (i=0; qs[i]; i++ ) { if (qs[i] == '+' ) { qs[i] = ' ';} };
952 +
953 + /** split on & and ; to extract name value pairs */
954 + token=strtok(qs, "&;");
955 + while (token) {
956 + unescape_url(token);
957 + put_var(FORM_VAR, token);
958 + token=strtok(NULL, "&;");
959 + }
960 +
961 + free(qs);
962 +
963 + return 0;
964 +}
965 +
966 +/*
967 + * LineToStr - Scans char and replaces the first "\n" with a "\0";
968 + * If it finds a "\r", it does that to; (fix DOS brokennes) returns
969 + * the length of the string;
970 + */
971 +static int LineToStr (char *string, size_t max) {
972 + size_t offset=0;
973 +
974 + while ((offset < max) && (string[offset] != '\n') && (string[offset] != '\r'))
975 + offset++;
976 +
977 + if (string[offset] == '\r') {
978 + string[offset]='\0';
979 + offset++;
980 + }
981 + if (string[offset] == '\n') {
982 + string[offset]='\0';
983 + offset++;
984 + }
985 +
986 + return offset;
987 +}
988 +
989 +
990 +/*
991 + * ReadMimeEncodedInput - handles things that are mime encoded
992 + * takes a pointer to the input; returns 0 on success
993 + */
994 +
995 +static int ReadMimeEncodedInput(char *qs)
996 +{
997 + char *boundary;
998 + char *ct;
999 + int i;
1000 + int datastart;
1001 + size_t cl;
1002 + size_t offset;
1003 + char *envname;
1004 + char *filename;
1005 + char *ptr;
1006 + int line;
1007 + char tmpname[] = TEMPDIR "/XXXXXX";
1008 + int fd;
1009 + /* we should only get here if the content type was set. Segfaults happen
1010 + if Content_Type is null */
1011 +
1012 + if (getenv("CONTENT_LENGTH") == NULL)
1013 + /* No content length?! */
1014 + return(-1);
1015 +
1016 + cl=atoi(getenv("CONTENT_LENGTH"));
1017 +
1018 + /* we do this 'cause we can't mess with the real env. variable - it would
1019 + * overwrite the environment - I tried.
1020 + */
1021 + i=strlen(getenv("CONTENT_TYPE"))+1;
1022 + ct=malloc(i);
1023 + if (ct)
1024 + memcpy(ct, getenv("CONTENT_TYPE"), i);
1025 + else
1026 + return(-1);
1027 +
1028 + i=(int) NULL;
1029 + if (ct != NULL) {
1030 + while (i < strlen(ct) && (strncmp("boundary=", &ct[i], 9) != 0))
1031 + i++;
1032 + }
1033 + if (i == strlen(ct)) {
1034 + /* no boundary informaiton found */
1035 + free(ct);
1036 + return -1;
1037 + }
1038 + boundary=&ct[i+7];
1039 + /* add two leading -- to the boundary */
1040 + boundary[0]='-';
1041 + boundary[1]='-';
1042 +
1043 + /* begin the big loop. Look for:
1044 + --boundary
1045 + Content-Disposition: form-data; name="......."
1046 + ....
1047 + <blank line>
1048 + content
1049 + --boundary
1050 + Content-Disposition: form-data; name="....." filename="....."
1051 + ...
1052 + <blank line>
1053 + --boundary--
1054 + eof
1055 + */
1056 +
1057 + offset=0;
1058 + while (offset < cl) {
1059 + /* first look for boundary */
1060 + while ((offset < cl) && (memcmp(&qs[offset], boundary, strlen(boundary))))
1061 + offset++;
1062 +
1063 + /* if we got here and we ran off the end, its an error */
1064 + if (offset >= cl) {
1065 + free(ct);
1066 + return -1;
1067 + }
1068 +
1069 + /* if the two characters following the boundary are --, */
1070 + /* then we are at the end, exit */
1071 + if (memcmp(&qs[offset+strlen(boundary)], "--", 2) == 0) {
1072 + offset+=2;
1073 + break;
1074 + }
1075 + /* find where the offset should be */
1076 + line=LineToStr(&qs[offset], cl-offset);
1077 + offset+=line;
1078 +
1079 + /* Now we're going to look for content-disposition */
1080 + line=LineToStr(&qs[offset], cl-offset);
1081 + if (strncasecmp(&qs[offset], "Content-Disposition", 19) != 0) {
1082 + /* hmm... content disposition was not where we expected it */
1083 + free(ct);
1084 + return -1;
1085 + }
1086 + /* Found it, so let's go find "name=" */
1087 + if (!(envname=strstr(&qs[offset], "name="))) {
1088 + /* now name= is missing?! */
1089 + free(ct);
1090 + return(-1);
1091 + } else
1092 + envname+=6;
1093 +
1094 + /* is there a filename tag? */
1095 + if ((filename=strstr(&qs[offset], "filename="))!= NULL)
1096 + filename+=10;
1097 + else
1098 + filename=NULL;
1099 +
1100 + /* make envname and filename ASCIIZ */
1101 + for (i=0; (envname[i] != '"') && (envname[i] != '\0'); i++);
1102 +
1103 + envname[i] = '\0';
1104 + if (filename) {
1105 + for (i=0; (filename[i] != '"') && (filename[i] != '\0'); i++);
1106 + filename[i] = '\0';
1107 + }
1108 + offset+=line;
1109 +
1110 + /* Ok, by some miracle, we have the name; let's skip till we */
1111 + /* come to a blank line */
1112 + line=LineToStr(&qs[offset], cl-offset);
1113 + while (strlen(&qs[offset]) > 1) {
1114 + offset+=line;
1115 + line=LineToStr(&qs[offset], cl-offset);
1116 + }
1117 + offset+=line;
1118 + datastart=offset;
1119 + /* And we go back to looking for a boundary */
1120 + while ((offset < cl) && (memcmp(&qs[offset], boundary, strlen(boundary))))
1121 + offset++;
1122 +
1123 + /* strip [cr] lf */
1124 + if ((qs[offset-1] == '\n') && (qs[offset-2] == '\r'))
1125 + offset-=2;
1126 + else
1127 + offset-=1;
1128 +
1129 + qs[offset]=0;
1130 +
1131 + /* ok, at this point, we know where the name is, and we know */
1132 + /* where the content is... we have to do one of two things */
1133 + /* based on whether its a file or not */
1134 + if (filename==NULL) { /* its not a file, so its easy */
1135 + /* just jam the content after the name */
1136 + memcpy(&envname[strlen(envname)+1], &qs[datastart], offset-datastart+1);
1137 + envname[strlen(envname)]='=';
1138 + put_var(FORM_VAR, envname);
1139 + } else { /* handle the fileupload case */
1140 + if (offset-datastart) { /* only if they uploaded */
1141 + if ( global_upload_size == 0 ) {
1142 + return -1;
1143 + }
1144 + /* stuff in the filename */
1145 + ptr= calloc ( sizeof (char), strlen(envname)+strlen(filename)+2+5 );
1146 + sprintf (ptr, "%s_name=%s", envname, filename);
1147 + put_var(FORM_VAR, ptr);
1148 + free(ptr);
1149 +
1150 + fd=mkstemp(tmpname);
1151 +
1152 + if (fd == -1)
1153 + return(-1);
1154 +
1155 + write(fd, &qs[datastart], offset-datastart);
1156 + close(fd);
1157 + ptr= calloc (sizeof(char), strlen(envname)+strlen(tmpname)+2);
1158 + sprintf (ptr, "%s=%s", envname, tmpname);
1159 + put_var(FORM_VAR, ptr);
1160 + free(ptr);
1161 + }
1162 + }
1163 + }
1164 + free(ct);
1165 + return 0;
1166 +}
1167 +
1168 +
1169 +/*-------------------------------------------------------------------------
1170 + *
1171 + * Main
1172 + *
1173 + *------------------------------------------------------------------------*/
1174 +
1175 +int cgi_init(var_handler putvar_handler)
1176 +{
1177 + int retval = 0;
1178 +
1179 + do_putvar = putvar_handler;
1180 +
1181 + /* Read the current environment into our chain */
1182 + CookieVars();
1183 + if (getenv("REQUEST_METHOD")) {
1184 + if (strcasecmp(getenv("REQUEST_METHOD"), "GET") == 0)
1185 + retval = ReadCGIQueryString();
1186 +
1187 + if (strcasecmp(getenv("REQUEST_METHOD"), "POST") == 0)
1188 + retval = ReadCGIPOSTValues();
1189 + }
1190 +
1191 + return retval;
1192 +}
1193 diff -purN bb.old/libbb/Kbuild bb.dev/libbb/Kbuild
1194 --- bb.old/libbb/Kbuild 2007-03-06 19:38:07.361080000 +0100
1195 +++ bb.dev/libbb/Kbuild 2007-03-11 18:40:51.384445712 +0100
1196 @@ -118,3 +118,6 @@ lib-$(CONFIG_GREP) += xregcomp.o
1197 lib-$(CONFIG_MDEV) += xregcomp.o
1198 lib-$(CONFIG_LESS) += xregcomp.o
1199 lib-$(CONFIG_DEVFSD) += xregcomp.o
1200 +
1201 +lib-$(CONFIG_AWX) += cgi.o
1202 +