ときどきの雑記帖 RE* (新南口)
POSIX.STD
POSIX.STD
gawk 5.1.0 のソースアーカイブをみたら POSIX.STDという 見慣れない名前のファイルがあったので内容をチェックしようとしたが…
Copyright (C) 1992, 1995, 1998, 2001, 2006, 2007, 2010, 2011, 2015, 2019
Free Software Foundation, Inc.Copying and distribution of this file, with or without modification,
are permitted in any medium without royalty provided the copyright
notice and this notice are preserved.
copyright の年が 1992からあるってことはその頃のバージョンからあったってことだよな。 存在を(自分が)忘れていた?
ざっと調べてみたところ、2.14 の時点で POSIX (拡張子なし)というファイルが増えていて、 3.0.0 で POSIX.STD となっていた模様。
前置きはこれくらいにして5.1.0 のPOSIX.STD から。
Sun Apr 21 14:27:19 IDT 2019
============================
This file documents several things related to the 2008 POSIX standard
that I noted after reviewing it.
以下各項目を順番に見ていく。
- POSIX leaves undefined what happens for something like
awk ‘{ print ; exit }’ if=42 /etc/passwd
Mawk diagnoses this. Gawk and BWK awk do not. This doesn’t seem to be
worth the effort to add the code, but at least I’m aware of it.
コマンドライン引数での変数代入で変数名がvalidなものでないときの動作がPOSIXでは定義されていない。と。 手元のmawkでやってみるとこう。
>mawk "{print; exit}" if=42 nul
mawk: run time error: cannot command line assign to if
type clash or keyword
FILENAME="" FNR=0 NR=0
一方gawk(5.0.0)では
>gawk "{print; exit}" if=42 nul
gawk: fatal: cannot use gawk builtin `if' as variable name
なるほど。
2番目は十六進表記の浮動小数点数に関して。
- The 2001-2004 standards accidentally required support for hexadecimal
floating point constants and for Infinity and Not-A-Number (NaN) values.The 2008 standard now explicitly allows, but does not require, such
support.
More discussion is provided in the node `POSIX Floating Point Problems’ in gawk.texi.
gawk.texi の POSIX Floating Point Problems からサンプルを転載。
$ echo nanny | gawk --posix '{ print $1 + 0 }'
nan
$ echo 0xDeadBeef | gawk --posix '{ print $1 + 0 }'
3735928559
nannyを数値として解釈するとnan、0xDeadBeefは十進数で3735928559ですよ。と。 gawk.texiによると–posix を指定しない場合でもgawkが特別扱いするパターンがあって、 それは以下の四つ。
Without –posix, gawk interprets the four string values
+inf,
-inf,
+nan,
and
-nan
specially, producing the corresponding special numeric values. The leading sign acts a signal to gawk (and the user) that the value is really numeric. Hexadecimal floating point is not supported (unless you also use –non-decimal-data, which is not recommended). For example:
$ echo nanny | gawk '{ print $1 + 0 }'
0
$ echo +nan | gawk '{ print $1 + 0 }'
+nan
$ echo 0xDeadBeef | gawk '{ print $1 + 0 }'
0
二番目の例は +をつけることによって “value is really numeric” であると gawk に指示しているのでこうなると。
gawk の内部的には数値は基本的に浮動小数点数なのでHexadecimal floating point のように書いているんだろうけど、例で使われているのが整数値(fractionがない) 0xDeadBeefてのは気になると言えば気になるところ。
- String comparison with <, <= etc is supposed to take the locale’s collating
sequence into account. By default gawk doesn’t do this. Rather, gawk
will do this only if –posix is in effect.
文字列比較について。–posix 指定時とそうでないときで動作が違うのに注意。
- According to POSIX, the function parameters of one function may not have
the same name as another function, making this invalid:function foo() { … }
function bar(foo) { …}Or even:
function bar(foo) { …}
function foo() { … }Gawk enforces this only with –posix.
関数名と(別の関数の)引数名に同じ名前を使えるかどうか。 POSIX的には使えない。で、gawk は –posix 指定時にのみそれに従うと。
- According to POSIX (following the A, K, & W book), when RS = “”, then
newline is also a separator, “no matter what the value of FS is”, implying
that this is true even if FS is a regexp.In fact, UNIX awk has never behaved that, way; it adds \n to the list
for FS = " " or any other single character, and gawk does too. This is
essentially a bug in POSIX.
RS に空文字列("")を設定したときの動作。 POSIX 的にはFSがどのような設定になっていてもそれに改行(\n)が追加された形になるけど、 UNIXの過去のawkやgawkでは FS が " " (デフォルト値すな)か any other single character で区切りになるものに改行が追加された形になると。
following the A, K, & W book とあるから、awkのバイブル本もそう書いてるってことか。
POSIX.STD のこの後の部分はPOSIXの規格書には明記されていない仕様について書かれている。 gawk.texi の中を探せば関連する記述もあったと思う。
The following things aren’t described by POSIX but ought to be:
- The value of $0 in an END rule
END ルール中での$0の値。最後のレコードを読んだのそのままか空かといったところですね、
- The return value of a function that either does return with no value or that falls off the end of the function body.
フローによって値を返したり返さなかったりしてしまう関数の 値を返さないときの戻り値(わかりづらい表現だ)。
- What happens with substr() if start is <= 0, or greater than the length of the string, or if length is <= 0.
substr の第二引数(start)に0以下だとか第三引数(length)に対象文字列の長さ以上の値を与えたときの動作、 また、第三引数に0以下の値を与えたときの動作。
- Whether “next” can be invoked from a function body.
関数の本体に next を置くとどうなるか。 次のレコードを読んでしまうかエラーになるか。