Minor.
[ir-tcl-moved-to-github.git] / tclmain.c
1 /*
2  * IR toolkit for tcl/tk
3  * (c) Index Data 1995-1996
4  * See the file LICENSE for details.
5  * Sebastian Hammer, Adam Dickmeiss
6  *
7  * $Log: tclmain.c,v $
8  * Revision 1.20  1997-04-30 07:26:08  adam
9  * Added support for shared libaries (if supported by Tcl itself).
10  *
11  * Revision 1.19  1996/08/20 09:27:49  adam
12  * More work on explain.
13  * Renamed tkinit.c to tkmain.c. The tcl shell uses the Tcl 7.5 interface
14  * for socket i/o instead of the handcrafted one (for Tcl 7.3 and Tcl7.4).
15  *
16  */
17
18 #include <unistd.h>
19 #include <sys/time.h>
20 #include <sys/types.h>
21 #ifdef _AIX
22 #include <sys/select.h>
23 #endif
24
25 #include <assert.h>
26
27 #include <tcl.h>
28 #include <log.h>
29 #include "ir-tcl.h"
30
31 int Tcl_AppInit (Tcl_Interp *interp)
32 {
33     if (Tcl_Init(interp) == TCL_ERROR)
34         return TCL_ERROR;
35     if (Irtcl_Init(interp) == TCL_ERROR)
36         return TCL_ERROR;
37 #if USE_WAIS
38     if (Waistcl_Init(interp) == TCL_ERROR)
39         return TCL_ERROR;
40 #endif
41     return TCL_OK;
42 }
43
44 #if TCL_MAJOR_VERSION > 7 || (TCL_MAJOR_VERSION == 7 && TCL_MINOR_VERSION > 4)
45 /* new version of tcl: version > 7.4 */
46 extern int matherr ();
47 int *tclDummyMathPtr = (int*) matherr;
48
49 int main (int argc, char **argv)
50 {
51     Tcl_Main (argc, argv, Tcl_AppInit);
52     return 0;
53 }
54
55 #else
56 /* old version of tcl: version <= 7.4 */
57
58 static char *fileName = NULL;
59 extern int main ();
60 int *tclDummyMainPtr = (int*) main;
61
62 /* select(2) callbacks */
63 struct callback {
64     void (*handle)(ClientData, int, int, int);
65     int r, w, e;
66     ClientData obj;
67 };
68 #define MAX_CALLBACK 200
69
70 static struct callback callback_table[MAX_CALLBACK];
71 static int max_fd = 3;            /* don't worry: it will grow... */
72
73 void tcl_mainloop (Tcl_Interp *interp, int interactive);
74
75 int main (int argc, char **argv)
76 {
77     Tcl_Interp *interp;
78     int code;
79     int i;
80
81     interp = Tcl_CreateInterp();
82     Tcl_SetVar (interp, "tcl_interactive", "0", TCL_GLOBAL_ONLY);
83     if (argc == 2)
84         fileName = argv[1];
85
86     if (Tcl_AppInit(interp) != TCL_OK) {
87         fprintf(stderr, "Tcl_AppInit failed: %s\n", interp->result);
88     }
89     for (i=0; i<MAX_CALLBACK; i++)
90         callback_table[i].handle = NULL;
91     if (fileName)
92     {
93         code = Tcl_EvalFile (interp, fileName);
94         if (*interp->result != 0)
95             printf ("%s\n", interp->result);
96         if (code != TCL_OK)
97             exit (1);
98         tcl_mainloop (interp, 0);
99     }
100     else if (isatty(0))
101     {
102
103         Tcl_SetVar (interp, "tcl_interactive", "1", TCL_GLOBAL_ONLY);
104         tcl_mainloop (interp, 1);
105     }
106     else
107     {
108         Tcl_DString command;
109         char input_buf[1024];
110         int count;
111
112         printf ("xx\n");
113         Tcl_DStringInit (&command);
114         while (fgets (input_buf, 1024, stdin))
115         {
116             count = strlen(input_buf);
117             Tcl_DStringAppend (&command, input_buf, count);
118             if (Tcl_CommandComplete (Tcl_DStringValue (&command)))
119             {
120                 int code = Tcl_Eval (interp, Tcl_DStringValue (&command));
121                 Tcl_DStringFree (&command);
122                 if (code)
123                     printf ("Error: %s\n", interp->result);
124             }
125         }
126         tcl_mainloop (interp, 0);
127     }
128     exit (0);
129 }
130
131 void tcl_mainloop (Tcl_Interp *interp, int interactive)
132 {
133     int i;
134     int res;
135     Tcl_DString command;
136     static fd_set fdset_tcl_r;
137     static fd_set fdset_tcl_w;
138     static fd_set fdset_tcl_x;
139     int min_fd;
140
141     min_fd = interactive ? 3 : 0;
142     if (interactive)
143     {
144         Tcl_DStringInit (&command);
145         printf ("%% "); fflush (stdout);
146     }
147     while (1)
148     {
149         FD_ZERO (&fdset_tcl_r);
150         FD_ZERO (&fdset_tcl_w);
151         FD_ZERO (&fdset_tcl_x);
152         if (interactive)
153             FD_SET (0, &fdset_tcl_r);
154         for (res=0, i=min_fd; i<=max_fd; i++)
155         {
156             if (callback_table[i].handle && callback_table[i].w)
157             {
158                 FD_SET (i, &fdset_tcl_w);
159                 res++;
160             }
161             if (callback_table[i].handle && callback_table[i].r)
162             {
163                 FD_SET (i, &fdset_tcl_r);
164                 res++;
165             }
166             if (callback_table[i].handle && callback_table[i].e)
167             {
168                 FD_SET (i, &fdset_tcl_x);
169                 res++;
170             }
171         }
172         if (!interactive && !res)
173             return;
174         if ((res = select(max_fd+1, &fdset_tcl_r, &fdset_tcl_w, 
175                           &fdset_tcl_x, 0)) < 0)
176         {
177             perror("select");
178             exit(1);
179         }
180         if (!res)
181             continue;
182         for (i=min_fd; i<=max_fd; i++)
183         {
184             int r_flag = 0;
185             int w_flag = 0;
186             int e_flag = 0;
187
188             if (!callback_table[i].handle)
189                 continue;
190             if (FD_ISSET (i, &fdset_tcl_r) && callback_table[i].r)
191                 r_flag = 1;
192             if (FD_ISSET (i, &fdset_tcl_w) && callback_table[i].w)
193                 w_flag = 1;
194             if (FD_ISSET (i, &fdset_tcl_x) && callback_table[i].e)
195                 e_flag = 1;
196             if (r_flag || w_flag || e_flag)
197                 (*callback_table[i].handle)(callback_table[i].obj,
198                  r_flag, w_flag, e_flag);
199         }
200         if (interactive && FD_ISSET(0, &fdset_tcl_r))
201         {
202             char input_buf[1024];
203             int count = read (0, input_buf, 1024);
204
205             if (count <= 0)
206                 exit (0);
207             Tcl_DStringAppend (&command, input_buf, count);
208             if (Tcl_CommandComplete (Tcl_DStringValue (&command)))
209             {
210                 int code = Tcl_Eval (interp, Tcl_DStringValue (&command));
211                 Tcl_DStringFree (&command);
212                 if (code)
213                     printf ("Error: %s\n", interp->result);
214                 else if (*interp->result)
215                     printf ("%s\n", interp->result);
216                 printf ("%% "); fflush (stdout);
217             }
218         }
219     }
220 }
221
222 void ir_tcl_select_set (void (*f)(ClientData clientData, int r, int w, int e),
223                         int fd, ClientData clientData, int r, int w, int e)
224 {
225     callback_table[fd].handle = f;
226     callback_table[fd].obj = clientData;
227     callback_table[fd].r = r;
228     callback_table[fd].w = w;
229     callback_table[fd].e = e;
230     if (fd > max_fd)
231         max_fd = fd;
232 }
233
234 #endif