2 * IR toolkit for tcl/tk
4 * See the file LICENSE for details.
5 * Sebastian Hammer, Adam Dickmeiss
8 * Revision 1.15 1996-01-10 09:18:45 adam
9 * PDU specific callbacks implemented: initRespnse, searchResponse,
10 * presentResponse and scanResponse.
11 * Bug fix in the command line shell (tclmain.c) - discovered on OSF/1.
13 * Revision 1.14 1995/09/21 13:11:53 adam
14 * Support of dynamic loading.
15 * Test script uses load command if necessary.
17 * Revision 1.13 1995/08/28 12:21:22 adam
18 * Removed lines and list as synonyms of list in MARC extractron.
19 * Configure searches also for tk4.0 / tcl7.4.
21 * Revision 1.12 1995/08/28 11:07:16 adam
24 * Revision 1.11 1995/08/03 13:23:02 adam
27 * Revision 1.10 1995/06/30 12:39:28 adam
28 * Bug fix: loadFile didn't set record type.
29 * The MARC routines are a little less strict in the interpretation.
30 * Script display.tcl replaces the old marc.tcl.
31 * New interactive script: shell.tcl.
33 * Revision 1.9 1995/06/26 10:20:20 adam
34 * ir-tk works like wish.
36 * Revision 1.8 1995/06/21 15:16:44 adam
37 * More work on configuration.
39 * Revision 1.7 1995/06/21 11:04:54 adam
40 * Uses GNU autoconf 2.3.
41 * Install procedure implemented.
42 * boook bitmaps moved to sub directory bitmaps.
44 * Revision 1.6 1995/05/29 08:44:28 adam
45 * Work on delete of objects.
47 * Revision 1.5 1995/03/20 08:53:30 adam
48 * Event loop in tclmain.c rewritten. New method searchStatus.
50 * Revision 1.4 1995/03/17 07:50:31 adam
51 * Headers have changed a little.
57 #include <sys/types.h>
59 #include <sys/select.h>
67 static char *fileName = NULL;
69 /* select(2) callbacks */
71 void (*r_handle)(ClientData);
72 void (*w_handle)(ClientData);
73 void (*x_handle)(ClientData);
76 #define MAX_CALLBACK 200
78 static struct callback callback_table[MAX_CALLBACK];
79 static int max_fd = 3; /* don't worry: it will grow... */
81 void tcl_mainloop (Tcl_Interp *interp, int interactive);
83 int Tcl_AppInit (Tcl_Interp *interp)
85 if (Tcl_Init(interp) == TCL_ERROR)
87 if (Irtcl_Init(interp) == TCL_ERROR)
92 int main (int argc, char **argv)
98 interp = Tcl_CreateInterp();
99 Tcl_SetVar (interp, "tcl_interactive", "0", TCL_GLOBAL_ONLY);
103 if (Tcl_AppInit(interp) != TCL_OK) {
104 fprintf(stderr, "Tcl_AppInit failed: %s\n", interp->result);
106 for (i=0; i<MAX_CALLBACK; i++)
108 callback_table[i].r_handle = NULL;
109 callback_table[i].w_handle = NULL;
110 callback_table[i].x_handle = NULL;
114 code = Tcl_EvalFile (interp, fileName);
115 if (*interp->result != 0)
116 printf ("%s\n", interp->result);
119 tcl_mainloop (interp, 0);
124 Tcl_SetVar (interp, "tcl_interactive", "1", TCL_GLOBAL_ONLY);
125 tcl_mainloop (interp, 1);
130 char input_buf[1024];
134 Tcl_DStringInit (&command);
135 while (fgets (input_buf, 1024, stdin))
137 count = strlen(input_buf);
138 Tcl_DStringAppend (&command, input_buf, count);
139 if (Tcl_CommandComplete (Tcl_DStringValue (&command)))
141 int code = Tcl_Eval (interp, Tcl_DStringValue (&command));
142 Tcl_DStringFree (&command);
144 printf ("Error: %s\n", interp->result);
147 tcl_mainloop (interp, 0);
152 void tcl_mainloop (Tcl_Interp *interp, int interactive)
157 static fd_set fdset_tcl_r;
158 static fd_set fdset_tcl_w;
159 static fd_set fdset_tcl_x;
162 min_fd = interactive ? 3 : 0;
165 Tcl_DStringInit (&command);
166 printf ("%% "); fflush (stdout);
170 FD_ZERO (&fdset_tcl_r);
171 FD_ZERO (&fdset_tcl_w);
172 FD_ZERO (&fdset_tcl_x);
174 FD_SET (0, &fdset_tcl_r);
175 for (res=0, i=min_fd; i<=max_fd; i++)
177 if (callback_table[i].w_handle)
179 FD_SET (i, &fdset_tcl_w);
182 if (callback_table[i].r_handle)
184 FD_SET (i, &fdset_tcl_r);
187 if (callback_table[i].x_handle)
189 FD_SET (i, &fdset_tcl_x);
193 if (!interactive && !res)
195 if ((res = select(max_fd+1, &fdset_tcl_r, &fdset_tcl_w,
196 &fdset_tcl_x, 0)) < 0)
203 for (i=min_fd; i<=max_fd; i++)
205 if (FD_ISSET (i, &fdset_tcl_r))
207 if (callback_table[i].r_handle)
208 (*callback_table[i].r_handle) (callback_table[i].obj);
210 if (FD_ISSET (i, &fdset_tcl_w))
212 if (callback_table[i].w_handle)
213 (*callback_table[i].w_handle) (callback_table[i].obj);
215 if (FD_ISSET (i, &fdset_tcl_x))
217 if (callback_table[i].x_handle)
218 (*callback_table[i].x_handle) (callback_table[i].obj);
221 if (interactive && FD_ISSET(0, &fdset_tcl_r))
223 char input_buf[1024];
224 int count = read (0, input_buf, 1024);
228 Tcl_DStringAppend (&command, input_buf, count);
229 if (Tcl_CommandComplete (Tcl_DStringValue (&command)))
231 int code = Tcl_Eval (interp, Tcl_DStringValue (&command));
232 Tcl_DStringFree (&command);
234 printf ("Error: %s\n", interp->result);
235 else if (*interp->result)
236 printf ("%s\n", interp->result);
237 printf ("%% "); fflush (stdout);
243 void ir_select_add (int fd, void *obj)
245 callback_table[fd].obj = obj;
246 callback_table[fd].r_handle = ir_select_read;
247 callback_table[fd].w_handle = NULL;
248 callback_table[fd].x_handle = NULL;
253 void ir_select_add_write (int fd, void *obj)
255 callback_table[fd].w_handle = ir_select_write;
260 void ir_select_remove_write (int fd, void *obj)
262 callback_table[fd].w_handle = NULL;
265 void ir_select_remove (int fd, void *obj)
267 callback_table[fd].r_handle = NULL;
268 callback_table[fd].w_handle = NULL;
269 callback_table[fd].x_handle = NULL;