Updated for version YAZ 1.6 and higher.
[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.18  2001/02/26 10:35:40  adam
45  * Updated for version YAZ 1.6 and higher.
46  *
47  * Revision 1.17  1996/03/15 14:43:26  adam
48  * Function egw_wait returns 'cancel' if new request is pending (user
49  * has cancelled); or egw_wait returns 'timeout' on timeout.
50  *
51  * Revision 1.16  1996/02/29  15:40:23  adam
52  * New function w_interp_irtcl_get that returns Tcl interpreter of
53  * IrTcl interpreter.
54  *
55  * Revision 1.15  1996/02/21  14:58:01  adam
56  * Modified to use ir_tcl_select_set.
57  *
58  * Revision 1.14  1996/02/12  10:10:31  adam
59  * Resource/config system used by the gateway.
60  *
61  * Revision 1.13  1996/01/24  08:26:54  adam
62  * All tcl commands prefixed with egw_ (except the html command).
63  *
64  * Revision 1.12  1996/01/12  10:05:18  adam
65  * If script name ends with ';' HTTP/GET/Expires will be defined.
66  * The cgi interface only reads final handshake if response from
67  * server (shell) was zero-terminated [If it isn't it probably died].
68  *
69  * Revision 1.11  1996/01/09  16:16:49  adam
70  * Port to OSF/1. Gif references moved from /gif/ to /egwgif/.
71  *
72  * Revision 1.10  1996/01/09  10:46:50  adam
73  * New defines: LOGDIR/EGWDIR/CGIDIR set in Makefile.
74  *
75  * Revision 1.9  1995/11/07  14:56:59  adam
76  * Work on search in multiple targets.
77  * New wtcl command: wlog.
78  * Optional timeout parameter to zwait.
79  *
80  * Revision 1.8  1995/11/06  17:44:22  adam
81  * State reestablised when shell restarts. History of previous
82  * result sets.
83  *
84  * Revision 1.7  1995/11/02  16:35:37  adam
85  * Bug fixes and select on FIFOs in wcgi - doesn't really work!
86  *
87  * Revision 1.6  1995/11/01  16:15:47  adam
88  * Better presentation of records. Query/set number persistent.
89  *
90  * Revision 1.5  1995/10/31  16:56:24  adam
91  * Record presentation.
92  *
93  * Revision 1.4  1995/10/31  10:03:53  adam
94  * Work on queries.
95  * New command implemented - aborts script.
96  *
97  * Revision 1.3  1995/10/30  17:35:18  adam
98  * New function zwait that waits for a variable change - due to i/o events
99  * that invoke callback routines.
100  *
101  * Revision 1.2  1995/10/27  17:30:16  adam
102  * First search request/response that works.
103  *
104  * Revision 1.1  1995/10/27  15:12:08  adam
105  * IrTcl incorporated in the gateway.
106  * Better separation of script types.
107  * Z39.50 gateway scripts entered.
108  *
109  */
110
111 #include <stdio.h>
112 #include <stdlib.h>
113 #include <unistd.h>
114 #include <sys/time.h>
115 #include <sys/types.h>
116 #ifdef AIX
117 #include <sys/select.h>
118 #endif
119 #include <string.h>
120 #include <assert.h>
121 #include <ctype.h>
122
123 #include <yaz/log.h>
124 #include "wtcl.h"
125 #include "wirtcl.h"
126
127 static void *do_create (WCLIENT wcl, void *args);
128 static int do_exec (const char *fname, char *parms, void *mydata);
129 static int do_load (char *parms, void *mydata);
130 static int do_save (char *parms, void *mydata);
131
132 static struct w_interp_type w_interp_t = {
133     "irtcl",
134     do_create,
135     do_exec,
136     do_load,
137     do_save
138 };
139
140 W_Interp_Type w_interp_irtcl = &w_interp_t;
141
142
143 static char *mod = "wirtcl";
144
145 struct tcl_info {
146     W_Interp w_interp;
147     Tcl_Interp *interp;
148     WCLIENT wcl;
149 };
150
151 static int events (struct tcl_info *p, char *waitVar, int tout);
152
153 static int proc_zwait_invoke (ClientData clientData, Tcl_Interp *interp,
154                               int argc, char **argv)
155 {
156     struct tcl_info *p = (struct tcl_info*) clientData;
157     
158     if (argc < 2)
159         return TCL_OK;
160     return events (p, argv[1], (argc == 3) ? atoi(argv[2]) : 0);
161 }
162
163 /* select callbacks */
164 struct callback {
165     void (*handle)(ClientData, int, int, int);
166     int r, w, e;
167     ClientData obj;
168 };
169 #define MAX_CALLBACK 200
170
171 static struct callback callback_table[MAX_CALLBACK];
172 static int max_fd = 3;            /* don't worry: it will grow... */
173
174 static void *do_create (WCLIENT wcl, void *args)
175 {
176     struct tcl_info *p;
177     int i;
178
179     if (!(p = malloc (sizeof(*p))))
180     {
181         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "malloc: irtcl_info");
182         exit (1);
183     }
184     if (!(p->w_interp = w_interp_create (w_interp_tcl, wcl, NULL)))
185     {
186         gw_log (GW_LOG_FATAL, mod, "Cannot make Tcl_Interp");
187         exit (1);
188     }
189     p->wcl = wcl;
190     p->interp = w_interp_tcl_get (p->w_interp);
191     if (Irtcl_Init(p->interp) == TCL_ERROR)
192     {
193         gw_log (GW_LOG_FATAL, mod, "Cannot make Irtcl_Interp");
194         exit (1);
195     }
196     yaz_log_init(LOG_ALL, "irtcl", "irtcl_log");
197     /* initialize irtcl */
198     Tcl_CreateCommand (p->interp, "egw_wait", proc_zwait_invoke, p, NULL);
199     for (i=0; i<MAX_CALLBACK; i++)
200         callback_table[i].handle = NULL;
201     return p;
202 }
203
204
205 static int do_exec (const char *fname, char *parms, void *mydata)
206 {
207     struct tcl_info *p = mydata;
208     int r;
209     if ((r = w_interp_exec (p->w_interp, fname, parms)))
210         return r;
211     return 0;
212 }
213
214
215 static int events (struct tcl_info *p, char *waitVar, int tout)
216 {
217     int r, i;
218     char *cp;
219     char *waitVarVal;
220     static fd_set fdset_tcl_r;
221     static fd_set fdset_tcl_w;
222     static fd_set fdset_tcl_x;
223     int fifo_in = p->wcl->linein;
224     if (fifo_in > max_fd)
225         max_fd = fifo_in;
226
227     assert (waitVar);
228     if ((cp = Tcl_GetVar (p->interp, waitVar, 0)))
229     {
230         waitVarVal = malloc (strlen(cp)+1);
231         strcpy (waitVarVal, cp);
232     }
233     else
234     {
235         char msg[128];
236
237         sprintf (msg, "Variable %s doesn't exist", waitVar);
238         gw_log (GW_LOG_WARN, mod, "%s", msg);
239         Tcl_AppendResult (p->interp, msg, NULL);
240         return TCL_ERROR;
241     }
242     gw_log (GW_LOG_DEBUG, mod, "Waiting %s=%s", waitVar, waitVarVal);
243     while (1)
244     {
245         struct timeval to, *top;
246         if (tout > 0)
247         {
248             to.tv_usec = 0;
249             to.tv_sec = tout;
250             top = &to;
251         }
252         else
253             top = 0;
254
255         if (!(cp = Tcl_GetVar (p->interp, waitVar, 0)) ||
256             strcmp (cp, waitVarVal))
257         {
258             gw_log (GW_LOG_DEBUG, mod, "Changed to %s", cp);
259             Tcl_AppendResult (p->interp, cp, NULL);
260             free (waitVarVal);
261             return TCL_OK;
262         }
263         FD_ZERO (&fdset_tcl_r);
264         FD_ZERO (&fdset_tcl_w);
265         FD_ZERO (&fdset_tcl_x);
266         
267         for (r=0, i=0; i<=max_fd; i++)
268         {
269             if (callback_table[i].handle && callback_table[i].w)
270             {
271                 FD_SET (i, &fdset_tcl_w);
272                 r++;
273             }
274             if (callback_table[i].handle && callback_table[i].r)
275             {
276                 FD_SET (i, &fdset_tcl_r);
277                 r++;
278             }
279             if (callback_table[i].handle && callback_table[i].e)
280             {
281                 FD_SET (i, &fdset_tcl_x);
282                 r++;
283             }
284         }
285         if (!r)
286             break;
287 #if 1
288         gw_log (GW_LOG_DEBUG, mod, "fifo select %d", fifo_in);
289         FD_SET (fifo_in, &fdset_tcl_r);
290 #endif
291         if ((r = select(max_fd+1, &fdset_tcl_r, &fdset_tcl_w, 
292                           &fdset_tcl_x, top)) < 0)
293         {
294             gw_log (GW_LOG_ERRNO|GW_LOG_FATAL, mod, "select");
295             exit(1);
296         }
297         if (!r)
298         {
299             gw_log (GW_LOG_DEBUG, mod, "timeout");
300             p->interp->result = "timeout";
301             free (waitVarVal);
302             return TCL_ERROR;
303         }
304         if (FD_ISSET (fifo_in, &fdset_tcl_r))
305         {
306             gw_log (GW_LOG_DEBUG, mod, "FIFO closed");
307             p->interp->result = "cancel";
308             free (waitVarVal);
309             return TCL_ERROR;
310         }
311         for (i=0; i<=max_fd; i++)
312         {
313             int r_flag = 0;
314             int w_flag = 0;
315             int e_flag = 0;
316
317             if (!callback_table[i].handle)
318                 continue;
319             if (FD_ISSET (i, &fdset_tcl_r) && callback_table[i].r)
320                 r_flag = 1;
321             if (FD_ISSET (i, &fdset_tcl_w) && callback_table[i].w)
322                 w_flag = 1;
323             if (FD_ISSET (i, &fdset_tcl_x) && callback_table[i].e)
324                 e_flag = 1;
325             if (r_flag || w_flag || e_flag)
326                 (*callback_table[i].handle)(callback_table[i].obj,
327                  r_flag, w_flag, e_flag);
328         }
329     }
330     free (waitVarVal);
331     return TCL_OK;
332 }
333
334 void ir_tcl_select_set (void (*f)(ClientData clientData, int r, int w, int e),
335                         int fd, ClientData clientData, int r, int w, int e)
336 {
337     callback_table[fd].handle = f;
338     callback_table[fd].obj = clientData;
339     callback_table[fd].r = r;
340     callback_table[fd].w = w;
341     callback_table[fd].e = e;
342     if (fd > max_fd)
343         max_fd = fd;
344 }
345
346 static int do_load (char *parms, void *mydata)
347 {
348     struct tcl_info *p = mydata;
349
350     return w_interp_load_state (p->w_interp, parms);
351 }
352
353 static int do_save (char *parms, void *mydata)
354 {
355     struct tcl_info *p = mydata;
356
357     return w_interp_save_state (p->w_interp, parms);
358 }
359
360 Tcl_Interp *w_interp_irtcl_get (W_Interp w_interp)
361 {
362     struct tcl_info *p;
363
364     if (strcmp (w_interp->ctrl->name, "irtcl"))
365     {
366         gw_log (GW_LOG_FATAL, mod, "Internal failure");
367         assert (0);
368     }
369     p = (struct tcl_info*) w_interp->mydata;
370     return p->interp;
371 }
372