Command Line Interface using FFI

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

Command Line Interface using FFI

Guillermo Polito
I've spent a few hours playing with Alien+FFI and I did want to have a Command line interface to play and use as a quick browser.

Gofer it
    squeaksource: 'CLInterface';
    package: 'ConfigurationOfCLInterface';
    load.

ConfigurationOfCLInterface load.


To play with it write a script, for example startCLI.st, with the following:

CLInterface start



And then, from the command line

CogVM -vm-display-null <image> startCLI.st


What it does support?
- executes smalltalk expressions
- handles the errors and shows them into the stdout (so it does not explode :P)
- stores variables as a workspace
Since I used standard C functions (fgets and printf only) this should work on every platform from scratch (having a VM with FFI working, of course)...

The problems I've ran so far:
- blocking FFI blocks the vm.  I've not tried yet to use threads through FFI (and I don't know if it works.  Does it?).  But It should be great to start a headless seaside server with a command line :/.

Ok, it's a very simple thing, but it allows to execute a expression from a remote server via ssh without doing much :).

Guille
Reply | Threaded
Open this post in threaded view
|

Re: Command Line Interface using FFI

Sven Van Caekenberghe
Hi Guillermo,

On 29 Jan 2012, at 21:11, Guillermo Polito wrote:

> I've spent a few hours playing with Alien+FFI and I did want to have a Command line interface to play and use as a quick browser.
>
> Gofer it
>     squeaksource: 'CLInterface';
>     package: 'ConfigurationOfCLInterface';
>     load.
>
> ConfigurationOfCLInterface load.
>
>
> To play with it write a script, for example startCLI.st, with the following:
>
> CLInterface start
>
>
>
> And then, from the command line
>
> CogVM -vm-display-null <image> startCLI.st
>
>
> What it does support?
> - executes smalltalk expressions
> - handles the errors and shows them into the stdout (so it does not explode :P)
> - stores variables as a workspace
> Since I used standard C functions (fgets and printf only) this should work on every platform from scratch (having a VM with FFI working, of course)...
>
> The problems I've ran so far:
> - blocking FFI blocks the vm.  I've not tried yet to use threads through FFI (and I don't know if it works.  Does it?).  But It should be great to start a headless seaside server with a command line :/.
>
> Ok, it's a very simple thing, but it allows to execute a expression from a remote server via ssh without doing much :).
>
> Guille

Cool, but why do you need to access those C functions ?
You do know that you can access std* from Smalltalk, do you ?
Have a look at the FileStream class 'stdio' category.

Apart from that, nice small, readable and useful hack.

Regards,

Sven


Reply | Threaded
Open this post in threaded view
|

Re: Command Line Interface using FFI

Guillermo Polito


On Sun, Jan 29, 2012 at 6:58 PM, Sven Van Caekenberghe <[hidden email]> wrote:
Hi Guillermo,

On 29 Jan 2012, at 21:11, Guillermo Polito wrote:

> I've spent a few hours playing with Alien+FFI and I did want to have a Command line interface to play and use as a quick browser.
>
> Gofer it
>     squeaksource: 'CLInterface';
>     package: 'ConfigurationOfCLInterface';
>     load.
>
> ConfigurationOfCLInterface load.
>
>
> To play with it write a script, for example startCLI.st, with the following:
>
> CLInterface start
>
>
>
> And then, from the command line
>
> CogVM -vm-display-null <image> startCLI.st
>
>
> What it does support?
> - executes smalltalk expressions
> - handles the errors and shows them into the stdout (so it does not explode :P)
> - stores variables as a workspace
> Since I used standard C functions (fgets and printf only) this should work on every platform from scratch (having a VM with FFI working, of course)...
>
> The problems I've ran so far:
> - blocking FFI blocks the vm.  I've not tried yet to use threads through FFI (and I don't know if it works.  Does it?).  But It should be great to start a headless seaside server with a command line :/.
>
> Ok, it's a very simple thing, but it allows to execute a expression from a remote server via ssh without doing much :).
>
> Guille

Cool, but why do you need to access those C functions ?
You do know that you can access std* from Smalltalk, do you ?

No! :D
 
Have a look at the FileStream class 'stdio' category.

will do :).  Will that solve the vm-block issue?
 

Apart from that, nice small, readable and useful hack.

Thanks :P.
 
Regards,

Sven



Reply | Threaded
Open this post in threaded view
|

Re: Command Line Interface using FFI

Guillermo Polito
FileStream stdout nextPutAll: 'something';crlf.

Works great, but

FileStream stdin next.

Never returns :/.  Whatever I try.

Any idea?

On Mon, Jan 30, 2012 at 1:29 AM, Guillermo Polito <[hidden email]> wrote:


On Sun, Jan 29, 2012 at 6:58 PM, Sven Van Caekenberghe <[hidden email]> wrote:
Hi Guillermo,

On 29 Jan 2012, at 21:11, Guillermo Polito wrote:

> I've spent a few hours playing with Alien+FFI and I did want to have a Command line interface to play and use as a quick browser.
>
> Gofer it
>     squeaksource: 'CLInterface';
>     package: 'ConfigurationOfCLInterface';
>     load.
>
> ConfigurationOfCLInterface load.
>
>
> To play with it write a script, for example startCLI.st, with the following:
>
> CLInterface start
>
>
>
> And then, from the command line
>
> CogVM -vm-display-null <image> startCLI.st
>
>
> What it does support?
> - executes smalltalk expressions
> - handles the errors and shows them into the stdout (so it does not explode :P)
> - stores variables as a workspace
> Since I used standard C functions (fgets and printf only) this should work on every platform from scratch (having a VM with FFI working, of course)...
>
> The problems I've ran so far:
> - blocking FFI blocks the vm.  I've not tried yet to use threads through FFI (and I don't know if it works.  Does it?).  But It should be great to start a headless seaside server with a command line :/.
>
> Ok, it's a very simple thing, but it allows to execute a expression from a remote server via ssh without doing much :).
>
> Guille

