2 * IR toolkit for tcl/tk
4 * See the file LICENSE for details.
5 * Sebastian Hammer, Adam Dickmeiss
8 * Revision 1.14 1995-09-21 13:11:53 adam
9 * Support of dynamic loading.
10 * Test script uses load command if necessary.
12 * Revision 1.13 1995/08/28 12:21:22 adam
13 * Removed lines and list as synonyms of list in MARC extractron.
14 * Configure searches also for tk4.0 / tcl7.4.
16 * Revision 1.12 1995/08/28 11:07:16 adam
19 * Revision 1.11 1995/08/03 13:23:02 adam
22 * Revision 1.10 1995/06/30 12:39:28 adam
23 * Bug fix: loadFile didn't set record type.
24 * The MARC routines are a little less strict in the interpretation.
25 * Script display.tcl replaces the old marc.tcl.
26 * New interactive script: shell.tcl.
28 * Revision 1.9 1995/06/26 10:20:20 adam
29 * ir-tk works like wish.
31 * Revision 1.8 1995/06/21 15:16:44 adam
32 * More work on configuration.
34 * Revision 1.7 1995/06/21 11:04:54 adam
35 * Uses GNU autoconf 2.3.
36 * Install procedure implemented.
37 * boook bitmaps moved to sub directory bitmaps.
39 * Revision 1.6 1995/05/29 08:44:28 adam
40 * Work on delete of objects.
42 * Revision 1.5 1995/03/20 08:53:30 adam
43 * Event loop in tclmain.c rewritten. New method searchStatus.
45 * Revision 1.4 1995/03/17 07:50:31 adam
46 * Headers have changed a little.
52 #include <sys/types.h>
54 #include <sys/select.h>
62 static char *fileName = NULL;
64 /* select(2) callbacks */
66 void (*r_handle)(ClientData);
67 void (*w_handle)(ClientData);
68 void (*x_handle)(ClientData);
71 #define MAX_CALLBACK 200
73 static struct callback callback_table[MAX_CALLBACK];
74 static int max_fd = 3; /* don't worry: it will grow... */
76 void tcl_mainloop (Tcl_Interp *interp, int interactive);
78 int Tcl_AppInit (Tcl_Interp *interp)
80 if (Tcl_Init(interp) == TCL_ERROR)
82 if (Irtcl_Init(interp) == TCL_ERROR)
87 int main (int argc, char **argv)
93 interp = Tcl_CreateInterp();
94 Tcl_SetVar (interp, "tcl_interactive", "0", TCL_GLOBAL_ONLY);
98 if (Tcl_AppInit(interp) != TCL_OK) {
99 fprintf(stderr, "Tcl_AppInit failed: %s\n", interp->result);
101 for (i=0; i<MAX_CALLBACK; i++)
103 callback_table[i].r_handle = NULL;
104 callback_table[i].w_handle = NULL;
105 callback_table[i].x_handle = NULL;
109 code = Tcl_EvalFile (interp, fileName);
110 if (*interp->result != 0)
111 printf ("%s\n", interp->result);
114 tcl_mainloop (interp, 0);
119 Tcl_SetVar (interp, "tcl_interactive", "1", TCL_GLOBAL_ONLY);
120 tcl_mainloop (interp, 1);
125 char input_buf[1024];
129 Tcl_DStringInit (&command);
130 while (fgets (input_buf, 1024, stdin))
132 count = strlen(input_buf);
133 Tcl_DStringAppend (&command, input_buf, count);
134 if (Tcl_CommandComplete (Tcl_DStringValue (&command)))
136 int code = Tcl_Eval (interp, Tcl_DStringValue (&command));
137 Tcl_DStringFree (&command);
139 printf ("Error: %s\n", interp->result);
142 tcl_mainloop (interp, 0);
147 void tcl_mainloop (Tcl_Interp *interp, int interactive)
152 static fd_set fdset_tcl_r;
153 static fd_set fdset_tcl_w;
154 static fd_set fdset_tcl_x;
157 min_fd = interactive ? 3 : 0;
160 Tcl_DStringInit (&command);
161 printf ("%% "); fflush (stdout);
165 FD_ZERO (&fdset_tcl_r);
166 FD_ZERO (&fdset_tcl_w);
167 FD_ZERO (&fdset_tcl_x);
169 FD_SET (0, &fdset_tcl_r);
170 for (res=0, i=min_fd; i<=max_fd; i++)
172 if (callback_table[i].w_handle)
174 FD_SET (i, &fdset_tcl_w);
177 if (callback_table[i].r_handle)
179 FD_SET (i, &fdset_tcl_r);
182 if (callback_table[i].x_handle)
184 FD_SET (i, &fdset_tcl_x);
188 if (!interactive && !res)
190 if ((res = select(max_fd+1, &fdset_tcl_r, &fdset_tcl_w,
191 &fdset_tcl_x, 0)) < 0)
198 for (i=min_fd; i<=max_fd; i++)
200 if (FD_ISSET (i, &fdset_tcl_r))
202 assert (callback_table[i].r_handle);
203 (*callback_table[i].r_handle) (callback_table[i].obj);
205 if (FD_ISSET (i, &fdset_tcl_w))
207 assert (callback_table[i].w_handle);
208 (*callback_table[i].w_handle) (callback_table[i].obj);
210 if (FD_ISSET (i, &fdset_tcl_x))
212 assert (callback_table[i].x_handle);
213 (*callback_table[i].x_handle) (callback_table[i].obj);
216 if (interactive && FD_ISSET(0, &fdset_tcl_r))
218 char input_buf[1024];
219 int count = read (0, input_buf, 1024);
223 Tcl_DStringAppend (&command, input_buf, count);
224 if (Tcl_CommandComplete (Tcl_DStringValue (&command)))
226 int code = Tcl_Eval (interp, Tcl_DStringValue (&command));
227 Tcl_DStringFree (&command);
229 printf ("Error: %s\n", interp->result);
230 else if (*interp->result)
231 printf ("%s\n", interp->result);
232 printf ("%% "); fflush (stdout);
238 void ir_select_add (int fd, void *obj)
240 callback_table[fd].obj = obj;
241 callback_table[fd].r_handle = ir_select_read;
242 callback_table[fd].w_handle = NULL;
243 callback_table[fd].x_handle = NULL;
248 void ir_select_add_write (int fd, void *obj)
250 callback_table[fd].w_handle = ir_select_write;
255 void ir_select_remove_write (int fd, void *obj)
257 callback_table[fd].w_handle = NULL;
260 void ir_select_remove (int fd, void *obj)
262 callback_table[fd].r_handle = NULL;
263 callback_table[fd].w_handle = NULL;
264 callback_table[fd].x_handle = NULL;