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