2 * IR toolkit for tcl/tk
4 * See the file LICENSE for details.
5 * Sebastian Hammer, Adam Dickmeiss
8 * Revision 1.36 1995-05-31 13:09:59 adam
9 * Client searches/presents may be interrupted.
10 * New moving book-logo.
12 * Revision 1.35 1995/05/31 08:36:33 adam
13 * Bug fix in client.tcl: didn't save options on clientrc.tcl.
14 * New method: referenceId. More work on scan.
16 * Revision 1.34 1995/05/29 10:33:42 adam
17 * README and rename of startup script.
19 * Revision 1.33 1995/05/29 09:15:11 quinn
20 * Changed CS_SR to PROTO_SR, etc.
22 * Revision 1.32 1995/05/29 08:44:16 adam
23 * Work on delete of objects.
25 * Revision 1.31 1995/05/26 11:44:10 adam
26 * Bugs fixed. More work on MARC utilities and queries. Test
27 * client is up-to-date again.
29 * Revision 1.30 1995/05/26 08:54:11 adam
30 * New MARC utilities. Uses prefix query.
32 * Revision 1.29 1995/05/24 14:10:22 adam
33 * Work on idAuthentication, protocolVersion and options.
35 * Revision 1.28 1995/05/23 15:34:48 adam
36 * Many new settings, userInformationField, smallSetUpperBound, etc.
37 * A number of settings are inherited when ir-set is executed.
38 * This version is incompatible with the graphical test client (client.tcl).
40 * Revision 1.27 1995/05/11 15:34:47 adam
41 * Scan request changed a bit. This version works with RLG.
43 * Revision 1.26 1995/04/18 16:11:51 adam
44 * First version of graphical Scan. Some work on query-by-form.
46 * Revision 1.25 1995/04/17 09:37:17 adam
47 * Further development of scan.
49 * Revision 1.24 1995/04/11 14:16:42 adam
50 * Further work on scan. Response works. Entries aren't saved yet.
52 * Revision 1.23 1995/04/10 10:50:27 adam
53 * Result-set name defaults to suffix of ir-set name.
54 * Started working on scan. Not finished at this point.
56 * Revision 1.22 1995/03/31 10:43:03 adam
57 * More robust when getting bad MARC records.
59 * Revision 1.21 1995/03/31 08:56:37 adam
60 * New button "Search".
62 * Revision 1.20 1995/03/29 16:07:09 adam
63 * Bug fix: Didn't use setName in present request.
65 * Revision 1.19 1995/03/28 12:45:23 adam
66 * New ir method failback: called on disconnect/protocol error.
67 * New ir set/get method: protocol: SR / Z3950.
68 * Simple popup and disconnect when failback is invoked.
70 * Revision 1.18 1995/03/21 15:50:12 adam
73 * Revision 1.17 1995/03/21 13:41:03 adam
74 * Comstack cs_create not used too often. Non-blocking connect.
76 * Revision 1.16 1995/03/21 08:26:06 adam
77 * New method, setName, to specify the result set name (other than Default).
78 * New method, responseStatus, which returns diagnostic info, if any, after
79 * present response / search response.
81 * Revision 1.15 1995/03/20 15:24:07 adam
82 * Diagnostic records saved on searchResponse.
84 * Revision 1.14 1995/03/20 08:53:22 adam
85 * Event loop in tclmain.c rewritten. New method searchStatus.
87 * Revision 1.13 1995/03/17 18:26:17 adam
88 * Non-blocking i/o used now. Database names popup as cascade items.
90 * Revision 1.12 1995/03/17 15:45:00 adam
91 * Improved target/database setup.
93 * Revision 1.11 1995/03/16 17:54:03 adam
94 * Minor changes really.
96 * Revision 1.10 1995/03/15 16:14:50 adam
97 * Blocking arg in cs_create changed.
99 * Revision 1.9 1995/03/15 13:59:24 adam
102 * Revision 1.8 1995/03/15 08:25:16 adam
103 * New method presentStatus to check for error on present. Misc. cleanup
104 * of IRRecordList manipulations. Full MARC record presentation in
107 * Revision 1.7 1995/03/14 17:32:29 adam
108 * Presentation of full Marc record in popup window.
110 * Revision 1.6 1995/03/12 19:31:55 adam
111 * Pattern matching implemented when retrieving MARC records. More
112 * diagnostic functions.
114 * Revision 1.5 1995/03/10 18:00:15 adam
115 * Actual presentation in line-by-line format. RPN query support.
117 * Revision 1.4 1995/03/09 16:15:08 adam
118 * First presentRequest attempts. Hot-target list.
124 #include <sys/time.h>
134 int (*method) (void *obj, Tcl_Interp *interp, int argc, char **argv);
142 static int do_disconnect (void *obj,Tcl_Interp *interp, int argc, char **argv);
144 static IRRecordList *new_IR_record (IRSetObj *setobj, int no, int which)
148 for (rl = setobj->record_list; rl; rl = rl->next)
154 case Z_NamePlusRecord_databaseRecord:
155 free (rl->u.dbrec.buf);
156 rl->u.dbrec.buf = NULL;
158 case Z_NamePlusRecord_surrogateDiagnostic:
159 free (rl->u.diag.addinfo);
160 rl->u.diag.addinfo = NULL;
168 rl = malloc (sizeof(*rl));
170 rl->next = setobj->record_list;
172 setobj->record_list = rl;
178 static IRRecordList *find_IR_record (IRSetObj *setobj, int no)
182 for (rl = setobj->record_list; rl; rl = rl->next)
188 static void delete_IR_records (IRSetObj *setobj)
190 IRRecordList *rl, *rl1;
192 for (rl = setobj->record_list; rl; rl = rl1)
196 case Z_NamePlusRecord_databaseRecord:
197 free (rl->u.dbrec.buf);
199 case Z_NamePlusRecord_surrogateDiagnostic:
200 free (rl->u.diag.addinfo);
206 setobj->record_list = NULL;
210 * getsetint: Set/get integer value
212 static int get_set_int (int *val, Tcl_Interp *interp, int argc, char **argv)
218 if (Tcl_GetInt (interp, argv[2], val)==TCL_ERROR)
221 sprintf (buf, "%d", *val);
222 Tcl_AppendResult (interp, buf, NULL);
227 * mk_nonSurrogateDiagnostics: Make Tcl result with diagnostic info
229 static int mk_nonSurrogateDiagnostics (Tcl_Interp *interp,
236 Tcl_AppendElement (interp, "NSD");
237 sprintf (buf, "%d", condition);
238 Tcl_AppendElement (interp, buf);
239 cp = diagbib1_str (condition);
241 Tcl_AppendElement (interp, (char*) cp);
243 Tcl_AppendElement (interp, "");
245 Tcl_AppendElement (interp, (char*) addinfo);
247 Tcl_AppendElement (interp, "");
252 * ir_method: Search for method in table and invoke method handler
254 int ir_method (Tcl_Interp *interp, int argc, char **argv, IRMethods *tab)
256 IRMethods *tab_i = tab;
259 for (tab_i = tab; tab_i->tab; tab_i++)
260 for (t = tab_i->tab; t->name; t++)
263 if ((*t->method)(tab_i->obj, interp, argc, argv) == TCL_ERROR)
267 if (!strcmp (t->name, argv[1]))
268 return (*t->method)(tab_i->obj, interp, argc, argv);
272 Tcl_AppendResult (interp, "Bad method. Possible methods:", NULL);
273 for (tab_i = tab; tab_i->tab; tab_i++)
274 for (t = tab_i->tab; t->name; t++)
275 Tcl_AppendResult (interp, " ", t->name, NULL);
280 * ir_method_r: Get status for all readable elements
282 int ir_method_r (void *obj, Tcl_Interp *interp, int argc, char **argv,
290 for (; tab->name; tab++)
293 argv_n[1] = tab->name;
294 Tcl_AppendResult (interp, "{", NULL);
295 (*tab->method)(obj, interp, argc_n, argv_n);
296 Tcl_AppendResult (interp, "} ", NULL);
302 * ir_named_bits: get/set named bits
304 int ir_named_bits (struct ir_named_entry *tab, Odr_bitmask *ob,
305 Tcl_Interp *interp, int argc, char **argv)
307 struct ir_named_entry *ti;
312 for (no = 0; no < argc; no++)
314 for (ti = tab; ti->name; ti++)
315 if (!strcmp (argv[no], ti->name))
317 ODR_MASK_SET (ob, ti->pos);
322 Tcl_AppendResult (interp, "Bad bit mask: ", argv[no], NULL);
328 for (ti = tab; ti->name; ti++)
329 if (ODR_MASK_GET (ob, ti->pos))
330 Tcl_AppendElement (interp, ti->name);
335 * ir_strdup: Duplicate string
337 int ir_strdup (Tcl_Interp *interp, char** p, const char *s)
339 *p = malloc (strlen(s)+1);
342 interp->result = "strdup fail";
350 * ir_strdel: Delete string
352 int ir_strdel (Tcl_Interp *interp, char **p)
360 * ir_malloc: Malloc function
362 void *ir_malloc (Tcl_Interp *interp, size_t size)
364 static char buf[128];
365 void *p = malloc (size);
369 sprintf (buf, "Malloc fail. %ld bytes requested", (long) size);
370 interp->result = buf;
376 static void set_referenceId (ODR o, Z_ReferenceId **dst, const char *src)
382 *dst = odr_malloc (o, sizeof(**dst));
383 (*dst)->size = (*dst)->len = strlen(src);
384 (*dst)->buf = odr_malloc (o, (*dst)->len);
385 memcpy ((*dst)->buf, src, (*dst)->len);
388 /* ------------------------------------------------------- */
391 * do_init_request: init method on IR object
393 static int do_init_request (void *obj, Tcl_Interp *interp,
394 int argc, char **argv)
396 Z_APDU apdu, *apdup = &apdu;
405 interp->result = "not connected";
408 odr_reset (p->odr_out);
410 set_referenceId (p->odr_out, &req.referenceId, p->set_inher.referenceId);
411 req.options = &p->options;
412 req.protocolVersion = &p->protocolVersion;
413 req.preferredMessageSize = &p->preferredMessageSize;
414 req.maximumRecordSize = &p->maximumRecordSize;
416 if (p->idAuthenticationGroupId)
418 Z_IdPass *pass = odr_malloc (p->odr_out, sizeof(*pass));
419 Z_IdAuthentication *auth = odr_malloc (p->odr_out, sizeof(*auth));
421 auth->which = Z_IdAuthentication_idPass;
422 auth->u.idPass = pass;
423 if (p->idAuthenticationGroupId && *p->idAuthenticationGroupId)
424 pass->groupId = p->idAuthenticationGroupId;
426 pass->groupId = NULL;
427 if (p->idAuthenticationUserId && *p->idAuthenticationUserId)
428 pass->userId = p->idAuthenticationUserId;
431 if (p->idAuthenticationPassword && *p->idAuthenticationPassword)
432 pass->password = p->idAuthenticationPassword;
434 pass->password = NULL;
435 req.idAuthentication = auth;
437 else if (!p->idAuthenticationOpen || !*p->idAuthenticationOpen)
438 req.idAuthentication = NULL;
441 Z_IdAuthentication *auth = odr_malloc (p->odr_out, sizeof(*auth));
443 auth->which = Z_IdAuthentication_open;
444 auth->u.open = p->idAuthenticationOpen;
445 req.idAuthentication = auth;
447 req.implementationId = p->implementationId;
448 req.implementationName = p->implementationName;
449 req.implementationVersion = "0.1";
450 req.userInformationField = 0;
452 apdu.u.initRequest = &req;
453 apdu.which = Z_APDU_initRequest;
455 if (!z_APDU (p->odr_out, &apdup, 0))
457 Tcl_AppendResult (interp, odr_errlist [odr_geterror (p->odr_out)],
459 odr_reset (p->odr_out);
462 p->sbuf = odr_getbuf (p->odr_out, &p->slen, NULL);
463 if ((r=cs_put (p->cs_link, p->sbuf, p->slen)) < 0)
465 interp->result = "cs_put failed in init";
466 do_disconnect (p, NULL, 2, NULL);
471 ir_select_add_write (cs_fileno(p->cs_link), p);
472 logf (LOG_DEBUG, "Sent part of initializeRequest (%d bytes)", p->slen);
475 logf (LOG_DEBUG, "Sent whole initializeRequest (%d bytes)", p->slen);
480 * do_protocolVersion: Set protocol Version
482 static int do_protocolVersion (void *obj, Tcl_Interp *interp,
483 int argc, char **argv)
485 static struct ir_named_entry version_tab[] = {
496 ODR_MASK_ZERO (&p->protocolVersion);
497 ODR_MASK_SET (&p->protocolVersion, 0);
498 ODR_MASK_SET (&p->protocolVersion, 1);
501 return ir_named_bits (version_tab, &p->protocolVersion,
502 interp, argc-2, argv+2);
506 * do_options: Set options
508 static int do_options (void *obj, Tcl_Interp *interp,
509 int argc, char **argv)
511 static struct ir_named_entry options_tab[] = {
515 { "resourceReport", 3 },
516 { "triggerResourceCtrl", 4},
517 { "resourceCtrl", 5},
521 { "extentedServices", 10},
522 { "level-1Segmentation", 11},
523 { "level-2Segmentation", 12},
524 { "concurrentOperations", 13},
525 { "namedResultSets", 14},
532 ODR_MASK_ZERO (&p->options);
533 ODR_MASK_SET (&p->options, 0);
534 ODR_MASK_SET (&p->options, 1);
535 ODR_MASK_SET (&p->options, 7);
536 ODR_MASK_SET (&p->options, 14);
539 return ir_named_bits (options_tab, &p->options, interp, argc-2, argv+2);
543 * do_preferredMessageSize: Set/get preferred message size
545 static int do_preferredMessageSize (void *obj, Tcl_Interp *interp,
546 int argc, char **argv)
552 p->preferredMessageSize = 4096;
555 return get_set_int (&p->preferredMessageSize, interp, argc, argv);
559 * do_maximumRecordSize: Set/get maximum record size
561 static int do_maximumRecordSize (void *obj, Tcl_Interp *interp,
562 int argc, char **argv)
568 p->maximumRecordSize = 32768;
571 return get_set_int (&p->maximumRecordSize, interp, argc, argv);
575 * do_initResult: Get init result
577 static int do_initResult (void *obj, Tcl_Interp *interp,
578 int argc, char **argv)
584 return get_set_int (&p->initResult, interp, argc, argv);
589 * do_implementationName: Set/get Implementation Name.
591 static int do_implementationName (void *obj, Tcl_Interp *interp,
592 int argc, char **argv)
597 return ir_strdup (interp, &p->implementationName, "TCL/TK on YAZ");
599 return ir_strdel (interp, &p->implementationName);
602 free (p->implementationName);
603 if (ir_strdup (interp, &p->implementationName, argv[2])
607 Tcl_AppendResult (interp, p->implementationName, (char*) NULL);
612 * do_implementationId: Set/get Implementation Id.
614 static int do_implementationId (void *obj, Tcl_Interp *interp,
615 int argc, char **argv)
620 return ir_strdup (interp, &p->implementationId, "81");
622 return ir_strdel (interp, &p->implementationId);
625 free (p->implementationId);
626 if (ir_strdup (interp, &p->implementationId, argv[2]) == TCL_ERROR)
629 Tcl_AppendResult (interp, p->implementationId, (char*) NULL);
634 * do_targetImplementationName: Get Implementation Name of target.
636 static int do_targetImplementationName (void *obj, Tcl_Interp *interp,
637 int argc, char **argv)
643 p->targetImplementationName = NULL;
647 return ir_strdel (interp, &p->targetImplementationName);
648 Tcl_AppendResult (interp, p->targetImplementationName, (char*) NULL);
653 * do_targetImplementationId: Get Implementation Id of target
655 static int do_targetImplementationId (void *obj, Tcl_Interp *interp,
656 int argc, char **argv)
662 p->targetImplementationId = NULL;
666 return ir_strdel (interp, &p->targetImplementationId);
667 Tcl_AppendResult (interp, p->targetImplementationId, (char*) NULL);
672 * do_targetImplementationVersion: Get Implementation Version of target
674 static int do_targetImplementationVersion (void *obj, Tcl_Interp *interp,
675 int argc, char **argv)
681 p->targetImplementationVersion = NULL;
685 return ir_strdel (interp, &p->targetImplementationVersion);
686 Tcl_AppendResult (interp, p->targetImplementationVersion, (char*) NULL);
691 * do_idAuthentication: Set/get id Authentication
693 static int do_idAuthentication (void *obj, Tcl_Interp *interp,
694 int argc, char **argv)
698 if (argc >= 3 || argc == -1)
700 free (p->idAuthenticationOpen);
701 free (p->idAuthenticationGroupId);
702 free (p->idAuthenticationUserId);
703 free (p->idAuthenticationPassword);
705 if (argc >= 3 || argc <= 0)
707 p->idAuthenticationOpen = NULL;
708 p->idAuthenticationGroupId = NULL;
709 p->idAuthenticationUserId = NULL;
710 p->idAuthenticationPassword = NULL;
718 if (ir_strdup (interp, &p->idAuthenticationOpen, argv[2])
724 if (ir_strdup (interp, &p->idAuthenticationGroupId, argv[2])
727 if (ir_strdup (interp, &p->idAuthenticationUserId, argv[3])
730 if (ir_strdup (interp, &p->idAuthenticationPassword, argv[4])
735 if (p->idAuthenticationOpen)
736 Tcl_AppendElement (interp, p->idAuthenticationOpen);
737 else if (p->idAuthenticationGroupId)
739 Tcl_AppendElement (interp, p->idAuthenticationGroupId);
740 Tcl_AppendElement (interp, p->idAuthenticationUserId);
741 Tcl_AppendElement (interp, p->idAuthenticationPassword);
747 * do_connect: connect method on IR object
749 static int do_connect (void *obj, Tcl_Interp *interp,
750 int argc, char **argv)
755 int protocol_type = PROTO_Z3950;
763 interp->result = "already connected";
766 if (!strcmp (p->protocol_type, "Z3950"))
767 protocol_type = PROTO_Z3950;
768 else if (!strcmp (p->protocol_type, "SR"))
769 protocol_type = PROTO_SR;
772 interp->result = "bad protocol type";
775 if (!strcmp (p->cs_type, "tcpip"))
777 p->cs_link = cs_create (tcpip_type, CS_BLOCK, protocol_type);
778 addr = tcpip_strtoaddr (argv[2]);
781 interp->result = "tcpip_strtoaddr fail";
784 logf (LOG_DEBUG, "tcp/ip connect %s", argv[2]);
787 else if (!strcmp (p->cs_type, "mosi"))
789 p->cs_link = cs_create (mosi_type, CS_BLOCK, protocol_type);
790 addr = mosi_strtoaddr (argv[2]);
793 interp->result = "mosi_strtoaddr fail";
796 logf (LOG_DEBUG, "mosi connect %s", argv[2]);
801 interp->result = "unknown comstack type";
804 if (ir_strdup (interp, &p->hostname, argv[2]) == TCL_ERROR)
806 if ((r=cs_connect (p->cs_link, addr)) < 0)
808 interp->result = "cs_connect fail";
809 do_disconnect (p, NULL, 2, NULL);
812 ir_select_add (cs_fileno (p->cs_link), p);
815 ir_select_add_write (cs_fileno (p->cs_link), p);
822 Tcl_Eval (p->interp, p->callback);
826 Tcl_AppendElement (interp, p->hostname);
831 * do_disconnect: disconnect method on IR object
833 static int do_disconnect (void *obj, Tcl_Interp *interp,
834 int argc, char **argv)
849 ir_select_remove_write (cs_fileno (p->cs_link), p);
850 ir_select_remove (cs_fileno (p->cs_link), p);
853 cs_close (p->cs_link);
856 assert (!p->cs_link);
861 * do_comstack: Set/get comstack method on IR object
863 static int do_comstack (void *o, Tcl_Interp *interp,
864 int argc, char **argv)
869 return ir_strdup (interp, &obj->cs_type, "tcpip");
871 return ir_strdel (interp, &obj->cs_type);
875 if (ir_strdup (interp, &obj->cs_type, argv[2]) == TCL_ERROR)
878 Tcl_AppendElement (interp, obj->cs_type);
883 * do_protocol: Set/get protocol method on IR object
885 static int do_protocol (void *o, Tcl_Interp *interp,
886 int argc, char **argv)
891 return ir_strdup (interp, &obj->protocol_type, "Z3950");
893 return ir_strdel (interp, &obj->protocol_type);
896 free (obj->protocol_type);
897 if (ir_strdup (interp, &obj->protocol_type, argv[2]) == TCL_ERROR)
900 Tcl_AppendElement (interp, obj->protocol_type);
905 * do_callback: add callback
907 static int do_callback (void *obj, Tcl_Interp *interp,
908 int argc, char **argv)
918 return ir_strdel (interp, &p->callback);
924 if (ir_strdup (interp, &p->callback, argv[2]) == TCL_ERROR)
935 * do_failback: add error handle callback
937 static int do_failback (void *obj, Tcl_Interp *interp,
938 int argc, char **argv)
948 return ir_strdel (interp, &p->failback);
954 if (ir_strdup (interp, &p->failback, argv[2]) == TCL_ERROR)
965 * do_databaseNames: specify database names
967 static int do_databaseNames (void *obj, Tcl_Interp *interp,
968 int argc, char **argv)
975 for (i=0; i<p->num_databaseNames; i++)
976 free (p->databaseNames[i]);
977 free (p->databaseNames);
981 p->num_databaseNames = 0;
982 p->databaseNames = NULL;
987 for (i=0; i<p->num_databaseNames; i++)
988 Tcl_AppendElement (interp, p->databaseNames[i]);
991 if (p->databaseNames)
993 for (i=0; i<p->num_databaseNames; i++)
994 free (p->databaseNames[i]);
995 free (p->databaseNames);
997 p->num_databaseNames = argc - 2;
998 if (!(p->databaseNames = ir_malloc (interp,
999 sizeof(*p->databaseNames) * p->num_databaseNames)))
1001 for (i=0; i<p->num_databaseNames; i++)
1003 if (ir_strdup (interp, &p->databaseNames[i], argv[2+i])
1011 * do_replaceIndicator: Set/get replace Set indicator
1013 static int do_replaceIndicator (void *obj, Tcl_Interp *interp,
1014 int argc, char **argv)
1020 p->replaceIndicator = 1;
1023 return get_set_int (&p->replaceIndicator, interp, argc, argv);
1027 * do_queryType: Set/Get query method
1029 static int do_queryType (void *obj, Tcl_Interp *interp,
1030 int argc, char **argv)
1035 return ir_strdup (interp, &p->queryType, "rpn");
1036 else if (argc == -1)
1037 return ir_strdel (interp, &p->queryType);
1040 free (p->queryType);
1041 if (ir_strdup (interp, &p->queryType, argv[2]) == TCL_ERROR)
1044 Tcl_AppendResult (interp, p->queryType, NULL);
1049 * do_userInformationField: Get User information field
1051 static int do_userInformationField (void *obj, Tcl_Interp *interp,
1052 int argc, char **argv)
1058 p->userInformationField = NULL;
1061 else if (argc == -1)
1062 return ir_strdel (interp, &p->userInformationField);
1063 Tcl_AppendResult (interp, p->userInformationField, NULL);
1068 * do_smallSetUpperBound: Set/get small set upper bound
1070 static int do_smallSetUpperBound (void *o, Tcl_Interp *interp,
1071 int argc, char **argv)
1077 p->smallSetUpperBound = 0;
1080 return get_set_int (&p->smallSetUpperBound, interp, argc, argv);
1084 * do_largeSetLowerBound: Set/get large set lower bound
1086 static int do_largeSetLowerBound (void *o, Tcl_Interp *interp,
1087 int argc, char **argv)
1093 p->largeSetLowerBound = 2;
1096 return get_set_int (&p->largeSetLowerBound, interp, argc, argv);
1100 * do_mediumSetPresentNumber: Set/get large set lower bound
1102 static int do_mediumSetPresentNumber (void *o, Tcl_Interp *interp,
1103 int argc, char **argv)
1109 p->mediumSetPresentNumber = 0;
1112 return get_set_int (&p->mediumSetPresentNumber, interp, argc, argv);
1116 * do_referenceId: Set/Get referenceId
1118 static int do_referenceId (void *obj, Tcl_Interp *interp,
1119 int argc, char **argv)
1124 p->referenceId = NULL;
1125 else if (argc == -1)
1126 return ir_strdel (interp, &p->referenceId);
1129 free (p->referenceId);
1130 if (ir_strdup (interp, &p->referenceId, argv[2]) == TCL_ERROR)
1133 Tcl_AppendResult (interp, p->referenceId, NULL);
1137 static IRMethod ir_method_tab[] = {
1138 { 1, "comstack", do_comstack },
1139 { 1, "protocol", do_protocol },
1140 { 0, "failback", do_failback },
1142 { 1, "connect", do_connect },
1143 { 0, "protocolVersion", do_protocolVersion },
1144 { 1, "preferredMessageSize", do_preferredMessageSize },
1145 { 1, "maximumRecordSize", do_maximumRecordSize },
1146 { 1, "implementationName", do_implementationName },
1147 { 1, "implementationId", do_implementationId },
1148 { 0, "targetImplementationName", do_targetImplementationName },
1149 { 0, "targetImplementationId", do_targetImplementationId },
1150 { 0, "targetImplementationVersion", do_targetImplementationVersion },
1151 { 0, "userInformationField", do_userInformationField },
1152 { 1, "idAuthentication", do_idAuthentication },
1153 { 0, "options", do_options },
1154 { 0, "init", do_init_request },
1155 { 0, "initResult", do_initResult },
1156 { 0, "disconnect", do_disconnect },
1157 { 0, "callback", do_callback },
1161 static IRMethod ir_set_c_method_tab[] = {
1162 { 0, "databaseNames", do_databaseNames},
1163 { 0, "replaceIndicator", do_replaceIndicator},
1164 { 0, "queryType", do_queryType },
1165 { 0, "smallSetUpperBound", do_smallSetUpperBound},
1166 { 0, "largeSetLowerBound", do_largeSetLowerBound},
1167 { 0, "mediumSetPresentNumber", do_mediumSetPresentNumber},
1168 { 0, "referenceId", do_referenceId },
1173 * ir_obj_method: IR Object methods
1175 static int ir_obj_method (ClientData clientData, Tcl_Interp *interp,
1176 int argc, char **argv)
1179 IRObj *p = clientData;
1182 return ir_method_r (clientData, interp, argc, argv, ir_method_tab);
1184 tab[0].tab = ir_method_tab;
1186 tab[1].tab = ir_set_c_method_tab;
1187 tab[1].obj = &p->set_inher;
1190 return ir_method (interp, argc, argv, tab);
1194 * ir_obj_delete: IR Object disposal
1196 static void ir_obj_delete (ClientData clientData)
1198 IRObj *obj = clientData;
1202 if (obj->ref_count > 0)
1204 assert (obj->ref_count == 0);
1206 tab[0].tab = ir_method_tab;
1208 tab[1].tab = ir_set_c_method_tab;
1209 tab[1].obj = &obj->set_inher;
1212 ir_method (NULL, -1, NULL, tab);
1213 odr_destroy (obj->odr_in);
1214 odr_destroy (obj->odr_out);
1215 odr_destroy (obj->odr_pr);
1216 free (obj->buf_out);
1222 * ir_obj_mk: IR Object creation
1224 static int ir_obj_mk (ClientData clientData, Tcl_Interp *interp,
1225 int argc, char **argv)
1235 interp->result = "wrong # args";
1238 if (!(obj = ir_malloc (interp, sizeof(*obj))))
1243 obj->bibset = ccl_qual_mk ();
1244 if ((inf = fopen ("default.bib", "r")))
1246 ccl_qual_file (obj->bibset, inf);
1251 obj->odr_in = odr_createmem (ODR_DECODE);
1252 obj->odr_out = odr_createmem (ODR_ENCODE);
1253 obj->odr_pr = odr_createmem (ODR_PRINT);
1255 obj->len_out = 10000;
1256 if (!(obj->buf_out = ir_malloc (interp, obj->len_out)))
1258 odr_setbuf (obj->odr_out, obj->buf_out, obj->len_out, 0);
1263 tab[0].tab = ir_method_tab;
1265 tab[1].tab = ir_set_c_method_tab;
1266 tab[1].obj = &obj->set_inher;
1269 if (ir_method (interp, 0, NULL, tab) == TCL_ERROR)
1271 Tcl_CreateCommand (interp, argv[1], ir_obj_method,
1272 (ClientData) obj, ir_obj_delete);
1276 /* ------------------------------------------------------- */
1278 * do_search: Do search request
1280 static int do_search (void *o, Tcl_Interp *interp,
1281 int argc, char **argv)
1283 Z_SearchRequest req;
1285 Z_APDU apdu, *apdup = &apdu;
1288 IRObj *p = obj->parent;
1298 interp->result = "wrong # args";
1301 if (!obj->set_inher.num_databaseNames)
1303 interp->result = "no databaseNames";
1308 interp->result = "not connected";
1311 odr_reset (p->odr_out);
1312 apdu.which = Z_APDU_searchRequest;
1313 apdu.u.searchRequest = &req;
1315 bib1.proto = PROTO_Z3950;
1316 bib1.class = CLASS_ATTSET;
1317 bib1.value = VAL_BIB1;
1319 set_referenceId (p->odr_out, &req.referenceId, obj->set_inher.referenceId);
1321 req.smallSetUpperBound = &obj->set_inher.smallSetUpperBound;
1322 req.largeSetLowerBound = &obj->set_inher.largeSetLowerBound;
1323 req.mediumSetPresentNumber = &obj->set_inher.mediumSetPresentNumber;
1324 req.replaceIndicator = &obj->set_inher.replaceIndicator;
1325 req.resultSetName = obj->setName ? obj->setName : "Default";
1326 logf (LOG_DEBUG, "Search, resultSetName %s", req.resultSetName);
1327 req.num_databaseNames = obj->set_inher.num_databaseNames;
1328 req.databaseNames = obj->set_inher.databaseNames;
1329 for (r=0; r < obj->set_inher.num_databaseNames; r++)
1330 logf (LOG_DEBUG, " Database %s", obj->set_inher.databaseNames[r]);
1331 req.smallSetElementSetNames = 0;
1332 req.mediumSetElementSetNames = 0;
1333 req.preferredRecordSyntax = 0;
1336 if (!strcmp (obj->set_inher.queryType, "rpn"))
1338 Z_RPNQuery *RPNquery;
1340 RPNquery = p_query_rpn (p->odr_out, argv[2]);
1343 Tcl_AppendResult (interp, "Syntax error in query", NULL);
1346 RPNquery->attributeSetId = oid_getoidbyent (&bib1);
1347 query.which = Z_Query_type_1;
1348 query.u.type_1 = RPNquery;
1349 logf (LOG_DEBUG, "RPN");
1352 else if (!strcmp (obj->set_inher.queryType, "cclrpn"))
1356 struct ccl_rpn_node *rpn;
1357 Z_RPNQuery *RPNquery;
1359 rpn = ccl_find_str(p->bibset, argv[2], &error, &pos);
1362 Tcl_AppendResult (interp, "CCL error: ", ccl_err_msg(error),NULL);
1365 ccl_pr_tree (rpn, stderr);
1366 fprintf (stderr, "\n");
1367 assert((RPNquery = ccl_rpn_query(rpn)));
1368 RPNquery->attributeSetId = oid_getoidbyent (&bib1);
1369 query.which = Z_Query_type_1;
1370 query.u.type_1 = RPNquery;
1371 logf (LOG_DEBUG, "CCLRPN");
1374 else if (!strcmp (obj->set_inher.queryType, "ccl"))
1376 query.which = Z_Query_type_2;
1377 query.u.type_2 = &ccl_query;
1378 ccl_query.buf = (unsigned char *) argv[2];
1379 ccl_query.len = strlen (argv[2]);
1380 logf (LOG_DEBUG, "CCL");
1384 interp->result = "unknown query method";
1387 if (!z_APDU (p->odr_out, &apdup, 0))
1389 interp->result = odr_errlist [odr_geterror (p->odr_out)];
1390 odr_reset (p->odr_out);
1393 p->sbuf = odr_getbuf (p->odr_out, &p->slen, NULL);
1394 if ((r=cs_put (p->cs_link, p->sbuf, p->slen)) < 0)
1396 interp->result = "cs_put failed in search";
1401 ir_select_add_write (cs_fileno(p->cs_link), p);
1402 logf (LOG_DEBUG, "Sent part of searchRequest (%d bytes)", p->slen);
1406 logf (LOG_DEBUG, "Whole search request (%d bytes)", p->slen);
1412 * do_resultCount: Get number of hits
1414 static int do_resultCount (void *o, Tcl_Interp *interp,
1415 int argc, char **argv)
1421 return get_set_int (&obj->resultCount, interp, argc, argv);
1425 * do_searchStatus: Get search status (after search response)
1427 static int do_searchStatus (void *o, Tcl_Interp *interp,
1428 int argc, char **argv)
1434 return get_set_int (&obj->searchStatus, interp, argc, argv);
1438 * do_presentStatus: Get search status (after search/present response)
1440 static int do_presentStatus (void *o, Tcl_Interp *interp,
1441 int argc, char **argv)
1447 return get_set_int (&obj->presentStatus, interp, argc, argv);
1451 * do_nextResultSetPosition: Get next result set position
1452 * (after search/present response)
1454 static int do_nextResultSetPosition (void *o, Tcl_Interp *interp,
1455 int argc, char **argv)
1461 return get_set_int (&obj->nextResultSetPosition, interp, argc, argv);
1465 * do_setName: Set result Set name
1467 static int do_setName (void *o, Tcl_Interp *interp,
1468 int argc, char **argv)
1473 return ir_strdup (interp, &obj->setName, "Default");
1474 else if (argc == -1)
1475 return ir_strdel (interp, &obj->setName);
1478 free (obj->setName);
1479 if (ir_strdup (interp, &obj->setName, argv[2])
1483 Tcl_AppendElement (interp, obj->setName);
1488 * do_numberOfRecordsReturned: Get number of records returned
1490 static int do_numberOfRecordsReturned (void *o, Tcl_Interp *interp,
1491 int argc, char **argv)
1497 return get_set_int (&obj->numberOfRecordsReturned, interp, argc, argv);
1501 * do_recordType: Return record type (if any) at position.
1503 static int do_recordType (void *o, Tcl_Interp *interp, int argc, char **argv)
1511 obj->record_list = NULL;
1514 else if (argc == -1)
1516 delete_IR_records (obj);
1521 sprintf (interp->result, "wrong # args");
1524 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
1526 rl = find_IR_record (obj, offset);
1531 case Z_NamePlusRecord_databaseRecord:
1532 interp->result = "databaseRecord";
1534 case Z_NamePlusRecord_surrogateDiagnostic:
1535 interp->result = "surrogateDiagnostic";
1542 * do_recordDiag: Return diagnostic record info
1544 static int do_recordDiag (void *o, Tcl_Interp *interp, int argc, char **argv)
1555 sprintf (interp->result, "wrong # args");
1558 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
1560 rl = find_IR_record (obj, offset);
1563 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
1566 if (rl->which != Z_NamePlusRecord_surrogateDiagnostic)
1568 Tcl_AppendResult (interp, "No Diagnostic record at #", argv[2], NULL);
1571 sprintf (buf, "%d", rl->u.diag.condition);
1572 Tcl_AppendResult (interp, buf, " {",
1573 (rl->u.diag.addinfo ? rl->u.diag.addinfo : ""),
1579 * do_getMarc: Get ISO2709 Record lines/fields
1581 static int do_getMarc (void *o, Tcl_Interp *interp, int argc, char **argv)
1591 sprintf (interp->result, "wrong # args");
1594 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
1596 rl = find_IR_record (obj, offset);
1599 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
1602 if (rl->which != Z_NamePlusRecord_databaseRecord)
1604 Tcl_AppendResult (interp, "No MARC record at #", argv[2], NULL);
1607 return ir_tcl_get_marc (interp, rl->u.dbrec.buf, argc, argv);
1612 * do_responseStatus: Return response status (present or search)
1614 static int do_responseStatus (void *o, Tcl_Interp *interp,
1615 int argc, char **argv)
1621 obj->recordFlag = 0;
1622 obj->addinfo = NULL;
1625 else if (argc == -1)
1626 return ir_strdel (interp, &obj->addinfo);
1627 if (!obj->recordFlag)
1629 Tcl_AppendElement (interp, "OK");
1634 case Z_Records_DBOSD:
1635 Tcl_AppendElement (interp, "DBOSD");
1638 return mk_nonSurrogateDiagnostics (interp, obj->condition,
1645 * do_present: Perform Present Request
1648 static int do_present (void *o, Tcl_Interp *interp,
1649 int argc, char **argv)
1652 IRObj *p = obj->parent;
1653 Z_APDU apdu, *apdup = &apdu;
1654 Z_PresentRequest req;
1663 if (Tcl_GetInt (interp, argv[2], &start) == TCL_ERROR)
1670 if (Tcl_GetInt (interp, argv[3], &number) == TCL_ERROR)
1677 interp->result = "not connected";
1680 odr_reset (p->odr_out);
1682 obj->number = number;
1684 apdu.which = Z_APDU_presentRequest;
1685 apdu.u.presentRequest = &req;
1687 set_referenceId (p->odr_out, &req.referenceId, obj->set_inher.referenceId);
1689 req.resultSetId = obj->setName ? obj->setName : "Default";
1691 req.resultSetStartPoint = &start;
1692 req.numberOfRecordsRequested = &number;
1693 req.elementSetNames = 0;
1694 req.preferredRecordSyntax = 0;
1696 if (!z_APDU (p->odr_out, &apdup, 0))
1698 interp->result = odr_errlist [odr_geterror (p->odr_out)];
1699 odr_reset (p->odr_out);
1702 p->sbuf = odr_getbuf (p->odr_out, &p->slen, NULL);
1703 if ((r=cs_put (p->cs_link, p->sbuf, p->slen)) < 0)
1705 interp->result = "cs_put failed in present";
1710 ir_select_add_write (cs_fileno(p->cs_link), p);
1711 logf (LOG_DEBUG, "Part of present request, start=%d, num=%d"
1712 " (%d bytes)", start, number, p->slen);
1716 logf (LOG_DEBUG, "Whole present request, start=%d, num=%d"
1717 " (%d bytes)", start, number, p->slen);
1723 * do_loadFile: Load result set from file
1726 static int do_loadFile (void *o, Tcl_Interp *interp,
1727 int argc, char **argv)
1729 IRSetObj *setobj = o;
1739 interp->result = "wrong # args";
1742 inf = fopen (argv[2], "r");
1745 Tcl_AppendResult (interp, "Cannot open ", argv[2], NULL);
1748 while ((buf = ir_tcl_fread_marc (inf, &size)))
1752 rl = new_IR_record (setobj, no, Z_NamePlusRecord_databaseRecord);
1753 rl->u.dbrec.buf = buf;
1754 rl->u.dbrec.size = size;
1757 setobj->numberOfRecordsReturned = no-1;
1762 static IRMethod ir_set_method_tab[] = {
1763 { 0, "search", do_search },
1764 { 0, "searchStatus", do_searchStatus },
1765 { 0, "presentStatus", do_presentStatus },
1766 { 0, "nextResultSetPosition", do_nextResultSetPosition },
1767 { 0, "setName", do_setName },
1768 { 0, "resultCount", do_resultCount },
1769 { 0, "numberOfRecordsReturned", do_numberOfRecordsReturned },
1770 { 0, "present", do_present },
1771 { 0, "recordType", do_recordType },
1772 { 0, "getMarc", do_getMarc },
1773 { 0, "Diag", do_recordDiag },
1774 { 0, "responseStatus", do_responseStatus },
1775 { 0, "loadFile", do_loadFile },
1780 * ir_set_obj_method: IR Set Object methods
1782 static int ir_set_obj_method (ClientData clientData, Tcl_Interp *interp,
1783 int argc, char **argv)
1786 IRSetObj *p = clientData;
1790 interp->result = "wrong # args";
1793 tabs[0].tab = ir_set_method_tab;
1795 tabs[1].tab = ir_set_c_method_tab;
1796 tabs[1].obj = &p->set_inher;
1799 return ir_method (interp, argc, argv, tabs);
1803 * ir_set_obj_delete: IR Set Object disposal
1805 static void ir_set_obj_delete (ClientData clientData)
1808 IRSetObj *p = clientData;
1810 tabs[0].tab = ir_set_method_tab;
1812 tabs[1].tab = ir_set_c_method_tab;
1813 tabs[1].obj = &p->set_inher;
1816 ir_method (NULL, -1, NULL, tabs);
1822 * ir_set_obj_mk: IR Set Object creation
1824 static int ir_set_obj_mk (ClientData clientData, Tcl_Interp *interp,
1825 int argc, char **argv)
1830 if (argc < 2 || argc > 3)
1832 interp->result = "wrong # args";
1835 if (!(obj = ir_malloc (interp, sizeof(*obj))))
1839 Tcl_CmdInfo parent_info;
1844 if (!Tcl_GetCommandInfo (interp, argv[2], &parent_info))
1846 interp->result = "No parent";
1849 obj->parent = (IRObj *) parent_info.clientData;
1851 dst = &obj->set_inher;
1852 src = &obj->parent->set_inher;
1854 dst->num_databaseNames = src->num_databaseNames;
1855 if (!(dst->databaseNames =
1856 ir_malloc (interp, sizeof (*dst->databaseNames)
1857 * dst->num_databaseNames)))
1859 for (i = 0; i < dst->num_databaseNames; i++)
1861 if (ir_strdup (interp, &dst->databaseNames[i],
1862 src->databaseNames[i]) == TCL_ERROR)
1865 if (ir_strdup (interp, &dst->queryType, src->queryType)
1869 if (ir_strdup (interp, &dst->referenceId, src->referenceId)
1873 dst->replaceIndicator = src->replaceIndicator;
1874 dst->smallSetUpperBound = src->smallSetUpperBound;
1875 dst->largeSetLowerBound = src->largeSetLowerBound;
1876 dst->mediumSetPresentNumber = src->mediumSetPresentNumber;
1881 tabs[0].tab = ir_set_method_tab;
1885 if (ir_method (interp, 0, NULL, tabs) == TCL_ERROR)
1888 Tcl_CreateCommand (interp, argv[1], ir_set_obj_method,
1889 (ClientData) obj, ir_set_obj_delete);
1893 /* ------------------------------------------------------- */
1896 * do_scan: Perform scan
1898 static int do_scan (void *o, Tcl_Interp *interp, int argc, char **argv)
1901 Z_APDU apdu, *apdup = &apdu;
1903 IRObj *p = obj->parent;
1907 struct ccl_rpn_node *rpn;
1916 interp->result = "wrong # args";
1919 if (!p->set_inher.num_databaseNames)
1921 interp->result = "no databaseNames";
1926 interp->result = "not connected";
1929 odr_reset (p->odr_out);
1931 bib1.proto = PROTO_Z3950;
1932 bib1.class = CLASS_ATTSET;
1933 bib1.value = VAL_BIB1;
1935 apdu.which = Z_APDU_scanRequest;
1936 apdu.u.scanRequest = &req;
1937 set_referenceId (p->odr_out, &req.referenceId, p->set_inher.referenceId);
1938 req.num_databaseNames = p->set_inher.num_databaseNames;
1939 req.databaseNames = p->set_inher.databaseNames;
1940 req.attributeSet = oid_getoidbyent (&bib1);
1943 if (!(req.termListAndStartPoint = p_query_scan (p->odr_out, argv[2])))
1945 Tcl_AppendResult (interp, "Syntax error in query", NULL);
1949 rpn = ccl_find_str(p->bibset, argv[2], &r, &pos);
1952 Tcl_AppendResult (interp, "CCL error: ", ccl_err_msg (r), NULL);
1955 ccl_pr_tree (rpn, stderr);
1956 fprintf (stderr, "\n");
1957 if (!(req.termListAndStartPoint = ccl_scan_query (rpn)))
1960 req.stepSize = &obj->stepSize;
1961 req.numberOfTermsRequested = &obj->numberOfTermsRequested;
1962 req.preferredPositionInResponse = &obj->preferredPositionInResponse;
1963 logf (LOG_DEBUG, "stepSize=%d", *req.stepSize);
1964 logf (LOG_DEBUG, "numberOfTermsRequested=%d",
1965 *req.numberOfTermsRequested);
1966 logf (LOG_DEBUG, "preferredPositionInResponse=%d",
1967 *req.preferredPositionInResponse);
1969 if (!z_APDU (p->odr_out, &apdup, 0))
1971 interp->result = odr_errlist [odr_geterror (p->odr_out)];
1972 odr_reset (p->odr_out);
1975 p->sbuf = odr_getbuf (p->odr_out, &p->slen, NULL);
1976 if ((r=cs_put (p->cs_link, p->sbuf, p->slen)) < 0)
1978 interp->result = "cs_put failed in scan";
1983 ir_select_add_write (cs_fileno(p->cs_link), p);
1984 logf (LOG_DEBUG, "Sent part of scanRequest (%d bytes)", p->slen);
1988 logf (LOG_DEBUG, "Whole scan request (%d bytes)", p->slen);
1994 * do_stepSize: Set/get replace Step Size
1996 static int do_stepSize (void *obj, Tcl_Interp *interp,
1997 int argc, char **argv)
2005 return get_set_int (&p->stepSize, interp, argc, argv);
2009 * do_numberOfTermsRequested: Set/get Number of Terms requested
2011 static int do_numberOfTermsRequested (void *obj, Tcl_Interp *interp,
2012 int argc, char **argv)
2018 p->numberOfTermsRequested = 20;
2021 return get_set_int (&p->numberOfTermsRequested, interp, argc, argv);
2026 * do_preferredPositionInResponse: Set/get preferred Position
2028 static int do_preferredPositionInResponse (void *obj, Tcl_Interp *interp,
2029 int argc, char **argv)
2035 p->preferredPositionInResponse = 1;
2038 return get_set_int (&p->preferredPositionInResponse, interp, argc, argv);
2042 * do_scanStatus: Get scan status
2044 static int do_scanStatus (void *obj, Tcl_Interp *interp,
2045 int argc, char **argv)
2051 return get_set_int (&p->scanStatus, interp, argc, argv);
2055 * do_numberOfEntriesReturned: Get number of Entries returned
2057 static int do_numberOfEntriesReturned (void *obj, Tcl_Interp *interp,
2058 int argc, char **argv)
2064 return get_set_int (&p->numberOfEntriesReturned, interp, argc, argv);
2068 * do_positionOfTerm: Get position of Term
2070 static int do_positionOfTerm (void *obj, Tcl_Interp *interp,
2071 int argc, char **argv)
2077 return get_set_int (&p->positionOfTerm, interp, argc, argv);
2081 * do_scanLine: get Scan Line (surrogate or normal) after response
2083 static int do_scanLine (void *obj, Tcl_Interp *interp, int argc, char **argv)
2091 p->entries_flag = 0;
2093 p->nonSurrogateDiagnostics = NULL;
2096 else if (argc == -1)
2098 p->entries_flag = 0;
2099 /* release entries */
2101 /* release non diagnostics */
2102 p->nonSurrogateDiagnostics = NULL;
2107 interp->result = "wrong # args";
2110 if (Tcl_GetInt (interp, argv[2], &i) == TCL_ERROR)
2112 if (!p->entries_flag || p->which != Z_ListEntries_entries || !p->entries
2113 || i >= p->num_entries || i < 0)
2115 switch (p->entries[i].which)
2117 case Z_Entry_termInfo:
2118 Tcl_AppendElement (interp, "T");
2119 if (p->entries[i].u.term.buf)
2120 Tcl_AppendElement (interp, p->entries[i].u.term.buf);
2122 Tcl_AppendElement (interp, "");
2123 sprintf (numstr, "%d", p->entries[i].u.term.globalOccurrences);
2124 Tcl_AppendElement (interp, numstr);
2126 case Z_Entry_surrogateDiagnostic:
2128 mk_nonSurrogateDiagnostics (interp, p->entries[i].u.diag.condition,
2129 p->entries[i].u.diag.addinfo);
2135 static IRMethod ir_scan_method_tab[] = {
2136 { 0, "scan", do_scan },
2137 { 0, "stepSize", do_stepSize },
2138 { 0, "numberOfTermsRequested", do_numberOfTermsRequested },
2139 { 0, "preferredPositionInResponse", do_preferredPositionInResponse },
2140 { 0, "scanStatus", do_scanStatus },
2141 { 0, "numberOfEntriesReturned", do_numberOfEntriesReturned },
2142 { 0, "positionOfTerm", do_positionOfTerm },
2143 { 0, "scanLine", do_scanLine },
2148 * ir_scan_obj_method: IR Scan Object methods
2150 static int ir_scan_obj_method (ClientData clientData, Tcl_Interp *interp,
2151 int argc, char **argv)
2157 interp->result = "wrong # args";
2160 tabs[0].tab = ir_scan_method_tab;
2161 tabs[0].obj = clientData;
2164 return ir_method (interp, argc, argv, tabs);
2168 * ir_scan_obj_delete: IR Scan Object disposal
2170 static void ir_scan_obj_delete (ClientData clientData)
2173 IRScanObj *obj = clientData;
2175 tabs[0].tab = ir_scan_method_tab;
2179 ir_method (NULL, -1, NULL, tabs);
2184 * ir_scan_obj_mk: IR Scan Object creation
2186 static int ir_scan_obj_mk (ClientData clientData, Tcl_Interp *interp,
2187 int argc, char **argv)
2189 Tcl_CmdInfo parent_info;
2195 interp->result = "wrong # args";
2198 if (!Tcl_GetCommandInfo (interp, argv[2], &parent_info))
2200 interp->result = "No parent";
2203 if (!(obj = ir_malloc (interp, sizeof(*obj))))
2206 obj->parent = (IRObj *) parent_info.clientData;
2208 tabs[0].tab = ir_scan_method_tab;
2212 if (ir_method (interp, 0, NULL, tabs) == TCL_ERROR)
2214 Tcl_CreateCommand (interp, argv[1], ir_scan_obj_method,
2215 (ClientData) obj, ir_scan_obj_delete);
2219 /* ------------------------------------------------------- */
2221 static void ir_initResponse (void *obj, Z_InitResponse *initrs)
2225 p->initResult = *initrs->result ? 1 : 0;
2226 if (!*initrs->result)
2227 logf (LOG_DEBUG, "Connection rejected by target");
2229 logf (LOG_DEBUG, "Connection accepted by target");
2231 free (p->targetImplementationId);
2232 ir_strdup (p->interp, &p->targetImplementationId,
2233 initrs->implementationId);
2234 free (p->targetImplementationName);
2235 ir_strdup (p->interp, &p->targetImplementationName,
2236 initrs->implementationName);
2237 free (p->targetImplementationVersion);
2238 ir_strdup (p->interp, &p->targetImplementationVersion,
2239 initrs->implementationVersion);
2241 p->maximumRecordSize = *initrs->maximumRecordSize;
2242 p->preferredMessageSize = *initrs->preferredMessageSize;
2244 memcpy (&p->options, initrs->options, sizeof(initrs->options));
2245 memcpy (&p->protocolVersion, initrs->protocolVersion,
2246 sizeof(initrs->protocolVersion));
2247 free (p->userInformationField);
2248 p->userInformationField = NULL;
2249 if (initrs->userInformationField)
2253 if (initrs->userInformationField->which == ODR_EXTERNAL_octet &&
2254 (p->userInformationField =
2256 initrs->userInformationField->u.octet_aligned->len)
2259 memcpy (p->userInformationField,
2260 initrs->userInformationField->u.octet_aligned->buf,
2262 (p->userInformationField)[len] = '\0';
2267 static void ir_handleRecords (void *o, Z_Records *zrs)
2270 IRSetObj *setobj = p->set_child;
2272 setobj->which = zrs->which;
2273 setobj->recordFlag = 1;
2274 if (zrs->which == Z_Records_NSD)
2276 const char *addinfo;
2278 setobj->numberOfRecordsReturned = 0;
2279 setobj->condition = *zrs->u.nonSurrogateDiagnostic->condition;
2280 free (setobj->addinfo);
2281 setobj->addinfo = NULL;
2282 addinfo = zrs->u.nonSurrogateDiagnostic->addinfo;
2283 if (addinfo && (setobj->addinfo = malloc (strlen(addinfo) + 1)))
2284 strcpy (setobj->addinfo, addinfo);
2285 logf (LOG_DEBUG, "Diagnostic response. %s (%d): %s",
2286 diagbib1_str (setobj->condition),
2288 setobj->addinfo ? setobj->addinfo : "");
2295 setobj->numberOfRecordsReturned =
2296 zrs->u.databaseOrSurDiagnostics->num_records;
2297 logf (LOG_DEBUG, "Got %d records", setobj->numberOfRecordsReturned);
2298 for (offset = 0; offset<setobj->numberOfRecordsReturned; offset++)
2300 rl = new_IR_record (setobj, setobj->start + offset,
2301 zrs->u.databaseOrSurDiagnostics->
2302 records[offset]->which);
2303 if (rl->which == Z_NamePlusRecord_surrogateDiagnostic)
2307 diagrec = zrs->u.databaseOrSurDiagnostics->
2308 records[offset]->u.surrogateDiagnostic;
2310 rl->u.diag.condition = *diagrec->condition;
2311 if (diagrec->addinfo && (rl->u.diag.addinfo =
2312 malloc (strlen (diagrec->addinfo)+1)))
2313 strcpy (rl->u.diag.addinfo, diagrec->addinfo);
2317 Z_DatabaseRecord *zr;
2320 zr = zrs->u.databaseOrSurDiagnostics->records[offset]
2322 oe = (Odr_external*) zr;
2323 rl->u.dbrec.size = zr->u.octet_aligned->len;
2324 if (oe->which == ODR_EXTERNAL_octet && rl->u.dbrec.size > 0)
2326 const char *buf = (char*) zr->u.octet_aligned->buf;
2327 if ((rl->u.dbrec.buf = malloc (rl->u.dbrec.size)))
2328 memcpy (rl->u.dbrec.buf, buf, rl->u.dbrec.size);
2331 rl->u.dbrec.buf = NULL;
2337 static void ir_searchResponse (void *o, Z_SearchResponse *searchrs)
2340 IRSetObj *setobj = p->set_child;
2341 Z_Records *zrs = searchrs->records;
2345 setobj->searchStatus = searchrs->searchStatus ? 1 : 0;
2346 setobj->resultCount = *searchrs->resultCount;
2347 if (searchrs->presentStatus)
2348 setobj->presentStatus = *searchrs->presentStatus;
2349 if (searchrs->nextResultSetPosition)
2350 setobj->nextResultSetPosition = *searchrs->nextResultSetPosition;
2352 logf (LOG_DEBUG, "Search response %d, %d hits",
2353 setobj->searchStatus, setobj->resultCount);
2355 ir_handleRecords (o, zrs);
2357 setobj->recordFlag = 0;
2360 logf (LOG_DEBUG, "Search response, no object!");
2364 static void ir_presentResponse (void *o, Z_PresentResponse *presrs)
2367 IRSetObj *setobj = p->set_child;
2368 Z_Records *zrs = presrs->records;
2370 logf (LOG_DEBUG, "Received presentResponse");
2371 setobj->presentStatus = *presrs->presentStatus;
2372 setobj->nextResultSetPosition = *presrs->nextResultSetPosition;
2374 ir_handleRecords (o, zrs);
2377 setobj->recordFlag = 0;
2378 logf (LOG_DEBUG, "No records!");
2382 static void ir_scanResponse (void *o, Z_ScanResponse *scanrs)
2385 IRScanObj *scanobj = p->scan_child;
2387 logf (LOG_DEBUG, "Received scanResponse");
2389 scanobj->scanStatus = *scanrs->scanStatus;
2390 logf (LOG_DEBUG, "scanStatus=%d", scanobj->scanStatus);
2392 if (scanrs->stepSize)
2393 scanobj->stepSize = *scanrs->stepSize;
2394 logf (LOG_DEBUG, "stepSize=%d", scanobj->stepSize);
2396 scanobj->numberOfEntriesReturned = *scanrs->numberOfEntriesReturned;
2397 logf (LOG_DEBUG, "numberOfEntriesReturned=%d",
2398 scanobj->numberOfEntriesReturned);
2400 if (scanrs->positionOfTerm)
2401 scanobj->positionOfTerm = *scanrs->positionOfTerm;
2403 scanobj->positionOfTerm = -1;
2404 logf (LOG_DEBUG, "positionOfTerm=%d", scanobj->positionOfTerm);
2406 free (scanobj->entries);
2407 scanobj->entries = NULL;
2408 free (scanobj->nonSurrogateDiagnostics);
2409 scanobj->nonSurrogateDiagnostics = NULL;
2411 if (scanrs->entries)
2416 scanobj->entries_flag = 1;
2417 scanobj->which = scanrs->entries->which;
2418 switch (scanobj->which)
2420 case Z_ListEntries_entries:
2421 scanobj->num_entries = scanrs->entries->u.entries->num_entries;
2422 scanobj->entries = malloc (scanobj->num_entries *
2423 sizeof(*scanobj->entries));
2424 for (i=0; i<scanobj->num_entries; i++)
2426 ze = scanrs->entries->u.entries->entries[i];
2427 scanobj->entries[i].which = ze->which;
2430 case Z_Entry_termInfo:
2431 if (ze->u.termInfo->term->which == Z_Term_general)
2433 int l = ze->u.termInfo->term->u.general->len;
2434 scanobj->entries[i].u.term.buf = malloc (1+l);
2435 memcpy (scanobj->entries[i].u.term.buf,
2436 ze->u.termInfo->term->u.general->buf,
2438 scanobj->entries[i].u.term.buf[l] = '\0';
2441 scanobj->entries[i].u.term.buf = NULL;
2442 if (ze->u.termInfo->globalOccurrences)
2443 scanobj->entries[i].u.term.globalOccurrences =
2444 *ze->u.termInfo->globalOccurrences;
2446 scanobj->entries[i].u.term.globalOccurrences = 0;
2448 case Z_Entry_surrogateDiagnostic:
2449 scanobj->entries[i].u.diag.addinfo =
2450 malloc (1+strlen(ze->u.surrogateDiagnostic->
2452 strcpy (scanobj->entries[i].u.diag.addinfo,
2453 ze->u.surrogateDiagnostic->addinfo);
2454 scanobj->entries[i].u.diag.condition =
2455 *ze->u.surrogateDiagnostic->condition;
2460 case Z_ListEntries_nonSurrogateDiagnostics:
2461 scanobj->num_diagRecs = scanrs->entries->
2462 u.nonSurrogateDiagnostics->num_diagRecs;
2463 scanobj->nonSurrogateDiagnostics = malloc (scanobj->num_diagRecs *
2464 sizeof(*scanobj->nonSurrogateDiagnostics));
2469 scanobj->entries_flag = 0;
2473 * ir_select_read: handle incoming packages
2475 void ir_select_read (ClientData clientData)
2477 IRObj *p = clientData;
2483 r = cs_rcvconnect (p->cs_link);
2487 ir_select_remove_write (cs_fileno (p->cs_link), p);
2490 logf (LOG_DEBUG, "cs_rcvconnect error");
2492 Tcl_Eval (p->interp, p->failback);
2493 do_disconnect (p, NULL, 2, NULL);
2497 Tcl_Eval (p->interp, p->callback);
2502 /* signal one more use of ir object - callbacks must not
2503 release the ir memory (p pointer) */
2505 if ((r=cs_get (p->cs_link, &p->buf_in, &p->len_in)) <= 0)
2507 logf (LOG_DEBUG, "cs_get failed, code %d", r);
2508 ir_select_remove (cs_fileno (p->cs_link), p);
2510 Tcl_Eval (p->interp, p->failback);
2511 do_disconnect (p, NULL, 2, NULL);
2513 /* relase ir object now if callback deleted it */
2519 odr_setbuf (p->odr_in, p->buf_in, r, 0);
2520 logf (LOG_DEBUG, "cs_get ok, got %d", r);
2521 if (!z_APDU (p->odr_in, &apdu, 0))
2523 logf (LOG_DEBUG, "%s", odr_errlist [odr_geterror (p->odr_in)]);
2525 Tcl_Eval (p->interp, p->failback);
2526 do_disconnect (p, NULL, 2, NULL);
2528 /* relase ir object now if callback deleted it */
2534 case Z_APDU_initResponse:
2535 ir_initResponse (p, apdu->u.initResponse);
2537 case Z_APDU_searchResponse:
2538 ir_searchResponse (p, apdu->u.searchResponse);
2540 case Z_APDU_presentResponse:
2541 ir_presentResponse (p, apdu->u.presentResponse);
2543 case Z_APDU_scanResponse:
2544 ir_scanResponse (p, apdu->u.scanResponse);
2547 logf (LOG_WARN, "Received unknown APDU type (%d)", apdu->which);
2549 Tcl_Eval (p->interp, p->failback);
2550 do_disconnect (p, NULL, 2, NULL);
2552 odr_reset (p->odr_in);
2554 Tcl_Eval (p->interp, p->callback);
2555 if (p->ref_count == 1)
2561 } while (p->cs_link && cs_more (p->cs_link));
2565 * ir_select_write: handle outgoing packages - not yet written.
2567 void ir_select_write (ClientData clientData)
2569 IRObj *p = clientData;
2572 logf (LOG_DEBUG, "In write handler");
2575 r = cs_rcvconnect (p->cs_link);
2581 logf (LOG_DEBUG, "cs_rcvconnect error");
2582 ir_select_remove_write (cs_fileno (p->cs_link), p);
2584 Tcl_Eval (p->interp, p->failback);
2585 do_disconnect (p, NULL, 2, NULL);
2588 ir_select_remove_write (cs_fileno (p->cs_link), p);
2590 Tcl_Eval (p->interp, p->callback);
2593 if ((r=cs_put (p->cs_link, p->sbuf, p->slen)) < 0)
2595 logf (LOG_DEBUG, "select write fail");
2597 Tcl_Eval (p->interp, p->failback);
2598 do_disconnect (p, NULL, 2, NULL);
2600 else if (r == 0) /* remove select bit */
2602 ir_select_remove_write (cs_fileno (p->cs_link), p);
2606 /* ------------------------------------------------------- */
2609 * ir_tcl_init: Registration of TCL commands.
2611 int ir_tcl_init (Tcl_Interp *interp)
2613 Tcl_CreateCommand (interp, "ir", ir_obj_mk, (ClientData) NULL,
2614 (Tcl_CmdDeleteProc *) NULL);
2615 Tcl_CreateCommand (interp, "ir-set", ir_set_obj_mk,
2616 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
2617 Tcl_CreateCommand (interp, "ir-scan", ir_scan_obj_mk,
2618 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);