2 * IR toolkit for tcl/tk
4 * See the file LICENSE for details.
5 * Sebastian Hammer, Adam Dickmeiss
8 * Revision 1.32 1995-05-29 08:44:16 adam
9 * Work on delete of objects.
11 * Revision 1.31 1995/05/26 11:44:10 adam
12 * Bugs fixed. More work on MARC utilities and queries. Test
13 * client is up-to-date again.
15 * Revision 1.30 1995/05/26 08:54:11 adam
16 * New MARC utilities. Uses prefix query.
18 * Revision 1.29 1995/05/24 14:10:22 adam
19 * Work on idAuthentication, protocolVersion and options.
21 * Revision 1.28 1995/05/23 15:34:48 adam
22 * Many new settings, userInformationField, smallSetUpperBound, etc.
23 * A number of settings are inherited when ir-set is executed.
24 * This version is incompatible with the graphical test client (client.tcl).
26 * Revision 1.27 1995/05/11 15:34:47 adam
27 * Scan request changed a bit. This version works with RLG.
29 * Revision 1.26 1995/04/18 16:11:51 adam
30 * First version of graphical Scan. Some work on query-by-form.
32 * Revision 1.25 1995/04/17 09:37:17 adam
33 * Further development of scan.
35 * Revision 1.24 1995/04/11 14:16:42 adam
36 * Further work on scan. Response works. Entries aren't saved yet.
38 * Revision 1.23 1995/04/10 10:50:27 adam
39 * Result-set name defaults to suffix of ir-set name.
40 * Started working on scan. Not finished at this point.
42 * Revision 1.22 1995/03/31 10:43:03 adam
43 * More robust when getting bad MARC records.
45 * Revision 1.21 1995/03/31 08:56:37 adam
46 * New button "Search".
48 * Revision 1.20 1995/03/29 16:07:09 adam
49 * Bug fix: Didn't use setName in present request.
51 * Revision 1.19 1995/03/28 12:45:23 adam
52 * New ir method failback: called on disconnect/protocol error.
53 * New ir set/get method: protocol: SR / Z3950.
54 * Simple popup and disconnect when failback is invoked.
56 * Revision 1.18 1995/03/21 15:50:12 adam
59 * Revision 1.17 1995/03/21 13:41:03 adam
60 * Comstack cs_create not used too often. Non-blocking connect.
62 * Revision 1.16 1995/03/21 08:26:06 adam
63 * New method, setName, to specify the result set name (other than Default).
64 * New method, responseStatus, which returns diagnostic info, if any, after
65 * present response / search response.
67 * Revision 1.15 1995/03/20 15:24:07 adam
68 * Diagnostic records saved on searchResponse.
70 * Revision 1.14 1995/03/20 08:53:22 adam
71 * Event loop in tclmain.c rewritten. New method searchStatus.
73 * Revision 1.13 1995/03/17 18:26:17 adam
74 * Non-blocking i/o used now. Database names popup as cascade items.
76 * Revision 1.12 1995/03/17 15:45:00 adam
77 * Improved target/database setup.
79 * Revision 1.11 1995/03/16 17:54:03 adam
80 * Minor changes really.
82 * Revision 1.10 1995/03/15 16:14:50 adam
83 * Blocking arg in cs_create changed.
85 * Revision 1.9 1995/03/15 13:59:24 adam
88 * Revision 1.8 1995/03/15 08:25:16 adam
89 * New method presentStatus to check for error on present. Misc. cleanup
90 * of IRRecordList manipulations. Full MARC record presentation in
93 * Revision 1.7 1995/03/14 17:32:29 adam
94 * Presentation of full Marc record in popup window.
96 * Revision 1.6 1995/03/12 19:31:55 adam
97 * Pattern matching implemented when retrieving MARC records. More
98 * diagnostic functions.
100 * Revision 1.5 1995/03/10 18:00:15 adam
101 * Actual presentation in line-by-line format. RPN query support.
103 * Revision 1.4 1995/03/09 16:15:08 adam
104 * First presentRequest attempts. Hot-target list.
110 #include <sys/time.h>
120 int (*method) (void *obj, Tcl_Interp *interp, int argc, char **argv);
128 static int do_disconnect (void *obj,Tcl_Interp *interp, int argc, char **argv);
130 static IRRecordList *new_IR_record (IRSetObj *setobj, int no, int which)
134 for (rl = setobj->record_list; rl; rl = rl->next)
140 case Z_NamePlusRecord_databaseRecord:
141 free (rl->u.dbrec.buf);
142 rl->u.dbrec.buf = NULL;
144 case Z_NamePlusRecord_surrogateDiagnostic:
145 free (rl->u.diag.addinfo);
146 rl->u.diag.addinfo = NULL;
154 rl = malloc (sizeof(*rl));
156 rl->next = setobj->record_list;
158 setobj->record_list = rl;
164 static IRRecordList *find_IR_record (IRSetObj *setobj, int no)
168 for (rl = setobj->record_list; rl; rl = rl->next)
174 static void delete_IR_records (IRSetObj *setobj)
176 IRRecordList *rl, *rl1;
178 for (rl = setobj->record_list; rl; rl = rl1)
182 case Z_NamePlusRecord_databaseRecord:
183 free (rl->u.dbrec.buf);
185 case Z_NamePlusRecord_surrogateDiagnostic:
186 free (rl->u.diag.addinfo);
192 setobj->record_list = NULL;
196 * getsetint: Set/get integer value
198 static int get_set_int (int *val, Tcl_Interp *interp, int argc, char **argv)
204 if (Tcl_GetInt (interp, argv[2], val)==TCL_ERROR)
207 sprintf (buf, "%d", *val);
208 Tcl_AppendResult (interp, buf, NULL);
213 * mk_nonSurrogateDiagnostics: Make Tcl result with diagnostic info
215 static int mk_nonSurrogateDiagnostics (Tcl_Interp *interp,
222 Tcl_AppendElement (interp, "NSD");
223 sprintf (buf, "%d", condition);
224 Tcl_AppendElement (interp, buf);
225 cp = diagbib1_str (condition);
227 Tcl_AppendElement (interp, (char*) cp);
229 Tcl_AppendElement (interp, "");
231 Tcl_AppendElement (interp, (char*) addinfo);
233 Tcl_AppendElement (interp, "");
238 * get_parent_info: Returns information about parent object.
240 static int get_parent_info (Tcl_Interp *interp, const char *name,
241 Tcl_CmdInfo *parent_info,
244 char parent_name[128];
245 const char *csep = strrchr (name, '.');
250 interp->result = "missing .";
258 memcpy (parent_name, name, pos);
259 parent_name[pos] = '\0';
260 if (!Tcl_GetCommandInfo (interp, parent_name, parent_info))
262 interp->result = "No parent";
269 * ir_method: Search for method in table and invoke method handler
271 int ir_method (Tcl_Interp *interp, int argc, char **argv, IRMethods *tab)
273 IRMethods *tab_i = tab;
276 for (tab_i = tab; tab_i->tab; tab_i++)
277 for (t = tab_i->tab; t->name; t++)
280 if ((*t->method)(tab_i->obj, interp, argc, argv) == TCL_ERROR)
284 if (!strcmp (t->name, argv[1]))
285 return (*t->method)(tab_i->obj, interp, argc, argv);
289 Tcl_AppendResult (interp, "Bad method. Possible methods:", NULL);
290 for (tab_i = tab; tab_i->tab; tab_i++)
291 for (t = tab_i->tab; t->name; t++)
292 Tcl_AppendResult (interp, " ", t->name, NULL);
297 * ir_method_r: Get status for all readable elements
299 int ir_method_r (void *obj, Tcl_Interp *interp, int argc, char **argv,
307 for (; tab->name; tab++)
310 argv_n[1] = tab->name;
311 Tcl_AppendResult (interp, "{", NULL);
312 (*tab->method)(obj, interp, argc_n, argv_n);
313 Tcl_AppendResult (interp, "} ", NULL);
319 * ir_named_bits: get/set named bits
321 int ir_named_bits (struct ir_named_entry *tab, Odr_bitmask *ob,
322 Tcl_Interp *interp, int argc, char **argv)
324 struct ir_named_entry *ti;
329 for (no = 0; no < argc; no++)
331 for (ti = tab; ti->name; ti++)
332 if (!strcmp (argv[no], ti->name))
334 ODR_MASK_SET (ob, ti->pos);
339 Tcl_AppendResult (interp, "Bad bit mask: ", argv[no], NULL);
345 for (ti = tab; ti->name; ti++)
346 if (ODR_MASK_GET (ob, ti->pos))
347 Tcl_AppendElement (interp, ti->name);
352 * ir_strdup: Duplicate string
354 int ir_strdup (Tcl_Interp *interp, char** p, const char *s)
356 *p = malloc (strlen(s)+1);
359 interp->result = "strdup fail";
367 * ir_strdel: Delete string
369 int ir_strdel (Tcl_Interp *interp, char **p)
377 * ir_malloc: Malloc function
379 void *ir_malloc (Tcl_Interp *interp, size_t size)
381 static char buf[128];
382 void *p = malloc (size);
386 sprintf (buf, "Malloc fail. %ld bytes requested", (long) size);
387 interp->result = buf;
393 /* ------------------------------------------------------- */
396 * do_init_request: init method on IR object
398 static int do_init_request (void *obj, Tcl_Interp *interp,
399 int argc, char **argv)
401 Z_APDU apdu, *apdup = &apdu;
410 interp->result = "not connected";
413 odr_reset (p->odr_out);
416 req.options = &p->options;
417 req.protocolVersion = &p->protocolVersion;
418 req.preferredMessageSize = &p->preferredMessageSize;
419 req.maximumRecordSize = &p->maximumRecordSize;
421 if (p->idAuthenticationGroupId)
423 Z_IdPass *pass = odr_malloc (p->odr_out, sizeof(*pass));
424 Z_IdAuthentication *auth = odr_malloc (p->odr_out, sizeof(*auth));
426 auth->which = Z_IdAuthentication_idPass;
427 auth->u.idPass = pass;
428 if (p->idAuthenticationGroupId && *p->idAuthenticationGroupId)
429 pass->groupId = p->idAuthenticationGroupId;
431 pass->groupId = NULL;
432 if (p->idAuthenticationUserId && *p->idAuthenticationUserId)
433 pass->userId = p->idAuthenticationUserId;
436 if (p->idAuthenticationPassword && *p->idAuthenticationPassword)
437 pass->password = p->idAuthenticationPassword;
439 pass->password = NULL;
440 req.idAuthentication = auth;
442 else if (!p->idAuthenticationOpen || !*p->idAuthenticationOpen)
443 req.idAuthentication = NULL;
446 Z_IdAuthentication *auth = odr_malloc (p->odr_out, sizeof(*auth));
448 auth->which = Z_IdAuthentication_open;
449 auth->u.open = p->idAuthenticationOpen;
450 req.idAuthentication = auth;
452 req.implementationId = p->implementationId;
453 req.implementationName = p->implementationName;
454 req.implementationVersion = "0.1";
455 req.userInformationField = 0;
457 apdu.u.initRequest = &req;
458 apdu.which = Z_APDU_initRequest;
460 if (!z_APDU (p->odr_out, &apdup, 0))
462 Tcl_AppendResult (interp, odr_errlist [odr_geterror (p->odr_out)],
464 odr_reset (p->odr_out);
467 p->sbuf = odr_getbuf (p->odr_out, &p->slen, NULL);
468 if ((r=cs_put (p->cs_link, p->sbuf, p->slen)) < 0)
470 interp->result = "cs_put failed in init";
471 do_disconnect (p, NULL, 2, NULL);
476 ir_select_add_write (cs_fileno(p->cs_link), p);
477 logf (LOG_DEBUG, "Sent part of initializeRequest (%d bytes)", p->slen);
480 logf (LOG_DEBUG, "Sent whole initializeRequest (%d bytes)", p->slen);
485 * do_protocolVersion: Set protocol Version
487 static int do_protocolVersion (void *obj, Tcl_Interp *interp,
488 int argc, char **argv)
490 static struct ir_named_entry version_tab[] = {
501 ODR_MASK_ZERO (&p->protocolVersion);
502 ODR_MASK_SET (&p->protocolVersion, 0);
503 ODR_MASK_SET (&p->protocolVersion, 1);
506 return ir_named_bits (version_tab, &p->protocolVersion,
507 interp, argc-2, argv+2);
511 * do_options: Set options
513 static int do_options (void *obj, Tcl_Interp *interp,
514 int argc, char **argv)
516 static struct ir_named_entry options_tab[] = {
520 { "resourceReport", 3 },
521 { "triggerResourceCtrl", 4},
522 { "resourceCtrl", 5},
526 { "extentedServices", 10},
527 { "level-1Segmentation", 11},
528 { "level-2Segmentation", 12},
529 { "concurrentOperations", 13},
530 { "namedResultSets", 14},
537 ODR_MASK_ZERO (&p->options);
538 ODR_MASK_SET (&p->options, 0);
539 ODR_MASK_SET (&p->options, 1);
540 ODR_MASK_SET (&p->options, 7);
541 ODR_MASK_SET (&p->options, 14);
544 return ir_named_bits (options_tab, &p->options, interp, argc-2, argv+2);
548 * do_preferredMessageSize: Set/get preferred message size
550 static int do_preferredMessageSize (void *obj, Tcl_Interp *interp,
551 int argc, char **argv)
557 p->preferredMessageSize = 4096;
560 return get_set_int (&p->preferredMessageSize, interp, argc, argv);
564 * do_maximumRecordSize: Set/get maximum record size
566 static int do_maximumRecordSize (void *obj, Tcl_Interp *interp,
567 int argc, char **argv)
573 p->maximumRecordSize = 32768;
576 return get_set_int (&p->maximumRecordSize, interp, argc, argv);
580 * do_initResult: Get init result
582 static int do_initResult (void *obj, Tcl_Interp *interp,
583 int argc, char **argv)
589 return get_set_int (&p->initResult, interp, argc, argv);
594 * do_implementationName: Set/get Implementation Name.
596 static int do_implementationName (void *obj, Tcl_Interp *interp,
597 int argc, char **argv)
602 return ir_strdup (interp, &p->implementationName, "TCL/TK on YAZ");
604 return ir_strdel (interp, &p->implementationName);
607 free (p->implementationName);
608 if (ir_strdup (interp, &p->implementationName, argv[2])
612 Tcl_AppendResult (interp, p->implementationName,
618 * do_implementationId: Set/get Implementation Id.
620 static int do_implementationId (void *obj, Tcl_Interp *interp,
621 int argc, char **argv)
626 return ir_strdup (interp, &p->implementationId, "81");
628 return ir_strdel (interp, &p->implementationId);
631 free (p->implementationId);
632 if (ir_strdup (interp, &p->implementationId, argv[2]) == TCL_ERROR)
635 Tcl_AppendResult (interp, p->implementationId, (char*) NULL);
640 * do_targetImplementationName: Get Implementation Name of target.
642 static int do_targetImplementationName (void *obj, Tcl_Interp *interp,
643 int argc, char **argv)
649 p->targetImplementationName = NULL;
653 return ir_strdel (interp, &p->targetImplementationName);
654 Tcl_AppendResult (interp, p->targetImplementationName,
660 * do_targetImplementationId: Get Implementation Id of target
662 static int do_targetImplementationId (void *obj, Tcl_Interp *interp,
663 int argc, char **argv)
669 p->targetImplementationId = NULL;
673 return ir_strdel (interp, &p->targetImplementationId);
674 Tcl_AppendResult (interp, p->targetImplementationId, (char*) NULL);
679 * do_targetImplementationVersion: Get Implementation Version of target
681 static int do_targetImplementationVersion (void *obj, Tcl_Interp *interp,
682 int argc, char **argv)
688 p->targetImplementationVersion = NULL;
692 return ir_strdel (interp, &p->targetImplementationVersion);
693 Tcl_AppendResult (interp, p->targetImplementationVersion, (char*) NULL);
698 * do_idAuthentication: Set/get id Authentication
700 static int do_idAuthentication (void *obj, Tcl_Interp *interp,
701 int argc, char **argv)
705 if (argc >= 3 || argc == -1)
707 free (p->idAuthenticationOpen);
708 free (p->idAuthenticationGroupId);
709 free (p->idAuthenticationUserId);
710 free (p->idAuthenticationPassword);
712 if (argc >= 3 || argc <= 0)
714 p->idAuthenticationOpen = NULL;
715 p->idAuthenticationGroupId = NULL;
716 p->idAuthenticationUserId = NULL;
717 p->idAuthenticationPassword = NULL;
725 if (ir_strdup (interp, &p->idAuthenticationOpen, argv[2])
731 if (ir_strdup (interp, &p->idAuthenticationGroupId, argv[2])
734 if (ir_strdup (interp, &p->idAuthenticationUserId, argv[3])
737 if (ir_strdup (interp, &p->idAuthenticationPassword, argv[4])
742 if (p->idAuthenticationOpen)
743 Tcl_AppendElement (interp, p->idAuthenticationOpen);
744 else if (p->idAuthenticationGroupId)
746 Tcl_AppendElement (interp, p->idAuthenticationGroupId);
747 Tcl_AppendElement (interp, p->idAuthenticationUserId);
748 Tcl_AppendElement (interp, p->idAuthenticationPassword);
754 * do_connect: connect method on IR object
756 static int do_connect (void *obj, Tcl_Interp *interp,
757 int argc, char **argv)
762 int protocol_type = CS_Z3950;
770 interp->result = "already connected";
773 if (!strcmp (p->protocol_type, "Z3950"))
774 protocol_type = CS_Z3950;
775 else if (!strcmp (p->protocol_type, "SR"))
776 protocol_type = CS_SR;
779 interp->result = "bad protocol type";
782 if (!strcmp (p->cs_type, "tcpip"))
784 p->cs_link = cs_create (tcpip_type, CS_BLOCK, protocol_type);
785 addr = tcpip_strtoaddr (argv[2]);
788 interp->result = "tcpip_strtoaddr fail";
791 logf (LOG_DEBUG, "tcp/ip connect %s", argv[2]);
794 else if (!strcmp (p->cs_type, "mosi"))
796 p->cs_link = cs_create (mosi_type, CS_BLOCK, protocol_type);
797 addr = mosi_strtoaddr (argv[2]);
800 interp->result = "mosi_strtoaddr fail";
803 logf (LOG_DEBUG, "mosi connect %s", argv[2]);
808 interp->result = "unknown comstack type";
811 if (ir_strdup (interp, &p->hostname, argv[2]) == TCL_ERROR)
813 if ((r=cs_connect (p->cs_link, addr)) < 0)
815 interp->result = "cs_connect fail";
816 do_disconnect (p, NULL, 2, NULL);
819 ir_select_add (cs_fileno (p->cs_link), p);
822 ir_select_add_write (cs_fileno (p->cs_link), p);
829 Tcl_Eval (p->interp, p->callback);
833 Tcl_AppendElement (interp, p->hostname);
838 * do_disconnect: disconnect method on IR object
840 static int do_disconnect (void *obj, Tcl_Interp *interp,
841 int argc, char **argv)
856 ir_select_remove_write (cs_fileno (p->cs_link), p);
857 ir_select_remove (cs_fileno (p->cs_link), p);
860 cs_close (p->cs_link);
863 assert (!p->cs_link);
868 * do_comstack: Set/get comstack method on IR object
870 static int do_comstack (void *o, Tcl_Interp *interp,
871 int argc, char **argv)
876 return ir_strdup (interp, &obj->cs_type, "tcpip");
878 return ir_strdel (interp, &obj->cs_type);
882 if (ir_strdup (interp, &obj->cs_type, argv[2]) == TCL_ERROR)
885 Tcl_AppendElement (interp, obj->cs_type);
890 * do_protocol: Set/get protocol method on IR object
892 static int do_protocol (void *o, Tcl_Interp *interp,
893 int argc, char **argv)
898 return ir_strdup (interp, &obj->protocol_type, "Z3950");
900 return ir_strdel (interp, &obj->protocol_type);
903 free (obj->protocol_type);
904 if (ir_strdup (interp, &obj->protocol_type, argv[2]) == TCL_ERROR)
907 Tcl_AppendElement (interp, obj->protocol_type);
912 * do_callback: add callback
914 static int do_callback (void *obj, Tcl_Interp *interp,
915 int argc, char **argv)
925 return ir_strdel (interp, &p->callback);
931 if (ir_strdup (interp, &p->callback, argv[2]) == TCL_ERROR)
942 * do_failback: add error handle callback
944 static int do_failback (void *obj, Tcl_Interp *interp,
945 int argc, char **argv)
955 return ir_strdel (interp, &p->failback);
961 if (ir_strdup (interp, &p->failback, argv[2]) == TCL_ERROR)
972 * do_databaseNames: specify database names
974 static int do_databaseNames (void *obj, Tcl_Interp *interp,
975 int argc, char **argv)
982 for (i=0; i<p->num_databaseNames; i++)
983 free (p->databaseNames[i]);
984 free (p->databaseNames);
988 p->num_databaseNames = 0;
989 p->databaseNames = NULL;
994 for (i=0; i<p->num_databaseNames; i++)
995 Tcl_AppendElement (interp, p->databaseNames[i]);
998 if (p->databaseNames)
1000 for (i=0; i<p->num_databaseNames; i++)
1001 free (p->databaseNames[i]);
1002 free (p->databaseNames);
1004 p->num_databaseNames = argc - 2;
1005 if (!(p->databaseNames = ir_malloc (interp,
1006 sizeof(*p->databaseNames) * p->num_databaseNames)))
1008 for (i=0; i<p->num_databaseNames; i++)
1010 if (ir_strdup (interp, &p->databaseNames[i], argv[2+i])
1018 * do_replaceIndicator: Set/get replace Set indicator
1020 static int do_replaceIndicator (void *obj, Tcl_Interp *interp,
1021 int argc, char **argv)
1027 p->replaceIndicator = 1;
1030 return get_set_int (&p->replaceIndicator, interp, argc, argv);
1034 * do_queryType: Set/Get query method
1036 static int do_queryType (void *obj, Tcl_Interp *interp,
1037 int argc, char **argv)
1042 return ir_strdup (interp, &p->queryType, "rpn");
1043 else if (argc == -1)
1044 return ir_strdel (interp, &p->queryType);
1047 free (p->queryType);
1048 if (ir_strdup (interp, &p->queryType, argv[2]) == TCL_ERROR)
1051 Tcl_AppendResult (interp, p->queryType, NULL);
1056 * do_userInformationField: Get User information field
1058 static int do_userInformationField (void *obj, Tcl_Interp *interp,
1059 int argc, char **argv)
1065 p->userInformationField = NULL;
1068 else if (argc == -1)
1069 return ir_strdel (interp, &p->userInformationField);
1070 Tcl_AppendResult (interp, p->userInformationField, NULL);
1075 * do_smallSetUpperBound: Set/get small set upper bound
1077 static int do_smallSetUpperBound (void *o, Tcl_Interp *interp,
1078 int argc, char **argv)
1084 p->smallSetUpperBound = 0;
1087 return get_set_int (&p->smallSetUpperBound, interp, argc, argv);
1091 * do_largeSetLowerBound: Set/get large set lower bound
1093 static int do_largeSetLowerBound (void *o, Tcl_Interp *interp,
1094 int argc, char **argv)
1100 p->largeSetLowerBound = 2;
1103 return get_set_int (&p->largeSetLowerBound, interp, argc, argv);
1107 * do_mediumSetPresentNumber: Set/get large set lower bound
1109 static int do_mediumSetPresentNumber (void *o, Tcl_Interp *interp,
1110 int argc, char **argv)
1116 p->mediumSetPresentNumber = 0;
1119 return get_set_int (&p->mediumSetPresentNumber, interp, argc, argv);
1123 static IRMethod ir_method_tab[] = {
1124 { 1, "comstack", do_comstack },
1125 { 1, "protocol", do_protocol },
1126 { 0, "failback", do_failback },
1128 { 1, "connect", do_connect },
1129 { 0, "protocolVersion", do_protocolVersion },
1130 { 1, "preferredMessageSize", do_preferredMessageSize },
1131 { 1, "maximumRecordSize", do_maximumRecordSize },
1132 { 1, "implementationName", do_implementationName },
1133 { 1, "implementationId", do_implementationId },
1134 { 0, "targetImplementationName", do_targetImplementationName },
1135 { 0, "targetImplementationId", do_targetImplementationId },
1136 { 0, "targetImplementationVersion", do_targetImplementationVersion },
1137 { 0, "userInformationField", do_userInformationField },
1138 { 1, "idAuthentication", do_idAuthentication },
1139 { 0, "options", do_options },
1140 { 0, "init", do_init_request },
1141 { 0, "initResult", do_initResult },
1142 { 0, "disconnect", do_disconnect },
1143 { 0, "callback", do_callback },
1147 static IRMethod ir_set_c_method_tab[] = {
1148 { 0, "databaseNames", do_databaseNames},
1149 { 0, "replaceIndicator", do_replaceIndicator},
1150 { 0, "queryType", do_queryType },
1151 { 0, "smallSetUpperBound", do_smallSetUpperBound},
1152 { 0, "largeSetLowerBound", do_largeSetLowerBound},
1153 { 0, "mediumSetPresentNumber", do_mediumSetPresentNumber},
1158 * ir_obj_method: IR Object methods
1160 static int ir_obj_method (ClientData clientData, Tcl_Interp *interp,
1161 int argc, char **argv)
1164 IRObj *p = clientData;
1167 return ir_method_r (clientData, interp, argc, argv, ir_method_tab);
1169 tab[0].tab = ir_method_tab;
1171 tab[1].tab = ir_set_c_method_tab;
1172 tab[1].obj = &p->set_inher;
1175 return ir_method (interp, argc, argv, tab);
1179 * ir_obj_delete: IR Object disposal
1181 static void ir_obj_delete (ClientData clientData)
1183 IRObj *obj = clientData;
1187 if (obj->ref_count > 0)
1189 assert (obj->ref_count == 0);
1191 tab[0].tab = ir_method_tab;
1193 tab[1].tab = ir_set_c_method_tab;
1194 tab[1].obj = &obj->set_inher;
1197 ir_method (NULL, -1, NULL, tab);
1198 odr_destroy (obj->odr_in);
1199 odr_destroy (obj->odr_out);
1200 odr_destroy (obj->odr_pr);
1201 free (obj->buf_out);
1207 * ir_obj_mk: IR Object creation
1209 static int ir_obj_mk (ClientData clientData, Tcl_Interp *interp,
1210 int argc, char **argv)
1220 interp->result = "wrong # args";
1223 if (!(obj = ir_malloc (interp, sizeof(*obj))))
1228 obj->bibset = ccl_qual_mk ();
1229 if ((inf = fopen ("default.bib", "r")))
1231 ccl_qual_file (obj->bibset, inf);
1236 obj->odr_in = odr_createmem (ODR_DECODE);
1237 obj->odr_out = odr_createmem (ODR_ENCODE);
1238 obj->odr_pr = odr_createmem (ODR_PRINT);
1240 obj->len_out = 10000;
1241 if (!(obj->buf_out = ir_malloc (interp, obj->len_out)))
1243 odr_setbuf (obj->odr_out, obj->buf_out, obj->len_out, 0);
1248 tab[0].tab = ir_method_tab;
1250 tab[1].tab = ir_set_c_method_tab;
1251 tab[1].obj = &obj->set_inher;
1254 if (ir_method (interp, 0, NULL, tab) == TCL_ERROR)
1256 Tcl_CreateCommand (interp, argv[1], ir_obj_method,
1257 (ClientData) obj, ir_obj_delete);
1261 /* ------------------------------------------------------- */
1263 * do_search: Do search request
1265 static int do_search (void *o, Tcl_Interp *interp,
1266 int argc, char **argv)
1268 Z_SearchRequest req;
1270 Z_APDU apdu, *apdup = &apdu;
1273 IRObj *p = obj->parent;
1283 interp->result = "wrong # args";
1286 if (!p->set_inher.num_databaseNames)
1288 interp->result = "no databaseNames";
1293 interp->result = "not connected";
1296 odr_reset (p->odr_out);
1297 apdu.which = Z_APDU_searchRequest;
1298 apdu.u.searchRequest = &req;
1300 bib1.proto = PROTO_Z3950;
1301 bib1.class = CLASS_ATTSET;
1302 bib1.value = VAL_BIB1;
1304 req.referenceId = 0;
1305 req.smallSetUpperBound = &p->set_inher.smallSetUpperBound;
1306 req.largeSetLowerBound = &p->set_inher.largeSetLowerBound;
1307 req.mediumSetPresentNumber = &p->set_inher.mediumSetPresentNumber;
1308 req.replaceIndicator = &p->set_inher.replaceIndicator;
1309 req.resultSetName = obj->setName ? obj->setName : "Default";
1310 logf (LOG_DEBUG, "Search, resultSetName %s", req.resultSetName);
1311 req.num_databaseNames = p->set_inher.num_databaseNames;
1312 req.databaseNames = p->set_inher.databaseNames;
1313 for (r=0; r < p->set_inher.num_databaseNames; r++)
1314 logf (LOG_DEBUG, " Database %s", p->set_inher.databaseNames[r]);
1315 req.smallSetElementSetNames = 0;
1316 req.mediumSetElementSetNames = 0;
1317 req.preferredRecordSyntax = 0;
1320 if (!strcmp (p->set_inher.queryType, "rpn"))
1322 Z_RPNQuery *RPNquery;
1324 RPNquery = p_query_rpn (p->odr_out, argv[2]);
1327 Tcl_AppendResult (interp, "Syntax error in query", NULL);
1330 RPNquery->attributeSetId = oid_getoidbyent (&bib1);
1331 query.which = Z_Query_type_1;
1332 query.u.type_1 = RPNquery;
1333 logf (LOG_DEBUG, "RPN");
1336 else if (!strcmp (p->set_inher.queryType, "cclrpn"))
1340 struct ccl_rpn_node *rpn;
1341 Z_RPNQuery *RPNquery;
1343 rpn = ccl_find_str(p->bibset, argv[2], &error, &pos);
1346 Tcl_AppendResult (interp, "CCL error: ", ccl_err_msg(error),NULL);
1349 ccl_pr_tree (rpn, stderr);
1350 fprintf (stderr, "\n");
1351 assert((RPNquery = ccl_rpn_query(rpn)));
1352 RPNquery->attributeSetId = oid_getoidbyent (&bib1);
1353 query.which = Z_Query_type_1;
1354 query.u.type_1 = RPNquery;
1355 logf (LOG_DEBUG, "CCLRPN");
1358 else if (!strcmp (p->set_inher.queryType, "ccl"))
1360 query.which = Z_Query_type_2;
1361 query.u.type_2 = &ccl_query;
1362 ccl_query.buf = (unsigned char *) argv[2];
1363 ccl_query.len = strlen (argv[2]);
1364 logf (LOG_DEBUG, "CCL");
1368 interp->result = "unknown query method";
1371 if (!z_APDU (p->odr_out, &apdup, 0))
1373 interp->result = odr_errlist [odr_geterror (p->odr_out)];
1374 odr_reset (p->odr_out);
1377 p->sbuf = odr_getbuf (p->odr_out, &p->slen, NULL);
1378 if ((r=cs_put (p->cs_link, p->sbuf, p->slen)) < 0)
1380 interp->result = "cs_put failed in search";
1385 ir_select_add_write (cs_fileno(p->cs_link), p);
1386 logf (LOG_DEBUG, "Sent part of searchRequest (%d bytes)", p->slen);
1390 logf (LOG_DEBUG, "Whole search request (%d bytes)", p->slen);
1396 * do_resultCount: Get number of hits
1398 static int do_resultCount (void *o, Tcl_Interp *interp,
1399 int argc, char **argv)
1405 return get_set_int (&obj->resultCount, interp, argc, argv);
1409 * do_searchStatus: Get search status (after search response)
1411 static int do_searchStatus (void *o, Tcl_Interp *interp,
1412 int argc, char **argv)
1418 return get_set_int (&obj->searchStatus, interp, argc, argv);
1422 * do_presentStatus: Get search status (after search/present response)
1424 static int do_presentStatus (void *o, Tcl_Interp *interp,
1425 int argc, char **argv)
1431 return get_set_int (&obj->presentStatus, interp, argc, argv);
1435 * do_nextResultSetPosition: Get next result set position
1436 * (after search/present response)
1438 static int do_nextResultSetPosition (void *o, Tcl_Interp *interp,
1439 int argc, char **argv)
1445 return get_set_int (&obj->nextResultSetPosition, interp, argc, argv);
1449 * do_setName: Set result Set name
1451 static int do_setName (void *o, Tcl_Interp *interp,
1452 int argc, char **argv)
1457 return ir_strdup (interp, &obj->setName, "Default");
1458 else if (argc == -1)
1459 return ir_strdel (interp, &obj->setName);
1462 free (obj->setName);
1463 if (ir_strdup (interp, &obj->setName, argv[2])
1467 Tcl_AppendElement (interp, obj->setName);
1472 * do_numberOfRecordsReturned: Get number of records returned
1474 static int do_numberOfRecordsReturned (void *o, Tcl_Interp *interp,
1475 int argc, char **argv)
1481 return get_set_int (&obj->numberOfRecordsReturned, interp, argc, argv);
1485 * do_recordType: Return record type (if any) at position.
1487 static int do_recordType (void *o, Tcl_Interp *interp, int argc, char **argv)
1495 obj->record_list = NULL;
1498 else if (argc == -1)
1500 delete_IR_records (obj);
1505 sprintf (interp->result, "wrong # args");
1508 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
1510 rl = find_IR_record (obj, offset);
1515 case Z_NamePlusRecord_databaseRecord:
1516 interp->result = "databaseRecord";
1518 case Z_NamePlusRecord_surrogateDiagnostic:
1519 interp->result = "surrogateDiagnostic";
1526 * do_recordDiag: Return diagnostic record info
1528 static int do_recordDiag (void *o, Tcl_Interp *interp, int argc, char **argv)
1539 sprintf (interp->result, "wrong # args");
1542 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
1544 rl = find_IR_record (obj, offset);
1547 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
1550 if (rl->which != Z_NamePlusRecord_surrogateDiagnostic)
1552 Tcl_AppendResult (interp, "No Diagnostic record at #", argv[2], NULL);
1555 sprintf (buf, "%d", rl->u.diag.condition);
1556 Tcl_AppendResult (interp, buf, " {",
1557 (rl->u.diag.addinfo ? rl->u.diag.addinfo : ""),
1563 * do_getMarc: Get ISO2709 Record lines/fields
1565 static int do_getMarc (void *o, Tcl_Interp *interp, int argc, char **argv)
1575 sprintf (interp->result, "wrong # args");
1578 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
1580 rl = find_IR_record (obj, offset);
1583 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
1586 if (rl->which != Z_NamePlusRecord_databaseRecord)
1588 Tcl_AppendResult (interp, "No MARC record at #", argv[2], NULL);
1591 return ir_tcl_get_marc (interp, rl->u.dbrec.buf, argc, argv);
1596 * do_responseStatus: Return response status (present or search)
1598 static int do_responseStatus (void *o, Tcl_Interp *interp,
1599 int argc, char **argv)
1605 obj->recordFlag = 0;
1606 obj->addinfo = NULL;
1609 else if (argc == -1)
1610 return ir_strdel (interp, &obj->addinfo);
1611 if (!obj->recordFlag)
1613 Tcl_AppendElement (interp, "OK");
1618 case Z_Records_DBOSD:
1619 Tcl_AppendElement (interp, "DBOSD");
1622 return mk_nonSurrogateDiagnostics (interp, obj->condition,
1629 * do_present: Perform Present Request
1632 static int do_present (void *o, Tcl_Interp *interp,
1633 int argc, char **argv)
1636 IRObj *p = obj->parent;
1637 Z_APDU apdu, *apdup = &apdu;
1638 Z_PresentRequest req;
1647 if (Tcl_GetInt (interp, argv[2], &start) == TCL_ERROR)
1654 if (Tcl_GetInt (interp, argv[3], &number) == TCL_ERROR)
1661 interp->result = "not connected";
1664 odr_reset (p->odr_out);
1666 obj->number = number;
1668 apdu.which = Z_APDU_presentRequest;
1669 apdu.u.presentRequest = &req;
1670 req.referenceId = 0;
1671 /* sprintf(setstring, "%d", setnumber); */
1673 req.resultSetId = obj->setName ? obj->setName : "Default";
1675 req.resultSetStartPoint = &start;
1676 req.numberOfRecordsRequested = &number;
1677 req.elementSetNames = 0;
1678 req.preferredRecordSyntax = 0;
1680 if (!z_APDU (p->odr_out, &apdup, 0))
1682 interp->result = odr_errlist [odr_geterror (p->odr_out)];
1683 odr_reset (p->odr_out);
1686 p->sbuf = odr_getbuf (p->odr_out, &p->slen, NULL);
1687 if ((r=cs_put (p->cs_link, p->sbuf, p->slen)) < 0)
1689 interp->result = "cs_put failed in present";
1694 ir_select_add_write (cs_fileno(p->cs_link), p);
1695 logf (LOG_DEBUG, "Part of present request, start=%d, num=%d"
1696 " (%d bytes)", start, number, p->slen);
1700 logf (LOG_DEBUG, "Whole present request, start=%d, num=%d"
1701 " (%d bytes)", start, number, p->slen);
1707 * do_loadFile: Load result set from file
1710 static int do_loadFile (void *o, Tcl_Interp *interp,
1711 int argc, char **argv)
1713 IRSetObj *setobj = o;
1723 interp->result = "wrong # args";
1726 inf = fopen (argv[2], "r");
1729 Tcl_AppendResult (interp, "Cannot open ", argv[2], NULL);
1732 while ((buf = ir_tcl_fread_marc (inf, &size)))
1736 rl = new_IR_record (setobj, no, Z_NamePlusRecord_databaseRecord);
1737 rl->u.dbrec.buf = buf;
1738 rl->u.dbrec.size = size;
1741 setobj->numberOfRecordsReturned = no-1;
1746 static IRMethod ir_set_method_tab[] = {
1747 { 0, "search", do_search },
1748 { 0, "searchStatus", do_searchStatus },
1749 { 0, "presentStatus", do_presentStatus },
1750 { 0, "nextResultSetPosition", do_nextResultSetPosition },
1751 { 0, "setName", do_setName },
1752 { 0, "resultCount", do_resultCount },
1753 { 0, "numberOfRecordsReturned", do_numberOfRecordsReturned },
1754 { 0, "present", do_present },
1755 { 0, "recordType", do_recordType },
1756 { 0, "getMarc", do_getMarc },
1757 { 0, "Diag", do_recordDiag },
1758 { 0, "responseStatus", do_responseStatus },
1759 { 0, "loadFile", do_loadFile },
1764 * ir_set_obj_method: IR Set Object methods
1766 static int ir_set_obj_method (ClientData clientData, Tcl_Interp *interp,
1767 int argc, char **argv)
1770 IRSetObj *p = clientData;
1774 interp->result = "wrong # args";
1777 tabs[0].tab = ir_set_method_tab;
1779 tabs[1].tab = ir_set_c_method_tab;
1780 tabs[1].obj = &p->set_inher;
1783 return ir_method (interp, argc, argv, tabs);
1787 * ir_set_obj_delete: IR Set Object disposal
1789 static void ir_set_obj_delete (ClientData clientData)
1792 IRSetObj *p = clientData;
1794 tabs[0].tab = ir_set_method_tab;
1796 tabs[1].tab = ir_set_c_method_tab;
1797 tabs[1].obj = &p->set_inher;
1800 ir_method (NULL, -1, NULL, tabs);
1806 * ir_set_obj_mk: IR Set Object creation
1808 static int ir_set_obj_mk (ClientData clientData, Tcl_Interp *interp,
1809 int argc, char **argv)
1814 if (argc < 2 || argc > 3)
1816 interp->result = "wrong # args";
1821 Tcl_CmdInfo parent_info;
1826 if (!Tcl_GetCommandInfo (interp, argv[2], &parent_info))
1828 interp->result = "No parent";
1831 if (!(obj = ir_malloc (interp, sizeof(*obj))))
1833 obj->parent = (IRObj *) parent_info.clientData;
1835 dst = &obj->set_inher;
1836 src = &obj->parent->set_inher;
1838 dst->num_databaseNames = src->num_databaseNames;
1839 if (!(dst->databaseNames =
1840 ir_malloc (interp, sizeof (*dst->databaseNames)
1841 * dst->num_databaseNames)))
1843 for (i = 0; i < dst->num_databaseNames; i++)
1845 if (ir_strdup (interp, &dst->databaseNames[i],
1846 src->databaseNames[i]) == TCL_ERROR)
1849 if (ir_strdup (interp, &dst->queryType, src->queryType)
1853 dst->smallSetUpperBound = src->smallSetUpperBound;
1854 dst->largeSetLowerBound = src->largeSetLowerBound;
1855 dst->mediumSetPresentNumber = src->mediumSetPresentNumber;
1860 tabs[0].tab = ir_set_method_tab;
1864 if (ir_method (interp, 0, NULL, tabs) == TCL_ERROR)
1867 Tcl_CreateCommand (interp, argv[1], ir_set_obj_method,
1868 (ClientData) obj, ir_set_obj_delete);
1872 /* ------------------------------------------------------- */
1875 * do_scan: Perform scan
1877 static int do_scan (void *o, Tcl_Interp *interp, int argc, char **argv)
1880 Z_APDU apdu, *apdup = &apdu;
1882 IRObj *p = obj->parent;
1886 struct ccl_rpn_node *rpn;
1895 interp->result = "wrong # args";
1898 if (!p->set_inher.num_databaseNames)
1900 interp->result = "no databaseNames";
1905 interp->result = "not connected";
1908 odr_reset (p->odr_out);
1910 bib1.proto = PROTO_Z3950;
1911 bib1.class = CLASS_ATTSET;
1912 bib1.value = VAL_BIB1;
1914 apdu.which = Z_APDU_scanRequest;
1915 apdu.u.scanRequest = &req;
1916 req.referenceId = NULL;
1917 req.num_databaseNames = p->set_inher.num_databaseNames;
1918 req.databaseNames = p->set_inher.databaseNames;
1919 req.attributeSet = oid_getoidbyent (&bib1);
1922 if (!(req.termListAndStartPoint = p_query_scan (p->odr_out, argv[2])))
1924 Tcl_AppendResult (interp, "Syntax error in query", NULL);
1928 rpn = ccl_find_str(p->bibset, argv[2], &r, &pos);
1931 Tcl_AppendResult (interp, "CCL error: ", ccl_err_msg (r), NULL);
1934 ccl_pr_tree (rpn, stderr);
1935 fprintf (stderr, "\n");
1936 if (!(req.termListAndStartPoint = ccl_scan_query (rpn)))
1939 req.stepSize = &obj->stepSize;
1940 req.numberOfTermsRequested = &obj->numberOfTermsRequested;
1941 req.preferredPositionInResponse = &obj->preferredPositionInResponse;
1942 logf (LOG_DEBUG, "stepSize=%d", *req.stepSize);
1943 logf (LOG_DEBUG, "numberOfTermsRequested=%d",
1944 *req.numberOfTermsRequested);
1945 logf (LOG_DEBUG, "preferredPositionInResponse=%d",
1946 *req.preferredPositionInResponse);
1948 if (!z_APDU (p->odr_out, &apdup, 0))
1950 interp->result = odr_errlist [odr_geterror (p->odr_out)];
1951 odr_reset (p->odr_out);
1954 p->sbuf = odr_getbuf (p->odr_out, &p->slen, NULL);
1955 if ((r=cs_put (p->cs_link, p->sbuf, p->slen)) < 0)
1957 interp->result = "cs_put failed in scan";
1962 ir_select_add_write (cs_fileno(p->cs_link), p);
1963 logf (LOG_DEBUG, "Sent part of scanRequest (%d bytes)", p->slen);
1967 logf (LOG_DEBUG, "Whole scan request (%d bytes)", p->slen);
1973 * do_stepSize: Set/get replace Step Size
1975 static int do_stepSize (void *obj, Tcl_Interp *interp,
1976 int argc, char **argv)
1984 return get_set_int (&p->stepSize, interp, argc, argv);
1988 * do_numberOfTermsRequested: Set/get Number of Terms requested
1990 static int do_numberOfTermsRequested (void *obj, Tcl_Interp *interp,
1991 int argc, char **argv)
1997 p->numberOfTermsRequested = 20;
2000 return get_set_int (&p->numberOfTermsRequested, interp, argc, argv);
2005 * do_preferredPositionInResponse: Set/get preferred Position
2007 static int do_preferredPositionInResponse (void *obj, Tcl_Interp *interp,
2008 int argc, char **argv)
2014 p->preferredPositionInResponse = 1;
2017 return get_set_int (&p->preferredPositionInResponse, interp, argc, argv);
2021 * do_scanStatus: Get scan status
2023 static int do_scanStatus (void *obj, Tcl_Interp *interp,
2024 int argc, char **argv)
2030 return get_set_int (&p->scanStatus, interp, argc, argv);
2034 * do_numberOfEntriesReturned: Get number of Entries returned
2036 static int do_numberOfEntriesReturned (void *obj, Tcl_Interp *interp,
2037 int argc, char **argv)
2043 return get_set_int (&p->numberOfEntriesReturned, interp, argc, argv);
2047 * do_positionOfTerm: Get position of Term
2049 static int do_positionOfTerm (void *obj, Tcl_Interp *interp,
2050 int argc, char **argv)
2056 return get_set_int (&p->positionOfTerm, interp, argc, argv);
2060 * do_scanLine: get Scan Line (surrogate or normal) after response
2062 static int do_scanLine (void *obj, Tcl_Interp *interp, int argc, char **argv)
2070 p->entries_flag = 0;
2072 p->nonSurrogateDiagnostics = NULL;
2075 else if (argc == -1)
2077 p->entries_flag = 0;
2078 /* release entries */
2080 /* release non diagnostics */
2081 p->nonSurrogateDiagnostics = NULL;
2086 interp->result = "wrong # args";
2089 if (Tcl_GetInt (interp, argv[2], &i) == TCL_ERROR)
2091 if (!p->entries_flag || p->which != Z_ListEntries_entries || !p->entries
2092 || i >= p->num_entries || i < 0)
2094 switch (p->entries[i].which)
2096 case Z_Entry_termInfo:
2097 Tcl_AppendElement (interp, "T");
2098 if (p->entries[i].u.term.buf)
2099 Tcl_AppendElement (interp, p->entries[i].u.term.buf);
2101 Tcl_AppendElement (interp, "");
2102 sprintf (numstr, "%d", p->entries[i].u.term.globalOccurrences);
2103 Tcl_AppendElement (interp, numstr);
2105 case Z_Entry_surrogateDiagnostic:
2107 mk_nonSurrogateDiagnostics (interp, p->entries[i].u.diag.condition,
2108 p->entries[i].u.diag.addinfo);
2114 static IRMethod ir_scan_method_tab[] = {
2115 { 0, "scan", do_scan },
2116 { 0, "stepSize", do_stepSize },
2117 { 0, "numberOfTermsRequested", do_numberOfTermsRequested },
2118 { 0, "preferredPositionInResponse", do_preferredPositionInResponse },
2119 { 0, "scanStatus", do_scanStatus },
2120 { 0, "numberOfEntriesReturned", do_numberOfEntriesReturned },
2121 { 0, "positionOfTerm", do_positionOfTerm },
2122 { 0, "scanLine", do_scanLine },
2127 * ir_scan_obj_method: IR Scan Object methods
2129 static int ir_scan_obj_method (ClientData clientData, Tcl_Interp *interp,
2130 int argc, char **argv)
2136 interp->result = "wrong # args";
2139 tabs[0].tab = ir_scan_method_tab;
2140 tabs[0].obj = clientData;
2143 return ir_method (interp, argc, argv, tabs);
2147 * ir_scan_obj_delete: IR Scan Object disposal
2149 static void ir_scan_obj_delete (ClientData clientData)
2151 free ( (void*) clientData);
2155 * ir_scan_obj_mk: IR Scan Object creation
2157 static int ir_scan_obj_mk (ClientData clientData, Tcl_Interp *interp,
2158 int argc, char **argv)
2160 Tcl_CmdInfo parent_info;
2166 interp->result = "wrong # args";
2169 if (get_parent_info (interp, argv[1], &parent_info, NULL) == TCL_ERROR)
2171 if (!(obj = ir_malloc (interp, sizeof(*obj))))
2174 tabs[0].tab = ir_scan_method_tab;
2175 tabs[0].obj = clientData;
2178 if (ir_method (interp, 0, NULL, tabs) == TCL_ERROR)
2182 obj->numberOfTermsRequested = 20;
2183 obj->preferredPositionInResponse = 1;
2185 obj->entries = NULL;
2186 obj->nonSurrogateDiagnostics = NULL;
2189 obj->parent = (IRObj *) parent_info.clientData;
2190 Tcl_CreateCommand (interp, argv[1], ir_scan_obj_method,
2191 (ClientData) obj, ir_scan_obj_delete);
2195 /* ------------------------------------------------------- */
2197 static void ir_initResponse (void *obj, Z_InitResponse *initrs)
2201 p->initResult = *initrs->result ? 1 : 0;
2202 if (!*initrs->result)
2203 logf (LOG_DEBUG, "Connection rejected by target");
2205 logf (LOG_DEBUG, "Connection accepted by target");
2207 free (p->targetImplementationId);
2208 ir_strdup (p->interp, &p->targetImplementationId,
2209 initrs->implementationId);
2210 free (p->targetImplementationName);
2211 ir_strdup (p->interp, &p->targetImplementationName,
2212 initrs->implementationName);
2213 free (p->targetImplementationVersion);
2214 ir_strdup (p->interp, &p->targetImplementationVersion,
2215 initrs->implementationVersion);
2217 p->maximumRecordSize = *initrs->maximumRecordSize;
2218 p->preferredMessageSize = *initrs->preferredMessageSize;
2220 memcpy (&p->options, initrs->options, sizeof(initrs->options));
2221 memcpy (&p->protocolVersion, initrs->protocolVersion,
2222 sizeof(initrs->protocolVersion));
2223 free (p->userInformationField);
2224 p->userInformationField = NULL;
2225 if (initrs->userInformationField)
2229 if (initrs->userInformationField->which == ODR_EXTERNAL_octet &&
2230 (p->userInformationField =
2232 initrs->userInformationField->u.octet_aligned->len)
2235 memcpy (p->userInformationField,
2236 initrs->userInformationField->u.octet_aligned->buf,
2238 (p->userInformationField)[len] = '\0';
2243 static void ir_handleRecords (void *o, Z_Records *zrs)
2246 IRSetObj *setobj = p->set_child;
2248 setobj->which = zrs->which;
2249 setobj->recordFlag = 1;
2250 if (zrs->which == Z_Records_NSD)
2252 const char *addinfo;
2254 setobj->numberOfRecordsReturned = 0;
2255 setobj->condition = *zrs->u.nonSurrogateDiagnostic->condition;
2256 free (setobj->addinfo);
2257 setobj->addinfo = NULL;
2258 addinfo = zrs->u.nonSurrogateDiagnostic->addinfo;
2259 if (addinfo && (setobj->addinfo = malloc (strlen(addinfo) + 1)))
2260 strcpy (setobj->addinfo, addinfo);
2261 logf (LOG_DEBUG, "Diagnostic response. %s (%d): %s",
2262 diagbib1_str (setobj->condition),
2264 setobj->addinfo ? setobj->addinfo : "");
2271 setobj->numberOfRecordsReturned =
2272 zrs->u.databaseOrSurDiagnostics->num_records;
2273 logf (LOG_DEBUG, "Got %d records", setobj->numberOfRecordsReturned);
2274 for (offset = 0; offset<setobj->numberOfRecordsReturned; offset++)
2276 rl = new_IR_record (setobj, setobj->start + offset,
2277 zrs->u.databaseOrSurDiagnostics->
2278 records[offset]->which);
2279 if (rl->which == Z_NamePlusRecord_surrogateDiagnostic)
2283 diagrec = zrs->u.databaseOrSurDiagnostics->
2284 records[offset]->u.surrogateDiagnostic;
2286 rl->u.diag.condition = *diagrec->condition;
2287 if (diagrec->addinfo && (rl->u.diag.addinfo =
2288 malloc (strlen (diagrec->addinfo)+1)))
2289 strcpy (rl->u.diag.addinfo, diagrec->addinfo);
2293 Z_DatabaseRecord *zr;
2296 zr = zrs->u.databaseOrSurDiagnostics->records[offset]
2298 oe = (Odr_external*) zr;
2299 rl->u.dbrec.size = zr->u.octet_aligned->len;
2300 if (oe->which == ODR_EXTERNAL_octet && rl->u.dbrec.size > 0)
2302 const char *buf = (char*) zr->u.octet_aligned->buf;
2303 if ((rl->u.dbrec.buf = malloc (rl->u.dbrec.size)))
2304 memcpy (rl->u.dbrec.buf, buf, rl->u.dbrec.size);
2307 rl->u.dbrec.buf = NULL;
2313 static void ir_searchResponse (void *o, Z_SearchResponse *searchrs)
2316 IRSetObj *setobj = p->set_child;
2317 Z_Records *zrs = searchrs->records;
2321 setobj->searchStatus = searchrs->searchStatus ? 1 : 0;
2322 setobj->resultCount = *searchrs->resultCount;
2323 if (searchrs->presentStatus)
2324 setobj->presentStatus = *searchrs->presentStatus;
2325 if (searchrs->nextResultSetPosition)
2326 setobj->nextResultSetPosition = *searchrs->nextResultSetPosition;
2328 logf (LOG_DEBUG, "Search response %d, %d hits",
2329 setobj->searchStatus, setobj->resultCount);
2331 ir_handleRecords (o, zrs);
2333 setobj->recordFlag = 0;
2336 logf (LOG_DEBUG, "Search response, no object!");
2340 static void ir_presentResponse (void *o, Z_PresentResponse *presrs)
2343 IRSetObj *setobj = p->set_child;
2344 Z_Records *zrs = presrs->records;
2346 logf (LOG_DEBUG, "Received presentResponse");
2347 setobj->presentStatus = *presrs->presentStatus;
2348 setobj->nextResultSetPosition = *presrs->nextResultSetPosition;
2350 ir_handleRecords (o, zrs);
2353 setobj->recordFlag = 0;
2354 logf (LOG_DEBUG, "No records!");
2358 static void ir_scanResponse (void *o, Z_ScanResponse *scanrs)
2361 IRScanObj *scanobj = p->scan_child;
2363 logf (LOG_DEBUG, "Received scanResponse");
2365 scanobj->scanStatus = *scanrs->scanStatus;
2366 logf (LOG_DEBUG, "scanStatus=%d", scanobj->scanStatus);
2368 if (scanrs->stepSize)
2369 scanobj->stepSize = *scanrs->stepSize;
2370 logf (LOG_DEBUG, "stepSize=%d", scanobj->stepSize);
2372 scanobj->numberOfEntriesReturned = *scanrs->numberOfEntriesReturned;
2373 logf (LOG_DEBUG, "numberOfEntriesReturned=%d",
2374 scanobj->numberOfEntriesReturned);
2376 if (scanrs->positionOfTerm)
2377 scanobj->positionOfTerm = *scanrs->positionOfTerm;
2379 scanobj->positionOfTerm = -1;
2380 logf (LOG_DEBUG, "positionOfTerm=%d", scanobj->positionOfTerm);
2382 free (scanobj->entries);
2383 scanobj->entries = NULL;
2384 free (scanobj->nonSurrogateDiagnostics);
2385 scanobj->nonSurrogateDiagnostics = NULL;
2387 if (scanrs->entries)
2392 scanobj->entries_flag = 1;
2393 scanobj->which = scanrs->entries->which;
2394 switch (scanobj->which)
2396 case Z_ListEntries_entries:
2397 scanobj->num_entries = scanrs->entries->u.entries->num_entries;
2398 scanobj->entries = malloc (scanobj->num_entries *
2399 sizeof(*scanobj->entries));
2400 for (i=0; i<scanobj->num_entries; i++)
2402 ze = scanrs->entries->u.entries->entries[i];
2403 scanobj->entries[i].which = ze->which;
2406 case Z_Entry_termInfo:
2407 if (ze->u.termInfo->term->which == Z_Term_general)
2409 int l = ze->u.termInfo->term->u.general->len;
2410 scanobj->entries[i].u.term.buf = malloc (1+l);
2411 memcpy (scanobj->entries[i].u.term.buf,
2412 ze->u.termInfo->term->u.general->buf,
2414 scanobj->entries[i].u.term.buf[l] = '\0';
2417 scanobj->entries[i].u.term.buf = NULL;
2418 if (ze->u.termInfo->globalOccurrences)
2419 scanobj->entries[i].u.term.globalOccurrences =
2420 *ze->u.termInfo->globalOccurrences;
2422 scanobj->entries[i].u.term.globalOccurrences = 0;
2424 case Z_Entry_surrogateDiagnostic:
2425 scanobj->entries[i].u.diag.addinfo =
2426 malloc (1+strlen(ze->u.surrogateDiagnostic->
2428 strcpy (scanobj->entries[i].u.diag.addinfo,
2429 ze->u.surrogateDiagnostic->addinfo);
2430 scanobj->entries[i].u.diag.condition =
2431 *ze->u.surrogateDiagnostic->condition;
2436 case Z_ListEntries_nonSurrogateDiagnostics:
2437 scanobj->num_diagRecs = scanrs->entries->
2438 u.nonSurrogateDiagnostics->num_diagRecs;
2439 scanobj->nonSurrogateDiagnostics = malloc (scanobj->num_diagRecs *
2440 sizeof(*scanobj->nonSurrogateDiagnostics));
2445 scanobj->entries_flag = 0;
2449 * ir_select_read: handle incoming packages
2451 void ir_select_read (ClientData clientData)
2453 IRObj *p = clientData;
2459 r = cs_rcvconnect (p->cs_link);
2463 ir_select_remove_write (cs_fileno (p->cs_link), p);
2466 logf (LOG_DEBUG, "cs_rcvconnect error");
2468 Tcl_Eval (p->interp, p->failback);
2469 do_disconnect (p, NULL, 2, NULL);
2473 Tcl_Eval (p->interp, p->callback);
2478 /* signal one more use of ir object - callbacks must not
2479 release the ir memory (p pointer) */
2481 if ((r=cs_get (p->cs_link, &p->buf_in, &p->len_in)) <= 0)
2483 logf (LOG_DEBUG, "cs_get failed, code %d", r);
2484 ir_select_remove (cs_fileno (p->cs_link), p);
2486 Tcl_Eval (p->interp, p->failback);
2487 do_disconnect (p, NULL, 2, NULL);
2489 /* relase ir object now if callback deleted it */
2495 odr_setbuf (p->odr_in, p->buf_in, r, 0);
2496 logf (LOG_DEBUG, "cs_get ok, got %d", r);
2497 if (!z_APDU (p->odr_in, &apdu, 0))
2499 logf (LOG_DEBUG, "%s", odr_errlist [odr_geterror (p->odr_in)]);
2501 Tcl_Eval (p->interp, p->failback);
2502 do_disconnect (p, NULL, 2, NULL);
2504 /* relase ir object now if callback deleted it */
2510 case Z_APDU_initResponse:
2511 ir_initResponse (p, apdu->u.initResponse);
2513 case Z_APDU_searchResponse:
2514 ir_searchResponse (p, apdu->u.searchResponse);
2516 case Z_APDU_presentResponse:
2517 ir_presentResponse (p, apdu->u.presentResponse);
2519 case Z_APDU_scanResponse:
2520 ir_scanResponse (p, apdu->u.scanResponse);
2523 logf (LOG_WARN, "Received unknown APDU type (%d)", apdu->which);
2525 Tcl_Eval (p->interp, p->failback);
2526 do_disconnect (p, NULL, 2, NULL);
2528 odr_reset (p->odr_in);
2530 Tcl_Eval (p->interp, p->callback);
2531 if (p->ref_count == 1)
2537 } while (p->cs_link && cs_more (p->cs_link));
2541 * ir_select_write: handle outgoing packages - not yet written.
2543 void ir_select_write (ClientData clientData)
2545 IRObj *p = clientData;
2548 logf (LOG_DEBUG, "In write handler");
2551 r = cs_rcvconnect (p->cs_link);
2557 logf (LOG_DEBUG, "cs_rcvconnect error");
2558 ir_select_remove_write (cs_fileno (p->cs_link), p);
2560 Tcl_Eval (p->interp, p->failback);
2561 do_disconnect (p, NULL, 2, NULL);
2564 ir_select_remove_write (cs_fileno (p->cs_link), p);
2566 Tcl_Eval (p->interp, p->callback);
2569 if ((r=cs_put (p->cs_link, p->sbuf, p->slen)) < 0)
2571 logf (LOG_DEBUG, "select write fail");
2573 Tcl_Eval (p->interp, p->failback);
2574 do_disconnect (p, NULL, 2, NULL);
2576 else if (r == 0) /* remove select bit */
2578 ir_select_remove_write (cs_fileno (p->cs_link), p);
2582 /* ------------------------------------------------------- */
2585 * ir_tcl_init: Registration of TCL commands.
2587 int ir_tcl_init (Tcl_Interp *interp)
2589 Tcl_CreateCommand (interp, "ir", ir_obj_mk, (ClientData) NULL,
2590 (Tcl_CmdDeleteProc *) NULL);
2591 Tcl_CreateCommand (interp, "ir-set", ir_set_obj_mk,
2592 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
2593 Tcl_CreateCommand (interp, "ir-scan", ir_scan_obj_mk,
2594 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);