1 Index: busybox-1.4.2/editors/awk.c
2 ===================================================================
3 --- busybox-1.4.2.orig/editors/awk.c 2007-06-04 13:21:35.202625056 +0200
4 +++ busybox-1.4.2/editors/awk.c 2007-06-04 13:21:37.927210856 +0200
6 /* these flags are static, don't change them when value is changed */
7 #define VF_DONTTOUCH (VF_ARRAY | VF_SPECIAL | VF_WALK | VF_CHILD | VF_DIRTY)
10 +#define fputs(s, stream) fputs_hook(s, stream)
11 +static inline int fputs_hook (__const char *__restrict __s, FILE *__restrict __stream);
15 typedef struct var_s {
16 unsigned short type; /* flags */
21 +typedef var *(*awk_cfunc)(var *res, var *args, int nargs);
23 typedef struct func_s {
25 - struct chain_s body;
26 + enum { AWKFUNC, CFUNC } type;
29 + struct chain_s body;
35 next_token(TC_FUNCTION);
37 f = newfunc(t.string);
38 - f->body.first = NULL;
40 + f->x.body.first = NULL;
42 while (next_token(TC_VARIABLE | TC_SEQTERM) & TC_VARIABLE) {
43 v = findvar(ahash, t.string);
45 if (next_token(TC_COMMA | TC_SEQTERM) & TC_SEQTERM)
57 - if (! op->r.f->body.first)
58 + if ((op->r.f->type == AWKFUNC) &&
59 + !op->r.f->x.body.first)
60 runtime_error(EMSG_UNDEF_FUNC);
62 X.v = R.v = nvalloc(op->r.f->nargs+1);
63 @@ -2277,7 +2289,11 @@
67 - res = evaluate(op->r.f->body.first, res);
68 + if (op->r.f->type == AWKFUNC)
69 + res = evaluate(op->r.f->x.body.first, res);
70 + else if (op->r.f->type == CFUNC)
71 + res = op->r.f->x.cfunc(res, fnargs, op->r.f->nargs);
76 @@ -2637,6 +2653,11 @@
81 +static int is_awx = 0;
85 int awk_main(int argc, char **argv)
88 @@ -2693,6 +2714,10 @@
97 while((c = getopt(argc, argv, "F:v:f:W:")) != EOF) {
99 Index: busybox-1.4.2/editors/awx.c
100 ===================================================================
101 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
102 +++ busybox-1.4.2/editors/awx.c 2007-06-04 13:21:37.928210704 +0200
105 + * awk web extension
107 + * Copyright (C) 2007 by Felix Fietkau <nbd@openwrt.org>
109 + * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
114 +#include "awx_parser.h"
116 +#define LINE_BUF 2048
117 +#define HASH_MAX 1536
118 +#define TR_START "@TR<<"
124 +static xhash *lstr = NULL;
125 +static xhash *formvar = NULL;
126 +static int lang_inuse = 0;
128 +/* look up a translation symbol from the hash */
129 +static inline char *translate_lookup(char *str)
131 + char *name, *def, *p;
136 + if (((p = strchr(str, '|')) != NULL)
137 + || ((p = strchr(str, '#')) != NULL)) {
142 + hi = hash_search(lstr, name);
148 + return getvar_s(v);
151 +/* look for translation markers in the line and return the translated string */
152 +static char *translate_line(char *line)
154 + char *tok[MAX_TR * 3];
155 + char *l, *p, *p2 = NULL, *res;
156 + int len = 0, _pos = 0, i, tr_abort = 0;
157 + static char *backlog = NULL;
159 + if (backlog && line) {
160 + backlog = xrealloc(backlog, strlen(backlog) + strlen(line) + 1);
161 + sprintf(backlog + strlen(backlog), line);
167 + while (l != NULL) {
168 + if ((p = strstr(l, TR_START)) == NULL) {
169 + len += strlen((tok[_pos++] = l));
173 + p2 = strstr(p, TR_END);
181 + len += strlen((tok[_pos++] = l));
183 + len += strlen((tok[_pos++] = translate_lookup(p + strlen(TR_START))));
186 + l += strlen(TR_END);
190 + p = xmalloc(len + 1);
193 + for (i = 0; i < _pos; i++) {
195 + p += strlen(tok[i]);
201 + if (tr_abort && p2)
207 +/* hook for intercepting awk's use of puts. used for running all printed strings
208 + * through the translation system */
209 +static inline int fputs_hook (__const char *__restrict __s, FILE *__restrict __stream)
211 + if (lang_inuse && (__stream == stdout)) {
215 + str = translate_line((char *) __s);
216 + ret = fputs(str, __stream);
222 + return fputs(__s, __stream);
225 +static var *init_lang(var *res, var *args, int nargs)
228 + lstr = hash_init();
235 +/* load and parse language file */
236 +static void load_lang_file(char *file)
239 + char *b, *name, *value;
240 + char buf1[LINE_BUF];
242 + if ((f = fopen(file, "r")) == NULL)
245 + while (!feof(f) && (fgets(buf1, LINE_BUF - 1, f) != NULL)) {
248 + continue; /* skip comments */
250 + while (isspace(*b))
251 + b++; /* skip leading spaces */
256 + if ((b = strstr(name, "=>")) == NULL)
257 + continue; /* separator not found */
264 + for (b--; isspace(*b); b--)
265 + *b = 0; /* remove trailing spaces */
267 + while (isspace(*value))
268 + value++; /* skip leading spaces */
270 + for (b = value + strlen(value) - 1; isspace(*b); b--)
271 + *b = 0; /* remove trailing spaces */
276 + setvar_s(findvar(lstr,name), value);
282 +static var *load_lang(var *res, var *args, int nargs)
284 + const char *langfmt = "/usr/lib/webif/lang/%s.txt";
285 + char lbuf[LINE_BUF];
289 + init_lang(res, args, nargs);
291 + lang = getvar_s(args);
292 + if (!lang || !strcmp(lang, ""))
295 + sprintf(lbuf, langfmt, lang);
296 + load_lang_file(lbuf);
301 +/* read the contents of an entire file */
302 +static char *get_file(char *fname)
308 + F = fopen(fname, "r");
313 + if (fseek(F, 0, SEEK_END) == 0) {
315 + s = (char *)xmalloc(flen+4);
316 + fseek(F, 0, SEEK_SET);
317 + i = 1 + fread(s+1, 1, flen, F);
319 + for (i=j=1; j>0; i+=j) {
320 + s = (char *)xrealloc(s, i+4096);
321 + j = fread(s+i, 1, 4094, F);
333 + * taken from parse_program from awk.c
334 + * END{} is not parsed here, and BEGIN{} is executed immediately
336 +static void parse_include(char *p)
339 + chain *initseq = NULL;
345 + memset(&tmp, 0, sizeof(tmp));
348 + while ((tclass = next_token(TC_EOF | TC_OPSEQ |
349 + TC_OPTERM | TC_BEGIN | TC_FUNCDECL)) != TC_EOF) {
350 + if (tclass & TC_OPTERM)
354 + if (tclass & TC_BEGIN) {
355 + initseq = xzalloc(sizeof(chain));
358 + } else if (tclass & TC_FUNCDECL) {
359 + next_token(TC_FUNCTION);
361 + f = newfunc(t.string);
363 + f->x.body.first = NULL;
365 + while (next_token(TC_VARIABLE | TC_SEQTERM) & TC_VARIABLE) {
366 + v = findvar(ahash, t.string);
367 + v->x.aidx = (f->nargs)++;
369 + if (next_token(TC_COMMA | TC_SEQTERM) & TC_SEQTERM)
372 + seq = &(f->x.body);
374 + clear_array(ahash);
377 + if (initseq && initseq->first)
378 + tv = evaluate(initseq->first, tv);
383 +/* include an awk file and run its BEGIN{} section */
384 +static xhash *includes = NULL;
385 +static void include_file(char *filename)
391 + includes = hash_init();
393 + /* find out if the file has been included already */
394 + v = findvar(includes, filename);
399 + /* read include file */
400 + s = get_file(filename);
402 + fprintf(stderr, "Could not open file.\n");
405 + parse_include(s+1);
409 +static var *include(var *res, var *args, int nargs)
413 + s = getvar_s(args);
414 + if (s && (strlen(s) > 0))
420 +/* parse an awk expression */
421 +static var *parse_awk(char *str, var *tv)
426 + memset(&body, 0, sizeof(body));
430 + /* end of expression, assume that there's going to be a free byte
431 + * at the end of the string that can be used for the ')' */
432 + strcat(str + strlen(str), "}");
433 + n = parse_expr(TC_GRPTERM);
437 + return evaluate(n, tv);
440 +static inline void print_translate(char *s)
444 + str = translate_line(s);
445 + fputs(str, stdout);
451 +static void render_element(struct template_cb *tcb, struct template_element *e)
462 + s = malloc(strlen(e->var) + 2);
464 + print_translate(s);
468 + s = malloc(strlen(e->var) + 2);
471 + s2 = strdup(getvar_s(parse_awk(s, v)));
473 + print_translate(s2);
478 + s = malloc(strlen(e->var) + 2);
481 + i = istrue(parse_awk(s, v));
486 + execute_template(tcb, e->sub);
488 + execute_template(tcb, e->sub2);
491 + v = newvar(e->var);
492 + hashwalk_init(v, iamarray(findvar(vhash, e->in)));
493 + while (hashwalk_next(v)) {
494 + execute_template(tcb, e->sub);
504 +/* awk method render(), which opens a template file and processes all awk ssi calls */
505 +static void render_file(char *filename)
507 + struct template_cb tcb;
508 + struct template_element *e;
517 + oldprg = programname;
518 + programname = filename;
520 + f = fopen(filename, "r");
524 + memset(&tcb, 0, sizeof(tcb));
525 + tcb.handle_element = render_element;
526 + e = parse_template(&tcb, f);
527 + execute_template(&tcb, e);
528 + free_template(&tcb, e);
530 + programname = oldprg;
534 +static var *render(var *res, var *args, int nargs)
538 + s = getvar_s(args);
547 +/* Call render, but only if this function hasn't been called already */
548 +static int layout_rendered = 0;
549 +static var *render_layout(var *res, var *args, int nargs)
551 + if (layout_rendered)
553 + layout_rendered = 1;
554 + return render(res, args, nargs);
557 +/* registers a global c function for the awk interpreter */
558 +static void register_cfunc(char *name, awk_cfunc cfunc, int nargs)
564 + f->x.cfunc = cfunc;
568 +static void putvar(vartype type, char *name, char *value)
570 + if (type != FORM_VAR)
573 + setvar_u(findvar(formvar, name), value);
576 +static char *cgi_getvar(char *name)
579 + formvar = hash_init();
583 + if (!formvar || !name)
586 + return getvar_s(findvar(formvar, name));
589 +/* function call for accessing cgi form variables */
590 +static var *getvar(var *res, var *args, int nargs)
595 + s = getvar_s(args);
599 + svar = cgi_getvar(s);
603 + setvar_u(res, svar);
608 +/* call an awk function without arguments by string reference */
609 +static var *call(var *res, var *args, int nargs)
611 + char *s = getvar_s(args);
618 + if (f && f->type == AWKFUNC && f->x.body.first)
619 + return evaluate(f->x.body.first, res);
626 +static int run_awxscript(char *name)
628 + var tv, *layout, *action;
629 + char *tmp, *s = NULL;
632 + programname = name;
634 + /* read the main controller source */
635 + s = get_file(programname);
637 + fprintf(stderr, "Could not open file\n");
640 + parse_program(s+1);
644 + /* set some defaults for ACTION and LAYOUT, which will have special meaning */
645 + layout = newvar("LAYOUT");
646 + setvar_s(layout, "views/layout.ahtml");
648 + /* run the BEGIN {} block */
649 + evaluate(beginseq.first, &tv);
651 + action = newvar("ACTION");
652 + if (!(strlen(getvar_s(action)) > 0)) {
653 + tmp = cgi_getvar("action");
654 + if (!tmp || (strlen(tmp) <= 0))
655 + tmp = strdup("default");
657 + setvar_p(action, tmp);
660 + /* call the action (precedence: begin block override > cgi parameter > "default") */
661 + tmp = xmalloc(strlen(getvar_s(action)) + 7);
662 + sprintf(tmp, "handle_%s", getvar_s(action));
663 + setvar_s(action, tmp);
664 + call(&tv, action, 1);
667 + /* render the selected layout, will do nothing if render_layout has been called from awk */
668 + render_layout(&tv, layout, 1);
674 +/* main awx processing function. called from awk_main() */
675 +static int do_awx(int argc, char **argv)
680 + char **args = argv;
684 + /* register awk C callbacks */
685 + register_cfunc("getvar", getvar, 1);
686 + register_cfunc("render", render, 1);
687 + register_cfunc("render_layout", render_layout, 1);
688 + register_cfunc("call", call, 1);
689 + register_cfunc("include", include, 1);
690 + register_cfunc("init_lang", init_lang, 1);
691 + register_cfunc("load_lang", load_lang, 1);
696 + /* fill in ARGV array */
697 + setvar_i(V[ARGC], argc + 1);
698 + setari_u(V[ARGV], 0, "awx");
701 + setari_u(V[ARGV], ++i, *args++);
703 + while((c = getopt(argc, argv, "i:f:")) != EOF) {
706 + programname = optarg;
707 + include_file(optarg);
711 + programname = optarg;
712 + render_file(optarg);
720 + fprintf(stderr, "Invalid argument.\n");
724 + ret = run_awxscript(*argv);
730 +/* entry point for awx applet */
731 +int awx_main(int argc, char **argv)
734 + return awk_main(argc, argv);
737 Index: busybox-1.4.2/editors/awx_parser.h
738 ===================================================================
739 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
740 +++ busybox-1.4.2/editors/awx_parser.h 2007-06-04 13:21:37.928210704 +0200
742 +#ifndef __TEMPLATE_PARSER_H
743 +#define __TEMPLATE_PARSER_H
752 +struct template_element;
755 +struct template_cb {
756 + void *(*prepare_code)(struct template_element *);
757 + void (*handle_element)(struct template_cb *, struct template_element *);
758 + void (*free_code)(struct template_element *);
761 +struct template_element {
767 + struct template_element *parent;
768 + struct template_element *sub;
769 + struct template_element *sub2;
770 + struct template_element *prev;
771 + struct template_element *next;
775 +struct template_element *parse_template(struct template_cb *cb, FILE *in);
776 +void execute_template(struct template_cb *cb, struct template_element *e);
777 +void free_template(struct template_cb *cb, struct template_element *e);
780 Index: busybox-1.4.2/editors/awx_parser.l
781 ===================================================================
782 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
783 +++ busybox-1.4.2/editors/awx_parser.l 2007-06-04 13:21:37.929210552 +0200
789 +#include "busybox.h"
790 +#include "awx_parser.h"
807 +char *statestr[] = {
808 + [S_INIT] = "S_INIT",
809 + [S_TEXT] = "S_TEXT",
810 + [S_CODE] = "S_CODE",
811 + [S_IF_START] = "S_IF_START",
812 + [S_FOR_START] = "S_FOR_START",
813 + [S_FOR_IN] = "S_FOR_IN",
818 + [T_TEXT] = "T_TEXT",
821 + [T_CODE] = "T_CODE"
825 +static struct template_cb *parse_cb;
826 +static struct template_element *cur, *head;
827 +static char *textbuf;
828 +static unsigned int buflen;
829 +static unsigned int buf_offset;
830 +static int _lnr = 0;
832 +static void buf_realloc(void)
835 + textbuf = xrealloc(textbuf, buflen);
838 +static void parse_error(char *str)
840 + fprintf(stderr, "Parse error%s%s\n", (str ? ": " : "!"), (str ?: ""));
845 +static struct template_element *new_template_element(struct template_element *parent)
847 + struct template_element *ptr;
849 + ptr = xzalloc(sizeof(struct template_element));
850 + ptr->parent = parent;
854 +static inline void next_template_element(void)
856 + cur->next = new_template_element(cur->parent);
857 + cur->next->prev = cur;
861 +static void addtext(char *text)
863 + while(buf_offset + strlen(text) + 1 > buflen)
866 + buf_offset += sprintf(&textbuf[buf_offset], "%s", text);
869 +static void set_state(int newstate)
874 + static int _rec = 0;
875 + fprintf(stderr, "DEBUG(%d): %s => %s: %s\n", _rec, statestr[state], statestr[newstate], textbuf);
877 + ptr = xstrdup(textbuf);
878 + if (state == S_FOR_IN)
883 + if (parse_cb && (cur->t == T_CODE) && parse_cb->prepare_code)
884 + parse_cb->prepare_code(cur);
897 + if (ptr || !cur->prev)
898 + next_template_element();
902 + if (ptr || !cur->prev)
903 + next_template_element();
909 + parse_error("'@else' without parent element");
910 + cur->sub2 = new_template_element(cur);
920 + parse_error("'@end' without parent element");
922 + next_template_element();
929 + next_template_element();
936 + cur->sub = new_template_element(cur);
945 + if (ptr || !cur->prev)
946 + next_template_element();
959 +"<%"[ \n\t]*"@if"[ \n\t]+ {
960 + if (state == S_TEXT)
961 + set_state(S_IF_START);
966 +"<%"[ \n\t]*"@for"[ \n\t]+ {
967 + if (state == S_TEXT)
968 + set_state(S_FOR_START);
973 +[ \n\t]+"in"[ \n\t]+ {
974 + if (state == S_FOR_START)
975 + set_state(S_FOR_IN);
980 +"<%"[ \n\t]*"@end"[ \n\t]*%> {
981 + if (state != S_TEXT)
986 +"<%"[ \n\t]*"@else"[ \n\t]*%> {
987 + if (state != S_TEXT)
993 + if (state != S_TEXT)
994 + parse_error("'<%' cannot be nested");
999 + if (state == S_TEXT)
1001 + set_state(S_TEXT);
1006 + if (state == S_TEXT)
1017 +void execute_template(struct template_cb *cb, struct template_element *e)
1019 + static int rec = 0;
1023 + fprintf(stderr, "DEBUG: execute(%d)\t%s\n", rec, typestr[e->t]);
1026 + if (cb->handle_element)
1027 + cb->handle_element(cb, e);
1039 +struct template_element *parse_template(struct template_cb *cb, FILE *in)
1047 + textbuf = xzalloc(buflen);
1049 + head = xzalloc(sizeof(struct template_element));
1059 +void free_template(struct template_cb *cb, struct template_element *e)
1061 + struct template_element *next;
1068 + if (cb->free_code)
1073 + free_template(cb, e->sub);
1085 + return free_template(cb, next);
1087 Index: busybox-1.4.2/editors/Config.in
1088 ===================================================================
1089 --- busybox-1.4.2.orig/editors/Config.in 2007-06-04 13:21:31.486190040 +0200
1090 +++ busybox-1.4.2/editors/Config.in 2007-06-04 13:21:37.929210552 +0200
1092 Awk is used as a pattern scanning and processing language. This is
1093 the BusyBox implementation of that programming language.
1096 + bool "Enable awx (awk web extension)"
1100 + awx - awk web extension
1102 config FEATURE_AWK_MATH
1103 bool "Enable math functions (requires libm)"
1105 Index: busybox-1.4.2/editors/Kbuild
1106 ===================================================================
1107 --- busybox-1.4.2.orig/editors/Kbuild 2007-06-04 13:21:31.492189128 +0200
1108 +++ busybox-1.4.2/editors/Kbuild 2007-06-04 13:21:37.929210552 +0200
1110 lib-$(CONFIG_PATCH) += patch.o
1111 lib-$(CONFIG_SED) += sed.o
1112 lib-$(CONFIG_VI) += vi.o
1113 +lib-$(CONFIG_AWX) += awx_parser.o
1115 +editors/awx_parser.c: editors/awx_parser.l editors/awx_parser.h
1119 +editors/awx_parser.o: editors/awx_parser.c FORCE
1120 + $(call cmd,force_checksrc)
1121 + $(call if_changed_rule,cc_o_c)
1122 Index: busybox-1.4.2/include/applets.h
1123 ===================================================================
1124 --- busybox-1.4.2.orig/include/applets.h 2007-06-04 13:21:36.732392496 +0200
1125 +++ busybox-1.4.2/include/applets.h 2007-06-04 13:21:37.929210552 +0200
1127 USE_ARPING(APPLET(arping, _BB_DIR_USR_BIN, _BB_SUID_NEVER))
1128 USE_ASH(APPLET_NOUSAGE(ash, ash, _BB_DIR_BIN, _BB_SUID_NEVER))
1129 USE_AWK(APPLET(awk, _BB_DIR_USR_BIN, _BB_SUID_NEVER))
1130 +USE_AWX(APPLET_NOUSAGE(awx, awx, _BB_DIR_USR_BIN, _BB_SUID_NEVER))
1131 USE_BASENAME(APPLET(basename, _BB_DIR_USR_BIN, _BB_SUID_NEVER))
1132 USE_BBCONFIG(APPLET(bbconfig, _BB_DIR_BIN, _BB_SUID_NEVER))
1133 //USE_BBSH(APPLET(bbsh, _BB_DIR_BIN, _BB_SUID_NEVER))
1134 Index: busybox-1.4.2/include/cgi.h
1135 ===================================================================
1136 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
1137 +++ busybox-1.4.2/include/cgi.h 2007-06-04 13:21:37.929210552 +0200
1142 +typedef enum { FORM_VAR, COOKIE_VAR } vartype;
1143 +typedef void (*var_handler) (vartype, char *, char *);
1144 +int cgi_init(var_handler);
1147 Index: busybox-1.4.2/libbb/cgi.c
1148 ===================================================================
1149 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
1150 +++ busybox-1.4.2/libbb/cgi.c 2007-06-04 13:21:37.930210400 +0200
1152 +/* --------------------------------------------------------------------------
1153 + * functions for processing cgi form data
1154 + * Copyright (C) 2007 by Felix Fietkau <nbd@openwrt.org>
1156 + * parts taken from the core of haserl.cgi - a poor-man's php for embedded/lightweight environments
1157 + * $Id: haserl.c,v 1.13 2004/11/10 17:59:35 nangel Exp $
1158 + * Copyright (c) 2003,2004 Nathan Angelacos (nangel@users.sourceforge.net)
1160 + * This program is free software; you can redistribute it and/or modify
1161 + * it under the terms of the GNU General Public License as published by
1162 + * the Free Software Foundation; either version 2 of the License, or
1163 + * (at your option) any later version.
1165 + * This program is distributed in the hope that it will be useful,
1166 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1167 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1168 + * General Public License for more details.
1170 + * You should have received a copy of the GNU General Public License
1171 + * along with this program; if not, write to the Free Software
1172 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1175 + * The x2c() and unescape_url() routines were taken from
1176 + * http://www.jmarshall.com/easy/cgi/getcgi.c.txt
1178 + * The comments in that text file state:
1180 + *** Written in 1996 by James Marshall, james@jmarshall.com, except
1181 + *** that the x2c() and unescape_url() routines were lifted directly
1182 + *** from NCSA's sample program util.c, packaged with their HTTPD.
1183 + *** For the latest, see http://www.jmarshall.com/easy/cgi/
1186 + ------------------------------------------------------------------------- */
1189 +#include <unistd.h>
1191 +#include <sys/mman.h>
1192 +#include <sys/types.h>
1193 +#include <sys/wait.h>
1194 +#include <sys/stat.h>
1195 +#include <sys/fcntl.h>
1196 +#include <stdlib.h>
1197 +#include <string.h>
1200 +#ifndef MAX_UPLOAD_KB
1201 +#define MAX_UPLOAD_KB 2048
1203 +#define TEMPDIR "/tmp"
1205 +static int global_upload_size = 0;
1206 +static int ReadMimeEncodedInput(char *qs);
1207 +static var_handler do_putvar = NULL;
1210 + * Convert 2 char hex string into char it represents
1211 + * (from http://www.jmarshall.com/easy/cgi)
1213 +static char x2c (char *what) {
1216 + digit=(what[0] >= 'A' ? ((what[0] & 0xdf) - 'A')+10 : (what[0] - '0'));
1218 + digit+=(what[1] >= 'A' ? ((what[1] & 0xdf) - 'A')+10 : (what[1] - '0'));
1224 + * unsescape %xx to the characters they represent
1227 +static void unescape_url (char *url) {
1230 + for (i=0, j=0; url[j]; ++i, ++j) {
1231 + if ((url[i] = url[j]) == '%') {
1232 + url[i] = x2c(&url[j+1]);
1239 +static inline void put_var(vartype type, char *var)
1246 + val = strchr(var, '=');
1252 + do_putvar(type, var, val);
1259 + * if HTTP_COOKIE is passed as an environment variable,
1260 + * attempt to parse its values into environment variables
1262 +static void CookieVars (void)
1267 + if (getenv("HTTP_COOKIE") != NULL)
1268 + qs=strdup(getenv("HTTP_COOKIE"));
1272 + /** split on; to extract name value pairs */
1273 + token=strtok(qs, ";");
1275 + // skip leading spaces
1276 + while ( token[0] == ' ' )
1279 + put_var(COOKIE_VAR, token);
1281 + token = strtok(NULL, ";");
1287 + * Read cgi variables from query string, and put in environment
1289 +static int ReadCGIQueryString (void)
1295 + if (getenv("QUERY_STRING") != NULL)
1296 + qs=strdup(getenv("QUERY_STRING"));
1300 + /* change plusses into spaces */
1301 + for (i=0; qs[i]; i++ ) { if (qs[i] == '+' ) { qs[i] = ' ';} };
1303 + /** split on & and ; to extract name value pairs */
1305 + token=strtok(qs, "&;");
1307 + unescape_url(token);
1308 + put_var(FORM_VAR, token);
1309 + token=strtok(NULL, "&;");
1318 + * Read cgi variables from stdin (for POST queries)
1319 + * (oh... and if its mime-encoded file upload, we save the
1320 + * file to /tmp; and return the name of the tmp file
1321 + * the cgi script is responsible for disposing of the tmp file
1324 +static int ReadCGIPOSTValues (void) {
1326 + int content_length;
1331 + if (getenv("CONTENT_LENGTH") == NULL)
1334 + content_length = atoi(getenv("CONTENT_LENGTH"));
1336 + /* protect ourselves from 20GB file uploads */
1337 + if (content_length > MAX_UPLOAD_KB * 1024 ) {
1338 + /* But we need to finish reading the content */
1339 + while ( fread( &i, sizeof(int), 1, stdin) == 1 );
1343 + if (!(qs=malloc(content_length+1)))
1346 + /* set the buffer to null, so that a browser messing with less
1347 + data than content_length won't buffer underrun us */
1348 + memset(qs, 0 ,content_length+1);
1350 + if ((!fread(qs,content_length,1,stdin) &&
1351 + (content_length > 0)
1352 + && !feof(stdin))) {
1358 + if (getenv("CONTENT_TYPE") && (strncasecmp(getenv("CONTENT_TYPE"), "multipart/form-data", 19) == 0)) {
1359 + /* This is a mime request, we need to go to the mime handler */
1360 + i=ReadMimeEncodedInput(qs);
1366 + /* change plusses into spaces */
1367 + for (i=0; qs[i]; i++ ) { if (qs[i] == '+' ) { qs[i] = ' ';} };
1369 + /** split on & and ; to extract name value pairs */
1370 + token=strtok(qs, "&;");
1372 + unescape_url(token);
1373 + put_var(FORM_VAR, token);
1374 + token=strtok(NULL, "&;");
1383 + * LineToStr - Scans char and replaces the first "\n" with a "\0";
1384 + * If it finds a "\r", it does that to; (fix DOS brokennes) returns
1385 + * the length of the string;
1387 +static int LineToStr (char *string, size_t max) {
1390 + while ((offset < max) && (string[offset] != '\n') && (string[offset] != '\r'))
1393 + if (string[offset] == '\r') {
1394 + string[offset]='\0';
1397 + if (string[offset] == '\n') {
1398 + string[offset]='\0';
1407 + * ReadMimeEncodedInput - handles things that are mime encoded
1408 + * takes a pointer to the input; returns 0 on success
1411 +static int ReadMimeEncodedInput(char *qs)
1423 + char tmpname[] = TEMPDIR "/XXXXXX";
1425 + /* we should only get here if the content type was set. Segfaults happen
1426 + if Content_Type is null */
1428 + if (getenv("CONTENT_LENGTH") == NULL)
1429 + /* No content length?! */
1432 + cl=atoi(getenv("CONTENT_LENGTH"));
1434 + /* we do this 'cause we can't mess with the real env. variable - it would
1435 + * overwrite the environment - I tried.
1437 + i=strlen(getenv("CONTENT_TYPE"))+1;
1440 + memcpy(ct, getenv("CONTENT_TYPE"), i);
1446 + while (i < strlen(ct) && (strncmp("boundary=", &ct[i], 9) != 0))
1449 + if (i == strlen(ct)) {
1450 + /* no boundary informaiton found */
1454 + boundary=&ct[i+7];
1455 + /* add two leading -- to the boundary */
1459 + /* begin the big loop. Look for:
1461 + Content-Disposition: form-data; name="......."
1466 + Content-Disposition: form-data; name="....." filename="....."
1474 + while (offset < cl) {
1475 + /* first look for boundary */
1476 + while ((offset < cl) && (memcmp(&qs[offset], boundary, strlen(boundary))))
1479 + /* if we got here and we ran off the end, its an error */
1480 + if (offset >= cl) {
1485 + /* if the two characters following the boundary are --, */
1486 + /* then we are at the end, exit */
1487 + if (memcmp(&qs[offset+strlen(boundary)], "--", 2) == 0) {
1491 + /* find where the offset should be */
1492 + line=LineToStr(&qs[offset], cl-offset);
1495 + /* Now we're going to look for content-disposition */
1496 + line=LineToStr(&qs[offset], cl-offset);
1497 + if (strncasecmp(&qs[offset], "Content-Disposition", 19) != 0) {
1498 + /* hmm... content disposition was not where we expected it */
1502 + /* Found it, so let's go find "name=" */
1503 + if (!(envname=strstr(&qs[offset], "name="))) {
1504 + /* now name= is missing?! */
1510 + /* is there a filename tag? */
1511 + if ((filename=strstr(&qs[offset], "filename="))!= NULL)
1516 + /* make envname and filename ASCIIZ */
1517 + for (i=0; (envname[i] != '"') && (envname[i] != '\0'); i++);
1519 + envname[i] = '\0';
1521 + for (i=0; (filename[i] != '"') && (filename[i] != '\0'); i++);
1522 + filename[i] = '\0';
1526 + /* Ok, by some miracle, we have the name; let's skip till we */
1527 + /* come to a blank line */
1528 + line=LineToStr(&qs[offset], cl-offset);
1529 + while (strlen(&qs[offset]) > 1) {
1531 + line=LineToStr(&qs[offset], cl-offset);
1535 + /* And we go back to looking for a boundary */
1536 + while ((offset < cl) && (memcmp(&qs[offset], boundary, strlen(boundary))))
1539 + /* strip [cr] lf */
1540 + if ((qs[offset-1] == '\n') && (qs[offset-2] == '\r'))
1547 + /* ok, at this point, we know where the name is, and we know */
1548 + /* where the content is... we have to do one of two things */
1549 + /* based on whether its a file or not */
1550 + if (filename==NULL) { /* its not a file, so its easy */
1551 + /* just jam the content after the name */
1552 + memcpy(&envname[strlen(envname)+1], &qs[datastart], offset-datastart+1);
1553 + envname[strlen(envname)]='=';
1554 + put_var(FORM_VAR, envname);
1555 + } else { /* handle the fileupload case */
1556 + if (offset-datastart) { /* only if they uploaded */
1557 + if ( global_upload_size == 0 ) {
1560 + /* stuff in the filename */
1561 + ptr= calloc ( sizeof (char), strlen(envname)+strlen(filename)+2+5 );
1562 + sprintf (ptr, "%s_name=%s", envname, filename);
1563 + put_var(FORM_VAR, ptr);
1566 + fd=mkstemp(tmpname);
1571 + write(fd, &qs[datastart], offset-datastart);
1573 + ptr= calloc (sizeof(char), strlen(envname)+strlen(tmpname)+2);
1574 + sprintf (ptr, "%s=%s", envname, tmpname);
1575 + put_var(FORM_VAR, ptr);
1585 +/*-------------------------------------------------------------------------
1589 + *------------------------------------------------------------------------*/
1591 +int cgi_init(var_handler putvar_handler)
1595 + do_putvar = putvar_handler;
1597 + /* Read the current environment into our chain */
1599 + if (getenv("REQUEST_METHOD")) {
1600 + if (strcasecmp(getenv("REQUEST_METHOD"), "GET") == 0)
1601 + retval = ReadCGIQueryString();
1603 + if (strcasecmp(getenv("REQUEST_METHOD"), "POST") == 0)
1604 + retval = ReadCGIPOSTValues();
1609 Index: busybox-1.4.2/libbb/Kbuild
1610 ===================================================================
1611 --- busybox-1.4.2.orig/libbb/Kbuild 2007-06-04 13:21:37.710243840 +0200
1612 +++ busybox-1.4.2/libbb/Kbuild 2007-06-04 13:21:37.930210400 +0200
1614 lib-$(CONFIG_MDEV) += xregcomp.o
1615 lib-$(CONFIG_LESS) += xregcomp.o
1616 lib-$(CONFIG_DEVFSD) += xregcomp.o
1618 +lib-$(CONFIG_AWX) += cgi.o