ir-tk works like wish.
[ir-tcl-moved-to-github.git] / tclmain.c
1 /*
2  * IR toolkit for tcl/tk
3  * (c) Index Data 1995
4  * See the file LICENSE for details.
5  * Sebastian Hammer, Adam Dickmeiss
6  *
7  * $Log: tclmain.c,v $
8  * Revision 1.9  1995-06-26 10:20:20  adam
9  * ir-tk works like wish.
10  *
11  * Revision 1.8  1995/06/21  15:16:44  adam
12  * More work on configuration.
13  *
14  * Revision 1.7  1995/06/21  11:04:54  adam
15  * Uses GNU autoconf 2.3.
16  * Install procedure implemented.
17  * boook bitmaps moved to sub directory bitmaps.
18  *
19  * Revision 1.6  1995/05/29  08:44:28  adam
20  * Work on delete of objects.
21  *
22  * Revision 1.5  1995/03/20  08:53:30  adam
23  * Event loop in tclmain.c rewritten. New method searchStatus.
24  *
25  * Revision 1.4  1995/03/17  07:50:31  adam
26  * Headers have changed a little.
27  *
28  */
29
30 #include <unistd.h>
31 #include <sys/time.h>
32 #include <sys/types.h>
33 #ifdef _AIX
34 #include <sys/select.h>
35 #endif
36 #include <assert.h>
37
38 #include <tcl.h>
39
40 #include "ir-tcl.h"
41
42 static char *fileName = NULL;
43
44 /* select(2) callbacks */
45 struct callback {
46     void (*r_handle)(void *p);
47     void (*w_handle)(void *p);
48     void (*x_handle)(void *p);
49     void *obj;
50 };
51 #define MAX_CALLBACK 200
52
53 static struct callback callback_table[MAX_CALLBACK];
54 static int max_fd = 3;            /* don't worry: it will grow... */
55
56 void tcl_mainloop (Tcl_Interp *interp, int interactive);
57
58 int Tcl_AppInit (Tcl_Interp *interp)
59 {
60     if (Tcl_Init(interp) == TCL_ERROR)
61         return TCL_ERROR;
62     if (ir_tcl_init(interp) == TCL_ERROR)
63         return TCL_ERROR;
64     return TCL_OK;
65 }
66
67 int main (int argc, char **argv)
68 {
69     Tcl_Interp *interp;
70     int code;
71     int i;
72
73     interp = Tcl_CreateInterp();
74     Tcl_SetVar (interp, "tcl_interactive", "0", TCL_GLOBAL_ONLY);
75     if (argc == 2)
76         fileName = argv[1];
77
78     if (Tcl_AppInit(interp) != TCL_OK) {
79         fprintf(stderr, "Tcl_AppInit failed: %s\n", interp->result);
80     }
81     for (i=0; i<MAX_CALLBACK; i++)
82     {
83         callback_table[i].r_handle = NULL;
84         callback_table[i].w_handle = NULL;
85         callback_table[i].x_handle = NULL;
86     }
87     if (fileName)
88     {
89         code = Tcl_EvalFile (interp, fileName);
90         if (*interp->result != 0)
91             printf ("%s\n", interp->result);
92         if (code != TCL_OK)
93             exit (1);
94         tcl_mainloop (interp, 0);
95     }
96     else if (isatty(0))
97     {
98
99         Tcl_SetVar (interp, "tcl_interactive", "1", TCL_GLOBAL_ONLY);
100         tcl_mainloop (interp, 1);
101     }
102     else
103     {
104         Tcl_DString command;
105         char input_buf[1024];
106         int count;
107
108         printf ("xx\n");
109         Tcl_DStringInit (&command);
110         while (fgets (input_buf, 1024, stdin))
111         {
112             count = strlen(input_buf);
113             Tcl_DStringAppend (&command, input_buf, count);
114             if (Tcl_CommandComplete (Tcl_DStringValue (&command)))
115             {
116                 int code = Tcl_Eval (interp, Tcl_DStringValue (&command));
117                 Tcl_DStringFree (&command);
118                 if (code)
119                     printf ("Error: %s\n", interp->result);
120             }
121         }
122         tcl_mainloop (interp, 0);
123     }
124     exit (0);
125 }
126
127 void tcl_mainloop (Tcl_Interp *interp, int interactive)
128 {
129     int i;
130     int res;
131     Tcl_DString command;
132     static fd_set fdset_tcl_r;
133     static fd_set fdset_tcl_w;
134     static fd_set fdset_tcl_x;
135     int min_fd;
136
137     min_fd = interactive ? 3 : 0;
138     if (interactive)
139     {
140         Tcl_DStringInit (&command);
141         printf ("%% "); fflush (stdout);
142     }
143     while (1)
144     {
145         FD_ZERO (&fdset_tcl_r);
146         FD_ZERO (&fdset_tcl_w);
147         FD_ZERO (&fdset_tcl_x);
148         if (interactive)
149             FD_SET (0, &fdset_tcl_r);
150         for (res=0, i=min_fd; i<=max_fd; i++)
151         {
152             if (callback_table[i].w_handle)
153             {
154                 FD_SET (i, &fdset_tcl_w);
155                 res++;
156             }
157             if (callback_table[i].r_handle)
158             {
159                 FD_SET (i, &fdset_tcl_r);
160                 res++;
161             }
162             if (callback_table[i].x_handle)
163             {
164                 FD_SET (i, &fdset_tcl_x);
165                 res++;
166             }
167         }
168         if (!interactive && !res)
169             return;
170         if ((res = select(max_fd+1, &fdset_tcl_r, &fdset_tcl_w, 
171                           &fdset_tcl_x, 0)) < 0)
172         {
173             perror("select");
174             exit(1);
175         }
176         if (!res)
177             continue;
178         for (i=min_fd; i<=max_fd; i++)
179         {
180             if (FD_ISSET (i, &fdset_tcl_r))
181             {
182                 assert (callback_table[i].r_handle);
183                 (*callback_table[i].r_handle) (callback_table[i].obj);
184             }
185             if (FD_ISSET (i, &fdset_tcl_w))
186             {
187                 assert (callback_table[i].w_handle);
188                 (*callback_table[i].w_handle) (callback_table[i].obj);
189             }
190             if (FD_ISSET (i, &fdset_tcl_x))
191             {
192                 assert (callback_table[i].x_handle);
193                 (*callback_table[i].x_handle) (callback_table[i].obj);
194             }
195         }
196         if (interactive && FD_ISSET(0, &fdset_tcl_r))
197         {
198             char input_buf[1024];
199             int count = read (0, input_buf, 1024);
200
201             if (count <= 0)
202                 exit (0);
203             Tcl_DStringAppend (&command, input_buf, count);
204             if (Tcl_CommandComplete (Tcl_DStringValue (&command)))
205             {
206                 int code = Tcl_Eval (interp, Tcl_DStringValue (&command));
207                 Tcl_DStringFree (&command);
208                 if (code)
209                     printf ("Error: %s\n", interp->result);
210                 else
211                     printf ("%s", interp->result);
212                 printf ("%% "); fflush (stdout);
213             }
214         }
215     }
216 }
217
218 void ir_select_add (int fd, void *obj)
219 {
220     callback_table[fd].obj = obj;
221     callback_table[fd].r_handle = ir_select_read;
222     callback_table[fd].w_handle = NULL;
223     callback_table[fd].x_handle = NULL;
224     if (fd > max_fd)
225         max_fd = fd;
226 }
227
228 void ir_select_add_write (int fd, void *obj)
229 {
230     callback_table[fd].w_handle = ir_select_write;
231     if (fd > max_fd)
232         max_fd = fd;
233 }
234
235 void ir_select_remove_write (int fd, void *obj)
236 {
237     callback_table[fd].w_handle = NULL;
238 }
239
240 void ir_select_remove (int fd, void *obj)
241 {
242     callback_table[fd].r_handle = NULL;
243     callback_table[fd].w_handle = NULL;
244     callback_table[fd].x_handle = NULL;
245 }