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