2 * IR toolkit for tcl/tk
4 * See the file LICENSE for details.
5 * Sebastian Hammer, Adam Dickmeiss
8 * Revision 1.47 1995-06-25 10:25:04 adam
9 * Working on triggerResourceControl. Description of compile/install
10 * procedure moved to ir-tcl.sgml.
12 * Revision 1.46 1995/06/22 13:15:06 adam
13 * Feature: SUTRS. Setting getSutrs implemented.
14 * Work on display formats.
15 * Preferred record syntax can be set by the user.
17 * Revision 1.45 1995/06/20 08:07:30 adam
18 * New setting: failInfo.
19 * Working on better cancel mechanism.
21 * Revision 1.44 1995/06/19 17:01:20 adam
24 * Revision 1.43 1995/06/19 13:06:08 adam
25 * New define: IR_TCL_VERSION.
27 * Revision 1.42 1995/06/19 08:08:52 adam
28 * client.tcl: hotTargets now contain both database and target name.
29 * ir-tcl.c: setting protocol edited. Errors in callbacks are logged
30 * by logf(LOG_WARN, ...) calls.
32 * Revision 1.41 1995/06/16 12:28:16 adam
33 * Implemented preferredRecordSyntax.
34 * Minor changes in diagnostic handling.
35 * Record list deleted when connection closes.
37 * Revision 1.40 1995/06/14 13:37:18 adam
38 * Setting recordType implemented.
39 * Setting implementationVersion implemented.
40 * Settings implementationId / implementationName edited.
42 * Revision 1.39 1995/06/08 10:26:32 adam
43 * Bug fix in ir_strdup.
45 * Revision 1.38 1995/06/01 16:36:47 adam
46 * About buttons. Minor bug fixes.
48 * Revision 1.37 1995/06/01 07:31:20 adam
49 * Rename of many typedefs -> IrTcl_...
51 * Revision 1.36 1995/05/31 13:09:59 adam
52 * Client searches/presents may be interrupted.
53 * New moving book-logo.
55 * Revision 1.35 1995/05/31 08:36:33 adam
56 * Bug fix in client.tcl: didn't save options on clientrc.tcl.
57 * New method: referenceId. More work on scan.
59 * Revision 1.34 1995/05/29 10:33:42 adam
60 * README and rename of startup script.
62 * Revision 1.33 1995/05/29 09:15:11 quinn
63 * Changed CS_SR to PROTO_SR, etc.
65 * Revision 1.32 1995/05/29 08:44:16 adam
66 * Work on delete of objects.
68 * Revision 1.31 1995/05/26 11:44:10 adam
69 * Bugs fixed. More work on MARC utilities and queries. Test
70 * client is up-to-date again.
72 * Revision 1.30 1995/05/26 08:54:11 adam
73 * New MARC utilities. Uses prefix query.
75 * Revision 1.29 1995/05/24 14:10:22 adam
76 * Work on idAuthentication, protocolVersion and options.
78 * Revision 1.28 1995/05/23 15:34:48 adam
79 * Many new settings, userInformationField, smallSetUpperBound, etc.
80 * A number of settings are inherited when ir-set is executed.
81 * This version is incompatible with the graphical test client (client.tcl).
83 * Revision 1.27 1995/05/11 15:34:47 adam
84 * Scan request changed a bit. This version works with RLG.
86 * Revision 1.26 1995/04/18 16:11:51 adam
87 * First version of graphical Scan. Some work on query-by-form.
89 * Revision 1.25 1995/04/17 09:37:17 adam
90 * Further development of scan.
92 * Revision 1.24 1995/04/11 14:16:42 adam
93 * Further work on scan. Response works. Entries aren't saved yet.
95 * Revision 1.23 1995/04/10 10:50:27 adam
96 * Result-set name defaults to suffix of ir-set name.
97 * Started working on scan. Not finished at this point.
99 * Revision 1.22 1995/03/31 10:43:03 adam
100 * More robust when getting bad MARC records.
102 * Revision 1.21 1995/03/31 08:56:37 adam
103 * New button "Search".
105 * Revision 1.20 1995/03/29 16:07:09 adam
106 * Bug fix: Didn't use setName in present request.
108 * Revision 1.19 1995/03/28 12:45:23 adam
109 * New ir method failback: called on disconnect/protocol error.
110 * New ir set/get method: protocol: SR / Z3950.
111 * Simple popup and disconnect when failback is invoked.
113 * Revision 1.18 1995/03/21 15:50:12 adam
116 * Revision 1.17 1995/03/21 13:41:03 adam
117 * Comstack cs_create not used too often. Non-blocking connect.
119 * Revision 1.16 1995/03/21 08:26:06 adam
120 * New method, setName, to specify the result set name (other than Default).
121 * New method, responseStatus, which returns diagnostic info, if any, after
122 * present response / search response.
124 * Revision 1.15 1995/03/20 15:24:07 adam
125 * Diagnostic records saved on searchResponse.
127 * Revision 1.14 1995/03/20 08:53:22 adam
128 * Event loop in tclmain.c rewritten. New method searchStatus.
130 * Revision 1.13 1995/03/17 18:26:17 adam
131 * Non-blocking i/o used now. Database names popup as cascade items.
133 * Revision 1.12 1995/03/17 15:45:00 adam
134 * Improved target/database setup.
136 * Revision 1.11 1995/03/16 17:54:03 adam
137 * Minor changes really.
139 * Revision 1.10 1995/03/15 16:14:50 adam
140 * Blocking arg in cs_create changed.
142 * Revision 1.9 1995/03/15 13:59:24 adam
145 * Revision 1.8 1995/03/15 08:25:16 adam
146 * New method presentStatus to check for error on present. Misc. cleanup
147 * of IrTcl_RecordList manipulations. Full MARC record presentation in
150 * Revision 1.7 1995/03/14 17:32:29 adam
151 * Presentation of full Marc record in popup window.
153 * Revision 1.6 1995/03/12 19:31:55 adam
154 * Pattern matching implemented when retrieving MARC records. More
155 * diagnostic functions.
157 * Revision 1.5 1995/03/10 18:00:15 adam
158 * Actual presentation in line-by-line format. RPN query support.
160 * Revision 1.4 1995/03/09 16:15:08 adam
161 * First presentRequest attempts. Hot-target list.
167 #include <sys/time.h>
177 int (*method) (void *obj, Tcl_Interp *interp, int argc, char **argv);
185 static Tcl_Interp *irTcl_interp;
187 static void ir_deleteDiags (IrTcl_Diagnostic **dst_list, int *dst_num);
188 static int do_disconnect (void *obj, Tcl_Interp *interp,
189 int argc, char **argv);
191 static IrTcl_RecordList *new_IR_record (IrTcl_SetObj *setobj,
194 IrTcl_RecordList *rl;
196 for (rl = setobj->record_list; rl; rl = rl->next)
202 case Z_NamePlusRecord_databaseRecord:
203 free (rl->u.dbrec.buf);
204 rl->u.dbrec.buf = NULL;
206 case Z_NamePlusRecord_surrogateDiagnostic:
207 ir_deleteDiags (&rl->u.surrogateDiagnostics.list,
208 &rl->u.surrogateDiagnostics.num);
216 rl = malloc (sizeof(*rl));
218 rl->next = setobj->record_list;
220 setobj->record_list = rl;
227 enum oid_value value;
229 } IrTcl_recordSyntaxTab[] = {
230 { VAL_UNIMARC, "UNIMARC" },
231 { VAL_INTERMARC, "INTERMARC" },
233 { VAL_USMARC, "USMARC" },
234 { VAL_UKMARC, "UKMARC" },
235 { VAL_NORMARC, "NORMARC" },
236 { VAL_LIBRISMARC, "LIBRISMARC" },
237 { VAL_DANMARC, "DANMARC" },
238 { VAL_FINMARC, "FINMARC" },
240 { VAL_CANMARC, "CANMARC" },
242 { VAL_PICAMARC, "PICAMARC" },
243 { VAL_AUSMARC, "AUSMARC" },
244 { VAL_IBERMARC, "IBERMARC" },
245 { VAL_SUTRS, "SUTRS" },
252 int IrTcl_eval (Tcl_Interp *interp, const char *command)
254 char *tmp = malloc (strlen(command)+1);
259 logf (LOG_FATAL, "Out of memory in IrTcl_eval");
262 strcpy (tmp, command);
263 r = Tcl_Eval (interp, tmp);
265 logf (LOG_WARN, "Tcl error in line %d: %s", interp->errorLine,
267 Tcl_FreeResult (interp);
273 * IrTcl_getRecordSyntaxStr: Return record syntax name of object id
275 static const char *IrTcl_getRecordSyntaxStr (enum oid_value value)
278 for (i = 0; IrTcl_recordSyntaxTab[i].name; i++)
279 if (IrTcl_recordSyntaxTab[i].value == value)
280 return IrTcl_recordSyntaxTab[i].name;
285 * IrTcl_getRecordSyntaxVal: Return record syntax value of string
287 static enum oid_value IrTcl_getRecordSyntaxVal (const char *name)
290 for (i = 0; IrTcl_recordSyntaxTab[i].name; i++)
291 if (!strcmp (IrTcl_recordSyntaxTab[i].name, name))
292 return IrTcl_recordSyntaxTab[i].value;
296 static IrTcl_RecordList *find_IR_record (IrTcl_SetObj *setobj, int no)
298 IrTcl_RecordList *rl;
300 for (rl = setobj->record_list; rl; rl = rl->next)
306 static void delete_IR_records (IrTcl_SetObj *setobj)
308 IrTcl_RecordList *rl, *rl1;
310 for (rl = setobj->record_list; rl; rl = rl1)
314 case Z_NamePlusRecord_databaseRecord:
315 free (rl->u.dbrec.buf);
317 case Z_NamePlusRecord_surrogateDiagnostic:
318 ir_deleteDiags (&rl->u.surrogateDiagnostics.list,
319 &rl->u.surrogateDiagnostics.num);
325 setobj->record_list = NULL;
329 * get_set_int: Set/get integer value
331 static int get_set_int (int *val, Tcl_Interp *interp, int argc, char **argv)
337 if (Tcl_GetInt (interp, argv[2], val)==TCL_ERROR)
340 sprintf (buf, "%d", *val);
341 Tcl_AppendResult (interp, buf, NULL);
346 * ir_method: Search for method in table and invoke method handler
348 int ir_method (Tcl_Interp *interp, int argc, char **argv, IrTcl_Methods *tab)
350 IrTcl_Methods *tab_i = tab;
353 for (tab_i = tab; tab_i->tab; tab_i++)
354 for (t = tab_i->tab; t->name; t++)
357 if ((*t->method)(tab_i->obj, interp, argc, argv) == TCL_ERROR)
361 if (!strcmp (t->name, argv[1]))
362 return (*t->method)(tab_i->obj, interp, argc, argv);
366 Tcl_AppendResult (interp, "Bad method. Possible methods:", NULL);
367 for (tab_i = tab; tab_i->tab; tab_i++)
368 for (t = tab_i->tab; t->name; t++)
369 Tcl_AppendResult (interp, " ", t->name, NULL);
374 * ir_method_r: Get status for all readable elements
376 int ir_method_r (void *obj, Tcl_Interp *interp, int argc, char **argv,
384 for (; tab->name; tab++)
387 argv_n[1] = tab->name;
388 Tcl_AppendResult (interp, "{", NULL);
389 (*tab->method)(obj, interp, argc_n, argv_n);
390 Tcl_AppendResult (interp, "} ", NULL);
396 * ir_named_bits: get/set named bits
398 int ir_named_bits (struct ir_named_entry *tab, Odr_bitmask *ob,
399 Tcl_Interp *interp, int argc, char **argv)
401 struct ir_named_entry *ti;
406 for (no = 0; no < argc; no++)
408 for (ti = tab; ti->name; ti++)
409 if (!strcmp (argv[no], ti->name))
411 ODR_MASK_SET (ob, ti->pos);
416 Tcl_AppendResult (interp, "Bad bit mask: ", argv[no], NULL);
422 for (ti = tab; ti->name; ti++)
423 if (ODR_MASK_GET (ob, ti->pos))
424 Tcl_AppendElement (interp, ti->name);
429 * ir_strdup: Duplicate string
431 int ir_strdup (Tcl_Interp *interp, char** p, const char *s)
438 *p = malloc (strlen(s)+1);
441 interp->result = "strdup fail";
449 * ir_strdel: Delete string
451 int ir_strdel (Tcl_Interp *interp, char **p)
459 * ir_malloc: Malloc function
461 void *ir_malloc (Tcl_Interp *interp, size_t size)
463 static char buf[128];
464 void *p = malloc (size);
468 sprintf (buf, "Malloc fail. %ld bytes requested", (long) size);
469 interp->result = buf;
475 static void set_referenceId (ODR o, Z_ReferenceId **dst, const char *src)
481 *dst = odr_malloc (o, sizeof(**dst));
482 (*dst)->size = (*dst)->len = strlen(src);
483 (*dst)->buf = odr_malloc (o, (*dst)->len);
484 memcpy ((*dst)->buf, src, (*dst)->len);
488 static void get_referenceId (char **dst, Z_ReferenceId *src)
496 *dst = malloc (src->len+1);
497 memcpy (*dst, src->buf, src->len);
498 (*dst)[src->len] = '\0';
501 /* ------------------------------------------------------- */
504 * do_init_request: init method on IR object
506 static int do_init_request (void *obj, Tcl_Interp *interp,
507 int argc, char **argv)
518 interp->result = "not connected";
521 odr_reset (p->odr_out);
522 apdu = zget_APDU (p->odr_out, Z_APDU_initRequest);
523 req = apdu->u.initRequest;
525 set_referenceId (p->odr_out, &req->referenceId, p->set_inher.referenceId);
526 req->options = &p->options;
527 req->protocolVersion = &p->protocolVersion;
528 req->preferredMessageSize = &p->preferredMessageSize;
529 req->maximumRecordSize = &p->maximumRecordSize;
531 if (p->idAuthenticationGroupId)
533 Z_IdPass *pass = odr_malloc (p->odr_out, sizeof(*pass));
534 Z_IdAuthentication *auth = odr_malloc (p->odr_out, sizeof(*auth));
536 auth->which = Z_IdAuthentication_idPass;
537 auth->u.idPass = pass;
538 if (p->idAuthenticationGroupId && *p->idAuthenticationGroupId)
539 pass->groupId = p->idAuthenticationGroupId;
541 pass->groupId = NULL;
542 if (p->idAuthenticationUserId && *p->idAuthenticationUserId)
543 pass->userId = p->idAuthenticationUserId;
546 if (p->idAuthenticationPassword && *p->idAuthenticationPassword)
547 pass->password = p->idAuthenticationPassword;
549 pass->password = NULL;
550 req->idAuthentication = auth;
552 else if (!p->idAuthenticationOpen || !*p->idAuthenticationOpen)
553 req->idAuthentication = NULL;
556 Z_IdAuthentication *auth = odr_malloc (p->odr_out, sizeof(*auth));
558 auth->which = Z_IdAuthentication_open;
559 auth->u.open = p->idAuthenticationOpen;
560 req->idAuthentication = auth;
562 req->implementationId = p->implementationId;
563 req->implementationName = p->implementationName;
564 req->implementationVersion = p->implementationVersion;
565 req->userInformationField = 0;
567 if (!z_APDU (p->odr_out, &apdu, 0))
569 Tcl_AppendResult (interp, odr_errlist [odr_geterror (p->odr_out)],
571 odr_reset (p->odr_out);
574 p->sbuf = odr_getbuf (p->odr_out, &p->slen, NULL);
575 if ((r=cs_put (p->cs_link, p->sbuf, p->slen)) < 0)
577 interp->result = "cs_put failed in init";
578 do_disconnect (p, NULL, 2, NULL);
583 ir_select_add_write (cs_fileno(p->cs_link), p);
584 logf (LOG_DEBUG, "Sent part of initializeRequest (%d bytes)", p->slen);
587 logf (LOG_DEBUG, "Sent whole initializeRequest (%d bytes)", p->slen);
592 * do_protocolVersion: Set protocol Version
594 static int do_protocolVersion (void *obj, Tcl_Interp *interp,
595 int argc, char **argv)
603 ODR_MASK_ZERO (&p->protocolVersion);
604 ODR_MASK_SET (&p->protocolVersion, 0);
605 ODR_MASK_SET (&p->protocolVersion, 1);
610 if (Tcl_GetInt (interp, argv[2], &version)==TCL_ERROR)
612 ODR_MASK_ZERO (&p->protocolVersion);
613 for (i = 0; i<version; i++)
614 ODR_MASK_SET (&p->protocolVersion, i);
616 for (i = 4; --i >= 0; )
617 if (ODR_MASK_GET (&p->protocolVersion, i))
619 sprintf (buf, "%d", i+1);
620 interp->result = buf;
625 * do_options: Set options
627 static int do_options (void *obj, Tcl_Interp *interp,
628 int argc, char **argv)
630 static struct ir_named_entry options_tab[] = {
634 { "resourceReport", 3 },
635 { "triggerResourceCtrl", 4},
636 { "resourceCtrl", 5},
640 { "extendedServices", 10},
641 { "level-1Segmentation", 11},
642 { "level-2Segmentation", 12},
643 { "concurrentOperations", 13},
644 { "namedResultSets", 14},
651 ODR_MASK_ZERO (&p->options);
652 ODR_MASK_SET (&p->options, 0);
653 ODR_MASK_SET (&p->options, 1);
654 ODR_MASK_SET (&p->options, 7);
655 ODR_MASK_SET (&p->options, 14);
658 return ir_named_bits (options_tab, &p->options, interp, argc-2, argv+2);
662 * do_failInfo: Get fail information
664 static int do_failInfo (void *obj, Tcl_Interp *interp, int argc, char **argv)
674 sprintf (buf, "%d", p->failInfo);
680 case IR_TCL_FAIL_CONNECT:
681 cp = "connect failed";
683 case IR_TCL_FAIL_READ:
684 cp = "connection closed";
686 case IR_TCL_FAIL_WRITE:
687 cp = "connection closed";
689 case IR_TCL_FAIL_IN_APDU:
690 cp = "failed to decode incoming APDU";
692 case IR_TCL_FAIL_UNKNOWN_APDU:
698 Tcl_AppendElement (interp, buf);
699 Tcl_AppendElement (interp, cp);
704 * do_preferredMessageSize: Set/get preferred message size
706 static int do_preferredMessageSize (void *obj, Tcl_Interp *interp,
707 int argc, char **argv)
713 p->preferredMessageSize = 30000;
716 return get_set_int (&p->preferredMessageSize, interp, argc, argv);
720 * do_maximumRecordSize: Set/get maximum record size
722 static int do_maximumRecordSize (void *obj, Tcl_Interp *interp,
723 int argc, char **argv)
729 p->maximumRecordSize = 30000;
732 return get_set_int (&p->maximumRecordSize, interp, argc, argv);
736 * do_initResult: Get init result
738 static int do_initResult (void *obj, Tcl_Interp *interp,
739 int argc, char **argv)
745 return get_set_int (&p->initResult, interp, argc, argv);
750 * do_implementationName: Set/get Implementation Name.
752 static int do_implementationName (void *obj, Tcl_Interp *interp,
753 int argc, char **argv)
758 return ir_strdup (interp, &p->implementationName,
759 "Index Data/IrTcl on YAZ");
761 return ir_strdel (interp, &p->implementationName);
764 free (p->implementationName);
765 if (ir_strdup (interp, &p->implementationName, argv[2])
769 Tcl_AppendResult (interp, p->implementationName, (char*) NULL);
774 * do_implementationId: Get Implementation Id.
776 static int do_implementationId (void *obj, Tcl_Interp *interp,
777 int argc, char **argv)
782 return ir_strdup (interp, &p->implementationId, "YAZ (id=81)");
784 return ir_strdel (interp, &p->implementationId);
785 Tcl_AppendResult (interp, p->implementationId, (char*) NULL);
790 * do_implementationVersion: get Implementation Version.
792 static int do_implementationVersion (void *obj, Tcl_Interp *interp,
793 int argc, char **argv)
798 return ir_strdup (interp, &p->implementationVersion,
799 "YAZ: " YAZ_VERSION " / IrTcl: " IR_TCL_VERSION);
801 return ir_strdel (interp, &p->implementationVersion);
802 Tcl_AppendResult (interp, p->implementationVersion, (char*) NULL);
807 * do_targetImplementationName: Get Implementation Name of target.
809 static int do_targetImplementationName (void *obj, Tcl_Interp *interp,
810 int argc, char **argv)
816 p->targetImplementationName = NULL;
820 return ir_strdel (interp, &p->targetImplementationName);
821 Tcl_AppendResult (interp, p->targetImplementationName, (char*) NULL);
826 * do_targetImplementationId: Get Implementation Id of target
828 static int do_targetImplementationId (void *obj, Tcl_Interp *interp,
829 int argc, char **argv)
835 p->targetImplementationId = NULL;
839 return ir_strdel (interp, &p->targetImplementationId);
840 Tcl_AppendResult (interp, p->targetImplementationId, (char*) NULL);
845 * do_targetImplementationVersion: Get Implementation Version of target
847 static int do_targetImplementationVersion (void *obj, Tcl_Interp *interp,
848 int argc, char **argv)
854 p->targetImplementationVersion = NULL;
858 return ir_strdel (interp, &p->targetImplementationVersion);
859 Tcl_AppendResult (interp, p->targetImplementationVersion, (char*) NULL);
864 * do_idAuthentication: Set/get id Authentication
866 static int do_idAuthentication (void *obj, Tcl_Interp *interp,
867 int argc, char **argv)
871 if (argc >= 3 || argc == -1)
873 free (p->idAuthenticationOpen);
874 free (p->idAuthenticationGroupId);
875 free (p->idAuthenticationUserId);
876 free (p->idAuthenticationPassword);
878 if (argc >= 3 || argc <= 0)
880 p->idAuthenticationOpen = NULL;
881 p->idAuthenticationGroupId = NULL;
882 p->idAuthenticationUserId = NULL;
883 p->idAuthenticationPassword = NULL;
891 if (ir_strdup (interp, &p->idAuthenticationOpen, argv[2])
897 if (ir_strdup (interp, &p->idAuthenticationGroupId, argv[2])
900 if (ir_strdup (interp, &p->idAuthenticationUserId, argv[3])
903 if (ir_strdup (interp, &p->idAuthenticationPassword, argv[4])
908 if (p->idAuthenticationOpen)
909 Tcl_AppendElement (interp, p->idAuthenticationOpen);
910 else if (p->idAuthenticationGroupId)
912 Tcl_AppendElement (interp, p->idAuthenticationGroupId);
913 Tcl_AppendElement (interp, p->idAuthenticationUserId);
914 Tcl_AppendElement (interp, p->idAuthenticationPassword);
920 * do_connect: connect method on IR object
922 static int do_connect (void *obj, Tcl_Interp *interp,
923 int argc, char **argv)
935 interp->result = "already connected";
938 if (!strcmp (p->cs_type, "tcpip"))
940 p->cs_link = cs_create (tcpip_type, CS_BLOCK, p->protocol_type);
941 addr = tcpip_strtoaddr (argv[2]);
944 interp->result = "tcpip_strtoaddr fail";
947 logf (LOG_DEBUG, "tcp/ip connect %s", argv[2]);
949 else if (!strcmp (p->cs_type, "mosi"))
952 p->cs_link = cs_create (mosi_type, CS_BLOCK, p->protocol_type);
953 addr = mosi_strtoaddr (argv[2]);
956 interp->result = "mosi_strtoaddr fail";
959 logf (LOG_DEBUG, "mosi connect %s", argv[2]);
961 interp->result = "MOSI support not there";
967 Tcl_AppendResult (interp, "Bad comstack type: ",
971 if (ir_strdup (interp, &p->hostname, argv[2]) == TCL_ERROR)
973 if ((r=cs_connect (p->cs_link, addr)) < 0)
975 interp->result = "connect fail";
976 do_disconnect (p, NULL, 2, NULL);
979 ir_select_add (cs_fileno (p->cs_link), p);
982 ir_select_add_write (cs_fileno (p->cs_link), p);
989 IrTcl_eval (p->interp, p->callback);
993 Tcl_AppendElement (interp, p->hostname);
998 * do_disconnect: disconnect method on IR object
1000 static int do_disconnect (void *obj, Tcl_Interp *interp,
1001 int argc, char **argv)
1016 ir_select_remove_write (cs_fileno (p->cs_link), p);
1017 ir_select_remove (cs_fileno (p->cs_link), p);
1019 assert (p->cs_link);
1020 cs_close (p->cs_link);
1023 ODR_MASK_ZERO (&p->options);
1024 ODR_MASK_SET (&p->options, 0);
1025 ODR_MASK_SET (&p->options, 1);
1026 ODR_MASK_SET (&p->options, 7);
1027 ODR_MASK_SET (&p->options, 14);
1029 ODR_MASK_ZERO (&p->protocolVersion);
1030 ODR_MASK_SET (&p->protocolVersion, 0);
1031 ODR_MASK_SET (&p->protocolVersion, 1);
1033 assert (!p->cs_link);
1038 * do_comstack: Set/get comstack method on IR object
1040 static int do_comstack (void *o, Tcl_Interp *interp,
1041 int argc, char **argv)
1046 return ir_strdup (interp, &obj->cs_type, "tcpip");
1047 else if (argc == -1)
1048 return ir_strdel (interp, &obj->cs_type);
1051 free (obj->cs_type);
1052 if (ir_strdup (interp, &obj->cs_type, argv[2]) == TCL_ERROR)
1055 Tcl_AppendElement (interp, obj->cs_type);
1061 * do_callback: add callback
1063 static int do_callback (void *obj, Tcl_Interp *interp,
1064 int argc, char **argv)
1073 else if (argc == -1)
1074 return ir_strdel (interp, &p->callback);
1080 if (ir_strdup (interp, &p->callback, argv[2]) == TCL_ERROR)
1091 * do_failback: add error handle callback
1093 static int do_failback (void *obj, Tcl_Interp *interp,
1094 int argc, char **argv)
1103 else if (argc == -1)
1104 return ir_strdel (interp, &p->failback);
1110 if (ir_strdup (interp, &p->failback, argv[2]) == TCL_ERROR)
1121 * do_protocol: Set/get protocol method on IR object
1123 static int do_protocol (void *o, Tcl_Interp *interp, int argc, char **argv)
1129 p->protocol_type = PROTO_Z3950;
1134 if (!strcmp (argv[2], "Z39"))
1135 p->protocol_type = PROTO_Z3950;
1136 else if (!strcmp (argv[2], "SR"))
1137 p->protocol_type = PROTO_SR;
1140 Tcl_AppendResult (interp, "Bad protocol: ", argv[2], NULL);
1145 switch (p->protocol_type)
1148 Tcl_AppendElement (interp, "Z39");
1151 Tcl_AppendElement (interp, "SR");
1158 * do_triggerResourceControl:
1160 static int do_triggerResourceControl (void *obj, Tcl_Interp *interp,
1161 int argc, char **argv)
1165 Z_TriggerResourceControlRequest *req;
1172 interp->result = "not connected";
1175 apdu = zget_APDU (p->odr_out, Z_APDU_triggerResourceControlRequest);
1176 req = apdu->u.triggerResourceControlRequest;
1178 if (!z_APDU (p->odr_out, &apdu, 0))
1180 Tcl_AppendResult (interp, odr_errlist [odr_geterror (p->odr_out)],
1182 odr_reset (p->odr_out);
1185 p->sbuf = odr_getbuf (p->odr_out, &p->slen, NULL);
1186 if ((r=cs_put (p->cs_link, p->sbuf, p->slen)) < 0)
1188 interp->result = "cs_put failed in triggerResourceControl";
1189 do_disconnect (p, NULL, 2, NULL);
1194 ir_select_add_write (cs_fileno(p->cs_link), p);
1195 logf (LOG_DEBUG, "Sent part of triggerResourceControl (%d bytes)",
1199 logf (LOG_DEBUG, "Sent whole of triggerResourceControl (%d bytes)",
1205 * do_databaseNames: specify database names
1207 static int do_databaseNames (void *obj, Tcl_Interp *interp,
1208 int argc, char **argv)
1211 IrTcl_SetCObj *p = obj;
1215 for (i=0; i<p->num_databaseNames; i++)
1216 free (p->databaseNames[i]);
1217 free (p->databaseNames);
1221 p->num_databaseNames = 0;
1222 p->databaseNames = NULL;
1227 for (i=0; i<p->num_databaseNames; i++)
1228 Tcl_AppendElement (interp, p->databaseNames[i]);
1231 if (p->databaseNames)
1233 for (i=0; i<p->num_databaseNames; i++)
1234 free (p->databaseNames[i]);
1235 free (p->databaseNames);
1237 p->num_databaseNames = argc - 2;
1238 if (!(p->databaseNames = ir_malloc (interp,
1239 sizeof(*p->databaseNames) * p->num_databaseNames)))
1241 for (i=0; i<p->num_databaseNames; i++)
1243 if (ir_strdup (interp, &p->databaseNames[i], argv[2+i])
1251 * do_replaceIndicator: Set/get replace Set indicator
1253 static int do_replaceIndicator (void *obj, Tcl_Interp *interp,
1254 int argc, char **argv)
1256 IrTcl_SetCObj *p = obj;
1260 p->replaceIndicator = 1;
1263 return get_set_int (&p->replaceIndicator, interp, argc, argv);
1267 * do_queryType: Set/Get query method
1269 static int do_queryType (void *obj, Tcl_Interp *interp,
1270 int argc, char **argv)
1272 IrTcl_SetCObj *p = obj;
1275 return ir_strdup (interp, &p->queryType, "rpn");
1276 else if (argc == -1)
1277 return ir_strdel (interp, &p->queryType);
1280 free (p->queryType);
1281 if (ir_strdup (interp, &p->queryType, argv[2]) == TCL_ERROR)
1284 Tcl_AppendResult (interp, p->queryType, NULL);
1289 * do_userInformationField: Get User information field
1291 static int do_userInformationField (void *obj, Tcl_Interp *interp,
1292 int argc, char **argv)
1298 p->userInformationField = NULL;
1301 else if (argc == -1)
1302 return ir_strdel (interp, &p->userInformationField);
1303 Tcl_AppendResult (interp, p->userInformationField, NULL);
1308 * do_smallSetUpperBound: Set/get small set upper bound
1310 static int do_smallSetUpperBound (void *o, Tcl_Interp *interp,
1311 int argc, char **argv)
1313 IrTcl_SetCObj *p = o;
1317 p->smallSetUpperBound = 0;
1320 return get_set_int (&p->smallSetUpperBound, interp, argc, argv);
1324 * do_largeSetLowerBound: Set/get large set lower bound
1326 static int do_largeSetLowerBound (void *o, Tcl_Interp *interp,
1327 int argc, char **argv)
1329 IrTcl_SetCObj *p = o;
1333 p->largeSetLowerBound = 2;
1336 return get_set_int (&p->largeSetLowerBound, interp, argc, argv);
1340 * do_mediumSetPresentNumber: Set/get large set lower bound
1342 static int do_mediumSetPresentNumber (void *o, Tcl_Interp *interp,
1343 int argc, char **argv)
1345 IrTcl_SetCObj *p = o;
1349 p->mediumSetPresentNumber = 0;
1352 return get_set_int (&p->mediumSetPresentNumber, interp, argc, argv);
1356 * do_referenceId: Set/Get referenceId
1358 static int do_referenceId (void *obj, Tcl_Interp *interp,
1359 int argc, char **argv)
1361 IrTcl_SetCObj *p = obj;
1365 p->referenceId = NULL;
1368 else if (argc == -1)
1369 return ir_strdel (interp, &p->referenceId);
1372 free (p->referenceId);
1373 if (ir_strdup (interp, &p->referenceId, argv[2]) == TCL_ERROR)
1376 Tcl_AppendResult (interp, p->referenceId, NULL);
1381 * do_preferredRecordSyntax: Set/get preferred record syntax
1383 static int do_preferredRecordSyntax (void *obj, Tcl_Interp *interp,
1384 int argc, char **argv)
1386 IrTcl_SetCObj *p = obj;
1390 p->preferredRecordSyntax = NULL;
1393 else if (argc == -1)
1395 free (p->preferredRecordSyntax);
1396 p->preferredRecordSyntax = NULL;
1401 free (p->preferredRecordSyntax);
1402 p->preferredRecordSyntax = NULL;
1403 if (argv[2][0] && (p->preferredRecordSyntax =
1404 malloc (sizeof(*p->preferredRecordSyntax))))
1405 *p->preferredRecordSyntax = IrTcl_getRecordSyntaxVal (argv[2]);
1410 static IrTcl_Method ir_method_tab[] = {
1411 { 1, "comstack", do_comstack },
1412 { 1, "protocol", do_protocol },
1413 { 0, "failback", do_failback },
1414 { 0, "failInfo", do_failInfo },
1416 { 1, "connect", do_connect },
1417 { 0, "protocolVersion", do_protocolVersion },
1418 { 1, "preferredMessageSize", do_preferredMessageSize },
1419 { 1, "maximumRecordSize", do_maximumRecordSize },
1420 { 1, "implementationName", do_implementationName },
1421 { 1, "implementationId", do_implementationId },
1422 { 1, "implementationVersion", do_implementationVersion },
1423 { 0, "targetImplementationName", do_targetImplementationName },
1424 { 0, "targetImplementationId", do_targetImplementationId },
1425 { 0, "targetImplementationVersion", do_targetImplementationVersion },
1426 { 0, "userInformationField", do_userInformationField },
1427 { 1, "idAuthentication", do_idAuthentication },
1428 { 0, "options", do_options },
1429 { 0, "init", do_init_request },
1430 { 0, "initResult", do_initResult },
1431 { 0, "disconnect", do_disconnect },
1432 { 0, "callback", do_callback },
1433 { 0, "triggerResourceControl", do_triggerResourceControl },
1437 static IrTcl_Method ir_set_c_method_tab[] = {
1438 { 0, "databaseNames", do_databaseNames},
1439 { 0, "replaceIndicator", do_replaceIndicator},
1440 { 0, "queryType", do_queryType },
1441 { 0, "preferredRecordSyntax", do_preferredRecordSyntax },
1442 { 0, "smallSetUpperBound", do_smallSetUpperBound},
1443 { 0, "largeSetLowerBound", do_largeSetLowerBound},
1444 { 0, "mediumSetPresentNumber", do_mediumSetPresentNumber},
1445 { 0, "referenceId", do_referenceId },
1450 * ir_obj_method: IR Object methods
1452 static int ir_obj_method (ClientData clientData, Tcl_Interp *interp,
1453 int argc, char **argv)
1455 IrTcl_Methods tab[3];
1456 IrTcl_Obj *p = clientData;
1459 return ir_method_r (clientData, interp, argc, argv, ir_method_tab);
1461 tab[0].tab = ir_method_tab;
1463 tab[1].tab = ir_set_c_method_tab;
1464 tab[1].obj = &p->set_inher;
1467 return ir_method (interp, argc, argv, tab);
1471 * ir_obj_delete: IR Object disposal
1473 static void ir_obj_delete (ClientData clientData)
1475 IrTcl_Obj *obj = clientData;
1476 IrTcl_Methods tab[3];
1479 if (obj->ref_count > 0)
1481 assert (obj->ref_count == 0);
1483 tab[0].tab = ir_method_tab;
1485 tab[1].tab = ir_set_c_method_tab;
1486 tab[1].obj = &obj->set_inher;
1489 ir_method (NULL, -1, NULL, tab);
1490 odr_destroy (obj->odr_in);
1491 odr_destroy (obj->odr_out);
1492 odr_destroy (obj->odr_pr);
1493 free (obj->buf_out);
1499 * ir_obj_mk: IR Object creation
1501 static int ir_obj_mk (ClientData clientData, Tcl_Interp *interp,
1502 int argc, char **argv)
1504 IrTcl_Methods tab[3];
1512 interp->result = "wrong # args";
1515 if (!(obj = ir_malloc (interp, sizeof(*obj))))
1520 obj->bibset = ccl_qual_mk ();
1521 if ((inf = fopen ("default.bib", "r")))
1523 ccl_qual_file (obj->bibset, inf);
1528 obj->odr_in = odr_createmem (ODR_DECODE);
1529 obj->odr_out = odr_createmem (ODR_ENCODE);
1530 obj->odr_pr = odr_createmem (ODR_PRINT);
1532 obj->len_out = 10000;
1533 if (!(obj->buf_out = ir_malloc (interp, obj->len_out)))
1535 odr_setbuf (obj->odr_out, obj->buf_out, obj->len_out, 0);
1540 tab[0].tab = ir_method_tab;
1542 tab[1].tab = ir_set_c_method_tab;
1543 tab[1].obj = &obj->set_inher;
1546 if (ir_method (interp, 0, NULL, tab) == TCL_ERROR)
1548 Tcl_CreateCommand (interp, argv[1], ir_obj_method,
1549 (ClientData) obj, ir_obj_delete);
1553 /* ------------------------------------------------------- */
1555 * do_search: Do search request
1557 static int do_search (void *o, Tcl_Interp *interp, int argc, char **argv)
1559 Z_SearchRequest *req;
1563 IrTcl_SetObj *obj = o;
1564 IrTcl_Obj *p = obj->parent;
1574 interp->result = "wrong # args";
1577 if (!obj->set_inher.num_databaseNames)
1579 interp->result = "no databaseNames";
1584 interp->result = "not connected";
1587 odr_reset (p->odr_out);
1588 apdu = zget_APDU (p->odr_out, Z_APDU_searchRequest);
1589 req = apdu->u.searchRequest;
1591 bib1.proto = p->protocol_type;
1592 bib1.class = CLASS_ATTSET;
1593 bib1.value = VAL_BIB1;
1595 set_referenceId (p->odr_out, &req->referenceId,
1596 obj->set_inher.referenceId);
1598 req->smallSetUpperBound = &obj->set_inher.smallSetUpperBound;
1599 req->largeSetLowerBound = &obj->set_inher.largeSetLowerBound;
1600 req->mediumSetPresentNumber = &obj->set_inher.mediumSetPresentNumber;
1601 req->replaceIndicator = &obj->set_inher.replaceIndicator;
1602 req->resultSetName = obj->setName ? obj->setName : "Default";
1603 logf (LOG_DEBUG, "Search, resultSetName %s", req->resultSetName);
1604 req->num_databaseNames = obj->set_inher.num_databaseNames;
1605 req->databaseNames = obj->set_inher.databaseNames;
1606 for (r=0; r < obj->set_inher.num_databaseNames; r++)
1607 logf (LOG_DEBUG, " Database %s", obj->set_inher.databaseNames[r]);
1608 req->smallSetElementSetNames = 0;
1609 req->mediumSetElementSetNames = 0;
1610 if (obj->set_inher.preferredRecordSyntax)
1612 struct oident ident;
1614 ident.proto = p->protocol_type;
1615 ident.class = CLASS_RECSYN;
1616 ident.value = *obj->set_inher.preferredRecordSyntax;
1617 logf (LOG_DEBUG, "Preferred record syntax is %d", ident.value);
1618 req->preferredRecordSyntax = odr_oiddup (p->odr_out,
1619 oid_getoidbyent (&ident));
1622 req->preferredRecordSyntax = 0;
1623 req->query = &query;
1625 if (!strcmp (obj->set_inher.queryType, "rpn"))
1627 Z_RPNQuery *RPNquery;
1629 RPNquery = p_query_rpn (p->odr_out, argv[2]);
1632 Tcl_AppendResult (interp, "Syntax error in query", NULL);
1635 RPNquery->attributeSetId = oid_getoidbyent (&bib1);
1636 query.which = Z_Query_type_1;
1637 query.u.type_1 = RPNquery;
1638 logf (LOG_DEBUG, "RPN");
1641 else if (!strcmp (obj->set_inher.queryType, "cclrpn"))
1645 struct ccl_rpn_node *rpn;
1646 Z_RPNQuery *RPNquery;
1648 rpn = ccl_find_str(p->bibset, argv[2], &error, &pos);
1651 Tcl_AppendResult (interp, "CCL error: ",
1652 ccl_err_msg(error), NULL);
1655 ccl_pr_tree (rpn, stderr);
1656 fprintf (stderr, "\n");
1657 assert((RPNquery = ccl_rpn_query(rpn)));
1658 RPNquery->attributeSetId = oid_getoidbyent (&bib1);
1659 query.which = Z_Query_type_1;
1660 query.u.type_1 = RPNquery;
1661 logf (LOG_DEBUG, "CCLRPN");
1664 else if (!strcmp (obj->set_inher.queryType, "ccl"))
1666 query.which = Z_Query_type_2;
1667 query.u.type_2 = &ccl_query;
1668 ccl_query.buf = (unsigned char *) argv[2];
1669 ccl_query.len = strlen (argv[2]);
1670 logf (LOG_DEBUG, "CCL");
1674 interp->result = "unknown query method";
1677 if (!z_APDU (p->odr_out, &apdu, 0))
1679 interp->result = odr_errlist [odr_geterror (p->odr_out)];
1680 odr_reset (p->odr_out);
1683 p->sbuf = odr_getbuf (p->odr_out, &p->slen, NULL);
1684 if ((r=cs_put (p->cs_link, p->sbuf, p->slen)) < 0)
1686 interp->result = "cs_put failed in search";
1691 ir_select_add_write (cs_fileno(p->cs_link), p);
1692 logf (LOG_DEBUG, "Sent part of searchRequest (%d bytes)", p->slen);
1696 logf (LOG_DEBUG, "Whole search request (%d bytes)", p->slen);
1702 * do_resultCount: Get number of hits
1704 static int do_resultCount (void *o, Tcl_Interp *interp,
1705 int argc, char **argv)
1707 IrTcl_SetObj *obj = o;
1711 return get_set_int (&obj->resultCount, interp, argc, argv);
1715 * do_searchStatus: Get search status (after search response)
1717 static int do_searchStatus (void *o, Tcl_Interp *interp,
1718 int argc, char **argv)
1720 IrTcl_SetObj *obj = o;
1724 return get_set_int (&obj->searchStatus, interp, argc, argv);
1728 * do_presentStatus: Get search status (after search/present response)
1730 static int do_presentStatus (void *o, Tcl_Interp *interp,
1731 int argc, char **argv)
1733 IrTcl_SetObj *obj = o;
1737 return get_set_int (&obj->presentStatus, interp, argc, argv);
1741 * do_nextResultSetPosition: Get next result set position
1742 * (after search/present response)
1744 static int do_nextResultSetPosition (void *o, Tcl_Interp *interp,
1745 int argc, char **argv)
1747 IrTcl_SetObj *obj = o;
1751 return get_set_int (&obj->nextResultSetPosition, interp, argc, argv);
1755 * do_setName: Set result Set name
1757 static int do_setName (void *o, Tcl_Interp *interp,
1758 int argc, char **argv)
1760 IrTcl_SetObj *obj = o;
1763 return ir_strdup (interp, &obj->setName, "Default");
1764 else if (argc == -1)
1765 return ir_strdel (interp, &obj->setName);
1768 free (obj->setName);
1769 if (ir_strdup (interp, &obj->setName, argv[2])
1773 Tcl_AppendElement (interp, obj->setName);
1778 * do_numberOfRecordsReturned: Get number of records returned
1780 static int do_numberOfRecordsReturned (void *o, Tcl_Interp *interp,
1781 int argc, char **argv)
1783 IrTcl_SetObj *obj = o;
1787 obj->numberOfRecordsReturned = 0;
1790 return get_set_int (&obj->numberOfRecordsReturned, interp, argc, argv);
1794 * do_type: Return type (if any) at position.
1796 static int do_type (void *o, Tcl_Interp *interp, int argc, char **argv)
1798 IrTcl_SetObj *obj = o;
1800 IrTcl_RecordList *rl;
1804 obj->record_list = NULL;
1807 else if (argc == -1)
1809 delete_IR_records (obj);
1814 sprintf (interp->result, "wrong # args");
1817 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
1819 rl = find_IR_record (obj, offset);
1824 case Z_NamePlusRecord_databaseRecord:
1825 interp->result = "DB";
1827 case Z_NamePlusRecord_surrogateDiagnostic:
1828 interp->result = "SD";
1836 * do_recordType: Return record type (if any) at position.
1838 static int do_recordType (void *o, Tcl_Interp *interp, int argc, char **argv)
1840 IrTcl_SetObj *obj = o;
1842 IrTcl_RecordList *rl;
1848 else if (argc == -1)
1854 sprintf (interp->result, "wrong # args");
1857 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
1859 rl = find_IR_record (obj, offset);
1862 if (rl->which != Z_NamePlusRecord_databaseRecord)
1864 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
1867 Tcl_AppendElement (interp, (char*)
1868 IrTcl_getRecordSyntaxStr (rl->u.dbrec.type));
1875 static int ir_diagResult (Tcl_Interp *interp, IrTcl_Diagnostic *list, int num)
1881 for (i = 0; i<num; i++)
1883 logf (LOG_DEBUG, "Diagnostic, code %d", list[i].condition);
1884 sprintf (buf, "%d", list[i].condition);
1885 Tcl_AppendElement (interp, buf);
1886 cp = diagbib1_str (list[i].condition);
1888 Tcl_AppendElement (interp, (char*) cp);
1890 Tcl_AppendElement (interp, "");
1891 if (list[i].addinfo)
1892 Tcl_AppendElement (interp, (char*) list[i].addinfo);
1894 Tcl_AppendElement (interp, "");
1900 * do_diag: Return diagnostic record info
1902 static int do_diag (void *o, Tcl_Interp *interp, int argc, char **argv)
1904 IrTcl_SetObj *obj = o;
1906 IrTcl_RecordList *rl;
1912 sprintf (interp->result, "wrong # args");
1915 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
1917 rl = find_IR_record (obj, offset);
1920 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
1923 if (rl->which != Z_NamePlusRecord_surrogateDiagnostic)
1925 Tcl_AppendResult (interp, "No Diagnostic record at #", argv[2], NULL);
1928 return ir_diagResult (interp, rl->u.surrogateDiagnostics.list,
1929 rl->u.surrogateDiagnostics.num);
1933 * do_getMarc: Get ISO2709 Record lines/fields
1935 static int do_getMarc (void *o, Tcl_Interp *interp, int argc, char **argv)
1937 IrTcl_SetObj *obj = o;
1939 IrTcl_RecordList *rl;
1945 sprintf (interp->result, "wrong # args");
1948 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
1950 rl = find_IR_record (obj, offset);
1953 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
1956 if (rl->which != Z_NamePlusRecord_databaseRecord)
1958 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
1961 return ir_tcl_get_marc (interp, rl->u.dbrec.buf, argc, argv);
1965 * do_getSutrs: Get SUTRS Record
1967 static int do_getSutrs (void *o, Tcl_Interp *interp, int argc, char **argv)
1969 IrTcl_SetObj *obj = o;
1971 IrTcl_RecordList *rl;
1977 sprintf (interp->result, "wrong # args");
1980 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
1982 rl = find_IR_record (obj, offset);
1985 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
1988 if (rl->which != Z_NamePlusRecord_databaseRecord)
1990 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
1993 if (rl->u.dbrec.type != VAL_SUTRS)
1995 Tcl_AppendElement (interp, rl->u.dbrec.buf);
2001 * do_responseStatus: Return response status (present or search)
2003 static int do_responseStatus (void *o, Tcl_Interp *interp,
2004 int argc, char **argv)
2006 IrTcl_SetObj *obj = o;
2010 obj->recordFlag = 0;
2011 obj->nonSurrogateDiagnosticNum = 0;
2012 obj->nonSurrogateDiagnosticList = NULL;
2015 else if (argc == -1)
2017 ir_deleteDiags (&obj->nonSurrogateDiagnosticList,
2018 &obj->nonSurrogateDiagnosticNum);
2021 if (!obj->recordFlag)
2023 Tcl_AppendElement (interp, "OK");
2028 case Z_Records_DBOSD:
2029 Tcl_AppendElement (interp, "DBOSD");
2032 Tcl_AppendElement (interp, "NSD");
2033 return ir_diagResult (interp, obj->nonSurrogateDiagnosticList,
2034 obj->nonSurrogateDiagnosticNum);
2040 * do_present: Perform Present Request
2043 static int do_present (void *o, Tcl_Interp *interp,
2044 int argc, char **argv)
2046 IrTcl_SetObj *obj = o;
2047 IrTcl_Obj *p = obj->parent;
2049 Z_PresentRequest *req;
2058 if (Tcl_GetInt (interp, argv[2], &start) == TCL_ERROR)
2065 if (Tcl_GetInt (interp, argv[3], &number) == TCL_ERROR)
2072 interp->result = "not connected";
2075 odr_reset (p->odr_out);
2077 obj->number = number;
2079 apdu = zget_APDU (p->odr_out, Z_APDU_presentRequest);
2080 req = apdu->u.presentRequest;
2082 set_referenceId (p->odr_out, &req->referenceId,
2083 obj->set_inher.referenceId);
2085 req->resultSetId = obj->setName ? obj->setName : "Default";
2087 req->resultSetStartPoint = &start;
2088 req->numberOfRecordsRequested = &number;
2089 if (obj->set_inher.preferredRecordSyntax)
2091 struct oident ident;
2093 ident.proto = p->protocol_type;
2094 ident.class = CLASS_RECSYN;
2095 ident.value = *obj->set_inher.preferredRecordSyntax;
2096 logf (LOG_DEBUG, "Preferred record syntax is %d", ident.value);
2097 req->preferredRecordSyntax = odr_oiddup (p->odr_out,
2098 oid_getoidbyent (&ident));
2101 req->preferredRecordSyntax = 0;
2103 if (!z_APDU (p->odr_out, &apdu, 0))
2105 interp->result = odr_errlist [odr_geterror (p->odr_out)];
2106 odr_reset (p->odr_out);
2109 p->sbuf = odr_getbuf (p->odr_out, &p->slen, NULL);
2110 if ((r=cs_put (p->cs_link, p->sbuf, p->slen)) < 0)
2112 interp->result = "cs_put failed in present";
2117 ir_select_add_write (cs_fileno(p->cs_link), p);
2118 logf (LOG_DEBUG, "Part of present request, start=%d, num=%d"
2119 " (%d bytes)", start, number, p->slen);
2123 logf (LOG_DEBUG, "Whole present request, start=%d, num=%d"
2124 " (%d bytes)", start, number, p->slen);
2130 * do_loadFile: Load result set from file
2133 static int do_loadFile (void *o, Tcl_Interp *interp,
2134 int argc, char **argv)
2136 IrTcl_SetObj *setobj = o;
2146 interp->result = "wrong # args";
2149 inf = fopen (argv[2], "r");
2152 Tcl_AppendResult (interp, "Cannot open ", argv[2], NULL);
2155 while ((buf = ir_tcl_fread_marc (inf, &size)))
2157 IrTcl_RecordList *rl;
2159 rl = new_IR_record (setobj, no, Z_NamePlusRecord_databaseRecord);
2160 rl->u.dbrec.buf = buf;
2161 rl->u.dbrec.size = size;
2164 setobj->numberOfRecordsReturned = no-1;
2169 static IrTcl_Method ir_set_method_tab[] = {
2170 { 0, "search", do_search },
2171 { 0, "searchStatus", do_searchStatus },
2172 { 0, "presentStatus", do_presentStatus },
2173 { 0, "nextResultSetPosition", do_nextResultSetPosition },
2174 { 0, "setName", do_setName },
2175 { 0, "resultCount", do_resultCount },
2176 { 0, "numberOfRecordsReturned", do_numberOfRecordsReturned },
2177 { 0, "present", do_present },
2178 { 0, "type", do_type },
2179 { 0, "getMarc", do_getMarc },
2180 { 0, "getSutrs", do_getSutrs },
2181 { 0, "recordType", do_recordType },
2182 { 0, "diag", do_diag },
2183 { 0, "responseStatus", do_responseStatus },
2184 { 0, "loadFile", do_loadFile },
2189 * ir_set_obj_method: IR Set Object methods
2191 static int ir_set_obj_method (ClientData clientData, Tcl_Interp *interp,
2192 int argc, char **argv)
2194 IrTcl_Methods tabs[3];
2195 IrTcl_SetObj *p = clientData;
2199 interp->result = "wrong # args";
2202 tabs[0].tab = ir_set_method_tab;
2204 tabs[1].tab = ir_set_c_method_tab;
2205 tabs[1].obj = &p->set_inher;
2208 return ir_method (interp, argc, argv, tabs);
2212 * ir_set_obj_delete: IR Set Object disposal
2214 static void ir_set_obj_delete (ClientData clientData)
2216 IrTcl_Methods tabs[3];
2217 IrTcl_SetObj *p = clientData;
2219 tabs[0].tab = ir_set_method_tab;
2221 tabs[1].tab = ir_set_c_method_tab;
2222 tabs[1].obj = &p->set_inher;
2225 ir_method (NULL, -1, NULL, tabs);
2231 * ir_set_obj_mk: IR Set Object creation
2233 static int ir_set_obj_mk (ClientData clientData, Tcl_Interp *interp,
2234 int argc, char **argv)
2236 IrTcl_Methods tabs[3];
2239 if (argc < 2 || argc > 3)
2241 interp->result = "wrong # args";
2244 if (!(obj = ir_malloc (interp, sizeof(*obj))))
2248 Tcl_CmdInfo parent_info;
2253 if (!Tcl_GetCommandInfo (interp, argv[2], &parent_info))
2255 interp->result = "No parent";
2258 obj->parent = (IrTcl_Obj *) parent_info.clientData;
2260 dst = &obj->set_inher;
2261 src = &obj->parent->set_inher;
2263 dst->num_databaseNames = src->num_databaseNames;
2264 if (!(dst->databaseNames =
2265 ir_malloc (interp, sizeof (*dst->databaseNames)
2266 * dst->num_databaseNames)))
2268 for (i = 0; i < dst->num_databaseNames; i++)
2270 if (ir_strdup (interp, &dst->databaseNames[i],
2271 src->databaseNames[i]) == TCL_ERROR)
2274 if (ir_strdup (interp, &dst->queryType, src->queryType)
2278 if (ir_strdup (interp, &dst->referenceId, src->referenceId)
2282 if (src->preferredRecordSyntax &&
2283 (dst->preferredRecordSyntax
2284 = malloc (sizeof(*dst->preferredRecordSyntax))))
2285 *dst->preferredRecordSyntax = *src->preferredRecordSyntax;
2287 dst->preferredRecordSyntax = NULL;
2288 dst->replaceIndicator = src->replaceIndicator;
2289 dst->smallSetUpperBound = src->smallSetUpperBound;
2290 dst->largeSetLowerBound = src->largeSetLowerBound;
2291 dst->mediumSetPresentNumber = src->mediumSetPresentNumber;
2296 tabs[0].tab = ir_set_method_tab;
2300 if (ir_method (interp, 0, NULL, tabs) == TCL_ERROR)
2303 Tcl_CreateCommand (interp, argv[1], ir_set_obj_method,
2304 (ClientData) obj, ir_set_obj_delete);
2308 /* ------------------------------------------------------- */
2311 * do_scan: Perform scan
2313 static int do_scan (void *o, Tcl_Interp *interp, int argc, char **argv)
2317 IrTcl_ScanObj *obj = o;
2318 IrTcl_Obj *p = obj->parent;
2322 struct ccl_rpn_node *rpn;
2331 interp->result = "wrong # args";
2334 if (!p->set_inher.num_databaseNames)
2336 interp->result = "no databaseNames";
2341 interp->result = "not connected";
2344 odr_reset (p->odr_out);
2346 bib1.proto = p->protocol_type;
2347 bib1.class = CLASS_ATTSET;
2348 bib1.value = VAL_BIB1;
2350 apdu = zget_APDU (p->odr_out, Z_APDU_scanRequest);
2351 req = apdu->u.scanRequest;
2353 set_referenceId (p->odr_out, &req->referenceId, p->set_inher.referenceId);
2354 req->num_databaseNames = p->set_inher.num_databaseNames;
2355 req->databaseNames = p->set_inher.databaseNames;
2356 req->attributeSet = oid_getoidbyent (&bib1);
2359 if (!(req->termListAndStartPoint = p_query_scan (p->odr_out, argv[2])))
2361 Tcl_AppendResult (interp, "Syntax error in query", NULL);
2365 rpn = ccl_find_str(p->bibset, argv[2], &r, &pos);
2368 Tcl_AppendResult (interp, "CCL error: ", ccl_err_msg (r), NULL);
2371 ccl_pr_tree (rpn, stderr);
2372 fprintf (stderr, "\n");
2373 if (!(req->termListAndStartPoint = ccl_scan_query (rpn)))
2376 req->stepSize = &obj->stepSize;
2377 req->numberOfTermsRequested = &obj->numberOfTermsRequested;
2378 req->preferredPositionInResponse = &obj->preferredPositionInResponse;
2379 logf (LOG_DEBUG, "stepSize=%d", *req->stepSize);
2380 logf (LOG_DEBUG, "numberOfTermsRequested=%d",
2381 *req->numberOfTermsRequested);
2382 logf (LOG_DEBUG, "preferredPositionInResponse=%d",
2383 *req->preferredPositionInResponse);
2385 if (!z_APDU (p->odr_out, &apdu, 0))
2387 interp->result = odr_errlist [odr_geterror (p->odr_out)];
2388 odr_reset (p->odr_out);
2391 p->sbuf = odr_getbuf (p->odr_out, &p->slen, NULL);
2392 if ((r=cs_put (p->cs_link, p->sbuf, p->slen)) < 0)
2394 interp->result = "cs_put failed in scan";
2399 ir_select_add_write (cs_fileno(p->cs_link), p);
2400 logf (LOG_DEBUG, "Sent part of scanRequest (%d bytes)", p->slen);
2404 logf (LOG_DEBUG, "Whole scan request (%d bytes)", p->slen);
2410 * do_stepSize: Set/get replace Step Size
2412 static int do_stepSize (void *obj, Tcl_Interp *interp,
2413 int argc, char **argv)
2415 IrTcl_ScanObj *p = obj;
2421 return get_set_int (&p->stepSize, interp, argc, argv);
2425 * do_numberOfTermsRequested: Set/get Number of Terms requested
2427 static int do_numberOfTermsRequested (void *obj, Tcl_Interp *interp,
2428 int argc, char **argv)
2430 IrTcl_ScanObj *p = obj;
2434 p->numberOfTermsRequested = 20;
2437 return get_set_int (&p->numberOfTermsRequested, interp, argc, argv);
2442 * do_preferredPositionInResponse: Set/get preferred Position
2444 static int do_preferredPositionInResponse (void *obj, Tcl_Interp *interp,
2445 int argc, char **argv)
2447 IrTcl_ScanObj *p = obj;
2451 p->preferredPositionInResponse = 1;
2454 return get_set_int (&p->preferredPositionInResponse, interp, argc, argv);
2458 * do_scanStatus: Get scan status
2460 static int do_scanStatus (void *obj, Tcl_Interp *interp,
2461 int argc, char **argv)
2463 IrTcl_ScanObj *p = obj;
2467 return get_set_int (&p->scanStatus, interp, argc, argv);
2471 * do_numberOfEntriesReturned: Get number of Entries returned
2473 static int do_numberOfEntriesReturned (void *obj, Tcl_Interp *interp,
2474 int argc, char **argv)
2476 IrTcl_ScanObj *p = obj;
2480 return get_set_int (&p->numberOfEntriesReturned, interp, argc, argv);
2484 * do_positionOfTerm: Get position of Term
2486 static int do_positionOfTerm (void *obj, Tcl_Interp *interp,
2487 int argc, char **argv)
2489 IrTcl_ScanObj *p = obj;
2493 return get_set_int (&p->positionOfTerm, interp, argc, argv);
2497 * do_scanLine: get Scan Line (surrogate or normal) after response
2499 static int do_scanLine (void *obj, Tcl_Interp *interp, int argc, char **argv)
2501 IrTcl_ScanObj *p = obj;
2507 p->entries_flag = 0;
2509 p->nonSurrogateDiagnosticNum = 0;
2510 p->nonSurrogateDiagnosticList = 0;
2513 else if (argc == -1)
2515 p->entries_flag = 0;
2516 /* release entries */
2519 ir_deleteDiags (&p->nonSurrogateDiagnosticList,
2520 &p->nonSurrogateDiagnosticNum);
2525 interp->result = "wrong # args";
2528 if (Tcl_GetInt (interp, argv[2], &i) == TCL_ERROR)
2530 if (!p->entries_flag || p->which != Z_ListEntries_entries || !p->entries
2531 || i >= p->num_entries || i < 0)
2533 switch (p->entries[i].which)
2535 case Z_Entry_termInfo:
2536 Tcl_AppendElement (interp, "T");
2537 if (p->entries[i].u.term.buf)
2538 Tcl_AppendElement (interp, p->entries[i].u.term.buf);
2540 Tcl_AppendElement (interp, "");
2541 sprintf (numstr, "%d", p->entries[i].u.term.globalOccurrences);
2542 Tcl_AppendElement (interp, numstr);
2544 case Z_Entry_surrogateDiagnostic:
2545 Tcl_AppendElement (interp, "SD");
2546 return ir_diagResult (interp, p->entries[i].u.diag.list,
2547 p->entries[i].u.diag.num);
2553 static IrTcl_Method ir_scan_method_tab[] = {
2554 { 0, "scan", do_scan },
2555 { 0, "stepSize", do_stepSize },
2556 { 0, "numberOfTermsRequested", do_numberOfTermsRequested },
2557 { 0, "preferredPositionInResponse", do_preferredPositionInResponse },
2558 { 0, "scanStatus", do_scanStatus },
2559 { 0, "numberOfEntriesReturned", do_numberOfEntriesReturned },
2560 { 0, "positionOfTerm", do_positionOfTerm },
2561 { 0, "scanLine", do_scanLine },
2566 * ir_scan_obj_method: IR Scan Object methods
2568 static int ir_scan_obj_method (ClientData clientData, Tcl_Interp *interp,
2569 int argc, char **argv)
2571 IrTcl_Methods tabs[2];
2575 interp->result = "wrong # args";
2578 tabs[0].tab = ir_scan_method_tab;
2579 tabs[0].obj = clientData;
2582 return ir_method (interp, argc, argv, tabs);
2586 * ir_scan_obj_delete: IR Scan Object disposal
2588 static void ir_scan_obj_delete (ClientData clientData)
2590 IrTcl_Methods tabs[2];
2591 IrTcl_ScanObj *obj = clientData;
2593 tabs[0].tab = ir_scan_method_tab;
2597 ir_method (NULL, -1, NULL, tabs);
2602 * ir_scan_obj_mk: IR Scan Object creation
2604 static int ir_scan_obj_mk (ClientData clientData, Tcl_Interp *interp,
2605 int argc, char **argv)
2607 Tcl_CmdInfo parent_info;
2609 IrTcl_Methods tabs[2];
2613 interp->result = "wrong # args";
2616 if (!Tcl_GetCommandInfo (interp, argv[2], &parent_info))
2618 interp->result = "No parent";
2621 if (!(obj = ir_malloc (interp, sizeof(*obj))))
2624 obj->parent = (IrTcl_Obj *) parent_info.clientData;
2626 tabs[0].tab = ir_scan_method_tab;
2630 if (ir_method (interp, 0, NULL, tabs) == TCL_ERROR)
2632 Tcl_CreateCommand (interp, argv[1], ir_scan_obj_method,
2633 (ClientData) obj, ir_scan_obj_delete);
2637 /* ------------------------------------------------------- */
2639 static void ir_initResponse (void *obj, Z_InitResponse *initrs)
2643 p->initResult = *initrs->result ? 1 : 0;
2644 if (!*initrs->result)
2645 logf (LOG_DEBUG, "Connection rejected by target");
2647 logf (LOG_DEBUG, "Connection accepted by target");
2649 get_referenceId (&p->set_inher.referenceId, initrs->referenceId);
2651 free (p->targetImplementationId);
2652 ir_strdup (p->interp, &p->targetImplementationId,
2653 initrs->implementationId);
2654 free (p->targetImplementationName);
2655 ir_strdup (p->interp, &p->targetImplementationName,
2656 initrs->implementationName);
2657 free (p->targetImplementationVersion);
2658 ir_strdup (p->interp, &p->targetImplementationVersion,
2659 initrs->implementationVersion);
2661 p->maximumRecordSize = *initrs->maximumRecordSize;
2662 p->preferredMessageSize = *initrs->preferredMessageSize;
2664 memcpy (&p->options, initrs->options, sizeof(initrs->options));
2665 memcpy (&p->protocolVersion, initrs->protocolVersion,
2666 sizeof(initrs->protocolVersion));
2667 free (p->userInformationField);
2668 p->userInformationField = NULL;
2669 if (initrs->userInformationField)
2673 if (initrs->userInformationField->which == ODR_EXTERNAL_octet &&
2674 (p->userInformationField =
2676 initrs->userInformationField->u.octet_aligned->len)
2679 memcpy (p->userInformationField,
2680 initrs->userInformationField->u.octet_aligned->buf,
2682 (p->userInformationField)[len] = '\0';
2687 static void ir_deleteDiags (IrTcl_Diagnostic **dst_list, int *dst_num)
2690 for (i = 0; i<*dst_num; i++)
2691 free (dst_list[i]->addinfo);
2697 static void ir_handleDiags (IrTcl_Diagnostic **dst_list, int *dst_num,
2698 Z_DiagRec **list, int num)
2704 *dst_list = malloc (sizeof(**dst_list) * num);
2710 for (i = 0; i<num; i++)
2712 switch (list[i]->which)
2714 case Z_DiagRec_defaultFormat:
2715 (*dst_list)[i].condition = *list[i]->u.defaultFormat->condition;
2716 addinfo = list[i]->u.defaultFormat->addinfo;
2718 ((*dst_list)[i].addinfo = malloc (strlen(addinfo)+1)))
2719 strcpy ((*dst_list)[i].addinfo, addinfo);
2722 (*dst_list)[i].addinfo = NULL;
2723 (*dst_list)[i].condition = 0;
2728 static void ir_handleRecords (void *o, Z_Records *zrs)
2731 IrTcl_SetObj *setobj = p->set_child;
2734 IrTcl_RecordList *rl;
2736 setobj->which = zrs->which;
2737 setobj->recordFlag = 1;
2739 ir_deleteDiags (&setobj->nonSurrogateDiagnosticList,
2740 &setobj->nonSurrogateDiagnosticNum);
2741 if (zrs->which == Z_Records_DBOSD)
2743 setobj->numberOfRecordsReturned =
2744 zrs->u.databaseOrSurDiagnostics->num_records;
2745 logf (LOG_DEBUG, "Got %d records", setobj->numberOfRecordsReturned);
2746 for (offset = 0; offset<setobj->numberOfRecordsReturned; offset++)
2748 rl = new_IR_record (setobj, setobj->start + offset,
2749 zrs->u.databaseOrSurDiagnostics->
2750 records[offset]->which);
2751 if (rl->which == Z_NamePlusRecord_surrogateDiagnostic)
2753 ir_handleDiags (&rl->u.surrogateDiagnostics.list,
2754 &rl->u.surrogateDiagnostics.num,
2755 &zrs->u.databaseOrSurDiagnostics->
2756 records[offset]->u.surrogateDiagnostic,
2761 Z_DatabaseRecord *zr;
2763 struct oident *ident;
2765 zr = zrs->u.databaseOrSurDiagnostics->records[offset]
2767 oe = (Odr_external*) zr;
2768 rl->u.dbrec.size = zr->u.octet_aligned->len;
2770 rl->u.dbrec.type = VAL_USMARC;
2771 ident = oid_getentbyoid (oe->direct_reference);
2772 rl->u.dbrec.type = ident->value;
2774 if (oe->which == ODR_EXTERNAL_octet && rl->u.dbrec.size > 0)
2776 char *buf = (char*) zr->u.octet_aligned->buf;
2777 if ((rl->u.dbrec.buf = malloc (rl->u.dbrec.size)))
2778 memcpy (rl->u.dbrec.buf, buf, rl->u.dbrec.size);
2780 else if (rl->u.dbrec.type == VAL_SUTRS &&
2781 oe->which == ODR_EXTERNAL_single)
2785 logf (LOG_DEBUG, "Decoding SUTRS");
2786 odr_setbuf (p->odr_in, (char*) oe->u.single_ASN1_type->buf,
2787 oe->u.single_ASN1_type->len, 0);
2788 if (!z_SUTRS(p->odr_in, &rc, 0))
2790 logf (LOG_WARN, "Cannot decode SUTRS");
2791 rl->u.dbrec.buf = NULL;
2795 if ((rl->u.dbrec.buf = malloc (rc->len+1)))
2797 memcpy (rl->u.dbrec.buf, rc->buf, rc->len);
2798 rl->u.dbrec.buf[rc->len] = '\0';
2800 rl->u.dbrec.size = rc->len;
2804 rl->u.dbrec.buf = NULL;
2808 else if (zrs->which == Z_Records_multipleNSD)
2810 logf (LOG_DEBUG, "multipleNonSurrogateDiagnostic %d",
2811 zrs->u.multipleNonSurDiagnostics->num_diagRecs);
2812 setobj->numberOfRecordsReturned = 0;
2813 ir_handleDiags (&setobj->nonSurrogateDiagnosticList,
2814 &setobj->nonSurrogateDiagnosticNum,
2815 zrs->u.multipleNonSurDiagnostics->diagRecs,
2816 zrs->u.multipleNonSurDiagnostics->num_diagRecs);
2820 logf (LOG_DEBUG, "NonSurrogateDiagnostic");
2821 setobj->numberOfRecordsReturned = 0;
2822 ir_handleDiags (&setobj->nonSurrogateDiagnosticList,
2823 &setobj->nonSurrogateDiagnosticNum,
2824 &zrs->u.nonSurrogateDiagnostic,
2829 static void ir_searchResponse (void *o, Z_SearchResponse *searchrs)
2832 IrTcl_SetObj *setobj = p->set_child;
2833 Z_Records *zrs = searchrs->records;
2835 logf (LOG_DEBUG, "Received search response");
2838 logf (LOG_DEBUG, "Search response, no object!");
2841 setobj->searchStatus = searchrs->searchStatus ? 1 : 0;
2842 get_referenceId (&setobj->set_inher.referenceId, searchrs->referenceId);
2843 setobj->resultCount = *searchrs->resultCount;
2844 if (searchrs->presentStatus)
2845 setobj->presentStatus = *searchrs->presentStatus;
2846 if (searchrs->nextResultSetPosition)
2847 setobj->nextResultSetPosition = *searchrs->nextResultSetPosition;
2849 logf (LOG_DEBUG, "Search response %d, %d hits",
2850 setobj->searchStatus, setobj->resultCount);
2852 ir_handleRecords (o, zrs);
2854 setobj->recordFlag = 0;
2858 static void ir_presentResponse (void *o, Z_PresentResponse *presrs)
2861 IrTcl_SetObj *setobj = p->set_child;
2862 Z_Records *zrs = presrs->records;
2864 logf (LOG_DEBUG, "Received present response");
2867 logf (LOG_DEBUG, "Present response, no object!");
2870 setobj->presentStatus = *presrs->presentStatus;
2871 get_referenceId (&setobj->set_inher.referenceId, presrs->referenceId);
2872 setobj->nextResultSetPosition = *presrs->nextResultSetPosition;
2874 ir_handleRecords (o, zrs);
2877 setobj->recordFlag = 0;
2878 logf (LOG_DEBUG, "No records!");
2882 static void ir_scanResponse (void *o, Z_ScanResponse *scanrs)
2885 IrTcl_ScanObj *scanobj = p->scan_child;
2887 logf (LOG_DEBUG, "Received scanResponse");
2889 get_referenceId (&p->set_inher.referenceId, scanrs->referenceId);
2890 scanobj->scanStatus = *scanrs->scanStatus;
2891 logf (LOG_DEBUG, "scanStatus=%d", scanobj->scanStatus);
2893 if (scanrs->stepSize)
2894 scanobj->stepSize = *scanrs->stepSize;
2895 logf (LOG_DEBUG, "stepSize=%d", scanobj->stepSize);
2897 scanobj->numberOfEntriesReturned = *scanrs->numberOfEntriesReturned;
2898 logf (LOG_DEBUG, "numberOfEntriesReturned=%d",
2899 scanobj->numberOfEntriesReturned);
2901 if (scanrs->positionOfTerm)
2902 scanobj->positionOfTerm = *scanrs->positionOfTerm;
2904 scanobj->positionOfTerm = -1;
2905 logf (LOG_DEBUG, "positionOfTerm=%d", scanobj->positionOfTerm);
2907 free (scanobj->entries);
2908 scanobj->entries = NULL;
2910 ir_deleteDiags (&scanobj->nonSurrogateDiagnosticList,
2911 &scanobj->nonSurrogateDiagnosticNum);
2912 if (scanrs->entries)
2917 scanobj->entries_flag = 1;
2918 scanobj->which = scanrs->entries->which;
2919 switch (scanobj->which)
2921 case Z_ListEntries_entries:
2922 scanobj->num_entries = scanrs->entries->u.entries->num_entries;
2923 scanobj->entries = malloc (scanobj->num_entries *
2924 sizeof(*scanobj->entries));
2925 for (i=0; i<scanobj->num_entries; i++)
2927 ze = scanrs->entries->u.entries->entries[i];
2928 scanobj->entries[i].which = ze->which;
2931 case Z_Entry_termInfo:
2932 if (ze->u.termInfo->term->which == Z_Term_general)
2934 int l = ze->u.termInfo->term->u.general->len;
2935 scanobj->entries[i].u.term.buf = malloc (1+l);
2936 memcpy (scanobj->entries[i].u.term.buf,
2937 ze->u.termInfo->term->u.general->buf,
2939 scanobj->entries[i].u.term.buf[l] = '\0';
2942 scanobj->entries[i].u.term.buf = NULL;
2943 if (ze->u.termInfo->globalOccurrences)
2944 scanobj->entries[i].u.term.globalOccurrences =
2945 *ze->u.termInfo->globalOccurrences;
2947 scanobj->entries[i].u.term.globalOccurrences = 0;
2949 case Z_Entry_surrogateDiagnostic:
2950 ir_handleDiags (&scanobj->entries[i].u.diag.list,
2951 &scanobj->entries[i].u.diag.num,
2952 &ze->u.surrogateDiagnostic,
2958 case Z_ListEntries_nonSurrogateDiagnostics:
2959 ir_handleDiags (&scanobj->nonSurrogateDiagnosticList,
2960 &scanobj->nonSurrogateDiagnosticNum,
2961 scanrs->entries->u.nonSurrogateDiagnostics->
2963 scanrs->entries->u.nonSurrogateDiagnostics->
2969 scanobj->entries_flag = 0;
2973 * ir_select_read: handle incoming packages
2975 void ir_select_read (ClientData clientData)
2977 IrTcl_Obj *p = clientData;
2983 r = cs_rcvconnect (p->cs_link);
2986 logf (LOG_WARN, "cs_rcvconnect returned 1");
2990 ir_select_remove_write (cs_fileno (p->cs_link), p);
2993 logf (LOG_DEBUG, "cs_rcvconnect error");
2996 p->failInfo = IR_TCL_FAIL_CONNECT;
2997 IrTcl_eval (p->interp, p->failback);
2999 do_disconnect (p, NULL, 2, NULL);
3003 IrTcl_eval (p->interp, p->callback);
3008 /* signal one more use of ir object - callbacks must not
3009 release the ir memory (p pointer) */
3011 if ((r=cs_get (p->cs_link, &p->buf_in, &p->len_in)) <= 0)
3013 logf (LOG_DEBUG, "cs_get failed, code %d", r);
3014 ir_select_remove (cs_fileno (p->cs_link), p);
3017 p->failInfo = IR_TCL_FAIL_READ;
3018 IrTcl_eval (p->interp, p->failback);
3020 do_disconnect (p, NULL, 2, NULL);
3022 /* relase ir object now if callback deleted it */
3028 odr_setbuf (p->odr_in, p->buf_in, r, 0);
3029 logf (LOG_DEBUG, "cs_get ok, got %d", r);
3030 if (!z_APDU (p->odr_in, &apdu, 0))
3032 logf (LOG_DEBUG, "%s", odr_errlist [odr_geterror (p->odr_in)]);
3035 p->failInfo = IR_TCL_FAIL_IN_APDU;
3036 IrTcl_eval (p->interp, p->failback);
3038 do_disconnect (p, NULL, 2, NULL);
3040 /* release ir object now if failback deleted it */
3046 case Z_APDU_initResponse:
3047 ir_initResponse (p, apdu->u.initResponse);
3049 case Z_APDU_searchResponse:
3050 ir_searchResponse (p, apdu->u.searchResponse);
3052 case Z_APDU_presentResponse:
3053 ir_presentResponse (p, apdu->u.presentResponse);
3055 case Z_APDU_scanResponse:
3056 ir_scanResponse (p, apdu->u.scanResponse);
3059 logf (LOG_WARN, "Received unknown APDU type (%d)", apdu->which);
3062 p->failInfo = IR_TCL_FAIL_UNKNOWN_APDU;
3063 IrTcl_eval (p->interp, p->failback);
3065 do_disconnect (p, NULL, 2, NULL);
3067 odr_reset (p->odr_in);
3069 IrTcl_eval (p->interp, p->callback);
3070 if (p->ref_count == 1)
3076 } while (p->cs_link && cs_more (p->cs_link));
3080 * ir_select_write: handle outgoing packages - not yet written.
3082 void ir_select_write (ClientData clientData)
3084 IrTcl_Obj *p = clientData;
3087 logf (LOG_DEBUG, "In write handler");
3090 r = cs_rcvconnect (p->cs_link);
3096 logf (LOG_DEBUG, "cs_rcvconnect error");
3097 ir_select_remove_write (cs_fileno (p->cs_link), p);
3100 p->failInfo = IR_TCL_FAIL_CONNECT;
3101 IrTcl_eval (p->interp, p->failback);
3103 do_disconnect (p, NULL, 2, NULL);
3106 ir_select_remove_write (cs_fileno (p->cs_link), p);
3108 IrTcl_eval (p->interp, p->callback);
3111 if ((r=cs_put (p->cs_link, p->sbuf, p->slen)) < 0)
3113 logf (LOG_DEBUG, "select write fail");
3116 p->failInfo = IR_TCL_FAIL_WRITE;
3117 IrTcl_eval (p->interp, p->failback);
3119 do_disconnect (p, NULL, 2, NULL);
3121 else if (r == 0) /* remove select bit */
3123 ir_select_remove_write (cs_fileno (p->cs_link), p);
3127 /* ------------------------------------------------------- */
3130 * ir_tcl_init: Registration of TCL commands.
3132 int ir_tcl_init (Tcl_Interp *interp)
3134 Tcl_CreateCommand (interp, "ir", ir_obj_mk, (ClientData) NULL,
3135 (Tcl_CmdDeleteProc *) NULL);
3136 Tcl_CreateCommand (interp, "ir-set", ir_set_obj_mk,
3137 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
3138 Tcl_CreateCommand (interp, "ir-scan", ir_scan_obj_mk,
3139 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
3140 irTcl_interp = interp;