2 * IR toolkit for tcl/tk
4 * See the file LICENSE for details.
5 * Sebastian Hammer, Adam Dickmeiss
8 * Revision 1.35 1995-05-31 08:36:33 adam
9 * Bug fix in client.tcl: didn't save options on clientrc.tcl.
10 * New method: referenceId. More work on scan.
12 * Revision 1.34 1995/05/29 10:33:42 adam
13 * README and rename of startup script.
15 * Revision 1.33 1995/05/29 09:15:11 quinn
16 * Changed CS_SR to PROTO_SR, etc.
18 * Revision 1.32 1995/05/29 08:44:16 adam
19 * Work on delete of objects.
21 * Revision 1.31 1995/05/26 11:44:10 adam
22 * Bugs fixed. More work on MARC utilities and queries. Test
23 * client is up-to-date again.
25 * Revision 1.30 1995/05/26 08:54:11 adam
26 * New MARC utilities. Uses prefix query.
28 * Revision 1.29 1995/05/24 14:10:22 adam
29 * Work on idAuthentication, protocolVersion and options.
31 * Revision 1.28 1995/05/23 15:34:48 adam
32 * Many new settings, userInformationField, smallSetUpperBound, etc.
33 * A number of settings are inherited when ir-set is executed.
34 * This version is incompatible with the graphical test client (client.tcl).
36 * Revision 1.27 1995/05/11 15:34:47 adam
37 * Scan request changed a bit. This version works with RLG.
39 * Revision 1.26 1995/04/18 16:11:51 adam
40 * First version of graphical Scan. Some work on query-by-form.
42 * Revision 1.25 1995/04/17 09:37:17 adam
43 * Further development of scan.
45 * Revision 1.24 1995/04/11 14:16:42 adam
46 * Further work on scan. Response works. Entries aren't saved yet.
48 * Revision 1.23 1995/04/10 10:50:27 adam
49 * Result-set name defaults to suffix of ir-set name.
50 * Started working on scan. Not finished at this point.
52 * Revision 1.22 1995/03/31 10:43:03 adam
53 * More robust when getting bad MARC records.
55 * Revision 1.21 1995/03/31 08:56:37 adam
56 * New button "Search".
58 * Revision 1.20 1995/03/29 16:07:09 adam
59 * Bug fix: Didn't use setName in present request.
61 * Revision 1.19 1995/03/28 12:45:23 adam
62 * New ir method failback: called on disconnect/protocol error.
63 * New ir set/get method: protocol: SR / Z3950.
64 * Simple popup and disconnect when failback is invoked.
66 * Revision 1.18 1995/03/21 15:50:12 adam
69 * Revision 1.17 1995/03/21 13:41:03 adam
70 * Comstack cs_create not used too often. Non-blocking connect.
72 * Revision 1.16 1995/03/21 08:26:06 adam
73 * New method, setName, to specify the result set name (other than Default).
74 * New method, responseStatus, which returns diagnostic info, if any, after
75 * present response / search response.
77 * Revision 1.15 1995/03/20 15:24:07 adam
78 * Diagnostic records saved on searchResponse.
80 * Revision 1.14 1995/03/20 08:53:22 adam
81 * Event loop in tclmain.c rewritten. New method searchStatus.
83 * Revision 1.13 1995/03/17 18:26:17 adam
84 * Non-blocking i/o used now. Database names popup as cascade items.
86 * Revision 1.12 1995/03/17 15:45:00 adam
87 * Improved target/database setup.
89 * Revision 1.11 1995/03/16 17:54:03 adam
90 * Minor changes really.
92 * Revision 1.10 1995/03/15 16:14:50 adam
93 * Blocking arg in cs_create changed.
95 * Revision 1.9 1995/03/15 13:59:24 adam
98 * Revision 1.8 1995/03/15 08:25:16 adam
99 * New method presentStatus to check for error on present. Misc. cleanup
100 * of IRRecordList manipulations. Full MARC record presentation in
103 * Revision 1.7 1995/03/14 17:32:29 adam
104 * Presentation of full Marc record in popup window.
106 * Revision 1.6 1995/03/12 19:31:55 adam
107 * Pattern matching implemented when retrieving MARC records. More
108 * diagnostic functions.
110 * Revision 1.5 1995/03/10 18:00:15 adam
111 * Actual presentation in line-by-line format. RPN query support.
113 * Revision 1.4 1995/03/09 16:15:08 adam
114 * First presentRequest attempts. Hot-target list.
120 #include <sys/time.h>
130 int (*method) (void *obj, Tcl_Interp *interp, int argc, char **argv);
138 static int do_disconnect (void *obj,Tcl_Interp *interp, int argc, char **argv);
140 static IRRecordList *new_IR_record (IRSetObj *setobj, int no, int which)
144 for (rl = setobj->record_list; rl; rl = rl->next)
150 case Z_NamePlusRecord_databaseRecord:
151 free (rl->u.dbrec.buf);
152 rl->u.dbrec.buf = NULL;
154 case Z_NamePlusRecord_surrogateDiagnostic:
155 free (rl->u.diag.addinfo);
156 rl->u.diag.addinfo = NULL;
164 rl = malloc (sizeof(*rl));
166 rl->next = setobj->record_list;
168 setobj->record_list = rl;
174 static IRRecordList *find_IR_record (IRSetObj *setobj, int no)
178 for (rl = setobj->record_list; rl; rl = rl->next)
184 static void delete_IR_records (IRSetObj *setobj)
186 IRRecordList *rl, *rl1;
188 for (rl = setobj->record_list; rl; rl = rl1)
192 case Z_NamePlusRecord_databaseRecord:
193 free (rl->u.dbrec.buf);
195 case Z_NamePlusRecord_surrogateDiagnostic:
196 free (rl->u.diag.addinfo);
202 setobj->record_list = NULL;
206 * getsetint: Set/get integer value
208 static int get_set_int (int *val, Tcl_Interp *interp, int argc, char **argv)
214 if (Tcl_GetInt (interp, argv[2], val)==TCL_ERROR)
217 sprintf (buf, "%d", *val);
218 Tcl_AppendResult (interp, buf, NULL);
223 * mk_nonSurrogateDiagnostics: Make Tcl result with diagnostic info
225 static int mk_nonSurrogateDiagnostics (Tcl_Interp *interp,
232 Tcl_AppendElement (interp, "NSD");
233 sprintf (buf, "%d", condition);
234 Tcl_AppendElement (interp, buf);
235 cp = diagbib1_str (condition);
237 Tcl_AppendElement (interp, (char*) cp);
239 Tcl_AppendElement (interp, "");
241 Tcl_AppendElement (interp, (char*) addinfo);
243 Tcl_AppendElement (interp, "");
248 * ir_method: Search for method in table and invoke method handler
250 int ir_method (Tcl_Interp *interp, int argc, char **argv, IRMethods *tab)
252 IRMethods *tab_i = tab;
255 for (tab_i = tab; tab_i->tab; tab_i++)
256 for (t = tab_i->tab; t->name; t++)
259 if ((*t->method)(tab_i->obj, interp, argc, argv) == TCL_ERROR)
263 if (!strcmp (t->name, argv[1]))
264 return (*t->method)(tab_i->obj, interp, argc, argv);
268 Tcl_AppendResult (interp, "Bad method. Possible methods:", NULL);
269 for (tab_i = tab; tab_i->tab; tab_i++)
270 for (t = tab_i->tab; t->name; t++)
271 Tcl_AppendResult (interp, " ", t->name, NULL);
276 * ir_method_r: Get status for all readable elements
278 int ir_method_r (void *obj, Tcl_Interp *interp, int argc, char **argv,
286 for (; tab->name; tab++)
289 argv_n[1] = tab->name;
290 Tcl_AppendResult (interp, "{", NULL);
291 (*tab->method)(obj, interp, argc_n, argv_n);
292 Tcl_AppendResult (interp, "} ", NULL);
298 * ir_named_bits: get/set named bits
300 int ir_named_bits (struct ir_named_entry *tab, Odr_bitmask *ob,
301 Tcl_Interp *interp, int argc, char **argv)
303 struct ir_named_entry *ti;
308 for (no = 0; no < argc; no++)
310 for (ti = tab; ti->name; ti++)
311 if (!strcmp (argv[no], ti->name))
313 ODR_MASK_SET (ob, ti->pos);
318 Tcl_AppendResult (interp, "Bad bit mask: ", argv[no], NULL);
324 for (ti = tab; ti->name; ti++)
325 if (ODR_MASK_GET (ob, ti->pos))
326 Tcl_AppendElement (interp, ti->name);
331 * ir_strdup: Duplicate string
333 int ir_strdup (Tcl_Interp *interp, char** p, const char *s)
335 *p = malloc (strlen(s)+1);
338 interp->result = "strdup fail";
346 * ir_strdel: Delete string
348 int ir_strdel (Tcl_Interp *interp, char **p)
356 * ir_malloc: Malloc function
358 void *ir_malloc (Tcl_Interp *interp, size_t size)
360 static char buf[128];
361 void *p = malloc (size);
365 sprintf (buf, "Malloc fail. %ld bytes requested", (long) size);
366 interp->result = buf;
372 static void set_referenceId (ODR o, Z_ReferenceId **dst, const char *src)
378 *dst = odr_malloc (o, sizeof(**dst));
379 (*dst)->size = (*dst)->len = strlen(src);
380 (*dst)->buf = odr_malloc (o, (*dst)->len);
381 memcpy ((*dst)->buf, src, (*dst)->len);
384 /* ------------------------------------------------------- */
387 * do_init_request: init method on IR object
389 static int do_init_request (void *obj, Tcl_Interp *interp,
390 int argc, char **argv)
392 Z_APDU apdu, *apdup = &apdu;
401 interp->result = "not connected";
404 odr_reset (p->odr_out);
406 set_referenceId (p->odr_out, &req.referenceId, p->set_inher.referenceId);
407 req.options = &p->options;
408 req.protocolVersion = &p->protocolVersion;
409 req.preferredMessageSize = &p->preferredMessageSize;
410 req.maximumRecordSize = &p->maximumRecordSize;
412 if (p->idAuthenticationGroupId)
414 Z_IdPass *pass = odr_malloc (p->odr_out, sizeof(*pass));
415 Z_IdAuthentication *auth = odr_malloc (p->odr_out, sizeof(*auth));
417 auth->which = Z_IdAuthentication_idPass;
418 auth->u.idPass = pass;
419 if (p->idAuthenticationGroupId && *p->idAuthenticationGroupId)
420 pass->groupId = p->idAuthenticationGroupId;
422 pass->groupId = NULL;
423 if (p->idAuthenticationUserId && *p->idAuthenticationUserId)
424 pass->userId = p->idAuthenticationUserId;
427 if (p->idAuthenticationPassword && *p->idAuthenticationPassword)
428 pass->password = p->idAuthenticationPassword;
430 pass->password = NULL;
431 req.idAuthentication = auth;
433 else if (!p->idAuthenticationOpen || !*p->idAuthenticationOpen)
434 req.idAuthentication = NULL;
437 Z_IdAuthentication *auth = odr_malloc (p->odr_out, sizeof(*auth));
439 auth->which = Z_IdAuthentication_open;
440 auth->u.open = p->idAuthenticationOpen;
441 req.idAuthentication = auth;
443 req.implementationId = p->implementationId;
444 req.implementationName = p->implementationName;
445 req.implementationVersion = "0.1";
446 req.userInformationField = 0;
448 apdu.u.initRequest = &req;
449 apdu.which = Z_APDU_initRequest;
451 if (!z_APDU (p->odr_out, &apdup, 0))
453 Tcl_AppendResult (interp, odr_errlist [odr_geterror (p->odr_out)],
455 odr_reset (p->odr_out);
458 p->sbuf = odr_getbuf (p->odr_out, &p->slen, NULL);
459 if ((r=cs_put (p->cs_link, p->sbuf, p->slen)) < 0)
461 interp->result = "cs_put failed in init";
462 do_disconnect (p, NULL, 2, NULL);
467 ir_select_add_write (cs_fileno(p->cs_link), p);
468 logf (LOG_DEBUG, "Sent part of initializeRequest (%d bytes)", p->slen);
471 logf (LOG_DEBUG, "Sent whole initializeRequest (%d bytes)", p->slen);
476 * do_protocolVersion: Set protocol Version
478 static int do_protocolVersion (void *obj, Tcl_Interp *interp,
479 int argc, char **argv)
481 static struct ir_named_entry version_tab[] = {
492 ODR_MASK_ZERO (&p->protocolVersion);
493 ODR_MASK_SET (&p->protocolVersion, 0);
494 ODR_MASK_SET (&p->protocolVersion, 1);
497 return ir_named_bits (version_tab, &p->protocolVersion,
498 interp, argc-2, argv+2);
502 * do_options: Set options
504 static int do_options (void *obj, Tcl_Interp *interp,
505 int argc, char **argv)
507 static struct ir_named_entry options_tab[] = {
511 { "resourceReport", 3 },
512 { "triggerResourceCtrl", 4},
513 { "resourceCtrl", 5},
517 { "extentedServices", 10},
518 { "level-1Segmentation", 11},
519 { "level-2Segmentation", 12},
520 { "concurrentOperations", 13},
521 { "namedResultSets", 14},
528 ODR_MASK_ZERO (&p->options);
529 ODR_MASK_SET (&p->options, 0);
530 ODR_MASK_SET (&p->options, 1);
531 ODR_MASK_SET (&p->options, 7);
532 ODR_MASK_SET (&p->options, 14);
535 return ir_named_bits (options_tab, &p->options, interp, argc-2, argv+2);
539 * do_preferredMessageSize: Set/get preferred message size
541 static int do_preferredMessageSize (void *obj, Tcl_Interp *interp,
542 int argc, char **argv)
548 p->preferredMessageSize = 4096;
551 return get_set_int (&p->preferredMessageSize, interp, argc, argv);
555 * do_maximumRecordSize: Set/get maximum record size
557 static int do_maximumRecordSize (void *obj, Tcl_Interp *interp,
558 int argc, char **argv)
564 p->maximumRecordSize = 32768;
567 return get_set_int (&p->maximumRecordSize, interp, argc, argv);
571 * do_initResult: Get init result
573 static int do_initResult (void *obj, Tcl_Interp *interp,
574 int argc, char **argv)
580 return get_set_int (&p->initResult, interp, argc, argv);
585 * do_implementationName: Set/get Implementation Name.
587 static int do_implementationName (void *obj, Tcl_Interp *interp,
588 int argc, char **argv)
593 return ir_strdup (interp, &p->implementationName, "TCL/TK on YAZ");
595 return ir_strdel (interp, &p->implementationName);
598 free (p->implementationName);
599 if (ir_strdup (interp, &p->implementationName, argv[2])
603 Tcl_AppendResult (interp, p->implementationName, (char*) NULL);
608 * do_implementationId: Set/get Implementation Id.
610 static int do_implementationId (void *obj, Tcl_Interp *interp,
611 int argc, char **argv)
616 return ir_strdup (interp, &p->implementationId, "81");
618 return ir_strdel (interp, &p->implementationId);
621 free (p->implementationId);
622 if (ir_strdup (interp, &p->implementationId, argv[2]) == TCL_ERROR)
625 Tcl_AppendResult (interp, p->implementationId, (char*) NULL);
630 * do_targetImplementationName: Get Implementation Name of target.
632 static int do_targetImplementationName (void *obj, Tcl_Interp *interp,
633 int argc, char **argv)
639 p->targetImplementationName = NULL;
643 return ir_strdel (interp, &p->targetImplementationName);
644 Tcl_AppendResult (interp, p->targetImplementationName, (char*) NULL);
649 * do_targetImplementationId: Get Implementation Id of target
651 static int do_targetImplementationId (void *obj, Tcl_Interp *interp,
652 int argc, char **argv)
658 p->targetImplementationId = NULL;
662 return ir_strdel (interp, &p->targetImplementationId);
663 Tcl_AppendResult (interp, p->targetImplementationId, (char*) NULL);
668 * do_targetImplementationVersion: Get Implementation Version of target
670 static int do_targetImplementationVersion (void *obj, Tcl_Interp *interp,
671 int argc, char **argv)
677 p->targetImplementationVersion = NULL;
681 return ir_strdel (interp, &p->targetImplementationVersion);
682 Tcl_AppendResult (interp, p->targetImplementationVersion, (char*) NULL);
687 * do_idAuthentication: Set/get id Authentication
689 static int do_idAuthentication (void *obj, Tcl_Interp *interp,
690 int argc, char **argv)
694 if (argc >= 3 || argc == -1)
696 free (p->idAuthenticationOpen);
697 free (p->idAuthenticationGroupId);
698 free (p->idAuthenticationUserId);
699 free (p->idAuthenticationPassword);
701 if (argc >= 3 || argc <= 0)
703 p->idAuthenticationOpen = NULL;
704 p->idAuthenticationGroupId = NULL;
705 p->idAuthenticationUserId = NULL;
706 p->idAuthenticationPassword = NULL;
714 if (ir_strdup (interp, &p->idAuthenticationOpen, argv[2])
720 if (ir_strdup (interp, &p->idAuthenticationGroupId, argv[2])
723 if (ir_strdup (interp, &p->idAuthenticationUserId, argv[3])
726 if (ir_strdup (interp, &p->idAuthenticationPassword, argv[4])
731 if (p->idAuthenticationOpen)
732 Tcl_AppendElement (interp, p->idAuthenticationOpen);
733 else if (p->idAuthenticationGroupId)
735 Tcl_AppendElement (interp, p->idAuthenticationGroupId);
736 Tcl_AppendElement (interp, p->idAuthenticationUserId);
737 Tcl_AppendElement (interp, p->idAuthenticationPassword);
743 * do_connect: connect method on IR object
745 static int do_connect (void *obj, Tcl_Interp *interp,
746 int argc, char **argv)
751 int protocol_type = PROTO_Z3950;
759 interp->result = "already connected";
762 if (!strcmp (p->protocol_type, "Z3950"))
763 protocol_type = PROTO_Z3950;
764 else if (!strcmp (p->protocol_type, "SR"))
765 protocol_type = PROTO_SR;
768 interp->result = "bad protocol type";
771 if (!strcmp (p->cs_type, "tcpip"))
773 p->cs_link = cs_create (tcpip_type, CS_BLOCK, protocol_type);
774 addr = tcpip_strtoaddr (argv[2]);
777 interp->result = "tcpip_strtoaddr fail";
780 logf (LOG_DEBUG, "tcp/ip connect %s", argv[2]);
783 else if (!strcmp (p->cs_type, "mosi"))
785 p->cs_link = cs_create (mosi_type, CS_BLOCK, protocol_type);
786 addr = mosi_strtoaddr (argv[2]);
789 interp->result = "mosi_strtoaddr fail";
792 logf (LOG_DEBUG, "mosi connect %s", argv[2]);
797 interp->result = "unknown comstack type";
800 if (ir_strdup (interp, &p->hostname, argv[2]) == TCL_ERROR)
802 if ((r=cs_connect (p->cs_link, addr)) < 0)
804 interp->result = "cs_connect fail";
805 do_disconnect (p, NULL, 2, NULL);
808 ir_select_add (cs_fileno (p->cs_link), p);
811 ir_select_add_write (cs_fileno (p->cs_link), p);
818 Tcl_Eval (p->interp, p->callback);
822 Tcl_AppendElement (interp, p->hostname);
827 * do_disconnect: disconnect method on IR object
829 static int do_disconnect (void *obj, Tcl_Interp *interp,
830 int argc, char **argv)
845 ir_select_remove_write (cs_fileno (p->cs_link), p);
846 ir_select_remove (cs_fileno (p->cs_link), p);
849 cs_close (p->cs_link);
852 assert (!p->cs_link);
857 * do_comstack: Set/get comstack method on IR object
859 static int do_comstack (void *o, Tcl_Interp *interp,
860 int argc, char **argv)
865 return ir_strdup (interp, &obj->cs_type, "tcpip");
867 return ir_strdel (interp, &obj->cs_type);
871 if (ir_strdup (interp, &obj->cs_type, argv[2]) == TCL_ERROR)
874 Tcl_AppendElement (interp, obj->cs_type);
879 * do_protocol: Set/get protocol method on IR object
881 static int do_protocol (void *o, Tcl_Interp *interp,
882 int argc, char **argv)
887 return ir_strdup (interp, &obj->protocol_type, "Z3950");
889 return ir_strdel (interp, &obj->protocol_type);
892 free (obj->protocol_type);
893 if (ir_strdup (interp, &obj->protocol_type, argv[2]) == TCL_ERROR)
896 Tcl_AppendElement (interp, obj->protocol_type);
901 * do_callback: add callback
903 static int do_callback (void *obj, Tcl_Interp *interp,
904 int argc, char **argv)
914 return ir_strdel (interp, &p->callback);
920 if (ir_strdup (interp, &p->callback, argv[2]) == TCL_ERROR)
931 * do_failback: add error handle callback
933 static int do_failback (void *obj, Tcl_Interp *interp,
934 int argc, char **argv)
944 return ir_strdel (interp, &p->failback);
950 if (ir_strdup (interp, &p->failback, argv[2]) == TCL_ERROR)
961 * do_databaseNames: specify database names
963 static int do_databaseNames (void *obj, Tcl_Interp *interp,
964 int argc, char **argv)
971 for (i=0; i<p->num_databaseNames; i++)
972 free (p->databaseNames[i]);
973 free (p->databaseNames);
977 p->num_databaseNames = 0;
978 p->databaseNames = NULL;
983 for (i=0; i<p->num_databaseNames; i++)
984 Tcl_AppendElement (interp, p->databaseNames[i]);
987 if (p->databaseNames)
989 for (i=0; i<p->num_databaseNames; i++)
990 free (p->databaseNames[i]);
991 free (p->databaseNames);
993 p->num_databaseNames = argc - 2;
994 if (!(p->databaseNames = ir_malloc (interp,
995 sizeof(*p->databaseNames) * p->num_databaseNames)))
997 for (i=0; i<p->num_databaseNames; i++)
999 if (ir_strdup (interp, &p->databaseNames[i], argv[2+i])
1007 * do_replaceIndicator: Set/get replace Set indicator
1009 static int do_replaceIndicator (void *obj, Tcl_Interp *interp,
1010 int argc, char **argv)
1016 p->replaceIndicator = 1;
1019 return get_set_int (&p->replaceIndicator, interp, argc, argv);
1023 * do_queryType: Set/Get query method
1025 static int do_queryType (void *obj, Tcl_Interp *interp,
1026 int argc, char **argv)
1031 return ir_strdup (interp, &p->queryType, "rpn");
1032 else if (argc == -1)
1033 return ir_strdel (interp, &p->queryType);
1036 free (p->queryType);
1037 if (ir_strdup (interp, &p->queryType, argv[2]) == TCL_ERROR)
1040 Tcl_AppendResult (interp, p->queryType, NULL);
1045 * do_userInformationField: Get User information field
1047 static int do_userInformationField (void *obj, Tcl_Interp *interp,
1048 int argc, char **argv)
1054 p->userInformationField = NULL;
1057 else if (argc == -1)
1058 return ir_strdel (interp, &p->userInformationField);
1059 Tcl_AppendResult (interp, p->userInformationField, NULL);
1064 * do_smallSetUpperBound: Set/get small set upper bound
1066 static int do_smallSetUpperBound (void *o, Tcl_Interp *interp,
1067 int argc, char **argv)
1073 p->smallSetUpperBound = 0;
1076 return get_set_int (&p->smallSetUpperBound, interp, argc, argv);
1080 * do_largeSetLowerBound: Set/get large set lower bound
1082 static int do_largeSetLowerBound (void *o, Tcl_Interp *interp,
1083 int argc, char **argv)
1089 p->largeSetLowerBound = 2;
1092 return get_set_int (&p->largeSetLowerBound, interp, argc, argv);
1096 * do_mediumSetPresentNumber: Set/get large set lower bound
1098 static int do_mediumSetPresentNumber (void *o, Tcl_Interp *interp,
1099 int argc, char **argv)
1105 p->mediumSetPresentNumber = 0;
1108 return get_set_int (&p->mediumSetPresentNumber, interp, argc, argv);
1112 * do_referenceId: Set/Get referenceId
1114 static int do_referenceId (void *obj, Tcl_Interp *interp,
1115 int argc, char **argv)
1120 p->referenceId = NULL;
1121 else if (argc == -1)
1122 return ir_strdel (interp, &p->referenceId);
1125 free (p->referenceId);
1126 if (ir_strdup (interp, &p->referenceId, argv[2]) == TCL_ERROR)
1129 Tcl_AppendResult (interp, p->referenceId, NULL);
1133 static IRMethod ir_method_tab[] = {
1134 { 1, "comstack", do_comstack },
1135 { 1, "protocol", do_protocol },
1136 { 0, "failback", do_failback },
1138 { 1, "connect", do_connect },
1139 { 0, "protocolVersion", do_protocolVersion },
1140 { 1, "preferredMessageSize", do_preferredMessageSize },
1141 { 1, "maximumRecordSize", do_maximumRecordSize },
1142 { 1, "implementationName", do_implementationName },
1143 { 1, "implementationId", do_implementationId },
1144 { 0, "targetImplementationName", do_targetImplementationName },
1145 { 0, "targetImplementationId", do_targetImplementationId },
1146 { 0, "targetImplementationVersion", do_targetImplementationVersion },
1147 { 0, "userInformationField", do_userInformationField },
1148 { 1, "idAuthentication", do_idAuthentication },
1149 { 0, "options", do_options },
1150 { 0, "init", do_init_request },
1151 { 0, "initResult", do_initResult },
1152 { 0, "disconnect", do_disconnect },
1153 { 0, "callback", do_callback },
1157 static IRMethod ir_set_c_method_tab[] = {
1158 { 0, "databaseNames", do_databaseNames},
1159 { 0, "replaceIndicator", do_replaceIndicator},
1160 { 0, "queryType", do_queryType },
1161 { 0, "smallSetUpperBound", do_smallSetUpperBound},
1162 { 0, "largeSetLowerBound", do_largeSetLowerBound},
1163 { 0, "mediumSetPresentNumber", do_mediumSetPresentNumber},
1164 { 0, "referenceId", do_referenceId },
1169 * ir_obj_method: IR Object methods
1171 static int ir_obj_method (ClientData clientData, Tcl_Interp *interp,
1172 int argc, char **argv)
1175 IRObj *p = clientData;
1178 return ir_method_r (clientData, interp, argc, argv, ir_method_tab);
1180 tab[0].tab = ir_method_tab;
1182 tab[1].tab = ir_set_c_method_tab;
1183 tab[1].obj = &p->set_inher;
1186 return ir_method (interp, argc, argv, tab);
1190 * ir_obj_delete: IR Object disposal
1192 static void ir_obj_delete (ClientData clientData)
1194 IRObj *obj = clientData;
1198 if (obj->ref_count > 0)
1200 assert (obj->ref_count == 0);
1202 tab[0].tab = ir_method_tab;
1204 tab[1].tab = ir_set_c_method_tab;
1205 tab[1].obj = &obj->set_inher;
1208 ir_method (NULL, -1, NULL, tab);
1209 odr_destroy (obj->odr_in);
1210 odr_destroy (obj->odr_out);
1211 odr_destroy (obj->odr_pr);
1212 free (obj->buf_out);
1218 * ir_obj_mk: IR Object creation
1220 static int ir_obj_mk (ClientData clientData, Tcl_Interp *interp,
1221 int argc, char **argv)
1231 interp->result = "wrong # args";
1234 if (!(obj = ir_malloc (interp, sizeof(*obj))))
1239 obj->bibset = ccl_qual_mk ();
1240 if ((inf = fopen ("default.bib", "r")))
1242 ccl_qual_file (obj->bibset, inf);
1247 obj->odr_in = odr_createmem (ODR_DECODE);
1248 obj->odr_out = odr_createmem (ODR_ENCODE);
1249 obj->odr_pr = odr_createmem (ODR_PRINT);
1251 obj->len_out = 10000;
1252 if (!(obj->buf_out = ir_malloc (interp, obj->len_out)))
1254 odr_setbuf (obj->odr_out, obj->buf_out, obj->len_out, 0);
1259 tab[0].tab = ir_method_tab;
1261 tab[1].tab = ir_set_c_method_tab;
1262 tab[1].obj = &obj->set_inher;
1265 if (ir_method (interp, 0, NULL, tab) == TCL_ERROR)
1267 Tcl_CreateCommand (interp, argv[1], ir_obj_method,
1268 (ClientData) obj, ir_obj_delete);
1272 /* ------------------------------------------------------- */
1274 * do_search: Do search request
1276 static int do_search (void *o, Tcl_Interp *interp,
1277 int argc, char **argv)
1279 Z_SearchRequest req;
1281 Z_APDU apdu, *apdup = &apdu;
1284 IRObj *p = obj->parent;
1294 interp->result = "wrong # args";
1297 if (!obj->set_inher.num_databaseNames)
1299 interp->result = "no databaseNames";
1304 interp->result = "not connected";
1307 odr_reset (p->odr_out);
1308 apdu.which = Z_APDU_searchRequest;
1309 apdu.u.searchRequest = &req;
1311 bib1.proto = PROTO_Z3950;
1312 bib1.class = CLASS_ATTSET;
1313 bib1.value = VAL_BIB1;
1315 set_referenceId (p->odr_out, &req.referenceId, obj->set_inher.referenceId);
1317 req.smallSetUpperBound = &obj->set_inher.smallSetUpperBound;
1318 req.largeSetLowerBound = &obj->set_inher.largeSetLowerBound;
1319 req.mediumSetPresentNumber = &obj->set_inher.mediumSetPresentNumber;
1320 req.replaceIndicator = &obj->set_inher.replaceIndicator;
1321 req.resultSetName = obj->setName ? obj->setName : "Default";
1322 logf (LOG_DEBUG, "Search, resultSetName %s", req.resultSetName);
1323 req.num_databaseNames = obj->set_inher.num_databaseNames;
1324 req.databaseNames = obj->set_inher.databaseNames;
1325 for (r=0; r < obj->set_inher.num_databaseNames; r++)
1326 logf (LOG_DEBUG, " Database %s", obj->set_inher.databaseNames[r]);
1327 req.smallSetElementSetNames = 0;
1328 req.mediumSetElementSetNames = 0;
1329 req.preferredRecordSyntax = 0;
1332 if (!strcmp (obj->set_inher.queryType, "rpn"))
1334 Z_RPNQuery *RPNquery;
1336 RPNquery = p_query_rpn (p->odr_out, argv[2]);
1339 Tcl_AppendResult (interp, "Syntax error in query", NULL);
1342 RPNquery->attributeSetId = oid_getoidbyent (&bib1);
1343 query.which = Z_Query_type_1;
1344 query.u.type_1 = RPNquery;
1345 logf (LOG_DEBUG, "RPN");
1348 else if (!strcmp (obj->set_inher.queryType, "cclrpn"))
1352 struct ccl_rpn_node *rpn;
1353 Z_RPNQuery *RPNquery;
1355 rpn = ccl_find_str(p->bibset, argv[2], &error, &pos);
1358 Tcl_AppendResult (interp, "CCL error: ", ccl_err_msg(error),NULL);
1361 ccl_pr_tree (rpn, stderr);
1362 fprintf (stderr, "\n");
1363 assert((RPNquery = ccl_rpn_query(rpn)));
1364 RPNquery->attributeSetId = oid_getoidbyent (&bib1);
1365 query.which = Z_Query_type_1;
1366 query.u.type_1 = RPNquery;
1367 logf (LOG_DEBUG, "CCLRPN");
1370 else if (!strcmp (obj->set_inher.queryType, "ccl"))
1372 query.which = Z_Query_type_2;
1373 query.u.type_2 = &ccl_query;
1374 ccl_query.buf = (unsigned char *) argv[2];
1375 ccl_query.len = strlen (argv[2]);
1376 logf (LOG_DEBUG, "CCL");
1380 interp->result = "unknown query method";
1383 if (!z_APDU (p->odr_out, &apdup, 0))
1385 interp->result = odr_errlist [odr_geterror (p->odr_out)];
1386 odr_reset (p->odr_out);
1389 p->sbuf = odr_getbuf (p->odr_out, &p->slen, NULL);
1390 if ((r=cs_put (p->cs_link, p->sbuf, p->slen)) < 0)
1392 interp->result = "cs_put failed in search";
1397 ir_select_add_write (cs_fileno(p->cs_link), p);
1398 logf (LOG_DEBUG, "Sent part of searchRequest (%d bytes)", p->slen);
1402 logf (LOG_DEBUG, "Whole search request (%d bytes)", p->slen);
1408 * do_resultCount: Get number of hits
1410 static int do_resultCount (void *o, Tcl_Interp *interp,
1411 int argc, char **argv)
1417 return get_set_int (&obj->resultCount, interp, argc, argv);
1421 * do_searchStatus: Get search status (after search response)
1423 static int do_searchStatus (void *o, Tcl_Interp *interp,
1424 int argc, char **argv)
1430 return get_set_int (&obj->searchStatus, interp, argc, argv);
1434 * do_presentStatus: Get search status (after search/present response)
1436 static int do_presentStatus (void *o, Tcl_Interp *interp,
1437 int argc, char **argv)
1443 return get_set_int (&obj->presentStatus, interp, argc, argv);
1447 * do_nextResultSetPosition: Get next result set position
1448 * (after search/present response)
1450 static int do_nextResultSetPosition (void *o, Tcl_Interp *interp,
1451 int argc, char **argv)
1457 return get_set_int (&obj->nextResultSetPosition, interp, argc, argv);
1461 * do_setName: Set result Set name
1463 static int do_setName (void *o, Tcl_Interp *interp,
1464 int argc, char **argv)
1469 return ir_strdup (interp, &obj->setName, "Default");
1470 else if (argc == -1)
1471 return ir_strdel (interp, &obj->setName);
1474 free (obj->setName);
1475 if (ir_strdup (interp, &obj->setName, argv[2])
1479 Tcl_AppendElement (interp, obj->setName);
1484 * do_numberOfRecordsReturned: Get number of records returned
1486 static int do_numberOfRecordsReturned (void *o, Tcl_Interp *interp,
1487 int argc, char **argv)
1493 return get_set_int (&obj->numberOfRecordsReturned, interp, argc, argv);
1497 * do_recordType: Return record type (if any) at position.
1499 static int do_recordType (void *o, Tcl_Interp *interp, int argc, char **argv)
1507 obj->record_list = NULL;
1510 else if (argc == -1)
1512 delete_IR_records (obj);
1517 sprintf (interp->result, "wrong # args");
1520 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
1522 rl = find_IR_record (obj, offset);
1527 case Z_NamePlusRecord_databaseRecord:
1528 interp->result = "databaseRecord";
1530 case Z_NamePlusRecord_surrogateDiagnostic:
1531 interp->result = "surrogateDiagnostic";
1538 * do_recordDiag: Return diagnostic record info
1540 static int do_recordDiag (void *o, Tcl_Interp *interp, int argc, char **argv)
1551 sprintf (interp->result, "wrong # args");
1554 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
1556 rl = find_IR_record (obj, offset);
1559 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
1562 if (rl->which != Z_NamePlusRecord_surrogateDiagnostic)
1564 Tcl_AppendResult (interp, "No Diagnostic record at #", argv[2], NULL);
1567 sprintf (buf, "%d", rl->u.diag.condition);
1568 Tcl_AppendResult (interp, buf, " {",
1569 (rl->u.diag.addinfo ? rl->u.diag.addinfo : ""),
1575 * do_getMarc: Get ISO2709 Record lines/fields
1577 static int do_getMarc (void *o, Tcl_Interp *interp, int argc, char **argv)
1587 sprintf (interp->result, "wrong # args");
1590 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
1592 rl = find_IR_record (obj, offset);
1595 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
1598 if (rl->which != Z_NamePlusRecord_databaseRecord)
1600 Tcl_AppendResult (interp, "No MARC record at #", argv[2], NULL);
1603 return ir_tcl_get_marc (interp, rl->u.dbrec.buf, argc, argv);
1608 * do_responseStatus: Return response status (present or search)
1610 static int do_responseStatus (void *o, Tcl_Interp *interp,
1611 int argc, char **argv)
1617 obj->recordFlag = 0;
1618 obj->addinfo = NULL;
1621 else if (argc == -1)
1622 return ir_strdel (interp, &obj->addinfo);
1623 if (!obj->recordFlag)
1625 Tcl_AppendElement (interp, "OK");
1630 case Z_Records_DBOSD:
1631 Tcl_AppendElement (interp, "DBOSD");
1634 return mk_nonSurrogateDiagnostics (interp, obj->condition,
1641 * do_present: Perform Present Request
1644 static int do_present (void *o, Tcl_Interp *interp,
1645 int argc, char **argv)
1648 IRObj *p = obj->parent;
1649 Z_APDU apdu, *apdup = &apdu;
1650 Z_PresentRequest req;
1659 if (Tcl_GetInt (interp, argv[2], &start) == TCL_ERROR)
1666 if (Tcl_GetInt (interp, argv[3], &number) == TCL_ERROR)
1673 interp->result = "not connected";
1676 odr_reset (p->odr_out);
1678 obj->number = number;
1680 apdu.which = Z_APDU_presentRequest;
1681 apdu.u.presentRequest = &req;
1683 set_referenceId (p->odr_out, &req.referenceId, obj->set_inher.referenceId);
1685 req.resultSetId = obj->setName ? obj->setName : "Default";
1687 req.resultSetStartPoint = &start;
1688 req.numberOfRecordsRequested = &number;
1689 req.elementSetNames = 0;
1690 req.preferredRecordSyntax = 0;
1692 if (!z_APDU (p->odr_out, &apdup, 0))
1694 interp->result = odr_errlist [odr_geterror (p->odr_out)];
1695 odr_reset (p->odr_out);
1698 p->sbuf = odr_getbuf (p->odr_out, &p->slen, NULL);
1699 if ((r=cs_put (p->cs_link, p->sbuf, p->slen)) < 0)
1701 interp->result = "cs_put failed in present";
1706 ir_select_add_write (cs_fileno(p->cs_link), p);
1707 logf (LOG_DEBUG, "Part of present request, start=%d, num=%d"
1708 " (%d bytes)", start, number, p->slen);
1712 logf (LOG_DEBUG, "Whole present request, start=%d, num=%d"
1713 " (%d bytes)", start, number, p->slen);
1719 * do_loadFile: Load result set from file
1722 static int do_loadFile (void *o, Tcl_Interp *interp,
1723 int argc, char **argv)
1725 IRSetObj *setobj = o;
1735 interp->result = "wrong # args";
1738 inf = fopen (argv[2], "r");
1741 Tcl_AppendResult (interp, "Cannot open ", argv[2], NULL);
1744 while ((buf = ir_tcl_fread_marc (inf, &size)))
1748 rl = new_IR_record (setobj, no, Z_NamePlusRecord_databaseRecord);
1749 rl->u.dbrec.buf = buf;
1750 rl->u.dbrec.size = size;
1753 setobj->numberOfRecordsReturned = no-1;
1758 static IRMethod ir_set_method_tab[] = {
1759 { 0, "search", do_search },
1760 { 0, "searchStatus", do_searchStatus },
1761 { 0, "presentStatus", do_presentStatus },
1762 { 0, "nextResultSetPosition", do_nextResultSetPosition },
1763 { 0, "setName", do_setName },
1764 { 0, "resultCount", do_resultCount },
1765 { 0, "numberOfRecordsReturned", do_numberOfRecordsReturned },
1766 { 0, "present", do_present },
1767 { 0, "recordType", do_recordType },
1768 { 0, "getMarc", do_getMarc },
1769 { 0, "Diag", do_recordDiag },
1770 { 0, "responseStatus", do_responseStatus },
1771 { 0, "loadFile", do_loadFile },
1776 * ir_set_obj_method: IR Set Object methods
1778 static int ir_set_obj_method (ClientData clientData, Tcl_Interp *interp,
1779 int argc, char **argv)
1782 IRSetObj *p = clientData;
1786 interp->result = "wrong # args";
1789 tabs[0].tab = ir_set_method_tab;
1791 tabs[1].tab = ir_set_c_method_tab;
1792 tabs[1].obj = &p->set_inher;
1795 return ir_method (interp, argc, argv, tabs);
1799 * ir_set_obj_delete: IR Set Object disposal
1801 static void ir_set_obj_delete (ClientData clientData)
1804 IRSetObj *p = clientData;
1806 tabs[0].tab = ir_set_method_tab;
1808 tabs[1].tab = ir_set_c_method_tab;
1809 tabs[1].obj = &p->set_inher;
1812 ir_method (NULL, -1, NULL, tabs);
1818 * ir_set_obj_mk: IR Set Object creation
1820 static int ir_set_obj_mk (ClientData clientData, Tcl_Interp *interp,
1821 int argc, char **argv)
1826 if (argc < 2 || argc > 3)
1828 interp->result = "wrong # args";
1831 if (!(obj = ir_malloc (interp, sizeof(*obj))))
1835 Tcl_CmdInfo parent_info;
1840 if (!Tcl_GetCommandInfo (interp, argv[2], &parent_info))
1842 interp->result = "No parent";
1845 obj->parent = (IRObj *) parent_info.clientData;
1847 dst = &obj->set_inher;
1848 src = &obj->parent->set_inher;
1850 dst->num_databaseNames = src->num_databaseNames;
1851 if (!(dst->databaseNames =
1852 ir_malloc (interp, sizeof (*dst->databaseNames)
1853 * dst->num_databaseNames)))
1855 for (i = 0; i < dst->num_databaseNames; i++)
1857 if (ir_strdup (interp, &dst->databaseNames[i],
1858 src->databaseNames[i]) == TCL_ERROR)
1861 if (ir_strdup (interp, &dst->queryType, src->queryType)
1865 if (ir_strdup (interp, &dst->referenceId, src->referenceId)
1869 dst->smallSetUpperBound = src->smallSetUpperBound;
1870 dst->largeSetLowerBound = src->largeSetLowerBound;
1871 dst->mediumSetPresentNumber = src->mediumSetPresentNumber;
1876 tabs[0].tab = ir_set_method_tab;
1880 if (ir_method (interp, 0, NULL, tabs) == TCL_ERROR)
1883 Tcl_CreateCommand (interp, argv[1], ir_set_obj_method,
1884 (ClientData) obj, ir_set_obj_delete);
1888 /* ------------------------------------------------------- */
1891 * do_scan: Perform scan
1893 static int do_scan (void *o, Tcl_Interp *interp, int argc, char **argv)
1896 Z_APDU apdu, *apdup = &apdu;
1898 IRObj *p = obj->parent;
1902 struct ccl_rpn_node *rpn;
1911 interp->result = "wrong # args";
1914 if (!p->set_inher.num_databaseNames)
1916 interp->result = "no databaseNames";
1921 interp->result = "not connected";
1924 odr_reset (p->odr_out);
1926 bib1.proto = PROTO_Z3950;
1927 bib1.class = CLASS_ATTSET;
1928 bib1.value = VAL_BIB1;
1930 apdu.which = Z_APDU_scanRequest;
1931 apdu.u.scanRequest = &req;
1932 set_referenceId (p->odr_out, &req.referenceId, p->set_inher.referenceId);
1933 req.num_databaseNames = p->set_inher.num_databaseNames;
1934 req.databaseNames = p->set_inher.databaseNames;
1935 req.attributeSet = oid_getoidbyent (&bib1);
1938 if (!(req.termListAndStartPoint = p_query_scan (p->odr_out, argv[2])))
1940 Tcl_AppendResult (interp, "Syntax error in query", NULL);
1944 rpn = ccl_find_str(p->bibset, argv[2], &r, &pos);
1947 Tcl_AppendResult (interp, "CCL error: ", ccl_err_msg (r), NULL);
1950 ccl_pr_tree (rpn, stderr);
1951 fprintf (stderr, "\n");
1952 if (!(req.termListAndStartPoint = ccl_scan_query (rpn)))
1955 req.stepSize = &obj->stepSize;
1956 req.numberOfTermsRequested = &obj->numberOfTermsRequested;
1957 req.preferredPositionInResponse = &obj->preferredPositionInResponse;
1958 logf (LOG_DEBUG, "stepSize=%d", *req.stepSize);
1959 logf (LOG_DEBUG, "numberOfTermsRequested=%d",
1960 *req.numberOfTermsRequested);
1961 logf (LOG_DEBUG, "preferredPositionInResponse=%d",
1962 *req.preferredPositionInResponse);
1964 if (!z_APDU (p->odr_out, &apdup, 0))
1966 interp->result = odr_errlist [odr_geterror (p->odr_out)];
1967 odr_reset (p->odr_out);
1970 p->sbuf = odr_getbuf (p->odr_out, &p->slen, NULL);
1971 if ((r=cs_put (p->cs_link, p->sbuf, p->slen)) < 0)
1973 interp->result = "cs_put failed in scan";
1978 ir_select_add_write (cs_fileno(p->cs_link), p);
1979 logf (LOG_DEBUG, "Sent part of scanRequest (%d bytes)", p->slen);
1983 logf (LOG_DEBUG, "Whole scan request (%d bytes)", p->slen);
1989 * do_stepSize: Set/get replace Step Size
1991 static int do_stepSize (void *obj, Tcl_Interp *interp,
1992 int argc, char **argv)
2000 return get_set_int (&p->stepSize, interp, argc, argv);
2004 * do_numberOfTermsRequested: Set/get Number of Terms requested
2006 static int do_numberOfTermsRequested (void *obj, Tcl_Interp *interp,
2007 int argc, char **argv)
2013 p->numberOfTermsRequested = 20;
2016 return get_set_int (&p->numberOfTermsRequested, interp, argc, argv);
2021 * do_preferredPositionInResponse: Set/get preferred Position
2023 static int do_preferredPositionInResponse (void *obj, Tcl_Interp *interp,
2024 int argc, char **argv)
2030 p->preferredPositionInResponse = 1;
2033 return get_set_int (&p->preferredPositionInResponse, interp, argc, argv);
2037 * do_scanStatus: Get scan status
2039 static int do_scanStatus (void *obj, Tcl_Interp *interp,
2040 int argc, char **argv)
2046 return get_set_int (&p->scanStatus, interp, argc, argv);
2050 * do_numberOfEntriesReturned: Get number of Entries returned
2052 static int do_numberOfEntriesReturned (void *obj, Tcl_Interp *interp,
2053 int argc, char **argv)
2059 return get_set_int (&p->numberOfEntriesReturned, interp, argc, argv);
2063 * do_positionOfTerm: Get position of Term
2065 static int do_positionOfTerm (void *obj, Tcl_Interp *interp,
2066 int argc, char **argv)
2072 return get_set_int (&p->positionOfTerm, interp, argc, argv);
2076 * do_scanLine: get Scan Line (surrogate or normal) after response
2078 static int do_scanLine (void *obj, Tcl_Interp *interp, int argc, char **argv)
2086 p->entries_flag = 0;
2088 p->nonSurrogateDiagnostics = NULL;
2091 else if (argc == -1)
2093 p->entries_flag = 0;
2094 /* release entries */
2096 /* release non diagnostics */
2097 p->nonSurrogateDiagnostics = NULL;
2102 interp->result = "wrong # args";
2105 if (Tcl_GetInt (interp, argv[2], &i) == TCL_ERROR)
2107 if (!p->entries_flag || p->which != Z_ListEntries_entries || !p->entries
2108 || i >= p->num_entries || i < 0)
2110 switch (p->entries[i].which)
2112 case Z_Entry_termInfo:
2113 Tcl_AppendElement (interp, "T");
2114 if (p->entries[i].u.term.buf)
2115 Tcl_AppendElement (interp, p->entries[i].u.term.buf);
2117 Tcl_AppendElement (interp, "");
2118 sprintf (numstr, "%d", p->entries[i].u.term.globalOccurrences);
2119 Tcl_AppendElement (interp, numstr);
2121 case Z_Entry_surrogateDiagnostic:
2123 mk_nonSurrogateDiagnostics (interp, p->entries[i].u.diag.condition,
2124 p->entries[i].u.diag.addinfo);
2130 static IRMethod ir_scan_method_tab[] = {
2131 { 0, "scan", do_scan },
2132 { 0, "stepSize", do_stepSize },
2133 { 0, "numberOfTermsRequested", do_numberOfTermsRequested },
2134 { 0, "preferredPositionInResponse", do_preferredPositionInResponse },
2135 { 0, "scanStatus", do_scanStatus },
2136 { 0, "numberOfEntriesReturned", do_numberOfEntriesReturned },
2137 { 0, "positionOfTerm", do_positionOfTerm },
2138 { 0, "scanLine", do_scanLine },
2143 * ir_scan_obj_method: IR Scan Object methods
2145 static int ir_scan_obj_method (ClientData clientData, Tcl_Interp *interp,
2146 int argc, char **argv)
2152 interp->result = "wrong # args";
2155 tabs[0].tab = ir_scan_method_tab;
2156 tabs[0].obj = clientData;
2159 return ir_method (interp, argc, argv, tabs);
2163 * ir_scan_obj_delete: IR Scan Object disposal
2165 static void ir_scan_obj_delete (ClientData clientData)
2168 IRScanObj *obj = clientData;
2170 tabs[0].tab = ir_scan_method_tab;
2174 ir_method (NULL, -1, NULL, tabs);
2179 * ir_scan_obj_mk: IR Scan Object creation
2181 static int ir_scan_obj_mk (ClientData clientData, Tcl_Interp *interp,
2182 int argc, char **argv)
2184 Tcl_CmdInfo parent_info;
2190 interp->result = "wrong # args";
2193 if (!Tcl_GetCommandInfo (interp, argv[2], &parent_info))
2195 interp->result = "No parent";
2198 if (!(obj = ir_malloc (interp, sizeof(*obj))))
2201 obj->parent = (IRObj *) parent_info.clientData;
2203 tabs[0].tab = ir_scan_method_tab;
2207 if (ir_method (interp, 0, NULL, tabs) == TCL_ERROR)
2209 Tcl_CreateCommand (interp, argv[1], ir_scan_obj_method,
2210 (ClientData) obj, ir_scan_obj_delete);
2214 /* ------------------------------------------------------- */
2216 static void ir_initResponse (void *obj, Z_InitResponse *initrs)
2220 p->initResult = *initrs->result ? 1 : 0;
2221 if (!*initrs->result)
2222 logf (LOG_DEBUG, "Connection rejected by target");
2224 logf (LOG_DEBUG, "Connection accepted by target");
2226 free (p->targetImplementationId);
2227 ir_strdup (p->interp, &p->targetImplementationId,
2228 initrs->implementationId);
2229 free (p->targetImplementationName);
2230 ir_strdup (p->interp, &p->targetImplementationName,
2231 initrs->implementationName);
2232 free (p->targetImplementationVersion);
2233 ir_strdup (p->interp, &p->targetImplementationVersion,
2234 initrs->implementationVersion);
2236 p->maximumRecordSize = *initrs->maximumRecordSize;
2237 p->preferredMessageSize = *initrs->preferredMessageSize;
2239 memcpy (&p->options, initrs->options, sizeof(initrs->options));
2240 memcpy (&p->protocolVersion, initrs->protocolVersion,
2241 sizeof(initrs->protocolVersion));
2242 free (p->userInformationField);
2243 p->userInformationField = NULL;
2244 if (initrs->userInformationField)
2248 if (initrs->userInformationField->which == ODR_EXTERNAL_octet &&
2249 (p->userInformationField =
2251 initrs->userInformationField->u.octet_aligned->len)
2254 memcpy (p->userInformationField,
2255 initrs->userInformationField->u.octet_aligned->buf,
2257 (p->userInformationField)[len] = '\0';
2262 static void ir_handleRecords (void *o, Z_Records *zrs)
2265 IRSetObj *setobj = p->set_child;
2267 setobj->which = zrs->which;
2268 setobj->recordFlag = 1;
2269 if (zrs->which == Z_Records_NSD)
2271 const char *addinfo;
2273 setobj->numberOfRecordsReturned = 0;
2274 setobj->condition = *zrs->u.nonSurrogateDiagnostic->condition;
2275 free (setobj->addinfo);
2276 setobj->addinfo = NULL;
2277 addinfo = zrs->u.nonSurrogateDiagnostic->addinfo;
2278 if (addinfo && (setobj->addinfo = malloc (strlen(addinfo) + 1)))
2279 strcpy (setobj->addinfo, addinfo);
2280 logf (LOG_DEBUG, "Diagnostic response. %s (%d): %s",
2281 diagbib1_str (setobj->condition),
2283 setobj->addinfo ? setobj->addinfo : "");
2290 setobj->numberOfRecordsReturned =
2291 zrs->u.databaseOrSurDiagnostics->num_records;
2292 logf (LOG_DEBUG, "Got %d records", setobj->numberOfRecordsReturned);
2293 for (offset = 0; offset<setobj->numberOfRecordsReturned; offset++)
2295 rl = new_IR_record (setobj, setobj->start + offset,
2296 zrs->u.databaseOrSurDiagnostics->
2297 records[offset]->which);
2298 if (rl->which == Z_NamePlusRecord_surrogateDiagnostic)
2302 diagrec = zrs->u.databaseOrSurDiagnostics->
2303 records[offset]->u.surrogateDiagnostic;
2305 rl->u.diag.condition = *diagrec->condition;
2306 if (diagrec->addinfo && (rl->u.diag.addinfo =
2307 malloc (strlen (diagrec->addinfo)+1)))
2308 strcpy (rl->u.diag.addinfo, diagrec->addinfo);
2312 Z_DatabaseRecord *zr;
2315 zr = zrs->u.databaseOrSurDiagnostics->records[offset]
2317 oe = (Odr_external*) zr;
2318 rl->u.dbrec.size = zr->u.octet_aligned->len;
2319 if (oe->which == ODR_EXTERNAL_octet && rl->u.dbrec.size > 0)
2321 const char *buf = (char*) zr->u.octet_aligned->buf;
2322 if ((rl->u.dbrec.buf = malloc (rl->u.dbrec.size)))
2323 memcpy (rl->u.dbrec.buf, buf, rl->u.dbrec.size);
2326 rl->u.dbrec.buf = NULL;
2332 static void ir_searchResponse (void *o, Z_SearchResponse *searchrs)
2335 IRSetObj *setobj = p->set_child;
2336 Z_Records *zrs = searchrs->records;
2340 setobj->searchStatus = searchrs->searchStatus ? 1 : 0;
2341 setobj->resultCount = *searchrs->resultCount;
2342 if (searchrs->presentStatus)
2343 setobj->presentStatus = *searchrs->presentStatus;
2344 if (searchrs->nextResultSetPosition)
2345 setobj->nextResultSetPosition = *searchrs->nextResultSetPosition;
2347 logf (LOG_DEBUG, "Search response %d, %d hits",
2348 setobj->searchStatus, setobj->resultCount);
2350 ir_handleRecords (o, zrs);
2352 setobj->recordFlag = 0;
2355 logf (LOG_DEBUG, "Search response, no object!");
2359 static void ir_presentResponse (void *o, Z_PresentResponse *presrs)
2362 IRSetObj *setobj = p->set_child;
2363 Z_Records *zrs = presrs->records;
2365 logf (LOG_DEBUG, "Received presentResponse");
2366 setobj->presentStatus = *presrs->presentStatus;
2367 setobj->nextResultSetPosition = *presrs->nextResultSetPosition;
2369 ir_handleRecords (o, zrs);
2372 setobj->recordFlag = 0;
2373 logf (LOG_DEBUG, "No records!");
2377 static void ir_scanResponse (void *o, Z_ScanResponse *scanrs)
2380 IRScanObj *scanobj = p->scan_child;
2382 logf (LOG_DEBUG, "Received scanResponse");
2384 scanobj->scanStatus = *scanrs->scanStatus;
2385 logf (LOG_DEBUG, "scanStatus=%d", scanobj->scanStatus);
2387 if (scanrs->stepSize)
2388 scanobj->stepSize = *scanrs->stepSize;
2389 logf (LOG_DEBUG, "stepSize=%d", scanobj->stepSize);
2391 scanobj->numberOfEntriesReturned = *scanrs->numberOfEntriesReturned;
2392 logf (LOG_DEBUG, "numberOfEntriesReturned=%d",
2393 scanobj->numberOfEntriesReturned);
2395 if (scanrs->positionOfTerm)
2396 scanobj->positionOfTerm = *scanrs->positionOfTerm;
2398 scanobj->positionOfTerm = -1;
2399 logf (LOG_DEBUG, "positionOfTerm=%d", scanobj->positionOfTerm);
2401 free (scanobj->entries);
2402 scanobj->entries = NULL;
2403 free (scanobj->nonSurrogateDiagnostics);
2404 scanobj->nonSurrogateDiagnostics = NULL;
2406 if (scanrs->entries)
2411 scanobj->entries_flag = 1;
2412 scanobj->which = scanrs->entries->which;
2413 switch (scanobj->which)
2415 case Z_ListEntries_entries:
2416 scanobj->num_entries = scanrs->entries->u.entries->num_entries;
2417 scanobj->entries = malloc (scanobj->num_entries *
2418 sizeof(*scanobj->entries));
2419 for (i=0; i<scanobj->num_entries; i++)
2421 ze = scanrs->entries->u.entries->entries[i];
2422 scanobj->entries[i].which = ze->which;
2425 case Z_Entry_termInfo:
2426 if (ze->u.termInfo->term->which == Z_Term_general)
2428 int l = ze->u.termInfo->term->u.general->len;
2429 scanobj->entries[i].u.term.buf = malloc (1+l);
2430 memcpy (scanobj->entries[i].u.term.buf,
2431 ze->u.termInfo->term->u.general->buf,
2433 scanobj->entries[i].u.term.buf[l] = '\0';
2436 scanobj->entries[i].u.term.buf = NULL;
2437 if (ze->u.termInfo->globalOccurrences)
2438 scanobj->entries[i].u.term.globalOccurrences =
2439 *ze->u.termInfo->globalOccurrences;
2441 scanobj->entries[i].u.term.globalOccurrences = 0;
2443 case Z_Entry_surrogateDiagnostic:
2444 scanobj->entries[i].u.diag.addinfo =
2445 malloc (1+strlen(ze->u.surrogateDiagnostic->
2447 strcpy (scanobj->entries[i].u.diag.addinfo,
2448 ze->u.surrogateDiagnostic->addinfo);
2449 scanobj->entries[i].u.diag.condition =
2450 *ze->u.surrogateDiagnostic->condition;
2455 case Z_ListEntries_nonSurrogateDiagnostics:
2456 scanobj->num_diagRecs = scanrs->entries->
2457 u.nonSurrogateDiagnostics->num_diagRecs;
2458 scanobj->nonSurrogateDiagnostics = malloc (scanobj->num_diagRecs *
2459 sizeof(*scanobj->nonSurrogateDiagnostics));
2464 scanobj->entries_flag = 0;
2468 * ir_select_read: handle incoming packages
2470 void ir_select_read (ClientData clientData)
2472 IRObj *p = clientData;
2478 r = cs_rcvconnect (p->cs_link);
2482 ir_select_remove_write (cs_fileno (p->cs_link), p);
2485 logf (LOG_DEBUG, "cs_rcvconnect error");
2487 Tcl_Eval (p->interp, p->failback);
2488 do_disconnect (p, NULL, 2, NULL);
2492 Tcl_Eval (p->interp, p->callback);
2497 /* signal one more use of ir object - callbacks must not
2498 release the ir memory (p pointer) */
2500 if ((r=cs_get (p->cs_link, &p->buf_in, &p->len_in)) <= 0)
2502 logf (LOG_DEBUG, "cs_get failed, code %d", r);
2503 ir_select_remove (cs_fileno (p->cs_link), p);
2505 Tcl_Eval (p->interp, p->failback);
2506 do_disconnect (p, NULL, 2, NULL);
2508 /* relase ir object now if callback deleted it */
2514 odr_setbuf (p->odr_in, p->buf_in, r, 0);
2515 logf (LOG_DEBUG, "cs_get ok, got %d", r);
2516 if (!z_APDU (p->odr_in, &apdu, 0))
2518 logf (LOG_DEBUG, "%s", odr_errlist [odr_geterror (p->odr_in)]);
2520 Tcl_Eval (p->interp, p->failback);
2521 do_disconnect (p, NULL, 2, NULL);
2523 /* relase ir object now if callback deleted it */
2529 case Z_APDU_initResponse:
2530 ir_initResponse (p, apdu->u.initResponse);
2532 case Z_APDU_searchResponse:
2533 ir_searchResponse (p, apdu->u.searchResponse);
2535 case Z_APDU_presentResponse:
2536 ir_presentResponse (p, apdu->u.presentResponse);
2538 case Z_APDU_scanResponse:
2539 ir_scanResponse (p, apdu->u.scanResponse);
2542 logf (LOG_WARN, "Received unknown APDU type (%d)", apdu->which);
2544 Tcl_Eval (p->interp, p->failback);
2545 do_disconnect (p, NULL, 2, NULL);
2547 odr_reset (p->odr_in);
2549 Tcl_Eval (p->interp, p->callback);
2550 if (p->ref_count == 1)
2556 } while (p->cs_link && cs_more (p->cs_link));
2560 * ir_select_write: handle outgoing packages - not yet written.
2562 void ir_select_write (ClientData clientData)
2564 IRObj *p = clientData;
2567 logf (LOG_DEBUG, "In write handler");
2570 r = cs_rcvconnect (p->cs_link);
2576 logf (LOG_DEBUG, "cs_rcvconnect error");
2577 ir_select_remove_write (cs_fileno (p->cs_link), p);
2579 Tcl_Eval (p->interp, p->failback);
2580 do_disconnect (p, NULL, 2, NULL);
2583 ir_select_remove_write (cs_fileno (p->cs_link), p);
2585 Tcl_Eval (p->interp, p->callback);
2588 if ((r=cs_put (p->cs_link, p->sbuf, p->slen)) < 0)
2590 logf (LOG_DEBUG, "select write fail");
2592 Tcl_Eval (p->interp, p->failback);
2593 do_disconnect (p, NULL, 2, NULL);
2595 else if (r == 0) /* remove select bit */
2597 ir_select_remove_write (cs_fileno (p->cs_link), p);
2601 /* ------------------------------------------------------- */
2604 * ir_tcl_init: Registration of TCL commands.
2606 int ir_tcl_init (Tcl_Interp *interp)
2608 Tcl_CreateCommand (interp, "ir", ir_obj_mk, (ClientData) NULL,
2609 (Tcl_CmdDeleteProc *) NULL);
2610 Tcl_CreateCommand (interp, "ir-set", ir_set_obj_mk,
2611 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
2612 Tcl_CreateCommand (interp, "ir-scan", ir_scan_obj_mk,
2613 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);