summaryrefslogtreecommitdiffstats
path: root/package/busybox/patches/920-awx.patch
blob: 4c8331722c6bd2511373e16f9283f9b94ae4d52a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
diff -purN bb.old/editors/awk.c bb.dev/editors/awk.c
--- bb.old/editors/awk.c	2007-03-06 19:38:07.278092000 +0100
+++ bb.dev/editors/awk.c	2007-03-11 05:14:11.776304544 +0100
@@ -30,6 +30,11 @@
 /* these flags are static, don't change them when value is changed */
 #define	VF_DONTTOUCH (VF_ARRAY | VF_SPECIAL | VF_WALK | VF_CHILD | VF_DIRTY)
 
+#ifdef CONFIG_AWX
+#define fputs(s, stream) fputs_hook(s, stream)
+static inline int fputs_hook (__const char *__restrict __s, FILE *__restrict __stream);
+#endif
+
 /* Variable */
 typedef struct var_s {
 	unsigned short type;		/* flags */
@@ -50,10 +55,15 @@ typedef struct chain_s {
 	char *programname;
 } chain;
 
+typedef var *(*awk_cfunc)(var *res, var *args, int nargs);
 /* Function */
 typedef struct func_s {
 	unsigned short nargs;
-	struct chain_s body;
+	enum { AWKFUNC, CFUNC } type;
+	union {
+		awk_cfunc cfunc;
+		struct chain_s body;
+	} x;
 } func;
 
 /* I/O stream */
@@ -1312,7 +1322,8 @@ static void parse_program(char *p)
 			next_token(TC_FUNCTION);
 			pos++;
 			f = newfunc(t.string);
-			f->body.first = NULL;
+			f->type = AWKFUNC;
+			f->x.body.first = NULL;
 			f->nargs = 0;
 			while (next_token(TC_VARIABLE | TC_SEQTERM) & TC_VARIABLE) {
 				v = findvar(ahash, t.string);
@@ -1321,7 +1332,7 @@ static void parse_program(char *p)
 				if (next_token(TC_COMMA | TC_SEQTERM) & TC_SEQTERM)
 					break;
 			}
-			seq = &(f->body);
+			seq = &(f->x.body);
 			chain_group();
 			clear_array(ahash);
 
@@ -2260,7 +2271,8 @@ static var *evaluate(node *op, var *res)
 			break;
 
 		case XC( OC_FUNC ):
-			if (! op->r.f->body.first)
+			if ((op->r.f->type == AWKFUNC) &&
+				!op->r.f->x.body.first)
 				runtime_error(EMSG_UNDEF_FUNC);
 
 			X.v = R.v = nvalloc(op->r.f->nargs+1);
@@ -2277,7 +2289,11 @@ static var *evaluate(node *op, var *res)
 			fnargs = X.v;
 
 			L.s = programname;
-			res = evaluate(op->r.f->body.first, res);
+			if (op->r.f->type == AWKFUNC)
+				res = evaluate(op->r.f->x.body.first, res);
+			else if (op->r.f->type == CFUNC)
+				res = op->r.f->x.cfunc(res, fnargs, op->r.f->nargs);
+
 			programname = L.s;
 
 			nvfree(fnargs);
@@ -2637,6 +2653,11 @@ static rstream *next_input_file(void)
 	return &rsm;
 }
 
+#ifdef CONFIG_AWX
+static int is_awx = 0;
+#include "awx.c"
+#endif
+
 int awk_main(int argc, char **argv)
 {
 	int i, j, flen;
@@ -2693,6 +2714,10 @@ int awk_main(int argc, char **argv)
 		free(s);
 	}
 
