2 * IR toolkit for tcl/tk
4 * See the file LICENSE for details.
5 * Sebastian Hammer, Adam Dickmeiss
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.
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.
16 * Revision 1.14 1995/09/21 13:11:53 adam
17 * Support of dynamic loading.
18 * Test script uses load command if necessary.
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.
24 * Revision 1.12 1995/08/28 11:07:16 adam
27 * Revision 1.11 1995/08/03 13:23:02 adam
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.
36 * Revision 1.9 1995/06/26 10:20:20 adam
37 * ir-tk works like wish.
39 * Revision 1.8 1995/06/21 15:16:44 adam
40 * More work on configuration.
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.
47 * Revision 1.6 1995/05/29 08:44:28 adam
48 * Work on delete of objects.
50 * Revision 1.5 1995/03/20 08:53:30 adam
51 * Event loop in tclmain.c rewritten. New method searchStatus.
53 * Revision 1.4 1995/03/17 07:50:31 adam
54 * Headers have changed a little.
60 #include <sys/types.h>
62 #include <sys/select.h>
70 static char *fileName = NULL;
72 /* select(2) callbacks */
74 void (*r_handle)(ClientData);
75 void (*w_handle)(ClientData);
76 void (*x_handle)(ClientData);
79 #define MAX_CALLBACK 200
81 static struct callback callback_table[MAX_CALLBACK];
82 static int max_fd = 3; /* don't worry: it will grow... */
84 void tcl_mainloop (Tcl_Interp *interp, int interactive);
86 int Tcl_AppInit (Tcl_Interp *interp)
88 if (Tcl_Init(interp) == TCL_ERROR)
90 if (Irtcl_Init(interp) == TCL_ERROR)
95 int main (int argc, char **argv)
101 interp = Tcl_CreateInterp();
102 Tcl_SetVar (interp, "tcl_interactive", "0", TCL_GLOBAL_ONLY);
106 if (Tcl_AppInit(interp) != TCL_OK) {
107 fprintf(stderr, "Tcl_AppInit failed: %s\n", interp->result);
109 for (i=0; i<MAX_CALLBACK; i++)
111 callback_table[i].r_handle = NULL;
112 callback_table[i].w_handle = NULL;
113 callback_table[i].x_handle = NULL;
117 code = Tcl_EvalFile (interp, fileName);
118 if (*interp->result != 0)
119 printf ("%s\n", interp->result);
122 tcl_mainloop (interp, 0);
127 Tcl_SetVar (interp, "tcl_interactive", "1", TCL_GLOBAL_ONLY);
128 tcl_mainloop (interp, 1);
133 char input_buf[1024];
137 Tcl_DStringInit (&command);
138 while (fgets (input_buf, 1024, stdin))
140 count = strlen(input_buf);
141 Tcl_DStringAppend (&command, input_buf, count);
142 if (Tcl_CommandComplete (Tcl_DStringValue (&command)))
144 int code = Tcl_Eval (interp, Tcl_DStringValue (&command));
145 Tcl_DStringFree (&command);
147 printf ("Error: %s\n", interp->result);
150 tcl_mainloop (interp, 0);
155 void tcl_mainloop (Tcl_Interp *interp, int interactive)
160 static fd_set fdset_tcl_r;
161 static fd_set fdset_tcl_w;
162 static fd_set fdset_tcl_x;
165 min_fd = interactive ? 3 : 0;
168 Tcl_DStringInit (&command);
169 printf ("%% "); fflush (stdout);
173 FD_ZERO (&fdset_tcl_r);
174 FD_ZERO (&fdset_tcl_w);
175 FD_ZERO (&fdset_tcl_x);
177 FD_SET (0, &fdset_tcl_r);
178 for (res=0, i=min_fd; i<=max_fd; i++)
180 if (callback_table[i].w_handle)
182 FD_SET (i, &fdset_tcl_w);
185 if (callback_table[i].r_handle)
187 FD_SET (i, &fdset_tcl_r);
190 if (callback_table[i].x_handle)
192 FD_SET (i, &fdset_tcl_x);
196 if (!interactive && !res)
198 if ((res = select(max_fd+1, &fdset_tcl_r, &fdset_tcl_w,
199 &fdset_tcl_x, 0)) < 0)
206 for (i=min_fd; i<=max_fd; i++)
208 if (FD_ISSET (i, &fdset_tcl_r))
210 if (callback_table[i].r_handle)
211 (*callback_table[i].r_handle) (callback_table[i].obj);
213 if (FD_ISSET (i, &fdset_tcl_w))
215 if (callback_table[i].w_handle)
216 (*callback_table[i].w_handle) (callback_table[i].obj);
218 if (FD_ISSET (i, &fdset_tcl_x))
220 if (callback_table[i].x_handle)
221 (*callback_table[i].x_handle) (callback_table[i].obj);
224 if (interactive && FD_ISSET(0, &fdset_tcl_r))
226 char input_buf[1024];
227 int count = read (0, input_buf, 1024);
231 Tcl_DStringAppend (&command, input_buf, count);
232 if (Tcl_CommandComplete (Tcl_DStringValue (&command)))
234 int code = Tcl_Eval (interp, Tcl_DStringValue (&command));
235 Tcl_DStringFree (&command);
237 printf ("Error: %s\n", interp->result);
238 else if (*interp->result)
239 printf ("%s\n", interp->result);
240 printf ("%% "); fflush (stdout);
246 #if IRTCL_GENERIC_FILES
247 void ir_select_add (Tcl_File file, void *obj)
249 int fd = (int) Tcl_GetFileInfo (file, NULL);
251 void ir_select_add (int fd, void *obj)
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;
262 #if IRTCL_GENERIC_FILES
263 void ir_select_add_write (Tcl_File file, void *obj)
265 int fd = (int) Tcl_GetFileInfo (file, NULL);
267 void ir_select_add_write (int fd, void *obj)
270 callback_table[fd].w_handle = ir_select_write;
275 #if IRTCL_GENERIC_FILES
276 void ir_select_remove_write (Tcl_File file, void *obj)
278 int fd = (int) Tcl_GetFileInfo (file, NULL);
280 void ir_select_remove_write (int fd, void *obj)
283 callback_table[fd].w_handle = NULL;
286 #if IRTCL_GENERIC_FILES
287 void ir_select_remove (Tcl_File file, void *obj)
289 int fd = (int) Tcl_GetFileInfo (file, NULL);
291 void ir_select_remove (int fd, void *obj)
294 callback_table[fd].r_handle = NULL;
295 callback_table[fd].w_handle = NULL;
296 callback_table[fd].x_handle = NULL;