]> git.sur5r.net Git - cc65/blob - libsrc/atari/getargs.s
Merge pull request #287 from silverdr/target_goodies
[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 ; XDOS support added 05/2016 by Christian Groessler
11
12 MAXARGS = 16            ; max. amount of arguments in arg. table
13 CL_SIZE = 64            ; command line buffer size
14 SPACE   = 32            ; SPACE char.
15
16         .include        "atari.inc"
17         .import         __argc, __argv
18         .importzp       ptr1
19         .import         __dos_type
20         .constructor    initmainargs, 25
21
22 ; --------------------------------------------------------------------------
23 ; Get command line
24
25 .segment        "ONCE"
26
27 nargdos:rts
28
29 initmainargs:
30         lda     __dos_type      ; which DOS?
31         cmp     #MAX_DOS_WITH_CMDLINE + 1
32         bcs     nargdos
33
34 ; Initialize ourcl buffer
35
36 argdos: ldy     #ATEOL
37         sty     ourcl+CL_SIZE
38
39 ; Move SpartaDOS/XDOS command line to our own buffer
40
41         cmp     #XDOS
42         bne     sparta
43
44         lda     #<XLINE
45         sta     ptr1
46         lda     #>XLINE
47         sta     ptr1+1
48         bne     cpcl0
49
50 sparta: lda     DOSVEC
51         clc
52         adc     #<LBUF
53         sta     ptr1
54         lda     DOSVEC+1
55         adc     #>LBUF
56         sta     ptr1+1
57
58 cpcl0:  ldy     #0
59 cpcl:   lda     (ptr1),y
60         sta     ourcl,y
61         iny
62         cmp     #ATEOL
63         beq     movdon
64         cpy     #CL_SIZE
65         bne     cpcl
66
67 movdon: lda     #0
68         sta     ourcl,y         ; null terminate behind ATEOL
69
70 ; Turn command line into argv table
71
72         ;ldy    #0
73         tay
74 eatspc: lda     ourcl,y         ; eat spaces
75         cmp     #ATEOL
76         beq     finargs
77         cmp     #SPACE
78         bne     rpar            ; begin of argument found
79         iny
80         cpy     #CL_SIZE
81         bne     eatspc
82         beq     finargs         ; only spaces is no argument
83
84 ; Store argument vector
85
86 rpar:   lda     __argc          ; low-byte
87         asl
88         tax                     ; table index
89         tya                     ; ourcl index
90         clc
91         adc     #<ourcl
92         sta     argv,x
93         lda     #>ourcl
94         adc     #0
95         sta     argv+1,x
96         ldx     __argc
97         inx
98         stx     __argc
99         cpx     #MAXARGS
100         beq     finargs
101
102 ; Skip this arg.
103
104 skiparg:
105         ldx     ourcl,y
106         cpx     #ATEOL          ; end of line?
107         beq     eopar
108         cpx     #SPACE
109         beq     eopar
110         iny
111         cpy     #CL_SIZE
112         bne     skiparg
113
114 ; End of arg. -> place 0
115
116 eopar:
117         lda     #0
118         sta     ourcl,y
119         iny                     ; y behind arg.
120         cpx     #ATEOL          ; was it the last arg?
121         bne     eatspc
122
123 ; Finish args
124
125 finargs:
126         lda     __argc
127         asl
128         tax
129         lda     #0
130         sta     argv,x
131         sta     argv+1,x
132         lda     #<argv
133         ldx     #>argv
134         sta     __argv
135         stx     __argv+1
136         rts
137
138 ; --------------------------------------------------------------------------
139 ; Data
140
141 .segment        "INIT"
142
143 argv:   .res    (1 + MAXARGS) * 2
144
145 ; Buffer for command line / argv strings
146
147 ourcl:  .res    CL_SIZE+1