1 diff -urN 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 */
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;
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);
44 if (next_token(TC_COMMA | TC_SEQTERM) & TC_SEQTERM)
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 @@
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 @@
80 +static int is_awx = 0;
84 int awk_main(int argc, char **argv)
87 @@ -2693,6 +2714,10 @@
96 while((c = getopt(argc, argv, "F:v:f:W:")) != EOF) {
98 diff -urN 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-23 19:13:40.459655704 +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 = NULL, *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);
180 + len += strlen((tok[_pos++] = l));
181 + len += strlen((tok[_pos++] = translate_lookup(p + strlen(TR_START))));
184 + l += strlen(TR_END);
188 + p = xmalloc(len + 1);
191 + for (i = 0; i < _pos; i++) {
193 + p += strlen(tok[i]);
195 + if (tr_abort && p2) {
204 +/* hook for intercepting awk's use of puts. used for running all printed strings
205 + * through the translation system */
206 +static inline int fputs_hook (__const char *__restrict __s, FILE *__restrict __stream)
208 + if (lang_inuse && (__stream == stdout)) {
212 + str = translate_line((char *) __s);
213 + ret = fputs(str, __stream);
219 + return fputs(__s, __stream);
222 +static var *init_lang(var *res, var *args, int nargs)
225 + lstr = hash_init();
232 +/* load and parse language file */
233 +static void load_lang_file(char *file)
236 + char *b, *name, *value;
237 + char buf1[LINE_BUF];
239 + if ((f = fopen(file, "r")) == NULL)
242 + while (!feof(f) && (fgets(buf1, LINE_BUF - 1, f) != NULL)) {
245 + continue; /* skip comments */
247 + while (isspace(*b))
248 + b++; /* skip leading spaces */
253 + if ((b = strstr(name, "=>")) == NULL)
254 + continue; /* separator not found */
261 + for (b--; isspace(*b); b--)
262 + *b = 0; /* remove trailing spaces */
264 + while (isspace(*value))
265 + value++; /* skip leading spaces */
267 + for (b = value + strlen(value) - 1; isspace(*b); b--)
268 + *b = 0; /* remove trailing spaces */
273 + setvar_s(findvar(lstr,name), value);
279 +static var *load_lang(var *res, var *args, int nargs)
281 + const char *langfmt = "/usr/lib/webif/lang/%s.txt";
282 + char lbuf[LINE_BUF];
286 + init_lang(res, args, nargs);
288 + lang = getvar_s(args);
289 + if (!lang || !strcmp(lang, ""))
292 + sprintf(lbuf, langfmt, lang);
293 + load_lang_file(lbuf);
298 +/* read the contents of an entire file */
299 +static char *get_file(char *fname)
305 + F = fopen(fname, "r");
310 + if (fseek(F, 0, SEEK_END) == 0) {
312 + s = (char *)xmalloc(flen+4);
313 + fseek(F, 0, SEEK_SET);
314 + i = 1 + fread(s+1, 1, flen, F);
316 + for (i=j=1; j>0; i+=j) {
317 + s = (char *)xrealloc(s, i+4096);
318 + j = fread(s+i, 1, 4094, F);
330 + * taken from parse_program from awk.c
331 + * END{} is not parsed here, and BEGIN{} is executed immediately
333 +static void parse_include(char *p)
336 + chain *initseq = NULL;
342 + memset(&tmp, 0, sizeof(tmp));
345 + while ((tclass = next_token(TC_EOF | TC_OPSEQ |
346 + TC_OPTERM | TC_BEGIN | TC_FUNCDECL)) != TC_EOF) {
347 + if (tclass & TC_OPTERM)
351 + if (tclass & TC_BEGIN) {
352 + initseq = xzalloc(sizeof(chain));
355 + } else if (tclass & TC_FUNCDECL) {
356 + next_token(TC_FUNCTION);
358 + f = newfunc(t.string);
360 + f->x.body.first = NULL;
362 + while (next_token(TC_VARIABLE | TC_SEQTERM) & TC_VARIABLE) {
363 + v = findvar(ahash, t.string);
364 + v->x.aidx = (f->nargs)++;
366 + if (next_token(TC_COMMA | TC_SEQTERM) & TC_SEQTERM)
369 + seq = &(f->x.body);
371 + clear_array(ahash);
374 + if (initseq && initseq->first)
375 + tv = evaluate(initseq->first, tv);
380 +/* include an awk file and run its BEGIN{} section */
381 +static xhash *includes = NULL;
382 +static void include_file(char *filename)
388 + includes = hash_init();
390 + /* find out if the file has been included already */
391 + v = findvar(includes, filename);
396 + /* read include file */
397 + s = get_file(filename);
399 + fprintf(stderr, "Could not open file.\n");
402 + parse_include(s+1);
406 +static var *include(var *res, var *args, int nargs)
410 + s = getvar_s(args);
411 + if (s && (strlen(s) > 0))
417 +/* parse an awk expression */
418 +static var *parse_awk(char *str, var *tv)
423 + memset(&body, 0, sizeof(body));
427 + /* end of expression, assume that there's going to be a free byte
428 + * at the end of the string that can be used for the ')' */
429 + strcat(str + strlen(str), "}");
430 + n = parse_expr(TC_GRPTERM);
434 + return evaluate(n, tv);
437 +static inline void print_translate(char *s)
441 + str = translate_line(s);
442 + fputs(str, stdout);
448 +static void render_element(struct template_cb *tcb, struct template_element *e)
459 + s = malloc(strlen(e->var) + 2);
461 + print_translate(s);
465 + s = malloc(strlen(e->var) + 2);
468 + s2 = strdup(getvar_s(parse_awk(s, v)));
470 + print_translate(s2);
475 + s = malloc(strlen(e->var) + 2);
478 + i = istrue(parse_awk(s, v));
483 + execute_template(tcb, e->sub);
485 + execute_template(tcb, e->sub2);
488 + v = newvar(e->var);
489 + hashwalk_init(v, iamarray(findvar(vhash, e->in)));
490 + while (hashwalk_next(v)) {
491 + execute_template(tcb, e->sub);
501 +/* awk method render(), which opens a template file and processes all awk ssi calls */
502 +static void render_file(char *filename)
504 + struct template_cb tcb;
505 + struct template_element *e;
514 + oldprg = programname;
515 + programname = filename;
517 + f = fopen(filename, "r");
521 + memset(&tcb, 0, sizeof(tcb));
522 + tcb.handle_element = render_element;
523 + e = parse_template(&tcb, f);
524 + execute_template(&tcb, e);
525 + free_template(&tcb, e);
527 + programname = oldprg;
531 +static var *render(var *res, var *args, int nargs)
535 + s = getvar_s(args);
544 +/* Call render, but only if this function hasn't been called already */
545 +static int layout_rendered = 0;
546 +static var *render_layout(var *res, var *args, int nargs)
548 + if (layout_rendered)
550 + layout_rendered = 1;
551 + return render(res, args, nargs);
554 +/* registers a global c function for the awk interpreter */
555 +static void register_cfunc(char *name, awk_cfunc cfunc, int nargs)
561 + f->x.cfunc = cfunc;
565 +static void putvar(vartype type, char *name, char *value)
567 + if (type != FORM_VAR)
570 + setvar_u(findvar(formvar, name), value);
573 +static char *cgi_getvar(char *name)
576 + formvar = hash_init();
580 + if (!formvar || !name)
583 + return getvar_s(findvar(formvar, name));
586 +/* function call for accessing cgi form variables */
587 +static var *getvar(var *res, var *args, int nargs)
592 + s = getvar_s(args);
596 + svar = cgi_getvar(s);
600 + setvar_u(res, svar);
605 +/* call an awk function without arguments by string reference */
606 +static var *call(var *res, var *args, int nargs)
608 + char *s = getvar_s(args);
615 + if (f && f->type == AWKFUNC && f->x.body.first)
616 + return evaluate(f->x.body.first, res);
623 +static int run_awxscript(char *name)
625 + var tv, *layout, *action;
626 + char *tmp, *s = NULL;
629 + programname = name;
631 + /* read the main controller source */
632 + s = get_file(programname);
634 + fprintf(stderr, "Could not open file\n");
637 + parse_program(s+1);
641 + /* set some defaults for ACTION and LAYOUT, which will have special meaning */
642 + layout = newvar("LAYOUT");
643 + setvar_s(layout, "views/layout.ahtml");
645 + /* run the BEGIN {} block */
646 + evaluate(beginseq.first, &tv);
648 + action = newvar("ACTION");
649 + if (!(strlen(getvar_s(action)) > 0)) {
650 + tmp = cgi_getvar("action");
651 + if (!tmp || (strlen(tmp) <= 0))
652 + tmp = strdup("default");
654 + setvar_p(action, tmp);
657 + /* call the action (precedence: begin block override > cgi parameter > "default") */
658 + tmp = xmalloc(strlen(getvar_s(action)) + 7);
659 + sprintf(tmp, "handle_%s", getvar_s(action));
660 + setvar_s(action, tmp);
661 + call(&tv, action, 1);
664 + /* render the selected layout, will do nothing if render_layout has been called from awk */
665 + render_layout(&tv, layout, 1);
671 +/* main awx processing function. called from awk_main() */
672 +static int do_awx(int argc, char **argv)
677 + char **args = argv;
681 + /* register awk C callbacks */
682 + register_cfunc("getvar", getvar, 1);
683 + register_cfunc("render", render, 1);
684 + register_cfunc("render_layout", render_layout, 1);
685 + register_cfunc("call", call, 1);
686 + register_cfunc("include", include, 1);
687 + register_cfunc("init_lang", init_lang, 1);
688 + register_cfunc("load_lang", load_lang, 1);
693 + /* fill in ARGV array */
694 + setvar_i(V[ARGC], argc + 1);
695 + setari_u(V[ARGV], 0, "awx");
698 + setari_u(V[ARGV], ++i, *args++);
700 + while((c = getopt(argc, argv, "i:f:")) != EOF) {
703 + programname = optarg;
704 + include_file(optarg);
708 + programname = optarg;
709 + render_file(optarg);
717 + fprintf(stderr, "Invalid argument.\n");
721 + ret = run_awxscript(*argv);
727 +/* entry point for awx applet */
728 +int awx_main(int argc, char **argv)
731 + return awk_main(argc, argv);
734 diff -urN bb.old/editors/awx_parser.h bb.dev/editors/awx_parser.h
735 --- bb.old/editors/awx_parser.h 1970-01-01 01:00:00.000000000 +0100
736 +++ bb.dev/editors/awx_parser.h 2007-05-20 22:30:31.380512280 +0200
738 +#ifndef __TEMPLATE_PARSER_H
739 +#define __TEMPLATE_PARSER_H
748 +struct template_element;
751 +struct template_cb {
752 + void *(*prepare_code)(struct template_element *);
753 + void (*handle_element)(struct template_cb *, struct template_element *);
754 + void (*free_code)(struct template_element *);
757 +struct template_element {
763 + struct template_element *parent;
764 + struct template_element *sub;
765 + struct template_element *sub2;
766 + struct template_element *prev;
767 + struct template_element *next;
771 +struct template_element *parse_template(struct template_cb *cb, FILE *in);
772 +void execute_template(struct template_cb *cb, struct template_element *e);
773 +void free_template(struct template_cb *cb, struct template_element *e);
776 diff -urN bb.old/editors/awx_parser.l bb.dev/editors/awx_parser.l
777 --- bb.old/editors/awx_parser.l 1970-01-01 01:00:00.000000000 +0100
778 +++ bb.dev/editors/awx_parser.l 2007-05-23 19:13:40.459655704 +0200
784 +#include "busybox.h"
785 +#include "awx_parser.h"
802 +char *statestr[] = {
803 + [S_INIT] = "S_INIT",
804 + [S_TEXT] = "S_TEXT",
805 + [S_CODE] = "S_CODE",
806 + [S_IF_START] = "S_IF_START",
807 + [S_FOR_START] = "S_FOR_START",
808 + [S_FOR_IN] = "S_FOR_IN",
813 + [T_TEXT] = "T_TEXT",
816 + [T_CODE] = "T_CODE"
820 +static struct template_cb *parse_cb;
821 +static struct template_element *cur, *head;
822 +static char *textbuf;
823 +static unsigned int buflen;
824 +static unsigned int buf_offset;
825 +static int _lnr = 0;
827 +static void buf_realloc(void)
830 + textbuf = xrealloc(textbuf, buflen);
833 +static void parse_error(char *str)
835 + fprintf(stderr, "Parse error%s%s\n", (str ? ": " : "!"), (str ?: ""));
840 +static struct template_element *new_template_element(struct template_element *parent)
842 + struct template_element *ptr;
844 + ptr = xzalloc(sizeof(struct template_element));
845 + ptr->parent = parent;
849 +static inline void next_template_element(void)
851 + cur->next = new_template_element(cur->parent);
852 + cur->next->prev = cur;
856 +static void addtext(char *text)
858 + while(buf_offset + strlen(text) + 1 > buflen)
861 + buf_offset += sprintf(&textbuf[buf_offset], "%s", text);
864 +static void set_state(int newstate)
869 + static int _rec = 0;
870 + fprintf(stderr, "DEBUG(%d): %s => %s: %s\n", _rec, statestr[state], statestr[newstate], textbuf);
872 + ptr = xstrdup(textbuf);
873 + if (state == S_FOR_IN)
878 + if (parse_cb && (cur->t == T_CODE) && parse_cb->prepare_code)
879 + parse_cb->prepare_code(cur);
892 + if (ptr || !cur->prev)
893 + next_template_element();
897 + if (ptr || !cur->prev)
898 + next_template_element();
904 + parse_error("'@else' without parent element");
905 + cur->sub2 = new_template_element(cur);
915 + parse_error("'@end' without parent element");
917 + next_template_element();
924 + next_template_element();
931 + cur->sub = new_template_element(cur);
940 + if (ptr || !cur->prev)
941 + next_template_element();
954 +"<%"[ \n\t]*"@if"[ \n\t]+ {
955 + if (state == S_TEXT)
956 + set_state(S_IF_START);
961 +"<%"[ \n\t]*"@for"[ \n\t]+ {
962 + if (state == S_TEXT)
963 + set_state(S_FOR_START);
968 +[ \n\t]+"in"[ \n\t]+ {
969 + if (state == S_FOR_START)
970 + set_state(S_FOR_IN);
975 +"<%"[ \n\t]*"@end"[ \n\t]*%> {
976 + if (state != S_TEXT)
981 +"<%"[ \n\t]*"@else"[ \n\t]*%> {
982 + if (state != S_TEXT)
988 + if (state != S_TEXT)
989 + parse_error("'<%' cannot be nested");
994 + if (state == S_TEXT)
1001 + if (state == S_TEXT)
1012 +void execute_template(struct template_cb *cb, struct template_element *e)
1014 + static int rec = 0;
1018 + fprintf(stderr, "DEBUG: execute(%d)\t%s\n", rec, typestr[e->t]);
1021 + if (cb->handle_element)
1022 + cb->handle_element(cb, e);
1034 +struct template_element *parse_template(struct template_cb *cb, FILE *in)
1042 + textbuf = xzalloc(buflen);
1044 + head = xzalloc(sizeof(struct template_element));
1054 +void free_template(struct template_cb *cb, struct template_element *e)
1056 + struct template_element *next;
1063 + if (cb->free_code)
1068 + free_template(cb, e->sub);
1080 + return free_template(cb, next);
1082 diff -urN bb.old/editors/Config.in bb.dev/editors/Config.in
1083 --- bb.old/editors/Config.in 2007-05-20 04:17:05.003197632 +0200
1084 +++ bb.dev/editors/Config.in 2007-05-20 22:30:31.380512280 +0200
1086 Awk is used as a pattern scanning and processing language. This is
1087 the BusyBox implementation of that programming language.
1090 + bool "Enable awx (awk web extension)"
1094 + awx - awk web extension
1096 config FEATURE_AWK_MATH
1097 bool "Enable math functions (requires libm)"
1099 diff -urN bb.old/editors/Kbuild bb.dev/editors/Kbuild
1100 --- bb.old/editors/Kbuild 2007-03-18 17:59:37.000000000 +0100
1101 +++ bb.dev/editors/Kbuild 2007-05-20 22:30:31.381512128 +0200
1103 lib-$(CONFIG_PATCH) += patch.o
1104 lib-$(CONFIG_SED) += sed.o
1105 lib-$(CONFIG_VI) += vi.o
1106 +lib-$(CONFIG_AWX) += awx_parser.o
1108 +editors/awx_parser.c: editors/awx_parser.l editors/awx_parser.h
1112 +editors/awx_parser.o: editors/awx_parser.c FORCE
1113 + $(call cmd,force_checksrc)
1114 + $(call if_changed_rule,cc_o_c)
1115 diff -urN bb.old/include/applets.h bb.dev/include/applets.h
1116 --- bb.old/include/applets.h 2007-05-20 04:17:05.003197632 +0200
1117 +++ bb.dev/include/applets.h 2007-05-20 22:30:31.381512128 +0200
1119 USE_ARPING(APPLET(arping, _BB_DIR_USR_BIN, _BB_SUID_NEVER))
1120 USE_ASH(APPLET_NOUSAGE(ash, ash, _BB_DIR_BIN, _BB_SUID_NEVER))
1121 USE_AWK(APPLET(awk, _BB_DIR_USR_BIN, _BB_SUID_NEVER))
1122 +USE_AWX(APPLET_NOUSAGE(awx, awx, _BB_DIR_USR_BIN, _BB_SUID_NEVER))
1123 USE_BASENAME(APPLET(basename, _BB_DIR_USR_BIN, _BB_SUID_NEVER))
1124 USE_BBCONFIG(APPLET(bbconfig, _BB_DIR_BIN, _BB_SUID_NEVER))
1125 //USE_BBSH(APPLET(bbsh, _BB_DIR_BIN, _BB_SUID_NEVER))
1126 diff -urN bb.old/include/cgi.h bb.dev/include/cgi.h
1127 --- bb.old/include/cgi.h 1970-01-01 01:00:00.000000000 +0100
1128 +++ bb.dev/include/cgi.h 2007-05-20 22:30:31.381512128 +0200
1133 +typedef enum { FORM_VAR, COOKIE_VAR } vartype;
1134 +typedef void (*var_handler) (vartype, char *, char *);
1135 +int cgi_init(var_handler);
1138 diff -urN bb.old/libbb/cgi.c bb.dev/libbb/cgi.c
1139 --- bb.old/libbb/cgi.c 1970-01-01 01:00:00.000000000 +0100
1140 +++ bb.dev/libbb/cgi.c 2007-05-20 22:30:31.382511976 +0200
1142 +/* --------------------------------------------------------------------------
1143 + * functions for processing cgi form data
1144 + * Copyright (C) 2007 by Felix Fietkau <nbd@openwrt.org>
1146 + * parts taken from the core of haserl.cgi - a poor-man's php for embedded/lightweight environments
1147 + * $Id: haserl.c,v 1.13 2004/11/10 17:59:35 nangel Exp $
1148 + * Copyright (c) 2003,2004 Nathan Angelacos (nangel@users.sourceforge.net)
1150 + * This program is free software; you can redistribute it and/or modify
1151 + * it under the terms of the GNU General Public License as published by
1152 + * the Free Software Foundation; either version 2 of the License, or
1153 + * (at your option) any later version.
1155 + * This program is distributed in the hope that it will be useful,
1156 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1157 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1158 + * General Public License for more details.
1160 + * You should have received a copy of the GNU General Public License
1161 + * along with this program; if not, write to the Free Software
1162 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1165 + * The x2c() and unescape_url() routines were taken from
1166 + * http://www.jmarshall.com/easy/cgi/getcgi.c.txt
1168 + * The comments in that text file state:
1170 + *** Written in 1996 by James Marshall, james@jmarshall.com, except
1171 + *** that the x2c() and unescape_url() routines were lifted directly
1172 + *** from NCSA's sample program util.c, packaged with their HTTPD.
1173 + *** For the latest, see http://www.jmarshall.com/easy/cgi/
1176 + ------------------------------------------------------------------------- */
1179 +#include <unistd.h>
1181 +#include <sys/mman.h>
1182 +#include <sys/types.h>
1183 +#include <sys/wait.h>
1184 +#include <sys/stat.h>
1185 +#include <sys/fcntl.h>
1186 +#include <stdlib.h>
1187 +#include <string.h>
1190 +#ifndef MAX_UPLOAD_KB
1191 +#define MAX_UPLOAD_KB 2048
1193 +#define TEMPDIR "/tmp"
1195 +static int global_upload_size = 0;
1196 +static int ReadMimeEncodedInput(char *qs);
1197 +static var_handler do_putvar = NULL;
1200 + * Convert 2 char hex string into char it represents
1201 + * (from http://www.jmarshall.com/easy/cgi)
1203 +static char x2c (char *what) {
1206 + digit=(what[0] >= 'A' ? ((what[0] & 0xdf) - 'A')+10 : (what[0] - '0'));
1208 + digit+=(what[1] >= 'A' ? ((what[1] & 0xdf) - 'A')+10 : (what[1] - '0'));
1214 + * unsescape %xx to the characters they represent
1217 +static void unescape_url (char *url) {
1220 + for (i=0, j=0; url[j]; ++i, ++j) {
1221 + if ((url[i] = url[j]) == '%') {
1222 + url[i] = x2c(&url[j+1]);
1229 +static inline void put_var(vartype type, char *var)
1236 + val = strchr(var, '=');
1242 + do_putvar(type, var, val);
1249 + * if HTTP_COOKIE is passed as an environment variable,
1250 + * attempt to parse its values into environment variables
1252 +static void CookieVars (void)
1257 + if (getenv("HTTP_COOKIE") != NULL)
1258 + qs=strdup(getenv("HTTP_COOKIE"));
1262 + /** split on; to extract name value pairs */
1263 + token=strtok(qs, ";");
1265 + // skip leading spaces
1266 + while ( token[0] == ' ' )
1269 + put_var(COOKIE_VAR, token);
1271 + token = strtok(NULL, ";");
1277 + * Read cgi variables from query string, and put in environment
1279 +static int ReadCGIQueryString (void)
1285 + if (getenv("QUERY_STRING") != NULL)
1286 + qs=strdup(getenv("QUERY_STRING"));
1290 + /* change plusses into spaces */
1291 + for (i=0; qs[i]; i++ ) { if (qs[i] == '+' ) { qs[i] = ' ';} };
1293 + /** split on & and ; to extract name value pairs */
1295 + token=strtok(qs, "&;");
1297 + unescape_url(token);
1298 + put_var(FORM_VAR, token);
1299 + token=strtok(NULL, "&;");
1308 + * Read cgi variables from stdin (for POST queries)
1309 + * (oh... and if its mime-encoded file upload, we save the
1310 + * file to /tmp; and return the name of the tmp file
1311 + * the cgi script is responsible for disposing of the tmp file
1314 +static int ReadCGIPOSTValues (void) {
1316 + int content_length;
1321 + if (getenv("CONTENT_LENGTH") == NULL)
1324 + content_length = atoi(getenv("CONTENT_LENGTH"));
1326 + /* protect ourselves from 20GB file uploads */
1327 + if (content_length > MAX_UPLOAD_KB * 1024 ) {
1328 + /* But we need to finish reading the content */
1329 + while ( fread( &i, sizeof(int), 1, stdin) == 1 );
1333 + if (!(qs=malloc(content_length+1)))
1336 + /* set the buffer to null, so that a browser messing with less
1337 + data than content_length won't buffer underrun us */
1338 + memset(qs, 0 ,content_length+1);
1340 + if ((!fread(qs,content_length,1,stdin) &&
1341 + (content_length > 0)
1342 + && !feof(stdin))) {
1348 + if (getenv("CONTENT_TYPE") && (strncasecmp(getenv("CONTENT_TYPE"), "multipart/form-data", 19) == 0)) {
1349 + /* This is a mime request, we need to go to the mime handler */
1350 + i=ReadMimeEncodedInput(qs);
1356 + /* change plusses into spaces */
1357 + for (i=0; qs[i]; i++ ) { if (qs[i] == '+' ) { qs[i] = ' ';} };
1359 + /** split on & and ; to extract name value pairs */
1360 + token=strtok(qs, "&;");
1362 + unescape_url(token);
1363 + put_var(FORM_VAR, token);
1364 + token=strtok(NULL, "&;");
1373 + * LineToStr - Scans char and replaces the first "\n" with a "\0";
1374 + * If it finds a "\r", it does that to; (fix DOS brokennes) returns
1375 + * the length of the string;
1377 +static int LineToStr (char *string, size_t max) {
1380 + while ((offset < max) && (string[offset] != '\n') && (string[offset] != '\r'))
1383 + if (string[offset] == '\r') {
1384 + string[offset]='\0';
1387 + if (string[offset] == '\n') {
1388 + string[offset]='\0';
1397 + * ReadMimeEncodedInput - handles things that are mime encoded
1398 + * takes a pointer to the input; returns 0 on success
1401 +static int ReadMimeEncodedInput(char *qs)
1413 + char tmpname[] = TEMPDIR "/XXXXXX";
1415 + /* we should only get here if the content type was set. Segfaults happen
1416 + if Content_Type is null */
1418 + if (getenv("CONTENT_LENGTH") == NULL)
1419 + /* No content length?! */
1422 + cl=atoi(getenv("CONTENT_LENGTH"));
1424 + /* we do this 'cause we can't mess with the real env. variable - it would
1425 + * overwrite the environment - I tried.
1427 + i=strlen(getenv("CONTENT_TYPE"))+1;
1430 + memcpy(ct, getenv("CONTENT_TYPE"), i);
1436 + while (i < strlen(ct) && (strncmp("boundary=", &ct[i], 9) != 0))
1439 + if (i == strlen(ct)) {
1440 + /* no boundary informaiton found */
1444 + boundary=&ct[i+7];
1445 + /* add two leading -- to the boundary */
1449 + /* begin the big loop. Look for:
1451 + Content-Disposition: form-data; name="......."
1456 + Content-Disposition: form-data; name="....." filename="....."
1464 + while (offset < cl) {
1465 + /* first look for boundary */
1466 + while ((offset < cl) && (memcmp(&qs[offset], boundary, strlen(boundary))))
1469 + /* if we got here and we ran off the end, its an error */
1470 + if (offset >= cl) {
1475 + /* if the two characters following the boundary are --, */
1476 + /* then we are at the end, exit */
1477 + if (memcmp(&qs[offset+strlen(boundary)], "--", 2) == 0) {
1481 + /* find where the offset should be */
1482 + line=LineToStr(&qs[offset], cl-offset);
1485 + /* Now we're going to look for content-disposition */
1486 + line=LineToStr(&qs[offset], cl-offset);
1487 + if (strncasecmp(&qs[offset], "Content-Disposition", 19) != 0) {
1488 + /* hmm... content disposition was not where we expected it */
1492 + /* Found it, so let's go find "name=" */
1493 + if (!(envname=strstr(&qs[offset], "name="))) {
1494 + /* now name= is missing?! */
1500 + /* is there a filename tag? */
1501 + if ((filename=strstr(&qs[offset], "filename="))!= NULL)
1506 + /* make envname and filename ASCIIZ */
1507 + for (i=0; (envname[i] != '"') && (envname[i] != '\0'); i++);
1509 + envname[i] = '\0';
1511 + for (i=0; (filename[i] != '"') && (filename[i] != '\0'); i++);
1512 + filename[i] = '\0';
1516 + /* Ok, by some miracle, we have the name; let's skip till we */
1517 + /* come to a blank line */
1518 + line=LineToStr(&qs[offset], cl-offset);
1519 + while (strlen(&qs[offset]) > 1) {
1521 + line=LineToStr(&qs[offset], cl-offset);
1525 + /* And we go back to looking for a boundary */
1526 + while ((offset < cl) && (memcmp(&qs[offset], boundary, strlen(boundary))))
1529 + /* strip [cr] lf */
1530 + if ((qs[offset-1] == '\n') && (qs[offset-2] == '\r'))
1537 + /* ok, at this point, we know where the name is, and we know */
1538 + /* where the content is... we have to do one of two things */
1539 + /* based on whether its a file or not */
1540 + if (filename==NULL) { /* its not a file, so its easy */
1541 + /* just jam the content after the name */
1542 + memcpy(&envname[strlen(envname)+1], &qs[datastart], offset-datastart+1);
1543 + envname[strlen(envname)]='=';
1544 + put_var(FORM_VAR, envname);
1545 + } else { /* handle the fileupload case */
1546 + if (offset-datastart) { /* only if they uploaded */
1547 + if ( global_upload_size == 0 ) {
1550 + /* stuff in the filename */
1551 + ptr= calloc ( sizeof (char), strlen(envname)+strlen(filename)+2+5 );
1552 + sprintf (ptr, "%s_name=%s", envname, filename);
1553 + put_var(FORM_VAR, ptr);
1556 + fd=mkstemp(tmpname);
1561 + write(fd, &qs[datastart], offset-datastart);
1563 + ptr= calloc (sizeof(char), strlen(envname)+strlen(tmpname)+2);
1564 + sprintf (ptr, "%s=%s", envname, tmpname);
1565 + put_var(FORM_VAR, ptr);
1575 +/*-------------------------------------------------------------------------
1579 + *------------------------------------------------------------------------*/
1581 +int cgi_init(var_handler putvar_handler)
1585 + do_putvar = putvar_handler;
1587 + /* Read the current environment into our chain */
1589 + if (getenv("REQUEST_METHOD")) {
1590 + if (strcasecmp(getenv("REQUEST_METHOD"), "GET") == 0)
1591 + retval = ReadCGIQueryString();
1593 + if (strcasecmp(getenv("REQUEST_METHOD"), "POST") == 0)
1594 + retval = ReadCGIPOSTValues();
1599 diff -urN bb.old/libbb/Kbuild bb.dev/libbb/Kbuild
1600 --- bb.old/libbb/Kbuild 2007-05-20 04:17:05.004197480 +0200
1601 +++ bb.dev/libbb/Kbuild 2007-05-20 22:30:31.382511976 +0200
1603 lib-$(CONFIG_MDEV) += xregcomp.o
1604 lib-$(CONFIG_LESS) += xregcomp.o
1605 lib-$(CONFIG_DEVFSD) += xregcomp.o
1607 +lib-$(CONFIG_AWX) += cgi.o