Author Topic: Access to argv and argc?  (Read 2798 times)

0 Members and 1 Guest are viewing this topic.

bugmagnet

  • Guest
Access to argv and argc?
« on: April 20, 2013, 06:22:25 PM »
Hello

Apart from calling GetCommandLine(), could OB make visible the original argc and argv of the program? Wouldn't mean much running in an editor, but once built the binary would then have access to command line items in a way that would be less demanding that using GetCommandLine

Kind regards,
bugmagnet

Charles Pegge

  • Guest
Re: Access to argv and argc?
« Reply #1 on: April 21, 2013, 03:21:20 AM »
Hi Bruce,

As you have realised, it is not trivial code.

I have written some candidate code here. I propose putting it into a file called mainutil.inc.


//http://en.wikipedia.org/wiki/Main_function

includepath "$/inc/"
include "minwin.inc"

indexbase 1
zstring z at (getcommandline)
sys v[32]
sys argc

'print z

'char z[]=`"command" "one two three four"`

byte b at @z
byte a
sys i=1
indexbase 1
do
  a=b[ i]
  if a=34
    do
      i++
      a=b[ i]
      if a=34 then i++ : jmp fwd ncommand
      if a=0 then jmp fwd ncommand
    end do
  end if 'a=34
  if i<33
    jmp fwd ncommand
  end if
  i++
end do

ncommand:

'skipspace
do
  a=b[ i]
  if a=0
    jmp fwd nparam
  elseif a>32
    exit do
  end if
  i++
end do

'exclude quotes
if b[ i]=34
  i++
  sys j=i+1
  do
    if b[j]=34
      b[j]=0
      exit do
    end if
    j++
  end do
end if

'array commands:

readnextparam:

do
  a=b[ i]
  if a=0
    jmp fwd nparam
  elseif a>32
    exit do
  end if
  i++
end do

argc++
v[argc]=@z+i-1
do
  a=b[ i]
  if a=0 then jmp fwd nparam
  if a<33
    b[ i]=0
    i++
    exit do
  end if
  i++
end do
jmp readnextparam

nparam:

! main(sys c,v) as sys
main(argc,v)

'TEST
'====

function main(sys argc,argv) as sys
sys j
for j=1 to argc
   print cast char* argv[j]
next
end function


Charles

bugmagnet

  • Guest
Re: Access to argv and argc?
« Reply #2 on: April 21, 2013, 05:17:13 AM »
Sorry, Charles, I should have read the history of Oxygen Basic before making that request. I had mistakenly thought that OB was developed in C and that exposing argv and argc would be, if not easy, at least not too challenging. Now I discover that it's got its root in assembler and FBC.

Apologetically,
Bruce/Bugmagnet

Charles Pegge

  • Guest
Re: Access to argv and argc?
« Reply #3 on: April 21, 2013, 07:14:56 AM »
Not at all, Bruce. I thought it was a good idea to support a C-style entry function. I am sure it will be useful for console and server apps, since parsing the command-line is always a chore.

Charles

bugmagnet

  • Guest
Re: Access to argv and argc?
« Reply #4 on: April 21, 2013, 05:26:52 PM »
since parsing the command-line is always a chore.

Indeed it is!

Thank you for making it easier.

Bugmagnet