2 * IR toolkit for tcl/tk
4 * See the file LICENSE for details.
5 * Sebastian Hammer, Adam Dickmeiss
8 * Revision 1.38 1995-06-01 16:36:47 adam
9 * About buttons. Minor bug fixes.
11 * Revision 1.37 1995/06/01 07:31:20 adam
12 * Rename of many typedefs -> IrTcl_...
14 * Revision 1.36 1995/05/31 13:09:59 adam
15 * Client searches/presents may be interrupted.
16 * New moving book-logo.
18 * Revision 1.35 1995/05/31 08:36:33 adam
19 * Bug fix in client.tcl: didn't save options on clientrc.tcl.
20 * New method: referenceId. More work on scan.
22 * Revision 1.34 1995/05/29 10:33:42 adam
23 * README and rename of startup script.
25 * Revision 1.33 1995/05/29 09:15:11 quinn
26 * Changed CS_SR to PROTO_SR, etc.
28 * Revision 1.32 1995/05/29 08:44:16 adam
29 * Work on delete of objects.
31 * Revision 1.31 1995/05/26 11:44:10 adam
32 * Bugs fixed. More work on MARC utilities and queries. Test
33 * client is up-to-date again.
35 * Revision 1.30 1995/05/26 08:54:11 adam
36 * New MARC utilities. Uses prefix query.
38 * Revision 1.29 1995/05/24 14:10:22 adam
39 * Work on idAuthentication, protocolVersion and options.
41 * Revision 1.28 1995/05/23 15:34:48 adam
42 * Many new settings, userInformationField, smallSetUpperBound, etc.
43 * A number of settings are inherited when ir-set is executed.
44 * This version is incompatible with the graphical test client (client.tcl).
46 * Revision 1.27 1995/05/11 15:34:47 adam
47 * Scan request changed a bit. This version works with RLG.
49 * Revision 1.26 1995/04/18 16:11:51 adam
50 * First version of graphical Scan. Some work on query-by-form.
52 * Revision 1.25 1995/04/17 09:37:17 adam
53 * Further development of scan.
55 * Revision 1.24 1995/04/11 14:16:42 adam
56 * Further work on scan. Response works. Entries aren't saved yet.
58 * Revision 1.23 1995/04/10 10:50:27 adam
59 * Result-set name defaults to suffix of ir-set name.
60 * Started working on scan. Not finished at this point.
62 * Revision 1.22 1995/03/31 10:43:03 adam
63 * More robust when getting bad MARC records.
65 * Revision 1.21 1995/03/31 08:56:37 adam
66 * New button "Search".
68 * Revision 1.20 1995/03/29 16:07:09 adam
69 * Bug fix: Didn't use setName in present request.
71 * Revision 1.19 1995/03/28 12:45:23 adam
72 * New ir method failback: called on disconnect/protocol error.
73 * New ir set/get method: protocol: SR / Z3950.
74 * Simple popup and disconnect when failback is invoked.
76 * Revision 1.18 1995/03/21 15:50:12 adam
79 * Revision 1.17 1995/03/21 13:41:03 adam
80 * Comstack cs_create not used too often. Non-blocking connect.
82 * Revision 1.16 1995/03/21 08:26:06 adam
83 * New method, setName, to specify the result set name (other than Default).
84 * New method, responseStatus, which returns diagnostic info, if any, after
85 * present response / search response.
87 * Revision 1.15 1995/03/20 15:24:07 adam
88 * Diagnostic records saved on searchResponse.
90 * Revision 1.14 1995/03/20 08:53:22 adam
91 * Event loop in tclmain.c rewritten. New method searchStatus.
93 * Revision 1.13 1995/03/17 18:26:17 adam
94 * Non-blocking i/o used now. Database names popup as cascade items.
96 * Revision 1.12 1995/03/17 15:45:00 adam
97 * Improved target/database setup.
99 * Revision 1.11 1995/03/16 17:54:03 adam
100 * Minor changes really.
102 * Revision 1.10 1995/03/15 16:14:50 adam
103 * Blocking arg in cs_create changed.
105 * Revision 1.9 1995/03/15 13:59:24 adam
108 * Revision 1.8 1995/03/15 08:25:16 adam
109 * New method presentStatus to check for error on present. Misc. cleanup
110 * of IrTcl_RecordList manipulations. Full MARC record presentation in
113 * Revision 1.7 1995/03/14 17:32:29 adam
114 * Presentation of full Marc record in popup window.
116 * Revision 1.6 1995/03/12 19:31:55 adam
117 * Pattern matching implemented when retrieving MARC records. More
118 * diagnostic functions.
120 * Revision 1.5 1995/03/10 18:00:15 adam
121 * Actual presentation in line-by-line format. RPN query support.
123 * Revision 1.4 1995/03/09 16:15:08 adam
124 * First presentRequest attempts. Hot-target list.
130 #include <sys/time.h>
140 int (*method) (void *obj, Tcl_Interp *interp, int argc, char **argv);
148 static int do_disconnect (void *obj, Tcl_Interp *interp,
149 int argc, char **argv);
151 static IrTcl_RecordList *new_IR_record (IrTcl_SetObj *setobj,
154 IrTcl_RecordList *rl;
156 for (rl = setobj->record_list; rl; rl = rl->next)
162 case Z_NamePlusRecord_databaseRecord:
163 free (rl->u.dbrec.buf);
164 rl->u.dbrec.buf = NULL;
166 case Z_NamePlusRecord_surrogateDiagnostic:
167 free (rl->u.diag.addinfo);
168 rl->u.diag.addinfo = NULL;
176 rl = malloc (sizeof(*rl));
178 rl->next = setobj->record_list;
180 setobj->record_list = rl;
186 static IrTcl_RecordList *find_IR_record (IrTcl_SetObj *setobj, int no)
188 IrTcl_RecordList *rl;
190 for (rl = setobj->record_list; rl; rl = rl->next)
196 static void delete_IR_records (IrTcl_SetObj *setobj)
198 IrTcl_RecordList *rl, *rl1;
200 for (rl = setobj->record_list; rl; rl = rl1)
204 case Z_NamePlusRecord_databaseRecord:
205 free (rl->u.dbrec.buf);
207 case Z_NamePlusRecord_surrogateDiagnostic:
208 free (rl->u.diag.addinfo);
214 setobj->record_list = NULL;
218 * getsetint: Set/get integer value
220 static int get_set_int (int *val, Tcl_Interp *interp, int argc, char **argv)
226 if (Tcl_GetInt (interp, argv[2], val)==TCL_ERROR)
229 sprintf (buf, "%d", *val);
230 Tcl_AppendResult (interp, buf, NULL);
235 * mk_nonSurrogateDiagnostics: Make Tcl result with diagnostic info
237 static int mk_nonSurrogateDiagnostics (Tcl_Interp *interp,
238 int condition, const char *addinfo)
243 Tcl_AppendElement (interp, "NSD");
244 sprintf (buf, "%d", condition);
245 Tcl_AppendElement (interp, buf);
246 cp = diagbib1_str (condition);
248 Tcl_AppendElement (interp, (char*) cp);
250 Tcl_AppendElement (interp, "");
252 Tcl_AppendElement (interp, (char*) addinfo);
254 Tcl_AppendElement (interp, "");
259 * ir_method: Search for method in table and invoke method handler
261 int ir_method (Tcl_Interp *interp, int argc, char **argv, IrTcl_Methods *tab)
263 IrTcl_Methods *tab_i = tab;
266 for (tab_i = tab; tab_i->tab; tab_i++)
267 for (t = tab_i->tab; t->name; t++)
270 if ((*t->method)(tab_i->obj, interp, argc, argv) == TCL_ERROR)
274 if (!strcmp (t->name, argv[1]))
275 return (*t->method)(tab_i->obj, interp, argc, argv);
279 Tcl_AppendResult (interp, "Bad method. Possible methods:", NULL);
280 for (tab_i = tab; tab_i->tab; tab_i++)
281 for (t = tab_i->tab; t->name; t++)
282 Tcl_AppendResult (interp, " ", t->name, NULL);
287 * ir_method_r: Get status for all readable elements
289 int ir_method_r (void *obj, Tcl_Interp *interp, int argc, char **argv,
297 for (; tab->name; tab++)
300 argv_n[1] = tab->name;
301 Tcl_AppendResult (interp, "{", NULL);
302 (*tab->method)(obj, interp, argc_n, argv_n);
303 Tcl_AppendResult (interp, "} ", NULL);
309 * ir_named_bits: get/set named bits
311 int ir_named_bits (struct ir_named_entry *tab, Odr_bitmask *ob,
312 Tcl_Interp *interp, int argc, char **argv)
314 struct ir_named_entry *ti;
319 for (no = 0; no < argc; no++)
321 for (ti = tab; ti->name; ti++)
322 if (!strcmp (argv[no], ti->name))
324 ODR_MASK_SET (ob, ti->pos);
329 Tcl_AppendResult (interp, "Bad bit mask: ", argv[no], NULL);
335 for (ti = tab; ti->name; ti++)
336 if (ODR_MASK_GET (ob, ti->pos))
337 Tcl_AppendElement (interp, ti->name);
342 * ir_strdup: Duplicate string
344 int ir_strdup (Tcl_Interp *interp, char** p, const char *s)
346 *p = malloc (strlen(s)+1);
349 interp->result = "strdup fail";
357 * ir_strdel: Delete string
359 int ir_strdel (Tcl_Interp *interp, char **p)
367 * ir_malloc: Malloc function
369 void *ir_malloc (Tcl_Interp *interp, size_t size)
371 static char buf[128];
372 void *p = malloc (size);
376 sprintf (buf, "Malloc fail. %ld bytes requested", (long) size);
377 interp->result = buf;
383 static void set_referenceId (ODR o, Z_ReferenceId **dst, const char *src)
389 *dst = odr_malloc (o, sizeof(**dst));
390 (*dst)->size = (*dst)->len = strlen(src);
391 (*dst)->buf = odr_malloc (o, (*dst)->len);
392 memcpy ((*dst)->buf, src, (*dst)->len);
396 static void get_referenceId (char **dst, Z_ReferenceId *src)
404 *dst = malloc (src->len+1);
405 memcpy (*dst, src->buf, src->len);
406 (*dst)[src->len] = '\0';
409 /* ------------------------------------------------------- */
412 * do_init_request: init method on IR object
414 static int do_init_request (void *obj, Tcl_Interp *interp,
415 int argc, char **argv)
417 Z_APDU apdu, *apdup = &apdu;
426 interp->result = "not connected";
429 odr_reset (p->odr_out);
431 set_referenceId (p->odr_out, &req.referenceId, p->set_inher.referenceId);
432 req.options = &p->options;
433 req.protocolVersion = &p->protocolVersion;
434 req.preferredMessageSize = &p->preferredMessageSize;
435 req.maximumRecordSize = &p->maximumRecordSize;
437 if (p->idAuthenticationGroupId)
439 Z_IdPass *pass = odr_malloc (p->odr_out, sizeof(*pass));
440 Z_IdAuthentication *auth = odr_malloc (p->odr_out, sizeof(*auth));
442 auth->which = Z_IdAuthentication_idPass;
443 auth->u.idPass = pass;
444 if (p->idAuthenticationGroupId && *p->idAuthenticationGroupId)
445 pass->groupId = p->idAuthenticationGroupId;
447 pass->groupId = NULL;
448 if (p->idAuthenticationUserId && *p->idAuthenticationUserId)
449 pass->userId = p->idAuthenticationUserId;
452 if (p->idAuthenticationPassword && *p->idAuthenticationPassword)
453 pass->password = p->idAuthenticationPassword;
455 pass->password = NULL;
456 req.idAuthentication = auth;
458 else if (!p->idAuthenticationOpen || !*p->idAuthenticationOpen)
459 req.idAuthentication = NULL;
462 Z_IdAuthentication *auth = odr_malloc (p->odr_out, sizeof(*auth));
464 auth->which = Z_IdAuthentication_open;
465 auth->u.open = p->idAuthenticationOpen;
466 req.idAuthentication = auth;
468 req.implementationId = p->implementationId;
469 req.implementationName = p->implementationName;
470 req.implementationVersion = "0.1";
471 req.userInformationField = 0;
473 apdu.u.initRequest = &req;
474 apdu.which = Z_APDU_initRequest;
476 if (!z_APDU (p->odr_out, &apdup, 0))
478 Tcl_AppendResult (interp, odr_errlist [odr_geterror (p->odr_out)],
480 odr_reset (p->odr_out);
483 p->sbuf = odr_getbuf (p->odr_out, &p->slen, NULL);
484 if ((r=cs_put (p->cs_link, p->sbuf, p->slen)) < 0)
486 interp->result = "cs_put failed in init";
487 do_disconnect (p, NULL, 2, NULL);
492 ir_select_add_write (cs_fileno(p->cs_link), p);
493 logf (LOG_DEBUG, "Sent part of initializeRequest (%d bytes)", p->slen);
496 logf (LOG_DEBUG, "Sent whole initializeRequest (%d bytes)", p->slen);
501 * do_protocolVersion: Set protocol Version
503 static int do_protocolVersion (void *obj, Tcl_Interp *interp,
504 int argc, char **argv)
506 static struct ir_named_entry version_tab[] = {
517 ODR_MASK_ZERO (&p->protocolVersion);
518 ODR_MASK_SET (&p->protocolVersion, 0);
519 ODR_MASK_SET (&p->protocolVersion, 1);
522 return ir_named_bits (version_tab, &p->protocolVersion,
523 interp, argc-2, argv+2);
527 * do_options: Set options
529 static int do_options (void *obj, Tcl_Interp *interp,
530 int argc, char **argv)
532 static struct ir_named_entry options_tab[] = {
536 { "resourceReport", 3 },
537 { "triggerResourceCtrl", 4},
538 { "resourceCtrl", 5},
542 { "extentedServices", 10},
543 { "level-1Segmentation", 11},
544 { "level-2Segmentation", 12},
545 { "concurrentOperations", 13},
546 { "namedResultSets", 14},
553 ODR_MASK_ZERO (&p->options);
554 ODR_MASK_SET (&p->options, 0);
555 ODR_MASK_SET (&p->options, 1);
556 ODR_MASK_SET (&p->options, 7);
557 ODR_MASK_SET (&p->options, 14);
560 return ir_named_bits (options_tab, &p->options, interp, argc-2, argv+2);
564 * do_preferredMessageSize: Set/get preferred message size
566 static int do_preferredMessageSize (void *obj, Tcl_Interp *interp,
567 int argc, char **argv)
573 p->preferredMessageSize = 4096;
576 return get_set_int (&p->preferredMessageSize, interp, argc, argv);
580 * do_maximumRecordSize: Set/get maximum record size
582 static int do_maximumRecordSize (void *obj, Tcl_Interp *interp,
583 int argc, char **argv)
589 p->maximumRecordSize = 32768;
592 return get_set_int (&p->maximumRecordSize, interp, argc, argv);
596 * do_initResult: Get init result
598 static int do_initResult (void *obj, Tcl_Interp *interp,
599 int argc, char **argv)
605 return get_set_int (&p->initResult, interp, argc, argv);
610 * do_implementationName: Set/get Implementation Name.
612 static int do_implementationName (void *obj, Tcl_Interp *interp,
613 int argc, char **argv)
618 return ir_strdup (interp, &p->implementationName, "TCL/TK on YAZ");
620 return ir_strdel (interp, &p->implementationName);
623 free (p->implementationName);
624 if (ir_strdup (interp, &p->implementationName, argv[2])
628 Tcl_AppendResult (interp, p->implementationName, (char*) NULL);
633 * do_implementationId: Set/get Implementation Id.
635 static int do_implementationId (void *obj, Tcl_Interp *interp,
636 int argc, char **argv)
641 return ir_strdup (interp, &p->implementationId, "81");
643 return ir_strdel (interp, &p->implementationId);
646 free (p->implementationId);
647 if (ir_strdup (interp, &p->implementationId, argv[2]) == TCL_ERROR)
650 Tcl_AppendResult (interp, p->implementationId, (char*) NULL);
655 * do_targetImplementationName: Get Implementation Name of target.
657 static int do_targetImplementationName (void *obj, Tcl_Interp *interp,
658 int argc, char **argv)
664 p->targetImplementationName = NULL;
668 return ir_strdel (interp, &p->targetImplementationName);
669 Tcl_AppendResult (interp, p->targetImplementationName, (char*) NULL);
674 * do_targetImplementationId: Get Implementation Id of target
676 static int do_targetImplementationId (void *obj, Tcl_Interp *interp,
677 int argc, char **argv)
683 p->targetImplementationId = NULL;
687 return ir_strdel (interp, &p->targetImplementationId);
688 Tcl_AppendResult (interp, p->targetImplementationId, (char*) NULL);
693 * do_targetImplementationVersion: Get Implementation Version of target
695 static int do_targetImplementationVersion (void *obj, Tcl_Interp *interp,
696 int argc, char **argv)
702 p->targetImplementationVersion = NULL;
706 return ir_strdel (interp, &p->targetImplementationVersion);
707 Tcl_AppendResult (interp, p->targetImplementationVersion, (char*) NULL);
712 * do_idAuthentication: Set/get id Authentication
714 static int do_idAuthentication (void *obj, Tcl_Interp *interp,
715 int argc, char **argv)
719 if (argc >= 3 || argc == -1)
721 free (p->idAuthenticationOpen);
722 free (p->idAuthenticationGroupId);
723 free (p->idAuthenticationUserId);
724 free (p->idAuthenticationPassword);
726 if (argc >= 3 || argc <= 0)
728 p->idAuthenticationOpen = NULL;
729 p->idAuthenticationGroupId = NULL;
730 p->idAuthenticationUserId = NULL;
731 p->idAuthenticationPassword = NULL;
739 if (ir_strdup (interp, &p->idAuthenticationOpen, argv[2])
745 if (ir_strdup (interp, &p->idAuthenticationGroupId, argv[2])
748 if (ir_strdup (interp, &p->idAuthenticationUserId, argv[3])
751 if (ir_strdup (interp, &p->idAuthenticationPassword, argv[4])
756 if (p->idAuthenticationOpen)
757 Tcl_AppendElement (interp, p->idAuthenticationOpen);
758 else if (p->idAuthenticationGroupId)
760 Tcl_AppendElement (interp, p->idAuthenticationGroupId);
761 Tcl_AppendElement (interp, p->idAuthenticationUserId);
762 Tcl_AppendElement (interp, p->idAuthenticationPassword);
768 * do_connect: connect method on IR object
770 static int do_connect (void *obj, Tcl_Interp *interp,
771 int argc, char **argv)
776 int protocol_type = PROTO_Z3950;
784 interp->result = "already connected";
787 if (!strcmp (p->protocol_type, "Z3950"))
788 protocol_type = PROTO_Z3950;
789 else if (!strcmp (p->protocol_type, "SR"))
790 protocol_type = PROTO_SR;
793 interp->result = "bad protocol type";
796 if (!strcmp (p->cs_type, "tcpip"))
798 p->cs_link = cs_create (tcpip_type, CS_BLOCK, protocol_type);
799 addr = tcpip_strtoaddr (argv[2]);
802 interp->result = "tcpip_strtoaddr fail";
805 logf (LOG_DEBUG, "tcp/ip connect %s", argv[2]);
808 else if (!strcmp (p->cs_type, "mosi"))
810 p->cs_link = cs_create (mosi_type, CS_BLOCK, protocol_type);
811 addr = mosi_strtoaddr (argv[2]);
814 interp->result = "mosi_strtoaddr fail";
817 logf (LOG_DEBUG, "mosi connect %s", argv[2]);
822 interp->result = "unknown comstack type";
825 if (ir_strdup (interp, &p->hostname, argv[2]) == TCL_ERROR)
827 if ((r=cs_connect (p->cs_link, addr)) < 0)
829 interp->result = "cs_connect fail";
830 do_disconnect (p, NULL, 2, NULL);
833 ir_select_add (cs_fileno (p->cs_link), p);
836 ir_select_add_write (cs_fileno (p->cs_link), p);
843 Tcl_Eval (p->interp, p->callback);
847 Tcl_AppendElement (interp, p->hostname);
852 * do_disconnect: disconnect method on IR object
854 static int do_disconnect (void *obj, Tcl_Interp *interp,
855 int argc, char **argv)
870 ir_select_remove_write (cs_fileno (p->cs_link), p);
871 ir_select_remove (cs_fileno (p->cs_link), p);
874 cs_close (p->cs_link);
877 assert (!p->cs_link);
882 * do_comstack: Set/get comstack method on IR object
884 static int do_comstack (void *o, Tcl_Interp *interp,
885 int argc, char **argv)
890 return ir_strdup (interp, &obj->cs_type, "tcpip");
892 return ir_strdel (interp, &obj->cs_type);
896 if (ir_strdup (interp, &obj->cs_type, argv[2]) == TCL_ERROR)
899 Tcl_AppendElement (interp, obj->cs_type);
904 * do_protocol: Set/get protocol method on IR object
906 static int do_protocol (void *o, Tcl_Interp *interp,
907 int argc, char **argv)
912 return ir_strdup (interp, &obj->protocol_type, "Z3950");
914 return ir_strdel (interp, &obj->protocol_type);
917 free (obj->protocol_type);
918 if (ir_strdup (interp, &obj->protocol_type, argv[2]) == TCL_ERROR)
921 Tcl_AppendElement (interp, obj->protocol_type);
926 * do_callback: add callback
928 static int do_callback (void *obj, Tcl_Interp *interp,
929 int argc, char **argv)
939 return ir_strdel (interp, &p->callback);
945 if (ir_strdup (interp, &p->callback, argv[2]) == TCL_ERROR)
956 * do_failback: add error handle callback
958 static int do_failback (void *obj, Tcl_Interp *interp,
959 int argc, char **argv)
969 return ir_strdel (interp, &p->failback);
975 if (ir_strdup (interp, &p->failback, argv[2]) == TCL_ERROR)
986 * do_databaseNames: specify database names
988 static int do_databaseNames (void *obj, Tcl_Interp *interp,
989 int argc, char **argv)
992 IrTcl_SetCObj *p = obj;
996 for (i=0; i<p->num_databaseNames; i++)
997 free (p->databaseNames[i]);
998 free (p->databaseNames);
1002 p->num_databaseNames = 0;
1003 p->databaseNames = NULL;
1008 for (i=0; i<p->num_databaseNames; i++)
1009 Tcl_AppendElement (interp, p->databaseNames[i]);
1012 if (p->databaseNames)
1014 for (i=0; i<p->num_databaseNames; i++)
1015 free (p->databaseNames[i]);
1016 free (p->databaseNames);
1018 p->num_databaseNames = argc - 2;
1019 if (!(p->databaseNames = ir_malloc (interp,
1020 sizeof(*p->databaseNames) * p->num_databaseNames)))
1022 for (i=0; i<p->num_databaseNames; i++)
1024 if (ir_strdup (interp, &p->databaseNames[i], argv[2+i])
1032 * do_replaceIndicator: Set/get replace Set indicator
1034 static int do_replaceIndicator (void *obj, Tcl_Interp *interp,
1035 int argc, char **argv)
1037 IrTcl_SetCObj *p = obj;
1041 p->replaceIndicator = 1;
1044 return get_set_int (&p->replaceIndicator, interp, argc, argv);
1048 * do_queryType: Set/Get query method
1050 static int do_queryType (void *obj, Tcl_Interp *interp,
1051 int argc, char **argv)
1053 IrTcl_SetCObj *p = obj;
1056 return ir_strdup (interp, &p->queryType, "rpn");
1057 else if (argc == -1)
1058 return ir_strdel (interp, &p->queryType);
1061 free (p->queryType);
1062 if (ir_strdup (interp, &p->queryType, argv[2]) == TCL_ERROR)
1065 Tcl_AppendResult (interp, p->queryType, NULL);
1070 * do_userInformationField: Get User information field
1072 static int do_userInformationField (void *obj, Tcl_Interp *interp,
1073 int argc, char **argv)
1079 p->userInformationField = NULL;
1082 else if (argc == -1)
1083 return ir_strdel (interp, &p->userInformationField);
1084 Tcl_AppendResult (interp, p->userInformationField, NULL);
1089 * do_smallSetUpperBound: Set/get small set upper bound
1091 static int do_smallSetUpperBound (void *o, Tcl_Interp *interp,
1092 int argc, char **argv)
1094 IrTcl_SetCObj *p = o;
1098 p->smallSetUpperBound = 0;
1101 return get_set_int (&p->smallSetUpperBound, interp, argc, argv);
1105 * do_largeSetLowerBound: Set/get large set lower bound
1107 static int do_largeSetLowerBound (void *o, Tcl_Interp *interp,
1108 int argc, char **argv)
1110 IrTcl_SetCObj *p = o;
1114 p->largeSetLowerBound = 2;
1117 return get_set_int (&p->largeSetLowerBound, interp, argc, argv);
1121 * do_mediumSetPresentNumber: Set/get large set lower bound
1123 static int do_mediumSetPresentNumber (void *o, Tcl_Interp *interp,
1124 int argc, char **argv)
1126 IrTcl_SetCObj *p = o;
1130 p->mediumSetPresentNumber = 0;
1133 return get_set_int (&p->mediumSetPresentNumber, interp, argc, argv);
1137 * do_referenceId: Set/Get referenceId
1139 static int do_referenceId (void *obj, Tcl_Interp *interp,
1140 int argc, char **argv)
1142 IrTcl_SetCObj *p = obj;
1145 p->referenceId = NULL;
1146 else if (argc == -1)
1147 return ir_strdel (interp, &p->referenceId);
1150 free (p->referenceId);
1151 if (ir_strdup (interp, &p->referenceId, argv[2]) == TCL_ERROR)
1154 Tcl_AppendResult (interp, p->referenceId, NULL);
1158 static IrTcl_Method ir_method_tab[] = {
1159 { 1, "comstack", do_comstack },
1160 { 1, "protocol", do_protocol },
1161 { 0, "failback", do_failback },
1163 { 1, "connect", do_connect },
1164 { 0, "protocolVersion", do_protocolVersion },
1165 { 1, "preferredMessageSize", do_preferredMessageSize },
1166 { 1, "maximumRecordSize", do_maximumRecordSize },
1167 { 1, "implementationName", do_implementationName },
1168 { 1, "implementationId", do_implementationId },
1169 { 0, "targetImplementationName", do_targetImplementationName },
1170 { 0, "targetImplementationId", do_targetImplementationId },
1171 { 0, "targetImplementationVersion", do_targetImplementationVersion },
1172 { 0, "userInformationField", do_userInformationField },
1173 { 1, "idAuthentication", do_idAuthentication },
1174 { 0, "options", do_options },
1175 { 0, "init", do_init_request },
1176 { 0, "initResult", do_initResult },
1177 { 0, "disconnect", do_disconnect },
1178 { 0, "callback", do_callback },
1182 static IrTcl_Method ir_set_c_method_tab[] = {
1183 { 0, "databaseNames", do_databaseNames},
1184 { 0, "replaceIndicator", do_replaceIndicator},
1185 { 0, "queryType", do_queryType },
1186 { 0, "smallSetUpperBound", do_smallSetUpperBound},
1187 { 0, "largeSetLowerBound", do_largeSetLowerBound},
1188 { 0, "mediumSetPresentNumber", do_mediumSetPresentNumber},
1189 { 0, "referenceId", do_referenceId },
1194 * ir_obj_method: IR Object methods
1196 static int ir_obj_method (ClientData clientData, Tcl_Interp *interp,
1197 int argc, char **argv)
1199 IrTcl_Methods tab[3];
1200 IrTcl_Obj *p = clientData;
1203 return ir_method_r (clientData, interp, argc, argv, ir_method_tab);
1205 tab[0].tab = ir_method_tab;
1207 tab[1].tab = ir_set_c_method_tab;
1208 tab[1].obj = &p->set_inher;
1211 return ir_method (interp, argc, argv, tab);
1215 * ir_obj_delete: IR Object disposal
1217 static void ir_obj_delete (ClientData clientData)
1219 IrTcl_Obj *obj = clientData;
1220 IrTcl_Methods tab[3];
1223 if (obj->ref_count > 0)
1225 assert (obj->ref_count == 0);
1227 tab[0].tab = ir_method_tab;
1229 tab[1].tab = ir_set_c_method_tab;
1230 tab[1].obj = &obj->set_inher;
1233 ir_method (NULL, -1, NULL, tab);
1234 odr_destroy (obj->odr_in);
1235 odr_destroy (obj->odr_out);
1236 odr_destroy (obj->odr_pr);
1237 free (obj->buf_out);
1243 * ir_obj_mk: IR Object creation
1245 static int ir_obj_mk (ClientData clientData, Tcl_Interp *interp,
1246 int argc, char **argv)
1248 IrTcl_Methods tab[3];
1256 interp->result = "wrong # args";
1259 if (!(obj = ir_malloc (interp, sizeof(*obj))))
1264 obj->bibset = ccl_qual_mk ();
1265 if ((inf = fopen ("default.bib", "r")))
1267 ccl_qual_file (obj->bibset, inf);
1272 obj->odr_in = odr_createmem (ODR_DECODE);
1273 obj->odr_out = odr_createmem (ODR_ENCODE);
1274 obj->odr_pr = odr_createmem (ODR_PRINT);
1276 obj->len_out = 10000;
1277 if (!(obj->buf_out = ir_malloc (interp, obj->len_out)))
1279 odr_setbuf (obj->odr_out, obj->buf_out, obj->len_out, 0);
1284 tab[0].tab = ir_method_tab;
1286 tab[1].tab = ir_set_c_method_tab;
1287 tab[1].obj = &obj->set_inher;
1290 if (ir_method (interp, 0, NULL, tab) == TCL_ERROR)
1292 Tcl_CreateCommand (interp, argv[1], ir_obj_method,
1293 (ClientData) obj, ir_obj_delete);
1297 /* ------------------------------------------------------- */
1299 * do_search: Do search request
1301 static int do_search (void *o, Tcl_Interp *interp,
1302 int argc, char **argv)
1304 Z_SearchRequest req;
1306 Z_APDU apdu, *apdup = &apdu;
1308 IrTcl_SetObj *obj = o;
1309 IrTcl_Obj *p = obj->parent;
1319 interp->result = "wrong # args";
1322 if (!obj->set_inher.num_databaseNames)
1324 interp->result = "no databaseNames";
1329 interp->result = "not connected";
1332 odr_reset (p->odr_out);
1333 apdu.which = Z_APDU_searchRequest;
1334 apdu.u.searchRequest = &req;
1336 bib1.proto = PROTO_Z3950;
1337 bib1.class = CLASS_ATTSET;
1338 bib1.value = VAL_BIB1;
1340 set_referenceId (p->odr_out, &req.referenceId, obj->set_inher.referenceId);
1342 req.smallSetUpperBound = &obj->set_inher.smallSetUpperBound;
1343 req.largeSetLowerBound = &obj->set_inher.largeSetLowerBound;
1344 req.mediumSetPresentNumber = &obj->set_inher.mediumSetPresentNumber;
1345 req.replaceIndicator = &obj->set_inher.replaceIndicator;
1346 req.resultSetName = obj->setName ? obj->setName : "Default";
1347 logf (LOG_DEBUG, "Search, resultSetName %s", req.resultSetName);
1348 req.num_databaseNames = obj->set_inher.num_databaseNames;
1349 req.databaseNames = obj->set_inher.databaseNames;
1350 for (r=0; r < obj->set_inher.num_databaseNames; r++)
1351 logf (LOG_DEBUG, " Database %s", obj->set_inher.databaseNames[r]);
1352 req.smallSetElementSetNames = 0;
1353 req.mediumSetElementSetNames = 0;
1354 req.preferredRecordSyntax = 0;
1357 if (!strcmp (obj->set_inher.queryType, "rpn"))
1359 Z_RPNQuery *RPNquery;
1361 RPNquery = p_query_rpn (p->odr_out, argv[2]);
1364 Tcl_AppendResult (interp, "Syntax error in query", NULL);
1367 RPNquery->attributeSetId = oid_getoidbyent (&bib1);
1368 query.which = Z_Query_type_1;
1369 query.u.type_1 = RPNquery;
1370 logf (LOG_DEBUG, "RPN");
1373 else if (!strcmp (obj->set_inher.queryType, "cclrpn"))
1377 struct ccl_rpn_node *rpn;
1378 Z_RPNQuery *RPNquery;
1380 rpn = ccl_find_str(p->bibset, argv[2], &error, &pos);
1383 Tcl_AppendResult (interp, "CCL error: ",
1384 ccl_err_msg(error), NULL);
1387 ccl_pr_tree (rpn, stderr);
1388 fprintf (stderr, "\n");
1389 assert((RPNquery = ccl_rpn_query(rpn)));
1390 RPNquery->attributeSetId = oid_getoidbyent (&bib1);
1391 query.which = Z_Query_type_1;
1392 query.u.type_1 = RPNquery;
1393 logf (LOG_DEBUG, "CCLRPN");
1396 else if (!strcmp (obj->set_inher.queryType, "ccl"))
1398 query.which = Z_Query_type_2;
1399 query.u.type_2 = &ccl_query;
1400 ccl_query.buf = (unsigned char *) argv[2];
1401 ccl_query.len = strlen (argv[2]);
1402 logf (LOG_DEBUG, "CCL");
1406 interp->result = "unknown query method";
1409 if (!z_APDU (p->odr_out, &apdup, 0))
1411 interp->result = odr_errlist [odr_geterror (p->odr_out)];
1412 odr_reset (p->odr_out);
1415 p->sbuf = odr_getbuf (p->odr_out, &p->slen, NULL);
1416 if ((r=cs_put (p->cs_link, p->sbuf, p->slen)) < 0)
1418 interp->result = "cs_put failed in search";
1423 ir_select_add_write (cs_fileno(p->cs_link), p);
1424 logf (LOG_DEBUG, "Sent part of searchRequest (%d bytes)", p->slen);
1428 logf (LOG_DEBUG, "Whole search request (%d bytes)", p->slen);
1434 * do_resultCount: Get number of hits
1436 static int do_resultCount (void *o, Tcl_Interp *interp,
1437 int argc, char **argv)
1439 IrTcl_SetObj *obj = o;
1443 return get_set_int (&obj->resultCount, interp, argc, argv);
1447 * do_searchStatus: Get search status (after search response)
1449 static int do_searchStatus (void *o, Tcl_Interp *interp,
1450 int argc, char **argv)
1452 IrTcl_SetObj *obj = o;
1456 return get_set_int (&obj->searchStatus, interp, argc, argv);
1460 * do_presentStatus: Get search status (after search/present response)
1462 static int do_presentStatus (void *o, Tcl_Interp *interp,
1463 int argc, char **argv)
1465 IrTcl_SetObj *obj = o;
1469 return get_set_int (&obj->presentStatus, interp, argc, argv);
1473 * do_nextResultSetPosition: Get next result set position
1474 * (after search/present response)
1476 static int do_nextResultSetPosition (void *o, Tcl_Interp *interp,
1477 int argc, char **argv)
1479 IrTcl_SetObj *obj = o;
1483 return get_set_int (&obj->nextResultSetPosition, interp, argc, argv);
1487 * do_setName: Set result Set name
1489 static int do_setName (void *o, Tcl_Interp *interp,
1490 int argc, char **argv)
1492 IrTcl_SetObj *obj = o;
1495 return ir_strdup (interp, &obj->setName, "Default");
1496 else if (argc == -1)
1497 return ir_strdel (interp, &obj->setName);
1500 free (obj->setName);
1501 if (ir_strdup (interp, &obj->setName, argv[2])
1505 Tcl_AppendElement (interp, obj->setName);
1510 * do_numberOfRecordsReturned: Get number of records returned
1512 static int do_numberOfRecordsReturned (void *o, Tcl_Interp *interp,
1513 int argc, char **argv)
1515 IrTcl_SetObj *obj = o;
1519 return get_set_int (&obj->numberOfRecordsReturned, interp, argc, argv);
1523 * do_type: Return type (if any) at position.
1525 static int do_type (void *o, Tcl_Interp *interp, int argc, char **argv)
1527 IrTcl_SetObj *obj = o;
1529 IrTcl_RecordList *rl;
1533 obj->record_list = NULL;
1536 else if (argc == -1)
1538 delete_IR_records (obj);
1543 sprintf (interp->result, "wrong # args");
1546 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
1548 rl = find_IR_record (obj, offset);
1553 case Z_NamePlusRecord_databaseRecord:
1554 interp->result = "DB";
1556 case Z_NamePlusRecord_surrogateDiagnostic:
1557 interp->result = "SD";
1564 * do_diag: Return diagnostic record info
1566 static int do_diag (void *o, Tcl_Interp *interp, int argc, char **argv)
1568 IrTcl_SetObj *obj = o;
1570 IrTcl_RecordList *rl;
1578 sprintf (interp->result, "wrong # args");
1581 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
1583 rl = find_IR_record (obj, offset);
1586 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
1589 if (rl->which != Z_NamePlusRecord_surrogateDiagnostic)
1591 Tcl_AppendResult (interp, "No Diagnostic record at #", argv[2], NULL);
1595 sprintf (buf, "%d", rl->u.diag.condition);
1596 Tcl_AppendElement (interp, buf);
1597 cp = diagbib1_str (rl->u.diag.condition);
1599 Tcl_AppendElement (interp, (char*) cp);
1601 Tcl_AppendElement (interp, "");
1602 if (rl->u.diag.addinfo)
1603 Tcl_AppendElement (interp, (char*) rl->u.diag.addinfo);
1605 Tcl_AppendElement (interp, "");
1610 * do_getMarc: Get ISO2709 Record lines/fields
1612 static int do_getMarc (void *o, Tcl_Interp *interp, int argc, char **argv)
1614 IrTcl_SetObj *obj = o;
1616 IrTcl_RecordList *rl;
1622 sprintf (interp->result, "wrong # args");
1625 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
1627 rl = find_IR_record (obj, offset);
1630 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
1633 if (rl->which != Z_NamePlusRecord_databaseRecord)
1635 Tcl_AppendResult (interp, "No MARC record at #", argv[2], NULL);
1638 return ir_tcl_get_marc (interp, rl->u.dbrec.buf, argc, argv);
1643 * do_responseStatus: Return response status (present or search)
1645 static int do_responseStatus (void *o, Tcl_Interp *interp,
1646 int argc, char **argv)
1648 IrTcl_SetObj *obj = o;
1652 obj->recordFlag = 0;
1653 obj->addinfo = NULL;
1656 else if (argc == -1)
1657 return ir_strdel (interp, &obj->addinfo);
1658 if (!obj->recordFlag)
1660 Tcl_AppendElement (interp, "OK");
1665 case Z_Records_DBOSD:
1666 Tcl_AppendElement (interp, "DBOSD");
1669 return mk_nonSurrogateDiagnostics (interp, obj->condition,
1676 * do_present: Perform Present Request
1679 static int do_present (void *o, Tcl_Interp *interp,
1680 int argc, char **argv)
1682 IrTcl_SetObj *obj = o;
1683 IrTcl_Obj *p = obj->parent;
1684 Z_APDU apdu, *apdup = &apdu;
1685 Z_PresentRequest req;
1694 if (Tcl_GetInt (interp, argv[2], &start) == TCL_ERROR)
1701 if (Tcl_GetInt (interp, argv[3], &number) == TCL_ERROR)
1708 interp->result = "not connected";
1711 odr_reset (p->odr_out);
1713 obj->number = number;
1715 apdu.which = Z_APDU_presentRequest;
1716 apdu.u.presentRequest = &req;
1718 set_referenceId (p->odr_out, &req.referenceId, obj->set_inher.referenceId);
1720 req.resultSetId = obj->setName ? obj->setName : "Default";
1722 req.resultSetStartPoint = &start;
1723 req.numberOfRecordsRequested = &number;
1724 req.elementSetNames = 0;
1725 req.preferredRecordSyntax = 0;
1727 if (!z_APDU (p->odr_out, &apdup, 0))
1729 interp->result = odr_errlist [odr_geterror (p->odr_out)];
1730 odr_reset (p->odr_out);
1733 p->sbuf = odr_getbuf (p->odr_out, &p->slen, NULL);
1734 if ((r=cs_put (p->cs_link, p->sbuf, p->slen)) < 0)
1736 interp->result = "cs_put failed in present";
1741 ir_select_add_write (cs_fileno(p->cs_link), p);
1742 logf (LOG_DEBUG, "Part of present request, start=%d, num=%d"
1743 " (%d bytes)", start, number, p->slen);
1747 logf (LOG_DEBUG, "Whole present request, start=%d, num=%d"
1748 " (%d bytes)", start, number, p->slen);
1754 * do_loadFile: Load result set from file
1757 static int do_loadFile (void *o, Tcl_Interp *interp,
1758 int argc, char **argv)
1760 IrTcl_SetObj *setobj = o;
1770 interp->result = "wrong # args";
1773 inf = fopen (argv[2], "r");
1776 Tcl_AppendResult (interp, "Cannot open ", argv[2], NULL);
1779 while ((buf = ir_tcl_fread_marc (inf, &size)))
1781 IrTcl_RecordList *rl;
1783 rl = new_IR_record (setobj, no, Z_NamePlusRecord_databaseRecord);
1784 rl->u.dbrec.buf = buf;
1785 rl->u.dbrec.size = size;
1788 setobj->numberOfRecordsReturned = no-1;
1793 static IrTcl_Method ir_set_method_tab[] = {
1794 { 0, "search", do_search },
1795 { 0, "searchStatus", do_searchStatus },
1796 { 0, "presentStatus", do_presentStatus },
1797 { 0, "nextResultSetPosition", do_nextResultSetPosition },
1798 { 0, "setName", do_setName },
1799 { 0, "resultCount", do_resultCount },
1800 { 0, "numberOfRecordsReturned", do_numberOfRecordsReturned },
1801 { 0, "present", do_present },
1802 { 0, "type", do_type },
1803 { 0, "getMarc", do_getMarc },
1804 { 0, "diag", do_diag },
1805 { 0, "responseStatus", do_responseStatus },
1806 { 0, "loadFile", do_loadFile },
1811 * ir_set_obj_method: IR Set Object methods
1813 static int ir_set_obj_method (ClientData clientData, Tcl_Interp *interp,
1814 int argc, char **argv)
1816 IrTcl_Methods tabs[3];
1817 IrTcl_SetObj *p = clientData;
1821 interp->result = "wrong # args";
1824 tabs[0].tab = ir_set_method_tab;
1826 tabs[1].tab = ir_set_c_method_tab;
1827 tabs[1].obj = &p->set_inher;
1830 return ir_method (interp, argc, argv, tabs);
1834 * ir_set_obj_delete: IR Set Object disposal
1836 static void ir_set_obj_delete (ClientData clientData)
1838 IrTcl_Methods tabs[3];
1839 IrTcl_SetObj *p = clientData;
1841 tabs[0].tab = ir_set_method_tab;
1843 tabs[1].tab = ir_set_c_method_tab;
1844 tabs[1].obj = &p->set_inher;
1847 ir_method (NULL, -1, NULL, tabs);
1853 * ir_set_obj_mk: IR Set Object creation
1855 static int ir_set_obj_mk (ClientData clientData, Tcl_Interp *interp,
1856 int argc, char **argv)
1858 IrTcl_Methods tabs[3];
1861 if (argc < 2 || argc > 3)
1863 interp->result = "wrong # args";
1866 if (!(obj = ir_malloc (interp, sizeof(*obj))))
1870 Tcl_CmdInfo parent_info;
1875 if (!Tcl_GetCommandInfo (interp, argv[2], &parent_info))
1877 interp->result = "No parent";
1880 obj->parent = (IrTcl_Obj *) parent_info.clientData;
1882 dst = &obj->set_inher;
1883 src = &obj->parent->set_inher;
1885 dst->num_databaseNames = src->num_databaseNames;
1886 if (!(dst->databaseNames =
1887 ir_malloc (interp, sizeof (*dst->databaseNames)
1888 * dst->num_databaseNames)))
1890 for (i = 0; i < dst->num_databaseNames; i++)
1892 if (ir_strdup (interp, &dst->databaseNames[i],
1893 src->databaseNames[i]) == TCL_ERROR)
1896 if (ir_strdup (interp, &dst->queryType, src->queryType)
1900 if (ir_strdup (interp, &dst->referenceId, src->referenceId)
1904 dst->replaceIndicator = src->replaceIndicator;
1905 dst->smallSetUpperBound = src->smallSetUpperBound;
1906 dst->largeSetLowerBound = src->largeSetLowerBound;
1907 dst->mediumSetPresentNumber = src->mediumSetPresentNumber;
1912 tabs[0].tab = ir_set_method_tab;
1916 if (ir_method (interp, 0, NULL, tabs) == TCL_ERROR)
1919 Tcl_CreateCommand (interp, argv[1], ir_set_obj_method,
1920 (ClientData) obj, ir_set_obj_delete);
1924 /* ------------------------------------------------------- */
1927 * do_scan: Perform scan
1929 static int do_scan (void *o, Tcl_Interp *interp, int argc, char **argv)
1932 Z_APDU apdu, *apdup = &apdu;
1933 IrTcl_ScanObj *obj = o;
1934 IrTcl_Obj *p = obj->parent;
1938 struct ccl_rpn_node *rpn;
1947 interp->result = "wrong # args";
1950 if (!p->set_inher.num_databaseNames)
1952 interp->result = "no databaseNames";
1957 interp->result = "not connected";
1960 odr_reset (p->odr_out);
1962 bib1.proto = PROTO_Z3950;
1963 bib1.class = CLASS_ATTSET;
1964 bib1.value = VAL_BIB1;
1966 apdu.which = Z_APDU_scanRequest;
1967 apdu.u.scanRequest = &req;
1968 set_referenceId (p->odr_out, &req.referenceId, p->set_inher.referenceId);
1969 req.num_databaseNames = p->set_inher.num_databaseNames;
1970 req.databaseNames = p->set_inher.databaseNames;
1971 req.attributeSet = oid_getoidbyent (&bib1);
1974 if (!(req.termListAndStartPoint = p_query_scan (p->odr_out, argv[2])))
1976 Tcl_AppendResult (interp, "Syntax error in query", NULL);
1980 rpn = ccl_find_str(p->bibset, argv[2], &r, &pos);
1983 Tcl_AppendResult (interp, "CCL error: ", ccl_err_msg (r), NULL);
1986 ccl_pr_tree (rpn, stderr);
1987 fprintf (stderr, "\n");
1988 if (!(req.termListAndStartPoint = ccl_scan_query (rpn)))
1991 req.stepSize = &obj->stepSize;
1992 req.numberOfTermsRequested = &obj->numberOfTermsRequested;
1993 req.preferredPositionInResponse = &obj->preferredPositionInResponse;
1994 logf (LOG_DEBUG, "stepSize=%d", *req.stepSize);
1995 logf (LOG_DEBUG, "numberOfTermsRequested=%d",
1996 *req.numberOfTermsRequested);
1997 logf (LOG_DEBUG, "preferredPositionInResponse=%d",
1998 *req.preferredPositionInResponse);
2000 if (!z_APDU (p->odr_out, &apdup, 0))
2002 interp->result = odr_errlist [odr_geterror (p->odr_out)];
2003 odr_reset (p->odr_out);
2006 p->sbuf = odr_getbuf (p->odr_out, &p->slen, NULL);
2007 if ((r=cs_put (p->cs_link, p->sbuf, p->slen)) < 0)
2009 interp->result = "cs_put failed in scan";
2014 ir_select_add_write (cs_fileno(p->cs_link), p);
2015 logf (LOG_DEBUG, "Sent part of scanRequest (%d bytes)", p->slen);
2019 logf (LOG_DEBUG, "Whole scan request (%d bytes)", p->slen);
2025 * do_stepSize: Set/get replace Step Size
2027 static int do_stepSize (void *obj, Tcl_Interp *interp,
2028 int argc, char **argv)
2030 IrTcl_ScanObj *p = obj;
2036 return get_set_int (&p->stepSize, interp, argc, argv);
2040 * do_numberOfTermsRequested: Set/get Number of Terms requested
2042 static int do_numberOfTermsRequested (void *obj, Tcl_Interp *interp,
2043 int argc, char **argv)
2045 IrTcl_ScanObj *p = obj;
2049 p->numberOfTermsRequested = 20;
2052 return get_set_int (&p->numberOfTermsRequested, interp, argc, argv);
2057 * do_preferredPositionInResponse: Set/get preferred Position
2059 static int do_preferredPositionInResponse (void *obj, Tcl_Interp *interp,
2060 int argc, char **argv)
2062 IrTcl_ScanObj *p = obj;
2066 p->preferredPositionInResponse = 1;
2069 return get_set_int (&p->preferredPositionInResponse, interp, argc, argv);
2073 * do_scanStatus: Get scan status
2075 static int do_scanStatus (void *obj, Tcl_Interp *interp,
2076 int argc, char **argv)
2078 IrTcl_ScanObj *p = obj;
2082 return get_set_int (&p->scanStatus, interp, argc, argv);
2086 * do_numberOfEntriesReturned: Get number of Entries returned
2088 static int do_numberOfEntriesReturned (void *obj, Tcl_Interp *interp,
2089 int argc, char **argv)
2091 IrTcl_ScanObj *p = obj;
2095 return get_set_int (&p->numberOfEntriesReturned, interp, argc, argv);
2099 * do_positionOfTerm: Get position of Term
2101 static int do_positionOfTerm (void *obj, Tcl_Interp *interp,
2102 int argc, char **argv)
2104 IrTcl_ScanObj *p = obj;
2108 return get_set_int (&p->positionOfTerm, interp, argc, argv);
2112 * do_scanLine: get Scan Line (surrogate or normal) after response
2114 static int do_scanLine (void *obj, Tcl_Interp *interp, int argc, char **argv)
2116 IrTcl_ScanObj *p = obj;
2122 p->entries_flag = 0;
2124 p->nonSurrogateDiagnostics = NULL;
2127 else if (argc == -1)
2129 p->entries_flag = 0;
2130 /* release entries */
2132 /* release non diagnostics */
2133 p->nonSurrogateDiagnostics = NULL;
2138 interp->result = "wrong # args";
2141 if (Tcl_GetInt (interp, argv[2], &i) == TCL_ERROR)
2143 if (!p->entries_flag || p->which != Z_ListEntries_entries || !p->entries
2144 || i >= p->num_entries || i < 0)
2146 switch (p->entries[i].which)
2148 case Z_Entry_termInfo:
2149 Tcl_AppendElement (interp, "T");
2150 if (p->entries[i].u.term.buf)
2151 Tcl_AppendElement (interp, p->entries[i].u.term.buf);
2153 Tcl_AppendElement (interp, "");
2154 sprintf (numstr, "%d", p->entries[i].u.term.globalOccurrences);
2155 Tcl_AppendElement (interp, numstr);
2157 case Z_Entry_surrogateDiagnostic:
2159 mk_nonSurrogateDiagnostics (interp, p->entries[i].u.diag.condition,
2160 p->entries[i].u.diag.addinfo);
2166 static IrTcl_Method ir_scan_method_tab[] = {
2167 { 0, "scan", do_scan },
2168 { 0, "stepSize", do_stepSize },
2169 { 0, "numberOfTermsRequested", do_numberOfTermsRequested },
2170 { 0, "preferredPositionInResponse", do_preferredPositionInResponse },
2171 { 0, "scanStatus", do_scanStatus },
2172 { 0, "numberOfEntriesReturned", do_numberOfEntriesReturned },
2173 { 0, "positionOfTerm", do_positionOfTerm },
2174 { 0, "scanLine", do_scanLine },
2179 * ir_scan_obj_method: IR Scan Object methods
2181 static int ir_scan_obj_method (ClientData clientData, Tcl_Interp *interp,
2182 int argc, char **argv)
2184 IrTcl_Methods tabs[2];
2188 interp->result = "wrong # args";
2191 tabs[0].tab = ir_scan_method_tab;
2192 tabs[0].obj = clientData;
2195 return ir_method (interp, argc, argv, tabs);
2199 * ir_scan_obj_delete: IR Scan Object disposal
2201 static void ir_scan_obj_delete (ClientData clientData)
2203 IrTcl_Methods tabs[2];
2204 IrTcl_ScanObj *obj = clientData;
2206 tabs[0].tab = ir_scan_method_tab;
2210 ir_method (NULL, -1, NULL, tabs);
2215 * ir_scan_obj_mk: IR Scan Object creation
2217 static int ir_scan_obj_mk (ClientData clientData, Tcl_Interp *interp,
2218 int argc, char **argv)
2220 Tcl_CmdInfo parent_info;
2222 IrTcl_Methods tabs[2];
2226 interp->result = "wrong # args";
2229 if (!Tcl_GetCommandInfo (interp, argv[2], &parent_info))
2231 interp->result = "No parent";
2234 if (!(obj = ir_malloc (interp, sizeof(*obj))))
2237 obj->parent = (IrTcl_Obj *) parent_info.clientData;
2239 tabs[0].tab = ir_scan_method_tab;
2243 if (ir_method (interp, 0, NULL, tabs) == TCL_ERROR)
2245 Tcl_CreateCommand (interp, argv[1], ir_scan_obj_method,
2246 (ClientData) obj, ir_scan_obj_delete);
2250 /* ------------------------------------------------------- */
2252 static void ir_initResponse (void *obj, Z_InitResponse *initrs)
2256 p->initResult = *initrs->result ? 1 : 0;
2257 if (!*initrs->result)
2258 logf (LOG_DEBUG, "Connection rejected by target");
2260 logf (LOG_DEBUG, "Connection accepted by target");
2262 get_referenceId (&p->set_inher.referenceId, initrs->referenceId);
2264 free (p->targetImplementationId);
2265 ir_strdup (p->interp, &p->targetImplementationId,
2266 initrs->implementationId);
2267 free (p->targetImplementationName);
2268 ir_strdup (p->interp, &p->targetImplementationName,
2269 initrs->implementationName);
2270 free (p->targetImplementationVersion);
2271 ir_strdup (p->interp, &p->targetImplementationVersion,
2272 initrs->implementationVersion);
2274 p->maximumRecordSize = *initrs->maximumRecordSize;
2275 p->preferredMessageSize = *initrs->preferredMessageSize;
2277 memcpy (&p->options, initrs->options, sizeof(initrs->options));
2278 memcpy (&p->protocolVersion, initrs->protocolVersion,
2279 sizeof(initrs->protocolVersion));
2280 free (p->userInformationField);
2281 p->userInformationField = NULL;
2282 if (initrs->userInformationField)
2286 if (initrs->userInformationField->which == ODR_EXTERNAL_octet &&
2287 (p->userInformationField =
2289 initrs->userInformationField->u.octet_aligned->len)
2292 memcpy (p->userInformationField,
2293 initrs->userInformationField->u.octet_aligned->buf,
2295 (p->userInformationField)[len] = '\0';
2300 static void ir_handleRecords (void *o, Z_Records *zrs)
2303 IrTcl_SetObj *setobj = p->set_child;
2305 setobj->which = zrs->which;
2306 setobj->recordFlag = 1;
2307 if (zrs->which == Z_Records_NSD)
2309 const char *addinfo;
2311 setobj->numberOfRecordsReturned = 0;
2312 setobj->condition = *zrs->u.nonSurrogateDiagnostic->condition;
2313 free (setobj->addinfo);
2314 setobj->addinfo = NULL;
2315 addinfo = zrs->u.nonSurrogateDiagnostic->addinfo;
2316 if (addinfo && (setobj->addinfo = malloc (strlen(addinfo) + 1)))
2317 strcpy (setobj->addinfo, addinfo);
2318 logf (LOG_DEBUG, "Diagnostic response. %s (%d): %s",
2319 diagbib1_str (setobj->condition),
2321 setobj->addinfo ? setobj->addinfo : "");
2326 IrTcl_RecordList *rl;
2328 setobj->numberOfRecordsReturned =
2329 zrs->u.databaseOrSurDiagnostics->num_records;
2330 logf (LOG_DEBUG, "Got %d records", setobj->numberOfRecordsReturned);
2331 for (offset = 0; offset<setobj->numberOfRecordsReturned; offset++)
2333 rl = new_IR_record (setobj, setobj->start + offset,
2334 zrs->u.databaseOrSurDiagnostics->
2335 records[offset]->which);
2336 if (rl->which == Z_NamePlusRecord_surrogateDiagnostic)
2340 diagrec = zrs->u.databaseOrSurDiagnostics->
2341 records[offset]->u.surrogateDiagnostic;
2343 rl->u.diag.condition = *diagrec->condition;
2344 if (diagrec->addinfo && (rl->u.diag.addinfo =
2345 malloc (strlen (diagrec->addinfo)+1)))
2346 strcpy (rl->u.diag.addinfo, diagrec->addinfo);
2350 Z_DatabaseRecord *zr;
2353 zr = zrs->u.databaseOrSurDiagnostics->records[offset]
2355 oe = (Odr_external*) zr;
2356 rl->u.dbrec.size = zr->u.octet_aligned->len;
2357 if (oe->which == ODR_EXTERNAL_octet && rl->u.dbrec.size > 0)
2359 const char *buf = (char*) zr->u.octet_aligned->buf;
2360 if ((rl->u.dbrec.buf = malloc (rl->u.dbrec.size)))
2361 memcpy (rl->u.dbrec.buf, buf, rl->u.dbrec.size);
2364 rl->u.dbrec.buf = NULL;
2370 static void ir_searchResponse (void *o, Z_SearchResponse *searchrs)
2373 IrTcl_SetObj *setobj = p->set_child;
2374 Z_Records *zrs = searchrs->records;
2376 logf (LOG_DEBUG, "Received search response");
2379 logf (LOG_DEBUG, "Search response, no object!");
2382 setobj->searchStatus = searchrs->searchStatus ? 1 : 0;
2383 get_referenceId (&setobj->set_inher.referenceId, searchrs->referenceId);
2384 setobj->resultCount = *searchrs->resultCount;
2385 if (searchrs->presentStatus)
2386 setobj->presentStatus = *searchrs->presentStatus;
2387 if (searchrs->nextResultSetPosition)
2388 setobj->nextResultSetPosition = *searchrs->nextResultSetPosition;
2390 logf (LOG_DEBUG, "Search response %d, %d hits",
2391 setobj->searchStatus, setobj->resultCount);
2393 ir_handleRecords (o, zrs);
2395 setobj->recordFlag = 0;
2399 static void ir_presentResponse (void *o, Z_PresentResponse *presrs)
2402 IrTcl_SetObj *setobj = p->set_child;
2403 Z_Records *zrs = presrs->records;
2405 logf (LOG_DEBUG, "Received present response");
2408 logf (LOG_DEBUG, "Present response, no object!");
2411 setobj->presentStatus = *presrs->presentStatus;
2412 get_referenceId (&setobj->set_inher.referenceId, presrs->referenceId);
2413 setobj->nextResultSetPosition = *presrs->nextResultSetPosition;
2415 ir_handleRecords (o, zrs);
2418 setobj->recordFlag = 0;
2419 logf (LOG_DEBUG, "No records!");
2423 static void ir_scanResponse (void *o, Z_ScanResponse *scanrs)
2426 IrTcl_ScanObj *scanobj = p->scan_child;
2428 logf (LOG_DEBUG, "Received scanResponse");
2430 get_referenceId (&p->set_inher.referenceId, scanrs->referenceId);
2431 scanobj->scanStatus = *scanrs->scanStatus;
2432 logf (LOG_DEBUG, "scanStatus=%d", scanobj->scanStatus);
2434 if (scanrs->stepSize)
2435 scanobj->stepSize = *scanrs->stepSize;
2436 logf (LOG_DEBUG, "stepSize=%d", scanobj->stepSize);
2438 scanobj->numberOfEntriesReturned = *scanrs->numberOfEntriesReturned;
2439 logf (LOG_DEBUG, "numberOfEntriesReturned=%d",
2440 scanobj->numberOfEntriesReturned);
2442 if (scanrs->positionOfTerm)
2443 scanobj->positionOfTerm = *scanrs->positionOfTerm;
2445 scanobj->positionOfTerm = -1;
2446 logf (LOG_DEBUG, "positionOfTerm=%d", scanobj->positionOfTerm);
2448 free (scanobj->entries);
2449 scanobj->entries = NULL;
2450 free (scanobj->nonSurrogateDiagnostics);
2451 scanobj->nonSurrogateDiagnostics = NULL;
2453 if (scanrs->entries)
2458 scanobj->entries_flag = 1;
2459 scanobj->which = scanrs->entries->which;
2460 switch (scanobj->which)
2462 case Z_ListEntries_entries:
2463 scanobj->num_entries = scanrs->entries->u.entries->num_entries;
2464 scanobj->entries = malloc (scanobj->num_entries *
2465 sizeof(*scanobj->entries));
2466 for (i=0; i<scanobj->num_entries; i++)
2468 ze = scanrs->entries->u.entries->entries[i];
2469 scanobj->entries[i].which = ze->which;
2472 case Z_Entry_termInfo:
2473 if (ze->u.termInfo->term->which == Z_Term_general)
2475 int l = ze->u.termInfo->term->u.general->len;
2476 scanobj->entries[i].u.term.buf = malloc (1+l);
2477 memcpy (scanobj->entries[i].u.term.buf,
2478 ze->u.termInfo->term->u.general->buf,
2480 scanobj->entries[i].u.term.buf[l] = '\0';
2483 scanobj->entries[i].u.term.buf = NULL;
2484 if (ze->u.termInfo->globalOccurrences)
2485 scanobj->entries[i].u.term.globalOccurrences =
2486 *ze->u.termInfo->globalOccurrences;
2488 scanobj->entries[i].u.term.globalOccurrences = 0;
2490 case Z_Entry_surrogateDiagnostic:
2491 scanobj->entries[i].u.diag.addinfo =
2492 malloc (1+strlen(ze->u.surrogateDiagnostic->
2494 strcpy (scanobj->entries[i].u.diag.addinfo,
2495 ze->u.surrogateDiagnostic->addinfo);
2496 scanobj->entries[i].u.diag.condition =
2497 *ze->u.surrogateDiagnostic->condition;
2502 case Z_ListEntries_nonSurrogateDiagnostics:
2503 scanobj->num_diagRecs = scanrs->entries->
2504 u.nonSurrogateDiagnostics->num_diagRecs;
2505 scanobj->nonSurrogateDiagnostics = malloc (scanobj->num_diagRecs *
2506 sizeof(*scanobj->nonSurrogateDiagnostics));
2511 scanobj->entries_flag = 0;
2515 * ir_select_read: handle incoming packages
2517 void ir_select_read (ClientData clientData)
2519 IrTcl_Obj *p = clientData;
2525 r = cs_rcvconnect (p->cs_link);
2529 ir_select_remove_write (cs_fileno (p->cs_link), p);
2532 logf (LOG_DEBUG, "cs_rcvconnect error");
2534 Tcl_Eval (p->interp, p->failback);
2535 do_disconnect (p, NULL, 2, NULL);
2539 Tcl_Eval (p->interp, p->callback);
2544 /* signal one more use of ir object - callbacks must not
2545 release the ir memory (p pointer) */
2547 if ((r=cs_get (p->cs_link, &p->buf_in, &p->len_in)) <= 0)
2549 logf (LOG_DEBUG, "cs_get failed, code %d", r);
2550 ir_select_remove (cs_fileno (p->cs_link), p);
2552 Tcl_Eval (p->interp, p->failback);
2553 do_disconnect (p, NULL, 2, NULL);
2555 /* relase ir object now if callback deleted it */
2561 odr_setbuf (p->odr_in, p->buf_in, r, 0);
2562 logf (LOG_DEBUG, "cs_get ok, got %d", r);
2563 if (!z_APDU (p->odr_in, &apdu, 0))
2565 logf (LOG_DEBUG, "%s", odr_errlist [odr_geterror (p->odr_in)]);
2567 Tcl_Eval (p->interp, p->failback);
2568 do_disconnect (p, NULL, 2, NULL);
2570 /* release ir object now if callback deleted it */
2576 case Z_APDU_initResponse:
2577 ir_initResponse (p, apdu->u.initResponse);
2579 case Z_APDU_searchResponse:
2580 ir_searchResponse (p, apdu->u.searchResponse);
2582 case Z_APDU_presentResponse:
2583 ir_presentResponse (p, apdu->u.presentResponse);
2585 case Z_APDU_scanResponse:
2586 ir_scanResponse (p, apdu->u.scanResponse);
2589 logf (LOG_WARN, "Received unknown APDU type (%d)", apdu->which);
2591 Tcl_Eval (p->interp, p->failback);
2592 do_disconnect (p, NULL, 2, NULL);
2594 odr_reset (p->odr_in);
2596 Tcl_Eval (p->interp, p->callback);
2597 if (p->ref_count == 1)
2603 } while (p->cs_link && cs_more (p->cs_link));
2607 * ir_select_write: handle outgoing packages - not yet written.
2609 void ir_select_write (ClientData clientData)
2611 IrTcl_Obj *p = clientData;
2614 logf (LOG_DEBUG, "In write handler");
2617 r = cs_rcvconnect (p->cs_link);
2623 logf (LOG_DEBUG, "cs_rcvconnect error");
2624 ir_select_remove_write (cs_fileno (p->cs_link), p);
2626 Tcl_Eval (p->interp, p->failback);
2627 do_disconnect (p, NULL, 2, NULL);
2630 ir_select_remove_write (cs_fileno (p->cs_link), p);
2632 Tcl_Eval (p->interp, p->callback);
2635 if ((r=cs_put (p->cs_link, p->sbuf, p->slen)) < 0)
2637 logf (LOG_DEBUG, "select write fail");
2639 Tcl_Eval (p->interp, p->failback);
2640 do_disconnect (p, NULL, 2, NULL);
2642 else if (r == 0) /* remove select bit */
2644 ir_select_remove_write (cs_fileno (p->cs_link), p);
2648 /* ------------------------------------------------------- */
2651 * ir_tcl_init: Registration of TCL commands.
2653 int ir_tcl_init (Tcl_Interp *interp)
2655 Tcl_CreateCommand (interp, "ir", ir_obj_mk, (ClientData) NULL,
2656 (Tcl_CmdDeleteProc *) NULL);
2657 Tcl_CreateCommand (interp, "ir-set", ir_set_obj_mk,
2658 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
2659 Tcl_CreateCommand (interp, "ir-scan", ir_scan_obj_mk,
2660 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);