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