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