Work on queries.
[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.4  1995/10/31 10:03:53  adam
45  * Work on queries.
46  * New command implemented - aborts script.
47  *
48  * Revision 1.3  1995/10/30  17:35:18  adam
49  * New function zwait that waits for a variable change - due to i/o events
50  * that invoke callback routines.
51  *
52  * Revision 1.2  1995/10/27  17:30:16  adam
53  * First search request/response that works.
54  *
55  * Revision 1.1  1995/10/27  15:12:08  adam
56  * IrTcl incorporated in the gateway.
57  * Better separation of script types.
58  * Z39.50 gateway scripts entered.
59  *
60  */
61
62 #include <stdio.h>
63 #include <stdlib.h>
64 #include <unistd.h>
65 #include <sys/time.h>
66 #include <sys/types.h>
67 #ifdef AIX
68 #include <sys/select.h>
69 #endif
70 #include <string.h>
71 #include <assert.h>
72 #include <ctype.h>
73
74 #include "wtcl.h"
75 #include "wirtcl.h"
76
77 static void *do_create (WCLIENT wcl, void *args);
78 static int do_exec (const char *fname, char *parms, void *mydata);
79
80 static struct w_interp_type w_interp_t = {
81     "irtcl",
82     do_create,
83     do_exec
84 };
85
86 W_Interp_Type w_interp_irtcl = &w_interp_t;
87
88
89 static char *mod = "wirtcl";
90
91 struct tcl_info {
92     W_Interp w_interp;
93     Tcl_Interp *interp;
94     WCLIENT wcl;
95 };
96
97
98 static int events (struct tcl_info *p, char *waitVar);
99
100 static int proc_zwait_invoke (ClientData clientData, Tcl_Interp *interp,
101                               int argc, char **argv)
102 {
103     struct tcl_info *p = (struct tcl_info*) clientData;
104     
105     if (argc < 2)
106         return TCL_OK;
107     events (p, argv[1]);
108     return TCL_OK;
109 }
110
111
112
113 /* select(2) callbacks */
114 struct callback {
115     void (*r_handle)(ClientData);
116     void (*w_handle)(ClientData);
117     void (*x_handle)(ClientData);
118     void *obj;
119 };
120 #define MAX_CALLBACK 200
121
122 static struct callback callback_table[MAX_CALLBACK];
123 static int max_fd = 3;            /* don't worry: it will grow... */
124
125 static void *do_create (WCLIENT wcl, void *args)
126 {
127     struct tcl_info *p;
128     int i;
129
130     if (!(p = malloc (sizeof(*p))))
131     {
132         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "malloc: irtcl_info");
133         exit (1);
134     }
135     if (!(p->w_interp = w_interp_create (w_interp_tcl, wcl, NULL)))
136     {
137         gw_log (GW_LOG_FATAL, mod, "Cannot make Tcl_Interp");
138         exit (1);
139     }
140     p->wcl = wcl;
141     p->interp = w_interp_tcl_get (p->w_interp);
142     if (Irtcl_Init(p->interp) == TCL_ERROR)
143     {
144         gw_log (GW_LOG_FATAL, mod, "Cannot make Irtcl_Interp");
145         exit (1);
146     }
147     /* initialize irtcl */
148     Tcl_CreateCommand (p->interp, "zwait", proc_zwait_invoke, p, NULL);
149     for (i=0; i<MAX_CALLBACK; i++)
150     {
151         callback_table[i].r_handle = NULL;
152         callback_table[i].w_handle = NULL;
153         callback_table[i].x_handle = NULL;
154     }
155     return p;
156 }
157
158
159 static int do_exec (const char *fname, char *parms, void *mydata)
160 {
161     struct tcl_info *p = mydata;
162     int r;
163     if ((r = w_interp_exec (p->w_interp, fname, parms)))
164         return r;
165     return 0;
166 }
167
168
169 static int events (struct tcl_info *p, char *waitVar)
170 {
171     int r, i;
172     char *cp;
173     char *waitVarVal;
174     static fd_set fdset_tcl_r;
175     static fd_set fdset_tcl_w;
176     static fd_set fdset_tcl_x;
177     int fifo_in = p->wcl->linein;
178     if (fifo_in > max_fd)
179         max_fd = fifo_in;
180
181     assert (waitVar);
182     if ((cp = Tcl_GetVar (p->interp, waitVar, 0)))
183     {
184         waitVarVal = malloc (strlen(cp)+1);
185         strcpy (waitVarVal, cp);
186     }
187     else
188     {
189         char msg[128];
190
191         sprintf (msg, "Variable %s doesn't exist", waitVar);
192         gw_log (GW_LOG_WARN, mod, "%s", msg);
193         Tcl_AppendResult (p->interp, msg, NULL);
194         return TCL_ERROR;
195     }
196     gw_log (GW_LOG_DEBUG, mod, "Waiting for variable %s=%s",
197             waitVar, waitVarVal);
198     while (1)
199     {
200         if (!(cp = Tcl_GetVar (p->interp, waitVar, 0)) ||
201             strcmp (cp, waitVarVal))
202         {
203             Tcl_AppendResult (p->interp, cp, NULL);
204             free (waitVarVal);
205             return TCL_OK;
206         }
207         FD_ZERO (&fdset_tcl_r);
208         FD_ZERO (&fdset_tcl_w);
209         FD_ZERO (&fdset_tcl_x);
210         
211         for (r=0, i=0; i<=max_fd; i++)
212         {
213             if (callback_table[i].w_handle)
214             {
215                 FD_SET (i, &fdset_tcl_w);
216                 r++;
217             }
218             if (callback_table[i].r_handle)
219             {
220                 FD_SET (i, &fdset_tcl_r);
221                 r++;
222             }
223             if (callback_table[i].x_handle)
224             {
225                 FD_SET (i, &fdset_tcl_x);
226                 r++;
227             }
228         }
229         if (!r)
230             break;
231         FD_SET (fifo_in, &fdset_tcl_r);
232         if ((r = select(max_fd+1, &fdset_tcl_r, &fdset_tcl_w, 
233                           &fdset_tcl_x, 0)) < 0)
234         {
235             perror("select");
236             exit(1);
237         }
238         if (!r)
239             break;
240         if (FD_ISSET (fifo_in, &fdset_tcl_r))
241             break;
242         for (i=0; i<=max_fd; i++)
243         {
244             if (FD_ISSET (i, &fdset_tcl_r))
245             {
246                 assert (callback_table[i].r_handle);
247                 (*callback_table[i].r_handle) (callback_table[i].obj);
248             }
249             if (FD_ISSET (i, &fdset_tcl_w))
250             {
251                 assert (callback_table[i].w_handle);
252                 (*callback_table[i].w_handle) (callback_table[i].obj);
253             }
254             if (FD_ISSET (i, &fdset_tcl_x))
255             {
256                 assert (callback_table[i].x_handle);
257                 (*callback_table[i].x_handle) (callback_table[i].obj);
258             }
259         }
260     }
261     free (waitVarVal);
262     return TCL_OK;
263 }
264
265 void ir_select_add (int fd, void *obj)
266 {
267     callback_table[fd].obj = obj;
268     callback_table[fd].r_handle = ir_select_read;
269     callback_table[fd].w_handle = NULL;
270     callback_table[fd].x_handle = NULL;
271     if (fd > max_fd)
272         max_fd = fd;
273 }
274
275 void ir_select_add_write (int fd, void *obj)
276 {
277     callback_table[fd].w_handle = ir_select_write;
278     if (fd > max_fd)
279         max_fd = fd;
280 }
281
282 void ir_select_remove_write (int fd, void *obj)
283 {
284     callback_table[fd].w_handle = NULL;
285 }
286
287 void ir_select_remove (int fd, void *obj)
288 {
289     callback_table[fd].r_handle = NULL;
290     callback_table[fd].w_handle = NULL;
291     callback_table[fd].x_handle = NULL;
292 }