diff options
author | Arnold D. Robbins <arnold@skeeve.com> | 2016-06-26 18:26:39 +0300 |
---|---|---|
committer | Arnold D. Robbins <arnold@skeeve.com> | 2016-06-26 18:26:39 +0300 |
commit | e18ebe10166e2c63f3385666978b678fe6ce67a2 (patch) | |
tree | 7f6a2249dfda678c8651f0e24a273bb8b54bb886 | |
parent | 278767f062a9c6f8ae9ff777b1a41e094813fa09 (diff) | |
download | egawk-e18ebe10166e2c63f3385666978b678fe6ce67a2.tar.gz egawk-e18ebe10166e2c63f3385666978b678fe6ce67a2.tar.bz2 egawk-e18ebe10166e2c63f3385666978b678fe6ce67a2.zip |
Minor improvements after Andy's reworking of stuff.
-rw-r--r-- | ChangeLog | 16 | ||||
-rw-r--r-- | awk.h | 14 | ||||
-rw-r--r-- | builtin.c | 21 | ||||
-rw-r--r-- | eval.c | 12 | ||||
-rw-r--r-- | int_array.c | 85 | ||||
-rw-r--r-- | mpfr.c | 16 | ||||
-rw-r--r-- | node.c | 17 |
7 files changed, 109 insertions, 72 deletions
@@ -1,5 +1,21 @@ 2016-06-26 Arnold D. Robbins <arnold@skeeve.com> + * awk.h: Edit some comments. Add others. Minor coding style changes. + * builtin.c (format_tree): Restore a comment. + (do_mktime): Restore saving/restoring of byte after format string. + (do_sub): Coding style. Use %.*s in warning message. + (nondec2awknum): Restore saving/restoring of byte after string value + being converted. + * eval.c: Minor coding style edits. + * int_array.c (is_integer): Fix order of checks for not + updating string value: check length == 0 before testing values. + Coding style edits. + * mpfr.c (do_mpfr_strtonum): Coding style edits. + * node.c (r_force_number): Restore saving/restoring of byte after + string value being converted. Edit comments some. + +2016-06-26 Arnold D. Robbins <arnold@skeeve.com> + Repair change of 2015-08-25 to handling of MAYBE_NUM. * mpfr.c (mpg_force_number): Just clear MAYBE_NUM. * node.c (r_force_number): Clear STRING separately after @@ -1825,22 +1825,25 @@ force_number(NODE *n) #endif /* GAWKDEBUG */ +/* fixtype --- make a node decide if it's a number or a string */ + /* * In certain contexts, the true type of a scalar value matters, and we - * must ascertain whether it is a a NUMBER or a STRING. In such situations, + * must ascertain whether it is a NUMBER or a STRING. In such situations, * please use this function to resolve the type. * * It is safe to assume that the return value will be the same NODE, - * since force_number on a MAYBE_NUM should always returns the same NODE, + * since force_number on a MAYBE_NUM should always return the same NODE, * and force_string on an INTIND should as well. * * There is no way to handle a Node_typedregex correctly, so we ignore * that case. */ + static inline NODE * fixtype(NODE *n) { - assert((n->type == Node_val) || (n->type == Node_typedregex)); + assert(n->type == Node_val || n->type == Node_typedregex); if (n->type == Node_val) { if ((n->flags & MAYBE_NUM) != 0) return force_number(n); @@ -1850,10 +1853,13 @@ fixtype(NODE *n) return n; } +/* boolval --- return true/false based on awk's criteria */ + /* - * In `awk', a value is considered to be true if it is nonzero _or_ + * In awk, a value is considered to be true if it is nonzero _or_ * non-null. Otherwise, the value is false. */ + static inline int boolval(NODE *t) { @@ -1048,6 +1048,7 @@ check_pos: case 'c': need_format = false; parse_next_arg(); + /* user input that looks numeric is numeric */ fixtype(arg); if ((arg->flags & NUMBER) != 0) { uval = get_number_uj(arg); @@ -2034,12 +2035,16 @@ do_mktime(int nargs) int month, day, hour, minute, second, count; int dst = -1; /* default is unknown */ time_t then_stamp; + char save; t1 = POP_SCALAR(); if (do_lint && (fixtype(t1)->flags & STRING) == 0) lintwarn(_("mktime: received non-string argument")); t1 = force_string(t1); + save = t1->stptr[t1->stlen]; + t1->stptr[t1->stlen] = '\0'; + count = sscanf(t1->stptr, "%ld %d %d %d %d %d %d", & year, & month, & day, & hour, & minute, & second, @@ -2053,6 +2058,7 @@ do_mktime(int nargs) || (month < 1 || month > 12) )) lintwarn(_("mktime: at least one of the values is out of the default range")); + t1->stptr[t1->stlen] = save; DEREF(t1); if (count < 6 @@ -2859,10 +2865,9 @@ do_sub(int nargs, unsigned int flags) target = POP_STRING(); /* original string */ glob_flag = POP_SCALAR(); /* value of global flag */ - if (((glob_flag->flags & STRING) != 0) - && (glob_flag->stlen > 0 - && (glob_flag->stptr[0] == 'g' - || glob_flag->stptr[0] == 'G'))) + if ( (glob_flag->flags & STRING) != 0 + && glob_flag->stlen > 0 + && (glob_flag->stptr[0] == 'g' || glob_flag->stptr[0] == 'G')) how_many = -1; else { (void) force_number(glob_flag); @@ -2875,7 +2880,9 @@ do_sub(int nargs, unsigned int flags) how_many = LONG_MAX; if (d <= 0) { (void) force_string(glob_flag); - warning(_("gensub: third argument `%s' treated as 1"), glob_flag->stptr); + warning(_("gensub: third argument `%.*s' treated as 1"), + (int) glob_flag->stlen, + glob_flag->stptr); } } DEREF(glob_flag); @@ -3561,6 +3568,7 @@ AWKNUM nondec2awknum(char *str, size_t len, char **endptr) { AWKNUM retval = 0.0; + char save; short val; char *start = str; @@ -3632,7 +3640,10 @@ nondec2awknum(char *str, size_t len, char **endptr) *endptr = str; } else { decimal: + save = str[len]; + str[len] = '\0'; retval = strtod(str, endptr); + str[len] = save; } done: return retval; @@ -942,18 +942,16 @@ set_LINT() if (lintlen == 5 && strncmp(lintval, "fatal", 5) == 0) lintfunc = r_fatal; else if (lintlen == 7 && strncmp(lintval, "invalid", 7) == 0) { - do_flags &= ~ DO_LINT_ALL; + do_flags &= ~DO_LINT_ALL; do_flags |= DO_LINT_INVALID; } } else { do_flags &= ~(DO_LINT_ALL|DO_LINT_INVALID); } - } else { - if (! iszero(n)) - do_flags |= DO_LINT_ALL; - else - do_flags &= ~(DO_LINT_ALL|DO_LINT_INVALID); - } + } else if (! iszero(n)) + do_flags |= DO_LINT_ALL; + else + do_flags &= ~(DO_LINT_ALL|DO_LINT_INVALID); /* explicitly use warning() here, in case lintfunc == r_fatal */ if (old_lint != do_lint && old_lint && ! do_lint) diff --git a/int_array.c b/int_array.c index 4ba7c98b..f17bddef 100644 --- a/int_array.c +++ b/int_array.c @@ -96,11 +96,13 @@ is_integer(NODE *symbol, NODE *subs) if ((subs->flags & STRCUR) != 0) { char *cp = subs->stptr; - if ((cp[0] == '0') || isspace((unsigned char) cp[0]) - || (subs->stlen < 1) + if ( subs->stlen == 0 + || cp[0] == '0' + || isspace((unsigned char) cp[0]) || isspace((unsigned char) cp[subs->stlen - 1]) - || ((subs->stlen >= 2) && (cp[0] == '-') - && (cp[1] == '0'))) + || ( subs->stlen >= 2 + && (cp[0] == '-' || cp[0] == '+') + && cp[1] == '0')) return NULL; } @@ -122,50 +124,51 @@ is_integer(NODE *symbol, NODE *subs) * a[-3]=1; print "-3" in a -- true */ - { - /* must be a STRING */ - char *cp = subs->stptr, *cpend, *ptr; - char save; - size_t len = subs->stlen; + /* must be a STRING */ + char *cp = subs->stptr, *cpend, *ptr; + char save; + size_t len = subs->stlen; - if (len == 0 || (! isdigit((unsigned char) *cp) && *cp != '-')) - return NULL; - if (len > 1 && - ((*cp == '0') /* "00", "011" .. */ - || (*cp == '-' && *(cp + 1) == '0') /* "-0", "-011" .. */ - ) - ) - return NULL; - if (len == 1 && *cp != '-') { /* single digit */ - subs->numbr = (long) (*cp - '0'); - if ((subs->flags & MAYBE_NUM) != 0) { - subs->flags &= ~(MAYBE_NUM|STRING); - subs->flags |= NUMBER; - } - subs->flags |= (NUMCUR|NUMINT); - return & success_node; - } - - cpend = cp + len; - save = *cpend; - *cpend = '\0'; + if (len == 0 || (! isdigit((unsigned char) *cp) && *cp != '-')) + return NULL; - errno = 0; - l = strtol(cp, & ptr, 10); - *cpend = save; - if (errno != 0 || ptr != cpend) - return NULL; - subs->numbr = l; + if (len > 1 && + ((*cp == '0') /* "00", "011" .. */ + || (*cp == '-' && *(cp + 1) == '0') /* "-0", "-011" .. */ + ) + ) + return NULL; + if (len == 1 && *cp != '-') { /* single digit */ + subs->numbr = (long) (*cp - '0'); if ((subs->flags & MAYBE_NUM) != 0) { subs->flags &= ~(MAYBE_NUM|STRING); subs->flags |= NUMBER; } - subs->flags |= NUMCUR; - if (l <= INT32_MAX && l >= INT32_MIN) { - subs->flags |= NUMINT; - return & success_node; - } + subs->flags |= (NUMCUR|NUMINT); + return & success_node; } + + cpend = cp + len; + save = *cpend; + *cpend = '\0'; + + errno = 0; + l = strtol(cp, & ptr, 10); + *cpend = save; + if (errno != 0 || ptr != cpend) + return NULL; + + subs->numbr = l; + if ((subs->flags & MAYBE_NUM) != 0) { + subs->flags &= ~(MAYBE_NUM|STRING); + subs->flags |= NUMBER; + } + subs->flags |= NUMCUR; + if (l <= INT32_MAX && l >= INT32_MIN) { + subs->flags |= NUMINT; + return & success_node; + } + return NULL; } @@ -1079,16 +1079,14 @@ do_mpfr_strtonum(int nargs) force_mpnum(r, true, use_lc_numeric); r->stptr = NULL; r->stlen = 0; + } else if (is_mpg_float(tmp)) { + int tval; + r = mpg_float(); + tval = mpfr_set(r->mpg_numbr, tmp->mpg_numbr, ROUND_MODE); + IEEE_FMT(r->mpg_numbr, tval); } else { - if (is_mpg_float(tmp)) { - int tval; - r = mpg_float(); - tval = mpfr_set(r->mpg_numbr, tmp->mpg_numbr, ROUND_MODE); - IEEE_FMT(r->mpg_numbr, tval); - } else { - r = mpg_integer(); - mpz_set(r->mpg_i, tmp->mpg_i); - } + r = mpg_integer(); + mpz_set(r->mpg_i, tmp->mpg_i); } DEREF(tmp); @@ -59,6 +59,7 @@ r_force_number(NODE *n) { char *cp; char *cpend; + char save; char *ptr; extern double strtod(); @@ -66,12 +67,12 @@ r_force_number(NODE *n) return n; /* - * we should always set NUMCUR and clear MAYBE_NUM, and we may possibly + * We should always set NUMCUR and clear MAYBE_NUM, and we may possibly * change STRING to NUMBER if MAYBE_NUM was set and it's a good numeric * string. */ - /* all the conditionals are an attempt to avoid the expensive strtod */ + /* All the conditionals are an attempt to avoid the expensive strtod */ n->flags |= NUMCUR; n->numbr = 0.0; @@ -122,23 +123,27 @@ r_force_number(NODE *n) goto badnum; } + errno = 0; if (do_non_decimal_data /* main.c assures false if do_posix */ && ! do_traditional && get_numbase(cp, true) != 10) { - errno = 0; + /* nondec2awknum() saves and restores the byte after the string itself */ n->numbr = nondec2awknum(cp, cpend - cp, &ptr); } else { - errno = 0; + save = *cpend; + *cpend = '\0'; n->numbr = (AWKNUM) strtod((const char *) cp, &ptr); + *cpend = save; } /* POSIX says trailing space is OK for NUMBER */ while (isspace((unsigned char) *ptr)) ptr++; + if (errno == 0) { if (ptr == cpend) goto goodnum; /* else keep the leading numeric value without updating flags */ - /* fall through to badnum*/ + /* fall through to badnum */ } else { errno = 0; /* @@ -151,7 +156,7 @@ r_force_number(NODE *n) * Or should we accept it as a NUMBER even though strtod * threw an error? */ - /* fall through to badnum*/ + /* fall through to badnum */ } badnum: n->flags &= ~MAYBE_NUM; |