aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSven-Göran Bergh <sgb@systemasis.org>2013-11-12 14:18:25 +0100
committerDenys Vlasenko <vda.linux@googlemail.com>2013-11-12 14:18:25 +0100
commitf200f732f4d04a26814f109be8db2b6510e7127c (patch)
tree6a25379ab278a37e65a8ee8999818247fec60376
parentcd0936be361ad2566200479d66fc1e5671182b73 (diff)
downloadbusybox-w32-f200f732f4d04a26814f109be8db2b6510e7127c.tar.gz
busybox-w32-f200f732f4d04a26814f109be8db2b6510e7127c.tar.bz2
busybox-w32-f200f732f4d04a26814f109be8db2b6510e7127c.zip
awk: optionally support -e AWK_PROG
function old new delta awk_main 959 978 +19 Signed-off-by: Sven-Göran Bergh <sgb@systemasis.org> Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--editors/Config.src15
-rw-r--r--editors/Kbuild.src1
-rw-r--r--editors/awk.c109
-rw-r--r--include/applets.src.h1
4 files changed, 84 insertions, 42 deletions
diff --git a/editors/Config.src b/editors/Config.src
index af1e1de5e..d3bd46e40 100644
--- a/editors/Config.src
+++ b/editors/Config.src
@@ -7,21 +7,6 @@ menu "Editors"
7 7
8INSERT 8INSERT
9 9
10config AWK
11 bool "awk"
12 default y
13 help
14 Awk is used as a pattern scanning and processing language. This is
15 the BusyBox implementation of that programming language.
16
17config FEATURE_AWK_LIBM
18 bool "Enable math functions (requires libm)"
19 default y
20 depends on AWK
21 help
22 Enable math functions of the Awk programming language.
23 NOTE: This will require libm to be present for linking.
24
25config CMP 10config CMP
26 bool "cmp" 11 bool "cmp"
27 default y 12 default y
diff --git a/editors/Kbuild.src b/editors/Kbuild.src
index 8888cba12..15d7a4cac 100644
--- a/editors/Kbuild.src
+++ b/editors/Kbuild.src
@@ -7,7 +7,6 @@
7lib-y:= 7lib-y:=
8 8
9INSERT 9INSERT
10lib-$(CONFIG_AWK) += awk.o
11lib-$(CONFIG_CMP) += cmp.o 10lib-$(CONFIG_CMP) += cmp.o
12lib-$(CONFIG_DIFF) += diff.o 11lib-$(CONFIG_DIFF) += diff.o
13lib-$(CONFIG_ED) += ed.o 12lib-$(CONFIG_ED) += ed.o
diff --git a/editors/awk.c b/editors/awk.c
index 8848d94a5..29fb2e782 100644
--- a/editors/awk.c
+++ b/editors/awk.c
@@ -7,12 +7,45 @@
7 * Licensed under GPLv2 or later, see file LICENSE in this source tree. 7 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
8 */ 8 */
9 9
10//config:config AWK
11//config: bool "awk"
12//config: default y
13//config: help
14//config: Awk is used as a pattern scanning and processing language. This is
15//config: the BusyBox implementation of that programming language.
16//config:
17//config:config FEATURE_AWK_LIBM
18//config: bool "Enable math functions (requires libm)"
19//config: default y
20//config: depends on AWK
21//config: help
22//config: Enable math functions of the Awk programming language.
23//config: NOTE: This will require libm to be present for linking.
24//config:
25//config:config FEATURE_AWK_GNU_EXTENSIONS
26//config: bool "Enable a few GNU extensions"
27//config: default y
28//config: depends on AWK
29//config: help
30//config: Enable a few features from gawk:
31//config: * command line option -e AWK_PROGRAM
32//config: * simultaneous use of -f and -e on the command line.
33//config: This enables the use of awk library files.
34//config: Ex: awk -f mylib.awk -e '{print myfunction($1);}' ...
35
36//applet:IF_AWK(APPLET_NOEXEC(awk, awk, BB_DIR_USR_BIN, BB_SUID_DROP, awk))
37
38//kbuild:lib-$(CONFIG_AWK) += awk.o
39
10//usage:#define awk_trivial_usage 40//usage:#define awk_trivial_usage
11//usage: "[OPTIONS] [AWK_PROGRAM] [FILE]..." 41//usage: "[OPTIONS] [AWK_PROGRAM] [FILE]..."
12//usage:#define awk_full_usage "\n\n" 42//usage:#define awk_full_usage "\n\n"
13//usage: " -v VAR=VAL Set variable" 43//usage: " -v VAR=VAL Set variable"
14//usage: "\n -F SEP Use SEP as field separator" 44//usage: "\n -F SEP Use SEP as field separator"
15//usage: "\n -f FILE Read program from FILE" 45//usage: "\n -f FILE Read program from FILE"
46//usage: IF_FEATURE_AWK_GNU_EXTENSIONS(
47//usage: "\n -e AWK_PROGRAM"
48//usage: )
16 49
17#include "libbb.h" 50#include "libbb.h"
18#include "xregex.h" 51#include "xregex.h"
@@ -38,6 +71,25 @@
38#endif 71#endif
39 72
40 73
74#define OPTSTR_AWK \
75 "F:v:f:" \
76 IF_FEATURE_AWK_GNU_EXTENSIONS("e:") \
77 "W:"
78#define OPTCOMPLSTR_AWK \
79 "v::f::" \
80 IF_FEATURE_AWK_GNU_EXTENSIONS("e::")
81enum {
82 OPTBIT_F, /* define field separator */
83 OPTBIT_v, /* define variable */
84 OPTBIT_f, /* pull in awk program from file */
85 IF_FEATURE_AWK_GNU_EXTENSIONS(OPTBIT_e,) /* -e AWK_PROGRAM */
86 OPTBIT_W, /* -W ignored */
87 OPT_F = 1 << OPTBIT_F,
88 OPT_v = 1 << OPTBIT_v,
89 OPT_f = 1 << OPTBIT_f,
90 OPT_e = IF_FEATURE_AWK_GNU_EXTENSIONS((1 << OPTBIT_e)) + 0,
91 OPT_W = 1 << OPTBIT_W
92};
41 93
42#define MAXVARFMT 240 94#define MAXVARFMT 240
43#define MINNVBLOCK 64 95#define MINNVBLOCK 64
@@ -3087,6 +3139,9 @@ int awk_main(int argc, char **argv)
3087 char *opt_F; 3139 char *opt_F;
3088 llist_t *list_v = NULL; 3140 llist_t *list_v = NULL;
3089 llist_t *list_f = NULL; 3141 llist_t *list_f = NULL;
3142#if ENABLE_FEATURE_AWK_GNU_EXTENSIONS
3143 llist_t *list_e = NULL;
3144#endif
3090 int i, j; 3145 int i, j;
3091 var *v; 3146 var *v;
3092 var tv; 3147 var tv;
@@ -3145,11 +3200,11 @@ int awk_main(int argc, char **argv)
3145 *s1 = '='; 3200 *s1 = '=';
3146 } 3201 }
3147 } 3202 }
3148 opt_complementary = "v::f::"; /* -v and -f can occur multiple times */ 3203 opt_complementary = OPTCOMPLSTR_AWK;
3149 opt = getopt32(argv, "F:v:f:W:", &opt_F, &list_v, &list_f, NULL); 3204 opt = getopt32(argv, OPTSTR_AWK, &opt_F, &list_v, &list_f, IF_FEATURE_AWK_GNU_EXTENSIONS(&list_e,) NULL);
3150 argv += optind; 3205 argv += optind;
3151 argc -= optind; 3206 argc -= optind;
3152 if (opt & 0x1) { /* -F */ 3207 if (opt & OPT_F) { /* -F */
3153 unescape_string_in_place(opt_F); 3208 unescape_string_in_place(opt_F);
3154 setvar_s(intvar[FS], opt_F); 3209 setvar_s(intvar[FS], opt_F);
3155 } 3210 }
@@ -3157,31 +3212,35 @@ int awk_main(int argc, char **argv)
3157 if (!is_assignment(llist_pop(&list_v))) 3212 if (!is_assignment(llist_pop(&list_v)))
3158 bb_show_usage(); 3213 bb_show_usage();
3159 } 3214 }
3160 if (list_f) { /* -f */ 3215 while (list_f) { /* -f */
3161 do { 3216 char *s = NULL;
3162 char *s = NULL; 3217 FILE *from_file;
3163 FILE *from_file; 3218
3164 3219 g_progname = llist_pop(&list_f);
3165 g_progname = llist_pop(&list_f); 3220 from_file = xfopen_stdin(g_progname);
3166 from_file = xfopen_stdin(g_progname); 3221 /* one byte is reserved for some trick in next_token */
3167 /* one byte is reserved for some trick in next_token */ 3222 for (i = j = 1; j > 0; i += j) {
3168 for (i = j = 1; j > 0; i += j) { 3223 s = xrealloc(s, i + 4096);
3169 s = xrealloc(s, i + 4096); 3224 j = fread(s + i, 1, 4094, from_file);
3170 j = fread(s + i, 1, 4094, from_file); 3225 }
3171 } 3226 s[i] = '\0';
3172 s[i] = '\0'; 3227 fclose(from_file);
3173 fclose(from_file); 3228 parse_program(s + 1);
3174 parse_program(s + 1); 3229 free(s);
3175 free(s); 3230 }
3176 } while (list_f); 3231 g_progname = "cmd. line";
3177 argc++; 3232#if ENABLE_FEATURE_AWK_GNU_EXTENSIONS
3178 } else { // no -f: take program from 1st parameter 3233 while (list_e) { /* -e */
3179 if (!argc) 3234 parse_program(llist_pop(&list_e));
3235 }
3236#endif
3237 if (!(opt & (OPT_f | OPT_e))) {
3238 if (!*argv)
3180 bb_show_usage(); 3239 bb_show_usage();
3181 g_progname = "cmd. line";
3182 parse_program(*argv++); 3240 parse_program(*argv++);
3241 argc++;
3183 } 3242 }
3184 if (opt & 0x8) // -W 3243 if (opt & OPT_W) // -W
3185 bb_error_msg("warning: option -W is ignored"); 3244 bb_error_msg("warning: option -W is ignored");
3186 3245
3187 /* fill in ARGV array */ 3246 /* fill in ARGV array */
diff --git a/include/applets.src.h b/include/applets.src.h
index 3a47e15b9..ac8f18056 100644
--- a/include/applets.src.h
+++ b/include/applets.src.h
@@ -82,7 +82,6 @@ IF_ADJTIMEX(APPLET(adjtimex, BB_DIR_SBIN, BB_SUID_DROP))
82IF_AR(APPLET(ar, BB_DIR_USR_BIN, BB_SUID_DROP)) 82IF_AR(APPLET(ar, BB_DIR_USR_BIN, BB_SUID_DROP))
83IF_ARP(APPLET(arp, BB_DIR_SBIN, BB_SUID_DROP)) 83IF_ARP(APPLET(arp, BB_DIR_SBIN, BB_SUID_DROP))
84IF_ARPING(APPLET(arping, BB_DIR_USR_SBIN, BB_SUID_DROP)) 84IF_ARPING(APPLET(arping, BB_DIR_USR_SBIN, BB_SUID_DROP))
85IF_AWK(APPLET_NOEXEC(awk, awk, BB_DIR_USR_BIN, BB_SUID_DROP, awk))
86IF_BASENAME(APPLET_NOFORK(basename, basename, BB_DIR_USR_BIN, BB_SUID_DROP, basename)) 85IF_BASENAME(APPLET_NOFORK(basename, basename, BB_DIR_USR_BIN, BB_SUID_DROP, basename))
87IF_BBCONFIG(APPLET(bbconfig, BB_DIR_BIN, BB_SUID_DROP)) 86IF_BBCONFIG(APPLET(bbconfig, BB_DIR_BIN, BB_SUID_DROP))
88IF_BEEP(APPLET(beep, BB_DIR_USR_BIN, BB_SUID_DROP)) 87IF_BEEP(APPLET(beep, BB_DIR_USR_BIN, BB_SUID_DROP))