2003-03-10 21:21:46 +00:00
|
|
|
;
|
2013-07-16 04:30:56 +00:00
|
|
|
; 2003-03-07, Ullrich von Bassewitz
|
|
|
|
; 2011-01-28, Stefan Haubenthal
|
|
|
|
; 2013-07-15, Greg King
|
2003-03-10 21:21:46 +00:00
|
|
|
;
|
|
|
|
; Setup arguments for main
|
|
|
|
;
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
.constructor initmainargs, 24
|
|
|
|
.import __argc, __argv
|
2013-07-16 04:30:56 +00:00
|
|
|
|
|
|
|
.include "atmos.inc"
|
2013-05-09 11:56:54 +00:00
|
|
|
.macpack generic
|
2003-03-10 21:21:46 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
MAXARGS = 10 ; Maximum number of arguments allowed
|
|
|
|
REM = $9d ; BASIC token-code
|
2003-03-10 21:21:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
;---------------------------------------------------------------------------
|
2005-02-26 09:28:46 +00:00
|
|
|
; Get possible command-line arguments. Goes into the special INIT segment,
|
|
|
|
; which may be reused after the startup code is run
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
.segment "INIT"
|
2003-03-10 21:21:46 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
.proc initmainargs
|
2003-03-10 21:21:46 +00:00
|
|
|
|
2011-07-03 13:23:07 +00:00
|
|
|
; 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.
|
|
|
|
;
|
2013-07-16 04:30:56 +00:00
|
|
|
ldy #FNAME_LEN - 1 ; limit the length
|
|
|
|
L0: lda CFOUND_NAME,y
|
2013-05-09 11:56:54 +00:00
|
|
|
sta name,y
|
|
|
|
dey
|
|
|
|
bpl L0
|
|
|
|
inc __argc ; argc always is equal to, at least, 1
|
2011-01-28 12:12:59 +00:00
|
|
|
|
|
|
|
; Find the "rem" token.
|
|
|
|
;
|
2013-05-09 11:56:54 +00:00
|
|
|
ldx #0
|
|
|
|
L2: lda BASIC_BUF,x
|
|
|
|
beq done ; no "rem," no args.
|
|
|
|
inx
|
|
|
|
cmp #REM
|
|
|
|
bne L2
|
|
|
|
ldy #1 * 2
|
2011-01-28 12:12:59 +00:00
|
|
|
|
|
|
|
; Find the next argument
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
next: lda BASIC_BUF,x
|
|
|
|
beq done ; End of line reached
|
|
|
|
inx
|
|
|
|
cmp #' ' ; Skip leading spaces
|
|
|
|
beq next ;
|
2011-01-28 12:12:59 +00:00
|
|
|
|
|
|
|
; 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.
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
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
|
2011-01-28 12:12:59 +00:00
|
|
|
|
|
|
|
; Now store a pointer to the argument into the next slot. Since the BASIC
|
2011-07-03 13:23:07 +00:00
|
|
|
; input buffer is located at the zero page, no calculations are necessary.
|
2011-01-28 12:12:59 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
txa ; Get low byte
|
|
|
|
add #<BASIC_BUF ; Not at page boundary
|
|
|
|
sta argv,y ; argv[y]= &arg
|
|
|
|
iny
|
|
|
|
iny
|
|
|
|
inc __argc ; Found another arg
|
2011-01-28 12:12:59 +00:00
|
|
|
|
|
|
|
; Search for the end of the argument
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
argloop:lda BASIC_BUF,x
|
|
|
|
beq done
|
|
|
|
inx
|
|
|
|
cmp term
|
|
|
|
bne argloop
|
2011-01-28 12:12:59 +00:00
|
|
|
|
|
|
|
; We've found the end of the argument. X points one character behind it, and
|
|
|
|
; A contains the terminating character. To make the argument a valid C string,
|
|
|
|
; replace the terminating character by a zero.
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
lda #0
|
|
|
|
sta BASIC_BUF-1,x
|
2011-01-28 12:12:59 +00:00
|
|
|
|
|
|
|
; Check if the maximum number of command line arguments is reached. If not,
|
|
|
|
; parse the next one.
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
lda __argc ; Get low byte of argument count
|
|
|
|
cmp #MAXARGS ; Maximum number of arguments reached?
|
|
|
|
bcc next ; Parse next one if not
|
2011-01-28 12:12:59 +00:00
|
|
|
|
|
|
|
; (The last vector in argv[] already is NULL.)
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
done: lda #<argv
|
|
|
|
ldx #>argv
|
|
|
|
sta __argv
|
|
|
|
stx __argv + 1
|
2011-01-28 12:12:59 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
rts
|
2003-03-10 21:21:46 +00:00
|
|
|
|
|
|
|
.endproc
|
|
|
|
|
2011-01-28 12:12:59 +00:00
|
|
|
; These arrays are zeroed before initmainargs is called.
|
2013-05-09 11:56:54 +00:00
|
|
|
; char name[16+1];
|
2011-01-28 12:12:59 +00:00
|
|
|
; char* argv[MAXARGS+1]={name};
|
|
|
|
;
|
|
|
|
.bss
|
2013-05-09 11:56:54 +00:00
|
|
|
term: .res 1
|
2013-07-16 04:30:56 +00:00
|
|
|
name: .res FNAME_LEN + 1
|
2003-03-10 21:21:46 +00:00
|
|
|
|
2011-01-28 12:12:59 +00:00
|
|
|
.data
|
2013-05-09 11:56:54 +00:00
|
|
|
argv: .addr name
|
|
|
|
.res MAXARGS * 2
|