New function w_interp_irtcl_get that returns Tcl interpreter of
[egate.git] / www / wirtcl.c
1 /*
2  * Copyright (c) 1995, the EUROPAGATE consortium (see below).
3  *
4  * The EUROPAGATE consortium members are:
5  *
6  *    University College Dublin
7  *    Danmarks Teknologiske Videnscenter
8  *    An Chomhairle Leabharlanna
9  *    Consejo Superior de Investigaciones Cientificas
10  *
11  * Permission to use, copy, modify, distribute, and sell this software and
12  * its documentation, in whole or in part, for any purpose, is hereby granted,
13  * provided that:
14  *
15  * 1. This copyright and permission notice appear in all copies of the
16  * software and its documentation. Notices of copyright or attribution
17  * which appear at the beginning of any file must remain unchanged.
18  *
19  * 2. The names of EUROPAGATE or the project partners may not be used to
20  * endorse or promote products derived from this software without specific
21  * prior written permission.
22  *
23  * 3. Users of this software (implementors and gateway operators) agree to
24  * inform the EUROPAGATE consortium of their use of the software. This
25  * information will be used to evaluate the EUROPAGATE project and the
26  * software, and to plan further developments. The consortium may use
27  * the information in later publications.
28  * 
29  * 4. Users of this software agree to make their best efforts, when
30  * documenting their use of the software, to acknowledge the EUROPAGATE
31  * consortium, and the role played by the software in their work.
32  *
33  * THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTY OF ANY KIND,
34  * EXPRESS, IMPLIED, OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
35  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
36  * IN NO EVENT SHALL THE EUROPAGATE CONSORTIUM OR ITS MEMBERS BE LIABLE
37  * FOR ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF
38  * ANY KIND, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA
39  * OR PROFITS, WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND
40  * ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
41  * USE OR PERFORMANCE OF THIS SOFTWARE.
42  *
43  * $Log: wirtcl.c,v $
44  * Revision 1.16  1996/02/29 15:40:23  adam
45  * New function w_interp_irtcl_get that returns Tcl interpreter of
46  * IrTcl interpreter.
47  *
48  * Revision 1.15  1996/02/21  14:58:01  adam
49  * Modified to use ir_tcl_select_set.
50  *
51  * Revision 1.14  1996/02/12  10:10:31  adam
52  * Resource/config system used by the gateway.
53  *
54  * Revision 1.13  1996/01/24  08:26:54  adam
55  * All tcl commands prefixed with egw_ (except the html command).
56  *
57  * Revision 1.12  1996/01/12  10:05:18  adam
58  * If script name ends with ';' HTTP/GET/Expires will be defined.
59  * The cgi interface only reads final handshake if response from
60  * server (shell) was zero-terminated [If it isn't it probably died].
61  *
62  * Revision 1.11  1996/01/09  16:16:49  adam
63  * Port to OSF/1. Gif references moved from /gif/ to /egwgif/.
64  *
65  * Revision 1.10  1996/01/09  10:46:50  adam
66  * New defines: LOGDIR/EGWDIR/CGIDIR set in Makefile.
67  *
68  * Revision 1.9  1995/11/07  14:56:59  adam
69  * Work on search in multiple targets.
70  * New wtcl command: wlog.
71  * Optional timeout parameter to zwait.
72  *
73  * Revision 1.8  1995/11/06  17:44:22  adam
74  * State reestablised when shell restarts. History of previous
75  * result sets.
76  *
77  * Revision 1.7  1995/11/02  16:35:37  adam
78  * Bug fixes and select on FIFOs in wcgi - doesn't really work!
79  *
80  * Revision 1.6  1995/11/01  16:15:47  adam
81  * Better presentation of records. Query/set number persistent.
82  *
83  * Revision 1.5  1995/10/31  16:56:24  adam
84  * Record presentation.
85  *
86  * Revision 1.4  1995/10/31  10:03:53  adam
87  * Work on queries.
88  * New command implemented - aborts script.
89  *
90  * Revision 1.3  1995/10/30  17:35:18  adam
91  * New function zwait that waits for a variable change - due to i/o events
92  * that invoke callback routines.
93  *
94  * Revision 1.2  1995/10/27  17:30:16  adam
95  * First search request/response that works.
96  *
97  * Revision 1.1  1995/10/27  15:12:08  adam
98  * IrTcl incorporated in the gateway.
99  * Better separation of script types.
100  * Z39.50 gateway scripts entered.
101  *
102  */
103
104 #include <stdio.h>
105 #include <stdlib.h>
106 #include <unistd.h>
107 #include <sys/time.h>
108 #include <sys/types.h>
109 #ifdef AIX
110 #include <sys/select.h>
111 #endif
112 #include <string.h>
113 #include <assert.h>
114 #include <ctype.h>
115
116 #include <log.h>
117 #include "wtcl.h"
118 #include "wirtcl.h"
119
120 static void *do_create (WCLIENT wcl, void *args);
121 static int do_exec (const char *fname, char *parms, void *mydata);
122 static int do_load (char *parms, void *mydata);
123 static int do_save (char *parms, void *mydata);
124
125 static struct w_interp_type w_interp_t = {
126     "irtcl",
127     do_create,
128     do_exec,
129     do_load,
130     do_save
131 };
132
133 W_Interp_Type w_interp_irtcl = &w_interp_t;
134
135
136 static char *mod = "wirtcl";
137
138 struct tcl_info {
139     W_Interp w_interp;
140     Tcl_Interp *interp;
141     WCLIENT wcl;
142 };
143
144 static int events (struct tcl_info *p, char *waitVar, int tout);
145
146 static int proc_zwait_invoke (ClientData clientData, Tcl_Interp *interp,
147                               int argc, char **argv)
148 {
149     struct tcl_info *p = (struct tcl_info*) clientData;
150     
151     if (argc < 2)
152         return TCL_OK;
153     return events (p, argv[1], (argc == 3) ? atoi(argv[2]) : 0);
154 }
155
156 /* select callbacks */
157 struct callback {
158     void (*handle)(ClientData, int, int, int);
159     int r, w, e;
160     ClientData obj;
161 };
162 #define MAX_CALLBACK 200
163
164 static struct callback callback_table[MAX_CALLBACK];
165 static int max_fd = 3;            /* don't worry: it will grow... */
166
167 static void *do_create (WCLIENT wcl, void *args)
168 {
169     struct tcl_info *p;
170     int i;
171
172     if (!(p = malloc (sizeof(*p))))
173     {
174         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "malloc: irtcl_info");
175         exit (1);
176     }
177     if (!(p->w_interp = w_interp_create (w_interp_tcl, wcl, NULL)))
178     {
179         gw_log (GW_LOG_FATAL, mod, "Cannot make Tcl_Interp");
180         exit (1);
181     }
182     p->wcl = wcl;
183     p->interp = w_interp_tcl_get (p->w_interp);
184     if (Irtcl_Init(p->interp) == TCL_ERROR)
185     {
186         gw_log (GW_LOG_FATAL, mod, "Cannot make Irtcl_Interp");
187         exit (1);
188     }
189     log_init(LOG_ALL, "irtcl", "irtcl_log");
190     /* initialize irtcl */
191     Tcl_CreateCommand (p->interp, "egw_wait", proc_zwait_invoke, p, NULL);
192     for (i=0; i<MAX_CALLBACK; i++)
193         callback_table[i].handle = NULL;
194     return p;
195 }
196
197
198 static int do_exec (const char *fname, char *parms, void *mydata)
199 {
200     struct tcl_info *p = mydata;
201     int r;
202     if ((r = w_interp_exec (p->w_interp, fname, parms)))
203         return r;
204     return 0;
205 }
206
207
208 static int events (struct tcl_info *p, char *waitVar, int tout)
209 {
210     int r, i;
211     char *cp;
212     char *waitVarVal;
213     static fd_set fdset_tcl_r;
214     static fd_set fdset_tcl_w;
215     static fd_set fdset_tcl_x;
216     int fifo_in = p->wcl->linein;
217     if (fifo_in > max_fd)
218         max_fd = fifo_in;
219
220     assert (waitVar);
221     if ((cp = Tcl_GetVar (p->interp, waitVar, 0)))
222     {
223         waitVarVal = malloc (strlen(cp)+1);
224         strcpy (waitVarVal, cp);
225     }
226     else
227     {
228         char msg[128];
229
230         sprintf (msg, "Variable %s doesn't exist", waitVar);
231         gw_log (GW_LOG_WARN, mod, "%s", msg);
232         Tcl_AppendResult (p->interp, msg, NULL);
233         return TCL_ERROR;
234     }
235     gw_log (GW_LOG_DEBUG, mod, "Waiting %s=%s", waitVar, waitVarVal);
236     while (1)
237     {
238         struct timeval to, *top;
239         if (tout > 0)
240         {
241             to.tv_usec = 0;
242             to.tv_sec = tout;
243             top = &to;
244         }
245         else
246             top = 0;
247
248         if (!(cp = Tcl_GetVar (p->interp, waitVar, 0)) ||
249             strcmp (cp, waitVarVal))
250         {
251             gw_log (GW_LOG_DEBUG, mod, "Changed to %s", cp);
252             Tcl_AppendResult (p->interp, cp, NULL);
253             free (waitVarVal);
254             return TCL_OK;
255         }
256         FD_ZERO (&fdset_tcl_r);
257         FD_ZERO (&fdset_tcl_w);
258         FD_ZERO (&fdset_tcl_x);
259         
260         for (r=0, i=0; i<=max_fd; i++)
261         {
262             if (callback_table[i].handle && callback_table[i].w)
263             {
264                 FD_SET (i, &fdset_tcl_w);
265                 r++;
266             }
267             if (callback_table[i].handle && callback_table[i].r)
268             {
269                 FD_SET (i, &fdset_tcl_r);
270                 r++;
271             }
272             if (callback_table[i].handle && callback_table[i].e)
273             {
274                 FD_SET (i, &fdset_tcl_x);
275                 r++;
276             }
277         }
278         if (!r)
279             break;
280 #if 1
281         gw_log (GW_LOG_DEBUG, mod, "fifo select %d", fifo_in);
282         FD_SET (fifo_in, &fdset_tcl_r);
283 #endif
284         if ((r = select(max_fd+1, &fdset_tcl_r, &fdset_tcl_w, 
285                           &fdset_tcl_x, top)) < 0)
286         {
287             gw_log (GW_LOG_ERRNO|GW_LOG_FATAL, mod, "select");
288             exit(1);
289         }
290         if (!r)
291         {
292             gw_log (GW_LOG_DEBUG, mod, "timeout");
293             free (waitVarVal);
294             return TCL_ERROR;
295         }
296         if (FD_ISSET (fifo_in, &fdset_tcl_r))
297         {
298             gw_log (GW_LOG_DEBUG, mod, "FIFO closed");
299             free (waitVarVal);
300             return TCL_ERROR;
301         }
302         for (i=0; i<=max_fd; i++)
303         {
304             int r_flag = 0;
305             int w_flag = 0;
306             int e_flag = 0;
307
308             if (!callback_table[i].handle)
309                 continue;
310             if (FD_ISSET (i, &fdset_tcl_r) && callback_table[i].r)
311                 r_flag = 1;
312             if (FD_ISSET (i, &fdset_tcl_w) && callback_table[i].w)
313                 w_flag = 1;
314             if (FD_ISSET (i, &fdset_tcl_x) && callback_table[i].e)
315                 e_flag = 1;
316             if (r_flag || w_flag || e_flag)
317                 (*callback_table[i].handle)(callback_table[i].obj,
318                  r_flag, w_flag, e_flag);
319         }
320     }
321     free (waitVarVal);
322     return TCL_OK;
323 }
324
325 void ir_tcl_select_set (void (*f)(ClientData clientData, int r, int w, int e),
326                         int fd, ClientData clientData, int r, int w, int e)
327 {
328     callback_table[fd].handle = f;
329     callback_table[fd].obj = clientData;
330     callback_table[fd].r = r;
331     callback_table[fd].w = w;
332     callback_table[fd].e = e;
333     if (fd > max_fd)
334         max_fd = fd;
335 }
336
337 static int do_load (char *parms, void *mydata)
338 {
339     struct tcl_info *p = mydata;
340
341     return w_interp_load_state (p->w_interp, parms);
342 }
343
344 static int do_save (char *parms, void *mydata)
345 {
346     struct tcl_info *p = mydata;
347
348     return w_interp_save_state (p->w_interp, parms);
349 }
350
351 Tcl_Interp *w_interp_irtcl_get (W_Interp w_interp)
352 {
353     struct tcl_info *p;
354
355     if (strcmp (w_interp->ctrl->name, "irtcl"))
356     {
357         gw_log (GW_LOG_FATAL, mod, "Internal failure");
358         assert (0);
359     }
360     p = (struct tcl_info*) w_interp->mydata;
361     return p->interp;
362 }
363