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.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.
49 * Revision 1.8 1995/11/06 17:44:22 adam
50 * State reestablised when shell restarts. History of previous
53 * Revision 1.7 1995/11/02 16:35:37 adam
54 * Bug fixes and select on FIFOs in wcgi - doesn't really work!
56 * Revision 1.6 1995/11/01 16:15:47 adam
57 * Better presentation of records. Query/set number persistent.
59 * Revision 1.5 1995/10/31 16:56:24 adam
60 * Record presentation.
62 * Revision 1.4 1995/10/31 10:03:53 adam
64 * New command implemented - aborts script.
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.
70 * Revision 1.2 1995/10/27 17:30:16 adam
71 * First search request/response that works.
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.
84 #include <sys/types.h>
86 #include <sys/select.h>
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);
101 static struct w_interp_type w_interp_t = {
109 W_Interp_Type w_interp_irtcl = &w_interp_t;
112 static char *mod = "wirtcl";
121 static int events (struct tcl_info *p, char *waitVar, int tout);
123 static int proc_zwait_invoke (ClientData clientData, Tcl_Interp *interp,
124 int argc, char **argv)
126 struct tcl_info *p = (struct tcl_info*) clientData;
130 return events (p, argv[1], (argc == 3) ? atoi(argv[2]) : 0);
135 /* select(2) callbacks */
137 void (*r_handle)(ClientData);
138 void (*w_handle)(ClientData);
139 void (*x_handle)(ClientData);
142 #define MAX_CALLBACK 200
144 static struct callback callback_table[MAX_CALLBACK];
145 static int max_fd = 3; /* don't worry: it will grow... */
147 static void *do_create (WCLIENT wcl, void *args)
152 if (!(p = malloc (sizeof(*p))))
154 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "malloc: irtcl_info");
157 if (!(p->w_interp = w_interp_create (w_interp_tcl, wcl, NULL)))
159 gw_log (GW_LOG_FATAL, mod, "Cannot make Tcl_Interp");
163 p->interp = w_interp_tcl_get (p->w_interp);
164 if (Irtcl_Init(p->interp) == TCL_ERROR)
166 gw_log (GW_LOG_FATAL, mod, "Cannot make Irtcl_Interp");
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++)
174 callback_table[i].r_handle = NULL;
175 callback_table[i].w_handle = NULL;
176 callback_table[i].x_handle = NULL;
182 static int do_exec (const char *fname, char *parms, void *mydata)
184 struct tcl_info *p = mydata;
186 if ((r = w_interp_exec (p->w_interp, fname, parms)))
192 static int events (struct tcl_info *p, char *waitVar, int tout)
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)
205 if ((cp = Tcl_GetVar (p->interp, waitVar, 0)))
207 waitVarVal = malloc (strlen(cp)+1);
208 strcpy (waitVarVal, cp);
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);
219 gw_log (GW_LOG_DEBUG, mod, "Waiting %s=%s", waitVar, waitVarVal);
222 struct timeval to, *top;
232 if (!(cp = Tcl_GetVar (p->interp, waitVar, 0)) ||
233 strcmp (cp, waitVarVal))
235 gw_log (GW_LOG_DEBUG, mod, "Changed to %s", cp);
236 Tcl_AppendResult (p->interp, cp, NULL);
240 FD_ZERO (&fdset_tcl_r);
241 FD_ZERO (&fdset_tcl_w);
242 FD_ZERO (&fdset_tcl_x);
244 for (r=0, i=0; i<=max_fd; i++)
246 if (callback_table[i].w_handle)
248 FD_SET (i, &fdset_tcl_w);
251 if (callback_table[i].r_handle)
253 FD_SET (i, &fdset_tcl_r);
256 if (callback_table[i].x_handle)
258 FD_SET (i, &fdset_tcl_x);
265 gw_log (GW_LOG_DEBUG, mod, "fifo select %d", fifo_in);
266 FD_SET (fifo_in, &fdset_tcl_r);
268 if ((r = select(max_fd+1, &fdset_tcl_r, &fdset_tcl_w,
269 &fdset_tcl_x, top)) < 0)
271 gw_log (GW_LOG_ERRNO|GW_LOG_FATAL, mod, "select");
276 gw_log (GW_LOG_DEBUG, mod, "timeout");
280 if (FD_ISSET (fifo_in, &fdset_tcl_r))
282 gw_log (GW_LOG_DEBUG, mod, "FIFO closed");
286 for (i=0; i<=max_fd; i++)
288 if (FD_ISSET (i, &fdset_tcl_r))
290 assert (callback_table[i].r_handle);
291 (*callback_table[i].r_handle) (callback_table[i].obj);
293 if (FD_ISSET (i, &fdset_tcl_w))
295 assert (callback_table[i].w_handle);
296 (*callback_table[i].w_handle) (callback_table[i].obj);
298 if (FD_ISSET (i, &fdset_tcl_x))
300 assert (callback_table[i].x_handle);
301 (*callback_table[i].x_handle) (callback_table[i].obj);
309 void ir_select_add (int fd, void *obj)
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;
319 void ir_select_add_write (int fd, void *obj)
321 callback_table[fd].w_handle = ir_select_write;
326 void ir_select_remove_write (int fd, void *obj)
328 callback_table[fd].w_handle = NULL;
331 void ir_select_remove (int fd, void *obj)
333 callback_table[fd].r_handle = NULL;
334 callback_table[fd].w_handle = NULL;
335 callback_table[fd].x_handle = NULL;
338 static int do_load (char *parms, void *mydata)
340 struct tcl_info *p = mydata;
342 return w_interp_load_state (p->w_interp, parms);
345 static int do_save (char *parms, void *mydata)
347 struct tcl_info *p = mydata;
349 return w_interp_save_state (p->w_interp, parms);