2 * IR toolkit for tcl/tk
4 * See the file LICENSE for details.
5 * Sebastian Hammer, Adam Dickmeiss
8 * Revision 1.68 1996-01-04 11:05:22 adam
9 * New setting: PDUType - returns type of last PDU returned from the target.
10 * Fixed a bug in configure/Makefile.
12 * Revision 1.67 1996/01/03 09:00:51 adam
13 * Updated to use new version of Yaz (names changed to avoid C++ conflict).
15 * Revision 1.66 1995/11/28 17:26:39 adam
16 * Removed Carriage return from ir-tcl.c!
17 * Removed misc. debug logs.
19 * Revision 1.65 1995/11/28 13:53:00 quinn
22 * Revision 1.64 1995/11/13 15:39:18 adam
23 * Bug fix: {small,medium}SetElementSetNames weren't set correctly.
24 * Bug fix: idAuthentication weren't set correctly.
26 * Revision 1.63 1995/11/13 09:55:39 adam
27 * Multiple records at a position in a result-set with differnt
30 * Revision 1.62 1995/10/18 17:20:33 adam
31 * Work on target setup in client.tcl.
33 * Revision 1.61 1995/10/18 16:42:42 adam
34 * New settings: smallSetElementSetNames and mediumSetElementSetNames.
36 * Revision 1.60 1995/10/18 15:43:31 adam
37 * In search: mediumSetElementSetNames and smallSetElementSetNames are
38 * set to elementSetNames.
40 * Revision 1.59 1995/10/17 12:18:58 adam
41 * Bug fix: when target connection closed, the connection was not
42 * properly reestablished.
44 * Revision 1.58 1995/10/16 17:00:55 adam
45 * New setting: elementSetNames.
46 * Various client improvements. Medium presentation format looks better.
48 * Revision 1.57 1995/09/21 13:11:51 adam
49 * Support of dynamic loading.
50 * Test script uses load command if necessary.
52 * Revision 1.56 1995/08/29 15:30:14 adam
53 * Work on GRS records.
55 * Revision 1.55 1995/08/28 09:43:25 adam
56 * Minor changes. configure only searches for yaz beta 3 and versions after
59 * Revision 1.54 1995/08/24 12:25:16 adam
60 * Modified to work with yaz 1.0b3.
62 * Revision 1.53 1995/08/04 12:49:26 adam
63 * Bug fix: reading uninitialized variable p.
65 * Revision 1.52 1995/08/04 11:32:38 adam
66 * More work on output queue. Memory related routines moved
69 * Revision 1.51 1995/08/03 13:22:54 adam
72 * Revision 1.50 1995/07/20 08:09:49 adam
73 * client.tcl: Targets removed from hotTargets list when targets
74 * are removed/modified.
75 * ir-tcl.c: More work on triggerResourceControl.
77 * Revision 1.49 1995/06/30 12:39:21 adam
78 * Bug fix: loadFile didn't set record type.
79 * The MARC routines are a little less strict in the interpretation.
80 * Script display.tcl replaces the old marc.tcl.
81 * New interactive script: shell.tcl.
83 * Revision 1.48 1995/06/27 19:03:50 adam
84 * Bug fix in do_present in ir-tcl.c: p->set_child member weren't set.
85 * nextResultSetPosition used instead of setOffset.
87 * Revision 1.47 1995/06/25 10:25:04 adam
88 * Working on triggerResourceControl. Description of compile/install
89 * procedure moved to ir-tcl.sgml.
91 * Revision 1.46 1995/06/22 13:15:06 adam
92 * Feature: SUTRS. Setting getSutrs implemented.
93 * Work on display formats.
94 * Preferred record syntax can be set by the user.
96 * Revision 1.45 1995/06/20 08:07:30 adam
97 * New setting: failInfo.
98 * Working on better cancel mechanism.
100 * Revision 1.44 1995/06/19 17:01:20 adam
103 * Revision 1.43 1995/06/19 13:06:08 adam
104 * New define: IR_TCL_VERSION.
106 * Revision 1.42 1995/06/19 08:08:52 adam
107 * client.tcl: hotTargets now contain both database and target name.
108 * ir-tcl.c: setting protocol edited. Errors in callbacks are logged
109 * by logf(LOG_WARN, ...) calls.
111 * Revision 1.41 1995/06/16 12:28:16 adam
112 * Implemented preferredRecordSyntax.
113 * Minor changes in diagnostic handling.
114 * Record list deleted when connection closes.
116 * Revision 1.40 1995/06/14 13:37:18 adam
117 * Setting recordType implemented.
118 * Setting implementationVersion implemented.
119 * Settings implementationId / implementationName edited.
121 * Revision 1.39 1995/06/08 10:26:32 adam
122 * Bug fix in ir_strdup.
124 * Revision 1.38 1995/06/01 16:36:47 adam
125 * About buttons. Minor bug fixes.
127 * Revision 1.37 1995/06/01 07:31:20 adam
128 * Rename of many typedefs -> IrTcl_...
130 * Revision 1.36 1995/05/31 13:09:59 adam
131 * Client searches/presents may be interrupted.
132 * New moving book-logo.
134 * Revision 1.35 1995/05/31 08:36:33 adam
135 * Bug fix in client.tcl: didn't save options on clientrc.tcl.
136 * New method: referenceId. More work on scan.
138 * Revision 1.34 1995/05/29 10:33:42 adam
139 * README and rename of startup script.
141 * Revision 1.33 1995/05/29 09:15:11 quinn
142 * Changed CS_SR to PROTO_SR, etc.
144 * Revision 1.32 1995/05/29 08:44:16 adam
145 * Work on delete of objects.
147 * Revision 1.31 1995/05/26 11:44:10 adam
148 * Bugs fixed. More work on MARC utilities and queries. Test
149 * client is up-to-date again.
151 * Revision 1.30 1995/05/26 08:54:11 adam
152 * New MARC utilities. Uses prefix query.
154 * Revision 1.29 1995/05/24 14:10:22 adam
155 * Work on idAuthentication, protocolVersion and options.
157 * Revision 1.28 1995/05/23 15:34:48 adam
158 * Many new settings, userInformationField, smallSetUpperBound, etc.
159 * A number of settings are inherited when ir-set is executed.
160 * This version is incompatible with the graphical test client (client.tcl).
162 * Revision 1.27 1995/05/11 15:34:47 adam
163 * Scan request changed a bit. This version works with RLG.
165 * Revision 1.26 1995/04/18 16:11:51 adam
166 * First version of graphical Scan. Some work on query-by-form.
168 * Revision 1.25 1995/04/17 09:37:17 adam
169 * Further development of scan.
171 * Revision 1.24 1995/04/11 14:16:42 adam
172 * Further work on scan. Response works. Entries aren't saved yet.
174 * Revision 1.23 1995/04/10 10:50:27 adam
175 * Result-set name defaults to suffix of ir-set name.
176 * Started working on scan. Not finished at this point.
178 * Revision 1.22 1995/03/31 10:43:03 adam
179 * More robust when getting bad MARC records.
181 * Revision 1.21 1995/03/31 08:56:37 adam
182 * New button "Search".
184 * Revision 1.20 1995/03/29 16:07:09 adam
185 * Bug fix: Didn't use setName in present request.
187 * Revision 1.19 1995/03/28 12:45:23 adam
188 * New ir method failback: called on disconnect/protocol error.
189 * New ir set/get method: protocol: SR / Z3950.
190 * Simple popup and disconnect when failback is invoked.
192 * Revision 1.18 1995/03/21 15:50:12 adam
195 * Revision 1.17 1995/03/21 13:41:03 adam
196 * Comstack cs_create not used too often. Non-blocking connect.
198 * Revision 1.16 1995/03/21 08:26:06 adam
199 * New method, setName, to specify the result set name (other than Default).
200 * New method, responseStatus, which returns diagnostic info, if any, after
201 * present response / search response.
203 * Revision 1.15 1995/03/20 15:24:07 adam
204 * Diagnostic records saved on searchResponse.
206 * Revision 1.14 1995/03/20 08:53:22 adam
207 * Event loop in tclmain.c rewritten. New method searchStatus.
209 * Revision 1.13 1995/03/17 18:26:17 adam
210 * Non-blocking i/o used now. Database names popup as cascade items.
212 * Revision 1.12 1995/03/17 15:45:00 adam
213 * Improved target/database setup.
215 * Revision 1.11 1995/03/16 17:54:03 adam
216 * Minor changes really.
218 * Revision 1.10 1995/03/15 16:14:50 adam
219 * Blocking arg in cs_create changed.
221 * Revision 1.9 1995/03/15 13:59:24 adam
224 * Revision 1.8 1995/03/15 08:25:16 adam
225 * New method presentStatus to check for error on present. Misc. cleanup
226 * of IrTcl_RecordList manipulations. Full MARC record presentation in
229 * Revision 1.7 1995/03/14 17:32:29 adam
230 * Presentation of full Marc record in popup window.
232 * Revision 1.6 1995/03/12 19:31:55 adam
233 * Pattern matching implemented when retrieving MARC records. More
234 * diagnostic functions.
236 * Revision 1.5 1995/03/10 18:00:15 adam
237 * Actual presentation in line-by-line format. RPN query support.
239 * Revision 1.4 1995/03/09 16:15:08 adam
240 * First presentRequest attempts. Hot-target list.
249 #include <sys/time.h>
260 int (*method) (void *obj, Tcl_Interp *interp, int argc, char **argv);
268 static void ir_deleteDiags (IrTcl_Diagnostic **dst_list, int *dst_num);
269 static int do_disconnect (void *obj, Tcl_Interp *interp,
270 int argc, char **argv);
272 static IrTcl_RecordList *new_IR_record (IrTcl_SetObj *setobj,
274 const char *elements)
276 IrTcl_RecordList *rl;
278 for (rl = setobj->record_list; rl; rl = rl->next)
280 if (no == rl->no && (!rl->elements || !elements ||
281 !strcmp(elements, rl->elements)))
286 case Z_NamePlusRecord_databaseRecord:
287 free (rl->u.dbrec.buf);
288 rl->u.dbrec.buf = NULL;
290 case Z_NamePlusRecord_surrogateDiagnostic:
291 ir_deleteDiags (&rl->u.surrogateDiagnostics.list,
292 &rl->u.surrogateDiagnostics.num);
300 rl = ir_tcl_malloc (sizeof(*rl));
301 rl->next = setobj->record_list;
303 setobj->record_list = rl;
306 ir_tcl_strdup (NULL, &rl->elements, elements);
311 enum oid_value value;
313 } IrTcl_recordSyntaxTab[] = {
314 { VAL_UNIMARC, "UNIMARC" },
315 { VAL_INTERMARC, "INTERMARC" },
317 { VAL_USMARC, "USMARC" },
318 { VAL_UKMARC, "UKMARC" },
319 { VAL_NORMARC, "NORMARC" },
320 { VAL_LIBRISMARC, "LIBRISMARC" },
321 { VAL_DANMARC, "DANMARC" },
322 { VAL_FINMARC, "FINMARC" },
324 { VAL_CANMARC, "CANMARC" },
326 { VAL_PICAMARC, "PICAMARC" },
327 { VAL_AUSMARC, "AUSMARC" },
328 { VAL_IBERMARC, "IBERMARC" },
329 { VAL_SUTRS, "SUTRS" },
330 { VAL_GRS1, "GRS1" },
337 int IrTcl_eval (Tcl_Interp *interp, const char *command)
339 char *tmp = ir_tcl_malloc (strlen(command)+1);
342 strcpy (tmp, command);
343 r = Tcl_Eval (interp, tmp);
345 logf (LOG_WARN, "Tcl error in line %d: %s", interp->errorLine,
347 Tcl_FreeResult (interp);
353 * IrTcl_getRecordSyntaxStr: Return record syntax name of object id
355 static const char *IrTcl_getRecordSyntaxStr (enum oid_value value)
358 for (i = 0; IrTcl_recordSyntaxTab[i].name; i++)
359 if (IrTcl_recordSyntaxTab[i].value == value)
360 return IrTcl_recordSyntaxTab[i].name;
365 * IrTcl_getRecordSyntaxVal: Return record syntax value of string
367 static enum oid_value IrTcl_getRecordSyntaxVal (const char *name)
370 for (i = 0; IrTcl_recordSyntaxTab[i].name; i++)
371 if (!strcmp (IrTcl_recordSyntaxTab[i].name, name))
372 return IrTcl_recordSyntaxTab[i].value;
376 static IrTcl_RecordList *find_IR_record (IrTcl_SetObj *setobj, int no)
378 IrTcl_RecordList *rl;
380 for (rl = setobj->record_list; rl; rl = rl->next)
382 (!setobj->recordElements || !rl->elements ||
383 !strcmp (setobj->recordElements, rl->elements)))
388 static void delete_IR_records (IrTcl_SetObj *setobj)
390 IrTcl_RecordList *rl, *rl1;
392 for (rl = setobj->record_list; rl; rl = rl1)
396 case Z_NamePlusRecord_databaseRecord:
397 free (rl->u.dbrec.buf);
399 case Z_NamePlusRecord_surrogateDiagnostic:
400 ir_deleteDiags (&rl->u.surrogateDiagnostics.list,
401 &rl->u.surrogateDiagnostics.num);
407 setobj->record_list = NULL;
411 * get_set_int: Set/get integer value
413 static int get_set_int (int *val, Tcl_Interp *interp, int argc, char **argv)
419 if (Tcl_GetInt (interp, argv[2], val)==TCL_ERROR)
422 sprintf (buf, "%d", *val);
423 Tcl_AppendResult (interp, buf, NULL);
428 * ir_method: Search for method in table and invoke method handler
430 int ir_method (Tcl_Interp *interp, int argc, char **argv, IrTcl_Methods *tab)
432 IrTcl_Methods *tab_i = tab;
435 for (tab_i = tab; tab_i->tab; tab_i++)
436 for (t = tab_i->tab; t->name; t++)
439 if ((*t->method)(tab_i->obj, interp, argc, argv) == TCL_ERROR)
443 if (!strcmp (t->name, argv[1]))
444 return (*t->method)(tab_i->obj, interp, argc, argv);
448 Tcl_AppendResult (interp, "Bad method: ", argv[1],
449 ". Possible methods:", NULL);
450 for (tab_i = tab; tab_i->tab; tab_i++)
451 for (t = tab_i->tab; t->name; t++)
452 Tcl_AppendResult (interp, " ", t->name, NULL);
457 * ir_method_r: Get status for all readable elements
459 int ir_method_r (void *obj, Tcl_Interp *interp, int argc, char **argv,
467 for (; tab->name; tab++)
470 argv_n[1] = tab->name;
471 Tcl_AppendResult (interp, "{", NULL);
472 (*tab->method)(obj, interp, argc_n, argv_n);
473 Tcl_AppendResult (interp, "} ", NULL);
479 * ir_named_bits: get/set named bits
481 int ir_named_bits (struct ir_named_entry *tab, Odr_bitmask *ob,
482 Tcl_Interp *interp, int argc, char **argv)
484 struct ir_named_entry *ti;
489 for (no = 0; no < argc; no++)
491 for (ti = tab; ti->name; ti++)
492 if (!strcmp (argv[no], ti->name))
494 ODR_MASK_SET (ob, ti->pos);
499 Tcl_AppendResult (interp, "Bad bit mask: ", argv[no], NULL);
505 for (ti = tab; ti->name; ti++)
506 if (ODR_MASK_GET (ob, ti->pos))
507 Tcl_AppendElement (interp, ti->name);
511 static void set_referenceId (ODR o, Z_ReferenceId **dst, const char *src)
517 *dst = odr_malloc (o, sizeof(**dst));
518 (*dst)->size = (*dst)->len = strlen(src);
519 (*dst)->buf = odr_malloc (o, (*dst)->len);
520 memcpy ((*dst)->buf, src, (*dst)->len);
524 static void get_referenceId (char **dst, Z_ReferenceId *src)
532 *dst = ir_tcl_malloc (src->len+1);
533 memcpy (*dst, src->buf, src->len);
534 (*dst)[src->len] = '\0';
537 /* ------------------------------------------------------- */
540 * do_init_request: init method on IR object
542 static int do_init_request (void *obj, Tcl_Interp *interp,
543 int argc, char **argv)
553 interp->result = "init: not connected";
556 apdu = zget_APDU (p->odr_out, Z_APDU_initRequest);
557 req = apdu->u.initRequest;
559 set_referenceId (p->odr_out, &req->referenceId, p->set_inher.referenceId);
560 req->options = &p->options;
561 req->protocolVersion = &p->protocolVersion;
562 req->preferredMessageSize = &p->preferredMessageSize;
563 req->maximumRecordSize = &p->maximumRecordSize;
565 if (p->idAuthenticationGroupId)
567 Z_IdPass *pass = odr_malloc (p->odr_out, sizeof(*pass));
568 Z_IdAuthentication *auth = odr_malloc (p->odr_out, sizeof(*auth));
570 logf (LOG_DEBUG, "using pass authentication");
572 auth->which = Z_IdAuthentication_idPass;
573 auth->u.idPass = pass;
574 if (p->idAuthenticationGroupId && *p->idAuthenticationGroupId)
575 pass->groupId = p->idAuthenticationGroupId;
577 pass->groupId = NULL;
578 if (p->idAuthenticationUserId && *p->idAuthenticationUserId)
579 pass->userId = p->idAuthenticationUserId;
582 if (p->idAuthenticationPassword && *p->idAuthenticationPassword)
583 pass->password = p->idAuthenticationPassword;
585 pass->password = NULL;
586 req->idAuthentication = auth;
588 else if (!p->idAuthenticationOpen || !*p->idAuthenticationOpen)
589 req->idAuthentication = NULL;
592 Z_IdAuthentication *auth = odr_malloc (p->odr_out, sizeof(*auth));
594 logf (LOG_DEBUG, "using open authentication");
595 auth->which = Z_IdAuthentication_open;
596 auth->u.open = p->idAuthenticationOpen;
597 req->idAuthentication = auth;
599 req->implementationId = p->implementationId;
600 req->implementationName = p->implementationName;
601 req->implementationVersion = p->implementationVersion;
602 req->userInformationField = 0;
604 return ir_tcl_send_APDU (interp, p, apdu, "init", argv[0]);
608 * do_protocolVersion: Set protocol Version
610 static int do_protocolVersion (void *obj, Tcl_Interp *interp,
611 int argc, char **argv)
619 ODR_MASK_ZERO (&p->protocolVersion);
620 ODR_MASK_SET (&p->protocolVersion, 0);
621 ODR_MASK_SET (&p->protocolVersion, 1);
626 if (Tcl_GetInt (interp, argv[2], &version)==TCL_ERROR)
628 ODR_MASK_ZERO (&p->protocolVersion);
629 for (i = 0; i<version; i++)
630 ODR_MASK_SET (&p->protocolVersion, i);
632 for (i = 4; --i >= 0; )
633 if (ODR_MASK_GET (&p->protocolVersion, i))
635 sprintf (buf, "%d", i+1);
636 interp->result = buf;
641 * do_options: Set options
643 static int do_options (void *obj, Tcl_Interp *interp,
644 int argc, char **argv)
646 static struct ir_named_entry options_tab[] = {
650 { "resourceReport", 3 },
651 { "triggerResourceCtrl", 4},
652 { "resourceCtrl", 5},
656 { "extendedServices", 10},
657 { "level-1Segmentation", 11},
658 { "level-2Segmentation", 12},
659 { "concurrentOperations", 13},
660 { "namedResultSets", 14},
667 ODR_MASK_ZERO (&p->options);
668 ODR_MASK_SET (&p->options, 0);
669 ODR_MASK_SET (&p->options, 1);
670 ODR_MASK_SET (&p->options, 4);
671 ODR_MASK_SET (&p->options, 7);
672 ODR_MASK_SET (&p->options, 14);
675 return ir_named_bits (options_tab, &p->options, interp, argc-2, argv+2);
679 * do_failInfo: Get fail information
681 static int do_failInfo (void *obj, Tcl_Interp *interp, int argc, char **argv)
691 sprintf (buf, "%d", p->failInfo);
697 case IR_TCL_FAIL_CONNECT:
698 cp = "connect failed";
700 case IR_TCL_FAIL_READ:
701 cp = "connection closed";
703 case IR_TCL_FAIL_WRITE:
704 cp = "connection closed";
706 case IR_TCL_FAIL_IN_APDU:
707 cp = "failed to decode incoming APDU";
709 case IR_TCL_FAIL_UNKNOWN_APDU:
715 Tcl_AppendElement (interp, buf);
716 Tcl_AppendElement (interp, cp);
721 * do_preferredMessageSize: Set/get preferred message size
723 static int do_preferredMessageSize (void *obj, Tcl_Interp *interp,
724 int argc, char **argv)
730 p->preferredMessageSize = 30000;
733 return get_set_int (&p->preferredMessageSize, interp, argc, argv);
737 * do_maximumRecordSize: Set/get maximum record size
739 static int do_maximumRecordSize (void *obj, Tcl_Interp *interp,
740 int argc, char **argv)
746 p->maximumRecordSize = 30000;
749 return get_set_int (&p->maximumRecordSize, interp, argc, argv);
753 * do_initResult: Get init result
755 static int do_initResult (void *obj, Tcl_Interp *interp,
756 int argc, char **argv)
762 return get_set_int (&p->initResult, interp, argc, argv);
767 * do_implementationName: Set/get Implementation Name.
769 static int do_implementationName (void *obj, Tcl_Interp *interp,
770 int argc, char **argv)
775 return ir_tcl_strdup (interp, &p->implementationName,
776 "Index Data/IrTcl on YAZ");
778 return ir_tcl_strdel (interp, &p->implementationName);
781 free (p->implementationName);
782 if (ir_tcl_strdup (interp, &p->implementationName, argv[2])
786 Tcl_AppendResult (interp, p->implementationName, (char*) NULL);
791 * do_implementationId: Get Implementation Id.
793 static int do_implementationId (void *obj, Tcl_Interp *interp,
794 int argc, char **argv)
799 return ir_tcl_strdup (interp, &p->implementationId, "YAZ (id=81)");
801 return ir_tcl_strdel (interp, &p->implementationId);
802 Tcl_AppendResult (interp, p->implementationId, (char*) NULL);
807 * do_implementationVersion: get Implementation Version.
809 static int do_implementationVersion (void *obj, Tcl_Interp *interp,
810 int argc, char **argv)
815 return ir_tcl_strdup (interp, &p->implementationVersion,
816 "YAZ: " YAZ_VERSION " / IrTcl: " IR_TCL_VERSION);
818 return ir_tcl_strdel (interp, &p->implementationVersion);
819 Tcl_AppendResult (interp, p->implementationVersion, (char*) NULL);
824 * do_targetImplementationName: Get Implementation Name of target.
826 static int do_targetImplementationName (void *obj, Tcl_Interp *interp,
827 int argc, char **argv)
833 p->targetImplementationName = NULL;
837 return ir_tcl_strdel (interp, &p->targetImplementationName);
838 Tcl_AppendResult (interp, p->targetImplementationName, (char*) NULL);
843 * do_targetImplementationId: Get Implementation Id of target
845 static int do_targetImplementationId (void *obj, Tcl_Interp *interp,
846 int argc, char **argv)
852 p->targetImplementationId = NULL;
856 return ir_tcl_strdel (interp, &p->targetImplementationId);
857 Tcl_AppendResult (interp, p->targetImplementationId, (char*) NULL);
862 * do_targetImplementationVersion: Get Implementation Version of target
864 static int do_targetImplementationVersion (void *obj, Tcl_Interp *interp,
865 int argc, char **argv)
871 p->targetImplementationVersion = NULL;
875 return ir_tcl_strdel (interp, &p->targetImplementationVersion);
876 Tcl_AppendResult (interp, p->targetImplementationVersion, (char*) NULL);
881 * do_idAuthentication: Set/get id Authentication
883 static int do_idAuthentication (void *obj, Tcl_Interp *interp,
884 int argc, char **argv)
888 if (argc >= 3 || argc == -1)
890 free (p->idAuthenticationOpen);
891 free (p->idAuthenticationGroupId);
892 free (p->idAuthenticationUserId);
893 free (p->idAuthenticationPassword);
895 if (argc >= 3 || argc <= 0)
897 p->idAuthenticationOpen = NULL;
898 p->idAuthenticationGroupId = NULL;
899 p->idAuthenticationUserId = NULL;
900 p->idAuthenticationPassword = NULL;
909 ir_tcl_strdup (interp, &p->idAuthenticationOpen, argv[2])
916 ir_tcl_strdup (interp, &p->idAuthenticationGroupId, argv[2])
920 ir_tcl_strdup (interp, &p->idAuthenticationUserId, argv[3])
924 ir_tcl_strdup (interp, &p->idAuthenticationPassword, argv[4])
929 if (p->idAuthenticationOpen)
930 Tcl_AppendElement (interp, p->idAuthenticationOpen);
931 else if (p->idAuthenticationGroupId)
933 Tcl_AppendElement (interp, p->idAuthenticationGroupId);
934 Tcl_AppendElement (interp, p->idAuthenticationUserId);
935 Tcl_AppendElement (interp, p->idAuthenticationPassword);
941 * do_connect: connect method on IR object
943 static int do_connect (void *obj, Tcl_Interp *interp,
944 int argc, char **argv)
956 interp->result = "already connected";
959 if (ir_tcl_strdup (interp, &p->hostname, argv[2]) == TCL_ERROR)
961 if (!strcmp (p->cs_type, "tcpip"))
963 p->cs_link = cs_create (tcpip_type, CS_BLOCK, p->protocol_type);
964 addr = tcpip_strtoaddr (argv[2]);
967 interp->result = "tcpip_strtoaddr fail";
970 logf (LOG_DEBUG, "tcp/ip connect %s", argv[2]);
972 else if (!strcmp (p->cs_type, "mosi"))
975 p->cs_link = cs_create (mosi_type, CS_BLOCK, p->protocol_type);
976 addr = mosi_strtoaddr (argv[2]);
979 interp->result = "mosi_strtoaddr fail";
982 logf (LOG_DEBUG, "mosi connect %s", argv[2]);
984 interp->result = "MOSI support not there";
990 Tcl_AppendResult (interp, "Bad comstack type: ",
994 if ((r=cs_connect (p->cs_link, addr)) < 0)
996 interp->result = "connect fail";
997 do_disconnect (p, NULL, 2, NULL);
1000 p->pduType = "connect";
1001 ir_select_add (cs_fileno (p->cs_link), p);
1004 ir_select_add_write (cs_fileno (p->cs_link), p);
1005 p->state = IR_TCL_R_Connecting;
1009 p->state = IR_TCL_R_Idle;
1011 IrTcl_eval (p->interp, p->callback);
1015 Tcl_AppendResult (interp, p->hostname, NULL);
1020 * do_disconnect: disconnect method on IR object
1022 static int do_disconnect (void *obj, Tcl_Interp *interp,
1023 int argc, char **argv)
1029 p->state = IR_TCL_R_Idle;
1039 ir_select_remove_write (cs_fileno (p->cs_link), p);
1040 ir_select_remove (cs_fileno (p->cs_link), p);
1042 odr_reset (p->odr_in);
1044 assert (p->cs_link);
1045 cs_close (p->cs_link);
1048 ODR_MASK_ZERO (&p->options);
1049 ODR_MASK_SET (&p->options, 0);
1050 ODR_MASK_SET (&p->options, 1);
1051 ODR_MASK_SET (&p->options, 4);
1052 ODR_MASK_SET (&p->options, 7);
1053 ODR_MASK_SET (&p->options, 14);
1055 ODR_MASK_ZERO (&p->protocolVersion);
1056 ODR_MASK_SET (&p->protocolVersion, 0);
1057 ODR_MASK_SET (&p->protocolVersion, 1);
1060 assert (!p->cs_link);
1065 * do_comstack: Set/get comstack method on IR object
1067 static int do_comstack (void *o, Tcl_Interp *interp,
1068 int argc, char **argv)
1073 return ir_tcl_strdup (interp, &obj->cs_type, "tcpip");
1074 else if (argc == -1)
1075 return ir_tcl_strdel (interp, &obj->cs_type);
1078 free (obj->cs_type);
1079 if (ir_tcl_strdup (interp, &obj->cs_type, argv[2]) == TCL_ERROR)
1082 Tcl_AppendElement (interp, obj->cs_type);
1087 * do_logLevel: Set log level
1089 static int do_logLevel (void *o, Tcl_Interp *interp,
1090 int argc, char **argv)
1095 log_init (log_mask_str (argv[2]), "", NULL);
1097 log_init (log_mask_str (argv[2]), argv[3], NULL);
1099 log_init (log_mask_str (argv[2]), argv[3], argv[4]);
1105 * do_pduType: Return type of last PDU received
1107 static int do_pduType (void *obj, Tcl_Interp *interp,
1108 int argc, char **argv)
1117 Tcl_AppendElement (interp, p->pduType ? p->pduType : "");
1123 * do_callback: add callback
1125 static int do_callback (void *obj, Tcl_Interp *interp,
1126 int argc, char **argv)
1135 else if (argc == -1)
1136 return ir_tcl_strdel (interp, &p->callback);
1142 if (ir_tcl_strdup (interp, &p->callback, argv[2]) == TCL_ERROR)
1152 * do_failback: add error handle callback
1154 static int do_failback (void *obj, Tcl_Interp *interp,
1155 int argc, char **argv)
1164 else if (argc == -1)
1165 return ir_tcl_strdel (interp, &p->failback);
1171 if (ir_tcl_strdup (interp, &p->failback, argv[2]) == TCL_ERROR)
1181 * do_protocol: Set/get protocol method on IR object
1183 static int do_protocol (void *o, Tcl_Interp *interp, int argc, char **argv)
1189 p->protocol_type = PROTO_Z3950;
1194 if (!strcmp (argv[2], "Z39"))
1195 p->protocol_type = PROTO_Z3950;
1196 else if (!strcmp (argv[2], "SR"))
1197 p->protocol_type = PROTO_SR;
1200 Tcl_AppendResult (interp, "Bad protocol: ", argv[2], NULL);
1205 switch (p->protocol_type)
1208 Tcl_AppendElement (interp, "Z39");
1211 Tcl_AppendElement (interp, "SR");
1218 * do_triggerResourceControl:
1220 static int do_triggerResourceControl (void *obj, Tcl_Interp *interp,
1221 int argc, char **argv)
1225 Z_TriggerResourceControlRequest *req;
1226 bool_t is_false = 0;
1232 interp->result = "triggerResourceControl: not connected";
1235 apdu = zget_APDU (p->odr_out, Z_APDU_triggerResourceControlRequest);
1236 req = apdu->u.triggerResourceControlRequest;
1237 *req->requestedAction = Z_TriggerResourceCtrl_cancel;
1238 req->resultSetWanted = &is_false;
1240 return ir_tcl_send_APDU (interp, p, apdu, "triggerResourceControl",
1245 * do_databaseNames: specify database names
1247 static int do_databaseNames (void *obj, Tcl_Interp *interp,
1248 int argc, char **argv)
1251 IrTcl_SetCObj *p = obj;
1255 for (i=0; i<p->num_databaseNames; i++)
1256 free (p->databaseNames[i]);
1257 free (p->databaseNames);
1261 p->num_databaseNames = 0;
1262 p->databaseNames = NULL;
1267 for (i=0; i<p->num_databaseNames; i++)
1268 Tcl_AppendElement (interp, p->databaseNames[i]);
1271 if (p->databaseNames)
1273 for (i=0; i<p->num_databaseNames; i++)
1274 free (p->databaseNames[i]);
1275 free (p->databaseNames);
1277 p->num_databaseNames = argc - 2;
1279 ir_tcl_malloc (sizeof(*p->databaseNames) * p->num_databaseNames);
1280 for (i=0; i<p->num_databaseNames; i++)
1282 if (ir_tcl_strdup (interp, &p->databaseNames[i], argv[2+i])
1290 * do_replaceIndicator: Set/get replace Set indicator
1292 static int do_replaceIndicator (void *obj, Tcl_Interp *interp,
1293 int argc, char **argv)
1295 IrTcl_SetCObj *p = obj;
1299 p->replaceIndicator = 1;
1302 return get_set_int (&p->replaceIndicator, interp, argc, argv);
1306 * do_queryType: Set/Get query method
1308 static int do_queryType (void *obj, Tcl_Interp *interp,
1309 int argc, char **argv)
1311 IrTcl_SetCObj *p = obj;
1314 return ir_tcl_strdup (interp, &p->queryType, "rpn");
1315 else if (argc == -1)
1316 return ir_tcl_strdel (interp, &p->queryType);
1319 free (p->queryType);
1320 if (ir_tcl_strdup (interp, &p->queryType, argv[2]) == TCL_ERROR)
1323 Tcl_AppendResult (interp, p->queryType, NULL);
1328 * do_userInformationField: Get User information field
1330 static int do_userInformationField (void *obj, Tcl_Interp *interp,
1331 int argc, char **argv)
1337 p->userInformationField = NULL;
1340 else if (argc == -1)
1341 return ir_tcl_strdel (interp, &p->userInformationField);
1342 Tcl_AppendResult (interp, p->userInformationField, NULL);
1347 * do_smallSetUpperBound: Set/get small set upper bound
1349 static int do_smallSetUpperBound (void *o, Tcl_Interp *interp,
1350 int argc, char **argv)
1352 IrTcl_SetCObj *p = o;
1356 p->smallSetUpperBound = 0;
1359 return get_set_int (&p->smallSetUpperBound, interp, argc, argv);
1363 * do_largeSetLowerBound: Set/get large set lower bound
1365 static int do_largeSetLowerBound (void *o, Tcl_Interp *interp,
1366 int argc, char **argv)
1368 IrTcl_SetCObj *p = o;
1372 p->largeSetLowerBound = 2;
1375 return get_set_int (&p->largeSetLowerBound, interp, argc, argv);
1379 * do_mediumSetPresentNumber: Set/get large set lower bound
1381 static int do_mediumSetPresentNumber (void *o, Tcl_Interp *interp,
1382 int argc, char **argv)
1384 IrTcl_SetCObj *p = o;
1388 p->mediumSetPresentNumber = 0;
1391 return get_set_int (&p->mediumSetPresentNumber, interp, argc, argv);
1395 * do_referenceId: Set/Get referenceId
1397 static int do_referenceId (void *obj, Tcl_Interp *interp,
1398 int argc, char **argv)
1400 IrTcl_SetCObj *p = obj;
1404 p->referenceId = NULL;
1407 else if (argc == -1)
1408 return ir_tcl_strdel (interp, &p->referenceId);
1411 free (p->referenceId);
1412 if (ir_tcl_strdup (interp, &p->referenceId, argv[2]) == TCL_ERROR)
1415 Tcl_AppendResult (interp, p->referenceId, NULL);
1420 * do_preferredRecordSyntax: Set/get preferred record syntax
1422 static int do_preferredRecordSyntax (void *obj, Tcl_Interp *interp,
1423 int argc, char **argv)
1425 IrTcl_SetCObj *p = obj;
1429 p->preferredRecordSyntax = NULL;
1432 else if (argc == -1)
1434 free (p->preferredRecordSyntax);
1435 p->preferredRecordSyntax = NULL;
1440 free (p->preferredRecordSyntax);
1441 p->preferredRecordSyntax = NULL;
1442 if (argv[2][0] && (p->preferredRecordSyntax =
1443 ir_tcl_malloc (sizeof(*p->preferredRecordSyntax))))
1444 *p->preferredRecordSyntax = IrTcl_getRecordSyntaxVal (argv[2]);
1451 * do_elementSetNames: Set/Get element Set Names
1453 static int do_elementSetNames (void *obj, Tcl_Interp *interp,
1454 int argc, char **argv)
1456 IrTcl_SetCObj *p = obj;
1460 p->elementSetNames = NULL;
1463 else if (argc == -1)
1464 return ir_tcl_strdel (interp, &p->elementSetNames);
1467 free (p->elementSetNames);
1468 if (ir_tcl_strdup (interp, &p->elementSetNames, argv[2]) == TCL_ERROR)
1471 Tcl_AppendResult (interp, p->elementSetNames, NULL);
1476 * do_smallSetElementSetNames: Set/Get small Set Element Set Names
1478 static int do_smallSetElementSetNames (void *obj, Tcl_Interp *interp,
1479 int argc, char **argv)
1481 IrTcl_SetCObj *p = obj;
1485 p->smallSetElementSetNames = NULL;
1488 else if (argc == -1)
1489 return ir_tcl_strdel (interp, &p->smallSetElementSetNames);
1492 free (p->smallSetElementSetNames);
1493 if (ir_tcl_strdup (interp, &p->smallSetElementSetNames,
1494 argv[2]) == TCL_ERROR)
1497 Tcl_AppendResult (interp, p->smallSetElementSetNames, NULL);
1502 * do_mediumSetElementSetNames: Set/Get medium Set Element Set Names
1504 static int do_mediumSetElementSetNames (void *obj, Tcl_Interp *interp,
1505 int argc, char **argv)
1507 IrTcl_SetCObj *p = obj;
1511 p->mediumSetElementSetNames = NULL;
1514 else if (argc == -1)
1515 return ir_tcl_strdel (interp, &p->mediumSetElementSetNames);
1518 free (p->mediumSetElementSetNames);
1519 if (ir_tcl_strdup (interp, &p->mediumSetElementSetNames,
1520 argv[2]) == TCL_ERROR)
1523 Tcl_AppendResult (interp, p->mediumSetElementSetNames, NULL);
1527 static IrTcl_Method ir_method_tab[] = {
1528 { 1, "comstack", do_comstack },
1529 { 1, "protocol", do_protocol },
1530 { 0, "failback", do_failback },
1531 { 0, "failInfo", do_failInfo },
1532 { 0, "logLevel", do_logLevel },
1534 { 0, "PDUType", do_pduType },
1535 { 1, "connect", do_connect },
1536 { 0, "protocolVersion", do_protocolVersion },
1537 { 1, "preferredMessageSize", do_preferredMessageSize },
1538 { 1, "maximumRecordSize", do_maximumRecordSize },
1539 { 1, "implementationName", do_implementationName },
1540 { 1, "implementationId", do_implementationId },
1541 { 1, "implementationVersion", do_implementationVersion },
1542 { 0, "targetImplementationName", do_targetImplementationName },
1543 { 0, "targetImplementationId", do_targetImplementationId },
1544 { 0, "targetImplementationVersion", do_targetImplementationVersion },
1545 { 0, "userInformationField", do_userInformationField },
1546 { 1, "idAuthentication", do_idAuthentication },
1547 { 0, "options", do_options },
1548 { 0, "init", do_init_request },
1549 { 0, "initResult", do_initResult },
1550 { 0, "disconnect", do_disconnect },
1551 { 0, "callback", do_callback },
1552 { 0, "triggerResourceControl", do_triggerResourceControl },
1556 static IrTcl_Method ir_set_c_method_tab[] = {
1557 { 0, "databaseNames", do_databaseNames},
1558 { 0, "replaceIndicator", do_replaceIndicator},
1559 { 0, "queryType", do_queryType },
1560 { 0, "preferredRecordSyntax", do_preferredRecordSyntax },
1561 { 0, "smallSetUpperBound", do_smallSetUpperBound},
1562 { 0, "largeSetLowerBound", do_largeSetLowerBound},
1563 { 0, "mediumSetPresentNumber", do_mediumSetPresentNumber},
1564 { 0, "referenceId", do_referenceId },
1565 { 0, "elementSetNames", do_elementSetNames },
1566 { 0, "smallSetElementSetNames", do_smallSetElementSetNames },
1567 { 0, "mediumSetElementSetNames", do_mediumSetElementSetNames },
1572 * ir_obj_method: IR Object methods
1574 static int ir_obj_method (ClientData clientData, Tcl_Interp *interp,
1575 int argc, char **argv)
1577 IrTcl_Methods tab[3];
1578 IrTcl_Obj *p = clientData;
1581 return ir_method_r (clientData, interp, argc, argv, ir_method_tab);
1583 tab[0].tab = ir_method_tab;
1585 tab[1].tab = ir_set_c_method_tab;
1586 tab[1].obj = &p->set_inher;
1589 return ir_method (interp, argc, argv, tab);
1593 * ir_obj_delete: IR Object disposal
1595 static void ir_obj_delete (ClientData clientData)
1597 IrTcl_Obj *obj = clientData;
1598 IrTcl_Methods tab[3];
1601 if (obj->ref_count > 0)
1603 assert (obj->ref_count == 0);
1605 logf (LOG_DEBUG, "ir object delete");
1606 tab[0].tab = ir_method_tab;
1608 tab[1].tab = ir_set_c_method_tab;
1609 tab[1].obj = &obj->set_inher;
1612 ir_method (NULL, -1, NULL, tab);
1615 odr_destroy (obj->odr_in);
1616 odr_destroy (obj->odr_out);
1617 odr_destroy (obj->odr_pr);
1622 * ir_obj_mk: IR Object creation
1624 static int ir_obj_mk (ClientData clientData, Tcl_Interp *interp,
1625 int argc, char **argv)
1627 IrTcl_Methods tab[3];
1635 interp->result = "wrong # args";
1638 obj = ir_tcl_malloc (sizeof(*obj));
1641 obj->bibset = ccl_qual_mk ();
1642 if ((inf = fopen ("default.bib", "r")))
1644 ccl_qual_file (obj->bibset, inf);
1649 logf (LOG_DEBUG, "ir object create");
1650 obj->odr_in = odr_createmem (ODR_DECODE);
1651 obj->odr_out = odr_createmem (ODR_ENCODE);
1652 obj->odr_pr = odr_createmem (ODR_PRINT);
1653 obj->state = IR_TCL_R_Idle;
1654 obj->interp = interp;
1658 obj->request_queue = NULL;
1660 tab[0].tab = ir_method_tab;
1662 tab[1].tab = ir_set_c_method_tab;
1663 tab[1].obj = &obj->set_inher;
1666 if (ir_method (interp, 0, NULL, tab) == TCL_ERROR)
1668 Tcl_CreateCommand (interp, argv[1], ir_obj_method,
1669 (ClientData) obj, ir_obj_delete);
1673 /* ------------------------------------------------------- */
1675 * do_search: Do search request
1677 static int do_search (void *o, Tcl_Interp *interp, int argc, char **argv)
1679 Z_SearchRequest *req;
1683 IrTcl_SetObj *obj = o;
1694 interp->result = "wrong # args";
1697 if (!obj->set_inher.num_databaseNames)
1699 interp->result = "no databaseNames";
1704 interp->result = "search: not connected";
1707 apdu = zget_APDU (p->odr_out, Z_APDU_searchRequest);
1708 req = apdu->u.searchRequest;
1712 bib1.proto = p->protocol_type;
1713 bib1.oclass = CLASS_ATTSET;
1714 bib1.value = VAL_BIB1;
1716 set_referenceId (p->odr_out, &req->referenceId,
1717 obj->set_inher.referenceId);
1719 req->smallSetUpperBound = &obj->set_inher.smallSetUpperBound;
1720 req->largeSetLowerBound = &obj->set_inher.largeSetLowerBound;
1721 req->mediumSetPresentNumber = &obj->set_inher.mediumSetPresentNumber;
1722 req->replaceIndicator = &obj->set_inher.replaceIndicator;
1723 req->resultSetName = obj->setName ? obj->setName : "Default";
1724 logf (LOG_DEBUG, "Search, resultSetName %s", req->resultSetName);
1725 req->num_databaseNames = obj->set_inher.num_databaseNames;
1726 req->databaseNames = obj->set_inher.databaseNames;
1727 for (r=0; r < obj->set_inher.num_databaseNames; r++)
1728 logf (LOG_DEBUG, " Database %s", obj->set_inher.databaseNames[r]);
1729 if (obj->set_inher.preferredRecordSyntax)
1731 struct oident ident;
1733 ident.proto = p->protocol_type;
1734 ident.oclass = CLASS_RECSYN;
1735 ident.value = *obj->set_inher.preferredRecordSyntax;
1736 logf (LOG_DEBUG, "Preferred record syntax is %d", ident.value);
1737 req->preferredRecordSyntax = odr_oiddup (p->odr_out,
1738 oid_getoidbyent (&ident));
1741 req->preferredRecordSyntax = 0;
1743 if (obj->set_inher.smallSetElementSetNames &&
1744 *obj->set_inher.smallSetElementSetNames)
1746 Z_ElementSetNames *esn = odr_malloc (p->odr_out, sizeof(*esn));
1748 esn->which = Z_ElementSetNames_generic;
1749 esn->u.generic = obj->set_inher.smallSetElementSetNames;
1750 req->smallSetElementSetNames = esn;
1753 req->smallSetElementSetNames = NULL;
1755 if (obj->set_inher.mediumSetElementSetNames &&
1756 *obj->set_inher.mediumSetElementSetNames)
1758 Z_ElementSetNames *esn = odr_malloc (p->odr_out, sizeof(*esn));
1760 esn->which = Z_ElementSetNames_generic;
1761 esn->u.generic = obj->set_inher.mediumSetElementSetNames;
1762 req->mediumSetElementSetNames = esn;
1765 req->mediumSetElementSetNames = NULL;
1767 req->query = &query;
1769 if (!strcmp (obj->set_inher.queryType, "rpn"))
1771 Z_RPNQuery *RPNquery;
1773 RPNquery = p_query_rpn (p->odr_out, argv[2]);
1776 Tcl_AppendResult (interp, "Syntax error in query", NULL);
1779 RPNquery->attributeSetId = oid_getoidbyent (&bib1);
1780 query.which = Z_Query_type_1;
1781 query.u.type_1 = RPNquery;
1782 logf (LOG_DEBUG, "RPN");
1785 else if (!strcmp (obj->set_inher.queryType, "cclrpn"))
1789 struct ccl_rpn_node *rpn;
1790 Z_RPNQuery *RPNquery;
1792 rpn = ccl_find_str(p->bibset, argv[2], &error, &pos);
1795 Tcl_AppendResult (interp, "CCL error: ",
1796 ccl_err_msg(error), NULL);
1799 ccl_pr_tree (rpn, stderr);
1800 fprintf (stderr, "\n");
1801 assert((RPNquery = ccl_rpn_query(rpn)));
1802 RPNquery->attributeSetId = oid_getoidbyent (&bib1);
1803 query.which = Z_Query_type_1;
1804 query.u.type_1 = RPNquery;
1805 logf (LOG_DEBUG, "CCLRPN");
1808 else if (!strcmp (obj->set_inher.queryType, "ccl"))
1810 query.which = Z_Query_type_2;
1811 query.u.type_2 = &ccl_query;
1812 ccl_query.buf = (unsigned char *) argv[2];
1813 ccl_query.len = strlen (argv[2]);
1814 logf (LOG_DEBUG, "CCL");
1818 interp->result = "unknown query method";
1821 return ir_tcl_send_APDU (interp, p, apdu, "search", argv[0]);
1825 * do_resultCount: Get number of hits
1827 static int do_resultCount (void *o, Tcl_Interp *interp,
1828 int argc, char **argv)
1830 IrTcl_SetObj *obj = o;
1834 return get_set_int (&obj->resultCount, interp, argc, argv);
1838 * do_searchStatus: Get search status (after search response)
1840 static int do_searchStatus (void *o, Tcl_Interp *interp,
1841 int argc, char **argv)
1843 IrTcl_SetObj *obj = o;
1847 return get_set_int (&obj->searchStatus, interp, argc, argv);
1851 * do_presentStatus: Get search status (after search/present response)
1853 static int do_presentStatus (void *o, Tcl_Interp *interp,
1854 int argc, char **argv)
1856 IrTcl_SetObj *obj = o;
1860 return get_set_int (&obj->presentStatus, interp, argc, argv);
1864 * do_nextResultSetPosition: Get next result set position
1865 * (after search/present response)
1867 static int do_nextResultSetPosition (void *o, Tcl_Interp *interp,
1868 int argc, char **argv)
1870 IrTcl_SetObj *obj = o;
1874 obj->nextResultSetPosition = 0;
1877 return get_set_int (&obj->nextResultSetPosition, interp, argc, argv);
1881 * do_setName: Set result Set name
1883 static int do_setName (void *o, Tcl_Interp *interp,
1884 int argc, char **argv)
1886 IrTcl_SetObj *obj = o;
1889 return ir_tcl_strdup (interp, &obj->setName, "Default");
1890 else if (argc == -1)
1891 return ir_tcl_strdel (interp, &obj->setName);
1894 free (obj->setName);
1895 if (ir_tcl_strdup (interp, &obj->setName, argv[2])
1899 Tcl_AppendElement (interp, obj->setName);
1904 * do_numberOfRecordsReturned: Get number of records returned
1906 static int do_numberOfRecordsReturned (void *o, Tcl_Interp *interp,
1907 int argc, char **argv)
1909 IrTcl_SetObj *obj = o;
1913 obj->numberOfRecordsReturned = 0;
1916 return get_set_int (&obj->numberOfRecordsReturned, interp, argc, argv);
1920 * do_type: Return type (if any) at position.
1922 static int do_type (void *o, Tcl_Interp *interp, int argc, char **argv)
1924 IrTcl_SetObj *obj = o;
1926 IrTcl_RecordList *rl;
1930 obj->record_list = NULL;
1933 else if (argc == -1)
1935 delete_IR_records (obj);
1940 sprintf (interp->result, "wrong # args");
1943 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
1945 rl = find_IR_record (obj, offset);
1948 logf (LOG_DEBUG, "No record at position %d", offset);
1953 case Z_NamePlusRecord_databaseRecord:
1954 interp->result = "DB";
1956 case Z_NamePlusRecord_surrogateDiagnostic:
1957 interp->result = "SD";
1965 * do_recordType: Return record type (if any) at position.
1967 static int do_recordType (void *o, Tcl_Interp *interp, int argc, char **argv)
1969 IrTcl_SetObj *obj = o;
1971 IrTcl_RecordList *rl;
1977 else if (argc == -1)
1983 sprintf (interp->result, "wrong # args");
1986 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
1988 rl = find_IR_record (obj, offset);
1991 if (rl->which != Z_NamePlusRecord_databaseRecord)
1993 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
1996 Tcl_AppendElement (interp, (char*)
1997 IrTcl_getRecordSyntaxStr (rl->u.dbrec.type));
2002 * set record elements (for record extraction)
2004 static int do_recordElements (void *o, Tcl_Interp *interp,
2005 int argc, char **argv)
2007 IrTcl_SetObj *obj = o;
2011 obj->recordElements = NULL;
2014 else if (argc == -1)
2015 return ir_tcl_strdel (NULL, &obj->recordElements);
2018 sprintf (interp->result, "wrong # args");
2023 free (obj->recordElements);
2024 return ir_tcl_strdup (NULL, &obj->recordElements,
2025 (*argv[2] ? argv[2] : NULL));
2027 Tcl_AppendResult (interp, obj->recordElements, NULL);
2034 static int ir_diagResult (Tcl_Interp *interp, IrTcl_Diagnostic *list, int num)
2040 for (i = 0; i<num; i++)
2042 logf (LOG_DEBUG, "Diagnostic, code %d", list[i].condition);
2043 sprintf (buf, "%d", list[i].condition);
2044 Tcl_AppendElement (interp, buf);
2045 cp = diagbib1_str (list[i].condition);
2047 Tcl_AppendElement (interp, (char*) cp);
2049 Tcl_AppendElement (interp, "");
2050 if (list[i].addinfo)
2051 Tcl_AppendElement (interp, (char*) list[i].addinfo);
2053 Tcl_AppendElement (interp, "");
2059 * do_diag: Return diagnostic record info
2061 static int do_diag (void *o, Tcl_Interp *interp, int argc, char **argv)
2063 IrTcl_SetObj *obj = o;
2065 IrTcl_RecordList *rl;
2071 sprintf (interp->result, "wrong # args");
2074 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2076 rl = find_IR_record (obj, offset);
2079 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2082 if (rl->which != Z_NamePlusRecord_surrogateDiagnostic)
2084 Tcl_AppendResult (interp, "No Diagnostic record at #", argv[2], NULL);
2087 return ir_diagResult (interp, rl->u.surrogateDiagnostics.list,
2088 rl->u.surrogateDiagnostics.num);
2092 * do_getMarc: Get ISO2709 Record lines/fields
2094 static int do_getMarc (void *o, Tcl_Interp *interp, int argc, char **argv)
2096 IrTcl_SetObj *obj = o;
2098 IrTcl_RecordList *rl;
2104 sprintf (interp->result, "wrong # args");
2107 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2109 rl = find_IR_record (obj, offset);
2112 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2115 if (rl->which != Z_NamePlusRecord_databaseRecord)
2117 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2120 return ir_tcl_get_marc (interp, rl->u.dbrec.buf, argc, argv);
2124 * do_getSutrs: Get SUTRS Record
2126 static int do_getSutrs (void *o, Tcl_Interp *interp, int argc, char **argv)
2128 IrTcl_SetObj *obj = o;
2130 IrTcl_RecordList *rl;
2136 sprintf (interp->result, "wrong # args");
2139 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2141 rl = find_IR_record (obj, offset);
2144 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2147 if (rl->which != Z_NamePlusRecord_databaseRecord)
2149 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2152 if (rl->u.dbrec.type != VAL_SUTRS)
2154 Tcl_AppendElement (interp, rl->u.dbrec.buf);
2160 * do_getGrs: Get a GRS1 Record
2162 static int do_getGrs (void *o, Tcl_Interp *interp, int argc, char **argv)
2164 IrTcl_SetObj *obj = o;
2166 IrTcl_RecordList *rl;
2172 sprintf (interp->result, "wrong # args");
2175 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2177 rl = find_IR_record (obj, offset);
2180 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2183 if (rl->which != Z_NamePlusRecord_databaseRecord)
2185 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2188 if (rl->u.dbrec.type != VAL_GRS1)
2190 return ir_tcl_get_grs (interp, rl->u.dbrec.u.grs1, argc, argv);
2195 * do_responseStatus: Return response status (present or search)
2197 static int do_responseStatus (void *o, Tcl_Interp *interp,
2198 int argc, char **argv)
2200 IrTcl_SetObj *obj = o;
2204 obj->recordFlag = 0;
2205 obj->nonSurrogateDiagnosticNum = 0;
2206 obj->nonSurrogateDiagnosticList = NULL;
2209 else if (argc == -1)
2211 ir_deleteDiags (&obj->nonSurrogateDiagnosticList,
2212 &obj->nonSurrogateDiagnosticNum);
2215 if (!obj->recordFlag)
2217 Tcl_AppendElement (interp, "OK");
2222 case Z_Records_DBOSD:
2223 Tcl_AppendElement (interp, "DBOSD");
2226 Tcl_AppendElement (interp, "NSD");
2227 return ir_diagResult (interp, obj->nonSurrogateDiagnosticList,
2228 obj->nonSurrogateDiagnosticNum);
2234 * do_present: Perform Present Request
2237 static int do_present (void *o, Tcl_Interp *interp, int argc, char **argv)
2239 IrTcl_SetObj *obj = o;
2242 Z_PresentRequest *req;
2250 if (Tcl_GetInt (interp, argv[2], &start) == TCL_ERROR)
2257 if (Tcl_GetInt (interp, argv[3], &number) == TCL_ERROR)
2265 interp->result = "present: not connected";
2270 obj->number = number;
2272 apdu = zget_APDU (p->odr_out, Z_APDU_presentRequest);
2273 req = apdu->u.presentRequest;
2275 set_referenceId (p->odr_out, &req->referenceId,
2276 obj->set_inher.referenceId);
2278 req->resultSetId = obj->setName ? obj->setName : "Default";
2280 req->resultSetStartPoint = &start;
2281 req->numberOfRecordsRequested = &number;
2282 if (obj->set_inher.preferredRecordSyntax)
2284 struct oident ident;
2286 ident.proto = p->protocol_type;
2287 ident.oclass = CLASS_RECSYN;
2288 ident.value = *obj->set_inher.preferredRecordSyntax;
2289 logf (LOG_DEBUG, "Preferred record syntax is %d", ident.value);
2290 req->preferredRecordSyntax = odr_oiddup (p->odr_out,
2291 oid_getoidbyent (&ident));
2294 req->preferredRecordSyntax = 0;
2296 if (obj->set_inher.elementSetNames && *obj->set_inher.elementSetNames)
2298 Z_ElementSetNames *esn = odr_malloc (p->odr_out, sizeof(*esn));
2299 Z_RecordComposition *compo = odr_malloc (p->odr_out, sizeof(*compo));
2301 esn->which = Z_ElementSetNames_generic;
2302 esn->u.generic = obj->set_inher.elementSetNames;
2304 req->recordComposition = compo;
2305 compo->which = Z_RecordComp_simple;
2306 compo->u.simple = esn;
2309 req->recordComposition = NULL;
2310 return ir_tcl_send_APDU (interp, p, apdu, "present", argv[0]);
2314 * do_loadFile: Load result set from file
2317 static int do_loadFile (void *o, Tcl_Interp *interp,
2318 int argc, char **argv)
2320 IrTcl_SetObj *setobj = o;
2330 interp->result = "wrong # args";
2333 inf = fopen (argv[2], "r");
2336 Tcl_AppendResult (interp, "Cannot open ", argv[2], NULL);
2339 while ((buf = ir_tcl_fread_marc (inf, &size)))
2341 IrTcl_RecordList *rl;
2343 rl = new_IR_record (setobj, no, Z_NamePlusRecord_databaseRecord, "F");
2344 rl->u.dbrec.type = VAL_USMARC;
2345 rl->u.dbrec.buf = buf;
2346 rl->u.dbrec.size = size;
2349 setobj->numberOfRecordsReturned = no-1;
2354 static IrTcl_Method ir_set_method_tab[] = {
2355 { 0, "search", do_search },
2356 { 0, "searchStatus", do_searchStatus },
2357 { 0, "presentStatus", do_presentStatus },
2358 { 0, "nextResultSetPosition", do_nextResultSetPosition },
2359 { 0, "setName", do_setName },
2360 { 0, "resultCount", do_resultCount },
2361 { 0, "numberOfRecordsReturned", do_numberOfRecordsReturned },
2362 { 0, "present", do_present },
2363 { 0, "type", do_type },
2364 { 0, "getMarc", do_getMarc },
2365 { 0, "getSutrs", do_getSutrs },
2366 { 0, "getGrs", do_getGrs },
2367 { 0, "recordType", do_recordType },
2368 { 0, "recordElements", do_recordElements },
2369 { 0, "diag", do_diag },
2370 { 0, "responseStatus", do_responseStatus },
2371 { 0, "loadFile", do_loadFile },
2376 * ir_set_obj_method: IR Set Object methods
2378 static int ir_set_obj_method (ClientData clientData, Tcl_Interp *interp,
2379 int argc, char **argv)
2381 IrTcl_Methods tabs[3];
2382 IrTcl_SetObj *p = clientData;
2386 interp->result = "wrong # args";
2389 tabs[0].tab = ir_set_method_tab;
2391 tabs[1].tab = ir_set_c_method_tab;
2392 tabs[1].obj = &p->set_inher;
2395 return ir_method (interp, argc, argv, tabs);
2399 * ir_set_obj_delete: IR Set Object disposal
2401 static void ir_set_obj_delete (ClientData clientData)
2403 IrTcl_Methods tabs[3];
2404 IrTcl_SetObj *p = clientData;
2406 logf (LOG_DEBUG, "ir set delete");
2408 tabs[0].tab = ir_set_method_tab;
2410 tabs[1].tab = ir_set_c_method_tab;
2411 tabs[1].obj = &p->set_inher;
2414 ir_method (NULL, -1, NULL, tabs);
2420 * ir_set_obj_mk: IR Set Object creation
2422 static int ir_set_obj_mk (ClientData clientData, Tcl_Interp *interp,
2423 int argc, char **argv)
2425 IrTcl_Methods tabs[3];
2428 if (argc < 2 || argc > 3)
2430 interp->result = "wrong # args";
2433 obj = ir_tcl_malloc (sizeof(*obj));
2434 logf (LOG_DEBUG, "ir set create");
2437 Tcl_CmdInfo parent_info;
2442 if (!Tcl_GetCommandInfo (interp, argv[2], &parent_info))
2444 interp->result = "No parent";
2447 obj->parent = (IrTcl_Obj *) parent_info.clientData;
2449 dst = &obj->set_inher;
2450 src = &obj->parent->set_inher;
2452 if ((dst->num_databaseNames = src->num_databaseNames))
2453 dst->databaseNames =
2454 ir_tcl_malloc (sizeof (*dst->databaseNames)
2455 * dst->num_databaseNames);
2457 dst->databaseNames = NULL;
2458 for (i = 0; i < dst->num_databaseNames; i++)
2459 if (ir_tcl_strdup (interp, &dst->databaseNames[i],
2460 src->databaseNames[i]) == TCL_ERROR)
2462 if (ir_tcl_strdup (interp, &dst->queryType, src->queryType)
2466 if (ir_tcl_strdup (interp, &dst->referenceId, src->referenceId)
2470 if (ir_tcl_strdup (interp, &dst->elementSetNames, src->elementSetNames)
2474 if (ir_tcl_strdup (interp, &dst->smallSetElementSetNames,
2475 src->smallSetElementSetNames)
2479 if (ir_tcl_strdup (interp, &dst->mediumSetElementSetNames,
2480 src->mediumSetElementSetNames)
2484 if (src->preferredRecordSyntax &&
2485 (dst->preferredRecordSyntax
2486 = ir_tcl_malloc (sizeof(*dst->preferredRecordSyntax))))
2487 *dst->preferredRecordSyntax = *src->preferredRecordSyntax;
2489 dst->preferredRecordSyntax = NULL;
2490 dst->replaceIndicator = src->replaceIndicator;
2491 dst->smallSetUpperBound = src->smallSetUpperBound;
2492 dst->largeSetLowerBound = src->largeSetLowerBound;
2493 dst->mediumSetPresentNumber = src->mediumSetPresentNumber;
2498 tabs[0].tab = ir_set_method_tab;
2502 if (ir_method (interp, 0, NULL, tabs) == TCL_ERROR)
2505 Tcl_CreateCommand (interp, argv[1], ir_set_obj_method,
2506 (ClientData) obj, ir_set_obj_delete);
2510 /* ------------------------------------------------------- */
2513 * do_scan: Perform scan
2515 static int do_scan (void *o, Tcl_Interp *interp, int argc, char **argv)
2519 IrTcl_ScanObj *obj = o;
2520 IrTcl_Obj *p = obj->parent;
2523 struct ccl_rpn_node *rpn;
2531 interp->result = "wrong # args";
2534 if (!p->set_inher.num_databaseNames)
2536 interp->result = "no databaseNames";
2541 interp->result = "scan: not connected";
2545 bib1.proto = p->protocol_type;
2546 bib1.oclass = CLASS_ATTSET;
2547 bib1.value = VAL_BIB1;
2549 apdu = zget_APDU (p->odr_out, Z_APDU_scanRequest);
2550 req = apdu->u.scanRequest;
2552 set_referenceId (p->odr_out, &req->referenceId, p->set_inher.referenceId);
2553 req->num_databaseNames = p->set_inher.num_databaseNames;
2554 req->databaseNames = p->set_inher.databaseNames;
2555 req->attributeSet = oid_getoidbyent (&bib1);
2558 if (!(req->termListAndStartPoint = p_query_scan (p->odr_out, argv[2])))
2560 Tcl_AppendResult (interp, "Syntax error in query", NULL);
2564 rpn = ccl_find_str(p->bibset, argv[2], &r, &pos);
2567 Tcl_AppendResult (interp, "CCL error: ", ccl_err_msg (r), NULL);
2570 ccl_pr_tree (rpn, stderr);
2571 fprintf (stderr, "\n");
2572 if (!(req->termListAndStartPoint = ccl_scan_query (rpn)))
2575 req->stepSize = &obj->stepSize;
2576 req->numberOfTermsRequested = &obj->numberOfTermsRequested;
2577 req->preferredPositionInResponse = &obj->preferredPositionInResponse;
2578 logf (LOG_DEBUG, "stepSize=%d", *req->stepSize);
2579 logf (LOG_DEBUG, "numberOfTermsRequested=%d",
2580 *req->numberOfTermsRequested);
2581 logf (LOG_DEBUG, "preferredPositionInResponse=%d",
2582 *req->preferredPositionInResponse);
2584 return ir_tcl_send_APDU (interp, p, apdu, "scan", argv[0]);
2588 * do_stepSize: Set/get replace Step Size
2590 static int do_stepSize (void *obj, Tcl_Interp *interp,
2591 int argc, char **argv)
2593 IrTcl_ScanObj *p = obj;
2599 return get_set_int (&p->stepSize, interp, argc, argv);
2603 * do_numberOfTermsRequested: Set/get Number of Terms requested
2605 static int do_numberOfTermsRequested (void *obj, Tcl_Interp *interp,
2606 int argc, char **argv)
2608 IrTcl_ScanObj *p = obj;
2612 p->numberOfTermsRequested = 20;
2615 return get_set_int (&p->numberOfTermsRequested, interp, argc, argv);
2620 * do_preferredPositionInResponse: Set/get preferred Position
2622 static int do_preferredPositionInResponse (void *obj, Tcl_Interp *interp,
2623 int argc, char **argv)
2625 IrTcl_ScanObj *p = obj;
2629 p->preferredPositionInResponse = 1;
2632 return get_set_int (&p->preferredPositionInResponse, interp, argc, argv);
2636 * do_scanStatus: Get scan status
2638 static int do_scanStatus (void *obj, Tcl_Interp *interp,
2639 int argc, char **argv)
2641 IrTcl_ScanObj *p = obj;
2645 return get_set_int (&p->scanStatus, interp, argc, argv);
2649 * do_numberOfEntriesReturned: Get number of Entries returned
2651 static int do_numberOfEntriesReturned (void *obj, Tcl_Interp *interp,
2652 int argc, char **argv)
2654 IrTcl_ScanObj *p = obj;
2658 return get_set_int (&p->numberOfEntriesReturned, interp, argc, argv);
2662 * do_positionOfTerm: Get position of Term
2664 static int do_positionOfTerm (void *obj, Tcl_Interp *interp,
2665 int argc, char **argv)
2667 IrTcl_ScanObj *p = obj;
2671 return get_set_int (&p->positionOfTerm, interp, argc, argv);
2675 * do_scanLine: get Scan Line (surrogate or normal) after response
2677 static int do_scanLine (void *obj, Tcl_Interp *interp, int argc, char **argv)
2679 IrTcl_ScanObj *p = obj;
2685 p->entries_flag = 0;
2687 p->nonSurrogateDiagnosticNum = 0;
2688 p->nonSurrogateDiagnosticList = 0;
2691 else if (argc == -1)
2693 p->entries_flag = 0;
2694 /* release entries */
2697 ir_deleteDiags (&p->nonSurrogateDiagnosticList,
2698 &p->nonSurrogateDiagnosticNum);
2703 interp->result = "wrong # args";
2706 if (Tcl_GetInt (interp, argv[2], &i) == TCL_ERROR)
2708 if (!p->entries_flag || p->which != Z_ListEntries_entries || !p->entries
2709 || i >= p->num_entries || i < 0)
2711 switch (p->entries[i].which)
2713 case Z_Entry_termInfo:
2714 Tcl_AppendElement (interp, "T");
2715 if (p->entries[i].u.term.buf)
2716 Tcl_AppendElement (interp, p->entries[i].u.term.buf);
2718 Tcl_AppendElement (interp, "");
2719 sprintf (numstr, "%d", p->entries[i].u.term.globalOccurrences);
2720 Tcl_AppendElement (interp, numstr);
2722 case Z_Entry_surrogateDiagnostic:
2723 Tcl_AppendElement (interp, "SD");
2724 return ir_diagResult (interp, p->entries[i].u.diag.list,
2725 p->entries[i].u.diag.num);
2731 static IrTcl_Method ir_scan_method_tab[] = {
2732 { 0, "scan", do_scan },
2733 { 0, "stepSize", do_stepSize },
2734 { 0, "numberOfTermsRequested", do_numberOfTermsRequested },
2735 { 0, "preferredPositionInResponse", do_preferredPositionInResponse },
2736 { 0, "scanStatus", do_scanStatus },
2737 { 0, "numberOfEntriesReturned", do_numberOfEntriesReturned },
2738 { 0, "positionOfTerm", do_positionOfTerm },
2739 { 0, "scanLine", do_scanLine },
2744 * ir_scan_obj_method: IR Scan Object methods
2746 static int ir_scan_obj_method (ClientData clientData, Tcl_Interp *interp,
2747 int argc, char **argv)
2749 IrTcl_Methods tabs[2];
2753 interp->result = "wrong # args";
2756 tabs[0].tab = ir_scan_method_tab;
2757 tabs[0].obj = clientData;
2760 return ir_method (interp, argc, argv, tabs);
2764 * ir_scan_obj_delete: IR Scan Object disposal
2766 static void ir_scan_obj_delete (ClientData clientData)
2768 IrTcl_Methods tabs[2];
2769 IrTcl_ScanObj *obj = clientData;
2771 tabs[0].tab = ir_scan_method_tab;
2775 ir_method (NULL, -1, NULL, tabs);
2780 * ir_scan_obj_mk: IR Scan Object creation
2782 static int ir_scan_obj_mk (ClientData clientData, Tcl_Interp *interp,
2783 int argc, char **argv)
2785 Tcl_CmdInfo parent_info;
2787 IrTcl_Methods tabs[2];
2791 interp->result = "wrong # args";
2794 if (!Tcl_GetCommandInfo (interp, argv[2], &parent_info))
2796 interp->result = "No parent";
2799 obj = ir_tcl_malloc (sizeof(*obj));
2800 obj->parent = (IrTcl_Obj *) parent_info.clientData;
2802 tabs[0].tab = ir_scan_method_tab;
2806 if (ir_method (interp, 0, NULL, tabs) == TCL_ERROR)
2808 Tcl_CreateCommand (interp, argv[1], ir_scan_obj_method,
2809 (ClientData) obj, ir_scan_obj_delete);
2813 /* ------------------------------------------------------- */
2815 static void ir_initResponse (void *obj, Z_InitResponse *initrs)
2819 p->initResult = *initrs->result ? 1 : 0;
2820 if (!*initrs->result)
2821 logf (LOG_DEBUG, "Connection rejected by target");
2823 logf (LOG_DEBUG, "Connection accepted by target");
2825 get_referenceId (&p->set_inher.referenceId, initrs->referenceId);
2827 free (p->targetImplementationId);
2828 ir_tcl_strdup (p->interp, &p->targetImplementationId,
2829 initrs->implementationId);
2830 free (p->targetImplementationName);
2831 ir_tcl_strdup (p->interp, &p->targetImplementationName,
2832 initrs->implementationName);
2833 free (p->targetImplementationVersion);
2834 ir_tcl_strdup (p->interp, &p->targetImplementationVersion,
2835 initrs->implementationVersion);
2837 p->maximumRecordSize = *initrs->maximumRecordSize;
2838 p->preferredMessageSize = *initrs->preferredMessageSize;
2840 memcpy (&p->options, initrs->options, sizeof(initrs->options));
2841 memcpy (&p->protocolVersion, initrs->protocolVersion,
2842 sizeof(initrs->protocolVersion));
2843 free (p->userInformationField);
2844 p->userInformationField = NULL;
2845 if (initrs->userInformationField)
2849 if (initrs->userInformationField->which == ODR_EXTERNAL_octet &&
2850 (p->userInformationField =
2851 ir_tcl_malloc ((len =
2852 initrs->userInformationField->
2853 u.octet_aligned->len) +1)))
2855 memcpy (p->userInformationField,
2856 initrs->userInformationField->u.octet_aligned->buf,
2858 (p->userInformationField)[len] = '\0';
2863 static void ir_deleteDiags (IrTcl_Diagnostic **dst_list, int *dst_num)
2866 for (i = 0; i<*dst_num; i++)
2867 free (dst_list[i]->addinfo);
2873 static void ir_handleDiags (IrTcl_Diagnostic **dst_list, int *dst_num,
2874 Z_DiagRec **list, int num)
2880 *dst_list = ir_tcl_malloc (sizeof(**dst_list) * num);
2881 for (i = 0; i<num; i++)
2883 switch (list[i]->which)
2885 case Z_DiagRec_defaultFormat:
2886 (*dst_list)[i].condition = *list[i]->u.defaultFormat->condition;
2887 addinfo = list[i]->u.defaultFormat->addinfo;
2889 ((*dst_list)[i].addinfo = ir_tcl_malloc (strlen(addinfo)+1)))
2890 strcpy ((*dst_list)[i].addinfo, addinfo);
2893 (*dst_list)[i].addinfo = NULL;
2894 (*dst_list)[i].condition = 0;
2899 static void ir_handleRecords (void *o, Z_Records *zrs, IrTcl_SetObj *setobj,
2900 const char *elements)
2905 IrTcl_RecordList *rl;
2907 setobj->which = zrs->which;
2908 setobj->recordFlag = 1;
2910 ir_deleteDiags (&setobj->nonSurrogateDiagnosticList,
2911 &setobj->nonSurrogateDiagnosticNum);
2912 if (zrs->which == Z_Records_DBOSD)
2914 setobj->numberOfRecordsReturned =
2915 zrs->u.databaseOrSurDiagnostics->num_records;
2916 logf (LOG_DEBUG, "Got %d records", setobj->numberOfRecordsReturned);
2917 for (offset = 0; offset<setobj->numberOfRecordsReturned; offset++)
2919 rl = new_IR_record (setobj, setobj->start + offset,
2920 zrs->u.databaseOrSurDiagnostics->
2921 records[offset]->which,
2923 if (rl->which == Z_NamePlusRecord_surrogateDiagnostic)
2925 ir_handleDiags (&rl->u.surrogateDiagnostics.list,
2926 &rl->u.surrogateDiagnostics.num,
2927 &zrs->u.databaseOrSurDiagnostics->
2928 records[offset]->u.surrogateDiagnostic,
2933 Z_DatabaseRecord *zr;
2935 struct oident *ident;
2937 zr = zrs->u.databaseOrSurDiagnostics->records[offset]
2939 oe = (Z_External*) zr;
2940 rl->u.dbrec.size = zr->u.octet_aligned->len;
2942 if ((ident = oid_getentbyoid (oe->direct_reference)))
2943 rl->u.dbrec.type = ident->value;
2945 rl->u.dbrec.type = VAL_USMARC;
2947 if (oe->which == ODR_EXTERNAL_octet && rl->u.dbrec.size > 0)
2949 char *buf = (char*) zr->u.octet_aligned->buf;
2950 if ((rl->u.dbrec.buf = ir_tcl_malloc (rl->u.dbrec.size)))
2951 memcpy (rl->u.dbrec.buf, buf, rl->u.dbrec.size);
2953 else if (rl->u.dbrec.type == VAL_SUTRS &&
2954 oe->which == Z_External_sutrs)
2956 odr_setbuf (p->odr_in, (char*) oe->u.single_ASN1_type->buf,
2957 oe->u.single_ASN1_type->len, 0);
2958 if ((rl->u.dbrec.buf = ir_tcl_malloc (oe->u.sutrs->len+1)))
2960 memcpy (rl->u.dbrec.buf, oe->u.sutrs->buf,
2962 rl->u.dbrec.buf[oe->u.sutrs->len] = '\0';
2964 rl->u.dbrec.size = oe->u.sutrs->len;
2966 else if (rl->u.dbrec.type == VAL_GRS1 &&
2967 oe->which == Z_External_grs1)
2969 ir_tcl_read_grs (oe->u.grs1, &rl->u.dbrec.u.grs1);
2970 rl->u.dbrec.buf = NULL;
2973 rl->u.dbrec.buf = NULL;
2977 else if (zrs->which == Z_Records_multipleNSD)
2979 logf (LOG_DEBUG, "multipleNonSurrogateDiagnostic %d",
2980 zrs->u.multipleNonSurDiagnostics->num_diagRecs);
2981 setobj->numberOfRecordsReturned = 0;
2982 ir_handleDiags (&setobj->nonSurrogateDiagnosticList,
2983 &setobj->nonSurrogateDiagnosticNum,
2984 zrs->u.multipleNonSurDiagnostics->diagRecs,
2985 zrs->u.multipleNonSurDiagnostics->num_diagRecs);
2989 logf (LOG_DEBUG, "NonSurrogateDiagnostic");
2990 setobj->numberOfRecordsReturned = 0;
2991 ir_handleDiags (&setobj->nonSurrogateDiagnosticList,
2992 &setobj->nonSurrogateDiagnosticNum,
2993 &zrs->u.nonSurrogateDiagnostic,
2998 static void ir_searchResponse (void *o, Z_SearchResponse *searchrs,
2999 IrTcl_SetObj *setobj)
3001 Z_Records *zrs = searchrs->records;
3003 logf (LOG_DEBUG, "Received search response");
3006 logf (LOG_DEBUG, "Search response, no object!");
3009 setobj->searchStatus = searchrs->searchStatus ? 1 : 0;
3010 get_referenceId (&setobj->set_inher.referenceId, searchrs->referenceId);
3011 setobj->resultCount = *searchrs->resultCount;
3012 if (searchrs->presentStatus)
3013 setobj->presentStatus = *searchrs->presentStatus;
3014 if (searchrs->nextResultSetPosition)
3015 setobj->nextResultSetPosition = *searchrs->nextResultSetPosition;
3017 logf (LOG_DEBUG, "Search response %d, %d hits",
3018 setobj->searchStatus, setobj->resultCount);
3022 if (setobj->resultCount <= setobj->set_inher.smallSetUpperBound)
3023 es = setobj->set_inher.smallSetElementSetNames;
3025 es = setobj->set_inher.mediumSetElementSetNames;
3026 ir_handleRecords (o, zrs, setobj, es);
3029 setobj->recordFlag = 0;
3033 static void ir_presentResponse (void *o, Z_PresentResponse *presrs,
3034 IrTcl_SetObj *setobj)
3036 Z_Records *zrs = presrs->records;
3038 logf (LOG_DEBUG, "Received present response");
3041 logf (LOG_DEBUG, "Present response, no object!");
3044 setobj->presentStatus = *presrs->presentStatus;
3045 get_referenceId (&setobj->set_inher.referenceId, presrs->referenceId);
3046 setobj->nextResultSetPosition = *presrs->nextResultSetPosition;
3048 ir_handleRecords (o, zrs, setobj, setobj->set_inher.elementSetNames);
3051 setobj->recordFlag = 0;
3052 logf (LOG_DEBUG, "No records!");
3056 static void ir_scanResponse (void *o, Z_ScanResponse *scanrs,
3057 IrTcl_ScanObj *scanobj)
3061 logf (LOG_DEBUG, "Received scanResponse");
3063 get_referenceId (&p->set_inher.referenceId, scanrs->referenceId);
3064 scanobj->scanStatus = *scanrs->scanStatus;
3065 logf (LOG_DEBUG, "scanStatus=%d", scanobj->scanStatus);
3067 if (scanrs->stepSize)
3068 scanobj->stepSize = *scanrs->stepSize;
3069 logf (LOG_DEBUG, "stepSize=%d", scanobj->stepSize);
3071 scanobj->numberOfEntriesReturned = *scanrs->numberOfEntriesReturned;
3072 logf (LOG_DEBUG, "numberOfEntriesReturned=%d",
3073 scanobj->numberOfEntriesReturned);
3075 if (scanrs->positionOfTerm)
3076 scanobj->positionOfTerm = *scanrs->positionOfTerm;
3078 scanobj->positionOfTerm = -1;
3079 logf (LOG_DEBUG, "positionOfTerm=%d", scanobj->positionOfTerm);
3081 free (scanobj->entries);
3082 scanobj->entries = NULL;
3084 ir_deleteDiags (&scanobj->nonSurrogateDiagnosticList,
3085 &scanobj->nonSurrogateDiagnosticNum);
3086 if (scanrs->entries)
3091 scanobj->entries_flag = 1;
3092 scanobj->which = scanrs->entries->which;
3093 switch (scanobj->which)
3095 case Z_ListEntries_entries:
3096 scanobj->num_entries = scanrs->entries->u.entries->num_entries;
3097 scanobj->entries = ir_tcl_malloc (scanobj->num_entries *
3098 sizeof(*scanobj->entries));
3099 for (i=0; i<scanobj->num_entries; i++)
3101 ze = scanrs->entries->u.entries->entries[i];
3102 scanobj->entries[i].which = ze->which;
3105 case Z_Entry_termInfo:
3106 if (ze->u.termInfo->term->which == Z_Term_general)
3108 int l = ze->u.termInfo->term->u.general->len;
3109 scanobj->entries[i].u.term.buf = ir_tcl_malloc (1+l);
3110 memcpy (scanobj->entries[i].u.term.buf,
3111 ze->u.termInfo->term->u.general->buf,
3113 scanobj->entries[i].u.term.buf[l] = '\0';
3116 scanobj->entries[i].u.term.buf = NULL;
3117 if (ze->u.termInfo->globalOccurrences)
3118 scanobj->entries[i].u.term.globalOccurrences =
3119 *ze->u.termInfo->globalOccurrences;
3121 scanobj->entries[i].u.term.globalOccurrences = 0;
3123 case Z_Entry_surrogateDiagnostic:
3124 ir_handleDiags (&scanobj->entries[i].u.diag.list,
3125 &scanobj->entries[i].u.diag.num,
3126 &ze->u.surrogateDiagnostic,
3132 case Z_ListEntries_nonSurrogateDiagnostics:
3133 ir_handleDiags (&scanobj->nonSurrogateDiagnosticList,
3134 &scanobj->nonSurrogateDiagnosticNum,
3135 scanrs->entries->u.nonSurrogateDiagnostics->
3137 scanrs->entries->u.nonSurrogateDiagnostics->
3143 scanobj->entries_flag = 0;
3147 * ir_select_read: handle incoming packages
3149 void ir_select_read (ClientData clientData)
3151 IrTcl_Obj *p = clientData;
3156 Tcl_CmdInfo cmd_info;
3158 if (p->state == IR_TCL_R_Connecting)
3160 r = cs_rcvconnect (p->cs_link);
3163 logf (LOG_WARN, "cs_rcvconnect returned 1");
3166 p->state = IR_TCL_R_Idle;
3167 ir_select_remove_write (cs_fileno (p->cs_link), p);
3170 logf (LOG_DEBUG, "cs_rcvconnect error");
3173 p->failInfo = IR_TCL_FAIL_CONNECT;
3174 IrTcl_eval (p->interp, p->failback);
3176 do_disconnect (p, NULL, 2, NULL);
3179 p->state = IR_TCL_R_Idle;
3181 IrTcl_eval (p->interp, p->callback);
3182 if (p->cs_link && p->request_queue && p->state == IR_TCL_R_Idle)
3183 ir_tcl_send_q (p, p->request_queue, "x");
3188 /* signal one more use of ir object - callbacks must not
3189 release the ir memory (p pointer) */
3190 p->state = IR_TCL_R_Reading;
3193 /* read incoming APDU */
3194 if ((r=cs_get (p->cs_link, &p->buf_in, &p->len_in)) <= 0)
3196 logf (LOG_DEBUG, "cs_get failed, code %d", r);
3197 ir_select_remove (cs_fileno (p->cs_link), p);
3198 do_disconnect (p, NULL, 2, NULL);
3201 p->failInfo = IR_TCL_FAIL_READ;
3202 IrTcl_eval (p->interp, p->failback);
3204 /* release ir object now if callback deleted it */
3210 /* got complete APDU. Now decode */
3211 odr_setbuf (p->odr_in, p->buf_in, r, 0);
3212 logf (LOG_DEBUG, "cs_get ok, got %d", r);
3213 if (!z_APDU (p->odr_in, &apdu, 0))
3215 logf (LOG_DEBUG, "%s", odr_errmsg (odr_geterror (p->odr_in)));
3216 do_disconnect (p, NULL, 2, NULL);
3219 p->failInfo = IR_TCL_FAIL_IN_APDU;
3220 IrTcl_eval (p->interp, p->failback);
3222 /* release ir object now if failback deleted it */
3226 /* handle APDU and invoke callback */
3227 rq = p->request_queue;
3230 logf (LOG_FATAL, "Internal error. No queue entry");
3233 object_name = rq->object_name;
3234 logf (LOG_DEBUG, "getCommandInfo (%s)", object_name);
3235 if (Tcl_GetCommandInfo (p->interp, object_name, &cmd_info))
3239 case Z_APDU_initResponse:
3240 p->pduType = "init";
3241 ir_initResponse (p, apdu->u.initResponse);
3243 case Z_APDU_searchResponse:
3244 p->pduType = "search";
3245 ir_searchResponse (p, apdu->u.searchResponse,
3246 (IrTcl_SetObj *) cmd_info.clientData);
3248 case Z_APDU_presentResponse:
3249 p->pduType = "present";
3250 ir_presentResponse (p, apdu->u.presentResponse,
3251 (IrTcl_SetObj *) cmd_info.clientData);
3253 case Z_APDU_scanResponse:
3254 p->pduType = "scan";
3255 ir_scanResponse (p, apdu->u.scanResponse,
3256 (IrTcl_ScanObj *) cmd_info.clientData);
3259 logf (LOG_WARN, "Received unknown APDU type (%d)",
3261 do_disconnect (p, NULL, 2, NULL);
3264 p->failInfo = IR_TCL_FAIL_UNKNOWN_APDU;
3265 IrTcl_eval (p->interp, p->failback);
3270 p->request_queue = rq->next;
3271 p->state = IR_TCL_R_Idle;
3274 IrTcl_eval (p->interp, rq->callback);
3276 free (rq->callback);
3277 free (rq->object_name);
3279 odr_reset (p->odr_in);
3280 if (p->ref_count == 1)
3286 } while (p->cs_link && cs_more (p->cs_link));
3287 if (p->cs_link && p->request_queue && p->state == IR_TCL_R_Idle)
3288 ir_tcl_send_q (p, p->request_queue, "x");
3292 * ir_select_write: handle outgoing packages - not yet written.
3294 void ir_select_write (ClientData clientData)
3296 IrTcl_Obj *p = clientData;
3300 logf (LOG_DEBUG, "In write handler");
3301 if (p->state == IR_TCL_R_Connecting)
3303 r = cs_rcvconnect (p->cs_link);
3306 p->state = IR_TCL_R_Idle;
3309 logf (LOG_DEBUG, "cs_rcvconnect error");
3310 ir_select_remove_write (cs_fileno (p->cs_link), p);
3313 p->failInfo = IR_TCL_FAIL_CONNECT;
3314 IrTcl_eval (p->interp, p->failback);
3316 do_disconnect (p, NULL, 2, NULL);
3319 ir_select_remove_write (cs_fileno (p->cs_link), p);
3321 IrTcl_eval (p->interp, p->callback);
3324 rq = p->request_queue;
3325 if (!rq || !rq->buf_out)
3328 if ((r=cs_put (p->cs_link, rq->buf_out, rq->len_out)) < 0)
3330 logf (LOG_DEBUG, "select write fail");
3333 p->failInfo = IR_TCL_FAIL_WRITE;
3334 IrTcl_eval (p->interp, p->failback);
3338 do_disconnect (p, NULL, 2, NULL);
3340 else if (r == 0) /* remove select bit */
3342 p->state = IR_TCL_R_Waiting;
3343 ir_select_remove_write (cs_fileno (p->cs_link), p);
3349 /* ------------------------------------------------------- */
3352 * Irtcl_init: Registration of TCL commands.
3354 int Irtcl_Init (Tcl_Interp *interp)
3356 Tcl_CreateCommand (interp, "ir", ir_obj_mk, (ClientData) NULL,
3357 (Tcl_CmdDeleteProc *) NULL);
3358 Tcl_CreateCommand (interp, "ir-set", ir_set_obj_mk,
3359 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
3360 Tcl_CreateCommand (interp, "ir-scan", ir_scan_obj_mk,
3361 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);