[SparForte][Banner]
[Top Main Menu] Intro | Tutorials | Reference | Packages | Examples | Contributors   [Back Page]      [Next Page]  

Calling SparForte from C: A Tutorial

Certain kinds of programs, such as games or the Gimp, allow users to write their own scripts   For example, there are game scripts that change the behavior of enemies, or Gimp scripts that apply imaging effects to a photograph.  SparForte can be used as a scripting language for these kind of applications.

There is a special pragma, pragma restriction( no_external_commands ), that will disable all operating system commands.   If you are using SparForte strictly as a scripting language, this pragma will guarantee that your SparForte scripts will be portable to other operating systems.  In addition, if you use pragma ada_95, your scripts will have less errors and commonly used scripts can compiled with GCC Ada (with only minor changes) for extra speed.

Scripts that interact with other programs must be able to share data with the SparForte scripts.  In order to share variables with SparForte, you will have to export your variables as environment variables or add the appropriate declarations to the scripts before you run them with SparForte.

A simple C example called scripting.c is included in the examples directory.

/* -------------------------------------------- */
/* scripting.c                                  */
/*                                              */
/* An example of using SparForte as a scripting      */
/* language for a C program.                    */
/* -------------------------------------------- */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>

int main() {
  char i_string[255];
   int i;
   FILE *f;

/* assign a value to i */

  i = 5;

/* export i */

  sprintf( i_string, "i_string=%d", i ); // convert i to a string
  if ( putenv( i_string ) != 0 )         // add i to the environment
    printf( "putenv i_string failed: %s\n", strerror( errno ) );

/* Create the script to run */

  f = fopen( "scripting_example.sp", "w" );
  fprintf( f, "%s\n", "pragma restriction( no_external_commands );" );
  fprintf( f, "%s\n", "pragma ada_95;" );
  fprintf( f, "%s\n", "procedure scripting_example is" );
  fprintf( f, "%s\n", "i : integer := numerics.value( i_string );" );
  fprintf( f, "%s\n", "begin" );
  fprintf( f, "%s\n", "put_line( i * 2 );" );
  fprintf( f, "%s\n", "end scripting_example;" );
  fclose( f );

/* Run the script. If successful, delete script */
 

Example: Hello World
 
[Right Submenu]

 Command Prompt Tutorial 1: SparForte as a Calculator

 Command Prompt Tutorial 2: Basic Shell Commands

 Command Prompt Tutorial 3: Working with Databases

 Script Tutorial 1: Basic Commands Scripts

 Script Tutorial 2: Intermediate Program Scripts

 Script Tutorial 3: Data Types

 Template Tutorial 1: Basic Templates

 Template Tutorial 2: Intermediate Templates

 GCC Tutorial: Compiling SparForte Scripts

 Debugging Tutorial - Using the SparForte Debugger

 Creating a Profile Script

 Calling SparForte from C: A Tutorial

 SparForte For PHP Developers

 SparForte Best Practices

[Back to Top] Back To Top [Small Forte Symbol]