Modified to use ir_tcl_select_set.
[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.15  1996/02/21 14:58:01  adam
45  * Modified to use ir_tcl_select_set.
46  *
47  * Revision 1.14  1996/02/12  10:10:31  adam
48  * Resource/config system used by the gateway.
49  *
50  * Revision 1.13  1996/01/24  08:26:54  adam
51  * All tcl commands prefixed with egw_ (except the html command).
52  *
53  * Revision 1.12  1996/01/12  10:05:18  adam
54  * If script name ends with ';' HTTP/GET/Expires will be defined.
55  * The cgi interface only reads final handshake if response from
56  * server (shell) was zero-terminated [If it isn't it probably died].
57  *
58  * Revision 1.11  1996/01/09  16:16:49  adam
59  * Port to OSF/1. Gif references moved from /gif/ to /egwgif/.
60  *
61  * Revision 1.10  1996/01/09  10:46:50  adam
62  * New defines: LOGDIR/EGWDIR/CGIDIR set in Makefile.
63  *
64  * Revision 1.9  1995/11/07  14:56:59  adam
65  * Work on search in multiple targets.
66  * New wtcl command: wlog.
67  * Optional timeout parameter to zwait.
68  *
69  * Revision 1.8  1995/11/06  17:44:22  adam
70  * State reestablised when shell restarts. History of previous
71  * result sets.
72  *
73  * Revision 1.7  1995/11/02  16:35:37  adam
74  * Bug fixes and select on FIFOs in wcgi - doesn't really work!
75  *
76  * Revision 1.6  1995/11/01  16:15:47  adam
77  * Better presentation of records. Query/set number persistent.
78  *
79  * Revision 1.5  1995/10/31  16:56:24  adam
80  * Record presentation.
81  *
82  * Revision 1.4  1995/10/31  10:03:53  adam
83  * Work on queries.
84  * New command implemented - aborts script.
85  *
86  * Revision 1.3  1995/10/30  17:35:18  adam
87  * New function zwait that waits for a variable change - due to i/o events
88  * that invoke callback routines.
89  *
90  * Revision 1.2  1995/10/27  17:30:16  adam
91  * First search request/response that works.
92  *
93  * Revision 1.1  1995/10/27  15:12:08  adam
94  * IrTcl incorporated in the gateway.
95  * Better separation of script types.
96  * Z39.50 gateway scripts entered.
97  *
98  */
99
100 #include <stdio.h>
101 #include <stdlib.h>
102 #include <unistd.h>
103 #include <sys/time.h>
104 #include <sys/types.h>
105 #ifdef AIX
106 #include <sys/select.h>
107 #endif
108 #include <string.h>
109 #include <assert.h>
110 #include <ctype.h>
111
112 #include <log.h>
113 #include "wtcl.h"
114 #include "wirtcl.h"
115
116 static void *do_create (WCLIENT wcl, void *args);
117 static int do_exec (const char *fname, char *parms, void *mydata);
118 static int do_load (char *parms, void *mydata);
119 static int do_save (char *parms, void *mydata);
120
121 static struct w_interp_type w_interp_t = {
122     "irtcl",
123     do_create,
124     do_exec,
125     do_load,
126     do_save
127 };
128
129 W_Interp_Type w_interp_irtcl = &w_interp_t;
130
131
132 static char *mod = "wirtcl";
133
134 struct tcl_info {
135     W_Interp w_interp;
136     Tcl_Interp *interp;
137     WCLIENT wcl;
138 };
139
140 static int events (struct tcl_info *p, char *waitVar, int tout);
141
142 static int proc_zwait_invoke (ClientData clientData, Tcl_Interp *interp,
143                               int argc, char **argv)
144 {
145     struct tcl_info *p = (struct tcl_info*) clientData;
146     
147     if (argc < 2)
148         return TCL_OK;
149     return events (p, argv[1], (argc == 3) ? atoi(argv[2]) : 0);
150 }
151
152 /* select callbacks */
153 struct callback {
154     void (*handle)(ClientData, int, int, int);
155     int r, w, e;
156     ClientData obj;
157 };
158 #define MAX_CALLBACK 200
159
160 static struct callback callback_table[MAX_CALLBACK];
161 static int max_fd = 3;            /* don't worry: it will grow... */
162
163 static void *do_create (WCLIENT wcl, void *args)
164 {
165     struct tcl_info *p;
166     int i;
167
168     if (!(p = malloc (sizeof(*p))))
169     {
170         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "malloc: irtcl_info");
171         exit (1);
172     }
173     if (!(p->w_interp = w_interp_create (w_interp_tcl, wcl, NULL)))
174     {
175         gw_log (GW_LOG_FATAL, mod, "Cannot make Tcl_Interp");
176         exit (1);
177     }
178     p->wcl = wcl;
179     p->interp = w_interp_tcl_get (p->w_interp);
180     if (Irtcl_Init(p->interp) == TCL_ERROR)
181     {
182         gw_log (GW_LOG_FATAL, mod, "Cannot make Irtcl_Interp");
183         exit (1);
184     }
185     log_init(LOG_ALL, "irtcl", "irtcl_log");
186     /* initialize irtcl */
187     Tcl_CreateCommand (p->interp, "egw_wait", proc_zwait_invoke, p, NULL);
188     for (i=0; i<MAX_CALLBACK; i++)
189         callback_table[i].handle = NULL;
190     return p;
191 }
192
193
194 static int do_exec (const char *fname, char *parms, void *mydata)
195 {
196     struct tcl_info *p = mydata;
197     int r;
198     if ((r = w_interp_exec (p->w_interp, fname, parms)))
199         return r;
200     return 0;
201 }
202
203
204 static int events (struct tcl_info *p, char *waitVar, int tout)
205 {
206     int r, i;
207     char *cp;
208     char *waitVarVal;
209     static fd_set fdset_tcl_r;
210     static fd_set fdset_tcl_w;
211     static fd_set fdset_tcl_x;
212     int fifo_in = p->wcl->linein;
213     if (fifo_in > max_fd)
214         max_fd = fifo_in;
215
216     assert (waitVar);
217     if ((cp = Tcl_GetVar (p->interp, waitVar, 0)))
218     {
219         waitVarVal = malloc (strlen(cp)+1);
220         strcpy (waitVarVal, cp);
221     }
222     else
223     {
224         char msg[128];
225
226         sprintf (msg, "Variable %s doesn't exist", waitVar);
227         gw_log (GW_LOG_WARN, mod, "%s", msg);
228         Tcl_AppendResult (p->interp, msg, NULL);
229         return TCL_ERROR;
230     }
231     gw_log (GW_LOG_DEBUG, mod, "Waiting %s=%s", waitVar, waitVarVal);
232     while (1)
233     {
234         struct timeval to, *top;
235         if (tout > 0)
236         {
237             to.tv_usec = 0;
238             to.tv_sec = tout;
239             top = &to;
240         }
241         else
242             top = 0;
243
244         if (!(cp = Tcl_GetVar (p->interp, waitVar, 0)) ||
245             strcmp (cp, waitVarVal))
246         {
247             gw_log (GW_LOG_DEBUG, mod, "Changed to %s", cp);
248             Tcl_AppendResult (p->interp, cp, NULL);
249             free (waitVarVal);
250             return TCL_OK;
251         }
252         FD_ZERO (&fdset_tcl_r);
253         FD_ZERO (&fdset_tcl_w);
254         FD_ZERO (&fdset_tcl_x);
255         
256         for (r=0, i=0; i<=max_fd; i++)
257         {
258             if (callback_table[i].handle && callback_table[i].w)
259             {
260                 FD_SET (i, &fdset_tcl_w);
261                 r++;
262             }
263             if (callback_table[i].handle && callback_table[i].r)
264             {
265                 FD_SET (i, &fdset_tcl_r);
266                 r++;
267             }
268             if (callback_table[i].handle && callback_table[i].e)
269             {
270                 FD_SET (i, &fdset_tcl_x);
271                 r++;
272             }
273         }
274         if (!r)
275             break;
276 #if 1
277         gw_log (GW_LOG_DEBUG, mod, "fifo select %d", fifo_in);
278         FD_SET (fifo_in, &fdset_tcl_r);
279 #endif
280         if ((r = select(max_fd+1, &fdset_tcl_r, &fdset_tcl_w, 
281                           &fdset_tcl_x, top)) < 0)
282         {
283             gw_log (GW_LOG_ERRNO|GW_LOG_FATAL, mod, "select");
284             exit(1);
285         }
286         if (!r)
287         {
288             gw_log (GW_LOG_DEBUG, mod, "timeout");
289             free (waitVarVal);
290             return TCL_ERROR;
291         }
292         if (FD_ISSET (fifo_in, &fdset_tcl_r))
293         {
294             gw_log (GW_LOG_DEBUG, mod, "FIFO closed");
295             free (waitVarVal);
296             return TCL_ERROR;
297         }
298         for (i=0; i<=max_fd; i++)
299         {
300             int r_flag = 0;
301             int w_flag = 0;
302             int e_flag = 0;
303
304             if (!callback_table[i].handle)
305                 continue;
306             if (FD_ISSET (i, &fdset_tcl_r) && callback_table[i].r)
307                 r_flag = 1;
308             if (FD_ISSET (i, &fdset_tcl_w) && callback_table[i].w)
309                 w_flag = 1;
310             if (FD_ISSET (i, &fdset_tcl_x) && callback_table[i].e)
311                 e_flag = 1;
312             if (r_flag || w_flag || e_flag)
313                 (*callback_table[i].handle)(callback_table[i].obj,
314                  r_flag, w_flag, e_flag);
315         }
316     }
317     free (waitVarVal);
318     return TCL_OK;
319 }
320
321 void ir_tcl_select_set (void (*f)(ClientData clientData, int r, int w, int e),
322                         int fd, ClientData clientData, int r, int w, int e)
323 {
324     callback_table[fd].handle = f;
325     callback_table[fd].obj = clientData;
326     callback_table[fd].r = r;
327     callback_table[fd].w = w;
328     callback_table[fd].e = e;
329     if (fd > max_fd)
330         max_fd = fd;
331 }
332
333 static int do_load (char *parms, void *mydata)
334 {
335     struct tcl_info *p = mydata;
336
337     return w_interp_load_state (p->w_interp, parms);
338 }
339
340 static int do_save (char *parms, void *mydata)
341 {
342     struct tcl_info *p = mydata;
343
344     return w_interp_save_state (p->w_interp, parms);
345 }
346