On Tuesday, May 5th, 2026 at 1:55 AM, Mark J. Reed <[email protected]> wrote:

> 

> 

> On Mon, May 4, 2026 at 11:53 AM jlyonm <[email protected]> wrote:
> 

> > I wanted to write a simple program that reads input from stdin, and writes 
> > it out to stdout doing a little translation.
> 

> 

> Ok, so does your program need to respond to each individual key press, or can 
> it read a whole line and then process that all at once?

I'm reading one character at a time.


> > At first I thought I could make use of key and key?, but I see the 
> > documentation indicates this can't be used on stdin.
> 

> 

> I'm confused - don't those words work exclusively on stdin?

In the Gforth info document, section 2.6 it says, "If you pipe into Gforth, 
your program should read with `read-file' or `read-line' from `stdin'. `Key' 
does not recognize the end of input." This is exactly what I am seeing with my 
little program. It never exits.


> > So, I tried read-line. This works mostly. I can read characters one at a 
> > time, and the main program's begin ... while loop will terminate at EOF. 
> > But, as one would expect by something called read-line, it consumes returns 
> > on its own. I'd like to receive those.
> 

> 

> Well, you can safely assume that there's a return at the end of the line. Can 
> even append one to the string if you need to..

I guess I could have assumed, if I read one character and get zero back, that 
it must have been a return, but it still felt a little clunky.


> > 

> > Then I tried a combination of read-file and read-line with:
> > 

> > chr 0 stdin read-line
> 

> 

> What's chr?

Sorry, I did not elaborate. As the documentation for read-line explains, the 
first item (third one on the stack) is the memory address into which it puts 
what's been read. I was trying to indicate that I wanted to use the read-line 
for its side-effect of setting flag, hoping that reading zero characters would 
still set that while not eating the return.


> > hoping to get the flag to know whether the end of the input has been 
> > reached (which read-file doesn't provide), but this doesn't work either, 
> > and the program hangs indefinitely.
> 

> 

> Just use stdin file-eof? ?

Thanks a lot, this is exactly what I needed, and makes it work. How does one 
find that word? It is not in the info docs, nor can I find it online. I wonder 
what else I am unaware of.


> > 

> > In case it matters, I'm actually running this on a phone running Lineage OS 
> > (ostensibly Android) under termux. I don't think it effects the behavior.
> > 

> > I did find something on rosettacode:
> > 

> > https://rosettacode.org/wiki/Copy_stdin_to_stdout#Forth
> > 

> > So I am currently using slurp-fid to dump all of stdin into memory, and 
> > then reading a character out and updating the address pointer as I go.
> > 

> > But I was wondering if it is possible to do this from just reading 
> > characters directly from the file descriptor. Is there some other check for 
> > EOF I missed in the documentation?
> > 

> > Thanks for reading.
> 

> 

> 

> --
> Mark J. Reed <[email protected]>

Attachment: publickey - [email protected] - 0x2EE30A91.asc
Description: application/pgp-keys

Attachment: signature.asc
Description: OpenPGP digital signature

Reply via email to