+void tcl_mainloop (Tcl_Interp *interp, int interactive)
+{
+ int i;
+ int res;
+ Tcl_DString command;
+ static fd_set fdset_tcl_r;
+ static fd_set fdset_tcl_w;
+ static fd_set fdset_tcl_x;
+ int min_fd;
+
+ min_fd = interactive ? 3 : 0;
+ if (interactive)
+ {
+ Tcl_DStringInit (&command);
+ printf ("%% "); fflush (stdout);
+ }
+ while (1)
+ {
+ FD_ZERO (&fdset_tcl_r);
+ FD_ZERO (&fdset_tcl_w);
+ FD_ZERO (&fdset_tcl_x);
+ if (interactive)
+ FD_SET (0, &fdset_tcl_r);
+ for (res=0, i=min_fd; i<=max_fd; i++)
+ {
+ if (callback_table[i].handle && callback_table[i].w)
+ {
+ FD_SET (i, &fdset_tcl_w);
+ res++;
+ }
+ if (callback_table[i].handle && callback_table[i].r)
+ {
+ FD_SET (i, &fdset_tcl_r);
+ res++;
+ }
+ if (callback_table[i].handle && callback_table[i].e)
+ {
+ FD_SET (i, &fdset_tcl_x);
+ res++;
+ }
+ }
+ if (!interactive && !res)
+ return;
+ if ((res = select(max_fd+1, &fdset_tcl_r, &fdset_tcl_w,
+ &fdset_tcl_x, 0)) < 0)
+ {
+ perror("select");
+ exit(1);
+ }
+ if (!res)
+ continue;
+ for (i=min_fd; i<=max_fd; i++)
+ {
+ int r_flag = 0;
+ int w_flag = 0;
+ int e_flag = 0;
+
+ if (!callback_table[i].handle)
+ continue;
+ if (FD_ISSET (i, &fdset_tcl_r) && callback_table[i].r)
+ r_flag = 1;
+ if (FD_ISSET (i, &fdset_tcl_w) && callback_table[i].w)
+ w_flag = 1;
+ if (FD_ISSET (i, &fdset_tcl_x) && callback_table[i].e)
+ e_flag = 1;
+ if (r_flag || w_flag || e_flag)
+ (*callback_table[i].handle)(callback_table[i].obj,
+ r_flag, w_flag, e_flag);
+ }
+ if (interactive && FD_ISSET(0, &fdset_tcl_r))
+ {
+ char input_buf[1024];
+ int count = read (0, input_buf, 1024);
+
+ if (count <= 0)
+ exit (0);
+ Tcl_DStringAppend (&command, input_buf, count);
+ if (Tcl_CommandComplete (Tcl_DStringValue (&command)))
+ {
+ int code = Tcl_Eval (interp, Tcl_DStringValue (&command));
+ Tcl_DStringFree (&command);
+ if (code)
+ printf ("Error: %s\n", interp->result);
+ else if (*interp->result)
+ printf ("%s\n", interp->result);
+ printf ("%% "); fflush (stdout);
+ }
+ }
+ }
+}
+
+void ir_tcl_select_set (void (*f)(ClientData clientData, int r, int w, int e),
+ int fd, ClientData clientData, int r, int w, int e)
+{
+ callback_table[fd].handle = f;
+ callback_table[fd].obj = clientData;
+ callback_table[fd].r = r;
+ callback_table[fd].w = w;
+ callback_table[fd].e = e;
+ if (fd > max_fd)
+ max_fd = fd;
+}
+