Forum > Unix

TProcess EOF - CloseInput

(1/1)

Snify:
Hello together :)

I have the following function to "read" a regular string from stdin:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---function GetStdIn: string;var   tmp: string;begin  result := '';  while not EOF do begin    readln (tmp);    if EOF then begin      result:= result + tmp;    end else begin      result:= result + tmp + LineEnding;    end;  end;end;    
The full test program looks like this:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---begin  writeln (StdErr, 'FIRST ERROR LINE');  writeln ('This line and the following stdin detection is on stdout');  writeln ('Stdin was:', LineEnding, GetStdIn);  writeln (StdErr, 'This is on stderror');  writeln ('Regular stdout again');  // end of programend.                               
I am not sure if my function is correct.
This works like a charm if I use the above function in a shell like this:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---printf "row1\nrow2\n..." | ./project1

However when I use the TProcess Class with the WriteInput Method, the process locks.

This is what I use to execute my program and try to read and write to the pipes:
(some of the code is taken from https://wiki.freepascal.org/Executing_External_Programs)



--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---MyProcess               := TProcess.Create(NIL);    MyProcess.Executable    := 'project1'; // the program with the "GetStdIn" functionMyProcess.Options               := MyProcess.Options + [poPassInput, poUsePipes];MyProcess.Execute;mystring                                := 'Hello Lazarus';MyProcess.Input.Write(mystring[1], Length(mystring));MyProcess.CloseInput; // does not trigger EOF?! // NumBytesAvailable is always 0 for output and stderrwhile MyProcess.Running or (MyProcess.Output.NumBytesAvailable > 0) or(MyProcess.Stderr.NumBytesAvailable > 0) dobegin    while MyProcess.Output.NumBytesAvailable > 0 do    begin        ReadCount := Min(512, MyProcess.Output.NumBytesAvailable); //Read up to buffer, not more        MyProcess.Output.Read(CharBuffer, ReadCount);    end;                                      while MyProcess.Stderr.NumBytesAvailable > 0 do    begin        ReadCount := Min(512, MyProcess.Stderr.NumBytesAvailable); //Read up to buffer, not more        MyProcess.Stderr.Read(CharBuffer, ReadCount);    end;end;MyProcess.Free;                                                                                 
The binary path etc. is simplified here and executes just as it should be.
If I remove the whole stdin stuff, it works just fine.

Can someone spot my error?

Thank you in advance

Snify:
I just figured out the problem.
Thank you.

I needed to avoid "poPassInput".
This causes to use the stdin from the "parent" process instead of it's own stdin.

Navigation

[0] Message Index

Go to full version