]> git.sur5r.net Git - cc65/blob - libsrc/apple2/mainargs.s
Allow up 127 chars of cmdline for programs started by the loader.
[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 characters is necessary for the
38 ; REM so 127 characters may be used before overwriting the ProDOS filename.
39 ; As we don't want to put further restrictions on the command-line length
40 ; we reserve those 127 characters terminated by a zero.
41
42 BUF_LEN = 128
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 INIT segment,
50 ; which may be reused after the startup code is run.
51
52         .segment        "INIT"
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 :       lda     BASIC_BUF,x
87         sta     buffer,y
88         inx
89         iny
90         cpy     #BUF_LEN - 1    ; Keep the terminating zero intact
91         bcc     :-
92
93 ; Start processing the arguments.
94
95         ldx     #$00
96         ldy     #$01 * 2        ; Start with argv[1]
97
98 ; Find the next argument. Stop if the end of the string or a character with the
99 ; hibit set is reached. The later is true if the string isn't already parsed by
100 ; BASIC (as expected) but is a still unprocessed input string. In this case the
101 ; string isn't the expected command-line at all. We found this out the hard way
102 ; by BRUNing the program with ProDOS on a machine with a slot-based clock (like
103 ; the Thunder Clock). ProDOS called the clock firmware which places the current
104 ; date as BASIC input string with hibits set in the input buffer. While looking
105 ; for the REM token we stumbled across the first '2' character ($32+$80 = $B2)
106 ; and interpreted the rest of the date as a spurious command-line parameter.
107
108 next:   lda     buffer,x
109         beq     done
110         bmi     done
111         inx
112         cmp     #' '            ; Skip leading spaces
113         beq     next
114
115 ; Found start of next argument. We've incremented the pointer in X already, so
116 ; it points to the second character of the argument. This is useful since we
117 ; will check now for a quoted argument, in which case we will have to skip this
118 ; first character.
119
120         cmp     #'"'            ; Is the argument quoted?
121         beq     :+              ; Jump if so
122         dex                     ; Reset pointer to first argument character
123         lda     #' '            ; A space ends the argument
124 :       sta     tmp1            ; Set end of argument marker
125
126 ; Now store a pointer to the argument into the next slot.
127
128         txa                     ; Get low byte
129         clc
130         adc     #<buffer
131         sta     argv,y          ; argv[y] = &arg
132         iny
133         lda     #$00
134         adc     #>buffer
135         sta     argv,y
136         iny
137         inc     __argc          ; Found another arg
138
139 ; Search for the end of the argument.
140
141 :       lda     buffer,x
142         beq     done
143         inx
144         cmp     tmp1
145         bne     :-
146
147 ; We've found the end of the argument. X points one character behind it, and
148 ; A contains the terminating character. To make the argument a valid C string,
149 ; replace the terminating character by a zero.
150
151         lda     #$00
152         sta     buffer-1,x
153
154 ; Check if the maximum number of command-line arguments is reached. If not,
155 ; parse the next one.
156
157         lda     __argc          ; Get low byte of argument count
158         cmp     #MAXARGS        ; Maximum number of arguments reached?
159         bcc     next            ; Parse next one if not
160
161 ; (The last vector in argv[] already is NULL.)
162
163 done:   lda     #<argv
164         ldx     #>argv
165         sta     __argv
166         stx     __argv+1
167         rts
168
169 ; This array is zeroed before initmainargs is called.
170 ; char* argv[MAXARGS+1] = {FNAM};
171
172         .data
173
174 argv:   .addr   FNAM
175         .res    MAXARGS * 2
176
177         .bss
178
179 buffer: .res    BUF_LEN