modprobe-small: improve config help text
[oweals/busybox.git] / editors / awk.c
index f47d1ab027d30d035e76ce3979bb2045d38baafa..685e8bed83fab87ffa603f2e740aec7ce6e8dde9 100644 (file)
@@ -7,6 +7,46 @@
  * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  */
 
+//config:config AWK
+//config:      bool "awk"
+//config:      default y
+//config:      help
+//config:        Awk is used as a pattern scanning and processing language. This is
+//config:        the BusyBox implementation of that programming language.
+//config:
+//config:config FEATURE_AWK_LIBM
+//config:      bool "Enable math functions (requires libm)"
+//config:      default y
+//config:      depends on AWK
+//config:      help
+//config:        Enable math functions of the Awk programming language.
+//config:        NOTE: This will require libm to be present for linking.
+//config:
+//config:config FEATURE_AWK_GNU_EXTENSIONS
+//config:      bool "Enable a few GNU extensions"
+//config:      default y
+//config:      depends on AWK
+//config:      help
+//config:        Enable a few features from gawk:
+//config:        * command line option -e AWK_PROGRAM
+//config:        * simultaneous use of -f and -e on the command line.
+//config:          This enables the use of awk library files.
+//config:          Ex: awk -f mylib.awk -e '{print myfunction($1);}' ...
+
+//applet:IF_AWK(APPLET_NOEXEC(awk, awk, BB_DIR_USR_BIN, BB_SUID_DROP, awk))
+
+//kbuild:lib-$(CONFIG_AWK) += awk.o
+
+//usage:#define awk_trivial_usage
+//usage:       "[OPTIONS] [AWK_PROGRAM] [FILE]..."
+//usage:#define awk_full_usage "\n\n"
+//usage:       "       -v VAR=VAL      Set variable"
+//usage:     "\n       -F SEP          Use SEP as field separator"
+//usage:     "\n       -f FILE         Read program from FILE"
+//usage:       IF_FEATURE_AWK_GNU_EXTENSIONS(
+//usage:     "\n       -e AWK_PROGRAM"
+//usage:       )
+
 #include "libbb.h"
 #include "xregex.h"
 #include <math.h>
 /* If you comment out one of these below, it will be #defined later
  * to perform debug printfs to stderr: */
 #define debug_printf_walker(...)  do {} while (0)
+#define debug_printf_eval(...)  do {} while (0)
+#define debug_printf_parse(...)  do {} while (0)
 
 #ifndef debug_printf_walker
 # define debug_printf_walker(...) (fprintf(stderr, __VA_ARGS__))
 #endif
+#ifndef debug_printf_eval
+# define debug_printf_eval(...) (fprintf(stderr, __VA_ARGS__))
+#endif
+#ifndef debug_printf_parse
+# define debug_printf_parse(...) (fprintf(stderr, __VA_ARGS__))
+#endif
 
 
+#define OPTSTR_AWK \
+       "F:v:*f:*" \
+       IF_FEATURE_AWK_GNU_EXTENSIONS("e:*") \
+       "W:"
+enum {
+       OPTBIT_F,       /* define field separator */
+       OPTBIT_v,       /* define variable */
+       OPTBIT_f,       /* pull in awk program from file */
+       IF_FEATURE_AWK_GNU_EXTENSIONS(OPTBIT_e,) /* -e AWK_PROGRAM */
+       OPTBIT_W,       /* -W ignored */
+       OPT_F = 1 << OPTBIT_F,
+       OPT_v = 1 << OPTBIT_v,
+       OPT_f = 1 << OPTBIT_f,
+       OPT_e = IF_FEATURE_AWK_GNU_EXTENSIONS((1 << OPTBIT_e)) + 0,
+       OPT_W = 1 << OPTBIT_W
+};
 
 #define        MAXVARFMT       240
 #define        MINNVBLOCK      64
@@ -140,7 +204,7 @@ typedef struct tsplitter_s {
 
 /* simple token classes */
 /* Order and hex values are very important!!!  See next_token() */
-#define        TC_SEQSTART      1                              /* ( */
+#define        TC_SEQSTART     (1 << 0)                /* ( */
 #define        TC_SEQTERM      (1 << 1)                /* ) */
 #define        TC_REGEXP       (1 << 2)                /* /.../ */
 #define        TC_OUTRDR       (1 << 3)                /* | > >> */
@@ -160,31 +224,39 @@ typedef struct tsplitter_s {
 #define        TC_WHILE        (1 << 17)
 #define        TC_ELSE         (1 << 18)
 #define        TC_BUILTIN      (1 << 19)
-#define        TC_GETLINE      (1 << 20)
-#define        TC_FUNCDECL     (1 << 21)               /* `function' `func' */
-#define        TC_BEGIN        (1 << 22)
-#define        TC_END          (1 << 23)
-#define        TC_EOF          (1 << 24)
-#define        TC_VARIABLE     (1 << 25)
-#define        TC_ARRAY        (1 << 26)
-#define        TC_FUNCTION     (1 << 27)
-#define        TC_STRING       (1 << 28)
-#define        TC_NUMBER       (1 << 29)
+/* This costs ~50 bytes of code.
+ * A separate class to support deprecated "length" form. If we don't need that
+ * (i.e. if we demand that only "length()" with () is valid), then TC_LENGTH
+ * can be merged with TC_BUILTIN:
+ */
+#define        TC_LENGTH       (1 << 20)
+#define        TC_GETLINE      (1 << 21)
+#define        TC_FUNCDECL     (1 << 22)               /* `function' `func' */
+#define        TC_BEGIN        (1 << 23)
+#define        TC_END          (1 << 24)
+#define        TC_EOF          (1 << 25)
+#define        TC_VARIABLE     (1 << 26)
+#define        TC_ARRAY        (1 << 27)
+#define        TC_FUNCTION     (1 << 28)
+#define        TC_STRING       (1 << 29)
+#define        TC_NUMBER       (1 << 30)
 
 #define        TC_UOPPRE  (TC_UOPPRE1 | TC_UOPPRE2)
 
 /* combined token classes */
 #define        TC_BINOP   (TC_BINOPX | TC_COMMA | TC_PIPE | TC_IN)
-#define        TC_UNARYOP (TC_UOPPRE | TC_UOPPOST)
+//#define      TC_UNARYOP (TC_UOPPRE | TC_UOPPOST)
 #define        TC_OPERAND (TC_VARIABLE | TC_ARRAY | TC_FUNCTION \
-                   | TC_BUILTIN | TC_GETLINE | TC_SEQSTART | TC_STRING | TC_NUMBER)
+                   | TC_BUILTIN | TC_LENGTH | TC_GETLINE \
+                   | TC_SEQSTART | TC_STRING | TC_NUMBER)
 
 #define        TC_STATEMNT (TC_STATX | TC_WHILE)
 #define        TC_OPTERM  (TC_SEMICOL | TC_NEWLINE)
 
 /* word tokens, cannot mean something else if not expected */
-#define        TC_WORD    (TC_IN | TC_STATEMNT | TC_ELSE | TC_BUILTIN \
-                   | TC_GETLINE | TC_FUNCDECL | TC_BEGIN | TC_END)
+#define        TC_WORD    (TC_IN | TC_STATEMNT | TC_ELSE \
+                   | TC_BUILTIN | TC_LENGTH | TC_GETLINE \
+                   | TC_FUNCDECL | TC_BEGIN | TC_END)
 
 /* discard newlines after these */
 #define        TC_NOTERM  (TC_COMMA | TC_GRPSTART | TC_GRPTERM \
