Standard input in Pharo?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
18 messages Options
Reply | Threaded
Open this post in threaded view
|

Standard input in Pharo?

Andrei Stebakov
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
[hidden email]
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
Reply | Threaded
Open this post in threaded view
|

Re: Standard input in Pharo?

Igor Stasenko
2010/5/11 Andrei Stebakov <[hidden email]>:
> 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
> [hidden email]
> http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
>



--
Best regards,
Igor Stasenko AKA sig.

_______________________________________________
Pharo-project mailing list
[hidden email]
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
Reply | Threaded
Open this post in threaded view
|

Re: Standard input in Pharo?

Alexandre Bergel-4
In reply to this post by Andrei Stebakov
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
> [hidden email]
> http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project


_______________________________________________
Pharo-project mailing list
[hidden email]
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
Reply | Threaded
Open this post in threaded view
|

Re: Standard input in Pharo?

Mariano Martinez Peck
In reply to this post by Andrei Stebakov


2010/5/11 Andrei Stebakov <[hidden email]>
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
[hidden email]
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project


_______________________________________________
Pharo-project mailing list
[hidden email]
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
Reply | Threaded
Open this post in threaded view
|

Re: Standard input in Pharo?

Stéphane Ducasse
In reply to this post by Igor Stasenko
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 <[hidden email]>:
>> 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
>> [hidden email]
>> http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
>>
>
>
>
> --
> Best regards,
> Igor Stasenko AKA sig.
>
> _______________________________________________
> Pharo-project mailing list
> [hidden email]
> http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project


_______________________________________________
Pharo-project mailing list
[hidden email]
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
Reply | Threaded
Open this post in threaded view
|

Re: Standard input in Pharo?

Sean P. DeNigris
Administrator
In reply to this post by Igor Stasenko
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"
Cheers,
Sean
Reply | Threaded
Open this post in threaded view
|

Re: Standard input in Pharo?

Sean P. DeNigris
Administrator
Sean P. DeNigris wrote
use stein
-> use stdin

Damn you, autocorrect ;-)
Cheers,
Sean
Reply | Threaded
Open this post in threaded view
|

Re: Standard input in Pharo?

Camillo Bruni-3
In reply to this post by Sean P. DeNigris

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.
Reply | Threaded
Open this post in threaded view
|

Re: Standard input in Pharo?

Sean P. DeNigris
Administrator
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
Cheers,
Sean
Reply | Threaded
Open this post in threaded view
|

Re: Standard input in Pharo?

Pavel Krivanek-3
In reply to this post by Andrei Stebakov
Does https://ci.lille.inria.fr/pharo/view/Pharo-Kernel%201.4/job/Pharo%20Kernel%201.4%20OSProcess%20evaluator/
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 <[hidden email]> 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
> [hidden email]
> http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project

Reply | Threaded
Open this post in threaded view
|

Re: Standard input in Pharo?

Sean P. DeNigris
Administrator
Pavel Krivanek-3 wrote
Thanks, Pavel! It works. Pretty cool :) Although I'm interested in having it work in the normal image, or we should remove the method...
Cheers,
Sean
Reply | Threaded
Open this post in threaded view
|

Re: Standard input in Pharo?

Pavel Krivanek-3
On Thu, May 3, 2012 at 2:16 PM, Sean P. DeNigris <[hidden email]> wrote:

>
> Pavel Krivanek-3 wrote
>>
>> Does
>> https://ci.lille.inria.fr/pharo/view/Pharo-Kernel%201.4/job/Pharo%20Kernel%201.4%20OSProcess%20evaluator/
>> 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/Kernel-2.0/OSProcessEvaluator.st

-- 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.
>

Reply | Threaded
Open this post in threaded view
|

Re: Standard input in Pharo?

Sean P. DeNigris
Administrator
In reply to this post by Sean P. DeNigris
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...
Cheers,
Sean
Reply | Threaded
Open this post in threaded view
|

Re: Standard input in Pharo?

Sean P. DeNigris
Administrator
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.
Cheers,
Sean
Reply | Threaded
Open this post in threaded view
|

Re: Standard input in Pharo?

Sean P. DeNigris
Administrator
Sean P. DeNigris wrote
The attached changeset fixes stdin.
Fix works in 1.3, 1.4, and 2.0...
Cheers,
Sean
Reply | Threaded
Open this post in threaded view
|

Re: Standard input in Pharo?

Guillermo Polito
:) 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 <[hidden email]> 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.


Reply | Threaded
Open this post in threaded view
|

Re: Standard input in Pharo?

Sean P. DeNigris
Administrator
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
Cheers,
Sean
Reply | Threaded
Open this post in threaded view
|

Re: Standard input in Pharo?

Igor Stasenko
On 5 May 2012 00:46, Sean P. DeNigris <[hidden email]> 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.