2 * IR toolkit for tcl/tk
4 * See the file LICENSE for details.
5 * Sebastian Hammer, Adam Dickmeiss
8 * Revision 1.8 1995-06-21 15:16:44 adam
9 * More work on configuration.
11 * Revision 1.7 1995/06/21 11:04:54 adam
12 * Uses GNU autoconf 2.3.
13 * Install procedure implemented.
14 * boook bitmaps moved to sub directory bitmaps.
16 * Revision 1.6 1995/05/29 08:44:28 adam
17 * Work on delete of objects.
19 * Revision 1.5 1995/03/20 08:53:30 adam
20 * Event loop in tclmain.c rewritten. New method searchStatus.
22 * Revision 1.4 1995/03/17 07:50:31 adam
23 * Headers have changed a little.
29 #include <sys/types.h>
31 #include <sys/select.h>
39 static char *fileName = NULL;
41 /* select(2) callbacks */
43 void (*r_handle)(void *p);
44 void (*w_handle)(void *p);
45 void (*x_handle)(void *p);
48 #define MAX_CALLBACK 200
50 static struct callback callback_table[MAX_CALLBACK];
51 static int max_fd = 3; /* don't worry: it will grow... */
53 void tcl_mainloop (Tcl_Interp *interp, int interactive);
55 int Tcl_AppInit (Tcl_Interp *interp)
57 if (Tcl_Init(interp) == TCL_ERROR)
59 if (ir_tcl_init(interp) == TCL_ERROR)
64 int main (int argc, char **argv)
70 interp = Tcl_CreateInterp();
71 Tcl_SetVar (interp, "tcl_interactive", "0", TCL_GLOBAL_ONLY);
75 if (Tcl_AppInit(interp) != TCL_OK) {
76 fprintf(stderr, "Tcl_AppInit failed: %s\n", interp->result);
78 for (i=0; i<MAX_CALLBACK; i++)
80 callback_table[i].r_handle = NULL;
81 callback_table[i].w_handle = NULL;
82 callback_table[i].x_handle = NULL;
86 code = Tcl_EvalFile (interp, fileName);
87 if (*interp->result != 0)
88 printf ("%s\n", interp->result);
91 tcl_mainloop (interp, 0);
95 Tcl_SetVar (interp, "tcl_interactive", "1", TCL_GLOBAL_ONLY);
96 tcl_mainloop (interp, 1);
101 void tcl_mainloop (Tcl_Interp *interp, int interactive)
106 static fd_set fdset_tcl_r;
107 static fd_set fdset_tcl_w;
108 static fd_set fdset_tcl_x;
111 min_fd = interactive ? 3 : 0;
114 Tcl_DStringInit (&command);
115 printf ("[TCL]"); fflush (stdout);
119 FD_ZERO (&fdset_tcl_r);
120 FD_ZERO (&fdset_tcl_w);
121 FD_ZERO (&fdset_tcl_x);
123 FD_SET (0, &fdset_tcl_r);
124 for (res=0, i=min_fd; i<=max_fd; i++)
126 if (callback_table[i].w_handle)
128 FD_SET (i, &fdset_tcl_w);
131 if (callback_table[i].r_handle)
133 FD_SET (i, &fdset_tcl_r);
136 if (callback_table[i].x_handle)
138 FD_SET (i, &fdset_tcl_x);
142 if (!interactive && !res)
144 if ((res = select(max_fd+1, &fdset_tcl_r, &fdset_tcl_w,
145 &fdset_tcl_x, 0)) < 0)
152 for (i=min_fd; i<=max_fd; i++)
154 if (FD_ISSET (i, &fdset_tcl_r))
156 assert (callback_table[i].r_handle);
157 (*callback_table[i].r_handle) (callback_table[i].obj);
159 if (FD_ISSET (i, &fdset_tcl_w))
161 assert (callback_table[i].w_handle);
162 (*callback_table[i].w_handle) (callback_table[i].obj);
164 if (FD_ISSET (i, &fdset_tcl_x))
166 assert (callback_table[i].x_handle);
167 (*callback_table[i].x_handle) (callback_table[i].obj);
170 if (interactive && FD_ISSET(0, &fdset_tcl_r))
173 int count = read (0, input_buf, 256);
177 Tcl_DStringAppend (&command, input_buf, count);
178 if (Tcl_CommandComplete (Tcl_DStringValue (&command)))
180 int code = Tcl_Eval (interp, Tcl_DStringValue (&command));
181 Tcl_DStringFree (&command);
183 printf ("[ERR:%s]\n", interp->result);
185 printf ("[RES:%s]\n", interp->result);
186 printf ("[TCL]"); fflush (stdout);
192 void ir_select_add (int fd, void *obj)
194 callback_table[fd].obj = obj;
195 callback_table[fd].r_handle = ir_select_read;
196 callback_table[fd].w_handle = NULL;
197 callback_table[fd].x_handle = NULL;
202 void ir_select_add_write (int fd, void *obj)
204 callback_table[fd].w_handle = ir_select_write;
209 void ir_select_remove_write (int fd, void *obj)
211 callback_table[fd].w_handle = NULL;
214 void ir_select_remove (int fd, void *obj)
216 callback_table[fd].r_handle = NULL;
217 callback_table[fd].w_handle = NULL;
218 callback_table[fd].x_handle = NULL;