Re: How to read characters from stdin?
Bernd Paysan <[email protected]> Tue, 05 May 2026 18:07:39 +0200
| Newsgroups | gmane.comp.lang.forth.gforth |
|---|---|
| Message-ID | <[email protected]> |
--nextPartWKxLTpgtQL-u6F9nzIEgOA Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8"; protected-headers="v1" From: Bernd Paysan <[email protected]> To: "Mark J. Reed" <[email protected]>, gforth-mXXj517/[email protected] Cc: "gforth-mXXj517/[email protected]" <gforth-mXXj517/[email protected]>, jlyonm <jlyonm-g/[email protected]> Subject: Re: How to read characters from stdin? Date: Tue, 05 May 2026 18:07:39 +0200 Message-ID: <[email protected]> MIME-Version: 1.0 Am Dienstag, 5. Mai 2026, 17:31:53 Mitteleurop=C3=A4ische Sommerzeit schrie= b jlyonm=20 via Gforth discussion and announcements: > On Tuesday, May 5th, 2026 at 1:55 AM, Mark J. Reed <[email protected]>= =20 wrote: > > On Mon, May 4, 2026 at 11:53=E2=80=AFAM jlyonm <jlyonm-g/[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.>=20 > > 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. >=20 > > > 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.>=20 > > I'm confused - don't those words work exclusively on stdin? >=20 > In the Gforth info document, section 2.6 it says, "If you pipe into Gfort= h, > 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. KEY does recognize end of file, but it throws an exception instead of passi= ng a=20 flag. =46or that purpose, there is now (in the development snapshot) KEY-IOR, whi= ch=20 returns a negative value for end of file or other exceptions. $ echo "check this" | gforth -e ": test begin key-ior dup 0>=3D while h. re= peat=20 drop ; test cr bye" $63 $68 $65 $63 $6B $20 $74 $68 $69 $73 $A End of file is error code -512 (which translates into =E2=80=9Csuccess=E2= =80=9D if thrown, as=20 there is no errno assigned to EOF). > > Just use stdin file-eof? ? >=20 > 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. =46ILE-EOF? is documented in the current development manual. https://net2o.de/gforth/ Go to the index, search for EOF, and second hit is it. > > > In case it matters, I'm actually running this on a phone running Line= age > > > OS (ostensibly Android) under termux. I don't think it effects the > > > behavior. The termux version is likely 0.7.3, and the development documentation can b= e=20 significantly ahead of the functions available there. =2D-=20 Bernd Paysan "If you want it done right, you have to do it yourself" net2o id: kQusJzA;7*?t=3Duy@X}1GWr!+0qqp_Cn176t4(dQ* https://net2o.de/ --nextPartWKxLTpgtQL-u6F9nzIEgOA Content-Type: application/pgp-signature; name="signature.asc" Content-Description: This is a digitally signed message part. Content-Transfer-Encoding: 7Bit -----BEGIN PGP SIGNATURE----- iQJPBAABCAA5FiEERJ1NDqPQRwYnwBjr9y2Uk5MtoGcFAmn6FcsbFIAAAAAABAAO bWFudTIsMi41KzEuMTIsMiwyAAoJEPctlJOTLaBnvTQP+wdDtL3DVk+Ae0hZvCVY Nb+It5s+Oq+l8JLHE2H0LIQ0KYA3a6Ad93B7oGVMEk7Eq/7avMjs/EkbbPU7i1kn 7RbG/vdpzHEhikXSe8qKY7xwC3z7rK5xrGKO8Fdfb+Q9kgEpCZSQt8p91+Z1eZp2 wbuBMjr/omCn90+GjEf2PFWOyChTR6YbV09d/cH4tlY7zKWYFWF/eHwMJsCtn9Ey RdgbGygpkn0Sd1m/OKpis+s84m7K+XC9heMl2G1Ed0AA80YNvkRT653+ui/SLtHR qzvEjUT4dRekOzyg6BOu2Conp2ZcLr1g3qs3IZqhlbhqhOYLp6B3Zv6ukmt0W2N4 QC240KID3VCQiLgXvZIJ9BHwoU5asrYEV+w6XzVqGu/fel2ZA4wwOlSQFto7qm8T 1bFKkdcHvAJyddmjLTXBq5/Lcsn2DmZ/i4zuhWRRkuKppo63+PgGdLrIZWGVU5lp XItVv/ecsXR8sh0u1f6rP2mDwjaBrH6ePgYs5SrfmIONVgBM4GqAsis7rvUp8QiK XKFzBfzgA04qKM/CUVA3Fi79bM02dFecv0djWHkwEw8kxoSk7jFiGJTup3xnukX1 i+Mq622BibqS9IH3CMKEsrvipp54gVmD0EosAqPu5MlB7nqQUt3L8ypFOPowbW0E +qQzOGIsha4PBfXpGrxk//TR =uh4Q -----END PGP SIGNATURE----- --nextPartWKxLTpgtQL-u6F9nzIEgOA--