]> git.sur5r.net Git - cc65/blob - libsrc/apple2/mainargs.s
Fixed gcc compiler warning (#867)
[cc65] / libsrc / apple2 / mainargs.s
1 ; mainargs.s
2 ;
3 ; Ullrich von Bassewitz, 2003-03-07
4 ; Based on code from Stefan A. Haubenthal <polluks@web.de>, 2003-11-08
5 ; Greg King, 2003-05-18
6 ; Stefan Haubenthal, 2005-01-07
7 ; Oliver Schmidt, 2005-04-05
8 ;
9 ; Scan a group of arguments that are in BASIC's input-buffer.
10 ; Build an array that points to the beginning of each argument.
11 ; Send, to main(), that array and the count of the arguments.
12
13 ; Command-lines look like these lines:
14 ;
15 ; call2051
16 ; call2051 : rem
17 ; call2051:rem arg1 " arg 2 is quoted "  arg3 "" arg5
18 ;
19 ; "call" and "rem" are entokenned; the args. are not. Leading and trailing
20 ; spaces outside of quotes are ignored.
21
22 ; TO-DO:
23 ; Add a control-character quoting mechanism.
24
25         .constructor    initmainargs, 24
26         .import         __argc, __argv, __dos_type
27
28         .include        "zeropage.inc"
29         .include        "apple2.inc"
30
31 ; Maximum number of arguments allowed in the argument table.
32 ; (An argument contains a comma, at least.)
33
34 MAXARGS = 10
35
36 ; ProDOS stores the filename in the second half of BASIC's input buffer, so
37 ; there are 128 characters left. At least 1 character is necessary for the
38 ; REM so 127 characters (including the terminating zero) may be used before
39 ; overwriting the ProDOS filename. As we don't want to further restrict the
40 ; command-line length we reserve those 127 characters.
41
42 BUF_LEN = 127
43
44 BASIC_BUF = $200
45 FNAM_LEN  = $280
46 FNAM      = $281
47 REM       = $B2                 ; BASIC token-code
48
49 ; Get possible command-line arguments. Goes into the special ONCE segment,
50 ; which may be reused after the startup code is run.
51
52         .segment        "ONCE"
53
54 initmainargs:
55
56 ; Assume that the program was loaded, a moment ago, by the traditional BLOAD
57 ; statement of BASIC.SYSTEM. Save the filename as argument #0 if available.
58
59         ldx     __dos_type      ; No ProDOS -> argv[0] = ""
60         beq     :+
61
62 ; Terminate the filename with a zero to make it a valid C string.
63
64         ldx     FNAM_LEN
65 :       lda     #$00
66         sta     FNAM,x
67
68         inc     __argc          ; argc always is equal to, at least, 1
69
70 ; Find the "rem" token.
71
72         ldx     #$00
73 :       lda     BASIC_BUF,x
74         beq     done            ; No "rem" -> no args
75         inx
76         cmp     #REM
77         bne     :-
78
79 ; If a clock is present it is called by ProDOS on file operations. On machines
80 ; with a slot-based clock (like the Thunder Clock) the clock firmware places
81 ; the current date in BASIC's input buffer. Therefore we have to create a copy
82 ; of the command-line in a different buffer before the original is potentially
83 ; destroyed.
84
85         ldy     #$00
86         sty     buffer + BUF_LEN - 1
87 :       lda     BASIC_BUF,x
88         sta     buffer,y
89         inx
90         iny
91         cpy     #BUF_LEN - 1    ; Keep the terminating zero intact
92         bcc     :-
93
94 ; Start processing the arguments.
95
96         ldx     #$00
97         ldy     #$01 * 2        ; Start with argv[1]
98
99 ; Find the next argument. Stop if the end of the string or a character with the
100 ; hibit set is reached. The later is true if the string isn't already parsed by
101 ; BASIC (as expected) but is a still unprocessed input string. In this case the
102 ; string isn't the expected command-line at all. We found this out the hard way
103 ; by BRUNing the program with ProDOS on a machine with a slot-based clock (like
104 ; the Thunder Clock). ProDOS called the clock firmware which places the current
105 ; date as BASIC input string with hibits set in the input buffer. While looking
106 ; for the REM token we stumbled across the first '2' character ($32+$80 = $B2)
107 ; and interpreted the rest of the date as a spurious command-line parameter.
108
109 next:   lda     buffer,x
110         beq     done
111         bmi     done
112         inx
113         cmp     #' '            ; Skip leading spaces
114         beq     next
115
116 ; Found start of next argument. We've incremented the pointer in X already, so
117 ; it points to the second character of the argument. This is useful since we
118 ; will check now for a quoted argument, in which case we will have to skip this
119 ; first character.
120
121         cmp     #'"'            ; Is the argument quoted?
122         beq     :+              ; Jump if so
123         dex                     ; Reset pointer to first argument character
124         lda     #' '            ; A space ends the argument
125 :       sta     tmp1            ; Set end of argument marker
126
127 ; Now store a pointer to the argument into the next slot.
128
129         txa                     ; Get low byte
130         clc
131         adc     #<buffer
132         sta     argv,y          ; argv[y] = &arg
133         iny
134         lda     #$00
135         adc     #>buffer
136         sta     argv,y
137         iny
138         inc     __argc          ; Found another arg
139
140 ; Search for the end of the argument.
141
142 :       lda     buffer,x
143         beq     done
144         inx
145         cmp     tmp1
146         bne     :-
147
148 ; We've found the end of the argument. X points one character behind it, and
149 ; A contains the terminating character. To make the argument a valid C string,
150 ; replace the terminating character by a zero.
151
152         lda     #$00
153         sta     buffer-1,x
154
155 ; Check if the maximum number of command-line arguments is reached. If not,
156 ; parse the next one.
157
158         lda     __argc          ; Get low byte of argument count
159         cmp     #MAXARGS        ; Maximum number of arguments reached?
160         bcc     next            ; Parse next one if not
161
162 ; (The last vector in argv[] already is NULL.)
163
164 done:   lda     #<argv
165         ldx     #>argv
166         sta     __argv
167         stx     __argv+1
168         rts
169
170         .data
171
172 ; char* argv[MAXARGS+1] = {FNAM};
173
174 argv:   .addr   FNAM
175         .res    MAXARGS * 2
176
177         .segment        "INIT"
178
179 buffer: .res    BUF_LEN