Groups | Search | Server Info | Login | Register


Groups > comp.lang.awk > #9921

getchar implementation without GNUishms

From anthk <anthk@openbsd.home>
Newsgroups comp.lang.awk
Subject getchar implementation without GNUishms
Date 2025-03-25 09:51 +0000
Organization A noiseless patient Spider
Message-ID <slrnvu4v4s.1su2.anthk@openbsd.home> (permalink)

Show all headers | View raw


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?


Back to comp.lang.awk | Previous | NextNext in thread | Find similar


Thread

getchar implementation without GNUishms anthk <anthk@openbsd.home> - 2025-03-25 09:51 +0000
  Re: getchar implementation without GNUishms Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2025-03-25 14:10 +0100
    Re: getchar implementation without GNUishms "Arti F. Idiot" <addr@is.invalid> - 2025-03-25 22:16 -0600
      Re: getchar implementation without GNUishms gazelle@shell.xmission.com (Kenny McCormack) - 2025-03-26 05:08 +0000
        Re: getchar implementation without GNUishms gazelle@shell.xmission.com (Kenny McCormack) - 2025-03-26 05:48 +0000
      Re: getchar implementation without GNUishms Janis Papanagnou <janis_papanagnou+ng@hotmail.com> - 2025-03-26 06:27 +0100
        What is getchar? (Was: getchar implementation without GNUishms) gazelle@shell.xmission.com (Kenny McCormack) - 2025-03-26 06:07 +0000
          Re: What is getchar? (Was: getchar implementation without GNUishms) Kaz Kylheku <643-408-1753@kylheku.com> - 2025-03-26 06:10 +0000

csiph-web