home *** CD-ROM | disk | FTP | other *** search
/ POINT Software Programming / PPROG1.ISO / pascal / swag / redirect.swg / 0005_REDIRCT3.PAS.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1993-05-28  |  4.0 KB  |  142 lines

  1. {
  2. MARK LEWIS
  3.  
  4. >> Still need a bit of help here.  I can't redirect output from a
  5. >> Program when executing it from a Pascal Program!  Is there any
  6. >> this from Pascal? Any help would be greatly appreciated.
  7. > if I understand you, you are using the Exec Procedure to run a
  8. > Program.  if that is the Case you won't be ablr to redirect since
  9. > this is a Function of Dos and not the Program you exec.  You will
  10. > need to run the Program through a child process in order to
  11. > perform the redirect, something like:
  12. > Exec(GetEnv('COMSPEC'),'/C MyProg.exe>redirect');
  13.  
  14. one could also utilize duplicate File handles -=B-)
  15.  
  16. }
  17. Unit Execute;
  18.  
  19. Interface
  20.  
  21. Procedure Exec(Path, CmdLine : String);
  22.  
  23. Implementation
  24.  
  25. Uses
  26.   Dos;
  27.  
  28. Function ExtractFileName(Var Line : String; Index : Integer) : String;
  29. Var
  30.   Temp : String;
  31. begin
  32.   Delete(Line, Index, 1);
  33.   While (Index <= Length(Line)) and (Line[Index] = ' ') Do
  34.     Delete(Line, Index, 1);
  35.   Temp := '';
  36.   While (Index <= Length(Line)) and (Line[Index] <> ' ') Do
  37.   begin
  38.     Temp := Temp + Line[Index];
  39.     Delete(Line, Index, 1);
  40.   end;
  41.   ExtractFileName := Temp;
  42. end;
  43.  
  44. Procedure CloseHandle(Handle : Word);
  45. Var
  46.   Regs : Registers;
  47. begin
  48.   With Regs Do
  49.   begin
  50.     AH := $3E;
  51.     BX := Handle;
  52.     MsDos(Regs);
  53.   end;
  54. end;
  55.  
  56. Procedure Duplicate(SourceHandle : Word;Var TargetHandle : Word);
  57. Var
  58.   Regs : Registers;
  59. begin
  60.   With Regs Do
  61.   begin
  62.     AH := $45;
  63.     BX := SourceHandle;
  64.     MsDos(Regs);
  65.     TargetHandle := AX;
  66.   end;
  67. end;
  68.  
  69. Procedure ForceDuplicate(SourceHandle : Word;Var TargetHandle : Word);
  70. Var
  71.   Regs : Registers;
  72. begin
  73.   With Regs Do
  74.   begin
  75.     AH := $46;
  76.     BX := SourceHandle;
  77.     CX := TargetHandle;
  78.     MsDos(Regs);
  79.     TargetHandle := AX;
  80.   end;
  81. end;
  82.  
  83. Procedure Exec(Path,CmdLine : String);
  84. Var
  85.   StdIn,
  86.   Stdout    : Word;
  87.   Index     : Integer;
  88.   FName     : String[80];
  89.   InFile,
  90.   OutFile   : Text;
  91.   InHandle,
  92.   OutHandle : Word;
  93.          { ===============>>>> }   { change below For STDERR }
  94. begin
  95.   StdIn  := 0;
  96.   StdOut := 1;                    { change to 2 For StdErr       }
  97.   Duplicate(StdIn, InHandle);      { duplicate standard input     }
  98.   Duplicate(StdOut, OutHandle);    { duplicate standard output    }
  99.   Index := Pos('>', CmdLine);
  100.   if Index > 0 Then               { check For output redirection }
  101.   begin
  102.     FName := ExtractFileName(CmdLine, Index);  { get output File name  }
  103.     Assign(OutFile, FName);                    { open a Text File      }
  104.     ReWrite(OutFile);                         { .. For output         }
  105.     ForceDuplicate(TextRec(OutFile).Handle, StdOut);{ make output same }
  106.   end;
  107.   Index := Pos('<', CmdLine);
  108.   if Index > 0 Then               { check For input redirection }
  109.   begin
  110.     FName := ExtractFileName(CmdLine, Index);  { get input File name  }
  111.     Assign(InFile, FName);                     { open a Text File     }
  112.     Reset(InFile);                            { For input            }
  113.     ForceDuplicate(TextRec(InFile).Handle, StdIn);  { make input same }
  114.   end;
  115.   Dos.Exec(Path, CmdLine);           { run EXEC }
  116.   ForceDuplicate(InHandle, StdIn);   { put standard input back to keyboard }
  117.   ForceDuplicate(OutHandle, StdOut); { put standard output back to screen  }
  118.   CloseHandle(InHandle);            { close the redirected input File     }
  119.   CloseHandle(OutHandle);           { close the redirected output File    }
  120. end;
  121.  
  122. end.
  123.  
  124. {===============================================================}
  125. {
  126. Use it exactly as you would the normal EXEC Procedure:
  127.  
  128.   Exec('MAsm.EXE','mystuff.Asm');
  129.  
  130. To activate redirection simply add the redirection symbols, etc:
  131.  
  132.   Exec('MAsm.EXE','mystuff.Asm >err.lst');
  133.  
  134.  
  135. One note of caution.  This routine temporarily Uses extra handles. It's
  136. either two or four more.  The Various books I have are not clear as to
  137. whether duplicated handles 'count' or not. My guess is yes.  if you don't
  138. plan on redirecting STDIN then remove all the code For duplicating it to
  139. cut your handle overhead in half.
  140. }
  141.  
  142.