Odd: ReadProgramString() running external commands

Linux specific forum
mdp
Enthusiast
Enthusiast
Posts: 115
Joined: Mon Apr 18, 2005 8:28 pm

Odd: ReadProgramString() running external commands

Post by mdp »

I'm posting this for (further?) clarification, as it puzzled me.
The correct way to interact with console commands seems to be:

Code: Select all

p=RunProgram("ls", "-al", ".", #PB_Program_Read|#PB_Program_Open)
While ProgramRunning(p)
  Debug ReadProgramString(p)
Wend

Debug "done!"
That's also because using the following:

Code: Select all

p=RunProgram("ls", "-al", ".", #PB_Program_Read|#PB_Program_Open)
While ProgramRunning(p)
   If AvailableProgramOutput(p)
      Debug ReadProgramString(p)
   EndIf
Wend
; You won't go past this
Debug "done!"
you will never see "done!" - the program will seem to run forever.
This seems to be related to ReadProgramString(), as on the other hand using ReadProgramData() works:

Code: Select all

s.s=Space(1024)
p=RunProgram("ls", "-al", ".", #PB_Program_Read|#PB_Program_Open)
While ProgramRunning(p)
   If AvailableProgramOutput(p)
      Debug ReadProgramData(p,@s,Len(s))
   EndIf
Wend
; You'll go past this
Debug "done!"
Sum-up: apparently,
- a console program will not end while its output buffer contains data, but
- a console program does not need end its output with a newline.
A program can run forever telling you you have output, you might not be able to necessarily fetch it with ReadProgramString()

Besides, I can't match what I am experiencing with the following from Timo:
The ReadProgramString() waits until a newline is found before it returns (or at program end for the last line),
so if the program outputs text but no newline, AvailableProgramOutput() will
return nonzero, but ReadProgramString() will not return immediately.
unless it means that at program end the function will "return" (instead of waiting) - but not output, [EDIT: no, sorry, that would not be the case for the first example]
and that "will not return immediately" - but not at program end

Pls correct me if I'm wrong / not precise.