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