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
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.
13 ; Command-lines look like these lines:
17 ; call2051:rem arg1 " arg 2 is quoted " arg3 "" arg5
19 ; "call" and "rem" are entokenned; the args. are not. Leading and trailing
20 ; spaces outside of quotes are ignored.
23 ; Add a control-character quoting mechanism.
25 .constructor initmainargs, 24
26 .import __argc, __argv, __dos_type
28 .include "zeropage.inc"
31 ; Maximum number of arguments allowed in the argument table.
32 ; (An argument contains a comma, at least.)
36 ; ProDOS stores the filename in the second half of BASIC's input buffer, so
37 ; there are 128 characters left. At least 7 characters are necessary for the
38 ; CALLxxxx:REM so 121 characters may be used before overwriting the ProDOS
39 ; filename. As we don't want to put further restrictions on the command-line
40 ; length we reserve those 121 characters terminated by a zero.
47 REM = $B2 ; BASIC token-code
49 ; Get possible command-line arguments. Goes into the special INIT segment,
50 ; which may be reused after the startup code is run.
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.
59 ldx __dos_type ; No ProDOS -> argv[0] = ""
62 ; Terminate the filename with a zero to make it a valid C string.
68 inc __argc ; argc always is equal to, at least, 1
70 ; Find the "rem" token.
74 beq done ; No "rem" -> no args
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
90 cpy #BUF_LEN - 1 ; Keep the terminating zero intact
93 ; Start processing the arguments.
96 ldy #$01 * 2 ; Start with argv[1]
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.
112 cmp #' ' ; Skip leading spaces
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
120 cmp #'"' ; Is the argument quoted?
122 dex ; Reset pointer to first argument character
123 lda #' ' ; A space ends the argument
124 : sta tmp1 ; Set end of argument marker
126 ; Now store a pointer to the argument into the next slot.
131 sta argv,y ; argv[y] = &arg
137 inc __argc ; Found another arg
139 ; Search for the end of the argument.
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.
154 ; Check if the maximum number of command-line arguments is reached. If not,
155 ; parse the next one.
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
161 ; (The last vector in argv[] already is NULL.)
169 ; This array is zeroed before initmainargs is called.
170 ; char* argv[MAXARGS+1] = {FNAM};