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