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.21 2000-02-22 23:11:03 adam
9 * Fixed include statements.
11 * Revision 1.20 1997/04/30 07:26:08 adam
12 * Added support for shared libaries (if supported by Tcl itself).
14 * Revision 1.19 1996/08/20 09:27:49 adam
15 * More work on explain.
16 * Renamed tkinit.c to tkmain.c. The tcl shell uses the Tcl 7.5 interface
17 * for socket i/o instead of the handcrafted one (for Tcl 7.3 and Tcl7.4).
23 #include <sys/types.h>
25 #include <sys/select.h>
34 int Tcl_AppInit (Tcl_Interp *interp)
36 if (Tcl_Init(interp) == TCL_ERROR)
38 if (Irtcl_Init(interp) == TCL_ERROR)
41 if (Waistcl_Init(interp) == TCL_ERROR)
47 #if TCL_MAJOR_VERSION > 7 || (TCL_MAJOR_VERSION == 7 && TCL_MINOR_VERSION > 4)
48 /* new version of tcl: version > 7.4 */
49 extern int matherr ();
50 int *tclDummyMathPtr = (int*) matherr;
52 int main (int argc, char **argv)
54 Tcl_Main (argc, argv, Tcl_AppInit);
59 /* old version of tcl: version <= 7.4 */
61 static char *fileName = NULL;
63 int *tclDummyMainPtr = (int*) main;
65 /* select(2) callbacks */
67 void (*handle)(ClientData, int, int, int);
71 #define MAX_CALLBACK 200
73 static struct callback callback_table[MAX_CALLBACK];
74 static int max_fd = 3; /* don't worry: it will grow... */
76 void tcl_mainloop (Tcl_Interp *interp, int interactive);
78 int main (int argc, char **argv)
84 interp = Tcl_CreateInterp();
85 Tcl_SetVar (interp, "tcl_interactive", "0", TCL_GLOBAL_ONLY);
89 if (Tcl_AppInit(interp) != TCL_OK) {
90 fprintf(stderr, "Tcl_AppInit failed: %s\n", interp->result);
92 for (i=0; i<MAX_CALLBACK; i++)
93 callback_table[i].handle = NULL;
96 code = Tcl_EvalFile (interp, fileName);
97 if (*interp->result != 0)
98 printf ("%s\n", interp->result);
101 tcl_mainloop (interp, 0);
106 Tcl_SetVar (interp, "tcl_interactive", "1", TCL_GLOBAL_ONLY);
107 tcl_mainloop (interp, 1);
112 char input_buf[1024];
116 Tcl_DStringInit (&command);
117 while (fgets (input_buf, 1024, stdin))
119 count = strlen(input_buf);
120 Tcl_DStringAppend (&command, input_buf, count);
121 if (Tcl_CommandComplete (Tcl_DStringValue (&command)))
123 int code = Tcl_Eval (interp, Tcl_DStringValue (&command));
124 Tcl_DStringFree (&command);
126 printf ("Error: %s\n", interp->result);
129 tcl_mainloop (interp, 0);
134 void tcl_mainloop (Tcl_Interp *interp, int interactive)
139 static fd_set fdset_tcl_r;
140 static fd_set fdset_tcl_w;
141 static fd_set fdset_tcl_x;
144 min_fd = interactive ? 3 : 0;
147 Tcl_DStringInit (&command);
148 printf ("%% "); fflush (stdout);
152 FD_ZERO (&fdset_tcl_r);
153 FD_ZERO (&fdset_tcl_w);
154 FD_ZERO (&fdset_tcl_x);
156 FD_SET (0, &fdset_tcl_r);
157 for (res=0, i=min_fd; i<=max_fd; i++)
159 if (callback_table[i].handle && callback_table[i].w)
161 FD_SET (i, &fdset_tcl_w);
164 if (callback_table[i].handle && callback_table[i].r)
166 FD_SET (i, &fdset_tcl_r);
169 if (callback_table[i].handle && callback_table[i].e)
171 FD_SET (i, &fdset_tcl_x);
175 if (!interactive && !res)
177 if ((res = select(max_fd+1, &fdset_tcl_r, &fdset_tcl_w,
178 &fdset_tcl_x, 0)) < 0)
185 for (i=min_fd; i<=max_fd; i++)
191 if (!callback_table[i].handle)
193 if (FD_ISSET (i, &fdset_tcl_r) && callback_table[i].r)
195 if (FD_ISSET (i, &fdset_tcl_w) && callback_table[i].w)
197 if (FD_ISSET (i, &fdset_tcl_x) && callback_table[i].e)
199 if (r_flag || w_flag || e_flag)
200 (*callback_table[i].handle)(callback_table[i].obj,
201 r_flag, w_flag, e_flag);
203 if (interactive && FD_ISSET(0, &fdset_tcl_r))
205 char input_buf[1024];
206 int count = read (0, input_buf, 1024);
210 Tcl_DStringAppend (&command, input_buf, count);
211 if (Tcl_CommandComplete (Tcl_DStringValue (&command)))
213 int code = Tcl_Eval (interp, Tcl_DStringValue (&command));
214 Tcl_DStringFree (&command);
216 printf ("Error: %s\n", interp->result);
217 else if (*interp->result)
218 printf ("%s\n", interp->result);
219 printf ("%% "); fflush (stdout);
225 void ir_tcl_select_set (void (*f)(ClientData clientData, int r, int w, int e),
226 int fd, ClientData clientData, int r, int w, int e)
228 callback_table[fd].handle = f;
229 callback_table[fd].obj = clientData;
230 callback_table[fd].r = r;
231 callback_table[fd].w = w;
232 callback_table[fd].e = e;