home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / pascal / library / dos / pibsoft / menu / pibdodos.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1986-01-16  |  11.5 KB  |  229 lines

  1. (*$V-,R-*)
  2. PROGRAM PibDoDos;
  3.  
  4. (*--------------------------------------------------------------------------*)
  5. (*                                                                          *)
  6. (*     Program:  PibDoDos                                                   *)
  7. (*                                                                          *)
  8. (*     Purpose:  Demonstrate TURBORUN.COM, an assembler routine to          *)
  9. (*               execute DOS commands.                                      *)
  10. (*                                                                          *)
  11. (*     Author:   Philip R. Burns                                            *)
  12. (*     Version:  1.0                                                        *)
  13. (*     Date:     April, 1985                                                *)
  14. (*                                                                          *)
  15. (*     Credits:  The external routine TURBORUN.COM was written by           *)
  16. (*               John Cooper and John Falconer.                             *)
  17. (*                                                                          *)
  18. (*               TURBORUN should be available on the same BBS as you found  *)
  19. (*               this program on.                                           *)
  20. (*                                                                          *)
  21. (*     Remarks:  This program demonstrates the external routine TURBORUN    *)
  22. (*               which allows Turbo Pascal programs to execute DOS commands *)
  23. (*               or other programs dynamically.  The environment string is  *)
  24. (*               searched for COMSPEC= to obtain the current setting of     *)
  25. (*               COMMAND.COM.  Then a prompt is issued for a command to be  *)
  26. (*               executed.  The command is passed to DOS for execution, if  *)
  27. (*               possible, and then control returns to this program.  The   *)
  28. (*               prompt for a new command is issued, and this continues     *)
  29. (*               until the command 'END' (in all capital letters) is        *)
  30. (*               entered.                                                   *)
  31. (*                                                                          *)
  32. (*               Note:  Entering a null line invokes a secondary copy of    *)
  33. (*                      the DOS command processor.  Enter an EXIT to get    *)
  34. (*                      back to this program.                               *)
  35. (*                                                                          *)
  36. (*     Glitches: DOS may freeze up if there is not enough memory to execute *)
  37. (*               the command, or if the command clobbers memory that does   *)
  38. (*               not belong to it.  In these cases, a re-boot is needed.    *)
  39. (*                                                                          *)
  40. (*               Note:  You should compile this to a .COM file, and set     *)
  41. (*                      the maximum heap size (A----) so that there is      *)
  42. (*                      enough memory for the program to be executed.       *)
  43. (*                                                                          *)
  44. (*--------------------------------------------------------------------------*)
  45. (*                                                                          *)
  46. (*     Send comments, suggestions, etc. to PHILIP BURNS on either of the    *)
  47. (*     following two Chicago BBSs:                                          *)
  48. (*                                                                          *)
  49. (*       Gene Plantz's BBS (312) 882 4227                                   *)
  50. (*       Ron Fox's BBS     (312) 940 6496                                   *)
  51. (*                                                                          *)
  52. (*--------------------------------------------------------------------------*)
  53.  
  54. CONST
  55.    NUL = #00                       (* Terminator for DOS Ascii z-strings *);
  56.  
  57. TYPE
  58.    AnyStr     = STRING[255];
  59.    Char_Array = ARRAY[1..1] OF CHAR;
  60.    Char_Ptr   = ^Char_Array;
  61.  
  62. VAR
  63.    Command_Line: AnyStr            (* Command to be executed       *);
  64.    Return_Code:  INTEGER           (* DOS return code              *);
  65.    ComSpec:      AnyStr            (* Comspec from DOS environment *);
  66.  
  67. (*--------------------------------------------------------------------------*)
  68. (*    RunExt --- invoke external assembler program to execute DOS command   *)
  69. (*--------------------------------------------------------------------------*)
  70.  
  71. PROCEDURE RunExt( VAR Ret_Code: INTEGER;
  72.                   VAR Command_Line );
  73.  
  74. (*--------------------------------------------------------------------------*)
  75. (*                                                                          *)
  76. (*     Procedure:  RunExt (EXTERNAL ASM)                                    *)
  77. (*                                                                          *)
  78. (*     Purpose:    Performs DOS execute on given command                    *)
  79. (*                                                                          *)
  80. (*     Calling Sequence:                                                    *)
  81. (*                                                                          *)
  82. (*        RunExt( VAR Ret_Code: INTEGER;  VAR Command_Line );               *)
  83. (*                                                                          *)
  84. (*           Ret_Code     --- return code from DOS.                         *)
  85. (*           Command_Line --- contains command to be executed.              *)
  86. (*                            If parameter passed is a string, then         *)
  87. (*                            be sure to specify 'Command_Line[1]' as       *)
  88. (*                            the actual argument.                          *)
  89. (*                                                                          *)
  90. (*     Remarks:                                                             *)
  91. (*                                                                          *)
  92. (*        This routine is an external assembler routine.                    *)
  93. (*                                                                          *)
  94. (*--------------------------------------------------------------------------*)
  95.  
  96.    EXTERNAL 'TURBORUN.COM';
  97.  
  98.  
  99. (*--------------------------------------------------------------------------*)
  100. (*        Get_ComSpec --- Get location of Command.Com from environment      *)
  101. (*--------------------------------------------------------------------------*)
  102.  
  103. PROCEDURE Get_ComSpec( VAR ComSpec: AnyStr );
  104.  
  105. (*--------------------------------------------------------------------------*)
  106. (*                                                                          *)
  107. (*     Procedure:  Get_ComSpec                                              *)
  108. (*                                                                          *)
  109. (*     Purpose:    Gets location of COMMAND.COM from DOS environment        *)
  110. (*                                                                          *)
  111. (*     Calling Sequence:                                                    *)
  112. (*                                                                          *)
  113. (*        Get_Comspec( VAR ComSpec: AnyStr );                               *)
  114. (*                                                                          *)
  115. (*           ComSpec --- Returned file specification for COMMAND.COM        *)
  116. (*                       in 'drive:\directory\COMMAND.COM' form.            *)
  117. (*                                                                          *)
  118. (*     Calls:  None                                                         *)
  119. (*                                                                          *)
  120. (*     Remarks:                                                             *)
  121. (*                                                                          *)
  122. (*        This routine assumes that the COMSPEC= parameter actually exists  *)
  123. (*        in the environment (it should).                                   *)
  124. (*                                                                          *)
  125. (*--------------------------------------------------------------------------*)
  126.  
  127. CONST
  128.    ComSpec_String: String[7] = 'OMSPEC=';
  129.  
  130. VAR
  131.    Env_Ptr:     Char_Ptr;
  132.    Env_Pos:     INTEGER;
  133.    Env_Found:   BOOLEAN;
  134.    Spec_Pos:    INTEGER;
  135.    I:           INTEGER;
  136.  
  137. BEGIN (* Get_ComSpec *)
  138.                                    (* Initialize ComSpec to null string *)
  139.    ComSpec     := '';
  140.                                    (* Pick up starting address, offset of *)
  141.                                    (* DOS environment string.             *)
  142.  
  143.    Env_Ptr     := PTR( MEMW[ CSEG: $2C] , 0 );
  144.    Env_Pos     := 0;
  145.                                    (* Search for COMSPEC= in environment.  *)
  146.                                    (* Following will be file definition of *)
  147.                                    (* COMMAND.COM.                         *)
  148.    REPEAT
  149.                                    (* Look for initial 'C' of 'COMSPEC='   *)
  150.  
  151.       WHILE( Env_Ptr^[Env_Pos] <> 'C' ) DO
  152.          Env_Pos := Env_Pos + 1;
  153.                                    (* Flag indicating environment string   *)
  154.                                    (* has been found -- assume TRUE to     *)
  155.                                    (* start                                *)
  156.       Env_Found := TRUE;
  157.  
  158.       I        := 1;
  159.                                    (* Check characters after 'C'.  Are they *)
  160.                                    (* 'OMSPEC=' ?                           *)
  161.  
  162.       WHILE ( Env_Found AND ( I < 8 ) ) DO
  163.          IF Env_Ptr^[Env_Pos + I] = ComSpec_String[ I ] THEN
  164.             I := I + 1
  165.          ELSE
  166.             Env_Found := FALSE;
  167.  
  168.       Spec_Pos := Env_Pos + I;
  169.                                    (* If 'OMSPEC=' found, then we found  *)
  170.                                    (* the comspec.  If not, keep going.  *)
  171.  
  172.       IF ( I = 8 ) THEN
  173.          Env_Found := TRUE
  174.       ELSE
  175.          BEGIN
  176.             WHILE ( Env_Ptr^[Spec_Pos] <> NUL ) DO
  177.                Spec_Pos := Spec_Pos + 1;
  178.             Env_Pos := Spec_Pos;
  179.          END;
  180.  
  181.    UNTIL Env_Found;
  182.  
  183.                                    (* Pick up the COMMAND.COM definition  *)
  184.                                    (* following the COMSPEC=.             *)
  185.  
  186.    WHILE ( Env_Ptr^[Spec_Pos] <> NUL ) DO
  187.       BEGIN
  188.          ComSpec  := ComSpec + Env_Ptr^[Spec_Pos];
  189.          Spec_Pos := Spec_Pos + 1;
  190.       END;
  191.  
  192. END   (* Get_ComSpec *);
  193.  
  194. (*--------------------------------------------------------------------------*)
  195.  
  196. BEGIN (* PibDoDos -- Main Program *)
  197.  
  198.                                    (* Obtain location of Command.Com *)
  199.     Get_ComSpec( ComSpec );
  200.     Writeln('Comspec = ',ComSpec);
  201.  
  202.                                    (* Read commands until 'END' entered *)
  203.     REPEAT
  204.  
  205.        WRITELN;
  206.        WRITE('Enter command or END to stop: ');
  207.  
  208.        READLN( Command_Line );
  209.  
  210.        IF Command_Line <> 'END' THEN
  211.           BEGIN
  212.                                    (* Prefix comspec to command line *)
  213.  
  214.              IF LENGTH( Command_Line ) > 0 THEN
  215.                 Command_Line := ComSpec + ' /C ' + Command_Line + NUL
  216.              ELSE
  217.                 Command_Line := ComSpec + NUL;
  218.  
  219.                                    (* Execute the command *)
  220.  
  221.              RunExt( Return_Code , Command_Line[1] );
  222.  
  223.           END;
  224.  
  225.     UNTIL ( Command_Line = 'END' );
  226.  
  227. END   (* PibDoDos *).
  228.  
  229.