SearchRequest (CCL-query) implemented.
[ir-tcl-moved-to-github.git] / tclmain.c
1 /*
2  * IR toolkit for tcl/tk
3  * (c) Index Data 1995
4  *
5  * $Id: tclmain.c,v 1.3 1995-03-09 08:35:58 adam Exp $
6  */
7
8 #include <sys/time.h>
9 #include <sys/types.h>
10 #include <assert.h>
11 #include <unistd.h>
12
13 #include <tcl.h>
14
15 #include "ir-tcl.h"
16
17 static char *fileName = NULL;
18
19 static fd_set fdset_tcl;
20
21 void tcl_mainloop (Tcl_Interp *interp);
22
23 int Tcl_AppInit (Tcl_Interp *interp)
24 {
25     if (Tcl_Init(interp) == TCL_ERROR)
26         return TCL_ERROR;
27     if (ir_tcl_init(interp) == TCL_ERROR)
28         return TCL_ERROR;
29     return TCL_OK;
30 }
31
32 int main (int argc, char **argv)
33 {
34     Tcl_Interp *interp;
35     int code;
36
37     interp = Tcl_CreateInterp();
38     Tcl_SetVar (interp, "tcl_interactive", "0", TCL_GLOBAL_ONLY);
39     if (argc == 2)
40         fileName = argv[1];
41
42     if (Tcl_AppInit(interp) != TCL_OK) {
43         fprintf(stderr, "Tcl_AppInit failed: %s\n", interp->result);
44     }
45     if (fileName)
46     {
47         code = Tcl_EvalFile (interp, fileName);
48         if (*interp->result != 0)
49             printf ("%s\n", interp->result);
50         if (code != TCL_OK)
51             exit (1);
52     }
53     Tcl_SetVar (interp, "tcl_interactive", "1", TCL_GLOBAL_ONLY);
54     tcl_mainloop (interp);
55     exit (0);
56 }
57
58 struct callback {
59     void (*handle)(void *p);
60     void *obj;
61 };
62
63 #define MAX_CALLBACK 20
64
65 struct callback callback_table[MAX_CALLBACK];
66
67 void tcl_mainloop (Tcl_Interp *interp)
68 {
69     int i;
70     int res;
71     int count;
72     char input_buf[256];
73     Tcl_DString command;
74
75     for (i=0; i<MAX_CALLBACK; i++)
76         callback_table[i].handle = NULL;
77     Tcl_DStringInit (&command);
78     printf ("[TCL]"); fflush (stdout);
79     while (1)
80     {
81         FD_ZERO (&fdset_tcl);
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)
87         {
88             perror("select");
89             exit(1);
90         }
91         if (!res)
92             continue;
93         for (i=3; i<MAX_CALLBACK; i++)
94             if (FD_ISSET (i, &fdset_tcl))
95             {
96                 assert (callback_table[i].handle);
97                 (*callback_table[i].handle) (callback_table[i].obj);
98             }
99         if (FD_ISSET(0, &fdset_tcl))
100         {
101             count = read (0, input_buf, 256);
102             if (count <= 0)
103                 exit (0);
104             Tcl_DStringAppend (&command, input_buf, count);
105             if (Tcl_CommandComplete (Tcl_DStringValue (&command)))
106             {
107                 int code = Tcl_Eval (interp, Tcl_DStringValue (&command));
108                 Tcl_DStringFree (&command);
109                 if (code)
110                     printf ("[ERR:%s]\n", interp->result);
111                 else
112                     printf ("[RES:%s]\n", interp->result);
113                 printf ("[TCL]"); fflush (stdout);
114             }
115         }
116     }
117 }
118
119 void ir_select_add (int fd, void *obj)
120 {
121     callback_table[fd].obj = obj;
122     callback_table[fd].handle = ir_select_proc;
123 }
124
125 void ir_select_remove (int fd, void *obj)
126 {
127     callback_table[fd].handle = NULL;
128 }