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