2 * IR toolkit for tcl/tk
4 * See the file LICENSE for details.
5 * Sebastian Hammer, Adam Dickmeiss
8 * Revision 1.40 1995-06-14 13:37:18 adam
9 * Setting recordType implemented.
10 * Setting implementationVersion implemented.
11 * Settings implementationId / implementationName edited.
13 * Revision 1.39 1995/06/08 10:26:32 adam
14 * Bug fix in ir_strdup.
16 * Revision 1.38 1995/06/01 16:36:47 adam
17 * About buttons. Minor bug fixes.
19 * Revision 1.37 1995/06/01 07:31:20 adam
20 * Rename of many typedefs -> IrTcl_...
22 * Revision 1.36 1995/05/31 13:09:59 adam
23 * Client searches/presents may be interrupted.
24 * New moving book-logo.
26 * Revision 1.35 1995/05/31 08:36:33 adam
27 * Bug fix in client.tcl: didn't save options on clientrc.tcl.
28 * New method: referenceId. More work on scan.
30 * Revision 1.34 1995/05/29 10:33:42 adam
31 * README and rename of startup script.
33 * Revision 1.33 1995/05/29 09:15:11 quinn
34 * Changed CS_SR to PROTO_SR, etc.
36 * Revision 1.32 1995/05/29 08:44:16 adam
37 * Work on delete of objects.
39 * Revision 1.31 1995/05/26 11:44:10 adam
40 * Bugs fixed. More work on MARC utilities and queries. Test
41 * client is up-to-date again.
43 * Revision 1.30 1995/05/26 08:54:11 adam
44 * New MARC utilities. Uses prefix query.
46 * Revision 1.29 1995/05/24 14:10:22 adam
47 * Work on idAuthentication, protocolVersion and options.
49 * Revision 1.28 1995/05/23 15:34:48 adam
50 * Many new settings, userInformationField, smallSetUpperBound, etc.
51 * A number of settings are inherited when ir-set is executed.
52 * This version is incompatible with the graphical test client (client.tcl).
54 * Revision 1.27 1995/05/11 15:34:47 adam
55 * Scan request changed a bit. This version works with RLG.
57 * Revision 1.26 1995/04/18 16:11:51 adam
58 * First version of graphical Scan. Some work on query-by-form.
60 * Revision 1.25 1995/04/17 09:37:17 adam
61 * Further development of scan.
63 * Revision 1.24 1995/04/11 14:16:42 adam
64 * Further work on scan. Response works. Entries aren't saved yet.
66 * Revision 1.23 1995/04/10 10:50:27 adam
67 * Result-set name defaults to suffix of ir-set name.
68 * Started working on scan. Not finished at this point.
70 * Revision 1.22 1995/03/31 10:43:03 adam
71 * More robust when getting bad MARC records.
73 * Revision 1.21 1995/03/31 08:56:37 adam
74 * New button "Search".
76 * Revision 1.20 1995/03/29 16:07:09 adam
77 * Bug fix: Didn't use setName in present request.
79 * Revision 1.19 1995/03/28 12:45:23 adam
80 * New ir method failback: called on disconnect/protocol error.
81 * New ir set/get method: protocol: SR / Z3950.
82 * Simple popup and disconnect when failback is invoked.
84 * Revision 1.18 1995/03/21 15:50:12 adam
87 * Revision 1.17 1995/03/21 13:41:03 adam
88 * Comstack cs_create not used too often. Non-blocking connect.
90 * Revision 1.16 1995/03/21 08:26:06 adam
91 * New method, setName, to specify the result set name (other than Default).
92 * New method, responseStatus, which returns diagnostic info, if any, after
93 * present response / search response.
95 * Revision 1.15 1995/03/20 15:24:07 adam
96 * Diagnostic records saved on searchResponse.
98 * Revision 1.14 1995/03/20 08:53:22 adam
99 * Event loop in tclmain.c rewritten. New method searchStatus.
101 * Revision 1.13 1995/03/17 18:26:17 adam
102 * Non-blocking i/o used now. Database names popup as cascade items.
104 * Revision 1.12 1995/03/17 15:45:00 adam
105 * Improved target/database setup.
107 * Revision 1.11 1995/03/16 17:54:03 adam
108 * Minor changes really.
110 * Revision 1.10 1995/03/15 16:14:50 adam
111 * Blocking arg in cs_create changed.
113 * Revision 1.9 1995/03/15 13:59:24 adam
116 * Revision 1.8 1995/03/15 08:25:16 adam
117 * New method presentStatus to check for error on present. Misc. cleanup
118 * of IrTcl_RecordList manipulations. Full MARC record presentation in
121 * Revision 1.7 1995/03/14 17:32:29 adam
122 * Presentation of full Marc record in popup window.
124 * Revision 1.6 1995/03/12 19:31:55 adam
125 * Pattern matching implemented when retrieving MARC records. More
126 * diagnostic functions.
128 * Revision 1.5 1995/03/10 18:00:15 adam
129 * Actual presentation in line-by-line format. RPN query support.
131 * Revision 1.4 1995/03/09 16:15:08 adam
132 * First presentRequest attempts. Hot-target list.
138 #include <sys/time.h>
148 int (*method) (void *obj, Tcl_Interp *interp, int argc, char **argv);
156 static int do_disconnect (void *obj, Tcl_Interp *interp,
157 int argc, char **argv);
159 static IrTcl_RecordList *new_IR_record (IrTcl_SetObj *setobj,
162 IrTcl_RecordList *rl;
164 for (rl = setobj->record_list; rl; rl = rl->next)
170 case Z_NamePlusRecord_databaseRecord:
171 free (rl->u.dbrec.buf);
172 rl->u.dbrec.buf = NULL;
174 case Z_NamePlusRecord_surrogateDiagnostic:
175 free (rl->u.diag.addinfo);
176 rl->u.diag.addinfo = NULL;
184 rl = malloc (sizeof(*rl));
186 rl->next = setobj->record_list;
188 setobj->record_list = rl;
194 static IrTcl_RecordList *find_IR_record (IrTcl_SetObj *setobj, int no)
196 IrTcl_RecordList *rl;
198 for (rl = setobj->record_list; rl; rl = rl->next)
204 static void delete_IR_records (IrTcl_SetObj *setobj)
206 IrTcl_RecordList *rl, *rl1;
208 for (rl = setobj->record_list; rl; rl = rl1)
212 case Z_NamePlusRecord_databaseRecord:
213 free (rl->u.dbrec.buf);
215 case Z_NamePlusRecord_surrogateDiagnostic:
216 free (rl->u.diag.addinfo);
222 setobj->record_list = NULL;
226 * getsetint: Set/get integer value
228 static int get_set_int (int *val, Tcl_Interp *interp, int argc, char **argv)
234 if (Tcl_GetInt (interp, argv[2], val)==TCL_ERROR)
237 sprintf (buf, "%d", *val);
238 Tcl_AppendResult (interp, buf, NULL);
243 * mk_nonSurrogateDiagnostics: Make Tcl result with diagnostic info
245 static int mk_nonSurrogateDiagnostics (Tcl_Interp *interp,
246 int condition, const char *addinfo)
251 Tcl_AppendElement (interp, "NSD");
252 sprintf (buf, "%d", condition);
253 Tcl_AppendElement (interp, buf);
254 cp = diagbib1_str (condition);
256 Tcl_AppendElement (interp, (char*) cp);
258 Tcl_AppendElement (interp, "");
260 Tcl_AppendElement (interp, (char*) addinfo);
262 Tcl_AppendElement (interp, "");
267 * ir_method: Search for method in table and invoke method handler
269 int ir_method (Tcl_Interp *interp, int argc, char **argv, IrTcl_Methods *tab)
271 IrTcl_Methods *tab_i = tab;
274 for (tab_i = tab; tab_i->tab; tab_i++)
275 for (t = tab_i->tab; t->name; t++)
278 if ((*t->method)(tab_i->obj, interp, argc, argv) == TCL_ERROR)
282 if (!strcmp (t->name, argv[1]))
283 return (*t->method)(tab_i->obj, interp, argc, argv);
287 Tcl_AppendResult (interp, "Bad method. Possible methods:", NULL);
288 for (tab_i = tab; tab_i->tab; tab_i++)
289 for (t = tab_i->tab; t->name; t++)
290 Tcl_AppendResult (interp, " ", t->name, NULL);
295 * ir_method_r: Get status for all readable elements
297 int ir_method_r (void *obj, Tcl_Interp *interp, int argc, char **argv,
305 for (; tab->name; tab++)
308 argv_n[1] = tab->name;
309 Tcl_AppendResult (interp, "{", NULL);
310 (*tab->method)(obj, interp, argc_n, argv_n);
311 Tcl_AppendResult (interp, "} ", NULL);
317 * ir_named_bits: get/set named bits
319 int ir_named_bits (struct ir_named_entry *tab, Odr_bitmask *ob,
320 Tcl_Interp *interp, int argc, char **argv)
322 struct ir_named_entry *ti;
327 for (no = 0; no < argc; no++)
329 for (ti = tab; ti->name; ti++)
330 if (!strcmp (argv[no], ti->name))
332 ODR_MASK_SET (ob, ti->pos);
337 Tcl_AppendResult (interp, "Bad bit mask: ", argv[no], NULL);
343 for (ti = tab; ti->name; ti++)
344 if (ODR_MASK_GET (ob, ti->pos))
345 Tcl_AppendElement (interp, ti->name);
350 * ir_strdup: Duplicate string
352 int ir_strdup (Tcl_Interp *interp, char** p, const char *s)
359 *p = malloc (strlen(s)+1);
362 interp->result = "strdup fail";
370 * ir_strdel: Delete string
372 int ir_strdel (Tcl_Interp *interp, char **p)
380 * ir_malloc: Malloc function
382 void *ir_malloc (Tcl_Interp *interp, size_t size)
384 static char buf[128];
385 void *p = malloc (size);
389 sprintf (buf, "Malloc fail. %ld bytes requested", (long) size);
390 interp->result = buf;
396 static void set_referenceId (ODR o, Z_ReferenceId **dst, const char *src)
402 *dst = odr_malloc (o, sizeof(**dst));
403 (*dst)->size = (*dst)->len = strlen(src);
404 (*dst)->buf = odr_malloc (o, (*dst)->len);
405 memcpy ((*dst)->buf, src, (*dst)->len);
409 static void get_referenceId (char **dst, Z_ReferenceId *src)
417 *dst = malloc (src->len+1);
418 memcpy (*dst, src->buf, src->len);
419 (*dst)[src->len] = '\0';
422 /* ------------------------------------------------------- */
425 * do_init_request: init method on IR object
427 static int do_init_request (void *obj, Tcl_Interp *interp,
428 int argc, char **argv)
430 Z_APDU apdu, *apdup = &apdu;
439 interp->result = "not connected";
442 odr_reset (p->odr_out);
444 set_referenceId (p->odr_out, &req.referenceId, p->set_inher.referenceId);
445 req.options = &p->options;
446 req.protocolVersion = &p->protocolVersion;
447 req.preferredMessageSize = &p->preferredMessageSize;
448 req.maximumRecordSize = &p->maximumRecordSize;
450 if (p->idAuthenticationGroupId)
452 Z_IdPass *pass = odr_malloc (p->odr_out, sizeof(*pass));
453 Z_IdAuthentication *auth = odr_malloc (p->odr_out, sizeof(*auth));
455 auth->which = Z_IdAuthentication_idPass;
456 auth->u.idPass = pass;
457 if (p->idAuthenticationGroupId && *p->idAuthenticationGroupId)
458 pass->groupId = p->idAuthenticationGroupId;
460 pass->groupId = NULL;
461 if (p->idAuthenticationUserId && *p->idAuthenticationUserId)
462 pass->userId = p->idAuthenticationUserId;
465 if (p->idAuthenticationPassword && *p->idAuthenticationPassword)
466 pass->password = p->idAuthenticationPassword;
468 pass->password = NULL;
469 req.idAuthentication = auth;
471 else if (!p->idAuthenticationOpen || !*p->idAuthenticationOpen)
472 req.idAuthentication = NULL;
475 Z_IdAuthentication *auth = odr_malloc (p->odr_out, sizeof(*auth));
477 auth->which = Z_IdAuthentication_open;
478 auth->u.open = p->idAuthenticationOpen;
479 req.idAuthentication = auth;
481 req.implementationId = p->implementationId;
482 req.implementationName = p->implementationName;
483 req.implementationVersion = p->implementationVersion;
484 req.userInformationField = 0;
486 apdu.u.initRequest = &req;
487 apdu.which = Z_APDU_initRequest;
489 if (!z_APDU (p->odr_out, &apdup, 0))
491 Tcl_AppendResult (interp, odr_errlist [odr_geterror (p->odr_out)],
493 odr_reset (p->odr_out);
496 p->sbuf = odr_getbuf (p->odr_out, &p->slen, NULL);
497 if ((r=cs_put (p->cs_link, p->sbuf, p->slen)) < 0)
499 interp->result = "cs_put failed in init";
500 do_disconnect (p, NULL, 2, NULL);
505 ir_select_add_write (cs_fileno(p->cs_link), p);
506 logf (LOG_DEBUG, "Sent part of initializeRequest (%d bytes)", p->slen);
509 logf (LOG_DEBUG, "Sent whole initializeRequest (%d bytes)", p->slen);
514 * do_protocolVersion: Set protocol Version
516 static int do_protocolVersion (void *obj, Tcl_Interp *interp,
517 int argc, char **argv)
519 static struct ir_named_entry version_tab[] = {
530 ODR_MASK_ZERO (&p->protocolVersion);
531 ODR_MASK_SET (&p->protocolVersion, 0);
532 ODR_MASK_SET (&p->protocolVersion, 1);
535 return ir_named_bits (version_tab, &p->protocolVersion,
536 interp, argc-2, argv+2);
540 * do_options: Set options
542 static int do_options (void *obj, Tcl_Interp *interp,
543 int argc, char **argv)
545 static struct ir_named_entry options_tab[] = {
549 { "resourceReport", 3 },
550 { "triggerResourceCtrl", 4},
551 { "resourceCtrl", 5},
555 { "extentedServices", 10},
556 { "level-1Segmentation", 11},
557 { "level-2Segmentation", 12},
558 { "concurrentOperations", 13},
559 { "namedResultSets", 14},
566 ODR_MASK_ZERO (&p->options);
567 ODR_MASK_SET (&p->options, 0);
568 ODR_MASK_SET (&p->options, 1);
569 ODR_MASK_SET (&p->options, 7);
570 ODR_MASK_SET (&p->options, 14);
573 return ir_named_bits (options_tab, &p->options, interp, argc-2, argv+2);
577 * do_preferredMessageSize: Set/get preferred message size
579 static int do_preferredMessageSize (void *obj, Tcl_Interp *interp,
580 int argc, char **argv)
586 p->preferredMessageSize = 4096;
589 return get_set_int (&p->preferredMessageSize, interp, argc, argv);
593 * do_maximumRecordSize: Set/get maximum record size
595 static int do_maximumRecordSize (void *obj, Tcl_Interp *interp,
596 int argc, char **argv)
602 p->maximumRecordSize = 32768;
605 return get_set_int (&p->maximumRecordSize, interp, argc, argv);
609 * do_initResult: Get init result
611 static int do_initResult (void *obj, Tcl_Interp *interp,
612 int argc, char **argv)
618 return get_set_int (&p->initResult, interp, argc, argv);
623 * do_implementationName: Set/get Implementation Name.
625 static int do_implementationName (void *obj, Tcl_Interp *interp,
626 int argc, char **argv)
631 return ir_strdup (interp, &p->implementationName,
632 "Index Data/TCL/TK on YAZ");
634 return ir_strdel (interp, &p->implementationName);
637 free (p->implementationName);
638 if (ir_strdup (interp, &p->implementationName, argv[2])
642 Tcl_AppendResult (interp, p->implementationName, (char*) NULL);
647 * do_implementationId: Get Implementation Id.
649 static int do_implementationId (void *obj, Tcl_Interp *interp,
650 int argc, char **argv)
655 return ir_strdup (interp, &p->implementationId, "YAZ (id=81)");
657 return ir_strdel (interp, &p->implementationId);
658 Tcl_AppendResult (interp, p->implementationId, (char*) NULL);
663 * do_implementationVersion: get Implementation Version.
665 static int do_implementationVersion (void *obj, Tcl_Interp *interp,
666 int argc, char **argv)
671 #define YAZ_VERSION "0unknown"
674 return ir_strdup (interp, &p->implementationVersion, YAZ_VERSION);
676 return ir_strdel (interp, &p->implementationVersion);
677 Tcl_AppendResult (interp, p->implementationVersion, (char*) NULL);
682 * do_targetImplementationName: Get Implementation Name of target.
684 static int do_targetImplementationName (void *obj, Tcl_Interp *interp,
685 int argc, char **argv)
691 p->targetImplementationName = NULL;
695 return ir_strdel (interp, &p->targetImplementationName);
696 Tcl_AppendResult (interp, p->targetImplementationName, (char*) NULL);
701 * do_targetImplementationId: Get Implementation Id of target
703 static int do_targetImplementationId (void *obj, Tcl_Interp *interp,
704 int argc, char **argv)
710 p->targetImplementationId = NULL;
714 return ir_strdel (interp, &p->targetImplementationId);
715 Tcl_AppendResult (interp, p->targetImplementationId, (char*) NULL);
720 * do_targetImplementationVersion: Get Implementation Version of target
722 static int do_targetImplementationVersion (void *obj, Tcl_Interp *interp,
723 int argc, char **argv)
729 p->targetImplementationVersion = NULL;
733 return ir_strdel (interp, &p->targetImplementationVersion);
734 Tcl_AppendResult (interp, p->targetImplementationVersion, (char*) NULL);
739 * do_idAuthentication: Set/get id Authentication
741 static int do_idAuthentication (void *obj, Tcl_Interp *interp,
742 int argc, char **argv)
746 if (argc >= 3 || argc == -1)
748 free (p->idAuthenticationOpen);
749 free (p->idAuthenticationGroupId);
750 free (p->idAuthenticationUserId);
751 free (p->idAuthenticationPassword);
753 if (argc >= 3 || argc <= 0)
755 p->idAuthenticationOpen = NULL;
756 p->idAuthenticationGroupId = NULL;
757 p->idAuthenticationUserId = NULL;
758 p->idAuthenticationPassword = NULL;
766 if (ir_strdup (interp, &p->idAuthenticationOpen, argv[2])
772 if (ir_strdup (interp, &p->idAuthenticationGroupId, argv[2])
775 if (ir_strdup (interp, &p->idAuthenticationUserId, argv[3])
778 if (ir_strdup (interp, &p->idAuthenticationPassword, argv[4])
783 if (p->idAuthenticationOpen)
784 Tcl_AppendElement (interp, p->idAuthenticationOpen);
785 else if (p->idAuthenticationGroupId)
787 Tcl_AppendElement (interp, p->idAuthenticationGroupId);
788 Tcl_AppendElement (interp, p->idAuthenticationUserId);
789 Tcl_AppendElement (interp, p->idAuthenticationPassword);
795 * do_connect: connect method on IR object
797 static int do_connect (void *obj, Tcl_Interp *interp,
798 int argc, char **argv)
803 int protocol_type = PROTO_Z3950;
811 interp->result = "already connected";
814 if (!strcmp (p->protocol_type, "Z3950"))
815 protocol_type = PROTO_Z3950;
816 else if (!strcmp (p->protocol_type, "SR"))
817 protocol_type = PROTO_SR;
820 interp->result = "bad protocol type";
823 if (!strcmp (p->cs_type, "tcpip"))
825 p->cs_link = cs_create (tcpip_type, CS_BLOCK, protocol_type);
826 addr = tcpip_strtoaddr (argv[2]);
829 interp->result = "tcpip_strtoaddr fail";
832 logf (LOG_DEBUG, "tcp/ip connect %s", argv[2]);
835 else if (!strcmp (p->cs_type, "mosi"))
837 p->cs_link = cs_create (mosi_type, CS_BLOCK, protocol_type);
838 addr = mosi_strtoaddr (argv[2]);
841 interp->result = "mosi_strtoaddr fail";
844 logf (LOG_DEBUG, "mosi connect %s", argv[2]);
849 interp->result = "unknown comstack type";
852 if (ir_strdup (interp, &p->hostname, argv[2]) == TCL_ERROR)
854 if ((r=cs_connect (p->cs_link, addr)) < 0)
856 interp->result = "cs_connect fail";
857 do_disconnect (p, NULL, 2, NULL);
860 ir_select_add (cs_fileno (p->cs_link), p);
863 ir_select_add_write (cs_fileno (p->cs_link), p);
870 Tcl_Eval (p->interp, p->callback);
874 Tcl_AppendElement (interp, p->hostname);
879 * do_disconnect: disconnect method on IR object
881 static int do_disconnect (void *obj, Tcl_Interp *interp,
882 int argc, char **argv)
897 ir_select_remove_write (cs_fileno (p->cs_link), p);
898 ir_select_remove (cs_fileno (p->cs_link), p);
901 cs_close (p->cs_link);
904 assert (!p->cs_link);
909 * do_comstack: Set/get comstack method on IR object
911 static int do_comstack (void *o, Tcl_Interp *interp,
912 int argc, char **argv)
917 return ir_strdup (interp, &obj->cs_type, "tcpip");
919 return ir_strdel (interp, &obj->cs_type);
923 if (ir_strdup (interp, &obj->cs_type, argv[2]) == TCL_ERROR)
926 Tcl_AppendElement (interp, obj->cs_type);
931 * do_protocol: Set/get protocol method on IR object
933 static int do_protocol (void *o, Tcl_Interp *interp,
934 int argc, char **argv)
939 return ir_strdup (interp, &obj->protocol_type, "Z3950");
941 return ir_strdel (interp, &obj->protocol_type);
944 free (obj->protocol_type);
945 if (ir_strdup (interp, &obj->protocol_type, argv[2]) == TCL_ERROR)
948 Tcl_AppendElement (interp, obj->protocol_type);
953 * do_callback: add callback
955 static int do_callback (void *obj, Tcl_Interp *interp,
956 int argc, char **argv)
966 return ir_strdel (interp, &p->callback);
972 if (ir_strdup (interp, &p->callback, argv[2]) == TCL_ERROR)
983 * do_failback: add error handle callback
985 static int do_failback (void *obj, Tcl_Interp *interp,
986 int argc, char **argv)
996 return ir_strdel (interp, &p->failback);
1002 if (ir_strdup (interp, &p->failback, argv[2]) == TCL_ERROR)
1013 * do_databaseNames: specify database names
1015 static int do_databaseNames (void *obj, Tcl_Interp *interp,
1016 int argc, char **argv)
1019 IrTcl_SetCObj *p = obj;
1023 for (i=0; i<p->num_databaseNames; i++)
1024 free (p->databaseNames[i]);
1025 free (p->databaseNames);
1029 p->num_databaseNames = 0;
1030 p->databaseNames = NULL;
1035 for (i=0; i<p->num_databaseNames; i++)
1036 Tcl_AppendElement (interp, p->databaseNames[i]);
1039 if (p->databaseNames)
1041 for (i=0; i<p->num_databaseNames; i++)
1042 free (p->databaseNames[i]);
1043 free (p->databaseNames);
1045 p->num_databaseNames = argc - 2;
1046 if (!(p->databaseNames = ir_malloc (interp,
1047 sizeof(*p->databaseNames) * p->num_databaseNames)))
1049 for (i=0; i<p->num_databaseNames; i++)
1051 if (ir_strdup (interp, &p->databaseNames[i], argv[2+i])
1059 * do_replaceIndicator: Set/get replace Set indicator
1061 static int do_replaceIndicator (void *obj, Tcl_Interp *interp,
1062 int argc, char **argv)
1064 IrTcl_SetCObj *p = obj;
1068 p->replaceIndicator = 1;
1071 return get_set_int (&p->replaceIndicator, interp, argc, argv);
1075 * do_queryType: Set/Get query method
1077 static int do_queryType (void *obj, Tcl_Interp *interp,
1078 int argc, char **argv)
1080 IrTcl_SetCObj *p = obj;
1083 return ir_strdup (interp, &p->queryType, "rpn");
1084 else if (argc == -1)
1085 return ir_strdel (interp, &p->queryType);
1088 free (p->queryType);
1089 if (ir_strdup (interp, &p->queryType, argv[2]) == TCL_ERROR)
1092 Tcl_AppendResult (interp, p->queryType, NULL);
1097 * do_userInformationField: Get User information field
1099 static int do_userInformationField (void *obj, Tcl_Interp *interp,
1100 int argc, char **argv)
1106 p->userInformationField = NULL;
1109 else if (argc == -1)
1110 return ir_strdel (interp, &p->userInformationField);
1111 Tcl_AppendResult (interp, p->userInformationField, NULL);
1116 * do_smallSetUpperBound: Set/get small set upper bound
1118 static int do_smallSetUpperBound (void *o, Tcl_Interp *interp,
1119 int argc, char **argv)
1121 IrTcl_SetCObj *p = o;
1125 p->smallSetUpperBound = 0;
1128 return get_set_int (&p->smallSetUpperBound, interp, argc, argv);
1132 * do_largeSetLowerBound: Set/get large set lower bound
1134 static int do_largeSetLowerBound (void *o, Tcl_Interp *interp,
1135 int argc, char **argv)
1137 IrTcl_SetCObj *p = o;
1141 p->largeSetLowerBound = 2;
1144 return get_set_int (&p->largeSetLowerBound, interp, argc, argv);
1148 * do_mediumSetPresentNumber: Set/get large set lower bound
1150 static int do_mediumSetPresentNumber (void *o, Tcl_Interp *interp,
1151 int argc, char **argv)
1153 IrTcl_SetCObj *p = o;
1157 p->mediumSetPresentNumber = 0;
1160 return get_set_int (&p->mediumSetPresentNumber, interp, argc, argv);
1164 * do_referenceId: Set/Get referenceId
1166 static int do_referenceId (void *obj, Tcl_Interp *interp,
1167 int argc, char **argv)
1169 IrTcl_SetCObj *p = obj;
1172 p->referenceId = NULL;
1173 else if (argc == -1)
1174 return ir_strdel (interp, &p->referenceId);
1177 free (p->referenceId);
1178 if (ir_strdup (interp, &p->referenceId, argv[2]) == TCL_ERROR)
1181 Tcl_AppendResult (interp, p->referenceId, NULL);
1185 static IrTcl_Method ir_method_tab[] = {
1186 { 1, "comstack", do_comstack },
1187 { 1, "protocol", do_protocol },
1188 { 0, "failback", do_failback },
1190 { 1, "connect", do_connect },
1191 { 0, "protocolVersion", do_protocolVersion },
1192 { 1, "preferredMessageSize", do_preferredMessageSize },
1193 { 1, "maximumRecordSize", do_maximumRecordSize },
1194 { 1, "implementationName", do_implementationName },
1195 { 1, "implementationId", do_implementationId },
1196 { 1, "implementationVersion", do_implementationVersion },
1197 { 0, "targetImplementationName", do_targetImplementationName },
1198 { 0, "targetImplementationId", do_targetImplementationId },
1199 { 0, "targetImplementationVersion", do_targetImplementationVersion },
1200 { 0, "userInformationField", do_userInformationField },
1201 { 1, "idAuthentication", do_idAuthentication },
1202 { 0, "options", do_options },
1203 { 0, "init", do_init_request },
1204 { 0, "initResult", do_initResult },
1205 { 0, "disconnect", do_disconnect },
1206 { 0, "callback", do_callback },
1210 static IrTcl_Method ir_set_c_method_tab[] = {
1211 { 0, "databaseNames", do_databaseNames},
1212 { 0, "replaceIndicator", do_replaceIndicator},
1213 { 0, "queryType", do_queryType },
1214 { 0, "smallSetUpperBound", do_smallSetUpperBound},
1215 { 0, "largeSetLowerBound", do_largeSetLowerBound},
1216 { 0, "mediumSetPresentNumber", do_mediumSetPresentNumber},
1217 { 0, "referenceId", do_referenceId },
1222 * ir_obj_method: IR Object methods
1224 static int ir_obj_method (ClientData clientData, Tcl_Interp *interp,
1225 int argc, char **argv)
1227 IrTcl_Methods tab[3];
1228 IrTcl_Obj *p = clientData;
1231 return ir_method_r (clientData, interp, argc, argv, ir_method_tab);
1233 tab[0].tab = ir_method_tab;
1235 tab[1].tab = ir_set_c_method_tab;
1236 tab[1].obj = &p->set_inher;
1239 return ir_method (interp, argc, argv, tab);
1243 * ir_obj_delete: IR Object disposal
1245 static void ir_obj_delete (ClientData clientData)
1247 IrTcl_Obj *obj = clientData;
1248 IrTcl_Methods tab[3];
1251 if (obj->ref_count > 0)
1253 assert (obj->ref_count == 0);
1255 tab[0].tab = ir_method_tab;
1257 tab[1].tab = ir_set_c_method_tab;
1258 tab[1].obj = &obj->set_inher;
1261 ir_method (NULL, -1, NULL, tab);
1262 odr_destroy (obj->odr_in);
1263 odr_destroy (obj->odr_out);
1264 odr_destroy (obj->odr_pr);
1265 free (obj->buf_out);
1271 * ir_obj_mk: IR Object creation
1273 static int ir_obj_mk (ClientData clientData, Tcl_Interp *interp,
1274 int argc, char **argv)
1276 IrTcl_Methods tab[3];
1284 interp->result = "wrong # args";
1287 if (!(obj = ir_malloc (interp, sizeof(*obj))))
1292 obj->bibset = ccl_qual_mk ();
1293 if ((inf = fopen ("default.bib", "r")))
1295 ccl_qual_file (obj->bibset, inf);
1300 obj->odr_in = odr_createmem (ODR_DECODE);
1301 obj->odr_out = odr_createmem (ODR_ENCODE);
1302 obj->odr_pr = odr_createmem (ODR_PRINT);
1304 obj->len_out = 10000;
1305 if (!(obj->buf_out = ir_malloc (interp, obj->len_out)))
1307 odr_setbuf (obj->odr_out, obj->buf_out, obj->len_out, 0);
1312 tab[0].tab = ir_method_tab;
1314 tab[1].tab = ir_set_c_method_tab;
1315 tab[1].obj = &obj->set_inher;
1318 if (ir_method (interp, 0, NULL, tab) == TCL_ERROR)
1320 Tcl_CreateCommand (interp, argv[1], ir_obj_method,
1321 (ClientData) obj, ir_obj_delete);
1325 /* ------------------------------------------------------- */
1327 * do_search: Do search request
1329 static int do_search (void *o, Tcl_Interp *interp,
1330 int argc, char **argv)
1332 Z_SearchRequest req;
1334 Z_APDU apdu, *apdup = &apdu;
1336 IrTcl_SetObj *obj = o;
1337 IrTcl_Obj *p = obj->parent;
1347 interp->result = "wrong # args";
1350 if (!obj->set_inher.num_databaseNames)
1352 interp->result = "no databaseNames";
1357 interp->result = "not connected";
1360 odr_reset (p->odr_out);
1361 apdu.which = Z_APDU_searchRequest;
1362 apdu.u.searchRequest = &req;
1364 bib1.proto = PROTO_Z3950;
1365 bib1.class = CLASS_ATTSET;
1366 bib1.value = VAL_BIB1;
1368 set_referenceId (p->odr_out, &req.referenceId, obj->set_inher.referenceId);
1370 req.smallSetUpperBound = &obj->set_inher.smallSetUpperBound;
1371 req.largeSetLowerBound = &obj->set_inher.largeSetLowerBound;
1372 req.mediumSetPresentNumber = &obj->set_inher.mediumSetPresentNumber;
1373 req.replaceIndicator = &obj->set_inher.replaceIndicator;
1374 req.resultSetName = obj->setName ? obj->setName : "Default";
1375 logf (LOG_DEBUG, "Search, resultSetName %s", req.resultSetName);
1376 req.num_databaseNames = obj->set_inher.num_databaseNames;
1377 req.databaseNames = obj->set_inher.databaseNames;
1378 for (r=0; r < obj->set_inher.num_databaseNames; r++)
1379 logf (LOG_DEBUG, " Database %s", obj->set_inher.databaseNames[r]);
1380 req.smallSetElementSetNames = 0;
1381 req.mediumSetElementSetNames = 0;
1382 req.preferredRecordSyntax = 0;
1385 if (!strcmp (obj->set_inher.queryType, "rpn"))
1387 Z_RPNQuery *RPNquery;
1389 RPNquery = p_query_rpn (p->odr_out, argv[2]);
1392 Tcl_AppendResult (interp, "Syntax error in query", NULL);
1395 RPNquery->attributeSetId = oid_getoidbyent (&bib1);
1396 query.which = Z_Query_type_1;
1397 query.u.type_1 = RPNquery;
1398 logf (LOG_DEBUG, "RPN");
1401 else if (!strcmp (obj->set_inher.queryType, "cclrpn"))
1405 struct ccl_rpn_node *rpn;
1406 Z_RPNQuery *RPNquery;
1408 rpn = ccl_find_str(p->bibset, argv[2], &error, &pos);
1411 Tcl_AppendResult (interp, "CCL error: ",
1412 ccl_err_msg(error), NULL);
1415 ccl_pr_tree (rpn, stderr);
1416 fprintf (stderr, "\n");
1417 assert((RPNquery = ccl_rpn_query(rpn)));
1418 RPNquery->attributeSetId = oid_getoidbyent (&bib1);
1419 query.which = Z_Query_type_1;
1420 query.u.type_1 = RPNquery;
1421 logf (LOG_DEBUG, "CCLRPN");
1424 else if (!strcmp (obj->set_inher.queryType, "ccl"))
1426 query.which = Z_Query_type_2;
1427 query.u.type_2 = &ccl_query;
1428 ccl_query.buf = (unsigned char *) argv[2];
1429 ccl_query.len = strlen (argv[2]);
1430 logf (LOG_DEBUG, "CCL");
1434 interp->result = "unknown query method";
1437 if (!z_APDU (p->odr_out, &apdup, 0))
1439 interp->result = odr_errlist [odr_geterror (p->odr_out)];
1440 odr_reset (p->odr_out);
1443 p->sbuf = odr_getbuf (p->odr_out, &p->slen, NULL);
1444 if ((r=cs_put (p->cs_link, p->sbuf, p->slen)) < 0)
1446 interp->result = "cs_put failed in search";
1451 ir_select_add_write (cs_fileno(p->cs_link), p);
1452 logf (LOG_DEBUG, "Sent part of searchRequest (%d bytes)", p->slen);
1456 logf (LOG_DEBUG, "Whole search request (%d bytes)", p->slen);
1462 * do_resultCount: Get number of hits
1464 static int do_resultCount (void *o, Tcl_Interp *interp,
1465 int argc, char **argv)
1467 IrTcl_SetObj *obj = o;
1471 return get_set_int (&obj->resultCount, interp, argc, argv);
1475 * do_searchStatus: Get search status (after search response)
1477 static int do_searchStatus (void *o, Tcl_Interp *interp,
1478 int argc, char **argv)
1480 IrTcl_SetObj *obj = o;
1484 return get_set_int (&obj->searchStatus, interp, argc, argv);
1488 * do_presentStatus: Get search status (after search/present response)
1490 static int do_presentStatus (void *o, Tcl_Interp *interp,
1491 int argc, char **argv)
1493 IrTcl_SetObj *obj = o;
1497 return get_set_int (&obj->presentStatus, interp, argc, argv);
1501 * do_nextResultSetPosition: Get next result set position
1502 * (after search/present response)
1504 static int do_nextResultSetPosition (void *o, Tcl_Interp *interp,
1505 int argc, char **argv)
1507 IrTcl_SetObj *obj = o;
1511 return get_set_int (&obj->nextResultSetPosition, interp, argc, argv);
1515 * do_setName: Set result Set name
1517 static int do_setName (void *o, Tcl_Interp *interp,
1518 int argc, char **argv)
1520 IrTcl_SetObj *obj = o;
1523 return ir_strdup (interp, &obj->setName, "Default");
1524 else if (argc == -1)
1525 return ir_strdel (interp, &obj->setName);
1528 free (obj->setName);
1529 if (ir_strdup (interp, &obj->setName, argv[2])
1533 Tcl_AppendElement (interp, obj->setName);
1538 * do_numberOfRecordsReturned: Get number of records returned
1540 static int do_numberOfRecordsReturned (void *o, Tcl_Interp *interp,
1541 int argc, char **argv)
1543 IrTcl_SetObj *obj = o;
1547 return get_set_int (&obj->numberOfRecordsReturned, interp, argc, argv);
1551 * do_type: Return type (if any) at position.
1553 static int do_type (void *o, Tcl_Interp *interp, int argc, char **argv)
1555 IrTcl_SetObj *obj = o;
1557 IrTcl_RecordList *rl;
1561 obj->record_list = NULL;
1564 else if (argc == -1)
1566 delete_IR_records (obj);
1571 sprintf (interp->result, "wrong # args");
1574 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
1576 rl = find_IR_record (obj, offset);
1581 case Z_NamePlusRecord_databaseRecord:
1582 interp->result = "DB";
1584 case Z_NamePlusRecord_surrogateDiagnostic:
1585 interp->result = "SD";
1592 * do_recordType: Return record type (if any) at position.
1594 static int do_recordType (void *o, Tcl_Interp *interp, int argc, char **argv)
1596 IrTcl_SetObj *obj = o;
1598 IrTcl_RecordList *rl;
1605 else if (argc == -1)
1611 sprintf (interp->result, "wrong # args");
1614 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
1616 rl = find_IR_record (obj, offset);
1619 if (rl->which != Z_NamePlusRecord_databaseRecord)
1621 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
1624 switch (rl->u.dbrec.type)
1644 case VAL_LIBRISMARC:
1677 Tcl_AppendElement (interp, cp);
1682 * do_diag: Return diagnostic record info
1684 static int do_diag (void *o, Tcl_Interp *interp, int argc, char **argv)
1686 IrTcl_SetObj *obj = o;
1688 IrTcl_RecordList *rl;
1696 sprintf (interp->result, "wrong # args");
1699 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
1701 rl = find_IR_record (obj, offset);
1704 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
1707 if (rl->which != Z_NamePlusRecord_surrogateDiagnostic)
1709 Tcl_AppendResult (interp, "No Diagnostic record at #", argv[2], NULL);
1713 sprintf (buf, "%d", rl->u.diag.condition);
1714 Tcl_AppendElement (interp, buf);
1715 cp = diagbib1_str (rl->u.diag.condition);
1717 Tcl_AppendElement (interp, (char*) cp);
1719 Tcl_AppendElement (interp, "");
1720 if (rl->u.diag.addinfo)
1721 Tcl_AppendElement (interp, (char*) rl->u.diag.addinfo);
1723 Tcl_AppendElement (interp, "");
1728 * do_getMarc: Get ISO2709 Record lines/fields
1730 static int do_getMarc (void *o, Tcl_Interp *interp, int argc, char **argv)
1732 IrTcl_SetObj *obj = o;
1734 IrTcl_RecordList *rl;
1740 sprintf (interp->result, "wrong # args");
1743 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
1745 rl = find_IR_record (obj, offset);
1748 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
1751 if (rl->which != Z_NamePlusRecord_databaseRecord)
1753 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
1756 return ir_tcl_get_marc (interp, rl->u.dbrec.buf, argc, argv);
1761 * do_responseStatus: Return response status (present or search)
1763 static int do_responseStatus (void *o, Tcl_Interp *interp,
1764 int argc, char **argv)
1766 IrTcl_SetObj *obj = o;
1770 obj->recordFlag = 0;
1771 obj->addinfo = NULL;
1774 else if (argc == -1)
1775 return ir_strdel (interp, &obj->addinfo);
1776 if (!obj->recordFlag)
1778 Tcl_AppendElement (interp, "OK");
1783 case Z_Records_DBOSD:
1784 Tcl_AppendElement (interp, "DBOSD");
1787 return mk_nonSurrogateDiagnostics (interp, obj->condition,
1794 * do_present: Perform Present Request
1797 static int do_present (void *o, Tcl_Interp *interp,
1798 int argc, char **argv)
1800 IrTcl_SetObj *obj = o;
1801 IrTcl_Obj *p = obj->parent;
1802 Z_APDU apdu, *apdup = &apdu;
1803 Z_PresentRequest req;
1812 if (Tcl_GetInt (interp, argv[2], &start) == TCL_ERROR)
1819 if (Tcl_GetInt (interp, argv[3], &number) == TCL_ERROR)
1826 interp->result = "not connected";
1829 odr_reset (p->odr_out);
1831 obj->number = number;
1833 apdu.which = Z_APDU_presentRequest;
1834 apdu.u.presentRequest = &req;
1836 set_referenceId (p->odr_out, &req.referenceId, obj->set_inher.referenceId);
1838 req.resultSetId = obj->setName ? obj->setName : "Default";
1840 req.resultSetStartPoint = &start;
1841 req.numberOfRecordsRequested = &number;
1842 req.elementSetNames = 0;
1843 req.preferredRecordSyntax = 0;
1845 if (!z_APDU (p->odr_out, &apdup, 0))
1847 interp->result = odr_errlist [odr_geterror (p->odr_out)];
1848 odr_reset (p->odr_out);
1851 p->sbuf = odr_getbuf (p->odr_out, &p->slen, NULL);
1852 if ((r=cs_put (p->cs_link, p->sbuf, p->slen)) < 0)
1854 interp->result = "cs_put failed in present";
1859 ir_select_add_write (cs_fileno(p->cs_link), p);
1860 logf (LOG_DEBUG, "Part of present request, start=%d, num=%d"
1861 " (%d bytes)", start, number, p->slen);
1865 logf (LOG_DEBUG, "Whole present request, start=%d, num=%d"
1866 " (%d bytes)", start, number, p->slen);
1872 * do_loadFile: Load result set from file
1875 static int do_loadFile (void *o, Tcl_Interp *interp,
1876 int argc, char **argv)
1878 IrTcl_SetObj *setobj = o;
1888 interp->result = "wrong # args";
1891 inf = fopen (argv[2], "r");
1894 Tcl_AppendResult (interp, "Cannot open ", argv[2], NULL);
1897 while ((buf = ir_tcl_fread_marc (inf, &size)))
1899 IrTcl_RecordList *rl;
1901 rl = new_IR_record (setobj, no, Z_NamePlusRecord_databaseRecord);
1902 rl->u.dbrec.buf = buf;
1903 rl->u.dbrec.size = size;
1906 setobj->numberOfRecordsReturned = no-1;
1911 static IrTcl_Method ir_set_method_tab[] = {
1912 { 0, "search", do_search },
1913 { 0, "searchStatus", do_searchStatus },
1914 { 0, "presentStatus", do_presentStatus },
1915 { 0, "nextResultSetPosition", do_nextResultSetPosition },
1916 { 0, "setName", do_setName },
1917 { 0, "resultCount", do_resultCount },
1918 { 0, "numberOfRecordsReturned", do_numberOfRecordsReturned },
1919 { 0, "present", do_present },
1920 { 0, "type", do_type },
1921 { 0, "getMarc", do_getMarc },
1922 { 0, "recordType", do_recordType },
1923 { 0, "diag", do_diag },
1924 { 0, "responseStatus", do_responseStatus },
1925 { 0, "loadFile", do_loadFile },
1930 * ir_set_obj_method: IR Set Object methods
1932 static int ir_set_obj_method (ClientData clientData, Tcl_Interp *interp,
1933 int argc, char **argv)
1935 IrTcl_Methods tabs[3];
1936 IrTcl_SetObj *p = clientData;
1940 interp->result = "wrong # args";
1943 tabs[0].tab = ir_set_method_tab;
1945 tabs[1].tab = ir_set_c_method_tab;
1946 tabs[1].obj = &p->set_inher;
1949 return ir_method (interp, argc, argv, tabs);
1953 * ir_set_obj_delete: IR Set Object disposal
1955 static void ir_set_obj_delete (ClientData clientData)
1957 IrTcl_Methods tabs[3];
1958 IrTcl_SetObj *p = clientData;
1960 tabs[0].tab = ir_set_method_tab;
1962 tabs[1].tab = ir_set_c_method_tab;
1963 tabs[1].obj = &p->set_inher;
1966 ir_method (NULL, -1, NULL, tabs);
1972 * ir_set_obj_mk: IR Set Object creation
1974 static int ir_set_obj_mk (ClientData clientData, Tcl_Interp *interp,
1975 int argc, char **argv)
1977 IrTcl_Methods tabs[3];
1980 if (argc < 2 || argc > 3)
1982 interp->result = "wrong # args";
1985 if (!(obj = ir_malloc (interp, sizeof(*obj))))
1989 Tcl_CmdInfo parent_info;
1994 if (!Tcl_GetCommandInfo (interp, argv[2], &parent_info))
1996 interp->result = "No parent";
1999 obj->parent = (IrTcl_Obj *) parent_info.clientData;
2001 dst = &obj->set_inher;
2002 src = &obj->parent->set_inher;
2004 dst->num_databaseNames = src->num_databaseNames;
2005 if (!(dst->databaseNames =
2006 ir_malloc (interp, sizeof (*dst->databaseNames)
2007 * dst->num_databaseNames)))
2009 for (i = 0; i < dst->num_databaseNames; i++)
2011 if (ir_strdup (interp, &dst->databaseNames[i],
2012 src->databaseNames[i]) == TCL_ERROR)
2015 if (ir_strdup (interp, &dst->queryType, src->queryType)
2019 if (ir_strdup (interp, &dst->referenceId, src->referenceId)
2023 dst->replaceIndicator = src->replaceIndicator;
2024 dst->smallSetUpperBound = src->smallSetUpperBound;
2025 dst->largeSetLowerBound = src->largeSetLowerBound;
2026 dst->mediumSetPresentNumber = src->mediumSetPresentNumber;
2031 tabs[0].tab = ir_set_method_tab;
2035 if (ir_method (interp, 0, NULL, tabs) == TCL_ERROR)
2038 Tcl_CreateCommand (interp, argv[1], ir_set_obj_method,
2039 (ClientData) obj, ir_set_obj_delete);
2043 /* ------------------------------------------------------- */
2046 * do_scan: Perform scan
2048 static int do_scan (void *o, Tcl_Interp *interp, int argc, char **argv)
2051 Z_APDU apdu, *apdup = &apdu;
2052 IrTcl_ScanObj *obj = o;
2053 IrTcl_Obj *p = obj->parent;
2057 struct ccl_rpn_node *rpn;
2066 interp->result = "wrong # args";
2069 if (!p->set_inher.num_databaseNames)
2071 interp->result = "no databaseNames";
2076 interp->result = "not connected";
2079 odr_reset (p->odr_out);
2081 bib1.proto = PROTO_Z3950;
2082 bib1.class = CLASS_ATTSET;
2083 bib1.value = VAL_BIB1;
2085 apdu.which = Z_APDU_scanRequest;
2086 apdu.u.scanRequest = &req;
2087 set_referenceId (p->odr_out, &req.referenceId, p->set_inher.referenceId);
2088 req.num_databaseNames = p->set_inher.num_databaseNames;
2089 req.databaseNames = p->set_inher.databaseNames;
2090 req.attributeSet = oid_getoidbyent (&bib1);
2093 if (!(req.termListAndStartPoint = p_query_scan (p->odr_out, argv[2])))
2095 Tcl_AppendResult (interp, "Syntax error in query", NULL);
2099 rpn = ccl_find_str(p->bibset, argv[2], &r, &pos);
2102 Tcl_AppendResult (interp, "CCL error: ", ccl_err_msg (r), NULL);
2105 ccl_pr_tree (rpn, stderr);
2106 fprintf (stderr, "\n");
2107 if (!(req.termListAndStartPoint = ccl_scan_query (rpn)))
2110 req.stepSize = &obj->stepSize;
2111 req.numberOfTermsRequested = &obj->numberOfTermsRequested;
2112 req.preferredPositionInResponse = &obj->preferredPositionInResponse;
2113 logf (LOG_DEBUG, "stepSize=%d", *req.stepSize);
2114 logf (LOG_DEBUG, "numberOfTermsRequested=%d",
2115 *req.numberOfTermsRequested);
2116 logf (LOG_DEBUG, "preferredPositionInResponse=%d",
2117 *req.preferredPositionInResponse);
2119 if (!z_APDU (p->odr_out, &apdup, 0))
2121 interp->result = odr_errlist [odr_geterror (p->odr_out)];
2122 odr_reset (p->odr_out);
2125 p->sbuf = odr_getbuf (p->odr_out, &p->slen, NULL);
2126 if ((r=cs_put (p->cs_link, p->sbuf, p->slen)) < 0)
2128 interp->result = "cs_put failed in scan";
2133 ir_select_add_write (cs_fileno(p->cs_link), p);
2134 logf (LOG_DEBUG, "Sent part of scanRequest (%d bytes)", p->slen);
2138 logf (LOG_DEBUG, "Whole scan request (%d bytes)", p->slen);
2144 * do_stepSize: Set/get replace Step Size
2146 static int do_stepSize (void *obj, Tcl_Interp *interp,
2147 int argc, char **argv)
2149 IrTcl_ScanObj *p = obj;
2155 return get_set_int (&p->stepSize, interp, argc, argv);
2159 * do_numberOfTermsRequested: Set/get Number of Terms requested
2161 static int do_numberOfTermsRequested (void *obj, Tcl_Interp *interp,
2162 int argc, char **argv)
2164 IrTcl_ScanObj *p = obj;
2168 p->numberOfTermsRequested = 20;
2171 return get_set_int (&p->numberOfTermsRequested, interp, argc, argv);
2176 * do_preferredPositionInResponse: Set/get preferred Position
2178 static int do_preferredPositionInResponse (void *obj, Tcl_Interp *interp,
2179 int argc, char **argv)
2181 IrTcl_ScanObj *p = obj;
2185 p->preferredPositionInResponse = 1;
2188 return get_set_int (&p->preferredPositionInResponse, interp, argc, argv);
2192 * do_scanStatus: Get scan status
2194 static int do_scanStatus (void *obj, Tcl_Interp *interp,
2195 int argc, char **argv)
2197 IrTcl_ScanObj *p = obj;
2201 return get_set_int (&p->scanStatus, interp, argc, argv);
2205 * do_numberOfEntriesReturned: Get number of Entries returned
2207 static int do_numberOfEntriesReturned (void *obj, Tcl_Interp *interp,
2208 int argc, char **argv)
2210 IrTcl_ScanObj *p = obj;
2214 return get_set_int (&p->numberOfEntriesReturned, interp, argc, argv);
2218 * do_positionOfTerm: Get position of Term
2220 static int do_positionOfTerm (void *obj, Tcl_Interp *interp,
2221 int argc, char **argv)
2223 IrTcl_ScanObj *p = obj;
2227 return get_set_int (&p->positionOfTerm, interp, argc, argv);
2231 * do_scanLine: get Scan Line (surrogate or normal) after response
2233 static int do_scanLine (void *obj, Tcl_Interp *interp, int argc, char **argv)
2235 IrTcl_ScanObj *p = obj;
2241 p->entries_flag = 0;
2243 p->nonSurrogateDiagnostics = NULL;
2246 else if (argc == -1)
2248 p->entries_flag = 0;
2249 /* release entries */
2251 /* release non diagnostics */
2252 p->nonSurrogateDiagnostics = NULL;
2257 interp->result = "wrong # args";
2260 if (Tcl_GetInt (interp, argv[2], &i) == TCL_ERROR)
2262 if (!p->entries_flag || p->which != Z_ListEntries_entries || !p->entries
2263 || i >= p->num_entries || i < 0)
2265 switch (p->entries[i].which)
2267 case Z_Entry_termInfo:
2268 Tcl_AppendElement (interp, "T");
2269 if (p->entries[i].u.term.buf)
2270 Tcl_AppendElement (interp, p->entries[i].u.term.buf);
2272 Tcl_AppendElement (interp, "");
2273 sprintf (numstr, "%d", p->entries[i].u.term.globalOccurrences);
2274 Tcl_AppendElement (interp, numstr);
2276 case Z_Entry_surrogateDiagnostic:
2278 mk_nonSurrogateDiagnostics (interp, p->entries[i].u.diag.condition,
2279 p->entries[i].u.diag.addinfo);
2285 static IrTcl_Method ir_scan_method_tab[] = {
2286 { 0, "scan", do_scan },
2287 { 0, "stepSize", do_stepSize },
2288 { 0, "numberOfTermsRequested", do_numberOfTermsRequested },
2289 { 0, "preferredPositionInResponse", do_preferredPositionInResponse },
2290 { 0, "scanStatus", do_scanStatus },
2291 { 0, "numberOfEntriesReturned", do_numberOfEntriesReturned },
2292 { 0, "positionOfTerm", do_positionOfTerm },
2293 { 0, "scanLine", do_scanLine },
2298 * ir_scan_obj_method: IR Scan Object methods
2300 static int ir_scan_obj_method (ClientData clientData, Tcl_Interp *interp,
2301 int argc, char **argv)
2303 IrTcl_Methods tabs[2];
2307 interp->result = "wrong # args";
2310 tabs[0].tab = ir_scan_method_tab;
2311 tabs[0].obj = clientData;
2314 return ir_method (interp, argc, argv, tabs);
2318 * ir_scan_obj_delete: IR Scan Object disposal
2320 static void ir_scan_obj_delete (ClientData clientData)
2322 IrTcl_Methods tabs[2];
2323 IrTcl_ScanObj *obj = clientData;
2325 tabs[0].tab = ir_scan_method_tab;
2329 ir_method (NULL, -1, NULL, tabs);
2334 * ir_scan_obj_mk: IR Scan Object creation
2336 static int ir_scan_obj_mk (ClientData clientData, Tcl_Interp *interp,
2337 int argc, char **argv)
2339 Tcl_CmdInfo parent_info;
2341 IrTcl_Methods tabs[2];
2345 interp->result = "wrong # args";
2348 if (!Tcl_GetCommandInfo (interp, argv[2], &parent_info))
2350 interp->result = "No parent";
2353 if (!(obj = ir_malloc (interp, sizeof(*obj))))
2356 obj->parent = (IrTcl_Obj *) parent_info.clientData;
2358 tabs[0].tab = ir_scan_method_tab;
2362 if (ir_method (interp, 0, NULL, tabs) == TCL_ERROR)
2364 Tcl_CreateCommand (interp, argv[1], ir_scan_obj_method,
2365 (ClientData) obj, ir_scan_obj_delete);
2369 /* ------------------------------------------------------- */
2371 static void ir_initResponse (void *obj, Z_InitResponse *initrs)
2375 p->initResult = *initrs->result ? 1 : 0;
2376 if (!*initrs->result)
2377 logf (LOG_DEBUG, "Connection rejected by target");
2379 logf (LOG_DEBUG, "Connection accepted by target");
2381 get_referenceId (&p->set_inher.referenceId, initrs->referenceId);
2383 free (p->targetImplementationId);
2384 ir_strdup (p->interp, &p->targetImplementationId,
2385 initrs->implementationId);
2386 free (p->targetImplementationName);
2387 ir_strdup (p->interp, &p->targetImplementationName,
2388 initrs->implementationName);
2389 free (p->targetImplementationVersion);
2390 ir_strdup (p->interp, &p->targetImplementationVersion,
2391 initrs->implementationVersion);
2393 p->maximumRecordSize = *initrs->maximumRecordSize;
2394 p->preferredMessageSize = *initrs->preferredMessageSize;
2396 memcpy (&p->options, initrs->options, sizeof(initrs->options));
2397 memcpy (&p->protocolVersion, initrs->protocolVersion,
2398 sizeof(initrs->protocolVersion));
2399 free (p->userInformationField);
2400 p->userInformationField = NULL;
2401 if (initrs->userInformationField)
2405 if (initrs->userInformationField->which == ODR_EXTERNAL_octet &&
2406 (p->userInformationField =
2408 initrs->userInformationField->u.octet_aligned->len)
2411 memcpy (p->userInformationField,
2412 initrs->userInformationField->u.octet_aligned->buf,
2414 (p->userInformationField)[len] = '\0';
2419 static void ir_handleRecords (void *o, Z_Records *zrs)
2422 IrTcl_SetObj *setobj = p->set_child;
2424 setobj->which = zrs->which;
2425 setobj->recordFlag = 1;
2426 if (zrs->which == Z_Records_NSD)
2428 const char *addinfo;
2430 setobj->numberOfRecordsReturned = 0;
2431 setobj->condition = *zrs->u.nonSurrogateDiagnostic->condition;
2432 free (setobj->addinfo);
2433 setobj->addinfo = NULL;
2434 addinfo = zrs->u.nonSurrogateDiagnostic->addinfo;
2435 if (addinfo && (setobj->addinfo = malloc (strlen(addinfo) + 1)))
2436 strcpy (setobj->addinfo, addinfo);
2437 logf (LOG_DEBUG, "Diagnostic response. %s (%d): %s",
2438 diagbib1_str (setobj->condition),
2440 setobj->addinfo ? setobj->addinfo : "");
2445 IrTcl_RecordList *rl;
2447 setobj->numberOfRecordsReturned =
2448 zrs->u.databaseOrSurDiagnostics->num_records;
2449 logf (LOG_DEBUG, "Got %d records", setobj->numberOfRecordsReturned);
2450 for (offset = 0; offset<setobj->numberOfRecordsReturned; offset++)
2452 rl = new_IR_record (setobj, setobj->start + offset,
2453 zrs->u.databaseOrSurDiagnostics->
2454 records[offset]->which);
2455 if (rl->which == Z_NamePlusRecord_surrogateDiagnostic)
2459 diagrec = zrs->u.databaseOrSurDiagnostics->
2460 records[offset]->u.surrogateDiagnostic;
2462 rl->u.diag.condition = *diagrec->condition;
2463 if (diagrec->addinfo && (rl->u.diag.addinfo =
2464 malloc (strlen (diagrec->addinfo)+1)))
2465 strcpy (rl->u.diag.addinfo, diagrec->addinfo);
2469 Z_DatabaseRecord *zr;
2472 zr = zrs->u.databaseOrSurDiagnostics->records[offset]
2474 oe = (Odr_external*) zr;
2475 rl->u.dbrec.size = zr->u.octet_aligned->len;
2476 rl->u.dbrec.type = VAL_USMARC;
2477 if (oe->which == ODR_EXTERNAL_octet && rl->u.dbrec.size > 0)
2479 const char *buf = (char*) zr->u.octet_aligned->buf;
2480 if ((rl->u.dbrec.buf = malloc (rl->u.dbrec.size)))
2481 memcpy (rl->u.dbrec.buf, buf, rl->u.dbrec.size);
2482 if (oe->direct_reference)
2484 struct oident *ident =
2485 oid_getentbyoid (oe->direct_reference);
2486 rl->u.dbrec.type = ident->value;
2490 rl->u.dbrec.buf = NULL;
2496 static void ir_searchResponse (void *o, Z_SearchResponse *searchrs)
2499 IrTcl_SetObj *setobj = p->set_child;
2500 Z_Records *zrs = searchrs->records;
2502 logf (LOG_DEBUG, "Received search response");
2505 logf (LOG_DEBUG, "Search response, no object!");
2508 setobj->searchStatus = searchrs->searchStatus ? 1 : 0;
2509 get_referenceId (&setobj->set_inher.referenceId, searchrs->referenceId);
2510 setobj->resultCount = *searchrs->resultCount;
2511 if (searchrs->presentStatus)
2512 setobj->presentStatus = *searchrs->presentStatus;
2513 if (searchrs->nextResultSetPosition)
2514 setobj->nextResultSetPosition = *searchrs->nextResultSetPosition;
2516 logf (LOG_DEBUG, "Search response %d, %d hits",
2517 setobj->searchStatus, setobj->resultCount);
2519 ir_handleRecords (o, zrs);
2521 setobj->recordFlag = 0;
2525 static void ir_presentResponse (void *o, Z_PresentResponse *presrs)
2528 IrTcl_SetObj *setobj = p->set_child;
2529 Z_Records *zrs = presrs->records;
2531 logf (LOG_DEBUG, "Received present response");
2534 logf (LOG_DEBUG, "Present response, no object!");
2537 setobj->presentStatus = *presrs->presentStatus;
2538 get_referenceId (&setobj->set_inher.referenceId, presrs->referenceId);
2539 setobj->nextResultSetPosition = *presrs->nextResultSetPosition;
2541 ir_handleRecords (o, zrs);
2544 setobj->recordFlag = 0;
2545 logf (LOG_DEBUG, "No records!");
2549 static void ir_scanResponse (void *o, Z_ScanResponse *scanrs)
2552 IrTcl_ScanObj *scanobj = p->scan_child;
2554 logf (LOG_DEBUG, "Received scanResponse");
2556 get_referenceId (&p->set_inher.referenceId, scanrs->referenceId);
2557 scanobj->scanStatus = *scanrs->scanStatus;
2558 logf (LOG_DEBUG, "scanStatus=%d", scanobj->scanStatus);
2560 if (scanrs->stepSize)
2561 scanobj->stepSize = *scanrs->stepSize;
2562 logf (LOG_DEBUG, "stepSize=%d", scanobj->stepSize);
2564 scanobj->numberOfEntriesReturned = *scanrs->numberOfEntriesReturned;
2565 logf (LOG_DEBUG, "numberOfEntriesReturned=%d",
2566 scanobj->numberOfEntriesReturned);
2568 if (scanrs->positionOfTerm)
2569 scanobj->positionOfTerm = *scanrs->positionOfTerm;
2571 scanobj->positionOfTerm = -1;
2572 logf (LOG_DEBUG, "positionOfTerm=%d", scanobj->positionOfTerm);
2574 free (scanobj->entries);
2575 scanobj->entries = NULL;
2576 free (scanobj->nonSurrogateDiagnostics);
2577 scanobj->nonSurrogateDiagnostics = NULL;
2579 if (scanrs->entries)
2584 scanobj->entries_flag = 1;
2585 scanobj->which = scanrs->entries->which;
2586 switch (scanobj->which)
2588 case Z_ListEntries_entries:
2589 scanobj->num_entries = scanrs->entries->u.entries->num_entries;
2590 scanobj->entries = malloc (scanobj->num_entries *
2591 sizeof(*scanobj->entries));
2592 for (i=0; i<scanobj->num_entries; i++)
2594 ze = scanrs->entries->u.entries->entries[i];
2595 scanobj->entries[i].which = ze->which;
2598 case Z_Entry_termInfo:
2599 if (ze->u.termInfo->term->which == Z_Term_general)
2601 int l = ze->u.termInfo->term->u.general->len;
2602 scanobj->entries[i].u.term.buf = malloc (1+l);
2603 memcpy (scanobj->entries[i].u.term.buf,
2604 ze->u.termInfo->term->u.general->buf,
2606 scanobj->entries[i].u.term.buf[l] = '\0';
2609 scanobj->entries[i].u.term.buf = NULL;
2610 if (ze->u.termInfo->globalOccurrences)
2611 scanobj->entries[i].u.term.globalOccurrences =
2612 *ze->u.termInfo->globalOccurrences;
2614 scanobj->entries[i].u.term.globalOccurrences = 0;
2616 case Z_Entry_surrogateDiagnostic:
2617 scanobj->entries[i].u.diag.addinfo =
2618 malloc (1+strlen(ze->u.surrogateDiagnostic->
2620 strcpy (scanobj->entries[i].u.diag.addinfo,
2621 ze->u.surrogateDiagnostic->addinfo);
2622 scanobj->entries[i].u.diag.condition =
2623 *ze->u.surrogateDiagnostic->condition;
2628 case Z_ListEntries_nonSurrogateDiagnostics:
2629 scanobj->num_diagRecs = scanrs->entries->
2630 u.nonSurrogateDiagnostics->num_diagRecs;
2631 scanobj->nonSurrogateDiagnostics = malloc (scanobj->num_diagRecs *
2632 sizeof(*scanobj->nonSurrogateDiagnostics));
2637 scanobj->entries_flag = 0;
2641 * ir_select_read: handle incoming packages
2643 void ir_select_read (ClientData clientData)
2645 IrTcl_Obj *p = clientData;
2651 r = cs_rcvconnect (p->cs_link);
2655 ir_select_remove_write (cs_fileno (p->cs_link), p);
2658 logf (LOG_DEBUG, "cs_rcvconnect error");
2660 Tcl_Eval (p->interp, p->failback);
2661 do_disconnect (p, NULL, 2, NULL);
2665 Tcl_Eval (p->interp, p->callback);
2670 /* signal one more use of ir object - callbacks must not
2671 release the ir memory (p pointer) */
2673 if ((r=cs_get (p->cs_link, &p->buf_in, &p->len_in)) <= 0)
2675 logf (LOG_DEBUG, "cs_get failed, code %d", r);
2676 ir_select_remove (cs_fileno (p->cs_link), p);
2678 Tcl_Eval (p->interp, p->failback);
2679 do_disconnect (p, NULL, 2, NULL);
2681 /* relase ir object now if callback deleted it */
2687 odr_setbuf (p->odr_in, p->buf_in, r, 0);
2688 logf (LOG_DEBUG, "cs_get ok, got %d", r);
2689 if (!z_APDU (p->odr_in, &apdu, 0))
2691 logf (LOG_DEBUG, "%s", odr_errlist [odr_geterror (p->odr_in)]);
2693 Tcl_Eval (p->interp, p->failback);
2694 do_disconnect (p, NULL, 2, NULL);
2696 /* release ir object now if callback deleted it */
2702 case Z_APDU_initResponse:
2703 ir_initResponse (p, apdu->u.initResponse);
2705 case Z_APDU_searchResponse:
2706 ir_searchResponse (p, apdu->u.searchResponse);
2708 case Z_APDU_presentResponse:
2709 ir_presentResponse (p, apdu->u.presentResponse);
2711 case Z_APDU_scanResponse:
2712 ir_scanResponse (p, apdu->u.scanResponse);
2715 logf (LOG_WARN, "Received unknown APDU type (%d)", apdu->which);
2717 Tcl_Eval (p->interp, p->failback);
2718 do_disconnect (p, NULL, 2, NULL);
2720 odr_reset (p->odr_in);
2722 Tcl_Eval (p->interp, p->callback);
2723 if (p->ref_count == 1)
2729 } while (p->cs_link && cs_more (p->cs_link));
2733 * ir_select_write: handle outgoing packages - not yet written.
2735 void ir_select_write (ClientData clientData)
2737 IrTcl_Obj *p = clientData;
2740 logf (LOG_DEBUG, "In write handler");
2743 r = cs_rcvconnect (p->cs_link);
2749 logf (LOG_DEBUG, "cs_rcvconnect error");
2750 ir_select_remove_write (cs_fileno (p->cs_link), p);
2752 Tcl_Eval (p->interp, p->failback);
2753 do_disconnect (p, NULL, 2, NULL);
2756 ir_select_remove_write (cs_fileno (p->cs_link), p);
2758 Tcl_Eval (p->interp, p->callback);
2761 if ((r=cs_put (p->cs_link, p->sbuf, p->slen)) < 0)
2763 logf (LOG_DEBUG, "select write fail");
2765 Tcl_Eval (p->interp, p->failback);
2766 do_disconnect (p, NULL, 2, NULL);
2768 else if (r == 0) /* remove select bit */
2770 ir_select_remove_write (cs_fileno (p->cs_link), p);
2774 /* ------------------------------------------------------- */
2777 * ir_tcl_init: Registration of TCL commands.
2779 int ir_tcl_init (Tcl_Interp *interp)
2781 Tcl_CreateCommand (interp, "ir", ir_obj_mk, (ClientData) NULL,
2782 (Tcl_CmdDeleteProc *) NULL);
2783 Tcl_CreateCommand (interp, "ir-set", ir_set_obj_mk,
2784 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
2785 Tcl_CreateCommand (interp, "ir-scan", ir_scan_obj_mk,
2786 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);