2 * IR toolkit for tcl/tk
4 * See the file LICENSE for details.
5 * Sebastian Hammer, Adam Dickmeiss
8 * Revision 1.10 1995-06-30 12:39:28 adam
9 * Bug fix: loadFile didn't set record type.
10 * The MARC routines are a little less strict in the interpretation.
11 * Script display.tcl replaces the old marc.tcl.
12 * New interactive script: shell.tcl.
14 * Revision 1.9 1995/06/26 10:20:20 adam
15 * ir-tk works like wish.
17 * Revision 1.8 1995/06/21 15:16:44 adam
18 * More work on configuration.
20 * Revision 1.7 1995/06/21 11:04:54 adam
21 * Uses GNU autoconf 2.3.
22 * Install procedure implemented.
23 * boook bitmaps moved to sub directory bitmaps.
25 * Revision 1.6 1995/05/29 08:44:28 adam
26 * Work on delete of objects.
28 * Revision 1.5 1995/03/20 08:53:30 adam
29 * Event loop in tclmain.c rewritten. New method searchStatus.
31 * Revision 1.4 1995/03/17 07:50:31 adam
32 * Headers have changed a little.
38 #include <sys/types.h>
40 #include <sys/select.h>
48 static char *fileName = NULL;
50 /* select(2) callbacks */
52 void (*r_handle)(void *p);
53 void (*w_handle)(void *p);
54 void (*x_handle)(void *p);
57 #define MAX_CALLBACK 200
59 static struct callback callback_table[MAX_CALLBACK];
60 static int max_fd = 3; /* don't worry: it will grow... */
62 void tcl_mainloop (Tcl_Interp *interp, int interactive);
64 int Tcl_AppInit (Tcl_Interp *interp)
66 if (Tcl_Init(interp) == TCL_ERROR)
68 if (ir_tcl_init(interp) == TCL_ERROR)
73 int main (int argc, char **argv)
79 interp = Tcl_CreateInterp();
80 Tcl_SetVar (interp, "tcl_interactive", "0", TCL_GLOBAL_ONLY);
84 if (Tcl_AppInit(interp) != TCL_OK) {
85 fprintf(stderr, "Tcl_AppInit failed: %s\n", interp->result);
87 for (i=0; i<MAX_CALLBACK; i++)
89 callback_table[i].r_handle = NULL;
90 callback_table[i].w_handle = NULL;
91 callback_table[i].x_handle = NULL;
95 code = Tcl_EvalFile (interp, fileName);
96 if (*interp->result != 0)
97 printf ("%s\n", interp->result);
100 tcl_mainloop (interp, 0);
105 Tcl_SetVar (interp, "tcl_interactive", "1", TCL_GLOBAL_ONLY);
106 tcl_mainloop (interp, 1);
111 char input_buf[1024];
115 Tcl_DStringInit (&command);
116 while (fgets (input_buf, 1024, stdin))
118 count = strlen(input_buf);
119 Tcl_DStringAppend (&command, input_buf, count);
120 if (Tcl_CommandComplete (Tcl_DStringValue (&command)))
122 int code = Tcl_Eval (interp, Tcl_DStringValue (&command));
123 Tcl_DStringFree (&command);
125 printf ("Error: %s\n", interp->result);
128 tcl_mainloop (interp, 0);
133 void tcl_mainloop (Tcl_Interp *interp, int interactive)
138 static fd_set fdset_tcl_r;
139 static fd_set fdset_tcl_w;
140 static fd_set fdset_tcl_x;
143 min_fd = interactive ? 3 : 0;
146 Tcl_DStringInit (&command);
147 printf ("%% "); fflush (stdout);
151 FD_ZERO (&fdset_tcl_r);
152 FD_ZERO (&fdset_tcl_w);
153 FD_ZERO (&fdset_tcl_x);
155 FD_SET (0, &fdset_tcl_r);
156 for (res=0, i=min_fd; i<=max_fd; i++)
158 if (callback_table[i].w_handle)
160 FD_SET (i, &fdset_tcl_w);
163 if (callback_table[i].r_handle)
165 FD_SET (i, &fdset_tcl_r);
168 if (callback_table[i].x_handle)
170 FD_SET (i, &fdset_tcl_x);
174 if (!interactive && !res)
176 if ((res = select(max_fd+1, &fdset_tcl_r, &fdset_tcl_w,
177 &fdset_tcl_x, 0)) < 0)
184 for (i=min_fd; i<=max_fd; i++)
186 if (FD_ISSET (i, &fdset_tcl_r))
188 assert (callback_table[i].r_handle);
189 (*callback_table[i].r_handle) (callback_table[i].obj);
191 if (FD_ISSET (i, &fdset_tcl_w))
193 assert (callback_table[i].w_handle);
194 (*callback_table[i].w_handle) (callback_table[i].obj);
196 if (FD_ISSET (i, &fdset_tcl_x))
198 assert (callback_table[i].x_handle);
199 (*callback_table[i].x_handle) (callback_table[i].obj);
202 if (interactive && FD_ISSET(0, &fdset_tcl_r))
204 char input_buf[1024];
205 int count = read (0, input_buf, 1024);
209 Tcl_DStringAppend (&command, input_buf, count);
210 if (Tcl_CommandComplete (Tcl_DStringValue (&command)))
212 int code = Tcl_Eval (interp, Tcl_DStringValue (&command));
213 Tcl_DStringFree (&command);
215 printf ("Error: %s\n", interp->result);
216 else if (*interp->result)
217 printf ("%s\n", interp->result);
218 printf ("%% "); fflush (stdout);
224 void ir_select_add (int fd, void *obj)
226 callback_table[fd].obj = obj;
227 callback_table[fd].r_handle = ir_select_read;
228 callback_table[fd].w_handle = NULL;
229 callback_table[fd].x_handle = NULL;
234 void ir_select_add_write (int fd, void *obj)
236 callback_table[fd].w_handle = ir_select_write;
241 void ir_select_remove_write (int fd, void *obj)
243 callback_table[fd].w_handle = NULL;
246 void ir_select_remove (int fd, void *obj)
248 callback_table[fd].r_handle = NULL;
249 callback_table[fd].w_handle = NULL;
250 callback_table[fd].x_handle = NULL;