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