home *** CD-ROM | disk | FTP | other *** search
/ ftp.shrubbery.net / 2015-02-07.ftp.shrubbery.net.tar / ftp.shrubbery.net / pub / pc / unix / unx.arc / ARGS.ASM next >
Assembly Source File  |  1985-06-10  |  5KB  |  195 lines

  1.     page    66,132
  2.     page
  3.  
  4. ;---- args.asm ----------------------------------------------------------
  5. ; This file contains both an argument grabber and a simple heap.
  6. ;
  7. ; Args parses the command line into a unix-style parameter array.
  8. ; To use it, you should be a .COM program: that is,
  9. ; you must start your program as follows:
  10. ;    code    segment public 'CODE'
  11. ;    assume    CS:code,DS:code,ES:code
  12. ;    extrn    _args:near,argc:word,argv:word        ;argc & argv in CS:
  13. ;
  14. ; and end the file as follows:
  15. ;    code    ends
  16. ;    end
  17. ; Link to your program with LINK YOURFILE,args; then EXE2BIN YOURFILE.
  18. ; Your program should start with an ORG 100H statement, and should
  19. ; CALL _ARGS to have it parse the cmdline.
  20. ; Argc and Argv are just as in C; argc = # of params on cmd line,
  21. ; argv is an array of pointers to strings (null terminated, of course). 
  22. ; Because MS-DOS doesn't pass us the name used to invoke the program,
  23. ; argv[0] always points to a null string.
  24. ; _SHIFT updates ARGC.
  25. ;
  26. ; See the file \bin\asm\exist.asm for an example.
  27. ; Do not call NEW before calling _ARGS.
  28. ;
  29. ; Changes:
  30. ;   16 July 1984   DRK    Copy args out of default location; fixed argv[0].
  31. ;   1 Aug 1984     DRK    Added _SHIFT. 
  32. ;   16 Oct 1984    MCN    _SHIFT updates argc
  33. ;   21 Mar 1985    DRK    oh yeah?
  34. ;--------------------------------------------------------------------------
  35.  
  36.     PUBLIC    _ARGS,ARGC,ARGV,_SHIFT, new, dispose
  37.  
  38. ; Predeclared file handles
  39. stdin    equ    0
  40. stdout    equ    1
  41. stderr    equ    2
  42. stdsio    equ    3
  43. stdprn    equ    4
  44.  
  45. ; Maximum number of parameters
  46. maxparms    equ    32
  47.  
  48. ; Declarations for accessing command line
  49. nchar    equ    80h        ; address of number of chars in argument line
  50. params    equ    81h        ; address of argument line
  51.  
  52. ;---- code starts here --------------------------------------------------------
  53. code    segment    public    'CODE'
  54. assume cs:code,ds:code
  55.  
  56. _args    proc    near
  57.     call    near ptr newinit
  58.  
  59.     ; initialize
  60.     cld            ; clear decrement mode
  61.     mov    bx, 2        ; argc = 0 (will sub 2 from bx at end)
  62.     mov    argv[0], offset null    ; prog name unknown; set to null.
  63.     mov    si, params    ; i = 0
  64.     mov    di, nchar    ; fuck this assembler -- won't mov cl,[81h]
  65.     mov    cl, [di]    ; cx = # of chars in command line
  66.     mov    ch, 0
  67.     jcxz    done        ; no arg chars -> we're done.
  68.  
  69.     ; Allocate room for arguments; returns pointer in BX.
  70.     push    bx
  71.     push    cx
  72.     mov    cx, 128        ; worst case
  73.     call    new
  74.     mov    di, bx
  75.     pop    cx
  76.     pop    bx
  77.  
  78.     ; Move arguments out of default DTA.
  79.     push    cx
  80.     push    di
  81.     rep    movsb
  82.     pop    si
  83.     pop    cx
  84.  
  85.     ; Big loop- find arguments...
  86. parml:
  87.     ; Little loop #1: strip leading blanks from argument.
  88.     ; while (i<nchar && params[i] = ' ') do i++;
  89. stripl:    lodsb            ; al = [si++]
  90.     cmp    al, ' '        ; space?
  91.     loopz    stripl        ; if so, keep skipping.
  92.     jne    gotone        ; If we found a nonblank, skip zero test.
  93.     jcxz    done        ; found no unblank chars -> we're done.
  94.  
  95. gotone:    dec    si        ; bump SI back to start of nonblank. 
  96.     inc    cx
  97.     mov    argv[bx],si    ; save pointer to this string
  98.  
  99.     ; Little loop #2: skip nonblank chars.
  100.     ; while (i<nchar && params[i] <> ' ') do i++;
  101. skipl:    lodsb
  102.     cmp    al, ' '
  103.     loopnz    skipl
  104.     jz    oky
  105.         ; Last char of line was not blank.
  106.         inc    si    ; make next statement put null AFTER arg.
  107. oky:
  108.     mov    byte ptr [si][-1], 0    ; put null at end of arg
  109.     add    bx,2        ; argc++;
  110.  
  111.     jcxz    done        ; if we ran off end of cmdline, no more args.
  112.  
  113.     cmp    bx, maxparms*2
  114.     jb    parml        ; loop if argc < maxparms.
  115.  
  116. done:    ; All done finding parms; now share argc with caller.
  117.     sub    bx, 2
  118.     shr    bx, 1
  119.     mov    argc, bx
  120.     ret
  121.  
  122. _args    endp
  123.  
  124. ;---- _Shift: --------------------------------------------
  125. ; Shifts %2 to %1, %3 to %2, etc.  Leaves %0 alone.
  126. ; Works by shuffling argv[*].
  127. _shift    proc    near
  128.     cld
  129.     mov    si, offset argv[4]
  130.     mov    di, offset argv[2]
  131.     mov    cx, maxparms
  132.     rep    movsw
  133. ; wrongo!  Caller should do this...    sub argc, 1        ; there is now one less argument
  134.     ret
  135. _shift    endp
  136.  
  137.  
  138. ;----- Newinit- initialize heap
  139.  
  140. newinit    proc    near
  141.     mov    CS:firstfree, offset lastcode
  142.     ret
  143. newinit    endp
  144.  
  145. ; Given length in CX, returns pointer in BX.
  146. ; Does not affect CX.
  147. new    proc    near
  148.     push    cx
  149.     mov    bx, cs:firstfree
  150.     add    cx, cs:firstfree
  151.     mov    cs:firstfree, cx
  152.     cmp    sp, cx
  153.     pop    cx
  154.  
  155.     jbe    newerror
  156.     ret
  157.  
  158. stackmsg:    db    '?New: heap overflow', 7, 13, 10
  159. newerror:
  160. sml    equ    newerror - stackmsg
  161.     push    cs
  162.     pop    ds
  163.     mov    dx, offset stackmsg
  164.     mov    cx, sml
  165.     mov    bx, stderr
  166.     mov    ah, 40h
  167.     int    21h
  168.     mov    ax, 4c02h        ; terminate- error code 2
  169.     int    21h
  170.  
  171. new    endp
  172.  
  173. ;---- Call Dispose with ptr in BX, length in CX.
  174. dispose    proc    near
  175.     ret
  176. dispose    endp
  177.  
  178. ;------ Heap variables ------
  179. firstfree    dw    ?
  180.  
  181. ;---- parameter count, array ---------------------------------------------------
  182. ; Pointers to up to maxparms parameter strings are held here.
  183.  
  184. null    dw    0        ; the null string
  185. argc    dw    ?
  186. argv    dw    maxparms dup (?)
  187.  
  188. lastcode    label    near
  189.  
  190. code    ends
  191.  
  192.     end
  193.  
  194.  
  195.