1 diff -purN bb.old/editors/awk.c bb.dev/editors/awk.c
2 --- bb.old/editors/awk.c 2007-05-20 04:17:05.002197784 +0200
3 +++ bb.dev/editors/awk.c 2007-05-20 22:40:48.183743936 +0200
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)
9 +#define fputs(s, stream) fputs_hook(s, stream)
10 +static inline int fputs_hook (__const char *__restrict __s, FILE *__restrict __stream);
14 typedef struct var_s {
15 unsigned short type; /* flags */
16 @@ -50,10 +55,15 @@ typedef struct chain_s {
20 +typedef var *(*awk_cfunc)(var *res, var *args, int nargs);
22 typedef struct func_s {
24 - struct chain_s body;
25 + enum { AWKFUNC, CFUNC } type;
28 + struct chain_s body;
33 @@ -1312,7 +1322,8 @@ static void parse_program(char *p)
34 next_token(TC_FUNCTION);
36 f = newfunc(t.string);
37 - f->body.first = NULL;
39 + f->x.body.first = NULL;
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)
52 @@ -2260,7 +2271,8 @@ static var *evaluate(node *op, var *res)
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);
61 X.v = R.v = nvalloc(op->r.f->nargs+1);
62 @@ -2277,7 +2289,11 @@ static var *evaluate(node *op, var *res)
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);
75 @@ -2637,6 +2653,11 @@ static rstream *next_input_file(void)
80 +static int is_awx = 0;
84 int awk_main(int argc, char **argv)
87 @@ -2693,6 +2714,10 @@ int awk_main(int argc, char **argv)
96 while((c = getopt(argc, argv, "F:v:f:W:")) != EOF) {
98 diff -purN bb.old/editors/awx.c bb.dev/editors/awx.c
99 --- bb.old/editors/awx.c 1970-01-01 01:00:00.000000000 +0100
100 +++ bb.dev/editors/awx.c 2007-05-20 22:57:59.666934656 +0200
103 + * awk web extension
105 + * Copyright (C) 2007 by Felix Fietkau <nbd@openwrt.org>
107 + * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
112 +#include "awx_parser.h"
114 +#define LINE_BUF 2048
115 +#define HASH_MAX 1536
116 +#define TR_START "@TR<<"
122 +static xhash *lstr = NULL;
123 +static xhash *formvar = NULL;
124 +static int lang_inuse = 0;
126 +/* look up a translation symbol from the hash */
127 +static inline char *translate_lookup(char *str)
129 + char *name, *def, *p;
134 + if (((p = strchr(str, '|')) != NULL)
135 + || ((p = strchr(str, '#')) != NULL)) {
140 + hi = hash_search(lstr, name);
146 + return getvar_s(v);
149 +/* look for translation markers in the line and return the translated string */
150 +static char *translate_line(char *line)
152 + char *tok[MAX_TR * 3];
153 + char *l, *p, *p2, *res;
154 + int len = 0, _pos = 0, i, tr_abort = 0;
155 + static char *backlog = NULL;
157 + if (backlog && line) {
158 + backlog = xrealloc(backlog, strlen(backlog) + strlen(line) + 1);
159 + sprintf(backlog + strlen(backlog), line);
165 + while (l != NULL) {
166 + if ((p = strstr(l, TR_START)) == NULL) {
167 + len += strlen((tok[_pos++] = l));
171 + p2 = strstr(p, TR_END);
174 + backlog = xstrdup(l);
183 + len += strlen((tok[_pos++] = l));
184 + len += strlen((tok[_pos++] = translate_lookup(p + strlen(TR_START))));
187 + l += strlen(TR_END);
191 + p = xmalloc(len + 1);
194 + for (i = 0; i < _pos; i++) {
196 + p += strlen(tok[i]);
198 + if (!tr_abort && backlog) {
206 +/* hook for intercepting awk's use of puts. used for running all printed strings
207 + * through the translation system */
208 +static inline int fputs_hook (__const char *__restrict __s, FILE *__restrict __stream)
210 + if (lang_inuse && (__stream == stdout)) {
214 + str = translate_line((char *) __s);
215 + ret = fputs(str, __stream);
221 + return fputs(__s, __stream);
224 +static var *init_lang(var *res, var *args, int nargs)
227 + lstr = hash_init();
234 +/* load and parse language file */
235 +static void load_lang_file(char *file)
238 + char *b, *name, *value;
239 + char buf1[LINE_BUF];
241 + if ((f = fopen(file, "r")) == NULL)
244 + while (!feof(f) && (fgets(buf1, LINE_BUF - 1, f) != NULL)) {
247 + continue; /* skip comments */
249 + while (isspace(*b))
250 + b++; /* skip leading spaces */
255 + if ((b = strstr(name, "=>")) == NULL)
256 + continue; /* separator not found */
263 + for (b--; isspace(*b); b--)
264 + *b = 0; /* remove trailing spaces */
266 + while (isspace(*value))
267 + value++; /* skip leading spaces */
269 + for (b = value + strlen(value) - 1; isspace(*b); b--)
270 + *b = 0; /* remove trailing spaces */
275 + setvar_s(findvar(lstr,name), value);
281 +static var *load_lang(var *res, var *args, int nargs)
283 + const char *langfmt = "/usr/lib/webif/lang/%s.txt";
284 + char lbuf[LINE_BUF];
288 + init_lang(res, args, nargs);
290 + lang = getvar_s(args);
291 + if (!lang || !strcmp(lang, ""))
294 + sprintf(lbuf, langfmt, lang);
295 + load_lang_file(lbuf);
300 +/* read the contents of an entire file */
301 +static char *get_file(char *fname)
307 + F = fopen(fname, "r");
312 + if (fseek(F, 0, SEEK_END) == 0) {
314 + s = (char *)xmalloc(flen+4);
315 + fseek(F, 0, SEEK_SET);
316 + i = 1 + fread(s+1, 1, flen, F);
318 + for (i=j=1; j>0; i+=j) {
319 + s = (char *)xrealloc(s, i+4096);
320 + j = fread(s+i, 1, 4094, F);
332 + * taken from parse_program from awk.c
333 + * END{} is not parsed here, and BEGIN{} is executed immediately
335 +static void parse_include(char *p)
338 + chain *initseq = NULL;
344 + memset(&tmp, 0, sizeof(tmp));
347 + while ((tclass = next_token(TC_EOF | TC_OPSEQ |
348 + TC_OPTERM | TC_BEGIN | TC_FUNCDECL)) != TC_EOF) {
349 + if (tclass & TC_OPTERM)
353 + if (tclass & TC_BEGIN) {
354 + initseq = xzalloc(sizeof(chain));
357 + } else if (tclass & TC_FUNCDECL) {
358 + next_token(TC_FUNCTION);
360 + f = newfunc(t.string);
362 + f->x.body.first = NULL;
364 + while (next_token(TC_VARIABLE | TC_SEQTERM) & TC_VARIABLE) {
365 + v = findvar(ahash, t.string);
366 + v->x.aidx = (f->nargs)++;
368 + if (next_token(TC_COMMA | TC_SEQTERM) & TC_SEQTERM)
371 + seq = &(f->x.body);
373 + clear_array(ahash);
376 + if (initseq && initseq->first)
377 + tv = evaluate(initseq->first, tv);
382 +/* include an awk file and run its BEGIN{} section */
383 +static xhash *includes = NULL;
384 +static void include_file(char *filename)
390 + includes = hash_init();
392 + /* find out if the file has been included already */
393 + v = findvar(includes, filename);
398 + /* read include file */
399 + s = get_file(filename);
401 + fprintf(stderr, "Could not open file.\n");
404 + parse_include(s+1);
408 +static var *include(var *res, var *args, int nargs)
412 + s = getvar_s(args);
413 + if (s && (strlen(s) > 0))
419 +/* parse an awk expression */
420 +static var *parse_awk(char *str, var *tv)
425 + memset(&body, 0, sizeof(body));
429 + /* end of expression, assume that there's going to be a free byte
430 + * at the end of the string that can be used for the ')' */
431 + strcat(str + strlen(str), "}");
432 + n = parse_expr(TC_GRPTERM);
436 + return evaluate(n, tv);
439 +static inline void print_translate(char *s)
443 + str = translate_line(s);
444 + fputs(str, stdout);
450 +static void render_element(struct template_cb *tcb, struct template_element *e)
461 + s = strdup(e->var);
462 + print_translate(s);
466 + s = strdup(e->var);
468 + s2 = strdup(getvar_s(parse_awk(s, v)));
470 + print_translate(s);
475 + s = strdup(e->var);
477 + i = istrue(parse_awk(s, v));
482 + execute_template(tcb, e->sub);
484 + execute_template(tcb, e->sub2);
487 + v = newvar(e->var);
488 + hashwalk_init(v, iamarray(findvar(vhash, e->in)));
489 + while (hashwalk_next(v)) {
490 + execute_template(tcb, e->sub);
500 +/* awk method render(), which opens a template file and processes all awk ssi calls */
501 +static void render_file(char *filename)
503 + struct template_cb tcb;
504 + struct template_element *e;
510 + oldprg = programname;
511 + programname = filename;
513 + f = fopen(filename, "r");
517 + memset(&tcb, 0, sizeof(tcb));
518 + tcb.handle_element = render_element;
519 + e = parse_template(&tcb, f);
520 + execute_template(&tcb, e);
521 + free_template(&tcb, e);
523 + programname = oldprg;
527 +static var *render(var *res, var *args, int nargs)
531 + s = getvar_s(args);
540 +/* Call render, but only if this function hasn't been called already */
541 +static int layout_rendered = 0;
542 +static var *render_layout(var *res, var *args, int nargs)
544 + if (layout_rendered)
546 + layout_rendered = 1;
547 + return render(res, args, nargs);
550 +/* registers a global c function for the awk interpreter */
551 +static void register_cfunc(char *name, awk_cfunc cfunc, int nargs)
557 + f->x.cfunc = cfunc;
561 +static void putvar(vartype type, char *name, char *value)
563 + if (type != FORM_VAR)
566 + setvar_u(findvar(formvar, name), value);
569 +static char *cgi_getvar(char *name)
572 + formvar = hash_init();
576 + if (!formvar || !name)
579 + return getvar_s(findvar(formvar, name));
582 +/* function call for accessing cgi form variables */
583 +static var *getvar(var *res, var *args, int nargs)
588 + s = getvar_s(args);
592 + svar = cgi_getvar(s);
596 + setvar_u(res, svar);
601 +/* call an awk function without arguments by string reference */
602 +static var *call(var *res, var *args, int nargs)
604 + char *s = getvar_s(args);
611 + if (f && f->type == AWKFUNC && f->x.body.first)
612 + return evaluate(f->x.body.first, res);
619 +static int run_awxscript(char *name)
621 + var tv, *layout, *action;
622 + char *tmp, *s = NULL;
625 + programname = name;
627 + /* read the main controller source */
628 + s = get_file(programname);
630 + fprintf(stderr, "Could not open file\n");
633 + parse_program(s+1);
637 + /* set some defaults for ACTION and LAYOUT, which will have special meaning */
638 + layout = newvar("LAYOUT");
639 + setvar_s(layout, "views/layout.ahtml");
641 + /* run the BEGIN {} block */
642 + evaluate(beginseq.first, &tv);
644 + action = newvar("ACTION");
645 + if (!(strlen(getvar_s(action)) > 0)) {
646 + tmp = cgi_getvar("action");
647 + if (!tmp || (strlen(tmp) <= 0))
648 + tmp = strdup("default");
650 + setvar_p(action, tmp);
653 + /* call the action (precedence: begin block override > cgi parameter > "default") */
654 + tmp = xmalloc(strlen(getvar_s(action)) + 7);
655 + sprintf(tmp, "handle_%s", getvar_s(action));
656 + setvar_s(action, tmp);
657 + call(&tv, action, 1);
660 + /* render the selected layout, will do nothing if render_layout has been called from awk */
661 + render_layout(&tv, layout, 1);
667 +/* main awx processing function. called from awk_main() */
668 +static int do_awx(int argc, char **argv)
673 + char **args = argv;
677 + /* register awk C callbacks */
678 + register_cfunc("getvar", getvar, 1);
679 + register_cfunc("render", render, 1);
680 + register_cfunc("render_layout", render_layout, 1);
681 + register_cfunc("call", call, 1);
682 + register_cfunc("include", include, 1);
683 + register_cfunc("init_lang", init_lang, 1);
684 + register_cfunc("load_lang", load_lang, 1);
689 + /* fill in ARGV array */
690 + setvar_i(V[ARGC], argc + 1);
691 + setari_u(V[ARGV], 0, "awx");
694 + setari_u(V[ARGV], ++i, *args++);
696 + while((c = getopt(argc, argv, "i:f:")) != EOF) {
699 + programname = optarg;
700 + include_file(optarg);
704 + programname = optarg;
705 + render_file(optarg);
713 + fprintf(stderr, "Invalid argument.\n");
717 + ret = run_awxscript(*argv);
723 +/* entry point for awx applet */
724 +int awx_main(int argc, char **argv)
727 + return awk_main(argc, argv);
730 diff -purN bb.old/editors/awx_parser.h bb.dev/editors/awx_parser.h
731 --- bb.old/editors/awx_parser.h 1970-01-01 01:00:00.000000000 +0100
732 +++ bb.dev/editors/awx_parser.h 2007-05-20 22:30:31.380512280 +0200
734 +#ifndef __TEMPLATE_PARSER_H
735 +#define __TEMPLATE_PARSER_H
744 +struct template_element;
747 +struct template_cb {
748 + void *(*prepare_code)(struct template_element *);
749 + void (*handle_element)(struct template_cb *, struct template_element *);
750 + void (*free_code)(struct template_element *);
753 +struct template_element {
759 + struct template_element *parent;
760 + struct template_element *sub;
761 + struct template_element *sub2;
762 + struct template_element *prev;
763 + struct template_element *next;
767 +struct template_element *parse_template(struct template_cb *cb, FILE *in);
768 +void execute_template(struct template_cb *cb, struct template_element *e);
769 +void free_template(struct template_cb *cb, struct template_element *e);
772 diff -purN bb.old/editors/awx_parser.l bb.dev/editors/awx_parser.l
773 --- bb.old/editors/awx_parser.l 1970-01-01 01:00:00.000000000 +0100
774 +++ bb.dev/editors/awx_parser.l 2007-05-20 22:30:31.380512280 +0200
780 +#include "busybox.h"
781 +#include "awx_parser.h"
798 +char *statestr[] = {
799 + [S_INIT] = "S_INIT",
800 + [S_TEXT] = "S_TEXT",
801 + [S_CODE] = "S_CODE",
802 + [S_IF_START] = "S_IF_START",
803 + [S_FOR_START] = "S_FOR_START",
804 + [S_FOR_IN] = "S_FOR_IN",
809 + [T_TEXT] = "T_TEXT",
812 + [T_CODE] = "T_CODE"
816 +static struct template_cb *parse_cb;
817 +static struct template_element *cur, *head;
818 +static char *textbuf;
819 +static unsigned int buflen;
820 +static unsigned int buf_offset;
821 +static int _lnr = 0;
823 +static void buf_realloc(void)
826 + textbuf = xrealloc(textbuf, buflen);
829 +static void parse_error(char *str)
831 + fprintf(stderr, "Parse error%s%s\n", (str ? ": " : "!"), (str ?: ""));
836 +static struct template_element *new_template_element(struct template_element *parent)
838 + struct template_element *ptr;
840 + ptr = xzalloc(sizeof(*ptr));
841 + ptr->parent = parent;
845 +static inline void next_template_element(void)
847 + cur->next = new_template_element(cur->parent);
848 + cur->next->prev = cur;
852 +static void addtext(char *text)
854 + while(buf_offset + strlen(text) + 1 > buflen)
857 + buf_offset += sprintf(&textbuf[buf_offset], "%s", text);
860 +static void set_state(int newstate)
865 + static int _rec = 0;
866 + fprintf(stderr, "DEBUG(%d): %s => %s: %s\n", _rec, statestr[state], statestr[newstate], textbuf);
868 + ptr = xstrdup(textbuf);
869 + if (state == S_FOR_IN)
874 + if (parse_cb && (cur->t == T_CODE) && parse_cb->prepare_code)
875 + parse_cb->prepare_code(cur);
888 + if (ptr || !cur->prev)
889 + next_template_element();
893 + if (ptr || !cur->prev)
894 + next_template_element();
900 + parse_error("'@else' without parent element");
901 + cur->sub2 = new_template_element(cur);
911 + parse_error("'@end' without parent element");
913 + next_template_element();
920 + next_template_element();
927 + cur->sub = new_template_element(cur);
936 + if (ptr || !cur->prev)
937 + next_template_element();
950 +"<%"[ \n\t]*"@if"[ \n\t]+ {
951 + if (state == S_TEXT)
952 + set_state(S_IF_START);
957 +"<%"[ \n\t]*"@for"[ \n\t]+ {
958 + if (state == S_TEXT)
959 + set_state(S_FOR_START);
964 +[ \n\t]+"in"[ \n\t]+ {
965 + if (state == S_FOR_START)
966 + set_state(S_FOR_IN);
971 +"<%"[ \n\t]*"@end"[ \n\t]*%> {
972 + if (state != S_TEXT)
977 +"<%"[ \n\t]*"@else"[ \n\t]*%> {
978 + if (state != S_TEXT)
984 + if (state != S_TEXT)
985 + parse_error("'<%' cannot be nested");
990 + if (state == S_TEXT)
997 + if (state == S_TEXT)
1008 +void execute_template(struct template_cb *cb, struct template_element *e)
1010 + static int rec = 0;
1014 + fprintf(stderr, "DEBUG: execute(%d)\t%s\n", rec, typestr[e->t]);
1017 + if (cb->handle_element)
1018 + cb->handle_element(cb, e);
1030 +struct template_element *parse_template(struct template_cb *cb, FILE *in)
1038 + textbuf = xzalloc(buflen);
1040 + head = xzalloc(sizeof(*head));
1050 +void free_template(struct template_cb *cb, struct template_element *e)
1052 + struct template_element *next;
1059 + if (cb->free_code)
1064 + free_template(cb, e->sub);
1076 + return free_template(cb, next);
1078 diff -purN bb.old/editors/Config.in bb.dev/editors/Config.in
1079 --- bb.old/editors/Config.in 2007-05-20 04:17:05.003197632 +0200
1080 +++ bb.dev/editors/Config.in 2007-05-20 22:30:31.380512280 +0200
1081 @@ -12,6 +12,13 @@ config AWK
1082 Awk is used as a pattern scanning and processing language. This is
1083 the BusyBox implementation of that programming language.
1086 + bool "Enable awx (awk web extension)"
1090 + awx - awk web extension
1092 config FEATURE_AWK_MATH
1093 bool "Enable math functions (requires libm)"
1095 diff -purN bb.old/editors/Kbuild bb.dev/editors/Kbuild
1096 --- bb.old/editors/Kbuild 2007-03-18 17:59:37.000000000 +0100
1097 +++ bb.dev/editors/Kbuild 2007-05-20 22:30:31.381512128 +0200
1098 @@ -10,3 +10,12 @@ lib-$(CONFIG_ED) += ed.o
1099 lib-$(CONFIG_PATCH) += patch.o
1100 lib-$(CONFIG_SED) += sed.o
1101 lib-$(CONFIG_VI) += vi.o
1102 +lib-$(CONFIG_AWX) += awx_parser.o
1104 +editors/awx_parser.c: editors/awx_parser.l editors/awx_parser.h
1108 +editors/awx_parser.o: editors/awx_parser.c FORCE
1109 + $(call cmd,force_checksrc)
1110 + $(call if_changed_rule,cc_o_c)
1111 diff -purN bb.old/include/applets.h bb.dev/include/applets.h
1112 --- bb.old/include/applets.h 2007-05-20 04:17:05.003197632 +0200
1113 +++ bb.dev/include/applets.h 2007-05-20 22:30:31.381512128 +0200
1114 @@ -60,6 +60,7 @@ USE_ARP(APPLET(arp, _BB_DIR_SBIN, _BB_SU
1115 USE_ARPING(APPLET(arping, _BB_DIR_USR_BIN, _BB_SUID_NEVER))
1116 USE_ASH(APPLET_NOUSAGE(ash, ash, _BB_DIR_BIN, _BB_SUID_NEVER))
1117 USE_AWK(APPLET(awk, _BB_DIR_USR_BIN, _BB_SUID_NEVER))
1118 +USE_AWX(APPLET_NOUSAGE(awx, awx, _BB_DIR_USR_BIN, _BB_SUID_NEVER))
1119 USE_BASENAME(APPLET(basename, _BB_DIR_USR_BIN, _BB_SUID_NEVER))
1120 USE_BBCONFIG(APPLET(bbconfig, _BB_DIR_BIN, _BB_SUID_NEVER))
1121 //USE_BBSH(APPLET(bbsh, _BB_DIR_BIN, _BB_SUID_NEVER))
1122 diff -purN bb.old/include/cgi.h bb.dev/include/cgi.h
1123 --- bb.old/include/cgi.h 1970-01-01 01:00:00.000000000 +0100
1124 +++ bb.dev/include/cgi.h 2007-05-20 22:30:31.381512128 +0200
1129 +typedef enum { FORM_VAR, COOKIE_VAR } vartype;
1130 +typedef void (*var_handler) (vartype, char *, char *);
1131 +int cgi_init(var_handler);
1134 diff -purN bb.old/libbb/cgi.c bb.dev/libbb/cgi.c
1135 --- bb.old/libbb/cgi.c 1970-01-01 01:00:00.000000000 +0100
1136 +++ bb.dev/libbb/cgi.c 2007-05-20 22:30:31.382511976 +0200
1138 +/* --------------------------------------------------------------------------
1139 + * functions for processing cgi form data
1140 + * Copyright (C) 2007 by Felix Fietkau <nbd@openwrt.org>
1142 + * parts taken from the core of haserl.cgi - a poor-man's php for embedded/lightweight environments
1143 + * $Id: haserl.c,v 1.13 2004/11/10 17:59:35 nangel Exp $
1144 + * Copyright (c) 2003,2004 Nathan Angelacos (nangel@users.sourceforge.net)
1146 + * This program is free software; you can redistribute it and/or modify
1147 + * it under the terms of the GNU General Public License as published by
1148 + * the Free Software Foundation; either version 2 of the License, or
1149 + * (at your option) any later version.
1151 + * This program is distributed in the hope that it will be useful,
1152 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1153 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1154 + * General Public License for more details.
1156 + * You should have received a copy of the GNU General Public License
1157 + * along with this program; if not, write to the Free Software
1158 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1161 + * The x2c() and unescape_url() routines were taken from
1162 + * http://www.jmarshall.com/easy/cgi/getcgi.c.txt
1164 + * The comments in that text file state:
1166 + *** Written in 1996 by James Marshall, james@jmarshall.com, except
1167 + *** that the x2c() and unescape_url() routines were lifted directly
1168 + *** from NCSA's sample program util.c, packaged with their HTTPD.
1169 + *** For the latest, see http://www.jmarshall.com/easy/cgi/
1172 + ------------------------------------------------------------------------- */
1175 +#include <unistd.h>
1177 +#include <sys/mman.h>
1178 +#include <sys/types.h>
1179 +#include <sys/wait.h>
1180 +#include <sys/stat.h>
1181 +#include <sys/fcntl.h>
1182 +#include <stdlib.h>
1183 +#include <string.h>
1186 +#ifndef MAX_UPLOAD_KB
1187 +#define MAX_UPLOAD_KB 2048
1189 +#define TEMPDIR "/tmp"
1191 +static int global_upload_size = 0;
1192 +static int ReadMimeEncodedInput(char *qs);
1193 +static var_handler do_putvar = NULL;
1196 + * Convert 2 char hex string into char it represents
1197 + * (from http://www.jmarshall.com/easy/cgi)
1199 +static char x2c (char *what) {
1202 + digit=(what[0] >= 'A' ? ((what[0] & 0xdf) - 'A')+10 : (what[0] - '0'));
1204 + digit+=(what[1] >= 'A' ? ((what[1] & 0xdf) - 'A')+10 : (what[1] - '0'));
1210 + * unsescape %xx to the characters they represent
1213 +static void unescape_url (char *url) {
1216 + for (i=0, j=0; url[j]; ++i, ++j) {
1217 + if ((url[i] = url[j]) == '%') {
1218 + url[i] = x2c(&url[j+1]);
1225 +static inline void put_var(vartype type, char *var)
1232 + val = strchr(var, '=');
1238 + do_putvar(type, var, val);
1245 + * if HTTP_COOKIE is passed as an environment variable,
1246 + * attempt to parse its values into environment variables
1248 +static void CookieVars (void)
1253 + if (getenv("HTTP_COOKIE") != NULL)
1254 + qs=strdup(getenv("HTTP_COOKIE"));
1258 + /** split on; to extract name value pairs */
1259 + token=strtok(qs, ";");
1261 + // skip leading spaces
1262 + while ( token[0] == ' ' )
1265 + put_var(COOKIE_VAR, token);
1267 + token = strtok(NULL, ";");
1273 + * Read cgi variables from query string, and put in environment
1275 +static int ReadCGIQueryString (void)
1281 + if (getenv("QUERY_STRING") != NULL)
1282 + qs=strdup(getenv("QUERY_STRING"));
1286 + /* change plusses into spaces */
1287 + for (i=0; qs[i]; i++ ) { if (qs[i] == '+' ) { qs[i] = ' ';} };
1289 + /** split on & and ; to extract name value pairs */
1291 + token=strtok(qs, "&;");
1293 + unescape_url(token);
1294 + put_var(FORM_VAR, token);
1295 + token=strtok(NULL, "&;");
1304 + * Read cgi variables from stdin (for POST queries)
1305 + * (oh... and if its mime-encoded file upload, we save the
1306 + * file to /tmp; and return the name of the tmp file
1307 + * the cgi script is responsible for disposing of the tmp file
1310 +static int ReadCGIPOSTValues (void) {
1312 + int content_length;
1317 + if (getenv("CONTENT_LENGTH") == NULL)
1320 + content_length = atoi(getenv("CONTENT_LENGTH"));
1322 + /* protect ourselves from 20GB file uploads */
1323 + if (content_length > MAX_UPLOAD_KB * 1024 ) {
1324 + /* But we need to finish reading the content */
1325 + while ( fread( &i, sizeof(int), 1, stdin) == 1 );
1329 + if (!(qs=malloc(content_length+1)))
1332 + /* set the buffer to null, so that a browser messing with less
1333 + data than content_length won't buffer underrun us */
1334 + memset(qs, 0 ,content_length+1);
1336 + if ((!fread(qs,content_length,1,stdin) &&
1337 + (content_length > 0)
1338 + && !feof(stdin))) {
1344 + if (getenv("CONTENT_TYPE") && (strncasecmp(getenv("CONTENT_TYPE"), "multipart/form-data", 19) == 0)) {
1345 + /* This is a mime request, we need to go to the mime handler */
1346 + i=ReadMimeEncodedInput(qs);
1352 + /* change plusses into spaces */
1353 + for (i=0; qs[i]; i++ ) { if (qs[i] == '+' ) { qs[i] = ' ';} };
1355 + /** split on & and ; to extract name value pairs */
1356 + token=strtok(qs, "&;");
1358 + unescape_url(token);
1359 + put_var(FORM_VAR, token);
1360 + token=strtok(NULL, "&;");
1369 + * LineToStr - Scans char and replaces the first "\n" with a "\0";
1370 + * If it finds a "\r", it does that to; (fix DOS brokennes) returns
1371 + * the length of the string;
1373 +static int LineToStr (char *string, size_t max) {
1376 + while ((offset < max) && (string[offset] != '\n') && (string[offset] != '\r'))
1379 + if (string[offset] == '\r') {
1380 + string[offset]='\0';
1383 + if (string[offset] == '\n') {
1384 + string[offset]='\0';
1393 + * ReadMimeEncodedInput - handles things that are mime encoded
1394 + * takes a pointer to the input; returns 0 on success
1397 +static int ReadMimeEncodedInput(char *qs)
1409 + char tmpname[] = TEMPDIR "/XXXXXX";
1411 + /* we should only get here if the content type was set. Segfaults happen
1412 + if Content_Type is null */
1414 + if (getenv("CONTENT_LENGTH") == NULL)
1415 + /* No content length?! */
1418 + cl=atoi(getenv("CONTENT_LENGTH"));
1420 + /* we do this 'cause we can't mess with the real env. variable - it would
1421 + * overwrite the environment - I tried.
1423 + i=strlen(getenv("CONTENT_TYPE"))+1;
1426 + memcpy(ct, getenv("CONTENT_TYPE"), i);
1432 + while (i < strlen(ct) && (strncmp("boundary=", &ct[i], 9) != 0))
1435 + if (i == strlen(ct)) {
1436 + /* no boundary informaiton found */
1440 + boundary=&ct[i+7];
1441 + /* add two leading -- to the boundary */
1445 + /* begin the big loop. Look for:
1447 + Content-Disposition: form-data; name="......."
1452 + Content-Disposition: form-data; name="....." filename="....."
1460 + while (offset < cl) {
1461 + /* first look for boundary */
1462 + while ((offset < cl) && (memcmp(&qs[offset], boundary, strlen(boundary))))
1465 + /* if we got here and we ran off the end, its an error */
1466 + if (offset >= cl) {
1471 + /* if the two characters following the boundary are --, */
1472 + /* then we are at the end, exit */
1473 + if (memcmp(&qs[offset+strlen(boundary)], "--", 2) == 0) {
1477 + /* find where the offset should be */
1478 + line=LineToStr(&qs[offset], cl-offset);
1481 + /* Now we're going to look for content-disposition */
1482 + line=LineToStr(&qs[offset], cl-offset);
1483 + if (strncasecmp(&qs[offset], "Content-Disposition", 19) != 0) {
1484 + /* hmm... content disposition was not where we expected it */
1488 + /* Found it, so let's go find "name=" */
1489 + if (!(envname=strstr(&qs[offset], "name="))) {
1490 + /* now name= is missing?! */
1496 + /* is there a filename tag? */
1497 + if ((filename=strstr(&qs[offset], "filename="))!= NULL)
1502 + /* make envname and filename ASCIIZ */
1503 + for (i=0; (envname[i] != '"') && (envname[i] != '\0'); i++);
1505 + envname[i] = '\0';
1507 + for (i=0; (filename[i] != '"') && (filename[i] != '\0'); i++);
1508 + filename[i] = '\0';
1512 + /* Ok, by some miracle, we have the name; let's skip till we */
1513 + /* come to a blank line */
1514 + line=LineToStr(&qs[offset], cl-offset);
1515 + while (strlen(&qs[offset]) > 1) {
1517 + line=LineToStr(&qs[offset], cl-offset);
1521 + /* And we go back to looking for a boundary */
1522 + while ((offset < cl) && (memcmp(&qs[offset], boundary, strlen(boundary))))
1525 + /* strip [cr] lf */
1526 + if ((qs[offset-1] == '\n') && (qs[offset-2] == '\r'))
1533 + /* ok, at this point, we know where the name is, and we know */
1534 + /* where the content is... we have to do one of two things */
1535 + /* based on whether its a file or not */
1536 + if (filename==NULL) { /* its not a file, so its easy */
1537 + /* just jam the content after the name */
1538 + memcpy(&envname[strlen(envname)+1], &qs[datastart], offset-datastart+1);
1539 + envname[strlen(envname)]='=';
1540 + put_var(FORM_VAR, envname);
1541 + } else { /* handle the fileupload case */
1542 + if (offset-datastart) { /* only if they uploaded */
1543 + if ( global_upload_size == 0 ) {
1546 + /* stuff in the filename */
1547 + ptr= calloc ( sizeof (char), strlen(envname)+strlen(filename)+2+5 );
1548 + sprintf (ptr, "%s_name=%s", envname, filename);
1549 + put_var(FORM_VAR, ptr);
1552 + fd=mkstemp(tmpname);
1557 + write(fd, &qs[datastart], offset-datastart);
1559 + ptr= calloc (sizeof(char), strlen(envname)+strlen(tmpname)+2);
1560 + sprintf (ptr, "%s=%s", envname, tmpname);
1561 + put_var(FORM_VAR, ptr);
1571 +/*-------------------------------------------------------------------------
1575 + *------------------------------------------------------------------------*/
1577 +int cgi_init(var_handler putvar_handler)
1581 + do_putvar = putvar_handler;
1583 + /* Read the current environment into our chain */
1585 + if (getenv("REQUEST_METHOD")) {
1586 + if (strcasecmp(getenv("REQUEST_METHOD"), "GET") == 0)
1587 + retval = ReadCGIQueryString();
1589 + if (strcasecmp(getenv("REQUEST_METHOD"), "POST") == 0)
1590 + retval = ReadCGIPOSTValues();
1595 diff -purN bb.old/libbb/Kbuild bb.dev/libbb/Kbuild
1596 --- bb.old/libbb/Kbuild 2007-05-20 04:17:05.004197480 +0200
1597 +++ bb.dev/libbb/Kbuild 2007-05-20 22:30:31.382511976 +0200
1598 @@ -118,3 +118,6 @@ lib-$(CONFIG_GREP) += xregcomp.o
1599 lib-$(CONFIG_MDEV) += xregcomp.o
1600 lib-$(CONFIG_LESS) += xregcomp.o
1601 lib-$(CONFIG_DEVFSD) += xregcomp.o
1603 +lib-$(CONFIG_AWX) += cgi.o