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
7 ; Scan a group of arguments that are in BASIC's input-buffer.
8 ; Build an array that points to the beginning of each argument.
9 ; Send, to main(), that array and the count of the arguments.
11 ; Command-lines look like these lines:
14 ; call2048 : rem no arguments because no comma!
15 ; call2048:rem,arg1," arg 2" , arg 3 ,, arg5, ...
17 ; "call" and "rem" are entokenned; the args. are not. Leading spaces are
18 ; ignored; trailing spaces are included -- unless the argument was quoted.
21 ; - The "file-name" might be a path-name; don't copy the directory-components.
22 ; - Add a control-character quoting mechanism.
24 .constructor initmainargs, 24
25 .import __argc, __argv
29 ; Maximum number of arguments allowed in the argument table.
30 ; (An argument contains a comma, at least.)
36 MAXARGS = BASIC_BUF_LEN - 2 ; (don't count REM and terminating '\0')
38 REM = $B2 ; BASIC token-code
39 NAME_LEN = 15 ; maximum length of command-name
41 ; Get possible command-line arguments.
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.
53 ldy #NAME_LEN - 1 ; limit the length
62 inc __argc ; argc always is equal to, at least, 1
64 ; Find the "rem" token.
68 beq done ; no "rem," no args.
74 ; Find the next argument.
79 cmp #',' ; look for argument-list separator
82 sta BASIC_BUF-1,x ; make the previous arg. be a legal C string
83 inc __argc ; found another arg.
86 beq point ; zero-length argument
89 beq L4 ; skip leading spaces
91 cmp #'"' ; is argument quoted?
93 dex ; no, don't skip over character
94 clc ; (quotation-mark sets flag)
95 L5: ror quoted ; save it
97 ; BASIC's input-buffer starts at the beginning of a RAM page.
98 ; So, we don't need to add the offset -- just store it.
101 sta argv,y ; argv[y]= &arg
107 asl quoted ; is argument a string-literal?
108 bcc next ; no, don't look for ending quotation-mark
115 sta BASIC_BUF-1,x ; make this arg. be a legal C string
118 ; (The last vector in argv[] already is NULL.)
126 ; These arrays are zeroed before initmainargs is called.
128 ; char* argv[MAXARGS+1]={name};
131 quoted: .res 1, %00000000
132 name: .res NAME_LEN + 1
133 argv: .res (MAXARGS + 1) * 2