2 * IR toolkit for tcl/tk
3 * (c) Index Data 1995-2001
4 * See the file LICENSE for details.
7 * Revision 1.119 2001-12-03 00:31:06 adam
8 * Towards 1.4. Configure updates.
10 * Revision 1.118 2001/03/27 16:27:21 adam
11 * Fixed bug in do_responseStatus.
13 * Revision 1.117 2001/03/26 11:39:34 adam
14 * Fixed bug in ir_deleteDiags - crash when receiving multiple diags.
16 * Revision 1.116 2001/02/09 11:58:04 adam
17 * Updated for Tcl8.1 and higher where internal encoding is UTF-8.
19 * Revision 1.115 2000/09/13 12:18:49 adam
20 * Logging utility patch (YAZ version 1.7).
22 * Revision 1.114 1999/05/17 20:37:41 adam
23 * Fixed problem with ASN code.
25 * Revision 1.113 1999/04/20 10:01:46 adam
26 * Modified calls to ODR encoders/decoders (name argument).
28 * Revision 1.112 1999/03/22 06:51:34 adam
31 * Revision 1.111 1999/02/11 11:30:09 adam
34 * Revision 1.110 1998/10/20 15:15:31 adam
35 * Changed scan response handler.
37 * Revision 1.109 1998/10/13 21:23:26 adam
38 * Fixed searchStatus method.
40 * Revision 1.108 1998/10/12 11:48:08 adam
41 * Removed printf call.
43 * Revision 1.107 1998/06/10 13:00:46 adam
44 * Added ir-version command.
46 * Revision 1.106 1998/05/20 12:25:35 adam
47 * Fixed bug that occurred in rare cases when encoding of incoming
50 * Revision 1.105 1998/04/02 14:31:08 adam
51 * This version works with compiled ASN.1 code.
53 * Revision 1.104 1998/02/27 14:26:07 adam
54 * Changed client so that it still works if target sets numberOfRecords
55 * in response to an illegal value.
57 * Revision 1.103 1997/11/19 11:22:10 adam
58 * Object identifiers can be accessed in GRS-1 records.
60 * Revision 1.102 1997/09/17 12:22:40 adam
61 * Changed to use YAZ version 1.4. The new comstack utility, cs_straddr,
64 * Revision 1.101 1997/09/09 10:19:53 adam
65 * New MSV5.0 port with fewer warnings.
67 * Revision 1.100 1997/05/01 15:04:05 adam
68 * Added ir-log command.
70 * Revision 1.99 1997/04/30 07:24:47 adam
71 * Spell fix of an error message.
73 * Revision 1.98 1997/04/13 18:57:20 adam
74 * Better error reporting and aligned with Tcl/Tk style.
75 * Rework of notifier code with Tcl_File handles.
77 * Revision 1.97 1996/11/14 17:11:07 adam
78 * Added Explain documentaion.
80 * Revision 1.96 1996/10/08 13:02:50 adam
81 * When dealing with records, odr_choice_enable_bias function is used to
82 * prevent decoding of externals.
84 * Revision 1.95 1996/09/13 10:51:49 adam
85 * Bug fix: ir_tcl_select_set called Tcl_GetFile at disconnect.
87 * Revision 1.94 1996/08/21 13:32:53 adam
88 * Implemented saveFile method and extended loadFile method to work with it.
90 * Revision 1.93 1996/08/16 15:07:45 adam
91 * First work on Explain.
93 * Revision 1.92 1996/08/09 15:33:07 adam
94 * Modified the code to use tk4.1/tcl7.5 patch level 1. The time-driven
95 * polling is no longer activated on Windows since asynchrounous I/O works
98 * Revision 1.91 1996/07/03 13:31:11 adam
99 * The xmalloc/xfree functions from YAZ are used to manage memory.
101 * Revision 1.90 1996/06/27 14:21:00 adam
102 * Yet another Windows port.
104 * Revision 1.89 1996/06/11 15:27:15 adam
105 * Event type set to connect a little earlier in the do_connect function.
107 * Revision 1.88 1996/06/03 09:04:22 adam
108 * Changed a few logf calls.
110 * Revision 1.87 1996/05/29 06:37:51 adam
111 * Function ir_tcl_get_grs_r enhanced so that specific elements can be
114 * Revision 1.86 1996/03/20 13:54:04 adam
115 * The Tcl_File structure is only manipulated in the Tk-event interface
118 * Revision 1.85 1996/03/15 11:15:48 adam
119 * Modified to use new prototypes for p_query_rpn and p_query_scan.
121 * Revision 1.84 1996/03/07 12:42:49 adam
122 * Better logging when callback is invoked.
124 * Revision 1.83 1996/03/05 09:21:09 adam
125 * Bug fix: memory used by GRS records wasn't freed.
126 * Rewrote some of the error handling code - the connection is always
127 * closed before failback is called.
128 * If failback is defined the send APDU methods (init, search, ...) will
129 * return OK but invoke failback (as is the case if the write operation
131 * Bug fix: ref_count in assoc object could grow if fraction of PDU was
134 * Revision 1.82 1996/02/29 15:30:21 adam
135 * Export of IrTcl functionality to extensions.
137 * Revision 1.81 1996/02/26 18:38:32 adam
138 * Work on export of set methods.
140 * Revision 1.80 1996/02/23 17:31:39 adam
141 * More functions made available to the wais tcl extension.
143 * Revision 1.79 1996/02/23 13:41:38 adam
144 * Work on public access to simple ir class system.
146 * Revision 1.78 1996/02/21 10:16:08 adam
147 * Simplified select handling. Only one function ir_tcl_select_set has
148 * to be externally defined.
150 * Revision 1.77 1996/02/20 17:52:58 adam
151 * Uses the YAZ oid system to name record syntax object identifiers.
153 * Revision 1.76 1996/02/20 16:09:51 adam
154 * Bug fix: didn't set element set names stamp correctly on result
155 * set records when element set names were set to the empty string.
157 * Revision 1.75 1996/02/19 15:41:53 adam
158 * Better log messages.
159 * Minor improvement of connect method.
161 * Revision 1.74 1996/02/05 17:58:03 adam
162 * Ported ir-tcl to use the beta releases of tcl7.5/tk4.1.
164 * Revision 1.73 1996/01/29 11:35:19 adam
165 * Bug fix: cs_type member renamed to comstackType to avoid conflict with
166 * cs_type macro defined by YAZ.
168 * Revision 1.72 1996/01/19 17:45:34 quinn
169 * Added debugging output
171 * Revision 1.71 1996/01/19 16:22:38 adam
172 * New method: apduDump - returns information about last incoming APDU.
174 * Revision 1.70 1996/01/10 09:18:34 adam
175 * PDU specific callbacks implemented: initRespnse, searchResponse,
176 * presentResponse and scanResponse.
177 * Bug fix in the command line shell (tclmain.c) - discovered on OSF/1.
179 * Revision 1.69 1996/01/04 16:12:12 adam
180 * Setting PDUType renamed to eventType.
182 * Revision 1.68 1996/01/04 11:05:22 adam
183 * New setting: PDUType - returns type of last PDU returned from the target.
184 * Fixed a bug in configure/Makefile.
186 * Revision 1.67 1996/01/03 09:00:51 adam
187 * Updated to use new version of Yaz (names changed to avoid C++ conflict).
189 * Revision 1.66 1995/11/28 17:26:39 adam
190 * Removed Carriage return from ir-tcl.c!
191 * Removed misc. debug logs.
193 * Revision 1.65 1995/11/28 13:53:00 quinn
196 * Revision 1.64 1995/11/13 15:39:18 adam
197 * Bug fix: {small,medium}SetElementSetNames weren't set correctly.
198 * Bug fix: idAuthentication weren't set correctly.
200 * Revision 1.63 1995/11/13 09:55:39 adam
201 * Multiple records at a position in a result-set with differnt
204 * Revision 1.62 1995/10/18 17:20:33 adam
205 * Work on target setup in client.tcl.
207 * Revision 1.61 1995/10/18 16:42:42 adam
208 * New settings: smallSetElementSetNames and mediumSetElementSetNames.
210 * Revision 1.60 1995/10/18 15:43:31 adam
211 * In search: mediumSetElementSetNames and smallSetElementSetNames are
212 * set to elementSetNames.
214 * Revision 1.59 1995/10/17 12:18:58 adam
215 * Bug fix: when target connection closed, the connection was not
216 * properly reestablished.
218 * Revision 1.58 1995/10/16 17:00:55 adam
219 * New setting: elementSetNames.
220 * Various client improvements. Medium presentation format looks better.
222 * Revision 1.57 1995/09/21 13:11:51 adam
223 * Support of dynamic loading.
224 * Test script uses load command if necessary.
226 * Revision 1.56 1995/08/29 15:30:14 adam
227 * Work on GRS records.
229 * Revision 1.55 1995/08/28 09:43:25 adam
230 * Minor changes. configure only searches for yaz beta 3 and versions after
233 * Revision 1.54 1995/08/24 12:25:16 adam
234 * Modified to work with yaz 1.0b3.
236 * Revision 1.53 1995/08/04 12:49:26 adam
237 * Bug fix: reading uninitialized variable p.
239 * Revision 1.52 1995/08/04 11:32:38 adam
240 * More work on output queue. Memory related routines moved
243 * Revision 1.51 1995/08/03 13:22:54 adam
246 * Revision 1.50 1995/07/20 08:09:49 adam
247 * client.tcl: Targets removed from hotTargets list when targets
248 * are removed/modified.
249 * ir-tcl.c: More work on triggerResourceControl.
251 * Revision 1.49 1995/06/30 12:39:21 adam
252 * Bug fix: loadFile didn't set record type.
253 * The MARC routines are a little less strict in the interpretation.
254 * Script display.tcl replaces the old marc.tcl.
255 * New interactive script: shell.tcl.
257 * Revision 1.48 1995/06/27 19:03:50 adam
258 * Bug fix in do_present in ir-tcl.c: p->set_child member weren't set.
259 * nextResultSetPosition used instead of setOffset.
261 * Revision 1.47 1995/06/25 10:25:04 adam
262 * Working on triggerResourceControl. Description of compile/install
263 * procedure moved to ir-tcl.sgml.
265 * Revision 1.46 1995/06/22 13:15:06 adam
266 * Feature: SUTRS. Setting getSutrs implemented.
267 * Work on display formats.
268 * Preferred record syntax can be set by the user.
270 * Revision 1.45 1995/06/20 08:07:30 adam
271 * New setting: failInfo.
272 * Working on better cancel mechanism.
274 * Revision 1.44 1995/06/19 17:01:20 adam
277 * Revision 1.43 1995/06/19 13:06:08 adam
278 * New define: IR_TCL_VERSION.
280 * Revision 1.42 1995/06/19 08:08:52 adam
281 * client.tcl: hotTargets now contain both database and target name.
282 * ir-tcl.c: setting protocol edited. Errors in callbacks are logged
283 * by logf(LOG_WARN, ...) calls.
285 * Revision 1.41 1995/06/16 12:28:16 adam
286 * Implemented preferredRecordSyntax.
287 * Minor changes in diagnostic handling.
288 * Record list deleted when connection closes.
290 * Revision 1.40 1995/06/14 13:37:18 adam
291 * Setting recordType implemented.
292 * Setting implementationVersion implemented.
293 * Settings implementationId / implementationName edited.
295 * Revision 1.39 1995/06/08 10:26:32 adam
296 * Bug fix in ir_strdup.
298 * Revision 1.38 1995/06/01 16:36:47 adam
299 * About buttons. Minor bug fixes.
301 * Revision 1.37 1995/06/01 07:31:20 adam
302 * Rename of many typedefs -> IrTcl_...
304 * Revision 1.36 1995/05/31 13:09:59 adam
305 * Client searches/presents may be interrupted.
306 * New moving book-logo.
308 * Revision 1.35 1995/05/31 08:36:33 adam
309 * Bug fix in client.tcl: didn't save options on clientrc.tcl.
310 * New method: referenceId. More work on scan.
312 * Revision 1.34 1995/05/29 10:33:42 adam
313 * README and rename of startup script.
315 * Revision 1.33 1995/05/29 09:15:11 quinn
316 * Changed CS_SR to PROTO_SR, etc.
318 * Revision 1.32 1995/05/29 08:44:16 adam
319 * Work on delete of objects.
321 * Revision 1.31 1995/05/26 11:44:10 adam
322 * Bugs fixed. More work on MARC utilities and queries. Test
323 * client is up-to-date again.
325 * Revision 1.30 1995/05/26 08:54:11 adam
326 * New MARC utilities. Uses prefix query.
328 * Revision 1.29 1995/05/24 14:10:22 adam
329 * Work on idAuthentication, protocolVersion and options.
331 * Revision 1.28 1995/05/23 15:34:48 adam
332 * Many new settings, userInformationField, smallSetUpperBound, etc.
333 * A number of settings are inherited when ir-set is executed.
334 * This version is incompatible with the graphical test client (client.tcl).
336 * Revision 1.27 1995/05/11 15:34:47 adam
337 * Scan request changed a bit. This version works with RLG.
339 * Revision 1.26 1995/04/18 16:11:51 adam
340 * First version of graphical Scan. Some work on query-by-form.
342 * Revision 1.25 1995/04/17 09:37:17 adam
343 * Further development of scan.
345 * Revision 1.24 1995/04/11 14:16:42 adam
346 * Further work on scan. Response works. Entries aren't saved yet.
348 * Revision 1.23 1995/04/10 10:50:27 adam
349 * Result-set name defaults to suffix of ir-set name.
350 * Started working on scan. Not finished at this point.
352 * Revision 1.22 1995/03/31 10:43:03 adam
353 * More robust when getting bad MARC records.
355 * Revision 1.21 1995/03/31 08:56:37 adam
356 * New button "Search".
358 * Revision 1.20 1995/03/29 16:07:09 adam
359 * Bug fix: Didn't use setName in present request.
361 * Revision 1.19 1995/03/28 12:45:23 adam
362 * New ir method failback: called on disconnect/protocol error.
363 * New ir set/get method: protocol: SR / Z3950.
364 * Simple popup and disconnect when failback is invoked.
366 * Revision 1.18 1995/03/21 15:50:12 adam
369 * Revision 1.17 1995/03/21 13:41:03 adam
370 * Comstack cs_create not used too often. Non-blocking connect.
372 * Revision 1.16 1995/03/21 08:26:06 adam
373 * New method, setName, to specify the result set name (other than Default).
374 * New method, responseStatus, which returns diagnostic info, if any, after
375 * present response / search response.
377 * Revision 1.15 1995/03/20 15:24:07 adam
378 * Diagnostic records saved on searchResponse.
380 * Revision 1.14 1995/03/20 08:53:22 adam
381 * Event loop in tclmain.c rewritten. New method searchStatus.
383 * Revision 1.13 1995/03/17 18:26:17 adam
384 * Non-blocking i/o used now. Database names popup as cascade items.
386 * Revision 1.12 1995/03/17 15:45:00 adam
387 * Improved target/database setup.
389 * Revision 1.11 1995/03/16 17:54:03 adam
390 * Minor changes really.
392 * Revision 1.10 1995/03/15 16:14:50 adam
393 * Blocking arg in cs_create changed.
395 * Revision 1.9 1995/03/15 13:59:24 adam
398 * Revision 1.8 1995/03/15 08:25:16 adam
399 * New method presentStatus to check for error on present. Misc. cleanup
400 * of IrTcl_RecordList manipulations. Full MARC record presentation in
403 * Revision 1.7 1995/03/14 17:32:29 adam
404 * Presentation of full Marc record in popup window.
406 * Revision 1.6 1995/03/12 19:31:55 adam
407 * Pattern matching implemented when retrieving MARC records. More
408 * diagnostic functions.
410 * Revision 1.5 1995/03/10 18:00:15 adam
411 * Actual presentation in line-by-line format. RPN query support.
413 * Revision 1.4 1995/03/09 16:15:08 adam
414 * First presentRequest attempts. Hot-target list.
432 #if defined(__WIN32__)
433 # define WIN32_LEAN_AND_MEAN
434 # include <windows.h>
435 # undef WIN32_LEAN_AND_MEAN
438 * VC++ has an alternate entry point called DllMain, so we need to rename
442 # if defined(_MSC_VER)
443 # define EXPORT(a,b) __declspec(dllexport) a b
444 # define DllEntryPoint DllMain
446 # if defined(__BORLANDC__)
447 # define EXPORT(a,b) a _export b
449 # define EXPORT(a,b) a b
453 # define EXPORT(a,b) a b
456 static char *wrongArgs = "wrong # args: should be \"";
458 static int ir_tcl_error_exec (Tcl_Interp *interp, int argc, char **argv)
461 Tcl_AppendResult (interp, " while executing ", NULL);
462 for (i = 0; i<argc; i++)
463 Tcl_AppendResult (interp, (i ? " " : "\""), argv[i], NULL);
464 Tcl_AppendResult (interp, "\"", NULL);
469 static void ir_deleteDiags (IrTcl_Diagnostic **dst_list, int *dst_num);
471 static void ir_select_notify (ClientData clientData, int r, int w, int e);
473 void ir_select_add (int fd, void *obj)
475 ir_tcl_select_set (ir_select_notify, fd, obj, 1, 0, 0);
478 void ir_select_add_write (int fd, void *obj)
480 ir_tcl_select_set (ir_select_notify, fd, obj, 1, 1, 0);
483 void ir_select_remove (int fd, void *obj)
485 ir_tcl_select_set (NULL, fd, obj, 0, 0, 0);
488 void ir_select_remove_write (int fd, void *obj)
490 ir_tcl_select_set (ir_select_notify, fd, obj, 1, 0, 0);
493 static void delete_IR_record (IrTcl_RecordList *rl)
497 case Z_NamePlusRecord_databaseRecord:
498 switch (rl->u.dbrec.type)
501 ir_tcl_grs_del (&rl->u.dbrec.u.grs1);
506 xfree (rl->u.dbrec.buf);
507 rl->u.dbrec.buf = NULL;
509 case Z_NamePlusRecord_surrogateDiagnostic:
510 ir_deleteDiags (&rl->u.surrogateDiagnostics.list,
511 &rl->u.surrogateDiagnostics.num);
514 xfree (rl->elements);
517 static void purge_IR_records (IrTcl_SetObj *setobj)
519 IrTcl_RecordList *rl;
520 while ((rl = setobj->record_list))
522 setobj->record_list = rl->next;
523 delete_IR_record (rl);
528 static IrTcl_RecordList *new_IR_record (IrTcl_SetObj *setobj,
530 const char *elements)
532 IrTcl_RecordList *rl;
534 if (elements && !*elements)
536 for (rl = setobj->record_list; rl; rl = rl->next)
538 if (no == rl->no && (!rl->elements || !elements ||
539 !strcmp(elements, rl->elements)))
541 delete_IR_record (rl);
547 rl = ir_tcl_malloc (sizeof(*rl));
548 rl->next = setobj->record_list;
550 setobj->record_list = rl;
553 ir_tcl_strdup (NULL, &rl->elements, elements);
560 int ir_tcl_eval (Tcl_Interp *interp, const char *command)
562 char *tmp = ir_tcl_malloc (strlen(command)+1);
565 logf (LOG_DEBUG, "Invoking %.23s ...", command);
566 strcpy (tmp, command);
567 r = Tcl_Eval (interp, tmp);
570 const char *errorInfo = Tcl_GetVar (interp, "errorInfo", 0);
571 logf (LOG_WARN, "Tcl error in line %d: %s\n%s", interp->errorLine,
572 interp->result, errorInfo ? errorInfo : "<null>");
574 Tcl_FreeResult (interp);
580 * IrTcl_getRecordSyntaxStr: Return record syntax name of object id
582 static char *IrTcl_getRecordSyntaxStr (enum oid_value value)
585 struct oident ent, *entp;
587 ent.proto = PROTO_Z3950;
588 ent.oclass = CLASS_RECSYN;
591 o = oid_getoidbyent (&ent);
592 entp = oid_getentbyoid (o);
600 * IrTcl_getRecordSyntaxVal: Return record syntax value of string
602 static enum oid_value IrTcl_getRecordSyntaxVal (const char *name)
604 return oid_getvalbyname (name);
607 static IrTcl_RecordList *find_IR_record (IrTcl_SetObj *setobj, int no)
609 IrTcl_RecordList *rl;
611 for (rl = setobj->record_list; rl; rl = rl->next)
613 (!setobj->recordElements || !rl->elements ||
614 !strcmp (setobj->recordElements, rl->elements)))
619 static void delete_IR_records (IrTcl_SetObj *setobj)
621 IrTcl_RecordList *rl, *rl1;
623 for (rl = setobj->record_list; rl; rl = rl1)
625 delete_IR_record (rl);
629 setobj->record_list = NULL;
633 * ir_tcl_get_set_int: Set/get integer value
635 int ir_tcl_get_set_int (int *val, Tcl_Interp *interp, int argc, char **argv)
641 if (Tcl_GetInt (interp, argv[2], val)==TCL_ERROR)
644 sprintf (buf, "%d", *val);
645 Tcl_AppendResult (interp, buf, NULL);
651 * ir_tcl_method_error
653 int ir_tcl_method_error (Tcl_Interp *interp, int argc, char **argv,
656 IrTcl_Methods *tab_i = tab;
659 Tcl_AppendResult (interp, "bad method: \"", *argv, " ", argv[1],
660 "\"\nmethod should be of:", NULL);
661 for (tab_i = tab; tab_i->tab; tab_i++)
662 for (t = tab_i->tab; t->name; t++)
663 Tcl_AppendResult (interp, " ", t->name, NULL);
668 * ir_tcl_method: Search for method in table and invoke method handler
670 int ir_tcl_method (Tcl_Interp *interp, int argc, char **argv,
671 IrTcl_Methods *tab, int *ret)
673 IrTcl_Methods *tab_i = tab;
676 for (tab_i = tab; tab_i->tab; tab_i++)
677 for (t = tab_i->tab; t->name; t++)
680 if ((*t->method)(tab_i->obj, interp, argc, argv) == TCL_ERROR)
684 if (!strcmp (t->name, argv[1]))
686 *ret = (*t->method)(tab_i->obj, interp, argc, argv);
697 * ir_tcl_named_bits: get/set named bits
699 int ir_tcl_named_bits (struct ir_named_entry *tab, Odr_bitmask *ob,
700 Tcl_Interp *interp, int argc, char **argv)
702 struct ir_named_entry *ti;
707 for (no = 0; no < argc; no++)
710 for (ti = tab; ti->name; ti++)
711 if (!strcmp(argv[no], "@all") || !strcmp (argv[no], ti->name))
713 ODR_MASK_SET (ob, ti->pos);
718 Tcl_AppendResult (interp, "bad bit mask ", argv[no], NULL);
719 return ir_tcl_error_exec (interp, argc, argv);
724 for (ti = tab; ti->name; ti++)
725 if (ODR_MASK_GET (ob, ti->pos))
726 Tcl_AppendElement (interp, ti->name);
730 static void set_referenceId (ODR o, Z_ReferenceId **dst, const char *src)
736 *dst = odr_malloc (o, sizeof(**dst));
737 (*dst)->size = (*dst)->len = strlen(src);
738 (*dst)->buf = odr_malloc (o, (*dst)->len);
739 memcpy ((*dst)->buf, src, (*dst)->len);
743 static void get_referenceId (char **dst, Z_ReferenceId *src)
751 *dst = ir_tcl_malloc (src->len+1);
752 memcpy (*dst, src->buf, src->len);
753 (*dst)[src->len] = '\0';
756 /* ------------------------------------------------------- */
759 * do_init_request: init method on IR object
761 static int do_init_request (void *obj, Tcl_Interp *interp,
762 int argc, char **argv)
770 logf (LOG_DEBUG, "init %s", *argv);
773 Tcl_AppendResult (interp, "not connected", NULL);
774 return ir_tcl_error_exec (interp, argc, argv);
776 apdu = zget_APDU (p->odr_out, Z_APDU_initRequest);
777 req = apdu->u.initRequest;
779 set_referenceId (p->odr_out, &req->referenceId, p->set_inher.referenceId);
780 req->options = &p->options;
781 req->protocolVersion = &p->protocolVersion;
782 req->preferredMessageSize = &p->preferredMessageSize;
783 req->maximumRecordSize = &p->maximumRecordSize;
785 if (p->idAuthenticationGroupId)
787 Z_IdPass *pass = odr_malloc (p->odr_out, sizeof(*pass));
788 Z_IdAuthentication *auth = odr_malloc (p->odr_out, sizeof(*auth));
790 logf (LOG_DEBUG, "using pass authentication");
792 auth->which = Z_IdAuthentication_idPass;
793 auth->u.idPass = pass;
794 if (p->idAuthenticationGroupId && *p->idAuthenticationGroupId)
795 pass->groupId = p->idAuthenticationGroupId;
797 pass->groupId = NULL;
798 if (p->idAuthenticationUserId && *p->idAuthenticationUserId)
799 pass->userId = p->idAuthenticationUserId;
802 if (p->idAuthenticationPassword && *p->idAuthenticationPassword)
803 pass->password = p->idAuthenticationPassword;
805 pass->password = NULL;
806 req->idAuthentication = auth;
808 else if (!p->idAuthenticationOpen || !*p->idAuthenticationOpen)
809 req->idAuthentication = NULL;
812 Z_IdAuthentication *auth = odr_malloc (p->odr_out, sizeof(*auth));
814 logf (LOG_DEBUG, "using open authentication");
815 auth->which = Z_IdAuthentication_open;
816 auth->u.open = p->idAuthenticationOpen;
817 req->idAuthentication = auth;
819 req->implementationId = p->implementationId;
820 req->implementationName = p->implementationName;
821 req->implementationVersion = p->implementationVersion;
822 req->userInformationField = 0;
824 return ir_tcl_send_APDU (interp, p, apdu, "init", *argv);
828 * do_protocolVersion: Set protocol Version
830 static int do_protocolVersion (void *obj, Tcl_Interp *interp,
831 int argc, char **argv)
839 ODR_MASK_ZERO (&p->protocolVersion);
840 ODR_MASK_SET (&p->protocolVersion, 0);
841 ODR_MASK_SET (&p->protocolVersion, 1);
846 if (Tcl_GetInt (interp, argv[2], &version)==TCL_ERROR)
848 ODR_MASK_ZERO (&p->protocolVersion);
849 for (i = 0; i<version; i++)
850 ODR_MASK_SET (&p->protocolVersion, i);
852 for (i = 4; --i >= 0; )
853 if (ODR_MASK_GET (&p->protocolVersion, i))
855 sprintf (buf, "%d", i+1);
856 interp->result = buf;
861 * do_options: Set options
863 static int do_options (void *obj, Tcl_Interp *interp,
864 int argc, char **argv)
866 static struct ir_named_entry options_tab[] = {
870 { "resourceReport", 3 },
871 { "triggerResourceCtrl", 4},
872 { "resourceCtrl", 5},
876 { "extendedServices", 10},
877 { "level-1Segmentation", 11},
878 { "level-2Segmentation", 12},
879 { "concurrentOperations", 13},
880 { "namedResultSets", 14},
887 ODR_MASK_ZERO (&p->options);
888 ODR_MASK_SET (&p->options, 0);
889 ODR_MASK_SET (&p->options, 1);
890 ODR_MASK_SET (&p->options, 4);
891 ODR_MASK_SET (&p->options, 7);
892 ODR_MASK_SET (&p->options, 14);
895 return ir_tcl_named_bits (options_tab, &p->options, interp, argc-2, argv+2);
899 * do_apduInfo: Get APDU information
901 static int do_apduInfo (void *obj, Tcl_Interp *interp, int argc, char **argv)
909 sprintf (buf, "%d", p->apduLen);
910 Tcl_AppendElement (interp, buf);
911 sprintf (buf, "%d", p->apduOffset);
912 Tcl_AppendElement (interp, buf);
915 Tcl_AppendElement (interp, "");
918 apduf = fopen ("apdu.tmp", "w");
921 Tcl_AppendElement (interp, "");
924 odr_dumpBER (apduf, p->buf_in, p->apduLen);
926 if (!(apduf = fopen ("apdu.tmp", "r")))
927 Tcl_AppendElement (interp, "");
932 Tcl_AppendResult (interp, " {", NULL);
933 while ((c = getc (apduf)) != EOF)
937 Tcl_AppendResult (interp, buf, NULL);
940 Tcl_AppendResult (interp, "}", NULL);
947 * do_failInfo: Get fail information
949 static int do_failInfo (void *obj, Tcl_Interp *interp, int argc, char **argv)
959 sprintf (buf, "%d", p->failInfo);
965 case IR_TCL_FAIL_CONNECT:
966 cp = "connect failed";
968 case IR_TCL_FAIL_READ:
969 cp = "connection closed";
971 case IR_TCL_FAIL_WRITE:
972 cp = "connection closed";
974 case IR_TCL_FAIL_IN_APDU:
975 cp = "failed to decode incoming APDU";
977 case IR_TCL_FAIL_UNKNOWN_APDU:
983 Tcl_AppendElement (interp, buf);
984 Tcl_AppendElement (interp, cp);
989 * do_preferredMessageSize: Set/get preferred message size
991 static int do_preferredMessageSize (void *obj, Tcl_Interp *interp,
992 int argc, char **argv)
998 p->preferredMessageSize = 30000;
1001 return ir_tcl_get_set_int (&p->preferredMessageSize, interp, argc, argv);
1005 * do_maximumRecordSize: Set/get maximum record size
1007 static int do_maximumRecordSize (void *obj, Tcl_Interp *interp,
1008 int argc, char **argv)
1014 p->maximumRecordSize = 30000;
1017 return ir_tcl_get_set_int (&p->maximumRecordSize, interp, argc, argv);
1021 * do_initResult: Get init result
1023 static int do_initResult (void *obj, Tcl_Interp *interp,
1024 int argc, char **argv)
1030 return ir_tcl_get_set_int (&p->initResult, interp, argc, argv);
1035 * do_implementationName: Set/get Implementation Name.
1037 static int do_implementationName (void *obj, Tcl_Interp *interp,
1038 int argc, char **argv)
1043 return ir_tcl_strdup (interp, &p->implementationName,
1045 else if (argc == -1)
1046 return ir_tcl_strdel (interp, &p->implementationName);
1049 xfree (p->implementationName);
1050 if (ir_tcl_strdup (interp, &p->implementationName, argv[2])
1054 Tcl_AppendResult (interp, p->implementationName, (char*) NULL);
1059 * do_implementationId: Get Implementation Id.
1061 static int do_implementationId (void *obj, Tcl_Interp *interp,
1062 int argc, char **argv)
1067 return ir_tcl_strdup (interp, &p->implementationId, "81");
1068 else if (argc == -1)
1069 return ir_tcl_strdel (interp, &p->implementationId);
1070 Tcl_AppendResult (interp, p->implementationId, (char*) NULL);
1075 * do_implementationVersion: get Implementation Version.
1077 static int do_implementationVersion (void *obj, Tcl_Interp *interp,
1078 int argc, char **argv)
1083 return ir_tcl_strdup (interp, &p->implementationVersion,
1084 #ifdef IR_TCL_VERSION
1089 else if (argc == -1)
1090 return ir_tcl_strdel (interp, &p->implementationVersion);
1091 Tcl_AppendResult (interp, p->implementationVersion, (char*) NULL);
1096 * do_targetImplementationName: Get Implementation Name of target.
1098 static int do_targetImplementationName (void *obj, Tcl_Interp *interp,
1099 int argc, char **argv)
1105 p->targetImplementationName = NULL;
1108 else if (argc == -1)
1109 return ir_tcl_strdel (interp, &p->targetImplementationName);
1110 Tcl_AppendResult (interp, p->targetImplementationName, (char*) NULL);
1115 * do_targetImplementationId: Get Implementation Id of target
1117 static int do_targetImplementationId (void *obj, Tcl_Interp *interp,
1118 int argc, char **argv)
1124 p->targetImplementationId = NULL;
1127 else if (argc == -1)
1128 return ir_tcl_strdel (interp, &p->targetImplementationId);
1129 Tcl_AppendResult (interp, p->targetImplementationId, (char*) NULL);
1134 * do_targetImplementationVersion: Get Implementation Version of target
1136 static int do_targetImplementationVersion (void *obj, Tcl_Interp *interp,
1137 int argc, char **argv)
1143 p->targetImplementationVersion = NULL;
1146 else if (argc == -1)
1147 return ir_tcl_strdel (interp, &p->targetImplementationVersion);
1148 Tcl_AppendResult (interp, p->targetImplementationVersion, (char*) NULL);
1153 * do_idAuthentication: Set/get id Authentication
1155 static int do_idAuthentication (void *obj, Tcl_Interp *interp,
1156 int argc, char **argv)
1160 if (argc >= 3 || argc == -1)
1162 xfree (p->idAuthenticationOpen);
1163 xfree (p->idAuthenticationGroupId);
1164 xfree (p->idAuthenticationUserId);
1165 xfree (p->idAuthenticationPassword);
1167 if (argc >= 3 || argc <= 0)
1169 p->idAuthenticationOpen = NULL;
1170 p->idAuthenticationGroupId = NULL;
1171 p->idAuthenticationUserId = NULL;
1172 p->idAuthenticationPassword = NULL;
1181 ir_tcl_strdup (interp, &p->idAuthenticationOpen, argv[2])
1188 ir_tcl_strdup (interp, &p->idAuthenticationGroupId, argv[2])
1192 ir_tcl_strdup (interp, &p->idAuthenticationUserId, argv[3])
1196 ir_tcl_strdup (interp, &p->idAuthenticationPassword, argv[4])
1201 if (p->idAuthenticationOpen)
1202 Tcl_AppendElement (interp, p->idAuthenticationOpen);
1203 else if (p->idAuthenticationGroupId)
1205 Tcl_AppendElement (interp, p->idAuthenticationGroupId);
1206 Tcl_AppendElement (interp, p->idAuthenticationUserId);
1207 Tcl_AppendElement (interp, p->idAuthenticationPassword);
1213 * do_connect: connect method on IR object
1215 static int do_connect (void *obj, Tcl_Interp *interp,
1216 int argc, char **argv)
1226 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
1227 " ?hostname?\"", NULL);
1232 Tcl_AppendResult (interp, p->hostname, NULL);
1236 logf (LOG_DEBUG, "connect %s %s", *argv, argv[2]);
1239 Tcl_AppendResult (interp, "already connected", NULL);
1240 return ir_tcl_error_exec (interp, argc, argv);
1242 if (!strcmp (p->comstackType, "tcpip"))
1244 p->cs_link = cs_create (tcpip_type, CS_BLOCK, p->protocol_type);
1245 logf (LOG_DEBUG, "tcp/ip connect %s", argv[2]);
1247 else if (!strcmp (p->comstackType, "mosi"))
1250 p->cs_link = cs_create (mosi_type, CS_BLOCK, p->protocol_type);
1251 logf (LOG_DEBUG, "mosi connect %s", argv[2]);
1253 Tcl_AppendResult (interp, "mosi not supported", NULL);
1254 return ir_tcl_error_exec (interp, argc, argv);
1259 Tcl_AppendResult (interp, "bad comstack type ",
1260 p->comstackType, NULL);
1261 return ir_tcl_error_exec (interp, argc, argv);
1263 if (ir_tcl_strdup (interp, &p->hostname, argv[2]) == TCL_ERROR)
1265 p->eventType = "connect";
1266 addr = cs_straddr (p->cs_link, argv[2]);
1269 ir_tcl_disconnect (p);
1270 Tcl_AppendResult (interp, "cs_straddr fail", NULL);
1271 return ir_tcl_error_exec (interp, argc, argv);
1273 if ((r=cs_connect (p->cs_link, addr)) < 0)
1275 ir_tcl_disconnect (p);
1276 Tcl_AppendResult (interp, "connect fail", NULL);
1277 return ir_tcl_error_exec (interp, argc, argv);
1279 ir_select_add (cs_fileno (p->cs_link), p);
1282 logf (LOG_DEBUG, "connect pending fd=%d", cs_fileno(p->cs_link));
1283 ir_select_add_write (cs_fileno (p->cs_link), p);
1284 p->state = IR_TCL_R_Connecting;
1288 logf (LOG_DEBUG, "connect ok fd=%d", cs_fileno(p->cs_link));
1289 p->state = IR_TCL_R_Idle;
1291 ir_tcl_eval (p->interp, p->callback);
1298 * ir_tcl_disconnect: close connection
1300 void ir_tcl_disconnect (IrTcl_Obj *p)
1304 logf(LOG_DEBUG, "Closing connection to %s", p->hostname);
1305 xfree (p->hostname);
1307 assert (p->cs_link);
1308 ir_select_remove (cs_fileno (p->cs_link), p);
1310 odr_reset (p->odr_in);
1312 #if TCL_MAJOR_VERSION == 8
1313 cs_fileno(p->cs_link) = -1;
1315 cs_close (p->cs_link);
1318 ODR_MASK_ZERO (&p->options);
1319 ODR_MASK_SET (&p->options, 0);
1320 ODR_MASK_SET (&p->options, 1);
1321 ODR_MASK_SET (&p->options, 4);
1322 ODR_MASK_SET (&p->options, 7);
1323 ODR_MASK_SET (&p->options, 14);
1325 ODR_MASK_ZERO (&p->protocolVersion);
1326 ODR_MASK_SET (&p->protocolVersion, 0);
1327 ODR_MASK_SET (&p->protocolVersion, 1);
1330 assert (!p->cs_link);
1334 * do_disconnect: disconnect method on IR object
1336 static int do_disconnect (void *obj, Tcl_Interp *interp,
1337 int argc, char **argv)
1343 p->state = IR_TCL_R_Idle;
1344 p->eventType = NULL;
1349 ir_tcl_disconnect (p);
1354 * do_comstack: Set/get comstack method on IR object
1356 static int do_comstack (void *o, Tcl_Interp *interp,
1357 int argc, char **argv)
1362 return ir_tcl_strdup (interp, &obj->comstackType, "tcpip");
1363 else if (argc == -1)
1364 return ir_tcl_strdel (interp, &obj->comstackType);
1367 xfree (obj->comstackType);
1368 if (ir_tcl_strdup (interp, &obj->comstackType, argv[2]) == TCL_ERROR)
1371 Tcl_AppendElement (interp, obj->comstackType);
1376 * do_logLevel: Set log level
1378 static int do_logLevel (void *o, Tcl_Interp *interp,
1379 int argc, char **argv)
1384 yaz_log_init (yaz_log_mask_str (argv[2]), "", NULL);
1386 yaz_log_init (yaz_log_mask_str (argv[2]), argv[3], NULL);
1388 yaz_log_init (yaz_log_mask_str (argv[2]), argv[3], argv[4]);
1394 * do_eventType: Return type of last event
1396 static int do_eventType (void *obj, Tcl_Interp *interp,
1397 int argc, char **argv)
1403 p->eventType = NULL;
1406 Tcl_AppendElement (interp, p->eventType ? p->eventType : "");
1412 * do_callback: add callback
1414 static int do_callback (void *obj, Tcl_Interp *interp,
1415 int argc, char **argv)
1424 else if (argc == -1)
1425 return ir_tcl_strdel (interp, &p->callback);
1428 xfree (p->callback);
1431 if (ir_tcl_strdup (interp, &p->callback, argv[2]) == TCL_ERROR)
1441 * do_failback: add error handle callback
1443 static int do_failback (void *obj, Tcl_Interp *interp,
1444 int argc, char **argv)
1453 else if (argc == -1)
1454 return ir_tcl_strdel (interp, &p->failback);
1457 xfree (p->failback);
1460 if (ir_tcl_strdup (interp, &p->failback, argv[2]) == TCL_ERROR)
1470 * do_initResponse: add init response handler
1472 static int do_initResponse (void *obj, Tcl_Interp *interp,
1473 int argc, char **argv)
1479 p->initResponse = NULL;
1482 else if (argc == -1)
1483 return ir_tcl_strdel (interp, &p->initResponse);
1486 xfree (p->initResponse);
1489 if (ir_tcl_strdup (interp, &p->initResponse, argv[2]) == TCL_ERROR)
1493 p->initResponse = NULL;
1498 * do_protocol: Set/get protocol method on IR object
1500 static int do_protocol (void *o, Tcl_Interp *interp, int argc, char **argv)
1506 p->protocol_type = PROTO_Z3950;
1511 if (!strcmp (argv[2], "Z39"))
1512 p->protocol_type = PROTO_Z3950;
1513 else if (!strcmp (argv[2], "SR"))
1514 p->protocol_type = PROTO_SR;
1517 Tcl_AppendResult (interp, "bad protocol ", argv[2], NULL);
1518 return ir_tcl_error_exec (interp, argc, argv);
1522 switch (p->protocol_type)
1525 Tcl_AppendElement (interp, "Z39");
1528 Tcl_AppendElement (interp, "SR");
1535 * do_triggerResourceControl:
1537 static int do_triggerResourceControl (void *obj, Tcl_Interp *interp,
1538 int argc, char **argv)
1542 Z_TriggerResourceControlRequest *req;
1543 bool_t is_false = 0;
1549 Tcl_AppendResult (interp, "not connected", NULL);
1550 return ir_tcl_error_exec (interp, argc, argv);
1552 apdu = zget_APDU (p->odr_out, Z_APDU_triggerResourceControlRequest);
1553 req = apdu->u.triggerResourceControlRequest;
1554 *req->requestedAction = Z_TriggerResourceCtrl_cancel;
1555 req->resultSetWanted = &is_false;
1557 return ir_tcl_send_APDU (interp, p, apdu, "triggerResourceControl",
1562 * do_databaseNames: specify database names
1564 static int do_databaseNames (void *obj, Tcl_Interp *interp,
1565 int argc, char **argv)
1568 IrTcl_SetCObj *p = obj;
1572 for (i=0; i<p->num_databaseNames; i++)
1573 xfree (p->databaseNames[i]);
1574 xfree (p->databaseNames);
1578 p->num_databaseNames = 0;
1579 p->databaseNames = NULL;
1584 for (i=0; i<p->num_databaseNames; i++)
1585 Tcl_AppendElement (interp, p->databaseNames[i]);
1588 if (p->databaseNames)
1590 for (i=0; i<p->num_databaseNames; i++)
1591 xfree (p->databaseNames[i]);
1592 xfree (p->databaseNames);
1594 p->num_databaseNames = argc - 2;
1596 ir_tcl_malloc (sizeof(*p->databaseNames) * (1+p->num_databaseNames));
1597 for (i=0; i<p->num_databaseNames; i++)
1599 if (ir_tcl_strdup (interp, &p->databaseNames[i], argv[2+i])
1603 p->databaseNames[i] = NULL;
1608 * do_replaceIndicator: Set/get replace Set indicator
1610 static int do_replaceIndicator (void *obj, Tcl_Interp *interp,
1611 int argc, char **argv)
1613 IrTcl_SetCObj *p = obj;
1617 p->replaceIndicator = 1;
1620 return ir_tcl_get_set_int (&p->replaceIndicator, interp, argc, argv);
1624 * do_queryType: Set/Get query method
1626 static int do_queryType (void *obj, Tcl_Interp *interp,
1627 int argc, char **argv)
1629 IrTcl_SetCObj *p = obj;
1632 return ir_tcl_strdup (interp, &p->queryType, "rpn");
1633 else if (argc == -1)
1634 return ir_tcl_strdel (interp, &p->queryType);
1637 xfree (p->queryType);
1638 if (ir_tcl_strdup (interp, &p->queryType, argv[2]) == TCL_ERROR)
1641 Tcl_AppendResult (interp, p->queryType, NULL);
1646 * do_userInformationField: Get User information field
1648 static int do_userInformationField (void *obj, Tcl_Interp *interp,
1649 int argc, char **argv)
1655 p->userInformationField = NULL;
1658 else if (argc == -1)
1659 return ir_tcl_strdel (interp, &p->userInformationField);
1660 Tcl_AppendResult (interp, p->userInformationField, NULL);
1665 * do_smallSetUpperBound: Set/get small set upper bound
1667 static int do_smallSetUpperBound (void *o, Tcl_Interp *interp,
1668 int argc, char **argv)
1670 IrTcl_SetCObj *p = o;
1674 p->smallSetUpperBound = 0;
1677 return ir_tcl_get_set_int (&p->smallSetUpperBound, interp, argc, argv);
1681 * do_largeSetLowerBound: Set/get large set lower bound
1683 static int do_largeSetLowerBound (void *o, Tcl_Interp *interp,
1684 int argc, char **argv)
1686 IrTcl_SetCObj *p = o;
1690 p->largeSetLowerBound = 2;
1693 return ir_tcl_get_set_int (&p->largeSetLowerBound, interp, argc, argv);
1697 * do_mediumSetPresentNumber: Set/get large set lower bound
1699 static int do_mediumSetPresentNumber (void *o, Tcl_Interp *interp,
1700 int argc, char **argv)
1702 IrTcl_SetCObj *p = o;
1706 p->mediumSetPresentNumber = 0;
1709 return ir_tcl_get_set_int (&p->mediumSetPresentNumber, interp, argc, argv);
1713 * do_referenceId: Set/Get referenceId
1715 static int do_referenceId (void *obj, Tcl_Interp *interp,
1716 int argc, char **argv)
1718 IrTcl_SetCObj *p = obj;
1722 p->referenceId = NULL;
1725 else if (argc == -1)
1726 return ir_tcl_strdel (interp, &p->referenceId);
1729 xfree (p->referenceId);
1730 if (ir_tcl_strdup (interp, &p->referenceId, argv[2]) == TCL_ERROR)
1733 Tcl_AppendResult (interp, p->referenceId, NULL);
1738 * do_preferredRecordSyntax: Set/get preferred record syntax
1740 static int do_preferredRecordSyntax (void *obj, Tcl_Interp *interp,
1741 int argc, char **argv)
1743 IrTcl_SetCObj *p = obj;
1747 p->preferredRecordSyntax = NULL;
1750 else if (argc == -1)
1752 xfree (p->preferredRecordSyntax);
1753 p->preferredRecordSyntax = NULL;
1758 xfree (p->preferredRecordSyntax);
1759 p->preferredRecordSyntax = NULL;
1760 if (argv[2][0] && (p->preferredRecordSyntax =
1761 ir_tcl_malloc (sizeof(*p->preferredRecordSyntax))))
1762 *p->preferredRecordSyntax = IrTcl_getRecordSyntaxVal (argv[2]);
1767 (interp,!p->preferredRecordSyntax ? "" :
1768 IrTcl_getRecordSyntaxStr(*p->preferredRecordSyntax));
1775 * do_elementSetNames: Set/Get element Set Names
1777 static int do_elementSetNames (void *obj, Tcl_Interp *interp,
1778 int argc, char **argv)
1780 IrTcl_SetCObj *p = obj;
1784 p->elementSetNames = NULL;
1787 else if (argc == -1)
1788 return ir_tcl_strdel (interp, &p->elementSetNames);
1791 xfree (p->elementSetNames);
1792 if (ir_tcl_strdup (interp, &p->elementSetNames, argv[2]) == TCL_ERROR)
1795 Tcl_AppendResult (interp, p->elementSetNames, NULL);
1800 * do_smallSetElementSetNames: Set/Get small Set Element Set Names
1802 static int do_smallSetElementSetNames (void *obj, Tcl_Interp *interp,
1803 int argc, char **argv)
1805 IrTcl_SetCObj *p = obj;
1809 p->smallSetElementSetNames = NULL;
1812 else if (argc == -1)
1813 return ir_tcl_strdel (interp, &p->smallSetElementSetNames);
1816 xfree (p->smallSetElementSetNames);
1817 if (ir_tcl_strdup (interp, &p->smallSetElementSetNames,
1818 argv[2]) == TCL_ERROR)
1821 Tcl_AppendResult (interp, p->smallSetElementSetNames, NULL);
1826 * do_mediumSetElementSetNames: Set/Get medium Set Element Set Names
1828 static int do_mediumSetElementSetNames (void *obj, Tcl_Interp *interp,
1829 int argc, char **argv)
1831 IrTcl_SetCObj *p = obj;
1835 p->mediumSetElementSetNames = NULL;
1838 else if (argc == -1)
1839 return ir_tcl_strdel (interp, &p->mediumSetElementSetNames);
1842 xfree (p->mediumSetElementSetNames);
1843 if (ir_tcl_strdup (interp, &p->mediumSetElementSetNames,
1844 argv[2]) == TCL_ERROR)
1847 Tcl_AppendResult (interp, p->mediumSetElementSetNames, NULL);
1851 static IrTcl_Method ir_method_tab[] = {
1852 { "comstack", do_comstack, NULL },
1853 { "protocol", do_protocol, NULL },
1854 { "failback", do_failback, NULL },
1855 { "failInfo", do_failInfo, NULL },
1856 { "apduInfo", do_apduInfo, NULL },
1857 { "logLevel", do_logLevel, NULL },
1859 { "eventType", do_eventType, NULL },
1860 { "connect", do_connect, NULL },
1861 { "protocolVersion", do_protocolVersion, NULL },
1862 { "preferredMessageSize", do_preferredMessageSize, NULL },
1863 { "maximumRecordSize", do_maximumRecordSize, NULL },
1864 { "implementationName", do_implementationName, NULL },
1865 { "implementationId", do_implementationId, NULL },
1866 { "implementationVersion", do_implementationVersion, NULL },
1867 { "targetImplementationName", do_targetImplementationName, NULL },
1868 { "targetImplementationId", do_targetImplementationId, NULL },
1869 { "targetImplementationVersion", do_targetImplementationVersion, NULL},
1870 { "userInformationField", do_userInformationField, NULL},
1871 { "idAuthentication", do_idAuthentication, NULL},
1872 { "options", do_options, NULL},
1873 { "init", do_init_request, NULL},
1874 { "initResult", do_initResult, NULL},
1875 { "disconnect", do_disconnect, NULL},
1876 { "callback", do_callback, NULL},
1877 { "initResponse", do_initResponse, NULL},
1878 { "triggerResourceControl", do_triggerResourceControl, NULL},
1879 { "initResponse", do_initResponse, NULL},
1883 static IrTcl_Method ir_set_c_method_tab[] = {
1884 { "databaseNames", do_databaseNames, NULL},
1885 { "replaceIndicator", do_replaceIndicator, NULL},
1886 { "queryType", do_queryType, NULL},
1887 { "preferredRecordSyntax", do_preferredRecordSyntax, NULL},
1888 { "smallSetUpperBound", do_smallSetUpperBound, NULL},
1889 { "largeSetLowerBound", do_largeSetLowerBound, NULL},
1890 { "mediumSetPresentNumber", do_mediumSetPresentNumber, NULL},
1891 { "referenceId", do_referenceId, NULL},
1892 { "elementSetNames", do_elementSetNames, NULL},
1893 { "smallSetElementSetNames", do_smallSetElementSetNames, NULL},
1894 { "mediumSetElementSetNames", do_mediumSetElementSetNames, NULL},
1899 * ir_obj_method: IR Object methods
1901 static int ir_obj_method (ClientData clientData, Tcl_Interp *interp,
1902 int argc, char **argv)
1904 IrTcl_Methods tab[3];
1905 IrTcl_Obj *p = (IrTcl_Obj *) clientData;
1910 Tcl_AppendResult (interp, wrongArgs, *argv, "method args...\"", NULL);
1914 tab[0].tab = ir_method_tab;
1916 tab[1].tab = ir_set_c_method_tab;
1917 tab[1].obj = &p->set_inher;
1920 if (ir_tcl_method (interp, argc, argv, tab, &r) == TCL_ERROR)
1921 return ir_tcl_method_error (interp, argc, argv, tab);
1926 * ir_obj_delete: IR Object disposal
1928 static void ir_obj_delete (ClientData clientData)
1930 IrTcl_Obj *obj = (IrTcl_Obj *) clientData;
1931 IrTcl_Methods tab[3];
1934 if (obj->ref_count > 0)
1936 assert (obj->ref_count == 0);
1938 logf (LOG_DEBUG, "ir object delete");
1939 tab[0].tab = ir_method_tab;
1941 tab[1].tab = ir_set_c_method_tab;
1942 tab[1].obj = &obj->set_inher;
1945 ir_tcl_method (NULL, -1, NULL, tab, NULL);
1948 odr_destroy (obj->odr_in);
1949 odr_destroy (obj->odr_out);
1950 odr_destroy (obj->odr_pr);
1955 * ir_obj_init: IR Object initialization
1957 int ir_obj_init (ClientData clientData, Tcl_Interp *interp,
1958 int argc, char **argv, ClientData *subData,
1959 ClientData parentData)
1961 IrTcl_Methods tab[3];
1969 Tcl_AppendResult (interp, wrongArgs, *argv, " objName\"", NULL);
1972 obj = ir_tcl_malloc (sizeof(*obj));
1975 obj->bibset = ccl_qual_mk ();
1976 if ((inf = fopen ("default.bib", "r")))
1978 ccl_qual_file (obj->bibset, inf);
1983 logf (LOG_DEBUG, "ir object create %s", argv[1]);
1984 obj->odr_in = odr_createmem (ODR_DECODE);
1985 odr_choice_enable_bias (obj->odr_in, 0);
1986 obj->odr_out = odr_createmem (ODR_ENCODE);
1987 obj->odr_pr = odr_createmem (ODR_PRINT);
1988 obj->state = IR_TCL_R_Idle;
1989 obj->interp = interp;
1993 obj->request_queue = NULL;
1995 tab[0].tab = ir_method_tab;
1997 tab[1].tab = ir_set_c_method_tab;
1998 tab[1].obj = &obj->set_inher;
2001 if (ir_tcl_method (interp, 0, NULL, tab, NULL) == TCL_ERROR)
2003 Tcl_AppendResult (interp, "Failed to initialize ", argv[1], NULL);
2006 *subData = (ClientData) obj;
2012 * ir_obj_mk: IR Object creation
2014 static int ir_obj_mk (ClientData clientData, Tcl_Interp *interp,
2015 int argc, char **argv)
2018 int r = ir_obj_init (clientData, interp, argc, argv, &subData, 0);
2022 Tcl_CreateCommand (interp, argv[1], ir_obj_method,
2023 subData, ir_obj_delete);
2027 IrTcl_Class ir_obj_class = {
2035 /* ------------------------------------------------------- */
2037 * do_search: Do search request
2039 static int do_search (void *o, Tcl_Interp *interp, int argc, char **argv)
2041 Z_SearchRequest *req;
2045 IrTcl_SetObj *obj = o;
2048 #if TCL_MAJOR_VERSION > 8 || (TCL_MAJOR_VERSION == 8 && TCL_MINOR_VERSION > 0)
2060 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1], "query\"",
2064 #if TCL_MAJOR_VERSION > 8 || (TCL_MAJOR_VERSION == 8 && TCL_MINOR_VERSION > 0)
2065 query_str = Tcl_UtfToExternalDString(0, argv[2], -1, &ds);
2067 query_str = argv[2];
2069 logf (LOG_DEBUG, "search %s %s", *argv, query_str);
2070 if (!obj->set_inher.num_databaseNames)
2072 Tcl_AppendResult (interp, "no databaseNames", NULL);
2073 code = ir_tcl_error_exec (interp, argc, argv);
2078 Tcl_AppendResult (interp, "not connected", NULL);
2079 code = ir_tcl_error_exec (interp, argc, argv);
2082 apdu = zget_APDU (p->odr_out, Z_APDU_searchRequest);
2083 req = apdu->u.searchRequest;
2087 set_referenceId (p->odr_out, &req->referenceId,
2088 obj->set_inher.referenceId);
2090 req->smallSetUpperBound = &obj->set_inher.smallSetUpperBound;
2091 req->largeSetLowerBound = &obj->set_inher.largeSetLowerBound;
2092 req->mediumSetPresentNumber = &obj->set_inher.mediumSetPresentNumber;
2093 req->replaceIndicator = &obj->set_inher.replaceIndicator;
2094 req->resultSetName = obj->setName ? obj->setName : "default";
2095 logf (LOG_DEBUG, "Search, resultSetName %s", req->resultSetName);
2096 req->num_databaseNames = obj->set_inher.num_databaseNames;
2097 req->databaseNames = obj->set_inher.databaseNames;
2098 for (r=0; r < obj->set_inher.num_databaseNames; r++)
2099 logf (LOG_DEBUG, " Database %s", obj->set_inher.databaseNames[r]);
2100 if (obj->set_inher.preferredRecordSyntax)
2102 struct oident ident;
2104 ident.proto = p->protocol_type;
2105 ident.oclass = CLASS_RECSYN;
2106 ident.value = *obj->set_inher.preferredRecordSyntax;
2107 logf (LOG_DEBUG, "Preferred record syntax is %d", ident.value);
2108 req->preferredRecordSyntax = odr_oiddup (p->odr_out,
2109 oid_getoidbyent (&ident));
2112 req->preferredRecordSyntax = 0;
2114 if (obj->set_inher.smallSetElementSetNames &&
2115 *obj->set_inher.smallSetElementSetNames)
2117 Z_ElementSetNames *esn = odr_malloc (p->odr_out, sizeof(*esn));
2119 esn->which = Z_ElementSetNames_generic;
2120 esn->u.generic = obj->set_inher.smallSetElementSetNames;
2121 req->smallSetElementSetNames = esn;
2124 req->smallSetElementSetNames = NULL;
2126 if (obj->set_inher.mediumSetElementSetNames &&
2127 *obj->set_inher.mediumSetElementSetNames)
2129 Z_ElementSetNames *esn = odr_malloc (p->odr_out, sizeof(*esn));
2131 esn->which = Z_ElementSetNames_generic;
2132 esn->u.generic = obj->set_inher.mediumSetElementSetNames;
2133 req->mediumSetElementSetNames = esn;
2136 req->mediumSetElementSetNames = NULL;
2138 req->query = &query;
2140 logf (LOG_DEBUG, "queryType %s", obj->set_inher.queryType);
2141 if (!strcmp (obj->set_inher.queryType, "rpn"))
2143 Z_RPNQuery *RPNquery;
2145 RPNquery = p_query_rpn (p->odr_out, p->protocol_type, query_str);
2148 Tcl_AppendResult (interp, "query syntax error", NULL);
2149 code = ir_tcl_error_exec (interp, argc, argv);
2152 query.which = Z_Query_type_1;
2153 query.u.type_1 = RPNquery;
2156 else if (!strcmp (obj->set_inher.queryType, "cclrpn"))
2160 struct ccl_rpn_node *rpn;
2161 Z_RPNQuery *RPNquery;
2164 bib1.proto = p->protocol_type;
2165 bib1.oclass = CLASS_ATTSET;
2166 bib1.value = VAL_BIB1;
2168 rpn = ccl_find_str(p->bibset, query_str, &error, &pos);
2171 Tcl_AppendResult (interp, "ccl syntax error ", ccl_err_msg(error),
2173 code = ir_tcl_error_exec (interp, argc, argv);
2177 ccl_pr_tree (rpn, stderr);
2178 fprintf (stderr, "\n");
2180 RPNquery = ccl_rpn_query(p->odr_out, rpn);
2181 RPNquery->attributeSetId = oid_getoidbyent (&bib1);
2182 query.which = Z_Query_type_1;
2183 query.u.type_1 = RPNquery;
2186 else if (!strcmp (obj->set_inher.queryType, "ccl"))
2188 query.which = Z_Query_type_2;
2189 query.u.type_2 = &ccl_query;
2190 ccl_query.buf = (unsigned char *) query_str;
2191 ccl_query.len = strlen (query_str);
2195 Tcl_AppendResult (interp, "invalid query method ",
2196 obj->set_inher.queryType, NULL);
2197 code = ir_tcl_error_exec (interp, argc, argv);
2200 code = ir_tcl_send_APDU (interp, p, apdu, "search", *argv);
2202 #if TCL_MAJOR_VERSION > 8 || (TCL_MAJOR_VERSION == 8 && TCL_MINOR_VERSION > 0)
2203 Tcl_DStringFree (&ds);
2209 * do_searchResponse: add search response handler
2211 static int do_searchResponse (void *o, Tcl_Interp *interp,
2212 int argc, char **argv)
2214 IrTcl_SetObj *obj = o;
2218 obj->searchResponse = NULL;
2221 else if (argc == -1)
2222 return ir_tcl_strdel (interp, &obj->searchResponse);
2225 xfree (obj->searchResponse);
2228 if (ir_tcl_strdup (interp, &obj->searchResponse, argv[2])
2233 obj->searchResponse = NULL;
2239 * do_presentResponse: add present response handler
2241 static int do_presentResponse (void *o, Tcl_Interp *interp,
2242 int argc, char **argv)
2244 IrTcl_SetObj *obj = o;
2248 obj->presentResponse = NULL;
2251 else if (argc == -1)
2252 return ir_tcl_strdel (interp, &obj->presentResponse);
2255 xfree (obj->presentResponse);
2258 if (ir_tcl_strdup (interp, &obj->presentResponse, argv[2])
2263 obj->presentResponse = NULL;
2269 * do_sortResponse: add sort response handler
2271 static int do_sortResponse (void *o, Tcl_Interp *interp,
2272 int argc, char **argv)
2274 IrTcl_SetObj *obj = o;
2278 obj->sortResponse = NULL;
2281 else if (argc == -1)
2282 return ir_tcl_strdel (interp, &obj->sortResponse);
2285 xfree (obj->sortResponse);
2288 if (ir_tcl_strdup (interp, &obj->sortResponse, argv[2])
2293 obj->sortResponse = NULL;
2299 * do_resultCount: Get number of hits
2301 static int do_resultCount (void *o, Tcl_Interp *interp,
2302 int argc, char **argv)
2304 IrTcl_SetObj *obj = o;
2308 obj->resultCount = 0;
2311 return ir_tcl_get_set_int (&obj->resultCount, interp, argc, argv);
2315 * do_searchStatus: Get search status (after search response)
2317 static int do_searchStatus (void *o, Tcl_Interp *interp,
2318 int argc, char **argv)
2320 IrTcl_SetObj *obj = o;
2324 return ir_tcl_get_set_int (&obj->searchStatus, interp, argc, argv);
2328 * do_presentStatus: Get search status (after search/present response)
2330 static int do_presentStatus (void *o, Tcl_Interp *interp,
2331 int argc, char **argv)
2333 IrTcl_SetObj *obj = o;
2337 return ir_tcl_get_set_int (&obj->presentStatus, interp, argc, argv);
2341 * do_sortStatus: Get sort status (after sort response)
2343 static int do_sortStatus (void *o, Tcl_Interp *interp,
2344 int argc, char **argv)
2346 IrTcl_SetObj *obj = o;
2351 obj->sortStatus = Z_SortStatus_failure;
2354 switch (obj->sortStatus)
2356 case Z_SortStatus_success:
2357 res = "success"; break;
2358 case Z_SortStatus_partial_1:
2359 res = "partial"; break;
2360 case Z_SortStatus_failure:
2361 res = "failure"; break;
2363 res = "unknown"; break;
2365 Tcl_AppendElement (interp, res);
2370 * do_nextResultSetPosition: Get next result set position
2371 * (after search/present response)
2373 static int do_nextResultSetPosition (void *o, Tcl_Interp *interp,
2374 int argc, char **argv)
2376 IrTcl_SetObj *obj = o;
2380 obj->nextResultSetPosition = 0;
2383 return ir_tcl_get_set_int (&obj->nextResultSetPosition, interp,
2388 * do_setName: Set result Set name
2390 static int do_setName (void *o, Tcl_Interp *interp,
2391 int argc, char **argv)
2393 IrTcl_SetObj *obj = o;
2396 return ir_tcl_strdup (interp, &obj->setName, "Default");
2397 else if (argc == -1)
2398 return ir_tcl_strdel (interp, &obj->setName);
2401 xfree (obj->setName);
2402 if (ir_tcl_strdup (interp, &obj->setName, argv[2])
2406 Tcl_AppendElement (interp, obj->setName);
2411 * do_numberOfRecordsReturned: Get number of records returned
2413 static int do_numberOfRecordsReturned (void *o, Tcl_Interp *interp,
2414 int argc, char **argv)
2416 IrTcl_SetObj *obj = o;
2420 obj->numberOfRecordsReturned = 0;
2423 return ir_tcl_get_set_int (&obj->numberOfRecordsReturned, interp,
2428 * do_type: Return type (if any) at position.
2430 static int do_type (void *o, Tcl_Interp *interp, int argc, char **argv)
2432 IrTcl_SetObj *obj = o;
2434 IrTcl_RecordList *rl;
2438 obj->record_list = NULL;
2441 else if (argc == -1)
2443 delete_IR_records (obj);
2448 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2449 " position\"", NULL);
2452 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2454 rl = find_IR_record (obj, offset);
2457 logf (LOG_DEBUG, "%s %s %s: no record", argv[0], argv[1], argv[2]);
2462 case Z_NamePlusRecord_databaseRecord:
2463 interp->result = "DB";
2465 case Z_NamePlusRecord_surrogateDiagnostic:
2466 interp->result = "SD";
2474 * do_recordType: Return record type (if any) at position.
2476 static int do_recordType (void *o, Tcl_Interp *interp, int argc, char **argv)
2478 IrTcl_SetObj *obj = o;
2480 IrTcl_RecordList *rl;
2486 else if (argc == -1)
2492 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2493 " position\"", NULL);
2496 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2498 rl = find_IR_record (obj, offset);
2501 logf (LOG_DEBUG, "%s %s %s: no record", argv[0], argv[1], argv[2]);
2504 if (rl->which != Z_NamePlusRecord_databaseRecord)
2506 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2509 Tcl_AppendElement (interp, (char*)
2510 IrTcl_getRecordSyntaxStr (rl->u.dbrec.type));
2515 * set record elements (for record extraction)
2517 static int do_recordElements (void *o, Tcl_Interp *interp,
2518 int argc, char **argv)
2520 IrTcl_SetObj *obj = o;
2524 obj->recordElements = NULL;
2527 else if (argc == -1)
2528 return ir_tcl_strdel (NULL, &obj->recordElements);
2531 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2532 " ?position?\"", NULL);
2537 xfree (obj->recordElements);
2538 return ir_tcl_strdup (NULL, &obj->recordElements,
2539 (*argv[2] ? argv[2] : NULL));
2541 Tcl_AppendResult (interp, obj->recordElements, NULL);
2548 static int ir_diagResult (Tcl_Interp *interp, IrTcl_Diagnostic *list, int num)
2554 for (i = 0; i<num; i++)
2556 sprintf (buf, "%d", list[i].condition);
2557 Tcl_AppendElement (interp, buf);
2558 cp = diagbib1_str (list[i].condition);
2560 Tcl_AppendElement (interp, (char*) cp);
2562 Tcl_AppendElement (interp, "");
2563 if (list[i].addinfo)
2564 Tcl_AppendElement (interp, (char*) list[i].addinfo);
2566 Tcl_AppendElement (interp, "");
2572 * do_diag: Return diagnostic record info
2574 static int do_diag (void *o, Tcl_Interp *interp, int argc, char **argv)
2576 IrTcl_SetObj *obj = o;
2578 IrTcl_RecordList *rl;
2584 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2585 " position\"", NULL);
2588 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2590 rl = find_IR_record (obj, offset);
2593 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2596 if (rl->which != Z_NamePlusRecord_surrogateDiagnostic)
2598 Tcl_AppendResult (interp, "No Diagnostic record at #", argv[2], NULL);
2601 return ir_diagResult (interp, rl->u.surrogateDiagnostics.list,
2602 rl->u.surrogateDiagnostics.num);
2606 * do_getMarc: Get ISO2709 Record lines/fields
2608 static int do_getMarc (void *o, Tcl_Interp *interp, int argc, char **argv)
2610 IrTcl_SetObj *obj = o;
2612 IrTcl_RecordList *rl;
2618 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2619 " position line|field tag ind field\"", NULL);
2622 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2624 rl = find_IR_record (obj, offset);
2627 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2630 if (rl->which != Z_NamePlusRecord_databaseRecord)
2632 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2635 return ir_tcl_get_marc (interp, rl->u.dbrec.buf, argc, argv);
2639 * do_getSutrs: Get SUTRS Record
2641 static int do_getSutrs (void *o, Tcl_Interp *interp, int argc, char **argv)
2643 IrTcl_SetObj *obj = o;
2645 IrTcl_RecordList *rl;
2651 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2652 " position\"", NULL);
2655 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2657 rl = find_IR_record (obj, offset);
2660 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2663 if (rl->which != Z_NamePlusRecord_databaseRecord)
2665 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2668 if (!rl->u.dbrec.buf || rl->u.dbrec.type != VAL_SUTRS)
2670 Tcl_AppendElement (interp, rl->u.dbrec.buf);
2676 * do_getGrs: Get a GRS-1 Record
2678 static int do_getGrs (void *o, Tcl_Interp *interp, int argc, char **argv)
2680 IrTcl_SetObj *obj = o;
2682 IrTcl_RecordList *rl;
2688 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2689 " position ?(set,tag) (set,tag) ...?\"", NULL);
2692 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2694 rl = find_IR_record (obj, offset);
2697 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2700 if (rl->which != Z_NamePlusRecord_databaseRecord)
2702 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2705 if (rl->u.dbrec.type != VAL_GRS1)
2707 return ir_tcl_get_grs (interp, rl->u.dbrec.u.grs1, argc, argv);
2712 * do_getExplain: Get an Explain Record
2714 static int do_getExplain (void *o, Tcl_Interp *interp, int argc, char **argv)
2716 IrTcl_SetObj *obj = o;
2717 IrTcl_Obj *p = obj->parent;
2719 Z_ext_typeent *etype;
2721 IrTcl_RecordList *rl;
2727 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2728 " position ?mask? ...\"", NULL);
2731 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2733 rl = find_IR_record (obj, offset);
2736 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2739 if (rl->which != Z_NamePlusRecord_databaseRecord)
2741 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2744 if (!rl->u.dbrec.buf || rl->u.dbrec.type != VAL_EXPLAIN)
2747 if (!(etype = z_ext_getentbyref (VAL_EXPLAIN)))
2749 assert (rl->u.dbrec.buf);
2750 odr_setbuf (p->odr_in, rl->u.dbrec.buf, rl->u.dbrec.size, 0);
2751 if (!(*etype->fun)(p->odr_in, (char **) &rr, 0, 0))
2754 if (etype->what != Z_External_explainRecord)
2757 return ir_tcl_get_explain (interp, rr, argc, argv);
2761 * do_responseStatus: Return response status (present or search)
2763 static int do_responseStatus (void *o, Tcl_Interp *interp,
2764 int argc, char **argv)
2766 IrTcl_SetObj *obj = o;
2770 obj->recordFlag = 0;
2771 obj->nonSurrogateDiagnosticNum = 0;
2772 obj->nonSurrogateDiagnosticList = NULL;
2775 else if (argc == -1)
2777 ir_deleteDiags (&obj->nonSurrogateDiagnosticList,
2778 &obj->nonSurrogateDiagnosticNum);
2781 if (!obj->recordFlag)
2783 Tcl_AppendElement (interp, "OK");
2788 case Z_Records_DBOSD:
2789 Tcl_AppendElement (interp, "DBOSD");
2792 Tcl_AppendElement (interp, "NSD");
2793 return ir_diagResult (interp, obj->nonSurrogateDiagnosticList,
2794 obj->nonSurrogateDiagnosticNum);
2795 case Z_Records_multipleNSD:
2796 Tcl_AppendElement (interp, "NSD");
2797 return ir_diagResult (interp, obj->nonSurrogateDiagnosticList,
2798 obj->nonSurrogateDiagnosticNum);
2804 * do_present: Perform Present Request
2807 static int do_present (void *o, Tcl_Interp *interp, int argc, char **argv)
2809 IrTcl_SetObj *obj = o;
2812 Z_PresentRequest *req;
2820 if (Tcl_GetInt (interp, argv[2], &start) == TCL_ERROR)
2827 if (Tcl_GetInt (interp, argv[3], &number) == TCL_ERROR)
2832 logf (LOG_DEBUG, "present %s %d %d", *argv, start, number);
2836 Tcl_AppendResult (interp, "not connected", NULL);
2837 return ir_tcl_error_exec (interp, argc, argv);
2840 obj->number = number;
2842 apdu = zget_APDU (p->odr_out, Z_APDU_presentRequest);
2843 req = apdu->u.presentRequest;
2845 set_referenceId (p->odr_out, &req->referenceId,
2846 obj->set_inher.referenceId);
2848 req->resultSetId = obj->setName ? obj->setName : "Default";
2850 req->resultSetStartPoint = &start;
2851 req->numberOfRecordsRequested = &number;
2852 if (obj->set_inher.preferredRecordSyntax)
2854 struct oident ident;
2856 ident.proto = p->protocol_type;
2857 ident.oclass = CLASS_RECSYN;
2858 ident.value = *obj->set_inher.preferredRecordSyntax;
2859 logf (LOG_DEBUG, "Preferred record syntax is %d", ident.value);
2860 req->preferredRecordSyntax = odr_oiddup (p->odr_out,
2861 oid_getoidbyent (&ident));
2864 req->preferredRecordSyntax = 0;
2866 if (obj->set_inher.elementSetNames && *obj->set_inher.elementSetNames)
2868 Z_ElementSetNames *esn = odr_malloc (p->odr_out, sizeof(*esn));
2869 Z_RecordComposition *compo = odr_malloc (p->odr_out, sizeof(*compo));
2871 esn->which = Z_ElementSetNames_generic;
2872 esn->u.generic = obj->set_inher.elementSetNames;
2874 req->recordComposition = compo;
2875 compo->which = Z_RecordComp_simple;
2876 compo->u.simple = esn;
2879 req->recordComposition = NULL;
2880 return ir_tcl_send_APDU (interp, p, apdu, "present", *argv);
2883 #define IR_TCL_RECORD_ENCODING_ISO2709 1
2884 #define IR_TCL_RECORD_ENCODING_RAW 2
2890 } IrTcl_FileRecordHead;
2893 * do_loadFile: Load result set from file
2895 static int do_loadFile (void *o, Tcl_Interp *interp,
2896 int argc, char **argv)
2898 IrTcl_SetObj *setobj = o;
2910 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2911 " filename ?start? ?number?\"", NULL);
2915 start = atoi (argv[3]);
2917 number = atoi (argv[4]);
2920 inf = fopen (argv[2], "r");
2923 Tcl_AppendResult (interp, "Cannot open ", argv[2], NULL);
2924 return ir_tcl_error_exec (interp, argc, argv);
2926 while (offset < (start+number))
2928 IrTcl_FileRecordHead head;
2929 IrTcl_RecordList *rl;
2931 if (fread (&head, sizeof(head), 1, inf) < 1)
2933 rl = new_IR_record (setobj, offset,
2934 Z_NamePlusRecord_databaseRecord,
2935 (argc > 5) ? argv[5] : NULL);
2936 rl->u.dbrec.type = head.syntax;
2937 if (head.encoding == IR_TCL_RECORD_ENCODING_ISO2709)
2939 if (!(buf = ir_tcl_fread_marc (inf, &size)))
2941 rl->u.dbrec.buf = buf;
2942 rl->u.dbrec.size = size;
2943 if (size != head.size)
2946 Tcl_AppendResult (interp, "bad ISO2709 encoding", NULL);
2947 return ir_tcl_error_exec (interp, argc, argv);
2950 else if (head.encoding == IR_TCL_RECORD_ENCODING_RAW)
2952 rl->u.dbrec.size = head.size;
2953 rl->u.dbrec.buf = ir_tcl_malloc (head.size + 1);
2954 if (fread (rl->u.dbrec.buf, rl->u.dbrec.size, 1, inf) < 1)
2957 Tcl_AppendResult (interp, "bad raw encoding", NULL);
2958 return ir_tcl_error_exec (interp, argc, argv);
2960 rl->u.dbrec.buf[rl->u.dbrec.size] = '\0';
2964 rl->u.dbrec.buf = NULL;
2965 rl->u.dbrec.size = 0;
2967 Tcl_AppendResult (interp, "bad encoding", NULL);
2968 return ir_tcl_error_exec (interp, argc, argv);
2972 setobj->numberOfRecordsReturned = offset - start;
2978 * do_saveFile: Save result set on file
2980 static int do_saveFile (void *o, Tcl_Interp *interp,
2981 int argc, char **argv)
2983 IrTcl_SetObj *setobj = o;
2988 IrTcl_RecordList *rl;
2994 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2995 " filename ?start? ?number?\"", NULL);
2999 start = atoi (argv[3]);
3001 number = atoi (argv[4]);
3004 outf = fopen (argv[2], "w");
3007 Tcl_AppendResult (interp, "cannot open file", NULL);
3008 return ir_tcl_error_exec (interp, argc, argv);
3010 while (offset < (start+number) && (rl = find_IR_record (setobj, offset)))
3012 if (rl->which == Z_NamePlusRecord_databaseRecord &&
3013 rl->u.dbrec.buf && rl->u.dbrec.size)
3015 IrTcl_FileRecordHead head;
3017 head.encoding = IR_TCL_RECORD_ENCODING_RAW;
3018 head.syntax = rl->u.dbrec.type;
3019 head.size = rl->u.dbrec.size;
3020 if (fwrite (&head, sizeof(head), 1, outf) < 1)
3022 Tcl_AppendResult (interp, "cannot write", NULL);
3023 return ir_tcl_error_exec (interp, argc, argv);
3025 if (fwrite (rl->u.dbrec.buf, rl->u.dbrec.size, 1, outf) < 1)
3027 Tcl_AppendResult (interp, "cannot write", NULL);
3028 return ir_tcl_error_exec (interp, argc, argv);
3035 Tcl_AppendResult (interp, "cannot write ", NULL);
3036 return ir_tcl_error_exec (interp, argc, argv);
3042 /* ------------------------------------------------------- */
3044 * do_sort: Do sort request
3046 static int do_sort (void *o, Tcl_Interp *interp, int argc, char **argv)
3050 IrTcl_SetObj *obj = o;
3052 char sort_string[64], sort_flags[64];
3055 Z_SortKeySpecList *sksl;
3066 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1], "query\"",
3070 logf (LOG_DEBUG, "sort %s %s", *argv, argv[2]);
3073 Tcl_AppendResult (interp, "not connected", NULL);
3074 return ir_tcl_error_exec (interp, argc, argv);
3076 apdu = zget_APDU (p->odr_out, Z_APDU_sortRequest);
3077 sksl = (Z_SortKeySpecList *) odr_malloc (p->odr_out, sizeof(*sksl));
3078 req = apdu->u.sortRequest;
3080 set_referenceId (p->odr_out, &req->referenceId,
3081 obj->set_inher.referenceId);
3084 req->num_inputResultSetNames = 1;
3085 req->inputResultSetNames = (Z_InternationalString **)
3086 odr_malloc (p->odr_out, sizeof(*req->inputResultSetNames));
3087 req->inputResultSetNames[0] = obj->setName;
3089 req->inputResultSetNames =
3090 (Z_StringList *)odr_malloc (p->odr_out,
3091 sizeof(*req->inputResultSetNames));
3092 req->inputResultSetNames->num_strings = 1;
3093 req->inputResultSetNames->strings =
3094 (char **)odr_malloc (p->odr_out,
3095 sizeof(*req->inputResultSetNames->strings));
3096 req->inputResultSetNames->strings[0] = obj->setName;
3099 req->sortedResultSetName = (char *) obj->setName;
3102 req->sortSequence = sksl;
3103 sksl->num_specs = 0;
3104 sksl->specs = (Z_SortKeySpec **)
3105 odr_malloc (p->odr_out, sizeof(sksl->specs) * 20);
3107 bib1.proto = PROTO_Z3950;
3108 bib1.oclass = CLASS_ATTSET;
3109 bib1.value = VAL_BIB1;
3111 while ((sscanf (arg, "%63s %63s%n", sort_string, sort_flags, &off)) == 2
3115 char *sort_string_sep;
3116 Z_SortKeySpec *sks = (Z_SortKeySpec *)
3117 odr_malloc (p->odr_out, sizeof(*sks));
3118 Z_SortKey *sk = (Z_SortKey *)
3119 odr_malloc (p->odr_out, sizeof(*sk));
3122 sksl->specs[sksl->num_specs++] = sks;
3123 sks->sortElement = (Z_SortElement *)
3124 odr_malloc (p->odr_out, sizeof(*sks->sortElement));
3125 sks->sortElement->which = Z_SortElement_generic;
3126 sks->sortElement->u.generic = sk;
3128 if ((sort_string_sep = strchr (sort_string, '=')))
3130 Z_AttributeElement *el = (Z_AttributeElement *)
3131 odr_malloc (p->odr_out, sizeof(*el));
3132 sk->which = Z_SortKey_sortAttributes;
3133 sk->u.sortAttributes =
3134 (Z_SortAttributes *)
3135 odr_malloc (p->odr_out, sizeof(*sk->u.sortAttributes));
3136 sk->u.sortAttributes->id = oid_ent_to_oid(&bib1, oid);
3137 sk->u.sortAttributes->list =
3139 odr_malloc (p->odr_out, sizeof(*sk->u.sortAttributes->list));
3140 sk->u.sortAttributes->list->num_attributes = 1;
3141 sk->u.sortAttributes->list->attributes =
3142 (Z_AttributeElement **)odr_malloc (p->odr_out,
3143 sizeof(*sk->u.sortAttributes->list->attributes));
3144 sk->u.sortAttributes->list->attributes[0] = el;
3145 el->attributeSet = 0;
3146 el->attributeType = (int *)
3147 odr_malloc (p->odr_out, sizeof(*el->attributeType));
3148 *el->attributeType = atoi (sort_string);
3149 el->which = Z_AttributeValue_numeric;
3150 el->value.numeric = (int *)
3151 odr_malloc (p->odr_out, sizeof(*el->value.numeric));
3152 *el->value.numeric = atoi (sort_string_sep + 1);
3156 sk->which = Z_SortKey_sortField;
3157 sk->u.sortField = (char *)odr_malloc (p->odr_out, strlen(sort_string)+1);
3158 strcpy (sk->u.sortField, sort_string);
3160 sks->sortRelation = (int *)
3161 odr_malloc (p->odr_out, sizeof(*sks->sortRelation));
3162 *sks->sortRelation = Z_SortRelation_ascending;
3163 sks->caseSensitivity = (int *)
3164 odr_malloc (p->odr_out, sizeof(*sks->caseSensitivity));
3165 *sks->caseSensitivity = Z_SortCase_caseSensitive;
3168 sks->which = Z_SortKeySpec_null;
3169 sks->u.null = odr_nullval ();
3171 sks->missingValueAction = NULL;
3174 for (i = 0; sort_flags[i]; i++)
3176 switch (sort_flags[i])
3181 *sks->sortRelation = Z_SortRelation_descending;
3186 *sks->sortRelation = Z_SortRelation_ascending;
3190 *sks->caseSensitivity = Z_SortCase_caseInsensitive;
3194 *sks->caseSensitivity = Z_SortCase_caseSensitive;
3199 if (!sksl->num_specs)
3201 printf ("Missing sort specifications\n");
3204 return ir_tcl_send_APDU (interp, p, apdu, "sort", *argv);
3207 static IrTcl_Method ir_set_method_tab[] = {
3208 { "search", do_search, NULL},
3209 { "searchResponse", do_searchResponse, NULL},
3210 { "presentResponse", do_presentResponse, NULL},
3211 { "searchStatus", do_searchStatus, NULL},
3212 { "presentStatus", do_presentStatus, NULL},
3213 { "nextResultSetPosition", do_nextResultSetPosition, NULL},
3214 { "setName", do_setName, NULL},
3215 { "resultCount", do_resultCount, NULL},
3216 { "numberOfRecordsReturned", do_numberOfRecordsReturned, NULL},
3217 { "present", do_present, NULL},
3218 { "type", do_type, NULL},
3219 { "getMarc", do_getMarc, NULL},
3220 { "getSutrs", do_getSutrs, NULL},
3221 { "getGrs", do_getGrs, NULL},
3222 { "getExplain", do_getExplain, NULL},
3223 { "recordType", do_recordType, NULL},
3224 { "recordElements", do_recordElements, NULL},
3225 { "diag", do_diag, NULL},
3226 { "responseStatus", do_responseStatus, NULL},
3227 { "loadFile", do_loadFile, NULL},
3228 { "saveFile", do_saveFile, NULL},
3229 { "sort", do_sort, NULL },
3230 { "sortResponse", do_sortResponse, NULL},
3231 { "sortStatus", do_sortStatus, NULL},
3236 * ir_set_obj_method: IR Set Object methods
3238 static int ir_set_obj_method (ClientData clientData, Tcl_Interp *interp,
3239 int argc, char **argv)
3241 IrTcl_Methods tabs[3];
3242 IrTcl_SetObj *p = (IrTcl_SetObj *) clientData;
3247 Tcl_AppendResult (interp, wrongArgs, *argv, " method args...\"", NULL);
3250 tabs[0].tab = ir_set_method_tab;
3252 tabs[1].tab = ir_set_c_method_tab;
3253 tabs[1].obj = &p->set_inher;
3256 if (ir_tcl_method (interp, argc, argv, tabs, &r) == TCL_ERROR)
3257 return ir_tcl_method_error (interp, argc, argv, tabs);
3262 * ir_set_obj_delete: IR Set Object disposal
3264 static void ir_set_obj_delete (ClientData clientData)
3266 IrTcl_Methods tabs[3];
3267 IrTcl_SetObj *p = (IrTcl_SetObj *) clientData;
3269 logf (LOG_DEBUG, "ir set delete");
3271 tabs[0].tab = ir_set_method_tab;
3273 tabs[1].tab = ir_set_c_method_tab;
3274 tabs[1].obj = &p->set_inher;
3277 ir_tcl_method (NULL, -1, NULL, tabs, NULL);
3283 * ir_set_obj_init: IR Set Object initialization
3285 static int ir_set_obj_init (ClientData clientData, Tcl_Interp *interp,
3286 int argc, char **argv, ClientData *subData,
3287 ClientData parentData)
3289 IrTcl_Methods tabs[3];
3292 if (argc < 2 || argc > 3)
3294 Tcl_AppendResult (interp, wrongArgs, *argv,
3295 " objSetName ?objParent?\"", NULL);
3298 obj = ir_tcl_malloc (sizeof(*obj));
3299 logf (LOG_DEBUG, "ir set create %s", argv[1]);
3306 obj->parent = (IrTcl_Obj *) parentData;
3308 dst = &obj->set_inher;
3309 src = &obj->parent->set_inher;
3311 if ((dst->num_databaseNames = src->num_databaseNames))
3313 dst->databaseNames =
3314 ir_tcl_malloc (sizeof (*dst->databaseNames)
3315 * (1+dst->num_databaseNames));
3316 for (i = 0; i < dst->num_databaseNames; i++)
3317 if (ir_tcl_strdup (interp, &dst->databaseNames[i],
3318 src->databaseNames[i]) == TCL_ERROR)
3320 dst->databaseNames[i] = NULL;
3323 dst->databaseNames = NULL;
3324 if (ir_tcl_strdup (interp, &dst->queryType, src->queryType)
3328 if (ir_tcl_strdup (interp, &dst->referenceId, src->referenceId)
3332 if (ir_tcl_strdup (interp, &dst->elementSetNames, src->elementSetNames)
3336 if (ir_tcl_strdup (interp, &dst->smallSetElementSetNames,
3337 src->smallSetElementSetNames)
3341 if (ir_tcl_strdup (interp, &dst->mediumSetElementSetNames,
3342 src->mediumSetElementSetNames)
3346 if (src->preferredRecordSyntax &&
3347 (dst->preferredRecordSyntax
3348 = ir_tcl_malloc (sizeof(*dst->preferredRecordSyntax))))
3349 *dst->preferredRecordSyntax = *src->preferredRecordSyntax;
3351 dst->preferredRecordSyntax = NULL;
3352 dst->replaceIndicator = src->replaceIndicator;
3353 dst->smallSetUpperBound = src->smallSetUpperBound;
3354 dst->largeSetLowerBound = src->largeSetLowerBound;
3355 dst->mediumSetPresentNumber = src->mediumSetPresentNumber;
3360 tabs[0].tab = ir_set_method_tab;
3364 if (ir_tcl_method (interp, 0, NULL, tabs, NULL) == TCL_ERROR)
3367 *subData = (ClientData) obj;
3372 * ir_set_obj_mk: IR Set Object creation
3374 static int ir_set_obj_mk (ClientData clientData, Tcl_Interp *interp,
3375 int argc, char **argv)
3378 ClientData parentData = 0;
3383 Tcl_CmdInfo parent_info;
3384 if (!Tcl_GetCommandInfo (interp, argv[2], &parent_info))
3386 Tcl_AppendResult (interp, "no object parent", NULL);
3387 return ir_tcl_error_exec (interp, argc, argv);
3389 parentData = parent_info.clientData;
3391 r = ir_set_obj_init (clientData, interp, argc, argv, &subData, parentData);
3394 Tcl_CreateCommand (interp, argv[1], ir_set_obj_method,
3395 subData, ir_set_obj_delete);
3399 IrTcl_Class ir_set_obj_class = {
3406 /* ------------------------------------------------------- */
3409 * do_scan: Perform scan
3411 static int do_scan (void *o, Tcl_Interp *interp, int argc, char **argv)
3415 IrTcl_ScanObj *obj = o;
3416 IrTcl_Obj *p = obj->parent;
3421 struct ccl_rpn_node *rpn;
3425 #if TCL_MAJOR_VERSION > 8 || (TCL_MAJOR_VERSION == 8 && TCL_MINOR_VERSION > 0)
3433 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
3434 " scanQuery\"", NULL);
3437 #if TCL_MAJOR_VERSION > 8 || (TCL_MAJOR_VERSION == 8 && TCL_MINOR_VERSION > 0)
3438 start_term = Tcl_UtfToExternalDString(0, argv[2], -1, &ds);
3440 start_term = argv[2];
3442 logf (LOG_DEBUG, "scan %s %s", *argv, start_term);
3443 if (!p->set_inher.num_databaseNames)
3445 Tcl_AppendResult (interp, "no databaseNames", NULL);
3446 code = ir_tcl_error_exec (interp, argc, argv);
3451 Tcl_AppendResult (interp, "not connected", NULL);
3452 code = ir_tcl_error_exec (interp, argc, argv);
3456 apdu = zget_APDU (p->odr_out, Z_APDU_scanRequest);
3457 req = apdu->u.scanRequest;
3459 set_referenceId (p->odr_out, &req->referenceId, p->set_inher.referenceId);
3460 req->num_databaseNames = p->set_inher.num_databaseNames;
3461 req->databaseNames = p->set_inher.databaseNames;
3465 if (!(req->termListAndStartPoint =
3466 p_query_scan (p->odr_out, p->protocol_type,
3467 &req->attributeSet, start_term)))
3469 Tcl_AppendResult (interp, "query syntax error", NULL);
3470 code = ir_tcl_error_exec (interp, argc, argv);
3474 rpn = ccl_find_str(p->bibset, start_term, &r, &pos);
3477 Tcl_AppendResult (interp, "ccl syntax error ", ccl_err_msg(r), NULL);
3478 code = ir_tcl_error_exec (interp, argc, argv);
3481 bib1.proto = p->protocol_type;
3482 bib1.oclass = CLASS_ATTSET;
3483 bib1.value = VAL_BIB1;
3485 req->attributeSet = oid_getoidbyent (&bib1);
3486 if (!(req->termListAndStartPoint = ccl_scan_query (p->odr_out, rpn)))
3492 req->stepSize = &obj->stepSize;
3493 req->numberOfTermsRequested = &obj->numberOfTermsRequested;
3494 req->preferredPositionInResponse = &obj->preferredPositionInResponse;
3495 logf (LOG_DEBUG, "stepSize=%d", *req->stepSize);
3496 logf (LOG_DEBUG, "numberOfTermsRequested=%d",
3497 *req->numberOfTermsRequested);
3498 logf (LOG_DEBUG, "preferredPositionInResponse=%d",
3499 *req->preferredPositionInResponse);
3501 code = ir_tcl_send_APDU (interp, p, apdu, "scan", *argv);
3503 #if TCL_MAJOR_VERSION > 8 || (TCL_MAJOR_VERSION == 8 && TCL_MINOR_VERSION > 0)
3504 Tcl_DStringFree (&ds);
3510 * do_scanResponse: add scan response handler
3512 static int do_scanResponse (void *o, Tcl_Interp *interp,
3513 int argc, char **argv)
3515 IrTcl_ScanObj *obj = o;
3519 obj->scanResponse = NULL;
3522 else if (argc == -1)
3523 return ir_tcl_strdel (interp, &obj->scanResponse);
3526 xfree (obj->scanResponse);
3529 if (ir_tcl_strdup (interp, &obj->scanResponse, argv[2])
3534 obj->scanResponse = NULL;
3540 * do_stepSize: Set/get replace Step Size
3542 static int do_stepSize (void *obj, Tcl_Interp *interp,
3543 int argc, char **argv)
3545 IrTcl_ScanObj *p = obj;
3551 return ir_tcl_get_set_int (&p->stepSize, interp, argc, argv);
3555 * do_numberOfTermsRequested: Set/get Number of Terms requested
3557 static int do_numberOfTermsRequested (void *obj, Tcl_Interp *interp,
3558 int argc, char **argv)
3560 IrTcl_ScanObj *p = obj;
3564 p->numberOfTermsRequested = 20;
3567 return ir_tcl_get_set_int (&p->numberOfTermsRequested, interp, argc, argv);
3572 * do_preferredPositionInResponse: Set/get preferred Position
3574 static int do_preferredPositionInResponse (void *obj, Tcl_Interp *interp,
3575 int argc, char **argv)
3577 IrTcl_ScanObj *p = obj;
3581 p->preferredPositionInResponse = 1;
3584 return ir_tcl_get_set_int (&p->preferredPositionInResponse, interp,
3589 * do_scanStatus: Get scan status
3591 static int do_scanStatus (void *obj, Tcl_Interp *interp,
3592 int argc, char **argv)
3594 IrTcl_ScanObj *p = obj;
3598 return ir_tcl_get_set_int (&p->scanStatus, interp, argc, argv);
3602 * do_numberOfEntriesReturned: Get number of Entries returned
3604 static int do_numberOfEntriesReturned (void *obj, Tcl_Interp *interp,
3605 int argc, char **argv)
3607 IrTcl_ScanObj *p = obj;
3611 return ir_tcl_get_set_int (&p->numberOfEntriesReturned, interp,
3616 * do_positionOfTerm: Get position of Term
3618 static int do_positionOfTerm (void *obj, Tcl_Interp *interp,
3619 int argc, char **argv)
3621 IrTcl_ScanObj *p = obj;
3625 return ir_tcl_get_set_int (&p->positionOfTerm, interp, argc, argv);
3629 * do_scanLine: get Scan Line (surrogate or normal) after response
3631 static int do_scanLine (void *obj, Tcl_Interp *interp, int argc, char **argv)
3633 IrTcl_ScanObj *p = obj;
3639 p->entries_flag = 0;
3641 p->nonSurrogateDiagnosticNum = 0;
3642 p->nonSurrogateDiagnosticList = 0;
3645 else if (argc == -1)
3647 p->entries_flag = 0;
3648 /* release entries */
3651 ir_deleteDiags (&p->nonSurrogateDiagnosticList,
3652 &p->nonSurrogateDiagnosticNum);
3657 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
3658 " position\"", NULL);
3661 if (Tcl_GetInt (interp, argv[2], &i) == TCL_ERROR)
3663 if (!p->entries_flag || !p->entries || i >= p->num_entries || i < 0)
3665 switch (p->entries[i].which)
3667 case Z_Entry_termInfo:
3668 Tcl_AppendElement (interp, "T");
3669 if (p->entries[i].u.term.buf)
3670 Tcl_AppendElement (interp, p->entries[i].u.term.buf);
3672 Tcl_AppendElement (interp, "");
3673 sprintf (numstr, "%d", p->entries[i].u.term.globalOccurrences);
3674 Tcl_AppendElement (interp, numstr);
3676 case Z_Entry_surrogateDiagnostic:
3677 Tcl_AppendElement (interp, "SD");
3678 return ir_diagResult (interp, p->entries[i].u.diag.list,
3679 p->entries[i].u.diag.num);
3685 static IrTcl_Method ir_scan_method_tab[] = {
3686 { "scan", do_scan, NULL},
3687 { "scanResponse", do_scanResponse, NULL},
3688 { "stepSize", do_stepSize, NULL},
3689 { "numberOfTermsRequested", do_numberOfTermsRequested, NULL},
3690 { "preferredPositionInResponse", do_preferredPositionInResponse, NULL},
3691 { "scanStatus", do_scanStatus, NULL},
3692 { "numberOfEntriesReturned", do_numberOfEntriesReturned, NULL},
3693 { "positionOfTerm", do_positionOfTerm, NULL},
3694 { "scanLine", do_scanLine, NULL},
3699 * ir_scan_obj_method: IR Scan Object methods
3701 static int ir_scan_obj_method (ClientData clientData, Tcl_Interp *interp,
3702 int argc, char **argv)
3704 IrTcl_Methods tabs[2];
3709 Tcl_AppendResult (interp, wrongArgs, *argv, " method args...\"", NULL);
3712 tabs[0].tab = ir_scan_method_tab;
3713 tabs[0].obj = clientData;
3716 if (ir_tcl_method (interp, argc, argv, tabs, &r) == TCL_ERROR)
3717 return ir_tcl_method_error (interp, argc, argv, tabs);
3722 * ir_scan_obj_delete: IR Scan Object disposal
3724 static void ir_scan_obj_delete (ClientData clientData)
3726 IrTcl_Methods tabs[2];
3727 IrTcl_ScanObj *obj = (IrTcl_ScanObj *) clientData;
3729 tabs[0].tab = ir_scan_method_tab;
3733 ir_tcl_method (NULL, -1, NULL, tabs, NULL);
3738 * ir_scan_obj_mk: IR Scan Object creation
3740 static int ir_scan_obj_mk (ClientData clientData, Tcl_Interp *interp,
3741 int argc, char **argv)
3743 Tcl_CmdInfo parent_info;
3745 IrTcl_Methods tabs[2];
3749 Tcl_AppendResult (interp, wrongArgs, *argv,
3750 "objScanName objParentName\"", NULL);
3753 logf (LOG_DEBUG, "ir scan create %s", argv[1]);
3754 if (!Tcl_GetCommandInfo (interp, argv[2], &parent_info))
3756 Tcl_AppendResult (interp, "no object parent", NULL);
3757 return ir_tcl_error_exec (interp, argc, argv);
3759 obj = ir_tcl_malloc (sizeof(*obj));
3760 obj->parent = (IrTcl_Obj *) parent_info.clientData;
3762 tabs[0].tab = ir_scan_method_tab;
3766 if (ir_tcl_method (interp, 0, NULL, tabs, NULL) == TCL_ERROR)
3768 Tcl_CreateCommand (interp, argv[1], ir_scan_obj_method,
3769 (ClientData) obj, ir_scan_obj_delete);
3773 /* ------------------------------------------------------- */
3776 * ir_log_init_proc: set yaz log level
3778 static int ir_log_init_proc (ClientData clientData, Tcl_Interp *interp,
3779 int argc, char **argv)
3781 if (argc <= 1 || argc > 4)
3783 Tcl_AppendResult (interp, wrongArgs, *argv,
3784 " ?level ?prefix ?filename\"", NULL);
3788 yaz_log_init (yaz_log_mask_str (argv[1]), "", NULL);
3790 yaz_log_init (yaz_log_mask_str (argv[1]), argv[2], NULL);
3792 yaz_log_init (yaz_log_mask_str (argv[1]), argv[2], argv[3]);
3797 * ir_log_proc: log yaz message
3799 static int ir_log_proc (ClientData clientData, Tcl_Interp *interp,
3800 int argc, char **argv)
3805 Tcl_AppendResult (interp, wrongArgs, *argv,
3806 " level string\"", NULL);
3809 mask = yaz_log_mask_str_x (argv[1], 0);
3810 logf (LOG_DEBUG, "%s", argv[2]);
3816 * ir_version: log ir version
3818 static int ir_version (ClientData clientData, Tcl_Interp *interp,
3819 int argc, char **argv)
3821 Tcl_AppendElement (interp, IR_TCL_VERSION);
3822 Tcl_AppendElement (interp, YAZ_VERSION);
3827 /* ------------------------------------------------------- */
3828 static void ir_initResponse (void *obj, Z_InitResponse *initrs)
3832 p->initResult = *initrs->result ? 1 : 0;
3833 if (!*initrs->result)
3834 logf (LOG_DEBUG, "Connection rejected by target");
3836 logf (LOG_DEBUG, "Connection accepted by target");
3838 get_referenceId (&p->set_inher.referenceId, initrs->referenceId);
3840 xfree (p->targetImplementationId);
3841 ir_tcl_strdup (p->interp, &p->targetImplementationId,
3842 initrs->implementationId);
3843 xfree (p->targetImplementationName);
3844 ir_tcl_strdup (p->interp, &p->targetImplementationName,
3845 initrs->implementationName);
3846 xfree (p->targetImplementationVersion);
3847 ir_tcl_strdup (p->interp, &p->targetImplementationVersion,
3848 initrs->implementationVersion);
3850 p->maximumRecordSize = *initrs->maximumRecordSize;
3851 p->preferredMessageSize = *initrs->preferredMessageSize;
3853 memcpy (&p->options, initrs->options, sizeof(initrs->options));
3854 memcpy (&p->protocolVersion, initrs->protocolVersion,
3855 sizeof(initrs->protocolVersion));
3856 xfree (p->userInformationField);
3857 p->userInformationField = NULL;
3858 if (initrs->userInformationField)
3862 if (initrs->userInformationField->which == ODR_EXTERNAL_octet &&
3863 (p->userInformationField =
3864 ir_tcl_malloc ((len =
3865 initrs->userInformationField->
3866 u.octet_aligned->len) +1)))
3868 memcpy (p->userInformationField,
3869 initrs->userInformationField->u.octet_aligned->buf,
3871 (p->userInformationField)[len] = '\0';
3876 static void ir_deleteDiags (IrTcl_Diagnostic **dst_list, int *dst_num)
3879 for (i = 0; i<*dst_num; i++)
3880 xfree ((*dst_list)[i].addinfo);
3886 static void ir_handleDiags (IrTcl_Diagnostic **dst_list, int *dst_num,
3887 Z_DiagRec **list, int num)
3890 char *addinfo = NULL;
3893 *dst_list = ir_tcl_malloc (sizeof(**dst_list) * num);
3894 for (i = 0; i<num; i++)
3897 switch (list[i]->which)
3899 case Z_DiagRec_defaultFormat:
3900 (*dst_list)[i].condition = *list[i]->u.defaultFormat->condition;
3902 switch (list[i]->u.defaultFormat->which)
3904 case Z_DefaultDiagFormat_v2Addinfo:
3905 addinfo = list[i]->u.defaultFormat->u.v2Addinfo;
3907 case Z_DefaultDiagFormat_v3Addinfo:
3908 addinfo = list[i]->u.defaultFormat->u.v3Addinfo;
3912 addinfo = list[i]->u.defaultFormat->addinfo;
3915 ((*dst_list)[i].addinfo = ir_tcl_malloc (strlen(addinfo)+1)))
3916 strcpy ((*dst_list)[i].addinfo, addinfo);
3917 cp = diagbib1_str ((*dst_list)[i].condition);
3918 logf (LOG_DEBUG, "Diag %d %s %s", (*dst_list)[i].condition,
3919 cp ? cp : "", addinfo ? addinfo : "");
3922 (*dst_list)[i].addinfo = NULL;
3923 (*dst_list)[i].condition = 0;
3928 static void ir_handleDBRecord (IrTcl_Obj *p, IrTcl_RecordList *rl,
3931 struct oident *ident;
3932 Z_ext_typeent *etype;
3934 logf (LOG_DEBUG, "handleDBRecord size=%d", oe->u.octet_aligned->len);
3935 rl->u.dbrec.size = oe->u.octet_aligned->len;
3936 rl->u.dbrec.buf = NULL;
3938 if ((ident = oid_getentbyoid (oe->direct_reference)))
3939 rl->u.dbrec.type = ident->value;
3941 rl->u.dbrec.type = VAL_USMARC;
3943 if (ident && (oe->which == Z_External_single ||
3944 oe->which == Z_External_octet)
3945 && (etype = z_ext_getentbyref (ident->value)))
3949 odr_setbuf (p->odr_in, (char*) oe->u.octet_aligned->buf,
3950 oe->u.octet_aligned->len, 0);
3951 if (!(*etype->fun)(p->odr_in, (char **) &rr, 0, 0))
3953 rl->u.dbrec.type = VAL_NONE;
3956 switch (etype->what)
3958 case Z_External_sutrs:
3959 logf (LOG_DEBUG, "Z_External_sutrs");
3961 if ((rl->u.dbrec.buf = ir_tcl_malloc (oe->u.sutrs->len+1)))
3963 memcpy (rl->u.dbrec.buf, oe->u.sutrs->buf,
3965 rl->u.dbrec.buf[oe->u.sutrs->len] = '\0';
3967 rl->u.dbrec.size = oe->u.sutrs->len;
3969 case Z_External_grs1:
3970 logf (LOG_DEBUG, "Z_External_grs1");
3972 ir_tcl_grs_mk (oe->u.grs1, &rl->u.dbrec.u.grs1);
3974 case Z_External_explainRecord:
3975 logf (LOG_DEBUG, "Z_External_explainRecord");
3976 if ((rl->u.dbrec.buf = ir_tcl_malloc (rl->u.dbrec.size)))
3978 memcpy (rl->u.dbrec.buf, oe->u.octet_aligned->buf,
3986 if (oe->which == Z_External_octet && rl->u.dbrec.size > 0)
3988 char *buf = (char*) oe->u.octet_aligned->buf;
3989 if ((rl->u.dbrec.buf = ir_tcl_malloc (rl->u.dbrec.size)))
3990 memcpy (rl->u.dbrec.buf, buf, rl->u.dbrec.size);
3992 else if (rl->u.dbrec.type == VAL_SUTRS &&
3993 oe->which == Z_External_sutrs)
3995 if ((rl->u.dbrec.buf = ir_tcl_malloc (oe->u.sutrs->len+1)))
3997 memcpy (rl->u.dbrec.buf, oe->u.sutrs->buf,
3999 rl->u.dbrec.buf[oe->u.sutrs->len] = '\0';
4001 rl->u.dbrec.size = oe->u.sutrs->len;
4003 else if (rl->u.dbrec.type == VAL_GRS1 &&
4004 oe->which == Z_External_grs1)
4006 ir_tcl_grs_mk (oe->u.grs1, &rl->u.dbrec.u.grs1);
4011 static void ir_handleZRecords (void *o, Z_Records *zrs, IrTcl_SetObj *setobj,
4012 const char *elements)
4017 IrTcl_RecordList *rl;
4019 setobj->which = zrs->which;
4020 setobj->recordFlag = 1;
4022 ir_deleteDiags (&setobj->nonSurrogateDiagnosticList,
4023 &setobj->nonSurrogateDiagnosticNum);
4024 if (zrs->which == Z_Records_DBOSD)
4026 int num_rec = zrs->u.databaseOrSurDiagnostics->num_records;
4028 if (num_rec != setobj->numberOfRecordsReturned)
4030 logf (LOG_WARN, "numberOfRecordsReturned=%d but num records=%d",
4031 setobj->numberOfRecordsReturned, num_rec);
4032 setobj->numberOfRecordsReturned = num_rec;
4035 for (offset = 0; offset < num_rec; offset++)
4037 Z_NamePlusRecord *znpr = zrs->u.databaseOrSurDiagnostics->
4040 rl = new_IR_record (setobj, setobj->start + offset, znpr->which,
4042 if (rl->which == Z_NamePlusRecord_surrogateDiagnostic)
4043 ir_handleDiags (&rl->u.surrogateDiagnostics.list,
4044 &rl->u.surrogateDiagnostics.num,
4045 &znpr->u.surrogateDiagnostic,
4048 ir_handleDBRecord (p, rl,
4049 (Z_External*) (znpr->u.databaseRecord));
4052 else if (zrs->which == Z_Records_multipleNSD)
4054 logf (LOG_DEBUG, "multipleNonSurrogateDiagnostic %d",
4055 zrs->u.multipleNonSurDiagnostics->num_diagRecs);
4056 setobj->numberOfRecordsReturned = 0;
4057 ir_handleDiags (&setobj->nonSurrogateDiagnosticList,
4058 &setobj->nonSurrogateDiagnosticNum,
4059 zrs->u.multipleNonSurDiagnostics->diagRecs,
4060 zrs->u.multipleNonSurDiagnostics->num_diagRecs);
4065 Z_DiagRec dr, *dr_p = &dr;
4066 dr.which = Z_DiagRec_defaultFormat;
4067 dr.u.defaultFormat = zrs->u.nonSurrogateDiagnostic;
4069 Z_DiagRec *dr_p = zrs->u.nonSurrogateDiagnostic;
4071 logf (LOG_DEBUG, "NonSurrogateDiagnostic");
4073 setobj->numberOfRecordsReturned = 0;
4074 ir_handleDiags (&setobj->nonSurrogateDiagnosticList,
4075 &setobj->nonSurrogateDiagnosticNum,
4080 static void ir_searchResponse (void *o, Z_SearchResponse *searchrs,
4081 IrTcl_SetObj *setobj)
4083 Z_Records *zrs = searchrs->records;
4085 logf (LOG_DEBUG, "Received search response");
4088 logf (LOG_DEBUG, "Search response, no object!");
4091 setobj->searchStatus = *searchrs->searchStatus;
4092 get_referenceId (&setobj->set_inher.referenceId, searchrs->referenceId);
4093 setobj->resultCount = *searchrs->resultCount;
4094 if (searchrs->presentStatus)
4095 setobj->presentStatus = *searchrs->presentStatus;
4097 setobj->presentStatus = Z_RES_NONE;
4098 if (searchrs->nextResultSetPosition)
4099 setobj->nextResultSetPosition = *searchrs->nextResultSetPosition;
4101 logf (LOG_DEBUG, "status %d hits %d",
4102 setobj->searchStatus, setobj->resultCount);
4106 if (setobj->resultCount <= setobj->set_inher.smallSetUpperBound)
4107 es = setobj->set_inher.smallSetElementSetNames;
4109 es = setobj->set_inher.mediumSetElementSetNames;
4110 setobj->numberOfRecordsReturned = *searchrs->numberOfRecordsReturned;
4111 ir_handleZRecords (o, zrs, setobj, es);
4115 setobj->numberOfRecordsReturned = 0;
4116 setobj->recordFlag = 0;
4121 static void ir_presentResponse (void *o, Z_PresentResponse *presrs,
4122 IrTcl_SetObj *setobj)
4124 Z_Records *zrs = presrs->records;
4126 logf (LOG_DEBUG, "Received present response");
4129 logf (LOG_DEBUG, "Present response, no object!");
4132 setobj->presentStatus = *presrs->presentStatus;
4133 get_referenceId (&setobj->set_inher.referenceId, presrs->referenceId);
4134 setobj->nextResultSetPosition = *presrs->nextResultSetPosition;
4137 setobj->numberOfRecordsReturned = *presrs->numberOfRecordsReturned;
4138 ir_handleZRecords (o, zrs, setobj, setobj->set_inher.elementSetNames);
4142 setobj->numberOfRecordsReturned = 0;
4143 setobj->recordFlag = 0;
4144 logf (LOG_DEBUG, "No records!");
4148 static void ir_scanResponse (void *o, Z_ScanResponse *scanrs,
4149 IrTcl_ScanObj *scanobj)
4153 logf (LOG_DEBUG, "Received scanResponse");
4155 get_referenceId (&p->set_inher.referenceId, scanrs->referenceId);
4156 scanobj->scanStatus = *scanrs->scanStatus;
4157 logf (LOG_DEBUG, "scanStatus=%d", scanobj->scanStatus);
4159 if (scanrs->stepSize)
4160 scanobj->stepSize = *scanrs->stepSize;
4161 logf (LOG_DEBUG, "stepSize=%d", scanobj->stepSize);
4163 scanobj->numberOfEntriesReturned = *scanrs->numberOfEntriesReturned;
4164 logf (LOG_DEBUG, "numberOfEntriesReturned=%d",
4165 scanobj->numberOfEntriesReturned);
4167 if (scanrs->positionOfTerm)
4168 scanobj->positionOfTerm = *scanrs->positionOfTerm;
4170 scanobj->positionOfTerm = -1;
4171 logf (LOG_DEBUG, "positionOfTerm=%d", scanobj->positionOfTerm);
4173 xfree (scanobj->entries);
4174 scanobj->entries = NULL;
4175 scanobj->num_entries = 0;
4176 scanobj->entries_flag = 0;
4178 ir_deleteDiags (&scanobj->nonSurrogateDiagnosticList,
4179 &scanobj->nonSurrogateDiagnosticNum);
4180 if (scanrs->entries)
4185 scanobj->entries_flag = 1;
4186 if (scanrs->entries)
4188 scanobj->num_entries = scanrs->entries->num_entries;
4189 scanobj->entries = ir_tcl_malloc (scanobj->num_entries *
4190 sizeof(*scanobj->entries));
4191 ze = scanrs->entries->entries;
4193 for (i=0; i<scanobj->num_entries; i++, ze++)
4195 scanobj->entries[i].which = (*ze)->which;
4196 switch ((*ze)->which)
4198 case Z_Entry_termInfo:
4199 if ((*ze)->u.termInfo->term->which == Z_Term_general)
4201 int l = (*ze)->u.termInfo->term->u.general->len;
4202 scanobj->entries[i].u.term.buf = ir_tcl_malloc (1+l);
4203 memcpy (scanobj->entries[i].u.term.buf,
4204 (*ze)->u.termInfo->term->u.general->buf,
4206 scanobj->entries[i].u.term.buf[l] = '\0';
4209 scanobj->entries[i].u.term.buf = NULL;
4210 if ((*ze)->u.termInfo->globalOccurrences)
4211 scanobj->entries[i].u.term.globalOccurrences =
4212 *(*ze)->u.termInfo->globalOccurrences;
4214 scanobj->entries[i].u.term.globalOccurrences = 0;
4216 case Z_Entry_surrogateDiagnostic:
4217 ir_handleDiags (&scanobj->entries[i].u.diag.list,
4218 &scanobj->entries[i].u.diag.num,
4219 &(*ze)->u.surrogateDiagnostic,
4224 if (scanrs->entries->nonsurrogateDiagnostics)
4225 ir_handleDiags (&scanobj->nonSurrogateDiagnosticList,
4226 &scanobj->nonSurrogateDiagnosticNum,
4227 scanrs->entries->nonsurrogateDiagnostics,
4228 scanrs->entries->num_nonsurrogateDiagnostics);
4233 static void ir_sortResponse (void *o, Z_SortResponse *sortrs,
4234 IrTcl_SetObj *setobj)
4238 logf (LOG_DEBUG, "Received sortResponse");
4243 purge_IR_records (setobj);
4245 get_referenceId (&p->set_inher.referenceId, sortrs->referenceId);
4247 setobj->sortStatus = *sortrs->sortStatus;
4249 ir_deleteDiags (&setobj->nonSurrogateDiagnosticList,
4250 &setobj->nonSurrogateDiagnosticNum);
4252 if (sortrs->diagnostics)
4253 ir_handleDiags (&setobj->nonSurrogateDiagnosticList,
4254 &setobj->nonSurrogateDiagnosticNum,
4255 sortrs->diagnostics,
4256 sortrs->num_diagnostics);
4258 if (sortrs->diagnostics)
4259 ir_handleDiags (&setobj->nonSurrogateDiagnosticList,
4260 &setobj->nonSurrogateDiagnosticNum,
4261 sortrs->diagnostics->diagRecs,
4262 sortrs->diagnostics->num_diagRecs);
4267 * ir_select_read: handle incoming packages
4269 static void ir_select_read (ClientData clientData)
4271 IrTcl_Obj *p = (IrTcl_Obj *) clientData;
4276 Tcl_CmdInfo cmd_info;
4277 const char *apdu_call;
4279 logf(LOG_DEBUG, "Read handler fd=%d", cs_fileno(p->cs_link));
4280 if (p->state == IR_TCL_R_Connecting)
4282 logf(LOG_DEBUG, "read: connect");
4283 r = cs_rcvconnect (p->cs_link);
4286 logf (LOG_WARN, "cs_rcvconnect returned 1");
4289 p->state = IR_TCL_R_Idle;
4291 ir_select_remove_write (cs_fileno (p->cs_link), p);
4294 logf (LOG_DEBUG, "cs_rcvconnect error");
4295 ir_tcl_disconnect (p);
4298 p->failInfo = IR_TCL_FAIL_CONNECT;
4299 ir_tcl_eval (p->interp, p->failback);
4301 ir_obj_delete ((ClientData) p);
4305 ir_tcl_eval (p->interp, p->callback);
4306 if (p->ref_count == 2 && p->cs_link && p->request_queue
4307 && p->state == IR_TCL_R_Idle)
4308 ir_tcl_send_q (p, p->request_queue, "x");
4309 ir_obj_delete ((ClientData) p);
4314 p->state = IR_TCL_R_Reading;
4316 /* read incoming APDU */
4317 if ((r=cs_get (p->cs_link, &p->buf_in, &p->len_in)) == 1)
4319 logf(LOG_DEBUG, "PDU Fraction read");
4322 /* signal one more use of ir object - callbacks must not
4323 release the ir memory (p pointer) */
4327 logf (LOG_DEBUG, "cs_get failed, code %d", r);
4328 ir_tcl_disconnect (p);
4331 p->failInfo = IR_TCL_FAIL_READ;
4332 ir_tcl_eval (p->interp, p->failback);
4334 /* release ir object now if callback deleted it */
4335 ir_obj_delete ((ClientData) p);
4338 /* got complete APDU. Now decode */
4341 odr_setbuf (p->odr_in, p->buf_in, r, 0);
4342 logf (LOG_DEBUG, "cs_get ok, total size %d", r);
4343 if (!z_APDU (p->odr_in, &apdu, 0, 0))
4345 logf (LOG_DEBUG, "cs_get failed: %s",
4346 odr_errmsg (odr_geterror (p->odr_in)));
4347 ir_tcl_disconnect (p);
4350 p->failInfo = IR_TCL_FAIL_IN_APDU;
4351 p->apduOffset = odr_offset (p->odr_in);
4352 ir_tcl_eval (p->interp, p->failback);
4354 /* release ir object now if failback deleted it */
4355 ir_obj_delete ((ClientData) p);
4358 /* handle APDU and invoke callback */
4359 rq = p->request_queue;
4362 logf (LOG_FATAL, "Internal error. No queue entry");
4365 object_name = rq->object_name;
4366 logf (LOG_DEBUG, "Object %s", object_name);
4368 if (Tcl_GetCommandInfo (p->interp, object_name, &cmd_info))
4372 case Z_APDU_initResponse:
4373 p->eventType = "init";
4374 ir_initResponse (p, apdu->u.initResponse);
4375 apdu_call = p->initResponse;
4377 case Z_APDU_searchResponse:
4378 p->eventType = "search";
4379 ir_searchResponse (p, apdu->u.searchResponse,
4380 (IrTcl_SetObj *) cmd_info.clientData);
4381 apdu_call = ((IrTcl_SetObj *)
4382 cmd_info.clientData)->searchResponse;
4384 case Z_APDU_presentResponse:
4385 p->eventType = "present";
4386 ir_presentResponse (p, apdu->u.presentResponse,
4387 (IrTcl_SetObj *) cmd_info.clientData);
4388 apdu_call = ((IrTcl_SetObj *)
4389 cmd_info.clientData)->presentResponse;
4391 case Z_APDU_scanResponse:
4392 p->eventType = "scan";
4393 ir_scanResponse (p, apdu->u.scanResponse,
4394 (IrTcl_ScanObj *) cmd_info.clientData);
4395 apdu_call = ((IrTcl_ScanObj *)
4396 cmd_info.clientData)->scanResponse;
4398 case Z_APDU_sortResponse:
4399 p->eventType = "sort";
4400 ir_sortResponse (p, apdu->u.sortResponse,
4401 (IrTcl_SetObj *) cmd_info.clientData);
4402 apdu_call = ((IrTcl_SetObj *)
4403 cmd_info.clientData)->sortResponse;
4406 logf (LOG_WARN, "Received unknown APDU type (%d)",
4408 ir_tcl_disconnect (p);
4411 p->failInfo = IR_TCL_FAIL_UNKNOWN_APDU;
4412 ir_tcl_eval (p->interp, p->failback);
4417 p->request_queue = rq->next;
4418 p->state = IR_TCL_R_Idle;
4421 ir_tcl_eval (p->interp, apdu_call);
4422 else if (rq->callback)
4423 ir_tcl_eval (p->interp, rq->callback);
4424 xfree (rq->buf_out);
4425 xfree (rq->callback);
4426 xfree (rq->object_name);
4428 odr_reset (p->odr_in);
4429 if (p->ref_count == 1)
4431 ir_obj_delete ((ClientData) p);
4434 ir_obj_delete ((ClientData) p);
4435 } while (p->cs_link && cs_more (p->cs_link));
4436 if (p->cs_link && p->request_queue && p->state == IR_TCL_R_Idle)
4437 ir_tcl_send_q (p, p->request_queue, "x");
4441 * ir_select_write: handle outgoing packages - not yet written.
4443 static int ir_select_write (ClientData clientData)
4445 IrTcl_Obj *p = (IrTcl_Obj *) clientData;
4449 logf (LOG_DEBUG, "Write handler fd=%d", cs_fileno(p->cs_link));
4450 if (p->state == IR_TCL_R_Connecting)
4452 logf(LOG_DEBUG, "write: connect");
4453 r = cs_rcvconnect (p->cs_link);
4456 logf (LOG_DEBUG, "cs_rcvconnect returned 1");
4459 p->state = IR_TCL_R_Idle;
4461 ir_select_remove_write (cs_fileno (p->cs_link), p);
4464 logf (LOG_DEBUG, "cs_rcvconnect error");
4465 ir_tcl_disconnect (p);
4468 p->failInfo = IR_TCL_FAIL_CONNECT;
4469 ir_tcl_eval (p->interp, p->failback);
4471 ir_obj_delete ((ClientData) p);
4475 ir_tcl_eval (p->interp, p->callback);
4476 ir_obj_delete ((ClientData) p);
4479 rq = p->request_queue;
4480 if (!rq || !rq->buf_out)
4483 if ((r=cs_put (p->cs_link, rq->buf_out, rq->len_out)) < 0)
4485 logf (LOG_DEBUG, "cs_put write fail");
4487 xfree (rq->buf_out);
4489 ir_tcl_disconnect (p);
4492 p->failInfo = IR_TCL_FAIL_WRITE;
4493 ir_tcl_eval (p->interp, p->failback);
4495 ir_obj_delete ((ClientData) p);
4497 else if (r == 0) /* remove select bit */
4499 logf (LOG_DEBUG, "Write completed");
4500 p->state = IR_TCL_R_Waiting;
4501 ir_select_remove_write (cs_fileno (p->cs_link), p);
4502 xfree (rq->buf_out);
4508 static void ir_select_notify (ClientData clientData, int r, int w, int e)
4512 if (!ir_select_write (clientData) && r)
4513 ir_select_read (clientData);
4517 ir_select_read (clientData);
4521 /*----------------------------------------------------------- */
4525 * This wrapper function is used by Windows to invoke the
4526 * initialization code for the DLL. If we are compiling
4527 * with Visual C++, this routine will be renamed to DllMain.
4539 DllEntryPoint(hInst, reason, reserved)
4540 HINSTANCE hInst; /* Library instance handle. */
4541 DWORD reason; /* Reason this function is being called. */
4542 LPVOID reserved; /* Not used. */
4548 /* ------------------------------------------------------- */
4550 * Irtcl_init: Registration of TCL commands.
4552 EXPORT (int,Irtcl_Init) (Tcl_Interp *interp)
4554 Tcl_CreateCommand (interp, "ir", ir_obj_mk, (ClientData) NULL,
4555 (Tcl_CmdDeleteProc *) NULL);
4556 Tcl_CreateCommand (interp, "ir-set", ir_set_obj_mk,
4557 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
4558 Tcl_CreateCommand (interp, "ir-scan", ir_scan_obj_mk,
4559 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
4560 Tcl_CreateCommand (interp, "ir-log-init", ir_log_init_proc,
4561 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
4562 Tcl_CreateCommand (interp, "ir-log", ir_log_proc,
4563 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
4564 Tcl_CreateCommand (interp, "ir-version", ir_version, (ClientData) NULL,
4565 (Tcl_CmdDeleteProc *) NULL);