awk: fix a case with multiple -f options. simplify -f file reading.
authorDenis Vlasenko <vda.linux@googlemail.com>
Tue, 1 Jul 2008 01:57:36 +0000 (01:57 -0000)
committerDenis Vlasenko <vda.linux@googlemail.com>
Tue, 1 Jul 2008 01:57:36 +0000 (01:57 -0000)
function                                             old     new   delta
parse_expr                                           833     841      +8
qrealloc                                              33      36      +3
next_input_file                                      203     198      -5
afopen                                                22       -     -22
ftello                                                41       -     -41
ftell                                                 41       -     -41
__GI_ftell                                            41       -     -41
awk_main                                            1002     944     -58
------------------------------------------------------------------------------
(add/remove: 0/4 grow/shrink: 2/2 up/down: 11/-208)          Total: -197 bytes
   text    data     bss     dec     hex filename
 804232     610    6804  811646   c627e busybox_old
 804120     610    6804  811534   c620e busybox_unstripped

editors/awk.c
testsuite/awk.tests
testsuite/awk_t1.tar.bz2 [new file with mode: 0644]

index fef3246b87b02f6cba3b9f1740db73e935f26f0c..cc5dc84b53b794ff4b2bc6743e102aa280c1ba45 100644 (file)
@@ -681,11 +681,6 @@ static ALWAYS_INLINE int isalnum_(int c)
        return (isalnum(c) || c == '_');
 }
 
-static FILE *afopen(const char *path, const char *mode)
-{
-       return (*path == '-' && *(path+1) == '\0') ? stdin : xfopen(path, mode);
-}
-
 /* -------- working with variables (set/get/copy/etc) -------- */
 
 static xhash *iamarray(var *v)
@@ -2740,7 +2735,7 @@ static rstream *next_input_file(void)
                        ind = getvar_s(incvar(intvar[ARGIND]));
                        fname = getvar_s(findvar(iamarray(intvar[ARGV]), ind));
                        if (fname && *fname && !is_assignment(fname))
-                               F = afopen(fname, "r");
+                               F = xfopen_stdin(fname);
                }
        } while (!F);
 
@@ -2757,8 +2752,9 @@ int awk_main(int argc, char **argv)
 {
        unsigned opt;
        char *opt_F, *opt_W;
-       llist_t *opt_v = NULL;
-       int i, j, flen;
+       llist_t *list_v = NULL;
+       llist_t *list_f = NULL;
+       int i, j;
        var *v;
        var tv;
        char **envp;
@@ -2816,35 +2812,33 @@ int awk_main(int argc, char **argv)
                        *s1 = '=';
                }
        }
-       opt_complementary = "v::";
-       opt = getopt32(argv, "F:v:f:W:", &opt_F, &opt_v, &g_progname, &opt_W);
+       opt_complementary = "v::f::"; /* -v and -f can occur multiple times */
+       opt = getopt32(argv, "F:v:f:W:", &opt_F, &list_v, &list_f, &opt_W);
        argv += optind;
        argc -= optind;
        if (opt & 0x1)
                setvar_s(intvar[FS], opt_F); // -F
-       while (opt_v) { /* -v */
-               if (!is_assignment(llist_pop(&opt_v)))
+       while (list_v) { /* -v */
+               if (!is_assignment(llist_pop(&list_v)))
                        bb_show_usage();
        }
-       if (opt & 0x4) { // -f
-               char *s = s; /* die, gcc, die */
-               FILE *from_file = afopen(g_progname, "r");
-               /* one byte is reserved for some trick in next_token */
-               if (fseek(from_file, 0, SEEK_END) == 0) {
-                       flen = ftell(from_file);
-                       s = xmalloc(flen + 4);
-                       fseek(from_file, 0, SEEK_SET);
-                       i = 1 + fread(s + 1, 1, flen, from_file);
-               } else {
+       if (list_f) { /* -f */
+               do {
+                       char *s = NULL;
+                       FILE *from_file;
+
+                       g_progname = llist_pop(&list_f);
+                       from_file = xfopen_stdin(g_progname);
+                       /* one byte is reserved for some trick in next_token */
                        for (i = j = 1; j > 0; i += j) {
                                s = xrealloc(s, i + 4096);
                                j = fread(s + i, 1, 4094, from_file);
                        }
-               }
-               s[i] = '\0';
-               fclose(from_file);
-               parse_program(s + 1);
-               free(s);
+                       s[i] = '\0';
+                       fclose(from_file);
+                       parse_program(s + 1);
+                       free(s);
+               } while (list_f);
        } else { // no -f: take program from 1st parameter
                if (!argc)
                        bb_show_usage();
index 91657418dd52034e35685a4922f6d66af899d3fa..f69da2cc97eb4199b734a90a86dad98be41d283c 100755 (executable)
@@ -17,4 +17,11 @@ testing "awk -F case 5" "awk -F '[#]' '{ print NF }'" "4\n" "" "#abc##zz\n"
 testing "awk -F case 6" "awk -F '[#]' '{ print NF }'" "4\n" "" "z#abc##zz\n"
 testing "awk -F case 7" "awk -F '[#]' '{ print NF }'" "5\n" "" "z##abc##zz\n"
 
+tar xjf awk_t1.tar.bz2
+testing "awk 'gcc build bug'" \
+       "awk -f awk_t1_opt-functions.awk -f awk_t1_opth-gen.awk <awk_t1_input | md5sum" \
+       "f842e256461a5ab1ec60b58d16f1114f  -\n" \
+       "" ""
+rm -rf awk_t1_*
+
 exit $FAILCOUNT
diff --git a/testsuite/awk_t1.tar.bz2 b/testsuite/awk_t1.tar.bz2
new file mode 100644 (file)
index 0000000..0fb8a07
Binary files /dev/null and b/testsuite/awk_t1.tar.bz2 differ