2 * IR toolkit for tcl/tk
3 * (c) Index Data 1995-1998
4 * See the file LICENSE for details.
5 * Sebastian Hammer, Adam Dickmeiss
8 * Revision 1.105 1998-04-02 14:31:08 adam
9 * This version works with compiled ASN.1 code.
11 * Revision 1.104 1998/02/27 14:26:07 adam
12 * Changed client so that it still works if target sets numberOfRecords
13 * in response to an illegal value.
15 * Revision 1.103 1997/11/19 11:22:10 adam
16 * Object identifiers can be accessed in GRS-1 records.
18 * Revision 1.102 1997/09/17 12:22:40 adam
19 * Changed to use YAZ version 1.4. The new comstack utility, cs_straddr,
22 * Revision 1.101 1997/09/09 10:19:53 adam
23 * New MSV5.0 port with fewer warnings.
25 * Revision 1.100 1997/05/01 15:04:05 adam
26 * Added ir-log command.
28 * Revision 1.99 1997/04/30 07:24:47 adam
29 * Spell fix of an error message.
31 * Revision 1.98 1997/04/13 18:57:20 adam
32 * Better error reporting and aligned with Tcl/Tk style.
33 * Rework of notifier code with Tcl_File handles.
35 * Revision 1.97 1996/11/14 17:11:07 adam
36 * Added Explain documentaion.
38 * Revision 1.96 1996/10/08 13:02:50 adam
39 * When dealing with records, odr_choice_enable_bias function is used to
40 * prevent decoding of externals.
42 * Revision 1.95 1996/09/13 10:51:49 adam
43 * Bug fix: ir_tcl_select_set called Tcl_GetFile at disconnect.
45 * Revision 1.94 1996/08/21 13:32:53 adam
46 * Implemented saveFile method and extended loadFile method to work with it.
48 * Revision 1.93 1996/08/16 15:07:45 adam
49 * First work on Explain.
51 * Revision 1.92 1996/08/09 15:33:07 adam
52 * Modified the code to use tk4.1/tcl7.5 patch level 1. The time-driven
53 * polling is no longer activated on Windows since asynchrounous I/O works
56 * Revision 1.91 1996/07/03 13:31:11 adam
57 * The xmalloc/xfree functions from YAZ are used to manage memory.
59 * Revision 1.90 1996/06/27 14:21:00 adam
60 * Yet another Windows port.
62 * Revision 1.89 1996/06/11 15:27:15 adam
63 * Event type set to connect a little earlier in the do_connect function.
65 * Revision 1.88 1996/06/03 09:04:22 adam
66 * Changed a few logf calls.
68 * Revision 1.87 1996/05/29 06:37:51 adam
69 * Function ir_tcl_get_grs_r enhanced so that specific elements can be
72 * Revision 1.86 1996/03/20 13:54:04 adam
73 * The Tcl_File structure is only manipulated in the Tk-event interface
76 * Revision 1.85 1996/03/15 11:15:48 adam
77 * Modified to use new prototypes for p_query_rpn and p_query_scan.
79 * Revision 1.84 1996/03/07 12:42:49 adam
80 * Better logging when callback is invoked.
82 * Revision 1.83 1996/03/05 09:21:09 adam
83 * Bug fix: memory used by GRS records wasn't freed.
84 * Rewrote some of the error handling code - the connection is always
85 * closed before failback is called.
86 * If failback is defined the send APDU methods (init, search, ...) will
87 * return OK but invoke failback (as is the case if the write operation
89 * Bug fix: ref_count in assoc object could grow if fraction of PDU was
92 * Revision 1.82 1996/02/29 15:30:21 adam
93 * Export of IrTcl functionality to extensions.
95 * Revision 1.81 1996/02/26 18:38:32 adam
96 * Work on export of set methods.
98 * Revision 1.80 1996/02/23 17:31:39 adam
99 * More functions made available to the wais tcl extension.
101 * Revision 1.79 1996/02/23 13:41:38 adam
102 * Work on public access to simple ir class system.
104 * Revision 1.78 1996/02/21 10:16:08 adam
105 * Simplified select handling. Only one function ir_tcl_select_set has
106 * to be externally defined.
108 * Revision 1.77 1996/02/20 17:52:58 adam
109 * Uses the YAZ oid system to name record syntax object identifiers.
111 * Revision 1.76 1996/02/20 16:09:51 adam
112 * Bug fix: didn't set element set names stamp correctly on result
113 * set records when element set names were set to the empty string.
115 * Revision 1.75 1996/02/19 15:41:53 adam
116 * Better log messages.
117 * Minor improvement of connect method.
119 * Revision 1.74 1996/02/05 17:58:03 adam
120 * Ported ir-tcl to use the beta releases of tcl7.5/tk4.1.
122 * Revision 1.73 1996/01/29 11:35:19 adam
123 * Bug fix: cs_type member renamed to comstackType to avoid conflict with
124 * cs_type macro defined by YAZ.
126 * Revision 1.72 1996/01/19 17:45:34 quinn
127 * Added debugging output
129 * Revision 1.71 1996/01/19 16:22:38 adam
130 * New method: apduDump - returns information about last incoming APDU.
132 * Revision 1.70 1996/01/10 09:18:34 adam
133 * PDU specific callbacks implemented: initRespnse, searchResponse,
134 * presentResponse and scanResponse.
135 * Bug fix in the command line shell (tclmain.c) - discovered on OSF/1.
137 * Revision 1.69 1996/01/04 16:12:12 adam
138 * Setting PDUType renamed to eventType.
140 * Revision 1.68 1996/01/04 11:05:22 adam
141 * New setting: PDUType - returns type of last PDU returned from the target.
142 * Fixed a bug in configure/Makefile.
144 * Revision 1.67 1996/01/03 09:00:51 adam
145 * Updated to use new version of Yaz (names changed to avoid C++ conflict).
147 * Revision 1.66 1995/11/28 17:26:39 adam
148 * Removed Carriage return from ir-tcl.c!
149 * Removed misc. debug logs.
151 * Revision 1.65 1995/11/28 13:53:00 quinn
154 * Revision 1.64 1995/11/13 15:39:18 adam
155 * Bug fix: {small,medium}SetElementSetNames weren't set correctly.
156 * Bug fix: idAuthentication weren't set correctly.
158 * Revision 1.63 1995/11/13 09:55:39 adam
159 * Multiple records at a position in a result-set with differnt
162 * Revision 1.62 1995/10/18 17:20:33 adam
163 * Work on target setup in client.tcl.
165 * Revision 1.61 1995/10/18 16:42:42 adam
166 * New settings: smallSetElementSetNames and mediumSetElementSetNames.
168 * Revision 1.60 1995/10/18 15:43:31 adam
169 * In search: mediumSetElementSetNames and smallSetElementSetNames are
170 * set to elementSetNames.
172 * Revision 1.59 1995/10/17 12:18:58 adam
173 * Bug fix: when target connection closed, the connection was not
174 * properly reestablished.
176 * Revision 1.58 1995/10/16 17:00:55 adam
177 * New setting: elementSetNames.
178 * Various client improvements. Medium presentation format looks better.
180 * Revision 1.57 1995/09/21 13:11:51 adam
181 * Support of dynamic loading.
182 * Test script uses load command if necessary.
184 * Revision 1.56 1995/08/29 15:30:14 adam
185 * Work on GRS records.
187 * Revision 1.55 1995/08/28 09:43:25 adam
188 * Minor changes. configure only searches for yaz beta 3 and versions after
191 * Revision 1.54 1995/08/24 12:25:16 adam
192 * Modified to work with yaz 1.0b3.
194 * Revision 1.53 1995/08/04 12:49:26 adam
195 * Bug fix: reading uninitialized variable p.
197 * Revision 1.52 1995/08/04 11:32:38 adam
198 * More work on output queue. Memory related routines moved
201 * Revision 1.51 1995/08/03 13:22:54 adam
204 * Revision 1.50 1995/07/20 08:09:49 adam
205 * client.tcl: Targets removed from hotTargets list when targets
206 * are removed/modified.
207 * ir-tcl.c: More work on triggerResourceControl.
209 * Revision 1.49 1995/06/30 12:39:21 adam
210 * Bug fix: loadFile didn't set record type.
211 * The MARC routines are a little less strict in the interpretation.
212 * Script display.tcl replaces the old marc.tcl.
213 * New interactive script: shell.tcl.
215 * Revision 1.48 1995/06/27 19:03:50 adam
216 * Bug fix in do_present in ir-tcl.c: p->set_child member weren't set.
217 * nextResultSetPosition used instead of setOffset.
219 * Revision 1.47 1995/06/25 10:25:04 adam
220 * Working on triggerResourceControl. Description of compile/install
221 * procedure moved to ir-tcl.sgml.
223 * Revision 1.46 1995/06/22 13:15:06 adam
224 * Feature: SUTRS. Setting getSutrs implemented.
225 * Work on display formats.
226 * Preferred record syntax can be set by the user.
228 * Revision 1.45 1995/06/20 08:07:30 adam
229 * New setting: failInfo.
230 * Working on better cancel mechanism.
232 * Revision 1.44 1995/06/19 17:01:20 adam
235 * Revision 1.43 1995/06/19 13:06:08 adam
236 * New define: IR_TCL_VERSION.
238 * Revision 1.42 1995/06/19 08:08:52 adam
239 * client.tcl: hotTargets now contain both database and target name.
240 * ir-tcl.c: setting protocol edited. Errors in callbacks are logged
241 * by logf(LOG_WARN, ...) calls.
243 * Revision 1.41 1995/06/16 12:28:16 adam
244 * Implemented preferredRecordSyntax.
245 * Minor changes in diagnostic handling.
246 * Record list deleted when connection closes.
248 * Revision 1.40 1995/06/14 13:37:18 adam
249 * Setting recordType implemented.
250 * Setting implementationVersion implemented.
251 * Settings implementationId / implementationName edited.
253 * Revision 1.39 1995/06/08 10:26:32 adam
254 * Bug fix in ir_strdup.
256 * Revision 1.38 1995/06/01 16:36:47 adam
257 * About buttons. Minor bug fixes.
259 * Revision 1.37 1995/06/01 07:31:20 adam
260 * Rename of many typedefs -> IrTcl_...
262 * Revision 1.36 1995/05/31 13:09:59 adam
263 * Client searches/presents may be interrupted.
264 * New moving book-logo.
266 * Revision 1.35 1995/05/31 08:36:33 adam
267 * Bug fix in client.tcl: didn't save options on clientrc.tcl.
268 * New method: referenceId. More work on scan.
270 * Revision 1.34 1995/05/29 10:33:42 adam
271 * README and rename of startup script.
273 * Revision 1.33 1995/05/29 09:15:11 quinn
274 * Changed CS_SR to PROTO_SR, etc.
276 * Revision 1.32 1995/05/29 08:44:16 adam
277 * Work on delete of objects.
279 * Revision 1.31 1995/05/26 11:44:10 adam
280 * Bugs fixed. More work on MARC utilities and queries. Test
281 * client is up-to-date again.
283 * Revision 1.30 1995/05/26 08:54:11 adam
284 * New MARC utilities. Uses prefix query.
286 * Revision 1.29 1995/05/24 14:10:22 adam
287 * Work on idAuthentication, protocolVersion and options.
289 * Revision 1.28 1995/05/23 15:34:48 adam
290 * Many new settings, userInformationField, smallSetUpperBound, etc.
291 * A number of settings are inherited when ir-set is executed.
292 * This version is incompatible with the graphical test client (client.tcl).
294 * Revision 1.27 1995/05/11 15:34:47 adam
295 * Scan request changed a bit. This version works with RLG.
297 * Revision 1.26 1995/04/18 16:11:51 adam
298 * First version of graphical Scan. Some work on query-by-form.
300 * Revision 1.25 1995/04/17 09:37:17 adam
301 * Further development of scan.
303 * Revision 1.24 1995/04/11 14:16:42 adam
304 * Further work on scan. Response works. Entries aren't saved yet.
306 * Revision 1.23 1995/04/10 10:50:27 adam
307 * Result-set name defaults to suffix of ir-set name.
308 * Started working on scan. Not finished at this point.
310 * Revision 1.22 1995/03/31 10:43:03 adam
311 * More robust when getting bad MARC records.
313 * Revision 1.21 1995/03/31 08:56:37 adam
314 * New button "Search".
316 * Revision 1.20 1995/03/29 16:07:09 adam
317 * Bug fix: Didn't use setName in present request.
319 * Revision 1.19 1995/03/28 12:45:23 adam
320 * New ir method failback: called on disconnect/protocol error.
321 * New ir set/get method: protocol: SR / Z3950.
322 * Simple popup and disconnect when failback is invoked.
324 * Revision 1.18 1995/03/21 15:50:12 adam
327 * Revision 1.17 1995/03/21 13:41:03 adam
328 * Comstack cs_create not used too often. Non-blocking connect.
330 * Revision 1.16 1995/03/21 08:26:06 adam
331 * New method, setName, to specify the result set name (other than Default).
332 * New method, responseStatus, which returns diagnostic info, if any, after
333 * present response / search response.
335 * Revision 1.15 1995/03/20 15:24:07 adam
336 * Diagnostic records saved on searchResponse.
338 * Revision 1.14 1995/03/20 08:53:22 adam
339 * Event loop in tclmain.c rewritten. New method searchStatus.
341 * Revision 1.13 1995/03/17 18:26:17 adam
342 * Non-blocking i/o used now. Database names popup as cascade items.
344 * Revision 1.12 1995/03/17 15:45:00 adam
345 * Improved target/database setup.
347 * Revision 1.11 1995/03/16 17:54:03 adam
348 * Minor changes really.
350 * Revision 1.10 1995/03/15 16:14:50 adam
351 * Blocking arg in cs_create changed.
353 * Revision 1.9 1995/03/15 13:59:24 adam
356 * Revision 1.8 1995/03/15 08:25:16 adam
357 * New method presentStatus to check for error on present. Misc. cleanup
358 * of IrTcl_RecordList manipulations. Full MARC record presentation in
361 * Revision 1.7 1995/03/14 17:32:29 adam
362 * Presentation of full Marc record in popup window.
364 * Revision 1.6 1995/03/12 19:31:55 adam
365 * Pattern matching implemented when retrieving MARC records. More
366 * diagnostic functions.
368 * Revision 1.5 1995/03/10 18:00:15 adam
369 * Actual presentation in line-by-line format. RPN query support.
371 * Revision 1.4 1995/03/09 16:15:08 adam
372 * First presentRequest attempts. Hot-target list.
390 #if defined(__WIN32__)
391 # define WIN32_LEAN_AND_MEAN
392 # include <windows.h>
393 # undef WIN32_LEAN_AND_MEAN
396 * VC++ has an alternate entry point called DllMain, so we need to rename
400 # if defined(_MSC_VER)
401 # define EXPORT(a,b) __declspec(dllexport) a b
402 # define DllEntryPoint DllMain
404 # if defined(__BORLANDC__)
405 # define EXPORT(a,b) a _export b
407 # define EXPORT(a,b) a b
411 # define EXPORT(a,b) a b
414 static char *wrongArgs = "wrong # args: should be \"";
416 static int ir_tcl_error_exec (Tcl_Interp *interp, int argc, char **argv)
419 Tcl_AppendResult (interp, " while executing ", NULL);
420 for (i = 0; i<argc; i++)
421 Tcl_AppendResult (interp, (i ? " " : "\""), argv[i], NULL);
422 Tcl_AppendResult (interp, "\"", NULL);
427 static void ir_deleteDiags (IrTcl_Diagnostic **dst_list, int *dst_num);
429 static void ir_select_notify (ClientData clientData, int r, int w, int e);
431 void ir_select_add (int fd, void *obj)
433 ir_tcl_select_set (ir_select_notify, fd, obj, 1, 0, 0);
436 void ir_select_add_write (int fd, void *obj)
438 ir_tcl_select_set (ir_select_notify, fd, obj, 1, 1, 0);
441 void ir_select_remove (int fd, void *obj)
443 ir_tcl_select_set (NULL, fd, obj, 0, 0, 0);
446 void ir_select_remove_write (int fd, void *obj)
448 ir_tcl_select_set (ir_select_notify, fd, obj, 1, 0, 0);
451 static void delete_IR_record (IrTcl_RecordList *rl)
455 case Z_NamePlusRecord_databaseRecord:
456 switch (rl->u.dbrec.type)
459 ir_tcl_grs_del (&rl->u.dbrec.u.grs1);
464 xfree (rl->u.dbrec.buf);
465 rl->u.dbrec.buf = NULL;
467 case Z_NamePlusRecord_surrogateDiagnostic:
468 ir_deleteDiags (&rl->u.surrogateDiagnostics.list,
469 &rl->u.surrogateDiagnostics.num);
472 xfree (rl->elements);
475 static IrTcl_RecordList *new_IR_record (IrTcl_SetObj *setobj,
477 const char *elements)
479 IrTcl_RecordList *rl;
481 if (elements && !*elements)
483 for (rl = setobj->record_list; rl; rl = rl->next)
485 if (no == rl->no && (!rl->elements || !elements ||
486 !strcmp(elements, rl->elements)))
488 delete_IR_record (rl);
494 rl = ir_tcl_malloc (sizeof(*rl));
495 rl->next = setobj->record_list;
497 setobj->record_list = rl;
500 ir_tcl_strdup (NULL, &rl->elements, elements);
507 int ir_tcl_eval (Tcl_Interp *interp, const char *command)
509 char *tmp = ir_tcl_malloc (strlen(command)+1);
512 logf (LOG_DEBUG, "Invoking %.23s ...", command);
513 strcpy (tmp, command);
514 r = Tcl_Eval (interp, tmp);
517 const char *errorInfo = Tcl_GetVar (interp, "errorInfo", 0);
518 logf (LOG_WARN, "Tcl error in line %d: %s\n%s", interp->errorLine,
519 interp->result, errorInfo ? errorInfo : "<null>");
521 Tcl_FreeResult (interp);
527 * IrTcl_getRecordSyntaxStr: Return record syntax name of object id
529 static char *IrTcl_getRecordSyntaxStr (enum oid_value value)
532 struct oident ent, *entp;
534 ent.proto = PROTO_Z3950;
535 ent.oclass = CLASS_RECSYN;
538 o = oid_getoidbyent (&ent);
539 entp = oid_getentbyoid (o);
547 * IrTcl_getRecordSyntaxVal: Return record syntax value of string
549 static enum oid_value IrTcl_getRecordSyntaxVal (const char *name)
551 return oid_getvalbyname (name);
554 static IrTcl_RecordList *find_IR_record (IrTcl_SetObj *setobj, int no)
556 IrTcl_RecordList *rl;
558 for (rl = setobj->record_list; rl; rl = rl->next)
560 (!setobj->recordElements || !rl->elements ||
561 !strcmp (setobj->recordElements, rl->elements)))
566 static void delete_IR_records (IrTcl_SetObj *setobj)
568 IrTcl_RecordList *rl, *rl1;
570 for (rl = setobj->record_list; rl; rl = rl1)
572 delete_IR_record (rl);
576 setobj->record_list = NULL;
580 * ir_tcl_get_set_int: Set/get integer value
582 int ir_tcl_get_set_int (int *val, Tcl_Interp *interp, int argc, char **argv)
588 if (Tcl_GetInt (interp, argv[2], val)==TCL_ERROR)
591 sprintf (buf, "%d", *val);
592 Tcl_AppendResult (interp, buf, NULL);
598 * ir_tcl_method_error
600 int ir_tcl_method_error (Tcl_Interp *interp, int argc, char **argv,
603 IrTcl_Methods *tab_i = tab;
606 Tcl_AppendResult (interp, "bad method: \"", *argv, " ", argv[1],
607 "\"\nmethod should be of:", NULL);
608 for (tab_i = tab; tab_i->tab; tab_i++)
609 for (t = tab_i->tab; t->name; t++)
610 Tcl_AppendResult (interp, " ", t->name, NULL);
615 * ir_tcl_method: Search for method in table and invoke method handler
617 int ir_tcl_method (Tcl_Interp *interp, int argc, char **argv,
618 IrTcl_Methods *tab, int *ret)
620 IrTcl_Methods *tab_i = tab;
623 for (tab_i = tab; tab_i->tab; tab_i++)
624 for (t = tab_i->tab; t->name; t++)
627 if ((*t->method)(tab_i->obj, interp, argc, argv) == TCL_ERROR)
631 if (!strcmp (t->name, argv[1]))
633 *ret = (*t->method)(tab_i->obj, interp, argc, argv);
644 * ir_tcl_named_bits: get/set named bits
646 int ir_tcl_named_bits (struct ir_named_entry *tab, Odr_bitmask *ob,
647 Tcl_Interp *interp, int argc, char **argv)
649 struct ir_named_entry *ti;
654 for (no = 0; no < argc; no++)
656 for (ti = tab; ti->name; ti++)
657 if (!strcmp (argv[no], ti->name))
659 ODR_MASK_SET (ob, ti->pos);
664 Tcl_AppendResult (interp, "bad bit mask ", argv[no], NULL);
665 return ir_tcl_error_exec (interp, argc, argv);
670 for (ti = tab; ti->name; ti++)
671 if (ODR_MASK_GET (ob, ti->pos))
672 Tcl_AppendElement (interp, ti->name);
676 static void set_referenceId (ODR o, Z_ReferenceId **dst, const char *src)
682 *dst = odr_malloc (o, sizeof(**dst));
683 (*dst)->size = (*dst)->len = strlen(src);
684 (*dst)->buf = odr_malloc (o, (*dst)->len);
685 memcpy ((*dst)->buf, src, (*dst)->len);
689 static void get_referenceId (char **dst, Z_ReferenceId *src)
697 *dst = ir_tcl_malloc (src->len+1);
698 memcpy (*dst, src->buf, src->len);
699 (*dst)[src->len] = '\0';
702 /* ------------------------------------------------------- */
705 * do_init_request: init method on IR object
707 static int do_init_request (void *obj, Tcl_Interp *interp,
708 int argc, char **argv)
716 logf (LOG_DEBUG, "init %s", *argv);
719 Tcl_AppendResult (interp, "not connected", NULL);
720 return ir_tcl_error_exec (interp, argc, argv);
722 apdu = zget_APDU (p->odr_out, Z_APDU_initRequest);
723 req = apdu->u.initRequest;
725 set_referenceId (p->odr_out, &req->referenceId, p->set_inher.referenceId);
726 req->options = &p->options;
727 req->protocolVersion = &p->protocolVersion;
728 req->preferredMessageSize = &p->preferredMessageSize;
729 req->maximumRecordSize = &p->maximumRecordSize;
731 if (p->idAuthenticationGroupId)
733 Z_IdPass *pass = odr_malloc (p->odr_out, sizeof(*pass));
734 Z_IdAuthentication *auth = odr_malloc (p->odr_out, sizeof(*auth));
736 logf (LOG_DEBUG, "using pass authentication");
738 auth->which = Z_IdAuthentication_idPass;
739 auth->u.idPass = pass;
740 if (p->idAuthenticationGroupId && *p->idAuthenticationGroupId)
741 pass->groupId = p->idAuthenticationGroupId;
743 pass->groupId = NULL;
744 if (p->idAuthenticationUserId && *p->idAuthenticationUserId)
745 pass->userId = p->idAuthenticationUserId;
748 if (p->idAuthenticationPassword && *p->idAuthenticationPassword)
749 pass->password = p->idAuthenticationPassword;
751 pass->password = NULL;
752 req->idAuthentication = auth;
754 else if (!p->idAuthenticationOpen || !*p->idAuthenticationOpen)
755 req->idAuthentication = NULL;
758 Z_IdAuthentication *auth = odr_malloc (p->odr_out, sizeof(*auth));
760 logf (LOG_DEBUG, "using open authentication");
761 auth->which = Z_IdAuthentication_open;
762 auth->u.open = p->idAuthenticationOpen;
763 req->idAuthentication = auth;
765 req->implementationId = p->implementationId;
766 req->implementationName = p->implementationName;
767 req->implementationVersion = p->implementationVersion;
768 req->userInformationField = 0;
770 return ir_tcl_send_APDU (interp, p, apdu, "init", *argv);
774 * do_protocolVersion: Set protocol Version
776 static int do_protocolVersion (void *obj, Tcl_Interp *interp,
777 int argc, char **argv)
785 ODR_MASK_ZERO (&p->protocolVersion);
786 ODR_MASK_SET (&p->protocolVersion, 0);
787 ODR_MASK_SET (&p->protocolVersion, 1);
792 if (Tcl_GetInt (interp, argv[2], &version)==TCL_ERROR)
794 ODR_MASK_ZERO (&p->protocolVersion);
795 for (i = 0; i<version; i++)
796 ODR_MASK_SET (&p->protocolVersion, i);
798 for (i = 4; --i >= 0; )
799 if (ODR_MASK_GET (&p->protocolVersion, i))
801 sprintf (buf, "%d", i+1);
802 interp->result = buf;
807 * do_options: Set options
809 static int do_options (void *obj, Tcl_Interp *interp,
810 int argc, char **argv)
812 static struct ir_named_entry options_tab[] = {
816 { "resourceReport", 3 },
817 { "triggerResourceCtrl", 4},
818 { "resourceCtrl", 5},
822 { "extendedServices", 10},
823 { "level-1Segmentation", 11},
824 { "level-2Segmentation", 12},
825 { "concurrentOperations", 13},
826 { "namedResultSets", 14},
833 ODR_MASK_ZERO (&p->options);
834 ODR_MASK_SET (&p->options, 0);
835 ODR_MASK_SET (&p->options, 1);
836 ODR_MASK_SET (&p->options, 4);
837 ODR_MASK_SET (&p->options, 7);
838 ODR_MASK_SET (&p->options, 14);
841 return ir_tcl_named_bits (options_tab, &p->options, interp, argc-2, argv+2);
845 * do_apduInfo: Get APDU information
847 static int do_apduInfo (void *obj, Tcl_Interp *interp, int argc, char **argv)
855 sprintf (buf, "%d", p->apduLen);
856 Tcl_AppendElement (interp, buf);
857 sprintf (buf, "%d", p->apduOffset);
858 Tcl_AppendElement (interp, buf);
861 Tcl_AppendElement (interp, "");
864 apduf = fopen ("apdu.tmp", "w");
867 Tcl_AppendElement (interp, "");
870 odr_dumpBER (apduf, p->buf_in, p->apduLen);
872 if (!(apduf = fopen ("apdu.tmp", "r")))
873 Tcl_AppendElement (interp, "");
878 Tcl_AppendResult (interp, " {", NULL);
879 while ((c = getc (apduf)) != EOF)
883 Tcl_AppendResult (interp, buf, NULL);
886 Tcl_AppendResult (interp, "}", NULL);
893 * do_failInfo: Get fail information
895 static int do_failInfo (void *obj, Tcl_Interp *interp, int argc, char **argv)
905 sprintf (buf, "%d", p->failInfo);
911 case IR_TCL_FAIL_CONNECT:
912 cp = "connect failed";
914 case IR_TCL_FAIL_READ:
915 cp = "connection closed";
917 case IR_TCL_FAIL_WRITE:
918 cp = "connection closed";
920 case IR_TCL_FAIL_IN_APDU:
921 cp = "failed to decode incoming APDU";
923 case IR_TCL_FAIL_UNKNOWN_APDU:
929 Tcl_AppendElement (interp, buf);
930 Tcl_AppendElement (interp, cp);
935 * do_preferredMessageSize: Set/get preferred message size
937 static int do_preferredMessageSize (void *obj, Tcl_Interp *interp,
938 int argc, char **argv)
944 p->preferredMessageSize = 30000;
947 return ir_tcl_get_set_int (&p->preferredMessageSize, interp, argc, argv);
951 * do_maximumRecordSize: Set/get maximum record size
953 static int do_maximumRecordSize (void *obj, Tcl_Interp *interp,
954 int argc, char **argv)
960 p->maximumRecordSize = 30000;
963 return ir_tcl_get_set_int (&p->maximumRecordSize, interp, argc, argv);
967 * do_initResult: Get init result
969 static int do_initResult (void *obj, Tcl_Interp *interp,
970 int argc, char **argv)
976 return ir_tcl_get_set_int (&p->initResult, interp, argc, argv);
981 * do_implementationName: Set/get Implementation Name.
983 static int do_implementationName (void *obj, Tcl_Interp *interp,
984 int argc, char **argv)
989 return ir_tcl_strdup (interp, &p->implementationName,
990 "Index Data/IrTcl on YAZ");
992 return ir_tcl_strdel (interp, &p->implementationName);
995 xfree (p->implementationName);
996 if (ir_tcl_strdup (interp, &p->implementationName, argv[2])
1000 Tcl_AppendResult (interp, p->implementationName, (char*) NULL);
1005 * do_implementationId: Get Implementation Id.
1007 static int do_implementationId (void *obj, Tcl_Interp *interp,
1008 int argc, char **argv)
1013 return ir_tcl_strdup (interp, &p->implementationId, "YAZ (id=81)");
1014 else if (argc == -1)
1015 return ir_tcl_strdel (interp, &p->implementationId);
1016 Tcl_AppendResult (interp, p->implementationId, (char*) NULL);
1021 * do_implementationVersion: get Implementation Version.
1023 static int do_implementationVersion (void *obj, Tcl_Interp *interp,
1024 int argc, char **argv)
1029 return ir_tcl_strdup (interp, &p->implementationVersion,
1031 #ifdef IR_TCL_VERSION
1032 " / Irtcl: " IR_TCL_VERSION
1035 else if (argc == -1)
1036 return ir_tcl_strdel (interp, &p->implementationVersion);
1037 Tcl_AppendResult (interp, p->implementationVersion, (char*) NULL);
1042 * do_targetImplementationName: Get Implementation Name of target.
1044 static int do_targetImplementationName (void *obj, Tcl_Interp *interp,
1045 int argc, char **argv)
1051 p->targetImplementationName = NULL;
1054 else if (argc == -1)
1055 return ir_tcl_strdel (interp, &p->targetImplementationName);
1056 Tcl_AppendResult (interp, p->targetImplementationName, (char*) NULL);
1061 * do_targetImplementationId: Get Implementation Id of target
1063 static int do_targetImplementationId (void *obj, Tcl_Interp *interp,
1064 int argc, char **argv)
1070 p->targetImplementationId = NULL;
1073 else if (argc == -1)
1074 return ir_tcl_strdel (interp, &p->targetImplementationId);
1075 Tcl_AppendResult (interp, p->targetImplementationId, (char*) NULL);
1080 * do_targetImplementationVersion: Get Implementation Version of target
1082 static int do_targetImplementationVersion (void *obj, Tcl_Interp *interp,
1083 int argc, char **argv)
1089 p->targetImplementationVersion = NULL;
1092 else if (argc == -1)
1093 return ir_tcl_strdel (interp, &p->targetImplementationVersion);
1094 Tcl_AppendResult (interp, p->targetImplementationVersion, (char*) NULL);
1099 * do_idAuthentication: Set/get id Authentication
1101 static int do_idAuthentication (void *obj, Tcl_Interp *interp,
1102 int argc, char **argv)
1106 if (argc >= 3 || argc == -1)
1108 xfree (p->idAuthenticationOpen);
1109 xfree (p->idAuthenticationGroupId);
1110 xfree (p->idAuthenticationUserId);
1111 xfree (p->idAuthenticationPassword);
1113 if (argc >= 3 || argc <= 0)
1115 p->idAuthenticationOpen = NULL;
1116 p->idAuthenticationGroupId = NULL;
1117 p->idAuthenticationUserId = NULL;
1118 p->idAuthenticationPassword = NULL;
1127 ir_tcl_strdup (interp, &p->idAuthenticationOpen, argv[2])
1134 ir_tcl_strdup (interp, &p->idAuthenticationGroupId, argv[2])
1138 ir_tcl_strdup (interp, &p->idAuthenticationUserId, argv[3])
1142 ir_tcl_strdup (interp, &p->idAuthenticationPassword, argv[4])
1147 if (p->idAuthenticationOpen)
1148 Tcl_AppendElement (interp, p->idAuthenticationOpen);
1149 else if (p->idAuthenticationGroupId)
1151 Tcl_AppendElement (interp, p->idAuthenticationGroupId);
1152 Tcl_AppendElement (interp, p->idAuthenticationUserId);
1153 Tcl_AppendElement (interp, p->idAuthenticationPassword);
1159 * do_connect: connect method on IR object
1161 static int do_connect (void *obj, Tcl_Interp *interp,
1162 int argc, char **argv)
1172 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
1173 " ?hostname?\"", NULL);
1178 Tcl_AppendResult (interp, p->hostname, NULL);
1182 logf (LOG_DEBUG, "connect %s %s", *argv, argv[2]);
1185 Tcl_AppendResult (interp, "already connected", NULL);
1186 return ir_tcl_error_exec (interp, argc, argv);
1188 if (!strcmp (p->comstackType, "tcpip"))
1190 p->cs_link = cs_create (tcpip_type, CS_BLOCK, p->protocol_type);
1191 logf (LOG_DEBUG, "tcp/ip connect %s", argv[2]);
1193 else if (!strcmp (p->comstackType, "mosi"))
1196 p->cs_link = cs_create (mosi_type, CS_BLOCK, p->protocol_type);
1197 logf (LOG_DEBUG, "mosi connect %s", argv[2]);
1199 Tcl_AppendResult (interp, "mosi not supported", NULL);
1200 return ir_tcl_error_exec (interp, argc, argv);
1205 Tcl_AppendResult (interp, "bad comstack type ",
1206 p->comstackType, NULL);
1207 return ir_tcl_error_exec (interp, argc, argv);
1209 if (ir_tcl_strdup (interp, &p->hostname, argv[2]) == TCL_ERROR)
1211 p->eventType = "connect";
1212 addr = cs_straddr (p->cs_link, argv[2]);
1215 ir_tcl_disconnect (p);
1216 Tcl_AppendResult (interp, "cs_straddr fail", NULL);
1217 return ir_tcl_error_exec (interp, argc, argv);
1219 if ((r=cs_connect (p->cs_link, addr)) < 0)
1221 ir_tcl_disconnect (p);
1222 Tcl_AppendResult (interp, "connect fail", NULL);
1223 return ir_tcl_error_exec (interp, argc, argv);
1225 ir_select_add (cs_fileno (p->cs_link), p);
1228 logf (LOG_DEBUG, "connect pending fd=%d", cs_fileno(p->cs_link));
1229 ir_select_add_write (cs_fileno (p->cs_link), p);
1230 p->state = IR_TCL_R_Connecting;
1234 logf (LOG_DEBUG, "connect ok fd=%d", cs_fileno(p->cs_link));
1235 p->state = IR_TCL_R_Idle;
1237 ir_tcl_eval (p->interp, p->callback);
1244 * ir_tcl_disconnect: close connection
1246 void ir_tcl_disconnect (IrTcl_Obj *p)
1250 logf(LOG_DEBUG, "Closing connection to %s", p->hostname);
1251 xfree (p->hostname);
1253 assert (p->cs_link);
1254 ir_select_remove (cs_fileno (p->cs_link), p);
1256 odr_reset (p->odr_in);
1258 #if TCL_MAJOR_VERSION == 8
1259 cs_fileno(p->cs_link) = -1;
1261 cs_close (p->cs_link);
1264 ODR_MASK_ZERO (&p->options);
1265 ODR_MASK_SET (&p->options, 0);
1266 ODR_MASK_SET (&p->options, 1);
1267 ODR_MASK_SET (&p->options, 4);
1268 ODR_MASK_SET (&p->options, 7);
1269 ODR_MASK_SET (&p->options, 14);
1271 ODR_MASK_ZERO (&p->protocolVersion);
1272 ODR_MASK_SET (&p->protocolVersion, 0);
1273 ODR_MASK_SET (&p->protocolVersion, 1);
1276 assert (!p->cs_link);
1280 * do_disconnect: disconnect method on IR object
1282 static int do_disconnect (void *obj, Tcl_Interp *interp,
1283 int argc, char **argv)
1289 p->state = IR_TCL_R_Idle;
1290 p->eventType = NULL;
1295 ir_tcl_disconnect (p);
1300 * do_comstack: Set/get comstack method on IR object
1302 static int do_comstack (void *o, Tcl_Interp *interp,
1303 int argc, char **argv)
1308 return ir_tcl_strdup (interp, &obj->comstackType, "tcpip");
1309 else if (argc == -1)
1310 return ir_tcl_strdel (interp, &obj->comstackType);
1313 xfree (obj->comstackType);
1314 if (ir_tcl_strdup (interp, &obj->comstackType, argv[2]) == TCL_ERROR)
1317 Tcl_AppendElement (interp, obj->comstackType);
1322 * do_logLevel: Set log level
1324 static int do_logLevel (void *o, Tcl_Interp *interp,
1325 int argc, char **argv)
1330 log_init (log_mask_str (argv[2]), "", NULL);
1332 log_init (log_mask_str (argv[2]), argv[3], NULL);
1334 log_init (log_mask_str (argv[2]), argv[3], argv[4]);
1340 * do_eventType: Return type of last event
1342 static int do_eventType (void *obj, Tcl_Interp *interp,
1343 int argc, char **argv)
1349 p->eventType = NULL;
1352 Tcl_AppendElement (interp, p->eventType ? p->eventType : "");
1358 * do_callback: add callback
1360 static int do_callback (void *obj, Tcl_Interp *interp,
1361 int argc, char **argv)
1370 else if (argc == -1)
1371 return ir_tcl_strdel (interp, &p->callback);
1374 xfree (p->callback);
1377 if (ir_tcl_strdup (interp, &p->callback, argv[2]) == TCL_ERROR)
1387 * do_failback: add error handle callback
1389 static int do_failback (void *obj, Tcl_Interp *interp,
1390 int argc, char **argv)
1399 else if (argc == -1)
1400 return ir_tcl_strdel (interp, &p->failback);
1403 xfree (p->failback);
1406 if (ir_tcl_strdup (interp, &p->failback, argv[2]) == TCL_ERROR)
1416 * do_initResponse: add init response handler
1418 static int do_initResponse (void *obj, Tcl_Interp *interp,
1419 int argc, char **argv)
1425 p->initResponse = NULL;
1428 else if (argc == -1)
1429 return ir_tcl_strdel (interp, &p->initResponse);
1432 xfree (p->initResponse);
1435 if (ir_tcl_strdup (interp, &p->initResponse, argv[2]) == TCL_ERROR)
1439 p->initResponse = NULL;
1444 * do_protocol: Set/get protocol method on IR object
1446 static int do_protocol (void *o, Tcl_Interp *interp, int argc, char **argv)
1452 p->protocol_type = PROTO_Z3950;
1457 if (!strcmp (argv[2], "Z39"))
1458 p->protocol_type = PROTO_Z3950;
1459 else if (!strcmp (argv[2], "SR"))
1460 p->protocol_type = PROTO_SR;
1463 Tcl_AppendResult (interp, "bad protocol ", argv[2], NULL);
1464 return ir_tcl_error_exec (interp, argc, argv);
1468 switch (p->protocol_type)
1471 Tcl_AppendElement (interp, "Z39");
1474 Tcl_AppendElement (interp, "SR");
1481 * do_triggerResourceControl:
1483 static int do_triggerResourceControl (void *obj, Tcl_Interp *interp,
1484 int argc, char **argv)
1488 Z_TriggerResourceControlRequest *req;
1489 bool_t is_false = 0;
1495 Tcl_AppendResult (interp, "not connected", NULL);
1496 return ir_tcl_error_exec (interp, argc, argv);
1498 apdu = zget_APDU (p->odr_out, Z_APDU_triggerResourceControlRequest);
1499 req = apdu->u.triggerResourceControlRequest;
1500 *req->requestedAction = Z_TriggerResourceCtrl_cancel;
1501 req->resultSetWanted = &is_false;
1503 return ir_tcl_send_APDU (interp, p, apdu, "triggerResourceControl",
1508 * do_databaseNames: specify database names
1510 static int do_databaseNames (void *obj, Tcl_Interp *interp,
1511 int argc, char **argv)
1514 IrTcl_SetCObj *p = obj;
1518 for (i=0; i<p->num_databaseNames; i++)
1519 xfree (p->databaseNames[i]);
1520 xfree (p->databaseNames);
1524 p->num_databaseNames = 0;
1525 p->databaseNames = NULL;
1530 for (i=0; i<p->num_databaseNames; i++)
1531 Tcl_AppendElement (interp, p->databaseNames[i]);
1534 if (p->databaseNames)
1536 for (i=0; i<p->num_databaseNames; i++)
1537 xfree (p->databaseNames[i]);
1538 xfree (p->databaseNames);
1540 p->num_databaseNames = argc - 2;
1542 ir_tcl_malloc (sizeof(*p->databaseNames) * (1+p->num_databaseNames));
1543 for (i=0; i<p->num_databaseNames; i++)
1545 if (ir_tcl_strdup (interp, &p->databaseNames[i], argv[2+i])
1549 p->databaseNames[i] = NULL;
1554 * do_replaceIndicator: Set/get replace Set indicator
1556 static int do_replaceIndicator (void *obj, Tcl_Interp *interp,
1557 int argc, char **argv)
1559 IrTcl_SetCObj *p = obj;
1563 p->replaceIndicator = 1;
1566 return ir_tcl_get_set_int (&p->replaceIndicator, interp, argc, argv);
1570 * do_queryType: Set/Get query method
1572 static int do_queryType (void *obj, Tcl_Interp *interp,
1573 int argc, char **argv)
1575 IrTcl_SetCObj *p = obj;
1578 return ir_tcl_strdup (interp, &p->queryType, "rpn");
1579 else if (argc == -1)
1580 return ir_tcl_strdel (interp, &p->queryType);
1583 xfree (p->queryType);
1584 if (ir_tcl_strdup (interp, &p->queryType, argv[2]) == TCL_ERROR)
1587 Tcl_AppendResult (interp, p->queryType, NULL);
1592 * do_userInformationField: Get User information field
1594 static int do_userInformationField (void *obj, Tcl_Interp *interp,
1595 int argc, char **argv)
1601 p->userInformationField = NULL;
1604 else if (argc == -1)
1605 return ir_tcl_strdel (interp, &p->userInformationField);
1606 Tcl_AppendResult (interp, p->userInformationField, NULL);
1611 * do_smallSetUpperBound: Set/get small set upper bound
1613 static int do_smallSetUpperBound (void *o, Tcl_Interp *interp,
1614 int argc, char **argv)
1616 IrTcl_SetCObj *p = o;
1620 p->smallSetUpperBound = 0;
1623 return ir_tcl_get_set_int (&p->smallSetUpperBound, interp, argc, argv);
1627 * do_largeSetLowerBound: Set/get large set lower bound
1629 static int do_largeSetLowerBound (void *o, Tcl_Interp *interp,
1630 int argc, char **argv)
1632 IrTcl_SetCObj *p = o;
1636 p->largeSetLowerBound = 2;
1639 return ir_tcl_get_set_int (&p->largeSetLowerBound, interp, argc, argv);
1643 * do_mediumSetPresentNumber: Set/get large set lower bound
1645 static int do_mediumSetPresentNumber (void *o, Tcl_Interp *interp,
1646 int argc, char **argv)
1648 IrTcl_SetCObj *p = o;
1652 p->mediumSetPresentNumber = 0;
1655 return ir_tcl_get_set_int (&p->mediumSetPresentNumber, interp, argc, argv);
1659 * do_referenceId: Set/Get referenceId
1661 static int do_referenceId (void *obj, Tcl_Interp *interp,
1662 int argc, char **argv)
1664 IrTcl_SetCObj *p = obj;
1668 p->referenceId = NULL;
1671 else if (argc == -1)
1672 return ir_tcl_strdel (interp, &p->referenceId);
1675 xfree (p->referenceId);
1676 if (ir_tcl_strdup (interp, &p->referenceId, argv[2]) == TCL_ERROR)
1679 Tcl_AppendResult (interp, p->referenceId, NULL);
1684 * do_preferredRecordSyntax: Set/get preferred record syntax
1686 static int do_preferredRecordSyntax (void *obj, Tcl_Interp *interp,
1687 int argc, char **argv)
1689 IrTcl_SetCObj *p = obj;
1693 p->preferredRecordSyntax = NULL;
1696 else if (argc == -1)
1698 xfree (p->preferredRecordSyntax);
1699 p->preferredRecordSyntax = NULL;
1704 xfree (p->preferredRecordSyntax);
1705 p->preferredRecordSyntax = NULL;
1706 if (argv[2][0] && (p->preferredRecordSyntax =
1707 ir_tcl_malloc (sizeof(*p->preferredRecordSyntax))))
1708 *p->preferredRecordSyntax = IrTcl_getRecordSyntaxVal (argv[2]);
1713 (interp,!p->preferredRecordSyntax ? "" :
1714 IrTcl_getRecordSyntaxStr(*p->preferredRecordSyntax));
1721 * do_elementSetNames: Set/Get element Set Names
1723 static int do_elementSetNames (void *obj, Tcl_Interp *interp,
1724 int argc, char **argv)
1726 IrTcl_SetCObj *p = obj;
1730 p->elementSetNames = NULL;
1733 else if (argc == -1)
1734 return ir_tcl_strdel (interp, &p->elementSetNames);
1737 xfree (p->elementSetNames);
1738 if (ir_tcl_strdup (interp, &p->elementSetNames, argv[2]) == TCL_ERROR)
1741 Tcl_AppendResult (interp, p->elementSetNames, NULL);
1746 * do_smallSetElementSetNames: Set/Get small Set Element Set Names
1748 static int do_smallSetElementSetNames (void *obj, Tcl_Interp *interp,
1749 int argc, char **argv)
1751 IrTcl_SetCObj *p = obj;
1755 p->smallSetElementSetNames = NULL;
1758 else if (argc == -1)
1759 return ir_tcl_strdel (interp, &p->smallSetElementSetNames);
1762 xfree (p->smallSetElementSetNames);
1763 if (ir_tcl_strdup (interp, &p->smallSetElementSetNames,
1764 argv[2]) == TCL_ERROR)
1767 Tcl_AppendResult (interp, p->smallSetElementSetNames, NULL);
1772 * do_mediumSetElementSetNames: Set/Get medium Set Element Set Names
1774 static int do_mediumSetElementSetNames (void *obj, Tcl_Interp *interp,
1775 int argc, char **argv)
1777 IrTcl_SetCObj *p = obj;
1781 p->mediumSetElementSetNames = NULL;
1784 else if (argc == -1)
1785 return ir_tcl_strdel (interp, &p->mediumSetElementSetNames);
1788 xfree (p->mediumSetElementSetNames);
1789 if (ir_tcl_strdup (interp, &p->mediumSetElementSetNames,
1790 argv[2]) == TCL_ERROR)
1793 Tcl_AppendResult (interp, p->mediumSetElementSetNames, NULL);
1797 static IrTcl_Method ir_method_tab[] = {
1798 { "comstack", do_comstack, NULL },
1799 { "protocol", do_protocol, NULL },
1800 { "failback", do_failback, NULL },
1801 { "failInfo", do_failInfo, NULL },
1802 { "apduInfo", do_apduInfo, NULL },
1803 { "logLevel", do_logLevel, NULL },
1805 { "eventType", do_eventType, NULL },
1806 { "connect", do_connect, NULL },
1807 { "protocolVersion", do_protocolVersion, NULL },
1808 { "preferredMessageSize", do_preferredMessageSize, NULL },
1809 { "maximumRecordSize", do_maximumRecordSize, NULL },
1810 { "implementationName", do_implementationName, NULL },
1811 { "implementationId", do_implementationId, NULL },
1812 { "implementationVersion", do_implementationVersion, NULL },
1813 { "targetImplementationName", do_targetImplementationName, NULL },
1814 { "targetImplementationId", do_targetImplementationId, NULL },
1815 { "targetImplementationVersion", do_targetImplementationVersion, NULL},
1816 { "userInformationField", do_userInformationField, NULL},
1817 { "idAuthentication", do_idAuthentication, NULL},
1818 { "options", do_options, NULL},
1819 { "init", do_init_request, NULL},
1820 { "initResult", do_initResult, NULL},
1821 { "disconnect", do_disconnect, NULL},
1822 { "callback", do_callback, NULL},
1823 { "initResponse", do_initResponse, NULL},
1824 { "triggerResourceControl", do_triggerResourceControl, NULL},
1825 { "initResponse", do_initResponse, NULL},
1829 static IrTcl_Method ir_set_c_method_tab[] = {
1830 { "databaseNames", do_databaseNames, NULL},
1831 { "replaceIndicator", do_replaceIndicator, NULL},
1832 { "queryType", do_queryType, NULL},
1833 { "preferredRecordSyntax", do_preferredRecordSyntax, NULL},
1834 { "smallSetUpperBound", do_smallSetUpperBound, NULL},
1835 { "largeSetLowerBound", do_largeSetLowerBound, NULL},
1836 { "mediumSetPresentNumber", do_mediumSetPresentNumber, NULL},
1837 { "referenceId", do_referenceId, NULL},
1838 { "elementSetNames", do_elementSetNames, NULL},
1839 { "smallSetElementSetNames", do_smallSetElementSetNames, NULL},
1840 { "mediumSetElementSetNames", do_mediumSetElementSetNames, NULL},
1845 * ir_obj_method: IR Object methods
1847 static int ir_obj_method (ClientData clientData, Tcl_Interp *interp,
1848 int argc, char **argv)
1850 IrTcl_Methods tab[3];
1851 IrTcl_Obj *p = (IrTcl_Obj *) clientData;
1856 Tcl_AppendResult (interp, wrongArgs, *argv, "method args...\"", NULL);
1860 tab[0].tab = ir_method_tab;
1862 tab[1].tab = ir_set_c_method_tab;
1863 tab[1].obj = &p->set_inher;
1866 if (ir_tcl_method (interp, argc, argv, tab, &r) == TCL_ERROR)
1867 return ir_tcl_method_error (interp, argc, argv, tab);
1872 * ir_obj_delete: IR Object disposal
1874 static void ir_obj_delete (ClientData clientData)
1876 IrTcl_Obj *obj = (IrTcl_Obj *) clientData;
1877 IrTcl_Methods tab[3];
1880 if (obj->ref_count > 0)
1882 assert (obj->ref_count == 0);
1884 logf (LOG_DEBUG, "ir object delete");
1885 tab[0].tab = ir_method_tab;
1887 tab[1].tab = ir_set_c_method_tab;
1888 tab[1].obj = &obj->set_inher;
1891 ir_tcl_method (NULL, -1, NULL, tab, NULL);
1894 odr_destroy (obj->odr_in);
1895 odr_destroy (obj->odr_out);
1896 odr_destroy (obj->odr_pr);
1901 * ir_obj_init: IR Object initialization
1903 int ir_obj_init (ClientData clientData, Tcl_Interp *interp,
1904 int argc, char **argv, ClientData *subData,
1905 ClientData parentData)
1907 IrTcl_Methods tab[3];
1915 Tcl_AppendResult (interp, wrongArgs, *argv, " objName\"", NULL);
1918 obj = ir_tcl_malloc (sizeof(*obj));
1921 obj->bibset = ccl_qual_mk ();
1922 if ((inf = fopen ("default.bib", "r")))
1924 ccl_qual_file (obj->bibset, inf);
1929 logf (LOG_DEBUG, "ir object create %s", argv[1]);
1930 obj->odr_in = odr_createmem (ODR_DECODE);
1931 odr_choice_enable_bias (obj->odr_in, 0);
1932 obj->odr_out = odr_createmem (ODR_ENCODE);
1933 obj->odr_pr = odr_createmem (ODR_PRINT);
1934 obj->state = IR_TCL_R_Idle;
1935 obj->interp = interp;
1939 obj->request_queue = NULL;
1941 tab[0].tab = ir_method_tab;
1943 tab[1].tab = ir_set_c_method_tab;
1944 tab[1].obj = &obj->set_inher;
1947 if (ir_tcl_method (interp, 0, NULL, tab, NULL) == TCL_ERROR)
1949 Tcl_AppendResult (interp, "Failed to initialize ", argv[1], NULL);
1952 *subData = (ClientData) obj;
1958 * ir_obj_mk: IR Object creation
1960 static int ir_obj_mk (ClientData clientData, Tcl_Interp *interp,
1961 int argc, char **argv)
1964 int r = ir_obj_init (clientData, interp, argc, argv, &subData, 0);
1968 Tcl_CreateCommand (interp, argv[1], ir_obj_method,
1969 subData, ir_obj_delete);
1973 IrTcl_Class ir_obj_class = {
1981 /* ------------------------------------------------------- */
1983 * do_search: Do search request
1985 static int do_search (void *o, Tcl_Interp *interp, int argc, char **argv)
1987 Z_SearchRequest *req;
1991 IrTcl_SetObj *obj = o;
2002 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1], "query\"",
2006 logf (LOG_DEBUG, "search %s %s", *argv, argv[2]);
2007 if (!obj->set_inher.num_databaseNames)
2009 Tcl_AppendResult (interp, "no databaseNames", NULL);
2010 return ir_tcl_error_exec (interp, argc, argv);
2014 Tcl_AppendResult (interp, "not connected", NULL);
2015 return ir_tcl_error_exec (interp, argc, argv);
2017 apdu = zget_APDU (p->odr_out, Z_APDU_searchRequest);
2018 req = apdu->u.searchRequest;
2022 set_referenceId (p->odr_out, &req->referenceId,
2023 obj->set_inher.referenceId);
2025 req->smallSetUpperBound = &obj->set_inher.smallSetUpperBound;
2026 req->largeSetLowerBound = &obj->set_inher.largeSetLowerBound;
2027 req->mediumSetPresentNumber = &obj->set_inher.mediumSetPresentNumber;
2028 req->replaceIndicator = &obj->set_inher.replaceIndicator;
2029 req->resultSetName = obj->setName ? obj->setName : "default";
2030 logf (LOG_DEBUG, "Search, resultSetName %s", req->resultSetName);
2031 req->num_databaseNames = obj->set_inher.num_databaseNames;
2032 req->databaseNames = obj->set_inher.databaseNames;
2033 for (r=0; r < obj->set_inher.num_databaseNames; r++)
2034 logf (LOG_DEBUG, " Database %s", obj->set_inher.databaseNames[r]);
2035 if (obj->set_inher.preferredRecordSyntax)
2037 struct oident ident;
2039 ident.proto = p->protocol_type;
2040 ident.oclass = CLASS_RECSYN;
2041 ident.value = *obj->set_inher.preferredRecordSyntax;
2042 logf (LOG_DEBUG, "Preferred record syntax is %d", ident.value);
2043 req->preferredRecordSyntax = odr_oiddup (p->odr_out,
2044 oid_getoidbyent (&ident));
2047 req->preferredRecordSyntax = 0;
2049 if (obj->set_inher.smallSetElementSetNames &&
2050 *obj->set_inher.smallSetElementSetNames)
2052 Z_ElementSetNames *esn = odr_malloc (p->odr_out, sizeof(*esn));
2054 esn->which = Z_ElementSetNames_generic;
2055 esn->u.generic = obj->set_inher.smallSetElementSetNames;
2056 req->smallSetElementSetNames = esn;
2059 req->smallSetElementSetNames = NULL;
2061 if (obj->set_inher.mediumSetElementSetNames &&
2062 *obj->set_inher.mediumSetElementSetNames)
2064 Z_ElementSetNames *esn = odr_malloc (p->odr_out, sizeof(*esn));
2066 esn->which = Z_ElementSetNames_generic;
2067 esn->u.generic = obj->set_inher.mediumSetElementSetNames;
2068 req->mediumSetElementSetNames = esn;
2071 req->mediumSetElementSetNames = NULL;
2073 req->query = &query;
2075 logf (LOG_DEBUG, "queryType %s", obj->set_inher.queryType);
2076 if (!strcmp (obj->set_inher.queryType, "rpn"))
2078 Z_RPNQuery *RPNquery;
2080 RPNquery = p_query_rpn (p->odr_out, p->protocol_type, argv[2]);
2083 Tcl_AppendResult (interp, "query syntax error", NULL);
2084 return ir_tcl_error_exec (interp, argc, argv);
2086 query.which = Z_Query_type_1;
2087 query.u.type_1 = RPNquery;
2090 else if (!strcmp (obj->set_inher.queryType, "cclrpn"))
2094 struct ccl_rpn_node *rpn;
2095 Z_RPNQuery *RPNquery;
2098 bib1.proto = p->protocol_type;
2099 bib1.oclass = CLASS_ATTSET;
2100 bib1.value = VAL_BIB1;
2102 rpn = ccl_find_str(p->bibset, argv[2], &error, &pos);
2105 Tcl_AppendResult (interp, "ccl syntax error ", ccl_err_msg(error),
2107 return ir_tcl_error_exec (interp, argc, argv);
2110 ccl_pr_tree (rpn, stderr);
2111 fprintf (stderr, "\n");
2113 assert((RPNquery = ccl_rpn_query(rpn)));
2114 RPNquery->attributeSetId = oid_getoidbyent (&bib1);
2115 query.which = Z_Query_type_1;
2116 query.u.type_1 = RPNquery;
2119 else if (!strcmp (obj->set_inher.queryType, "ccl"))
2121 query.which = Z_Query_type_2;
2122 query.u.type_2 = &ccl_query;
2123 ccl_query.buf = (unsigned char *) argv[2];
2124 ccl_query.len = strlen (argv[2]);
2128 Tcl_AppendResult (interp, "invalid query method ",
2129 obj->set_inher.queryType, NULL);
2130 return ir_tcl_error_exec (interp, argc, argv);
2132 return ir_tcl_send_APDU (interp, p, apdu, "search", *argv);
2136 * do_searchResponse: add search response handler
2138 static int do_searchResponse (void *o, Tcl_Interp *interp,
2139 int argc, char **argv)
2141 IrTcl_SetObj *obj = o;
2145 obj->searchResponse = NULL;
2148 else if (argc == -1)
2149 return ir_tcl_strdel (interp, &obj->searchResponse);
2152 xfree (obj->searchResponse);
2155 if (ir_tcl_strdup (interp, &obj->searchResponse, argv[2])
2160 obj->searchResponse = NULL;
2166 * do_presentResponse: add present response handler
2168 static int do_presentResponse (void *o, Tcl_Interp *interp,
2169 int argc, char **argv)
2171 IrTcl_SetObj *obj = o;
2175 obj->presentResponse = NULL;
2178 else if (argc == -1)
2179 return ir_tcl_strdel (interp, &obj->presentResponse);
2182 xfree (obj->presentResponse);
2185 if (ir_tcl_strdup (interp, &obj->presentResponse, argv[2])
2190 obj->presentResponse = NULL;
2196 * do_resultCount: Get number of hits
2198 static int do_resultCount (void *o, Tcl_Interp *interp,
2199 int argc, char **argv)
2201 IrTcl_SetObj *obj = o;
2205 obj->resultCount = 0;
2208 return ir_tcl_get_set_int (&obj->resultCount, interp, argc, argv);
2212 * do_searchStatus: Get search status (after search response)
2214 static int do_searchStatus (void *o, Tcl_Interp *interp,
2215 int argc, char **argv)
2217 IrTcl_SetObj *obj = o;
2221 return ir_tcl_get_set_int (&obj->searchStatus, interp, argc, argv);
2225 * do_presentStatus: Get search status (after search/present response)
2227 static int do_presentStatus (void *o, Tcl_Interp *interp,
2228 int argc, char **argv)
2230 IrTcl_SetObj *obj = o;
2234 return ir_tcl_get_set_int (&obj->presentStatus, interp, argc, argv);
2238 * do_nextResultSetPosition: Get next result set position
2239 * (after search/present response)
2241 static int do_nextResultSetPosition (void *o, Tcl_Interp *interp,
2242 int argc, char **argv)
2244 IrTcl_SetObj *obj = o;
2248 obj->nextResultSetPosition = 0;
2251 return ir_tcl_get_set_int (&obj->nextResultSetPosition, interp,
2256 * do_setName: Set result Set name
2258 static int do_setName (void *o, Tcl_Interp *interp,
2259 int argc, char **argv)
2261 IrTcl_SetObj *obj = o;
2264 return ir_tcl_strdup (interp, &obj->setName, "Default");
2265 else if (argc == -1)
2266 return ir_tcl_strdel (interp, &obj->setName);
2269 xfree (obj->setName);
2270 if (ir_tcl_strdup (interp, &obj->setName, argv[2])
2274 Tcl_AppendElement (interp, obj->setName);
2279 * do_numberOfRecordsReturned: Get number of records returned
2281 static int do_numberOfRecordsReturned (void *o, Tcl_Interp *interp,
2282 int argc, char **argv)
2284 IrTcl_SetObj *obj = o;
2288 obj->numberOfRecordsReturned = 0;
2291 return ir_tcl_get_set_int (&obj->numberOfRecordsReturned, interp,
2296 * do_type: Return type (if any) at position.
2298 static int do_type (void *o, Tcl_Interp *interp, int argc, char **argv)
2300 IrTcl_SetObj *obj = o;
2302 IrTcl_RecordList *rl;
2306 obj->record_list = NULL;
2309 else if (argc == -1)
2311 delete_IR_records (obj);
2316 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2317 " position\"", NULL);
2320 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2322 rl = find_IR_record (obj, offset);
2325 logf (LOG_DEBUG, "%s %s %s: no record", argv[0], argv[1], argv[2]);
2330 case Z_NamePlusRecord_databaseRecord:
2331 interp->result = "DB";
2333 case Z_NamePlusRecord_surrogateDiagnostic:
2334 interp->result = "SD";
2342 * do_recordType: Return record type (if any) at position.
2344 static int do_recordType (void *o, Tcl_Interp *interp, int argc, char **argv)
2346 IrTcl_SetObj *obj = o;
2348 IrTcl_RecordList *rl;
2354 else if (argc == -1)
2360 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2361 " position\"", NULL);
2364 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2366 rl = find_IR_record (obj, offset);
2369 logf (LOG_DEBUG, "%s %s %s: no record", argv[0], argv[1], argv[2]);
2372 if (rl->which != Z_NamePlusRecord_databaseRecord)
2374 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2377 Tcl_AppendElement (interp, (char*)
2378 IrTcl_getRecordSyntaxStr (rl->u.dbrec.type));
2383 * set record elements (for record extraction)
2385 static int do_recordElements (void *o, Tcl_Interp *interp,
2386 int argc, char **argv)
2388 IrTcl_SetObj *obj = o;
2392 obj->recordElements = NULL;
2395 else if (argc == -1)
2396 return ir_tcl_strdel (NULL, &obj->recordElements);
2399 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2400 " ?position?\"", NULL);
2405 xfree (obj->recordElements);
2406 return ir_tcl_strdup (NULL, &obj->recordElements,
2407 (*argv[2] ? argv[2] : NULL));
2409 Tcl_AppendResult (interp, obj->recordElements, NULL);
2416 static int ir_diagResult (Tcl_Interp *interp, IrTcl_Diagnostic *list, int num)
2422 for (i = 0; i<num; i++)
2424 sprintf (buf, "%d", list[i].condition);
2425 Tcl_AppendElement (interp, buf);
2426 cp = diagbib1_str (list[i].condition);
2428 Tcl_AppendElement (interp, (char*) cp);
2430 Tcl_AppendElement (interp, "");
2431 if (list[i].addinfo)
2432 Tcl_AppendElement (interp, (char*) list[i].addinfo);
2434 Tcl_AppendElement (interp, "");
2440 * do_diag: Return diagnostic record info
2442 static int do_diag (void *o, Tcl_Interp *interp, int argc, char **argv)
2444 IrTcl_SetObj *obj = o;
2446 IrTcl_RecordList *rl;
2452 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2453 " position\"", NULL);
2456 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2458 rl = find_IR_record (obj, offset);
2461 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2464 if (rl->which != Z_NamePlusRecord_surrogateDiagnostic)
2466 Tcl_AppendResult (interp, "No Diagnostic record at #", argv[2], NULL);
2469 return ir_diagResult (interp, rl->u.surrogateDiagnostics.list,
2470 rl->u.surrogateDiagnostics.num);
2474 * do_getMarc: Get ISO2709 Record lines/fields
2476 static int do_getMarc (void *o, Tcl_Interp *interp, int argc, char **argv)
2478 IrTcl_SetObj *obj = o;
2480 IrTcl_RecordList *rl;
2486 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2487 " position line|field tag ind field\"", NULL);
2490 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2492 rl = find_IR_record (obj, offset);
2495 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2498 if (rl->which != Z_NamePlusRecord_databaseRecord)
2500 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2503 return ir_tcl_get_marc (interp, rl->u.dbrec.buf, argc, argv);
2507 * do_getSutrs: Get SUTRS Record
2509 static int do_getSutrs (void *o, Tcl_Interp *interp, int argc, char **argv)
2511 IrTcl_SetObj *obj = o;
2513 IrTcl_RecordList *rl;
2519 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2520 " position\"", NULL);
2523 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2525 rl = find_IR_record (obj, offset);
2528 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2531 if (rl->which != Z_NamePlusRecord_databaseRecord)
2533 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2536 if (!rl->u.dbrec.buf || rl->u.dbrec.type != VAL_SUTRS)
2538 Tcl_AppendElement (interp, rl->u.dbrec.buf);
2544 * do_getGrs: Get a GRS-1 Record
2546 static int do_getGrs (void *o, Tcl_Interp *interp, int argc, char **argv)
2548 IrTcl_SetObj *obj = o;
2550 IrTcl_RecordList *rl;
2556 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2557 " position ?(set,tag) (set,tag) ...?\"", NULL);
2560 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2562 rl = find_IR_record (obj, offset);
2565 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2568 if (rl->which != Z_NamePlusRecord_databaseRecord)
2570 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2573 if (rl->u.dbrec.type != VAL_GRS1)
2575 return ir_tcl_get_grs (interp, rl->u.dbrec.u.grs1, argc, argv);
2580 * do_getExplain: Get an Explain Record
2582 static int do_getExplain (void *o, Tcl_Interp *interp, int argc, char **argv)
2584 IrTcl_SetObj *obj = o;
2585 IrTcl_Obj *p = obj->parent;
2587 Z_ext_typeent *etype;
2589 IrTcl_RecordList *rl;
2595 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2596 " position ?mask? ...\"", NULL);
2599 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2601 rl = find_IR_record (obj, offset);
2604 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2607 if (rl->which != Z_NamePlusRecord_databaseRecord)
2609 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2612 if (!rl->u.dbrec.buf || rl->u.dbrec.type != VAL_EXPLAIN)
2615 if (!(etype = z_ext_getentbyref (VAL_EXPLAIN)))
2617 assert (rl->u.dbrec.buf);
2618 odr_setbuf (p->odr_in, rl->u.dbrec.buf, rl->u.dbrec.size, 0);
2619 if (!(*etype->fun)(p->odr_in, (char **) &rr, 0))
2622 if (etype->what != Z_External_explainRecord)
2625 return ir_tcl_get_explain (interp, rr, argc, argv);
2629 * do_responseStatus: Return response status (present or search)
2631 static int do_responseStatus (void *o, Tcl_Interp *interp,
2632 int argc, char **argv)
2634 IrTcl_SetObj *obj = o;
2638 obj->recordFlag = 0;
2639 obj->nonSurrogateDiagnosticNum = 0;
2640 obj->nonSurrogateDiagnosticList = NULL;
2643 else if (argc == -1)
2645 ir_deleteDiags (&obj->nonSurrogateDiagnosticList,
2646 &obj->nonSurrogateDiagnosticNum);
2649 if (!obj->recordFlag)
2651 Tcl_AppendElement (interp, "OK");
2656 case Z_Records_DBOSD:
2657 Tcl_AppendElement (interp, "DBOSD");
2660 Tcl_AppendElement (interp, "NSD");
2661 return ir_diagResult (interp, obj->nonSurrogateDiagnosticList,
2662 obj->nonSurrogateDiagnosticNum);
2668 * do_present: Perform Present Request
2671 static int do_present (void *o, Tcl_Interp *interp, int argc, char **argv)
2673 IrTcl_SetObj *obj = o;
2676 Z_PresentRequest *req;
2684 if (Tcl_GetInt (interp, argv[2], &start) == TCL_ERROR)
2691 if (Tcl_GetInt (interp, argv[3], &number) == TCL_ERROR)
2696 logf (LOG_DEBUG, "present %s %d %d", *argv, start, number);
2700 Tcl_AppendResult (interp, "not connected", NULL);
2701 return ir_tcl_error_exec (interp, argc, argv);
2704 obj->number = number;
2706 apdu = zget_APDU (p->odr_out, Z_APDU_presentRequest);
2707 req = apdu->u.presentRequest;
2709 set_referenceId (p->odr_out, &req->referenceId,
2710 obj->set_inher.referenceId);
2712 req->resultSetId = obj->setName ? obj->setName : "Default";
2714 req->resultSetStartPoint = &start;
2715 req->numberOfRecordsRequested = &number;
2716 if (obj->set_inher.preferredRecordSyntax)
2718 struct oident ident;
2720 ident.proto = p->protocol_type;
2721 ident.oclass = CLASS_RECSYN;
2722 ident.value = *obj->set_inher.preferredRecordSyntax;
2723 logf (LOG_DEBUG, "Preferred record syntax is %d", ident.value);
2724 req->preferredRecordSyntax = odr_oiddup (p->odr_out,
2725 oid_getoidbyent (&ident));
2728 req->preferredRecordSyntax = 0;
2730 if (obj->set_inher.elementSetNames && *obj->set_inher.elementSetNames)
2732 Z_ElementSetNames *esn = odr_malloc (p->odr_out, sizeof(*esn));
2733 Z_RecordComposition *compo = odr_malloc (p->odr_out, sizeof(*compo));
2735 esn->which = Z_ElementSetNames_generic;
2736 esn->u.generic = obj->set_inher.elementSetNames;
2738 req->recordComposition = compo;
2739 compo->which = Z_RecordComp_simple;
2740 compo->u.simple = esn;
2743 req->recordComposition = NULL;
2744 return ir_tcl_send_APDU (interp, p, apdu, "present", *argv);
2747 #define IR_TCL_RECORD_ENCODING_ISO2709 1
2748 #define IR_TCL_RECORD_ENCODING_RAW 2
2754 } IrTcl_FileRecordHead;
2757 * do_loadFile: Load result set from file
2759 static int do_loadFile (void *o, Tcl_Interp *interp,
2760 int argc, char **argv)
2762 IrTcl_SetObj *setobj = o;
2774 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2775 " filename ?start? ?number?\"", NULL);
2779 start = atoi (argv[3]);
2781 number = atoi (argv[4]);
2784 inf = fopen (argv[2], "r");
2787 Tcl_AppendResult (interp, "Cannot open ", argv[2], NULL);
2788 return ir_tcl_error_exec (interp, argc, argv);
2790 while (offset < (start+number))
2792 IrTcl_FileRecordHead head;
2793 IrTcl_RecordList *rl;
2795 if (fread (&head, sizeof(head), 1, inf) < 1)
2797 rl = new_IR_record (setobj, offset,
2798 Z_NamePlusRecord_databaseRecord,
2799 (argc > 5) ? argv[5] : NULL);
2800 rl->u.dbrec.type = head.syntax;
2801 if (head.encoding == IR_TCL_RECORD_ENCODING_ISO2709)
2803 if (!(buf = ir_tcl_fread_marc (inf, &size)))
2805 rl->u.dbrec.buf = buf;
2806 rl->u.dbrec.size = size;
2807 if (size != head.size)
2810 Tcl_AppendResult (interp, "bad ISO2709 encoding", NULL);
2811 return ir_tcl_error_exec (interp, argc, argv);
2814 else if (head.encoding == IR_TCL_RECORD_ENCODING_RAW)
2816 rl->u.dbrec.size = head.size;
2817 rl->u.dbrec.buf = ir_tcl_malloc (head.size + 1);
2818 if (fread (rl->u.dbrec.buf, rl->u.dbrec.size, 1, inf) < 1)
2821 Tcl_AppendResult (interp, "bad raw encoding", NULL);
2822 return ir_tcl_error_exec (interp, argc, argv);
2824 rl->u.dbrec.buf[rl->u.dbrec.size] = '\0';
2828 rl->u.dbrec.buf = NULL;
2829 rl->u.dbrec.size = 0;
2831 Tcl_AppendResult (interp, "bad encoding", NULL);
2832 return ir_tcl_error_exec (interp, argc, argv);
2836 setobj->numberOfRecordsReturned = offset - start;
2842 * do_saveFile: Save result set on file
2844 static int do_saveFile (void *o, Tcl_Interp *interp,
2845 int argc, char **argv)
2847 IrTcl_SetObj *setobj = o;
2852 IrTcl_RecordList *rl;
2858 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2859 " filename ?start? ?number?\"", NULL);
2863 start = atoi (argv[3]);
2865 number = atoi (argv[4]);
2868 outf = fopen (argv[2], "w");
2871 Tcl_AppendResult (interp, "cannot open file", NULL);
2872 return ir_tcl_error_exec (interp, argc, argv);
2874 while (offset < (start+number) && (rl = find_IR_record (setobj, offset)))
2876 if (rl->which == Z_NamePlusRecord_databaseRecord &&
2877 rl->u.dbrec.buf && rl->u.dbrec.size)
2879 IrTcl_FileRecordHead head;
2881 head.encoding = IR_TCL_RECORD_ENCODING_RAW;
2882 head.syntax = rl->u.dbrec.type;
2883 head.size = rl->u.dbrec.size;
2884 if (fwrite (&head, sizeof(head), 1, outf) < 1)
2886 Tcl_AppendResult (interp, "cannot write", NULL);
2887 return ir_tcl_error_exec (interp, argc, argv);
2889 if (fwrite (rl->u.dbrec.buf, rl->u.dbrec.size, 1, outf) < 1)
2891 Tcl_AppendResult (interp, "cannot write", NULL);
2892 return ir_tcl_error_exec (interp, argc, argv);
2899 Tcl_AppendResult (interp, "cannot write ", NULL);
2900 return ir_tcl_error_exec (interp, argc, argv);
2906 static IrTcl_Method ir_set_method_tab[] = {
2907 { "search", do_search, NULL},
2908 { "searchResponse", do_searchResponse, NULL},
2909 { "presentResponse", do_presentResponse, NULL},
2910 { "searchStatus", do_searchStatus, NULL},
2911 { "presentStatus", do_presentStatus, NULL},
2912 { "nextResultSetPosition", do_nextResultSetPosition, NULL},
2913 { "setName", do_setName, NULL},
2914 { "resultCount", do_resultCount, NULL},
2915 { "numberOfRecordsReturned", do_numberOfRecordsReturned, NULL},
2916 { "present", do_present, NULL},
2917 { "type", do_type, NULL},
2918 { "getMarc", do_getMarc, NULL},
2919 { "getSutrs", do_getSutrs, NULL},
2920 { "getGrs", do_getGrs, NULL},
2921 { "getExplain", do_getExplain, NULL},
2922 { "recordType", do_recordType, NULL},
2923 { "recordElements", do_recordElements, NULL},
2924 { "diag", do_diag, NULL},
2925 { "responseStatus", do_responseStatus, NULL},
2926 { "loadFile", do_loadFile, NULL},
2927 { "saveFile", do_saveFile, NULL},
2932 * ir_set_obj_method: IR Set Object methods
2934 static int ir_set_obj_method (ClientData clientData, Tcl_Interp *interp,
2935 int argc, char **argv)
2937 IrTcl_Methods tabs[3];
2938 IrTcl_SetObj *p = (IrTcl_SetObj *) clientData;
2943 Tcl_AppendResult (interp, wrongArgs, *argv, " method args...\"", NULL);
2946 tabs[0].tab = ir_set_method_tab;
2948 tabs[1].tab = ir_set_c_method_tab;
2949 tabs[1].obj = &p->set_inher;
2952 if (ir_tcl_method (interp, argc, argv, tabs, &r) == TCL_ERROR)
2953 return ir_tcl_method_error (interp, argc, argv, tabs);
2958 * ir_set_obj_delete: IR Set Object disposal
2960 static void ir_set_obj_delete (ClientData clientData)
2962 IrTcl_Methods tabs[3];
2963 IrTcl_SetObj *p = (IrTcl_SetObj *) clientData;
2965 logf (LOG_DEBUG, "ir set delete");
2967 tabs[0].tab = ir_set_method_tab;
2969 tabs[1].tab = ir_set_c_method_tab;
2970 tabs[1].obj = &p->set_inher;
2973 ir_tcl_method (NULL, -1, NULL, tabs, NULL);
2979 * ir_set_obj_init: IR Set Object initialization
2981 static int ir_set_obj_init (ClientData clientData, Tcl_Interp *interp,
2982 int argc, char **argv, ClientData *subData,
2983 ClientData parentData)
2985 IrTcl_Methods tabs[3];
2988 if (argc < 2 || argc > 3)
2990 Tcl_AppendResult (interp, wrongArgs, *argv,
2991 " objSetName ?objParent?\"", NULL);
2994 obj = ir_tcl_malloc (sizeof(*obj));
2995 logf (LOG_DEBUG, "ir set create %s", argv[1]);
3002 obj->parent = (IrTcl_Obj *) parentData;
3004 dst = &obj->set_inher;
3005 src = &obj->parent->set_inher;
3007 if ((dst->num_databaseNames = src->num_databaseNames))
3009 dst->databaseNames =
3010 ir_tcl_malloc (sizeof (*dst->databaseNames)
3011 * (1+dst->num_databaseNames));
3012 for (i = 0; i < dst->num_databaseNames; i++)
3013 if (ir_tcl_strdup (interp, &dst->databaseNames[i],
3014 src->databaseNames[i]) == TCL_ERROR)
3016 dst->databaseNames[i] = NULL;
3019 dst->databaseNames = NULL;
3020 if (ir_tcl_strdup (interp, &dst->queryType, src->queryType)
3024 if (ir_tcl_strdup (interp, &dst->referenceId, src->referenceId)
3028 if (ir_tcl_strdup (interp, &dst->elementSetNames, src->elementSetNames)
3032 if (ir_tcl_strdup (interp, &dst->smallSetElementSetNames,
3033 src->smallSetElementSetNames)
3037 if (ir_tcl_strdup (interp, &dst->mediumSetElementSetNames,
3038 src->mediumSetElementSetNames)
3042 if (src->preferredRecordSyntax &&
3043 (dst->preferredRecordSyntax
3044 = ir_tcl_malloc (sizeof(*dst->preferredRecordSyntax))))
3045 *dst->preferredRecordSyntax = *src->preferredRecordSyntax;
3047 dst->preferredRecordSyntax = NULL;
3048 dst->replaceIndicator = src->replaceIndicator;
3049 dst->smallSetUpperBound = src->smallSetUpperBound;
3050 dst->largeSetLowerBound = src->largeSetLowerBound;
3051 dst->mediumSetPresentNumber = src->mediumSetPresentNumber;
3056 tabs[0].tab = ir_set_method_tab;
3060 if (ir_tcl_method (interp, 0, NULL, tabs, NULL) == TCL_ERROR)
3063 *subData = (ClientData) obj;
3068 * ir_set_obj_mk: IR Set Object creation
3070 static int ir_set_obj_mk (ClientData clientData, Tcl_Interp *interp,
3071 int argc, char **argv)
3074 ClientData parentData = 0;
3079 Tcl_CmdInfo parent_info;
3080 if (!Tcl_GetCommandInfo (interp, argv[2], &parent_info))
3082 Tcl_AppendResult (interp, "no object parent", NULL);
3083 return ir_tcl_error_exec (interp, argc, argv);
3085 parentData = parent_info.clientData;
3087 r = ir_set_obj_init (clientData, interp, argc, argv, &subData, parentData);
3090 Tcl_CreateCommand (interp, argv[1], ir_set_obj_method,
3091 subData, ir_set_obj_delete);
3095 IrTcl_Class ir_set_obj_class = {
3102 /* ------------------------------------------------------- */
3105 * do_scan: Perform scan
3107 static int do_scan (void *o, Tcl_Interp *interp, int argc, char **argv)
3111 IrTcl_ScanObj *obj = o;
3112 IrTcl_Obj *p = obj->parent;
3115 struct ccl_rpn_node *rpn;
3123 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
3124 " scanQuery\"", NULL);
3127 logf (LOG_DEBUG, "scan %s %s", *argv, argv[2]);
3128 if (!p->set_inher.num_databaseNames)
3130 Tcl_AppendResult (interp, "no databaseNames", NULL);
3131 return ir_tcl_error_exec (interp, argc, argv);
3135 Tcl_AppendResult (interp, "not connected", NULL);
3136 return ir_tcl_error_exec (interp, argc, argv);
3139 apdu = zget_APDU (p->odr_out, Z_APDU_scanRequest);
3140 req = apdu->u.scanRequest;
3142 set_referenceId (p->odr_out, &req->referenceId, p->set_inher.referenceId);
3143 req->num_databaseNames = p->set_inher.num_databaseNames;
3144 req->databaseNames = p->set_inher.databaseNames;
3147 if (!(req->termListAndStartPoint =
3148 p_query_scan (p->odr_out, p->protocol_type,
3149 &req->attributeSet, argv[2])))
3151 Tcl_AppendResult (interp, "query syntax error", NULL);
3152 return ir_tcl_error_exec (interp, argc, argv);
3155 rpn = ccl_find_str(p->bibset, argv[2], &r, &pos);
3158 Tcl_AppendResult (interp, "ccl syntax error ", ccl_err_msg(r), NULL);
3159 return ir_tcl_error_exec (interp, argc, argv);
3161 bib1.proto = p->protocol_type;
3162 bib1.oclass = CLASS_ATTSET;
3163 bib1.value = VAL_BIB1;
3165 req->attributeSet = oid_getoidbyent (&bib1);
3166 if (!(req->termListAndStartPoint = ccl_scan_query (rpn)))
3169 req->stepSize = &obj->stepSize;
3170 req->numberOfTermsRequested = &obj->numberOfTermsRequested;
3171 req->preferredPositionInResponse = &obj->preferredPositionInResponse;
3172 logf (LOG_DEBUG, "stepSize=%d", *req->stepSize);
3173 logf (LOG_DEBUG, "numberOfTermsRequested=%d",
3174 *req->numberOfTermsRequested);
3175 logf (LOG_DEBUG, "preferredPositionInResponse=%d",
3176 *req->preferredPositionInResponse);
3178 return ir_tcl_send_APDU (interp, p, apdu, "scan", *argv);
3182 * do_scanResponse: add scan response handler
3184 static int do_scanResponse (void *o, Tcl_Interp *interp,
3185 int argc, char **argv)
3187 IrTcl_ScanObj *obj = o;
3191 obj->scanResponse = NULL;
3194 else if (argc == -1)
3195 return ir_tcl_strdel (interp, &obj->scanResponse);
3198 xfree (obj->scanResponse);
3201 if (ir_tcl_strdup (interp, &obj->scanResponse, argv[2])
3206 obj->scanResponse = NULL;
3212 * do_stepSize: Set/get replace Step Size
3214 static int do_stepSize (void *obj, Tcl_Interp *interp,
3215 int argc, char **argv)
3217 IrTcl_ScanObj *p = obj;
3223 return ir_tcl_get_set_int (&p->stepSize, interp, argc, argv);
3227 * do_numberOfTermsRequested: Set/get Number of Terms requested
3229 static int do_numberOfTermsRequested (void *obj, Tcl_Interp *interp,
3230 int argc, char **argv)
3232 IrTcl_ScanObj *p = obj;
3236 p->numberOfTermsRequested = 20;
3239 return ir_tcl_get_set_int (&p->numberOfTermsRequested, interp, argc, argv);
3244 * do_preferredPositionInResponse: Set/get preferred Position
3246 static int do_preferredPositionInResponse (void *obj, Tcl_Interp *interp,
3247 int argc, char **argv)
3249 IrTcl_ScanObj *p = obj;
3253 p->preferredPositionInResponse = 1;
3256 return ir_tcl_get_set_int (&p->preferredPositionInResponse, interp,
3261 * do_scanStatus: Get scan status
3263 static int do_scanStatus (void *obj, Tcl_Interp *interp,
3264 int argc, char **argv)
3266 IrTcl_ScanObj *p = obj;
3270 return ir_tcl_get_set_int (&p->scanStatus, interp, argc, argv);
3274 * do_numberOfEntriesReturned: Get number of Entries returned
3276 static int do_numberOfEntriesReturned (void *obj, Tcl_Interp *interp,
3277 int argc, char **argv)
3279 IrTcl_ScanObj *p = obj;
3283 return ir_tcl_get_set_int (&p->numberOfEntriesReturned, interp,
3288 * do_positionOfTerm: Get position of Term
3290 static int do_positionOfTerm (void *obj, Tcl_Interp *interp,
3291 int argc, char **argv)
3293 IrTcl_ScanObj *p = obj;
3297 return ir_tcl_get_set_int (&p->positionOfTerm, interp, argc, argv);
3301 * do_scanLine: get Scan Line (surrogate or normal) after response
3303 static int do_scanLine (void *obj, Tcl_Interp *interp, int argc, char **argv)
3305 IrTcl_ScanObj *p = obj;
3311 p->entries_flag = 0;
3313 p->nonSurrogateDiagnosticNum = 0;
3314 p->nonSurrogateDiagnosticList = 0;
3317 else if (argc == -1)
3319 p->entries_flag = 0;
3320 /* release entries */
3323 ir_deleteDiags (&p->nonSurrogateDiagnosticList,
3324 &p->nonSurrogateDiagnosticNum);
3329 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
3330 " position\"", NULL);
3333 printf ("argv[2]=%s\n", argv[2]);
3334 if (Tcl_GetInt (interp, argv[2], &i) == TCL_ERROR)
3336 if (!p->entries_flag || !p->entries || i >= p->num_entries || i < 0)
3338 switch (p->entries[i].which)
3340 case Z_Entry_termInfo:
3341 Tcl_AppendElement (interp, "T");
3342 if (p->entries[i].u.term.buf)
3343 Tcl_AppendElement (interp, p->entries[i].u.term.buf);
3345 Tcl_AppendElement (interp, "");
3346 sprintf (numstr, "%d", p->entries[i].u.term.globalOccurrences);
3347 Tcl_AppendElement (interp, numstr);
3349 case Z_Entry_surrogateDiagnostic:
3350 Tcl_AppendElement (interp, "SD");
3351 return ir_diagResult (interp, p->entries[i].u.diag.list,
3352 p->entries[i].u.diag.num);
3358 static IrTcl_Method ir_scan_method_tab[] = {
3359 { "scan", do_scan, NULL},
3360 { "scanResponse", do_scanResponse, NULL},
3361 { "stepSize", do_stepSize, NULL},
3362 { "numberOfTermsRequested", do_numberOfTermsRequested, NULL},
3363 { "preferredPositionInResponse", do_preferredPositionInResponse, NULL},
3364 { "scanStatus", do_scanStatus, NULL},
3365 { "numberOfEntriesReturned", do_numberOfEntriesReturned, NULL},
3366 { "positionOfTerm", do_positionOfTerm, NULL},
3367 { "scanLine", do_scanLine, NULL},
3372 * ir_scan_obj_method: IR Scan Object methods
3374 static int ir_scan_obj_method (ClientData clientData, Tcl_Interp *interp,
3375 int argc, char **argv)
3377 IrTcl_Methods tabs[2];
3382 Tcl_AppendResult (interp, wrongArgs, *argv, " method args...\"", NULL);
3385 tabs[0].tab = ir_scan_method_tab;
3386 tabs[0].obj = clientData;
3389 if (ir_tcl_method (interp, argc, argv, tabs, &r) == TCL_ERROR)
3390 return ir_tcl_method_error (interp, argc, argv, tabs);
3395 * ir_scan_obj_delete: IR Scan Object disposal
3397 static void ir_scan_obj_delete (ClientData clientData)
3399 IrTcl_Methods tabs[2];
3400 IrTcl_ScanObj *obj = (IrTcl_ScanObj *) clientData;
3402 tabs[0].tab = ir_scan_method_tab;
3406 ir_tcl_method (NULL, -1, NULL, tabs, NULL);
3411 * ir_scan_obj_mk: IR Scan Object creation
3413 static int ir_scan_obj_mk (ClientData clientData, Tcl_Interp *interp,
3414 int argc, char **argv)
3416 Tcl_CmdInfo parent_info;
3418 IrTcl_Methods tabs[2];
3422 Tcl_AppendResult (interp, wrongArgs, *argv,
3423 "objScanName objParentName\"", NULL);
3426 logf (LOG_DEBUG, "ir scan create %s", argv[1]);
3427 if (!Tcl_GetCommandInfo (interp, argv[2], &parent_info))
3429 Tcl_AppendResult (interp, "no object parent", NULL);
3430 return ir_tcl_error_exec (interp, argc, argv);
3432 obj = ir_tcl_malloc (sizeof(*obj));
3433 obj->parent = (IrTcl_Obj *) parent_info.clientData;
3435 tabs[0].tab = ir_scan_method_tab;
3439 if (ir_tcl_method (interp, 0, NULL, tabs, NULL) == TCL_ERROR)
3441 Tcl_CreateCommand (interp, argv[1], ir_scan_obj_method,
3442 (ClientData) obj, ir_scan_obj_delete);
3446 /* ------------------------------------------------------- */
3449 * ir_log_init_proc: set yaz log level
3451 static int ir_log_init_proc (ClientData clientData, Tcl_Interp *interp,
3452 int argc, char **argv)
3454 if (argc <= 1 || argc > 4)
3456 Tcl_AppendResult (interp, wrongArgs, *argv,
3457 " ?level ?prefix ?filename\"", NULL);
3461 log_init (log_mask_str (argv[1]), "", NULL);
3463 log_init (log_mask_str (argv[1]), argv[2], NULL);
3465 log_init (log_mask_str (argv[1]), argv[2], argv[3]);
3470 * ir_log_proc: log yaz message
3472 static int ir_log_proc (ClientData clientData, Tcl_Interp *interp,
3473 int argc, char **argv)
3478 Tcl_AppendResult (interp, wrongArgs, *argv,
3479 " level string\"", NULL);
3482 mask = log_mask_str_x (argv[1], 0);
3483 logf (LOG_DEBUG, "%s", argv[2]);
3488 /* ------------------------------------------------------- */
3489 static void ir_initResponse (void *obj, Z_InitResponse *initrs)
3493 p->initResult = *initrs->result ? 1 : 0;
3494 if (!*initrs->result)
3495 logf (LOG_DEBUG, "Connection rejected by target");
3497 logf (LOG_DEBUG, "Connection accepted by target");
3499 get_referenceId (&p->set_inher.referenceId, initrs->referenceId);
3501 xfree (p->targetImplementationId);
3502 ir_tcl_strdup (p->interp, &p->targetImplementationId,
3503 initrs->implementationId);
3504 xfree (p->targetImplementationName);
3505 ir_tcl_strdup (p->interp, &p->targetImplementationName,
3506 initrs->implementationName);
3507 xfree (p->targetImplementationVersion);
3508 ir_tcl_strdup (p->interp, &p->targetImplementationVersion,
3509 initrs->implementationVersion);
3511 p->maximumRecordSize = *initrs->maximumRecordSize;
3512 p->preferredMessageSize = *initrs->preferredMessageSize;
3514 memcpy (&p->options, initrs->options, sizeof(initrs->options));
3515 memcpy (&p->protocolVersion, initrs->protocolVersion,
3516 sizeof(initrs->protocolVersion));
3517 xfree (p->userInformationField);
3518 p->userInformationField = NULL;
3519 if (initrs->userInformationField)
3523 if (initrs->userInformationField->which == ODR_EXTERNAL_octet &&
3524 (p->userInformationField =
3525 ir_tcl_malloc ((len =
3526 initrs->userInformationField->
3527 u.octet_aligned->len) +1)))
3529 memcpy (p->userInformationField,
3530 initrs->userInformationField->u.octet_aligned->buf,
3532 (p->userInformationField)[len] = '\0';
3537 static void ir_deleteDiags (IrTcl_Diagnostic **dst_list, int *dst_num)
3540 for (i = 0; i<*dst_num; i++)
3541 xfree (dst_list[i]->addinfo);
3547 static void ir_handleDiags (IrTcl_Diagnostic **dst_list, int *dst_num,
3548 Z_DiagRec **list, int num)
3551 char *addinfo = NULL;
3554 *dst_list = ir_tcl_malloc (sizeof(**dst_list) * num);
3555 for (i = 0; i<num; i++)
3558 switch (list[i]->which)
3560 case Z_DiagRec_defaultFormat:
3561 (*dst_list)[i].condition = *list[i]->u.defaultFormat->condition;
3563 switch (list[i]->u.defaultFormat->which)
3565 case Z_DefaultDiagFormat_v2Addinfo:
3566 addinfo = list[i]->u.defaultFormat->u.v2Addinfo;
3568 case Z_DefaultDiagFormat_v3Addinfo:
3569 addinfo = list[i]->u.defaultFormat->u.v3Addinfo;
3573 addinfo = list[i]->u.defaultFormat->addinfo;
3576 ((*dst_list)[i].addinfo = ir_tcl_malloc (strlen(addinfo)+1)))
3577 strcpy ((*dst_list)[i].addinfo, addinfo);
3578 cp = diagbib1_str ((*dst_list)[i].condition);
3579 logf (LOG_DEBUG, "Diag %d %s %s", (*dst_list)[i].condition,
3580 cp ? cp : "", addinfo ? addinfo : "");
3583 (*dst_list)[i].addinfo = NULL;
3584 (*dst_list)[i].condition = 0;
3589 static void ir_handleDBRecord (IrTcl_Obj *p, IrTcl_RecordList *rl,
3592 struct oident *ident;
3593 Z_ext_typeent *etype;
3595 rl->u.dbrec.size = oe->u.octet_aligned->len;
3596 rl->u.dbrec.buf = NULL;
3598 if ((ident = oid_getentbyoid (oe->direct_reference)))
3599 rl->u.dbrec.type = ident->value;
3601 rl->u.dbrec.type = VAL_USMARC;
3603 if (ident && (oe->which == Z_External_single ||
3604 oe->which == Z_External_octet)
3605 && (etype = z_ext_getentbyref (ident->value)))
3609 odr_setbuf (p->odr_in, (char*) oe->u.octet_aligned->buf,
3610 oe->u.octet_aligned->len, 0);
3611 if (!(*etype->fun)(p->odr_in, (char **) &rr, 0))
3613 switch (etype->what)
3615 case Z_External_sutrs:
3616 logf (LOG_DEBUG, "Z_External_sutrs");
3618 if ((rl->u.dbrec.buf = ir_tcl_malloc (oe->u.sutrs->len+1)))
3620 memcpy (rl->u.dbrec.buf, oe->u.sutrs->buf,
3622 rl->u.dbrec.buf[oe->u.sutrs->len] = '\0';
3624 rl->u.dbrec.size = oe->u.sutrs->len;
3626 case Z_External_grs1:
3627 logf (LOG_DEBUG, "Z_External_grs1");
3629 ir_tcl_grs_mk (oe->u.grs1, &rl->u.dbrec.u.grs1);
3631 case Z_External_explainRecord:
3632 logf (LOG_DEBUG, "Z_External_explainRecord");
3633 if ((rl->u.dbrec.buf = ir_tcl_malloc (rl->u.dbrec.size)))
3635 memcpy (rl->u.dbrec.buf, oe->u.octet_aligned->buf,
3643 if (oe->which == Z_External_octet && rl->u.dbrec.size > 0)
3645 char *buf = (char*) oe->u.octet_aligned->buf;
3646 if ((rl->u.dbrec.buf = ir_tcl_malloc (rl->u.dbrec.size)))
3647 memcpy (rl->u.dbrec.buf, buf, rl->u.dbrec.size);
3649 else if (rl->u.dbrec.type == VAL_SUTRS &&
3650 oe->which == Z_External_sutrs)
3652 if ((rl->u.dbrec.buf = ir_tcl_malloc (oe->u.sutrs->len+1)))
3654 memcpy (rl->u.dbrec.buf, oe->u.sutrs->buf,
3656 rl->u.dbrec.buf[oe->u.sutrs->len] = '\0';
3658 rl->u.dbrec.size = oe->u.sutrs->len;
3660 else if (rl->u.dbrec.type == VAL_GRS1 &&
3661 oe->which == Z_External_grs1)
3663 ir_tcl_grs_mk (oe->u.grs1, &rl->u.dbrec.u.grs1);
3668 static void ir_handleZRecords (void *o, Z_Records *zrs, IrTcl_SetObj *setobj,
3669 const char *elements)
3674 IrTcl_RecordList *rl;
3676 setobj->which = zrs->which;
3677 setobj->recordFlag = 1;
3679 ir_deleteDiags (&setobj->nonSurrogateDiagnosticList,
3680 &setobj->nonSurrogateDiagnosticNum);
3681 if (zrs->which == Z_Records_DBOSD)
3683 int num_rec = zrs->u.databaseOrSurDiagnostics->num_records;
3685 if (num_rec != setobj->numberOfRecordsReturned)
3687 logf (LOG_WARN, "numberOfRecordsReturned=%d but num records=%d",
3688 setobj->numberOfRecordsReturned, num_rec);
3689 setobj->numberOfRecordsReturned = num_rec;
3692 for (offset = 0; offset < num_rec; offset++)
3694 Z_NamePlusRecord *znpr = zrs->u.databaseOrSurDiagnostics->
3697 rl = new_IR_record (setobj, setobj->start + offset, znpr->which,
3699 if (rl->which == Z_NamePlusRecord_surrogateDiagnostic)
3700 ir_handleDiags (&rl->u.surrogateDiagnostics.list,
3701 &rl->u.surrogateDiagnostics.num,
3702 &znpr->u.surrogateDiagnostic,
3705 ir_handleDBRecord (p, rl,
3706 (Z_External*) (znpr->u.databaseRecord));
3709 else if (zrs->which == Z_Records_multipleNSD)
3711 logf (LOG_DEBUG, "multipleNonSurrogateDiagnostic %d",
3712 zrs->u.multipleNonSurDiagnostics->num_diagRecs);
3713 setobj->numberOfRecordsReturned = 0;
3714 ir_handleDiags (&setobj->nonSurrogateDiagnosticList,
3715 &setobj->nonSurrogateDiagnosticNum,
3716 zrs->u.multipleNonSurDiagnostics->diagRecs,
3717 zrs->u.multipleNonSurDiagnostics->num_diagRecs);
3722 Z_DiagRec dr, *dr_p = &dr;
3723 dr.which = Z_DiagRec_defaultFormat;
3724 dr.u.defaultFormat = zrs->u.nonSurrogateDiagnostic;
3726 Z_DiagRec *dr_p = zrs->u.nonSurrogateDiagnostic;
3728 logf (LOG_DEBUG, "NonSurrogateDiagnostic");
3730 setobj->numberOfRecordsReturned = 0;
3731 ir_handleDiags (&setobj->nonSurrogateDiagnosticList,
3732 &setobj->nonSurrogateDiagnosticNum,
3737 static void ir_searchResponse (void *o, Z_SearchResponse *searchrs,
3738 IrTcl_SetObj *setobj)
3740 Z_Records *zrs = searchrs->records;
3742 logf (LOG_DEBUG, "Received search response");
3745 logf (LOG_DEBUG, "Search response, no object!");
3748 setobj->searchStatus = searchrs->searchStatus ? 1 : 0;
3749 get_referenceId (&setobj->set_inher.referenceId, searchrs->referenceId);
3750 setobj->resultCount = *searchrs->resultCount;
3751 if (searchrs->presentStatus)
3752 setobj->presentStatus = *searchrs->presentStatus;
3753 if (searchrs->nextResultSetPosition)
3754 setobj->nextResultSetPosition = *searchrs->nextResultSetPosition;
3756 logf (LOG_DEBUG, "status %d hits %d",
3757 setobj->searchStatus, setobj->resultCount);
3761 if (setobj->resultCount <= setobj->set_inher.smallSetUpperBound)
3762 es = setobj->set_inher.smallSetElementSetNames;
3764 es = setobj->set_inher.mediumSetElementSetNames;
3765 setobj->numberOfRecordsReturned = *searchrs->numberOfRecordsReturned;
3766 ir_handleZRecords (o, zrs, setobj, es);
3770 setobj->numberOfRecordsReturned = 0;
3771 setobj->recordFlag = 0;
3776 static void ir_presentResponse (void *o, Z_PresentResponse *presrs,
3777 IrTcl_SetObj *setobj)
3779 Z_Records *zrs = presrs->records;
3781 logf (LOG_DEBUG, "Received present response");
3784 logf (LOG_DEBUG, "Present response, no object!");
3787 setobj->presentStatus = *presrs->presentStatus;
3788 get_referenceId (&setobj->set_inher.referenceId, presrs->referenceId);
3789 setobj->nextResultSetPosition = *presrs->nextResultSetPosition;
3792 setobj->numberOfRecordsReturned = *presrs->numberOfRecordsReturned;
3793 ir_handleZRecords (o, zrs, setobj, setobj->set_inher.elementSetNames);
3797 setobj->numberOfRecordsReturned = 0;
3798 setobj->recordFlag = 0;
3799 logf (LOG_DEBUG, "No records!");
3803 static void ir_scanResponse (void *o, Z_ScanResponse *scanrs,
3804 IrTcl_ScanObj *scanobj)
3808 logf (LOG_DEBUG, "Received scanResponse");
3810 get_referenceId (&p->set_inher.referenceId, scanrs->referenceId);
3811 scanobj->scanStatus = *scanrs->scanStatus;
3812 logf (LOG_DEBUG, "scanStatus=%d", scanobj->scanStatus);
3814 if (scanrs->stepSize)
3815 scanobj->stepSize = *scanrs->stepSize;
3816 logf (LOG_DEBUG, "stepSize=%d", scanobj->stepSize);
3818 scanobj->numberOfEntriesReturned = *scanrs->numberOfEntriesReturned;
3819 logf (LOG_DEBUG, "numberOfEntriesReturned=%d",
3820 scanobj->numberOfEntriesReturned);
3822 if (scanrs->positionOfTerm)
3823 scanobj->positionOfTerm = *scanrs->positionOfTerm;
3825 scanobj->positionOfTerm = -1;
3826 logf (LOG_DEBUG, "positionOfTerm=%d", scanobj->positionOfTerm);
3828 xfree (scanobj->entries);
3829 scanobj->entries = NULL;
3830 scanobj->num_entries = 0;
3831 scanobj->entries_flag = 0;
3833 ir_deleteDiags (&scanobj->nonSurrogateDiagnosticList,
3834 &scanobj->nonSurrogateDiagnosticNum);
3835 if (scanrs->entries)
3840 scanobj->entries_flag = 1;
3842 if (scanrs->entries)
3844 scanobj->num_entries = scanrs->entries->num_entries;
3845 scanobj->entries = ir_tcl_malloc (scanobj->num_entries *
3846 sizeof(*scanobj->entries));
3847 ze = scanrs->entries->entries;
3850 if (scanrs->entries->which == Z_ListEntries_entries)
3852 scanobj->num_entries = scanrs->entries->u.entries->num_entries;
3853 scanobj->entries = ir_tcl_malloc (scanobj->num_entries *
3854 sizeof(*scanobj->entries));
3855 ze = scanrs->entries->u.entries->entries;
3858 for (i=0; i<scanobj->num_entries; i++, ze++)
3860 scanobj->entries[i].which = (*ze)->which;
3861 switch ((*ze)->which)
3863 case Z_Entry_termInfo:
3864 if ((*ze)->u.termInfo->term->which == Z_Term_general)
3866 int l = (*ze)->u.termInfo->term->u.general->len;
3867 scanobj->entries[i].u.term.buf = ir_tcl_malloc (1+l);
3868 memcpy (scanobj->entries[i].u.term.buf,
3869 (*ze)->u.termInfo->term->u.general->buf,
3871 scanobj->entries[i].u.term.buf[l] = '\0';
3874 scanobj->entries[i].u.term.buf = NULL;
3875 if ((*ze)->u.termInfo->globalOccurrences)
3876 scanobj->entries[i].u.term.globalOccurrences =
3877 *(*ze)->u.termInfo->globalOccurrences;
3879 scanobj->entries[i].u.term.globalOccurrences = 0;
3881 case Z_Entry_surrogateDiagnostic:
3882 ir_handleDiags (&scanobj->entries[i].u.diag.list,
3883 &scanobj->entries[i].u.diag.num,
3884 &(*ze)->u.surrogateDiagnostic,
3890 if (scanrs->entries->nonsurrogateDiagnostics)
3891 ir_handleDiags (&scanobj->nonSurrogateDiagnosticList,
3892 &scanobj->nonSurrogateDiagnosticNum,
3893 scanrs->entries->nonsurrogateDiagnostics,
3894 scanrs->entries->num_nonsurrogateDiagnostics);
3896 if (scanrs->entries->which == Z_ListEntries_nonSurrogateDiagnostics)
3897 ir_handleDiags (&scanobj->nonSurrogateDiagnosticList,
3898 &scanobj->nonSurrogateDiagnosticNum,
3899 scanrs->entries->u.nonSurrogateDiagnostics->
3901 scanrs->entries->u.nonSurrogateDiagnostics->
3908 * ir_select_read: handle incoming packages
3910 static void ir_select_read (ClientData clientData)
3912 IrTcl_Obj *p = (IrTcl_Obj *) clientData;
3917 Tcl_CmdInfo cmd_info;
3918 const char *apdu_call;
3920 logf(LOG_DEBUG, "Read handler fd=%d", cs_fileno(p->cs_link));
3921 if (p->state == IR_TCL_R_Connecting)
3923 logf(LOG_DEBUG, "read: connect");
3924 r = cs_rcvconnect (p->cs_link);
3927 logf (LOG_WARN, "cs_rcvconnect returned 1");
3930 p->state = IR_TCL_R_Idle;
3932 ir_select_remove_write (cs_fileno (p->cs_link), p);
3935 logf (LOG_DEBUG, "cs_rcvconnect error");
3936 ir_tcl_disconnect (p);
3939 p->failInfo = IR_TCL_FAIL_CONNECT;
3940 ir_tcl_eval (p->interp, p->failback);
3942 ir_obj_delete ((ClientData) p);
3946 ir_tcl_eval (p->interp, p->callback);
3947 if (p->ref_count == 2 && p->cs_link && p->request_queue
3948 && p->state == IR_TCL_R_Idle)
3949 ir_tcl_send_q (p, p->request_queue, "x");
3950 ir_obj_delete ((ClientData) p);
3955 p->state = IR_TCL_R_Reading;
3957 /* read incoming APDU */
3958 if ((r=cs_get (p->cs_link, &p->buf_in, &p->len_in)) == 1)
3960 logf(LOG_DEBUG, "PDU Fraction read");
3963 /* signal one more use of ir object - callbacks must not
3964 release the ir memory (p pointer) */
3968 logf (LOG_DEBUG, "cs_get failed, code %d", r);
3969 ir_tcl_disconnect (p);
3972 p->failInfo = IR_TCL_FAIL_READ;
3973 ir_tcl_eval (p->interp, p->failback);
3975 /* release ir object now if callback deleted it */
3976 ir_obj_delete ((ClientData) p);
3979 /* got complete APDU. Now decode */
3982 odr_setbuf (p->odr_in, p->buf_in, r, 0);
3983 logf (LOG_DEBUG, "cs_get ok, total size %d", r);
3984 if (!z_APDU (p->odr_in, &apdu, 0))
3986 logf (LOG_DEBUG, "cs_get failed: %s",
3987 odr_errmsg (odr_geterror (p->odr_in)));
3988 ir_tcl_disconnect (p);
3991 p->failInfo = IR_TCL_FAIL_IN_APDU;
3992 p->apduOffset = odr_offset (p->odr_in);
3993 ir_tcl_eval (p->interp, p->failback);
3995 /* release ir object now if failback deleted it */
3996 ir_obj_delete ((ClientData) p);
3999 /* handle APDU and invoke callback */
4000 rq = p->request_queue;
4003 logf (LOG_FATAL, "Internal error. No queue entry");
4006 object_name = rq->object_name;
4007 logf (LOG_DEBUG, "Object %s", object_name);
4009 if (Tcl_GetCommandInfo (p->interp, object_name, &cmd_info))
4013 case Z_APDU_initResponse:
4014 p->eventType = "init";
4015 ir_initResponse (p, apdu->u.initResponse);
4016 apdu_call = p->initResponse;
4018 case Z_APDU_searchResponse:
4019 p->eventType = "search";
4020 ir_searchResponse (p, apdu->u.searchResponse,
4021 (IrTcl_SetObj *) cmd_info.clientData);
4022 apdu_call = ((IrTcl_SetObj *)
4023 cmd_info.clientData)->searchResponse;
4025 case Z_APDU_presentResponse:
4026 p->eventType = "present";
4027 ir_presentResponse (p, apdu->u.presentResponse,
4028 (IrTcl_SetObj *) cmd_info.clientData);
4029 apdu_call = ((IrTcl_SetObj *)
4030 cmd_info.clientData)->presentResponse;
4032 case Z_APDU_scanResponse:
4033 p->eventType = "scan";
4034 ir_scanResponse (p, apdu->u.scanResponse,
4035 (IrTcl_ScanObj *) cmd_info.clientData);
4036 apdu_call = ((IrTcl_ScanObj *)
4037 cmd_info.clientData)->scanResponse;
4040 logf (LOG_WARN, "Received unknown APDU type (%d)",
4042 ir_tcl_disconnect (p);
4045 p->failInfo = IR_TCL_FAIL_UNKNOWN_APDU;
4046 ir_tcl_eval (p->interp, p->failback);
4051 p->request_queue = rq->next;
4052 p->state = IR_TCL_R_Idle;
4055 ir_tcl_eval (p->interp, apdu_call);
4056 else if (rq->callback)
4057 ir_tcl_eval (p->interp, rq->callback);
4058 xfree (rq->buf_out);
4059 xfree (rq->callback);
4060 xfree (rq->object_name);
4062 odr_reset (p->odr_in);
4063 if (p->ref_count == 1)
4065 ir_obj_delete ((ClientData) p);
4068 ir_obj_delete ((ClientData) p);
4069 } while (p->cs_link && cs_more (p->cs_link));
4070 if (p->cs_link && p->request_queue && p->state == IR_TCL_R_Idle)
4071 ir_tcl_send_q (p, p->request_queue, "x");
4075 * ir_select_write: handle outgoing packages - not yet written.
4077 static int ir_select_write (ClientData clientData)
4079 IrTcl_Obj *p = (IrTcl_Obj *) clientData;
4083 logf (LOG_DEBUG, "Write handler fd=%d", cs_fileno(p->cs_link));
4084 if (p->state == IR_TCL_R_Connecting)
4086 logf(LOG_DEBUG, "write: connect");
4087 r = cs_rcvconnect (p->cs_link);
4090 logf (LOG_DEBUG, "cs_rcvconnect returned 1");
4093 p->state = IR_TCL_R_Idle;
4095 ir_select_remove_write (cs_fileno (p->cs_link), p);
4098 logf (LOG_DEBUG, "cs_rcvconnect error");
4099 ir_tcl_disconnect (p);
4102 p->failInfo = IR_TCL_FAIL_CONNECT;
4103 ir_tcl_eval (p->interp, p->failback);
4105 ir_obj_delete ((ClientData) p);
4109 ir_tcl_eval (p->interp, p->callback);
4110 ir_obj_delete ((ClientData) p);
4113 rq = p->request_queue;
4114 if (!rq || !rq->buf_out)
4117 if ((r=cs_put (p->cs_link, rq->buf_out, rq->len_out)) < 0)
4119 logf (LOG_DEBUG, "cs_put write fail");
4121 xfree (rq->buf_out);
4123 ir_tcl_disconnect (p);
4126 p->failInfo = IR_TCL_FAIL_WRITE;
4127 ir_tcl_eval (p->interp, p->failback);
4129 ir_obj_delete ((ClientData) p);
4131 else if (r == 0) /* remove select bit */
4133 logf (LOG_DEBUG, "Write completed");
4134 p->state = IR_TCL_R_Waiting;
4135 ir_select_remove_write (cs_fileno (p->cs_link), p);
4136 xfree (rq->buf_out);
4142 static void ir_select_notify (ClientData clientData, int r, int w, int e)
4146 if (!ir_select_write (clientData) && r)
4147 ir_select_read (clientData);
4151 ir_select_read (clientData);
4155 /*----------------------------------------------------------- */
4159 * This wrapper function is used by Windows to invoke the
4160 * initialization code for the DLL. If we are compiling
4161 * with Visual C++, this routine will be renamed to DllMain.
4173 DllEntryPoint(hInst, reason, reserved)
4174 HINSTANCE hInst; /* Library instance handle. */
4175 DWORD reason; /* Reason this function is being called. */
4176 LPVOID reserved; /* Not used. */
4182 /* ------------------------------------------------------- */
4184 * Irtcl_init: Registration of TCL commands.
4186 EXPORT (int,Irtcl_Init) (Tcl_Interp *interp)
4188 Tcl_CreateCommand (interp, "ir", ir_obj_mk, (ClientData) NULL,
4189 (Tcl_CmdDeleteProc *) NULL);
4190 Tcl_CreateCommand (interp, "ir-set", ir_set_obj_mk,
4191 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
4192 Tcl_CreateCommand (interp, "ir-scan", ir_scan_obj_mk,
4193 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
4194 Tcl_CreateCommand (interp, "ir-log-init", ir_log_init_proc,
4195 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
4196 Tcl_CreateCommand (interp, "ir-log", ir_log_proc,
4197 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);