2 * IR toolkit for tcl/tk
4 * See the file LICENSE for details.
5 * Sebastian Hammer, Adam Dickmeiss
8 * Revision 1.6 1995-05-29 08:44:28 adam
9 * Work on delete of objects.
11 * Revision 1.5 1995/03/20 08:53:30 adam
12 * Event loop in tclmain.c rewritten. New method searchStatus.
14 * Revision 1.4 1995/03/17 07:50:31 adam
15 * Headers have changed a little.
20 #include <sys/types.h>
28 static char *fileName = NULL;
30 /* select(2) callbacks */
32 void (*r_handle)(void *p);
33 void (*w_handle)(void *p);
34 void (*x_handle)(void *p);
37 #define MAX_CALLBACK 200
39 static struct callback callback_table[MAX_CALLBACK];
40 static int max_fd = 3; /* don't worry: it will grow... */
42 void tcl_mainloop (Tcl_Interp *interp, int interactive);
44 int Tcl_AppInit (Tcl_Interp *interp)
46 if (Tcl_Init(interp) == TCL_ERROR)
48 if (ir_tcl_init(interp) == TCL_ERROR)
53 int main (int argc, char **argv)
59 interp = Tcl_CreateInterp();
60 Tcl_SetVar (interp, "tcl_interactive", "0", TCL_GLOBAL_ONLY);
64 if (Tcl_AppInit(interp) != TCL_OK) {
65 fprintf(stderr, "Tcl_AppInit failed: %s\n", interp->result);
67 for (i=0; i<MAX_CALLBACK; i++)
69 callback_table[i].r_handle = NULL;
70 callback_table[i].w_handle = NULL;
71 callback_table[i].x_handle = NULL;
75 code = Tcl_EvalFile (interp, fileName);
76 if (*interp->result != 0)
77 printf ("%s\n", interp->result);
80 tcl_mainloop (interp, 0);
84 Tcl_SetVar (interp, "tcl_interactive", "1", TCL_GLOBAL_ONLY);
85 tcl_mainloop (interp, 1);
90 void tcl_mainloop (Tcl_Interp *interp, int interactive)
95 static fd_set fdset_tcl_r;
96 static fd_set fdset_tcl_w;
97 static fd_set fdset_tcl_x;
100 min_fd = interactive ? 3 : 0;
103 Tcl_DStringInit (&command);
104 printf ("[TCL]"); fflush (stdout);
108 FD_ZERO (&fdset_tcl_r);
109 FD_ZERO (&fdset_tcl_w);
110 FD_ZERO (&fdset_tcl_x);
112 FD_SET (0, &fdset_tcl_r);
113 for (res=0, i=min_fd; i<=max_fd; i++)
115 if (callback_table[i].w_handle)
117 FD_SET (i, &fdset_tcl_w);
120 if (callback_table[i].r_handle)
122 FD_SET (i, &fdset_tcl_r);
125 if (callback_table[i].x_handle)
127 FD_SET (i, &fdset_tcl_x);
131 if (!interactive && !res)
133 if ((res = select(max_fd+1, &fdset_tcl_r, &fdset_tcl_w,
134 &fdset_tcl_x, 0)) < 0)
141 for (i=min_fd; i<=max_fd; i++)
143 if (FD_ISSET (i, &fdset_tcl_r))
145 assert (callback_table[i].r_handle);
146 (*callback_table[i].r_handle) (callback_table[i].obj);
148 if (FD_ISSET (i, &fdset_tcl_w))
150 assert (callback_table[i].w_handle);
151 (*callback_table[i].w_handle) (callback_table[i].obj);
153 if (FD_ISSET (i, &fdset_tcl_x))
155 assert (callback_table[i].x_handle);
156 (*callback_table[i].x_handle) (callback_table[i].obj);
159 if (interactive && FD_ISSET(0, &fdset_tcl_r))
162 int count = read (0, input_buf, 256);
166 Tcl_DStringAppend (&command, input_buf, count);
167 if (Tcl_CommandComplete (Tcl_DStringValue (&command)))
169 int code = Tcl_Eval (interp, Tcl_DStringValue (&command));
170 Tcl_DStringFree (&command);
172 printf ("[ERR:%s]\n", interp->result);
174 printf ("[RES:%s]\n", interp->result);
175 printf ("[TCL]"); fflush (stdout);
181 void ir_select_add (int fd, void *obj)
183 callback_table[fd].obj = obj;
184 callback_table[fd].r_handle = ir_select_read;
185 callback_table[fd].w_handle = NULL;
186 callback_table[fd].x_handle = NULL;
191 void ir_select_add_write (int fd, void *obj)
193 callback_table[fd].w_handle = ir_select_write;
198 void ir_select_remove_write (int fd, void *obj)
200 callback_table[fd].w_handle = NULL;
203 void ir_select_remove (int fd, void *obj)
205 callback_table[fd].r_handle = NULL;
206 callback_table[fd].w_handle = NULL;
207 callback_table[fd].x_handle = NULL;