1; Command line parsing
2
3; Push pointers to argv[n] onto the stack now
4; We must start from the end
5; Entry:  hl = end of arguments
6;	   c = length of arguments
7;	   b = 0
8; Exit:	  bc = argc
9;         hl = argv
10
11    ld      de,0	;NULL pointer at end of array, just in case
12    push    de
13; Try to find the end of the arguments
14argv_loop_1:
15    ld      a,(hl)          ;Strip off trailing spaces
16    cp      ' '
17    jr      nz,argv_loop_2
18    ld      (hl),0
19    dec     hl
20    dec     c
21    jr      nz,argv_loop_1
22; We've located the end of the last argument, try to find the start
23argv_loop_2:
24    ld      a,(hl)
25    cp      ' '
26    jr      nz,argv_loop_3
27    inc     hl              ; We're now on the first character of the argument
28    inc     c
29IF CRT_ENABLE_STDIO
30  IF !DEFINED_noredir
31    IF !DEFINED_nostreams
32        EXTERN freopen
33        xor     a
34        add     b
35        jr      nz,no_redir_stdout
36        ld      a,(hl)
37        cp      '>'
38        jr      nz,no_redir_stdout
39        push    hl
40        inc     hl
41        cp      (hl)
42        dec     hl
43        ld      de,redir_fopen_flag	; "a" or "w"
44        jr      nz,noappendb
45        ld      a,'a'
46        ld      (de),a
47        inc     hl
48noappendb:
49        inc     hl
50
51        push    bc
52        push    hl					; file name ptr
53        push    de
54        ld      de,__sgoioblk+10		; file struct for stdout
55        push    de
56        call    freopen
57        pop     de
58        pop     de
59        pop     hl
60        pop     bc
61        pop     hl
62        dec     hl
63        jr      argv_zloop
64no_redir_stdout:
65        ld      a,(hl)
66        cp      '<'
67        jr      nz,no_redir_stdin
68        push    hl
69        inc     hl
70        ld      de,redir_fopen_flagr
71        push    bc
72        push    hl					; file name ptr
73        push    de
74        ld      de,__sgoioblk		; file struct for stdin
75        push    de
76        call    freopen
77        pop     de
78        pop     de
79        pop     hl
80        pop     bc
81        pop     hl
82        dec	    hl
83        jr      argv_zloop
84no_redir_stdin:
85    ENDIF
86  ENDIF
87ENDIF
88    push    hl
89    inc     b
90empty_arg:
91    dec     hl
92    dec     c
93; skip extra blanks
94argv_zloop:
95    ld      (hl),0      ;Terminate the previous argument
96    dec     hl          ;Last character of previous argument
97    dec     c
98    jr      z,argv_done
99    ld      a,(hl)
100    cp      ' '
101    jr      z,argv_zloop ;Skip over multiple spaces
102    jr      argv_loop_2  ;And do the next argument
103
104argv_loop_3:
105    dec     hl
106    dec     c
107    jr      nz,argv_loop_2
108
109argv_done:
110    ; We may still have an argument left (if it was at the start of the buffer)
111
112argv_push_final_arg:
113    ld      a,(hl)              ;Strip leading spaces
114    cp      ' '
115    jr      nz,argv_push_final_arg2
116    inc     hl
117    jr      argv_push_final_arg
118argv_push_final_arg2:
119    pop     de                  ;Is it the same as the last argument we pushed?
120    push    de
121    ld      a,h
122    sub     d
123    jr      nz,argv_push_final_arg3
124    ld      a,l
125    sub     e
126    jr      z,argv_done_2
127argv_push_final_arg3:
128    ld      a,(hl)
129    and     a
130    jr      z,argv_done_2
131    push    hl
132    inc     b
133
134argv_done_2:
135    ld      hl,end	;name of program (NULL)
136    push    hl
137    inc     b
138    ld      hl,0
139    add     hl,sp	;address of argv
140    ld      c,b
141    ld      b,0
142
143