home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast.iso / envutil / envrpt.asm < prev    next >
Assembly Source File  |  1994-03-04  |  7KB  |  283 lines

  1. ;
  2. ; Program to examine the environment and give return codes for BATCH inquiry
  3. ;
  4. ; Gerald Lotto    01/24/85
  5. ;
  6. ; Usage:
  7. ;       ENVRPT <var> [<val>]
  8. ; <var> is an environment variable with value
  9. ; <val> (optional)
  10. ;
  11. ; Valid return codes are:
  12. ;
  13. ;    0 - <var> is found to equal <val>
  14. ;    1 - <var> is found to not equal <val>
  15. ;    3 - <var> is not found
  16. ;    4 - Illegal arguments were given, i.e. ENVRPT (cr)
  17. ;
  18. ;    Revised:
  19. ;        DATE    INIT    REASON
  20. ;
  21. ;
  22.     Title    ENVRPT
  23.  
  24. CSEG    SEGMENT    PARA 'CODE'
  25.  
  26. NULL    EQU    00H
  27. SPACE    EQU    20H
  28. CR    EQU    0DH
  29. LF    EQU    0AH
  30. ENVSIZE    EQU    800H                ; This is maximum size
  31.                         ; for my (modified) DOS
  32.     ORG    2CH
  33. ENVSEG    LABEL    WORD                ; Segment of environment
  34.  
  35.     ORG    80H
  36. DOSCMD    LABEL    BYTE                ; Command line from DOS
  37.  
  38.     ORG    100H
  39. ENVRPT    PROC    FAR
  40.     ASSUME    CS:CSEG,DS:NOTHING,ES:NOTHING
  41.  
  42.     JMP    START
  43.  
  44. VALUE    DB    80H DUP(0)            ; Internal work area for
  45.                         ; storing translation string
  46. START:    MOV    AX,CS
  47.     MOV    DS,AX                ; Ensure that DS and ES are
  48.     MOV    ES,AX                ; set up
  49.     MOV    SI,OFFSET DOSCMD + 1        ; Start of argument string
  50.     MOV    CX,0000
  51.     MOV    CL,BYTE PTR DOSCMD        ; Length of argument string
  52.      CALL    GETWORD                ; Get first argument
  53.  
  54.     CMP    BYTE PTR [DI],NULL        ; Is it NULL?
  55.     JZ    ERROR                ; Yes, exit with code 4
  56.  
  57.     CALL    UPCASE                ; Upper case it in place
  58.  
  59.     CALL    XLATE                ; Get translation string
  60.                         ; from environment in VALUE
  61.     CMP    BYTE PTR CS:VALUE,NULL        ; Is the translation NULL?
  62.     JZ    NOTFOUND            ; Yes, exit with code 3
  63.  
  64.     MOV    DI,OFFSET DS:VALUE        ; Locate translation string
  65.     MOV    AX,0200H            ; DOS Print character
  66.  
  67. PRINT:    MOV    DL,BYTE PTR [DI]        ; Get next char to print
  68.     INT    21H                ; Print it,
  69.     INC    DI                ; and move up one
  70.     CMP    BYTE PTR [DI],NULL        ; Is it the end?
  71.     JNZ    PRINT                ; No, go another round
  72.  
  73.     MOV    DL,CR                ; Yes, follow by a carriage
  74.     INT    21H                ; return
  75.  
  76.     MOV    DL,LF                ; and a line feed
  77.     INT    21H
  78.  
  79.     ADD    SI,CX                ; Position at the end of arg2
  80.     MOV    BYTE PTR [SI],NULL        ; and put a NULL there
  81.     INC    CX                ; Increase to count all chars
  82.     STD                    ; Reverse direction
  83.     REPZ    CMPS BYTE PTR DS:[SI],ES:[DI]    ; and compare the strings
  84.  
  85.     CLD                    ; Go forward again
  86.     JNZ    FALSEEXIT            ; If the comparison failed,
  87.                         ; exit with code 1
  88. TRUEEXIT:
  89.     MOV    AX,4C00H            ; Otherwise exit with code 0
  90.     INT    21H
  91.  
  92. FALSEEXIT:
  93.     MOV    AX,4C01H            ; This is the exit for a
  94.     INT    21H                ; non-match
  95.  
  96. NOTFOUND:
  97.     MOV    AX,4C03H            ; This is the exit for not
  98.     INT    21H                ; finding the requested
  99.                         ; ENVIRONMENT variable
  100. ERROR:
  101.     MOV    AX,4C04H            ; This is the exit for bad
  102.     INT    21H                ; arguments
  103.  
  104. ENVRPT    ENDP
  105.  
  106. ;    Procedure GETWORD
  107. ;
  108. ;    Call with:
  109. ;    SI - Pointer in an ASCII string (Possible leading spaces)
  110. ;    CX - Length to end of string
  111. ;
  112. ;    Returns with:
  113. ;    DI - Pointer to first word now terminated with a null
  114. ;    SI, CX - Now point to next word and have remaining string length
  115. ;
  116. ;    Does not look back for data before SI
  117. ;    AX, BX, CX zapped, all other regs preserved
  118. ;
  119. ;    Thanks to Dan Tappan, this code was modified from OLDER.ASM
  120. ;
  121. GETWORD    PROC
  122.     CLD
  123.     PUSH    SI
  124.     PUSH    CX
  125.     MOV    BX,CX
  126.     MOV    AL,SPACE
  127.     MOV    BYTE PTR [BX+SI],AL        ; Put a space at the end
  128.     MOV    DI,SI
  129.     CMP    CX,+00                ; Check length
  130.     JLE    GOTWORD                ; Return if NULL
  131.  
  132.     REPZ    SCASB                ; Skip leading spaces
  133.  
  134.     CMP    CX,+00                ; See if you ran out of string
  135.     JG    READWORD            ; If not, go to it
  136.  
  137.     MOV    DI,SI                ; If so, back to the beginning
  138.     JMP    GOTWORD                ; and return
  139.  
  140. READWORD:    
  141.     DEC    DI                ; Back up to the first char
  142.     NOT    CX                ; Count back from -1
  143.     PUSH    DI                ; Save the start
  144.     REPNZ    SCASB                ; Find the next space
  145.  
  146.     DEC    DI                ; Back up to the end
  147.     MOV    SI,DI                ; Save it,
  148.     POP    DI                ; and restore the pointer
  149.                         ; to the start
  150. GOTWORD:
  151.     MOV    BYTE PTR [SI],00        ; Terminate word with a null
  152.     INC    SI                ; Advance to next word
  153.     POP    CX                ; Original length
  154.     POP    BX                ; The original location
  155.     SUB    BX,SI                ; - the current location
  156.     ADD    CX,BX                ; + the overall length
  157.     RET                    ; = the remaining length
  158.  
  159. GETWORD    ENDP
  160.  
  161. ;    Procedure UPCASE
  162. ;
  163. ;    Call with:
  164. ;    DI - Pointer in an ASCII string terminated by a space (or less)
  165. ;
  166. ;    Returns with:
  167. ;    String UPPERCASED in place
  168. ;
  169. ;    AX zapped, all other regs preserved
  170. ;
  171. UPCASE    PROC
  172.     CLD    
  173.     PUSH    SI
  174.     MOV    SI,DI
  175.  
  176. NEXTBYTE:
  177.     LODSB    
  178.     AND    AL,07FH                ; Strip hi bit
  179.     CMP    AL,SPACE            ; Is this the end?
  180.     JLE    UPCASED                ; Yes, we are done.
  181.  
  182.     CMP    AL,'a'                ; Skip if it is less
  183.     JL    NEXTBYTE            ; then 'a'
  184.  
  185.     CMP    AL,'z'                ; or if it is greater
  186.     JG    NEXTBYTE            ; then 'z'
  187.  
  188.     AND    AL,0DFH                ; Else, UPPERCASE it
  189.     MOV    BYTE PTR [SI-01],AL        ; Replace it
  190.     JMP    NEXTBYTE            ; and grab the next one
  191.  
  192. UPCASED:
  193.     POP    SI
  194.     RET    
  195.  
  196. UPCASE    ENDP
  197.  
  198. ;    Procedure XLATE
  199. ;
  200. ;    Call with:
  201. ;    DI - Pointer to an UPPERCASED string, null terminated
  202. ;
  203. ;    Returns with:
  204. ;    Translation of string in an internal data area, terminated
  205. ;    with a 00, '$'
  206. ;
  207. ;    AX zapped, all other regs preserved
  208. ;
  209. XLATE    PROC
  210.     CLD    
  211.     PUSH    DS
  212.     PUSH    SI
  213.     PUSH    DI
  214.     PUSH    CX
  215.     MOV    SI,DI
  216.     MOV    AX,WORD PTR ENVSEG        ; From the PSP, DOS gives
  217.     MOV    ES,AX                ; the segment and offset of
  218.     MOV    DI,00                ; a copy of the environment
  219.     MOV    CX,WORD PTR ENVSIZE        ; Max size, see params up top
  220.     LODS    BYTE PTR DS:[SI]        ; Get the first char of arg
  221.  
  222. TESTMATCH:
  223.     PUSH    AX                ; and save it
  224.     CMP    AL,BYTE PTR ES:[DI]        ; Check against ENV string #1
  225.     JZ    GOTMATCH            ; Possible match
  226.  
  227. FAILED:
  228.     MOV    AX,0000                ; No match, search ENVIRONMENT
  229.     REPNZ    SCAS BYTE PTR ES:[DI]        ; for next NULL
  230.  
  231.     POP    AX                ; Restore first char of arg
  232.     CMP    CX,00                ; Have we run out of space?
  233.     JZ    TERMINATE            ; Yes, stop looking
  234.  
  235.     CMP    BYTE PTR ES:[DI],NULL        ; Are we at environment end?
  236.     JNZ    TESTMATCH            ; No, test next entry
  237.  
  238.     JMP    TERMINATE            ; Yes, stop looking
  239.  
  240. GOTMATCH:
  241.     INC    DI                ; Step to next char in ENV
  242.     REPZ    CMPS BYTE PTR DS:[SI],ES:[DI]    ; Compare strings
  243.  
  244.     DEC    SI                ; Back up to last character
  245.     DEC    DI                ; in arg and ENV strings
  246.     CMP    BYTE PTR DS:[SI],NULL        ; Are we at the end of arg?
  247.     JNZ    FAILED                ; No, try next ENV string
  248. ;
  249. ; Insert code to handle wildcards here
  250. ;
  251.     CMP    BYTE PTR ES:[DI],'='        ; End of the ENV string?
  252.     JNZ    FAILED                ; No, try next ENV string
  253.  
  254.     POP    AX                ; We have a match, trash AX
  255.     INC    DI
  256.     PUSH    DI                ; Save start of translation
  257.     MOV    CX,0FFFFH            ; Count backwards from -1
  258.     MOV    AX,0000
  259.     REPNZ    SCAS BYTE PTR ES:[DI]        ; up to the next NULL
  260.  
  261.     NOT    CX
  262.     DEC    CX                ; Adjust count to + number
  263.     POP    SI                ; Start of translation string
  264.     MOV    AX,ES                ; Swap seg regs around for
  265.     MOV    DS,AX                ; MOVS with DS = ENVSEG
  266.     MOV    AX,CS                ; and ES = CS
  267.     MOV    ES,AX
  268.     MOV    DI,OFFSET CS:VALUE        ; Point to the internal data
  269.     REPZ    MOVS BYTE PTR ES:[DI],DS:[SI]    ; area and copy translation
  270.  
  271. TERMINATE:
  272.     MOV    BYTE PTR ES:[DI],NULL        ; Terminate with a NULL
  273.     POP    CX
  274.     POP    DI
  275.     POP    SI
  276.     POP    DS
  277.     RET    
  278.  
  279. XLATE    ENDP
  280.  
  281. CSEG    ENDS
  282.     END    ENVRPT
  283.