2 * IR toolkit for tcl/tk
3 * (c) Index Data 1995-2002
4 * See the file LICENSE for details.
7 * Revision 1.120 2002-03-20 14:48:54 adam
8 * implemented USR.1 SearchResult-1
10 * Revision 1.119 2001/12/03 00:31:06 adam
11 * Towards 1.4. Configure updates.
13 * Revision 1.118 2001/03/27 16:27:21 adam
14 * Fixed bug in do_responseStatus.
16 * Revision 1.117 2001/03/26 11:39:34 adam
17 * Fixed bug in ir_deleteDiags - crash when receiving multiple diags.
19 * Revision 1.116 2001/02/09 11:58:04 adam
20 * Updated for Tcl8.1 and higher where internal encoding is UTF-8.
22 * Revision 1.115 2000/09/13 12:18:49 adam
23 * Logging utility patch (YAZ version 1.7).
25 * Revision 1.114 1999/05/17 20:37:41 adam
26 * Fixed problem with ASN code.
28 * Revision 1.113 1999/04/20 10:01:46 adam
29 * Modified calls to ODR encoders/decoders (name argument).
31 * Revision 1.112 1999/03/22 06:51:34 adam
34 * Revision 1.111 1999/02/11 11:30:09 adam
37 * Revision 1.110 1998/10/20 15:15:31 adam
38 * Changed scan response handler.
40 * Revision 1.109 1998/10/13 21:23:26 adam
41 * Fixed searchStatus method.
43 * Revision 1.108 1998/10/12 11:48:08 adam
44 * Removed printf call.
46 * Revision 1.107 1998/06/10 13:00:46 adam
47 * Added ir-version command.
49 * Revision 1.106 1998/05/20 12:25:35 adam
50 * Fixed bug that occurred in rare cases when encoding of incoming
53 * Revision 1.105 1998/04/02 14:31:08 adam
54 * This version works with compiled ASN.1 code.
56 * Revision 1.104 1998/02/27 14:26:07 adam
57 * Changed client so that it still works if target sets numberOfRecords
58 * in response to an illegal value.
60 * Revision 1.103 1997/11/19 11:22:10 adam
61 * Object identifiers can be accessed in GRS-1 records.
63 * Revision 1.102 1997/09/17 12:22:40 adam
64 * Changed to use YAZ version 1.4. The new comstack utility, cs_straddr,
67 * Revision 1.101 1997/09/09 10:19:53 adam
68 * New MSV5.0 port with fewer warnings.
70 * Revision 1.100 1997/05/01 15:04:05 adam
71 * Added ir-log command.
73 * Revision 1.99 1997/04/30 07:24:47 adam
74 * Spell fix of an error message.
76 * Revision 1.98 1997/04/13 18:57:20 adam
77 * Better error reporting and aligned with Tcl/Tk style.
78 * Rework of notifier code with Tcl_File handles.
80 * Revision 1.97 1996/11/14 17:11:07 adam
81 * Added Explain documentaion.
83 * Revision 1.96 1996/10/08 13:02:50 adam
84 * When dealing with records, odr_choice_enable_bias function is used to
85 * prevent decoding of externals.
87 * Revision 1.95 1996/09/13 10:51:49 adam
88 * Bug fix: ir_tcl_select_set called Tcl_GetFile at disconnect.
90 * Revision 1.94 1996/08/21 13:32:53 adam
91 * Implemented saveFile method and extended loadFile method to work with it.
93 * Revision 1.93 1996/08/16 15:07:45 adam
94 * First work on Explain.
96 * Revision 1.92 1996/08/09 15:33:07 adam
97 * Modified the code to use tk4.1/tcl7.5 patch level 1. The time-driven
98 * polling is no longer activated on Windows since asynchrounous I/O works
101 * Revision 1.91 1996/07/03 13:31:11 adam
102 * The xmalloc/xfree functions from YAZ are used to manage memory.
104 * Revision 1.90 1996/06/27 14:21:00 adam
105 * Yet another Windows port.
107 * Revision 1.89 1996/06/11 15:27:15 adam
108 * Event type set to connect a little earlier in the do_connect function.
110 * Revision 1.88 1996/06/03 09:04:22 adam
111 * Changed a few logf calls.
113 * Revision 1.87 1996/05/29 06:37:51 adam
114 * Function ir_tcl_get_grs_r enhanced so that specific elements can be
117 * Revision 1.86 1996/03/20 13:54:04 adam
118 * The Tcl_File structure is only manipulated in the Tk-event interface
121 * Revision 1.85 1996/03/15 11:15:48 adam
122 * Modified to use new prototypes for p_query_rpn and p_query_scan.
124 * Revision 1.84 1996/03/07 12:42:49 adam
125 * Better logging when callback is invoked.
127 * Revision 1.83 1996/03/05 09:21:09 adam
128 * Bug fix: memory used by GRS records wasn't freed.
129 * Rewrote some of the error handling code - the connection is always
130 * closed before failback is called.
131 * If failback is defined the send APDU methods (init, search, ...) will
132 * return OK but invoke failback (as is the case if the write operation
134 * Bug fix: ref_count in assoc object could grow if fraction of PDU was
137 * Revision 1.82 1996/02/29 15:30:21 adam
138 * Export of IrTcl functionality to extensions.
140 * Revision 1.81 1996/02/26 18:38:32 adam
141 * Work on export of set methods.
143 * Revision 1.80 1996/02/23 17:31:39 adam
144 * More functions made available to the wais tcl extension.
146 * Revision 1.79 1996/02/23 13:41:38 adam
147 * Work on public access to simple ir class system.
149 * Revision 1.78 1996/02/21 10:16:08 adam
150 * Simplified select handling. Only one function ir_tcl_select_set has
151 * to be externally defined.
153 * Revision 1.77 1996/02/20 17:52:58 adam
154 * Uses the YAZ oid system to name record syntax object identifiers.
156 * Revision 1.76 1996/02/20 16:09:51 adam
157 * Bug fix: didn't set element set names stamp correctly on result
158 * set records when element set names were set to the empty string.
160 * Revision 1.75 1996/02/19 15:41:53 adam
161 * Better log messages.
162 * Minor improvement of connect method.
164 * Revision 1.74 1996/02/05 17:58:03 adam
165 * Ported ir-tcl to use the beta releases of tcl7.5/tk4.1.
167 * Revision 1.73 1996/01/29 11:35:19 adam
168 * Bug fix: cs_type member renamed to comstackType to avoid conflict with
169 * cs_type macro defined by YAZ.
171 * Revision 1.72 1996/01/19 17:45:34 quinn
172 * Added debugging output
174 * Revision 1.71 1996/01/19 16:22:38 adam
175 * New method: apduDump - returns information about last incoming APDU.
177 * Revision 1.70 1996/01/10 09:18:34 adam
178 * PDU specific callbacks implemented: initRespnse, searchResponse,
179 * presentResponse and scanResponse.
180 * Bug fix in the command line shell (tclmain.c) - discovered on OSF/1.
182 * Revision 1.69 1996/01/04 16:12:12 adam
183 * Setting PDUType renamed to eventType.
185 * Revision 1.68 1996/01/04 11:05:22 adam
186 * New setting: PDUType - returns type of last PDU returned from the target.
187 * Fixed a bug in configure/Makefile.
189 * Revision 1.67 1996/01/03 09:00:51 adam
190 * Updated to use new version of Yaz (names changed to avoid C++ conflict).
192 * Revision 1.66 1995/11/28 17:26:39 adam
193 * Removed Carriage return from ir-tcl.c!
194 * Removed misc. debug logs.
196 * Revision 1.65 1995/11/28 13:53:00 quinn
199 * Revision 1.64 1995/11/13 15:39:18 adam
200 * Bug fix: {small,medium}SetElementSetNames weren't set correctly.
201 * Bug fix: idAuthentication weren't set correctly.
203 * Revision 1.63 1995/11/13 09:55:39 adam
204 * Multiple records at a position in a result-set with differnt
207 * Revision 1.62 1995/10/18 17:20:33 adam
208 * Work on target setup in client.tcl.
210 * Revision 1.61 1995/10/18 16:42:42 adam
211 * New settings: smallSetElementSetNames and mediumSetElementSetNames.
213 * Revision 1.60 1995/10/18 15:43:31 adam
214 * In search: mediumSetElementSetNames and smallSetElementSetNames are
215 * set to elementSetNames.
217 * Revision 1.59 1995/10/17 12:18:58 adam
218 * Bug fix: when target connection closed, the connection was not
219 * properly reestablished.
221 * Revision 1.58 1995/10/16 17:00:55 adam
222 * New setting: elementSetNames.
223 * Various client improvements. Medium presentation format looks better.
225 * Revision 1.57 1995/09/21 13:11:51 adam
226 * Support of dynamic loading.
227 * Test script uses load command if necessary.
229 * Revision 1.56 1995/08/29 15:30:14 adam
230 * Work on GRS records.
232 * Revision 1.55 1995/08/28 09:43:25 adam
233 * Minor changes. configure only searches for yaz beta 3 and versions after
236 * Revision 1.54 1995/08/24 12:25:16 adam
237 * Modified to work with yaz 1.0b3.
239 * Revision 1.53 1995/08/04 12:49:26 adam
240 * Bug fix: reading uninitialized variable p.
242 * Revision 1.52 1995/08/04 11:32:38 adam
243 * More work on output queue. Memory related routines moved
246 * Revision 1.51 1995/08/03 13:22:54 adam
249 * Revision 1.50 1995/07/20 08:09:49 adam
250 * client.tcl: Targets removed from hotTargets list when targets
251 * are removed/modified.
252 * ir-tcl.c: More work on triggerResourceControl.
254 * Revision 1.49 1995/06/30 12:39:21 adam
255 * Bug fix: loadFile didn't set record type.
256 * The MARC routines are a little less strict in the interpretation.
257 * Script display.tcl replaces the old marc.tcl.
258 * New interactive script: shell.tcl.
260 * Revision 1.48 1995/06/27 19:03:50 adam
261 * Bug fix in do_present in ir-tcl.c: p->set_child member weren't set.
262 * nextResultSetPosition used instead of setOffset.
264 * Revision 1.47 1995/06/25 10:25:04 adam
265 * Working on triggerResourceControl. Description of compile/install
266 * procedure moved to ir-tcl.sgml.
268 * Revision 1.46 1995/06/22 13:15:06 adam
269 * Feature: SUTRS. Setting getSutrs implemented.
270 * Work on display formats.
271 * Preferred record syntax can be set by the user.
273 * Revision 1.45 1995/06/20 08:07:30 adam
274 * New setting: failInfo.
275 * Working on better cancel mechanism.
277 * Revision 1.44 1995/06/19 17:01:20 adam
280 * Revision 1.43 1995/06/19 13:06:08 adam
281 * New define: IR_TCL_VERSION.
283 * Revision 1.42 1995/06/19 08:08:52 adam
284 * client.tcl: hotTargets now contain both database and target name.
285 * ir-tcl.c: setting protocol edited. Errors in callbacks are logged
286 * by logf(LOG_WARN, ...) calls.
288 * Revision 1.41 1995/06/16 12:28:16 adam
289 * Implemented preferredRecordSyntax.
290 * Minor changes in diagnostic handling.
291 * Record list deleted when connection closes.
293 * Revision 1.40 1995/06/14 13:37:18 adam
294 * Setting recordType implemented.
295 * Setting implementationVersion implemented.
296 * Settings implementationId / implementationName edited.
298 * Revision 1.39 1995/06/08 10:26:32 adam
299 * Bug fix in ir_strdup.
301 * Revision 1.38 1995/06/01 16:36:47 adam
302 * About buttons. Minor bug fixes.
304 * Revision 1.37 1995/06/01 07:31:20 adam
305 * Rename of many typedefs -> IrTcl_...
307 * Revision 1.36 1995/05/31 13:09:59 adam
308 * Client searches/presents may be interrupted.
309 * New moving book-logo.
311 * Revision 1.35 1995/05/31 08:36:33 adam
312 * Bug fix in client.tcl: didn't save options on clientrc.tcl.
313 * New method: referenceId. More work on scan.
315 * Revision 1.34 1995/05/29 10:33:42 adam
316 * README and rename of startup script.
318 * Revision 1.33 1995/05/29 09:15:11 quinn
319 * Changed CS_SR to PROTO_SR, etc.
321 * Revision 1.32 1995/05/29 08:44:16 adam
322 * Work on delete of objects.
324 * Revision 1.31 1995/05/26 11:44:10 adam
325 * Bugs fixed. More work on MARC utilities and queries. Test
326 * client is up-to-date again.
328 * Revision 1.30 1995/05/26 08:54:11 adam
329 * New MARC utilities. Uses prefix query.
331 * Revision 1.29 1995/05/24 14:10:22 adam
332 * Work on idAuthentication, protocolVersion and options.
334 * Revision 1.28 1995/05/23 15:34:48 adam
335 * Many new settings, userInformationField, smallSetUpperBound, etc.
336 * A number of settings are inherited when ir-set is executed.
337 * This version is incompatible with the graphical test client (client.tcl).
339 * Revision 1.27 1995/05/11 15:34:47 adam
340 * Scan request changed a bit. This version works with RLG.
342 * Revision 1.26 1995/04/18 16:11:51 adam
343 * First version of graphical Scan. Some work on query-by-form.
345 * Revision 1.25 1995/04/17 09:37:17 adam
346 * Further development of scan.
348 * Revision 1.24 1995/04/11 14:16:42 adam
349 * Further work on scan. Response works. Entries aren't saved yet.
351 * Revision 1.23 1995/04/10 10:50:27 adam
352 * Result-set name defaults to suffix of ir-set name.
353 * Started working on scan. Not finished at this point.
355 * Revision 1.22 1995/03/31 10:43:03 adam
356 * More robust when getting bad MARC records.
358 * Revision 1.21 1995/03/31 08:56:37 adam
359 * New button "Search".
361 * Revision 1.20 1995/03/29 16:07:09 adam
362 * Bug fix: Didn't use setName in present request.
364 * Revision 1.19 1995/03/28 12:45:23 adam
365 * New ir method failback: called on disconnect/protocol error.
366 * New ir set/get method: protocol: SR / Z3950.
367 * Simple popup and disconnect when failback is invoked.
369 * Revision 1.18 1995/03/21 15:50:12 adam
372 * Revision 1.17 1995/03/21 13:41:03 adam
373 * Comstack cs_create not used too often. Non-blocking connect.
375 * Revision 1.16 1995/03/21 08:26:06 adam
376 * New method, setName, to specify the result set name (other than Default).
377 * New method, responseStatus, which returns diagnostic info, if any, after
378 * present response / search response.
380 * Revision 1.15 1995/03/20 15:24:07 adam
381 * Diagnostic records saved on searchResponse.
383 * Revision 1.14 1995/03/20 08:53:22 adam
384 * Event loop in tclmain.c rewritten. New method searchStatus.
386 * Revision 1.13 1995/03/17 18:26:17 adam
387 * Non-blocking i/o used now. Database names popup as cascade items.
389 * Revision 1.12 1995/03/17 15:45:00 adam
390 * Improved target/database setup.
392 * Revision 1.11 1995/03/16 17:54:03 adam
393 * Minor changes really.
395 * Revision 1.10 1995/03/15 16:14:50 adam
396 * Blocking arg in cs_create changed.
398 * Revision 1.9 1995/03/15 13:59:24 adam
401 * Revision 1.8 1995/03/15 08:25:16 adam
402 * New method presentStatus to check for error on present. Misc. cleanup
403 * of IrTcl_RecordList manipulations. Full MARC record presentation in
406 * Revision 1.7 1995/03/14 17:32:29 adam
407 * Presentation of full Marc record in popup window.
409 * Revision 1.6 1995/03/12 19:31:55 adam
410 * Pattern matching implemented when retrieving MARC records. More
411 * diagnostic functions.
413 * Revision 1.5 1995/03/10 18:00:15 adam
414 * Actual presentation in line-by-line format. RPN query support.
416 * Revision 1.4 1995/03/09 16:15:08 adam
417 * First presentRequest attempts. Hot-target list.
435 #if defined(__WIN32__)
436 # define WIN32_LEAN_AND_MEAN
437 # include <windows.h>
438 # undef WIN32_LEAN_AND_MEAN
441 * VC++ has an alternate entry point called DllMain, so we need to rename
445 # if defined(_MSC_VER)
446 # define EXPORT(a,b) __declspec(dllexport) a b
447 # define DllEntryPoint DllMain
449 # if defined(__BORLANDC__)
450 # define EXPORT(a,b) a _export b
452 # define EXPORT(a,b) a b
456 # define EXPORT(a,b) a b
459 static char *wrongArgs = "wrong # args: should be \"";
461 static int ir_tcl_error_exec (Tcl_Interp *interp, int argc, char **argv)
464 Tcl_AppendResult (interp, " while executing ", NULL);
465 for (i = 0; i<argc; i++)
466 Tcl_AppendResult (interp, (i ? " " : "\""), argv[i], NULL);
467 Tcl_AppendResult (interp, "\"", NULL);
472 static void ir_deleteDiags (IrTcl_Diagnostic **dst_list, int *dst_num);
474 static void ir_select_notify (ClientData clientData, int r, int w, int e);
476 void ir_select_add (int fd, void *obj)
478 ir_tcl_select_set (ir_select_notify, fd, obj, 1, 0, 0);
481 void ir_select_add_write (int fd, void *obj)
483 ir_tcl_select_set (ir_select_notify, fd, obj, 1, 1, 0);
486 void ir_select_remove (int fd, void *obj)
488 ir_tcl_select_set (NULL, fd, obj, 0, 0, 0);
491 void ir_select_remove_write (int fd, void *obj)
493 ir_tcl_select_set (ir_select_notify, fd, obj, 1, 0, 0);
496 static void delete_IR_record (IrTcl_RecordList *rl)
500 case Z_NamePlusRecord_databaseRecord:
501 switch (rl->u.dbrec.type)
504 ir_tcl_grs_del (&rl->u.dbrec.u.grs1);
509 xfree (rl->u.dbrec.buf);
510 rl->u.dbrec.buf = NULL;
512 case Z_NamePlusRecord_surrogateDiagnostic:
513 ir_deleteDiags (&rl->u.surrogateDiagnostics.list,
514 &rl->u.surrogateDiagnostics.num);
517 xfree (rl->elements);
520 static void purge_IR_records (IrTcl_SetObj *setobj)
522 IrTcl_RecordList *rl;
523 while ((rl = setobj->record_list))
525 setobj->record_list = rl->next;
526 delete_IR_record (rl);
531 static IrTcl_RecordList *new_IR_record (IrTcl_SetObj *setobj,
533 const char *elements)
535 IrTcl_RecordList *rl;
537 if (elements && !*elements)
539 for (rl = setobj->record_list; rl; rl = rl->next)
541 if (no == rl->no && (!rl->elements || !elements ||
542 !strcmp(elements, rl->elements)))
544 delete_IR_record (rl);
550 rl = ir_tcl_malloc (sizeof(*rl));
551 rl->next = setobj->record_list;
553 setobj->record_list = rl;
556 ir_tcl_strdup (NULL, &rl->elements, elements);
563 int ir_tcl_eval (Tcl_Interp *interp, const char *command)
565 char *tmp = ir_tcl_malloc (strlen(command)+1);
568 logf (LOG_DEBUG, "Invoking %.23s ...", command);
569 strcpy (tmp, command);
570 r = Tcl_Eval (interp, tmp);
573 const char *errorInfo = Tcl_GetVar (interp, "errorInfo", 0);
574 logf (LOG_WARN, "Tcl error in line %d: %s\n%s", interp->errorLine,
575 interp->result, errorInfo ? errorInfo : "<null>");
577 Tcl_FreeResult (interp);
583 * IrTcl_getRecordSyntaxStr: Return record syntax name of object id
585 static char *IrTcl_getRecordSyntaxStr (enum oid_value value)
588 struct oident ent, *entp;
590 ent.proto = PROTO_Z3950;
591 ent.oclass = CLASS_RECSYN;
594 o = oid_getoidbyent (&ent);
595 entp = oid_getentbyoid (o);
603 * IrTcl_getRecordSyntaxVal: Return record syntax value of string
605 static enum oid_value IrTcl_getRecordSyntaxVal (const char *name)
607 return oid_getvalbyname (name);
610 static IrTcl_RecordList *find_IR_record (IrTcl_SetObj *setobj, int no)
612 IrTcl_RecordList *rl;
614 for (rl = setobj->record_list; rl; rl = rl->next)
616 (!setobj->recordElements || !rl->elements ||
617 !strcmp (setobj->recordElements, rl->elements)))
622 static void delete_IR_records (IrTcl_SetObj *setobj)
624 IrTcl_RecordList *rl, *rl1;
626 for (rl = setobj->record_list; rl; rl = rl1)
628 delete_IR_record (rl);
632 setobj->record_list = NULL;
636 * ir_tcl_get_set_int: Set/get integer value
638 int ir_tcl_get_set_int (int *val, Tcl_Interp *interp, int argc, char **argv)
644 if (Tcl_GetInt (interp, argv[2], val)==TCL_ERROR)
647 sprintf (buf, "%d", *val);
648 Tcl_AppendResult (interp, buf, NULL);
654 * ir_tcl_method_error
656 int ir_tcl_method_error (Tcl_Interp *interp, int argc, char **argv,
659 IrTcl_Methods *tab_i = tab;
662 Tcl_AppendResult (interp, "bad method: \"", *argv, " ", argv[1],
663 "\"\nmethod should be of:", NULL);
664 for (tab_i = tab; tab_i->tab; tab_i++)
665 for (t = tab_i->tab; t->name; t++)
666 Tcl_AppendResult (interp, " ", t->name, NULL);
671 * ir_tcl_method: Search for method in table and invoke method handler
673 int ir_tcl_method (Tcl_Interp *interp, int argc, char **argv,
674 IrTcl_Methods *tab, int *ret)
676 IrTcl_Methods *tab_i = tab;
679 for (tab_i = tab; tab_i->tab; tab_i++)
680 for (t = tab_i->tab; t->name; t++)
683 if ((*t->method)(tab_i->obj, interp, argc, argv) == TCL_ERROR)
687 if (!strcmp (t->name, argv[1]))
689 *ret = (*t->method)(tab_i->obj, interp, argc, argv);
700 * ir_tcl_named_bits: get/set named bits
702 int ir_tcl_named_bits (struct ir_named_entry *tab, Odr_bitmask *ob,
703 Tcl_Interp *interp, int argc, char **argv)
705 struct ir_named_entry *ti;
710 for (no = 0; no < argc; no++)
713 for (ti = tab; ti->name; ti++)
714 if (!strcmp(argv[no], "@all") || !strcmp (argv[no], ti->name))
716 ODR_MASK_SET (ob, ti->pos);
721 Tcl_AppendResult (interp, "bad bit mask ", argv[no], NULL);
722 return ir_tcl_error_exec (interp, argc, argv);
727 for (ti = tab; ti->name; ti++)
728 if (ODR_MASK_GET (ob, ti->pos))
729 Tcl_AppendElement (interp, ti->name);
733 static void set_referenceId (ODR o, Z_ReferenceId **dst, const char *src)
739 *dst = odr_malloc (o, sizeof(**dst));
740 (*dst)->size = (*dst)->len = strlen(src);
741 (*dst)->buf = odr_malloc (o, (*dst)->len);
742 memcpy ((*dst)->buf, src, (*dst)->len);
746 static void get_referenceId (char **dst, Z_ReferenceId *src)
754 *dst = ir_tcl_malloc (src->len+1);
755 memcpy (*dst, src->buf, src->len);
756 (*dst)[src->len] = '\0';
759 /* ------------------------------------------------------- */
762 * do_init_request: init method on IR object
764 static int do_init_request (void *obj, Tcl_Interp *interp,
765 int argc, char **argv)
773 logf (LOG_DEBUG, "init %s", *argv);
776 Tcl_AppendResult (interp, "not connected", NULL);
777 return ir_tcl_error_exec (interp, argc, argv);
779 apdu = zget_APDU (p->odr_out, Z_APDU_initRequest);
780 req = apdu->u.initRequest;
782 set_referenceId (p->odr_out, &req->referenceId, p->set_inher.referenceId);
783 req->options = &p->options;
784 req->protocolVersion = &p->protocolVersion;
785 req->preferredMessageSize = &p->preferredMessageSize;
786 req->maximumRecordSize = &p->maximumRecordSize;
788 if (p->idAuthenticationGroupId)
790 Z_IdPass *pass = odr_malloc (p->odr_out, sizeof(*pass));
791 Z_IdAuthentication *auth = odr_malloc (p->odr_out, sizeof(*auth));
793 logf (LOG_DEBUG, "using pass authentication");
795 auth->which = Z_IdAuthentication_idPass;
796 auth->u.idPass = pass;
797 if (p->idAuthenticationGroupId && *p->idAuthenticationGroupId)
798 pass->groupId = p->idAuthenticationGroupId;
800 pass->groupId = NULL;
801 if (p->idAuthenticationUserId && *p->idAuthenticationUserId)
802 pass->userId = p->idAuthenticationUserId;
805 if (p->idAuthenticationPassword && *p->idAuthenticationPassword)
806 pass->password = p->idAuthenticationPassword;
808 pass->password = NULL;
809 req->idAuthentication = auth;
811 else if (!p->idAuthenticationOpen || !*p->idAuthenticationOpen)
812 req->idAuthentication = NULL;
815 Z_IdAuthentication *auth = odr_malloc (p->odr_out, sizeof(*auth));
817 logf (LOG_DEBUG, "using open authentication");
818 auth->which = Z_IdAuthentication_open;
819 auth->u.open = p->idAuthenticationOpen;
820 req->idAuthentication = auth;
822 req->implementationId = p->implementationId;
823 req->implementationName = p->implementationName;
824 req->implementationVersion = p->implementationVersion;
825 req->userInformationField = 0;
827 return ir_tcl_send_APDU (interp, p, apdu, "init", *argv);
831 * do_protocolVersion: Set protocol Version
833 static int do_protocolVersion (void *obj, Tcl_Interp *interp,
834 int argc, char **argv)
842 ODR_MASK_ZERO (&p->protocolVersion);
843 ODR_MASK_SET (&p->protocolVersion, 0);
844 ODR_MASK_SET (&p->protocolVersion, 1);
849 if (Tcl_GetInt (interp, argv[2], &version)==TCL_ERROR)
851 ODR_MASK_ZERO (&p->protocolVersion);
852 for (i = 0; i<version; i++)
853 ODR_MASK_SET (&p->protocolVersion, i);
855 for (i = 4; --i >= 0; )
856 if (ODR_MASK_GET (&p->protocolVersion, i))
858 sprintf (buf, "%d", i+1);
859 interp->result = buf;
864 * do_options: Set options
866 static int do_options (void *obj, Tcl_Interp *interp,
867 int argc, char **argv)
869 static struct ir_named_entry options_tab[] = {
873 { "resourceReport", 3 },
874 { "triggerResourceCtrl", 4},
875 { "resourceCtrl", 5},
879 { "extendedServices", 10},
880 { "level-1Segmentation", 11},
881 { "level-2Segmentation", 12},
882 { "concurrentOperations", 13},
883 { "namedResultSets", 14},
890 ODR_MASK_ZERO (&p->options);
891 ODR_MASK_SET (&p->options, 0);
892 ODR_MASK_SET (&p->options, 1);
893 ODR_MASK_SET (&p->options, 4);
894 ODR_MASK_SET (&p->options, 7);
895 ODR_MASK_SET (&p->options, 14);
898 return ir_tcl_named_bits (options_tab, &p->options, interp, argc-2, argv+2);
902 * do_apduInfo: Get APDU information
904 static int do_apduInfo (void *obj, Tcl_Interp *interp, int argc, char **argv)
912 sprintf (buf, "%d", p->apduLen);
913 Tcl_AppendElement (interp, buf);
914 sprintf (buf, "%d", p->apduOffset);
915 Tcl_AppendElement (interp, buf);
918 Tcl_AppendElement (interp, "");
921 apduf = fopen ("apdu.tmp", "w");
924 Tcl_AppendElement (interp, "");
927 odr_dumpBER (apduf, p->buf_in, p->apduLen);
929 if (!(apduf = fopen ("apdu.tmp", "r")))
930 Tcl_AppendElement (interp, "");
935 Tcl_AppendResult (interp, " {", NULL);
936 while ((c = getc (apduf)) != EOF)
940 Tcl_AppendResult (interp, buf, NULL);
943 Tcl_AppendResult (interp, "}", NULL);
950 * do_failInfo: Get fail information
952 static int do_failInfo (void *obj, Tcl_Interp *interp, int argc, char **argv)
962 sprintf (buf, "%d", p->failInfo);
968 case IR_TCL_FAIL_CONNECT:
969 cp = "connect failed";
971 case IR_TCL_FAIL_READ:
972 cp = "connection closed";
974 case IR_TCL_FAIL_WRITE:
975 cp = "connection closed";
977 case IR_TCL_FAIL_IN_APDU:
978 cp = "failed to decode incoming APDU";
980 case IR_TCL_FAIL_UNKNOWN_APDU:
986 Tcl_AppendElement (interp, buf);
987 Tcl_AppendElement (interp, cp);
992 * do_preferredMessageSize: Set/get preferred message size
994 static int do_preferredMessageSize (void *obj, Tcl_Interp *interp,
995 int argc, char **argv)
1001 p->preferredMessageSize = 30000;
1004 return ir_tcl_get_set_int (&p->preferredMessageSize, interp, argc, argv);
1008 * do_maximumRecordSize: Set/get maximum record size
1010 static int do_maximumRecordSize (void *obj, Tcl_Interp *interp,
1011 int argc, char **argv)
1017 p->maximumRecordSize = 30000;
1020 return ir_tcl_get_set_int (&p->maximumRecordSize, interp, argc, argv);
1024 * do_initResult: Get init result
1026 static int do_initResult (void *obj, Tcl_Interp *interp,
1027 int argc, char **argv)
1033 return ir_tcl_get_set_int (&p->initResult, interp, argc, argv);
1038 * do_implementationName: Set/get Implementation Name.
1040 static int do_implementationName (void *obj, Tcl_Interp *interp,
1041 int argc, char **argv)
1046 return ir_tcl_strdup (interp, &p->implementationName,
1048 else if (argc == -1)
1049 return ir_tcl_strdel (interp, &p->implementationName);
1052 xfree (p->implementationName);
1053 if (ir_tcl_strdup (interp, &p->implementationName, argv[2])
1057 Tcl_AppendResult (interp, p->implementationName, (char*) NULL);
1062 * do_implementationId: Get Implementation Id.
1064 static int do_implementationId (void *obj, Tcl_Interp *interp,
1065 int argc, char **argv)
1070 return ir_tcl_strdup (interp, &p->implementationId, "81");
1071 else if (argc == -1)
1072 return ir_tcl_strdel (interp, &p->implementationId);
1073 Tcl_AppendResult (interp, p->implementationId, (char*) NULL);
1078 * do_implementationVersion: get Implementation Version.
1080 static int do_implementationVersion (void *obj, Tcl_Interp *interp,
1081 int argc, char **argv)
1086 return ir_tcl_strdup (interp, &p->implementationVersion,
1087 #ifdef IR_TCL_VERSION
1092 else if (argc == -1)
1093 return ir_tcl_strdel (interp, &p->implementationVersion);
1094 Tcl_AppendResult (interp, p->implementationVersion, (char*) NULL);
1099 * do_targetImplementationName: Get Implementation Name of target.
1101 static int do_targetImplementationName (void *obj, Tcl_Interp *interp,
1102 int argc, char **argv)
1108 p->targetImplementationName = NULL;
1111 else if (argc == -1)
1112 return ir_tcl_strdel (interp, &p->targetImplementationName);
1113 Tcl_AppendResult (interp, p->targetImplementationName, (char*) NULL);
1118 * do_targetImplementationId: Get Implementation Id of target
1120 static int do_targetImplementationId (void *obj, Tcl_Interp *interp,
1121 int argc, char **argv)
1127 p->targetImplementationId = NULL;
1130 else if (argc == -1)
1131 return ir_tcl_strdel (interp, &p->targetImplementationId);
1132 Tcl_AppendResult (interp, p->targetImplementationId, (char*) NULL);
1137 * do_targetImplementationVersion: Get Implementation Version of target
1139 static int do_targetImplementationVersion (void *obj, Tcl_Interp *interp,
1140 int argc, char **argv)
1146 p->targetImplementationVersion = NULL;
1149 else if (argc == -1)
1150 return ir_tcl_strdel (interp, &p->targetImplementationVersion);
1151 Tcl_AppendResult (interp, p->targetImplementationVersion, (char*) NULL);
1156 * do_idAuthentication: Set/get id Authentication
1158 static int do_idAuthentication (void *obj, Tcl_Interp *interp,
1159 int argc, char **argv)
1163 if (argc >= 3 || argc == -1)
1165 xfree (p->idAuthenticationOpen);
1166 xfree (p->idAuthenticationGroupId);
1167 xfree (p->idAuthenticationUserId);
1168 xfree (p->idAuthenticationPassword);
1170 if (argc >= 3 || argc <= 0)
1172 p->idAuthenticationOpen = NULL;
1173 p->idAuthenticationGroupId = NULL;
1174 p->idAuthenticationUserId = NULL;
1175 p->idAuthenticationPassword = NULL;
1184 ir_tcl_strdup (interp, &p->idAuthenticationOpen, argv[2])
1191 ir_tcl_strdup (interp, &p->idAuthenticationGroupId, argv[2])
1195 ir_tcl_strdup (interp, &p->idAuthenticationUserId, argv[3])
1199 ir_tcl_strdup (interp, &p->idAuthenticationPassword, argv[4])
1204 if (p->idAuthenticationOpen)
1205 Tcl_AppendElement (interp, p->idAuthenticationOpen);
1206 else if (p->idAuthenticationGroupId)
1208 Tcl_AppendElement (interp, p->idAuthenticationGroupId);
1209 Tcl_AppendElement (interp, p->idAuthenticationUserId);
1210 Tcl_AppendElement (interp, p->idAuthenticationPassword);
1216 * do_connect: connect method on IR object
1218 static int do_connect (void *obj, Tcl_Interp *interp,
1219 int argc, char **argv)
1229 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
1230 " ?hostname?\"", NULL);
1235 Tcl_AppendResult (interp, p->hostname, NULL);
1239 logf (LOG_DEBUG, "connect %s %s", *argv, argv[2]);
1242 Tcl_AppendResult (interp, "already connected", NULL);
1243 return ir_tcl_error_exec (interp, argc, argv);
1245 if (!strcmp (p->comstackType, "tcpip"))
1247 p->cs_link = cs_create (tcpip_type, CS_BLOCK, p->protocol_type);
1248 logf (LOG_DEBUG, "tcp/ip connect %s", argv[2]);
1250 else if (!strcmp (p->comstackType, "mosi"))
1253 p->cs_link = cs_create (mosi_type, CS_BLOCK, p->protocol_type);
1254 logf (LOG_DEBUG, "mosi connect %s", argv[2]);
1256 Tcl_AppendResult (interp, "mosi not supported", NULL);
1257 return ir_tcl_error_exec (interp, argc, argv);
1262 Tcl_AppendResult (interp, "bad comstack type ",
1263 p->comstackType, NULL);
1264 return ir_tcl_error_exec (interp, argc, argv);
1266 if (ir_tcl_strdup (interp, &p->hostname, argv[2]) == TCL_ERROR)
1268 p->eventType = "connect";
1269 addr = cs_straddr (p->cs_link, argv[2]);
1272 ir_tcl_disconnect (p);
1273 Tcl_AppendResult (interp, "cs_straddr fail", NULL);
1274 return ir_tcl_error_exec (interp, argc, argv);
1276 if ((r=cs_connect (p->cs_link, addr)) < 0)
1278 ir_tcl_disconnect (p);
1279 Tcl_AppendResult (interp, "connect fail", NULL);
1280 return ir_tcl_error_exec (interp, argc, argv);
1282 ir_select_add (cs_fileno (p->cs_link), p);
1285 logf (LOG_DEBUG, "connect pending fd=%d", cs_fileno(p->cs_link));
1286 ir_select_add_write (cs_fileno (p->cs_link), p);
1287 p->state = IR_TCL_R_Connecting;
1291 logf (LOG_DEBUG, "connect ok fd=%d", cs_fileno(p->cs_link));
1292 p->state = IR_TCL_R_Idle;
1294 ir_tcl_eval (p->interp, p->callback);
1301 * ir_tcl_disconnect: close connection
1303 void ir_tcl_disconnect (IrTcl_Obj *p)
1307 logf(LOG_DEBUG, "Closing connection to %s", p->hostname);
1308 xfree (p->hostname);
1310 assert (p->cs_link);
1311 ir_select_remove (cs_fileno (p->cs_link), p);
1313 odr_reset (p->odr_in);
1315 #if TCL_MAJOR_VERSION == 8
1316 cs_fileno(p->cs_link) = -1;
1318 cs_close (p->cs_link);
1321 ODR_MASK_ZERO (&p->options);
1322 ODR_MASK_SET (&p->options, 0);
1323 ODR_MASK_SET (&p->options, 1);
1324 ODR_MASK_SET (&p->options, 4);
1325 ODR_MASK_SET (&p->options, 7);
1326 ODR_MASK_SET (&p->options, 14);
1328 ODR_MASK_ZERO (&p->protocolVersion);
1329 ODR_MASK_SET (&p->protocolVersion, 0);
1330 ODR_MASK_SET (&p->protocolVersion, 1);
1333 assert (!p->cs_link);
1337 * do_disconnect: disconnect method on IR object
1339 static int do_disconnect (void *obj, Tcl_Interp *interp,
1340 int argc, char **argv)
1346 p->state = IR_TCL_R_Idle;
1347 p->eventType = NULL;
1352 ir_tcl_disconnect (p);
1357 * do_comstack: Set/get comstack method on IR object
1359 static int do_comstack (void *o, Tcl_Interp *interp,
1360 int argc, char **argv)
1365 return ir_tcl_strdup (interp, &obj->comstackType, "tcpip");
1366 else if (argc == -1)
1367 return ir_tcl_strdel (interp, &obj->comstackType);
1370 xfree (obj->comstackType);
1371 if (ir_tcl_strdup (interp, &obj->comstackType, argv[2]) == TCL_ERROR)
1374 Tcl_AppendElement (interp, obj->comstackType);
1379 * do_logLevel: Set log level
1381 static int do_logLevel (void *o, Tcl_Interp *interp,
1382 int argc, char **argv)
1387 yaz_log_init (yaz_log_mask_str (argv[2]), "", NULL);
1389 yaz_log_init (yaz_log_mask_str (argv[2]), argv[3], NULL);
1391 yaz_log_init (yaz_log_mask_str (argv[2]), argv[3], argv[4]);
1397 * do_eventType: Return type of last event
1399 static int do_eventType (void *obj, Tcl_Interp *interp,
1400 int argc, char **argv)
1406 p->eventType = NULL;
1409 Tcl_AppendElement (interp, p->eventType ? p->eventType : "");
1415 * do_callback: add callback
1417 static int do_callback (void *obj, Tcl_Interp *interp,
1418 int argc, char **argv)
1427 else if (argc == -1)
1428 return ir_tcl_strdel (interp, &p->callback);
1431 xfree (p->callback);
1434 if (ir_tcl_strdup (interp, &p->callback, argv[2]) == TCL_ERROR)
1444 * do_failback: add error handle callback
1446 static int do_failback (void *obj, Tcl_Interp *interp,
1447 int argc, char **argv)
1456 else if (argc == -1)
1457 return ir_tcl_strdel (interp, &p->failback);
1460 xfree (p->failback);
1463 if (ir_tcl_strdup (interp, &p->failback, argv[2]) == TCL_ERROR)
1473 * do_initResponse: add init response handler
1475 static int do_initResponse (void *obj, Tcl_Interp *interp,
1476 int argc, char **argv)
1482 p->initResponse = NULL;
1485 else if (argc == -1)
1486 return ir_tcl_strdel (interp, &p->initResponse);
1489 xfree (p->initResponse);
1492 if (ir_tcl_strdup (interp, &p->initResponse, argv[2]) == TCL_ERROR)
1496 p->initResponse = NULL;
1501 * do_protocol: Set/get protocol method on IR object
1503 static int do_protocol (void *o, Tcl_Interp *interp, int argc, char **argv)
1509 p->protocol_type = PROTO_Z3950;
1514 if (!strcmp (argv[2], "Z39"))
1515 p->protocol_type = PROTO_Z3950;
1516 else if (!strcmp (argv[2], "SR"))
1517 p->protocol_type = PROTO_SR;
1520 Tcl_AppendResult (interp, "bad protocol ", argv[2], NULL);
1521 return ir_tcl_error_exec (interp, argc, argv);
1525 switch (p->protocol_type)
1528 Tcl_AppendElement (interp, "Z39");
1531 Tcl_AppendElement (interp, "SR");
1538 * do_triggerResourceControl:
1540 static int do_triggerResourceControl (void *obj, Tcl_Interp *interp,
1541 int argc, char **argv)
1545 Z_TriggerResourceControlRequest *req;
1546 bool_t is_false = 0;
1552 Tcl_AppendResult (interp, "not connected", NULL);
1553 return ir_tcl_error_exec (interp, argc, argv);
1555 apdu = zget_APDU (p->odr_out, Z_APDU_triggerResourceControlRequest);
1556 req = apdu->u.triggerResourceControlRequest;
1557 *req->requestedAction = Z_TriggerResourceCtrl_cancel;
1558 req->resultSetWanted = &is_false;
1560 return ir_tcl_send_APDU (interp, p, apdu, "triggerResourceControl",
1565 * do_databaseNames: specify database names
1567 static int do_databaseNames (void *obj, Tcl_Interp *interp,
1568 int argc, char **argv)
1571 IrTcl_SetCObj *p = obj;
1575 for (i=0; i<p->num_databaseNames; i++)
1576 xfree (p->databaseNames[i]);
1577 xfree (p->databaseNames);
1581 p->num_databaseNames = 0;
1582 p->databaseNames = NULL;
1587 for (i=0; i<p->num_databaseNames; i++)
1588 Tcl_AppendElement (interp, p->databaseNames[i]);
1591 if (p->databaseNames)
1593 for (i=0; i<p->num_databaseNames; i++)
1594 xfree (p->databaseNames[i]);
1595 xfree (p->databaseNames);
1597 p->num_databaseNames = argc - 2;
1599 ir_tcl_malloc (sizeof(*p->databaseNames) * (1+p->num_databaseNames));
1600 for (i=0; i<p->num_databaseNames; i++)
1602 if (ir_tcl_strdup (interp, &p->databaseNames[i], argv[2+i])
1606 p->databaseNames[i] = NULL;
1611 * do_replaceIndicator: Set/get replace Set indicator
1613 static int do_replaceIndicator (void *obj, Tcl_Interp *interp,
1614 int argc, char **argv)
1616 IrTcl_SetCObj *p = obj;
1620 p->replaceIndicator = 1;
1623 return ir_tcl_get_set_int (&p->replaceIndicator, interp, argc, argv);
1627 * do_queryType: Set/Get query method
1629 static int do_queryType (void *obj, Tcl_Interp *interp,
1630 int argc, char **argv)
1632 IrTcl_SetCObj *p = obj;
1635 return ir_tcl_strdup (interp, &p->queryType, "rpn");
1636 else if (argc == -1)
1637 return ir_tcl_strdel (interp, &p->queryType);
1640 xfree (p->queryType);
1641 if (ir_tcl_strdup (interp, &p->queryType, argv[2]) == TCL_ERROR)
1644 Tcl_AppendResult (interp, p->queryType, NULL);
1649 * do_userInformationField: Get User information field
1651 static int do_userInformationField (void *obj, Tcl_Interp *interp,
1652 int argc, char **argv)
1658 p->userInformationField = NULL;
1661 else if (argc == -1)
1662 return ir_tcl_strdel (interp, &p->userInformationField);
1663 Tcl_AppendResult (interp, p->userInformationField, NULL);
1668 * do_smallSetUpperBound: Set/get small set upper bound
1670 static int do_smallSetUpperBound (void *o, Tcl_Interp *interp,
1671 int argc, char **argv)
1673 IrTcl_SetCObj *p = o;
1677 p->smallSetUpperBound = 0;
1680 return ir_tcl_get_set_int (&p->smallSetUpperBound, interp, argc, argv);
1684 * do_largeSetLowerBound: Set/get large set lower bound
1686 static int do_largeSetLowerBound (void *o, Tcl_Interp *interp,
1687 int argc, char **argv)
1689 IrTcl_SetCObj *p = o;
1693 p->largeSetLowerBound = 2;
1696 return ir_tcl_get_set_int (&p->largeSetLowerBound, interp, argc, argv);
1700 * do_mediumSetPresentNumber: Set/get large set lower bound
1702 static int do_mediumSetPresentNumber (void *o, Tcl_Interp *interp,
1703 int argc, char **argv)
1705 IrTcl_SetCObj *p = o;
1709 p->mediumSetPresentNumber = 0;
1712 return ir_tcl_get_set_int (&p->mediumSetPresentNumber, interp, argc, argv);
1716 * do_referenceId: Set/Get referenceId
1718 static int do_referenceId (void *obj, Tcl_Interp *interp,
1719 int argc, char **argv)
1721 IrTcl_SetCObj *p = obj;
1725 p->referenceId = NULL;
1728 else if (argc == -1)
1729 return ir_tcl_strdel (interp, &p->referenceId);
1732 xfree (p->referenceId);
1733 if (ir_tcl_strdup (interp, &p->referenceId, argv[2]) == TCL_ERROR)
1736 Tcl_AppendResult (interp, p->referenceId, NULL);
1741 * do_preferredRecordSyntax: Set/get preferred record syntax
1743 static int do_preferredRecordSyntax (void *obj, Tcl_Interp *interp,
1744 int argc, char **argv)
1746 IrTcl_SetCObj *p = obj;
1750 p->preferredRecordSyntax = NULL;
1753 else if (argc == -1)
1755 xfree (p->preferredRecordSyntax);
1756 p->preferredRecordSyntax = NULL;
1761 xfree (p->preferredRecordSyntax);
1762 p->preferredRecordSyntax = NULL;
1763 if (argv[2][0] && (p->preferredRecordSyntax =
1764 ir_tcl_malloc (sizeof(*p->preferredRecordSyntax))))
1765 *p->preferredRecordSyntax = IrTcl_getRecordSyntaxVal (argv[2]);
1770 (interp,!p->preferredRecordSyntax ? "" :
1771 IrTcl_getRecordSyntaxStr(*p->preferredRecordSyntax));
1778 * do_elementSetNames: Set/Get element Set Names
1780 static int do_elementSetNames (void *obj, Tcl_Interp *interp,
1781 int argc, char **argv)
1783 IrTcl_SetCObj *p = obj;
1787 p->elementSetNames = NULL;
1790 else if (argc == -1)
1791 return ir_tcl_strdel (interp, &p->elementSetNames);
1794 xfree (p->elementSetNames);
1795 if (ir_tcl_strdup (interp, &p->elementSetNames, argv[2]) == TCL_ERROR)
1798 Tcl_AppendResult (interp, p->elementSetNames, NULL);
1803 * do_smallSetElementSetNames: Set/Get small Set Element Set Names
1805 static int do_smallSetElementSetNames (void *obj, Tcl_Interp *interp,
1806 int argc, char **argv)
1808 IrTcl_SetCObj *p = obj;
1812 p->smallSetElementSetNames = NULL;
1815 else if (argc == -1)
1816 return ir_tcl_strdel (interp, &p->smallSetElementSetNames);
1819 xfree (p->smallSetElementSetNames);
1820 if (ir_tcl_strdup (interp, &p->smallSetElementSetNames,
1821 argv[2]) == TCL_ERROR)
1824 Tcl_AppendResult (interp, p->smallSetElementSetNames, NULL);
1829 * do_mediumSetElementSetNames: Set/Get medium Set Element Set Names
1831 static int do_mediumSetElementSetNames (void *obj, Tcl_Interp *interp,
1832 int argc, char **argv)
1834 IrTcl_SetCObj *p = obj;
1838 p->mediumSetElementSetNames = NULL;
1841 else if (argc == -1)
1842 return ir_tcl_strdel (interp, &p->mediumSetElementSetNames);
1845 xfree (p->mediumSetElementSetNames);
1846 if (ir_tcl_strdup (interp, &p->mediumSetElementSetNames,
1847 argv[2]) == TCL_ERROR)
1850 Tcl_AppendResult (interp, p->mediumSetElementSetNames, NULL);
1854 static IrTcl_Method ir_method_tab[] = {
1855 { "comstack", do_comstack, NULL },
1856 { "protocol", do_protocol, NULL },
1857 { "failback", do_failback, NULL },
1858 { "failInfo", do_failInfo, NULL },
1859 { "apduInfo", do_apduInfo, NULL },
1860 { "logLevel", do_logLevel, NULL },
1862 { "eventType", do_eventType, NULL },
1863 { "connect", do_connect, NULL },
1864 { "protocolVersion", do_protocolVersion, NULL },
1865 { "preferredMessageSize", do_preferredMessageSize, NULL },
1866 { "maximumRecordSize", do_maximumRecordSize, NULL },
1867 { "implementationName", do_implementationName, NULL },
1868 { "implementationId", do_implementationId, NULL },
1869 { "implementationVersion", do_implementationVersion, NULL },
1870 { "targetImplementationName", do_targetImplementationName, NULL },
1871 { "targetImplementationId", do_targetImplementationId, NULL },
1872 { "targetImplementationVersion", do_targetImplementationVersion, NULL},
1873 { "userInformationField", do_userInformationField, NULL},
1874 { "idAuthentication", do_idAuthentication, NULL},
1875 { "options", do_options, NULL},
1876 { "init", do_init_request, NULL},
1877 { "initResult", do_initResult, NULL},
1878 { "disconnect", do_disconnect, NULL},
1879 { "callback", do_callback, NULL},
1880 { "initResponse", do_initResponse, NULL},
1881 { "triggerResourceControl", do_triggerResourceControl, NULL},
1882 { "initResponse", do_initResponse, NULL},
1886 static IrTcl_Method ir_set_c_method_tab[] = {
1887 { "databaseNames", do_databaseNames, NULL},
1888 { "replaceIndicator", do_replaceIndicator, NULL},
1889 { "queryType", do_queryType, NULL},
1890 { "preferredRecordSyntax", do_preferredRecordSyntax, NULL},
1891 { "smallSetUpperBound", do_smallSetUpperBound, NULL},
1892 { "largeSetLowerBound", do_largeSetLowerBound, NULL},
1893 { "mediumSetPresentNumber", do_mediumSetPresentNumber, NULL},
1894 { "referenceId", do_referenceId, NULL},
1895 { "elementSetNames", do_elementSetNames, NULL},
1896 { "smallSetElementSetNames", do_smallSetElementSetNames, NULL},
1897 { "mediumSetElementSetNames", do_mediumSetElementSetNames, NULL},
1902 * ir_obj_method: IR Object methods
1904 static int ir_obj_method (ClientData clientData, Tcl_Interp *interp,
1905 int argc, char **argv)
1907 IrTcl_Methods tab[3];
1908 IrTcl_Obj *p = (IrTcl_Obj *) clientData;
1913 Tcl_AppendResult (interp, wrongArgs, *argv, "method args...\"", NULL);
1917 tab[0].tab = ir_method_tab;
1919 tab[1].tab = ir_set_c_method_tab;
1920 tab[1].obj = &p->set_inher;
1923 if (ir_tcl_method (interp, argc, argv, tab, &r) == TCL_ERROR)
1924 return ir_tcl_method_error (interp, argc, argv, tab);
1929 * ir_obj_delete: IR Object disposal
1931 static void ir_obj_delete (ClientData clientData)
1933 IrTcl_Obj *obj = (IrTcl_Obj *) clientData;
1934 IrTcl_Methods tab[3];
1937 if (obj->ref_count > 0)
1939 assert (obj->ref_count == 0);
1941 logf (LOG_DEBUG, "ir object delete");
1942 tab[0].tab = ir_method_tab;
1944 tab[1].tab = ir_set_c_method_tab;
1945 tab[1].obj = &obj->set_inher;
1948 ir_tcl_method (NULL, -1, NULL, tab, NULL);
1951 odr_destroy (obj->odr_in);
1952 odr_destroy (obj->odr_out);
1953 odr_destroy (obj->odr_pr);
1958 * ir_obj_init: IR Object initialization
1960 int ir_obj_init (ClientData clientData, Tcl_Interp *interp,
1961 int argc, char **argv, ClientData *subData,
1962 ClientData parentData)
1964 IrTcl_Methods tab[3];
1972 Tcl_AppendResult (interp, wrongArgs, *argv, " objName\"", NULL);
1975 obj = ir_tcl_malloc (sizeof(*obj));
1978 obj->bibset = ccl_qual_mk ();
1979 if ((inf = fopen ("default.bib", "r")))
1981 ccl_qual_file (obj->bibset, inf);
1986 logf (LOG_DEBUG, "ir object create %s", argv[1]);
1987 obj->odr_in = odr_createmem (ODR_DECODE);
1988 odr_choice_enable_bias (obj->odr_in, 0);
1989 obj->odr_out = odr_createmem (ODR_ENCODE);
1990 obj->odr_pr = odr_createmem (ODR_PRINT);
1991 obj->state = IR_TCL_R_Idle;
1992 obj->interp = interp;
1996 obj->request_queue = NULL;
1998 tab[0].tab = ir_method_tab;
2000 tab[1].tab = ir_set_c_method_tab;
2001 tab[1].obj = &obj->set_inher;
2004 if (ir_tcl_method (interp, 0, NULL, tab, NULL) == TCL_ERROR)
2006 Tcl_AppendResult (interp, "Failed to initialize ", argv[1], NULL);
2009 *subData = (ClientData) obj;
2015 * ir_obj_mk: IR Object creation
2017 static int ir_obj_mk (ClientData clientData, Tcl_Interp *interp,
2018 int argc, char **argv)
2021 int r = ir_obj_init (clientData, interp, argc, argv, &subData, 0);
2025 Tcl_CreateCommand (interp, argv[1], ir_obj_method,
2026 subData, ir_obj_delete);
2030 IrTcl_Class ir_obj_class = {
2038 /* ------------------------------------------------------- */
2040 * do_search: Do search request
2042 static int do_search (void *o, Tcl_Interp *interp, int argc, char **argv)
2044 Z_SearchRequest *req;
2048 IrTcl_SetObj *obj = o;
2051 #if TCL_MAJOR_VERSION > 8 || (TCL_MAJOR_VERSION == 8 && TCL_MINOR_VERSION > 0)
2063 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1], "query\"",
2067 #if TCL_MAJOR_VERSION > 8 || (TCL_MAJOR_VERSION == 8 && TCL_MINOR_VERSION > 0)
2068 query_str = Tcl_UtfToExternalDString(0, argv[2], -1, &ds);
2070 query_str = argv[2];
2072 logf (LOG_DEBUG, "search %s %s", *argv, query_str);
2073 if (!obj->set_inher.num_databaseNames)
2075 Tcl_AppendResult (interp, "no databaseNames", NULL);
2076 code = ir_tcl_error_exec (interp, argc, argv);
2081 Tcl_AppendResult (interp, "not connected", NULL);
2082 code = ir_tcl_error_exec (interp, argc, argv);
2085 apdu = zget_APDU (p->odr_out, Z_APDU_searchRequest);
2086 req = apdu->u.searchRequest;
2090 set_referenceId (p->odr_out, &req->referenceId,
2091 obj->set_inher.referenceId);
2093 req->smallSetUpperBound = &obj->set_inher.smallSetUpperBound;
2094 req->largeSetLowerBound = &obj->set_inher.largeSetLowerBound;
2095 req->mediumSetPresentNumber = &obj->set_inher.mediumSetPresentNumber;
2096 req->replaceIndicator = &obj->set_inher.replaceIndicator;
2097 req->resultSetName = obj->setName ? obj->setName : "default";
2098 logf (LOG_DEBUG, "Search, resultSetName %s", req->resultSetName);
2099 req->num_databaseNames = obj->set_inher.num_databaseNames;
2100 req->databaseNames = obj->set_inher.databaseNames;
2101 for (r=0; r < obj->set_inher.num_databaseNames; r++)
2102 logf (LOG_DEBUG, " Database %s", obj->set_inher.databaseNames[r]);
2103 if (obj->set_inher.preferredRecordSyntax)
2105 struct oident ident;
2107 ident.proto = p->protocol_type;
2108 ident.oclass = CLASS_RECSYN;
2109 ident.value = *obj->set_inher.preferredRecordSyntax;
2110 logf (LOG_DEBUG, "Preferred record syntax is %d", ident.value);
2111 req->preferredRecordSyntax = odr_oiddup (p->odr_out,
2112 oid_getoidbyent (&ident));
2115 req->preferredRecordSyntax = 0;
2117 if (obj->set_inher.smallSetElementSetNames &&
2118 *obj->set_inher.smallSetElementSetNames)
2120 Z_ElementSetNames *esn = odr_malloc (p->odr_out, sizeof(*esn));
2122 esn->which = Z_ElementSetNames_generic;
2123 esn->u.generic = obj->set_inher.smallSetElementSetNames;
2124 req->smallSetElementSetNames = esn;
2127 req->smallSetElementSetNames = NULL;
2129 if (obj->set_inher.mediumSetElementSetNames &&
2130 *obj->set_inher.mediumSetElementSetNames)
2132 Z_ElementSetNames *esn = odr_malloc (p->odr_out, sizeof(*esn));
2134 esn->which = Z_ElementSetNames_generic;
2135 esn->u.generic = obj->set_inher.mediumSetElementSetNames;
2136 req->mediumSetElementSetNames = esn;
2139 req->mediumSetElementSetNames = NULL;
2141 req->query = &query;
2143 logf (LOG_DEBUG, "queryType %s", obj->set_inher.queryType);
2144 if (!strcmp (obj->set_inher.queryType, "rpn"))
2146 Z_RPNQuery *RPNquery;
2148 RPNquery = p_query_rpn (p->odr_out, p->protocol_type, query_str);
2151 Tcl_AppendResult (interp, "query syntax error", NULL);
2152 code = ir_tcl_error_exec (interp, argc, argv);
2155 query.which = Z_Query_type_1;
2156 query.u.type_1 = RPNquery;
2159 else if (!strcmp (obj->set_inher.queryType, "cclrpn"))
2163 struct ccl_rpn_node *rpn;
2164 Z_RPNQuery *RPNquery;
2167 bib1.proto = p->protocol_type;
2168 bib1.oclass = CLASS_ATTSET;
2169 bib1.value = VAL_BIB1;
2171 rpn = ccl_find_str(p->bibset, query_str, &error, &pos);
2174 Tcl_AppendResult (interp, "ccl syntax error ", ccl_err_msg(error),
2176 code = ir_tcl_error_exec (interp, argc, argv);
2180 ccl_pr_tree (rpn, stderr);
2181 fprintf (stderr, "\n");
2183 RPNquery = ccl_rpn_query(p->odr_out, rpn);
2184 RPNquery->attributeSetId = oid_getoidbyent (&bib1);
2185 query.which = Z_Query_type_1;
2186 query.u.type_1 = RPNquery;
2189 else if (!strcmp (obj->set_inher.queryType, "ccl"))
2191 query.which = Z_Query_type_2;
2192 query.u.type_2 = &ccl_query;
2193 ccl_query.buf = (unsigned char *) query_str;
2194 ccl_query.len = strlen (query_str);
2198 Tcl_AppendResult (interp, "invalid query method ",
2199 obj->set_inher.queryType, NULL);
2200 code = ir_tcl_error_exec (interp, argc, argv);
2203 code = ir_tcl_send_APDU (interp, p, apdu, "search", *argv);
2205 #if TCL_MAJOR_VERSION > 8 || (TCL_MAJOR_VERSION == 8 && TCL_MINOR_VERSION > 0)
2206 Tcl_DStringFree (&ds);
2212 * do_searchResponse: add search response handler
2214 static int do_searchResponse (void *o, Tcl_Interp *interp,
2215 int argc, char **argv)
2217 IrTcl_SetObj *obj = o;
2221 obj->searchResponse = NULL;
2224 else if (argc == -1)
2225 return ir_tcl_strdel (interp, &obj->searchResponse);
2228 xfree (obj->searchResponse);
2231 if (ir_tcl_strdup (interp, &obj->searchResponse, argv[2])
2236 obj->searchResponse = NULL;
2242 * do_presentResponse: add present response handler
2244 static int do_presentResponse (void *o, Tcl_Interp *interp,
2245 int argc, char **argv)
2247 IrTcl_SetObj *obj = o;
2251 obj->presentResponse = NULL;
2254 else if (argc == -1)
2255 return ir_tcl_strdel (interp, &obj->presentResponse);
2258 xfree (obj->presentResponse);
2261 if (ir_tcl_strdup (interp, &obj->presentResponse, argv[2])
2266 obj->presentResponse = NULL;
2272 * do_sortResponse: add sort response handler
2274 static int do_sortResponse (void *o, Tcl_Interp *interp,
2275 int argc, char **argv)
2277 IrTcl_SetObj *obj = o;
2281 obj->sortResponse = NULL;
2284 else if (argc == -1)
2285 return ir_tcl_strdel (interp, &obj->sortResponse);
2288 xfree (obj->sortResponse);
2291 if (ir_tcl_strdup (interp, &obj->sortResponse, argv[2])
2296 obj->sortResponse = NULL;
2302 * do_resultCount: Get number of hits
2304 static int do_resultCount (void *o, Tcl_Interp *interp,
2305 int argc, char **argv)
2307 IrTcl_SetObj *obj = o;
2311 obj->resultCount = 0;
2314 return ir_tcl_get_set_int (&obj->resultCount, interp, argc, argv);
2318 * do_searchStatus: Get search status (after search response)
2320 static int do_searchStatus (void *o, Tcl_Interp *interp,
2321 int argc, char **argv)
2323 IrTcl_SetObj *obj = o;
2327 return ir_tcl_get_set_int (&obj->searchStatus, interp, argc, argv);
2330 static void reset_searchResult (IrTcl_SetObj *setobj)
2333 for (i = 0; i<setobj->searchResult_num; i++)
2334 xfree (setobj->searchResult_terms[i]);
2335 xfree (setobj->searchResult_terms);
2336 xfree (setobj->searchResult_count);
2337 setobj->searchResult_terms = 0;
2338 setobj->searchResult_num = 0;
2343 * do_searchResult Get USR:Search-Result1 (after search response)
2345 static int do_searchResult (void *o, Tcl_Interp *interp,
2346 int argc, char **argv)
2348 IrTcl_SetObj *obj = o;
2353 obj->searchResult_num = 0;
2354 obj->searchResult_terms = 0;
2355 obj->searchResult_count = 0;
2358 else if (argc == -1)
2360 reset_searchResult (obj);
2363 for (i = 0; i<obj->searchResult_num; i++)
2366 sprintf (str, "%d", obj->searchResult_count[i]);
2367 Tcl_AppendResult (interp, "{", NULL);
2368 if (obj->searchResult_terms[i])
2369 Tcl_AppendElement (interp, obj->searchResult_terms[i]);
2371 Tcl_AppendElement (interp, "");
2372 Tcl_AppendElement (interp, str);
2373 Tcl_AppendResult (interp, "} ", NULL);
2379 * do_presentStatus: Get search status (after search/present response)
2381 static int do_presentStatus (void *o, Tcl_Interp *interp,
2382 int argc, char **argv)
2384 IrTcl_SetObj *obj = o;
2388 return ir_tcl_get_set_int (&obj->presentStatus, interp, argc, argv);
2392 * do_sortStatus: Get sort status (after sort response)
2394 static int do_sortStatus (void *o, Tcl_Interp *interp,
2395 int argc, char **argv)
2397 IrTcl_SetObj *obj = o;
2402 obj->sortStatus = Z_SortStatus_failure;
2405 switch (obj->sortStatus)
2407 case Z_SortStatus_success:
2408 res = "success"; break;
2409 case Z_SortStatus_partial_1:
2410 res = "partial"; break;
2411 case Z_SortStatus_failure:
2412 res = "failure"; break;
2414 res = "unknown"; break;
2416 Tcl_AppendElement (interp, res);
2421 * do_nextResultSetPosition: Get next result set position
2422 * (after search/present response)
2424 static int do_nextResultSetPosition (void *o, Tcl_Interp *interp,
2425 int argc, char **argv)
2427 IrTcl_SetObj *obj = o;
2431 obj->nextResultSetPosition = 0;
2434 return ir_tcl_get_set_int (&obj->nextResultSetPosition, interp,
2439 * do_setName: Set result Set name
2441 static int do_setName (void *o, Tcl_Interp *interp,
2442 int argc, char **argv)
2444 IrTcl_SetObj *obj = o;
2447 return ir_tcl_strdup (interp, &obj->setName, "Default");
2448 else if (argc == -1)
2449 return ir_tcl_strdel (interp, &obj->setName);
2452 xfree (obj->setName);
2453 if (ir_tcl_strdup (interp, &obj->setName, argv[2])
2457 Tcl_AppendElement (interp, obj->setName);
2462 * do_numberOfRecordsReturned: Get number of records returned
2464 static int do_numberOfRecordsReturned (void *o, Tcl_Interp *interp,
2465 int argc, char **argv)
2467 IrTcl_SetObj *obj = o;
2471 obj->numberOfRecordsReturned = 0;
2474 return ir_tcl_get_set_int (&obj->numberOfRecordsReturned, interp,
2479 * do_type: Return type (if any) at position.
2481 static int do_type (void *o, Tcl_Interp *interp, int argc, char **argv)
2483 IrTcl_SetObj *obj = o;
2485 IrTcl_RecordList *rl;
2489 obj->record_list = NULL;
2492 else if (argc == -1)
2494 delete_IR_records (obj);
2499 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2500 " position\"", NULL);
2503 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2505 rl = find_IR_record (obj, offset);
2508 logf (LOG_DEBUG, "%s %s %s: no record", argv[0], argv[1], argv[2]);
2513 case Z_NamePlusRecord_databaseRecord:
2514 interp->result = "DB";
2516 case Z_NamePlusRecord_surrogateDiagnostic:
2517 interp->result = "SD";
2525 * do_recordType: Return record type (if any) at position.
2527 static int do_recordType (void *o, Tcl_Interp *interp, int argc, char **argv)
2529 IrTcl_SetObj *obj = o;
2531 IrTcl_RecordList *rl;
2537 else if (argc == -1)
2543 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2544 " position\"", NULL);
2547 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2549 rl = find_IR_record (obj, offset);
2552 logf (LOG_DEBUG, "%s %s %s: no record", argv[0], argv[1], argv[2]);
2555 if (rl->which != Z_NamePlusRecord_databaseRecord)
2557 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2560 Tcl_AppendElement (interp, (char*)
2561 IrTcl_getRecordSyntaxStr (rl->u.dbrec.type));
2566 * set record elements (for record extraction)
2568 static int do_recordElements (void *o, Tcl_Interp *interp,
2569 int argc, char **argv)
2571 IrTcl_SetObj *obj = o;
2575 obj->recordElements = NULL;
2578 else if (argc == -1)
2579 return ir_tcl_strdel (NULL, &obj->recordElements);
2582 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2583 " ?position?\"", NULL);
2588 xfree (obj->recordElements);
2589 return ir_tcl_strdup (NULL, &obj->recordElements,
2590 (*argv[2] ? argv[2] : NULL));
2592 Tcl_AppendResult (interp, obj->recordElements, NULL);
2599 static int ir_diagResult (Tcl_Interp *interp, IrTcl_Diagnostic *list, int num)
2605 for (i = 0; i<num; i++)
2607 sprintf (buf, "%d", list[i].condition);
2608 Tcl_AppendElement (interp, buf);
2609 cp = diagbib1_str (list[i].condition);
2611 Tcl_AppendElement (interp, (char*) cp);
2613 Tcl_AppendElement (interp, "");
2614 if (list[i].addinfo)
2615 Tcl_AppendElement (interp, (char*) list[i].addinfo);
2617 Tcl_AppendElement (interp, "");
2623 * do_diag: Return diagnostic record info
2625 static int do_diag (void *o, Tcl_Interp *interp, int argc, char **argv)
2627 IrTcl_SetObj *obj = o;
2629 IrTcl_RecordList *rl;
2635 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2636 " position\"", NULL);
2639 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2641 rl = find_IR_record (obj, offset);
2644 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2647 if (rl->which != Z_NamePlusRecord_surrogateDiagnostic)
2649 Tcl_AppendResult (interp, "No Diagnostic record at #", argv[2], NULL);
2652 return ir_diagResult (interp, rl->u.surrogateDiagnostics.list,
2653 rl->u.surrogateDiagnostics.num);
2657 * do_getMarc: Get ISO2709 Record lines/fields
2659 static int do_getMarc (void *o, Tcl_Interp *interp, int argc, char **argv)
2661 IrTcl_SetObj *obj = o;
2663 IrTcl_RecordList *rl;
2669 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2670 " position line|field tag ind field\"", NULL);
2673 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2675 rl = find_IR_record (obj, offset);
2678 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2681 if (rl->which != Z_NamePlusRecord_databaseRecord)
2683 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2686 return ir_tcl_get_marc (interp, rl->u.dbrec.buf, argc, argv);
2690 * do_getSutrs: Get SUTRS Record
2692 static int do_getSutrs (void *o, Tcl_Interp *interp, int argc, char **argv)
2694 IrTcl_SetObj *obj = o;
2696 IrTcl_RecordList *rl;
2702 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2703 " position\"", NULL);
2706 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2708 rl = find_IR_record (obj, offset);
2711 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2714 if (rl->which != Z_NamePlusRecord_databaseRecord)
2716 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2719 if (!rl->u.dbrec.buf || rl->u.dbrec.type != VAL_SUTRS)
2721 Tcl_AppendElement (interp, rl->u.dbrec.buf);
2727 * do_getGrs: Get a GRS-1 Record
2729 static int do_getGrs (void *o, Tcl_Interp *interp, int argc, char **argv)
2731 IrTcl_SetObj *obj = o;
2733 IrTcl_RecordList *rl;
2739 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2740 " position ?(set,tag) (set,tag) ...?\"", NULL);
2743 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2745 rl = find_IR_record (obj, offset);
2748 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2751 if (rl->which != Z_NamePlusRecord_databaseRecord)
2753 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2756 if (rl->u.dbrec.type != VAL_GRS1)
2758 return ir_tcl_get_grs (interp, rl->u.dbrec.u.grs1, argc, argv);
2763 * do_getExplain: Get an Explain Record
2765 static int do_getExplain (void *o, Tcl_Interp *interp, int argc, char **argv)
2767 IrTcl_SetObj *obj = o;
2768 IrTcl_Obj *p = obj->parent;
2770 Z_ext_typeent *etype;
2772 IrTcl_RecordList *rl;
2778 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2779 " position ?mask? ...\"", NULL);
2782 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2784 rl = find_IR_record (obj, offset);
2787 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2790 if (rl->which != Z_NamePlusRecord_databaseRecord)
2792 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2795 if (!rl->u.dbrec.buf || rl->u.dbrec.type != VAL_EXPLAIN)
2798 if (!(etype = z_ext_getentbyref (VAL_EXPLAIN)))
2800 assert (rl->u.dbrec.buf);
2801 odr_setbuf (p->odr_in, rl->u.dbrec.buf, rl->u.dbrec.size, 0);
2802 if (!(*etype->fun)(p->odr_in, (char **) &rr, 0, 0))
2805 if (etype->what != Z_External_explainRecord)
2808 return ir_tcl_get_explain (interp, rr, argc, argv);
2812 * do_responseStatus: Return response status (present or search)
2814 static int do_responseStatus (void *o, Tcl_Interp *interp,
2815 int argc, char **argv)
2817 IrTcl_SetObj *obj = o;
2821 obj->recordFlag = 0;
2822 obj->nonSurrogateDiagnosticNum = 0;
2823 obj->nonSurrogateDiagnosticList = NULL;
2826 else if (argc == -1)
2828 ir_deleteDiags (&obj->nonSurrogateDiagnosticList,
2829 &obj->nonSurrogateDiagnosticNum);
2832 if (!obj->recordFlag)
2834 Tcl_AppendElement (interp, "OK");
2839 case Z_Records_DBOSD:
2840 Tcl_AppendElement (interp, "DBOSD");
2843 Tcl_AppendElement (interp, "NSD");
2844 return ir_diagResult (interp, obj->nonSurrogateDiagnosticList,
2845 obj->nonSurrogateDiagnosticNum);
2846 case Z_Records_multipleNSD:
2847 Tcl_AppendElement (interp, "NSD");
2848 return ir_diagResult (interp, obj->nonSurrogateDiagnosticList,
2849 obj->nonSurrogateDiagnosticNum);
2855 * do_present: Perform Present Request
2858 static int do_present (void *o, Tcl_Interp *interp, int argc, char **argv)
2860 IrTcl_SetObj *obj = o;
2863 Z_PresentRequest *req;
2871 if (Tcl_GetInt (interp, argv[2], &start) == TCL_ERROR)
2878 if (Tcl_GetInt (interp, argv[3], &number) == TCL_ERROR)
2883 logf (LOG_DEBUG, "present %s %d %d", *argv, start, number);
2887 Tcl_AppendResult (interp, "not connected", NULL);
2888 return ir_tcl_error_exec (interp, argc, argv);
2891 obj->number = number;
2893 apdu = zget_APDU (p->odr_out, Z_APDU_presentRequest);
2894 req = apdu->u.presentRequest;
2896 set_referenceId (p->odr_out, &req->referenceId,
2897 obj->set_inher.referenceId);
2899 req->resultSetId = obj->setName ? obj->setName : "Default";
2901 req->resultSetStartPoint = &start;
2902 req->numberOfRecordsRequested = &number;
2903 if (obj->set_inher.preferredRecordSyntax)
2905 struct oident ident;
2907 ident.proto = p->protocol_type;
2908 ident.oclass = CLASS_RECSYN;
2909 ident.value = *obj->set_inher.preferredRecordSyntax;
2910 logf (LOG_DEBUG, "Preferred record syntax is %d", ident.value);
2911 req->preferredRecordSyntax = odr_oiddup (p->odr_out,
2912 oid_getoidbyent (&ident));
2915 req->preferredRecordSyntax = 0;
2917 if (obj->set_inher.elementSetNames && *obj->set_inher.elementSetNames)
2919 Z_ElementSetNames *esn = odr_malloc (p->odr_out, sizeof(*esn));
2920 Z_RecordComposition *compo = odr_malloc (p->odr_out, sizeof(*compo));
2922 esn->which = Z_ElementSetNames_generic;
2923 esn->u.generic = obj->set_inher.elementSetNames;
2925 req->recordComposition = compo;
2926 compo->which = Z_RecordComp_simple;
2927 compo->u.simple = esn;
2930 req->recordComposition = NULL;
2931 return ir_tcl_send_APDU (interp, p, apdu, "present", *argv);
2934 #define IR_TCL_RECORD_ENCODING_ISO2709 1
2935 #define IR_TCL_RECORD_ENCODING_RAW 2
2941 } IrTcl_FileRecordHead;
2944 * do_loadFile: Load result set from file
2946 static int do_loadFile (void *o, Tcl_Interp *interp,
2947 int argc, char **argv)
2949 IrTcl_SetObj *setobj = o;
2961 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2962 " filename ?start? ?number?\"", NULL);
2966 start = atoi (argv[3]);
2968 number = atoi (argv[4]);
2971 inf = fopen (argv[2], "r");
2974 Tcl_AppendResult (interp, "Cannot open ", argv[2], NULL);
2975 return ir_tcl_error_exec (interp, argc, argv);
2977 while (offset < (start+number))
2979 IrTcl_FileRecordHead head;
2980 IrTcl_RecordList *rl;
2982 if (fread (&head, sizeof(head), 1, inf) < 1)
2984 rl = new_IR_record (setobj, offset,
2985 Z_NamePlusRecord_databaseRecord,
2986 (argc > 5) ? argv[5] : NULL);
2987 rl->u.dbrec.type = head.syntax;
2988 if (head.encoding == IR_TCL_RECORD_ENCODING_ISO2709)
2990 if (!(buf = ir_tcl_fread_marc (inf, &size)))
2992 rl->u.dbrec.buf = buf;
2993 rl->u.dbrec.size = size;
2994 if (size != head.size)
2997 Tcl_AppendResult (interp, "bad ISO2709 encoding", NULL);
2998 return ir_tcl_error_exec (interp, argc, argv);
3001 else if (head.encoding == IR_TCL_RECORD_ENCODING_RAW)
3003 rl->u.dbrec.size = head.size;
3004 rl->u.dbrec.buf = ir_tcl_malloc (head.size + 1);
3005 if (fread (rl->u.dbrec.buf, rl->u.dbrec.size, 1, inf) < 1)
3008 Tcl_AppendResult (interp, "bad raw encoding", NULL);
3009 return ir_tcl_error_exec (interp, argc, argv);
3011 rl->u.dbrec.buf[rl->u.dbrec.size] = '\0';
3015 rl->u.dbrec.buf = NULL;
3016 rl->u.dbrec.size = 0;
3018 Tcl_AppendResult (interp, "bad encoding", NULL);
3019 return ir_tcl_error_exec (interp, argc, argv);
3023 setobj->numberOfRecordsReturned = offset - start;
3029 * do_saveFile: Save result set on file
3031 static int do_saveFile (void *o, Tcl_Interp *interp,
3032 int argc, char **argv)
3034 IrTcl_SetObj *setobj = o;
3039 IrTcl_RecordList *rl;
3045 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
3046 " filename ?start? ?number?\"", NULL);
3050 start = atoi (argv[3]);
3052 number = atoi (argv[4]);
3055 outf = fopen (argv[2], "w");
3058 Tcl_AppendResult (interp, "cannot open file", NULL);
3059 return ir_tcl_error_exec (interp, argc, argv);
3061 while (offset < (start+number) && (rl = find_IR_record (setobj, offset)))
3063 if (rl->which == Z_NamePlusRecord_databaseRecord &&
3064 rl->u.dbrec.buf && rl->u.dbrec.size)
3066 IrTcl_FileRecordHead head;
3068 head.encoding = IR_TCL_RECORD_ENCODING_RAW;
3069 head.syntax = rl->u.dbrec.type;
3070 head.size = rl->u.dbrec.size;
3071 if (fwrite (&head, sizeof(head), 1, outf) < 1)
3073 Tcl_AppendResult (interp, "cannot write", NULL);
3074 return ir_tcl_error_exec (interp, argc, argv);
3076 if (fwrite (rl->u.dbrec.buf, rl->u.dbrec.size, 1, outf) < 1)
3078 Tcl_AppendResult (interp, "cannot write", NULL);
3079 return ir_tcl_error_exec (interp, argc, argv);
3086 Tcl_AppendResult (interp, "cannot write ", NULL);
3087 return ir_tcl_error_exec (interp, argc, argv);
3093 /* ------------------------------------------------------- */
3095 * do_sort: Do sort request
3097 static int do_sort (void *o, Tcl_Interp *interp, int argc, char **argv)
3101 IrTcl_SetObj *obj = o;
3103 char sort_string[64], sort_flags[64];
3106 Z_SortKeySpecList *sksl;
3117 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1], "query\"",
3121 logf (LOG_DEBUG, "sort %s %s", *argv, argv[2]);
3124 Tcl_AppendResult (interp, "not connected", NULL);
3125 return ir_tcl_error_exec (interp, argc, argv);
3127 apdu = zget_APDU (p->odr_out, Z_APDU_sortRequest);
3128 sksl = (Z_SortKeySpecList *) odr_malloc (p->odr_out, sizeof(*sksl));
3129 req = apdu->u.sortRequest;
3131 set_referenceId (p->odr_out, &req->referenceId,
3132 obj->set_inher.referenceId);
3135 req->num_inputResultSetNames = 1;
3136 req->inputResultSetNames = (Z_InternationalString **)
3137 odr_malloc (p->odr_out, sizeof(*req->inputResultSetNames));
3138 req->inputResultSetNames[0] = obj->setName;
3140 req->inputResultSetNames =
3141 (Z_StringList *)odr_malloc (p->odr_out,
3142 sizeof(*req->inputResultSetNames));
3143 req->inputResultSetNames->num_strings = 1;
3144 req->inputResultSetNames->strings =
3145 (char **)odr_malloc (p->odr_out,
3146 sizeof(*req->inputResultSetNames->strings));
3147 req->inputResultSetNames->strings[0] = obj->setName;
3150 req->sortedResultSetName = (char *) obj->setName;
3153 req->sortSequence = sksl;
3154 sksl->num_specs = 0;
3155 sksl->specs = (Z_SortKeySpec **)
3156 odr_malloc (p->odr_out, sizeof(sksl->specs) * 20);
3158 bib1.proto = PROTO_Z3950;
3159 bib1.oclass = CLASS_ATTSET;
3160 bib1.value = VAL_BIB1;
3162 while ((sscanf (arg, "%63s %63s%n", sort_string, sort_flags, &off)) == 2
3166 char *sort_string_sep;
3167 Z_SortKeySpec *sks = (Z_SortKeySpec *)
3168 odr_malloc (p->odr_out, sizeof(*sks));
3169 Z_SortKey *sk = (Z_SortKey *)
3170 odr_malloc (p->odr_out, sizeof(*sk));
3173 sksl->specs[sksl->num_specs++] = sks;
3174 sks->sortElement = (Z_SortElement *)
3175 odr_malloc (p->odr_out, sizeof(*sks->sortElement));
3176 sks->sortElement->which = Z_SortElement_generic;
3177 sks->sortElement->u.generic = sk;
3179 if ((sort_string_sep = strchr (sort_string, '=')))
3181 Z_AttributeElement *el = (Z_AttributeElement *)
3182 odr_malloc (p->odr_out, sizeof(*el));
3183 sk->which = Z_SortKey_sortAttributes;
3184 sk->u.sortAttributes =
3185 (Z_SortAttributes *)
3186 odr_malloc (p->odr_out, sizeof(*sk->u.sortAttributes));
3187 sk->u.sortAttributes->id = oid_ent_to_oid(&bib1, oid);
3188 sk->u.sortAttributes->list =
3190 odr_malloc (p->odr_out, sizeof(*sk->u.sortAttributes->list));
3191 sk->u.sortAttributes->list->num_attributes = 1;
3192 sk->u.sortAttributes->list->attributes =
3193 (Z_AttributeElement **)odr_malloc (p->odr_out,
3194 sizeof(*sk->u.sortAttributes->list->attributes));
3195 sk->u.sortAttributes->list->attributes[0] = el;
3196 el->attributeSet = 0;
3197 el->attributeType = (int *)
3198 odr_malloc (p->odr_out, sizeof(*el->attributeType));
3199 *el->attributeType = atoi (sort_string);
3200 el->which = Z_AttributeValue_numeric;
3201 el->value.numeric = (int *)
3202 odr_malloc (p->odr_out, sizeof(*el->value.numeric));
3203 *el->value.numeric = atoi (sort_string_sep + 1);
3207 sk->which = Z_SortKey_sortField;
3208 sk->u.sortField = (char *)odr_malloc (p->odr_out, strlen(sort_string)+1);
3209 strcpy (sk->u.sortField, sort_string);
3211 sks->sortRelation = (int *)
3212 odr_malloc (p->odr_out, sizeof(*sks->sortRelation));
3213 *sks->sortRelation = Z_SortRelation_ascending;
3214 sks->caseSensitivity = (int *)
3215 odr_malloc (p->odr_out, sizeof(*sks->caseSensitivity));
3216 *sks->caseSensitivity = Z_SortCase_caseSensitive;
3219 sks->which = Z_SortKeySpec_null;
3220 sks->u.null = odr_nullval ();
3222 sks->missingValueAction = NULL;
3225 for (i = 0; sort_flags[i]; i++)
3227 switch (sort_flags[i])
3232 *sks->sortRelation = Z_SortRelation_descending;
3237 *sks->sortRelation = Z_SortRelation_ascending;
3241 *sks->caseSensitivity = Z_SortCase_caseInsensitive;
3245 *sks->caseSensitivity = Z_SortCase_caseSensitive;
3250 if (!sksl->num_specs)
3252 printf ("Missing sort specifications\n");
3255 return ir_tcl_send_APDU (interp, p, apdu, "sort", *argv);
3258 static IrTcl_Method ir_set_method_tab[] = {
3259 { "search", do_search, NULL},
3260 { "searchResponse", do_searchResponse, NULL},
3261 { "presentResponse", do_presentResponse, NULL},
3262 { "searchStatus", do_searchStatus, NULL},
3263 { "presentStatus", do_presentStatus, NULL},
3264 { "nextResultSetPosition", do_nextResultSetPosition, NULL},
3265 { "setName", do_setName, NULL},
3266 { "resultCount", do_resultCount, NULL},
3267 { "numberOfRecordsReturned", do_numberOfRecordsReturned, NULL},
3268 { "present", do_present, NULL},
3269 { "type", do_type, NULL},
3270 { "getMarc", do_getMarc, NULL},
3271 { "getSutrs", do_getSutrs, NULL},
3272 { "getGrs", do_getGrs, NULL},
3273 { "getExplain", do_getExplain, NULL},
3274 { "recordType", do_recordType, NULL},
3275 { "recordElements", do_recordElements, NULL},
3276 { "diag", do_diag, NULL},
3277 { "responseStatus", do_responseStatus, NULL},
3278 { "loadFile", do_loadFile, NULL},
3279 { "saveFile", do_saveFile, NULL},
3280 { "sort", do_sort, NULL },
3281 { "sortResponse", do_sortResponse, NULL},
3282 { "sortStatus", do_sortStatus, NULL},
3283 { "searchResult", do_searchResult, NULL},
3288 * ir_set_obj_method: IR Set Object methods
3290 static int ir_set_obj_method (ClientData clientData, Tcl_Interp *interp,
3291 int argc, char **argv)
3293 IrTcl_Methods tabs[3];
3294 IrTcl_SetObj *p = (IrTcl_SetObj *) clientData;
3299 Tcl_AppendResult (interp, wrongArgs, *argv, " method args...\"", NULL);
3302 tabs[0].tab = ir_set_method_tab;
3304 tabs[1].tab = ir_set_c_method_tab;
3305 tabs[1].obj = &p->set_inher;
3308 if (ir_tcl_method (interp, argc, argv, tabs, &r) == TCL_ERROR)
3309 return ir_tcl_method_error (interp, argc, argv, tabs);
3314 * ir_set_obj_delete: IR Set Object disposal
3316 static void ir_set_obj_delete (ClientData clientData)
3318 IrTcl_Methods tabs[3];
3319 IrTcl_SetObj *p = (IrTcl_SetObj *) clientData;
3321 logf (LOG_DEBUG, "ir set delete");
3323 tabs[0].tab = ir_set_method_tab;
3325 tabs[1].tab = ir_set_c_method_tab;
3326 tabs[1].obj = &p->set_inher;
3329 ir_tcl_method (NULL, -1, NULL, tabs, NULL);
3335 * ir_set_obj_init: IR Set Object initialization
3337 static int ir_set_obj_init (ClientData clientData, Tcl_Interp *interp,
3338 int argc, char **argv, ClientData *subData,
3339 ClientData parentData)
3341 IrTcl_Methods tabs[3];
3344 if (argc < 2 || argc > 3)
3346 Tcl_AppendResult (interp, wrongArgs, *argv,
3347 " objSetName ?objParent?\"", NULL);
3350 obj = ir_tcl_malloc (sizeof(*obj));
3351 logf (LOG_DEBUG, "ir set create %s", argv[1]);
3358 obj->parent = (IrTcl_Obj *) parentData;
3360 dst = &obj->set_inher;
3361 src = &obj->parent->set_inher;
3363 if ((dst->num_databaseNames = src->num_databaseNames))
3365 dst->databaseNames =
3366 ir_tcl_malloc (sizeof (*dst->databaseNames)
3367 * (1+dst->num_databaseNames));
3368 for (i = 0; i < dst->num_databaseNames; i++)
3369 if (ir_tcl_strdup (interp, &dst->databaseNames[i],
3370 src->databaseNames[i]) == TCL_ERROR)
3372 dst->databaseNames[i] = NULL;
3375 dst->databaseNames = NULL;
3376 if (ir_tcl_strdup (interp, &dst->queryType, src->queryType)
3380 if (ir_tcl_strdup (interp, &dst->referenceId, src->referenceId)
3384 if (ir_tcl_strdup (interp, &dst->elementSetNames, src->elementSetNames)
3388 if (ir_tcl_strdup (interp, &dst->smallSetElementSetNames,
3389 src->smallSetElementSetNames)
3393 if (ir_tcl_strdup (interp, &dst->mediumSetElementSetNames,
3394 src->mediumSetElementSetNames)
3398 if (src->preferredRecordSyntax &&
3399 (dst->preferredRecordSyntax
3400 = ir_tcl_malloc (sizeof(*dst->preferredRecordSyntax))))
3401 *dst->preferredRecordSyntax = *src->preferredRecordSyntax;
3403 dst->preferredRecordSyntax = NULL;
3404 dst->replaceIndicator = src->replaceIndicator;
3405 dst->smallSetUpperBound = src->smallSetUpperBound;
3406 dst->largeSetLowerBound = src->largeSetLowerBound;
3407 dst->mediumSetPresentNumber = src->mediumSetPresentNumber;
3412 tabs[0].tab = ir_set_method_tab;
3416 if (ir_tcl_method (interp, 0, NULL, tabs, NULL) == TCL_ERROR)
3419 *subData = (ClientData) obj;
3424 * ir_set_obj_mk: IR Set Object creation
3426 static int ir_set_obj_mk (ClientData clientData, Tcl_Interp *interp,
3427 int argc, char **argv)
3430 ClientData parentData = 0;
3435 Tcl_CmdInfo parent_info;
3436 if (!Tcl_GetCommandInfo (interp, argv[2], &parent_info))
3438 Tcl_AppendResult (interp, "no object parent", NULL);
3439 return ir_tcl_error_exec (interp, argc, argv);
3441 parentData = parent_info.clientData;
3443 r = ir_set_obj_init (clientData, interp, argc, argv, &subData, parentData);
3446 Tcl_CreateCommand (interp, argv[1], ir_set_obj_method,
3447 subData, ir_set_obj_delete);
3451 IrTcl_Class ir_set_obj_class = {
3458 /* ------------------------------------------------------- */
3461 * do_scan: Perform scan
3463 static int do_scan (void *o, Tcl_Interp *interp, int argc, char **argv)
3467 IrTcl_ScanObj *obj = o;
3468 IrTcl_Obj *p = obj->parent;
3473 struct ccl_rpn_node *rpn;
3477 #if TCL_MAJOR_VERSION > 8 || (TCL_MAJOR_VERSION == 8 && TCL_MINOR_VERSION > 0)
3485 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
3486 " scanQuery\"", NULL);
3489 #if TCL_MAJOR_VERSION > 8 || (TCL_MAJOR_VERSION == 8 && TCL_MINOR_VERSION > 0)
3490 start_term = Tcl_UtfToExternalDString(0, argv[2], -1, &ds);
3492 start_term = argv[2];
3494 logf (LOG_DEBUG, "scan %s %s", *argv, start_term);
3495 if (!p->set_inher.num_databaseNames)
3497 Tcl_AppendResult (interp, "no databaseNames", NULL);
3498 code = ir_tcl_error_exec (interp, argc, argv);
3503 Tcl_AppendResult (interp, "not connected", NULL);
3504 code = ir_tcl_error_exec (interp, argc, argv);
3508 apdu = zget_APDU (p->odr_out, Z_APDU_scanRequest);
3509 req = apdu->u.scanRequest;
3511 set_referenceId (p->odr_out, &req->referenceId, p->set_inher.referenceId);
3512 req->num_databaseNames = p->set_inher.num_databaseNames;
3513 req->databaseNames = p->set_inher.databaseNames;
3517 if (!(req->termListAndStartPoint =
3518 p_query_scan (p->odr_out, p->protocol_type,
3519 &req->attributeSet, start_term)))
3521 Tcl_AppendResult (interp, "query syntax error", NULL);
3522 code = ir_tcl_error_exec (interp, argc, argv);
3526 rpn = ccl_find_str(p->bibset, start_term, &r, &pos);
3529 Tcl_AppendResult (interp, "ccl syntax error ", ccl_err_msg(r), NULL);
3530 code = ir_tcl_error_exec (interp, argc, argv);
3533 bib1.proto = p->protocol_type;
3534 bib1.oclass = CLASS_ATTSET;
3535 bib1.value = VAL_BIB1;
3537 req->attributeSet = oid_getoidbyent (&bib1);
3538 if (!(req->termListAndStartPoint = ccl_scan_query (p->odr_out, rpn)))
3544 req->stepSize = &obj->stepSize;
3545 req->numberOfTermsRequested = &obj->numberOfTermsRequested;
3546 req->preferredPositionInResponse = &obj->preferredPositionInResponse;
3547 logf (LOG_DEBUG, "stepSize=%d", *req->stepSize);
3548 logf (LOG_DEBUG, "numberOfTermsRequested=%d",
3549 *req->numberOfTermsRequested);
3550 logf (LOG_DEBUG, "preferredPositionInResponse=%d",
3551 *req->preferredPositionInResponse);
3553 code = ir_tcl_send_APDU (interp, p, apdu, "scan", *argv);
3555 #if TCL_MAJOR_VERSION > 8 || (TCL_MAJOR_VERSION == 8 && TCL_MINOR_VERSION > 0)
3556 Tcl_DStringFree (&ds);
3562 * do_scanResponse: add scan response handler
3564 static int do_scanResponse (void *o, Tcl_Interp *interp,
3565 int argc, char **argv)
3567 IrTcl_ScanObj *obj = o;
3571 obj->scanResponse = NULL;
3574 else if (argc == -1)
3575 return ir_tcl_strdel (interp, &obj->scanResponse);
3578 xfree (obj->scanResponse);
3581 if (ir_tcl_strdup (interp, &obj->scanResponse, argv[2])
3586 obj->scanResponse = NULL;
3592 * do_stepSize: Set/get replace Step Size
3594 static int do_stepSize (void *obj, Tcl_Interp *interp,
3595 int argc, char **argv)
3597 IrTcl_ScanObj *p = obj;
3603 return ir_tcl_get_set_int (&p->stepSize, interp, argc, argv);
3607 * do_numberOfTermsRequested: Set/get Number of Terms requested
3609 static int do_numberOfTermsRequested (void *obj, Tcl_Interp *interp,
3610 int argc, char **argv)
3612 IrTcl_ScanObj *p = obj;
3616 p->numberOfTermsRequested = 20;
3619 return ir_tcl_get_set_int (&p->numberOfTermsRequested, interp, argc, argv);
3624 * do_preferredPositionInResponse: Set/get preferred Position
3626 static int do_preferredPositionInResponse (void *obj, Tcl_Interp *interp,
3627 int argc, char **argv)
3629 IrTcl_ScanObj *p = obj;
3633 p->preferredPositionInResponse = 1;
3636 return ir_tcl_get_set_int (&p->preferredPositionInResponse, interp,
3641 * do_scanStatus: Get scan status
3643 static int do_scanStatus (void *obj, Tcl_Interp *interp,
3644 int argc, char **argv)
3646 IrTcl_ScanObj *p = obj;
3650 return ir_tcl_get_set_int (&p->scanStatus, interp, argc, argv);
3654 * do_numberOfEntriesReturned: Get number of Entries returned
3656 static int do_numberOfEntriesReturned (void *obj, Tcl_Interp *interp,
3657 int argc, char **argv)
3659 IrTcl_ScanObj *p = obj;
3663 return ir_tcl_get_set_int (&p->numberOfEntriesReturned, interp,
3668 * do_positionOfTerm: Get position of Term
3670 static int do_positionOfTerm (void *obj, Tcl_Interp *interp,
3671 int argc, char **argv)
3673 IrTcl_ScanObj *p = obj;
3677 return ir_tcl_get_set_int (&p->positionOfTerm, interp, argc, argv);
3681 * do_scanLine: get Scan Line (surrogate or normal) after response
3683 static int do_scanLine (void *obj, Tcl_Interp *interp, int argc, char **argv)
3685 IrTcl_ScanObj *p = obj;
3691 p->entries_flag = 0;
3693 p->nonSurrogateDiagnosticNum = 0;
3694 p->nonSurrogateDiagnosticList = 0;
3697 else if (argc == -1)
3699 p->entries_flag = 0;
3700 /* release entries */
3703 ir_deleteDiags (&p->nonSurrogateDiagnosticList,
3704 &p->nonSurrogateDiagnosticNum);
3709 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
3710 " position\"", NULL);
3713 if (Tcl_GetInt (interp, argv[2], &i) == TCL_ERROR)
3715 if (!p->entries_flag || !p->entries || i >= p->num_entries || i < 0)
3717 switch (p->entries[i].which)
3719 case Z_Entry_termInfo:
3720 Tcl_AppendElement (interp, "T");
3721 if (p->entries[i].u.term.buf)
3722 Tcl_AppendElement (interp, p->entries[i].u.term.buf);
3724 Tcl_AppendElement (interp, "");
3725 sprintf (numstr, "%d", p->entries[i].u.term.globalOccurrences);
3726 Tcl_AppendElement (interp, numstr);
3728 case Z_Entry_surrogateDiagnostic:
3729 Tcl_AppendElement (interp, "SD");
3730 return ir_diagResult (interp, p->entries[i].u.diag.list,
3731 p->entries[i].u.diag.num);
3737 static IrTcl_Method ir_scan_method_tab[] = {
3738 { "scan", do_scan, NULL},
3739 { "scanResponse", do_scanResponse, NULL},
3740 { "stepSize", do_stepSize, NULL},
3741 { "numberOfTermsRequested", do_numberOfTermsRequested, NULL},
3742 { "preferredPositionInResponse", do_preferredPositionInResponse, NULL},
3743 { "scanStatus", do_scanStatus, NULL},
3744 { "numberOfEntriesReturned", do_numberOfEntriesReturned, NULL},
3745 { "positionOfTerm", do_positionOfTerm, NULL},
3746 { "scanLine", do_scanLine, NULL},
3751 * ir_scan_obj_method: IR Scan Object methods
3753 static int ir_scan_obj_method (ClientData clientData, Tcl_Interp *interp,
3754 int argc, char **argv)
3756 IrTcl_Methods tabs[2];
3761 Tcl_AppendResult (interp, wrongArgs, *argv, " method args...\"", NULL);
3764 tabs[0].tab = ir_scan_method_tab;
3765 tabs[0].obj = clientData;
3768 if (ir_tcl_method (interp, argc, argv, tabs, &r) == TCL_ERROR)
3769 return ir_tcl_method_error (interp, argc, argv, tabs);
3774 * ir_scan_obj_delete: IR Scan Object disposal
3776 static void ir_scan_obj_delete (ClientData clientData)
3778 IrTcl_Methods tabs[2];
3779 IrTcl_ScanObj *obj = (IrTcl_ScanObj *) clientData;
3781 tabs[0].tab = ir_scan_method_tab;
3785 ir_tcl_method (NULL, -1, NULL, tabs, NULL);
3790 * ir_scan_obj_mk: IR Scan Object creation
3792 static int ir_scan_obj_mk (ClientData clientData, Tcl_Interp *interp,
3793 int argc, char **argv)
3795 Tcl_CmdInfo parent_info;
3797 IrTcl_Methods tabs[2];
3801 Tcl_AppendResult (interp, wrongArgs, *argv,
3802 "objScanName objParentName\"", NULL);
3805 logf (LOG_DEBUG, "ir scan create %s", argv[1]);
3806 if (!Tcl_GetCommandInfo (interp, argv[2], &parent_info))
3808 Tcl_AppendResult (interp, "no object parent", NULL);
3809 return ir_tcl_error_exec (interp, argc, argv);
3811 obj = ir_tcl_malloc (sizeof(*obj));
3812 obj->parent = (IrTcl_Obj *) parent_info.clientData;
3814 tabs[0].tab = ir_scan_method_tab;
3818 if (ir_tcl_method (interp, 0, NULL, tabs, NULL) == TCL_ERROR)
3820 Tcl_CreateCommand (interp, argv[1], ir_scan_obj_method,
3821 (ClientData) obj, ir_scan_obj_delete);
3825 /* ------------------------------------------------------- */
3828 * ir_log_init_proc: set yaz log level
3830 static int ir_log_init_proc (ClientData clientData, Tcl_Interp *interp,
3831 int argc, char **argv)
3833 if (argc <= 1 || argc > 4)
3835 Tcl_AppendResult (interp, wrongArgs, *argv,
3836 " ?level ?prefix ?filename\"", NULL);
3840 yaz_log_init (yaz_log_mask_str (argv[1]), "", NULL);
3842 yaz_log_init (yaz_log_mask_str (argv[1]), argv[2], NULL);
3844 yaz_log_init (yaz_log_mask_str (argv[1]), argv[2], argv[3]);
3849 * ir_log_proc: log yaz message
3851 static int ir_log_proc (ClientData clientData, Tcl_Interp *interp,
3852 int argc, char **argv)
3857 Tcl_AppendResult (interp, wrongArgs, *argv,
3858 " level string\"", NULL);
3861 mask = yaz_log_mask_str_x (argv[1], 0);
3862 logf (LOG_DEBUG, "%s", argv[2]);
3868 * ir_version: log ir version
3870 static int ir_version (ClientData clientData, Tcl_Interp *interp,
3871 int argc, char **argv)
3873 Tcl_AppendElement (interp, IR_TCL_VERSION);
3874 Tcl_AppendElement (interp, YAZ_VERSION);
3879 /* ------------------------------------------------------- */
3880 static void ir_initResponse (void *obj, Z_InitResponse *initrs)
3884 p->initResult = *initrs->result ? 1 : 0;
3885 if (!*initrs->result)
3886 logf (LOG_DEBUG, "Connection rejected by target");
3888 logf (LOG_DEBUG, "Connection accepted by target");
3890 get_referenceId (&p->set_inher.referenceId, initrs->referenceId);
3892 xfree (p->targetImplementationId);
3893 ir_tcl_strdup (p->interp, &p->targetImplementationId,
3894 initrs->implementationId);
3895 xfree (p->targetImplementationName);
3896 ir_tcl_strdup (p->interp, &p->targetImplementationName,
3897 initrs->implementationName);
3898 xfree (p->targetImplementationVersion);
3899 ir_tcl_strdup (p->interp, &p->targetImplementationVersion,
3900 initrs->implementationVersion);
3902 p->maximumRecordSize = *initrs->maximumRecordSize;
3903 p->preferredMessageSize = *initrs->preferredMessageSize;
3905 memcpy (&p->options, initrs->options, sizeof(initrs->options));
3906 memcpy (&p->protocolVersion, initrs->protocolVersion,
3907 sizeof(initrs->protocolVersion));
3908 xfree (p->userInformationField);
3909 p->userInformationField = NULL;
3910 if (initrs->userInformationField)
3914 if (initrs->userInformationField->which == ODR_EXTERNAL_octet &&
3915 (p->userInformationField =
3916 ir_tcl_malloc ((len =
3917 initrs->userInformationField->
3918 u.octet_aligned->len) +1)))
3920 memcpy (p->userInformationField,
3921 initrs->userInformationField->u.octet_aligned->buf,
3923 (p->userInformationField)[len] = '\0';
3928 static void ir_deleteDiags (IrTcl_Diagnostic **dst_list, int *dst_num)
3931 for (i = 0; i<*dst_num; i++)
3932 xfree ((*dst_list)[i].addinfo);
3938 static void ir_handleDiags (IrTcl_Diagnostic **dst_list, int *dst_num,
3939 Z_DiagRec **list, int num)
3942 char *addinfo = NULL;
3945 *dst_list = ir_tcl_malloc (sizeof(**dst_list) * num);
3946 for (i = 0; i<num; i++)
3949 switch (list[i]->which)
3951 case Z_DiagRec_defaultFormat:
3952 (*dst_list)[i].condition = *list[i]->u.defaultFormat->condition;
3954 switch (list[i]->u.defaultFormat->which)
3956 case Z_DefaultDiagFormat_v2Addinfo:
3957 addinfo = list[i]->u.defaultFormat->u.v2Addinfo;
3959 case Z_DefaultDiagFormat_v3Addinfo:
3960 addinfo = list[i]->u.defaultFormat->u.v3Addinfo;
3964 addinfo = list[i]->u.defaultFormat->addinfo;
3967 ((*dst_list)[i].addinfo = ir_tcl_malloc (strlen(addinfo)+1)))
3968 strcpy ((*dst_list)[i].addinfo, addinfo);
3969 cp = diagbib1_str ((*dst_list)[i].condition);
3970 logf (LOG_DEBUG, "Diag %d %s %s", (*dst_list)[i].condition,
3971 cp ? cp : "", addinfo ? addinfo : "");
3974 (*dst_list)[i].addinfo = NULL;
3975 (*dst_list)[i].condition = 0;
3980 static void ir_handleDBRecord (IrTcl_Obj *p, IrTcl_RecordList *rl,
3983 struct oident *ident;
3984 Z_ext_typeent *etype;
3986 logf (LOG_DEBUG, "handleDBRecord size=%d", oe->u.octet_aligned->len);
3987 rl->u.dbrec.size = oe->u.octet_aligned->len;
3988 rl->u.dbrec.buf = NULL;
3990 if ((ident = oid_getentbyoid (oe->direct_reference)))
3991 rl->u.dbrec.type = ident->value;
3993 rl->u.dbrec.type = VAL_USMARC;
3995 if (ident && (oe->which == Z_External_single ||
3996 oe->which == Z_External_octet)
3997 && (etype = z_ext_getentbyref (ident->value)))
4001 odr_setbuf (p->odr_in, (char*) oe->u.octet_aligned->buf,
4002 oe->u.octet_aligned->len, 0);
4003 if (!(*etype->fun)(p->odr_in, (char **) &rr, 0, 0))
4005 rl->u.dbrec.type = VAL_NONE;
4008 switch (etype->what)
4010 case Z_External_sutrs:
4011 logf (LOG_DEBUG, "Z_External_sutrs");
4013 if ((rl->u.dbrec.buf = ir_tcl_malloc (oe->u.sutrs->len+1)))
4015 memcpy (rl->u.dbrec.buf, oe->u.sutrs->buf,
4017 rl->u.dbrec.buf[oe->u.sutrs->len] = '\0';
4019 rl->u.dbrec.size = oe->u.sutrs->len;
4021 case Z_External_grs1:
4022 logf (LOG_DEBUG, "Z_External_grs1");
4024 ir_tcl_grs_mk (oe->u.grs1, &rl->u.dbrec.u.grs1);
4026 case Z_External_explainRecord:
4027 logf (LOG_DEBUG, "Z_External_explainRecord");
4028 if ((rl->u.dbrec.buf = ir_tcl_malloc (rl->u.dbrec.size)))
4030 memcpy (rl->u.dbrec.buf, oe->u.octet_aligned->buf,
4038 if (oe->which == Z_External_octet && rl->u.dbrec.size > 0)
4040 char *buf = (char*) oe->u.octet_aligned->buf;
4041 if ((rl->u.dbrec.buf = ir_tcl_malloc (rl->u.dbrec.size)))
4042 memcpy (rl->u.dbrec.buf, buf, rl->u.dbrec.size);
4044 else if (rl->u.dbrec.type == VAL_SUTRS &&
4045 oe->which == 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 else if (rl->u.dbrec.type == VAL_GRS1 &&
4056 oe->which == Z_External_grs1)
4058 ir_tcl_grs_mk (oe->u.grs1, &rl->u.dbrec.u.grs1);
4063 static void ir_handleZRecords (void *o, Z_Records *zrs, IrTcl_SetObj *setobj,
4064 const char *elements)
4069 IrTcl_RecordList *rl;
4071 setobj->which = zrs->which;
4072 setobj->recordFlag = 1;
4074 ir_deleteDiags (&setobj->nonSurrogateDiagnosticList,
4075 &setobj->nonSurrogateDiagnosticNum);
4076 if (zrs->which == Z_Records_DBOSD)
4078 int num_rec = zrs->u.databaseOrSurDiagnostics->num_records;
4080 if (num_rec != setobj->numberOfRecordsReturned)
4082 logf (LOG_WARN, "numberOfRecordsReturned=%d but num records=%d",
4083 setobj->numberOfRecordsReturned, num_rec);
4084 setobj->numberOfRecordsReturned = num_rec;
4087 for (offset = 0; offset < num_rec; offset++)
4089 Z_NamePlusRecord *znpr = zrs->u.databaseOrSurDiagnostics->
4092 rl = new_IR_record (setobj, setobj->start + offset, znpr->which,
4094 if (rl->which == Z_NamePlusRecord_surrogateDiagnostic)
4095 ir_handleDiags (&rl->u.surrogateDiagnostics.list,
4096 &rl->u.surrogateDiagnostics.num,
4097 &znpr->u.surrogateDiagnostic,
4100 ir_handleDBRecord (p, rl,
4101 (Z_External*) (znpr->u.databaseRecord));
4104 else if (zrs->which == Z_Records_multipleNSD)
4106 logf (LOG_DEBUG, "multipleNonSurrogateDiagnostic %d",
4107 zrs->u.multipleNonSurDiagnostics->num_diagRecs);
4108 setobj->numberOfRecordsReturned = 0;
4109 ir_handleDiags (&setobj->nonSurrogateDiagnosticList,
4110 &setobj->nonSurrogateDiagnosticNum,
4111 zrs->u.multipleNonSurDiagnostics->diagRecs,
4112 zrs->u.multipleNonSurDiagnostics->num_diagRecs);
4117 Z_DiagRec dr, *dr_p = &dr;
4118 dr.which = Z_DiagRec_defaultFormat;
4119 dr.u.defaultFormat = zrs->u.nonSurrogateDiagnostic;
4121 Z_DiagRec *dr_p = zrs->u.nonSurrogateDiagnostic;
4123 logf (LOG_DEBUG, "NonSurrogateDiagnostic");
4125 setobj->numberOfRecordsReturned = 0;
4126 ir_handleDiags (&setobj->nonSurrogateDiagnosticList,
4127 &setobj->nonSurrogateDiagnosticNum,
4132 static char *set_queryExpression (Z_QueryExpression *qe)
4137 if (qe->which == Z_QueryExpression_term)
4139 if (qe->u.term->queryTerm)
4141 Z_Term *term = qe->u.term->queryTerm;
4142 if (term->which == Z_Term_general)
4144 termz = xmalloc (term->u.general->len+1);
4145 memcpy (termz, term->u.general->buf, term->u.general->len);
4146 termz[term->u.general->len] = 0;
4153 static void set_searchResult (Z_OtherInformation *o,
4154 IrTcl_SetObj *setobj)
4159 for (i = 0; i < o->num_elements; i++)
4161 if (o->list[i]->which == Z_OtherInfo_externallyDefinedInfo)
4163 ODR odr = odr_createmem (ODR_DECODE);
4164 Z_External *ext = o->list[i]->information.externallyDefinedInfo;
4165 Z_SearchInfoReport *sr = 0;
4167 if (ext->which == Z_External_single)
4169 odr_setbuf (odr, ext->u.single_ASN1_type->buf,
4170 ext->u.single_ASN1_type->len, 0);
4171 z_SearchInfoReport (odr, &sr, 0, "searchInfo");
4173 if (ext->which == Z_External_searchResult1)
4174 sr = ext->u.searchResult1;
4178 reset_searchResult (setobj);
4180 setobj->searchResult_num = sr->num;
4181 setobj->searchResult_terms =
4182 xmalloc (sr->num * sizeof(*setobj->searchResult_terms));
4183 setobj->searchResult_count =
4184 xmalloc (sr->num * sizeof(*setobj->searchResult_count));
4186 for (j = 0; j < sr->num; j++)
4188 setobj->searchResult_terms[j] =
4189 set_queryExpression (
4190 sr->elements[j]->subqueryExpression);
4191 if (sr->elements[j]->subqueryCount)
4192 setobj->searchResult_count[j] =
4193 *sr->elements[j]->subqueryCount;
4195 setobj->searchResult_count[j] = 0;
4203 static void ir_searchResponse (void *o, Z_SearchResponse *searchrs,
4204 IrTcl_SetObj *setobj)
4206 Z_Records *zrs = searchrs->records;
4208 logf (LOG_DEBUG, "Received search response");
4211 logf (LOG_DEBUG, "Search response, no object!");
4214 setobj->searchStatus = *searchrs->searchStatus;
4215 get_referenceId (&setobj->set_inher.referenceId, searchrs->referenceId);
4216 setobj->resultCount = *searchrs->resultCount;
4217 if (searchrs->presentStatus)
4218 setobj->presentStatus = *searchrs->presentStatus;
4220 setobj->presentStatus = Z_RES_NONE;
4221 if (searchrs->nextResultSetPosition)
4222 setobj->nextResultSetPosition = *searchrs->nextResultSetPosition;
4224 logf (LOG_DEBUG, "status %d hits %d",
4225 setobj->searchStatus, setobj->resultCount);
4226 set_searchResult (searchrs->additionalSearchInfo, setobj);
4230 if (setobj->resultCount <= setobj->set_inher.smallSetUpperBound)
4231 es = setobj->set_inher.smallSetElementSetNames;
4233 es = setobj->set_inher.mediumSetElementSetNames;
4234 setobj->numberOfRecordsReturned = *searchrs->numberOfRecordsReturned;
4235 ir_handleZRecords (o, zrs, setobj, es);
4239 setobj->numberOfRecordsReturned = 0;
4240 setobj->recordFlag = 0;
4245 static void ir_presentResponse (void *o, Z_PresentResponse *presrs,
4246 IrTcl_SetObj *setobj)
4248 Z_Records *zrs = presrs->records;
4250 logf (LOG_DEBUG, "Received present response");
4253 logf (LOG_DEBUG, "Present response, no object!");
4256 setobj->presentStatus = *presrs->presentStatus;
4257 get_referenceId (&setobj->set_inher.referenceId, presrs->referenceId);
4258 setobj->nextResultSetPosition = *presrs->nextResultSetPosition;
4261 setobj->numberOfRecordsReturned = *presrs->numberOfRecordsReturned;
4262 ir_handleZRecords (o, zrs, setobj, setobj->set_inher.elementSetNames);
4266 setobj->numberOfRecordsReturned = 0;
4267 setobj->recordFlag = 0;
4268 logf (LOG_DEBUG, "No records!");
4272 static void ir_scanResponse (void *o, Z_ScanResponse *scanrs,
4273 IrTcl_ScanObj *scanobj)
4277 logf (LOG_DEBUG, "Received scanResponse");
4279 get_referenceId (&p->set_inher.referenceId, scanrs->referenceId);
4280 scanobj->scanStatus = *scanrs->scanStatus;
4281 logf (LOG_DEBUG, "scanStatus=%d", scanobj->scanStatus);
4283 if (scanrs->stepSize)
4284 scanobj->stepSize = *scanrs->stepSize;
4285 logf (LOG_DEBUG, "stepSize=%d", scanobj->stepSize);
4287 scanobj->numberOfEntriesReturned = *scanrs->numberOfEntriesReturned;
4288 logf (LOG_DEBUG, "numberOfEntriesReturned=%d",
4289 scanobj->numberOfEntriesReturned);
4291 if (scanrs->positionOfTerm)
4292 scanobj->positionOfTerm = *scanrs->positionOfTerm;
4294 scanobj->positionOfTerm = -1;
4295 logf (LOG_DEBUG, "positionOfTerm=%d", scanobj->positionOfTerm);
4297 xfree (scanobj->entries);
4298 scanobj->entries = NULL;
4299 scanobj->num_entries = 0;
4300 scanobj->entries_flag = 0;
4302 ir_deleteDiags (&scanobj->nonSurrogateDiagnosticList,
4303 &scanobj->nonSurrogateDiagnosticNum);
4304 if (scanrs->entries)
4309 scanobj->entries_flag = 1;
4310 if (scanrs->entries)
4312 scanobj->num_entries = scanrs->entries->num_entries;
4313 scanobj->entries = ir_tcl_malloc (scanobj->num_entries *
4314 sizeof(*scanobj->entries));
4315 ze = scanrs->entries->entries;
4317 for (i=0; i<scanobj->num_entries; i++, ze++)
4319 scanobj->entries[i].which = (*ze)->which;
4320 switch ((*ze)->which)
4322 case Z_Entry_termInfo:
4323 if ((*ze)->u.termInfo->term->which == Z_Term_general)
4325 int l = (*ze)->u.termInfo->term->u.general->len;
4326 scanobj->entries[i].u.term.buf = ir_tcl_malloc (1+l);
4327 memcpy (scanobj->entries[i].u.term.buf,
4328 (*ze)->u.termInfo->term->u.general->buf,
4330 scanobj->entries[i].u.term.buf[l] = '\0';
4333 scanobj->entries[i].u.term.buf = NULL;
4334 if ((*ze)->u.termInfo->globalOccurrences)
4335 scanobj->entries[i].u.term.globalOccurrences =
4336 *(*ze)->u.termInfo->globalOccurrences;
4338 scanobj->entries[i].u.term.globalOccurrences = 0;
4340 case Z_Entry_surrogateDiagnostic:
4341 ir_handleDiags (&scanobj->entries[i].u.diag.list,
4342 &scanobj->entries[i].u.diag.num,
4343 &(*ze)->u.surrogateDiagnostic,
4348 if (scanrs->entries->nonsurrogateDiagnostics)
4349 ir_handleDiags (&scanobj->nonSurrogateDiagnosticList,
4350 &scanobj->nonSurrogateDiagnosticNum,
4351 scanrs->entries->nonsurrogateDiagnostics,
4352 scanrs->entries->num_nonsurrogateDiagnostics);
4357 static void ir_sortResponse (void *o, Z_SortResponse *sortrs,
4358 IrTcl_SetObj *setobj)
4362 logf (LOG_DEBUG, "Received sortResponse");
4367 purge_IR_records (setobj);
4369 get_referenceId (&p->set_inher.referenceId, sortrs->referenceId);
4371 setobj->sortStatus = *sortrs->sortStatus;
4373 ir_deleteDiags (&setobj->nonSurrogateDiagnosticList,
4374 &setobj->nonSurrogateDiagnosticNum);
4376 if (sortrs->diagnostics)
4377 ir_handleDiags (&setobj->nonSurrogateDiagnosticList,
4378 &setobj->nonSurrogateDiagnosticNum,
4379 sortrs->diagnostics,
4380 sortrs->num_diagnostics);
4382 if (sortrs->diagnostics)
4383 ir_handleDiags (&setobj->nonSurrogateDiagnosticList,
4384 &setobj->nonSurrogateDiagnosticNum,
4385 sortrs->diagnostics->diagRecs,
4386 sortrs->diagnostics->num_diagRecs);
4391 * ir_select_read: handle incoming packages
4393 static void ir_select_read (ClientData clientData)
4395 IrTcl_Obj *p = (IrTcl_Obj *) clientData;
4400 Tcl_CmdInfo cmd_info;
4401 const char *apdu_call;
4403 logf(LOG_DEBUG, "Read handler fd=%d", cs_fileno(p->cs_link));
4404 if (p->state == IR_TCL_R_Connecting)
4406 logf(LOG_DEBUG, "read: connect");
4407 r = cs_rcvconnect (p->cs_link);
4410 logf (LOG_WARN, "cs_rcvconnect returned 1");
4413 p->state = IR_TCL_R_Idle;
4415 ir_select_remove_write (cs_fileno (p->cs_link), p);
4418 logf (LOG_DEBUG, "cs_rcvconnect error");
4419 ir_tcl_disconnect (p);
4422 p->failInfo = IR_TCL_FAIL_CONNECT;
4423 ir_tcl_eval (p->interp, p->failback);
4425 ir_obj_delete ((ClientData) p);
4429 ir_tcl_eval (p->interp, p->callback);
4430 if (p->ref_count == 2 && p->cs_link && p->request_queue
4431 && p->state == IR_TCL_R_Idle)
4432 ir_tcl_send_q (p, p->request_queue, "x");
4433 ir_obj_delete ((ClientData) p);
4438 p->state = IR_TCL_R_Reading;
4440 /* read incoming APDU */
4441 if ((r=cs_get (p->cs_link, &p->buf_in, &p->len_in)) == 1)
4443 logf(LOG_DEBUG, "PDU Fraction read");
4446 /* signal one more use of ir object - callbacks must not
4447 release the ir memory (p pointer) */
4451 logf (LOG_DEBUG, "cs_get failed, code %d", r);
4452 ir_tcl_disconnect (p);
4455 p->failInfo = IR_TCL_FAIL_READ;
4456 ir_tcl_eval (p->interp, p->failback);
4458 /* release ir object now if callback deleted it */
4459 ir_obj_delete ((ClientData) p);
4462 /* got complete APDU. Now decode */
4465 odr_setbuf (p->odr_in, p->buf_in, r, 0);
4466 logf (LOG_DEBUG, "cs_get ok, total size %d", r);
4467 if (!z_APDU (p->odr_in, &apdu, 0, 0))
4469 logf (LOG_DEBUG, "cs_get failed: %s",
4470 odr_errmsg (odr_geterror (p->odr_in)));
4471 ir_tcl_disconnect (p);
4474 p->failInfo = IR_TCL_FAIL_IN_APDU;
4475 p->apduOffset = odr_offset (p->odr_in);
4476 ir_tcl_eval (p->interp, p->failback);
4478 /* release ir object now if failback deleted it */
4479 ir_obj_delete ((ClientData) p);
4482 /* handle APDU and invoke callback */
4483 rq = p->request_queue;
4486 logf (LOG_FATAL, "Internal error. No queue entry");
4489 object_name = rq->object_name;
4490 logf (LOG_DEBUG, "Object %s", object_name);
4492 if (Tcl_GetCommandInfo (p->interp, object_name, &cmd_info))
4496 case Z_APDU_initResponse:
4497 p->eventType = "init";
4498 ir_initResponse (p, apdu->u.initResponse);
4499 apdu_call = p->initResponse;
4501 case Z_APDU_searchResponse:
4502 p->eventType = "search";
4503 ir_searchResponse (p, apdu->u.searchResponse,
4504 (IrTcl_SetObj *) cmd_info.clientData);
4505 apdu_call = ((IrTcl_SetObj *)
4506 cmd_info.clientData)->searchResponse;
4508 case Z_APDU_presentResponse:
4509 p->eventType = "present";
4510 ir_presentResponse (p, apdu->u.presentResponse,
4511 (IrTcl_SetObj *) cmd_info.clientData);
4512 apdu_call = ((IrTcl_SetObj *)
4513 cmd_info.clientData)->presentResponse;
4515 case Z_APDU_scanResponse:
4516 p->eventType = "scan";
4517 ir_scanResponse (p, apdu->u.scanResponse,
4518 (IrTcl_ScanObj *) cmd_info.clientData);
4519 apdu_call = ((IrTcl_ScanObj *)
4520 cmd_info.clientData)->scanResponse;
4522 case Z_APDU_sortResponse:
4523 p->eventType = "sort";
4524 ir_sortResponse (p, apdu->u.sortResponse,
4525 (IrTcl_SetObj *) cmd_info.clientData);
4526 apdu_call = ((IrTcl_SetObj *)
4527 cmd_info.clientData)->sortResponse;
4530 logf (LOG_WARN, "Received unknown APDU type (%d)",
4532 ir_tcl_disconnect (p);
4535 p->failInfo = IR_TCL_FAIL_UNKNOWN_APDU;
4536 ir_tcl_eval (p->interp, p->failback);
4541 p->request_queue = rq->next;
4542 p->state = IR_TCL_R_Idle;
4545 ir_tcl_eval (p->interp, apdu_call);
4546 else if (rq->callback)
4547 ir_tcl_eval (p->interp, rq->callback);
4548 xfree (rq->buf_out);
4549 xfree (rq->callback);
4550 xfree (rq->object_name);
4552 odr_reset (p->odr_in);
4553 if (p->ref_count == 1)
4555 ir_obj_delete ((ClientData) p);
4558 ir_obj_delete ((ClientData) p);
4559 } while (p->cs_link && cs_more (p->cs_link));
4560 if (p->cs_link && p->request_queue && p->state == IR_TCL_R_Idle)
4561 ir_tcl_send_q (p, p->request_queue, "x");
4565 * ir_select_write: handle outgoing packages - not yet written.
4567 static int ir_select_write (ClientData clientData)
4569 IrTcl_Obj *p = (IrTcl_Obj *) clientData;
4573 logf (LOG_DEBUG, "Write handler fd=%d", cs_fileno(p->cs_link));
4574 if (p->state == IR_TCL_R_Connecting)
4576 logf(LOG_DEBUG, "write: connect");
4577 r = cs_rcvconnect (p->cs_link);
4580 logf (LOG_DEBUG, "cs_rcvconnect returned 1");
4583 p->state = IR_TCL_R_Idle;
4585 ir_select_remove_write (cs_fileno (p->cs_link), p);
4588 logf (LOG_DEBUG, "cs_rcvconnect error");
4589 ir_tcl_disconnect (p);
4592 p->failInfo = IR_TCL_FAIL_CONNECT;
4593 ir_tcl_eval (p->interp, p->failback);
4595 ir_obj_delete ((ClientData) p);
4599 ir_tcl_eval (p->interp, p->callback);
4600 ir_obj_delete ((ClientData) p);
4603 rq = p->request_queue;
4604 if (!rq || !rq->buf_out)
4607 if ((r=cs_put (p->cs_link, rq->buf_out, rq->len_out)) < 0)
4609 logf (LOG_DEBUG, "cs_put write fail");
4611 xfree (rq->buf_out);
4613 ir_tcl_disconnect (p);
4616 p->failInfo = IR_TCL_FAIL_WRITE;
4617 ir_tcl_eval (p->interp, p->failback);
4619 ir_obj_delete ((ClientData) p);
4621 else if (r == 0) /* remove select bit */
4623 logf (LOG_DEBUG, "Write completed");
4624 p->state = IR_TCL_R_Waiting;
4625 ir_select_remove_write (cs_fileno (p->cs_link), p);
4626 xfree (rq->buf_out);
4632 static void ir_select_notify (ClientData clientData, int r, int w, int e)
4636 if (!ir_select_write (clientData) && r)
4637 ir_select_read (clientData);
4641 ir_select_read (clientData);
4645 /*----------------------------------------------------------- */
4649 * This wrapper function is used by Windows to invoke the
4650 * initialization code for the DLL. If we are compiling
4651 * with Visual C++, this routine will be renamed to DllMain.
4663 DllEntryPoint(hInst, reason, reserved)
4664 HINSTANCE hInst; /* Library instance handle. */
4665 DWORD reason; /* Reason this function is being called. */
4666 LPVOID reserved; /* Not used. */
4672 /* ------------------------------------------------------- */
4674 * Irtcl_init: Registration of TCL commands.
4676 EXPORT (int,Irtcl_Init) (Tcl_Interp *interp)
4678 Tcl_CreateCommand (interp, "ir", ir_obj_mk, (ClientData) NULL,
4679 (Tcl_CmdDeleteProc *) NULL);
4680 Tcl_CreateCommand (interp, "ir-set", ir_set_obj_mk,
4681 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
4682 Tcl_CreateCommand (interp, "ir-scan", ir_scan_obj_mk,
4683 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
4684 Tcl_CreateCommand (interp, "ir-log-init", ir_log_init_proc,
4685 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
4686 Tcl_CreateCommand (interp, "ir-log", ir_log_proc,
4687 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
4688 Tcl_CreateCommand (interp, "ir-version", ir_version, (ClientData) NULL,
4689 (Tcl_CmdDeleteProc *) NULL);