2 * Copyright (c) 1995, the EUROPAGATE consortium (see below).
4 * The EUROPAGATE consortium members are:
6 * University College Dublin
7 * Danmarks Teknologiske Videnscenter
8 * An Chomhairle Leabharlanna
9 * Consejo Superior de Investigaciones Cientificas
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,
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.
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.
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.
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.
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.
44 * Revision 1.6 1995/11/01 16:15:47 adam
45 * Better presentation of records. Query/set number persistent.
47 * Revision 1.5 1995/10/31 16:56:24 adam
48 * Record presentation.
50 * Revision 1.4 1995/10/31 10:03:53 adam
52 * New command implemented - aborts script.
54 * Revision 1.3 1995/10/30 17:35:18 adam
55 * New function zwait that waits for a variable change - due to i/o events
56 * that invoke callback routines.
58 * Revision 1.2 1995/10/27 17:30:16 adam
59 * First search request/response that works.
61 * Revision 1.1 1995/10/27 15:12:08 adam
62 * IrTcl incorporated in the gateway.
63 * Better separation of script types.
64 * Z39.50 gateway scripts entered.
72 #include <sys/types.h>
74 #include <sys/select.h>
84 static void *do_create (WCLIENT wcl, void *args);
85 static int do_exec (const char *fname, char *parms, void *mydata);
87 static struct w_interp_type w_interp_t = {
93 W_Interp_Type w_interp_irtcl = &w_interp_t;
96 static char *mod = "wirtcl";
105 static int events (struct tcl_info *p, char *waitVar);
107 static int proc_zwait_invoke (ClientData clientData, Tcl_Interp *interp,
108 int argc, char **argv)
110 struct tcl_info *p = (struct tcl_info*) clientData;
120 /* select(2) callbacks */
122 void (*r_handle)(ClientData);
123 void (*w_handle)(ClientData);
124 void (*x_handle)(ClientData);
127 #define MAX_CALLBACK 200
129 static struct callback callback_table[MAX_CALLBACK];
130 static int max_fd = 3; /* don't worry: it will grow... */
132 static void *do_create (WCLIENT wcl, void *args)
137 if (!(p = malloc (sizeof(*p))))
139 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "malloc: irtcl_info");
142 if (!(p->w_interp = w_interp_create (w_interp_tcl, wcl, NULL)))
144 gw_log (GW_LOG_FATAL, mod, "Cannot make Tcl_Interp");
148 p->interp = w_interp_tcl_get (p->w_interp);
149 if (Irtcl_Init(p->interp) == TCL_ERROR)
151 gw_log (GW_LOG_FATAL, mod, "Cannot make Irtcl_Interp");
154 log_init(LOG_ALL, "irtcl", "/usr/local/etc/httpd/logs/irtcl_log");
155 /* initialize irtcl */
156 Tcl_CreateCommand (p->interp, "zwait", proc_zwait_invoke, p, NULL);
157 for (i=0; i<MAX_CALLBACK; i++)
159 callback_table[i].r_handle = NULL;
160 callback_table[i].w_handle = NULL;
161 callback_table[i].x_handle = NULL;
167 static int do_exec (const char *fname, char *parms, void *mydata)
169 struct tcl_info *p = mydata;
171 if ((r = w_interp_exec (p->w_interp, fname, parms)))
177 static int events (struct tcl_info *p, char *waitVar)
182 static fd_set fdset_tcl_r;
183 static fd_set fdset_tcl_w;
184 static fd_set fdset_tcl_x;
185 int fifo_in = p->wcl->linein;
186 if (fifo_in > max_fd)
190 if ((cp = Tcl_GetVar (p->interp, waitVar, 0)))
192 waitVarVal = malloc (strlen(cp)+1);
193 strcpy (waitVarVal, cp);
199 sprintf (msg, "Variable %s doesn't exist", waitVar);
200 gw_log (GW_LOG_WARN, mod, "%s", msg);
201 Tcl_AppendResult (p->interp, msg, NULL);
204 gw_log (GW_LOG_DEBUG, mod, "Waiting %s=%s", waitVar, waitVarVal);
207 if (!(cp = Tcl_GetVar (p->interp, waitVar, 0)) ||
208 strcmp (cp, waitVarVal))
210 gw_log (GW_LOG_DEBUG, mod, "Changed to %s", cp);
211 Tcl_AppendResult (p->interp, cp, NULL);
215 FD_ZERO (&fdset_tcl_r);
216 FD_ZERO (&fdset_tcl_w);
217 FD_ZERO (&fdset_tcl_x);
219 for (r=0, i=0; i<=max_fd; i++)
221 if (callback_table[i].w_handle)
223 FD_SET (i, &fdset_tcl_w);
226 if (callback_table[i].r_handle)
228 FD_SET (i, &fdset_tcl_r);
231 if (callback_table[i].x_handle)
233 FD_SET (i, &fdset_tcl_x);
239 gw_log (GW_LOG_DEBUG, mod, "fifo select %d", fifo_in);
240 FD_SET (fifo_in, &fdset_tcl_r);
241 if ((r = select(max_fd+1, &fdset_tcl_r, &fdset_tcl_w,
242 &fdset_tcl_x, 300)) < 0)
244 gw_log (GW_LOG_ERRNO|GW_LOG_FATAL, mod, "select");
249 gw_log (GW_LOG_DEBUG, mod, "timeout");
252 if (FD_ISSET (fifo_in, &fdset_tcl_r))
254 gw_log (GW_LOG_DEBUG, mod, "FIFO closed");
257 for (i=0; i<=max_fd; i++)
259 if (FD_ISSET (i, &fdset_tcl_r))
261 assert (callback_table[i].r_handle);
262 (*callback_table[i].r_handle) (callback_table[i].obj);
264 if (FD_ISSET (i, &fdset_tcl_w))
266 assert (callback_table[i].w_handle);
267 (*callback_table[i].w_handle) (callback_table[i].obj);
269 if (FD_ISSET (i, &fdset_tcl_x))
271 assert (callback_table[i].x_handle);
272 (*callback_table[i].x_handle) (callback_table[i].obj);
280 void ir_select_add (int fd, void *obj)
282 callback_table[fd].obj = obj;
283 callback_table[fd].r_handle = ir_select_read;
284 callback_table[fd].w_handle = NULL;
285 callback_table[fd].x_handle = NULL;
290 void ir_select_add_write (int fd, void *obj)
292 callback_table[fd].w_handle = ir_select_write;
297 void ir_select_remove_write (int fd, void *obj)
299 callback_table[fd].w_handle = NULL;
302 void ir_select_remove (int fd, void *obj)
304 callback_table[fd].r_handle = NULL;
305 callback_table[fd].w_handle = NULL;
306 callback_table[fd].x_handle = NULL;