Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - Support

Pages: 1 ... 6 7 [8] 9 10 ... 59
106
What's New / SBx 3 Forms
« on: May 27, 2015, 11:19:46 PM »
As it turns out, you really don't need threading to achieve multiple window support. As I see it threading of a IUP dialog would be a special use case. It's good to know it can be done.

Code: Script BASIC
  1. ' SBx_buttons Example (3 Form Version)
  2.  
  3. IMPORT iup.bas
  4. IMPORT sbt.inc
  5. IMPORT "SBx"
  6.  
  7. ' Form 1 Callback Routines
  8. SUB frm1_btn1_clicked
  9.   PRINT "Form 1 Button 1 Pressed\n"
  10.   PRINT "Which Mouse Button: ",CHR(Iup::GetBtnPressed()),"\n"
  11.   PRINT "Button Up/Dn State: ",Iup::GetBtnState(),"\n"
  12. END SUB
  13.  
  14. SUB frm1_btn2_clicked
  15.   PRINT "Form 1 Button 2 Pressed\n"
  16. END SUB
  17.  
  18. SUB frm1_btn3_clicked
  19.   PRINT "Form 1 Button 3 Pressed\n"
  20. END SUB
  21.  
  22. ' Form 2  Callback Routines
  23. SUB frm2_btn1_clicked
  24.   PRINT "Form 2 Button 1 Pressed\n"
  25.   PRINT "Which Mouse Button: ",CHR(Iup::GetBtnPressed()),"\n"
  26.   PRINT "Button Up/Dn State: ",Iup::GetBtnState(),"\n"
  27. END SUB
  28.  
  29. SUB frm2_btn2_clicked
  30.   PRINT "Form 2 Button 2 Pressed\n"
  31. END SUB
  32.  
  33. SUB frm2_btn3_clicked
  34.   PRINT "Form 2 Button 3 Pressed\n"
  35. END SUB
  36.  
  37. ' Form 3 Callback Routines
  38. SUB frm3_btn1_clicked
  39.   PRINT "Form 3 Button 1 Pressed\n"
  40.   PRINT "Which Mouse Button: ",CHR(Iup::GetBtnPressed()),"\n"
  41.   PRINT "Button Up/Dn State: ",Iup::GetBtnState(),"\n"
  42. END SUB
  43.  
  44. SUB frm3_btn2_clicked
  45.   PRINT "Form 3 Button 2 Pressed\n"
  46. END SUB
  47.  
  48. SUB frm3_btn3_clicked
  49.   PRINT "Form 3 Button 3 Pressed\n"
  50. END SUB
  51.  
  52. SUB win_exit
  53.   ' Good-Bye
  54. END SUB
  55.  
  56. Iup::Open()
  57.  
  58. ' Form 1 Dialog
  59. win1 = DIALOG()
  60. SETPROPERTIES(win1, "TITLE=\"SBx Form 1\", SIZE=300x")
  61. horzbox1 = HBOX()
  62. SETPROPERTIES horzbox1, "GAP=5"
  63. btn1_1 = BUTTON()
  64. SETPROPERTIES btn1_1, "TITLE=\"Button 1\", EXPAND=HORIZONTAL"
  65. btn1_2 = BUTTON()
  66. SETPROPERTIES btn1_2, "TITLE=\"Button 2\", EXPAND=HORIZONTAL"
  67. btn1_3 = BUTTON()
  68. SETPROPERTIES btn1_3, "TITLE=\"Button 3\", EXPAND=HORIZONTAL"
  69. APPEND horzbox1, btn1_1
  70. APPEND horzbox1, btn1_2
  71. APPEND horzbox1, btn1_3
  72. APPEND win1, horzbox1
  73. Iup::SetCallback win1, "CLOSE_CB", ADDRESS(win_exit())
  74. Iup::SetCallback btn1_1, "BUTTON_CB", ADDRESS(frm1_btn1_clicked())
  75. Iup::SetCallback btn1_2, "ACTION", ADDRESS(frm1_btn2_clicked())
  76. Iup::SetCallback btn1_3, "ACTION", ADDRESS(frm1_btn3_clicked())
  77. Iup::ShowXY(win1,500,200)
  78.  
  79.  
  80.  
  81. ' Form 2 Dialog
  82. win2 = DIALOG()
  83. SETPROPERTIES win2, "TITLE=\"SBx Form 2\", SIZE=300x"
  84. horzbox2 = HBOX()
  85. SETPROPERTIES horzbox2, "GAP=5"
  86. btn2_1 = BUTTON()
  87. SETPROPERTIES btn2_1, "TITLE=\"Button 1\", EXPAND=HORIZONTAL"
  88. btn2_2 = BUTTON()
  89. SETPROPERTIES btn2_2, "TITLE=\"Button 2\", EXPAND=HORIZONTAL"
  90. btn2_3 = BUTTON()
  91. SETPROPERTIES btn2_3, "TITLE=\"Button 3\", EXPAND=HORIZONTAL"
  92. APPEND horzbox2, btn2_1
  93. APPEND horzbox2, btn2_2
  94. APPEND horzbox2, btn2_3
  95. APPEND win2, horzbox2
  96. Iup::SetCallback win2, "CLOSE_CB", ADDRESS(win_exit())
  97. Iup::SetCallback btn2_1, "BUTTON_CB", ADDRESS(frm2_btn1_clicked())
  98. Iup::SetCallback btn2_2, "ACTION", ADDRESS(frm2_btn2_clicked())
  99. Iup::SetCallback btn2_3, "ACTION", ADDRESS(frm2_btn3_clicked())
  100. Iup::ShowXY(win2,500,400)
  101.  
  102. ' Form 3 Dialog
  103. win3 = DIALOG()
  104. SETPROPERTIES win3, "TITLE=\"SBx Form 3\", SIZE=300x"
  105. horzbox3 = HBOX()
  106. SETPROPERTIES horzbox3, "GAP=5"
  107. btn3_1 = BUTTON()
  108. SETPROPERTIES btn3_1, "TITLE=\"Button 1\", EXPAND=HORIZONTAL"
  109. btn3_2 = BUTTON()
  110. SETPROPERTIES btn3_2, "TITLE=\"Button 2\", EXPAND=HORIZONTAL"
  111. btn3_3 = BUTTON()
  112. SETPROPERTIES btn3_3, "TITLE=\"Button 3\", EXPAND=HORIZONTAL"
  113. APPEND horzbox3, btn3_1
  114. APPEND horzbox3, btn3_2
  115. APPEND horzbox3, btn3_3
  116. APPEND win3, horzbox3
  117. Iup::SetCallback win3, "CLOSE_CB", ADDRESS(win_exit())
  118. Iup::SetCallback btn3_1, "BUTTON_CB", ADDRESS(frm3_btn1_clicked())
  119. Iup::SetCallback btn3_2, "ACTION", ADDRESS(frm3_btn2_clicked())
  120. Iup::SetCallback btn3_3, "ACTION", ADDRESS(frm3_btn3_clicked())
  121. Iup::ShowXY(win3,500,600)
  122.  
  123.  
  124.  
  125. ' Event Loop
  126. windows = 3
  127.  
  128. WHILE windows
  129.   Iup::LoopStep()
  130.   this_event = Iup::GetEvent()
  131.   this_event = Iup::BB_HTA(this_event)
  132.   IF this_event = event{this_event}[0] THEN
  133.     ICALL event{this_event}[1]
  134.     IF Iup::GetActionName() = "CLOSE_CB" THEN windows -= 1
  135.   END IF  
  136.   SB_msSleep(250)
  137. WEND
  138.  
  139. Iup::Close
  140.  

iup.bas - I changed the Iup::SetCallback() to create the event array in the main namespace.
Code: Script BASIC
  1. FUNCTION SetCallback(ih, aname, fname)
  2.   main::event{BB_HTA(ih)}[0] = BB_HTA(ih)
  3.   main::event{BB_HTA(ih)}[1] = fname
  4.   SetCallback = __SetCallback(ih, aname)
  5. END FUNCTION
  6.  

107
What's New / IUP Threaded - Linux 64 bit - SBT 3*3
« on: May 25, 2015, 07:20:52 PM »
The solution to my unstable IUP start-up issues were resolved with creating an IUP dialog in the parent script before creating threaded children dialogs.  I moved the Iup::MainLoop and Iup::GetThreadCallback routines into the main script from the IUP extension module. At this point everything is working as expected and I couldn't be happier.

Moral: How can you have well behaved children if you don't have a mature parent in charge?  ::)

SBx_Main
Code: Script BASIC
  1. ' SBT IUP Theaded Example
  2.  
  3. IMPORT mt.bas
  4. IMPORT sbt.inc
  5. IMPORT iup.bas
  6. IMPORT "SBx"
  7.  
  8. Iup::Open()
  9.  
  10. SUB SB_Wait(mtvar)
  11.   WHILE mt::GetVariable(mtvar) <> "OK"
  12.     SB_msSleep(5000)
  13.   WEND
  14. END SUB
  15.  
  16. SUB btn1_clicked
  17.   PRINT "Main 0 Button 1 Pressed\n"
  18.   PRINT "Which Mouse Button: ",CHR(Iup::GetBtnPressed()),"\n"
  19.   PRINT "Button Up/Dn State: ",Iup::GetBtnState(),"\n"
  20. END SUB
  21.  
  22. SUB btn2_clicked
  23.   PRINT "Main 0 Button 2 Pressed\n"
  24. END SUB
  25.  
  26. SUB btn3_clicked
  27.   PRINT "Main 0 Button 3 Pressed\n"
  28. END SUB
  29.  
  30. SUB win_exit
  31.   ' Good-Bye
  32. END SUB
  33.  
  34. win = DIALOG()
  35. SETPROPERTIES win, "TITLE=\"SBx Main 0\", SIZE=300x"
  36. horzbox = HBOX()
  37. SETPROPERTIES horzbox, "GAP=5"
  38. btn1 = BUTTON()
  39. SETPROPERTIES btn1, "TITLE=\"Button 1\", EXPAND=HORIZONTAL"
  40. btn2 = BUTTON()
  41. SETPROPERTIES btn2, "TITLE=\"Button 2\", EXPAND=HORIZONTAL"
  42. btn3 = BUTTON()
  43. SETPROPERTIES btn3, "TITLE=\"Button 3\", EXPAND=HORIZONTAL"
  44. APPEND horzbox, btn1
  45. APPEND horzbox, btn2
  46. APPEND horzbox, btn3
  47. APPEND win, horzbox
  48. Iup::SetThreadCallback(win, "CLOSE_CB", ADDRESS(win_exit()), 0)
  49. Iup::SetThreadCallback(btn1, "BUTTON_CB", ADDRESS(btn1_clicked()), 0)
  50. Iup::SetThreadCallback(btn2, "ACTION", ADDRESS(btn2_clicked()), 0)
  51. Iup::SetThreadCallback(btn3, "ACTION", ADDRESS(btn3_clicked()), 0)
  52. SHOW win
  53.  
  54. ' Puppet Show
  55.  
  56. sb1 = SB_ThreadStart("SBx_T1",undef,"/etc/scriba/basic.conf")
  57. SB_Wait("sb1_loaded")
  58. sb1_rtn = SB_CallSubArgs(sb1, "main::main", sb1)
  59.  
  60. sb2 = SB_ThreadStart("SBx_T2",undef,"/etc/scriba/basic.conf")
  61. SB_Wait("sb2_loaded")
  62. sb2_rtn = SB_CallSubArgs(sb2, "main::main", sb2)
  63.  
  64. threads = 3
  65.  
  66. t_event = mt::GetVariable("Callback_Map")
  67. SPLITA t_event BY "\n" TO e_list
  68. FOR x = 0 TO UBOUND(e_list)
  69.   SPLITA e_list[x] BY "|" TO e_array
  70.   event{e_array[0]}[0] = e_array[0]
  71.   event{e_array[0]}[1] = e_array[1]
  72.   event{e_array[0]}[2] = e_array[2]
  73. NEXT
  74.  
  75. WHILE threads
  76.   Iup::LoopStep()
  77.   this_event = Iup::GetEvent()
  78.   hex_event = Iup::BB_HTA(this_event)
  79.   IF hex_event = event{hex_event}[0] THEN
  80.     IF event{hex_event}[2] = 0 THEN
  81.       ICALL event{hex_event}[1]
  82.     ELSE IF event{hex_event}[2] = 1 THEN
  83.       SB_CallSub(main::sb1, event{hex_event}[1])
  84.     ELSE IF event{hex_event}[2] = 2 THEN
  85.       SB_CallSub(main::sb2, event{hex_event}[1])
  86.     END IF
  87.     IF Iup::GetActionName() = "CLOSE_CB" THEN threads -= 1
  88.   END IF  
  89.   SB_msSleep(250)
  90. WEND
  91.  
  92. Iup::Close()
  93. SB_Destroy(sb2)
  94. SB_Destroy(sb1)
  95.  

