2 * IR toolkit for tcl/tk
4 * See the file LICENSE for details.
5 * Sebastian Hammer, Adam Dickmeiss
8 * Revision 1.11 1995-08-03 13:23:02 adam
11 * Revision 1.10 1995/06/30 12:39:28 adam
12 * Bug fix: loadFile didn't set record type.
13 * The MARC routines are a little less strict in the interpretation.
14 * Script display.tcl replaces the old marc.tcl.
15 * New interactive script: shell.tcl.
17 * Revision 1.9 1995/06/26 10:20:20 adam
18 * ir-tk works like wish.
20 * Revision 1.8 1995/06/21 15:16:44 adam
21 * More work on configuration.
23 * Revision 1.7 1995/06/21 11:04:54 adam
24 * Uses GNU autoconf 2.3.
25 * Install procedure implemented.
26 * boook bitmaps moved to sub directory bitmaps.
28 * Revision 1.6 1995/05/29 08:44:28 adam
29 * Work on delete of objects.
31 * Revision 1.5 1995/03/20 08:53:30 adam
32 * Event loop in tclmain.c rewritten. New method searchStatus.
34 * Revision 1.4 1995/03/17 07:50:31 adam
35 * Headers have changed a little.
41 #include <sys/types.h>
43 #include <sys/select.h>
51 static char *fileName = NULL;
53 /* select(2) callbacks */
55 void (*r_handle)(void *p);
56 void (*w_handle)(void *p);
57 void (*x_handle)(void *p);
60 #define MAX_CALLBACK 200
62 static struct callback callback_table[MAX_CALLBACK];
63 static int max_fd = 3; /* don't worry: it will grow... */
65 void tcl_mainloop (Tcl_Interp *interp, int interactive);
67 int Tcl_AppInit (Tcl_Interp *interp)
69 if (Tcl_Init(interp) == TCL_ERROR)
71 if (ir_tcl_init(interp) == TCL_ERROR)
76 int main (int argc, char **argv)
82 interp = Tcl_CreateInterp();
83 Tcl_SetVar (interp, "tcl_interactive", "0", TCL_GLOBAL_ONLY);
87 log_init (LOG_ALL, "", NULL);
88 if (Tcl_AppInit(interp) != TCL_OK) {
89 fprintf(stderr, "Tcl_AppInit failed: %s\n", interp->result);
91 for (i=0; i<MAX_CALLBACK; i++)
93 callback_table[i].r_handle = NULL;
94 callback_table[i].w_handle = NULL;
95 callback_table[i].x_handle = NULL;
99 code = Tcl_EvalFile (interp, fileName);
100 if (*interp->result != 0)
101 printf ("%s\n", interp->result);
104 tcl_mainloop (interp, 0);
109 Tcl_SetVar (interp, "tcl_interactive", "1", TCL_GLOBAL_ONLY);
110 tcl_mainloop (interp, 1);
115 char input_buf[1024];
119 Tcl_DStringInit (&command);
120 while (fgets (input_buf, 1024, stdin))
122 count = strlen(input_buf);
123 Tcl_DStringAppend (&command, input_buf, count);
124 if (Tcl_CommandComplete (Tcl_DStringValue (&command)))
126 int code = Tcl_Eval (interp, Tcl_DStringValue (&command));
127 Tcl_DStringFree (&command);
129 printf ("Error: %s\n", interp->result);
132 tcl_mainloop (interp, 0);
137 void tcl_mainloop (Tcl_Interp *interp, int interactive)
142 static fd_set fdset_tcl_r;
143 static fd_set fdset_tcl_w;
144 static fd_set fdset_tcl_x;
147 min_fd = interactive ? 3 : 0;
150 Tcl_DStringInit (&command);
151 printf ("%% "); fflush (stdout);
155 FD_ZERO (&fdset_tcl_r);
156 FD_ZERO (&fdset_tcl_w);
157 FD_ZERO (&fdset_tcl_x);
159 FD_SET (0, &fdset_tcl_r);
160 for (res=0, i=min_fd; i<=max_fd; i++)
162 if (callback_table[i].w_handle)
164 FD_SET (i, &fdset_tcl_w);
167 if (callback_table[i].r_handle)
169 FD_SET (i, &fdset_tcl_r);
172 if (callback_table[i].x_handle)
174 FD_SET (i, &fdset_tcl_x);
178 if (!interactive && !res)
180 if ((res = select(max_fd+1, &fdset_tcl_r, &fdset_tcl_w,
181 &fdset_tcl_x, 0)) < 0)
188 for (i=min_fd; i<=max_fd; i++)
190 if (FD_ISSET (i, &fdset_tcl_r))
192 assert (callback_table[i].r_handle);
193 (*callback_table[i].r_handle) (callback_table[i].obj);
195 if (FD_ISSET (i, &fdset_tcl_w))
197 assert (callback_table[i].w_handle);
198 (*callback_table[i].w_handle) (callback_table[i].obj);
200 if (FD_ISSET (i, &fdset_tcl_x))
202 assert (callback_table[i].x_handle);
203 (*callback_table[i].x_handle) (callback_table[i].obj);
206 if (interactive && FD_ISSET(0, &fdset_tcl_r))
208 char input_buf[1024];
209 int count = read (0, input_buf, 1024);
213 Tcl_DStringAppend (&command, input_buf, count);
214 if (Tcl_CommandComplete (Tcl_DStringValue (&command)))
216 int code = Tcl_Eval (interp, Tcl_DStringValue (&command));
217 Tcl_DStringFree (&command);
219 printf ("Error: %s\n", interp->result);
220 else if (*interp->result)
221 printf ("%s\n", interp->result);
222 printf ("%% "); fflush (stdout);
228 void ir_select_add (int fd, void *obj)
230 callback_table[fd].obj = obj;
231 callback_table[fd].r_handle = ir_select_read;
232 callback_table[fd].w_handle = NULL;
233 callback_table[fd].x_handle = NULL;
238 void ir_select_add_write (int fd, void *obj)
240 callback_table[fd].w_handle = ir_select_write;
245 void ir_select_remove_write (int fd, void *obj)
247 callback_table[fd].w_handle = NULL;
250 void ir_select_remove (int fd, void *obj)
252 callback_table[fd].r_handle = NULL;
253 callback_table[fd].w_handle = NULL;
254 callback_table[fd].x_handle = NULL;