New Tcl calls egw_enc/egw_dec to encode/decode binary URL info.
[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.12  1996/03/07 12:45:34  adam
45  * New Tcl calls egw_enc/egw_dec to encode/decode binary URL info.
46  *
47  * Revision 1.11  1996/01/24  08:26:56  adam
48  * All tcl commands prefixed with egw_ (except the html command).
49  *
50  * Revision 1.10  1995/11/08  16:14:35  adam
51  * Many improvements and bug fixes.
52  * First version that ran on dtbsun.
53  *
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.
58  *
59  * Revision 1.8  1995/11/06  17:44:23  adam
60  * State reestablised when shell restarts. History of previous
61  * result sets.
62  *
63  * Revision 1.7  1995/10/31  16:56:25  adam
64  * Record presentation.
65  *
66  * Revision 1.6  1995/10/31  10:03:54  adam
67  * Work on queries.
68  * New command implemented - aborts script.
69  *
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.
73  *
74  * Revision 1.4  1995/10/27  17:30:16  adam
75  * First search request/response that works.
76  *
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.
81  *
82  * Revision 1.2  1995/10/23  16:55:43  adam
83  * A lot of changes - really.
84  *
85  * Revision 1.1  1995/10/20  14:02:42  adam
86  * First version of WWW gateway with embedded Tcl.
87  *
88  */
89
90 #include <stdio.h>
91 #include <stdlib.h>
92 #include <string.h>
93 #include <assert.h>
94 #include <ctype.h>
95
96 #include "wtcl.h"
97
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);
102
103 static struct w_interp_type w_interp_t = {
104     "tcl",
105     do_create,
106     do_exec,
107     do_load,
108     do_save
109 };
110
111 W_Interp_Type w_interp_tcl = &w_interp_t;
112
113
114 static char *mod = "wtcl";
115
116 struct tcl_info {
117     Tcl_Interp *interp;
118     char  *fbuf;
119     int    fbuf_size;
120     int    fbuf_ptr;
121     int    wabort;
122     WCLIENT wcl;
123 };
124
125 Tcl_Interp *w_interp_tcl_get (W_Interp w_interp)
126 {
127     struct tcl_info *p;
128
129     if (strcmp (w_interp->ctrl->name, "tcl"))
130     {
131         gw_log (GW_LOG_FATAL, mod, "Internal failure");
132         assert (0);
133     }
134     p = (struct tcl_info*) w_interp->mydata;
135     return p->interp;
136 }
137
138 static int proc_wabort_invoke (ClientData clientData, Tcl_Interp *interp,
139                                int argc, char **argv)
140 {
141     struct tcl_info *p = (struct tcl_info*) clientData;
142
143     p->wabort = 1;
144     if (argc > 1)
145         Tcl_AppendResult (interp, argv[1], NULL);
146     return TCL_RETURN;
147 }
148
149 static int proc_wflush_invoke (ClientData clientData, Tcl_Interp *interp,
150                                int argc, char **argv)
151 {
152     struct tcl_info *p = (struct tcl_info*) clientData;
153
154     wo_flush (p->wcl);
155     return TCL_OK;
156 }
157
158 static int proc_html_invoke (ClientData clientData, Tcl_Interp *interp,
159                              int argc, char **argv)
160 {
161     struct tcl_info *p = (struct tcl_info*) clientData;
162     int i;
163
164     for (i = 1; i<argc; i++)
165         wo_write (p->wcl, argv[i], strlen(argv[i]));
166     return TCL_OK;
167 }
168
169 static int proc_wform_invoke (ClientData clientData, Tcl_Interp *interp,
170                               int argc, char **argv)
171 {
172     struct tcl_info *p = (struct tcl_info*) clientData;
173     int i;
174     if (argc == 2)
175     {
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);
180         return TCL_OK;
181     }    
182     for (i = 0; *p->wcl->wf_data[i].name; i++)
183     { 
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);
188     }
189     return TCL_OK;
190 }
191
192 static int proc_wlog_invoke (ClientData clientData, Tcl_Interp *interp,
193                              int argc, char **argv)
194 {
195     unsigned mask;
196
197     if (argc < 3)
198         return TCL_OK;
199     if (!strcmp (argv[1], "debug"))
200         mask = GW_LOG_DEBUG;
201     else if (!strcmp (argv[1], "fatal"))
202         mask = GW_LOG_FATAL;
203     else if (!strcmp (argv[1], "warn"))
204         mask = GW_LOG_WARN;
205     else if (!strcmp (argv[1], "acct"))
206         mask = GW_LOG_ACCT;
207     else
208         mask = GW_LOG_DEBUG;
209     switch (argc)
210     {
211     case 3:
212         gw_log (mask, mod, "%s", argv[2]);
213         break;
214     case 4:
215         gw_log (mask, mod, "%s %s", argv[2], argv[3]);
216         break;
217     case 5:
218         gw_log (mask, mod, "%s %s %s", argv[2], argv[3], argv[4]);
219         break;
220     case 6:
221         gw_log (mask, mod, "%s %s %s %s", argv[2], argv[3], argv[4], argv[5]);
222         break;
223     }
224     return TCL_OK;
225 }
226
227 static int proc_enc (ClientData clientData, Tcl_Interp *interp,
228                      int argc, char **argv)
229 {
230     int i;
231     char buf1[4];
232     char buf2[2];
233     
234     buf1[0] = '%';
235     buf2[1] = '\0';
236     for (i = 1; i<argc; i++)
237     {
238         const char *cp = argv[i];
239         while (*cp)
240         {
241             if (*cp <= ' ' || *cp >= 127 || *cp == '/' || *cp == ' ' ||
242                 *cp == '&' || *cp == ':' || *cp == '%')
243             {
244                 sprintf (buf1+1, "%02X", *cp);
245                 Tcl_AppendResult (interp, buf1, NULL);
246             }
247             else
248             {
249                 buf2[0] = *cp;
250                 Tcl_AppendResult (interp, buf2, NULL);
251             }
252             cp++;
253         }
254     }
255     return TCL_OK;
256 }
257
258 static int proc_dec (ClientData clientData, Tcl_Interp *interp,
259                      int argc, char **argv)
260 {
261     int i;
262     unsigned val;
263     char buf[2];
264     
265     buf[1] = '\0';
266     for (i = 1; i<argc; i++)
267     {
268         const char *cp = argv[i];
269         while (*cp)
270         {
271             if (*cp == '%' && cp[1] && cp[2])
272             {
273                 if (cp[1] >= 'A')
274                     val = cp[1] - 'A'+10;
275                 else
276                     val = cp[1] - '0';
277
278                 
279                 if (cp[2] >= 'A')
280                     val = val*16 + (cp[2] - 'A'+10);
281                 else
282                     val = val*16 + (cp[2] - '0');
283                 buf[0] = val;
284                 cp += 3;
285             }
286             else
287                 buf[0] = *cp++;
288             Tcl_AppendResult (interp, buf, NULL);
289         }
290     }
291     return TCL_OK;
292 }
293
294
295 int Tcl_AppInit (Tcl_Interp *interp)
296 {
297     if (Tcl_Init (interp) == TCL_ERROR)
298         return TCL_ERROR;
299     return TCL_OK;
300 }
301
302 static void *do_create (WCLIENT wcl, void *args)
303 {
304     struct tcl_info *p;
305     char tmp_str[256];
306
307     if (!(p = malloc (sizeof(*p))))
308     {
309         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "malloc: tcl_info");
310         exit (1);
311     }
312     if (!(p->interp = Tcl_CreateInterp ()))
313     {
314         gw_log (GW_LOG_FATAL, mod, "Cannot make Tcl_Interp");
315         exit (1);
316     }
317     p->wcl = wcl;
318     p->fbuf_size = 1024;
319     if (!(p->fbuf = malloc (p->fbuf_size)))
320     {
321         gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "malloc: tcl_info fbuf");
322         exit (1);
323     }
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);
334     return p;
335 }
336
337 static void report_error (struct tcl_info *p, int errorLine,
338                           const char *pre, const char *msg)
339 {
340     if (!msg)
341         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);
346 }
347
348 static int tcl_exec (const char *fname, char *parms,
349                      struct tcl_info *p, FILE *inf, int *lineno)
350 {
351     int c, escape = 0, level = 0;
352     int r, fbuf_ptr = 0;
353     int local_line = 0;
354
355     while (1)
356     {
357         if (fbuf_ptr == p->fbuf_size-1)
358         {
359             char *newb;
360
361             if (!(newb = malloc (p->fbuf_size += 16384)))
362             {
363                 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, mod, "malloc: fbuf");
364                 exit (1);
365             }
366             memcpy (newb, p->fbuf, fbuf_ptr);
367             free (p->fbuf);
368             p->fbuf = newb;
369         }
370         c = getc (inf);
371         if (c == EOF)
372         {
373             report_error (p, *lineno, "Error in Tcl script starting at line",
374                                       "Unexpected EOF (missing right brace)");
375             return TCL_ERROR;
376         }
377         if (c == '\\')
378             escape = 1;
379         else if (c == '{' && !escape)
380         {
381             level++;
382             escape = 0;
383         }
384         else if (c == '}' && !escape)
385         {
386             if (--level < 0)
387                 break;
388             escape = 0;
389         }
390         else
391         {
392             if (c == '\n')
393                 local_line++;
394             escape = 0;
395         }
396         p->fbuf[fbuf_ptr++] = c;
397     }
398     p->fbuf[fbuf_ptr] = '\0';
399     p->wabort = 0;
400     r = Tcl_Eval (p->interp, p->fbuf);
401     if (r == TCL_ERROR)
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;
406     if (p->wabort)
407         return TCL_RETURN;
408     return r;
409 }
410
411 static int do_exec (const char *fname, char *parms, void *mydata)
412 {
413     struct tcl_info *p = mydata;
414     int c, escape = 0;
415     int lineno = 1;
416     FILE *inf = fopen (fname, "r");
417
418     gw_log (GW_LOG_DEBUG, mod, "Executing %s", fname);
419     if (!inf)
420     {
421         gw_log (GW_LOG_WARN|GW_LOG_ERRNO, mod, "open %s", fname);
422         return -1;
423     }
424     Tcl_SetVar (p->interp, "sessionParms", parms, TCL_GLOBAL_ONLY);
425     while ((c = getc(inf)) != EOF)
426     {
427         if (c == '\\')
428             escape = 1;
429         else if (c == '{')
430         {
431             if (escape)
432                 wo_putc (p->wcl, c);
433             else
434             {
435                 int r = tcl_exec (fname, parms, p, inf, &lineno);
436                 if (r == TCL_RETURN)
437                 {
438                     fclose (inf);
439                     return 0;
440                 }
441                 else if (r == TCL_ERROR)
442                 {
443                     fclose (inf);
444                     return -2;
445                 }
446             }
447             escape = 0;
448         }
449         else
450         {
451             if (c == '\n')
452                 lineno++;
453             escape = 0;
454             wo_putc (p->wcl, c);
455         }
456     }
457     fclose (inf);
458     return 0;
459 }
460
461
462 static int do_load (char *parms, void *mydata)
463 {
464     struct tcl_info *p = mydata;
465     char fname[80];
466     int r;
467
468     sprintf (fname, "tcl.state.%d", p->wcl->id);
469     r = Tcl_EvalFile (p->interp, fname);
470     if (r == TCL_ERROR)
471         gw_log (GW_LOG_WARN, mod, "Cannot load Tcl state" );
472     return 0;
473 }
474
475 static int do_save (char *parms, void *mydata)
476 {
477     struct tcl_info *p = mydata;
478     struct Tcl_CmdInfo cinfo;
479
480     if (Tcl_GetCommandInfo(p->interp, "saveState", &cinfo))
481     {
482         int r;
483
484         gw_log (GW_LOG_DEBUG, mod, "saveState");
485         r = Tcl_Eval (p->interp, "saveState\n");
486         if (r == TCL_ERROR)
487             report_error (p, p->interp->errorLine, 
488                           "Error in Tcl saveState in line", 
489                           Tcl_GetVar (p->interp, "errorInfo", 0));
490     }
491     return 0;
492 }
493