SBx_T1 - T2 is the same
Code: Script BASIC
  1. ' SBx_buttons Example (Thread 1)
  2.  
  3. IMPORT mt.bas
  4. IMPORT iup.bas
  5. IMPORT "SBx"
  6.  
  7. SUB btn1_clicked
  8.   PRINT "Thread 1 Button 1 Pressed\n"
  9.   PRINT "Which Mouse Button: ",CHR(Iup::GetBtnPressed()),"\n"
  10.   PRINT "Button Up/Dn State: ",Iup::GetBtnState(),"\n"
  11. END SUB
  12.  
  13. SUB btn2_clicked
  14.   PRINT "Thread 1 Button 2 Pressed\n"
  15. END SUB
  16.  
  17. SUB btn3_clicked
  18.   PRINT "Thread 1 Button 3 Pressed\n"
  19. END SUB
  20.  
  21. SUB win_exit
  22.   ' Good-Bye
  23. END SUB
  24.  
  25. SUB main
  26.   win = DIALOG()
  27.   SETPROPERTIES win, "TITLE=\"SBx Thread 1\", SIZE=300x"
  28.   horzbox = HBOX()
  29.   SETPROPERTIES horzbox, "GAP=5"
  30.   btn1 = BUTTON()
  31.   SETPROPERTIES btn1, "TITLE=\"Button 1\", EXPAND=HORIZONTAL"
  32.   btn2 = BUTTON()
  33.   SETPROPERTIES btn2, "TITLE=\"Button 2\", EXPAND=HORIZONTAL"
  34.   btn3 = BUTTON()
  35.   SETPROPERTIES btn3, "TITLE=\"Button 3\", EXPAND=HORIZONTAL"
  36.   APPEND horzbox, btn1
  37.   APPEND horzbox, btn2
  38.   APPEND horzbox, btn3
  39.   APPEND win, horzbox
  40.   Iup::SetThreadCallback(win, "CLOSE_CB", "main::win_exit", 1)
  41.   Iup::SetThreadCallback(btn1, "BUTTON_CB", "main::btn1_clicked", 1)
  42.    Iup::SetThreadCallback(btn2, "ACTION", "main::btn2_clicked", 1)
  43.   Iup::SetThreadCallback(btn3, "ACTION", "main::btn3_clicked", 1)
  44.   SHOW win
  45. END SUB
  46. mt::SetVariable("sb1_loaded","OK")
  47.  

SBx (Experimental IUP Wrapper)
Code: Script BASIC
  1. ' ScriptBasic IUP Interface
  2.  
  3. FUNCTION DIALOG
  4.   DIALOG = Iup::Create("dialog")
  5. END FUNCTION
  6.  
  7. SUB SETPROPERTIES(ih, propstr)
  8.   Iup::SetAttributes(ih, propstr)
  9. END SUB
  10.  
  11. SUB SETPROPERTY(ih, typ, value)
  12.   Iup::SetAttribute(ih, typ, value)
  13. END SUB
  14.  
  15. FUNCTION GETPROPERTY(ih, typ)
  16.   GETPROPERTY = Iup::GetAttribute(ih, typ)
  17. END FUNCTION
  18.  
  19. FUNCTION VBOX
  20.   VBOX = Iup::Create("vbox")
  21. END FUNCTION
  22.  
  23. FUNCTION HBOX
  24.   HBOX = Iup::Create("hbox")
  25. END FUNCTION
  26.  
  27. FUNCTION FRAME
  28.   FRAME = Iup::Create("frame")
  29. END FUNCTION
  30.  
  31. FUNCTION BUTTON
  32.   BUTTON = Iup::Create("button")
  33. END FUNCTION
  34.  
  35. FUNCTION LIST
  36.   LIST = Iup::Create("list")
  37. END FUNCTION
  38.  
  39. FUNCTION TEXT
  40.   TEXT = Iup::Create("text")
  41. END FUNCTION
  42.  
  43. FUNCTION LABEL
  44.   LABEL = Iup::Create("label")
  45. END FUNCTION
  46.  
  47. FUNCTION TOGGLE
  48.   TOGGLE = Iup::Create("toggle")
  49. END FUNCTION
  50.  
  51. SUB MESSAGE(title, body)
  52.   Iup::Message(title, body)
  53. END SUB
  54.  
  55. FUNCTION GETITEM
  56.   GETITEM = Iup::GetListText()
  57. END FUNCTION
  58.  
  59. SUB APPEND(ih_to, ih_from)
  60.   Iup::Append(ih_to, ih_from)
  61. END SUB
  62.  
  63. FUNCTION FOCUS(ih)
  64.   FOCUS = Iup::SetFocus(ih)
  65. END FUNCTION
  66.  
  67. FUNCTION UPDATE(ih)
  68.   UPDATE = Iup::Update(ih)
  69. END FUNCTION
  70.  
  71. SUB CLEAR(ih)
  72.   Iup::ClearList(ih)
  73. END SUB
  74.  
  75. ' SUB SETEVENT(ih, class, funcaddr)
  76. '   Iup::SetCallback(ih, class,  funcaddr)
  77. ' END SUB
  78.  
  79. SUB SHOW(ih)
  80.   Iup::Show(ih)
  81. END SUB  
  82.  
  83. ' SUB GETEVENT
  84. '   Iup::MainLoop
  85. '   Iup::Close
  86. ' END SUB
  87.  

108
What's New / IUP Threaded - Linux 64 bit - SBT
« on: May 23, 2015, 07:26:41 PM »
Done!

I finally got this worked out and didn't have to inform Gtk or IUP that they're being threaded8)

I can click on either thread window's button as fast as I can and it responds with the being pressed message. The only minor issue I still have is the second thread window will open in max size and sometimes without a max/restore window button. At this point I'm happy.

iup.bas - callback handling functions
Code: Script BASIC
  1. FUNCTION MainLoop
  2. LOCAL hex_event
  3.   LoopStep()
  4.   this_event = GetEvent()
  5.   hex_event = BB_HTA(this_event)
  6.   IF hex_event = event{hex_event}[0] THEN
  7.     IF event{hex_event}[2] = 1 THEN
  8.       SB_CallSub(main::sb1, event{hex_event}[1])
  9.     ELSEIF event{hex_event}[2] = 2 THEN
  10.       SB_CallSub(main::sb2, event{hex_event}[1])
  11.     END IF
  12.     MainLoop = GetActionName()
  13.   END IF  
  14. END FUNCTION
  15.  
  16. FUNCTION SetThreadCallback(ih, aname, fname, tnum)
  17.   t_event = mt::GetVariable("Callback_Map")
  18.   IF t_event = undef THEN t_event = ""
  19.   t_event = t_event & BB_HTA(ih) & "|" & fname & "|" & tnum & "\n"
  20.   mt::SetVariable("Callback_Map", t_event)
  21.   SetThreadCallback = __SetCallback(ih, aname)
  22. END FUNCTION
  23.  
  24. SUB GetThreadCallback
  25.   LOCAL t_event, e_list, e_array, x
  26.   t_event = mt::GetVariable("Callback_Map")
  27.   SPLITA t_event BY "\n" TO e_list
  28.   FOR x = 0 TO UBOUND(e_list)
  29.     SPLITA e_list[x] BY "|" TO e_array
  30.     event{e_array[0]}[0] = e_array[0]
  31.     event{e_array[0]}[1] = e_array[1]
  32.     event{e_array[0]}[2] = e_array[2]
  33.   NEXT
  34. END SUB
  35.  
  36. FUNCTION BB_HTA(AsciiStr)
  37.   LOCAL AsciiLen,ScanPos,HexStr
  38.   AsciiLen = LEN(AsciiStr)
  39.   HexStr = ""
  40.   IF AsciiLen THEN
  41.     FOR ScanPos = 1 TO AsciiLen
  42.       HexStr &= RIGHT("0" & HEX(ASC(MID(AsciiStr, ScanPos, 1))),2)
  43.     NEXT ScanPos
  44.   ELSE
  45.     HexStr = ""
  46.   END IF
  47.   BB_HTA = HexStr
  48. END FUNCTION
  49.  

iupmain.sb - host SB script (puppet master)
Code: Script BASIC
  1. IMPORT mt.bas
  2. IMPORT sbt.inc
  3. IMPORT iup.bas
  4.  
  5. Iup::Open()
  6.  
  7. SUB SB_Wait(mtvar)
  8.   WHILE mt::GetVariable(mtvar) <> "OK"
  9.     SB_msSleep(5000)
  10.   WEND
  11. END SUB
  12.  
  13. sb1 = SB_ThreadStart("rqdemo1.sb",undef,"/etc/scriba/basic.conf")
  14. SB_Wait("sb1_loaded")
  15. sb1_rtn = SB_CallSubArgs(sb1, "main::main", sb1)
  16.  
  17. sb2 = SB_ThreadStart("rqdemo2.sb",undef,"/etc/scriba/basic.conf")
  18. SB_Wait("sb2_loaded")
  19. sb2_rtn = SB_CallSubArgs(sb2, "main::main", sb2)
  20.  
  21. threads = 2
  22.  
  23. Iup::GetThreadCallback()
  24.  
  25. WHILE threads
  26.   event_class = Iup::MainLoop()
  27.   IF event_class = "CLOSE_CB" THEN
  28.     threads -= 1
  29.     IF Iup::event{Iup::BB_HTA(Iup::this_event)}[2] = 1 THEN
  30.       SB_CallSub(sb1, "iup::exitloop")
  31.     ELSEIF Iup::event{Iup::BB_HTA(Iup::this_event)}[2] = 2 THEN
  32.       SB_CallSub(sb2, "iup::exitloop")
  33.     END IF
  34.   END IF  
  35.   SB_msSleep(250)
  36. WEND
  37.  
  38. Iup::Close()
  39. SB_Destroy(sb2)
  40. SB_Destroy(sb1)
  41.  

rqdemo1.sb - rqdemo2.sb is identical other than the references to it been the second thread.
Code: Script BASIC
  1. ' Script BASIC Rapid-Q form conversion
  2.  
  3. IMPORT mt.bas
  4. IMPORT iup.bas
  5.  
  6. ' CALLBACKS FUNCTIONS
  7.  
  8. SUB button_quit
  9.   PRINT "Thread 1 Quit Button Pressed\n"
  10. END SUB  
  11.  
  12. SUB win_exit
  13.   ' Good-Bye  
  14. END SUB
  15.  
  16. SUB main
  17.  
  18.   ' SBIUP-Q INIT
  19.  
  20.   Iup::Open()
  21.   Iup::SetGlobal("DEFAULTFONT", "Sans, 7.5")
  22.  
  23.   ' CREATE FORM
  24.  
  25.   Form = Iup::Create("dialog")
  26.          Iup::SetAttributes(Form, "RASTERSIZE=320x240, TITLE=\"Thread 1\"")
  27.  
  28.        Label1  = Iup::Create("label")
  29.                  Iup::SetAttributes(Label1, "TITLE=\"Customer\", RASTERSIZE=55x13, FLOATING=YES, POSITION=\"19,19\"")
  30.  
  31.        Edit1   = Iup::Create("text")
  32.                  Iup::SetAttributes(Edit1, "RASTERSIZE=121x21, FLOATING=YES, POSITION=\"72,16\"")
  33.  
  34.        Button1 = Iup::Create("button")
  35.                  Iup::SetAttributes(Button1, "TITLE=\"&Quit\", RASTERSIZE=75x25, FLOATING=YES, POSITION=\"107,164\"")
  36.  
  37.   vbx = Iup::Vbox(Label1, Edit1, Button1)
  38.   Iup::Append(Form, vbx)
  39.                  
  40.   ' SET CALLBACKS
  41.  
  42.   Iup::SetThreadCallback(Form, "CLOSE_CB", "main::win_exit", 1)
  43.   Iup::SetThreadCallback(Button1, "ACTION", "main::button_quit", 1)
  44.   Iup::Show(Form)
  45. END SUB
  46. mt::SetVariable("sb1_loaded","OK")
  47.  

109
What's New / IUP Threaded - Windows 32 bit - DLLC
« on: May 17, 2015, 10:30:46 PM »
Here is an example of IUP running in a threaded mode with the DLLC extension module for Windows 32 bit.

The DLLC Windows 32 bit extension module can be found in the current OxygenBasic build and maintained by Charles Pegge.







Thread #1 script
Code: Script BASIC
  1. ' Thread #1 Script
  2.  
  3. INCLUDE "dllcinc.sb"
  4.  
  5. iup = dllfile("iup.dll")
  6.  
  7. IupOpen          = dllproc(iup,"IupOpen          cdecl i = (i argc, i argv)")
  8. IupCreate        = dllproc(iup,"IupCreate        cdecl i = (c *classname)")
  9. IupSetAttributes = dllproc(iup,"IupSetAttributes cdecl i = (i ih, c *attr_str)")
  10. IupAppend        = dllproc(iup,"IupAppend        cdecl i = (i ih, cdecl i new_child)")
  11. IupSetCallback   = dllproc(iup,"IupSetCallback   cdecl i = (i ih, c*cb_name, i funcaddr)")
  12. IupShow          = dllproc(iup,"IupShow          cdecl i = (i ih)")
  13. IupMainLoop      = dllproc(iup,"IupMainLoop      cdecl i = ()")
  14. IupClose         = dllproc(iup,"IupClose         cdecl     ()")
  15.  
  16. GLOBAL CONST IUP_DEFAULT = -2
  17.  
  18. FUNCTION Btn1_T1(ih, mbut, pstat)
  19.   PRINT "B1 - T1 ", CHR(mbut), " - ", pstat, "\n"
  20.   Btn1_clicked = IUP_DEFAULT
  21. END FUNCTION
  22.  
  23. FUNCTION Btn2_T1(ih)
  24.   dllprnt"B2 - T1\n"
  25.   Btn2_clicked = IUP_DEFAULT
  26. END FUNCTION
  27.  
  28. FUNCTION Btn3_T1(ih)
  29.   dllprnt"B3 - T1\n"
  30.   Btn3_clicked = IUP_DEFAULT
  31. END FUNCTION
  32.  
  33. FUNCTION main(pProg,idat)
  34.   dllcall(IupOpen, 0, 0)
  35.   win = dllcall(IupCreate, "dialog")
  36.   dllcall(IupSetAttributes, win, "TITLE=\"Thread #1\", SIZE=300x")
  37.   horzbox = dllcall(IupCreate, "hbox")
  38.   dllcall(IupSetAttributes, horzbox, "GAP=5")
  39.   btn1 = dllcall(IupCreate, "button")
  40.   dllcall(IupSetAttributes, btn1, "TITLE=Button1, EXPAND=HORIZONTAL")
  41.   btn2 = dllcall(IupCreate, "button")
  42.   dllcall(IupSetAttributes, btn2, "TITLE=Button2, EXPAND=HORIZONTAL")
  43.   btn3 = dllcall(IupCreate, "button")
  44.   dllcall(IupSetAttributes, btn3, "TITLE=Button3, EXPAND=HORIZONTAL")
  45.   dllcall(IupAppend, horzbox, btn1)
  46.   dllcall(IupAppend, horzbox, btn2)
  47.   dllcall(IupAppend, horzbox, btn3)
  48.   dllcall(IupAppend, win, horzbox)
  49.   dllcall(IupSetCallback, btn1, "BUTTON_CB", dllclbk(1, pProg, "MAIN::Btn1_T1", 3,IUP_DEFAULT,idat))
  50.   dllcall(IupSetCallback, btn2, "ACTION", dllclbk(2, pProg, "MAIN::Btn2_T1", 1,IUP_DEFAULT,idat))
  51.   dllcall(IupSetCallback, btn3, "ACTION", dllclbk(3, pProg, "MAIN::Btn3_T1", 1,IUP_DEFAULT,idat))
  52.   dllcall(IupShow, win)
  53.   Main=IupMainLoop
  54. END FUNCTION
  55.  

