Main Page | Class List | Directories | File List | Class Members | File Members

calc.c File Reference

#include "config.h"
#include <stdio.h>
#include <math.h>
#include "slang.h"

Go to the source code of this file.

Classes

struct  My_Struct_Type

Defines

#define BENCHMARK_TESTING   0

Functions

static int open_readline (void)
static void close_readline (void)
static void help (void)
static void quit_calc (void)
static void exit_calc (int *status)
static int add_my_struct_type (void)
static int take_input (void)
int main (int argc, char **argv)
static char * read_using_readline (SLang_Load_Type *x)

Variables

static SLang_Intrin_Fun_Type Calc_Intrinsics []
static My_Struct_Type My_Struct
static My_Struct_TypeMy_Struct_Ptr = &My_Struct
static SLang_IStruct_Field_Type My_Struct_Field_Table []
static SLrline_TypeCalc_RLI
SLang_Load_TypeReadline_Load_Object


Define Documentation

#define BENCHMARK_TESTING   0
 

Definition at line 10 of file calc.c.


Function Documentation

static int add_my_struct_type void   )  [static]
 

Definition at line 83 of file calc.c.

References SLadd_istruct_table().

Referenced by main().

00084 {
00085    return SLadd_istruct_table (My_Struct_Field_Table,
00086                                (VOID_STAR) &My_Struct_Ptr,
00087                                "MyS");
00088 }

static void close_readline void   )  [static]
 

Definition at line 191 of file calc.c.

References SLang_reset_tty(), SLdeallocate_load_type(), and SLrline_close().

00192 {
00193    if (Readline_Load_Object != NULL)
00194      {
00195         SLdeallocate_load_type (Readline_Load_Object);
00196         Readline_Load_Object = NULL;
00197      }
00198    SLrline_close (Calc_RLI);
00199    Calc_RLI = NULL;
00200    SLang_reset_tty ();
00201 }

static void exit_calc int *  status  )  [static]
 

Definition at line 40 of file calc.c.

References close_readline().

00041 {
00042    close_readline ();
00043    exit (*status);
00044 }

static void help void   )  [static]
 

Definition at line 23 of file calc.c.

References SLang_run_hooks().

00024 {
00025    puts("ALL statements MUST be terminated with a ';' character, e.g., quit();\n");
00026    puts("Available functions:");
00027    puts("  cos, sin, tan, atan, acos, asin, exp, log, sqrt, fabs, log10, pow, PI, E");
00028    puts("\nas well as other intrinsic S-Lang functions.");
00029    puts("See S-Lang language documentation for further details.\n");
00030    SLang_run_hooks ("calc_help", 0);
00031 }

int main int  argc,
char **  argv
 

Definition at line 92 of file calc.c.

References add_my_struct_type(), open_readline(), SLadd_intrin_fun_table(), SLang_get_error(), SLang_init_all(), SLang_load_file(), SLang_restart(), SLang_Traceback, SLKeyBoard_Quit, and take_input().

00093 {
00094    if ((-1 == SLang_init_all ())
00095        /* || (-1 == SLang_init_import ()) / * dynamic linking */
00096        || (-1 == add_my_struct_type ())
00097        || (-1 == SLadd_intrin_fun_table (Calc_Intrinsics, NULL)))
00098      {
00099         fprintf(stderr, "Unable to initialize S-Lang.\n");
00100         exit (1);
00101      }
00102 
00103    SLang_Traceback = 1;
00104 
00105    if (argc == 1)
00106      SLang_load_file("calc.sl");
00107 
00108    while (--argc && !SLang_get_error ())
00109      {
00110         argv++;
00111         SLang_load_file (*argv);
00112      }
00113 
00114    fputs("Type 'help();' for help and a list of available functions.\n", stdout);
00115    fputs("All statements must end with a ';' character, e.g, 2*7+3;\n", stdout);
00116    fputs("\nType `quit;' to exit this program.\n", stdout);
00117 
00118    if (-1 == open_readline ())
00119      return 1;
00120 
00121    while (1)
00122      {
00123         if (SLang_get_error ())
00124           {
00125              SLang_restart (1);
00126              /* SLang_set_error (0); */
00127           }
00128         SLKeyBoard_Quit = 0;
00129         take_input ();
00130      }
00131 }

static int open_readline void   )  [static]
 

Definition at line 165 of file calc.c.

References close_readline(), SLang_Load_Type::read, read_using_readline(), SL_RLINE_BLINK_MATCH, SLallocate_load_type(), SLang_init_tty(), SLang_set_abort_signal(), and SLrline_open().

00166 {
00167    if (SLang_init_tty (-1, 0, 1))
00168      {
00169         fprintf(stderr, "Unable to initialize tty.\n");
00170         return -1;
00171      }
00172    SLang_set_abort_signal (NULL);
00173 
00174    if (Calc_RLI == NULL)
00175      {
00176         Calc_RLI = SLrline_open (80, SL_RLINE_BLINK_MATCH);
00177         if (Calc_RLI == NULL)
00178           return -1;
00179      }
00180 
00181    if (NULL == (Readline_Load_Object = SLallocate_load_type ("<stdin>")))
00182      {
00183         close_readline ();
00184         return -1;
00185      }
00186 
00187    Readline_Load_Object->read = read_using_readline;
00188    return 0;
00189 }

static void quit_calc void   )  [static]
 

Definition at line 34 of file calc.c.

References close_readline().

00035 {
00036    close_readline ();
00037    exit (1);
00038 }

static char* read_using_readline SLang_Load_Type x  )  [static]
 

Definition at line 138 of file calc.c.

References SLang_Load_Type::parse_level, prompt(), SLang_run_hooks(), SLmake_string(), SLrline_read_line(), and SLrline_save_line().

00139 {
00140    static char *input_hook = "calc_take_input_hook";
00141    char *prompt, *line;
00142 
00143    if (x->parse_level == 0)
00144      {
00145         if ((input_hook != NULL)
00146             && (-1 == SLang_run_hooks (input_hook, 0)))
00147           {
00148              input_hook = NULL;
00149              return NULL;
00150           }
00151         prompt = "Calc> ";
00152      }
00153    else
00154      prompt = "      ";
00155 
00156    if (NULL == (line = SLrline_read_line (Calc_RLI, prompt, NULL)))
00157      return SLmake_string ("quit();");
00158 
00159    putc ('\n', stdout);  fflush (stdout);
00160 
00161    SLrline_save_line (Calc_RLI);
00162    return line;
00163 }

static int take_input void   )  [static]
 

Definition at line 203 of file calc.c.

References SLang_load_object().

Referenced by main().

00204 {
00205    return SLang_load_object (Readline_Load_Object);
00206 }


Variable Documentation

SLang_Intrin_Fun_Type Calc_Intrinsics[] [static]
 

Initial value:

Definition at line 49 of file calc.c.

SLrline_Type* Calc_RLI [static]
 

Definition at line 135 of file calc.c.

My_Struct_Type My_Struct [static]
 

Initial value:

 
{
   -41,
   NULL,
   7.18
}

Definition at line 65 of file calc.c.

SLang_IStruct_Field_Type My_Struct_Field_Table[] [static]
 

Initial value:

Definition at line 74 of file calc.c.

My_Struct_Type* My_Struct_Ptr = &My_Struct [static]
 

Definition at line 72 of file calc.c.

SLang_Load_Type* Readline_Load_Object
 

Definition at line 136 of file calc.c.


© sourcejam.com 2005-2008