Invoke an octave session via pipe

View: New views
13 Messages — Rating Filter:   Alert me  

Invoke an octave session via pipe

by Qianqian Fang :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I am trying to run a simple octave script I wrote from a GUI. The GUI was written in free pascal with Lazarus. I created a process for "octave" using pipes, and sent commands to the octave session via stdin, and tried to read the output from stdout. Unfortunately, my program did not work. Octave was started correctly, and from stdout, I could capture the copyright info printed at the beginning of an octave session. However, this message ended at "... from previous versions, type `news'", and I can not read from the output pipe anymore. Sending a new command also did not give me a new buffer to read.

My question is: is this the correct way to invoke octave from another program? if octave directs its output not to stdout, then which pipe it should be?

My full test unit can be found at the following link:
http://forum.lazarus.freepascal.org/index.php/topic,6693.0.html

any suggestions are appreciated, thanks in advance.

Re: Invoke an octave session via pipe

by Jaroslav Hajek-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Thu, May 7, 2009 at 7:29 PM, Qianqian Fang <fangqq@...> wrote:

>
> I am trying to run a simple octave script I wrote from a GUI. The GUI was
> written in free pascal with Lazarus. I created a process for "octave" using
> pipes, and sent commands to the octave session via stdin, and tried to read
> the output from stdout. Unfortunately, my program did not work. Octave was
> started correctly, and from stdout, I could capture the copyright info
> printed at the beginning of an octave session. However, this message ended
> at "... from previous versions, type `news'", and I can not read from the
> output pipe anymore. Sending a new command also did not give me a new buffer
> to read.
>
> My question is: is this the correct way to invoke octave from another
> program? if octave directs its output not to stdout, then which pipe it
> should be?
>
> My full test unit can be found at the following link:
> http://forum.lazarus.freepascal.org/index.php/topic,6693.0.html
>
> any suggestions are appreciated, thanks in advance.

When writing to a pipe you typically need to flush it afterwards to
ensure the other process actually gets the data. It seems your code
doesn't do it, so unless FreePascal flushes automatically, which I
doubt, I would try that. No idea of the syntax in Pascal, but you'll
probably figure out.

regards

--
RNDr. Jaroslav Hajek
computing expert & GNU Octave developer
Aeronautical Research and Test Institute (VZLU)
Prague, Czech Republic
url: www.highegg.matfyz.cz
_______________________________________________
Help-octave mailing list
Help-octave@...
https://www-old.cae.wisc.edu/mailman/listinfo/help-octave

Re: Invoke an octave session via pipe

by Qianqian Fang :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Jaroslav Hajek wrote:
> When writing to a pipe you typically need to flush it afterwards to
> ensure the other process actually gets the data. It seems your code
> doesn't do it, so unless FreePascal flushes automatically, which I
> doubt, I would try that. No idea of the syntax in Pascal, but you'll
> probably figure out.
>
> regards
>
>  
hi Jaroslav

thank you for the reply. The TProcess class in free pascal does not have
"flush" function, which I believe it does it internally. Running this on
other programs worked fine.

What makes me curious is why my stdout reading did not give me the
octave prompt, i.e. "octave:1>"? looks like once octave get started,
it will start something else, which I can not access its stdout via
octave's stdout.
_______________________________________________
Help-octave mailing list
Help-octave@...
https://www-old.cae.wisc.edu/mailman/listinfo/help-octave

Re: Invoke an octave session via pipe

by John W. Eaton-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On  7-May-2009, Qianqian Fang wrote:

| What makes me curious is why my stdout reading did not give me the
| octave prompt, i.e. "octave:1>"? looks like once octave get started,
| it will start something else, which I can not access its stdout via
| octave's stdout.

No, I think Octave writes the prompt and most output to stdout.
Probably you want to use the --interactive option when talking to
Octave over a pipe.  If you are on a Unixy system, try this
experiment:

  in one terminal window, run

    mkdir fifo
    octave --interactive < fifo > foo.out

  and in another, type

    cat > fifo
    svd (rand (3))
    fprintf (stderr, "stderr!\n")

