3 ; 2003-03-07, Ullrich von Bassewitz,
4 ; based on code from Stefan A. Haubenthal, <polluks@web.de>
5 ; 2005-02-26, Ullrich von Bassewitz
6 ; 2014-09-10, Greg King
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
27 .import sys_bank, restore_bank
28 .import sysp0:zp, ptr1:zp
34 MAXARGS = 10 ; Maximum number of arguments allowed
35 REM = $8f ; BASIC token-code
36 NAME_LEN = 16 ; Maximum length of command-name
38 ; Get possible command-line arguments. Goes into the special ONCE segment,
39 ; which may be reused after the startup code is run.
45 ; Assume that the program was loaded, a moment ago, by the traditional LOAD
46 ; statement. Save the "most-recent filename" as argument #0.
47 ; Because the buffer, that we're copying into, was zeroed out,
48 ; we don't need to add a NUL character.
52 lda (sysp0),y ; Get file-name pointer from system bank
63 lda #0 ; The terminating NUL character
64 stx IndReg ; Look for name in correct bank
67 ldy #NAME_LEN ; Limit the length
68 bne L1 ; Branch always
74 inc __argc ; argc always is equal to at least 1
80 bze done ; No "rem," no args.
86 ; Find the next argument.
89 bze done ; End of line reached
91 cmp #' ' ; Skip leading spaces
94 ; Found start of next argument. We've incremented the pointer in X already, so
95 ; it points to the second character of the argument. That is useful because we
96 ; will check now for a quoted argument; in which case, we will have to skip that
99 found: cmp #'"' ; Is the argument quoted?
100 beq setterm ; Jump if so
101 dex ; Reset pointer to first argument character
102 lda #' ' ; A space ends the argument
103 setterm:sta term ; Set end-of-argument marker
105 ; Now, store a pointer to the argument into the next slot.
109 sta argv,y ; argv[y]= &arg
115 inc __argc ; Found another arg
117 ; Search for the end of the argument.
119 argloop:lda BASIC_BUF,x
125 ; We've found the end of the argument. X points one character behind it, and
126 ; A contains the terminating character. To make the argument a valid C string,
127 ; replace the terminating character by a zero.
132 ; Check if the maximum number of command-line arguments is reached. If not,
133 ; parse the next one.
135 lda __argc ; Get low byte of argument count
136 cmp #MAXARGS ; Maximum number of arguments reached?
137 blt next ; Parse next one if not
139 ; (The last vector in argv[] already is NULL.)
150 name: .res NAME_LEN + 1
154 ; char* argv[MAXARGS+1]={name};