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 ; call2048: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, 18
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.)
39 REM = $B2 ; BASIC token-code
41 ; Get possible command-line arguments. Goes into the special INIT segment,
42 ; which may be reused after the startup code is run
48 ; Assume that the program was loaded, a moment ago, by the traditional BLOAD
49 ; statement of BASIC.SYSTEM. Save the "most-recent filename" as argument #0.
51 ldx __dos_type ; No ProDOS -> argv[0] = ""
54 ; Terminate the filename with a zero to make it a valid C string.
60 inc __argc ; argc always is equal to, at least, 1
62 ; Find the "rem" token.
66 beq done ; No "rem" -> no args
70 ldy #$01 * 2 ; Start with argv[1]
72 ; Find the next argument.
77 cmp #' ' ; Skip leading spaces
80 ; Found start of next argument. We've incremented the pointer in X already, so
81 ; it points to the second character of the argument. This is useful since we
82 ; will check now for a quoted argument, in which case we will have to skip this
85 cmp #'"' ; Is the argument quoted?
86 beq setterm ; Jump if so
87 dex ; Reset pointer to first argument character
88 lda #' ' ; A space ends the argument
89 setterm:sta tmp1 ; Set end of argument marker
91 ; Now store a pointer to the argument into the next slot. Since the BASIC
92 ; input buffer is located at the start of a RAM page, no calculations are
96 sta argv,y ; argv[y] = &arg
101 inc __argc ; Found another arg
103 ; Search for the end of the argument
105 argloop:lda BASIC_BUF,x
111 ; We've found the end of the argument. X points one character behind it, and
112 ; A contains the terminating character. To make the argument a valid C string,
113 ; replace the terminating character by a zero.
118 ; Check if the maximum number of command line arguments is reached. If not,
119 ; parse the next one.
121 lda __argc ; Get low byte of argument count
122 cmp #MAXARGS ; Maximum number of arguments reached?
123 bcc next ; Parse next one if not
125 ; (The last vector in argv[] already is NULL.)
133 ; This array is zeroed before initmainargs is called.
134 ; char* argv[MAXARGS+1] = {FNAM};