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