home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / SYSPC22.ZIP / SHELL.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1989-02-19  |  5.0 KB  |  181 lines

  1. overlay procedure dos_shell (cmdlin:anystr);
  2.  
  3.  
  4. type
  5. _exec_str255 = string[255];
  6. _exec_str66 = string[66];
  7.  
  8.  
  9. Var
  10.   _Exec_Regs: Record Case Integer Of
  11.                 1: (AX,BX,CX,DX,BP,SI,DI,DS,ES,Flags: Integer);
  12.                 2: (AL,AH,BL,BH,CL,CH,DL,DH: Byte);
  13.               End;
  14.   { NOTE: the above variable is referenced in an Inline statement.  It MUST
  15.     be a global variable (not a local variable or a typed constant)!! }
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22. Function SubProcess(CommandLine: _Exec_Str255): Integer;
  23.   Const
  24.     SSSave: Integer=0;
  25.     SPSave: Integer=0;
  26.  
  27.   Var
  28.     FCB1,FCB2: Array [0..36] Of Byte; {*}
  29.     PathName: _Exec_Str66;            {*}
  30.     CommandTail: _Exec_Str255;
  31.     ParmTable: Record                 {*}
  32.                  EnvSeg: Integer;
  33.                  ComLin: ^Integer;
  34.                  FCB1Pr: ^Integer;
  35.                  FCB2Pr: ^Integer;
  36.                End;
  37.     RegsFlags: Integer;               {*}
  38.     {*: these variables are accessed in an Inline statement; their
  39.         declarations must not be changed }
  40.  
  41.   Begin
  42.     If Pos(' ',CommandLine)=0 Then
  43.      Begin
  44.       PathName:=CommandLine+#0;
  45.       CommandTail:=^M;
  46.      End
  47.     Else
  48.      Begin
  49.       PathName:=Copy(CommandLine,1,Pred(Pos(' ',CommandLine)))+#0;
  50.       CommandTail:=Copy(CommandLine,Pos(' ',CommandLine),255)+^M;
  51.      End;
  52.     CommandTail[0]:=Pred(CommandTail[0]);
  53.     With _Exec_Regs Do
  54.      Begin
  55.       FillChar(FCB1,Sizeof(FCB1),0);
  56.       AX:=$2901;
  57.       DS:=Seg(CommandTail[1]);
  58.       SI:=Ofs(CommandTail[1]);
  59.       ES:=Seg(FCB1);
  60.       DI:=Ofs(FCB1);
  61.       MsDos(_Exec_Regs); { Create FCB 1 }
  62.       FillChar(FCB2,Sizeof(FCB2),0);
  63.       AX:=$2901;
  64.       ES:=Seg(FCB2);
  65.       DI:=Ofs(FCB2);
  66.       MsDos(_Exec_Regs); { Create FCB 2 }
  67.       With ParmTable Do
  68.        Begin
  69.         EnvSeg:=MemW[CSeg:$002C];
  70.         ComLin:=Addr(CommandTail);
  71.         FCB1Pr:=Addr(FCB1);
  72.         FCB2Pr:=Addr(FCB2);
  73.        End;
  74.       InLine($8D/$96/ PathName /$42/  { <DX>:=Ofs(PathName[1]); }
  75.              $8D/$9E/ ParmTable /     { <BX>:=Ofs(ParmTable);   }
  76.              $B8/$00/$4B/             { <AX>:=$4B00;            }
  77.              $1E/$55/                 { Save <DS>, <BP>         }
  78.              $16/$1F/                 { <DS>:=Seg(PathName[1]); }
  79.              $16/$07/                 { <ES>:=Seg(ParmTable);   }
  80.              $2E/$8C/$16/ SSSave /    { Save <SS> in SSSave     }
  81.              $2E/$89/$26/ SPSave /    { Save <SP> in SPSave     }
  82.              $FA/                     { Disable interrupts      }
  83.              $CD/$21/                 { Call MS-DOS             }
  84.              $FA/                     { Disable interrupts      }
  85.              $2E/$8B/$26/ SPSave /    { Restore <SP>            }
  86.              $2E/$8E/$16/ SSSave /    { Restore <SS>            }
  87.              $FB/                     { Enable interrupts       }
  88.              $5D/$1F/                 { Restore <BP>,<DS>       }
  89.              $9C/$8F/$86/ RegsFlags / { Flags:=<CPU flags>      }
  90.              $A3/ _Exec_Regs );       { _Exec_Regs.AX:=<AX>;    }
  91.       If (RegsFlags And 1)<>0 Then SubProcess:=AX
  92.       Else SubProcess:=0;
  93.      End;
  94.   End;
  95.  
  96. Function GetEnvStr(SearchString: _Exec_Str255): _Exec_Str255;
  97.   Type
  98.     Env=Array [0..32767] Of Char;
  99.   Var
  100.     EPtr: ^Env;
  101.     EStr: _Exec_Str255;
  102.     Done: Boolean;
  103.     I: Integer;
  104.  
  105.   Begin
  106.     GetEnvStr:='';
  107.     If SearchString<>'' Then
  108.      Begin
  109.       EPtr:=Ptr(MemW[CSeg:$002C],0);
  110.       I:=0;
  111.       SearchString:=SearchString+'=';
  112.       Done:=False;
  113.       EStr:='';
  114.       Repeat
  115.         If EPtr^[I]=#0 Then
  116.          Begin
  117.           If EPtr^[Succ(I)]=#0 Then
  118.            Begin
  119.             Done:=True;
  120.             If SearchString='==' Then
  121.              Begin
  122.               EStr:='';
  123.               I:=I+4;
  124.               While EPtr^[I]<>#0 Do
  125.                Begin
  126.                 EStr:=EStr+EPtr^[I];
  127.                 I:=Succ(I);
  128.                End;
  129.               GetEnvStr:=EStr;
  130.              End;
  131.            End;
  132.           If Copy(EStr,1,Length(SearchString))=SearchString Then
  133.            Begin
  134.             GetEnvStr:=Copy(EStr,Succ(Length(SearchString)),255);
  135.             Done:=True;
  136.            End;
  137.           EStr:='';
  138.          End
  139.         Else EStr:=EStr+EPtr^[I];
  140.         I:=Succ(I);
  141.       Until Done;
  142.      End;
  143.   End;
  144.  
  145. Function GetComSpec: _Exec_Str66;
  146.   Begin
  147.     GetComSpec:=GetEnvStr('COMSPEC');
  148.   End;
  149.  
  150.  
  151.  
  152. Function SubProcessViaCOMMAND(CommandLine: _Exec_Str255): Integer;
  153.   Begin
  154.     SubProcessViaCOMMAND:=SubProcess(GetComSpec+' /C '+CommandLine);
  155.   End;
  156.  
  157.  
  158. Function Shell: Integer;
  159.   Begin
  160.     Shell:=SubProcess(GetComSpec);
  161.   End;
  162.  
  163.  
  164. Function SubProcessReturnCode: Integer;
  165.   Begin
  166.     _Exec_Regs.AH:=$4D;
  167.     MsDos(_Exec_Regs);
  168.     SubProcessReturnCode:=_Exec_Regs.AX;
  169.   End;
  170.  
  171.  
  172. var
  173.   temp : integer;
  174.  
  175. begin
  176. UNINIT;
  177. IF CMDLiN='*' THEN WRITELN (USR,' Type EXIT to return back!');
  178.   if cmdlin='*' then temp:=shell else temp := subprocessviacommand(cmdlin);
  179.              setparam (usecom,baudrate,parity);
  180. Shell_Return_Code:=subprocessreturnCode;
  181. end;