home *** CD-ROM | disk | FTP | other *** search
/ C!T ROM 2 / ctrom_ii_b.zip / ctrom_ii_b / PROGRAM / PASCAL / 30TURUTL / TURRUN.ASM < prev    next >
Assembly Source File  |  1985-02-18  |  17KB  |  382 lines

  1. Page 60,132
  2. Title TURBORUN a utility for Turbo Pascal, MS/PC-DOS v2.xx
  3. Name TurboRun
  4.  
  5. ;************************************************************************
  6. ;*                                                                      *
  7. ;*                            T U R B O R U N                           *
  8. ;*                              Version 1.0B                            *
  9. ;*                                                                      *
  10. ;*                 By John Cooper       John Falconer                   *
  11. ;*                    (913) 262-8451    (415) 521-7245                  *
  12. ;*                    CIS  74775,756    CIS  72435,1617                 *
  13. ;*                                                                      *
  14. ;************************************************************************
  15.  
  16. Comment*
  17.  
  18. Revision History
  19.         Version 1.0 was successfully tested on 12/10/84.
  20.            By John Cooper
  21.         
  22.         Updated to Version 1.0A on 12/22/84. 
  23.                    By John Falconer  
  24.                                       
  25.             1.  Changed ssmax from a constant to a code-base-relative
  26.                 value.  This change allows Turborun to be used with
  27.                 different versions of Turbo Pascal.  Although undocumented,
  28.                 the four versions of Turbo I tested all store their
  29.                 maximum stack segment size as a word located seven bytes
  30.                 ahead of the beginning of user code. 
  31.             2.  Added code to determine the length of the command line
  32.                 which is passed to the child process.  Moved the child's
  33.                 command line starting point to PSP+081H as called for in
  34.                 the DOS manual (Ver 2.0 page E-6) and placed the length
  35.                 byte for the command line at PSP+080H.  This allows the
  36.                 command line passed to the child to be used as a string
  37.                 with string[0] containing the valid length.  Note that
  38.                 a carriage return is placed at the end of the passed command
  39.                 line but that it is not counted in the length.  Note also
  40.                 that this change corrupts the CmdLine variable passed
  41.                 to Turborun by changing the last character of the file
  42.                 name to the length of the child's CmdLine.
  43.             3.  Changed original code so that all characters on the command
  44.                 line are passed on to the child.  This is as called for in 
  45.                 the DOS manual.  Previously the first terminator (usually
  46.                 a space) was skipped over.
  47.             4.  Corrected a few minor documentation errors.
  48.  
  49.  
  50.     Updated to Version 1.0B January 10, 1985
  51.            By John Cooper 
  52.  
  53.         1.  A note concerning the lack of IO-redirection support
  54.         by the MS/PC DOS EXEC function call, the call which is
  55.         at the heart of this program, was removed, as it has 
  56.         been determined that, at least in the case of MS-DOS 2.11,
  57.         EXEC does provide full IO-redirection, via the command line
  58.         symbols "> < |".
  59.  
  60.         2.  Changed the way in which the program detects the end
  61.         of the childs name, to include all filename separators,
  62.         as listed on page 1-88 of MS-DOS v2.0 Programmer's 
  63.         Reference, except the back-slash (\) and colon (:).  These
  64.         two were excluded because the program needs to accept a
  65.         full-path name, which could include these symbols.  Pre-
  66.         viously only a space or null was considered a terminator.
  67.         This change was recommended but not implemented in version
  68.         1.0A.
  69.  
  70.         3.  Added notes on installation concerning the requirement,
  71.         introduced by an enhancement made in 1.0A, that the
  72.         external declaration of this program be the first in the
  73.         Turbo source code.  Also added some warnings and documentation
  74.         notes.
  75.  
  76. *
  77. Page
  78. Comment*
  79.  
  80.                          - - - W A R N I N G - - -
  81.     Although every effort has been made, by the two authors, to
  82.     ensure that this program will perform as specified, there are
  83.     many programs which will not function correctly when envoked
  84.     from Turbo, via Turborun, but will do just fine if envoked
  85.     from COMMAND.COM.  An excellent example is WordStar(tm).  In
  86.     some, but not all, cases this can be solved by first envoking
  87.     COMMAND.COM and then envoking the child from COMMAND.COM.  
  88.     Assume that there is a program, WORKING.COM, that will not
  89.     work from Turborun but does work from DOS.  The following
  90.     command line might help;
  91.  
  92.      COMMAND.COM /c WORKING.COM
  93.  
  94.     This of course assume that there is not a shortage of
  95.     memory of some other explainable problem.  The switch, /c,
  96.     is included to force COMMAND.COM to return control to the
  97.     parent after the processing of its, COMMAND.COM's, command line.
  98.  
  99.  
  100. Installation Notes:
  101.         The enhancement to provide for a version-independent SSMAX,
  102.     provided by John F., has introduced the additional requirement
  103.     that this external be the first code generating structute in
  104.     the Turbo Pascal source code, i.e. make TurboRun the first 
  105.     procedure, following the Const, Type, and Var blocks, in the
  106.     main program.  Turborun assumes it is the first and can,
  107.     therefore, find the SSMAX word exactly seven bytes prior to
  108.     its own base address.
  109.  
  110.  
  111.     Memory Compiles.
  112.         The use of this program in "memory mode" is not recom-
  113.         mended.  Turbo seems to dislike having its memory 
  114.         limited, which is required to run the child.  It may
  115.         work but it is, at best, risky!  The prefered method
  116.         is to do a memory-compile, if possible, then switch to
  117.         Com and limit the Maximum Stack Size and compile again,
  118.         this time producing a COM file.  If compilation to memory
  119.         is not possible, due to lack of memory or the use of 
  120.         Overlays, you MUST implement some sort of patch to
  121.         work around Turbo Pascal 2.0B's known "bug" in its handling
  122.         of externals greater that 128-bytes in length.  The "bug"
  123.         is easy to work around, by either loading the external
  124.         at run-time(recommended) or converting it to an INLINE 
  125.         $Include file.
  126.  
  127.  
  128.         Limiting the Stack Segment for Turbo Pascal.
  129.         If the Stack Size is not limited, at compile-time, then
  130.         Turbo Pascal may want to consume all of the system's 
  131.         available RAM.  Of course, that precludes the use of child
  132.         processes because the BLOCK call will fail, due to a lack
  133.         of resources.   It is, therefore, recommended that you limit
  134.         Maximum Stack Size to just enough memory to allow the Pascal
  135.         program to operate, this will leave the balance of your 
  136.         system's resources available for child processes.  
  137.  
  138.  
  139.     Terminate But Stay Resident.
  140.         Programs which use "Terminate but stay resident" or 
  141.         "Keep process" to terminate but keep itself in memory should
  142.         not be run from Turborun.  The reason is that will cause
  143.         DOS to attempt to set the allocation block map to prevent
  144.         overlaying of the child.  If the system survives this, it
  145.         still leave DOS in an unpredictable state.  
  146.  
  147.  
  148.     Exit codes.
  149.         In order for the parent to correctly detect the reason the
  150.         child terminated the child must pass that infomation back,
  151.         so that the Get Return Code can find it.  May currently,
  152.         available programs do not provide this and hence the code
  153.         value passed back to RetCode may not reflect the actual
  154.         reason the child was terminated.  An example is COMMAND.COM.
  155.  
  156.     "BUGS".
  157.         If you encounter difficulties using this program or you find
  158.         "bugs" in it, please contact either author or the latest 
  159.         revisor.
  160.  
  161.  
  162.         After this source code is assembled, linked, and converted to 
  163.         a COM file it should be included as an external procedure
  164.         within the Turbo Pascal source file.  The external should be
  165.         of the form shown below.
  166.  
  167.         Procedure GoForIt(Var RetCode:Integer; Var Cmdline);
  168.                 External 'TurboRun.com';
  169.  
  170.         Cmdline can be either type-CHAR or type-STRING.  If the latter
  171.         is used you should pass CmdLine[1] to skip the length byte.
  172.  
  173.         Format of CmdLine:
  174.  path-name-to-child [<sp> First-FCB [<sp> Second-FCB [<sp> switchs and/or IO redirection]]] #0
  175.  
  176.         Notice that the Cmdline to followed by a byte of 0, this is binary 0
  177.         not ASCII 0. <-- VERY IMPORTANT! 
  178.  
  179.         The variable, RetCode, will contain the return code from the
  180.         child process.  RetCode is actually used as two, seperate,
  181.         byte-length variables.  The table below explains how to interpret
  182.         this variable in terms of its high and low bytes.
  183.  
  184.         High byte
  185.                 0 : Normal Termination, No error
  186.                 1 : Child was terminated via by CONTROL-C
  187.                 2 : Child was aborted because of a DOS Hard Error
  188.                 3 : Child used terminate-but-stay-resident
  189.             (System needs to be rebooted)
  190.                 4 : EXEC call failed.
  191.                 8 : Memory allocation error, EXEC was not attempted
  192.  
  193.         In the cases of 0,1,2, and 3 the low byte has the value passed
  194.         back by the child.  Any of these values indicate that the EXEC
  195.         call was successful.  Programs which use terminate-but-stay-
  196.         resident should not be called by the program.
  197.  
  198.         In the case of 4 the low byte has the reason the EXEC failed.
  199.           2 : The path specified was invalid or not found.
  200.           8 : There was not enough memory for the process to be created.
  201.          11 : The process was an EXE format file and contained information
  202.               that was internally inconsistent.
  203.  
  204.         In the case of 8 the low byte has the reason the memory allocation
  205.         request failed.
  206.           7 : The arena was trashed because a user program has changed
  207.               memory that does not belong to it. System needs to be Rebooted.
  208.           8 : Insufficient memory.
  209.  
  210. *       
  211. Page      
  212. Subttl Main Program
  213. ;
  214. ;------------------- E Q U A T E S --------------------
  215. ;
  216. spsave  equ     cs:0080h        ;Storage for SP register
  217. sssave  equ     cs:0082h        ;Storage for SS register
  218. ;
  219. ;-------------------- M A C R O S ---------------------
  220. ;
  221. MsDos   Macro   function
  222.         If function lt 256
  223.         mov     ah,function
  224.         Else
  225.         mov     ax,function
  226.         EndIf
  227.         int     21h
  228.         EndM
  229. ;
  230. ;
  231. ;------------------ M A I N  P R O G R A M -----------------
  232. ;
  233. code    segment para public 'CODE'
  234.         assume  cs:code,ds:nothing,es:code,ss:nothing
  235.  
  236.         org 00h  
  237. run     proc    near
  238.         call    entry           ;Find out where we were loaded
  239.                                 ;There is no return from this CALL
  240. separate db    ';,=+"[]><| ',9,0 ;Filename separators
  241.                 ;Excluding ":" and "\" which cannot appear in
  242.                 ;a filename but can appear in a pathname.
  243.          
  244. child   db      128 dup(?)      ;Path to child (with filename extension) 
  245. fcb1    db      16 dup(?)       ;Optional fcb
  246. fcb2    db      16 dup(?)       ;Optional fcb
  247. parablk dw      0,12 dup(?)     ;Parameter block (DOS 2.0 Manual p. D-45)
  248. cmdstrt dw      0               ;Offset to first char in child's CmdLine
  249. ssmax   dw      0               ;Storage for parent's maximum stack size, 
  250.                                 ;in paragraphs
  251.  
  252. entry:  
  253.         pop     bx              ;Offset+3
  254.         sub     bx,3            ;Actual offset address of RUN.COM
  255.         pop     dx              ;Turbo(tm) return address 
  256.         pop     si              ;Offset to command string
  257.         pop     cx              ;Segment address of command string
  258.         push    dx              ;Restore turbo's RET address
  259.         mov     dx,si           
  260.         push    ds
  261.         push    bp
  262.         pushf
  263.  
  264.         push    bx              ;Save bx temporarily
  265.         sub     bx,7            ;Point to parent's max stack size
  266.         mov     ax,cs:[bx]      ;Move it to ax
  267.         pop     bx              ;Recover bx
  268.         mov     [ssmax+bx],ax   ;Save the max stack size for SETBLOCK call
  269.  
  270.         mov     ds,cx           ;Point to command string segment
  271.         push    cs              ;Make ES=CS 
  272.         pop     es
  273.         mov     di,offset child ;Child process path name
  274.         add     di,bx           ;Relocation offset
  275.         cld                     ;Force direction
  276. cmdlp:  lodsb                   ;Get character
  277.     push    di        ;Save DI
  278.     mov    cx,13        ;look for any of 13 filename separators
  279.     mov    di,offset separate
  280.     add    di,bx        ;Relocation offset
  281.     repnz    scasb        ;Look for match
  282.     jz    endstr        ;Exit if a separator is found
  283.     pop    di        ;Otherwise, retrieve DI and continue
  284.         stosb                   ;Store byte
  285.         jmp     cmdlp         ;Try next character
  286. endstr: pop    di        ;Retrieve DI
  287.     mov     [cmdstrt+bx],si  ;Pointer to first char in child's CmdLine
  288.         mov     al,0            ;Put terminating 0 in child path
  289.         stosb
  290.         dec     si              ;Back up one for stosb, 
  291.         dec     si              ;Back up one for the length byte
  292.  
  293. ;ds:si point to the childs command line
  294.  
  295. u1:     mov     [parablk+2+bx],si ;Store offset address
  296.         inc     si              ;Start parsing at head of child's CmdLine
  297.         mov     cx,ds
  298.         mov     [parablk+4+bx],cx ;Store segment address
  299.         mov     di,offset fcb1  ;Setup to parse first filename
  300.         add     di,bx           ;Relocation
  301.  
  302. ;di has offset from cs to default FCB passed at 5Ch
  303.  
  304.         mov     [parablk+6+bx],di
  305.         MsDos   2901h           ;Ignore leading separators
  306.         mov     di,offset fcb2  ;Parse second filename
  307.         add     di,bx           ;Relocation
  308.  
  309. ;di has offset from cs to default FCB passed at 6Ch
  310.  
  311.         mov     [parablk+10+bx],di
  312.         MsDos   2901h
  313. ;
  314. ;Place <cr> at end of CmdLine, calculate and store child's CmdLine length
  315. ;
  316. u2:     cmp     byte ptr ds:[si],0 ;Look for end of line
  317.         jz      u3
  318.         inc     si
  319.         jmp     u2
  320. u3:     mov     byte ptr ds:[si],13
  321.  
  322.         mov     ax,[cmdstrt+bx];Get pointer to first char in child's CmdLine
  323.         xchg    ax,si           ;Swap it with current pointer
  324.         dec     si              ;We don't include the <cr> in our length
  325.         sub     ax,si           ;Calculate the length
  326.         dec     si              ;Point to the length byte
  327.         mov     byte ptr ds:[si],al ;Store calculated length there
  328.  
  329.         mov     cx,cs           ;Setup segments in parameter block
  330.         mov     [parablk+8+bx],cx
  331.         mov     [parablk+12+bx],cx
  332.         mov     [parablk+bx],0  ;Pass environment to child, unchanged
  333.  
  334. ;Now the childs parameter block is setup.
  335. ;All that remains is to allocate memory and go for it.
  336.  
  337.         push    bx              ;Save reloc register
  338.         mov     bx,ss
  339.         add     bx,offset ssmax ;Allow for Turbo's(tm) stack
  340.         sub     bx,cx           ;Remember CX=CS
  341.         MsDos   4ah             ;Modify memory allocation
  342.         jnc     $+5
  343.         jmp     alocer          ;Allocation error, Exit
  344.         pop     bx              ;Recover reloc register
  345.         push    bx              ;Save a copy on stack
  346.         mov     dx,bx
  347.         add     bx,offset parablk
  348.         mov     [spsave],sp     ;Save SP
  349.         mov     cx,ss
  350.         mov     [sssave],cx     ;Save SS
  351.         push    cs              ;Point ds:sx to child
  352.         pop     ds
  353.         add     dx,offset child
  354.         MsDos   4b00h           ;Go for it
  355.         cli                     ;Prevent interuption
  356.         mov     sp,[spsave]     ;Recover SP
  357.         mov     cx,[sssave]
  358.         mov     ss,cx           ;Recover SS
  359.         sti
  360.         jnc     $+5
  361.         jmp     runerr          ;Problems, Exit
  362.         pop     bx              ;Get reloc register
  363.         MsDos   4dh             ;Get return code from child
  364. getbac: popf                    ;Turbo's Flags
  365.         pop     bp              ;Turbo's BP
  366.         pop     ds              ;Turbo's DS
  367.         pop     cx              ;Return address
  368.         pop     di              ;Offset to RC Variable
  369.         pop     es              ;Segment of RC Variable
  370.         mov     es:[di],ax      ;Store return code
  371.         push    cx              ;Restore return address
  372.         ret                     ;Back to Turbo
  373. alocer: pop     bx              ;Reloc register
  374.         mov     ah,1000b        ;Indicate Allocation error
  375.         jmp     short getbac
  376. runerr: pop     bx
  377.         mov     ah,0100b        ;Indicate RUN error
  378.         jmp     short getbac
  379. run     endp
  380. code    ends
  381.         end     run
  382.