2 * IR toolkit for tcl/tk
3 * (c) Index Data 1995-2003
4 * See the file LICENSE for details.
7 * Revision 1.125 2003-04-29 10:51:23 adam
8 * Null terminate octet aligned records
10 * Revision 1.124 2003/03/05 22:02:47 adam
13 * Revision 1.123 2003/03/05 21:21:41 adam
14 * APDU log. default largeSetLowerBound changed from 2 to 1
16 * Revision 1.122 2003/03/05 18:02:08 adam
17 * Fix bug with idAuthentication that didn't work for empty group.
19 * Revision 1.121 2003/01/30 13:27:07 adam
20 * Changed version to 1.4.1. Added WIN32 version resource.
21 * IrTcl ignores unexpected PDU's, rather than die.
23 * Revision 1.120 2002/03/20 14:48:54 adam
24 * implemented USR.1 SearchResult-1
26 * Revision 1.119 2001/12/03 00:31:06 adam
27 * Towards 1.4. Configure updates.
29 * Revision 1.118 2001/03/27 16:27:21 adam
30 * Fixed bug in do_responseStatus.
32 * Revision 1.117 2001/03/26 11:39:34 adam
33 * Fixed bug in ir_deleteDiags - crash when receiving multiple diags.
35 * Revision 1.116 2001/02/09 11:58:04 adam
36 * Updated for Tcl8.1 and higher where internal encoding is UTF-8.
38 * Revision 1.115 2000/09/13 12:18:49 adam
39 * Logging utility patch (YAZ version 1.7).
41 * Revision 1.114 1999/05/17 20:37:41 adam
42 * Fixed problem with ASN code.
44 * Revision 1.113 1999/04/20 10:01:46 adam
45 * Modified calls to ODR encoders/decoders (name argument).
47 * Revision 1.112 1999/03/22 06:51:34 adam
50 * Revision 1.111 1999/02/11 11:30:09 adam
53 * Revision 1.110 1998/10/20 15:15:31 adam
54 * Changed scan response handler.
56 * Revision 1.109 1998/10/13 21:23:26 adam
57 * Fixed searchStatus method.
59 * Revision 1.108 1998/10/12 11:48:08 adam
60 * Removed printf call.
62 * Revision 1.107 1998/06/10 13:00:46 adam
63 * Added ir-version command.
65 * Revision 1.106 1998/05/20 12:25:35 adam
66 * Fixed bug that occurred in rare cases when encoding of incoming
69 * Revision 1.105 1998/04/02 14:31:08 adam
70 * This version works with compiled ASN.1 code.
72 * Revision 1.104 1998/02/27 14:26:07 adam
73 * Changed client so that it still works if target sets numberOfRecords
74 * in response to an illegal value.
76 * Revision 1.103 1997/11/19 11:22:10 adam
77 * Object identifiers can be accessed in GRS-1 records.
79 * Revision 1.102 1997/09/17 12:22:40 adam
80 * Changed to use YAZ version 1.4. The new comstack utility, cs_straddr,
83 * Revision 1.101 1997/09/09 10:19:53 adam
84 * New MSV5.0 port with fewer warnings.
86 * Revision 1.100 1997/05/01 15:04:05 adam
87 * Added ir-log command.
89 * Revision 1.99 1997/04/30 07:24:47 adam
90 * Spell fix of an error message.
92 * Revision 1.98 1997/04/13 18:57:20 adam
93 * Better error reporting and aligned with Tcl/Tk style.
94 * Rework of notifier code with Tcl_File handles.
96 * Revision 1.97 1996/11/14 17:11:07 adam
97 * Added Explain documentaion.
99 * Revision 1.96 1996/10/08 13:02:50 adam
100 * When dealing with records, odr_choice_enable_bias function is used to
101 * prevent decoding of externals.
103 * Revision 1.95 1996/09/13 10:51:49 adam
104 * Bug fix: ir_tcl_select_set called Tcl_GetFile at disconnect.
106 * Revision 1.94 1996/08/21 13:32:53 adam
107 * Implemented saveFile method and extended loadFile method to work with it.
109 * Revision 1.93 1996/08/16 15:07:45 adam
110 * First work on Explain.
112 * Revision 1.92 1996/08/09 15:33:07 adam
113 * Modified the code to use tk4.1/tcl7.5 patch level 1. The time-driven
114 * polling is no longer activated on Windows since asynchrounous I/O works
117 * Revision 1.91 1996/07/03 13:31:11 adam
118 * The xmalloc/xfree functions from YAZ are used to manage memory.
120 * Revision 1.90 1996/06/27 14:21:00 adam
121 * Yet another Windows port.
123 * Revision 1.89 1996/06/11 15:27:15 adam
124 * Event type set to connect a little earlier in the do_connect function.
126 * Revision 1.88 1996/06/03 09:04:22 adam
127 * Changed a few logf calls.
129 * Revision 1.87 1996/05/29 06:37:51 adam
130 * Function ir_tcl_get_grs_r enhanced so that specific elements can be
133 * Revision 1.86 1996/03/20 13:54:04 adam
134 * The Tcl_File structure is only manipulated in the Tk-event interface
137 * Revision 1.85 1996/03/15 11:15:48 adam
138 * Modified to use new prototypes for p_query_rpn and p_query_scan.
140 * Revision 1.84 1996/03/07 12:42:49 adam
141 * Better logging when callback is invoked.
143 * Revision 1.83 1996/03/05 09:21:09 adam
144 * Bug fix: memory used by GRS records wasn't freed.
145 * Rewrote some of the error handling code - the connection is always
146 * closed before failback is called.
147 * If failback is defined the send APDU methods (init, search, ...) will
148 * return OK but invoke failback (as is the case if the write operation
150 * Bug fix: ref_count in assoc object could grow if fraction of PDU was
153 * Revision 1.82 1996/02/29 15:30:21 adam
154 * Export of IrTcl functionality to extensions.
156 * Revision 1.81 1996/02/26 18:38:32 adam
157 * Work on export of set methods.
159 * Revision 1.80 1996/02/23 17:31:39 adam
160 * More functions made available to the wais tcl extension.
162 * Revision 1.79 1996/02/23 13:41:38 adam
163 * Work on public access to simple ir class system.
165 * Revision 1.78 1996/02/21 10:16:08 adam
166 * Simplified select handling. Only one function ir_tcl_select_set has
167 * to be externally defined.
169 * Revision 1.77 1996/02/20 17:52:58 adam
170 * Uses the YAZ oid system to name record syntax object identifiers.
172 * Revision 1.76 1996/02/20 16:09:51 adam
173 * Bug fix: didn't set element set names stamp correctly on result
174 * set records when element set names were set to the empty string.
176 * Revision 1.75 1996/02/19 15:41:53 adam
177 * Better log messages.
178 * Minor improvement of connect method.
180 * Revision 1.74 1996/02/05 17:58:03 adam
181 * Ported ir-tcl to use the beta releases of tcl7.5/tk4.1.
183 * Revision 1.73 1996/01/29 11:35:19 adam
184 * Bug fix: cs_type member renamed to comstackType to avoid conflict with
185 * cs_type macro defined by YAZ.
187 * Revision 1.72 1996/01/19 17:45:34 quinn
188 * Added debugging output
190 * Revision 1.71 1996/01/19 16:22:38 adam
191 * New method: apduDump - returns information about last incoming APDU.
193 * Revision 1.70 1996/01/10 09:18:34 adam
194 * PDU specific callbacks implemented: initRespnse, searchResponse,
195 * presentResponse and scanResponse.
196 * Bug fix in the command line shell (tclmain.c) - discovered on OSF/1.
198 * Revision 1.69 1996/01/04 16:12:12 adam
199 * Setting PDUType renamed to eventType.
201 * Revision 1.68 1996/01/04 11:05:22 adam
202 * New setting: PDUType - returns type of last PDU returned from the target.
203 * Fixed a bug in configure/Makefile.
205 * Revision 1.67 1996/01/03 09:00:51 adam
206 * Updated to use new version of Yaz (names changed to avoid C++ conflict).
208 * Revision 1.66 1995/11/28 17:26:39 adam
209 * Removed Carriage return from ir-tcl.c!
210 * Removed misc. debug logs.
212 * Revision 1.65 1995/11/28 13:53:00 quinn
215 * Revision 1.64 1995/11/13 15:39:18 adam
216 * Bug fix: {small,medium}SetElementSetNames weren't set correctly.
217 * Bug fix: idAuthentication weren't set correctly.
219 * Revision 1.63 1995/11/13 09:55:39 adam
220 * Multiple records at a position in a result-set with differnt
223 * Revision 1.62 1995/10/18 17:20:33 adam
224 * Work on target setup in client.tcl.
226 * Revision 1.61 1995/10/18 16:42:42 adam
227 * New settings: smallSetElementSetNames and mediumSetElementSetNames.
229 * Revision 1.60 1995/10/18 15:43:31 adam
230 * In search: mediumSetElementSetNames and smallSetElementSetNames are
231 * set to elementSetNames.
233 * Revision 1.59 1995/10/17 12:18:58 adam
234 * Bug fix: when target connection closed, the connection was not
235 * properly reestablished.
237 * Revision 1.58 1995/10/16 17:00:55 adam
238 * New setting: elementSetNames.
239 * Various client improvements. Medium presentation format looks better.
241 * Revision 1.57 1995/09/21 13:11:51 adam
242 * Support of dynamic loading.
243 * Test script uses load command if necessary.
245 * Revision 1.56 1995/08/29 15:30:14 adam
246 * Work on GRS records.
248 * Revision 1.55 1995/08/28 09:43:25 adam
249 * Minor changes. configure only searches for yaz beta 3 and versions after
252 * Revision 1.54 1995/08/24 12:25:16 adam
253 * Modified to work with yaz 1.0b3.
255 * Revision 1.53 1995/08/04 12:49:26 adam
256 * Bug fix: reading uninitialized variable p.
258 * Revision 1.52 1995/08/04 11:32:38 adam
259 * More work on output queue. Memory related routines moved
262 * Revision 1.51 1995/08/03 13:22:54 adam
265 * Revision 1.50 1995/07/20 08:09:49 adam
266 * client.tcl: Targets removed from hotTargets list when targets
267 * are removed/modified.
268 * ir-tcl.c: More work on triggerResourceControl.
270 * Revision 1.49 1995/06/30 12:39:21 adam
271 * Bug fix: loadFile didn't set record type.
272 * The MARC routines are a little less strict in the interpretation.
273 * Script display.tcl replaces the old marc.tcl.
274 * New interactive script: shell.tcl.
276 * Revision 1.48 1995/06/27 19:03:50 adam
277 * Bug fix in do_present in ir-tcl.c: p->set_child member weren't set.
278 * nextResultSetPosition used instead of setOffset.
280 * Revision 1.47 1995/06/25 10:25:04 adam
281 * Working on triggerResourceControl. Description of compile/install
282 * procedure moved to ir-tcl.sgml.
284 * Revision 1.46 1995/06/22 13:15:06 adam
285 * Feature: SUTRS. Setting getSutrs implemented.
286 * Work on display formats.
287 * Preferred record syntax can be set by the user.
289 * Revision 1.45 1995/06/20 08:07:30 adam
290 * New setting: failInfo.
291 * Working on better cancel mechanism.
293 * Revision 1.44 1995/06/19 17:01:20 adam
296 * Revision 1.43 1995/06/19 13:06:08 adam
297 * New define: IR_TCL_VERSION.
299 * Revision 1.42 1995/06/19 08:08:52 adam
300 * client.tcl: hotTargets now contain both database and target name.
301 * ir-tcl.c: setting protocol edited. Errors in callbacks are logged
302 * by logf(LOG_WARN, ...) calls.
304 * Revision 1.41 1995/06/16 12:28:16 adam
305 * Implemented preferredRecordSyntax.
306 * Minor changes in diagnostic handling.
307 * Record list deleted when connection closes.
309 * Revision 1.40 1995/06/14 13:37:18 adam
310 * Setting recordType implemented.
311 * Setting implementationVersion implemented.
312 * Settings implementationId / implementationName edited.
314 * Revision 1.39 1995/06/08 10:26:32 adam
315 * Bug fix in ir_strdup.
317 * Revision 1.38 1995/06/01 16:36:47 adam
318 * About buttons. Minor bug fixes.
320 * Revision 1.37 1995/06/01 07:31:20 adam
321 * Rename of many typedefs -> IrTcl_...
323 * Revision 1.36 1995/05/31 13:09:59 adam
324 * Client searches/presents may be interrupted.
325 * New moving book-logo.
327 * Revision 1.35 1995/05/31 08:36:33 adam
328 * Bug fix in client.tcl: didn't save options on clientrc.tcl.
329 * New method: referenceId. More work on scan.
331 * Revision 1.34 1995/05/29 10:33:42 adam
332 * README and rename of startup script.
334 * Revision 1.33 1995/05/29 09:15:11 quinn
335 * Changed CS_SR to PROTO_SR, etc.
337 * Revision 1.32 1995/05/29 08:44:16 adam
338 * Work on delete of objects.
340 * Revision 1.31 1995/05/26 11:44:10 adam
341 * Bugs fixed. More work on MARC utilities and queries. Test
342 * client is up-to-date again.
344 * Revision 1.30 1995/05/26 08:54:11 adam
345 * New MARC utilities. Uses prefix query.
347 * Revision 1.29 1995/05/24 14:10:22 adam
348 * Work on idAuthentication, protocolVersion and options.
350 * Revision 1.28 1995/05/23 15:34:48 adam
351 * Many new settings, userInformationField, smallSetUpperBound, etc.
352 * A number of settings are inherited when ir-set is executed.
353 * This version is incompatible with the graphical test client (client.tcl).
355 * Revision 1.27 1995/05/11 15:34:47 adam
356 * Scan request changed a bit. This version works with RLG.
358 * Revision 1.26 1995/04/18 16:11:51 adam
359 * First version of graphical Scan. Some work on query-by-form.
361 * Revision 1.25 1995/04/17 09:37:17 adam
362 * Further development of scan.
364 * Revision 1.24 1995/04/11 14:16:42 adam
365 * Further work on scan. Response works. Entries aren't saved yet.
367 * Revision 1.23 1995/04/10 10:50:27 adam
368 * Result-set name defaults to suffix of ir-set name.
369 * Started working on scan. Not finished at this point.
371 * Revision 1.22 1995/03/31 10:43:03 adam
372 * More robust when getting bad MARC records.
374 * Revision 1.21 1995/03/31 08:56:37 adam
375 * New button "Search".
377 * Revision 1.20 1995/03/29 16:07:09 adam
378 * Bug fix: Didn't use setName in present request.
380 * Revision 1.19 1995/03/28 12:45:23 adam
381 * New ir method failback: called on disconnect/protocol error.
382 * New ir set/get method: protocol: SR / Z3950.
383 * Simple popup and disconnect when failback is invoked.
385 * Revision 1.18 1995/03/21 15:50:12 adam
388 * Revision 1.17 1995/03/21 13:41:03 adam
389 * Comstack cs_create not used too often. Non-blocking connect.
391 * Revision 1.16 1995/03/21 08:26:06 adam
392 * New method, setName, to specify the result set name (other than Default).
393 * New method, responseStatus, which returns diagnostic info, if any, after
394 * present response / search response.
396 * Revision 1.15 1995/03/20 15:24:07 adam
397 * Diagnostic records saved on searchResponse.
399 * Revision 1.14 1995/03/20 08:53:22 adam
400 * Event loop in tclmain.c rewritten. New method searchStatus.
402 * Revision 1.13 1995/03/17 18:26:17 adam
403 * Non-blocking i/o used now. Database names popup as cascade items.
405 * Revision 1.12 1995/03/17 15:45:00 adam
406 * Improved target/database setup.
408 * Revision 1.11 1995/03/16 17:54:03 adam
409 * Minor changes really.
411 * Revision 1.10 1995/03/15 16:14:50 adam
412 * Blocking arg in cs_create changed.
414 * Revision 1.9 1995/03/15 13:59:24 adam
417 * Revision 1.8 1995/03/15 08:25:16 adam
418 * New method presentStatus to check for error on present. Misc. cleanup
419 * of IrTcl_RecordList manipulations. Full MARC record presentation in
422 * Revision 1.7 1995/03/14 17:32:29 adam
423 * Presentation of full Marc record in popup window.
425 * Revision 1.6 1995/03/12 19:31:55 adam
426 * Pattern matching implemented when retrieving MARC records. More
427 * diagnostic functions.
429 * Revision 1.5 1995/03/10 18:00:15 adam
430 * Actual presentation in line-by-line format. RPN query support.
432 * Revision 1.4 1995/03/09 16:15:08 adam
433 * First presentRequest attempts. Hot-target list.
451 #if defined(__WIN32__)
452 # define WIN32_LEAN_AND_MEAN
453 # include <windows.h>
454 # undef WIN32_LEAN_AND_MEAN
457 * VC++ has an alternate entry point called DllMain, so we need to rename
461 # if defined(_MSC_VER)
462 # define EXPORT(a,b) __declspec(dllexport) a b
463 # define DllEntryPoint DllMain
465 # if defined(__BORLANDC__)
466 # define EXPORT(a,b) a _export b
468 # define EXPORT(a,b) a b
472 # define EXPORT(a,b) a b
475 static char *wrongArgs = "wrong # args: should be \"";
476 static FILE *odr_print_file = 0;
478 static int ir_tcl_error_exec (Tcl_Interp *interp, int argc, char **argv)
481 Tcl_AppendResult (interp, " while executing ", NULL);
482 for (i = 0; i<argc; i++)
483 Tcl_AppendResult (interp, (i ? " " : "\""), argv[i], NULL);
484 Tcl_AppendResult (interp, "\"", NULL);
489 static void ir_deleteDiags (IrTcl_Diagnostic **dst_list, int *dst_num);
491 static void ir_select_notify (ClientData clientData, int r, int w, int e);
493 void ir_select_add (int fd, void *obj)
495 ir_tcl_select_set (ir_select_notify, fd, obj, 1, 0, 0);
498 void ir_select_add_write (int fd, void *obj)
500 ir_tcl_select_set (ir_select_notify, fd, obj, 1, 1, 0);
503 void ir_select_remove (int fd, void *obj)
505 ir_tcl_select_set (NULL, fd, obj, 0, 0, 0);
508 void ir_select_remove_write (int fd, void *obj)
510 ir_tcl_select_set (ir_select_notify, fd, obj, 1, 0, 0);
513 static void delete_IR_record (IrTcl_RecordList *rl)
517 case Z_NamePlusRecord_databaseRecord:
518 switch (rl->u.dbrec.type)
521 ir_tcl_grs_del (&rl->u.dbrec.u.grs1);
526 xfree (rl->u.dbrec.buf);
527 rl->u.dbrec.buf = NULL;
529 case Z_NamePlusRecord_surrogateDiagnostic:
530 ir_deleteDiags (&rl->u.surrogateDiagnostics.list,
531 &rl->u.surrogateDiagnostics.num);
534 xfree (rl->elements);
537 static void purge_IR_records (IrTcl_SetObj *setobj)
539 IrTcl_RecordList *rl;
540 while ((rl = setobj->record_list))
542 setobj->record_list = rl->next;
543 delete_IR_record (rl);
548 static IrTcl_RecordList *new_IR_record (IrTcl_SetObj *setobj,
550 const char *elements)
552 IrTcl_RecordList *rl;
554 if (elements && !*elements)
556 for (rl = setobj->record_list; rl; rl = rl->next)
558 if (no == rl->no && (!rl->elements || !elements ||
559 !strcmp(elements, rl->elements)))
561 delete_IR_record (rl);
567 rl = ir_tcl_malloc (sizeof(*rl));
568 rl->next = setobj->record_list;
570 setobj->record_list = rl;
573 ir_tcl_strdup (NULL, &rl->elements, elements);
580 int ir_tcl_eval (Tcl_Interp *interp, const char *command)
582 char *tmp = ir_tcl_malloc (strlen(command)+1);
585 logf (LOG_DEBUG, "Invoking %.23s ...", command);
586 strcpy (tmp, command);
587 r = Tcl_Eval (interp, tmp);
590 const char *errorInfo = Tcl_GetVar (interp, "errorInfo", 0);
591 logf (LOG_WARN, "Tcl error in line %d: %s\n%s", interp->errorLine,
592 interp->result, errorInfo ? errorInfo : "<null>");
594 Tcl_FreeResult (interp);
600 * IrTcl_getRecordSyntaxStr: Return record syntax name of object id
602 static char *IrTcl_getRecordSyntaxStr (enum oid_value value)
605 struct oident ent, *entp;
607 ent.proto = PROTO_Z3950;
608 ent.oclass = CLASS_RECSYN;
611 o = oid_getoidbyent (&ent);
612 entp = oid_getentbyoid (o);
620 * IrTcl_getRecordSyntaxVal: Return record syntax value of string
622 static enum oid_value IrTcl_getRecordSyntaxVal (const char *name)
624 return oid_getvalbyname (name);
627 static IrTcl_RecordList *find_IR_record (IrTcl_SetObj *setobj, int no)
629 IrTcl_RecordList *rl;
631 for (rl = setobj->record_list; rl; rl = rl->next)
633 (!setobj->recordElements || !rl->elements ||
634 !strcmp (setobj->recordElements, rl->elements)))
639 static void delete_IR_records (IrTcl_SetObj *setobj)
641 IrTcl_RecordList *rl, *rl1;
643 for (rl = setobj->record_list; rl; rl = rl1)
645 delete_IR_record (rl);
649 setobj->record_list = NULL;
653 * ir_tcl_get_set_int: Set/get integer value
655 int ir_tcl_get_set_int (int *val, Tcl_Interp *interp, int argc, char **argv)
661 if (Tcl_GetInt (interp, argv[2], val)==TCL_ERROR)
664 sprintf (buf, "%d", *val);
665 Tcl_AppendResult (interp, buf, NULL);
671 * ir_tcl_method_error
673 int ir_tcl_method_error (Tcl_Interp *interp, int argc, char **argv,
676 IrTcl_Methods *tab_i = tab;
679 Tcl_AppendResult (interp, "bad method: \"", *argv, " ", argv[1],
680 "\"\nmethod should be of:", NULL);
681 for (tab_i = tab; tab_i->tab; tab_i++)
682 for (t = tab_i->tab; t->name; t++)
683 Tcl_AppendResult (interp, " ", t->name, NULL);
688 * ir_tcl_method: Search for method in table and invoke method handler
690 int ir_tcl_method (Tcl_Interp *interp, int argc, char **argv,
691 IrTcl_Methods *tab, int *ret)
693 IrTcl_Methods *tab_i = tab;
696 for (tab_i = tab; tab_i->tab; tab_i++)
697 for (t = tab_i->tab; t->name; t++)
700 if ((*t->method)(tab_i->obj, interp, argc, argv) == TCL_ERROR)
704 if (!strcmp (t->name, argv[1]))
706 *ret = (*t->method)(tab_i->obj, interp, argc, argv);
717 * ir_tcl_named_bits: get/set named bits
719 int ir_tcl_named_bits (struct ir_named_entry *tab, Odr_bitmask *ob,
720 Tcl_Interp *interp, int argc, char **argv)
722 struct ir_named_entry *ti;
727 for (no = 0; no < argc; no++)
730 for (ti = tab; ti->name; ti++)
731 if (!strcmp(argv[no], "@all") || !strcmp (argv[no], ti->name))
733 ODR_MASK_SET (ob, ti->pos);
738 Tcl_AppendResult (interp, "bad bit mask ", argv[no], NULL);
739 return ir_tcl_error_exec (interp, argc, argv);
744 for (ti = tab; ti->name; ti++)
745 if (ODR_MASK_GET (ob, ti->pos))
746 Tcl_AppendElement (interp, ti->name);
750 static void set_referenceId (ODR o, Z_ReferenceId **dst, const char *src)
756 *dst = odr_malloc (o, sizeof(**dst));
757 (*dst)->size = (*dst)->len = strlen(src);
758 (*dst)->buf = odr_malloc (o, (*dst)->len);
759 memcpy ((*dst)->buf, src, (*dst)->len);
763 static void get_referenceId (char **dst, Z_ReferenceId *src)
771 *dst = ir_tcl_malloc (src->len+1);
772 memcpy (*dst, src->buf, src->len);
773 (*dst)[src->len] = '\0';
776 /* ------------------------------------------------------- */
779 * do_init_request: init method on IR object
781 static int do_init_request (void *obj, Tcl_Interp *interp,
782 int argc, char **argv)
790 logf (LOG_DEBUG, "init %s", *argv);
793 Tcl_AppendResult (interp, "not connected", NULL);
794 return ir_tcl_error_exec (interp, argc, argv);
796 apdu = zget_APDU (p->odr_out, Z_APDU_initRequest);
797 req = apdu->u.initRequest;
799 set_referenceId (p->odr_out, &req->referenceId, p->set_inher.referenceId);
800 req->options = &p->options;
801 req->protocolVersion = &p->protocolVersion;
802 req->preferredMessageSize = &p->preferredMessageSize;
803 req->maximumRecordSize = &p->maximumRecordSize;
805 if (p->idAuthenticationGroupId || p->idAuthenticationUserId)
807 Z_IdPass *pass = odr_malloc (p->odr_out, sizeof(*pass));
808 Z_IdAuthentication *auth = odr_malloc (p->odr_out, sizeof(*auth));
810 logf (LOG_DEBUG, "using pass authentication");
812 auth->which = Z_IdAuthentication_idPass;
813 auth->u.idPass = pass;
814 if (p->idAuthenticationGroupId && *p->idAuthenticationGroupId)
815 pass->groupId = p->idAuthenticationGroupId;
817 pass->groupId = NULL;
818 if (p->idAuthenticationUserId && *p->idAuthenticationUserId)
819 pass->userId = p->idAuthenticationUserId;
822 if (p->idAuthenticationPassword && *p->idAuthenticationPassword)
823 pass->password = p->idAuthenticationPassword;
825 pass->password = NULL;
826 req->idAuthentication = auth;
828 else if (p->idAuthenticationOpen && *p->idAuthenticationOpen)
830 Z_IdAuthentication *auth = odr_malloc (p->odr_out, sizeof(*auth));
832 logf (LOG_DEBUG, "using open authentication");
833 auth->which = Z_IdAuthentication_open;
834 auth->u.open = p->idAuthenticationOpen;
835 req->idAuthentication = auth;
838 req->idAuthentication = NULL;
839 req->implementationId = p->implementationId;
840 req->implementationName = p->implementationName;
841 req->implementationVersion = p->implementationVersion;
842 req->userInformationField = 0;
844 return ir_tcl_send_APDU (interp, p, apdu, "init", *argv);
848 * do_protocolVersion: Set protocol Version
850 static int do_protocolVersion (void *obj, Tcl_Interp *interp,
851 int argc, char **argv)
859 ODR_MASK_ZERO (&p->protocolVersion);
860 ODR_MASK_SET (&p->protocolVersion, 0);
861 ODR_MASK_SET (&p->protocolVersion, 1);
866 if (Tcl_GetInt (interp, argv[2], &version)==TCL_ERROR)
868 ODR_MASK_ZERO (&p->protocolVersion);
869 for (i = 0; i<version; i++)
870 ODR_MASK_SET (&p->protocolVersion, i);
872 for (i = 4; --i >= 0; )
873 if (ODR_MASK_GET (&p->protocolVersion, i))
875 sprintf (buf, "%d", i+1);
876 interp->result = buf;
881 * do_options: Set options
883 static int do_options (void *obj, Tcl_Interp *interp,
884 int argc, char **argv)
886 static struct ir_named_entry options_tab[] = {
890 { "resourceReport", 3 },
891 { "triggerResourceCtrl", 4},
892 { "resourceCtrl", 5},
896 { "extendedServices", 10},
897 { "level-1Segmentation", 11},
898 { "level-2Segmentation", 12},
899 { "concurrentOperations", 13},
900 { "namedResultSets", 14},
907 ODR_MASK_ZERO (&p->options);
908 ODR_MASK_SET (&p->options, 0);
909 ODR_MASK_SET (&p->options, 1);
910 ODR_MASK_SET (&p->options, 4);
911 ODR_MASK_SET (&p->options, 7);
912 ODR_MASK_SET (&p->options, 14);
915 return ir_tcl_named_bits (options_tab, &p->options, interp, argc-2, argv+2);
919 * do_apduInfo: Get APDU information
921 static int do_apduInfo (void *obj, Tcl_Interp *interp, int argc, char **argv)
929 sprintf (buf, "%d", p->apduLen);
930 Tcl_AppendElement (interp, buf);
931 sprintf (buf, "%d", p->apduOffset);
932 Tcl_AppendElement (interp, buf);
935 Tcl_AppendElement (interp, "");
938 apduf = fopen ("apdu.tmp", "w");
941 Tcl_AppendElement (interp, "");
944 odr_dumpBER (apduf, p->buf_in, p->apduLen);
946 if (!(apduf = fopen ("apdu.tmp", "r")))
947 Tcl_AppendElement (interp, "");
952 Tcl_AppendResult (interp, " {", NULL);
953 while ((c = getc (apduf)) != EOF)
957 Tcl_AppendResult (interp, buf, NULL);
960 Tcl_AppendResult (interp, "}", NULL);
967 * do_failInfo: Get fail information
969 static int do_failInfo (void *obj, Tcl_Interp *interp, int argc, char **argv)
979 sprintf (buf, "%d", p->failInfo);
985 case IR_TCL_FAIL_CONNECT:
986 cp = "connect failed";
988 case IR_TCL_FAIL_READ:
989 cp = "connection closed";
991 case IR_TCL_FAIL_WRITE:
992 cp = "connection closed";
994 case IR_TCL_FAIL_IN_APDU:
995 cp = "failed to decode incoming APDU";
997 case IR_TCL_FAIL_UNKNOWN_APDU:
1003 Tcl_AppendElement (interp, buf);
1004 Tcl_AppendElement (interp, cp);
1009 * do_preferredMessageSize: Set/get preferred message size
1011 static int do_preferredMessageSize (void *obj, Tcl_Interp *interp,
1012 int argc, char **argv)
1018 p->preferredMessageSize = 30000;
1021 return ir_tcl_get_set_int (&p->preferredMessageSize, interp, argc, argv);
1025 * do_maximumRecordSize: Set/get maximum record size
1027 static int do_maximumRecordSize (void *obj, Tcl_Interp *interp,
1028 int argc, char **argv)
1034 p->maximumRecordSize = 30000;
1037 return ir_tcl_get_set_int (&p->maximumRecordSize, interp, argc, argv);
1041 * do_initResult: Get init result
1043 static int do_initResult (void *obj, Tcl_Interp *interp,
1044 int argc, char **argv)
1050 return ir_tcl_get_set_int (&p->initResult, interp, argc, argv);
1055 * do_implementationName: Set/get Implementation Name.
1057 static int do_implementationName (void *obj, Tcl_Interp *interp,
1058 int argc, char **argv)
1063 return ir_tcl_strdup (interp, &p->implementationName,
1065 else if (argc == -1)
1066 return ir_tcl_strdel (interp, &p->implementationName);
1069 xfree (p->implementationName);
1070 if (ir_tcl_strdup (interp, &p->implementationName, argv[2])
1074 Tcl_AppendResult (interp, p->implementationName, (char*) NULL);
1079 * do_implementationId: Get Implementation Id.
1081 static int do_implementationId (void *obj, Tcl_Interp *interp,
1082 int argc, char **argv)
1087 return ir_tcl_strdup (interp, &p->implementationId, "81");
1088 else if (argc == -1)
1089 return ir_tcl_strdel (interp, &p->implementationId);
1090 Tcl_AppendResult (interp, p->implementationId, (char*) NULL);
1095 * do_implementationVersion: get Implementation Version.
1097 static int do_implementationVersion (void *obj, Tcl_Interp *interp,
1098 int argc, char **argv)
1103 return ir_tcl_strdup (interp, &p->implementationVersion,
1104 #ifdef IR_TCL_VERSION
1109 else if (argc == -1)
1110 return ir_tcl_strdel (interp, &p->implementationVersion);
1111 Tcl_AppendResult (interp, p->implementationVersion, (char*) NULL);
1116 * do_targetImplementationName: Get Implementation Name of target.
1118 static int do_targetImplementationName (void *obj, Tcl_Interp *interp,
1119 int argc, char **argv)
1125 p->targetImplementationName = NULL;
1128 else if (argc == -1)
1129 return ir_tcl_strdel (interp, &p->targetImplementationName);
1130 Tcl_AppendResult (interp, p->targetImplementationName, (char*) NULL);
1135 * do_targetImplementationId: Get Implementation Id of target
1137 static int do_targetImplementationId (void *obj, Tcl_Interp *interp,
1138 int argc, char **argv)
1144 p->targetImplementationId = NULL;
1147 else if (argc == -1)
1148 return ir_tcl_strdel (interp, &p->targetImplementationId);
1149 Tcl_AppendResult (interp, p->targetImplementationId, (char*) NULL);
1154 * do_targetImplementationVersion: Get Implementation Version of target
1156 static int do_targetImplementationVersion (void *obj, Tcl_Interp *interp,
1157 int argc, char **argv)
1163 p->targetImplementationVersion = NULL;
1166 else if (argc == -1)
1167 return ir_tcl_strdel (interp, &p->targetImplementationVersion);
1168 Tcl_AppendResult (interp, p->targetImplementationVersion, (char*) NULL);
1173 * do_idAuthentication: Set/get id Authentication
1175 static int do_idAuthentication (void *obj, Tcl_Interp *interp,
1176 int argc, char **argv)
1180 if (argc >= 3 || argc == -1)
1182 xfree (p->idAuthenticationOpen);
1183 xfree (p->idAuthenticationGroupId);
1184 xfree (p->idAuthenticationUserId);
1185 xfree (p->idAuthenticationPassword);
1187 if (argc >= 3 || argc <= 0)
1189 p->idAuthenticationOpen = NULL;
1190 p->idAuthenticationGroupId = NULL;
1191 p->idAuthenticationUserId = NULL;
1192 p->idAuthenticationPassword = NULL;
1200 xfree (p->idAuthenticationGroupId);
1201 xfree (p->idAuthenticationUserId);
1202 xfree (p->idAuthenticationPassword);
1203 p->idAuthenticationGroupId = NULL;
1204 p->idAuthenticationUserId = NULL;
1205 p->idAuthenticationPassword = NULL;
1207 ir_tcl_strdup (interp, &p->idAuthenticationOpen, argv[2])
1213 xfree (p->idAuthenticationOpen);
1214 p->idAuthenticationOpen = NULL;
1216 ir_tcl_strdup (interp, &p->idAuthenticationGroupId, argv[2])
1220 ir_tcl_strdup (interp, &p->idAuthenticationUserId, argv[3])
1224 ir_tcl_strdup (interp, &p->idAuthenticationPassword, argv[4])
1229 if (p->idAuthenticationOpen)
1230 Tcl_AppendElement (interp, p->idAuthenticationOpen);
1231 else if (p->idAuthenticationGroupId || p->idAuthenticationUserId)
1233 Tcl_AppendElement (interp, p->idAuthenticationGroupId);
1234 Tcl_AppendElement (interp, p->idAuthenticationUserId);
1235 Tcl_AppendElement (interp, p->idAuthenticationPassword);
1241 * do_connect: connect method on IR object
1243 static int do_connect (void *obj, Tcl_Interp *interp,
1244 int argc, char **argv)
1254 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
1255 " ?hostname?\"", NULL);
1260 Tcl_AppendResult (interp, p->hostname, NULL);
1264 logf (LOG_DEBUG, "connect %s %s", *argv, argv[2]);
1267 Tcl_AppendResult (interp, "already connected", NULL);
1268 return ir_tcl_error_exec (interp, argc, argv);
1270 if (!strcmp (p->comstackType, "tcpip"))
1272 p->cs_link = cs_create (tcpip_type, CS_BLOCK, p->protocol_type);
1273 logf (LOG_DEBUG, "tcp/ip connect %s", argv[2]);
1275 else if (!strcmp (p->comstackType, "mosi"))
1278 p->cs_link = cs_create (mosi_type, CS_BLOCK, p->protocol_type);
1279 logf (LOG_DEBUG, "mosi connect %s", argv[2]);
1281 Tcl_AppendResult (interp, "mosi not supported", NULL);
1282 return ir_tcl_error_exec (interp, argc, argv);
1287 Tcl_AppendResult (interp, "bad comstack type ",
1288 p->comstackType, NULL);
1289 return ir_tcl_error_exec (interp, argc, argv);
1291 if (ir_tcl_strdup (interp, &p->hostname, argv[2]) == TCL_ERROR)
1293 p->eventType = "connect";
1294 addr = cs_straddr (p->cs_link, argv[2]);
1297 ir_tcl_disconnect (p);
1298 Tcl_AppendResult (interp, "cs_straddr fail", NULL);
1299 return ir_tcl_error_exec (interp, argc, argv);
1301 if ((r=cs_connect (p->cs_link, addr)) < 0)
1303 ir_tcl_disconnect (p);
1304 Tcl_AppendResult (interp, "connect fail", NULL);
1305 return ir_tcl_error_exec (interp, argc, argv);
1307 ir_select_add (cs_fileno (p->cs_link), p);
1310 logf (LOG_DEBUG, "connect pending fd=%d", cs_fileno(p->cs_link));
1311 ir_select_add_write (cs_fileno (p->cs_link), p);
1312 p->state = IR_TCL_R_Connecting;
1316 logf (LOG_DEBUG, "connect ok fd=%d", cs_fileno(p->cs_link));
1317 p->state = IR_TCL_R_Idle;
1319 ir_tcl_eval (p->interp, p->callback);
1326 * ir_tcl_disconnect: close connection
1328 void ir_tcl_disconnect (IrTcl_Obj *p)
1332 logf(LOG_DEBUG, "Closing connection to %s", p->hostname);
1333 xfree (p->hostname);
1335 assert (p->cs_link);
1336 ir_select_remove (cs_fileno (p->cs_link), p);
1338 odr_reset (p->odr_in);
1340 #if TCL_MAJOR_VERSION == 8
1341 cs_fileno(p->cs_link) = -1;
1343 cs_close (p->cs_link);
1346 ODR_MASK_ZERO (&p->options);
1347 ODR_MASK_SET (&p->options, 0);
1348 ODR_MASK_SET (&p->options, 1);
1349 ODR_MASK_SET (&p->options, 4);
1350 ODR_MASK_SET (&p->options, 7);
1351 ODR_MASK_SET (&p->options, 14);
1353 ODR_MASK_ZERO (&p->protocolVersion);
1354 ODR_MASK_SET (&p->protocolVersion, 0);
1355 ODR_MASK_SET (&p->protocolVersion, 1);
1358 assert (!p->cs_link);
1362 * do_disconnect: disconnect method on IR object
1364 static int do_disconnect (void *obj, Tcl_Interp *interp,
1365 int argc, char **argv)
1371 p->state = IR_TCL_R_Idle;
1372 p->eventType = NULL;
1377 ir_tcl_disconnect (p);
1382 * do_comstack: Set/get comstack method on IR object
1384 static int do_comstack (void *o, Tcl_Interp *interp,
1385 int argc, char **argv)
1390 return ir_tcl_strdup (interp, &obj->comstackType, "tcpip");
1391 else if (argc == -1)
1392 return ir_tcl_strdel (interp, &obj->comstackType);
1395 xfree (obj->comstackType);
1396 if (ir_tcl_strdup (interp, &obj->comstackType, argv[2]) == TCL_ERROR)
1399 Tcl_AppendElement (interp, obj->comstackType);
1404 * do_logLevel: Set log level
1406 static int do_logLevel (void *o, Tcl_Interp *interp,
1407 int argc, char **argv)
1412 yaz_log_init (yaz_log_mask_str (argv[2]), "", NULL);
1414 yaz_log_init (yaz_log_mask_str (argv[2]), argv[3], NULL);
1416 yaz_log_init (yaz_log_mask_str (argv[2]), argv[3], argv[4]);
1422 * do_eventType: Return type of last event
1424 static int do_eventType (void *obj, Tcl_Interp *interp,
1425 int argc, char **argv)
1431 p->eventType = NULL;
1434 Tcl_AppendElement (interp, p->eventType ? p->eventType : "");
1440 * do_callback: add callback
1442 static int do_callback (void *obj, Tcl_Interp *interp,
1443 int argc, char **argv)
1452 else if (argc == -1)
1453 return ir_tcl_strdel (interp, &p->callback);
1456 xfree (p->callback);
1459 if (ir_tcl_strdup (interp, &p->callback, argv[2]) == TCL_ERROR)
1469 * do_failback: add error handle callback
1471 static int do_failback (void *obj, Tcl_Interp *interp,
1472 int argc, char **argv)
1481 else if (argc == -1)
1482 return ir_tcl_strdel (interp, &p->failback);
1485 xfree (p->failback);
1488 if (ir_tcl_strdup (interp, &p->failback, argv[2]) == TCL_ERROR)
1498 * do_initResponse: add init response handler
1500 static int do_initResponse (void *obj, Tcl_Interp *interp,
1501 int argc, char **argv)
1507 p->initResponse = NULL;
1510 else if (argc == -1)
1511 return ir_tcl_strdel (interp, &p->initResponse);
1514 xfree (p->initResponse);
1517 if (ir_tcl_strdup (interp, &p->initResponse, argv[2]) == TCL_ERROR)
1521 p->initResponse = NULL;
1526 * do_protocol: Set/get protocol method on IR object
1528 static int do_protocol (void *o, Tcl_Interp *interp, int argc, char **argv)
1534 p->protocol_type = PROTO_Z3950;
1539 if (!strcmp (argv[2], "Z39"))
1540 p->protocol_type = PROTO_Z3950;
1541 else if (!strcmp (argv[2], "SR"))
1542 p->protocol_type = PROTO_SR;
1545 Tcl_AppendResult (interp, "bad protocol ", argv[2], NULL);
1546 return ir_tcl_error_exec (interp, argc, argv);
1550 switch (p->protocol_type)
1553 Tcl_AppendElement (interp, "Z39");
1556 Tcl_AppendElement (interp, "SR");
1563 * do_triggerResourceControl:
1565 static int do_triggerResourceControl (void *obj, Tcl_Interp *interp,
1566 int argc, char **argv)
1570 Z_TriggerResourceControlRequest *req;
1571 bool_t is_false = 0;
1577 Tcl_AppendResult (interp, "not connected", NULL);
1578 return ir_tcl_error_exec (interp, argc, argv);
1580 apdu = zget_APDU (p->odr_out, Z_APDU_triggerResourceControlRequest);
1581 req = apdu->u.triggerResourceControlRequest;
1582 *req->requestedAction = Z_TriggerResourceCtrl_cancel;
1583 req->resultSetWanted = &is_false;
1585 return ir_tcl_send_APDU (interp, p, apdu, "triggerResourceControl",
1590 * do_databaseNames: specify database names
1592 static int do_databaseNames (void *obj, Tcl_Interp *interp,
1593 int argc, char **argv)
1596 IrTcl_SetCObj *p = obj;
1600 for (i=0; i<p->num_databaseNames; i++)
1601 xfree (p->databaseNames[i]);
1602 xfree (p->databaseNames);
1606 p->num_databaseNames = 0;
1607 p->databaseNames = NULL;
1612 for (i=0; i<p->num_databaseNames; i++)
1613 Tcl_AppendElement (interp, p->databaseNames[i]);
1616 if (p->databaseNames)
1618 for (i=0; i<p->num_databaseNames; i++)
1619 xfree (p->databaseNames[i]);
1620 xfree (p->databaseNames);
1622 p->num_databaseNames = argc - 2;
1624 ir_tcl_malloc (sizeof(*p->databaseNames) * (1+p->num_databaseNames));
1625 for (i=0; i<p->num_databaseNames; i++)
1627 if (ir_tcl_strdup (interp, &p->databaseNames[i], argv[2+i])
1631 p->databaseNames[i] = NULL;
1636 * do_replaceIndicator: Set/get replace Set indicator
1638 static int do_replaceIndicator (void *obj, Tcl_Interp *interp,
1639 int argc, char **argv)
1641 IrTcl_SetCObj *p = obj;
1645 p->replaceIndicator = 1;
1648 return ir_tcl_get_set_int (&p->replaceIndicator, interp, argc, argv);
1652 * do_queryType: Set/Get query method
1654 static int do_queryType (void *obj, Tcl_Interp *interp,
1655 int argc, char **argv)
1657 IrTcl_SetCObj *p = obj;
1660 return ir_tcl_strdup (interp, &p->queryType, "rpn");
1661 else if (argc == -1)
1662 return ir_tcl_strdel (interp, &p->queryType);
1665 xfree (p->queryType);
1666 if (ir_tcl_strdup (interp, &p->queryType, argv[2]) == TCL_ERROR)
1669 Tcl_AppendResult (interp, p->queryType, NULL);
1674 * do_userInformationField: Get User information field
1676 static int do_userInformationField (void *obj, Tcl_Interp *interp,
1677 int argc, char **argv)
1683 p->userInformationField = NULL;
1686 else if (argc == -1)
1687 return ir_tcl_strdel (interp, &p->userInformationField);
1688 Tcl_AppendResult (interp, p->userInformationField, NULL);
1693 * do_smallSetUpperBound: Set/get small set upper bound
1695 static int do_smallSetUpperBound (void *o, Tcl_Interp *interp,
1696 int argc, char **argv)
1698 IrTcl_SetCObj *p = o;
1702 p->smallSetUpperBound = 0;
1705 return ir_tcl_get_set_int (&p->smallSetUpperBound, interp, argc, argv);
1709 * do_largeSetLowerBound: Set/get large set lower bound
1711 static int do_largeSetLowerBound (void *o, Tcl_Interp *interp,
1712 int argc, char **argv)
1714 IrTcl_SetCObj *p = o;
1718 p->largeSetLowerBound = 1;
1721 return ir_tcl_get_set_int (&p->largeSetLowerBound, interp, argc, argv);
1725 * do_mediumSetPresentNumber: Set/get large set lower bound
1727 static int do_mediumSetPresentNumber (void *o, Tcl_Interp *interp,
1728 int argc, char **argv)
1730 IrTcl_SetCObj *p = o;
1734 p->mediumSetPresentNumber = 0;
1737 return ir_tcl_get_set_int (&p->mediumSetPresentNumber, interp, argc, argv);
1741 * do_referenceId: Set/Get referenceId
1743 static int do_referenceId (void *obj, Tcl_Interp *interp,
1744 int argc, char **argv)
1746 IrTcl_SetCObj *p = obj;
1750 p->referenceId = NULL;
1753 else if (argc == -1)
1754 return ir_tcl_strdel (interp, &p->referenceId);
1757 xfree (p->referenceId);
1758 if (ir_tcl_strdup (interp, &p->referenceId, argv[2]) == TCL_ERROR)
1761 Tcl_AppendResult (interp, p->referenceId, NULL);
1766 * do_preferredRecordSyntax: Set/get preferred record syntax
1768 static int do_preferredRecordSyntax (void *obj, Tcl_Interp *interp,
1769 int argc, char **argv)
1771 IrTcl_SetCObj *p = obj;
1775 p->preferredRecordSyntax = NULL;
1778 else if (argc == -1)
1780 xfree (p->preferredRecordSyntax);
1781 p->preferredRecordSyntax = NULL;
1786 xfree (p->preferredRecordSyntax);
1787 p->preferredRecordSyntax = NULL;
1788 if (argv[2][0] && (p->preferredRecordSyntax =
1789 ir_tcl_malloc (sizeof(*p->preferredRecordSyntax))))
1790 *p->preferredRecordSyntax = IrTcl_getRecordSyntaxVal (argv[2]);
1795 (interp,!p->preferredRecordSyntax ? "" :
1796 IrTcl_getRecordSyntaxStr(*p->preferredRecordSyntax));
1803 * do_elementSetNames: Set/Get element Set Names
1805 static int do_elementSetNames (void *obj, Tcl_Interp *interp,
1806 int argc, char **argv)
1808 IrTcl_SetCObj *p = obj;
1812 p->elementSetNames = NULL;
1815 else if (argc == -1)
1816 return ir_tcl_strdel (interp, &p->elementSetNames);
1819 xfree (p->elementSetNames);
1820 if (ir_tcl_strdup (interp, &p->elementSetNames, argv[2]) == TCL_ERROR)
1823 Tcl_AppendResult (interp, p->elementSetNames, NULL);
1828 * do_smallSetElementSetNames: Set/Get small Set Element Set Names
1830 static int do_smallSetElementSetNames (void *obj, Tcl_Interp *interp,
1831 int argc, char **argv)
1833 IrTcl_SetCObj *p = obj;
1837 p->smallSetElementSetNames = NULL;
1840 else if (argc == -1)
1841 return ir_tcl_strdel (interp, &p->smallSetElementSetNames);
1844 xfree (p->smallSetElementSetNames);
1845 if (ir_tcl_strdup (interp, &p->smallSetElementSetNames,
1846 argv[2]) == TCL_ERROR)
1849 Tcl_AppendResult (interp, p->smallSetElementSetNames, NULL);
1854 * do_mediumSetElementSetNames: Set/Get medium Set Element Set Names
1856 static int do_mediumSetElementSetNames (void *obj, Tcl_Interp *interp,
1857 int argc, char **argv)
1859 IrTcl_SetCObj *p = obj;
1863 p->mediumSetElementSetNames = NULL;
1866 else if (argc == -1)
1867 return ir_tcl_strdel (interp, &p->mediumSetElementSetNames);
1870 xfree (p->mediumSetElementSetNames);
1871 if (ir_tcl_strdup (interp, &p->mediumSetElementSetNames,
1872 argv[2]) == TCL_ERROR)
1875 Tcl_AppendResult (interp, p->mediumSetElementSetNames, NULL);
1879 static IrTcl_Method ir_method_tab[] = {
1880 { "comstack", do_comstack, NULL },
1881 { "protocol", do_protocol, NULL },
1882 { "failback", do_failback, NULL },
1883 { "failInfo", do_failInfo, NULL },
1884 { "apduInfo", do_apduInfo, NULL },
1885 { "logLevel", do_logLevel, NULL },
1887 { "eventType", do_eventType, NULL },
1888 { "connect", do_connect, NULL },
1889 { "protocolVersion", do_protocolVersion, NULL },
1890 { "preferredMessageSize", do_preferredMessageSize, NULL },
1891 { "maximumRecordSize", do_maximumRecordSize, NULL },
1892 { "implementationName", do_implementationName, NULL },
1893 { "implementationId", do_implementationId, NULL },
1894 { "implementationVersion", do_implementationVersion, NULL },
1895 { "targetImplementationName", do_targetImplementationName, NULL },
1896 { "targetImplementationId", do_targetImplementationId, NULL },
1897 { "targetImplementationVersion", do_targetImplementationVersion, NULL},
1898 { "userInformationField", do_userInformationField, NULL},
1899 { "idAuthentication", do_idAuthentication, NULL},
1900 { "options", do_options, NULL},
1901 { "init", do_init_request, NULL},
1902 { "initResult", do_initResult, NULL},
1903 { "disconnect", do_disconnect, NULL},
1904 { "callback", do_callback, NULL},
1905 { "initResponse", do_initResponse, NULL},
1906 { "triggerResourceControl", do_triggerResourceControl, NULL},
1907 { "initResponse", do_initResponse, NULL},
1911 static IrTcl_Method ir_set_c_method_tab[] = {
1912 { "databaseNames", do_databaseNames, NULL},
1913 { "replaceIndicator", do_replaceIndicator, NULL},
1914 { "queryType", do_queryType, NULL},
1915 { "preferredRecordSyntax", do_preferredRecordSyntax, NULL},
1916 { "smallSetUpperBound", do_smallSetUpperBound, NULL},
1917 { "largeSetLowerBound", do_largeSetLowerBound, NULL},
1918 { "mediumSetPresentNumber", do_mediumSetPresentNumber, NULL},
1919 { "referenceId", do_referenceId, NULL},
1920 { "elementSetNames", do_elementSetNames, NULL},
1921 { "smallSetElementSetNames", do_smallSetElementSetNames, NULL},
1922 { "mediumSetElementSetNames", do_mediumSetElementSetNames, NULL},
1927 * ir_obj_method: IR Object methods
1929 static int ir_obj_method (ClientData clientData, Tcl_Interp *interp,
1930 int argc, char **argv)
1932 IrTcl_Methods tab[3];
1933 IrTcl_Obj *p = (IrTcl_Obj *) clientData;
1938 Tcl_AppendResult (interp, wrongArgs, *argv, "method args...\"", NULL);
1942 tab[0].tab = ir_method_tab;
1944 tab[1].tab = ir_set_c_method_tab;
1945 tab[1].obj = &p->set_inher;
1948 if (ir_tcl_method (interp, argc, argv, tab, &r) == TCL_ERROR)
1949 return ir_tcl_method_error (interp, argc, argv, tab);
1954 * ir_obj_delete: IR Object disposal
1956 static void ir_obj_delete (ClientData clientData)
1958 IrTcl_Obj *obj = (IrTcl_Obj *) clientData;
1959 IrTcl_Methods tab[3];
1962 if (obj->ref_count > 0)
1964 assert (obj->ref_count == 0);
1966 logf (LOG_DEBUG, "ir object delete");
1967 tab[0].tab = ir_method_tab;
1969 tab[1].tab = ir_set_c_method_tab;
1970 tab[1].obj = &obj->set_inher;
1973 ir_tcl_method (NULL, -1, NULL, tab, NULL);
1976 odr_destroy (obj->odr_in);
1977 odr_destroy (obj->odr_out);
1980 obj->odr_pr->print = 0;
1981 odr_destroy (obj->odr_pr);
1987 * ir_obj_init: IR Object initialization
1989 int ir_obj_init (ClientData clientData, Tcl_Interp *interp,
1990 int argc, char **argv, ClientData *subData,
1991 ClientData parentData)
1993 IrTcl_Methods tab[3];
2001 Tcl_AppendResult (interp, wrongArgs, *argv, " objName\"", NULL);
2004 obj = ir_tcl_malloc (sizeof(*obj));
2007 obj->bibset = ccl_qual_mk ();
2008 if ((inf = fopen ("default.bib", "r")))
2010 ccl_qual_file (obj->bibset, inf);
2015 logf (LOG_DEBUG, "ir object create %s", argv[1]);
2016 obj->odr_in = odr_createmem (ODR_DECODE);
2017 odr_choice_enable_bias (obj->odr_in, 0);
2018 obj->odr_out = odr_createmem (ODR_ENCODE);
2022 obj->odr_pr = odr_createmem (ODR_PRINT);
2023 odr_setprint(obj->odr_pr, odr_print_file);
2025 obj->state = IR_TCL_R_Idle;
2026 obj->interp = interp;
2030 obj->request_queue = NULL;
2032 tab[0].tab = ir_method_tab;
2034 tab[1].tab = ir_set_c_method_tab;
2035 tab[1].obj = &obj->set_inher;
2038 if (ir_tcl_method (interp, 0, NULL, tab, NULL) == TCL_ERROR)
2040 Tcl_AppendResult (interp, "Failed to initialize ", argv[1], NULL);
2043 *subData = (ClientData) obj;
2049 * ir_obj_mk: IR Object creation
2051 static int ir_obj_mk (ClientData clientData, Tcl_Interp *interp,
2052 int argc, char **argv)
2055 int r = ir_obj_init (clientData, interp, argc, argv, &subData, 0);
2059 Tcl_CreateCommand (interp, argv[1], ir_obj_method,
2060 subData, ir_obj_delete);
2064 IrTcl_Class ir_obj_class = {
2072 /* ------------------------------------------------------- */
2074 * do_search: Do search request
2076 static int do_search (void *o, Tcl_Interp *interp, int argc, char **argv)
2078 Z_SearchRequest *req;
2082 IrTcl_SetObj *obj = o;
2085 #if TCL_MAJOR_VERSION > 8 || (TCL_MAJOR_VERSION == 8 && TCL_MINOR_VERSION > 0)
2097 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1], "query\"",
2101 #if TCL_MAJOR_VERSION > 8 || (TCL_MAJOR_VERSION == 8 && TCL_MINOR_VERSION > 0)
2102 query_str = Tcl_UtfToExternalDString(0, argv[2], -1, &ds);
2104 query_str = argv[2];
2106 logf (LOG_DEBUG, "search %s %s", *argv, query_str);
2107 if (!obj->set_inher.num_databaseNames)
2109 Tcl_AppendResult (interp, "no databaseNames", NULL);
2110 code = ir_tcl_error_exec (interp, argc, argv);
2115 Tcl_AppendResult (interp, "not connected", NULL);
2116 code = ir_tcl_error_exec (interp, argc, argv);
2119 apdu = zget_APDU (p->odr_out, Z_APDU_searchRequest);
2120 req = apdu->u.searchRequest;
2124 set_referenceId (p->odr_out, &req->referenceId,
2125 obj->set_inher.referenceId);
2127 req->smallSetUpperBound = &obj->set_inher.smallSetUpperBound;
2128 req->largeSetLowerBound = &obj->set_inher.largeSetLowerBound;
2129 req->mediumSetPresentNumber = &obj->set_inher.mediumSetPresentNumber;
2130 req->replaceIndicator = &obj->set_inher.replaceIndicator;
2131 req->resultSetName = obj->setName ? obj->setName : "default";
2132 logf (LOG_DEBUG, "Search, resultSetName %s", req->resultSetName);
2133 req->num_databaseNames = obj->set_inher.num_databaseNames;
2134 req->databaseNames = obj->set_inher.databaseNames;
2135 for (r=0; r < obj->set_inher.num_databaseNames; r++)
2136 logf (LOG_DEBUG, " Database %s", obj->set_inher.databaseNames[r]);
2137 if (obj->set_inher.preferredRecordSyntax)
2139 struct oident ident;
2141 ident.proto = p->protocol_type;
2142 ident.oclass = CLASS_RECSYN;
2143 ident.value = *obj->set_inher.preferredRecordSyntax;
2144 logf (LOG_DEBUG, "Preferred record syntax is %d", ident.value);
2145 req->preferredRecordSyntax = odr_oiddup (p->odr_out,
2146 oid_getoidbyent (&ident));
2149 req->preferredRecordSyntax = 0;
2151 if (obj->set_inher.smallSetElementSetNames &&
2152 *obj->set_inher.smallSetElementSetNames)
2154 Z_ElementSetNames *esn = odr_malloc (p->odr_out, sizeof(*esn));
2156 esn->which = Z_ElementSetNames_generic;
2157 esn->u.generic = obj->set_inher.smallSetElementSetNames;
2158 req->smallSetElementSetNames = esn;
2161 req->smallSetElementSetNames = NULL;
2163 if (obj->set_inher.mediumSetElementSetNames &&
2164 *obj->set_inher.mediumSetElementSetNames)
2166 Z_ElementSetNames *esn = odr_malloc (p->odr_out, sizeof(*esn));
2168 esn->which = Z_ElementSetNames_generic;
2169 esn->u.generic = obj->set_inher.mediumSetElementSetNames;
2170 req->mediumSetElementSetNames = esn;
2173 req->mediumSetElementSetNames = NULL;
2175 req->query = &query;
2177 logf (LOG_DEBUG, "queryType %s", obj->set_inher.queryType);
2178 if (!strcmp (obj->set_inher.queryType, "rpn"))
2180 Z_RPNQuery *RPNquery;
2182 RPNquery = p_query_rpn (p->odr_out, p->protocol_type, query_str);
2185 Tcl_AppendResult (interp, "query syntax error", NULL);
2186 code = ir_tcl_error_exec (interp, argc, argv);
2189 query.which = Z_Query_type_1;
2190 query.u.type_1 = RPNquery;
2193 else if (!strcmp (obj->set_inher.queryType, "cclrpn"))
2197 struct ccl_rpn_node *rpn;
2198 Z_RPNQuery *RPNquery;
2201 bib1.proto = p->protocol_type;
2202 bib1.oclass = CLASS_ATTSET;
2203 bib1.value = VAL_BIB1;
2205 rpn = ccl_find_str(p->bibset, query_str, &error, &pos);
2208 Tcl_AppendResult (interp, "ccl syntax error ", ccl_err_msg(error),
2210 code = ir_tcl_error_exec (interp, argc, argv);
2214 ccl_pr_tree (rpn, stderr);
2215 fprintf (stderr, "\n");
2217 RPNquery = ccl_rpn_query(p->odr_out, rpn);
2218 RPNquery->attributeSetId = oid_getoidbyent (&bib1);
2219 query.which = Z_Query_type_1;
2220 query.u.type_1 = RPNquery;
2223 else if (!strcmp (obj->set_inher.queryType, "ccl"))
2225 query.which = Z_Query_type_2;
2226 query.u.type_2 = &ccl_query;
2227 ccl_query.buf = (unsigned char *) query_str;
2228 ccl_query.len = strlen (query_str);
2232 Tcl_AppendResult (interp, "invalid query method ",
2233 obj->set_inher.queryType, NULL);
2234 code = ir_tcl_error_exec (interp, argc, argv);
2237 code = ir_tcl_send_APDU (interp, p, apdu, "search", *argv);
2239 #if TCL_MAJOR_VERSION > 8 || (TCL_MAJOR_VERSION == 8 && TCL_MINOR_VERSION > 0)
2240 Tcl_DStringFree (&ds);
2246 * do_searchResponse: add search response handler
2248 static int do_searchResponse (void *o, Tcl_Interp *interp,
2249 int argc, char **argv)
2251 IrTcl_SetObj *obj = o;
2255 obj->searchResponse = NULL;
2258 else if (argc == -1)
2259 return ir_tcl_strdel (interp, &obj->searchResponse);
2262 xfree (obj->searchResponse);
2265 if (ir_tcl_strdup (interp, &obj->searchResponse, argv[2])
2270 obj->searchResponse = NULL;
2276 * do_presentResponse: add present response handler
2278 static int do_presentResponse (void *o, Tcl_Interp *interp,
2279 int argc, char **argv)
2281 IrTcl_SetObj *obj = o;
2285 obj->presentResponse = NULL;
2288 else if (argc == -1)
2289 return ir_tcl_strdel (interp, &obj->presentResponse);
2292 xfree (obj->presentResponse);
2295 if (ir_tcl_strdup (interp, &obj->presentResponse, argv[2])
2300 obj->presentResponse = NULL;
2306 * do_sortResponse: add sort response handler
2308 static int do_sortResponse (void *o, Tcl_Interp *interp,
2309 int argc, char **argv)
2311 IrTcl_SetObj *obj = o;
2315 obj->sortResponse = NULL;
2318 else if (argc == -1)
2319 return ir_tcl_strdel (interp, &obj->sortResponse);
2322 xfree (obj->sortResponse);
2325 if (ir_tcl_strdup (interp, &obj->sortResponse, argv[2])
2330 obj->sortResponse = NULL;
2336 * do_resultCount: Get number of hits
2338 static int do_resultCount (void *o, Tcl_Interp *interp,
2339 int argc, char **argv)
2341 IrTcl_SetObj *obj = o;
2345 obj->resultCount = 0;
2348 return ir_tcl_get_set_int (&obj->resultCount, interp, argc, argv);
2352 * do_searchStatus: Get search status (after search response)
2354 static int do_searchStatus (void *o, Tcl_Interp *interp,
2355 int argc, char **argv)
2357 IrTcl_SetObj *obj = o;
2361 return ir_tcl_get_set_int (&obj->searchStatus, interp, argc, argv);
2364 static void reset_searchResult (IrTcl_SetObj *setobj)
2367 for (i = 0; i<setobj->searchResult_num; i++)
2368 xfree (setobj->searchResult_terms[i]);
2369 xfree (setobj->searchResult_terms);
2370 xfree (setobj->searchResult_count);
2371 setobj->searchResult_terms = 0;
2372 setobj->searchResult_num = 0;
2377 * do_searchResult Get USR:Search-Result1 (after search response)
2379 static int do_searchResult (void *o, Tcl_Interp *interp,
2380 int argc, char **argv)
2382 IrTcl_SetObj *obj = o;
2387 obj->searchResult_num = 0;
2388 obj->searchResult_terms = 0;
2389 obj->searchResult_count = 0;
2392 else if (argc == -1)
2394 reset_searchResult (obj);
2397 for (i = 0; i<obj->searchResult_num; i++)
2400 sprintf (str, "%d", obj->searchResult_count[i]);
2401 Tcl_AppendResult (interp, "{", NULL);
2402 if (obj->searchResult_terms[i])
2403 Tcl_AppendElement (interp, obj->searchResult_terms[i]);
2405 Tcl_AppendElement (interp, "");
2406 Tcl_AppendElement (interp, str);
2407 Tcl_AppendResult (interp, "} ", NULL);
2413 * do_presentStatus: Get search status (after search/present response)
2415 static int do_presentStatus (void *o, Tcl_Interp *interp,
2416 int argc, char **argv)
2418 IrTcl_SetObj *obj = o;
2422 return ir_tcl_get_set_int (&obj->presentStatus, interp, argc, argv);
2426 * do_sortStatus: Get sort status (after sort response)
2428 static int do_sortStatus (void *o, Tcl_Interp *interp,
2429 int argc, char **argv)
2431 IrTcl_SetObj *obj = o;
2436 obj->sortStatus = Z_SortStatus_failure;
2439 switch (obj->sortStatus)
2441 case Z_SortStatus_success:
2442 res = "success"; break;
2443 case Z_SortStatus_partial_1:
2444 res = "partial"; break;
2445 case Z_SortStatus_failure:
2446 res = "failure"; break;
2448 res = "unknown"; break;
2450 Tcl_AppendElement (interp, res);
2455 * do_nextResultSetPosition: Get next result set position
2456 * (after search/present response)
2458 static int do_nextResultSetPosition (void *o, Tcl_Interp *interp,
2459 int argc, char **argv)
2461 IrTcl_SetObj *obj = o;
2465 obj->nextResultSetPosition = 0;
2468 return ir_tcl_get_set_int (&obj->nextResultSetPosition, interp,
2473 * do_setName: Set result Set name
2475 static int do_setName (void *o, Tcl_Interp *interp,
2476 int argc, char **argv)
2478 IrTcl_SetObj *obj = o;
2481 return ir_tcl_strdup (interp, &obj->setName, "Default");
2482 else if (argc == -1)
2483 return ir_tcl_strdel (interp, &obj->setName);
2486 xfree (obj->setName);
2487 if (ir_tcl_strdup (interp, &obj->setName, argv[2])
2491 Tcl_AppendElement (interp, obj->setName);
2496 * do_numberOfRecordsReturned: Get number of records returned
2498 static int do_numberOfRecordsReturned (void *o, Tcl_Interp *interp,
2499 int argc, char **argv)
2501 IrTcl_SetObj *obj = o;
2505 obj->numberOfRecordsReturned = 0;
2508 return ir_tcl_get_set_int (&obj->numberOfRecordsReturned, interp,
2513 * do_type: Return type (if any) at position.
2515 static int do_type (void *o, Tcl_Interp *interp, int argc, char **argv)
2517 IrTcl_SetObj *obj = o;
2519 IrTcl_RecordList *rl;
2523 obj->record_list = NULL;
2526 else if (argc == -1)
2528 delete_IR_records (obj);
2533 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2534 " position\"", NULL);
2537 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2539 rl = find_IR_record (obj, offset);
2542 logf (LOG_DEBUG, "%s %s %s: no record", argv[0], argv[1], argv[2]);
2547 case Z_NamePlusRecord_databaseRecord:
2548 interp->result = "DB";
2550 case Z_NamePlusRecord_surrogateDiagnostic:
2551 interp->result = "SD";
2559 * do_recordType: Return record type (if any) at position.
2561 static int do_recordType (void *o, Tcl_Interp *interp, int argc, char **argv)
2563 IrTcl_SetObj *obj = o;
2565 IrTcl_RecordList *rl;
2571 else if (argc == -1)
2577 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2578 " position\"", NULL);
2581 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2583 rl = find_IR_record (obj, offset);
2586 logf (LOG_DEBUG, "%s %s %s: no record", argv[0], argv[1], argv[2]);
2589 if (rl->which != Z_NamePlusRecord_databaseRecord)
2591 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2594 Tcl_AppendElement (interp, (char*)
2595 IrTcl_getRecordSyntaxStr (rl->u.dbrec.type));
2600 * set record elements (for record extraction)
2602 static int do_recordElements (void *o, Tcl_Interp *interp,
2603 int argc, char **argv)
2605 IrTcl_SetObj *obj = o;
2609 obj->recordElements = NULL;
2612 else if (argc == -1)
2613 return ir_tcl_strdel (NULL, &obj->recordElements);
2616 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2617 " ?position?\"", NULL);
2622 xfree (obj->recordElements);
2623 return ir_tcl_strdup (NULL, &obj->recordElements,
2624 (*argv[2] ? argv[2] : NULL));
2626 Tcl_AppendResult (interp, obj->recordElements, NULL);
2633 static int ir_diagResult (Tcl_Interp *interp, IrTcl_Diagnostic *list, int num)
2639 for (i = 0; i<num; i++)
2641 sprintf (buf, "%d", list[i].condition);
2642 Tcl_AppendElement (interp, buf);
2643 cp = diagbib1_str (list[i].condition);
2645 Tcl_AppendElement (interp, (char*) cp);
2647 Tcl_AppendElement (interp, "");
2648 if (list[i].addinfo)
2649 Tcl_AppendElement (interp, (char*) list[i].addinfo);
2651 Tcl_AppendElement (interp, "");
2657 * do_diag: Return diagnostic record info
2659 static int do_diag (void *o, Tcl_Interp *interp, int argc, char **argv)
2661 IrTcl_SetObj *obj = o;
2663 IrTcl_RecordList *rl;
2669 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2670 " position\"", NULL);
2673 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2675 rl = find_IR_record (obj, offset);
2678 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2681 if (rl->which != Z_NamePlusRecord_surrogateDiagnostic)
2683 Tcl_AppendResult (interp, "No Diagnostic record at #", argv[2], NULL);
2686 return ir_diagResult (interp, rl->u.surrogateDiagnostics.list,
2687 rl->u.surrogateDiagnostics.num);
2691 * do_getMarc: Get ISO2709 Record lines/fields
2693 static int do_getMarc (void *o, Tcl_Interp *interp, int argc, char **argv)
2695 IrTcl_SetObj *obj = o;
2697 IrTcl_RecordList *rl;
2703 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2704 " position line|field tag ind field\"", NULL);
2707 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2709 rl = find_IR_record (obj, offset);
2712 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2715 if (rl->which != Z_NamePlusRecord_databaseRecord)
2717 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2720 return ir_tcl_get_marc (interp, rl->u.dbrec.buf, argc, argv);
2724 * do_getSutrs: Get SUTRS Record
2726 static int do_getSutrs (void *o, Tcl_Interp *interp, int argc, char **argv)
2728 IrTcl_SetObj *obj = o;
2730 IrTcl_RecordList *rl;
2736 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2737 " position\"", NULL);
2740 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2742 rl = find_IR_record (obj, offset);
2745 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2748 if (rl->which != Z_NamePlusRecord_databaseRecord)
2750 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2753 if (!rl->u.dbrec.buf || rl->u.dbrec.type != VAL_SUTRS)
2755 Tcl_AppendElement (interp, rl->u.dbrec.buf);
2761 * do_getGrs: Get a GRS-1 Record
2763 static int do_getGrs (void *o, Tcl_Interp *interp, int argc, char **argv)
2765 IrTcl_SetObj *obj = o;
2767 IrTcl_RecordList *rl;
2773 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2774 " position ?(set,tag) (set,tag) ...?\"", NULL);
2777 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2779 rl = find_IR_record (obj, offset);
2782 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2785 if (rl->which != Z_NamePlusRecord_databaseRecord)
2787 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2790 if (rl->u.dbrec.type != VAL_GRS1)
2792 return ir_tcl_get_grs (interp, rl->u.dbrec.u.grs1, argc, argv);
2797 * do_getExplain: Get an Explain Record
2799 static int do_getExplain (void *o, Tcl_Interp *interp, int argc, char **argv)
2801 IrTcl_SetObj *obj = o;
2802 IrTcl_Obj *p = obj->parent;
2804 Z_ext_typeent *etype;
2806 IrTcl_RecordList *rl;
2812 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2813 " position ?mask? ...\"", NULL);
2816 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2818 rl = find_IR_record (obj, offset);
2821 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2824 if (rl->which != Z_NamePlusRecord_databaseRecord)
2826 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2829 if (!rl->u.dbrec.buf || rl->u.dbrec.type != VAL_EXPLAIN)
2832 if (!(etype = z_ext_getentbyref (VAL_EXPLAIN)))
2834 assert (rl->u.dbrec.buf);
2835 odr_setbuf (p->odr_in, rl->u.dbrec.buf, rl->u.dbrec.size, 0);
2836 if (!(*etype->fun)(p->odr_in, (char **) &rr, 0, 0))
2839 if (etype->what != Z_External_explainRecord)
2842 return ir_tcl_get_explain (interp, rr, argc, argv);
2846 * do_responseStatus: Return response status (present or search)
2848 static int do_responseStatus (void *o, Tcl_Interp *interp,
2849 int argc, char **argv)
2851 IrTcl_SetObj *obj = o;
2855 obj->recordFlag = 0;
2856 obj->nonSurrogateDiagnosticNum = 0;
2857 obj->nonSurrogateDiagnosticList = NULL;
2860 else if (argc == -1)
2862 ir_deleteDiags (&obj->nonSurrogateDiagnosticList,
2863 &obj->nonSurrogateDiagnosticNum);
2866 if (!obj->recordFlag)
2868 Tcl_AppendElement (interp, "OK");
2873 case Z_Records_DBOSD:
2874 Tcl_AppendElement (interp, "DBOSD");
2877 Tcl_AppendElement (interp, "NSD");
2878 return ir_diagResult (interp, obj->nonSurrogateDiagnosticList,
2879 obj->nonSurrogateDiagnosticNum);
2880 case Z_Records_multipleNSD:
2881 Tcl_AppendElement (interp, "NSD");
2882 return ir_diagResult (interp, obj->nonSurrogateDiagnosticList,
2883 obj->nonSurrogateDiagnosticNum);
2889 * do_present: Perform Present Request
2892 static int do_present (void *o, Tcl_Interp *interp, int argc, char **argv)
2894 IrTcl_SetObj *obj = o;
2897 Z_PresentRequest *req;
2905 if (Tcl_GetInt (interp, argv[2], &start) == TCL_ERROR)
2912 if (Tcl_GetInt (interp, argv[3], &number) == TCL_ERROR)
2917 logf (LOG_DEBUG, "present %s %d %d", *argv, start, number);
2921 Tcl_AppendResult (interp, "not connected", NULL);
2922 return ir_tcl_error_exec (interp, argc, argv);
2925 obj->number = number;
2927 apdu = zget_APDU (p->odr_out, Z_APDU_presentRequest);
2928 req = apdu->u.presentRequest;
2930 set_referenceId (p->odr_out, &req->referenceId,
2931 obj->set_inher.referenceId);
2933 req->resultSetId = obj->setName ? obj->setName : "Default";
2935 req->resultSetStartPoint = &start;
2936 req->numberOfRecordsRequested = &number;
2937 if (obj->set_inher.preferredRecordSyntax)
2939 struct oident ident;
2941 ident.proto = p->protocol_type;
2942 ident.oclass = CLASS_RECSYN;
2943 ident.value = *obj->set_inher.preferredRecordSyntax;
2944 logf (LOG_DEBUG, "Preferred record syntax is %d", ident.value);
2945 req->preferredRecordSyntax = odr_oiddup (p->odr_out,
2946 oid_getoidbyent (&ident));
2949 req->preferredRecordSyntax = 0;
2951 if (obj->set_inher.elementSetNames && *obj->set_inher.elementSetNames)
2953 Z_ElementSetNames *esn = odr_malloc (p->odr_out, sizeof(*esn));
2954 Z_RecordComposition *compo = odr_malloc (p->odr_out, sizeof(*compo));
2956 esn->which = Z_ElementSetNames_generic;
2957 esn->u.generic = obj->set_inher.elementSetNames;
2959 req->recordComposition = compo;
2960 compo->which = Z_RecordComp_simple;
2961 compo->u.simple = esn;
2964 req->recordComposition = NULL;
2965 return ir_tcl_send_APDU (interp, p, apdu, "present", *argv);
2968 #define IR_TCL_RECORD_ENCODING_ISO2709 1
2969 #define IR_TCL_RECORD_ENCODING_RAW 2
2975 } IrTcl_FileRecordHead;
2978 * do_loadFile: Load result set from file
2980 static int do_loadFile (void *o, Tcl_Interp *interp,
2981 int argc, char **argv)
2983 IrTcl_SetObj *setobj = o;
2995 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2996 " filename ?start? ?number?\"", NULL);
3000 start = atoi (argv[3]);
3002 number = atoi (argv[4]);
3005 inf = fopen (argv[2], "r");
3008 Tcl_AppendResult (interp, "Cannot open ", argv[2], NULL);
3009 return ir_tcl_error_exec (interp, argc, argv);
3011 while (offset < (start+number))
3013 IrTcl_FileRecordHead head;
3014 IrTcl_RecordList *rl;
3016 if (fread (&head, sizeof(head), 1, inf) < 1)
3018 rl = new_IR_record (setobj, offset,
3019 Z_NamePlusRecord_databaseRecord,
3020 (argc > 5) ? argv[5] : NULL);
3021 rl->u.dbrec.type = head.syntax;
3022 if (head.encoding == IR_TCL_RECORD_ENCODING_ISO2709)
3024 if (!(buf = ir_tcl_fread_marc (inf, &size)))
3026 rl->u.dbrec.buf = buf;
3027 rl->u.dbrec.size = size;
3028 if (size != head.size)
3031 Tcl_AppendResult (interp, "bad ISO2709 encoding", NULL);
3032 return ir_tcl_error_exec (interp, argc, argv);
3035 else if (head.encoding == IR_TCL_RECORD_ENCODING_RAW)
3037 rl->u.dbrec.size = head.size;
3038 rl->u.dbrec.buf = ir_tcl_malloc (head.size + 1);
3039 if (fread (rl->u.dbrec.buf, rl->u.dbrec.size, 1, inf) < 1)
3042 Tcl_AppendResult (interp, "bad raw encoding", NULL);
3043 return ir_tcl_error_exec (interp, argc, argv);
3045 rl->u.dbrec.buf[rl->u.dbrec.size] = '\0';
3049 rl->u.dbrec.buf = NULL;
3050 rl->u.dbrec.size = 0;
3052 Tcl_AppendResult (interp, "bad encoding", NULL);
3053 return ir_tcl_error_exec (interp, argc, argv);
3057 setobj->numberOfRecordsReturned = offset - start;
3063 * do_saveFile: Save result set on file
3065 static int do_saveFile (void *o, Tcl_Interp *interp,
3066 int argc, char **argv)
3068 IrTcl_SetObj *setobj = o;
3073 IrTcl_RecordList *rl;
3079 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
3080 " filename ?start? ?number?\"", NULL);
3084 start = atoi (argv[3]);
3086 number = atoi (argv[4]);
3089 outf = fopen (argv[2], "w");
3092 Tcl_AppendResult (interp, "cannot open file", NULL);
3093 return ir_tcl_error_exec (interp, argc, argv);
3095 while (offset < (start+number) && (rl = find_IR_record (setobj, offset)))
3097 if (rl->which == Z_NamePlusRecord_databaseRecord &&
3098 rl->u.dbrec.buf && rl->u.dbrec.size)
3100 IrTcl_FileRecordHead head;
3102 head.encoding = IR_TCL_RECORD_ENCODING_RAW;
3103 head.syntax = rl->u.dbrec.type;
3104 head.size = rl->u.dbrec.size;
3105 if (fwrite (&head, sizeof(head), 1, outf) < 1)
3107 Tcl_AppendResult (interp, "cannot write", NULL);
3108 return ir_tcl_error_exec (interp, argc, argv);
3110 if (fwrite (rl->u.dbrec.buf, rl->u.dbrec.size, 1, outf) < 1)
3112 Tcl_AppendResult (interp, "cannot write", NULL);
3113 return ir_tcl_error_exec (interp, argc, argv);
3120 Tcl_AppendResult (interp, "cannot write ", NULL);
3121 return ir_tcl_error_exec (interp, argc, argv);
3127 /* ------------------------------------------------------- */
3129 * do_sort: Do sort request
3131 static int do_sort (void *o, Tcl_Interp *interp, int argc, char **argv)
3135 IrTcl_SetObj *obj = o;
3137 char sort_string[64], sort_flags[64];
3140 Z_SortKeySpecList *sksl;
3151 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1], "query\"",
3155 logf (LOG_DEBUG, "sort %s %s", *argv, argv[2]);
3158 Tcl_AppendResult (interp, "not connected", NULL);
3159 return ir_tcl_error_exec (interp, argc, argv);
3161 apdu = zget_APDU (p->odr_out, Z_APDU_sortRequest);
3162 sksl = (Z_SortKeySpecList *) odr_malloc (p->odr_out, sizeof(*sksl));
3163 req = apdu->u.sortRequest;
3165 set_referenceId (p->odr_out, &req->referenceId,
3166 obj->set_inher.referenceId);
3169 req->num_inputResultSetNames = 1;
3170 req->inputResultSetNames = (Z_InternationalString **)
3171 odr_malloc (p->odr_out, sizeof(*req->inputResultSetNames));
3172 req->inputResultSetNames[0] = obj->setName;
3174 req->inputResultSetNames =
3175 (Z_StringList *)odr_malloc (p->odr_out,
3176 sizeof(*req->inputResultSetNames));
3177 req->inputResultSetNames->num_strings = 1;
3178 req->inputResultSetNames->strings =
3179 (char **)odr_malloc (p->odr_out,
3180 sizeof(*req->inputResultSetNames->strings));
3181 req->inputResultSetNames->strings[0] = obj->setName;
3184 req->sortedResultSetName = (char *) obj->setName;
3187 req->sortSequence = sksl;
3188 sksl->num_specs = 0;
3189 sksl->specs = (Z_SortKeySpec **)
3190 odr_malloc (p->odr_out, sizeof(sksl->specs) * 20);
3192 bib1.proto = PROTO_Z3950;
3193 bib1.oclass = CLASS_ATTSET;
3194 bib1.value = VAL_BIB1;
3196 while ((sscanf (arg, "%63s %63s%n", sort_string, sort_flags, &off)) == 2
3200 char *sort_string_sep;
3201 Z_SortKeySpec *sks = (Z_SortKeySpec *)
3202 odr_malloc (p->odr_out, sizeof(*sks));
3203 Z_SortKey *sk = (Z_SortKey *)
3204 odr_malloc (p->odr_out, sizeof(*sk));
3207 sksl->specs[sksl->num_specs++] = sks;
3208 sks->sortElement = (Z_SortElement *)
3209 odr_malloc (p->odr_out, sizeof(*sks->sortElement));
3210 sks->sortElement->which = Z_SortElement_generic;
3211 sks->sortElement->u.generic = sk;
3213 if ((sort_string_sep = strchr (sort_string, '=')))
3215 Z_AttributeElement *el = (Z_AttributeElement *)
3216 odr_malloc (p->odr_out, sizeof(*el));
3217 sk->which = Z_SortKey_sortAttributes;
3218 sk->u.sortAttributes =
3219 (Z_SortAttributes *)
3220 odr_malloc (p->odr_out, sizeof(*sk->u.sortAttributes));
3221 sk->u.sortAttributes->id = oid_ent_to_oid(&bib1, oid);
3222 sk->u.sortAttributes->list =
3224 odr_malloc (p->odr_out, sizeof(*sk->u.sortAttributes->list));
3225 sk->u.sortAttributes->list->num_attributes = 1;
3226 sk->u.sortAttributes->list->attributes =
3227 (Z_AttributeElement **)odr_malloc (p->odr_out,
3228 sizeof(*sk->u.sortAttributes->list->attributes));
3229 sk->u.sortAttributes->list->attributes[0] = el;
3230 el->attributeSet = 0;
3231 el->attributeType = (int *)
3232 odr_malloc (p->odr_out, sizeof(*el->attributeType));
3233 *el->attributeType = atoi (sort_string);
3234 el->which = Z_AttributeValue_numeric;
3235 el->value.numeric = (int *)
3236 odr_malloc (p->odr_out, sizeof(*el->value.numeric));
3237 *el->value.numeric = atoi (sort_string_sep + 1);
3241 sk->which = Z_SortKey_sortField;
3242 sk->u.sortField = (char *)odr_malloc (p->odr_out, strlen(sort_string)+1);
3243 strcpy (sk->u.sortField, sort_string);
3245 sks->sortRelation = (int *)
3246 odr_malloc (p->odr_out, sizeof(*sks->sortRelation));
3247 *sks->sortRelation = Z_SortRelation_ascending;
3248 sks->caseSensitivity = (int *)
3249 odr_malloc (p->odr_out, sizeof(*sks->caseSensitivity));
3250 *sks->caseSensitivity = Z_SortCase_caseSensitive;
3253 sks->which = Z_SortKeySpec_null;
3254 sks->u.null = odr_nullval ();
3256 sks->missingValueAction = NULL;
3259 for (i = 0; sort_flags[i]; i++)
3261 switch (sort_flags[i])
3266 *sks->sortRelation = Z_SortRelation_descending;
3271 *sks->sortRelation = Z_SortRelation_ascending;
3275 *sks->caseSensitivity = Z_SortCase_caseInsensitive;
3279 *sks->caseSensitivity = Z_SortCase_caseSensitive;
3284 if (!sksl->num_specs)
3286 printf ("Missing sort specifications\n");
3289 return ir_tcl_send_APDU (interp, p, apdu, "sort", *argv);
3292 static IrTcl_Method ir_set_method_tab[] = {
3293 { "search", do_search, NULL},
3294 { "searchResponse", do_searchResponse, NULL},
3295 { "presentResponse", do_presentResponse, NULL},
3296 { "searchStatus", do_searchStatus, NULL},
3297 { "presentStatus", do_presentStatus, NULL},
3298 { "nextResultSetPosition", do_nextResultSetPosition, NULL},
3299 { "setName", do_setName, NULL},
3300 { "resultCount", do_resultCount, NULL},
3301 { "numberOfRecordsReturned", do_numberOfRecordsReturned, NULL},
3302 { "present", do_present, NULL},
3303 { "type", do_type, NULL},
3304 { "getMarc", do_getMarc, NULL},
3305 { "getSutrs", do_getSutrs, NULL},
3306 { "getGrs", do_getGrs, NULL},
3307 { "getExplain", do_getExplain, NULL},
3308 { "recordType", do_recordType, NULL},
3309 { "recordElements", do_recordElements, NULL},
3310 { "diag", do_diag, NULL},
3311 { "responseStatus", do_responseStatus, NULL},
3312 { "loadFile", do_loadFile, NULL},
3313 { "saveFile", do_saveFile, NULL},
3314 { "sort", do_sort, NULL },
3315 { "sortResponse", do_sortResponse, NULL},
3316 { "sortStatus", do_sortStatus, NULL},
3317 { "searchResult", do_searchResult, NULL},
3322 * ir_set_obj_method: IR Set Object methods
3324 static int ir_set_obj_method (ClientData clientData, Tcl_Interp *interp,
3325 int argc, char **argv)
3327 IrTcl_Methods tabs[3];
3328 IrTcl_SetObj *p = (IrTcl_SetObj *) clientData;
3333 Tcl_AppendResult (interp, wrongArgs, *argv, " method args...\"", NULL);
3336 tabs[0].tab = ir_set_method_tab;
3338 tabs[1].tab = ir_set_c_method_tab;
3339 tabs[1].obj = &p->set_inher;
3342 if (ir_tcl_method (interp, argc, argv, tabs, &r) == TCL_ERROR)
3343 return ir_tcl_method_error (interp, argc, argv, tabs);
3348 * ir_set_obj_delete: IR Set Object disposal
3350 static void ir_set_obj_delete (ClientData clientData)
3352 IrTcl_Methods tabs[3];
3353 IrTcl_SetObj *p = (IrTcl_SetObj *) clientData;
3355 logf (LOG_DEBUG, "ir set delete");
3357 tabs[0].tab = ir_set_method_tab;
3359 tabs[1].tab = ir_set_c_method_tab;
3360 tabs[1].obj = &p->set_inher;
3363 ir_tcl_method (NULL, -1, NULL, tabs, NULL);
3369 * ir_set_obj_init: IR Set Object initialization
3371 static int ir_set_obj_init (ClientData clientData, Tcl_Interp *interp,
3372 int argc, char **argv, ClientData *subData,
3373 ClientData parentData)
3375 IrTcl_Methods tabs[3];
3378 if (argc < 2 || argc > 3)
3380 Tcl_AppendResult (interp, wrongArgs, *argv,
3381 " objSetName ?objParent?\"", NULL);
3384 obj = ir_tcl_malloc (sizeof(*obj));
3385 logf (LOG_DEBUG, "ir set create %s", argv[1]);
3392 obj->parent = (IrTcl_Obj *) parentData;
3394 dst = &obj->set_inher;
3395 src = &obj->parent->set_inher;
3397 if ((dst->num_databaseNames = src->num_databaseNames))
3399 dst->databaseNames =
3400 ir_tcl_malloc (sizeof (*dst->databaseNames)
3401 * (1+dst->num_databaseNames));
3402 for (i = 0; i < dst->num_databaseNames; i++)
3403 if (ir_tcl_strdup (interp, &dst->databaseNames[i],
3404 src->databaseNames[i]) == TCL_ERROR)
3406 dst->databaseNames[i] = NULL;
3409 dst->databaseNames = NULL;
3410 if (ir_tcl_strdup (interp, &dst->queryType, src->queryType)
3414 if (ir_tcl_strdup (interp, &dst->referenceId, src->referenceId)
3418 if (ir_tcl_strdup (interp, &dst->elementSetNames, src->elementSetNames)
3422 if (ir_tcl_strdup (interp, &dst->smallSetElementSetNames,
3423 src->smallSetElementSetNames)
3427 if (ir_tcl_strdup (interp, &dst->mediumSetElementSetNames,
3428 src->mediumSetElementSetNames)
3432 if (src->preferredRecordSyntax &&
3433 (dst->preferredRecordSyntax
3434 = ir_tcl_malloc (sizeof(*dst->preferredRecordSyntax))))
3435 *dst->preferredRecordSyntax = *src->preferredRecordSyntax;
3437 dst->preferredRecordSyntax = NULL;
3438 dst->replaceIndicator = src->replaceIndicator;
3439 dst->smallSetUpperBound = src->smallSetUpperBound;
3440 dst->largeSetLowerBound = src->largeSetLowerBound;
3441 dst->mediumSetPresentNumber = src->mediumSetPresentNumber;
3446 tabs[0].tab = ir_set_method_tab;
3450 if (ir_tcl_method (interp, 0, NULL, tabs, NULL) == TCL_ERROR)
3453 *subData = (ClientData) obj;
3458 * ir_set_obj_mk: IR Set Object creation
3460 static int ir_set_obj_mk (ClientData clientData, Tcl_Interp *interp,
3461 int argc, char **argv)
3464 ClientData parentData = 0;
3469 Tcl_CmdInfo parent_info;
3470 if (!Tcl_GetCommandInfo (interp, argv[2], &parent_info))
3472 Tcl_AppendResult (interp, "no object parent", NULL);
3473 return ir_tcl_error_exec (interp, argc, argv);
3475 parentData = parent_info.clientData;
3477 r = ir_set_obj_init (clientData, interp, argc, argv, &subData, parentData);
3480 Tcl_CreateCommand (interp, argv[1], ir_set_obj_method,
3481 subData, ir_set_obj_delete);
3485 IrTcl_Class ir_set_obj_class = {
3492 /* ------------------------------------------------------- */
3495 * do_scan: Perform scan
3497 static int do_scan (void *o, Tcl_Interp *interp, int argc, char **argv)
3501 IrTcl_ScanObj *obj = o;
3502 IrTcl_Obj *p = obj->parent;
3507 struct ccl_rpn_node *rpn;
3511 #if TCL_MAJOR_VERSION > 8 || (TCL_MAJOR_VERSION == 8 && TCL_MINOR_VERSION > 0)
3519 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
3520 " scanQuery\"", NULL);
3523 #if TCL_MAJOR_VERSION > 8 || (TCL_MAJOR_VERSION == 8 && TCL_MINOR_VERSION > 0)
3524 start_term = Tcl_UtfToExternalDString(0, argv[2], -1, &ds);
3526 start_term = argv[2];
3528 logf (LOG_DEBUG, "scan %s %s", *argv, start_term);
3529 if (!p->set_inher.num_databaseNames)
3531 Tcl_AppendResult (interp, "no databaseNames", NULL);
3532 code = ir_tcl_error_exec (interp, argc, argv);
3537 Tcl_AppendResult (interp, "not connected", NULL);
3538 code = ir_tcl_error_exec (interp, argc, argv);
3542 apdu = zget_APDU (p->odr_out, Z_APDU_scanRequest);
3543 req = apdu->u.scanRequest;
3545 set_referenceId (p->odr_out, &req->referenceId, p->set_inher.referenceId);
3546 req->num_databaseNames = p->set_inher.num_databaseNames;
3547 req->databaseNames = p->set_inher.databaseNames;
3551 if (!(req->termListAndStartPoint =
3552 p_query_scan (p->odr_out, p->protocol_type,
3553 &req->attributeSet, start_term)))
3555 Tcl_AppendResult (interp, "query syntax error", NULL);
3556 code = ir_tcl_error_exec (interp, argc, argv);
3560 rpn = ccl_find_str(p->bibset, start_term, &r, &pos);
3563 Tcl_AppendResult (interp, "ccl syntax error ", ccl_err_msg(r), NULL);
3564 code = ir_tcl_error_exec (interp, argc, argv);
3567 bib1.proto = p->protocol_type;
3568 bib1.oclass = CLASS_ATTSET;
3569 bib1.value = VAL_BIB1;
3571 req->attributeSet = oid_getoidbyent (&bib1);
3572 if (!(req->termListAndStartPoint = ccl_scan_query (p->odr_out, rpn)))
3578 req->stepSize = &obj->stepSize;
3579 req->numberOfTermsRequested = &obj->numberOfTermsRequested;
3580 req->preferredPositionInResponse = &obj->preferredPositionInResponse;
3581 logf (LOG_DEBUG, "stepSize=%d", *req->stepSize);
3582 logf (LOG_DEBUG, "numberOfTermsRequested=%d",
3583 *req->numberOfTermsRequested);
3584 logf (LOG_DEBUG, "preferredPositionInResponse=%d",
3585 *req->preferredPositionInResponse);
3587 code = ir_tcl_send_APDU (interp, p, apdu, "scan", *argv);
3589 #if TCL_MAJOR_VERSION > 8 || (TCL_MAJOR_VERSION == 8 && TCL_MINOR_VERSION > 0)
3590 Tcl_DStringFree (&ds);
3596 * do_scanResponse: add scan response handler
3598 static int do_scanResponse (void *o, Tcl_Interp *interp,
3599 int argc, char **argv)
3601 IrTcl_ScanObj *obj = o;
3605 obj->scanResponse = NULL;
3608 else if (argc == -1)
3609 return ir_tcl_strdel (interp, &obj->scanResponse);
3612 xfree (obj->scanResponse);
3615 if (ir_tcl_strdup (interp, &obj->scanResponse, argv[2])
3620 obj->scanResponse = NULL;
3626 * do_stepSize: Set/get replace Step Size
3628 static int do_stepSize (void *obj, Tcl_Interp *interp,
3629 int argc, char **argv)
3631 IrTcl_ScanObj *p = obj;
3637 return ir_tcl_get_set_int (&p->stepSize, interp, argc, argv);
3641 * do_numberOfTermsRequested: Set/get Number of Terms requested
3643 static int do_numberOfTermsRequested (void *obj, Tcl_Interp *interp,
3644 int argc, char **argv)
3646 IrTcl_ScanObj *p = obj;
3650 p->numberOfTermsRequested = 20;
3653 return ir_tcl_get_set_int (&p->numberOfTermsRequested, interp, argc, argv);
3658 * do_preferredPositionInResponse: Set/get preferred Position
3660 static int do_preferredPositionInResponse (void *obj, Tcl_Interp *interp,
3661 int argc, char **argv)
3663 IrTcl_ScanObj *p = obj;
3667 p->preferredPositionInResponse = 1;
3670 return ir_tcl_get_set_int (&p->preferredPositionInResponse, interp,
3675 * do_scanStatus: Get scan status
3677 static int do_scanStatus (void *obj, Tcl_Interp *interp,
3678 int argc, char **argv)
3680 IrTcl_ScanObj *p = obj;
3684 return ir_tcl_get_set_int (&p->scanStatus, interp, argc, argv);
3688 * do_numberOfEntriesReturned: Get number of Entries returned
3690 static int do_numberOfEntriesReturned (void *obj, Tcl_Interp *interp,
3691 int argc, char **argv)
3693 IrTcl_ScanObj *p = obj;
3697 return ir_tcl_get_set_int (&p->numberOfEntriesReturned, interp,
3702 * do_positionOfTerm: Get position of Term
3704 static int do_positionOfTerm (void *obj, Tcl_Interp *interp,
3705 int argc, char **argv)
3707 IrTcl_ScanObj *p = obj;
3711 return ir_tcl_get_set_int (&p->positionOfTerm, interp, argc, argv);
3715 * do_scanLine: get Scan Line (surrogate or normal) after response
3717 static int do_scanLine (void *obj, Tcl_Interp *interp, int argc, char **argv)
3719 IrTcl_ScanObj *p = obj;
3725 p->entries_flag = 0;
3727 p->nonSurrogateDiagnosticNum = 0;
3728 p->nonSurrogateDiagnosticList = 0;
3731 else if (argc == -1)
3733 p->entries_flag = 0;
3734 /* release entries */
3737 ir_deleteDiags (&p->nonSurrogateDiagnosticList,
3738 &p->nonSurrogateDiagnosticNum);
3743 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
3744 " position\"", NULL);
3747 if (Tcl_GetInt (interp, argv[2], &i) == TCL_ERROR)
3749 if (!p->entries_flag || !p->entries || i >= p->num_entries || i < 0)
3751 switch (p->entries[i].which)
3753 case Z_Entry_termInfo:
3754 Tcl_AppendElement (interp, "T");
3755 if (p->entries[i].u.term.buf)
3756 Tcl_AppendElement (interp, p->entries[i].u.term.buf);
3758 Tcl_AppendElement (interp, "");
3759 sprintf (numstr, "%d", p->entries[i].u.term.globalOccurrences);
3760 Tcl_AppendElement (interp, numstr);
3762 case Z_Entry_surrogateDiagnostic:
3763 Tcl_AppendElement (interp, "SD");
3764 return ir_diagResult (interp, p->entries[i].u.diag.list,
3765 p->entries[i].u.diag.num);
3771 static IrTcl_Method ir_scan_method_tab[] = {
3772 { "scan", do_scan, NULL},
3773 { "scanResponse", do_scanResponse, NULL},
3774 { "stepSize", do_stepSize, NULL},
3775 { "numberOfTermsRequested", do_numberOfTermsRequested, NULL},
3776 { "preferredPositionInResponse", do_preferredPositionInResponse, NULL},
3777 { "scanStatus", do_scanStatus, NULL},
3778 { "numberOfEntriesReturned", do_numberOfEntriesReturned, NULL},
3779 { "positionOfTerm", do_positionOfTerm, NULL},
3780 { "scanLine", do_scanLine, NULL},
3785 * ir_scan_obj_method: IR Scan Object methods
3787 static int ir_scan_obj_method (ClientData clientData, Tcl_Interp *interp,
3788 int argc, char **argv)
3790 IrTcl_Methods tabs[2];
3795 Tcl_AppendResult (interp, wrongArgs, *argv, " method args...\"", NULL);
3798 tabs[0].tab = ir_scan_method_tab;
3799 tabs[0].obj = clientData;
3802 if (ir_tcl_method (interp, argc, argv, tabs, &r) == TCL_ERROR)
3803 return ir_tcl_method_error (interp, argc, argv, tabs);
3808 * ir_scan_obj_delete: IR Scan Object disposal
3810 static void ir_scan_obj_delete (ClientData clientData)
3812 IrTcl_Methods tabs[2];
3813 IrTcl_ScanObj *obj = (IrTcl_ScanObj *) clientData;
3815 tabs[0].tab = ir_scan_method_tab;
3819 ir_tcl_method (NULL, -1, NULL, tabs, NULL);
3824 * ir_scan_obj_mk: IR Scan Object creation
3826 static int ir_scan_obj_mk (ClientData clientData, Tcl_Interp *interp,
3827 int argc, char **argv)
3829 Tcl_CmdInfo parent_info;
3831 IrTcl_Methods tabs[2];
3835 Tcl_AppendResult (interp, wrongArgs, *argv,
3836 "objScanName objParentName\"", NULL);
3839 logf (LOG_DEBUG, "ir scan create %s", argv[1]);
3840 if (!Tcl_GetCommandInfo (interp, argv[2], &parent_info))
3842 Tcl_AppendResult (interp, "no object parent", NULL);
3843 return ir_tcl_error_exec (interp, argc, argv);
3845 obj = ir_tcl_malloc (sizeof(*obj));
3846 obj->parent = (IrTcl_Obj *) parent_info.clientData;
3848 tabs[0].tab = ir_scan_method_tab;
3852 if (ir_tcl_method (interp, 0, NULL, tabs, NULL) == TCL_ERROR)
3854 Tcl_CreateCommand (interp, argv[1], ir_scan_obj_method,
3855 (ClientData) obj, ir_scan_obj_delete);
3859 /* ------------------------------------------------------- */
3862 * ir_log_init_proc: set yaz log level
3864 static int ir_log_init_proc (ClientData clientData, Tcl_Interp *interp,
3865 int argc, char **argv)
3868 if (argc <= 1 || argc > 4)
3870 Tcl_AppendResult (interp, wrongArgs, *argv,
3871 " ?level ?prefix ?filename\"", NULL);
3874 lev = yaz_log_mask_str (argv[1]);
3876 yaz_log_init (lev, "", NULL);
3878 yaz_log_init (lev, argv[2], NULL);
3880 yaz_log_init (lev, argv[2], argv[3]);
3881 if (lev & LOG_DEBUG)
3882 odr_print_file = yaz_log_file();
3889 * ir_log_proc: log yaz message
3891 static int ir_log_proc (ClientData clientData, Tcl_Interp *interp,
3892 int argc, char **argv)
3897 Tcl_AppendResult (interp, wrongArgs, *argv,
3898 " level string\"", NULL);
3901 mask = yaz_log_mask_str_x (argv[1], 0);
3902 logf (LOG_DEBUG, "%s", argv[2]);
3908 * ir_version: log ir version
3910 static int ir_version (ClientData clientData, Tcl_Interp *interp,
3911 int argc, char **argv)
3913 Tcl_AppendElement (interp, IR_TCL_VERSION);
3914 Tcl_AppendElement (interp, YAZ_VERSION);
3919 /* ------------------------------------------------------- */
3920 static void ir_initResponse (void *obj, Z_InitResponse *initrs)
3924 p->initResult = *initrs->result ? 1 : 0;
3925 if (!*initrs->result)
3926 logf (LOG_DEBUG, "Connection rejected by target");
3928 logf (LOG_DEBUG, "Connection accepted by target");
3930 get_referenceId (&p->set_inher.referenceId, initrs->referenceId);
3932 xfree (p->targetImplementationId);
3933 ir_tcl_strdup (p->interp, &p->targetImplementationId,
3934 initrs->implementationId);
3935 xfree (p->targetImplementationName);
3936 ir_tcl_strdup (p->interp, &p->targetImplementationName,
3937 initrs->implementationName);
3938 xfree (p->targetImplementationVersion);
3939 ir_tcl_strdup (p->interp, &p->targetImplementationVersion,
3940 initrs->implementationVersion);
3942 p->maximumRecordSize = *initrs->maximumRecordSize;
3943 p->preferredMessageSize = *initrs->preferredMessageSize;
3945 memcpy (&p->options, initrs->options, sizeof(initrs->options));
3946 memcpy (&p->protocolVersion, initrs->protocolVersion,
3947 sizeof(initrs->protocolVersion));
3948 xfree (p->userInformationField);
3949 p->userInformationField = NULL;
3950 if (initrs->userInformationField)
3954 if (initrs->userInformationField->which == ODR_EXTERNAL_octet &&
3955 (p->userInformationField =
3956 ir_tcl_malloc ((len =
3957 initrs->userInformationField->
3958 u.octet_aligned->len) +1)))
3960 memcpy (p->userInformationField,
3961 initrs->userInformationField->u.octet_aligned->buf,
3963 (p->userInformationField)[len] = '\0';
3968 static void ir_deleteDiags (IrTcl_Diagnostic **dst_list, int *dst_num)
3971 for (i = 0; i<*dst_num; i++)
3972 xfree ((*dst_list)[i].addinfo);
3978 static void ir_handleDiags (IrTcl_Diagnostic **dst_list, int *dst_num,
3979 Z_DiagRec **list, int num)
3982 char *addinfo = NULL;
3985 *dst_list = ir_tcl_malloc (sizeof(**dst_list) * num);
3986 for (i = 0; i<num; i++)
3989 switch (list[i]->which)
3991 case Z_DiagRec_defaultFormat:
3992 (*dst_list)[i].condition = *list[i]->u.defaultFormat->condition;
3994 switch (list[i]->u.defaultFormat->which)
3996 case Z_DefaultDiagFormat_v2Addinfo:
3997 addinfo = list[i]->u.defaultFormat->u.v2Addinfo;
3999 case Z_DefaultDiagFormat_v3Addinfo:
4000 addinfo = list[i]->u.defaultFormat->u.v3Addinfo;
4004 addinfo = list[i]->u.defaultFormat->addinfo;
4007 ((*dst_list)[i].addinfo = ir_tcl_malloc (strlen(addinfo)+1)))
4008 strcpy ((*dst_list)[i].addinfo, addinfo);
4009 cp = diagbib1_str ((*dst_list)[i].condition);
4010 logf (LOG_DEBUG, "Diag %d %s %s", (*dst_list)[i].condition,
4011 cp ? cp : "", addinfo ? addinfo : "");
4014 (*dst_list)[i].addinfo = NULL;
4015 (*dst_list)[i].condition = 0;
4020 static void ir_handleDBRecord (IrTcl_Obj *p, IrTcl_RecordList *rl,
4023 struct oident *ident;
4024 Z_ext_typeent *etype;
4026 logf (LOG_DEBUG, "handleDBRecord size=%d", oe->u.octet_aligned->len);
4027 rl->u.dbrec.size = oe->u.octet_aligned->len;
4028 rl->u.dbrec.buf = NULL;
4030 if ((ident = oid_getentbyoid (oe->direct_reference)))
4031 rl->u.dbrec.type = ident->value;
4033 rl->u.dbrec.type = VAL_USMARC;
4035 if (ident && (oe->which == Z_External_single ||
4036 oe->which == Z_External_octet)
4037 && (etype = z_ext_getentbyref (ident->value)))
4041 odr_setbuf (p->odr_in, (char*) oe->u.octet_aligned->buf,
4042 oe->u.octet_aligned->len, 0);
4043 if (!(*etype->fun)(p->odr_in, (char **) &rr, 0, 0))
4045 rl->u.dbrec.type = VAL_NONE;
4048 switch (etype->what)
4050 case Z_External_sutrs:
4051 logf (LOG_DEBUG, "Z_External_sutrs");
4053 if ((rl->u.dbrec.buf = ir_tcl_malloc (oe->u.sutrs->len+1)))
4055 memcpy (rl->u.dbrec.buf, oe->u.sutrs->buf,
4057 rl->u.dbrec.buf[oe->u.sutrs->len] = '\0';
4059 rl->u.dbrec.size = oe->u.sutrs->len;
4061 case Z_External_grs1:
4062 logf (LOG_DEBUG, "Z_External_grs1");
4064 ir_tcl_grs_mk (oe->u.grs1, &rl->u.dbrec.u.grs1);
4066 case Z_External_explainRecord:
4067 logf (LOG_DEBUG, "Z_External_explainRecord");
4068 if ((rl->u.dbrec.buf = ir_tcl_malloc (rl->u.dbrec.size)))
4070 memcpy (rl->u.dbrec.buf, oe->u.octet_aligned->buf,
4078 if (oe->which == Z_External_octet && rl->u.dbrec.size > 0)
4080 char *buf = (char*) oe->u.octet_aligned->buf;
4081 if ((rl->u.dbrec.buf = ir_tcl_malloc (rl->u.dbrec.size+1)))
4083 memcpy (rl->u.dbrec.buf, buf, rl->u.dbrec.size);
4084 rl->u.dbrec.buf[rl->u.dbrec.size] = '\0';
4087 else if (rl->u.dbrec.type == VAL_SUTRS &&
4088 oe->which == Z_External_sutrs)
4090 if ((rl->u.dbrec.buf = ir_tcl_malloc (oe->u.sutrs->len+1)))
4092 memcpy (rl->u.dbrec.buf, oe->u.sutrs->buf,
4094 rl->u.dbrec.buf[oe->u.sutrs->len] = '\0';
4096 rl->u.dbrec.size = oe->u.sutrs->len;
4098 else if (rl->u.dbrec.type == VAL_GRS1 &&
4099 oe->which == Z_External_grs1)
4101 ir_tcl_grs_mk (oe->u.grs1, &rl->u.dbrec.u.grs1);
4106 static void ir_handleZRecords (void *o, Z_Records *zrs, IrTcl_SetObj *setobj,
4107 const char *elements)
4112 IrTcl_RecordList *rl;
4114 setobj->which = zrs->which;
4115 setobj->recordFlag = 1;
4117 ir_deleteDiags (&setobj->nonSurrogateDiagnosticList,
4118 &setobj->nonSurrogateDiagnosticNum);
4119 if (zrs->which == Z_Records_DBOSD)
4121 int num_rec = zrs->u.databaseOrSurDiagnostics->num_records;
4123 if (num_rec != setobj->numberOfRecordsReturned)
4125 logf (LOG_WARN, "numberOfRecordsReturned=%d but num records=%d",
4126 setobj->numberOfRecordsReturned, num_rec);
4127 setobj->numberOfRecordsReturned = num_rec;
4130 for (offset = 0; offset < num_rec; offset++)
4132 Z_NamePlusRecord *znpr = zrs->u.databaseOrSurDiagnostics->
4135 rl = new_IR_record (setobj, setobj->start + offset, znpr->which,
4137 if (rl->which == Z_NamePlusRecord_surrogateDiagnostic)
4138 ir_handleDiags (&rl->u.surrogateDiagnostics.list,
4139 &rl->u.surrogateDiagnostics.num,
4140 &znpr->u.surrogateDiagnostic,
4143 ir_handleDBRecord (p, rl,
4144 (Z_External*) (znpr->u.databaseRecord));
4147 else if (zrs->which == Z_Records_multipleNSD)
4149 logf (LOG_DEBUG, "multipleNonSurrogateDiagnostic %d",
4150 zrs->u.multipleNonSurDiagnostics->num_diagRecs);
4151 setobj->numberOfRecordsReturned = 0;
4152 ir_handleDiags (&setobj->nonSurrogateDiagnosticList,
4153 &setobj->nonSurrogateDiagnosticNum,
4154 zrs->u.multipleNonSurDiagnostics->diagRecs,
4155 zrs->u.multipleNonSurDiagnostics->num_diagRecs);
4160 Z_DiagRec dr, *dr_p = &dr;
4161 dr.which = Z_DiagRec_defaultFormat;
4162 dr.u.defaultFormat = zrs->u.nonSurrogateDiagnostic;
4164 Z_DiagRec *dr_p = zrs->u.nonSurrogateDiagnostic;
4166 logf (LOG_DEBUG, "NonSurrogateDiagnostic");
4168 setobj->numberOfRecordsReturned = 0;
4169 ir_handleDiags (&setobj->nonSurrogateDiagnosticList,
4170 &setobj->nonSurrogateDiagnosticNum,
4175 static char *set_queryExpression (Z_QueryExpression *qe)
4180 if (qe->which == Z_QueryExpression_term)
4182 if (qe->u.term->queryTerm)
4184 Z_Term *term = qe->u.term->queryTerm;
4185 if (term->which == Z_Term_general)
4187 termz = xmalloc (term->u.general->len+1);
4188 memcpy (termz, term->u.general->buf, term->u.general->len);
4189 termz[term->u.general->len] = 0;
4196 static void set_searchResult (Z_OtherInformation *o,
4197 IrTcl_SetObj *setobj)
4202 for (i = 0; i < o->num_elements; i++)
4204 if (o->list[i]->which == Z_OtherInfo_externallyDefinedInfo)
4206 ODR odr = odr_createmem (ODR_DECODE);
4207 Z_External *ext = o->list[i]->information.externallyDefinedInfo;
4208 Z_SearchInfoReport *sr = 0;
4210 if (ext->which == Z_External_single)
4212 odr_setbuf (odr, ext->u.single_ASN1_type->buf,
4213 ext->u.single_ASN1_type->len, 0);
4214 z_SearchInfoReport (odr, &sr, 0, "searchInfo");
4216 if (ext->which == Z_External_searchResult1)
4217 sr = ext->u.searchResult1;
4221 reset_searchResult (setobj);
4223 setobj->searchResult_num = sr->num;
4224 setobj->searchResult_terms =
4225 xmalloc (sr->num * sizeof(*setobj->searchResult_terms));
4226 setobj->searchResult_count =
4227 xmalloc (sr->num * sizeof(*setobj->searchResult_count));
4229 for (j = 0; j < sr->num; j++)
4231 setobj->searchResult_terms[j] =
4232 set_queryExpression (
4233 sr->elements[j]->subqueryExpression);
4234 if (sr->elements[j]->subqueryCount)
4235 setobj->searchResult_count[j] =
4236 *sr->elements[j]->subqueryCount;
4238 setobj->searchResult_count[j] = 0;
4246 static void ir_searchResponse (void *o, Z_SearchResponse *searchrs,
4247 IrTcl_SetObj *setobj)
4249 Z_Records *zrs = searchrs->records;
4251 logf (LOG_DEBUG, "Received search response");
4254 logf (LOG_DEBUG, "Search response, no object!");
4257 setobj->searchStatus = *searchrs->searchStatus;
4258 get_referenceId (&setobj->set_inher.referenceId, searchrs->referenceId);
4259 setobj->resultCount = *searchrs->resultCount;
4260 if (searchrs->presentStatus)
4261 setobj->presentStatus = *searchrs->presentStatus;
4263 setobj->presentStatus = Z_RES_NONE;
4264 if (searchrs->nextResultSetPosition)
4265 setobj->nextResultSetPosition = *searchrs->nextResultSetPosition;
4267 logf (LOG_DEBUG, "status %d hits %d",
4268 setobj->searchStatus, setobj->resultCount);
4269 set_searchResult (searchrs->additionalSearchInfo, setobj);
4273 if (setobj->resultCount <= setobj->set_inher.smallSetUpperBound)
4274 es = setobj->set_inher.smallSetElementSetNames;
4276 es = setobj->set_inher.mediumSetElementSetNames;
4277 setobj->numberOfRecordsReturned = *searchrs->numberOfRecordsReturned;
4278 ir_handleZRecords (o, zrs, setobj, es);
4282 setobj->numberOfRecordsReturned = 0;
4283 setobj->recordFlag = 0;
4288 static void ir_presentResponse (void *o, Z_PresentResponse *presrs,
4289 IrTcl_SetObj *setobj)
4291 Z_Records *zrs = presrs->records;
4293 logf (LOG_DEBUG, "Received present response");
4296 logf (LOG_DEBUG, "Present response, no object!");
4299 setobj->presentStatus = *presrs->presentStatus;
4300 get_referenceId (&setobj->set_inher.referenceId, presrs->referenceId);
4301 setobj->nextResultSetPosition = *presrs->nextResultSetPosition;
4304 setobj->numberOfRecordsReturned = *presrs->numberOfRecordsReturned;
4305 ir_handleZRecords (o, zrs, setobj, setobj->set_inher.elementSetNames);
4309 setobj->numberOfRecordsReturned = 0;
4310 setobj->recordFlag = 0;
4311 logf (LOG_DEBUG, "No records!");
4315 static void ir_scanResponse (void *o, Z_ScanResponse *scanrs,
4316 IrTcl_ScanObj *scanobj)
4320 logf (LOG_DEBUG, "Received scanResponse");
4322 get_referenceId (&p->set_inher.referenceId, scanrs->referenceId);
4323 scanobj->scanStatus = *scanrs->scanStatus;
4324 logf (LOG_DEBUG, "scanStatus=%d", scanobj->scanStatus);
4326 if (scanrs->stepSize)
4327 scanobj->stepSize = *scanrs->stepSize;
4328 logf (LOG_DEBUG, "stepSize=%d", scanobj->stepSize);
4330 scanobj->numberOfEntriesReturned = *scanrs->numberOfEntriesReturned;
4331 logf (LOG_DEBUG, "numberOfEntriesReturned=%d",
4332 scanobj->numberOfEntriesReturned);
4334 if (scanrs->positionOfTerm)
4335 scanobj->positionOfTerm = *scanrs->positionOfTerm;
4337 scanobj->positionOfTerm = -1;
4338 logf (LOG_DEBUG, "positionOfTerm=%d", scanobj->positionOfTerm);
4340 xfree (scanobj->entries);
4341 scanobj->entries = NULL;
4342 scanobj->num_entries = 0;
4343 scanobj->entries_flag = 0;
4345 ir_deleteDiags (&scanobj->nonSurrogateDiagnosticList,
4346 &scanobj->nonSurrogateDiagnosticNum);
4347 if (scanrs->entries)
4352 scanobj->entries_flag = 1;
4353 if (scanrs->entries)
4355 scanobj->num_entries = scanrs->entries->num_entries;
4356 scanobj->entries = ir_tcl_malloc (scanobj->num_entries *
4357 sizeof(*scanobj->entries));
4358 ze = scanrs->entries->entries;
4360 for (i=0; i<scanobj->num_entries; i++, ze++)
4362 scanobj->entries[i].which = (*ze)->which;
4363 switch ((*ze)->which)
4365 case Z_Entry_termInfo:
4366 if ((*ze)->u.termInfo->term->which == Z_Term_general)
4368 int l = (*ze)->u.termInfo->term->u.general->len;
4369 scanobj->entries[i].u.term.buf = ir_tcl_malloc (1+l);
4370 memcpy (scanobj->entries[i].u.term.buf,
4371 (*ze)->u.termInfo->term->u.general->buf,
4373 scanobj->entries[i].u.term.buf[l] = '\0';
4376 scanobj->entries[i].u.term.buf = NULL;
4377 if ((*ze)->u.termInfo->globalOccurrences)
4378 scanobj->entries[i].u.term.globalOccurrences =
4379 *(*ze)->u.termInfo->globalOccurrences;
4381 scanobj->entries[i].u.term.globalOccurrences = 0;
4383 case Z_Entry_surrogateDiagnostic:
4384 ir_handleDiags (&scanobj->entries[i].u.diag.list,
4385 &scanobj->entries[i].u.diag.num,
4386 &(*ze)->u.surrogateDiagnostic,
4391 if (scanrs->entries->nonsurrogateDiagnostics)
4392 ir_handleDiags (&scanobj->nonSurrogateDiagnosticList,
4393 &scanobj->nonSurrogateDiagnosticNum,
4394 scanrs->entries->nonsurrogateDiagnostics,
4395 scanrs->entries->num_nonsurrogateDiagnostics);
4400 static void ir_sortResponse (void *o, Z_SortResponse *sortrs,
4401 IrTcl_SetObj *setobj)
4405 logf (LOG_DEBUG, "Received sortResponse");
4410 purge_IR_records (setobj);
4412 get_referenceId (&p->set_inher.referenceId, sortrs->referenceId);
4414 setobj->sortStatus = *sortrs->sortStatus;
4416 ir_deleteDiags (&setobj->nonSurrogateDiagnosticList,
4417 &setobj->nonSurrogateDiagnosticNum);
4419 if (sortrs->diagnostics)
4420 ir_handleDiags (&setobj->nonSurrogateDiagnosticList,
4421 &setobj->nonSurrogateDiagnosticNum,
4422 sortrs->diagnostics,
4423 sortrs->num_diagnostics);
4425 if (sortrs->diagnostics)
4426 ir_handleDiags (&setobj->nonSurrogateDiagnosticList,
4427 &setobj->nonSurrogateDiagnosticNum,
4428 sortrs->diagnostics->diagRecs,
4429 sortrs->diagnostics->num_diagRecs);
4434 * ir_select_read: handle incoming packages
4436 static void ir_select_read (ClientData clientData)
4438 IrTcl_Obj *p = (IrTcl_Obj *) clientData;
4443 Tcl_CmdInfo cmd_info;
4444 const char *apdu_call;
4447 logf(LOG_DEBUG, "Read handler fd=%d", cs_fileno(p->cs_link));
4448 if (p->state == IR_TCL_R_Connecting)
4450 logf(LOG_DEBUG, "read: connect");
4451 r = cs_rcvconnect (p->cs_link);
4454 logf (LOG_WARN, "cs_rcvconnect returned 1");
4457 p->state = IR_TCL_R_Idle;
4459 ir_select_remove_write (cs_fileno (p->cs_link), p);
4462 logf (LOG_DEBUG, "cs_rcvconnect error");
4463 ir_tcl_disconnect (p);
4466 p->failInfo = IR_TCL_FAIL_CONNECT;
4467 ir_tcl_eval (p->interp, p->failback);
4469 ir_obj_delete ((ClientData) p);
4473 ir_tcl_eval (p->interp, p->callback);
4474 if (p->ref_count == 2 && p->cs_link && p->request_queue
4475 && p->state == IR_TCL_R_Idle)
4476 ir_tcl_send_q (p, p->request_queue, "x");
4477 ir_obj_delete ((ClientData) p);
4482 p->state = IR_TCL_R_Reading;
4485 yaz_log(LOG_DEBUG, "round %d", round);
4486 /* read incoming APDU */
4487 if ((r=cs_get (p->cs_link, &p->buf_in, &p->len_in)) == 1)
4489 logf(LOG_DEBUG, "PDU Fraction read");
4492 /* signal one more use of ir object - callbacks must not
4493 release the ir memory (p pointer) */
4497 logf (LOG_DEBUG, "cs_get failed, code %d", r);
4498 ir_tcl_disconnect (p);
4501 p->failInfo = IR_TCL_FAIL_READ;
4502 ir_tcl_eval (p->interp, p->failback);
4504 /* release ir object now if callback deleted it */
4505 ir_obj_delete ((ClientData) p);
4508 /* got complete APDU. Now decode */
4511 odr_setbuf (p->odr_in, p->buf_in, r, 0);
4512 logf (LOG_DEBUG, "cs_get ok, total size %d", r);
4513 if (!z_APDU (p->odr_in, &apdu, 0, 0))
4515 logf (LOG_DEBUG, "cs_get failed: %s",
4516 odr_errmsg (odr_geterror (p->odr_in)));
4517 ir_tcl_disconnect (p);
4520 p->failInfo = IR_TCL_FAIL_IN_APDU;
4521 p->apduOffset = odr_offset (p->odr_in);
4522 ir_tcl_eval (p->interp, p->failback);
4524 /* release ir object now if failback deleted it */
4525 ir_obj_delete ((ClientData) p);
4529 z_APDU(p->odr_pr, &apdu, 0, 0);
4530 /* handle APDU and invoke callback */
4531 rq = p->request_queue;
4534 /* no corresponding request. Skip it. */
4535 logf(LOG_DEBUG, "no corresponding request. Skipping it");
4536 p->state = IR_TCL_R_Idle;
4539 object_name = rq->object_name;
4540 logf (LOG_DEBUG, "Object %s", object_name);
4542 if (Tcl_GetCommandInfo (p->interp, object_name, &cmd_info))
4546 case Z_APDU_initResponse:
4547 p->eventType = "init";
4548 ir_initResponse (p, apdu->u.initResponse);
4549 apdu_call = p->initResponse;
4551 case Z_APDU_searchResponse:
4552 p->eventType = "search";
4553 ir_searchResponse (p, apdu->u.searchResponse,
4554 (IrTcl_SetObj *) cmd_info.clientData);
4555 apdu_call = ((IrTcl_SetObj *)
4556 cmd_info.clientData)->searchResponse;
4558 case Z_APDU_presentResponse:
4559 p->eventType = "present";
4560 ir_presentResponse (p, apdu->u.presentResponse,
4561 (IrTcl_SetObj *) cmd_info.clientData);
4562 apdu_call = ((IrTcl_SetObj *)
4563 cmd_info.clientData)->presentResponse;
4565 case Z_APDU_scanResponse:
4566 p->eventType = "scan";
4567 ir_scanResponse (p, apdu->u.scanResponse,
4568 (IrTcl_ScanObj *) cmd_info.clientData);
4569 apdu_call = ((IrTcl_ScanObj *)
4570 cmd_info.clientData)->scanResponse;
4572 case Z_APDU_sortResponse:
4573 p->eventType = "sort";
4574 ir_sortResponse (p, apdu->u.sortResponse,
4575 (IrTcl_SetObj *) cmd_info.clientData);
4576 apdu_call = ((IrTcl_SetObj *)
4577 cmd_info.clientData)->sortResponse;
4580 logf (LOG_WARN, "Received unknown APDU type (%d)",
4582 ir_tcl_disconnect (p);
4585 p->failInfo = IR_TCL_FAIL_UNKNOWN_APDU;
4586 ir_tcl_eval (p->interp, p->failback);
4591 p->request_queue = rq->next;
4592 p->state = IR_TCL_R_Idle;
4595 ir_tcl_eval (p->interp, apdu_call);
4596 else if (rq->callback)
4597 ir_tcl_eval (p->interp, rq->callback);
4598 xfree (rq->buf_out);
4599 xfree (rq->callback);
4600 xfree (rq->object_name);
4602 odr_reset (p->odr_in);
4603 if (p->ref_count == 1)
4605 ir_obj_delete ((ClientData) p);
4608 ir_obj_delete ((ClientData) p);
4609 } while (p->cs_link && cs_more (p->cs_link));
4610 if (p->cs_link && p->request_queue && p->state == IR_TCL_R_Idle)
4611 ir_tcl_send_q (p, p->request_queue, "x");
4615 * ir_select_write: handle outgoing packages - not yet written.
4617 static int ir_select_write (ClientData clientData)
4619 IrTcl_Obj *p = (IrTcl_Obj *) clientData;
4623 logf (LOG_DEBUG, "Write handler fd=%d", cs_fileno(p->cs_link));
4624 if (p->state == IR_TCL_R_Connecting)
4626 logf(LOG_DEBUG, "write: connect");
4627 r = cs_rcvconnect (p->cs_link);
4630 logf (LOG_DEBUG, "cs_rcvconnect returned 1");
4633 p->state = IR_TCL_R_Idle;
4635 ir_select_remove_write (cs_fileno (p->cs_link), p);
4638 logf (LOG_DEBUG, "cs_rcvconnect error");
4639 ir_tcl_disconnect (p);
4642 p->failInfo = IR_TCL_FAIL_CONNECT;
4643 ir_tcl_eval (p->interp, p->failback);
4645 ir_obj_delete ((ClientData) p);
4649 ir_tcl_eval (p->interp, p->callback);
4650 ir_obj_delete ((ClientData) p);
4653 rq = p->request_queue;
4654 if (!rq || !rq->buf_out)
4657 if ((r=cs_put (p->cs_link, rq->buf_out, rq->len_out)) < 0)
4659 logf (LOG_DEBUG, "cs_put write fail");
4661 xfree (rq->buf_out);
4663 ir_tcl_disconnect (p);
4666 p->failInfo = IR_TCL_FAIL_WRITE;
4667 ir_tcl_eval (p->interp, p->failback);
4669 ir_obj_delete ((ClientData) p);
4671 else if (r == 0) /* remove select bit */
4673 logf (LOG_DEBUG, "Write completed");
4674 p->state = IR_TCL_R_Waiting;
4675 ir_select_remove_write (cs_fileno (p->cs_link), p);
4676 xfree (rq->buf_out);
4682 static void ir_select_notify (ClientData clientData, int r, int w, int e)
4686 if (!ir_select_write (clientData) && r)
4687 ir_select_read (clientData);
4691 ir_select_read (clientData);
4695 /*----------------------------------------------------------- */
4699 * This wrapper function is used by Windows to invoke the
4700 * initialization code for the DLL. If we are compiling
4701 * with Visual C++, this routine will be renamed to DllMain.
4713 DllEntryPoint(hInst, reason, reserved)
4714 HINSTANCE hInst; /* Library instance handle. */
4715 DWORD reason; /* Reason this function is being called. */
4716 LPVOID reserved; /* Not used. */
4722 /* ------------------------------------------------------- */
4724 * Irtcl_init: Registration of TCL commands.
4726 EXPORT (int,Irtcl_Init) (Tcl_Interp *interp)
4729 if (Tcl_InitStubs(interp, "8.1", 0) == NULL) {
4733 Tcl_CreateCommand (interp, "ir", ir_obj_mk, (ClientData) NULL,
4734 (Tcl_CmdDeleteProc *) NULL);
4735 Tcl_CreateCommand (interp, "ir-set", ir_set_obj_mk,
4736 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
4737 Tcl_CreateCommand (interp, "ir-scan", ir_scan_obj_mk,
4738 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
4739 Tcl_CreateCommand (interp, "ir-log-init", ir_log_init_proc,
4740 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
4741 Tcl_CreateCommand (interp, "ir-log", ir_log_proc,
4742 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
4743 Tcl_CreateCommand (interp, "ir-version", ir_version, (ClientData) NULL,
4744 (Tcl_CmdDeleteProc *) NULL);