Reading a process output on OSX

BlitzMax Forums/BlitzMax Programming/Reading a process output on OSX

JoshK(Posted 2011) [#1]
I'm creating a BlitzMax process on OSX, using the same code I use on Windows, but am not reading any output from the launched process. Is this supposed to work on OSX? The application is not built as a GUI program.

Here is my code:
	Method Convert:Int(path:String,commandline:String="")
		path=RealPath(path)
		Local outfile:String = StripExt(path)+"."+outformat
		Local proc:TProcess
		Local s:String
		Local success:Int=False
		
		Print "Converting ~q"+path+"~q to ~q"+outfile+"~q..."
		
		Local procpath:String=RealPath(AppDir+"/"+apppath)
		Print procpath
		proc=CreateProcess(procpath+" +path ~q"+path+"~q"+" "+commandline)
		If proc
			While proc.status()
				Delay 1
			Wend
			While proc.pipe.readavail()
				s=proc.pipe.ReadLine().Trim()
				Print s
				If s.tolower()="success" success=True
			Wend
			proc.close()
			If success
				Return True
			Else
				DeleteFile outfile
				Return False
			EndIf
		Else
			Print "Error: Failed to create process ~q"+procpath+"~q."
			Return False
		EndIf
	EndMethod



skidracer(Posted 2011) [#2]
What is the launched process? You could try replacing the ReadLine code with ReadPipe, dumping the values of the returned byte array so you can verify it's speaking plain ascii with standard end of line characters.

Last edited 2011


JoshK(Posted 2011) [#3]
I found I had to do it like this, and then it worked. ReadLine() will cut off lines after the first one, for some reason:
		path=RealPath(path)
		Local outfile:String = StripExt(path)+"."+outformat
		Local proc:TProcess
		Local s:String
		Local success:Int=False
		Local ba:Byte[]
		Local sarr:String[]
		
		Print "Converting ~q"+path+"~q to ~q"+outfile+"~q..."
		
		Local procpath:String=RealPath(AppDir+"/"+apppath)
		Print procpath
		proc=CreateProcess("~q"+procpath+"~q +path ~q"+path+"~q"+" "+commandline)
		If proc
			While proc.status()
				Delay 1
			Wend
			While proc.pipe.readavail()
				's=proc.pipe.ReadLine().Trim()
				ba = proc.pipe.ReadPipe()
				ba = ba[..ba.length+1]
				ba[ba.length-1]=0
				s:+String.fromcstring(ba)+"~n"
			Wend
			sarr=s.Trim().split("~n")
			For s=EachIn sarr
				Print s
				If s.tolower()="success" success=True
			Next
			proc.close()
			If success
				Return True
			Else
				DeleteFile outfile
				Return False
			EndIf
		Else
			Print "Error: Failed to create process ~q"+procpath+"~q."
			Return False
		EndIf



Sub_Zero(Posted 2011) [#4]
In linux, i had trouble, the process wouldn't start without the HIDECONSOLE option, ie:
Createprocess("./process". HIDECONSOLE)


Last edited 2011