Browse Source

* body of runcommand to TProcess class. If more flexibility is added to this function (onevent properties called in the loop), it becomes easier to run your own with slightly different features.

git-svn-id: trunk@39516 -
marco 7 years ago
parent
commit
70c09d86a8
1 changed files with 38 additions and 20 deletions
  1. 38 20
      packages/fcl-process/src/process.pp

+ 38 - 20
packages/fcl-process/src/process.pp

@@ -124,6 +124,8 @@ Type
     Function WaitOnExit : Boolean;
     Function WaitOnExit(Timeout : DWord) : Boolean;
     function ReadInputStream(p:TInputPipeStream;var BytesRead:integer;var DataLength:integer;var Data:string;MaxLoops:integer=10):boolean;
+    function intRuncommand(out outputstring:string;out stderrstring:string; out anexitstatus:integer):integer;
+
     Property WindowRect : Trect Read GetWindowRect Write SetWindowRect;
     Property Handle : THandle Read FProcessHandle;
     Property ProcessHandle : THandle Read FProcessHandle;
@@ -502,41 +504,40 @@ end;
 // helperfunction that does the bulk of the work.
 // We need to also collect stderr output in order to avoid
 // lock out if the stderr pipe is full.
-function internalRuncommand(p:TProcess;out outputstring:string;
-                            out stderrstring:string; out exitstatus:integer):integer;
+function TProcess.intRuncommand(out outputstring:string;
+                            out stderrstring:string; out anexitstatus:integer):integer;
 var
     numbytes,bytesread,available : integer;
     outputlength, stderrlength : integer;
     stderrnumbytes,stderrbytesread : integer;
 begin
   result:=-1;
-  try
     try
-    p.Options := p.Options + [poUsePipes];
+    Options := Options + [poUsePipes];
     bytesread:=0;
     outputlength:=0;
     stderrbytesread:=0;
     stderrlength:=0;
-    p.Execute;
-    while p.Running do
+    Execute;
+    while Running do
       begin
         // Only call ReadFromStream if Data from corresponding stream
         // is already available, otherwise, on  linux, the read call
         // is blocking, and thus it is not possible to be sure to handle
         // big data amounts bboth on output and stderr pipes. PM.
-        if not p.ReadInputStream(p.output,BytesRead,OutputLength,OutputString,1) then
+        if not ReadInputStream(output,BytesRead,OutputLength,OutputString,1) then
           // The check for assigned(P.stderr) is mainly here so that
           // if we use poStderrToOutput in p.Options, we do not access invalid memory.
-          if assigned(p.stderr) then
-            if not p.ReadInputStream(p.StdErr,StdErrBytesRead,StdErrLength,StdErrString,1) then
+          if assigned(stderr) then
+            if not ReadInputStream(StdErr,StdErrBytesRead,StdErrLength,StdErrString,1) then
               sleep(100);
       end;
     // Get left output after end of execution
-    p.ReadInputStream(p.output,BytesRead,OutputLength,OutputString,250);
+    ReadInputStream(output,BytesRead,OutputLength,OutputString,250);
     setlength(outputstring,BytesRead);
-    p.ReadInputStream(p.StdErr,StdErrBytesRead,StdErrLength,StdErrString,250);
+    ReadInputStream(StdErr,StdErrBytesRead,StdErrLength,StdErrString,250);
     setlength(stderrstring,StderrBytesRead);
-    exitstatus:=p.exitstatus;
+    anexitstatus:=exitstatus;
     result:=0; // we came to here, document that.
     except
       on e : Exception do
@@ -546,9 +547,6 @@ begin
            setlength(stderrstring,StderrBytesRead);
          end;
      end;
-  finally
-    p.free;
-    end;
 end;
 
 { Functions without StderrString }
@@ -571,7 +569,11 @@ begin
   if high(commands)>=0 then
    for i:=low(commands) to high(commands) do
      p.Parameters.add(commands[i]);
-  result:=internalruncommand(p,outputstring,errorstring,exitstatus);
+  try
+    result:=p.intRuncommand(outputstring,errorstring,exitstatus);
+  finally
+    p.free;
+  end;
 end;
 
 function RunCommandInDir(const curdir,cmdline:string;out outputstring:string):boolean; deprecated;
@@ -584,7 +586,11 @@ begin
   p.setcommandline(cmdline);
   if curdir<>'' then
     p.CurrentDirectory:=curdir;
-  result:=internalruncommand(p,outputstring,errorstring,exitstatus)=0;
+  try
+    result:=p.intRuncommand(outputstring,errorstring,exitstatus)=0;
+  finally
+    p.free;
+  end;
   if exitstatus<>0 then result:=false;
 end;
 
@@ -604,7 +610,11 @@ begin
   if high(commands)>=0 then
    for i:=low(commands) to high(commands) do
      p.Parameters.add(commands[i]);
-  result:=internalruncommand(p,outputstring,errorstring,exitstatus)=0;
+  try
+    result:=p.intRuncommand(outputstring,errorstring,exitstatus)=0;
+  finally
+    p.free;
+  end;
   if exitstatus<>0 then result:=false;
 end;
 
@@ -616,7 +626,11 @@ Var
 begin
   p:=TProcess.create(nil);
   p.setcommandline(cmdline);
-  result:=internalruncommand(p,outputstring,errorstring,exitstatus)=0;
+  try
+    result:=p.intRuncommand(outputstring,errorstring,exitstatus)=0;
+  finally
+    p.free;
+  end;
   if exitstatus<>0 then result:=false;
 end;
 
@@ -634,7 +648,11 @@ begin
   if high(commands)>=0 then
    for i:=low(commands) to high(commands) do
      p.Parameters.add(commands[i]);
-  result:=internalruncommand(p,outputstring,errorstring,exitstatus)=0;
+  try
+    result:=p.intRuncommand(outputstring,errorstring,exitstatus)=0;
+  finally
+    p.free;
+  end;
   if exitstatus<>0 then result:=false;
 end;