Cool, but why do you need to access those C functions ?
You do know that you can access std* from Smalltalk, do you ?

No! :D
 
Have a look at the FileStream class 'stdio' category.

will do :).  Will that solve the vm-block issue?
 

Apart from that, nice small, readable and useful hack.

Thanks :P.
 
Regards,

Sven




Reply | Threaded
Open this post in threaded view
|

Re: Command Line Interface using FFI

Sven Van Caekenberghe

On 30 Jan 2012, at 11:44, Guillermo Polito wrote:

> FileStream stdout nextPutAll: 'something';crlf.
>
> Works great, but
>
> FileStream stdin next.
>
> Never returns :/.  Whatever I try.
>
> Any idea?

That is not good, I must admit I only tried #stout, so I did:

$ cat run.sh

#!/bin/bash
script_home=$(dirname $0)
script_home=$(cd $script_home && pwd)
image=$script_home/experimental.image
script=$script_home/run.st
args=$*
vm=$script_home/bin/CogVM
options="-vm-display-null -vm-sound-null"
#echo $vm $options $image $script $args
$vm $options $image $script $args

$ cat run.st
[ FileStream stdin atEnd ] whileFalse: [
    FileStream stdout nextPut: FileStream stdin next ].
!
Smalltalk snapshot: false andQuit: true.

$ cat /tmp/foo.txt
Hello there!
Welcome to Smalltalk.



$ cat /tmp/foo.txt | ./run.sh
Hello there!
Welcome to Smalltalk.



So that seems to work.

HTH,

Sven


Reply | Threaded
Open this post in threaded view
|

Re: Command Line Interface using FFI

David T. Lewis
In reply to this post by Guillermo Polito
On Mon, Jan 30, 2012 at 07:44:40AM -0300, Guillermo Polito wrote:

> FileStream stdout nextPutAll: 'something';crlf.
>
> Works great, but
>
> FileStream stdin next.
>
> Never returns :/.  Whatever I try.
>
> Any idea?
>

This is because no data was received on the standard input stream. Try
typing something followed by <enter> so that the operating system terminal
actually sends some data.

By default, the stdin, stdout, and stderr streams are all in blocking mode
unless you set them otherwise. If you want to set the stdin to nonblocking
mode, see #setNonBlocking in package OSProcess. Note that there is no
"correct" setting for this, it depends on what you are trying to do with
the stream.

HTH,
Dave


Reply | Threaded
Open this post in threaded view
|

Re: Command Line Interface using FFI

Guillermo Polito
On Mon, Jan 30, 2012 at 9:17 AM, David T. Lewis <[hidden email]> wrote:
On Mon, Jan 30, 2012 at 07:44:40AM -0300, Guillermo Polito wrote:
> FileStream stdout nextPutAll: 'something';crlf.
>
> Works great, but
>
> FileStream stdin next.
>
> Never returns :/.  Whatever I try.
>
> Any idea?
>

This is because no data was received on the standard input stream. Try
typing something followed by <enter> so that the operating system terminal
actually sends some data.

I did :/.  I always open my images from the terminal.  And the stdout prints in the terminal where I've opened the image.  But when if I use stdin and write to that console, it does not work :/.

However, if I do this with fgets+FFI it works... (It blocks but when I enter data into the terminal the control is returned to the image).


By default, the stdin, stdout, and stderr streams are all in blocking mode
unless you set them otherwise. If you want to set the stdin to nonblocking
mode, see #setNonBlocking in package OSProcess. Note that there is no
"correct" setting for this, it depends on what you are trying to do with
the stream.

HTH,
Dave

 
Thanks!
Reply | Threaded
Open this post in threaded view
|

Re: Command Line Interface using FFI

Guillermo Polito
In reply to this post by Sven Van Caekenberghe
Hmm, I've tried doing it from a non-headless image.  Maybe it has something to do with it?

Guille

On Mon, Jan 30, 2012 at 8:34 AM, Sven Van Caekenberghe <[hidden email]> wrote:

On 30 Jan 2012, at 11:44, Guillermo Polito wrote:

> FileStream stdout nextPutAll: 'something';crlf.
>
> Works great, but
>
> FileStream stdin next.
>
> Never returns :/.  Whatever I try.
>
> Any idea?

That is not good, I must admit I only tried #stout, so I did:

$ cat run.sh

#!/bin/bash
script_home=$(dirname $0)
script_home=$(cd $script_home && pwd)
image=$script_home/experimental.image
script=$script_home/run.st
args=$*
vm=$script_home/bin/CogVM
options="-vm-display-null -vm-sound-null"
#echo $vm $options $image $script $args
$vm $options $image $script $args

$ cat run.st
[ FileStream stdin atEnd ] whileFalse: [
   FileStream stdout nextPut: FileStream stdin next ].
!
Smalltalk snapshot: false andQuit: true.

$ cat /tmp/foo.txt
Hello there!
Welcome to Smalltalk.



$ cat /tmp/foo.txt | ./run.sh
Hello there!
Welcome to Smalltalk.



So that seems to work.

HTH,

Sven