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