Thread #2 script
Code: Script BASIC
  1. ' Thread #2 Script
  2.  
  3. INCLUDE "dllcinc.sb"
  4.  
  5. iup = dllfile("iup.dll")
  6.  
  7. IupOpen          = dllproc(iup,"IupOpen          cdecl i = (i argc, i argv)")
  8. IupCreate        = dllproc(iup,"IupCreate        cdecl i = (c *classname)")
  9. IupSetAttributes = dllproc(iup,"IupSetAttributes cdecl i = (i ih, c *attr_str)")
  10. IupAppend        = dllproc(iup,"IupAppend        cdecl i = (i ih, cdecl i new_child)")
  11. IupSetCallback   = dllproc(iup,"IupSetCallback   cdecl i = (i ih, c*cb_name, i funcaddr)")
  12. IupShow          = dllproc(iup,"IupShow          cdecl i = (i ih)")
  13. IupMainLoop      = dllproc(iup,"IupMainLoop      cdecl i = ()")
  14. IupClose         = dllproc(iup,"IupClose         cdecl     ()")
  15.  
  16. GLOBAL CONST IUP_DEFAULT = -2
  17.  
  18. FUNCTION Btn1_T2(ih)
  19.   dllprnt"B1 - T2\n"
  20.   Btn1_clicked = IUP_DEFAULT
  21. END FUNCTION
  22.  
  23. FUNCTION Btn2_T2(ih)
  24.   dllprnt"B2 - T2\n"
  25.   Btn2_clicked = IUP_DEFAULT
  26. END FUNCTION
  27.  
  28. FUNCTION Btn3_T2(ih)
  29.   dllprnt"B3 - T2\n"
  30.   Btn3_clicked = IUP_DEFAULT
  31. END FUNCTION
  32.  
  33. FUNCTION main(pProg,idat)
  34.   dllcall(IupOpen, 0, 0)
  35.   win = dllcall(IupCreate, "dialog")
  36.   dllcall(IupSetAttributes, win, "TITLE=\"Thread #2\", SIZE=300x")
  37.   horzbox = dllcall(IupCreate, "hbox")
  38.   dllcall(IupSetAttributes, horzbox, "GAP=5")
  39.   btn1 = dllcall(IupCreate, "button")
  40.   dllcall(IupSetAttributes, btn1, "TITLE=Button1, EXPAND=HORIZONTAL")
  41.   btn2 = dllcall(IupCreate, "button")
  42.   dllcall(IupSetAttributes, btn2, "TITLE=Button2, EXPAND=HORIZONTAL")
  43.   btn3 = dllcall(IupCreate, "button")
  44.   dllcall(IupSetAttributes, btn3, "TITLE=Button3, EXPAND=HORIZONTAL")
  45.   dllcall(IupAppend, horzbox, btn1)
  46.   dllcall(IupAppend, horzbox, btn2)
  47.   dllcall(IupAppend, horzbox, btn3)
  48.   dllcall(IupAppend, win, horzbox)
  49.   dllcall(IupSetCallback, btn1, "ACTION", dllclbk(4, pProg, "MAIN::Btn1_T2", 1,IUP_DEFAULT,idat))
  50.   dllcall(IupSetCallback, btn2, "ACTION", dllclbk(5, pProg, "MAIN::Btn2_T2", 1,IUP_DEFAULT,idat))
  51.   dllcall(IupSetCallback, btn3, "ACTION", dllclbk(6, pProg, "MAIN::Btn3_T2", 1,IUP_DEFAULT,idat))
  52.   dllcall(IupShow, win)
  53.   Main=IupMainLoop
  54. END FUNCTION
  55.  

Start script
Code: Script BASIC
  1. ' Boot (Main / Launcher)
  2.  
  3. INCLUDE "dllcinc.sb"
  4. bdat=string(8192,chr(0))
  5. idat=dllsptr(bdat)
  6.  
  7. thrM1 = dlltran("T1.sb","main::main",1,idat)
  8. thrM2 = dlltran("T2.sb","main::main",2,idat)
  9.  
  10. LINE INPUT wait
  11.  
  12. dllclos thrM1,thrM2
  13. dllfile
  14.  

110
What's New / Re: SBT - Thread Enhancements
« on: May 17, 2015, 07:52:50 PM »
I made a few improvement to the SBT extension module to allow threads to act more like the embedded API I started off with. A thread will not terminate at the end of its run. The script can be rerun in the thread if you like. You can access thread script variables, call FUNCTIONs and SUBs and use the MT extension module for thread status for the host or other threads.

tcallmain
Code: Script BASIC
  1. IMPORT sbt.inc
  2.  
  3. sb = SB_ThreadStart("tcall.sb",undef,"/etc/scriba/basic.conf")
  4. SB_SetInt sb, "main::a", 123
  5. SB_SetDbl sb, "main::b", 1.23
  6. SB_SetStr sb, "main::c", "One, Two, Three"
  7. funcrtn = SB_CallSubArgs(sb, "main::prtvars", _
  8.           SB_GetVar(sb, "main::a"), _
  9.           SB_GetVar(sb, "main::b"), _
  10.           SB_GetVar(sb, "main::c"))      
  11. PRINT funcrtn,"\n"
  12. SB_Destroy sb
  13.  

tcall.sb
Code: Script BASIC
  1. FUNCTION prtvars(a, b, c)    
  2.   PRINT a,"\n"              
  3.   PRINT FORMAT("%g\n", b)  
  4.   PRINT c,"\n"              
  5.   prtvars = "Function Return"
  6. END FUNCTION                
  7.                              
  8. a = 0                        
  9. b = 0                        
  10. c = ""                      
  11.  


jrs@laptop:~/sb/sb22/sbt$ scriba tcallmain.sb
123
1.23
One, Two, Three
Function Return
jrs@laptop:~/sb/sb22/sbt$


Here is an example of rerunning a script in an existing thread.

Code: Script BASIC
  1. IMPORT sbt.inc
  2.  
  3. sb = SB_ThreadStart("tprint.sb")
  4. SB_Run(sb,"")
  5. SB_Destroy(sb)
  6.  

Code: Script BASIC
  1. PRINT 123,"\n"
  2. PRINT FORMAT("%g\n",1.23)
  3. PRINT "One,Two,Three\n"
  4.  


jrs@laptop:~/sb/sb22/sbt$ scriba tpmain.sb
123
1.23
One,Two,Three
123
1.23
One,Two,Three
jrs@laptop:~/sb/sb22/sbt$


111
I have added thread support to the SBT extension module. It also supports the MT multi-threaded shared (lockable R/W) variable and session manager.

Here is an example of using the MT extension module to communicate between threads and the host script. The command line and configuration file are optional arguments. If not passed, The threaded version of the script uses the internal defaults. This method doesn't provide the paths to the modules & include directory that the configuration file provides. As long as you IMPORT your extension modules in the host script, a simple DECLARE of the function is all that is needed.

ttmain.sb
Code: Script BASIC
  1. IMPORT mt.bas
  2. IMPORT sbt.inc
  3.  
  4. SB_ThreadStart("tt1.sb", "JRS","/etc/scriba/basic.conf")
  5. PRINT "SB Host\n"
  6. LINE INPUT wait
  7. PRINT mt::GetVariable("thread_status"),"\n"
  8.  

tt1.sb
Code: Script BASIC
  1. ' Test Thread
  2.  
  3. IMPORT mt.bas
  4. IMPORT sbt.inc
  5.  
  6. cmd = COMMAND()
  7. PRINT cmd,"\n"
  8.  
  9. FOR x = 1 TO 10
  10.   PRINT "Thread 1: ",x,"\n"
  11. NEXT
  12.  
  13. mt::SetVariable "thread_status","Completed"
  14.  
  15. SB_ThreadEnd
  16.  

Output

jrs@laptop:~/sb/sb22/sbt$ scriba ttmain.sb
SB Host
JRS
Thread 1: 1
Thread 1: 2
Thread 1: 3
Thread 1: 4
Thread 1: 5
Thread 1: 6
Thread 1: 7
Thread 1: 8
Thread 1: 9
Thread 1: 10

Completed
jrs@laptop:~/sb/sb22/sbt$

112
What's New / dbgcon - Script BASIC remote debugger
« on: May 05, 2015, 07:47:55 PM »
A few years ago I made some bug fixes to Peter's Verhas's experimental sdbg remote Script BASIC debugger. I wrote my own console client in SB which works great. I plan on using some of Dave Zimmer's enhancements to his VB/COM IDE/Debugger preprocessor project to view array contents and trace the call stack. The remote socket based Script BASIC debugger works with both local desktop scripts and remote sbhttpd proxy server web applications.

dbgcon.sb
Code: Script BASIC
  1. ' ScriptBasic Remote Console Debugger
  2.  
  3. cmdln = TRIM(COMMAND())
  4. IF cmdln = "" THEN
  5.   PRINT "Usage: dbgcon [prog2debug]\n"
  6.   END
  7. END IF
  8. exitcode = EXECUTE("/usr/bin/scriba -i sdbg " & cmdln,-1,PID)
  9. OPEN "127.0.0.1:6647" FOR SOCKET AS #1
  10. WHILE NOT EOF(1)
  11.   LINE INPUT #1, dbgs
  12.   IF dbgs = ".\n" THEN
  13.     PRINT "-> "
  14.     LINE INPUT dbgc
  15.     IF LCASE(CHOMP(dbgc)) = "h" THEN
  16. PRINT """h help
  17. s step one line
  18. S step one line, do not step into functions or subs
  19. o step until getting out of the current function
  20.  (if you stepped into but changed your mind)
  21. ? var  print the value of a variable
  22. u step one level up in the stack
  23. d step one level down in the stack (for variable printing)
  24. D step down in the stack to current execution depth
  25. G list all global variables
  26. L list all local variables
  27. l [n-m] list the source lines
  28. r [n] run to line n
  29. R [n] run to line n but do not stop in recursive function call
  30. b [n] set breakpoint on the line n or the current line
  31. B [n-m] remove breakpoints from lines
  32. q quit the program
  33. """
  34.     END IF
  35.     PRINT #1, dbgc
  36.     IF CHOMP(dbgc) = "q" THEN GOTO Done
  37.   ELSE
  38.     dbgcmd = CHOMP(dbgs)
  39. ' l - List Source  
  40.    IF INSTR(dbgcmd,"Break-Point: ")<>undef THEN
  41.       p = INSTR(dbgcmd,"Break-Point: ")
  42.       IF MID(dbgcmd,p+13,1) = "0" THEN
  43.         PRINT " "
  44.       ELSE
  45.         PRINT "*"
  46.       END IF
  47.       GOTO IT
  48.     END IF
  49.     IF INSTR(dbgcmd,"Line-Number: ")<>undef THEN
  50.       p = INSTR(dbgcmd,"Line-Number: ")
  51.       PRINT FORMAT("%~[0000] ~",VAL(MID(dbgcmd,p+13)))
  52.       online = TRUE
  53.       GOTO IT
  54.     END IF
  55.     IF INSTR(dbgcmd,"Line: ")<>undef THEN
  56.       p = INSTR(dbgcmd,"Line: ")
  57.       IF online THEN
  58.         PRINT MID(dbgcmd,p+6),"\n"
  59.       ELSE
  60.         PRINT MID(dbgcmd,p),"\n"
  61.       END IF        
  62.       online = FALSE
  63.       GOTO IT
  64.     END IF
  65.     IF INSTR(dbgcmd,"Global-Variable")<>undef THEN
  66.       p = INSTR(dbgcmd,"Global-Variable")
  67.       PRINT "G-Var" & MID(dbgcmd,p+15) & "\n"
  68.       GOTO IT
  69.     END IF
  70. ' Unprocessed out
  71.   PRINT dbgs
  72.   END IF
  73. IT:
  74. WEND
  75.  
  76. Done:
  77. PRINT #1,"q"
  78. CLOSE(1)
  79. PRINT "Debug session closed.\n"
  80. END
  81.  

