2 * IR toolkit for tcl/tk
3 * (c) Index Data 1995-2003
4 * See the file LICENSE for details.
7 * Revision 1.122 2003-03-05 18:02:08 adam
8 * Fix bug with idAuthentication that didn't work for empty group.
10 * Revision 1.121 2003/01/30 13:27:07 adam
11 * Changed version to 1.4.1. Added WIN32 version resource.
12 * IrTcl ignores unexpected PDU's, rather than die.
14 * Revision 1.120 2002/03/20 14:48:54 adam
15 * implemented USR.1 SearchResult-1
17 * Revision 1.119 2001/12/03 00:31:06 adam
18 * Towards 1.4. Configure updates.
20 * Revision 1.118 2001/03/27 16:27:21 adam
21 * Fixed bug in do_responseStatus.
23 * Revision 1.117 2001/03/26 11:39:34 adam
24 * Fixed bug in ir_deleteDiags - crash when receiving multiple diags.
26 * Revision 1.116 2001/02/09 11:58:04 adam
27 * Updated for Tcl8.1 and higher where internal encoding is UTF-8.
29 * Revision 1.115 2000/09/13 12:18:49 adam
30 * Logging utility patch (YAZ version 1.7).
32 * Revision 1.114 1999/05/17 20:37:41 adam
33 * Fixed problem with ASN code.
35 * Revision 1.113 1999/04/20 10:01:46 adam
36 * Modified calls to ODR encoders/decoders (name argument).
38 * Revision 1.112 1999/03/22 06:51:34 adam
41 * Revision 1.111 1999/02/11 11:30:09 adam
44 * Revision 1.110 1998/10/20 15:15:31 adam
45 * Changed scan response handler.
47 * Revision 1.109 1998/10/13 21:23:26 adam
48 * Fixed searchStatus method.
50 * Revision 1.108 1998/10/12 11:48:08 adam
51 * Removed printf call.
53 * Revision 1.107 1998/06/10 13:00:46 adam
54 * Added ir-version command.
56 * Revision 1.106 1998/05/20 12:25:35 adam
57 * Fixed bug that occurred in rare cases when encoding of incoming
60 * Revision 1.105 1998/04/02 14:31:08 adam
61 * This version works with compiled ASN.1 code.
63 * Revision 1.104 1998/02/27 14:26:07 adam
64 * Changed client so that it still works if target sets numberOfRecords
65 * in response to an illegal value.
67 * Revision 1.103 1997/11/19 11:22:10 adam
68 * Object identifiers can be accessed in GRS-1 records.
70 * Revision 1.102 1997/09/17 12:22:40 adam
71 * Changed to use YAZ version 1.4. The new comstack utility, cs_straddr,
74 * Revision 1.101 1997/09/09 10:19:53 adam
75 * New MSV5.0 port with fewer warnings.
77 * Revision 1.100 1997/05/01 15:04:05 adam
78 * Added ir-log command.
80 * Revision 1.99 1997/04/30 07:24:47 adam
81 * Spell fix of an error message.
83 * Revision 1.98 1997/04/13 18:57:20 adam
84 * Better error reporting and aligned with Tcl/Tk style.
85 * Rework of notifier code with Tcl_File handles.
87 * Revision 1.97 1996/11/14 17:11:07 adam
88 * Added Explain documentaion.
90 * Revision 1.96 1996/10/08 13:02:50 adam
91 * When dealing with records, odr_choice_enable_bias function is used to
92 * prevent decoding of externals.
94 * Revision 1.95 1996/09/13 10:51:49 adam
95 * Bug fix: ir_tcl_select_set called Tcl_GetFile at disconnect.
97 * Revision 1.94 1996/08/21 13:32:53 adam
98 * Implemented saveFile method and extended loadFile method to work with it.
100 * Revision 1.93 1996/08/16 15:07:45 adam
101 * First work on Explain.
103 * Revision 1.92 1996/08/09 15:33:07 adam
104 * Modified the code to use tk4.1/tcl7.5 patch level 1. The time-driven
105 * polling is no longer activated on Windows since asynchrounous I/O works
108 * Revision 1.91 1996/07/03 13:31:11 adam
109 * The xmalloc/xfree functions from YAZ are used to manage memory.
111 * Revision 1.90 1996/06/27 14:21:00 adam
112 * Yet another Windows port.
114 * Revision 1.89 1996/06/11 15:27:15 adam
115 * Event type set to connect a little earlier in the do_connect function.
117 * Revision 1.88 1996/06/03 09:04:22 adam
118 * Changed a few logf calls.
120 * Revision 1.87 1996/05/29 06:37:51 adam
121 * Function ir_tcl_get_grs_r enhanced so that specific elements can be
124 * Revision 1.86 1996/03/20 13:54:04 adam
125 * The Tcl_File structure is only manipulated in the Tk-event interface
128 * Revision 1.85 1996/03/15 11:15:48 adam
129 * Modified to use new prototypes for p_query_rpn and p_query_scan.
131 * Revision 1.84 1996/03/07 12:42:49 adam
132 * Better logging when callback is invoked.
134 * Revision 1.83 1996/03/05 09:21:09 adam
135 * Bug fix: memory used by GRS records wasn't freed.
136 * Rewrote some of the error handling code - the connection is always
137 * closed before failback is called.
138 * If failback is defined the send APDU methods (init, search, ...) will
139 * return OK but invoke failback (as is the case if the write operation
141 * Bug fix: ref_count in assoc object could grow if fraction of PDU was
144 * Revision 1.82 1996/02/29 15:30:21 adam
145 * Export of IrTcl functionality to extensions.
147 * Revision 1.81 1996/02/26 18:38:32 adam
148 * Work on export of set methods.
150 * Revision 1.80 1996/02/23 17:31:39 adam
151 * More functions made available to the wais tcl extension.
153 * Revision 1.79 1996/02/23 13:41:38 adam
154 * Work on public access to simple ir class system.
156 * Revision 1.78 1996/02/21 10:16:08 adam
157 * Simplified select handling. Only one function ir_tcl_select_set has
158 * to be externally defined.
160 * Revision 1.77 1996/02/20 17:52:58 adam
161 * Uses the YAZ oid system to name record syntax object identifiers.
163 * Revision 1.76 1996/02/20 16:09:51 adam
164 * Bug fix: didn't set element set names stamp correctly on result
165 * set records when element set names were set to the empty string.
167 * Revision 1.75 1996/02/19 15:41:53 adam
168 * Better log messages.
169 * Minor improvement of connect method.
171 * Revision 1.74 1996/02/05 17:58:03 adam
172 * Ported ir-tcl to use the beta releases of tcl7.5/tk4.1.
174 * Revision 1.73 1996/01/29 11:35:19 adam
175 * Bug fix: cs_type member renamed to comstackType to avoid conflict with
176 * cs_type macro defined by YAZ.
178 * Revision 1.72 1996/01/19 17:45:34 quinn
179 * Added debugging output
181 * Revision 1.71 1996/01/19 16:22:38 adam
182 * New method: apduDump - returns information about last incoming APDU.
184 * Revision 1.70 1996/01/10 09:18:34 adam
185 * PDU specific callbacks implemented: initRespnse, searchResponse,
186 * presentResponse and scanResponse.
187 * Bug fix in the command line shell (tclmain.c) - discovered on OSF/1.
189 * Revision 1.69 1996/01/04 16:12:12 adam
190 * Setting PDUType renamed to eventType.
192 * Revision 1.68 1996/01/04 11:05:22 adam
193 * New setting: PDUType - returns type of last PDU returned from the target.
194 * Fixed a bug in configure/Makefile.
196 * Revision 1.67 1996/01/03 09:00:51 adam
197 * Updated to use new version of Yaz (names changed to avoid C++ conflict).
199 * Revision 1.66 1995/11/28 17:26:39 adam
200 * Removed Carriage return from ir-tcl.c!
201 * Removed misc. debug logs.
203 * Revision 1.65 1995/11/28 13:53:00 quinn
206 * Revision 1.64 1995/11/13 15:39:18 adam
207 * Bug fix: {small,medium}SetElementSetNames weren't set correctly.
208 * Bug fix: idAuthentication weren't set correctly.
210 * Revision 1.63 1995/11/13 09:55:39 adam
211 * Multiple records at a position in a result-set with differnt
214 * Revision 1.62 1995/10/18 17:20:33 adam
215 * Work on target setup in client.tcl.
217 * Revision 1.61 1995/10/18 16:42:42 adam
218 * New settings: smallSetElementSetNames and mediumSetElementSetNames.
220 * Revision 1.60 1995/10/18 15:43:31 adam
221 * In search: mediumSetElementSetNames and smallSetElementSetNames are
222 * set to elementSetNames.
224 * Revision 1.59 1995/10/17 12:18:58 adam
225 * Bug fix: when target connection closed, the connection was not
226 * properly reestablished.
228 * Revision 1.58 1995/10/16 17:00:55 adam
229 * New setting: elementSetNames.
230 * Various client improvements. Medium presentation format looks better.
232 * Revision 1.57 1995/09/21 13:11:51 adam
233 * Support of dynamic loading.
234 * Test script uses load command if necessary.
236 * Revision 1.56 1995/08/29 15:30:14 adam
237 * Work on GRS records.
239 * Revision 1.55 1995/08/28 09:43:25 adam
240 * Minor changes. configure only searches for yaz beta 3 and versions after
243 * Revision 1.54 1995/08/24 12:25:16 adam
244 * Modified to work with yaz 1.0b3.
246 * Revision 1.53 1995/08/04 12:49:26 adam
247 * Bug fix: reading uninitialized variable p.
249 * Revision 1.52 1995/08/04 11:32:38 adam
250 * More work on output queue. Memory related routines moved
253 * Revision 1.51 1995/08/03 13:22:54 adam
256 * Revision 1.50 1995/07/20 08:09:49 adam
257 * client.tcl: Targets removed from hotTargets list when targets
258 * are removed/modified.
259 * ir-tcl.c: More work on triggerResourceControl.
261 * Revision 1.49 1995/06/30 12:39:21 adam
262 * Bug fix: loadFile didn't set record type.
263 * The MARC routines are a little less strict in the interpretation.
264 * Script display.tcl replaces the old marc.tcl.
265 * New interactive script: shell.tcl.
267 * Revision 1.48 1995/06/27 19:03:50 adam
268 * Bug fix in do_present in ir-tcl.c: p->set_child member weren't set.
269 * nextResultSetPosition used instead of setOffset.
271 * Revision 1.47 1995/06/25 10:25:04 adam
272 * Working on triggerResourceControl. Description of compile/install
273 * procedure moved to ir-tcl.sgml.
275 * Revision 1.46 1995/06/22 13:15:06 adam
276 * Feature: SUTRS. Setting getSutrs implemented.
277 * Work on display formats.
278 * Preferred record syntax can be set by the user.
280 * Revision 1.45 1995/06/20 08:07:30 adam
281 * New setting: failInfo.
282 * Working on better cancel mechanism.
284 * Revision 1.44 1995/06/19 17:01:20 adam
287 * Revision 1.43 1995/06/19 13:06:08 adam
288 * New define: IR_TCL_VERSION.
290 * Revision 1.42 1995/06/19 08:08:52 adam
291 * client.tcl: hotTargets now contain both database and target name.
292 * ir-tcl.c: setting protocol edited. Errors in callbacks are logged
293 * by logf(LOG_WARN, ...) calls.
295 * Revision 1.41 1995/06/16 12:28:16 adam
296 * Implemented preferredRecordSyntax.
297 * Minor changes in diagnostic handling.
298 * Record list deleted when connection closes.
300 * Revision 1.40 1995/06/14 13:37:18 adam
301 * Setting recordType implemented.
302 * Setting implementationVersion implemented.
303 * Settings implementationId / implementationName edited.
305 * Revision 1.39 1995/06/08 10:26:32 adam
306 * Bug fix in ir_strdup.
308 * Revision 1.38 1995/06/01 16:36:47 adam
309 * About buttons. Minor bug fixes.
311 * Revision 1.37 1995/06/01 07:31:20 adam
312 * Rename of many typedefs -> IrTcl_...
314 * Revision 1.36 1995/05/31 13:09:59 adam
315 * Client searches/presents may be interrupted.
316 * New moving book-logo.
318 * Revision 1.35 1995/05/31 08:36:33 adam
319 * Bug fix in client.tcl: didn't save options on clientrc.tcl.
320 * New method: referenceId. More work on scan.
322 * Revision 1.34 1995/05/29 10:33:42 adam
323 * README and rename of startup script.
325 * Revision 1.33 1995/05/29 09:15:11 quinn
326 * Changed CS_SR to PROTO_SR, etc.
328 * Revision 1.32 1995/05/29 08:44:16 adam
329 * Work on delete of objects.
331 * Revision 1.31 1995/05/26 11:44:10 adam
332 * Bugs fixed. More work on MARC utilities and queries. Test
333 * client is up-to-date again.
335 * Revision 1.30 1995/05/26 08:54:11 adam
336 * New MARC utilities. Uses prefix query.
338 * Revision 1.29 1995/05/24 14:10:22 adam
339 * Work on idAuthentication, protocolVersion and options.
341 * Revision 1.28 1995/05/23 15:34:48 adam
342 * Many new settings, userInformationField, smallSetUpperBound, etc.
343 * A number of settings are inherited when ir-set is executed.
344 * This version is incompatible with the graphical test client (client.tcl).
346 * Revision 1.27 1995/05/11 15:34:47 adam
347 * Scan request changed a bit. This version works with RLG.
349 * Revision 1.26 1995/04/18 16:11:51 adam
350 * First version of graphical Scan. Some work on query-by-form.
352 * Revision 1.25 1995/04/17 09:37:17 adam
353 * Further development of scan.
355 * Revision 1.24 1995/04/11 14:16:42 adam
356 * Further work on scan. Response works. Entries aren't saved yet.
358 * Revision 1.23 1995/04/10 10:50:27 adam
359 * Result-set name defaults to suffix of ir-set name.
360 * Started working on scan. Not finished at this point.
362 * Revision 1.22 1995/03/31 10:43:03 adam
363 * More robust when getting bad MARC records.
365 * Revision 1.21 1995/03/31 08:56:37 adam
366 * New button "Search".
368 * Revision 1.20 1995/03/29 16:07:09 adam
369 * Bug fix: Didn't use setName in present request.
371 * Revision 1.19 1995/03/28 12:45:23 adam
372 * New ir method failback: called on disconnect/protocol error.
373 * New ir set/get method: protocol: SR / Z3950.
374 * Simple popup and disconnect when failback is invoked.
376 * Revision 1.18 1995/03/21 15:50:12 adam
379 * Revision 1.17 1995/03/21 13:41:03 adam
380 * Comstack cs_create not used too often. Non-blocking connect.
382 * Revision 1.16 1995/03/21 08:26:06 adam
383 * New method, setName, to specify the result set name (other than Default).
384 * New method, responseStatus, which returns diagnostic info, if any, after
385 * present response / search response.
387 * Revision 1.15 1995/03/20 15:24:07 adam
388 * Diagnostic records saved on searchResponse.
390 * Revision 1.14 1995/03/20 08:53:22 adam
391 * Event loop in tclmain.c rewritten. New method searchStatus.
393 * Revision 1.13 1995/03/17 18:26:17 adam
394 * Non-blocking i/o used now. Database names popup as cascade items.
396 * Revision 1.12 1995/03/17 15:45:00 adam
397 * Improved target/database setup.
399 * Revision 1.11 1995/03/16 17:54:03 adam
400 * Minor changes really.
402 * Revision 1.10 1995/03/15 16:14:50 adam
403 * Blocking arg in cs_create changed.
405 * Revision 1.9 1995/03/15 13:59:24 adam
408 * Revision 1.8 1995/03/15 08:25:16 adam
409 * New method presentStatus to check for error on present. Misc. cleanup
410 * of IrTcl_RecordList manipulations. Full MARC record presentation in
413 * Revision 1.7 1995/03/14 17:32:29 adam
414 * Presentation of full Marc record in popup window.
416 * Revision 1.6 1995/03/12 19:31:55 adam
417 * Pattern matching implemented when retrieving MARC records. More
418 * diagnostic functions.
420 * Revision 1.5 1995/03/10 18:00:15 adam
421 * Actual presentation in line-by-line format. RPN query support.
423 * Revision 1.4 1995/03/09 16:15:08 adam
424 * First presentRequest attempts. Hot-target list.
442 #if defined(__WIN32__)
443 # define WIN32_LEAN_AND_MEAN
444 # include <windows.h>
445 # undef WIN32_LEAN_AND_MEAN
448 * VC++ has an alternate entry point called DllMain, so we need to rename
452 # if defined(_MSC_VER)
453 # define EXPORT(a,b) __declspec(dllexport) a b
454 # define DllEntryPoint DllMain
456 # if defined(__BORLANDC__)
457 # define EXPORT(a,b) a _export b
459 # define EXPORT(a,b) a b
463 # define EXPORT(a,b) a b
466 static char *wrongArgs = "wrong # args: should be \"";
468 static int ir_tcl_error_exec (Tcl_Interp *interp, int argc, char **argv)
471 Tcl_AppendResult (interp, " while executing ", NULL);
472 for (i = 0; i<argc; i++)
473 Tcl_AppendResult (interp, (i ? " " : "\""), argv[i], NULL);
474 Tcl_AppendResult (interp, "\"", NULL);
479 static void ir_deleteDiags (IrTcl_Diagnostic **dst_list, int *dst_num);
481 static void ir_select_notify (ClientData clientData, int r, int w, int e);
483 void ir_select_add (int fd, void *obj)
485 ir_tcl_select_set (ir_select_notify, fd, obj, 1, 0, 0);
488 void ir_select_add_write (int fd, void *obj)
490 ir_tcl_select_set (ir_select_notify, fd, obj, 1, 1, 0);
493 void ir_select_remove (int fd, void *obj)
495 ir_tcl_select_set (NULL, fd, obj, 0, 0, 0);
498 void ir_select_remove_write (int fd, void *obj)
500 ir_tcl_select_set (ir_select_notify, fd, obj, 1, 0, 0);
503 static void delete_IR_record (IrTcl_RecordList *rl)
507 case Z_NamePlusRecord_databaseRecord:
508 switch (rl->u.dbrec.type)
511 ir_tcl_grs_del (&rl->u.dbrec.u.grs1);
516 xfree (rl->u.dbrec.buf);
517 rl->u.dbrec.buf = NULL;
519 case Z_NamePlusRecord_surrogateDiagnostic:
520 ir_deleteDiags (&rl->u.surrogateDiagnostics.list,
521 &rl->u.surrogateDiagnostics.num);
524 xfree (rl->elements);
527 static void purge_IR_records (IrTcl_SetObj *setobj)
529 IrTcl_RecordList *rl;
530 while ((rl = setobj->record_list))
532 setobj->record_list = rl->next;
533 delete_IR_record (rl);
538 static IrTcl_RecordList *new_IR_record (IrTcl_SetObj *setobj,
540 const char *elements)
542 IrTcl_RecordList *rl;
544 if (elements && !*elements)
546 for (rl = setobj->record_list; rl; rl = rl->next)
548 if (no == rl->no && (!rl->elements || !elements ||
549 !strcmp(elements, rl->elements)))
551 delete_IR_record (rl);
557 rl = ir_tcl_malloc (sizeof(*rl));
558 rl->next = setobj->record_list;
560 setobj->record_list = rl;
563 ir_tcl_strdup (NULL, &rl->elements, elements);
570 int ir_tcl_eval (Tcl_Interp *interp, const char *command)
572 char *tmp = ir_tcl_malloc (strlen(command)+1);
575 logf (LOG_DEBUG, "Invoking %.23s ...", command);
576 strcpy (tmp, command);
577 r = Tcl_Eval (interp, tmp);
580 const char *errorInfo = Tcl_GetVar (interp, "errorInfo", 0);
581 logf (LOG_WARN, "Tcl error in line %d: %s\n%s", interp->errorLine,
582 interp->result, errorInfo ? errorInfo : "<null>");
584 Tcl_FreeResult (interp);
590 * IrTcl_getRecordSyntaxStr: Return record syntax name of object id
592 static char *IrTcl_getRecordSyntaxStr (enum oid_value value)
595 struct oident ent, *entp;
597 ent.proto = PROTO_Z3950;
598 ent.oclass = CLASS_RECSYN;
601 o = oid_getoidbyent (&ent);
602 entp = oid_getentbyoid (o);
610 * IrTcl_getRecordSyntaxVal: Return record syntax value of string
612 static enum oid_value IrTcl_getRecordSyntaxVal (const char *name)
614 return oid_getvalbyname (name);
617 static IrTcl_RecordList *find_IR_record (IrTcl_SetObj *setobj, int no)
619 IrTcl_RecordList *rl;
621 for (rl = setobj->record_list; rl; rl = rl->next)
623 (!setobj->recordElements || !rl->elements ||
624 !strcmp (setobj->recordElements, rl->elements)))
629 static void delete_IR_records (IrTcl_SetObj *setobj)
631 IrTcl_RecordList *rl, *rl1;
633 for (rl = setobj->record_list; rl; rl = rl1)
635 delete_IR_record (rl);
639 setobj->record_list = NULL;
643 * ir_tcl_get_set_int: Set/get integer value
645 int ir_tcl_get_set_int (int *val, Tcl_Interp *interp, int argc, char **argv)
651 if (Tcl_GetInt (interp, argv[2], val)==TCL_ERROR)
654 sprintf (buf, "%d", *val);
655 Tcl_AppendResult (interp, buf, NULL);
661 * ir_tcl_method_error
663 int ir_tcl_method_error (Tcl_Interp *interp, int argc, char **argv,
666 IrTcl_Methods *tab_i = tab;
669 Tcl_AppendResult (interp, "bad method: \"", *argv, " ", argv[1],
670 "\"\nmethod should be of:", NULL);
671 for (tab_i = tab; tab_i->tab; tab_i++)
672 for (t = tab_i->tab; t->name; t++)
673 Tcl_AppendResult (interp, " ", t->name, NULL);
678 * ir_tcl_method: Search for method in table and invoke method handler
680 int ir_tcl_method (Tcl_Interp *interp, int argc, char **argv,
681 IrTcl_Methods *tab, int *ret)
683 IrTcl_Methods *tab_i = tab;
686 for (tab_i = tab; tab_i->tab; tab_i++)
687 for (t = tab_i->tab; t->name; t++)
690 if ((*t->method)(tab_i->obj, interp, argc, argv) == TCL_ERROR)
694 if (!strcmp (t->name, argv[1]))
696 *ret = (*t->method)(tab_i->obj, interp, argc, argv);
707 * ir_tcl_named_bits: get/set named bits
709 int ir_tcl_named_bits (struct ir_named_entry *tab, Odr_bitmask *ob,
710 Tcl_Interp *interp, int argc, char **argv)
712 struct ir_named_entry *ti;
717 for (no = 0; no < argc; no++)
720 for (ti = tab; ti->name; ti++)
721 if (!strcmp(argv[no], "@all") || !strcmp (argv[no], ti->name))
723 ODR_MASK_SET (ob, ti->pos);
728 Tcl_AppendResult (interp, "bad bit mask ", argv[no], NULL);
729 return ir_tcl_error_exec (interp, argc, argv);
734 for (ti = tab; ti->name; ti++)
735 if (ODR_MASK_GET (ob, ti->pos))
736 Tcl_AppendElement (interp, ti->name);
740 static void set_referenceId (ODR o, Z_ReferenceId **dst, const char *src)
746 *dst = odr_malloc (o, sizeof(**dst));
747 (*dst)->size = (*dst)->len = strlen(src);
748 (*dst)->buf = odr_malloc (o, (*dst)->len);
749 memcpy ((*dst)->buf, src, (*dst)->len);
753 static void get_referenceId (char **dst, Z_ReferenceId *src)
761 *dst = ir_tcl_malloc (src->len+1);
762 memcpy (*dst, src->buf, src->len);
763 (*dst)[src->len] = '\0';
766 /* ------------------------------------------------------- */
769 * do_init_request: init method on IR object
771 static int do_init_request (void *obj, Tcl_Interp *interp,
772 int argc, char **argv)
780 logf (LOG_DEBUG, "init %s", *argv);
783 Tcl_AppendResult (interp, "not connected", NULL);
784 return ir_tcl_error_exec (interp, argc, argv);
786 apdu = zget_APDU (p->odr_out, Z_APDU_initRequest);
787 req = apdu->u.initRequest;
789 set_referenceId (p->odr_out, &req->referenceId, p->set_inher.referenceId);
790 req->options = &p->options;
791 req->protocolVersion = &p->protocolVersion;
792 req->preferredMessageSize = &p->preferredMessageSize;
793 req->maximumRecordSize = &p->maximumRecordSize;
795 if (p->idAuthenticationGroupId || p->idAuthenticationUserId)
797 Z_IdPass *pass = odr_malloc (p->odr_out, sizeof(*pass));
798 Z_IdAuthentication *auth = odr_malloc (p->odr_out, sizeof(*auth));
800 logf (LOG_DEBUG, "using pass authentication");
802 auth->which = Z_IdAuthentication_idPass;
803 auth->u.idPass = pass;
804 if (p->idAuthenticationGroupId && *p->idAuthenticationGroupId)
805 pass->groupId = p->idAuthenticationGroupId;
807 pass->groupId = NULL;
808 if (p->idAuthenticationUserId && *p->idAuthenticationUserId)
809 pass->userId = p->idAuthenticationUserId;
812 if (p->idAuthenticationPassword && *p->idAuthenticationPassword)
813 pass->password = p->idAuthenticationPassword;
815 pass->password = NULL;
816 req->idAuthentication = auth;
818 else if (p->idAuthenticationOpen && *p->idAuthenticationOpen)
820 Z_IdAuthentication *auth = odr_malloc (p->odr_out, sizeof(*auth));
822 logf (LOG_DEBUG, "using open authentication");
823 auth->which = Z_IdAuthentication_open;
824 auth->u.open = p->idAuthenticationOpen;
825 req->idAuthentication = auth;
828 req->idAuthentication = NULL;
829 req->implementationId = p->implementationId;
830 req->implementationName = p->implementationName;
831 req->implementationVersion = p->implementationVersion;
832 req->userInformationField = 0;
834 return ir_tcl_send_APDU (interp, p, apdu, "init", *argv);
838 * do_protocolVersion: Set protocol Version
840 static int do_protocolVersion (void *obj, Tcl_Interp *interp,
841 int argc, char **argv)
849 ODR_MASK_ZERO (&p->protocolVersion);
850 ODR_MASK_SET (&p->protocolVersion, 0);
851 ODR_MASK_SET (&p->protocolVersion, 1);
856 if (Tcl_GetInt (interp, argv[2], &version)==TCL_ERROR)
858 ODR_MASK_ZERO (&p->protocolVersion);
859 for (i = 0; i<version; i++)
860 ODR_MASK_SET (&p->protocolVersion, i);
862 for (i = 4; --i >= 0; )
863 if (ODR_MASK_GET (&p->protocolVersion, i))
865 sprintf (buf, "%d", i+1);
866 interp->result = buf;
871 * do_options: Set options
873 static int do_options (void *obj, Tcl_Interp *interp,
874 int argc, char **argv)
876 static struct ir_named_entry options_tab[] = {
880 { "resourceReport", 3 },
881 { "triggerResourceCtrl", 4},
882 { "resourceCtrl", 5},
886 { "extendedServices", 10},
887 { "level-1Segmentation", 11},
888 { "level-2Segmentation", 12},
889 { "concurrentOperations", 13},
890 { "namedResultSets", 14},
897 ODR_MASK_ZERO (&p->options);
898 ODR_MASK_SET (&p->options, 0);
899 ODR_MASK_SET (&p->options, 1);
900 ODR_MASK_SET (&p->options, 4);
901 ODR_MASK_SET (&p->options, 7);
902 ODR_MASK_SET (&p->options, 14);
905 return ir_tcl_named_bits (options_tab, &p->options, interp, argc-2, argv+2);
909 * do_apduInfo: Get APDU information
911 static int do_apduInfo (void *obj, Tcl_Interp *interp, int argc, char **argv)
919 sprintf (buf, "%d", p->apduLen);
920 Tcl_AppendElement (interp, buf);
921 sprintf (buf, "%d", p->apduOffset);
922 Tcl_AppendElement (interp, buf);
925 Tcl_AppendElement (interp, "");
928 apduf = fopen ("apdu.tmp", "w");
931 Tcl_AppendElement (interp, "");
934 odr_dumpBER (apduf, p->buf_in, p->apduLen);
936 if (!(apduf = fopen ("apdu.tmp", "r")))
937 Tcl_AppendElement (interp, "");
942 Tcl_AppendResult (interp, " {", NULL);
943 while ((c = getc (apduf)) != EOF)
947 Tcl_AppendResult (interp, buf, NULL);
950 Tcl_AppendResult (interp, "}", NULL);
957 * do_failInfo: Get fail information
959 static int do_failInfo (void *obj, Tcl_Interp *interp, int argc, char **argv)
969 sprintf (buf, "%d", p->failInfo);
975 case IR_TCL_FAIL_CONNECT:
976 cp = "connect failed";
978 case IR_TCL_FAIL_READ:
979 cp = "connection closed";
981 case IR_TCL_FAIL_WRITE:
982 cp = "connection closed";
984 case IR_TCL_FAIL_IN_APDU:
985 cp = "failed to decode incoming APDU";
987 case IR_TCL_FAIL_UNKNOWN_APDU:
993 Tcl_AppendElement (interp, buf);
994 Tcl_AppendElement (interp, cp);
999 * do_preferredMessageSize: Set/get preferred message size
1001 static int do_preferredMessageSize (void *obj, Tcl_Interp *interp,
1002 int argc, char **argv)
1008 p->preferredMessageSize = 30000;
1011 return ir_tcl_get_set_int (&p->preferredMessageSize, interp, argc, argv);
1015 * do_maximumRecordSize: Set/get maximum record size
1017 static int do_maximumRecordSize (void *obj, Tcl_Interp *interp,
1018 int argc, char **argv)
1024 p->maximumRecordSize = 30000;
1027 return ir_tcl_get_set_int (&p->maximumRecordSize, interp, argc, argv);
1031 * do_initResult: Get init result
1033 static int do_initResult (void *obj, Tcl_Interp *interp,
1034 int argc, char **argv)
1040 return ir_tcl_get_set_int (&p->initResult, interp, argc, argv);
1045 * do_implementationName: Set/get Implementation Name.
1047 static int do_implementationName (void *obj, Tcl_Interp *interp,
1048 int argc, char **argv)
1053 return ir_tcl_strdup (interp, &p->implementationName,
1055 else if (argc == -1)
1056 return ir_tcl_strdel (interp, &p->implementationName);
1059 xfree (p->implementationName);
1060 if (ir_tcl_strdup (interp, &p->implementationName, argv[2])
1064 Tcl_AppendResult (interp, p->implementationName, (char*) NULL);
1069 * do_implementationId: Get Implementation Id.
1071 static int do_implementationId (void *obj, Tcl_Interp *interp,
1072 int argc, char **argv)
1077 return ir_tcl_strdup (interp, &p->implementationId, "81");
1078 else if (argc == -1)
1079 return ir_tcl_strdel (interp, &p->implementationId);
1080 Tcl_AppendResult (interp, p->implementationId, (char*) NULL);
1085 * do_implementationVersion: get Implementation Version.
1087 static int do_implementationVersion (void *obj, Tcl_Interp *interp,
1088 int argc, char **argv)
1093 return ir_tcl_strdup (interp, &p->implementationVersion,
1094 #ifdef IR_TCL_VERSION
1099 else if (argc == -1)
1100 return ir_tcl_strdel (interp, &p->implementationVersion);
1101 Tcl_AppendResult (interp, p->implementationVersion, (char*) NULL);
1106 * do_targetImplementationName: Get Implementation Name of target.
1108 static int do_targetImplementationName (void *obj, Tcl_Interp *interp,
1109 int argc, char **argv)
1115 p->targetImplementationName = NULL;
1118 else if (argc == -1)
1119 return ir_tcl_strdel (interp, &p->targetImplementationName);
1120 Tcl_AppendResult (interp, p->targetImplementationName, (char*) NULL);
1125 * do_targetImplementationId: Get Implementation Id of target
1127 static int do_targetImplementationId (void *obj, Tcl_Interp *interp,
1128 int argc, char **argv)
1134 p->targetImplementationId = NULL;
1137 else if (argc == -1)
1138 return ir_tcl_strdel (interp, &p->targetImplementationId);
1139 Tcl_AppendResult (interp, p->targetImplementationId, (char*) NULL);
1144 * do_targetImplementationVersion: Get Implementation Version of target
1146 static int do_targetImplementationVersion (void *obj, Tcl_Interp *interp,
1147 int argc, char **argv)
1153 p->targetImplementationVersion = NULL;
1156 else if (argc == -1)
1157 return ir_tcl_strdel (interp, &p->targetImplementationVersion);
1158 Tcl_AppendResult (interp, p->targetImplementationVersion, (char*) NULL);
1163 * do_idAuthentication: Set/get id Authentication
1165 static int do_idAuthentication (void *obj, Tcl_Interp *interp,
1166 int argc, char **argv)
1170 if (argc >= 3 || argc == -1)
1172 xfree (p->idAuthenticationOpen);
1173 xfree (p->idAuthenticationGroupId);
1174 xfree (p->idAuthenticationUserId);
1175 xfree (p->idAuthenticationPassword);
1177 if (argc >= 3 || argc <= 0)
1179 p->idAuthenticationOpen = NULL;
1180 p->idAuthenticationGroupId = NULL;
1181 p->idAuthenticationUserId = NULL;
1182 p->idAuthenticationPassword = NULL;
1190 xfree (p->idAuthenticationGroupId);
1191 xfree (p->idAuthenticationUserId);
1192 xfree (p->idAuthenticationPassword);
1193 p->idAuthenticationGroupId = NULL;
1194 p->idAuthenticationUserId = NULL;
1195 p->idAuthenticationPassword = NULL;
1197 ir_tcl_strdup (interp, &p->idAuthenticationOpen, argv[2])
1203 xfree (p->idAuthenticationOpen);
1204 p->idAuthenticationOpen = NULL;
1206 ir_tcl_strdup (interp, &p->idAuthenticationGroupId, argv[2])
1210 ir_tcl_strdup (interp, &p->idAuthenticationUserId, argv[3])
1214 ir_tcl_strdup (interp, &p->idAuthenticationPassword, argv[4])
1219 if (p->idAuthenticationOpen)
1220 Tcl_AppendElement (interp, p->idAuthenticationOpen);
1221 else if (p->idAuthenticationGroupId || p->idAuthenticationUserId)
1223 Tcl_AppendElement (interp, p->idAuthenticationGroupId);
1224 Tcl_AppendElement (interp, p->idAuthenticationUserId);
1225 Tcl_AppendElement (interp, p->idAuthenticationPassword);
1231 * do_connect: connect method on IR object
1233 static int do_connect (void *obj, Tcl_Interp *interp,
1234 int argc, char **argv)
1244 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
1245 " ?hostname?\"", NULL);
1250 Tcl_AppendResult (interp, p->hostname, NULL);
1254 logf (LOG_DEBUG, "connect %s %s", *argv, argv[2]);
1257 Tcl_AppendResult (interp, "already connected", NULL);
1258 return ir_tcl_error_exec (interp, argc, argv);
1260 if (!strcmp (p->comstackType, "tcpip"))
1262 p->cs_link = cs_create (tcpip_type, CS_BLOCK, p->protocol_type);
1263 logf (LOG_DEBUG, "tcp/ip connect %s", argv[2]);
1265 else if (!strcmp (p->comstackType, "mosi"))
1268 p->cs_link = cs_create (mosi_type, CS_BLOCK, p->protocol_type);
1269 logf (LOG_DEBUG, "mosi connect %s", argv[2]);
1271 Tcl_AppendResult (interp, "mosi not supported", NULL);
1272 return ir_tcl_error_exec (interp, argc, argv);
1277 Tcl_AppendResult (interp, "bad comstack type ",
1278 p->comstackType, NULL);
1279 return ir_tcl_error_exec (interp, argc, argv);
1281 if (ir_tcl_strdup (interp, &p->hostname, argv[2]) == TCL_ERROR)
1283 p->eventType = "connect";
1284 addr = cs_straddr (p->cs_link, argv[2]);
1287 ir_tcl_disconnect (p);
1288 Tcl_AppendResult (interp, "cs_straddr fail", NULL);
1289 return ir_tcl_error_exec (interp, argc, argv);
1291 if ((r=cs_connect (p->cs_link, addr)) < 0)
1293 ir_tcl_disconnect (p);
1294 Tcl_AppendResult (interp, "connect fail", NULL);
1295 return ir_tcl_error_exec (interp, argc, argv);
1297 ir_select_add (cs_fileno (p->cs_link), p);
1300 logf (LOG_DEBUG, "connect pending fd=%d", cs_fileno(p->cs_link));
1301 ir_select_add_write (cs_fileno (p->cs_link), p);
1302 p->state = IR_TCL_R_Connecting;
1306 logf (LOG_DEBUG, "connect ok fd=%d", cs_fileno(p->cs_link));
1307 p->state = IR_TCL_R_Idle;
1309 ir_tcl_eval (p->interp, p->callback);
1316 * ir_tcl_disconnect: close connection
1318 void ir_tcl_disconnect (IrTcl_Obj *p)
1322 logf(LOG_DEBUG, "Closing connection to %s", p->hostname);
1323 xfree (p->hostname);
1325 assert (p->cs_link);
1326 ir_select_remove (cs_fileno (p->cs_link), p);
1328 odr_reset (p->odr_in);
1330 #if TCL_MAJOR_VERSION == 8
1331 cs_fileno(p->cs_link) = -1;
1333 cs_close (p->cs_link);
1336 ODR_MASK_ZERO (&p->options);
1337 ODR_MASK_SET (&p->options, 0);
1338 ODR_MASK_SET (&p->options, 1);
1339 ODR_MASK_SET (&p->options, 4);
1340 ODR_MASK_SET (&p->options, 7);
1341 ODR_MASK_SET (&p->options, 14);
1343 ODR_MASK_ZERO (&p->protocolVersion);
1344 ODR_MASK_SET (&p->protocolVersion, 0);
1345 ODR_MASK_SET (&p->protocolVersion, 1);
1348 assert (!p->cs_link);
1352 * do_disconnect: disconnect method on IR object
1354 static int do_disconnect (void *obj, Tcl_Interp *interp,
1355 int argc, char **argv)
1361 p->state = IR_TCL_R_Idle;
1362 p->eventType = NULL;
1367 ir_tcl_disconnect (p);
1372 * do_comstack: Set/get comstack method on IR object
1374 static int do_comstack (void *o, Tcl_Interp *interp,
1375 int argc, char **argv)
1380 return ir_tcl_strdup (interp, &obj->comstackType, "tcpip");
1381 else if (argc == -1)
1382 return ir_tcl_strdel (interp, &obj->comstackType);
1385 xfree (obj->comstackType);
1386 if (ir_tcl_strdup (interp, &obj->comstackType, argv[2]) == TCL_ERROR)
1389 Tcl_AppendElement (interp, obj->comstackType);
1394 * do_logLevel: Set log level
1396 static int do_logLevel (void *o, Tcl_Interp *interp,
1397 int argc, char **argv)
1402 yaz_log_init (yaz_log_mask_str (argv[2]), "", NULL);
1404 yaz_log_init (yaz_log_mask_str (argv[2]), argv[3], NULL);
1406 yaz_log_init (yaz_log_mask_str (argv[2]), argv[3], argv[4]);
1412 * do_eventType: Return type of last event
1414 static int do_eventType (void *obj, Tcl_Interp *interp,
1415 int argc, char **argv)
1421 p->eventType = NULL;
1424 Tcl_AppendElement (interp, p->eventType ? p->eventType : "");
1430 * do_callback: add callback
1432 static int do_callback (void *obj, Tcl_Interp *interp,
1433 int argc, char **argv)
1442 else if (argc == -1)
1443 return ir_tcl_strdel (interp, &p->callback);
1446 xfree (p->callback);
1449 if (ir_tcl_strdup (interp, &p->callback, argv[2]) == TCL_ERROR)
1459 * do_failback: add error handle callback
1461 static int do_failback (void *obj, Tcl_Interp *interp,
1462 int argc, char **argv)
1471 else if (argc == -1)
1472 return ir_tcl_strdel (interp, &p->failback);
1475 xfree (p->failback);
1478 if (ir_tcl_strdup (interp, &p->failback, argv[2]) == TCL_ERROR)
1488 * do_initResponse: add init response handler
1490 static int do_initResponse (void *obj, Tcl_Interp *interp,
1491 int argc, char **argv)
1497 p->initResponse = NULL;
1500 else if (argc == -1)
1501 return ir_tcl_strdel (interp, &p->initResponse);
1504 xfree (p->initResponse);
1507 if (ir_tcl_strdup (interp, &p->initResponse, argv[2]) == TCL_ERROR)
1511 p->initResponse = NULL;
1516 * do_protocol: Set/get protocol method on IR object
1518 static int do_protocol (void *o, Tcl_Interp *interp, int argc, char **argv)
1524 p->protocol_type = PROTO_Z3950;
1529 if (!strcmp (argv[2], "Z39"))
1530 p->protocol_type = PROTO_Z3950;
1531 else if (!strcmp (argv[2], "SR"))
1532 p->protocol_type = PROTO_SR;
1535 Tcl_AppendResult (interp, "bad protocol ", argv[2], NULL);
1536 return ir_tcl_error_exec (interp, argc, argv);
1540 switch (p->protocol_type)
1543 Tcl_AppendElement (interp, "Z39");
1546 Tcl_AppendElement (interp, "SR");
1553 * do_triggerResourceControl:
1555 static int do_triggerResourceControl (void *obj, Tcl_Interp *interp,
1556 int argc, char **argv)
1560 Z_TriggerResourceControlRequest *req;
1561 bool_t is_false = 0;
1567 Tcl_AppendResult (interp, "not connected", NULL);
1568 return ir_tcl_error_exec (interp, argc, argv);
1570 apdu = zget_APDU (p->odr_out, Z_APDU_triggerResourceControlRequest);
1571 req = apdu->u.triggerResourceControlRequest;
1572 *req->requestedAction = Z_TriggerResourceCtrl_cancel;
1573 req->resultSetWanted = &is_false;
1575 return ir_tcl_send_APDU (interp, p, apdu, "triggerResourceControl",
1580 * do_databaseNames: specify database names
1582 static int do_databaseNames (void *obj, Tcl_Interp *interp,
1583 int argc, char **argv)
1586 IrTcl_SetCObj *p = obj;
1590 for (i=0; i<p->num_databaseNames; i++)
1591 xfree (p->databaseNames[i]);
1592 xfree (p->databaseNames);
1596 p->num_databaseNames = 0;
1597 p->databaseNames = NULL;
1602 for (i=0; i<p->num_databaseNames; i++)
1603 Tcl_AppendElement (interp, p->databaseNames[i]);
1606 if (p->databaseNames)
1608 for (i=0; i<p->num_databaseNames; i++)
1609 xfree (p->databaseNames[i]);
1610 xfree (p->databaseNames);
1612 p->num_databaseNames = argc - 2;
1614 ir_tcl_malloc (sizeof(*p->databaseNames) * (1+p->num_databaseNames));
1615 for (i=0; i<p->num_databaseNames; i++)
1617 if (ir_tcl_strdup (interp, &p->databaseNames[i], argv[2+i])
1621 p->databaseNames[i] = NULL;
1626 * do_replaceIndicator: Set/get replace Set indicator
1628 static int do_replaceIndicator (void *obj, Tcl_Interp *interp,
1629 int argc, char **argv)
1631 IrTcl_SetCObj *p = obj;
1635 p->replaceIndicator = 1;
1638 return ir_tcl_get_set_int (&p->replaceIndicator, interp, argc, argv);
1642 * do_queryType: Set/Get query method
1644 static int do_queryType (void *obj, Tcl_Interp *interp,
1645 int argc, char **argv)
1647 IrTcl_SetCObj *p = obj;
1650 return ir_tcl_strdup (interp, &p->queryType, "rpn");
1651 else if (argc == -1)
1652 return ir_tcl_strdel (interp, &p->queryType);
1655 xfree (p->queryType);
1656 if (ir_tcl_strdup (interp, &p->queryType, argv[2]) == TCL_ERROR)
1659 Tcl_AppendResult (interp, p->queryType, NULL);
1664 * do_userInformationField: Get User information field
1666 static int do_userInformationField (void *obj, Tcl_Interp *interp,
1667 int argc, char **argv)
1673 p->userInformationField = NULL;
1676 else if (argc == -1)
1677 return ir_tcl_strdel (interp, &p->userInformationField);
1678 Tcl_AppendResult (interp, p->userInformationField, NULL);
1683 * do_smallSetUpperBound: Set/get small set upper bound
1685 static int do_smallSetUpperBound (void *o, Tcl_Interp *interp,
1686 int argc, char **argv)
1688 IrTcl_SetCObj *p = o;
1692 p->smallSetUpperBound = 0;
1695 return ir_tcl_get_set_int (&p->smallSetUpperBound, interp, argc, argv);
1699 * do_largeSetLowerBound: Set/get large set lower bound
1701 static int do_largeSetLowerBound (void *o, Tcl_Interp *interp,
1702 int argc, char **argv)
1704 IrTcl_SetCObj *p = o;
1708 p->largeSetLowerBound = 2;
1711 return ir_tcl_get_set_int (&p->largeSetLowerBound, interp, argc, argv);
1715 * do_mediumSetPresentNumber: Set/get large set lower bound
1717 static int do_mediumSetPresentNumber (void *o, Tcl_Interp *interp,
1718 int argc, char **argv)
1720 IrTcl_SetCObj *p = o;
1724 p->mediumSetPresentNumber = 0;
1727 return ir_tcl_get_set_int (&p->mediumSetPresentNumber, interp, argc, argv);
1731 * do_referenceId: Set/Get referenceId
1733 static int do_referenceId (void *obj, Tcl_Interp *interp,
1734 int argc, char **argv)
1736 IrTcl_SetCObj *p = obj;
1740 p->referenceId = NULL;
1743 else if (argc == -1)
1744 return ir_tcl_strdel (interp, &p->referenceId);
1747 xfree (p->referenceId);
1748 if (ir_tcl_strdup (interp, &p->referenceId, argv[2]) == TCL_ERROR)
1751 Tcl_AppendResult (interp, p->referenceId, NULL);
1756 * do_preferredRecordSyntax: Set/get preferred record syntax
1758 static int do_preferredRecordSyntax (void *obj, Tcl_Interp *interp,
1759 int argc, char **argv)
1761 IrTcl_SetCObj *p = obj;
1765 p->preferredRecordSyntax = NULL;
1768 else if (argc == -1)
1770 xfree (p->preferredRecordSyntax);
1771 p->preferredRecordSyntax = NULL;
1776 xfree (p->preferredRecordSyntax);
1777 p->preferredRecordSyntax = NULL;
1778 if (argv[2][0] && (p->preferredRecordSyntax =
1779 ir_tcl_malloc (sizeof(*p->preferredRecordSyntax))))
1780 *p->preferredRecordSyntax = IrTcl_getRecordSyntaxVal (argv[2]);
1785 (interp,!p->preferredRecordSyntax ? "" :
1786 IrTcl_getRecordSyntaxStr(*p->preferredRecordSyntax));
1793 * do_elementSetNames: Set/Get element Set Names
1795 static int do_elementSetNames (void *obj, Tcl_Interp *interp,
1796 int argc, char **argv)
1798 IrTcl_SetCObj *p = obj;
1802 p->elementSetNames = NULL;
1805 else if (argc == -1)
1806 return ir_tcl_strdel (interp, &p->elementSetNames);
1809 xfree (p->elementSetNames);
1810 if (ir_tcl_strdup (interp, &p->elementSetNames, argv[2]) == TCL_ERROR)
1813 Tcl_AppendResult (interp, p->elementSetNames, NULL);
1818 * do_smallSetElementSetNames: Set/Get small Set Element Set Names
1820 static int do_smallSetElementSetNames (void *obj, Tcl_Interp *interp,
1821 int argc, char **argv)
1823 IrTcl_SetCObj *p = obj;
1827 p->smallSetElementSetNames = NULL;
1830 else if (argc == -1)
1831 return ir_tcl_strdel (interp, &p->smallSetElementSetNames);
1834 xfree (p->smallSetElementSetNames);
1835 if (ir_tcl_strdup (interp, &p->smallSetElementSetNames,
1836 argv[2]) == TCL_ERROR)
1839 Tcl_AppendResult (interp, p->smallSetElementSetNames, NULL);
1844 * do_mediumSetElementSetNames: Set/Get medium Set Element Set Names
1846 static int do_mediumSetElementSetNames (void *obj, Tcl_Interp *interp,
1847 int argc, char **argv)
1849 IrTcl_SetCObj *p = obj;
1853 p->mediumSetElementSetNames = NULL;
1856 else if (argc == -1)
1857 return ir_tcl_strdel (interp, &p->mediumSetElementSetNames);
1860 xfree (p->mediumSetElementSetNames);
1861 if (ir_tcl_strdup (interp, &p->mediumSetElementSetNames,
1862 argv[2]) == TCL_ERROR)
1865 Tcl_AppendResult (interp, p->mediumSetElementSetNames, NULL);
1869 static IrTcl_Method ir_method_tab[] = {
1870 { "comstack", do_comstack, NULL },
1871 { "protocol", do_protocol, NULL },
1872 { "failback", do_failback, NULL },
1873 { "failInfo", do_failInfo, NULL },
1874 { "apduInfo", do_apduInfo, NULL },
1875 { "logLevel", do_logLevel, NULL },
1877 { "eventType", do_eventType, NULL },
1878 { "connect", do_connect, NULL },
1879 { "protocolVersion", do_protocolVersion, NULL },
1880 { "preferredMessageSize", do_preferredMessageSize, NULL },
1881 { "maximumRecordSize", do_maximumRecordSize, NULL },
1882 { "implementationName", do_implementationName, NULL },
1883 { "implementationId", do_implementationId, NULL },
1884 { "implementationVersion", do_implementationVersion, NULL },
1885 { "targetImplementationName", do_targetImplementationName, NULL },
1886 { "targetImplementationId", do_targetImplementationId, NULL },
1887 { "targetImplementationVersion", do_targetImplementationVersion, NULL},
1888 { "userInformationField", do_userInformationField, NULL},
1889 { "idAuthentication", do_idAuthentication, NULL},
1890 { "options", do_options, NULL},
1891 { "init", do_init_request, NULL},
1892 { "initResult", do_initResult, NULL},
1893 { "disconnect", do_disconnect, NULL},
1894 { "callback", do_callback, NULL},
1895 { "initResponse", do_initResponse, NULL},
1896 { "triggerResourceControl", do_triggerResourceControl, NULL},
1897 { "initResponse", do_initResponse, NULL},
1901 static IrTcl_Method ir_set_c_method_tab[] = {
1902 { "databaseNames", do_databaseNames, NULL},
1903 { "replaceIndicator", do_replaceIndicator, NULL},
1904 { "queryType", do_queryType, NULL},
1905 { "preferredRecordSyntax", do_preferredRecordSyntax, NULL},
1906 { "smallSetUpperBound", do_smallSetUpperBound, NULL},
1907 { "largeSetLowerBound", do_largeSetLowerBound, NULL},
1908 { "mediumSetPresentNumber", do_mediumSetPresentNumber, NULL},
1909 { "referenceId", do_referenceId, NULL},
1910 { "elementSetNames", do_elementSetNames, NULL},
1911 { "smallSetElementSetNames", do_smallSetElementSetNames, NULL},
1912 { "mediumSetElementSetNames", do_mediumSetElementSetNames, NULL},
1917 * ir_obj_method: IR Object methods
1919 static int ir_obj_method (ClientData clientData, Tcl_Interp *interp,
1920 int argc, char **argv)
1922 IrTcl_Methods tab[3];
1923 IrTcl_Obj *p = (IrTcl_Obj *) clientData;
1928 Tcl_AppendResult (interp, wrongArgs, *argv, "method args...\"", NULL);
1932 tab[0].tab = ir_method_tab;
1934 tab[1].tab = ir_set_c_method_tab;
1935 tab[1].obj = &p->set_inher;
1938 if (ir_tcl_method (interp, argc, argv, tab, &r) == TCL_ERROR)
1939 return ir_tcl_method_error (interp, argc, argv, tab);
1944 * ir_obj_delete: IR Object disposal
1946 static void ir_obj_delete (ClientData clientData)
1948 IrTcl_Obj *obj = (IrTcl_Obj *) clientData;
1949 IrTcl_Methods tab[3];
1952 if (obj->ref_count > 0)
1954 assert (obj->ref_count == 0);
1956 logf (LOG_DEBUG, "ir object delete");
1957 tab[0].tab = ir_method_tab;
1959 tab[1].tab = ir_set_c_method_tab;
1960 tab[1].obj = &obj->set_inher;
1963 ir_tcl_method (NULL, -1, NULL, tab, NULL);
1966 odr_destroy (obj->odr_in);
1967 odr_destroy (obj->odr_out);
1968 odr_destroy (obj->odr_pr);
1973 * ir_obj_init: IR Object initialization
1975 int ir_obj_init (ClientData clientData, Tcl_Interp *interp,
1976 int argc, char **argv, ClientData *subData,
1977 ClientData parentData)
1979 IrTcl_Methods tab[3];
1987 Tcl_AppendResult (interp, wrongArgs, *argv, " objName\"", NULL);
1990 obj = ir_tcl_malloc (sizeof(*obj));
1993 obj->bibset = ccl_qual_mk ();
1994 if ((inf = fopen ("default.bib", "r")))
1996 ccl_qual_file (obj->bibset, inf);
2001 logf (LOG_DEBUG, "ir object create %s", argv[1]);
2002 obj->odr_in = odr_createmem (ODR_DECODE);
2003 odr_choice_enable_bias (obj->odr_in, 0);
2004 obj->odr_out = odr_createmem (ODR_ENCODE);
2005 obj->odr_pr = odr_createmem (ODR_PRINT);
2006 obj->state = IR_TCL_R_Idle;
2007 obj->interp = interp;
2011 obj->request_queue = NULL;
2013 tab[0].tab = ir_method_tab;
2015 tab[1].tab = ir_set_c_method_tab;
2016 tab[1].obj = &obj->set_inher;
2019 if (ir_tcl_method (interp, 0, NULL, tab, NULL) == TCL_ERROR)
2021 Tcl_AppendResult (interp, "Failed to initialize ", argv[1], NULL);
2024 *subData = (ClientData) obj;
2030 * ir_obj_mk: IR Object creation
2032 static int ir_obj_mk (ClientData clientData, Tcl_Interp *interp,
2033 int argc, char **argv)
2036 int r = ir_obj_init (clientData, interp, argc, argv, &subData, 0);
2040 Tcl_CreateCommand (interp, argv[1], ir_obj_method,
2041 subData, ir_obj_delete);
2045 IrTcl_Class ir_obj_class = {
2053 /* ------------------------------------------------------- */
2055 * do_search: Do search request
2057 static int do_search (void *o, Tcl_Interp *interp, int argc, char **argv)
2059 Z_SearchRequest *req;
2063 IrTcl_SetObj *obj = o;
2066 #if TCL_MAJOR_VERSION > 8 || (TCL_MAJOR_VERSION == 8 && TCL_MINOR_VERSION > 0)
2078 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1], "query\"",
2082 #if TCL_MAJOR_VERSION > 8 || (TCL_MAJOR_VERSION == 8 && TCL_MINOR_VERSION > 0)
2083 query_str = Tcl_UtfToExternalDString(0, argv[2], -1, &ds);
2085 query_str = argv[2];
2087 logf (LOG_DEBUG, "search %s %s", *argv, query_str);
2088 if (!obj->set_inher.num_databaseNames)
2090 Tcl_AppendResult (interp, "no databaseNames", NULL);
2091 code = ir_tcl_error_exec (interp, argc, argv);
2096 Tcl_AppendResult (interp, "not connected", NULL);
2097 code = ir_tcl_error_exec (interp, argc, argv);
2100 apdu = zget_APDU (p->odr_out, Z_APDU_searchRequest);
2101 req = apdu->u.searchRequest;
2105 set_referenceId (p->odr_out, &req->referenceId,
2106 obj->set_inher.referenceId);
2108 req->smallSetUpperBound = &obj->set_inher.smallSetUpperBound;
2109 req->largeSetLowerBound = &obj->set_inher.largeSetLowerBound;
2110 req->mediumSetPresentNumber = &obj->set_inher.mediumSetPresentNumber;
2111 req->replaceIndicator = &obj->set_inher.replaceIndicator;
2112 req->resultSetName = obj->setName ? obj->setName : "default";
2113 logf (LOG_DEBUG, "Search, resultSetName %s", req->resultSetName);
2114 req->num_databaseNames = obj->set_inher.num_databaseNames;
2115 req->databaseNames = obj->set_inher.databaseNames;
2116 for (r=0; r < obj->set_inher.num_databaseNames; r++)
2117 logf (LOG_DEBUG, " Database %s", obj->set_inher.databaseNames[r]);
2118 if (obj->set_inher.preferredRecordSyntax)
2120 struct oident ident;
2122 ident.proto = p->protocol_type;
2123 ident.oclass = CLASS_RECSYN;
2124 ident.value = *obj->set_inher.preferredRecordSyntax;
2125 logf (LOG_DEBUG, "Preferred record syntax is %d", ident.value);
2126 req->preferredRecordSyntax = odr_oiddup (p->odr_out,
2127 oid_getoidbyent (&ident));
2130 req->preferredRecordSyntax = 0;
2132 if (obj->set_inher.smallSetElementSetNames &&
2133 *obj->set_inher.smallSetElementSetNames)
2135 Z_ElementSetNames *esn = odr_malloc (p->odr_out, sizeof(*esn));
2137 esn->which = Z_ElementSetNames_generic;
2138 esn->u.generic = obj->set_inher.smallSetElementSetNames;
2139 req->smallSetElementSetNames = esn;
2142 req->smallSetElementSetNames = NULL;
2144 if (obj->set_inher.mediumSetElementSetNames &&
2145 *obj->set_inher.mediumSetElementSetNames)
2147 Z_ElementSetNames *esn = odr_malloc (p->odr_out, sizeof(*esn));
2149 esn->which = Z_ElementSetNames_generic;
2150 esn->u.generic = obj->set_inher.mediumSetElementSetNames;
2151 req->mediumSetElementSetNames = esn;
2154 req->mediumSetElementSetNames = NULL;
2156 req->query = &query;
2158 logf (LOG_DEBUG, "queryType %s", obj->set_inher.queryType);
2159 if (!strcmp (obj->set_inher.queryType, "rpn"))
2161 Z_RPNQuery *RPNquery;
2163 RPNquery = p_query_rpn (p->odr_out, p->protocol_type, query_str);
2166 Tcl_AppendResult (interp, "query syntax error", NULL);
2167 code = ir_tcl_error_exec (interp, argc, argv);
2170 query.which = Z_Query_type_1;
2171 query.u.type_1 = RPNquery;
2174 else if (!strcmp (obj->set_inher.queryType, "cclrpn"))
2178 struct ccl_rpn_node *rpn;
2179 Z_RPNQuery *RPNquery;
2182 bib1.proto = p->protocol_type;
2183 bib1.oclass = CLASS_ATTSET;
2184 bib1.value = VAL_BIB1;
2186 rpn = ccl_find_str(p->bibset, query_str, &error, &pos);
2189 Tcl_AppendResult (interp, "ccl syntax error ", ccl_err_msg(error),
2191 code = ir_tcl_error_exec (interp, argc, argv);
2195 ccl_pr_tree (rpn, stderr);
2196 fprintf (stderr, "\n");
2198 RPNquery = ccl_rpn_query(p->odr_out, rpn);
2199 RPNquery->attributeSetId = oid_getoidbyent (&bib1);
2200 query.which = Z_Query_type_1;
2201 query.u.type_1 = RPNquery;
2204 else if (!strcmp (obj->set_inher.queryType, "ccl"))
2206 query.which = Z_Query_type_2;
2207 query.u.type_2 = &ccl_query;
2208 ccl_query.buf = (unsigned char *) query_str;
2209 ccl_query.len = strlen (query_str);
2213 Tcl_AppendResult (interp, "invalid query method ",
2214 obj->set_inher.queryType, NULL);
2215 code = ir_tcl_error_exec (interp, argc, argv);
2218 code = ir_tcl_send_APDU (interp, p, apdu, "search", *argv);
2220 #if TCL_MAJOR_VERSION > 8 || (TCL_MAJOR_VERSION == 8 && TCL_MINOR_VERSION > 0)
2221 Tcl_DStringFree (&ds);
2227 * do_searchResponse: add search response handler
2229 static int do_searchResponse (void *o, Tcl_Interp *interp,
2230 int argc, char **argv)
2232 IrTcl_SetObj *obj = o;
2236 obj->searchResponse = NULL;
2239 else if (argc == -1)
2240 return ir_tcl_strdel (interp, &obj->searchResponse);
2243 xfree (obj->searchResponse);
2246 if (ir_tcl_strdup (interp, &obj->searchResponse, argv[2])
2251 obj->searchResponse = NULL;
2257 * do_presentResponse: add present response handler
2259 static int do_presentResponse (void *o, Tcl_Interp *interp,
2260 int argc, char **argv)
2262 IrTcl_SetObj *obj = o;
2266 obj->presentResponse = NULL;
2269 else if (argc == -1)
2270 return ir_tcl_strdel (interp, &obj->presentResponse);
2273 xfree (obj->presentResponse);
2276 if (ir_tcl_strdup (interp, &obj->presentResponse, argv[2])
2281 obj->presentResponse = NULL;
2287 * do_sortResponse: add sort response handler
2289 static int do_sortResponse (void *o, Tcl_Interp *interp,
2290 int argc, char **argv)
2292 IrTcl_SetObj *obj = o;
2296 obj->sortResponse = NULL;
2299 else if (argc == -1)
2300 return ir_tcl_strdel (interp, &obj->sortResponse);
2303 xfree (obj->sortResponse);
2306 if (ir_tcl_strdup (interp, &obj->sortResponse, argv[2])
2311 obj->sortResponse = NULL;
2317 * do_resultCount: Get number of hits
2319 static int do_resultCount (void *o, Tcl_Interp *interp,
2320 int argc, char **argv)
2322 IrTcl_SetObj *obj = o;
2326 obj->resultCount = 0;
2329 return ir_tcl_get_set_int (&obj->resultCount, interp, argc, argv);
2333 * do_searchStatus: Get search status (after search response)
2335 static int do_searchStatus (void *o, Tcl_Interp *interp,
2336 int argc, char **argv)
2338 IrTcl_SetObj *obj = o;
2342 return ir_tcl_get_set_int (&obj->searchStatus, interp, argc, argv);
2345 static void reset_searchResult (IrTcl_SetObj *setobj)
2348 for (i = 0; i<setobj->searchResult_num; i++)
2349 xfree (setobj->searchResult_terms[i]);
2350 xfree (setobj->searchResult_terms);
2351 xfree (setobj->searchResult_count);
2352 setobj->searchResult_terms = 0;
2353 setobj->searchResult_num = 0;
2358 * do_searchResult Get USR:Search-Result1 (after search response)
2360 static int do_searchResult (void *o, Tcl_Interp *interp,
2361 int argc, char **argv)
2363 IrTcl_SetObj *obj = o;
2368 obj->searchResult_num = 0;
2369 obj->searchResult_terms = 0;
2370 obj->searchResult_count = 0;
2373 else if (argc == -1)
2375 reset_searchResult (obj);
2378 for (i = 0; i<obj->searchResult_num; i++)
2381 sprintf (str, "%d", obj->searchResult_count[i]);
2382 Tcl_AppendResult (interp, "{", NULL);
2383 if (obj->searchResult_terms[i])
2384 Tcl_AppendElement (interp, obj->searchResult_terms[i]);
2386 Tcl_AppendElement (interp, "");
2387 Tcl_AppendElement (interp, str);
2388 Tcl_AppendResult (interp, "} ", NULL);
2394 * do_presentStatus: Get search status (after search/present response)
2396 static int do_presentStatus (void *o, Tcl_Interp *interp,
2397 int argc, char **argv)
2399 IrTcl_SetObj *obj = o;
2403 return ir_tcl_get_set_int (&obj->presentStatus, interp, argc, argv);
2407 * do_sortStatus: Get sort status (after sort response)
2409 static int do_sortStatus (void *o, Tcl_Interp *interp,
2410 int argc, char **argv)
2412 IrTcl_SetObj *obj = o;
2417 obj->sortStatus = Z_SortStatus_failure;
2420 switch (obj->sortStatus)
2422 case Z_SortStatus_success:
2423 res = "success"; break;
2424 case Z_SortStatus_partial_1:
2425 res = "partial"; break;
2426 case Z_SortStatus_failure:
2427 res = "failure"; break;
2429 res = "unknown"; break;
2431 Tcl_AppendElement (interp, res);
2436 * do_nextResultSetPosition: Get next result set position
2437 * (after search/present response)
2439 static int do_nextResultSetPosition (void *o, Tcl_Interp *interp,
2440 int argc, char **argv)
2442 IrTcl_SetObj *obj = o;
2446 obj->nextResultSetPosition = 0;
2449 return ir_tcl_get_set_int (&obj->nextResultSetPosition, interp,
2454 * do_setName: Set result Set name
2456 static int do_setName (void *o, Tcl_Interp *interp,
2457 int argc, char **argv)
2459 IrTcl_SetObj *obj = o;
2462 return ir_tcl_strdup (interp, &obj->setName, "Default");
2463 else if (argc == -1)
2464 return ir_tcl_strdel (interp, &obj->setName);
2467 xfree (obj->setName);
2468 if (ir_tcl_strdup (interp, &obj->setName, argv[2])
2472 Tcl_AppendElement (interp, obj->setName);
2477 * do_numberOfRecordsReturned: Get number of records returned
2479 static int do_numberOfRecordsReturned (void *o, Tcl_Interp *interp,
2480 int argc, char **argv)
2482 IrTcl_SetObj *obj = o;
2486 obj->numberOfRecordsReturned = 0;
2489 return ir_tcl_get_set_int (&obj->numberOfRecordsReturned, interp,
2494 * do_type: Return type (if any) at position.
2496 static int do_type (void *o, Tcl_Interp *interp, int argc, char **argv)
2498 IrTcl_SetObj *obj = o;
2500 IrTcl_RecordList *rl;
2504 obj->record_list = NULL;
2507 else if (argc == -1)
2509 delete_IR_records (obj);
2514 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2515 " position\"", NULL);
2518 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2520 rl = find_IR_record (obj, offset);
2523 logf (LOG_DEBUG, "%s %s %s: no record", argv[0], argv[1], argv[2]);
2528 case Z_NamePlusRecord_databaseRecord:
2529 interp->result = "DB";
2531 case Z_NamePlusRecord_surrogateDiagnostic:
2532 interp->result = "SD";
2540 * do_recordType: Return record type (if any) at position.
2542 static int do_recordType (void *o, Tcl_Interp *interp, int argc, char **argv)
2544 IrTcl_SetObj *obj = o;
2546 IrTcl_RecordList *rl;
2552 else if (argc == -1)
2558 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2559 " position\"", NULL);
2562 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2564 rl = find_IR_record (obj, offset);
2567 logf (LOG_DEBUG, "%s %s %s: no record", argv[0], argv[1], argv[2]);
2570 if (rl->which != Z_NamePlusRecord_databaseRecord)
2572 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2575 Tcl_AppendElement (interp, (char*)
2576 IrTcl_getRecordSyntaxStr (rl->u.dbrec.type));
2581 * set record elements (for record extraction)
2583 static int do_recordElements (void *o, Tcl_Interp *interp,
2584 int argc, char **argv)
2586 IrTcl_SetObj *obj = o;
2590 obj->recordElements = NULL;
2593 else if (argc == -1)
2594 return ir_tcl_strdel (NULL, &obj->recordElements);
2597 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2598 " ?position?\"", NULL);
2603 xfree (obj->recordElements);
2604 return ir_tcl_strdup (NULL, &obj->recordElements,
2605 (*argv[2] ? argv[2] : NULL));
2607 Tcl_AppendResult (interp, obj->recordElements, NULL);
2614 static int ir_diagResult (Tcl_Interp *interp, IrTcl_Diagnostic *list, int num)
2620 for (i = 0; i<num; i++)
2622 sprintf (buf, "%d", list[i].condition);
2623 Tcl_AppendElement (interp, buf);
2624 cp = diagbib1_str (list[i].condition);
2626 Tcl_AppendElement (interp, (char*) cp);
2628 Tcl_AppendElement (interp, "");
2629 if (list[i].addinfo)
2630 Tcl_AppendElement (interp, (char*) list[i].addinfo);
2632 Tcl_AppendElement (interp, "");
2638 * do_diag: Return diagnostic record info
2640 static int do_diag (void *o, Tcl_Interp *interp, int argc, char **argv)
2642 IrTcl_SetObj *obj = o;
2644 IrTcl_RecordList *rl;
2650 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2651 " position\"", NULL);
2654 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2656 rl = find_IR_record (obj, offset);
2659 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2662 if (rl->which != Z_NamePlusRecord_surrogateDiagnostic)
2664 Tcl_AppendResult (interp, "No Diagnostic record at #", argv[2], NULL);
2667 return ir_diagResult (interp, rl->u.surrogateDiagnostics.list,
2668 rl->u.surrogateDiagnostics.num);
2672 * do_getMarc: Get ISO2709 Record lines/fields
2674 static int do_getMarc (void *o, Tcl_Interp *interp, int argc, char **argv)
2676 IrTcl_SetObj *obj = o;
2678 IrTcl_RecordList *rl;
2684 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2685 " position line|field tag ind field\"", NULL);
2688 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2690 rl = find_IR_record (obj, offset);
2693 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2696 if (rl->which != Z_NamePlusRecord_databaseRecord)
2698 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2701 return ir_tcl_get_marc (interp, rl->u.dbrec.buf, argc, argv);
2705 * do_getSutrs: Get SUTRS Record
2707 static int do_getSutrs (void *o, Tcl_Interp *interp, int argc, char **argv)
2709 IrTcl_SetObj *obj = o;
2711 IrTcl_RecordList *rl;
2717 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2718 " position\"", NULL);
2721 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2723 rl = find_IR_record (obj, offset);
2726 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2729 if (rl->which != Z_NamePlusRecord_databaseRecord)
2731 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2734 if (!rl->u.dbrec.buf || rl->u.dbrec.type != VAL_SUTRS)
2736 Tcl_AppendElement (interp, rl->u.dbrec.buf);
2742 * do_getGrs: Get a GRS-1 Record
2744 static int do_getGrs (void *o, Tcl_Interp *interp, int argc, char **argv)
2746 IrTcl_SetObj *obj = o;
2748 IrTcl_RecordList *rl;
2754 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2755 " position ?(set,tag) (set,tag) ...?\"", NULL);
2758 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2760 rl = find_IR_record (obj, offset);
2763 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2766 if (rl->which != Z_NamePlusRecord_databaseRecord)
2768 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2771 if (rl->u.dbrec.type != VAL_GRS1)
2773 return ir_tcl_get_grs (interp, rl->u.dbrec.u.grs1, argc, argv);
2778 * do_getExplain: Get an Explain Record
2780 static int do_getExplain (void *o, Tcl_Interp *interp, int argc, char **argv)
2782 IrTcl_SetObj *obj = o;
2783 IrTcl_Obj *p = obj->parent;
2785 Z_ext_typeent *etype;
2787 IrTcl_RecordList *rl;
2793 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2794 " position ?mask? ...\"", NULL);
2797 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2799 rl = find_IR_record (obj, offset);
2802 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2805 if (rl->which != Z_NamePlusRecord_databaseRecord)
2807 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2810 if (!rl->u.dbrec.buf || rl->u.dbrec.type != VAL_EXPLAIN)
2813 if (!(etype = z_ext_getentbyref (VAL_EXPLAIN)))
2815 assert (rl->u.dbrec.buf);
2816 odr_setbuf (p->odr_in, rl->u.dbrec.buf, rl->u.dbrec.size, 0);
2817 if (!(*etype->fun)(p->odr_in, (char **) &rr, 0, 0))
2820 if (etype->what != Z_External_explainRecord)
2823 return ir_tcl_get_explain (interp, rr, argc, argv);
2827 * do_responseStatus: Return response status (present or search)
2829 static int do_responseStatus (void *o, Tcl_Interp *interp,
2830 int argc, char **argv)
2832 IrTcl_SetObj *obj = o;
2836 obj->recordFlag = 0;
2837 obj->nonSurrogateDiagnosticNum = 0;
2838 obj->nonSurrogateDiagnosticList = NULL;
2841 else if (argc == -1)
2843 ir_deleteDiags (&obj->nonSurrogateDiagnosticList,
2844 &obj->nonSurrogateDiagnosticNum);
2847 if (!obj->recordFlag)
2849 Tcl_AppendElement (interp, "OK");
2854 case Z_Records_DBOSD:
2855 Tcl_AppendElement (interp, "DBOSD");
2858 Tcl_AppendElement (interp, "NSD");
2859 return ir_diagResult (interp, obj->nonSurrogateDiagnosticList,
2860 obj->nonSurrogateDiagnosticNum);
2861 case Z_Records_multipleNSD:
2862 Tcl_AppendElement (interp, "NSD");
2863 return ir_diagResult (interp, obj->nonSurrogateDiagnosticList,
2864 obj->nonSurrogateDiagnosticNum);
2870 * do_present: Perform Present Request
2873 static int do_present (void *o, Tcl_Interp *interp, int argc, char **argv)
2875 IrTcl_SetObj *obj = o;
2878 Z_PresentRequest *req;
2886 if (Tcl_GetInt (interp, argv[2], &start) == TCL_ERROR)
2893 if (Tcl_GetInt (interp, argv[3], &number) == TCL_ERROR)
2898 logf (LOG_DEBUG, "present %s %d %d", *argv, start, number);
2902 Tcl_AppendResult (interp, "not connected", NULL);
2903 return ir_tcl_error_exec (interp, argc, argv);
2906 obj->number = number;
2908 apdu = zget_APDU (p->odr_out, Z_APDU_presentRequest);
2909 req = apdu->u.presentRequest;
2911 set_referenceId (p->odr_out, &req->referenceId,
2912 obj->set_inher.referenceId);
2914 req->resultSetId = obj->setName ? obj->setName : "Default";
2916 req->resultSetStartPoint = &start;
2917 req->numberOfRecordsRequested = &number;
2918 if (obj->set_inher.preferredRecordSyntax)
2920 struct oident ident;
2922 ident.proto = p->protocol_type;
2923 ident.oclass = CLASS_RECSYN;
2924 ident.value = *obj->set_inher.preferredRecordSyntax;
2925 logf (LOG_DEBUG, "Preferred record syntax is %d", ident.value);
2926 req->preferredRecordSyntax = odr_oiddup (p->odr_out,
2927 oid_getoidbyent (&ident));
2930 req->preferredRecordSyntax = 0;
2932 if (obj->set_inher.elementSetNames && *obj->set_inher.elementSetNames)
2934 Z_ElementSetNames *esn = odr_malloc (p->odr_out, sizeof(*esn));
2935 Z_RecordComposition *compo = odr_malloc (p->odr_out, sizeof(*compo));
2937 esn->which = Z_ElementSetNames_generic;
2938 esn->u.generic = obj->set_inher.elementSetNames;
2940 req->recordComposition = compo;
2941 compo->which = Z_RecordComp_simple;
2942 compo->u.simple = esn;
2945 req->recordComposition = NULL;
2946 return ir_tcl_send_APDU (interp, p, apdu, "present", *argv);
2949 #define IR_TCL_RECORD_ENCODING_ISO2709 1
2950 #define IR_TCL_RECORD_ENCODING_RAW 2
2956 } IrTcl_FileRecordHead;
2959 * do_loadFile: Load result set from file
2961 static int do_loadFile (void *o, Tcl_Interp *interp,
2962 int argc, char **argv)
2964 IrTcl_SetObj *setobj = o;
2976 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2977 " filename ?start? ?number?\"", NULL);
2981 start = atoi (argv[3]);
2983 number = atoi (argv[4]);
2986 inf = fopen (argv[2], "r");
2989 Tcl_AppendResult (interp, "Cannot open ", argv[2], NULL);
2990 return ir_tcl_error_exec (interp, argc, argv);
2992 while (offset < (start+number))
2994 IrTcl_FileRecordHead head;
2995 IrTcl_RecordList *rl;
2997 if (fread (&head, sizeof(head), 1, inf) < 1)
2999 rl = new_IR_record (setobj, offset,
3000 Z_NamePlusRecord_databaseRecord,
3001 (argc > 5) ? argv[5] : NULL);
3002 rl->u.dbrec.type = head.syntax;
3003 if (head.encoding == IR_TCL_RECORD_ENCODING_ISO2709)
3005 if (!(buf = ir_tcl_fread_marc (inf, &size)))
3007 rl->u.dbrec.buf = buf;
3008 rl->u.dbrec.size = size;
3009 if (size != head.size)
3012 Tcl_AppendResult (interp, "bad ISO2709 encoding", NULL);
3013 return ir_tcl_error_exec (interp, argc, argv);
3016 else if (head.encoding == IR_TCL_RECORD_ENCODING_RAW)
3018 rl->u.dbrec.size = head.size;
3019 rl->u.dbrec.buf = ir_tcl_malloc (head.size + 1);
3020 if (fread (rl->u.dbrec.buf, rl->u.dbrec.size, 1, inf) < 1)
3023 Tcl_AppendResult (interp, "bad raw encoding", NULL);
3024 return ir_tcl_error_exec (interp, argc, argv);
3026 rl->u.dbrec.buf[rl->u.dbrec.size] = '\0';
3030 rl->u.dbrec.buf = NULL;
3031 rl->u.dbrec.size = 0;
3033 Tcl_AppendResult (interp, "bad encoding", NULL);
3034 return ir_tcl_error_exec (interp, argc, argv);
3038 setobj->numberOfRecordsReturned = offset - start;
3044 * do_saveFile: Save result set on file
3046 static int do_saveFile (void *o, Tcl_Interp *interp,
3047 int argc, char **argv)
3049 IrTcl_SetObj *setobj = o;
3054 IrTcl_RecordList *rl;
3060 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
3061 " filename ?start? ?number?\"", NULL);
3065 start = atoi (argv[3]);
3067 number = atoi (argv[4]);
3070 outf = fopen (argv[2], "w");
3073 Tcl_AppendResult (interp, "cannot open file", NULL);
3074 return ir_tcl_error_exec (interp, argc, argv);
3076 while (offset < (start+number) && (rl = find_IR_record (setobj, offset)))
3078 if (rl->which == Z_NamePlusRecord_databaseRecord &&
3079 rl->u.dbrec.buf && rl->u.dbrec.size)
3081 IrTcl_FileRecordHead head;
3083 head.encoding = IR_TCL_RECORD_ENCODING_RAW;
3084 head.syntax = rl->u.dbrec.type;
3085 head.size = rl->u.dbrec.size;
3086 if (fwrite (&head, sizeof(head), 1, outf) < 1)
3088 Tcl_AppendResult (interp, "cannot write", NULL);
3089 return ir_tcl_error_exec (interp, argc, argv);
3091 if (fwrite (rl->u.dbrec.buf, rl->u.dbrec.size, 1, outf) < 1)
3093 Tcl_AppendResult (interp, "cannot write", NULL);
3094 return ir_tcl_error_exec (interp, argc, argv);
3101 Tcl_AppendResult (interp, "cannot write ", NULL);
3102 return ir_tcl_error_exec (interp, argc, argv);
3108 /* ------------------------------------------------------- */
3110 * do_sort: Do sort request
3112 static int do_sort (void *o, Tcl_Interp *interp, int argc, char **argv)
3116 IrTcl_SetObj *obj = o;
3118 char sort_string[64], sort_flags[64];
3121 Z_SortKeySpecList *sksl;
3132 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1], "query\"",
3136 logf (LOG_DEBUG, "sort %s %s", *argv, argv[2]);
3139 Tcl_AppendResult (interp, "not connected", NULL);
3140 return ir_tcl_error_exec (interp, argc, argv);
3142 apdu = zget_APDU (p->odr_out, Z_APDU_sortRequest);
3143 sksl = (Z_SortKeySpecList *) odr_malloc (p->odr_out, sizeof(*sksl));
3144 req = apdu->u.sortRequest;
3146 set_referenceId (p->odr_out, &req->referenceId,
3147 obj->set_inher.referenceId);
3150 req->num_inputResultSetNames = 1;
3151 req->inputResultSetNames = (Z_InternationalString **)
3152 odr_malloc (p->odr_out, sizeof(*req->inputResultSetNames));
3153 req->inputResultSetNames[0] = obj->setName;
3155 req->inputResultSetNames =
3156 (Z_StringList *)odr_malloc (p->odr_out,
3157 sizeof(*req->inputResultSetNames));
3158 req->inputResultSetNames->num_strings = 1;
3159 req->inputResultSetNames->strings =
3160 (char **)odr_malloc (p->odr_out,
3161 sizeof(*req->inputResultSetNames->strings));
3162 req->inputResultSetNames->strings[0] = obj->setName;
3165 req->sortedResultSetName = (char *) obj->setName;
3168 req->sortSequence = sksl;
3169 sksl->num_specs = 0;
3170 sksl->specs = (Z_SortKeySpec **)
3171 odr_malloc (p->odr_out, sizeof(sksl->specs) * 20);
3173 bib1.proto = PROTO_Z3950;
3174 bib1.oclass = CLASS_ATTSET;
3175 bib1.value = VAL_BIB1;
3177 while ((sscanf (arg, "%63s %63s%n", sort_string, sort_flags, &off)) == 2
3181 char *sort_string_sep;
3182 Z_SortKeySpec *sks = (Z_SortKeySpec *)
3183 odr_malloc (p->odr_out, sizeof(*sks));
3184 Z_SortKey *sk = (Z_SortKey *)
3185 odr_malloc (p->odr_out, sizeof(*sk));
3188 sksl->specs[sksl->num_specs++] = sks;
3189 sks->sortElement = (Z_SortElement *)
3190 odr_malloc (p->odr_out, sizeof(*sks->sortElement));
3191 sks->sortElement->which = Z_SortElement_generic;
3192 sks->sortElement->u.generic = sk;
3194 if ((sort_string_sep = strchr (sort_string, '=')))
3196 Z_AttributeElement *el = (Z_AttributeElement *)
3197 odr_malloc (p->odr_out, sizeof(*el));
3198 sk->which = Z_SortKey_sortAttributes;
3199 sk->u.sortAttributes =
3200 (Z_SortAttributes *)
3201 odr_malloc (p->odr_out, sizeof(*sk->u.sortAttributes));
3202 sk->u.sortAttributes->id = oid_ent_to_oid(&bib1, oid);
3203 sk->u.sortAttributes->list =
3205 odr_malloc (p->odr_out, sizeof(*sk->u.sortAttributes->list));
3206 sk->u.sortAttributes->list->num_attributes = 1;
3207 sk->u.sortAttributes->list->attributes =
3208 (Z_AttributeElement **)odr_malloc (p->odr_out,
3209 sizeof(*sk->u.sortAttributes->list->attributes));
3210 sk->u.sortAttributes->list->attributes[0] = el;
3211 el->attributeSet = 0;
3212 el->attributeType = (int *)
3213 odr_malloc (p->odr_out, sizeof(*el->attributeType));
3214 *el->attributeType = atoi (sort_string);
3215 el->which = Z_AttributeValue_numeric;
3216 el->value.numeric = (int *)
3217 odr_malloc (p->odr_out, sizeof(*el->value.numeric));
3218 *el->value.numeric = atoi (sort_string_sep + 1);
3222 sk->which = Z_SortKey_sortField;
3223 sk->u.sortField = (char *)odr_malloc (p->odr_out, strlen(sort_string)+1);
3224 strcpy (sk->u.sortField, sort_string);
3226 sks->sortRelation = (int *)
3227 odr_malloc (p->odr_out, sizeof(*sks->sortRelation));
3228 *sks->sortRelation = Z_SortRelation_ascending;
3229 sks->caseSensitivity = (int *)
3230 odr_malloc (p->odr_out, sizeof(*sks->caseSensitivity));
3231 *sks->caseSensitivity = Z_SortCase_caseSensitive;
3234 sks->which = Z_SortKeySpec_null;
3235 sks->u.null = odr_nullval ();
3237 sks->missingValueAction = NULL;
3240 for (i = 0; sort_flags[i]; i++)
3242 switch (sort_flags[i])
3247 *sks->sortRelation = Z_SortRelation_descending;
3252 *sks->sortRelation = Z_SortRelation_ascending;
3256 *sks->caseSensitivity = Z_SortCase_caseInsensitive;
3260 *sks->caseSensitivity = Z_SortCase_caseSensitive;
3265 if (!sksl->num_specs)
3267 printf ("Missing sort specifications\n");
3270 return ir_tcl_send_APDU (interp, p, apdu, "sort", *argv);
3273 static IrTcl_Method ir_set_method_tab[] = {
3274 { "search", do_search, NULL},
3275 { "searchResponse", do_searchResponse, NULL},
3276 { "presentResponse", do_presentResponse, NULL},
3277 { "searchStatus", do_searchStatus, NULL},
3278 { "presentStatus", do_presentStatus, NULL},
3279 { "nextResultSetPosition", do_nextResultSetPosition, NULL},
3280 { "setName", do_setName, NULL},
3281 { "resultCount", do_resultCount, NULL},
3282 { "numberOfRecordsReturned", do_numberOfRecordsReturned, NULL},
3283 { "present", do_present, NULL},
3284 { "type", do_type, NULL},
3285 { "getMarc", do_getMarc, NULL},
3286 { "getSutrs", do_getSutrs, NULL},
3287 { "getGrs", do_getGrs, NULL},
3288 { "getExplain", do_getExplain, NULL},
3289 { "recordType", do_recordType, NULL},
3290 { "recordElements", do_recordElements, NULL},
3291 { "diag", do_diag, NULL},
3292 { "responseStatus", do_responseStatus, NULL},
3293 { "loadFile", do_loadFile, NULL},
3294 { "saveFile", do_saveFile, NULL},
3295 { "sort", do_sort, NULL },
3296 { "sortResponse", do_sortResponse, NULL},
3297 { "sortStatus", do_sortStatus, NULL},
3298 { "searchResult", do_searchResult, NULL},
3303 * ir_set_obj_method: IR Set Object methods
3305 static int ir_set_obj_method (ClientData clientData, Tcl_Interp *interp,
3306 int argc, char **argv)
3308 IrTcl_Methods tabs[3];
3309 IrTcl_SetObj *p = (IrTcl_SetObj *) clientData;
3314 Tcl_AppendResult (interp, wrongArgs, *argv, " method args...\"", NULL);
3317 tabs[0].tab = ir_set_method_tab;
3319 tabs[1].tab = ir_set_c_method_tab;
3320 tabs[1].obj = &p->set_inher;
3323 if (ir_tcl_method (interp, argc, argv, tabs, &r) == TCL_ERROR)
3324 return ir_tcl_method_error (interp, argc, argv, tabs);
3329 * ir_set_obj_delete: IR Set Object disposal
3331 static void ir_set_obj_delete (ClientData clientData)
3333 IrTcl_Methods tabs[3];
3334 IrTcl_SetObj *p = (IrTcl_SetObj *) clientData;
3336 logf (LOG_DEBUG, "ir set delete");
3338 tabs[0].tab = ir_set_method_tab;
3340 tabs[1].tab = ir_set_c_method_tab;
3341 tabs[1].obj = &p->set_inher;
3344 ir_tcl_method (NULL, -1, NULL, tabs, NULL);
3350 * ir_set_obj_init: IR Set Object initialization
3352 static int ir_set_obj_init (ClientData clientData, Tcl_Interp *interp,
3353 int argc, char **argv, ClientData *subData,
3354 ClientData parentData)
3356 IrTcl_Methods tabs[3];
3359 if (argc < 2 || argc > 3)
3361 Tcl_AppendResult (interp, wrongArgs, *argv,
3362 " objSetName ?objParent?\"", NULL);
3365 obj = ir_tcl_malloc (sizeof(*obj));
3366 logf (LOG_DEBUG, "ir set create %s", argv[1]);
3373 obj->parent = (IrTcl_Obj *) parentData;
3375 dst = &obj->set_inher;
3376 src = &obj->parent->set_inher;
3378 if ((dst->num_databaseNames = src->num_databaseNames))
3380 dst->databaseNames =
3381 ir_tcl_malloc (sizeof (*dst->databaseNames)
3382 * (1+dst->num_databaseNames));
3383 for (i = 0; i < dst->num_databaseNames; i++)
3384 if (ir_tcl_strdup (interp, &dst->databaseNames[i],
3385 src->databaseNames[i]) == TCL_ERROR)
3387 dst->databaseNames[i] = NULL;
3390 dst->databaseNames = NULL;
3391 if (ir_tcl_strdup (interp, &dst->queryType, src->queryType)
3395 if (ir_tcl_strdup (interp, &dst->referenceId, src->referenceId)
3399 if (ir_tcl_strdup (interp, &dst->elementSetNames, src->elementSetNames)
3403 if (ir_tcl_strdup (interp, &dst->smallSetElementSetNames,
3404 src->smallSetElementSetNames)
3408 if (ir_tcl_strdup (interp, &dst->mediumSetElementSetNames,
3409 src->mediumSetElementSetNames)
3413 if (src->preferredRecordSyntax &&
3414 (dst->preferredRecordSyntax
3415 = ir_tcl_malloc (sizeof(*dst->preferredRecordSyntax))))
3416 *dst->preferredRecordSyntax = *src->preferredRecordSyntax;
3418 dst->preferredRecordSyntax = NULL;
3419 dst->replaceIndicator = src->replaceIndicator;
3420 dst->smallSetUpperBound = src->smallSetUpperBound;
3421 dst->largeSetLowerBound = src->largeSetLowerBound;
3422 dst->mediumSetPresentNumber = src->mediumSetPresentNumber;
3427 tabs[0].tab = ir_set_method_tab;
3431 if (ir_tcl_method (interp, 0, NULL, tabs, NULL) == TCL_ERROR)
3434 *subData = (ClientData) obj;
3439 * ir_set_obj_mk: IR Set Object creation
3441 static int ir_set_obj_mk (ClientData clientData, Tcl_Interp *interp,
3442 int argc, char **argv)
3445 ClientData parentData = 0;
3450 Tcl_CmdInfo parent_info;
3451 if (!Tcl_GetCommandInfo (interp, argv[2], &parent_info))
3453 Tcl_AppendResult (interp, "no object parent", NULL);
3454 return ir_tcl_error_exec (interp, argc, argv);
3456 parentData = parent_info.clientData;
3458 r = ir_set_obj_init (clientData, interp, argc, argv, &subData, parentData);
3461 Tcl_CreateCommand (interp, argv[1], ir_set_obj_method,
3462 subData, ir_set_obj_delete);
3466 IrTcl_Class ir_set_obj_class = {
3473 /* ------------------------------------------------------- */
3476 * do_scan: Perform scan
3478 static int do_scan (void *o, Tcl_Interp *interp, int argc, char **argv)
3482 IrTcl_ScanObj *obj = o;
3483 IrTcl_Obj *p = obj->parent;
3488 struct ccl_rpn_node *rpn;
3492 #if TCL_MAJOR_VERSION > 8 || (TCL_MAJOR_VERSION == 8 && TCL_MINOR_VERSION > 0)
3500 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
3501 " scanQuery\"", NULL);
3504 #if TCL_MAJOR_VERSION > 8 || (TCL_MAJOR_VERSION == 8 && TCL_MINOR_VERSION > 0)
3505 start_term = Tcl_UtfToExternalDString(0, argv[2], -1, &ds);
3507 start_term = argv[2];
3509 logf (LOG_DEBUG, "scan %s %s", *argv, start_term);
3510 if (!p->set_inher.num_databaseNames)
3512 Tcl_AppendResult (interp, "no databaseNames", NULL);
3513 code = ir_tcl_error_exec (interp, argc, argv);
3518 Tcl_AppendResult (interp, "not connected", NULL);
3519 code = ir_tcl_error_exec (interp, argc, argv);
3523 apdu = zget_APDU (p->odr_out, Z_APDU_scanRequest);
3524 req = apdu->u.scanRequest;
3526 set_referenceId (p->odr_out, &req->referenceId, p->set_inher.referenceId);
3527 req->num_databaseNames = p->set_inher.num_databaseNames;
3528 req->databaseNames = p->set_inher.databaseNames;
3532 if (!(req->termListAndStartPoint =
3533 p_query_scan (p->odr_out, p->protocol_type,
3534 &req->attributeSet, start_term)))
3536 Tcl_AppendResult (interp, "query syntax error", NULL);
3537 code = ir_tcl_error_exec (interp, argc, argv);
3541 rpn = ccl_find_str(p->bibset, start_term, &r, &pos);
3544 Tcl_AppendResult (interp, "ccl syntax error ", ccl_err_msg(r), NULL);
3545 code = ir_tcl_error_exec (interp, argc, argv);
3548 bib1.proto = p->protocol_type;
3549 bib1.oclass = CLASS_ATTSET;
3550 bib1.value = VAL_BIB1;
3552 req->attributeSet = oid_getoidbyent (&bib1);
3553 if (!(req->termListAndStartPoint = ccl_scan_query (p->odr_out, rpn)))
3559 req->stepSize = &obj->stepSize;
3560 req->numberOfTermsRequested = &obj->numberOfTermsRequested;
3561 req->preferredPositionInResponse = &obj->preferredPositionInResponse;
3562 logf (LOG_DEBUG, "stepSize=%d", *req->stepSize);
3563 logf (LOG_DEBUG, "numberOfTermsRequested=%d",
3564 *req->numberOfTermsRequested);
3565 logf (LOG_DEBUG, "preferredPositionInResponse=%d",
3566 *req->preferredPositionInResponse);
3568 code = ir_tcl_send_APDU (interp, p, apdu, "scan", *argv);
3570 #if TCL_MAJOR_VERSION > 8 || (TCL_MAJOR_VERSION == 8 && TCL_MINOR_VERSION > 0)
3571 Tcl_DStringFree (&ds);
3577 * do_scanResponse: add scan response handler
3579 static int do_scanResponse (void *o, Tcl_Interp *interp,
3580 int argc, char **argv)
3582 IrTcl_ScanObj *obj = o;
3586 obj->scanResponse = NULL;
3589 else if (argc == -1)
3590 return ir_tcl_strdel (interp, &obj->scanResponse);
3593 xfree (obj->scanResponse);
3596 if (ir_tcl_strdup (interp, &obj->scanResponse, argv[2])
3601 obj->scanResponse = NULL;
3607 * do_stepSize: Set/get replace Step Size
3609 static int do_stepSize (void *obj, Tcl_Interp *interp,
3610 int argc, char **argv)
3612 IrTcl_ScanObj *p = obj;
3618 return ir_tcl_get_set_int (&p->stepSize, interp, argc, argv);
3622 * do_numberOfTermsRequested: Set/get Number of Terms requested
3624 static int do_numberOfTermsRequested (void *obj, Tcl_Interp *interp,
3625 int argc, char **argv)
3627 IrTcl_ScanObj *p = obj;
3631 p->numberOfTermsRequested = 20;
3634 return ir_tcl_get_set_int (&p->numberOfTermsRequested, interp, argc, argv);
3639 * do_preferredPositionInResponse: Set/get preferred Position
3641 static int do_preferredPositionInResponse (void *obj, Tcl_Interp *interp,
3642 int argc, char **argv)
3644 IrTcl_ScanObj *p = obj;
3648 p->preferredPositionInResponse = 1;
3651 return ir_tcl_get_set_int (&p->preferredPositionInResponse, interp,
3656 * do_scanStatus: Get scan status
3658 static int do_scanStatus (void *obj, Tcl_Interp *interp,
3659 int argc, char **argv)
3661 IrTcl_ScanObj *p = obj;
3665 return ir_tcl_get_set_int (&p->scanStatus, interp, argc, argv);
3669 * do_numberOfEntriesReturned: Get number of Entries returned
3671 static int do_numberOfEntriesReturned (void *obj, Tcl_Interp *interp,
3672 int argc, char **argv)
3674 IrTcl_ScanObj *p = obj;
3678 return ir_tcl_get_set_int (&p->numberOfEntriesReturned, interp,
3683 * do_positionOfTerm: Get position of Term
3685 static int do_positionOfTerm (void *obj, Tcl_Interp *interp,
3686 int argc, char **argv)
3688 IrTcl_ScanObj *p = obj;
3692 return ir_tcl_get_set_int (&p->positionOfTerm, interp, argc, argv);
3696 * do_scanLine: get Scan Line (surrogate or normal) after response
3698 static int do_scanLine (void *obj, Tcl_Interp *interp, int argc, char **argv)
3700 IrTcl_ScanObj *p = obj;
3706 p->entries_flag = 0;
3708 p->nonSurrogateDiagnosticNum = 0;
3709 p->nonSurrogateDiagnosticList = 0;
3712 else if (argc == -1)
3714 p->entries_flag = 0;
3715 /* release entries */
3718 ir_deleteDiags (&p->nonSurrogateDiagnosticList,
3719 &p->nonSurrogateDiagnosticNum);
3724 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
3725 " position\"", NULL);
3728 if (Tcl_GetInt (interp, argv[2], &i) == TCL_ERROR)
3730 if (!p->entries_flag || !p->entries || i >= p->num_entries || i < 0)
3732 switch (p->entries[i].which)
3734 case Z_Entry_termInfo:
3735 Tcl_AppendElement (interp, "T");
3736 if (p->entries[i].u.term.buf)
3737 Tcl_AppendElement (interp, p->entries[i].u.term.buf);
3739 Tcl_AppendElement (interp, "");
3740 sprintf (numstr, "%d", p->entries[i].u.term.globalOccurrences);
3741 Tcl_AppendElement (interp, numstr);
3743 case Z_Entry_surrogateDiagnostic:
3744 Tcl_AppendElement (interp, "SD");
3745 return ir_diagResult (interp, p->entries[i].u.diag.list,
3746 p->entries[i].u.diag.num);
3752 static IrTcl_Method ir_scan_method_tab[] = {
3753 { "scan", do_scan, NULL},
3754 { "scanResponse", do_scanResponse, NULL},
3755 { "stepSize", do_stepSize, NULL},
3756 { "numberOfTermsRequested", do_numberOfTermsRequested, NULL},
3757 { "preferredPositionInResponse", do_preferredPositionInResponse, NULL},
3758 { "scanStatus", do_scanStatus, NULL},
3759 { "numberOfEntriesReturned", do_numberOfEntriesReturned, NULL},
3760 { "positionOfTerm", do_positionOfTerm, NULL},
3761 { "scanLine", do_scanLine, NULL},
3766 * ir_scan_obj_method: IR Scan Object methods
3768 static int ir_scan_obj_method (ClientData clientData, Tcl_Interp *interp,
3769 int argc, char **argv)
3771 IrTcl_Methods tabs[2];
3776 Tcl_AppendResult (interp, wrongArgs, *argv, " method args...\"", NULL);
3779 tabs[0].tab = ir_scan_method_tab;
3780 tabs[0].obj = clientData;
3783 if (ir_tcl_method (interp, argc, argv, tabs, &r) == TCL_ERROR)
3784 return ir_tcl_method_error (interp, argc, argv, tabs);
3789 * ir_scan_obj_delete: IR Scan Object disposal
3791 static void ir_scan_obj_delete (ClientData clientData)
3793 IrTcl_Methods tabs[2];
3794 IrTcl_ScanObj *obj = (IrTcl_ScanObj *) clientData;
3796 tabs[0].tab = ir_scan_method_tab;
3800 ir_tcl_method (NULL, -1, NULL, tabs, NULL);
3805 * ir_scan_obj_mk: IR Scan Object creation
3807 static int ir_scan_obj_mk (ClientData clientData, Tcl_Interp *interp,
3808 int argc, char **argv)
3810 Tcl_CmdInfo parent_info;
3812 IrTcl_Methods tabs[2];
3816 Tcl_AppendResult (interp, wrongArgs, *argv,
3817 "objScanName objParentName\"", NULL);
3820 logf (LOG_DEBUG, "ir scan create %s", argv[1]);
3821 if (!Tcl_GetCommandInfo (interp, argv[2], &parent_info))
3823 Tcl_AppendResult (interp, "no object parent", NULL);
3824 return ir_tcl_error_exec (interp, argc, argv);
3826 obj = ir_tcl_malloc (sizeof(*obj));
3827 obj->parent = (IrTcl_Obj *) parent_info.clientData;
3829 tabs[0].tab = ir_scan_method_tab;
3833 if (ir_tcl_method (interp, 0, NULL, tabs, NULL) == TCL_ERROR)
3835 Tcl_CreateCommand (interp, argv[1], ir_scan_obj_method,
3836 (ClientData) obj, ir_scan_obj_delete);
3840 /* ------------------------------------------------------- */
3843 * ir_log_init_proc: set yaz log level
3845 static int ir_log_init_proc (ClientData clientData, Tcl_Interp *interp,
3846 int argc, char **argv)
3848 if (argc <= 1 || argc > 4)
3850 Tcl_AppendResult (interp, wrongArgs, *argv,
3851 " ?level ?prefix ?filename\"", NULL);
3855 yaz_log_init (yaz_log_mask_str (argv[1]), "", NULL);
3857 yaz_log_init (yaz_log_mask_str (argv[1]), argv[2], NULL);
3859 yaz_log_init (yaz_log_mask_str (argv[1]), argv[2], argv[3]);
3864 * ir_log_proc: log yaz message
3866 static int ir_log_proc (ClientData clientData, Tcl_Interp *interp,
3867 int argc, char **argv)
3872 Tcl_AppendResult (interp, wrongArgs, *argv,
3873 " level string\"", NULL);
3876 mask = yaz_log_mask_str_x (argv[1], 0);
3877 logf (LOG_DEBUG, "%s", argv[2]);
3883 * ir_version: log ir version
3885 static int ir_version (ClientData clientData, Tcl_Interp *interp,
3886 int argc, char **argv)
3888 Tcl_AppendElement (interp, IR_TCL_VERSION);
3889 Tcl_AppendElement (interp, YAZ_VERSION);
3894 /* ------------------------------------------------------- */
3895 static void ir_initResponse (void *obj, Z_InitResponse *initrs)
3899 p->initResult = *initrs->result ? 1 : 0;
3900 if (!*initrs->result)
3901 logf (LOG_DEBUG, "Connection rejected by target");
3903 logf (LOG_DEBUG, "Connection accepted by target");
3905 get_referenceId (&p->set_inher.referenceId, initrs->referenceId);
3907 xfree (p->targetImplementationId);
3908 ir_tcl_strdup (p->interp, &p->targetImplementationId,
3909 initrs->implementationId);
3910 xfree (p->targetImplementationName);
3911 ir_tcl_strdup (p->interp, &p->targetImplementationName,
3912 initrs->implementationName);
3913 xfree (p->targetImplementationVersion);
3914 ir_tcl_strdup (p->interp, &p->targetImplementationVersion,
3915 initrs->implementationVersion);
3917 p->maximumRecordSize = *initrs->maximumRecordSize;
3918 p->preferredMessageSize = *initrs->preferredMessageSize;
3920 memcpy (&p->options, initrs->options, sizeof(initrs->options));
3921 memcpy (&p->protocolVersion, initrs->protocolVersion,
3922 sizeof(initrs->protocolVersion));
3923 xfree (p->userInformationField);
3924 p->userInformationField = NULL;
3925 if (initrs->userInformationField)
3929 if (initrs->userInformationField->which == ODR_EXTERNAL_octet &&
3930 (p->userInformationField =
3931 ir_tcl_malloc ((len =
3932 initrs->userInformationField->
3933 u.octet_aligned->len) +1)))
3935 memcpy (p->userInformationField,
3936 initrs->userInformationField->u.octet_aligned->buf,
3938 (p->userInformationField)[len] = '\0';
3943 static void ir_deleteDiags (IrTcl_Diagnostic **dst_list, int *dst_num)
3946 for (i = 0; i<*dst_num; i++)
3947 xfree ((*dst_list)[i].addinfo);
3953 static void ir_handleDiags (IrTcl_Diagnostic **dst_list, int *dst_num,
3954 Z_DiagRec **list, int num)
3957 char *addinfo = NULL;
3960 *dst_list = ir_tcl_malloc (sizeof(**dst_list) * num);
3961 for (i = 0; i<num; i++)
3964 switch (list[i]->which)
3966 case Z_DiagRec_defaultFormat:
3967 (*dst_list)[i].condition = *list[i]->u.defaultFormat->condition;
3969 switch (list[i]->u.defaultFormat->which)
3971 case Z_DefaultDiagFormat_v2Addinfo:
3972 addinfo = list[i]->u.defaultFormat->u.v2Addinfo;
3974 case Z_DefaultDiagFormat_v3Addinfo:
3975 addinfo = list[i]->u.defaultFormat->u.v3Addinfo;
3979 addinfo = list[i]->u.defaultFormat->addinfo;
3982 ((*dst_list)[i].addinfo = ir_tcl_malloc (strlen(addinfo)+1)))
3983 strcpy ((*dst_list)[i].addinfo, addinfo);
3984 cp = diagbib1_str ((*dst_list)[i].condition);
3985 logf (LOG_DEBUG, "Diag %d %s %s", (*dst_list)[i].condition,
3986 cp ? cp : "", addinfo ? addinfo : "");
3989 (*dst_list)[i].addinfo = NULL;
3990 (*dst_list)[i].condition = 0;
3995 static void ir_handleDBRecord (IrTcl_Obj *p, IrTcl_RecordList *rl,
3998 struct oident *ident;
3999 Z_ext_typeent *etype;
4001 logf (LOG_DEBUG, "handleDBRecord size=%d", oe->u.octet_aligned->len);
4002 rl->u.dbrec.size = oe->u.octet_aligned->len;
4003 rl->u.dbrec.buf = NULL;
4005 if ((ident = oid_getentbyoid (oe->direct_reference)))
4006 rl->u.dbrec.type = ident->value;
4008 rl->u.dbrec.type = VAL_USMARC;
4010 if (ident && (oe->which == Z_External_single ||
4011 oe->which == Z_External_octet)
4012 && (etype = z_ext_getentbyref (ident->value)))
4016 odr_setbuf (p->odr_in, (char*) oe->u.octet_aligned->buf,
4017 oe->u.octet_aligned->len, 0);
4018 if (!(*etype->fun)(p->odr_in, (char **) &rr, 0, 0))
4020 rl->u.dbrec.type = VAL_NONE;
4023 switch (etype->what)
4025 case Z_External_sutrs:
4026 logf (LOG_DEBUG, "Z_External_sutrs");
4028 if ((rl->u.dbrec.buf = ir_tcl_malloc (oe->u.sutrs->len+1)))
4030 memcpy (rl->u.dbrec.buf, oe->u.sutrs->buf,
4032 rl->u.dbrec.buf[oe->u.sutrs->len] = '\0';
4034 rl->u.dbrec.size = oe->u.sutrs->len;
4036 case Z_External_grs1:
4037 logf (LOG_DEBUG, "Z_External_grs1");
4039 ir_tcl_grs_mk (oe->u.grs1, &rl->u.dbrec.u.grs1);
4041 case Z_External_explainRecord:
4042 logf (LOG_DEBUG, "Z_External_explainRecord");
4043 if ((rl->u.dbrec.buf = ir_tcl_malloc (rl->u.dbrec.size)))
4045 memcpy (rl->u.dbrec.buf, oe->u.octet_aligned->buf,
4053 if (oe->which == Z_External_octet && rl->u.dbrec.size > 0)
4055 char *buf = (char*) oe->u.octet_aligned->buf;
4056 if ((rl->u.dbrec.buf = ir_tcl_malloc (rl->u.dbrec.size)))
4057 memcpy (rl->u.dbrec.buf, buf, rl->u.dbrec.size);
4059 else if (rl->u.dbrec.type == VAL_SUTRS &&
4060 oe->which == Z_External_sutrs)
4062 if ((rl->u.dbrec.buf = ir_tcl_malloc (oe->u.sutrs->len+1)))
4064 memcpy (rl->u.dbrec.buf, oe->u.sutrs->buf,
4066 rl->u.dbrec.buf[oe->u.sutrs->len] = '\0';
4068 rl->u.dbrec.size = oe->u.sutrs->len;
4070 else if (rl->u.dbrec.type == VAL_GRS1 &&
4071 oe->which == Z_External_grs1)
4073 ir_tcl_grs_mk (oe->u.grs1, &rl->u.dbrec.u.grs1);
4078 static void ir_handleZRecords (void *o, Z_Records *zrs, IrTcl_SetObj *setobj,
4079 const char *elements)
4084 IrTcl_RecordList *rl;
4086 setobj->which = zrs->which;
4087 setobj->recordFlag = 1;
4089 ir_deleteDiags (&setobj->nonSurrogateDiagnosticList,
4090 &setobj->nonSurrogateDiagnosticNum);
4091 if (zrs->which == Z_Records_DBOSD)
4093 int num_rec = zrs->u.databaseOrSurDiagnostics->num_records;
4095 if (num_rec != setobj->numberOfRecordsReturned)
4097 logf (LOG_WARN, "numberOfRecordsReturned=%d but num records=%d",
4098 setobj->numberOfRecordsReturned, num_rec);
4099 setobj->numberOfRecordsReturned = num_rec;
4102 for (offset = 0; offset < num_rec; offset++)
4104 Z_NamePlusRecord *znpr = zrs->u.databaseOrSurDiagnostics->
4107 rl = new_IR_record (setobj, setobj->start + offset, znpr->which,
4109 if (rl->which == Z_NamePlusRecord_surrogateDiagnostic)
4110 ir_handleDiags (&rl->u.surrogateDiagnostics.list,
4111 &rl->u.surrogateDiagnostics.num,
4112 &znpr->u.surrogateDiagnostic,
4115 ir_handleDBRecord (p, rl,
4116 (Z_External*) (znpr->u.databaseRecord));
4119 else if (zrs->which == Z_Records_multipleNSD)
4121 logf (LOG_DEBUG, "multipleNonSurrogateDiagnostic %d",
4122 zrs->u.multipleNonSurDiagnostics->num_diagRecs);
4123 setobj->numberOfRecordsReturned = 0;
4124 ir_handleDiags (&setobj->nonSurrogateDiagnosticList,
4125 &setobj->nonSurrogateDiagnosticNum,
4126 zrs->u.multipleNonSurDiagnostics->diagRecs,
4127 zrs->u.multipleNonSurDiagnostics->num_diagRecs);
4132 Z_DiagRec dr, *dr_p = &dr;
4133 dr.which = Z_DiagRec_defaultFormat;
4134 dr.u.defaultFormat = zrs->u.nonSurrogateDiagnostic;
4136 Z_DiagRec *dr_p = zrs->u.nonSurrogateDiagnostic;
4138 logf (LOG_DEBUG, "NonSurrogateDiagnostic");
4140 setobj->numberOfRecordsReturned = 0;
4141 ir_handleDiags (&setobj->nonSurrogateDiagnosticList,
4142 &setobj->nonSurrogateDiagnosticNum,
4147 static char *set_queryExpression (Z_QueryExpression *qe)
4152 if (qe->which == Z_QueryExpression_term)
4154 if (qe->u.term->queryTerm)
4156 Z_Term *term = qe->u.term->queryTerm;
4157 if (term->which == Z_Term_general)
4159 termz = xmalloc (term->u.general->len+1);
4160 memcpy (termz, term->u.general->buf, term->u.general->len);
4161 termz[term->u.general->len] = 0;
4168 static void set_searchResult (Z_OtherInformation *o,
4169 IrTcl_SetObj *setobj)
4174 for (i = 0; i < o->num_elements; i++)
4176 if (o->list[i]->which == Z_OtherInfo_externallyDefinedInfo)
4178 ODR odr = odr_createmem (ODR_DECODE);
4179 Z_External *ext = o->list[i]->information.externallyDefinedInfo;
4180 Z_SearchInfoReport *sr = 0;
4182 if (ext->which == Z_External_single)
4184 odr_setbuf (odr, ext->u.single_ASN1_type->buf,
4185 ext->u.single_ASN1_type->len, 0);
4186 z_SearchInfoReport (odr, &sr, 0, "searchInfo");
4188 if (ext->which == Z_External_searchResult1)
4189 sr = ext->u.searchResult1;
4193 reset_searchResult (setobj);
4195 setobj->searchResult_num = sr->num;
4196 setobj->searchResult_terms =
4197 xmalloc (sr->num * sizeof(*setobj->searchResult_terms));
4198 setobj->searchResult_count =
4199 xmalloc (sr->num * sizeof(*setobj->searchResult_count));
4201 for (j = 0; j < sr->num; j++)
4203 setobj->searchResult_terms[j] =
4204 set_queryExpression (
4205 sr->elements[j]->subqueryExpression);
4206 if (sr->elements[j]->subqueryCount)
4207 setobj->searchResult_count[j] =
4208 *sr->elements[j]->subqueryCount;
4210 setobj->searchResult_count[j] = 0;
4218 static void ir_searchResponse (void *o, Z_SearchResponse *searchrs,
4219 IrTcl_SetObj *setobj)
4221 Z_Records *zrs = searchrs->records;
4223 logf (LOG_DEBUG, "Received search response");
4226 logf (LOG_DEBUG, "Search response, no object!");
4229 setobj->searchStatus = *searchrs->searchStatus;
4230 get_referenceId (&setobj->set_inher.referenceId, searchrs->referenceId);
4231 setobj->resultCount = *searchrs->resultCount;
4232 if (searchrs->presentStatus)
4233 setobj->presentStatus = *searchrs->presentStatus;
4235 setobj->presentStatus = Z_RES_NONE;
4236 if (searchrs->nextResultSetPosition)
4237 setobj->nextResultSetPosition = *searchrs->nextResultSetPosition;
4239 logf (LOG_DEBUG, "status %d hits %d",
4240 setobj->searchStatus, setobj->resultCount);
4241 set_searchResult (searchrs->additionalSearchInfo, setobj);
4245 if (setobj->resultCount <= setobj->set_inher.smallSetUpperBound)
4246 es = setobj->set_inher.smallSetElementSetNames;
4248 es = setobj->set_inher.mediumSetElementSetNames;
4249 setobj->numberOfRecordsReturned = *searchrs->numberOfRecordsReturned;
4250 ir_handleZRecords (o, zrs, setobj, es);
4254 setobj->numberOfRecordsReturned = 0;
4255 setobj->recordFlag = 0;
4260 static void ir_presentResponse (void *o, Z_PresentResponse *presrs,
4261 IrTcl_SetObj *setobj)
4263 Z_Records *zrs = presrs->records;
4265 logf (LOG_DEBUG, "Received present response");
4268 logf (LOG_DEBUG, "Present response, no object!");
4271 setobj->presentStatus = *presrs->presentStatus;
4272 get_referenceId (&setobj->set_inher.referenceId, presrs->referenceId);
4273 setobj->nextResultSetPosition = *presrs->nextResultSetPosition;
4276 setobj->numberOfRecordsReturned = *presrs->numberOfRecordsReturned;
4277 ir_handleZRecords (o, zrs, setobj, setobj->set_inher.elementSetNames);
4281 setobj->numberOfRecordsReturned = 0;
4282 setobj->recordFlag = 0;
4283 logf (LOG_DEBUG, "No records!");
4287 static void ir_scanResponse (void *o, Z_ScanResponse *scanrs,
4288 IrTcl_ScanObj *scanobj)
4292 logf (LOG_DEBUG, "Received scanResponse");
4294 get_referenceId (&p->set_inher.referenceId, scanrs->referenceId);
4295 scanobj->scanStatus = *scanrs->scanStatus;
4296 logf (LOG_DEBUG, "scanStatus=%d", scanobj->scanStatus);
4298 if (scanrs->stepSize)
4299 scanobj->stepSize = *scanrs->stepSize;
4300 logf (LOG_DEBUG, "stepSize=%d", scanobj->stepSize);
4302 scanobj->numberOfEntriesReturned = *scanrs->numberOfEntriesReturned;
4303 logf (LOG_DEBUG, "numberOfEntriesReturned=%d",
4304 scanobj->numberOfEntriesReturned);
4306 if (scanrs->positionOfTerm)
4307 scanobj->positionOfTerm = *scanrs->positionOfTerm;
4309 scanobj->positionOfTerm = -1;
4310 logf (LOG_DEBUG, "positionOfTerm=%d", scanobj->positionOfTerm);
4312 xfree (scanobj->entries);
4313 scanobj->entries = NULL;
4314 scanobj->num_entries = 0;
4315 scanobj->entries_flag = 0;
4317 ir_deleteDiags (&scanobj->nonSurrogateDiagnosticList,
4318 &scanobj->nonSurrogateDiagnosticNum);
4319 if (scanrs->entries)
4324 scanobj->entries_flag = 1;
4325 if (scanrs->entries)
4327 scanobj->num_entries = scanrs->entries->num_entries;
4328 scanobj->entries = ir_tcl_malloc (scanobj->num_entries *
4329 sizeof(*scanobj->entries));
4330 ze = scanrs->entries->entries;
4332 for (i=0; i<scanobj->num_entries; i++, ze++)
4334 scanobj->entries[i].which = (*ze)->which;
4335 switch ((*ze)->which)
4337 case Z_Entry_termInfo:
4338 if ((*ze)->u.termInfo->term->which == Z_Term_general)
4340 int l = (*ze)->u.termInfo->term->u.general->len;
4341 scanobj->entries[i].u.term.buf = ir_tcl_malloc (1+l);
4342 memcpy (scanobj->entries[i].u.term.buf,
4343 (*ze)->u.termInfo->term->u.general->buf,
4345 scanobj->entries[i].u.term.buf[l] = '\0';
4348 scanobj->entries[i].u.term.buf = NULL;
4349 if ((*ze)->u.termInfo->globalOccurrences)
4350 scanobj->entries[i].u.term.globalOccurrences =
4351 *(*ze)->u.termInfo->globalOccurrences;
4353 scanobj->entries[i].u.term.globalOccurrences = 0;
4355 case Z_Entry_surrogateDiagnostic:
4356 ir_handleDiags (&scanobj->entries[i].u.diag.list,
4357 &scanobj->entries[i].u.diag.num,
4358 &(*ze)->u.surrogateDiagnostic,
4363 if (scanrs->entries->nonsurrogateDiagnostics)
4364 ir_handleDiags (&scanobj->nonSurrogateDiagnosticList,
4365 &scanobj->nonSurrogateDiagnosticNum,
4366 scanrs->entries->nonsurrogateDiagnostics,
4367 scanrs->entries->num_nonsurrogateDiagnostics);
4372 static void ir_sortResponse (void *o, Z_SortResponse *sortrs,
4373 IrTcl_SetObj *setobj)
4377 logf (LOG_DEBUG, "Received sortResponse");
4382 purge_IR_records (setobj);
4384 get_referenceId (&p->set_inher.referenceId, sortrs->referenceId);
4386 setobj->sortStatus = *sortrs->sortStatus;
4388 ir_deleteDiags (&setobj->nonSurrogateDiagnosticList,
4389 &setobj->nonSurrogateDiagnosticNum);
4391 if (sortrs->diagnostics)
4392 ir_handleDiags (&setobj->nonSurrogateDiagnosticList,
4393 &setobj->nonSurrogateDiagnosticNum,
4394 sortrs->diagnostics,
4395 sortrs->num_diagnostics);
4397 if (sortrs->diagnostics)
4398 ir_handleDiags (&setobj->nonSurrogateDiagnosticList,
4399 &setobj->nonSurrogateDiagnosticNum,
4400 sortrs->diagnostics->diagRecs,
4401 sortrs->diagnostics->num_diagRecs);
4406 * ir_select_read: handle incoming packages
4408 static void ir_select_read (ClientData clientData)
4410 IrTcl_Obj *p = (IrTcl_Obj *) clientData;
4415 Tcl_CmdInfo cmd_info;
4416 const char *apdu_call;
4419 logf(LOG_DEBUG, "Read handler fd=%d", cs_fileno(p->cs_link));
4420 if (p->state == IR_TCL_R_Connecting)
4422 logf(LOG_DEBUG, "read: connect");
4423 r = cs_rcvconnect (p->cs_link);
4426 logf (LOG_WARN, "cs_rcvconnect returned 1");
4429 p->state = IR_TCL_R_Idle;
4431 ir_select_remove_write (cs_fileno (p->cs_link), p);
4434 logf (LOG_DEBUG, "cs_rcvconnect error");
4435 ir_tcl_disconnect (p);
4438 p->failInfo = IR_TCL_FAIL_CONNECT;
4439 ir_tcl_eval (p->interp, p->failback);
4441 ir_obj_delete ((ClientData) p);
4445 ir_tcl_eval (p->interp, p->callback);
4446 if (p->ref_count == 2 && p->cs_link && p->request_queue
4447 && p->state == IR_TCL_R_Idle)
4448 ir_tcl_send_q (p, p->request_queue, "x");
4449 ir_obj_delete ((ClientData) p);
4454 p->state = IR_TCL_R_Reading;
4457 yaz_log(LOG_DEBUG, "round %d", round);
4458 /* read incoming APDU */
4459 if ((r=cs_get (p->cs_link, &p->buf_in, &p->len_in)) == 1)
4461 logf(LOG_DEBUG, "PDU Fraction read");
4464 /* signal one more use of ir object - callbacks must not
4465 release the ir memory (p pointer) */
4469 logf (LOG_DEBUG, "cs_get failed, code %d", r);
4470 ir_tcl_disconnect (p);
4473 p->failInfo = IR_TCL_FAIL_READ;
4474 ir_tcl_eval (p->interp, p->failback);
4476 /* release ir object now if callback deleted it */
4477 ir_obj_delete ((ClientData) p);
4480 /* got complete APDU. Now decode */
4483 odr_setbuf (p->odr_in, p->buf_in, r, 0);
4484 logf (LOG_DEBUG, "cs_get ok, total size %d", r);
4485 if (!z_APDU (p->odr_in, &apdu, 0, 0))
4487 logf (LOG_DEBUG, "cs_get failed: %s",
4488 odr_errmsg (odr_geterror (p->odr_in)));
4489 ir_tcl_disconnect (p);
4492 p->failInfo = IR_TCL_FAIL_IN_APDU;
4493 p->apduOffset = odr_offset (p->odr_in);
4494 ir_tcl_eval (p->interp, p->failback);
4496 /* release ir object now if failback deleted it */
4497 ir_obj_delete ((ClientData) p);
4500 /* handle APDU and invoke callback */
4501 rq = p->request_queue;
4504 /* no corresponding request. Skip it. */
4505 logf(LOG_DEBUG, "no corresponding request. Skipping it");
4506 p->state = IR_TCL_R_Idle;
4509 object_name = rq->object_name;
4510 logf (LOG_DEBUG, "Object %s", object_name);
4512 if (Tcl_GetCommandInfo (p->interp, object_name, &cmd_info))
4516 case Z_APDU_initResponse:
4517 p->eventType = "init";
4518 ir_initResponse (p, apdu->u.initResponse);
4519 apdu_call = p->initResponse;
4521 case Z_APDU_searchResponse:
4522 p->eventType = "search";
4523 ir_searchResponse (p, apdu->u.searchResponse,
4524 (IrTcl_SetObj *) cmd_info.clientData);
4525 apdu_call = ((IrTcl_SetObj *)
4526 cmd_info.clientData)->searchResponse;
4528 case Z_APDU_presentResponse:
4529 p->eventType = "present";
4530 ir_presentResponse (p, apdu->u.presentResponse,
4531 (IrTcl_SetObj *) cmd_info.clientData);
4532 apdu_call = ((IrTcl_SetObj *)
4533 cmd_info.clientData)->presentResponse;
4535 case Z_APDU_scanResponse:
4536 p->eventType = "scan";
4537 ir_scanResponse (p, apdu->u.scanResponse,
4538 (IrTcl_ScanObj *) cmd_info.clientData);
4539 apdu_call = ((IrTcl_ScanObj *)
4540 cmd_info.clientData)->scanResponse;
4542 case Z_APDU_sortResponse:
4543 p->eventType = "sort";
4544 ir_sortResponse (p, apdu->u.sortResponse,
4545 (IrTcl_SetObj *) cmd_info.clientData);
4546 apdu_call = ((IrTcl_SetObj *)
4547 cmd_info.clientData)->sortResponse;
4550 logf (LOG_WARN, "Received unknown APDU type (%d)",
4552 ir_tcl_disconnect (p);
4555 p->failInfo = IR_TCL_FAIL_UNKNOWN_APDU;
4556 ir_tcl_eval (p->interp, p->failback);
4561 p->request_queue = rq->next;
4562 p->state = IR_TCL_R_Idle;
4565 ir_tcl_eval (p->interp, apdu_call);
4566 else if (rq->callback)
4567 ir_tcl_eval (p->interp, rq->callback);
4568 xfree (rq->buf_out);
4569 xfree (rq->callback);
4570 xfree (rq->object_name);
4572 odr_reset (p->odr_in);
4573 if (p->ref_count == 1)
4575 ir_obj_delete ((ClientData) p);
4578 ir_obj_delete ((ClientData) p);
4579 } while (p->cs_link && cs_more (p->cs_link));
4580 if (p->cs_link && p->request_queue && p->state == IR_TCL_R_Idle)
4581 ir_tcl_send_q (p, p->request_queue, "x");
4585 * ir_select_write: handle outgoing packages - not yet written.
4587 static int ir_select_write (ClientData clientData)
4589 IrTcl_Obj *p = (IrTcl_Obj *) clientData;
4593 logf (LOG_DEBUG, "Write handler fd=%d", cs_fileno(p->cs_link));
4594 if (p->state == IR_TCL_R_Connecting)
4596 logf(LOG_DEBUG, "write: connect");
4597 r = cs_rcvconnect (p->cs_link);
4600 logf (LOG_DEBUG, "cs_rcvconnect returned 1");
4603 p->state = IR_TCL_R_Idle;
4605 ir_select_remove_write (cs_fileno (p->cs_link), p);
4608 logf (LOG_DEBUG, "cs_rcvconnect error");
4609 ir_tcl_disconnect (p);
4612 p->failInfo = IR_TCL_FAIL_CONNECT;
4613 ir_tcl_eval (p->interp, p->failback);
4615 ir_obj_delete ((ClientData) p);
4619 ir_tcl_eval (p->interp, p->callback);
4620 ir_obj_delete ((ClientData) p);
4623 rq = p->request_queue;
4624 if (!rq || !rq->buf_out)
4627 if ((r=cs_put (p->cs_link, rq->buf_out, rq->len_out)) < 0)
4629 logf (LOG_DEBUG, "cs_put write fail");
4631 xfree (rq->buf_out);
4633 ir_tcl_disconnect (p);
4636 p->failInfo = IR_TCL_FAIL_WRITE;
4637 ir_tcl_eval (p->interp, p->failback);
4639 ir_obj_delete ((ClientData) p);
4641 else if (r == 0) /* remove select bit */
4643 logf (LOG_DEBUG, "Write completed");
4644 p->state = IR_TCL_R_Waiting;
4645 ir_select_remove_write (cs_fileno (p->cs_link), p);
4646 xfree (rq->buf_out);
4652 static void ir_select_notify (ClientData clientData, int r, int w, int e)
4656 if (!ir_select_write (clientData) && r)
4657 ir_select_read (clientData);
4661 ir_select_read (clientData);
4665 /*----------------------------------------------------------- */
4669 * This wrapper function is used by Windows to invoke the
4670 * initialization code for the DLL. If we are compiling
4671 * with Visual C++, this routine will be renamed to DllMain.
4683 DllEntryPoint(hInst, reason, reserved)
4684 HINSTANCE hInst; /* Library instance handle. */
4685 DWORD reason; /* Reason this function is being called. */
4686 LPVOID reserved; /* Not used. */
4692 /* ------------------------------------------------------- */
4694 * Irtcl_init: Registration of TCL commands.
4696 EXPORT (int,Irtcl_Init) (Tcl_Interp *interp)
4698 Tcl_CreateCommand (interp, "ir", ir_obj_mk, (ClientData) NULL,
4699 (Tcl_CmdDeleteProc *) NULL);
4700 Tcl_CreateCommand (interp, "ir-set", ir_set_obj_mk,
4701 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
4702 Tcl_CreateCommand (interp, "ir-scan", ir_scan_obj_mk,
4703 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
4704 Tcl_CreateCommand (interp, "ir-log-init", ir_log_init_proc,
4705 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
4706 Tcl_CreateCommand (interp, "ir-log", ir_log_proc,
4707 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
4708 Tcl_CreateCommand (interp, "ir-version", ir_version, (ClientData) NULL,
4709 (Tcl_CmdDeleteProc *) NULL);