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