Author Topic: SB2C  (Read 8912 times)

Support

  • Administrator
  • *****
  • Posts: 22
    • View Profile
SB2C
« on: August 29, 2011, 01:31:07 AM »
If anyone is interested in creating small footprint compiled (C) applications with ScriptBasic, here is how it's done. (Linux, Win & Mac 32/64 bit)

Note: Paths used in this example may need adjustment based on where you installed ScriptBasic and the Linux platform you're running under.

Example SB script.
Code: [Select]
for x=-5 to 5
  print x,"\n"
next

Creating the C wrapper of the above script.

scriba -Co testfor.c testfor.bas

Code: [Select]
/* FILE: testfor.c
   This file contains the binary code of a ScriptBasic program
   To run this file you have to compile it to object file and
   link it with scribast.lib or whatever the library code is
   called on your platform.
*/
unsigned long ulGlobalVariables=1;
unsigned long ulNodeCounter=20;
unsigned long ulStartNode=7;
unsigned long ulStringTableSize=10;
unsigned char szCommandArray[] ={
0xB8, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x0A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x76, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0xCF, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x0A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x0A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x4D, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0D, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x42, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00 };
char szStringTable[]={
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0A, 0x00,
0x00 };
#ifdef WIN32
main(int argc, char *argv[]){stndlone(argc,argv);}
#else
char **_environ;
main(int argc, char *argv[], char *env[]){stndlone(argc,argv,env);}
#endif
/*End of file testfor.c */

If you haven't created the sb.a runtime interface, here is how it's done.

cd /home/jrs/sb/scriptbasic/bin/obj/
ar rcs sb.a stndlone.o report.o myalloc.o errcodes.o dynlolib.o

chmod 755 sb.a
sudo cp sb.a /usr/lib

Make sure your ScriptBasic runtime shared object is present.

$ ls -l /usr/lib/libscriba*
-rwxr-xr-x 1 root root 1093954 2011-05-03 22:00 /usr/lib/libscriba.a
-rwxr-xr-x 1 root root  844476 2011-05-03 21:14 /usr/lib/libscriba.so

Looks like we are ready to compile our script.

$ gcc -Os testfor.c -I /home/jrs/sb/scriptbasic/ -ldl /usr/lib/sb.a -lscriba -lm -lpthread -o testfor

$ ./testfor
-5
-4
-3
-2
-1
0
1
2
3
4
5

$ ls -l testfor
-rwxr-xr-x 1 jrs jrs 29110 2011-08-29 00:17 testfor


This is an example of using the MySQL extension module and creating a standalone executable.

Code: [Select]
' MySQL Test Program

INCLUDE mysql.bas

dbh = mysql::RealConnect("host","user","password","dbname")

mysql::query(dbh,"SELECT * FROM test")

WHILE mysql::FetchHash(dbh,column)

PRINTNL
PRINT column{"ID"},"\n"
PRINT column{"NAME"},"\n"
PRINTNL

WEND

PRINTNL
PRINT "The database handle is: ",dbh,"\n"
PRINT "Affected rows by SELECT: ",mysql::AffectedRows(dbh),"\n"
PRINT "Character set name is: ",mysql::CharacterSetName(dbh),"\n"
PRINT "Last error is: ",mysql::ErrorMessage(dbh),"\n"
PRINT "Client info is: ",mysql::GetClientInfo(),"\n"
PRINT "Host info is: ",mysql::GetHostInfo(dbh),"\n"
PRINT "Proto info is: ",mysql::GetProtoInfo(dbh),"\n"
PRINT "Server info is: ",mysql::GetServerInfo(dbh),"\n"
PRINT "PING result: ",mysql::Ping(dbh),"\n"
PRINT "Thread ID: ",mysql::ThreadId(dbh),"\n"
PRINT "Status is: ",mysql::Stat(dbh),"\n"

mysql::Close(dbh)

END

$ scriba -Co tstsql.c testmysql.sb
$ gcc -Os tstsql.c -I /home/jrs/sb/scriptbasic/ -ldl /usr/lib/sb.a -lscriba -lm -lpthread -o tstsql
$ ./tstsql

1
John Spikowski


The database handle is: 1
Affected rows by SELECT: 1
Character set name is: latin1
Last error is:
Client info is: 5.1.54
Host info is: Localhost via UNIX socket
Proto info is: 10
Server info is: 5.1.54-1ubuntu4
PING result: -1
Thread ID: 0
Status is: Uptime: 28068  Threads: 1  Questions: 137  Slow queries: 0  Opens: 147  Flush tables: 1  Open tables: 35  Queries per second avg: 0.4

$ ls -l tstsql
-rwxr-xr-x 1 jrs jrs 35797 2011-08-29 02:54 tstsql

Compiling your scripts offers some distinct advantages over the scriba command line interpreter wrapper of the SBAPI.

  • Pre-compiled (tokenized) script
  • Shared object runtime
  • Static linking of extension modules rather than the default dynamic linking if desired

If you don't want to go through the trouble of converting the script to C and compiling with gcc, you can use the scriba command line option to create a standalone executable. This method attaches a tokenized (binay) version of the script to the end of a copy of scriba. Scriba always checks to see if there is a binary script attached before running the script name passed on the command line.

/usr/bin/scriba -Eo tstsql testmysql.sb


« Last Edit: August 29, 2011, 02:12:31 PM by support »

Support

  • Administrator
  • *****
  • Posts: 22
    • View Profile
Re: SB2C
« Reply #1 on: October 03, 2011, 06:18:26 AM »
Thanks Ron for the heads up. Are you using the 32 or 64 bit version of ScriptBasic?

I'll post both here until I figure out why the .deb packages don't include it.

Update

I have attached the 32 and 64  bit versions of libscriba.so in a tar Z format.
« Last Edit: October 03, 2011, 06:47:57 PM by support »