Re: How to read characters from stdin?
jlyonm via Gforth discussion and announcements <gforth-mXXj517/[email protected]> Tue, 05 May 2026 15:31:53 +0000
| Newsgroups | gmane.comp.lang.forth.gforth |
|---|---|
| Message-ID | <B_A3mbG4hpWJnZcVhusyWvGywOTIqiyNNJZCzl4uir-QLVV45w2mg19QvDMiz4LfQmzdbopp81ny5T0jFgm15ybO8LMvwVYgszSfKoqnKJ0=@protonmail.com> |
This is an OpenPGP/MIME signed message (RFC 4880 and 3156) --------d9a968f1897be5a53ef39a86106579eaa4d74fa3d8fe0ad88de52d097c659758 Content-Type: multipart/mixed;boundary=---------------------37c2ede7bd13abcdf25638974900cf9c -----------------------37c2ede7bd13abcdf25638974900cf9c Content-Transfer-Encoding: quoted-printable Content-Type: text/plain;charset=utf-8 On Tuesday, May 5th, 2026 at 1:55 AM, Mark J. Reed <[email protected]> w= rote: > = > = > On Mon, May 4, 2026 at 11:53=E2=80=AFAM jlyonm <jlyonm-g/[email protected]> w= rote: > = > > I wanted to write a simple program that reads input from stdin, and wr= ites it out to stdout doing a little translation. > = > = > Ok, so does your program need to respond to each individual key press, o= r 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 doc= umentation 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 seein= g 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 retu= rns 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, t= hat 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, t= he 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 ch= aracters would still set that while not eating the return. > > hoping to get the flag to know whether the end of the input has been r= eached (which read-file doesn't provide), but this doesn't work either, an= d the program hangs indefinitely. > = > = > Just use stdin file-eof? ? Thanks a lot, this is exactly what I needed, and makes it work. How does o= ne 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 Linea= ge OS (ostensibly Android) under termux. I don't think it effects the beha= vior. > > = > > 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, an= d 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 cha= racters directly from the file descriptor. Is there some other check for E= OF I missed in the documentation? > > = > > Thanks for reading. > = > = > = > -- > Mark J. Reed <[email protected]> -----------------------37c2ede7bd13abcdf25638974900cf9c Content-Type: application/pgp-keys; filename="publickey - jlyonm-g/[email protected] - 0x2EE30A91.asc"; name="publickey - jlyonm-g/[email protected] - 0x2EE30A91.asc" Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename="publickey - jlyonm-g/[email protected] - 0x2EE30A91.asc"; name="publickey - jlyonm-g/[email protected] - 0x2EE30A91.asc" LS0tLS1CRUdJTiBQR1AgUFVCTElDIEtFWSBCTE9DSy0tLS0tCgp4ak1FWmVTaEFCWUpLd1lCQkFI YVJ3OEJBUWRBd0ltdkFvNnkwYm1pN2lmVk94ZFhac3NDL0ZPRTVxeXEKbzl2RHhXVWxDUEhOTFdw c2VXOXViVUJ3Y205MGIyNXRZV2xzTG1OdmJTQThhbXg1YjI1dFFIQnliM1J2CmJtMWhhV3d1WTI5 dFBzS01CQkFXQ2dBK0JZSmw1S0VBQkFzSkJ3Z0prT240NDY2V0hPbUJBeFVJQ2dRVwpBQUlCQWhr QkFwc0RBaDRCRmlFRUx1TUtrZGhYYmVHYkNoU2Q2ZmpqcnBZYzZZRUFBS2lqQVFETWl0dnkKc0xw UUlFMkNsWjV2VGhxSFRrMVdLdTFCc01BNGxpZmV1R0xFR1FEK1B1YVN5UUxmVTRHS1ZBcU1JcWp0 CnJHODd0MDkxTlllRHlLNXU2eThiZXc3T09BUmw1S0VBRWdvckJnRUVBWmRWQVFVQkFRZEFZUmdM WDFQagova0s0cC9mdFBuUnJUZkd2VG51eitGOXUyTExDUlBpS0tIRURBUWdId25nRUdCWUtBQ29G Z21Ya29RQUoKa09uNDQ2NldIT21CQXBzTUZpRUVMdU1La2RoWGJlR2JDaFNkNmZqanJwWWM2WUVB QUczdEFQNHdaUC9BCmVHQW5GbUdqTFRZeDN1TlBWdDlPUXM2RmtEV1hvU3IwUXZQMFJ3RC9jbWNO eWZhUmorcnlpeTlMaXBtLwoyb0hvd09PL2NudUZUVmpMc0w3TVVnVT0KPWxjU3IKLS0tLS1FTkQg UEdQIFBVQkxJQyBLRVkgQkxPQ0stLS0tLQo= -----------------------37c2ede7bd13abcdf25638974900cf9c-- --------d9a968f1897be5a53ef39a86106579eaa4d74fa3d8fe0ad88de52d097c659758 Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: ProtonMail wrsEARYKAG0Fgmn6DVsJEOn4466WHOmBRRQAAAAAABwAIHNhbHRAbm90YXRp b25zLm9wZW5wZ3Bqcy5vcmdP2X087zG+5sRx090enqL2SrASsdg5HQkZhWEz Fzyo4BYhBC7jCpHYV23hmwoUnen4466WHOmBAAC76wD/b0WY8BucHYxGG91Z ruigyaADz8E2et4daDdRkwwSi3IA/2RbwMzwcrb8j9I9AWjWE4zyfVKdjkaR 72u3zF1HM/AI =C0vT -----END PGP SIGNATURE----- --------d9a968f1897be5a53ef39a86106579eaa4d74fa3d8fe0ad88de52d097c659758--