! Function Name: Fn'Get'Switches(cmdlin,fdelims,arg,sw1,sw2,...swN) ! Description: Parse command line switches ! Author: Jack McGregor ! Date: 06-Nov-2008 !------------------------------------------------------------------------- !Function: Fn'Get'Switches(cmdlin,arg,sw1,sw2,...) !Parameters: ! cmdlin [s512,in] - command line to parse ! fdelims [s20,in] - list of (single char) delimiters ! arg [s160,out] - first part of passed line (prior to switches) ! swN [s100,out] - switches individually returned (up to 20) !Returns: ! # of switches actually processed (up to 25) ! <0 for error !Notes: ! Eg. Passing the following line with "/" for the delimiter: ! IMGDSX /T:title/B/E/X/A:1234 /C:SYM="A/B" ! would return arg="IMGDSX", following by individual arguments ! "/T:title", "/B", "/E", "/X", "A:1234 " and "C:SYM=""A/B""" !------------------------------------------------------------------------- !Edit History !VEDIT=100 ![100] November 06, 2008 03:10 PM Edited by jack ![101] November 25, 2008 01:55 PM Edited by jack ! Fix illegal subscript loophole !--------------------------------------------------------------------- ++ifndef INC_GET_SWITCHES_BSI_ ! avoid including more than once define INC_GET_SWITCHES_BSI_ = 1 define MAX_GET_SWITCHES = 20 Function Fn'Get'Switches(cline as s512, fdelims as s20, arg as s80, & s1 as s80, s2 as s80, s3 as s80, s4 as s80, s5 as s80, & s6 as s80, s7 as s80, s8 as s80, s9 as s80, s10 as s80, & s11 as s80, s12 as s80, s13 as s80, s14 as s80, s15 as s80, & s16 as s80, s17 as s80, s18 as s80, s19 as s80, s20 as s80) as i2 map1 locals map2 args,b,2,.argcnt ! copy .argcnt to loc var (before it gets changed) map2 op,b,1,2 ! strtok opcode (2=recognize quoting) map2 rdelim,s,2,"" ! no record delimiters map2 i,i,2 ! here we use a trick to overlay the individual params with an array ! (The function will create virtual map statements (MAP1 Sn,S,80) for ! each, in sequence. As long as they are even in size, an array will ! overlay it perfectly.) Otherwise we could just reference the individual ! variables throughout (as we do in the XCALL STRTOK); but the array is ! handy to condense the return operation. map1 soverlay,@s1 ! overlay the virtual map1 s*,s,80 array map2 sx(MAX_GET_SWITCHES),s,80 xcall STRTOK,op,cline,fdelims,arg,rdelim, & s1,s2,s3,s4,s5,s6,s7,s8,s9,s10, & s11,s12,s13,s14,s15,s16,s17,s18,s19,s20 ! note that xputarg will not have any effect on args ! which were not passed to us, so we'll just return ! as many as we parsed. if we wanted, we could use ! the .argcnt info to limit the return to only those ! args which will be processed by the caller. xputarg 3,arg i = 1 ![102] do while (i <= MAX_GET_SWITCHES) and (sx(i) # "") ![102] need to split up the 'and' since Basic always executes both sides ![102] so sx(i) would err out if i > MAX_GET_SWITCHES do while (i <= MAX_GET_SWITCHES) if sx(i) = "" exit ! [102] xputarg i+3,sx(i) i = i + 1 loop Fn'Get'Switches = i - 1 End Function ++endif