Deutsch English Français Italiano |
<slrnvu4v4s.1su2.anthk@openbsd.home> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail From: anthk <anthk@openbsd.home> Newsgroups: comp.lang.awk Subject: getchar implementation without GNUishms Date: Tue, 25 Mar 2025 09:51:54 -0000 (UTC) Organization: A noiseless patient Spider Lines: 75 Message-ID: <slrnvu4v4s.1su2.anthk@openbsd.home> Injection-Date: Tue, 25 Mar 2025 10:51:54 +0100 (CET) Injection-Info: dont-email.me; posting-host="255fe19b2fdad5e351cb8f0bc47a7f0f"; logging-data="3230209"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+TCgQ2TVuwxfH/Cb9fCQHX" User-Agent: slrn/1.0.3 (OpenBSD) Cancel-Lock: sha1:7s/E+39oNQVr0vGMuGvMHNbA9cE= Bytes: 2889 Hello the guy from https://luxferre.top and gopher://hoi.st has pretty interesting stuff, such as git://git.luxferre.top/awk-gold-collection.git (Run git clone --recursive git://git.luxferre.top/awk-gold-collection.git to get them all) The issue is 'subleq.awk' uses read from GNU Bash I'd guess; thus, is not portable to sh/ksh. The flags are missing: function getchar(c, cmd) { # POSIX-compatible getchar emulation with sh read (cmd="c='';IFS= read -r -n 1 -d $'\\0' c;printf '%u' \"'$c\"") | getline c close(cmd) return int(c) } There's another one implemented at tgl.awk, some 'universal' library with mission functions for POSIX awk's. Not so universal, as it falls in the same traps: it depends on 'od' from GNU coreutils, with flags equallly missing: function getchar(c) { if(!TGL_GCH_CMD) TGL_GCH_CMD = "od -tu1 -w1 -N1 -An -v" # first time usage TGL_GCH_CMD | getline c close(TGL_GCH_CMD) return int(c) } Then I tried to it under C, both with a leading space and a newline and with just the char: int main() { int c; if ((c = getchar()) != EOF) { printf(" %d\n", c); /* printf("%d", c); */ } } Then I edited both commands to be piped into getline to use my custom C program, but I had no luck. Subleq programs with no input work fine. Once I try a Forth implemented in subleq, no input is parsed right, as Forth doesn't eval a simple "2 2 + .s" instruction. subleq.fth and forth: https://github.com/howerj/subleq/ The one in C works fine: ../subleq sublec.dec sublec.fth 2 3 + .s 5 ok Not the case with awk (OpenBSD, one true awk), mawk and gawk: awk -f subleq.awk ./subleq/subleq.dec ./sublec.fth sublec.fth has extra Forth words, thus it can be slower to parse under subleq.awk. But the core subleq.dec has a minimal implementation enough to do basic arithmetic. It works under the C implementation of subleq, again, but not under subleq.awk (outside GNU oses). Could it be possible to implement a true portable getchar?