2 * IR toolkit for tcl/tk
4 * See the file LICENSE for details.
5 * Sebastian Hammer, Adam Dickmeiss
8 * Revision 1.13 1995-08-28 12:21:22 adam
9 * Removed lines and list as synonyms of list in MARC extractron.
10 * Configure searches also for tk4.0 / tcl7.4.
12 * Revision 1.12 1995/08/28 11:07:16 adam
15 * Revision 1.11 1995/08/03 13:23:02 adam
18 * Revision 1.10 1995/06/30 12:39:28 adam
19 * Bug fix: loadFile didn't set record type.
20 * The MARC routines are a little less strict in the interpretation.
21 * Script display.tcl replaces the old marc.tcl.
22 * New interactive script: shell.tcl.
24 * Revision 1.9 1995/06/26 10:20:20 adam
25 * ir-tk works like wish.
27 * Revision 1.8 1995/06/21 15:16:44 adam
28 * More work on configuration.
30 * Revision 1.7 1995/06/21 11:04:54 adam
31 * Uses GNU autoconf 2.3.
32 * Install procedure implemented.
33 * boook bitmaps moved to sub directory bitmaps.
35 * Revision 1.6 1995/05/29 08:44:28 adam
36 * Work on delete of objects.
38 * Revision 1.5 1995/03/20 08:53:30 adam
39 * Event loop in tclmain.c rewritten. New method searchStatus.
41 * Revision 1.4 1995/03/17 07:50:31 adam
42 * Headers have changed a little.
48 #include <sys/types.h>
50 #include <sys/select.h>
58 static char *fileName = NULL;
60 /* select(2) callbacks */
62 void (*r_handle)(ClientData);
63 void (*w_handle)(ClientData);
64 void (*x_handle)(ClientData);
67 #define MAX_CALLBACK 200
69 static struct callback callback_table[MAX_CALLBACK];
70 static int max_fd = 3; /* don't worry: it will grow... */
72 void tcl_mainloop (Tcl_Interp *interp, int interactive);
74 int Tcl_AppInit (Tcl_Interp *interp)
76 if (Tcl_Init(interp) == TCL_ERROR)
78 if (ir_tcl_init(interp) == TCL_ERROR)
83 int main (int argc, char **argv)
89 interp = Tcl_CreateInterp();
90 Tcl_SetVar (interp, "tcl_interactive", "0", TCL_GLOBAL_ONLY);
94 if (Tcl_AppInit(interp) != TCL_OK) {
95 fprintf(stderr, "Tcl_AppInit failed: %s\n", interp->result);
97 for (i=0; i<MAX_CALLBACK; i++)
99 callback_table[i].r_handle = NULL;
100 callback_table[i].w_handle = NULL;
101 callback_table[i].x_handle = NULL;
105 code = Tcl_EvalFile (interp, fileName);
106 if (*interp->result != 0)
107 printf ("%s\n", interp->result);
110 tcl_mainloop (interp, 0);
115 Tcl_SetVar (interp, "tcl_interactive", "1", TCL_GLOBAL_ONLY);
116 tcl_mainloop (interp, 1);
121 char input_buf[1024];
125 Tcl_DStringInit (&command);
126 while (fgets (input_buf, 1024, stdin))
128 count = strlen(input_buf);
129 Tcl_DStringAppend (&command, input_buf, count);
130 if (Tcl_CommandComplete (Tcl_DStringValue (&command)))
132 int code = Tcl_Eval (interp, Tcl_DStringValue (&command));
133 Tcl_DStringFree (&command);
135 printf ("Error: %s\n", interp->result);
138 tcl_mainloop (interp, 0);
143 void tcl_mainloop (Tcl_Interp *interp, int interactive)
148 static fd_set fdset_tcl_r;
149 static fd_set fdset_tcl_w;
150 static fd_set fdset_tcl_x;
153 min_fd = interactive ? 3 : 0;
156 Tcl_DStringInit (&command);
157 printf ("%% "); fflush (stdout);
161 FD_ZERO (&fdset_tcl_r);
162 FD_ZERO (&fdset_tcl_w);
163 FD_ZERO (&fdset_tcl_x);
165 FD_SET (0, &fdset_tcl_r);
166 for (res=0, i=min_fd; i<=max_fd; i++)
168 if (callback_table[i].w_handle)
170 FD_SET (i, &fdset_tcl_w);
173 if (callback_table[i].r_handle)
175 FD_SET (i, &fdset_tcl_r);
178 if (callback_table[i].x_handle)
180 FD_SET (i, &fdset_tcl_x);
184 if (!interactive && !res)
186 if ((res = select(max_fd+1, &fdset_tcl_r, &fdset_tcl_w,
187 &fdset_tcl_x, 0)) < 0)
194 for (i=min_fd; i<=max_fd; i++)
196 if (FD_ISSET (i, &fdset_tcl_r))
198 assert (callback_table[i].r_handle);
199 (*callback_table[i].r_handle) (callback_table[i].obj);
201 if (FD_ISSET (i, &fdset_tcl_w))
203 assert (callback_table[i].w_handle);
204 (*callback_table[i].w_handle) (callback_table[i].obj);
206 if (FD_ISSET (i, &fdset_tcl_x))
208 assert (callback_table[i].x_handle);
209 (*callback_table[i].x_handle) (callback_table[i].obj);
212 if (interactive && FD_ISSET(0, &fdset_tcl_r))
214 char input_buf[1024];
215 int count = read (0, input_buf, 1024);
219 Tcl_DStringAppend (&command, input_buf, count);
220 if (Tcl_CommandComplete (Tcl_DStringValue (&command)))
222 int code = Tcl_Eval (interp, Tcl_DStringValue (&command));
223 Tcl_DStringFree (&command);
225 printf ("Error: %s\n", interp->result);
226 else if (*interp->result)
227 printf ("%s\n", interp->result);
228 printf ("%% "); fflush (stdout);
234 void ir_select_add (int fd, void *obj)
236 callback_table[fd].obj = obj;
237 callback_table[fd].r_handle = ir_select_read;
238 callback_table[fd].w_handle = NULL;
239 callback_table[fd].x_handle = NULL;
244 void ir_select_add_write (int fd, void *obj)
246 callback_table[fd].w_handle = ir_select_write;
251 void ir_select_remove_write (int fd, void *obj)
253 callback_table[fd].w_handle = NULL;
256 void ir_select_remove (int fd, void *obj)
258 callback_table[fd].r_handle = NULL;
259 callback_table[fd].w_handle = NULL;
260 callback_table[fd].x_handle = NULL;