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