[PATCH] awk: GNU extension -e

Sven-Göran Bergh svengbergh-busybox at yahoo.com
Thu Nov 14 15:57:11 UTC 2013


2013-11-12, Denys Vlasenko <vda.linux at googlemail.com>:

>On Tue, Nov 5, 2013 at 12:55 PM, Sven-Göran Bergh
><svengbergh-busybox at yahoo.com> wrote:
>>   Example: awk -f mylib.awk '{print myfunction($1);}' ...>
>+    g_progname = "cmd. line";
>+#if ENABLE_FEATURE_AWK_GNU_EXTENSIONS
>+    while (list_e) { /* -e */
>+        parse_program(llist_pop(&list_e));
>+    }
>+    if (AWK_PROG_DEFINED) /* -e or -f */
>+        argc++;
>+    if (*argv && access(*argv, F_OK)) /* not a file -> parse it */
>+        parse_program(*argv++);
>+    else if (!AWK_PROG_DEFINED) /* no program defined */
>+               bb_show_usage();
>+#else
>+    if (AWK_PROG_DEFINED) {
>         argc++;
>     } else { // no -f: take program from 1st parameter
>         if (!argc)
>             bb_show_usage();
>-        g_progname = "cmd. line";
>         parse_program(*argv++);
>     }
>-    if (opt & 0x8) // -W
>+#endif
>
>
>The above looks too complex. Can it be just:
>
>        g_progname = "cmd. line";
>#if ENABLE_FEATURE_AWK_GNU_EXTENSIONS
>        while (list_e) { /* -e */
>                parse_program(llist_pop(&list_e));
>        }
>#endif
>        if (!(opt & (OPT_f | OPT_e))) {
>                if (!*argv)
>                        bb_show_usage();
>                parse_program(*argv++);
>                argc++;
>        }

I agree, it is complex. I guess it does not need to be that user friendly, so a
simplified version is fine with me. However, the above code will not work. It
needs to be:

        g_progname = "cmd. line";
#if ENABLE_FEATURE_AWK_GNU_EXTENSIONS
        while (list_e) { /* -e */
                parse_program(llist_pop(&list_e));
        }
#endif
        if ((opt & (OPT_f | OPT_e))) {

                argc++;
        } else {
                if (!*argv)
                        bb_show_usage();
                parse_program(*argv++);
        }


>>+//config:        * simultaneous use of -f and an Awk program on the command line.
>>+//config:          This enables the use of awk library files.
>>+//config:           Ex: awk -f mylib.awk '{print myfunction($1);}' ...
>
>This does not seem to work with GNU Awk 4.0.1:
>
>$ awk -f EMPTYFILE 'BEGIN {print "hello";}'
>$ awk -f EMPTYFILE -e 'BEGIN {print "hello";}'
>hello
>$
>
>In the first case, it seems that 'BEGIN {print "hello";}'
>is taken as a _filename_, not _awk program_.

Indeed, you are right. I do not know why I thought the first form was accepted by
gawk. My mistake. This makes the simplified code above even more relevant.

A new patch with theses changes is attached.

Brgds
/S-G
-------------- next part --------------
A non-text attachment was scrubbed...
Name: awk-e.patch
Type: text/x-patch
Size: 7109 bytes
Desc: not available
URL: <http://lists.busybox.net/pipermail/busybox/attachments/20131114/52c7127f/attachment.bin>


More information about the busybox mailing list