2 * IR toolkit for tcl/tk
4 * See the file LICENSE for details.
5 * Sebastian Hammer, Adam Dickmeiss
8 * Revision 1.48 1995-06-27 19:03:50 adam
9 * Bug fix in do_present in ir-tcl.c: p->set_child member weren't set.
10 * nextResultSetPosition used instead of setOffset.
12 * Revision 1.47 1995/06/25 10:25:04 adam
13 * Working on triggerResourceControl. Description of compile/install
14 * procedure moved to ir-tcl.sgml.
16 * Revision 1.46 1995/06/22 13:15:06 adam
17 * Feature: SUTRS. Setting getSutrs implemented.
18 * Work on display formats.
19 * Preferred record syntax can be set by the user.
21 * Revision 1.45 1995/06/20 08:07:30 adam
22 * New setting: failInfo.
23 * Working on better cancel mechanism.
25 * Revision 1.44 1995/06/19 17:01:20 adam
28 * Revision 1.43 1995/06/19 13:06:08 adam
29 * New define: IR_TCL_VERSION.
31 * Revision 1.42 1995/06/19 08:08:52 adam
32 * client.tcl: hotTargets now contain both database and target name.
33 * ir-tcl.c: setting protocol edited. Errors in callbacks are logged
34 * by logf(LOG_WARN, ...) calls.
36 * Revision 1.41 1995/06/16 12:28:16 adam
37 * Implemented preferredRecordSyntax.
38 * Minor changes in diagnostic handling.
39 * Record list deleted when connection closes.
41 * Revision 1.40 1995/06/14 13:37:18 adam
42 * Setting recordType implemented.
43 * Setting implementationVersion implemented.
44 * Settings implementationId / implementationName edited.
46 * Revision 1.39 1995/06/08 10:26:32 adam
47 * Bug fix in ir_strdup.
49 * Revision 1.38 1995/06/01 16:36:47 adam
50 * About buttons. Minor bug fixes.
52 * Revision 1.37 1995/06/01 07:31:20 adam
53 * Rename of many typedefs -> IrTcl_...
55 * Revision 1.36 1995/05/31 13:09:59 adam
56 * Client searches/presents may be interrupted.
57 * New moving book-logo.
59 * Revision 1.35 1995/05/31 08:36:33 adam
60 * Bug fix in client.tcl: didn't save options on clientrc.tcl.
61 * New method: referenceId. More work on scan.
63 * Revision 1.34 1995/05/29 10:33:42 adam
64 * README and rename of startup script.
66 * Revision 1.33 1995/05/29 09:15:11 quinn
67 * Changed CS_SR to PROTO_SR, etc.
69 * Revision 1.32 1995/05/29 08:44:16 adam
70 * Work on delete of objects.
72 * Revision 1.31 1995/05/26 11:44:10 adam
73 * Bugs fixed. More work on MARC utilities and queries. Test
74 * client is up-to-date again.
76 * Revision 1.30 1995/05/26 08:54:11 adam
77 * New MARC utilities. Uses prefix query.
79 * Revision 1.29 1995/05/24 14:10:22 adam
80 * Work on idAuthentication, protocolVersion and options.
82 * Revision 1.28 1995/05/23 15:34:48 adam
83 * Many new settings, userInformationField, smallSetUpperBound, etc.
84 * A number of settings are inherited when ir-set is executed.
85 * This version is incompatible with the graphical test client (client.tcl).
87 * Revision 1.27 1995/05/11 15:34:47 adam
88 * Scan request changed a bit. This version works with RLG.
90 * Revision 1.26 1995/04/18 16:11:51 adam
91 * First version of graphical Scan. Some work on query-by-form.
93 * Revision 1.25 1995/04/17 09:37:17 adam
94 * Further development of scan.
96 * Revision 1.24 1995/04/11 14:16:42 adam
97 * Further work on scan. Response works. Entries aren't saved yet.
99 * Revision 1.23 1995/04/10 10:50:27 adam
100 * Result-set name defaults to suffix of ir-set name.
101 * Started working on scan. Not finished at this point.
103 * Revision 1.22 1995/03/31 10:43:03 adam
104 * More robust when getting bad MARC records.
106 * Revision 1.21 1995/03/31 08:56:37 adam
107 * New button "Search".
109 * Revision 1.20 1995/03/29 16:07:09 adam
110 * Bug fix: Didn't use setName in present request.
112 * Revision 1.19 1995/03/28 12:45:23 adam
113 * New ir method failback: called on disconnect/protocol error.
114 * New ir set/get method: protocol: SR / Z3950.
115 * Simple popup and disconnect when failback is invoked.
117 * Revision 1.18 1995/03/21 15:50:12 adam
120 * Revision 1.17 1995/03/21 13:41:03 adam
121 * Comstack cs_create not used too often. Non-blocking connect.
123 * Revision 1.16 1995/03/21 08:26:06 adam
124 * New method, setName, to specify the result set name (other than Default).
125 * New method, responseStatus, which returns diagnostic info, if any, after
126 * present response / search response.
128 * Revision 1.15 1995/03/20 15:24:07 adam
129 * Diagnostic records saved on searchResponse.
131 * Revision 1.14 1995/03/20 08:53:22 adam
132 * Event loop in tclmain.c rewritten. New method searchStatus.
134 * Revision 1.13 1995/03/17 18:26:17 adam
135 * Non-blocking i/o used now. Database names popup as cascade items.
137 * Revision 1.12 1995/03/17 15:45:00 adam
138 * Improved target/database setup.
140 * Revision 1.11 1995/03/16 17:54:03 adam
141 * Minor changes really.
143 * Revision 1.10 1995/03/15 16:14:50 adam
144 * Blocking arg in cs_create changed.
146 * Revision 1.9 1995/03/15 13:59:24 adam
149 * Revision 1.8 1995/03/15 08:25:16 adam
150 * New method presentStatus to check for error on present. Misc. cleanup
151 * of IrTcl_RecordList manipulations. Full MARC record presentation in
154 * Revision 1.7 1995/03/14 17:32:29 adam
155 * Presentation of full Marc record in popup window.
157 * Revision 1.6 1995/03/12 19:31:55 adam
158 * Pattern matching implemented when retrieving MARC records. More
159 * diagnostic functions.
161 * Revision 1.5 1995/03/10 18:00:15 adam
162 * Actual presentation in line-by-line format. RPN query support.
164 * Revision 1.4 1995/03/09 16:15:08 adam
165 * First presentRequest attempts. Hot-target list.
171 #include <sys/time.h>
181 int (*method) (void *obj, Tcl_Interp *interp, int argc, char **argv);
189 static Tcl_Interp *irTcl_interp;
191 static void ir_deleteDiags (IrTcl_Diagnostic **dst_list, int *dst_num);
192 static int do_disconnect (void *obj, Tcl_Interp *interp,
193 int argc, char **argv);
195 static IrTcl_RecordList *new_IR_record (IrTcl_SetObj *setobj,
198 IrTcl_RecordList *rl;
200 for (rl = setobj->record_list; rl; rl = rl->next)
206 case Z_NamePlusRecord_databaseRecord:
207 free (rl->u.dbrec.buf);
208 rl->u.dbrec.buf = NULL;
210 case Z_NamePlusRecord_surrogateDiagnostic:
211 ir_deleteDiags (&rl->u.surrogateDiagnostics.list,
212 &rl->u.surrogateDiagnostics.num);
220 rl = malloc (sizeof(*rl));
222 rl->next = setobj->record_list;
224 setobj->record_list = rl;
231 enum oid_value value;
233 } IrTcl_recordSyntaxTab[] = {
234 { VAL_UNIMARC, "UNIMARC" },
235 { VAL_INTERMARC, "INTERMARC" },
237 { VAL_USMARC, "USMARC" },
238 { VAL_UKMARC, "UKMARC" },
239 { VAL_NORMARC, "NORMARC" },
240 { VAL_LIBRISMARC, "LIBRISMARC" },
241 { VAL_DANMARC, "DANMARC" },
242 { VAL_FINMARC, "FINMARC" },
244 { VAL_CANMARC, "CANMARC" },
246 { VAL_PICAMARC, "PICAMARC" },
247 { VAL_AUSMARC, "AUSMARC" },
248 { VAL_IBERMARC, "IBERMARC" },
249 { VAL_SUTRS, "SUTRS" },
256 int IrTcl_eval (Tcl_Interp *interp, const char *command)
258 char *tmp = malloc (strlen(command)+1);
263 logf (LOG_FATAL, "Out of memory in IrTcl_eval");
266 strcpy (tmp, command);
267 r = Tcl_Eval (interp, tmp);
269 logf (LOG_WARN, "Tcl error in line %d: %s", interp->errorLine,
271 Tcl_FreeResult (interp);
277 * IrTcl_getRecordSyntaxStr: Return record syntax name of object id
279 static const char *IrTcl_getRecordSyntaxStr (enum oid_value value)
282 for (i = 0; IrTcl_recordSyntaxTab[i].name; i++)
283 if (IrTcl_recordSyntaxTab[i].value == value)
284 return IrTcl_recordSyntaxTab[i].name;
289 * IrTcl_getRecordSyntaxVal: Return record syntax value of string
291 static enum oid_value IrTcl_getRecordSyntaxVal (const char *name)
294 for (i = 0; IrTcl_recordSyntaxTab[i].name; i++)
295 if (!strcmp (IrTcl_recordSyntaxTab[i].name, name))
296 return IrTcl_recordSyntaxTab[i].value;
300 static IrTcl_RecordList *find_IR_record (IrTcl_SetObj *setobj, int no)
302 IrTcl_RecordList *rl;
304 for (rl = setobj->record_list; rl; rl = rl->next)
310 static void delete_IR_records (IrTcl_SetObj *setobj)
312 IrTcl_RecordList *rl, *rl1;
314 for (rl = setobj->record_list; rl; rl = rl1)
318 case Z_NamePlusRecord_databaseRecord:
319 free (rl->u.dbrec.buf);
321 case Z_NamePlusRecord_surrogateDiagnostic:
322 ir_deleteDiags (&rl->u.surrogateDiagnostics.list,
323 &rl->u.surrogateDiagnostics.num);
329 setobj->record_list = NULL;
333 * get_set_int: Set/get integer value
335 static int get_set_int (int *val, Tcl_Interp *interp, int argc, char **argv)
341 if (Tcl_GetInt (interp, argv[2], val)==TCL_ERROR)
344 sprintf (buf, "%d", *val);
345 Tcl_AppendResult (interp, buf, NULL);
350 * ir_method: Search for method in table and invoke method handler
352 int ir_method (Tcl_Interp *interp, int argc, char **argv, IrTcl_Methods *tab)
354 IrTcl_Methods *tab_i = tab;
357 for (tab_i = tab; tab_i->tab; tab_i++)
358 for (t = tab_i->tab; t->name; t++)
361 if ((*t->method)(tab_i->obj, interp, argc, argv) == TCL_ERROR)
365 if (!strcmp (t->name, argv[1]))
366 return (*t->method)(tab_i->obj, interp, argc, argv);
370 Tcl_AppendResult (interp, "Bad method. Possible methods:", NULL);
371 for (tab_i = tab; tab_i->tab; tab_i++)
372 for (t = tab_i->tab; t->name; t++)
373 Tcl_AppendResult (interp, " ", t->name, NULL);
378 * ir_method_r: Get status for all readable elements
380 int ir_method_r (void *obj, Tcl_Interp *interp, int argc, char **argv,
388 for (; tab->name; tab++)
391 argv_n[1] = tab->name;
392 Tcl_AppendResult (interp, "{", NULL);
393 (*tab->method)(obj, interp, argc_n, argv_n);
394 Tcl_AppendResult (interp, "} ", NULL);
400 * ir_named_bits: get/set named bits
402 int ir_named_bits (struct ir_named_entry *tab, Odr_bitmask *ob,
403 Tcl_Interp *interp, int argc, char **argv)
405 struct ir_named_entry *ti;
410 for (no = 0; no < argc; no++)
412 for (ti = tab; ti->name; ti++)
413 if (!strcmp (argv[no], ti->name))
415 ODR_MASK_SET (ob, ti->pos);
420 Tcl_AppendResult (interp, "Bad bit mask: ", argv[no], NULL);
426 for (ti = tab; ti->name; ti++)
427 if (ODR_MASK_GET (ob, ti->pos))
428 Tcl_AppendElement (interp, ti->name);
433 * ir_strdup: Duplicate string
435 int ir_strdup (Tcl_Interp *interp, char** p, const char *s)
442 *p = malloc (strlen(s)+1);
445 interp->result = "strdup fail";
453 * ir_strdel: Delete string
455 int ir_strdel (Tcl_Interp *interp, char **p)
463 * ir_malloc: Malloc function
465 void *ir_malloc (Tcl_Interp *interp, size_t size)
467 static char buf[128];
468 void *p = malloc (size);
472 sprintf (buf, "Malloc fail. %ld bytes requested", (long) size);
473 interp->result = buf;
479 static void set_referenceId (ODR o, Z_ReferenceId **dst, const char *src)
485 *dst = odr_malloc (o, sizeof(**dst));
486 (*dst)->size = (*dst)->len = strlen(src);
487 (*dst)->buf = odr_malloc (o, (*dst)->len);
488 memcpy ((*dst)->buf, src, (*dst)->len);
492 static void get_referenceId (char **dst, Z_ReferenceId *src)
500 *dst = malloc (src->len+1);
501 memcpy (*dst, src->buf, src->len);
502 (*dst)[src->len] = '\0';
505 /* ------------------------------------------------------- */
508 * do_init_request: init method on IR object
510 static int do_init_request (void *obj, Tcl_Interp *interp,
511 int argc, char **argv)
522 interp->result = "not connected";
525 odr_reset (p->odr_out);
526 apdu = zget_APDU (p->odr_out, Z_APDU_initRequest);
527 req = apdu->u.initRequest;
529 set_referenceId (p->odr_out, &req->referenceId, p->set_inher.referenceId);
530 req->options = &p->options;
531 req->protocolVersion = &p->protocolVersion;
532 req->preferredMessageSize = &p->preferredMessageSize;
533 req->maximumRecordSize = &p->maximumRecordSize;
535 if (p->idAuthenticationGroupId)
537 Z_IdPass *pass = odr_malloc (p->odr_out, sizeof(*pass));
538 Z_IdAuthentication *auth = odr_malloc (p->odr_out, sizeof(*auth));
540 auth->which = Z_IdAuthentication_idPass;
541 auth->u.idPass = pass;
542 if (p->idAuthenticationGroupId && *p->idAuthenticationGroupId)
543 pass->groupId = p->idAuthenticationGroupId;
545 pass->groupId = NULL;
546 if (p->idAuthenticationUserId && *p->idAuthenticationUserId)
547 pass->userId = p->idAuthenticationUserId;
550 if (p->idAuthenticationPassword && *p->idAuthenticationPassword)
551 pass->password = p->idAuthenticationPassword;
553 pass->password = NULL;
554 req->idAuthentication = auth;
556 else if (!p->idAuthenticationOpen || !*p->idAuthenticationOpen)
557 req->idAuthentication = NULL;
560 Z_IdAuthentication *auth = odr_malloc (p->odr_out, sizeof(*auth));
562 auth->which = Z_IdAuthentication_open;
563 auth->u.open = p->idAuthenticationOpen;
564 req->idAuthentication = auth;
566 req->implementationId = p->implementationId;
567 req->implementationName = p->implementationName;
568 req->implementationVersion = p->implementationVersion;
569 req->userInformationField = 0;
571 if (!z_APDU (p->odr_out, &apdu, 0))
573 Tcl_AppendResult (interp, odr_errlist [odr_geterror (p->odr_out)],
575 odr_reset (p->odr_out);
578 p->sbuf = odr_getbuf (p->odr_out, &p->slen, NULL);
579 if ((r=cs_put (p->cs_link, p->sbuf, p->slen)) < 0)
581 interp->result = "cs_put failed in init";
582 do_disconnect (p, NULL, 2, NULL);
587 ir_select_add_write (cs_fileno(p->cs_link), p);
588 logf (LOG_DEBUG, "Sent part of initializeRequest (%d bytes)", p->slen);
591 logf (LOG_DEBUG, "Sent whole initializeRequest (%d bytes)", p->slen);
596 * do_protocolVersion: Set protocol Version
598 static int do_protocolVersion (void *obj, Tcl_Interp *interp,
599 int argc, char **argv)
607 ODR_MASK_ZERO (&p->protocolVersion);
608 ODR_MASK_SET (&p->protocolVersion, 0);
609 ODR_MASK_SET (&p->protocolVersion, 1);
614 if (Tcl_GetInt (interp, argv[2], &version)==TCL_ERROR)
616 ODR_MASK_ZERO (&p->protocolVersion);
617 for (i = 0; i<version; i++)
618 ODR_MASK_SET (&p->protocolVersion, i);
620 for (i = 4; --i >= 0; )
621 if (ODR_MASK_GET (&p->protocolVersion, i))
623 sprintf (buf, "%d", i+1);
624 interp->result = buf;
629 * do_options: Set options
631 static int do_options (void *obj, Tcl_Interp *interp,
632 int argc, char **argv)
634 static struct ir_named_entry options_tab[] = {
638 { "resourceReport", 3 },
639 { "triggerResourceCtrl", 4},
640 { "resourceCtrl", 5},
644 { "extendedServices", 10},
645 { "level-1Segmentation", 11},
646 { "level-2Segmentation", 12},
647 { "concurrentOperations", 13},
648 { "namedResultSets", 14},
655 ODR_MASK_ZERO (&p->options);
656 ODR_MASK_SET (&p->options, 0);
657 ODR_MASK_SET (&p->options, 1);
658 ODR_MASK_SET (&p->options, 7);
659 ODR_MASK_SET (&p->options, 14);
662 return ir_named_bits (options_tab, &p->options, interp, argc-2, argv+2);
666 * do_failInfo: Get fail information
668 static int do_failInfo (void *obj, Tcl_Interp *interp, int argc, char **argv)
678 sprintf (buf, "%d", p->failInfo);
684 case IR_TCL_FAIL_CONNECT:
685 cp = "connect failed";
687 case IR_TCL_FAIL_READ:
688 cp = "connection closed";
690 case IR_TCL_FAIL_WRITE:
691 cp = "connection closed";
693 case IR_TCL_FAIL_IN_APDU:
694 cp = "failed to decode incoming APDU";
696 case IR_TCL_FAIL_UNKNOWN_APDU:
702 Tcl_AppendElement (interp, buf);
703 Tcl_AppendElement (interp, cp);
708 * do_preferredMessageSize: Set/get preferred message size
710 static int do_preferredMessageSize (void *obj, Tcl_Interp *interp,
711 int argc, char **argv)
717 p->preferredMessageSize = 30000;
720 return get_set_int (&p->preferredMessageSize, interp, argc, argv);
724 * do_maximumRecordSize: Set/get maximum record size
726 static int do_maximumRecordSize (void *obj, Tcl_Interp *interp,
727 int argc, char **argv)
733 p->maximumRecordSize = 30000;
736 return get_set_int (&p->maximumRecordSize, interp, argc, argv);
740 * do_initResult: Get init result
742 static int do_initResult (void *obj, Tcl_Interp *interp,
743 int argc, char **argv)
749 return get_set_int (&p->initResult, interp, argc, argv);
754 * do_implementationName: Set/get Implementation Name.
756 static int do_implementationName (void *obj, Tcl_Interp *interp,
757 int argc, char **argv)
762 return ir_strdup (interp, &p->implementationName,
763 "Index Data/IrTcl on YAZ");
765 return ir_strdel (interp, &p->implementationName);
768 free (p->implementationName);
769 if (ir_strdup (interp, &p->implementationName, argv[2])
773 Tcl_AppendResult (interp, p->implementationName, (char*) NULL);
778 * do_implementationId: Get Implementation Id.
780 static int do_implementationId (void *obj, Tcl_Interp *interp,
781 int argc, char **argv)
786 return ir_strdup (interp, &p->implementationId, "YAZ (id=81)");
788 return ir_strdel (interp, &p->implementationId);
789 Tcl_AppendResult (interp, p->implementationId, (char*) NULL);
794 * do_implementationVersion: get Implementation Version.
796 static int do_implementationVersion (void *obj, Tcl_Interp *interp,
797 int argc, char **argv)
802 return ir_strdup (interp, &p->implementationVersion,
803 "YAZ: " YAZ_VERSION " / IrTcl: " IR_TCL_VERSION);
805 return ir_strdel (interp, &p->implementationVersion);
806 Tcl_AppendResult (interp, p->implementationVersion, (char*) NULL);
811 * do_targetImplementationName: Get Implementation Name of target.
813 static int do_targetImplementationName (void *obj, Tcl_Interp *interp,
814 int argc, char **argv)
820 p->targetImplementationName = NULL;
824 return ir_strdel (interp, &p->targetImplementationName);
825 Tcl_AppendResult (interp, p->targetImplementationName, (char*) NULL);
830 * do_targetImplementationId: Get Implementation Id of target
832 static int do_targetImplementationId (void *obj, Tcl_Interp *interp,
833 int argc, char **argv)
839 p->targetImplementationId = NULL;
843 return ir_strdel (interp, &p->targetImplementationId);
844 Tcl_AppendResult (interp, p->targetImplementationId, (char*) NULL);
849 * do_targetImplementationVersion: Get Implementation Version of target
851 static int do_targetImplementationVersion (void *obj, Tcl_Interp *interp,
852 int argc, char **argv)
858 p->targetImplementationVersion = NULL;
862 return ir_strdel (interp, &p->targetImplementationVersion);
863 Tcl_AppendResult (interp, p->targetImplementationVersion, (char*) NULL);
868 * do_idAuthentication: Set/get id Authentication
870 static int do_idAuthentication (void *obj, Tcl_Interp *interp,
871 int argc, char **argv)
875 if (argc >= 3 || argc == -1)
877 free (p->idAuthenticationOpen);
878 free (p->idAuthenticationGroupId);
879 free (p->idAuthenticationUserId);
880 free (p->idAuthenticationPassword);
882 if (argc >= 3 || argc <= 0)
884 p->idAuthenticationOpen = NULL;
885 p->idAuthenticationGroupId = NULL;
886 p->idAuthenticationUserId = NULL;
887 p->idAuthenticationPassword = NULL;
895 if (ir_strdup (interp, &p->idAuthenticationOpen, argv[2])
901 if (ir_strdup (interp, &p->idAuthenticationGroupId, argv[2])
904 if (ir_strdup (interp, &p->idAuthenticationUserId, argv[3])
907 if (ir_strdup (interp, &p->idAuthenticationPassword, argv[4])
912 if (p->idAuthenticationOpen)
913 Tcl_AppendElement (interp, p->idAuthenticationOpen);
914 else if (p->idAuthenticationGroupId)
916 Tcl_AppendElement (interp, p->idAuthenticationGroupId);
917 Tcl_AppendElement (interp, p->idAuthenticationUserId);
918 Tcl_AppendElement (interp, p->idAuthenticationPassword);
924 * do_connect: connect method on IR object
926 static int do_connect (void *obj, Tcl_Interp *interp,
927 int argc, char **argv)
939 interp->result = "already connected";
942 if (!strcmp (p->cs_type, "tcpip"))
944 p->cs_link = cs_create (tcpip_type, CS_BLOCK, p->protocol_type);
945 addr = tcpip_strtoaddr (argv[2]);
948 interp->result = "tcpip_strtoaddr fail";
951 logf (LOG_DEBUG, "tcp/ip connect %s", argv[2]);
953 else if (!strcmp (p->cs_type, "mosi"))
956 p->cs_link = cs_create (mosi_type, CS_BLOCK, p->protocol_type);
957 addr = mosi_strtoaddr (argv[2]);
960 interp->result = "mosi_strtoaddr fail";
963 logf (LOG_DEBUG, "mosi connect %s", argv[2]);
965 interp->result = "MOSI support not there";
971 Tcl_AppendResult (interp, "Bad comstack type: ",
975 if (ir_strdup (interp, &p->hostname, argv[2]) == TCL_ERROR)
977 if ((r=cs_connect (p->cs_link, addr)) < 0)
979 interp->result = "connect fail";
980 do_disconnect (p, NULL, 2, NULL);
983 ir_select_add (cs_fileno (p->cs_link), p);
986 ir_select_add_write (cs_fileno (p->cs_link), p);
993 IrTcl_eval (p->interp, p->callback);
997 Tcl_AppendElement (interp, p->hostname);
1002 * do_disconnect: disconnect method on IR object
1004 static int do_disconnect (void *obj, Tcl_Interp *interp,
1005 int argc, char **argv)
1020 ir_select_remove_write (cs_fileno (p->cs_link), p);
1021 ir_select_remove (cs_fileno (p->cs_link), p);
1023 assert (p->cs_link);
1024 cs_close (p->cs_link);
1027 ODR_MASK_ZERO (&p->options);
1028 ODR_MASK_SET (&p->options, 0);
1029 ODR_MASK_SET (&p->options, 1);
1030 ODR_MASK_SET (&p->options, 7);
1031 ODR_MASK_SET (&p->options, 14);
1033 ODR_MASK_ZERO (&p->protocolVersion);
1034 ODR_MASK_SET (&p->protocolVersion, 0);
1035 ODR_MASK_SET (&p->protocolVersion, 1);
1037 assert (!p->cs_link);
1042 * do_comstack: Set/get comstack method on IR object
1044 static int do_comstack (void *o, Tcl_Interp *interp,
1045 int argc, char **argv)
1050 return ir_strdup (interp, &obj->cs_type, "tcpip");
1051 else if (argc == -1)
1052 return ir_strdel (interp, &obj->cs_type);
1055 free (obj->cs_type);
1056 if (ir_strdup (interp, &obj->cs_type, argv[2]) == TCL_ERROR)
1059 Tcl_AppendElement (interp, obj->cs_type);
1065 * do_callback: add callback
1067 static int do_callback (void *obj, Tcl_Interp *interp,
1068 int argc, char **argv)
1077 else if (argc == -1)
1078 return ir_strdel (interp, &p->callback);
1084 if (ir_strdup (interp, &p->callback, argv[2]) == TCL_ERROR)
1095 * do_failback: add error handle callback
1097 static int do_failback (void *obj, Tcl_Interp *interp,
1098 int argc, char **argv)
1107 else if (argc == -1)
1108 return ir_strdel (interp, &p->failback);
1114 if (ir_strdup (interp, &p->failback, argv[2]) == TCL_ERROR)
1125 * do_protocol: Set/get protocol method on IR object
1127 static int do_protocol (void *o, Tcl_Interp *interp, int argc, char **argv)
1133 p->protocol_type = PROTO_Z3950;
1138 if (!strcmp (argv[2], "Z39"))
1139 p->protocol_type = PROTO_Z3950;
1140 else if (!strcmp (argv[2], "SR"))
1141 p->protocol_type = PROTO_SR;
1144 Tcl_AppendResult (interp, "Bad protocol: ", argv[2], NULL);
1149 switch (p->protocol_type)
1152 Tcl_AppendElement (interp, "Z39");
1155 Tcl_AppendElement (interp, "SR");
1162 * do_triggerResourceControl:
1164 static int do_triggerResourceControl (void *obj, Tcl_Interp *interp,
1165 int argc, char **argv)
1169 Z_TriggerResourceControlRequest *req;
1176 interp->result = "not connected";
1179 apdu = zget_APDU (p->odr_out, Z_APDU_triggerResourceControlRequest);
1180 req = apdu->u.triggerResourceControlRequest;
1182 if (!z_APDU (p->odr_out, &apdu, 0))
1184 Tcl_AppendResult (interp, odr_errlist [odr_geterror (p->odr_out)],
1186 odr_reset (p->odr_out);
1189 p->sbuf = odr_getbuf (p->odr_out, &p->slen, NULL);
1190 if ((r=cs_put (p->cs_link, p->sbuf, p->slen)) < 0)
1192 interp->result = "cs_put failed in triggerResourceControl";
1193 do_disconnect (p, NULL, 2, NULL);
1198 ir_select_add_write (cs_fileno(p->cs_link), p);
1199 logf (LOG_DEBUG, "Sent part of triggerResourceControl (%d bytes)",
1203 logf (LOG_DEBUG, "Sent whole of triggerResourceControl (%d bytes)",
1209 * do_databaseNames: specify database names
1211 static int do_databaseNames (void *obj, Tcl_Interp *interp,
1212 int argc, char **argv)
1215 IrTcl_SetCObj *p = obj;
1219 for (i=0; i<p->num_databaseNames; i++)
1220 free (p->databaseNames[i]);
1221 free (p->databaseNames);
1225 p->num_databaseNames = 0;
1226 p->databaseNames = NULL;
1231 for (i=0; i<p->num_databaseNames; i++)
1232 Tcl_AppendElement (interp, p->databaseNames[i]);
1235 if (p->databaseNames)
1237 for (i=0; i<p->num_databaseNames; i++)
1238 free (p->databaseNames[i]);
1239 free (p->databaseNames);
1241 p->num_databaseNames = argc - 2;
1242 if (!(p->databaseNames = ir_malloc (interp,
1243 sizeof(*p->databaseNames) * p->num_databaseNames)))
1245 for (i=0; i<p->num_databaseNames; i++)
1247 if (ir_strdup (interp, &p->databaseNames[i], argv[2+i])
1255 * do_replaceIndicator: Set/get replace Set indicator
1257 static int do_replaceIndicator (void *obj, Tcl_Interp *interp,
1258 int argc, char **argv)
1260 IrTcl_SetCObj *p = obj;
1264 p->replaceIndicator = 1;
1267 return get_set_int (&p->replaceIndicator, interp, argc, argv);
1271 * do_queryType: Set/Get query method
1273 static int do_queryType (void *obj, Tcl_Interp *interp,
1274 int argc, char **argv)
1276 IrTcl_SetCObj *p = obj;
1279 return ir_strdup (interp, &p->queryType, "rpn");
1280 else if (argc == -1)
1281 return ir_strdel (interp, &p->queryType);
1284 free (p->queryType);
1285 if (ir_strdup (interp, &p->queryType, argv[2]) == TCL_ERROR)
1288 Tcl_AppendResult (interp, p->queryType, NULL);
1293 * do_userInformationField: Get User information field
1295 static int do_userInformationField (void *obj, Tcl_Interp *interp,
1296 int argc, char **argv)
1302 p->userInformationField = NULL;
1305 else if (argc == -1)
1306 return ir_strdel (interp, &p->userInformationField);
1307 Tcl_AppendResult (interp, p->userInformationField, NULL);
1312 * do_smallSetUpperBound: Set/get small set upper bound
1314 static int do_smallSetUpperBound (void *o, Tcl_Interp *interp,
1315 int argc, char **argv)
1317 IrTcl_SetCObj *p = o;
1321 p->smallSetUpperBound = 0;
1324 return get_set_int (&p->smallSetUpperBound, interp, argc, argv);
1328 * do_largeSetLowerBound: Set/get large set lower bound
1330 static int do_largeSetLowerBound (void *o, Tcl_Interp *interp,
1331 int argc, char **argv)
1333 IrTcl_SetCObj *p = o;
1337 p->largeSetLowerBound = 2;
1340 return get_set_int (&p->largeSetLowerBound, interp, argc, argv);
1344 * do_mediumSetPresentNumber: Set/get large set lower bound
1346 static int do_mediumSetPresentNumber (void *o, Tcl_Interp *interp,
1347 int argc, char **argv)
1349 IrTcl_SetCObj *p = o;
1353 p->mediumSetPresentNumber = 0;
1356 return get_set_int (&p->mediumSetPresentNumber, interp, argc, argv);
1360 * do_referenceId: Set/Get referenceId
1362 static int do_referenceId (void *obj, Tcl_Interp *interp,
1363 int argc, char **argv)
1365 IrTcl_SetCObj *p = obj;
1369 p->referenceId = NULL;
1372 else if (argc == -1)
1373 return ir_strdel (interp, &p->referenceId);
1376 free (p->referenceId);
1377 if (ir_strdup (interp, &p->referenceId, argv[2]) == TCL_ERROR)
1380 Tcl_AppendResult (interp, p->referenceId, NULL);
1385 * do_preferredRecordSyntax: Set/get preferred record syntax
1387 static int do_preferredRecordSyntax (void *obj, Tcl_Interp *interp,
1388 int argc, char **argv)
1390 IrTcl_SetCObj *p = obj;
1394 p->preferredRecordSyntax = NULL;
1397 else if (argc == -1)
1399 free (p->preferredRecordSyntax);
1400 p->preferredRecordSyntax = NULL;
1405 free (p->preferredRecordSyntax);
1406 p->preferredRecordSyntax = NULL;
1407 if (argv[2][0] && (p->preferredRecordSyntax =
1408 malloc (sizeof(*p->preferredRecordSyntax))))
1409 *p->preferredRecordSyntax = IrTcl_getRecordSyntaxVal (argv[2]);
1414 static IrTcl_Method ir_method_tab[] = {
1415 { 1, "comstack", do_comstack },
1416 { 1, "protocol", do_protocol },
1417 { 0, "failback", do_failback },
1418 { 0, "failInfo", do_failInfo },
1420 { 1, "connect", do_connect },
1421 { 0, "protocolVersion", do_protocolVersion },
1422 { 1, "preferredMessageSize", do_preferredMessageSize },
1423 { 1, "maximumRecordSize", do_maximumRecordSize },
1424 { 1, "implementationName", do_implementationName },
1425 { 1, "implementationId", do_implementationId },
1426 { 1, "implementationVersion", do_implementationVersion },
1427 { 0, "targetImplementationName", do_targetImplementationName },
1428 { 0, "targetImplementationId", do_targetImplementationId },
1429 { 0, "targetImplementationVersion", do_targetImplementationVersion },
1430 { 0, "userInformationField", do_userInformationField },
1431 { 1, "idAuthentication", do_idAuthentication },
1432 { 0, "options", do_options },
1433 { 0, "init", do_init_request },
1434 { 0, "initResult", do_initResult },
1435 { 0, "disconnect", do_disconnect },
1436 { 0, "callback", do_callback },
1437 { 0, "triggerResourceControl", do_triggerResourceControl },
1441 static IrTcl_Method ir_set_c_method_tab[] = {
1442 { 0, "databaseNames", do_databaseNames},
1443 { 0, "replaceIndicator", do_replaceIndicator},
1444 { 0, "queryType", do_queryType },
1445 { 0, "preferredRecordSyntax", do_preferredRecordSyntax },
1446 { 0, "smallSetUpperBound", do_smallSetUpperBound},
1447 { 0, "largeSetLowerBound", do_largeSetLowerBound},
1448 { 0, "mediumSetPresentNumber", do_mediumSetPresentNumber},
1449 { 0, "referenceId", do_referenceId },
1454 * ir_obj_method: IR Object methods
1456 static int ir_obj_method (ClientData clientData, Tcl_Interp *interp,
1457 int argc, char **argv)
1459 IrTcl_Methods tab[3];
1460 IrTcl_Obj *p = clientData;
1463 return ir_method_r (clientData, interp, argc, argv, ir_method_tab);
1465 tab[0].tab = ir_method_tab;
1467 tab[1].tab = ir_set_c_method_tab;
1468 tab[1].obj = &p->set_inher;
1471 return ir_method (interp, argc, argv, tab);
1475 * ir_obj_delete: IR Object disposal
1477 static void ir_obj_delete (ClientData clientData)
1479 IrTcl_Obj *obj = clientData;
1480 IrTcl_Methods tab[3];
1483 if (obj->ref_count > 0)
1485 assert (obj->ref_count == 0);
1487 tab[0].tab = ir_method_tab;
1489 tab[1].tab = ir_set_c_method_tab;
1490 tab[1].obj = &obj->set_inher;
1493 ir_method (NULL, -1, NULL, tab);
1494 odr_destroy (obj->odr_in);
1495 odr_destroy (obj->odr_out);
1496 odr_destroy (obj->odr_pr);
1497 free (obj->buf_out);
1503 * ir_obj_mk: IR Object creation
1505 static int ir_obj_mk (ClientData clientData, Tcl_Interp *interp,
1506 int argc, char **argv)
1508 IrTcl_Methods tab[3];
1516 interp->result = "wrong # args";
1519 if (!(obj = ir_malloc (interp, sizeof(*obj))))
1524 obj->bibset = ccl_qual_mk ();
1525 if ((inf = fopen ("default.bib", "r")))
1527 ccl_qual_file (obj->bibset, inf);
1532 obj->odr_in = odr_createmem (ODR_DECODE);
1533 obj->odr_out = odr_createmem (ODR_ENCODE);
1534 obj->odr_pr = odr_createmem (ODR_PRINT);
1536 obj->len_out = 10000;
1537 if (!(obj->buf_out = ir_malloc (interp, obj->len_out)))
1539 odr_setbuf (obj->odr_out, obj->buf_out, obj->len_out, 0);
1544 tab[0].tab = ir_method_tab;
1546 tab[1].tab = ir_set_c_method_tab;
1547 tab[1].obj = &obj->set_inher;
1550 if (ir_method (interp, 0, NULL, tab) == TCL_ERROR)
1552 Tcl_CreateCommand (interp, argv[1], ir_obj_method,
1553 (ClientData) obj, ir_obj_delete);
1557 /* ------------------------------------------------------- */
1559 * do_search: Do search request
1561 static int do_search (void *o, Tcl_Interp *interp, int argc, char **argv)
1563 Z_SearchRequest *req;
1567 IrTcl_SetObj *obj = o;
1579 interp->result = "wrong # args";
1582 if (!obj->set_inher.num_databaseNames)
1584 interp->result = "no databaseNames";
1589 interp->result = "not connected";
1592 odr_reset (p->odr_out);
1593 apdu = zget_APDU (p->odr_out, Z_APDU_searchRequest);
1594 req = apdu->u.searchRequest;
1596 bib1.proto = p->protocol_type;
1597 bib1.class = CLASS_ATTSET;
1598 bib1.value = VAL_BIB1;
1600 set_referenceId (p->odr_out, &req->referenceId,
1601 obj->set_inher.referenceId);
1603 req->smallSetUpperBound = &obj->set_inher.smallSetUpperBound;
1604 req->largeSetLowerBound = &obj->set_inher.largeSetLowerBound;
1605 req->mediumSetPresentNumber = &obj->set_inher.mediumSetPresentNumber;
1606 req->replaceIndicator = &obj->set_inher.replaceIndicator;
1607 req->resultSetName = obj->setName ? obj->setName : "Default";
1608 logf (LOG_DEBUG, "Search, resultSetName %s", req->resultSetName);
1609 req->num_databaseNames = obj->set_inher.num_databaseNames;
1610 req->databaseNames = obj->set_inher.databaseNames;
1611 for (r=0; r < obj->set_inher.num_databaseNames; r++)
1612 logf (LOG_DEBUG, " Database %s", obj->set_inher.databaseNames[r]);
1613 req->smallSetElementSetNames = 0;
1614 req->mediumSetElementSetNames = 0;
1615 if (obj->set_inher.preferredRecordSyntax)
1617 struct oident ident;
1619 ident.proto = p->protocol_type;
1620 ident.class = CLASS_RECSYN;
1621 ident.value = *obj->set_inher.preferredRecordSyntax;
1622 logf (LOG_DEBUG, "Preferred record syntax is %d", ident.value);
1623 req->preferredRecordSyntax = odr_oiddup (p->odr_out,
1624 oid_getoidbyent (&ident));
1627 req->preferredRecordSyntax = 0;
1628 req->query = &query;
1630 if (!strcmp (obj->set_inher.queryType, "rpn"))
1632 Z_RPNQuery *RPNquery;
1634 RPNquery = p_query_rpn (p->odr_out, argv[2]);
1637 Tcl_AppendResult (interp, "Syntax error in query", NULL);
1640 RPNquery->attributeSetId = oid_getoidbyent (&bib1);
1641 query.which = Z_Query_type_1;
1642 query.u.type_1 = RPNquery;
1643 logf (LOG_DEBUG, "RPN");
1646 else if (!strcmp (obj->set_inher.queryType, "cclrpn"))
1650 struct ccl_rpn_node *rpn;
1651 Z_RPNQuery *RPNquery;
1653 rpn = ccl_find_str(p->bibset, argv[2], &error, &pos);
1656 Tcl_AppendResult (interp, "CCL error: ",
1657 ccl_err_msg(error), NULL);
1660 ccl_pr_tree (rpn, stderr);
1661 fprintf (stderr, "\n");
1662 assert((RPNquery = ccl_rpn_query(rpn)));
1663 RPNquery->attributeSetId = oid_getoidbyent (&bib1);
1664 query.which = Z_Query_type_1;
1665 query.u.type_1 = RPNquery;
1666 logf (LOG_DEBUG, "CCLRPN");
1669 else if (!strcmp (obj->set_inher.queryType, "ccl"))
1671 query.which = Z_Query_type_2;
1672 query.u.type_2 = &ccl_query;
1673 ccl_query.buf = (unsigned char *) argv[2];
1674 ccl_query.len = strlen (argv[2]);
1675 logf (LOG_DEBUG, "CCL");
1679 interp->result = "unknown query method";
1682 if (!z_APDU (p->odr_out, &apdu, 0))
1684 interp->result = odr_errlist [odr_geterror (p->odr_out)];
1685 odr_reset (p->odr_out);
1688 p->sbuf = odr_getbuf (p->odr_out, &p->slen, NULL);
1689 if ((r=cs_put (p->cs_link, p->sbuf, p->slen)) < 0)
1691 interp->result = "cs_put failed in search";
1696 ir_select_add_write (cs_fileno(p->cs_link), p);
1697 logf (LOG_DEBUG, "Sent part of searchRequest (%d bytes)", p->slen);
1701 logf (LOG_DEBUG, "Whole search request (%d bytes)", p->slen);
1707 * do_resultCount: Get number of hits
1709 static int do_resultCount (void *o, Tcl_Interp *interp,
1710 int argc, char **argv)
1712 IrTcl_SetObj *obj = o;
1716 return get_set_int (&obj->resultCount, interp, argc, argv);
1720 * do_searchStatus: Get search status (after search response)
1722 static int do_searchStatus (void *o, Tcl_Interp *interp,
1723 int argc, char **argv)
1725 IrTcl_SetObj *obj = o;
1729 return get_set_int (&obj->searchStatus, interp, argc, argv);
1733 * do_presentStatus: Get search status (after search/present response)
1735 static int do_presentStatus (void *o, Tcl_Interp *interp,
1736 int argc, char **argv)
1738 IrTcl_SetObj *obj = o;
1742 return get_set_int (&obj->presentStatus, interp, argc, argv);
1746 * do_nextResultSetPosition: Get next result set position
1747 * (after search/present response)
1749 static int do_nextResultSetPosition (void *o, Tcl_Interp *interp,
1750 int argc, char **argv)
1752 IrTcl_SetObj *obj = o;
1756 obj->nextResultSetPosition = 0;
1759 return get_set_int (&obj->nextResultSetPosition, interp, argc, argv);
1763 * do_setName: Set result Set name
1765 static int do_setName (void *o, Tcl_Interp *interp,
1766 int argc, char **argv)
1768 IrTcl_SetObj *obj = o;
1771 return ir_strdup (interp, &obj->setName, "Default");
1772 else if (argc == -1)
1773 return ir_strdel (interp, &obj->setName);
1776 free (obj->setName);
1777 if (ir_strdup (interp, &obj->setName, argv[2])
1781 Tcl_AppendElement (interp, obj->setName);
1786 * do_numberOfRecordsReturned: Get number of records returned
1788 static int do_numberOfRecordsReturned (void *o, Tcl_Interp *interp,
1789 int argc, char **argv)
1791 IrTcl_SetObj *obj = o;
1795 obj->numberOfRecordsReturned = 0;
1798 return get_set_int (&obj->numberOfRecordsReturned, interp, argc, argv);
1802 * do_type: Return type (if any) at position.
1804 static int do_type (void *o, Tcl_Interp *interp, int argc, char **argv)
1806 IrTcl_SetObj *obj = o;
1808 IrTcl_RecordList *rl;
1812 obj->record_list = NULL;
1815 else if (argc == -1)
1817 delete_IR_records (obj);
1822 sprintf (interp->result, "wrong # args");
1825 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
1827 rl = find_IR_record (obj, offset);
1832 case Z_NamePlusRecord_databaseRecord:
1833 interp->result = "DB";
1835 case Z_NamePlusRecord_surrogateDiagnostic:
1836 interp->result = "SD";
1844 * do_recordType: Return record type (if any) at position.
1846 static int do_recordType (void *o, Tcl_Interp *interp, int argc, char **argv)
1848 IrTcl_SetObj *obj = o;
1850 IrTcl_RecordList *rl;
1856 else if (argc == -1)
1862 sprintf (interp->result, "wrong # args");
1865 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
1867 rl = find_IR_record (obj, offset);
1870 if (rl->which != Z_NamePlusRecord_databaseRecord)
1872 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
1875 Tcl_AppendElement (interp, (char*)
1876 IrTcl_getRecordSyntaxStr (rl->u.dbrec.type));
1883 static int ir_diagResult (Tcl_Interp *interp, IrTcl_Diagnostic *list, int num)
1889 for (i = 0; i<num; i++)
1891 logf (LOG_DEBUG, "Diagnostic, code %d", list[i].condition);
1892 sprintf (buf, "%d", list[i].condition);
1893 Tcl_AppendElement (interp, buf);
1894 cp = diagbib1_str (list[i].condition);
1896 Tcl_AppendElement (interp, (char*) cp);
1898 Tcl_AppendElement (interp, "");
1899 if (list[i].addinfo)
1900 Tcl_AppendElement (interp, (char*) list[i].addinfo);
1902 Tcl_AppendElement (interp, "");
1908 * do_diag: Return diagnostic record info
1910 static int do_diag (void *o, Tcl_Interp *interp, int argc, char **argv)
1912 IrTcl_SetObj *obj = o;
1914 IrTcl_RecordList *rl;
1920 sprintf (interp->result, "wrong # args");
1923 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
1925 rl = find_IR_record (obj, offset);
1928 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
1931 if (rl->which != Z_NamePlusRecord_surrogateDiagnostic)
1933 Tcl_AppendResult (interp, "No Diagnostic record at #", argv[2], NULL);
1936 return ir_diagResult (interp, rl->u.surrogateDiagnostics.list,
1937 rl->u.surrogateDiagnostics.num);
1941 * do_getMarc: Get ISO2709 Record lines/fields
1943 static int do_getMarc (void *o, Tcl_Interp *interp, int argc, char **argv)
1945 IrTcl_SetObj *obj = o;
1947 IrTcl_RecordList *rl;
1953 sprintf (interp->result, "wrong # args");
1956 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
1958 rl = find_IR_record (obj, offset);
1961 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
1964 if (rl->which != Z_NamePlusRecord_databaseRecord)
1966 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
1969 return ir_tcl_get_marc (interp, rl->u.dbrec.buf, argc, argv);
1973 * do_getSutrs: Get SUTRS Record
1975 static int do_getSutrs (void *o, Tcl_Interp *interp, int argc, char **argv)
1977 IrTcl_SetObj *obj = o;
1979 IrTcl_RecordList *rl;
1985 sprintf (interp->result, "wrong # args");
1988 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
1990 rl = find_IR_record (obj, offset);
1993 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
1996 if (rl->which != Z_NamePlusRecord_databaseRecord)
1998 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2001 if (rl->u.dbrec.type != VAL_SUTRS)
2003 Tcl_AppendElement (interp, rl->u.dbrec.buf);
2009 * do_responseStatus: Return response status (present or search)
2011 static int do_responseStatus (void *o, Tcl_Interp *interp,
2012 int argc, char **argv)
2014 IrTcl_SetObj *obj = o;
2018 obj->recordFlag = 0;
2019 obj->nonSurrogateDiagnosticNum = 0;
2020 obj->nonSurrogateDiagnosticList = NULL;
2023 else if (argc == -1)
2025 ir_deleteDiags (&obj->nonSurrogateDiagnosticList,
2026 &obj->nonSurrogateDiagnosticNum);
2029 if (!obj->recordFlag)
2031 Tcl_AppendElement (interp, "OK");
2036 case Z_Records_DBOSD:
2037 Tcl_AppendElement (interp, "DBOSD");
2040 Tcl_AppendElement (interp, "NSD");
2041 return ir_diagResult (interp, obj->nonSurrogateDiagnosticList,
2042 obj->nonSurrogateDiagnosticNum);
2048 * do_present: Perform Present Request
2051 static int do_present (void *o, Tcl_Interp *interp,
2052 int argc, char **argv)
2054 IrTcl_SetObj *obj = o;
2057 Z_PresentRequest *req;
2066 if (Tcl_GetInt (interp, argv[2], &start) == TCL_ERROR)
2073 if (Tcl_GetInt (interp, argv[3], &number) == TCL_ERROR)
2080 interp->result = "not connected";
2086 odr_reset (p->odr_out);
2088 obj->number = number;
2090 apdu = zget_APDU (p->odr_out, Z_APDU_presentRequest);
2091 req = apdu->u.presentRequest;
2093 set_referenceId (p->odr_out, &req->referenceId,
2094 obj->set_inher.referenceId);
2096 req->resultSetId = obj->setName ? obj->setName : "Default";
2098 req->resultSetStartPoint = &start;
2099 req->numberOfRecordsRequested = &number;
2100 if (obj->set_inher.preferredRecordSyntax)
2102 struct oident ident;
2104 ident.proto = p->protocol_type;
2105 ident.class = CLASS_RECSYN;
2106 ident.value = *obj->set_inher.preferredRecordSyntax;
2107 logf (LOG_DEBUG, "Preferred record syntax is %d", ident.value);
2108 req->preferredRecordSyntax = odr_oiddup (p->odr_out,
2109 oid_getoidbyent (&ident));
2112 req->preferredRecordSyntax = 0;
2114 if (!z_APDU (p->odr_out, &apdu, 0))
2116 interp->result = odr_errlist [odr_geterror (p->odr_out)];
2117 odr_reset (p->odr_out);
2120 p->sbuf = odr_getbuf (p->odr_out, &p->slen, NULL);
2121 if ((r=cs_put (p->cs_link, p->sbuf, p->slen)) < 0)
2123 interp->result = "cs_put failed in present";
2128 ir_select_add_write (cs_fileno(p->cs_link), p);
2129 logf (LOG_DEBUG, "Part of present request, start=%d, num=%d"
2130 " (%d bytes)", start, number, p->slen);
2134 logf (LOG_DEBUG, "Whole present request, start=%d, num=%d"
2135 " (%d bytes)", start, number, p->slen);
2141 * do_loadFile: Load result set from file
2144 static int do_loadFile (void *o, Tcl_Interp *interp,
2145 int argc, char **argv)
2147 IrTcl_SetObj *setobj = o;
2157 interp->result = "wrong # args";
2160 inf = fopen (argv[2], "r");
2163 Tcl_AppendResult (interp, "Cannot open ", argv[2], NULL);
2166 while ((buf = ir_tcl_fread_marc (inf, &size)))
2168 IrTcl_RecordList *rl;
2170 rl = new_IR_record (setobj, no, Z_NamePlusRecord_databaseRecord);
2171 rl->u.dbrec.buf = buf;
2172 rl->u.dbrec.size = size;
2175 setobj->numberOfRecordsReturned = no-1;
2180 static IrTcl_Method ir_set_method_tab[] = {
2181 { 0, "search", do_search },
2182 { 0, "searchStatus", do_searchStatus },
2183 { 0, "presentStatus", do_presentStatus },
2184 { 0, "nextResultSetPosition", do_nextResultSetPosition },
2185 { 0, "setName", do_setName },
2186 { 0, "resultCount", do_resultCount },
2187 { 0, "numberOfRecordsReturned", do_numberOfRecordsReturned },
2188 { 0, "present", do_present },
2189 { 0, "type", do_type },
2190 { 0, "getMarc", do_getMarc },
2191 { 0, "getSutrs", do_getSutrs },
2192 { 0, "recordType", do_recordType },
2193 { 0, "diag", do_diag },
2194 { 0, "responseStatus", do_responseStatus },
2195 { 0, "loadFile", do_loadFile },
2200 * ir_set_obj_method: IR Set Object methods
2202 static int ir_set_obj_method (ClientData clientData, Tcl_Interp *interp,
2203 int argc, char **argv)
2205 IrTcl_Methods tabs[3];
2206 IrTcl_SetObj *p = clientData;
2210 interp->result = "wrong # args";
2213 tabs[0].tab = ir_set_method_tab;
2215 tabs[1].tab = ir_set_c_method_tab;
2216 tabs[1].obj = &p->set_inher;
2219 return ir_method (interp, argc, argv, tabs);
2223 * ir_set_obj_delete: IR Set Object disposal
2225 static void ir_set_obj_delete (ClientData clientData)
2227 IrTcl_Methods tabs[3];
2228 IrTcl_SetObj *p = clientData;
2230 tabs[0].tab = ir_set_method_tab;
2232 tabs[1].tab = ir_set_c_method_tab;
2233 tabs[1].obj = &p->set_inher;
2236 ir_method (NULL, -1, NULL, tabs);
2242 * ir_set_obj_mk: IR Set Object creation
2244 static int ir_set_obj_mk (ClientData clientData, Tcl_Interp *interp,
2245 int argc, char **argv)
2247 IrTcl_Methods tabs[3];
2250 if (argc < 2 || argc > 3)
2252 interp->result = "wrong # args";
2255 if (!(obj = ir_malloc (interp, sizeof(*obj))))
2259 Tcl_CmdInfo parent_info;
2264 if (!Tcl_GetCommandInfo (interp, argv[2], &parent_info))
2266 interp->result = "No parent";
2269 obj->parent = (IrTcl_Obj *) parent_info.clientData;
2271 dst = &obj->set_inher;
2272 src = &obj->parent->set_inher;
2274 dst->num_databaseNames = src->num_databaseNames;
2275 if (!(dst->databaseNames =
2276 ir_malloc (interp, sizeof (*dst->databaseNames)
2277 * dst->num_databaseNames)))
2279 for (i = 0; i < dst->num_databaseNames; i++)
2281 if (ir_strdup (interp, &dst->databaseNames[i],
2282 src->databaseNames[i]) == TCL_ERROR)
2285 if (ir_strdup (interp, &dst->queryType, src->queryType)
2289 if (ir_strdup (interp, &dst->referenceId, src->referenceId)
2293 if (src->preferredRecordSyntax &&
2294 (dst->preferredRecordSyntax
2295 = malloc (sizeof(*dst->preferredRecordSyntax))))
2296 *dst->preferredRecordSyntax = *src->preferredRecordSyntax;
2298 dst->preferredRecordSyntax = NULL;
2299 dst->replaceIndicator = src->replaceIndicator;
2300 dst->smallSetUpperBound = src->smallSetUpperBound;
2301 dst->largeSetLowerBound = src->largeSetLowerBound;
2302 dst->mediumSetPresentNumber = src->mediumSetPresentNumber;
2307 tabs[0].tab = ir_set_method_tab;
2311 if (ir_method (interp, 0, NULL, tabs) == TCL_ERROR)
2314 Tcl_CreateCommand (interp, argv[1], ir_set_obj_method,
2315 (ClientData) obj, ir_set_obj_delete);
2319 /* ------------------------------------------------------- */
2322 * do_scan: Perform scan
2324 static int do_scan (void *o, Tcl_Interp *interp, int argc, char **argv)
2328 IrTcl_ScanObj *obj = o;
2329 IrTcl_Obj *p = obj->parent;
2333 struct ccl_rpn_node *rpn;
2342 interp->result = "wrong # args";
2345 if (!p->set_inher.num_databaseNames)
2347 interp->result = "no databaseNames";
2352 interp->result = "not connected";
2355 odr_reset (p->odr_out);
2357 bib1.proto = p->protocol_type;
2358 bib1.class = CLASS_ATTSET;
2359 bib1.value = VAL_BIB1;
2361 apdu = zget_APDU (p->odr_out, Z_APDU_scanRequest);
2362 req = apdu->u.scanRequest;
2364 set_referenceId (p->odr_out, &req->referenceId, p->set_inher.referenceId);
2365 req->num_databaseNames = p->set_inher.num_databaseNames;
2366 req->databaseNames = p->set_inher.databaseNames;
2367 req->attributeSet = oid_getoidbyent (&bib1);
2370 if (!(req->termListAndStartPoint = p_query_scan (p->odr_out, argv[2])))
2372 Tcl_AppendResult (interp, "Syntax error in query", NULL);
2376 rpn = ccl_find_str(p->bibset, argv[2], &r, &pos);
2379 Tcl_AppendResult (interp, "CCL error: ", ccl_err_msg (r), NULL);
2382 ccl_pr_tree (rpn, stderr);
2383 fprintf (stderr, "\n");
2384 if (!(req->termListAndStartPoint = ccl_scan_query (rpn)))
2387 req->stepSize = &obj->stepSize;
2388 req->numberOfTermsRequested = &obj->numberOfTermsRequested;
2389 req->preferredPositionInResponse = &obj->preferredPositionInResponse;
2390 logf (LOG_DEBUG, "stepSize=%d", *req->stepSize);
2391 logf (LOG_DEBUG, "numberOfTermsRequested=%d",
2392 *req->numberOfTermsRequested);
2393 logf (LOG_DEBUG, "preferredPositionInResponse=%d",
2394 *req->preferredPositionInResponse);
2396 if (!z_APDU (p->odr_out, &apdu, 0))
2398 interp->result = odr_errlist [odr_geterror (p->odr_out)];
2399 odr_reset (p->odr_out);
2402 p->sbuf = odr_getbuf (p->odr_out, &p->slen, NULL);
2403 if ((r=cs_put (p->cs_link, p->sbuf, p->slen)) < 0)
2405 interp->result = "cs_put failed in scan";
2410 ir_select_add_write (cs_fileno(p->cs_link), p);
2411 logf (LOG_DEBUG, "Sent part of scanRequest (%d bytes)", p->slen);
2415 logf (LOG_DEBUG, "Whole scan request (%d bytes)", p->slen);
2421 * do_stepSize: Set/get replace Step Size
2423 static int do_stepSize (void *obj, Tcl_Interp *interp,
2424 int argc, char **argv)
2426 IrTcl_ScanObj *p = obj;
2432 return get_set_int (&p->stepSize, interp, argc, argv);
2436 * do_numberOfTermsRequested: Set/get Number of Terms requested
2438 static int do_numberOfTermsRequested (void *obj, Tcl_Interp *interp,
2439 int argc, char **argv)
2441 IrTcl_ScanObj *p = obj;
2445 p->numberOfTermsRequested = 20;
2448 return get_set_int (&p->numberOfTermsRequested, interp, argc, argv);
2453 * do_preferredPositionInResponse: Set/get preferred Position
2455 static int do_preferredPositionInResponse (void *obj, Tcl_Interp *interp,
2456 int argc, char **argv)
2458 IrTcl_ScanObj *p = obj;
2462 p->preferredPositionInResponse = 1;
2465 return get_set_int (&p->preferredPositionInResponse, interp, argc, argv);
2469 * do_scanStatus: Get scan status
2471 static int do_scanStatus (void *obj, Tcl_Interp *interp,
2472 int argc, char **argv)
2474 IrTcl_ScanObj *p = obj;
2478 return get_set_int (&p->scanStatus, interp, argc, argv);
2482 * do_numberOfEntriesReturned: Get number of Entries returned
2484 static int do_numberOfEntriesReturned (void *obj, Tcl_Interp *interp,
2485 int argc, char **argv)
2487 IrTcl_ScanObj *p = obj;
2491 return get_set_int (&p->numberOfEntriesReturned, interp, argc, argv);
2495 * do_positionOfTerm: Get position of Term
2497 static int do_positionOfTerm (void *obj, Tcl_Interp *interp,
2498 int argc, char **argv)
2500 IrTcl_ScanObj *p = obj;
2504 return get_set_int (&p->positionOfTerm, interp, argc, argv);
2508 * do_scanLine: get Scan Line (surrogate or normal) after response
2510 static int do_scanLine (void *obj, Tcl_Interp *interp, int argc, char **argv)
2512 IrTcl_ScanObj *p = obj;
2518 p->entries_flag = 0;
2520 p->nonSurrogateDiagnosticNum = 0;
2521 p->nonSurrogateDiagnosticList = 0;
2524 else if (argc == -1)
2526 p->entries_flag = 0;
2527 /* release entries */
2530 ir_deleteDiags (&p->nonSurrogateDiagnosticList,
2531 &p->nonSurrogateDiagnosticNum);
2536 interp->result = "wrong # args";
2539 if (Tcl_GetInt (interp, argv[2], &i) == TCL_ERROR)
2541 if (!p->entries_flag || p->which != Z_ListEntries_entries || !p->entries
2542 || i >= p->num_entries || i < 0)
2544 switch (p->entries[i].which)
2546 case Z_Entry_termInfo:
2547 Tcl_AppendElement (interp, "T");
2548 if (p->entries[i].u.term.buf)
2549 Tcl_AppendElement (interp, p->entries[i].u.term.buf);
2551 Tcl_AppendElement (interp, "");
2552 sprintf (numstr, "%d", p->entries[i].u.term.globalOccurrences);
2553 Tcl_AppendElement (interp, numstr);
2555 case Z_Entry_surrogateDiagnostic:
2556 Tcl_AppendElement (interp, "SD");
2557 return ir_diagResult (interp, p->entries[i].u.diag.list,
2558 p->entries[i].u.diag.num);
2564 static IrTcl_Method ir_scan_method_tab[] = {
2565 { 0, "scan", do_scan },
2566 { 0, "stepSize", do_stepSize },
2567 { 0, "numberOfTermsRequested", do_numberOfTermsRequested },
2568 { 0, "preferredPositionInResponse", do_preferredPositionInResponse },
2569 { 0, "scanStatus", do_scanStatus },
2570 { 0, "numberOfEntriesReturned", do_numberOfEntriesReturned },
2571 { 0, "positionOfTerm", do_positionOfTerm },
2572 { 0, "scanLine", do_scanLine },
2577 * ir_scan_obj_method: IR Scan Object methods
2579 static int ir_scan_obj_method (ClientData clientData, Tcl_Interp *interp,
2580 int argc, char **argv)
2582 IrTcl_Methods tabs[2];
2586 interp->result = "wrong # args";
2589 tabs[0].tab = ir_scan_method_tab;
2590 tabs[0].obj = clientData;
2593 return ir_method (interp, argc, argv, tabs);
2597 * ir_scan_obj_delete: IR Scan Object disposal
2599 static void ir_scan_obj_delete (ClientData clientData)
2601 IrTcl_Methods tabs[2];
2602 IrTcl_ScanObj *obj = clientData;
2604 tabs[0].tab = ir_scan_method_tab;
2608 ir_method (NULL, -1, NULL, tabs);
2613 * ir_scan_obj_mk: IR Scan Object creation
2615 static int ir_scan_obj_mk (ClientData clientData, Tcl_Interp *interp,
2616 int argc, char **argv)
2618 Tcl_CmdInfo parent_info;
2620 IrTcl_Methods tabs[2];
2624 interp->result = "wrong # args";
2627 if (!Tcl_GetCommandInfo (interp, argv[2], &parent_info))
2629 interp->result = "No parent";
2632 if (!(obj = ir_malloc (interp, sizeof(*obj))))
2635 obj->parent = (IrTcl_Obj *) parent_info.clientData;
2637 tabs[0].tab = ir_scan_method_tab;
2641 if (ir_method (interp, 0, NULL, tabs) == TCL_ERROR)
2643 Tcl_CreateCommand (interp, argv[1], ir_scan_obj_method,
2644 (ClientData) obj, ir_scan_obj_delete);
2648 /* ------------------------------------------------------- */
2650 static void ir_initResponse (void *obj, Z_InitResponse *initrs)
2654 p->initResult = *initrs->result ? 1 : 0;
2655 if (!*initrs->result)
2656 logf (LOG_DEBUG, "Connection rejected by target");
2658 logf (LOG_DEBUG, "Connection accepted by target");
2660 get_referenceId (&p->set_inher.referenceId, initrs->referenceId);
2662 free (p->targetImplementationId);
2663 ir_strdup (p->interp, &p->targetImplementationId,
2664 initrs->implementationId);
2665 free (p->targetImplementationName);
2666 ir_strdup (p->interp, &p->targetImplementationName,
2667 initrs->implementationName);
2668 free (p->targetImplementationVersion);
2669 ir_strdup (p->interp, &p->targetImplementationVersion,
2670 initrs->implementationVersion);
2672 p->maximumRecordSize = *initrs->maximumRecordSize;
2673 p->preferredMessageSize = *initrs->preferredMessageSize;
2675 memcpy (&p->options, initrs->options, sizeof(initrs->options));
2676 memcpy (&p->protocolVersion, initrs->protocolVersion,
2677 sizeof(initrs->protocolVersion));
2678 free (p->userInformationField);
2679 p->userInformationField = NULL;
2680 if (initrs->userInformationField)
2684 if (initrs->userInformationField->which == ODR_EXTERNAL_octet &&
2685 (p->userInformationField =
2687 initrs->userInformationField->u.octet_aligned->len)
2690 memcpy (p->userInformationField,
2691 initrs->userInformationField->u.octet_aligned->buf,
2693 (p->userInformationField)[len] = '\0';
2698 static void ir_deleteDiags (IrTcl_Diagnostic **dst_list, int *dst_num)
2701 for (i = 0; i<*dst_num; i++)
2702 free (dst_list[i]->addinfo);
2708 static void ir_handleDiags (IrTcl_Diagnostic **dst_list, int *dst_num,
2709 Z_DiagRec **list, int num)
2715 *dst_list = malloc (sizeof(**dst_list) * num);
2721 for (i = 0; i<num; i++)
2723 switch (list[i]->which)
2725 case Z_DiagRec_defaultFormat:
2726 (*dst_list)[i].condition = *list[i]->u.defaultFormat->condition;
2727 addinfo = list[i]->u.defaultFormat->addinfo;
2729 ((*dst_list)[i].addinfo = malloc (strlen(addinfo)+1)))
2730 strcpy ((*dst_list)[i].addinfo, addinfo);
2733 (*dst_list)[i].addinfo = NULL;
2734 (*dst_list)[i].condition = 0;
2739 static void ir_handleRecords (void *o, Z_Records *zrs)
2742 IrTcl_SetObj *setobj = p->set_child;
2745 IrTcl_RecordList *rl;
2747 setobj->which = zrs->which;
2748 setobj->recordFlag = 1;
2750 ir_deleteDiags (&setobj->nonSurrogateDiagnosticList,
2751 &setobj->nonSurrogateDiagnosticNum);
2752 if (zrs->which == Z_Records_DBOSD)
2754 setobj->numberOfRecordsReturned =
2755 zrs->u.databaseOrSurDiagnostics->num_records;
2756 logf (LOG_DEBUG, "Got %d records", setobj->numberOfRecordsReturned);
2757 for (offset = 0; offset<setobj->numberOfRecordsReturned; offset++)
2759 rl = new_IR_record (setobj, setobj->start + offset,
2760 zrs->u.databaseOrSurDiagnostics->
2761 records[offset]->which);
2762 if (rl->which == Z_NamePlusRecord_surrogateDiagnostic)
2764 ir_handleDiags (&rl->u.surrogateDiagnostics.list,
2765 &rl->u.surrogateDiagnostics.num,
2766 &zrs->u.databaseOrSurDiagnostics->
2767 records[offset]->u.surrogateDiagnostic,
2772 Z_DatabaseRecord *zr;
2774 struct oident *ident;
2776 zr = zrs->u.databaseOrSurDiagnostics->records[offset]
2778 oe = (Odr_external*) zr;
2779 rl->u.dbrec.size = zr->u.octet_aligned->len;
2781 rl->u.dbrec.type = VAL_USMARC;
2782 ident = oid_getentbyoid (oe->direct_reference);
2783 rl->u.dbrec.type = ident->value;
2785 if (oe->which == ODR_EXTERNAL_octet && rl->u.dbrec.size > 0)
2787 char *buf = (char*) zr->u.octet_aligned->buf;
2788 if ((rl->u.dbrec.buf = malloc (rl->u.dbrec.size)))
2789 memcpy (rl->u.dbrec.buf, buf, rl->u.dbrec.size);
2791 else if (rl->u.dbrec.type == VAL_SUTRS &&
2792 oe->which == ODR_EXTERNAL_single)
2796 logf (LOG_DEBUG, "Decoding SUTRS");
2797 odr_setbuf (p->odr_in, (char*) oe->u.single_ASN1_type->buf,
2798 oe->u.single_ASN1_type->len, 0);
2799 if (!z_SUTRS(p->odr_in, &rc, 0))
2801 logf (LOG_WARN, "Cannot decode SUTRS");
2802 rl->u.dbrec.buf = NULL;
2806 if ((rl->u.dbrec.buf = malloc (rc->len+1)))
2808 memcpy (rl->u.dbrec.buf, rc->buf, rc->len);
2809 rl->u.dbrec.buf[rc->len] = '\0';
2811 rl->u.dbrec.size = rc->len;
2815 rl->u.dbrec.buf = NULL;
2819 else if (zrs->which == Z_Records_multipleNSD)
2821 logf (LOG_DEBUG, "multipleNonSurrogateDiagnostic %d",
2822 zrs->u.multipleNonSurDiagnostics->num_diagRecs);
2823 setobj->numberOfRecordsReturned = 0;
2824 ir_handleDiags (&setobj->nonSurrogateDiagnosticList,
2825 &setobj->nonSurrogateDiagnosticNum,
2826 zrs->u.multipleNonSurDiagnostics->diagRecs,
2827 zrs->u.multipleNonSurDiagnostics->num_diagRecs);
2831 logf (LOG_DEBUG, "NonSurrogateDiagnostic");
2832 setobj->numberOfRecordsReturned = 0;
2833 ir_handleDiags (&setobj->nonSurrogateDiagnosticList,
2834 &setobj->nonSurrogateDiagnosticNum,
2835 &zrs->u.nonSurrogateDiagnostic,
2840 static void ir_searchResponse (void *o, Z_SearchResponse *searchrs)
2843 IrTcl_SetObj *setobj = p->set_child;
2844 Z_Records *zrs = searchrs->records;
2846 logf (LOG_DEBUG, "Received search response");
2849 logf (LOG_DEBUG, "Search response, no object!");
2852 setobj->searchStatus = searchrs->searchStatus ? 1 : 0;
2853 get_referenceId (&setobj->set_inher.referenceId, searchrs->referenceId);
2854 setobj->resultCount = *searchrs->resultCount;
2855 if (searchrs->presentStatus)
2856 setobj->presentStatus = *searchrs->presentStatus;
2857 if (searchrs->nextResultSetPosition)
2858 setobj->nextResultSetPosition = *searchrs->nextResultSetPosition;
2860 logf (LOG_DEBUG, "Search response %d, %d hits",
2861 setobj->searchStatus, setobj->resultCount);
2863 ir_handleRecords (o, zrs);
2865 setobj->recordFlag = 0;
2869 static void ir_presentResponse (void *o, Z_PresentResponse *presrs)
2872 IrTcl_SetObj *setobj = p->set_child;
2873 Z_Records *zrs = presrs->records;
2875 logf (LOG_DEBUG, "Received present response");
2878 logf (LOG_DEBUG, "Present response, no object!");
2881 setobj->presentStatus = *presrs->presentStatus;
2882 get_referenceId (&setobj->set_inher.referenceId, presrs->referenceId);
2883 setobj->nextResultSetPosition = *presrs->nextResultSetPosition;
2885 ir_handleRecords (o, zrs);
2888 setobj->recordFlag = 0;
2889 logf (LOG_DEBUG, "No records!");
2893 static void ir_scanResponse (void *o, Z_ScanResponse *scanrs)
2896 IrTcl_ScanObj *scanobj = p->scan_child;
2898 logf (LOG_DEBUG, "Received scanResponse");
2900 get_referenceId (&p->set_inher.referenceId, scanrs->referenceId);
2901 scanobj->scanStatus = *scanrs->scanStatus;
2902 logf (LOG_DEBUG, "scanStatus=%d", scanobj->scanStatus);
2904 if (scanrs->stepSize)
2905 scanobj->stepSize = *scanrs->stepSize;
2906 logf (LOG_DEBUG, "stepSize=%d", scanobj->stepSize);
2908 scanobj->numberOfEntriesReturned = *scanrs->numberOfEntriesReturned;
2909 logf (LOG_DEBUG, "numberOfEntriesReturned=%d",
2910 scanobj->numberOfEntriesReturned);
2912 if (scanrs->positionOfTerm)
2913 scanobj->positionOfTerm = *scanrs->positionOfTerm;
2915 scanobj->positionOfTerm = -1;
2916 logf (LOG_DEBUG, "positionOfTerm=%d", scanobj->positionOfTerm);
2918 free (scanobj->entries);
2919 scanobj->entries = NULL;
2921 ir_deleteDiags (&scanobj->nonSurrogateDiagnosticList,
2922 &scanobj->nonSurrogateDiagnosticNum);
2923 if (scanrs->entries)
2928 scanobj->entries_flag = 1;
2929 scanobj->which = scanrs->entries->which;
2930 switch (scanobj->which)
2932 case Z_ListEntries_entries:
2933 scanobj->num_entries = scanrs->entries->u.entries->num_entries;
2934 scanobj->entries = malloc (scanobj->num_entries *
2935 sizeof(*scanobj->entries));
2936 for (i=0; i<scanobj->num_entries; i++)
2938 ze = scanrs->entries->u.entries->entries[i];
2939 scanobj->entries[i].which = ze->which;
2942 case Z_Entry_termInfo:
2943 if (ze->u.termInfo->term->which == Z_Term_general)
2945 int l = ze->u.termInfo->term->u.general->len;
2946 scanobj->entries[i].u.term.buf = malloc (1+l);
2947 memcpy (scanobj->entries[i].u.term.buf,
2948 ze->u.termInfo->term->u.general->buf,
2950 scanobj->entries[i].u.term.buf[l] = '\0';
2953 scanobj->entries[i].u.term.buf = NULL;
2954 if (ze->u.termInfo->globalOccurrences)
2955 scanobj->entries[i].u.term.globalOccurrences =
2956 *ze->u.termInfo->globalOccurrences;
2958 scanobj->entries[i].u.term.globalOccurrences = 0;
2960 case Z_Entry_surrogateDiagnostic:
2961 ir_handleDiags (&scanobj->entries[i].u.diag.list,
2962 &scanobj->entries[i].u.diag.num,
2963 &ze->u.surrogateDiagnostic,
2969 case Z_ListEntries_nonSurrogateDiagnostics:
2970 ir_handleDiags (&scanobj->nonSurrogateDiagnosticList,
2971 &scanobj->nonSurrogateDiagnosticNum,
2972 scanrs->entries->u.nonSurrogateDiagnostics->
2974 scanrs->entries->u.nonSurrogateDiagnostics->
2980 scanobj->entries_flag = 0;
2984 * ir_select_read: handle incoming packages
2986 void ir_select_read (ClientData clientData)
2988 IrTcl_Obj *p = clientData;
2994 r = cs_rcvconnect (p->cs_link);
2997 logf (LOG_WARN, "cs_rcvconnect returned 1");
3001 ir_select_remove_write (cs_fileno (p->cs_link), p);
3004 logf (LOG_DEBUG, "cs_rcvconnect error");
3007 p->failInfo = IR_TCL_FAIL_CONNECT;
3008 IrTcl_eval (p->interp, p->failback);
3010 do_disconnect (p, NULL, 2, NULL);
3014 IrTcl_eval (p->interp, p->callback);
3019 /* signal one more use of ir object - callbacks must not
3020 release the ir memory (p pointer) */
3022 if ((r=cs_get (p->cs_link, &p->buf_in, &p->len_in)) <= 0)
3024 logf (LOG_DEBUG, "cs_get failed, code %d", r);
3025 ir_select_remove (cs_fileno (p->cs_link), p);
3028 p->failInfo = IR_TCL_FAIL_READ;
3029 IrTcl_eval (p->interp, p->failback);
3031 do_disconnect (p, NULL, 2, NULL);
3033 /* relase ir object now if callback deleted it */
3039 odr_setbuf (p->odr_in, p->buf_in, r, 0);
3040 logf (LOG_DEBUG, "cs_get ok, got %d", r);
3041 if (!z_APDU (p->odr_in, &apdu, 0))
3043 logf (LOG_DEBUG, "%s", odr_errlist [odr_geterror (p->odr_in)]);
3046 p->failInfo = IR_TCL_FAIL_IN_APDU;
3047 IrTcl_eval (p->interp, p->failback);
3049 do_disconnect (p, NULL, 2, NULL);
3051 /* release ir object now if failback deleted it */
3057 case Z_APDU_initResponse:
3058 ir_initResponse (p, apdu->u.initResponse);
3060 case Z_APDU_searchResponse:
3061 ir_searchResponse (p, apdu->u.searchResponse);
3063 case Z_APDU_presentResponse:
3064 ir_presentResponse (p, apdu->u.presentResponse);
3066 case Z_APDU_scanResponse:
3067 ir_scanResponse (p, apdu->u.scanResponse);
3070 logf (LOG_WARN, "Received unknown APDU type (%d)", apdu->which);
3073 p->failInfo = IR_TCL_FAIL_UNKNOWN_APDU;
3074 IrTcl_eval (p->interp, p->failback);
3076 do_disconnect (p, NULL, 2, NULL);
3078 odr_reset (p->odr_in);
3080 IrTcl_eval (p->interp, p->callback);
3081 if (p->ref_count == 1)
3087 } while (p->cs_link && cs_more (p->cs_link));
3091 * ir_select_write: handle outgoing packages - not yet written.
3093 void ir_select_write (ClientData clientData)
3095 IrTcl_Obj *p = clientData;
3098 logf (LOG_DEBUG, "In write handler");
3101 r = cs_rcvconnect (p->cs_link);
3107 logf (LOG_DEBUG, "cs_rcvconnect error");
3108 ir_select_remove_write (cs_fileno (p->cs_link), p);
3111 p->failInfo = IR_TCL_FAIL_CONNECT;
3112 IrTcl_eval (p->interp, p->failback);
3114 do_disconnect (p, NULL, 2, NULL);
3117 ir_select_remove_write (cs_fileno (p->cs_link), p);
3119 IrTcl_eval (p->interp, p->callback);
3122 if ((r=cs_put (p->cs_link, p->sbuf, p->slen)) < 0)
3124 logf (LOG_DEBUG, "select write fail");
3127 p->failInfo = IR_TCL_FAIL_WRITE;
3128 IrTcl_eval (p->interp, p->failback);
3130 do_disconnect (p, NULL, 2, NULL);
3132 else if (r == 0) /* remove select bit */
3134 ir_select_remove_write (cs_fileno (p->cs_link), p);
3138 /* ------------------------------------------------------- */
3141 * ir_tcl_init: Registration of TCL commands.
3143 int ir_tcl_init (Tcl_Interp *interp)
3145 Tcl_CreateCommand (interp, "ir", ir_obj_mk, (ClientData) NULL,
3146 (Tcl_CmdDeleteProc *) NULL);
3147 Tcl_CreateCommand (interp, "ir-set", ir_set_obj_mk,
3148 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
3149 Tcl_CreateCommand (interp, "ir-scan", ir_scan_obj_mk,
3150 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
3151 irTcl_interp = interp;