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