2 * IR toolkit for tcl/tk
3 * (c) Index Data 1995-2003
4 * See the file LICENSE for details.
7 * Revision 1.123 2003-03-05 21:21:41 adam
8 * APDU log. default largeSetLowerBound changed from 2 to 1
10 * Revision 1.122 2003/03/05 18:02:08 adam
11 * Fix bug with idAuthentication that didn't work for empty group.
13 * Revision 1.121 2003/01/30 13:27:07 adam
14 * Changed version to 1.4.1. Added WIN32 version resource.
15 * IrTcl ignores unexpected PDU's, rather than die.
17 * Revision 1.120 2002/03/20 14:48:54 adam
18 * implemented USR.1 SearchResult-1
20 * Revision 1.119 2001/12/03 00:31:06 adam
21 * Towards 1.4. Configure updates.
23 * Revision 1.118 2001/03/27 16:27:21 adam
24 * Fixed bug in do_responseStatus.
26 * Revision 1.117 2001/03/26 11:39:34 adam
27 * Fixed bug in ir_deleteDiags - crash when receiving multiple diags.
29 * Revision 1.116 2001/02/09 11:58:04 adam
30 * Updated for Tcl8.1 and higher where internal encoding is UTF-8.
32 * Revision 1.115 2000/09/13 12:18:49 adam
33 * Logging utility patch (YAZ version 1.7).
35 * Revision 1.114 1999/05/17 20:37:41 adam
36 * Fixed problem with ASN code.
38 * Revision 1.113 1999/04/20 10:01:46 adam
39 * Modified calls to ODR encoders/decoders (name argument).
41 * Revision 1.112 1999/03/22 06:51:34 adam
44 * Revision 1.111 1999/02/11 11:30:09 adam
47 * Revision 1.110 1998/10/20 15:15:31 adam
48 * Changed scan response handler.
50 * Revision 1.109 1998/10/13 21:23:26 adam
51 * Fixed searchStatus method.
53 * Revision 1.108 1998/10/12 11:48:08 adam
54 * Removed printf call.
56 * Revision 1.107 1998/06/10 13:00:46 adam
57 * Added ir-version command.
59 * Revision 1.106 1998/05/20 12:25:35 adam
60 * Fixed bug that occurred in rare cases when encoding of incoming
63 * Revision 1.105 1998/04/02 14:31:08 adam
64 * This version works with compiled ASN.1 code.
66 * Revision 1.104 1998/02/27 14:26:07 adam
67 * Changed client so that it still works if target sets numberOfRecords
68 * in response to an illegal value.
70 * Revision 1.103 1997/11/19 11:22:10 adam
71 * Object identifiers can be accessed in GRS-1 records.
73 * Revision 1.102 1997/09/17 12:22:40 adam
74 * Changed to use YAZ version 1.4. The new comstack utility, cs_straddr,
77 * Revision 1.101 1997/09/09 10:19:53 adam
78 * New MSV5.0 port with fewer warnings.
80 * Revision 1.100 1997/05/01 15:04:05 adam
81 * Added ir-log command.
83 * Revision 1.99 1997/04/30 07:24:47 adam
84 * Spell fix of an error message.
86 * Revision 1.98 1997/04/13 18:57:20 adam
87 * Better error reporting and aligned with Tcl/Tk style.
88 * Rework of notifier code with Tcl_File handles.
90 * Revision 1.97 1996/11/14 17:11:07 adam
91 * Added Explain documentaion.
93 * Revision 1.96 1996/10/08 13:02:50 adam
94 * When dealing with records, odr_choice_enable_bias function is used to
95 * prevent decoding of externals.
97 * Revision 1.95 1996/09/13 10:51:49 adam
98 * Bug fix: ir_tcl_select_set called Tcl_GetFile at disconnect.
100 * Revision 1.94 1996/08/21 13:32:53 adam
101 * Implemented saveFile method and extended loadFile method to work with it.
103 * Revision 1.93 1996/08/16 15:07:45 adam
104 * First work on Explain.
106 * Revision 1.92 1996/08/09 15:33:07 adam
107 * Modified the code to use tk4.1/tcl7.5 patch level 1. The time-driven
108 * polling is no longer activated on Windows since asynchrounous I/O works
111 * Revision 1.91 1996/07/03 13:31:11 adam
112 * The xmalloc/xfree functions from YAZ are used to manage memory.
114 * Revision 1.90 1996/06/27 14:21:00 adam
115 * Yet another Windows port.
117 * Revision 1.89 1996/06/11 15:27:15 adam
118 * Event type set to connect a little earlier in the do_connect function.
120 * Revision 1.88 1996/06/03 09:04:22 adam
121 * Changed a few logf calls.
123 * Revision 1.87 1996/05/29 06:37:51 adam
124 * Function ir_tcl_get_grs_r enhanced so that specific elements can be
127 * Revision 1.86 1996/03/20 13:54:04 adam
128 * The Tcl_File structure is only manipulated in the Tk-event interface
131 * Revision 1.85 1996/03/15 11:15:48 adam
132 * Modified to use new prototypes for p_query_rpn and p_query_scan.
134 * Revision 1.84 1996/03/07 12:42:49 adam
135 * Better logging when callback is invoked.
137 * Revision 1.83 1996/03/05 09:21:09 adam
138 * Bug fix: memory used by GRS records wasn't freed.
139 * Rewrote some of the error handling code - the connection is always
140 * closed before failback is called.
141 * If failback is defined the send APDU methods (init, search, ...) will
142 * return OK but invoke failback (as is the case if the write operation
144 * Bug fix: ref_count in assoc object could grow if fraction of PDU was
147 * Revision 1.82 1996/02/29 15:30:21 adam
148 * Export of IrTcl functionality to extensions.
150 * Revision 1.81 1996/02/26 18:38:32 adam
151 * Work on export of set methods.
153 * Revision 1.80 1996/02/23 17:31:39 adam
154 * More functions made available to the wais tcl extension.
156 * Revision 1.79 1996/02/23 13:41:38 adam
157 * Work on public access to simple ir class system.
159 * Revision 1.78 1996/02/21 10:16:08 adam
160 * Simplified select handling. Only one function ir_tcl_select_set has
161 * to be externally defined.
163 * Revision 1.77 1996/02/20 17:52:58 adam
164 * Uses the YAZ oid system to name record syntax object identifiers.
166 * Revision 1.76 1996/02/20 16:09:51 adam
167 * Bug fix: didn't set element set names stamp correctly on result
168 * set records when element set names were set to the empty string.
170 * Revision 1.75 1996/02/19 15:41:53 adam
171 * Better log messages.
172 * Minor improvement of connect method.
174 * Revision 1.74 1996/02/05 17:58:03 adam
175 * Ported ir-tcl to use the beta releases of tcl7.5/tk4.1.
177 * Revision 1.73 1996/01/29 11:35:19 adam
178 * Bug fix: cs_type member renamed to comstackType to avoid conflict with
179 * cs_type macro defined by YAZ.
181 * Revision 1.72 1996/01/19 17:45:34 quinn
182 * Added debugging output
184 * Revision 1.71 1996/01/19 16:22:38 adam
185 * New method: apduDump - returns information about last incoming APDU.
187 * Revision 1.70 1996/01/10 09:18:34 adam
188 * PDU specific callbacks implemented: initRespnse, searchResponse,
189 * presentResponse and scanResponse.
190 * Bug fix in the command line shell (tclmain.c) - discovered on OSF/1.
192 * Revision 1.69 1996/01/04 16:12:12 adam
193 * Setting PDUType renamed to eventType.
195 * Revision 1.68 1996/01/04 11:05:22 adam
196 * New setting: PDUType - returns type of last PDU returned from the target.
197 * Fixed a bug in configure/Makefile.
199 * Revision 1.67 1996/01/03 09:00:51 adam
200 * Updated to use new version of Yaz (names changed to avoid C++ conflict).
202 * Revision 1.66 1995/11/28 17:26:39 adam
203 * Removed Carriage return from ir-tcl.c!
204 * Removed misc. debug logs.
206 * Revision 1.65 1995/11/28 13:53:00 quinn
209 * Revision 1.64 1995/11/13 15:39:18 adam
210 * Bug fix: {small,medium}SetElementSetNames weren't set correctly.
211 * Bug fix: idAuthentication weren't set correctly.
213 * Revision 1.63 1995/11/13 09:55:39 adam
214 * Multiple records at a position in a result-set with differnt
217 * Revision 1.62 1995/10/18 17:20:33 adam
218 * Work on target setup in client.tcl.
220 * Revision 1.61 1995/10/18 16:42:42 adam
221 * New settings: smallSetElementSetNames and mediumSetElementSetNames.
223 * Revision 1.60 1995/10/18 15:43:31 adam
224 * In search: mediumSetElementSetNames and smallSetElementSetNames are
225 * set to elementSetNames.
227 * Revision 1.59 1995/10/17 12:18:58 adam
228 * Bug fix: when target connection closed, the connection was not
229 * properly reestablished.
231 * Revision 1.58 1995/10/16 17:00:55 adam
232 * New setting: elementSetNames.
233 * Various client improvements. Medium presentation format looks better.
235 * Revision 1.57 1995/09/21 13:11:51 adam
236 * Support of dynamic loading.
237 * Test script uses load command if necessary.
239 * Revision 1.56 1995/08/29 15:30:14 adam
240 * Work on GRS records.
242 * Revision 1.55 1995/08/28 09:43:25 adam
243 * Minor changes. configure only searches for yaz beta 3 and versions after
246 * Revision 1.54 1995/08/24 12:25:16 adam
247 * Modified to work with yaz 1.0b3.
249 * Revision 1.53 1995/08/04 12:49:26 adam
250 * Bug fix: reading uninitialized variable p.
252 * Revision 1.52 1995/08/04 11:32:38 adam
253 * More work on output queue. Memory related routines moved
256 * Revision 1.51 1995/08/03 13:22:54 adam
259 * Revision 1.50 1995/07/20 08:09:49 adam
260 * client.tcl: Targets removed from hotTargets list when targets
261 * are removed/modified.
262 * ir-tcl.c: More work on triggerResourceControl.
264 * Revision 1.49 1995/06/30 12:39:21 adam
265 * Bug fix: loadFile didn't set record type.
266 * The MARC routines are a little less strict in the interpretation.
267 * Script display.tcl replaces the old marc.tcl.
268 * New interactive script: shell.tcl.
270 * Revision 1.48 1995/06/27 19:03:50 adam
271 * Bug fix in do_present in ir-tcl.c: p->set_child member weren't set.
272 * nextResultSetPosition used instead of setOffset.
274 * Revision 1.47 1995/06/25 10:25:04 adam
275 * Working on triggerResourceControl. Description of compile/install
276 * procedure moved to ir-tcl.sgml.
278 * Revision 1.46 1995/06/22 13:15:06 adam
279 * Feature: SUTRS. Setting getSutrs implemented.
280 * Work on display formats.
281 * Preferred record syntax can be set by the user.
283 * Revision 1.45 1995/06/20 08:07:30 adam
284 * New setting: failInfo.
285 * Working on better cancel mechanism.
287 * Revision 1.44 1995/06/19 17:01:20 adam
290 * Revision 1.43 1995/06/19 13:06:08 adam
291 * New define: IR_TCL_VERSION.
293 * Revision 1.42 1995/06/19 08:08:52 adam
294 * client.tcl: hotTargets now contain both database and target name.
295 * ir-tcl.c: setting protocol edited. Errors in callbacks are logged
296 * by logf(LOG_WARN, ...) calls.
298 * Revision 1.41 1995/06/16 12:28:16 adam
299 * Implemented preferredRecordSyntax.
300 * Minor changes in diagnostic handling.
301 * Record list deleted when connection closes.
303 * Revision 1.40 1995/06/14 13:37:18 adam
304 * Setting recordType implemented.
305 * Setting implementationVersion implemented.
306 * Settings implementationId / implementationName edited.
308 * Revision 1.39 1995/06/08 10:26:32 adam
309 * Bug fix in ir_strdup.
311 * Revision 1.38 1995/06/01 16:36:47 adam
312 * About buttons. Minor bug fixes.
314 * Revision 1.37 1995/06/01 07:31:20 adam
315 * Rename of many typedefs -> IrTcl_...
317 * Revision 1.36 1995/05/31 13:09:59 adam
318 * Client searches/presents may be interrupted.
319 * New moving book-logo.
321 * Revision 1.35 1995/05/31 08:36:33 adam
322 * Bug fix in client.tcl: didn't save options on clientrc.tcl.
323 * New method: referenceId. More work on scan.
325 * Revision 1.34 1995/05/29 10:33:42 adam
326 * README and rename of startup script.
328 * Revision 1.33 1995/05/29 09:15:11 quinn
329 * Changed CS_SR to PROTO_SR, etc.
331 * Revision 1.32 1995/05/29 08:44:16 adam
332 * Work on delete of objects.
334 * Revision 1.31 1995/05/26 11:44:10 adam
335 * Bugs fixed. More work on MARC utilities and queries. Test
336 * client is up-to-date again.
338 * Revision 1.30 1995/05/26 08:54:11 adam
339 * New MARC utilities. Uses prefix query.
341 * Revision 1.29 1995/05/24 14:10:22 adam
342 * Work on idAuthentication, protocolVersion and options.
344 * Revision 1.28 1995/05/23 15:34:48 adam
345 * Many new settings, userInformationField, smallSetUpperBound, etc.
346 * A number of settings are inherited when ir-set is executed.
347 * This version is incompatible with the graphical test client (client.tcl).
349 * Revision 1.27 1995/05/11 15:34:47 adam
350 * Scan request changed a bit. This version works with RLG.
352 * Revision 1.26 1995/04/18 16:11:51 adam
353 * First version of graphical Scan. Some work on query-by-form.
355 * Revision 1.25 1995/04/17 09:37:17 adam
356 * Further development of scan.
358 * Revision 1.24 1995/04/11 14:16:42 adam
359 * Further work on scan. Response works. Entries aren't saved yet.
361 * Revision 1.23 1995/04/10 10:50:27 adam
362 * Result-set name defaults to suffix of ir-set name.
363 * Started working on scan. Not finished at this point.
365 * Revision 1.22 1995/03/31 10:43:03 adam
366 * More robust when getting bad MARC records.
368 * Revision 1.21 1995/03/31 08:56:37 adam
369 * New button "Search".
371 * Revision 1.20 1995/03/29 16:07:09 adam
372 * Bug fix: Didn't use setName in present request.
374 * Revision 1.19 1995/03/28 12:45:23 adam
375 * New ir method failback: called on disconnect/protocol error.
376 * New ir set/get method: protocol: SR / Z3950.
377 * Simple popup and disconnect when failback is invoked.
379 * Revision 1.18 1995/03/21 15:50:12 adam
382 * Revision 1.17 1995/03/21 13:41:03 adam
383 * Comstack cs_create not used too often. Non-blocking connect.
385 * Revision 1.16 1995/03/21 08:26:06 adam
386 * New method, setName, to specify the result set name (other than Default).
387 * New method, responseStatus, which returns diagnostic info, if any, after
388 * present response / search response.
390 * Revision 1.15 1995/03/20 15:24:07 adam
391 * Diagnostic records saved on searchResponse.
393 * Revision 1.14 1995/03/20 08:53:22 adam
394 * Event loop in tclmain.c rewritten. New method searchStatus.
396 * Revision 1.13 1995/03/17 18:26:17 adam
397 * Non-blocking i/o used now. Database names popup as cascade items.
399 * Revision 1.12 1995/03/17 15:45:00 adam
400 * Improved target/database setup.
402 * Revision 1.11 1995/03/16 17:54:03 adam
403 * Minor changes really.
405 * Revision 1.10 1995/03/15 16:14:50 adam
406 * Blocking arg in cs_create changed.
408 * Revision 1.9 1995/03/15 13:59:24 adam
411 * Revision 1.8 1995/03/15 08:25:16 adam
412 * New method presentStatus to check for error on present. Misc. cleanup
413 * of IrTcl_RecordList manipulations. Full MARC record presentation in
416 * Revision 1.7 1995/03/14 17:32:29 adam
417 * Presentation of full Marc record in popup window.
419 * Revision 1.6 1995/03/12 19:31:55 adam
420 * Pattern matching implemented when retrieving MARC records. More
421 * diagnostic functions.
423 * Revision 1.5 1995/03/10 18:00:15 adam
424 * Actual presentation in line-by-line format. RPN query support.
426 * Revision 1.4 1995/03/09 16:15:08 adam
427 * First presentRequest attempts. Hot-target list.
445 #if defined(__WIN32__)
446 # define WIN32_LEAN_AND_MEAN
447 # include <windows.h>
448 # undef WIN32_LEAN_AND_MEAN
451 * VC++ has an alternate entry point called DllMain, so we need to rename
455 # if defined(_MSC_VER)
456 # define EXPORT(a,b) __declspec(dllexport) a b
457 # define DllEntryPoint DllMain
459 # if defined(__BORLANDC__)
460 # define EXPORT(a,b) a _export b
462 # define EXPORT(a,b) a b
466 # define EXPORT(a,b) a b
469 static char *wrongArgs = "wrong # args: should be \"";
470 static FILE *odr_print_file = 0;
472 static int ir_tcl_error_exec (Tcl_Interp *interp, int argc, char **argv)
475 Tcl_AppendResult (interp, " while executing ", NULL);
476 for (i = 0; i<argc; i++)
477 Tcl_AppendResult (interp, (i ? " " : "\""), argv[i], NULL);
478 Tcl_AppendResult (interp, "\"", NULL);
483 static void ir_deleteDiags (IrTcl_Diagnostic **dst_list, int *dst_num);
485 static void ir_select_notify (ClientData clientData, int r, int w, int e);
487 void ir_select_add (int fd, void *obj)
489 ir_tcl_select_set (ir_select_notify, fd, obj, 1, 0, 0);
492 void ir_select_add_write (int fd, void *obj)
494 ir_tcl_select_set (ir_select_notify, fd, obj, 1, 1, 0);
497 void ir_select_remove (int fd, void *obj)
499 ir_tcl_select_set (NULL, fd, obj, 0, 0, 0);
502 void ir_select_remove_write (int fd, void *obj)
504 ir_tcl_select_set (ir_select_notify, fd, obj, 1, 0, 0);
507 static void delete_IR_record (IrTcl_RecordList *rl)
511 case Z_NamePlusRecord_databaseRecord:
512 switch (rl->u.dbrec.type)
515 ir_tcl_grs_del (&rl->u.dbrec.u.grs1);
520 xfree (rl->u.dbrec.buf);
521 rl->u.dbrec.buf = NULL;
523 case Z_NamePlusRecord_surrogateDiagnostic:
524 ir_deleteDiags (&rl->u.surrogateDiagnostics.list,
525 &rl->u.surrogateDiagnostics.num);
528 xfree (rl->elements);
531 static void purge_IR_records (IrTcl_SetObj *setobj)
533 IrTcl_RecordList *rl;
534 while ((rl = setobj->record_list))
536 setobj->record_list = rl->next;
537 delete_IR_record (rl);
542 static IrTcl_RecordList *new_IR_record (IrTcl_SetObj *setobj,
544 const char *elements)
546 IrTcl_RecordList *rl;
548 if (elements && !*elements)
550 for (rl = setobj->record_list; rl; rl = rl->next)
552 if (no == rl->no && (!rl->elements || !elements ||
553 !strcmp(elements, rl->elements)))
555 delete_IR_record (rl);
561 rl = ir_tcl_malloc (sizeof(*rl));
562 rl->next = setobj->record_list;
564 setobj->record_list = rl;
567 ir_tcl_strdup (NULL, &rl->elements, elements);
574 int ir_tcl_eval (Tcl_Interp *interp, const char *command)
576 char *tmp = ir_tcl_malloc (strlen(command)+1);
579 logf (LOG_DEBUG, "Invoking %.23s ...", command);
580 strcpy (tmp, command);
581 r = Tcl_Eval (interp, tmp);
584 const char *errorInfo = Tcl_GetVar (interp, "errorInfo", 0);
585 logf (LOG_WARN, "Tcl error in line %d: %s\n%s", interp->errorLine,
586 interp->result, errorInfo ? errorInfo : "<null>");
588 Tcl_FreeResult (interp);
594 * IrTcl_getRecordSyntaxStr: Return record syntax name of object id
596 static char *IrTcl_getRecordSyntaxStr (enum oid_value value)
599 struct oident ent, *entp;
601 ent.proto = PROTO_Z3950;
602 ent.oclass = CLASS_RECSYN;
605 o = oid_getoidbyent (&ent);
606 entp = oid_getentbyoid (o);
614 * IrTcl_getRecordSyntaxVal: Return record syntax value of string
616 static enum oid_value IrTcl_getRecordSyntaxVal (const char *name)
618 return oid_getvalbyname (name);
621 static IrTcl_RecordList *find_IR_record (IrTcl_SetObj *setobj, int no)
623 IrTcl_RecordList *rl;
625 for (rl = setobj->record_list; rl; rl = rl->next)
627 (!setobj->recordElements || !rl->elements ||
628 !strcmp (setobj->recordElements, rl->elements)))
633 static void delete_IR_records (IrTcl_SetObj *setobj)
635 IrTcl_RecordList *rl, *rl1;
637 for (rl = setobj->record_list; rl; rl = rl1)
639 delete_IR_record (rl);
643 setobj->record_list = NULL;
647 * ir_tcl_get_set_int: Set/get integer value
649 int ir_tcl_get_set_int (int *val, Tcl_Interp *interp, int argc, char **argv)
655 if (Tcl_GetInt (interp, argv[2], val)==TCL_ERROR)
658 sprintf (buf, "%d", *val);
659 Tcl_AppendResult (interp, buf, NULL);
665 * ir_tcl_method_error
667 int ir_tcl_method_error (Tcl_Interp *interp, int argc, char **argv,
670 IrTcl_Methods *tab_i = tab;
673 Tcl_AppendResult (interp, "bad method: \"", *argv, " ", argv[1],
674 "\"\nmethod should be of:", NULL);
675 for (tab_i = tab; tab_i->tab; tab_i++)
676 for (t = tab_i->tab; t->name; t++)
677 Tcl_AppendResult (interp, " ", t->name, NULL);
682 * ir_tcl_method: Search for method in table and invoke method handler
684 int ir_tcl_method (Tcl_Interp *interp, int argc, char **argv,
685 IrTcl_Methods *tab, int *ret)
687 IrTcl_Methods *tab_i = tab;
690 for (tab_i = tab; tab_i->tab; tab_i++)
691 for (t = tab_i->tab; t->name; t++)
694 if ((*t->method)(tab_i->obj, interp, argc, argv) == TCL_ERROR)
698 if (!strcmp (t->name, argv[1]))
700 *ret = (*t->method)(tab_i->obj, interp, argc, argv);
711 * ir_tcl_named_bits: get/set named bits
713 int ir_tcl_named_bits (struct ir_named_entry *tab, Odr_bitmask *ob,
714 Tcl_Interp *interp, int argc, char **argv)
716 struct ir_named_entry *ti;
721 for (no = 0; no < argc; no++)
724 for (ti = tab; ti->name; ti++)
725 if (!strcmp(argv[no], "@all") || !strcmp (argv[no], ti->name))
727 ODR_MASK_SET (ob, ti->pos);
732 Tcl_AppendResult (interp, "bad bit mask ", argv[no], NULL);
733 return ir_tcl_error_exec (interp, argc, argv);
738 for (ti = tab; ti->name; ti++)
739 if (ODR_MASK_GET (ob, ti->pos))
740 Tcl_AppendElement (interp, ti->name);
744 static void set_referenceId (ODR o, Z_ReferenceId **dst, const char *src)
750 *dst = odr_malloc (o, sizeof(**dst));
751 (*dst)->size = (*dst)->len = strlen(src);
752 (*dst)->buf = odr_malloc (o, (*dst)->len);
753 memcpy ((*dst)->buf, src, (*dst)->len);
757 static void get_referenceId (char **dst, Z_ReferenceId *src)
765 *dst = ir_tcl_malloc (src->len+1);
766 memcpy (*dst, src->buf, src->len);
767 (*dst)[src->len] = '\0';
770 /* ------------------------------------------------------- */
773 * do_init_request: init method on IR object
775 static int do_init_request (void *obj, Tcl_Interp *interp,
776 int argc, char **argv)
784 logf (LOG_DEBUG, "init %s", *argv);
787 Tcl_AppendResult (interp, "not connected", NULL);
788 return ir_tcl_error_exec (interp, argc, argv);
790 apdu = zget_APDU (p->odr_out, Z_APDU_initRequest);
791 req = apdu->u.initRequest;
793 set_referenceId (p->odr_out, &req->referenceId, p->set_inher.referenceId);
794 req->options = &p->options;
795 req->protocolVersion = &p->protocolVersion;
796 req->preferredMessageSize = &p->preferredMessageSize;
797 req->maximumRecordSize = &p->maximumRecordSize;
799 if (p->idAuthenticationGroupId || p->idAuthenticationUserId)
801 Z_IdPass *pass = odr_malloc (p->odr_out, sizeof(*pass));
802 Z_IdAuthentication *auth = odr_malloc (p->odr_out, sizeof(*auth));
804 logf (LOG_DEBUG, "using pass authentication");
806 auth->which = Z_IdAuthentication_idPass;
807 auth->u.idPass = pass;
808 if (p->idAuthenticationGroupId && *p->idAuthenticationGroupId)
809 pass->groupId = p->idAuthenticationGroupId;
811 pass->groupId = NULL;
812 if (p->idAuthenticationUserId && *p->idAuthenticationUserId)
813 pass->userId = p->idAuthenticationUserId;
816 if (p->idAuthenticationPassword && *p->idAuthenticationPassword)
817 pass->password = p->idAuthenticationPassword;
819 pass->password = NULL;
820 req->idAuthentication = auth;
822 else if (p->idAuthenticationOpen && *p->idAuthenticationOpen)
824 Z_IdAuthentication *auth = odr_malloc (p->odr_out, sizeof(*auth));
826 logf (LOG_DEBUG, "using open authentication");
827 auth->which = Z_IdAuthentication_open;
828 auth->u.open = p->idAuthenticationOpen;
829 req->idAuthentication = auth;
832 req->idAuthentication = NULL;
833 req->implementationId = p->implementationId;
834 req->implementationName = p->implementationName;
835 req->implementationVersion = p->implementationVersion;
836 req->userInformationField = 0;
838 return ir_tcl_send_APDU (interp, p, apdu, "init", *argv);
842 * do_protocolVersion: Set protocol Version
844 static int do_protocolVersion (void *obj, Tcl_Interp *interp,
845 int argc, char **argv)
853 ODR_MASK_ZERO (&p->protocolVersion);
854 ODR_MASK_SET (&p->protocolVersion, 0);
855 ODR_MASK_SET (&p->protocolVersion, 1);
860 if (Tcl_GetInt (interp, argv[2], &version)==TCL_ERROR)
862 ODR_MASK_ZERO (&p->protocolVersion);
863 for (i = 0; i<version; i++)
864 ODR_MASK_SET (&p->protocolVersion, i);
866 for (i = 4; --i >= 0; )
867 if (ODR_MASK_GET (&p->protocolVersion, i))
869 sprintf (buf, "%d", i+1);
870 interp->result = buf;
875 * do_options: Set options
877 static int do_options (void *obj, Tcl_Interp *interp,
878 int argc, char **argv)
880 static struct ir_named_entry options_tab[] = {
884 { "resourceReport", 3 },
885 { "triggerResourceCtrl", 4},
886 { "resourceCtrl", 5},
890 { "extendedServices", 10},
891 { "level-1Segmentation", 11},
892 { "level-2Segmentation", 12},
893 { "concurrentOperations", 13},
894 { "namedResultSets", 14},
901 ODR_MASK_ZERO (&p->options);
902 ODR_MASK_SET (&p->options, 0);
903 ODR_MASK_SET (&p->options, 1);
904 ODR_MASK_SET (&p->options, 4);
905 ODR_MASK_SET (&p->options, 7);
906 ODR_MASK_SET (&p->options, 14);
909 return ir_tcl_named_bits (options_tab, &p->options, interp, argc-2, argv+2);
913 * do_apduInfo: Get APDU information
915 static int do_apduInfo (void *obj, Tcl_Interp *interp, int argc, char **argv)
923 sprintf (buf, "%d", p->apduLen);
924 Tcl_AppendElement (interp, buf);
925 sprintf (buf, "%d", p->apduOffset);
926 Tcl_AppendElement (interp, buf);
929 Tcl_AppendElement (interp, "");
932 apduf = fopen ("apdu.tmp", "w");
935 Tcl_AppendElement (interp, "");
938 odr_dumpBER (apduf, p->buf_in, p->apduLen);
940 if (!(apduf = fopen ("apdu.tmp", "r")))
941 Tcl_AppendElement (interp, "");
946 Tcl_AppendResult (interp, " {", NULL);
947 while ((c = getc (apduf)) != EOF)
951 Tcl_AppendResult (interp, buf, NULL);
954 Tcl_AppendResult (interp, "}", NULL);
961 * do_failInfo: Get fail information
963 static int do_failInfo (void *obj, Tcl_Interp *interp, int argc, char **argv)
973 sprintf (buf, "%d", p->failInfo);
979 case IR_TCL_FAIL_CONNECT:
980 cp = "connect failed";
982 case IR_TCL_FAIL_READ:
983 cp = "connection closed";
985 case IR_TCL_FAIL_WRITE:
986 cp = "connection closed";
988 case IR_TCL_FAIL_IN_APDU:
989 cp = "failed to decode incoming APDU";
991 case IR_TCL_FAIL_UNKNOWN_APDU:
997 Tcl_AppendElement (interp, buf);
998 Tcl_AppendElement (interp, cp);
1003 * do_preferredMessageSize: Set/get preferred message size
1005 static int do_preferredMessageSize (void *obj, Tcl_Interp *interp,
1006 int argc, char **argv)
1012 p->preferredMessageSize = 30000;
1015 return ir_tcl_get_set_int (&p->preferredMessageSize, interp, argc, argv);
1019 * do_maximumRecordSize: Set/get maximum record size
1021 static int do_maximumRecordSize (void *obj, Tcl_Interp *interp,
1022 int argc, char **argv)
1028 p->maximumRecordSize = 30000;
1031 return ir_tcl_get_set_int (&p->maximumRecordSize, interp, argc, argv);
1035 * do_initResult: Get init result
1037 static int do_initResult (void *obj, Tcl_Interp *interp,
1038 int argc, char **argv)
1044 return ir_tcl_get_set_int (&p->initResult, interp, argc, argv);
1049 * do_implementationName: Set/get Implementation Name.
1051 static int do_implementationName (void *obj, Tcl_Interp *interp,
1052 int argc, char **argv)
1057 return ir_tcl_strdup (interp, &p->implementationName,
1059 else if (argc == -1)
1060 return ir_tcl_strdel (interp, &p->implementationName);
1063 xfree (p->implementationName);
1064 if (ir_tcl_strdup (interp, &p->implementationName, argv[2])
1068 Tcl_AppendResult (interp, p->implementationName, (char*) NULL);
1073 * do_implementationId: Get Implementation Id.
1075 static int do_implementationId (void *obj, Tcl_Interp *interp,
1076 int argc, char **argv)
1081 return ir_tcl_strdup (interp, &p->implementationId, "81");
1082 else if (argc == -1)
1083 return ir_tcl_strdel (interp, &p->implementationId);
1084 Tcl_AppendResult (interp, p->implementationId, (char*) NULL);
1089 * do_implementationVersion: get Implementation Version.
1091 static int do_implementationVersion (void *obj, Tcl_Interp *interp,
1092 int argc, char **argv)
1097 return ir_tcl_strdup (interp, &p->implementationVersion,
1098 #ifdef IR_TCL_VERSION
1103 else if (argc == -1)
1104 return ir_tcl_strdel (interp, &p->implementationVersion);
1105 Tcl_AppendResult (interp, p->implementationVersion, (char*) NULL);
1110 * do_targetImplementationName: Get Implementation Name of target.
1112 static int do_targetImplementationName (void *obj, Tcl_Interp *interp,
1113 int argc, char **argv)
1119 p->targetImplementationName = NULL;
1122 else if (argc == -1)
1123 return ir_tcl_strdel (interp, &p->targetImplementationName);
1124 Tcl_AppendResult (interp, p->targetImplementationName, (char*) NULL);
1129 * do_targetImplementationId: Get Implementation Id of target
1131 static int do_targetImplementationId (void *obj, Tcl_Interp *interp,
1132 int argc, char **argv)
1138 p->targetImplementationId = NULL;
1141 else if (argc == -1)
1142 return ir_tcl_strdel (interp, &p->targetImplementationId);
1143 Tcl_AppendResult (interp, p->targetImplementationId, (char*) NULL);
1148 * do_targetImplementationVersion: Get Implementation Version of target
1150 static int do_targetImplementationVersion (void *obj, Tcl_Interp *interp,
1151 int argc, char **argv)
1157 p->targetImplementationVersion = NULL;
1160 else if (argc == -1)
1161 return ir_tcl_strdel (interp, &p->targetImplementationVersion);
1162 Tcl_AppendResult (interp, p->targetImplementationVersion, (char*) NULL);
1167 * do_idAuthentication: Set/get id Authentication
1169 static int do_idAuthentication (void *obj, Tcl_Interp *interp,
1170 int argc, char **argv)
1174 if (argc >= 3 || argc == -1)
1176 xfree (p->idAuthenticationOpen);
1177 xfree (p->idAuthenticationGroupId);
1178 xfree (p->idAuthenticationUserId);
1179 xfree (p->idAuthenticationPassword);
1181 if (argc >= 3 || argc <= 0)
1183 p->idAuthenticationOpen = NULL;
1184 p->idAuthenticationGroupId = NULL;
1185 p->idAuthenticationUserId = NULL;
1186 p->idAuthenticationPassword = NULL;
1194 xfree (p->idAuthenticationGroupId);
1195 xfree (p->idAuthenticationUserId);
1196 xfree (p->idAuthenticationPassword);
1197 p->idAuthenticationGroupId = NULL;
1198 p->idAuthenticationUserId = NULL;
1199 p->idAuthenticationPassword = NULL;
1201 ir_tcl_strdup (interp, &p->idAuthenticationOpen, argv[2])
1207 xfree (p->idAuthenticationOpen);
1208 p->idAuthenticationOpen = NULL;
1210 ir_tcl_strdup (interp, &p->idAuthenticationGroupId, argv[2])
1214 ir_tcl_strdup (interp, &p->idAuthenticationUserId, argv[3])
1218 ir_tcl_strdup (interp, &p->idAuthenticationPassword, argv[4])
1223 if (p->idAuthenticationOpen)
1224 Tcl_AppendElement (interp, p->idAuthenticationOpen);
1225 else if (p->idAuthenticationGroupId || p->idAuthenticationUserId)
1227 Tcl_AppendElement (interp, p->idAuthenticationGroupId);
1228 Tcl_AppendElement (interp, p->idAuthenticationUserId);
1229 Tcl_AppendElement (interp, p->idAuthenticationPassword);
1235 * do_connect: connect method on IR object
1237 static int do_connect (void *obj, Tcl_Interp *interp,
1238 int argc, char **argv)
1248 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
1249 " ?hostname?\"", NULL);
1254 Tcl_AppendResult (interp, p->hostname, NULL);
1258 logf (LOG_DEBUG, "connect %s %s", *argv, argv[2]);
1261 Tcl_AppendResult (interp, "already connected", NULL);
1262 return ir_tcl_error_exec (interp, argc, argv);
1264 if (!strcmp (p->comstackType, "tcpip"))
1266 p->cs_link = cs_create (tcpip_type, CS_BLOCK, p->protocol_type);
1267 logf (LOG_DEBUG, "tcp/ip connect %s", argv[2]);
1269 else if (!strcmp (p->comstackType, "mosi"))
1272 p->cs_link = cs_create (mosi_type, CS_BLOCK, p->protocol_type);
1273 logf (LOG_DEBUG, "mosi connect %s", argv[2]);
1275 Tcl_AppendResult (interp, "mosi not supported", NULL);
1276 return ir_tcl_error_exec (interp, argc, argv);
1281 Tcl_AppendResult (interp, "bad comstack type ",
1282 p->comstackType, NULL);
1283 return ir_tcl_error_exec (interp, argc, argv);
1285 if (ir_tcl_strdup (interp, &p->hostname, argv[2]) == TCL_ERROR)
1287 p->eventType = "connect";
1288 addr = cs_straddr (p->cs_link, argv[2]);
1291 ir_tcl_disconnect (p);
1292 Tcl_AppendResult (interp, "cs_straddr fail", NULL);
1293 return ir_tcl_error_exec (interp, argc, argv);
1295 if ((r=cs_connect (p->cs_link, addr)) < 0)
1297 ir_tcl_disconnect (p);
1298 Tcl_AppendResult (interp, "connect fail", NULL);
1299 return ir_tcl_error_exec (interp, argc, argv);
1301 ir_select_add (cs_fileno (p->cs_link), p);
1304 logf (LOG_DEBUG, "connect pending fd=%d", cs_fileno(p->cs_link));
1305 ir_select_add_write (cs_fileno (p->cs_link), p);
1306 p->state = IR_TCL_R_Connecting;
1310 logf (LOG_DEBUG, "connect ok fd=%d", cs_fileno(p->cs_link));
1311 p->state = IR_TCL_R_Idle;
1313 ir_tcl_eval (p->interp, p->callback);
1320 * ir_tcl_disconnect: close connection
1322 void ir_tcl_disconnect (IrTcl_Obj *p)
1326 logf(LOG_DEBUG, "Closing connection to %s", p->hostname);
1327 xfree (p->hostname);
1329 assert (p->cs_link);
1330 ir_select_remove (cs_fileno (p->cs_link), p);
1332 odr_reset (p->odr_in);
1334 #if TCL_MAJOR_VERSION == 8
1335 cs_fileno(p->cs_link) = -1;
1337 cs_close (p->cs_link);
1340 ODR_MASK_ZERO (&p->options);
1341 ODR_MASK_SET (&p->options, 0);
1342 ODR_MASK_SET (&p->options, 1);
1343 ODR_MASK_SET (&p->options, 4);
1344 ODR_MASK_SET (&p->options, 7);
1345 ODR_MASK_SET (&p->options, 14);
1347 ODR_MASK_ZERO (&p->protocolVersion);
1348 ODR_MASK_SET (&p->protocolVersion, 0);
1349 ODR_MASK_SET (&p->protocolVersion, 1);
1352 assert (!p->cs_link);
1356 * do_disconnect: disconnect method on IR object
1358 static int do_disconnect (void *obj, Tcl_Interp *interp,
1359 int argc, char **argv)
1365 p->state = IR_TCL_R_Idle;
1366 p->eventType = NULL;
1371 ir_tcl_disconnect (p);
1376 * do_comstack: Set/get comstack method on IR object
1378 static int do_comstack (void *o, Tcl_Interp *interp,
1379 int argc, char **argv)
1384 return ir_tcl_strdup (interp, &obj->comstackType, "tcpip");
1385 else if (argc == -1)
1386 return ir_tcl_strdel (interp, &obj->comstackType);
1389 xfree (obj->comstackType);
1390 if (ir_tcl_strdup (interp, &obj->comstackType, argv[2]) == TCL_ERROR)
1393 Tcl_AppendElement (interp, obj->comstackType);
1398 * do_logLevel: Set log level
1400 static int do_logLevel (void *o, Tcl_Interp *interp,
1401 int argc, char **argv)
1406 yaz_log_init (yaz_log_mask_str (argv[2]), "", NULL);
1408 yaz_log_init (yaz_log_mask_str (argv[2]), argv[3], NULL);
1410 yaz_log_init (yaz_log_mask_str (argv[2]), argv[3], argv[4]);
1416 * do_eventType: Return type of last event
1418 static int do_eventType (void *obj, Tcl_Interp *interp,
1419 int argc, char **argv)
1425 p->eventType = NULL;
1428 Tcl_AppendElement (interp, p->eventType ? p->eventType : "");
1434 * do_callback: add callback
1436 static int do_callback (void *obj, Tcl_Interp *interp,
1437 int argc, char **argv)
1446 else if (argc == -1)
1447 return ir_tcl_strdel (interp, &p->callback);
1450 xfree (p->callback);
1453 if (ir_tcl_strdup (interp, &p->callback, argv[2]) == TCL_ERROR)
1463 * do_failback: add error handle callback
1465 static int do_failback (void *obj, Tcl_Interp *interp,
1466 int argc, char **argv)
1475 else if (argc == -1)
1476 return ir_tcl_strdel (interp, &p->failback);
1479 xfree (p->failback);
1482 if (ir_tcl_strdup (interp, &p->failback, argv[2]) == TCL_ERROR)
1492 * do_initResponse: add init response handler
1494 static int do_initResponse (void *obj, Tcl_Interp *interp,
1495 int argc, char **argv)
1501 p->initResponse = NULL;
1504 else if (argc == -1)
1505 return ir_tcl_strdel (interp, &p->initResponse);
1508 xfree (p->initResponse);
1511 if (ir_tcl_strdup (interp, &p->initResponse, argv[2]) == TCL_ERROR)
1515 p->initResponse = NULL;
1520 * do_protocol: Set/get protocol method on IR object
1522 static int do_protocol (void *o, Tcl_Interp *interp, int argc, char **argv)
1528 p->protocol_type = PROTO_Z3950;
1533 if (!strcmp (argv[2], "Z39"))
1534 p->protocol_type = PROTO_Z3950;
1535 else if (!strcmp (argv[2], "SR"))
1536 p->protocol_type = PROTO_SR;
1539 Tcl_AppendResult (interp, "bad protocol ", argv[2], NULL);
1540 return ir_tcl_error_exec (interp, argc, argv);
1544 switch (p->protocol_type)
1547 Tcl_AppendElement (interp, "Z39");
1550 Tcl_AppendElement (interp, "SR");
1557 * do_triggerResourceControl:
1559 static int do_triggerResourceControl (void *obj, Tcl_Interp *interp,
1560 int argc, char **argv)
1564 Z_TriggerResourceControlRequest *req;
1565 bool_t is_false = 0;
1571 Tcl_AppendResult (interp, "not connected", NULL);
1572 return ir_tcl_error_exec (interp, argc, argv);
1574 apdu = zget_APDU (p->odr_out, Z_APDU_triggerResourceControlRequest);
1575 req = apdu->u.triggerResourceControlRequest;
1576 *req->requestedAction = Z_TriggerResourceCtrl_cancel;
1577 req->resultSetWanted = &is_false;
1579 return ir_tcl_send_APDU (interp, p, apdu, "triggerResourceControl",
1584 * do_databaseNames: specify database names
1586 static int do_databaseNames (void *obj, Tcl_Interp *interp,
1587 int argc, char **argv)
1590 IrTcl_SetCObj *p = obj;
1594 for (i=0; i<p->num_databaseNames; i++)
1595 xfree (p->databaseNames[i]);
1596 xfree (p->databaseNames);
1600 p->num_databaseNames = 0;
1601 p->databaseNames = NULL;
1606 for (i=0; i<p->num_databaseNames; i++)
1607 Tcl_AppendElement (interp, p->databaseNames[i]);
1610 if (p->databaseNames)
1612 for (i=0; i<p->num_databaseNames; i++)
1613 xfree (p->databaseNames[i]);
1614 xfree (p->databaseNames);
1616 p->num_databaseNames = argc - 2;
1618 ir_tcl_malloc (sizeof(*p->databaseNames) * (1+p->num_databaseNames));
1619 for (i=0; i<p->num_databaseNames; i++)
1621 if (ir_tcl_strdup (interp, &p->databaseNames[i], argv[2+i])
1625 p->databaseNames[i] = NULL;
1630 * do_replaceIndicator: Set/get replace Set indicator
1632 static int do_replaceIndicator (void *obj, Tcl_Interp *interp,
1633 int argc, char **argv)
1635 IrTcl_SetCObj *p = obj;
1639 p->replaceIndicator = 1;
1642 return ir_tcl_get_set_int (&p->replaceIndicator, interp, argc, argv);
1646 * do_queryType: Set/Get query method
1648 static int do_queryType (void *obj, Tcl_Interp *interp,
1649 int argc, char **argv)
1651 IrTcl_SetCObj *p = obj;
1654 return ir_tcl_strdup (interp, &p->queryType, "rpn");
1655 else if (argc == -1)
1656 return ir_tcl_strdel (interp, &p->queryType);
1659 xfree (p->queryType);
1660 if (ir_tcl_strdup (interp, &p->queryType, argv[2]) == TCL_ERROR)
1663 Tcl_AppendResult (interp, p->queryType, NULL);
1668 * do_userInformationField: Get User information field
1670 static int do_userInformationField (void *obj, Tcl_Interp *interp,
1671 int argc, char **argv)
1677 p->userInformationField = NULL;
1680 else if (argc == -1)
1681 return ir_tcl_strdel (interp, &p->userInformationField);
1682 Tcl_AppendResult (interp, p->userInformationField, NULL);
1687 * do_smallSetUpperBound: Set/get small set upper bound
1689 static int do_smallSetUpperBound (void *o, Tcl_Interp *interp,
1690 int argc, char **argv)
1692 IrTcl_SetCObj *p = o;
1696 p->smallSetUpperBound = 0;
1699 return ir_tcl_get_set_int (&p->smallSetUpperBound, interp, argc, argv);
1703 * do_largeSetLowerBound: Set/get large set lower bound
1705 static int do_largeSetLowerBound (void *o, Tcl_Interp *interp,
1706 int argc, char **argv)
1708 IrTcl_SetCObj *p = o;
1712 p->largeSetLowerBound = 1;
1715 return ir_tcl_get_set_int (&p->largeSetLowerBound, interp, argc, argv);
1719 * do_mediumSetPresentNumber: Set/get large set lower bound
1721 static int do_mediumSetPresentNumber (void *o, Tcl_Interp *interp,
1722 int argc, char **argv)
1724 IrTcl_SetCObj *p = o;
1728 p->mediumSetPresentNumber = 0;
1731 return ir_tcl_get_set_int (&p->mediumSetPresentNumber, interp, argc, argv);
1735 * do_referenceId: Set/Get referenceId
1737 static int do_referenceId (void *obj, Tcl_Interp *interp,
1738 int argc, char **argv)
1740 IrTcl_SetCObj *p = obj;
1744 p->referenceId = NULL;
1747 else if (argc == -1)
1748 return ir_tcl_strdel (interp, &p->referenceId);
1751 xfree (p->referenceId);
1752 if (ir_tcl_strdup (interp, &p->referenceId, argv[2]) == TCL_ERROR)
1755 Tcl_AppendResult (interp, p->referenceId, NULL);
1760 * do_preferredRecordSyntax: Set/get preferred record syntax
1762 static int do_preferredRecordSyntax (void *obj, Tcl_Interp *interp,
1763 int argc, char **argv)
1765 IrTcl_SetCObj *p = obj;
1769 p->preferredRecordSyntax = NULL;
1772 else if (argc == -1)
1774 xfree (p->preferredRecordSyntax);
1775 p->preferredRecordSyntax = NULL;
1780 xfree (p->preferredRecordSyntax);
1781 p->preferredRecordSyntax = NULL;
1782 if (argv[2][0] && (p->preferredRecordSyntax =
1783 ir_tcl_malloc (sizeof(*p->preferredRecordSyntax))))
1784 *p->preferredRecordSyntax = IrTcl_getRecordSyntaxVal (argv[2]);
1789 (interp,!p->preferredRecordSyntax ? "" :
1790 IrTcl_getRecordSyntaxStr(*p->preferredRecordSyntax));
1797 * do_elementSetNames: Set/Get element Set Names
1799 static int do_elementSetNames (void *obj, Tcl_Interp *interp,
1800 int argc, char **argv)
1802 IrTcl_SetCObj *p = obj;
1806 p->elementSetNames = NULL;
1809 else if (argc == -1)
1810 return ir_tcl_strdel (interp, &p->elementSetNames);
1813 xfree (p->elementSetNames);
1814 if (ir_tcl_strdup (interp, &p->elementSetNames, argv[2]) == TCL_ERROR)
1817 Tcl_AppendResult (interp, p->elementSetNames, NULL);
1822 * do_smallSetElementSetNames: Set/Get small Set Element Set Names
1824 static int do_smallSetElementSetNames (void *obj, Tcl_Interp *interp,
1825 int argc, char **argv)
1827 IrTcl_SetCObj *p = obj;
1831 p->smallSetElementSetNames = NULL;
1834 else if (argc == -1)
1835 return ir_tcl_strdel (interp, &p->smallSetElementSetNames);
1838 xfree (p->smallSetElementSetNames);
1839 if (ir_tcl_strdup (interp, &p->smallSetElementSetNames,
1840 argv[2]) == TCL_ERROR)
1843 Tcl_AppendResult (interp, p->smallSetElementSetNames, NULL);
1848 * do_mediumSetElementSetNames: Set/Get medium Set Element Set Names
1850 static int do_mediumSetElementSetNames (void *obj, Tcl_Interp *interp,
1851 int argc, char **argv)
1853 IrTcl_SetCObj *p = obj;
1857 p->mediumSetElementSetNames = NULL;
1860 else if (argc == -1)
1861 return ir_tcl_strdel (interp, &p->mediumSetElementSetNames);
1864 xfree (p->mediumSetElementSetNames);
1865 if (ir_tcl_strdup (interp, &p->mediumSetElementSetNames,
1866 argv[2]) == TCL_ERROR)
1869 Tcl_AppendResult (interp, p->mediumSetElementSetNames, NULL);
1873 static IrTcl_Method ir_method_tab[] = {
1874 { "comstack", do_comstack, NULL },
1875 { "protocol", do_protocol, NULL },
1876 { "failback", do_failback, NULL },
1877 { "failInfo", do_failInfo, NULL },
1878 { "apduInfo", do_apduInfo, NULL },
1879 { "logLevel", do_logLevel, NULL },
1881 { "eventType", do_eventType, NULL },
1882 { "connect", do_connect, NULL },
1883 { "protocolVersion", do_protocolVersion, NULL },
1884 { "preferredMessageSize", do_preferredMessageSize, NULL },
1885 { "maximumRecordSize", do_maximumRecordSize, NULL },
1886 { "implementationName", do_implementationName, NULL },
1887 { "implementationId", do_implementationId, NULL },
1888 { "implementationVersion", do_implementationVersion, NULL },
1889 { "targetImplementationName", do_targetImplementationName, NULL },
1890 { "targetImplementationId", do_targetImplementationId, NULL },
1891 { "targetImplementationVersion", do_targetImplementationVersion, NULL},
1892 { "userInformationField", do_userInformationField, NULL},
1893 { "idAuthentication", do_idAuthentication, NULL},
1894 { "options", do_options, NULL},
1895 { "init", do_init_request, NULL},
1896 { "initResult", do_initResult, NULL},
1897 { "disconnect", do_disconnect, NULL},
1898 { "callback", do_callback, NULL},
1899 { "initResponse", do_initResponse, NULL},
1900 { "triggerResourceControl", do_triggerResourceControl, NULL},
1901 { "initResponse", do_initResponse, NULL},
1905 static IrTcl_Method ir_set_c_method_tab[] = {
1906 { "databaseNames", do_databaseNames, NULL},
1907 { "replaceIndicator", do_replaceIndicator, NULL},
1908 { "queryType", do_queryType, NULL},
1909 { "preferredRecordSyntax", do_preferredRecordSyntax, NULL},
1910 { "smallSetUpperBound", do_smallSetUpperBound, NULL},
1911 { "largeSetLowerBound", do_largeSetLowerBound, NULL},
1912 { "mediumSetPresentNumber", do_mediumSetPresentNumber, NULL},
1913 { "referenceId", do_referenceId, NULL},
1914 { "elementSetNames", do_elementSetNames, NULL},
1915 { "smallSetElementSetNames", do_smallSetElementSetNames, NULL},
1916 { "mediumSetElementSetNames", do_mediumSetElementSetNames, NULL},
1921 * ir_obj_method: IR Object methods
1923 static int ir_obj_method (ClientData clientData, Tcl_Interp *interp,
1924 int argc, char **argv)
1926 IrTcl_Methods tab[3];
1927 IrTcl_Obj *p = (IrTcl_Obj *) clientData;
1932 Tcl_AppendResult (interp, wrongArgs, *argv, "method args...\"", NULL);
1936 tab[0].tab = ir_method_tab;
1938 tab[1].tab = ir_set_c_method_tab;
1939 tab[1].obj = &p->set_inher;
1942 if (ir_tcl_method (interp, argc, argv, tab, &r) == TCL_ERROR)
1943 return ir_tcl_method_error (interp, argc, argv, tab);
1948 * ir_obj_delete: IR Object disposal
1950 static void ir_obj_delete (ClientData clientData)
1952 IrTcl_Obj *obj = (IrTcl_Obj *) clientData;
1953 IrTcl_Methods tab[3];
1956 if (obj->ref_count > 0)
1958 assert (obj->ref_count == 0);
1960 logf (LOG_DEBUG, "ir object delete");
1961 tab[0].tab = ir_method_tab;
1963 tab[1].tab = ir_set_c_method_tab;
1964 tab[1].obj = &obj->set_inher;
1967 ir_tcl_method (NULL, -1, NULL, tab, NULL);
1970 odr_destroy (obj->odr_in);
1971 odr_destroy (obj->odr_out);
1974 obj->odr_pr->print = 0;
1975 odr_destroy (obj->odr_pr);
1981 * ir_obj_init: IR Object initialization
1983 int ir_obj_init (ClientData clientData, Tcl_Interp *interp,
1984 int argc, char **argv, ClientData *subData,
1985 ClientData parentData)
1987 IrTcl_Methods tab[3];
1995 Tcl_AppendResult (interp, wrongArgs, *argv, " objName\"", NULL);
1998 obj = ir_tcl_malloc (sizeof(*obj));
2001 obj->bibset = ccl_qual_mk ();
2002 if ((inf = fopen ("default.bib", "r")))
2004 ccl_qual_file (obj->bibset, inf);
2009 logf (LOG_DEBUG, "ir object create %s", argv[1]);
2010 obj->odr_in = odr_createmem (ODR_DECODE);
2011 odr_choice_enable_bias (obj->odr_in, 0);
2012 obj->odr_out = odr_createmem (ODR_ENCODE);
2016 obj->odr_pr = odr_createmem (ODR_PRINT);
2017 odr_setprint(obj->odr_pr, odr_print_file);
2019 obj->state = IR_TCL_R_Idle;
2020 obj->interp = interp;
2024 obj->request_queue = NULL;
2026 tab[0].tab = ir_method_tab;
2028 tab[1].tab = ir_set_c_method_tab;
2029 tab[1].obj = &obj->set_inher;
2032 if (ir_tcl_method (interp, 0, NULL, tab, NULL) == TCL_ERROR)
2034 Tcl_AppendResult (interp, "Failed to initialize ", argv[1], NULL);
2037 *subData = (ClientData) obj;
2043 * ir_obj_mk: IR Object creation
2045 static int ir_obj_mk (ClientData clientData, Tcl_Interp *interp,
2046 int argc, char **argv)
2049 int r = ir_obj_init (clientData, interp, argc, argv, &subData, 0);
2053 Tcl_CreateCommand (interp, argv[1], ir_obj_method,
2054 subData, ir_obj_delete);
2058 IrTcl_Class ir_obj_class = {
2066 /* ------------------------------------------------------- */
2068 * do_search: Do search request
2070 static int do_search (void *o, Tcl_Interp *interp, int argc, char **argv)
2072 Z_SearchRequest *req;
2076 IrTcl_SetObj *obj = o;
2079 #if TCL_MAJOR_VERSION > 8 || (TCL_MAJOR_VERSION == 8 && TCL_MINOR_VERSION > 0)
2091 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1], "query\"",
2095 #if TCL_MAJOR_VERSION > 8 || (TCL_MAJOR_VERSION == 8 && TCL_MINOR_VERSION > 0)
2096 query_str = Tcl_UtfToExternalDString(0, argv[2], -1, &ds);
2098 query_str = argv[2];
2100 logf (LOG_DEBUG, "search %s %s", *argv, query_str);
2101 if (!obj->set_inher.num_databaseNames)
2103 Tcl_AppendResult (interp, "no databaseNames", NULL);
2104 code = ir_tcl_error_exec (interp, argc, argv);
2109 Tcl_AppendResult (interp, "not connected", NULL);
2110 code = ir_tcl_error_exec (interp, argc, argv);
2113 apdu = zget_APDU (p->odr_out, Z_APDU_searchRequest);
2114 req = apdu->u.searchRequest;
2118 set_referenceId (p->odr_out, &req->referenceId,
2119 obj->set_inher.referenceId);
2121 req->smallSetUpperBound = &obj->set_inher.smallSetUpperBound;
2122 req->largeSetLowerBound = &obj->set_inher.largeSetLowerBound;
2123 req->mediumSetPresentNumber = &obj->set_inher.mediumSetPresentNumber;
2124 req->replaceIndicator = &obj->set_inher.replaceIndicator;
2125 req->resultSetName = obj->setName ? obj->setName : "default";
2126 logf (LOG_DEBUG, "Search, resultSetName %s", req->resultSetName);
2127 req->num_databaseNames = obj->set_inher.num_databaseNames;
2128 req->databaseNames = obj->set_inher.databaseNames;
2129 for (r=0; r < obj->set_inher.num_databaseNames; r++)
2130 logf (LOG_DEBUG, " Database %s", obj->set_inher.databaseNames[r]);
2131 if (obj->set_inher.preferredRecordSyntax)
2133 struct oident ident;
2135 ident.proto = p->protocol_type;
2136 ident.oclass = CLASS_RECSYN;
2137 ident.value = *obj->set_inher.preferredRecordSyntax;
2138 logf (LOG_DEBUG, "Preferred record syntax is %d", ident.value);
2139 req->preferredRecordSyntax = odr_oiddup (p->odr_out,
2140 oid_getoidbyent (&ident));
2143 req->preferredRecordSyntax = 0;
2145 if (obj->set_inher.smallSetElementSetNames &&
2146 *obj->set_inher.smallSetElementSetNames)
2148 Z_ElementSetNames *esn = odr_malloc (p->odr_out, sizeof(*esn));
2150 esn->which = Z_ElementSetNames_generic;
2151 esn->u.generic = obj->set_inher.smallSetElementSetNames;
2152 req->smallSetElementSetNames = esn;
2155 req->smallSetElementSetNames = NULL;
2157 if (obj->set_inher.mediumSetElementSetNames &&
2158 *obj->set_inher.mediumSetElementSetNames)
2160 Z_ElementSetNames *esn = odr_malloc (p->odr_out, sizeof(*esn));
2162 esn->which = Z_ElementSetNames_generic;
2163 esn->u.generic = obj->set_inher.mediumSetElementSetNames;
2164 req->mediumSetElementSetNames = esn;
2167 req->mediumSetElementSetNames = NULL;
2169 req->query = &query;
2171 logf (LOG_DEBUG, "queryType %s", obj->set_inher.queryType);
2172 if (!strcmp (obj->set_inher.queryType, "rpn"))
2174 Z_RPNQuery *RPNquery;
2176 RPNquery = p_query_rpn (p->odr_out, p->protocol_type, query_str);
2179 Tcl_AppendResult (interp, "query syntax error", NULL);
2180 code = ir_tcl_error_exec (interp, argc, argv);
2183 query.which = Z_Query_type_1;
2184 query.u.type_1 = RPNquery;
2187 else if (!strcmp (obj->set_inher.queryType, "cclrpn"))
2191 struct ccl_rpn_node *rpn;
2192 Z_RPNQuery *RPNquery;
2195 bib1.proto = p->protocol_type;
2196 bib1.oclass = CLASS_ATTSET;
2197 bib1.value = VAL_BIB1;
2199 rpn = ccl_find_str(p->bibset, query_str, &error, &pos);
2202 Tcl_AppendResult (interp, "ccl syntax error ", ccl_err_msg(error),
2204 code = ir_tcl_error_exec (interp, argc, argv);
2208 ccl_pr_tree (rpn, stderr);
2209 fprintf (stderr, "\n");
2211 RPNquery = ccl_rpn_query(p->odr_out, rpn);
2212 RPNquery->attributeSetId = oid_getoidbyent (&bib1);
2213 query.which = Z_Query_type_1;
2214 query.u.type_1 = RPNquery;
2217 else if (!strcmp (obj->set_inher.queryType, "ccl"))
2219 query.which = Z_Query_type_2;
2220 query.u.type_2 = &ccl_query;
2221 ccl_query.buf = (unsigned char *) query_str;
2222 ccl_query.len = strlen (query_str);
2226 Tcl_AppendResult (interp, "invalid query method ",
2227 obj->set_inher.queryType, NULL);
2228 code = ir_tcl_error_exec (interp, argc, argv);
2231 code = ir_tcl_send_APDU (interp, p, apdu, "search", *argv);
2233 #if TCL_MAJOR_VERSION > 8 || (TCL_MAJOR_VERSION == 8 && TCL_MINOR_VERSION > 0)
2234 Tcl_DStringFree (&ds);
2240 * do_searchResponse: add search response handler
2242 static int do_searchResponse (void *o, Tcl_Interp *interp,
2243 int argc, char **argv)
2245 IrTcl_SetObj *obj = o;
2249 obj->searchResponse = NULL;
2252 else if (argc == -1)
2253 return ir_tcl_strdel (interp, &obj->searchResponse);
2256 xfree (obj->searchResponse);
2259 if (ir_tcl_strdup (interp, &obj->searchResponse, argv[2])
2264 obj->searchResponse = NULL;
2270 * do_presentResponse: add present response handler
2272 static int do_presentResponse (void *o, Tcl_Interp *interp,
2273 int argc, char **argv)
2275 IrTcl_SetObj *obj = o;
2279 obj->presentResponse = NULL;
2282 else if (argc == -1)
2283 return ir_tcl_strdel (interp, &obj->presentResponse);
2286 xfree (obj->presentResponse);
2289 if (ir_tcl_strdup (interp, &obj->presentResponse, argv[2])
2294 obj->presentResponse = NULL;
2300 * do_sortResponse: add sort response handler
2302 static int do_sortResponse (void *o, Tcl_Interp *interp,
2303 int argc, char **argv)
2305 IrTcl_SetObj *obj = o;
2309 obj->sortResponse = NULL;
2312 else if (argc == -1)
2313 return ir_tcl_strdel (interp, &obj->sortResponse);
2316 xfree (obj->sortResponse);
2319 if (ir_tcl_strdup (interp, &obj->sortResponse, argv[2])
2324 obj->sortResponse = NULL;
2330 * do_resultCount: Get number of hits
2332 static int do_resultCount (void *o, Tcl_Interp *interp,
2333 int argc, char **argv)
2335 IrTcl_SetObj *obj = o;
2339 obj->resultCount = 0;
2342 return ir_tcl_get_set_int (&obj->resultCount, interp, argc, argv);
2346 * do_searchStatus: Get search status (after search response)
2348 static int do_searchStatus (void *o, Tcl_Interp *interp,
2349 int argc, char **argv)
2351 IrTcl_SetObj *obj = o;
2355 return ir_tcl_get_set_int (&obj->searchStatus, interp, argc, argv);
2358 static void reset_searchResult (IrTcl_SetObj *setobj)
2361 for (i = 0; i<setobj->searchResult_num; i++)
2362 xfree (setobj->searchResult_terms[i]);
2363 xfree (setobj->searchResult_terms);
2364 xfree (setobj->searchResult_count);
2365 setobj->searchResult_terms = 0;
2366 setobj->searchResult_num = 0;
2371 * do_searchResult Get USR:Search-Result1 (after search response)
2373 static int do_searchResult (void *o, Tcl_Interp *interp,
2374 int argc, char **argv)
2376 IrTcl_SetObj *obj = o;
2381 obj->searchResult_num = 0;
2382 obj->searchResult_terms = 0;
2383 obj->searchResult_count = 0;
2386 else if (argc == -1)
2388 reset_searchResult (obj);
2391 for (i = 0; i<obj->searchResult_num; i++)
2394 sprintf (str, "%d", obj->searchResult_count[i]);
2395 Tcl_AppendResult (interp, "{", NULL);
2396 if (obj->searchResult_terms[i])
2397 Tcl_AppendElement (interp, obj->searchResult_terms[i]);
2399 Tcl_AppendElement (interp, "");
2400 Tcl_AppendElement (interp, str);
2401 Tcl_AppendResult (interp, "} ", NULL);
2407 * do_presentStatus: Get search status (after search/present response)
2409 static int do_presentStatus (void *o, Tcl_Interp *interp,
2410 int argc, char **argv)
2412 IrTcl_SetObj *obj = o;
2416 return ir_tcl_get_set_int (&obj->presentStatus, interp, argc, argv);
2420 * do_sortStatus: Get sort status (after sort response)
2422 static int do_sortStatus (void *o, Tcl_Interp *interp,
2423 int argc, char **argv)
2425 IrTcl_SetObj *obj = o;
2430 obj->sortStatus = Z_SortStatus_failure;
2433 switch (obj->sortStatus)
2435 case Z_SortStatus_success:
2436 res = "success"; break;
2437 case Z_SortStatus_partial_1:
2438 res = "partial"; break;
2439 case Z_SortStatus_failure:
2440 res = "failure"; break;
2442 res = "unknown"; break;
2444 Tcl_AppendElement (interp, res);
2449 * do_nextResultSetPosition: Get next result set position
2450 * (after search/present response)
2452 static int do_nextResultSetPosition (void *o, Tcl_Interp *interp,
2453 int argc, char **argv)
2455 IrTcl_SetObj *obj = o;
2459 obj->nextResultSetPosition = 0;
2462 return ir_tcl_get_set_int (&obj->nextResultSetPosition, interp,
2467 * do_setName: Set result Set name
2469 static int do_setName (void *o, Tcl_Interp *interp,
2470 int argc, char **argv)
2472 IrTcl_SetObj *obj = o;
2475 return ir_tcl_strdup (interp, &obj->setName, "Default");
2476 else if (argc == -1)
2477 return ir_tcl_strdel (interp, &obj->setName);
2480 xfree (obj->setName);
2481 if (ir_tcl_strdup (interp, &obj->setName, argv[2])
2485 Tcl_AppendElement (interp, obj->setName);
2490 * do_numberOfRecordsReturned: Get number of records returned
2492 static int do_numberOfRecordsReturned (void *o, Tcl_Interp *interp,
2493 int argc, char **argv)
2495 IrTcl_SetObj *obj = o;
2499 obj->numberOfRecordsReturned = 0;
2502 return ir_tcl_get_set_int (&obj->numberOfRecordsReturned, interp,
2507 * do_type: Return type (if any) at position.
2509 static int do_type (void *o, Tcl_Interp *interp, int argc, char **argv)
2511 IrTcl_SetObj *obj = o;
2513 IrTcl_RecordList *rl;
2517 obj->record_list = NULL;
2520 else if (argc == -1)
2522 delete_IR_records (obj);
2527 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2528 " position\"", NULL);
2531 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2533 rl = find_IR_record (obj, offset);
2536 logf (LOG_DEBUG, "%s %s %s: no record", argv[0], argv[1], argv[2]);
2541 case Z_NamePlusRecord_databaseRecord:
2542 interp->result = "DB";
2544 case Z_NamePlusRecord_surrogateDiagnostic:
2545 interp->result = "SD";
2553 * do_recordType: Return record type (if any) at position.
2555 static int do_recordType (void *o, Tcl_Interp *interp, int argc, char **argv)
2557 IrTcl_SetObj *obj = o;
2559 IrTcl_RecordList *rl;
2565 else if (argc == -1)
2571 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2572 " position\"", NULL);
2575 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2577 rl = find_IR_record (obj, offset);
2580 logf (LOG_DEBUG, "%s %s %s: no record", argv[0], argv[1], argv[2]);
2583 if (rl->which != Z_NamePlusRecord_databaseRecord)
2585 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2588 Tcl_AppendElement (interp, (char*)
2589 IrTcl_getRecordSyntaxStr (rl->u.dbrec.type));
2594 * set record elements (for record extraction)
2596 static int do_recordElements (void *o, Tcl_Interp *interp,
2597 int argc, char **argv)
2599 IrTcl_SetObj *obj = o;
2603 obj->recordElements = NULL;
2606 else if (argc == -1)
2607 return ir_tcl_strdel (NULL, &obj->recordElements);
2610 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2611 " ?position?\"", NULL);
2616 xfree (obj->recordElements);
2617 return ir_tcl_strdup (NULL, &obj->recordElements,
2618 (*argv[2] ? argv[2] : NULL));
2620 Tcl_AppendResult (interp, obj->recordElements, NULL);
2627 static int ir_diagResult (Tcl_Interp *interp, IrTcl_Diagnostic *list, int num)
2633 for (i = 0; i<num; i++)
2635 sprintf (buf, "%d", list[i].condition);
2636 Tcl_AppendElement (interp, buf);
2637 cp = diagbib1_str (list[i].condition);
2639 Tcl_AppendElement (interp, (char*) cp);
2641 Tcl_AppendElement (interp, "");
2642 if (list[i].addinfo)
2643 Tcl_AppendElement (interp, (char*) list[i].addinfo);
2645 Tcl_AppendElement (interp, "");
2651 * do_diag: Return diagnostic record info
2653 static int do_diag (void *o, Tcl_Interp *interp, int argc, char **argv)
2655 IrTcl_SetObj *obj = o;
2657 IrTcl_RecordList *rl;
2663 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2664 " position\"", NULL);
2667 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2669 rl = find_IR_record (obj, offset);
2672 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2675 if (rl->which != Z_NamePlusRecord_surrogateDiagnostic)
2677 Tcl_AppendResult (interp, "No Diagnostic record at #", argv[2], NULL);
2680 return ir_diagResult (interp, rl->u.surrogateDiagnostics.list,
2681 rl->u.surrogateDiagnostics.num);
2685 * do_getMarc: Get ISO2709 Record lines/fields
2687 static int do_getMarc (void *o, Tcl_Interp *interp, int argc, char **argv)
2689 IrTcl_SetObj *obj = o;
2691 IrTcl_RecordList *rl;
2697 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2698 " position line|field tag ind field\"", NULL);
2701 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2703 rl = find_IR_record (obj, offset);
2706 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2709 if (rl->which != Z_NamePlusRecord_databaseRecord)
2711 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2714 return ir_tcl_get_marc (interp, rl->u.dbrec.buf, argc, argv);
2718 * do_getSutrs: Get SUTRS Record
2720 static int do_getSutrs (void *o, Tcl_Interp *interp, int argc, char **argv)
2722 IrTcl_SetObj *obj = o;
2724 IrTcl_RecordList *rl;
2730 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2731 " position\"", NULL);
2734 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2736 rl = find_IR_record (obj, offset);
2739 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2742 if (rl->which != Z_NamePlusRecord_databaseRecord)
2744 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2747 if (!rl->u.dbrec.buf || rl->u.dbrec.type != VAL_SUTRS)
2749 Tcl_AppendElement (interp, rl->u.dbrec.buf);
2755 * do_getGrs: Get a GRS-1 Record
2757 static int do_getGrs (void *o, Tcl_Interp *interp, int argc, char **argv)
2759 IrTcl_SetObj *obj = o;
2761 IrTcl_RecordList *rl;
2767 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2768 " position ?(set,tag) (set,tag) ...?\"", NULL);
2771 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2773 rl = find_IR_record (obj, offset);
2776 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2779 if (rl->which != Z_NamePlusRecord_databaseRecord)
2781 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2784 if (rl->u.dbrec.type != VAL_GRS1)
2786 return ir_tcl_get_grs (interp, rl->u.dbrec.u.grs1, argc, argv);
2791 * do_getExplain: Get an Explain Record
2793 static int do_getExplain (void *o, Tcl_Interp *interp, int argc, char **argv)
2795 IrTcl_SetObj *obj = o;
2796 IrTcl_Obj *p = obj->parent;
2798 Z_ext_typeent *etype;
2800 IrTcl_RecordList *rl;
2806 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2807 " position ?mask? ...\"", NULL);
2810 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2812 rl = find_IR_record (obj, offset);
2815 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2818 if (rl->which != Z_NamePlusRecord_databaseRecord)
2820 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2823 if (!rl->u.dbrec.buf || rl->u.dbrec.type != VAL_EXPLAIN)
2826 if (!(etype = z_ext_getentbyref (VAL_EXPLAIN)))
2828 assert (rl->u.dbrec.buf);
2829 odr_setbuf (p->odr_in, rl->u.dbrec.buf, rl->u.dbrec.size, 0);
2830 if (!(*etype->fun)(p->odr_in, (char **) &rr, 0, 0))
2833 if (etype->what != Z_External_explainRecord)
2836 return ir_tcl_get_explain (interp, rr, argc, argv);
2840 * do_responseStatus: Return response status (present or search)
2842 static int do_responseStatus (void *o, Tcl_Interp *interp,
2843 int argc, char **argv)
2845 IrTcl_SetObj *obj = o;
2849 obj->recordFlag = 0;
2850 obj->nonSurrogateDiagnosticNum = 0;
2851 obj->nonSurrogateDiagnosticList = NULL;
2854 else if (argc == -1)
2856 ir_deleteDiags (&obj->nonSurrogateDiagnosticList,
2857 &obj->nonSurrogateDiagnosticNum);
2860 if (!obj->recordFlag)
2862 Tcl_AppendElement (interp, "OK");
2867 case Z_Records_DBOSD:
2868 Tcl_AppendElement (interp, "DBOSD");
2871 Tcl_AppendElement (interp, "NSD");
2872 return ir_diagResult (interp, obj->nonSurrogateDiagnosticList,
2873 obj->nonSurrogateDiagnosticNum);
2874 case Z_Records_multipleNSD:
2875 Tcl_AppendElement (interp, "NSD");
2876 return ir_diagResult (interp, obj->nonSurrogateDiagnosticList,
2877 obj->nonSurrogateDiagnosticNum);
2883 * do_present: Perform Present Request
2886 static int do_present (void *o, Tcl_Interp *interp, int argc, char **argv)
2888 IrTcl_SetObj *obj = o;
2891 Z_PresentRequest *req;
2899 if (Tcl_GetInt (interp, argv[2], &start) == TCL_ERROR)
2906 if (Tcl_GetInt (interp, argv[3], &number) == TCL_ERROR)
2911 logf (LOG_DEBUG, "present %s %d %d", *argv, start, number);
2915 Tcl_AppendResult (interp, "not connected", NULL);
2916 return ir_tcl_error_exec (interp, argc, argv);
2919 obj->number = number;
2921 apdu = zget_APDU (p->odr_out, Z_APDU_presentRequest);
2922 req = apdu->u.presentRequest;
2924 set_referenceId (p->odr_out, &req->referenceId,
2925 obj->set_inher.referenceId);
2927 req->resultSetId = obj->setName ? obj->setName : "Default";
2929 req->resultSetStartPoint = &start;
2930 req->numberOfRecordsRequested = &number;
2931 if (obj->set_inher.preferredRecordSyntax)
2933 struct oident ident;
2935 ident.proto = p->protocol_type;
2936 ident.oclass = CLASS_RECSYN;
2937 ident.value = *obj->set_inher.preferredRecordSyntax;
2938 logf (LOG_DEBUG, "Preferred record syntax is %d", ident.value);
2939 req->preferredRecordSyntax = odr_oiddup (p->odr_out,
2940 oid_getoidbyent (&ident));
2943 req->preferredRecordSyntax = 0;
2945 if (obj->set_inher.elementSetNames && *obj->set_inher.elementSetNames)
2947 Z_ElementSetNames *esn = odr_malloc (p->odr_out, sizeof(*esn));
2948 Z_RecordComposition *compo = odr_malloc (p->odr_out, sizeof(*compo));
2950 esn->which = Z_ElementSetNames_generic;
2951 esn->u.generic = obj->set_inher.elementSetNames;
2953 req->recordComposition = compo;
2954 compo->which = Z_RecordComp_simple;
2955 compo->u.simple = esn;
2958 req->recordComposition = NULL;
2959 return ir_tcl_send_APDU (interp, p, apdu, "present", *argv);
2962 #define IR_TCL_RECORD_ENCODING_ISO2709 1
2963 #define IR_TCL_RECORD_ENCODING_RAW 2
2969 } IrTcl_FileRecordHead;
2972 * do_loadFile: Load result set from file
2974 static int do_loadFile (void *o, Tcl_Interp *interp,
2975 int argc, char **argv)
2977 IrTcl_SetObj *setobj = o;
2989 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2990 " filename ?start? ?number?\"", NULL);
2994 start = atoi (argv[3]);
2996 number = atoi (argv[4]);
2999 inf = fopen (argv[2], "r");
3002 Tcl_AppendResult (interp, "Cannot open ", argv[2], NULL);
3003 return ir_tcl_error_exec (interp, argc, argv);
3005 while (offset < (start+number))
3007 IrTcl_FileRecordHead head;
3008 IrTcl_RecordList *rl;
3010 if (fread (&head, sizeof(head), 1, inf) < 1)
3012 rl = new_IR_record (setobj, offset,
3013 Z_NamePlusRecord_databaseRecord,
3014 (argc > 5) ? argv[5] : NULL);
3015 rl->u.dbrec.type = head.syntax;
3016 if (head.encoding == IR_TCL_RECORD_ENCODING_ISO2709)
3018 if (!(buf = ir_tcl_fread_marc (inf, &size)))
3020 rl->u.dbrec.buf = buf;
3021 rl->u.dbrec.size = size;
3022 if (size != head.size)
3025 Tcl_AppendResult (interp, "bad ISO2709 encoding", NULL);
3026 return ir_tcl_error_exec (interp, argc, argv);
3029 else if (head.encoding == IR_TCL_RECORD_ENCODING_RAW)
3031 rl->u.dbrec.size = head.size;
3032 rl->u.dbrec.buf = ir_tcl_malloc (head.size + 1);
3033 if (fread (rl->u.dbrec.buf, rl->u.dbrec.size, 1, inf) < 1)
3036 Tcl_AppendResult (interp, "bad raw encoding", NULL);
3037 return ir_tcl_error_exec (interp, argc, argv);
3039 rl->u.dbrec.buf[rl->u.dbrec.size] = '\0';
3043 rl->u.dbrec.buf = NULL;
3044 rl->u.dbrec.size = 0;
3046 Tcl_AppendResult (interp, "bad encoding", NULL);
3047 return ir_tcl_error_exec (interp, argc, argv);
3051 setobj->numberOfRecordsReturned = offset - start;
3057 * do_saveFile: Save result set on file
3059 static int do_saveFile (void *o, Tcl_Interp *interp,
3060 int argc, char **argv)
3062 IrTcl_SetObj *setobj = o;
3067 IrTcl_RecordList *rl;
3073 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
3074 " filename ?start? ?number?\"", NULL);
3078 start = atoi (argv[3]);
3080 number = atoi (argv[4]);
3083 outf = fopen (argv[2], "w");
3086 Tcl_AppendResult (interp, "cannot open file", NULL);
3087 return ir_tcl_error_exec (interp, argc, argv);
3089 while (offset < (start+number) && (rl = find_IR_record (setobj, offset)))
3091 if (rl->which == Z_NamePlusRecord_databaseRecord &&
3092 rl->u.dbrec.buf && rl->u.dbrec.size)
3094 IrTcl_FileRecordHead head;
3096 head.encoding = IR_TCL_RECORD_ENCODING_RAW;
3097 head.syntax = rl->u.dbrec.type;
3098 head.size = rl->u.dbrec.size;
3099 if (fwrite (&head, sizeof(head), 1, outf) < 1)
3101 Tcl_AppendResult (interp, "cannot write", NULL);
3102 return ir_tcl_error_exec (interp, argc, argv);
3104 if (fwrite (rl->u.dbrec.buf, rl->u.dbrec.size, 1, outf) < 1)
3106 Tcl_AppendResult (interp, "cannot write", NULL);
3107 return ir_tcl_error_exec (interp, argc, argv);
3114 Tcl_AppendResult (interp, "cannot write ", NULL);
3115 return ir_tcl_error_exec (interp, argc, argv);
3121 /* ------------------------------------------------------- */
3123 * do_sort: Do sort request
3125 static int do_sort (void *o, Tcl_Interp *interp, int argc, char **argv)
3129 IrTcl_SetObj *obj = o;
3131 char sort_string[64], sort_flags[64];
3134 Z_SortKeySpecList *sksl;
3145 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1], "query\"",
3149 logf (LOG_DEBUG, "sort %s %s", *argv, argv[2]);
3152 Tcl_AppendResult (interp, "not connected", NULL);
3153 return ir_tcl_error_exec (interp, argc, argv);
3155 apdu = zget_APDU (p->odr_out, Z_APDU_sortRequest);
3156 sksl = (Z_SortKeySpecList *) odr_malloc (p->odr_out, sizeof(*sksl));
3157 req = apdu->u.sortRequest;
3159 set_referenceId (p->odr_out, &req->referenceId,
3160 obj->set_inher.referenceId);
3163 req->num_inputResultSetNames = 1;
3164 req->inputResultSetNames = (Z_InternationalString **)
3165 odr_malloc (p->odr_out, sizeof(*req->inputResultSetNames));
3166 req->inputResultSetNames[0] = obj->setName;
3168 req->inputResultSetNames =
3169 (Z_StringList *)odr_malloc (p->odr_out,
3170 sizeof(*req->inputResultSetNames));
3171 req->inputResultSetNames->num_strings = 1;
3172 req->inputResultSetNames->strings =
3173 (char **)odr_malloc (p->odr_out,
3174 sizeof(*req->inputResultSetNames->strings));
3175 req->inputResultSetNames->strings[0] = obj->setName;
3178 req->sortedResultSetName = (char *) obj->setName;
3181 req->sortSequence = sksl;
3182 sksl->num_specs = 0;
3183 sksl->specs = (Z_SortKeySpec **)
3184 odr_malloc (p->odr_out, sizeof(sksl->specs) * 20);
3186 bib1.proto = PROTO_Z3950;
3187 bib1.oclass = CLASS_ATTSET;
3188 bib1.value = VAL_BIB1;
3190 while ((sscanf (arg, "%63s %63s%n", sort_string, sort_flags, &off)) == 2
3194 char *sort_string_sep;
3195 Z_SortKeySpec *sks = (Z_SortKeySpec *)
3196 odr_malloc (p->odr_out, sizeof(*sks));
3197 Z_SortKey *sk = (Z_SortKey *)
3198 odr_malloc (p->odr_out, sizeof(*sk));
3201 sksl->specs[sksl->num_specs++] = sks;
3202 sks->sortElement = (Z_SortElement *)
3203 odr_malloc (p->odr_out, sizeof(*sks->sortElement));
3204 sks->sortElement->which = Z_SortElement_generic;
3205 sks->sortElement->u.generic = sk;
3207 if ((sort_string_sep = strchr (sort_string, '=')))
3209 Z_AttributeElement *el = (Z_AttributeElement *)
3210 odr_malloc (p->odr_out, sizeof(*el));
3211 sk->which = Z_SortKey_sortAttributes;
3212 sk->u.sortAttributes =
3213 (Z_SortAttributes *)
3214 odr_malloc (p->odr_out, sizeof(*sk->u.sortAttributes));
3215 sk->u.sortAttributes->id = oid_ent_to_oid(&bib1, oid);
3216 sk->u.sortAttributes->list =
3218 odr_malloc (p->odr_out, sizeof(*sk->u.sortAttributes->list));
3219 sk->u.sortAttributes->list->num_attributes = 1;
3220 sk->u.sortAttributes->list->attributes =
3221 (Z_AttributeElement **)odr_malloc (p->odr_out,
3222 sizeof(*sk->u.sortAttributes->list->attributes));
3223 sk->u.sortAttributes->list->attributes[0] = el;
3224 el->attributeSet = 0;
3225 el->attributeType = (int *)
3226 odr_malloc (p->odr_out, sizeof(*el->attributeType));
3227 *el->attributeType = atoi (sort_string);
3228 el->which = Z_AttributeValue_numeric;
3229 el->value.numeric = (int *)
3230 odr_malloc (p->odr_out, sizeof(*el->value.numeric));
3231 *el->value.numeric = atoi (sort_string_sep + 1);
3235 sk->which = Z_SortKey_sortField;
3236 sk->u.sortField = (char *)odr_malloc (p->odr_out, strlen(sort_string)+1);
3237 strcpy (sk->u.sortField, sort_string);
3239 sks->sortRelation = (int *)
3240 odr_malloc (p->odr_out, sizeof(*sks->sortRelation));
3241 *sks->sortRelation = Z_SortRelation_ascending;
3242 sks->caseSensitivity = (int *)
3243 odr_malloc (p->odr_out, sizeof(*sks->caseSensitivity));
3244 *sks->caseSensitivity = Z_SortCase_caseSensitive;
3247 sks->which = Z_SortKeySpec_null;
3248 sks->u.null = odr_nullval ();
3250 sks->missingValueAction = NULL;
3253 for (i = 0; sort_flags[i]; i++)
3255 switch (sort_flags[i])
3260 *sks->sortRelation = Z_SortRelation_descending;
3265 *sks->sortRelation = Z_SortRelation_ascending;
3269 *sks->caseSensitivity = Z_SortCase_caseInsensitive;
3273 *sks->caseSensitivity = Z_SortCase_caseSensitive;
3278 if (!sksl->num_specs)
3280 printf ("Missing sort specifications\n");
3283 return ir_tcl_send_APDU (interp, p, apdu, "sort", *argv);
3286 static IrTcl_Method ir_set_method_tab[] = {
3287 { "search", do_search, NULL},
3288 { "searchResponse", do_searchResponse, NULL},
3289 { "presentResponse", do_presentResponse, NULL},
3290 { "searchStatus", do_searchStatus, NULL},
3291 { "presentStatus", do_presentStatus, NULL},
3292 { "nextResultSetPosition", do_nextResultSetPosition, NULL},
3293 { "setName", do_setName, NULL},
3294 { "resultCount", do_resultCount, NULL},
3295 { "numberOfRecordsReturned", do_numberOfRecordsReturned, NULL},
3296 { "present", do_present, NULL},
3297 { "type", do_type, NULL},
3298 { "getMarc", do_getMarc, NULL},
3299 { "getSutrs", do_getSutrs, NULL},
3300 { "getGrs", do_getGrs, NULL},
3301 { "getExplain", do_getExplain, NULL},
3302 { "recordType", do_recordType, NULL},
3303 { "recordElements", do_recordElements, NULL},
3304 { "diag", do_diag, NULL},
3305 { "responseStatus", do_responseStatus, NULL},
3306 { "loadFile", do_loadFile, NULL},
3307 { "saveFile", do_saveFile, NULL},
3308 { "sort", do_sort, NULL },
3309 { "sortResponse", do_sortResponse, NULL},
3310 { "sortStatus", do_sortStatus, NULL},
3311 { "searchResult", do_searchResult, NULL},
3316 * ir_set_obj_method: IR Set Object methods
3318 static int ir_set_obj_method (ClientData clientData, Tcl_Interp *interp,
3319 int argc, char **argv)
3321 IrTcl_Methods tabs[3];
3322 IrTcl_SetObj *p = (IrTcl_SetObj *) clientData;
3327 Tcl_AppendResult (interp, wrongArgs, *argv, " method args...\"", NULL);
3330 tabs[0].tab = ir_set_method_tab;
3332 tabs[1].tab = ir_set_c_method_tab;
3333 tabs[1].obj = &p->set_inher;
3336 if (ir_tcl_method (interp, argc, argv, tabs, &r) == TCL_ERROR)
3337 return ir_tcl_method_error (interp, argc, argv, tabs);
3342 * ir_set_obj_delete: IR Set Object disposal
3344 static void ir_set_obj_delete (ClientData clientData)
3346 IrTcl_Methods tabs[3];
3347 IrTcl_SetObj *p = (IrTcl_SetObj *) clientData;
3349 logf (LOG_DEBUG, "ir set delete");
3351 tabs[0].tab = ir_set_method_tab;
3353 tabs[1].tab = ir_set_c_method_tab;
3354 tabs[1].obj = &p->set_inher;
3357 ir_tcl_method (NULL, -1, NULL, tabs, NULL);
3363 * ir_set_obj_init: IR Set Object initialization
3365 static int ir_set_obj_init (ClientData clientData, Tcl_Interp *interp,
3366 int argc, char **argv, ClientData *subData,
3367 ClientData parentData)
3369 IrTcl_Methods tabs[3];
3372 if (argc < 2 || argc > 3)
3374 Tcl_AppendResult (interp, wrongArgs, *argv,
3375 " objSetName ?objParent?\"", NULL);
3378 obj = ir_tcl_malloc (sizeof(*obj));
3379 logf (LOG_DEBUG, "ir set create %s", argv[1]);
3386 obj->parent = (IrTcl_Obj *) parentData;
3388 dst = &obj->set_inher;
3389 src = &obj->parent->set_inher;
3391 if ((dst->num_databaseNames = src->num_databaseNames))
3393 dst->databaseNames =
3394 ir_tcl_malloc (sizeof (*dst->databaseNames)
3395 * (1+dst->num_databaseNames));
3396 for (i = 0; i < dst->num_databaseNames; i++)
3397 if (ir_tcl_strdup (interp, &dst->databaseNames[i],
3398 src->databaseNames[i]) == TCL_ERROR)
3400 dst->databaseNames[i] = NULL;
3403 dst->databaseNames = NULL;
3404 if (ir_tcl_strdup (interp, &dst->queryType, src->queryType)
3408 if (ir_tcl_strdup (interp, &dst->referenceId, src->referenceId)
3412 if (ir_tcl_strdup (interp, &dst->elementSetNames, src->elementSetNames)
3416 if (ir_tcl_strdup (interp, &dst->smallSetElementSetNames,
3417 src->smallSetElementSetNames)
3421 if (ir_tcl_strdup (interp, &dst->mediumSetElementSetNames,
3422 src->mediumSetElementSetNames)
3426 if (src->preferredRecordSyntax &&
3427 (dst->preferredRecordSyntax
3428 = ir_tcl_malloc (sizeof(*dst->preferredRecordSyntax))))
3429 *dst->preferredRecordSyntax = *src->preferredRecordSyntax;
3431 dst->preferredRecordSyntax = NULL;
3432 dst->replaceIndicator = src->replaceIndicator;
3433 dst->smallSetUpperBound = src->smallSetUpperBound;
3434 dst->largeSetLowerBound = src->largeSetLowerBound;
3435 dst->mediumSetPresentNumber = src->mediumSetPresentNumber;
3440 tabs[0].tab = ir_set_method_tab;
3444 if (ir_tcl_method (interp, 0, NULL, tabs, NULL) == TCL_ERROR)
3447 *subData = (ClientData) obj;
3452 * ir_set_obj_mk: IR Set Object creation
3454 static int ir_set_obj_mk (ClientData clientData, Tcl_Interp *interp,
3455 int argc, char **argv)
3458 ClientData parentData = 0;
3463 Tcl_CmdInfo parent_info;
3464 if (!Tcl_GetCommandInfo (interp, argv[2], &parent_info))
3466 Tcl_AppendResult (interp, "no object parent", NULL);
3467 return ir_tcl_error_exec (interp, argc, argv);
3469 parentData = parent_info.clientData;
3471 r = ir_set_obj_init (clientData, interp, argc, argv, &subData, parentData);
3474 Tcl_CreateCommand (interp, argv[1], ir_set_obj_method,
3475 subData, ir_set_obj_delete);
3479 IrTcl_Class ir_set_obj_class = {
3486 /* ------------------------------------------------------- */
3489 * do_scan: Perform scan
3491 static int do_scan (void *o, Tcl_Interp *interp, int argc, char **argv)
3495 IrTcl_ScanObj *obj = o;
3496 IrTcl_Obj *p = obj->parent;
3501 struct ccl_rpn_node *rpn;
3505 #if TCL_MAJOR_VERSION > 8 || (TCL_MAJOR_VERSION == 8 && TCL_MINOR_VERSION > 0)
3513 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
3514 " scanQuery\"", NULL);
3517 #if TCL_MAJOR_VERSION > 8 || (TCL_MAJOR_VERSION == 8 && TCL_MINOR_VERSION > 0)
3518 start_term = Tcl_UtfToExternalDString(0, argv[2], -1, &ds);
3520 start_term = argv[2];
3522 logf (LOG_DEBUG, "scan %s %s", *argv, start_term);
3523 if (!p->set_inher.num_databaseNames)
3525 Tcl_AppendResult (interp, "no databaseNames", NULL);
3526 code = ir_tcl_error_exec (interp, argc, argv);
3531 Tcl_AppendResult (interp, "not connected", NULL);
3532 code = ir_tcl_error_exec (interp, argc, argv);
3536 apdu = zget_APDU (p->odr_out, Z_APDU_scanRequest);
3537 req = apdu->u.scanRequest;
3539 set_referenceId (p->odr_out, &req->referenceId, p->set_inher.referenceId);
3540 req->num_databaseNames = p->set_inher.num_databaseNames;
3541 req->databaseNames = p->set_inher.databaseNames;
3545 if (!(req->termListAndStartPoint =
3546 p_query_scan (p->odr_out, p->protocol_type,
3547 &req->attributeSet, start_term)))
3549 Tcl_AppendResult (interp, "query syntax error", NULL);
3550 code = ir_tcl_error_exec (interp, argc, argv);
3554 rpn = ccl_find_str(p->bibset, start_term, &r, &pos);
3557 Tcl_AppendResult (interp, "ccl syntax error ", ccl_err_msg(r), NULL);
3558 code = ir_tcl_error_exec (interp, argc, argv);
3561 bib1.proto = p->protocol_type;
3562 bib1.oclass = CLASS_ATTSET;
3563 bib1.value = VAL_BIB1;
3565 req->attributeSet = oid_getoidbyent (&bib1);
3566 if (!(req->termListAndStartPoint = ccl_scan_query (p->odr_out, rpn)))
3572 req->stepSize = &obj->stepSize;
3573 req->numberOfTermsRequested = &obj->numberOfTermsRequested;
3574 req->preferredPositionInResponse = &obj->preferredPositionInResponse;
3575 logf (LOG_DEBUG, "stepSize=%d", *req->stepSize);
3576 logf (LOG_DEBUG, "numberOfTermsRequested=%d",
3577 *req->numberOfTermsRequested);
3578 logf (LOG_DEBUG, "preferredPositionInResponse=%d",
3579 *req->preferredPositionInResponse);
3581 code = ir_tcl_send_APDU (interp, p, apdu, "scan", *argv);
3583 #if TCL_MAJOR_VERSION > 8 || (TCL_MAJOR_VERSION == 8 && TCL_MINOR_VERSION > 0)
3584 Tcl_DStringFree (&ds);
3590 * do_scanResponse: add scan response handler
3592 static int do_scanResponse (void *o, Tcl_Interp *interp,
3593 int argc, char **argv)
3595 IrTcl_ScanObj *obj = o;
3599 obj->scanResponse = NULL;
3602 else if (argc == -1)
3603 return ir_tcl_strdel (interp, &obj->scanResponse);
3606 xfree (obj->scanResponse);
3609 if (ir_tcl_strdup (interp, &obj->scanResponse, argv[2])
3614 obj->scanResponse = NULL;
3620 * do_stepSize: Set/get replace Step Size
3622 static int do_stepSize (void *obj, Tcl_Interp *interp,
3623 int argc, char **argv)
3625 IrTcl_ScanObj *p = obj;
3631 return ir_tcl_get_set_int (&p->stepSize, interp, argc, argv);
3635 * do_numberOfTermsRequested: Set/get Number of Terms requested
3637 static int do_numberOfTermsRequested (void *obj, Tcl_Interp *interp,
3638 int argc, char **argv)
3640 IrTcl_ScanObj *p = obj;
3644 p->numberOfTermsRequested = 20;
3647 return ir_tcl_get_set_int (&p->numberOfTermsRequested, interp, argc, argv);
3652 * do_preferredPositionInResponse: Set/get preferred Position
3654 static int do_preferredPositionInResponse (void *obj, Tcl_Interp *interp,
3655 int argc, char **argv)
3657 IrTcl_ScanObj *p = obj;
3661 p->preferredPositionInResponse = 1;
3664 return ir_tcl_get_set_int (&p->preferredPositionInResponse, interp,
3669 * do_scanStatus: Get scan status
3671 static int do_scanStatus (void *obj, Tcl_Interp *interp,
3672 int argc, char **argv)
3674 IrTcl_ScanObj *p = obj;
3678 return ir_tcl_get_set_int (&p->scanStatus, interp, argc, argv);
3682 * do_numberOfEntriesReturned: Get number of Entries returned
3684 static int do_numberOfEntriesReturned (void *obj, Tcl_Interp *interp,
3685 int argc, char **argv)
3687 IrTcl_ScanObj *p = obj;
3691 return ir_tcl_get_set_int (&p->numberOfEntriesReturned, interp,
3696 * do_positionOfTerm: Get position of Term
3698 static int do_positionOfTerm (void *obj, Tcl_Interp *interp,
3699 int argc, char **argv)
3701 IrTcl_ScanObj *p = obj;
3705 return ir_tcl_get_set_int (&p->positionOfTerm, interp, argc, argv);
3709 * do_scanLine: get Scan Line (surrogate or normal) after response
3711 static int do_scanLine (void *obj, Tcl_Interp *interp, int argc, char **argv)
3713 IrTcl_ScanObj *p = obj;
3719 p->entries_flag = 0;
3721 p->nonSurrogateDiagnosticNum = 0;
3722 p->nonSurrogateDiagnosticList = 0;
3725 else if (argc == -1)
3727 p->entries_flag = 0;
3728 /* release entries */
3731 ir_deleteDiags (&p->nonSurrogateDiagnosticList,
3732 &p->nonSurrogateDiagnosticNum);
3737 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
3738 " position\"", NULL);
3741 if (Tcl_GetInt (interp, argv[2], &i) == TCL_ERROR)
3743 if (!p->entries_flag || !p->entries || i >= p->num_entries || i < 0)
3745 switch (p->entries[i].which)
3747 case Z_Entry_termInfo:
3748 Tcl_AppendElement (interp, "T");
3749 if (p->entries[i].u.term.buf)
3750 Tcl_AppendElement (interp, p->entries[i].u.term.buf);
3752 Tcl_AppendElement (interp, "");
3753 sprintf (numstr, "%d", p->entries[i].u.term.globalOccurrences);
3754 Tcl_AppendElement (interp, numstr);
3756 case Z_Entry_surrogateDiagnostic:
3757 Tcl_AppendElement (interp, "SD");
3758 return ir_diagResult (interp, p->entries[i].u.diag.list,
3759 p->entries[i].u.diag.num);
3765 static IrTcl_Method ir_scan_method_tab[] = {
3766 { "scan", do_scan, NULL},
3767 { "scanResponse", do_scanResponse, NULL},
3768 { "stepSize", do_stepSize, NULL},
3769 { "numberOfTermsRequested", do_numberOfTermsRequested, NULL},
3770 { "preferredPositionInResponse", do_preferredPositionInResponse, NULL},
3771 { "scanStatus", do_scanStatus, NULL},
3772 { "numberOfEntriesReturned", do_numberOfEntriesReturned, NULL},
3773 { "positionOfTerm", do_positionOfTerm, NULL},
3774 { "scanLine", do_scanLine, NULL},
3779 * ir_scan_obj_method: IR Scan Object methods
3781 static int ir_scan_obj_method (ClientData clientData, Tcl_Interp *interp,
3782 int argc, char **argv)
3784 IrTcl_Methods tabs[2];
3789 Tcl_AppendResult (interp, wrongArgs, *argv, " method args...\"", NULL);
3792 tabs[0].tab = ir_scan_method_tab;
3793 tabs[0].obj = clientData;
3796 if (ir_tcl_method (interp, argc, argv, tabs, &r) == TCL_ERROR)
3797 return ir_tcl_method_error (interp, argc, argv, tabs);
3802 * ir_scan_obj_delete: IR Scan Object disposal
3804 static void ir_scan_obj_delete (ClientData clientData)
3806 IrTcl_Methods tabs[2];
3807 IrTcl_ScanObj *obj = (IrTcl_ScanObj *) clientData;
3809 tabs[0].tab = ir_scan_method_tab;
3813 ir_tcl_method (NULL, -1, NULL, tabs, NULL);
3818 * ir_scan_obj_mk: IR Scan Object creation
3820 static int ir_scan_obj_mk (ClientData clientData, Tcl_Interp *interp,
3821 int argc, char **argv)
3823 Tcl_CmdInfo parent_info;
3825 IrTcl_Methods tabs[2];
3829 Tcl_AppendResult (interp, wrongArgs, *argv,
3830 "objScanName objParentName\"", NULL);
3833 logf (LOG_DEBUG, "ir scan create %s", argv[1]);
3834 if (!Tcl_GetCommandInfo (interp, argv[2], &parent_info))
3836 Tcl_AppendResult (interp, "no object parent", NULL);
3837 return ir_tcl_error_exec (interp, argc, argv);
3839 obj = ir_tcl_malloc (sizeof(*obj));
3840 obj->parent = (IrTcl_Obj *) parent_info.clientData;
3842 tabs[0].tab = ir_scan_method_tab;
3846 if (ir_tcl_method (interp, 0, NULL, tabs, NULL) == TCL_ERROR)
3848 Tcl_CreateCommand (interp, argv[1], ir_scan_obj_method,
3849 (ClientData) obj, ir_scan_obj_delete);
3853 /* ------------------------------------------------------- */
3856 * ir_log_init_proc: set yaz log level
3858 static int ir_log_init_proc (ClientData clientData, Tcl_Interp *interp,
3859 int argc, char **argv)
3862 if (argc <= 1 || argc > 4)
3864 Tcl_AppendResult (interp, wrongArgs, *argv,
3865 " ?level ?prefix ?filename\"", NULL);
3868 lev = yaz_log_mask_str (argv[1]);
3870 yaz_log_init (lev, "", NULL);
3872 yaz_log_init (lev, argv[2], NULL);
3874 yaz_log_init (lev, argv[2], argv[3]);
3875 if (lev & LOG_DEBUG)
3876 odr_print_file = yaz_log_file();
3883 * ir_log_proc: log yaz message
3885 static int ir_log_proc (ClientData clientData, Tcl_Interp *interp,
3886 int argc, char **argv)
3891 Tcl_AppendResult (interp, wrongArgs, *argv,
3892 " level string\"", NULL);
3895 mask = yaz_log_mask_str_x (argv[1], 0);
3896 logf (LOG_DEBUG, "%s", argv[2]);
3902 * ir_version: log ir version
3904 static int ir_version (ClientData clientData, Tcl_Interp *interp,
3905 int argc, char **argv)
3907 Tcl_AppendElement (interp, IR_TCL_VERSION);
3908 Tcl_AppendElement (interp, YAZ_VERSION);
3913 /* ------------------------------------------------------- */
3914 static void ir_initResponse (void *obj, Z_InitResponse *initrs)
3918 p->initResult = *initrs->result ? 1 : 0;
3919 if (!*initrs->result)
3920 logf (LOG_DEBUG, "Connection rejected by target");
3922 logf (LOG_DEBUG, "Connection accepted by target");
3924 get_referenceId (&p->set_inher.referenceId, initrs->referenceId);
3926 xfree (p->targetImplementationId);
3927 ir_tcl_strdup (p->interp, &p->targetImplementationId,
3928 initrs->implementationId);
3929 xfree (p->targetImplementationName);
3930 ir_tcl_strdup (p->interp, &p->targetImplementationName,
3931 initrs->implementationName);
3932 xfree (p->targetImplementationVersion);
3933 ir_tcl_strdup (p->interp, &p->targetImplementationVersion,
3934 initrs->implementationVersion);
3936 p->maximumRecordSize = *initrs->maximumRecordSize;
3937 p->preferredMessageSize = *initrs->preferredMessageSize;
3939 memcpy (&p->options, initrs->options, sizeof(initrs->options));
3940 memcpy (&p->protocolVersion, initrs->protocolVersion,
3941 sizeof(initrs->protocolVersion));
3942 xfree (p->userInformationField);
3943 p->userInformationField = NULL;
3944 if (initrs->userInformationField)
3948 if (initrs->userInformationField->which == ODR_EXTERNAL_octet &&
3949 (p->userInformationField =
3950 ir_tcl_malloc ((len =
3951 initrs->userInformationField->
3952 u.octet_aligned->len) +1)))
3954 memcpy (p->userInformationField,
3955 initrs->userInformationField->u.octet_aligned->buf,
3957 (p->userInformationField)[len] = '\0';
3962 static void ir_deleteDiags (IrTcl_Diagnostic **dst_list, int *dst_num)
3965 for (i = 0; i<*dst_num; i++)
3966 xfree ((*dst_list)[i].addinfo);
3972 static void ir_handleDiags (IrTcl_Diagnostic **dst_list, int *dst_num,
3973 Z_DiagRec **list, int num)
3976 char *addinfo = NULL;
3979 *dst_list = ir_tcl_malloc (sizeof(**dst_list) * num);
3980 for (i = 0; i<num; i++)
3983 switch (list[i]->which)
3985 case Z_DiagRec_defaultFormat:
3986 (*dst_list)[i].condition = *list[i]->u.defaultFormat->condition;
3988 switch (list[i]->u.defaultFormat->which)
3990 case Z_DefaultDiagFormat_v2Addinfo:
3991 addinfo = list[i]->u.defaultFormat->u.v2Addinfo;
3993 case Z_DefaultDiagFormat_v3Addinfo:
3994 addinfo = list[i]->u.defaultFormat->u.v3Addinfo;
3998 addinfo = list[i]->u.defaultFormat->addinfo;
4001 ((*dst_list)[i].addinfo = ir_tcl_malloc (strlen(addinfo)+1)))
4002 strcpy ((*dst_list)[i].addinfo, addinfo);
4003 cp = diagbib1_str ((*dst_list)[i].condition);
4004 logf (LOG_DEBUG, "Diag %d %s %s", (*dst_list)[i].condition,
4005 cp ? cp : "", addinfo ? addinfo : "");
4008 (*dst_list)[i].addinfo = NULL;
4009 (*dst_list)[i].condition = 0;
4014 static void ir_handleDBRecord (IrTcl_Obj *p, IrTcl_RecordList *rl,
4017 struct oident *ident;
4018 Z_ext_typeent *etype;
4020 logf (LOG_DEBUG, "handleDBRecord size=%d", oe->u.octet_aligned->len);
4021 rl->u.dbrec.size = oe->u.octet_aligned->len;
4022 rl->u.dbrec.buf = NULL;
4024 if ((ident = oid_getentbyoid (oe->direct_reference)))
4025 rl->u.dbrec.type = ident->value;
4027 rl->u.dbrec.type = VAL_USMARC;
4029 if (ident && (oe->which == Z_External_single ||
4030 oe->which == Z_External_octet)
4031 && (etype = z_ext_getentbyref (ident->value)))
4035 odr_setbuf (p->odr_in, (char*) oe->u.octet_aligned->buf,
4036 oe->u.octet_aligned->len, 0);
4037 if (!(*etype->fun)(p->odr_in, (char **) &rr, 0, 0))
4039 rl->u.dbrec.type = VAL_NONE;
4042 switch (etype->what)
4044 case Z_External_sutrs:
4045 logf (LOG_DEBUG, "Z_External_sutrs");
4047 if ((rl->u.dbrec.buf = ir_tcl_malloc (oe->u.sutrs->len+1)))
4049 memcpy (rl->u.dbrec.buf, oe->u.sutrs->buf,
4051 rl->u.dbrec.buf[oe->u.sutrs->len] = '\0';
4053 rl->u.dbrec.size = oe->u.sutrs->len;
4055 case Z_External_grs1:
4056 logf (LOG_DEBUG, "Z_External_grs1");
4058 ir_tcl_grs_mk (oe->u.grs1, &rl->u.dbrec.u.grs1);
4060 case Z_External_explainRecord:
4061 logf (LOG_DEBUG, "Z_External_explainRecord");
4062 if ((rl->u.dbrec.buf = ir_tcl_malloc (rl->u.dbrec.size)))
4064 memcpy (rl->u.dbrec.buf, oe->u.octet_aligned->buf,
4072 if (oe->which == Z_External_octet && rl->u.dbrec.size > 0)
4074 char *buf = (char*) oe->u.octet_aligned->buf;
4075 if ((rl->u.dbrec.buf = ir_tcl_malloc (rl->u.dbrec.size)))
4076 memcpy (rl->u.dbrec.buf, buf, rl->u.dbrec.size);
4078 else if (rl->u.dbrec.type == VAL_SUTRS &&
4079 oe->which == Z_External_sutrs)
4081 if ((rl->u.dbrec.buf = ir_tcl_malloc (oe->u.sutrs->len+1)))
4083 memcpy (rl->u.dbrec.buf, oe->u.sutrs->buf,
4085 rl->u.dbrec.buf[oe->u.sutrs->len] = '\0';
4087 rl->u.dbrec.size = oe->u.sutrs->len;
4089 else if (rl->u.dbrec.type == VAL_GRS1 &&
4090 oe->which == Z_External_grs1)
4092 ir_tcl_grs_mk (oe->u.grs1, &rl->u.dbrec.u.grs1);
4097 static void ir_handleZRecords (void *o, Z_Records *zrs, IrTcl_SetObj *setobj,
4098 const char *elements)
4103 IrTcl_RecordList *rl;
4105 setobj->which = zrs->which;
4106 setobj->recordFlag = 1;
4108 ir_deleteDiags (&setobj->nonSurrogateDiagnosticList,
4109 &setobj->nonSurrogateDiagnosticNum);
4110 if (zrs->which == Z_Records_DBOSD)
4112 int num_rec = zrs->u.databaseOrSurDiagnostics->num_records;
4114 if (num_rec != setobj->numberOfRecordsReturned)
4116 logf (LOG_WARN, "numberOfRecordsReturned=%d but num records=%d",
4117 setobj->numberOfRecordsReturned, num_rec);
4118 setobj->numberOfRecordsReturned = num_rec;
4121 for (offset = 0; offset < num_rec; offset++)
4123 Z_NamePlusRecord *znpr = zrs->u.databaseOrSurDiagnostics->
4126 rl = new_IR_record (setobj, setobj->start + offset, znpr->which,
4128 if (rl->which == Z_NamePlusRecord_surrogateDiagnostic)
4129 ir_handleDiags (&rl->u.surrogateDiagnostics.list,
4130 &rl->u.surrogateDiagnostics.num,
4131 &znpr->u.surrogateDiagnostic,
4134 ir_handleDBRecord (p, rl,
4135 (Z_External*) (znpr->u.databaseRecord));
4138 else if (zrs->which == Z_Records_multipleNSD)
4140 logf (LOG_DEBUG, "multipleNonSurrogateDiagnostic %d",
4141 zrs->u.multipleNonSurDiagnostics->num_diagRecs);
4142 setobj->numberOfRecordsReturned = 0;
4143 ir_handleDiags (&setobj->nonSurrogateDiagnosticList,
4144 &setobj->nonSurrogateDiagnosticNum,
4145 zrs->u.multipleNonSurDiagnostics->diagRecs,
4146 zrs->u.multipleNonSurDiagnostics->num_diagRecs);
4151 Z_DiagRec dr, *dr_p = &dr;
4152 dr.which = Z_DiagRec_defaultFormat;
4153 dr.u.defaultFormat = zrs->u.nonSurrogateDiagnostic;
4155 Z_DiagRec *dr_p = zrs->u.nonSurrogateDiagnostic;
4157 logf (LOG_DEBUG, "NonSurrogateDiagnostic");
4159 setobj->numberOfRecordsReturned = 0;
4160 ir_handleDiags (&setobj->nonSurrogateDiagnosticList,
4161 &setobj->nonSurrogateDiagnosticNum,
4166 static char *set_queryExpression (Z_QueryExpression *qe)
4171 if (qe->which == Z_QueryExpression_term)
4173 if (qe->u.term->queryTerm)
4175 Z_Term *term = qe->u.term->queryTerm;
4176 if (term->which == Z_Term_general)
4178 termz = xmalloc (term->u.general->len+1);
4179 memcpy (termz, term->u.general->buf, term->u.general->len);
4180 termz[term->u.general->len] = 0;
4187 static void set_searchResult (Z_OtherInformation *o,
4188 IrTcl_SetObj *setobj)
4193 for (i = 0; i < o->num_elements; i++)
4195 if (o->list[i]->which == Z_OtherInfo_externallyDefinedInfo)
4197 ODR odr = odr_createmem (ODR_DECODE);
4198 Z_External *ext = o->list[i]->information.externallyDefinedInfo;
4199 Z_SearchInfoReport *sr = 0;
4201 if (ext->which == Z_External_single)
4203 odr_setbuf (odr, ext->u.single_ASN1_type->buf,
4204 ext->u.single_ASN1_type->len, 0);
4205 z_SearchInfoReport (odr, &sr, 0, "searchInfo");
4207 if (ext->which == Z_External_searchResult1)
4208 sr = ext->u.searchResult1;
4212 reset_searchResult (setobj);
4214 setobj->searchResult_num = sr->num;
4215 setobj->searchResult_terms =
4216 xmalloc (sr->num * sizeof(*setobj->searchResult_terms));
4217 setobj->searchResult_count =
4218 xmalloc (sr->num * sizeof(*setobj->searchResult_count));
4220 for (j = 0; j < sr->num; j++)
4222 setobj->searchResult_terms[j] =
4223 set_queryExpression (
4224 sr->elements[j]->subqueryExpression);
4225 if (sr->elements[j]->subqueryCount)
4226 setobj->searchResult_count[j] =
4227 *sr->elements[j]->subqueryCount;
4229 setobj->searchResult_count[j] = 0;
4237 static void ir_searchResponse (void *o, Z_SearchResponse *searchrs,
4238 IrTcl_SetObj *setobj)
4240 Z_Records *zrs = searchrs->records;
4242 logf (LOG_DEBUG, "Received search response");
4245 logf (LOG_DEBUG, "Search response, no object!");
4248 setobj->searchStatus = *searchrs->searchStatus;
4249 get_referenceId (&setobj->set_inher.referenceId, searchrs->referenceId);
4250 setobj->resultCount = *searchrs->resultCount;
4251 if (searchrs->presentStatus)
4252 setobj->presentStatus = *searchrs->presentStatus;
4254 setobj->presentStatus = Z_RES_NONE;
4255 if (searchrs->nextResultSetPosition)
4256 setobj->nextResultSetPosition = *searchrs->nextResultSetPosition;
4258 logf (LOG_DEBUG, "status %d hits %d",
4259 setobj->searchStatus, setobj->resultCount);
4260 set_searchResult (searchrs->additionalSearchInfo, setobj);
4264 if (setobj->resultCount <= setobj->set_inher.smallSetUpperBound)
4265 es = setobj->set_inher.smallSetElementSetNames;
4267 es = setobj->set_inher.mediumSetElementSetNames;
4268 setobj->numberOfRecordsReturned = *searchrs->numberOfRecordsReturned;
4269 ir_handleZRecords (o, zrs, setobj, es);
4273 setobj->numberOfRecordsReturned = 0;
4274 setobj->recordFlag = 0;
4279 static void ir_presentResponse (void *o, Z_PresentResponse *presrs,
4280 IrTcl_SetObj *setobj)
4282 Z_Records *zrs = presrs->records;
4284 logf (LOG_DEBUG, "Received present response");
4287 logf (LOG_DEBUG, "Present response, no object!");
4290 setobj->presentStatus = *presrs->presentStatus;
4291 get_referenceId (&setobj->set_inher.referenceId, presrs->referenceId);
4292 setobj->nextResultSetPosition = *presrs->nextResultSetPosition;
4295 setobj->numberOfRecordsReturned = *presrs->numberOfRecordsReturned;
4296 ir_handleZRecords (o, zrs, setobj, setobj->set_inher.elementSetNames);
4300 setobj->numberOfRecordsReturned = 0;
4301 setobj->recordFlag = 0;
4302 logf (LOG_DEBUG, "No records!");
4306 static void ir_scanResponse (void *o, Z_ScanResponse *scanrs,
4307 IrTcl_ScanObj *scanobj)
4311 logf (LOG_DEBUG, "Received scanResponse");
4313 get_referenceId (&p->set_inher.referenceId, scanrs->referenceId);
4314 scanobj->scanStatus = *scanrs->scanStatus;
4315 logf (LOG_DEBUG, "scanStatus=%d", scanobj->scanStatus);
4317 if (scanrs->stepSize)
4318 scanobj->stepSize = *scanrs->stepSize;
4319 logf (LOG_DEBUG, "stepSize=%d", scanobj->stepSize);
4321 scanobj->numberOfEntriesReturned = *scanrs->numberOfEntriesReturned;
4322 logf (LOG_DEBUG, "numberOfEntriesReturned=%d",
4323 scanobj->numberOfEntriesReturned);
4325 if (scanrs->positionOfTerm)
4326 scanobj->positionOfTerm = *scanrs->positionOfTerm;
4328 scanobj->positionOfTerm = -1;
4329 logf (LOG_DEBUG, "positionOfTerm=%d", scanobj->positionOfTerm);
4331 xfree (scanobj->entries);
4332 scanobj->entries = NULL;
4333 scanobj->num_entries = 0;
4334 scanobj->entries_flag = 0;
4336 ir_deleteDiags (&scanobj->nonSurrogateDiagnosticList,
4337 &scanobj->nonSurrogateDiagnosticNum);
4338 if (scanrs->entries)
4343 scanobj->entries_flag = 1;
4344 if (scanrs->entries)
4346 scanobj->num_entries = scanrs->entries->num_entries;
4347 scanobj->entries = ir_tcl_malloc (scanobj->num_entries *
4348 sizeof(*scanobj->entries));
4349 ze = scanrs->entries->entries;
4351 for (i=0; i<scanobj->num_entries; i++, ze++)
4353 scanobj->entries[i].which = (*ze)->which;
4354 switch ((*ze)->which)
4356 case Z_Entry_termInfo:
4357 if ((*ze)->u.termInfo->term->which == Z_Term_general)
4359 int l = (*ze)->u.termInfo->term->u.general->len;
4360 scanobj->entries[i].u.term.buf = ir_tcl_malloc (1+l);
4361 memcpy (scanobj->entries[i].u.term.buf,
4362 (*ze)->u.termInfo->term->u.general->buf,
4364 scanobj->entries[i].u.term.buf[l] = '\0';
4367 scanobj->entries[i].u.term.buf = NULL;
4368 if ((*ze)->u.termInfo->globalOccurrences)
4369 scanobj->entries[i].u.term.globalOccurrences =
4370 *(*ze)->u.termInfo->globalOccurrences;
4372 scanobj->entries[i].u.term.globalOccurrences = 0;
4374 case Z_Entry_surrogateDiagnostic:
4375 ir_handleDiags (&scanobj->entries[i].u.diag.list,
4376 &scanobj->entries[i].u.diag.num,
4377 &(*ze)->u.surrogateDiagnostic,
4382 if (scanrs->entries->nonsurrogateDiagnostics)
4383 ir_handleDiags (&scanobj->nonSurrogateDiagnosticList,
4384 &scanobj->nonSurrogateDiagnosticNum,
4385 scanrs->entries->nonsurrogateDiagnostics,
4386 scanrs->entries->num_nonsurrogateDiagnostics);
4391 static void ir_sortResponse (void *o, Z_SortResponse *sortrs,
4392 IrTcl_SetObj *setobj)
4396 logf (LOG_DEBUG, "Received sortResponse");
4401 purge_IR_records (setobj);
4403 get_referenceId (&p->set_inher.referenceId, sortrs->referenceId);
4405 setobj->sortStatus = *sortrs->sortStatus;
4407 ir_deleteDiags (&setobj->nonSurrogateDiagnosticList,
4408 &setobj->nonSurrogateDiagnosticNum);
4410 if (sortrs->diagnostics)
4411 ir_handleDiags (&setobj->nonSurrogateDiagnosticList,
4412 &setobj->nonSurrogateDiagnosticNum,
4413 sortrs->diagnostics,
4414 sortrs->num_diagnostics);
4416 if (sortrs->diagnostics)
4417 ir_handleDiags (&setobj->nonSurrogateDiagnosticList,
4418 &setobj->nonSurrogateDiagnosticNum,
4419 sortrs->diagnostics->diagRecs,
4420 sortrs->diagnostics->num_diagRecs);
4425 * ir_select_read: handle incoming packages
4427 static void ir_select_read (ClientData clientData)
4429 IrTcl_Obj *p = (IrTcl_Obj *) clientData;
4434 Tcl_CmdInfo cmd_info;
4435 const char *apdu_call;
4438 logf(LOG_DEBUG, "Read handler fd=%d", cs_fileno(p->cs_link));
4439 if (p->state == IR_TCL_R_Connecting)
4441 logf(LOG_DEBUG, "read: connect");
4442 r = cs_rcvconnect (p->cs_link);
4445 logf (LOG_WARN, "cs_rcvconnect returned 1");
4448 p->state = IR_TCL_R_Idle;
4450 ir_select_remove_write (cs_fileno (p->cs_link), p);
4453 logf (LOG_DEBUG, "cs_rcvconnect error");
4454 ir_tcl_disconnect (p);
4457 p->failInfo = IR_TCL_FAIL_CONNECT;
4458 ir_tcl_eval (p->interp, p->failback);
4460 ir_obj_delete ((ClientData) p);
4464 ir_tcl_eval (p->interp, p->callback);
4465 if (p->ref_count == 2 && p->cs_link && p->request_queue
4466 && p->state == IR_TCL_R_Idle)
4467 ir_tcl_send_q (p, p->request_queue, "x");
4468 ir_obj_delete ((ClientData) p);
4473 p->state = IR_TCL_R_Reading;
4476 yaz_log(LOG_DEBUG, "round %d", round);
4477 /* read incoming APDU */
4478 if ((r=cs_get (p->cs_link, &p->buf_in, &p->len_in)) == 1)
4480 logf(LOG_DEBUG, "PDU Fraction read");
4483 /* signal one more use of ir object - callbacks must not
4484 release the ir memory (p pointer) */
4488 logf (LOG_DEBUG, "cs_get failed, code %d", r);
4489 ir_tcl_disconnect (p);
4492 p->failInfo = IR_TCL_FAIL_READ;
4493 ir_tcl_eval (p->interp, p->failback);
4495 /* release ir object now if callback deleted it */
4496 ir_obj_delete ((ClientData) p);
4499 /* got complete APDU. Now decode */
4502 odr_setbuf (p->odr_in, p->buf_in, r, 0);
4503 logf (LOG_DEBUG, "cs_get ok, total size %d", r);
4504 if (!z_APDU (p->odr_in, &apdu, 0, 0))
4506 logf (LOG_DEBUG, "cs_get failed: %s",
4507 odr_errmsg (odr_geterror (p->odr_in)));
4508 ir_tcl_disconnect (p);
4511 p->failInfo = IR_TCL_FAIL_IN_APDU;
4512 p->apduOffset = odr_offset (p->odr_in);
4513 ir_tcl_eval (p->interp, p->failback);
4515 /* release ir object now if failback deleted it */
4516 ir_obj_delete ((ClientData) p);
4520 z_APDU(p->odr_pr, &apdu, 0, 0);
4521 /* handle APDU and invoke callback */
4522 rq = p->request_queue;
4525 /* no corresponding request. Skip it. */
4526 logf(LOG_DEBUG, "no corresponding request. Skipping it");
4527 p->state = IR_TCL_R_Idle;
4530 object_name = rq->object_name;
4531 logf (LOG_DEBUG, "Object %s", object_name);
4533 if (Tcl_GetCommandInfo (p->interp, object_name, &cmd_info))
4537 case Z_APDU_initResponse:
4538 p->eventType = "init";
4539 ir_initResponse (p, apdu->u.initResponse);
4540 apdu_call = p->initResponse;
4542 case Z_APDU_searchResponse:
4543 p->eventType = "search";
4544 ir_searchResponse (p, apdu->u.searchResponse,
4545 (IrTcl_SetObj *) cmd_info.clientData);
4546 apdu_call = ((IrTcl_SetObj *)
4547 cmd_info.clientData)->searchResponse;
4549 case Z_APDU_presentResponse:
4550 p->eventType = "present";
4551 ir_presentResponse (p, apdu->u.presentResponse,
4552 (IrTcl_SetObj *) cmd_info.clientData);
4553 apdu_call = ((IrTcl_SetObj *)
4554 cmd_info.clientData)->presentResponse;
4556 case Z_APDU_scanResponse:
4557 p->eventType = "scan";
4558 ir_scanResponse (p, apdu->u.scanResponse,
4559 (IrTcl_ScanObj *) cmd_info.clientData);
4560 apdu_call = ((IrTcl_ScanObj *)
4561 cmd_info.clientData)->scanResponse;
4563 case Z_APDU_sortResponse:
4564 p->eventType = "sort";
4565 ir_sortResponse (p, apdu->u.sortResponse,
4566 (IrTcl_SetObj *) cmd_info.clientData);
4567 apdu_call = ((IrTcl_SetObj *)
4568 cmd_info.clientData)->sortResponse;
4571 logf (LOG_WARN, "Received unknown APDU type (%d)",
4573 ir_tcl_disconnect (p);
4576 p->failInfo = IR_TCL_FAIL_UNKNOWN_APDU;
4577 ir_tcl_eval (p->interp, p->failback);
4582 p->request_queue = rq->next;
4583 p->state = IR_TCL_R_Idle;
4586 ir_tcl_eval (p->interp, apdu_call);
4587 else if (rq->callback)
4588 ir_tcl_eval (p->interp, rq->callback);
4589 xfree (rq->buf_out);
4590 xfree (rq->callback);
4591 xfree (rq->object_name);
4593 odr_reset (p->odr_in);
4594 if (p->ref_count == 1)
4596 ir_obj_delete ((ClientData) p);
4599 ir_obj_delete ((ClientData) p);
4600 } while (p->cs_link && cs_more (p->cs_link));
4601 if (p->cs_link && p->request_queue && p->state == IR_TCL_R_Idle)
4602 ir_tcl_send_q (p, p->request_queue, "x");
4606 * ir_select_write: handle outgoing packages - not yet written.
4608 static int ir_select_write (ClientData clientData)
4610 IrTcl_Obj *p = (IrTcl_Obj *) clientData;
4614 logf (LOG_DEBUG, "Write handler fd=%d", cs_fileno(p->cs_link));
4615 if (p->state == IR_TCL_R_Connecting)
4617 logf(LOG_DEBUG, "write: connect");
4618 r = cs_rcvconnect (p->cs_link);
4621 logf (LOG_DEBUG, "cs_rcvconnect returned 1");
4624 p->state = IR_TCL_R_Idle;
4626 ir_select_remove_write (cs_fileno (p->cs_link), p);
4629 logf (LOG_DEBUG, "cs_rcvconnect error");
4630 ir_tcl_disconnect (p);
4633 p->failInfo = IR_TCL_FAIL_CONNECT;
4634 ir_tcl_eval (p->interp, p->failback);
4636 ir_obj_delete ((ClientData) p);
4640 ir_tcl_eval (p->interp, p->callback);
4641 ir_obj_delete ((ClientData) p);
4644 rq = p->request_queue;
4645 if (!rq || !rq->buf_out)
4648 if ((r=cs_put (p->cs_link, rq->buf_out, rq->len_out)) < 0)
4650 logf (LOG_DEBUG, "cs_put write fail");
4652 xfree (rq->buf_out);
4654 ir_tcl_disconnect (p);
4657 p->failInfo = IR_TCL_FAIL_WRITE;
4658 ir_tcl_eval (p->interp, p->failback);
4660 ir_obj_delete ((ClientData) p);
4662 else if (r == 0) /* remove select bit */
4664 logf (LOG_DEBUG, "Write completed");
4665 p->state = IR_TCL_R_Waiting;
4666 ir_select_remove_write (cs_fileno (p->cs_link), p);
4667 xfree (rq->buf_out);
4673 static void ir_select_notify (ClientData clientData, int r, int w, int e)
4677 if (!ir_select_write (clientData) && r)
4678 ir_select_read (clientData);
4682 ir_select_read (clientData);
4686 /*----------------------------------------------------------- */
4690 * This wrapper function is used by Windows to invoke the
4691 * initialization code for the DLL. If we are compiling
4692 * with Visual C++, this routine will be renamed to DllMain.
4704 DllEntryPoint(hInst, reason, reserved)
4705 HINSTANCE hInst; /* Library instance handle. */
4706 DWORD reason; /* Reason this function is being called. */
4707 LPVOID reserved; /* Not used. */
4713 /* ------------------------------------------------------- */
4715 * Irtcl_init: Registration of TCL commands.
4717 EXPORT (int,Irtcl_Init) (Tcl_Interp *interp)
4719 Tcl_CreateCommand (interp, "ir", ir_obj_mk, (ClientData) NULL,
4720 (Tcl_CmdDeleteProc *) NULL);
4721 Tcl_CreateCommand (interp, "ir-set", ir_set_obj_mk,
4722 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
4723 Tcl_CreateCommand (interp, "ir-scan", ir_scan_obj_mk,
4724 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
4725 Tcl_CreateCommand (interp, "ir-log-init", ir_log_init_proc,
4726 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
4727 Tcl_CreateCommand (interp, "ir-log", ir_log_proc,
4728 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
4729 Tcl_CreateCommand (interp, "ir-version", ir_version, (ClientData) NULL,
4730 (Tcl_CmdDeleteProc *) NULL);