2 * IR toolkit for tcl/tk
4 * See the file LICENSE for details.
5 * Sebastian Hammer, Adam Dickmeiss
8 * Revision 1.33 1995-05-29 09:15:11 quinn
9 * Changed CS_SR to PROTO_SR, etc.
11 * Revision 1.32 1995/05/29 08:44:16 adam
12 * Work on delete of objects.
14 * Revision 1.31 1995/05/26 11:44:10 adam
15 * Bugs fixed. More work on MARC utilities and queries. Test
16 * client is up-to-date again.
18 * Revision 1.30 1995/05/26 08:54:11 adam
19 * New MARC utilities. Uses prefix query.
21 * Revision 1.29 1995/05/24 14:10:22 adam
22 * Work on idAuthentication, protocolVersion and options.
24 * Revision 1.28 1995/05/23 15:34:48 adam
25 * Many new settings, userInformationField, smallSetUpperBound, etc.
26 * A number of settings are inherited when ir-set is executed.
27 * This version is incompatible with the graphical test client (client.tcl).
29 * Revision 1.27 1995/05/11 15:34:47 adam
30 * Scan request changed a bit. This version works with RLG.
32 * Revision 1.26 1995/04/18 16:11:51 adam
33 * First version of graphical Scan. Some work on query-by-form.
35 * Revision 1.25 1995/04/17 09:37:17 adam
36 * Further development of scan.
38 * Revision 1.24 1995/04/11 14:16:42 adam
39 * Further work on scan. Response works. Entries aren't saved yet.
41 * Revision 1.23 1995/04/10 10:50:27 adam
42 * Result-set name defaults to suffix of ir-set name.
43 * Started working on scan. Not finished at this point.
45 * Revision 1.22 1995/03/31 10:43:03 adam
46 * More robust when getting bad MARC records.
48 * Revision 1.21 1995/03/31 08:56:37 adam
49 * New button "Search".
51 * Revision 1.20 1995/03/29 16:07:09 adam
52 * Bug fix: Didn't use setName in present request.
54 * Revision 1.19 1995/03/28 12:45:23 adam
55 * New ir method failback: called on disconnect/protocol error.
56 * New ir set/get method: protocol: SR / Z3950.
57 * Simple popup and disconnect when failback is invoked.
59 * Revision 1.18 1995/03/21 15:50:12 adam
62 * Revision 1.17 1995/03/21 13:41:03 adam
63 * Comstack cs_create not used too often. Non-blocking connect.
65 * Revision 1.16 1995/03/21 08:26:06 adam
66 * New method, setName, to specify the result set name (other than Default).
67 * New method, responseStatus, which returns diagnostic info, if any, after
68 * present response / search response.
70 * Revision 1.15 1995/03/20 15:24:07 adam
71 * Diagnostic records saved on searchResponse.
73 * Revision 1.14 1995/03/20 08:53:22 adam
74 * Event loop in tclmain.c rewritten. New method searchStatus.
76 * Revision 1.13 1995/03/17 18:26:17 adam
77 * Non-blocking i/o used now. Database names popup as cascade items.
79 * Revision 1.12 1995/03/17 15:45:00 adam
80 * Improved target/database setup.
82 * Revision 1.11 1995/03/16 17:54:03 adam
83 * Minor changes really.
85 * Revision 1.10 1995/03/15 16:14:50 adam
86 * Blocking arg in cs_create changed.
88 * Revision 1.9 1995/03/15 13:59:24 adam
91 * Revision 1.8 1995/03/15 08:25:16 adam
92 * New method presentStatus to check for error on present. Misc. cleanup
93 * of IRRecordList manipulations. Full MARC record presentation in
96 * Revision 1.7 1995/03/14 17:32:29 adam
97 * Presentation of full Marc record in popup window.
99 * Revision 1.6 1995/03/12 19:31:55 adam
100 * Pattern matching implemented when retrieving MARC records. More
101 * diagnostic functions.
103 * Revision 1.5 1995/03/10 18:00:15 adam
104 * Actual presentation in line-by-line format. RPN query support.
106 * Revision 1.4 1995/03/09 16:15:08 adam
107 * First presentRequest attempts. Hot-target list.
113 #include <sys/time.h>
123 int (*method) (void *obj, Tcl_Interp *interp, int argc, char **argv);
131 static int do_disconnect (void *obj,Tcl_Interp *interp, int argc, char **argv);
133 static IRRecordList *new_IR_record (IRSetObj *setobj, int no, int which)
137 for (rl = setobj->record_list; rl; rl = rl->next)
143 case Z_NamePlusRecord_databaseRecord:
144 free (rl->u.dbrec.buf);
145 rl->u.dbrec.buf = NULL;
147 case Z_NamePlusRecord_surrogateDiagnostic:
148 free (rl->u.diag.addinfo);
149 rl->u.diag.addinfo = NULL;
157 rl = malloc (sizeof(*rl));
159 rl->next = setobj->record_list;
161 setobj->record_list = rl;
167 static IRRecordList *find_IR_record (IRSetObj *setobj, int no)
171 for (rl = setobj->record_list; rl; rl = rl->next)
177 static void delete_IR_records (IRSetObj *setobj)
179 IRRecordList *rl, *rl1;
181 for (rl = setobj->record_list; rl; rl = rl1)
185 case Z_NamePlusRecord_databaseRecord:
186 free (rl->u.dbrec.buf);
188 case Z_NamePlusRecord_surrogateDiagnostic:
189 free (rl->u.diag.addinfo);
195 setobj->record_list = NULL;
199 * getsetint: Set/get integer value
201 static int get_set_int (int *val, Tcl_Interp *interp, int argc, char **argv)
207 if (Tcl_GetInt (interp, argv[2], val)==TCL_ERROR)
210 sprintf (buf, "%d", *val);
211 Tcl_AppendResult (interp, buf, NULL);
216 * mk_nonSurrogateDiagnostics: Make Tcl result with diagnostic info
218 static int mk_nonSurrogateDiagnostics (Tcl_Interp *interp,
225 Tcl_AppendElement (interp, "NSD");
226 sprintf (buf, "%d", condition);
227 Tcl_AppendElement (interp, buf);
228 cp = diagbib1_str (condition);
230 Tcl_AppendElement (interp, (char*) cp);
232 Tcl_AppendElement (interp, "");
234 Tcl_AppendElement (interp, (char*) addinfo);
236 Tcl_AppendElement (interp, "");
241 * get_parent_info: Returns information about parent object.
243 static int get_parent_info (Tcl_Interp *interp, const char *name,
244 Tcl_CmdInfo *parent_info,
247 char parent_name[128];
248 const char *csep = strrchr (name, '.');
253 interp->result = "missing .";
261 memcpy (parent_name, name, pos);
262 parent_name[pos] = '\0';
263 if (!Tcl_GetCommandInfo (interp, parent_name, parent_info))
265 interp->result = "No parent";
272 * ir_method: Search for method in table and invoke method handler
274 int ir_method (Tcl_Interp *interp, int argc, char **argv, IRMethods *tab)
276 IRMethods *tab_i = tab;
279 for (tab_i = tab; tab_i->tab; tab_i++)
280 for (t = tab_i->tab; t->name; t++)
283 if ((*t->method)(tab_i->obj, interp, argc, argv) == TCL_ERROR)
287 if (!strcmp (t->name, argv[1]))
288 return (*t->method)(tab_i->obj, interp, argc, argv);
292 Tcl_AppendResult (interp, "Bad method. Possible methods:", NULL);
293 for (tab_i = tab; tab_i->tab; tab_i++)
294 for (t = tab_i->tab; t->name; t++)
295 Tcl_AppendResult (interp, " ", t->name, NULL);
300 * ir_method_r: Get status for all readable elements
302 int ir_method_r (void *obj, Tcl_Interp *interp, int argc, char **argv,
310 for (; tab->name; tab++)
313 argv_n[1] = tab->name;
314 Tcl_AppendResult (interp, "{", NULL);
315 (*tab->method)(obj, interp, argc_n, argv_n);
316 Tcl_AppendResult (interp, "} ", NULL);
322 * ir_named_bits: get/set named bits
324 int ir_named_bits (struct ir_named_entry *tab, Odr_bitmask *ob,
325 Tcl_Interp *interp, int argc, char **argv)
327 struct ir_named_entry *ti;
332 for (no = 0; no < argc; no++)
334 for (ti = tab; ti->name; ti++)
335 if (!strcmp (argv[no], ti->name))
337 ODR_MASK_SET (ob, ti->pos);
342 Tcl_AppendResult (interp, "Bad bit mask: ", argv[no], NULL);
348 for (ti = tab; ti->name; ti++)
349 if (ODR_MASK_GET (ob, ti->pos))
350 Tcl_AppendElement (interp, ti->name);
355 * ir_strdup: Duplicate string
357 int ir_strdup (Tcl_Interp *interp, char** p, const char *s)
359 *p = malloc (strlen(s)+1);
362 interp->result = "strdup fail";
370 * ir_strdel: Delete string
372 int ir_strdel (Tcl_Interp *interp, char **p)
380 * ir_malloc: Malloc function
382 void *ir_malloc (Tcl_Interp *interp, size_t size)
384 static char buf[128];
385 void *p = malloc (size);
389 sprintf (buf, "Malloc fail. %ld bytes requested", (long) size);
390 interp->result = buf;
396 /* ------------------------------------------------------- */
399 * do_init_request: init method on IR object
401 static int do_init_request (void *obj, Tcl_Interp *interp,
402 int argc, char **argv)
404 Z_APDU apdu, *apdup = &apdu;
413 interp->result = "not connected";
416 odr_reset (p->odr_out);
419 req.options = &p->options;
420 req.protocolVersion = &p->protocolVersion;
421 req.preferredMessageSize = &p->preferredMessageSize;
422 req.maximumRecordSize = &p->maximumRecordSize;
424 if (p->idAuthenticationGroupId)
426 Z_IdPass *pass = odr_malloc (p->odr_out, sizeof(*pass));
427 Z_IdAuthentication *auth = odr_malloc (p->odr_out, sizeof(*auth));
429 auth->which = Z_IdAuthentication_idPass;
430 auth->u.idPass = pass;
431 if (p->idAuthenticationGroupId && *p->idAuthenticationGroupId)
432 pass->groupId = p->idAuthenticationGroupId;
434 pass->groupId = NULL;
435 if (p->idAuthenticationUserId && *p->idAuthenticationUserId)
436 pass->userId = p->idAuthenticationUserId;
439 if (p->idAuthenticationPassword && *p->idAuthenticationPassword)
440 pass->password = p->idAuthenticationPassword;
442 pass->password = NULL;
443 req.idAuthentication = auth;
445 else if (!p->idAuthenticationOpen || !*p->idAuthenticationOpen)
446 req.idAuthentication = NULL;
449 Z_IdAuthentication *auth = odr_malloc (p->odr_out, sizeof(*auth));
451 auth->which = Z_IdAuthentication_open;
452 auth->u.open = p->idAuthenticationOpen;
453 req.idAuthentication = auth;
455 req.implementationId = p->implementationId;
456 req.implementationName = p->implementationName;
457 req.implementationVersion = "0.1";
458 req.userInformationField = 0;
460 apdu.u.initRequest = &req;
461 apdu.which = Z_APDU_initRequest;
463 if (!z_APDU (p->odr_out, &apdup, 0))
465 Tcl_AppendResult (interp, odr_errlist [odr_geterror (p->odr_out)],
467 odr_reset (p->odr_out);
470 p->sbuf = odr_getbuf (p->odr_out, &p->slen, NULL);
471 if ((r=cs_put (p->cs_link, p->sbuf, p->slen)) < 0)
473 interp->result = "cs_put failed in init";
474 do_disconnect (p, NULL, 2, NULL);
479 ir_select_add_write (cs_fileno(p->cs_link), p);
480 logf (LOG_DEBUG, "Sent part of initializeRequest (%d bytes)", p->slen);
483 logf (LOG_DEBUG, "Sent whole initializeRequest (%d bytes)", p->slen);
488 * do_protocolVersion: Set protocol Version
490 static int do_protocolVersion (void *obj, Tcl_Interp *interp,
491 int argc, char **argv)
493 static struct ir_named_entry version_tab[] = {
504 ODR_MASK_ZERO (&p->protocolVersion);
505 ODR_MASK_SET (&p->protocolVersion, 0);
506 ODR_MASK_SET (&p->protocolVersion, 1);
509 return ir_named_bits (version_tab, &p->protocolVersion,
510 interp, argc-2, argv+2);
514 * do_options: Set options
516 static int do_options (void *obj, Tcl_Interp *interp,
517 int argc, char **argv)
519 static struct ir_named_entry options_tab[] = {
523 { "resourceReport", 3 },
524 { "triggerResourceCtrl", 4},
525 { "resourceCtrl", 5},
529 { "extentedServices", 10},
530 { "level-1Segmentation", 11},
531 { "level-2Segmentation", 12},
532 { "concurrentOperations", 13},
533 { "namedResultSets", 14},
540 ODR_MASK_ZERO (&p->options);
541 ODR_MASK_SET (&p->options, 0);
542 ODR_MASK_SET (&p->options, 1);
543 ODR_MASK_SET (&p->options, 7);
544 ODR_MASK_SET (&p->options, 14);
547 return ir_named_bits (options_tab, &p->options, interp, argc-2, argv+2);
551 * do_preferredMessageSize: Set/get preferred message size
553 static int do_preferredMessageSize (void *obj, Tcl_Interp *interp,
554 int argc, char **argv)
560 p->preferredMessageSize = 4096;
563 return get_set_int (&p->preferredMessageSize, interp, argc, argv);
567 * do_maximumRecordSize: Set/get maximum record size
569 static int do_maximumRecordSize (void *obj, Tcl_Interp *interp,
570 int argc, char **argv)
576 p->maximumRecordSize = 32768;
579 return get_set_int (&p->maximumRecordSize, interp, argc, argv);
583 * do_initResult: Get init result
585 static int do_initResult (void *obj, Tcl_Interp *interp,
586 int argc, char **argv)
592 return get_set_int (&p->initResult, interp, argc, argv);
597 * do_implementationName: Set/get Implementation Name.
599 static int do_implementationName (void *obj, Tcl_Interp *interp,
600 int argc, char **argv)
605 return ir_strdup (interp, &p->implementationName, "TCL/TK on YAZ");
607 return ir_strdel (interp, &p->implementationName);
610 free (p->implementationName);
611 if (ir_strdup (interp, &p->implementationName, argv[2])
615 Tcl_AppendResult (interp, p->implementationName,
621 * do_implementationId: Set/get Implementation Id.
623 static int do_implementationId (void *obj, Tcl_Interp *interp,
624 int argc, char **argv)
629 return ir_strdup (interp, &p->implementationId, "81");
631 return ir_strdel (interp, &p->implementationId);
634 free (p->implementationId);
635 if (ir_strdup (interp, &p->implementationId, argv[2]) == TCL_ERROR)
638 Tcl_AppendResult (interp, p->implementationId, (char*) NULL);
643 * do_targetImplementationName: Get Implementation Name of target.
645 static int do_targetImplementationName (void *obj, Tcl_Interp *interp,
646 int argc, char **argv)
652 p->targetImplementationName = NULL;
656 return ir_strdel (interp, &p->targetImplementationName);
657 Tcl_AppendResult (interp, p->targetImplementationName,
663 * do_targetImplementationId: Get Implementation Id of target
665 static int do_targetImplementationId (void *obj, Tcl_Interp *interp,
666 int argc, char **argv)
672 p->targetImplementationId = NULL;
676 return ir_strdel (interp, &p->targetImplementationId);
677 Tcl_AppendResult (interp, p->targetImplementationId, (char*) NULL);
682 * do_targetImplementationVersion: Get Implementation Version of target
684 static int do_targetImplementationVersion (void *obj, Tcl_Interp *interp,
685 int argc, char **argv)
691 p->targetImplementationVersion = NULL;
695 return ir_strdel (interp, &p->targetImplementationVersion);
696 Tcl_AppendResult (interp, p->targetImplementationVersion, (char*) NULL);
701 * do_idAuthentication: Set/get id Authentication
703 static int do_idAuthentication (void *obj, Tcl_Interp *interp,
704 int argc, char **argv)
708 if (argc >= 3 || argc == -1)
710 free (p->idAuthenticationOpen);
711 free (p->idAuthenticationGroupId);
712 free (p->idAuthenticationUserId);
713 free (p->idAuthenticationPassword);
715 if (argc >= 3 || argc <= 0)
717 p->idAuthenticationOpen = NULL;
718 p->idAuthenticationGroupId = NULL;
719 p->idAuthenticationUserId = NULL;
720 p->idAuthenticationPassword = NULL;
728 if (ir_strdup (interp, &p->idAuthenticationOpen, argv[2])
734 if (ir_strdup (interp, &p->idAuthenticationGroupId, argv[2])
737 if (ir_strdup (interp, &p->idAuthenticationUserId, argv[3])
740 if (ir_strdup (interp, &p->idAuthenticationPassword, argv[4])
745 if (p->idAuthenticationOpen)
746 Tcl_AppendElement (interp, p->idAuthenticationOpen);
747 else if (p->idAuthenticationGroupId)
749 Tcl_AppendElement (interp, p->idAuthenticationGroupId);
750 Tcl_AppendElement (interp, p->idAuthenticationUserId);
751 Tcl_AppendElement (interp, p->idAuthenticationPassword);
757 * do_connect: connect method on IR object
759 static int do_connect (void *obj, Tcl_Interp *interp,
760 int argc, char **argv)
765 int protocol_type = PROTO_Z3950;
773 interp->result = "already connected";
776 if (!strcmp (p->protocol_type, "Z3950"))
777 protocol_type = PROTO_Z3950;
778 else if (!strcmp (p->protocol_type, "SR"))
779 protocol_type = PROTO_SR;
782 interp->result = "bad protocol type";
785 if (!strcmp (p->cs_type, "tcpip"))
787 p->cs_link = cs_create (tcpip_type, CS_BLOCK, protocol_type);
788 addr = tcpip_strtoaddr (argv[2]);
791 interp->result = "tcpip_strtoaddr fail";
794 logf (LOG_DEBUG, "tcp/ip connect %s", argv[2]);
797 else if (!strcmp (p->cs_type, "mosi"))
799 p->cs_link = cs_create (mosi_type, CS_BLOCK, protocol_type);
800 addr = mosi_strtoaddr (argv[2]);
803 interp->result = "mosi_strtoaddr fail";
806 logf (LOG_DEBUG, "mosi connect %s", argv[2]);
811 interp->result = "unknown comstack type";
814 if (ir_strdup (interp, &p->hostname, argv[2]) == TCL_ERROR)
816 if ((r=cs_connect (p->cs_link, addr)) < 0)
818 interp->result = "cs_connect fail";
819 do_disconnect (p, NULL, 2, NULL);
822 ir_select_add (cs_fileno (p->cs_link), p);
825 ir_select_add_write (cs_fileno (p->cs_link), p);
832 Tcl_Eval (p->interp, p->callback);
836 Tcl_AppendElement (interp, p->hostname);
841 * do_disconnect: disconnect method on IR object
843 static int do_disconnect (void *obj, Tcl_Interp *interp,
844 int argc, char **argv)
859 ir_select_remove_write (cs_fileno (p->cs_link), p);
860 ir_select_remove (cs_fileno (p->cs_link), p);
863 cs_close (p->cs_link);
866 assert (!p->cs_link);
871 * do_comstack: Set/get comstack method on IR object
873 static int do_comstack (void *o, Tcl_Interp *interp,
874 int argc, char **argv)
879 return ir_strdup (interp, &obj->cs_type, "tcpip");
881 return ir_strdel (interp, &obj->cs_type);
885 if (ir_strdup (interp, &obj->cs_type, argv[2]) == TCL_ERROR)
888 Tcl_AppendElement (interp, obj->cs_type);
893 * do_protocol: Set/get protocol method on IR object
895 static int do_protocol (void *o, Tcl_Interp *interp,
896 int argc, char **argv)
901 return ir_strdup (interp, &obj->protocol_type, "Z3950");
903 return ir_strdel (interp, &obj->protocol_type);
906 free (obj->protocol_type);
907 if (ir_strdup (interp, &obj->protocol_type, argv[2]) == TCL_ERROR)
910 Tcl_AppendElement (interp, obj->protocol_type);
915 * do_callback: add callback
917 static int do_callback (void *obj, Tcl_Interp *interp,
918 int argc, char **argv)
928 return ir_strdel (interp, &p->callback);
934 if (ir_strdup (interp, &p->callback, argv[2]) == TCL_ERROR)
945 * do_failback: add error handle callback
947 static int do_failback (void *obj, Tcl_Interp *interp,
948 int argc, char **argv)
958 return ir_strdel (interp, &p->failback);
964 if (ir_strdup (interp, &p->failback, argv[2]) == TCL_ERROR)
975 * do_databaseNames: specify database names
977 static int do_databaseNames (void *obj, Tcl_Interp *interp,
978 int argc, char **argv)
985 for (i=0; i<p->num_databaseNames; i++)
986 free (p->databaseNames[i]);
987 free (p->databaseNames);
991 p->num_databaseNames = 0;
992 p->databaseNames = NULL;
997 for (i=0; i<p->num_databaseNames; i++)
998 Tcl_AppendElement (interp, p->databaseNames[i]);
1001 if (p->databaseNames)
1003 for (i=0; i<p->num_databaseNames; i++)
1004 free (p->databaseNames[i]);
1005 free (p->databaseNames);
1007 p->num_databaseNames = argc - 2;
1008 if (!(p->databaseNames = ir_malloc (interp,
1009 sizeof(*p->databaseNames) * p->num_databaseNames)))
1011 for (i=0; i<p->num_databaseNames; i++)
1013 if (ir_strdup (interp, &p->databaseNames[i], argv[2+i])
1021 * do_replaceIndicator: Set/get replace Set indicator
1023 static int do_replaceIndicator (void *obj, Tcl_Interp *interp,
1024 int argc, char **argv)
1030 p->replaceIndicator = 1;
1033 return get_set_int (&p->replaceIndicator, interp, argc, argv);
1037 * do_queryType: Set/Get query method
1039 static int do_queryType (void *obj, Tcl_Interp *interp,
1040 int argc, char **argv)
1045 return ir_strdup (interp, &p->queryType, "rpn");
1046 else if (argc == -1)
1047 return ir_strdel (interp, &p->queryType);
1050 free (p->queryType);
1051 if (ir_strdup (interp, &p->queryType, argv[2]) == TCL_ERROR)
1054 Tcl_AppendResult (interp, p->queryType, NULL);
1059 * do_userInformationField: Get User information field
1061 static int do_userInformationField (void *obj, Tcl_Interp *interp,
1062 int argc, char **argv)
1068 p->userInformationField = NULL;
1071 else if (argc == -1)
1072 return ir_strdel (interp, &p->userInformationField);
1073 Tcl_AppendResult (interp, p->userInformationField, NULL);
1078 * do_smallSetUpperBound: Set/get small set upper bound
1080 static int do_smallSetUpperBound (void *o, Tcl_Interp *interp,
1081 int argc, char **argv)
1087 p->smallSetUpperBound = 0;
1090 return get_set_int (&p->smallSetUpperBound, interp, argc, argv);
1094 * do_largeSetLowerBound: Set/get large set lower bound
1096 static int do_largeSetLowerBound (void *o, Tcl_Interp *interp,
1097 int argc, char **argv)
1103 p->largeSetLowerBound = 2;
1106 return get_set_int (&p->largeSetLowerBound, interp, argc, argv);
1110 * do_mediumSetPresentNumber: Set/get large set lower bound
1112 static int do_mediumSetPresentNumber (void *o, Tcl_Interp *interp,
1113 int argc, char **argv)
1119 p->mediumSetPresentNumber = 0;
1122 return get_set_int (&p->mediumSetPresentNumber, interp, argc, argv);
1126 static IRMethod ir_method_tab[] = {
1127 { 1, "comstack", do_comstack },
1128 { 1, "protocol", do_protocol },
1129 { 0, "failback", do_failback },
1131 { 1, "connect", do_connect },
1132 { 0, "protocolVersion", do_protocolVersion },
1133 { 1, "preferredMessageSize", do_preferredMessageSize },
1134 { 1, "maximumRecordSize", do_maximumRecordSize },
1135 { 1, "implementationName", do_implementationName },
1136 { 1, "implementationId", do_implementationId },
1137 { 0, "targetImplementationName", do_targetImplementationName },
1138 { 0, "targetImplementationId", do_targetImplementationId },
1139 { 0, "targetImplementationVersion", do_targetImplementationVersion },
1140 { 0, "userInformationField", do_userInformationField },
1141 { 1, "idAuthentication", do_idAuthentication },
1142 { 0, "options", do_options },
1143 { 0, "init", do_init_request },
1144 { 0, "initResult", do_initResult },
1145 { 0, "disconnect", do_disconnect },
1146 { 0, "callback", do_callback },
1150 static IRMethod ir_set_c_method_tab[] = {
1151 { 0, "databaseNames", do_databaseNames},
1152 { 0, "replaceIndicator", do_replaceIndicator},
1153 { 0, "queryType", do_queryType },
1154 { 0, "smallSetUpperBound", do_smallSetUpperBound},
1155 { 0, "largeSetLowerBound", do_largeSetLowerBound},
1156 { 0, "mediumSetPresentNumber", do_mediumSetPresentNumber},
1161 * ir_obj_method: IR Object methods
1163 static int ir_obj_method (ClientData clientData, Tcl_Interp *interp,
1164 int argc, char **argv)
1167 IRObj *p = clientData;
1170 return ir_method_r (clientData, interp, argc, argv, ir_method_tab);
1172 tab[0].tab = ir_method_tab;
1174 tab[1].tab = ir_set_c_method_tab;
1175 tab[1].obj = &p->set_inher;
1178 return ir_method (interp, argc, argv, tab);
1182 * ir_obj_delete: IR Object disposal
1184 static void ir_obj_delete (ClientData clientData)
1186 IRObj *obj = clientData;
1190 if (obj->ref_count > 0)
1192 assert (obj->ref_count == 0);
1194 tab[0].tab = ir_method_tab;
1196 tab[1].tab = ir_set_c_method_tab;
1197 tab[1].obj = &obj->set_inher;
1200 ir_method (NULL, -1, NULL, tab);
1201 odr_destroy (obj->odr_in);
1202 odr_destroy (obj->odr_out);
1203 odr_destroy (obj->odr_pr);
1204 free (obj->buf_out);
1210 * ir_obj_mk: IR Object creation
1212 static int ir_obj_mk (ClientData clientData, Tcl_Interp *interp,
1213 int argc, char **argv)
1223 interp->result = "wrong # args";
1226 if (!(obj = ir_malloc (interp, sizeof(*obj))))
1231 obj->bibset = ccl_qual_mk ();
1232 if ((inf = fopen ("default.bib", "r")))
1234 ccl_qual_file (obj->bibset, inf);
1239 obj->odr_in = odr_createmem (ODR_DECODE);
1240 obj->odr_out = odr_createmem (ODR_ENCODE);
1241 obj->odr_pr = odr_createmem (ODR_PRINT);
1243 obj->len_out = 10000;
1244 if (!(obj->buf_out = ir_malloc (interp, obj->len_out)))
1246 odr_setbuf (obj->odr_out, obj->buf_out, obj->len_out, 0);
1251 tab[0].tab = ir_method_tab;
1253 tab[1].tab = ir_set_c_method_tab;
1254 tab[1].obj = &obj->set_inher;
1257 if (ir_method (interp, 0, NULL, tab) == TCL_ERROR)
1259 Tcl_CreateCommand (interp, argv[1], ir_obj_method,
1260 (ClientData) obj, ir_obj_delete);
1264 /* ------------------------------------------------------- */
1266 * do_search: Do search request
1268 static int do_search (void *o, Tcl_Interp *interp,
1269 int argc, char **argv)
1271 Z_SearchRequest req;
1273 Z_APDU apdu, *apdup = &apdu;
1276 IRObj *p = obj->parent;
1286 interp->result = "wrong # args";
1289 if (!p->set_inher.num_databaseNames)
1291 interp->result = "no databaseNames";
1296 interp->result = "not connected";
1299 odr_reset (p->odr_out);
1300 apdu.which = Z_APDU_searchRequest;
1301 apdu.u.searchRequest = &req;
1303 bib1.proto = PROTO_Z3950;
1304 bib1.class = CLASS_ATTSET;
1305 bib1.value = VAL_BIB1;
1307 req.referenceId = 0;
1308 req.smallSetUpperBound = &p->set_inher.smallSetUpperBound;
1309 req.largeSetLowerBound = &p->set_inher.largeSetLowerBound;
1310 req.mediumSetPresentNumber = &p->set_inher.mediumSetPresentNumber;
1311 req.replaceIndicator = &p->set_inher.replaceIndicator;
1312 req.resultSetName = obj->setName ? obj->setName : "Default";
1313 logf (LOG_DEBUG, "Search, resultSetName %s", req.resultSetName);
1314 req.num_databaseNames = p->set_inher.num_databaseNames;
1315 req.databaseNames = p->set_inher.databaseNames;
1316 for (r=0; r < p->set_inher.num_databaseNames; r++)
1317 logf (LOG_DEBUG, " Database %s", p->set_inher.databaseNames[r]);
1318 req.smallSetElementSetNames = 0;
1319 req.mediumSetElementSetNames = 0;
1320 req.preferredRecordSyntax = 0;
1323 if (!strcmp (p->set_inher.queryType, "rpn"))
1325 Z_RPNQuery *RPNquery;
1327 RPNquery = p_query_rpn (p->odr_out, argv[2]);
1330 Tcl_AppendResult (interp, "Syntax error in query", NULL);
1333 RPNquery->attributeSetId = oid_getoidbyent (&bib1);
1334 query.which = Z_Query_type_1;
1335 query.u.type_1 = RPNquery;
1336 logf (LOG_DEBUG, "RPN");
1339 else if (!strcmp (p->set_inher.queryType, "cclrpn"))
1343 struct ccl_rpn_node *rpn;
1344 Z_RPNQuery *RPNquery;
1346 rpn = ccl_find_str(p->bibset, argv[2], &error, &pos);
1349 Tcl_AppendResult (interp, "CCL error: ", ccl_err_msg(error),NULL);
1352 ccl_pr_tree (rpn, stderr);
1353 fprintf (stderr, "\n");
1354 assert((RPNquery = ccl_rpn_query(rpn)));
1355 RPNquery->attributeSetId = oid_getoidbyent (&bib1);
1356 query.which = Z_Query_type_1;
1357 query.u.type_1 = RPNquery;
1358 logf (LOG_DEBUG, "CCLRPN");
1361 else if (!strcmp (p->set_inher.queryType, "ccl"))
1363 query.which = Z_Query_type_2;
1364 query.u.type_2 = &ccl_query;
1365 ccl_query.buf = (unsigned char *) argv[2];
1366 ccl_query.len = strlen (argv[2]);
1367 logf (LOG_DEBUG, "CCL");
1371 interp->result = "unknown query method";
1374 if (!z_APDU (p->odr_out, &apdup, 0))
1376 interp->result = odr_errlist [odr_geterror (p->odr_out)];
1377 odr_reset (p->odr_out);
1380 p->sbuf = odr_getbuf (p->odr_out, &p->slen, NULL);
1381 if ((r=cs_put (p->cs_link, p->sbuf, p->slen)) < 0)
1383 interp->result = "cs_put failed in search";
1388 ir_select_add_write (cs_fileno(p->cs_link), p);
1389 logf (LOG_DEBUG, "Sent part of searchRequest (%d bytes)", p->slen);
1393 logf (LOG_DEBUG, "Whole search request (%d bytes)", p->slen);
1399 * do_resultCount: Get number of hits
1401 static int do_resultCount (void *o, Tcl_Interp *interp,
1402 int argc, char **argv)
1408 return get_set_int (&obj->resultCount, interp, argc, argv);
1412 * do_searchStatus: Get search status (after search response)
1414 static int do_searchStatus (void *o, Tcl_Interp *interp,
1415 int argc, char **argv)
1421 return get_set_int (&obj->searchStatus, interp, argc, argv);
1425 * do_presentStatus: Get search status (after search/present response)
1427 static int do_presentStatus (void *o, Tcl_Interp *interp,
1428 int argc, char **argv)
1434 return get_set_int (&obj->presentStatus, interp, argc, argv);
1438 * do_nextResultSetPosition: Get next result set position
1439 * (after search/present response)
1441 static int do_nextResultSetPosition (void *o, Tcl_Interp *interp,
1442 int argc, char **argv)
1448 return get_set_int (&obj->nextResultSetPosition, interp, argc, argv);
1452 * do_setName: Set result Set name
1454 static int do_setName (void *o, Tcl_Interp *interp,
1455 int argc, char **argv)
1460 return ir_strdup (interp, &obj->setName, "Default");
1461 else if (argc == -1)
1462 return ir_strdel (interp, &obj->setName);
1465 free (obj->setName);
1466 if (ir_strdup (interp, &obj->setName, argv[2])
1470 Tcl_AppendElement (interp, obj->setName);
1475 * do_numberOfRecordsReturned: Get number of records returned
1477 static int do_numberOfRecordsReturned (void *o, Tcl_Interp *interp,
1478 int argc, char **argv)
1484 return get_set_int (&obj->numberOfRecordsReturned, interp, argc, argv);
1488 * do_recordType: Return record type (if any) at position.
1490 static int do_recordType (void *o, Tcl_Interp *interp, int argc, char **argv)
1498 obj->record_list = NULL;
1501 else if (argc == -1)
1503 delete_IR_records (obj);
1508 sprintf (interp->result, "wrong # args");
1511 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
1513 rl = find_IR_record (obj, offset);
1518 case Z_NamePlusRecord_databaseRecord:
1519 interp->result = "databaseRecord";
1521 case Z_NamePlusRecord_surrogateDiagnostic:
1522 interp->result = "surrogateDiagnostic";
1529 * do_recordDiag: Return diagnostic record info
1531 static int do_recordDiag (void *o, Tcl_Interp *interp, int argc, char **argv)
1542 sprintf (interp->result, "wrong # args");
1545 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
1547 rl = find_IR_record (obj, offset);
1550 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
1553 if (rl->which != Z_NamePlusRecord_surrogateDiagnostic)
1555 Tcl_AppendResult (interp, "No Diagnostic record at #", argv[2], NULL);
1558 sprintf (buf, "%d", rl->u.diag.condition);
1559 Tcl_AppendResult (interp, buf, " {",
1560 (rl->u.diag.addinfo ? rl->u.diag.addinfo : ""),
1566 * do_getMarc: Get ISO2709 Record lines/fields
1568 static int do_getMarc (void *o, Tcl_Interp *interp, int argc, char **argv)
1578 sprintf (interp->result, "wrong # args");
1581 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
1583 rl = find_IR_record (obj, offset);
1586 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
1589 if (rl->which != Z_NamePlusRecord_databaseRecord)
1591 Tcl_AppendResult (interp, "No MARC record at #", argv[2], NULL);
1594 return ir_tcl_get_marc (interp, rl->u.dbrec.buf, argc, argv);
1599 * do_responseStatus: Return response status (present or search)
1601 static int do_responseStatus (void *o, Tcl_Interp *interp,
1602 int argc, char **argv)
1608 obj->recordFlag = 0;
1609 obj->addinfo = NULL;
1612 else if (argc == -1)
1613 return ir_strdel (interp, &obj->addinfo);
1614 if (!obj->recordFlag)
1616 Tcl_AppendElement (interp, "OK");
1621 case Z_Records_DBOSD:
1622 Tcl_AppendElement (interp, "DBOSD");
1625 return mk_nonSurrogateDiagnostics (interp, obj->condition,
1632 * do_present: Perform Present Request
1635 static int do_present (void *o, Tcl_Interp *interp,
1636 int argc, char **argv)
1639 IRObj *p = obj->parent;
1640 Z_APDU apdu, *apdup = &apdu;
1641 Z_PresentRequest req;
1650 if (Tcl_GetInt (interp, argv[2], &start) == TCL_ERROR)
1657 if (Tcl_GetInt (interp, argv[3], &number) == TCL_ERROR)
1664 interp->result = "not connected";
1667 odr_reset (p->odr_out);
1669 obj->number = number;
1671 apdu.which = Z_APDU_presentRequest;
1672 apdu.u.presentRequest = &req;
1673 req.referenceId = 0;
1674 /* sprintf(setstring, "%d", setnumber); */
1676 req.resultSetId = obj->setName ? obj->setName : "Default";
1678 req.resultSetStartPoint = &start;
1679 req.numberOfRecordsRequested = &number;
1680 req.elementSetNames = 0;
1681 req.preferredRecordSyntax = 0;
1683 if (!z_APDU (p->odr_out, &apdup, 0))
1685 interp->result = odr_errlist [odr_geterror (p->odr_out)];
1686 odr_reset (p->odr_out);
1689 p->sbuf = odr_getbuf (p->odr_out, &p->slen, NULL);
1690 if ((r=cs_put (p->cs_link, p->sbuf, p->slen)) < 0)
1692 interp->result = "cs_put failed in present";
1697 ir_select_add_write (cs_fileno(p->cs_link), p);
1698 logf (LOG_DEBUG, "Part of present request, start=%d, num=%d"
1699 " (%d bytes)", start, number, p->slen);
1703 logf (LOG_DEBUG, "Whole present request, start=%d, num=%d"
1704 " (%d bytes)", start, number, p->slen);
1710 * do_loadFile: Load result set from file
1713 static int do_loadFile (void *o, Tcl_Interp *interp,
1714 int argc, char **argv)
1716 IRSetObj *setobj = o;
1726 interp->result = "wrong # args";
1729 inf = fopen (argv[2], "r");
1732 Tcl_AppendResult (interp, "Cannot open ", argv[2], NULL);
1735 while ((buf = ir_tcl_fread_marc (inf, &size)))
1739 rl = new_IR_record (setobj, no, Z_NamePlusRecord_databaseRecord);
1740 rl->u.dbrec.buf = buf;
1741 rl->u.dbrec.size = size;
1744 setobj->numberOfRecordsReturned = no-1;
1749 static IRMethod ir_set_method_tab[] = {
1750 { 0, "search", do_search },
1751 { 0, "searchStatus", do_searchStatus },
1752 { 0, "presentStatus", do_presentStatus },
1753 { 0, "nextResultSetPosition", do_nextResultSetPosition },
1754 { 0, "setName", do_setName },
1755 { 0, "resultCount", do_resultCount },
1756 { 0, "numberOfRecordsReturned", do_numberOfRecordsReturned },
1757 { 0, "present", do_present },
1758 { 0, "recordType", do_recordType },
1759 { 0, "getMarc", do_getMarc },
1760 { 0, "Diag", do_recordDiag },
1761 { 0, "responseStatus", do_responseStatus },
1762 { 0, "loadFile", do_loadFile },
1767 * ir_set_obj_method: IR Set Object methods
1769 static int ir_set_obj_method (ClientData clientData, Tcl_Interp *interp,
1770 int argc, char **argv)
1773 IRSetObj *p = clientData;
1777 interp->result = "wrong # args";
1780 tabs[0].tab = ir_set_method_tab;
1782 tabs[1].tab = ir_set_c_method_tab;
1783 tabs[1].obj = &p->set_inher;
1786 return ir_method (interp, argc, argv, tabs);
1790 * ir_set_obj_delete: IR Set Object disposal
1792 static void ir_set_obj_delete (ClientData clientData)
1795 IRSetObj *p = clientData;
1797 tabs[0].tab = ir_set_method_tab;
1799 tabs[1].tab = ir_set_c_method_tab;
1800 tabs[1].obj = &p->set_inher;
1803 ir_method (NULL, -1, NULL, tabs);
1809 * ir_set_obj_mk: IR Set Object creation
1811 static int ir_set_obj_mk (ClientData clientData, Tcl_Interp *interp,
1812 int argc, char **argv)
1817 if (argc < 2 || argc > 3)
1819 interp->result = "wrong # args";
1824 Tcl_CmdInfo parent_info;
1829 if (!Tcl_GetCommandInfo (interp, argv[2], &parent_info))
1831 interp->result = "No parent";
1834 if (!(obj = ir_malloc (interp, sizeof(*obj))))
1836 obj->parent = (IRObj *) parent_info.clientData;
1838 dst = &obj->set_inher;
1839 src = &obj->parent->set_inher;
1841 dst->num_databaseNames = src->num_databaseNames;
1842 if (!(dst->databaseNames =
1843 ir_malloc (interp, sizeof (*dst->databaseNames)
1844 * dst->num_databaseNames)))
1846 for (i = 0; i < dst->num_databaseNames; i++)
1848 if (ir_strdup (interp, &dst->databaseNames[i],
1849 src->databaseNames[i]) == TCL_ERROR)
1852 if (ir_strdup (interp, &dst->queryType, src->queryType)
1856 dst->smallSetUpperBound = src->smallSetUpperBound;
1857 dst->largeSetLowerBound = src->largeSetLowerBound;
1858 dst->mediumSetPresentNumber = src->mediumSetPresentNumber;
1863 tabs[0].tab = ir_set_method_tab;
1867 if (ir_method (interp, 0, NULL, tabs) == TCL_ERROR)
1870 Tcl_CreateCommand (interp, argv[1], ir_set_obj_method,
1871 (ClientData) obj, ir_set_obj_delete);
1875 /* ------------------------------------------------------- */
1878 * do_scan: Perform scan
1880 static int do_scan (void *o, Tcl_Interp *interp, int argc, char **argv)
1883 Z_APDU apdu, *apdup = &apdu;
1885 IRObj *p = obj->parent;
1889 struct ccl_rpn_node *rpn;
1898 interp->result = "wrong # args";
1901 if (!p->set_inher.num_databaseNames)
1903 interp->result = "no databaseNames";
1908 interp->result = "not connected";
1911 odr_reset (p->odr_out);
1913 bib1.proto = PROTO_Z3950;
1914 bib1.class = CLASS_ATTSET;
1915 bib1.value = VAL_BIB1;
1917 apdu.which = Z_APDU_scanRequest;
1918 apdu.u.scanRequest = &req;
1919 req.referenceId = NULL;
1920 req.num_databaseNames = p->set_inher.num_databaseNames;
1921 req.databaseNames = p->set_inher.databaseNames;
1922 req.attributeSet = oid_getoidbyent (&bib1);
1925 if (!(req.termListAndStartPoint = p_query_scan (p->odr_out, argv[2])))
1927 Tcl_AppendResult (interp, "Syntax error in query", NULL);
1931 rpn = ccl_find_str(p->bibset, argv[2], &r, &pos);
1934 Tcl_AppendResult (interp, "CCL error: ", ccl_err_msg (r), NULL);
1937 ccl_pr_tree (rpn, stderr);
1938 fprintf (stderr, "\n");
1939 if (!(req.termListAndStartPoint = ccl_scan_query (rpn)))
1942 req.stepSize = &obj->stepSize;
1943 req.numberOfTermsRequested = &obj->numberOfTermsRequested;
1944 req.preferredPositionInResponse = &obj->preferredPositionInResponse;
1945 logf (LOG_DEBUG, "stepSize=%d", *req.stepSize);
1946 logf (LOG_DEBUG, "numberOfTermsRequested=%d",
1947 *req.numberOfTermsRequested);
1948 logf (LOG_DEBUG, "preferredPositionInResponse=%d",
1949 *req.preferredPositionInResponse);
1951 if (!z_APDU (p->odr_out, &apdup, 0))
1953 interp->result = odr_errlist [odr_geterror (p->odr_out)];
1954 odr_reset (p->odr_out);
1957 p->sbuf = odr_getbuf (p->odr_out, &p->slen, NULL);
1958 if ((r=cs_put (p->cs_link, p->sbuf, p->slen)) < 0)
1960 interp->result = "cs_put failed in scan";
1965 ir_select_add_write (cs_fileno(p->cs_link), p);
1966 logf (LOG_DEBUG, "Sent part of scanRequest (%d bytes)", p->slen);
1970 logf (LOG_DEBUG, "Whole scan request (%d bytes)", p->slen);
1976 * do_stepSize: Set/get replace Step Size
1978 static int do_stepSize (void *obj, Tcl_Interp *interp,
1979 int argc, char **argv)
1987 return get_set_int (&p->stepSize, interp, argc, argv);
1991 * do_numberOfTermsRequested: Set/get Number of Terms requested
1993 static int do_numberOfTermsRequested (void *obj, Tcl_Interp *interp,
1994 int argc, char **argv)
2000 p->numberOfTermsRequested = 20;
2003 return get_set_int (&p->numberOfTermsRequested, interp, argc, argv);
2008 * do_preferredPositionInResponse: Set/get preferred Position
2010 static int do_preferredPositionInResponse (void *obj, Tcl_Interp *interp,
2011 int argc, char **argv)
2017 p->preferredPositionInResponse = 1;
2020 return get_set_int (&p->preferredPositionInResponse, interp, argc, argv);
2024 * do_scanStatus: Get scan status
2026 static int do_scanStatus (void *obj, Tcl_Interp *interp,
2027 int argc, char **argv)
2033 return get_set_int (&p->scanStatus, interp, argc, argv);
2037 * do_numberOfEntriesReturned: Get number of Entries returned
2039 static int do_numberOfEntriesReturned (void *obj, Tcl_Interp *interp,
2040 int argc, char **argv)
2046 return get_set_int (&p->numberOfEntriesReturned, interp, argc, argv);
2050 * do_positionOfTerm: Get position of Term
2052 static int do_positionOfTerm (void *obj, Tcl_Interp *interp,
2053 int argc, char **argv)
2059 return get_set_int (&p->positionOfTerm, interp, argc, argv);
2063 * do_scanLine: get Scan Line (surrogate or normal) after response
2065 static int do_scanLine (void *obj, Tcl_Interp *interp, int argc, char **argv)
2073 p->entries_flag = 0;
2075 p->nonSurrogateDiagnostics = NULL;
2078 else if (argc == -1)
2080 p->entries_flag = 0;
2081 /* release entries */
2083 /* release non diagnostics */
2084 p->nonSurrogateDiagnostics = NULL;
2089 interp->result = "wrong # args";
2092 if (Tcl_GetInt (interp, argv[2], &i) == TCL_ERROR)
2094 if (!p->entries_flag || p->which != Z_ListEntries_entries || !p->entries
2095 || i >= p->num_entries || i < 0)
2097 switch (p->entries[i].which)
2099 case Z_Entry_termInfo:
2100 Tcl_AppendElement (interp, "T");
2101 if (p->entries[i].u.term.buf)
2102 Tcl_AppendElement (interp, p->entries[i].u.term.buf);
2104 Tcl_AppendElement (interp, "");
2105 sprintf (numstr, "%d", p->entries[i].u.term.globalOccurrences);
2106 Tcl_AppendElement (interp, numstr);
2108 case Z_Entry_surrogateDiagnostic:
2110 mk_nonSurrogateDiagnostics (interp, p->entries[i].u.diag.condition,
2111 p->entries[i].u.diag.addinfo);
2117 static IRMethod ir_scan_method_tab[] = {
2118 { 0, "scan", do_scan },
2119 { 0, "stepSize", do_stepSize },
2120 { 0, "numberOfTermsRequested", do_numberOfTermsRequested },
2121 { 0, "preferredPositionInResponse", do_preferredPositionInResponse },
2122 { 0, "scanStatus", do_scanStatus },
2123 { 0, "numberOfEntriesReturned", do_numberOfEntriesReturned },
2124 { 0, "positionOfTerm", do_positionOfTerm },
2125 { 0, "scanLine", do_scanLine },
2130 * ir_scan_obj_method: IR Scan Object methods
2132 static int ir_scan_obj_method (ClientData clientData, Tcl_Interp *interp,
2133 int argc, char **argv)
2139 interp->result = "wrong # args";
2142 tabs[0].tab = ir_scan_method_tab;
2143 tabs[0].obj = clientData;
2146 return ir_method (interp, argc, argv, tabs);
2150 * ir_scan_obj_delete: IR Scan Object disposal
2152 static void ir_scan_obj_delete (ClientData clientData)
2154 free ( (void*) clientData);
2158 * ir_scan_obj_mk: IR Scan Object creation
2160 static int ir_scan_obj_mk (ClientData clientData, Tcl_Interp *interp,
2161 int argc, char **argv)
2163 Tcl_CmdInfo parent_info;
2169 interp->result = "wrong # args";
2172 if (get_parent_info (interp, argv[1], &parent_info, NULL) == TCL_ERROR)
2174 if (!(obj = ir_malloc (interp, sizeof(*obj))))
2177 tabs[0].tab = ir_scan_method_tab;
2178 tabs[0].obj = clientData;
2181 if (ir_method (interp, 0, NULL, tabs) == TCL_ERROR)
2185 obj->numberOfTermsRequested = 20;
2186 obj->preferredPositionInResponse = 1;
2188 obj->entries = NULL;
2189 obj->nonSurrogateDiagnostics = NULL;
2192 obj->parent = (IRObj *) parent_info.clientData;
2193 Tcl_CreateCommand (interp, argv[1], ir_scan_obj_method,
2194 (ClientData) obj, ir_scan_obj_delete);
2198 /* ------------------------------------------------------- */
2200 static void ir_initResponse (void *obj, Z_InitResponse *initrs)
2204 p->initResult = *initrs->result ? 1 : 0;
2205 if (!*initrs->result)
2206 logf (LOG_DEBUG, "Connection rejected by target");
2208 logf (LOG_DEBUG, "Connection accepted by target");
2210 free (p->targetImplementationId);
2211 ir_strdup (p->interp, &p->targetImplementationId,
2212 initrs->implementationId);
2213 free (p->targetImplementationName);
2214 ir_strdup (p->interp, &p->targetImplementationName,
2215 initrs->implementationName);
2216 free (p->targetImplementationVersion);
2217 ir_strdup (p->interp, &p->targetImplementationVersion,
2218 initrs->implementationVersion);
2220 p->maximumRecordSize = *initrs->maximumRecordSize;
2221 p->preferredMessageSize = *initrs->preferredMessageSize;
2223 memcpy (&p->options, initrs->options, sizeof(initrs->options));
2224 memcpy (&p->protocolVersion, initrs->protocolVersion,
2225 sizeof(initrs->protocolVersion));
2226 free (p->userInformationField);
2227 p->userInformationField = NULL;
2228 if (initrs->userInformationField)
2232 if (initrs->userInformationField->which == ODR_EXTERNAL_octet &&
2233 (p->userInformationField =
2235 initrs->userInformationField->u.octet_aligned->len)
2238 memcpy (p->userInformationField,
2239 initrs->userInformationField->u.octet_aligned->buf,
2241 (p->userInformationField)[len] = '\0';
2246 static void ir_handleRecords (void *o, Z_Records *zrs)
2249 IRSetObj *setobj = p->set_child;
2251 setobj->which = zrs->which;
2252 setobj->recordFlag = 1;
2253 if (zrs->which == Z_Records_NSD)
2255 const char *addinfo;
2257 setobj->numberOfRecordsReturned = 0;
2258 setobj->condition = *zrs->u.nonSurrogateDiagnostic->condition;
2259 free (setobj->addinfo);
2260 setobj->addinfo = NULL;
2261 addinfo = zrs->u.nonSurrogateDiagnostic->addinfo;
2262 if (addinfo && (setobj->addinfo = malloc (strlen(addinfo) + 1)))
2263 strcpy (setobj->addinfo, addinfo);
2264 logf (LOG_DEBUG, "Diagnostic response. %s (%d): %s",
2265 diagbib1_str (setobj->condition),
2267 setobj->addinfo ? setobj->addinfo : "");
2274 setobj->numberOfRecordsReturned =
2275 zrs->u.databaseOrSurDiagnostics->num_records;
2276 logf (LOG_DEBUG, "Got %d records", setobj->numberOfRecordsReturned);
2277 for (offset = 0; offset<setobj->numberOfRecordsReturned; offset++)
2279 rl = new_IR_record (setobj, setobj->start + offset,
2280 zrs->u.databaseOrSurDiagnostics->
2281 records[offset]->which);
2282 if (rl->which == Z_NamePlusRecord_surrogateDiagnostic)
2286 diagrec = zrs->u.databaseOrSurDiagnostics->
2287 records[offset]->u.surrogateDiagnostic;
2289 rl->u.diag.condition = *diagrec->condition;
2290 if (diagrec->addinfo && (rl->u.diag.addinfo =
2291 malloc (strlen (diagrec->addinfo)+1)))
2292 strcpy (rl->u.diag.addinfo, diagrec->addinfo);
2296 Z_DatabaseRecord *zr;
2299 zr = zrs->u.databaseOrSurDiagnostics->records[offset]
2301 oe = (Odr_external*) zr;
2302 rl->u.dbrec.size = zr->u.octet_aligned->len;
2303 if (oe->which == ODR_EXTERNAL_octet && rl->u.dbrec.size > 0)
2305 const char *buf = (char*) zr->u.octet_aligned->buf;
2306 if ((rl->u.dbrec.buf = malloc (rl->u.dbrec.size)))
2307 memcpy (rl->u.dbrec.buf, buf, rl->u.dbrec.size);
2310 rl->u.dbrec.buf = NULL;
2316 static void ir_searchResponse (void *o, Z_SearchResponse *searchrs)
2319 IRSetObj *setobj = p->set_child;
2320 Z_Records *zrs = searchrs->records;
2324 setobj->searchStatus = searchrs->searchStatus ? 1 : 0;
2325 setobj->resultCount = *searchrs->resultCount;
2326 if (searchrs->presentStatus)
2327 setobj->presentStatus = *searchrs->presentStatus;
2328 if (searchrs->nextResultSetPosition)
2329 setobj->nextResultSetPosition = *searchrs->nextResultSetPosition;
2331 logf (LOG_DEBUG, "Search response %d, %d hits",
2332 setobj->searchStatus, setobj->resultCount);
2334 ir_handleRecords (o, zrs);
2336 setobj->recordFlag = 0;
2339 logf (LOG_DEBUG, "Search response, no object!");
2343 static void ir_presentResponse (void *o, Z_PresentResponse *presrs)
2346 IRSetObj *setobj = p->set_child;
2347 Z_Records *zrs = presrs->records;
2349 logf (LOG_DEBUG, "Received presentResponse");
2350 setobj->presentStatus = *presrs->presentStatus;
2351 setobj->nextResultSetPosition = *presrs->nextResultSetPosition;
2353 ir_handleRecords (o, zrs);
2356 setobj->recordFlag = 0;
2357 logf (LOG_DEBUG, "No records!");
2361 static void ir_scanResponse (void *o, Z_ScanResponse *scanrs)
2364 IRScanObj *scanobj = p->scan_child;
2366 logf (LOG_DEBUG, "Received scanResponse");
2368 scanobj->scanStatus = *scanrs->scanStatus;
2369 logf (LOG_DEBUG, "scanStatus=%d", scanobj->scanStatus);
2371 if (scanrs->stepSize)
2372 scanobj->stepSize = *scanrs->stepSize;
2373 logf (LOG_DEBUG, "stepSize=%d", scanobj->stepSize);
2375 scanobj->numberOfEntriesReturned = *scanrs->numberOfEntriesReturned;
2376 logf (LOG_DEBUG, "numberOfEntriesReturned=%d",
2377 scanobj->numberOfEntriesReturned);
2379 if (scanrs->positionOfTerm)
2380 scanobj->positionOfTerm = *scanrs->positionOfTerm;
2382 scanobj->positionOfTerm = -1;
2383 logf (LOG_DEBUG, "positionOfTerm=%d", scanobj->positionOfTerm);
2385 free (scanobj->entries);
2386 scanobj->entries = NULL;
2387 free (scanobj->nonSurrogateDiagnostics);
2388 scanobj->nonSurrogateDiagnostics = NULL;
2390 if (scanrs->entries)
2395 scanobj->entries_flag = 1;
2396 scanobj->which = scanrs->entries->which;
2397 switch (scanobj->which)
2399 case Z_ListEntries_entries:
2400 scanobj->num_entries = scanrs->entries->u.entries->num_entries;
2401 scanobj->entries = malloc (scanobj->num_entries *
2402 sizeof(*scanobj->entries));
2403 for (i=0; i<scanobj->num_entries; i++)
2405 ze = scanrs->entries->u.entries->entries[i];
2406 scanobj->entries[i].which = ze->which;
2409 case Z_Entry_termInfo:
2410 if (ze->u.termInfo->term->which == Z_Term_general)
2412 int l = ze->u.termInfo->term->u.general->len;
2413 scanobj->entries[i].u.term.buf = malloc (1+l);
2414 memcpy (scanobj->entries[i].u.term.buf,
2415 ze->u.termInfo->term->u.general->buf,
2417 scanobj->entries[i].u.term.buf[l] = '\0';
2420 scanobj->entries[i].u.term.buf = NULL;
2421 if (ze->u.termInfo->globalOccurrences)
2422 scanobj->entries[i].u.term.globalOccurrences =
2423 *ze->u.termInfo->globalOccurrences;
2425 scanobj->entries[i].u.term.globalOccurrences = 0;
2427 case Z_Entry_surrogateDiagnostic:
2428 scanobj->entries[i].u.diag.addinfo =
2429 malloc (1+strlen(ze->u.surrogateDiagnostic->
2431 strcpy (scanobj->entries[i].u.diag.addinfo,
2432 ze->u.surrogateDiagnostic->addinfo);
2433 scanobj->entries[i].u.diag.condition =
2434 *ze->u.surrogateDiagnostic->condition;
2439 case Z_ListEntries_nonSurrogateDiagnostics:
2440 scanobj->num_diagRecs = scanrs->entries->
2441 u.nonSurrogateDiagnostics->num_diagRecs;
2442 scanobj->nonSurrogateDiagnostics = malloc (scanobj->num_diagRecs *
2443 sizeof(*scanobj->nonSurrogateDiagnostics));
2448 scanobj->entries_flag = 0;
2452 * ir_select_read: handle incoming packages
2454 void ir_select_read (ClientData clientData)
2456 IRObj *p = clientData;
2462 r = cs_rcvconnect (p->cs_link);
2466 ir_select_remove_write (cs_fileno (p->cs_link), p);
2469 logf (LOG_DEBUG, "cs_rcvconnect error");
2471 Tcl_Eval (p->interp, p->failback);
2472 do_disconnect (p, NULL, 2, NULL);
2476 Tcl_Eval (p->interp, p->callback);
2481 /* signal one more use of ir object - callbacks must not
2482 release the ir memory (p pointer) */
2484 if ((r=cs_get (p->cs_link, &p->buf_in, &p->len_in)) <= 0)
2486 logf (LOG_DEBUG, "cs_get failed, code %d", r);
2487 ir_select_remove (cs_fileno (p->cs_link), p);
2489 Tcl_Eval (p->interp, p->failback);
2490 do_disconnect (p, NULL, 2, NULL);
2492 /* relase ir object now if callback deleted it */
2498 odr_setbuf (p->odr_in, p->buf_in, r, 0);
2499 logf (LOG_DEBUG, "cs_get ok, got %d", r);
2500 if (!z_APDU (p->odr_in, &apdu, 0))
2502 logf (LOG_DEBUG, "%s", odr_errlist [odr_geterror (p->odr_in)]);
2504 Tcl_Eval (p->interp, p->failback);
2505 do_disconnect (p, NULL, 2, NULL);
2507 /* relase ir object now if callback deleted it */
2513 case Z_APDU_initResponse:
2514 ir_initResponse (p, apdu->u.initResponse);
2516 case Z_APDU_searchResponse:
2517 ir_searchResponse (p, apdu->u.searchResponse);
2519 case Z_APDU_presentResponse:
2520 ir_presentResponse (p, apdu->u.presentResponse);
2522 case Z_APDU_scanResponse:
2523 ir_scanResponse (p, apdu->u.scanResponse);
2526 logf (LOG_WARN, "Received unknown APDU type (%d)", apdu->which);
2528 Tcl_Eval (p->interp, p->failback);
2529 do_disconnect (p, NULL, 2, NULL);
2531 odr_reset (p->odr_in);
2533 Tcl_Eval (p->interp, p->callback);
2534 if (p->ref_count == 1)
2540 } while (p->cs_link && cs_more (p->cs_link));
2544 * ir_select_write: handle outgoing packages - not yet written.
2546 void ir_select_write (ClientData clientData)
2548 IRObj *p = clientData;
2551 logf (LOG_DEBUG, "In write handler");
2554 r = cs_rcvconnect (p->cs_link);
2560 logf (LOG_DEBUG, "cs_rcvconnect error");
2561 ir_select_remove_write (cs_fileno (p->cs_link), p);
2563 Tcl_Eval (p->interp, p->failback);
2564 do_disconnect (p, NULL, 2, NULL);
2567 ir_select_remove_write (cs_fileno (p->cs_link), p);
2569 Tcl_Eval (p->interp, p->callback);
2572 if ((r=cs_put (p->cs_link, p->sbuf, p->slen)) < 0)
2574 logf (LOG_DEBUG, "select write fail");
2576 Tcl_Eval (p->interp, p->failback);
2577 do_disconnect (p, NULL, 2, NULL);
2579 else if (r == 0) /* remove select bit */
2581 ir_select_remove_write (cs_fileno (p->cs_link), p);
2585 /* ------------------------------------------------------- */
2588 * ir_tcl_init: Registration of TCL commands.
2590 int ir_tcl_init (Tcl_Interp *interp)
2592 Tcl_CreateCommand (interp, "ir", ir_obj_mk, (ClientData) NULL,
2593 (Tcl_CmdDeleteProc *) NULL);
2594 Tcl_CreateCommand (interp, "ir-set", ir_set_obj_mk,
2595 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
2596 Tcl_CreateCommand (interp, "ir-scan", ir_scan_obj_mk,
2597 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);