2 * IR toolkit for tcl/tk
4 * See the file LICENSE for details.
5 * Sebastian Hammer, Adam Dickmeiss
8 * Revision 1.7 1995-06-21 11:04:54 adam
9 * Uses GNU autoconf 2.3.
10 * Install procedure implemented.
11 * boook bitmaps moved to sub directory bitmaps.
13 * Revision 1.6 1995/05/29 08:44:28 adam
14 * Work on delete of objects.
16 * Revision 1.5 1995/03/20 08:53:30 adam
17 * Event loop in tclmain.c rewritten. New method searchStatus.
19 * Revision 1.4 1995/03/17 07:50:31 adam
20 * Headers have changed a little.
26 #include <sys/types.h>
28 #include <sys/select.h>
36 static char *fileName = NULL;
38 /* select(2) callbacks */
40 void (*r_handle)(void *p);
41 void (*w_handle)(void *p);
42 void (*x_handle)(void *p);
45 #define MAX_CALLBACK 200
47 static struct callback callback_table[MAX_CALLBACK];
48 static int max_fd = 3; /* don't worry: it will grow... */
50 void tcl_mainloop (Tcl_Interp *interp, int interactive);
52 int Tcl_AppInit (Tcl_Interp *interp)
54 if (Tcl_Init(interp) == TCL_ERROR)
56 if (ir_tcl_init(interp) == TCL_ERROR)
61 int main (int argc, char **argv)
67 interp = Tcl_CreateInterp();
68 Tcl_SetVar (interp, "tcl_interactive", "0", TCL_GLOBAL_ONLY);
72 if (Tcl_AppInit(interp) != TCL_OK) {
73 fprintf(stderr, "Tcl_AppInit failed: %s\n", interp->result);
75 for (i=0; i<MAX_CALLBACK; i++)
77 callback_table[i].r_handle = NULL;
78 callback_table[i].w_handle = NULL;
79 callback_table[i].x_handle = NULL;
83 code = Tcl_EvalFile (interp, fileName);
84 if (*interp->result != 0)
85 printf ("%s\n", interp->result);
88 tcl_mainloop (interp, 0);
92 Tcl_SetVar (interp, "tcl_interactive", "1", TCL_GLOBAL_ONLY);
93 tcl_mainloop (interp, 1);
98 void tcl_mainloop (Tcl_Interp *interp, int interactive)
103 static fd_set fdset_tcl_r;
104 static fd_set fdset_tcl_w;
105 static fd_set fdset_tcl_x;
108 min_fd = interactive ? 3 : 0;
111 Tcl_DStringInit (&command);
112 printf ("[TCL]"); fflush (stdout);
116 FD_ZERO (&fdset_tcl_r);
117 FD_ZERO (&fdset_tcl_w);
118 FD_ZERO (&fdset_tcl_x);
120 FD_SET (0, &fdset_tcl_r);
121 for (res=0, i=min_fd; i<=max_fd; i++)
123 if (callback_table[i].w_handle)
125 FD_SET (i, &fdset_tcl_w);
128 if (callback_table[i].r_handle)
130 FD_SET (i, &fdset_tcl_r);
133 if (callback_table[i].x_handle)
135 FD_SET (i, &fdset_tcl_x);
139 if (!interactive && !res)
141 if ((res = select(max_fd+1, &fdset_tcl_r, &fdset_tcl_w,
142 &fdset_tcl_x, 0)) < 0)
149 for (i=min_fd; i<=max_fd; i++)
151 if (FD_ISSET (i, &fdset_tcl_r))
153 assert (callback_table[i].r_handle);
154 (*callback_table[i].r_handle) (callback_table[i].obj);
156 if (FD_ISSET (i, &fdset_tcl_w))
158 assert (callback_table[i].w_handle);
159 (*callback_table[i].w_handle) (callback_table[i].obj);
161 if (FD_ISSET (i, &fdset_tcl_x))
163 assert (callback_table[i].x_handle);
164 (*callback_table[i].x_handle) (callback_table[i].obj);
167 if (interactive && FD_ISSET(0, &fdset_tcl_r))
170 int count = read (0, input_buf, 256);
174 Tcl_DStringAppend (&command, input_buf, count);
175 if (Tcl_CommandComplete (Tcl_DStringValue (&command)))
177 int code = Tcl_Eval (interp, Tcl_DStringValue (&command));
178 Tcl_DStringFree (&command);
180 printf ("[ERR:%s]\n", interp->result);
182 printf ("[RES:%s]\n", interp->result);
183 printf ("[TCL]"); fflush (stdout);
189 void ir_select_add (int fd, void *obj)
191 callback_table[fd].obj = obj;
192 callback_table[fd].r_handle = ir_select_read;
193 callback_table[fd].w_handle = NULL;
194 callback_table[fd].x_handle = NULL;
199 void ir_select_add_write (int fd, void *obj)
201 callback_table[fd].w_handle = ir_select_write;
206 void ir_select_remove_write (int fd, void *obj)
208 callback_table[fd].w_handle = NULL;
211 void ir_select_remove (int fd, void *obj)
213 callback_table[fd].r_handle = NULL;
214 callback_table[fd].w_handle = NULL;
215 callback_table[fd].x_handle = NULL;