rem run a binary, possibly with redirection to a file
rem usage: binrun /c program [args] [>|>> file]
rem this is for fast shelling without cmd overhead.
rem if the named binary can't be found or the command line
rem contains pipes, odd redirection or anything else it
rem doesn't understand then it passes the command to
rem the stock %windir%\system32\cmd.exe shell.
rem WTN 11/1/09 Public Domain.

#include "crt/stdio.bi"
#include "file.bi"

dim as string comline,tempstr,pathline
dim as string binname,arguments,redirfile,binfile
dim redirmode as integer '0=none 1=redirect 2=append
dim as integer tempint,foundfile,candoit

'separate binary name, arguments and redirection...
comline=command
tempstr=left(comline,2)
'check for things this can't parse...
if tempstr <> "/c" or tempstr <> "/C" then candoit=1
tempint=instr(comline,"|")+instr(comline,"1>")+instr(comline,"2>")
if tempint then candoit=0
comline=ltrim(rtrim(mid(comline,3)))
if comline = "" then candoit=0
if candoit then
 tempint=instr(comline," ")
 if tempint=0 then
  binname=comline
 else
  binname=left(comline,tempint-1)
  arguments=mid(comline,tempint+1)
  tempint=instr(arguments,">")
  if tempint then
   tempstr=arguments
   arguments=rtrim(left(tempstr,tempint-1))
   redirfile=ltrim(mid(tempstr,tempint+1))
   redirmode=1
   if left(redirfile,1)=">" then
    redirfile=ltrim(mid(redirfile,2))
    redirmode=2
   end if
   if redirfile="" then
    candoit=0
   end if
  end if
 end if
 
 if candoit then
  'search for the specified binname...
  if fileexists(binname) then
   binfile=binname
   foundfile=1
  else
   binfile=binname+".com"
   if fileexists(binfile) then
    foundfile=1
   else
    binfile=binname+".exe"
    if fileexists(binfile) then
     foundfile=1
    else
     pathline=environ("PATH")
     if pathline <> "" then
      pathline=pathline+"; " 'easier parsing
      tempint=instr(pathline,";")
      do
       tempstr=ltrim(rtrim(left(pathline,tempint-1)))
       pathline=mid(pathline,tempint+1)
       binfile=tempstr+"\"+binname
       if fileexists(binfile) then
        foundfile=1
       else
        binfile=tempstr+"\"+binname+".com"
        if fileexists(binfile) then
         foundfile=1
        else
         binfile=tempstr+"\"+binname+".exe"
         if fileexists(binfile) then
          foundfile=1
         end if
        end if
       end if
       tempint=instr(pathline,";")
      loop until foundfile or tempint=0
     end if 'gotta search path
    end if 'binname.exe exists
   end if 'binname.com exists
  end if 'binname exists
 end if 'can separate args
end if 'can attempt to do it

'if not found or not understood hand it off to cmd.exe
if foundfile=0 then
 exec(environ("windir")+"\SYSTEM32\CMD.EXE",command)
 system
end if

'process redirection to a file...
if redirmode then
 if redirmode=1 then
  open redirfile for output as #1
 else
  open redirfile for append as #1
 end if
 freopen(redirfile,"a",stdout)
end if
'run the program...
exec(binfile,arguments)
'if redirected undirect...
if redirmode then
 freopen ("con:","w",stdout)
 close #1
end if

'that's it, return to caller...
system
