]> git.sur5r.net Git - cc65/blob - libsrc/atari/getargs.s
add a comment describing why this file is really included despite an obvious referenc...
[cc65] / libsrc / atari / getargs.s
1 ; get arguments from command line (when DOS supports it)
2
3 ; Freddy Offenga, 4/21/2000
4
5 ; initmainargs is forcibly included by the C compiler if it encounters a
6 ; main() function with arguments. Therefore it isn't referenced by the
7 ; startup code but is nevertheless included in the compiled program when
8 ; needed.
9
10 MAXARGS = 16            ; max. amount of arguments in arg. table
11 CL_SIZE = 64            ; command line buffer size
12 SPACE   = 32            ; SPACE char.
13
14         .include        "atari.inc"
15         .import         __argc, __argv
16         .importzp       ptr1
17         .import         __dos_type
18         .constructor    initmainargs,25
19
20 ; --------------------------------------------------------------------------
21 ; Get command line
22
23 .segment        "INIT"
24
25 initmainargs:
26         lda     #0
27         sta     __argc
28         sta     __argc+1
29         sta     __argv
30         sta     __argv+1
31
32         lda     __dos_type      ; which DOS?
33         cmp     #ATARIDOS
34         beq     nargdos         ; DOS does not support arguments
35         cmp     #MYDOS
36         bne     argdos          ; DOS supports arguments
37 nargdos:rts
38
39 ; Initialize ourcl buffer
40
41 argdos: lda     #ATEOL
42         sta     ourcl+CL_SIZE
43
44 ; Move SpartaDOS command line to our own buffer
45
46         lda     DOSVEC
47         clc
48         adc     #<LBUF
49         sta     ptr1
50         lda     DOSVEC+1
51         adc     #>LBUF
52         sta     ptr1+1
53
54         ldy     #0
55 cpcl:   lda     (ptr1),y
56         sta     ourcl,y
57         iny
58         cmp     #ATEOL
59         beq     movdon
60         cpy     #CL_SIZE
61         bne     cpcl
62
63 movdon: lda     #0
64         sta     ourcl,y         ; null terminate behind ATEOL
65
66 ; Turn command line into argv table
67
68         ;ldy    #0
69         tay
70 eatspc: lda     ourcl,y         ; eat spaces
71         cmp     #ATEOL
72         beq     finargs
73         cmp     #SPACE
74         bne     rpar            ; begin of argument found
75         iny
76         cpy     #CL_SIZE
77         bne     eatspc
78         beq     finargs         ; only spaces is no argument
79
80 ; Store argument vector
81
82 rpar:   lda     __argc          ; low-byte
83         asl
84         tax                     ; table index
85         tya                     ; ourcl index
86         clc
87         adc     #<ourcl
88         sta     argv,x
89         lda     #>ourcl
90         adc     #0
91         sta     argv+1,x
92         ldx     __argc
93         inx
94         stx     __argc
95         cpx     #MAXARGS
96         beq     finargs
97
98 ; Skip this arg.
99
100 skiparg:
101         ldx     ourcl,y
102         cpx     #ATEOL          ; end of line?
103         beq     eopar
104         cpx     #SPACE
105         beq     eopar
106         iny
107         cpy     #CL_SIZE
108         bne     skiparg
109
110 ; End of arg. -> place 0
111
112 eopar:
113         lda     #0
114         sta     ourcl,y
115         iny                     ; y behind arg.
116         cpx     #ATEOL          ; was it the last arg?
117         bne     eatspc
118
119 ; Finish args
120
121 finargs:
122         lda     __argc
123         asl           
124         tax
125         lda     #0
126         sta     argv,x
127         sta     argv+1,x
128         lda     #<argv
129         ldx     #>argv
130         sta     __argv
131         stx     __argv+1
132         rts
133
134 ; --------------------------------------------------------------------------
135 ; Data
136
137 .bss
138
139 argv:   .res    (1 + MAXARGS) * 2
140
141 ; Buffer for command line / argv strings
142
143 ourcl:  .res    CL_SIZE+1