Author Topic: ScriptBasic O2h Embedding  (Read 26914 times)

0 Members and 2 Guests are viewing this topic.

Charles Pegge

  • Guest
Re: ScriptBasic O2h Embedding
« Reply #60 on: May 22, 2011, 09:49:53 PM »
Yes I see.

Here is some console code for OxygenBasic/tools/toolSrc. It shows how to setup simple console i/o.

I think you need to make a call to kernel32.dll

  %STD_OUTPUT_HANDLE = -11
  ConsOut=GetStdHandle (%STD_OUTPUT_HANDLE)

Code: [Select]

  library "KERNEL32.DLL"
  declare FUNCTION AllocConsole    ALIAS "AllocConsole" () AS LONG
  declare FUNCTION GetCommandLine  ALIAS "GetCommandLineA" () AS DWORD
  declare FUNCTION GetStdHandle    ALIAS "GetStdHandle" (BYVAL handle AS DWORD) AS DWORD
  declare FUNCTION WriteConsole    ALIAS "WriteConsoleA" (BYVAL hConsoleOutput AS DWORD, lpBuffer AS ASCIIZ, BYVAL nNumberOfCharsToWrite AS LONG, lpNumberOfCharsWritten AS LONG, BYVAL lpReserved AS LONG) AS LONG
  declare FUNCTION ReadConsole     ALIAS "ReadConsoleA" (BYVAL hConsoleInput AS DWORD, BYVAL lpBuffer AS DWORD, BYVAL nNumberOfCharsToRead AS LONG, lpNumberOfCharsRead AS LONG, pInputControl AS ANY) AS LONG
  declare FUNCTION SetConsoleTitle ALIAS "SetConsoleTitleA" (lpConsoleTitle AS ASCIIZ) AS LONG
  library ""


  %STD_INPUT_HANDLE  = -10
  %STD_OUTPUT_HANDLE = -11
  %STD_ERROR_HANDLE  = -12


  AllocConsole

  dim as long consIn,consOut,consErr

  ConsIn =GetStdHandle (%STD_INPUT_HANDLE)
  ConsOut=GetStdHandle (%STD_OUTPUT_HANDLE)
  ConsErr=GetStdHandle (%STD_ERROR_HANDLE)

  dim as string bufin
  dim as long buflen,bufrit

  dim as string tab,cr,qu

  tab=chr 9
  qu=chr 34
  cr=chr(13)+chr(10)

  bufin=nuls 1000


  SetConsoleTitle "Oxygen PE SPY"

  '---------------------------
  function output(bufout as string)
  '===========================
    buflen=len bufout
    WriteConsole ConsOut,bufout,buflen,bufrit,0
  end function

  '-------------------------------------
  function input(s as string) as string
  '=====================================
    output s
    ReadConsole consin,*bufin,100,bufrit,0
    function=left bufin,bufrit
  end function


  '-------------------------------
  function commandline() as string
  '=============================== 
    dim byref z as zstring 
    &z=GetCommandLine
    function=z
  end function

  '------------------------------------------
  function stripquotes(s as string) as string
  '==========================================
    dim as long a
    a=asc(s,1)
    if a=34 then
      a=instr 2,s,qu
      s=mid s,2,a-2
    end if
    function=s
  end function


Charles

JRS

  • Guest
Re: ScriptBasic O2h Embedding
« Reply #61 on: May 22, 2011, 10:10:59 PM »
That returns 79 which may be the handle but I need the function pointer to STDOUT.


Charles Pegge

  • Guest
Re: ScriptBasic O2h Embedding
« Reply #62 on: May 22, 2011, 10:35:21 PM »
These kernel32.dll calls will get you the address of WriteConsole


a=LoadLibraryA("kernal32.dll")
p=GetProcAddress(a,"WriteConsoleA")


but I don't know whether this is the function printf expects to use

List of console functions:

http://msdn.microsoft.com/en-us/library/ms682073(v=vs.85).aspx

You need to add 'A' to the names where there is a choice of Ansi or 'W' for wide character.


Code: [Select]
 '---------------------------
  function output(bufout as string)
  '===========================
    buflen=len bufout
    WriteConsole ConsOut,bufout,buflen,bufrit,0
  end function

Charles
« Last Edit: May 22, 2011, 10:40:38 PM by Charles Pegge »

JRS

  • Guest
Re: ScriptBasic O2h Embedding
« Reply #63 on: May 22, 2011, 11:04:17 PM »
Code: [Select]
DECLARE SUB DLL ALIAS "dyc" LIB "dyc"

