1 /* This demo indicates how to read and parse a S-Lang file by bypassing the
2 * built-in routines.
3 */
4 #include "config.h"
5 #include <stdio.h>
6 #include <string.h>
7 #ifdef HAVE_STDLIB_H
8 # include <stdlib.h>
9 #endif
10 #include <slang.h>
11
12 /* Suppose that you want to read input using a read line package
13 * such as one provided by S-Lang. For generality, lets assume that this
14 * function is called 'readline' and it is prototyped as:
15 * int readline (char *prompt, char *buffer);
16 * where it returns the number of characters read and -1 if end of file. The
17 * first parameter is a prompt and the second represents the buffer where the
18 * characters are to placed. Also assume that this routine requires that the
19 * function 'init_readline' be called first before it can be used and
20 * 'reset_readline' must be called after using it.
21 *
22 * The goal here is to get S-Lang to call the readline function.
23 */
24
25 /* For the purposes of this demo, we will use just fgets */
26 #define MAX_BUF_LEN 256
readline(char * prompt,char * buf)27 static int readline (char *prompt, char *buf)
28 {
29 fputs (prompt, stdout); fflush (stdout);
30 if (NULL == fgets (buf, MAX_BUF_LEN, stdin)) return -1;
31 return (int) strlen (buf);
32 }
33
init_readline(void)34 static int init_readline (void)
35 {
36 puts ("Initializing readline."); fflush (stdout);
37 return 0;
38 }
39
reset_readline(void)40 static void reset_readline (void)
41 {
42 puts ("Resetting readline."); fflush (stdout);
43 }
44
45 /* Now lets define the function that S-Lang will use to actually read the data.
46 * It calls readline. S-Lang will call this function and the function must
47 * return a pointer to the buffer containg the characters of the line or NULL
48 * upon end of file. In many ways, it is like fgets except that it is passed
49 * a pointer to SLang_Load_Type in stead of FILE.
50 */
51
52 typedef struct
53 {
54 char buf[MAX_BUF_LEN];
55 char *prompt;
56 }
57 Our_Client_Data_Type;
58
read_using_readline(SLang_Load_Type * x)59 static char *read_using_readline (SLang_Load_Type *x)
60 {
61 Our_Client_Data_Type *client_data;
62
63 client_data = (Our_Client_Data_Type *) x->client_data;
64 if (-1 == readline (client_data->prompt, client_data->buf))
65 return NULL;
66
67 return client_data->buf;
68 }
69
70 /* Now, we all of this is tied together in this routine which will be called
71 * from main below.
72 */
73
read_input(void)74 static int read_input (void)
75 {
76 SLang_Load_Type *x;
77 Our_Client_Data_Type client_data;
78
79 if (NULL == (x = SLallocate_load_type ("<readline>")))
80 return -1;
81
82 client_data.prompt = "Demo> ";
83
84 x->client_data = (VOID_STAR) &client_data;
85 x->read = read_using_readline; /* function to call to perform the read */
86 SLang_load_object (x);
87 return 0;
88 }
89
90 /* Now here is are some intrinsic functions */
91
main(int argc,char ** argv)92 int main (int argc, char **argv)
93 {
94 /* usual stuff */
95
96 (void) argc; (void) argv;
97
98 if ((-1 == SLang_init_slang ()) /* basic interpreter functions */
99 || (-1 == SLang_init_slmath ()) /* sin, cos, etc... */
100 #ifdef unix
101 || (-1 == SLang_init_slunix ()) /* unix system calls */
102 #endif
103 || (-1 == SLang_init_slfile ())) /* file i/o */
104 {
105 fprintf(stderr, "Unable to initialize S-Lang.\n");
106 return 1;
107 }
108
109 init_readline ();
110
111 read_input ();
112
113 reset_readline ();
114 return SLang_get_error ();
115 }
116
117