2 * IR toolkit for tcl/tk
4 * See the file LICENSE for details.
5 * Sebastian Hammer, Adam Dickmeiss
8 * Revision 1.9 1995-06-26 10:20:20 adam
9 * ir-tk works like wish.
11 * Revision 1.8 1995/06/21 15:16:44 adam
12 * More work on configuration.
14 * Revision 1.7 1995/06/21 11:04:54 adam
15 * Uses GNU autoconf 2.3.
16 * Install procedure implemented.
17 * boook bitmaps moved to sub directory bitmaps.
19 * Revision 1.6 1995/05/29 08:44:28 adam
20 * Work on delete of objects.
22 * Revision 1.5 1995/03/20 08:53:30 adam
23 * Event loop in tclmain.c rewritten. New method searchStatus.
25 * Revision 1.4 1995/03/17 07:50:31 adam
26 * Headers have changed a little.
32 #include <sys/types.h>
34 #include <sys/select.h>
42 static char *fileName = NULL;
44 /* select(2) callbacks */
46 void (*r_handle)(void *p);
47 void (*w_handle)(void *p);
48 void (*x_handle)(void *p);
51 #define MAX_CALLBACK 200
53 static struct callback callback_table[MAX_CALLBACK];
54 static int max_fd = 3; /* don't worry: it will grow... */
56 void tcl_mainloop (Tcl_Interp *interp, int interactive);
58 int Tcl_AppInit (Tcl_Interp *interp)
60 if (Tcl_Init(interp) == TCL_ERROR)
62 if (ir_tcl_init(interp) == TCL_ERROR)
67 int main (int argc, char **argv)
73 interp = Tcl_CreateInterp();
74 Tcl_SetVar (interp, "tcl_interactive", "0", TCL_GLOBAL_ONLY);
78 if (Tcl_AppInit(interp) != TCL_OK) {
79 fprintf(stderr, "Tcl_AppInit failed: %s\n", interp->result);
81 for (i=0; i<MAX_CALLBACK; i++)
83 callback_table[i].r_handle = NULL;
84 callback_table[i].w_handle = NULL;
85 callback_table[i].x_handle = NULL;
89 code = Tcl_EvalFile (interp, fileName);
90 if (*interp->result != 0)
91 printf ("%s\n", interp->result);
94 tcl_mainloop (interp, 0);
99 Tcl_SetVar (interp, "tcl_interactive", "1", TCL_GLOBAL_ONLY);
100 tcl_mainloop (interp, 1);
105 char input_buf[1024];
109 Tcl_DStringInit (&command);
110 while (fgets (input_buf, 1024, stdin))
112 count = strlen(input_buf);
113 Tcl_DStringAppend (&command, input_buf, count);
114 if (Tcl_CommandComplete (Tcl_DStringValue (&command)))
116 int code = Tcl_Eval (interp, Tcl_DStringValue (&command));
117 Tcl_DStringFree (&command);
119 printf ("Error: %s\n", interp->result);
122 tcl_mainloop (interp, 0);
127 void tcl_mainloop (Tcl_Interp *interp, int interactive)
132 static fd_set fdset_tcl_r;
133 static fd_set fdset_tcl_w;
134 static fd_set fdset_tcl_x;
137 min_fd = interactive ? 3 : 0;
140 Tcl_DStringInit (&command);
141 printf ("%% "); fflush (stdout);
145 FD_ZERO (&fdset_tcl_r);
146 FD_ZERO (&fdset_tcl_w);
147 FD_ZERO (&fdset_tcl_x);
149 FD_SET (0, &fdset_tcl_r);
150 for (res=0, i=min_fd; i<=max_fd; i++)
152 if (callback_table[i].w_handle)
154 FD_SET (i, &fdset_tcl_w);
157 if (callback_table[i].r_handle)
159 FD_SET (i, &fdset_tcl_r);
162 if (callback_table[i].x_handle)
164 FD_SET (i, &fdset_tcl_x);
168 if (!interactive && !res)
170 if ((res = select(max_fd+1, &fdset_tcl_r, &fdset_tcl_w,
171 &fdset_tcl_x, 0)) < 0)
178 for (i=min_fd; i<=max_fd; i++)
180 if (FD_ISSET (i, &fdset_tcl_r))
182 assert (callback_table[i].r_handle);
183 (*callback_table[i].r_handle) (callback_table[i].obj);
185 if (FD_ISSET (i, &fdset_tcl_w))
187 assert (callback_table[i].w_handle);
188 (*callback_table[i].w_handle) (callback_table[i].obj);
190 if (FD_ISSET (i, &fdset_tcl_x))
192 assert (callback_table[i].x_handle);
193 (*callback_table[i].x_handle) (callback_table[i].obj);
196 if (interactive && FD_ISSET(0, &fdset_tcl_r))
198 char input_buf[1024];
199 int count = read (0, input_buf, 1024);
203 Tcl_DStringAppend (&command, input_buf, count);
204 if (Tcl_CommandComplete (Tcl_DStringValue (&command)))
206 int code = Tcl_Eval (interp, Tcl_DStringValue (&command));
207 Tcl_DStringFree (&command);
209 printf ("Error: %s\n", interp->result);
211 printf ("%s", interp->result);
212 printf ("%% "); fflush (stdout);
218 void ir_select_add (int fd, void *obj)
220 callback_table[fd].obj = obj;
221 callback_table[fd].r_handle = ir_select_read;
222 callback_table[fd].w_handle = NULL;
223 callback_table[fd].x_handle = NULL;
228 void ir_select_add_write (int fd, void *obj)
230 callback_table[fd].w_handle = ir_select_write;
235 void ir_select_remove_write (int fd, void *obj)
237 callback_table[fd].w_handle = NULL;
240 void ir_select_remove (int fd, void *obj)
242 callback_table[fd].r_handle = NULL;
243 callback_table[fd].w_handle = NULL;
244 callback_table[fd].x_handle = NULL;