CONST GSL_SUCCESS  = 0

a = DLL("ms,l,kernel32.dll,LoadLibrary,z","kernel32.dll")
p = DLL("ms,l,kernel32.dll,GetProcAddress,lz",a,"WriteConsoleA")

PRINT "All subsets of {0,1,2,3} by size:\n"

FOR i = 0 TO 4
  c = DLL("ms,l,libgsl-0.dll,gsl_combination_calloc,ll", 4, i)

  DO
    PRINT "{"
    DLL("ms,i,libgsl-0.dll,gsl_combination_fprintf,llz", p, c, " %u")
    PRINT " }\n"
  LOOP WHILE DLL("ms,i,libgsl-0.dll,gsl_combination_next,l", c) = GSL_SUCCESS
  DLL("ms,i,libgsl-0.dll,gsl_combination_free,l", c)
NEXT

I'm still getting the exception error.   ???
I added a debug print after getting p and it looks like an address.

C:\gsl\test>scriba gslcombo.sb
2072191600

C:\gsl\test>

int gsl_combination_fprintf (FILE * stream, const gsl_combination * c, const char * format)

I wonder if it might be easier for you to get this example working in O2 first rather then me hacking away with DYC.

Quote
UNIX defines 3 predefined streams (in stdio.h):

   stdin,  stdout,  stderr
« Last Edit: May 22, 2011, 11:38:22 PM by JRS »

Charles Pegge

  • Guest
Re: ScriptBasic O2h Embedding
« Reply #64 on: May 22, 2011, 11:42:16 PM »

Hmm.. I did not see Microsoft on the platform list. - an odd omission, and I don't understand why it needs you to tell it anything about the console.

http://www.gnu.org/software/gsl/

Perhaps a C programmer would be able sort the console i/o for you in the source code or compiler options.

Charles


JRS

  • Guest
Re: ScriptBasic O2h Embedding
« Reply #65 on: May 23, 2011, 12:05:55 AM »
The GSL library was compiled using MinGW. I'll take a look how Peter handles STDOUT in SB for Unix & Windows platforms. Maybe that will shed some light and what is needed.

Here is the site I got the libgsl for Windows from.

http://junjie8422.cn/archives/13

It shows how to build the library under Windows and provides a pre-compiled library if you you don't want to do it yourself.


« Last Edit: May 23, 2011, 08:47:53 AM by JRS »

JRS

  • Guest
Re: ScriptBasic O2h Embedding
« Reply #66 on: May 25, 2011, 06:03:20 PM »
Charles,

I created a ScriptBasic Linux 64 bit extension module to wrap the GSL functions. I gave up on GTK-Server to script GSL.

Code: [Select]
/*
   GNU Scientific Library
   Based on GSL 1.15
   Interface By: John Spikowski
*/

#include <stdio.h>
#include "../../basext.h"
#include <gsl/gsl_sf_bessel.h>
      
besVERSION_NEGOTIATE
    return (int)INTERFACE_VERSION;
besEND

besSUB_START
besEND

besSUB_FINISH
besEND

besFUNCTION(sf_bessel_J0)
  VARIABLE Argument;

  Argument = besARGUMENT(1);
  besDEREFERENCE(Argument);

  besRETURN_DOUBLE(gsl_sf_bessel_J0 (DOUBLEVALUE(Argument)));
besEND

Code: [Select]
DECLARE SUB besselJ0 ALIAS "sf_bessel_J0" LIB "gsl"

PRINT FORMAT("J0(%g) = %.48g", 5, besselJ0(5.0)),"\n"

jrs@laptop:~/sb/test/gsl$ scriba bj0.sb
J0(5) = -0.177596771314338264247112419980112463235855102539
jrs@laptop:~/sb/test/gsl$

Peter Verhas had the C programmer in mind when he created the extension module macro API interface.
« Last Edit: May 25, 2011, 06:15:29 PM by JRS »

Charles Pegge

  • Guest
Re: ScriptBasic O2h Embedding
« Reply #67 on: May 25, 2011, 07:00:04 PM »

Hi John,

For Windows/Wine I see there is a ready to use libGSL.dll here

http://gnuwin32.sourceforge.net/packages/gsl.htm

gsl-1.8-bin.zip

I took a peek inside the DLL. The names are free of mangling so it looks simple to use.


Charles

JRS

  • Guest
Re: ScriptBasic O2h Embedding
« Reply #68 on: May 25, 2011, 08:08:19 PM »
The same extension module I'm creating under Linux should compile under Windows as a DLL.

