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