Commit graph

35 commits

Author SHA1 Message Date
Ori Bernstein 10afa189d5 awk: correct incoherent cell in assignment (thanks smj, mpinjr)
In run.c::assign(), assigning to $0 from $F, a field,
where F >= 2, produces an incoherent cell.

The assignment occurs in two steps, first the string value
and then the float. When the string value is assigned to $0,
setsval invalidates the fields. If FS hasn't changed, after
getfval rebuilds the fields, NF = 1 and F >= 2, therefore $F
is definitely uninitialized. The result is a float val of
0.0, producing a boolean false in the pattern expression.

Coercing a string comparison gives the expected result
because the incoherent cell has the correct string
value, which is not empty and evaluates to true.
2022-06-25 20:03:41 +00:00
Ori Bernstein 5579176f4a awk: initialize records fully in recinit()
when using records in BEGIN, we would read
from the field table before we read into it;
this ensures that the fields are an empty
string before we start touching their contents.
2022-06-25 18:58:55 +00:00
cinap_lenrek c14ea9fdd1 awk: fix off-by-one string buffer overflow from gsub
the bug happens when we did the fast exit thru "done" label,
where we would not make sure that theres space in the buffer
for the NUL terminator.

instead, avoid the fast exit and always do the final
adjbuf() that makes sure we have space for the NUL terminator.

remove the pointless pb checks, they'r wrong (should'v
been bp >= buf+bufsz) and adjbuf() already makes sure this
can never happen.
2022-03-12 12:29:15 +00:00
Anthony Martin ef2fdb6fdd awk: fix truncated input after fflush
Before the "native" awk work, a call to the fflush function resulted
in one or more calls to the APE fflush(2).

Calling fflush on a stream open for reading has different behavior
based on the environment: within APE, it's a no-op¹; on OpenBSD, it's
an error²; in musl, it depends on whether or not the underlying file
descriptor is seekable³; etc. I'm sure glibc is subtly different.

Now that awk uses libbio, things are different: calling Bflush(2) on a
file open for reading simply discards any data in the buffer. This
explains why we're seeing truncated input. When awk attempts to read
in the next record, there's nothing in the buffer and no more data to
read so it gets EOF and exits normally. Note that this behavior is not
documented in bio(2). It was added in the second edition but I haven't
figured out why or what depends on it.

The simple fix is to have awk only call Bflush on files that were
opened for writing. You could argue that this is the only correct
behavior according to the awk(1) manual and it is, in fact, how GNU
awk behaves⁴.

1. /sys/src/ape/lib/ap/stdio/fflush.c
2. https://cvsweb.openbsd.org/src/lib/libc/stdio/fflush.c?rev=1.9
3. https://git.musl-libc.org/cgit/musl/tree/src/stdio/fflush.c
4. https://git.savannah.gnu.org/cgit/gawk.git/tree/io.c#n1492
2020-11-19 23:05:26 -08:00
cinap_lenrek 9960a125a3 awk: fix race condition with sub-mk in mkfile
the maketab helper program was generated in parallel, which
had a dependency to y.tab.h which lead to yacc running
twice in parallel.

this removes the dependency to y.tab.h in the virtual
maketab.$objtype target to prevent this race condition.

the dependency to y.tab.h is resolved in the main mk at the
$cputype.maketab target which serializes with the other
targets.
2020-05-24 16:00:45 +02:00
Ori Bernstein 5bc9b0c3ca improve usage messages (thanks henesy)
Fix inconsistencies between programs and their usage
messages,  correct instances where information seems
to be missing or lost. This  includes missing arguments,
making usage consistent with manuals, and so on.
2020-03-10 10:09:34 -07:00
Ori Bernstein e3a43c4f2b awk: make empty FS unicodely-correct. 2019-10-09 17:36:02 -07:00
cinap_lenrek d7684982e8 awk: no need to call getargv() twice to get the value for FILENAME 2019-05-24 17:25:44 +02:00
cinap_lenrek 7c33bdd2d3 awk: fix nextfile crash (thanks leetspete)
to reproduce the bug:

term% awk 'BEGIN{nextfile; nextfile;}'
sys: trap: fault read addr=0x10 pc=0x00019a3a
awk 6584: suicide: sys: trap: fault read addr=0x10 pc=0x00019a3a
2019-04-16 16:13:30 +02:00
Alex Musolino 913be4e74a awk(1): fix append operator to avoid truncating file 2018-10-31 16:49:02 +10:30
cinap_lenrek 9cf3dc9a25 awk: allow string as exit status 2017-08-12 21:34:06 +02:00
cinap_lenrek 3f9d5e4a4f awk: don't get into a infinite loop with eof while in string (thanks BurnZeZ) 2017-08-11 03:38:10 +02:00
cinap_lenrek 178e05f9b7 awk: handle bad/incomplete input in maketab (thanks kenji arisawa) 2017-06-02 19:03:37 +02:00
cinap_lenrek 4f72cda4ac awk: improve random number generation
don't use rand() and scale it to 0..1, instead call
native frand() which produces uniform random number.

instead of seeding the rng with time(0), use truerand().
2016-12-18 18:00:45 +01:00
spew db7d1ed2b1 fix awk format printing bugs (thanks aiju) 2016-08-18 08:52:58 -05:00
cinap_lenrek e8eed628ae awk: restore old buffering behaviour for printf 2016-05-27 18:56:31 +02:00
spew 9ccbf1dcb1 just use wait in "system" call for awk, get rid of unix dependent status code divisor 2016-05-01 20:54:46 -05:00
spew ee68dedf53 null terminate await buffer for proper tokenize call 2016-05-01 20:34:07 -05:00
ben a141f4c53a use rc in awk, remove awk from APEDIRS in cmd mkfile 2016-05-01 20:22:35 -05:00
cinap_lenrek 49cbee690f awk: dont require sed to build awk 2016-05-02 00:35:52 +02:00
cinap_lenrek 42a7fcabb0 awk: bring back ENVIRON[] support 2016-05-02 00:34:23 +02:00
cinap_lenrek 974c4fc768 awk: temporarily fix for NaN() exceptions on 386
problem is NaN() produces a SNaN, not a QNaN... and on the 387,
storing 80 bit SNaN in register to a 64-bit memory destination
traps.

SNaN/QNaN encoding is machine specific. mips has the qiet/signaling
bit inverted.

disabling fp exception in main() now, but that sucks.

i think the best solution would be to not even call strtod() in
is_number() but just write a regex or a little state machine that
will only accept numbers without nan and ±inf.

that might even make it faster and is more robust than relying on
the os's strtod() details.
2016-04-30 05:12:20 +02:00
aiju 46008ac6b7 handle NaN in awk (thanks spew) 2016-04-29 18:11:29 +02:00
stanley lieber 415b5dfe9b awk: fix mkfile for cross-compiles (thanks, spew) 2016-04-28 23:59:59 -04:00
ben 4d5fb6c357 fix printing a char from an empty string 2016-04-28 12:07:24 -05:00
ben 6886164e34 fix printing a char from a string that starts with a rune 2016-04-28 11:20:38 -05:00
ben 7420dc6ada fix unsigned printing for awk 2016-04-28 11:07:43 -05:00
ben b8986a889d use Beof for awk port 2016-04-27 08:09:16 -05:00
ben 85824350b5 remove ape regexp library, add utility for awk native port 2016-04-27 07:52:41 -05:00
ben 0a460e1722 New libregexp and APE ported to native 2016-04-26 22:23:44 -05:00
ppatience0 9af0b2c683 awk: proctab.c is always regenerated from maketab, so it is unnecessary to keep it 2013-05-09 20:48:23 -04:00
cinap_lenrek e93d22ef18 awk: fix the fix
be more carefull. have to preserve DONTFREE flag!
2012-12-10 09:19:41 +01:00
cinap_lenrek 6e4554b7d5 awk: prevent split(a[x], a) from freeing a[x]
the freesymtab() call frees the y argument. temporarily mark it
not to be freed.
2012-12-10 07:20:00 +01:00
Taru Karttunen a9060cc06b Import sources from 2011-03-30 iso image - lib 2011-03-30 19:35:09 +03:00
Taru Karttunen e5888a1ffd Import sources from 2011-03-30 iso image 2011-03-30 15:46:40 +03:00