testarray.sb
Code: Script BASIC
  1. ' Long / Double / String
  2. i = 1
  3. d = .99
  4. s = "JRS"
  5. ' Indices array
  6. a[0,0] = 0
  7. a[0,1] = 123
  8. a[0,2] = 1.23
  9. a[0,3] = "One,Two,Three"
  10. a[1,10] = "Zero"
  11. a[1,11] = 321
  12. a[1,12] = 32.1
  13. a[1,13] = "Three,Two,One"
  14. ' Asscociative array
  15. b{"One"} = 1
  16. b{"Two"} = .2
  17. b{"Three"} = "*3*"
  18. ' Mix asscociative & indices array
  19. c{"JRS"}[1] = 1
  20. c{"JRS"}[2] = .2
  21. c{"JRS"}[3] = "*3*"
  22. PRINT "Done\n"
  23.  

Output

jrs@laptop:~/sb/sb22/sbt$ scriba dbgcon.sb testarray.sb
Application: ScriptBasic Remote Debugger - Linux
Version: 1.0
Source-File-Count: 1
Source-File: testarray.sb
Line: 2
-> b15
Message: done
Line: 2
-> l1-
 [0001] ' Long / Double / String
 [0002] i = 1
 [0003] d = .99
 [0004] s = "JRS"
 [0005] ' Indices array
 [0006] a[0,0] = 0
 [0007] a[0,1] = 123
 [0008] a[0,2] = 1.23
 [0009] a[0,3] = "One,Two,Three"
 [0010] a[1,10] = "Zero"
 [0011] a[1,11] = 321
 [0012] a[1,12] = 32.1
 [0013] a[1,13] = "Three,Two,One"
 [0014] ' Asscociative array
*[0015] b{"One"} = 1
 [0016] b{"Two"} = .2
 [0017] b{"Three"} = "*3*"
 [0018] ' Mix asscociative & indices array
 [0019] c{"JRS"}[1] = 1
 [0020] c{"JRS"}[2] = .2
 [0021] c{"JRS"}[3] = "*3*"
 [0022] PRINT "Done\n"
Line: 2
-> r
Line: 15
-> r22
Line: 22
-> G
G-Var-Name: VT=0 @ 0x014BBD18 VN=main::i
G-Var-Value: 1
G-Var-Name: VT=1 @ 0x014C9258 VN=main::d
G-Var-Value: 0.990000
G-Var-Name: VT=2 @ 0x014C92B8 VN=main::s
G-Var-Value: "JRS"
G-Var-Name: VT=3 @ 0x014C9378 LB=0 : UB=1 VN=main::a
G-Var-Value: 
LB=0 : UB=3 VN=[0]
[0] VT=3 @ 0x014C9468 
[0] VT=0 @ 0x014C9558 0
[1] VT=0 @ 0x014C96D8 123
[2] VT=1 @ 0x014C9898 1.230000
[3] VT=2 @ 0x014BC0A8 "One,Two,Three"
LB=10 : UB=13 VN=[1]
[1] VT=3 @ 0x014BC1C8 
[10] VT=2 @ 0x014BC228 "Zero"
[11] VT=0 @ 0x014BC3A8 321
[12] VT=1 @ 0x014BC568 32.100000
[13] VT=2 @ 0x014BC6D8 "Three,Two,One"
G-Var-Name: VT=3 @ 0x014BC798 LB=0 : UB=5 VN=main::b
G-Var-Value: 
[0] VT=2 @ 0x014BC7F8 "One"
[1] VT=0 @ 0x014BC8B8 1
[2] VT=2 @ 0x014BC968 "Two"
[3] VT=1 @ 0x014BCA28 0.200000
[4] VT=2 @ 0x014BCAE8 "Three"
[5] VT=2 @ 0x014BCB48 "*3*"
G-Var-Name: VT=3 @ 0x014BCBA8 LB=0 : UB=1 VN=main::c
G-Var-Value: 
[0] VT=2 @ 0x014BCC08 "JRS"
LB=1 : UB=3 VN=[1]
[1] VT=3 @ 0x014BCCC8 
[1] VT=0 @ 0x014BCD88 1
[2] VT=1 @ 0x014BCEA8 0.200000
[3] VT=2 @ 0x014BD5C8 "*3*"
Line: 22
-> ?s
Value: "JRS"
Line: 22
-> ?b
Value: 
[0] VT=2 @ 0x014BC7F8 "One"
[1] VT=0 @ 0x014BC8B8 1
[2] VT=2 @ 0x014BC968 "Two"
[3] VT=1 @ 0x014BCA28 0.200000
[4] VT=2 @ 0x014BCAE8 "Three"
[5] VT=2 @ 0x014BCB48 "*3*"
Line: 22
-> r
Done
Debug session closed.
jrs@laptop:~/sb/sb22/sbt$


113
What's New / SBT - Script BASIC Tutorial API extension module
« on: May 05, 2015, 06:14:26 PM »
I have embedded Script BASIC into itself as an easy to use example of the embedding and extension API's. I used the C BASIC C preprocessor defines to extend Script BASIC's extensive macro and define definitions in the interface.c design for readability.

Current SBT Download Attached


