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