traceroute: fix help text to not show -6 when traceroute6 is off
[oweals/busybox.git] / editors / awk.c
index 7af9e1eeb0a27c8046deb5abe8dd1bf519feb3bc..ce6c9b726d80c4cefb74d48dc4e79ecbe69a442f 100644 (file)
@@ -114,7 +114,7 @@ typedef struct nvblock_s {
        var *pos;
        struct nvblock_s *prev;
        struct nvblock_s *next;
-       var nv[0];
+       var nv[];
 } nvblock;
 
 typedef struct tsplitter_s {
@@ -250,7 +250,7 @@ enum {
 
 /* builtins */
 enum {
-       B_a2,   B_ix,   B_ma,   B_sp,   B_ss,   B_ti,   B_lo,   B_up,
+       B_a2,   B_ix,   B_ma,   B_sp,   B_ss,   B_ti,   B_mt,   B_lo,   B_up,
        B_ge,   B_gs,   B_su,
        B_an,   B_co,   B_ls,   B_or,   B_rs,   B_xo,
 };
@@ -299,7 +299,7 @@ static const char tokenlist[] ALIGN1 =
        "\4rand"    "\3sin"     "\4sqrt"    "\5srand"
        "\6gensub"  "\4gsub"    "\5index"   "\6length"
        "\5match"   "\5split"   "\7sprintf" "\3sub"
-       "\6substr"  "\7systime" "\10strftime"
+       "\6substr"  "\7systime" "\10strftime" "\6mktime"
        "\7tolower" "\7toupper" NTC
        "\7getline" NTC
        "\4func"    "\10function"   NTC
@@ -353,7 +353,7 @@ static const uint32_t tokeninfo[] = {
        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_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_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_GETLINE|SV|P(0),
        0,      0,
@@ -366,25 +366,22 @@ static const uint32_t tokeninfo[] = {
 enum {
        CONVFMT,    OFMT,       FS,         OFS,
        ORS,        RS,         RT,         FILENAME,
-       SUBSEP,     ARGIND,     ARGC,       ARGV,
-       ERRNO,      FNR,
-       NR,         NF,         IGNORECASE,
-       ENVIRON,    F0,         NUM_INTERNAL_VARS
+       SUBSEP,     F0,         ARGIND,     ARGC,
+       ARGV,       ERRNO,      FNR,        NR,
+       NF,         IGNORECASE, ENVIRON,    NUM_INTERNAL_VARS
 };
 
 static const char vNames[] ALIGN1 =
        "CONVFMT\0" "OFMT\0"    "FS\0*"     "OFS\0"
        "ORS\0"     "RS\0*"     "RT\0"      "FILENAME\0"
-       "SUBSEP\0"  "ARGIND\0"  "ARGC\0"    "ARGV\0"
-       "ERRNO\0"   "FNR\0"
-       "NR\0"      "NF\0*"     "IGNORECASE\0*"
-       "ENVIRON\0" "$\0*"      "\0";
+       "SUBSEP\0"  "$\0*"      "ARGIND\0"  "ARGC\0"
+       "ARGV\0"    "ERRNO\0"   "FNR\0"     "NR\0"
+       "NF\0*"     "IGNORECASE\0*" "ENVIRON\0" "\0";
 
 static const char vValues[] ALIGN1 =
        "%.6g\0"    "%.6g\0"    " \0"       " \0"
        "\n\0"      "\n\0"      "\0"        "\0"
-       "\034\0"
-       "\377";
+       "\034\0"    "\0"        "\377";
 
 /* hash size may grow to these values */
 #define FIRST_PRIME 61
@@ -392,8 +389,12 @@ static const uint16_t PRIMES[] ALIGN2 = { 251, 1021, 4093, 16381, 65521 };
 
 
 /* Globals. Split in two parts so that first one is addressed
- * with (mostly short) negative offsets */
+ * with (mostly short) negative offsets.
+ * NB: it's unsafe to put members of type "double"
+ * into globals2 (gcc may fail to align them).
+ */
 struct globals {
+       double t_double;
        chain beginseq, mainseq, endseq;
        chain *seq;
        node *break_ptr, *continue_ptr;
@@ -442,16 +443,16 @@ struct globals2 {
        tsplitter exec_builtin__tspl;
 
        /* biggest and least used members go last */
-       double t_double;
        tsplitter fsplitter, rsplitter;
 };
 #define G1 (ptr_to_globals[-1])
 #define G (*(struct globals2 *)ptr_to_globals)
 /* For debug. nm --size-sort awk.o | grep -vi ' [tr] ' */
-/* char G1size[sizeof(G1)]; - 0x6c */
-/* char Gsize[sizeof(G)]; - 0x1cc */
+/*char G1size[sizeof(G1)]; - 0x74 */
+/*char Gsize[sizeof(G)]; - 0x1c4 */
 /* Trying to keep most of members accessible with short offsets: */
-/* char Gofs_seed[offsetof(struct globals2, evaluate__seed)]; - 0x90 */
+/*char Gofs_seed[offsetof(struct globals2, evaluate__seed)]; - 0x90 */
+#define t_double     (G1.t_double    )
 #define beginseq     (G1.beginseq    )
 #define mainseq      (G1.mainseq     )
 #define endseq       (G1.endseq      )
@@ -479,14 +480,13 @@ struct globals2 {
 #define t_info       (G.t_info      )
 #define t_tclass     (G.t_tclass    )
 #define t_string     (G.t_string    )
-#define t_double     (G.t_double    )
 #define t_lineno     (G.t_lineno    )
 #define t_rollback   (G.t_rollback  )
 #define intvar       (G.intvar      )
 #define fsplitter    (G.fsplitter   )
 #define rsplitter    (G.rsplitter   )
 #define INIT_G() do { \
-       SET_PTR_TO_GLOBALS(xzalloc(sizeof(G1) + sizeof(G)) + sizeof(G1)); \
+       SET_PTR_TO_GLOBALS((char*)xzalloc(sizeof(G1)+sizeof(G)) + sizeof(G1)); \
        G.next_token__ltclass = TC_OPTERM; \
        G.evaluate__seed = 1; \
 } while (0)
@@ -512,7 +512,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_MATH
+#if !ENABLE_FEATURE_AWK_LIBM
 static const char EMSG_NO_MATH[] ALIGN1 = "Math support is not compiled in";
 #endif
 
@@ -521,8 +521,8 @@ static void zero_out_var(var * vp)
        memset(vp, 0, sizeof(*vp));
 }
 
-static void syntax_error(const char *const message) NORETURN;
-static void syntax_error(const char *const message)
+static void syntax_error(const char *message) NORETURN;
+static void syntax_error(const char *message)
 {
        bb_error_msg_and_die("%s:%i: %s", g_progname, g_lineno, message);
 }
@@ -604,8 +604,8 @@ static void *hash_find(xhash *hash, const char *name)
                        hash_rebuild(hash);
 
                l = strlen(name) + 1;
-               hi = xzalloc(sizeof(hash_item) + l);
-               memcpy(hi->name, name, l);
+               hi = xzalloc(sizeof(*hi) + l);
+               strcpy(hi->name, name);
 
                idx = hashidx(name) % hash->csize;
                hi->next = hash->items[idx];
@@ -681,6 +681,18 @@ static ALWAYS_INLINE int isalnum_(int c)
        return (isalnum(c) || 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);
+       }
+#endif
+       return strtod(*pp, pp);
+}
+
 /* -------- working with variables (set/get/copy/etc) -------- */
 
 static xhash *iamarray(var *v)
@@ -753,11 +765,9 @@ static var *setvar_u(var *v, const char *value)
 /* set array element to user string */
 static void setari_u(var *a, int idx, const char *s)
 {
-       char sidx[sizeof(int)*3 + 1];
        var *v;
 
-       sprintf(sidx, "%d", idx);
-       v = findvar(iamarray(a), sidx);
+       v = findvar(iamarray(a), itoa(idx));
        setvar_u(v, s);
 }
 
@@ -790,7 +800,7 @@ static double getvar_i(var *v)
                v->number = 0;
                s = v->string;
                if (s && *s) {
-                       v->number = strtod(s, &s);
+                       v->number = my_strtod(&s);
                        if (v->type & VF_USER) {
                                skip_spaces(&s);
                                if (*s != '\0')
@@ -804,6 +814,19 @@ static double getvar_i(var *v)
        return v->number;
 }
 
+/* Used for operands of bitwise ops */
+static unsigned long getvar_i_int(var *v)
+{
+       double d = getvar_i(v);
+
+       /* Casting doubles to longs is undefined for values outside
+        * of target type range. Try to widen it as much as possible */
+       if (d >= 0)
+               return (unsigned long)d;
+       /* Why? Think about d == -4294967295.0 (assuming 32bit longs) */
+       return - (long) (unsigned long) (-d);
+}
+
 static var *copyvar(var *dest, const var *src)
 {
        if (dest != src) {
@@ -973,7 +996,7 @@ static uint32_t next_token(uint32_t expected)
 
                } else if (*p == '.' || isdigit(*p)) {
                        /* it's a number */
-                       t_double = strtod(p, &p);
+                       t_double = my_strtod(&p);
                        if (*p == '.')
                                syntax_error(EMSG_UNEXP_TOKEN);
                        tc = TC_NUMBER;
@@ -1457,6 +1480,7 @@ static node *mk_splitter(const char *s, tsplitter *spl)
  */
 static regex_t *as_regex(node *op, regex_t *preg)
 {
+       int cflags;
        var *v;
        const char *s;
 
@@ -1465,7 +1489,17 @@ static regex_t *as_regex(node *op, regex_t *preg)
        }
        v = nvalloc(1);
        s = getvar_s(evaluate(op, v));
-       xregcomp(preg, s, icase ? REG_EXTENDED | REG_ICASE : REG_EXTENDED);
+
+       cflags = icase ? REG_EXTENDED | REG_ICASE : REG_EXTENDED;
+       /* Testcase where REG_EXTENDED fails (unpaired '{'):
+        * echo Hi | awk 'gsub("@(samp|code|file)\{","");'
+        * gawk 3.1.5 eats this. We revert to ~REG_EXTENDED
+        * (maybe gsub is not supposed to use REG_EXTENDED?).
+        */
+       if (regcomp(preg, s, cflags)) {
+               cflags &= ~REG_EXTENDED;
+               xregcomp(preg, s, cflags);
+       }
        nvfree(v);
        return preg;
 }
@@ -1535,10 +1569,14 @@ static int awk_split(const char *s, node *spl, char **slist)
                                n++; /* we saw yet another delimiter */
                        } else {
                                pmatch[0].rm_eo = l;
-                               if (s[l]) pmatch[0].rm_eo++;
+                               if (s[l])
+                                       pmatch[0].rm_eo++;
                        }
                        memcpy(s1, s, l);
-                       s1[l] = '\0';
+                       /* make sure we remove *all* of the separator chars */
+                       do {
+                               s1[l] = '\0';
+                       } while (++l < pmatch[0].rm_eo);
                        nextword(&s1);
                        s += pmatch[0].rm_eo;
                } while (*s);
@@ -1831,7 +1869,6 @@ static int fmt_num(char *b, int size, const char *format, double n, int int_as_i
        return r;
 }
 
-
 /* formatted output into an allocated buffer, return ptr to buffer */
 static char *awk_printf(node *n)
 {
@@ -1962,11 +1999,38 @@ static int awk_sub(node *rn, const char *repl, int nm, var *src, var *dest, int
        return i;
 }
 
-static var *exec_builtin(node *op, var *res)
+static NOINLINE int do_mktime(const char *ds)
+{
+       struct tm then;
+       int count;
+
+       /*memset(&then, 0, sizeof(then)); - not needed */
+       then.tm_isdst = -1; /* default is unknown */
+
+       /* manpage of mktime says these fields are ints,
+        * so we can sscanf stuff directly into them */
+       count = sscanf(ds, "%u %u %u %u %u %u %d",
+               &then.tm_year, &then.tm_mon, &then.tm_mday,
+               &then.tm_hour, &then.tm_min, &then.tm_sec,
+               &then.tm_isdst);
+
+       if (count < 6
+        || (unsigned)then.tm_mon < 1
+        || (unsigned)then.tm_year < 1900
+       ) {
+               return -1;
+       }
+
+       then.tm_mon -= 1;
+       then.tm_year -= 1900;
+
+       return mktime(&then);
+}
+
+static NOINLINE var *exec_builtin(node *op, var *res)
 {
 #define tspl (G.exec_builtin__tspl)
 
-       int (*to_xxx)(int);
        var *tv;
        node *an[4];
        var *av[4];
@@ -1996,11 +2060,12 @@ static var *exec_builtin(node *op, var *res)
        if ((uint32_t)nargs < (info >> 30))
                syntax_error(EMSG_TOO_FEW_ARGS);
 
-       switch (info & OPNMASK) {
+       info &= OPNMASK;
+       switch (info) {
 
        case B_a2:
-#if ENABLE_FEATURE_AWK_MATH
-               setvar_i(res, atan2(getvar_i(av[i]), getvar_i(av[1])));
+#if ENABLE_FEATURE_AWK_LIBM
+               setvar_i(res, atan2(getvar_i(av[0]), getvar_i(av[1])));
 #else
                syntax_error(EMSG_NO_MATH);
 #endif
@@ -2017,7 +2082,7 @@ static var *exec_builtin(node *op, var *res)
                n = awk_split(as[0], spl, &s);
                s1 = s;
                clear_array(iamarray(av[1]));
-               for (i=1; i<=n; i++)
+               for (i = 1; i <= n; i++)
                        setari_u(av[1], i, nextword(&s1));
                free(s);
                setvar_i(res, n);
@@ -2034,40 +2099,39 @@ static var *exec_builtin(node *op, var *res)
                setvar_p(res, s);
                break;
 
+       /* Bitwise ops must assume that operands are unsigned. GNU Awk 3.1.5:
+        * awk '{ print or(-1,1) }' gives "4.29497e+09", not "-2.xxxe+09" */
        case B_an:
-               setvar_i(res, (long)getvar_i(av[0]) & (long)getvar_i(av[1]));
+               setvar_i(res, getvar_i_int(av[0]) & getvar_i_int(av[1]));
                break;
 
        case B_co:
-               setvar_i(res, ~(long)getvar_i(av[0]));
+               setvar_i(res, ~getvar_i_int(av[0]));
                break;
 
        case B_ls:
-               setvar_i(res, (long)getvar_i(av[0]) << (long)getvar_i(av[1]));
+               setvar_i(res, getvar_i_int(av[0]) << getvar_i_int(av[1]));
                break;
 
        case B_or:
-               setvar_i(res, (long)getvar_i(av[0]) | (long)getvar_i(av[1]));
+               setvar_i(res, getvar_i_int(av[0]) | getvar_i_int(av[1]));
                break;
 
        case B_rs:
-               setvar_i(res, (long)((unsigned long)getvar_i(av[0]) >> (unsigned long)getvar_i(av[1])));
+               setvar_i(res, getvar_i_int(av[0]) >> getvar_i_int(av[1]));
                break;
 
        case B_xo:
-               setvar_i(res, (long)getvar_i(av[0]) ^ (long)getvar_i(av[1]));
+               setvar_i(res, getvar_i_int(av[0]) ^ getvar_i_int(av[1]));
                break;
 
        case B_lo:
-               to_xxx = tolower;
-               goto lo_cont;
-
        case B_up:
-               to_xxx = toupper;
- lo_cont:
                s1 = s = xstrdup(as[0]);
                while (*s1) {
-                       *s1 = (*to_xxx)(*s1);
+                       //*s1 = (info == B_up) ? toupper(*s1) : tolower(*s1);
+                       if ((unsigned char)((*s1 | 0x20) - 'a') <= ('z' - 'a'))
+                               *s1 = (info == B_up) ? (*s1 & 0xdf) : (*s1 | 0x20);
                        s1++;
                }
                setvar_p(res, s);
@@ -2109,6 +2173,10 @@ static var *exec_builtin(node *op, var *res)
                setvar_s(res, g_buf);
                break;
 
+       case B_mt:
+               setvar_i(res, do_mktime(as[0]));
+               break;
+
        case B_ma:
                re = as_regex(an[1], &sreg);
                n = regexec(re, as[0], 1, pmatch, 0);
@@ -2400,7 +2468,7 @@ static var *evaluate(node *op, var *res)
                                                X.rsm->F = popen(L.s, "r");
                                                X.rsm->is_pipe = TRUE;
                                        } else {
-                                               X.rsm->F = fopen(L.s, "r");             /* not xfopen! */
+                                               X.rsm->F = fopen_for_read(L.s);         /* not xfopen! */
                                        }
                                }
                        } else {
@@ -2438,7 +2506,7 @@ static var *evaluate(node *op, var *res)
                        case F_rn:
                                R.d = (double)rand() / (double)RAND_MAX;
                                break;
-#if ENABLE_FEATURE_AWK_MATH
+#if ENABLE_FEATURE_AWK_LIBM
                        case F_co:
                                R.d = cos(L.d);
                                break;
@@ -2484,7 +2552,7 @@ static var *evaluate(node *op, var *res)
                                break;
 
                        case F_sy:
-                               fflush(NULL);
+                               fflush_all();
                                R.d = (ENABLE_FEATURE_ALLOW_EXEC && L.s && *L.s)
                                                ? (system(L.s) >> 8) : 0;
                                break;
@@ -2497,7 +2565,7 @@ static var *evaluate(node *op, var *res)
                                                X.rsm = newfile(L.s);
                                                fflush(X.rsm->F);
                                        } else {
-                                               fflush(NULL);
+                                               fflush_all();
                                        }
                                }
                                break;
@@ -2606,7 +2674,7 @@ static var *evaluate(node *op, var *res)
                                L.d /= R.d;
                                break;
                        case '&':
-#if ENABLE_FEATURE_AWK_MATH
+#if ENABLE_FEATURE_AWK_LIBM
                                L.d = pow(L.d, R.d);
 #else
                                syntax_error(EMSG_NO_MATH);
@@ -2839,18 +2907,18 @@ int awk_main(int argc, char **argv)
                        parse_program(s + 1);
                        free(s);
                } while (list_f);
+               argc++;
        } else { // no -f: take program from 1st parameter
                if (!argc)
                        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 + 1);
+       setvar_i(intvar[ARGC], argc);
        setari_u(intvar[ARGV], 0, "awk");
        i = 0;
        while (*argv)