2 ; 2003-03-07, Ullrich von Bassewitz
3 ; 2011-01-28, Stefan Haubenthal
4 ; 2014-09-10, Greg King
6 ; Set up arguments for main
9 .constructor initmainargs, 24
10 .import __argc, __argv
15 MAXARGS = 10 ; Maximum number of arguments allowed
16 REM = $9d ; BASIC token-code
19 ;---------------------------------------------------------------------------
20 ; Get possible command-line arguments. Goes into the special INIT segment,
21 ; which may be reused after the startup code is run
27 ; Assume that the program was loaded, a moment ago, by the traditional LOAD
28 ; statement. Save the "most-recent filename" as argument #0.
29 ; Because the buffer, that we're copying into, was zeroed out,
30 ; we don't need to add a NUL character.
32 ldy #FNAME_LEN - 1 ; limit the length
37 inc __argc ; argc always is equal to, at least, 1
39 ; Find the "rem" token.
43 beq done ; no "rem", no args.
48 ; The arguments must be copied to a safe place because BASIC's input buffer
49 ; might be re-used by the stdin console.
51 ldy #(SCREEN_XSIZE * 2 - 1) - 1
57 ldy #1 * 2 ; Point to second argv slot
59 ; Find the next argument
62 beq done ; End of line reached
64 cmp #' ' ; Skip leading spaces
67 ; Found start of next argument. We've incremented the pointer in X already, so
68 ; it points to the second character of the argument. This is useful since we
69 ; will check now for a quoted argument, in which case we will have to skip this
72 found: cmp #'"' ; Is the argument quoted?
73 beq setterm ; Jump if so
74 dex ; Reset pointer to first argument character
75 lda #' ' ; A space ends the argument
76 setterm:sta term ; Set end of argument marker
78 ; Now, store a pointer, to the argument, into the next slot.
82 sta argv,y ; argv[y]= &arg
88 inc __argc ; Found another arg
90 ; Search for the end of the argument
92 argloop:lda BASIC_BUF,x
98 ; We've found the end of the argument. X points one character behind it, and
99 ; A contains the terminating character. To make the argument a valid C string,
100 ; replace the terminating character by a zero.
105 ; Check if the maximum number of command line arguments is reached. If not,
106 ; parse the next one.
108 lda __argc ; Get low byte of argument count
109 cmp #MAXARGS ; Maximum number of arguments reached?
110 bcc next ; Parse next one if not
112 ; (The last vector in argv[] already is NULL.)
122 ; These arrays are zeroed before initmainargs is called.
124 ; char* argv[MAXARGS+1]={name};
128 name: .res FNAME_LEN + 1
129 args: .res SCREEN_XSIZE * 2 - 1
133 .res MAXARGS * 2, $00