2 * IR toolkit for tcl/tk
3 * (c) Index Data 1995-1996
4 * See the file LICENSE for details.
5 * Sebastian Hammer, Adam Dickmeiss
8 * Revision 1.19 1996-08-20 09:27:49 adam
9 * More work on explain.
10 * Renamed tkinit.c to tkmain.c. The tcl shell uses the Tcl 7.5 interface
11 * for socket i/o instead of the handcrafted one (for Tcl 7.3 and Tcl7.4).
17 #include <sys/types.h>
19 #include <sys/select.h>
28 int Tcl_AppInit (Tcl_Interp *interp)
30 if (Tcl_Init(interp) == TCL_ERROR)
32 if (Irtcl_Init(interp) == TCL_ERROR)
35 if (Waistcl_Init(interp) == TCL_ERROR)
41 #if TCL_MAJOR_VERSION > 7 || (TCL_MAJOR_VERSION == 7 && TCL_MINOR_VERSION > 4)
42 extern int matherr ();
43 int *tclDummyMathPtr = (int*) matherr;
45 int main (int argc, char **argv)
47 Tcl_Main (argc, argv, Tcl_AppInit);
52 static char *fileName = NULL;
54 int *tclDummyMainPtr = (int*) main;
56 /* select(2) callbacks */
58 void (*handle)(ClientData, int, int, int);
62 #define MAX_CALLBACK 200
64 static struct callback callback_table[MAX_CALLBACK];
65 static int max_fd = 3; /* don't worry: it will grow... */
67 void tcl_mainloop (Tcl_Interp *interp, int interactive);
69 int main (int argc, char **argv)
75 interp = Tcl_CreateInterp();
76 Tcl_SetVar (interp, "tcl_interactive", "0", TCL_GLOBAL_ONLY);
80 if (Tcl_AppInit(interp) != TCL_OK) {
81 fprintf(stderr, "Tcl_AppInit failed: %s\n", interp->result);
83 for (i=0; i<MAX_CALLBACK; i++)
84 callback_table[i].handle = NULL;
87 code = Tcl_EvalFile (interp, fileName);
88 if (*interp->result != 0)
89 printf ("%s\n", interp->result);
92 tcl_mainloop (interp, 0);
97 Tcl_SetVar (interp, "tcl_interactive", "1", TCL_GLOBAL_ONLY);
98 tcl_mainloop (interp, 1);
103 char input_buf[1024];
107 Tcl_DStringInit (&command);
108 while (fgets (input_buf, 1024, stdin))
110 count = strlen(input_buf);
111 Tcl_DStringAppend (&command, input_buf, count);
112 if (Tcl_CommandComplete (Tcl_DStringValue (&command)))
114 int code = Tcl_Eval (interp, Tcl_DStringValue (&command));
115 Tcl_DStringFree (&command);
117 printf ("Error: %s\n", interp->result);
120 tcl_mainloop (interp, 0);
125 void tcl_mainloop (Tcl_Interp *interp, int interactive)
130 static fd_set fdset_tcl_r;
131 static fd_set fdset_tcl_w;
132 static fd_set fdset_tcl_x;
135 min_fd = interactive ? 3 : 0;
138 Tcl_DStringInit (&command);
139 printf ("%% "); fflush (stdout);
143 FD_ZERO (&fdset_tcl_r);
144 FD_ZERO (&fdset_tcl_w);
145 FD_ZERO (&fdset_tcl_x);
147 FD_SET (0, &fdset_tcl_r);
148 for (res=0, i=min_fd; i<=max_fd; i++)
150 if (callback_table[i].handle && callback_table[i].w)
152 FD_SET (i, &fdset_tcl_w);
155 if (callback_table[i].handle && callback_table[i].r)
157 FD_SET (i, &fdset_tcl_r);
160 if (callback_table[i].handle && callback_table[i].e)
162 FD_SET (i, &fdset_tcl_x);
166 if (!interactive && !res)
168 if ((res = select(max_fd+1, &fdset_tcl_r, &fdset_tcl_w,
169 &fdset_tcl_x, 0)) < 0)
176 for (i=min_fd; i<=max_fd; i++)
182 if (!callback_table[i].handle)
184 if (FD_ISSET (i, &fdset_tcl_r) && callback_table[i].r)
186 if (FD_ISSET (i, &fdset_tcl_w) && callback_table[i].w)
188 if (FD_ISSET (i, &fdset_tcl_x) && callback_table[i].e)
190 if (r_flag || w_flag || e_flag)
191 (*callback_table[i].handle)(callback_table[i].obj,
192 r_flag, w_flag, e_flag);
194 if (interactive && FD_ISSET(0, &fdset_tcl_r))
196 char input_buf[1024];
197 int count = read (0, input_buf, 1024);
201 Tcl_DStringAppend (&command, input_buf, count);
202 if (Tcl_CommandComplete (Tcl_DStringValue (&command)))
204 int code = Tcl_Eval (interp, Tcl_DStringValue (&command));
205 Tcl_DStringFree (&command);
207 printf ("Error: %s\n", interp->result);
208 else if (*interp->result)
209 printf ("%s\n", interp->result);
210 printf ("%% "); fflush (stdout);
216 void ir_tcl_select_set (void (*f)(ClientData clientData, int r, int w, int e),
217 int fd, ClientData clientData, int r, int w, int e)
219 callback_table[fd].handle = f;
220 callback_table[fd].obj = clientData;
221 callback_table[fd].r = r;
222 callback_table[fd].w = w;
223 callback_table[fd].e = e;