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 22:37:38.547183608 +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);
179 + 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]);
199 + if (tr_abort && p2)
205 +/* hook for intercepting awk's use of puts. used for running all printed strings
206 + * through the translation system */
207 +static inline int fputs_hook (__const char *__restrict __s, FILE *__restrict __stream)
209 + if (lang_inuse && (__stream == stdout)) {
213 + str = translate_line((char *) __s);
214 + ret = fputs(str, __stream);
220 + return fputs(__s, __stream);
223 +static var *init_lang(var *res, var *args, int nargs)
226 + lstr = hash_init();
233 +/* load and parse language file */
234 +static void load_lang_file(char *file)
237 + char *b, *name, *value;
238 + char buf1[LINE_BUF];
240 + if ((f = fopen(file, "r")) == NULL)
243 + while (!feof(f) && (fgets(buf1, LINE_BUF - 1, f) != NULL)) {
246 + continue; /* skip comments */
248 + while (isspace(*b))
249 + b++; /* skip leading spaces */
254 + if ((b = strstr(name, "=>")) == NULL)
255 + continue; /* separator not found */
262 + for (b--; isspace(*b); b--)
263 + *b = 0; /* remove trailing spaces */
265 + while (isspace(*value))
266 + value++; /* skip leading spaces */
268 + for (b = value + strlen(value) - 1; isspace(*b); b--)
269 + *b = 0; /* remove trailing spaces */
274 + setvar_s(findvar(lstr,name), value);
280 +static var *load_lang(var *res, var *args, int nargs)
282 + const char *langfmt = "/usr/lib/webif/lang/%s.txt";
283 + char lbuf[LINE_BUF];
287 + init_lang(res, args, nargs);
289 + lang = getvar_s(args);
290 + if (!lang || !strcmp(lang, ""))
293 + sprintf(lbuf, langfmt, lang);
294 + load_lang_file(lbuf);
299 +/* read the contents of an entire file */
300 +static char *get_file(char *fname)
306 + F = fopen(fname, "r");
311 + if (fseek(F, 0, SEEK_END) == 0) {
313 + s = (char *)xmalloc(flen+4);
314 + fseek(F, 0, SEEK_SET);
315 + i = 1 + fread(s+1, 1, flen, F);
317 + for (i=j=1; j>0; i+=j) {
318 + s = (char *)xrealloc(s, i+4096);
319 + j = fread(s+i, 1, 4094, F);
331 + * taken from parse_program from awk.c
332 + * END{} is not parsed here, and BEGIN{} is executed immediately
334 +static void parse_include(char *p)
337 + chain *initseq = NULL;
343 + memset(&tmp, 0, sizeof(tmp));
346 + while ((tclass = next_token(TC_EOF | TC_OPSEQ |
347 + TC_OPTERM | TC_BEGIN | TC_FUNCDECL)) != TC_EOF) {
348 + if (tclass & TC_OPTERM)
352 + if (tclass & TC_BEGIN) {
353 + initseq = xzalloc(sizeof(chain));
356 + } else if (tclass & TC_FUNCDECL) {
357 + next_token(TC_FUNCTION);
359 + f = newfunc(t.string);
361 + f->x.body.first = NULL;
363 + while (next_token(TC_VARIABLE | TC_SEQTERM) & TC_VARIABLE) {
364 + v = findvar(ahash, t.string);
365 + v->x.aidx = (f->nargs)++;
367 + if (next_token(TC_COMMA | TC_SEQTERM) & TC_SEQTERM)
370 + seq = &(f->x.body);
372 + clear_array(ahash);
375 + if (initseq && initseq->first)
376 + tv = evaluate(initseq->first, tv);
381 +/* include an awk file and run its BEGIN{} section */
382 +static xhash *includes = NULL;
383 +static void include_file(char *filename)
389 + includes = hash_init();
391 + /* find out if the file has been included already */
392 + v = findvar(includes, filename);
397 + /* read include file */
398 + s = get_file(filename);
400 + fprintf(stderr, "Could not open file.\n");
403 + parse_include(s+1);
407 +static var *include(var *res, var *args, int nargs)
411 + s = getvar_s(args);
412 + if (s && (strlen(s) > 0))
418 +/* parse an awk expression */
419 +static var *parse_awk(char *str, var *tv)
424 + memset(&body, 0, sizeof(body));
428 + /* end of expression, assume that there's going to be a free byte
429 + * at the end of the string that can be used for the ')' */
430 + strcat(str + strlen(str), "}");
431 + n = parse_expr(TC_GRPTERM);
435 + return evaluate(n, tv);
438 +static inline void print_translate(char *s)
442 + str = translate_line(s);
443 + fputs(str, stdout);
449 +static void render_element(struct template_cb *tcb, struct template_element *e)
460 + s = malloc(strlen(e->var) + 2);
462 + print_translate(s);
466 + s = malloc(strlen(e->var) + 2);
469 + s2 = strdup(getvar_s(parse_awk(s, v)));
471 + print_translate(s2);
476 + s = malloc(strlen(e->var) + 2);
479 + i = istrue(parse_awk(s, v));
484 + execute_template(tcb, e->sub);
486 + execute_template(tcb, e->sub2);
489 + v = newvar(e->var);
490 + hashwalk_init(v, iamarray(findvar(vhash, e->in)));
491 + while (hashwalk_next(v)) {
492 + execute_template(tcb, e->sub);
502 +/* awk method render(), which opens a template file and processes all awk ssi calls */
503 +static void render_file(char *filename)
505 + struct template_cb tcb;
506 + struct template_element *e;
515 + oldprg = programname;
516 + programname = filename;
518 + f = fopen(filename, "r");
522 + memset(&tcb, 0, sizeof(tcb));
523 + tcb.handle_element = render_element;
524 + e = parse_template(&tcb, f);
525 + execute_template(&tcb, e);
526 + free_template(&tcb, e);
528 + programname = oldprg;
532 +static var *render(var *res, var *args, int nargs)
536 + s = getvar_s(args);
545 +/* Call render, but only if this function hasn't been called already */
546 +static int layout_rendered = 0;
547 +static var *render_layout(var *res, var *args, int nargs)
549 + if (layout_rendered)
551 + layout_rendered = 1;
552 + return render(res, args, nargs);
555 +/* registers a global c function for the awk interpreter */
556 +static void register_cfunc(char *name, awk_cfunc cfunc, int nargs)
562 + f->x.cfunc = cfunc;
566 +static void putvar(vartype type, char *name, char *value)
568 + if (type != FORM_VAR)
571 + setvar_u(findvar(formvar, name), value);
574 +static char *cgi_getvar(char *name)
577 + formvar = hash_init();
581 + if (!formvar || !name)
584 + return getvar_s(findvar(formvar, name));
587 +/* function call for accessing cgi form variables */
588 +static var *getvar(var *res, var *args, int nargs)
593 + s = getvar_s(args);
597 + svar = cgi_getvar(s);
601 + setvar_u(res, svar);
606 +/* call an awk function without arguments by string reference */
607 +static var *call(var *res, var *args, int nargs)
609 + char *s = getvar_s(args);
616 + if (f && f->type == AWKFUNC && f->x.body.first)
617 + return evaluate(f->x.body.first, res);
624 +static int run_awxscript(char *name)
626 + var tv, *layout, *action;
627 + char *tmp, *s = NULL;
630 + programname = name;
632 + /* read the main controller source */
633 + s = get_file(programname);
635 + fprintf(stderr, "Could not open file\n");
638 + parse_program(s+1);
642 + /* set some defaults for ACTION and LAYOUT, which will have special meaning */
643 + layout = newvar("LAYOUT");
644 + setvar_s(layout, "views/layout.ahtml");
646 + /* run the BEGIN {} block */
647 + evaluate(beginseq.first, &tv);
649 + action = newvar("ACTION");
650 + if (!(strlen(getvar_s(action)) > 0)) {
651 + tmp = cgi_getvar("action");
652 + if (!tmp || (strlen(tmp) <= 0))
653 + tmp = strdup("default");
655 + setvar_p(action, tmp);
658 + /* call the action (precedence: begin block override > cgi parameter > "default") */
659 + tmp = xmalloc(strlen(getvar_s(action)) + 7);
660 + sprintf(tmp, "handle_%s", getvar_s(action));
661 + setvar_s(action, tmp);
662 + call(&tv, action, 1);
665 + /* render the selected layout, will do nothing if render_layout has been called from awk */
666 + render_layout(&tv, layout, 1);
672 +/* main awx processing function. called from awk_main() */
673 +static int do_awx(int argc, char **argv)
678 + char **args = argv;
682 + /* register awk C callbacks */
683 + register_cfunc("getvar", getvar, 1);
684 + register_cfunc("render", render, 1);
685 + register_cfunc("render_layout", render_layout, 1);
686 + register_cfunc("call", call, 1);
687 + register_cfunc("include", include, 1);
688 + register_cfunc("init_lang", init_lang, 1);
689 + register_cfunc("load_lang", load_lang, 1);
694 + /* fill in ARGV array */
695 + setvar_i(V[ARGC], argc + 1);
696 + setari_u(V[ARGV], 0, "awx");
699 + setari_u(V[ARGV], ++i, *args++);
701 + while((c = getopt(argc, argv, "i:f:")) != EOF) {
704 + programname = optarg;
705 + include_file(optarg);
709 + programname = optarg;
710 + render_file(optarg);
718 + fprintf(stderr, "Invalid argument.\n");
722 + ret = run_awxscript(*argv);
728 +/* entry point for awx applet */
729 +int awx_main(int argc, char **argv)
732 + return awk_main(argc, argv);
735 diff -urN bb.old/editors/awx_parser.h bb.dev/editors/awx_parser.h
736 --- bb.old/editors/awx_parser.h 1970-01-01 01:00:00.000000000 +0100
737 +++ bb.dev/editors/awx_parser.h 2007-05-20 22:30:31.380512280 +0200
739 +#ifndef __TEMPLATE_PARSER_H
740 +#define __TEMPLATE_PARSER_H
749 +struct template_element;
752 +struct template_cb {
753 + void *(*prepare_code)(struct template_element *);
754 + void (*handle_element)(struct template_cb *, struct template_element *);
755 + void (*free_code)(struct template_element *);
758 +struct template_element {
764 + struct template_element *parent;
765 + struct template_element *sub;
766 + struct template_element *sub2;
767 + struct template_element *prev;
768 + struct template_element *next;
772 +struct template_element *parse_template(struct template_cb *cb, FILE *in);
773 +void execute_template(struct template_cb *cb, struct template_element *e);
774 +void free_template(struct template_cb *cb, struct template_element *e);
777 diff -urN bb.old/editors/awx_parser.l bb.dev/editors/awx_parser.l
778 --- bb.old/editors/awx_parser.l 1970-01-01 01:00:00.000000000 +0100
779 +++ bb.dev/editors/awx_parser.l 2007-05-23 19:13:40.459655704 +0200
785 +#include "busybox.h"
786 +#include "awx_parser.h"
803 +char *statestr[] = {
804 + [S_INIT] = "S_INIT",
805 + [S_TEXT] = "S_TEXT",
806 + [S_CODE] = "S_CODE",
807 + [S_IF_START] = "S_IF_START",
808 + [S_FOR_START] = "S_FOR_START",
809 + [S_FOR_IN] = "S_FOR_IN",
814 + [T_TEXT] = "T_TEXT",
817 + [T_CODE] = "T_CODE"
821 +static struct template_cb *parse_cb;
822 +static struct template_element *cur, *head;
823 +static char *textbuf;
824 +static unsigned int buflen;
825 +static unsigned int buf_offset;
826 +static int _lnr = 0;
828 +static void buf_realloc(void)
831 + textbuf = xrealloc(textbuf, buflen);
834 +static void parse_error(char *str)
836 + fprintf(stderr, "Parse error%s%s\n", (str ? ": " : "!"), (str ?: ""));
841 +static struct template_element *new_template_element(struct template_element *parent)
843 + struct template_element *ptr;
845 + ptr = xzalloc(sizeof(struct template_element));
846 + ptr->parent = parent;
850 +static inline void next_template_element(void)
852 + cur->next = new_template_element(cur->parent);
853 + cur->next->prev = cur;
857 +static void addtext(char *text)
859 + while(buf_offset + strlen(text) + 1 > buflen)
862 + buf_offset += sprintf(&textbuf[buf_offset], "%s", text);
865 +static void set_state(int newstate)
870 + static int _rec = 0;
871 + fprintf(stderr, "DEBUG(%d): %s => %s: %s\n", _rec, statestr[state], statestr[newstate], textbuf);
873 + ptr = xstrdup(textbuf);
874 + if (state == S_FOR_IN)
879 + if (parse_cb && (cur->t == T_CODE) && parse_cb->prepare_code)
880 + parse_cb->prepare_code(cur);
893 + if (ptr || !cur->prev)
894 + next_template_element();
898 + if (ptr || !cur->prev)
899 + next_template_element();
905 + parse_error("'@else' without parent element");
906 + cur->sub2 = new_template_element(cur);
916 + parse_error("'@end' without parent element");
918 + next_template_element();
925 + next_template_element();
932 + cur->sub = new_template_element(cur);
941 + if (ptr || !cur->prev)
942 + next_template_element();
955 +"<%"[ \n\t]*"@if"[ \n\t]+ {
956 + if (state == S_TEXT)
957 + set_state(S_IF_START);
962 +"<%"[ \n\t]*"@for"[ \n\t]+ {
963 + if (state == S_TEXT)
964 + set_state(S_FOR_START);
969 +[ \n\t]+"in"[ \n\t]+ {
970 + if (state == S_FOR_START)
971 + set_state(S_FOR_IN);
976 +"<%"[ \n\t]*"@end"[ \n\t]*%> {
977 + if (state != S_TEXT)
982 +"<%"[ \n\t]*"@else"[ \n\t]*%> {
983 + if (state != S_TEXT)
989 + if (state != S_TEXT)
990 + parse_error("'<%' cannot be nested");
995 + if (state == S_TEXT)
1002 + if (state == S_TEXT)
1013 +void execute_template(struct template_cb *cb, struct template_element *e)
1015 + static int rec = 0;
1019 + fprintf(stderr, "DEBUG: execute(%d)\t%s\n", rec, typestr[e->t]);
1022 + if (cb->handle_element)
1023 + cb->handle_element(cb, e);
1035 +struct template_element *parse_template(struct template_cb *cb, FILE *in)
1043 + textbuf = xzalloc(buflen);
1045 + head = xzalloc(sizeof(struct template_element));
1055 +void free_template(struct template_cb *cb, struct template_element *e)
1057 + struct template_element *next;
1064 + if (cb->free_code)
1069 + free_template(cb, e->sub);
1081 + return free_template(cb, next);
1083 diff -urN bb.old/editors/Config.in bb.dev/editors/Config.in
1084 --- bb.old/editors/Config.in 2007-05-20 04:17:05.003197632 +0200
1085 +++ bb.dev/editors/Config.in 2007-05-20 22:30:31.380512280 +0200
1087 Awk is used as a pattern scanning and processing language. This is
1088 the BusyBox implementation of that programming language.
1091 + bool "Enable awx (awk web extension)"
1095 + awx - awk web extension
1097 config FEATURE_AWK_MATH
1098 bool "Enable math functions (requires libm)"
1100 diff -urN bb.old/editors/Kbuild bb.dev/editors/Kbuild
1101 --- bb.old/editors/Kbuild 2007-03-18 17:59:37.000000000 +0100
1102 +++ bb.dev/editors/Kbuild 2007-05-20 22:30:31.381512128 +0200
1104 lib-$(CONFIG_PATCH) += patch.o
1105 lib-$(CONFIG_SED) += sed.o
1106 lib-$(CONFIG_VI) += vi.o
1107 +lib-$(CONFIG_AWX) += awx_parser.o
1109 +editors/awx_parser.c: editors/awx_parser.l editors/awx_parser.h
1113 +editors/awx_parser.o: editors/awx_parser.c FORCE
1114 + $(call cmd,force_checksrc)
1115 + $(call if_changed_rule,cc_o_c)
1116 diff -urN bb.old/include/applets.h bb.dev/include/applets.h
1117 --- bb.old/include/applets.h 2007-05-20 04:17:05.003197632 +0200
1118 +++ bb.dev/include/applets.h 2007-05-20 22:30:31.381512128 +0200
1120 USE_ARPING(APPLET(arping, _BB_DIR_USR_BIN, _BB_SUID_NEVER))
1121 USE_ASH(APPLET_NOUSAGE(ash, ash, _BB_DIR_BIN, _BB_SUID_NEVER))
1122 USE_AWK(APPLET(awk, _BB_DIR_USR_BIN, _BB_SUID_NEVER))
1123 +USE_AWX(APPLET_NOUSAGE(awx, awx, _BB_DIR_USR_BIN, _BB_SUID_NEVER))
1124 USE_BASENAME(APPLET(basename, _BB_DIR_USR_BIN, _BB_SUID_NEVER))
1125 USE_BBCONFIG(APPLET(bbconfig, _BB_DIR_BIN, _BB_SUID_NEVER))
1126 //USE_BBSH(APPLET(bbsh, _BB_DIR_BIN, _BB_SUID_NEVER))
1127 diff -urN bb.old/include/cgi.h bb.dev/include/cgi.h
1128 --- bb.old/include/cgi.h 1970-01-01 01:00:00.000000000 +0100
1129 +++ bb.dev/include/cgi.h 2007-05-20 22:30:31.381512128 +0200
1134 +typedef enum { FORM_VAR, COOKIE_VAR } vartype;
1135 +typedef void (*var_handler) (vartype, char *, char *);
1136 +int cgi_init(var_handler);
1139 diff -urN bb.old/libbb/cgi.c bb.dev/libbb/cgi.c
1140 --- bb.old/libbb/cgi.c 1970-01-01 01:00:00.000000000 +0100
1141 +++ bb.dev/libbb/cgi.c 2007-05-20 22:30:31.382511976 +0200
1143 +/* --------------------------------------------------------------------------
1144 + * functions for processing cgi form data
1145 + * Copyright (C) 2007 by Felix Fietkau <nbd@openwrt.org>
1147 + * parts taken from the core of haserl.cgi - a poor-man's php for embedded/lightweight environments
1148 + * $Id: haserl.c,v 1.13 2004/11/10 17:59:35 nangel Exp $
1149 + * Copyright (c) 2003,2004 Nathan Angelacos (nangel@users.sourceforge.net)
1151 + * This program is free software; you can redistribute it and/or modify
1152 + * it under the terms of the GNU General Public License as published by
1153 + * the Free Software Foundation; either version 2 of the License, or
1154 + * (at your option) any later version.
1156 + * This program is distributed in the hope that it will be useful,
1157 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1158 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1159 + * General Public License for more details.
1161 + * You should have received a copy of the GNU General Public License
1162 + * along with this program; if not, write to the Free Software
1163 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1166 + * The x2c() and unescape_url() routines were taken from
1167 + * http://www.jmarshall.com/easy/cgi/getcgi.c.txt
1169 + * The comments in that text file state:
1171 + *** Written in 1996 by James Marshall, james@jmarshall.com, except
1172 + *** that the x2c() and unescape_url() routines were lifted directly
1173 + *** from NCSA's sample program util.c, packaged with their HTTPD.
1174 + *** For the latest, see http://www.jmarshall.com/easy/cgi/
1177 + ------------------------------------------------------------------------- */
1180 +#include <unistd.h>
1182 +#include <sys/mman.h>
1183 +#include <sys/types.h>
1184 +#include <sys/wait.h>
1185 +#include <sys/stat.h>
1186 +#include <sys/fcntl.h>
1187 +#include <stdlib.h>
1188 +#include <string.h>
1191 +#ifndef MAX_UPLOAD_KB
1192 +#define MAX_UPLOAD_KB 2048
1194 +#define TEMPDIR "/tmp"
1196 +static int global_upload_size = 0;
1197 +static int ReadMimeEncodedInput(char *qs);
1198 +static var_handler do_putvar = NULL;
1201 + * Convert 2 char hex string into char it represents
1202 + * (from http://www.jmarshall.com/easy/cgi)
1204 +static char x2c (char *what) {
1207 + digit=(what[0] >= 'A' ? ((what[0] & 0xdf) - 'A')+10 : (what[0] - '0'));
1209 + digit+=(what[1] >= 'A' ? ((what[1] & 0xdf) - 'A')+10 : (what[1] - '0'));
1215 + * unsescape %xx to the characters they represent
1218 +static void unescape_url (char *url) {
1221 + for (i=0, j=0; url[j]; ++i, ++j) {
1222 + if ((url[i] = url[j]) == '%') {
1223 + url[i] = x2c(&url[j+1]);
1230 +static inline void put_var(vartype type, char *var)
1237 + val = strchr(var, '=');
1243 + do_putvar(type, var, val);
1250 + * if HTTP_COOKIE is passed as an environment variable,
1251 + * attempt to parse its values into environment variables
1253 +static void CookieVars (void)
1258 + if (getenv("HTTP_COOKIE") != NULL)
1259 + qs=strdup(getenv("HTTP_COOKIE"));
1263 + /** split on; to extract name value pairs */
1264 + token=strtok(qs, ";");
1266 + // skip leading spaces
1267 + while ( token[0] == ' ' )
1270 + put_var(COOKIE_VAR, token);
1272 + token = strtok(NULL, ";");
1278 + * Read cgi variables from query string, and put in environment
1280 +static int ReadCGIQueryString (void)
1286 + if (getenv("QUERY_STRING") != NULL)
1287 + qs=strdup(getenv("QUERY_STRING"));
1291 + /* change plusses into spaces */
1292 + for (i=0; qs[i]; i++ ) { if (qs[i] == '+' ) { qs[i] = ' ';} };
1294 + /** split on & and ; to extract name value pairs */
1296 + token=strtok(qs, "&;");
1298 + unescape_url(token);
1299 + put_var(FORM_VAR, token);
1300 + token=strtok(NULL, "&;");
1309 + * Read cgi variables from stdin (for POST queries)
1310 + * (oh... and if its mime-encoded file upload, we save the
1311 + * file to /tmp; and return the name of the tmp file
1312 + * the cgi script is responsible for disposing of the tmp file
1315 +static int ReadCGIPOSTValues (void) {
1317 + int content_length;
1322 + if (getenv("CONTENT_LENGTH") == NULL)
1325 + content_length = atoi(getenv("CONTENT_LENGTH"));
1327 + /* protect ourselves from 20GB file uploads */
1328 + if (content_length > MAX_UPLOAD_KB * 1024 ) {
1329 + /* But we need to finish reading the content */
1330 + while ( fread( &i, sizeof(int), 1, stdin) == 1 );
1334 + if (!(qs=malloc(content_length+1)))
1337 + /* set the buffer to null, so that a browser messing with less
1338 + data than content_length won't buffer underrun us */
1339 + memset(qs, 0 ,content_length+1);
1341 + if ((!fread(qs,content_length,1,stdin) &&
1342 + (content_length > 0)
1343 + && !feof(stdin))) {
1349 + if (getenv("CONTENT_TYPE") && (strncasecmp(getenv("CONTENT_TYPE"), "multipart/form-data", 19) == 0)) {
1350 + /* This is a mime request, we need to go to the mime handler */
1351 + i=ReadMimeEncodedInput(qs);
1357 + /* change plusses into spaces */
1358 + for (i=0; qs[i]; i++ ) { if (qs[i] == '+' ) { qs[i] = ' ';} };
1360 + /** split on & and ; to extract name value pairs */
1361 + token=strtok(qs, "&;");
1363 + unescape_url(token);
1364 + put_var(FORM_VAR, token);
1365 + token=strtok(NULL, "&;");
1374 + * LineToStr - Scans char and replaces the first "\n" with a "\0";
1375 + * If it finds a "\r", it does that to; (fix DOS brokennes) returns
1376 + * the length of the string;
1378 +static int LineToStr (char *string, size_t max) {
1381 + while ((offset < max) && (string[offset] != '\n') && (string[offset] != '\r'))
1384 + if (string[offset] == '\r') {
1385 + string[offset]='\0';
1388 + if (string[offset] == '\n') {
1389 + string[offset]='\0';
1398 + * ReadMimeEncodedInput - handles things that are mime encoded
1399 + * takes a pointer to the input; returns 0 on success
1402 +static int ReadMimeEncodedInput(char *qs)
1414 + char tmpname[] = TEMPDIR "/XXXXXX";
1416 + /* we should only get here if the content type was set. Segfaults happen
1417 + if Content_Type is null */
1419 + if (getenv("CONTENT_LENGTH") == NULL)
1420 + /* No content length?! */
1423 + cl=atoi(getenv("CONTENT_LENGTH"));
1425 + /* we do this 'cause we can't mess with the real env. variable - it would
1426 + * overwrite the environment - I tried.
1428 + i=strlen(getenv("CONTENT_TYPE"))+1;
1431 + memcpy(ct, getenv("CONTENT_TYPE"), i);
1437 + while (i < strlen(ct) && (strncmp("boundary=", &ct[i], 9) != 0))
1440 + if (i == strlen(ct)) {
1441 + /* no boundary informaiton found */
1445 + boundary=&ct[i+7];
1446 + /* add two leading -- to the boundary */
1450 + /* begin the big loop. Look for:
1452 + Content-Disposition: form-data; name="......."
1457 + Content-Disposition: form-data; name="....." filename="....."
1465 + while (offset < cl) {
1466 + /* first look for boundary */
1467 + while ((offset < cl) && (memcmp(&qs[offset], boundary, strlen(boundary))))
1470 + /* if we got here and we ran off the end, its an error */
1471 + if (offset >= cl) {
1476 + /* if the two characters following the boundary are --, */
1477 + /* then we are at the end, exit */
1478 + if (memcmp(&qs[offset+strlen(boundary)], "--", 2) == 0) {
1482 + /* find where the offset should be */
1483 + line=LineToStr(&qs[offset], cl-offset);
1486 + /* Now we're going to look for content-disposition */
1487 + line=LineToStr(&qs[offset], cl-offset);
1488 + if (strncasecmp(&qs[offset], "Content-Disposition", 19) != 0) {
1489 + /* hmm... content disposition was not where we expected it */
1493 + /* Found it, so let's go find "name=" */
1494 + if (!(envname=strstr(&qs[offset], "name="))) {
1495 + /* now name= is missing?! */
1501 + /* is there a filename tag? */
1502 + if ((filename=strstr(&qs[offset], "filename="))!= NULL)
1507 + /* make envname and filename ASCIIZ */
1508 + for (i=0; (envname[i] != '"') && (envname[i] != '\0'); i++);
1510 + envname[i] = '\0';
1512 + for (i=0; (filename[i] != '"') && (filename[i] != '\0'); i++);
1513 + filename[i] = '\0';
1517 + /* Ok, by some miracle, we have the name; let's skip till we */
1518 + /* come to a blank line */
1519 + line=LineToStr(&qs[offset], cl-offset);
1520 + while (strlen(&qs[offset]) > 1) {
1522 + line=LineToStr(&qs[offset], cl-offset);
1526 + /* And we go back to looking for a boundary */
1527 + while ((offset < cl) && (memcmp(&qs[offset], boundary, strlen(boundary))))
1530 + /* strip [cr] lf */
1531 + if ((qs[offset-1] == '\n') && (qs[offset-2] == '\r'))
1538 + /* ok, at this point, we know where the name is, and we know */
1539 + /* where the content is... we have to do one of two things */
1540 + /* based on whether its a file or not */
1541 + if (filename==NULL) { /* its not a file, so its easy */
1542 + /* just jam the content after the name */
1543 + memcpy(&envname[strlen(envname)+1], &qs[datastart], offset-datastart+1);
1544 + envname[strlen(envname)]='=';
1545 + put_var(FORM_VAR, envname);
1546 + } else { /* handle the fileupload case */
1547 + if (offset-datastart) { /* only if they uploaded */
1548 + if ( global_upload_size == 0 ) {
1551 + /* stuff in the filename */
1552 + ptr= calloc ( sizeof (char), strlen(envname)+strlen(filename)+2+5 );
1553 + sprintf (ptr, "%s_name=%s", envname, filename);
1554 + put_var(FORM_VAR, ptr);
1557 + fd=mkstemp(tmpname);
1562 + write(fd, &qs[datastart], offset-datastart);
1564 + ptr= calloc (sizeof(char), strlen(envname)+strlen(tmpname)+2);
1565 + sprintf (ptr, "%s=%s", envname, tmpname);
1566 + put_var(FORM_VAR, ptr);
1576 +/*-------------------------------------------------------------------------
1580 + *------------------------------------------------------------------------*/
1582 +int cgi_init(var_handler putvar_handler)
1586 + do_putvar = putvar_handler;
1588 + /* Read the current environment into our chain */
1590 + if (getenv("REQUEST_METHOD")) {
1591 + if (strcasecmp(getenv("REQUEST_METHOD"), "GET") == 0)
1592 + retval = ReadCGIQueryString();
1594 + if (strcasecmp(getenv("REQUEST_METHOD"), "POST") == 0)
1595 + retval = ReadCGIPOSTValues();
1600 diff -urN bb.old/libbb/Kbuild bb.dev/libbb/Kbuild
1601 --- bb.old/libbb/Kbuild 2007-05-20 04:17:05.004197480 +0200
1602 +++ bb.dev/libbb/Kbuild 2007-05-20 22:30:31.382511976 +0200
1604 lib-$(CONFIG_MDEV) += xregcomp.o
1605 lib-$(CONFIG_LESS) += xregcomp.o
1606 lib-$(CONFIG_DEVFSD) += xregcomp.o
1608 +lib-$(CONFIG_AWX) += cgi.o