SBT interface.c
Code: C
  1. /*  SBT (Script BASIC Tutorial) - Extension Module */
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <ctype.h>
  7. #include <math.h>
  8. #include <time.h>
  9. #include <unistd.h>
  10. #include "../../basext.h"
  11. #include "../../scriba.h"
  12. #include "cbasic.h"
  13.  
  14.  
  15. /****************************
  16.  Extension Module Functions
  17. ****************************/
  18.  
  19. besVERSION_NEGOTIATE
  20.   RETURN_FUNCTION((int)INTERFACE_VERSION);
  21. besEND
  22.  
  23. besSUB_START
  24.   DIM AS long PTR p;
  25.   besMODULEPOINTER = besALLOC(sizeof(long));
  26.   IF (besMODULEPOINTER EQ NULL) THEN_DO RETURN_FUNCTION(0);
  27.   p = (long PTR)besMODULEPOINTER;
  28.   RETURN_FUNCTION(0);
  29. besEND
  30.  
  31. besSUB_FINISH
  32.   DIM AS long PTR p;
  33.   p = (long PTR)besMODULEPOINTER;
  34.   IF (p EQ NULL) THEN_DO RETURN_FUNCTION(0);
  35.   RETURN_FUNCTION(0);
  36. besEND
  37.  
  38.  
  39. /**********************
  40.  Script BASIC Instance
  41. **********************/
  42.  
  43. /******************
  44.  Support Routines
  45. ******************/
  46.  
  47. struct _RunServiceProgram {
  48.   char *pszProgramFileName;
  49.   char *pszCmdLineArgs;
  50.   char *pszConfigFileName;
  51.   pSbProgram pTProgram;
  52.   int iRestart;
  53.   };
  54.  
  55. static void ExecuteProgramThread(void *p){
  56.   pSbProgram pProgram;
  57.   char szInputFile[1024];
  58.   int iErrorCode;
  59.   struct _RunServiceProgram *pRSP;
  60.   pRSP = p;
  61.   strcpy(szInputFile,pRSP->pszProgramFileName);
  62.   pProgram = scriba_new(malloc,free);
  63.   pRSP->pTProgram = pProgram;
  64.   if( pProgram == NULL )return;
  65.   scriba_SetFileName(pProgram,szInputFile);
  66.   if (pRSP->pszConfigFileName != NULL){
  67.         strcpy(szInputFile,pRSP->pszConfigFileName);
  68.         scriba_LoadConfiguration(pProgram, pRSP->pszConfigFileName);
  69.   }else{
  70.         scriba_SetProcessSbObject(pProgram,pProgram);
  71.   }    
  72.   scriba_LoadSourceProgram(pProgram);
  73.   if (pRSP->pszCmdLineArgs != NULL){
  74.         strcpy(szInputFile,pRSP->pszCmdLineArgs);
  75.     iErrorCode = scriba_Run(pProgram,pRSP->pszCmdLineArgs);
  76.   }else{
  77.     iErrorCode = scriba_Run(pProgram,NULL);
  78.   }    
  79. //  scriba_destroy(pProgram);
  80.   return;
  81. }
  82.  
  83. besFUNCTION(SB_New)
  84.   DIM AS pSbProgram sbobj;
  85.   sbobj = scriba_new(malloc,free);
  86.   besRETURN_LONG(sbobj);
  87. besEND
  88.  
  89. besFUNCTION(SB_Configure)
  90.   DIM AS unsigned long sbobj;
  91.   DIM AS char PTR cfgfilename;
  92.   DIM AS int rtnval = -1;
  93.   besARGUMENTS("iz")
  94.     AT sbobj, AT cfgfilename
  95.   besARGEND
  96.   rtnval = scriba_LoadConfiguration(sbobj, cfgfilename);
  97.   besRETURN_LONG(rtnval);
  98. besEND
  99.  
  100. besFUNCTION(SB_Load)
  101.   DIM AS unsigned long sbobj;
  102.   DIM AS char PTR sbfilename;
  103.   DIM AS int rtnval = -1;
  104.   besARGUMENTS("iz")
  105.     AT sbobj, AT sbfilename
  106.   besARGEND
  107.   rtnval = scriba_SetFileName(sbobj, sbfilename);
  108.   scriba_LoadSourceProgram(sbobj);
  109.   besRETURN_LONG(rtnval);
  110. besEND
  111.  
  112. besFUNCTION(SB_LoadStr)
  113.   DIM AS unsigned long sbobj;
  114.   DIM AS char PTR sbpgm;
  115.   DIM AS int rtnval = -1;
  116.   besARGUMENTS("iz")
  117.     AT sbobj, AT sbpgm
  118.   besARGEND
  119.   scriba_SetFileName(sbobj, "fake");
  120.   rtnval = scriba_LoadProgramString(sbobj, sbpgm, strlen(sbpgm));
  121.   besRETURN_LONG(rtnval);
  122. besEND
  123.  
  124. besFUNCTION(SB_Run)
  125.   DIM AS unsigned long sbobj;
  126.   DIM AS int rtnval;
  127.   DIM AS char PTR sbcmdline;
  128.   besARGUMENTS("iz")
  129.     AT sbobj, AT sbcmdline
  130.   besARGEND
  131.   IF (besARGNR < 2) THEN_DO sbcmdline = "";
  132.   rtnval = scriba_Run(sbobj, sbcmdline);
  133.   besRETURN_LONG(rtnval);
  134. besEND
  135.  
  136. besFUNCTION(SB_NoRun)
  137.   DIM AS unsigned long sbobj;
  138.   DIM AS int rtnval;
  139.   besARGUMENTS("i")
  140.     AT sbobj
  141.   besARGEND
  142.   rtnval = scriba_NoRun(sbobj);
  143.   besRETURN_LONG(rtnval);
  144. besEND
  145.  
  146. besFUNCTION(SB_ThreadStart)
  147.   DIM AS struct _RunServiceProgram PTR pRSP;
  148.   DIM AS THREADHANDLE T;
  149.   DIM AS char PTR pszProgramFileName;
  150.   DIM AS char PTR pszCmdLineArgs;
  151.   DIM AS char PTR pszConfigFileName;
  152.   DIM AS unsigned long rtnval;
  153.   besARGUMENTS("z[z][z]")
  154.     AT pszProgramFileName, AT pszCmdLineArgs, AT pszConfigFileName
  155.   besARGEND
  156.   pRSP = (struct _RunServiceProgram PTR)malloc( sizeof(struct _RunServiceProgram) );
  157.   pRSP->pszProgramFileName = (char PTR)malloc(strlen(pszProgramFileName) + 1);  
  158.   strcpy(pRSP->pszProgramFileName,pszProgramFileName);
  159.   IF (pszCmdLineArgs NE NULL) THEN
  160.     pRSP->pszCmdLineArgs = (char PTR)malloc(strlen(pszCmdLineArgs) + 1);  
  161.     strcpy(pRSP->pszCmdLineArgs,pszCmdLineArgs);
  162.   ELSE
  163.         pRSP->pszCmdLineArgs = NULL;
  164.   END_IF
  165.   IF (pszConfigFileName NE NULL) THEN
  166.     pRSP->pszConfigFileName = (char PTR)malloc(strlen(pszConfigFileName) + 1);  
  167.     strcpy(pRSP->pszConfigFileName,pszConfigFileName);
  168.   ELSE
  169.         pRSP->pszConfigFileName = NULL;
  170.   END_IF
  171.   pRSP->iRestart = 0;
  172.   thread_CreateThread(AT T,ExecuteProgramThread,pRSP);
  173.   usleep(500);
  174.   rtnval = pRSP->pTProgram;
  175.   besRETURN_LONG(rtnval);
  176. besEND
  177.  
  178. besFUNCTION(SB_ThreadEnd)
  179.   thread_ExitThread();
  180.   besRETURNVALUE = NULL;
  181. besEND
  182.  
  183. besFUNCTION(SB_Destroy)
  184.   DIM AS unsigned long sbobj;
  185.   besARGUMENTS("i")
  186.     AT sbobj
  187.   besARGEND
  188.   scriba_destroy(sbobj);
  189.   RETURN_FUNCTION(0);
  190. besEND
  191.  
  192. besFUNCTION(SB_CallSub)
  193.   DIM AS unsigned long sbobj;
  194.   DIM AS int funcsernum;
  195.   DIM AS char PTR funcname;
  196.   besARGUMENTS("iz")
  197.     AT sbobj, AT funcname
  198.   besARGEND
  199.   funcsernum = scriba_LookupFunctionByName(sbobj, funcname);
  200.   besRETURN_LONG(scriba_Call(sbobj, funcsernum));
  201. besEND
  202.  
  203. besFUNCTION(SB_CallSubArgs)
  204.   DIM AS VARIABLE Argument;
  205.   DIM AS SbData ArgData[8];
  206.   DIM AS SbData FunctionResult;
  207.   DIM AS unsigned long sbobj;
  208.   DIM AS char PTR funcname;
  209.   DIM AS int i, sbtype, fnsn;
  210.  
  211.   Argument = besARGUMENT(1);
  212.   besDEREFERENCE(Argument);
  213.   sbobj = LONGVALUE(Argument);
  214.  
  215.   Argument = besARGUMENT(2);
  216.   besDEREFERENCE(Argument);
  217.   funcname = STRINGVALUE(Argument);
  218.  
  219.   DEF_FOR (i = 3 TO i <= besARGNR STEP INCR i)
  220.   BEGIN_FOR
  221.     Argument = besARGUMENT(i);
  222.     besDEREFERENCE(Argument);
  223.     SELECT_CASE (sbtype = TYPE(Argument))
  224.     BEGIN_SELECT
  225.       CASE VTYPE_LONG:
  226.         ArgData[i-3] = PTR scriba_NewSbLong(sbobj, LONGVALUE(Argument));
  227.         END_CASE
  228.       CASE VTYPE_DOUBLE:
  229.         ArgData[i-3] = PTR scriba_NewSbDouble(sbobj, DOUBLEVALUE(Argument));
  230.         END_CASE
  231.       CASE VTYPE_STRING:
  232.         ArgData[i-3] = PTR scriba_NewSbString(sbobj, STRINGVALUE(Argument));
  233.         END_CASE
  234.       CASE_ELSE
  235.         ArgData[i-3] = PTR scriba_NewSbUndef(sbobj);
  236.         END_CASE
  237.     END_SELECT
  238.   NEXT
  239.  
  240.   fnsn = scriba_LookupFunctionByName(sbobj, funcname);
  241.   scriba_CallArgEx(sbobj, fnsn, AT FunctionResult, besARGNR - 2, AT ArgData);
  242.  
  243.   SELECT_CASE (FunctionResult.type)
  244.   BEGIN_SELECT
  245.     CASE SBT_LONG:
  246.       besRETURN_LONG(FunctionResult.v.l);
  247.       END_CASE
  248.     CASE SBT_DOUBLE:
  249.       besRETURN_DOUBLE(FunctionResult.v.d);
  250.       END_CASE
  251.     CASE SBT_STRING:
  252.       besRETURN_STRING(FunctionResult.v.s);
  253.       END_CASE
  254.     CASE SBT_UNDEF:
  255.       besRETURNVALUE = NULL;
  256.       END_CASE
  257.   END_SELECT
  258. besEND
  259.  
  260. besFUNCTION(SB_GetVar)
  261.   DIM AS pSbData varobj;
  262.   DIM AS unsigned long sbobj;
  263.   DIM AS int vsn;
  264.   DIM AS char PTR varname;
  265.   besARGUMENTS("iz")
  266.     AT sbobj, AT varname
  267.   besARGEND
  268.   vsn = scriba_LookupVariableByName(sbobj, varname);
  269.   scriba_GetVariable(sbobj, vsn, AT varobj);
  270.   SELECT_CASE (scriba_GetVariableType(sbobj, vsn))
  271.   BEGIN_SELECT
  272.     CASE SBT_LONG   :
  273.       besRETURN_LONG(varobj[0].v.l);
  274.       END_CASE
  275.     CASE SBT_DOUBLE :
  276.       besRETURN_DOUBLE(varobj[0].v.d);
  277.       END_CASE
  278.     CASE SBT_STRING :
  279.       besRETURN_STRING(varobj[0].v.s);
  280.       END_CASE
  281.     CASE SBT_UNDEF  :
  282.       besRETURNVALUE = NULL;;
  283.       END_CASE
  284.   END_SELECT
  285. besEND
  286.  
  287. besFUNCTION(SB_SetUndef)
  288.   DIM AS pSbData varobj;
  289.   DIM AS unsigned long sbobj;
  290.   DIM AS int vsn;
  291.   DIM AS char PTR varname;
  292.   besARGUMENTS("iz")
  293.     AT sbobj, AT varname
  294.   besARGEND
  295.   vsn = scriba_LookupVariableByName(sbobj, varname);
  296.   besRETURN_LONG(scriba_SetVariable(sbobj, vsn, SBT_UNDEF, NULL, 0, "", 0));
  297. besEND
  298.  
  299. besFUNCTION(SB_SetInt)
  300.   DIM AS VARIABLE Argument;
  301.   DIM AS pSbData varobj;
  302.   DIM AS unsigned long sbobj;
  303.   DIM AS int vsn, usrval, i;
  304.   DIM AS char PTR varname;
  305.   IF (besARGNR < 3) THEN_DO RETURN_FUNCTION(EX_ERROR_TOO_FEW_ARGUMENTS);
  306.   DEF_FOR (i = 1 TO i <= 3 STEP INCR i)
  307.   BEGIN_FOR
  308.     Argument = besARGUMENT(i);
  309.     besDEREFERENCE(Argument);
  310.     IF (i EQ 1) THEN_DO sbobj = LONGVALUE(Argument);
  311.     IF (i EQ 2) THEN_DO varname = STRINGVALUE(Argument);
  312.     IF (i EQ 3) THEN_DO usrval = LONGVALUE(Argument);
  313.   NEXT
  314.   vsn = scriba_LookupVariableByName(sbobj, varname);
  315.   besRETURN_LONG(scriba_SetVariable(sbobj, vsn, SBT_LONG, usrval, 0, "", 0));
  316. besEND
  317.  
  318. besFUNCTION(SB_SetDbl)
  319.   DIM AS VARIABLE Argument;
  320.   DIM AS pSbData varobj;
  321.   DIM AS unsigned long sbobj;
  322.   DIM AS int vsn, i;
  323.   DIM AS char PTR varname;
  324.   DIM AS double usrval;
  325.   IF (besARGNR < 3) THEN_DO RETURN_FUNCTION(EX_ERROR_TOO_FEW_ARGUMENTS);
  326.   DEF_FOR (i = 1 TO i <= 3 STEP INCR i)
  327.   BEGIN_FOR
  328.     Argument = besARGUMENT(i);
  329.     besDEREFERENCE(Argument);
  330.     IF (i EQ 1) THEN_DO sbobj = LONGVALUE(Argument);
  331.     IF (i EQ 2) THEN_DO varname = STRINGVALUE(Argument);
  332.     IF (i EQ 3) THEN_DO usrval = DOUBLEVALUE(Argument);
  333.   NEXT
  334.   vsn = scriba_LookupVariableByName(sbobj, varname);
  335.   besRETURN_LONG(scriba_SetVariable(sbobj, vsn,  SBT_DOUBLE, 0, usrval, "", 0));
  336. besEND
  337.  
  338. besFUNCTION(SB_SetStr)
  339.   DIM AS VARIABLE Argument;
  340.   DIM AS pSbData varobj;
  341.   DIM AS unsigned long sbobj;
  342.   DIM AS int vsn, i;
  343.   DIM AS char PTR varname;
  344.   DIM AS char PTR usrval;
  345.   IF (besARGNR < 3) THEN_DO RETURN_FUNCTION(EX_ERROR_TOO_FEW_ARGUMENTS);
  346.   DEF_FOR (i = 1 TO i <= 3 STEP INCR i)
  347.   BEGIN_FOR
  348.     Argument = besARGUMENT(i);
  349.     besDEREFERENCE(Argument);
  350.     IF (i EQ 1) THEN_DO sbobj = LONGVALUE(Argument);
  351.     IF (i EQ 2) THEN_DO varname = STRINGVALUE(Argument);
  352.     IF (i EQ 3) THEN_DO usrval = STRINGVALUE(Argument);
  353.   NEXT
  354.   vsn = scriba_LookupVariableByName(sbobj, varname);
  355.   besRETURN_LONG(scriba_SetVariable(sbobj, vsn,  SBT_STRING, 0, 0, usrval, strlen(usrval)));
  356. besEND
  357.  
  358. besFUNCTION(SB_ResetVars)
  359.   DIM AS unsigned long sbobj;
  360.   besARGUMENTS("i")
  361.     AT sbobj
  362.   besARGEND
  363.   scriba_ResetVariables(sbobj);
  364.   besRETURNVALUE = NULL;
  365. besEND
  366.  

sbt.inc
Code: Script BASIC
  1. DECLARE SUB SB_New ALIAS "SB_New" LIB "sbt"
  2. DECLARE SUB SB_Configure ALIAS "SB_Configure" LIB "sbt"
  3. DECLARE SUB SB_Load ALIAS "SB_Load" LIB "sbt"
  4. DECLARE SUB SB_LoadStr ALIAS "SB_LoadStr" LIB "sbt"
  5. DECLARE SUB SB_Run ALIAS "SB_Run" LIB "sbt"
  6. DECLARE SUB SB_NoRun ALIAS "SB_NoRun" LIB "sbt"
  7. DECLARE SUB SB_ThreadStart ALIAS "SB_ThreadStart" LIB "sbt"
  8. DECLARE SUB SB_ThreadEnd ALIAS "SB_ThreadEnd" LIB "sbt"
  9. DECLARE SUB SB_GetVar ALIAS "SB_GetVar" LIB "sbt"
  10. DECLARE SUB SB_SetUndef ALIAS "SB_SetUndef" LIB "sbt"
  11. DECLARE SUB SB_SetInt ALIAS "SB_SetInt" LIB "sbt"
  12. DECLARE SUB SB_SetDbl ALIAS "SB_SetDbl" LIB "sbt"
  13. DECLARE SUB SB_SetStr ALIAS "SB_SetStr" LIB "sbt"
  14. DECLARE SUB SB_ResetVars ALIAS "SB_ResetVars" LIB "sbt"
  15. DECLARE SUB SB_CallSub ALIAS "SB_CallSub" LIB "sbt"
  16. DECLARE SUB SB_CallSubArgs ALIAS "SB_CallSubArgs" LIB "sbt"
  17. DECLARE SUB SB_Destroy ALIAS "SB_Destroy" LIB "sbt"
  18.  

sbtdemo.sb
Code: Script BASIC
  1. ' SBT (Script BASIC Tutorial) - Example Script
  2.  
  3. IMPORT sbt.inc
  4.  
  5. sb_code = """
  6. FUNCTION prtvars(a, b, c)
  7.  PRINT a,"\\n"
  8.  PRINT FORMAT("%g\\n", b)
  9.  PRINT c,"\\n"
  10.  prtvars = "Function Return"
  11. END FUNCTION
  12.  
  13. a = 0
  14. b = 0
  15. c = ""
  16. """
  17.  
  18. sb = SB_New()
  19. SB_Configure sb, "/etc/scriba/basic.conf"
  20. SB_LoadStr sb, sb_code
  21. SB_NoRun sb
  22. funcrtn = SB_CallSubArgs(sb,"main::prtvars", 123, 1.23, "One, Two, Three")
  23. PRINT funcrtn,"\n"
  24. SB_Run sb, ""
  25. SB_SetInt sb, "main::a", 321
  26. SB_SetDbl sb, "main::b", 32.1
  27. SB_SetStr sb, "main::c", "Three,Two,One"
  28. SB_CallSubArgs sb, "main::prtvars", _
  29.           SB_GetVar(sb, "main::a"), _
  30.           SB_GetVar(sb, "main::b"), _
  31.           SB_GetVar(sb, "main::c")      
  32. SB_Destroy sb
  33.  

Output

jrs@laptop:~/sb/sb22/sbt$ time scriba sbtdemo.sb
123
1.23
One, Two, Three
Function Return
321
32.1
Three,Two,One

real   0m0.007s
user   0m0.007s
sys   0m0.000s
jrs@laptop:~/sb/sb22/sbt$


114
Business BASIC Migrations / Business BASIC Helper Library
« on: May 04, 2015, 01:30:51 AM »
Here are a few ProvideX Business Basic like functions that can be used to help with your conversion to Script BASIC.

