home *** CD-ROM | disk | FTP | other *** search
/ POINT Software Programming / PPROG1.ISO / pascal / swag / redirect.swg / 0001_DOS-REDR.PAS.pas next >
Encoding:
Pascal/Delphi Source File  |  1993-05-28  |  3.4 KB  |  135 lines

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