Allow YAZ 2 series only
[ir-tcl-moved-to-github.git] / tclmain.c
1 /*
2  * IR toolkit for tcl/tk
3  * (c) Index Data 1995-1996
4  * See the file LICENSE for details.
5  * Sebastian Hammer, Adam Dickmeiss
6  *
7  * $Log: tclmain.c,v $
8  * Revision 1.21  2000-02-22 23:11:03  adam
9  * Fixed include statements.
10  *
11  * Revision 1.20  1997/04/30 07:26:08  adam
12  * Added support for shared libaries (if supported by Tcl itself).
13  *
14  * Revision 1.19  1996/08/20 09:27:49  adam
15  * More work on explain.
16  * Renamed tkinit.c to tkmain.c. The tcl shell uses the Tcl 7.5 interface
17  * for socket i/o instead of the handcrafted one (for Tcl 7.3 and Tcl7.4).
18  *
19  */
20
21 #include <unistd.h>
22 #include <sys/time.h>
23 #include <sys/types.h>
24 #ifdef _AIX
25 #include <sys/select.h>
26 #endif
27
28 #include <assert.h>
29
30 #include <tcl.h>
31 #include <yaz/log.h>
32 #include "ir-tcl.h"
33
34 int Tcl_AppInit (Tcl_Interp *interp)
35 {
36     if (Tcl_Init(interp) == TCL_ERROR)
37         return TCL_ERROR;
38     if (Irtcl_Init(interp) == TCL_ERROR)
39         return TCL_ERROR;
40 #if USE_WAIS
41     if (Waistcl_Init(interp) == TCL_ERROR)
42         return TCL_ERROR;
43 #endif
44     return TCL_OK;
45 }
46
47 #if TCL_MAJOR_VERSION > 7 || (TCL_MAJOR_VERSION == 7 && TCL_MINOR_VERSION > 4)
48 /* new version of tcl: version > 7.4 */
49 extern int matherr ();
50 int *tclDummyMathPtr = (int*) matherr;
51
52 int main (int argc, char **argv)
53 {
54     Tcl_Main (argc, argv, Tcl_AppInit);
55     return 0;
56 }
57
58 #else
59 /* old version of tcl: version <= 7.4 */
60
61 static char *fileName = NULL;
62 extern int main ();
63 int *tclDummyMainPtr = (int*) main;
64
65 /* select(2) callbacks */
66 struct callback {
67     void (*handle)(ClientData, int, int, int);
68     int r, w, e;
69     ClientData obj;
70 };
71 #define MAX_CALLBACK 200
72
73 static struct callback callback_table[MAX_CALLBACK];
74 static int max_fd = 3;            /* don't worry: it will grow... */
75
76 void tcl_mainloop (Tcl_Interp *interp, int interactive);
77
78 int main (int argc, char **argv)
79 {
80     Tcl_Interp *interp;
81     int code;
82     int i;
83
84     interp = Tcl_CreateInterp();
85     Tcl_SetVar (interp, "tcl_interactive", "0", TCL_GLOBAL_ONLY);
86     if (argc == 2)
87         fileName = argv[1];
88
89     if (Tcl_AppInit(interp) != TCL_OK) {
90         fprintf(stderr, "Tcl_AppInit failed: %s\n", interp->result);
91     }
92     for (i=0; i<MAX_CALLBACK; i++)
93         callback_table[i].handle = NULL;
94     if (fileName)
95     {
96         code = Tcl_EvalFile (interp, fileName);
97         if (*interp->result != 0)
98             printf ("%s\n", interp->result);
99         if (code != TCL_OK)
100             exit (1);
101         tcl_mainloop (interp, 0);
102     }
103     else if (isatty(0))
104     {
105
106         Tcl_SetVar (interp, "tcl_interactive", "1", TCL_GLOBAL_ONLY);
107         tcl_mainloop (interp, 1);
108     }
109     else
110     {
111         Tcl_DString command;
112         char input_buf[1024];
113         int count;
114
115         printf ("xx\n");
116         Tcl_DStringInit (&command);
117         while (fgets (input_buf, 1024, stdin))
118         {
119             count = strlen(input_buf);
120             Tcl_DStringAppend (&command, input_buf, count);
121             if (Tcl_CommandComplete (Tcl_DStringValue (&command)))
122             {
123                 int code = Tcl_Eval (interp, Tcl_DStringValue (&command));
124                 Tcl_DStringFree (&command);
125                 if (code)
126                     printf ("Error: %s\n", interp->result);
127             }
128         }
129         tcl_mainloop (interp, 0);
130     }
131     exit (0);
132 }
133
134 void tcl_mainloop (Tcl_Interp *interp, int interactive)
135 {
136     int i;
137     int res;
138     Tcl_DString command;
139     static fd_set fdset_tcl_r;
140     static fd_set fdset_tcl_w;
141     static fd_set fdset_tcl_x;
142     int min_fd;
143
144     min_fd = interactive ? 3 : 0;
145     if (interactive)
146     {
147         Tcl_DStringInit (&command);
148         printf ("%% "); fflush (stdout);
149     }
150     while (1)
151     {
152         FD_ZERO (&fdset_tcl_r);
153         FD_ZERO (&fdset_tcl_w);
154         FD_ZERO (&fdset_tcl_x);
155         if (interactive)
156             FD_SET (0, &fdset_tcl_r);
157         for (res=0, i=min_fd; i<=max_fd; i++)
158         {
159             if (callback_table[i].handle && callback_table[i].w)
160             {
161                 FD_SET (i, &fdset_tcl_w);
162                 res++;
163             }
164             if (callback_table[i].handle && callback_table[i].r)
165             {
166                 FD_SET (i, &fdset_tcl_r);
167                 res++;
168             }
169             if (callback_table[i].handle && callback_table[i].e)
170             {
171                 FD_SET (i, &fdset_tcl_x);
172                 res++;
173             }
174         }
175         if (!interactive && !res)
176             return;
177         if ((res = select(max_fd+1, &fdset_tcl_r, &fdset_tcl_w, 
178                           &fdset_tcl_x, 0)) < 0)
179         {
180             perror("select");
181             exit(1);
182         }
183         if (!res)
184             continue;
185         for (i=min_fd; i<=max_fd; i++)
186         {
187             int r_flag = 0;
188             int w_flag = 0;
189             int e_flag = 0;
190
191             if (!callback_table[i].handle)
192                 continue;
193             if (FD_ISSET (i, &fdset_tcl_r) && callback_table[i].r)
194                 r_flag = 1;
195             if (FD_ISSET (i, &fdset_tcl_w) && callback_table[i].w)
196                 w_flag = 1;
197             if (FD_ISSET (i, &fdset_tcl_x) && callback_table[i].e)
198                 e_flag = 1;
199             if (r_flag || w_flag || e_flag)
200                 (*callback_table[i].handle)(callback_table[i].obj,
201                  r_flag, w_flag, e_flag);
202         }
203         if (interactive && FD_ISSET(0, &fdset_tcl_r))
204         {
205             char input_buf[1024];
206             int count = read (0, input_buf, 1024);
207
208             if (count <= 0)
209                 exit (0);
210             Tcl_DStringAppend (&command, input_buf, count);
211             if (Tcl_CommandComplete (Tcl_DStringValue (&command)))
212             {
213                 int code = Tcl_Eval (interp, Tcl_DStringValue (&command));
214                 Tcl_DStringFree (&command);
215                 if (code)
216                     printf ("Error: %s\n", interp->result);
217                 else if (*interp->result)
218                     printf ("%s\n", interp->result);
219                 printf ("%% "); fflush (stdout);
220             }
221         }
222     }
223 }
224
225 void ir_tcl_select_set (void (*f)(ClientData clientData, int r, int w, int e),
226                         int fd, ClientData clientData, int r, int w, int e)
227 {
228     callback_table[fd].handle = f;
229     callback_table[fd].obj = clientData;
230     callback_table[fd].r = r;
231     callback_table[fd].w = w;
232     callback_table[fd].e = e;
233     if (fd > max_fd)
234         max_fd = fd;
235 }
236
237 #endif