Path: csiph.com!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail From: Janis Papanagnou Newsgroups: comp.lang.awk Subject: Re: getchar implementation without GNUishms Date: Wed, 26 Mar 2025 06:27:52 +0100 Organization: A noiseless patient Spider Lines: 64 Message-ID: References: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit Injection-Date: Wed, 26 Mar 2025 06:27:54 +0100 (CET) Injection-Info: dont-email.me; posting-host="e6a35e9678f9481385dd1adce673b0e8"; logging-data="1137677"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18cVuntx+d7ntN6zF7d8tSX" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.8.0 Cancel-Lock: sha1:LnL4sdYMm/Hov5rmRXuhUxWpM8w= In-Reply-To: X-Enigmail-Draft-Status: N1110 Xref: csiph.com comp.lang.awk:9925 On 26.03.2025 05:16, Arti F. Idiot wrote: > On 3/25/25 7:10 AM, Janis Papanagnou wrote: >> On 25.03.2025 10:51, anthk wrote: >>> Hello the guy from https://luxferre.top and gopher://hoi.st >>> has pretty interesting stuff, such as >>> >>> [ shell specific or other tries to emulate some getchar function ] >>> >>> Could it be possible to implement a true portable getchar? >> >> Those who think that getchar is a useful function may implement that >> natively in Awk. (It avoids external dependencies and all the issues >> that the posted/quoted code has made obvious.) > > I know you can iterate over input strings a character at a time in AWK > but I don't think you can read a single character from stdin without > also providing a newline via ENTER, which is perhaps what the OP was > actually wanting to do? I don't know. - When I read 'getchar' I associated a character oriented function like awk's 'getline'. That would mean not reading from stdin with some I/O buffered return-terminated input but just processing the data as Awk would read its input from stdin or using 'getline' would. You need to maintain some state, though; here the actual read in line which acts like a buffer in buffered OS reads. What I associated was actually something like function getchar () { if (_pos >= _len) { do { if ((getline _line) <=0) return "" } while (!(_len = length (_line))) _pos = 0 } return substr (_line, ++_pos, 1) } used in contexts like BEGIN { # RS = "$^" while (c = getchar()) print ">" c "<" } where the commented RS assignment could be activated in case you wanted to also read the newline characters, or, without it, to just read in the payload data of a line (or record). The necessary state information is stored in those variables that are named with a leading underscore. As said it's native Awk code without those dependencies on OS, on tools, or on specific non-standard flags of tools. (But as indicated in my previous response, I don't have any need for a function like that. Maybe others do, don't know.) Janis > [...]