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