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