Add Embedded Scripting to Your C++ Application

Scripting tends to come on late in the life of your application. About the time that the feature-set is approaching completeness and customers are starting to get happy, you get hit with a new requirement. Take the scenario when the boss marches in and tells you how many more copies the company could sell if only it could allow users to automate report writing themselves. Or perhaps after rewriting or tweaking reports for the fourth time this week, you have the epiphany yourself. Other times, you find that ordinary macro recording and playback tools fail to meet the needs for rigorous quality assurance testing. What you really want is a test-rig that can run your algorithm 100 or 1,000 times with minor variations to really give it a good shakedown. If you're a game developer, you may want to expose part of your engine to allow third-party developers or your own in-house crew to design new AI behaviors for the zombies. You get the idea: Nearly every application can benefit from a scriptable interface.

The C Scripting Language (CSL)

The C Scripting Language (CSL) is a well-structured and easy-to-learn script programming language available for Windows 95 thru Windows XP, OS/2, Linux, FreeBSD, and other variants of UNIX. CSL follows the C syntax style very closely so programmers accustomed to C, C++, or Java will be immediately comfortable. CSL can be used as a standalone interpreter or is embeddable into your C/C++ application.

As an interpreter, you write the program with your favorite editor and run it directly like any shell script or run it through your Web server's CGI-BIN interface. The CSL C interface can be embedded with most C/C++ compilers; the C++ class library is available for use with Visual C++ 5.0 or later, IBM VisualAge, Borland C++ 5.x, and GCC.

CSL in Standalone Mode and Basic Syntax

Investigating standalone mode is perhaps the quickest way to get your feet wet and convince yourself that the CSL language is both familiar and friendly. Take the following short script as your first exploration into CSL. It computes the average of several numbers passed on the command line:

Running a CSL Script Inside Your App

Related Articles

Take a look at the minimal amount of code needed for your application to embed CSL. By embed, I of course mean loading and executing arbitrary CSL scripts at runtime. You'll see more advanced usage later in this article, but you have to crawl before you can fly. Whether the script was explicitly loaded by the user choosing File | Open Script in your app, pulled from a database, or dynamically generated is irrelevant to this part of the exercise. The following example uses a static in-memory script just to make it crystal clear how the mechanism works:

Line #15 opens a handle to the CSL system, which you later close in line #37. This handle is the basis for all your CSL operations. The meat of this example is lines #21-31, which load a particular script into a module called "Embed". This script contains a single function called foo() that executes three "sys" functions and then exits. Although not terribly useful, the example gets the point across. The output appears below:

D:\csl\VV>testcsl
Using csl version 4.04
compile a script from memory
call 'foo' within compiled script
current time is 15:31:46
current dir is D:\csl\VV
closing csl

Calling a C Function from Your App Inside CSL

I know what you're thinking: I haven't seen anything yet that I couldn't do with a system() call to a Perl script or something. This time around, the example exposes a C function from within your application, which is called from CSL. The C function is foo_factorial(), which computes N factorial as expected and is called csl_factorial() inside the CSL script (just to prove that it can be an independent namespace if needed):

First, look at the new CSL-callable function in lines #10-30. Every CSL-callable function will have this same signature: ZExportAPI(void) and one argument of type ZcslHandle(). Once inside the function, you can retrieve its actual arguments by name with ZcslGet() on line #18. You do your function execution stuff, which in this case computes the factorial, and then return the value to CSL with ZCslSetResult() in line #29.

Back in the main() function, the new thing to notice is your dynamic installation of the CSL-callable function via ZCslAddFunc() on line #42. Again, you referred to the formal parameter "p1" by name back on line #18. You actually call csl_factorial() right in the middle of your in-memory script, as shown on line #53. Again, this need not have been an in-memory script; it could have been typed into an edit control in your application, loaded from a file, and so forth. When you run the program, you'll see the output of 5 factorial (!):

Using csl version 4.04
compile a script from memory
call 'foo' within compiled script
current time is 16:29:08
5! is 24

Other Embedded Interpreters to Consider

It's possible that this approach is backwards from what you want. If you want to turn your application inside out and make all of its guts callable from other scripting languages, such as Perl, Python, and Tcl/TK, what you want is SWIG (Software Interface Generator).

S-Lang library by John E. Davis is another C-like environment with facilities required by interactive applications such as display/screen management, keyboard input, keymaps, and so on. The most exciting feature of the library is the slang interpreter that you can easily embed into a program to make it extensible.

EiC is a freely available C language interpreter in both source and binary form. EiC allows you to write C programs, and then "execute" them as if they were a script (like a Perl script or a shell script). EiC can be run in several different modes: (1) interactively, (2) non-interactively, (3) in scripting mode, and (4) embedded as an interpreter in other systems.

Everyone Could Use Scripting

CSL provides all the required functionality for plugging a scripting language into your app with minimal hassle. You can export functions from your application with relative ease to make them callable within CSL scripts. The CSL language will be instantly usable by any end-user with C or Java experience. It also contains a rich set of librariesincluding database accessthat this article hasn't even described.

Once you try embedded scripting in your app, you and your end-users (or zombies) will wonder how you ever got along without it.

About the Author

Victor Volkman has been writing for C/C++ Users Journal and other programming journals since the late 1980s. He is a graduate of Michigan Tech and a faculty advisor board member for Washtenaw Community College CIS department. Volkman is the editor of numerous books, including C/C++ Treasure Chest and is the owner of Loving Healing Press. He can help you in your quest for open source tools and libraries, just drop an e-mail to sysop@HAL9K.com.

Please enable Javascript in your browser, before you post the comment! Now Javascript is disabled.