2 * IR toolkit for tcl/tk
5 * $Id: tclmain.c,v 1.2 1995-03-08 07:28:37 adam Exp $
17 static char *fileName = NULL;
19 static fd_set fdset_tcl;
21 void tcl_mainloop (Tcl_Interp *interp);
23 int Tcl_AppInit (Tcl_Interp *interp)
25 if (Tcl_Init(interp) == TCL_ERROR)
27 if (ir_tcl_init(interp) == TCL_ERROR)
32 int main (int argc, char **argv)
37 interp = Tcl_CreateInterp();
41 fprintf (stderr, "Script file expected\n");
47 fprintf (stderr, "No filename specified\n");
50 if (Tcl_AppInit(interp) != TCL_OK) {
51 fprintf(stderr, "Tcl_AppInit failed: %s\n", interp->result);
53 code = Tcl_EvalFile (interp, fileName);
54 if (*interp->result != 0)
55 printf ("%s\n", interp->result);
58 tcl_mainloop (interp);
63 void (*handle)(void *p);
67 #define MAX_CALLBACK 20
69 struct callback callback_table[MAX_CALLBACK];
71 void tcl_mainloop (Tcl_Interp *interp)
79 for (i=0; i<MAX_CALLBACK; i++)
80 callback_table[i].handle = NULL;
81 Tcl_DStringInit (&command);
82 printf ("[TCL]"); fflush (stdout);
86 FD_SET (0, &fdset_tcl);
87 for (i=3; i<MAX_CALLBACK; i++)
88 if (callback_table[i].handle)
89 FD_SET (i, &fdset_tcl);
90 if ((res = select(MAX_CALLBACK+1, &fdset_tcl, 0, 0, 0)) < 0)
97 for (i=3; i<MAX_CALLBACK; i++)
98 if (FD_ISSET (i, &fdset_tcl))
100 assert (callback_table[i].handle);
101 (*callback_table[i].handle) (callback_table[i].obj);
103 if (FD_ISSET(0, &fdset_tcl))
105 count = read (0, input_buf, 256);
108 Tcl_DStringAppend (&command, input_buf, count);
109 if (Tcl_CommandComplete (Tcl_DStringValue (&command)))
111 int code = Tcl_Eval (interp, Tcl_DStringValue (&command));
112 Tcl_DStringFree (&command);
113 printf ("[RES:%s]\n", interp->result);
114 printf ("[TCL]"); fflush (stdout);
120 void ir_select_add (int fd, void *obj)
122 callback_table[fd].obj = obj;
123 callback_table[fd].handle = ir_select_proc;
126 void ir_select_remove (int fd, void *obj)
128 callback_table[fd].handle = NULL;