and then look at foo.out.  It should contain all of Octave's output
except the "stderr!" message, which should have been printed to the
terminal where you started Octave.

jwe
_______________________________________________
Help-octave mailing list
Help-octave@...
https://www-old.cae.wisc.edu/mailman/listinfo/help-octave

Re: Invoke an octave session via pipe

by Michael Goffioul-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Octave won't give you a prompt by default when operating
over pipes. To force interactive behavior, use the -i and/or
--line-editing command flags.

Michael.


On Thu, May 7, 2009 at 8:57 PM, Qianqian Fang <fangqq@...> wrote:

> Jaroslav Hajek wrote:
>> When writing to a pipe you typically need to flush it afterwards to
>> ensure the other process actually gets the data. It seems your code
>> doesn't do it, so unless FreePascal flushes automatically, which I
>> doubt, I would try that. No idea of the syntax in Pascal, but you'll
>> probably figure out.
>>
>> regards
>>
>>
> hi Jaroslav
>
> thank you for the reply. The TProcess class in free pascal does not have
> "flush" function, which I believe it does it internally. Running this on
> other programs worked fine.
>
> What makes me curious is why my stdout reading did not give me the
> octave prompt, i.e. "octave:1>"? looks like once octave get started,
> it will start something else, which I can not access its stdout via
> octave's stdout.
> _______________________________________________
> Help-octave mailing list
> Help-octave@...
> https://www-old.cae.wisc.edu/mailman/listinfo/help-octave
>
_______________________________________________
Help-octave mailing list
Help-octave@...
https://www-old.cae.wisc.edu/mailman/listinfo/help-octave

Re: Invoke an octave session via pipe

by Qianqian Fang :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

thank you John, and also Michael's reply for using the -i option.

I tried -i and my initial pipe read returned the "octave:1>" prompt,
however, sending commands via my pascal function still
failed to give me any more output to read.

I am not sure I understood the the experiment outlined in your reply.
I ran the "octave --interactive < fifo > foo.out " command and it simply
gave an empty foo.out file; I did not have chance to run the cat command
from another term and octave already quit.

In the past, I only got succeed using pipes to run a .m file from
command line and retrieve the printed results, but that was a
single execution; but now what I am trying to achieve is to interact
with an persistent session (sort of like a GUI), I am not sure if this
is indeed possible with pipe operations.

I also found some possible issues on the pascal side, I posted it
on the Lazarus forum and I will let you know if I get any useful
feedback from there.

Qianqian


John W. Eaton wrote:

> On  7-May-2009, Qianqian Fang wrote:
>
> | What makes me curious is why my stdout reading did not give me the
> | octave prompt, i.e. "octave:1>"? looks like once octave get started,
> | it will start something else, which I can not access its stdout via
> | octave's stdout.
>
> No, I think Octave writes the prompt and most output to stdout.
> Probably you want to use the --interactive option when talking to
> Octave over a pipe.  If you are on a Unixy system, try this
> experiment:
>
>   in one terminal window, run
>
>     mkdir fifo
>     octave --interactive < fifo > foo.out
>
>   and in another, type
>
>     cat > fifo
>     svd (rand (3))
>     fprintf (stderr, "stderr!\n")
>
> and then look at foo.out.  It should contain all of Octave's output
> except the "stderr!" message, which should have been printed to the
> terminal where you started Octave.
>
> jwe
>
>  

_______________________________________________
Help-octave mailing list
Help-octave@...
https://www-old.cae.wisc.edu/mailman/listinfo/help-octave

Re: Invoke an octave session via pipe

by Judd Storrs :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Thu, May 7, 2009 at 4:17 PM, John W. Eaton <jwe@...> wrote:
   mkdir fifo
   octave --interactive < fifo > foo.out

