2 * IR toolkit for tcl/tk
3 * (c) Index Data 1995-2003
4 * See the file LICENSE for details.
7 * Revision 1.124 2003-03-05 22:02:47 adam
10 * Revision 1.123 2003/03/05 21:21:41 adam
11 * APDU log. default largeSetLowerBound changed from 2 to 1
13 * Revision 1.122 2003/03/05 18:02:08 adam
14 * Fix bug with idAuthentication that didn't work for empty group.
16 * Revision 1.121 2003/01/30 13:27:07 adam
17 * Changed version to 1.4.1. Added WIN32 version resource.
18 * IrTcl ignores unexpected PDU's, rather than die.
20 * Revision 1.120 2002/03/20 14:48:54 adam
21 * implemented USR.1 SearchResult-1
23 * Revision 1.119 2001/12/03 00:31:06 adam
24 * Towards 1.4. Configure updates.
26 * Revision 1.118 2001/03/27 16:27:21 adam
27 * Fixed bug in do_responseStatus.
29 * Revision 1.117 2001/03/26 11:39:34 adam
30 * Fixed bug in ir_deleteDiags - crash when receiving multiple diags.
32 * Revision 1.116 2001/02/09 11:58:04 adam
33 * Updated for Tcl8.1 and higher where internal encoding is UTF-8.
35 * Revision 1.115 2000/09/13 12:18:49 adam
36 * Logging utility patch (YAZ version 1.7).
38 * Revision 1.114 1999/05/17 20:37:41 adam
39 * Fixed problem with ASN code.
41 * Revision 1.113 1999/04/20 10:01:46 adam
42 * Modified calls to ODR encoders/decoders (name argument).
44 * Revision 1.112 1999/03/22 06:51:34 adam
47 * Revision 1.111 1999/02/11 11:30:09 adam
50 * Revision 1.110 1998/10/20 15:15:31 adam
51 * Changed scan response handler.
53 * Revision 1.109 1998/10/13 21:23:26 adam
54 * Fixed searchStatus method.
56 * Revision 1.108 1998/10/12 11:48:08 adam
57 * Removed printf call.
59 * Revision 1.107 1998/06/10 13:00:46 adam
60 * Added ir-version command.
62 * Revision 1.106 1998/05/20 12:25:35 adam
63 * Fixed bug that occurred in rare cases when encoding of incoming
66 * Revision 1.105 1998/04/02 14:31:08 adam
67 * This version works with compiled ASN.1 code.
69 * Revision 1.104 1998/02/27 14:26:07 adam
70 * Changed client so that it still works if target sets numberOfRecords
71 * in response to an illegal value.
73 * Revision 1.103 1997/11/19 11:22:10 adam
74 * Object identifiers can be accessed in GRS-1 records.
76 * Revision 1.102 1997/09/17 12:22:40 adam
77 * Changed to use YAZ version 1.4. The new comstack utility, cs_straddr,
80 * Revision 1.101 1997/09/09 10:19:53 adam
81 * New MSV5.0 port with fewer warnings.
83 * Revision 1.100 1997/05/01 15:04:05 adam
84 * Added ir-log command.
86 * Revision 1.99 1997/04/30 07:24:47 adam
87 * Spell fix of an error message.
89 * Revision 1.98 1997/04/13 18:57:20 adam
90 * Better error reporting and aligned with Tcl/Tk style.
91 * Rework of notifier code with Tcl_File handles.
93 * Revision 1.97 1996/11/14 17:11:07 adam
94 * Added Explain documentaion.
96 * Revision 1.96 1996/10/08 13:02:50 adam
97 * When dealing with records, odr_choice_enable_bias function is used to
98 * prevent decoding of externals.
100 * Revision 1.95 1996/09/13 10:51:49 adam
101 * Bug fix: ir_tcl_select_set called Tcl_GetFile at disconnect.
103 * Revision 1.94 1996/08/21 13:32:53 adam
104 * Implemented saveFile method and extended loadFile method to work with it.
106 * Revision 1.93 1996/08/16 15:07:45 adam
107 * First work on Explain.
109 * Revision 1.92 1996/08/09 15:33:07 adam
110 * Modified the code to use tk4.1/tcl7.5 patch level 1. The time-driven
111 * polling is no longer activated on Windows since asynchrounous I/O works
114 * Revision 1.91 1996/07/03 13:31:11 adam
115 * The xmalloc/xfree functions from YAZ are used to manage memory.
117 * Revision 1.90 1996/06/27 14:21:00 adam
118 * Yet another Windows port.
120 * Revision 1.89 1996/06/11 15:27:15 adam
121 * Event type set to connect a little earlier in the do_connect function.
123 * Revision 1.88 1996/06/03 09:04:22 adam
124 * Changed a few logf calls.
126 * Revision 1.87 1996/05/29 06:37:51 adam
127 * Function ir_tcl_get_grs_r enhanced so that specific elements can be
130 * Revision 1.86 1996/03/20 13:54:04 adam
131 * The Tcl_File structure is only manipulated in the Tk-event interface
134 * Revision 1.85 1996/03/15 11:15:48 adam
135 * Modified to use new prototypes for p_query_rpn and p_query_scan.
137 * Revision 1.84 1996/03/07 12:42:49 adam
138 * Better logging when callback is invoked.
140 * Revision 1.83 1996/03/05 09:21:09 adam
141 * Bug fix: memory used by GRS records wasn't freed.
142 * Rewrote some of the error handling code - the connection is always
143 * closed before failback is called.
144 * If failback is defined the send APDU methods (init, search, ...) will
145 * return OK but invoke failback (as is the case if the write operation
147 * Bug fix: ref_count in assoc object could grow if fraction of PDU was
150 * Revision 1.82 1996/02/29 15:30:21 adam
151 * Export of IrTcl functionality to extensions.
153 * Revision 1.81 1996/02/26 18:38:32 adam
154 * Work on export of set methods.
156 * Revision 1.80 1996/02/23 17:31:39 adam
157 * More functions made available to the wais tcl extension.
159 * Revision 1.79 1996/02/23 13:41:38 adam
160 * Work on public access to simple ir class system.
162 * Revision 1.78 1996/02/21 10:16:08 adam
163 * Simplified select handling. Only one function ir_tcl_select_set has
164 * to be externally defined.
166 * Revision 1.77 1996/02/20 17:52:58 adam
167 * Uses the YAZ oid system to name record syntax object identifiers.
169 * Revision 1.76 1996/02/20 16:09:51 adam
170 * Bug fix: didn't set element set names stamp correctly on result
171 * set records when element set names were set to the empty string.
173 * Revision 1.75 1996/02/19 15:41:53 adam
174 * Better log messages.
175 * Minor improvement of connect method.
177 * Revision 1.74 1996/02/05 17:58:03 adam
178 * Ported ir-tcl to use the beta releases of tcl7.5/tk4.1.
180 * Revision 1.73 1996/01/29 11:35:19 adam
181 * Bug fix: cs_type member renamed to comstackType to avoid conflict with
182 * cs_type macro defined by YAZ.
184 * Revision 1.72 1996/01/19 17:45:34 quinn
185 * Added debugging output
187 * Revision 1.71 1996/01/19 16:22:38 adam
188 * New method: apduDump - returns information about last incoming APDU.
190 * Revision 1.70 1996/01/10 09:18:34 adam
191 * PDU specific callbacks implemented: initRespnse, searchResponse,
192 * presentResponse and scanResponse.
193 * Bug fix in the command line shell (tclmain.c) - discovered on OSF/1.
195 * Revision 1.69 1996/01/04 16:12:12 adam
196 * Setting PDUType renamed to eventType.
198 * Revision 1.68 1996/01/04 11:05:22 adam
199 * New setting: PDUType - returns type of last PDU returned from the target.
200 * Fixed a bug in configure/Makefile.
202 * Revision 1.67 1996/01/03 09:00:51 adam
203 * Updated to use new version of Yaz (names changed to avoid C++ conflict).
205 * Revision 1.66 1995/11/28 17:26:39 adam
206 * Removed Carriage return from ir-tcl.c!
207 * Removed misc. debug logs.
209 * Revision 1.65 1995/11/28 13:53:00 quinn
212 * Revision 1.64 1995/11/13 15:39:18 adam
213 * Bug fix: {small,medium}SetElementSetNames weren't set correctly.
214 * Bug fix: idAuthentication weren't set correctly.
216 * Revision 1.63 1995/11/13 09:55:39 adam
217 * Multiple records at a position in a result-set with differnt
220 * Revision 1.62 1995/10/18 17:20:33 adam
221 * Work on target setup in client.tcl.
223 * Revision 1.61 1995/10/18 16:42:42 adam
224 * New settings: smallSetElementSetNames and mediumSetElementSetNames.
226 * Revision 1.60 1995/10/18 15:43:31 adam
227 * In search: mediumSetElementSetNames and smallSetElementSetNames are
228 * set to elementSetNames.
230 * Revision 1.59 1995/10/17 12:18:58 adam
231 * Bug fix: when target connection closed, the connection was not
232 * properly reestablished.
234 * Revision 1.58 1995/10/16 17:00:55 adam
235 * New setting: elementSetNames.
236 * Various client improvements. Medium presentation format looks better.
238 * Revision 1.57 1995/09/21 13:11:51 adam
239 * Support of dynamic loading.
240 * Test script uses load command if necessary.
242 * Revision 1.56 1995/08/29 15:30:14 adam
243 * Work on GRS records.
245 * Revision 1.55 1995/08/28 09:43:25 adam
246 * Minor changes. configure only searches for yaz beta 3 and versions after
249 * Revision 1.54 1995/08/24 12:25:16 adam
250 * Modified to work with yaz 1.0b3.
252 * Revision 1.53 1995/08/04 12:49:26 adam
253 * Bug fix: reading uninitialized variable p.
255 * Revision 1.52 1995/08/04 11:32:38 adam
256 * More work on output queue. Memory related routines moved
259 * Revision 1.51 1995/08/03 13:22:54 adam
262 * Revision 1.50 1995/07/20 08:09:49 adam
263 * client.tcl: Targets removed from hotTargets list when targets
264 * are removed/modified.
265 * ir-tcl.c: More work on triggerResourceControl.
267 * Revision 1.49 1995/06/30 12:39:21 adam
268 * Bug fix: loadFile didn't set record type.
269 * The MARC routines are a little less strict in the interpretation.
270 * Script display.tcl replaces the old marc.tcl.
271 * New interactive script: shell.tcl.
273 * Revision 1.48 1995/06/27 19:03:50 adam
274 * Bug fix in do_present in ir-tcl.c: p->set_child member weren't set.
275 * nextResultSetPosition used instead of setOffset.
277 * Revision 1.47 1995/06/25 10:25:04 adam
278 * Working on triggerResourceControl. Description of compile/install
279 * procedure moved to ir-tcl.sgml.
281 * Revision 1.46 1995/06/22 13:15:06 adam
282 * Feature: SUTRS. Setting getSutrs implemented.
283 * Work on display formats.
284 * Preferred record syntax can be set by the user.
286 * Revision 1.45 1995/06/20 08:07:30 adam
287 * New setting: failInfo.
288 * Working on better cancel mechanism.
290 * Revision 1.44 1995/06/19 17:01:20 adam
293 * Revision 1.43 1995/06/19 13:06:08 adam
294 * New define: IR_TCL_VERSION.
296 * Revision 1.42 1995/06/19 08:08:52 adam
297 * client.tcl: hotTargets now contain both database and target name.
298 * ir-tcl.c: setting protocol edited. Errors in callbacks are logged
299 * by logf(LOG_WARN, ...) calls.
301 * Revision 1.41 1995/06/16 12:28:16 adam
302 * Implemented preferredRecordSyntax.
303 * Minor changes in diagnostic handling.
304 * Record list deleted when connection closes.
306 * Revision 1.40 1995/06/14 13:37:18 adam
307 * Setting recordType implemented.
308 * Setting implementationVersion implemented.
309 * Settings implementationId / implementationName edited.
311 * Revision 1.39 1995/06/08 10:26:32 adam
312 * Bug fix in ir_strdup.
314 * Revision 1.38 1995/06/01 16:36:47 adam
315 * About buttons. Minor bug fixes.
317 * Revision 1.37 1995/06/01 07:31:20 adam
318 * Rename of many typedefs -> IrTcl_...
320 * Revision 1.36 1995/05/31 13:09:59 adam
321 * Client searches/presents may be interrupted.
322 * New moving book-logo.
324 * Revision 1.35 1995/05/31 08:36:33 adam
325 * Bug fix in client.tcl: didn't save options on clientrc.tcl.
326 * New method: referenceId. More work on scan.
328 * Revision 1.34 1995/05/29 10:33:42 adam
329 * README and rename of startup script.
331 * Revision 1.33 1995/05/29 09:15:11 quinn
332 * Changed CS_SR to PROTO_SR, etc.
334 * Revision 1.32 1995/05/29 08:44:16 adam
335 * Work on delete of objects.
337 * Revision 1.31 1995/05/26 11:44:10 adam
338 * Bugs fixed. More work on MARC utilities and queries. Test
339 * client is up-to-date again.
341 * Revision 1.30 1995/05/26 08:54:11 adam
342 * New MARC utilities. Uses prefix query.
344 * Revision 1.29 1995/05/24 14:10:22 adam
345 * Work on idAuthentication, protocolVersion and options.
347 * Revision 1.28 1995/05/23 15:34:48 adam
348 * Many new settings, userInformationField, smallSetUpperBound, etc.
349 * A number of settings are inherited when ir-set is executed.
350 * This version is incompatible with the graphical test client (client.tcl).
352 * Revision 1.27 1995/05/11 15:34:47 adam
353 * Scan request changed a bit. This version works with RLG.
355 * Revision 1.26 1995/04/18 16:11:51 adam
356 * First version of graphical Scan. Some work on query-by-form.
358 * Revision 1.25 1995/04/17 09:37:17 adam
359 * Further development of scan.
361 * Revision 1.24 1995/04/11 14:16:42 adam
362 * Further work on scan. Response works. Entries aren't saved yet.
364 * Revision 1.23 1995/04/10 10:50:27 adam
365 * Result-set name defaults to suffix of ir-set name.
366 * Started working on scan. Not finished at this point.
368 * Revision 1.22 1995/03/31 10:43:03 adam
369 * More robust when getting bad MARC records.
371 * Revision 1.21 1995/03/31 08:56:37 adam
372 * New button "Search".
374 * Revision 1.20 1995/03/29 16:07:09 adam
375 * Bug fix: Didn't use setName in present request.
377 * Revision 1.19 1995/03/28 12:45:23 adam
378 * New ir method failback: called on disconnect/protocol error.
379 * New ir set/get method: protocol: SR / Z3950.
380 * Simple popup and disconnect when failback is invoked.
382 * Revision 1.18 1995/03/21 15:50:12 adam
385 * Revision 1.17 1995/03/21 13:41:03 adam
386 * Comstack cs_create not used too often. Non-blocking connect.
388 * Revision 1.16 1995/03/21 08:26:06 adam
389 * New method, setName, to specify the result set name (other than Default).
390 * New method, responseStatus, which returns diagnostic info, if any, after
391 * present response / search response.
393 * Revision 1.15 1995/03/20 15:24:07 adam
394 * Diagnostic records saved on searchResponse.
396 * Revision 1.14 1995/03/20 08:53:22 adam
397 * Event loop in tclmain.c rewritten. New method searchStatus.
399 * Revision 1.13 1995/03/17 18:26:17 adam
400 * Non-blocking i/o used now. Database names popup as cascade items.
402 * Revision 1.12 1995/03/17 15:45:00 adam
403 * Improved target/database setup.
405 * Revision 1.11 1995/03/16 17:54:03 adam
406 * Minor changes really.
408 * Revision 1.10 1995/03/15 16:14:50 adam
409 * Blocking arg in cs_create changed.
411 * Revision 1.9 1995/03/15 13:59:24 adam
414 * Revision 1.8 1995/03/15 08:25:16 adam
415 * New method presentStatus to check for error on present. Misc. cleanup
416 * of IrTcl_RecordList manipulations. Full MARC record presentation in
419 * Revision 1.7 1995/03/14 17:32:29 adam
420 * Presentation of full Marc record in popup window.
422 * Revision 1.6 1995/03/12 19:31:55 adam
423 * Pattern matching implemented when retrieving MARC records. More
424 * diagnostic functions.
426 * Revision 1.5 1995/03/10 18:00:15 adam
427 * Actual presentation in line-by-line format. RPN query support.
429 * Revision 1.4 1995/03/09 16:15:08 adam
430 * First presentRequest attempts. Hot-target list.
448 #if defined(__WIN32__)
449 # define WIN32_LEAN_AND_MEAN
450 # include <windows.h>
451 # undef WIN32_LEAN_AND_MEAN
454 * VC++ has an alternate entry point called DllMain, so we need to rename
458 # if defined(_MSC_VER)
459 # define EXPORT(a,b) __declspec(dllexport) a b
460 # define DllEntryPoint DllMain
462 # if defined(__BORLANDC__)
463 # define EXPORT(a,b) a _export b
465 # define EXPORT(a,b) a b
469 # define EXPORT(a,b) a b
472 static char *wrongArgs = "wrong # args: should be \"";
473 static FILE *odr_print_file = 0;
475 static int ir_tcl_error_exec (Tcl_Interp *interp, int argc, char **argv)
478 Tcl_AppendResult (interp, " while executing ", NULL);
479 for (i = 0; i<argc; i++)
480 Tcl_AppendResult (interp, (i ? " " : "\""), argv[i], NULL);
481 Tcl_AppendResult (interp, "\"", NULL);
486 static void ir_deleteDiags (IrTcl_Diagnostic **dst_list, int *dst_num);
488 static void ir_select_notify (ClientData clientData, int r, int w, int e);
490 void ir_select_add (int fd, void *obj)
492 ir_tcl_select_set (ir_select_notify, fd, obj, 1, 0, 0);
495 void ir_select_add_write (int fd, void *obj)
497 ir_tcl_select_set (ir_select_notify, fd, obj, 1, 1, 0);
500 void ir_select_remove (int fd, void *obj)
502 ir_tcl_select_set (NULL, fd, obj, 0, 0, 0);
505 void ir_select_remove_write (int fd, void *obj)
507 ir_tcl_select_set (ir_select_notify, fd, obj, 1, 0, 0);
510 static void delete_IR_record (IrTcl_RecordList *rl)
514 case Z_NamePlusRecord_databaseRecord:
515 switch (rl->u.dbrec.type)
518 ir_tcl_grs_del (&rl->u.dbrec.u.grs1);
523 xfree (rl->u.dbrec.buf);
524 rl->u.dbrec.buf = NULL;
526 case Z_NamePlusRecord_surrogateDiagnostic:
527 ir_deleteDiags (&rl->u.surrogateDiagnostics.list,
528 &rl->u.surrogateDiagnostics.num);
531 xfree (rl->elements);
534 static void purge_IR_records (IrTcl_SetObj *setobj)
536 IrTcl_RecordList *rl;
537 while ((rl = setobj->record_list))
539 setobj->record_list = rl->next;
540 delete_IR_record (rl);
545 static IrTcl_RecordList *new_IR_record (IrTcl_SetObj *setobj,
547 const char *elements)
549 IrTcl_RecordList *rl;
551 if (elements && !*elements)
553 for (rl = setobj->record_list; rl; rl = rl->next)
555 if (no == rl->no && (!rl->elements || !elements ||
556 !strcmp(elements, rl->elements)))
558 delete_IR_record (rl);
564 rl = ir_tcl_malloc (sizeof(*rl));
565 rl->next = setobj->record_list;
567 setobj->record_list = rl;
570 ir_tcl_strdup (NULL, &rl->elements, elements);
577 int ir_tcl_eval (Tcl_Interp *interp, const char *command)
579 char *tmp = ir_tcl_malloc (strlen(command)+1);
582 logf (LOG_DEBUG, "Invoking %.23s ...", command);
583 strcpy (tmp, command);
584 r = Tcl_Eval (interp, tmp);
587 const char *errorInfo = Tcl_GetVar (interp, "errorInfo", 0);
588 logf (LOG_WARN, "Tcl error in line %d: %s\n%s", interp->errorLine,
589 interp->result, errorInfo ? errorInfo : "<null>");
591 Tcl_FreeResult (interp);
597 * IrTcl_getRecordSyntaxStr: Return record syntax name of object id
599 static char *IrTcl_getRecordSyntaxStr (enum oid_value value)
602 struct oident ent, *entp;
604 ent.proto = PROTO_Z3950;
605 ent.oclass = CLASS_RECSYN;
608 o = oid_getoidbyent (&ent);
609 entp = oid_getentbyoid (o);
617 * IrTcl_getRecordSyntaxVal: Return record syntax value of string
619 static enum oid_value IrTcl_getRecordSyntaxVal (const char *name)
621 return oid_getvalbyname (name);
624 static IrTcl_RecordList *find_IR_record (IrTcl_SetObj *setobj, int no)
626 IrTcl_RecordList *rl;
628 for (rl = setobj->record_list; rl; rl = rl->next)
630 (!setobj->recordElements || !rl->elements ||
631 !strcmp (setobj->recordElements, rl->elements)))
636 static void delete_IR_records (IrTcl_SetObj *setobj)
638 IrTcl_RecordList *rl, *rl1;
640 for (rl = setobj->record_list; rl; rl = rl1)
642 delete_IR_record (rl);
646 setobj->record_list = NULL;
650 * ir_tcl_get_set_int: Set/get integer value
652 int ir_tcl_get_set_int (int *val, Tcl_Interp *interp, int argc, char **argv)
658 if (Tcl_GetInt (interp, argv[2], val)==TCL_ERROR)
661 sprintf (buf, "%d", *val);
662 Tcl_AppendResult (interp, buf, NULL);
668 * ir_tcl_method_error
670 int ir_tcl_method_error (Tcl_Interp *interp, int argc, char **argv,
673 IrTcl_Methods *tab_i = tab;
676 Tcl_AppendResult (interp, "bad method: \"", *argv, " ", argv[1],
677 "\"\nmethod should be of:", NULL);
678 for (tab_i = tab; tab_i->tab; tab_i++)
679 for (t = tab_i->tab; t->name; t++)
680 Tcl_AppendResult (interp, " ", t->name, NULL);
685 * ir_tcl_method: Search for method in table and invoke method handler
687 int ir_tcl_method (Tcl_Interp *interp, int argc, char **argv,
688 IrTcl_Methods *tab, int *ret)
690 IrTcl_Methods *tab_i = tab;
693 for (tab_i = tab; tab_i->tab; tab_i++)
694 for (t = tab_i->tab; t->name; t++)
697 if ((*t->method)(tab_i->obj, interp, argc, argv) == TCL_ERROR)
701 if (!strcmp (t->name, argv[1]))
703 *ret = (*t->method)(tab_i->obj, interp, argc, argv);
714 * ir_tcl_named_bits: get/set named bits
716 int ir_tcl_named_bits (struct ir_named_entry *tab, Odr_bitmask *ob,
717 Tcl_Interp *interp, int argc, char **argv)
719 struct ir_named_entry *ti;
724 for (no = 0; no < argc; no++)
727 for (ti = tab; ti->name; ti++)
728 if (!strcmp(argv[no], "@all") || !strcmp (argv[no], ti->name))
730 ODR_MASK_SET (ob, ti->pos);
735 Tcl_AppendResult (interp, "bad bit mask ", argv[no], NULL);
736 return ir_tcl_error_exec (interp, argc, argv);
741 for (ti = tab; ti->name; ti++)
742 if (ODR_MASK_GET (ob, ti->pos))
743 Tcl_AppendElement (interp, ti->name);
747 static void set_referenceId (ODR o, Z_ReferenceId **dst, const char *src)
753 *dst = odr_malloc (o, sizeof(**dst));
754 (*dst)->size = (*dst)->len = strlen(src);
755 (*dst)->buf = odr_malloc (o, (*dst)->len);
756 memcpy ((*dst)->buf, src, (*dst)->len);
760 static void get_referenceId (char **dst, Z_ReferenceId *src)
768 *dst = ir_tcl_malloc (src->len+1);
769 memcpy (*dst, src->buf, src->len);
770 (*dst)[src->len] = '\0';
773 /* ------------------------------------------------------- */
776 * do_init_request: init method on IR object
778 static int do_init_request (void *obj, Tcl_Interp *interp,
779 int argc, char **argv)
787 logf (LOG_DEBUG, "init %s", *argv);
790 Tcl_AppendResult (interp, "not connected", NULL);
791 return ir_tcl_error_exec (interp, argc, argv);
793 apdu = zget_APDU (p->odr_out, Z_APDU_initRequest);
794 req = apdu->u.initRequest;
796 set_referenceId (p->odr_out, &req->referenceId, p->set_inher.referenceId);
797 req->options = &p->options;
798 req->protocolVersion = &p->protocolVersion;
799 req->preferredMessageSize = &p->preferredMessageSize;
800 req->maximumRecordSize = &p->maximumRecordSize;
802 if (p->idAuthenticationGroupId || p->idAuthenticationUserId)
804 Z_IdPass *pass = odr_malloc (p->odr_out, sizeof(*pass));
805 Z_IdAuthentication *auth = odr_malloc (p->odr_out, sizeof(*auth));
807 logf (LOG_DEBUG, "using pass authentication");
809 auth->which = Z_IdAuthentication_idPass;
810 auth->u.idPass = pass;
811 if (p->idAuthenticationGroupId && *p->idAuthenticationGroupId)
812 pass->groupId = p->idAuthenticationGroupId;
814 pass->groupId = NULL;
815 if (p->idAuthenticationUserId && *p->idAuthenticationUserId)
816 pass->userId = p->idAuthenticationUserId;
819 if (p->idAuthenticationPassword && *p->idAuthenticationPassword)
820 pass->password = p->idAuthenticationPassword;
822 pass->password = NULL;
823 req->idAuthentication = auth;
825 else if (p->idAuthenticationOpen && *p->idAuthenticationOpen)
827 Z_IdAuthentication *auth = odr_malloc (p->odr_out, sizeof(*auth));
829 logf (LOG_DEBUG, "using open authentication");
830 auth->which = Z_IdAuthentication_open;
831 auth->u.open = p->idAuthenticationOpen;
832 req->idAuthentication = auth;
835 req->idAuthentication = NULL;
836 req->implementationId = p->implementationId;
837 req->implementationName = p->implementationName;
838 req->implementationVersion = p->implementationVersion;
839 req->userInformationField = 0;
841 return ir_tcl_send_APDU (interp, p, apdu, "init", *argv);
845 * do_protocolVersion: Set protocol Version
847 static int do_protocolVersion (void *obj, Tcl_Interp *interp,
848 int argc, char **argv)
856 ODR_MASK_ZERO (&p->protocolVersion);
857 ODR_MASK_SET (&p->protocolVersion, 0);
858 ODR_MASK_SET (&p->protocolVersion, 1);
863 if (Tcl_GetInt (interp, argv[2], &version)==TCL_ERROR)
865 ODR_MASK_ZERO (&p->protocolVersion);
866 for (i = 0; i<version; i++)
867 ODR_MASK_SET (&p->protocolVersion, i);
869 for (i = 4; --i >= 0; )
870 if (ODR_MASK_GET (&p->protocolVersion, i))
872 sprintf (buf, "%d", i+1);
873 interp->result = buf;
878 * do_options: Set options
880 static int do_options (void *obj, Tcl_Interp *interp,
881 int argc, char **argv)
883 static struct ir_named_entry options_tab[] = {
887 { "resourceReport", 3 },
888 { "triggerResourceCtrl", 4},
889 { "resourceCtrl", 5},
893 { "extendedServices", 10},
894 { "level-1Segmentation", 11},
895 { "level-2Segmentation", 12},
896 { "concurrentOperations", 13},
897 { "namedResultSets", 14},
904 ODR_MASK_ZERO (&p->options);
905 ODR_MASK_SET (&p->options, 0);
906 ODR_MASK_SET (&p->options, 1);
907 ODR_MASK_SET (&p->options, 4);
908 ODR_MASK_SET (&p->options, 7);
909 ODR_MASK_SET (&p->options, 14);
912 return ir_tcl_named_bits (options_tab, &p->options, interp, argc-2, argv+2);
916 * do_apduInfo: Get APDU information
918 static int do_apduInfo (void *obj, Tcl_Interp *interp, int argc, char **argv)
926 sprintf (buf, "%d", p->apduLen);
927 Tcl_AppendElement (interp, buf);
928 sprintf (buf, "%d", p->apduOffset);
929 Tcl_AppendElement (interp, buf);
932 Tcl_AppendElement (interp, "");
935 apduf = fopen ("apdu.tmp", "w");
938 Tcl_AppendElement (interp, "");
941 odr_dumpBER (apduf, p->buf_in, p->apduLen);
943 if (!(apduf = fopen ("apdu.tmp", "r")))
944 Tcl_AppendElement (interp, "");
949 Tcl_AppendResult (interp, " {", NULL);
950 while ((c = getc (apduf)) != EOF)
954 Tcl_AppendResult (interp, buf, NULL);
957 Tcl_AppendResult (interp, "}", NULL);
964 * do_failInfo: Get fail information
966 static int do_failInfo (void *obj, Tcl_Interp *interp, int argc, char **argv)
976 sprintf (buf, "%d", p->failInfo);
982 case IR_TCL_FAIL_CONNECT:
983 cp = "connect failed";
985 case IR_TCL_FAIL_READ:
986 cp = "connection closed";
988 case IR_TCL_FAIL_WRITE:
989 cp = "connection closed";
991 case IR_TCL_FAIL_IN_APDU:
992 cp = "failed to decode incoming APDU";
994 case IR_TCL_FAIL_UNKNOWN_APDU:
1000 Tcl_AppendElement (interp, buf);
1001 Tcl_AppendElement (interp, cp);
1006 * do_preferredMessageSize: Set/get preferred message size
1008 static int do_preferredMessageSize (void *obj, Tcl_Interp *interp,
1009 int argc, char **argv)
1015 p->preferredMessageSize = 30000;
1018 return ir_tcl_get_set_int (&p->preferredMessageSize, interp, argc, argv);
1022 * do_maximumRecordSize: Set/get maximum record size
1024 static int do_maximumRecordSize (void *obj, Tcl_Interp *interp,
1025 int argc, char **argv)
1031 p->maximumRecordSize = 30000;
1034 return ir_tcl_get_set_int (&p->maximumRecordSize, interp, argc, argv);
1038 * do_initResult: Get init result
1040 static int do_initResult (void *obj, Tcl_Interp *interp,
1041 int argc, char **argv)
1047 return ir_tcl_get_set_int (&p->initResult, interp, argc, argv);
1052 * do_implementationName: Set/get Implementation Name.
1054 static int do_implementationName (void *obj, Tcl_Interp *interp,
1055 int argc, char **argv)
1060 return ir_tcl_strdup (interp, &p->implementationName,
1062 else if (argc == -1)
1063 return ir_tcl_strdel (interp, &p->implementationName);
1066 xfree (p->implementationName);
1067 if (ir_tcl_strdup (interp, &p->implementationName, argv[2])
1071 Tcl_AppendResult (interp, p->implementationName, (char*) NULL);
1076 * do_implementationId: Get Implementation Id.
1078 static int do_implementationId (void *obj, Tcl_Interp *interp,
1079 int argc, char **argv)
1084 return ir_tcl_strdup (interp, &p->implementationId, "81");
1085 else if (argc == -1)
1086 return ir_tcl_strdel (interp, &p->implementationId);
1087 Tcl_AppendResult (interp, p->implementationId, (char*) NULL);
1092 * do_implementationVersion: get Implementation Version.
1094 static int do_implementationVersion (void *obj, Tcl_Interp *interp,
1095 int argc, char **argv)
1100 return ir_tcl_strdup (interp, &p->implementationVersion,
1101 #ifdef IR_TCL_VERSION
1106 else if (argc == -1)
1107 return ir_tcl_strdel (interp, &p->implementationVersion);
1108 Tcl_AppendResult (interp, p->implementationVersion, (char*) NULL);
1113 * do_targetImplementationName: Get Implementation Name of target.
1115 static int do_targetImplementationName (void *obj, Tcl_Interp *interp,
1116 int argc, char **argv)
1122 p->targetImplementationName = NULL;
1125 else if (argc == -1)
1126 return ir_tcl_strdel (interp, &p->targetImplementationName);
1127 Tcl_AppendResult (interp, p->targetImplementationName, (char*) NULL);
1132 * do_targetImplementationId: Get Implementation Id of target
1134 static int do_targetImplementationId (void *obj, Tcl_Interp *interp,
1135 int argc, char **argv)
1141 p->targetImplementationId = NULL;
1144 else if (argc == -1)
1145 return ir_tcl_strdel (interp, &p->targetImplementationId);
1146 Tcl_AppendResult (interp, p->targetImplementationId, (char*) NULL);
1151 * do_targetImplementationVersion: Get Implementation Version of target
1153 static int do_targetImplementationVersion (void *obj, Tcl_Interp *interp,
1154 int argc, char **argv)
1160 p->targetImplementationVersion = NULL;
1163 else if (argc == -1)
1164 return ir_tcl_strdel (interp, &p->targetImplementationVersion);
1165 Tcl_AppendResult (interp, p->targetImplementationVersion, (char*) NULL);
1170 * do_idAuthentication: Set/get id Authentication
1172 static int do_idAuthentication (void *obj, Tcl_Interp *interp,
1173 int argc, char **argv)
1177 if (argc >= 3 || argc == -1)
1179 xfree (p->idAuthenticationOpen);
1180 xfree (p->idAuthenticationGroupId);
1181 xfree (p->idAuthenticationUserId);
1182 xfree (p->idAuthenticationPassword);
1184 if (argc >= 3 || argc <= 0)
1186 p->idAuthenticationOpen = NULL;
1187 p->idAuthenticationGroupId = NULL;
1188 p->idAuthenticationUserId = NULL;
1189 p->idAuthenticationPassword = NULL;
1197 xfree (p->idAuthenticationGroupId);
1198 xfree (p->idAuthenticationUserId);
1199 xfree (p->idAuthenticationPassword);
1200 p->idAuthenticationGroupId = NULL;
1201 p->idAuthenticationUserId = NULL;
1202 p->idAuthenticationPassword = NULL;
1204 ir_tcl_strdup (interp, &p->idAuthenticationOpen, argv[2])
1210 xfree (p->idAuthenticationOpen);
1211 p->idAuthenticationOpen = NULL;
1213 ir_tcl_strdup (interp, &p->idAuthenticationGroupId, argv[2])
1217 ir_tcl_strdup (interp, &p->idAuthenticationUserId, argv[3])
1221 ir_tcl_strdup (interp, &p->idAuthenticationPassword, argv[4])
1226 if (p->idAuthenticationOpen)
1227 Tcl_AppendElement (interp, p->idAuthenticationOpen);
1228 else if (p->idAuthenticationGroupId || p->idAuthenticationUserId)
1230 Tcl_AppendElement (interp, p->idAuthenticationGroupId);
1231 Tcl_AppendElement (interp, p->idAuthenticationUserId);
1232 Tcl_AppendElement (interp, p->idAuthenticationPassword);
1238 * do_connect: connect method on IR object
1240 static int do_connect (void *obj, Tcl_Interp *interp,
1241 int argc, char **argv)
1251 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
1252 " ?hostname?\"", NULL);
1257 Tcl_AppendResult (interp, p->hostname, NULL);
1261 logf (LOG_DEBUG, "connect %s %s", *argv, argv[2]);
1264 Tcl_AppendResult (interp, "already connected", NULL);
1265 return ir_tcl_error_exec (interp, argc, argv);
1267 if (!strcmp (p->comstackType, "tcpip"))
1269 p->cs_link = cs_create (tcpip_type, CS_BLOCK, p->protocol_type);
1270 logf (LOG_DEBUG, "tcp/ip connect %s", argv[2]);
1272 else if (!strcmp (p->comstackType, "mosi"))
1275 p->cs_link = cs_create (mosi_type, CS_BLOCK, p->protocol_type);
1276 logf (LOG_DEBUG, "mosi connect %s", argv[2]);
1278 Tcl_AppendResult (interp, "mosi not supported", NULL);
1279 return ir_tcl_error_exec (interp, argc, argv);
1284 Tcl_AppendResult (interp, "bad comstack type ",
1285 p->comstackType, NULL);
1286 return ir_tcl_error_exec (interp, argc, argv);
1288 if (ir_tcl_strdup (interp, &p->hostname, argv[2]) == TCL_ERROR)
1290 p->eventType = "connect";
1291 addr = cs_straddr (p->cs_link, argv[2]);
1294 ir_tcl_disconnect (p);
1295 Tcl_AppendResult (interp, "cs_straddr fail", NULL);
1296 return ir_tcl_error_exec (interp, argc, argv);
1298 if ((r=cs_connect (p->cs_link, addr)) < 0)
1300 ir_tcl_disconnect (p);
1301 Tcl_AppendResult (interp, "connect fail", NULL);
1302 return ir_tcl_error_exec (interp, argc, argv);
1304 ir_select_add (cs_fileno (p->cs_link), p);
1307 logf (LOG_DEBUG, "connect pending fd=%d", cs_fileno(p->cs_link));
1308 ir_select_add_write (cs_fileno (p->cs_link), p);
1309 p->state = IR_TCL_R_Connecting;
1313 logf (LOG_DEBUG, "connect ok fd=%d", cs_fileno(p->cs_link));
1314 p->state = IR_TCL_R_Idle;
1316 ir_tcl_eval (p->interp, p->callback);
1323 * ir_tcl_disconnect: close connection
1325 void ir_tcl_disconnect (IrTcl_Obj *p)
1329 logf(LOG_DEBUG, "Closing connection to %s", p->hostname);
1330 xfree (p->hostname);
1332 assert (p->cs_link);
1333 ir_select_remove (cs_fileno (p->cs_link), p);
1335 odr_reset (p->odr_in);
1337 #if TCL_MAJOR_VERSION == 8
1338 cs_fileno(p->cs_link) = -1;
1340 cs_close (p->cs_link);
1343 ODR_MASK_ZERO (&p->options);
1344 ODR_MASK_SET (&p->options, 0);
1345 ODR_MASK_SET (&p->options, 1);
1346 ODR_MASK_SET (&p->options, 4);
1347 ODR_MASK_SET (&p->options, 7);
1348 ODR_MASK_SET (&p->options, 14);
1350 ODR_MASK_ZERO (&p->protocolVersion);
1351 ODR_MASK_SET (&p->protocolVersion, 0);
1352 ODR_MASK_SET (&p->protocolVersion, 1);
1355 assert (!p->cs_link);
1359 * do_disconnect: disconnect method on IR object
1361 static int do_disconnect (void *obj, Tcl_Interp *interp,
1362 int argc, char **argv)
1368 p->state = IR_TCL_R_Idle;
1369 p->eventType = NULL;
1374 ir_tcl_disconnect (p);
1379 * do_comstack: Set/get comstack method on IR object
1381 static int do_comstack (void *o, Tcl_Interp *interp,
1382 int argc, char **argv)
1387 return ir_tcl_strdup (interp, &obj->comstackType, "tcpip");
1388 else if (argc == -1)
1389 return ir_tcl_strdel (interp, &obj->comstackType);
1392 xfree (obj->comstackType);
1393 if (ir_tcl_strdup (interp, &obj->comstackType, argv[2]) == TCL_ERROR)
1396 Tcl_AppendElement (interp, obj->comstackType);
1401 * do_logLevel: Set log level
1403 static int do_logLevel (void *o, Tcl_Interp *interp,
1404 int argc, char **argv)
1409 yaz_log_init (yaz_log_mask_str (argv[2]), "", NULL);
1411 yaz_log_init (yaz_log_mask_str (argv[2]), argv[3], NULL);
1413 yaz_log_init (yaz_log_mask_str (argv[2]), argv[3], argv[4]);
1419 * do_eventType: Return type of last event
1421 static int do_eventType (void *obj, Tcl_Interp *interp,
1422 int argc, char **argv)
1428 p->eventType = NULL;
1431 Tcl_AppendElement (interp, p->eventType ? p->eventType : "");
1437 * do_callback: add callback
1439 static int do_callback (void *obj, Tcl_Interp *interp,
1440 int argc, char **argv)
1449 else if (argc == -1)
1450 return ir_tcl_strdel (interp, &p->callback);
1453 xfree (p->callback);
1456 if (ir_tcl_strdup (interp, &p->callback, argv[2]) == TCL_ERROR)
1466 * do_failback: add error handle callback
1468 static int do_failback (void *obj, Tcl_Interp *interp,
1469 int argc, char **argv)
1478 else if (argc == -1)
1479 return ir_tcl_strdel (interp, &p->failback);
1482 xfree (p->failback);
1485 if (ir_tcl_strdup (interp, &p->failback, argv[2]) == TCL_ERROR)
1495 * do_initResponse: add init response handler
1497 static int do_initResponse (void *obj, Tcl_Interp *interp,
1498 int argc, char **argv)
1504 p->initResponse = NULL;
1507 else if (argc == -1)
1508 return ir_tcl_strdel (interp, &p->initResponse);
1511 xfree (p->initResponse);
1514 if (ir_tcl_strdup (interp, &p->initResponse, argv[2]) == TCL_ERROR)
1518 p->initResponse = NULL;
1523 * do_protocol: Set/get protocol method on IR object
1525 static int do_protocol (void *o, Tcl_Interp *interp, int argc, char **argv)
1531 p->protocol_type = PROTO_Z3950;
1536 if (!strcmp (argv[2], "Z39"))
1537 p->protocol_type = PROTO_Z3950;
1538 else if (!strcmp (argv[2], "SR"))
1539 p->protocol_type = PROTO_SR;
1542 Tcl_AppendResult (interp, "bad protocol ", argv[2], NULL);
1543 return ir_tcl_error_exec (interp, argc, argv);
1547 switch (p->protocol_type)
1550 Tcl_AppendElement (interp, "Z39");
1553 Tcl_AppendElement (interp, "SR");
1560 * do_triggerResourceControl:
1562 static int do_triggerResourceControl (void *obj, Tcl_Interp *interp,
1563 int argc, char **argv)
1567 Z_TriggerResourceControlRequest *req;
1568 bool_t is_false = 0;
1574 Tcl_AppendResult (interp, "not connected", NULL);
1575 return ir_tcl_error_exec (interp, argc, argv);
1577 apdu = zget_APDU (p->odr_out, Z_APDU_triggerResourceControlRequest);
1578 req = apdu->u.triggerResourceControlRequest;
1579 *req->requestedAction = Z_TriggerResourceCtrl_cancel;
1580 req->resultSetWanted = &is_false;
1582 return ir_tcl_send_APDU (interp, p, apdu, "triggerResourceControl",
1587 * do_databaseNames: specify database names
1589 static int do_databaseNames (void *obj, Tcl_Interp *interp,
1590 int argc, char **argv)
1593 IrTcl_SetCObj *p = obj;
1597 for (i=0; i<p->num_databaseNames; i++)
1598 xfree (p->databaseNames[i]);
1599 xfree (p->databaseNames);
1603 p->num_databaseNames = 0;
1604 p->databaseNames = NULL;
1609 for (i=0; i<p->num_databaseNames; i++)
1610 Tcl_AppendElement (interp, p->databaseNames[i]);
1613 if (p->databaseNames)
1615 for (i=0; i<p->num_databaseNames; i++)
1616 xfree (p->databaseNames[i]);
1617 xfree (p->databaseNames);
1619 p->num_databaseNames = argc - 2;
1621 ir_tcl_malloc (sizeof(*p->databaseNames) * (1+p->num_databaseNames));
1622 for (i=0; i<p->num_databaseNames; i++)
1624 if (ir_tcl_strdup (interp, &p->databaseNames[i], argv[2+i])
1628 p->databaseNames[i] = NULL;
1633 * do_replaceIndicator: Set/get replace Set indicator
1635 static int do_replaceIndicator (void *obj, Tcl_Interp *interp,
1636 int argc, char **argv)
1638 IrTcl_SetCObj *p = obj;
1642 p->replaceIndicator = 1;
1645 return ir_tcl_get_set_int (&p->replaceIndicator, interp, argc, argv);
1649 * do_queryType: Set/Get query method
1651 static int do_queryType (void *obj, Tcl_Interp *interp,
1652 int argc, char **argv)
1654 IrTcl_SetCObj *p = obj;
1657 return ir_tcl_strdup (interp, &p->queryType, "rpn");
1658 else if (argc == -1)
1659 return ir_tcl_strdel (interp, &p->queryType);
1662 xfree (p->queryType);
1663 if (ir_tcl_strdup (interp, &p->queryType, argv[2]) == TCL_ERROR)
1666 Tcl_AppendResult (interp, p->queryType, NULL);
1671 * do_userInformationField: Get User information field
1673 static int do_userInformationField (void *obj, Tcl_Interp *interp,
1674 int argc, char **argv)
1680 p->userInformationField = NULL;
1683 else if (argc == -1)
1684 return ir_tcl_strdel (interp, &p->userInformationField);
1685 Tcl_AppendResult (interp, p->userInformationField, NULL);
1690 * do_smallSetUpperBound: Set/get small set upper bound
1692 static int do_smallSetUpperBound (void *o, Tcl_Interp *interp,
1693 int argc, char **argv)
1695 IrTcl_SetCObj *p = o;
1699 p->smallSetUpperBound = 0;
1702 return ir_tcl_get_set_int (&p->smallSetUpperBound, interp, argc, argv);
1706 * do_largeSetLowerBound: Set/get large set lower bound
1708 static int do_largeSetLowerBound (void *o, Tcl_Interp *interp,
1709 int argc, char **argv)
1711 IrTcl_SetCObj *p = o;
1715 p->largeSetLowerBound = 1;
1718 return ir_tcl_get_set_int (&p->largeSetLowerBound, interp, argc, argv);
1722 * do_mediumSetPresentNumber: Set/get large set lower bound
1724 static int do_mediumSetPresentNumber (void *o, Tcl_Interp *interp,
1725 int argc, char **argv)
1727 IrTcl_SetCObj *p = o;
1731 p->mediumSetPresentNumber = 0;
1734 return ir_tcl_get_set_int (&p->mediumSetPresentNumber, interp, argc, argv);
1738 * do_referenceId: Set/Get referenceId
1740 static int do_referenceId (void *obj, Tcl_Interp *interp,
1741 int argc, char **argv)
1743 IrTcl_SetCObj *p = obj;
1747 p->referenceId = NULL;
1750 else if (argc == -1)
1751 return ir_tcl_strdel (interp, &p->referenceId);
1754 xfree (p->referenceId);
1755 if (ir_tcl_strdup (interp, &p->referenceId, argv[2]) == TCL_ERROR)
1758 Tcl_AppendResult (interp, p->referenceId, NULL);
1763 * do_preferredRecordSyntax: Set/get preferred record syntax
1765 static int do_preferredRecordSyntax (void *obj, Tcl_Interp *interp,
1766 int argc, char **argv)
1768 IrTcl_SetCObj *p = obj;
1772 p->preferredRecordSyntax = NULL;
1775 else if (argc == -1)
1777 xfree (p->preferredRecordSyntax);
1778 p->preferredRecordSyntax = NULL;
1783 xfree (p->preferredRecordSyntax);
1784 p->preferredRecordSyntax = NULL;
1785 if (argv[2][0] && (p->preferredRecordSyntax =
1786 ir_tcl_malloc (sizeof(*p->preferredRecordSyntax))))
1787 *p->preferredRecordSyntax = IrTcl_getRecordSyntaxVal (argv[2]);
1792 (interp,!p->preferredRecordSyntax ? "" :
1793 IrTcl_getRecordSyntaxStr(*p->preferredRecordSyntax));
1800 * do_elementSetNames: Set/Get element Set Names
1802 static int do_elementSetNames (void *obj, Tcl_Interp *interp,
1803 int argc, char **argv)
1805 IrTcl_SetCObj *p = obj;
1809 p->elementSetNames = NULL;
1812 else if (argc == -1)
1813 return ir_tcl_strdel (interp, &p->elementSetNames);
1816 xfree (p->elementSetNames);
1817 if (ir_tcl_strdup (interp, &p->elementSetNames, argv[2]) == TCL_ERROR)
1820 Tcl_AppendResult (interp, p->elementSetNames, NULL);
1825 * do_smallSetElementSetNames: Set/Get small Set Element Set Names
1827 static int do_smallSetElementSetNames (void *obj, Tcl_Interp *interp,
1828 int argc, char **argv)
1830 IrTcl_SetCObj *p = obj;
1834 p->smallSetElementSetNames = NULL;
1837 else if (argc == -1)
1838 return ir_tcl_strdel (interp, &p->smallSetElementSetNames);
1841 xfree (p->smallSetElementSetNames);
1842 if (ir_tcl_strdup (interp, &p->smallSetElementSetNames,
1843 argv[2]) == TCL_ERROR)
1846 Tcl_AppendResult (interp, p->smallSetElementSetNames, NULL);
1851 * do_mediumSetElementSetNames: Set/Get medium Set Element Set Names
1853 static int do_mediumSetElementSetNames (void *obj, Tcl_Interp *interp,
1854 int argc, char **argv)
1856 IrTcl_SetCObj *p = obj;
1860 p->mediumSetElementSetNames = NULL;
1863 else if (argc == -1)
1864 return ir_tcl_strdel (interp, &p->mediumSetElementSetNames);
1867 xfree (p->mediumSetElementSetNames);
1868 if (ir_tcl_strdup (interp, &p->mediumSetElementSetNames,
1869 argv[2]) == TCL_ERROR)
1872 Tcl_AppendResult (interp, p->mediumSetElementSetNames, NULL);
1876 static IrTcl_Method ir_method_tab[] = {
1877 { "comstack", do_comstack, NULL },
1878 { "protocol", do_protocol, NULL },
1879 { "failback", do_failback, NULL },
1880 { "failInfo", do_failInfo, NULL },
1881 { "apduInfo", do_apduInfo, NULL },
1882 { "logLevel", do_logLevel, NULL },
1884 { "eventType", do_eventType, NULL },
1885 { "connect", do_connect, NULL },
1886 { "protocolVersion", do_protocolVersion, NULL },
1887 { "preferredMessageSize", do_preferredMessageSize, NULL },
1888 { "maximumRecordSize", do_maximumRecordSize, NULL },
1889 { "implementationName", do_implementationName, NULL },
1890 { "implementationId", do_implementationId, NULL },
1891 { "implementationVersion", do_implementationVersion, NULL },
1892 { "targetImplementationName", do_targetImplementationName, NULL },
1893 { "targetImplementationId", do_targetImplementationId, NULL },
1894 { "targetImplementationVersion", do_targetImplementationVersion, NULL},
1895 { "userInformationField", do_userInformationField, NULL},
1896 { "idAuthentication", do_idAuthentication, NULL},
1897 { "options", do_options, NULL},
1898 { "init", do_init_request, NULL},
1899 { "initResult", do_initResult, NULL},
1900 { "disconnect", do_disconnect, NULL},
1901 { "callback", do_callback, NULL},
1902 { "initResponse", do_initResponse, NULL},
1903 { "triggerResourceControl", do_triggerResourceControl, NULL},
1904 { "initResponse", do_initResponse, NULL},
1908 static IrTcl_Method ir_set_c_method_tab[] = {
1909 { "databaseNames", do_databaseNames, NULL},
1910 { "replaceIndicator", do_replaceIndicator, NULL},
1911 { "queryType", do_queryType, NULL},
1912 { "preferredRecordSyntax", do_preferredRecordSyntax, NULL},
1913 { "smallSetUpperBound", do_smallSetUpperBound, NULL},
1914 { "largeSetLowerBound", do_largeSetLowerBound, NULL},
1915 { "mediumSetPresentNumber", do_mediumSetPresentNumber, NULL},
1916 { "referenceId", do_referenceId, NULL},
1917 { "elementSetNames", do_elementSetNames, NULL},
1918 { "smallSetElementSetNames", do_smallSetElementSetNames, NULL},
1919 { "mediumSetElementSetNames", do_mediumSetElementSetNames, NULL},
1924 * ir_obj_method: IR Object methods
1926 static int ir_obj_method (ClientData clientData, Tcl_Interp *interp,
1927 int argc, char **argv)
1929 IrTcl_Methods tab[3];
1930 IrTcl_Obj *p = (IrTcl_Obj *) clientData;
1935 Tcl_AppendResult (interp, wrongArgs, *argv, "method args...\"", NULL);
1939 tab[0].tab = ir_method_tab;
1941 tab[1].tab = ir_set_c_method_tab;
1942 tab[1].obj = &p->set_inher;
1945 if (ir_tcl_method (interp, argc, argv, tab, &r) == TCL_ERROR)
1946 return ir_tcl_method_error (interp, argc, argv, tab);
1951 * ir_obj_delete: IR Object disposal
1953 static void ir_obj_delete (ClientData clientData)
1955 IrTcl_Obj *obj = (IrTcl_Obj *) clientData;
1956 IrTcl_Methods tab[3];
1959 if (obj->ref_count > 0)
1961 assert (obj->ref_count == 0);
1963 logf (LOG_DEBUG, "ir object delete");
1964 tab[0].tab = ir_method_tab;
1966 tab[1].tab = ir_set_c_method_tab;
1967 tab[1].obj = &obj->set_inher;
1970 ir_tcl_method (NULL, -1, NULL, tab, NULL);
1973 odr_destroy (obj->odr_in);
1974 odr_destroy (obj->odr_out);
1977 obj->odr_pr->print = 0;
1978 odr_destroy (obj->odr_pr);
1984 * ir_obj_init: IR Object initialization
1986 int ir_obj_init (ClientData clientData, Tcl_Interp *interp,
1987 int argc, char **argv, ClientData *subData,
1988 ClientData parentData)
1990 IrTcl_Methods tab[3];
1998 Tcl_AppendResult (interp, wrongArgs, *argv, " objName\"", NULL);
2001 obj = ir_tcl_malloc (sizeof(*obj));
2004 obj->bibset = ccl_qual_mk ();
2005 if ((inf = fopen ("default.bib", "r")))
2007 ccl_qual_file (obj->bibset, inf);
2012 logf (LOG_DEBUG, "ir object create %s", argv[1]);
2013 obj->odr_in = odr_createmem (ODR_DECODE);
2014 odr_choice_enable_bias (obj->odr_in, 0);
2015 obj->odr_out = odr_createmem (ODR_ENCODE);
2019 obj->odr_pr = odr_createmem (ODR_PRINT);
2020 odr_setprint(obj->odr_pr, odr_print_file);
2022 obj->state = IR_TCL_R_Idle;
2023 obj->interp = interp;
2027 obj->request_queue = NULL;
2029 tab[0].tab = ir_method_tab;
2031 tab[1].tab = ir_set_c_method_tab;
2032 tab[1].obj = &obj->set_inher;
2035 if (ir_tcl_method (interp, 0, NULL, tab, NULL) == TCL_ERROR)
2037 Tcl_AppendResult (interp, "Failed to initialize ", argv[1], NULL);
2040 *subData = (ClientData) obj;
2046 * ir_obj_mk: IR Object creation
2048 static int ir_obj_mk (ClientData clientData, Tcl_Interp *interp,
2049 int argc, char **argv)
2052 int r = ir_obj_init (clientData, interp, argc, argv, &subData, 0);
2056 Tcl_CreateCommand (interp, argv[1], ir_obj_method,
2057 subData, ir_obj_delete);
2061 IrTcl_Class ir_obj_class = {
2069 /* ------------------------------------------------------- */
2071 * do_search: Do search request
2073 static int do_search (void *o, Tcl_Interp *interp, int argc, char **argv)
2075 Z_SearchRequest *req;
2079 IrTcl_SetObj *obj = o;
2082 #if TCL_MAJOR_VERSION > 8 || (TCL_MAJOR_VERSION == 8 && TCL_MINOR_VERSION > 0)
2094 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1], "query\"",
2098 #if TCL_MAJOR_VERSION > 8 || (TCL_MAJOR_VERSION == 8 && TCL_MINOR_VERSION > 0)
2099 query_str = Tcl_UtfToExternalDString(0, argv[2], -1, &ds);
2101 query_str = argv[2];
2103 logf (LOG_DEBUG, "search %s %s", *argv, query_str);
2104 if (!obj->set_inher.num_databaseNames)
2106 Tcl_AppendResult (interp, "no databaseNames", NULL);
2107 code = ir_tcl_error_exec (interp, argc, argv);
2112 Tcl_AppendResult (interp, "not connected", NULL);
2113 code = ir_tcl_error_exec (interp, argc, argv);
2116 apdu = zget_APDU (p->odr_out, Z_APDU_searchRequest);
2117 req = apdu->u.searchRequest;
2121 set_referenceId (p->odr_out, &req->referenceId,
2122 obj->set_inher.referenceId);
2124 req->smallSetUpperBound = &obj->set_inher.smallSetUpperBound;
2125 req->largeSetLowerBound = &obj->set_inher.largeSetLowerBound;
2126 req->mediumSetPresentNumber = &obj->set_inher.mediumSetPresentNumber;
2127 req->replaceIndicator = &obj->set_inher.replaceIndicator;
2128 req->resultSetName = obj->setName ? obj->setName : "default";
2129 logf (LOG_DEBUG, "Search, resultSetName %s", req->resultSetName);
2130 req->num_databaseNames = obj->set_inher.num_databaseNames;
2131 req->databaseNames = obj->set_inher.databaseNames;
2132 for (r=0; r < obj->set_inher.num_databaseNames; r++)
2133 logf (LOG_DEBUG, " Database %s", obj->set_inher.databaseNames[r]);
2134 if (obj->set_inher.preferredRecordSyntax)
2136 struct oident ident;
2138 ident.proto = p->protocol_type;
2139 ident.oclass = CLASS_RECSYN;
2140 ident.value = *obj->set_inher.preferredRecordSyntax;
2141 logf (LOG_DEBUG, "Preferred record syntax is %d", ident.value);
2142 req->preferredRecordSyntax = odr_oiddup (p->odr_out,
2143 oid_getoidbyent (&ident));
2146 req->preferredRecordSyntax = 0;
2148 if (obj->set_inher.smallSetElementSetNames &&
2149 *obj->set_inher.smallSetElementSetNames)
2151 Z_ElementSetNames *esn = odr_malloc (p->odr_out, sizeof(*esn));
2153 esn->which = Z_ElementSetNames_generic;
2154 esn->u.generic = obj->set_inher.smallSetElementSetNames;
2155 req->smallSetElementSetNames = esn;
2158 req->smallSetElementSetNames = NULL;
2160 if (obj->set_inher.mediumSetElementSetNames &&
2161 *obj->set_inher.mediumSetElementSetNames)
2163 Z_ElementSetNames *esn = odr_malloc (p->odr_out, sizeof(*esn));
2165 esn->which = Z_ElementSetNames_generic;
2166 esn->u.generic = obj->set_inher.mediumSetElementSetNames;
2167 req->mediumSetElementSetNames = esn;
2170 req->mediumSetElementSetNames = NULL;
2172 req->query = &query;
2174 logf (LOG_DEBUG, "queryType %s", obj->set_inher.queryType);
2175 if (!strcmp (obj->set_inher.queryType, "rpn"))
2177 Z_RPNQuery *RPNquery;
2179 RPNquery = p_query_rpn (p->odr_out, p->protocol_type, query_str);
2182 Tcl_AppendResult (interp, "query syntax error", NULL);
2183 code = ir_tcl_error_exec (interp, argc, argv);
2186 query.which = Z_Query_type_1;
2187 query.u.type_1 = RPNquery;
2190 else if (!strcmp (obj->set_inher.queryType, "cclrpn"))
2194 struct ccl_rpn_node *rpn;
2195 Z_RPNQuery *RPNquery;
2198 bib1.proto = p->protocol_type;
2199 bib1.oclass = CLASS_ATTSET;
2200 bib1.value = VAL_BIB1;
2202 rpn = ccl_find_str(p->bibset, query_str, &error, &pos);
2205 Tcl_AppendResult (interp, "ccl syntax error ", ccl_err_msg(error),
2207 code = ir_tcl_error_exec (interp, argc, argv);
2211 ccl_pr_tree (rpn, stderr);
2212 fprintf (stderr, "\n");
2214 RPNquery = ccl_rpn_query(p->odr_out, rpn);
2215 RPNquery->attributeSetId = oid_getoidbyent (&bib1);
2216 query.which = Z_Query_type_1;
2217 query.u.type_1 = RPNquery;
2220 else if (!strcmp (obj->set_inher.queryType, "ccl"))
2222 query.which = Z_Query_type_2;
2223 query.u.type_2 = &ccl_query;
2224 ccl_query.buf = (unsigned char *) query_str;
2225 ccl_query.len = strlen (query_str);
2229 Tcl_AppendResult (interp, "invalid query method ",
2230 obj->set_inher.queryType, NULL);
2231 code = ir_tcl_error_exec (interp, argc, argv);
2234 code = ir_tcl_send_APDU (interp, p, apdu, "search", *argv);
2236 #if TCL_MAJOR_VERSION > 8 || (TCL_MAJOR_VERSION == 8 && TCL_MINOR_VERSION > 0)
2237 Tcl_DStringFree (&ds);
2243 * do_searchResponse: add search response handler
2245 static int do_searchResponse (void *o, Tcl_Interp *interp,
2246 int argc, char **argv)
2248 IrTcl_SetObj *obj = o;
2252 obj->searchResponse = NULL;
2255 else if (argc == -1)
2256 return ir_tcl_strdel (interp, &obj->searchResponse);
2259 xfree (obj->searchResponse);
2262 if (ir_tcl_strdup (interp, &obj->searchResponse, argv[2])
2267 obj->searchResponse = NULL;
2273 * do_presentResponse: add present response handler
2275 static int do_presentResponse (void *o, Tcl_Interp *interp,
2276 int argc, char **argv)
2278 IrTcl_SetObj *obj = o;
2282 obj->presentResponse = NULL;
2285 else if (argc == -1)
2286 return ir_tcl_strdel (interp, &obj->presentResponse);
2289 xfree (obj->presentResponse);
2292 if (ir_tcl_strdup (interp, &obj->presentResponse, argv[2])
2297 obj->presentResponse = NULL;
2303 * do_sortResponse: add sort response handler
2305 static int do_sortResponse (void *o, Tcl_Interp *interp,
2306 int argc, char **argv)
2308 IrTcl_SetObj *obj = o;
2312 obj->sortResponse = NULL;
2315 else if (argc == -1)
2316 return ir_tcl_strdel (interp, &obj->sortResponse);
2319 xfree (obj->sortResponse);
2322 if (ir_tcl_strdup (interp, &obj->sortResponse, argv[2])
2327 obj->sortResponse = NULL;
2333 * do_resultCount: Get number of hits
2335 static int do_resultCount (void *o, Tcl_Interp *interp,
2336 int argc, char **argv)
2338 IrTcl_SetObj *obj = o;
2342 obj->resultCount = 0;
2345 return ir_tcl_get_set_int (&obj->resultCount, interp, argc, argv);
2349 * do_searchStatus: Get search status (after search response)
2351 static int do_searchStatus (void *o, Tcl_Interp *interp,
2352 int argc, char **argv)
2354 IrTcl_SetObj *obj = o;
2358 return ir_tcl_get_set_int (&obj->searchStatus, interp, argc, argv);
2361 static void reset_searchResult (IrTcl_SetObj *setobj)
2364 for (i = 0; i<setobj->searchResult_num; i++)
2365 xfree (setobj->searchResult_terms[i]);
2366 xfree (setobj->searchResult_terms);
2367 xfree (setobj->searchResult_count);
2368 setobj->searchResult_terms = 0;
2369 setobj->searchResult_num = 0;
2374 * do_searchResult Get USR:Search-Result1 (after search response)
2376 static int do_searchResult (void *o, Tcl_Interp *interp,
2377 int argc, char **argv)
2379 IrTcl_SetObj *obj = o;
2384 obj->searchResult_num = 0;
2385 obj->searchResult_terms = 0;
2386 obj->searchResult_count = 0;
2389 else if (argc == -1)
2391 reset_searchResult (obj);
2394 for (i = 0; i<obj->searchResult_num; i++)
2397 sprintf (str, "%d", obj->searchResult_count[i]);
2398 Tcl_AppendResult (interp, "{", NULL);
2399 if (obj->searchResult_terms[i])
2400 Tcl_AppendElement (interp, obj->searchResult_terms[i]);
2402 Tcl_AppendElement (interp, "");
2403 Tcl_AppendElement (interp, str);
2404 Tcl_AppendResult (interp, "} ", NULL);
2410 * do_presentStatus: Get search status (after search/present response)
2412 static int do_presentStatus (void *o, Tcl_Interp *interp,
2413 int argc, char **argv)
2415 IrTcl_SetObj *obj = o;
2419 return ir_tcl_get_set_int (&obj->presentStatus, interp, argc, argv);
2423 * do_sortStatus: Get sort status (after sort response)
2425 static int do_sortStatus (void *o, Tcl_Interp *interp,
2426 int argc, char **argv)
2428 IrTcl_SetObj *obj = o;
2433 obj->sortStatus = Z_SortStatus_failure;
2436 switch (obj->sortStatus)
2438 case Z_SortStatus_success:
2439 res = "success"; break;
2440 case Z_SortStatus_partial_1:
2441 res = "partial"; break;
2442 case Z_SortStatus_failure:
2443 res = "failure"; break;
2445 res = "unknown"; break;
2447 Tcl_AppendElement (interp, res);
2452 * do_nextResultSetPosition: Get next result set position
2453 * (after search/present response)
2455 static int do_nextResultSetPosition (void *o, Tcl_Interp *interp,
2456 int argc, char **argv)
2458 IrTcl_SetObj *obj = o;
2462 obj->nextResultSetPosition = 0;
2465 return ir_tcl_get_set_int (&obj->nextResultSetPosition, interp,
2470 * do_setName: Set result Set name
2472 static int do_setName (void *o, Tcl_Interp *interp,
2473 int argc, char **argv)
2475 IrTcl_SetObj *obj = o;
2478 return ir_tcl_strdup (interp, &obj->setName, "Default");
2479 else if (argc == -1)
2480 return ir_tcl_strdel (interp, &obj->setName);
2483 xfree (obj->setName);
2484 if (ir_tcl_strdup (interp, &obj->setName, argv[2])
2488 Tcl_AppendElement (interp, obj->setName);
2493 * do_numberOfRecordsReturned: Get number of records returned
2495 static int do_numberOfRecordsReturned (void *o, Tcl_Interp *interp,
2496 int argc, char **argv)
2498 IrTcl_SetObj *obj = o;
2502 obj->numberOfRecordsReturned = 0;
2505 return ir_tcl_get_set_int (&obj->numberOfRecordsReturned, interp,
2510 * do_type: Return type (if any) at position.
2512 static int do_type (void *o, Tcl_Interp *interp, int argc, char **argv)
2514 IrTcl_SetObj *obj = o;
2516 IrTcl_RecordList *rl;
2520 obj->record_list = NULL;
2523 else if (argc == -1)
2525 delete_IR_records (obj);
2530 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2531 " position\"", NULL);
2534 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2536 rl = find_IR_record (obj, offset);
2539 logf (LOG_DEBUG, "%s %s %s: no record", argv[0], argv[1], argv[2]);
2544 case Z_NamePlusRecord_databaseRecord:
2545 interp->result = "DB";
2547 case Z_NamePlusRecord_surrogateDiagnostic:
2548 interp->result = "SD";
2556 * do_recordType: Return record type (if any) at position.
2558 static int do_recordType (void *o, Tcl_Interp *interp, int argc, char **argv)
2560 IrTcl_SetObj *obj = o;
2562 IrTcl_RecordList *rl;
2568 else if (argc == -1)
2574 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2575 " position\"", NULL);
2578 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2580 rl = find_IR_record (obj, offset);
2583 logf (LOG_DEBUG, "%s %s %s: no record", argv[0], argv[1], argv[2]);
2586 if (rl->which != Z_NamePlusRecord_databaseRecord)
2588 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2591 Tcl_AppendElement (interp, (char*)
2592 IrTcl_getRecordSyntaxStr (rl->u.dbrec.type));
2597 * set record elements (for record extraction)
2599 static int do_recordElements (void *o, Tcl_Interp *interp,
2600 int argc, char **argv)
2602 IrTcl_SetObj *obj = o;
2606 obj->recordElements = NULL;
2609 else if (argc == -1)
2610 return ir_tcl_strdel (NULL, &obj->recordElements);
2613 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2614 " ?position?\"", NULL);
2619 xfree (obj->recordElements);
2620 return ir_tcl_strdup (NULL, &obj->recordElements,
2621 (*argv[2] ? argv[2] : NULL));
2623 Tcl_AppendResult (interp, obj->recordElements, NULL);
2630 static int ir_diagResult (Tcl_Interp *interp, IrTcl_Diagnostic *list, int num)
2636 for (i = 0; i<num; i++)
2638 sprintf (buf, "%d", list[i].condition);
2639 Tcl_AppendElement (interp, buf);
2640 cp = diagbib1_str (list[i].condition);
2642 Tcl_AppendElement (interp, (char*) cp);
2644 Tcl_AppendElement (interp, "");
2645 if (list[i].addinfo)
2646 Tcl_AppendElement (interp, (char*) list[i].addinfo);
2648 Tcl_AppendElement (interp, "");
2654 * do_diag: Return diagnostic record info
2656 static int do_diag (void *o, Tcl_Interp *interp, int argc, char **argv)
2658 IrTcl_SetObj *obj = o;
2660 IrTcl_RecordList *rl;
2666 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2667 " position\"", NULL);
2670 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2672 rl = find_IR_record (obj, offset);
2675 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2678 if (rl->which != Z_NamePlusRecord_surrogateDiagnostic)
2680 Tcl_AppendResult (interp, "No Diagnostic record at #", argv[2], NULL);
2683 return ir_diagResult (interp, rl->u.surrogateDiagnostics.list,
2684 rl->u.surrogateDiagnostics.num);
2688 * do_getMarc: Get ISO2709 Record lines/fields
2690 static int do_getMarc (void *o, Tcl_Interp *interp, int argc, char **argv)
2692 IrTcl_SetObj *obj = o;
2694 IrTcl_RecordList *rl;
2700 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2701 " position line|field tag ind field\"", NULL);
2704 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2706 rl = find_IR_record (obj, offset);
2709 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2712 if (rl->which != Z_NamePlusRecord_databaseRecord)
2714 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2717 return ir_tcl_get_marc (interp, rl->u.dbrec.buf, argc, argv);
2721 * do_getSutrs: Get SUTRS Record
2723 static int do_getSutrs (void *o, Tcl_Interp *interp, int argc, char **argv)
2725 IrTcl_SetObj *obj = o;
2727 IrTcl_RecordList *rl;
2733 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2734 " position\"", NULL);
2737 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2739 rl = find_IR_record (obj, offset);
2742 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2745 if (rl->which != Z_NamePlusRecord_databaseRecord)
2747 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2750 if (!rl->u.dbrec.buf || rl->u.dbrec.type != VAL_SUTRS)
2752 Tcl_AppendElement (interp, rl->u.dbrec.buf);
2758 * do_getGrs: Get a GRS-1 Record
2760 static int do_getGrs (void *o, Tcl_Interp *interp, int argc, char **argv)
2762 IrTcl_SetObj *obj = o;
2764 IrTcl_RecordList *rl;
2770 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2771 " position ?(set,tag) (set,tag) ...?\"", NULL);
2774 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2776 rl = find_IR_record (obj, offset);
2779 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2782 if (rl->which != Z_NamePlusRecord_databaseRecord)
2784 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2787 if (rl->u.dbrec.type != VAL_GRS1)
2789 return ir_tcl_get_grs (interp, rl->u.dbrec.u.grs1, argc, argv);
2794 * do_getExplain: Get an Explain Record
2796 static int do_getExplain (void *o, Tcl_Interp *interp, int argc, char **argv)
2798 IrTcl_SetObj *obj = o;
2799 IrTcl_Obj *p = obj->parent;
2801 Z_ext_typeent *etype;
2803 IrTcl_RecordList *rl;
2809 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2810 " position ?mask? ...\"", NULL);
2813 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2815 rl = find_IR_record (obj, offset);
2818 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2821 if (rl->which != Z_NamePlusRecord_databaseRecord)
2823 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2826 if (!rl->u.dbrec.buf || rl->u.dbrec.type != VAL_EXPLAIN)
2829 if (!(etype = z_ext_getentbyref (VAL_EXPLAIN)))
2831 assert (rl->u.dbrec.buf);
2832 odr_setbuf (p->odr_in, rl->u.dbrec.buf, rl->u.dbrec.size, 0);
2833 if (!(*etype->fun)(p->odr_in, (char **) &rr, 0, 0))
2836 if (etype->what != Z_External_explainRecord)
2839 return ir_tcl_get_explain (interp, rr, argc, argv);
2843 * do_responseStatus: Return response status (present or search)
2845 static int do_responseStatus (void *o, Tcl_Interp *interp,
2846 int argc, char **argv)
2848 IrTcl_SetObj *obj = o;
2852 obj->recordFlag = 0;
2853 obj->nonSurrogateDiagnosticNum = 0;
2854 obj->nonSurrogateDiagnosticList = NULL;
2857 else if (argc == -1)
2859 ir_deleteDiags (&obj->nonSurrogateDiagnosticList,
2860 &obj->nonSurrogateDiagnosticNum);
2863 if (!obj->recordFlag)
2865 Tcl_AppendElement (interp, "OK");
2870 case Z_Records_DBOSD:
2871 Tcl_AppendElement (interp, "DBOSD");
2874 Tcl_AppendElement (interp, "NSD");
2875 return ir_diagResult (interp, obj->nonSurrogateDiagnosticList,
2876 obj->nonSurrogateDiagnosticNum);
2877 case Z_Records_multipleNSD:
2878 Tcl_AppendElement (interp, "NSD");
2879 return ir_diagResult (interp, obj->nonSurrogateDiagnosticList,
2880 obj->nonSurrogateDiagnosticNum);
2886 * do_present: Perform Present Request
2889 static int do_present (void *o, Tcl_Interp *interp, int argc, char **argv)
2891 IrTcl_SetObj *obj = o;
2894 Z_PresentRequest *req;
2902 if (Tcl_GetInt (interp, argv[2], &start) == TCL_ERROR)
2909 if (Tcl_GetInt (interp, argv[3], &number) == TCL_ERROR)
2914 logf (LOG_DEBUG, "present %s %d %d", *argv, start, number);
2918 Tcl_AppendResult (interp, "not connected", NULL);
2919 return ir_tcl_error_exec (interp, argc, argv);
2922 obj->number = number;
2924 apdu = zget_APDU (p->odr_out, Z_APDU_presentRequest);
2925 req = apdu->u.presentRequest;
2927 set_referenceId (p->odr_out, &req->referenceId,
2928 obj->set_inher.referenceId);
2930 req->resultSetId = obj->setName ? obj->setName : "Default";
2932 req->resultSetStartPoint = &start;
2933 req->numberOfRecordsRequested = &number;
2934 if (obj->set_inher.preferredRecordSyntax)
2936 struct oident ident;
2938 ident.proto = p->protocol_type;
2939 ident.oclass = CLASS_RECSYN;
2940 ident.value = *obj->set_inher.preferredRecordSyntax;
2941 logf (LOG_DEBUG, "Preferred record syntax is %d", ident.value);
2942 req->preferredRecordSyntax = odr_oiddup (p->odr_out,
2943 oid_getoidbyent (&ident));
2946 req->preferredRecordSyntax = 0;
2948 if (obj->set_inher.elementSetNames && *obj->set_inher.elementSetNames)
2950 Z_ElementSetNames *esn = odr_malloc (p->odr_out, sizeof(*esn));
2951 Z_RecordComposition *compo = odr_malloc (p->odr_out, sizeof(*compo));
2953 esn->which = Z_ElementSetNames_generic;
2954 esn->u.generic = obj->set_inher.elementSetNames;
2956 req->recordComposition = compo;
2957 compo->which = Z_RecordComp_simple;
2958 compo->u.simple = esn;
2961 req->recordComposition = NULL;
2962 return ir_tcl_send_APDU (interp, p, apdu, "present", *argv);
2965 #define IR_TCL_RECORD_ENCODING_ISO2709 1
2966 #define IR_TCL_RECORD_ENCODING_RAW 2
2972 } IrTcl_FileRecordHead;
2975 * do_loadFile: Load result set from file
2977 static int do_loadFile (void *o, Tcl_Interp *interp,
2978 int argc, char **argv)
2980 IrTcl_SetObj *setobj = o;
2992 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2993 " filename ?start? ?number?\"", NULL);
2997 start = atoi (argv[3]);
2999 number = atoi (argv[4]);
3002 inf = fopen (argv[2], "r");
3005 Tcl_AppendResult (interp, "Cannot open ", argv[2], NULL);
3006 return ir_tcl_error_exec (interp, argc, argv);
3008 while (offset < (start+number))
3010 IrTcl_FileRecordHead head;
3011 IrTcl_RecordList *rl;
3013 if (fread (&head, sizeof(head), 1, inf) < 1)
3015 rl = new_IR_record (setobj, offset,
3016 Z_NamePlusRecord_databaseRecord,
3017 (argc > 5) ? argv[5] : NULL);
3018 rl->u.dbrec.type = head.syntax;
3019 if (head.encoding == IR_TCL_RECORD_ENCODING_ISO2709)
3021 if (!(buf = ir_tcl_fread_marc (inf, &size)))
3023 rl->u.dbrec.buf = buf;
3024 rl->u.dbrec.size = size;
3025 if (size != head.size)
3028 Tcl_AppendResult (interp, "bad ISO2709 encoding", NULL);
3029 return ir_tcl_error_exec (interp, argc, argv);
3032 else if (head.encoding == IR_TCL_RECORD_ENCODING_RAW)
3034 rl->u.dbrec.size = head.size;
3035 rl->u.dbrec.buf = ir_tcl_malloc (head.size + 1);
3036 if (fread (rl->u.dbrec.buf, rl->u.dbrec.size, 1, inf) < 1)
3039 Tcl_AppendResult (interp, "bad raw encoding", NULL);
3040 return ir_tcl_error_exec (interp, argc, argv);
3042 rl->u.dbrec.buf[rl->u.dbrec.size] = '\0';
3046 rl->u.dbrec.buf = NULL;
3047 rl->u.dbrec.size = 0;
3049 Tcl_AppendResult (interp, "bad encoding", NULL);
3050 return ir_tcl_error_exec (interp, argc, argv);
3054 setobj->numberOfRecordsReturned = offset - start;
3060 * do_saveFile: Save result set on file
3062 static int do_saveFile (void *o, Tcl_Interp *interp,
3063 int argc, char **argv)
3065 IrTcl_SetObj *setobj = o;
3070 IrTcl_RecordList *rl;
3076 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
3077 " filename ?start? ?number?\"", NULL);
3081 start = atoi (argv[3]);
3083 number = atoi (argv[4]);
3086 outf = fopen (argv[2], "w");
3089 Tcl_AppendResult (interp, "cannot open file", NULL);
3090 return ir_tcl_error_exec (interp, argc, argv);
3092 while (offset < (start+number) && (rl = find_IR_record (setobj, offset)))
3094 if (rl->which == Z_NamePlusRecord_databaseRecord &&
3095 rl->u.dbrec.buf && rl->u.dbrec.size)
3097 IrTcl_FileRecordHead head;
3099 head.encoding = IR_TCL_RECORD_ENCODING_RAW;
3100 head.syntax = rl->u.dbrec.type;
3101 head.size = rl->u.dbrec.size;
3102 if (fwrite (&head, sizeof(head), 1, outf) < 1)
3104 Tcl_AppendResult (interp, "cannot write", NULL);
3105 return ir_tcl_error_exec (interp, argc, argv);
3107 if (fwrite (rl->u.dbrec.buf, rl->u.dbrec.size, 1, outf) < 1)
3109 Tcl_AppendResult (interp, "cannot write", NULL);
3110 return ir_tcl_error_exec (interp, argc, argv);
3117 Tcl_AppendResult (interp, "cannot write ", NULL);
3118 return ir_tcl_error_exec (interp, argc, argv);
3124 /* ------------------------------------------------------- */
3126 * do_sort: Do sort request
3128 static int do_sort (void *o, Tcl_Interp *interp, int argc, char **argv)
3132 IrTcl_SetObj *obj = o;
3134 char sort_string[64], sort_flags[64];
3137 Z_SortKeySpecList *sksl;
3148 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1], "query\"",
3152 logf (LOG_DEBUG, "sort %s %s", *argv, argv[2]);
3155 Tcl_AppendResult (interp, "not connected", NULL);
3156 return ir_tcl_error_exec (interp, argc, argv);
3158 apdu = zget_APDU (p->odr_out, Z_APDU_sortRequest);
3159 sksl = (Z_SortKeySpecList *) odr_malloc (p->odr_out, sizeof(*sksl));
3160 req = apdu->u.sortRequest;
3162 set_referenceId (p->odr_out, &req->referenceId,
3163 obj->set_inher.referenceId);
3166 req->num_inputResultSetNames = 1;
3167 req->inputResultSetNames = (Z_InternationalString **)
3168 odr_malloc (p->odr_out, sizeof(*req->inputResultSetNames));
3169 req->inputResultSetNames[0] = obj->setName;
3171 req->inputResultSetNames =
3172 (Z_StringList *)odr_malloc (p->odr_out,
3173 sizeof(*req->inputResultSetNames));
3174 req->inputResultSetNames->num_strings = 1;
3175 req->inputResultSetNames->strings =
3176 (char **)odr_malloc (p->odr_out,
3177 sizeof(*req->inputResultSetNames->strings));
3178 req->inputResultSetNames->strings[0] = obj->setName;
3181 req->sortedResultSetName = (char *) obj->setName;
3184 req->sortSequence = sksl;
3185 sksl->num_specs = 0;
3186 sksl->specs = (Z_SortKeySpec **)
3187 odr_malloc (p->odr_out, sizeof(sksl->specs) * 20);
3189 bib1.proto = PROTO_Z3950;
3190 bib1.oclass = CLASS_ATTSET;
3191 bib1.value = VAL_BIB1;
3193 while ((sscanf (arg, "%63s %63s%n", sort_string, sort_flags, &off)) == 2
3197 char *sort_string_sep;
3198 Z_SortKeySpec *sks = (Z_SortKeySpec *)
3199 odr_malloc (p->odr_out, sizeof(*sks));
3200 Z_SortKey *sk = (Z_SortKey *)
3201 odr_malloc (p->odr_out, sizeof(*sk));
3204 sksl->specs[sksl->num_specs++] = sks;
3205 sks->sortElement = (Z_SortElement *)
3206 odr_malloc (p->odr_out, sizeof(*sks->sortElement));
3207 sks->sortElement->which = Z_SortElement_generic;
3208 sks->sortElement->u.generic = sk;
3210 if ((sort_string_sep = strchr (sort_string, '=')))
3212 Z_AttributeElement *el = (Z_AttributeElement *)
3213 odr_malloc (p->odr_out, sizeof(*el));
3214 sk->which = Z_SortKey_sortAttributes;
3215 sk->u.sortAttributes =
3216 (Z_SortAttributes *)
3217 odr_malloc (p->odr_out, sizeof(*sk->u.sortAttributes));
3218 sk->u.sortAttributes->id = oid_ent_to_oid(&bib1, oid);
3219 sk->u.sortAttributes->list =
3221 odr_malloc (p->odr_out, sizeof(*sk->u.sortAttributes->list));
3222 sk->u.sortAttributes->list->num_attributes = 1;
3223 sk->u.sortAttributes->list->attributes =
3224 (Z_AttributeElement **)odr_malloc (p->odr_out,
3225 sizeof(*sk->u.sortAttributes->list->attributes));
3226 sk->u.sortAttributes->list->attributes[0] = el;
3227 el->attributeSet = 0;
3228 el->attributeType = (int *)
3229 odr_malloc (p->odr_out, sizeof(*el->attributeType));
3230 *el->attributeType = atoi (sort_string);
3231 el->which = Z_AttributeValue_numeric;
3232 el->value.numeric = (int *)
3233 odr_malloc (p->odr_out, sizeof(*el->value.numeric));
3234 *el->value.numeric = atoi (sort_string_sep + 1);
3238 sk->which = Z_SortKey_sortField;
3239 sk->u.sortField = (char *)odr_malloc (p->odr_out, strlen(sort_string)+1);
3240 strcpy (sk->u.sortField, sort_string);
3242 sks->sortRelation = (int *)
3243 odr_malloc (p->odr_out, sizeof(*sks->sortRelation));
3244 *sks->sortRelation = Z_SortRelation_ascending;
3245 sks->caseSensitivity = (int *)
3246 odr_malloc (p->odr_out, sizeof(*sks->caseSensitivity));
3247 *sks->caseSensitivity = Z_SortCase_caseSensitive;
3250 sks->which = Z_SortKeySpec_null;
3251 sks->u.null = odr_nullval ();
3253 sks->missingValueAction = NULL;
3256 for (i = 0; sort_flags[i]; i++)
3258 switch (sort_flags[i])
3263 *sks->sortRelation = Z_SortRelation_descending;
3268 *sks->sortRelation = Z_SortRelation_ascending;
3272 *sks->caseSensitivity = Z_SortCase_caseInsensitive;
3276 *sks->caseSensitivity = Z_SortCase_caseSensitive;
3281 if (!sksl->num_specs)
3283 printf ("Missing sort specifications\n");
3286 return ir_tcl_send_APDU (interp, p, apdu, "sort", *argv);
3289 static IrTcl_Method ir_set_method_tab[] = {
3290 { "search", do_search, NULL},
3291 { "searchResponse", do_searchResponse, NULL},
3292 { "presentResponse", do_presentResponse, NULL},
3293 { "searchStatus", do_searchStatus, NULL},
3294 { "presentStatus", do_presentStatus, NULL},
3295 { "nextResultSetPosition", do_nextResultSetPosition, NULL},
3296 { "setName", do_setName, NULL},
3297 { "resultCount", do_resultCount, NULL},
3298 { "numberOfRecordsReturned", do_numberOfRecordsReturned, NULL},
3299 { "present", do_present, NULL},
3300 { "type", do_type, NULL},
3301 { "getMarc", do_getMarc, NULL},
3302 { "getSutrs", do_getSutrs, NULL},
3303 { "getGrs", do_getGrs, NULL},
3304 { "getExplain", do_getExplain, NULL},
3305 { "recordType", do_recordType, NULL},
3306 { "recordElements", do_recordElements, NULL},
3307 { "diag", do_diag, NULL},
3308 { "responseStatus", do_responseStatus, NULL},
3309 { "loadFile", do_loadFile, NULL},
3310 { "saveFile", do_saveFile, NULL},
3311 { "sort", do_sort, NULL },
3312 { "sortResponse", do_sortResponse, NULL},
3313 { "sortStatus", do_sortStatus, NULL},
3314 { "searchResult", do_searchResult, NULL},
3319 * ir_set_obj_method: IR Set Object methods
3321 static int ir_set_obj_method (ClientData clientData, Tcl_Interp *interp,
3322 int argc, char **argv)
3324 IrTcl_Methods tabs[3];
3325 IrTcl_SetObj *p = (IrTcl_SetObj *) clientData;
3330 Tcl_AppendResult (interp, wrongArgs, *argv, " method args...\"", NULL);
3333 tabs[0].tab = ir_set_method_tab;
3335 tabs[1].tab = ir_set_c_method_tab;
3336 tabs[1].obj = &p->set_inher;
3339 if (ir_tcl_method (interp, argc, argv, tabs, &r) == TCL_ERROR)
3340 return ir_tcl_method_error (interp, argc, argv, tabs);
3345 * ir_set_obj_delete: IR Set Object disposal
3347 static void ir_set_obj_delete (ClientData clientData)
3349 IrTcl_Methods tabs[3];
3350 IrTcl_SetObj *p = (IrTcl_SetObj *) clientData;
3352 logf (LOG_DEBUG, "ir set delete");
3354 tabs[0].tab = ir_set_method_tab;
3356 tabs[1].tab = ir_set_c_method_tab;
3357 tabs[1].obj = &p->set_inher;
3360 ir_tcl_method (NULL, -1, NULL, tabs, NULL);
3366 * ir_set_obj_init: IR Set Object initialization
3368 static int ir_set_obj_init (ClientData clientData, Tcl_Interp *interp,
3369 int argc, char **argv, ClientData *subData,
3370 ClientData parentData)
3372 IrTcl_Methods tabs[3];
3375 if (argc < 2 || argc > 3)
3377 Tcl_AppendResult (interp, wrongArgs, *argv,
3378 " objSetName ?objParent?\"", NULL);
3381 obj = ir_tcl_malloc (sizeof(*obj));
3382 logf (LOG_DEBUG, "ir set create %s", argv[1]);
3389 obj->parent = (IrTcl_Obj *) parentData;
3391 dst = &obj->set_inher;
3392 src = &obj->parent->set_inher;
3394 if ((dst->num_databaseNames = src->num_databaseNames))
3396 dst->databaseNames =
3397 ir_tcl_malloc (sizeof (*dst->databaseNames)
3398 * (1+dst->num_databaseNames));
3399 for (i = 0; i < dst->num_databaseNames; i++)
3400 if (ir_tcl_strdup (interp, &dst->databaseNames[i],
3401 src->databaseNames[i]) == TCL_ERROR)
3403 dst->databaseNames[i] = NULL;
3406 dst->databaseNames = NULL;
3407 if (ir_tcl_strdup (interp, &dst->queryType, src->queryType)
3411 if (ir_tcl_strdup (interp, &dst->referenceId, src->referenceId)
3415 if (ir_tcl_strdup (interp, &dst->elementSetNames, src->elementSetNames)
3419 if (ir_tcl_strdup (interp, &dst->smallSetElementSetNames,
3420 src->smallSetElementSetNames)
3424 if (ir_tcl_strdup (interp, &dst->mediumSetElementSetNames,
3425 src->mediumSetElementSetNames)
3429 if (src->preferredRecordSyntax &&
3430 (dst->preferredRecordSyntax
3431 = ir_tcl_malloc (sizeof(*dst->preferredRecordSyntax))))
3432 *dst->preferredRecordSyntax = *src->preferredRecordSyntax;
3434 dst->preferredRecordSyntax = NULL;
3435 dst->replaceIndicator = src->replaceIndicator;
3436 dst->smallSetUpperBound = src->smallSetUpperBound;
3437 dst->largeSetLowerBound = src->largeSetLowerBound;
3438 dst->mediumSetPresentNumber = src->mediumSetPresentNumber;
3443 tabs[0].tab = ir_set_method_tab;
3447 if (ir_tcl_method (interp, 0, NULL, tabs, NULL) == TCL_ERROR)
3450 *subData = (ClientData) obj;
3455 * ir_set_obj_mk: IR Set Object creation
3457 static int ir_set_obj_mk (ClientData clientData, Tcl_Interp *interp,
3458 int argc, char **argv)
3461 ClientData parentData = 0;
3466 Tcl_CmdInfo parent_info;
3467 if (!Tcl_GetCommandInfo (interp, argv[2], &parent_info))
3469 Tcl_AppendResult (interp, "no object parent", NULL);
3470 return ir_tcl_error_exec (interp, argc, argv);
3472 parentData = parent_info.clientData;
3474 r = ir_set_obj_init (clientData, interp, argc, argv, &subData, parentData);
3477 Tcl_CreateCommand (interp, argv[1], ir_set_obj_method,
3478 subData, ir_set_obj_delete);
3482 IrTcl_Class ir_set_obj_class = {
3489 /* ------------------------------------------------------- */
3492 * do_scan: Perform scan
3494 static int do_scan (void *o, Tcl_Interp *interp, int argc, char **argv)
3498 IrTcl_ScanObj *obj = o;
3499 IrTcl_Obj *p = obj->parent;
3504 struct ccl_rpn_node *rpn;
3508 #if TCL_MAJOR_VERSION > 8 || (TCL_MAJOR_VERSION == 8 && TCL_MINOR_VERSION > 0)
3516 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
3517 " scanQuery\"", NULL);
3520 #if TCL_MAJOR_VERSION > 8 || (TCL_MAJOR_VERSION == 8 && TCL_MINOR_VERSION > 0)
3521 start_term = Tcl_UtfToExternalDString(0, argv[2], -1, &ds);
3523 start_term = argv[2];
3525 logf (LOG_DEBUG, "scan %s %s", *argv, start_term);
3526 if (!p->set_inher.num_databaseNames)
3528 Tcl_AppendResult (interp, "no databaseNames", NULL);
3529 code = ir_tcl_error_exec (interp, argc, argv);
3534 Tcl_AppendResult (interp, "not connected", NULL);
3535 code = ir_tcl_error_exec (interp, argc, argv);
3539 apdu = zget_APDU (p->odr_out, Z_APDU_scanRequest);
3540 req = apdu->u.scanRequest;
3542 set_referenceId (p->odr_out, &req->referenceId, p->set_inher.referenceId);
3543 req->num_databaseNames = p->set_inher.num_databaseNames;
3544 req->databaseNames = p->set_inher.databaseNames;
3548 if (!(req->termListAndStartPoint =
3549 p_query_scan (p->odr_out, p->protocol_type,
3550 &req->attributeSet, start_term)))
3552 Tcl_AppendResult (interp, "query syntax error", NULL);
3553 code = ir_tcl_error_exec (interp, argc, argv);
3557 rpn = ccl_find_str(p->bibset, start_term, &r, &pos);
3560 Tcl_AppendResult (interp, "ccl syntax error ", ccl_err_msg(r), NULL);
3561 code = ir_tcl_error_exec (interp, argc, argv);
3564 bib1.proto = p->protocol_type;
3565 bib1.oclass = CLASS_ATTSET;
3566 bib1.value = VAL_BIB1;
3568 req->attributeSet = oid_getoidbyent (&bib1);
3569 if (!(req->termListAndStartPoint = ccl_scan_query (p->odr_out, rpn)))
3575 req->stepSize = &obj->stepSize;
3576 req->numberOfTermsRequested = &obj->numberOfTermsRequested;
3577 req->preferredPositionInResponse = &obj->preferredPositionInResponse;
3578 logf (LOG_DEBUG, "stepSize=%d", *req->stepSize);
3579 logf (LOG_DEBUG, "numberOfTermsRequested=%d",
3580 *req->numberOfTermsRequested);
3581 logf (LOG_DEBUG, "preferredPositionInResponse=%d",
3582 *req->preferredPositionInResponse);
3584 code = ir_tcl_send_APDU (interp, p, apdu, "scan", *argv);
3586 #if TCL_MAJOR_VERSION > 8 || (TCL_MAJOR_VERSION == 8 && TCL_MINOR_VERSION > 0)
3587 Tcl_DStringFree (&ds);
3593 * do_scanResponse: add scan response handler
3595 static int do_scanResponse (void *o, Tcl_Interp *interp,
3596 int argc, char **argv)
3598 IrTcl_ScanObj *obj = o;
3602 obj->scanResponse = NULL;
3605 else if (argc == -1)
3606 return ir_tcl_strdel (interp, &obj->scanResponse);
3609 xfree (obj->scanResponse);
3612 if (ir_tcl_strdup (interp, &obj->scanResponse, argv[2])
3617 obj->scanResponse = NULL;
3623 * do_stepSize: Set/get replace Step Size
3625 static int do_stepSize (void *obj, Tcl_Interp *interp,
3626 int argc, char **argv)
3628 IrTcl_ScanObj *p = obj;
3634 return ir_tcl_get_set_int (&p->stepSize, interp, argc, argv);
3638 * do_numberOfTermsRequested: Set/get Number of Terms requested
3640 static int do_numberOfTermsRequested (void *obj, Tcl_Interp *interp,
3641 int argc, char **argv)
3643 IrTcl_ScanObj *p = obj;
3647 p->numberOfTermsRequested = 20;
3650 return ir_tcl_get_set_int (&p->numberOfTermsRequested, interp, argc, argv);
3655 * do_preferredPositionInResponse: Set/get preferred Position
3657 static int do_preferredPositionInResponse (void *obj, Tcl_Interp *interp,
3658 int argc, char **argv)
3660 IrTcl_ScanObj *p = obj;
3664 p->preferredPositionInResponse = 1;
3667 return ir_tcl_get_set_int (&p->preferredPositionInResponse, interp,
3672 * do_scanStatus: Get scan status
3674 static int do_scanStatus (void *obj, Tcl_Interp *interp,
3675 int argc, char **argv)
3677 IrTcl_ScanObj *p = obj;
3681 return ir_tcl_get_set_int (&p->scanStatus, interp, argc, argv);
3685 * do_numberOfEntriesReturned: Get number of Entries returned
3687 static int do_numberOfEntriesReturned (void *obj, Tcl_Interp *interp,
3688 int argc, char **argv)
3690 IrTcl_ScanObj *p = obj;
3694 return ir_tcl_get_set_int (&p->numberOfEntriesReturned, interp,
3699 * do_positionOfTerm: Get position of Term
3701 static int do_positionOfTerm (void *obj, Tcl_Interp *interp,
3702 int argc, char **argv)
3704 IrTcl_ScanObj *p = obj;
3708 return ir_tcl_get_set_int (&p->positionOfTerm, interp, argc, argv);
3712 * do_scanLine: get Scan Line (surrogate or normal) after response
3714 static int do_scanLine (void *obj, Tcl_Interp *interp, int argc, char **argv)
3716 IrTcl_ScanObj *p = obj;
3722 p->entries_flag = 0;
3724 p->nonSurrogateDiagnosticNum = 0;
3725 p->nonSurrogateDiagnosticList = 0;
3728 else if (argc == -1)
3730 p->entries_flag = 0;
3731 /* release entries */
3734 ir_deleteDiags (&p->nonSurrogateDiagnosticList,
3735 &p->nonSurrogateDiagnosticNum);
3740 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
3741 " position\"", NULL);
3744 if (Tcl_GetInt (interp, argv[2], &i) == TCL_ERROR)
3746 if (!p->entries_flag || !p->entries || i >= p->num_entries || i < 0)
3748 switch (p->entries[i].which)
3750 case Z_Entry_termInfo:
3751 Tcl_AppendElement (interp, "T");
3752 if (p->entries[i].u.term.buf)
3753 Tcl_AppendElement (interp, p->entries[i].u.term.buf);
3755 Tcl_AppendElement (interp, "");
3756 sprintf (numstr, "%d", p->entries[i].u.term.globalOccurrences);
3757 Tcl_AppendElement (interp, numstr);
3759 case Z_Entry_surrogateDiagnostic:
3760 Tcl_AppendElement (interp, "SD");
3761 return ir_diagResult (interp, p->entries[i].u.diag.list,
3762 p->entries[i].u.diag.num);
3768 static IrTcl_Method ir_scan_method_tab[] = {
3769 { "scan", do_scan, NULL},
3770 { "scanResponse", do_scanResponse, NULL},
3771 { "stepSize", do_stepSize, NULL},
3772 { "numberOfTermsRequested", do_numberOfTermsRequested, NULL},
3773 { "preferredPositionInResponse", do_preferredPositionInResponse, NULL},
3774 { "scanStatus", do_scanStatus, NULL},
3775 { "numberOfEntriesReturned", do_numberOfEntriesReturned, NULL},
3776 { "positionOfTerm", do_positionOfTerm, NULL},
3777 { "scanLine", do_scanLine, NULL},
3782 * ir_scan_obj_method: IR Scan Object methods
3784 static int ir_scan_obj_method (ClientData clientData, Tcl_Interp *interp,
3785 int argc, char **argv)
3787 IrTcl_Methods tabs[2];
3792 Tcl_AppendResult (interp, wrongArgs, *argv, " method args...\"", NULL);
3795 tabs[0].tab = ir_scan_method_tab;
3796 tabs[0].obj = clientData;
3799 if (ir_tcl_method (interp, argc, argv, tabs, &r) == TCL_ERROR)
3800 return ir_tcl_method_error (interp, argc, argv, tabs);
3805 * ir_scan_obj_delete: IR Scan Object disposal
3807 static void ir_scan_obj_delete (ClientData clientData)
3809 IrTcl_Methods tabs[2];
3810 IrTcl_ScanObj *obj = (IrTcl_ScanObj *) clientData;
3812 tabs[0].tab = ir_scan_method_tab;
3816 ir_tcl_method (NULL, -1, NULL, tabs, NULL);
3821 * ir_scan_obj_mk: IR Scan Object creation
3823 static int ir_scan_obj_mk (ClientData clientData, Tcl_Interp *interp,
3824 int argc, char **argv)
3826 Tcl_CmdInfo parent_info;
3828 IrTcl_Methods tabs[2];
3832 Tcl_AppendResult (interp, wrongArgs, *argv,
3833 "objScanName objParentName\"", NULL);
3836 logf (LOG_DEBUG, "ir scan create %s", argv[1]);
3837 if (!Tcl_GetCommandInfo (interp, argv[2], &parent_info))
3839 Tcl_AppendResult (interp, "no object parent", NULL);
3840 return ir_tcl_error_exec (interp, argc, argv);
3842 obj = ir_tcl_malloc (sizeof(*obj));
3843 obj->parent = (IrTcl_Obj *) parent_info.clientData;
3845 tabs[0].tab = ir_scan_method_tab;
3849 if (ir_tcl_method (interp, 0, NULL, tabs, NULL) == TCL_ERROR)
3851 Tcl_CreateCommand (interp, argv[1], ir_scan_obj_method,
3852 (ClientData) obj, ir_scan_obj_delete);
3856 /* ------------------------------------------------------- */
3859 * ir_log_init_proc: set yaz log level
3861 static int ir_log_init_proc (ClientData clientData, Tcl_Interp *interp,
3862 int argc, char **argv)
3865 if (argc <= 1 || argc > 4)
3867 Tcl_AppendResult (interp, wrongArgs, *argv,
3868 " ?level ?prefix ?filename\"", NULL);
3871 lev = yaz_log_mask_str (argv[1]);
3873 yaz_log_init (lev, "", NULL);
3875 yaz_log_init (lev, argv[2], NULL);
3877 yaz_log_init (lev, argv[2], argv[3]);
3878 if (lev & LOG_DEBUG)
3879 odr_print_file = yaz_log_file();
3886 * ir_log_proc: log yaz message
3888 static int ir_log_proc (ClientData clientData, Tcl_Interp *interp,
3889 int argc, char **argv)
3894 Tcl_AppendResult (interp, wrongArgs, *argv,
3895 " level string\"", NULL);
3898 mask = yaz_log_mask_str_x (argv[1], 0);
3899 logf (LOG_DEBUG, "%s", argv[2]);
3905 * ir_version: log ir version
3907 static int ir_version (ClientData clientData, Tcl_Interp *interp,
3908 int argc, char **argv)
3910 Tcl_AppendElement (interp, IR_TCL_VERSION);
3911 Tcl_AppendElement (interp, YAZ_VERSION);
3916 /* ------------------------------------------------------- */
3917 static void ir_initResponse (void *obj, Z_InitResponse *initrs)
3921 p->initResult = *initrs->result ? 1 : 0;
3922 if (!*initrs->result)
3923 logf (LOG_DEBUG, "Connection rejected by target");
3925 logf (LOG_DEBUG, "Connection accepted by target");
3927 get_referenceId (&p->set_inher.referenceId, initrs->referenceId);
3929 xfree (p->targetImplementationId);
3930 ir_tcl_strdup (p->interp, &p->targetImplementationId,
3931 initrs->implementationId);
3932 xfree (p->targetImplementationName);
3933 ir_tcl_strdup (p->interp, &p->targetImplementationName,
3934 initrs->implementationName);
3935 xfree (p->targetImplementationVersion);
3936 ir_tcl_strdup (p->interp, &p->targetImplementationVersion,
3937 initrs->implementationVersion);
3939 p->maximumRecordSize = *initrs->maximumRecordSize;
3940 p->preferredMessageSize = *initrs->preferredMessageSize;
3942 memcpy (&p->options, initrs->options, sizeof(initrs->options));
3943 memcpy (&p->protocolVersion, initrs->protocolVersion,
3944 sizeof(initrs->protocolVersion));
3945 xfree (p->userInformationField);
3946 p->userInformationField = NULL;
3947 if (initrs->userInformationField)
3951 if (initrs->userInformationField->which == ODR_EXTERNAL_octet &&
3952 (p->userInformationField =
3953 ir_tcl_malloc ((len =
3954 initrs->userInformationField->
3955 u.octet_aligned->len) +1)))
3957 memcpy (p->userInformationField,
3958 initrs->userInformationField->u.octet_aligned->buf,
3960 (p->userInformationField)[len] = '\0';
3965 static void ir_deleteDiags (IrTcl_Diagnostic **dst_list, int *dst_num)
3968 for (i = 0; i<*dst_num; i++)
3969 xfree ((*dst_list)[i].addinfo);
3975 static void ir_handleDiags (IrTcl_Diagnostic **dst_list, int *dst_num,
3976 Z_DiagRec **list, int num)
3979 char *addinfo = NULL;
3982 *dst_list = ir_tcl_malloc (sizeof(**dst_list) * num);
3983 for (i = 0; i<num; i++)
3986 switch (list[i]->which)
3988 case Z_DiagRec_defaultFormat:
3989 (*dst_list)[i].condition = *list[i]->u.defaultFormat->condition;
3991 switch (list[i]->u.defaultFormat->which)
3993 case Z_DefaultDiagFormat_v2Addinfo:
3994 addinfo = list[i]->u.defaultFormat->u.v2Addinfo;
3996 case Z_DefaultDiagFormat_v3Addinfo:
3997 addinfo = list[i]->u.defaultFormat->u.v3Addinfo;
4001 addinfo = list[i]->u.defaultFormat->addinfo;
4004 ((*dst_list)[i].addinfo = ir_tcl_malloc (strlen(addinfo)+1)))
4005 strcpy ((*dst_list)[i].addinfo, addinfo);
4006 cp = diagbib1_str ((*dst_list)[i].condition);
4007 logf (LOG_DEBUG, "Diag %d %s %s", (*dst_list)[i].condition,
4008 cp ? cp : "", addinfo ? addinfo : "");
4011 (*dst_list)[i].addinfo = NULL;
4012 (*dst_list)[i].condition = 0;
4017 static void ir_handleDBRecord (IrTcl_Obj *p, IrTcl_RecordList *rl,
4020 struct oident *ident;
4021 Z_ext_typeent *etype;
4023 logf (LOG_DEBUG, "handleDBRecord size=%d", oe->u.octet_aligned->len);
4024 rl->u.dbrec.size = oe->u.octet_aligned->len;
4025 rl->u.dbrec.buf = NULL;
4027 if ((ident = oid_getentbyoid (oe->direct_reference)))
4028 rl->u.dbrec.type = ident->value;
4030 rl->u.dbrec.type = VAL_USMARC;
4032 if (ident && (oe->which == Z_External_single ||
4033 oe->which == Z_External_octet)
4034 && (etype = z_ext_getentbyref (ident->value)))
4038 odr_setbuf (p->odr_in, (char*) oe->u.octet_aligned->buf,
4039 oe->u.octet_aligned->len, 0);
4040 if (!(*etype->fun)(p->odr_in, (char **) &rr, 0, 0))
4042 rl->u.dbrec.type = VAL_NONE;
4045 switch (etype->what)
4047 case Z_External_sutrs:
4048 logf (LOG_DEBUG, "Z_External_sutrs");
4050 if ((rl->u.dbrec.buf = ir_tcl_malloc (oe->u.sutrs->len+1)))
4052 memcpy (rl->u.dbrec.buf, oe->u.sutrs->buf,
4054 rl->u.dbrec.buf[oe->u.sutrs->len] = '\0';
4056 rl->u.dbrec.size = oe->u.sutrs->len;
4058 case Z_External_grs1:
4059 logf (LOG_DEBUG, "Z_External_grs1");
4061 ir_tcl_grs_mk (oe->u.grs1, &rl->u.dbrec.u.grs1);
4063 case Z_External_explainRecord:
4064 logf (LOG_DEBUG, "Z_External_explainRecord");
4065 if ((rl->u.dbrec.buf = ir_tcl_malloc (rl->u.dbrec.size)))
4067 memcpy (rl->u.dbrec.buf, oe->u.octet_aligned->buf,
4075 if (oe->which == Z_External_octet && rl->u.dbrec.size > 0)
4077 char *buf = (char*) oe->u.octet_aligned->buf;
4078 if ((rl->u.dbrec.buf = ir_tcl_malloc (rl->u.dbrec.size)))
4079 memcpy (rl->u.dbrec.buf, buf, rl->u.dbrec.size);
4081 else if (rl->u.dbrec.type == VAL_SUTRS &&
4082 oe->which == Z_External_sutrs)
4084 if ((rl->u.dbrec.buf = ir_tcl_malloc (oe->u.sutrs->len+1)))
4086 memcpy (rl->u.dbrec.buf, oe->u.sutrs->buf,
4088 rl->u.dbrec.buf[oe->u.sutrs->len] = '\0';
4090 rl->u.dbrec.size = oe->u.sutrs->len;
4092 else if (rl->u.dbrec.type == VAL_GRS1 &&
4093 oe->which == Z_External_grs1)
4095 ir_tcl_grs_mk (oe->u.grs1, &rl->u.dbrec.u.grs1);
4100 static void ir_handleZRecords (void *o, Z_Records *zrs, IrTcl_SetObj *setobj,
4101 const char *elements)
4106 IrTcl_RecordList *rl;
4108 setobj->which = zrs->which;
4109 setobj->recordFlag = 1;
4111 ir_deleteDiags (&setobj->nonSurrogateDiagnosticList,
4112 &setobj->nonSurrogateDiagnosticNum);
4113 if (zrs->which == Z_Records_DBOSD)
4115 int num_rec = zrs->u.databaseOrSurDiagnostics->num_records;
4117 if (num_rec != setobj->numberOfRecordsReturned)
4119 logf (LOG_WARN, "numberOfRecordsReturned=%d but num records=%d",
4120 setobj->numberOfRecordsReturned, num_rec);
4121 setobj->numberOfRecordsReturned = num_rec;
4124 for (offset = 0; offset < num_rec; offset++)
4126 Z_NamePlusRecord *znpr = zrs->u.databaseOrSurDiagnostics->
4129 rl = new_IR_record (setobj, setobj->start + offset, znpr->which,
4131 if (rl->which == Z_NamePlusRecord_surrogateDiagnostic)
4132 ir_handleDiags (&rl->u.surrogateDiagnostics.list,
4133 &rl->u.surrogateDiagnostics.num,
4134 &znpr->u.surrogateDiagnostic,
4137 ir_handleDBRecord (p, rl,
4138 (Z_External*) (znpr->u.databaseRecord));
4141 else if (zrs->which == Z_Records_multipleNSD)
4143 logf (LOG_DEBUG, "multipleNonSurrogateDiagnostic %d",
4144 zrs->u.multipleNonSurDiagnostics->num_diagRecs);
4145 setobj->numberOfRecordsReturned = 0;
4146 ir_handleDiags (&setobj->nonSurrogateDiagnosticList,
4147 &setobj->nonSurrogateDiagnosticNum,
4148 zrs->u.multipleNonSurDiagnostics->diagRecs,
4149 zrs->u.multipleNonSurDiagnostics->num_diagRecs);
4154 Z_DiagRec dr, *dr_p = &dr;
4155 dr.which = Z_DiagRec_defaultFormat;
4156 dr.u.defaultFormat = zrs->u.nonSurrogateDiagnostic;
4158 Z_DiagRec *dr_p = zrs->u.nonSurrogateDiagnostic;
4160 logf (LOG_DEBUG, "NonSurrogateDiagnostic");
4162 setobj->numberOfRecordsReturned = 0;
4163 ir_handleDiags (&setobj->nonSurrogateDiagnosticList,
4164 &setobj->nonSurrogateDiagnosticNum,
4169 static char *set_queryExpression (Z_QueryExpression *qe)
4174 if (qe->which == Z_QueryExpression_term)
4176 if (qe->u.term->queryTerm)
4178 Z_Term *term = qe->u.term->queryTerm;
4179 if (term->which == Z_Term_general)
4181 termz = xmalloc (term->u.general->len+1);
4182 memcpy (termz, term->u.general->buf, term->u.general->len);
4183 termz[term->u.general->len] = 0;
4190 static void set_searchResult (Z_OtherInformation *o,
4191 IrTcl_SetObj *setobj)
4196 for (i = 0; i < o->num_elements; i++)
4198 if (o->list[i]->which == Z_OtherInfo_externallyDefinedInfo)
4200 ODR odr = odr_createmem (ODR_DECODE);
4201 Z_External *ext = o->list[i]->information.externallyDefinedInfo;
4202 Z_SearchInfoReport *sr = 0;
4204 if (ext->which == Z_External_single)
4206 odr_setbuf (odr, ext->u.single_ASN1_type->buf,
4207 ext->u.single_ASN1_type->len, 0);
4208 z_SearchInfoReport (odr, &sr, 0, "searchInfo");
4210 if (ext->which == Z_External_searchResult1)
4211 sr = ext->u.searchResult1;
4215 reset_searchResult (setobj);
4217 setobj->searchResult_num = sr->num;
4218 setobj->searchResult_terms =
4219 xmalloc (sr->num * sizeof(*setobj->searchResult_terms));
4220 setobj->searchResult_count =
4221 xmalloc (sr->num * sizeof(*setobj->searchResult_count));
4223 for (j = 0; j < sr->num; j++)
4225 setobj->searchResult_terms[j] =
4226 set_queryExpression (
4227 sr->elements[j]->subqueryExpression);
4228 if (sr->elements[j]->subqueryCount)
4229 setobj->searchResult_count[j] =
4230 *sr->elements[j]->subqueryCount;
4232 setobj->searchResult_count[j] = 0;
4240 static void ir_searchResponse (void *o, Z_SearchResponse *searchrs,
4241 IrTcl_SetObj *setobj)
4243 Z_Records *zrs = searchrs->records;
4245 logf (LOG_DEBUG, "Received search response");
4248 logf (LOG_DEBUG, "Search response, no object!");
4251 setobj->searchStatus = *searchrs->searchStatus;
4252 get_referenceId (&setobj->set_inher.referenceId, searchrs->referenceId);
4253 setobj->resultCount = *searchrs->resultCount;
4254 if (searchrs->presentStatus)
4255 setobj->presentStatus = *searchrs->presentStatus;
4257 setobj->presentStatus = Z_RES_NONE;
4258 if (searchrs->nextResultSetPosition)
4259 setobj->nextResultSetPosition = *searchrs->nextResultSetPosition;
4261 logf (LOG_DEBUG, "status %d hits %d",
4262 setobj->searchStatus, setobj->resultCount);
4263 set_searchResult (searchrs->additionalSearchInfo, setobj);
4267 if (setobj->resultCount <= setobj->set_inher.smallSetUpperBound)
4268 es = setobj->set_inher.smallSetElementSetNames;
4270 es = setobj->set_inher.mediumSetElementSetNames;
4271 setobj->numberOfRecordsReturned = *searchrs->numberOfRecordsReturned;
4272 ir_handleZRecords (o, zrs, setobj, es);
4276 setobj->numberOfRecordsReturned = 0;
4277 setobj->recordFlag = 0;
4282 static void ir_presentResponse (void *o, Z_PresentResponse *presrs,
4283 IrTcl_SetObj *setobj)
4285 Z_Records *zrs = presrs->records;
4287 logf (LOG_DEBUG, "Received present response");
4290 logf (LOG_DEBUG, "Present response, no object!");
4293 setobj->presentStatus = *presrs->presentStatus;
4294 get_referenceId (&setobj->set_inher.referenceId, presrs->referenceId);
4295 setobj->nextResultSetPosition = *presrs->nextResultSetPosition;
4298 setobj->numberOfRecordsReturned = *presrs->numberOfRecordsReturned;
4299 ir_handleZRecords (o, zrs, setobj, setobj->set_inher.elementSetNames);
4303 setobj->numberOfRecordsReturned = 0;
4304 setobj->recordFlag = 0;
4305 logf (LOG_DEBUG, "No records!");
4309 static void ir_scanResponse (void *o, Z_ScanResponse *scanrs,
4310 IrTcl_ScanObj *scanobj)
4314 logf (LOG_DEBUG, "Received scanResponse");
4316 get_referenceId (&p->set_inher.referenceId, scanrs->referenceId);
4317 scanobj->scanStatus = *scanrs->scanStatus;
4318 logf (LOG_DEBUG, "scanStatus=%d", scanobj->scanStatus);
4320 if (scanrs->stepSize)
4321 scanobj->stepSize = *scanrs->stepSize;
4322 logf (LOG_DEBUG, "stepSize=%d", scanobj->stepSize);
4324 scanobj->numberOfEntriesReturned = *scanrs->numberOfEntriesReturned;
4325 logf (LOG_DEBUG, "numberOfEntriesReturned=%d",
4326 scanobj->numberOfEntriesReturned);
4328 if (scanrs->positionOfTerm)
4329 scanobj->positionOfTerm = *scanrs->positionOfTerm;
4331 scanobj->positionOfTerm = -1;
4332 logf (LOG_DEBUG, "positionOfTerm=%d", scanobj->positionOfTerm);
4334 xfree (scanobj->entries);
4335 scanobj->entries = NULL;
4336 scanobj->num_entries = 0;
4337 scanobj->entries_flag = 0;
4339 ir_deleteDiags (&scanobj->nonSurrogateDiagnosticList,
4340 &scanobj->nonSurrogateDiagnosticNum);
4341 if (scanrs->entries)
4346 scanobj->entries_flag = 1;
4347 if (scanrs->entries)
4349 scanobj->num_entries = scanrs->entries->num_entries;
4350 scanobj->entries = ir_tcl_malloc (scanobj->num_entries *
4351 sizeof(*scanobj->entries));
4352 ze = scanrs->entries->entries;
4354 for (i=0; i<scanobj->num_entries; i++, ze++)
4356 scanobj->entries[i].which = (*ze)->which;
4357 switch ((*ze)->which)
4359 case Z_Entry_termInfo:
4360 if ((*ze)->u.termInfo->term->which == Z_Term_general)
4362 int l = (*ze)->u.termInfo->term->u.general->len;
4363 scanobj->entries[i].u.term.buf = ir_tcl_malloc (1+l);
4364 memcpy (scanobj->entries[i].u.term.buf,
4365 (*ze)->u.termInfo->term->u.general->buf,
4367 scanobj->entries[i].u.term.buf[l] = '\0';
4370 scanobj->entries[i].u.term.buf = NULL;
4371 if ((*ze)->u.termInfo->globalOccurrences)
4372 scanobj->entries[i].u.term.globalOccurrences =
4373 *(*ze)->u.termInfo->globalOccurrences;
4375 scanobj->entries[i].u.term.globalOccurrences = 0;
4377 case Z_Entry_surrogateDiagnostic:
4378 ir_handleDiags (&scanobj->entries[i].u.diag.list,
4379 &scanobj->entries[i].u.diag.num,
4380 &(*ze)->u.surrogateDiagnostic,
4385 if (scanrs->entries->nonsurrogateDiagnostics)
4386 ir_handleDiags (&scanobj->nonSurrogateDiagnosticList,
4387 &scanobj->nonSurrogateDiagnosticNum,
4388 scanrs->entries->nonsurrogateDiagnostics,
4389 scanrs->entries->num_nonsurrogateDiagnostics);
4394 static void ir_sortResponse (void *o, Z_SortResponse *sortrs,
4395 IrTcl_SetObj *setobj)
4399 logf (LOG_DEBUG, "Received sortResponse");
4404 purge_IR_records (setobj);
4406 get_referenceId (&p->set_inher.referenceId, sortrs->referenceId);
4408 setobj->sortStatus = *sortrs->sortStatus;
4410 ir_deleteDiags (&setobj->nonSurrogateDiagnosticList,
4411 &setobj->nonSurrogateDiagnosticNum);
4413 if (sortrs->diagnostics)
4414 ir_handleDiags (&setobj->nonSurrogateDiagnosticList,
4415 &setobj->nonSurrogateDiagnosticNum,
4416 sortrs->diagnostics,
4417 sortrs->num_diagnostics);
4419 if (sortrs->diagnostics)
4420 ir_handleDiags (&setobj->nonSurrogateDiagnosticList,
4421 &setobj->nonSurrogateDiagnosticNum,
4422 sortrs->diagnostics->diagRecs,
4423 sortrs->diagnostics->num_diagRecs);
4428 * ir_select_read: handle incoming packages
4430 static void ir_select_read (ClientData clientData)
4432 IrTcl_Obj *p = (IrTcl_Obj *) clientData;
4437 Tcl_CmdInfo cmd_info;
4438 const char *apdu_call;
4441 logf(LOG_DEBUG, "Read handler fd=%d", cs_fileno(p->cs_link));
4442 if (p->state == IR_TCL_R_Connecting)
4444 logf(LOG_DEBUG, "read: connect");
4445 r = cs_rcvconnect (p->cs_link);
4448 logf (LOG_WARN, "cs_rcvconnect returned 1");
4451 p->state = IR_TCL_R_Idle;
4453 ir_select_remove_write (cs_fileno (p->cs_link), p);
4456 logf (LOG_DEBUG, "cs_rcvconnect error");
4457 ir_tcl_disconnect (p);
4460 p->failInfo = IR_TCL_FAIL_CONNECT;
4461 ir_tcl_eval (p->interp, p->failback);
4463 ir_obj_delete ((ClientData) p);
4467 ir_tcl_eval (p->interp, p->callback);
4468 if (p->ref_count == 2 && p->cs_link && p->request_queue
4469 && p->state == IR_TCL_R_Idle)
4470 ir_tcl_send_q (p, p->request_queue, "x");
4471 ir_obj_delete ((ClientData) p);
4476 p->state = IR_TCL_R_Reading;
4479 yaz_log(LOG_DEBUG, "round %d", round);
4480 /* read incoming APDU */
4481 if ((r=cs_get (p->cs_link, &p->buf_in, &p->len_in)) == 1)
4483 logf(LOG_DEBUG, "PDU Fraction read");
4486 /* signal one more use of ir object - callbacks must not
4487 release the ir memory (p pointer) */
4491 logf (LOG_DEBUG, "cs_get failed, code %d", r);
4492 ir_tcl_disconnect (p);
4495 p->failInfo = IR_TCL_FAIL_READ;
4496 ir_tcl_eval (p->interp, p->failback);
4498 /* release ir object now if callback deleted it */
4499 ir_obj_delete ((ClientData) p);
4502 /* got complete APDU. Now decode */
4505 odr_setbuf (p->odr_in, p->buf_in, r, 0);
4506 logf (LOG_DEBUG, "cs_get ok, total size %d", r);
4507 if (!z_APDU (p->odr_in, &apdu, 0, 0))
4509 logf (LOG_DEBUG, "cs_get failed: %s",
4510 odr_errmsg (odr_geterror (p->odr_in)));
4511 ir_tcl_disconnect (p);
4514 p->failInfo = IR_TCL_FAIL_IN_APDU;
4515 p->apduOffset = odr_offset (p->odr_in);
4516 ir_tcl_eval (p->interp, p->failback);
4518 /* release ir object now if failback deleted it */
4519 ir_obj_delete ((ClientData) p);
4523 z_APDU(p->odr_pr, &apdu, 0, 0);
4524 /* handle APDU and invoke callback */
4525 rq = p->request_queue;
4528 /* no corresponding request. Skip it. */
4529 logf(LOG_DEBUG, "no corresponding request. Skipping it");
4530 p->state = IR_TCL_R_Idle;
4533 object_name = rq->object_name;
4534 logf (LOG_DEBUG, "Object %s", object_name);
4536 if (Tcl_GetCommandInfo (p->interp, object_name, &cmd_info))
4540 case Z_APDU_initResponse:
4541 p->eventType = "init";
4542 ir_initResponse (p, apdu->u.initResponse);
4543 apdu_call = p->initResponse;
4545 case Z_APDU_searchResponse:
4546 p->eventType = "search";
4547 ir_searchResponse (p, apdu->u.searchResponse,
4548 (IrTcl_SetObj *) cmd_info.clientData);
4549 apdu_call = ((IrTcl_SetObj *)
4550 cmd_info.clientData)->searchResponse;
4552 case Z_APDU_presentResponse:
4553 p->eventType = "present";
4554 ir_presentResponse (p, apdu->u.presentResponse,
4555 (IrTcl_SetObj *) cmd_info.clientData);
4556 apdu_call = ((IrTcl_SetObj *)
4557 cmd_info.clientData)->presentResponse;
4559 case Z_APDU_scanResponse:
4560 p->eventType = "scan";
4561 ir_scanResponse (p, apdu->u.scanResponse,
4562 (IrTcl_ScanObj *) cmd_info.clientData);
4563 apdu_call = ((IrTcl_ScanObj *)
4564 cmd_info.clientData)->scanResponse;
4566 case Z_APDU_sortResponse:
4567 p->eventType = "sort";
4568 ir_sortResponse (p, apdu->u.sortResponse,
4569 (IrTcl_SetObj *) cmd_info.clientData);
4570 apdu_call = ((IrTcl_SetObj *)
4571 cmd_info.clientData)->sortResponse;
4574 logf (LOG_WARN, "Received unknown APDU type (%d)",
4576 ir_tcl_disconnect (p);
4579 p->failInfo = IR_TCL_FAIL_UNKNOWN_APDU;
4580 ir_tcl_eval (p->interp, p->failback);
4585 p->request_queue = rq->next;
4586 p->state = IR_TCL_R_Idle;
4589 ir_tcl_eval (p->interp, apdu_call);
4590 else if (rq->callback)
4591 ir_tcl_eval (p->interp, rq->callback);
4592 xfree (rq->buf_out);
4593 xfree (rq->callback);
4594 xfree (rq->object_name);
4596 odr_reset (p->odr_in);
4597 if (p->ref_count == 1)
4599 ir_obj_delete ((ClientData) p);
4602 ir_obj_delete ((ClientData) p);
4603 } while (p->cs_link && cs_more (p->cs_link));
4604 if (p->cs_link && p->request_queue && p->state == IR_TCL_R_Idle)
4605 ir_tcl_send_q (p, p->request_queue, "x");
4609 * ir_select_write: handle outgoing packages - not yet written.
4611 static int ir_select_write (ClientData clientData)
4613 IrTcl_Obj *p = (IrTcl_Obj *) clientData;
4617 logf (LOG_DEBUG, "Write handler fd=%d", cs_fileno(p->cs_link));
4618 if (p->state == IR_TCL_R_Connecting)
4620 logf(LOG_DEBUG, "write: connect");
4621 r = cs_rcvconnect (p->cs_link);
4624 logf (LOG_DEBUG, "cs_rcvconnect returned 1");
4627 p->state = IR_TCL_R_Idle;
4629 ir_select_remove_write (cs_fileno (p->cs_link), p);
4632 logf (LOG_DEBUG, "cs_rcvconnect error");
4633 ir_tcl_disconnect (p);
4636 p->failInfo = IR_TCL_FAIL_CONNECT;
4637 ir_tcl_eval (p->interp, p->failback);
4639 ir_obj_delete ((ClientData) p);
4643 ir_tcl_eval (p->interp, p->callback);
4644 ir_obj_delete ((ClientData) p);
4647 rq = p->request_queue;
4648 if (!rq || !rq->buf_out)
4651 if ((r=cs_put (p->cs_link, rq->buf_out, rq->len_out)) < 0)
4653 logf (LOG_DEBUG, "cs_put write fail");
4655 xfree (rq->buf_out);
4657 ir_tcl_disconnect (p);
4660 p->failInfo = IR_TCL_FAIL_WRITE;
4661 ir_tcl_eval (p->interp, p->failback);
4663 ir_obj_delete ((ClientData) p);
4665 else if (r == 0) /* remove select bit */
4667 logf (LOG_DEBUG, "Write completed");
4668 p->state = IR_TCL_R_Waiting;
4669 ir_select_remove_write (cs_fileno (p->cs_link), p);
4670 xfree (rq->buf_out);
4676 static void ir_select_notify (ClientData clientData, int r, int w, int e)
4680 if (!ir_select_write (clientData) && r)
4681 ir_select_read (clientData);
4685 ir_select_read (clientData);
4689 /*----------------------------------------------------------- */
4693 * This wrapper function is used by Windows to invoke the
4694 * initialization code for the DLL. If we are compiling
4695 * with Visual C++, this routine will be renamed to DllMain.
4707 DllEntryPoint(hInst, reason, reserved)
4708 HINSTANCE hInst; /* Library instance handle. */
4709 DWORD reason; /* Reason this function is being called. */
4710 LPVOID reserved; /* Not used. */
4716 /* ------------------------------------------------------- */
4718 * Irtcl_init: Registration of TCL commands.
4720 EXPORT (int,Irtcl_Init) (Tcl_Interp *interp)
4723 if (Tcl_InitStubs(interp, "8.1", 0) == NULL) {
4727 Tcl_CreateCommand (interp, "ir", ir_obj_mk, (ClientData) NULL,
4728 (Tcl_CmdDeleteProc *) NULL);
4729 Tcl_CreateCommand (interp, "ir-set", ir_set_obj_mk,
4730 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
4731 Tcl_CreateCommand (interp, "ir-scan", ir_scan_obj_mk,
4732 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
4733 Tcl_CreateCommand (interp, "ir-log-init", ir_log_init_proc,
4734 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
4735 Tcl_CreateCommand (interp, "ir-log", ir_log_proc,
4736 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
4737 Tcl_CreateCommand (interp, "ir-version", ir_version, (ClientData) NULL,
4738 (Tcl_CmdDeleteProc *) NULL);