2 * IR toolkit for tcl/tk
4 * See the file LICENSE for details.
5 * Sebastian Hammer, Adam Dickmeiss
8 * Revision 1.39 1995-06-08 10:26:32 adam
9 * Bug fix in ir_strdup.
11 * Revision 1.38 1995/06/01 16:36:47 adam
12 * About buttons. Minor bug fixes.
14 * Revision 1.37 1995/06/01 07:31:20 adam
15 * Rename of many typedefs -> IrTcl_...
17 * Revision 1.36 1995/05/31 13:09:59 adam
18 * Client searches/presents may be interrupted.
19 * New moving book-logo.
21 * Revision 1.35 1995/05/31 08:36:33 adam
22 * Bug fix in client.tcl: didn't save options on clientrc.tcl.
23 * New method: referenceId. More work on scan.
25 * Revision 1.34 1995/05/29 10:33:42 adam
26 * README and rename of startup script.
28 * Revision 1.33 1995/05/29 09:15:11 quinn
29 * Changed CS_SR to PROTO_SR, etc.
31 * Revision 1.32 1995/05/29 08:44:16 adam
32 * Work on delete of objects.
34 * Revision 1.31 1995/05/26 11:44:10 adam
35 * Bugs fixed. More work on MARC utilities and queries. Test
36 * client is up-to-date again.
38 * Revision 1.30 1995/05/26 08:54:11 adam
39 * New MARC utilities. Uses prefix query.
41 * Revision 1.29 1995/05/24 14:10:22 adam
42 * Work on idAuthentication, protocolVersion and options.
44 * Revision 1.28 1995/05/23 15:34:48 adam
45 * Many new settings, userInformationField, smallSetUpperBound, etc.
46 * A number of settings are inherited when ir-set is executed.
47 * This version is incompatible with the graphical test client (client.tcl).
49 * Revision 1.27 1995/05/11 15:34:47 adam
50 * Scan request changed a bit. This version works with RLG.
52 * Revision 1.26 1995/04/18 16:11:51 adam
53 * First version of graphical Scan. Some work on query-by-form.
55 * Revision 1.25 1995/04/17 09:37:17 adam
56 * Further development of scan.
58 * Revision 1.24 1995/04/11 14:16:42 adam
59 * Further work on scan. Response works. Entries aren't saved yet.
61 * Revision 1.23 1995/04/10 10:50:27 adam
62 * Result-set name defaults to suffix of ir-set name.
63 * Started working on scan. Not finished at this point.
65 * Revision 1.22 1995/03/31 10:43:03 adam
66 * More robust when getting bad MARC records.
68 * Revision 1.21 1995/03/31 08:56:37 adam
69 * New button "Search".
71 * Revision 1.20 1995/03/29 16:07:09 adam
72 * Bug fix: Didn't use setName in present request.
74 * Revision 1.19 1995/03/28 12:45:23 adam
75 * New ir method failback: called on disconnect/protocol error.
76 * New ir set/get method: protocol: SR / Z3950.
77 * Simple popup and disconnect when failback is invoked.
79 * Revision 1.18 1995/03/21 15:50:12 adam
82 * Revision 1.17 1995/03/21 13:41:03 adam
83 * Comstack cs_create not used too often. Non-blocking connect.
85 * Revision 1.16 1995/03/21 08:26:06 adam
86 * New method, setName, to specify the result set name (other than Default).
87 * New method, responseStatus, which returns diagnostic info, if any, after
88 * present response / search response.
90 * Revision 1.15 1995/03/20 15:24:07 adam
91 * Diagnostic records saved on searchResponse.
93 * Revision 1.14 1995/03/20 08:53:22 adam
94 * Event loop in tclmain.c rewritten. New method searchStatus.
96 * Revision 1.13 1995/03/17 18:26:17 adam
97 * Non-blocking i/o used now. Database names popup as cascade items.
99 * Revision 1.12 1995/03/17 15:45:00 adam
100 * Improved target/database setup.
102 * Revision 1.11 1995/03/16 17:54:03 adam
103 * Minor changes really.
105 * Revision 1.10 1995/03/15 16:14:50 adam
106 * Blocking arg in cs_create changed.
108 * Revision 1.9 1995/03/15 13:59:24 adam
111 * Revision 1.8 1995/03/15 08:25:16 adam
112 * New method presentStatus to check for error on present. Misc. cleanup
113 * of IrTcl_RecordList manipulations. Full MARC record presentation in
116 * Revision 1.7 1995/03/14 17:32:29 adam
117 * Presentation of full Marc record in popup window.
119 * Revision 1.6 1995/03/12 19:31:55 adam
120 * Pattern matching implemented when retrieving MARC records. More
121 * diagnostic functions.
123 * Revision 1.5 1995/03/10 18:00:15 adam
124 * Actual presentation in line-by-line format. RPN query support.
126 * Revision 1.4 1995/03/09 16:15:08 adam
127 * First presentRequest attempts. Hot-target list.
133 #include <sys/time.h>
143 int (*method) (void *obj, Tcl_Interp *interp, int argc, char **argv);
151 static int do_disconnect (void *obj, Tcl_Interp *interp,
152 int argc, char **argv);
154 static IrTcl_RecordList *new_IR_record (IrTcl_SetObj *setobj,
157 IrTcl_RecordList *rl;
159 for (rl = setobj->record_list; rl; rl = rl->next)
165 case Z_NamePlusRecord_databaseRecord:
166 free (rl->u.dbrec.buf);
167 rl->u.dbrec.buf = NULL;
169 case Z_NamePlusRecord_surrogateDiagnostic:
170 free (rl->u.diag.addinfo);
171 rl->u.diag.addinfo = NULL;
179 rl = malloc (sizeof(*rl));
181 rl->next = setobj->record_list;
183 setobj->record_list = rl;
189 static IrTcl_RecordList *find_IR_record (IrTcl_SetObj *setobj, int no)
191 IrTcl_RecordList *rl;
193 for (rl = setobj->record_list; rl; rl = rl->next)
199 static void delete_IR_records (IrTcl_SetObj *setobj)
201 IrTcl_RecordList *rl, *rl1;
203 for (rl = setobj->record_list; rl; rl = rl1)
207 case Z_NamePlusRecord_databaseRecord:
208 free (rl->u.dbrec.buf);
210 case Z_NamePlusRecord_surrogateDiagnostic:
211 free (rl->u.diag.addinfo);
217 setobj->record_list = NULL;
221 * getsetint: Set/get integer value
223 static int get_set_int (int *val, Tcl_Interp *interp, int argc, char **argv)
229 if (Tcl_GetInt (interp, argv[2], val)==TCL_ERROR)
232 sprintf (buf, "%d", *val);
233 Tcl_AppendResult (interp, buf, NULL);
238 * mk_nonSurrogateDiagnostics: Make Tcl result with diagnostic info
240 static int mk_nonSurrogateDiagnostics (Tcl_Interp *interp,
241 int condition, const char *addinfo)
246 Tcl_AppendElement (interp, "NSD");
247 sprintf (buf, "%d", condition);
248 Tcl_AppendElement (interp, buf);
249 cp = diagbib1_str (condition);
251 Tcl_AppendElement (interp, (char*) cp);
253 Tcl_AppendElement (interp, "");
255 Tcl_AppendElement (interp, (char*) addinfo);
257 Tcl_AppendElement (interp, "");
262 * ir_method: Search for method in table and invoke method handler
264 int ir_method (Tcl_Interp *interp, int argc, char **argv, IrTcl_Methods *tab)
266 IrTcl_Methods *tab_i = tab;
269 for (tab_i = tab; tab_i->tab; tab_i++)
270 for (t = tab_i->tab; t->name; t++)
273 if ((*t->method)(tab_i->obj, interp, argc, argv) == TCL_ERROR)
277 if (!strcmp (t->name, argv[1]))
278 return (*t->method)(tab_i->obj, interp, argc, argv);
282 Tcl_AppendResult (interp, "Bad method. Possible methods:", NULL);
283 for (tab_i = tab; tab_i->tab; tab_i++)
284 for (t = tab_i->tab; t->name; t++)
285 Tcl_AppendResult (interp, " ", t->name, NULL);
290 * ir_method_r: Get status for all readable elements
292 int ir_method_r (void *obj, Tcl_Interp *interp, int argc, char **argv,
300 for (; tab->name; tab++)
303 argv_n[1] = tab->name;
304 Tcl_AppendResult (interp, "{", NULL);
305 (*tab->method)(obj, interp, argc_n, argv_n);
306 Tcl_AppendResult (interp, "} ", NULL);
312 * ir_named_bits: get/set named bits
314 int ir_named_bits (struct ir_named_entry *tab, Odr_bitmask *ob,
315 Tcl_Interp *interp, int argc, char **argv)
317 struct ir_named_entry *ti;
322 for (no = 0; no < argc; no++)
324 for (ti = tab; ti->name; ti++)
325 if (!strcmp (argv[no], ti->name))
327 ODR_MASK_SET (ob, ti->pos);
332 Tcl_AppendResult (interp, "Bad bit mask: ", argv[no], NULL);
338 for (ti = tab; ti->name; ti++)
339 if (ODR_MASK_GET (ob, ti->pos))
340 Tcl_AppendElement (interp, ti->name);
345 * ir_strdup: Duplicate string
347 int ir_strdup (Tcl_Interp *interp, char** p, const char *s)
354 *p = malloc (strlen(s)+1);
357 interp->result = "strdup fail";
365 * ir_strdel: Delete string
367 int ir_strdel (Tcl_Interp *interp, char **p)
375 * ir_malloc: Malloc function
377 void *ir_malloc (Tcl_Interp *interp, size_t size)
379 static char buf[128];
380 void *p = malloc (size);
384 sprintf (buf, "Malloc fail. %ld bytes requested", (long) size);
385 interp->result = buf;
391 static void set_referenceId (ODR o, Z_ReferenceId **dst, const char *src)
397 *dst = odr_malloc (o, sizeof(**dst));
398 (*dst)->size = (*dst)->len = strlen(src);
399 (*dst)->buf = odr_malloc (o, (*dst)->len);
400 memcpy ((*dst)->buf, src, (*dst)->len);
404 static void get_referenceId (char **dst, Z_ReferenceId *src)
412 *dst = malloc (src->len+1);
413 memcpy (*dst, src->buf, src->len);
414 (*dst)[src->len] = '\0';
417 /* ------------------------------------------------------- */
420 * do_init_request: init method on IR object
422 static int do_init_request (void *obj, Tcl_Interp *interp,
423 int argc, char **argv)
425 Z_APDU apdu, *apdup = &apdu;
434 interp->result = "not connected";
437 odr_reset (p->odr_out);
439 set_referenceId (p->odr_out, &req.referenceId, p->set_inher.referenceId);
440 req.options = &p->options;
441 req.protocolVersion = &p->protocolVersion;
442 req.preferredMessageSize = &p->preferredMessageSize;
443 req.maximumRecordSize = &p->maximumRecordSize;
445 if (p->idAuthenticationGroupId)
447 Z_IdPass *pass = odr_malloc (p->odr_out, sizeof(*pass));
448 Z_IdAuthentication *auth = odr_malloc (p->odr_out, sizeof(*auth));
450 auth->which = Z_IdAuthentication_idPass;
451 auth->u.idPass = pass;
452 if (p->idAuthenticationGroupId && *p->idAuthenticationGroupId)
453 pass->groupId = p->idAuthenticationGroupId;
455 pass->groupId = NULL;
456 if (p->idAuthenticationUserId && *p->idAuthenticationUserId)
457 pass->userId = p->idAuthenticationUserId;
460 if (p->idAuthenticationPassword && *p->idAuthenticationPassword)
461 pass->password = p->idAuthenticationPassword;
463 pass->password = NULL;
464 req.idAuthentication = auth;
466 else if (!p->idAuthenticationOpen || !*p->idAuthenticationOpen)
467 req.idAuthentication = NULL;
470 Z_IdAuthentication *auth = odr_malloc (p->odr_out, sizeof(*auth));
472 auth->which = Z_IdAuthentication_open;
473 auth->u.open = p->idAuthenticationOpen;
474 req.idAuthentication = auth;
476 req.implementationId = p->implementationId;
477 req.implementationName = p->implementationName;
478 req.implementationVersion = "0.1";
479 req.userInformationField = 0;
481 apdu.u.initRequest = &req;
482 apdu.which = Z_APDU_initRequest;
484 if (!z_APDU (p->odr_out, &apdup, 0))
486 Tcl_AppendResult (interp, odr_errlist [odr_geterror (p->odr_out)],
488 odr_reset (p->odr_out);
491 p->sbuf = odr_getbuf (p->odr_out, &p->slen, NULL);
492 if ((r=cs_put (p->cs_link, p->sbuf, p->slen)) < 0)
494 interp->result = "cs_put failed in init";
495 do_disconnect (p, NULL, 2, NULL);
500 ir_select_add_write (cs_fileno(p->cs_link), p);
501 logf (LOG_DEBUG, "Sent part of initializeRequest (%d bytes)", p->slen);
504 logf (LOG_DEBUG, "Sent whole initializeRequest (%d bytes)", p->slen);
509 * do_protocolVersion: Set protocol Version
511 static int do_protocolVersion (void *obj, Tcl_Interp *interp,
512 int argc, char **argv)
514 static struct ir_named_entry version_tab[] = {
525 ODR_MASK_ZERO (&p->protocolVersion);
526 ODR_MASK_SET (&p->protocolVersion, 0);
527 ODR_MASK_SET (&p->protocolVersion, 1);
530 return ir_named_bits (version_tab, &p->protocolVersion,
531 interp, argc-2, argv+2);
535 * do_options: Set options
537 static int do_options (void *obj, Tcl_Interp *interp,
538 int argc, char **argv)
540 static struct ir_named_entry options_tab[] = {
544 { "resourceReport", 3 },
545 { "triggerResourceCtrl", 4},
546 { "resourceCtrl", 5},
550 { "extentedServices", 10},
551 { "level-1Segmentation", 11},
552 { "level-2Segmentation", 12},
553 { "concurrentOperations", 13},
554 { "namedResultSets", 14},
561 ODR_MASK_ZERO (&p->options);
562 ODR_MASK_SET (&p->options, 0);
563 ODR_MASK_SET (&p->options, 1);
564 ODR_MASK_SET (&p->options, 7);
565 ODR_MASK_SET (&p->options, 14);
568 return ir_named_bits (options_tab, &p->options, interp, argc-2, argv+2);
572 * do_preferredMessageSize: Set/get preferred message size
574 static int do_preferredMessageSize (void *obj, Tcl_Interp *interp,
575 int argc, char **argv)
581 p->preferredMessageSize = 4096;
584 return get_set_int (&p->preferredMessageSize, interp, argc, argv);
588 * do_maximumRecordSize: Set/get maximum record size
590 static int do_maximumRecordSize (void *obj, Tcl_Interp *interp,
591 int argc, char **argv)
597 p->maximumRecordSize = 32768;
600 return get_set_int (&p->maximumRecordSize, interp, argc, argv);
604 * do_initResult: Get init result
606 static int do_initResult (void *obj, Tcl_Interp *interp,
607 int argc, char **argv)
613 return get_set_int (&p->initResult, interp, argc, argv);
618 * do_implementationName: Set/get Implementation Name.
620 static int do_implementationName (void *obj, Tcl_Interp *interp,
621 int argc, char **argv)
626 return ir_strdup (interp, &p->implementationName, "TCL/TK on YAZ");
628 return ir_strdel (interp, &p->implementationName);
631 free (p->implementationName);
632 if (ir_strdup (interp, &p->implementationName, argv[2])
636 Tcl_AppendResult (interp, p->implementationName, (char*) NULL);
641 * do_implementationId: Set/get Implementation Id.
643 static int do_implementationId (void *obj, Tcl_Interp *interp,
644 int argc, char **argv)
649 return ir_strdup (interp, &p->implementationId, "81");
651 return ir_strdel (interp, &p->implementationId);
654 free (p->implementationId);
655 if (ir_strdup (interp, &p->implementationId, argv[2]) == TCL_ERROR)
658 Tcl_AppendResult (interp, p->implementationId, (char*) NULL);
663 * do_targetImplementationName: Get Implementation Name of target.
665 static int do_targetImplementationName (void *obj, Tcl_Interp *interp,
666 int argc, char **argv)
672 p->targetImplementationName = NULL;
676 return ir_strdel (interp, &p->targetImplementationName);
677 Tcl_AppendResult (interp, p->targetImplementationName, (char*) NULL);
682 * do_targetImplementationId: Get Implementation Id of target
684 static int do_targetImplementationId (void *obj, Tcl_Interp *interp,
685 int argc, char **argv)
691 p->targetImplementationId = NULL;
695 return ir_strdel (interp, &p->targetImplementationId);
696 Tcl_AppendResult (interp, p->targetImplementationId, (char*) NULL);
701 * do_targetImplementationVersion: Get Implementation Version of target
703 static int do_targetImplementationVersion (void *obj, Tcl_Interp *interp,
704 int argc, char **argv)
710 p->targetImplementationVersion = NULL;
714 return ir_strdel (interp, &p->targetImplementationVersion);
715 Tcl_AppendResult (interp, p->targetImplementationVersion, (char*) NULL);
720 * do_idAuthentication: Set/get id Authentication
722 static int do_idAuthentication (void *obj, Tcl_Interp *interp,
723 int argc, char **argv)
727 if (argc >= 3 || argc == -1)
729 free (p->idAuthenticationOpen);
730 free (p->idAuthenticationGroupId);
731 free (p->idAuthenticationUserId);
732 free (p->idAuthenticationPassword);
734 if (argc >= 3 || argc <= 0)
736 p->idAuthenticationOpen = NULL;
737 p->idAuthenticationGroupId = NULL;
738 p->idAuthenticationUserId = NULL;
739 p->idAuthenticationPassword = NULL;
747 if (ir_strdup (interp, &p->idAuthenticationOpen, argv[2])
753 if (ir_strdup (interp, &p->idAuthenticationGroupId, argv[2])
756 if (ir_strdup (interp, &p->idAuthenticationUserId, argv[3])
759 if (ir_strdup (interp, &p->idAuthenticationPassword, argv[4])
764 if (p->idAuthenticationOpen)
765 Tcl_AppendElement (interp, p->idAuthenticationOpen);
766 else if (p->idAuthenticationGroupId)
768 Tcl_AppendElement (interp, p->idAuthenticationGroupId);
769 Tcl_AppendElement (interp, p->idAuthenticationUserId);
770 Tcl_AppendElement (interp, p->idAuthenticationPassword);
776 * do_connect: connect method on IR object
778 static int do_connect (void *obj, Tcl_Interp *interp,
779 int argc, char **argv)
784 int protocol_type = PROTO_Z3950;
792 interp->result = "already connected";
795 if (!strcmp (p->protocol_type, "Z3950"))
796 protocol_type = PROTO_Z3950;
797 else if (!strcmp (p->protocol_type, "SR"))
798 protocol_type = PROTO_SR;
801 interp->result = "bad protocol type";
804 if (!strcmp (p->cs_type, "tcpip"))
806 p->cs_link = cs_create (tcpip_type, CS_BLOCK, protocol_type);
807 addr = tcpip_strtoaddr (argv[2]);
810 interp->result = "tcpip_strtoaddr fail";
813 logf (LOG_DEBUG, "tcp/ip connect %s", argv[2]);
816 else if (!strcmp (p->cs_type, "mosi"))
818 p->cs_link = cs_create (mosi_type, CS_BLOCK, protocol_type);
819 addr = mosi_strtoaddr (argv[2]);
822 interp->result = "mosi_strtoaddr fail";
825 logf (LOG_DEBUG, "mosi connect %s", argv[2]);
830 interp->result = "unknown comstack type";
833 if (ir_strdup (interp, &p->hostname, argv[2]) == TCL_ERROR)
835 if ((r=cs_connect (p->cs_link, addr)) < 0)
837 interp->result = "cs_connect fail";
838 do_disconnect (p, NULL, 2, NULL);
841 ir_select_add (cs_fileno (p->cs_link), p);
844 ir_select_add_write (cs_fileno (p->cs_link), p);
851 Tcl_Eval (p->interp, p->callback);
855 Tcl_AppendElement (interp, p->hostname);
860 * do_disconnect: disconnect method on IR object
862 static int do_disconnect (void *obj, Tcl_Interp *interp,
863 int argc, char **argv)
878 ir_select_remove_write (cs_fileno (p->cs_link), p);
879 ir_select_remove (cs_fileno (p->cs_link), p);
882 cs_close (p->cs_link);
885 assert (!p->cs_link);
890 * do_comstack: Set/get comstack method on IR object
892 static int do_comstack (void *o, Tcl_Interp *interp,
893 int argc, char **argv)
898 return ir_strdup (interp, &obj->cs_type, "tcpip");
900 return ir_strdel (interp, &obj->cs_type);
904 if (ir_strdup (interp, &obj->cs_type, argv[2]) == TCL_ERROR)
907 Tcl_AppendElement (interp, obj->cs_type);
912 * do_protocol: Set/get protocol method on IR object
914 static int do_protocol (void *o, Tcl_Interp *interp,
915 int argc, char **argv)
920 return ir_strdup (interp, &obj->protocol_type, "Z3950");
922 return ir_strdel (interp, &obj->protocol_type);
925 free (obj->protocol_type);
926 if (ir_strdup (interp, &obj->protocol_type, argv[2]) == TCL_ERROR)
929 Tcl_AppendElement (interp, obj->protocol_type);
934 * do_callback: add callback
936 static int do_callback (void *obj, Tcl_Interp *interp,
937 int argc, char **argv)
947 return ir_strdel (interp, &p->callback);
953 if (ir_strdup (interp, &p->callback, argv[2]) == TCL_ERROR)
964 * do_failback: add error handle callback
966 static int do_failback (void *obj, Tcl_Interp *interp,
967 int argc, char **argv)
977 return ir_strdel (interp, &p->failback);
983 if (ir_strdup (interp, &p->failback, argv[2]) == TCL_ERROR)
994 * do_databaseNames: specify database names
996 static int do_databaseNames (void *obj, Tcl_Interp *interp,
997 int argc, char **argv)
1000 IrTcl_SetCObj *p = obj;
1004 for (i=0; i<p->num_databaseNames; i++)
1005 free (p->databaseNames[i]);
1006 free (p->databaseNames);
1010 p->num_databaseNames = 0;
1011 p->databaseNames = NULL;
1016 for (i=0; i<p->num_databaseNames; i++)
1017 Tcl_AppendElement (interp, p->databaseNames[i]);
1020 if (p->databaseNames)
1022 for (i=0; i<p->num_databaseNames; i++)
1023 free (p->databaseNames[i]);
1024 free (p->databaseNames);
1026 p->num_databaseNames = argc - 2;
1027 if (!(p->databaseNames = ir_malloc (interp,
1028 sizeof(*p->databaseNames) * p->num_databaseNames)))
1030 for (i=0; i<p->num_databaseNames; i++)
1032 if (ir_strdup (interp, &p->databaseNames[i], argv[2+i])
1040 * do_replaceIndicator: Set/get replace Set indicator
1042 static int do_replaceIndicator (void *obj, Tcl_Interp *interp,
1043 int argc, char **argv)
1045 IrTcl_SetCObj *p = obj;
1049 p->replaceIndicator = 1;
1052 return get_set_int (&p->replaceIndicator, interp, argc, argv);
1056 * do_queryType: Set/Get query method
1058 static int do_queryType (void *obj, Tcl_Interp *interp,
1059 int argc, char **argv)
1061 IrTcl_SetCObj *p = obj;
1064 return ir_strdup (interp, &p->queryType, "rpn");
1065 else if (argc == -1)
1066 return ir_strdel (interp, &p->queryType);
1069 free (p->queryType);
1070 if (ir_strdup (interp, &p->queryType, argv[2]) == TCL_ERROR)
1073 Tcl_AppendResult (interp, p->queryType, NULL);
1078 * do_userInformationField: Get User information field
1080 static int do_userInformationField (void *obj, Tcl_Interp *interp,
1081 int argc, char **argv)
1087 p->userInformationField = NULL;
1090 else if (argc == -1)
1091 return ir_strdel (interp, &p->userInformationField);
1092 Tcl_AppendResult (interp, p->userInformationField, NULL);
1097 * do_smallSetUpperBound: Set/get small set upper bound
1099 static int do_smallSetUpperBound (void *o, Tcl_Interp *interp,
1100 int argc, char **argv)
1102 IrTcl_SetCObj *p = o;
1106 p->smallSetUpperBound = 0;
1109 return get_set_int (&p->smallSetUpperBound, interp, argc, argv);
1113 * do_largeSetLowerBound: Set/get large set lower bound
1115 static int do_largeSetLowerBound (void *o, Tcl_Interp *interp,
1116 int argc, char **argv)
1118 IrTcl_SetCObj *p = o;
1122 p->largeSetLowerBound = 2;
1125 return get_set_int (&p->largeSetLowerBound, interp, argc, argv);
1129 * do_mediumSetPresentNumber: Set/get large set lower bound
1131 static int do_mediumSetPresentNumber (void *o, Tcl_Interp *interp,
1132 int argc, char **argv)
1134 IrTcl_SetCObj *p = o;
1138 p->mediumSetPresentNumber = 0;
1141 return get_set_int (&p->mediumSetPresentNumber, interp, argc, argv);
1145 * do_referenceId: Set/Get referenceId
1147 static int do_referenceId (void *obj, Tcl_Interp *interp,
1148 int argc, char **argv)
1150 IrTcl_SetCObj *p = obj;
1153 p->referenceId = NULL;
1154 else if (argc == -1)
1155 return ir_strdel (interp, &p->referenceId);
1158 free (p->referenceId);
1159 if (ir_strdup (interp, &p->referenceId, argv[2]) == TCL_ERROR)
1162 Tcl_AppendResult (interp, p->referenceId, NULL);
1166 static IrTcl_Method ir_method_tab[] = {
1167 { 1, "comstack", do_comstack },
1168 { 1, "protocol", do_protocol },
1169 { 0, "failback", do_failback },
1171 { 1, "connect", do_connect },
1172 { 0, "protocolVersion", do_protocolVersion },
1173 { 1, "preferredMessageSize", do_preferredMessageSize },
1174 { 1, "maximumRecordSize", do_maximumRecordSize },
1175 { 1, "implementationName", do_implementationName },
1176 { 1, "implementationId", do_implementationId },
1177 { 0, "targetImplementationName", do_targetImplementationName },
1178 { 0, "targetImplementationId", do_targetImplementationId },
1179 { 0, "targetImplementationVersion", do_targetImplementationVersion },
1180 { 0, "userInformationField", do_userInformationField },
1181 { 1, "idAuthentication", do_idAuthentication },
1182 { 0, "options", do_options },
1183 { 0, "init", do_init_request },
1184 { 0, "initResult", do_initResult },
1185 { 0, "disconnect", do_disconnect },
1186 { 0, "callback", do_callback },
1190 static IrTcl_Method ir_set_c_method_tab[] = {
1191 { 0, "databaseNames", do_databaseNames},
1192 { 0, "replaceIndicator", do_replaceIndicator},
1193 { 0, "queryType", do_queryType },
1194 { 0, "smallSetUpperBound", do_smallSetUpperBound},
1195 { 0, "largeSetLowerBound", do_largeSetLowerBound},
1196 { 0, "mediumSetPresentNumber", do_mediumSetPresentNumber},
1197 { 0, "referenceId", do_referenceId },
1202 * ir_obj_method: IR Object methods
1204 static int ir_obj_method (ClientData clientData, Tcl_Interp *interp,
1205 int argc, char **argv)
1207 IrTcl_Methods tab[3];
1208 IrTcl_Obj *p = clientData;
1211 return ir_method_r (clientData, interp, argc, argv, ir_method_tab);
1213 tab[0].tab = ir_method_tab;
1215 tab[1].tab = ir_set_c_method_tab;
1216 tab[1].obj = &p->set_inher;
1219 return ir_method (interp, argc, argv, tab);
1223 * ir_obj_delete: IR Object disposal
1225 static void ir_obj_delete (ClientData clientData)
1227 IrTcl_Obj *obj = clientData;
1228 IrTcl_Methods tab[3];
1231 if (obj->ref_count > 0)
1233 assert (obj->ref_count == 0);
1235 tab[0].tab = ir_method_tab;
1237 tab[1].tab = ir_set_c_method_tab;
1238 tab[1].obj = &obj->set_inher;
1241 ir_method (NULL, -1, NULL, tab);
1242 odr_destroy (obj->odr_in);
1243 odr_destroy (obj->odr_out);
1244 odr_destroy (obj->odr_pr);
1245 free (obj->buf_out);
1251 * ir_obj_mk: IR Object creation
1253 static int ir_obj_mk (ClientData clientData, Tcl_Interp *interp,
1254 int argc, char **argv)
1256 IrTcl_Methods tab[3];
1264 interp->result = "wrong # args";
1267 if (!(obj = ir_malloc (interp, sizeof(*obj))))
1272 obj->bibset = ccl_qual_mk ();
1273 if ((inf = fopen ("default.bib", "r")))
1275 ccl_qual_file (obj->bibset, inf);
1280 obj->odr_in = odr_createmem (ODR_DECODE);
1281 obj->odr_out = odr_createmem (ODR_ENCODE);
1282 obj->odr_pr = odr_createmem (ODR_PRINT);
1284 obj->len_out = 10000;
1285 if (!(obj->buf_out = ir_malloc (interp, obj->len_out)))
1287 odr_setbuf (obj->odr_out, obj->buf_out, obj->len_out, 0);
1292 tab[0].tab = ir_method_tab;
1294 tab[1].tab = ir_set_c_method_tab;
1295 tab[1].obj = &obj->set_inher;
1298 if (ir_method (interp, 0, NULL, tab) == TCL_ERROR)
1300 Tcl_CreateCommand (interp, argv[1], ir_obj_method,
1301 (ClientData) obj, ir_obj_delete);
1305 /* ------------------------------------------------------- */
1307 * do_search: Do search request
1309 static int do_search (void *o, Tcl_Interp *interp,
1310 int argc, char **argv)
1312 Z_SearchRequest req;
1314 Z_APDU apdu, *apdup = &apdu;
1316 IrTcl_SetObj *obj = o;
1317 IrTcl_Obj *p = obj->parent;
1327 interp->result = "wrong # args";
1330 if (!obj->set_inher.num_databaseNames)
1332 interp->result = "no databaseNames";
1337 interp->result = "not connected";
1340 odr_reset (p->odr_out);
1341 apdu.which = Z_APDU_searchRequest;
1342 apdu.u.searchRequest = &req;
1344 bib1.proto = PROTO_Z3950;
1345 bib1.class = CLASS_ATTSET;
1346 bib1.value = VAL_BIB1;
1348 set_referenceId (p->odr_out, &req.referenceId, obj->set_inher.referenceId);
1350 req.smallSetUpperBound = &obj->set_inher.smallSetUpperBound;
1351 req.largeSetLowerBound = &obj->set_inher.largeSetLowerBound;
1352 req.mediumSetPresentNumber = &obj->set_inher.mediumSetPresentNumber;
1353 req.replaceIndicator = &obj->set_inher.replaceIndicator;
1354 req.resultSetName = obj->setName ? obj->setName : "Default";
1355 logf (LOG_DEBUG, "Search, resultSetName %s", req.resultSetName);
1356 req.num_databaseNames = obj->set_inher.num_databaseNames;
1357 req.databaseNames = obj->set_inher.databaseNames;
1358 for (r=0; r < obj->set_inher.num_databaseNames; r++)
1359 logf (LOG_DEBUG, " Database %s", obj->set_inher.databaseNames[r]);
1360 req.smallSetElementSetNames = 0;
1361 req.mediumSetElementSetNames = 0;
1362 req.preferredRecordSyntax = 0;
1365 if (!strcmp (obj->set_inher.queryType, "rpn"))
1367 Z_RPNQuery *RPNquery;
1369 RPNquery = p_query_rpn (p->odr_out, argv[2]);
1372 Tcl_AppendResult (interp, "Syntax error in query", NULL);
1375 RPNquery->attributeSetId = oid_getoidbyent (&bib1);
1376 query.which = Z_Query_type_1;
1377 query.u.type_1 = RPNquery;
1378 logf (LOG_DEBUG, "RPN");
1381 else if (!strcmp (obj->set_inher.queryType, "cclrpn"))
1385 struct ccl_rpn_node *rpn;
1386 Z_RPNQuery *RPNquery;
1388 rpn = ccl_find_str(p->bibset, argv[2], &error, &pos);
1391 Tcl_AppendResult (interp, "CCL error: ",
1392 ccl_err_msg(error), NULL);
1395 ccl_pr_tree (rpn, stderr);
1396 fprintf (stderr, "\n");
1397 assert((RPNquery = ccl_rpn_query(rpn)));
1398 RPNquery->attributeSetId = oid_getoidbyent (&bib1);
1399 query.which = Z_Query_type_1;
1400 query.u.type_1 = RPNquery;
1401 logf (LOG_DEBUG, "CCLRPN");
1404 else if (!strcmp (obj->set_inher.queryType, "ccl"))
1406 query.which = Z_Query_type_2;
1407 query.u.type_2 = &ccl_query;
1408 ccl_query.buf = (unsigned char *) argv[2];
1409 ccl_query.len = strlen (argv[2]);
1410 logf (LOG_DEBUG, "CCL");
1414 interp->result = "unknown query method";
1417 if (!z_APDU (p->odr_out, &apdup, 0))
1419 interp->result = odr_errlist [odr_geterror (p->odr_out)];
1420 odr_reset (p->odr_out);
1423 p->sbuf = odr_getbuf (p->odr_out, &p->slen, NULL);
1424 if ((r=cs_put (p->cs_link, p->sbuf, p->slen)) < 0)
1426 interp->result = "cs_put failed in search";
1431 ir_select_add_write (cs_fileno(p->cs_link), p);
1432 logf (LOG_DEBUG, "Sent part of searchRequest (%d bytes)", p->slen);
1436 logf (LOG_DEBUG, "Whole search request (%d bytes)", p->slen);
1442 * do_resultCount: Get number of hits
1444 static int do_resultCount (void *o, Tcl_Interp *interp,
1445 int argc, char **argv)
1447 IrTcl_SetObj *obj = o;
1451 return get_set_int (&obj->resultCount, interp, argc, argv);
1455 * do_searchStatus: Get search status (after search response)
1457 static int do_searchStatus (void *o, Tcl_Interp *interp,
1458 int argc, char **argv)
1460 IrTcl_SetObj *obj = o;
1464 return get_set_int (&obj->searchStatus, interp, argc, argv);
1468 * do_presentStatus: Get search status (after search/present response)
1470 static int do_presentStatus (void *o, Tcl_Interp *interp,
1471 int argc, char **argv)
1473 IrTcl_SetObj *obj = o;
1477 return get_set_int (&obj->presentStatus, interp, argc, argv);
1481 * do_nextResultSetPosition: Get next result set position
1482 * (after search/present response)
1484 static int do_nextResultSetPosition (void *o, Tcl_Interp *interp,
1485 int argc, char **argv)
1487 IrTcl_SetObj *obj = o;
1491 return get_set_int (&obj->nextResultSetPosition, interp, argc, argv);
1495 * do_setName: Set result Set name
1497 static int do_setName (void *o, Tcl_Interp *interp,
1498 int argc, char **argv)
1500 IrTcl_SetObj *obj = o;
1503 return ir_strdup (interp, &obj->setName, "Default");
1504 else if (argc == -1)
1505 return ir_strdel (interp, &obj->setName);
1508 free (obj->setName);
1509 if (ir_strdup (interp, &obj->setName, argv[2])
1513 Tcl_AppendElement (interp, obj->setName);
1518 * do_numberOfRecordsReturned: Get number of records returned
1520 static int do_numberOfRecordsReturned (void *o, Tcl_Interp *interp,
1521 int argc, char **argv)
1523 IrTcl_SetObj *obj = o;
1527 return get_set_int (&obj->numberOfRecordsReturned, interp, argc, argv);
1531 * do_type: Return type (if any) at position.
1533 static int do_type (void *o, Tcl_Interp *interp, int argc, char **argv)
1535 IrTcl_SetObj *obj = o;
1537 IrTcl_RecordList *rl;
1541 obj->record_list = NULL;
1544 else if (argc == -1)
1546 delete_IR_records (obj);
1551 sprintf (interp->result, "wrong # args");
1554 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
1556 rl = find_IR_record (obj, offset);
1561 case Z_NamePlusRecord_databaseRecord:
1562 interp->result = "DB";
1564 case Z_NamePlusRecord_surrogateDiagnostic:
1565 interp->result = "SD";
1572 * do_diag: Return diagnostic record info
1574 static int do_diag (void *o, Tcl_Interp *interp, int argc, char **argv)
1576 IrTcl_SetObj *obj = o;
1578 IrTcl_RecordList *rl;
1586 sprintf (interp->result, "wrong # args");
1589 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
1591 rl = find_IR_record (obj, offset);
1594 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
1597 if (rl->which != Z_NamePlusRecord_surrogateDiagnostic)
1599 Tcl_AppendResult (interp, "No Diagnostic record at #", argv[2], NULL);
1603 sprintf (buf, "%d", rl->u.diag.condition);
1604 Tcl_AppendElement (interp, buf);
1605 cp = diagbib1_str (rl->u.diag.condition);
1607 Tcl_AppendElement (interp, (char*) cp);
1609 Tcl_AppendElement (interp, "");
1610 if (rl->u.diag.addinfo)
1611 Tcl_AppendElement (interp, (char*) rl->u.diag.addinfo);
1613 Tcl_AppendElement (interp, "");
1618 * do_getMarc: Get ISO2709 Record lines/fields
1620 static int do_getMarc (void *o, Tcl_Interp *interp, int argc, char **argv)
1622 IrTcl_SetObj *obj = o;
1624 IrTcl_RecordList *rl;
1630 sprintf (interp->result, "wrong # args");
1633 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
1635 rl = find_IR_record (obj, offset);
1638 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
1641 if (rl->which != Z_NamePlusRecord_databaseRecord)
1643 Tcl_AppendResult (interp, "No MARC record at #", argv[2], NULL);
1646 return ir_tcl_get_marc (interp, rl->u.dbrec.buf, argc, argv);
1651 * do_responseStatus: Return response status (present or search)
1653 static int do_responseStatus (void *o, Tcl_Interp *interp,
1654 int argc, char **argv)
1656 IrTcl_SetObj *obj = o;
1660 obj->recordFlag = 0;
1661 obj->addinfo = NULL;
1664 else if (argc == -1)
1665 return ir_strdel (interp, &obj->addinfo);
1666 if (!obj->recordFlag)
1668 Tcl_AppendElement (interp, "OK");
1673 case Z_Records_DBOSD:
1674 Tcl_AppendElement (interp, "DBOSD");
1677 return mk_nonSurrogateDiagnostics (interp, obj->condition,
1684 * do_present: Perform Present Request
1687 static int do_present (void *o, Tcl_Interp *interp,
1688 int argc, char **argv)
1690 IrTcl_SetObj *obj = o;
1691 IrTcl_Obj *p = obj->parent;
1692 Z_APDU apdu, *apdup = &apdu;
1693 Z_PresentRequest req;
1702 if (Tcl_GetInt (interp, argv[2], &start) == TCL_ERROR)
1709 if (Tcl_GetInt (interp, argv[3], &number) == TCL_ERROR)
1716 interp->result = "not connected";
1719 odr_reset (p->odr_out);
1721 obj->number = number;
1723 apdu.which = Z_APDU_presentRequest;
1724 apdu.u.presentRequest = &req;
1726 set_referenceId (p->odr_out, &req.referenceId, obj->set_inher.referenceId);
1728 req.resultSetId = obj->setName ? obj->setName : "Default";
1730 req.resultSetStartPoint = &start;
1731 req.numberOfRecordsRequested = &number;
1732 req.elementSetNames = 0;
1733 req.preferredRecordSyntax = 0;
1735 if (!z_APDU (p->odr_out, &apdup, 0))
1737 interp->result = odr_errlist [odr_geterror (p->odr_out)];
1738 odr_reset (p->odr_out);
1741 p->sbuf = odr_getbuf (p->odr_out, &p->slen, NULL);
1742 if ((r=cs_put (p->cs_link, p->sbuf, p->slen)) < 0)
1744 interp->result = "cs_put failed in present";
1749 ir_select_add_write (cs_fileno(p->cs_link), p);
1750 logf (LOG_DEBUG, "Part of present request, start=%d, num=%d"
1751 " (%d bytes)", start, number, p->slen);
1755 logf (LOG_DEBUG, "Whole present request, start=%d, num=%d"
1756 " (%d bytes)", start, number, p->slen);
1762 * do_loadFile: Load result set from file
1765 static int do_loadFile (void *o, Tcl_Interp *interp,
1766 int argc, char **argv)
1768 IrTcl_SetObj *setobj = o;
1778 interp->result = "wrong # args";
1781 inf = fopen (argv[2], "r");
1784 Tcl_AppendResult (interp, "Cannot open ", argv[2], NULL);
1787 while ((buf = ir_tcl_fread_marc (inf, &size)))
1789 IrTcl_RecordList *rl;
1791 rl = new_IR_record (setobj, no, Z_NamePlusRecord_databaseRecord);
1792 rl->u.dbrec.buf = buf;
1793 rl->u.dbrec.size = size;
1796 setobj->numberOfRecordsReturned = no-1;
1801 static IrTcl_Method ir_set_method_tab[] = {
1802 { 0, "search", do_search },
1803 { 0, "searchStatus", do_searchStatus },
1804 { 0, "presentStatus", do_presentStatus },
1805 { 0, "nextResultSetPosition", do_nextResultSetPosition },
1806 { 0, "setName", do_setName },
1807 { 0, "resultCount", do_resultCount },
1808 { 0, "numberOfRecordsReturned", do_numberOfRecordsReturned },
1809 { 0, "present", do_present },
1810 { 0, "type", do_type },
1811 { 0, "getMarc", do_getMarc },
1812 { 0, "diag", do_diag },
1813 { 0, "responseStatus", do_responseStatus },
1814 { 0, "loadFile", do_loadFile },
1819 * ir_set_obj_method: IR Set Object methods
1821 static int ir_set_obj_method (ClientData clientData, Tcl_Interp *interp,
1822 int argc, char **argv)
1824 IrTcl_Methods tabs[3];
1825 IrTcl_SetObj *p = clientData;
1829 interp->result = "wrong # args";
1832 tabs[0].tab = ir_set_method_tab;
1834 tabs[1].tab = ir_set_c_method_tab;
1835 tabs[1].obj = &p->set_inher;
1838 return ir_method (interp, argc, argv, tabs);
1842 * ir_set_obj_delete: IR Set Object disposal
1844 static void ir_set_obj_delete (ClientData clientData)
1846 IrTcl_Methods tabs[3];
1847 IrTcl_SetObj *p = clientData;
1849 tabs[0].tab = ir_set_method_tab;
1851 tabs[1].tab = ir_set_c_method_tab;
1852 tabs[1].obj = &p->set_inher;
1855 ir_method (NULL, -1, NULL, tabs);
1861 * ir_set_obj_mk: IR Set Object creation
1863 static int ir_set_obj_mk (ClientData clientData, Tcl_Interp *interp,
1864 int argc, char **argv)
1866 IrTcl_Methods tabs[3];
1869 if (argc < 2 || argc > 3)
1871 interp->result = "wrong # args";
1874 if (!(obj = ir_malloc (interp, sizeof(*obj))))
1878 Tcl_CmdInfo parent_info;
1883 if (!Tcl_GetCommandInfo (interp, argv[2], &parent_info))
1885 interp->result = "No parent";
1888 obj->parent = (IrTcl_Obj *) parent_info.clientData;
1890 dst = &obj->set_inher;
1891 src = &obj->parent->set_inher;
1893 dst->num_databaseNames = src->num_databaseNames;
1894 if (!(dst->databaseNames =
1895 ir_malloc (interp, sizeof (*dst->databaseNames)
1896 * dst->num_databaseNames)))
1898 for (i = 0; i < dst->num_databaseNames; i++)
1900 if (ir_strdup (interp, &dst->databaseNames[i],
1901 src->databaseNames[i]) == TCL_ERROR)
1904 if (ir_strdup (interp, &dst->queryType, src->queryType)
1908 if (ir_strdup (interp, &dst->referenceId, src->referenceId)
1912 dst->replaceIndicator = src->replaceIndicator;
1913 dst->smallSetUpperBound = src->smallSetUpperBound;
1914 dst->largeSetLowerBound = src->largeSetLowerBound;
1915 dst->mediumSetPresentNumber = src->mediumSetPresentNumber;
1920 tabs[0].tab = ir_set_method_tab;
1924 if (ir_method (interp, 0, NULL, tabs) == TCL_ERROR)
1927 Tcl_CreateCommand (interp, argv[1], ir_set_obj_method,
1928 (ClientData) obj, ir_set_obj_delete);
1932 /* ------------------------------------------------------- */
1935 * do_scan: Perform scan
1937 static int do_scan (void *o, Tcl_Interp *interp, int argc, char **argv)
1940 Z_APDU apdu, *apdup = &apdu;
1941 IrTcl_ScanObj *obj = o;
1942 IrTcl_Obj *p = obj->parent;
1946 struct ccl_rpn_node *rpn;
1955 interp->result = "wrong # args";
1958 if (!p->set_inher.num_databaseNames)
1960 interp->result = "no databaseNames";
1965 interp->result = "not connected";
1968 odr_reset (p->odr_out);
1970 bib1.proto = PROTO_Z3950;
1971 bib1.class = CLASS_ATTSET;
1972 bib1.value = VAL_BIB1;
1974 apdu.which = Z_APDU_scanRequest;
1975 apdu.u.scanRequest = &req;
1976 set_referenceId (p->odr_out, &req.referenceId, p->set_inher.referenceId);
1977 req.num_databaseNames = p->set_inher.num_databaseNames;
1978 req.databaseNames = p->set_inher.databaseNames;
1979 req.attributeSet = oid_getoidbyent (&bib1);
1982 if (!(req.termListAndStartPoint = p_query_scan (p->odr_out, argv[2])))
1984 Tcl_AppendResult (interp, "Syntax error in query", NULL);
1988 rpn = ccl_find_str(p->bibset, argv[2], &r, &pos);
1991 Tcl_AppendResult (interp, "CCL error: ", ccl_err_msg (r), NULL);
1994 ccl_pr_tree (rpn, stderr);
1995 fprintf (stderr, "\n");
1996 if (!(req.termListAndStartPoint = ccl_scan_query (rpn)))
1999 req.stepSize = &obj->stepSize;
2000 req.numberOfTermsRequested = &obj->numberOfTermsRequested;
2001 req.preferredPositionInResponse = &obj->preferredPositionInResponse;
2002 logf (LOG_DEBUG, "stepSize=%d", *req.stepSize);
2003 logf (LOG_DEBUG, "numberOfTermsRequested=%d",
2004 *req.numberOfTermsRequested);
2005 logf (LOG_DEBUG, "preferredPositionInResponse=%d",
2006 *req.preferredPositionInResponse);
2008 if (!z_APDU (p->odr_out, &apdup, 0))
2010 interp->result = odr_errlist [odr_geterror (p->odr_out)];
2011 odr_reset (p->odr_out);
2014 p->sbuf = odr_getbuf (p->odr_out, &p->slen, NULL);
2015 if ((r=cs_put (p->cs_link, p->sbuf, p->slen)) < 0)
2017 interp->result = "cs_put failed in scan";
2022 ir_select_add_write (cs_fileno(p->cs_link), p);
2023 logf (LOG_DEBUG, "Sent part of scanRequest (%d bytes)", p->slen);
2027 logf (LOG_DEBUG, "Whole scan request (%d bytes)", p->slen);
2033 * do_stepSize: Set/get replace Step Size
2035 static int do_stepSize (void *obj, Tcl_Interp *interp,
2036 int argc, char **argv)
2038 IrTcl_ScanObj *p = obj;
2044 return get_set_int (&p->stepSize, interp, argc, argv);
2048 * do_numberOfTermsRequested: Set/get Number of Terms requested
2050 static int do_numberOfTermsRequested (void *obj, Tcl_Interp *interp,
2051 int argc, char **argv)
2053 IrTcl_ScanObj *p = obj;
2057 p->numberOfTermsRequested = 20;
2060 return get_set_int (&p->numberOfTermsRequested, interp, argc, argv);
2065 * do_preferredPositionInResponse: Set/get preferred Position
2067 static int do_preferredPositionInResponse (void *obj, Tcl_Interp *interp,
2068 int argc, char **argv)
2070 IrTcl_ScanObj *p = obj;
2074 p->preferredPositionInResponse = 1;
2077 return get_set_int (&p->preferredPositionInResponse, interp, argc, argv);
2081 * do_scanStatus: Get scan status
2083 static int do_scanStatus (void *obj, Tcl_Interp *interp,
2084 int argc, char **argv)
2086 IrTcl_ScanObj *p = obj;
2090 return get_set_int (&p->scanStatus, interp, argc, argv);
2094 * do_numberOfEntriesReturned: Get number of Entries returned
2096 static int do_numberOfEntriesReturned (void *obj, Tcl_Interp *interp,
2097 int argc, char **argv)
2099 IrTcl_ScanObj *p = obj;
2103 return get_set_int (&p->numberOfEntriesReturned, interp, argc, argv);
2107 * do_positionOfTerm: Get position of Term
2109 static int do_positionOfTerm (void *obj, Tcl_Interp *interp,
2110 int argc, char **argv)
2112 IrTcl_ScanObj *p = obj;
2116 return get_set_int (&p->positionOfTerm, interp, argc, argv);
2120 * do_scanLine: get Scan Line (surrogate or normal) after response
2122 static int do_scanLine (void *obj, Tcl_Interp *interp, int argc, char **argv)
2124 IrTcl_ScanObj *p = obj;
2130 p->entries_flag = 0;
2132 p->nonSurrogateDiagnostics = NULL;
2135 else if (argc == -1)
2137 p->entries_flag = 0;
2138 /* release entries */
2140 /* release non diagnostics */
2141 p->nonSurrogateDiagnostics = NULL;
2146 interp->result = "wrong # args";
2149 if (Tcl_GetInt (interp, argv[2], &i) == TCL_ERROR)
2151 if (!p->entries_flag || p->which != Z_ListEntries_entries || !p->entries
2152 || i >= p->num_entries || i < 0)
2154 switch (p->entries[i].which)
2156 case Z_Entry_termInfo:
2157 Tcl_AppendElement (interp, "T");
2158 if (p->entries[i].u.term.buf)
2159 Tcl_AppendElement (interp, p->entries[i].u.term.buf);
2161 Tcl_AppendElement (interp, "");
2162 sprintf (numstr, "%d", p->entries[i].u.term.globalOccurrences);
2163 Tcl_AppendElement (interp, numstr);
2165 case Z_Entry_surrogateDiagnostic:
2167 mk_nonSurrogateDiagnostics (interp, p->entries[i].u.diag.condition,
2168 p->entries[i].u.diag.addinfo);
2174 static IrTcl_Method ir_scan_method_tab[] = {
2175 { 0, "scan", do_scan },
2176 { 0, "stepSize", do_stepSize },
2177 { 0, "numberOfTermsRequested", do_numberOfTermsRequested },
2178 { 0, "preferredPositionInResponse", do_preferredPositionInResponse },
2179 { 0, "scanStatus", do_scanStatus },
2180 { 0, "numberOfEntriesReturned", do_numberOfEntriesReturned },
2181 { 0, "positionOfTerm", do_positionOfTerm },
2182 { 0, "scanLine", do_scanLine },
2187 * ir_scan_obj_method: IR Scan Object methods
2189 static int ir_scan_obj_method (ClientData clientData, Tcl_Interp *interp,
2190 int argc, char **argv)
2192 IrTcl_Methods tabs[2];
2196 interp->result = "wrong # args";
2199 tabs[0].tab = ir_scan_method_tab;
2200 tabs[0].obj = clientData;
2203 return ir_method (interp, argc, argv, tabs);
2207 * ir_scan_obj_delete: IR Scan Object disposal
2209 static void ir_scan_obj_delete (ClientData clientData)
2211 IrTcl_Methods tabs[2];
2212 IrTcl_ScanObj *obj = clientData;
2214 tabs[0].tab = ir_scan_method_tab;
2218 ir_method (NULL, -1, NULL, tabs);
2223 * ir_scan_obj_mk: IR Scan Object creation
2225 static int ir_scan_obj_mk (ClientData clientData, Tcl_Interp *interp,
2226 int argc, char **argv)
2228 Tcl_CmdInfo parent_info;
2230 IrTcl_Methods tabs[2];
2234 interp->result = "wrong # args";
2237 if (!Tcl_GetCommandInfo (interp, argv[2], &parent_info))
2239 interp->result = "No parent";
2242 if (!(obj = ir_malloc (interp, sizeof(*obj))))
2245 obj->parent = (IrTcl_Obj *) parent_info.clientData;
2247 tabs[0].tab = ir_scan_method_tab;
2251 if (ir_method (interp, 0, NULL, tabs) == TCL_ERROR)
2253 Tcl_CreateCommand (interp, argv[1], ir_scan_obj_method,
2254 (ClientData) obj, ir_scan_obj_delete);
2258 /* ------------------------------------------------------- */
2260 static void ir_initResponse (void *obj, Z_InitResponse *initrs)
2264 p->initResult = *initrs->result ? 1 : 0;
2265 if (!*initrs->result)
2266 logf (LOG_DEBUG, "Connection rejected by target");
2268 logf (LOG_DEBUG, "Connection accepted by target");
2270 get_referenceId (&p->set_inher.referenceId, initrs->referenceId);
2272 free (p->targetImplementationId);
2273 ir_strdup (p->interp, &p->targetImplementationId,
2274 initrs->implementationId);
2275 free (p->targetImplementationName);
2276 ir_strdup (p->interp, &p->targetImplementationName,
2277 initrs->implementationName);
2278 free (p->targetImplementationVersion);
2279 ir_strdup (p->interp, &p->targetImplementationVersion,
2280 initrs->implementationVersion);
2282 p->maximumRecordSize = *initrs->maximumRecordSize;
2283 p->preferredMessageSize = *initrs->preferredMessageSize;
2285 memcpy (&p->options, initrs->options, sizeof(initrs->options));
2286 memcpy (&p->protocolVersion, initrs->protocolVersion,
2287 sizeof(initrs->protocolVersion));
2288 free (p->userInformationField);
2289 p->userInformationField = NULL;
2290 if (initrs->userInformationField)
2294 if (initrs->userInformationField->which == ODR_EXTERNAL_octet &&
2295 (p->userInformationField =
2297 initrs->userInformationField->u.octet_aligned->len)
2300 memcpy (p->userInformationField,
2301 initrs->userInformationField->u.octet_aligned->buf,
2303 (p->userInformationField)[len] = '\0';
2308 static void ir_handleRecords (void *o, Z_Records *zrs)
2311 IrTcl_SetObj *setobj = p->set_child;
2313 setobj->which = zrs->which;
2314 setobj->recordFlag = 1;
2315 if (zrs->which == Z_Records_NSD)
2317 const char *addinfo;
2319 setobj->numberOfRecordsReturned = 0;
2320 setobj->condition = *zrs->u.nonSurrogateDiagnostic->condition;
2321 free (setobj->addinfo);
2322 setobj->addinfo = NULL;
2323 addinfo = zrs->u.nonSurrogateDiagnostic->addinfo;
2324 if (addinfo && (setobj->addinfo = malloc (strlen(addinfo) + 1)))
2325 strcpy (setobj->addinfo, addinfo);
2326 logf (LOG_DEBUG, "Diagnostic response. %s (%d): %s",
2327 diagbib1_str (setobj->condition),
2329 setobj->addinfo ? setobj->addinfo : "");
2334 IrTcl_RecordList *rl;
2336 setobj->numberOfRecordsReturned =
2337 zrs->u.databaseOrSurDiagnostics->num_records;
2338 logf (LOG_DEBUG, "Got %d records", setobj->numberOfRecordsReturned);
2339 for (offset = 0; offset<setobj->numberOfRecordsReturned; offset++)
2341 rl = new_IR_record (setobj, setobj->start + offset,
2342 zrs->u.databaseOrSurDiagnostics->
2343 records[offset]->which);
2344 if (rl->which == Z_NamePlusRecord_surrogateDiagnostic)
2348 diagrec = zrs->u.databaseOrSurDiagnostics->
2349 records[offset]->u.surrogateDiagnostic;
2351 rl->u.diag.condition = *diagrec->condition;
2352 if (diagrec->addinfo && (rl->u.diag.addinfo =
2353 malloc (strlen (diagrec->addinfo)+1)))
2354 strcpy (rl->u.diag.addinfo, diagrec->addinfo);
2358 Z_DatabaseRecord *zr;
2361 zr = zrs->u.databaseOrSurDiagnostics->records[offset]
2363 oe = (Odr_external*) zr;
2364 rl->u.dbrec.size = zr->u.octet_aligned->len;
2365 if (oe->which == ODR_EXTERNAL_octet && rl->u.dbrec.size > 0)
2367 const char *buf = (char*) zr->u.octet_aligned->buf;
2368 if ((rl->u.dbrec.buf = malloc (rl->u.dbrec.size)))
2369 memcpy (rl->u.dbrec.buf, buf, rl->u.dbrec.size);
2372 rl->u.dbrec.buf = NULL;
2378 static void ir_searchResponse (void *o, Z_SearchResponse *searchrs)
2381 IrTcl_SetObj *setobj = p->set_child;
2382 Z_Records *zrs = searchrs->records;
2384 logf (LOG_DEBUG, "Received search response");
2387 logf (LOG_DEBUG, "Search response, no object!");
2390 setobj->searchStatus = searchrs->searchStatus ? 1 : 0;
2391 get_referenceId (&setobj->set_inher.referenceId, searchrs->referenceId);
2392 setobj->resultCount = *searchrs->resultCount;
2393 if (searchrs->presentStatus)
2394 setobj->presentStatus = *searchrs->presentStatus;
2395 if (searchrs->nextResultSetPosition)
2396 setobj->nextResultSetPosition = *searchrs->nextResultSetPosition;
2398 logf (LOG_DEBUG, "Search response %d, %d hits",
2399 setobj->searchStatus, setobj->resultCount);
2401 ir_handleRecords (o, zrs);
2403 setobj->recordFlag = 0;
2407 static void ir_presentResponse (void *o, Z_PresentResponse *presrs)
2410 IrTcl_SetObj *setobj = p->set_child;
2411 Z_Records *zrs = presrs->records;
2413 logf (LOG_DEBUG, "Received present response");
2416 logf (LOG_DEBUG, "Present response, no object!");
2419 setobj->presentStatus = *presrs->presentStatus;
2420 get_referenceId (&setobj->set_inher.referenceId, presrs->referenceId);
2421 setobj->nextResultSetPosition = *presrs->nextResultSetPosition;
2423 ir_handleRecords (o, zrs);
2426 setobj->recordFlag = 0;
2427 logf (LOG_DEBUG, "No records!");
2431 static void ir_scanResponse (void *o, Z_ScanResponse *scanrs)
2434 IrTcl_ScanObj *scanobj = p->scan_child;
2436 logf (LOG_DEBUG, "Received scanResponse");
2438 get_referenceId (&p->set_inher.referenceId, scanrs->referenceId);
2439 scanobj->scanStatus = *scanrs->scanStatus;
2440 logf (LOG_DEBUG, "scanStatus=%d", scanobj->scanStatus);
2442 if (scanrs->stepSize)
2443 scanobj->stepSize = *scanrs->stepSize;
2444 logf (LOG_DEBUG, "stepSize=%d", scanobj->stepSize);
2446 scanobj->numberOfEntriesReturned = *scanrs->numberOfEntriesReturned;
2447 logf (LOG_DEBUG, "numberOfEntriesReturned=%d",
2448 scanobj->numberOfEntriesReturned);
2450 if (scanrs->positionOfTerm)
2451 scanobj->positionOfTerm = *scanrs->positionOfTerm;
2453 scanobj->positionOfTerm = -1;
2454 logf (LOG_DEBUG, "positionOfTerm=%d", scanobj->positionOfTerm);
2456 free (scanobj->entries);
2457 scanobj->entries = NULL;
2458 free (scanobj->nonSurrogateDiagnostics);
2459 scanobj->nonSurrogateDiagnostics = NULL;
2461 if (scanrs->entries)
2466 scanobj->entries_flag = 1;
2467 scanobj->which = scanrs->entries->which;
2468 switch (scanobj->which)
2470 case Z_ListEntries_entries:
2471 scanobj->num_entries = scanrs->entries->u.entries->num_entries;
2472 scanobj->entries = malloc (scanobj->num_entries *
2473 sizeof(*scanobj->entries));
2474 for (i=0; i<scanobj->num_entries; i++)
2476 ze = scanrs->entries->u.entries->entries[i];
2477 scanobj->entries[i].which = ze->which;
2480 case Z_Entry_termInfo:
2481 if (ze->u.termInfo->term->which == Z_Term_general)
2483 int l = ze->u.termInfo->term->u.general->len;
2484 scanobj->entries[i].u.term.buf = malloc (1+l);
2485 memcpy (scanobj->entries[i].u.term.buf,
2486 ze->u.termInfo->term->u.general->buf,
2488 scanobj->entries[i].u.term.buf[l] = '\0';
2491 scanobj->entries[i].u.term.buf = NULL;
2492 if (ze->u.termInfo->globalOccurrences)
2493 scanobj->entries[i].u.term.globalOccurrences =
2494 *ze->u.termInfo->globalOccurrences;
2496 scanobj->entries[i].u.term.globalOccurrences = 0;
2498 case Z_Entry_surrogateDiagnostic:
2499 scanobj->entries[i].u.diag.addinfo =
2500 malloc (1+strlen(ze->u.surrogateDiagnostic->
2502 strcpy (scanobj->entries[i].u.diag.addinfo,
2503 ze->u.surrogateDiagnostic->addinfo);
2504 scanobj->entries[i].u.diag.condition =
2505 *ze->u.surrogateDiagnostic->condition;
2510 case Z_ListEntries_nonSurrogateDiagnostics:
2511 scanobj->num_diagRecs = scanrs->entries->
2512 u.nonSurrogateDiagnostics->num_diagRecs;
2513 scanobj->nonSurrogateDiagnostics = malloc (scanobj->num_diagRecs *
2514 sizeof(*scanobj->nonSurrogateDiagnostics));
2519 scanobj->entries_flag = 0;
2523 * ir_select_read: handle incoming packages
2525 void ir_select_read (ClientData clientData)
2527 IrTcl_Obj *p = clientData;
2533 r = cs_rcvconnect (p->cs_link);
2537 ir_select_remove_write (cs_fileno (p->cs_link), p);
2540 logf (LOG_DEBUG, "cs_rcvconnect error");
2542 Tcl_Eval (p->interp, p->failback);
2543 do_disconnect (p, NULL, 2, NULL);
2547 Tcl_Eval (p->interp, p->callback);
2552 /* signal one more use of ir object - callbacks must not
2553 release the ir memory (p pointer) */
2555 if ((r=cs_get (p->cs_link, &p->buf_in, &p->len_in)) <= 0)
2557 logf (LOG_DEBUG, "cs_get failed, code %d", r);
2558 ir_select_remove (cs_fileno (p->cs_link), p);
2560 Tcl_Eval (p->interp, p->failback);
2561 do_disconnect (p, NULL, 2, NULL);
2563 /* relase ir object now if callback deleted it */
2569 odr_setbuf (p->odr_in, p->buf_in, r, 0);
2570 logf (LOG_DEBUG, "cs_get ok, got %d", r);
2571 if (!z_APDU (p->odr_in, &apdu, 0))
2573 logf (LOG_DEBUG, "%s", odr_errlist [odr_geterror (p->odr_in)]);
2575 Tcl_Eval (p->interp, p->failback);
2576 do_disconnect (p, NULL, 2, NULL);
2578 /* release ir object now if callback deleted it */
2584 case Z_APDU_initResponse:
2585 ir_initResponse (p, apdu->u.initResponse);
2587 case Z_APDU_searchResponse:
2588 ir_searchResponse (p, apdu->u.searchResponse);
2590 case Z_APDU_presentResponse:
2591 ir_presentResponse (p, apdu->u.presentResponse);
2593 case Z_APDU_scanResponse:
2594 ir_scanResponse (p, apdu->u.scanResponse);
2597 logf (LOG_WARN, "Received unknown APDU type (%d)", apdu->which);
2599 Tcl_Eval (p->interp, p->failback);
2600 do_disconnect (p, NULL, 2, NULL);
2602 odr_reset (p->odr_in);
2604 Tcl_Eval (p->interp, p->callback);
2605 if (p->ref_count == 1)
2611 } while (p->cs_link && cs_more (p->cs_link));
2615 * ir_select_write: handle outgoing packages - not yet written.
2617 void ir_select_write (ClientData clientData)
2619 IrTcl_Obj *p = clientData;
2622 logf (LOG_DEBUG, "In write handler");
2625 r = cs_rcvconnect (p->cs_link);
2631 logf (LOG_DEBUG, "cs_rcvconnect error");
2632 ir_select_remove_write (cs_fileno (p->cs_link), p);
2634 Tcl_Eval (p->interp, p->failback);
2635 do_disconnect (p, NULL, 2, NULL);
2638 ir_select_remove_write (cs_fileno (p->cs_link), p);
2640 Tcl_Eval (p->interp, p->callback);
2643 if ((r=cs_put (p->cs_link, p->sbuf, p->slen)) < 0)
2645 logf (LOG_DEBUG, "select write fail");
2647 Tcl_Eval (p->interp, p->failback);
2648 do_disconnect (p, NULL, 2, NULL);
2650 else if (r == 0) /* remove select bit */
2652 ir_select_remove_write (cs_fileno (p->cs_link), p);
2656 /* ------------------------------------------------------- */
2659 * ir_tcl_init: Registration of TCL commands.
2661 int ir_tcl_init (Tcl_Interp *interp)
2663 Tcl_CreateCommand (interp, "ir", ir_obj_mk, (ClientData) NULL,
2664 (Tcl_CmdDeleteProc *) NULL);
2665 Tcl_CreateCommand (interp, "ir-set", ir_set_obj_mk,
2666 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
2667 Tcl_CreateCommand (interp, "ir-scan", ir_scan_obj_mk,
2668 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);