2 * IR toolkit for tcl/tk
4 * See the file LICENSE for details.
5 * Sebastian Hammer, Adam Dickmeiss
8 * Revision 1.71 1996-01-19 16:22:38 adam
9 * New method: apduDump - returns information about last incoming APDU.
11 * Revision 1.70 1996/01/10 09:18:34 adam
12 * PDU specific callbacks implemented: initRespnse, searchResponse,
13 * presentResponse and scanResponse.
14 * Bug fix in the command line shell (tclmain.c) - discovered on OSF/1.
16 * Revision 1.69 1996/01/04 16:12:12 adam
17 * Setting PDUType renamed to eventType.
19 * Revision 1.68 1996/01/04 11:05:22 adam
20 * New setting: PDUType - returns type of last PDU returned from the target.
21 * Fixed a bug in configure/Makefile.
23 * Revision 1.67 1996/01/03 09:00:51 adam
24 * Updated to use new version of Yaz (names changed to avoid C++ conflict).
26 * Revision 1.66 1995/11/28 17:26:39 adam
27 * Removed Carriage return from ir-tcl.c!
28 * Removed misc. debug logs.
30 * Revision 1.65 1995/11/28 13:53:00 quinn
33 * Revision 1.64 1995/11/13 15:39:18 adam
34 * Bug fix: {small,medium}SetElementSetNames weren't set correctly.
35 * Bug fix: idAuthentication weren't set correctly.
37 * Revision 1.63 1995/11/13 09:55:39 adam
38 * Multiple records at a position in a result-set with differnt
41 * Revision 1.62 1995/10/18 17:20:33 adam
42 * Work on target setup in client.tcl.
44 * Revision 1.61 1995/10/18 16:42:42 adam
45 * New settings: smallSetElementSetNames and mediumSetElementSetNames.
47 * Revision 1.60 1995/10/18 15:43:31 adam
48 * In search: mediumSetElementSetNames and smallSetElementSetNames are
49 * set to elementSetNames.
51 * Revision 1.59 1995/10/17 12:18:58 adam
52 * Bug fix: when target connection closed, the connection was not
53 * properly reestablished.
55 * Revision 1.58 1995/10/16 17:00:55 adam
56 * New setting: elementSetNames.
57 * Various client improvements. Medium presentation format looks better.
59 * Revision 1.57 1995/09/21 13:11:51 adam
60 * Support of dynamic loading.
61 * Test script uses load command if necessary.
63 * Revision 1.56 1995/08/29 15:30:14 adam
64 * Work on GRS records.
66 * Revision 1.55 1995/08/28 09:43:25 adam
67 * Minor changes. configure only searches for yaz beta 3 and versions after
70 * Revision 1.54 1995/08/24 12:25:16 adam
71 * Modified to work with yaz 1.0b3.
73 * Revision 1.53 1995/08/04 12:49:26 adam
74 * Bug fix: reading uninitialized variable p.
76 * Revision 1.52 1995/08/04 11:32:38 adam
77 * More work on output queue. Memory related routines moved
80 * Revision 1.51 1995/08/03 13:22:54 adam
83 * Revision 1.50 1995/07/20 08:09:49 adam
84 * client.tcl: Targets removed from hotTargets list when targets
85 * are removed/modified.
86 * ir-tcl.c: More work on triggerResourceControl.
88 * Revision 1.49 1995/06/30 12:39:21 adam
89 * Bug fix: loadFile didn't set record type.
90 * The MARC routines are a little less strict in the interpretation.
91 * Script display.tcl replaces the old marc.tcl.
92 * New interactive script: shell.tcl.
94 * Revision 1.48 1995/06/27 19:03:50 adam
95 * Bug fix in do_present in ir-tcl.c: p->set_child member weren't set.
96 * nextResultSetPosition used instead of setOffset.
98 * Revision 1.47 1995/06/25 10:25:04 adam
99 * Working on triggerResourceControl. Description of compile/install
100 * procedure moved to ir-tcl.sgml.
102 * Revision 1.46 1995/06/22 13:15:06 adam
103 * Feature: SUTRS. Setting getSutrs implemented.
104 * Work on display formats.
105 * Preferred record syntax can be set by the user.
107 * Revision 1.45 1995/06/20 08:07:30 adam
108 * New setting: failInfo.
109 * Working on better cancel mechanism.
111 * Revision 1.44 1995/06/19 17:01:20 adam
114 * Revision 1.43 1995/06/19 13:06:08 adam
115 * New define: IR_TCL_VERSION.
117 * Revision 1.42 1995/06/19 08:08:52 adam
118 * client.tcl: hotTargets now contain both database and target name.
119 * ir-tcl.c: setting protocol edited. Errors in callbacks are logged
120 * by logf(LOG_WARN, ...) calls.
122 * Revision 1.41 1995/06/16 12:28:16 adam
123 * Implemented preferredRecordSyntax.
124 * Minor changes in diagnostic handling.
125 * Record list deleted when connection closes.
127 * Revision 1.40 1995/06/14 13:37:18 adam
128 * Setting recordType implemented.
129 * Setting implementationVersion implemented.
130 * Settings implementationId / implementationName edited.
132 * Revision 1.39 1995/06/08 10:26:32 adam
133 * Bug fix in ir_strdup.
135 * Revision 1.38 1995/06/01 16:36:47 adam
136 * About buttons. Minor bug fixes.
138 * Revision 1.37 1995/06/01 07:31:20 adam
139 * Rename of many typedefs -> IrTcl_...
141 * Revision 1.36 1995/05/31 13:09:59 adam
142 * Client searches/presents may be interrupted.
143 * New moving book-logo.
145 * Revision 1.35 1995/05/31 08:36:33 adam
146 * Bug fix in client.tcl: didn't save options on clientrc.tcl.
147 * New method: referenceId. More work on scan.
149 * Revision 1.34 1995/05/29 10:33:42 adam
150 * README and rename of startup script.
152 * Revision 1.33 1995/05/29 09:15:11 quinn
153 * Changed CS_SR to PROTO_SR, etc.
155 * Revision 1.32 1995/05/29 08:44:16 adam
156 * Work on delete of objects.
158 * Revision 1.31 1995/05/26 11:44:10 adam
159 * Bugs fixed. More work on MARC utilities and queries. Test
160 * client is up-to-date again.
162 * Revision 1.30 1995/05/26 08:54:11 adam
163 * New MARC utilities. Uses prefix query.
165 * Revision 1.29 1995/05/24 14:10:22 adam
166 * Work on idAuthentication, protocolVersion and options.
168 * Revision 1.28 1995/05/23 15:34:48 adam
169 * Many new settings, userInformationField, smallSetUpperBound, etc.
170 * A number of settings are inherited when ir-set is executed.
171 * This version is incompatible with the graphical test client (client.tcl).
173 * Revision 1.27 1995/05/11 15:34:47 adam
174 * Scan request changed a bit. This version works with RLG.
176 * Revision 1.26 1995/04/18 16:11:51 adam
177 * First version of graphical Scan. Some work on query-by-form.
179 * Revision 1.25 1995/04/17 09:37:17 adam
180 * Further development of scan.
182 * Revision 1.24 1995/04/11 14:16:42 adam
183 * Further work on scan. Response works. Entries aren't saved yet.
185 * Revision 1.23 1995/04/10 10:50:27 adam
186 * Result-set name defaults to suffix of ir-set name.
187 * Started working on scan. Not finished at this point.
189 * Revision 1.22 1995/03/31 10:43:03 adam
190 * More robust when getting bad MARC records.
192 * Revision 1.21 1995/03/31 08:56:37 adam
193 * New button "Search".
195 * Revision 1.20 1995/03/29 16:07:09 adam
196 * Bug fix: Didn't use setName in present request.
198 * Revision 1.19 1995/03/28 12:45:23 adam
199 * New ir method failback: called on disconnect/protocol error.
200 * New ir set/get method: protocol: SR / Z3950.
201 * Simple popup and disconnect when failback is invoked.
203 * Revision 1.18 1995/03/21 15:50:12 adam
206 * Revision 1.17 1995/03/21 13:41:03 adam
207 * Comstack cs_create not used too often. Non-blocking connect.
209 * Revision 1.16 1995/03/21 08:26:06 adam
210 * New method, setName, to specify the result set name (other than Default).
211 * New method, responseStatus, which returns diagnostic info, if any, after
212 * present response / search response.
214 * Revision 1.15 1995/03/20 15:24:07 adam
215 * Diagnostic records saved on searchResponse.
217 * Revision 1.14 1995/03/20 08:53:22 adam
218 * Event loop in tclmain.c rewritten. New method searchStatus.
220 * Revision 1.13 1995/03/17 18:26:17 adam
221 * Non-blocking i/o used now. Database names popup as cascade items.
223 * Revision 1.12 1995/03/17 15:45:00 adam
224 * Improved target/database setup.
226 * Revision 1.11 1995/03/16 17:54:03 adam
227 * Minor changes really.
229 * Revision 1.10 1995/03/15 16:14:50 adam
230 * Blocking arg in cs_create changed.
232 * Revision 1.9 1995/03/15 13:59:24 adam
235 * Revision 1.8 1995/03/15 08:25:16 adam
236 * New method presentStatus to check for error on present. Misc. cleanup
237 * of IrTcl_RecordList manipulations. Full MARC record presentation in
240 * Revision 1.7 1995/03/14 17:32:29 adam
241 * Presentation of full Marc record in popup window.
243 * Revision 1.6 1995/03/12 19:31:55 adam
244 * Pattern matching implemented when retrieving MARC records. More
245 * diagnostic functions.
247 * Revision 1.5 1995/03/10 18:00:15 adam
248 * Actual presentation in line-by-line format. RPN query support.
250 * Revision 1.4 1995/03/09 16:15:08 adam
251 * First presentRequest attempts. Hot-target list.
261 #include <sys/time.h>
272 int (*method) (void *obj, Tcl_Interp *interp, int argc, char **argv);
280 static void ir_deleteDiags (IrTcl_Diagnostic **dst_list, int *dst_num);
281 static int do_disconnect (void *obj, Tcl_Interp *interp,
282 int argc, char **argv);
284 static IrTcl_RecordList *new_IR_record (IrTcl_SetObj *setobj,
286 const char *elements)
288 IrTcl_RecordList *rl;
290 for (rl = setobj->record_list; rl; rl = rl->next)
292 if (no == rl->no && (!rl->elements || !elements ||
293 !strcmp(elements, rl->elements)))
298 case Z_NamePlusRecord_databaseRecord:
299 free (rl->u.dbrec.buf);
300 rl->u.dbrec.buf = NULL;
302 case Z_NamePlusRecord_surrogateDiagnostic:
303 ir_deleteDiags (&rl->u.surrogateDiagnostics.list,
304 &rl->u.surrogateDiagnostics.num);
312 rl = ir_tcl_malloc (sizeof(*rl));
313 rl->next = setobj->record_list;
315 setobj->record_list = rl;
318 ir_tcl_strdup (NULL, &rl->elements, elements);
323 enum oid_value value;
325 } IrTcl_recordSyntaxTab[] = {
326 { VAL_UNIMARC, "UNIMARC" },
327 { VAL_INTERMARC, "INTERMARC" },
329 { VAL_USMARC, "USMARC" },
330 { VAL_UKMARC, "UKMARC" },
331 { VAL_NORMARC, "NORMARC" },
332 { VAL_LIBRISMARC, "LIBRISMARC" },
333 { VAL_DANMARC, "DANMARC" },
334 { VAL_FINMARC, "FINMARC" },
336 { VAL_CANMARC, "CANMARC" },
338 { VAL_PICAMARC, "PICAMARC" },
339 { VAL_AUSMARC, "AUSMARC" },
340 { VAL_IBERMARC, "IBERMARC" },
341 { VAL_SUTRS, "SUTRS" },
342 { VAL_GRS1, "GRS1" },
349 int IrTcl_eval (Tcl_Interp *interp, const char *command)
351 char *tmp = ir_tcl_malloc (strlen(command)+1);
354 strcpy (tmp, command);
355 r = Tcl_Eval (interp, tmp);
357 logf (LOG_WARN, "Tcl error in line %d: %s", interp->errorLine,
359 Tcl_FreeResult (interp);
365 * IrTcl_getRecordSyntaxStr: Return record syntax name of object id
367 static const char *IrTcl_getRecordSyntaxStr (enum oid_value value)
370 for (i = 0; IrTcl_recordSyntaxTab[i].name; i++)
371 if (IrTcl_recordSyntaxTab[i].value == value)
372 return IrTcl_recordSyntaxTab[i].name;
377 * IrTcl_getRecordSyntaxVal: Return record syntax value of string
379 static enum oid_value IrTcl_getRecordSyntaxVal (const char *name)
382 for (i = 0; IrTcl_recordSyntaxTab[i].name; i++)
383 if (!strcmp (IrTcl_recordSyntaxTab[i].name, name))
384 return IrTcl_recordSyntaxTab[i].value;
388 static IrTcl_RecordList *find_IR_record (IrTcl_SetObj *setobj, int no)
390 IrTcl_RecordList *rl;
392 for (rl = setobj->record_list; rl; rl = rl->next)
394 (!setobj->recordElements || !rl->elements ||
395 !strcmp (setobj->recordElements, rl->elements)))
400 static void delete_IR_records (IrTcl_SetObj *setobj)
402 IrTcl_RecordList *rl, *rl1;
404 for (rl = setobj->record_list; rl; rl = rl1)
408 case Z_NamePlusRecord_databaseRecord:
409 free (rl->u.dbrec.buf);
411 case Z_NamePlusRecord_surrogateDiagnostic:
412 ir_deleteDiags (&rl->u.surrogateDiagnostics.list,
413 &rl->u.surrogateDiagnostics.num);
419 setobj->record_list = NULL;
423 * get_set_int: Set/get integer value
425 static int get_set_int (int *val, Tcl_Interp *interp, int argc, char **argv)
431 if (Tcl_GetInt (interp, argv[2], val)==TCL_ERROR)
434 sprintf (buf, "%d", *val);
435 Tcl_AppendResult (interp, buf, NULL);
440 * ir_method: Search for method in table and invoke method handler
442 int ir_method (Tcl_Interp *interp, int argc, char **argv, IrTcl_Methods *tab)
444 IrTcl_Methods *tab_i = tab;
447 for (tab_i = tab; tab_i->tab; tab_i++)
448 for (t = tab_i->tab; t->name; t++)
451 if ((*t->method)(tab_i->obj, interp, argc, argv) == TCL_ERROR)
455 if (!strcmp (t->name, argv[1]))
456 return (*t->method)(tab_i->obj, interp, argc, argv);
460 Tcl_AppendResult (interp, "Bad method: ", argv[1],
461 ". Possible methods:", NULL);
462 for (tab_i = tab; tab_i->tab; tab_i++)
463 for (t = tab_i->tab; t->name; t++)
464 Tcl_AppendResult (interp, " ", t->name, NULL);
469 * ir_method_r: Get status for all readable elements
471 int ir_method_r (void *obj, Tcl_Interp *interp, int argc, char **argv,
479 for (; tab->name; tab++)
482 argv_n[1] = tab->name;
483 Tcl_AppendResult (interp, "{", NULL);
484 (*tab->method)(obj, interp, argc_n, argv_n);
485 Tcl_AppendResult (interp, "} ", NULL);
491 * ir_named_bits: get/set named bits
493 int ir_named_bits (struct ir_named_entry *tab, Odr_bitmask *ob,
494 Tcl_Interp *interp, int argc, char **argv)
496 struct ir_named_entry *ti;
501 for (no = 0; no < argc; no++)
503 for (ti = tab; ti->name; ti++)
504 if (!strcmp (argv[no], ti->name))
506 ODR_MASK_SET (ob, ti->pos);
511 Tcl_AppendResult (interp, "Bad bit mask: ", argv[no], NULL);
517 for (ti = tab; ti->name; ti++)
518 if (ODR_MASK_GET (ob, ti->pos))
519 Tcl_AppendElement (interp, ti->name);
523 static void set_referenceId (ODR o, Z_ReferenceId **dst, const char *src)
529 *dst = odr_malloc (o, sizeof(**dst));
530 (*dst)->size = (*dst)->len = strlen(src);
531 (*dst)->buf = odr_malloc (o, (*dst)->len);
532 memcpy ((*dst)->buf, src, (*dst)->len);
536 static void get_referenceId (char **dst, Z_ReferenceId *src)
544 *dst = ir_tcl_malloc (src->len+1);
545 memcpy (*dst, src->buf, src->len);
546 (*dst)[src->len] = '\0';
549 /* ------------------------------------------------------- */
552 * do_init_request: init method on IR object
554 static int do_init_request (void *obj, Tcl_Interp *interp,
555 int argc, char **argv)
565 interp->result = "init: not connected";
568 apdu = zget_APDU (p->odr_out, Z_APDU_initRequest);
569 req = apdu->u.initRequest;
571 set_referenceId (p->odr_out, &req->referenceId, p->set_inher.referenceId);
572 req->options = &p->options;
573 req->protocolVersion = &p->protocolVersion;
574 req->preferredMessageSize = &p->preferredMessageSize;
575 req->maximumRecordSize = &p->maximumRecordSize;
577 if (p->idAuthenticationGroupId)
579 Z_IdPass *pass = odr_malloc (p->odr_out, sizeof(*pass));
580 Z_IdAuthentication *auth = odr_malloc (p->odr_out, sizeof(*auth));
582 logf (LOG_DEBUG, "using pass authentication");
584 auth->which = Z_IdAuthentication_idPass;
585 auth->u.idPass = pass;
586 if (p->idAuthenticationGroupId && *p->idAuthenticationGroupId)
587 pass->groupId = p->idAuthenticationGroupId;
589 pass->groupId = NULL;
590 if (p->idAuthenticationUserId && *p->idAuthenticationUserId)
591 pass->userId = p->idAuthenticationUserId;
594 if (p->idAuthenticationPassword && *p->idAuthenticationPassword)
595 pass->password = p->idAuthenticationPassword;
597 pass->password = NULL;
598 req->idAuthentication = auth;
600 else if (!p->idAuthenticationOpen || !*p->idAuthenticationOpen)
601 req->idAuthentication = NULL;
604 Z_IdAuthentication *auth = odr_malloc (p->odr_out, sizeof(*auth));
606 logf (LOG_DEBUG, "using open authentication");
607 auth->which = Z_IdAuthentication_open;
608 auth->u.open = p->idAuthenticationOpen;
609 req->idAuthentication = auth;
611 req->implementationId = p->implementationId;
612 req->implementationName = p->implementationName;
613 req->implementationVersion = p->implementationVersion;
614 req->userInformationField = 0;
616 return ir_tcl_send_APDU (interp, p, apdu, "init", *argv);
620 * do_protocolVersion: Set protocol Version
622 static int do_protocolVersion (void *obj, Tcl_Interp *interp,
623 int argc, char **argv)
631 ODR_MASK_ZERO (&p->protocolVersion);
632 ODR_MASK_SET (&p->protocolVersion, 0);
633 ODR_MASK_SET (&p->protocolVersion, 1);
638 if (Tcl_GetInt (interp, argv[2], &version)==TCL_ERROR)
640 ODR_MASK_ZERO (&p->protocolVersion);
641 for (i = 0; i<version; i++)
642 ODR_MASK_SET (&p->protocolVersion, i);
644 for (i = 4; --i >= 0; )
645 if (ODR_MASK_GET (&p->protocolVersion, i))
647 sprintf (buf, "%d", i+1);
648 interp->result = buf;
653 * do_options: Set options
655 static int do_options (void *obj, Tcl_Interp *interp,
656 int argc, char **argv)
658 static struct ir_named_entry options_tab[] = {
662 { "resourceReport", 3 },
663 { "triggerResourceCtrl", 4},
664 { "resourceCtrl", 5},
668 { "extendedServices", 10},
669 { "level-1Segmentation", 11},
670 { "level-2Segmentation", 12},
671 { "concurrentOperations", 13},
672 { "namedResultSets", 14},
679 ODR_MASK_ZERO (&p->options);
680 ODR_MASK_SET (&p->options, 0);
681 ODR_MASK_SET (&p->options, 1);
682 ODR_MASK_SET (&p->options, 4);
683 ODR_MASK_SET (&p->options, 7);
684 ODR_MASK_SET (&p->options, 14);
687 return ir_named_bits (options_tab, &p->options, interp, argc-2, argv+2);
691 * do_apduInfo: Get APDU information
693 static int do_apduInfo (void *obj, Tcl_Interp *interp, int argc, char **argv)
701 sprintf (buf, "%d", p->apduLen);
702 Tcl_AppendElement (interp, buf);
703 sprintf (buf, "%d", p->apduOffset);
704 Tcl_AppendElement (interp, buf);
707 Tcl_AppendElement (interp, "");
710 apduf = fopen ("apdu.tmp", "w");
713 Tcl_AppendElement (interp, "");
716 odr_dumpBER (apduf, p->buf_in, p->apduLen);
718 if (!(apduf = fopen ("apdu.tmp", "r")))
719 Tcl_AppendElement (interp, "");
724 Tcl_AppendResult (interp, " {", NULL);
725 while ((c = getc (apduf)) != EOF)
729 Tcl_AppendResult (interp, buf, NULL);
732 Tcl_AppendResult (interp, "}", NULL);
739 * do_failInfo: Get fail information
741 static int do_failInfo (void *obj, Tcl_Interp *interp, int argc, char **argv)
751 sprintf (buf, "%d", p->failInfo);
757 case IR_TCL_FAIL_CONNECT:
758 cp = "connect failed";
760 case IR_TCL_FAIL_READ:
761 cp = "connection closed";
763 case IR_TCL_FAIL_WRITE:
764 cp = "connection closed";
766 case IR_TCL_FAIL_IN_APDU:
767 cp = "failed to decode incoming APDU";
769 case IR_TCL_FAIL_UNKNOWN_APDU:
775 Tcl_AppendElement (interp, buf);
776 Tcl_AppendElement (interp, cp);
781 * do_preferredMessageSize: Set/get preferred message size
783 static int do_preferredMessageSize (void *obj, Tcl_Interp *interp,
784 int argc, char **argv)
790 p->preferredMessageSize = 30000;
793 return get_set_int (&p->preferredMessageSize, interp, argc, argv);
797 * do_maximumRecordSize: Set/get maximum record size
799 static int do_maximumRecordSize (void *obj, Tcl_Interp *interp,
800 int argc, char **argv)
806 p->maximumRecordSize = 30000;
809 return get_set_int (&p->maximumRecordSize, interp, argc, argv);
813 * do_initResult: Get init result
815 static int do_initResult (void *obj, Tcl_Interp *interp,
816 int argc, char **argv)
822 return get_set_int (&p->initResult, interp, argc, argv);
827 * do_implementationName: Set/get Implementation Name.
829 static int do_implementationName (void *obj, Tcl_Interp *interp,
830 int argc, char **argv)
835 return ir_tcl_strdup (interp, &p->implementationName,
836 "Index Data/IrTcl on YAZ");
838 return ir_tcl_strdel (interp, &p->implementationName);
841 free (p->implementationName);
842 if (ir_tcl_strdup (interp, &p->implementationName, argv[2])
846 Tcl_AppendResult (interp, p->implementationName, (char*) NULL);
851 * do_implementationId: Get Implementation Id.
853 static int do_implementationId (void *obj, Tcl_Interp *interp,
854 int argc, char **argv)
859 return ir_tcl_strdup (interp, &p->implementationId, "YAZ (id=81)");
861 return ir_tcl_strdel (interp, &p->implementationId);
862 Tcl_AppendResult (interp, p->implementationId, (char*) NULL);
867 * do_implementationVersion: get Implementation Version.
869 static int do_implementationVersion (void *obj, Tcl_Interp *interp,
870 int argc, char **argv)
875 return ir_tcl_strdup (interp, &p->implementationVersion,
876 "YAZ: " YAZ_VERSION " / IrTcl: " IR_TCL_VERSION);
878 return ir_tcl_strdel (interp, &p->implementationVersion);
879 Tcl_AppendResult (interp, p->implementationVersion, (char*) NULL);
884 * do_targetImplementationName: Get Implementation Name of target.
886 static int do_targetImplementationName (void *obj, Tcl_Interp *interp,
887 int argc, char **argv)
893 p->targetImplementationName = NULL;
897 return ir_tcl_strdel (interp, &p->targetImplementationName);
898 Tcl_AppendResult (interp, p->targetImplementationName, (char*) NULL);
903 * do_targetImplementationId: Get Implementation Id of target
905 static int do_targetImplementationId (void *obj, Tcl_Interp *interp,
906 int argc, char **argv)
912 p->targetImplementationId = NULL;
916 return ir_tcl_strdel (interp, &p->targetImplementationId);
917 Tcl_AppendResult (interp, p->targetImplementationId, (char*) NULL);
922 * do_targetImplementationVersion: Get Implementation Version of target
924 static int do_targetImplementationVersion (void *obj, Tcl_Interp *interp,
925 int argc, char **argv)
931 p->targetImplementationVersion = NULL;
935 return ir_tcl_strdel (interp, &p->targetImplementationVersion);
936 Tcl_AppendResult (interp, p->targetImplementationVersion, (char*) NULL);
941 * do_idAuthentication: Set/get id Authentication
943 static int do_idAuthentication (void *obj, Tcl_Interp *interp,
944 int argc, char **argv)
948 if (argc >= 3 || argc == -1)
950 free (p->idAuthenticationOpen);
951 free (p->idAuthenticationGroupId);
952 free (p->idAuthenticationUserId);
953 free (p->idAuthenticationPassword);
955 if (argc >= 3 || argc <= 0)
957 p->idAuthenticationOpen = NULL;
958 p->idAuthenticationGroupId = NULL;
959 p->idAuthenticationUserId = NULL;
960 p->idAuthenticationPassword = NULL;
969 ir_tcl_strdup (interp, &p->idAuthenticationOpen, argv[2])
976 ir_tcl_strdup (interp, &p->idAuthenticationGroupId, argv[2])
980 ir_tcl_strdup (interp, &p->idAuthenticationUserId, argv[3])
984 ir_tcl_strdup (interp, &p->idAuthenticationPassword, argv[4])
989 if (p->idAuthenticationOpen)
990 Tcl_AppendElement (interp, p->idAuthenticationOpen);
991 else if (p->idAuthenticationGroupId)
993 Tcl_AppendElement (interp, p->idAuthenticationGroupId);
994 Tcl_AppendElement (interp, p->idAuthenticationUserId);
995 Tcl_AppendElement (interp, p->idAuthenticationPassword);
1001 * do_connect: connect method on IR object
1003 static int do_connect (void *obj, Tcl_Interp *interp,
1004 int argc, char **argv)
1016 interp->result = "already connected";
1019 if (ir_tcl_strdup (interp, &p->hostname, argv[2]) == TCL_ERROR)
1021 if (!strcmp (p->cs_type, "tcpip"))
1023 p->cs_link = cs_create (tcpip_type, CS_BLOCK, p->protocol_type);
1024 addr = tcpip_strtoaddr (argv[2]);
1027 interp->result = "tcpip_strtoaddr fail";
1030 logf (LOG_DEBUG, "tcp/ip connect %s", argv[2]);
1032 else if (!strcmp (p->cs_type, "mosi"))
1035 p->cs_link = cs_create (mosi_type, CS_BLOCK, p->protocol_type);
1036 addr = mosi_strtoaddr (argv[2]);
1039 interp->result = "mosi_strtoaddr fail";
1042 logf (LOG_DEBUG, "mosi connect %s", argv[2]);
1044 interp->result = "MOSI support not there";
1050 Tcl_AppendResult (interp, "Bad comstack type: ",
1054 if ((r=cs_connect (p->cs_link, addr)) < 0)
1056 interp->result = "connect fail";
1057 do_disconnect (p, NULL, 2, NULL);
1060 p->eventType = "connect";
1061 ir_select_add (cs_fileno (p->cs_link), p);
1064 ir_select_add_write (cs_fileno (p->cs_link), p);
1065 p->state = IR_TCL_R_Connecting;
1069 p->state = IR_TCL_R_Idle;
1071 IrTcl_eval (p->interp, p->callback);
1075 Tcl_AppendResult (interp, p->hostname, NULL);
1080 * do_disconnect: disconnect method on IR object
1082 static int do_disconnect (void *obj, Tcl_Interp *interp,
1083 int argc, char **argv)
1089 p->state = IR_TCL_R_Idle;
1090 p->eventType = NULL;
1099 ir_select_remove_write (cs_fileno (p->cs_link), p);
1100 ir_select_remove (cs_fileno (p->cs_link), p);
1102 odr_reset (p->odr_in);
1104 assert (p->cs_link);
1105 cs_close (p->cs_link);
1108 ODR_MASK_ZERO (&p->options);
1109 ODR_MASK_SET (&p->options, 0);
1110 ODR_MASK_SET (&p->options, 1);
1111 ODR_MASK_SET (&p->options, 4);
1112 ODR_MASK_SET (&p->options, 7);
1113 ODR_MASK_SET (&p->options, 14);
1115 ODR_MASK_ZERO (&p->protocolVersion);
1116 ODR_MASK_SET (&p->protocolVersion, 0);
1117 ODR_MASK_SET (&p->protocolVersion, 1);
1120 assert (!p->cs_link);
1125 * do_comstack: Set/get comstack method on IR object
1127 static int do_comstack (void *o, Tcl_Interp *interp,
1128 int argc, char **argv)
1133 return ir_tcl_strdup (interp, &obj->cs_type, "tcpip");
1134 else if (argc == -1)
1135 return ir_tcl_strdel (interp, &obj->cs_type);
1138 free (obj->cs_type);
1139 if (ir_tcl_strdup (interp, &obj->cs_type, argv[2]) == TCL_ERROR)
1142 Tcl_AppendElement (interp, obj->cs_type);
1147 * do_logLevel: Set log level
1149 static int do_logLevel (void *o, Tcl_Interp *interp,
1150 int argc, char **argv)
1155 log_init (log_mask_str (argv[2]), "", NULL);
1157 log_init (log_mask_str (argv[2]), argv[3], NULL);
1159 log_init (log_mask_str (argv[2]), argv[3], argv[4]);
1165 * do_eventType: Return type of last event
1167 static int do_eventType (void *obj, Tcl_Interp *interp,
1168 int argc, char **argv)
1174 p->eventType = NULL;
1177 Tcl_AppendElement (interp, p->eventType ? p->eventType : "");
1183 * do_callback: add callback
1185 static int do_callback (void *obj, Tcl_Interp *interp,
1186 int argc, char **argv)
1195 else if (argc == -1)
1196 return ir_tcl_strdel (interp, &p->callback);
1202 if (ir_tcl_strdup (interp, &p->callback, argv[2]) == TCL_ERROR)
1212 * do_failback: add error handle callback
1214 static int do_failback (void *obj, Tcl_Interp *interp,
1215 int argc, char **argv)
1224 else if (argc == -1)
1225 return ir_tcl_strdel (interp, &p->failback);
1231 if (ir_tcl_strdup (interp, &p->failback, argv[2]) == TCL_ERROR)
1241 * do_initResponse: add init response handler
1243 static int do_initResponse (void *obj, Tcl_Interp *interp,
1244 int argc, char **argv)
1250 p->initResponse = NULL;
1253 else if (argc == -1)
1254 return ir_tcl_strdel (interp, &p->initResponse);
1257 free (p->initResponse);
1260 if (ir_tcl_strdup (interp, &p->initResponse, argv[2]) == TCL_ERROR)
1264 p->initResponse = NULL;
1269 * do_protocol: Set/get protocol method on IR object
1271 static int do_protocol (void *o, Tcl_Interp *interp, int argc, char **argv)
1277 p->protocol_type = PROTO_Z3950;
1282 if (!strcmp (argv[2], "Z39"))
1283 p->protocol_type = PROTO_Z3950;
1284 else if (!strcmp (argv[2], "SR"))
1285 p->protocol_type = PROTO_SR;
1288 Tcl_AppendResult (interp, "Bad protocol: ", argv[2], NULL);
1293 switch (p->protocol_type)
1296 Tcl_AppendElement (interp, "Z39");
1299 Tcl_AppendElement (interp, "SR");
1306 * do_triggerResourceControl:
1308 static int do_triggerResourceControl (void *obj, Tcl_Interp *interp,
1309 int argc, char **argv)
1313 Z_TriggerResourceControlRequest *req;
1314 bool_t is_false = 0;
1320 interp->result = "triggerResourceControl: not connected";
1323 apdu = zget_APDU (p->odr_out, Z_APDU_triggerResourceControlRequest);
1324 req = apdu->u.triggerResourceControlRequest;
1325 *req->requestedAction = Z_TriggerResourceCtrl_cancel;
1326 req->resultSetWanted = &is_false;
1328 return ir_tcl_send_APDU (interp, p, apdu, "triggerResourceControl",
1333 * do_databaseNames: specify database names
1335 static int do_databaseNames (void *obj, Tcl_Interp *interp,
1336 int argc, char **argv)
1339 IrTcl_SetCObj *p = obj;
1343 for (i=0; i<p->num_databaseNames; i++)
1344 free (p->databaseNames[i]);
1345 free (p->databaseNames);
1349 p->num_databaseNames = 0;
1350 p->databaseNames = NULL;
1355 for (i=0; i<p->num_databaseNames; i++)
1356 Tcl_AppendElement (interp, p->databaseNames[i]);
1359 if (p->databaseNames)
1361 for (i=0; i<p->num_databaseNames; i++)
1362 free (p->databaseNames[i]);
1363 free (p->databaseNames);
1365 p->num_databaseNames = argc - 2;
1367 ir_tcl_malloc (sizeof(*p->databaseNames) * p->num_databaseNames);
1368 for (i=0; i<p->num_databaseNames; i++)
1370 if (ir_tcl_strdup (interp, &p->databaseNames[i], argv[2+i])
1378 * do_replaceIndicator: Set/get replace Set indicator
1380 static int do_replaceIndicator (void *obj, Tcl_Interp *interp,
1381 int argc, char **argv)
1383 IrTcl_SetCObj *p = obj;
1387 p->replaceIndicator = 1;
1390 return get_set_int (&p->replaceIndicator, interp, argc, argv);
1394 * do_queryType: Set/Get query method
1396 static int do_queryType (void *obj, Tcl_Interp *interp,
1397 int argc, char **argv)
1399 IrTcl_SetCObj *p = obj;
1402 return ir_tcl_strdup (interp, &p->queryType, "rpn");
1403 else if (argc == -1)
1404 return ir_tcl_strdel (interp, &p->queryType);
1407 free (p->queryType);
1408 if (ir_tcl_strdup (interp, &p->queryType, argv[2]) == TCL_ERROR)
1411 Tcl_AppendResult (interp, p->queryType, NULL);
1416 * do_userInformationField: Get User information field
1418 static int do_userInformationField (void *obj, Tcl_Interp *interp,
1419 int argc, char **argv)
1425 p->userInformationField = NULL;
1428 else if (argc == -1)
1429 return ir_tcl_strdel (interp, &p->userInformationField);
1430 Tcl_AppendResult (interp, p->userInformationField, NULL);
1435 * do_smallSetUpperBound: Set/get small set upper bound
1437 static int do_smallSetUpperBound (void *o, Tcl_Interp *interp,
1438 int argc, char **argv)
1440 IrTcl_SetCObj *p = o;
1444 p->smallSetUpperBound = 0;
1447 return get_set_int (&p->smallSetUpperBound, interp, argc, argv);
1451 * do_largeSetLowerBound: Set/get large set lower bound
1453 static int do_largeSetLowerBound (void *o, Tcl_Interp *interp,
1454 int argc, char **argv)
1456 IrTcl_SetCObj *p = o;
1460 p->largeSetLowerBound = 2;
1463 return get_set_int (&p->largeSetLowerBound, interp, argc, argv);
1467 * do_mediumSetPresentNumber: Set/get large set lower bound
1469 static int do_mediumSetPresentNumber (void *o, Tcl_Interp *interp,
1470 int argc, char **argv)
1472 IrTcl_SetCObj *p = o;
1476 p->mediumSetPresentNumber = 0;
1479 return get_set_int (&p->mediumSetPresentNumber, interp, argc, argv);
1483 * do_referenceId: Set/Get referenceId
1485 static int do_referenceId (void *obj, Tcl_Interp *interp,
1486 int argc, char **argv)
1488 IrTcl_SetCObj *p = obj;
1492 p->referenceId = NULL;
1495 else if (argc == -1)
1496 return ir_tcl_strdel (interp, &p->referenceId);
1499 free (p->referenceId);
1500 if (ir_tcl_strdup (interp, &p->referenceId, argv[2]) == TCL_ERROR)
1503 Tcl_AppendResult (interp, p->referenceId, NULL);
1508 * do_preferredRecordSyntax: Set/get preferred record syntax
1510 static int do_preferredRecordSyntax (void *obj, Tcl_Interp *interp,
1511 int argc, char **argv)
1513 IrTcl_SetCObj *p = obj;
1517 p->preferredRecordSyntax = NULL;
1520 else if (argc == -1)
1522 free (p->preferredRecordSyntax);
1523 p->preferredRecordSyntax = NULL;
1528 free (p->preferredRecordSyntax);
1529 p->preferredRecordSyntax = NULL;
1530 if (argv[2][0] && (p->preferredRecordSyntax =
1531 ir_tcl_malloc (sizeof(*p->preferredRecordSyntax))))
1532 *p->preferredRecordSyntax = IrTcl_getRecordSyntaxVal (argv[2]);
1539 * do_elementSetNames: Set/Get element Set Names
1541 static int do_elementSetNames (void *obj, Tcl_Interp *interp,
1542 int argc, char **argv)
1544 IrTcl_SetCObj *p = obj;
1548 p->elementSetNames = NULL;
1551 else if (argc == -1)
1552 return ir_tcl_strdel (interp, &p->elementSetNames);
1555 free (p->elementSetNames);
1556 if (ir_tcl_strdup (interp, &p->elementSetNames, argv[2]) == TCL_ERROR)
1559 Tcl_AppendResult (interp, p->elementSetNames, NULL);
1564 * do_smallSetElementSetNames: Set/Get small Set Element Set Names
1566 static int do_smallSetElementSetNames (void *obj, Tcl_Interp *interp,
1567 int argc, char **argv)
1569 IrTcl_SetCObj *p = obj;
1573 p->smallSetElementSetNames = NULL;
1576 else if (argc == -1)
1577 return ir_tcl_strdel (interp, &p->smallSetElementSetNames);
1580 free (p->smallSetElementSetNames);
1581 if (ir_tcl_strdup (interp, &p->smallSetElementSetNames,
1582 argv[2]) == TCL_ERROR)
1585 Tcl_AppendResult (interp, p->smallSetElementSetNames, NULL);
1590 * do_mediumSetElementSetNames: Set/Get medium Set Element Set Names
1592 static int do_mediumSetElementSetNames (void *obj, Tcl_Interp *interp,
1593 int argc, char **argv)
1595 IrTcl_SetCObj *p = obj;
1599 p->mediumSetElementSetNames = NULL;
1602 else if (argc == -1)
1603 return ir_tcl_strdel (interp, &p->mediumSetElementSetNames);
1606 free (p->mediumSetElementSetNames);
1607 if (ir_tcl_strdup (interp, &p->mediumSetElementSetNames,
1608 argv[2]) == TCL_ERROR)
1611 Tcl_AppendResult (interp, p->mediumSetElementSetNames, NULL);
1615 static IrTcl_Method ir_method_tab[] = {
1616 { 1, "comstack", do_comstack },
1617 { 1, "protocol", do_protocol },
1618 { 0, "failback", do_failback },
1619 { 0, "failInfo", do_failInfo },
1620 { 0, "apduInfo", do_apduInfo },
1621 { 0, "logLevel", do_logLevel },
1623 { 0, "eventType", do_eventType },
1624 { 1, "connect", do_connect },
1625 { 0, "protocolVersion", do_protocolVersion },
1626 { 1, "preferredMessageSize", do_preferredMessageSize },
1627 { 1, "maximumRecordSize", do_maximumRecordSize },
1628 { 1, "implementationName", do_implementationName },
1629 { 1, "implementationId", do_implementationId },
1630 { 1, "implementationVersion", do_implementationVersion },
1631 { 0, "targetImplementationName", do_targetImplementationName },
1632 { 0, "targetImplementationId", do_targetImplementationId },
1633 { 0, "targetImplementationVersion", do_targetImplementationVersion },
1634 { 0, "userInformationField", do_userInformationField },
1635 { 1, "idAuthentication", do_idAuthentication },
1636 { 0, "options", do_options },
1637 { 0, "init", do_init_request },
1638 { 0, "initResult", do_initResult },
1639 { 0, "disconnect", do_disconnect },
1640 { 0, "callback", do_callback },
1641 { 0, "initResponse", do_initResponse },
1642 { 0, "triggerResourceControl", do_triggerResourceControl },
1643 { 0, "initResponse", do_initResponse },
1647 static IrTcl_Method ir_set_c_method_tab[] = {
1648 { 0, "databaseNames", do_databaseNames},
1649 { 0, "replaceIndicator", do_replaceIndicator},
1650 { 0, "queryType", do_queryType },
1651 { 0, "preferredRecordSyntax", do_preferredRecordSyntax },
1652 { 0, "smallSetUpperBound", do_smallSetUpperBound},
1653 { 0, "largeSetLowerBound", do_largeSetLowerBound},
1654 { 0, "mediumSetPresentNumber", do_mediumSetPresentNumber},
1655 { 0, "referenceId", do_referenceId },
1656 { 0, "elementSetNames", do_elementSetNames },
1657 { 0, "smallSetElementSetNames", do_smallSetElementSetNames },
1658 { 0, "mediumSetElementSetNames", do_mediumSetElementSetNames },
1663 * ir_obj_method: IR Object methods
1665 static int ir_obj_method (ClientData clientData, Tcl_Interp *interp,
1666 int argc, char **argv)
1668 IrTcl_Methods tab[3];
1669 IrTcl_Obj *p = clientData;
1672 return ir_method_r (clientData, interp, argc, argv, ir_method_tab);
1674 tab[0].tab = ir_method_tab;
1676 tab[1].tab = ir_set_c_method_tab;
1677 tab[1].obj = &p->set_inher;
1680 return ir_method (interp, argc, argv, tab);
1684 * ir_obj_delete: IR Object disposal
1686 static void ir_obj_delete (ClientData clientData)
1688 IrTcl_Obj *obj = clientData;
1689 IrTcl_Methods tab[3];
1692 if (obj->ref_count > 0)
1694 assert (obj->ref_count == 0);
1696 logf (LOG_DEBUG, "ir object delete");
1697 tab[0].tab = ir_method_tab;
1699 tab[1].tab = ir_set_c_method_tab;
1700 tab[1].obj = &obj->set_inher;
1703 ir_method (NULL, -1, NULL, tab);
1706 odr_destroy (obj->odr_in);
1707 odr_destroy (obj->odr_out);
1708 odr_destroy (obj->odr_pr);
1713 * ir_obj_mk: IR Object creation
1715 static int ir_obj_mk (ClientData clientData, Tcl_Interp *interp,
1716 int argc, char **argv)
1718 IrTcl_Methods tab[3];
1726 interp->result = "wrong # args";
1729 obj = ir_tcl_malloc (sizeof(*obj));
1732 obj->bibset = ccl_qual_mk ();
1733 if ((inf = fopen ("default.bib", "r")))
1735 ccl_qual_file (obj->bibset, inf);
1740 logf (LOG_DEBUG, "ir object create");
1741 obj->odr_in = odr_createmem (ODR_DECODE);
1742 obj->odr_out = odr_createmem (ODR_ENCODE);
1743 obj->odr_pr = odr_createmem (ODR_PRINT);
1744 obj->state = IR_TCL_R_Idle;
1745 obj->interp = interp;
1749 obj->request_queue = NULL;
1751 tab[0].tab = ir_method_tab;
1753 tab[1].tab = ir_set_c_method_tab;
1754 tab[1].obj = &obj->set_inher;
1757 if (ir_method (interp, 0, NULL, tab) == TCL_ERROR)
1759 Tcl_CreateCommand (interp, argv[1], ir_obj_method,
1760 (ClientData) obj, ir_obj_delete);
1764 /* ------------------------------------------------------- */
1766 * do_search: Do search request
1768 static int do_search (void *o, Tcl_Interp *interp, int argc, char **argv)
1770 Z_SearchRequest *req;
1774 IrTcl_SetObj *obj = o;
1785 interp->result = "wrong # args";
1788 if (!obj->set_inher.num_databaseNames)
1790 interp->result = "no databaseNames";
1795 interp->result = "search: not connected";
1798 apdu = zget_APDU (p->odr_out, Z_APDU_searchRequest);
1799 req = apdu->u.searchRequest;
1803 bib1.proto = p->protocol_type;
1804 bib1.oclass = CLASS_ATTSET;
1805 bib1.value = VAL_BIB1;
1807 set_referenceId (p->odr_out, &req->referenceId,
1808 obj->set_inher.referenceId);
1810 req->smallSetUpperBound = &obj->set_inher.smallSetUpperBound;
1811 req->largeSetLowerBound = &obj->set_inher.largeSetLowerBound;
1812 req->mediumSetPresentNumber = &obj->set_inher.mediumSetPresentNumber;
1813 req->replaceIndicator = &obj->set_inher.replaceIndicator;
1814 req->resultSetName = obj->setName ? obj->setName : "Default";
1815 logf (LOG_DEBUG, "Search, resultSetName %s", req->resultSetName);
1816 req->num_databaseNames = obj->set_inher.num_databaseNames;
1817 req->databaseNames = obj->set_inher.databaseNames;
1818 for (r=0; r < obj->set_inher.num_databaseNames; r++)
1819 logf (LOG_DEBUG, " Database %s", obj->set_inher.databaseNames[r]);
1820 if (obj->set_inher.preferredRecordSyntax)
1822 struct oident ident;
1824 ident.proto = p->protocol_type;
1825 ident.oclass = CLASS_RECSYN;
1826 ident.value = *obj->set_inher.preferredRecordSyntax;
1827 logf (LOG_DEBUG, "Preferred record syntax is %d", ident.value);
1828 req->preferredRecordSyntax = odr_oiddup (p->odr_out,
1829 oid_getoidbyent (&ident));
1832 req->preferredRecordSyntax = 0;
1834 if (obj->set_inher.smallSetElementSetNames &&
1835 *obj->set_inher.smallSetElementSetNames)
1837 Z_ElementSetNames *esn = odr_malloc (p->odr_out, sizeof(*esn));
1839 esn->which = Z_ElementSetNames_generic;
1840 esn->u.generic = obj->set_inher.smallSetElementSetNames;
1841 req->smallSetElementSetNames = esn;
1844 req->smallSetElementSetNames = NULL;
1846 if (obj->set_inher.mediumSetElementSetNames &&
1847 *obj->set_inher.mediumSetElementSetNames)
1849 Z_ElementSetNames *esn = odr_malloc (p->odr_out, sizeof(*esn));
1851 esn->which = Z_ElementSetNames_generic;
1852 esn->u.generic = obj->set_inher.mediumSetElementSetNames;
1853 req->mediumSetElementSetNames = esn;
1856 req->mediumSetElementSetNames = NULL;
1858 req->query = &query;
1860 if (!strcmp (obj->set_inher.queryType, "rpn"))
1862 Z_RPNQuery *RPNquery;
1864 RPNquery = p_query_rpn (p->odr_out, argv[2]);
1867 Tcl_AppendResult (interp, "Syntax error in query", NULL);
1870 RPNquery->attributeSetId = oid_getoidbyent (&bib1);
1871 query.which = Z_Query_type_1;
1872 query.u.type_1 = RPNquery;
1873 logf (LOG_DEBUG, "RPN");
1876 else if (!strcmp (obj->set_inher.queryType, "cclrpn"))
1880 struct ccl_rpn_node *rpn;
1881 Z_RPNQuery *RPNquery;
1883 rpn = ccl_find_str(p->bibset, argv[2], &error, &pos);
1886 Tcl_AppendResult (interp, "CCL error: ",
1887 ccl_err_msg(error), NULL);
1890 ccl_pr_tree (rpn, stderr);
1891 fprintf (stderr, "\n");
1892 assert((RPNquery = ccl_rpn_query(rpn)));
1893 RPNquery->attributeSetId = oid_getoidbyent (&bib1);
1894 query.which = Z_Query_type_1;
1895 query.u.type_1 = RPNquery;
1896 logf (LOG_DEBUG, "CCLRPN");
1899 else if (!strcmp (obj->set_inher.queryType, "ccl"))
1901 query.which = Z_Query_type_2;
1902 query.u.type_2 = &ccl_query;
1903 ccl_query.buf = (unsigned char *) argv[2];
1904 ccl_query.len = strlen (argv[2]);
1905 logf (LOG_DEBUG, "CCL");
1909 interp->result = "unknown query method";
1912 return ir_tcl_send_APDU (interp, p, apdu, "search", *argv);
1916 * do_searchResponse: add search response handler
1918 static int do_searchResponse (void *o, Tcl_Interp *interp,
1919 int argc, char **argv)
1921 IrTcl_SetObj *obj = o;
1925 obj->searchResponse = NULL;
1928 else if (argc == -1)
1929 return ir_tcl_strdel (interp, &obj->searchResponse);
1932 free (obj->searchResponse);
1935 if (ir_tcl_strdup (interp, &obj->searchResponse, argv[2])
1940 obj->searchResponse = NULL;
1946 * do_presentResponse: add present response handler
1948 static int do_presentResponse (void *o, Tcl_Interp *interp,
1949 int argc, char **argv)
1951 IrTcl_SetObj *obj = o;
1955 obj->presentResponse = NULL;
1958 else if (argc == -1)
1959 return ir_tcl_strdel (interp, &obj->presentResponse);
1962 free (obj->presentResponse);
1965 if (ir_tcl_strdup (interp, &obj->presentResponse, argv[2])
1970 obj->presentResponse = NULL;
1976 * do_resultCount: Get number of hits
1978 static int do_resultCount (void *o, Tcl_Interp *interp,
1979 int argc, char **argv)
1981 IrTcl_SetObj *obj = o;
1985 return get_set_int (&obj->resultCount, interp, argc, argv);
1989 * do_searchStatus: Get search status (after search response)
1991 static int do_searchStatus (void *o, Tcl_Interp *interp,
1992 int argc, char **argv)
1994 IrTcl_SetObj *obj = o;
1998 return get_set_int (&obj->searchStatus, interp, argc, argv);
2002 * do_presentStatus: Get search status (after search/present response)
2004 static int do_presentStatus (void *o, Tcl_Interp *interp,
2005 int argc, char **argv)
2007 IrTcl_SetObj *obj = o;
2011 return get_set_int (&obj->presentStatus, interp, argc, argv);
2015 * do_nextResultSetPosition: Get next result set position
2016 * (after search/present response)
2018 static int do_nextResultSetPosition (void *o, Tcl_Interp *interp,
2019 int argc, char **argv)
2021 IrTcl_SetObj *obj = o;
2025 obj->nextResultSetPosition = 0;
2028 return get_set_int (&obj->nextResultSetPosition, interp, argc, argv);
2032 * do_setName: Set result Set name
2034 static int do_setName (void *o, Tcl_Interp *interp,
2035 int argc, char **argv)
2037 IrTcl_SetObj *obj = o;
2040 return ir_tcl_strdup (interp, &obj->setName, "Default");
2041 else if (argc == -1)
2042 return ir_tcl_strdel (interp, &obj->setName);
2045 free (obj->setName);
2046 if (ir_tcl_strdup (interp, &obj->setName, argv[2])
2050 Tcl_AppendElement (interp, obj->setName);
2055 * do_numberOfRecordsReturned: Get number of records returned
2057 static int do_numberOfRecordsReturned (void *o, Tcl_Interp *interp,
2058 int argc, char **argv)
2060 IrTcl_SetObj *obj = o;
2064 obj->numberOfRecordsReturned = 0;
2067 return get_set_int (&obj->numberOfRecordsReturned, interp, argc, argv);
2071 * do_type: Return type (if any) at position.
2073 static int do_type (void *o, Tcl_Interp *interp, int argc, char **argv)
2075 IrTcl_SetObj *obj = o;
2077 IrTcl_RecordList *rl;
2081 obj->record_list = NULL;
2084 else if (argc == -1)
2086 delete_IR_records (obj);
2091 sprintf (interp->result, "wrong # args");
2094 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2096 rl = find_IR_record (obj, offset);
2099 logf (LOG_DEBUG, "No record at position %d", offset);
2104 case Z_NamePlusRecord_databaseRecord:
2105 interp->result = "DB";
2107 case Z_NamePlusRecord_surrogateDiagnostic:
2108 interp->result = "SD";
2116 * do_recordType: Return record type (if any) at position.
2118 static int do_recordType (void *o, Tcl_Interp *interp, int argc, char **argv)
2120 IrTcl_SetObj *obj = o;
2122 IrTcl_RecordList *rl;
2128 else if (argc == -1)
2134 sprintf (interp->result, "wrong # args");
2137 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2139 rl = find_IR_record (obj, offset);
2142 if (rl->which != Z_NamePlusRecord_databaseRecord)
2144 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2147 Tcl_AppendElement (interp, (char*)
2148 IrTcl_getRecordSyntaxStr (rl->u.dbrec.type));
2153 * set record elements (for record extraction)
2155 static int do_recordElements (void *o, Tcl_Interp *interp,
2156 int argc, char **argv)
2158 IrTcl_SetObj *obj = o;
2162 obj->recordElements = NULL;
2165 else if (argc == -1)
2166 return ir_tcl_strdel (NULL, &obj->recordElements);
2169 sprintf (interp->result, "wrong # args");
2174 free (obj->recordElements);
2175 return ir_tcl_strdup (NULL, &obj->recordElements,
2176 (*argv[2] ? argv[2] : NULL));
2178 Tcl_AppendResult (interp, obj->recordElements, NULL);
2185 static int ir_diagResult (Tcl_Interp *interp, IrTcl_Diagnostic *list, int num)
2191 for (i = 0; i<num; i++)
2193 logf (LOG_DEBUG, "Diagnostic, code %d", list[i].condition);
2194 sprintf (buf, "%d", list[i].condition);
2195 Tcl_AppendElement (interp, buf);
2196 cp = diagbib1_str (list[i].condition);
2198 Tcl_AppendElement (interp, (char*) cp);
2200 Tcl_AppendElement (interp, "");
2201 if (list[i].addinfo)
2202 Tcl_AppendElement (interp, (char*) list[i].addinfo);
2204 Tcl_AppendElement (interp, "");
2210 * do_diag: Return diagnostic record info
2212 static int do_diag (void *o, Tcl_Interp *interp, int argc, char **argv)
2214 IrTcl_SetObj *obj = o;
2216 IrTcl_RecordList *rl;
2222 sprintf (interp->result, "wrong # args");
2225 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2227 rl = find_IR_record (obj, offset);
2230 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2233 if (rl->which != Z_NamePlusRecord_surrogateDiagnostic)
2235 Tcl_AppendResult (interp, "No Diagnostic record at #", argv[2], NULL);
2238 return ir_diagResult (interp, rl->u.surrogateDiagnostics.list,
2239 rl->u.surrogateDiagnostics.num);
2243 * do_getMarc: Get ISO2709 Record lines/fields
2245 static int do_getMarc (void *o, Tcl_Interp *interp, int argc, char **argv)
2247 IrTcl_SetObj *obj = o;
2249 IrTcl_RecordList *rl;
2255 sprintf (interp->result, "wrong # args");
2258 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2260 rl = find_IR_record (obj, offset);
2263 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2266 if (rl->which != Z_NamePlusRecord_databaseRecord)
2268 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2271 return ir_tcl_get_marc (interp, rl->u.dbrec.buf, argc, argv);
2275 * do_getSutrs: Get SUTRS Record
2277 static int do_getSutrs (void *o, Tcl_Interp *interp, int argc, char **argv)
2279 IrTcl_SetObj *obj = o;
2281 IrTcl_RecordList *rl;
2287 sprintf (interp->result, "wrong # args");
2290 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2292 rl = find_IR_record (obj, offset);
2295 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2298 if (rl->which != Z_NamePlusRecord_databaseRecord)
2300 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2303 if (rl->u.dbrec.type != VAL_SUTRS)
2305 Tcl_AppendElement (interp, rl->u.dbrec.buf);
2311 * do_getGrs: Get a GRS1 Record
2313 static int do_getGrs (void *o, Tcl_Interp *interp, int argc, char **argv)
2315 IrTcl_SetObj *obj = o;
2317 IrTcl_RecordList *rl;
2323 sprintf (interp->result, "wrong # args");
2326 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2328 rl = find_IR_record (obj, offset);
2331 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2334 if (rl->which != Z_NamePlusRecord_databaseRecord)
2336 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2339 if (rl->u.dbrec.type != VAL_GRS1)
2341 return ir_tcl_get_grs (interp, rl->u.dbrec.u.grs1, argc, argv);
2346 * do_responseStatus: Return response status (present or search)
2348 static int do_responseStatus (void *o, Tcl_Interp *interp,
2349 int argc, char **argv)
2351 IrTcl_SetObj *obj = o;
2355 obj->recordFlag = 0;
2356 obj->nonSurrogateDiagnosticNum = 0;
2357 obj->nonSurrogateDiagnosticList = NULL;
2360 else if (argc == -1)
2362 ir_deleteDiags (&obj->nonSurrogateDiagnosticList,
2363 &obj->nonSurrogateDiagnosticNum);
2366 if (!obj->recordFlag)
2368 Tcl_AppendElement (interp, "OK");
2373 case Z_Records_DBOSD:
2374 Tcl_AppendElement (interp, "DBOSD");
2377 Tcl_AppendElement (interp, "NSD");
2378 return ir_diagResult (interp, obj->nonSurrogateDiagnosticList,
2379 obj->nonSurrogateDiagnosticNum);
2385 * do_present: Perform Present Request
2388 static int do_present (void *o, Tcl_Interp *interp, int argc, char **argv)
2390 IrTcl_SetObj *obj = o;
2393 Z_PresentRequest *req;
2401 if (Tcl_GetInt (interp, argv[2], &start) == TCL_ERROR)
2408 if (Tcl_GetInt (interp, argv[3], &number) == TCL_ERROR)
2416 interp->result = "present: not connected";
2421 obj->number = number;
2423 apdu = zget_APDU (p->odr_out, Z_APDU_presentRequest);
2424 req = apdu->u.presentRequest;
2426 set_referenceId (p->odr_out, &req->referenceId,
2427 obj->set_inher.referenceId);
2429 req->resultSetId = obj->setName ? obj->setName : "Default";
2431 req->resultSetStartPoint = &start;
2432 req->numberOfRecordsRequested = &number;
2433 if (obj->set_inher.preferredRecordSyntax)
2435 struct oident ident;
2437 ident.proto = p->protocol_type;
2438 ident.oclass = CLASS_RECSYN;
2439 ident.value = *obj->set_inher.preferredRecordSyntax;
2440 logf (LOG_DEBUG, "Preferred record syntax is %d", ident.value);
2441 req->preferredRecordSyntax = odr_oiddup (p->odr_out,
2442 oid_getoidbyent (&ident));
2445 req->preferredRecordSyntax = 0;
2447 if (obj->set_inher.elementSetNames && *obj->set_inher.elementSetNames)
2449 Z_ElementSetNames *esn = odr_malloc (p->odr_out, sizeof(*esn));
2450 Z_RecordComposition *compo = odr_malloc (p->odr_out, sizeof(*compo));
2452 esn->which = Z_ElementSetNames_generic;
2453 esn->u.generic = obj->set_inher.elementSetNames;
2455 req->recordComposition = compo;
2456 compo->which = Z_RecordComp_simple;
2457 compo->u.simple = esn;
2460 req->recordComposition = NULL;
2461 return ir_tcl_send_APDU (interp, p, apdu, "present", *argv);
2465 * do_loadFile: Load result set from file
2468 static int do_loadFile (void *o, Tcl_Interp *interp,
2469 int argc, char **argv)
2471 IrTcl_SetObj *setobj = o;
2481 interp->result = "wrong # args";
2484 inf = fopen (argv[2], "r");
2487 Tcl_AppendResult (interp, "Cannot open ", argv[2], NULL);
2490 while ((buf = ir_tcl_fread_marc (inf, &size)))
2492 IrTcl_RecordList *rl;
2494 rl = new_IR_record (setobj, no, Z_NamePlusRecord_databaseRecord, "F");
2495 rl->u.dbrec.type = VAL_USMARC;
2496 rl->u.dbrec.buf = buf;
2497 rl->u.dbrec.size = size;
2500 setobj->numberOfRecordsReturned = no-1;
2505 static IrTcl_Method ir_set_method_tab[] = {
2506 { 0, "search", do_search },
2507 { 0, "searchResponse", do_searchResponse },
2508 { 0, "presentResponse", do_presentResponse },
2509 { 0, "searchStatus", do_searchStatus },
2510 { 0, "presentStatus", do_presentStatus },
2511 { 0, "nextResultSetPosition", do_nextResultSetPosition },
2512 { 0, "setName", do_setName },
2513 { 0, "resultCount", do_resultCount },
2514 { 0, "numberOfRecordsReturned", do_numberOfRecordsReturned },
2515 { 0, "present", do_present },
2516 { 0, "type", do_type },
2517 { 0, "getMarc", do_getMarc },
2518 { 0, "getSutrs", do_getSutrs },
2519 { 0, "getGrs", do_getGrs },
2520 { 0, "recordType", do_recordType },
2521 { 0, "recordElements", do_recordElements },
2522 { 0, "diag", do_diag },
2523 { 0, "responseStatus", do_responseStatus },
2524 { 0, "loadFile", do_loadFile },
2529 * ir_set_obj_method: IR Set Object methods
2531 static int ir_set_obj_method (ClientData clientData, Tcl_Interp *interp,
2532 int argc, char **argv)
2534 IrTcl_Methods tabs[3];
2535 IrTcl_SetObj *p = clientData;
2539 interp->result = "wrong # args";
2542 tabs[0].tab = ir_set_method_tab;
2544 tabs[1].tab = ir_set_c_method_tab;
2545 tabs[1].obj = &p->set_inher;
2548 return ir_method (interp, argc, argv, tabs);
2552 * ir_set_obj_delete: IR Set Object disposal
2554 static void ir_set_obj_delete (ClientData clientData)
2556 IrTcl_Methods tabs[3];
2557 IrTcl_SetObj *p = clientData;
2559 logf (LOG_DEBUG, "ir set delete");
2561 tabs[0].tab = ir_set_method_tab;
2563 tabs[1].tab = ir_set_c_method_tab;
2564 tabs[1].obj = &p->set_inher;
2567 ir_method (NULL, -1, NULL, tabs);
2573 * ir_set_obj_mk: IR Set Object creation
2575 static int ir_set_obj_mk (ClientData clientData, Tcl_Interp *interp,
2576 int argc, char **argv)
2578 IrTcl_Methods tabs[3];
2581 if (argc < 2 || argc > 3)
2583 interp->result = "wrong # args";
2586 obj = ir_tcl_malloc (sizeof(*obj));
2587 logf (LOG_DEBUG, "ir set create");
2590 Tcl_CmdInfo parent_info;
2595 if (!Tcl_GetCommandInfo (interp, argv[2], &parent_info))
2597 interp->result = "No parent";
2600 obj->parent = (IrTcl_Obj *) parent_info.clientData;
2602 dst = &obj->set_inher;
2603 src = &obj->parent->set_inher;
2605 if ((dst->num_databaseNames = src->num_databaseNames))
2606 dst->databaseNames =
2607 ir_tcl_malloc (sizeof (*dst->databaseNames)
2608 * dst->num_databaseNames);
2610 dst->databaseNames = NULL;
2611 for (i = 0; i < dst->num_databaseNames; i++)
2612 if (ir_tcl_strdup (interp, &dst->databaseNames[i],
2613 src->databaseNames[i]) == TCL_ERROR)
2615 if (ir_tcl_strdup (interp, &dst->queryType, src->queryType)
2619 if (ir_tcl_strdup (interp, &dst->referenceId, src->referenceId)
2623 if (ir_tcl_strdup (interp, &dst->elementSetNames, src->elementSetNames)
2627 if (ir_tcl_strdup (interp, &dst->smallSetElementSetNames,
2628 src->smallSetElementSetNames)
2632 if (ir_tcl_strdup (interp, &dst->mediumSetElementSetNames,
2633 src->mediumSetElementSetNames)
2637 if (src->preferredRecordSyntax &&
2638 (dst->preferredRecordSyntax
2639 = ir_tcl_malloc (sizeof(*dst->preferredRecordSyntax))))
2640 *dst->preferredRecordSyntax = *src->preferredRecordSyntax;
2642 dst->preferredRecordSyntax = NULL;
2643 dst->replaceIndicator = src->replaceIndicator;
2644 dst->smallSetUpperBound = src->smallSetUpperBound;
2645 dst->largeSetLowerBound = src->largeSetLowerBound;
2646 dst->mediumSetPresentNumber = src->mediumSetPresentNumber;
2651 tabs[0].tab = ir_set_method_tab;
2655 if (ir_method (interp, 0, NULL, tabs) == TCL_ERROR)
2658 Tcl_CreateCommand (interp, argv[1], ir_set_obj_method,
2659 (ClientData) obj, ir_set_obj_delete);
2663 /* ------------------------------------------------------- */
2666 * do_scan: Perform scan
2668 static int do_scan (void *o, Tcl_Interp *interp, int argc, char **argv)
2672 IrTcl_ScanObj *obj = o;
2673 IrTcl_Obj *p = obj->parent;
2676 struct ccl_rpn_node *rpn;
2684 interp->result = "wrong # args";
2687 if (!p->set_inher.num_databaseNames)
2689 interp->result = "no databaseNames";
2694 interp->result = "scan: not connected";
2698 bib1.proto = p->protocol_type;
2699 bib1.oclass = CLASS_ATTSET;
2700 bib1.value = VAL_BIB1;
2702 apdu = zget_APDU (p->odr_out, Z_APDU_scanRequest);
2703 req = apdu->u.scanRequest;
2705 set_referenceId (p->odr_out, &req->referenceId, p->set_inher.referenceId);
2706 req->num_databaseNames = p->set_inher.num_databaseNames;
2707 req->databaseNames = p->set_inher.databaseNames;
2708 req->attributeSet = oid_getoidbyent (&bib1);
2711 if (!(req->termListAndStartPoint = p_query_scan (p->odr_out, argv[2])))
2713 Tcl_AppendResult (interp, "Syntax error in query", NULL);
2717 rpn = ccl_find_str(p->bibset, argv[2], &r, &pos);
2720 Tcl_AppendResult (interp, "CCL error: ", ccl_err_msg (r), NULL);
2723 ccl_pr_tree (rpn, stderr);
2724 fprintf (stderr, "\n");
2725 if (!(req->termListAndStartPoint = ccl_scan_query (rpn)))
2728 req->stepSize = &obj->stepSize;
2729 req->numberOfTermsRequested = &obj->numberOfTermsRequested;
2730 req->preferredPositionInResponse = &obj->preferredPositionInResponse;
2731 logf (LOG_DEBUG, "stepSize=%d", *req->stepSize);
2732 logf (LOG_DEBUG, "numberOfTermsRequested=%d",
2733 *req->numberOfTermsRequested);
2734 logf (LOG_DEBUG, "preferredPositionInResponse=%d",
2735 *req->preferredPositionInResponse);
2737 return ir_tcl_send_APDU (interp, p, apdu, "scan", *argv);
2741 * do_scanResponse: add scan response handler
2743 static int do_scanResponse (void *o, Tcl_Interp *interp,
2744 int argc, char **argv)
2746 IrTcl_ScanObj *obj = o;
2750 obj->scanResponse = NULL;
2753 else if (argc == -1)
2754 return ir_tcl_strdel (interp, &obj->scanResponse);
2757 free (obj->scanResponse);
2760 if (ir_tcl_strdup (interp, &obj->scanResponse, argv[2])
2765 obj->scanResponse = NULL;
2771 * do_stepSize: Set/get replace Step Size
2773 static int do_stepSize (void *obj, Tcl_Interp *interp,
2774 int argc, char **argv)
2776 IrTcl_ScanObj *p = obj;
2782 return get_set_int (&p->stepSize, interp, argc, argv);
2786 * do_numberOfTermsRequested: Set/get Number of Terms requested
2788 static int do_numberOfTermsRequested (void *obj, Tcl_Interp *interp,
2789 int argc, char **argv)
2791 IrTcl_ScanObj *p = obj;
2795 p->numberOfTermsRequested = 20;
2798 return get_set_int (&p->numberOfTermsRequested, interp, argc, argv);
2803 * do_preferredPositionInResponse: Set/get preferred Position
2805 static int do_preferredPositionInResponse (void *obj, Tcl_Interp *interp,
2806 int argc, char **argv)
2808 IrTcl_ScanObj *p = obj;
2812 p->preferredPositionInResponse = 1;
2815 return get_set_int (&p->preferredPositionInResponse, interp, argc, argv);
2819 * do_scanStatus: Get scan status
2821 static int do_scanStatus (void *obj, Tcl_Interp *interp,
2822 int argc, char **argv)
2824 IrTcl_ScanObj *p = obj;
2828 return get_set_int (&p->scanStatus, interp, argc, argv);
2832 * do_numberOfEntriesReturned: Get number of Entries returned
2834 static int do_numberOfEntriesReturned (void *obj, Tcl_Interp *interp,
2835 int argc, char **argv)
2837 IrTcl_ScanObj *p = obj;
2841 return get_set_int (&p->numberOfEntriesReturned, interp, argc, argv);
2845 * do_positionOfTerm: Get position of Term
2847 static int do_positionOfTerm (void *obj, Tcl_Interp *interp,
2848 int argc, char **argv)
2850 IrTcl_ScanObj *p = obj;
2854 return get_set_int (&p->positionOfTerm, interp, argc, argv);
2858 * do_scanLine: get Scan Line (surrogate or normal) after response
2860 static int do_scanLine (void *obj, Tcl_Interp *interp, int argc, char **argv)
2862 IrTcl_ScanObj *p = obj;
2868 p->entries_flag = 0;
2870 p->nonSurrogateDiagnosticNum = 0;
2871 p->nonSurrogateDiagnosticList = 0;
2874 else if (argc == -1)
2876 p->entries_flag = 0;
2877 /* release entries */
2880 ir_deleteDiags (&p->nonSurrogateDiagnosticList,
2881 &p->nonSurrogateDiagnosticNum);
2886 interp->result = "wrong # args";
2889 if (Tcl_GetInt (interp, argv[2], &i) == TCL_ERROR)
2891 if (!p->entries_flag || p->which != Z_ListEntries_entries || !p->entries
2892 || i >= p->num_entries || i < 0)
2894 switch (p->entries[i].which)
2896 case Z_Entry_termInfo:
2897 Tcl_AppendElement (interp, "T");
2898 if (p->entries[i].u.term.buf)
2899 Tcl_AppendElement (interp, p->entries[i].u.term.buf);
2901 Tcl_AppendElement (interp, "");
2902 sprintf (numstr, "%d", p->entries[i].u.term.globalOccurrences);
2903 Tcl_AppendElement (interp, numstr);
2905 case Z_Entry_surrogateDiagnostic:
2906 Tcl_AppendElement (interp, "SD");
2907 return ir_diagResult (interp, p->entries[i].u.diag.list,
2908 p->entries[i].u.diag.num);
2914 static IrTcl_Method ir_scan_method_tab[] = {
2915 { 0, "scan", do_scan },
2916 { 0, "scanResponse", do_scanResponse },
2917 { 0, "stepSize", do_stepSize },
2918 { 0, "numberOfTermsRequested", do_numberOfTermsRequested },
2919 { 0, "preferredPositionInResponse", do_preferredPositionInResponse },
2920 { 0, "scanStatus", do_scanStatus },
2921 { 0, "numberOfEntriesReturned", do_numberOfEntriesReturned },
2922 { 0, "positionOfTerm", do_positionOfTerm },
2923 { 0, "scanLine", do_scanLine },
2928 * ir_scan_obj_method: IR Scan Object methods
2930 static int ir_scan_obj_method (ClientData clientData, Tcl_Interp *interp,
2931 int argc, char **argv)
2933 IrTcl_Methods tabs[2];
2937 interp->result = "wrong # args";
2940 tabs[0].tab = ir_scan_method_tab;
2941 tabs[0].obj = clientData;
2944 return ir_method (interp, argc, argv, tabs);
2948 * ir_scan_obj_delete: IR Scan Object disposal
2950 static void ir_scan_obj_delete (ClientData clientData)
2952 IrTcl_Methods tabs[2];
2953 IrTcl_ScanObj *obj = clientData;
2955 tabs[0].tab = ir_scan_method_tab;
2959 ir_method (NULL, -1, NULL, tabs);
2964 * ir_scan_obj_mk: IR Scan Object creation
2966 static int ir_scan_obj_mk (ClientData clientData, Tcl_Interp *interp,
2967 int argc, char **argv)
2969 Tcl_CmdInfo parent_info;
2971 IrTcl_Methods tabs[2];
2975 interp->result = "wrong # args";
2978 if (!Tcl_GetCommandInfo (interp, argv[2], &parent_info))
2980 interp->result = "No parent";
2983 obj = ir_tcl_malloc (sizeof(*obj));
2984 obj->parent = (IrTcl_Obj *) parent_info.clientData;
2986 tabs[0].tab = ir_scan_method_tab;
2990 if (ir_method (interp, 0, NULL, tabs) == TCL_ERROR)
2992 Tcl_CreateCommand (interp, argv[1], ir_scan_obj_method,
2993 (ClientData) obj, ir_scan_obj_delete);
2997 /* ------------------------------------------------------- */
2999 static void ir_initResponse (void *obj, Z_InitResponse *initrs)
3003 p->initResult = *initrs->result ? 1 : 0;
3004 if (!*initrs->result)
3005 logf (LOG_DEBUG, "Connection rejected by target");
3007 logf (LOG_DEBUG, "Connection accepted by target");
3009 get_referenceId (&p->set_inher.referenceId, initrs->referenceId);
3011 free (p->targetImplementationId);
3012 ir_tcl_strdup (p->interp, &p->targetImplementationId,
3013 initrs->implementationId);
3014 free (p->targetImplementationName);
3015 ir_tcl_strdup (p->interp, &p->targetImplementationName,
3016 initrs->implementationName);
3017 free (p->targetImplementationVersion);
3018 ir_tcl_strdup (p->interp, &p->targetImplementationVersion,
3019 initrs->implementationVersion);
3021 p->maximumRecordSize = *initrs->maximumRecordSize;
3022 p->preferredMessageSize = *initrs->preferredMessageSize;
3024 memcpy (&p->options, initrs->options, sizeof(initrs->options));
3025 memcpy (&p->protocolVersion, initrs->protocolVersion,
3026 sizeof(initrs->protocolVersion));
3027 free (p->userInformationField);
3028 p->userInformationField = NULL;
3029 if (initrs->userInformationField)
3033 if (initrs->userInformationField->which == ODR_EXTERNAL_octet &&
3034 (p->userInformationField =
3035 ir_tcl_malloc ((len =
3036 initrs->userInformationField->
3037 u.octet_aligned->len) +1)))
3039 memcpy (p->userInformationField,
3040 initrs->userInformationField->u.octet_aligned->buf,
3042 (p->userInformationField)[len] = '\0';
3047 static void ir_deleteDiags (IrTcl_Diagnostic **dst_list, int *dst_num)
3050 for (i = 0; i<*dst_num; i++)
3051 free (dst_list[i]->addinfo);
3057 static void ir_handleDiags (IrTcl_Diagnostic **dst_list, int *dst_num,
3058 Z_DiagRec **list, int num)
3064 *dst_list = ir_tcl_malloc (sizeof(**dst_list) * num);
3065 for (i = 0; i<num; i++)
3067 switch (list[i]->which)
3069 case Z_DiagRec_defaultFormat:
3070 (*dst_list)[i].condition = *list[i]->u.defaultFormat->condition;
3071 addinfo = list[i]->u.defaultFormat->addinfo;
3073 ((*dst_list)[i].addinfo = ir_tcl_malloc (strlen(addinfo)+1)))
3074 strcpy ((*dst_list)[i].addinfo, addinfo);
3077 (*dst_list)[i].addinfo = NULL;
3078 (*dst_list)[i].condition = 0;
3083 static void ir_handleRecords (void *o, Z_Records *zrs, IrTcl_SetObj *setobj,
3084 const char *elements)
3089 IrTcl_RecordList *rl;
3091 setobj->which = zrs->which;
3092 setobj->recordFlag = 1;
3094 ir_deleteDiags (&setobj->nonSurrogateDiagnosticList,
3095 &setobj->nonSurrogateDiagnosticNum);
3096 if (zrs->which == Z_Records_DBOSD)
3098 setobj->numberOfRecordsReturned =
3099 zrs->u.databaseOrSurDiagnostics->num_records;
3100 logf (LOG_DEBUG, "Got %d records", setobj->numberOfRecordsReturned);
3101 for (offset = 0; offset<setobj->numberOfRecordsReturned; offset++)
3103 rl = new_IR_record (setobj, setobj->start + offset,
3104 zrs->u.databaseOrSurDiagnostics->
3105 records[offset]->which,
3107 if (rl->which == Z_NamePlusRecord_surrogateDiagnostic)
3109 ir_handleDiags (&rl->u.surrogateDiagnostics.list,
3110 &rl->u.surrogateDiagnostics.num,
3111 &zrs->u.databaseOrSurDiagnostics->
3112 records[offset]->u.surrogateDiagnostic,
3117 Z_DatabaseRecord *zr;
3119 struct oident *ident;
3121 zr = zrs->u.databaseOrSurDiagnostics->records[offset]
3123 oe = (Z_External*) zr;
3124 rl->u.dbrec.size = zr->u.octet_aligned->len;
3126 if ((ident = oid_getentbyoid (oe->direct_reference)))
3127 rl->u.dbrec.type = ident->value;
3129 rl->u.dbrec.type = VAL_USMARC;
3131 if (oe->which == ODR_EXTERNAL_octet && rl->u.dbrec.size > 0)
3133 char *buf = (char*) zr->u.octet_aligned->buf;
3134 if ((rl->u.dbrec.buf = ir_tcl_malloc (rl->u.dbrec.size)))
3135 memcpy (rl->u.dbrec.buf, buf, rl->u.dbrec.size);
3137 else if (rl->u.dbrec.type == VAL_SUTRS &&
3138 oe->which == Z_External_sutrs)
3140 odr_setbuf (p->odr_in, (char*) oe->u.single_ASN1_type->buf,
3141 oe->u.single_ASN1_type->len, 0);
3142 if ((rl->u.dbrec.buf = ir_tcl_malloc (oe->u.sutrs->len+1)))
3144 memcpy (rl->u.dbrec.buf, oe->u.sutrs->buf,
3146 rl->u.dbrec.buf[oe->u.sutrs->len] = '\0';
3148 rl->u.dbrec.size = oe->u.sutrs->len;
3150 else if (rl->u.dbrec.type == VAL_GRS1 &&
3151 oe->which == Z_External_grs1)
3153 ir_tcl_read_grs (oe->u.grs1, &rl->u.dbrec.u.grs1);
3154 rl->u.dbrec.buf = NULL;
3157 rl->u.dbrec.buf = NULL;
3161 else if (zrs->which == Z_Records_multipleNSD)
3163 logf (LOG_DEBUG, "multipleNonSurrogateDiagnostic %d",
3164 zrs->u.multipleNonSurDiagnostics->num_diagRecs);
3165 setobj->numberOfRecordsReturned = 0;
3166 ir_handleDiags (&setobj->nonSurrogateDiagnosticList,
3167 &setobj->nonSurrogateDiagnosticNum,
3168 zrs->u.multipleNonSurDiagnostics->diagRecs,
3169 zrs->u.multipleNonSurDiagnostics->num_diagRecs);
3173 logf (LOG_DEBUG, "NonSurrogateDiagnostic");
3174 setobj->numberOfRecordsReturned = 0;
3175 ir_handleDiags (&setobj->nonSurrogateDiagnosticList,
3176 &setobj->nonSurrogateDiagnosticNum,
3177 &zrs->u.nonSurrogateDiagnostic,
3182 static void ir_searchResponse (void *o, Z_SearchResponse *searchrs,
3183 IrTcl_SetObj *setobj)
3185 Z_Records *zrs = searchrs->records;
3187 logf (LOG_DEBUG, "Received search response");
3190 logf (LOG_DEBUG, "Search response, no object!");
3193 setobj->searchStatus = searchrs->searchStatus ? 1 : 0;
3194 get_referenceId (&setobj->set_inher.referenceId, searchrs->referenceId);
3195 setobj->resultCount = *searchrs->resultCount;
3196 if (searchrs->presentStatus)
3197 setobj->presentStatus = *searchrs->presentStatus;
3198 if (searchrs->nextResultSetPosition)
3199 setobj->nextResultSetPosition = *searchrs->nextResultSetPosition;
3201 logf (LOG_DEBUG, "Search response %d, %d hits",
3202 setobj->searchStatus, setobj->resultCount);
3206 if (setobj->resultCount <= setobj->set_inher.smallSetUpperBound)
3207 es = setobj->set_inher.smallSetElementSetNames;
3209 es = setobj->set_inher.mediumSetElementSetNames;
3210 ir_handleRecords (o, zrs, setobj, es);
3213 setobj->recordFlag = 0;
3217 static void ir_presentResponse (void *o, Z_PresentResponse *presrs,
3218 IrTcl_SetObj *setobj)
3220 Z_Records *zrs = presrs->records;
3222 logf (LOG_DEBUG, "Received present response");
3225 logf (LOG_DEBUG, "Present response, no object!");
3228 setobj->presentStatus = *presrs->presentStatus;
3229 get_referenceId (&setobj->set_inher.referenceId, presrs->referenceId);
3230 setobj->nextResultSetPosition = *presrs->nextResultSetPosition;
3232 ir_handleRecords (o, zrs, setobj, setobj->set_inher.elementSetNames);
3235 setobj->recordFlag = 0;
3236 logf (LOG_DEBUG, "No records!");
3240 static void ir_scanResponse (void *o, Z_ScanResponse *scanrs,
3241 IrTcl_ScanObj *scanobj)
3245 logf (LOG_DEBUG, "Received scanResponse");
3247 get_referenceId (&p->set_inher.referenceId, scanrs->referenceId);
3248 scanobj->scanStatus = *scanrs->scanStatus;
3249 logf (LOG_DEBUG, "scanStatus=%d", scanobj->scanStatus);
3251 if (scanrs->stepSize)
3252 scanobj->stepSize = *scanrs->stepSize;
3253 logf (LOG_DEBUG, "stepSize=%d", scanobj->stepSize);
3255 scanobj->numberOfEntriesReturned = *scanrs->numberOfEntriesReturned;
3256 logf (LOG_DEBUG, "numberOfEntriesReturned=%d",
3257 scanobj->numberOfEntriesReturned);
3259 if (scanrs->positionOfTerm)
3260 scanobj->positionOfTerm = *scanrs->positionOfTerm;
3262 scanobj->positionOfTerm = -1;
3263 logf (LOG_DEBUG, "positionOfTerm=%d", scanobj->positionOfTerm);
3265 free (scanobj->entries);
3266 scanobj->entries = NULL;
3268 ir_deleteDiags (&scanobj->nonSurrogateDiagnosticList,
3269 &scanobj->nonSurrogateDiagnosticNum);
3270 if (scanrs->entries)
3275 scanobj->entries_flag = 1;
3276 scanobj->which = scanrs->entries->which;
3277 switch (scanobj->which)
3279 case Z_ListEntries_entries:
3280 scanobj->num_entries = scanrs->entries->u.entries->num_entries;
3281 scanobj->entries = ir_tcl_malloc (scanobj->num_entries *
3282 sizeof(*scanobj->entries));
3283 for (i=0; i<scanobj->num_entries; i++)
3285 ze = scanrs->entries->u.entries->entries[i];
3286 scanobj->entries[i].which = ze->which;
3289 case Z_Entry_termInfo:
3290 if (ze->u.termInfo->term->which == Z_Term_general)
3292 int l = ze->u.termInfo->term->u.general->len;
3293 scanobj->entries[i].u.term.buf = ir_tcl_malloc (1+l);
3294 memcpy (scanobj->entries[i].u.term.buf,
3295 ze->u.termInfo->term->u.general->buf,
3297 scanobj->entries[i].u.term.buf[l] = '\0';
3300 scanobj->entries[i].u.term.buf = NULL;
3301 if (ze->u.termInfo->globalOccurrences)
3302 scanobj->entries[i].u.term.globalOccurrences =
3303 *ze->u.termInfo->globalOccurrences;
3305 scanobj->entries[i].u.term.globalOccurrences = 0;
3307 case Z_Entry_surrogateDiagnostic:
3308 ir_handleDiags (&scanobj->entries[i].u.diag.list,
3309 &scanobj->entries[i].u.diag.num,
3310 &ze->u.surrogateDiagnostic,
3316 case Z_ListEntries_nonSurrogateDiagnostics:
3317 ir_handleDiags (&scanobj->nonSurrogateDiagnosticList,
3318 &scanobj->nonSurrogateDiagnosticNum,
3319 scanrs->entries->u.nonSurrogateDiagnostics->
3321 scanrs->entries->u.nonSurrogateDiagnostics->
3327 scanobj->entries_flag = 0;
3331 * ir_select_read: handle incoming packages
3333 void ir_select_read (ClientData clientData)
3335 IrTcl_Obj *p = clientData;
3340 Tcl_CmdInfo cmd_info;
3341 const char *apdu_call;
3343 if (p->state == IR_TCL_R_Connecting)
3345 r = cs_rcvconnect (p->cs_link);
3348 logf (LOG_WARN, "cs_rcvconnect returned 1");
3351 p->state = IR_TCL_R_Idle;
3352 ir_select_remove_write (cs_fileno (p->cs_link), p);
3355 logf (LOG_DEBUG, "cs_rcvconnect error");
3358 p->failInfo = IR_TCL_FAIL_CONNECT;
3359 IrTcl_eval (p->interp, p->failback);
3361 do_disconnect (p, NULL, 2, NULL);
3364 p->state = IR_TCL_R_Idle;
3366 IrTcl_eval (p->interp, p->callback);
3367 if (p->cs_link && p->request_queue && p->state == IR_TCL_R_Idle)
3368 ir_tcl_send_q (p, p->request_queue, "x");
3373 /* signal one more use of ir object - callbacks must not
3374 release the ir memory (p pointer) */
3375 p->state = IR_TCL_R_Reading;
3378 /* read incoming APDU */
3379 if ((r=cs_get (p->cs_link, &p->buf_in, &p->len_in)) <= 0)
3381 logf (LOG_DEBUG, "cs_get failed, code %d", r);
3382 ir_select_remove (cs_fileno (p->cs_link), p);
3383 do_disconnect (p, NULL, 2, NULL);
3386 p->failInfo = IR_TCL_FAIL_READ;
3387 IrTcl_eval (p->interp, p->failback);
3389 /* release ir object now if callback deleted it */
3395 /* got complete APDU. Now decode */
3398 odr_setbuf (p->odr_in, p->buf_in, r, 0);
3399 logf (LOG_DEBUG, "cs_get ok, got %d", r);
3400 if (!z_APDU (p->odr_in, &apdu, 0))
3402 logf (LOG_DEBUG, "%s", odr_errmsg (odr_geterror (p->odr_in)));
3403 do_disconnect (p, NULL, 2, NULL);
3406 p->failInfo = IR_TCL_FAIL_IN_APDU;
3407 p->apduOffset = odr_offset (p->odr_in);
3408 IrTcl_eval (p->interp, p->failback);
3410 /* release ir object now if failback deleted it */
3414 /* handle APDU and invoke callback */
3415 rq = p->request_queue;
3418 logf (LOG_FATAL, "Internal error. No queue entry");
3421 object_name = rq->object_name;
3422 logf (LOG_DEBUG, "getCommandInfo (%s)", object_name);
3424 if (Tcl_GetCommandInfo (p->interp, object_name, &cmd_info))
3428 case Z_APDU_initResponse:
3429 p->eventType = "init";
3430 ir_initResponse (p, apdu->u.initResponse);
3431 apdu_call = p->initResponse;
3433 case Z_APDU_searchResponse:
3434 p->eventType = "search";
3435 ir_searchResponse (p, apdu->u.searchResponse,
3436 (IrTcl_SetObj *) cmd_info.clientData);
3437 apdu_call = ((IrTcl_SetObj *)
3438 cmd_info.clientData)->searchResponse;
3440 case Z_APDU_presentResponse:
3441 p->eventType = "present";
3442 ir_presentResponse (p, apdu->u.presentResponse,
3443 (IrTcl_SetObj *) cmd_info.clientData);
3444 apdu_call = ((IrTcl_SetObj *)
3445 cmd_info.clientData)->presentResponse;
3447 case Z_APDU_scanResponse:
3448 p->eventType = "scan";
3449 ir_scanResponse (p, apdu->u.scanResponse,
3450 (IrTcl_ScanObj *) cmd_info.clientData);
3451 apdu_call = ((IrTcl_ScanObj *)
3452 cmd_info.clientData)->scanResponse;
3455 logf (LOG_WARN, "Received unknown APDU type (%d)",
3457 do_disconnect (p, NULL, 2, NULL);
3460 p->failInfo = IR_TCL_FAIL_UNKNOWN_APDU;
3461 IrTcl_eval (p->interp, p->failback);
3466 p->request_queue = rq->next;
3467 p->state = IR_TCL_R_Idle;
3470 IrTcl_eval (p->interp, apdu_call);
3471 else if (rq->callback)
3472 IrTcl_eval (p->interp, rq->callback);
3474 free (rq->callback);
3475 free (rq->object_name);
3477 odr_reset (p->odr_in);
3478 if (p->ref_count == 1)
3484 } while (p->cs_link && cs_more (p->cs_link));
3485 if (p->cs_link && p->request_queue && p->state == IR_TCL_R_Idle)
3486 ir_tcl_send_q (p, p->request_queue, "x");
3490 * ir_select_write: handle outgoing packages - not yet written.
3492 void ir_select_write (ClientData clientData)
3494 IrTcl_Obj *p = clientData;
3498 logf (LOG_DEBUG, "In write handler");
3499 if (p->state == IR_TCL_R_Connecting)
3501 r = cs_rcvconnect (p->cs_link);
3504 p->state = IR_TCL_R_Idle;
3507 logf (LOG_DEBUG, "cs_rcvconnect error");
3508 ir_select_remove_write (cs_fileno (p->cs_link), p);
3511 p->failInfo = IR_TCL_FAIL_CONNECT;
3512 IrTcl_eval (p->interp, p->failback);
3514 do_disconnect (p, NULL, 2, NULL);
3517 ir_select_remove_write (cs_fileno (p->cs_link), p);
3519 IrTcl_eval (p->interp, p->callback);
3522 rq = p->request_queue;
3523 if (!rq || !rq->buf_out)
3526 if ((r=cs_put (p->cs_link, rq->buf_out, rq->len_out)) < 0)
3528 logf (LOG_DEBUG, "select write fail");
3531 p->failInfo = IR_TCL_FAIL_WRITE;
3532 IrTcl_eval (p->interp, p->failback);
3536 do_disconnect (p, NULL, 2, NULL);
3538 else if (r == 0) /* remove select bit */
3540 p->state = IR_TCL_R_Waiting;
3541 ir_select_remove_write (cs_fileno (p->cs_link), p);
3547 /* ------------------------------------------------------- */
3550 * Irtcl_init: Registration of TCL commands.
3552 int Irtcl_Init (Tcl_Interp *interp)
3554 Tcl_CreateCommand (interp, "ir", ir_obj_mk, (ClientData) NULL,
3555 (Tcl_CmdDeleteProc *) NULL);
3556 Tcl_CreateCommand (interp, "ir-set", ir_set_obj_mk,
3557 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
3558 Tcl_CreateCommand (interp, "ir-scan", ir_scan_obj_mk,
3559 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);