;Copyright 2000 - Mark McDonald All rights reserved
; DECLARE: FUNCTION ARGC() AS INTEGER
; ARGC()
;     Returns the number of space delimited words present in COMMAND$.
;     Example:   T = ARGC()

MCODE   Segment Byte
        Assume  CS: MCODE

        Public  argc

argc Proc       Far
        push    bp                      ; Save Power Basic
        mov     bp,sp                   ; registers
        push    ds                      ;

        mov     AH, 62h                 ; get PSP segment from DOS
        int     21h                     ; call DOS
        mov     DS, BX                  ; point DS to PSP segment
        mov     DI, 80h                 ; pointer to start of cmd line-1
        mov     CX, 0                   ; start count at zero
CntLoop:
        inc     DI                      ; move on to next char
        cmp     Byte Ptr [DI], 32       ; skip all spaces
        je      CntLoop                 ;
        mov     AL, [DI]                ; get char
        cmp     AL, 13                  ; is it carriage return?
        je      ArgcDone                ; yes, exit

        inc     CX                      ; count this argument

        cmp     AL, 34                  ; is it a (") quote?
        je      GotQuote                ; yes, find end of quote
        cmp     AL, 39                  ; is it a (') quote?
        je      GotQuote                ; yes, find end of quote
SkipWord:
        inc     DI                      ; move to next char
        cmp     Byte Ptr [DI], 32       ; is it a space?
        je      CntLoop                 ; yes, get next argument
        cmp     Byte Ptr [DI], 47       ; switch char (/) ?
        je      CntLoop                 ; yes, get next argument
        cmp     Byte Ptr [DI], 13       ; carriage return?
        je      ArgcDone                ; yes, we're done
        jmp     SkipWord                ; get next char
GotQuote:
        inc     DI                      ; move to next char
        cmp     AL, [DI]                ; is it the same kind of quote?
        je      CntLoop                 ; yes, next argument
        cmp     Byte Ptr [DI], 13       ; See if EOLN
        jne     GotQuote                ; no, search for end of quoted area
ArgcDone:
        mov     AX, CX                  ; put count in AX
        pop     DS                      ;
        pop     BP                      ;
        retf                            ;
argc EndP
MCODE   EndS
        End