home *** CD-ROM | disk | FTP | other *** search
/ ftp.barnyard.co.uk / 2015.02.ftp.barnyard.co.uk.tar / ftp.barnyard.co.uk / cpm / walnut-creek-CDROM / ZSYS / SIMTEL20 / SYSLIB / SLIB1.LBR / SARGV.Z80 < prev    next >
Text File  |  2000-06-30  |  3KB  |  118 lines

  1. ;
  2. ; SYSLIB Module Name: SARGV
  3. ; Author: Richard Conn
  4. ; SYSLIB Version Number: 3.6
  5. ; Module Version Number: 1.1
  6.  
  7.     public    argv
  8.  
  9. ;
  10. ;    ARGV is a UNIX-style ARGC/ARGV string parser.  It is passed
  11. ; a null-terminated string in HL and the address of a token pointer
  12. ; table in DE as follows:
  13. ;
  14. ;        LXI    H,STRING
  15. ;        LXI    D,ARGV$TABLE
  16. ;        MVI    A,0        ; do not mark token end
  17. ;        CALL    ARGV
  18. ;        JNZ    TOKEN$OVFL    ; indicates more tokens than allowed
  19. ;        ...
  20. ;    ARGV$TABLE:
  21. ;        DB    MAX$ENT    ; max number of entries permitted
  22. ;        DS    1    ; number of entries stored by ARGV
  23. ;        DS    2    ; pointer to token 1
  24. ;        DS    2    ; pointer to token 2
  25. ;        ...
  26. ;        DS    2    ; pointer to token MAX$ENT
  27. ;
  28. ;    Tokens are delimited by spaces and tabs.
  29. ;    On input, if A=0, the end of each token is not marked with a null.
  30. ; If A<>0, a null is placed after the last byte of each token.
  31. ;    If all went well, return with A=0 and Zero Flag set.  If there
  32. ; are possibly more tokens than pointers, return with A=0FFH and NZ.
  33. ;
  34. argv:
  35.     push    bc    ; save regs
  36.     push    de
  37.     push    hl
  38.     ld    c,a    ; save mark flag
  39.     ex    de,hl
  40.     ld    b,(hl)    ; get max entry count
  41.     push    hl    ; save address of max entry count
  42.     inc    hl    ; pt to token count
  43.     inc    hl    ; pt to first pointer
  44. ;
  45. ; On each loop, DE = address of next char in string and HL = address of
  46. ; next pointer buffer; B = number of pointer buffers remaining and C =
  47. ; mark flag (0 = no mark)
  48. ;
  49. loop:
  50.     call    sksp    ; skip spaces and tabs in string pted to by DE
  51.     or    a    ; end of string?
  52.     jp    z,done
  53.     ld    (hl),e    ; store low
  54.     inc    hl
  55.     ld    (hl),d    ; store high
  56.     inc    hl
  57.     dec    b    ; count down
  58.     jp    z,loop2
  59.     call    sknsp    ; skip until end of token
  60.     or    a    ; done?
  61.     jp    z,done
  62.     ld    a,c    ; get mark flag
  63.     or    a    ; 0=no mark
  64.     jp    z,loop1
  65.     xor    a    ; mark with null
  66.     ld    (de),a    ; store null
  67.     inc    de    ; pt to next char
  68. loop1:
  69.     ld    a,b    ; check count
  70.     or    a
  71.     jp    nz,loop    ; continue on
  72. loop2:
  73.     call    sknsp    ; skip over token
  74.     call    sksp    ; any tokens left?
  75.     or    a
  76.     jp    z,done    ; none if EOL
  77.     or    0ffh    ; make A = 0FFH to indicate more to come
  78. done:
  79.     pop    hl    ; get address of max token count
  80.     push    af    ; save return flags
  81.     ld    a,(hl)    ; get max token count
  82.     sub    b    ; subtract counter
  83.     inc    hl    ; pt to return count
  84.     ld    (hl),a    ; set return count
  85.     pop    af    ; get return flag
  86.     pop    hl    ; restore regs
  87.     pop    de
  88.     pop    bc
  89.     ret
  90. ;
  91. ;  Skip over space or tab characters
  92. ;
  93. sksp:
  94.     ld    a,(de)    ; get char
  95.     and    7fh    ; mask
  96.     inc    de    ; pt to next
  97.     cp    ' '    ; continue if space
  98.     jp    z,sksp
  99.     cp    9    ; continue if tab
  100.     jp    z,sksp
  101.     dec    de    ; pt to character
  102.     ret
  103. ;
  104. ;  Skip over non-space and non-tab characters
  105. ;
  106. sknsp:
  107.     ld    a,(de)    ; get char
  108.     and    7fh    ; mask
  109.     ret    z        ; done if null
  110.     cp    ' '
  111.     ret    z        ; done if space
  112.     cp    9
  113.     ret    z        ; done if tab
  114.     inc    de    ; pt to next
  115.     jp    sknsp
  116.  
  117.     end
  118.