+#ifdef CONFIG_AWX
+	do_awx(argc, argv);
+#endif
+
 	programname = NULL;
 	while((c = getopt(argc, argv, "F:v:f:W:")) != EOF) {
 		switch (c) {
diff -purN bb.old/editors/awx.c bb.dev/editors/awx.c
--- bb.old/editors/awx.c	1970-01-01 01:00:00.000000000 +0100
+++ bb.dev/editors/awx.c	2007-03-11 19:03:27.417297384 +0100
@@ -0,0 +1,553 @@
+/*
+ * awk web extension
+ *
+ * Copyright (C) 2007 by Felix Fietkau <nbd@openwrt.org>
+ *
+ * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
+ */
+
+#include <cgi.h>
+#include <glob.h>
+
+#define LINE_BUF 2048
+#define HASH_MAX	1536
+#define TR_START	"@TR<<"
+#define TR_END		">>"
+#define MAX_TR	32
+#define SSI_START "<%"
+#define SSI_END "%>"
+
+#undef fputs
+
+static xhash *lstr = NULL;
+static xhash *formvar = NULL;
+static int lang_inuse = 0;
+
+/* look up a translation symbol from the hash */
+static inline char *translate_lookup(char *str)
+{
+	char *name, *def, *p;
+	hash_item *hi;
+	var *v;
+
+	def = name = str;
+	if (((p = strchr(str, '|')) != NULL)
+		|| ((p = strchr(str, '#')) != NULL)) {
+		def = p + 1;
+		*p = 0;
+	}
+	
+	hi = hash_search(lstr, name);
+	if (!hi)
+		return def;
+	
+	v = &hi->data.v;
+
+	return getvar_s(v);
+}
+
+/* look for translation markers in the line and return the translated string */
+static char *translate_line(char *line)
+{
+	char *tok[MAX_TR * 3];
+	char *l, *p, *p2, *res;
+	int len = 0, _pos = 0, i;
+
+	l = line;
+	while (l != NULL) {
+		if ((p = strstr(l, TR_START)) == NULL) {
+			len += strlen((tok[_pos++] = l));
+			break;
+		}
+
+		p2 = strstr(p, TR_END);
+		if (p2 == NULL)
+			break;
+
+		*p = 0;
+		*p2 = 0;
+		len += strlen((tok[_pos++] = l));
+		len += strlen((tok[_pos++] = translate_lookup(p + strlen(TR_START))));
+
+		l = p2;
+		l += strlen(TR_END);
+	}
+	len++;
+
+	p = xmalloc(len + 1);
+	*p = 0;
+	res = p;
+	for (i = 0; i < _pos; i++) {
+		strcat(p, tok[i]);
+		p += strlen(tok[i]);
+	}
+
+	return res;
+}
+
+/* hook for intercepting awk's use of puts. used for running all printed strings
+ * through the translation system */
+static inline int fputs_hook (__const char *__restrict __s, FILE *__restrict __stream)
+{
+	if (lang_inuse && (__stream == stdout)) {
+		int ret;
+		char *str;
+	
+		str = translate_line((char *) __s);
+		ret = fputs(str, __stream);
+		free(str);
+
+		return ret;
+	}
+
+	return fputs(__s, __stream);
+}
+
+static var *init_lang(var *res, var *args, int nargs)
+{
+	if (!lstr)
+		lstr = hash_init();
+
+	lang_inuse = 1;
+	return res;
+}
+
+
+/* load and parse language file */
+static void load_lang_file(char *file)
+{
+	FILE *f;
+	char *b, *name, *value;
+	char buf1[LINE_BUF];
+
+	if ((f = fopen(file, "r")) == NULL)
+		return;
+
+	while (!feof(f) && (fgets(buf1, LINE_BUF - 1, f) != NULL)) {
+		b = buf1;
+		if (*b == '#')
+			continue; /* skip comments */
+
+		while (isspace(*b))
+			b++; /* skip leading spaces */
+		if (!*b)
+			continue;
+		
+		name = b;
+		if ((b = strstr(name, "=>")) == NULL)
+			continue; /* separator not found */
+
+		value = b + 2;
+		if (!*value)
+			continue;
+		
+		*b = 0;
+		for (b--; isspace(*b); b--)
+			*b = 0; /* remove trailing spaces */
+		
+		while (isspace(*value))
+			value++; /* skip leading spaces */
+
+		for (b = value + strlen(value) - 1; isspace(*b); b--)
+			*b = 0; /* remove trailing spaces */
+		
+		if (!*value)
+			continue;
+
+		setvar_s(findvar(lstr,name), value);
+	}
+
+	fclose(f);
+}
+
+static var *load_lang(var *res, var *args, int nargs)
+{
+	const char *langfmt = "/usr/lib/webif/lang/%s.txt";
+	char lbuf[LINE_BUF];
+	char *lang;
+
+	if (!lang_inuse)
+		init_lang(res, args, nargs);
+	
+	lang = getvar_s(args);
+	if (!lang || !strcmp(lang, ""))
+		return res;
+
+	sprintf(lbuf, langfmt, lang);
+	load_lang_file(lbuf);
+
+	return res;	
+}
+		
+/* read the contents of an entire file */
+static char *get_file(char *fname)
+{
+	FILE *F;
+	char *s = NULL;
+	int i, j, flen;
+
+	F = fopen(fname, "r");
+	if (!F) {
+		return NULL;
+	}
+
+	if (fseek(F, 0, SEEK_END) == 0) {
+		flen = ftell(F);
+		s = (char *)xmalloc(flen+4);
+		fseek(F, 0, SEEK_SET);
+		i = 1 + fread(s+1, 1, flen, F);
+	} else {
+		for (i=j=1; j>0; i+=j) {
+			s = (char *)xrealloc(s, i+4096);
+			j = fread(s+i, 1, 4094, F);
+		}
+	}
+
+	s[i] = '\0';
+	fclose(F);
+	return s;
+}
+
+
+/* parse_include():
+ * 
+ * taken from parse_program from awk.c
+ * END{} is not parsed here, and BEGIN{} is executed immediately
+ */
+static void parse_include(char *p)
+{
+	uint32_t tclass;
+	chain initseq;
+	func *f;
+	var *v, tv;
+	int has_init = 0;
+
+	pos = p;
+	t.lineno = 1;
+	memset(&initseq, 0, sizeof(initseq));
+	while ((tclass = next_token(TC_EOF | TC_OPSEQ |
+				TC_OPTERM | TC_BEGIN | TC_FUNCDECL)) != TC_EOF) {
+		if (tclass & TC_OPTERM)
+			continue;
+
+		seq = &initseq;
+		if (tclass & TC_BEGIN) {
+			has_init = 1;
+			chain_group();
+		} else if (tclass & TC_FUNCDECL) {
+			next_token(TC_FUNCTION);
+			pos++;
+			f = newfunc(t.string);
+			f->type = AWKFUNC;
+			f->x.body.first = NULL;
+			f->nargs = 0;
+			while (next_token(TC_VARIABLE | TC_SEQTERM) & TC_VARIABLE) {
+				v = findvar(ahash, t.string);
+				v->x.aidx = (f->nargs)++;
+
+				if (next_token(TC_COMMA | TC_SEQTERM) & TC_SEQTERM)
+					break;
+			}
+			seq = &(f->x.body);
+			chain_group();
+			clear_array(ahash);
+		}
+	}
+	if (has_init)
+		evaluate(initseq.first, &tv);
+}
+
+/* include an awk file and run its BEGIN{} section */
+static var *include(var *res, var *args, int nargs)
+{
+	static xhash *includes = NULL;
+	char *s;
+	var *v;
+
+	s = getvar_s(args);
+	if (!s)
+		return res;
+
+	if (!includes)
+		includes = hash_init();
+	
+	/* find out if the file has been included already */
+	v = findvar(includes, s);
+	if (istrue(v))
+		return res;
+	setvar_s(v, "1");
+
+	/* read include file */
+	s = get_file(s);
+	if (!s) {
+		fprintf(stderr, "Could not open file.\n");
+		return res;
+	}
+	parse_include(s+1);
+	free(s);
+
+	return res;
+}
+
+
+/* parse and evaluate an awk expression and return the result as string */
+static char *render_lookup(char *fname, int lnr, char *str)
+{
+	chain body;
+	var tv;
+
+	memset(&body, 0, sizeof(body));
+	zero_out_var(&tv);
+	pos = str;
+	seq = &body;
+	
+	/* end of expression, assume that there's going to be a free byte
+	 * at the end of the string that can be used for the ')' */
+	strcat(str + strlen(str), ")");
+	return getvar_s(evaluate(parse_expr(TC_SEQTERM), &tv));
+}
+
+static inline void print_translate(char *s)
+{
+	char *str = s;
+	if (lang_inuse)
+		str = translate_line(s);
+	fputs(str, stdout);
+	fflush(stdout);
+	if (lang_inuse)
+		free(str);
+}
+
+/* process awk calls in a template line and print the output to stdout */
+static void render_line(char *fname, int lnr, char *line)
+{
+	char *tok[MAX_TR * 3];
+	char *l, *p, *p2, *res;
+	int len = 0, _pos = 0, i;
+
+	l = line;
+	while (l != NULL) {
+		if ((p = strstr(l, SSI_START)) == NULL) {
+			len += strlen((tok[_pos++] = l));
+			break;
+		}
+
+		p2 = strstr(p, SSI_END);
+		if (p2 == NULL) {
+			fprintf(stderr, "Parse error in '%s', line '%d', unmatched %s\n", fname, lnr, SSI_END);
+			break;
+		}
+
+		*p = 0;
+		*p2 = 0;
+		
+		len += strlen((tok[_pos++] = l));
+		len += strlen((tok[_pos++] = render_lookup(fname, lnr, p + strlen(SSI_START))));
+
+		l = p2;
+		l += strlen(SSI_END);
+	}
+	len++;
+
+	p = xmalloc(len + 1);
+	*p = 0;
+	res = p;
+	for (i = 0; i < _pos; i++) {
+		strcat(p, tok[i]);
+		p += strlen(tok[i]);
+	}
+	print_translate(res);
+	free(res);
+}
+
+/* awk method render(), which opens a template file and processes all awk ssi calls */
+static var *render(var *res, var *args, int nargs)
+{
+	char *s;
+	int lnr = 0;
+	FILE *f;
+	char *buf1;
+			
+	buf1 = xmalloc(LINE_BUF);
+	s = getvar_s(args);
+	if (!s)
+		goto done;
+
+	f = fopen(s, "r");
+	if (!f)
+		goto done;
+
+	while (!feof(f) && (fgets(buf1, LINE_BUF - 1, f) != NULL)) {
+		render_line(s, ++lnr, buf1);
+	}
+	
+done:
+	free(buf1);
+	return res;
+}
+
+/* Call render, but only if this function hasn't been called already */
+static int layout_rendered = 0;
+static var *render_layout(var *res, var *args, int nargs)
+{
+	if (layout_rendered)
+		return res;
+	layout_rendered = 1;
+	return render(res, args, nargs);
+}
+
+/* registers a global c function for the awk interpreter */
+static void register_cfunc(char *name, awk_cfunc cfunc, int nargs)
+{
+	func *f;
+
+	f = newfunc(name);
+	f->type = CFUNC;
+	f->x.cfunc = cfunc;
+	f->nargs = nargs;
+}
+
+static void putvar(vartype type, char *name, char *value)
+{
+	if (type != FORM_VAR)
+		return;
+
+	setvar_u(findvar(formvar, name), value);
+}
+
+static char *cgi_getvar(char *name)
+{
+	if (!formvar) {
+		formvar = hash_init();
+		cgi_init(putvar);
+	}
+
+	if (!formvar || !name)
+		return NULL;
+	
+	return getvar_s(findvar(formvar, name));
+}
+
+/* function call for accessing cgi form variables */
+static var *getvar(var *res, var *args, int nargs)
+{
+	char *s;
+	char *svar;
+
+	s = getvar_s(args);
+	if (!s)
+		return res;
+	
+	svar = cgi_getvar(s);
+	if (!svar)
+		return res;
+
+	setvar_u(res, svar);
+
+	return res;
+}
+
+/* call an awk function without arguments by string reference */
+static var *call(var *res, var *args, int nargs)
+{
+	char *s = getvar_s(args);
+	func *f;
+
+	if (!s)
+		goto done;
+	
+	f = newfunc(s);
+	if (f && f->type == AWKFUNC && f->x.body.first)
+		return evaluate(f->x.body.first, res);
+
+done:
+	return res;
+}
+
+
+/* main awx processing function. called from awk_main() */
+static int do_awx(int argc, char **argv)
+{
+	int ret = -1;
+	var tv;
+	char *s = NULL;
+	var *layout;
+	var *action;
+	char *tmp;
+	int i;
+	
+	zero_out_var(&tv);
+
+	/* register awk C callbacks */
+	register_cfunc("getvar", getvar, 1);
+	register_cfunc("render", render, 1);
+	register_cfunc("render_layout", render_layout, 1);
+	register_cfunc("call", call, 1);
+	register_cfunc("include", include, 1);
+	register_cfunc("init_lang", init_lang, 1);
+	register_cfunc("load_lang", load_lang, 1);
+
+	if (!is_awx)
+		return 0;
+
+	/* fill in ARGV array */
+	programname = argv[1];
+	setvar_i(V[ARGC], argc + 1);
+	setari_u(V[ARGV], 0, "awx");
+	i = 0;
+	while (*argv)
+		setari_u(V[ARGV], ++i, *argv++);
+
+	if (argc < 2) {
+		fprintf(stderr, "Invalid argument.\n");
+		goto done;
+	}
+	
+	/* read the main controller source */
+	s = get_file(programname);
+	if (!s) {
+		fprintf(stderr, "Could not open file\n");
+		goto done;
+	}
+	parse_program(s+1);
+	free(s);
+
+
+	/* set some defaults for ACTION and LAYOUT, which will have special meaning */
+	layout = newvar("LAYOUT");
+	setvar_s(layout, "views/layout.ahtml");
+
+	/* run the BEGIN {} block */
+	evaluate(beginseq.first, &tv);
+
+	action = newvar("ACTION");
+	if (!(strlen(getvar_s(action)) > 0)) {
+		tmp = cgi_getvar("action");
+		if (!tmp || (strlen(tmp) <= 0))
+			tmp = strdup("default");
+
+		setvar_p(action, tmp);
+	}
+	
+	/* call the action (precedence: begin block override > cgi parameter > "default") */
+	tmp = xmalloc(strlen(getvar_s(action)) + 7);
+	sprintf(tmp, "handle_%s", getvar_s(action));
+	setvar_s(action, tmp);
+	call(&tv, action, 1);
+	free(tmp);
+
+	/* render the selected layout, will do nothing if render_layout has been called from awk */
+	render_layout(&tv, layout, 1);
+
+	ret = 0;
+done:
+	exit(0);
+}
+
+/* entry point for awx applet */
+int awx_main(int argc, char **argv)
+{
+	is_awx = 1;
+	return awk_main(argc, argv);
+}
+
diff -purN bb.old/editors/Config.in bb.dev/editors/Config.in
--- bb.old/editors/Config.in	2007-01-24 22:34:50.000000000 +0100
+++ bb.dev/editors/Config.in	2007-03-11 06:19:51.469380160 +0100
@@ -12,6 +12,13 @@ config AWK
 	  Awk is used as a pattern scanning and processing language.  This is
 	  the BusyBox implementation of that programming language.
 
+config AWX
+	bool "Enable awx (awk web extension)"
+	default n
+	depends on AWK
+	help
+	  awx - awk web extension
+
 config FEATURE_AWK_MATH
 	bool "Enable math functions (requires libm)"
 	default y
diff -purN bb.old/include/applets.h bb.dev/include/applets.h
--- bb.old/include/applets.h	2007-03-06 19:38:07.355081000 +0100
+++ bb.dev/include/applets.h	2007-03-07 02:12:24.280681880 +0100
@@ -60,6 +60,7 @@ USE_ARP(APPLET(arp, _BB_DIR_SBIN, _BB_SU
 USE_ARPING(APPLET(arping, _BB_DIR_USR_BIN, _BB_SUID_NEVER))
 USE_ASH(APPLET_NOUSAGE(ash, ash, _BB_DIR_BIN, _BB_SUID_NEVER))
 USE_AWK(APPLET(awk, _BB_DIR_USR_BIN, _BB_SUID_NEVER))
+USE_AWX(APPLET_NOUSAGE(awx, awx, _BB_DIR_USR_BIN, _BB_SUID_NEVER)) 
 USE_BASENAME(APPLET(basename, _BB_DIR_USR_BIN, _BB_SUID_NEVER))
 USE_BBCONFIG(APPLET(bbconfig, _BB_DIR_BIN, _BB_SUID_NEVER))
 //USE_BBSH(APPLET(bbsh, _BB_DIR_BIN, _BB_SUID_NEVER))
diff -purN bb.old/include/cgi.h bb.dev/include/cgi.h
--- bb.old/include/cgi.h	1970-01-01 01:00:00.000000000 +0100
+++ bb.dev/include/cgi.h	2007-03-11 18:58:10.708444448 +0100
@@ -0,0 +1,8 @@
+#ifndef CGI_H
+#define CGI_H
+
+typedef enum { FORM_VAR, COOKIE_VAR } vartype;
+typedef void (*var_handler) (vartype, char *, char *);
+int cgi_init(var_handler);
+
+#endif
diff -purN bb.old/libbb/cgi.c bb.dev/libbb/cgi.c
--- bb.old/libbb/cgi.c	1970-01-01 01:00:00.000000000 +0100
+++ bb.dev/libbb/cgi.c	2007-03-11 19:02:04.691873560 +0100
@@ -0,0 +1,457 @@
+/* --------------------------------------------------------------------------
+ * functions for processing cgi form data
+ * Copyright (C) 2007 by Felix Fietkau <nbd@openwrt.org>
+ *
+ * parts taken from the core of haserl.cgi - a poor-man's php for embedded/lightweight environments
+ * $Id: haserl.c,v 1.13 2004/11/10 17:59:35 nangel Exp $ 
+ * Copyright (c) 2003,2004    Nathan Angelacos (nangel@users.sourceforge.net)
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ * -----
+ * The x2c() and unescape_url() routines were taken from  
+ *  http://www.jmarshall.com/easy/cgi/getcgi.c.txt 
+ * 
+ * The comments in that text file state:
+ *
+ ***  Written in 1996 by James Marshall, james@jmarshall.com, except 
+ ***  that the x2c() and unescape_url() routines were lifted directly 
+ ***  from NCSA's sample program util.c, packaged with their HTTPD. 
+ ***     For the latest, see http://www.jmarshall.com/easy/cgi/ 
+ * -----
+ *
+ ------------------------------------------------------------------------- */
+
+#include <stdio.h>
+#include <unistd.h>
+#include <time.h>
+#include <sys/mman.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <sys/stat.h>
+#include <sys/fcntl.h>
+#include <stdlib.h>
+#include <string.h>
+#include <cgi.h>
+
+#ifndef MAX_UPLOAD_KB
+#define MAX_UPLOAD_KB 2048
+#endif
+#define TEMPDIR "/tmp"
+
+static int global_upload_size = 0;
+static int ReadMimeEncodedInput(char *qs);
+static var_handler do_putvar = NULL;
+
+/*
+ * Convert 2 char hex string into char it represents
+ * (from http://www.jmarshall.com/easy/cgi)
+ */
+static char x2c (char *what) {
+	char digit;
+	
+	digit=(what[0] >= 'A' ? ((what[0] & 0xdf) - 'A')+10 : (what[0] - '0'));
+	digit *=16;
+	digit+=(what[1] >= 'A' ? ((what[1] & 0xdf) - 'A')+10 : (what[1] - '0'));
+
+	return digit;
+}
+
+/*
+ * unsescape %xx to the characters they represent 
+ */
+
+static void unescape_url (char *url) {
+	int  i,j;
+
+	for (i=0, j=0; url[j]; ++i, ++j) {
+		if ((url[i] = url[j]) == '%') {
+			url[i] = x2c(&url[j+1]);
+			j+=2;	
+		}
+	}
+	url[i]='\0';
+}
+
+static inline void put_var(vartype type, char *var)
+{
+	char *val;
+	
+	if (!do_putvar)
+		return;
+
+	val = strchr(var, '=');
+	if (!val)
+		return;
+	
+	*val = 0;
+	val++;
+	do_putvar(type, var, val);
+	
+	return;
+}
+
+
+/* CookieVars ()
+ * if HTTP_COOKIE is passed as an environment variable,
+ * attempt to parse its values into environment variables
+ */
+static void CookieVars (void)
+{
+	char *qs;
+	char *token;
+
+	if (getenv("HTTP_COOKIE") != NULL)
+		qs=strdup(getenv("HTTP_COOKIE"));
+	else
+		return;
+	
+	/** split on; to extract name value pairs */
+	token=strtok(qs, ";");
+	while (token) {
+		// skip leading spaces 
+		while ( token[0] == ' ' ) 
+			token++;
+		
+		put_var(COOKIE_VAR, token);
+		
+		token = strtok(NULL, ";");
+	}
+	free (qs);
+}
+
+/* 
+ * Read cgi variables from query string, and put in environment
+ */
+static int ReadCGIQueryString (void) 
+{
+	char *qs;
+	char *token;
+	int i;
+
+	if (getenv("QUERY_STRING") != NULL)
+		qs=strdup(getenv("QUERY_STRING"));
+	else
+		return 0;
+	
+	/* change plusses into spaces */
+	for (i=0; qs[i]; i++ ) { if (qs[i] == '+' ) { qs[i] = ' ';} };
+
+	/** split on & and ; to extract name value pairs */
+	
+	token=strtok(qs, "&;");
+	while (token) {
+		unescape_url(token);
+		put_var(FORM_VAR, token);
+		token=strtok(NULL, "&;");
+	}
+	free(qs);
+
+	return 0;
+}
+
+
+/* 
+ * Read cgi variables from stdin (for POST queries)
+ * (oh... and if its mime-encoded file upload, we save the
+ * file to /tmp; and return the name of the tmp file
+ * the cgi script is responsible for disposing of the tmp file
+ */
+
+static int ReadCGIPOSTValues (void) {
+	char *qs;
+	int content_length;
+	int i;
+	char *token;
+
+
+ 	if (getenv("CONTENT_LENGTH") == NULL) 
+		return(-1);
+	else
+		content_length = atoi(getenv("CONTENT_LENGTH"));
+	
+	/* protect ourselves from 20GB file uploads */
+	if (content_length > MAX_UPLOAD_KB * 1024 ) {
+		/* But we need to finish reading the content */
+	 	while (	fread( &i, sizeof(int), 1, stdin) == 1 );
+		return -1;
+	}
+ 
+ 	if (!(qs=malloc(content_length+1)))
+		return -1;
+
+	/* set the buffer to null, so that a browser messing with less 
+  	   data than content_length won't buffer underrun us */
+	memset(qs, 0 ,content_length+1);
+
+	if ((!fread(qs,content_length,1,stdin) &&
+		(content_length > 0) 
+		&& !feof(stdin))) {
+			
+		free(qs);
+		return -1;
+	}
+
+	if (getenv("CONTENT_TYPE") && (strncasecmp(getenv("CONTENT_TYPE"), "multipart/form-data", 19) == 0)) {
+		/* This is a mime request, we need to go to the mime handler */
+		i=ReadMimeEncodedInput(qs);
+		free(qs);
+			
+		return i;
+	}
+
+	/* change plusses into spaces */
+	for (i=0; qs[i]; i++ ) { if (qs[i] == '+' ) { qs[i] = ' ';} };
+
+	/** split on & and ; to extract name value pairs */
+	token=strtok(qs, "&;");
+	while (token) {
+		unescape_url(token);
+		put_var(FORM_VAR, token);
+		token=strtok(NULL, "&;");
+	}
+	
+	free(qs);
+	
+	return 0;
+}
+
+/*
+ *  LineToStr - Scans char and replaces the first "\n" with a "\0";
+ *  If it finds a "\r", it does that to; (fix DOS brokennes) returns
+ *  the length of the string;
+ */
+static int LineToStr (char *string, size_t max) {
+	size_t offset=0;
+
+	while ((offset < max) && (string[offset] != '\n') && (string[offset] != '\r'))
+		offset++;
+
+	if (string[offset] == '\r') {
+		string[offset]='\0';
+		offset++;
+	}
+	if (string[offset] == '\n') {
+		string[offset]='\0';
+		offset++;
+	}
+
+	return offset;
+}
+
+
+/*
+ * ReadMimeEncodedInput - handles things that are mime encoded
+ * takes a pointer to the input; returns 0 on success
+ */
+
+static int ReadMimeEncodedInput(char *qs) 
+{
+	char 	*boundary;
+	char	*ct;
+	int	i;
+	int	datastart;
+ 	size_t	cl;
+	size_t	offset;
+	char    *envname;
+	char	*filename;
+	char 	*ptr;
+	int	line;
+	char 	tmpname[] = TEMPDIR "/XXXXXX";
+	int	fd;
+	/* we should only get here if the content type was set. Segfaults happen
+	   if Content_Type is null */
+
+	if (getenv("CONTENT_LENGTH") == NULL)
+		/* No content length?! */
+		return(-1);
+
+	cl=atoi(getenv("CONTENT_LENGTH"));
+	
+	/* we do this 'cause we can't mess with the real env. variable - it would
+	 * overwrite the environment - I tried.
+	 */
+	i=strlen(getenv("CONTENT_TYPE"))+1;
+	ct=malloc(i);
+	if (ct)
+		memcpy(ct, getenv("CONTENT_TYPE"), i);
+	else
+		return(-1);
+	
+	i=(int) NULL;
+ 	if (ct != NULL) {
+		while (i < strlen(ct) && (strncmp("boundary=", &ct[i], 9) != 0)) 
+			i++;
+	}
+	if (i == strlen(ct)) {
+		/* no boundary informaiton found */
+		free(ct);
+		return -1;
+	}
+	boundary=&ct[i+7];
+	/* add two leading -- to the boundary */
+	boundary[0]='-';
+	boundary[1]='-';
+	
+	/* begin the big loop.  Look for:
+		--boundary
+		Content-Disposition: form-data;  name="......." 
+		....
+		<blank line>
+		content
+		--boundary
+		Content-Disposition: form-data; name="....." filename="....."
+		...
+		<blank line>
+		--boundary--
+		eof
+	*/
+
+	offset=0;
+	while (offset < cl) {
+		/* first look for boundary */
+		while ((offset < cl) && (memcmp(&qs[offset], boundary, strlen(boundary)))) 
+			offset++;
+
+		/* if we got here and we ran off the end, its an error 		*/
+		if (offset >= cl) { 
+			free(ct);
+			return -1;
+		}
+
+		/* if the two characters following the boundary are --, 	*/ 
+		/* then we are at the end, exit 				*/
+		if (memcmp(&qs[offset+strlen(boundary)], "--", 2) == 0) {
+			offset+=2;
+			break;
+		}
+		/* find where the offset should be */
+		line=LineToStr(&qs[offset], cl-offset);
+		offset+=line;
+				
+		/* Now we're going to look for content-disposition		*/ 
+		line=LineToStr(&qs[offset], cl-offset);
+		if (strncasecmp(&qs[offset], "Content-Disposition", 19) != 0) {
+			/* hmm... content disposition was not where we expected it */
+			free(ct);
+			return -1;
+		}
+		/* Found it, so let's go find "name="				*/
+	 	if (!(envname=strstr(&qs[offset], "name="))) {
+			/* now name= is missing?!  				*/
+			free(ct);
+			return(-1);
+		} else
+			envname+=6;
+
+		/* is there a filename tag? 					*/
+		if ((filename=strstr(&qs[offset], "filename="))!= NULL)
+			filename+=10;
+		else
+			filename=NULL;
+		
+		/* make envname and filename ASCIIZ				*/
+		for (i=0; (envname[i] != '"') && (envname[i] != '\0'); i++);
+		
+		envname[i] = '\0';
+		if (filename) {
+			for (i=0; (filename[i] != '"') && (filename[i] != '\0'); i++);
+			filename[i] = '\0';
+		}
+		offset+=line;
+		
+		/* Ok, by some miracle, we have the name; let's skip till we	*/
+		/* come to a blank line						*/
+		line=LineToStr(&qs[offset], cl-offset);
+		while (strlen(&qs[offset]) > 1) {
+			offset+=line;
+			line=LineToStr(&qs[offset], cl-offset);
+		}
+		offset+=line;
+		datastart=offset;
+		/* And we go back to looking for a boundary */
+		while ((offset < cl) && (memcmp(&qs[offset], boundary, strlen(boundary))))
+			offset++;
+
+		/* strip [cr] lf */
+		if ((qs[offset-1] == '\n') && (qs[offset-2] == '\r'))
+			offset-=2; 
+		else
+			offset-=1;
+
+		qs[offset]=0;
+
+		/* ok, at this point, we know where the name is, and we know    */
+		/* where the content is... we have to do one of two things      */
+		/* based on whether its a file or not				*/
+		if (filename==NULL) { /* its not a file, so its easy		*/
+			/* just jam the content after the name		*/
+			memcpy(&envname[strlen(envname)+1], &qs[datastart], offset-datastart+1);
+			envname[strlen(envname)]='=';
+			put_var(FORM_VAR, envname);
+		} else {	/* handle the fileupload case		*/
+			if (offset-datastart) {  /* only if they uploaded */
+				if ( global_upload_size == 0 ) {
+					return -1;
+				}
+				/*  stuff in the filename */
+				ptr= calloc ( sizeof (char), strlen(envname)+strlen(filename)+2+5 );
+				sprintf (ptr, "%s_name=%s", envname, filename);
+				put_var(FORM_VAR, ptr);
+				free(ptr);
+						
+				fd=mkstemp(tmpname);
+				
+				if (fd == -1)
+					return(-1);
+
+				write(fd, &qs[datastart], offset-datastart);
+				close(fd);
+				ptr= calloc (sizeof(char), strlen(envname)+strlen(tmpname)+2);
+				sprintf (ptr, "%s=%s", envname, tmpname);
+				put_var(FORM_VAR, ptr);
+				free(ptr);
+			}
+		}
+	}
+	free(ct);
+	return 0;
+}
+
+	
+/*-------------------------------------------------------------------------
+ *
+ * Main 
+ *
+ *------------------------------------------------------------------------*/
+
+int cgi_init(var_handler putvar_handler)
+{
+	int	retval = 0;
+	
+	do_putvar = putvar_handler;
+
+	/* Read the current environment into our chain */
+	CookieVars();
+	if (getenv("REQUEST_METHOD")) {
+		if (strcasecmp(getenv("REQUEST_METHOD"), "GET") == 0)
+			retval = ReadCGIQueryString();
+
+		if (strcasecmp(getenv("REQUEST_METHOD"), "POST") == 0)
+			retval = ReadCGIPOSTValues();
+	}
+
+	return retval;
+} 
diff -purN bb.old/libbb/Kbuild bb.dev/libbb/Kbuild
--- bb.old/libbb/Kbuild	2007-03-06 19:38:07.361080000 +0100
+++ bb.dev/libbb/Kbuild	2007-03-11 18:40:51.384445712 +0100
@@ -118,3 +118,6 @@ lib-$(CONFIG_GREP) += xregcomp.o
 lib-$(CONFIG_MDEV) += xregcomp.o
 lib-$(CONFIG_LESS) += xregcomp.o
 lib-$(CONFIG_DEVFSD) += xregcomp.o
+
+lib-$(CONFIG_AWX) += cgi.o
+