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.12 1996/03/07 12:45:34 adam
45 * New Tcl calls egw_enc/egw_dec to encode/decode binary URL info.
47 * Revision 1.11 1996/01/24 08:26:56 adam
48 * All tcl commands prefixed with egw_ (except the html command).
50 * Revision 1.10 1995/11/08 16:14:35 adam
51 * Many improvements and bug fixes.
52 * First version that ran on dtbsun.
54 * Revision 1.9 1995/11/07 14:57:00 adam
55 * Work on search in multiple targets.
56 * New wtcl command: wlog.
57 * Optional timeout parameter to zwait.
59 * Revision 1.8 1995/11/06 17:44:23 adam
60 * State reestablised when shell restarts. History of previous
63 * Revision 1.7 1995/10/31 16:56:25 adam
64 * Record presentation.
66 * Revision 1.6 1995/10/31 10:03:54 adam
68 * New command implemented - aborts script.
70 * Revision 1.5 1995/10/30 17:35:18 adam
71 * New function zwait that waits for a variable change - due to i/o events
72 * that invoke callback routines.
74 * Revision 1.4 1995/10/27 17:30:16 adam
75 * First search request/response that works.
77 * Revision 1.3 1995/10/27 15:12:14 adam
78 * IrTcl incorporated in the gateway.
79 * Better separation of script types.
80 * Z39.50 gateway scripts entered.
82 * Revision 1.2 1995/10/23 16:55:43 adam
83 * A lot of changes - really.
85 * Revision 1.1 1995/10/20 14:02:42 adam
86 * First version of WWW gateway with embedded Tcl.
98 static void *do_create (WCLIENT wcl, void *args);
99 static int do_exec (const char *fname, char *parms, void *mydata);
100 static int do_load (char *parms, void *mydata);
101 static int do_save (char *parms, void *mydata);
103 static struct w_interp_type w_interp_t = {
111 W_Interp_Type w_interp_tcl = &w_interp_t;
114 static char *mod = "wtcl";
125 Tcl_Interp *w_interp_tcl_get (W_Interp w_interp)
129 if (strcmp (w_interp->ctrl->name, "tcl"))
131 gw_log (GW_LOG_FATAL, mod, "Internal failure");
134 p = (struct tcl_info*) w_interp->mydata;
138 static int proc_wabort_invoke (ClientData clientData, Tcl_Interp *interp,
139 int argc, char **argv)
141 struct tcl_info *p = (struct tcl_info*) clientData;
145 Tcl_AppendResult (interp, argv[1], NULL);
149 static int proc_wflush_invoke (ClientData clientData, Tcl_Interp *interp,
150 int argc, char **argv)
152 struct tcl_info *p = (struct tcl_info*) clientData;
158 static int proc_html_invoke (ClientData clientData, Tcl_Interp *interp,
159 int argc, char **argv)
161 struct tcl_info *p = (struct tcl_info*) clientData;
164 for (i = 1; i<argc; i++)
165 wo_write (p->wcl, argv[i], strlen(argv[i]));
169 static int proc_wform_invoke (ClientData clientData, Tcl_Interp *interp,
170 int argc, char **argv)
172 struct tcl_info *p = (struct tcl_info*) clientData;
176 for (i = 0; *p->wcl->wf_data[i].name; i++)
177 if (!strcmp (argv[1], p->wcl->wf_data[i].name) &&
178 *p->wcl->wf_data[i].value)
179 Tcl_AppendElement (p->interp, p->wcl->wf_data[i].value);
182 for (i = 0; *p->wcl->wf_data[i].name; i++)
184 Tcl_AppendResult (p->interp, "{ ", NULL);
185 Tcl_AppendElement (p->interp, p->wcl->wf_data[i].name);
186 Tcl_AppendElement (p->interp, p->wcl->wf_data[i].value);
187 Tcl_AppendResult (p->interp, " }\n", NULL);
192 static int proc_wlog_invoke (ClientData clientData, Tcl_Interp *interp,
193 int argc, char **argv)
199 if (!strcmp (argv[1], "debug"))
201 else if (!strcmp (argv[1], "fatal"))
203 else if (!strcmp (argv[1], "warn"))
205 else if (!strcmp (argv[1], "acct"))
212 gw_log (mask, mod, "%s", argv[2]);
215 gw_log (mask, mod, "%s %s", argv[2], argv[3]);
218 gw_log (mask, mod, "%s %s %s", argv[2], argv[3], argv[4]);
221 gw_log (mask, mod, "%s %s %s %s", argv[2], argv[3], argv[4], argv[5]);
227 static int proc_enc (ClientData clientData, Tcl_Interp *interp,
228 int argc, char **argv)
236 for (i = 1; i<argc; i++)
238 const char *cp = argv[i];
241 if (*cp <= ' ' || *cp >= 127 || *cp == '/' || *cp == ' ' ||
242 *cp == '&' || *cp == ':' || *cp == '%')
244 sprintf (buf1+1, "%02X", *cp);
245 Tcl_AppendResult (interp, buf1, NULL);
250 Tcl_AppendResult (interp, buf2, NULL);
258 static int proc_dec (ClientData clientData, Tcl_Interp *interp,
259 int argc, char **argv)
266 for (i = 1; i<argc; i++)
268 const char *cp = argv[i];
271 if (*cp == '%' && cp[1] && cp[2])
274 val = cp[1] - 'A'+10;
280 val = val*16 + (cp[2] - 'A'+10);
282 val = val*16 + (cp[2] - '0');
288 Tcl_AppendResult (interp, buf, NULL);
295 int Tcl_AppInit (Tcl_Interp *interp)
297 if (Tcl_Init (interp) == TCL_ERROR)
302 static void *do_create (WCLIENT wcl, void *args)
307 if (!(p = malloc (sizeof(*p))))
309 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "malloc: tcl_info");
312 if (!(p->interp = Tcl_CreateInterp ()))
314 gw_log (GW_LOG_FATAL, mod, "Cannot make Tcl_Interp");
319 if (!(p->fbuf = malloc (p->fbuf_size)))
321 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "malloc: tcl_info fbuf");
324 Tcl_AppInit (p->interp);
325 Tcl_CreateCommand (p->interp, "html", proc_html_invoke, p, NULL);
326 Tcl_CreateCommand (p->interp, "egw_form", proc_wform_invoke, p, NULL);
327 Tcl_CreateCommand (p->interp, "egw_abort", proc_wabort_invoke, p, NULL);
328 Tcl_CreateCommand (p->interp, "egw_flush", proc_wflush_invoke, p, NULL);
329 Tcl_CreateCommand (p->interp, "egw_log", proc_wlog_invoke, p, NULL);
330 Tcl_CreateCommand (p->interp, "egw_enc", proc_enc, p, NULL);
331 Tcl_CreateCommand (p->interp, "egw_dec", proc_dec, p, NULL);
332 sprintf (tmp_str, "%d", wcl->id);
333 Tcl_SetVar (p->interp, "sessionId", tmp_str, TCL_GLOBAL_ONLY);
337 static void report_error (struct tcl_info *p, int errorLine,
338 const char *pre, const char *msg)
342 gw_log (GW_LOG_WARN, mod, "%s %d %s", pre, errorLine, msg);
343 wo_printf (p->wcl, "\n<br><hr>\n<strong>"
344 "%s %d</strong><br>\n", pre, errorLine);
345 wo_printf (p->wcl, "<xmp>\n%s</xmp>\n<hr>\n", msg);
348 static int tcl_exec (const char *fname, char *parms,
349 struct tcl_info *p, FILE *inf, int *lineno)
351 int c, escape = 0, level = 0;
357 if (fbuf_ptr == p->fbuf_size-1)
361 if (!(newb = malloc (p->fbuf_size += 16384)))
363 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "malloc: fbuf");
366 memcpy (newb, p->fbuf, fbuf_ptr);
373 report_error (p, *lineno, "Error in Tcl script starting at line",
374 "Unexpected EOF (missing right brace)");
379 else if (c == '{' && !escape)
384 else if (c == '}' && !escape)
396 p->fbuf[fbuf_ptr++] = c;
398 p->fbuf[fbuf_ptr] = '\0';
400 r = Tcl_Eval (p->interp, p->fbuf);
402 report_error (p, p->interp->errorLine + *lineno - 1,
403 "Error in Tcl script in line",
404 Tcl_GetVar (p->interp, "errorInfo", 0));
405 (*lineno) += local_line;
411 static int do_exec (const char *fname, char *parms, void *mydata)
413 struct tcl_info *p = mydata;
416 FILE *inf = fopen (fname, "r");
418 gw_log (GW_LOG_DEBUG, mod, "Executing %s", fname);
421 gw_log (GW_LOG_WARN|GW_LOG_ERRNO, mod, "open %s", fname);
424 Tcl_SetVar (p->interp, "sessionParms", parms, TCL_GLOBAL_ONLY);
425 while ((c = getc(inf)) != EOF)
435 int r = tcl_exec (fname, parms, p, inf, &lineno);
441 else if (r == TCL_ERROR)
462 static int do_load (char *parms, void *mydata)
464 struct tcl_info *p = mydata;
468 sprintf (fname, "tcl.state.%d", p->wcl->id);
469 r = Tcl_EvalFile (p->interp, fname);
471 gw_log (GW_LOG_WARN, mod, "Cannot load Tcl state" );
475 static int do_save (char *parms, void *mydata)
477 struct tcl_info *p = mydata;
478 struct Tcl_CmdInfo cinfo;
480 if (Tcl_GetCommandInfo(p->interp, "saveState", &cinfo))
484 gw_log (GW_LOG_DEBUG, mod, "saveState");
485 r = Tcl_Eval (p->interp, "saveState\n");
487 report_error (p, p->interp->errorLine,
488 "Error in Tcl saveState in line",
489 Tcl_GetVar (p->interp, "errorInfo", 0));