Read from a program without closing it?

Just starting out? Need help? Post your questions and find answers here.
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Read from a program without closing it?

Post by Mistrel »

I'm trying to open the PureBasic compiler using /standby to communicate with it. Before I can send data I need to wait for the "READY" signal. But whenever I specify the #PB_Program_Read flag the program no longer waits and exits immediately.

Why is it closing prematurely like this?

Code: Select all

ProgramID=RunProgram("PBCompiler.exe","/STANDBY","",#PB_Program_Open|#PB_Program_Read)

If ProgramID
  While ProgramRunning(ProgramID)
  	If AvailableProgramOutput(ProgramID)
    	;Output.s+ReadProgramString(ProgramID)+Chr(13)
  	EndIf
  Wend
  Output.s+Chr(13)+Chr(13)
  Output.s+"Exitcode: "+Str(ProgramExitCode(ProgramID))
EndIf

MessageRequester("Output", Output.s)
Fred
Administrator
Administrator
Posts: 18557
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

Did you try with #PB_Program_Wait ?
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Post by Mistrel »

Yes. It says:
[ERROR] Invalid Flags: Cannot combine #PB_Program_Wait with the communication flags.
freak
PureBasic Team
PureBasic Team
Posts: 5962
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

Your code works as expected here. What do you get ? (and what is the text actually read?)
quidquid Latine dictum sit altum videtur
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Post by Mistrel »

The program closes immediately after it finishes reading the output if I use #PB_Program_Read. It won't close if I use just #PB_Program_Open but then I can't read data from it.

I don't want it to close because I haven't sent the file to be compiled to it yet. And I need to read the output to know when the compiler is "READY".
freak
PureBasic Team
PureBasic Team
Posts: 5962
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

This is weird, as here it waits patiently for input once the "READY" was read.

What PB version are you using, and what OS ?
Also, try to specify #PB_Program_Write as well, so the compiler actually gets a pipe for reading.
quidquid Latine dictum sit altum videtur
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Post by Mistrel »

I'm using PB 4.20 and XP sp2.

If I specify the flags #PB_Program_Open|#PB_Program_Read|#PB_Program_Write then the program sits there with a blinking cursor waiting for input.

CompilerInterface.txt says that I should get output on startup:
Except on startup, the compiler will write on the standard output only as a response of a command, so
you only need to check it for data after a command was sent. The amount of lines that are sent as a
response depends on the command that was sent. If not noted otherwise, the response is a single line only.

Immediately on startup, the compiler writes this line:

STARTING<T><VersionNr><VersionString>
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Post by Mistrel »

I just tried this with 4.30 on my notebook and it still closes. Am I doing something incorrectly?

I expect the compiler to remain running when I use the /STANDBY parameter so that I can communicate wit it. Am I misunderstanding how I should be interacting with it?

Freak/Fred can you provide a simple example that loads the compiler using /STANDBY, passes a .pb file, reports the current progress, and continues to wait for the next source after compilation?
Fred
Administrator
Administrator
Posts: 18557
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

Here is a small compiler stub we use to test quickly the standby mode (linux version, will probably needs some tweaks to run on Windows):

Code: Select all

; Compiler stub
;

OpenConsole()

Compiler = RunProgram(#PB_Compiler_Home + "compilers/pbcompiler", "--standby", "", #PB_Program_Open | #PB_Program_Read | #PB_Program_Write)
If Compiler

  ; Wait for the version string
  PrintN(ReadProgramString(Compiler))

  ; Wait for the READY string
  PrintN(ReadProgramString(Compiler))
  
  
  WriteProgramStringN(Compiler, "FUNCTIONLIST")
  NbLines = Val(ReadProgramString(Compiler))
  PrintN(Str(NbLines))
  For k=0 To NbLines
    ReadProgramString(Compiler)
  Next
  

  WriteProgramStringN(Compiler, "STRUCTURELIST")
  NbLines = Val(ReadProgramString(Compiler))
  PrintN(Str(NbLines))
  For k=0 To NbLines
    ReadProgramString(Compiler)
  Next

  
  WriteProgramStringN(Compiler, "INTERFACELIST")
  NbLines = Val(ReadProgramString(Compiler))
  PrintN(Str(NbLines))
  For k=0 To NbLines
    ReadProgramString(Compiler)
  Next
  
    
  WriteProgramStringN(Compiler, "SOURCE"+Chr(9)+"/tmp/PB_EditorOutput.pb")
  WriteProgramStringN(Compiler, "TARGET"+Chr(9)+"/tmp/PureBasic0.app")
  WriteProgramStringN(Compiler, "INCLUDEPATH"+Chr(9)+"/Users/fred/Desktop/IncludeBug/")
  WriteProgramStringN(Compiler, "SOURCEALIAS"+Chr(9)+"/Users/fred/Desktop/IncludeBug/XMLtest4.pb")
  WriteProgramStringN(Compiler, "COMPILE"+Chr(9)+"PROGRESS"+Chr(9)+"DEBUGGER")
   
  Repeat
  
    Output$ = ReadProgramString(Compiler)
    PrintN(Output$)
  
  Until Output$ = "OUTPUT"+Chr(9)+"COMPLETE"
  
  

Else
  MessageRequester("Error", "Can't launch the compiler in standby mode")
EndIf
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Post by Mistrel »

The problem was that I didn't realize I could read from the window even when nothing was displayed. #PB_Program_Write is also necessary to prevent it from closing.

It's working great now. Thanks. :)
Post Reply