2 * IR toolkit for tcl/tk
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.5 1995-03-20 08:53:30 adam
8 * Event loop in tclmain.c rewritten. New method searchStatus.
10 * Revision 1.4 1995/03/17 07:50:31 adam
11 * Headers have changed a little.
16 #include <sys/types.h>
24 static char *fileName = NULL;
26 /* select(2) callbacks */
28 void (*r_handle)(void *p);
29 void (*w_handle)(void *p);
30 void (*x_handle)(void *p);
33 #define MAX_CALLBACK 200
35 static struct callback callback_table[MAX_CALLBACK];
36 static int max_fd = 3; /* don't worry: it will grow... */
38 void tcl_mainloop (Tcl_Interp *interp, int interactive);
40 int Tcl_AppInit (Tcl_Interp *interp)
42 if (Tcl_Init(interp) == TCL_ERROR)
44 if (ir_tcl_init(interp) == TCL_ERROR)
49 int main (int argc, char **argv)
55 interp = Tcl_CreateInterp();
56 Tcl_SetVar (interp, "tcl_interactive", "0", TCL_GLOBAL_ONLY);
60 if (Tcl_AppInit(interp) != TCL_OK) {
61 fprintf(stderr, "Tcl_AppInit failed: %s\n", interp->result);
63 for (i=0; i<MAX_CALLBACK; i++)
65 callback_table[i].r_handle = NULL;
66 callback_table[i].w_handle = NULL;
67 callback_table[i].x_handle = NULL;
71 code = Tcl_EvalFile (interp, fileName);
72 if (*interp->result != 0)
73 printf ("%s\n", interp->result);
76 tcl_mainloop (interp, 0);
80 Tcl_SetVar (interp, "tcl_interactive", "1", TCL_GLOBAL_ONLY);
81 tcl_mainloop (interp, 1);
86 void tcl_mainloop (Tcl_Interp *interp, int interactive)
91 static fd_set fdset_tcl_r;
92 static fd_set fdset_tcl_w;
93 static fd_set fdset_tcl_x;
96 min_fd = interactive ? 3 : 0;
99 Tcl_DStringInit (&command);
100 printf ("[TCL]"); fflush (stdout);
104 FD_ZERO (&fdset_tcl_r);
105 FD_ZERO (&fdset_tcl_w);
106 FD_ZERO (&fdset_tcl_x);
108 FD_SET (0, &fdset_tcl_r);
109 for (res=0, i=min_fd; i<=max_fd; i++)
111 if (callback_table[i].w_handle)
113 FD_SET (i, &fdset_tcl_w);
116 if (callback_table[i].r_handle)
118 FD_SET (i, &fdset_tcl_r);
121 if (callback_table[i].x_handle)
123 FD_SET (i, &fdset_tcl_x);
127 if (!interactive && !res)
129 if ((res = select(max_fd+1, &fdset_tcl_r, &fdset_tcl_w,
130 &fdset_tcl_x, 0)) < 0)
137 for (i=min_fd; i<=max_fd; i++)
139 if (FD_ISSET (i, &fdset_tcl_r))
141 assert (callback_table[i].r_handle);
142 (*callback_table[i].r_handle) (callback_table[i].obj);
144 if (FD_ISSET (i, &fdset_tcl_w))
146 assert (callback_table[i].w_handle);
147 (*callback_table[i].w_handle) (callback_table[i].obj);
149 if (FD_ISSET (i, &fdset_tcl_x))
151 assert (callback_table[i].x_handle);
152 (*callback_table[i].x_handle) (callback_table[i].obj);
155 if (interactive && FD_ISSET(0, &fdset_tcl_r))
158 int count = read (0, input_buf, 256);
162 Tcl_DStringAppend (&command, input_buf, count);
163 if (Tcl_CommandComplete (Tcl_DStringValue (&command)))
165 int code = Tcl_Eval (interp, Tcl_DStringValue (&command));
166 Tcl_DStringFree (&command);
168 printf ("[ERR:%s]\n", interp->result);
170 printf ("[RES:%s]\n", interp->result);
171 printf ("[TCL]"); fflush (stdout);
177 void ir_select_add (int fd, void *obj)
179 callback_table[fd].obj = obj;
180 callback_table[fd].r_handle = ir_select_read;
181 callback_table[fd].w_handle = NULL;
182 callback_table[fd].x_handle = NULL;
187 void ir_select_add_write (int fd, void *obj)
189 callback_table[fd].w_handle = ir_select_write;
194 void ir_select_remove_write (int fd, void *obj)
196 callback_table[fd].w_handle = NULL;
199 void ir_select_remove (int fd, void *obj)
201 callback_table[fd].r_handle = NULL;
202 callback_table[fd].w_handle = NULL;
203 callback_table[fd].x_handle = NULL;