; TITLE "SARGV - Syslib 4.0" NAME ('ARGV') ;================================================================= ; The Libraries, Version 4, (C) 1989 by Alpha Systems Corp. ;----------------------------------------------------------------- ; Author : Harold F. Bower ; Derived from SARGV.Z80 Ver 1.1 by Richard Conn ; Date : 17 Sep 89 ; Version : 1.2 ; Module : SARGV ; Abstract: This module contains the routine ARGV which is a ; Unix-style ARGC/ARGV string parser. A null-terminated ; string is passed to it along with the address of a token ; pointer table as: ; ; LXI H,STRING ; LXI D,ARGV$TABLE ; MVI A,0 ; do not mark token end ; CALL ARGV ; JNZ TOKEN$OVFL ; indicates more tokens than allowed ; ... ; ARGV$TABLE: ; DB MAX$ENT ; max number of entries permitted ; DS 1 ; number of entries stored by ARGV ; DS 2 ; pointer to token 1 ; DS 2 ; pointer to token 2 ; ... ; DS 2 ; pointer to token MAX$ENT ; Tokens are delimited by spaces and tabs. ; Revision: ;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ; Module Entry Points PUBLIC ARGV .Z80 CSEG ;=============================================================== ; NAME - ARGV ; Entry: HL - Null-terminated string starting address ; DE - Starting address for token pointer table ; A - A = 0, end of each token NOT marked with Null ; A <> 0, End of each token marked with Null ; Exit : AF - A = 0 and Zero Flag Set (Z) if Ok ; A = 0FFH and Zero Reset (NZ) if more tokens than ptrs ; Uses : AF ; Special Requirements: None ;=============================================================== ARGV: PUSH BC ; save regs PUSH DE PUSH HL LD C,A ; save mark flag EX DE,HL LD B,(HL) ; get max entry count PUSH HL ; save address of max entry count INC HL ; pt to token count INC HL ; pt to first pointer ; On each loop, DE = address of next char in string and HL = address of ; next pointer buffer; B = number of pointer buffers remaining and C = ; mark flag (0 = no mark) LOOP: CALL SKSP ; skip spaces and tabs in string pted to by DE OR A ; end of string? JR Z,DONE LD (HL),E ; store low INC HL LD (HL),D ; store high INC HL DEC B ; count down JR Z,LOOP2 CALL SKNSP ; skip until end of token OR A ; done? JR Z,DONE LD A,C ; get mark flag OR A ; 0=no mark JR Z,LOOP1 XOR A ; mark with null LD (DE),A ; store null INC DE ; pt to next char LOOP1: LD A,B ; check count OR A JR NZ,LOOP ; continue on LOOP2: CALL SKNSP ; skip over token CALL SKSP ; any tokens left? OR A JR Z,DONE ; none if EOL OR 0FFH ; make A = 0FFH to indicate more to come DONE: POP HL ; get address of max token count PUSH AF ; save return flags LD A,(HL) ; get max token count SUB B ; subtract counter INC HL ; pt to return count LD (HL),A ; set return count POP AF ; get return flag POP HL ; restore regs POP DE POP BC RET ; Skip over space or tab characters SKSP: LD A,(DE) ; get char AND 7FH ; mask INC DE ; pt to next CP ' ' ; continue if space JR Z,SKSP CP 9 ; continue if tab JR Z,SKSP DEC DE ; pt to character RET ; Skip over non-space and non-tab characters SKNSP: LD A,(DE) ; get char AND 7FH ; mask RET Z ; ..exit done if Null CP ' ' ; Space? RET Z ; ..exit if so CP 9 ; Tab? RET Z ; ..exit if so INC DE ; Else pt to next JR SKNSP ; ..and continue END