@@ -227,6 +299,9 @@ typedef struct tsplitter_s {
  * For builtins it has different meaning: n n s3 s2 s1 v3 v2 v1,
  * n - min. number of args, vN - resolve Nth arg to var, sN - resolve to string
  */
+#undef P
+#undef PRIMASK
+#undef PRIMASK2
 #define P(x)      (x << 24)
 #define PRIMASK   0x7F000000
 #define PRIMASK2  0x7E000000
@@ -273,108 +348,100 @@ enum {
 
 /* tokens and their corresponding info values */
 
-#define        NTC     "\377"  /* switch to next token class (tc<<1) */
-#define        NTCC    '\377'
-
-#define        OC_B    OC_BUILTIN
+#define NTC     "\377"  /* switch to next token class (tc<<1) */
+#define NTCC    '\377'
 
 static const char tokenlist[] ALIGN1 =
-       "\1("       NTC
-       "\1)"       NTC
-       "\1/"       NTC                                 /* REGEXP */
-       "\2>>"      "\1>"       "\1|"       NTC         /* OUTRDR */
-       "\2++"      "\2--"      NTC                     /* UOPPOST */
-       "\2++"      "\2--"      "\1$"       NTC         /* UOPPRE1 */
-       "\2=="      "\1="       "\2+="      "\2-="      /* BINOPX */
-       "\2*="      "\2/="      "\2%="      "\2^="
-       "\1+"       "\1-"       "\3**="     "\2**"
-       "\1/"       "\1%"       "\1^"       "\1*"
-       "\2!="      "\2>="      "\2<="      "\1>"
-       "\1<"       "\2!~"      "\1~"       "\2&&"
-       "\2||"      "\1?"       "\1:"       NTC
-       "\2in"      NTC
-       "\1,"       NTC
-       "\1|"       NTC
-       "\1+"       "\1-"       "\1!"       NTC         /* UOPPRE2 */
-       "\1]"       NTC
-       "\1{"       NTC
-       "\1}"       NTC
-       "\1;"       NTC
-       "\1\n"      NTC
-       "\2if"      "\2do"      "\3for"     "\5break"   /* STATX */
-       "\10continue"           "\6delete"  "\5print"
-       "\6printf"  "\4next"    "\10nextfile"
-       "\6return"  "\4exit"    NTC
-       "\5while"   NTC
-       "\4else"    NTC
-
-       "\3and"     "\5compl"   "\6lshift"  "\2or"
-       "\6rshift"  "\3xor"
-       "\5close"   "\6system"  "\6fflush"  "\5atan2"   /* BUILTIN */
-       "\3cos"     "\3exp"     "\3int"     "\3log"
-       "\4rand"    "\3sin"     "\4sqrt"    "\5srand"
-       "\6gensub"  "\4gsub"    "\5index"   "\6length"
-       "\5match"   "\5split"   "\7sprintf" "\3sub"
-       "\6substr"  "\7systime" "\10strftime" "\6mktime"
-       "\7tolower" "\7toupper" NTC
-       "\7getline" NTC
-       "\4func"    "\10function"   NTC
-       "\5BEGIN"   NTC
-       "\3END"     "\0"
+       "\1("         NTC                                   /* TC_SEQSTART */
+       "\1)"         NTC                                   /* TC_SEQTERM */
+       "\1/"         NTC                                   /* TC_REGEXP */
+       "\2>>"        "\1>"         "\1|"       NTC         /* TC_OUTRDR */
+       "\2++"        "\2--"        NTC                     /* TC_UOPPOST */
+       "\2++"        "\2--"        "\1$"       NTC         /* TC_UOPPRE1 */
+       "\2=="        "\1="         "\2+="      "\2-="      /* TC_BINOPX */
+       "\2*="        "\2/="        "\2%="      "\2^="
+       "\1+"         "\1-"         "\3**="     "\2**"
+       "\1/"         "\1%"         "\1^"       "\1*"
+       "\2!="        "\2>="        "\2<="      "\1>"
+       "\1<"         "\2!~"        "\1~"       "\2&&"
+       "\2||"        "\1?"         "\1:"       NTC
+       "\2in"        NTC                                   /* TC_IN */
+       "\1,"         NTC                                   /* TC_COMMA */
+       "\1|"         NTC                                   /* TC_PIPE */
+       "\1+"         "\1-"         "\1!"       NTC         /* TC_UOPPRE2 */
+       "\1]"         NTC                                   /* TC_ARRTERM */
+       "\1{"         NTC                                   /* TC_GRPSTART */
+       "\1}"         NTC                                   /* TC_GRPTERM */
+       "\1;"         NTC                                   /* TC_SEMICOL */
+       "\1\n"        NTC                                   /* TC_NEWLINE */
+       "\2if"        "\2do"        "\3for"     "\5break"   /* TC_STATX */
+       "\10continue" "\6delete"    "\5print"
+       "\6printf"    "\4next"      "\10nextfile"
+       "\6return"    "\4exit"      NTC
+       "\5while"     NTC                                   /* TC_WHILE */
+       "\4else"      NTC                                   /* TC_ELSE */
+       "\3and"       "\5compl"     "\6lshift"  "\2or"      /* TC_BUILTIN */
+       "\6rshift"    "\3xor"
+       "\5close"     "\6system"    "\6fflush"  "\5atan2"
+       "\3cos"       "\3exp"       "\3int"     "\3log"
+       "\4rand"      "\3sin"       "\4sqrt"    "\5srand"
+       "\6gensub"    "\4gsub"      "\5index"   /* "\6length" was here */
+       "\5match"     "\5split"     "\7sprintf" "\3sub"
+       "\6substr"    "\7systime"   "\10strftime" "\6mktime"
+       "\7tolower"   "\7toupper"   NTC
+       "\6length"    NTC                                   /* TC_LENGTH */
+       "\7getline"   NTC                                   /* TC_GETLINE */
+       "\4func"      "\10function" NTC                     /* TC_FUNCDECL */
+       "\5BEGIN"     NTC                                   /* TC_BEGIN */
+       "\3END"                                             /* TC_END */
+       /* compiler adds trailing "\0" */
        ;
 
+#define OC_B  OC_BUILTIN
+
 static const uint32_t tokeninfo[] = {
        0,
        0,
        OC_REGEXP,
-       xS|'a',     xS|'w',     xS|'|',
-       OC_UNARY|xV|P(9)|'p',       OC_UNARY|xV|P(9)|'m',
-       OC_UNARY|xV|P(9)|'P',       OC_UNARY|xV|P(9)|'M',
-           OC_FIELD|xV|P(5),
-       OC_COMPARE|VV|P(39)|5,      OC_MOVE|VV|P(74),
-           OC_REPLACE|NV|P(74)|'+',    OC_REPLACE|NV|P(74)|'-',
-       OC_REPLACE|NV|P(74)|'*',    OC_REPLACE|NV|P(74)|'/',
-           OC_REPLACE|NV|P(74)|'%',    OC_REPLACE|NV|P(74)|'&',
-       OC_BINARY|NV|P(29)|'+',     OC_BINARY|NV|P(29)|'-',
-           OC_REPLACE|NV|P(74)|'&',    OC_BINARY|NV|P(15)|'&',
-       OC_BINARY|NV|P(25)|'/',     OC_BINARY|NV|P(25)|'%',
-           OC_BINARY|NV|P(15)|'&',     OC_BINARY|NV|P(25)|'*',
-       OC_COMPARE|VV|P(39)|4,      OC_COMPARE|VV|P(39)|3,
-           OC_COMPARE|VV|P(39)|0,      OC_COMPARE|VV|P(39)|1,
-       OC_COMPARE|VV|P(39)|2,      OC_MATCH|Sx|P(45)|'!',
-           OC_MATCH|Sx|P(45)|'~',      OC_LAND|Vx|P(55),
-       OC_LOR|Vx|P(59),            OC_TERNARY|Vx|P(64)|'?',
-           OC_COLON|xx|P(67)|':',
-       OC_IN|SV|P(49),
+       xS|'a',                  xS|'w',                  xS|'|',
+       OC_UNARY|xV|P(9)|'p',    OC_UNARY|xV|P(9)|'m',
+       OC_UNARY|xV|P(9)|'P',    OC_UNARY|xV|P(9)|'M',    OC_FIELD|xV|P(5),
+       OC_COMPARE|VV|P(39)|5,   OC_MOVE|VV|P(74),        OC_REPLACE|NV|P(74)|'+', OC_REPLACE|NV|P(74)|'-',
+       OC_REPLACE|NV|P(74)|'*', OC_REPLACE|NV|P(74)|'/', OC_REPLACE|NV|P(74)|'%', OC_REPLACE|NV|P(74)|'&',
+       OC_BINARY|NV|P(29)|'+',  OC_BINARY|NV|P(29)|'-',  OC_REPLACE|NV|P(74)|'&', OC_BINARY|NV|P(15)|'&',
+       OC_BINARY|NV|P(25)|'/',  OC_BINARY|NV|P(25)|'%',  OC_BINARY|NV|P(15)|'&',  OC_BINARY|NV|P(25)|'*',
+       OC_COMPARE|VV|P(39)|4,   OC_COMPARE|VV|P(39)|3,   OC_COMPARE|VV|P(39)|0,   OC_COMPARE|VV|P(39)|1,
+       OC_COMPARE|VV|P(39)|2,   OC_MATCH|Sx|P(45)|'!',   OC_MATCH|Sx|P(45)|'~',   OC_LAND|Vx|P(55),
+       OC_LOR|Vx|P(59),         OC_TERNARY|Vx|P(64)|'?', OC_COLON|xx|P(67)|':',
+       OC_IN|SV|P(49), /* TC_IN */
        OC_COMMA|SS|P(80),
        OC_PGETLINE|SV|P(37),
-       OC_UNARY|xV|P(19)|'+',      OC_UNARY|xV|P(19)|'-',
-           OC_UNARY|xV|P(19)|'!',
+       OC_UNARY|xV|P(19)|'+',   OC_UNARY|xV|P(19)|'-',   OC_UNARY|xV|P(19)|'!',
+       0, /* ] */
        0,
        0,
        0,
-       0,
-       0,
-       ST_IF,          ST_DO,          ST_FOR,         OC_BREAK,
-       OC_CONTINUE,                    OC_DELETE|Vx,   OC_PRINT,
-       OC_PRINTF,      OC_NEXT,        OC_NEXTFILE,
-       OC_RETURN|Vx,   OC_EXIT|Nx,
+       0, /* \n */
+       ST_IF,        ST_DO,        ST_FOR,      OC_BREAK,
+       OC_CONTINUE,  OC_DELETE|Vx, OC_PRINT,
+       OC_PRINTF,    OC_NEXT,      OC_NEXTFILE,
+       OC_RETURN|Vx, OC_EXIT|Nx,
        ST_WHILE,
-       0,
-
+       0, /* else */
        OC_B|B_an|P(0x83), OC_B|B_co|P(0x41), OC_B|B_ls|P(0x83), OC_B|B_or|P(0x83),
        OC_B|B_rs|P(0x83), OC_B|B_xo|P(0x83),
        OC_FBLTIN|Sx|F_cl, OC_FBLTIN|Sx|F_sy, OC_FBLTIN|Sx|F_ff, OC_B|B_a2|P(0x83),
        OC_FBLTIN|Nx|F_co, OC_FBLTIN|Nx|F_ex, OC_FBLTIN|Nx|F_in, OC_FBLTIN|Nx|F_lg,
        OC_FBLTIN|F_rn,    OC_FBLTIN|Nx|F_si, OC_FBLTIN|Nx|F_sq, OC_FBLTIN|Nx|F_sr,
-       OC_B|B_ge|P(0xd6), OC_B|B_gs|P(0xb6), OC_B|B_ix|P(0x9b), OC_FBLTIN|Sx|F_le,
+       OC_B|B_ge|P(0xd6), OC_B|B_gs|P(0xb6), OC_B|B_ix|P(0x9b), /* OC_FBLTIN|Sx|F_le, was here */
        OC_B|B_ma|P(0x89), OC_B|B_sp|P(0x8b), OC_SPRINTF,        OC_B|B_su|P(0xb6),
        OC_B|B_ss|P(0x8f), OC_FBLTIN|F_ti,    OC_B|B_ti|P(0x0b), OC_B|B_mt|P(0x0b),
        OC_B|B_lo|P(0x49), OC_B|B_up|P(0x49),
+       OC_FBLTIN|Sx|F_le, /* TC_LENGTH */
        OC_GETLINE|SV|P(0),
-       0,      0,
+       0,                 0,
        0,
-       0
+       0 /* TC_END */
 };
 
 /* internal variable names and their initial values       */
@@ -384,7 +451,7 @@ enum {
        ORS,        RS,         RT,         FILENAME,
        SUBSEP,     F0,         ARGIND,     ARGC,
        ARGV,       ERRNO,      FNR,        NR,
-       NF,         IGNORECASE, ENVIRON,    NUM_INTERNAL_VARS
+       NF,         IGNORECASE, ENVIRON,    NUM_INTERNAL_VARS
 };
 
 static const char vNames[] ALIGN1 =
@@ -429,13 +496,13 @@ struct globals {
        smallint nextrec;
        smallint nextfile;
        smallint is_f0_split;
+       smallint t_rollback;
 };
 struct globals2 {
        uint32_t t_info; /* often used */
        uint32_t t_tclass;
        char *t_string;
        int t_lineno;
-       int t_rollback;
 
        var *intvar[NUM_INTERNAL_VARS]; /* often used */
 
@@ -493,11 +560,11 @@ struct globals2 {
 #define nextrec      (G1.nextrec     )
 #define nextfile     (G1.nextfile    )
 #define is_f0_split  (G1.is_f0_split )
+#define t_rollback   (G1.t_rollback  )
 #define t_info       (G.t_info      )
 #define t_tclass     (G.t_tclass    )
 #define t_string     (G.t_string    )
 #define t_lineno     (G.t_lineno    )
-#define t_rollback   (G.t_rollback  )
 #define intvar       (G.intvar      )
 #define fsplitter    (G.fsplitter   )
 #define rsplitter    (G.rsplitter   )
@@ -528,9 +595,7 @@ static const char EMSG_TOO_FEW_ARGS[] ALIGN1 = "Too few arguments for builtin";
 static const char EMSG_NOT_ARRAY[] ALIGN1 = "Not an array";
 static const char EMSG_POSSIBLE_ERROR[] ALIGN1 = "Possible syntax error";
 static const char EMSG_UNDEF_FUNC[] ALIGN1 = "Call to undefined function";
-#if !ENABLE_FEATURE_AWK_LIBM
 static const char EMSG_NO_MATH[] ALIGN1 = "Math support is not compiled in";
-#endif
 
 static void zero_out_var(var *vp)
 {
@@ -688,11 +753,27 @@ static char nextchar(char **s)
        pps = *s;
        if (c == '\\')
                c = bb_process_escape_sequence((const char**)s);
-       if (c == '\\' && *s == pps)
-               c = *(*s)++;
+       /* Example awk statement:
+        * s = "abc\"def"
+        * we must treat \" as "
+        */
+       if (c == '\\' && *s == pps) { /* unrecognized \z? */
+               c = *(*s); /* yes, fetch z */
+               if (c)
+                       (*s)++; /* advance unless z = NUL */
+       }
        return c;
 }
 
+/* TODO: merge with strcpy_and_process_escape_sequences()?
+ */
+static void unescape_string_in_place(char *s1)
+{
+       char *s = s1;
+       while ((*s1 = nextchar(&s)) != '\0')
+               s1++;
+}
+
 static ALWAYS_INLINE int isalnum_(int c)
 {
        return (isalnum(c) || c == '_');
@@ -700,14 +781,25 @@ static ALWAYS_INLINE int isalnum_(int c)
 
 static double my_strtod(char **pp)
 {
-#if ENABLE_DESKTOP
-       if ((*pp)[0] == '0'
-        && ((((*pp)[1] | 0x20) == 'x') || isdigit((*pp)[1]))
-       ) {
-               return strtoull(*pp, pp, 0);
+       char *cp = *pp;
+       if (ENABLE_DESKTOP && cp[0] == '0') {
+               /* Might be hex or octal integer: 0x123abc or 07777 */
+               char c = (cp[1] | 0x20);
+               if (c == 'x' || isdigit(cp[1])) {
+                       unsigned long long ull = strtoull(cp, pp, 0);
+                       if (c == 'x')
+                               return ull;
+                       c = **pp;
+                       if (!isdigit(c) && c != '.')
+                               return ull;
+                       /* else: it may be a floating number. Examples:
+                        * 009.123 (*pp points to '9')
+                        * 000.123 (*pp points to '.')
+                        * fall through to strtod.
+                        */
+               }
        }
-#endif
-       return strtod(*pp, pp);
+       return strtod(cp, pp);
 }
 
 /* -------- working with variables (set/get/copy/etc) -------- */
@@ -817,17 +909,21 @@ static double getvar_i(var *v)
                v->number = 0;
                s = v->string;
                if (s && *s) {
+                       debug_printf_eval("getvar_i: '%s'->", s);
                        v->number = my_strtod(&s);
+                       debug_printf_eval("%f (s:'%s')\n", v->number, s);
                        if (v->type & VF_USER) {
                                s = skip_spaces(s);
                                if (*s != '\0')
                                        v->type &= ~VF_USER;
                        }
                } else {
+                       debug_printf_eval("getvar_i: '%s'->zero\n", s);
                        v->type &= ~VF_USER;
                }
                v->type |= VF_CACHED;
        }
+       debug_printf_eval("getvar_i: %f\n", v->number);
        return v->number;
 }
 
@@ -849,6 +945,7 @@ static var *copyvar(var *dest, const var *src)
        if (dest != src) {
                clrvar(dest);
                dest->type |= (src->type & ~(VF_DONTTOUCH | VF_FSTR));
+               debug_printf_eval("copyvar: number:%f string:'%s'\n", src->number, src->string);
                dest->number = src->number;
                if (src->string)
                        dest->string = xstrdup(src->string);
@@ -965,16 +1062,13 @@ static uint32_t next_token(uint32_t expected)
        const char *tl;
        uint32_t tc;
        const uint32_t *ti;
-       int l;
 
        if (t_rollback) {
                t_rollback = FALSE;
-
        } else if (concat_inserted) {
                concat_inserted = FALSE;
                t_tclass = save_tclass;
                t_info = save_info;
-
        } else {
                p = g_pos;
  readnext:
@@ -989,21 +1083,22 @@ static uint32_t next_token(uint32_t expected)
 
                if (*p == '\0') {
                        tc = TC_EOF;
-
+                       debug_printf_parse("%s: token found: TC_EOF\n", __func__);
                } else if (*p == '\"') {
                        /* it's a string */
                        t_string = s = ++p;
                        while (*p != '\"') {
-                               char *pp = p;
+                               char *pp;
                                if (*p == '\0' || *p == '\n')
                                        syntax_error(EMSG_UNEXP_EOS);
+                               pp = p;
                                *s++ = nextchar(&pp);
                                p = pp;
                        }
                        p++;
                        *s = '\0';
                        tc = TC_STRING;
-
+                       debug_printf_parse("%s: token found:'%s' TC_STRING\n", __func__, t_string);
                } else if ((expected & TC_REGEXP) && *p == '/') {
                        /* it's regexp */
                        t_string = s = ++p;
@@ -1025,69 +1120,73 @@ static uint32_t next_token(uint32_t expected)
                        p++;
                        *s = '\0';
                        tc = TC_REGEXP;
+                       debug_printf_parse("%s: token found:'%s' TC_REGEXP\n", __func__, t_string);
 
                } else if (*p == '.' || isdigit(*p)) {
                        /* it's a number */
                        char *pp = p;
                        t_double = my_strtod(&pp);
                        p = pp;
-                       if (*pp == '.')
+                       if (*p == '.')
                                syntax_error(EMSG_UNEXP_TOKEN);
                        tc = TC_NUMBER;
-
+                       debug_printf_parse("%s: token found:%f TC_NUMBER\n", __func__, t_double);
                } else {
                        /* search for something known */
                        tl = tokenlist;
                        tc = 0x00000001;
                        ti = tokeninfo;
                        while (*tl) {
-                               l = *tl++;
-                               if (l == NTCC) {
+                               int l = (unsigned char) *tl++;
+                               if (l == (unsigned char) NTCC) {
                                        tc <<= 1;
                                        continue;
                                }
-                               /* if token class is expected, token
-                                * matches and it's not a longer word,
-                                * then this is what we are looking for
+                               /* if token class is expected,
+                                * token matches,
+                                * and it's not a longer word,
                                 */
                                if ((tc & (expected | TC_WORD | TC_NEWLINE))
-                                && *tl == *p && strncmp(p, tl, l) == 0
+                                && strncmp(p, tl, l) == 0
                                 && !((tc & TC_WORD) && isalnum_(p[l]))
                                ) {
+                                       /* then this is what we are looking for */
                                        t_info = *ti;
+                                       debug_printf_parse("%s: token found:'%.*s' t_info:%x\n", __func__, l, p, t_info);
                                        p += l;
-                                       break;
+                                       goto token_found;
                                }
                                ti++;
                                tl += l;
                        }
-
-                       if (!*tl) {
-                               /* it's a name (var/array/function),
-                                * otherwise it's something wrong
-                                */
-                               if (!isalnum_(*p))
-                                       syntax_error(EMSG_UNEXP_TOKEN);
-
-                               t_string = --p;
-                               while (isalnum_(*++p)) {
-                                       p[-1] = *p;
-                               }
-                               p[-1] = '\0';
-                               tc = TC_VARIABLE;
-                               /* also consume whitespace between functionname and bracket */
-                               if (!(expected & TC_VARIABLE) || (expected & TC_ARRAY))
-                                       p = skip_spaces(p);
-                               if (*p == '(') {
-                                       tc = TC_FUNCTION;
-                               } else {
-                                       if (*p == '[') {
-                                               p++;
-                                               tc = TC_ARRAY;
-                                       }
-                               }
+                       /* not a known token */
+
+                       /* is it a name? (var/array/function) */
+                       if (!isalnum_(*p))
+                               syntax_error(EMSG_UNEXP_TOKEN); /* no */
+                       /* yes */
+                       t_string = --p;
+                       while (isalnum_(*++p)) {
+                               p[-1] = *p;
+                       }
+                       p[-1] = '\0';
+                       tc = TC_VARIABLE;
+                       /* also consume whitespace between functionname and bracket */
+                       if (!(expected & TC_VARIABLE) || (expected & TC_ARRAY))
+                               p = skip_spaces(p);
+                       if (*p == '(') {
+                               tc = TC_FUNCTION;
+                               debug_printf_parse("%s: token found:'%s' TC_FUNCTION\n", __func__, t_string);
+                       } else {
+                               if (*p == '[') {
+                                       p++;
+                                       tc = TC_ARRAY;
+                                       debug_printf_parse("%s: token found:'%s' TC_ARRAY\n", __func__, t_string);
+                               } else
+                                       debug_printf_parse("%s: token found:'%s' TC_VARIABLE\n", __func__, t_string);
                        }
                }
+ token_found:
                g_pos = p;
 
                /* skipping newlines in some cases */
@@ -1108,9 +1207,10 @@ static uint32_t next_token(uint32_t expected)
        ltclass = t_tclass;
 
        /* Are we ready for this? */
-       if (!(ltclass & expected))
+       if (!(ltclass & expected)) {
                syntax_error((ltclass & (TC_NEWLINE | TC_EOF)) ?
                                EMSG_UNEXP_EOS : EMSG_UNEXP_TOKEN);
+       }
 
        return ltclass;
 #undef concat_inserted
@@ -1159,19 +1259,24 @@ static node *parse_expr(uint32_t iexp)
        uint32_t tc, xtc;
        var *v;
 
+       debug_printf_parse("%s(%x)\n", __func__, iexp);
+
        sn.info = PRIMASK;
        sn.r.n = glptr = NULL;
        xtc = TC_OPERAND | TC_UOPPRE | TC_REGEXP | iexp;
 
        while (!((tc = next_token(xtc)) & iexp)) {
+
                if (glptr && (t_info == (OC_COMPARE | VV | P(39) | 2))) {
                        /* input redirection (<) attached to glptr node */
+                       debug_printf_parse("%s: input redir\n", __func__);
                        cn = glptr->l.n = new_node(OC_CONCAT | SS | P(37));
                        cn->a.n = glptr;
                        xtc = TC_OPERAND | TC_UOPPRE;
                        glptr = NULL;
 
                } else if (tc & (TC_BINOP | TC_UOPPOST)) {
+                       debug_printf_parse("%s: TC_BINOP | TC_UOPPOST\n", __func__);
                        /* for binary and postfix-unary operators, jump back over
                         * previous operators with higher priority */
                        vn = cn;
@@ -1201,6 +1306,7 @@ static node *parse_expr(uint32_t iexp)
                        vn->a.n = cn;
 
                } else {
+                       debug_printf_parse("%s: other\n", __func__);
                        /* for operands and prefix-unary operators, attach them
                         * to last node */
                        vn = cn;
@@ -1208,12 +1314,14 @@ static node *parse_expr(uint32_t iexp)
                        cn->a.n = vn;
                        xtc = TC_OPERAND | TC_UOPPRE | TC_REGEXP;
                        if (tc & (TC_OPERAND | TC_REGEXP)) {
+                               debug_printf_parse("%s: TC_OPERAND | TC_REGEXP\n", __func__);
                                xtc = TC_UOPPRE | TC_UOPPOST | TC_BINOP | TC_OPERAND | iexp;
                                /* one should be very careful with switch on tclass -
                                 * only simple tclasses should be used! */
                                switch (tc) {
                                case TC_VARIABLE:
                                case TC_ARRAY:
+                                       debug_printf_parse("%s: TC_VARIABLE | TC_ARRAY\n", __func__);
                                        cn->info = OC_VAR;
                                        v = hash_search(ahash, t_string);
                                        if (v != NULL) {
@@ -1230,6 +1338,7 @@ static node *parse_expr(uint32_t iexp)
 
                                case TC_NUMBER:
                                case TC_STRING:
+                                       debug_printf_parse("%s: TC_NUMBER | TC_STRING\n", __func__);
                                        cn->info = OC_VAR;
                                        v = cn->l.v = xzalloc(sizeof(var));
                                        if (tc & TC_NUMBER)
@@ -1239,32 +1348,51 @@ static node *parse_expr(uint32_t iexp)
                                        break;
 
                                case TC_REGEXP:
+                                       debug_printf_parse("%s: TC_REGEXP\n", __func__);
                                        mk_re_node(t_string, cn, xzalloc(sizeof(regex_t)*2));
                                        break;
 
                                case TC_FUNCTION:
+                                       debug_printf_parse("%s: TC_FUNCTION\n", __func__);
                                        cn->info = OC_FUNC;
                                        cn->r.f = newfunc(t_string);
                                        cn->l.n = condition();
                                        break;
 
                                case TC_SEQSTART:
+                                       debug_printf_parse("%s: TC_SEQSTART\n", __func__);
                                        cn = vn->r.n = parse_expr(TC_SEQTERM);
+                                       if (!cn)
+                                               syntax_error("Empty sequence");
                                        cn->a.n = vn;
                                        break;
 
                                case TC_GETLINE:
+                                       debug_printf_parse("%s: TC_GETLINE\n", __func__);
                                        glptr = cn;
                                        xtc = TC_OPERAND | TC_UOPPRE | TC_BINOP | iexp;
                                        break;
 
                                case TC_BUILTIN:
+                                       debug_printf_parse("%s: TC_BUILTIN\n", __func__);
                                        cn->l.n = condition();
                                        break;
+
+                               case TC_LENGTH:
+                                       debug_printf_parse("%s: TC_LENGTH\n", __func__);
+                                       next_token(TC_SEQSTART | TC_OPTERM | TC_GRPTERM);
+                                       rollback_token();
+                                       if (t_tclass & TC_SEQSTART) {
+                                               /* It was a "(" token. Handle just like TC_BUILTIN */
+                                               cn->l.n = condition();
+                                       }
+                                       break;
                                }
                        }
                }
        }
+
+       debug_printf_parse("%s() returns %p\n", __func__, sn.r.n);
        return sn.r.n;
 }
 
@@ -1333,18 +1461,25 @@ static void chain_group(void)
        } while (c & TC_NEWLINE);
 
        if (c & TC_GRPSTART) {
+               debug_printf_parse("%s: TC_GRPSTART\n", __func__);
                while (next_token(TC_GRPSEQ | TC_GRPTERM) != TC_GRPTERM) {
+                       debug_printf_parse("%s: !TC_GRPTERM\n", __func__);
                        if (t_tclass & TC_NEWLINE)
                                continue;
                        rollback_token();
                        chain_group();
                }
+               debug_printf_parse("%s: TC_GRPTERM\n", __func__);
        } else if (c & (TC_OPSEQ | TC_OPTERM)) {
+               debug_printf_parse("%s: TC_OPSEQ | TC_OPTERM\n", __func__);
                rollback_token();
                chain_expr(OC_EXEC | Vx);
-       } else {                                                /* TC_STATEMNT */
+       } else {
+               /* TC_STATEMNT */
+               debug_printf_parse("%s: TC_STATEMNT(?)\n", __func__);
                switch (t_info & OPCLSMASK) {
                case ST_IF:
+                       debug_printf_parse("%s: ST_IF\n", __func__);
                        n = chain_node(OC_BR | Vx);
                        n->l.n = condition();
                        chain_group();
@@ -1359,12 +1494,14 @@ static void chain_group(void)
                        break;
 
                case ST_WHILE:
+                       debug_printf_parse("%s: ST_WHILE\n", __func__);
                        n2 = condition();
                        n = chain_loop(NULL);
                        n->l.n = n2;
                        break;
 
                case ST_DO:
+                       debug_printf_parse("%s: ST_DO\n", __func__);
                        n2 = chain_node(OC_EXEC);
                        n = chain_loop(NULL);
                        n2->a.n = n->a.n;
@@ -1373,10 +1510,11 @@ static void chain_group(void)
                        break;
 
                case ST_FOR:
+                       debug_printf_parse("%s: ST_FOR\n", __func__);
                        next_token(TC_SEQSTART);
                        n2 = parse_expr(TC_SEMICOL | TC_SEQTERM);
                        if (t_tclass & TC_SEQTERM) {    /* for-in */
-                               if ((n2->info & OPCLSMASK) != OC_IN)
+                               if (!n2 || (n2->info & OPCLSMASK) != OC_IN)
                                        syntax_error(EMSG_UNEXP_TOKEN);
                                n = chain_node(OC_WALKINIT | VV);
                                n->l.n = n2->l.n;
@@ -1398,6 +1536,7 @@ static void chain_group(void)
 
                case OC_PRINT:
                case OC_PRINTF:
+                       debug_printf_parse("%s: OC_PRINT[F]\n", __func__);
                        n = chain_node(t_info);
                        n->l.n = parse_expr(TC_OPTERM | TC_OUTRDR | TC_GRPTERM);
                        if (t_tclass & TC_OUTRDR) {
@@ -1409,17 +1548,22 @@ static void chain_group(void)
                        break;
 
                case OC_BREAK:
+                       debug_printf_parse("%s: OC_BREAK\n", __func__);
                        n = chain_node(OC_EXEC);
                        n->a.n = break_ptr;
+                       chain_expr(t_info);
                        break;
 
                case OC_CONTINUE:
+                       debug_printf_parse("%s: OC_CONTINUE\n", __func__);
                        n = chain_node(OC_EXEC);
                        n->a.n = continue_ptr;
+                       chain_expr(t_info);
                        break;
 
                /* delete, next, nextfile, return, exit */
                default:
+                       debug_printf_parse("%s: default\n", __func__);
                        chain_expr(t_info);
                }
        }
@@ -1437,19 +1581,22 @@ static void parse_program(char *p)
        while ((tclass = next_token(TC_EOF | TC_OPSEQ | TC_GRPSTART |
                        TC_OPTERM | TC_BEGIN | TC_END | TC_FUNCDECL)) != TC_EOF) {
 
-               if (tclass & TC_OPTERM)
+               if (tclass & TC_OPTERM) {
+                       debug_printf_parse("%s: TC_OPTERM\n", __func__);
                        continue;
+               }
 
                seq = &mainseq;
                if (tclass & TC_BEGIN) {
+                       debug_printf_parse("%s: TC_BEGIN\n", __func__);
                        seq = &beginseq;
                        chain_group();
-
                } else if (tclass & TC_END) {
+                       debug_printf_parse("%s: TC_END\n", __func__);
                        seq = &endseq;
                        chain_group();
-
                } else if (tclass & TC_FUNCDECL) {
+                       debug_printf_parse("%s: TC_FUNCDECL\n", __func__);
                        next_token(TC_FUNCTION);
                        g_pos++;
                        f = newfunc(t_string);
@@ -1465,24 +1612,27 @@ static void parse_program(char *p)
                        seq = &f->body;
                        chain_group();
                        clear_array(ahash);
-
                } else if (tclass & TC_OPSEQ) {
+                       debug_printf_parse("%s: TC_OPSEQ\n", __func__);
                        rollback_token();
                        cn = chain_node(OC_TEST);
                        cn->l.n = parse_expr(TC_OPTERM | TC_EOF | TC_GRPSTART);
                        if (t_tclass & TC_GRPSTART) {
+                               debug_printf_parse("%s: TC_GRPSTART\n", __func__);
                                rollback_token();
                                chain_group();
                        } else {
+                               debug_printf_parse("%s: !TC_GRPSTART\n", __func__);
                                chain_node(OC_PRINT);
                        }
                        cn->r.n = mainseq.last;
-
                } else /* if (tclass & TC_GRPSTART) */ {
+                       debug_printf_parse("%s: TC_GRPSTART(?)\n", __func__);
                        rollback_token();
                        chain_group();
                }
        }
+       debug_printf_parse("%s: TC_EOF\n", __func__);
 }
 
 
@@ -1500,10 +1650,10 @@ static node *mk_splitter(const char *s, tsplitter *spl)
                regfree(re);
                regfree(ire); // TODO: nuke ire, use re+1?
        }
-       if (strlen(s) > 1) {
+       if (s[0] && s[1]) { /* strlen(s) > 1 */
                mk_re_node(s, n, re);
        } else {
-               n->info = (uint32_t) *s;
+               n->info = (uint32_t) s[0];
        }
 
        return n;
@@ -1560,24 +1710,22 @@ static void fsrealloc(int size)
        if (size >= maxfields) {
                i = maxfields;
                maxfields = size + 16;
-               Fields = xrealloc(Fields, maxfields * sizeof(var));
+               Fields = xrealloc(Fields, maxfields * sizeof(Fields[0]));
                for (; i < maxfields; i++) {
                        Fields[i].type = VF_SPECIAL;
                        Fields[i].string = NULL;
                }
        }
-
-       if (size < nfields) {
-               for (i = size; i < nfields; i++) {
-                       clrvar(Fields + i);
-               }
+       /* if size < nfields, clear extra field variables */
+       for (i = size; i < nfields; i++) {
+               clrvar(Fields + i);
        }
        nfields = size;
 }
 
 static int awk_split(const char *s, node *spl, char **slist)
 {
-       int l, n = 0;
+       int l, n;
        char c[4];
        char *s1;
        regmatch_t pmatch[2]; // TODO: why [2]? [1] is enough...
@@ -1591,6 +1739,7 @@ static int awk_split(const char *s, node *spl, char **slist)
        if (*getvar_s(intvar[RS]) == '\0')
                c[2] = '\n';
 
+       n = 0;
        if ((spl->info & OPCLSMASK) == OC_REGEXP) {  /* regex split */
                if (!*s)
                        return n; /* "": zero fields */
@@ -1636,7 +1785,7 @@ static int awk_split(const char *s, node *spl, char **slist)
                }
                if (*s1)
                        n++;
-               while ((s1 = strpbrk(s1, c))) {
+               while ((s1 = strpbrk(s1, c)) != NULL) {
                        *s1++ = '\0';
                        n++;
                }
@@ -1724,14 +1873,23 @@ static void handle_special(var *v)
                is_f0_split = FALSE;
 
        } else if (v == intvar[FS]) {
-               mk_splitter(getvar_s(v), &fsplitter);
+               /*
+                * The POSIX-2008 standard says that changing FS should have no effect on the
+                * current input line, but only on the next one. The language is:
+                *
+                * > Before the first reference to a field in the record is evaluated, the record
+                * > shall be split into fields, according to the rules in Regular Expressions,
+                * > using the value of FS that was current at the time the record was read.
+                *
+                * So, split up current line before assignment to FS:
+                */
+               split_f0();
 
+               mk_splitter(getvar_s(v), &fsplitter);
        } else if (v == intvar[RS]) {
                mk_splitter(getvar_s(v), &rsplitter);
-
        } else if (v == intvar[IGNORECASE]) {
                icase = istrue(v);
-
        } else {                                /* $n */
                n = getvar_i(intvar[NF]);
                setvar_i(intvar[NF], n > v-Fields ? n : v-Fields+1);
@@ -1816,6 +1974,8 @@ static int awk_getline(rstream *rsm, var *v)
        int fd, so, eo, r, rp;
        char c, *m, *s;
 
+       debug_printf_eval("entered %s()\n", __func__);
+
        /* we're using our own buffer since we need access to accumulating
         * characters
         */
@@ -1902,6 +2062,8 @@ static int awk_getline(rstream *rsm, var *v)
        rsm->pos = p - eo;
        rsm->size = size;
 
+       debug_printf_eval("returning from %s(): %d\n", __func__, r);
+
        return r;
 }
 
@@ -1911,8 +2073,8 @@ static int fmt_num(char *b, int size, const char *format, double n, int int_as_i
        char c;
        const char *s = format;
 
-       if (int_as_int && n == (int)n) {
-               r = snprintf(b, size, "%d", (int)n);
+       if (int_as_int && n == (long long)n) {
+               r = snprintf(b, size, "%lld", (long long)n);
        } else {
                do { c = *s; } while (c && *++s);
                if (strchr("diouxX", c)) {
@@ -2148,11 +2310,10 @@ static NOINLINE var *exec_builtin(node *op, var *res)
        switch (info) {
 
        case B_a2:
-#if ENABLE_FEATURE_AWK_LIBM
-               setvar_i(res, atan2(getvar_i(av[0]), getvar_i(av[1])));
-#else
-               syntax_error(EMSG_NO_MATH);
-#endif
+               if (ENABLE_FEATURE_AWK_LIBM)
+                       setvar_i(res, atan2(getvar_i(av[0]), getvar_i(av[1])));
+               else
+                       syntax_error(EMSG_NO_MATH);
                break;
 
        case B_sp: {
@@ -2320,13 +2481,15 @@ static var *evaluate(node *op, var *res)
 #define fnargs (G.evaluate__fnargs)
 /* seed is initialized to 1 */
 #define seed   (G.evaluate__seed)
-#define        sreg   (G.evaluate__sreg)
+#define sreg   (G.evaluate__sreg)
 
        var *v1;
 
        if (!op)
                return setvar_s(res, NULL);
 
+       debug_printf_eval("entered %s()\n", __func__);
+
        v1 = nvalloc(2);
 
        while (op) {
@@ -2347,19 +2510,27 @@ static var *evaluate(node *op, var *res)
                opn = (opinfo & OPNMASK);
                g_lineno = op->lineno;
                op1 = op->l.n;
+               debug_printf_eval("opinfo:%08x opn:%08x\n", opinfo, opn);
 
                /* execute inevitable things */
                if (opinfo & OF_RES1)
                        L.v = evaluate(op1, v1);
                if (opinfo & OF_RES2)
                        R.v = evaluate(op->r.n, v1+1);
-               if (opinfo & OF_STR1)
+               if (opinfo & OF_STR1) {
                        L.s = getvar_s(L.v);
-               if (opinfo & OF_STR2)
+                       debug_printf_eval("L.s:'%s'\n", L.s);
+               }
+               if (opinfo & OF_STR2) {
                        R.s = getvar_s(R.v);
-               if (opinfo & OF_NUM1)
+                       debug_printf_eval("R.s:'%s'\n", R.s);
+               }
+               if (opinfo & OF_NUM1) {
                        L_d = getvar_i(L.v);
+                       debug_printf_eval("L_d:%f\n", L_d);
+               }
 
+               debug_printf_eval("switch(0x%x)\n", XC(opinfo & OPCLSMASK));
                switch (XC(opinfo & OPCLSMASK)) {
 
                /* -- iterative node type -- */
@@ -2526,6 +2697,7 @@ static var *evaluate(node *op, var *res)
                        break;
 
                case XC( OC_MOVE ):
+                       debug_printf_eval("MOVE\n");
                        /* if source is a temporary string, jusk relink it to dest */
 //Disabled: if R.v is numeric but happens to have cached R.v->string,
 //then L.v ends up being a string, which is wrong
@@ -2547,7 +2719,8 @@ static var *evaluate(node *op, var *res)
                        var *vbeg, *v;
                        const char *sv_progname;
 
-                       if (!op->r.f->body.first)
+                       /* The body might be empty, still has to eval the args */
+                       if (!op->r.n->info && !op->r.f->body.first)
                                syntax_error(EMSG_UNDEF_FUNC);
 
                        vbeg = v = nvalloc(op->r.f->nargs + 1);
@@ -2585,7 +2758,7 @@ static var *evaluate(node *op, var *res)
                                                rsm->F = popen(L.s, "r");
                                                rsm->is_pipe = TRUE;
                                        } else {
-                                               rsm->F = fopen_for_read(L.s);           /* not xfopen! */
+                                               rsm->F = fopen_for_read(L.s);  /* not xfopen! */
                                        }
                                }
                        } else {
@@ -2594,7 +2767,7 @@ static var *evaluate(node *op, var *res)
                                rsm = iF;
                        }
 
-                       if (!rsm->F) {
+                       if (!rsm || !rsm->F) {
                                setvar_i(intvar[ERRNO], errno);
                                setvar_i(res, -1);
                                break;
@@ -2614,47 +2787,50 @@ static var *evaluate(node *op, var *res)
 
                /* simple builtins */
                case XC( OC_FBLTIN ): {
-                       int i;
-                       rstream *rsm;
                        double R_d = R_d; /* for compiler */
 
                        switch (opn) {
                        case F_in:
-                               R_d = (int)L_d;
+                               R_d = (long long)L_d;
                                break;
 
                        case F_rn:
                                R_d = (double)rand() / (double)RAND_MAX;
                                break;
-#if ENABLE_FEATURE_AWK_LIBM
+
                        case F_co:
-                               R_d = cos(L_d);
-                               break;
+                               if (ENABLE_FEATURE_AWK_LIBM) {
+                                       R_d = cos(L_d);
+                                       break;
+                               }
 
                        case F_ex:
-                               R_d = exp(L_d);
-                               break;
+                               if (ENABLE_FEATURE_AWK_LIBM) {
+                                       R_d = exp(L_d);
+                                       break;
+                               }
 
                        case F_lg:
-                               R_d = log(L_d);
-                               break;
+                               if (ENABLE_FEATURE_AWK_LIBM) {
+                                       R_d = log(L_d);
+                                       break;
+                               }
 
                        case F_si:
-                               R_d = sin(L_d);
-                               break;
+                               if (ENABLE_FEATURE_AWK_LIBM) {
+                                       R_d = sin(L_d);
+                                       break;
+                               }
 
                        case F_sq:
-                               R_d = sqrt(L_d);
-                               break;
-#else
-                       case F_co:
-                       case F_ex:
-                       case F_lg:
-                       case F_si:
-                       case F_sq:
+                               if (ENABLE_FEATURE_AWK_LIBM) {
+                                       R_d = sqrt(L_d);
+                                       break;
+                               }
+
                                syntax_error(EMSG_NO_MATH);
                                break;
-#endif
+
                        case F_sr:
                                R_d = (double)seed;
                                seed = op1 ? (unsigned)L_d : (unsigned)time(NULL);
@@ -2666,8 +2842,16 @@ static var *evaluate(node *op, var *res)
                                break;
 
                        case F_le:
-                               if (!op1)
+                               debug_printf_eval("length: L.s:'%s'\n", L.s);
+                               if (!op1) {
                                        L.s = getvar_s(intvar[F0]);
+                                       debug_printf_eval("length: L.s='%s'\n", L.s);
+                               }
+                               else if (L.v->type & VF_ARRAY) {
+                                       R_d = L.v->x.array->nel;
+                                       debug_printf_eval("length: array_len:%d\n", L.v->x.array->nel);
+                                       break;
+                               }
                                R_d = strlen(L.s);
                                break;
 
@@ -2681,26 +2865,37 @@ static var *evaluate(node *op, var *res)
                                if (!op1) {
                                        fflush(stdout);
                                } else if (L.s && *L.s) {
-                                       rsm = newfile(L.s);
+                                       rstream *rsm = newfile(L.s);
                                        fflush(rsm->F);
                                } else {
                                        fflush_all();
                                }
                                break;
 
-                       case F_cl:
-                               i = 0;
+                       case F_cl: {
+                               rstream *rsm;
+                               int err = 0;
                                rsm = (rstream *)hash_search(fdhash, L.s);
+                               debug_printf_eval("OC_FBLTIN F_cl rsm:%p\n", rsm);
                                if (rsm) {
-                                       i = rsm->is_pipe ? pclose(rsm->F) : fclose(rsm->F);
+                                       debug_printf_eval("OC_FBLTIN F_cl "
+                                               "rsm->is_pipe:%d, ->F:%p\n",
+                                               rsm->is_pipe, rsm->F);
+                                       /* Can be NULL if open failed. Example:
+                                        * getline line <"doesnt_exist";
+                                        * close("doesnt_exist"); <--- here rsm->F is NULL
+                                        */
+                                       if (rsm->F)
+                                               err = rsm->is_pipe ? pclose(rsm->F) : fclose(rsm->F);
                                        free(rsm->buffer);
                                        hash_remove(fdhash, L.s);
                                }
-                               if (i != 0)
+                               if (err)
                                        setvar_i(intvar[ERRNO], errno);
-                               R_d = (double)i;
+                               R_d = (double)err;
                                break;
                        }
+                       } /* switch */
                        setvar_i(res, R_d);
                        break;
                }
@@ -2777,6 +2972,7 @@ static var *evaluate(node *op, var *res)
                case XC( OC_BINARY ):
                case XC( OC_REPLACE ): {
                        double R_d = getvar_i(R.v);
+                       debug_printf_eval("BINARY/REPLACE: R_d:%f opn:%c\n", R_d, opn);
                        switch (opn) {
                        case '+':
                                L_d += R_d;
@@ -2793,18 +2989,18 @@ static var *evaluate(node *op, var *res)
                                L_d /= R_d;
                                break;
                        case '&':
-#if ENABLE_FEATURE_AWK_LIBM
-                               L_d = pow(L_d, R_d);
-#else
-                               syntax_error(EMSG_NO_MATH);
-#endif
+                               if (ENABLE_FEATURE_AWK_LIBM)
+                                       L_d = pow(L_d, R_d);
+                               else
+                                       syntax_error(EMSG_NO_MATH);
                                break;
                        case '%':
                                if (R_d == 0)
                                        syntax_error(EMSG_DIV_BY_ZERO);
-                               L_d -= (int)(L_d / R_d) * R_d;
+                               L_d -= (long long)(L_d / R_d) * R_d;
                                break;
                        }
+                       debug_printf_eval("BINARY/REPLACE result:%f\n", L_d);
                        res = setvar_i(((opinfo & OPCLSMASK) == OC_BINARY) ? res : L.v, L_d);
                        break;
                }
@@ -2847,6 +3043,7 @@ static var *evaluate(node *op, var *res)
        } /* while (op) */
 
        nvfree(v1);
+       debug_printf_eval("returning from %s(): %p\n", __func__, res);
        return res;
 #undef fnargs
 #undef seed
@@ -2887,21 +3084,18 @@ static int awk_exit(int r)
  * otherwise return 0 */
 static int is_assignment(const char *expr)
 {
-       char *exprc, *s, *s0, *s1;
+       char *exprc, *val;
 
-       exprc = xstrdup(expr);
-       if (!isalnum_(*exprc) || (s = strchr(exprc, '=')) == NULL) {
-               free(exprc);
+       if (!isalnum_(*expr) || (val = strchr(expr, '=')) == NULL) {
                return FALSE;
        }
 
-       *s++ = '\0';
-       s0 = s1 = s;
-       while (*s)
-               *s1++ = nextchar(&s);
+       exprc = xstrdup(expr);
+       val = exprc + (val - expr);
+       *val++ = '\0';
 
-       *s1 = '\0';
-       setvar_u(newvar(exprc), s0);
+       unescape_string_in_place(val);
+       setvar_u(newvar(exprc), val);
        free(exprc);
        return TRUE;
 }
@@ -2912,7 +3106,7 @@ static rstream *next_input_file(void)
 #define rsm          (G.next_input_file__rsm)
 #define files_happen (G.next_input_file__files_happen)
 
-       FILE *F = NULL;
+       FILE *F;
        const char *fname, *ind;
 
        if (rsm.F)
@@ -2920,19 +3114,21 @@ static rstream *next_input_file(void)
        rsm.F = NULL;
        rsm.pos = rsm.adv = 0;
 
-       do {
+       for (;;) {
                if (getvar_i(intvar[ARGIND])+1 >= getvar_i(intvar[ARGC])) {
                        if (files_happen)
                                return NULL;
                        fname = "-";
                        F = stdin;
-               } else {
-                       ind = getvar_s(incvar(intvar[ARGIND]));
-                       fname = getvar_s(findvar(iamarray(intvar[ARGV]), ind));
-                       if (fname && *fname && !is_assignment(fname))
-                               F = xfopen_stdin(fname);
+                       break;
                }
-       } while (!F);
+               ind = getvar_s(incvar(intvar[ARGIND]));
+               fname = getvar_s(findvar(iamarray(intvar[ARGV]), ind));
+               if (fname && *fname && !is_assignment(fname)) {
+                       F = xfopen_stdin(fname);
+                       break;
+               }
+       }
 
        files_happen = TRUE;
        setvar_s(intvar[FILENAME], fname);
@@ -2946,9 +3142,12 @@ int awk_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 int awk_main(int argc, char **argv)
 {
        unsigned opt;
-       char *opt_F, *opt_W;
+       char *opt_F;
        llist_t *list_v = NULL;
        llist_t *list_f = NULL;
+#if ENABLE_FEATURE_AWK_GNU_EXTENSIONS
+       llist_t *list_e = NULL;
+#endif
        int i, j;
        var *v;
        var tv;
@@ -3007,45 +3206,50 @@ int awk_main(int argc, char **argv)
                        *s1 = '=';
                }
        }
-       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);
+       opt = getopt32(argv, OPTSTR_AWK, &opt_F, &list_v, &list_f, IF_FEATURE_AWK_GNU_EXTENSIONS(&list_e,) NULL);
        argv += optind;
        argc -= optind;
-       if (opt & 0x1)
-               setvar_s(intvar[FS], opt_F); // -F
-       while (list_v) { /* -v */
+       if (opt & OPT_W)
+               bb_error_msg("warning: option -W is ignored");
+       if (opt & OPT_F) {
+               unescape_string_in_place(opt_F);
+               setvar_s(intvar[FS], opt_F);
+       }
+       while (list_v) {
                if (!is_assignment(llist_pop(&list_v)))
                        bb_show_usage();
        }
-       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);
-               } while (list_f);
-               argc++;
-       } else { // no -f: take program from 1st parameter
-               if (!argc)
+       while (list_f) {
+               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);
+       }
+       g_progname = "cmd. line";
+#if ENABLE_FEATURE_AWK_GNU_EXTENSIONS
+       while (list_e) {
+               parse_program(llist_pop(&list_e));
+       }
+#endif
+       if (!(opt & (OPT_f | OPT_e))) {
+               if (!*argv)
                        bb_show_usage();
-               g_progname = "cmd. line";
                parse_program(*argv++);
+               argc--;
        }
-       if (opt & 0x8) // -W
-               bb_error_msg("warning: unrecognized option '-W %s' ignored", opt_W);
 
        /* fill in ARGV array */
-       setvar_i(intvar[ARGC], argc);
+       setvar_i(intvar[ARGC], argc + 1);
        setari_u(intvar[ARGV], 0, "awk");
        i = 0;
        while (*argv)