]> git.sur5r.net Git - cc65/blob - libsrc/atari/getargs.s
Adjusted constructors.
[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        "ONCE"
24
25 initmainargs:
26         lda     __dos_type      ; which DOS?
27         cmp     #ATARIDOS
28         beq     nargdos         ; DOS does not support arguments
29         cmp     #MYDOS
30         bne     argdos          ; DOS supports arguments
31 nargdos:rts
32
33 ; Initialize ourcl buffer
34
35 argdos: lda     #ATEOL
36         sta     ourcl+CL_SIZE
37
38 ; Move SpartaDOS command line to our own buffer
39
40         lda     DOSVEC
41         clc
42         adc     #<LBUF
43         sta     ptr1
44         lda     DOSVEC+1
45         adc     #>LBUF
46         sta     ptr1+1
47
48         ldy     #0
49 cpcl:   lda     (ptr1),y
50         sta     ourcl,y
51         iny
52         cmp     #ATEOL
53         beq     movdon
54         cpy     #CL_SIZE
55         bne     cpcl
56
57 movdon: lda     #0
58         sta     ourcl,y         ; null terminate behind ATEOL
59
60 ; Turn command line into argv table
61
62         ;ldy    #0
63         tay
64 eatspc: lda     ourcl,y         ; eat spaces
65         cmp     #ATEOL
66         beq     finargs
67         cmp     #SPACE
68         bne     rpar            ; begin of argument found
69         iny
70         cpy     #CL_SIZE
71         bne     eatspc
72         beq     finargs         ; only spaces is no argument
73
74 ; Store argument vector
75
76 rpar:   lda     __argc          ; low-byte
77         asl
78         tax                     ; table index
79         tya                     ; ourcl index
80         clc
81         adc     #<ourcl
82         sta     argv,x
83         lda     #>ourcl
84         adc     #0
85         sta     argv+1,x
86         ldx     __argc
87         inx
88         stx     __argc
89         cpx     #MAXARGS
90         beq     finargs
91
92 ; Skip this arg.
93
94 skiparg:
95         ldx     ourcl,y
96         cpx     #ATEOL          ; end of line?
97         beq     eopar
98         cpx     #SPACE
99         beq     eopar
100         iny
101         cpy     #CL_SIZE
102         bne     skiparg
103
104 ; End of arg. -> place 0
105
106 eopar:
107         lda     #0
108         sta     ourcl,y
109         iny                     ; y behind arg.
110         cpx     #ATEOL          ; was it the last arg?
111         bne     eatspc
112
113 ; Finish args
114
115 finargs:
116         lda     __argc
117         asl
118         tax
119         lda     #0
120         sta     argv,x
121         sta     argv+1,x
122         lda     #<argv
123         ldx     #>argv
124         sta     __argv
125         stx     __argv+1
126         rts
127
128 ; --------------------------------------------------------------------------
129 ; Data
130
131 .segment        "INIT"
132
133 argv:   .res    (1 + MAXARGS) * 2
134
135 ; Buffer for command line / argv strings
136
137 ourcl:  .res    CL_SIZE+1