Code: Script BASIC
  1. ' BB Function Helper Library
  2.  
  3.  
  4. ' BB_ATH - Business BASIC ATH() function
  5. '
  6. ' Converts a text string of hex character pairs to ASCII values.
  7. '
  8. FUNCTION BB_ATH(HexStr)
  9.  
  10.   LOCAL LenHex, AsciiStr, HexTable, ScanPos, HiByte, LowByte
  11.   LenHex = LEN(HexStr)
  12.   IF LenHex % 2 = 0 THEN
  13.     HexTable = "0123456789ABCDEF"
  14.     FOR ScanPos = 1 TO LenHex STEP 2
  15.       HiByte = INSTR(HexTable,UCASE(MID(HexStr, ScanPos, 1))) - 1
  16.       LowByte = INSTR(HexTable,UCASE(MID(HexStr, ScanPos + 1, 1))) - 1
  17.       IF ISINTEGER(HiByte) AND ISINTEGER(LowByte) THEN
  18.         AsciiStr &= CHR(HiByte * 16 + LowByte)
  19.       ELSE
  20.         AsciiStr = ""
  21.         GOTO Exit_For        
  22.       END IF
  23.     NEXT ScanPos
  24.     Exit_For:
  25.   ELSE
  26.     AsciiStr = ""
  27.   END IF
  28.   BB_ATH = AsciiStr
  29.  
  30. END FUNCTION
  31.  
  32.  
  33. ' BB_CVS - Business Basic CVS() function
  34. '
  35. ' Action:
  36. '  1   = Remove Leading characters
  37. '  2   = Remove Trailing characters
  38. '  4   = Convert String to Upper Case
  39. '  8   = Convert String to Lower Case
  40. '  16  = Replace characters < 32 with the control character
  41. '  32  = Replace multiple occurrence of the character with one
  42. '  64  = * Replace $ with defined Windows currency symbol
  43. '  128 = * Replace defined Windows currency, comma and thousand symbol
  44. '  256 = * Ucase first char of each word, rest to lower
  45. '    * = Not implemented yet.
  46. '
  47. FUNCTION BB_CVS(StrExpr, Action, CtrlChar)
  48.  
  49.   LOCAL Char, ExprLen, TempStr, ScanPos
  50.   IF CtrlChar = undef THEN CtrlChar = " "
  51.   Char = ASC(CtrlChar)
  52.  
  53.   ' Remove Leading characters
  54.  IF (Action AND 1) THEN  
  55.     ExprLen = LEN(StrExpr)
  56.     IF CtrlChar = " " THEN
  57.       StrExpr = LTRIM(StrExpr)
  58.     ELSE
  59.       TempStr = ""
  60.       FOR ScanPos = 1 TO ExprLen
  61.         IF MID(StrExpr, ScanPos, 1) <> CtrlChar THEN TempStr &= MID(StrExpr, ScanPos, 1)
  62.       NEXT ScanPos
  63.       StrExpr = TempStr
  64.     END IF
  65.   END IF
  66.  
  67.   ' Remove Trailing characters
  68.  IF (Action AND 2) THEN  
  69.     ExprLen = LEN(StrExpr)
  70.     IF CtrlChar = " " THEN
  71.       StrExpr = RTRIM(StrExpr)
  72.     ELSE
  73.       TempStr = ""
  74.       FOR ScanPos = ExprLen TO 1 STEP - 1
  75.         IF MID(StrExpr, ScanPos, 1) = CtrlChar THEN TempStr = LEFT(StrExpr, ScanPos - 1)
  76.       NEXT ScanPos
  77.       IF LEN(TempStr) THEN StrExpr = TempStr
  78.     END IF
  79.   END IF
  80.  
  81.   ' Convert String to Upper Case
  82.  IF (Action AND 4) THEN  
  83.     StrExpr = UCASE(StrExpr)
  84.   END IF
  85.  
  86.   ' Convert String to Lower Case
  87.  IF (Action AND 8) THEN  
  88.     StrExpr = LCASE(StrExpr)
  89.   END IF
  90.  
  91.   ' Replace characters < 32 with the control character
  92.  IF (Action AND 16) THEN  
  93.     FOR ScanPos = 1 TO LEN(StrExpr)
  94.          IF ASC(MID(StrExpr, ScanPos, 1)) < 32 THEN StrExpr = LEFT(StrExpr, ScanPos -1) & CtrlChar & MID(StrExpr, ScanPos + 1)
  95.     NEXT ScanPos
  96.   END IF
  97.  
  98.   ' Replace multiple occurence of the character with one
  99.  IF (Action AND 32) THEN  
  100.     HitCnt = 0
  101.     StartPos = 1
  102.     NextPos:
  103.     ScanPos = INSTR(StrExpr,CtrlChar,StartPos)
  104.     IF ISINTEGER(ScanPos) THEN
  105.       IF HitCnt THEN  
  106.         IF ASC(MID(StrExpr, ScanPos,1)) = CtrlChar THEN TeStrExpr = LEFT(StrExpr, ScanPos -1) & MID(StrExpr, ScanPos + 1)
  107.       ELSE
  108.         HitCnt += 1
  109.       END IF
  110.       StartPos += 1
  111.       GOTO NextPos
  112.     END IF
  113.   END IF        
  114.   BB_CVS = StrExpr
  115.  
  116. END FUNCTION
  117.  
  118.  
  119. ' BB_DEC - Business BASIC DEC() function
  120. '
  121. ' Returns a two's complement binary equivalent of the string.
  122. '
  123. FUNCTION BB_DEC(BinStr)                                  
  124.  
  125.   LOCAL i, d                                      
  126.   FOR i = LEN(BinStr) TO 1 STEP -1                        
  127.     d += ASC(MID(BinStr,i,1)) * 256 ^ ABS(i - LEN(BinStr))
  128.   NEXT i                                                    
  129.   BB_DEC = d                                                
  130.  
  131. END FUNCTION                                              
  132.  
  133.  
  134. ' BB_HTA - Business BASIC HTA() function
  135. '
  136. ' Returns the hexadecimal text string of the pasted argument string.
  137. '
  138. FUNCTION BB_HTA(AsciiStr)
  139.   LOCAL AsciiLen,ScanPos,HexStr
  140.   AsciiLen = LEN(AsciiStr)
  141.   IF AsciiLen THEN
  142.     FOR ScanPos = 1 TO AsciiLen
  143.       HexStr &= RIGHT("0" & HEX(ASC(MID(AsciiStr, ScanPos, 1))),2)
  144.     NEXT ScanPos
  145.   ELSE
  146.     HexStr = ""
  147.   END IF
  148.   BB_HTA = HexStr
  149. END FUNCTION
  150.  
  151.  
  152. ' BB_JUL - Business BASIC JUL() function
  153. '
  154. ' Converts a date from year, month, day to a Julian date.
  155. '
  156. FUNCTION BB_JUL(Y,M,D)
  157.  
  158.   IF Y = undef AND M = undef AND D = undef THEN
  159.     BB_JUL = NOW
  160.   ELSE
  161.     BB_JUL = TIMEVALUE(Y,M,D)
  162.   END IF
  163.  
  164. END FUNCTION
  165.  
  166.  
  167. ' BB_LRC - Business Basic LRC() function.
  168. '
  169. ' Returns a one byte string containing the longitudinal redundancy checksum of a character string.
  170. '
  171. FUNCTION BB_LRC(ArgStr)
  172.  
  173.   LOCAL ArgStrLen, ScanPos, LRCVal
  174.   LRCVal = 0
  175.   ArgStrLen = LEN(ArgStr)
  176.   IF ArgStrLen THEN
  177.     FOR ScanPos = 1 TO ArgStrLen
  178.       LRCVal += LRCVal XOR ASC(MID(ArgStr, ScanPos, 1))
  179.     NEXT ScanPos
  180.     BB_LRC = CHR(LRCVal)
  181.   ELSE
  182.     BB_LRC = CHR(&H00)
  183.   END IF
  184.  
  185. END FUNCTION
  186.  
  187.  
  188. ' BB_PAD - Business BASIC PAD() funtion
  189. '
  190. ' Returns a character string of the length specified (NumExpr)
  191. '
  192. ' NOTE: StrExpr    = String to be processed
  193. '       NewLen     = Desired length of string
  194. '       HowToPad   = This parameter defines how to pad the string
  195. '                     0 - Pad on left  (right justify)
  196. '                     1 - Pad on right (left justify)
  197. '                     2 - Center in string
  198. '       StrPad     = First character of this string used to pad StrExpr
  199. '
  200. FUNCTION BB_PAD(StrExpr,NewLen,HowToPad,StrPad)
  201.  
  202.   LOCAL StrExpr,NewLen,HowToPad,StrPad,PadVal,StrExprLen,ResultStr,RLPLen
  203.   IF HowToPad = undef THEN
  204.     PadVal = 1
  205.   ELSE IF HowToPad = 0 OR UCASE(HowToPad) = "L" THEN
  206.     PadVal = 0
  207.   ELSE IF HowToPad = 1 OR UCASE(HowToPad) = "R" THEN  
  208.     PadVal = 1
  209.   ELSE IF HowToPad = 2 OR UCASE(HowToPad) = "C" THEN      
  210.     PadVal = 2
  211.   ELSE
  212.     BB_ERR = 41
  213.     BB_PAD = ""
  214.     EXIT FUNCTION
  215.   END IF
  216.  
  217.   IF StrPad = undef THEN StrPad = " "
  218.   StrExprLen = LEN(StrExpr)
  219.  
  220.   IF PadVal = 0 THEN
  221.     IF NewLen < StrExprLen THEN
  222.       ResultStr = RIGHT(StrExpr, NewLen)
  223.     ELSE
  224.       ResultStr = STRING(NewLen - StrExprLen, StrPad) & StrExpr
  225.     END IF
  226.   END IF
  227.  
  228.   IF PadVal = 1 THEN
  229.     IF NewLen < StrExprLen THEN
  230.       ResultStr = LEFT(StrExpr, NewLen)
  231.     ELSE
  232.       ResultStr = StrExpr & STRING(NewLen - StrExprLen, StrPad)
  233.     END IF
  234.   END IF
  235.  
  236.   IF PadVal = 2 THEN
  237.     IF NewLen < StrExprLen THEN
  238.       ResultStr = LEFT(StrExpr, NewLen)
  239.     ELSE
  240.       RLPLen = (NewLen - StrExprLen) / 2
  241.       IF RLPLen % 2 THEN
  242.         ResultStr = STRING(FIX(RLPLen),StrPad) & StrExpr & STRING(FIX(RLPLen) + 1,StrPad)
  243.       ELSE
  244.         ResultStr = STRING(RLPLen,StrPad) & StrExpr & STRING(RLPLen,StrPad)
  245.       END IF
  246.     ENDIF
  247.   END IF
  248.  
  249.   BB_PAD = ResultStr
  250.  
  251. END FUNCTION
  252.  
  253.  
  254. ' BB_POS - Business Basic POS() function
  255. '
  256. ' BB_POS follows these logic steps:
  257. '
  258. ' 1. If stringA or StringB is null, return 0
  259. ' 2. Start with first byte in stringB if intA is positive, or the Nth byte
  260. '    from the end of stringB if intA is negatine (-N).
  261. ' 3. If past either the begining or end of stringB then return 0
  262. '    (or occurrence count if intB is 0)
  263. ' 4. Compare stringA with the substring at the current position in stringB.
  264. '    The length of substring will be either the length of stringA or the
  265. '    remainder of stringB, whichever is shorter.
  266. ' 5. If a given releationship is true and if this was the Nth successful
  267. '    try (specified by intB=N) then return the current scan position.
  268. ' 6. If the relation was not satisfied then bump the scan position
  269. '    (possibly backwards if intA is negative) and go to step 3 and try again.
  270. '
  271. ' Relationship Operators:
  272. '
  273. ' "="   -   Equal To
  274. ' "<"   -   Less Than
  275. ' ">"   -   Greater Than
  276. ' "<="  -   Less Than Or Equal To
  277. ' ">="  -   Greater Than Or Equal To
  278. ' "<>"  -   Not Equal To
  279. ' ":"   -   Equal to Any
  280. ' "^"   -   Not Equal To Any
  281. '
  282. FUNCTION BB_POS(MatchStr,ScanStr,Relate,IncVal,OccurVal)
  283.  
  284.   LOCAL LenMatchStr,LenScanStr,ScanPos,OccurCnt,Item,StartVal,EndVal
  285.   IF Relate = undef THEN Relate = "="
  286.   IF IncVal = undef  THEN IncVal = 1
  287.   IF OccurVal = undef  THEN OccurVal = 1
  288.   LenMatchStr = LEN(MatchStr)
  289.   IF INSTR(":^", Relate) THEN LenMatchStr = 1
  290.   LenScanStr = LEN(ScanStr)
  291.   IF LenMatchStr = 0 OR LenScanStr = 0 OR OccurVal < 0 THEN
  292.     BB_POS = 0
  293.     EXIT FUNCTION
  294.   END IF
  295.   IF IncVal > 0 THEN
  296.     StartVal = 1
  297.     EndVal = LenScanStr
  298.   ELSE
  299.     StartVal = LenScanStr
  300.     EndVal = 1
  301.   END IF
  302.   FOR ScanPos = StartVal TO EndVal STEP IncVal
  303.     Item = MID(ScanStr, ScanPos, LenMatchStr)
  304.     IF Relate = "=" THEN
  305.       IF MatchStr = Item THEN OccurCnt += 1
  306.     ELSE IF Relate = "<" THEN
  307.       IF MatchStr < Item THEN OccurCnt += 1
  308.     ELSE IF Relate = ">" THEN
  309.       IF MatchStr > Item THEN OccurCnt += 1
  310.     ELSE IF Relate = "<=" OR Relate = "=<" THEN
  311.       IF MatchStr <= Item THEN OccurCnt += 1
  312.     ELSE IF Relate = ">=" OR Relate = "=>" THEN
  313.       IF MatchStr >= Item THEN OccurCnt += 1
  314.     ELSE IF Relate = "<>" OR Relate = "><" THEN
  315.       IF MatchStr <> Item THEN OccurCnt += 1
  316.     ELSE IF Relate = ":" THEN
  317.       IF INSTR(MatchStr, Item) THEN OccurCnt += 1
  318.     ELSE IF Relate = "^" THEN
  319.       IF NOT ISNUMERIC(INSTR(MatchStr, Item)) THEN OccurCnt += 1
  320.     ELSE
  321.       BB_POS = 0
  322.       EXIT FUNCTION
  323.     END IF
  324.     IF OccurVal > 0 THEN
  325.       IF OccurCnt = OccurVal THEN GOTO Done
  326.     END IF
  327.   NEXT ScanPos
  328.  
  329.   Done:
  330.  
  331.   IF OccurVal = 0 THEN
  332.     BB_POS = OccurCnt
  333.   ELSE IF OccurCnt THEN
  334.     BB_POS = ScanPos
  335.   ELSE
  336.     BB_POS = 0  
  337.   END IF
  338.  
  339. END FUNCTION
  340.  

