2 * IR toolkit for tcl/tk
4 * See the file LICENSE for details.
5 * Sebastian Hammer, Adam Dickmeiss
8 * Revision 1.12 1995-08-28 11:07:16 adam
11 * Revision 1.11 1995/08/03 13:23:02 adam
14 * Revision 1.10 1995/06/30 12:39:28 adam
15 * Bug fix: loadFile didn't set record type.
16 * The MARC routines are a little less strict in the interpretation.
17 * Script display.tcl replaces the old marc.tcl.
18 * New interactive script: shell.tcl.
20 * Revision 1.9 1995/06/26 10:20:20 adam
21 * ir-tk works like wish.
23 * Revision 1.8 1995/06/21 15:16:44 adam
24 * More work on configuration.
26 * Revision 1.7 1995/06/21 11:04:54 adam
27 * Uses GNU autoconf 2.3.
28 * Install procedure implemented.
29 * boook bitmaps moved to sub directory bitmaps.
31 * Revision 1.6 1995/05/29 08:44:28 adam
32 * Work on delete of objects.
34 * Revision 1.5 1995/03/20 08:53:30 adam
35 * Event loop in tclmain.c rewritten. New method searchStatus.
37 * Revision 1.4 1995/03/17 07:50:31 adam
38 * Headers have changed a little.
44 #include <sys/types.h>
46 #include <sys/select.h>
54 static char *fileName = NULL;
56 /* select(2) callbacks */
58 void (*r_handle)(ClientData);
59 void (*w_handle)(ClientData);
60 void (*x_handle)(ClientData);
63 #define MAX_CALLBACK 200
65 static struct callback callback_table[MAX_CALLBACK];
66 static int max_fd = 3; /* don't worry: it will grow... */
68 void tcl_mainloop (Tcl_Interp *interp, int interactive);
70 int Tcl_AppInit (Tcl_Interp *interp)
72 if (Tcl_Init(interp) == TCL_ERROR)
74 if (ir_tcl_init(interp) == TCL_ERROR)
79 int main (int argc, char **argv)
85 interp = Tcl_CreateInterp();
86 Tcl_SetVar (interp, "tcl_interactive", "0", TCL_GLOBAL_ONLY);
90 log_init (LOG_ALL, "", NULL);
91 if (Tcl_AppInit(interp) != TCL_OK) {
92 fprintf(stderr, "Tcl_AppInit failed: %s\n", interp->result);
94 for (i=0; i<MAX_CALLBACK; i++)
96 callback_table[i].r_handle = NULL;
97 callback_table[i].w_handle = NULL;
98 callback_table[i].x_handle = NULL;
102 code = Tcl_EvalFile (interp, fileName);
103 if (*interp->result != 0)
104 printf ("%s\n", interp->result);
107 tcl_mainloop (interp, 0);
112 Tcl_SetVar (interp, "tcl_interactive", "1", TCL_GLOBAL_ONLY);
113 tcl_mainloop (interp, 1);
118 char input_buf[1024];
122 Tcl_DStringInit (&command);
123 while (fgets (input_buf, 1024, stdin))
125 count = strlen(input_buf);
126 Tcl_DStringAppend (&command, input_buf, count);
127 if (Tcl_CommandComplete (Tcl_DStringValue (&command)))
129 int code = Tcl_Eval (interp, Tcl_DStringValue (&command));
130 Tcl_DStringFree (&command);
132 printf ("Error: %s\n", interp->result);
135 tcl_mainloop (interp, 0);
140 void tcl_mainloop (Tcl_Interp *interp, int interactive)
145 static fd_set fdset_tcl_r;
146 static fd_set fdset_tcl_w;
147 static fd_set fdset_tcl_x;
150 min_fd = interactive ? 3 : 0;
153 Tcl_DStringInit (&command);
154 printf ("%% "); fflush (stdout);
158 FD_ZERO (&fdset_tcl_r);
159 FD_ZERO (&fdset_tcl_w);
160 FD_ZERO (&fdset_tcl_x);
162 FD_SET (0, &fdset_tcl_r);
163 for (res=0, i=min_fd; i<=max_fd; i++)
165 if (callback_table[i].w_handle)
167 FD_SET (i, &fdset_tcl_w);
170 if (callback_table[i].r_handle)
172 FD_SET (i, &fdset_tcl_r);
175 if (callback_table[i].x_handle)
177 FD_SET (i, &fdset_tcl_x);
181 if (!interactive && !res)
183 if ((res = select(max_fd+1, &fdset_tcl_r, &fdset_tcl_w,
184 &fdset_tcl_x, 0)) < 0)
191 for (i=min_fd; i<=max_fd; i++)
193 if (FD_ISSET (i, &fdset_tcl_r))
195 assert (callback_table[i].r_handle);
196 (*callback_table[i].r_handle) (callback_table[i].obj);
198 if (FD_ISSET (i, &fdset_tcl_w))
200 assert (callback_table[i].w_handle);
201 (*callback_table[i].w_handle) (callback_table[i].obj);
203 if (FD_ISSET (i, &fdset_tcl_x))
205 assert (callback_table[i].x_handle);
206 (*callback_table[i].x_handle) (callback_table[i].obj);
209 if (interactive && FD_ISSET(0, &fdset_tcl_r))
211 char input_buf[1024];
212 int count = read (0, input_buf, 1024);
216 Tcl_DStringAppend (&command, input_buf, count);
217 if (Tcl_CommandComplete (Tcl_DStringValue (&command)))
219 int code = Tcl_Eval (interp, Tcl_DStringValue (&command));
220 Tcl_DStringFree (&command);
222 printf ("Error: %s\n", interp->result);
223 else if (*interp->result)
224 printf ("%s\n", interp->result);
225 printf ("%% "); fflush (stdout);
231 void ir_select_add (int fd, void *obj)
233 callback_table[fd].obj = obj;
234 callback_table[fd].r_handle = ir_select_read;
235 callback_table[fd].w_handle = NULL;
236 callback_table[fd].x_handle = NULL;
241 void ir_select_add_write (int fd, void *obj)
243 callback_table[fd].w_handle = ir_select_write;
248 void ir_select_remove_write (int fd, void *obj)
250 callback_table[fd].w_handle = NULL;
253 void ir_select_remove (int fd, void *obj)
255 callback_table[fd].r_handle = NULL;
256 callback_table[fd].w_handle = NULL;
257 callback_table[fd].x_handle = NULL;