I think this was a slight typo; try "mkfifo fifo" instead of "mkdir fifo".

Cheers,

--judd



_______________________________________________
Help-octave mailing list
Help-octave@...
https://www-old.cae.wisc.edu/mailman/listinfo/help-octave

Re: Invoke an octave session via pipe

by Olaf Till :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Thu, May 07, 2009 at 07:16:28PM -0400, Qianqian Fang wrote:
> thank you John, and also Michael's reply for using the -i option.
>
> I tried -i and my initial pipe read returned the "octave:1>" prompt,
> however, sending commands via my pascal function still
> failed to give me any more output to read.

After you send the command to Octave, it might be necessary to send
the additional command

fflush (stdout);

which should flush the pipe from Octave to your pascal program since
Octaves stdout is probably redirected to that pipe.

Olaf
_______________________________________________
Help-octave mailing list
Help-octave@...
https://www-old.cae.wisc.edu/mailman/listinfo/help-octave

Re: Invoke an octave session via pipe

by George Kousiouris :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


I am not sure if this will help you, I have not worked before with
interactive mode,with the octave prompt,or pipes but what I have done is
to run the octave script from Windows command line like:
c:\>octave script.m

In the beginning I had defined the script.m as a function, which lead to
invoking octave but not executing the script. Octave started but suddenly
it stopped before actually executing the script. This was fixed by
removing the function definition from the beginning of the m file. Then it
executed normally.

George

On Fri, May 8, 2009 02:16, Qianqian Fang wrote:

> thank you John, and also Michael's reply for using the -i option.
>
> I tried -i and my initial pipe read returned the "octave:1>" prompt,
> however, sending commands via my pascal function still failed to give me
> any more output to read.
>
> I am not sure I understood the the experiment outlined in your reply.
> I ran the "octave --interactive < fifo > foo.out " command and it simply
> gave an empty foo.out file; I did not have chance to run the cat command
> from another term and octave already quit.
>
> In the past, I only got succeed using pipes to run a .m file from
> command line and retrieve the printed results, but that was a single
> execution; but now what I am trying to achieve is to interact with an
> persistent session (sort of like a GUI), I am not sure if this is indeed
> possible with pipe operations.
>
> I also found some possible issues on the pascal side, I posted it
> on the Lazarus forum and I will let you know if I get any useful feedback
> from there.
>
> Qianqian
>
>
>
> John W. Eaton wrote:
>
>> On  7-May-2009, Qianqian Fang wrote:
>>
>>
>> | What makes me curious is why my stdout reading did not give me the
>> | octave prompt, i.e. "octave:1>"? looks like once octave get started,
>> | it will start something else, which I can not access its stdout via
>> | octave's stdout.
>>
>>
>> No, I think Octave writes the prompt and most output to stdout.
>> Probably you want to use the --interactive option when talking to
>> Octave over a pipe.  If you are on a Unixy system, try this
>> experiment:
>>
>>
>> in one terminal window, run
>>
>> mkdir fifo octave --interactive < fifo > foo.out
>>
>> and in another, type
>>
>> cat > fifo svd (rand (3)) fprintf (stderr, "stderr!\n")
>>
>> and then look at foo.out.  It should contain all of Octave's output
>> except the "stderr!" message, which should have been printed to the
>> terminal where you started Octave.
>>
>> jwe
>>
>>
>
> _______________________________________________
> Help-octave mailing list
> Help-octave@...
> https://www-old.cae.wisc.edu/mailman/listinfo/help-octave
>
>


----

George Kousiouris
Research Associate
Division of Communications,
Electronics and Information Engineering
School of Electrical and Computer Engineering
Tel: +30 210 772 2546
Mobile: +30 6939354121
Fax: +30 210 772 2569
E-mail: gkousiou@...

National Technical University of Athens
9 Heroon Polytechniou str., 157 73 Zografou, Athens, Greece


