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