;===============================================================================
;
; pdtlib::ManageCmdline
;
; in 4(sp) CMDLINE* cl
; 8(sp) void* Data
; 12(sp) char* SwitchesList
; 16(sp) ushort (*ArgNotSwitch) (void* Data asm (a0))
; 20(sp) ushort (*ArgSwitch1) (void* Data asm (a0),
; short Sign asm (d1.w)
; )
; 24(sp) (*) ...
;
; out d0.w ushort
;
; destroy std
;
; Look at pdtlib.h for return values
;
; SwitchesList format:
;
; dc.b 'ShortSwitch1', "Long Switch 1", 0
; dc.b 'ShortSwitch2', "Long Switch 2", 0
; dc.b ...
; dc.b 0, 0
;
; 'ShortSwitchX' or "Long Switch X" may be 0, if no such switch exists
; When both are 0, this is the end of the table
;
;===============================================================================
DEFINE pdtlib@0001
ManageCmdline:
movea.l 4(sp),a0 ; CMDLINE*
bsr GetNextArg
moveq.l #PDTLIB_END_OF_PARSING,d0
move.l a0,d1
beq.s \Return
moveq.l #0,d2 ; Offset in the callbacks table, initialized to call
; ArgNotSwitch if the arg haven't a sign
;-----------------------------------------------------------------------
; Check that an arg exists
;-----------------------------------------------------------------------
moveq.l #'+',d1 ; +
cmp.b (a0),d1
beq.s \SignFound
addq.l #2,d1 ; -
cmp.b (a0),d1
beq.s \SignFound
cmpi.b #173,(a0) ; (-) is accepted
bne.s \Callback
;-----------------------------------------------------------------------
; Begin to analyse the arg
;-----------------------------------------------------------------------
\SignFound:
addq.l #1,a0 ; Skip sign
movea.l 12(sp),a1 ; SwitchList
moveq.l #4,d2 ; Switch offset in the list
moveq.l #PDTLIB_EMPTY_SWITCH,d0 ; Check if the sign is alone
tst.b (a0)
beq.s \Return
tst.b 1(a0)
beq.s \ShortSwitch
;-----------------------------------------------------------------------
; Handle a long switch
;-----------------------------------------------------------------------
\LongSwitchLoop:
movem.l d1-d2/a0-a1,-(sp)
pea (a0)
pea 1(a1)
ROMT strcmp
addq.l #8,sp
movem.l (sp)+,d1-d2/a0-a1
tst.w d0
beq.s \Callback
bsr.s \SkipItem
bra.s \LongSwitchLoop
;-----------------------------------------------------------------------
; Handle a short switch
;-----------------------------------------------------------------------
\ShortSwitch:
move.b (a0),d0
cmp.b (a1),d0
beq.s \Callback
bsr.s \SkipItem ; Won't return if EOT is reached
bra.s \ShortSwitch
;-----------------------------------------------------------------------
; Call a callback, and check its return value
;-----------------------------------------------------------------------
\Callback:
movea.l 8(sp),a0 ; Data*
movea.l 16(sp,d2.w),a1 ; (*Callback)
jsr (a1) ; Sign is already in d1.w for switches
\CheckCallbackReturnValue:
cmpi.w #PDTLIB_CONTINUE_PARSING,d0 ; Callback wants the parsing to continue
beq.s ManageCmdline
cmpi.w #PDTLIB_STOP_PARSING,d0
bne.s \WrongReturnValue
moveq.l #PDTLIB_PARSING_STOPPED,d0 ; Callback wants the parsing to stop
\Return: rts
\WrongReturnValue:
moveq.l #PDTLIB_WRONG_RETURN_VALUE,d0 ; Callback throws an unhandled value
rts
;-----------------------------------------------------------------------
; Skip the item pointed to by a0 in the SwitchList
; a1 points to the first byte of the item (short switch)
;-----------------------------------------------------------------------
\SkipItem:
addq.l #1,a1
\SkipItemLoop:
tst.b (a1)+
bne.s \SkipItemLoop
tst.b (a1) ; Test first byte after terminal 0
bne.s \NextSwitch ; This is another item, with a short switch
tst.b 1(a1) ; No short switch, is there a long switch ?
bne.s \NextSwitch ; Yes
addq.l #4,sp ; Else we have reached EOT, remove return address
moveq.l #PDTLIB_SWITCH_NOT_FOUND,d0
\NextSwitch:
addq.w #4,d2 ; Update offset in the callbacks list
rts