aboutsummaryrefslogtreecommitdiffstats
path: root/builtin.c
diff options
context:
space:
mode:
Diffstat (limited to 'builtin.c')
-rw-r--r--builtin.c331
1 files changed, 165 insertions, 166 deletions
diff --git a/builtin.c b/builtin.c
index 6ca6509d..6e7e78bd 100644
--- a/builtin.c
+++ b/builtin.c
@@ -73,7 +73,7 @@ extern void srandom(unsigned long seed);
extern NODE **args_array;
extern int max_args;
extern NODE **fields_arr;
-extern int output_is_tty;
+extern bool output_is_tty;
extern FILE *output_fp;
@@ -83,7 +83,7 @@ s1 = POP(); \
do { if (s1->type == Node_var_array) { \
DEREF(s2); \
fatal(_("attempt to use array `%s' in a scalar context"), array_vname(s1)); \
-}} while (FALSE)
+}} while (false)
/*
@@ -92,9 +92,6 @@ fatal(_("attempt to use array `%s' in a scalar context"), array_vname(s1)); \
*/
#define GAWK_RANDOM_MAX 0x7fffffffL
-static void efwrite(const void *ptr, size_t size, size_t count, FILE *fp,
- const char *from, struct redirect *rp, int flush);
-
/* efwrite --- like fwrite, but with error checking */
static void
@@ -104,17 +101,26 @@ efwrite(const void *ptr,
FILE *fp,
const char *from,
struct redirect *rp,
- int flush)
+ bool flush)
{
errno = 0;
- if (fwrite(ptr, size, count, fp) != count)
+ if (rp != NULL) {
+ if (rp->output.gawk_fwrite(ptr, size, count, fp, rp->output.opaque) != count)
+ goto wrerror;
+ } else if (fwrite(ptr, size, count, fp) != count)
goto wrerror;
if (flush
&& ((fp == stdout && output_is_tty)
|| (rp != NULL && (rp->flag & RED_NOBUF)))) {
- fflush(fp);
- if (ferror(fp))
- goto wrerror;
+ if (rp != NULL) {
+ rp->output.gawk_fflush(fp, rp->output.opaque);
+ if (rp->output.gawk_ferror(fp, rp->output.opaque))
+ goto wrerror;
+ } else {
+ fflush(fp);
+ if (ferror(fp))
+ goto wrerror;
+ }
}
return;
@@ -207,9 +213,9 @@ do_fflush(int nargs)
DEREF(tmp);
return make_number((AWKNUM) status);
}
- fp = rp->fp;
+ fp = rp->output.fp;
if (fp != NULL)
- status = fflush(fp);
+ status = rp->output.gawk_fflush(fp, rp->output.opaque);
} else if ((fp = stdfile(tmp->stptr, tmp->stlen)) != NULL) {
status = fflush(fp);
} else {
@@ -319,7 +325,7 @@ do_index(int nargs)
size_t l1, l2;
long ret;
#if MBS_SUPPORT
- int do_single_byte = FALSE;
+ bool do_single_byte = false;
mbstate_t mbs1, mbs2;
if (gawk_mb_cur_max > 1) {
@@ -491,13 +497,13 @@ do_length(int nargs)
tmp = POP();
if (tmp->type == Node_var_array) {
- static short warned = FALSE;
+ static bool warned = false;
unsigned long size;
if (do_posix)
fatal(_("length: received array argument"));
if (do_lint && ! warned) {
- warned = TRUE;
+ warned = true;
lintwarn(_("`length(array)' is a gawk extension"));
}
@@ -569,7 +575,7 @@ mpz2mpfr(mpz_ptr zi)
{
size_t prec;
static mpfr_t mpfrval;
- static int inited = FALSE;
+ static bool inited = false;
int tval;
/* estimate minimum precision for exact conversion */
@@ -582,7 +588,7 @@ mpz2mpfr(mpz_ptr zi)
if (! inited) {
mpfr_init2(mpfrval, prec);
- inited = TRUE;
+ inited = true;
} else
mpfr_set_prec(mpfrval, prec);
tval = mpfr_set_z(mpfrval, zi, ROUND_MODE);
@@ -649,7 +655,7 @@ format_tree(
size_t cur_arg = 0;
NODE *r = NULL;
int i, nc;
- int toofew = FALSE;
+ bool toofew = false;
char *obuf, *obufout;
size_t osiz, ofre;
const char *chbuf;
@@ -657,11 +663,11 @@ format_tree(
int cs1;
NODE *arg;
long fw, prec, argnum;
- int used_dollar;
- int lj, alt, big_flag, bigbig_flag, small_flag, have_prec, need_format;
+ bool used_dollar;
+ bool lj, alt, big_flag, bigbig_flag, small_flag, have_prec, need_format;
long *cur = NULL;
uintmax_t uval;
- int sgn;
+ bool sgn;
int base;
/*
* Although this is an array, the elements serve two different
@@ -681,10 +687,10 @@ format_tree(
char *cp;
const char *fill;
AWKNUM tmpval = 0.0;
- char signchar = FALSE;
+ char signchar = '\0';
size_t len;
- int zero_flag = FALSE;
- int quote_flag = FALSE;
+ bool zero_flag = false;
+ bool quote_flag = false;
int ii, jj;
char *chp;
size_t copy_count, char_count;
@@ -756,7 +762,7 @@ format_tree(
goto out; \
} else if (cur_arg >= num_args) { \
arg = 0; /* shutup the compiler */ \
- toofew = TRUE; \
+ toofew = true; \
break; \
} else { \
arg = the_args[cur_arg]; \
@@ -764,8 +770,8 @@ format_tree(
} \
}
- need_format = FALSE;
- used_dollar = FALSE;
+ need_format = false;
+ used_dollar = false;
s0 = s1 = fmt_string;
while (n0-- > 0) {
@@ -773,7 +779,7 @@ format_tree(
s1++;
continue;
}
- need_format = TRUE;
+ need_format = true;
bchunk(s0, s1 - s0);
s0 = s1;
cur = &fw;
@@ -782,17 +788,17 @@ format_tree(
base = 0;
argnum = 0;
base = 0;
- have_prec = FALSE;
- signchar = FALSE;
- zero_flag = FALSE;
- quote_flag = FALSE;
+ have_prec = false;
+ signchar = '\0';
+ zero_flag = false;
+ quote_flag = false;
#ifdef HAVE_MPFR
mf = NULL;
zi = NULL;
#endif
fmt_type = 0;
- lj = alt = big_flag = bigbig_flag = small_flag = FALSE;
+ lj = alt = big_flag = bigbig_flag = small_flag = false;
fill = sp;
cp = cend;
chbuf = lchbuf;
@@ -809,7 +815,7 @@ check_pos:
break; /* reject as a valid format */
goto retry;
case '%':
- need_format = FALSE;
+ need_format = false;
/*
* 29 Oct. 2002:
* The C99 standard pages 274 and 279 seem to imply that
@@ -841,7 +847,7 @@ check_pos:
* screws up floating point formatting.
*/
if (cur == & fw)
- zero_flag = TRUE;
+ zero_flag = true;
if (lj)
goto retry;
/* FALL through */
@@ -868,7 +874,7 @@ check_pos:
*cur = *cur * 10 + *s1++ - '0';
}
if (prec < 0) /* negative precision is discarded */
- have_prec = FALSE;
+ have_prec = false;
if (cur == &prec)
cur = NULL;
if (n0 == 0) /* badly formatted control string */
@@ -883,7 +889,7 @@ check_pos:
if (cur == &fw) {
argnum = fw;
fw = 0;
- used_dollar = TRUE;
+ used_dollar = true;
if (argnum <= 0) {
msg(_("fatal: arg count with `$' must be > 0"));
goto out;
@@ -916,7 +922,7 @@ check_pos:
n0--;
}
if (val >= num_args) {
- toofew = TRUE;
+ toofew = true;
break;
}
arg = the_args[val];
@@ -931,16 +937,16 @@ check_pos:
}
if (cur == &prec) {
if (*cur >= 0)
- have_prec = TRUE;
+ have_prec = true;
else
- have_prec = FALSE;
+ have_prec = false;
cur = NULL;
}
goto retry;
case ' ': /* print ' ' or '-' */
/* 'space' flag is ignored */
/* if '+' already present */
- if (signchar != FALSE)
+ if (signchar != false)
goto check_pos;
/* FALL THROUGH */
case '+': /* print '+' or '-' */
@@ -960,16 +966,16 @@ check_pos:
if (cur != &fw)
break;
cur = &prec;
- have_prec = TRUE;
+ have_prec = true;
goto retry;
case '#':
- alt = TRUE;
+ alt = true;
goto check_pos;
case '\'':
#if defined(HAVE_LOCALE_H)
/* allow quote_flag if there is a thousands separator. */
if (loc.thousands_sep[0] != '\0')
- quote_flag = TRUE;
+ quote_flag = true;
goto check_pos;
#else
goto retry;
@@ -978,55 +984,55 @@ check_pos:
if (big_flag)
break;
else {
- static short warned = FALSE;
+ static bool warned = false;
if (do_lint && ! warned) {
lintwarn(_("`l' is meaningless in awk formats; ignored"));
- warned = TRUE;
+ warned = true;
}
if (do_posix) {
msg(_("fatal: `l' is not permitted in POSIX awk formats"));
goto out;
}
}
- big_flag = TRUE;
+ big_flag = true;
goto retry;
case 'L':
if (bigbig_flag)
break;
else {
- static short warned = FALSE;
+ static bool warned = false;
if (do_lint && ! warned) {
lintwarn(_("`L' is meaningless in awk formats; ignored"));
- warned = TRUE;
+ warned = true;
}
if (do_posix) {
msg(_("fatal: `L' is not permitted in POSIX awk formats"));
goto out;
}
}
- bigbig_flag = TRUE;
+ bigbig_flag = true;
goto retry;
case 'h':
if (small_flag)
break;
else {
- static short warned = FALSE;
+ static bool warned = false;
if (do_lint && ! warned) {
lintwarn(_("`h' is meaningless in awk formats; ignored"));
- warned = TRUE;
+ warned = true;
}
if (do_posix) {
msg(_("fatal: `h' is not permitted in POSIX awk formats"));
goto out;
}
}
- small_flag = TRUE;
+ small_flag = true;
goto retry;
case 'c':
- need_format = FALSE;
+ need_format = false;
parse_next_arg();
/* user input that looks numeric is numeric */
if ((arg->flags & (MAYBE_NUM|NUMBER)) == MAYBE_NUM)
@@ -1099,7 +1105,7 @@ out2:
prec = 1;
goto pr_tail;
case 's':
- need_format = FALSE;
+ need_format = false;
parse_next_arg();
arg = force_string(arg);
if (fw == 0 && ! have_prec)
@@ -1113,7 +1119,7 @@ out2:
goto pr_tail;
case 'd':
case 'i':
- need_format = FALSE;
+ need_format = false;
parse_next_arg();
(void) force_number(arg);
#ifdef HAVE_MPFR
@@ -1142,12 +1148,12 @@ out2:
if (tmpval < 0) {
tmpval = -tmpval;
- sgn = TRUE;
+ sgn = true;
} else {
if (tmpval == -0.0)
/* avoid printing -0 */
tmpval = 0.0;
- sgn = FALSE;
+ sgn = false;
}
/*
* Use snprintf return value to tell if there
@@ -1183,7 +1189,7 @@ out2:
if (loc.grouping[ii+1] == 0)
jj = 0; /* keep using current val in loc.grouping[ii] */
else if (loc.grouping[ii+1] == CHAR_MAX)
- quote_flag = FALSE;
+ quote_flag = false;
else {
ii++;
jj = 0;
@@ -1233,7 +1239,7 @@ out2:
base += 2; /* FALL THROUGH */
case 'o':
base += 8;
- need_format = FALSE;
+ need_format = false;
parse_next_arg();
(void) force_number(arg);
#ifdef HAVE_MPFR
@@ -1252,7 +1258,7 @@ mpz0:
mf = mpz2mpfr(zi);
goto mpf1;
}
- signchar = FALSE; /* Don't print '+' */
+ signchar = '\0'; /* Don't print '+' */
}
/* See comments above about when to fill with zeros */
@@ -1292,7 +1298,7 @@ mpf1:
goto pr_tail; /* printf("%.0x", 0) is no characters */
goto int0;
}
- signchar = FALSE; /* Don't print '+' */
+ signchar = '\0'; /* Don't print '+' */
}
/* See comments above about when to fill with zeros */
@@ -1358,7 +1364,7 @@ mpf1:
if (loc.grouping[ii+1] == 0)
jj = 0; /* keep using current val in loc.grouping[ii] */
else if (loc.grouping[ii+1] == CHAR_MAX)
- quote_flag = FALSE;
+ quote_flag = false;
else {
ii++;
jj = 0;
@@ -1429,7 +1435,7 @@ mpf1:
case 'e':
case 'f':
case 'E':
- need_format = FALSE;
+ need_format = false;
parse_next_arg();
(void) force_number(arg);
@@ -1624,7 +1630,7 @@ do_printf(int nargs, int redirtype)
fatal(_("attempt to use array `%s' in a scalar context"), array_vname(redir_exp));
rp = redirect(redir_exp, redirtype, & errflg);
if (rp != NULL)
- fp = rp->fp;
+ fp = rp->output.fp;
} else
fp = output_fp;
@@ -1638,9 +1644,9 @@ do_printf(int nargs, int redirtype)
DEREF(tmp);
return;
}
- efwrite(tmp->stptr, sizeof(char), tmp->stlen, fp, "printf", rp, TRUE);
+ efwrite(tmp->stptr, sizeof(char), tmp->stlen, fp, "printf", rp, true);
if (rp != NULL && (rp->flag & RED_TWOWAY) != 0)
- fflush(rp->fp);
+ rp->output.gawk_fflush(rp->output.fp, rp->output.opaque);
DEREF(tmp);
} else
gawk_exit(EXIT_FATAL);
@@ -1836,7 +1842,7 @@ do_strftime(int nargs)
format = def_strftime_format; /* traditional date format */
formatlen = strlen(format);
(void) time(& fclock); /* current time of day */
- do_gmt = FALSE;
+ do_gmt = false;
if (PROCINFO_node != NULL) {
sub = make_string("strftime", 8);
@@ -2050,7 +2056,7 @@ do_print(int nargs, int redirtype)
fatal(_("attempt to use array `%s' in a scalar context"), array_vname(redir_exp));
rp = redirect(redir_exp, redirtype, & errflg);
if (rp != NULL)
- fp = rp->fp;
+ fp = rp->output.fp;
} else
fp = output_fp;
@@ -2082,18 +2088,18 @@ do_print(int nargs, int redirtype)
}
for (i = nargs; i > 0; i--) {
- efwrite(args_array[i]->stptr, sizeof(char), args_array[i]->stlen, fp, "print", rp, FALSE);
+ efwrite(args_array[i]->stptr, sizeof(char), args_array[i]->stlen, fp, "print", rp, false);
DEREF(args_array[i]);
if (i != 1 && OFSlen > 0)
efwrite(OFS, sizeof(char), (size_t) OFSlen,
- fp, "print", rp, FALSE);
+ fp, "print", rp, false);
}
if (ORSlen > 0)
- efwrite(ORS, sizeof(char), (size_t) ORSlen, fp, "print", rp, TRUE);
+ efwrite(ORS, sizeof(char), (size_t) ORSlen, fp, "print", rp, true);
if (rp != NULL && (rp->flag & RED_TWOWAY) != 0)
- fflush(rp->fp);
+ rp->output.gawk_fflush(rp->output.fp, rp->output.opaque);
}
/* do_print_rec --- special case printing of $0, for speed */
@@ -2112,7 +2118,7 @@ do_print_rec(int nargs, int redirtype)
redir_exp = TOP();
rp = redirect(redir_exp, redirtype, & errflg);
if (rp != NULL)
- fp = rp->fp;
+ fp = rp->output.fp;
DEREF(redir_exp);
decr_sp();
} else
@@ -2129,13 +2135,13 @@ do_print_rec(int nargs, int redirtype)
if (do_lint && f0 == Nnull_string)
lintwarn(_("reference to uninitialized field `$%d'"), 0);
- efwrite(f0->stptr, sizeof(char), f0->stlen, fp, "print", rp, FALSE);
+ efwrite(f0->stptr, sizeof(char), f0->stlen, fp, "print", rp, false);
if (ORSlen > 0)
- efwrite(ORS, sizeof(char), (size_t) ORSlen, fp, "print", rp, TRUE);
+ efwrite(ORS, sizeof(char), (size_t) ORSlen, fp, "print", rp, true);
if (rp != NULL && (rp->flag & RED_TWOWAY) != 0)
- fflush(rp->fp);
+ rp->output.gawk_fflush(rp->output.fp, rp->output.opaque);
}
#if MBS_SUPPORT
@@ -2329,7 +2335,7 @@ do_cos(int nargs)
/* do_rand --- do the rand function */
-static int firstrand = TRUE;
+static bool firstrand = true;
/* Some systems require this array to be integer aligned. Sigh. */
#define SIZEOF_STATE 256
static uint32_t istate[SIZEOF_STATE/sizeof(uint32_t)];
@@ -2342,7 +2348,7 @@ do_rand(int nargs ATTRIBUTE_UNUSED)
if (firstrand) {
(void) initstate((unsigned) 1, state, SIZEOF_STATE);
/* don't need to srandom(1), initstate() does it for us. */
- firstrand = FALSE;
+ firstrand = false;
setstate(state);
}
/*
@@ -2365,7 +2371,7 @@ do_srand(int nargs)
if (firstrand) {
(void) initstate((unsigned) 1, state, SIZEOF_STATE);
/* don't need to srandom(1), we're changing the seed below */
- firstrand = FALSE;
+ firstrand = false;
(void) setstate(state);
}
@@ -2533,7 +2539,7 @@ do_match(int nargs)
* #! /usr/local/bin/mawk -f
*
* BEGIN {
- * TRUE = 1; FALSE = 0
+ * true = 1; false = 0
* print "--->", mygsub("abc", "b+", "FOO")
* print "--->", mygsub("abc", "x*", "X")
* print "--->", mygsub("abc", "b*", "X")
@@ -2545,10 +2551,10 @@ do_match(int nargs)
* function mygsub(str, regex, replace, origstr, newstr, eosflag, nonzeroflag)
* {
* origstr = str;
- * eosflag = nonzeroflag = FALSE
+ * eosflag = nonzeroflag = false
* while (match(str, regex)) {
* if (RLENGTH > 0) { # easy case
- * nonzeroflag = TRUE
+ * nonzeroflag = true
* if (RSTART == 1) { # match at front of string
* newstr = newstr replace
* } else {
@@ -2561,7 +2567,7 @@ do_match(int nargs)
* # which we don't really want, so skip over it
* newstr = newstr substr(str, 1, 1)
* str = substr(str, 2)
- * nonzeroflag = FALSE
+ * nonzeroflag = false
* } else {
* # 0-length match
* if (RSTART == 1) {
@@ -2575,7 +2581,7 @@ do_match(int nargs)
* if (eosflag)
* break
* else
- * eosflag = TRUE
+ * eosflag = true
* }
* if (length(str) > 0)
* newstr = newstr str # rest of string
@@ -2642,7 +2648,7 @@ do_sub(int nargs, unsigned int flags)
long how_many = 1; /* one substitution for sub, also gensub default */
int global;
long current;
- int lastmatchnonzero;
+ bool lastmatchnonzero;
char *mb_indices = NULL;
if ((flags & GENSUB) != 0) {
@@ -2780,7 +2786,7 @@ set_how_many:
}
}
- lastmatchnonzero = FALSE;
+ lastmatchnonzero = false;
bp = buf;
for (current = 1;; current++) {
matches++;
@@ -2812,7 +2818,7 @@ set_how_many:
if (matchstart == matchend
&& lastmatchnonzero
&& matchstart == text) {
- lastmatchnonzero = FALSE;
+ lastmatchnonzero = false;
matches--;
goto empty;
}
@@ -2881,7 +2887,7 @@ set_how_many:
} else
*bp++ = *scan;
if (matchstart != matchend)
- lastmatchnonzero = TRUE;
+ lastmatchnonzero = true;
} else {
/*
* don't want this match, skip over it by copying
@@ -2978,11 +2984,11 @@ do_lshift(int nargs)
shift = force_number(s2)->numbr;
if (do_lint) {
if (val < 0 || shift < 0)
- lintwarn(_("lshift(%lf, %lf): negative values will give strange results"), val, shift);
+ lintwarn(_("lshift(%f, %f): negative values will give strange results"), val, shift);
if (double_to_int(val) != val || double_to_int(shift) != shift)
- lintwarn(_("lshift(%lf, %lf): fractional values will be truncated"), val, shift);
+ lintwarn(_("lshift(%f, %f): fractional values will be truncated"), val, shift);
if (shift >= sizeof(uintmax_t) * CHAR_BIT)
- lintwarn(_("lshift(%lf, %lf): too large shift value will give strange results"), val, shift);
+ lintwarn(_("lshift(%f, %f): too large shift value will give strange results"), val, shift);
}
DEREF(s1);
@@ -3015,11 +3021,11 @@ do_rshift(int nargs)
shift = force_number(s2)->numbr;
if (do_lint) {
if (val < 0 || shift < 0)
- lintwarn(_("rshift(%lf, %lf): negative values will give strange results"), val, shift);
+ lintwarn(_("rshift(%f, %f): negative values will give strange results"), val, shift);
if (double_to_int(val) != val || double_to_int(shift) != shift)
- lintwarn(_("rshift(%lf, %lf): fractional values will be truncated"), val, shift);
+ lintwarn(_("rshift(%f, %f): fractional values will be truncated"), val, shift);
if (shift >= sizeof(uintmax_t) * CHAR_BIT)
- lintwarn(_("rshift(%lf, %lf): too large shift value will give strange results"), val, shift);
+ lintwarn(_("rshift(%f, %f): too large shift value will give strange results"), val, shift);
}
DEREF(s1);
@@ -3037,33 +3043,30 @@ do_rshift(int nargs)
NODE *
do_and(int nargs)
{
- NODE *s1, *s2;
- uintmax_t uleft, uright, res;
- AWKNUM left, right;
+ NODE *s1;
+ uintmax_t res, uval;
+ AWKNUM val;
+ int i;
- POP_TWO_SCALARS(s1, s2);
- if (do_lint) {
- if ((s1->flags & (NUMCUR|NUMBER)) == 0)
- lintwarn(_("and: received non-numeric first argument"));
- if ((s2->flags & (NUMCUR|NUMBER)) == 0)
- lintwarn(_("and: received non-numeric second argument"));
- }
- left = force_number(s1)->numbr;
- right = force_number(s2)->numbr;
- if (do_lint) {
- if (left < 0 || right < 0)
- lintwarn(_("and(%lf, %lf): negative values will give strange results"), left, right);
- if (double_to_int(left) != left || double_to_int(right) != right)
- lintwarn(_("and(%lf, %lf): fractional values will be truncated"), left, right);
- }
+ res = ~0; /* start off with all ones */
+ if (nargs < 2)
+ fatal(_("and: called with less than two arguments"));
- DEREF(s1);
- DEREF(s2);
+ for (i = 1; nargs > 0; nargs--, i++) {
+ s1 = POP_SCALAR();
+ if (do_lint && (s1->flags & (NUMCUR|NUMBER)) == 0)
+ lintwarn(_("and: argument %d is non-numeric"), i);
+
+ val = force_number(s1)->numbr;
+ if (do_lint && val < 0)
+ lintwarn(_("and: argument %d negative value %g will give strange results"), i, val);
- uleft = (uintmax_t) left;
- uright = (uintmax_t) right;
+ uval = (uintmax_t) val;
+ res &= uval;
+
+ DEREF(s1);
+ }
- res = uleft & uright;
return make_integer(res);
}
@@ -3072,33 +3075,30 @@ do_and(int nargs)
NODE *
do_or(int nargs)
{
- NODE *s1, *s2;
- uintmax_t uleft, uright, res;
- AWKNUM left, right;
+ NODE *s1;
+ uintmax_t res, uval;
+ AWKNUM val;
+ int i;
- POP_TWO_SCALARS(s1, s2);
- if (do_lint) {
- if ((s1->flags & (NUMCUR|NUMBER)) == 0)
- lintwarn(_("or: received non-numeric first argument"));
- if ((s2->flags & (NUMCUR|NUMBER)) == 0)
- lintwarn(_("or: received non-numeric second argument"));
- }
- left = force_number(s1)->numbr;
- right = force_number(s2)->numbr;
- if (do_lint) {
- if (left < 0 || right < 0)
- lintwarn(_("or(%lf, %lf): negative values will give strange results"), left, right);
- if (double_to_int(left) != left || double_to_int(right) != right)
- lintwarn(_("or(%lf, %lf): fractional values will be truncated"), left, right);
- }
+ res = 0;
+ if (nargs < 2)
+ fatal(_("or: called with less than two arguments"));
- DEREF(s1);
- DEREF(s2);
+ for (i = 1; nargs > 0; nargs--, i++) {
+ s1 = POP_SCALAR();
+ if (do_lint && (s1->flags & (NUMCUR|NUMBER)) == 0)
+ lintwarn(_("or: argument %d is non-numeric"), i);
+
+ val = force_number(s1)->numbr;
+ if (do_lint && val < 0)
+ lintwarn(_("or: argument %d negative value %g will give strange results"), i, val);
+
+ uval = (uintmax_t) val;
+ res |= uval;
- uleft = (uintmax_t) left;
- uright = (uintmax_t) right;
+ DEREF(s1);
+ }
- res = uleft | uright;
return make_integer(res);
}
@@ -3107,34 +3107,33 @@ do_or(int nargs)
NODE *
do_xor(int nargs)
{
- NODE *s1, *s2;
- uintmax_t uleft, uright, res;
- AWKNUM left, right;
+ NODE *s1;
+ uintmax_t res, uval;
+ AWKNUM val;
+ int i;
- POP_TWO_SCALARS(s1, s2);
+ if (nargs < 2)
+ fatal(_("xor: called with less than two arguments"));
- if (do_lint) {
- if ((s1->flags & (NUMCUR|NUMBER)) == 0)
- lintwarn(_("xor: received non-numeric first argument"));
- if ((s2->flags & (NUMCUR|NUMBER)) == 0)
- lintwarn(_("xor: received non-numeric second argument"));
- }
- left = force_number(s1)->numbr;
- right = force_number(s2)->numbr;
- if (do_lint) {
- if (left < 0 || right < 0)
- lintwarn(_("xor(%lf, %lf): negative values will give strange results"), left, right);
- if (double_to_int(left) != left || double_to_int(right) != right)
- lintwarn(_("xor(%lf, %lf): fractional values will be truncated"), left, right);
- }
+ res = 0; /* silence compiler warning */
+ for (i = 1; nargs > 0; nargs--, i++) {
+ s1 = POP_SCALAR();
+ if (do_lint && (s1->flags & (NUMCUR|NUMBER)) == 0)
+ lintwarn(_("xor: argument %d is non-numeric"), i);
- DEREF(s1);
- DEREF(s2);
+ val = force_number(s1)->numbr;
+ if (do_lint && val < 0)
+ lintwarn(_("xor: argument %d negative value %g will give strange results"), i, val);
- uleft = (uintmax_t) left;
- uright = (uintmax_t) right;
+ uval = (uintmax_t) val;
+ if (i == 1)
+ res = uval;
+ else
+ res ^= uval;
+
+ DEREF(s1);
+ }
- res = uleft ^ uright;
return make_integer(res);
}
@@ -3155,9 +3154,9 @@ do_compl(int nargs)
if (do_lint) {
if (d < 0)
- lintwarn(_("compl(%lf): negative value will give strange results"), d);
+ lintwarn(_("compl(%f): negative value will give strange results"), d);
if (double_to_int(d) != d)
- lintwarn(_("compl(%lf): fractional value will be truncated"), d);
+ lintwarn(_("compl(%f): fractional value will be truncated"), d);
}
uval = (uintmax_t) d;