_______________________________________________
Help-octave mailing list
Help-octave@...
https://www-old.cae.wisc.edu/mailman/listinfo/help-octave

Parent Message unknown Re: Invoke an octave session via pipe

by Sergei Steshenko-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message




--- On Thu, 5/7/09, Qianqian Fang <fangqq@...> wrote:

> From: Qianqian Fang <fangqq@...>
> Subject: Re: Invoke an octave session via pipe
> To: "John W. Eaton" <jwe@...>
> Cc: help-octave@...
> Date: Thursday, May 7, 2009, 4:16 PM
> thank you John, and also Michael's
> reply for using the -i option.
>
> I tried -i and my initial pipe read returned the
> "octave:1>" prompt,
> however, sending commands via my pascal function still
> failed to give me any more output to read.
>
> I am not sure I understood the the experiment outlined in
> your reply.
> I ran the "octave --interactive < fifo > foo.out "
> command and it simply
> gave an empty foo.out file; I did not have chance to run
> the cat command
> from another term and octave already quit.
>
> In the past, I only got succeed using pipes to run a .m
> file from
> command line and retrieve the printed results, but that was
> a
> single execution; but now what I am trying to achieve is to
> interact
> with an persistent session (sort of like a GUI), I am not
> sure if this
> is indeed possible with pipe operations.
>
> I also found some possible issues on the pascal side, I
> posted it
> on the Lazarus forum and I will let you know if I get any
> useful
> feedback from there.
>
> Qianqian
>
>
> John W. Eaton wrote:
> > On  7-May-2009, Qianqian Fang wrote:
> >
> > | What makes me curious is why my stdout reading did
> not give me the
> > | octave prompt, i.e. "octave:1>"? looks like once
> octave get started,
> > | it will start something else, which I can not access
> its stdout via
> > | octave's stdout.
> >
> > No, I think Octave writes the prompt and most output
> to stdout.
> > Probably you want to use the --interactive option when
> talking to
> > Octave over a pipe.  If you are on a Unixy
> system, try this
> > experiment:
> >
> >   in one terminal window, run
> >
> >     mkdir fifo
> >     octave --interactive < fifo
> > foo.out
> >
> >   and in another, type
> >
> >     cat > fifo
> >     svd (rand (3))
> >     fprintf (stderr, "stderr!\n")
> >
> > and then look at foo.out.  It should contain all
> of Octave's output
> > except the "stderr!" message, which should have been
> printed to the
> > terminal where you started Octave.
> >
> > jwe
> >
> >   
>

I think JWE meant

mkfifo fifo

, not

mkdir fifo.


I.e. a named pipe is needed (and on Windows is created differently).

Then, after the named fifo is created, another application can write
commands into fifo.

Regards,
  Sergei.


     

_______________________________________________
Help-octave mailing list
Help-octave@...
https://www-old.cae.wisc.edu/mailman/listinfo/help-octave

Re: Invoke an octave session via pipe

by Qianqian Fang :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

hi

thank you all for the help. After reading a working example posted at Lazarus's forum (http://forum.lazarus.freepascal.org/index.php?topic=6693.new;topicseen#new), I realized that I made a stupid mistake when appending '\n' to the end of the command; that's why octave did not respond to my commands. After fixing that mistake, the output were printed correctly.

One follow up question, if I send a command and wait for output, is there a way to tell if octave has finished printing (or in a non-busy state) in the process level ? Now I can set a maximum wait period and record whatever printed before time-out, which is probably not reliable.

thanks

Qianqian


On Fri, May 8, 2009 at 4:53 AM, Sergei Steshenko <sergstesh@...> wrote:



--- On Thu, 5/7/09, Qianqian Fang <fangqq@...> wrote:

