2 * IR toolkit for tcl/tk
4 * See the file LICENSE for details.
5 * Sebastian Hammer, Adam Dickmeiss
8 * Revision 1.18 1996-02-23 17:31:42 adam
9 * More functions made available to the wais tcl extension.
11 * Revision 1.17 1996/02/21 10:16:21 adam
12 * Simplified select handling. Only one function ir_tcl_select_set has
13 * to be externally defined.
15 * Revision 1.16 1996/02/05 17:58:05 adam
16 * Ported ir-tcl to use the beta releases of tcl7.5/tk4.1.
18 * Revision 1.15 1996/01/10 09:18:45 adam
19 * PDU specific callbacks implemented: initRespnse, searchResponse,
20 * presentResponse and scanResponse.
21 * Bug fix in the command line shell (tclmain.c) - discovered on OSF/1.
23 * Revision 1.14 1995/09/21 13:11:53 adam
24 * Support of dynamic loading.
25 * Test script uses load command if necessary.
27 * Revision 1.13 1995/08/28 12:21:22 adam
28 * Removed lines and list as synonyms of list in MARC extractron.
29 * Configure searches also for tk4.0 / tcl7.4.
31 * Revision 1.12 1995/08/28 11:07:16 adam
34 * Revision 1.11 1995/08/03 13:23:02 adam
37 * Revision 1.10 1995/06/30 12:39:28 adam
38 * Bug fix: loadFile didn't set record type.
39 * The MARC routines are a little less strict in the interpretation.
40 * Script display.tcl replaces the old marc.tcl.
41 * New interactive script: shell.tcl.
43 * Revision 1.9 1995/06/26 10:20:20 adam
44 * ir-tk works like wish.
46 * Revision 1.8 1995/06/21 15:16:44 adam
47 * More work on configuration.
49 * Revision 1.7 1995/06/21 11:04:54 adam
50 * Uses GNU autoconf 2.3.
51 * Install procedure implemented.
52 * boook bitmaps moved to sub directory bitmaps.
54 * Revision 1.6 1995/05/29 08:44:28 adam
55 * Work on delete of objects.
57 * Revision 1.5 1995/03/20 08:53:30 adam
58 * Event loop in tclmain.c rewritten. New method searchStatus.
60 * Revision 1.4 1995/03/17 07:50:31 adam
61 * Headers have changed a little.
67 #include <sys/types.h>
69 #include <sys/select.h>
77 static char *fileName = NULL;
79 /* select(2) callbacks */
81 void (*handle)(ClientData, int, int, int);
85 #define MAX_CALLBACK 200
87 static struct callback callback_table[MAX_CALLBACK];
88 static int max_fd = 3; /* don't worry: it will grow... */
90 void tcl_mainloop (Tcl_Interp *interp, int interactive);
92 int Tcl_AppInit (Tcl_Interp *interp)
94 if (Tcl_Init(interp) == TCL_ERROR)
96 if (Irtcl_Init(interp) == TCL_ERROR)
99 if (Waistcl_Init(interp) == TCL_ERROR)
105 int main (int argc, char **argv)
111 interp = Tcl_CreateInterp();
112 Tcl_SetVar (interp, "tcl_interactive", "0", TCL_GLOBAL_ONLY);
116 if (Tcl_AppInit(interp) != TCL_OK) {
117 fprintf(stderr, "Tcl_AppInit failed: %s\n", interp->result);
119 for (i=0; i<MAX_CALLBACK; i++)
120 callback_table[i].handle = NULL;
123 code = Tcl_EvalFile (interp, fileName);
124 if (*interp->result != 0)
125 printf ("%s\n", interp->result);
128 tcl_mainloop (interp, 0);
133 Tcl_SetVar (interp, "tcl_interactive", "1", TCL_GLOBAL_ONLY);
134 tcl_mainloop (interp, 1);
139 char input_buf[1024];
143 Tcl_DStringInit (&command);
144 while (fgets (input_buf, 1024, stdin))
146 count = strlen(input_buf);
147 Tcl_DStringAppend (&command, input_buf, count);
148 if (Tcl_CommandComplete (Tcl_DStringValue (&command)))
150 int code = Tcl_Eval (interp, Tcl_DStringValue (&command));
151 Tcl_DStringFree (&command);
153 printf ("Error: %s\n", interp->result);
156 tcl_mainloop (interp, 0);
161 void tcl_mainloop (Tcl_Interp *interp, int interactive)
166 static fd_set fdset_tcl_r;
167 static fd_set fdset_tcl_w;
168 static fd_set fdset_tcl_x;
171 min_fd = interactive ? 3 : 0;
174 Tcl_DStringInit (&command);
175 printf ("%% "); fflush (stdout);
179 FD_ZERO (&fdset_tcl_r);
180 FD_ZERO (&fdset_tcl_w);
181 FD_ZERO (&fdset_tcl_x);
183 FD_SET (0, &fdset_tcl_r);
184 for (res=0, i=min_fd; i<=max_fd; i++)
186 if (callback_table[i].handle && callback_table[i].w)
188 FD_SET (i, &fdset_tcl_w);
191 if (callback_table[i].handle && callback_table[i].r)
193 FD_SET (i, &fdset_tcl_r);
196 if (callback_table[i].handle && callback_table[i].e)
198 FD_SET (i, &fdset_tcl_x);
202 if (!interactive && !res)
204 if ((res = select(max_fd+1, &fdset_tcl_r, &fdset_tcl_w,
205 &fdset_tcl_x, 0)) < 0)
212 for (i=min_fd; i<=max_fd; i++)
218 if (!callback_table[i].handle)
220 if (FD_ISSET (i, &fdset_tcl_r) && callback_table[i].r)
222 if (FD_ISSET (i, &fdset_tcl_w) && callback_table[i].w)
224 if (FD_ISSET (i, &fdset_tcl_x) && callback_table[i].e)
226 if (r_flag || w_flag || e_flag)
227 (*callback_table[i].handle)(callback_table[i].obj,
228 r_flag, w_flag, e_flag);
230 if (interactive && FD_ISSET(0, &fdset_tcl_r))
232 char input_buf[1024];
233 int count = read (0, input_buf, 1024);
237 Tcl_DStringAppend (&command, input_buf, count);
238 if (Tcl_CommandComplete (Tcl_DStringValue (&command)))
240 int code = Tcl_Eval (interp, Tcl_DStringValue (&command));
241 Tcl_DStringFree (&command);
243 printf ("Error: %s\n", interp->result);
244 else if (*interp->result)
245 printf ("%s\n", interp->result);
246 printf ("%% "); fflush (stdout);
252 void ir_tcl_select_set (void (*f)(ClientData clientData, int r, int w, int e),
253 int fd, ClientData clientData, int r, int w, int e)
255 callback_table[fd].handle = f;
256 callback_table[fd].obj = clientData;
257 callback_table[fd].r = r;
258 callback_table[fd].w = w;
259 callback_table[fd].e = e;