First search request/response that works.
[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.2  1995/10/27 17:30:16  adam
45  * First search request/response that works.
46  *
47  * Revision 1.1  1995/10/27  15:12:08  adam
48  * IrTcl incorporated in the gateway.
49  * Better separation of script types.
50  * Z39.50 gateway scripts entered.
51  *
52  */
53
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <unistd.h>
57 #include <sys/time.h>
58 #include <sys/types.h>
59 #ifdef AIX
60 #include <sys/select.h>
61 #endif
62 #include <string.h>
63 #include <assert.h>
64 #include <ctype.h>
65
66 #include "wtcl.h"
67 #include "wirtcl.h"
68
69 static void *do_create (WCLIENT wcl, void *args);
70 static int do_exec (const char *fname, char *parms, void *mydata);
71
72 static struct w_interp_type w_interp_t = {
73     "irtcl",
74     do_create,
75     do_exec
76 };
77
78 W_Interp_Type w_interp_irtcl = &w_interp_t;
79
80
81 static char *mod = "wirtcl";
82
83 struct tcl_info {
84     W_Interp w_interp;
85     Tcl_Interp *interp;
86     WCLIENT wcl;
87 };
88
89
90 /* select(2) callbacks */
91 struct callback {
92     void (*r_handle)(ClientData);
93     void (*w_handle)(ClientData);
94     void (*x_handle)(ClientData);
95     void *obj;
96 };
97 #define MAX_CALLBACK 200
98
99 static struct callback callback_table[MAX_CALLBACK];
100 static int max_fd = 3;            /* don't worry: it will grow... */
101
102 static void *do_create (WCLIENT wcl, void *args)
103 {
104     struct tcl_info *p;
105     int i;
106
107     if (!(p = malloc (sizeof(*p))))
108     {
109         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "malloc: irtcl_info");
110         exit (1);
111     }
112     if (!(p->w_interp = w_interp_create (w_interp_tcl, wcl, NULL)))
113     {
114         gw_log (GW_LOG_FATAL, mod, "Cannot make Tcl_Interp");
115         exit (1);
116     }
117     p->wcl = wcl;
118     p->interp = w_interp_tcl_get (p->w_interp);
119     if (Irtcl_Init(p->interp) == TCL_ERROR)
120     {
121         gw_log (GW_LOG_FATAL, mod, "Cannot make Irtcl_Interp");
122         exit (1);
123     }
124     /* initialize irtcl */
125     for (i=0; i<MAX_CALLBACK; i++)
126     {
127         callback_table[i].r_handle = NULL;
128         callback_table[i].w_handle = NULL;
129         callback_table[i].x_handle = NULL;
130     }
131     return p;
132 }
133
134
135 static int do_exec (const char *fname, char *parms, void *mydata)
136 {
137     struct tcl_info *p = mydata;
138     int i, r, min_fd = 0;
139     const char *cp;
140     static fd_set fdset_tcl_r;
141     static fd_set fdset_tcl_w;
142     static fd_set fdset_tcl_x;
143
144     if ((r = w_interp_exec (p->w_interp, fname, parms)))
145         return r;
146     while (1)
147     {
148         FD_ZERO (&fdset_tcl_r);
149         FD_ZERO (&fdset_tcl_w);
150         FD_ZERO (&fdset_tcl_x);
151
152         if ((cp=Tcl_GetVar (p->interp, "sessionWait", 0)) && !strcmp (cp, "0"))
153             return 0;
154         for (r=0, i=min_fd; i<=max_fd; i++)
155         {
156             if (callback_table[i].w_handle)
157             {
158                 FD_SET (i, &fdset_tcl_w);
159                 r++;
160             }
161             if (callback_table[i].r_handle)
162             {
163                 FD_SET (i, &fdset_tcl_r);
164                 r++;
165             }
166             if (callback_table[i].x_handle)
167             {
168                 FD_SET (i, &fdset_tcl_x);
169                 r++;
170             }
171         }
172         if (!r)
173             return 0;
174         if ((r = select(max_fd+1, &fdset_tcl_r, &fdset_tcl_w, 
175                           &fdset_tcl_x, 0)) < 0)
176         {
177             perror("select");
178             exit(1);
179         }
180         if (!r)
181             continue;
182         for (i=min_fd; i<=max_fd; i++)
183         {
184             if (FD_ISSET (i, &fdset_tcl_r))
185             {
186                 assert (callback_table[i].r_handle);
187                 (*callback_table[i].r_handle) (callback_table[i].obj);
188             }
189             if (FD_ISSET (i, &fdset_tcl_w))
190             {
191                 assert (callback_table[i].w_handle);
192                 (*callback_table[i].w_handle) (callback_table[i].obj);
193             }
194             if (FD_ISSET (i, &fdset_tcl_x))
195             {
196                 assert (callback_table[i].x_handle);
197                 (*callback_table[i].x_handle) (callback_table[i].obj);
198             }
199         }
200     }
201     return 0;
202 }
203
204 void ir_select_add (int fd, void *obj)
205 {
206     callback_table[fd].obj = obj;
207     callback_table[fd].r_handle = ir_select_read;
208     callback_table[fd].w_handle = NULL;
209     callback_table[fd].x_handle = NULL;
210     if (fd > max_fd)
211         max_fd = fd;
212 }
213
214 void ir_select_add_write (int fd, void *obj)
215 {
216     callback_table[fd].w_handle = ir_select_write;
217     if (fd > max_fd)
218         max_fd = fd;
219 }
220
221 void ir_select_remove_write (int fd, void *obj)
222 {
223     callback_table[fd].w_handle = NULL;
224 }
225
226 void ir_select_remove (int fd, void *obj)
227 {
228     callback_table[fd].r_handle = NULL;
229     callback_table[fd].w_handle = NULL;
230     callback_table[fd].x_handle = NULL;
231 }