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