2 * IR toolkit for tcl/tk
4 * See the file LICENSE for details.
5 * Sebastian Hammer, Adam Dickmeiss
8 * Revision 1.74 1996-02-05 17:58:03 adam
9 * Ported ir-tcl to use the beta releases of tcl7.5/tk4.1.
11 * Revision 1.73 1996/01/29 11:35:19 adam
12 * Bug fix: cs_type member renamed to comstackType to avoid conflict with
13 * cs_type macro defined by YAZ.
15 * Revision 1.72 1996/01/19 17:45:34 quinn
16 * Added debugging output
18 * Revision 1.71 1996/01/19 16:22:38 adam
19 * New method: apduDump - returns information about last incoming APDU.
21 * Revision 1.70 1996/01/10 09:18:34 adam
22 * PDU specific callbacks implemented: initRespnse, searchResponse,
23 * presentResponse and scanResponse.
24 * Bug fix in the command line shell (tclmain.c) - discovered on OSF/1.
26 * Revision 1.69 1996/01/04 16:12:12 adam
27 * Setting PDUType renamed to eventType.
29 * Revision 1.68 1996/01/04 11:05:22 adam
30 * New setting: PDUType - returns type of last PDU returned from the target.
31 * Fixed a bug in configure/Makefile.
33 * Revision 1.67 1996/01/03 09:00:51 adam
34 * Updated to use new version of Yaz (names changed to avoid C++ conflict).
36 * Revision 1.66 1995/11/28 17:26:39 adam
37 * Removed Carriage return from ir-tcl.c!
38 * Removed misc. debug logs.
40 * Revision 1.65 1995/11/28 13:53:00 quinn
43 * Revision 1.64 1995/11/13 15:39:18 adam
44 * Bug fix: {small,medium}SetElementSetNames weren't set correctly.
45 * Bug fix: idAuthentication weren't set correctly.
47 * Revision 1.63 1995/11/13 09:55:39 adam
48 * Multiple records at a position in a result-set with differnt
51 * Revision 1.62 1995/10/18 17:20:33 adam
52 * Work on target setup in client.tcl.
54 * Revision 1.61 1995/10/18 16:42:42 adam
55 * New settings: smallSetElementSetNames and mediumSetElementSetNames.
57 * Revision 1.60 1995/10/18 15:43:31 adam
58 * In search: mediumSetElementSetNames and smallSetElementSetNames are
59 * set to elementSetNames.
61 * Revision 1.59 1995/10/17 12:18:58 adam
62 * Bug fix: when target connection closed, the connection was not
63 * properly reestablished.
65 * Revision 1.58 1995/10/16 17:00:55 adam
66 * New setting: elementSetNames.
67 * Various client improvements. Medium presentation format looks better.
69 * Revision 1.57 1995/09/21 13:11:51 adam
70 * Support of dynamic loading.
71 * Test script uses load command if necessary.
73 * Revision 1.56 1995/08/29 15:30:14 adam
74 * Work on GRS records.
76 * Revision 1.55 1995/08/28 09:43:25 adam
77 * Minor changes. configure only searches for yaz beta 3 and versions after
80 * Revision 1.54 1995/08/24 12:25:16 adam
81 * Modified to work with yaz 1.0b3.
83 * Revision 1.53 1995/08/04 12:49:26 adam
84 * Bug fix: reading uninitialized variable p.
86 * Revision 1.52 1995/08/04 11:32:38 adam
87 * More work on output queue. Memory related routines moved
90 * Revision 1.51 1995/08/03 13:22:54 adam
93 * Revision 1.50 1995/07/20 08:09:49 adam
94 * client.tcl: Targets removed from hotTargets list when targets
95 * are removed/modified.
96 * ir-tcl.c: More work on triggerResourceControl.
98 * Revision 1.49 1995/06/30 12:39:21 adam
99 * Bug fix: loadFile didn't set record type.
100 * The MARC routines are a little less strict in the interpretation.
101 * Script display.tcl replaces the old marc.tcl.
102 * New interactive script: shell.tcl.
104 * Revision 1.48 1995/06/27 19:03:50 adam
105 * Bug fix in do_present in ir-tcl.c: p->set_child member weren't set.
106 * nextResultSetPosition used instead of setOffset.
108 * Revision 1.47 1995/06/25 10:25:04 adam
109 * Working on triggerResourceControl. Description of compile/install
110 * procedure moved to ir-tcl.sgml.
112 * Revision 1.46 1995/06/22 13:15:06 adam
113 * Feature: SUTRS. Setting getSutrs implemented.
114 * Work on display formats.
115 * Preferred record syntax can be set by the user.
117 * Revision 1.45 1995/06/20 08:07:30 adam
118 * New setting: failInfo.
119 * Working on better cancel mechanism.
121 * Revision 1.44 1995/06/19 17:01:20 adam
124 * Revision 1.43 1995/06/19 13:06:08 adam
125 * New define: IR_TCL_VERSION.
127 * Revision 1.42 1995/06/19 08:08:52 adam
128 * client.tcl: hotTargets now contain both database and target name.
129 * ir-tcl.c: setting protocol edited. Errors in callbacks are logged
130 * by logf(LOG_WARN, ...) calls.
132 * Revision 1.41 1995/06/16 12:28:16 adam
133 * Implemented preferredRecordSyntax.
134 * Minor changes in diagnostic handling.
135 * Record list deleted when connection closes.
137 * Revision 1.40 1995/06/14 13:37:18 adam
138 * Setting recordType implemented.
139 * Setting implementationVersion implemented.
140 * Settings implementationId / implementationName edited.
142 * Revision 1.39 1995/06/08 10:26:32 adam
143 * Bug fix in ir_strdup.
145 * Revision 1.38 1995/06/01 16:36:47 adam
146 * About buttons. Minor bug fixes.
148 * Revision 1.37 1995/06/01 07:31:20 adam
149 * Rename of many typedefs -> IrTcl_...
151 * Revision 1.36 1995/05/31 13:09:59 adam
152 * Client searches/presents may be interrupted.
153 * New moving book-logo.
155 * Revision 1.35 1995/05/31 08:36:33 adam
156 * Bug fix in client.tcl: didn't save options on clientrc.tcl.
157 * New method: referenceId. More work on scan.
159 * Revision 1.34 1995/05/29 10:33:42 adam
160 * README and rename of startup script.
162 * Revision 1.33 1995/05/29 09:15:11 quinn
163 * Changed CS_SR to PROTO_SR, etc.
165 * Revision 1.32 1995/05/29 08:44:16 adam
166 * Work on delete of objects.
168 * Revision 1.31 1995/05/26 11:44:10 adam
169 * Bugs fixed. More work on MARC utilities and queries. Test
170 * client is up-to-date again.
172 * Revision 1.30 1995/05/26 08:54:11 adam
173 * New MARC utilities. Uses prefix query.
175 * Revision 1.29 1995/05/24 14:10:22 adam
176 * Work on idAuthentication, protocolVersion and options.
178 * Revision 1.28 1995/05/23 15:34:48 adam
179 * Many new settings, userInformationField, smallSetUpperBound, etc.
180 * A number of settings are inherited when ir-set is executed.
181 * This version is incompatible with the graphical test client (client.tcl).
183 * Revision 1.27 1995/05/11 15:34:47 adam
184 * Scan request changed a bit. This version works with RLG.
186 * Revision 1.26 1995/04/18 16:11:51 adam
187 * First version of graphical Scan. Some work on query-by-form.
189 * Revision 1.25 1995/04/17 09:37:17 adam
190 * Further development of scan.
192 * Revision 1.24 1995/04/11 14:16:42 adam
193 * Further work on scan. Response works. Entries aren't saved yet.
195 * Revision 1.23 1995/04/10 10:50:27 adam
196 * Result-set name defaults to suffix of ir-set name.
197 * Started working on scan. Not finished at this point.
199 * Revision 1.22 1995/03/31 10:43:03 adam
200 * More robust when getting bad MARC records.
202 * Revision 1.21 1995/03/31 08:56:37 adam
203 * New button "Search".
205 * Revision 1.20 1995/03/29 16:07:09 adam
206 * Bug fix: Didn't use setName in present request.
208 * Revision 1.19 1995/03/28 12:45:23 adam
209 * New ir method failback: called on disconnect/protocol error.
210 * New ir set/get method: protocol: SR / Z3950.
211 * Simple popup and disconnect when failback is invoked.
213 * Revision 1.18 1995/03/21 15:50:12 adam
216 * Revision 1.17 1995/03/21 13:41:03 adam
217 * Comstack cs_create not used too often. Non-blocking connect.
219 * Revision 1.16 1995/03/21 08:26:06 adam
220 * New method, setName, to specify the result set name (other than Default).
221 * New method, responseStatus, which returns diagnostic info, if any, after
222 * present response / search response.
224 * Revision 1.15 1995/03/20 15:24:07 adam
225 * Diagnostic records saved on searchResponse.
227 * Revision 1.14 1995/03/20 08:53:22 adam
228 * Event loop in tclmain.c rewritten. New method searchStatus.
230 * Revision 1.13 1995/03/17 18:26:17 adam
231 * Non-blocking i/o used now. Database names popup as cascade items.
233 * Revision 1.12 1995/03/17 15:45:00 adam
234 * Improved target/database setup.
236 * Revision 1.11 1995/03/16 17:54:03 adam
237 * Minor changes really.
239 * Revision 1.10 1995/03/15 16:14:50 adam
240 * Blocking arg in cs_create changed.
242 * Revision 1.9 1995/03/15 13:59:24 adam
245 * Revision 1.8 1995/03/15 08:25:16 adam
246 * New method presentStatus to check for error on present. Misc. cleanup
247 * of IrTcl_RecordList manipulations. Full MARC record presentation in
250 * Revision 1.7 1995/03/14 17:32:29 adam
251 * Presentation of full Marc record in popup window.
253 * Revision 1.6 1995/03/12 19:31:55 adam
254 * Pattern matching implemented when retrieving MARC records. More
255 * diagnostic functions.
257 * Revision 1.5 1995/03/10 18:00:15 adam
258 * Actual presentation in line-by-line format. RPN query support.
260 * Revision 1.4 1995/03/09 16:15:08 adam
261 * First presentRequest attempts. Hot-target list.
278 int (*method) (void *obj, Tcl_Interp *interp, int argc, char **argv);
286 static void ir_deleteDiags (IrTcl_Diagnostic **dst_list, int *dst_num);
287 static int do_disconnect (void *obj, Tcl_Interp *interp,
288 int argc, char **argv);
290 static IrTcl_RecordList *new_IR_record (IrTcl_SetObj *setobj,
292 const char *elements)
294 IrTcl_RecordList *rl;
296 for (rl = setobj->record_list; rl; rl = rl->next)
298 if (no == rl->no && (!rl->elements || !elements ||
299 !strcmp(elements, rl->elements)))
304 case Z_NamePlusRecord_databaseRecord:
305 free (rl->u.dbrec.buf);
306 rl->u.dbrec.buf = NULL;
308 case Z_NamePlusRecord_surrogateDiagnostic:
309 ir_deleteDiags (&rl->u.surrogateDiagnostics.list,
310 &rl->u.surrogateDiagnostics.num);
318 rl = ir_tcl_malloc (sizeof(*rl));
319 rl->next = setobj->record_list;
321 setobj->record_list = rl;
324 ir_tcl_strdup (NULL, &rl->elements, elements);
329 enum oid_value value;
331 } IrTcl_recordSyntaxTab[] = {
332 { VAL_UNIMARC, "UNIMARC" },
333 { VAL_INTERMARC, "INTERMARC" },
335 { VAL_USMARC, "USMARC" },
336 { VAL_UKMARC, "UKMARC" },
337 { VAL_NORMARC, "NORMARC" },
338 { VAL_LIBRISMARC, "LIBRISMARC" },
339 { VAL_DANMARC, "DANMARC" },
340 { VAL_FINMARC, "FINMARC" },
342 { VAL_CANMARC, "CANMARC" },
344 { VAL_PICAMARC, "PICAMARC" },
345 { VAL_AUSMARC, "AUSMARC" },
346 { VAL_IBERMARC, "IBERMARC" },
347 { VAL_SUTRS, "SUTRS" },
348 { VAL_GRS1, "GRS1" },
355 int IrTcl_eval (Tcl_Interp *interp, const char *command)
357 char *tmp = ir_tcl_malloc (strlen(command)+1);
360 strcpy (tmp, command);
361 r = Tcl_Eval (interp, tmp);
363 logf (LOG_WARN, "Tcl error in line %d: %s", interp->errorLine,
365 Tcl_FreeResult (interp);
371 * IrTcl_getRecordSyntaxStr: Return record syntax name of object id
373 static const char *IrTcl_getRecordSyntaxStr (enum oid_value value)
376 for (i = 0; IrTcl_recordSyntaxTab[i].name; i++)
377 if (IrTcl_recordSyntaxTab[i].value == value)
378 return IrTcl_recordSyntaxTab[i].name;
383 * IrTcl_getRecordSyntaxVal: Return record syntax value of string
385 static enum oid_value IrTcl_getRecordSyntaxVal (const char *name)
388 for (i = 0; IrTcl_recordSyntaxTab[i].name; i++)
389 if (!strcmp (IrTcl_recordSyntaxTab[i].name, name))
390 return IrTcl_recordSyntaxTab[i].value;
394 static IrTcl_RecordList *find_IR_record (IrTcl_SetObj *setobj, int no)
396 IrTcl_RecordList *rl;
398 for (rl = setobj->record_list; rl; rl = rl->next)
400 (!setobj->recordElements || !rl->elements ||
401 !strcmp (setobj->recordElements, rl->elements)))
406 static void delete_IR_records (IrTcl_SetObj *setobj)
408 IrTcl_RecordList *rl, *rl1;
410 for (rl = setobj->record_list; rl; rl = rl1)
414 case Z_NamePlusRecord_databaseRecord:
415 free (rl->u.dbrec.buf);
417 case Z_NamePlusRecord_surrogateDiagnostic:
418 ir_deleteDiags (&rl->u.surrogateDiagnostics.list,
419 &rl->u.surrogateDiagnostics.num);
425 setobj->record_list = NULL;
429 * get_set_int: Set/get integer value
431 static int get_set_int (int *val, Tcl_Interp *interp, int argc, char **argv)
437 if (Tcl_GetInt (interp, argv[2], val)==TCL_ERROR)
440 sprintf (buf, "%d", *val);
441 Tcl_AppendResult (interp, buf, NULL);
446 * ir_method: Search for method in table and invoke method handler
448 int ir_method (Tcl_Interp *interp, int argc, char **argv, IrTcl_Methods *tab)
450 IrTcl_Methods *tab_i = tab;
453 for (tab_i = tab; tab_i->tab; tab_i++)
454 for (t = tab_i->tab; t->name; t++)
457 if ((*t->method)(tab_i->obj, interp, argc, argv) == TCL_ERROR)
461 if (!strcmp (t->name, argv[1]))
462 return (*t->method)(tab_i->obj, interp, argc, argv);
466 Tcl_AppendResult (interp, "Bad method: ", argv[1],
467 ". Possible methods:", NULL);
468 for (tab_i = tab; tab_i->tab; tab_i++)
469 for (t = tab_i->tab; t->name; t++)
470 Tcl_AppendResult (interp, " ", t->name, NULL);
475 * ir_method_r: Get status for all readable elements
477 int ir_method_r (void *obj, Tcl_Interp *interp, int argc, char **argv,
485 for (; tab->name; tab++)
488 argv_n[1] = tab->name;
489 Tcl_AppendResult (interp, "{", NULL);
490 (*tab->method)(obj, interp, argc_n, argv_n);
491 Tcl_AppendResult (interp, "} ", NULL);
497 * ir_named_bits: get/set named bits
499 int ir_named_bits (struct ir_named_entry *tab, Odr_bitmask *ob,
500 Tcl_Interp *interp, int argc, char **argv)
502 struct ir_named_entry *ti;
507 for (no = 0; no < argc; no++)
509 for (ti = tab; ti->name; ti++)
510 if (!strcmp (argv[no], ti->name))
512 ODR_MASK_SET (ob, ti->pos);
517 Tcl_AppendResult (interp, "Bad bit mask: ", argv[no], NULL);
523 for (ti = tab; ti->name; ti++)
524 if (ODR_MASK_GET (ob, ti->pos))
525 Tcl_AppendElement (interp, ti->name);
529 static void set_referenceId (ODR o, Z_ReferenceId **dst, const char *src)
535 *dst = odr_malloc (o, sizeof(**dst));
536 (*dst)->size = (*dst)->len = strlen(src);
537 (*dst)->buf = odr_malloc (o, (*dst)->len);
538 memcpy ((*dst)->buf, src, (*dst)->len);
542 static void get_referenceId (char **dst, Z_ReferenceId *src)
550 *dst = ir_tcl_malloc (src->len+1);
551 memcpy (*dst, src->buf, src->len);
552 (*dst)[src->len] = '\0';
555 /* ------------------------------------------------------- */
558 * do_init_request: init method on IR object
560 static int do_init_request (void *obj, Tcl_Interp *interp,
561 int argc, char **argv)
571 interp->result = "init: not connected";
574 apdu = zget_APDU (p->odr_out, Z_APDU_initRequest);
575 req = apdu->u.initRequest;
577 set_referenceId (p->odr_out, &req->referenceId, p->set_inher.referenceId);
578 req->options = &p->options;
579 req->protocolVersion = &p->protocolVersion;
580 req->preferredMessageSize = &p->preferredMessageSize;
581 req->maximumRecordSize = &p->maximumRecordSize;
583 if (p->idAuthenticationGroupId)
585 Z_IdPass *pass = odr_malloc (p->odr_out, sizeof(*pass));
586 Z_IdAuthentication *auth = odr_malloc (p->odr_out, sizeof(*auth));
588 logf (LOG_DEBUG, "using pass authentication");
590 auth->which = Z_IdAuthentication_idPass;
591 auth->u.idPass = pass;
592 if (p->idAuthenticationGroupId && *p->idAuthenticationGroupId)
593 pass->groupId = p->idAuthenticationGroupId;
595 pass->groupId = NULL;
596 if (p->idAuthenticationUserId && *p->idAuthenticationUserId)
597 pass->userId = p->idAuthenticationUserId;
600 if (p->idAuthenticationPassword && *p->idAuthenticationPassword)
601 pass->password = p->idAuthenticationPassword;
603 pass->password = NULL;
604 req->idAuthentication = auth;
606 else if (!p->idAuthenticationOpen || !*p->idAuthenticationOpen)
607 req->idAuthentication = NULL;
610 Z_IdAuthentication *auth = odr_malloc (p->odr_out, sizeof(*auth));
612 logf (LOG_DEBUG, "using open authentication");
613 auth->which = Z_IdAuthentication_open;
614 auth->u.open = p->idAuthenticationOpen;
615 req->idAuthentication = auth;
617 req->implementationId = p->implementationId;
618 req->implementationName = p->implementationName;
619 req->implementationVersion = p->implementationVersion;
620 req->userInformationField = 0;
622 return ir_tcl_send_APDU (interp, p, apdu, "init", *argv);
626 * do_protocolVersion: Set protocol Version
628 static int do_protocolVersion (void *obj, Tcl_Interp *interp,
629 int argc, char **argv)
637 ODR_MASK_ZERO (&p->protocolVersion);
638 ODR_MASK_SET (&p->protocolVersion, 0);
639 ODR_MASK_SET (&p->protocolVersion, 1);
644 if (Tcl_GetInt (interp, argv[2], &version)==TCL_ERROR)
646 ODR_MASK_ZERO (&p->protocolVersion);
647 for (i = 0; i<version; i++)
648 ODR_MASK_SET (&p->protocolVersion, i);
650 for (i = 4; --i >= 0; )
651 if (ODR_MASK_GET (&p->protocolVersion, i))
653 sprintf (buf, "%d", i+1);
654 interp->result = buf;
659 * do_options: Set options
661 static int do_options (void *obj, Tcl_Interp *interp,
662 int argc, char **argv)
664 static struct ir_named_entry options_tab[] = {
668 { "resourceReport", 3 },
669 { "triggerResourceCtrl", 4},
670 { "resourceCtrl", 5},
674 { "extendedServices", 10},
675 { "level-1Segmentation", 11},
676 { "level-2Segmentation", 12},
677 { "concurrentOperations", 13},
678 { "namedResultSets", 14},
685 ODR_MASK_ZERO (&p->options);
686 ODR_MASK_SET (&p->options, 0);
687 ODR_MASK_SET (&p->options, 1);
688 ODR_MASK_SET (&p->options, 4);
689 ODR_MASK_SET (&p->options, 7);
690 ODR_MASK_SET (&p->options, 14);
693 return ir_named_bits (options_tab, &p->options, interp, argc-2, argv+2);
697 * do_apduInfo: Get APDU information
699 static int do_apduInfo (void *obj, Tcl_Interp *interp, int argc, char **argv)
707 sprintf (buf, "%d", p->apduLen);
708 Tcl_AppendElement (interp, buf);
709 sprintf (buf, "%d", p->apduOffset);
710 Tcl_AppendElement (interp, buf);
713 Tcl_AppendElement (interp, "");
716 apduf = fopen ("apdu.tmp", "w");
719 Tcl_AppendElement (interp, "");
722 odr_dumpBER (apduf, p->buf_in, p->apduLen);
724 if (!(apduf = fopen ("apdu.tmp", "r")))
725 Tcl_AppendElement (interp, "");
730 Tcl_AppendResult (interp, " {", NULL);
731 while ((c = getc (apduf)) != EOF)
735 Tcl_AppendResult (interp, buf, NULL);
738 Tcl_AppendResult (interp, "}", NULL);
745 * do_failInfo: Get fail information
747 static int do_failInfo (void *obj, Tcl_Interp *interp, int argc, char **argv)
757 sprintf (buf, "%d", p->failInfo);
763 case IR_TCL_FAIL_CONNECT:
764 cp = "connect failed";
766 case IR_TCL_FAIL_READ:
767 cp = "connection closed";
769 case IR_TCL_FAIL_WRITE:
770 cp = "connection closed";
772 case IR_TCL_FAIL_IN_APDU:
773 cp = "failed to decode incoming APDU";
775 case IR_TCL_FAIL_UNKNOWN_APDU:
781 Tcl_AppendElement (interp, buf);
782 Tcl_AppendElement (interp, cp);
787 * do_preferredMessageSize: Set/get preferred message size
789 static int do_preferredMessageSize (void *obj, Tcl_Interp *interp,
790 int argc, char **argv)
796 p->preferredMessageSize = 30000;
799 return get_set_int (&p->preferredMessageSize, interp, argc, argv);
803 * do_maximumRecordSize: Set/get maximum record size
805 static int do_maximumRecordSize (void *obj, Tcl_Interp *interp,
806 int argc, char **argv)
812 p->maximumRecordSize = 30000;
815 return get_set_int (&p->maximumRecordSize, interp, argc, argv);
819 * do_initResult: Get init result
821 static int do_initResult (void *obj, Tcl_Interp *interp,
822 int argc, char **argv)
828 return get_set_int (&p->initResult, interp, argc, argv);
833 * do_implementationName: Set/get Implementation Name.
835 static int do_implementationName (void *obj, Tcl_Interp *interp,
836 int argc, char **argv)
841 return ir_tcl_strdup (interp, &p->implementationName,
842 "Index Data/IrTcl on YAZ");
844 return ir_tcl_strdel (interp, &p->implementationName);
847 free (p->implementationName);
848 if (ir_tcl_strdup (interp, &p->implementationName, argv[2])
852 Tcl_AppendResult (interp, p->implementationName, (char*) NULL);
857 * do_implementationId: Get Implementation Id.
859 static int do_implementationId (void *obj, Tcl_Interp *interp,
860 int argc, char **argv)
865 return ir_tcl_strdup (interp, &p->implementationId, "YAZ (id=81)");
867 return ir_tcl_strdel (interp, &p->implementationId);
868 Tcl_AppendResult (interp, p->implementationId, (char*) NULL);
873 * do_implementationVersion: get Implementation Version.
875 static int do_implementationVersion (void *obj, Tcl_Interp *interp,
876 int argc, char **argv)
881 return ir_tcl_strdup (interp, &p->implementationVersion,
882 "YAZ: " YAZ_VERSION " / IrTcl: " IR_TCL_VERSION);
884 return ir_tcl_strdel (interp, &p->implementationVersion);
885 Tcl_AppendResult (interp, p->implementationVersion, (char*) NULL);
890 * do_targetImplementationName: Get Implementation Name of target.
892 static int do_targetImplementationName (void *obj, Tcl_Interp *interp,
893 int argc, char **argv)
899 p->targetImplementationName = NULL;
903 return ir_tcl_strdel (interp, &p->targetImplementationName);
904 Tcl_AppendResult (interp, p->targetImplementationName, (char*) NULL);
909 * do_targetImplementationId: Get Implementation Id of target
911 static int do_targetImplementationId (void *obj, Tcl_Interp *interp,
912 int argc, char **argv)
918 p->targetImplementationId = NULL;
922 return ir_tcl_strdel (interp, &p->targetImplementationId);
923 Tcl_AppendResult (interp, p->targetImplementationId, (char*) NULL);
928 * do_targetImplementationVersion: Get Implementation Version of target
930 static int do_targetImplementationVersion (void *obj, Tcl_Interp *interp,
931 int argc, char **argv)
937 p->targetImplementationVersion = NULL;
941 return ir_tcl_strdel (interp, &p->targetImplementationVersion);
942 Tcl_AppendResult (interp, p->targetImplementationVersion, (char*) NULL);
947 * do_idAuthentication: Set/get id Authentication
949 static int do_idAuthentication (void *obj, Tcl_Interp *interp,
950 int argc, char **argv)
954 if (argc >= 3 || argc == -1)
956 free (p->idAuthenticationOpen);
957 free (p->idAuthenticationGroupId);
958 free (p->idAuthenticationUserId);
959 free (p->idAuthenticationPassword);
961 if (argc >= 3 || argc <= 0)
963 p->idAuthenticationOpen = NULL;
964 p->idAuthenticationGroupId = NULL;
965 p->idAuthenticationUserId = NULL;
966 p->idAuthenticationPassword = NULL;
975 ir_tcl_strdup (interp, &p->idAuthenticationOpen, argv[2])
982 ir_tcl_strdup (interp, &p->idAuthenticationGroupId, argv[2])
986 ir_tcl_strdup (interp, &p->idAuthenticationUserId, argv[3])
990 ir_tcl_strdup (interp, &p->idAuthenticationPassword, argv[4])
995 if (p->idAuthenticationOpen)
996 Tcl_AppendElement (interp, p->idAuthenticationOpen);
997 else if (p->idAuthenticationGroupId)
999 Tcl_AppendElement (interp, p->idAuthenticationGroupId);
1000 Tcl_AppendElement (interp, p->idAuthenticationUserId);
1001 Tcl_AppendElement (interp, p->idAuthenticationPassword);
1007 * do_connect: connect method on IR object
1009 static int do_connect (void *obj, Tcl_Interp *interp,
1010 int argc, char **argv)
1022 interp->result = "already connected";
1025 if (ir_tcl_strdup (interp, &p->hostname, argv[2]) == TCL_ERROR)
1027 if (!strcmp (p->comstackType, "tcpip"))
1029 p->cs_link = cs_create (tcpip_type, CS_BLOCK, p->protocol_type);
1030 addr = tcpip_strtoaddr (argv[2]);
1033 interp->result = "tcpip_strtoaddr fail";
1036 logf (LOG_DEBUG, "tcp/ip connect %s", argv[2]);
1038 else if (!strcmp (p->comstackType, "mosi"))
1041 p->cs_link = cs_create (mosi_type, CS_BLOCK, p->protocol_type);
1042 addr = mosi_strtoaddr (argv[2]);
1045 interp->result = "mosi_strtoaddr fail";
1048 logf (LOG_DEBUG, "mosi connect %s", argv[2]);
1050 interp->result = "MOSI support not there";
1056 Tcl_AppendResult (interp, "Bad comstack type: ",
1057 p->comstackType, NULL);
1060 #if IRTCL_GENERIC_FILES
1062 p->csFile = Tcl_GetFile (cs_fileno(p->cs_link), TCL_WIN_SOCKET);
1064 p->csFile = Tcl_GetFile (cs_fileno(p->cs_link), TCL_UNIX_FD);
1067 if ((r=cs_connect (p->cs_link, addr)) < 0)
1069 interp->result = "connect fail";
1070 do_disconnect (p, NULL, 2, NULL);
1073 logf(LOG_DEBUG, "cs_connect() returned %d", r);
1074 p->eventType = "connect";
1075 #if IRTCL_GENERIC_FILES
1076 ir_select_add (p->csFile, p);
1078 ir_select_add (cs_fileno (p->cs_link), p);
1082 #if IRTCL_GENERIC_FILES
1083 ir_select_add_write (p->csFile, p);
1085 ir_select_add_write (cs_fileno (p->cs_link), p);
1087 p->state = IR_TCL_R_Connecting;
1091 p->state = IR_TCL_R_Idle;
1093 IrTcl_eval (p->interp, p->callback);
1097 Tcl_AppendResult (interp, p->hostname, NULL);
1102 * do_disconnect: disconnect method on IR object
1104 static int do_disconnect (void *obj, Tcl_Interp *interp,
1105 int argc, char **argv)
1111 p->state = IR_TCL_R_Idle;
1112 p->eventType = NULL;
1115 #if IRTCL_GENERIC_FILES
1124 #if IRTCL_GENERIC_FILES
1125 ir_select_remove_write (p->csFile, p);
1126 ir_select_remove (p->csFile, p);
1128 ir_select_remove_write (cs_fileno (p->cs_link), p);
1129 ir_select_remove (cs_fileno (p->cs_link), p);
1132 odr_reset (p->odr_in);
1134 assert (p->cs_link);
1135 logf(LOG_DEBUG, "Closing connection");
1136 cs_close (p->cs_link);
1138 #if IRTCL_GENERIC_FILES
1139 Tcl_FreeFile (p->csFile);
1143 ODR_MASK_ZERO (&p->options);
1144 ODR_MASK_SET (&p->options, 0);
1145 ODR_MASK_SET (&p->options, 1);
1146 ODR_MASK_SET (&p->options, 4);
1147 ODR_MASK_SET (&p->options, 7);
1148 ODR_MASK_SET (&p->options, 14);
1150 ODR_MASK_ZERO (&p->protocolVersion);
1151 ODR_MASK_SET (&p->protocolVersion, 0);
1152 ODR_MASK_SET (&p->protocolVersion, 1);
1155 assert (!p->cs_link);
1160 * do_comstack: Set/get comstack method on IR object
1162 static int do_comstack (void *o, Tcl_Interp *interp,
1163 int argc, char **argv)
1168 return ir_tcl_strdup (interp, &obj->comstackType, "tcpip");
1169 else if (argc == -1)
1170 return ir_tcl_strdel (interp, &obj->comstackType);
1173 free (obj->comstackType);
1174 if (ir_tcl_strdup (interp, &obj->comstackType, argv[2]) == TCL_ERROR)
1177 Tcl_AppendElement (interp, obj->comstackType);
1182 * do_logLevel: Set log level
1184 static int do_logLevel (void *o, Tcl_Interp *interp,
1185 int argc, char **argv)
1190 log_init (log_mask_str (argv[2]), "", NULL);
1192 log_init (log_mask_str (argv[2]), argv[3], NULL);
1194 log_init (log_mask_str (argv[2]), argv[3], argv[4]);
1200 * do_eventType: Return type of last event
1202 static int do_eventType (void *obj, Tcl_Interp *interp,
1203 int argc, char **argv)
1209 p->eventType = NULL;
1212 Tcl_AppendElement (interp, p->eventType ? p->eventType : "");
1218 * do_callback: add callback
1220 static int do_callback (void *obj, Tcl_Interp *interp,
1221 int argc, char **argv)
1230 else if (argc == -1)
1231 return ir_tcl_strdel (interp, &p->callback);
1237 if (ir_tcl_strdup (interp, &p->callback, argv[2]) == TCL_ERROR)
1247 * do_failback: add error handle callback
1249 static int do_failback (void *obj, Tcl_Interp *interp,
1250 int argc, char **argv)
1259 else if (argc == -1)
1260 return ir_tcl_strdel (interp, &p->failback);
1266 if (ir_tcl_strdup (interp, &p->failback, argv[2]) == TCL_ERROR)
1276 * do_initResponse: add init response handler
1278 static int do_initResponse (void *obj, Tcl_Interp *interp,
1279 int argc, char **argv)
1285 p->initResponse = NULL;
1288 else if (argc == -1)
1289 return ir_tcl_strdel (interp, &p->initResponse);
1292 free (p->initResponse);
1295 if (ir_tcl_strdup (interp, &p->initResponse, argv[2]) == TCL_ERROR)
1299 p->initResponse = NULL;
1304 * do_protocol: Set/get protocol method on IR object
1306 static int do_protocol (void *o, Tcl_Interp *interp, int argc, char **argv)
1312 p->protocol_type = PROTO_Z3950;
1317 if (!strcmp (argv[2], "Z39"))
1318 p->protocol_type = PROTO_Z3950;
1319 else if (!strcmp (argv[2], "SR"))
1320 p->protocol_type = PROTO_SR;
1323 Tcl_AppendResult (interp, "Bad protocol: ", argv[2], NULL);
1328 switch (p->protocol_type)
1331 Tcl_AppendElement (interp, "Z39");
1334 Tcl_AppendElement (interp, "SR");
1341 * do_triggerResourceControl:
1343 static int do_triggerResourceControl (void *obj, Tcl_Interp *interp,
1344 int argc, char **argv)
1348 Z_TriggerResourceControlRequest *req;
1349 bool_t is_false = 0;
1355 interp->result = "triggerResourceControl: not connected";
1358 apdu = zget_APDU (p->odr_out, Z_APDU_triggerResourceControlRequest);
1359 req = apdu->u.triggerResourceControlRequest;
1360 *req->requestedAction = Z_TriggerResourceCtrl_cancel;
1361 req->resultSetWanted = &is_false;
1363 return ir_tcl_send_APDU (interp, p, apdu, "triggerResourceControl",
1368 * do_databaseNames: specify database names
1370 static int do_databaseNames (void *obj, Tcl_Interp *interp,
1371 int argc, char **argv)
1374 IrTcl_SetCObj *p = obj;
1378 for (i=0; i<p->num_databaseNames; i++)
1379 free (p->databaseNames[i]);
1380 free (p->databaseNames);
1384 p->num_databaseNames = 0;
1385 p->databaseNames = NULL;
1390 for (i=0; i<p->num_databaseNames; i++)
1391 Tcl_AppendElement (interp, p->databaseNames[i]);
1394 if (p->databaseNames)
1396 for (i=0; i<p->num_databaseNames; i++)
1397 free (p->databaseNames[i]);
1398 free (p->databaseNames);
1400 p->num_databaseNames = argc - 2;
1402 ir_tcl_malloc (sizeof(*p->databaseNames) * p->num_databaseNames);
1403 for (i=0; i<p->num_databaseNames; i++)
1405 if (ir_tcl_strdup (interp, &p->databaseNames[i], argv[2+i])
1413 * do_replaceIndicator: Set/get replace Set indicator
1415 static int do_replaceIndicator (void *obj, Tcl_Interp *interp,
1416 int argc, char **argv)
1418 IrTcl_SetCObj *p = obj;
1422 p->replaceIndicator = 1;
1425 return get_set_int (&p->replaceIndicator, interp, argc, argv);
1429 * do_queryType: Set/Get query method
1431 static int do_queryType (void *obj, Tcl_Interp *interp,
1432 int argc, char **argv)
1434 IrTcl_SetCObj *p = obj;
1437 return ir_tcl_strdup (interp, &p->queryType, "rpn");
1438 else if (argc == -1)
1439 return ir_tcl_strdel (interp, &p->queryType);
1442 free (p->queryType);
1443 if (ir_tcl_strdup (interp, &p->queryType, argv[2]) == TCL_ERROR)
1446 Tcl_AppendResult (interp, p->queryType, NULL);
1451 * do_userInformationField: Get User information field
1453 static int do_userInformationField (void *obj, Tcl_Interp *interp,
1454 int argc, char **argv)
1460 p->userInformationField = NULL;
1463 else if (argc == -1)
1464 return ir_tcl_strdel (interp, &p->userInformationField);
1465 Tcl_AppendResult (interp, p->userInformationField, NULL);
1470 * do_smallSetUpperBound: Set/get small set upper bound
1472 static int do_smallSetUpperBound (void *o, Tcl_Interp *interp,
1473 int argc, char **argv)
1475 IrTcl_SetCObj *p = o;
1479 p->smallSetUpperBound = 0;
1482 return get_set_int (&p->smallSetUpperBound, interp, argc, argv);
1486 * do_largeSetLowerBound: Set/get large set lower bound
1488 static int do_largeSetLowerBound (void *o, Tcl_Interp *interp,
1489 int argc, char **argv)
1491 IrTcl_SetCObj *p = o;
1495 p->largeSetLowerBound = 2;
1498 return get_set_int (&p->largeSetLowerBound, interp, argc, argv);
1502 * do_mediumSetPresentNumber: Set/get large set lower bound
1504 static int do_mediumSetPresentNumber (void *o, Tcl_Interp *interp,
1505 int argc, char **argv)
1507 IrTcl_SetCObj *p = o;
1511 p->mediumSetPresentNumber = 0;
1514 return get_set_int (&p->mediumSetPresentNumber, interp, argc, argv);
1518 * do_referenceId: Set/Get referenceId
1520 static int do_referenceId (void *obj, Tcl_Interp *interp,
1521 int argc, char **argv)
1523 IrTcl_SetCObj *p = obj;
1527 p->referenceId = NULL;
1530 else if (argc == -1)
1531 return ir_tcl_strdel (interp, &p->referenceId);
1534 free (p->referenceId);
1535 if (ir_tcl_strdup (interp, &p->referenceId, argv[2]) == TCL_ERROR)
1538 Tcl_AppendResult (interp, p->referenceId, NULL);
1543 * do_preferredRecordSyntax: Set/get preferred record syntax
1545 static int do_preferredRecordSyntax (void *obj, Tcl_Interp *interp,
1546 int argc, char **argv)
1548 IrTcl_SetCObj *p = obj;
1552 p->preferredRecordSyntax = NULL;
1555 else if (argc == -1)
1557 free (p->preferredRecordSyntax);
1558 p->preferredRecordSyntax = NULL;
1563 free (p->preferredRecordSyntax);
1564 p->preferredRecordSyntax = NULL;
1565 if (argv[2][0] && (p->preferredRecordSyntax =
1566 ir_tcl_malloc (sizeof(*p->preferredRecordSyntax))))
1567 *p->preferredRecordSyntax = IrTcl_getRecordSyntaxVal (argv[2]);
1574 * do_elementSetNames: Set/Get element Set Names
1576 static int do_elementSetNames (void *obj, Tcl_Interp *interp,
1577 int argc, char **argv)
1579 IrTcl_SetCObj *p = obj;
1583 p->elementSetNames = NULL;
1586 else if (argc == -1)
1587 return ir_tcl_strdel (interp, &p->elementSetNames);
1590 free (p->elementSetNames);
1591 if (ir_tcl_strdup (interp, &p->elementSetNames, argv[2]) == TCL_ERROR)
1594 Tcl_AppendResult (interp, p->elementSetNames, NULL);
1599 * do_smallSetElementSetNames: Set/Get small Set Element Set Names
1601 static int do_smallSetElementSetNames (void *obj, Tcl_Interp *interp,
1602 int argc, char **argv)
1604 IrTcl_SetCObj *p = obj;
1608 p->smallSetElementSetNames = NULL;
1611 else if (argc == -1)
1612 return ir_tcl_strdel (interp, &p->smallSetElementSetNames);
1615 free (p->smallSetElementSetNames);
1616 if (ir_tcl_strdup (interp, &p->smallSetElementSetNames,
1617 argv[2]) == TCL_ERROR)
1620 Tcl_AppendResult (interp, p->smallSetElementSetNames, NULL);
1625 * do_mediumSetElementSetNames: Set/Get medium Set Element Set Names
1627 static int do_mediumSetElementSetNames (void *obj, Tcl_Interp *interp,
1628 int argc, char **argv)
1630 IrTcl_SetCObj *p = obj;
1634 p->mediumSetElementSetNames = NULL;
1637 else if (argc == -1)
1638 return ir_tcl_strdel (interp, &p->mediumSetElementSetNames);
1641 free (p->mediumSetElementSetNames);
1642 if (ir_tcl_strdup (interp, &p->mediumSetElementSetNames,
1643 argv[2]) == TCL_ERROR)
1646 Tcl_AppendResult (interp, p->mediumSetElementSetNames, NULL);
1650 static IrTcl_Method ir_method_tab[] = {
1651 { 1, "comstack", do_comstack },
1652 { 1, "protocol", do_protocol },
1653 { 0, "failback", do_failback },
1654 { 0, "failInfo", do_failInfo },
1655 { 0, "apduInfo", do_apduInfo },
1656 { 0, "logLevel", do_logLevel },
1658 { 0, "eventType", do_eventType },
1659 { 1, "connect", do_connect },
1660 { 0, "protocolVersion", do_protocolVersion },
1661 { 1, "preferredMessageSize", do_preferredMessageSize },
1662 { 1, "maximumRecordSize", do_maximumRecordSize },
1663 { 1, "implementationName", do_implementationName },
1664 { 1, "implementationId", do_implementationId },
1665 { 1, "implementationVersion", do_implementationVersion },
1666 { 0, "targetImplementationName", do_targetImplementationName },
1667 { 0, "targetImplementationId", do_targetImplementationId },
1668 { 0, "targetImplementationVersion", do_targetImplementationVersion },
1669 { 0, "userInformationField", do_userInformationField },
1670 { 1, "idAuthentication", do_idAuthentication },
1671 { 0, "options", do_options },
1672 { 0, "init", do_init_request },
1673 { 0, "initResult", do_initResult },
1674 { 0, "disconnect", do_disconnect },
1675 { 0, "callback", do_callback },
1676 { 0, "initResponse", do_initResponse },
1677 { 0, "triggerResourceControl", do_triggerResourceControl },
1678 { 0, "initResponse", do_initResponse },
1682 static IrTcl_Method ir_set_c_method_tab[] = {
1683 { 0, "databaseNames", do_databaseNames},
1684 { 0, "replaceIndicator", do_replaceIndicator},
1685 { 0, "queryType", do_queryType },
1686 { 0, "preferredRecordSyntax", do_preferredRecordSyntax },
1687 { 0, "smallSetUpperBound", do_smallSetUpperBound},
1688 { 0, "largeSetLowerBound", do_largeSetLowerBound},
1689 { 0, "mediumSetPresentNumber", do_mediumSetPresentNumber},
1690 { 0, "referenceId", do_referenceId },
1691 { 0, "elementSetNames", do_elementSetNames },
1692 { 0, "smallSetElementSetNames", do_smallSetElementSetNames },
1693 { 0, "mediumSetElementSetNames", do_mediumSetElementSetNames },
1698 * ir_obj_method: IR Object methods
1700 static int ir_obj_method (ClientData clientData, Tcl_Interp *interp,
1701 int argc, char **argv)
1703 IrTcl_Methods tab[3];
1704 IrTcl_Obj *p = clientData;
1707 return ir_method_r (clientData, interp, argc, argv, ir_method_tab);
1709 tab[0].tab = ir_method_tab;
1711 tab[1].tab = ir_set_c_method_tab;
1712 tab[1].obj = &p->set_inher;
1715 return ir_method (interp, argc, argv, tab);
1719 * ir_obj_delete: IR Object disposal
1721 static void ir_obj_delete (ClientData clientData)
1723 IrTcl_Obj *obj = clientData;
1724 IrTcl_Methods tab[3];
1727 if (obj->ref_count > 0)
1729 assert (obj->ref_count == 0);
1731 logf (LOG_DEBUG, "ir object delete");
1732 tab[0].tab = ir_method_tab;
1734 tab[1].tab = ir_set_c_method_tab;
1735 tab[1].obj = &obj->set_inher;
1738 ir_method (NULL, -1, NULL, tab);
1741 odr_destroy (obj->odr_in);
1742 odr_destroy (obj->odr_out);
1743 odr_destroy (obj->odr_pr);
1748 * ir_obj_mk: IR Object creation
1750 static int ir_obj_mk (ClientData clientData, Tcl_Interp *interp,
1751 int argc, char **argv)
1753 IrTcl_Methods tab[3];
1761 interp->result = "wrong # args";
1764 obj = ir_tcl_malloc (sizeof(*obj));
1767 obj->bibset = ccl_qual_mk ();
1768 if ((inf = fopen ("default.bib", "r")))
1770 ccl_qual_file (obj->bibset, inf);
1775 logf (LOG_DEBUG, "ir object create");
1776 obj->odr_in = odr_createmem (ODR_DECODE);
1777 obj->odr_out = odr_createmem (ODR_ENCODE);
1778 obj->odr_pr = odr_createmem (ODR_PRINT);
1779 obj->state = IR_TCL_R_Idle;
1780 obj->interp = interp;
1784 obj->request_queue = NULL;
1786 tab[0].tab = ir_method_tab;
1788 tab[1].tab = ir_set_c_method_tab;
1789 tab[1].obj = &obj->set_inher;
1792 if (ir_method (interp, 0, NULL, tab) == TCL_ERROR)
1794 Tcl_CreateCommand (interp, argv[1], ir_obj_method,
1795 (ClientData) obj, ir_obj_delete);
1799 /* ------------------------------------------------------- */
1801 * do_search: Do search request
1803 static int do_search (void *o, Tcl_Interp *interp, int argc, char **argv)
1805 Z_SearchRequest *req;
1809 IrTcl_SetObj *obj = o;
1820 interp->result = "wrong # args";
1823 if (!obj->set_inher.num_databaseNames)
1825 interp->result = "no databaseNames";
1830 interp->result = "search: not connected";
1833 apdu = zget_APDU (p->odr_out, Z_APDU_searchRequest);
1834 req = apdu->u.searchRequest;
1838 bib1.proto = p->protocol_type;
1839 bib1.oclass = CLASS_ATTSET;
1840 bib1.value = VAL_BIB1;
1842 set_referenceId (p->odr_out, &req->referenceId,
1843 obj->set_inher.referenceId);
1845 req->smallSetUpperBound = &obj->set_inher.smallSetUpperBound;
1846 req->largeSetLowerBound = &obj->set_inher.largeSetLowerBound;
1847 req->mediumSetPresentNumber = &obj->set_inher.mediumSetPresentNumber;
1848 req->replaceIndicator = &obj->set_inher.replaceIndicator;
1849 req->resultSetName = obj->setName ? obj->setName : "Default";
1850 logf (LOG_DEBUG, "Search, resultSetName %s", req->resultSetName);
1851 req->num_databaseNames = obj->set_inher.num_databaseNames;
1852 req->databaseNames = obj->set_inher.databaseNames;
1853 for (r=0; r < obj->set_inher.num_databaseNames; r++)
1854 logf (LOG_DEBUG, " Database %s", obj->set_inher.databaseNames[r]);
1855 if (obj->set_inher.preferredRecordSyntax)
1857 struct oident ident;
1859 ident.proto = p->protocol_type;
1860 ident.oclass = CLASS_RECSYN;
1861 ident.value = *obj->set_inher.preferredRecordSyntax;
1862 logf (LOG_DEBUG, "Preferred record syntax is %d", ident.value);
1863 req->preferredRecordSyntax = odr_oiddup (p->odr_out,
1864 oid_getoidbyent (&ident));
1867 req->preferredRecordSyntax = 0;
1869 if (obj->set_inher.smallSetElementSetNames &&
1870 *obj->set_inher.smallSetElementSetNames)
1872 Z_ElementSetNames *esn = odr_malloc (p->odr_out, sizeof(*esn));
1874 esn->which = Z_ElementSetNames_generic;
1875 esn->u.generic = obj->set_inher.smallSetElementSetNames;
1876 req->smallSetElementSetNames = esn;
1879 req->smallSetElementSetNames = NULL;
1881 if (obj->set_inher.mediumSetElementSetNames &&
1882 *obj->set_inher.mediumSetElementSetNames)
1884 Z_ElementSetNames *esn = odr_malloc (p->odr_out, sizeof(*esn));
1886 esn->which = Z_ElementSetNames_generic;
1887 esn->u.generic = obj->set_inher.mediumSetElementSetNames;
1888 req->mediumSetElementSetNames = esn;
1891 req->mediumSetElementSetNames = NULL;
1893 req->query = &query;
1895 if (!strcmp (obj->set_inher.queryType, "rpn"))
1897 Z_RPNQuery *RPNquery;
1899 RPNquery = p_query_rpn (p->odr_out, argv[2]);
1902 Tcl_AppendResult (interp, "Syntax error in query", NULL);
1905 RPNquery->attributeSetId = oid_getoidbyent (&bib1);
1906 query.which = Z_Query_type_1;
1907 query.u.type_1 = RPNquery;
1908 logf (LOG_DEBUG, "RPN");
1911 else if (!strcmp (obj->set_inher.queryType, "cclrpn"))
1915 struct ccl_rpn_node *rpn;
1916 Z_RPNQuery *RPNquery;
1918 rpn = ccl_find_str(p->bibset, argv[2], &error, &pos);
1921 Tcl_AppendResult (interp, "CCL error: ",
1922 ccl_err_msg(error), NULL);
1925 ccl_pr_tree (rpn, stderr);
1926 fprintf (stderr, "\n");
1927 assert((RPNquery = ccl_rpn_query(rpn)));
1928 RPNquery->attributeSetId = oid_getoidbyent (&bib1);
1929 query.which = Z_Query_type_1;
1930 query.u.type_1 = RPNquery;
1931 logf (LOG_DEBUG, "CCLRPN");
1934 else if (!strcmp (obj->set_inher.queryType, "ccl"))
1936 query.which = Z_Query_type_2;
1937 query.u.type_2 = &ccl_query;
1938 ccl_query.buf = (unsigned char *) argv[2];
1939 ccl_query.len = strlen (argv[2]);
1940 logf (LOG_DEBUG, "CCL");
1944 interp->result = "unknown query method";
1947 return ir_tcl_send_APDU (interp, p, apdu, "search", *argv);
1951 * do_searchResponse: add search response handler
1953 static int do_searchResponse (void *o, Tcl_Interp *interp,
1954 int argc, char **argv)
1956 IrTcl_SetObj *obj = o;
1960 obj->searchResponse = NULL;
1963 else if (argc == -1)
1964 return ir_tcl_strdel (interp, &obj->searchResponse);
1967 free (obj->searchResponse);
1970 if (ir_tcl_strdup (interp, &obj->searchResponse, argv[2])
1975 obj->searchResponse = NULL;
1981 * do_presentResponse: add present response handler
1983 static int do_presentResponse (void *o, Tcl_Interp *interp,
1984 int argc, char **argv)
1986 IrTcl_SetObj *obj = o;
1990 obj->presentResponse = NULL;
1993 else if (argc == -1)
1994 return ir_tcl_strdel (interp, &obj->presentResponse);
1997 free (obj->presentResponse);
2000 if (ir_tcl_strdup (interp, &obj->presentResponse, argv[2])
2005 obj->presentResponse = NULL;
2011 * do_resultCount: Get number of hits
2013 static int do_resultCount (void *o, Tcl_Interp *interp,
2014 int argc, char **argv)
2016 IrTcl_SetObj *obj = o;
2020 return get_set_int (&obj->resultCount, interp, argc, argv);
2024 * do_searchStatus: Get search status (after search response)
2026 static int do_searchStatus (void *o, Tcl_Interp *interp,
2027 int argc, char **argv)
2029 IrTcl_SetObj *obj = o;
2033 return get_set_int (&obj->searchStatus, interp, argc, argv);
2037 * do_presentStatus: Get search status (after search/present response)
2039 static int do_presentStatus (void *o, Tcl_Interp *interp,
2040 int argc, char **argv)
2042 IrTcl_SetObj *obj = o;
2046 return get_set_int (&obj->presentStatus, interp, argc, argv);
2050 * do_nextResultSetPosition: Get next result set position
2051 * (after search/present response)
2053 static int do_nextResultSetPosition (void *o, Tcl_Interp *interp,
2054 int argc, char **argv)
2056 IrTcl_SetObj *obj = o;
2060 obj->nextResultSetPosition = 0;
2063 return get_set_int (&obj->nextResultSetPosition, interp, argc, argv);
2067 * do_setName: Set result Set name
2069 static int do_setName (void *o, Tcl_Interp *interp,
2070 int argc, char **argv)
2072 IrTcl_SetObj *obj = o;
2075 return ir_tcl_strdup (interp, &obj->setName, "Default");
2076 else if (argc == -1)
2077 return ir_tcl_strdel (interp, &obj->setName);
2080 free (obj->setName);
2081 if (ir_tcl_strdup (interp, &obj->setName, argv[2])
2085 Tcl_AppendElement (interp, obj->setName);
2090 * do_numberOfRecordsReturned: Get number of records returned
2092 static int do_numberOfRecordsReturned (void *o, Tcl_Interp *interp,
2093 int argc, char **argv)
2095 IrTcl_SetObj *obj = o;
2099 obj->numberOfRecordsReturned = 0;
2102 return get_set_int (&obj->numberOfRecordsReturned, interp, argc, argv);
2106 * do_type: Return type (if any) at position.
2108 static int do_type (void *o, Tcl_Interp *interp, int argc, char **argv)
2110 IrTcl_SetObj *obj = o;
2112 IrTcl_RecordList *rl;
2116 obj->record_list = NULL;
2119 else if (argc == -1)
2121 delete_IR_records (obj);
2126 sprintf (interp->result, "wrong # args");
2129 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2131 rl = find_IR_record (obj, offset);
2134 logf (LOG_DEBUG, "No record at position %d", offset);
2139 case Z_NamePlusRecord_databaseRecord:
2140 interp->result = "DB";
2142 case Z_NamePlusRecord_surrogateDiagnostic:
2143 interp->result = "SD";
2151 * do_recordType: Return record type (if any) at position.
2153 static int do_recordType (void *o, Tcl_Interp *interp, int argc, char **argv)
2155 IrTcl_SetObj *obj = o;
2157 IrTcl_RecordList *rl;
2163 else if (argc == -1)
2169 sprintf (interp->result, "wrong # args");
2172 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2174 rl = find_IR_record (obj, offset);
2177 if (rl->which != Z_NamePlusRecord_databaseRecord)
2179 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2182 Tcl_AppendElement (interp, (char*)
2183 IrTcl_getRecordSyntaxStr (rl->u.dbrec.type));
2188 * set record elements (for record extraction)
2190 static int do_recordElements (void *o, Tcl_Interp *interp,
2191 int argc, char **argv)
2193 IrTcl_SetObj *obj = o;
2197 obj->recordElements = NULL;
2200 else if (argc == -1)
2201 return ir_tcl_strdel (NULL, &obj->recordElements);
2204 sprintf (interp->result, "wrong # args");
2209 free (obj->recordElements);
2210 return ir_tcl_strdup (NULL, &obj->recordElements,
2211 (*argv[2] ? argv[2] : NULL));
2213 Tcl_AppendResult (interp, obj->recordElements, NULL);
2220 static int ir_diagResult (Tcl_Interp *interp, IrTcl_Diagnostic *list, int num)
2226 for (i = 0; i<num; i++)
2228 logf (LOG_DEBUG, "Diagnostic, code %d", list[i].condition);
2229 sprintf (buf, "%d", list[i].condition);
2230 Tcl_AppendElement (interp, buf);
2231 cp = diagbib1_str (list[i].condition);
2233 Tcl_AppendElement (interp, (char*) cp);
2235 Tcl_AppendElement (interp, "");
2236 if (list[i].addinfo)
2237 Tcl_AppendElement (interp, (char*) list[i].addinfo);
2239 Tcl_AppendElement (interp, "");
2245 * do_diag: Return diagnostic record info
2247 static int do_diag (void *o, Tcl_Interp *interp, int argc, char **argv)
2249 IrTcl_SetObj *obj = o;
2251 IrTcl_RecordList *rl;
2257 sprintf (interp->result, "wrong # args");
2260 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2262 rl = find_IR_record (obj, offset);
2265 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2268 if (rl->which != Z_NamePlusRecord_surrogateDiagnostic)
2270 Tcl_AppendResult (interp, "No Diagnostic record at #", argv[2], NULL);
2273 return ir_diagResult (interp, rl->u.surrogateDiagnostics.list,
2274 rl->u.surrogateDiagnostics.num);
2278 * do_getMarc: Get ISO2709 Record lines/fields
2280 static int do_getMarc (void *o, Tcl_Interp *interp, int argc, char **argv)
2282 IrTcl_SetObj *obj = o;
2284 IrTcl_RecordList *rl;
2290 sprintf (interp->result, "wrong # args");
2293 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2295 rl = find_IR_record (obj, offset);
2298 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2301 if (rl->which != Z_NamePlusRecord_databaseRecord)
2303 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2306 return ir_tcl_get_marc (interp, rl->u.dbrec.buf, argc, argv);
2310 * do_getSutrs: Get SUTRS Record
2312 static int do_getSutrs (void *o, Tcl_Interp *interp, int argc, char **argv)
2314 IrTcl_SetObj *obj = o;
2316 IrTcl_RecordList *rl;
2322 sprintf (interp->result, "wrong # args");
2325 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2327 rl = find_IR_record (obj, offset);
2330 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2333 if (rl->which != Z_NamePlusRecord_databaseRecord)
2335 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2338 if (rl->u.dbrec.type != VAL_SUTRS)
2340 Tcl_AppendElement (interp, rl->u.dbrec.buf);
2346 * do_getGrs: Get a GRS1 Record
2348 static int do_getGrs (void *o, Tcl_Interp *interp, int argc, char **argv)
2350 IrTcl_SetObj *obj = o;
2352 IrTcl_RecordList *rl;
2358 sprintf (interp->result, "wrong # args");
2361 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2363 rl = find_IR_record (obj, offset);
2366 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2369 if (rl->which != Z_NamePlusRecord_databaseRecord)
2371 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2374 if (rl->u.dbrec.type != VAL_GRS1)
2376 return ir_tcl_get_grs (interp, rl->u.dbrec.u.grs1, argc, argv);
2381 * do_responseStatus: Return response status (present or search)
2383 static int do_responseStatus (void *o, Tcl_Interp *interp,
2384 int argc, char **argv)
2386 IrTcl_SetObj *obj = o;
2390 obj->recordFlag = 0;
2391 obj->nonSurrogateDiagnosticNum = 0;
2392 obj->nonSurrogateDiagnosticList = NULL;
2395 else if (argc == -1)
2397 ir_deleteDiags (&obj->nonSurrogateDiagnosticList,
2398 &obj->nonSurrogateDiagnosticNum);
2401 if (!obj->recordFlag)
2403 Tcl_AppendElement (interp, "OK");
2408 case Z_Records_DBOSD:
2409 Tcl_AppendElement (interp, "DBOSD");
2412 Tcl_AppendElement (interp, "NSD");
2413 return ir_diagResult (interp, obj->nonSurrogateDiagnosticList,
2414 obj->nonSurrogateDiagnosticNum);
2420 * do_present: Perform Present Request
2423 static int do_present (void *o, Tcl_Interp *interp, int argc, char **argv)
2425 IrTcl_SetObj *obj = o;
2428 Z_PresentRequest *req;
2436 if (Tcl_GetInt (interp, argv[2], &start) == TCL_ERROR)
2443 if (Tcl_GetInt (interp, argv[3], &number) == TCL_ERROR)
2451 interp->result = "present: not connected";
2456 obj->number = number;
2458 apdu = zget_APDU (p->odr_out, Z_APDU_presentRequest);
2459 req = apdu->u.presentRequest;
2461 set_referenceId (p->odr_out, &req->referenceId,
2462 obj->set_inher.referenceId);
2464 req->resultSetId = obj->setName ? obj->setName : "Default";
2466 req->resultSetStartPoint = &start;
2467 req->numberOfRecordsRequested = &number;
2468 if (obj->set_inher.preferredRecordSyntax)
2470 struct oident ident;
2472 ident.proto = p->protocol_type;
2473 ident.oclass = CLASS_RECSYN;
2474 ident.value = *obj->set_inher.preferredRecordSyntax;
2475 logf (LOG_DEBUG, "Preferred record syntax is %d", ident.value);
2476 req->preferredRecordSyntax = odr_oiddup (p->odr_out,
2477 oid_getoidbyent (&ident));
2480 req->preferredRecordSyntax = 0;
2482 if (obj->set_inher.elementSetNames && *obj->set_inher.elementSetNames)
2484 Z_ElementSetNames *esn = odr_malloc (p->odr_out, sizeof(*esn));
2485 Z_RecordComposition *compo = odr_malloc (p->odr_out, sizeof(*compo));
2487 esn->which = Z_ElementSetNames_generic;
2488 esn->u.generic = obj->set_inher.elementSetNames;
2490 req->recordComposition = compo;
2491 compo->which = Z_RecordComp_simple;
2492 compo->u.simple = esn;
2495 req->recordComposition = NULL;
2496 return ir_tcl_send_APDU (interp, p, apdu, "present", *argv);
2500 * do_loadFile: Load result set from file
2503 static int do_loadFile (void *o, Tcl_Interp *interp,
2504 int argc, char **argv)
2506 IrTcl_SetObj *setobj = o;
2516 interp->result = "wrong # args";
2519 inf = fopen (argv[2], "r");
2522 Tcl_AppendResult (interp, "Cannot open ", argv[2], NULL);
2525 while ((buf = ir_tcl_fread_marc (inf, &size)))
2527 IrTcl_RecordList *rl;
2529 rl = new_IR_record (setobj, no, Z_NamePlusRecord_databaseRecord, "F");
2530 rl->u.dbrec.type = VAL_USMARC;
2531 rl->u.dbrec.buf = buf;
2532 rl->u.dbrec.size = size;
2535 setobj->numberOfRecordsReturned = no-1;
2540 static IrTcl_Method ir_set_method_tab[] = {
2541 { 0, "search", do_search },
2542 { 0, "searchResponse", do_searchResponse },
2543 { 0, "presentResponse", do_presentResponse },
2544 { 0, "searchStatus", do_searchStatus },
2545 { 0, "presentStatus", do_presentStatus },
2546 { 0, "nextResultSetPosition", do_nextResultSetPosition },
2547 { 0, "setName", do_setName },
2548 { 0, "resultCount", do_resultCount },
2549 { 0, "numberOfRecordsReturned", do_numberOfRecordsReturned },
2550 { 0, "present", do_present },
2551 { 0, "type", do_type },
2552 { 0, "getMarc", do_getMarc },
2553 { 0, "getSutrs", do_getSutrs },
2554 { 0, "getGrs", do_getGrs },
2555 { 0, "recordType", do_recordType },
2556 { 0, "recordElements", do_recordElements },
2557 { 0, "diag", do_diag },
2558 { 0, "responseStatus", do_responseStatus },
2559 { 0, "loadFile", do_loadFile },
2564 * ir_set_obj_method: IR Set Object methods
2566 static int ir_set_obj_method (ClientData clientData, Tcl_Interp *interp,
2567 int argc, char **argv)
2569 IrTcl_Methods tabs[3];
2570 IrTcl_SetObj *p = clientData;
2574 interp->result = "wrong # args";
2577 tabs[0].tab = ir_set_method_tab;
2579 tabs[1].tab = ir_set_c_method_tab;
2580 tabs[1].obj = &p->set_inher;
2583 return ir_method (interp, argc, argv, tabs);
2587 * ir_set_obj_delete: IR Set Object disposal
2589 static void ir_set_obj_delete (ClientData clientData)
2591 IrTcl_Methods tabs[3];
2592 IrTcl_SetObj *p = clientData;
2594 logf (LOG_DEBUG, "ir set delete");
2596 tabs[0].tab = ir_set_method_tab;
2598 tabs[1].tab = ir_set_c_method_tab;
2599 tabs[1].obj = &p->set_inher;
2602 ir_method (NULL, -1, NULL, tabs);
2608 * ir_set_obj_mk: IR Set Object creation
2610 static int ir_set_obj_mk (ClientData clientData, Tcl_Interp *interp,
2611 int argc, char **argv)
2613 IrTcl_Methods tabs[3];
2616 if (argc < 2 || argc > 3)
2618 interp->result = "wrong # args";
2621 obj = ir_tcl_malloc (sizeof(*obj));
2622 logf (LOG_DEBUG, "ir set create");
2625 Tcl_CmdInfo parent_info;
2630 if (!Tcl_GetCommandInfo (interp, argv[2], &parent_info))
2632 interp->result = "No parent";
2635 obj->parent = (IrTcl_Obj *) parent_info.clientData;
2637 dst = &obj->set_inher;
2638 src = &obj->parent->set_inher;
2640 if ((dst->num_databaseNames = src->num_databaseNames))
2641 dst->databaseNames =
2642 ir_tcl_malloc (sizeof (*dst->databaseNames)
2643 * dst->num_databaseNames);
2645 dst->databaseNames = NULL;
2646 for (i = 0; i < dst->num_databaseNames; i++)
2647 if (ir_tcl_strdup (interp, &dst->databaseNames[i],
2648 src->databaseNames[i]) == TCL_ERROR)
2650 if (ir_tcl_strdup (interp, &dst->queryType, src->queryType)
2654 if (ir_tcl_strdup (interp, &dst->referenceId, src->referenceId)
2658 if (ir_tcl_strdup (interp, &dst->elementSetNames, src->elementSetNames)
2662 if (ir_tcl_strdup (interp, &dst->smallSetElementSetNames,
2663 src->smallSetElementSetNames)
2667 if (ir_tcl_strdup (interp, &dst->mediumSetElementSetNames,
2668 src->mediumSetElementSetNames)
2672 if (src->preferredRecordSyntax &&
2673 (dst->preferredRecordSyntax
2674 = ir_tcl_malloc (sizeof(*dst->preferredRecordSyntax))))
2675 *dst->preferredRecordSyntax = *src->preferredRecordSyntax;
2677 dst->preferredRecordSyntax = NULL;
2678 dst->replaceIndicator = src->replaceIndicator;
2679 dst->smallSetUpperBound = src->smallSetUpperBound;
2680 dst->largeSetLowerBound = src->largeSetLowerBound;
2681 dst->mediumSetPresentNumber = src->mediumSetPresentNumber;
2686 tabs[0].tab = ir_set_method_tab;
2690 if (ir_method (interp, 0, NULL, tabs) == TCL_ERROR)
2693 Tcl_CreateCommand (interp, argv[1], ir_set_obj_method,
2694 (ClientData) obj, ir_set_obj_delete);
2698 /* ------------------------------------------------------- */
2701 * do_scan: Perform scan
2703 static int do_scan (void *o, Tcl_Interp *interp, int argc, char **argv)
2707 IrTcl_ScanObj *obj = o;
2708 IrTcl_Obj *p = obj->parent;
2711 struct ccl_rpn_node *rpn;
2719 interp->result = "wrong # args";
2722 if (!p->set_inher.num_databaseNames)
2724 interp->result = "no databaseNames";
2729 interp->result = "scan: not connected";
2733 bib1.proto = p->protocol_type;
2734 bib1.oclass = CLASS_ATTSET;
2735 bib1.value = VAL_BIB1;
2737 apdu = zget_APDU (p->odr_out, Z_APDU_scanRequest);
2738 req = apdu->u.scanRequest;
2740 set_referenceId (p->odr_out, &req->referenceId, p->set_inher.referenceId);
2741 req->num_databaseNames = p->set_inher.num_databaseNames;
2742 req->databaseNames = p->set_inher.databaseNames;
2743 req->attributeSet = oid_getoidbyent (&bib1);
2746 if (!(req->termListAndStartPoint = p_query_scan (p->odr_out, argv[2])))
2748 Tcl_AppendResult (interp, "Syntax error in query", NULL);
2752 rpn = ccl_find_str(p->bibset, argv[2], &r, &pos);
2755 Tcl_AppendResult (interp, "CCL error: ", ccl_err_msg (r), NULL);
2758 ccl_pr_tree (rpn, stderr);
2759 fprintf (stderr, "\n");
2760 if (!(req->termListAndStartPoint = ccl_scan_query (rpn)))
2763 req->stepSize = &obj->stepSize;
2764 req->numberOfTermsRequested = &obj->numberOfTermsRequested;
2765 req->preferredPositionInResponse = &obj->preferredPositionInResponse;
2766 logf (LOG_DEBUG, "stepSize=%d", *req->stepSize);
2767 logf (LOG_DEBUG, "numberOfTermsRequested=%d",
2768 *req->numberOfTermsRequested);
2769 logf (LOG_DEBUG, "preferredPositionInResponse=%d",
2770 *req->preferredPositionInResponse);
2772 return ir_tcl_send_APDU (interp, p, apdu, "scan", *argv);
2776 * do_scanResponse: add scan response handler
2778 static int do_scanResponse (void *o, Tcl_Interp *interp,
2779 int argc, char **argv)
2781 IrTcl_ScanObj *obj = o;
2785 obj->scanResponse = NULL;
2788 else if (argc == -1)
2789 return ir_tcl_strdel (interp, &obj->scanResponse);
2792 free (obj->scanResponse);
2795 if (ir_tcl_strdup (interp, &obj->scanResponse, argv[2])
2800 obj->scanResponse = NULL;
2806 * do_stepSize: Set/get replace Step Size
2808 static int do_stepSize (void *obj, Tcl_Interp *interp,
2809 int argc, char **argv)
2811 IrTcl_ScanObj *p = obj;
2817 return get_set_int (&p->stepSize, interp, argc, argv);
2821 * do_numberOfTermsRequested: Set/get Number of Terms requested
2823 static int do_numberOfTermsRequested (void *obj, Tcl_Interp *interp,
2824 int argc, char **argv)
2826 IrTcl_ScanObj *p = obj;
2830 p->numberOfTermsRequested = 20;
2833 return get_set_int (&p->numberOfTermsRequested, interp, argc, argv);
2838 * do_preferredPositionInResponse: Set/get preferred Position
2840 static int do_preferredPositionInResponse (void *obj, Tcl_Interp *interp,
2841 int argc, char **argv)
2843 IrTcl_ScanObj *p = obj;
2847 p->preferredPositionInResponse = 1;
2850 return get_set_int (&p->preferredPositionInResponse, interp, argc, argv);
2854 * do_scanStatus: Get scan status
2856 static int do_scanStatus (void *obj, Tcl_Interp *interp,
2857 int argc, char **argv)
2859 IrTcl_ScanObj *p = obj;
2863 return get_set_int (&p->scanStatus, interp, argc, argv);
2867 * do_numberOfEntriesReturned: Get number of Entries returned
2869 static int do_numberOfEntriesReturned (void *obj, Tcl_Interp *interp,
2870 int argc, char **argv)
2872 IrTcl_ScanObj *p = obj;
2876 return get_set_int (&p->numberOfEntriesReturned, interp, argc, argv);
2880 * do_positionOfTerm: Get position of Term
2882 static int do_positionOfTerm (void *obj, Tcl_Interp *interp,
2883 int argc, char **argv)
2885 IrTcl_ScanObj *p = obj;
2889 return get_set_int (&p->positionOfTerm, interp, argc, argv);
2893 * do_scanLine: get Scan Line (surrogate or normal) after response
2895 static int do_scanLine (void *obj, Tcl_Interp *interp, int argc, char **argv)
2897 IrTcl_ScanObj *p = obj;
2903 p->entries_flag = 0;
2905 p->nonSurrogateDiagnosticNum = 0;
2906 p->nonSurrogateDiagnosticList = 0;
2909 else if (argc == -1)
2911 p->entries_flag = 0;
2912 /* release entries */
2915 ir_deleteDiags (&p->nonSurrogateDiagnosticList,
2916 &p->nonSurrogateDiagnosticNum);
2921 interp->result = "wrong # args";
2924 if (Tcl_GetInt (interp, argv[2], &i) == TCL_ERROR)
2926 if (!p->entries_flag || p->which != Z_ListEntries_entries || !p->entries
2927 || i >= p->num_entries || i < 0)
2929 switch (p->entries[i].which)
2931 case Z_Entry_termInfo:
2932 Tcl_AppendElement (interp, "T");
2933 if (p->entries[i].u.term.buf)
2934 Tcl_AppendElement (interp, p->entries[i].u.term.buf);
2936 Tcl_AppendElement (interp, "");
2937 sprintf (numstr, "%d", p->entries[i].u.term.globalOccurrences);
2938 Tcl_AppendElement (interp, numstr);
2940 case Z_Entry_surrogateDiagnostic:
2941 Tcl_AppendElement (interp, "SD");
2942 return ir_diagResult (interp, p->entries[i].u.diag.list,
2943 p->entries[i].u.diag.num);
2949 static IrTcl_Method ir_scan_method_tab[] = {
2950 { 0, "scan", do_scan },
2951 { 0, "scanResponse", do_scanResponse },
2952 { 0, "stepSize", do_stepSize },
2953 { 0, "numberOfTermsRequested", do_numberOfTermsRequested },
2954 { 0, "preferredPositionInResponse", do_preferredPositionInResponse },
2955 { 0, "scanStatus", do_scanStatus },
2956 { 0, "numberOfEntriesReturned", do_numberOfEntriesReturned },
2957 { 0, "positionOfTerm", do_positionOfTerm },
2958 { 0, "scanLine", do_scanLine },
2963 * ir_scan_obj_method: IR Scan Object methods
2965 static int ir_scan_obj_method (ClientData clientData, Tcl_Interp *interp,
2966 int argc, char **argv)
2968 IrTcl_Methods tabs[2];
2972 interp->result = "wrong # args";
2975 tabs[0].tab = ir_scan_method_tab;
2976 tabs[0].obj = clientData;
2979 return ir_method (interp, argc, argv, tabs);
2983 * ir_scan_obj_delete: IR Scan Object disposal
2985 static void ir_scan_obj_delete (ClientData clientData)
2987 IrTcl_Methods tabs[2];
2988 IrTcl_ScanObj *obj = clientData;
2990 tabs[0].tab = ir_scan_method_tab;
2994 ir_method (NULL, -1, NULL, tabs);
2999 * ir_scan_obj_mk: IR Scan Object creation
3001 static int ir_scan_obj_mk (ClientData clientData, Tcl_Interp *interp,
3002 int argc, char **argv)
3004 Tcl_CmdInfo parent_info;
3006 IrTcl_Methods tabs[2];
3010 interp->result = "wrong # args";
3013 if (!Tcl_GetCommandInfo (interp, argv[2], &parent_info))
3015 interp->result = "No parent";
3018 obj = ir_tcl_malloc (sizeof(*obj));
3019 obj->parent = (IrTcl_Obj *) parent_info.clientData;
3021 tabs[0].tab = ir_scan_method_tab;
3025 if (ir_method (interp, 0, NULL, tabs) == TCL_ERROR)
3027 Tcl_CreateCommand (interp, argv[1], ir_scan_obj_method,
3028 (ClientData) obj, ir_scan_obj_delete);
3032 /* ------------------------------------------------------- */
3034 static void ir_initResponse (void *obj, Z_InitResponse *initrs)
3038 p->initResult = *initrs->result ? 1 : 0;
3039 if (!*initrs->result)
3040 logf (LOG_DEBUG, "Connection rejected by target");
3042 logf (LOG_DEBUG, "Connection accepted by target");
3044 get_referenceId (&p->set_inher.referenceId, initrs->referenceId);
3046 free (p->targetImplementationId);
3047 ir_tcl_strdup (p->interp, &p->targetImplementationId,
3048 initrs->implementationId);
3049 free (p->targetImplementationName);
3050 ir_tcl_strdup (p->interp, &p->targetImplementationName,
3051 initrs->implementationName);
3052 free (p->targetImplementationVersion);
3053 ir_tcl_strdup (p->interp, &p->targetImplementationVersion,
3054 initrs->implementationVersion);
3056 p->maximumRecordSize = *initrs->maximumRecordSize;
3057 p->preferredMessageSize = *initrs->preferredMessageSize;
3059 memcpy (&p->options, initrs->options, sizeof(initrs->options));
3060 memcpy (&p->protocolVersion, initrs->protocolVersion,
3061 sizeof(initrs->protocolVersion));
3062 free (p->userInformationField);
3063 p->userInformationField = NULL;
3064 if (initrs->userInformationField)
3068 if (initrs->userInformationField->which == ODR_EXTERNAL_octet &&
3069 (p->userInformationField =
3070 ir_tcl_malloc ((len =
3071 initrs->userInformationField->
3072 u.octet_aligned->len) +1)))
3074 memcpy (p->userInformationField,
3075 initrs->userInformationField->u.octet_aligned->buf,
3077 (p->userInformationField)[len] = '\0';
3082 static void ir_deleteDiags (IrTcl_Diagnostic **dst_list, int *dst_num)
3085 for (i = 0; i<*dst_num; i++)
3086 free (dst_list[i]->addinfo);
3092 static void ir_handleDiags (IrTcl_Diagnostic **dst_list, int *dst_num,
3093 Z_DiagRec **list, int num)
3099 *dst_list = ir_tcl_malloc (sizeof(**dst_list) * num);
3100 for (i = 0; i<num; i++)
3102 switch (list[i]->which)
3104 case Z_DiagRec_defaultFormat:
3105 (*dst_list)[i].condition = *list[i]->u.defaultFormat->condition;
3106 addinfo = list[i]->u.defaultFormat->addinfo;
3108 ((*dst_list)[i].addinfo = ir_tcl_malloc (strlen(addinfo)+1)))
3109 strcpy ((*dst_list)[i].addinfo, addinfo);
3112 (*dst_list)[i].addinfo = NULL;
3113 (*dst_list)[i].condition = 0;
3118 static void ir_handleRecords (void *o, Z_Records *zrs, IrTcl_SetObj *setobj,
3119 const char *elements)
3124 IrTcl_RecordList *rl;
3126 setobj->which = zrs->which;
3127 setobj->recordFlag = 1;
3129 ir_deleteDiags (&setobj->nonSurrogateDiagnosticList,
3130 &setobj->nonSurrogateDiagnosticNum);
3131 if (zrs->which == Z_Records_DBOSD)
3133 setobj->numberOfRecordsReturned =
3134 zrs->u.databaseOrSurDiagnostics->num_records;
3135 logf (LOG_DEBUG, "Got %d records", setobj->numberOfRecordsReturned);
3136 for (offset = 0; offset<setobj->numberOfRecordsReturned; offset++)
3138 rl = new_IR_record (setobj, setobj->start + offset,
3139 zrs->u.databaseOrSurDiagnostics->
3140 records[offset]->which,
3142 if (rl->which == Z_NamePlusRecord_surrogateDiagnostic)
3144 ir_handleDiags (&rl->u.surrogateDiagnostics.list,
3145 &rl->u.surrogateDiagnostics.num,
3146 &zrs->u.databaseOrSurDiagnostics->
3147 records[offset]->u.surrogateDiagnostic,
3152 Z_DatabaseRecord *zr;
3154 struct oident *ident;
3156 zr = zrs->u.databaseOrSurDiagnostics->records[offset]
3158 oe = (Z_External*) zr;
3159 rl->u.dbrec.size = zr->u.octet_aligned->len;
3161 if ((ident = oid_getentbyoid (oe->direct_reference)))
3162 rl->u.dbrec.type = ident->value;
3164 rl->u.dbrec.type = VAL_USMARC;
3166 if (oe->which == ODR_EXTERNAL_octet && rl->u.dbrec.size > 0)
3168 char *buf = (char*) zr->u.octet_aligned->buf;
3169 if ((rl->u.dbrec.buf = ir_tcl_malloc (rl->u.dbrec.size)))
3170 memcpy (rl->u.dbrec.buf, buf, rl->u.dbrec.size);
3172 else if (rl->u.dbrec.type == VAL_SUTRS &&
3173 oe->which == Z_External_sutrs)
3175 odr_setbuf (p->odr_in, (char*) oe->u.single_ASN1_type->buf,
3176 oe->u.single_ASN1_type->len, 0);
3177 if ((rl->u.dbrec.buf = ir_tcl_malloc (oe->u.sutrs->len+1)))
3179 memcpy (rl->u.dbrec.buf, oe->u.sutrs->buf,
3181 rl->u.dbrec.buf[oe->u.sutrs->len] = '\0';
3183 rl->u.dbrec.size = oe->u.sutrs->len;
3185 else if (rl->u.dbrec.type == VAL_GRS1 &&
3186 oe->which == Z_External_grs1)
3188 ir_tcl_read_grs (oe->u.grs1, &rl->u.dbrec.u.grs1);
3189 rl->u.dbrec.buf = NULL;
3192 rl->u.dbrec.buf = NULL;
3196 else if (zrs->which == Z_Records_multipleNSD)
3198 logf (LOG_DEBUG, "multipleNonSurrogateDiagnostic %d",
3199 zrs->u.multipleNonSurDiagnostics->num_diagRecs);
3200 setobj->numberOfRecordsReturned = 0;
3201 ir_handleDiags (&setobj->nonSurrogateDiagnosticList,
3202 &setobj->nonSurrogateDiagnosticNum,
3203 zrs->u.multipleNonSurDiagnostics->diagRecs,
3204 zrs->u.multipleNonSurDiagnostics->num_diagRecs);
3208 logf (LOG_DEBUG, "NonSurrogateDiagnostic");
3209 setobj->numberOfRecordsReturned = 0;
3210 ir_handleDiags (&setobj->nonSurrogateDiagnosticList,
3211 &setobj->nonSurrogateDiagnosticNum,
3212 &zrs->u.nonSurrogateDiagnostic,
3217 static void ir_searchResponse (void *o, Z_SearchResponse *searchrs,
3218 IrTcl_SetObj *setobj)
3220 Z_Records *zrs = searchrs->records;
3222 logf (LOG_DEBUG, "Received search response");
3225 logf (LOG_DEBUG, "Search response, no object!");
3228 setobj->searchStatus = searchrs->searchStatus ? 1 : 0;
3229 get_referenceId (&setobj->set_inher.referenceId, searchrs->referenceId);
3230 setobj->resultCount = *searchrs->resultCount;
3231 if (searchrs->presentStatus)
3232 setobj->presentStatus = *searchrs->presentStatus;
3233 if (searchrs->nextResultSetPosition)
3234 setobj->nextResultSetPosition = *searchrs->nextResultSetPosition;
3236 logf (LOG_DEBUG, "Search response %d, %d hits",
3237 setobj->searchStatus, setobj->resultCount);
3241 if (setobj->resultCount <= setobj->set_inher.smallSetUpperBound)
3242 es = setobj->set_inher.smallSetElementSetNames;
3244 es = setobj->set_inher.mediumSetElementSetNames;
3245 ir_handleRecords (o, zrs, setobj, es);
3248 setobj->recordFlag = 0;
3252 static void ir_presentResponse (void *o, Z_PresentResponse *presrs,
3253 IrTcl_SetObj *setobj)
3255 Z_Records *zrs = presrs->records;
3257 logf (LOG_DEBUG, "Received present response");
3260 logf (LOG_DEBUG, "Present response, no object!");
3263 setobj->presentStatus = *presrs->presentStatus;
3264 get_referenceId (&setobj->set_inher.referenceId, presrs->referenceId);
3265 setobj->nextResultSetPosition = *presrs->nextResultSetPosition;
3267 ir_handleRecords (o, zrs, setobj, setobj->set_inher.elementSetNames);
3270 setobj->recordFlag = 0;
3271 logf (LOG_DEBUG, "No records!");
3275 static void ir_scanResponse (void *o, Z_ScanResponse *scanrs,
3276 IrTcl_ScanObj *scanobj)
3280 logf (LOG_DEBUG, "Received scanResponse");
3282 get_referenceId (&p->set_inher.referenceId, scanrs->referenceId);
3283 scanobj->scanStatus = *scanrs->scanStatus;
3284 logf (LOG_DEBUG, "scanStatus=%d", scanobj->scanStatus);
3286 if (scanrs->stepSize)
3287 scanobj->stepSize = *scanrs->stepSize;
3288 logf (LOG_DEBUG, "stepSize=%d", scanobj->stepSize);
3290 scanobj->numberOfEntriesReturned = *scanrs->numberOfEntriesReturned;
3291 logf (LOG_DEBUG, "numberOfEntriesReturned=%d",
3292 scanobj->numberOfEntriesReturned);
3294 if (scanrs->positionOfTerm)
3295 scanobj->positionOfTerm = *scanrs->positionOfTerm;
3297 scanobj->positionOfTerm = -1;
3298 logf (LOG_DEBUG, "positionOfTerm=%d", scanobj->positionOfTerm);
3300 free (scanobj->entries);
3301 scanobj->entries = NULL;
3303 ir_deleteDiags (&scanobj->nonSurrogateDiagnosticList,
3304 &scanobj->nonSurrogateDiagnosticNum);
3305 if (scanrs->entries)
3310 scanobj->entries_flag = 1;
3311 scanobj->which = scanrs->entries->which;
3312 switch (scanobj->which)
3314 case Z_ListEntries_entries:
3315 scanobj->num_entries = scanrs->entries->u.entries->num_entries;
3316 scanobj->entries = ir_tcl_malloc (scanobj->num_entries *
3317 sizeof(*scanobj->entries));
3318 for (i=0; i<scanobj->num_entries; i++)
3320 ze = scanrs->entries->u.entries->entries[i];
3321 scanobj->entries[i].which = ze->which;
3324 case Z_Entry_termInfo:
3325 if (ze->u.termInfo->term->which == Z_Term_general)
3327 int l = ze->u.termInfo->term->u.general->len;
3328 scanobj->entries[i].u.term.buf = ir_tcl_malloc (1+l);
3329 memcpy (scanobj->entries[i].u.term.buf,
3330 ze->u.termInfo->term->u.general->buf,
3332 scanobj->entries[i].u.term.buf[l] = '\0';
3335 scanobj->entries[i].u.term.buf = NULL;
3336 if (ze->u.termInfo->globalOccurrences)
3337 scanobj->entries[i].u.term.globalOccurrences =
3338 *ze->u.termInfo->globalOccurrences;
3340 scanobj->entries[i].u.term.globalOccurrences = 0;
3342 case Z_Entry_surrogateDiagnostic:
3343 ir_handleDiags (&scanobj->entries[i].u.diag.list,
3344 &scanobj->entries[i].u.diag.num,
3345 &ze->u.surrogateDiagnostic,
3351 case Z_ListEntries_nonSurrogateDiagnostics:
3352 ir_handleDiags (&scanobj->nonSurrogateDiagnosticList,
3353 &scanobj->nonSurrogateDiagnosticNum,
3354 scanrs->entries->u.nonSurrogateDiagnostics->
3356 scanrs->entries->u.nonSurrogateDiagnostics->
3362 scanobj->entries_flag = 0;
3366 * ir_select_read: handle incoming packages
3368 void ir_select_read (ClientData clientData)
3370 IrTcl_Obj *p = clientData;
3375 Tcl_CmdInfo cmd_info;
3376 const char *apdu_call;
3378 logf(LOG_DEBUG, "Read handler");
3379 if (p->state == IR_TCL_R_Connecting)
3381 logf(LOG_DEBUG, "Connect handler");
3382 r = cs_rcvconnect (p->cs_link);
3385 logf (LOG_WARN, "cs_rcvconnect returned 1");
3388 p->state = IR_TCL_R_Idle;
3389 #if IRTCL_GENERIC_FILES
3390 ir_select_remove_write (p->csFile, p);
3392 ir_select_remove_write (cs_fileno (p->cs_link), p);
3396 logf (LOG_DEBUG, "cs_rcvconnect error");
3399 p->failInfo = IR_TCL_FAIL_CONNECT;
3400 IrTcl_eval (p->interp, p->failback);
3402 do_disconnect (p, NULL, 2, NULL);
3405 p->state = IR_TCL_R_Idle;
3407 IrTcl_eval (p->interp, p->callback);
3408 if (p->cs_link && p->request_queue && p->state == IR_TCL_R_Idle)
3409 ir_tcl_send_q (p, p->request_queue, "x");
3414 /* signal one more use of ir object - callbacks must not
3415 release the ir memory (p pointer) */
3416 p->state = IR_TCL_R_Reading;
3419 /* read incoming APDU */
3420 if ((r=cs_get (p->cs_link, &p->buf_in, &p->len_in)) <= 0)
3422 logf (LOG_DEBUG, "cs_get failed, code %d", r);
3423 #if IRTCL_GENERIC_FILES
3424 ir_select_remove (p->csFile, p);
3426 ir_select_remove (cs_fileno (p->cs_link), p);
3428 do_disconnect (p, NULL, 2, NULL);
3431 p->failInfo = IR_TCL_FAIL_READ;
3432 IrTcl_eval (p->interp, p->failback);
3434 /* release ir object now if callback deleted it */
3440 logf(LOG_DEBUG, "PDU Fraction read");
3443 /* got complete APDU. Now decode */
3446 odr_setbuf (p->odr_in, p->buf_in, r, 0);
3447 logf (LOG_DEBUG, "cs_get ok, total size %d", r);
3448 if (!z_APDU (p->odr_in, &apdu, 0))
3450 logf (LOG_DEBUG, "cs_get failed: %s",
3451 odr_errmsg (odr_geterror (p->odr_in)));
3452 do_disconnect (p, NULL, 2, NULL);
3455 p->failInfo = IR_TCL_FAIL_IN_APDU;
3456 p->apduOffset = odr_offset (p->odr_in);
3457 IrTcl_eval (p->interp, p->failback);
3459 /* release ir object now if failback deleted it */
3463 logf(LOG_DEBUG, "Decoded ok");
3464 /* handle APDU and invoke callback */
3465 rq = p->request_queue;
3468 logf (LOG_FATAL, "Internal error. No queue entry");
3471 object_name = rq->object_name;
3472 logf (LOG_DEBUG, "getCommandInfo (%s)", object_name);
3474 if (Tcl_GetCommandInfo (p->interp, object_name, &cmd_info))
3478 case Z_APDU_initResponse:
3479 p->eventType = "init";
3480 ir_initResponse (p, apdu->u.initResponse);
3481 apdu_call = p->initResponse;
3483 case Z_APDU_searchResponse:
3484 p->eventType = "search";
3485 ir_searchResponse (p, apdu->u.searchResponse,
3486 (IrTcl_SetObj *) cmd_info.clientData);
3487 apdu_call = ((IrTcl_SetObj *)
3488 cmd_info.clientData)->searchResponse;
3490 case Z_APDU_presentResponse:
3491 p->eventType = "present";
3492 ir_presentResponse (p, apdu->u.presentResponse,
3493 (IrTcl_SetObj *) cmd_info.clientData);
3494 apdu_call = ((IrTcl_SetObj *)
3495 cmd_info.clientData)->presentResponse;
3497 case Z_APDU_scanResponse:
3498 p->eventType = "scan";
3499 ir_scanResponse (p, apdu->u.scanResponse,
3500 (IrTcl_ScanObj *) cmd_info.clientData);
3501 apdu_call = ((IrTcl_ScanObj *)
3502 cmd_info.clientData)->scanResponse;
3505 logf (LOG_WARN, "Received unknown APDU type (%d)",
3507 do_disconnect (p, NULL, 2, NULL);
3510 p->failInfo = IR_TCL_FAIL_UNKNOWN_APDU;
3511 IrTcl_eval (p->interp, p->failback);
3516 p->request_queue = rq->next;
3517 p->state = IR_TCL_R_Idle;
3520 IrTcl_eval (p->interp, apdu_call);
3521 else if (rq->callback)
3522 IrTcl_eval (p->interp, rq->callback);
3524 free (rq->callback);
3525 free (rq->object_name);
3527 odr_reset (p->odr_in);
3528 if (p->ref_count == 1)
3534 } while (p->cs_link && cs_more (p->cs_link));
3535 if (p->cs_link && p->request_queue && p->state == IR_TCL_R_Idle)
3536 ir_tcl_send_q (p, p->request_queue, "x");
3540 * ir_select_write: handle outgoing packages - not yet written.
3542 void ir_select_write (ClientData clientData)
3544 IrTcl_Obj *p = clientData;
3548 logf (LOG_DEBUG, "Write handler");
3549 if (p->state == IR_TCL_R_Connecting)
3551 logf(LOG_DEBUG, "Connect handler");
3552 r = cs_rcvconnect (p->cs_link);
3555 p->state = IR_TCL_R_Idle;
3558 logf (LOG_DEBUG, "cs_rcvconnect error");
3559 #if IRTCL_GENERIC_FILES
3560 ir_select_remove_write (p->csFile, p);
3562 ir_select_remove_write (cs_fileno (p->cs_link), p);
3566 p->failInfo = IR_TCL_FAIL_CONNECT;
3567 IrTcl_eval (p->interp, p->failback);
3569 do_disconnect (p, NULL, 2, NULL);
3572 #if IRTCL_GENERIC_FILES
3573 ir_select_remove_write (p->csFile, p);
3575 ir_select_remove_write (cs_fileno (p->cs_link), p);
3578 IrTcl_eval (p->interp, p->callback);
3581 rq = p->request_queue;
3582 if (!rq || !rq->buf_out)
3585 if ((r=cs_put (p->cs_link, rq->buf_out, rq->len_out)) < 0)
3587 logf (LOG_DEBUG, "cs_put write fail");
3590 p->failInfo = IR_TCL_FAIL_WRITE;
3591 IrTcl_eval (p->interp, p->failback);
3595 do_disconnect (p, NULL, 2, NULL);
3597 else if (r == 0) /* remove select bit */
3599 logf(LOG_DEBUG, "Write completed");
3600 p->state = IR_TCL_R_Waiting;
3601 #if IRTCL_GENERIC_FILES
3602 ir_select_remove_write (p->csFile, p);
3604 ir_select_remove_write (cs_fileno (p->cs_link), p);
3611 /* ------------------------------------------------------- */
3614 * Irtcl_init: Registration of TCL commands.
3616 int Irtcl_Init (Tcl_Interp *interp)
3618 Tcl_CreateCommand (interp, "ir", ir_obj_mk, (ClientData) NULL,
3619 (Tcl_CmdDeleteProc *) NULL);
3620 Tcl_CreateCommand (interp, "ir-set", ir_set_obj_mk,
3621 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
3622 Tcl_CreateCommand (interp, "ir-scan", ir_scan_obj_mk,
3623 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);