> From: Qianqian Fang <fangqq@...>
> Subject: Re: Invoke an octave session via pipe
> To: "John W. Eaton" <jwe@...>
> Cc: help-octave@...
> Date: Thursday, May 7, 2009, 4:16 PM
> thank you John, and also Michael's
> reply for using the -i option.
>
> I tried -i and my initial pipe read returned the
> "octave:1>" prompt,
> however, sending commands via my pascal function still
> failed to give me any more output to read.
>
> I am not sure I understood the the experiment outlined in
> your reply.
> I ran the "octave --interactive < fifo > foo.out "
> command and it simply
> gave an empty foo.out file; I did not have chance to run
> the cat command
> from another term and octave already quit.
>
> In the past, I only got succeed using pipes to run a .m
> file from
> command line and retrieve the printed results, but that was
> a
> single execution; but now what I am trying to achieve is to
> interact
> with an persistent session (sort of like a GUI), I am not
> sure if this
> is indeed possible with pipe operations.
>
> I also found some possible issues on the pascal side, I
> posted it
> on the Lazarus forum and I will let you know if I get any
> useful
> feedback from there.
>
> Qianqian
>
>
> John W. Eaton wrote:
> > On  7-May-2009, Qianqian Fang wrote:
> >
> > | What makes me curious is why my stdout reading did
> not give me the
> > | octave prompt, i.e. "octave:1>"? looks like once
> octave get started,
> > | it will start something else, which I can not access
> its stdout via
> > | octave's stdout.
> >
> > No, I think Octave writes the prompt and most output
> to stdout.
> > Probably you want to use the --interactive option when
> talking to
> > Octave over a pipe.  If you are on a Unixy
> system, try this
> > experiment:
> >
> >   in one terminal window, run
> >
> >     mkdir fifo
> >     octave --interactive < fifo
> > foo.out
> >
> >   and in another, type
> >
> >     cat > fifo
> >     svd (rand (3))
> >     fprintf (stderr, "stderr!\n")
> >
> > and then look at foo.out.  It should contain all
> of Octave's output
> > except the "stderr!" message, which should have been
> printed to the
> > terminal where you started Octave.
> >
> > jwe
> >
> >   
>

I think JWE meant

mkfifo fifo

, not

mkdir fifo.


I.e. a named pipe is needed (and on Windows is created differently).

Then, after the named fifo is created, another application can write
commands into fifo.

Regards,
 Sergei.





_______________________________________________
Help-octave mailing list
Help-octave@...
https://www-old.cae.wisc.edu/mailman/listinfo/help-octave

Re: Invoke an octave session via pipe

by John W. Eaton-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On 11-May-2009, Qianqian Fang wrote:

| thank you all for the help. After reading a working example posted at
| Lazarus's forum (
| http://forum.lazarus.freepascal.org/index.php?topic=6693.new;topicseen#new),
| I realized that I made a stupid mistake when appending '\n' to the end of
| the command; that's why octave did not respond to my commands. After fixing
| that mistake, the output were printed correctly.
|
| One follow up question, if I send a command and wait for output, is there a
| way to tell if octave has finished printing (or in a non-busy state) in the
| process level ? Now I can set a maximum wait period and record whatever
| printed before time-out, which is probably not reliable.

I think the normal thing would be to wait for the Octave prompt to
appear in the output.  If you want to see an example of interacting
with Octave over a pipe, you could look at how the Emacs octave-inf
mode works.

jwe
_______________________________________________
Help-octave mailing list
Help-octave@...
https://www-old.cae.wisc.edu/mailman/listinfo/help-octave

Re: Invoke an octave session via pipe

by Qianqian Fang :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

excellent, that worked very well for me. thank you again John.

Qianqian

On Mon, May 11, 2009 at 1:17 PM, John W. Eaton <jwe@...> wrote:
I think the normal thing would be to wait for the Octave prompt to
appear in the output.  If you want to see an example of interacting
with Octave over a pipe, you could look at how the Emacs octave-inf
mode works.

jwe


_______________________________________________
Help-octave mailing list
Help-octave@...
https://www-old.cae.wisc.edu/mailman/listinfo/help-octave