+.segment "INIT"
+
+.proc initmainargs
+
+; Assume that the program was loaded, a moment ago, by the traditional LOAD
+; statement. Save the "most-recent filename" as argument #0.
+; Because the buffer, that we're copying into, was zeroed out,
+; we don't need to add a NUL character.
+;
+ ldy FNLEN
+ cpy #NAME_LEN + 1
+ bcc L1
+ ldy #NAME_LEN - 1 ; limit the length
+L0: lda (FNADR),y
+ sta name,y
+L1: dey
+ bpl L0
+ inc __argc ; argc always is equal to, at least, 1
+
+; Find the "rem" token.
+;
+ ldx #0
+L2: lda BASIC_BUF,x
+ beq done ; no "rem," no args.
+ inx
+ cmp #REM
+ bne L2
+ ldy #1 * 2
+
+; Find the next argument
+
+next: lda BASIC_BUF,x
+ beq done ; End of line reached
+ inx
+ cmp #' ' ; Skip leading spaces
+ beq next ;
+
+; Found start of next argument. We've incremented the pointer in X already, so
+; it points to the second character of the argument. This is useful since we
+; will check now for a quoted argument, in which case we will have to skip this
+; first character.
+
+found: cmp #'"' ; Is the argument quoted?
+ beq setterm ; Jump if so
+ dex ; Reset pointer to first argument character
+ lda #' ' ; A space ends the argument
+setterm:sta term ; Set end of argument marker