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.8 1995/11/06 17:44:23 adam
45 * State reestablised when shell restarts. History of previous
48 * Revision 1.7 1995/10/31 16:56:25 adam
49 * Record presentation.
51 * Revision 1.6 1995/10/31 10:03:54 adam
53 * New command implemented - aborts script.
55 * Revision 1.5 1995/10/30 17:35:18 adam
56 * New function zwait that waits for a variable change - due to i/o events
57 * that invoke callback routines.
59 * Revision 1.4 1995/10/27 17:30:16 adam
60 * First search request/response that works.
62 * Revision 1.3 1995/10/27 15:12:14 adam
63 * IrTcl incorporated in the gateway.
64 * Better separation of script types.
65 * Z39.50 gateway scripts entered.
67 * Revision 1.2 1995/10/23 16:55:43 adam
68 * A lot of changes - really.
70 * Revision 1.1 1995/10/20 14:02:42 adam
71 * First version of WWW gateway with embedded Tcl.
83 static void *do_create (WCLIENT wcl, void *args);
84 static int do_exec (const char *fname, char *parms, void *mydata);
85 static int do_load (char *parms, void *mydata);
86 static int do_save (char *parms, void *mydata);
88 static struct w_interp_type w_interp_t = {
96 W_Interp_Type w_interp_tcl = &w_interp_t;
99 static char *mod = "wtcl";
110 Tcl_Interp *w_interp_tcl_get (W_Interp w_interp)
114 if (strcmp (w_interp->ctrl->name, "tcl"))
116 gw_log (GW_LOG_FATAL, mod, "Internal failure");
119 p = (struct tcl_info*) w_interp->mydata;
123 static int proc_wabort_invoke (ClientData clientData, Tcl_Interp *interp,
124 int argc, char **argv)
126 struct tcl_info *p = (struct tcl_info*) clientData;
130 Tcl_AppendResult (interp, argv[1], NULL);
134 static int proc_wflush_invoke (ClientData clientData, Tcl_Interp *interp,
135 int argc, char **argv)
137 struct tcl_info *p = (struct tcl_info*) clientData;
143 static int proc_html_invoke (ClientData clientData, Tcl_Interp *interp,
144 int argc, char **argv)
146 struct tcl_info *p = (struct tcl_info*) clientData;
149 for (i = 1; i<argc; i++)
150 wo_puts (p->wcl, argv[i]);
154 static int proc_htmlr_invoke (ClientData clientData, Tcl_Interp *interp,
155 int argc, char **argv)
157 struct tcl_info *p = (struct tcl_info*) clientData;
160 r = proc_html_invoke (clientData, interp, argc, argv);
161 wo_putc (p->wcl, '\n');
165 static int proc_wform_invoke (ClientData clientData, Tcl_Interp *interp,
166 int argc, char **argv)
168 struct tcl_info *p = (struct tcl_info*) clientData;
172 for (i = 0; *p->wcl->wf_data[i].name; i++)
173 if (!strcmp (argv[1], p->wcl->wf_data[i].name) &&
174 *p->wcl->wf_data[i].value)
175 Tcl_AppendElement (p->interp, p->wcl->wf_data[i].value);
178 for (i = 0; *p->wcl->wf_data[i].name; i++)
180 Tcl_AppendResult (p->interp, "{ ", NULL);
181 Tcl_AppendElement (p->interp, p->wcl->wf_data[i].name);
182 Tcl_AppendElement (p->interp, p->wcl->wf_data[i].value);
183 Tcl_AppendResult (p->interp, " }\n", NULL);
188 int Tcl_AppInit (Tcl_Interp *interp)
190 if (Tcl_Init (interp) == TCL_ERROR)
195 static void *do_create (WCLIENT wcl, void *args)
200 if (!(p = malloc (sizeof(*p))))
202 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "malloc: tcl_info");
205 if (!(p->interp = Tcl_CreateInterp ()))
207 gw_log (GW_LOG_FATAL, mod, "Cannot make Tcl_Interp");
212 if (!(p->fbuf = malloc (p->fbuf_size)))
214 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "malloc: tcl_info fbuf");
217 Tcl_AppInit (p->interp);
218 Tcl_CreateCommand (p->interp, "html", proc_html_invoke, p, NULL);
219 Tcl_CreateCommand (p->interp, "htmlr", proc_htmlr_invoke, p, NULL);
220 Tcl_CreateCommand (p->interp, "wform", proc_wform_invoke, p, NULL);
221 Tcl_CreateCommand (p->interp, "wabort", proc_wabort_invoke, p, NULL);
222 Tcl_CreateCommand (p->interp, "wflush", proc_wflush_invoke, p, NULL);
223 sprintf (tmp_str, "%d", wcl->id);
224 Tcl_SetVar (p->interp, "sessionId", tmp_str, TCL_GLOBAL_ONLY);
228 static void report_error (struct tcl_info *p, int errorLine,
229 const char *pre, const char *msg)
231 gw_log (GW_LOG_WARN, mod, "%s %d %s", pre, errorLine, msg);
232 wo_printf (p->wcl, "\n<br><hr>\n<strong>"
233 "%s %d</strong><br>\n", pre, errorLine);
234 wo_printf (p->wcl, "<xmp>\n%s</xmp>\n<hr>\n", msg);
237 static int tcl_exec (const char *fname, char *parms,
238 struct tcl_info *p, FILE *inf, int *lineno)
240 int c, escape = 0, level = 0;
246 if (fbuf_ptr == p->fbuf_size-1)
250 if (!(newb = malloc (p->fbuf_size += 16384)))
252 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "malloc: fbuf");
255 memcpy (newb, p->fbuf, fbuf_ptr);
262 report_error (p, *lineno, "Error in Tcl script starting at line",
263 "Unexpected EOF (missing right brace)");
268 else if (c == '{' && !escape)
273 else if (c == '}' && !escape)
285 p->fbuf[fbuf_ptr++] = c;
287 p->fbuf[fbuf_ptr] = '\0';
289 r = Tcl_Eval (p->interp, p->fbuf);
291 report_error (p, p->interp->errorLine + *lineno - 1,
292 "Error in Tcl script in line",
293 Tcl_GetVar (p->interp, "errorInfo", 0));
294 (*lineno) += local_line;
300 static int do_exec (const char *fname, char *parms, void *mydata)
302 struct tcl_info *p = mydata;
305 FILE *inf = fopen (fname, "r");
307 gw_log (GW_LOG_DEBUG, mod, "Executing %s", fname);
310 gw_log (GW_LOG_WARN|GW_LOG_ERRNO, mod, "open %s", fname);
313 Tcl_SetVar (p->interp, "sessionParms", parms, TCL_GLOBAL_ONLY);
314 while ((c = getc(inf)) != EOF)
324 int r = tcl_exec (fname, parms, p, inf, &lineno);
330 else if (r == TCL_ERROR)
351 static int do_load (char *parms, void *mydata)
353 struct tcl_info *p = mydata;
357 sprintf (fname, "tcl.state.%d", p->wcl->id);
358 r = Tcl_EvalFile (p->interp, fname);
360 report_error (p, p->interp->errorLine,
361 "Error in Tcl loadState in line",
362 Tcl_GetVar (p->interp, "errorInfo", 0));
366 static int do_save (char *parms, void *mydata)
368 struct tcl_info *p = mydata;
369 struct Tcl_CmdInfo cinfo;
371 if (Tcl_GetCommandInfo(p->interp, "saveState", &cinfo))
375 gw_log (GW_LOG_DEBUG, mod, "saveState");
376 r = Tcl_Eval (p->interp, "saveState\n");
378 report_error (p, p->interp->errorLine,
379 "Error in Tcl saveState in line",
380 Tcl_GetVar (p->interp, "errorInfo", 0));