bbtest.sb
Code: Script BASIC
  1. ' bb.inc test script
  2.  
  3. IMPORT bb.inc
  4.  
  5.  
  6. PRINT "HTA()\n"
  7. hta = BB_HTA("Script BASIC")
  8. PRINT hta,"\n\n"
  9. PRINT "ATH()\n"
  10. PRINT BB_ATH(hta),"\n\n"
  11. PRINT "PAD()\n"
  12. s = BB_PAD("BASIC",10,2," ")
  13. PRINT "|" & s & "|\n\n"
  14. PRINT "POS()\n"
  15. s = "The quick brown fox"
  16. ' yields 5
  17. PRINT BB_POS("q",s,"="),"\n"
  18. ' yields 0
  19. PRINT BB_POS("z",s,"="),"\n"
  20. ' yields 13
  21. PRINT BB_POS("o",s,"=") ,"\n"
  22. ' yields 18 - Scan from end (fox)
  23. PRINT BB_POS("o",s,"=",-1),"\n"
  24. ' yields 18 - Second occurrence (fox)
  25. PRINT BB_POS("o",s,"=",1,2),"\n"
  26. ' yields 13 - Checks every 2nd position
  27. PRINT BB_POS("o",s,"=",2),"\n"
  28. ' yields 6 - "u" is first char. > "r"
  29. PRINT BB_POS("r",s,"<"),"\n"
  30. PRINT "CVS()\n"
  31. s = "  sb  "
  32. PRINT "|" & BB_CVS(s,3," "),"|\n"
  33. PRINT "|" & BB_CVS(s,4,""),"|\n\n"
  34.  

Output

jrs@laptop:~/sb/sb22/PHB$ time scriba bbtest.sb
HTA()
536372697074204241534943

ATH()
Script BASIC

PAD()
|  BASIC  |

POS()
5
0
13
18
18
13
6
CVS()
|sb|
|  SB  |


real   0m0.026s
user   0m0.022s
sys   0m0.004s
jrs@laptop:~/sb/sb22/PHB$


115
MY-BASIC / MY-BASIC Extension Module
« on: April 24, 2015, 09:35:40 PM »
Here is an example of the new MY-BASIC extension module for Script BASIC. Attached is the Windows 32 bit and Linux (Ubuntu) 64 bit shared objects. (dll/so)

interface.c
Code: C
  1. // MY-BASIC - Script BASIC extension module
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <stdarg.h>
  7. #include <ctype.h>
  8. #include <math.h>
  9. #include <time.h>
  10. #include "../../basext.h"
  11. #include "cbasic.h"
  12.  
  13. #include "my_basic.h"
  14.  
  15.  
  16. /****************************
  17.  Extension Module Functions
  18. ****************************/
  19.  
  20. besVERSION_NEGOTIATE
  21.   RETURN_FUNCTION((int)INTERFACE_VERSION);
  22. besEND
  23.  
  24. besSUB_START
  25.   DIM AS long PTR p;
  26.   besMODULEPOINTER = besALLOC(sizeof(long));
  27.   IF (besMODULEPOINTER EQ NULL) THEN_DO RETURN_FUNCTION(0);
  28.   p = (long PTR)besMODULEPOINTER;
  29.   RETURN_FUNCTION(0);
  30. besEND
  31.  
  32. besSUB_FINISH
  33.   DIM AS long PTR p;
  34.   p = (long PTR)besMODULEPOINTER;
  35.   IF (p EQ NULL) THEN_DO RETURN_FUNCTION(0);
  36.   RETURN_FUNCTION(0);
  37. besEND
  38.  
  39.  
  40. /********************
  41.  MY-BASIC Functions
  42. ********************/
  43.  
  44. static struct mb_interpreter_t* bas = 0;
  45.  
  46. static int watch(struct mb_interpreter_t* s, void** l) {
  47.   int result = MB_FUNC_OK;
  48.   int_t arg = 0;
  49.   mb_assert(s && l);
  50.   mb_check(mb_attempt_open_bracket(s, l));
  51.   mb_check(mb_pop_int(s, l, &arg)); // That's it!
  52.   mb_check(mb_attempt_close_bracket(s, l));
  53.   // arg is what you want.
  54.   return result;
  55. }
  56.  
  57. besFUNCTION(mbas_init)
  58.   besRETURN_LONG(mb_init());
  59. besEND
  60.  
  61. besFUNCTION(mbas_dispose)
  62.   besRETURN_LONG(mb_dispose());
  63. besEND
  64.  
  65. besFUNCTION(mbas_open)
  66.   besRETURN_LONG(mb_open(AT bas));
  67. besEND
  68.  
  69. besFUNCTION(mbas_close)
  70.   besRETURN_LONG(mb_close(AT bas));
  71. besEND
  72.  
  73. besFUNCTION(mbas_load_str)
  74.   DIM AS const char PTR pgm;
  75.   besARGUMENTS("z")
  76.     AT pgm
  77.   besARGEND
  78.   besRETURN_LONG(mb_load_string(bas, pgm));
  79. besEND
  80.  
  81. besFUNCTION(mbas_load_file)
  82.   DIM AS const char PTR pgm;
  83.   besARGUMENTS("z")
  84.     AT pgm
  85.   besARGEND
  86.   besRETURN_LONG(mb_load_file(bas, pgm));
  87. besEND
  88.  
  89. besFUNCTION(mbas_run)
  90.   besRETURN_LONG(mb_run(bas));
  91. besEND
  92.  
  93. besFUNCTION(mbas_reset)
  94.   besRETURN_LONG(mb_reset(bas, false));
  95. besEND
  96.  
  97. besFUNCTION(mbas_getint)
  98.   DIM AS mb_value_t mbval;
  99.   DIM AS const char PTR varname;
  100.   besARGUMENTS("z")
  101.     AT varname
  102.   besARGEND
  103.   mbval.type = MB_DT_INT;
  104.   mb_debug_get(bas, varname, &mbval);
  105.   besRETURN_LONG(mbval.value.integer);
  106. besEND
  107.  
  108. besFUNCTION(mbas_getdbl)
  109.   DIM AS mb_value_t mbval;
  110.   DIM AS const char PTR varname;
  111.   besARGUMENTS("z")
  112.     AT varname
  113.   besARGEND
  114.   mbval.type = MB_DT_REAL;
  115.   mb_debug_get(bas, varname, &mbval);
  116.   besRETURN_DOUBLE(mbval.value.float_point);
  117. besEND
  118.  
  119. besFUNCTION(mbas_getstr)
  120.   DIM AS mb_value_t mbval;
  121.   DIM AS const char PTR varname;
  122.   besARGUMENTS("z")
  123.     AT varname
  124.   besARGEND
  125.   mbval.type = MB_DT_STRING;
  126.   mb_debug_get(bas, varname, &mbval);
  127.   besRETURN_STRING(mbval.value.string);
  128. besEND
  129.  
  130. besFUNCTION(mbas_setint)
  131.   DIM AS VARIABLE Argument;
  132.   DIM AS mb_value_t mbval;
  133.   DIM AS int usrval, i, rtnval;
  134.   DIM AS const char PTR varname;
  135.   IF (besARGNR < 2) THEN_DO RETURN_FUNCTION(EX_ERROR_TOO_FEW_ARGUMENTS);
  136.   DEF_FOR (i = 1 TO i <= 2 STEP INCR i)
  137.   BEGIN_FOR
  138.     Argument = besARGUMENT(i);
  139.     besDEREFERENCE(Argument);
  140.     IF (i EQ 1) THEN_DO varname = STRINGVALUE(Argument);
  141.     IF (i EQ 2) THEN_DO usrval = LONGVALUE(Argument);
  142.   NEXT
  143.   mbval.type = MB_DT_INT;
  144.   mbval.value.integer = usrval;
  145.   rtnval = mb_debug_set(bas, varname, mbval);
  146.   besRETURN_LONG(rtnval);
  147. besEND
  148.  
  149. besFUNCTION(mbas_setdbl)
  150.   DIM AS VARIABLE Argument;
  151.   DIM AS mb_value_t mbval;
  152.   DIM AS int i, rtnval;
  153.   DIM AS double usrval;
  154.   DIM AS const char PTR varname;
  155.   IF (besARGNR < 2) THEN_DO RETURN_FUNCTION(EX_ERROR_TOO_FEW_ARGUMENTS);
  156.   DEF_FOR (i = 1 TO i <= 2 STEP INCR i)
  157.   BEGIN_FOR
  158.     Argument = besARGUMENT(i);
  159.     besDEREFERENCE(Argument);
  160.     IF (i EQ 1) THEN_DO varname = STRINGVALUE(Argument);
  161.     IF (i EQ 2) THEN_DO usrval = DOUBLEVALUE(Argument);
  162.   NEXT
  163.   mbval.type = MB_DT_REAL;
  164.   mbval.value.float_point = usrval;
  165.   rtnval = mb_debug_set(bas, varname, mbval);
  166.   besRETURN_LONG(rtnval);
  167. besEND
  168.  
  169. besFUNCTION(mbas_setstr)
  170.   DIM AS VARIABLE Argument;
  171.   DIM AS mb_value_t mbval;
  172.   DIM AS int i, rtnval;
  173.   DIM AS const char PTR varname;
  174.   DIM AS const char PTR usrval;
  175.   IF (besARGNR < 2) THEN_DO RETURN_FUNCTION(EX_ERROR_TOO_FEW_ARGUMENTS);
  176.   DEF_FOR (i = 1 TO i <= 2 STEP INCR i)
  177.   BEGIN_FOR
  178.     Argument = besARGUMENT(i);
  179.     besDEREFERENCE(Argument);
  180.     IF (i EQ 1) THEN_DO varname = STRINGVALUE(Argument);
  181.     IF (i EQ 2) THEN_DO usrval = STRINGVALUE(Argument);
  182.   NEXT
  183.   mbval.type = MB_DT_STRING;
  184.   usrval = mb_memdup(usrval, strlen(usrval) + 1);
  185.   mbval.value.string = usrval;
  186.   besRETURN_LONG(mb_debug_set(bas, varname, mbval));
  187. besEND
  188.  

mbvars.sb
Code: Script BASIC
  1. DECLARE SUB mb_init ALIAS "mbas_init" LIB "mb"
  2. DECLARE SUB mb_dispose ALIAS "mbas_dispose" LIB "mb"
  3. DECLARE SUB mb_open ALIAS "mbas_open" LIB "mb"
  4. DECLARE SUB mb_close ALIAS "mbas_close" LIB "mb"
  5. DECLARE SUB mb_load_str ALIAS "mbas_load_str" LIB "mb"
  6. DECLARE SUB mb_load_file ALIAS "mbas_load_file" LIB "mb"
  7. DECLARE SUB mb_run ALIAS "mbas_run" LIB "mb"
  8. DECLARE SUB mb_getint ALIAS "mbas_getint" LIB "mb"
  9. DECLARE SUB mb_getdbl ALIAS "mbas_getdbl" LIB "mb"
  10. DECLARE SUB mb_getstr ALIAS "mbas_getstr" LIB "mb"
  11. DECLARE SUB mb_setint ALIAS "mbas_setint" LIB "mb"
  12. DECLARE SUB mb_setdbl ALIAS "mbas_setdbl" LIB "mb"
  13. DECLARE SUB mb_setstr ALIAS "mbas_setstr" LIB "mb"
  14. DECLARE SUB mb_reset ALIAS "mbas_reset" LIB "mb"
  15.  
  16. mb_init
  17. mb_open
  18. mb_load_file "setvars.bas"
  19. mb_run
  20. mb_setint "A", 123
  21. mb_setdbl "B", 1.23
  22. mb_setstr "C$", "One,Two,Three"
  23. PRINT mb_getint("A"),"\n"
  24. PRINT FORMAT("%g\n", mb_getdbl("B"))
  25. PRINT mb_getstr("C$"),"\n"
  26. mb_close
  27. mb_dispose
  28.  

setvars.bas
Code: [Select]
a = 0
b = 0.0
c$ = ""

Output - Linux 64 bit

jrs@laptop:~/sb/sb22/mybasic$ time scriba mbvars.sb
123
1.23
One,Two,Three

real   0m0.006s
user   0m0.005s
sys   0m0.005s
jrs@laptop:~/sb/sb22/mybasic$


Output - Windows 32 bit

C:\sb22\mybasic>scriba mbvars.sb
123
1.23
One,Two,Three

C:\sb22\mybasic>


116
TinyScheme / Re: TinyScheme Extension Module
« on: April 21, 2015, 07:12:46 PM »
Here is a Prime Number example done in TinyScheme by Mike (FBSL author)

Code: Script BASIC
  1. IMPORT ts.inc
  2.  
  3. sc = TS_New()
  4. TS_Cmd sc, "(load \"init.scm\")"
  5. ts_src = """
  6. (define (prime? n)
  7.  (if (< n 4) (> n 1)
  8.      (and (odd? n)
  9.           (let loop ((k 3))
  10.             (or (> (* k k) n)
  11.                 (and (positive? (remainder n k))
  12.                      (loop (+ k 2))))))))
  13.  
  14. (define (main)
  15.  (do ((i 3 (+ i 2)))
  16.    ((> i 4999) #t)
  17.      (if (prime? i) (begin (display i)(display " ")) "")
  18.  )
  19.  (newline)
  20. )
  21.  
  22. (main)
  23. """
  24. PRINT TS_Cmd(sc, ts_src),"\n"
  25. TS_Close sc
  26.  

