  '18:19 26/08/2018
  '10:53 02/06/2019

  '------------
  'SELECTED API
  '============
  '
    '#include once "windows.bi"
    '#include once "win/ole2.bi"
    '
    #define STD_INPUT_HANDLE    -10
    #define STD_OUTPUT_HANDLE   -11
    #define STD_ERROR_HANDLE    -12 
    '
    #define DLL_PROCESS_ATTACH   1
    #define DLL_THREAD_ATTACH    2
    #define DLL_THREAD_DETACH    3
    #define DLL_PROCESS_DETACH   0
    #define DLL_PROCESS_VERIFIER 4
    '
    #define sys Long
    '
    Type HWND As sys
    Type HMODULE As sys
    Type HINSTANCE As sys
    Type BOOL As sys
    Type UINT As sys
    Type DWORD As sys
    Type BSTR As Any Ptr
    Type FARPROC As Any Ptr
    Type PVOID As Any Ptr
    Type PCVOID As Any Ptr
    Type PLONG As Long Ptr
    Type PDWORD As dword Ptr
    Type LPCSTR As Zstring Ptr
    Type LPSECURITY_ATTRIBUTES As Any Ptr
    Type LPOVERLAPPED As Any Ptr
    Type HANDLE As sys
    #define iostring bstr
    '
  Extern "windows" lib "kernel32"
    
    'AllocConsole               () as sys
    'AttachConsole              (sys pp) as sys
    'FreeConsole                () as sys
    'GetStdHandle               (sys h) as sys
    'WriteConsole    alias "WriteConsoleA"    (sys hOut, lpBuffer, nNumberOfCharsToWrite, *lpNumberOfCharsWritten, lpReserved) as sys
    Declare Function AllocConsole alias "AllocConsole" ( ) AS DWORD
    Declare Function AttachConsole alias "AttachConsole" (AS LONG) AS DWORD
    Declare Function FreeConsole alias "FreeConsole" ( ) AS DWORD
    'Declare Function Sleep alias "Sleep" (AS DWORD) AS DWORD
    Declare Function GetStdHandle alias "GetStdHandle" ( _
      AS HANDLE ) AS DWORD
   Declare Function WriteConsole alias "WriteConsoleA" ( _
      AS HANDLE, _
      AS ZSTRING PTR,  _
      AS LONG,   _
      AS PLONG,  _
      AS DWORD ) AS DWORD
   Declare Function WriteFile alias "WriteFile" ( _
      AS HANDLE, _
      AS ZSTRING PTR,  _
      AS LONG,   _
      AS PLONG,  _
      AS DWORD ) AS DWORD
    Declare Function ReadFile ( _
      As HANDLE, _
      As PVOID,  _
      As DWORD,  _
      As PDWORD,  _
      As LPOVERLAPPED) As BOOL
    'Declare Function WriteFile ( _
    '  Byval As HANDLE, _
    '  As PCVOID,  _
    '  As DWORD,  _
    '  As PDWORD, _
    '  As LPOVERLAPPED) As BOOL
    Declare Function LoadLibrary Alias "LoadLibraryA" ( _
      As LPCSTR) As HINSTANCE
    Declare Function CreateFile Alias "CreateFileA" ( _
      As LPCSTR, _
      As DWORD, _
      As DWORD, _
      As LPSECURITY_ATTRIBUTES, _
      As DWORD, _
      As DWORD, _
      As HANDLE) As HANDLE
    Declare Function FreeLibrary(Byval As HMODULE) As BOOL
    Declare Function GetProcAddress( _
      As HINSTANCE, _
      As LPCSTR) As FARPROC
    Declare Function GetFileSize ( _
      As HANDLE, _
      As PDWORD) As DWORD
    Declare Function SetFilePointer( _
      As HANDLE, _
      As Long, _
      As PLONG, _
      As DWORD) As DWORD
    Declare Function CloseHandle( _
      As HANDLE) As BOOL
  End Extern

  Extern "windows" lib "oleaut32"
    Declare Function SysAllocStringByteLen ( _
      As Zstring Ptr, _
      As Uinteger) As BSTR
    Declare Sub SysFreeString (Byval As BSTR)
  End Extern

  Extern "windows" lib "user32"
    Declare Function MessageBox Alias "MessageBoxA" ( _
      As HWND, _
      As LPCSTR, _
      As LPCSTR, _
      As UINT) As Integer
    Declare Function MessageBoxW Alias "MessageBoxW" ( _
      As HWND, _
      As LPCSTR, _
      As LPCSTR, _
      As UINT) As Integer
  End Extern
    #define AllocString(p,le) SysAllocStringByteLen(p,le)
    #define FreeString(p) SysFreeString(p)
    #define Library(s) LoadLibrary(s)
    #define FreeLibr(a) FreeLibrary(a)
    #define ProcAddr(a,b) GetProcAddress(a,b)

  extern "windows-MS" lib "oxygen"

  declare sub      o2_mode  (byval m as long)
  declare function o2_abst  (byval p as zstring ptr) as zstring ptr
  declare sub      o2_basic (byval p as zstring ptr)
  declare function o2_exec  (byval addr as long) as long
  declare function o2_buf   (byval n as long) as long
  declare function o2_errno () as long
  declare function o2_error () as zstring ptr
  declare function o2_len   () as long
  declare function o2_prep  (byval p as zstring ptr) as zstring ptr
  declare function o2_view  (byval p as zstring ptr) as zstring ptr

  end extern

  extern "windows" lib "kernel32"

  declare FUNCTION GetCommandLine ALIAS "GetCommandLineA" () AS zstring ptr

  end extern

  const as string qu=chr(34)
  const as string tb=chr(9)
  const as string cr=chr(13)+chr(10)

  dim shared as string prs
  dim shared as string cf,bfname

  '--------------------------
  sub pri(byval s as string)
  '==========================
  prs+=s+cr
 'print s+cr
  end sub

  sub ExtensName(s as string, byref mnm as string, byref xnm as string)
  '===================================================================
    dim as long i,dt,qt
    i=1
    if asc(s,i)=34 then qt=1 : i=2
    do
      select case asc(s,i)
      case 0       : exit do
      case 1 to 32 : if qt=0 then exit do
      case 34      : exit do
      case 46      : dt=i
      case 47      : dt=0
      case 92      : dt=0
      end select
      i+=1
    loop
    '
    if dt then
      mnm=mid(s,qt+1,dt-1-qt)
      xnm=mid(s,dt,i-dt)
    else
      mnm=mid(s,qt+1,i-1-qt)
    end if
    '
  end sub



  '====
  'main
  '====


  dim z as zstring ptr 
  z=GetCommandLine

  'print "Commandline: "+ *z

  
  dim as sys a,b,e,i,le,swa,swb,swc,swm
  dim as string n,s,t,u,mnm,xnm
  dim as zstring ptr er
  dim as any ptr p
  '
  n=ltrim(lcase(*z))

  '
  'SKIP COMMAND
  '============
  '
  a=asc(n)
  b=1
  if a=34 then
    b=instr(2,n,qu)
    b+=1
  end if
  '
  '
  'CHECK FOR PARAMETER FOLLOWING
  '=============================
  '
  a=instr(b,n," ")
  '
  if a=0 then
    n="" : goto nofile
  end if
  '
  n=mid(n,a+1)
  n=ltrim(n)
  ''
  '
  'SWITCHES
  '========
  do
    if asc(n)=45 then
      a=asc(n,2)
      b=3
      select case a
      case &h69 : swa=a        '-i  intermediate output
      case &h61 : swa=a        '-a  asm output
      case &h62 : swa=a        '-b  o2 output
      case &h63 : swc=a :      '-c  32bit compile
      case &h33 : swc=a : b+=1 '-32 32bit compile
      case &h36 : swc=a : b+=1 '-64 64bit compile
      case &h6d : swm=a        '-m do not show messagebox
      end select
      n=ltrim(mid(n,b))
    else
      exit do
    end if
  loop
  '
  '-------------
  nofile: 'check
  '=============
  '
  if len(n)=0 then
    t="compiler options:"+cr+_
      "<filename>       compile and execute directly in memory"+cr+_
      "-a  <filename>    list assembly code"+cr+_
      "-b  <filename>    list o2 machine script"+cr+_
      "-c  <filename>    compile to a 32bit executable"+cr+_
      "-32 <filename>    compile to a 32bit executable"+cr+_
      "-64 <filename>    compile to a 64bit executable"+cr+_
      "-i  <filename>    list intermediate code"+cr+_
      "-m                output to console"+cr+_
      ""
    pri t
    goto finish
  end if

  '
  'CHECK FOR .BAS EXTENSION NAME
  '=============================
  '
  '
  xnm=".o2bas"
  ExtensName(n,mnm,xnm)
  s=mnm+xnm
