4 /* these flags are static, don't change them when value is changed */
5 #define VF_DONTTOUCH (VF_ARRAY | VF_SPECIAL | VF_WALK | VF_CHILD | VF_DIRTY)
8 +#define fputs(s, stream) fputs_hook(s, stream)
9 +static inline int fputs_hook (__const char *__restrict __s, FILE *__restrict __stream);
13 typedef struct var_s {
14 unsigned type; /* flags */
19 +typedef var *(*awk_cfunc)(var *res, var *args, int nargs);
20 typedef struct func_s {
22 + enum { AWKFUNC, CFUNC } type;
31 next_token(TC_FUNCTION);
33 f = newfunc(t_string);
34 - f->body.first = NULL;
36 + f->x.body.first = NULL;
38 while (next_token(TC_VARIABLE | TC_SEQTERM) & TC_VARIABLE) {
39 v = findvar(ahash, t_string);
41 if (next_token(TC_COMMA | TC_SEQTERM) & TC_SEQTERM)
53 - if (!op->r.f->body.first)
54 + if ((op->r.f->type == AWKFUNC) &&
55 + !op->r.f->x.body.first)
56 syntax_error(EMSG_UNDEF_FUNC);
58 X.v = R.v = nvalloc(op->r.f->nargs+1);
59 @@ -2384,7 +2396,10 @@
63 - res = evaluate(op->r.f->body.first, res);
64 + if (op->r.f->type == AWKFUNC)
65 + res = evaluate(op->r.f->x.body.first, res);
66 + else if (op->r.f->type == CFUNC)
67 + res = op->r.f->x.cfunc(res, fnargs, op->r.f->nargs);
71 @@ -2748,6 +2763,12 @@
74 int awk_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
75 +int awx_main(int argc, char **argv);
78 +static int is_awx = 0;
81 int awk_main(int argc, char **argv)
84 @@ -2812,6 +2833,11 @@
93 opt_complementary = "v::f::"; /* -v and -f can occur multiple times */
94 opt = getopt32(argv, "F:v:f:W:", &opt_F, &list_v, &list_f, &opt_W);
100 + * awk web extension
102 + * Copyright (C) 2007 by Felix Fietkau <nbd@openwrt.org>
104 + * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
109 +#include "awx_parser.h"
111 +#define LINE_BUF 2048
112 +#define HASH_MAX 1536
113 +#define TR_START "@TR<<"
119 +static xhash *lstr = NULL;
120 +static xhash *formvar = NULL;
121 +static int lang_inuse = 0;
123 +/* look up a translation symbol from the hash */
124 +static inline const char *translate_lookup(char *str)
126 + char *name, *def, *p;
131 + if (((p = strchr(str, '|')) != NULL)
132 + || ((p = strchr(str, '#')) != NULL)) {
137 + hi = hash_search(lstr, name);
143 + return getvar_s(v);
146 +/* look for translation markers in the line and return the translated string */
147 +static char *translate_line(char *line)
149 + const char *tok[MAX_TR * 3];
150 + char *l, *p, *p2 = NULL, *res;
151 + int len = 0, _pos = 0, i, tr_abort = 0;
152 + static char *backlog = NULL;
154 + if (backlog && line) {
155 + backlog = xrealloc(backlog, strlen(backlog) + strlen(line) + 1);
156 + sprintf(backlog + strlen(backlog), line);
162 + while (l != NULL) {
163 + if ((p = strstr(l, TR_START)) == NULL) {
164 + len += strlen((tok[_pos++] = l));
168 + p2 = strstr(p, TR_END);
176 + len += strlen((tok[_pos++] = l));
178 + len += strlen((tok[_pos++] = translate_lookup(p + strlen(TR_START))));
181 + l += strlen(TR_END);
185 + p = xmalloc(len + 1);
188 + for (i = 0; i < _pos; i++) {
190 + p += strlen(tok[i]);
196 + if (tr_abort && p2)
202 +/* hook for intercepting awk's use of puts. used for running all printed strings
203 + * through the translation system */
204 +static inline int fputs_hook (__const char *__restrict __s, FILE *__restrict __stream)
206 + if (lang_inuse && (__stream == stdout)) {
210 + str = translate_line((char *) __s);
211 + ret = fputs(str, __stream);
217 + return fputs(__s, __stream);
220 +static var *init_lang(var *res, var *args, int nargs)
223 + lstr = hash_init();
230 +/* load and parse language file */
231 +static void load_lang_file(char *file)
234 + char *b, *name, *value;
235 + char buf1[LINE_BUF];
237 + if ((f = fopen(file, "r")) == NULL)
240 + while (!feof(f) && (fgets(buf1, LINE_BUF - 1, f) != NULL)) {
243 + continue; /* skip comments */
245 + while (isspace(*b))
246 + b++; /* skip leading spaces */
251 + if ((b = strstr(name, "=>")) == NULL)
252 + continue; /* separator not found */
259 + for (b--; isspace(*b); b--)
260 + *b = 0; /* remove trailing spaces */
262 + while (isspace(*value))
263 + value++; /* skip leading spaces */
265 + for (b = value + strlen(value) - 1; isspace(*b); b--)
266 + *b = 0; /* remove trailing spaces */
271 + setvar_s(findvar(lstr,name), value);
277 +static var *load_lang(var *res, var *args, int nargs)
279 + const char *langfmt = "/usr/lib/webif/lang/%s.txt";
280 + char lbuf[LINE_BUF];
284 + init_lang(res, args, nargs);
286 + lang = getvar_s(args);
287 + if (!lang || !strcmp(lang, ""))
290 + sprintf(lbuf, langfmt, lang);
291 + load_lang_file(lbuf);
296 +/* read the contents of an entire file */
297 +static char *get_file(const char *fname)
303 + F = fopen(fname, "r");
308 + if (fseek(F, 0, SEEK_END) == 0) {
310 + s = (char *)xmalloc(flen+4);
311 + fseek(F, 0, SEEK_SET);
312 + i = 1 + fread(s+1, 1, flen, F);
314 + for (i=j=1; j>0; i+=j) {
315 + s = (char *)xrealloc(s, i+4096);
316 + j = fread(s+i, 1, 4094, F);
328 + * taken from parse_program from awk.c
329 + * END{} is not parsed here, and BEGIN{} is executed immediately
331 +static void parse_include(char *p)
334 + chain *initseq = NULL;
340 + memset(&tmp, 0, sizeof(tmp));
343 + while ((tclass = next_token(TC_EOF | TC_OPSEQ |
344 + TC_OPTERM | TC_BEGIN | TC_FUNCDECL)) != TC_EOF) {
345 + if (tclass & TC_OPTERM)
349 + if (tclass & TC_BEGIN) {
350 + initseq = xzalloc(sizeof(chain));
353 + } else if (tclass & TC_FUNCDECL) {
354 + next_token(TC_FUNCTION);
356 + f = newfunc(t_string);
358 + f->x.body.first = NULL;
360 + while (next_token(TC_VARIABLE | TC_SEQTERM) & TC_VARIABLE) {
361 + v = findvar(ahash, t_string);
362 + v->x.aidx = (f->nargs)++;
364 + if (next_token(TC_COMMA | TC_SEQTERM) & TC_SEQTERM)
367 + seq = &(f->x.body);
369 + clear_array(ahash);
372 + if (initseq && initseq->first)
373 + tv = evaluate(initseq->first, tv);
378 +/* include an awk file and run its BEGIN{} section */
379 +static xhash *includes = NULL;
380 +static void include_file(const char *filename)
384 + int oldlnr = g_lineno;
385 + const char *oldprg = g_progname;
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");
403 + g_progname = xstrdup(filename);
404 + parse_include(s+1);
407 + g_progname = oldprg;
410 +static var *include(var *res, var *args, int nargs)
414 + s = getvar_s(args);
415 + if (s && (strlen(s) > 0))
421 +/* parse an awk expression */
422 +static var *parse_awk(char *str, var *tv)
427 + memset(&body, 0, sizeof(body));
431 + /* end of expression, assume that there's going to be a free byte
432 + * at the end of the string that can be used for the ')' */
433 + strcat(str + strlen(str), "}");
434 + n = parse_expr(TC_GRPTERM);
438 + return evaluate(n, tv);
441 +static inline void print_translate(char *s)
445 + str = translate_line(s);
446 + fputs(str, stdout);
452 +static void render_element(struct template_cb *tcb, struct template_element *e)
460 + g_lineno = e->line;
463 + s = malloc(strlen(e->var) + 2);
465 + print_translate(s);
469 + s = malloc(strlen(e->var) + 2);
472 + s2 = strdup(getvar_s(parse_awk(s, v)));
474 + print_translate(s2);
479 + s = malloc(strlen(e->var) + 2);
482 + i = istrue(parse_awk(s, v));
487 + execute_template(tcb, e->sub);
489 + execute_template(tcb, e->sub2);
492 + v = newvar(e->var);
493 + hashwalk_init(v, iamarray(findvar(vhash, e->in)));
494 + while (hashwalk_next(v)) {
495 + execute_template(tcb, e->sub);
505 +/* awk method render(), which opens a template file and processes all awk ssi calls */
506 +static void render_file(const char *filename)
508 + struct template_cb tcb;
509 + struct template_element *e;
511 + const char *oldprg = g_progname;
512 + int oldlnr = g_lineno;
517 + f = fopen(filename, "r");
521 + g_progname = xstrdup(filename);
523 + memset(&tcb, 0, sizeof(tcb));
524 + tcb.handle_element = render_element;
525 + e = parse_template(&tcb, f);
526 + execute_template(&tcb, e);
527 + free_template(&tcb, e);
529 + g_progname = oldprg;
533 +static var *render(var *res, var *args, int nargs)
537 + s = getvar_s(args);
546 +/* Call render, but only if this function hasn't been called already */
547 +static int layout_rendered = 0;
548 +static var *render_layout(var *res, var *args, int nargs)
550 + if (layout_rendered)
552 + layout_rendered = 1;
553 + return render(res, args, nargs);
556 +/* registers a global c function for the awk interpreter */
557 +static void register_cfunc(const char *name, awk_cfunc cfunc, int nargs)
563 + f->x.cfunc = cfunc;
567 +static void putvar(vartype type, char *name, char *value)
569 + if (type != FORM_VAR)
572 + setvar_u(findvar(formvar, name), value);
575 +static const char *cgi_getvar(const char *name)
578 + formvar = hash_init();
582 + if (!formvar || !name)
585 + return getvar_s(findvar(formvar, name));
588 +/* function call for accessing cgi form variables */
589 +static var *getvar(var *res, var *args, int nargs)
591 + const char *s, *svar;
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 + const 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;
632 + /* read the main controller source */
633 + s = get_file(g_progname);
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 = (char *) 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(intvar[ARGC], argc + 1);
696 + setari_u(intvar[ARGV], 0, "awx");
699 + setari_u(intvar[ARGV], ++i, *args++);
701 + while((c = getopt(argc, argv, "i:f:")) != EOF) {
704 + g_progname = optarg;
705 + include_file(optarg);
709 + g_progname = 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);
736 +++ b/editors/awx_parser.h
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);
777 +++ b/editors/awx_parser.l
783 +#include "busybox.h"
784 +#include "awx_parser.h"
801 +char *statestr[] = {
802 + [S_INIT] = "S_INIT",
803 + [S_TEXT] = "S_TEXT",
804 + [S_CODE] = "S_CODE",
805 + [S_IF_START] = "S_IF_START",
806 + [S_FOR_START] = "S_FOR_START",
807 + [S_FOR_IN] = "S_FOR_IN",
812 + [T_TEXT] = "T_TEXT",
815 + [T_CODE] = "T_CODE"
819 +static struct template_cb *parse_cb;
820 +static struct template_element *cur, *head;
821 +static char *textbuf;
822 +static unsigned int buflen;
823 +static unsigned int buf_offset;
824 +static int _lnr = 0;
826 +static void buf_realloc(void)
829 + textbuf = xrealloc(textbuf, buflen);
832 +static void parse_error(char *str)
834 + fprintf(stderr, "Parse error%s%s\n", (str ? ": " : "!"), (str ?: ""));
839 +static struct template_element *new_template_element(struct template_element *parent)
841 + struct template_element *ptr;
843 + ptr = xzalloc(sizeof(struct template_element));
844 + ptr->parent = parent;
848 +static inline void next_template_element(void)
850 + cur->next = new_template_element(cur->parent);
851 + cur->next->prev = cur;
855 +static void addtext(char *text)
857 + while(buf_offset + strlen(text) + 1 > buflen)
860 + buf_offset += sprintf(&textbuf[buf_offset], "%s", text);
863 +static void set_state(int newstate)
868 + static int _rec = 0;
869 + fprintf(stderr, "DEBUG(%d): %s => %s: %s\n", _rec, statestr[state], statestr[newstate], textbuf);
871 + ptr = xstrdup(textbuf);
872 + if (state == S_FOR_IN)
877 + if (parse_cb && (cur->t == T_CODE) && parse_cb->prepare_code)
878 + parse_cb->prepare_code(cur);
891 + if (ptr || !cur->prev)
892 + next_template_element();
896 + if (ptr || !cur->prev)
897 + next_template_element();
903 + parse_error("'@else' without parent element");
904 + cur->sub2 = new_template_element(cur);
914 + parse_error("'@end' without parent element");
916 + next_template_element();
923 + next_template_element();
930 + cur->sub = new_template_element(cur);
939 + if (ptr || !cur->prev)
940 + next_template_element();
953 +"<%"[ \n\t]*"@if"[ \n\t]+ {
954 + if (state == S_TEXT)
955 + set_state(S_IF_START);
960 +"<%"[ \n\t]*"@for"[ \n\t]+ {
961 + if (state == S_TEXT)
962 + set_state(S_FOR_START);
967 +[ \n\t]+"in"[ \n\t]+ {
968 + if (state == S_FOR_START)
969 + set_state(S_FOR_IN);
974 +"<%"[ \n\t]*"@end"[ \n\t]*%> {
975 + if (state != S_TEXT)
980 +"<%"[ \n\t]*"@else"[ \n\t]*%> {
981 + if (state != S_TEXT)
987 + if (state != S_TEXT)
988 + parse_error("'<%' cannot be nested");
993 + if (state == S_TEXT)
1000 + if (state == S_TEXT)
1011 +void execute_template(struct template_cb *cb, struct template_element *e)
1013 + static int rec = 0;
1017 + fprintf(stderr, "DEBUG: execute(%d)\t%s\n", rec, typestr[e->t]);
1020 + if (cb->handle_element)
1021 + cb->handle_element(cb, e);
1033 +struct template_element *parse_template(struct template_cb *cb, FILE *in)
1041 + textbuf = xzalloc(buflen);
1043 + head = xzalloc(sizeof(struct template_element));
1053 +void free_template(struct template_cb *cb, struct template_element *e)
1055 + struct template_element *next;
1062 + if (cb->free_code)
1067 + free_template(cb, e->sub);
1079 + return free_template(cb, next);
1081 --- a/editors/Config.in
1082 +++ b/editors/Config.in
1084 Awk is used as a pattern scanning and processing language. This is
1085 the BusyBox implementation of that programming language.
1088 + bool "Enable awx (awk web extension)"
1092 + awx - awk web extension
1094 config FEATURE_AWK_MATH
1095 bool "Enable math functions (requires libm)"
1097 --- a/editors/Kbuild
1098 +++ b/editors/Kbuild
1100 lib-$(CONFIG_PATCH) += patch.o
1101 lib-$(CONFIG_SED) += sed.o
1102 lib-$(CONFIG_VI) += vi.o
1103 +lib-$(CONFIG_AWX) += awx_parser.o
1105 +editors/awx_parser.c: editors/awx_parser.l editors/awx_parser.h
1109 +editors/awx_parser.o: editors/awx_parser.c FORCE
1110 + $(call cmd,force_checksrc)
1111 + $(call if_changed_rule,cc_o_c)
1112 --- a/include/applets.h
1113 +++ b/include/applets.h
1115 USE_ARPING(APPLET(arping, _BB_DIR_USR_BIN, _BB_SUID_NEVER))
1116 USE_ASH(APPLET(ash, _BB_DIR_BIN, _BB_SUID_NEVER))
1117 USE_AWK(APPLET_NOEXEC(awk, awk, _BB_DIR_USR_BIN, _BB_SUID_NEVER, awk))
1118 +USE_AWK(APPLET_ODDNAME(awx, awk, _BB_DIR_USR_BIN, _BB_SUID_NEVER, awx))
1119 USE_BASENAME(APPLET_NOFORK(basename, basename, _BB_DIR_USR_BIN, _BB_SUID_NEVER, basename))
1120 USE_BBCONFIG(APPLET(bbconfig, _BB_DIR_BIN, _BB_SUID_NEVER))
1121 //USE_BBSH(APPLET(bbsh, _BB_DIR_BIN, _BB_SUID_NEVER))
1122 --- a/include/usage.h
1123 +++ b/include/usage.h
1125 "\n -F sep Use sep as field separator" \
1126 "\n -f file Read program from file" \
1128 +#define awx_trivial_usage NOUSAGE_STR
1129 +#define awx_full_usage ""
1131 #define basename_trivial_usage \
1133 #define basename_full_usage "\n\n" \
1140 +typedef enum { FORM_VAR, COOKIE_VAR } vartype;
1141 +typedef void (*var_handler) (vartype, char *, char *);
1142 +int cgi_init(var_handler);
1148 +/* --------------------------------------------------------------------------
1149 + * functions for processing cgi form data
1150 + * Copyright (C) 2007 by Felix Fietkau <nbd@openwrt.org>
1152 + * parts taken from the core of haserl.cgi - a poor-man's php for embedded/lightweight environments
1153 + * $Id: haserl.c,v 1.13 2004/11/10 17:59:35 nangel Exp $
1154 + * Copyright (c) 2003,2004 Nathan Angelacos (nangel@users.sourceforge.net)
1156 + * This program is free software; you can redistribute it and/or modify
1157 + * it under the terms of the GNU General Public License as published by
1158 + * the Free Software Foundation; either version 2 of the License, or
1159 + * (at your option) any later version.
1161 + * This program is distributed in the hope that it will be useful,
1162 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1163 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1164 + * General Public License for more details.
1166 + * You should have received a copy of the GNU General Public License
1167 + * along with this program; if not, write to the Free Software
1168 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1171 + * The x2c() and unescape_url() routines were taken from
1172 + * http://www.jmarshall.com/easy/cgi/getcgi.c.txt
1174 + * The comments in that text file state:
1176 + *** Written in 1996 by James Marshall, james@jmarshall.com, except
1177 + *** that the x2c() and unescape_url() routines were lifted directly
1178 + *** from NCSA's sample program util.c, packaged with their HTTPD.
1179 + *** For the latest, see http://www.jmarshall.com/easy/cgi/
1182 + ------------------------------------------------------------------------- */
1185 +#include <unistd.h>
1187 +#include <sys/mman.h>
1188 +#include <sys/types.h>
1189 +#include <sys/wait.h>
1190 +#include <sys/stat.h>
1191 +#include <sys/fcntl.h>
1192 +#include <stdlib.h>
1193 +#include <string.h>
1196 +#ifndef MAX_UPLOAD_KB
1197 +#define MAX_UPLOAD_KB 2048
1199 +#define TEMPDIR "/tmp"
1201 +static int global_upload_size = 0;
1202 +static int ReadMimeEncodedInput(char *qs);
1203 +static var_handler do_putvar = NULL;
1206 + * Convert 2 char hex string into char it represents
1207 + * (from http://www.jmarshall.com/easy/cgi)
1209 +static char x2c (char *what) {
1212 + digit=(what[0] >= 'A' ? ((what[0] & 0xdf) - 'A')+10 : (what[0] - '0'));
1214 + digit+=(what[1] >= 'A' ? ((what[1] & 0xdf) - 'A')+10 : (what[1] - '0'));
1220 + * unsescape %xx to the characters they represent
1223 +static void unescape_url (char *url) {
1226 + for (i=0, j=0; url[j]; ++i, ++j) {
1227 + if ((url[i] = url[j]) == '%') {
1228 + url[i] = x2c(&url[j+1]);
1235 +static inline void put_var(vartype type, char *var)
1242 + val = strchr(var, '=');
1248 + do_putvar(type, var, val);
1255 + * if HTTP_COOKIE is passed as an environment variable,
1256 + * attempt to parse its values into environment variables
1258 +static void CookieVars (void)
1263 + if (getenv("HTTP_COOKIE") != NULL)
1264 + qs=strdup(getenv("HTTP_COOKIE"));
1268 + /** split on; to extract name value pairs */
1269 + token=strtok(qs, ";");
1271 + // skip leading spaces
1272 + while ( token[0] == ' ' )
1275 + put_var(COOKIE_VAR, token);
1277 + token = strtok(NULL, ";");
1283 + * Read cgi variables from query string, and put in environment
1285 +static int ReadCGIQueryString (void)
1291 + if (getenv("QUERY_STRING") != NULL)
1292 + qs=strdup(getenv("QUERY_STRING"));
1296 + /* change plusses into spaces */
1297 + for (i=0; qs[i]; i++ ) { if (qs[i] == '+' ) { qs[i] = ' ';} };
1299 + /** split on & and ; to extract name value pairs */
1301 + token=strtok(qs, "&;");
1303 + unescape_url(token);
1304 + put_var(FORM_VAR, token);
1305 + token=strtok(NULL, "&;");
1314 + * Read cgi variables from stdin (for POST queries)
1315 + * (oh... and if its mime-encoded file upload, we save the
1316 + * file to /tmp; and return the name of the tmp file
1317 + * the cgi script is responsible for disposing of the tmp file
1320 +static int ReadCGIPOSTValues (void) {
1322 + int content_length;
1327 + if (getenv("CONTENT_LENGTH") == NULL)
1330 + content_length = atoi(getenv("CONTENT_LENGTH"));
1332 + /* protect ourselves from 20GB file uploads */
1333 + if (content_length > MAX_UPLOAD_KB * 1024 ) {
1334 + /* But we need to finish reading the content */
1335 + while ( fread( &i, sizeof(int), 1, stdin) == 1 );
1339 + if (!(qs=malloc(content_length+1)))
1342 + /* set the buffer to null, so that a browser messing with less
1343 + data than content_length won't buffer underrun us */
1344 + memset(qs, 0 ,content_length+1);
1346 + if ((!fread(qs,content_length,1,stdin) &&
1347 + (content_length > 0)
1348 + && !feof(stdin))) {
1354 + if (getenv("CONTENT_TYPE") && (strncasecmp(getenv("CONTENT_TYPE"), "multipart/form-data", 19) == 0)) {
1355 + /* This is a mime request, we need to go to the mime handler */
1356 + i=ReadMimeEncodedInput(qs);
1362 + /* change plusses into spaces */
1363 + for (i=0; qs[i]; i++ ) { if (qs[i] == '+' ) { qs[i] = ' ';} };
1365 + /** split on & and ; to extract name value pairs */
1366 + token=strtok(qs, "&;");
1368 + unescape_url(token);
1369 + put_var(FORM_VAR, token);
1370 + token=strtok(NULL, "&;");
1379 + * LineToStr - Scans char and replaces the first "\n" with a "\0";
1380 + * If it finds a "\r", it does that to; (fix DOS brokennes) returns
1381 + * the length of the string;
1383 +static int LineToStr (char *string, size_t max) {
1386 + while ((offset < max) && (string[offset] != '\n') && (string[offset] != '\r'))
1389 + if (string[offset] == '\r') {
1390 + string[offset]='\0';
1393 + if (string[offset] == '\n') {
1394 + string[offset]='\0';
1403 + * ReadMimeEncodedInput - handles things that are mime encoded
1404 + * takes a pointer to the input; returns 0 on success
1407 +static int ReadMimeEncodedInput(char *qs)
1419 + char tmpname[] = TEMPDIR "/XXXXXX";
1421 + /* we should only get here if the content type was set. Segfaults happen
1422 + if Content_Type is null */
1424 + if (getenv("CONTENT_LENGTH") == NULL)
1425 + /* No content length?! */
1428 + cl=atoi(getenv("CONTENT_LENGTH"));
1430 + /* we do this 'cause we can't mess with the real env. variable - it would
1431 + * overwrite the environment - I tried.
1433 + i=strlen(getenv("CONTENT_TYPE"))+1;
1436 + memcpy(ct, getenv("CONTENT_TYPE"), i);
1442 + while (i < strlen(ct) && (strncmp("boundary=", &ct[i], 9) != 0))
1445 + if (i == strlen(ct)) {
1446 + /* no boundary informaiton found */
1450 + boundary=&ct[i+7];
1451 + /* add two leading -- to the boundary */
1455 + /* begin the big loop. Look for:
1457 + Content-Disposition: form-data; name="......."
1462 + Content-Disposition: form-data; name="....." filename="....."
1470 + while (offset < cl) {
1471 + /* first look for boundary */
1472 + while ((offset < cl) && (memcmp(&qs[offset], boundary, strlen(boundary))))
1475 + /* if we got here and we ran off the end, its an error */
1476 + if (offset >= cl) {
1481 + /* if the two characters following the boundary are --, */
1482 + /* then we are at the end, exit */
1483 + if (memcmp(&qs[offset+strlen(boundary)], "--", 2) == 0) {
1487 + /* find where the offset should be */
1488 + line=LineToStr(&qs[offset], cl-offset);
1491 + /* Now we're going to look for content-disposition */
1492 + line=LineToStr(&qs[offset], cl-offset);
1493 + if (strncasecmp(&qs[offset], "Content-Disposition", 19) != 0) {
1494 + /* hmm... content disposition was not where we expected it */
1498 + /* Found it, so let's go find "name=" */
1499 + if (!(envname=strstr(&qs[offset], "name="))) {
1500 + /* now name= is missing?! */
1506 + /* is there a filename tag? */
1507 + if ((filename=strstr(&qs[offset], "filename="))!= NULL)
1512 + /* make envname and filename ASCIIZ */
1513 + for (i=0; (envname[i] != '"') && (envname[i] != '\0'); i++);
1515 + envname[i] = '\0';
1517 + for (i=0; (filename[i] != '"') && (filename[i] != '\0'); i++);
1518 + filename[i] = '\0';
1522 + /* Ok, by some miracle, we have the name; let's skip till we */
1523 + /* come to a blank line */
1524 + line=LineToStr(&qs[offset], cl-offset);
1525 + while (strlen(&qs[offset]) > 1) {
1527 + line=LineToStr(&qs[offset], cl-offset);
1531 + /* And we go back to looking for a boundary */
1532 + while ((offset < cl) && (memcmp(&qs[offset], boundary, strlen(boundary))))
1535 + /* strip [cr] lf */
1536 + if ((qs[offset-1] == '\n') && (qs[offset-2] == '\r'))
1543 + /* ok, at this point, we know where the name is, and we know */
1544 + /* where the content is... we have to do one of two things */
1545 + /* based on whether its a file or not */
1546 + if (filename==NULL) { /* its not a file, so its easy */
1547 + /* just jam the content after the name */
1548 + memcpy(&envname[strlen(envname)+1], &qs[datastart], offset-datastart+1);
1549 + envname[strlen(envname)]='=';
1550 + put_var(FORM_VAR, envname);
1551 + } else { /* handle the fileupload case */
1552 + if (offset-datastart) { /* only if they uploaded */
1553 + if ( global_upload_size == 0 ) {
1556 + /* stuff in the filename */
1557 + ptr= calloc ( sizeof (char), strlen(envname)+strlen(filename)+2+5 );
1558 + sprintf (ptr, "%s_name=%s", envname, filename);
1559 + put_var(FORM_VAR, ptr);
1562 + fd=mkstemp(tmpname);
1567 + write(fd, &qs[datastart], offset-datastart);
1569 + ptr= calloc (sizeof(char), strlen(envname)+strlen(tmpname)+2);
1570 + sprintf (ptr, "%s=%s", envname, tmpname);
1571 + put_var(FORM_VAR, ptr);
1581 +/*-------------------------------------------------------------------------
1585 + *------------------------------------------------------------------------*/
1587 +int cgi_init(var_handler putvar_handler)
1591 + do_putvar = putvar_handler;
1593 + /* Read the current environment into our chain */
1595 + if (getenv("REQUEST_METHOD")) {
1596 + if (strcasecmp(getenv("REQUEST_METHOD"), "GET") == 0)
1597 + retval = ReadCGIQueryString();
1599 + if (strcasecmp(getenv("REQUEST_METHOD"), "POST") == 0)
1600 + retval = ReadCGIPOSTValues();
1608 lib-y += xreadlink.o
1610 # conditionally compiled objects:
1611 +lib-$(CONFIG_AWX) += cgi.o
1612 lib-$(CONFIG_FEATURE_MOUNT_LOOP) += loop.o
1613 lib-$(CONFIG_LOSETUP) += loop.o
1614 lib-$(CONFIG_FEATURE_MTAB_SUPPORT) += mtab.o