[Pharo-project] Standard input in Pharo?
I've been just going through "Computer Programming with GNU Smalltalk" book using Pharo 1.0 as an actual ST implementation. The book mentions "stdin nextLine" as a way to get an input from a user via the standard input. Is there a similar class/method in Pharo?
2010/5/11 Andrei Stebakov <lispercat@gmail.com>:
I've been just going through "Computer Programming with GNU Smalltalk" book using Pharo 1.0 as an actual ST implementation. The book mentions "stdin nextLine" as a way to get an input from a user via the standard input. Is there a similar class/method in Pharo?
If you are on linux, you can do just: stream := FileStream readOnlyFileNamed: '/dev/stdin'. stream nextLine Unfortunately, Squeak VM doesn't provides a default API for working with stdin/out, since its more a GUI-centric , not command-line centric thing.
_______________________________________________ Pharo-project mailing list Pharo-project@lists.gforge.inria.fr http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
-- Best regards, Igor Stasenko AKA sig.
Yes I would really like to have that in Pharo 1.2 if people send us some code. Stef On May 11, 2010, at 1:48 AM, Igor Stasenko wrote:
2010/5/11 Andrei Stebakov <lispercat@gmail.com>:
I've been just going through "Computer Programming with GNU Smalltalk" book using Pharo 1.0 as an actual ST implementation. The book mentions "stdin nextLine" as a way to get an input from a user via the standard input. Is there a similar class/method in Pharo?
If you are on linux, you can do just:
stream := FileStream readOnlyFileNamed: '/dev/stdin'. stream nextLine
Unfortunately, Squeak VM doesn't provides a default API for working with stdin/out, since its more a GUI-centric , not command-line centric thing.
_______________________________________________ Pharo-project mailing list Pharo-project@lists.gforge.inria.fr http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
-- Best regards, Igor Stasenko AKA sig.
_______________________________________________ Pharo-project mailing list Pharo-project@lists.gforge.inria.fr http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
Igor Stasenko wrote
If you are on linux, you can do just:
stream := FileStream readOnlyFileNamed: '/dev/stdin'. stream nextLine
Unfortunately, Squeak VM doesn't provides a default API for working with stdin/out,
Is anyone able to use stein from inside an image? I found a lot of mailing list posts about it, esp. around Coral, but I'm not sure if it ever made it into the image... On Mac OS X Lion with recent Jenkins Cocoa Cog Jit VM, none of the following seemed to work (most hung the image): FileStream stdin upToEnd. FileStream stdin next. (FileStream fileNamed: '/dev/fd/0') readStream nextLine. (FileStream fileNamed: '/dev/stdin'). "Can't even create. symlink on OS X" -- View this message in context: http://forum.world.st/Standard-input-in-Pharo-tp2173080p4605172.html Sent from the Pharo Smalltalk mailing list archive at Nabble.com.
Sean P. DeNigris wrote
use stein
-> use stdin Damn you, autocorrect ;-) -- View this message in context: http://forum.world.st/Standard-input-in-Pharo-tp2173080p4605173.html Sent from the Pharo Smalltalk mailing list archive at Nabble.com.
On 2012-05-03, at 07:29, Sean P. DeNigris wrote:
Igor Stasenko wrote
If you are on linux, you can do just:
stream := FileStream readOnlyFileNamed: '/dev/stdin'. stream nextLine
Unfortunately, Squeak VM doesn't provides a default API for working with stdin/out,
Is anyone able to use stein from inside an image? I found a lot of mailing list posts about it, esp. around Coral, but I'm not sure if it ever made it into the image...
On Mac OS X Lion with recent Jenkins Cocoa Cog Jit VM, none of the following seemed to work (most hung the image): FileStream stdin upToEnd. FileStream stdin next. (FileStream fileNamed: '/dev/fd/0') readStream nextLine. (FileStream fileNamed: '/dev/stdin'). "Can't even create. symlink on OS X"
blocking on stdin is normal since most probably there is no data yet. If you run cog / stack vm from the command line you will be able to read stuff. However the default settings for stdin allows you only to receive full lines, not single characters.
Camillo Bruni-3 wrote
If you run cog / stack vm from the command line you will be able to read stuff.
These are the steps I followed: 1. /path/to/JitCocoaVM.app/Contents/MacOS/CogVM "/path/to/Pharo-2.0.image" 2. Type some chars and cr at the command line 3. In workspace, "FileStream stdin nextLine". Vm hangs, beachball... 4. Repeat step 2. No effect. What do I need to do differently? Thanks. Sean -- View this message in context: http://forum.world.st/Standard-input-in-Pharo-tp2173080p4605820.html Sent from the Pharo Smalltalk mailing list archive at Nabble.com.
Sean P. DeNigris wrote
What do I need to do differently?
I found the problem in the VM, which is two-fold: 1. sqFileReadIntoAt reads from stdin with "fread(dst, 1, count, file);". fread reads until EOF. Because we're looking for "count" number of items all at once, if the input is shorter than this, it will never return unless an EOF is sent (ctrl-D on mac). Either of the following (not actually suggesting this as the final patch) fixes the problem and allows input to be read: sqFileReadIntoAt ... do { clearerr(file); if (file == stdin) { printf("Option 1: reading with fgets from stdin...\n"); fgets (dst, count, file); bytesRead = strlen(dst); //printf("Option 2: reading a character at a time with fread from stdin..."); //bytesRead = fread(dst, 1, 1, file); } else { printf("reading other...\n"); bytesRead = fread(dst, 1, count, file); } } while (bytesRead <= 0 && ferror(file) && errno == EINTR); HTH, Sean p.s. this was the same line that was hanging up PipeableOSProcess>>upToEnd... dunno if it's related... -- View this message in context: http://forum.world.st/Standard-input-in-Pharo-tp2173080p4607448.html Sent from the Pharo Smalltalk mailing list archive at Nabble.com.
Sean P. DeNigris wrote
Because we're looking for "count" number of items all at once, if the input is shorter than this, it will never return unless an EOF is sent
Digging further, I found that the "count" is always 2048 because read buffering is enabled for io streams. When reading a normal file, we have two options - we read the number of characters we were looking for, or we reach EOF first. With stdin however, there is a third option, which is - there aren't enough chars available to fill the buffer and there is also no EOF encountered. The following returns successfully after a line is typed in the terminal (ending in newline): in := FileStream stdin. in disableReadBuffering. "Really only look for the requested number of characters" in next. Here's an issue: http://code.google.com/p/pharo/issues/detail?id=5800 The attached changeset fixes stdin. -- View this message in context: http://forum.world.st/Standard-input-in-Pharo-tp2173080p4607687.html Sent from the Pharo Smalltalk mailing list archive at Nabble.com.
Sean P. DeNigris wrote
The attached changeset fixes stdin.
Fix works in 1.3, 1.4, and 2.0... -- View this message in context: http://forum.world.st/Standard-input-in-Pharo-tp2173080p4609820.html Sent from the Pharo Smalltalk mailing list archive at Nabble.com.
:) I remember a few months ago I stepped with stdin hanging. Thanks for fixing :) On Fri, May 4, 2012 at 11:06 PM, Sean P. DeNigris <sean@clipperadams.com>wrote:
Sean P. DeNigris wrote
The attached changeset fixes stdin.
Fix works in 1.3, 1.4, and 2.0...
-- View this message in context: http://forum.world.st/Standard-input-in-Pharo-tp2173080p4609820.html Sent from the Pharo Smalltalk mailing list archive at Nabble.com.
Guillermo Polito wrote
Thanks for fixing :)
Sure! For fun, here's a REPL using no packages added to 1.4: [ command := FileStream stdin nextLine. command = 'exit' ] whileFalse: [ result := Compiler evaluate: command. FileStream stdout nextPutAll: result asString; lf ]. You just have to start from a terminal... Sean -- View this message in context: http://forum.world.st/Standard-input-in-Pharo-tp2173080p4610033.html Sent from the Pharo Smalltalk mailing list archive at Nabble.com.
On 5 May 2012 00:46, Sean P. DeNigris <sean@clipperadams.com> wrote:
Guillermo Polito wrote
Thanks for fixing :)
Sure!
For fun, here's a REPL using no packages added to 1.4: [ Â Â Â Â command := FileStream stdin nextLine. Â Â Â Â command = 'exit' ] whileFalse: [ Â Â Â Â Â Â Â Â result := Compiler evaluate: command. Â Â Â Â Â Â Â Â FileStream stdout nextPutAll: result asString; lf ].
"for completeness of your example, i would add after loop: " Smalltalk snapshot: false andQuit:true.
You just have to start from a terminal...
Sean
-- View this message in context: http://forum.world.st/Standard-input-in-Pharo-tp2173080p4610033.html Sent from the Pharo Smalltalk mailing list archive at Nabble.com.
-- Best regards, Igor Stasenko.
Maybe OSProcess can do something about that. I just tried on Mac to inspect a "(FileStream fileNamed: '/dev/stdin') readStream" Sending #nextLine to it, and entering text in the xterm triggers a loop. It seems to work for #next however. Someone tried this? Cheers, Alexandre On 10 May 2010, at 19:00, Andrei Stebakov wrote:
I've been just going through "Computer Programming with GNU Smalltalk" book using Pharo 1.0 as an actual ST implementation. The book mentions "stdin nextLine" as a way to get an input from a user via the standard input. Is there a similar class/method in Pharo?
_______________________________________________ Pharo-project mailing list Pharo-project@lists.gforge.inria.fr http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
2010/5/11 Andrei Stebakov <lispercat@gmail.com>
I've been just going through "Computer Programming with GNU Smalltalk" book using Pharo 1.0 as an actual ST implementation.
You may want to read also Pharo By Example: http://pharobyexample.org/ Cheers Mariano
The book mentions "stdin nextLine" as a way to get an input from a user via the standard input. Is there a similar class/method in Pharo?
_______________________________________________ Pharo-project mailing list Pharo-project@lists.gforge.inria.fr http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
Does https://ci.lille.inria.fr/pharo/view/Pharo-Kernel%201.4/job/Pharo%20Kernel%2... work for you on Mac? If yes, you can use the code from there. -- Pavel On Tue, May 11, 2010 at 1:00 AM, Andrei Stebakov <lispercat@gmail.com> wrote:
I've been just going through "Computer Programming with GNU Smalltalk" book using Pharo 1.0 as an actual ST implementation. The book mentions "stdin nextLine" as a way to get an input from a user via the standard input. Is there a similar class/method in Pharo?
_______________________________________________ Pharo-project mailing list Pharo-project@lists.gforge.inria.fr http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
Pavel Krivanek-3 wrote
Does https://ci.lille.inria.fr/pharo/view/Pharo-Kernel%201.4/job/Pharo%20Kernel%2... work for you on Mac? If yes, you can use the code from there.
Thanks, Pavel! It works. Pretty cool :) Although I'm interested in having it work in the normal image, or we should remove the method... -- View this message in context: http://forum.world.st/Standard-input-in-Pharo-tp2173080p4605851.html Sent from the Pharo Smalltalk mailing list archive at Nabble.com.
On Thu, May 3, 2012 at 2:16 PM, Sean P. DeNigris <sean@clipperadams.com> wrote:
Pavel Krivanek-3 wrote
Does https://ci.lille.inria.fr/pharo/view/Pharo-Kernel%201.4/job/Pharo%20Kernel%2... work for you on Mac? If yes, you can use the code from there.
Thanks, Pavel! It works. Pretty cool :) Although I'm interested in having it work in the normal image, or we should remove the method...
OK, load OSProcess and take code from here: http://gitorious.org/pharo-build/pharo-build/blobs/master/scripts/pharo/Kern... -- Pavel
-- View this message in context: http://forum.world.st/Standard-input-in-Pharo-tp2173080p4605851.html Sent from the Pharo Smalltalk mailing list archive at Nabble.com.
participants (9)
-
Alexandre Bergel -
Andrei Stebakov -
Camillo Bruni -
Guillermo Polito -
Igor Stasenko -
Mariano Martinez Peck -
Pavel Krivanek -
Sean P. DeNigris -
Stéphane Ducasse