2 * Copyright (c) 1995, the EUROPAGATE consortium (see below).
4 * The EUROPAGATE consortium members are:
6 * University College Dublin
7 * Danmarks Teknologiske Videnscenter
8 * An Chomhairle Leabharlanna
9 * Consejo Superior de Investigaciones Cientificas
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,
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.
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.
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.
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.
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.
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.
49 * Revision 1.2 1995/10/23 16:55:43 adam
50 * A lot of changes - really.
52 * Revision 1.1 1995/10/20 14:02:42 adam
53 * First version of WWW gateway with embedded Tcl.
65 static void *do_create (WCLIENT wcl, void *args);
66 static int do_exec (const char *fname, char *parms, void *mydata);
68 static struct w_interp_type w_interp_t = {
74 W_Interp_Type w_interp_tcl = &w_interp_t;
77 static char *mod = "wtcl";
87 Tcl_Interp *w_interp_tcl_get (W_Interp w_interp)
91 if (strcmp (w_interp->ctrl->name, "tcl"))
93 gw_log (GW_LOG_FATAL, mod, "Internal failure");
96 p = (struct tcl_info*) w_interp->mydata;
100 static int proc_html_invoke (ClientData clientData, Tcl_Interp *interp,
101 int argc, char **argv)
103 struct tcl_info *p = (struct tcl_info*) clientData;
106 for (i = 1; i<argc; i++)
107 wo_puts (p->wcl, argv[i]);
111 static int proc_htmlr_invoke (ClientData clientData, Tcl_Interp *interp,
112 int argc, char **argv)
114 struct tcl_info *p = (struct tcl_info*) clientData;
117 r = proc_html_invoke (clientData, interp, argc, argv);
118 wo_putc (p->wcl, '\n');
122 static int proc_form_invoke (ClientData clientData, Tcl_Interp *interp,
123 int argc, char **argv)
125 struct tcl_info *p = (struct tcl_info*) clientData;
129 Tcl_AppendResult (p->interp, wgetval (p->wcl, argv[1]), NULL);
132 for (i = 0; *p->wcl->wf_data[i].name; i++)
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);
142 int Tcl_AppInit (Tcl_Interp *interp)
144 if (Tcl_Init (interp) == TCL_ERROR)
149 static void *do_create (WCLIENT wcl, void *args)
154 if (!(p = malloc (sizeof(*p))))
156 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "malloc: tcl_info");
159 if (!(p->interp = Tcl_CreateInterp ()))
161 gw_log (GW_LOG_FATAL, mod, "Cannot make Tcl_Interp");
166 if (!(p->fbuf = malloc (p->fbuf_size)))
168 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "malloc: tcl_info fbuf");
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);
180 static void report_error (struct tcl_info *p, int errorLine,
181 const char *pre, const char *msg)
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);
189 static int tcl_exec (const char *fname, char *parms,
190 struct tcl_info *p, FILE *inf, int *lineno)
192 int c, escape = 0, level = 0;
198 if (fbuf_ptr == p->fbuf_size-1)
202 if (!(newb = malloc (p->fbuf_size += 16384)))
204 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "malloc: fbuf");
207 memcpy (newb, p->fbuf, fbuf_ptr);
214 report_error (p, *lineno, "Error in Tcl script starting at line",
215 "Unexpected EOF (missing right brace)");
220 else if (c == '{' && !escape)
225 else if (c == '}' && !escape)
237 p->fbuf[fbuf_ptr++] = c;
239 p->fbuf[fbuf_ptr] = '\0';
240 r = Tcl_Eval (p->interp, p->fbuf);
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;
249 static int do_exec (const char *fname, char *parms, void *mydata)
251 struct tcl_info *p = mydata;
254 FILE *inf = fopen (fname, "r");
256 gw_log (GW_LOG_DEBUG, mod, "Executing %s", fname);
259 gw_log (GW_LOG_WARN|GW_LOG_ERRNO, mod, "open %s", fname);
262 Tcl_SetVar (p->interp, "sessionParms", parms, TCL_GLOBAL_ONLY);
263 while ((c = getc(inf)) != EOF)
273 if (tcl_exec (fname, parms, p, inf, &lineno))