home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / PPOS2.ZIP / ARGV.ASM < prev    next >
Assembly Source File  |  1988-09-13  |  5KB  |  135 lines

  1.         title   ARGV --- return argument pointer
  2.         page    55,132
  3.         .286
  4.  
  5. ;
  6. ; ARGV.ASM:     Return address and length of specified 
  7. ;               command line argument or fully qualified 
  8. ;               program name.  Treats blanks and tabs 
  9. ;               as whitespace.
  10. ;
  11. ; OS/2 version Copyright (C) 1987 Ray Duncan
  12. ; Call with:    AX    = argument number (0 based)
  13. ;
  14. ; Returns:      ES:BX = argument address
  15. ;               AX    = argument length (0=no argument)
  16. ;
  17. ; Uses:         nothing (other registers preserved)
  18. ;
  19. ; Note: if called with AX=0 (argv[0]), returns ES:BX 
  20. ; pointing to fully qualified program name in environment 
  21. ; block and AX=length.
  22. ;
  23.  
  24. tab     equ     09h             ; ASCII tab
  25. blank   equ     20h             ; ASCII space character
  26.  
  27.         extrn   DosGetEnv:far
  28.  
  29. _TEXT   segment word public 'CODE'
  30.  
  31.         assume  cs:_TEXT
  32.  
  33.         public  argv            ; make ARGV available to Linker
  34.  
  35.                                 ; local variables...
  36. envseg  equ     [bp-2]          ; environment segment
  37. cmdoffs equ     [bp-4]          ; command line offset   
  38.  
  39. argv    proc    near
  40.  
  41.         enter   4,0             ; make room for local variables
  42.         push    cx              ; save original CX and DI 
  43.         push    di
  44.  
  45.         push    ax              ; save argument number
  46.  
  47.         push    ss              ; get selector for environment 
  48.         lea     ax,envseg       ; and offset of command line 
  49.         push    ax
  50.         push    ss
  51.         lea     ax,cmdoffs
  52.         push    ax
  53.         call    DosGetEnv       ; transfer to OS/2      
  54.         or      ax,ax           ; test operation status
  55.         pop     ax              ; restore argument number
  56.         jnz     argv7           ; jump if DosGetEnv failed
  57.  
  58.         mov     es,envseg       ; set ES:BX = command line
  59.         mov     bx,cmdoffs
  60.  
  61.         or      ax,ax           ; is requested argument=0?
  62.         jz      argv8           ; yes, jump to get program name
  63.  
  64. argv0:  inc     bx              ; scan off first field
  65.         cmp     byte ptr es:[bx],0      
  66.         jne     argv0
  67.  
  68.         xor     ah,ah           ; initialize argument counter
  69.  
  70. argv1:  mov     cx,-1           ; set flag = outside argument
  71.  
  72. argv2:  inc     bx              ; point to next character 
  73.         cmp     byte ptr es:[bx],0
  74.         je      argv7           ; exit if null byte
  75.         cmp     byte ptr es:[bx],blank
  76.         je      argv1           ; outside argument if ASCII blank
  77.         cmp     byte ptr es:[bx],tab    
  78.         je      argv1           ; outside argument if ASCII tab
  79.  
  80.                                 ; if not blank or tab...
  81.         jcxz    argv2           ; jump if already inside argument
  82.  
  83.         inc     ah              ; else count arguments found
  84.         cmp     ah,al           ; is this the one we're looking for?
  85.         je      argv4           ; yes, go find its length
  86.         not     cx              ; no, set flag = inside argument
  87.         jmp     argv2           ; and look at next character
  88.  
  89. argv4:                          ; found desired argument, now
  90.                                 ; determine its length...
  91.         mov     ax,bx           ; save param. starting address 
  92.  
  93. argv5:  inc     bx              ; point to next character
  94.         cmp     byte ptr es:[bx],0
  95.         je      argv6           ; found end if null byte
  96.         cmp     byte ptr es:[bx],blank
  97.         je      argv6           ; found end if ASCII blank
  98.         cmp     byte ptr es:[bx],tab    
  99.         jne     argv5           ; found end if ASCII tab
  100.  
  101. argv6:  xchg    bx,ax           ; set ES:BX = argument address
  102.         sub     ax,bx           ; and AX = argument length
  103.         jmp     argvx           ; return to caller
  104.  
  105. argv7:  xor     ax,ax           ; set AX = 0, argument not found
  106.         jmp     argvx           ; return to caller
  107.  
  108. argv8:                          ; special handling for argv=0
  109.         xor     di,di           ; find the program name by
  110.         xor     al,al           ; first skipping over all the
  111.         mov     cx,-1           ; environment variables...
  112.         cld
  113. argv9:  repne scasb             ; scan for double null (can't use
  114.         scasb                   ; (SCASW since might be odd addr.)
  115.         jne     argv9           ; loop if it was a single null
  116.         mov     bx,di           ; save program name address
  117.         mov     cx,-1           ; now find its length... 
  118.         repne scasb             ; scan for another null byte
  119.         not     cx              ; convert CX to length 
  120.         dec     cx
  121.         mov     ax,cx           ; return length in AX
  122.  
  123. argvx:                          ; common exit point
  124.         pop     di              ; restore original CX and DI
  125.         pop     cx
  126.         leave                   ; discard stack frame
  127.         ret                     ; return to caller
  128.  
  129. argv    endp
  130.  
  131. _TEXT   ends
  132.  
  133.         end     
  134.