ThomasDickey / original-mawk

bug-reports for mawk (originally on GoogleCode)
http://invisible-island.net/mawk/mawk.html
17 stars 2 forks source link

printf "%c" with *hex* arguments only prints letters and numbers, "0" for all others #32

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?

From Bash:

mawk '{ printf "%c == %c\n", $1, $2 }' <<<$'0x41 65\n0x2d 45'  # pass chars. 
'A' and '-' first as hex., then as dec. number

What is the expected output? What do you see instead?

Expected:

A == A
- == -

Actual:

A == A
0 == -      # <-- !! BUG: '0' should be '-'

What version of the product are you using? On what operating system?

mawk 1.3.4-20141027
OSX 10.10.3

Please provide any additional information below.

Original issue reported on code.google.com by mkleme...@gmail.com on 14 May 2015 at 12:07

GoogleCodeExporter commented 8 years ago
mawk is not the only one having problems with this.
The same problem happens with gawk.
busybox awk seems to do the thing you expect.

For gawk there are two solutions:
  - strtonum
  - --non-decimal-data

https://www.gnu.org/software/gawk/manual/html_node/Nondecimal_002dnumbers.html

$ for AWK in mawk gawk 'busybox awk' ; do
    echo "$AWK:";
    ${AWK} '{ printf "%c == %c\n", $1, $2 }' <<<$'0x41 65\n0x2d 45';
    echo;
done

mawk:
A == A
0 == -

gawk:
0 == A
0 == -

busybox awk:
A == A
- == -

$ for AWK in mawk gawk 'busybox awk' ; do
    echo "$AWK:";
    ${AWK} '{ printf "%c == %c\n", $1+0, $2 }' <<<$'0x41 65\n0x2d 45';
    echo;
done

mawk:
A == A
- == -

gawk:
 == A
 == -

busybox awk:
A == A
- == -

$ for AWK in mawk gawk 'busybox awk' ; do
    echo "$AWK:";
    ${AWK} '{ printf "%c == %c\n", int($1), $2 }' <<<$'0x41 65\n0x2d 45';
    echo;
done

mawk:
A == A
- == -

gawk:
 == A
 == -

busybox awk:
A == A
- == -

$ for AWK in mawk gawk 'busybox awk' ; do
    echo "$AWK:";
    ${AWK} '{ printf "%c == %c\n", strtonum($1), $2 }' <<<$'0x41 65\n0x2d 45';
    echo;
done

mawk:
mawk: line 2: function strtonum never defined

gawk:
A == A
- == -

busybox awk:
awk: cmd. line:1: Call to undefined function

for AWK in mawk gawk 'busybox awk' ; do
    echo "$AWK:";
    ${AWK} --non-decimal-data '{ printf "%c == %c\n", $1, $2 }' <<<$'0x41 65\n0x2d 45';
    echo;
done
mawk:
mawk: not an option: --non-decimal-data

gawk:
A == A
- == -

busybox awk:
awk: unrecognized option `--non-decimal-data'
BusyBox v1.15.1 (2013-10-21 09:15:29 EDT) multi-call binary

Usage: awk [OPTIONS] [AWK_PROGRAM] [FILE]...

Options:
    -v VAR=VAL  Set variable
    -F SEP      Use SEP as field separator
    -f FILE     Read program from file

Original comment by hulselma...@gmail.com on 16 Jul 2015 at 6:45

GoogleCodeExporter commented 8 years ago
Thanks for that - good to know there are workarounds, even though there's no 
single one that works for all versions (using `int()` comes closest, but Gawk 
still needs `--non-decimal-data` or `--posix` in addition).

BSD Awk also works as expected.

Gawk's default behavior of not recognizing hex. numbers is by design, and can 
be changed as you describe (`--posix` would also make it work, but implies many 
additional behavioral changes). 

The POSIX spec. says that compliant implementations are allowed, but not 
required to accept hex. numbers.

However, the fact that it *half* works in Mawk makes me suspect that it's a bug.

Original comment by mkleme...@gmail.com on 16 Jul 2015 at 7:08