Page 1 of 1

Process: write to its stdin and capture its stdout

Posted: Tuesday 26th December 2023 6:09pm
by Carotino
Hello to everybody.

I have this problem.
I need to Exec a command, writing to its standard input, then capturing what it writes to stdout.
I've been bouncing back and forth in the Gambas documentation wiki, without much success.

Dim proc As Process
proc = Exec [...] For Read Write

' this seems to work
proc.Begin()
Print #proc, "something"
proc.Send()
 
proc.Wait()

' Error: the stream is closed
If Lof(proc) Then myVar = Read #proc, Lof(proc)

'This also doesn't work: the stream is closed
For Each ostrega In proc.Lines
    Print ostrega    
  Next


There is some code here in the wiki https://gambaswiki.org/wiki/lang/open which seems to align with the notion of "read events" I found here and there, which involves the definition of a sub called NAMEOFFILE_Read that is seemingly associated in a magic way to the file with the same name. But it looks like I cannot define a sub inside a sub, so I'm at a loss.

Any help is greatly appreciated.

Re: Process: write to its stdin and capture its stdout

Posted: Tuesday 26th December 2023 9:50pm
by vuott
Carotino wrote: Tuesday 26th December 2023 6:09pm
    Print ostrega    
...ma sei Veneto ?

Re: Process: write to its stdin and capture its stdout

Posted: Tuesday 26th December 2023 9:54pm
by vuott

Re: Process: write to its stdin and capture its stdout

Posted: Tuesday 26th December 2023 10:22pm
by Carotino
vuott wrote: Tuesday 26th December 2023 9:50pm
Carotino wrote: Tuesday 26th December 2023 6:09pm
    Print ostrega    
...ma sei Veneto ?
LOL hai ragione, in realtà sono bergamasco, di solito uso nomi sensati per le variabili ma per l'appunto ero arrivato all'ostrega:)

Re: Process: write to its stdin and capture its stdout

Posted: Tuesday 26th December 2023 11:00pm
by Carotino
Well, congrats for your research!
By the way I understood what I previously referred to as "magical", that is Event names.

In the end I think I'll end up to use something to the likes of

Shell "Echo something | mySoftware" To myVariable


or, which I thinks should be better in the long run, I'll use temporary files in /dev/shm to pass data around.

It still baffles me that such a trivial task is so overly convoluted

Thanks very much

Re: Process: write to its stdin and capture its stdout

Posted: Tuesday 26th December 2023 11:23pm
by vuott
Carotino wrote: Tuesday 26th December 2023 11:00pm I'll use temporary files in /dev/shm to pass data around.
For passing data from one program to another, you can also see these pages:

https://www.gambas-it.org/wiki/index.ph ... mma_Gambas

Re: Process: write to its stdin and capture its stdout

Posted: Tuesday 26th December 2023 11:48pm
by BruceSteers
Carotino wrote: Tuesday 26th December 2023 11:00pm Well, congrats for your research!
By the way I understood what I previously referred to as "magical", that is Event names.

In the end I think I'll end up to use something to the likes of

Shell "Echo something | mySoftware" To myVariable


or, which I thinks should be better in the long run, I'll use temporary files in /dev/shm to pass data around.

It still baffles me that such a trivial task is so overly convoluted

Thanks very much
Here is a simple method to use piping with your program..


Private $sPipedString As String  ' this will be the piped text from stdin

Static Public Sub Main()

  $sPipedString = GetStdio()

' If user typed something like echo "this text" | myProgram.gambas in a terminal
' now $sPipedString will be "this text"

End

Static Public Sub GetStdio() As String

  Dim SourceBuffer As String = ""

  If Not Lof(File.In) Then Return

  While Not Eof(File.in)
    SourceBuffer &= File.In.ReadLine() & "\n"
  Wend

  Return SourceBuffer

End