3 ; Ullrich von Bassewitz, 2003-03-07
4 ; Based on code from Stefan A. Haubenthal, <polluks@web.de>
5 ; 2003-05-18, Greg King
6 ; 2004-04-28, Ullrich von Bassewitz
8 ; Scan a group of arguments that are in BASIC's input-buffer.
9 ; Build an array that points to the beginning of each argument.
10 ; Send, to main(), that array and the count of the arguments.
12 ; Command-lines look like these lines:
16 ; run:rem arg1 " arg 2 is quoted " arg3 "" arg5
18 ; "run" and "rem" are entokenned; the args. are not. Leading and trailing
19 ; spaces outside of quotes are ignored.
22 ; - The "file-name" might be a path-name; don't copy the directory-components.
23 ; - Add a control-character quoting mechanism.
25 .constructor initmainargs, 24
26 .import __argc, __argv
32 MAXARGS = 10 ; Maximum number of arguments allowed
33 REM = $8f ; BASIC token-code
34 NAME_LEN = 16 ; maximum length of command-name
36 ; Get possible command-line arguments.
40 ; Assume that the program was loaded, a moment ago, by the traditional LOAD
41 ; statement. Save the "most-recent filename" as argument #0.
42 ; Because the buffer, that we're copying into, was zeroed out,
43 ; we don't need to add a NUL character.
48 ldy #NAME_LEN - 1 ; limit the length
53 inc __argc ; argc always is equal to, at least, 1
55 ; Find the "rem" token.
59 beq done ; no "rem," no args.
65 ; Find the next argument
68 beq done ; End of line reached
70 cmp #' ' ; Skip leading spaces
73 ; Found start of next argument. We've incremented the pointer in X already, so
74 ; it points to the second character of the argument. This is useful since we
75 ; will check now for a quoted argument, in which case we will have to skip this
78 found: cmp #'"' ; Is the argument quoted?
79 beq setterm ; Jump if so
80 dex ; Reset pointer to first argument character
81 lda #' ' ; A space ends the argument
82 setterm:sta term ; Set end of argument marker
84 ; Now store a pointer to the argument into the next slot. Since the BASIC
85 ; input buffer is located at the start of a RAM page, no calculations are
89 sta argv,y ; argv[y]= &arg
94 inc __argc ; Found another arg
96 ; Search for the end of the argument
98 argloop:lda BASIC_BUF,x
104 ; We've found the end of the argument. X points one character behind it, and
105 ; A contains the terminating character. To make the argument a valid C string,
106 ; replace the terminating character by a zero.
111 ; Check if the maximum number of command line arguments is reached. If not,
112 ; parse the next one.
114 lda __argc ; Get low byte of argument count
115 cmp #MAXARGS ; Maximum number of arguments reached?
116 bcc next ; Parse next one if not
118 ; (The last vector in argv[] already is NULL.)
126 ; These arrays are zeroed before initmainargs is called.
128 ; char* argv[MAXARGS+1]={name};
132 name: .res NAME_LEN + 1