Hello together
I have the following function to "read" a regular string from stdin:
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:
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 program
end.
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:
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)
MyProcess := TProcess.Create(NIL);
MyProcess.Executable := 'project1'; // the program with the "GetStdIn" function
MyProcess.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 stderr
while MyProcess.Running or (MyProcess.Output.NumBytesAvailable > 0) or
(MyProcess.Stderr.NumBytesAvailable > 0) do
begin
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