2 * IR toolkit for tcl/tk
4 * See the file LICENSE for details.
5 * Sebastian Hammer, Adam Dickmeiss
8 * Revision 1.61 1995-10-18 16:42:42 adam
9 * New settings: smallSetElementSetNames and mediumSetElementSetNames.
11 * Revision 1.60 1995/10/18 15:43:31 adam
12 * In search: mediumSetElementSetNames and smallSetElementSetNames are
13 * set to elementSetNames.
15 * Revision 1.59 1995/10/17 12:18:58 adam
16 * Bug fix: when target connection closed, the connection was not
17 * properly reestablished.
19 * Revision 1.58 1995/10/16 17:00:55 adam
20 * New setting: elementSetNames.
21 * Various client improvements. Medium presentation format looks better.
23 * Revision 1.57 1995/09/21 13:11:51 adam
24 * Support of dynamic loading.
25 * Test script uses load command if necessary.
27 * Revision 1.56 1995/08/29 15:30:14 adam
28 * Work on GRS records.
30 * Revision 1.55 1995/08/28 09:43:25 adam
31 * Minor changes. configure only searches for yaz beta 3 and versions after
34 * Revision 1.54 1995/08/24 12:25:16 adam
35 * Modified to work with yaz 1.0b3.
37 * Revision 1.53 1995/08/04 12:49:26 adam
38 * Bug fix: reading uninitialized variable p.
40 * Revision 1.52 1995/08/04 11:32:38 adam
41 * More work on output queue. Memory related routines moved
44 * Revision 1.51 1995/08/03 13:22:54 adam
47 * Revision 1.50 1995/07/20 08:09:49 adam
48 * client.tcl: Targets removed from hotTargets list when targets
49 * are removed/modified.
50 * ir-tcl.c: More work on triggerResourceControl.
52 * Revision 1.49 1995/06/30 12:39:21 adam
53 * Bug fix: loadFile didn't set record type.
54 * The MARC routines are a little less strict in the interpretation.
55 * Script display.tcl replaces the old marc.tcl.
56 * New interactive script: shell.tcl.
58 * Revision 1.48 1995/06/27 19:03:50 adam
59 * Bug fix in do_present in ir-tcl.c: p->set_child member weren't set.
60 * nextResultSetPosition used instead of setOffset.
62 * Revision 1.47 1995/06/25 10:25:04 adam
63 * Working on triggerResourceControl. Description of compile/install
64 * procedure moved to ir-tcl.sgml.
66 * Revision 1.46 1995/06/22 13:15:06 adam
67 * Feature: SUTRS. Setting getSutrs implemented.
68 * Work on display formats.
69 * Preferred record syntax can be set by the user.
71 * Revision 1.45 1995/06/20 08:07:30 adam
72 * New setting: failInfo.
73 * Working on better cancel mechanism.
75 * Revision 1.44 1995/06/19 17:01:20 adam
78 * Revision 1.43 1995/06/19 13:06:08 adam
79 * New define: IR_TCL_VERSION.
81 * Revision 1.42 1995/06/19 08:08:52 adam
82 * client.tcl: hotTargets now contain both database and target name.
83 * ir-tcl.c: setting protocol edited. Errors in callbacks are logged
84 * by logf(LOG_WARN, ...) calls.
86 * Revision 1.41 1995/06/16 12:28:16 adam
87 * Implemented preferredRecordSyntax.
88 * Minor changes in diagnostic handling.
89 * Record list deleted when connection closes.
91 * Revision 1.40 1995/06/14 13:37:18 adam
92 * Setting recordType implemented.
93 * Setting implementationVersion implemented.
94 * Settings implementationId / implementationName edited.
96 * Revision 1.39 1995/06/08 10:26:32 adam
97 * Bug fix in ir_strdup.
99 * Revision 1.38 1995/06/01 16:36:47 adam
100 * About buttons. Minor bug fixes.
102 * Revision 1.37 1995/06/01 07:31:20 adam
103 * Rename of many typedefs -> IrTcl_...
105 * Revision 1.36 1995/05/31 13:09:59 adam
106 * Client searches/presents may be interrupted.
107 * New moving book-logo.
109 * Revision 1.35 1995/05/31 08:36:33 adam
110 * Bug fix in client.tcl: didn't save options on clientrc.tcl.
111 * New method: referenceId. More work on scan.
113 * Revision 1.34 1995/05/29 10:33:42 adam
114 * README and rename of startup script.
116 * Revision 1.33 1995/05/29 09:15:11 quinn
117 * Changed CS_SR to PROTO_SR, etc.
119 * Revision 1.32 1995/05/29 08:44:16 adam
120 * Work on delete of objects.
122 * Revision 1.31 1995/05/26 11:44:10 adam
123 * Bugs fixed. More work on MARC utilities and queries. Test
124 * client is up-to-date again.
126 * Revision 1.30 1995/05/26 08:54:11 adam
127 * New MARC utilities. Uses prefix query.
129 * Revision 1.29 1995/05/24 14:10:22 adam
130 * Work on idAuthentication, protocolVersion and options.
132 * Revision 1.28 1995/05/23 15:34:48 adam
133 * Many new settings, userInformationField, smallSetUpperBound, etc.
134 * A number of settings are inherited when ir-set is executed.
135 * This version is incompatible with the graphical test client (client.tcl).
137 * Revision 1.27 1995/05/11 15:34:47 adam
138 * Scan request changed a bit. This version works with RLG.
140 * Revision 1.26 1995/04/18 16:11:51 adam
141 * First version of graphical Scan. Some work on query-by-form.
143 * Revision 1.25 1995/04/17 09:37:17 adam
144 * Further development of scan.
146 * Revision 1.24 1995/04/11 14:16:42 adam
147 * Further work on scan. Response works. Entries aren't saved yet.
149 * Revision 1.23 1995/04/10 10:50:27 adam
150 * Result-set name defaults to suffix of ir-set name.
151 * Started working on scan. Not finished at this point.
153 * Revision 1.22 1995/03/31 10:43:03 adam
154 * More robust when getting bad MARC records.
156 * Revision 1.21 1995/03/31 08:56:37 adam
157 * New button "Search".
159 * Revision 1.20 1995/03/29 16:07:09 adam
160 * Bug fix: Didn't use setName in present request.
162 * Revision 1.19 1995/03/28 12:45:23 adam
163 * New ir method failback: called on disconnect/protocol error.
164 * New ir set/get method: protocol: SR / Z3950.
165 * Simple popup and disconnect when failback is invoked.
167 * Revision 1.18 1995/03/21 15:50:12 adam
170 * Revision 1.17 1995/03/21 13:41:03 adam
171 * Comstack cs_create not used too often. Non-blocking connect.
173 * Revision 1.16 1995/03/21 08:26:06 adam
174 * New method, setName, to specify the result set name (other than Default).
175 * New method, responseStatus, which returns diagnostic info, if any, after
176 * present response / search response.
178 * Revision 1.15 1995/03/20 15:24:07 adam
179 * Diagnostic records saved on searchResponse.
181 * Revision 1.14 1995/03/20 08:53:22 adam
182 * Event loop in tclmain.c rewritten. New method searchStatus.
184 * Revision 1.13 1995/03/17 18:26:17 adam
185 * Non-blocking i/o used now. Database names popup as cascade items.
187 * Revision 1.12 1995/03/17 15:45:00 adam
188 * Improved target/database setup.
190 * Revision 1.11 1995/03/16 17:54:03 adam
191 * Minor changes really.
193 * Revision 1.10 1995/03/15 16:14:50 adam
194 * Blocking arg in cs_create changed.
196 * Revision 1.9 1995/03/15 13:59:24 adam
199 * Revision 1.8 1995/03/15 08:25:16 adam
200 * New method presentStatus to check for error on present. Misc. cleanup
201 * of IrTcl_RecordList manipulations. Full MARC record presentation in
204 * Revision 1.7 1995/03/14 17:32:29 adam
205 * Presentation of full Marc record in popup window.
207 * Revision 1.6 1995/03/12 19:31:55 adam
208 * Pattern matching implemented when retrieving MARC records. More
209 * diagnostic functions.
211 * Revision 1.5 1995/03/10 18:00:15 adam
212 * Actual presentation in line-by-line format. RPN query support.
214 * Revision 1.4 1995/03/09 16:15:08 adam
215 * First presentRequest attempts. Hot-target list.
221 #include <sys/time.h>
231 int (*method) (void *obj, Tcl_Interp *interp, int argc, char **argv);
239 static void ir_deleteDiags (IrTcl_Diagnostic **dst_list, int *dst_num);
240 static int do_disconnect (void *obj, Tcl_Interp *interp,
241 int argc, char **argv);
243 static IrTcl_RecordList *new_IR_record (IrTcl_SetObj *setobj,
246 IrTcl_RecordList *rl;
248 for (rl = setobj->record_list; rl; rl = rl->next)
254 case Z_NamePlusRecord_databaseRecord:
255 free (rl->u.dbrec.buf);
256 rl->u.dbrec.buf = NULL;
258 case Z_NamePlusRecord_surrogateDiagnostic:
259 ir_deleteDiags (&rl->u.surrogateDiagnostics.list,
260 &rl->u.surrogateDiagnostics.num);
268 rl = ir_tcl_malloc (sizeof(*rl));
269 rl->next = setobj->record_list;
271 setobj->record_list = rl;
278 enum oid_value value;
280 } IrTcl_recordSyntaxTab[] = {
281 { VAL_UNIMARC, "UNIMARC" },
282 { VAL_INTERMARC, "INTERMARC" },
284 { VAL_USMARC, "USMARC" },
285 { VAL_UKMARC, "UKMARC" },
286 { VAL_NORMARC, "NORMARC" },
287 { VAL_LIBRISMARC, "LIBRISMARC" },
288 { VAL_DANMARC, "DANMARC" },
289 { VAL_FINMARC, "FINMARC" },
291 { VAL_CANMARC, "CANMARC" },
293 { VAL_PICAMARC, "PICAMARC" },
294 { VAL_AUSMARC, "AUSMARC" },
295 { VAL_IBERMARC, "IBERMARC" },
296 { VAL_SUTRS, "SUTRS" },
297 { VAL_GRS1, "GRS1" },
304 int IrTcl_eval (Tcl_Interp *interp, const char *command)
306 char *tmp = ir_tcl_malloc (strlen(command)+1);
309 strcpy (tmp, command);
310 r = Tcl_Eval (interp, tmp);
312 logf (LOG_WARN, "Tcl error in line %d: %s", interp->errorLine,
314 Tcl_FreeResult (interp);
320 * IrTcl_getRecordSyntaxStr: Return record syntax name of object id
322 static const char *IrTcl_getRecordSyntaxStr (enum oid_value value)
325 for (i = 0; IrTcl_recordSyntaxTab[i].name; i++)
326 if (IrTcl_recordSyntaxTab[i].value == value)
327 return IrTcl_recordSyntaxTab[i].name;
332 * IrTcl_getRecordSyntaxVal: Return record syntax value of string
334 static enum oid_value IrTcl_getRecordSyntaxVal (const char *name)
337 for (i = 0; IrTcl_recordSyntaxTab[i].name; i++)
338 if (!strcmp (IrTcl_recordSyntaxTab[i].name, name))
339 return IrTcl_recordSyntaxTab[i].value;
343 static IrTcl_RecordList *find_IR_record (IrTcl_SetObj *setobj, int no)
345 IrTcl_RecordList *rl;
347 for (rl = setobj->record_list; rl; rl = rl->next)
353 static void delete_IR_records (IrTcl_SetObj *setobj)
355 IrTcl_RecordList *rl, *rl1;
357 for (rl = setobj->record_list; rl; rl = rl1)
361 case Z_NamePlusRecord_databaseRecord:
362 free (rl->u.dbrec.buf);
364 case Z_NamePlusRecord_surrogateDiagnostic:
365 ir_deleteDiags (&rl->u.surrogateDiagnostics.list,
366 &rl->u.surrogateDiagnostics.num);
372 setobj->record_list = NULL;
376 * get_set_int: Set/get integer value
378 static int get_set_int (int *val, Tcl_Interp *interp, int argc, char **argv)
384 if (Tcl_GetInt (interp, argv[2], val)==TCL_ERROR)
387 sprintf (buf, "%d", *val);
388 Tcl_AppendResult (interp, buf, NULL);
393 * ir_method: Search for method in table and invoke method handler
395 int ir_method (Tcl_Interp *interp, int argc, char **argv, IrTcl_Methods *tab)
397 IrTcl_Methods *tab_i = tab;
400 for (tab_i = tab; tab_i->tab; tab_i++)
401 for (t = tab_i->tab; t->name; t++)
404 if ((*t->method)(tab_i->obj, interp, argc, argv) == TCL_ERROR)
408 if (!strcmp (t->name, argv[1]))
409 return (*t->method)(tab_i->obj, interp, argc, argv);
413 Tcl_AppendResult (interp, "Bad method: ", argv[1],
414 ". Possible methods:", NULL);
415 for (tab_i = tab; tab_i->tab; tab_i++)
416 for (t = tab_i->tab; t->name; t++)
417 Tcl_AppendResult (interp, " ", t->name, NULL);
422 * ir_method_r: Get status for all readable elements
424 int ir_method_r (void *obj, Tcl_Interp *interp, int argc, char **argv,
432 for (; tab->name; tab++)
435 argv_n[1] = tab->name;
436 Tcl_AppendResult (interp, "{", NULL);
437 (*tab->method)(obj, interp, argc_n, argv_n);
438 Tcl_AppendResult (interp, "} ", NULL);
444 * ir_named_bits: get/set named bits
446 int ir_named_bits (struct ir_named_entry *tab, Odr_bitmask *ob,
447 Tcl_Interp *interp, int argc, char **argv)
449 struct ir_named_entry *ti;
454 for (no = 0; no < argc; no++)
456 for (ti = tab; ti->name; ti++)
457 if (!strcmp (argv[no], ti->name))
459 ODR_MASK_SET (ob, ti->pos);
464 Tcl_AppendResult (interp, "Bad bit mask: ", argv[no], NULL);
470 for (ti = tab; ti->name; ti++)
471 if (ODR_MASK_GET (ob, ti->pos))
472 Tcl_AppendElement (interp, ti->name);
476 static void set_referenceId (ODR o, Z_ReferenceId **dst, const char *src)
482 *dst = odr_malloc (o, sizeof(**dst));
483 (*dst)->size = (*dst)->len = strlen(src);
484 (*dst)->buf = odr_malloc (o, (*dst)->len);
485 memcpy ((*dst)->buf, src, (*dst)->len);
489 static void get_referenceId (char **dst, Z_ReferenceId *src)
497 *dst = ir_tcl_malloc (src->len+1);
498 memcpy (*dst, src->buf, src->len);
499 (*dst)[src->len] = '\0';
502 /* ------------------------------------------------------- */
505 * do_init_request: init method on IR object
507 static int do_init_request (void *obj, Tcl_Interp *interp,
508 int argc, char **argv)
518 interp->result = "init: not connected";
521 apdu = zget_APDU (p->odr_out, Z_APDU_initRequest);
522 req = apdu->u.initRequest;
524 set_referenceId (p->odr_out, &req->referenceId, p->set_inher.referenceId);
525 req->options = &p->options;
526 req->protocolVersion = &p->protocolVersion;
527 req->preferredMessageSize = &p->preferredMessageSize;
528 req->maximumRecordSize = &p->maximumRecordSize;
530 if (p->idAuthenticationGroupId)
532 Z_IdPass *pass = odr_malloc (p->odr_out, sizeof(*pass));
533 Z_IdAuthentication *auth = odr_malloc (p->odr_out, sizeof(*auth));
535 auth->which = Z_IdAuthentication_idPass;
536 auth->u.idPass = pass;
537 if (p->idAuthenticationGroupId && *p->idAuthenticationGroupId)
538 pass->groupId = p->idAuthenticationGroupId;
540 pass->groupId = NULL;
541 if (p->idAuthenticationUserId && *p->idAuthenticationUserId)
542 pass->userId = p->idAuthenticationUserId;
545 if (p->idAuthenticationPassword && *p->idAuthenticationPassword)
546 pass->password = p->idAuthenticationPassword;
548 pass->password = NULL;
549 req->idAuthentication = auth;
551 else if (!p->idAuthenticationOpen || !*p->idAuthenticationOpen)
552 req->idAuthentication = NULL;
555 Z_IdAuthentication *auth = odr_malloc (p->odr_out, sizeof(*auth));
557 auth->which = Z_IdAuthentication_open;
558 auth->u.open = p->idAuthenticationOpen;
559 req->idAuthentication = auth;
561 req->implementationId = p->implementationId;
562 req->implementationName = p->implementationName;
563 req->implementationVersion = p->implementationVersion;
564 req->userInformationField = 0;
566 return ir_tcl_send_APDU (interp, p, apdu, "init", argv[0]);
570 * do_protocolVersion: Set protocol Version
572 static int do_protocolVersion (void *obj, Tcl_Interp *interp,
573 int argc, char **argv)
581 ODR_MASK_ZERO (&p->protocolVersion);
582 ODR_MASK_SET (&p->protocolVersion, 0);
583 ODR_MASK_SET (&p->protocolVersion, 1);
588 if (Tcl_GetInt (interp, argv[2], &version)==TCL_ERROR)
590 ODR_MASK_ZERO (&p->protocolVersion);
591 for (i = 0; i<version; i++)
592 ODR_MASK_SET (&p->protocolVersion, i);
594 for (i = 4; --i >= 0; )
595 if (ODR_MASK_GET (&p->protocolVersion, i))
597 sprintf (buf, "%d", i+1);
598 interp->result = buf;
603 * do_options: Set options
605 static int do_options (void *obj, Tcl_Interp *interp,
606 int argc, char **argv)
608 static struct ir_named_entry options_tab[] = {
612 { "resourceReport", 3 },
613 { "triggerResourceCtrl", 4},
614 { "resourceCtrl", 5},
618 { "extendedServices", 10},
619 { "level-1Segmentation", 11},
620 { "level-2Segmentation", 12},
621 { "concurrentOperations", 13},
622 { "namedResultSets", 14},
629 ODR_MASK_ZERO (&p->options);
630 ODR_MASK_SET (&p->options, 0);
631 ODR_MASK_SET (&p->options, 1);
632 ODR_MASK_SET (&p->options, 4);
633 ODR_MASK_SET (&p->options, 7);
634 ODR_MASK_SET (&p->options, 14);
637 return ir_named_bits (options_tab, &p->options, interp, argc-2, argv+2);
641 * do_failInfo: Get fail information
643 static int do_failInfo (void *obj, Tcl_Interp *interp, int argc, char **argv)
653 sprintf (buf, "%d", p->failInfo);
659 case IR_TCL_FAIL_CONNECT:
660 cp = "connect failed";
662 case IR_TCL_FAIL_READ:
663 cp = "connection closed";
665 case IR_TCL_FAIL_WRITE:
666 cp = "connection closed";
668 case IR_TCL_FAIL_IN_APDU:
669 cp = "failed to decode incoming APDU";
671 case IR_TCL_FAIL_UNKNOWN_APDU:
677 Tcl_AppendElement (interp, buf);
678 Tcl_AppendElement (interp, cp);
683 * do_preferredMessageSize: Set/get preferred message size
685 static int do_preferredMessageSize (void *obj, Tcl_Interp *interp,
686 int argc, char **argv)
692 p->preferredMessageSize = 30000;
695 return get_set_int (&p->preferredMessageSize, interp, argc, argv);
699 * do_maximumRecordSize: Set/get maximum record size
701 static int do_maximumRecordSize (void *obj, Tcl_Interp *interp,
702 int argc, char **argv)
708 p->maximumRecordSize = 30000;
711 return get_set_int (&p->maximumRecordSize, interp, argc, argv);
715 * do_initResult: Get init result
717 static int do_initResult (void *obj, Tcl_Interp *interp,
718 int argc, char **argv)
724 return get_set_int (&p->initResult, interp, argc, argv);
729 * do_implementationName: Set/get Implementation Name.
731 static int do_implementationName (void *obj, Tcl_Interp *interp,
732 int argc, char **argv)
737 return ir_tcl_strdup (interp, &p->implementationName,
738 "Index Data/IrTcl on YAZ");
740 return ir_tcl_strdel (interp, &p->implementationName);
743 free (p->implementationName);
744 if (ir_tcl_strdup (interp, &p->implementationName, argv[2])
748 Tcl_AppendResult (interp, p->implementationName, (char*) NULL);
753 * do_implementationId: Get Implementation Id.
755 static int do_implementationId (void *obj, Tcl_Interp *interp,
756 int argc, char **argv)
761 return ir_tcl_strdup (interp, &p->implementationId, "YAZ (id=81)");
763 return ir_tcl_strdel (interp, &p->implementationId);
764 Tcl_AppendResult (interp, p->implementationId, (char*) NULL);
769 * do_implementationVersion: get Implementation Version.
771 static int do_implementationVersion (void *obj, Tcl_Interp *interp,
772 int argc, char **argv)
777 return ir_tcl_strdup (interp, &p->implementationVersion,
778 "YAZ: " YAZ_VERSION " / IrTcl: " IR_TCL_VERSION);
780 return ir_tcl_strdel (interp, &p->implementationVersion);
781 Tcl_AppendResult (interp, p->implementationVersion, (char*) NULL);
786 * do_targetImplementationName: Get Implementation Name of target.
788 static int do_targetImplementationName (void *obj, Tcl_Interp *interp,
789 int argc, char **argv)
795 p->targetImplementationName = NULL;
799 return ir_tcl_strdel (interp, &p->targetImplementationName);
800 Tcl_AppendResult (interp, p->targetImplementationName, (char*) NULL);
805 * do_targetImplementationId: Get Implementation Id of target
807 static int do_targetImplementationId (void *obj, Tcl_Interp *interp,
808 int argc, char **argv)
814 p->targetImplementationId = NULL;
818 return ir_tcl_strdel (interp, &p->targetImplementationId);
819 Tcl_AppendResult (interp, p->targetImplementationId, (char*) NULL);
824 * do_targetImplementationVersion: Get Implementation Version of target
826 static int do_targetImplementationVersion (void *obj, Tcl_Interp *interp,
827 int argc, char **argv)
833 p->targetImplementationVersion = NULL;
837 return ir_tcl_strdel (interp, &p->targetImplementationVersion);
838 Tcl_AppendResult (interp, p->targetImplementationVersion, (char*) NULL);
843 * do_idAuthentication: Set/get id Authentication
845 static int do_idAuthentication (void *obj, Tcl_Interp *interp,
846 int argc, char **argv)
850 if (argc >= 3 || argc == -1)
852 free (p->idAuthenticationOpen);
853 free (p->idAuthenticationGroupId);
854 free (p->idAuthenticationUserId);
855 free (p->idAuthenticationPassword);
857 if (argc >= 3 || argc <= 0)
859 p->idAuthenticationOpen = NULL;
860 p->idAuthenticationGroupId = NULL;
861 p->idAuthenticationUserId = NULL;
862 p->idAuthenticationPassword = NULL;
870 if (ir_tcl_strdup (interp, &p->idAuthenticationOpen, argv[2])
876 if (ir_tcl_strdup (interp, &p->idAuthenticationGroupId, argv[2])
879 if (ir_tcl_strdup (interp, &p->idAuthenticationUserId, argv[3])
882 if (ir_tcl_strdup (interp, &p->idAuthenticationPassword, argv[4])
887 if (p->idAuthenticationOpen)
888 Tcl_AppendElement (interp, p->idAuthenticationOpen);
889 else if (p->idAuthenticationGroupId)
891 Tcl_AppendElement (interp, p->idAuthenticationGroupId);
892 Tcl_AppendElement (interp, p->idAuthenticationUserId);
893 Tcl_AppendElement (interp, p->idAuthenticationPassword);
899 * do_connect: connect method on IR object
901 static int do_connect (void *obj, Tcl_Interp *interp,
902 int argc, char **argv)
914 interp->result = "already connected";
917 if (ir_tcl_strdup (interp, &p->hostname, argv[2]) == TCL_ERROR)
919 if (!strcmp (p->cs_type, "tcpip"))
921 p->cs_link = cs_create (tcpip_type, CS_BLOCK, p->protocol_type);
922 addr = tcpip_strtoaddr (argv[2]);
925 interp->result = "tcpip_strtoaddr fail";
928 logf (LOG_DEBUG, "tcp/ip connect %s", argv[2]);
930 else if (!strcmp (p->cs_type, "mosi"))
933 p->cs_link = cs_create (mosi_type, CS_BLOCK, p->protocol_type);
934 addr = mosi_strtoaddr (argv[2]);
937 interp->result = "mosi_strtoaddr fail";
940 logf (LOG_DEBUG, "mosi connect %s", argv[2]);
942 interp->result = "MOSI support not there";
948 Tcl_AppendResult (interp, "Bad comstack type: ",
952 if ((r=cs_connect (p->cs_link, addr)) < 0)
954 interp->result = "connect fail";
955 do_disconnect (p, NULL, 2, NULL);
958 ir_select_add (cs_fileno (p->cs_link), p);
961 ir_select_add_write (cs_fileno (p->cs_link), p);
962 p->state = IR_TCL_R_Connecting;
966 p->state = IR_TCL_R_Idle;
968 IrTcl_eval (p->interp, p->callback);
972 Tcl_AppendResult (interp, p->hostname, NULL);
977 * do_disconnect: disconnect method on IR object
979 static int do_disconnect (void *obj, Tcl_Interp *interp,
980 int argc, char **argv)
986 p->state = IR_TCL_R_Idle;
995 ir_select_remove_write (cs_fileno (p->cs_link), p);
996 ir_select_remove (cs_fileno (p->cs_link), p);
998 odr_reset (p->odr_in);
1000 assert (p->cs_link);
1001 cs_close (p->cs_link);
1004 ODR_MASK_ZERO (&p->options);
1005 ODR_MASK_SET (&p->options, 0);
1006 ODR_MASK_SET (&p->options, 1);
1007 ODR_MASK_SET (&p->options, 4);
1008 ODR_MASK_SET (&p->options, 7);
1009 ODR_MASK_SET (&p->options, 14);
1011 ODR_MASK_ZERO (&p->protocolVersion);
1012 ODR_MASK_SET (&p->protocolVersion, 0);
1013 ODR_MASK_SET (&p->protocolVersion, 1);
1016 assert (!p->cs_link);
1021 * do_comstack: Set/get comstack method on IR object
1023 static int do_comstack (void *o, Tcl_Interp *interp,
1024 int argc, char **argv)
1029 return ir_tcl_strdup (interp, &obj->cs_type, "tcpip");
1030 else if (argc == -1)
1031 return ir_tcl_strdel (interp, &obj->cs_type);
1034 free (obj->cs_type);
1035 if (ir_tcl_strdup (interp, &obj->cs_type, argv[2]) == TCL_ERROR)
1038 Tcl_AppendElement (interp, obj->cs_type);
1043 * do_logLevel: Set log level
1045 static int do_logLevel (void *o, Tcl_Interp *interp,
1046 int argc, char **argv)
1051 log_init (log_mask_str (argv[2]), "", NULL);
1053 log_init (log_mask_str (argv[2]), argv[3], NULL);
1055 log_init (log_mask_str (argv[2]), argv[3], argv[4]);
1061 * do_callback: add callback
1063 static int do_callback (void *obj, Tcl_Interp *interp,
1064 int argc, char **argv)
1073 else if (argc == -1)
1074 return ir_tcl_strdel (interp, &p->callback);
1080 if (ir_tcl_strdup (interp, &p->callback, argv[2]) == TCL_ERROR)
1090 * do_failback: add error handle callback
1092 static int do_failback (void *obj, Tcl_Interp *interp,
1093 int argc, char **argv)
1102 else if (argc == -1)
1103 return ir_tcl_strdel (interp, &p->failback);
1109 if (ir_tcl_strdup (interp, &p->failback, argv[2]) == TCL_ERROR)
1119 * do_protocol: Set/get protocol method on IR object
1121 static int do_protocol (void *o, Tcl_Interp *interp, int argc, char **argv)
1127 p->protocol_type = PROTO_Z3950;
1132 if (!strcmp (argv[2], "Z39"))
1133 p->protocol_type = PROTO_Z3950;
1134 else if (!strcmp (argv[2], "SR"))
1135 p->protocol_type = PROTO_SR;
1138 Tcl_AppendResult (interp, "Bad protocol: ", argv[2], NULL);
1143 switch (p->protocol_type)
1146 Tcl_AppendElement (interp, "Z39");
1149 Tcl_AppendElement (interp, "SR");
1156 * do_triggerResourceControl:
1158 static int do_triggerResourceControl (void *obj, Tcl_Interp *interp,
1159 int argc, char **argv)
1163 Z_TriggerResourceControlRequest *req;
1164 bool_t is_false = 0;
1170 interp->result = "triggerResourceControl: not connected";
1173 apdu = zget_APDU (p->odr_out, Z_APDU_triggerResourceControlRequest);
1174 req = apdu->u.triggerResourceControlRequest;
1175 *req->requestedAction = Z_TriggerResourceCtrl_cancel;
1176 req->resultSetWanted = &is_false;
1178 return ir_tcl_send_APDU (interp, p, apdu, "triggerResourceControl",
1183 * do_databaseNames: specify database names
1185 static int do_databaseNames (void *obj, Tcl_Interp *interp,
1186 int argc, char **argv)
1189 IrTcl_SetCObj *p = obj;
1193 for (i=0; i<p->num_databaseNames; i++)
1194 free (p->databaseNames[i]);
1195 free (p->databaseNames);
1199 p->num_databaseNames = 0;
1200 p->databaseNames = NULL;
1205 for (i=0; i<p->num_databaseNames; i++)
1206 Tcl_AppendElement (interp, p->databaseNames[i]);
1209 if (p->databaseNames)
1211 for (i=0; i<p->num_databaseNames; i++)
1212 free (p->databaseNames[i]);
1213 free (p->databaseNames);
1215 p->num_databaseNames = argc - 2;
1217 ir_tcl_malloc (sizeof(*p->databaseNames) * p->num_databaseNames);
1218 for (i=0; i<p->num_databaseNames; i++)
1220 if (ir_tcl_strdup (interp, &p->databaseNames[i], argv[2+i])
1228 * do_replaceIndicator: Set/get replace Set indicator
1230 static int do_replaceIndicator (void *obj, Tcl_Interp *interp,
1231 int argc, char **argv)
1233 IrTcl_SetCObj *p = obj;
1237 p->replaceIndicator = 1;
1240 return get_set_int (&p->replaceIndicator, interp, argc, argv);
1244 * do_queryType: Set/Get query method
1246 static int do_queryType (void *obj, Tcl_Interp *interp,
1247 int argc, char **argv)
1249 IrTcl_SetCObj *p = obj;
1252 return ir_tcl_strdup (interp, &p->queryType, "rpn");
1253 else if (argc == -1)
1254 return ir_tcl_strdel (interp, &p->queryType);
1257 free (p->queryType);
1258 if (ir_tcl_strdup (interp, &p->queryType, argv[2]) == TCL_ERROR)
1261 Tcl_AppendResult (interp, p->queryType, NULL);
1266 * do_userInformationField: Get User information field
1268 static int do_userInformationField (void *obj, Tcl_Interp *interp,
1269 int argc, char **argv)
1275 p->userInformationField = NULL;
1278 else if (argc == -1)
1279 return ir_tcl_strdel (interp, &p->userInformationField);
1280 Tcl_AppendResult (interp, p->userInformationField, NULL);
1285 * do_smallSetUpperBound: Set/get small set upper bound
1287 static int do_smallSetUpperBound (void *o, Tcl_Interp *interp,
1288 int argc, char **argv)
1290 IrTcl_SetCObj *p = o;
1294 p->smallSetUpperBound = 0;
1297 return get_set_int (&p->smallSetUpperBound, interp, argc, argv);
1301 * do_largeSetLowerBound: Set/get large set lower bound
1303 static int do_largeSetLowerBound (void *o, Tcl_Interp *interp,
1304 int argc, char **argv)
1306 IrTcl_SetCObj *p = o;
1310 p->largeSetLowerBound = 2;
1313 return get_set_int (&p->largeSetLowerBound, interp, argc, argv);
1317 * do_mediumSetPresentNumber: Set/get large set lower bound
1319 static int do_mediumSetPresentNumber (void *o, Tcl_Interp *interp,
1320 int argc, char **argv)
1322 IrTcl_SetCObj *p = o;
1326 p->mediumSetPresentNumber = 0;
1329 return get_set_int (&p->mediumSetPresentNumber, interp, argc, argv);
1333 * do_referenceId: Set/Get referenceId
1335 static int do_referenceId (void *obj, Tcl_Interp *interp,
1336 int argc, char **argv)
1338 IrTcl_SetCObj *p = obj;
1342 p->referenceId = NULL;
1345 else if (argc == -1)
1346 return ir_tcl_strdel (interp, &p->referenceId);
1349 free (p->referenceId);
1350 if (ir_tcl_strdup (interp, &p->referenceId, argv[2]) == TCL_ERROR)
1353 Tcl_AppendResult (interp, p->referenceId, NULL);
1358 * do_preferredRecordSyntax: Set/get preferred record syntax
1360 static int do_preferredRecordSyntax (void *obj, Tcl_Interp *interp,
1361 int argc, char **argv)
1363 IrTcl_SetCObj *p = obj;
1367 p->preferredRecordSyntax = NULL;
1370 else if (argc == -1)
1372 free (p->preferredRecordSyntax);
1373 p->preferredRecordSyntax = NULL;
1378 free (p->preferredRecordSyntax);
1379 p->preferredRecordSyntax = NULL;
1380 if (argv[2][0] && (p->preferredRecordSyntax =
1381 ir_tcl_malloc (sizeof(*p->preferredRecordSyntax))))
1382 *p->preferredRecordSyntax = IrTcl_getRecordSyntaxVal (argv[2]);
1389 * do_elementSetNames: Set/Get element Set Names
1391 static int do_elementSetNames (void *obj, Tcl_Interp *interp,
1392 int argc, char **argv)
1394 IrTcl_SetCObj *p = obj;
1398 p->elementSetNames = NULL;
1401 else if (argc == -1)
1402 return ir_tcl_strdel (interp, &p->elementSetNames);
1405 free (p->elementSetNames);
1406 if (ir_tcl_strdup (interp, &p->elementSetNames, argv[2]) == TCL_ERROR)
1409 Tcl_AppendResult (interp, p->elementSetNames, NULL);
1414 * do_smallSetElementSetNames: Set/Get element Set Names
1416 static int do_smallSetElementSetNames (void *obj, Tcl_Interp *interp,
1417 int argc, char **argv)
1419 IrTcl_SetCObj *p = obj;
1423 p->smallSetElementSetNames = NULL;
1426 else if (argc == -1)
1427 return ir_tcl_strdel (interp, &p->smallSetElementSetNames);
1430 free (p->smallSetElementSetNames);
1431 if (ir_tcl_strdup (interp, &p->smallSetElementSetNames,
1432 argv[2]) == TCL_ERROR)
1435 Tcl_AppendResult (interp, p->smallSetElementSetNames, NULL);
1440 * do_mediumSetElementSetNames: Set/Get element Set Names
1442 static int do_mediumSetElementSetNames (void *obj, Tcl_Interp *interp,
1443 int argc, char **argv)
1445 IrTcl_SetCObj *p = obj;
1449 p->mediumSetElementSetNames = NULL;
1452 else if (argc == -1)
1453 return ir_tcl_strdel (interp, &p->mediumSetElementSetNames);
1456 free (p->mediumSetElementSetNames);
1457 if (ir_tcl_strdup (interp, &p->mediumSetElementSetNames,
1458 argv[2]) == TCL_ERROR)
1461 Tcl_AppendResult (interp, p->mediumSetElementSetNames, NULL);
1466 static IrTcl_Method ir_method_tab[] = {
1467 { 1, "comstack", do_comstack },
1468 { 1, "protocol", do_protocol },
1469 { 0, "failback", do_failback },
1470 { 0, "failInfo", do_failInfo },
1471 { 0, "logLevel", do_logLevel },
1473 { 1, "connect", do_connect },
1474 { 0, "protocolVersion", do_protocolVersion },
1475 { 1, "preferredMessageSize", do_preferredMessageSize },
1476 { 1, "maximumRecordSize", do_maximumRecordSize },
1477 { 1, "implementationName", do_implementationName },
1478 { 1, "implementationId", do_implementationId },
1479 { 1, "implementationVersion", do_implementationVersion },
1480 { 0, "targetImplementationName", do_targetImplementationName },
1481 { 0, "targetImplementationId", do_targetImplementationId },
1482 { 0, "targetImplementationVersion", do_targetImplementationVersion },
1483 { 0, "userInformationField", do_userInformationField },
1484 { 1, "idAuthentication", do_idAuthentication },
1485 { 0, "options", do_options },
1486 { 0, "init", do_init_request },
1487 { 0, "initResult", do_initResult },
1488 { 0, "disconnect", do_disconnect },
1489 { 0, "callback", do_callback },
1490 { 0, "triggerResourceControl", do_triggerResourceControl },
1494 static IrTcl_Method ir_set_c_method_tab[] = {
1495 { 0, "databaseNames", do_databaseNames},
1496 { 0, "replaceIndicator", do_replaceIndicator},
1497 { 0, "queryType", do_queryType },
1498 { 0, "preferredRecordSyntax", do_preferredRecordSyntax },
1499 { 0, "smallSetUpperBound", do_smallSetUpperBound},
1500 { 0, "largeSetLowerBound", do_largeSetLowerBound},
1501 { 0, "mediumSetPresentNumber", do_mediumSetPresentNumber},
1502 { 0, "referenceId", do_referenceId },
1503 { 0, "elementSetNames", do_elementSetNames },
1504 { 0, "smallSetElementSetNames", do_smallSetElementSetNames },
1505 { 0, "mediumSetElementSetNames", do_mediumSetElementSetNames },
1510 * ir_obj_method: IR Object methods
1512 static int ir_obj_method (ClientData clientData, Tcl_Interp *interp,
1513 int argc, char **argv)
1515 IrTcl_Methods tab[3];
1516 IrTcl_Obj *p = clientData;
1519 return ir_method_r (clientData, interp, argc, argv, ir_method_tab);
1521 tab[0].tab = ir_method_tab;
1523 tab[1].tab = ir_set_c_method_tab;
1524 tab[1].obj = &p->set_inher;
1527 return ir_method (interp, argc, argv, tab);
1531 * ir_obj_delete: IR Object disposal
1533 static void ir_obj_delete (ClientData clientData)
1535 IrTcl_Obj *obj = clientData;
1536 IrTcl_Methods tab[3];
1539 if (obj->ref_count > 0)
1541 assert (obj->ref_count == 0);
1543 logf (LOG_DEBUG, "ir object delete");
1544 tab[0].tab = ir_method_tab;
1546 tab[1].tab = ir_set_c_method_tab;
1547 tab[1].obj = &obj->set_inher;
1550 ir_method (NULL, -1, NULL, tab);
1553 odr_destroy (obj->odr_in);
1554 odr_destroy (obj->odr_out);
1555 odr_destroy (obj->odr_pr);
1560 * ir_obj_mk: IR Object creation
1562 static int ir_obj_mk (ClientData clientData, Tcl_Interp *interp,
1563 int argc, char **argv)
1565 IrTcl_Methods tab[3];
1573 interp->result = "wrong # args";
1576 obj = ir_tcl_malloc (sizeof(*obj));
1579 obj->bibset = ccl_qual_mk ();
1580 if ((inf = fopen ("default.bib", "r")))
1582 ccl_qual_file (obj->bibset, inf);
1587 logf (LOG_DEBUG, "ir object create");
1588 obj->odr_in = odr_createmem (ODR_DECODE);
1589 obj->odr_out = odr_createmem (ODR_ENCODE);
1590 obj->odr_pr = odr_createmem (ODR_PRINT);
1591 obj->state = IR_TCL_R_Idle;
1592 obj->interp = interp;
1596 obj->request_queue = NULL;
1598 tab[0].tab = ir_method_tab;
1600 tab[1].tab = ir_set_c_method_tab;
1601 tab[1].obj = &obj->set_inher;
1604 if (ir_method (interp, 0, NULL, tab) == TCL_ERROR)
1606 Tcl_CreateCommand (interp, argv[1], ir_obj_method,
1607 (ClientData) obj, ir_obj_delete);
1611 /* ------------------------------------------------------- */
1613 * do_search: Do search request
1615 static int do_search (void *o, Tcl_Interp *interp, int argc, char **argv)
1617 Z_SearchRequest *req;
1621 IrTcl_SetObj *obj = o;
1632 interp->result = "wrong # args";
1635 if (!obj->set_inher.num_databaseNames)
1637 interp->result = "no databaseNames";
1642 interp->result = "search: not connected";
1645 apdu = zget_APDU (p->odr_out, Z_APDU_searchRequest);
1646 req = apdu->u.searchRequest;
1650 bib1.proto = p->protocol_type;
1651 bib1.class = CLASS_ATTSET;
1652 bib1.value = VAL_BIB1;
1654 set_referenceId (p->odr_out, &req->referenceId,
1655 obj->set_inher.referenceId);
1657 req->smallSetUpperBound = &obj->set_inher.smallSetUpperBound;
1658 req->largeSetLowerBound = &obj->set_inher.largeSetLowerBound;
1659 req->mediumSetPresentNumber = &obj->set_inher.mediumSetPresentNumber;
1660 req->replaceIndicator = &obj->set_inher.replaceIndicator;
1661 req->resultSetName = obj->setName ? obj->setName : "Default";
1662 logf (LOG_DEBUG, "Search, resultSetName %s", req->resultSetName);
1663 req->num_databaseNames = obj->set_inher.num_databaseNames;
1664 req->databaseNames = obj->set_inher.databaseNames;
1665 for (r=0; r < obj->set_inher.num_databaseNames; r++)
1666 logf (LOG_DEBUG, " Database %s", obj->set_inher.databaseNames[r]);
1667 if (obj->set_inher.preferredRecordSyntax)
1669 struct oident ident;
1671 ident.proto = p->protocol_type;
1672 ident.class = CLASS_RECSYN;
1673 ident.value = *obj->set_inher.preferredRecordSyntax;
1674 logf (LOG_DEBUG, "Preferred record syntax is %d", ident.value);
1675 req->preferredRecordSyntax = odr_oiddup (p->odr_out,
1676 oid_getoidbyent (&ident));
1679 req->preferredRecordSyntax = 0;
1681 if (obj->set_inher.smallSetElementSetNames &&
1682 *obj->set_inher.smallSetElementSetNames)
1684 Z_ElementSetNames *esn = odr_malloc (p->odr_out, sizeof(*esn));
1686 esn->which = Z_ElementSetNames_generic;
1687 esn->u.generic = obj->set_inher.elementSetNames;
1688 req->smallSetElementSetNames = esn;
1691 req->smallSetElementSetNames = NULL;
1693 if (obj->set_inher.mediumSetElementSetNames &&
1694 *obj->set_inher.mediumSetElementSetNames)
1696 Z_ElementSetNames *esn = odr_malloc (p->odr_out, sizeof(*esn));
1698 esn->which = Z_ElementSetNames_generic;
1699 esn->u.generic = obj->set_inher.elementSetNames;
1700 req->mediumSetElementSetNames = esn;
1703 req->mediumSetElementSetNames = NULL;
1705 req->query = &query;
1707 if (!strcmp (obj->set_inher.queryType, "rpn"))
1709 Z_RPNQuery *RPNquery;
1711 RPNquery = p_query_rpn (p->odr_out, argv[2]);
1714 Tcl_AppendResult (interp, "Syntax error in query", NULL);
1717 RPNquery->attributeSetId = oid_getoidbyent (&bib1);
1718 query.which = Z_Query_type_1;
1719 query.u.type_1 = RPNquery;
1720 logf (LOG_DEBUG, "RPN");
1723 else if (!strcmp (obj->set_inher.queryType, "cclrpn"))
1727 struct ccl_rpn_node *rpn;
1728 Z_RPNQuery *RPNquery;
1730 rpn = ccl_find_str(p->bibset, argv[2], &error, &pos);
1733 Tcl_AppendResult (interp, "CCL error: ",
1734 ccl_err_msg(error), NULL);
1737 ccl_pr_tree (rpn, stderr);
1738 fprintf (stderr, "\n");
1739 assert((RPNquery = ccl_rpn_query(rpn)));
1740 RPNquery->attributeSetId = oid_getoidbyent (&bib1);
1741 query.which = Z_Query_type_1;
1742 query.u.type_1 = RPNquery;
1743 logf (LOG_DEBUG, "CCLRPN");
1746 else if (!strcmp (obj->set_inher.queryType, "ccl"))
1748 query.which = Z_Query_type_2;
1749 query.u.type_2 = &ccl_query;
1750 ccl_query.buf = (unsigned char *) argv[2];
1751 ccl_query.len = strlen (argv[2]);
1752 logf (LOG_DEBUG, "CCL");
1756 interp->result = "unknown query method";
1759 return ir_tcl_send_APDU (interp, p, apdu, "search", argv[0]);
1763 * do_resultCount: Get number of hits
1765 static int do_resultCount (void *o, Tcl_Interp *interp,
1766 int argc, char **argv)
1768 IrTcl_SetObj *obj = o;
1772 return get_set_int (&obj->resultCount, interp, argc, argv);
1776 * do_searchStatus: Get search status (after search response)
1778 static int do_searchStatus (void *o, Tcl_Interp *interp,
1779 int argc, char **argv)
1781 IrTcl_SetObj *obj = o;
1785 return get_set_int (&obj->searchStatus, interp, argc, argv);
1789 * do_presentStatus: Get search status (after search/present response)
1791 static int do_presentStatus (void *o, Tcl_Interp *interp,
1792 int argc, char **argv)
1794 IrTcl_SetObj *obj = o;
1798 return get_set_int (&obj->presentStatus, interp, argc, argv);
1802 * do_nextResultSetPosition: Get next result set position
1803 * (after search/present response)
1805 static int do_nextResultSetPosition (void *o, Tcl_Interp *interp,
1806 int argc, char **argv)
1808 IrTcl_SetObj *obj = o;
1812 obj->nextResultSetPosition = 0;
1815 return get_set_int (&obj->nextResultSetPosition, interp, argc, argv);
1819 * do_setName: Set result Set name
1821 static int do_setName (void *o, Tcl_Interp *interp,
1822 int argc, char **argv)
1824 IrTcl_SetObj *obj = o;
1827 return ir_tcl_strdup (interp, &obj->setName, "Default");
1828 else if (argc == -1)
1829 return ir_tcl_strdel (interp, &obj->setName);
1832 free (obj->setName);
1833 if (ir_tcl_strdup (interp, &obj->setName, argv[2])
1837 Tcl_AppendElement (interp, obj->setName);
1842 * do_numberOfRecordsReturned: Get number of records returned
1844 static int do_numberOfRecordsReturned (void *o, Tcl_Interp *interp,
1845 int argc, char **argv)
1847 IrTcl_SetObj *obj = o;
1851 obj->numberOfRecordsReturned = 0;
1854 return get_set_int (&obj->numberOfRecordsReturned, interp, argc, argv);
1858 * do_type: Return type (if any) at position.
1860 static int do_type (void *o, Tcl_Interp *interp, int argc, char **argv)
1862 IrTcl_SetObj *obj = o;
1864 IrTcl_RecordList *rl;
1868 obj->record_list = NULL;
1871 else if (argc == -1)
1873 delete_IR_records (obj);
1878 sprintf (interp->result, "wrong # args");
1881 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
1883 rl = find_IR_record (obj, offset);
1886 logf (LOG_DEBUG, "No record at position %d", offset);
1891 case Z_NamePlusRecord_databaseRecord:
1892 interp->result = "DB";
1894 case Z_NamePlusRecord_surrogateDiagnostic:
1895 interp->result = "SD";
1903 * do_recordType: Return record type (if any) at position.
1905 static int do_recordType (void *o, Tcl_Interp *interp, int argc, char **argv)
1907 IrTcl_SetObj *obj = o;
1909 IrTcl_RecordList *rl;
1915 else if (argc == -1)
1921 sprintf (interp->result, "wrong # args");
1924 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
1926 rl = find_IR_record (obj, offset);
1929 if (rl->which != Z_NamePlusRecord_databaseRecord)
1931 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
1934 Tcl_AppendElement (interp, (char*)
1935 IrTcl_getRecordSyntaxStr (rl->u.dbrec.type));
1942 static int ir_diagResult (Tcl_Interp *interp, IrTcl_Diagnostic *list, int num)
1948 for (i = 0; i<num; i++)
1950 logf (LOG_DEBUG, "Diagnostic, code %d", list[i].condition);
1951 sprintf (buf, "%d", list[i].condition);
1952 Tcl_AppendElement (interp, buf);
1953 cp = diagbib1_str (list[i].condition);
1955 Tcl_AppendElement (interp, (char*) cp);
1957 Tcl_AppendElement (interp, "");
1958 if (list[i].addinfo)
1959 Tcl_AppendElement (interp, (char*) list[i].addinfo);
1961 Tcl_AppendElement (interp, "");
1967 * do_diag: Return diagnostic record info
1969 static int do_diag (void *o, Tcl_Interp *interp, int argc, char **argv)
1971 IrTcl_SetObj *obj = o;
1973 IrTcl_RecordList *rl;
1979 sprintf (interp->result, "wrong # args");
1982 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
1984 rl = find_IR_record (obj, offset);
1987 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
1990 if (rl->which != Z_NamePlusRecord_surrogateDiagnostic)
1992 Tcl_AppendResult (interp, "No Diagnostic record at #", argv[2], NULL);
1995 return ir_diagResult (interp, rl->u.surrogateDiagnostics.list,
1996 rl->u.surrogateDiagnostics.num);
2000 * do_getMarc: Get ISO2709 Record lines/fields
2002 static int do_getMarc (void *o, Tcl_Interp *interp, int argc, char **argv)
2004 IrTcl_SetObj *obj = o;
2006 IrTcl_RecordList *rl;
2012 sprintf (interp->result, "wrong # args");
2015 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2017 rl = find_IR_record (obj, offset);
2020 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2023 if (rl->which != Z_NamePlusRecord_databaseRecord)
2025 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2028 return ir_tcl_get_marc (interp, rl->u.dbrec.buf, argc, argv);
2032 * do_getSutrs: Get SUTRS Record
2034 static int do_getSutrs (void *o, Tcl_Interp *interp, int argc, char **argv)
2036 IrTcl_SetObj *obj = o;
2038 IrTcl_RecordList *rl;
2044 sprintf (interp->result, "wrong # args");
2047 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2049 rl = find_IR_record (obj, offset);
2052 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2055 if (rl->which != Z_NamePlusRecord_databaseRecord)
2057 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2060 if (rl->u.dbrec.type != VAL_SUTRS)
2062 Tcl_AppendElement (interp, rl->u.dbrec.buf);
2068 * do_getGrs: Get a GRS1 Record
2070 static int do_getGrs (void *o, Tcl_Interp *interp, int argc, char **argv)
2072 IrTcl_SetObj *obj = o;
2074 IrTcl_RecordList *rl;
2080 sprintf (interp->result, "wrong # args");
2083 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2085 rl = find_IR_record (obj, offset);
2088 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2091 if (rl->which != Z_NamePlusRecord_databaseRecord)
2093 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2096 if (rl->u.dbrec.type != VAL_GRS1)
2098 return ir_tcl_get_grs (interp, rl->u.dbrec.u.grs1, argc, argv);
2103 * do_responseStatus: Return response status (present or search)
2105 static int do_responseStatus (void *o, Tcl_Interp *interp,
2106 int argc, char **argv)
2108 IrTcl_SetObj *obj = o;
2112 obj->recordFlag = 0;
2113 obj->nonSurrogateDiagnosticNum = 0;
2114 obj->nonSurrogateDiagnosticList = NULL;
2117 else if (argc == -1)
2119 ir_deleteDiags (&obj->nonSurrogateDiagnosticList,
2120 &obj->nonSurrogateDiagnosticNum);
2123 if (!obj->recordFlag)
2125 Tcl_AppendElement (interp, "OK");
2130 case Z_Records_DBOSD:
2131 Tcl_AppendElement (interp, "DBOSD");
2134 Tcl_AppendElement (interp, "NSD");
2135 return ir_diagResult (interp, obj->nonSurrogateDiagnosticList,
2136 obj->nonSurrogateDiagnosticNum);
2142 * do_present: Perform Present Request
2145 static int do_present (void *o, Tcl_Interp *interp, int argc, char **argv)
2147 IrTcl_SetObj *obj = o;
2150 Z_PresentRequest *req;
2158 if (Tcl_GetInt (interp, argv[2], &start) == TCL_ERROR)
2165 if (Tcl_GetInt (interp, argv[3], &number) == TCL_ERROR)
2173 interp->result = "present: not connected";
2178 obj->number = number;
2180 apdu = zget_APDU (p->odr_out, Z_APDU_presentRequest);
2181 req = apdu->u.presentRequest;
2183 set_referenceId (p->odr_out, &req->referenceId,
2184 obj->set_inher.referenceId);
2186 req->resultSetId = obj->setName ? obj->setName : "Default";
2188 req->resultSetStartPoint = &start;
2189 req->numberOfRecordsRequested = &number;
2190 if (obj->set_inher.preferredRecordSyntax)
2192 struct oident ident;
2194 ident.proto = p->protocol_type;
2195 ident.class = CLASS_RECSYN;
2196 ident.value = *obj->set_inher.preferredRecordSyntax;
2197 logf (LOG_DEBUG, "Preferred record syntax is %d", ident.value);
2198 req->preferredRecordSyntax = odr_oiddup (p->odr_out,
2199 oid_getoidbyent (&ident));
2202 req->preferredRecordSyntax = 0;
2204 if (obj->set_inher.elementSetNames && *obj->set_inher.elementSetNames)
2206 Z_ElementSetNames *esn = odr_malloc (p->odr_out, sizeof(*esn));
2207 Z_RecordComposition *compo = odr_malloc (p->odr_out, sizeof(*compo));
2209 esn->which = Z_ElementSetNames_generic;
2210 esn->u.generic = obj->set_inher.elementSetNames;
2212 req->recordComposition = compo;
2213 compo->which = Z_RecordComp_simple;
2214 compo->u.simple = esn;
2217 req->recordComposition = NULL;
2218 return ir_tcl_send_APDU (interp, p, apdu, "present", argv[0]);
2222 * do_loadFile: Load result set from file
2225 static int do_loadFile (void *o, Tcl_Interp *interp,
2226 int argc, char **argv)
2228 IrTcl_SetObj *setobj = o;
2238 interp->result = "wrong # args";
2241 inf = fopen (argv[2], "r");
2244 Tcl_AppendResult (interp, "Cannot open ", argv[2], NULL);
2247 while ((buf = ir_tcl_fread_marc (inf, &size)))
2249 IrTcl_RecordList *rl;
2251 rl = new_IR_record (setobj, no, Z_NamePlusRecord_databaseRecord);
2252 rl->u.dbrec.type = VAL_USMARC;
2253 rl->u.dbrec.buf = buf;
2254 rl->u.dbrec.size = size;
2257 setobj->numberOfRecordsReturned = no-1;
2262 static IrTcl_Method ir_set_method_tab[] = {
2263 { 0, "search", do_search },
2264 { 0, "searchStatus", do_searchStatus },
2265 { 0, "presentStatus", do_presentStatus },
2266 { 0, "nextResultSetPosition", do_nextResultSetPosition },
2267 { 0, "setName", do_setName },
2268 { 0, "resultCount", do_resultCount },
2269 { 0, "numberOfRecordsReturned", do_numberOfRecordsReturned },
2270 { 0, "present", do_present },
2271 { 0, "type", do_type },
2272 { 0, "getMarc", do_getMarc },
2273 { 0, "getSutrs", do_getSutrs },
2274 { 0, "getGrs", do_getGrs },
2275 { 0, "recordType", do_recordType },
2276 { 0, "diag", do_diag },
2277 { 0, "responseStatus", do_responseStatus },
2278 { 0, "loadFile", do_loadFile },
2283 * ir_set_obj_method: IR Set Object methods
2285 static int ir_set_obj_method (ClientData clientData, Tcl_Interp *interp,
2286 int argc, char **argv)
2288 IrTcl_Methods tabs[3];
2289 IrTcl_SetObj *p = clientData;
2293 interp->result = "wrong # args";
2296 tabs[0].tab = ir_set_method_tab;
2298 tabs[1].tab = ir_set_c_method_tab;
2299 tabs[1].obj = &p->set_inher;
2302 return ir_method (interp, argc, argv, tabs);
2306 * ir_set_obj_delete: IR Set Object disposal
2308 static void ir_set_obj_delete (ClientData clientData)
2310 IrTcl_Methods tabs[3];
2311 IrTcl_SetObj *p = clientData;
2313 logf (LOG_DEBUG, "ir set delete");
2315 tabs[0].tab = ir_set_method_tab;
2317 tabs[1].tab = ir_set_c_method_tab;
2318 tabs[1].obj = &p->set_inher;
2321 ir_method (NULL, -1, NULL, tabs);
2327 * ir_set_obj_mk: IR Set Object creation
2329 static int ir_set_obj_mk (ClientData clientData, Tcl_Interp *interp,
2330 int argc, char **argv)
2332 IrTcl_Methods tabs[3];
2335 if (argc < 2 || argc > 3)
2337 interp->result = "wrong # args";
2340 obj = ir_tcl_malloc (sizeof(*obj));
2341 logf (LOG_DEBUG, "ir set create");
2344 Tcl_CmdInfo parent_info;
2349 if (!Tcl_GetCommandInfo (interp, argv[2], &parent_info))
2351 interp->result = "No parent";
2354 obj->parent = (IrTcl_Obj *) parent_info.clientData;
2356 dst = &obj->set_inher;
2357 src = &obj->parent->set_inher;
2359 if ((dst->num_databaseNames = src->num_databaseNames))
2360 dst->databaseNames =
2361 ir_tcl_malloc (sizeof (*dst->databaseNames)
2362 * dst->num_databaseNames);
2364 dst->databaseNames = NULL;
2365 for (i = 0; i < dst->num_databaseNames; i++)
2366 if (ir_tcl_strdup (interp, &dst->databaseNames[i],
2367 src->databaseNames[i]) == TCL_ERROR)
2369 if (ir_tcl_strdup (interp, &dst->queryType, src->queryType)
2373 if (ir_tcl_strdup (interp, &dst->referenceId, src->referenceId)
2377 if (ir_tcl_strdup (interp, &dst->elementSetNames, src->elementSetNames)
2381 if (ir_tcl_strdup (interp, &dst->smallSetElementSetNames,
2382 src->smallSetElementSetNames)
2386 if (ir_tcl_strdup (interp, &dst->mediumSetElementSetNames,
2387 src->mediumSetElementSetNames)
2391 if (src->preferredRecordSyntax &&
2392 (dst->preferredRecordSyntax
2393 = ir_tcl_malloc (sizeof(*dst->preferredRecordSyntax))))
2394 *dst->preferredRecordSyntax = *src->preferredRecordSyntax;
2396 dst->preferredRecordSyntax = NULL;
2397 dst->replaceIndicator = src->replaceIndicator;
2398 dst->smallSetUpperBound = src->smallSetUpperBound;
2399 dst->largeSetLowerBound = src->largeSetLowerBound;
2400 dst->mediumSetPresentNumber = src->mediumSetPresentNumber;
2405 tabs[0].tab = ir_set_method_tab;
2409 if (ir_method (interp, 0, NULL, tabs) == TCL_ERROR)
2412 Tcl_CreateCommand (interp, argv[1], ir_set_obj_method,
2413 (ClientData) obj, ir_set_obj_delete);
2417 /* ------------------------------------------------------- */
2420 * do_scan: Perform scan
2422 static int do_scan (void *o, Tcl_Interp *interp, int argc, char **argv)
2426 IrTcl_ScanObj *obj = o;
2427 IrTcl_Obj *p = obj->parent;
2430 struct ccl_rpn_node *rpn;
2438 interp->result = "wrong # args";
2441 if (!p->set_inher.num_databaseNames)
2443 interp->result = "no databaseNames";
2448 interp->result = "scan: not connected";
2452 bib1.proto = p->protocol_type;
2453 bib1.class = CLASS_ATTSET;
2454 bib1.value = VAL_BIB1;
2456 apdu = zget_APDU (p->odr_out, Z_APDU_scanRequest);
2457 req = apdu->u.scanRequest;
2459 set_referenceId (p->odr_out, &req->referenceId, p->set_inher.referenceId);
2460 req->num_databaseNames = p->set_inher.num_databaseNames;
2461 req->databaseNames = p->set_inher.databaseNames;
2462 req->attributeSet = oid_getoidbyent (&bib1);
2465 if (!(req->termListAndStartPoint = p_query_scan (p->odr_out, argv[2])))
2467 Tcl_AppendResult (interp, "Syntax error in query", NULL);
2471 rpn = ccl_find_str(p->bibset, argv[2], &r, &pos);
2474 Tcl_AppendResult (interp, "CCL error: ", ccl_err_msg (r), NULL);
2477 ccl_pr_tree (rpn, stderr);
2478 fprintf (stderr, "\n");
2479 if (!(req->termListAndStartPoint = ccl_scan_query (rpn)))
2482 req->stepSize = &obj->stepSize;
2483 req->numberOfTermsRequested = &obj->numberOfTermsRequested;
2484 req->preferredPositionInResponse = &obj->preferredPositionInResponse;
2485 logf (LOG_DEBUG, "stepSize=%d", *req->stepSize);
2486 logf (LOG_DEBUG, "numberOfTermsRequested=%d",
2487 *req->numberOfTermsRequested);
2488 logf (LOG_DEBUG, "preferredPositionInResponse=%d",
2489 *req->preferredPositionInResponse);
2491 return ir_tcl_send_APDU (interp, p, apdu, "scan", argv[0]);
2495 * do_stepSize: Set/get replace Step Size
2497 static int do_stepSize (void *obj, Tcl_Interp *interp,
2498 int argc, char **argv)
2500 IrTcl_ScanObj *p = obj;
2506 return get_set_int (&p->stepSize, interp, argc, argv);
2510 * do_numberOfTermsRequested: Set/get Number of Terms requested
2512 static int do_numberOfTermsRequested (void *obj, Tcl_Interp *interp,
2513 int argc, char **argv)
2515 IrTcl_ScanObj *p = obj;
2519 p->numberOfTermsRequested = 20;
2522 return get_set_int (&p->numberOfTermsRequested, interp, argc, argv);
2527 * do_preferredPositionInResponse: Set/get preferred Position
2529 static int do_preferredPositionInResponse (void *obj, Tcl_Interp *interp,
2530 int argc, char **argv)
2532 IrTcl_ScanObj *p = obj;
2536 p->preferredPositionInResponse = 1;
2539 return get_set_int (&p->preferredPositionInResponse, interp, argc, argv);
2543 * do_scanStatus: Get scan status
2545 static int do_scanStatus (void *obj, Tcl_Interp *interp,
2546 int argc, char **argv)
2548 IrTcl_ScanObj *p = obj;
2552 return get_set_int (&p->scanStatus, interp, argc, argv);
2556 * do_numberOfEntriesReturned: Get number of Entries returned
2558 static int do_numberOfEntriesReturned (void *obj, Tcl_Interp *interp,
2559 int argc, char **argv)
2561 IrTcl_ScanObj *p = obj;
2565 return get_set_int (&p->numberOfEntriesReturned, interp, argc, argv);
2569 * do_positionOfTerm: Get position of Term
2571 static int do_positionOfTerm (void *obj, Tcl_Interp *interp,
2572 int argc, char **argv)
2574 IrTcl_ScanObj *p = obj;
2578 return get_set_int (&p->positionOfTerm, interp, argc, argv);
2582 * do_scanLine: get Scan Line (surrogate or normal) after response
2584 static int do_scanLine (void *obj, Tcl_Interp *interp, int argc, char **argv)
2586 IrTcl_ScanObj *p = obj;
2592 p->entries_flag = 0;
2594 p->nonSurrogateDiagnosticNum = 0;
2595 p->nonSurrogateDiagnosticList = 0;
2598 else if (argc == -1)
2600 p->entries_flag = 0;
2601 /* release entries */
2604 ir_deleteDiags (&p->nonSurrogateDiagnosticList,
2605 &p->nonSurrogateDiagnosticNum);
2610 interp->result = "wrong # args";
2613 if (Tcl_GetInt (interp, argv[2], &i) == TCL_ERROR)
2615 if (!p->entries_flag || p->which != Z_ListEntries_entries || !p->entries
2616 || i >= p->num_entries || i < 0)
2618 switch (p->entries[i].which)
2620 case Z_Entry_termInfo:
2621 Tcl_AppendElement (interp, "T");
2622 if (p->entries[i].u.term.buf)
2623 Tcl_AppendElement (interp, p->entries[i].u.term.buf);
2625 Tcl_AppendElement (interp, "");
2626 sprintf (numstr, "%d", p->entries[i].u.term.globalOccurrences);
2627 Tcl_AppendElement (interp, numstr);
2629 case Z_Entry_surrogateDiagnostic:
2630 Tcl_AppendElement (interp, "SD");
2631 return ir_diagResult (interp, p->entries[i].u.diag.list,
2632 p->entries[i].u.diag.num);
2638 static IrTcl_Method ir_scan_method_tab[] = {
2639 { 0, "scan", do_scan },
2640 { 0, "stepSize", do_stepSize },
2641 { 0, "numberOfTermsRequested", do_numberOfTermsRequested },
2642 { 0, "preferredPositionInResponse", do_preferredPositionInResponse },
2643 { 0, "scanStatus", do_scanStatus },
2644 { 0, "numberOfEntriesReturned", do_numberOfEntriesReturned },
2645 { 0, "positionOfTerm", do_positionOfTerm },
2646 { 0, "scanLine", do_scanLine },
2651 * ir_scan_obj_method: IR Scan Object methods
2653 static int ir_scan_obj_method (ClientData clientData, Tcl_Interp *interp,
2654 int argc, char **argv)
2656 IrTcl_Methods tabs[2];
2660 interp->result = "wrong # args";
2663 tabs[0].tab = ir_scan_method_tab;
2664 tabs[0].obj = clientData;
2667 return ir_method (interp, argc, argv, tabs);
2671 * ir_scan_obj_delete: IR Scan Object disposal
2673 static void ir_scan_obj_delete (ClientData clientData)
2675 IrTcl_Methods tabs[2];
2676 IrTcl_ScanObj *obj = clientData;
2678 tabs[0].tab = ir_scan_method_tab;
2682 ir_method (NULL, -1, NULL, tabs);
2687 * ir_scan_obj_mk: IR Scan Object creation
2689 static int ir_scan_obj_mk (ClientData clientData, Tcl_Interp *interp,
2690 int argc, char **argv)
2692 Tcl_CmdInfo parent_info;
2694 IrTcl_Methods tabs[2];
2698 interp->result = "wrong # args";
2701 if (!Tcl_GetCommandInfo (interp, argv[2], &parent_info))
2703 interp->result = "No parent";
2706 obj = ir_tcl_malloc (sizeof(*obj));
2707 obj->parent = (IrTcl_Obj *) parent_info.clientData;
2709 tabs[0].tab = ir_scan_method_tab;
2713 if (ir_method (interp, 0, NULL, tabs) == TCL_ERROR)
2715 Tcl_CreateCommand (interp, argv[1], ir_scan_obj_method,
2716 (ClientData) obj, ir_scan_obj_delete);
2720 /* ------------------------------------------------------- */
2722 static void ir_initResponse (void *obj, Z_InitResponse *initrs)
2726 p->initResult = *initrs->result ? 1 : 0;
2727 if (!*initrs->result)
2728 logf (LOG_DEBUG, "Connection rejected by target");
2730 logf (LOG_DEBUG, "Connection accepted by target");
2732 get_referenceId (&p->set_inher.referenceId, initrs->referenceId);
2734 free (p->targetImplementationId);
2735 ir_tcl_strdup (p->interp, &p->targetImplementationId,
2736 initrs->implementationId);
2737 free (p->targetImplementationName);
2738 ir_tcl_strdup (p->interp, &p->targetImplementationName,
2739 initrs->implementationName);
2740 free (p->targetImplementationVersion);
2741 ir_tcl_strdup (p->interp, &p->targetImplementationVersion,
2742 initrs->implementationVersion);
2744 p->maximumRecordSize = *initrs->maximumRecordSize;
2745 p->preferredMessageSize = *initrs->preferredMessageSize;
2747 memcpy (&p->options, initrs->options, sizeof(initrs->options));
2748 memcpy (&p->protocolVersion, initrs->protocolVersion,
2749 sizeof(initrs->protocolVersion));
2750 free (p->userInformationField);
2751 p->userInformationField = NULL;
2752 if (initrs->userInformationField)
2756 if (initrs->userInformationField->which == ODR_EXTERNAL_octet &&
2757 (p->userInformationField =
2758 ir_tcl_malloc ((len =
2759 initrs->userInformationField->
2760 u.octet_aligned->len) +1)))
2762 memcpy (p->userInformationField,
2763 initrs->userInformationField->u.octet_aligned->buf,
2765 (p->userInformationField)[len] = '\0';
2770 static void ir_deleteDiags (IrTcl_Diagnostic **dst_list, int *dst_num)
2773 for (i = 0; i<*dst_num; i++)
2774 free (dst_list[i]->addinfo);
2780 static void ir_handleDiags (IrTcl_Diagnostic **dst_list, int *dst_num,
2781 Z_DiagRec **list, int num)
2787 *dst_list = ir_tcl_malloc (sizeof(**dst_list) * num);
2788 for (i = 0; i<num; i++)
2790 switch (list[i]->which)
2792 case Z_DiagRec_defaultFormat:
2793 (*dst_list)[i].condition = *list[i]->u.defaultFormat->condition;
2794 addinfo = list[i]->u.defaultFormat->addinfo;
2796 ((*dst_list)[i].addinfo = ir_tcl_malloc (strlen(addinfo)+1)))
2797 strcpy ((*dst_list)[i].addinfo, addinfo);
2800 (*dst_list)[i].addinfo = NULL;
2801 (*dst_list)[i].condition = 0;
2806 static void ir_handleRecords (void *o, Z_Records *zrs, IrTcl_SetObj *setobj)
2811 IrTcl_RecordList *rl;
2813 setobj->which = zrs->which;
2814 setobj->recordFlag = 1;
2816 ir_deleteDiags (&setobj->nonSurrogateDiagnosticList,
2817 &setobj->nonSurrogateDiagnosticNum);
2818 if (zrs->which == Z_Records_DBOSD)
2820 setobj->numberOfRecordsReturned =
2821 zrs->u.databaseOrSurDiagnostics->num_records;
2822 logf (LOG_DEBUG, "Got %d records", setobj->numberOfRecordsReturned);
2823 for (offset = 0; offset<setobj->numberOfRecordsReturned; offset++)
2825 rl = new_IR_record (setobj, setobj->start + offset,
2826 zrs->u.databaseOrSurDiagnostics->
2827 records[offset]->which);
2828 if (rl->which == Z_NamePlusRecord_surrogateDiagnostic)
2830 ir_handleDiags (&rl->u.surrogateDiagnostics.list,
2831 &rl->u.surrogateDiagnostics.num,
2832 &zrs->u.databaseOrSurDiagnostics->
2833 records[offset]->u.surrogateDiagnostic,
2838 Z_DatabaseRecord *zr;
2840 struct oident *ident;
2842 zr = zrs->u.databaseOrSurDiagnostics->records[offset]
2844 oe = (Z_External*) zr;
2845 rl->u.dbrec.size = zr->u.octet_aligned->len;
2847 if ((ident = oid_getentbyoid (oe->direct_reference)))
2848 rl->u.dbrec.type = ident->value;
2850 rl->u.dbrec.type = VAL_USMARC;
2852 if (oe->which == ODR_EXTERNAL_octet && rl->u.dbrec.size > 0)
2854 char *buf = (char*) zr->u.octet_aligned->buf;
2855 if ((rl->u.dbrec.buf = ir_tcl_malloc (rl->u.dbrec.size)))
2856 memcpy (rl->u.dbrec.buf, buf, rl->u.dbrec.size);
2858 else if (rl->u.dbrec.type == VAL_SUTRS &&
2859 oe->which == Z_External_sutrs)
2861 odr_setbuf (p->odr_in, (char*) oe->u.single_ASN1_type->buf,
2862 oe->u.single_ASN1_type->len, 0);
2863 if ((rl->u.dbrec.buf = ir_tcl_malloc (oe->u.sutrs->len+1)))
2865 memcpy (rl->u.dbrec.buf, oe->u.sutrs->buf,
2867 rl->u.dbrec.buf[oe->u.sutrs->len] = '\0';
2869 rl->u.dbrec.size = oe->u.sutrs->len;
2871 else if (rl->u.dbrec.type == VAL_GRS1 &&
2872 oe->which == Z_External_grs1)
2874 ir_tcl_read_grs (oe->u.grs1, &rl->u.dbrec.u.grs1);
2875 rl->u.dbrec.buf = NULL;
2878 rl->u.dbrec.buf = NULL;
2882 else if (zrs->which == Z_Records_multipleNSD)
2884 logf (LOG_DEBUG, "multipleNonSurrogateDiagnostic %d",
2885 zrs->u.multipleNonSurDiagnostics->num_diagRecs);
2886 setobj->numberOfRecordsReturned = 0;
2887 ir_handleDiags (&setobj->nonSurrogateDiagnosticList,
2888 &setobj->nonSurrogateDiagnosticNum,
2889 zrs->u.multipleNonSurDiagnostics->diagRecs,
2890 zrs->u.multipleNonSurDiagnostics->num_diagRecs);
2894 logf (LOG_DEBUG, "NonSurrogateDiagnostic");
2895 setobj->numberOfRecordsReturned = 0;
2896 ir_handleDiags (&setobj->nonSurrogateDiagnosticList,
2897 &setobj->nonSurrogateDiagnosticNum,
2898 &zrs->u.nonSurrogateDiagnostic,
2903 static void ir_searchResponse (void *o, Z_SearchResponse *searchrs,
2904 IrTcl_SetObj *setobj)
2906 Z_Records *zrs = searchrs->records;
2908 logf (LOG_DEBUG, "Received search response");
2911 logf (LOG_DEBUG, "Search response, no object!");
2914 setobj->searchStatus = searchrs->searchStatus ? 1 : 0;
2915 get_referenceId (&setobj->set_inher.referenceId, searchrs->referenceId);
2916 setobj->resultCount = *searchrs->resultCount;
2917 if (searchrs->presentStatus)
2918 setobj->presentStatus = *searchrs->presentStatus;
2919 if (searchrs->nextResultSetPosition)
2920 setobj->nextResultSetPosition = *searchrs->nextResultSetPosition;
2922 logf (LOG_DEBUG, "Search response %d, %d hits",
2923 setobj->searchStatus, setobj->resultCount);
2925 ir_handleRecords (o, zrs, setobj);
2927 setobj->recordFlag = 0;
2931 static void ir_presentResponse (void *o, Z_PresentResponse *presrs,
2932 IrTcl_SetObj *setobj)
2934 Z_Records *zrs = presrs->records;
2936 logf (LOG_DEBUG, "Received present response");
2939 logf (LOG_DEBUG, "Present response, no object!");
2942 setobj->presentStatus = *presrs->presentStatus;
2943 get_referenceId (&setobj->set_inher.referenceId, presrs->referenceId);
2944 setobj->nextResultSetPosition = *presrs->nextResultSetPosition;
2946 ir_handleRecords (o, zrs, setobj);
2949 setobj->recordFlag = 0;
2950 logf (LOG_DEBUG, "No records!");
2954 static void ir_scanResponse (void *o, Z_ScanResponse *scanrs,
2955 IrTcl_ScanObj *scanobj)
2959 logf (LOG_DEBUG, "Received scanResponse");
2961 get_referenceId (&p->set_inher.referenceId, scanrs->referenceId);
2962 scanobj->scanStatus = *scanrs->scanStatus;
2963 logf (LOG_DEBUG, "scanStatus=%d", scanobj->scanStatus);
2965 if (scanrs->stepSize)
2966 scanobj->stepSize = *scanrs->stepSize;
2967 logf (LOG_DEBUG, "stepSize=%d", scanobj->stepSize);
2969 scanobj->numberOfEntriesReturned = *scanrs->numberOfEntriesReturned;
2970 logf (LOG_DEBUG, "numberOfEntriesReturned=%d",
2971 scanobj->numberOfEntriesReturned);
2973 if (scanrs->positionOfTerm)
2974 scanobj->positionOfTerm = *scanrs->positionOfTerm;
2976 scanobj->positionOfTerm = -1;
2977 logf (LOG_DEBUG, "positionOfTerm=%d", scanobj->positionOfTerm);
2979 free (scanobj->entries);
2980 scanobj->entries = NULL;
2982 ir_deleteDiags (&scanobj->nonSurrogateDiagnosticList,
2983 &scanobj->nonSurrogateDiagnosticNum);
2984 if (scanrs->entries)
2989 scanobj->entries_flag = 1;
2990 scanobj->which = scanrs->entries->which;
2991 switch (scanobj->which)
2993 case Z_ListEntries_entries:
2994 scanobj->num_entries = scanrs->entries->u.entries->num_entries;
2995 scanobj->entries = ir_tcl_malloc (scanobj->num_entries *
2996 sizeof(*scanobj->entries));
2997 for (i=0; i<scanobj->num_entries; i++)
2999 ze = scanrs->entries->u.entries->entries[i];
3000 scanobj->entries[i].which = ze->which;
3003 case Z_Entry_termInfo:
3004 if (ze->u.termInfo->term->which == Z_Term_general)
3006 int l = ze->u.termInfo->term->u.general->len;
3007 scanobj->entries[i].u.term.buf = ir_tcl_malloc (1+l);
3008 memcpy (scanobj->entries[i].u.term.buf,
3009 ze->u.termInfo->term->u.general->buf,
3011 scanobj->entries[i].u.term.buf[l] = '\0';
3014 scanobj->entries[i].u.term.buf = NULL;
3015 if (ze->u.termInfo->globalOccurrences)
3016 scanobj->entries[i].u.term.globalOccurrences =
3017 *ze->u.termInfo->globalOccurrences;
3019 scanobj->entries[i].u.term.globalOccurrences = 0;
3021 case Z_Entry_surrogateDiagnostic:
3022 ir_handleDiags (&scanobj->entries[i].u.diag.list,
3023 &scanobj->entries[i].u.diag.num,
3024 &ze->u.surrogateDiagnostic,
3030 case Z_ListEntries_nonSurrogateDiagnostics:
3031 ir_handleDiags (&scanobj->nonSurrogateDiagnosticList,
3032 &scanobj->nonSurrogateDiagnosticNum,
3033 scanrs->entries->u.nonSurrogateDiagnostics->
3035 scanrs->entries->u.nonSurrogateDiagnostics->
3041 scanobj->entries_flag = 0;
3045 * ir_select_read: handle incoming packages
3047 void ir_select_read (ClientData clientData)
3049 IrTcl_Obj *p = clientData;
3054 Tcl_CmdInfo cmd_info;
3056 if (p->state == IR_TCL_R_Connecting)
3058 r = cs_rcvconnect (p->cs_link);
3061 logf (LOG_WARN, "cs_rcvconnect returned 1");
3064 p->state = IR_TCL_R_Idle;
3065 ir_select_remove_write (cs_fileno (p->cs_link), p);
3068 logf (LOG_DEBUG, "cs_rcvconnect error");
3071 p->failInfo = IR_TCL_FAIL_CONNECT;
3072 IrTcl_eval (p->interp, p->failback);
3074 do_disconnect (p, NULL, 2, NULL);
3077 p->state = IR_TCL_R_Idle;
3079 IrTcl_eval (p->interp, p->callback);
3080 if (p->cs_link && p->request_queue && p->state == IR_TCL_R_Idle)
3081 ir_tcl_send_q (p, p->request_queue, "x");
3086 /* signal one more use of ir object - callbacks must not
3087 release the ir memory (p pointer) */
3088 p->state = IR_TCL_R_Reading;
3091 /* read incoming APDU */
3092 if ((r=cs_get (p->cs_link, &p->buf_in, &p->len_in)) <= 0)
3094 logf (LOG_DEBUG, "cs_get failed, code %d", r);
3095 ir_select_remove (cs_fileno (p->cs_link), p);
3096 do_disconnect (p, NULL, 2, NULL);
3099 p->failInfo = IR_TCL_FAIL_READ;
3100 IrTcl_eval (p->interp, p->failback);
3102 /* release ir object now if callback deleted it */
3108 /* got complete APDU. Now decode */
3109 odr_setbuf (p->odr_in, p->buf_in, r, 0);
3110 logf (LOG_DEBUG, "cs_get ok, got %d", r);
3111 if (!z_APDU (p->odr_in, &apdu, 0))
3113 logf (LOG_DEBUG, "%s", odr_errlist [odr_geterror (p->odr_in)]);
3114 do_disconnect (p, NULL, 2, NULL);
3117 p->failInfo = IR_TCL_FAIL_IN_APDU;
3118 IrTcl_eval (p->interp, p->failback);
3120 /* release ir object now if failback deleted it */
3124 /* handle APDU and invoke callback */
3125 rq = p->request_queue;
3128 logf (LOG_FATAL, "Internal error. No queue entry");
3131 object_name = rq->object_name;
3132 logf (LOG_DEBUG, "getCommandInfo (%s)", object_name);
3133 if (Tcl_GetCommandInfo (p->interp, object_name, &cmd_info))
3137 case Z_APDU_initResponse:
3138 ir_initResponse (p, apdu->u.initResponse);
3140 case Z_APDU_searchResponse:
3141 ir_searchResponse (p, apdu->u.searchResponse,
3142 (IrTcl_SetObj *) cmd_info.clientData);
3144 case Z_APDU_presentResponse:
3145 ir_presentResponse (p, apdu->u.presentResponse,
3146 (IrTcl_SetObj *) cmd_info.clientData);
3148 case Z_APDU_scanResponse:
3149 ir_scanResponse (p, apdu->u.scanResponse,
3150 (IrTcl_ScanObj *) cmd_info.clientData);
3153 logf (LOG_WARN, "Received unknown APDU type (%d)",
3155 do_disconnect (p, NULL, 2, NULL);
3158 p->failInfo = IR_TCL_FAIL_UNKNOWN_APDU;
3159 IrTcl_eval (p->interp, p->failback);
3164 p->request_queue = rq->next;
3165 p->state = IR_TCL_R_Idle;
3168 IrTcl_eval (p->interp, rq->callback);
3170 free (rq->callback);
3171 free (rq->object_name);
3173 odr_reset (p->odr_in);
3174 if (p->ref_count == 1)
3180 } while (p->cs_link && cs_more (p->cs_link));
3181 if (p->cs_link && p->request_queue && p->state == IR_TCL_R_Idle)
3182 ir_tcl_send_q (p, p->request_queue, "x");
3186 * ir_select_write: handle outgoing packages - not yet written.
3188 void ir_select_write (ClientData clientData)
3190 IrTcl_Obj *p = clientData;
3194 logf (LOG_DEBUG, "In write handler");
3195 if (p->state == IR_TCL_R_Connecting)
3197 r = cs_rcvconnect (p->cs_link);
3200 p->state = IR_TCL_R_Idle;
3203 logf (LOG_DEBUG, "cs_rcvconnect error");
3204 ir_select_remove_write (cs_fileno (p->cs_link), p);
3207 p->failInfo = IR_TCL_FAIL_CONNECT;
3208 IrTcl_eval (p->interp, p->failback);
3210 do_disconnect (p, NULL, 2, NULL);
3213 ir_select_remove_write (cs_fileno (p->cs_link), p);
3215 IrTcl_eval (p->interp, p->callback);
3218 rq = p->request_queue;
3220 if ((r=cs_put (p->cs_link, rq->buf_out, rq->len_out)) < 0)
3222 logf (LOG_DEBUG, "select write fail");
3225 p->failInfo = IR_TCL_FAIL_WRITE;
3226 IrTcl_eval (p->interp, p->failback);
3230 do_disconnect (p, NULL, 2, NULL);
3232 else if (r == 0) /* remove select bit */
3234 p->state = IR_TCL_R_Waiting;
3235 ir_select_remove_write (cs_fileno (p->cs_link), p);
3241 /* ------------------------------------------------------- */
3244 * Irtcl_init: Registration of TCL commands.
3246 int Irtcl_Init (Tcl_Interp *interp)
3248 Tcl_CreateCommand (interp, "ir", ir_obj_mk, (ClientData) NULL,
3249 (Tcl_CmdDeleteProc *) NULL);
3250 Tcl_CreateCommand (interp, "ir-set", ir_set_obj_mk,
3251 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
3252 Tcl_CreateCommand (interp, "ir-scan", ir_scan_obj_mk,
3253 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);