#include "libbb.h"
#include "xregex.h"
#include <math.h>
-extern char **environ;
/* This is a NOEXEC applet. Be very careful! */
var *pos;
struct nvblock_s *prev;
struct nvblock_s *next;
- var nv[0];
+ var nv[];
} nvblock;
typedef struct tsplitter_s {
/* 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,
};
#define OC_B OC_BUILTIN
-static const char tokenlist[] =
+static const char tokenlist[] ALIGN1 =
"\1(" NTC
"\1)" NTC
"\1/" NTC /* REGEXP */
"\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
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,
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[] =
+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[] =
+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;
-static const unsigned PRIMES[] = { 251, 1021, 4093, 16381, 65521 };
-enum { NPRIMES = sizeof(PRIMES) / sizeof(PRIMES[0]) };
+#define FIRST_PRIME 61
+static const uint16_t PRIMES[] ALIGN2 = { 251, 1021, 4093, 16381, 65521 };
-/* globals */
+/* Globals. Split in two parts so that first one is addressed
+ * 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 {
- /* former 'struct t' */
- uint32_t t_info; /* often used */
- uint32_t t_tclass;
- char *t_string;
double t_double;
- int t_lineno;
- int t_rollback;
-
- /* the rest */
- smallint icase;
- smallint exiting;
- smallint nextrec;
- smallint nextfile;
- smallint is_f0_split;
- chain beginseq, mainseq, endseq, *seq;
+ chain beginseq, mainseq, endseq;
+ chain *seq;
node *break_ptr, *continue_ptr;
rstream *iF;
xhash *vhash, *ahash, *fdhash, *fnhash;
nvblock *g_cb;
char *g_pos;
char *g_buf;
+ smallint icase;
+ smallint exiting;
+ smallint nextrec;
+ smallint nextfile;
+ smallint is_f0_split;
+};
+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 */
/* former statics from various functions */
char *split_f0__fstrings;
- rstream next_input_file__rsm;
- smallint next_input_file__files_happen;
-
- smallint next_token__concat_inserted;
uint32_t next_token__save_tclass;
uint32_t next_token__save_info;
uint32_t next_token__ltclass;
+ smallint next_token__concat_inserted;
+
+ smallint next_input_file__files_happen;
+ rstream next_input_file__rsm;
var *evaluate__fnargs;
unsigned evaluate__seed;
tsplitter exec_builtin__tspl;
- /* biggest members go last */
- var *intvar[NUM_INTERNAL_VARS];
+ /* biggest and least used members go last */
tsplitter fsplitter, rsplitter;
};
-#define G (*ptr_to_globals)
-/* for debug */
-/* char Gsize[sizeof(G)]; ~0x240 */
+#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)]; - 0x74 */
+/*char Gsize[sizeof(G)]; - 0x1c4 */
/* Trying to keep most of members accessible with short offsets: */
-/* char Gofs_seed[offsetof(struct globals, evaluate__seed)]; ~0xc0 */
+/*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 )
+#define seq (G1.seq )
+#define break_ptr (G1.break_ptr )
+#define continue_ptr (G1.continue_ptr)
+#define iF (G1.iF )
+#define vhash (G1.vhash )
+#define ahash (G1.ahash )
+#define fdhash (G1.fdhash )
+#define fnhash (G1.fnhash )
+#define g_progname (G1.g_progname )
+#define g_lineno (G1.g_lineno )
+#define nfields (G1.nfields )
+#define maxfields (G1.maxfields )
+#define Fields (G1.Fields )
+#define g_cb (G1.g_cb )
+#define g_pos (G1.g_pos )
+#define g_buf (G1.g_buf )
+#define icase (G1.icase )
+#define exiting (G1.exiting )
+#define nextrec (G1.nextrec )
+#define nextfile (G1.nextfile )
+#define is_f0_split (G1.is_f0_split )
#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 icase (G.icase )
-#define exiting (G.exiting )
-#define nextrec (G.nextrec )
-#define nextfile (G.nextfile )
-#define is_f0_split (G.is_f0_split )
-#define beginseq (G.beginseq )
-#define mainseq (G.mainseq )
-#define endseq (G.endseq )
-#define seq (G.seq )
-#define break_ptr (G.break_ptr )
-#define continue_ptr (G.continue_ptr)
-#define iF (G.iF )
-#define vhash (G.vhash )
-#define ahash (G.ahash )
-#define fdhash (G.fdhash )
-#define fnhash (G.fnhash )
-#define g_progname (G.g_progname )
-#define g_lineno (G.g_lineno )
-#define nfields (G.nfields )
-#define maxfields (G.maxfields )
-#define Fields (G.Fields )
-#define g_cb (G.g_cb )
-#define g_pos (G.g_pos )
-#define g_buf (G.g_buf )
#define intvar (G.intvar )
#define fsplitter (G.fsplitter )
#define rsplitter (G.rsplitter )
#define INIT_G() do { \
- PTR_TO_GLOBALS = xzalloc(sizeof(G)); \
+ SET_PTR_TO_GLOBALS((char*)xzalloc(sizeof(G1)+sizeof(G)) + sizeof(G1)); \
G.next_token__ltclass = TC_OPTERM; \
G.evaluate__seed = 1; \
} while (0)
static var *evaluate(node *, var *);
static rstream *next_input_file(void);
static int fmt_num(char *, int, const char *, double, int);
-static int awk_exit(int) ATTRIBUTE_NORETURN;
+static int awk_exit(int) NORETURN;
/* ---- error handling ---- */
-static const char EMSG_INTERNAL_ERROR[] = "Internal error";
-static const char EMSG_UNEXP_EOS[] = "Unexpected end of string";
-static const char EMSG_UNEXP_TOKEN[] = "Unexpected token";
-static const char EMSG_DIV_BY_ZERO[] = "Division by zero";
-static const char EMSG_INV_FMT[] = "Invalid format specifier";
-static const char EMSG_TOO_FEW_ARGS[] = "Too few arguments for builtin";
-static const char EMSG_NOT_ARRAY[] = "Not an array";
-static const char EMSG_POSSIBLE_ERROR[] = "Possible syntax error";
-static const char EMSG_UNDEF_FUNC[] = "Call to undefined function";
-#if !ENABLE_FEATURE_AWK_MATH
-static const char EMSG_NO_MATH[] = "Math support is not compiled in";
+static const char EMSG_INTERNAL_ERROR[] ALIGN1 = "Internal error";
+static const char EMSG_UNEXP_EOS[] ALIGN1 = "Unexpected end of string";
+static const char EMSG_UNEXP_TOKEN[] ALIGN1 = "Unexpected token";
+static const char EMSG_DIV_BY_ZERO[] ALIGN1 = "Division by zero";
+static const char EMSG_INV_FMT[] ALIGN1 = "Invalid format specifier";
+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)
memset(vp, 0, sizeof(*vp));
}
-static void syntax_error(const char * const message) ATTRIBUTE_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);
}
unsigned newsize, i, idx;
hash_item **newitems, *hi, *thi;
- if (hash->nprime == NPRIMES)
+ if (hash->nprime == ARRAY_SIZE(PRIMES))
return;
newsize = PRIMES[hash->nprime++];
int l;
hi = hash_search(hash, name);
- if (! hi) {
+ if (!hi) {
if (++hash->nel / hash->csize > 10)
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];
return c;
}
-static int ATTRIBUTE_ALWAYS_INLINE isalnum_(int c)
+static ALWAYS_INLINE int isalnum_(int c)
{
return (isalnum(c) || c == '_');
}
-static FILE *afopen(const char *path, const char *mode)
+static double my_strtod(char **pp)
{
- return (*path == '-' && *(path+1) == '\0') ? stdin : xfopen(path, mode);
+#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) -------- */
/* 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);
}
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')
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) {
static var *incvar(var *v)
{
- return setvar_i(v, getvar_i(v)+1.);
+ return setvar_i(v, getvar_i(v) + 1.);
}
/* return true if v is number or numeric string */
if (!g_cb) {
size = (n <= MINNVBLOCK) ? MINNVBLOCK : n;
- g_cb = xmalloc(sizeof(nvblock) + size * sizeof(var));
+ g_cb = xzalloc(sizeof(nvblock) + size * sizeof(var));
g_cb->size = size;
g_cb->pos = g_cb->nv;
g_cb->prev = pb;
- g_cb->next = NULL;
+ /*g_cb->next = NULL; - xzalloc did it */
if (pb) pb->next = g_cb;
}
} 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;
n3 = parse_expr(TC_SEQTERM);
n = chain_loop(n3);
n->l.n = n2;
- if (! n2)
+ if (!n2)
n->info = OC_EXEC;
}
break;
n = &spl->n;
if ((n->info & OPCLSMASK) == OC_REGEXP) {
regfree(re);
- regfree(ire);
+ regfree(ire); // TODO: nuke ire, use re+1?
}
if (strlen(s) > 1) {
mk_re_node(s, n, re);
*/
static regex_t *as_regex(node *op, regex_t *preg)
{
+ int cflags;
var *v;
const char *s;
}
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;
}
/* gradually increasing buffer */
static void qrealloc(char **b, int n, int *size)
{
- if (!*b || n >= *size)
- *b = xrealloc(*b, *size = n + (n>>1) + 80);
+ if (!*b || n >= *size) {
+ *size = n + (n>>1) + 80;
+ *b = xrealloc(*b, *size);
+ }
}
/* resize field storage space */
int l, n = 0;
char c[4];
char *s1;
- regmatch_t pmatch[2];
+ regmatch_t pmatch[2]; // TODO: why [2]? [1] is enough...
/* in worst case, each char would be a separate field */
*slist = s1 = xzalloc(strlen(s) * 2 + 3);
if (*getvar_s(intvar[RS]) == '\0')
c[2] = '\n';
- if ((spl->info & OPCLSMASK) == OC_REGEXP) { /* regex split */
- while (*s) {
- l = strcspn(s, c+2);
+ if ((spl->info & OPCLSMASK) == OC_REGEXP) { /* regex split */
+ if (!*s)
+ return n; /* "": zero fields */
+ n++; /* at least one field will be there */
+ do {
+ l = strcspn(s, c+2); /* len till next NUL or \n */
if (regexec(icase ? spl->r.ire : spl->l.re, s, 1, pmatch, 0) == 0
&& pmatch[0].rm_so <= l
) {
l++;
pmatch[0].rm_eo++;
}
+ 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;
- n++;
- }
- } else if (c[0] == '\0') { /* null split */
+ } while (*s);
+ return n;
+ }
+ if (c[0] == '\0') { /* null split */
while (*s) {
*s1++ = *s++;
*s1++ = '\0';
n++;
}
- } else if (c[0] != ' ') { /* single-character split */
+ return n;
+ }
+ if (c[0] != ' ') { /* single-character split */
if (icase) {
c[0] = toupper(c[0]);
c[1] = tolower(c[1]);
*s1++ = '\0';
n++;
}
- } else { /* space split */
- while (*s) {
- s = skip_whitespace(s);
- if (!*s) break;
- n++;
- while (*s && !isspace(*s))
- *s1++ = *s++;
- *s1++ = '\0';
- }
+ return n;
+ }
+ /* space split */
+ while (*s) {
+ s = skip_whitespace(s);
+ if (!*s) break;
+ n++;
+ while (*s && !isspace(*s))
+ *s1++ = *s++;
+ *s1++ = '\0';
}
return n;
}
static void split_f0(void)
{
+/* static char *fstrings; */
#define fstrings (G.split_f0__fstrings)
int i, n;
{
char **w;
hash_item *hi;
- int i;
+ unsigned i;
if (v->type & VF_WALK)
free(v->x.walker);
c = (char) rsplitter.n.info;
rp = 0;
- if (! m) qrealloc(&m, 256, &size);
+ if (!m) qrealloc(&m, 256, &size);
do {
b = m + a;
so = eo = p;
}
} else if (c != '\0') {
s = strchr(b+pp, c);
- if (! s) s = memchr(b+pp, '\0', p - pp);
+ if (!s) s = memchr(b+pp, '\0', p - pp);
if (s) {
so = eo = s-b;
eo++;
return r;
}
-
/* formatted output into an allocated buffer, return ptr to buffer */
static char *awk_printf(node *n)
{
regex_t sreg, *re;
re = as_regex(rn, &sreg);
- if (! src) src = intvar[F0];
- if (! dest) dest = intvar[F0];
+ if (!src) src = intvar[F0];
+ if (!dest) dest = intvar[F0];
i = di = 0;
sp = getvar_s(src);
sp += eo;
if (i == nm) break;
if (eo == so) {
- if (! (ds[di++] = *sp++)) break;
+ ds[di] = *sp++;
+ if (!ds[di++]) break;
}
}
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];
}
nargs = i;
- if (nargs < (info >> 30))
+ 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
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);
if (i < 0) i = 0;
n = (nargs > 2) ? getvar_i(av[2]) : l-i;
if (n < 0) n = 0;
- s = xmalloc(n+1);
- strncpy(s, as[0]+i, n);
- s[n] = '\0';
+ s = xstrndup(as[0]+i, n);
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);
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);
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 {
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;
break;
case F_sy:
- fflush(NULL);
+ fflush_all();
R.d = (ENABLE_FEATURE_ALLOW_EXEC && L.s && *L.s)
? (system(L.s) >> 8) : 0;
break;
X.rsm = newfile(L.s);
fflush(X.rsm->F);
} else {
- fflush(NULL);
+ fflush_all();
}
}
break;
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);
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);
#undef files_happen
}
-int awk_main(int argc, char **argv);
+int awk_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
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;
/* Huh, people report that sometimes environ is NULL. Oh well. */
if (environ) for (envp = environ; *envp; envp++) {
- char *s = xstrdup(*envp);
+ /* environ is writable, thus we don't strdup it needlessly */
+ char *s = *envp;
char *s1 = strchr(s, '=');
if (s1) {
- *s1++ = '\0';
- setvar_u(findvar(iamarray(intvar[ENVIRON]), s), s1);
+ *s1 = '\0';
+ /* Both findvar and setvar_u take const char*
+ * as 2nd arg -> environment is not trashed */
+ setvar_u(findvar(iamarray(intvar[ENVIRON]), s), s1 + 1);
+ *s1 = '=';
}
- free(s);
}
- opt_complementary = "v::";
- opt = getopt32(argc, 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);
+ 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)