2 * IR toolkit for tcl/tk
5 * $Id: tclmain.c,v 1.3 1995-03-09 08:35:58 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();
38 Tcl_SetVar (interp, "tcl_interactive", "0", TCL_GLOBAL_ONLY);
42 if (Tcl_AppInit(interp) != TCL_OK) {
43 fprintf(stderr, "Tcl_AppInit failed: %s\n", interp->result);
47 code = Tcl_EvalFile (interp, fileName);
48 if (*interp->result != 0)
49 printf ("%s\n", interp->result);
53 Tcl_SetVar (interp, "tcl_interactive", "1", TCL_GLOBAL_ONLY);
54 tcl_mainloop (interp);
59 void (*handle)(void *p);
63 #define MAX_CALLBACK 20
65 struct callback callback_table[MAX_CALLBACK];
67 void tcl_mainloop (Tcl_Interp *interp)
75 for (i=0; i<MAX_CALLBACK; i++)
76 callback_table[i].handle = NULL;
77 Tcl_DStringInit (&command);
78 printf ("[TCL]"); fflush (stdout);
82 FD_SET (0, &fdset_tcl);
83 for (i=3; i<MAX_CALLBACK; i++)
84 if (callback_table[i].handle)
85 FD_SET (i, &fdset_tcl);
86 if ((res = select(MAX_CALLBACK+1, &fdset_tcl, 0, 0, 0)) < 0)
93 for (i=3; i<MAX_CALLBACK; i++)
94 if (FD_ISSET (i, &fdset_tcl))
96 assert (callback_table[i].handle);
97 (*callback_table[i].handle) (callback_table[i].obj);
99 if (FD_ISSET(0, &fdset_tcl))
101 count = read (0, input_buf, 256);
104 Tcl_DStringAppend (&command, input_buf, count);
105 if (Tcl_CommandComplete (Tcl_DStringValue (&command)))
107 int code = Tcl_Eval (interp, Tcl_DStringValue (&command));
108 Tcl_DStringFree (&command);
110 printf ("[ERR:%s]\n", interp->result);
112 printf ("[RES:%s]\n", interp->result);
113 printf ("[TCL]"); fflush (stdout);
119 void ir_select_add (int fd, void *obj)
121 callback_table[fd].obj = obj;
122 callback_table[fd].handle = ir_select_proc;
125 void ir_select_remove (int fd, void *obj)
127 callback_table[fd].handle = NULL;