2 ; Ullrich von Bassewitz, 2003-03-07
3 ; Stefan Haubenthal, 2011-01-28
5 ; Setup arguments for main
8 .constructor initmainargs, 24
12 MAXARGS = 10 ; Maximum number of arguments allowed
13 REM = $9d ; BASIC token-code
14 NAME_LEN = 16 ; maximum length of command-name
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 #NAME_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.
49 ; Find the next argument
52 beq done ; End of line reached
54 cmp #' ' ; Skip leading spaces
57 ; Found start of next argument. We've incremented the pointer in X already, so
58 ; it points to the second character of the argument. This is useful since we
59 ; will check now for a quoted argument, in which case we will have to skip this
62 found: cmp #'"' ; Is the argument quoted?
63 beq setterm ; Jump if so
64 dex ; Reset pointer to first argument character
65 lda #' ' ; A space ends the argument
66 setterm:sta term ; Set end of argument marker
68 ; Now store a pointer to the argument into the next slot. Since the BASIC
69 ; input buffer is located at the zero page, no calculations are necessary.
72 add #<BASIC_BUF ; Not at page boundary
73 sta argv,y ; argv[y]= &arg
76 inc __argc ; Found another arg
78 ; Search for the end of the argument
80 argloop:lda BASIC_BUF,x
86 ; We've found the end of the argument. X points one character behind it, and
87 ; A contains the terminating character. To make the argument a valid C string,
88 ; replace the terminating character by a zero.
93 ; Check if the maximum number of command line arguments is reached. If not,
96 lda __argc ; Get low byte of argument count
97 cmp #MAXARGS ; Maximum number of arguments reached?
98 bcc next ; Parse next one if not
100 ; (The last vector in argv[] already is NULL.)
111 ; These arrays are zeroed before initmainargs is called.
113 ; char* argv[MAXARGS+1]={name};
117 name: .res NAME_LEN + 1