First version of WWW gateway with embedded Tcl.
[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.1  1995/10/20 14:02:42  adam
45  * First version of WWW gateway with embedded Tcl.
46  *
47  */
48
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <assert.h>
53 #include <ctype.h>
54
55 #include <tcl.h>
56
57 #include "wproto.h"
58 #include "winterp.h"
59
60 static void *do_create (void *args);
61 static int do_exec (WCLIENT wcl, const char *fname, char *parms, void *mydata);
62
63 static struct w_interp_type w_interp_t = {
64     "tcl",
65     do_create,
66     do_exec
67 };
68
69 W_Interp_Type w_interp_tcl = &w_interp_t;
70
71
72 static char *mod = "wtcl";
73
74 struct tcl_info {
75     Tcl_Interp *interp;
76     char  *fbuf;
77     int    fbuf_size;
78     int    fbuf_ptr;
79 };
80
81 static void *do_create (void *args)
82 {
83     struct tcl_info *p;
84
85     if (!(p = malloc (sizeof(*p))))
86     {
87         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "malloc: tcl_info");
88         exit (1);
89     }
90     if (!(p->interp = Tcl_CreateInterp ()))
91     {
92         gw_log (GW_LOG_FATAL, mod, "Cannot make Tcl_Interp");
93         exit (1);
94     }
95     p->fbuf_size = 1024;
96     if (!(p->fbuf = malloc (p->fbuf_size)))
97     {
98         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "malloc: tcl_info fbuf");
99         exit (1);
100     }
101     return p;
102 }
103
104 static int tcl_exec (WCLIENT wcl, const char *fname, char *parms,
105                      struct tcl_info *p, FILE *inf, int *lineno)
106 {
107     int c, escape = 0, level = 0;
108     int r, fbuf_ptr = 0;
109     int local_line = 0;
110
111     gw_log (GW_LOG_DEBUG, mod, "tcl_exec. line %d", *lineno);
112     while (1)
113     {
114         if (fbuf_ptr == p->fbuf_size-1)
115         {
116             char *newb;
117
118             if (!(newb = malloc (p->fbuf_size += 16384)))
119             {
120                 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "malloc: fbuf");
121                 exit (1);
122             }
123             memcpy (newb, p->fbuf, fbuf_ptr);
124             free (p->fbuf);
125             p->fbuf = newb;
126         }
127         c = getc (inf);
128         if (c == EOF)
129         {
130             gw_log (GW_LOG_WARN, mod, "Unexpected EOF: unbalanced braces");
131             return -1;
132         }
133         if (c == '\\')
134             escape = 1;
135         else if (c == '{' && !escape)
136         {
137             level++;
138             escape = 0;
139         }
140         else if (c == '}' && !escape)
141         {
142             if (--level < 0)
143                 break;
144             escape = 0;
145         }
146         else
147         {
148             if (c == '\n')
149                 local_line++;
150             escape = 0;
151         }
152         p->fbuf[fbuf_ptr++] = c;
153     }
154     p->fbuf[fbuf_ptr] = '\0';
155     gw_log (GW_LOG_DEBUG, mod, "Tcl_Eval. %d lines", local_line);
156     r = Tcl_Eval (p->interp, p->fbuf);
157     if (r != TCL_OK)
158     {
159         gw_log (GW_LOG_WARN, mod, "Error in Tcl script starting on line %d",
160                 *lineno);
161     }
162     (*lineno) += local_line;
163     return 0;
164 }
165
166 static int do_exec (WCLIENT wcl, const char *fname, char *parms,
167                     void *mydata)
168 {
169     struct tcl_info *p = mydata;
170     int c, escape = 0;
171     int lineno = 1;
172     FILE *inf = fopen (fname, "r");
173
174     gw_log (GW_LOG_DEBUG, mod, "Executing %s", fname);
175     if (!inf)
176     {
177         gw_log (GW_LOG_WARN|GW_LOG_ERRNO, mod, "open %s", fname);
178         return -1;
179     }
180     while ((c = getc(inf)) != EOF)
181     {
182         if (c == '\\')
183             escape = 1;
184         else if (c == '{')
185         {
186             if (escape)
187                 wo_putc (wcl, c);
188             else
189             {
190                 if (tcl_exec (wcl, fname, parms, p, inf, &lineno))
191                 {
192                     fclose (inf);
193                     return -2;
194                 }
195             }
196             escape = 0;
197         }
198         else
199         {
200             if (c == '\n')
201                 lineno++;
202             escape = 0;
203             wo_putc (wcl, c);
204         }
205     }
206     fclose (inf);
207     return 0;
208 }