getchar implementation without GNUishms

Liste des GroupesRevenir à cl awk 
Sujet : getchar implementation without GNUishms
De : anthk (at) *nospam* openbsd.home (anthk)
Groupes : comp.lang.awk
Date : 25. Mar 2025, 10:51:54
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <slrnvu4v4s.1su2.anthk@openbsd.home>
User-Agent : slrn/1.0.3 (OpenBSD)
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?




Date Sujet#  Auteur
25 Mar 25 * getchar implementation without GNUishms8anthk
25 Mar 25 `* Re: getchar implementation without GNUishms7Janis Papanagnou
26 Mar 25  `* Re: getchar implementation without GNUishms6Arti F. Idiot
26 Mar 25   +* Re: getchar implementation without GNUishms2Kenny McCormack
26 Mar 25   i`- Re: getchar implementation without GNUishms1Kenny McCormack
26 Mar 25   `* Re: getchar implementation without GNUishms3Janis Papanagnou
26 Mar 25    `* What is getchar? (Was: getchar implementation without GNUishms)2Kenny McCormack
26 Mar 25     `- Re: What is getchar? (Was: getchar implementation without GNUishms)1Kaz Kylheku

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal