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