
includepath "$/inc/"
% filename "t.exe"
'include "RTL32.inc"
include "console.inc"

string cla[4],clp[64]
sys    cln

function CommandLineSplit()
===========================
char* s
@s= (sys) getCommandline
printl s
byte *b=strptr s
int i,j,k,le
sys bb=@b,bs=@b,mm=0,ms=0
do
  select case b
  case 0  : exit do
  case 32 : if mm=0 then exit do '   space (no quote at start)
  case 34 : if mm   then exit do ' " double quote
            mm=@b
            bs=@b+1
  case 47 : ms=@b             ' / fwd slash
  case 92 : ms=@b             ' \ back slash
  end select
  @b++
end do
cla[1]=s
if ms then
  cla[2]=mid(s,bs-bb+1,ms-bs+1) 'path
else
  ms=bb-1
end if
cla[3]=mid(s,ms-bb+2,@b-ms-1) 'filename
@b++
i=@b-bb+1
le=len s
cln=0
if mm then
  do 'look for first arg
    @b++ : i++
    select b
    case 0  : exit do 'no args
    case 34 : @b++ : i++ : exit do 'skip quote
    case 33 to 255 : exit do
    end select
    @b++ : i++
  end do
  cla[4]=mid(s,i,le-i) 'remainder as args without quotes
else
  cla[4]=mid(s,i)     'remainder as args. no quotes expected
end if
'
le=len cla[3]
if le=0 then return
i=1
j=1
k=0
@b=strptr cla[4]
do 'capture all args
  do 'skip leading spaces
    select b
    case 0 : jmp fwd done
    case 33 to 255 : exit do
    end select
    i++ : j++ : @b++
  end do
  if b=0 then jmp fwd done
  do 'capture each arg
    select case b
    case 0 to 32
      k++ : cln++
      clp[k]=mid cla[4],j,i-j
      if b=0 then jmp fwd done
      j=i
      @b++ : i++ : j++
      exit do
    end select
    @b++ : i++
  end do
end do
done:
end function

'TEST
CommandLineSplit()
printl "raw"  tab cla[1]
printl "path" tab cla[2]
printl "file" tab cla[3]
printl "Args" tab cla[4]
printl "Arg count: " cln
int i
for i=1 to cln
  printl i tab clp[i]
next

waitkey



