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.20 1997-04-30 07:26:08 adam
9 * Added support for shared libaries (if supported by Tcl itself).
11 * Revision 1.19 1996/08/20 09:27:49 adam
12 * More work on explain.
13 * Renamed tkinit.c to tkmain.c. The tcl shell uses the Tcl 7.5 interface
14 * for socket i/o instead of the handcrafted one (for Tcl 7.3 and Tcl7.4).
20 #include <sys/types.h>
22 #include <sys/select.h>
31 int Tcl_AppInit (Tcl_Interp *interp)
33 if (Tcl_Init(interp) == TCL_ERROR)
35 if (Irtcl_Init(interp) == TCL_ERROR)
38 if (Waistcl_Init(interp) == TCL_ERROR)
44 #if TCL_MAJOR_VERSION > 7 || (TCL_MAJOR_VERSION == 7 && TCL_MINOR_VERSION > 4)
45 /* new version of tcl: version > 7.4 */
46 extern int matherr ();
47 int *tclDummyMathPtr = (int*) matherr;
49 int main (int argc, char **argv)
51 Tcl_Main (argc, argv, Tcl_AppInit);
56 /* old version of tcl: version <= 7.4 */
58 static char *fileName = NULL;
60 int *tclDummyMainPtr = (int*) main;
62 /* select(2) callbacks */
64 void (*handle)(ClientData, int, int, int);
68 #define MAX_CALLBACK 200
70 static struct callback callback_table[MAX_CALLBACK];
71 static int max_fd = 3; /* don't worry: it will grow... */
73 void tcl_mainloop (Tcl_Interp *interp, int interactive);
75 int main (int argc, char **argv)
81 interp = Tcl_CreateInterp();
82 Tcl_SetVar (interp, "tcl_interactive", "0", TCL_GLOBAL_ONLY);
86 if (Tcl_AppInit(interp) != TCL_OK) {
87 fprintf(stderr, "Tcl_AppInit failed: %s\n", interp->result);
89 for (i=0; i<MAX_CALLBACK; i++)
90 callback_table[i].handle = NULL;
93 code = Tcl_EvalFile (interp, fileName);
94 if (*interp->result != 0)
95 printf ("%s\n", interp->result);
98 tcl_mainloop (interp, 0);
103 Tcl_SetVar (interp, "tcl_interactive", "1", TCL_GLOBAL_ONLY);
104 tcl_mainloop (interp, 1);
109 char input_buf[1024];
113 Tcl_DStringInit (&command);
114 while (fgets (input_buf, 1024, stdin))
116 count = strlen(input_buf);
117 Tcl_DStringAppend (&command, input_buf, count);
118 if (Tcl_CommandComplete (Tcl_DStringValue (&command)))
120 int code = Tcl_Eval (interp, Tcl_DStringValue (&command));
121 Tcl_DStringFree (&command);
123 printf ("Error: %s\n", interp->result);
126 tcl_mainloop (interp, 0);
131 void tcl_mainloop (Tcl_Interp *interp, int interactive)
136 static fd_set fdset_tcl_r;
137 static fd_set fdset_tcl_w;
138 static fd_set fdset_tcl_x;
141 min_fd = interactive ? 3 : 0;
144 Tcl_DStringInit (&command);
145 printf ("%% "); fflush (stdout);
149 FD_ZERO (&fdset_tcl_r);
150 FD_ZERO (&fdset_tcl_w);
151 FD_ZERO (&fdset_tcl_x);
153 FD_SET (0, &fdset_tcl_r);
154 for (res=0, i=min_fd; i<=max_fd; i++)
156 if (callback_table[i].handle && callback_table[i].w)
158 FD_SET (i, &fdset_tcl_w);
161 if (callback_table[i].handle && callback_table[i].r)
163 FD_SET (i, &fdset_tcl_r);
166 if (callback_table[i].handle && callback_table[i].e)
168 FD_SET (i, &fdset_tcl_x);
172 if (!interactive && !res)
174 if ((res = select(max_fd+1, &fdset_tcl_r, &fdset_tcl_w,
175 &fdset_tcl_x, 0)) < 0)
182 for (i=min_fd; i<=max_fd; i++)
188 if (!callback_table[i].handle)
190 if (FD_ISSET (i, &fdset_tcl_r) && callback_table[i].r)
192 if (FD_ISSET (i, &fdset_tcl_w) && callback_table[i].w)
194 if (FD_ISSET (i, &fdset_tcl_x) && callback_table[i].e)
196 if (r_flag || w_flag || e_flag)
197 (*callback_table[i].handle)(callback_table[i].obj,
198 r_flag, w_flag, e_flag);
200 if (interactive && FD_ISSET(0, &fdset_tcl_r))
202 char input_buf[1024];
203 int count = read (0, input_buf, 1024);
207 Tcl_DStringAppend (&command, input_buf, count);
208 if (Tcl_CommandComplete (Tcl_DStringValue (&command)))
210 int code = Tcl_Eval (interp, Tcl_DStringValue (&command));
211 Tcl_DStringFree (&command);
213 printf ("Error: %s\n", interp->result);
214 else if (*interp->result)
215 printf ("%s\n", interp->result);
216 printf ("%% "); fflush (stdout);
222 void ir_tcl_select_set (void (*f)(ClientData clientData, int r, int w, int e),
223 int fd, ClientData clientData, int r, int w, int e)
225 callback_table[fd].handle = f;
226 callback_table[fd].obj = clientData;
227 callback_table[fd].r = r;
228 callback_table[fd].w = w;
229 callback_table[fd].e = e;