I can now fully appreciate the work you put into cracking the SB macros so they can be called from Oxygen.

Latest GSL version (May 6th, 2011) gsl-1.15.tar.gz

« Last Edit: May 25, 2011, 08:43:18 PM by JRS »

Charles Pegge

  • Guest
Re: ScriptBasic O2h Embedding
« Reply #69 on: May 26, 2011, 02:31:22 AM »

Would an enhanced DYC be worth developing John?

I would not advocate it for building Windows GUIs but for less complex interfacing it could fill the gap.

Charles

JRS

  • Guest
Re: ScriptBasic O2h Embedding
« Reply #70 on: May 26, 2011, 08:40:35 AM »
Quote
Would an enhanced DYC be worth developing John?

I think the ability to script an external library or system API without having to create a extension module is key feature of DYC. A VARPTR() function and ability to access structures given their pointer would be a big plus.


Charles Pegge

  • Guest
Re: ScriptBasic O2h Embedding
« Reply #71 on: May 26, 2011, 09:09:01 AM »

For passing and returning structures, I think we should use the join and split functions and make text the principle form of data transfer.

The structures will need to carry a type-signature just like the parameter signature in a dyc call.

For example, a windows rectangle (rect) would look something like this:

rect1="100,200,640,480,LLLL"

(Placing the signature as the final term will make array indexation to the values a little easier.)

Charles

JRS

  • Guest
Re: ScriptBasic O2h Embedding
« Reply #72 on: May 26, 2011, 09:31:57 AM »
I like it!

I plan on creating an argument validation function for extension modules that check the number of passed arguments and they are of the right type for the called function.

argerr = besArgVal("D")

This function would test to make sure only one argument should be passed and as a number in a floating point format. The function will return a non-zero if invalid arguments are passed and would tell scriba an error occurred.

JRS

  • Guest
Re: ScriptBasic O2h Embedding
« Reply #73 on: May 27, 2011, 11:02:27 AM »
Charles,

I'm trying to add this function to the SB GSL extension module and I'm getting a segment fault when accessing the function.

Code: [Select]
besFUNCTION(_frexp)
  VARIABLE Argument;

  Argument = besARGUMENT(1);
  besDEREFERENCE(Argument);
  double x = DOUBLEVALUE(Argument);
  Argument = besARGUMENT(2);
  besDEREFERENCE(Argument);
  int * y = LONGVALUE(Argument);

  besRETURN_DOUBLE(gsl_frexp(x, y));
besEND

Quote
Function: double gsl_frexp (double x, int * e)

This function splits the number x into its normalized fraction f and exponent e, such that x = f * 2^e and 0.5 <= f < 1. The function returns f and stores the exponent in e. If x is zero, both f and e are set to zero. This function provides an alternative to the standard math function frexp(x, e).

Code: [Select]
DECLARE SUB frexp ALIAS "_frexp" LIB "gsl"

e = 0
PRINT FORMAT("%g",frexp(12.5,e)),"\n"
PRINT e,"\n"


JRS

  • Guest
Re: ScriptBasic O2h Embedding
« Reply #74 on: May 27, 2011, 12:24:40 PM »
I figured it out.

Code: [Select]
besFUNCTION(_frexp)
  VARIABLE Argument;
  LEFTVALUE Lval;
  unsigned long __refcount_;

  Argument = besARGUMENT(1);
  besDEREFERENCE(Argument);
  double x = DOUBLEVALUE(Argument);
  Argument = besARGUMENT(2);
  besLEFTVALUE(Argument,Lval);
  besRELEASE(*Lval);
  *Lval = besNEWLONG;

  besRETURN_DOUBLE(gsl_frexp(x, *Lval));
besEND

Code: [Select]
DECLARE SUB frexp ALIAS "_frexp" LIB "gsl"

x = 16.4
fraction = frexp(x, e)

PRINT FORMAT("%g",fraction),"\n"
PRINT e,"\n"

jrs@laptop:~/sb/test/gsl$ scriba emath.sb
0.5125
5

Quote from: LEFTVALUE
A left value is a special expression that a value can be assigned, and therefore they usually stand on the left side of the assignment operator. That is the reason for the name.

I would have gone with BYREFVALUE instead. The __refcount_ should have been defined in the basext.h file for the LEFTVALUE macro. I can see a bit more macro polishing still needing to be done.
« Last Edit: May 27, 2011, 04:04:51 PM by JRS »