Record presentation.
[egate.git] / www / wtcl.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: wtcl.c,v $
44  * Revision 1.7  1995/10/31 16:56:25  adam
45  * Record presentation.
46  *
47  * Revision 1.6  1995/10/31  10:03:54  adam
48  * Work on queries.
49  * New command implemented - aborts script.
50  *
51  * Revision 1.5  1995/10/30  17:35:18  adam
52  * New function zwait that waits for a variable change - due to i/o events
53  * that invoke callback routines.
54  *
55  * Revision 1.4  1995/10/27  17:30:16  adam
56  * First search request/response that works.
57  *
58  * Revision 1.3  1995/10/27  15:12:14  adam
59  * IrTcl incorporated in the gateway.
60  * Better separation of script types.
61  * Z39.50 gateway scripts entered.
62  *
63  * Revision 1.2  1995/10/23  16:55:43  adam
64  * A lot of changes - really.
65  *
66  * Revision 1.1  1995/10/20  14:02:42  adam
67  * First version of WWW gateway with embedded Tcl.
68  *
69  */
70
71 #include <stdio.h>
72 #include <stdlib.h>
73 #include <string.h>
74 #include <assert.h>
75 #include <ctype.h>
76
77 #include "wtcl.h"
78
79 static void *do_create (WCLIENT wcl, void *args);
80 static int do_exec (const char *fname, char *parms, void *mydata);
81
82 static struct w_interp_type w_interp_t = {
83     "tcl",
84     do_create,
85     do_exec
86 };
87
88 W_Interp_Type w_interp_tcl = &w_interp_t;
89
90
91 static char *mod = "wtcl";
92
93 struct tcl_info {
94     Tcl_Interp *interp;
95     char  *fbuf;
96     int    fbuf_size;
97     int    fbuf_ptr;
98     int    wabort;
99     WCLIENT wcl;
100 };
101
102 Tcl_Interp *w_interp_tcl_get (W_Interp w_interp)
103 {
104     struct tcl_info *p;
105
106     if (strcmp (w_interp->ctrl->name, "tcl"))
107     {
108         gw_log (GW_LOG_FATAL, mod, "Internal failure");
109         assert (0);
110     }
111     p = (struct tcl_info*) w_interp->mydata;
112     return p->interp;
113 }
114
115 static int proc_wabort_invoke (ClientData clientData, Tcl_Interp *interp,
116                                int argc, char **argv)
117 {
118     struct tcl_info *p = (struct tcl_info*) clientData;
119
120     p->wabort = 1;
121     return TCL_RETURN;
122 }
123
124 static int proc_wflush_invoke (ClientData clientData, Tcl_Interp *interp,
125                                int argc, char **argv)
126 {
127     struct tcl_info *p = (struct tcl_info*) clientData;
128
129     wo_flush (p->wcl);
130     return TCL_OK;
131 }
132
133 static int proc_html_invoke (ClientData clientData, Tcl_Interp *interp,
134                              int argc, char **argv)
135 {
136     struct tcl_info *p = (struct tcl_info*) clientData;
137     int i;
138
139     for (i = 1; i<argc; i++)
140         wo_puts (p->wcl, argv[i]);
141     return TCL_OK;
142 }
143
144 static int proc_htmlr_invoke (ClientData clientData, Tcl_Interp *interp,
145                               int argc, char **argv)
146 {
147     struct tcl_info *p = (struct tcl_info*) clientData;
148     int r;
149
150     r = proc_html_invoke (clientData, interp, argc, argv);
151     wo_putc (p->wcl, '\n');
152     return r;
153 }
154
155 static int proc_wform_invoke (ClientData clientData, Tcl_Interp *interp,
156                               int argc, char **argv)
157 {
158     struct tcl_info *p = (struct tcl_info*) clientData;
159     int i;
160     if (argc == 2)
161     {
162         for (i = 0; *p->wcl->wf_data[i].name; i++)
163             if (!strcmp (argv[1], p->wcl->wf_data[i].name) && 
164                 *p->wcl->wf_data[i].value)
165                 Tcl_AppendElement (p->interp, p->wcl->wf_data[i].value);
166         return TCL_OK;
167     }    
168     for (i = 0; *p->wcl->wf_data[i].name; i++)
169     { 
170         Tcl_AppendResult (p->interp, "{ ", NULL);
171         Tcl_AppendElement (p->interp, p->wcl->wf_data[i].name);
172         Tcl_AppendElement (p->interp, p->wcl->wf_data[i].value);
173         Tcl_AppendResult (p->interp, " }\n", NULL);
174     }
175     return TCL_OK;
176 }
177
178 int Tcl_AppInit (Tcl_Interp *interp)
179 {
180     if (Tcl_Init (interp) == TCL_ERROR)
181         return TCL_ERROR;
182     return TCL_OK;
183 }
184
185 static void *do_create (WCLIENT wcl, void *args)
186 {
187     struct tcl_info *p;
188     char tmp_str[256];
189
190     if (!(p = malloc (sizeof(*p))))
191     {
192         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "malloc: tcl_info");
193         exit (1);
194     }
195     if (!(p->interp = Tcl_CreateInterp ()))
196     {
197         gw_log (GW_LOG_FATAL, mod, "Cannot make Tcl_Interp");
198         exit (1);
199     }
200     p->wcl = wcl;
201     p->fbuf_size = 1024;
202     if (!(p->fbuf = malloc (p->fbuf_size)))
203     {
204         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "malloc: tcl_info fbuf");
205         exit (1);
206     }
207     Tcl_AppInit (p->interp);
208     Tcl_CreateCommand (p->interp, "html", proc_html_invoke, p, NULL);
209     Tcl_CreateCommand (p->interp, "htmlr", proc_htmlr_invoke, p, NULL);
210     Tcl_CreateCommand (p->interp, "wform", proc_wform_invoke, p, NULL);
211     Tcl_CreateCommand (p->interp, "wabort", proc_wabort_invoke, p, NULL);
212     Tcl_CreateCommand (p->interp, "wflush", proc_wflush_invoke, p, NULL);
213     sprintf (tmp_str, "%d", wcl->id);
214     Tcl_SetVar (p->interp, "sessionId", tmp_str, TCL_GLOBAL_ONLY);
215     return p;
216 }
217
218 static void report_error (struct tcl_info *p, int errorLine,
219                           const char *pre, const char *msg)
220 {
221     gw_log (GW_LOG_WARN, mod, "%s %d %s", pre, errorLine, msg);
222     wo_printf (p->wcl, "\n<br><hr>\n<strong>"
223                "%s %d</strong><br>\n", pre, errorLine);
224     wo_printf (p->wcl, "<xmp>\n%s</xmp>\n<hr>\n", msg);
225 }
226
227 static int tcl_exec (const char *fname, char *parms,
228                      struct tcl_info *p, FILE *inf, int *lineno)
229 {
230     int c, escape = 0, level = 0;
231     int r, fbuf_ptr = 0;
232     int local_line = 0;
233
234     while (1)
235     {
236         if (fbuf_ptr == p->fbuf_size-1)
237         {
238             char *newb;
239
240             if (!(newb = malloc (p->fbuf_size += 16384)))
241             {
242                 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "malloc: fbuf");
243                 exit (1);
244             }
245             memcpy (newb, p->fbuf, fbuf_ptr);
246             free (p->fbuf);
247             p->fbuf = newb;
248         }
249         c = getc (inf);
250         if (c == EOF)
251         {
252             report_error (p, *lineno, "Error in Tcl script starting at line",
253                                       "Unexpected EOF (missing right brace)");
254             return TCL_ERROR;
255         }
256         if (c == '\\')
257             escape = 1;
258         else if (c == '{' && !escape)
259         {
260             level++;
261             escape = 0;
262         }
263         else if (c == '}' && !escape)
264         {
265             if (--level < 0)
266                 break;
267             escape = 0;
268         }
269         else
270         {
271             if (c == '\n')
272                 local_line++;
273             escape = 0;
274         }
275         p->fbuf[fbuf_ptr++] = c;
276     }
277     p->fbuf[fbuf_ptr] = '\0';
278     p->wabort = 0;
279     r = Tcl_Eval (p->interp, p->fbuf);
280     if (r == TCL_ERROR)
281         report_error (p, p->interp->errorLine + *lineno - 1, 
282                       "Error in Tcl script in line", 
283                       Tcl_GetVar (p->interp, "errorInfo", 0));
284     (*lineno) += local_line;
285     if (p->wabort)
286         return TCL_RETURN;
287     return r;
288 }
289
290 static int do_exec (const char *fname, char *parms, void *mydata)
291 {
292     struct tcl_info *p = mydata;
293     int c, escape = 0;
294     int lineno = 1;
295     FILE *inf = fopen (fname, "r");
296
297     gw_log (GW_LOG_DEBUG, mod, "Executing %s", fname);
298     if (!inf)
299     {
300         gw_log (GW_LOG_WARN|GW_LOG_ERRNO, mod, "open %s", fname);
301         return -1;
302     }
303     Tcl_SetVar (p->interp, "sessionParms", parms, TCL_GLOBAL_ONLY);
304     while ((c = getc(inf)) != EOF)
305     {
306         if (c == '\\')
307             escape = 1;
308         else if (c == '{')
309         {
310             if (escape)
311                 wo_putc (p->wcl, c);
312             else
313             {
314                 int r = tcl_exec (fname, parms, p, inf, &lineno);
315                 if (r == TCL_RETURN)
316                 {
317                     fclose (inf);
318                     return 0;
319                 }
320                 else if (r == TCL_ERROR)
321                 {
322                     fclose (inf);
323                     return -2;
324                 }
325             }
326             escape = 0;
327         }
328         else
329         {
330             if (c == '\n')
331                 lineno++;
332             escape = 0;
333             wo_putc (p->wcl, c);
334         }
335     }
336     fclose (inf);
337     return 0;
338 }