2 * IR toolkit for tcl/tk
4 * See the file LICENSE for details.
5 * Sebastian Hammer, Adam Dickmeiss
8 * Revision 1.49 1995-06-30 12:39:21 adam
9 * Bug fix: loadFile didn't set record type.
10 * The MARC routines are a little less strict in the interpretation.
11 * Script display.tcl replaces the old marc.tcl.
12 * New interactive script: shell.tcl.
14 * Revision 1.48 1995/06/27 19:03:50 adam
15 * Bug fix in do_present in ir-tcl.c: p->set_child member weren't set.
16 * nextResultSetPosition used instead of setOffset.
18 * Revision 1.47 1995/06/25 10:25:04 adam
19 * Working on triggerResourceControl. Description of compile/install
20 * procedure moved to ir-tcl.sgml.
22 * Revision 1.46 1995/06/22 13:15:06 adam
23 * Feature: SUTRS. Setting getSutrs implemented.
24 * Work on display formats.
25 * Preferred record syntax can be set by the user.
27 * Revision 1.45 1995/06/20 08:07:30 adam
28 * New setting: failInfo.
29 * Working on better cancel mechanism.
31 * Revision 1.44 1995/06/19 17:01:20 adam
34 * Revision 1.43 1995/06/19 13:06:08 adam
35 * New define: IR_TCL_VERSION.
37 * Revision 1.42 1995/06/19 08:08:52 adam
38 * client.tcl: hotTargets now contain both database and target name.
39 * ir-tcl.c: setting protocol edited. Errors in callbacks are logged
40 * by logf(LOG_WARN, ...) calls.
42 * Revision 1.41 1995/06/16 12:28:16 adam
43 * Implemented preferredRecordSyntax.
44 * Minor changes in diagnostic handling.
45 * Record list deleted when connection closes.
47 * Revision 1.40 1995/06/14 13:37:18 adam
48 * Setting recordType implemented.
49 * Setting implementationVersion implemented.
50 * Settings implementationId / implementationName edited.
52 * Revision 1.39 1995/06/08 10:26:32 adam
53 * Bug fix in ir_strdup.
55 * Revision 1.38 1995/06/01 16:36:47 adam
56 * About buttons. Minor bug fixes.
58 * Revision 1.37 1995/06/01 07:31:20 adam
59 * Rename of many typedefs -> IrTcl_...
61 * Revision 1.36 1995/05/31 13:09:59 adam
62 * Client searches/presents may be interrupted.
63 * New moving book-logo.
65 * Revision 1.35 1995/05/31 08:36:33 adam
66 * Bug fix in client.tcl: didn't save options on clientrc.tcl.
67 * New method: referenceId. More work on scan.
69 * Revision 1.34 1995/05/29 10:33:42 adam
70 * README and rename of startup script.
72 * Revision 1.33 1995/05/29 09:15:11 quinn
73 * Changed CS_SR to PROTO_SR, etc.
75 * Revision 1.32 1995/05/29 08:44:16 adam
76 * Work on delete of objects.
78 * Revision 1.31 1995/05/26 11:44:10 adam
79 * Bugs fixed. More work on MARC utilities and queries. Test
80 * client is up-to-date again.
82 * Revision 1.30 1995/05/26 08:54:11 adam
83 * New MARC utilities. Uses prefix query.
85 * Revision 1.29 1995/05/24 14:10:22 adam
86 * Work on idAuthentication, protocolVersion and options.
88 * Revision 1.28 1995/05/23 15:34:48 adam
89 * Many new settings, userInformationField, smallSetUpperBound, etc.
90 * A number of settings are inherited when ir-set is executed.
91 * This version is incompatible with the graphical test client (client.tcl).
93 * Revision 1.27 1995/05/11 15:34:47 adam
94 * Scan request changed a bit. This version works with RLG.
96 * Revision 1.26 1995/04/18 16:11:51 adam
97 * First version of graphical Scan. Some work on query-by-form.
99 * Revision 1.25 1995/04/17 09:37:17 adam
100 * Further development of scan.
102 * Revision 1.24 1995/04/11 14:16:42 adam
103 * Further work on scan. Response works. Entries aren't saved yet.
105 * Revision 1.23 1995/04/10 10:50:27 adam
106 * Result-set name defaults to suffix of ir-set name.
107 * Started working on scan. Not finished at this point.
109 * Revision 1.22 1995/03/31 10:43:03 adam
110 * More robust when getting bad MARC records.
112 * Revision 1.21 1995/03/31 08:56:37 adam
113 * New button "Search".
115 * Revision 1.20 1995/03/29 16:07:09 adam
116 * Bug fix: Didn't use setName in present request.
118 * Revision 1.19 1995/03/28 12:45:23 adam
119 * New ir method failback: called on disconnect/protocol error.
120 * New ir set/get method: protocol: SR / Z3950.
121 * Simple popup and disconnect when failback is invoked.
123 * Revision 1.18 1995/03/21 15:50:12 adam
126 * Revision 1.17 1995/03/21 13:41:03 adam
127 * Comstack cs_create not used too often. Non-blocking connect.
129 * Revision 1.16 1995/03/21 08:26:06 adam
130 * New method, setName, to specify the result set name (other than Default).
131 * New method, responseStatus, which returns diagnostic info, if any, after
132 * present response / search response.
134 * Revision 1.15 1995/03/20 15:24:07 adam
135 * Diagnostic records saved on searchResponse.
137 * Revision 1.14 1995/03/20 08:53:22 adam
138 * Event loop in tclmain.c rewritten. New method searchStatus.
140 * Revision 1.13 1995/03/17 18:26:17 adam
141 * Non-blocking i/o used now. Database names popup as cascade items.
143 * Revision 1.12 1995/03/17 15:45:00 adam
144 * Improved target/database setup.
146 * Revision 1.11 1995/03/16 17:54:03 adam
147 * Minor changes really.
149 * Revision 1.10 1995/03/15 16:14:50 adam
150 * Blocking arg in cs_create changed.
152 * Revision 1.9 1995/03/15 13:59:24 adam
155 * Revision 1.8 1995/03/15 08:25:16 adam
156 * New method presentStatus to check for error on present. Misc. cleanup
157 * of IrTcl_RecordList manipulations. Full MARC record presentation in
160 * Revision 1.7 1995/03/14 17:32:29 adam
161 * Presentation of full Marc record in popup window.
163 * Revision 1.6 1995/03/12 19:31:55 adam
164 * Pattern matching implemented when retrieving MARC records. More
165 * diagnostic functions.
167 * Revision 1.5 1995/03/10 18:00:15 adam
168 * Actual presentation in line-by-line format. RPN query support.
170 * Revision 1.4 1995/03/09 16:15:08 adam
171 * First presentRequest attempts. Hot-target list.
177 #include <sys/time.h>
187 int (*method) (void *obj, Tcl_Interp *interp, int argc, char **argv);
195 static Tcl_Interp *irTcl_interp;
197 static void ir_deleteDiags (IrTcl_Diagnostic **dst_list, int *dst_num);
198 static int do_disconnect (void *obj, Tcl_Interp *interp,
199 int argc, char **argv);
201 static IrTcl_RecordList *new_IR_record (IrTcl_SetObj *setobj,
204 IrTcl_RecordList *rl;
206 for (rl = setobj->record_list; rl; rl = rl->next)
212 case Z_NamePlusRecord_databaseRecord:
213 free (rl->u.dbrec.buf);
214 rl->u.dbrec.buf = NULL;
216 case Z_NamePlusRecord_surrogateDiagnostic:
217 ir_deleteDiags (&rl->u.surrogateDiagnostics.list,
218 &rl->u.surrogateDiagnostics.num);
226 rl = malloc (sizeof(*rl));
228 rl->next = setobj->record_list;
230 setobj->record_list = rl;
237 enum oid_value value;
239 } IrTcl_recordSyntaxTab[] = {
240 { VAL_UNIMARC, "UNIMARC" },
241 { VAL_INTERMARC, "INTERMARC" },
243 { VAL_USMARC, "USMARC" },
244 { VAL_UKMARC, "UKMARC" },
245 { VAL_NORMARC, "NORMARC" },
246 { VAL_LIBRISMARC, "LIBRISMARC" },
247 { VAL_DANMARC, "DANMARC" },
248 { VAL_FINMARC, "FINMARC" },
250 { VAL_CANMARC, "CANMARC" },
252 { VAL_PICAMARC, "PICAMARC" },
253 { VAL_AUSMARC, "AUSMARC" },
254 { VAL_IBERMARC, "IBERMARC" },
255 { VAL_SUTRS, "SUTRS" },
262 int IrTcl_eval (Tcl_Interp *interp, const char *command)
264 char *tmp = malloc (strlen(command)+1);
269 logf (LOG_FATAL, "Out of memory in IrTcl_eval");
272 strcpy (tmp, command);
273 r = Tcl_Eval (interp, tmp);
275 logf (LOG_WARN, "Tcl error in line %d: %s", interp->errorLine,
277 Tcl_FreeResult (interp);
283 * IrTcl_getRecordSyntaxStr: Return record syntax name of object id
285 static const char *IrTcl_getRecordSyntaxStr (enum oid_value value)
288 for (i = 0; IrTcl_recordSyntaxTab[i].name; i++)
289 if (IrTcl_recordSyntaxTab[i].value == value)
290 return IrTcl_recordSyntaxTab[i].name;
295 * IrTcl_getRecordSyntaxVal: Return record syntax value of string
297 static enum oid_value IrTcl_getRecordSyntaxVal (const char *name)
300 for (i = 0; IrTcl_recordSyntaxTab[i].name; i++)
301 if (!strcmp (IrTcl_recordSyntaxTab[i].name, name))
302 return IrTcl_recordSyntaxTab[i].value;
306 static IrTcl_RecordList *find_IR_record (IrTcl_SetObj *setobj, int no)
308 IrTcl_RecordList *rl;
310 for (rl = setobj->record_list; rl; rl = rl->next)
316 static void delete_IR_records (IrTcl_SetObj *setobj)
318 IrTcl_RecordList *rl, *rl1;
320 for (rl = setobj->record_list; rl; rl = rl1)
324 case Z_NamePlusRecord_databaseRecord:
325 free (rl->u.dbrec.buf);
327 case Z_NamePlusRecord_surrogateDiagnostic:
328 ir_deleteDiags (&rl->u.surrogateDiagnostics.list,
329 &rl->u.surrogateDiagnostics.num);
335 setobj->record_list = NULL;
339 * get_set_int: Set/get integer value
341 static int get_set_int (int *val, Tcl_Interp *interp, int argc, char **argv)
347 if (Tcl_GetInt (interp, argv[2], val)==TCL_ERROR)
350 sprintf (buf, "%d", *val);
351 Tcl_AppendResult (interp, buf, NULL);
356 * ir_method: Search for method in table and invoke method handler
358 int ir_method (Tcl_Interp *interp, int argc, char **argv, IrTcl_Methods *tab)
360 IrTcl_Methods *tab_i = tab;
363 for (tab_i = tab; tab_i->tab; tab_i++)
364 for (t = tab_i->tab; t->name; t++)
367 if ((*t->method)(tab_i->obj, interp, argc, argv) == TCL_ERROR)
371 if (!strcmp (t->name, argv[1]))
372 return (*t->method)(tab_i->obj, interp, argc, argv);
376 Tcl_AppendResult (interp, "Bad method: ", argv[1],
377 ". Possible methods:", NULL);
378 for (tab_i = tab; tab_i->tab; tab_i++)
379 for (t = tab_i->tab; t->name; t++)
380 Tcl_AppendResult (interp, " ", t->name, NULL);
385 * ir_method_r: Get status for all readable elements
387 int ir_method_r (void *obj, Tcl_Interp *interp, int argc, char **argv,
395 for (; tab->name; tab++)
398 argv_n[1] = tab->name;
399 Tcl_AppendResult (interp, "{", NULL);
400 (*tab->method)(obj, interp, argc_n, argv_n);
401 Tcl_AppendResult (interp, "} ", NULL);
407 * ir_named_bits: get/set named bits
409 int ir_named_bits (struct ir_named_entry *tab, Odr_bitmask *ob,
410 Tcl_Interp *interp, int argc, char **argv)
412 struct ir_named_entry *ti;
417 for (no = 0; no < argc; no++)
419 for (ti = tab; ti->name; ti++)
420 if (!strcmp (argv[no], ti->name))
422 ODR_MASK_SET (ob, ti->pos);
427 Tcl_AppendResult (interp, "Bad bit mask: ", argv[no], NULL);
433 for (ti = tab; ti->name; ti++)
434 if (ODR_MASK_GET (ob, ti->pos))
435 Tcl_AppendElement (interp, ti->name);
440 * ir_strdup: Duplicate string
442 int ir_strdup (Tcl_Interp *interp, char** p, const char *s)
449 *p = malloc (strlen(s)+1);
452 interp->result = "strdup fail";
460 * ir_strdel: Delete string
462 int ir_strdel (Tcl_Interp *interp, char **p)
470 * ir_malloc: Malloc function
472 void *ir_malloc (Tcl_Interp *interp, size_t size)
474 static char buf[128];
475 void *p = malloc (size);
479 sprintf (buf, "Malloc fail. %ld bytes requested", (long) size);
480 interp->result = buf;
486 static void set_referenceId (ODR o, Z_ReferenceId **dst, const char *src)
492 *dst = odr_malloc (o, sizeof(**dst));
493 (*dst)->size = (*dst)->len = strlen(src);
494 (*dst)->buf = odr_malloc (o, (*dst)->len);
495 memcpy ((*dst)->buf, src, (*dst)->len);
499 static void get_referenceId (char **dst, Z_ReferenceId *src)
507 *dst = malloc (src->len+1);
508 memcpy (*dst, src->buf, src->len);
509 (*dst)[src->len] = '\0';
512 /* ------------------------------------------------------- */
515 * do_init_request: init method on IR object
517 static int do_init_request (void *obj, Tcl_Interp *interp,
518 int argc, char **argv)
529 interp->result = "not connected";
532 odr_reset (p->odr_out);
533 apdu = zget_APDU (p->odr_out, Z_APDU_initRequest);
534 req = apdu->u.initRequest;
536 set_referenceId (p->odr_out, &req->referenceId, p->set_inher.referenceId);
537 req->options = &p->options;
538 req->protocolVersion = &p->protocolVersion;
539 req->preferredMessageSize = &p->preferredMessageSize;
540 req->maximumRecordSize = &p->maximumRecordSize;
542 if (p->idAuthenticationGroupId)
544 Z_IdPass *pass = odr_malloc (p->odr_out, sizeof(*pass));
545 Z_IdAuthentication *auth = odr_malloc (p->odr_out, sizeof(*auth));
547 auth->which = Z_IdAuthentication_idPass;
548 auth->u.idPass = pass;
549 if (p->idAuthenticationGroupId && *p->idAuthenticationGroupId)
550 pass->groupId = p->idAuthenticationGroupId;
552 pass->groupId = NULL;
553 if (p->idAuthenticationUserId && *p->idAuthenticationUserId)
554 pass->userId = p->idAuthenticationUserId;
557 if (p->idAuthenticationPassword && *p->idAuthenticationPassword)
558 pass->password = p->idAuthenticationPassword;
560 pass->password = NULL;
561 req->idAuthentication = auth;
563 else if (!p->idAuthenticationOpen || !*p->idAuthenticationOpen)
564 req->idAuthentication = NULL;
567 Z_IdAuthentication *auth = odr_malloc (p->odr_out, sizeof(*auth));
569 auth->which = Z_IdAuthentication_open;
570 auth->u.open = p->idAuthenticationOpen;
571 req->idAuthentication = auth;
573 req->implementationId = p->implementationId;
574 req->implementationName = p->implementationName;
575 req->implementationVersion = p->implementationVersion;
576 req->userInformationField = 0;
578 if (!z_APDU (p->odr_out, &apdu, 0))
580 Tcl_AppendResult (interp, odr_errlist [odr_geterror (p->odr_out)],
582 odr_reset (p->odr_out);
585 p->sbuf = odr_getbuf (p->odr_out, &p->slen, NULL);
586 if ((r=cs_put (p->cs_link, p->sbuf, p->slen)) < 0)
588 interp->result = "cs_put failed in init";
589 do_disconnect (p, NULL, 2, NULL);
594 ir_select_add_write (cs_fileno(p->cs_link), p);
595 logf (LOG_DEBUG, "Sent part of initializeRequest (%d bytes)", p->slen);
598 logf (LOG_DEBUG, "Sent whole initializeRequest (%d bytes)", p->slen);
603 * do_protocolVersion: Set protocol Version
605 static int do_protocolVersion (void *obj, Tcl_Interp *interp,
606 int argc, char **argv)
614 ODR_MASK_ZERO (&p->protocolVersion);
615 ODR_MASK_SET (&p->protocolVersion, 0);
616 ODR_MASK_SET (&p->protocolVersion, 1);
621 if (Tcl_GetInt (interp, argv[2], &version)==TCL_ERROR)
623 ODR_MASK_ZERO (&p->protocolVersion);
624 for (i = 0; i<version; i++)
625 ODR_MASK_SET (&p->protocolVersion, i);
627 for (i = 4; --i >= 0; )
628 if (ODR_MASK_GET (&p->protocolVersion, i))
630 sprintf (buf, "%d", i+1);
631 interp->result = buf;
636 * do_options: Set options
638 static int do_options (void *obj, Tcl_Interp *interp,
639 int argc, char **argv)
641 static struct ir_named_entry options_tab[] = {
645 { "resourceReport", 3 },
646 { "triggerResourceCtrl", 4},
647 { "resourceCtrl", 5},
651 { "extendedServices", 10},
652 { "level-1Segmentation", 11},
653 { "level-2Segmentation", 12},
654 { "concurrentOperations", 13},
655 { "namedResultSets", 14},
662 ODR_MASK_ZERO (&p->options);
663 ODR_MASK_SET (&p->options, 0);
664 ODR_MASK_SET (&p->options, 1);
665 ODR_MASK_SET (&p->options, 7);
666 ODR_MASK_SET (&p->options, 14);
669 return ir_named_bits (options_tab, &p->options, interp, argc-2, argv+2);
673 * do_failInfo: Get fail information
675 static int do_failInfo (void *obj, Tcl_Interp *interp, int argc, char **argv)
685 sprintf (buf, "%d", p->failInfo);
691 case IR_TCL_FAIL_CONNECT:
692 cp = "connect failed";
694 case IR_TCL_FAIL_READ:
695 cp = "connection closed";
697 case IR_TCL_FAIL_WRITE:
698 cp = "connection closed";
700 case IR_TCL_FAIL_IN_APDU:
701 cp = "failed to decode incoming APDU";
703 case IR_TCL_FAIL_UNKNOWN_APDU:
709 Tcl_AppendElement (interp, buf);
710 Tcl_AppendElement (interp, cp);
715 * do_preferredMessageSize: Set/get preferred message size
717 static int do_preferredMessageSize (void *obj, Tcl_Interp *interp,
718 int argc, char **argv)
724 p->preferredMessageSize = 30000;
727 return get_set_int (&p->preferredMessageSize, interp, argc, argv);
731 * do_maximumRecordSize: Set/get maximum record size
733 static int do_maximumRecordSize (void *obj, Tcl_Interp *interp,
734 int argc, char **argv)
740 p->maximumRecordSize = 30000;
743 return get_set_int (&p->maximumRecordSize, interp, argc, argv);
747 * do_initResult: Get init result
749 static int do_initResult (void *obj, Tcl_Interp *interp,
750 int argc, char **argv)
756 return get_set_int (&p->initResult, interp, argc, argv);
761 * do_implementationName: Set/get Implementation Name.
763 static int do_implementationName (void *obj, Tcl_Interp *interp,
764 int argc, char **argv)
769 return ir_strdup (interp, &p->implementationName,
770 "Index Data/IrTcl on YAZ");
772 return ir_strdel (interp, &p->implementationName);
775 free (p->implementationName);
776 if (ir_strdup (interp, &p->implementationName, argv[2])
780 Tcl_AppendResult (interp, p->implementationName, (char*) NULL);
785 * do_implementationId: Get Implementation Id.
787 static int do_implementationId (void *obj, Tcl_Interp *interp,
788 int argc, char **argv)
793 return ir_strdup (interp, &p->implementationId, "YAZ (id=81)");
795 return ir_strdel (interp, &p->implementationId);
796 Tcl_AppendResult (interp, p->implementationId, (char*) NULL);
801 * do_implementationVersion: get Implementation Version.
803 static int do_implementationVersion (void *obj, Tcl_Interp *interp,
804 int argc, char **argv)
809 return ir_strdup (interp, &p->implementationVersion,
810 "YAZ: " YAZ_VERSION " / IrTcl: " IR_TCL_VERSION);
812 return ir_strdel (interp, &p->implementationVersion);
813 Tcl_AppendResult (interp, p->implementationVersion, (char*) NULL);
818 * do_targetImplementationName: Get Implementation Name of target.
820 static int do_targetImplementationName (void *obj, Tcl_Interp *interp,
821 int argc, char **argv)
827 p->targetImplementationName = NULL;
831 return ir_strdel (interp, &p->targetImplementationName);
832 Tcl_AppendResult (interp, p->targetImplementationName, (char*) NULL);
837 * do_targetImplementationId: Get Implementation Id of target
839 static int do_targetImplementationId (void *obj, Tcl_Interp *interp,
840 int argc, char **argv)
846 p->targetImplementationId = NULL;
850 return ir_strdel (interp, &p->targetImplementationId);
851 Tcl_AppendResult (interp, p->targetImplementationId, (char*) NULL);
856 * do_targetImplementationVersion: Get Implementation Version of target
858 static int do_targetImplementationVersion (void *obj, Tcl_Interp *interp,
859 int argc, char **argv)
865 p->targetImplementationVersion = NULL;
869 return ir_strdel (interp, &p->targetImplementationVersion);
870 Tcl_AppendResult (interp, p->targetImplementationVersion, (char*) NULL);
875 * do_idAuthentication: Set/get id Authentication
877 static int do_idAuthentication (void *obj, Tcl_Interp *interp,
878 int argc, char **argv)
882 if (argc >= 3 || argc == -1)
884 free (p->idAuthenticationOpen);
885 free (p->idAuthenticationGroupId);
886 free (p->idAuthenticationUserId);
887 free (p->idAuthenticationPassword);
889 if (argc >= 3 || argc <= 0)
891 p->idAuthenticationOpen = NULL;
892 p->idAuthenticationGroupId = NULL;
893 p->idAuthenticationUserId = NULL;
894 p->idAuthenticationPassword = NULL;
902 if (ir_strdup (interp, &p->idAuthenticationOpen, argv[2])
908 if (ir_strdup (interp, &p->idAuthenticationGroupId, argv[2])
911 if (ir_strdup (interp, &p->idAuthenticationUserId, argv[3])
914 if (ir_strdup (interp, &p->idAuthenticationPassword, argv[4])
919 if (p->idAuthenticationOpen)
920 Tcl_AppendElement (interp, p->idAuthenticationOpen);
921 else if (p->idAuthenticationGroupId)
923 Tcl_AppendElement (interp, p->idAuthenticationGroupId);
924 Tcl_AppendElement (interp, p->idAuthenticationUserId);
925 Tcl_AppendElement (interp, p->idAuthenticationPassword);
931 * do_connect: connect method on IR object
933 static int do_connect (void *obj, Tcl_Interp *interp,
934 int argc, char **argv)
946 interp->result = "already connected";
949 if (!strcmp (p->cs_type, "tcpip"))
951 p->cs_link = cs_create (tcpip_type, CS_BLOCK, p->protocol_type);
952 addr = tcpip_strtoaddr (argv[2]);
955 interp->result = "tcpip_strtoaddr fail";
958 logf (LOG_DEBUG, "tcp/ip connect %s", argv[2]);
960 else if (!strcmp (p->cs_type, "mosi"))
963 p->cs_link = cs_create (mosi_type, CS_BLOCK, p->protocol_type);
964 addr = mosi_strtoaddr (argv[2]);
967 interp->result = "mosi_strtoaddr fail";
970 logf (LOG_DEBUG, "mosi connect %s", argv[2]);
972 interp->result = "MOSI support not there";
978 Tcl_AppendResult (interp, "Bad comstack type: ",
982 if (ir_strdup (interp, &p->hostname, argv[2]) == TCL_ERROR)
984 if ((r=cs_connect (p->cs_link, addr)) < 0)
986 interp->result = "connect fail";
987 do_disconnect (p, NULL, 2, NULL);
990 ir_select_add (cs_fileno (p->cs_link), p);
993 ir_select_add_write (cs_fileno (p->cs_link), p);
1000 IrTcl_eval (p->interp, p->callback);
1007 * do_disconnect: disconnect method on IR object
1009 static int do_disconnect (void *obj, Tcl_Interp *interp,
1010 int argc, char **argv)
1025 ir_select_remove_write (cs_fileno (p->cs_link), p);
1026 ir_select_remove (cs_fileno (p->cs_link), p);
1028 assert (p->cs_link);
1029 cs_close (p->cs_link);
1032 ODR_MASK_ZERO (&p->options);
1033 ODR_MASK_SET (&p->options, 0);
1034 ODR_MASK_SET (&p->options, 1);
1035 ODR_MASK_SET (&p->options, 7);
1036 ODR_MASK_SET (&p->options, 14);
1038 ODR_MASK_ZERO (&p->protocolVersion);
1039 ODR_MASK_SET (&p->protocolVersion, 0);
1040 ODR_MASK_SET (&p->protocolVersion, 1);
1042 assert (!p->cs_link);
1047 * do_comstack: Set/get comstack method on IR object
1049 static int do_comstack (void *o, Tcl_Interp *interp,
1050 int argc, char **argv)
1055 return ir_strdup (interp, &obj->cs_type, "tcpip");
1056 else if (argc == -1)
1057 return ir_strdel (interp, &obj->cs_type);
1060 free (obj->cs_type);
1061 if (ir_strdup (interp, &obj->cs_type, argv[2]) == TCL_ERROR)
1064 Tcl_AppendElement (interp, obj->cs_type);
1070 * do_callback: add callback
1072 static int do_callback (void *obj, Tcl_Interp *interp,
1073 int argc, char **argv)
1082 else if (argc == -1)
1083 return ir_strdel (interp, &p->callback);
1089 if (ir_strdup (interp, &p->callback, argv[2]) == TCL_ERROR)
1100 * do_failback: add error handle callback
1102 static int do_failback (void *obj, Tcl_Interp *interp,
1103 int argc, char **argv)
1112 else if (argc == -1)
1113 return ir_strdel (interp, &p->failback);
1119 if (ir_strdup (interp, &p->failback, argv[2]) == TCL_ERROR)
1130 * do_protocol: Set/get protocol method on IR object
1132 static int do_protocol (void *o, Tcl_Interp *interp, int argc, char **argv)
1138 p->protocol_type = PROTO_Z3950;
1143 if (!strcmp (argv[2], "Z39"))
1144 p->protocol_type = PROTO_Z3950;
1145 else if (!strcmp (argv[2], "SR"))
1146 p->protocol_type = PROTO_SR;
1149 Tcl_AppendResult (interp, "Bad protocol: ", argv[2], NULL);
1154 switch (p->protocol_type)
1157 Tcl_AppendElement (interp, "Z39");
1160 Tcl_AppendElement (interp, "SR");
1167 * do_triggerResourceControl:
1169 static int do_triggerResourceControl (void *obj, Tcl_Interp *interp,
1170 int argc, char **argv)
1174 Z_TriggerResourceControlRequest *req;
1181 interp->result = "not connected";
1184 apdu = zget_APDU (p->odr_out, Z_APDU_triggerResourceControlRequest);
1185 req = apdu->u.triggerResourceControlRequest;
1187 if (!z_APDU (p->odr_out, &apdu, 0))
1189 Tcl_AppendResult (interp, odr_errlist [odr_geterror (p->odr_out)],
1191 odr_reset (p->odr_out);
1194 p->sbuf = odr_getbuf (p->odr_out, &p->slen, NULL);
1195 if ((r=cs_put (p->cs_link, p->sbuf, p->slen)) < 0)
1197 interp->result = "cs_put failed in triggerResourceControl";
1198 do_disconnect (p, NULL, 2, NULL);
1203 ir_select_add_write (cs_fileno(p->cs_link), p);
1204 logf (LOG_DEBUG, "Sent part of triggerResourceControl (%d bytes)",
1208 logf (LOG_DEBUG, "Sent whole of triggerResourceControl (%d bytes)",
1214 * do_databaseNames: specify database names
1216 static int do_databaseNames (void *obj, Tcl_Interp *interp,
1217 int argc, char **argv)
1220 IrTcl_SetCObj *p = obj;
1224 for (i=0; i<p->num_databaseNames; i++)
1225 free (p->databaseNames[i]);
1226 free (p->databaseNames);
1230 p->num_databaseNames = 0;
1231 p->databaseNames = NULL;
1236 for (i=0; i<p->num_databaseNames; i++)
1237 Tcl_AppendElement (interp, p->databaseNames[i]);
1240 if (p->databaseNames)
1242 for (i=0; i<p->num_databaseNames; i++)
1243 free (p->databaseNames[i]);
1244 free (p->databaseNames);
1246 p->num_databaseNames = argc - 2;
1247 if (!(p->databaseNames = ir_malloc (interp,
1248 sizeof(*p->databaseNames) * p->num_databaseNames)))
1250 for (i=0; i<p->num_databaseNames; i++)
1252 if (ir_strdup (interp, &p->databaseNames[i], argv[2+i])
1260 * do_replaceIndicator: Set/get replace Set indicator
1262 static int do_replaceIndicator (void *obj, Tcl_Interp *interp,
1263 int argc, char **argv)
1265 IrTcl_SetCObj *p = obj;
1269 p->replaceIndicator = 1;
1272 return get_set_int (&p->replaceIndicator, interp, argc, argv);
1276 * do_queryType: Set/Get query method
1278 static int do_queryType (void *obj, Tcl_Interp *interp,
1279 int argc, char **argv)
1281 IrTcl_SetCObj *p = obj;
1284 return ir_strdup (interp, &p->queryType, "rpn");
1285 else if (argc == -1)
1286 return ir_strdel (interp, &p->queryType);
1289 free (p->queryType);
1290 if (ir_strdup (interp, &p->queryType, argv[2]) == TCL_ERROR)
1293 Tcl_AppendResult (interp, p->queryType, NULL);
1298 * do_userInformationField: Get User information field
1300 static int do_userInformationField (void *obj, Tcl_Interp *interp,
1301 int argc, char **argv)
1307 p->userInformationField = NULL;
1310 else if (argc == -1)
1311 return ir_strdel (interp, &p->userInformationField);
1312 Tcl_AppendResult (interp, p->userInformationField, NULL);
1317 * do_smallSetUpperBound: Set/get small set upper bound
1319 static int do_smallSetUpperBound (void *o, Tcl_Interp *interp,
1320 int argc, char **argv)
1322 IrTcl_SetCObj *p = o;
1326 p->smallSetUpperBound = 0;
1329 return get_set_int (&p->smallSetUpperBound, interp, argc, argv);
1333 * do_largeSetLowerBound: Set/get large set lower bound
1335 static int do_largeSetLowerBound (void *o, Tcl_Interp *interp,
1336 int argc, char **argv)
1338 IrTcl_SetCObj *p = o;
1342 p->largeSetLowerBound = 2;
1345 return get_set_int (&p->largeSetLowerBound, interp, argc, argv);
1349 * do_mediumSetPresentNumber: Set/get large set lower bound
1351 static int do_mediumSetPresentNumber (void *o, Tcl_Interp *interp,
1352 int argc, char **argv)
1354 IrTcl_SetCObj *p = o;
1358 p->mediumSetPresentNumber = 0;
1361 return get_set_int (&p->mediumSetPresentNumber, interp, argc, argv);
1365 * do_referenceId: Set/Get referenceId
1367 static int do_referenceId (void *obj, Tcl_Interp *interp,
1368 int argc, char **argv)
1370 IrTcl_SetCObj *p = obj;
1374 p->referenceId = NULL;
1377 else if (argc == -1)
1378 return ir_strdel (interp, &p->referenceId);
1381 free (p->referenceId);
1382 if (ir_strdup (interp, &p->referenceId, argv[2]) == TCL_ERROR)
1385 Tcl_AppendResult (interp, p->referenceId, NULL);
1390 * do_preferredRecordSyntax: Set/get preferred record syntax
1392 static int do_preferredRecordSyntax (void *obj, Tcl_Interp *interp,
1393 int argc, char **argv)
1395 IrTcl_SetCObj *p = obj;
1399 p->preferredRecordSyntax = NULL;
1402 else if (argc == -1)
1404 free (p->preferredRecordSyntax);
1405 p->preferredRecordSyntax = NULL;
1410 free (p->preferredRecordSyntax);
1411 p->preferredRecordSyntax = NULL;
1412 if (argv[2][0] && (p->preferredRecordSyntax =
1413 malloc (sizeof(*p->preferredRecordSyntax))))
1414 *p->preferredRecordSyntax = IrTcl_getRecordSyntaxVal (argv[2]);
1419 static IrTcl_Method ir_method_tab[] = {
1420 { 1, "comstack", do_comstack },
1421 { 1, "protocol", do_protocol },
1422 { 0, "failback", do_failback },
1423 { 0, "failInfo", do_failInfo },
1425 { 1, "connect", do_connect },
1426 { 0, "protocolVersion", do_protocolVersion },
1427 { 1, "preferredMessageSize", do_preferredMessageSize },
1428 { 1, "maximumRecordSize", do_maximumRecordSize },
1429 { 1, "implementationName", do_implementationName },
1430 { 1, "implementationId", do_implementationId },
1431 { 1, "implementationVersion", do_implementationVersion },
1432 { 0, "targetImplementationName", do_targetImplementationName },
1433 { 0, "targetImplementationId", do_targetImplementationId },
1434 { 0, "targetImplementationVersion", do_targetImplementationVersion },
1435 { 0, "userInformationField", do_userInformationField },
1436 { 1, "idAuthentication", do_idAuthentication },
1437 { 0, "options", do_options },
1438 { 0, "init", do_init_request },
1439 { 0, "initResult", do_initResult },
1440 { 0, "disconnect", do_disconnect },
1441 { 0, "callback", do_callback },
1442 { 0, "triggerResourceControl", do_triggerResourceControl },
1446 static IrTcl_Method ir_set_c_method_tab[] = {
1447 { 0, "databaseNames", do_databaseNames},
1448 { 0, "replaceIndicator", do_replaceIndicator},
1449 { 0, "queryType", do_queryType },
1450 { 0, "preferredRecordSyntax", do_preferredRecordSyntax },
1451 { 0, "smallSetUpperBound", do_smallSetUpperBound},
1452 { 0, "largeSetLowerBound", do_largeSetLowerBound},
1453 { 0, "mediumSetPresentNumber", do_mediumSetPresentNumber},
1454 { 0, "referenceId", do_referenceId },
1459 * ir_obj_method: IR Object methods
1461 static int ir_obj_method (ClientData clientData, Tcl_Interp *interp,
1462 int argc, char **argv)
1464 IrTcl_Methods tab[3];
1465 IrTcl_Obj *p = clientData;
1468 return ir_method_r (clientData, interp, argc, argv, ir_method_tab);
1470 tab[0].tab = ir_method_tab;
1472 tab[1].tab = ir_set_c_method_tab;
1473 tab[1].obj = &p->set_inher;
1476 return ir_method (interp, argc, argv, tab);
1480 * ir_obj_delete: IR Object disposal
1482 static void ir_obj_delete (ClientData clientData)
1484 IrTcl_Obj *obj = clientData;
1485 IrTcl_Methods tab[3];
1488 if (obj->ref_count > 0)
1490 assert (obj->ref_count == 0);
1492 tab[0].tab = ir_method_tab;
1494 tab[1].tab = ir_set_c_method_tab;
1495 tab[1].obj = &obj->set_inher;
1498 ir_method (NULL, -1, NULL, tab);
1499 odr_destroy (obj->odr_in);
1500 odr_destroy (obj->odr_out);
1501 odr_destroy (obj->odr_pr);
1502 free (obj->buf_out);
1508 * ir_obj_mk: IR Object creation
1510 static int ir_obj_mk (ClientData clientData, Tcl_Interp *interp,
1511 int argc, char **argv)
1513 IrTcl_Methods tab[3];
1521 interp->result = "wrong # args";
1524 if (!(obj = ir_malloc (interp, sizeof(*obj))))
1529 obj->bibset = ccl_qual_mk ();
1530 if ((inf = fopen ("default.bib", "r")))
1532 ccl_qual_file (obj->bibset, inf);
1537 obj->odr_in = odr_createmem (ODR_DECODE);
1538 obj->odr_out = odr_createmem (ODR_ENCODE);
1539 obj->odr_pr = odr_createmem (ODR_PRINT);
1541 obj->len_out = 10000;
1542 if (!(obj->buf_out = ir_malloc (interp, obj->len_out)))
1544 odr_setbuf (obj->odr_out, obj->buf_out, obj->len_out, 0);
1549 tab[0].tab = ir_method_tab;
1551 tab[1].tab = ir_set_c_method_tab;
1552 tab[1].obj = &obj->set_inher;
1555 if (ir_method (interp, 0, NULL, tab) == TCL_ERROR)
1557 Tcl_CreateCommand (interp, argv[1], ir_obj_method,
1558 (ClientData) obj, ir_obj_delete);
1562 /* ------------------------------------------------------- */
1564 * do_search: Do search request
1566 static int do_search (void *o, Tcl_Interp *interp, int argc, char **argv)
1568 Z_SearchRequest *req;
1572 IrTcl_SetObj *obj = o;
1584 interp->result = "wrong # args";
1587 if (!obj->set_inher.num_databaseNames)
1589 interp->result = "no databaseNames";
1594 interp->result = "not connected";
1597 odr_reset (p->odr_out);
1598 apdu = zget_APDU (p->odr_out, Z_APDU_searchRequest);
1599 req = apdu->u.searchRequest;
1601 bib1.proto = p->protocol_type;
1602 bib1.class = CLASS_ATTSET;
1603 bib1.value = VAL_BIB1;
1605 set_referenceId (p->odr_out, &req->referenceId,
1606 obj->set_inher.referenceId);
1608 req->smallSetUpperBound = &obj->set_inher.smallSetUpperBound;
1609 req->largeSetLowerBound = &obj->set_inher.largeSetLowerBound;
1610 req->mediumSetPresentNumber = &obj->set_inher.mediumSetPresentNumber;
1611 req->replaceIndicator = &obj->set_inher.replaceIndicator;
1612 req->resultSetName = obj->setName ? obj->setName : "Default";
1613 logf (LOG_DEBUG, "Search, resultSetName %s", req->resultSetName);
1614 req->num_databaseNames = obj->set_inher.num_databaseNames;
1615 req->databaseNames = obj->set_inher.databaseNames;
1616 for (r=0; r < obj->set_inher.num_databaseNames; r++)
1617 logf (LOG_DEBUG, " Database %s", obj->set_inher.databaseNames[r]);
1618 req->smallSetElementSetNames = 0;
1619 req->mediumSetElementSetNames = 0;
1620 if (obj->set_inher.preferredRecordSyntax)
1622 struct oident ident;
1624 ident.proto = p->protocol_type;
1625 ident.class = CLASS_RECSYN;
1626 ident.value = *obj->set_inher.preferredRecordSyntax;
1627 logf (LOG_DEBUG, "Preferred record syntax is %d", ident.value);
1628 req->preferredRecordSyntax = odr_oiddup (p->odr_out,
1629 oid_getoidbyent (&ident));
1632 req->preferredRecordSyntax = 0;
1633 req->query = &query;
1635 if (!strcmp (obj->set_inher.queryType, "rpn"))
1637 Z_RPNQuery *RPNquery;
1639 RPNquery = p_query_rpn (p->odr_out, argv[2]);
1642 Tcl_AppendResult (interp, "Syntax error in query", NULL);
1645 RPNquery->attributeSetId = oid_getoidbyent (&bib1);
1646 query.which = Z_Query_type_1;
1647 query.u.type_1 = RPNquery;
1648 logf (LOG_DEBUG, "RPN");
1651 else if (!strcmp (obj->set_inher.queryType, "cclrpn"))
1655 struct ccl_rpn_node *rpn;
1656 Z_RPNQuery *RPNquery;
1658 rpn = ccl_find_str(p->bibset, argv[2], &error, &pos);
1661 Tcl_AppendResult (interp, "CCL error: ",
1662 ccl_err_msg(error), NULL);
1665 ccl_pr_tree (rpn, stderr);
1666 fprintf (stderr, "\n");
1667 assert((RPNquery = ccl_rpn_query(rpn)));
1668 RPNquery->attributeSetId = oid_getoidbyent (&bib1);
1669 query.which = Z_Query_type_1;
1670 query.u.type_1 = RPNquery;
1671 logf (LOG_DEBUG, "CCLRPN");
1674 else if (!strcmp (obj->set_inher.queryType, "ccl"))
1676 query.which = Z_Query_type_2;
1677 query.u.type_2 = &ccl_query;
1678 ccl_query.buf = (unsigned char *) argv[2];
1679 ccl_query.len = strlen (argv[2]);
1680 logf (LOG_DEBUG, "CCL");
1684 interp->result = "unknown query method";
1687 if (!z_APDU (p->odr_out, &apdu, 0))
1689 interp->result = odr_errlist [odr_geterror (p->odr_out)];
1690 odr_reset (p->odr_out);
1693 p->sbuf = odr_getbuf (p->odr_out, &p->slen, NULL);
1694 if ((r=cs_put (p->cs_link, p->sbuf, p->slen)) < 0)
1696 interp->result = "cs_put failed in search";
1701 ir_select_add_write (cs_fileno(p->cs_link), p);
1702 logf (LOG_DEBUG, "Sent part of searchRequest (%d bytes)", p->slen);
1706 logf (LOG_DEBUG, "Whole search request (%d bytes)", p->slen);
1712 * do_resultCount: Get number of hits
1714 static int do_resultCount (void *o, Tcl_Interp *interp,
1715 int argc, char **argv)
1717 IrTcl_SetObj *obj = o;
1721 return get_set_int (&obj->resultCount, interp, argc, argv);
1725 * do_searchStatus: Get search status (after search response)
1727 static int do_searchStatus (void *o, Tcl_Interp *interp,
1728 int argc, char **argv)
1730 IrTcl_SetObj *obj = o;
1734 return get_set_int (&obj->searchStatus, interp, argc, argv);
1738 * do_presentStatus: Get search status (after search/present response)
1740 static int do_presentStatus (void *o, Tcl_Interp *interp,
1741 int argc, char **argv)
1743 IrTcl_SetObj *obj = o;
1747 return get_set_int (&obj->presentStatus, interp, argc, argv);
1751 * do_nextResultSetPosition: Get next result set position
1752 * (after search/present response)
1754 static int do_nextResultSetPosition (void *o, Tcl_Interp *interp,
1755 int argc, char **argv)
1757 IrTcl_SetObj *obj = o;
1761 obj->nextResultSetPosition = 0;
1764 return get_set_int (&obj->nextResultSetPosition, interp, argc, argv);
1768 * do_setName: Set result Set name
1770 static int do_setName (void *o, Tcl_Interp *interp,
1771 int argc, char **argv)
1773 IrTcl_SetObj *obj = o;
1776 return ir_strdup (interp, &obj->setName, "Default");
1777 else if (argc == -1)
1778 return ir_strdel (interp, &obj->setName);
1781 free (obj->setName);
1782 if (ir_strdup (interp, &obj->setName, argv[2])
1786 Tcl_AppendElement (interp, obj->setName);
1791 * do_numberOfRecordsReturned: Get number of records returned
1793 static int do_numberOfRecordsReturned (void *o, Tcl_Interp *interp,
1794 int argc, char **argv)
1796 IrTcl_SetObj *obj = o;
1800 obj->numberOfRecordsReturned = 0;
1803 return get_set_int (&obj->numberOfRecordsReturned, interp, argc, argv);
1807 * do_type: Return type (if any) at position.
1809 static int do_type (void *o, Tcl_Interp *interp, int argc, char **argv)
1811 IrTcl_SetObj *obj = o;
1813 IrTcl_RecordList *rl;
1817 obj->record_list = NULL;
1820 else if (argc == -1)
1822 delete_IR_records (obj);
1827 sprintf (interp->result, "wrong # args");
1830 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
1832 rl = find_IR_record (obj, offset);
1837 case Z_NamePlusRecord_databaseRecord:
1838 interp->result = "DB";
1840 case Z_NamePlusRecord_surrogateDiagnostic:
1841 interp->result = "SD";
1849 * do_recordType: Return record type (if any) at position.
1851 static int do_recordType (void *o, Tcl_Interp *interp, int argc, char **argv)
1853 IrTcl_SetObj *obj = o;
1855 IrTcl_RecordList *rl;
1861 else if (argc == -1)
1867 sprintf (interp->result, "wrong # args");
1870 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
1872 rl = find_IR_record (obj, offset);
1875 if (rl->which != Z_NamePlusRecord_databaseRecord)
1877 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
1880 Tcl_AppendElement (interp, (char*)
1881 IrTcl_getRecordSyntaxStr (rl->u.dbrec.type));
1888 static int ir_diagResult (Tcl_Interp *interp, IrTcl_Diagnostic *list, int num)
1894 for (i = 0; i<num; i++)
1896 logf (LOG_DEBUG, "Diagnostic, code %d", list[i].condition);
1897 sprintf (buf, "%d", list[i].condition);
1898 Tcl_AppendElement (interp, buf);
1899 cp = diagbib1_str (list[i].condition);
1901 Tcl_AppendElement (interp, (char*) cp);
1903 Tcl_AppendElement (interp, "");
1904 if (list[i].addinfo)
1905 Tcl_AppendElement (interp, (char*) list[i].addinfo);
1907 Tcl_AppendElement (interp, "");
1913 * do_diag: Return diagnostic record info
1915 static int do_diag (void *o, Tcl_Interp *interp, int argc, char **argv)
1917 IrTcl_SetObj *obj = o;
1919 IrTcl_RecordList *rl;
1925 sprintf (interp->result, "wrong # args");
1928 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
1930 rl = find_IR_record (obj, offset);
1933 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
1936 if (rl->which != Z_NamePlusRecord_surrogateDiagnostic)
1938 Tcl_AppendResult (interp, "No Diagnostic record at #", argv[2], NULL);
1941 return ir_diagResult (interp, rl->u.surrogateDiagnostics.list,
1942 rl->u.surrogateDiagnostics.num);
1946 * do_getMarc: Get ISO2709 Record lines/fields
1948 static int do_getMarc (void *o, Tcl_Interp *interp, int argc, char **argv)
1950 IrTcl_SetObj *obj = o;
1952 IrTcl_RecordList *rl;
1958 sprintf (interp->result, "wrong # args");
1961 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
1963 rl = find_IR_record (obj, offset);
1966 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
1969 if (rl->which != Z_NamePlusRecord_databaseRecord)
1971 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
1974 return ir_tcl_get_marc (interp, rl->u.dbrec.buf, argc, argv);
1978 * do_getSutrs: Get SUTRS Record
1980 static int do_getSutrs (void *o, Tcl_Interp *interp, int argc, char **argv)
1982 IrTcl_SetObj *obj = o;
1984 IrTcl_RecordList *rl;
1990 sprintf (interp->result, "wrong # args");
1993 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
1995 rl = find_IR_record (obj, offset);
1998 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2001 if (rl->which != Z_NamePlusRecord_databaseRecord)
2003 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2006 if (rl->u.dbrec.type != VAL_SUTRS)
2008 Tcl_AppendElement (interp, rl->u.dbrec.buf);
2014 * do_responseStatus: Return response status (present or search)
2016 static int do_responseStatus (void *o, Tcl_Interp *interp,
2017 int argc, char **argv)
2019 IrTcl_SetObj *obj = o;
2023 obj->recordFlag = 0;
2024 obj->nonSurrogateDiagnosticNum = 0;
2025 obj->nonSurrogateDiagnosticList = NULL;
2028 else if (argc == -1)
2030 ir_deleteDiags (&obj->nonSurrogateDiagnosticList,
2031 &obj->nonSurrogateDiagnosticNum);
2034 if (!obj->recordFlag)
2036 Tcl_AppendElement (interp, "OK");
2041 case Z_Records_DBOSD:
2042 Tcl_AppendElement (interp, "DBOSD");
2045 Tcl_AppendElement (interp, "NSD");
2046 return ir_diagResult (interp, obj->nonSurrogateDiagnosticList,
2047 obj->nonSurrogateDiagnosticNum);
2053 * do_present: Perform Present Request
2056 static int do_present (void *o, Tcl_Interp *interp,
2057 int argc, char **argv)
2059 IrTcl_SetObj *obj = o;
2062 Z_PresentRequest *req;
2071 if (Tcl_GetInt (interp, argv[2], &start) == TCL_ERROR)
2078 if (Tcl_GetInt (interp, argv[3], &number) == TCL_ERROR)
2085 interp->result = "not connected";
2091 odr_reset (p->odr_out);
2093 obj->number = number;
2095 apdu = zget_APDU (p->odr_out, Z_APDU_presentRequest);
2096 req = apdu->u.presentRequest;
2098 set_referenceId (p->odr_out, &req->referenceId,
2099 obj->set_inher.referenceId);
2101 req->resultSetId = obj->setName ? obj->setName : "Default";
2103 req->resultSetStartPoint = &start;
2104 req->numberOfRecordsRequested = &number;
2105 if (obj->set_inher.preferredRecordSyntax)
2107 struct oident ident;
2109 ident.proto = p->protocol_type;
2110 ident.class = CLASS_RECSYN;
2111 ident.value = *obj->set_inher.preferredRecordSyntax;
2112 logf (LOG_DEBUG, "Preferred record syntax is %d", ident.value);
2113 req->preferredRecordSyntax = odr_oiddup (p->odr_out,
2114 oid_getoidbyent (&ident));
2117 req->preferredRecordSyntax = 0;
2119 if (!z_APDU (p->odr_out, &apdu, 0))
2121 interp->result = odr_errlist [odr_geterror (p->odr_out)];
2122 odr_reset (p->odr_out);
2125 p->sbuf = odr_getbuf (p->odr_out, &p->slen, NULL);
2126 if ((r=cs_put (p->cs_link, p->sbuf, p->slen)) < 0)
2128 interp->result = "cs_put failed in present";
2133 ir_select_add_write (cs_fileno(p->cs_link), p);
2134 logf (LOG_DEBUG, "Part of present request, start=%d, num=%d"
2135 " (%d bytes)", start, number, p->slen);
2139 logf (LOG_DEBUG, "Whole present request, start=%d, num=%d"
2140 " (%d bytes)", start, number, p->slen);
2146 * do_loadFile: Load result set from file
2149 static int do_loadFile (void *o, Tcl_Interp *interp,
2150 int argc, char **argv)
2152 IrTcl_SetObj *setobj = o;
2162 interp->result = "wrong # args";
2165 inf = fopen (argv[2], "r");
2168 Tcl_AppendResult (interp, "Cannot open ", argv[2], NULL);
2171 while ((buf = ir_tcl_fread_marc (inf, &size)))
2173 IrTcl_RecordList *rl;
2175 rl = new_IR_record (setobj, no, Z_NamePlusRecord_databaseRecord);
2176 rl->u.dbrec.type = VAL_USMARC;
2177 rl->u.dbrec.buf = buf;
2178 rl->u.dbrec.size = size;
2181 setobj->numberOfRecordsReturned = no-1;
2186 static IrTcl_Method ir_set_method_tab[] = {
2187 { 0, "search", do_search },
2188 { 0, "searchStatus", do_searchStatus },
2189 { 0, "presentStatus", do_presentStatus },
2190 { 0, "nextResultSetPosition", do_nextResultSetPosition },
2191 { 0, "setName", do_setName },
2192 { 0, "resultCount", do_resultCount },
2193 { 0, "numberOfRecordsReturned", do_numberOfRecordsReturned },
2194 { 0, "present", do_present },
2195 { 0, "type", do_type },
2196 { 0, "getMarc", do_getMarc },
2197 { 0, "getSutrs", do_getSutrs },
2198 { 0, "recordType", do_recordType },
2199 { 0, "diag", do_diag },
2200 { 0, "responseStatus", do_responseStatus },
2201 { 0, "loadFile", do_loadFile },
2206 * ir_set_obj_method: IR Set Object methods
2208 static int ir_set_obj_method (ClientData clientData, Tcl_Interp *interp,
2209 int argc, char **argv)
2211 IrTcl_Methods tabs[3];
2212 IrTcl_SetObj *p = clientData;
2216 interp->result = "wrong # args";
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 return ir_method (interp, argc, argv, tabs);
2229 * ir_set_obj_delete: IR Set Object disposal
2231 static void ir_set_obj_delete (ClientData clientData)
2233 IrTcl_Methods tabs[3];
2234 IrTcl_SetObj *p = clientData;
2236 tabs[0].tab = ir_set_method_tab;
2238 tabs[1].tab = ir_set_c_method_tab;
2239 tabs[1].obj = &p->set_inher;
2242 ir_method (NULL, -1, NULL, tabs);
2248 * ir_set_obj_mk: IR Set Object creation
2250 static int ir_set_obj_mk (ClientData clientData, Tcl_Interp *interp,
2251 int argc, char **argv)
2253 IrTcl_Methods tabs[3];
2256 if (argc < 2 || argc > 3)
2258 interp->result = "wrong # args";
2261 if (!(obj = ir_malloc (interp, sizeof(*obj))))
2265 Tcl_CmdInfo parent_info;
2270 if (!Tcl_GetCommandInfo (interp, argv[2], &parent_info))
2272 interp->result = "No parent";
2275 obj->parent = (IrTcl_Obj *) parent_info.clientData;
2277 dst = &obj->set_inher;
2278 src = &obj->parent->set_inher;
2280 dst->num_databaseNames = src->num_databaseNames;
2281 if (!(dst->databaseNames =
2282 ir_malloc (interp, sizeof (*dst->databaseNames)
2283 * dst->num_databaseNames)))
2285 for (i = 0; i < dst->num_databaseNames; i++)
2287 if (ir_strdup (interp, &dst->databaseNames[i],
2288 src->databaseNames[i]) == TCL_ERROR)
2291 if (ir_strdup (interp, &dst->queryType, src->queryType)
2295 if (ir_strdup (interp, &dst->referenceId, src->referenceId)
2299 if (src->preferredRecordSyntax &&
2300 (dst->preferredRecordSyntax
2301 = malloc (sizeof(*dst->preferredRecordSyntax))))
2302 *dst->preferredRecordSyntax = *src->preferredRecordSyntax;
2304 dst->preferredRecordSyntax = NULL;
2305 dst->replaceIndicator = src->replaceIndicator;
2306 dst->smallSetUpperBound = src->smallSetUpperBound;
2307 dst->largeSetLowerBound = src->largeSetLowerBound;
2308 dst->mediumSetPresentNumber = src->mediumSetPresentNumber;
2313 tabs[0].tab = ir_set_method_tab;
2317 if (ir_method (interp, 0, NULL, tabs) == TCL_ERROR)
2320 Tcl_CreateCommand (interp, argv[1], ir_set_obj_method,
2321 (ClientData) obj, ir_set_obj_delete);
2325 /* ------------------------------------------------------- */
2328 * do_scan: Perform scan
2330 static int do_scan (void *o, Tcl_Interp *interp, int argc, char **argv)
2334 IrTcl_ScanObj *obj = o;
2335 IrTcl_Obj *p = obj->parent;
2339 struct ccl_rpn_node *rpn;
2348 interp->result = "wrong # args";
2351 if (!p->set_inher.num_databaseNames)
2353 interp->result = "no databaseNames";
2358 interp->result = "not connected";
2361 odr_reset (p->odr_out);
2363 bib1.proto = p->protocol_type;
2364 bib1.class = CLASS_ATTSET;
2365 bib1.value = VAL_BIB1;
2367 apdu = zget_APDU (p->odr_out, Z_APDU_scanRequest);
2368 req = apdu->u.scanRequest;
2370 set_referenceId (p->odr_out, &req->referenceId, p->set_inher.referenceId);
2371 req->num_databaseNames = p->set_inher.num_databaseNames;
2372 req->databaseNames = p->set_inher.databaseNames;
2373 req->attributeSet = oid_getoidbyent (&bib1);
2376 if (!(req->termListAndStartPoint = p_query_scan (p->odr_out, argv[2])))
2378 Tcl_AppendResult (interp, "Syntax error in query", NULL);
2382 rpn = ccl_find_str(p->bibset, argv[2], &r, &pos);
2385 Tcl_AppendResult (interp, "CCL error: ", ccl_err_msg (r), NULL);
2388 ccl_pr_tree (rpn, stderr);
2389 fprintf (stderr, "\n");
2390 if (!(req->termListAndStartPoint = ccl_scan_query (rpn)))
2393 req->stepSize = &obj->stepSize;
2394 req->numberOfTermsRequested = &obj->numberOfTermsRequested;
2395 req->preferredPositionInResponse = &obj->preferredPositionInResponse;
2396 logf (LOG_DEBUG, "stepSize=%d", *req->stepSize);
2397 logf (LOG_DEBUG, "numberOfTermsRequested=%d",
2398 *req->numberOfTermsRequested);
2399 logf (LOG_DEBUG, "preferredPositionInResponse=%d",
2400 *req->preferredPositionInResponse);
2402 if (!z_APDU (p->odr_out, &apdu, 0))
2404 interp->result = odr_errlist [odr_geterror (p->odr_out)];
2405 odr_reset (p->odr_out);
2408 p->sbuf = odr_getbuf (p->odr_out, &p->slen, NULL);
2409 if ((r=cs_put (p->cs_link, p->sbuf, p->slen)) < 0)
2411 interp->result = "cs_put failed in scan";
2416 ir_select_add_write (cs_fileno(p->cs_link), p);
2417 logf (LOG_DEBUG, "Sent part of scanRequest (%d bytes)", p->slen);
2421 logf (LOG_DEBUG, "Whole scan request (%d bytes)", p->slen);
2427 * do_stepSize: Set/get replace Step Size
2429 static int do_stepSize (void *obj, Tcl_Interp *interp,
2430 int argc, char **argv)
2432 IrTcl_ScanObj *p = obj;
2438 return get_set_int (&p->stepSize, interp, argc, argv);
2442 * do_numberOfTermsRequested: Set/get Number of Terms requested
2444 static int do_numberOfTermsRequested (void *obj, Tcl_Interp *interp,
2445 int argc, char **argv)
2447 IrTcl_ScanObj *p = obj;
2451 p->numberOfTermsRequested = 20;
2454 return get_set_int (&p->numberOfTermsRequested, interp, argc, argv);
2459 * do_preferredPositionInResponse: Set/get preferred Position
2461 static int do_preferredPositionInResponse (void *obj, Tcl_Interp *interp,
2462 int argc, char **argv)
2464 IrTcl_ScanObj *p = obj;
2468 p->preferredPositionInResponse = 1;
2471 return get_set_int (&p->preferredPositionInResponse, interp, argc, argv);
2475 * do_scanStatus: Get scan status
2477 static int do_scanStatus (void *obj, Tcl_Interp *interp,
2478 int argc, char **argv)
2480 IrTcl_ScanObj *p = obj;
2484 return get_set_int (&p->scanStatus, interp, argc, argv);
2488 * do_numberOfEntriesReturned: Get number of Entries returned
2490 static int do_numberOfEntriesReturned (void *obj, Tcl_Interp *interp,
2491 int argc, char **argv)
2493 IrTcl_ScanObj *p = obj;
2497 return get_set_int (&p->numberOfEntriesReturned, interp, argc, argv);
2501 * do_positionOfTerm: Get position of Term
2503 static int do_positionOfTerm (void *obj, Tcl_Interp *interp,
2504 int argc, char **argv)
2506 IrTcl_ScanObj *p = obj;
2510 return get_set_int (&p->positionOfTerm, interp, argc, argv);
2514 * do_scanLine: get Scan Line (surrogate or normal) after response
2516 static int do_scanLine (void *obj, Tcl_Interp *interp, int argc, char **argv)
2518 IrTcl_ScanObj *p = obj;
2524 p->entries_flag = 0;
2526 p->nonSurrogateDiagnosticNum = 0;
2527 p->nonSurrogateDiagnosticList = 0;
2530 else if (argc == -1)
2532 p->entries_flag = 0;
2533 /* release entries */
2536 ir_deleteDiags (&p->nonSurrogateDiagnosticList,
2537 &p->nonSurrogateDiagnosticNum);
2542 interp->result = "wrong # args";
2545 if (Tcl_GetInt (interp, argv[2], &i) == TCL_ERROR)
2547 if (!p->entries_flag || p->which != Z_ListEntries_entries || !p->entries
2548 || i >= p->num_entries || i < 0)
2550 switch (p->entries[i].which)
2552 case Z_Entry_termInfo:
2553 Tcl_AppendElement (interp, "T");
2554 if (p->entries[i].u.term.buf)
2555 Tcl_AppendElement (interp, p->entries[i].u.term.buf);
2557 Tcl_AppendElement (interp, "");
2558 sprintf (numstr, "%d", p->entries[i].u.term.globalOccurrences);
2559 Tcl_AppendElement (interp, numstr);
2561 case Z_Entry_surrogateDiagnostic:
2562 Tcl_AppendElement (interp, "SD");
2563 return ir_diagResult (interp, p->entries[i].u.diag.list,
2564 p->entries[i].u.diag.num);
2570 static IrTcl_Method ir_scan_method_tab[] = {
2571 { 0, "scan", do_scan },
2572 { 0, "stepSize", do_stepSize },
2573 { 0, "numberOfTermsRequested", do_numberOfTermsRequested },
2574 { 0, "preferredPositionInResponse", do_preferredPositionInResponse },
2575 { 0, "scanStatus", do_scanStatus },
2576 { 0, "numberOfEntriesReturned", do_numberOfEntriesReturned },
2577 { 0, "positionOfTerm", do_positionOfTerm },
2578 { 0, "scanLine", do_scanLine },
2583 * ir_scan_obj_method: IR Scan Object methods
2585 static int ir_scan_obj_method (ClientData clientData, Tcl_Interp *interp,
2586 int argc, char **argv)
2588 IrTcl_Methods tabs[2];
2592 interp->result = "wrong # args";
2595 tabs[0].tab = ir_scan_method_tab;
2596 tabs[0].obj = clientData;
2599 return ir_method (interp, argc, argv, tabs);
2603 * ir_scan_obj_delete: IR Scan Object disposal
2605 static void ir_scan_obj_delete (ClientData clientData)
2607 IrTcl_Methods tabs[2];
2608 IrTcl_ScanObj *obj = clientData;
2610 tabs[0].tab = ir_scan_method_tab;
2614 ir_method (NULL, -1, NULL, tabs);
2619 * ir_scan_obj_mk: IR Scan Object creation
2621 static int ir_scan_obj_mk (ClientData clientData, Tcl_Interp *interp,
2622 int argc, char **argv)
2624 Tcl_CmdInfo parent_info;
2626 IrTcl_Methods tabs[2];
2630 interp->result = "wrong # args";
2633 if (!Tcl_GetCommandInfo (interp, argv[2], &parent_info))
2635 interp->result = "No parent";
2638 if (!(obj = ir_malloc (interp, sizeof(*obj))))
2641 obj->parent = (IrTcl_Obj *) parent_info.clientData;
2643 tabs[0].tab = ir_scan_method_tab;
2647 if (ir_method (interp, 0, NULL, tabs) == TCL_ERROR)
2649 Tcl_CreateCommand (interp, argv[1], ir_scan_obj_method,
2650 (ClientData) obj, ir_scan_obj_delete);
2654 /* ------------------------------------------------------- */
2656 static void ir_initResponse (void *obj, Z_InitResponse *initrs)
2660 p->initResult = *initrs->result ? 1 : 0;
2661 if (!*initrs->result)
2662 logf (LOG_DEBUG, "Connection rejected by target");
2664 logf (LOG_DEBUG, "Connection accepted by target");
2666 get_referenceId (&p->set_inher.referenceId, initrs->referenceId);
2668 free (p->targetImplementationId);
2669 ir_strdup (p->interp, &p->targetImplementationId,
2670 initrs->implementationId);
2671 free (p->targetImplementationName);
2672 ir_strdup (p->interp, &p->targetImplementationName,
2673 initrs->implementationName);
2674 free (p->targetImplementationVersion);
2675 ir_strdup (p->interp, &p->targetImplementationVersion,
2676 initrs->implementationVersion);
2678 p->maximumRecordSize = *initrs->maximumRecordSize;
2679 p->preferredMessageSize = *initrs->preferredMessageSize;
2681 memcpy (&p->options, initrs->options, sizeof(initrs->options));
2682 memcpy (&p->protocolVersion, initrs->protocolVersion,
2683 sizeof(initrs->protocolVersion));
2684 free (p->userInformationField);
2685 p->userInformationField = NULL;
2686 if (initrs->userInformationField)
2690 if (initrs->userInformationField->which == ODR_EXTERNAL_octet &&
2691 (p->userInformationField =
2693 initrs->userInformationField->u.octet_aligned->len)
2696 memcpy (p->userInformationField,
2697 initrs->userInformationField->u.octet_aligned->buf,
2699 (p->userInformationField)[len] = '\0';
2704 static void ir_deleteDiags (IrTcl_Diagnostic **dst_list, int *dst_num)
2707 for (i = 0; i<*dst_num; i++)
2708 free (dst_list[i]->addinfo);
2714 static void ir_handleDiags (IrTcl_Diagnostic **dst_list, int *dst_num,
2715 Z_DiagRec **list, int num)
2721 *dst_list = malloc (sizeof(**dst_list) * num);
2727 for (i = 0; i<num; i++)
2729 switch (list[i]->which)
2731 case Z_DiagRec_defaultFormat:
2732 (*dst_list)[i].condition = *list[i]->u.defaultFormat->condition;
2733 addinfo = list[i]->u.defaultFormat->addinfo;
2735 ((*dst_list)[i].addinfo = malloc (strlen(addinfo)+1)))
2736 strcpy ((*dst_list)[i].addinfo, addinfo);
2739 (*dst_list)[i].addinfo = NULL;
2740 (*dst_list)[i].condition = 0;
2745 static void ir_handleRecords (void *o, Z_Records *zrs)
2748 IrTcl_SetObj *setobj = p->set_child;
2751 IrTcl_RecordList *rl;
2753 setobj->which = zrs->which;
2754 setobj->recordFlag = 1;
2756 ir_deleteDiags (&setobj->nonSurrogateDiagnosticList,
2757 &setobj->nonSurrogateDiagnosticNum);
2758 if (zrs->which == Z_Records_DBOSD)
2760 setobj->numberOfRecordsReturned =
2761 zrs->u.databaseOrSurDiagnostics->num_records;
2762 logf (LOG_DEBUG, "Got %d records", setobj->numberOfRecordsReturned);
2763 for (offset = 0; offset<setobj->numberOfRecordsReturned; offset++)
2765 rl = new_IR_record (setobj, setobj->start + offset,
2766 zrs->u.databaseOrSurDiagnostics->
2767 records[offset]->which);
2768 if (rl->which == Z_NamePlusRecord_surrogateDiagnostic)
2770 ir_handleDiags (&rl->u.surrogateDiagnostics.list,
2771 &rl->u.surrogateDiagnostics.num,
2772 &zrs->u.databaseOrSurDiagnostics->
2773 records[offset]->u.surrogateDiagnostic,
2778 Z_DatabaseRecord *zr;
2780 struct oident *ident;
2782 zr = zrs->u.databaseOrSurDiagnostics->records[offset]
2784 oe = (Odr_external*) zr;
2785 rl->u.dbrec.size = zr->u.octet_aligned->len;
2787 rl->u.dbrec.type = VAL_USMARC;
2788 if ((ident = oid_getentbyoid (oe->direct_reference)))
2789 rl->u.dbrec.type = ident->value;
2790 if (oe->which == ODR_EXTERNAL_octet && rl->u.dbrec.size > 0)
2792 char *buf = (char*) zr->u.octet_aligned->buf;
2793 if ((rl->u.dbrec.buf = malloc (rl->u.dbrec.size)))
2794 memcpy (rl->u.dbrec.buf, buf, rl->u.dbrec.size);
2796 else if (rl->u.dbrec.type == VAL_SUTRS &&
2797 oe->which == ODR_EXTERNAL_single)
2801 logf (LOG_DEBUG, "Decoding SUTRS");
2802 odr_setbuf (p->odr_in, (char*) oe->u.single_ASN1_type->buf,
2803 oe->u.single_ASN1_type->len, 0);
2804 if (!z_SUTRS(p->odr_in, &rc, 0))
2806 logf (LOG_WARN, "Cannot decode SUTRS");
2807 rl->u.dbrec.buf = NULL;
2811 if ((rl->u.dbrec.buf = malloc (rc->len+1)))
2813 memcpy (rl->u.dbrec.buf, rc->buf, rc->len);
2814 rl->u.dbrec.buf[rc->len] = '\0';
2816 rl->u.dbrec.size = rc->len;
2820 rl->u.dbrec.buf = NULL;
2824 else if (zrs->which == Z_Records_multipleNSD)
2826 logf (LOG_DEBUG, "multipleNonSurrogateDiagnostic %d",
2827 zrs->u.multipleNonSurDiagnostics->num_diagRecs);
2828 setobj->numberOfRecordsReturned = 0;
2829 ir_handleDiags (&setobj->nonSurrogateDiagnosticList,
2830 &setobj->nonSurrogateDiagnosticNum,
2831 zrs->u.multipleNonSurDiagnostics->diagRecs,
2832 zrs->u.multipleNonSurDiagnostics->num_diagRecs);
2836 logf (LOG_DEBUG, "NonSurrogateDiagnostic");
2837 setobj->numberOfRecordsReturned = 0;
2838 ir_handleDiags (&setobj->nonSurrogateDiagnosticList,
2839 &setobj->nonSurrogateDiagnosticNum,
2840 &zrs->u.nonSurrogateDiagnostic,
2845 static void ir_searchResponse (void *o, Z_SearchResponse *searchrs)
2848 IrTcl_SetObj *setobj = p->set_child;
2849 Z_Records *zrs = searchrs->records;
2851 logf (LOG_DEBUG, "Received search response");
2854 logf (LOG_DEBUG, "Search response, no object!");
2857 setobj->searchStatus = searchrs->searchStatus ? 1 : 0;
2858 get_referenceId (&setobj->set_inher.referenceId, searchrs->referenceId);
2859 setobj->resultCount = *searchrs->resultCount;
2860 if (searchrs->presentStatus)
2861 setobj->presentStatus = *searchrs->presentStatus;
2862 if (searchrs->nextResultSetPosition)
2863 setobj->nextResultSetPosition = *searchrs->nextResultSetPosition;
2865 logf (LOG_DEBUG, "Search response %d, %d hits",
2866 setobj->searchStatus, setobj->resultCount);
2868 ir_handleRecords (o, zrs);
2870 setobj->recordFlag = 0;
2874 static void ir_presentResponse (void *o, Z_PresentResponse *presrs)
2877 IrTcl_SetObj *setobj = p->set_child;
2878 Z_Records *zrs = presrs->records;
2880 logf (LOG_DEBUG, "Received present response");
2883 logf (LOG_DEBUG, "Present response, no object!");
2886 setobj->presentStatus = *presrs->presentStatus;
2887 get_referenceId (&setobj->set_inher.referenceId, presrs->referenceId);
2888 setobj->nextResultSetPosition = *presrs->nextResultSetPosition;
2890 ir_handleRecords (o, zrs);
2893 setobj->recordFlag = 0;
2894 logf (LOG_DEBUG, "No records!");
2898 static void ir_scanResponse (void *o, Z_ScanResponse *scanrs)
2901 IrTcl_ScanObj *scanobj = p->scan_child;
2903 logf (LOG_DEBUG, "Received scanResponse");
2905 get_referenceId (&p->set_inher.referenceId, scanrs->referenceId);
2906 scanobj->scanStatus = *scanrs->scanStatus;
2907 logf (LOG_DEBUG, "scanStatus=%d", scanobj->scanStatus);
2909 if (scanrs->stepSize)
2910 scanobj->stepSize = *scanrs->stepSize;
2911 logf (LOG_DEBUG, "stepSize=%d", scanobj->stepSize);
2913 scanobj->numberOfEntriesReturned = *scanrs->numberOfEntriesReturned;
2914 logf (LOG_DEBUG, "numberOfEntriesReturned=%d",
2915 scanobj->numberOfEntriesReturned);
2917 if (scanrs->positionOfTerm)
2918 scanobj->positionOfTerm = *scanrs->positionOfTerm;
2920 scanobj->positionOfTerm = -1;
2921 logf (LOG_DEBUG, "positionOfTerm=%d", scanobj->positionOfTerm);
2923 free (scanobj->entries);
2924 scanobj->entries = NULL;
2926 ir_deleteDiags (&scanobj->nonSurrogateDiagnosticList,
2927 &scanobj->nonSurrogateDiagnosticNum);
2928 if (scanrs->entries)
2933 scanobj->entries_flag = 1;
2934 scanobj->which = scanrs->entries->which;
2935 switch (scanobj->which)
2937 case Z_ListEntries_entries:
2938 scanobj->num_entries = scanrs->entries->u.entries->num_entries;
2939 scanobj->entries = malloc (scanobj->num_entries *
2940 sizeof(*scanobj->entries));
2941 for (i=0; i<scanobj->num_entries; i++)
2943 ze = scanrs->entries->u.entries->entries[i];
2944 scanobj->entries[i].which = ze->which;
2947 case Z_Entry_termInfo:
2948 if (ze->u.termInfo->term->which == Z_Term_general)
2950 int l = ze->u.termInfo->term->u.general->len;
2951 scanobj->entries[i].u.term.buf = malloc (1+l);
2952 memcpy (scanobj->entries[i].u.term.buf,
2953 ze->u.termInfo->term->u.general->buf,
2955 scanobj->entries[i].u.term.buf[l] = '\0';
2958 scanobj->entries[i].u.term.buf = NULL;
2959 if (ze->u.termInfo->globalOccurrences)
2960 scanobj->entries[i].u.term.globalOccurrences =
2961 *ze->u.termInfo->globalOccurrences;
2963 scanobj->entries[i].u.term.globalOccurrences = 0;
2965 case Z_Entry_surrogateDiagnostic:
2966 ir_handleDiags (&scanobj->entries[i].u.diag.list,
2967 &scanobj->entries[i].u.diag.num,
2968 &ze->u.surrogateDiagnostic,
2974 case Z_ListEntries_nonSurrogateDiagnostics:
2975 ir_handleDiags (&scanobj->nonSurrogateDiagnosticList,
2976 &scanobj->nonSurrogateDiagnosticNum,
2977 scanrs->entries->u.nonSurrogateDiagnostics->
2979 scanrs->entries->u.nonSurrogateDiagnostics->
2985 scanobj->entries_flag = 0;
2989 * ir_select_read: handle incoming packages
2991 void ir_select_read (ClientData clientData)
2993 IrTcl_Obj *p = clientData;
2999 r = cs_rcvconnect (p->cs_link);
3002 logf (LOG_WARN, "cs_rcvconnect returned 1");
3006 ir_select_remove_write (cs_fileno (p->cs_link), p);
3009 logf (LOG_DEBUG, "cs_rcvconnect error");
3012 p->failInfo = IR_TCL_FAIL_CONNECT;
3013 IrTcl_eval (p->interp, p->failback);
3015 do_disconnect (p, NULL, 2, NULL);
3019 IrTcl_eval (p->interp, p->callback);
3024 /* signal one more use of ir object - callbacks must not
3025 release the ir memory (p pointer) */
3027 if ((r=cs_get (p->cs_link, &p->buf_in, &p->len_in)) <= 0)
3029 logf (LOG_DEBUG, "cs_get failed, code %d", r);
3030 ir_select_remove (cs_fileno (p->cs_link), p);
3033 p->failInfo = IR_TCL_FAIL_READ;
3034 IrTcl_eval (p->interp, p->failback);
3036 do_disconnect (p, NULL, 2, NULL);
3038 /* relase ir object now if callback deleted it */
3044 odr_setbuf (p->odr_in, p->buf_in, r, 0);
3045 logf (LOG_DEBUG, "cs_get ok, got %d", r);
3046 if (!z_APDU (p->odr_in, &apdu, 0))
3048 logf (LOG_DEBUG, "%s", odr_errlist [odr_geterror (p->odr_in)]);
3051 p->failInfo = IR_TCL_FAIL_IN_APDU;
3052 IrTcl_eval (p->interp, p->failback);
3054 do_disconnect (p, NULL, 2, NULL);
3056 /* release ir object now if failback deleted it */
3062 case Z_APDU_initResponse:
3063 ir_initResponse (p, apdu->u.initResponse);
3065 case Z_APDU_searchResponse:
3066 ir_searchResponse (p, apdu->u.searchResponse);
3068 case Z_APDU_presentResponse:
3069 ir_presentResponse (p, apdu->u.presentResponse);
3071 case Z_APDU_scanResponse:
3072 ir_scanResponse (p, apdu->u.scanResponse);
3075 logf (LOG_WARN, "Received unknown APDU type (%d)", apdu->which);
3078 p->failInfo = IR_TCL_FAIL_UNKNOWN_APDU;
3079 IrTcl_eval (p->interp, p->failback);
3081 do_disconnect (p, NULL, 2, NULL);
3083 odr_reset (p->odr_in);
3085 IrTcl_eval (p->interp, p->callback);
3086 if (p->ref_count == 1)
3092 } while (p->cs_link && cs_more (p->cs_link));
3096 * ir_select_write: handle outgoing packages - not yet written.
3098 void ir_select_write (ClientData clientData)
3100 IrTcl_Obj *p = clientData;
3103 logf (LOG_DEBUG, "In write handler");
3106 r = cs_rcvconnect (p->cs_link);
3112 logf (LOG_DEBUG, "cs_rcvconnect error");
3113 ir_select_remove_write (cs_fileno (p->cs_link), p);
3116 p->failInfo = IR_TCL_FAIL_CONNECT;
3117 IrTcl_eval (p->interp, p->failback);
3119 do_disconnect (p, NULL, 2, NULL);
3122 ir_select_remove_write (cs_fileno (p->cs_link), p);
3124 IrTcl_eval (p->interp, p->callback);
3127 if ((r=cs_put (p->cs_link, p->sbuf, p->slen)) < 0)
3129 logf (LOG_DEBUG, "select write fail");
3132 p->failInfo = IR_TCL_FAIL_WRITE;
3133 IrTcl_eval (p->interp, p->failback);
3135 do_disconnect (p, NULL, 2, NULL);
3137 else if (r == 0) /* remove select bit */
3139 ir_select_remove_write (cs_fileno (p->cs_link), p);
3143 /* ------------------------------------------------------- */
3146 * ir_tcl_init: Registration of TCL commands.
3148 int ir_tcl_init (Tcl_Interp *interp)
3150 Tcl_CreateCommand (interp, "ir", ir_obj_mk, (ClientData) NULL,
3151 (Tcl_CmdDeleteProc *) NULL);
3152 Tcl_CreateCommand (interp, "ir-set", ir_set_obj_mk,
3153 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
3154 Tcl_CreateCommand (interp, "ir-scan", ir_scan_obj_mk,
3155 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
3156 irTcl_interp = interp;