home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / TPTOOL5.ZIP / TPEXEC.INC < prev    next >
Encoding:
Text File  |  1987-03-28  |  4.8 KB  |  185 lines

  1.  
  2. const tpexec_tag: string[90]
  3.    = #0'@(#)CURRENT_FILE LAST_UPDATE Execute DOS commands 1.0'#0;
  4. #log Execute DOS commands 1.0
  5.  
  6. (*
  7.  * exec.inc - execute dos command line from turbo pascal
  8.  *
  9.  * note:  compile with max-heap < $1000 to leave room for
  10.  *        the subprogram!
  11.  *
  12.  *)
  13.  
  14. const
  15.    null =              #00;
  16.    cr =                #13;
  17.  
  18. type
  19.    longstring =        string [64];
  20.  
  21.    fcb =               array [1..37] of char;
  22.  
  23. var
  24.    reg:                regpack;
  25.    fcb1,
  26.    fcb2:               fcb;
  27.    filespec,
  28.    dta,
  29.    psp80:              longstring;
  30.    flagreg,
  31.    exitstat,
  32.    memreq:             integer;
  33.  
  34.    parm_blk:           record
  35.          environ:            integer;
  36.          clp,
  37.          f1,
  38.          f2:                 ^longstring;
  39.    end;
  40.  
  41.  
  42.  
  43. function exec (comname:      longstring;
  44.                params:       longstring): boolean;
  45.                            {-execute a .COM or .EXE file using the
  46.                             standard command processor.  return
  47.                             TRUE if any errorlevels other than 0}
  48.  
  49.  
  50. procedure shrinkmem (memreq:             integer);
  51.                            {-free memory above this program for use by
  52.                              the DOS command}
  53.  
  54. var
  55.    membyte:            real;
  56.  
  57. begin
  58.    memreq := memavail + seg (heapptr^)- cseg + 800;
  59.    parm_blk.environ := memw [cseg : $2C];
  60.    parm_blk.clp := ptr (dseg, ofs (psp80 [1]));
  61.    parm_blk.f1 := ptr (dseg, ofs (fcb1 [1]));
  62.    parm_blk.f2 := ptr (dseg, ofs (fcb2 [1]));
  63.  
  64.    membyte := 16.0 * Int (memreq);
  65.    if membyte > 128000.0 then
  66.    begin
  67.       writeln(whoami,':  Program requires ', membyte : 6 : 0, ' bytes');
  68.       writeln('The Max-Heap option was probably not set when ',whoami);
  69.       writeln('was last compiled.');
  70.       halt(1);
  71.    end;
  72.  
  73.    reg.es := cseg;
  74.    reg.bx := memreq;
  75.    reg.ax := $4A00;
  76.    msdos(reg);                {use DOS SETBLOCK function}
  77.  
  78.  
  79.    if (reg.flags and 1)= 1 then
  80.    begin
  81.       writeln(whoami,':  Memory allocation error');
  82.       halt(1);
  83.    end;
  84. end;                       {shrinkmem}
  85.  
  86.  
  87.  
  88. procedure initfcb (var x:              fcb);
  89.                            {-initialize a file control block}
  90.  
  91. var
  92.    i:                  integer;
  93.  
  94. begin
  95.    x[1]:= null;               {drive ID}
  96.  
  97.    for i := 2 to 12 do
  98.       x[i]:= '?';             {filename and extension as wildcards}
  99.  
  100.    for i := 13 to 37 do
  101.       x[i]:= null;
  102. end;                       {init}
  103.  
  104.  
  105.  
  106. procedure callexec;        {-preserve registers and use DOS EXEC function}
  107.  
  108. begin
  109.    inline                     {save the registers which will be wiped
  110.                                 out}
  111.  
  112.    ($9C/                    {pushf}
  113.     $2E/$89/$2E/$80/$00 /   {MOV cs:80H,BP}
  114.     $2E/$89/$26/$82/$00 /   {MOV cs:82H,SP}
  115.     $2E/$8C/$1E/$84/$00 /   {MOV cs:84H,DS}
  116.     $2E/$8C/$16/$86/$00 /   {MOV cs:86H,SS}
  117.     $1E/$07/                {mov ES,DS}
  118.     $BA/filespec/           {mov dx,offset(filespec[0])}
  119.     $42/                    {inc dx (to point to filespec[1])}
  120.     $BB/parm_blk/           {mov bx,offset(parm_block)}
  121.     $B8/$00/$4B/            {mov ax,4B00H}
  122.     $FB/                    {sti}
  123.     $cd/$21/                {int 21 - call EXEC}
  124.     $FA/                    {cli: avoid interrupts while restoring stack}
  125.     $2E/$8B/$2E/$80/$00 /   {MOV BP,cs:80H}
  126.     $2E/$8B/$26/$82/$00 /   {MOV SP,cs:82H}
  127.     $2E/$8E/$1E/$84/$00 /   {MOV DS,cs:84H}
  128.     $2E/$8E/$16/$86/$00 /   {MOV SS,cs:86H}
  129.     $FB/                    {sti}
  130.     $9C/                    {pushf}
  131.     $58/                    {pop ax}
  132.     $A3/flagreg/            {mov [flagreg],ax}
  133.     $B8/$00/$4D/            {mov ax,4D00H}
  134.     $cd/$21/                {int 21 - call WAIT (return exit code)}
  135.     $A3/exitstat/           {mov [exitstat],ax}
  136.     $2E/$8B/$2E/$80/$00 /   {MOV BP,cs:80H}
  137.     $9D);                   {popf}
  138.  
  139.  
  140.    writeln;
  141.  
  142.    if odd (flagreg) then
  143.    begin
  144.       exec := true;
  145.       writeln(whoami,':  Could not execute ',comname);
  146.    end
  147.    else
  148.  
  149.    if exitstat <> 0 then
  150.    begin
  151.       exec := true;
  152.       writeln(whoami,':  Abnormal program exit (code=',exitstat,')');
  153.    end
  154.  
  155.    else
  156.       exec := false;    {normal termination}
  157.  
  158.  
  159. end;                       {callexec}
  160.  
  161.  
  162.  
  163.  
  164. begin                         {exec}
  165.  
  166.    shrinkmem(memreq);         {release memory above this program (each time)}
  167.  
  168.    initfcb(fcb1);
  169.    initfcb(fcb2);
  170.    psp80 := params;
  171.    psp80 := chr (length (psp80))+ psp80 + cr + null;
  172.                               {pass length in first byte}
  173.  
  174.    filespec := comname + null;
  175.                               {path and name of command processor}
  176.  
  177.    flush(output);
  178.  
  179.    callexec;                  {execute program and set 'exec' return value}
  180.  
  181.    flush(output);
  182.  
  183. end;                       {exec}
  184.  
  185.