2 * IR toolkit for tcl/tk
4 * See the file LICENSE for details.
5 * Sebastian Hammer, Adam Dickmeiss
8 * Revision 1.34 1995-05-29 10:33:42 adam
9 * README and rename of startup script.
11 * Revision 1.33 1995/05/29 09:15:11 quinn
12 * Changed CS_SR to PROTO_SR, etc.
14 * Revision 1.32 1995/05/29 08:44:16 adam
15 * Work on delete of objects.
17 * Revision 1.31 1995/05/26 11:44:10 adam
18 * Bugs fixed. More work on MARC utilities and queries. Test
19 * client is up-to-date again.
21 * Revision 1.30 1995/05/26 08:54:11 adam
22 * New MARC utilities. Uses prefix query.
24 * Revision 1.29 1995/05/24 14:10:22 adam
25 * Work on idAuthentication, protocolVersion and options.
27 * Revision 1.28 1995/05/23 15:34:48 adam
28 * Many new settings, userInformationField, smallSetUpperBound, etc.
29 * A number of settings are inherited when ir-set is executed.
30 * This version is incompatible with the graphical test client (client.tcl).
32 * Revision 1.27 1995/05/11 15:34:47 adam
33 * Scan request changed a bit. This version works with RLG.
35 * Revision 1.26 1995/04/18 16:11:51 adam
36 * First version of graphical Scan. Some work on query-by-form.
38 * Revision 1.25 1995/04/17 09:37:17 adam
39 * Further development of scan.
41 * Revision 1.24 1995/04/11 14:16:42 adam
42 * Further work on scan. Response works. Entries aren't saved yet.
44 * Revision 1.23 1995/04/10 10:50:27 adam
45 * Result-set name defaults to suffix of ir-set name.
46 * Started working on scan. Not finished at this point.
48 * Revision 1.22 1995/03/31 10:43:03 adam
49 * More robust when getting bad MARC records.
51 * Revision 1.21 1995/03/31 08:56:37 adam
52 * New button "Search".
54 * Revision 1.20 1995/03/29 16:07:09 adam
55 * Bug fix: Didn't use setName in present request.
57 * Revision 1.19 1995/03/28 12:45:23 adam
58 * New ir method failback: called on disconnect/protocol error.
59 * New ir set/get method: protocol: SR / Z3950.
60 * Simple popup and disconnect when failback is invoked.
62 * Revision 1.18 1995/03/21 15:50:12 adam
65 * Revision 1.17 1995/03/21 13:41:03 adam
66 * Comstack cs_create not used too often. Non-blocking connect.
68 * Revision 1.16 1995/03/21 08:26:06 adam
69 * New method, setName, to specify the result set name (other than Default).
70 * New method, responseStatus, which returns diagnostic info, if any, after
71 * present response / search response.
73 * Revision 1.15 1995/03/20 15:24:07 adam
74 * Diagnostic records saved on searchResponse.
76 * Revision 1.14 1995/03/20 08:53:22 adam
77 * Event loop in tclmain.c rewritten. New method searchStatus.
79 * Revision 1.13 1995/03/17 18:26:17 adam
80 * Non-blocking i/o used now. Database names popup as cascade items.
82 * Revision 1.12 1995/03/17 15:45:00 adam
83 * Improved target/database setup.
85 * Revision 1.11 1995/03/16 17:54:03 adam
86 * Minor changes really.
88 * Revision 1.10 1995/03/15 16:14:50 adam
89 * Blocking arg in cs_create changed.
91 * Revision 1.9 1995/03/15 13:59:24 adam
94 * Revision 1.8 1995/03/15 08:25:16 adam
95 * New method presentStatus to check for error on present. Misc. cleanup
96 * of IRRecordList manipulations. Full MARC record presentation in
99 * Revision 1.7 1995/03/14 17:32:29 adam
100 * Presentation of full Marc record in popup window.
102 * Revision 1.6 1995/03/12 19:31:55 adam
103 * Pattern matching implemented when retrieving MARC records. More
104 * diagnostic functions.
106 * Revision 1.5 1995/03/10 18:00:15 adam
107 * Actual presentation in line-by-line format. RPN query support.
109 * Revision 1.4 1995/03/09 16:15:08 adam
110 * First presentRequest attempts. Hot-target list.
116 #include <sys/time.h>
126 int (*method) (void *obj, Tcl_Interp *interp, int argc, char **argv);
134 static int do_disconnect (void *obj,Tcl_Interp *interp, int argc, char **argv);
136 static IRRecordList *new_IR_record (IRSetObj *setobj, int no, int which)
140 for (rl = setobj->record_list; rl; rl = rl->next)
146 case Z_NamePlusRecord_databaseRecord:
147 free (rl->u.dbrec.buf);
148 rl->u.dbrec.buf = NULL;
150 case Z_NamePlusRecord_surrogateDiagnostic:
151 free (rl->u.diag.addinfo);
152 rl->u.diag.addinfo = NULL;
160 rl = malloc (sizeof(*rl));
162 rl->next = setobj->record_list;
164 setobj->record_list = rl;
170 static IRRecordList *find_IR_record (IRSetObj *setobj, int no)
174 for (rl = setobj->record_list; rl; rl = rl->next)
180 static void delete_IR_records (IRSetObj *setobj)
182 IRRecordList *rl, *rl1;
184 for (rl = setobj->record_list; rl; rl = rl1)
188 case Z_NamePlusRecord_databaseRecord:
189 free (rl->u.dbrec.buf);
191 case Z_NamePlusRecord_surrogateDiagnostic:
192 free (rl->u.diag.addinfo);
198 setobj->record_list = NULL;
202 * getsetint: Set/get integer value
204 static int get_set_int (int *val, Tcl_Interp *interp, int argc, char **argv)
210 if (Tcl_GetInt (interp, argv[2], val)==TCL_ERROR)
213 sprintf (buf, "%d", *val);
214 Tcl_AppendResult (interp, buf, NULL);
219 * mk_nonSurrogateDiagnostics: Make Tcl result with diagnostic info
221 static int mk_nonSurrogateDiagnostics (Tcl_Interp *interp,
228 Tcl_AppendElement (interp, "NSD");
229 sprintf (buf, "%d", condition);
230 Tcl_AppendElement (interp, buf);
231 cp = diagbib1_str (condition);
233 Tcl_AppendElement (interp, (char*) cp);
235 Tcl_AppendElement (interp, "");
237 Tcl_AppendElement (interp, (char*) addinfo);
239 Tcl_AppendElement (interp, "");
244 * get_parent_info: Returns information about parent object.
246 static int get_parent_info (Tcl_Interp *interp, const char *name,
247 Tcl_CmdInfo *parent_info,
250 char parent_name[128];
251 const char *csep = strrchr (name, '.');
256 interp->result = "missing .";
264 memcpy (parent_name, name, pos);
265 parent_name[pos] = '\0';
266 if (!Tcl_GetCommandInfo (interp, parent_name, parent_info))
268 interp->result = "No parent";
275 * ir_method: Search for method in table and invoke method handler
277 int ir_method (Tcl_Interp *interp, int argc, char **argv, IRMethods *tab)
279 IRMethods *tab_i = tab;
282 for (tab_i = tab; tab_i->tab; tab_i++)
283 for (t = tab_i->tab; t->name; t++)
286 if ((*t->method)(tab_i->obj, interp, argc, argv) == TCL_ERROR)
290 if (!strcmp (t->name, argv[1]))
291 return (*t->method)(tab_i->obj, interp, argc, argv);
295 Tcl_AppendResult (interp, "Bad method. Possible methods:", NULL);
296 for (tab_i = tab; tab_i->tab; tab_i++)
297 for (t = tab_i->tab; t->name; t++)
298 Tcl_AppendResult (interp, " ", t->name, NULL);
303 * ir_method_r: Get status for all readable elements
305 int ir_method_r (void *obj, Tcl_Interp *interp, int argc, char **argv,
313 for (; tab->name; tab++)
316 argv_n[1] = tab->name;
317 Tcl_AppendResult (interp, "{", NULL);
318 (*tab->method)(obj, interp, argc_n, argv_n);
319 Tcl_AppendResult (interp, "} ", NULL);
325 * ir_named_bits: get/set named bits
327 int ir_named_bits (struct ir_named_entry *tab, Odr_bitmask *ob,
328 Tcl_Interp *interp, int argc, char **argv)
330 struct ir_named_entry *ti;
335 for (no = 0; no < argc; no++)
337 for (ti = tab; ti->name; ti++)
338 if (!strcmp (argv[no], ti->name))
340 ODR_MASK_SET (ob, ti->pos);
345 Tcl_AppendResult (interp, "Bad bit mask: ", argv[no], NULL);
351 for (ti = tab; ti->name; ti++)
352 if (ODR_MASK_GET (ob, ti->pos))
353 Tcl_AppendElement (interp, ti->name);
358 * ir_strdup: Duplicate string
360 int ir_strdup (Tcl_Interp *interp, char** p, const char *s)
362 *p = malloc (strlen(s)+1);
365 interp->result = "strdup fail";
373 * ir_strdel: Delete string
375 int ir_strdel (Tcl_Interp *interp, char **p)
383 * ir_malloc: Malloc function
385 void *ir_malloc (Tcl_Interp *interp, size_t size)
387 static char buf[128];
388 void *p = malloc (size);
392 sprintf (buf, "Malloc fail. %ld bytes requested", (long) size);
393 interp->result = buf;
399 /* ------------------------------------------------------- */
402 * do_init_request: init method on IR object
404 static int do_init_request (void *obj, Tcl_Interp *interp,
405 int argc, char **argv)
407 Z_APDU apdu, *apdup = &apdu;
416 interp->result = "not connected";
419 odr_reset (p->odr_out);
422 req.options = &p->options;
423 req.protocolVersion = &p->protocolVersion;
424 req.preferredMessageSize = &p->preferredMessageSize;
425 req.maximumRecordSize = &p->maximumRecordSize;
427 if (p->idAuthenticationGroupId)
429 Z_IdPass *pass = odr_malloc (p->odr_out, sizeof(*pass));
430 Z_IdAuthentication *auth = odr_malloc (p->odr_out, sizeof(*auth));
432 auth->which = Z_IdAuthentication_idPass;
433 auth->u.idPass = pass;
434 if (p->idAuthenticationGroupId && *p->idAuthenticationGroupId)
435 pass->groupId = p->idAuthenticationGroupId;
437 pass->groupId = NULL;
438 if (p->idAuthenticationUserId && *p->idAuthenticationUserId)
439 pass->userId = p->idAuthenticationUserId;
442 if (p->idAuthenticationPassword && *p->idAuthenticationPassword)
443 pass->password = p->idAuthenticationPassword;
445 pass->password = NULL;
446 req.idAuthentication = auth;
448 else if (!p->idAuthenticationOpen || !*p->idAuthenticationOpen)
449 req.idAuthentication = NULL;
452 Z_IdAuthentication *auth = odr_malloc (p->odr_out, sizeof(*auth));
454 auth->which = Z_IdAuthentication_open;
455 auth->u.open = p->idAuthenticationOpen;
456 req.idAuthentication = auth;
458 req.implementationId = p->implementationId;
459 req.implementationName = p->implementationName;
460 req.implementationVersion = "0.1";
461 req.userInformationField = 0;
463 apdu.u.initRequest = &req;
464 apdu.which = Z_APDU_initRequest;
466 if (!z_APDU (p->odr_out, &apdup, 0))
468 Tcl_AppendResult (interp, odr_errlist [odr_geterror (p->odr_out)],
470 odr_reset (p->odr_out);
473 p->sbuf = odr_getbuf (p->odr_out, &p->slen, NULL);
474 if ((r=cs_put (p->cs_link, p->sbuf, p->slen)) < 0)
476 interp->result = "cs_put failed in init";
477 do_disconnect (p, NULL, 2, NULL);
482 ir_select_add_write (cs_fileno(p->cs_link), p);
483 logf (LOG_DEBUG, "Sent part of initializeRequest (%d bytes)", p->slen);
486 logf (LOG_DEBUG, "Sent whole initializeRequest (%d bytes)", p->slen);
491 * do_protocolVersion: Set protocol Version
493 static int do_protocolVersion (void *obj, Tcl_Interp *interp,
494 int argc, char **argv)
496 static struct ir_named_entry version_tab[] = {
507 ODR_MASK_ZERO (&p->protocolVersion);
508 ODR_MASK_SET (&p->protocolVersion, 0);
509 ODR_MASK_SET (&p->protocolVersion, 1);
512 return ir_named_bits (version_tab, &p->protocolVersion,
513 interp, argc-2, argv+2);
517 * do_options: Set options
519 static int do_options (void *obj, Tcl_Interp *interp,
520 int argc, char **argv)
522 static struct ir_named_entry options_tab[] = {
526 { "resourceReport", 3 },
527 { "triggerResourceCtrl", 4},
528 { "resourceCtrl", 5},
532 { "extentedServices", 10},
533 { "level-1Segmentation", 11},
534 { "level-2Segmentation", 12},
535 { "concurrentOperations", 13},
536 { "namedResultSets", 14},
543 ODR_MASK_ZERO (&p->options);
544 ODR_MASK_SET (&p->options, 0);
545 ODR_MASK_SET (&p->options, 1);
546 ODR_MASK_SET (&p->options, 7);
547 ODR_MASK_SET (&p->options, 14);
550 return ir_named_bits (options_tab, &p->options, interp, argc-2, argv+2);
554 * do_preferredMessageSize: Set/get preferred message size
556 static int do_preferredMessageSize (void *obj, Tcl_Interp *interp,
557 int argc, char **argv)
563 p->preferredMessageSize = 4096;
566 return get_set_int (&p->preferredMessageSize, interp, argc, argv);
570 * do_maximumRecordSize: Set/get maximum record size
572 static int do_maximumRecordSize (void *obj, Tcl_Interp *interp,
573 int argc, char **argv)
579 p->maximumRecordSize = 32768;
582 return get_set_int (&p->maximumRecordSize, interp, argc, argv);
586 * do_initResult: Get init result
588 static int do_initResult (void *obj, Tcl_Interp *interp,
589 int argc, char **argv)
595 return get_set_int (&p->initResult, interp, argc, argv);
600 * do_implementationName: Set/get Implementation Name.
602 static int do_implementationName (void *obj, Tcl_Interp *interp,
603 int argc, char **argv)
608 return ir_strdup (interp, &p->implementationName, "TCL/TK on YAZ");
610 return ir_strdel (interp, &p->implementationName);
613 free (p->implementationName);
614 if (ir_strdup (interp, &p->implementationName, argv[2])
618 Tcl_AppendResult (interp, p->implementationName,
624 * do_implementationId: Set/get Implementation Id.
626 static int do_implementationId (void *obj, Tcl_Interp *interp,
627 int argc, char **argv)
632 return ir_strdup (interp, &p->implementationId, "81");
634 return ir_strdel (interp, &p->implementationId);
637 free (p->implementationId);
638 if (ir_strdup (interp, &p->implementationId, argv[2]) == TCL_ERROR)
641 Tcl_AppendResult (interp, p->implementationId, (char*) NULL);
646 * do_targetImplementationName: Get Implementation Name of target.
648 static int do_targetImplementationName (void *obj, Tcl_Interp *interp,
649 int argc, char **argv)
655 p->targetImplementationName = NULL;
659 return ir_strdel (interp, &p->targetImplementationName);
660 Tcl_AppendResult (interp, p->targetImplementationName,
666 * do_targetImplementationId: Get Implementation Id of target
668 static int do_targetImplementationId (void *obj, Tcl_Interp *interp,
669 int argc, char **argv)
675 p->targetImplementationId = NULL;
679 return ir_strdel (interp, &p->targetImplementationId);
680 Tcl_AppendResult (interp, p->targetImplementationId, (char*) NULL);
685 * do_targetImplementationVersion: Get Implementation Version of target
687 static int do_targetImplementationVersion (void *obj, Tcl_Interp *interp,
688 int argc, char **argv)
694 p->targetImplementationVersion = NULL;
698 return ir_strdel (interp, &p->targetImplementationVersion);
699 Tcl_AppendResult (interp, p->targetImplementationVersion, (char*) NULL);
704 * do_idAuthentication: Set/get id Authentication
706 static int do_idAuthentication (void *obj, Tcl_Interp *interp,
707 int argc, char **argv)
711 if (argc >= 3 || argc == -1)
713 free (p->idAuthenticationOpen);
714 free (p->idAuthenticationGroupId);
715 free (p->idAuthenticationUserId);
716 free (p->idAuthenticationPassword);
718 if (argc >= 3 || argc <= 0)
720 p->idAuthenticationOpen = NULL;
721 p->idAuthenticationGroupId = NULL;
722 p->idAuthenticationUserId = NULL;
723 p->idAuthenticationPassword = NULL;
731 if (ir_strdup (interp, &p->idAuthenticationOpen, argv[2])
737 if (ir_strdup (interp, &p->idAuthenticationGroupId, argv[2])
740 if (ir_strdup (interp, &p->idAuthenticationUserId, argv[3])
743 if (ir_strdup (interp, &p->idAuthenticationPassword, argv[4])
748 if (p->idAuthenticationOpen)
749 Tcl_AppendElement (interp, p->idAuthenticationOpen);
750 else if (p->idAuthenticationGroupId)
752 Tcl_AppendElement (interp, p->idAuthenticationGroupId);
753 Tcl_AppendElement (interp, p->idAuthenticationUserId);
754 Tcl_AppendElement (interp, p->idAuthenticationPassword);
760 * do_connect: connect method on IR object
762 static int do_connect (void *obj, Tcl_Interp *interp,
763 int argc, char **argv)
768 int protocol_type = PROTO_Z3950;
776 interp->result = "already connected";
779 if (!strcmp (p->protocol_type, "Z3950"))
780 protocol_type = PROTO_Z3950;
781 else if (!strcmp (p->protocol_type, "SR"))
782 protocol_type = PROTO_SR;
785 interp->result = "bad protocol type";
788 if (!strcmp (p->cs_type, "tcpip"))
790 p->cs_link = cs_create (tcpip_type, CS_BLOCK, protocol_type);
791 addr = tcpip_strtoaddr (argv[2]);
794 interp->result = "tcpip_strtoaddr fail";
797 logf (LOG_DEBUG, "tcp/ip connect %s", argv[2]);
800 else if (!strcmp (p->cs_type, "mosi"))
802 p->cs_link = cs_create (mosi_type, CS_BLOCK, protocol_type);
803 addr = mosi_strtoaddr (argv[2]);
806 interp->result = "mosi_strtoaddr fail";
809 logf (LOG_DEBUG, "mosi connect %s", argv[2]);
814 interp->result = "unknown comstack type";
817 if (ir_strdup (interp, &p->hostname, argv[2]) == TCL_ERROR)
819 if ((r=cs_connect (p->cs_link, addr)) < 0)
821 interp->result = "cs_connect fail";
822 do_disconnect (p, NULL, 2, NULL);
825 ir_select_add (cs_fileno (p->cs_link), p);
828 ir_select_add_write (cs_fileno (p->cs_link), p);
835 Tcl_Eval (p->interp, p->callback);
839 Tcl_AppendElement (interp, p->hostname);
844 * do_disconnect: disconnect method on IR object
846 static int do_disconnect (void *obj, Tcl_Interp *interp,
847 int argc, char **argv)
862 ir_select_remove_write (cs_fileno (p->cs_link), p);
863 ir_select_remove (cs_fileno (p->cs_link), p);
866 cs_close (p->cs_link);
869 assert (!p->cs_link);
874 * do_comstack: Set/get comstack method on IR object
876 static int do_comstack (void *o, Tcl_Interp *interp,
877 int argc, char **argv)
882 return ir_strdup (interp, &obj->cs_type, "tcpip");
884 return ir_strdel (interp, &obj->cs_type);
888 if (ir_strdup (interp, &obj->cs_type, argv[2]) == TCL_ERROR)
891 Tcl_AppendElement (interp, obj->cs_type);
896 * do_protocol: Set/get protocol method on IR object
898 static int do_protocol (void *o, Tcl_Interp *interp,
899 int argc, char **argv)
904 return ir_strdup (interp, &obj->protocol_type, "Z3950");
906 return ir_strdel (interp, &obj->protocol_type);
909 free (obj->protocol_type);
910 if (ir_strdup (interp, &obj->protocol_type, argv[2]) == TCL_ERROR)
913 Tcl_AppendElement (interp, obj->protocol_type);
918 * do_callback: add callback
920 static int do_callback (void *obj, Tcl_Interp *interp,
921 int argc, char **argv)
931 return ir_strdel (interp, &p->callback);
937 if (ir_strdup (interp, &p->callback, argv[2]) == TCL_ERROR)
948 * do_failback: add error handle callback
950 static int do_failback (void *obj, Tcl_Interp *interp,
951 int argc, char **argv)
961 return ir_strdel (interp, &p->failback);
967 if (ir_strdup (interp, &p->failback, argv[2]) == TCL_ERROR)
978 * do_databaseNames: specify database names
980 static int do_databaseNames (void *obj, Tcl_Interp *interp,
981 int argc, char **argv)
988 for (i=0; i<p->num_databaseNames; i++)
989 free (p->databaseNames[i]);
990 free (p->databaseNames);
994 p->num_databaseNames = 0;
995 p->databaseNames = NULL;
1000 for (i=0; i<p->num_databaseNames; i++)
1001 Tcl_AppendElement (interp, p->databaseNames[i]);
1004 if (p->databaseNames)
1006 for (i=0; i<p->num_databaseNames; i++)
1007 free (p->databaseNames[i]);
1008 free (p->databaseNames);
1010 p->num_databaseNames = argc - 2;
1011 if (!(p->databaseNames = ir_malloc (interp,
1012 sizeof(*p->databaseNames) * p->num_databaseNames)))
1014 for (i=0; i<p->num_databaseNames; i++)
1016 if (ir_strdup (interp, &p->databaseNames[i], argv[2+i])
1024 * do_replaceIndicator: Set/get replace Set indicator
1026 static int do_replaceIndicator (void *obj, Tcl_Interp *interp,
1027 int argc, char **argv)
1033 p->replaceIndicator = 1;
1036 return get_set_int (&p->replaceIndicator, interp, argc, argv);
1040 * do_queryType: Set/Get query method
1042 static int do_queryType (void *obj, Tcl_Interp *interp,
1043 int argc, char **argv)
1048 return ir_strdup (interp, &p->queryType, "rpn");
1049 else if (argc == -1)
1050 return ir_strdel (interp, &p->queryType);
1053 free (p->queryType);
1054 if (ir_strdup (interp, &p->queryType, argv[2]) == TCL_ERROR)
1057 Tcl_AppendResult (interp, p->queryType, NULL);
1062 * do_userInformationField: Get User information field
1064 static int do_userInformationField (void *obj, Tcl_Interp *interp,
1065 int argc, char **argv)
1071 p->userInformationField = NULL;
1074 else if (argc == -1)
1075 return ir_strdel (interp, &p->userInformationField);
1076 Tcl_AppendResult (interp, p->userInformationField, NULL);
1081 * do_smallSetUpperBound: Set/get small set upper bound
1083 static int do_smallSetUpperBound (void *o, Tcl_Interp *interp,
1084 int argc, char **argv)
1090 p->smallSetUpperBound = 0;
1093 return get_set_int (&p->smallSetUpperBound, interp, argc, argv);
1097 * do_largeSetLowerBound: Set/get large set lower bound
1099 static int do_largeSetLowerBound (void *o, Tcl_Interp *interp,
1100 int argc, char **argv)
1106 p->largeSetLowerBound = 2;
1109 return get_set_int (&p->largeSetLowerBound, interp, argc, argv);
1113 * do_mediumSetPresentNumber: Set/get large set lower bound
1115 static int do_mediumSetPresentNumber (void *o, Tcl_Interp *interp,
1116 int argc, char **argv)
1122 p->mediumSetPresentNumber = 0;
1125 return get_set_int (&p->mediumSetPresentNumber, interp, argc, argv);
1129 static IRMethod ir_method_tab[] = {
1130 { 1, "comstack", do_comstack },
1131 { 1, "protocol", do_protocol },
1132 { 0, "failback", do_failback },
1134 { 1, "connect", do_connect },
1135 { 0, "protocolVersion", do_protocolVersion },
1136 { 1, "preferredMessageSize", do_preferredMessageSize },
1137 { 1, "maximumRecordSize", do_maximumRecordSize },
1138 { 1, "implementationName", do_implementationName },
1139 { 1, "implementationId", do_implementationId },
1140 { 0, "targetImplementationName", do_targetImplementationName },
1141 { 0, "targetImplementationId", do_targetImplementationId },
1142 { 0, "targetImplementationVersion", do_targetImplementationVersion },
1143 { 0, "userInformationField", do_userInformationField },
1144 { 1, "idAuthentication", do_idAuthentication },
1145 { 0, "options", do_options },
1146 { 0, "init", do_init_request },
1147 { 0, "initResult", do_initResult },
1148 { 0, "disconnect", do_disconnect },
1149 { 0, "callback", do_callback },
1153 static IRMethod ir_set_c_method_tab[] = {
1154 { 0, "databaseNames", do_databaseNames},
1155 { 0, "replaceIndicator", do_replaceIndicator},
1156 { 0, "queryType", do_queryType },
1157 { 0, "smallSetUpperBound", do_smallSetUpperBound},
1158 { 0, "largeSetLowerBound", do_largeSetLowerBound},
1159 { 0, "mediumSetPresentNumber", do_mediumSetPresentNumber},
1164 * ir_obj_method: IR Object methods
1166 static int ir_obj_method (ClientData clientData, Tcl_Interp *interp,
1167 int argc, char **argv)
1170 IRObj *p = clientData;
1173 return ir_method_r (clientData, interp, argc, argv, ir_method_tab);
1175 tab[0].tab = ir_method_tab;
1177 tab[1].tab = ir_set_c_method_tab;
1178 tab[1].obj = &p->set_inher;
1181 return ir_method (interp, argc, argv, tab);
1185 * ir_obj_delete: IR Object disposal
1187 static void ir_obj_delete (ClientData clientData)
1189 IRObj *obj = clientData;
1193 if (obj->ref_count > 0)
1195 assert (obj->ref_count == 0);
1197 tab[0].tab = ir_method_tab;
1199 tab[1].tab = ir_set_c_method_tab;
1200 tab[1].obj = &obj->set_inher;
1203 ir_method (NULL, -1, NULL, tab);
1204 odr_destroy (obj->odr_in);
1205 odr_destroy (obj->odr_out);
1206 odr_destroy (obj->odr_pr);
1207 free (obj->buf_out);
1213 * ir_obj_mk: IR Object creation
1215 static int ir_obj_mk (ClientData clientData, Tcl_Interp *interp,
1216 int argc, char **argv)
1226 interp->result = "wrong # args";
1229 if (!(obj = ir_malloc (interp, sizeof(*obj))))
1234 obj->bibset = ccl_qual_mk ();
1235 if ((inf = fopen ("default.bib", "r")))
1237 ccl_qual_file (obj->bibset, inf);
1242 obj->odr_in = odr_createmem (ODR_DECODE);
1243 obj->odr_out = odr_createmem (ODR_ENCODE);
1244 obj->odr_pr = odr_createmem (ODR_PRINT);
1246 obj->len_out = 10000;
1247 if (!(obj->buf_out = ir_malloc (interp, obj->len_out)))
1249 odr_setbuf (obj->odr_out, obj->buf_out, obj->len_out, 0);
1254 tab[0].tab = ir_method_tab;
1256 tab[1].tab = ir_set_c_method_tab;
1257 tab[1].obj = &obj->set_inher;
1260 if (ir_method (interp, 0, NULL, tab) == TCL_ERROR)
1262 Tcl_CreateCommand (interp, argv[1], ir_obj_method,
1263 (ClientData) obj, ir_obj_delete);
1267 /* ------------------------------------------------------- */
1269 * do_search: Do search request
1271 static int do_search (void *o, Tcl_Interp *interp,
1272 int argc, char **argv)
1274 Z_SearchRequest req;
1276 Z_APDU apdu, *apdup = &apdu;
1279 IRObj *p = obj->parent;
1289 interp->result = "wrong # args";
1292 if (!p->set_inher.num_databaseNames)
1294 interp->result = "no databaseNames";
1299 interp->result = "not connected";
1302 odr_reset (p->odr_out);
1303 apdu.which = Z_APDU_searchRequest;
1304 apdu.u.searchRequest = &req;
1306 bib1.proto = PROTO_Z3950;
1307 bib1.class = CLASS_ATTSET;
1308 bib1.value = VAL_BIB1;
1310 req.referenceId = 0;
1311 req.smallSetUpperBound = &p->set_inher.smallSetUpperBound;
1312 req.largeSetLowerBound = &p->set_inher.largeSetLowerBound;
1313 req.mediumSetPresentNumber = &p->set_inher.mediumSetPresentNumber;
1314 req.replaceIndicator = &p->set_inher.replaceIndicator;
1315 req.resultSetName = obj->setName ? obj->setName : "Default";
1316 logf (LOG_DEBUG, "Search, resultSetName %s", req.resultSetName);
1317 req.num_databaseNames = p->set_inher.num_databaseNames;
1318 req.databaseNames = p->set_inher.databaseNames;
1319 for (r=0; r < p->set_inher.num_databaseNames; r++)
1320 logf (LOG_DEBUG, " Database %s", p->set_inher.databaseNames[r]);
1321 req.smallSetElementSetNames = 0;
1322 req.mediumSetElementSetNames = 0;
1323 req.preferredRecordSyntax = 0;
1326 if (!strcmp (p->set_inher.queryType, "rpn"))
1328 Z_RPNQuery *RPNquery;
1330 RPNquery = p_query_rpn (p->odr_out, argv[2]);
1333 Tcl_AppendResult (interp, "Syntax error in query", NULL);
1336 RPNquery->attributeSetId = oid_getoidbyent (&bib1);
1337 query.which = Z_Query_type_1;
1338 query.u.type_1 = RPNquery;
1339 logf (LOG_DEBUG, "RPN");
1342 else if (!strcmp (p->set_inher.queryType, "cclrpn"))
1346 struct ccl_rpn_node *rpn;
1347 Z_RPNQuery *RPNquery;
1349 rpn = ccl_find_str(p->bibset, argv[2], &error, &pos);
1352 Tcl_AppendResult (interp, "CCL error: ", ccl_err_msg(error),NULL);
1355 ccl_pr_tree (rpn, stderr);
1356 fprintf (stderr, "\n");
1357 assert((RPNquery = ccl_rpn_query(rpn)));
1358 RPNquery->attributeSetId = oid_getoidbyent (&bib1);
1359 query.which = Z_Query_type_1;
1360 query.u.type_1 = RPNquery;
1361 logf (LOG_DEBUG, "CCLRPN");
1364 else if (!strcmp (p->set_inher.queryType, "ccl"))
1366 query.which = Z_Query_type_2;
1367 query.u.type_2 = &ccl_query;
1368 ccl_query.buf = (unsigned char *) argv[2];
1369 ccl_query.len = strlen (argv[2]);
1370 logf (LOG_DEBUG, "CCL");
1374 interp->result = "unknown query method";
1377 if (!z_APDU (p->odr_out, &apdup, 0))
1379 interp->result = odr_errlist [odr_geterror (p->odr_out)];
1380 odr_reset (p->odr_out);
1383 p->sbuf = odr_getbuf (p->odr_out, &p->slen, NULL);
1384 if ((r=cs_put (p->cs_link, p->sbuf, p->slen)) < 0)
1386 interp->result = "cs_put failed in search";
1391 ir_select_add_write (cs_fileno(p->cs_link), p);
1392 logf (LOG_DEBUG, "Sent part of searchRequest (%d bytes)", p->slen);
1396 logf (LOG_DEBUG, "Whole search request (%d bytes)", p->slen);
1402 * do_resultCount: Get number of hits
1404 static int do_resultCount (void *o, Tcl_Interp *interp,
1405 int argc, char **argv)
1411 return get_set_int (&obj->resultCount, interp, argc, argv);
1415 * do_searchStatus: Get search status (after search response)
1417 static int do_searchStatus (void *o, Tcl_Interp *interp,
1418 int argc, char **argv)
1424 return get_set_int (&obj->searchStatus, interp, argc, argv);
1428 * do_presentStatus: Get search status (after search/present response)
1430 static int do_presentStatus (void *o, Tcl_Interp *interp,
1431 int argc, char **argv)
1437 return get_set_int (&obj->presentStatus, interp, argc, argv);
1441 * do_nextResultSetPosition: Get next result set position
1442 * (after search/present response)
1444 static int do_nextResultSetPosition (void *o, Tcl_Interp *interp,
1445 int argc, char **argv)
1451 return get_set_int (&obj->nextResultSetPosition, interp, argc, argv);
1455 * do_setName: Set result Set name
1457 static int do_setName (void *o, Tcl_Interp *interp,
1458 int argc, char **argv)
1463 return ir_strdup (interp, &obj->setName, "Default");
1464 else if (argc == -1)
1465 return ir_strdel (interp, &obj->setName);
1468 free (obj->setName);
1469 if (ir_strdup (interp, &obj->setName, argv[2])
1473 Tcl_AppendElement (interp, obj->setName);
1478 * do_numberOfRecordsReturned: Get number of records returned
1480 static int do_numberOfRecordsReturned (void *o, Tcl_Interp *interp,
1481 int argc, char **argv)
1487 return get_set_int (&obj->numberOfRecordsReturned, interp, argc, argv);
1491 * do_recordType: Return record type (if any) at position.
1493 static int do_recordType (void *o, Tcl_Interp *interp, int argc, char **argv)
1501 obj->record_list = NULL;
1504 else if (argc == -1)
1506 delete_IR_records (obj);
1511 sprintf (interp->result, "wrong # args");
1514 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
1516 rl = find_IR_record (obj, offset);
1521 case Z_NamePlusRecord_databaseRecord:
1522 interp->result = "databaseRecord";
1524 case Z_NamePlusRecord_surrogateDiagnostic:
1525 interp->result = "surrogateDiagnostic";
1532 * do_recordDiag: Return diagnostic record info
1534 static int do_recordDiag (void *o, Tcl_Interp *interp, int argc, char **argv)
1545 sprintf (interp->result, "wrong # args");
1548 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
1550 rl = find_IR_record (obj, offset);
1553 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
1556 if (rl->which != Z_NamePlusRecord_surrogateDiagnostic)
1558 Tcl_AppendResult (interp, "No Diagnostic record at #", argv[2], NULL);
1561 sprintf (buf, "%d", rl->u.diag.condition);
1562 Tcl_AppendResult (interp, buf, " {",
1563 (rl->u.diag.addinfo ? rl->u.diag.addinfo : ""),
1569 * do_getMarc: Get ISO2709 Record lines/fields
1571 static int do_getMarc (void *o, Tcl_Interp *interp, int argc, char **argv)
1581 sprintf (interp->result, "wrong # args");
1584 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
1586 rl = find_IR_record (obj, offset);
1589 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
1592 if (rl->which != Z_NamePlusRecord_databaseRecord)
1594 Tcl_AppendResult (interp, "No MARC record at #", argv[2], NULL);
1597 return ir_tcl_get_marc (interp, rl->u.dbrec.buf, argc, argv);
1602 * do_responseStatus: Return response status (present or search)
1604 static int do_responseStatus (void *o, Tcl_Interp *interp,
1605 int argc, char **argv)
1611 obj->recordFlag = 0;
1612 obj->addinfo = NULL;
1615 else if (argc == -1)
1616 return ir_strdel (interp, &obj->addinfo);
1617 if (!obj->recordFlag)
1619 Tcl_AppendElement (interp, "OK");
1624 case Z_Records_DBOSD:
1625 Tcl_AppendElement (interp, "DBOSD");
1628 return mk_nonSurrogateDiagnostics (interp, obj->condition,
1635 * do_present: Perform Present Request
1638 static int do_present (void *o, Tcl_Interp *interp,
1639 int argc, char **argv)
1642 IRObj *p = obj->parent;
1643 Z_APDU apdu, *apdup = &apdu;
1644 Z_PresentRequest req;
1653 if (Tcl_GetInt (interp, argv[2], &start) == TCL_ERROR)
1660 if (Tcl_GetInt (interp, argv[3], &number) == TCL_ERROR)
1667 interp->result = "not connected";
1670 odr_reset (p->odr_out);
1672 obj->number = number;
1674 apdu.which = Z_APDU_presentRequest;
1675 apdu.u.presentRequest = &req;
1676 req.referenceId = 0;
1677 /* sprintf(setstring, "%d", setnumber); */
1679 req.resultSetId = obj->setName ? obj->setName : "Default";
1681 req.resultSetStartPoint = &start;
1682 req.numberOfRecordsRequested = &number;
1683 req.elementSetNames = 0;
1684 req.preferredRecordSyntax = 0;
1686 if (!z_APDU (p->odr_out, &apdup, 0))
1688 interp->result = odr_errlist [odr_geterror (p->odr_out)];
1689 odr_reset (p->odr_out);
1692 p->sbuf = odr_getbuf (p->odr_out, &p->slen, NULL);
1693 if ((r=cs_put (p->cs_link, p->sbuf, p->slen)) < 0)
1695 interp->result = "cs_put failed in present";
1700 ir_select_add_write (cs_fileno(p->cs_link), p);
1701 logf (LOG_DEBUG, "Part of present request, start=%d, num=%d"
1702 " (%d bytes)", start, number, p->slen);
1706 logf (LOG_DEBUG, "Whole present request, start=%d, num=%d"
1707 " (%d bytes)", start, number, p->slen);
1713 * do_loadFile: Load result set from file
1716 static int do_loadFile (void *o, Tcl_Interp *interp,
1717 int argc, char **argv)
1719 IRSetObj *setobj = o;
1729 interp->result = "wrong # args";
1732 inf = fopen (argv[2], "r");
1735 Tcl_AppendResult (interp, "Cannot open ", argv[2], NULL);
1738 while ((buf = ir_tcl_fread_marc (inf, &size)))
1742 rl = new_IR_record (setobj, no, Z_NamePlusRecord_databaseRecord);
1743 rl->u.dbrec.buf = buf;
1744 rl->u.dbrec.size = size;
1747 setobj->numberOfRecordsReturned = no-1;
1752 static IRMethod ir_set_method_tab[] = {
1753 { 0, "search", do_search },
1754 { 0, "searchStatus", do_searchStatus },
1755 { 0, "presentStatus", do_presentStatus },
1756 { 0, "nextResultSetPosition", do_nextResultSetPosition },
1757 { 0, "setName", do_setName },
1758 { 0, "resultCount", do_resultCount },
1759 { 0, "numberOfRecordsReturned", do_numberOfRecordsReturned },
1760 { 0, "present", do_present },
1761 { 0, "recordType", do_recordType },
1762 { 0, "getMarc", do_getMarc },
1763 { 0, "Diag", do_recordDiag },
1764 { 0, "responseStatus", do_responseStatus },
1765 { 0, "loadFile", do_loadFile },
1770 * ir_set_obj_method: IR Set Object methods
1772 static int ir_set_obj_method (ClientData clientData, Tcl_Interp *interp,
1773 int argc, char **argv)
1776 IRSetObj *p = clientData;
1780 interp->result = "wrong # args";
1783 tabs[0].tab = ir_set_method_tab;
1785 tabs[1].tab = ir_set_c_method_tab;
1786 tabs[1].obj = &p->set_inher;
1789 return ir_method (interp, argc, argv, tabs);
1793 * ir_set_obj_delete: IR Set Object disposal
1795 static void ir_set_obj_delete (ClientData clientData)
1798 IRSetObj *p = clientData;
1800 tabs[0].tab = ir_set_method_tab;
1802 tabs[1].tab = ir_set_c_method_tab;
1803 tabs[1].obj = &p->set_inher;
1806 ir_method (NULL, -1, NULL, tabs);
1812 * ir_set_obj_mk: IR Set Object creation
1814 static int ir_set_obj_mk (ClientData clientData, Tcl_Interp *interp,
1815 int argc, char **argv)
1820 if (argc < 2 || argc > 3)
1822 interp->result = "wrong # args";
1825 if (!(obj = ir_malloc (interp, sizeof(*obj))))
1829 Tcl_CmdInfo parent_info;
1834 if (!Tcl_GetCommandInfo (interp, argv[2], &parent_info))
1836 interp->result = "No parent";
1839 obj->parent = (IRObj *) parent_info.clientData;
1841 dst = &obj->set_inher;
1842 src = &obj->parent->set_inher;
1844 dst->num_databaseNames = src->num_databaseNames;
1845 if (!(dst->databaseNames =
1846 ir_malloc (interp, sizeof (*dst->databaseNames)
1847 * dst->num_databaseNames)))
1849 for (i = 0; i < dst->num_databaseNames; i++)
1851 if (ir_strdup (interp, &dst->databaseNames[i],
1852 src->databaseNames[i]) == TCL_ERROR)
1855 if (ir_strdup (interp, &dst->queryType, src->queryType)
1859 dst->smallSetUpperBound = src->smallSetUpperBound;
1860 dst->largeSetLowerBound = src->largeSetLowerBound;
1861 dst->mediumSetPresentNumber = src->mediumSetPresentNumber;
1866 tabs[0].tab = ir_set_method_tab;
1870 if (ir_method (interp, 0, NULL, tabs) == TCL_ERROR)
1873 Tcl_CreateCommand (interp, argv[1], ir_set_obj_method,
1874 (ClientData) obj, ir_set_obj_delete);
1878 /* ------------------------------------------------------- */
1881 * do_scan: Perform scan
1883 static int do_scan (void *o, Tcl_Interp *interp, int argc, char **argv)
1886 Z_APDU apdu, *apdup = &apdu;
1888 IRObj *p = obj->parent;
1892 struct ccl_rpn_node *rpn;
1901 interp->result = "wrong # args";
1904 if (!p->set_inher.num_databaseNames)
1906 interp->result = "no databaseNames";
1911 interp->result = "not connected";
1914 odr_reset (p->odr_out);
1916 bib1.proto = PROTO_Z3950;
1917 bib1.class = CLASS_ATTSET;
1918 bib1.value = VAL_BIB1;
1920 apdu.which = Z_APDU_scanRequest;
1921 apdu.u.scanRequest = &req;
1922 req.referenceId = NULL;
1923 req.num_databaseNames = p->set_inher.num_databaseNames;
1924 req.databaseNames = p->set_inher.databaseNames;
1925 req.attributeSet = oid_getoidbyent (&bib1);
1928 if (!(req.termListAndStartPoint = p_query_scan (p->odr_out, argv[2])))
1930 Tcl_AppendResult (interp, "Syntax error in query", NULL);
1934 rpn = ccl_find_str(p->bibset, argv[2], &r, &pos);
1937 Tcl_AppendResult (interp, "CCL error: ", ccl_err_msg (r), NULL);
1940 ccl_pr_tree (rpn, stderr);
1941 fprintf (stderr, "\n");
1942 if (!(req.termListAndStartPoint = ccl_scan_query (rpn)))
1945 req.stepSize = &obj->stepSize;
1946 req.numberOfTermsRequested = &obj->numberOfTermsRequested;
1947 req.preferredPositionInResponse = &obj->preferredPositionInResponse;
1948 logf (LOG_DEBUG, "stepSize=%d", *req.stepSize);
1949 logf (LOG_DEBUG, "numberOfTermsRequested=%d",
1950 *req.numberOfTermsRequested);
1951 logf (LOG_DEBUG, "preferredPositionInResponse=%d",
1952 *req.preferredPositionInResponse);
1954 if (!z_APDU (p->odr_out, &apdup, 0))
1956 interp->result = odr_errlist [odr_geterror (p->odr_out)];
1957 odr_reset (p->odr_out);
1960 p->sbuf = odr_getbuf (p->odr_out, &p->slen, NULL);
1961 if ((r=cs_put (p->cs_link, p->sbuf, p->slen)) < 0)
1963 interp->result = "cs_put failed in scan";
1968 ir_select_add_write (cs_fileno(p->cs_link), p);
1969 logf (LOG_DEBUG, "Sent part of scanRequest (%d bytes)", p->slen);
1973 logf (LOG_DEBUG, "Whole scan request (%d bytes)", p->slen);
1979 * do_stepSize: Set/get replace Step Size
1981 static int do_stepSize (void *obj, Tcl_Interp *interp,
1982 int argc, char **argv)
1990 return get_set_int (&p->stepSize, interp, argc, argv);
1994 * do_numberOfTermsRequested: Set/get Number of Terms requested
1996 static int do_numberOfTermsRequested (void *obj, Tcl_Interp *interp,
1997 int argc, char **argv)
2003 p->numberOfTermsRequested = 20;
2006 return get_set_int (&p->numberOfTermsRequested, interp, argc, argv);
2011 * do_preferredPositionInResponse: Set/get preferred Position
2013 static int do_preferredPositionInResponse (void *obj, Tcl_Interp *interp,
2014 int argc, char **argv)
2020 p->preferredPositionInResponse = 1;
2023 return get_set_int (&p->preferredPositionInResponse, interp, argc, argv);
2027 * do_scanStatus: Get scan status
2029 static int do_scanStatus (void *obj, Tcl_Interp *interp,
2030 int argc, char **argv)
2036 return get_set_int (&p->scanStatus, interp, argc, argv);
2040 * do_numberOfEntriesReturned: Get number of Entries returned
2042 static int do_numberOfEntriesReturned (void *obj, Tcl_Interp *interp,
2043 int argc, char **argv)
2049 return get_set_int (&p->numberOfEntriesReturned, interp, argc, argv);
2053 * do_positionOfTerm: Get position of Term
2055 static int do_positionOfTerm (void *obj, Tcl_Interp *interp,
2056 int argc, char **argv)
2062 return get_set_int (&p->positionOfTerm, interp, argc, argv);
2066 * do_scanLine: get Scan Line (surrogate or normal) after response
2068 static int do_scanLine (void *obj, Tcl_Interp *interp, int argc, char **argv)
2076 p->entries_flag = 0;
2078 p->nonSurrogateDiagnostics = NULL;
2081 else if (argc == -1)
2083 p->entries_flag = 0;
2084 /* release entries */
2086 /* release non diagnostics */
2087 p->nonSurrogateDiagnostics = NULL;
2092 interp->result = "wrong # args";
2095 if (Tcl_GetInt (interp, argv[2], &i) == TCL_ERROR)
2097 if (!p->entries_flag || p->which != Z_ListEntries_entries || !p->entries
2098 || i >= p->num_entries || i < 0)
2100 switch (p->entries[i].which)
2102 case Z_Entry_termInfo:
2103 Tcl_AppendElement (interp, "T");
2104 if (p->entries[i].u.term.buf)
2105 Tcl_AppendElement (interp, p->entries[i].u.term.buf);
2107 Tcl_AppendElement (interp, "");
2108 sprintf (numstr, "%d", p->entries[i].u.term.globalOccurrences);
2109 Tcl_AppendElement (interp, numstr);
2111 case Z_Entry_surrogateDiagnostic:
2113 mk_nonSurrogateDiagnostics (interp, p->entries[i].u.diag.condition,
2114 p->entries[i].u.diag.addinfo);
2120 static IRMethod ir_scan_method_tab[] = {
2121 { 0, "scan", do_scan },
2122 { 0, "stepSize", do_stepSize },
2123 { 0, "numberOfTermsRequested", do_numberOfTermsRequested },
2124 { 0, "preferredPositionInResponse", do_preferredPositionInResponse },
2125 { 0, "scanStatus", do_scanStatus },
2126 { 0, "numberOfEntriesReturned", do_numberOfEntriesReturned },
2127 { 0, "positionOfTerm", do_positionOfTerm },
2128 { 0, "scanLine", do_scanLine },
2133 * ir_scan_obj_method: IR Scan Object methods
2135 static int ir_scan_obj_method (ClientData clientData, Tcl_Interp *interp,
2136 int argc, char **argv)
2142 interp->result = "wrong # args";
2145 tabs[0].tab = ir_scan_method_tab;
2146 tabs[0].obj = clientData;
2149 return ir_method (interp, argc, argv, tabs);
2153 * ir_scan_obj_delete: IR Scan Object disposal
2155 static void ir_scan_obj_delete (ClientData clientData)
2157 free ( (void*) clientData);
2161 * ir_scan_obj_mk: IR Scan Object creation
2163 static int ir_scan_obj_mk (ClientData clientData, Tcl_Interp *interp,
2164 int argc, char **argv)
2166 Tcl_CmdInfo parent_info;
2172 interp->result = "wrong # args";
2175 if (get_parent_info (interp, argv[1], &parent_info, NULL) == TCL_ERROR)
2177 if (!(obj = ir_malloc (interp, sizeof(*obj))))
2180 tabs[0].tab = ir_scan_method_tab;
2184 if (ir_method (interp, 0, NULL, tabs) == TCL_ERROR)
2188 obj->numberOfTermsRequested = 20;
2189 obj->preferredPositionInResponse = 1;
2191 obj->entries = NULL;
2192 obj->nonSurrogateDiagnostics = NULL;
2195 obj->parent = (IRObj *) parent_info.clientData;
2196 Tcl_CreateCommand (interp, argv[1], ir_scan_obj_method,
2197 (ClientData) obj, ir_scan_obj_delete);
2201 /* ------------------------------------------------------- */
2203 static void ir_initResponse (void *obj, Z_InitResponse *initrs)
2207 p->initResult = *initrs->result ? 1 : 0;
2208 if (!*initrs->result)
2209 logf (LOG_DEBUG, "Connection rejected by target");
2211 logf (LOG_DEBUG, "Connection accepted by target");
2213 free (p->targetImplementationId);
2214 ir_strdup (p->interp, &p->targetImplementationId,
2215 initrs->implementationId);
2216 free (p->targetImplementationName);
2217 ir_strdup (p->interp, &p->targetImplementationName,
2218 initrs->implementationName);
2219 free (p->targetImplementationVersion);
2220 ir_strdup (p->interp, &p->targetImplementationVersion,
2221 initrs->implementationVersion);
2223 p->maximumRecordSize = *initrs->maximumRecordSize;
2224 p->preferredMessageSize = *initrs->preferredMessageSize;
2226 memcpy (&p->options, initrs->options, sizeof(initrs->options));
2227 memcpy (&p->protocolVersion, initrs->protocolVersion,
2228 sizeof(initrs->protocolVersion));
2229 free (p->userInformationField);
2230 p->userInformationField = NULL;
2231 if (initrs->userInformationField)
2235 if (initrs->userInformationField->which == ODR_EXTERNAL_octet &&
2236 (p->userInformationField =
2238 initrs->userInformationField->u.octet_aligned->len)
2241 memcpy (p->userInformationField,
2242 initrs->userInformationField->u.octet_aligned->buf,
2244 (p->userInformationField)[len] = '\0';
2249 static void ir_handleRecords (void *o, Z_Records *zrs)
2252 IRSetObj *setobj = p->set_child;
2254 setobj->which = zrs->which;
2255 setobj->recordFlag = 1;
2256 if (zrs->which == Z_Records_NSD)
2258 const char *addinfo;
2260 setobj->numberOfRecordsReturned = 0;
2261 setobj->condition = *zrs->u.nonSurrogateDiagnostic->condition;
2262 free (setobj->addinfo);
2263 setobj->addinfo = NULL;
2264 addinfo = zrs->u.nonSurrogateDiagnostic->addinfo;
2265 if (addinfo && (setobj->addinfo = malloc (strlen(addinfo) + 1)))
2266 strcpy (setobj->addinfo, addinfo);
2267 logf (LOG_DEBUG, "Diagnostic response. %s (%d): %s",
2268 diagbib1_str (setobj->condition),
2270 setobj->addinfo ? setobj->addinfo : "");
2277 setobj->numberOfRecordsReturned =
2278 zrs->u.databaseOrSurDiagnostics->num_records;
2279 logf (LOG_DEBUG, "Got %d records", setobj->numberOfRecordsReturned);
2280 for (offset = 0; offset<setobj->numberOfRecordsReturned; offset++)
2282 rl = new_IR_record (setobj, setobj->start + offset,
2283 zrs->u.databaseOrSurDiagnostics->
2284 records[offset]->which);
2285 if (rl->which == Z_NamePlusRecord_surrogateDiagnostic)
2289 diagrec = zrs->u.databaseOrSurDiagnostics->
2290 records[offset]->u.surrogateDiagnostic;
2292 rl->u.diag.condition = *diagrec->condition;
2293 if (diagrec->addinfo && (rl->u.diag.addinfo =
2294 malloc (strlen (diagrec->addinfo)+1)))
2295 strcpy (rl->u.diag.addinfo, diagrec->addinfo);
2299 Z_DatabaseRecord *zr;
2302 zr = zrs->u.databaseOrSurDiagnostics->records[offset]
2304 oe = (Odr_external*) zr;
2305 rl->u.dbrec.size = zr->u.octet_aligned->len;
2306 if (oe->which == ODR_EXTERNAL_octet && rl->u.dbrec.size > 0)
2308 const char *buf = (char*) zr->u.octet_aligned->buf;
2309 if ((rl->u.dbrec.buf = malloc (rl->u.dbrec.size)))
2310 memcpy (rl->u.dbrec.buf, buf, rl->u.dbrec.size);
2313 rl->u.dbrec.buf = NULL;
2319 static void ir_searchResponse (void *o, Z_SearchResponse *searchrs)
2322 IRSetObj *setobj = p->set_child;
2323 Z_Records *zrs = searchrs->records;
2327 setobj->searchStatus = searchrs->searchStatus ? 1 : 0;
2328 setobj->resultCount = *searchrs->resultCount;
2329 if (searchrs->presentStatus)
2330 setobj->presentStatus = *searchrs->presentStatus;
2331 if (searchrs->nextResultSetPosition)
2332 setobj->nextResultSetPosition = *searchrs->nextResultSetPosition;
2334 logf (LOG_DEBUG, "Search response %d, %d hits",
2335 setobj->searchStatus, setobj->resultCount);
2337 ir_handleRecords (o, zrs);
2339 setobj->recordFlag = 0;
2342 logf (LOG_DEBUG, "Search response, no object!");
2346 static void ir_presentResponse (void *o, Z_PresentResponse *presrs)
2349 IRSetObj *setobj = p->set_child;
2350 Z_Records *zrs = presrs->records;
2352 logf (LOG_DEBUG, "Received presentResponse");
2353 setobj->presentStatus = *presrs->presentStatus;
2354 setobj->nextResultSetPosition = *presrs->nextResultSetPosition;
2356 ir_handleRecords (o, zrs);
2359 setobj->recordFlag = 0;
2360 logf (LOG_DEBUG, "No records!");
2364 static void ir_scanResponse (void *o, Z_ScanResponse *scanrs)
2367 IRScanObj *scanobj = p->scan_child;
2369 logf (LOG_DEBUG, "Received scanResponse");
2371 scanobj->scanStatus = *scanrs->scanStatus;
2372 logf (LOG_DEBUG, "scanStatus=%d", scanobj->scanStatus);
2374 if (scanrs->stepSize)
2375 scanobj->stepSize = *scanrs->stepSize;
2376 logf (LOG_DEBUG, "stepSize=%d", scanobj->stepSize);
2378 scanobj->numberOfEntriesReturned = *scanrs->numberOfEntriesReturned;
2379 logf (LOG_DEBUG, "numberOfEntriesReturned=%d",
2380 scanobj->numberOfEntriesReturned);
2382 if (scanrs->positionOfTerm)
2383 scanobj->positionOfTerm = *scanrs->positionOfTerm;
2385 scanobj->positionOfTerm = -1;
2386 logf (LOG_DEBUG, "positionOfTerm=%d", scanobj->positionOfTerm);
2388 free (scanobj->entries);
2389 scanobj->entries = NULL;
2390 free (scanobj->nonSurrogateDiagnostics);
2391 scanobj->nonSurrogateDiagnostics = NULL;
2393 if (scanrs->entries)
2398 scanobj->entries_flag = 1;
2399 scanobj->which = scanrs->entries->which;
2400 switch (scanobj->which)
2402 case Z_ListEntries_entries:
2403 scanobj->num_entries = scanrs->entries->u.entries->num_entries;
2404 scanobj->entries = malloc (scanobj->num_entries *
2405 sizeof(*scanobj->entries));
2406 for (i=0; i<scanobj->num_entries; i++)
2408 ze = scanrs->entries->u.entries->entries[i];
2409 scanobj->entries[i].which = ze->which;
2412 case Z_Entry_termInfo:
2413 if (ze->u.termInfo->term->which == Z_Term_general)
2415 int l = ze->u.termInfo->term->u.general->len;
2416 scanobj->entries[i].u.term.buf = malloc (1+l);
2417 memcpy (scanobj->entries[i].u.term.buf,
2418 ze->u.termInfo->term->u.general->buf,
2420 scanobj->entries[i].u.term.buf[l] = '\0';
2423 scanobj->entries[i].u.term.buf = NULL;
2424 if (ze->u.termInfo->globalOccurrences)
2425 scanobj->entries[i].u.term.globalOccurrences =
2426 *ze->u.termInfo->globalOccurrences;
2428 scanobj->entries[i].u.term.globalOccurrences = 0;
2430 case Z_Entry_surrogateDiagnostic:
2431 scanobj->entries[i].u.diag.addinfo =
2432 malloc (1+strlen(ze->u.surrogateDiagnostic->
2434 strcpy (scanobj->entries[i].u.diag.addinfo,
2435 ze->u.surrogateDiagnostic->addinfo);
2436 scanobj->entries[i].u.diag.condition =
2437 *ze->u.surrogateDiagnostic->condition;
2442 case Z_ListEntries_nonSurrogateDiagnostics:
2443 scanobj->num_diagRecs = scanrs->entries->
2444 u.nonSurrogateDiagnostics->num_diagRecs;
2445 scanobj->nonSurrogateDiagnostics = malloc (scanobj->num_diagRecs *
2446 sizeof(*scanobj->nonSurrogateDiagnostics));
2451 scanobj->entries_flag = 0;
2455 * ir_select_read: handle incoming packages
2457 void ir_select_read (ClientData clientData)
2459 IRObj *p = clientData;
2465 r = cs_rcvconnect (p->cs_link);
2469 ir_select_remove_write (cs_fileno (p->cs_link), p);
2472 logf (LOG_DEBUG, "cs_rcvconnect error");
2474 Tcl_Eval (p->interp, p->failback);
2475 do_disconnect (p, NULL, 2, NULL);
2479 Tcl_Eval (p->interp, p->callback);
2484 /* signal one more use of ir object - callbacks must not
2485 release the ir memory (p pointer) */
2487 if ((r=cs_get (p->cs_link, &p->buf_in, &p->len_in)) <= 0)
2489 logf (LOG_DEBUG, "cs_get failed, code %d", r);
2490 ir_select_remove (cs_fileno (p->cs_link), p);
2492 Tcl_Eval (p->interp, p->failback);
2493 do_disconnect (p, NULL, 2, NULL);
2495 /* relase ir object now if callback deleted it */
2501 odr_setbuf (p->odr_in, p->buf_in, r, 0);
2502 logf (LOG_DEBUG, "cs_get ok, got %d", r);
2503 if (!z_APDU (p->odr_in, &apdu, 0))
2505 logf (LOG_DEBUG, "%s", odr_errlist [odr_geterror (p->odr_in)]);
2507 Tcl_Eval (p->interp, p->failback);
2508 do_disconnect (p, NULL, 2, NULL);
2510 /* relase ir object now if callback deleted it */
2516 case Z_APDU_initResponse:
2517 ir_initResponse (p, apdu->u.initResponse);
2519 case Z_APDU_searchResponse:
2520 ir_searchResponse (p, apdu->u.searchResponse);
2522 case Z_APDU_presentResponse:
2523 ir_presentResponse (p, apdu->u.presentResponse);
2525 case Z_APDU_scanResponse:
2526 ir_scanResponse (p, apdu->u.scanResponse);
2529 logf (LOG_WARN, "Received unknown APDU type (%d)", apdu->which);
2531 Tcl_Eval (p->interp, p->failback);
2532 do_disconnect (p, NULL, 2, NULL);
2534 odr_reset (p->odr_in);
2536 Tcl_Eval (p->interp, p->callback);
2537 if (p->ref_count == 1)
2543 } while (p->cs_link && cs_more (p->cs_link));
2547 * ir_select_write: handle outgoing packages - not yet written.
2549 void ir_select_write (ClientData clientData)
2551 IRObj *p = clientData;
2554 logf (LOG_DEBUG, "In write handler");
2557 r = cs_rcvconnect (p->cs_link);
2563 logf (LOG_DEBUG, "cs_rcvconnect error");
2564 ir_select_remove_write (cs_fileno (p->cs_link), p);
2566 Tcl_Eval (p->interp, p->failback);
2567 do_disconnect (p, NULL, 2, NULL);
2570 ir_select_remove_write (cs_fileno (p->cs_link), p);
2572 Tcl_Eval (p->interp, p->callback);
2575 if ((r=cs_put (p->cs_link, p->sbuf, p->slen)) < 0)
2577 logf (LOG_DEBUG, "select write fail");
2579 Tcl_Eval (p->interp, p->failback);
2580 do_disconnect (p, NULL, 2, NULL);
2582 else if (r == 0) /* remove select bit */
2584 ir_select_remove_write (cs_fileno (p->cs_link), p);
2588 /* ------------------------------------------------------- */
2591 * ir_tcl_init: Registration of TCL commands.
2593 int ir_tcl_init (Tcl_Interp *interp)
2595 Tcl_CreateCommand (interp, "ir", ir_obj_mk, (ClientData) NULL,
2596 (Tcl_CmdDeleteProc *) NULL);
2597 Tcl_CreateCommand (interp, "ir-set", ir_set_obj_mk,
2598 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
2599 Tcl_CreateCommand (interp, "ir-scan", ir_scan_obj_mk,
2600 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);