Output

jrs@laptop:~/sb/sb22/TS$ time scriba tspnmike.sb
3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401 409 419 421 431 433 439 443 449 457 461 463 467 479 487 491 499 503 509 521 523 541 547 557 563 569 571 577 587 593 599 601 607 613 617 619 631 641 643 647 653 659 661 673 677 683 691 701 709 719 727 733 739 743 751 757 761 769 773 787 797 809 811 821 823 827 829 839 853 857 859 863 877 881 883 887 907 911 919 929 937 941 947 953 967 971 977 983 991 997 1009 1013 1019 1021 1031 1033 1039 1049 1051 1061 1063 1069 1087 1091 1093 1097 1103 1109 1117 1123 1129 1151 1153 1163 1171 1181 1187 1193 1201 1213 1217 1223 1229 1231 1237 1249 1259 1277 1279 1283 1289 1291 1297 1301 1303 1307 1319 1321 1327 1361 1367 1373 1381 1399 1409 1423 1427 1429 1433 1439 1447 1451 1453 1459 1471 1481 1483 1487 1489 1493 1499 1511 1523 1531 1543 1549 1553 1559 1567 1571 1579 1583 1597 1601 1607 1609 1613 1619 1621 1627 1637 1657 1663 1667 1669 1693 1697 1699 1709 1721 1723 1733 1741 1747 1753 1759 1777 1783 1787 1789 1801 1811 1823 1831 1847 1861 1867 1871 1873 1877 1879 1889 1901 1907 1913 1931 1933 1949 1951 1973 1979 1987 1993 1997 1999 2003 2011 2017 2027 2029 2039 2053 2063 2069 2081 2083 2087 2089 2099 2111 2113 2129 2131 2137 2141 2143 2153 2161 2179 2203 2207 2213 2221 2237 2239 2243 2251 2267 2269 2273 2281 2287 2293 2297 2309 2311 2333 2339 2341 2347 2351 2357 2371 2377 2381 2383 2389 2393 2399 2411 2417 2423 2437 2441 2447 2459 2467 2473 2477 2503 2521 2531 2539 2543 2549 2551 2557 2579 2591 2593 2609 2617 2621 2633 2647 2657 2659 2663 2671 2677 2683 2687 2689 2693 2699 2707 2711 2713 2719 2729 2731 2741 2749 2753 2767 2777 2789 2791 2797 2801 2803 2819 2833 2837 2843 2851 2857 2861 2879 2887 2897 2903 2909 2917 2927 2939 2953 2957 2963 2969 2971 2999 3001 3011 3019 3023 3037 3041 3049 3061 3067 3079 3083 3089 3109 3119 3121 3137 3163 3167 3169 3181 3187 3191 3203 3209 3217 3221 3229 3251 3253 3257 3259 3271 3299 3301 3307 3313 3319 3323 3329 3331 3343 3347 3359 3361 3371 3373 3389 3391 3407 3413 3433 3449 3457 3461 3463 3467 3469 3491 3499 3511 3517 3527 3529 3533 3539 3541 3547 3557 3559 3571 3581 3583 3593 3607 3613 3617 3623 3631 3637 3643 3659 3671 3673 3677 3691 3697 3701 3709 3719 3727 3733 3739 3761 3767 3769 3779 3793 3797 3803 3821 3823 3833 3847 3851 3853 3863 3877 3881 3889 3907 3911 3917 3919 3923 3929 3931 3943 3947 3967 3989 4001 4003 4007 4013 4019 4021 4027 4049 4051 4057 4073 4079 4091 4093 4099 4111 4127 4129 4133 4139 4153 4157 4159 4177 4201 4211 4217 4219 4229 4231 4241 4243 4253 4259 4261 4271 4273 4283 4289 4297 4327 4337 4339 4349 4357 4363 4373 4391 4397 4409 4421 4423 4441 4447 4451 4457 4463 4481 4483 4493 4507 4513 4517 4519 4523 4547 4549 4561 4567 4583 4591 4597 4603 4621 4637 4639 4643 4649 4651 4657 4663 4673 4679 4691 4703 4721 4723 4729 4733 4751 4759 4783 4787 4789 4793 4799 4801 4813 4817 4831 4861 4871 4877 4889 4903 4909 4919 4931 4933 4937 4943 4951 4957 4967 4969 4973 4987 4993 4999

real   0m0.394s
user   0m0.392s
sys   0m0.000s
jrs@laptop:~/sb/sb22/TS$


117
What's New / Re: Perl Extension Module
« on: April 08, 2015, 11:56:17 PM »
Here is the above Perl example using the Script BASIC re (regx) extension module. This example is in 32 Windows XP as re seq faults under 64 bit.  :-\

Code: Script BASIC
  1. IMPORT re.bas
  2.  
  3. SUB test(regx, target)
  4.   IF re::match(target,regx) THEN
  5.     PRINT regx," is in ",target,"      <",re::dollar(0),">\n"
  6.   ELSE
  7.     PRINT regx," is NOT in ",target,"\n"
  8.   END IF
  9. END SUB
  10.  
  11. test("st.v.", "steve was here")
  12. test("st.v.", "kitchen stove")
  13. test("st.v.", "kitchen store")
  14.  
  15. PRINTNL  
  16.  

Output


C:\sb22\test>scriba testregx.sb
st.v. is in steve was here      <steve>
st.v. is in kitchen stove      <stove>
st.v. is NOT in kitchen store


C:\sb22\test>

118
What's New / Re: Perl Extension Module
« on: April 08, 2015, 09:41:40 PM »
The following is a Perl example of using regular expression parsing. (regx)

Code: Script BASIC
  1. DECLARE SUB pl_Init ALIAS "pl_Init" LIB "sbperl"
  2. DECLARE SUB pl_Eval ALIAS "pl_Eval" LIB "sbperl"
  3. DECLARE SUB pl_GetInt ALIAS "pl_GetInt" LIB "sbperl"
  4. DECLARE SUB pl_GetDbl ALIAS "pl_GetDbl" LIB "sbperl"
  5. DECLARE SUB pl_GetStr ALIAS "pl_GetStr" LIB "sbperl"
  6. DECLARE SUB pl_Destroy ALIAS "pl_Destroy" LIB "sbperl"
  7.  
  8. pl_Init
  9.  
  10. pl_code = """
  11. sub test($$)
  12.         {
  13.         my $lookfor = shift;
  14.         my $string = shift;
  15.         print "\n$lookfor ";
  16.         if($string =~ m/($lookfor)/)
  17.                 {
  18.                 print " is in ";
  19.                 }
  20.         else
  21.                 {
  22.                 print " is NOT in ";
  23.                 }
  24.         print "$string.";
  25.         if(defined($1))
  26.                 {
  27.                 print "      <$1>";
  28.                 }
  29.         print "\n";
  30.         }
  31.  
  32. test("st.v.", "steve was here");
  33. test("st.v.", "kitchen stove");
  34. test("st.v.", "kitchen store");
  35. """
  36. PRINTNL
  37.  
  38. pl_Eval pl_code
  39.  
  40. pl_Destroy
  41.  

Output


jrs@laptop:~/sb/sb22/test$ time scriba perlmatch.sb


st.v.  is in steve was here.      <steve>

st.v.  is in kitchen stove.      <stove>

st.v.  is NOT in kitchen store.

real   0m0.004s
user   0m0.000s
sys   0m0.004s
jrs@laptop:~/sb/sb22/test$

119
What's New / Re: Perl Extension Module
« on: April 08, 2015, 10:45:31 AM »
Perl has an console interactive (debugger) mode you can play with.


jrs@laptop:~/sb/sb22/test$ perl -d -e 1

Loading DB routines from perl5db.pl version 1.39_10
Editor support available.

Enter h or 'h h' for help, or 'man perldebug' for more help.

main::(-e:1):   1
  DB<1> h
List/search source lines:               Control script execution:
  l [ln|sub]  List source code            T           Stack trace
  - or .      List previous/current line  s [expr]    Single step [in expr]
  v [line]    View around line            n [expr]    Next, steps over subs
  f filename  View source in file         <CR/Enter>  Repeat last n or s
  /pattern/ ?patt?   Search forw/backw    r           Return from subroutine
  M           Show module versions        c [ln|sub]  Continue until position
Debugger controls:                        L           List break/watch/actions
  o [...]     Set debugger options        t [n] [expr] Toggle trace [max depth] ][trace expr]
  <[<]|{[{]|>[>] [cmd] Do pre/post-prompt b [ln|event|sub] [cnd] Set breakpoint
  ! [N|pat]   Redo a previous command     B ln|*      Delete a/all breakpoints
  H [-num]    Display last num commands   a [ln] cmd  Do cmd before line
  = [a val]   Define/list an alias        A ln|*      Delete a/all actions
  h [db_cmd]  Get help on command         w expr      Add a watch expression
  h h         Complete help page          W expr|*    Delete a/all watch exprs
  |[|]db_cmd  Send output to pager        ![!] syscmd Run cmd in a subprocess
  q or ^D     Quit                        R           Attempt a restart
Data Examination:     expr     Execute perl code, also see: s,n,t expr
  x|m expr       Evals expr in list context, dumps the result or lists methods.
  p expr         Print expression (uses script's current package).
  S [[!]pat]     List subroutine names [not] matching pattern
  V [Pk [Vars]]  List Variables in Package.  Vars can be ~pattern or !pattern.
  X [Vars]       Same as "V current_package [Vars]".  i class inheritance tree.
  y [n [Vars]]   List lexicals in higher scope <n>.  Vars same as V.
  e     Display thread id     E Display all thread ids.
For more help, type h cmd_letter, or run man perldebug for all docs.
  DB<1>

120
What's New / Re: Perl Extension Module
« on: April 08, 2015, 09:11:33 AM »
Here is the current Script BASIC interface.c (sbperl.so) extension module source.

Perl Embedding API Documentation

Code: C
  1. /* Perl - Script BASIC extension module
  2.  
  3. UXLIBS: -lperl
  4.  
  5. */
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <ctype.h>
  11. #include <math.h>
  12. #include <time.h>
  13. #include "../../basext.h"
  14. #include "cbasic.h"
  15.  
  16. #include <EXTERN.h>
  17. #include <perl.h>
  18.  
  19. static PerlInterpreter *my_perl;
  20.  
  21. /****************************
  22.  Extension Module Functions
  23. ****************************/
  24.  
  25. besVERSION_NEGOTIATE
  26.   RETURN_FUNCTION((int)INTERFACE_VERSION);
  27. besEND
  28.  
  29. besSUB_START
  30.   DIM AS long PTR p;
  31.   besMODULEPOINTER = besALLOC(sizeof(long));
  32.   IF (besMODULEPOINTER EQ NULL) THEN_DO RETURN_FUNCTION(0);
  33.   p = (long PTR)besMODULEPOINTER;
  34.   RETURN_FUNCTION(0);
  35. besEND
  36.  
  37. besSUB_FINISH
  38.   DIM AS long PTR p;
  39.   p = (long PTR)besMODULEPOINTER;
  40.   IF (p EQ NULL) THEN_DO RETURN_FUNCTION(0);
  41.   RETURN_FUNCTION(0);
  42. besEND
  43.  
  44.  
  45. /****************
  46.  Perl Functions
  47. ****************/
  48.  
  49. besFUNCTION(pl_Init)
  50.   DIM AS char *embedding[] = { "", "-e", "0" };
  51.   my_perl = perl_alloc();
  52.   perl_construct(my_perl);
  53.   perl_parse(my_perl, NULL, 3, embedding, NULL);
  54.   perl_run(my_perl);
  55.   besRETURN_LONG(my_perl);
  56. besEND
  57.  
  58. besFUNCTION(pl_Eval)
  59.   DIM AS const char PTR cmdstr;
  60.   besARGUMENTS("z")
  61.     AT cmdstr
  62.   besARGEND
  63.   eval_pv(cmdstr, TRUE);
  64.   besRETURNVALUE = NULL;
  65. besEND
  66.  
  67. besFUNCTION(pl_GetInt)
  68.   DIM AS const char PTR cmdstr;
  69.   DIM AS int rtnval;
  70.   besARGUMENTS("z")
  71.     AT cmdstr
  72.   besARGEND
  73.   rtnval = SvIV(get_sv(cmdstr, FALSE));
  74.   besRETURN_LONG(rtnval);
  75. besEND
  76.  
  77. besFUNCTION(pl_GetDbl)
  78.   DIM AS const char PTR cmdstr;
  79.   DIM AS double rtnval;
  80.   besARGUMENTS("z")
  81.     AT cmdstr
  82.   besARGEND
  83.   rtnval = SvNV(get_sv(cmdstr, FALSE));
  84.   besRETURN_DOUBLE(rtnval);
  85. besEND
  86.  
  87. besFUNCTION(pl_GetStr)
  88.   DIM AS const char PTR cmdstr;
  89.   DIM AS char PTR rtnval;
  90.   DIM AS STRLEN n_a;
  91.   besARGUMENTS("z")
  92.     AT cmdstr
  93.   besARGEND
  94.   rtnval = SvPV(get_sv(cmdstr, FALSE), n_a);
  95.   besRETURN_STRING(rtnval);
  96. besEND
  97.  
  98. besFUNCTION(pl_Destroy)
  99.   perl_destruct(my_perl);
  100.   perl_free(my_perl);
  101.   besRETURNVALUE = NULL;
  102. besEND
  103.  

Pages: 1 ... 6 7 [8] 9 10 ... 59