3 ; Ullrich von Bassewitz, 2003-03-07
4 ; Based on code from Stefan A. Haubenthal, <polluks@web.de>
5 ; 2003-05-18, Greg King
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 ; run : rem no arguments because no comma!
15 ; run:rem,arg1," arg 2" , arg 3 ,, arg5, ...
17 ; "run" 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.)
32 MAXARGS = BASIC_BUF_LEN - 2 ; (don't count REM and terminating '\0')
34 REM = $8f ; BASIC token-code
35 NAME_LEN = 16 ; maximum length of command-name
37 ; Get possible command-line arguments.
41 ; Assume that the program was loaded, a moment ago, by the traditional LOAD
42 ; statement. Save the "most-recent filename" as argument #0.
43 ; Because the buffer, that we're copying into, was zeroed out,
44 ; we don't need to add a NUL character.
49 ldy #NAME_LEN - 1 ; limit the length
58 inc __argc ; argc always is equal to, at least, 1
60 ; Find the "rem" token.
64 beq done ; no "rem," no args.
70 ; Find the next argument.
75 cmp #',' ; look for argument-list separator
78 sta BASIC_BUF-1,x ; make the previous arg. be a legal C string
79 inc __argc ; found another arg.
82 beq point ; zero-length argument
85 beq L4 ; skip leading spaces
87 cmp #'"' ; is argument quoted?
89 dex ; no, don't skip over character
90 clc ; (quotation-mark sets flag)
91 L5: ror quoted ; save it
93 ; BASIC's input-buffer starts at the beginning of a RAM page.
94 ; So, we don't need to add the offset -- just store it.
97 sta argv,y ; argv[y]= &arg
103 asl quoted ; is argument a string-literal?
104 bcc next ; no, don't look for ending quotation-mark
111 sta BASIC_BUF-1,x ; make this arg. be a legal C string
114 ; (The last vector in argv[] already is NULL.)
122 ; These arrays are zeroed before initmainargs is called.
124 ; char* argv[MAXARGS+1]={name};
127 quoted: .res 1, %00000000
128 name: .res NAME_LEN + 1
129 argv: .res (MAXARGS + 1) * 2