2 * IR toolkit for tcl/tk
4 * See the file LICENSE for details.
5 * Sebastian Hammer, Adam Dickmeiss
8 * Revision 1.77 1996-02-20 17:52:58 adam
9 * Uses the YAZ oid system to name record syntax object identifiers.
11 * Revision 1.76 1996/02/20 16:09:51 adam
12 * Bug fix: didn't set element set names stamp correctly on result
13 * set records when element set names were set to the empty string.
15 * Revision 1.75 1996/02/19 15:41:53 adam
16 * Better log messages.
17 * Minor improvement of connect method.
19 * Revision 1.74 1996/02/05 17:58:03 adam
20 * Ported ir-tcl to use the beta releases of tcl7.5/tk4.1.
22 * Revision 1.73 1996/01/29 11:35:19 adam
23 * Bug fix: cs_type member renamed to comstackType to avoid conflict with
24 * cs_type macro defined by YAZ.
26 * Revision 1.72 1996/01/19 17:45:34 quinn
27 * Added debugging output
29 * Revision 1.71 1996/01/19 16:22:38 adam
30 * New method: apduDump - returns information about last incoming APDU.
32 * Revision 1.70 1996/01/10 09:18:34 adam
33 * PDU specific callbacks implemented: initRespnse, searchResponse,
34 * presentResponse and scanResponse.
35 * Bug fix in the command line shell (tclmain.c) - discovered on OSF/1.
37 * Revision 1.69 1996/01/04 16:12:12 adam
38 * Setting PDUType renamed to eventType.
40 * Revision 1.68 1996/01/04 11:05:22 adam
41 * New setting: PDUType - returns type of last PDU returned from the target.
42 * Fixed a bug in configure/Makefile.
44 * Revision 1.67 1996/01/03 09:00:51 adam
45 * Updated to use new version of Yaz (names changed to avoid C++ conflict).
47 * Revision 1.66 1995/11/28 17:26:39 adam
48 * Removed Carriage return from ir-tcl.c!
49 * Removed misc. debug logs.
51 * Revision 1.65 1995/11/28 13:53:00 quinn
54 * Revision 1.64 1995/11/13 15:39:18 adam
55 * Bug fix: {small,medium}SetElementSetNames weren't set correctly.
56 * Bug fix: idAuthentication weren't set correctly.
58 * Revision 1.63 1995/11/13 09:55:39 adam
59 * Multiple records at a position in a result-set with differnt
62 * Revision 1.62 1995/10/18 17:20:33 adam
63 * Work on target setup in client.tcl.
65 * Revision 1.61 1995/10/18 16:42:42 adam
66 * New settings: smallSetElementSetNames and mediumSetElementSetNames.
68 * Revision 1.60 1995/10/18 15:43:31 adam
69 * In search: mediumSetElementSetNames and smallSetElementSetNames are
70 * set to elementSetNames.
72 * Revision 1.59 1995/10/17 12:18:58 adam
73 * Bug fix: when target connection closed, the connection was not
74 * properly reestablished.
76 * Revision 1.58 1995/10/16 17:00:55 adam
77 * New setting: elementSetNames.
78 * Various client improvements. Medium presentation format looks better.
80 * Revision 1.57 1995/09/21 13:11:51 adam
81 * Support of dynamic loading.
82 * Test script uses load command if necessary.
84 * Revision 1.56 1995/08/29 15:30:14 adam
85 * Work on GRS records.
87 * Revision 1.55 1995/08/28 09:43:25 adam
88 * Minor changes. configure only searches for yaz beta 3 and versions after
91 * Revision 1.54 1995/08/24 12:25:16 adam
92 * Modified to work with yaz 1.0b3.
94 * Revision 1.53 1995/08/04 12:49:26 adam
95 * Bug fix: reading uninitialized variable p.
97 * Revision 1.52 1995/08/04 11:32:38 adam
98 * More work on output queue. Memory related routines moved
101 * Revision 1.51 1995/08/03 13:22:54 adam
104 * Revision 1.50 1995/07/20 08:09:49 adam
105 * client.tcl: Targets removed from hotTargets list when targets
106 * are removed/modified.
107 * ir-tcl.c: More work on triggerResourceControl.
109 * Revision 1.49 1995/06/30 12:39:21 adam
110 * Bug fix: loadFile didn't set record type.
111 * The MARC routines are a little less strict in the interpretation.
112 * Script display.tcl replaces the old marc.tcl.
113 * New interactive script: shell.tcl.
115 * Revision 1.48 1995/06/27 19:03:50 adam
116 * Bug fix in do_present in ir-tcl.c: p->set_child member weren't set.
117 * nextResultSetPosition used instead of setOffset.
119 * Revision 1.47 1995/06/25 10:25:04 adam
120 * Working on triggerResourceControl. Description of compile/install
121 * procedure moved to ir-tcl.sgml.
123 * Revision 1.46 1995/06/22 13:15:06 adam
124 * Feature: SUTRS. Setting getSutrs implemented.
125 * Work on display formats.
126 * Preferred record syntax can be set by the user.
128 * Revision 1.45 1995/06/20 08:07:30 adam
129 * New setting: failInfo.
130 * Working on better cancel mechanism.
132 * Revision 1.44 1995/06/19 17:01:20 adam
135 * Revision 1.43 1995/06/19 13:06:08 adam
136 * New define: IR_TCL_VERSION.
138 * Revision 1.42 1995/06/19 08:08:52 adam
139 * client.tcl: hotTargets now contain both database and target name.
140 * ir-tcl.c: setting protocol edited. Errors in callbacks are logged
141 * by logf(LOG_WARN, ...) calls.
143 * Revision 1.41 1995/06/16 12:28:16 adam
144 * Implemented preferredRecordSyntax.
145 * Minor changes in diagnostic handling.
146 * Record list deleted when connection closes.
148 * Revision 1.40 1995/06/14 13:37:18 adam
149 * Setting recordType implemented.
150 * Setting implementationVersion implemented.
151 * Settings implementationId / implementationName edited.
153 * Revision 1.39 1995/06/08 10:26:32 adam
154 * Bug fix in ir_strdup.
156 * Revision 1.38 1995/06/01 16:36:47 adam
157 * About buttons. Minor bug fixes.
159 * Revision 1.37 1995/06/01 07:31:20 adam
160 * Rename of many typedefs -> IrTcl_...
162 * Revision 1.36 1995/05/31 13:09:59 adam
163 * Client searches/presents may be interrupted.
164 * New moving book-logo.
166 * Revision 1.35 1995/05/31 08:36:33 adam
167 * Bug fix in client.tcl: didn't save options on clientrc.tcl.
168 * New method: referenceId. More work on scan.
170 * Revision 1.34 1995/05/29 10:33:42 adam
171 * README and rename of startup script.
173 * Revision 1.33 1995/05/29 09:15:11 quinn
174 * Changed CS_SR to PROTO_SR, etc.
176 * Revision 1.32 1995/05/29 08:44:16 adam
177 * Work on delete of objects.
179 * Revision 1.31 1995/05/26 11:44:10 adam
180 * Bugs fixed. More work on MARC utilities and queries. Test
181 * client is up-to-date again.
183 * Revision 1.30 1995/05/26 08:54:11 adam
184 * New MARC utilities. Uses prefix query.
186 * Revision 1.29 1995/05/24 14:10:22 adam
187 * Work on idAuthentication, protocolVersion and options.
189 * Revision 1.28 1995/05/23 15:34:48 adam
190 * Many new settings, userInformationField, smallSetUpperBound, etc.
191 * A number of settings are inherited when ir-set is executed.
192 * This version is incompatible with the graphical test client (client.tcl).
194 * Revision 1.27 1995/05/11 15:34:47 adam
195 * Scan request changed a bit. This version works with RLG.
197 * Revision 1.26 1995/04/18 16:11:51 adam
198 * First version of graphical Scan. Some work on query-by-form.
200 * Revision 1.25 1995/04/17 09:37:17 adam
201 * Further development of scan.
203 * Revision 1.24 1995/04/11 14:16:42 adam
204 * Further work on scan. Response works. Entries aren't saved yet.
206 * Revision 1.23 1995/04/10 10:50:27 adam
207 * Result-set name defaults to suffix of ir-set name.
208 * Started working on scan. Not finished at this point.
210 * Revision 1.22 1995/03/31 10:43:03 adam
211 * More robust when getting bad MARC records.
213 * Revision 1.21 1995/03/31 08:56:37 adam
214 * New button "Search".
216 * Revision 1.20 1995/03/29 16:07:09 adam
217 * Bug fix: Didn't use setName in present request.
219 * Revision 1.19 1995/03/28 12:45:23 adam
220 * New ir method failback: called on disconnect/protocol error.
221 * New ir set/get method: protocol: SR / Z3950.
222 * Simple popup and disconnect when failback is invoked.
224 * Revision 1.18 1995/03/21 15:50:12 adam
227 * Revision 1.17 1995/03/21 13:41:03 adam
228 * Comstack cs_create not used too often. Non-blocking connect.
230 * Revision 1.16 1995/03/21 08:26:06 adam
231 * New method, setName, to specify the result set name (other than Default).
232 * New method, responseStatus, which returns diagnostic info, if any, after
233 * present response / search response.
235 * Revision 1.15 1995/03/20 15:24:07 adam
236 * Diagnostic records saved on searchResponse.
238 * Revision 1.14 1995/03/20 08:53:22 adam
239 * Event loop in tclmain.c rewritten. New method searchStatus.
241 * Revision 1.13 1995/03/17 18:26:17 adam
242 * Non-blocking i/o used now. Database names popup as cascade items.
244 * Revision 1.12 1995/03/17 15:45:00 adam
245 * Improved target/database setup.
247 * Revision 1.11 1995/03/16 17:54:03 adam
248 * Minor changes really.
250 * Revision 1.10 1995/03/15 16:14:50 adam
251 * Blocking arg in cs_create changed.
253 * Revision 1.9 1995/03/15 13:59:24 adam
256 * Revision 1.8 1995/03/15 08:25:16 adam
257 * New method presentStatus to check for error on present. Misc. cleanup
258 * of IrTcl_RecordList manipulations. Full MARC record presentation in
261 * Revision 1.7 1995/03/14 17:32:29 adam
262 * Presentation of full Marc record in popup window.
264 * Revision 1.6 1995/03/12 19:31:55 adam
265 * Pattern matching implemented when retrieving MARC records. More
266 * diagnostic functions.
268 * Revision 1.5 1995/03/10 18:00:15 adam
269 * Actual presentation in line-by-line format. RPN query support.
271 * Revision 1.4 1995/03/09 16:15:08 adam
272 * First presentRequest attempts. Hot-target list.
289 int (*method) (void *obj, Tcl_Interp *interp, int argc, char **argv);
297 static void ir_deleteDiags (IrTcl_Diagnostic **dst_list, int *dst_num);
298 static int do_disconnect (void *obj, Tcl_Interp *interp,
299 int argc, char **argv);
301 static IrTcl_RecordList *new_IR_record (IrTcl_SetObj *setobj,
303 const char *elements)
305 IrTcl_RecordList *rl;
307 if (elements && !*elements)
309 for (rl = setobj->record_list; rl; rl = rl->next)
311 if (no == rl->no && (!rl->elements || !elements ||
312 !strcmp(elements, rl->elements)))
317 case Z_NamePlusRecord_databaseRecord:
318 free (rl->u.dbrec.buf);
319 rl->u.dbrec.buf = NULL;
321 case Z_NamePlusRecord_surrogateDiagnostic:
322 ir_deleteDiags (&rl->u.surrogateDiagnostics.list,
323 &rl->u.surrogateDiagnostics.num);
331 rl = ir_tcl_malloc (sizeof(*rl));
332 rl->next = setobj->record_list;
334 setobj->record_list = rl;
337 ir_tcl_strdup (NULL, &rl->elements, elements);
344 int IrTcl_eval (Tcl_Interp *interp, const char *command)
346 char *tmp = ir_tcl_malloc (strlen(command)+1);
349 strcpy (tmp, command);
350 r = Tcl_Eval (interp, tmp);
352 logf (LOG_WARN, "Tcl error in line %d: %s", interp->errorLine,
354 Tcl_FreeResult (interp);
360 * IrTcl_getRecordSyntaxStr: Return record syntax name of object id
362 static char *IrTcl_getRecordSyntaxStr (enum oid_value value)
365 struct oident ent, *entp;
367 ent.proto = PROTO_Z3950;
368 ent.oclass = CLASS_RECSYN;
371 o = oid_getoidbyent (&ent);
372 entp = oid_getentbyoid (o);
380 * IrTcl_getRecordSyntaxVal: Return record syntax value of string
382 static enum oid_value IrTcl_getRecordSyntaxVal (const char *name)
384 return oid_getvalbyname (name);
387 static IrTcl_RecordList *find_IR_record (IrTcl_SetObj *setobj, int no)
389 IrTcl_RecordList *rl;
391 for (rl = setobj->record_list; rl; rl = rl->next)
393 (!setobj->recordElements || !rl->elements ||
394 !strcmp (setobj->recordElements, rl->elements)))
399 static void delete_IR_records (IrTcl_SetObj *setobj)
401 IrTcl_RecordList *rl, *rl1;
403 for (rl = setobj->record_list; rl; rl = rl1)
407 case Z_NamePlusRecord_databaseRecord:
408 free (rl->u.dbrec.buf);
410 case Z_NamePlusRecord_surrogateDiagnostic:
411 ir_deleteDiags (&rl->u.surrogateDiagnostics.list,
412 &rl->u.surrogateDiagnostics.num);
418 setobj->record_list = NULL;
422 * get_set_int: Set/get integer value
424 static int get_set_int (int *val, Tcl_Interp *interp, int argc, char **argv)
430 if (Tcl_GetInt (interp, argv[2], val)==TCL_ERROR)
433 sprintf (buf, "%d", *val);
434 Tcl_AppendResult (interp, buf, NULL);
439 * ir_method: Search for method in table and invoke method handler
441 int ir_method (Tcl_Interp *interp, int argc, char **argv, IrTcl_Methods *tab)
443 IrTcl_Methods *tab_i = tab;
446 for (tab_i = tab; tab_i->tab; tab_i++)
447 for (t = tab_i->tab; t->name; t++)
450 if ((*t->method)(tab_i->obj, interp, argc, argv) == TCL_ERROR)
454 if (!strcmp (t->name, argv[1]))
455 return (*t->method)(tab_i->obj, interp, argc, argv);
459 Tcl_AppendResult (interp, "Bad method: ", argv[1],
460 ". Possible methods:", NULL);
461 for (tab_i = tab; tab_i->tab; tab_i++)
462 for (t = tab_i->tab; t->name; t++)
463 Tcl_AppendResult (interp, " ", t->name, NULL);
468 * ir_method_r: Get status for all readable elements
470 int ir_method_r (void *obj, Tcl_Interp *interp, int argc, char **argv,
478 for (; tab->name; tab++)
481 argv_n[1] = tab->name;
482 Tcl_AppendResult (interp, "{", NULL);
483 (*tab->method)(obj, interp, argc_n, argv_n);
484 Tcl_AppendResult (interp, "} ", NULL);
490 * ir_named_bits: get/set named bits
492 int ir_named_bits (struct ir_named_entry *tab, Odr_bitmask *ob,
493 Tcl_Interp *interp, int argc, char **argv)
495 struct ir_named_entry *ti;
500 for (no = 0; no < argc; no++)
502 for (ti = tab; ti->name; ti++)
503 if (!strcmp (argv[no], ti->name))
505 ODR_MASK_SET (ob, ti->pos);
510 Tcl_AppendResult (interp, "Bad bit mask: ", argv[no], NULL);
516 for (ti = tab; ti->name; ti++)
517 if (ODR_MASK_GET (ob, ti->pos))
518 Tcl_AppendElement (interp, ti->name);
522 static void set_referenceId (ODR o, Z_ReferenceId **dst, const char *src)
528 *dst = odr_malloc (o, sizeof(**dst));
529 (*dst)->size = (*dst)->len = strlen(src);
530 (*dst)->buf = odr_malloc (o, (*dst)->len);
531 memcpy ((*dst)->buf, src, (*dst)->len);
535 static void get_referenceId (char **dst, Z_ReferenceId *src)
543 *dst = ir_tcl_malloc (src->len+1);
544 memcpy (*dst, src->buf, src->len);
545 (*dst)[src->len] = '\0';
548 /* ------------------------------------------------------- */
551 * do_init_request: init method on IR object
553 static int do_init_request (void *obj, Tcl_Interp *interp,
554 int argc, char **argv)
564 interp->result = "init: not connected";
567 apdu = zget_APDU (p->odr_out, Z_APDU_initRequest);
568 req = apdu->u.initRequest;
570 set_referenceId (p->odr_out, &req->referenceId, p->set_inher.referenceId);
571 req->options = &p->options;
572 req->protocolVersion = &p->protocolVersion;
573 req->preferredMessageSize = &p->preferredMessageSize;
574 req->maximumRecordSize = &p->maximumRecordSize;
576 if (p->idAuthenticationGroupId)
578 Z_IdPass *pass = odr_malloc (p->odr_out, sizeof(*pass));
579 Z_IdAuthentication *auth = odr_malloc (p->odr_out, sizeof(*auth));
581 logf (LOG_DEBUG, "using pass authentication");
583 auth->which = Z_IdAuthentication_idPass;
584 auth->u.idPass = pass;
585 if (p->idAuthenticationGroupId && *p->idAuthenticationGroupId)
586 pass->groupId = p->idAuthenticationGroupId;
588 pass->groupId = NULL;
589 if (p->idAuthenticationUserId && *p->idAuthenticationUserId)
590 pass->userId = p->idAuthenticationUserId;
593 if (p->idAuthenticationPassword && *p->idAuthenticationPassword)
594 pass->password = p->idAuthenticationPassword;
596 pass->password = NULL;
597 req->idAuthentication = auth;
599 else if (!p->idAuthenticationOpen || !*p->idAuthenticationOpen)
600 req->idAuthentication = NULL;
603 Z_IdAuthentication *auth = odr_malloc (p->odr_out, sizeof(*auth));
605 logf (LOG_DEBUG, "using open authentication");
606 auth->which = Z_IdAuthentication_open;
607 auth->u.open = p->idAuthenticationOpen;
608 req->idAuthentication = auth;
610 req->implementationId = p->implementationId;
611 req->implementationName = p->implementationName;
612 req->implementationVersion = p->implementationVersion;
613 req->userInformationField = 0;
615 return ir_tcl_send_APDU (interp, p, apdu, "init", *argv);
619 * do_protocolVersion: Set protocol Version
621 static int do_protocolVersion (void *obj, Tcl_Interp *interp,
622 int argc, char **argv)
630 ODR_MASK_ZERO (&p->protocolVersion);
631 ODR_MASK_SET (&p->protocolVersion, 0);
632 ODR_MASK_SET (&p->protocolVersion, 1);
637 if (Tcl_GetInt (interp, argv[2], &version)==TCL_ERROR)
639 ODR_MASK_ZERO (&p->protocolVersion);
640 for (i = 0; i<version; i++)
641 ODR_MASK_SET (&p->protocolVersion, i);
643 for (i = 4; --i >= 0; )
644 if (ODR_MASK_GET (&p->protocolVersion, i))
646 sprintf (buf, "%d", i+1);
647 interp->result = buf;
652 * do_options: Set options
654 static int do_options (void *obj, Tcl_Interp *interp,
655 int argc, char **argv)
657 static struct ir_named_entry options_tab[] = {
661 { "resourceReport", 3 },
662 { "triggerResourceCtrl", 4},
663 { "resourceCtrl", 5},
667 { "extendedServices", 10},
668 { "level-1Segmentation", 11},
669 { "level-2Segmentation", 12},
670 { "concurrentOperations", 13},
671 { "namedResultSets", 14},
678 ODR_MASK_ZERO (&p->options);
679 ODR_MASK_SET (&p->options, 0);
680 ODR_MASK_SET (&p->options, 1);
681 ODR_MASK_SET (&p->options, 4);
682 ODR_MASK_SET (&p->options, 7);
683 ODR_MASK_SET (&p->options, 14);
686 return ir_named_bits (options_tab, &p->options, interp, argc-2, argv+2);
690 * do_apduInfo: Get APDU information
692 static int do_apduInfo (void *obj, Tcl_Interp *interp, int argc, char **argv)
700 sprintf (buf, "%d", p->apduLen);
701 Tcl_AppendElement (interp, buf);
702 sprintf (buf, "%d", p->apduOffset);
703 Tcl_AppendElement (interp, buf);
706 Tcl_AppendElement (interp, "");
709 apduf = fopen ("apdu.tmp", "w");
712 Tcl_AppendElement (interp, "");
715 odr_dumpBER (apduf, p->buf_in, p->apduLen);
717 if (!(apduf = fopen ("apdu.tmp", "r")))
718 Tcl_AppendElement (interp, "");
723 Tcl_AppendResult (interp, " {", NULL);
724 while ((c = getc (apduf)) != EOF)
728 Tcl_AppendResult (interp, buf, NULL);
731 Tcl_AppendResult (interp, "}", NULL);
738 * do_failInfo: Get fail information
740 static int do_failInfo (void *obj, Tcl_Interp *interp, int argc, char **argv)
750 sprintf (buf, "%d", p->failInfo);
756 case IR_TCL_FAIL_CONNECT:
757 cp = "connect failed";
759 case IR_TCL_FAIL_READ:
760 cp = "connection closed";
762 case IR_TCL_FAIL_WRITE:
763 cp = "connection closed";
765 case IR_TCL_FAIL_IN_APDU:
766 cp = "failed to decode incoming APDU";
768 case IR_TCL_FAIL_UNKNOWN_APDU:
774 Tcl_AppendElement (interp, buf);
775 Tcl_AppendElement (interp, cp);
780 * do_preferredMessageSize: Set/get preferred message size
782 static int do_preferredMessageSize (void *obj, Tcl_Interp *interp,
783 int argc, char **argv)
789 p->preferredMessageSize = 30000;
792 return get_set_int (&p->preferredMessageSize, interp, argc, argv);
796 * do_maximumRecordSize: Set/get maximum record size
798 static int do_maximumRecordSize (void *obj, Tcl_Interp *interp,
799 int argc, char **argv)
805 p->maximumRecordSize = 30000;
808 return get_set_int (&p->maximumRecordSize, interp, argc, argv);
812 * do_initResult: Get init result
814 static int do_initResult (void *obj, Tcl_Interp *interp,
815 int argc, char **argv)
821 return get_set_int (&p->initResult, interp, argc, argv);
826 * do_implementationName: Set/get Implementation Name.
828 static int do_implementationName (void *obj, Tcl_Interp *interp,
829 int argc, char **argv)
834 return ir_tcl_strdup (interp, &p->implementationName,
835 "Index Data/IrTcl on YAZ");
837 return ir_tcl_strdel (interp, &p->implementationName);
840 free (p->implementationName);
841 if (ir_tcl_strdup (interp, &p->implementationName, argv[2])
845 Tcl_AppendResult (interp, p->implementationName, (char*) NULL);
850 * do_implementationId: Get Implementation Id.
852 static int do_implementationId (void *obj, Tcl_Interp *interp,
853 int argc, char **argv)
858 return ir_tcl_strdup (interp, &p->implementationId, "YAZ (id=81)");
860 return ir_tcl_strdel (interp, &p->implementationId);
861 Tcl_AppendResult (interp, p->implementationId, (char*) NULL);
866 * do_implementationVersion: get Implementation Version.
868 static int do_implementationVersion (void *obj, Tcl_Interp *interp,
869 int argc, char **argv)
874 return ir_tcl_strdup (interp, &p->implementationVersion,
875 "YAZ: " YAZ_VERSION " / IrTcl: " IR_TCL_VERSION);
877 return ir_tcl_strdel (interp, &p->implementationVersion);
878 Tcl_AppendResult (interp, p->implementationVersion, (char*) NULL);
883 * do_targetImplementationName: Get Implementation Name of target.
885 static int do_targetImplementationName (void *obj, Tcl_Interp *interp,
886 int argc, char **argv)
892 p->targetImplementationName = NULL;
896 return ir_tcl_strdel (interp, &p->targetImplementationName);
897 Tcl_AppendResult (interp, p->targetImplementationName, (char*) NULL);
902 * do_targetImplementationId: Get Implementation Id of target
904 static int do_targetImplementationId (void *obj, Tcl_Interp *interp,
905 int argc, char **argv)
911 p->targetImplementationId = NULL;
915 return ir_tcl_strdel (interp, &p->targetImplementationId);
916 Tcl_AppendResult (interp, p->targetImplementationId, (char*) NULL);
921 * do_targetImplementationVersion: Get Implementation Version of target
923 static int do_targetImplementationVersion (void *obj, Tcl_Interp *interp,
924 int argc, char **argv)
930 p->targetImplementationVersion = NULL;
934 return ir_tcl_strdel (interp, &p->targetImplementationVersion);
935 Tcl_AppendResult (interp, p->targetImplementationVersion, (char*) NULL);
940 * do_idAuthentication: Set/get id Authentication
942 static int do_idAuthentication (void *obj, Tcl_Interp *interp,
943 int argc, char **argv)
947 if (argc >= 3 || argc == -1)
949 free (p->idAuthenticationOpen);
950 free (p->idAuthenticationGroupId);
951 free (p->idAuthenticationUserId);
952 free (p->idAuthenticationPassword);
954 if (argc >= 3 || argc <= 0)
956 p->idAuthenticationOpen = NULL;
957 p->idAuthenticationGroupId = NULL;
958 p->idAuthenticationUserId = NULL;
959 p->idAuthenticationPassword = NULL;
968 ir_tcl_strdup (interp, &p->idAuthenticationOpen, argv[2])
975 ir_tcl_strdup (interp, &p->idAuthenticationGroupId, argv[2])
979 ir_tcl_strdup (interp, &p->idAuthenticationUserId, argv[3])
983 ir_tcl_strdup (interp, &p->idAuthenticationPassword, argv[4])
988 if (p->idAuthenticationOpen)
989 Tcl_AppendElement (interp, p->idAuthenticationOpen);
990 else if (p->idAuthenticationGroupId)
992 Tcl_AppendElement (interp, p->idAuthenticationGroupId);
993 Tcl_AppendElement (interp, p->idAuthenticationUserId);
994 Tcl_AppendElement (interp, p->idAuthenticationPassword);
1000 * do_connect: connect method on IR object
1002 static int do_connect (void *obj, Tcl_Interp *interp,
1003 int argc, char **argv)
1015 interp->result = "already connected";
1018 if (!strcmp (p->comstackType, "tcpip"))
1020 p->cs_link = cs_create (tcpip_type, CS_BLOCK, p->protocol_type);
1021 addr = tcpip_strtoaddr (argv[2]);
1024 interp->result = "tcpip_strtoaddr fail";
1027 logf (LOG_DEBUG, "tcp/ip connect %s", argv[2]);
1029 else if (!strcmp (p->comstackType, "mosi"))
1032 p->cs_link = cs_create (mosi_type, CS_BLOCK, p->protocol_type);
1033 addr = mosi_strtoaddr (argv[2]);
1036 interp->result = "mosi_strtoaddr fail";
1039 logf (LOG_DEBUG, "mosi connect %s", argv[2]);
1041 interp->result = "MOSI support not there";
1047 Tcl_AppendResult (interp, "Bad comstack type: ",
1048 p->comstackType, NULL);
1051 if (ir_tcl_strdup (interp, &p->hostname, argv[2]) == TCL_ERROR)
1053 #if IRTCL_GENERIC_FILES
1055 p->csFile = Tcl_GetFile (cs_fileno(p->cs_link), TCL_WIN_SOCKET);
1057 p->csFile = Tcl_GetFile (cs_fileno(p->cs_link), TCL_UNIX_FD);
1060 if ((r=cs_connect (p->cs_link, addr)) < 0)
1062 interp->result = "connect fail";
1063 do_disconnect (p, NULL, 2, NULL);
1066 logf(LOG_DEBUG, "cs_connect() returned %d fd=%d", r,
1067 cs_fileno(p->cs_link));
1068 p->eventType = "connect";
1069 #if IRTCL_GENERIC_FILES
1070 ir_select_add (p->csFile, p);
1072 ir_select_add (cs_fileno (p->cs_link), p);
1076 #if IRTCL_GENERIC_FILES
1077 ir_select_add_write (p->csFile, p);
1079 ir_select_add_write (cs_fileno (p->cs_link), p);
1081 p->state = IR_TCL_R_Connecting;
1085 p->state = IR_TCL_R_Idle;
1087 IrTcl_eval (p->interp, p->callback);
1091 Tcl_AppendResult (interp, p->hostname, NULL);
1096 * do_disconnect: disconnect method on IR object
1098 static int do_disconnect (void *obj, Tcl_Interp *interp,
1099 int argc, char **argv)
1105 p->state = IR_TCL_R_Idle;
1106 p->eventType = NULL;
1109 #if IRTCL_GENERIC_FILES
1116 logf(LOG_DEBUG, "Closing connection to %s", p->hostname);
1119 #if IRTCL_GENERIC_FILES
1120 ir_select_remove_write (p->csFile, p);
1121 ir_select_remove (p->csFile, p);
1123 ir_select_remove_write (cs_fileno (p->cs_link), p);
1124 ir_select_remove (cs_fileno (p->cs_link), p);
1127 odr_reset (p->odr_in);
1129 assert (p->cs_link);
1130 cs_close (p->cs_link);
1132 #if IRTCL_GENERIC_FILES
1133 Tcl_FreeFile (p->csFile);
1137 ODR_MASK_ZERO (&p->options);
1138 ODR_MASK_SET (&p->options, 0);
1139 ODR_MASK_SET (&p->options, 1);
1140 ODR_MASK_SET (&p->options, 4);
1141 ODR_MASK_SET (&p->options, 7);
1142 ODR_MASK_SET (&p->options, 14);
1144 ODR_MASK_ZERO (&p->protocolVersion);
1145 ODR_MASK_SET (&p->protocolVersion, 0);
1146 ODR_MASK_SET (&p->protocolVersion, 1);
1149 assert (!p->cs_link);
1154 * do_comstack: Set/get comstack method on IR object
1156 static int do_comstack (void *o, Tcl_Interp *interp,
1157 int argc, char **argv)
1162 return ir_tcl_strdup (interp, &obj->comstackType, "tcpip");
1163 else if (argc == -1)
1164 return ir_tcl_strdel (interp, &obj->comstackType);
1167 free (obj->comstackType);
1168 if (ir_tcl_strdup (interp, &obj->comstackType, argv[2]) == TCL_ERROR)
1171 Tcl_AppendElement (interp, obj->comstackType);
1176 * do_logLevel: Set log level
1178 static int do_logLevel (void *o, Tcl_Interp *interp,
1179 int argc, char **argv)
1184 log_init (log_mask_str (argv[2]), "", NULL);
1186 log_init (log_mask_str (argv[2]), argv[3], NULL);
1188 log_init (log_mask_str (argv[2]), argv[3], argv[4]);
1194 * do_eventType: Return type of last event
1196 static int do_eventType (void *obj, Tcl_Interp *interp,
1197 int argc, char **argv)
1203 p->eventType = NULL;
1206 Tcl_AppendElement (interp, p->eventType ? p->eventType : "");
1212 * do_callback: add callback
1214 static int do_callback (void *obj, Tcl_Interp *interp,
1215 int argc, char **argv)
1224 else if (argc == -1)
1225 return ir_tcl_strdel (interp, &p->callback);
1231 if (ir_tcl_strdup (interp, &p->callback, argv[2]) == TCL_ERROR)
1241 * do_failback: add error handle callback
1243 static int do_failback (void *obj, Tcl_Interp *interp,
1244 int argc, char **argv)
1253 else if (argc == -1)
1254 return ir_tcl_strdel (interp, &p->failback);
1260 if (ir_tcl_strdup (interp, &p->failback, argv[2]) == TCL_ERROR)
1270 * do_initResponse: add init response handler
1272 static int do_initResponse (void *obj, Tcl_Interp *interp,
1273 int argc, char **argv)
1279 p->initResponse = NULL;
1282 else if (argc == -1)
1283 return ir_tcl_strdel (interp, &p->initResponse);
1286 free (p->initResponse);
1289 if (ir_tcl_strdup (interp, &p->initResponse, argv[2]) == TCL_ERROR)
1293 p->initResponse = NULL;
1298 * do_protocol: Set/get protocol method on IR object
1300 static int do_protocol (void *o, Tcl_Interp *interp, int argc, char **argv)
1306 p->protocol_type = PROTO_Z3950;
1311 if (!strcmp (argv[2], "Z39"))
1312 p->protocol_type = PROTO_Z3950;
1313 else if (!strcmp (argv[2], "SR"))
1314 p->protocol_type = PROTO_SR;
1317 Tcl_AppendResult (interp, "Bad protocol: ", argv[2], NULL);
1322 switch (p->protocol_type)
1325 Tcl_AppendElement (interp, "Z39");
1328 Tcl_AppendElement (interp, "SR");
1335 * do_triggerResourceControl:
1337 static int do_triggerResourceControl (void *obj, Tcl_Interp *interp,
1338 int argc, char **argv)
1342 Z_TriggerResourceControlRequest *req;
1343 bool_t is_false = 0;
1349 interp->result = "triggerResourceControl: not connected";
1352 apdu = zget_APDU (p->odr_out, Z_APDU_triggerResourceControlRequest);
1353 req = apdu->u.triggerResourceControlRequest;
1354 *req->requestedAction = Z_TriggerResourceCtrl_cancel;
1355 req->resultSetWanted = &is_false;
1357 return ir_tcl_send_APDU (interp, p, apdu, "triggerResourceControl",
1362 * do_databaseNames: specify database names
1364 static int do_databaseNames (void *obj, Tcl_Interp *interp,
1365 int argc, char **argv)
1368 IrTcl_SetCObj *p = obj;
1372 for (i=0; i<p->num_databaseNames; i++)
1373 free (p->databaseNames[i]);
1374 free (p->databaseNames);
1378 p->num_databaseNames = 0;
1379 p->databaseNames = NULL;
1384 for (i=0; i<p->num_databaseNames; i++)
1385 Tcl_AppendElement (interp, p->databaseNames[i]);
1388 if (p->databaseNames)
1390 for (i=0; i<p->num_databaseNames; i++)
1391 free (p->databaseNames[i]);
1392 free (p->databaseNames);
1394 p->num_databaseNames = argc - 2;
1396 ir_tcl_malloc (sizeof(*p->databaseNames) * p->num_databaseNames);
1397 for (i=0; i<p->num_databaseNames; i++)
1399 if (ir_tcl_strdup (interp, &p->databaseNames[i], argv[2+i])
1407 * do_replaceIndicator: Set/get replace Set indicator
1409 static int do_replaceIndicator (void *obj, Tcl_Interp *interp,
1410 int argc, char **argv)
1412 IrTcl_SetCObj *p = obj;
1416 p->replaceIndicator = 1;
1419 return get_set_int (&p->replaceIndicator, interp, argc, argv);
1423 * do_queryType: Set/Get query method
1425 static int do_queryType (void *obj, Tcl_Interp *interp,
1426 int argc, char **argv)
1428 IrTcl_SetCObj *p = obj;
1431 return ir_tcl_strdup (interp, &p->queryType, "rpn");
1432 else if (argc == -1)
1433 return ir_tcl_strdel (interp, &p->queryType);
1436 free (p->queryType);
1437 if (ir_tcl_strdup (interp, &p->queryType, argv[2]) == TCL_ERROR)
1440 Tcl_AppendResult (interp, p->queryType, NULL);
1445 * do_userInformationField: Get User information field
1447 static int do_userInformationField (void *obj, Tcl_Interp *interp,
1448 int argc, char **argv)
1454 p->userInformationField = NULL;
1457 else if (argc == -1)
1458 return ir_tcl_strdel (interp, &p->userInformationField);
1459 Tcl_AppendResult (interp, p->userInformationField, NULL);
1464 * do_smallSetUpperBound: Set/get small set upper bound
1466 static int do_smallSetUpperBound (void *o, Tcl_Interp *interp,
1467 int argc, char **argv)
1469 IrTcl_SetCObj *p = o;
1473 p->smallSetUpperBound = 0;
1476 return get_set_int (&p->smallSetUpperBound, interp, argc, argv);
1480 * do_largeSetLowerBound: Set/get large set lower bound
1482 static int do_largeSetLowerBound (void *o, Tcl_Interp *interp,
1483 int argc, char **argv)
1485 IrTcl_SetCObj *p = o;
1489 p->largeSetLowerBound = 2;
1492 return get_set_int (&p->largeSetLowerBound, interp, argc, argv);
1496 * do_mediumSetPresentNumber: Set/get large set lower bound
1498 static int do_mediumSetPresentNumber (void *o, Tcl_Interp *interp,
1499 int argc, char **argv)
1501 IrTcl_SetCObj *p = o;
1505 p->mediumSetPresentNumber = 0;
1508 return get_set_int (&p->mediumSetPresentNumber, interp, argc, argv);
1512 * do_referenceId: Set/Get referenceId
1514 static int do_referenceId (void *obj, Tcl_Interp *interp,
1515 int argc, char **argv)
1517 IrTcl_SetCObj *p = obj;
1521 p->referenceId = NULL;
1524 else if (argc == -1)
1525 return ir_tcl_strdel (interp, &p->referenceId);
1528 free (p->referenceId);
1529 if (ir_tcl_strdup (interp, &p->referenceId, argv[2]) == TCL_ERROR)
1532 Tcl_AppendResult (interp, p->referenceId, NULL);
1537 * do_preferredRecordSyntax: Set/get preferred record syntax
1539 static int do_preferredRecordSyntax (void *obj, Tcl_Interp *interp,
1540 int argc, char **argv)
1542 IrTcl_SetCObj *p = obj;
1546 p->preferredRecordSyntax = NULL;
1549 else if (argc == -1)
1551 free (p->preferredRecordSyntax);
1552 p->preferredRecordSyntax = NULL;
1557 free (p->preferredRecordSyntax);
1558 p->preferredRecordSyntax = NULL;
1559 if (argv[2][0] && (p->preferredRecordSyntax =
1560 ir_tcl_malloc (sizeof(*p->preferredRecordSyntax))))
1561 *p->preferredRecordSyntax = IrTcl_getRecordSyntaxVal (argv[2]);
1565 Tcl_AppendElement (interp, IrTcl_getRecordSyntaxStr
1566 (*p->preferredRecordSyntax));
1573 * do_elementSetNames: Set/Get element Set Names
1575 static int do_elementSetNames (void *obj, Tcl_Interp *interp,
1576 int argc, char **argv)
1578 IrTcl_SetCObj *p = obj;
1582 p->elementSetNames = NULL;
1585 else if (argc == -1)
1586 return ir_tcl_strdel (interp, &p->elementSetNames);
1589 free (p->elementSetNames);
1590 if (ir_tcl_strdup (interp, &p->elementSetNames, argv[2]) == TCL_ERROR)
1593 Tcl_AppendResult (interp, p->elementSetNames, NULL);
1598 * do_smallSetElementSetNames: Set/Get small Set Element Set Names
1600 static int do_smallSetElementSetNames (void *obj, Tcl_Interp *interp,
1601 int argc, char **argv)
1603 IrTcl_SetCObj *p = obj;
1607 p->smallSetElementSetNames = NULL;
1610 else if (argc == -1)
1611 return ir_tcl_strdel (interp, &p->smallSetElementSetNames);
1614 free (p->smallSetElementSetNames);
1615 if (ir_tcl_strdup (interp, &p->smallSetElementSetNames,
1616 argv[2]) == TCL_ERROR)
1619 Tcl_AppendResult (interp, p->smallSetElementSetNames, NULL);
1624 * do_mediumSetElementSetNames: Set/Get medium Set Element Set Names
1626 static int do_mediumSetElementSetNames (void *obj, Tcl_Interp *interp,
1627 int argc, char **argv)
1629 IrTcl_SetCObj *p = obj;
1633 p->mediumSetElementSetNames = NULL;
1636 else if (argc == -1)
1637 return ir_tcl_strdel (interp, &p->mediumSetElementSetNames);
1640 free (p->mediumSetElementSetNames);
1641 if (ir_tcl_strdup (interp, &p->mediumSetElementSetNames,
1642 argv[2]) == TCL_ERROR)
1645 Tcl_AppendResult (interp, p->mediumSetElementSetNames, NULL);
1649 static IrTcl_Method ir_method_tab[] = {
1650 { 1, "comstack", do_comstack },
1651 { 1, "protocol", do_protocol },
1652 { 0, "failback", do_failback },
1653 { 0, "failInfo", do_failInfo },
1654 { 0, "apduInfo", do_apduInfo },
1655 { 0, "logLevel", do_logLevel },
1657 { 0, "eventType", do_eventType },
1658 { 1, "connect", do_connect },
1659 { 0, "protocolVersion", do_protocolVersion },
1660 { 1, "preferredMessageSize", do_preferredMessageSize },
1661 { 1, "maximumRecordSize", do_maximumRecordSize },
1662 { 1, "implementationName", do_implementationName },
1663 { 1, "implementationId", do_implementationId },
1664 { 1, "implementationVersion", do_implementationVersion },
1665 { 0, "targetImplementationName", do_targetImplementationName },
1666 { 0, "targetImplementationId", do_targetImplementationId },
1667 { 0, "targetImplementationVersion", do_targetImplementationVersion },
1668 { 0, "userInformationField", do_userInformationField },
1669 { 1, "idAuthentication", do_idAuthentication },
1670 { 0, "options", do_options },
1671 { 0, "init", do_init_request },
1672 { 0, "initResult", do_initResult },
1673 { 0, "disconnect", do_disconnect },
1674 { 0, "callback", do_callback },
1675 { 0, "initResponse", do_initResponse },
1676 { 0, "triggerResourceControl", do_triggerResourceControl },
1677 { 0, "initResponse", do_initResponse },
1681 static IrTcl_Method ir_set_c_method_tab[] = {
1682 { 0, "databaseNames", do_databaseNames},
1683 { 0, "replaceIndicator", do_replaceIndicator},
1684 { 0, "queryType", do_queryType },
1685 { 0, "preferredRecordSyntax", do_preferredRecordSyntax },
1686 { 0, "smallSetUpperBound", do_smallSetUpperBound},
1687 { 0, "largeSetLowerBound", do_largeSetLowerBound},
1688 { 0, "mediumSetPresentNumber", do_mediumSetPresentNumber},
1689 { 0, "referenceId", do_referenceId },
1690 { 0, "elementSetNames", do_elementSetNames },
1691 { 0, "smallSetElementSetNames", do_smallSetElementSetNames },
1692 { 0, "mediumSetElementSetNames", do_mediumSetElementSetNames },
1697 * ir_obj_method: IR Object methods
1699 static int ir_obj_method (ClientData clientData, Tcl_Interp *interp,
1700 int argc, char **argv)
1702 IrTcl_Methods tab[3];
1703 IrTcl_Obj *p = clientData;
1706 return ir_method_r (clientData, interp, argc, argv, ir_method_tab);
1708 tab[0].tab = ir_method_tab;
1710 tab[1].tab = ir_set_c_method_tab;
1711 tab[1].obj = &p->set_inher;
1714 return ir_method (interp, argc, argv, tab);
1718 * ir_obj_delete: IR Object disposal
1720 static void ir_obj_delete (ClientData clientData)
1722 IrTcl_Obj *obj = clientData;
1723 IrTcl_Methods tab[3];
1726 if (obj->ref_count > 0)
1728 assert (obj->ref_count == 0);
1730 logf (LOG_DEBUG, "ir object delete");
1731 tab[0].tab = ir_method_tab;
1733 tab[1].tab = ir_set_c_method_tab;
1734 tab[1].obj = &obj->set_inher;
1737 ir_method (NULL, -1, NULL, tab);
1740 odr_destroy (obj->odr_in);
1741 odr_destroy (obj->odr_out);
1742 odr_destroy (obj->odr_pr);
1747 * ir_obj_mk: IR Object creation
1749 static int ir_obj_mk (ClientData clientData, Tcl_Interp *interp,
1750 int argc, char **argv)
1752 IrTcl_Methods tab[3];
1760 interp->result = "wrong # args";
1763 obj = ir_tcl_malloc (sizeof(*obj));
1766 obj->bibset = ccl_qual_mk ();
1767 if ((inf = fopen ("default.bib", "r")))
1769 ccl_qual_file (obj->bibset, inf);
1774 logf (LOG_DEBUG, "ir object create %s", argv[1]);
1775 obj->odr_in = odr_createmem (ODR_DECODE);
1776 obj->odr_out = odr_createmem (ODR_ENCODE);
1777 obj->odr_pr = odr_createmem (ODR_PRINT);
1778 obj->state = IR_TCL_R_Idle;
1779 obj->interp = interp;
1783 obj->request_queue = NULL;
1785 tab[0].tab = ir_method_tab;
1787 tab[1].tab = ir_set_c_method_tab;
1788 tab[1].obj = &obj->set_inher;
1791 if (ir_method (interp, 0, NULL, tab) == TCL_ERROR)
1793 Tcl_CreateCommand (interp, argv[1], ir_obj_method,
1794 (ClientData) obj, ir_obj_delete);
1798 /* ------------------------------------------------------- */
1800 * do_search: Do search request
1802 static int do_search (void *o, Tcl_Interp *interp, int argc, char **argv)
1804 Z_SearchRequest *req;
1808 IrTcl_SetObj *obj = o;
1819 interp->result = "wrong # args";
1822 if (!obj->set_inher.num_databaseNames)
1824 interp->result = "no databaseNames";
1829 interp->result = "search: not connected";
1832 apdu = zget_APDU (p->odr_out, Z_APDU_searchRequest);
1833 req = apdu->u.searchRequest;
1837 bib1.proto = p->protocol_type;
1838 bib1.oclass = CLASS_ATTSET;
1839 bib1.value = VAL_BIB1;
1841 set_referenceId (p->odr_out, &req->referenceId,
1842 obj->set_inher.referenceId);
1844 req->smallSetUpperBound = &obj->set_inher.smallSetUpperBound;
1845 req->largeSetLowerBound = &obj->set_inher.largeSetLowerBound;
1846 req->mediumSetPresentNumber = &obj->set_inher.mediumSetPresentNumber;
1847 req->replaceIndicator = &obj->set_inher.replaceIndicator;
1848 req->resultSetName = obj->setName ? obj->setName : "Default";
1849 logf (LOG_DEBUG, "Search, resultSetName %s", req->resultSetName);
1850 req->num_databaseNames = obj->set_inher.num_databaseNames;
1851 req->databaseNames = obj->set_inher.databaseNames;
1852 for (r=0; r < obj->set_inher.num_databaseNames; r++)
1853 logf (LOG_DEBUG, " Database %s", obj->set_inher.databaseNames[r]);
1854 if (obj->set_inher.preferredRecordSyntax)
1856 struct oident ident;
1858 ident.proto = p->protocol_type;
1859 ident.oclass = CLASS_RECSYN;
1860 ident.value = *obj->set_inher.preferredRecordSyntax;
1861 logf (LOG_DEBUG, "Preferred record syntax is %d", ident.value);
1862 req->preferredRecordSyntax = odr_oiddup (p->odr_out,
1863 oid_getoidbyent (&ident));
1866 req->preferredRecordSyntax = 0;
1868 if (obj->set_inher.smallSetElementSetNames &&
1869 *obj->set_inher.smallSetElementSetNames)
1871 Z_ElementSetNames *esn = odr_malloc (p->odr_out, sizeof(*esn));
1873 esn->which = Z_ElementSetNames_generic;
1874 esn->u.generic = obj->set_inher.smallSetElementSetNames;
1875 req->smallSetElementSetNames = esn;
1878 req->smallSetElementSetNames = NULL;
1880 if (obj->set_inher.mediumSetElementSetNames &&
1881 *obj->set_inher.mediumSetElementSetNames)
1883 Z_ElementSetNames *esn = odr_malloc (p->odr_out, sizeof(*esn));
1885 esn->which = Z_ElementSetNames_generic;
1886 esn->u.generic = obj->set_inher.mediumSetElementSetNames;
1887 req->mediumSetElementSetNames = esn;
1890 req->mediumSetElementSetNames = NULL;
1892 req->query = &query;
1894 if (!strcmp (obj->set_inher.queryType, "rpn"))
1896 Z_RPNQuery *RPNquery;
1898 RPNquery = p_query_rpn (p->odr_out, argv[2]);
1901 Tcl_AppendResult (interp, "Syntax error in query", NULL);
1904 RPNquery->attributeSetId = oid_getoidbyent (&bib1);
1905 query.which = Z_Query_type_1;
1906 query.u.type_1 = RPNquery;
1907 logf (LOG_DEBUG, "RPN");
1910 else if (!strcmp (obj->set_inher.queryType, "cclrpn"))
1914 struct ccl_rpn_node *rpn;
1915 Z_RPNQuery *RPNquery;
1917 rpn = ccl_find_str(p->bibset, argv[2], &error, &pos);
1920 Tcl_AppendResult (interp, "CCL error: ",
1921 ccl_err_msg(error), NULL);
1924 ccl_pr_tree (rpn, stderr);
1925 fprintf (stderr, "\n");
1926 assert((RPNquery = ccl_rpn_query(rpn)));
1927 RPNquery->attributeSetId = oid_getoidbyent (&bib1);
1928 query.which = Z_Query_type_1;
1929 query.u.type_1 = RPNquery;
1930 logf (LOG_DEBUG, "CCLRPN");
1933 else if (!strcmp (obj->set_inher.queryType, "ccl"))
1935 query.which = Z_Query_type_2;
1936 query.u.type_2 = &ccl_query;
1937 ccl_query.buf = (unsigned char *) argv[2];
1938 ccl_query.len = strlen (argv[2]);
1939 logf (LOG_DEBUG, "CCL");
1943 interp->result = "unknown query method";
1946 return ir_tcl_send_APDU (interp, p, apdu, "search", *argv);
1950 * do_searchResponse: add search response handler
1952 static int do_searchResponse (void *o, Tcl_Interp *interp,
1953 int argc, char **argv)
1955 IrTcl_SetObj *obj = o;
1959 obj->searchResponse = NULL;
1962 else if (argc == -1)
1963 return ir_tcl_strdel (interp, &obj->searchResponse);
1966 free (obj->searchResponse);
1969 if (ir_tcl_strdup (interp, &obj->searchResponse, argv[2])
1974 obj->searchResponse = NULL;
1980 * do_presentResponse: add present response handler
1982 static int do_presentResponse (void *o, Tcl_Interp *interp,
1983 int argc, char **argv)
1985 IrTcl_SetObj *obj = o;
1989 obj->presentResponse = NULL;
1992 else if (argc == -1)
1993 return ir_tcl_strdel (interp, &obj->presentResponse);
1996 free (obj->presentResponse);
1999 if (ir_tcl_strdup (interp, &obj->presentResponse, argv[2])
2004 obj->presentResponse = NULL;
2010 * do_resultCount: Get number of hits
2012 static int do_resultCount (void *o, Tcl_Interp *interp,
2013 int argc, char **argv)
2015 IrTcl_SetObj *obj = o;
2019 obj->resultCount = 0;
2022 return get_set_int (&obj->resultCount, interp, argc, argv);
2026 * do_searchStatus: Get search status (after search response)
2028 static int do_searchStatus (void *o, Tcl_Interp *interp,
2029 int argc, char **argv)
2031 IrTcl_SetObj *obj = o;
2035 return get_set_int (&obj->searchStatus, interp, argc, argv);
2039 * do_presentStatus: Get search status (after search/present response)
2041 static int do_presentStatus (void *o, Tcl_Interp *interp,
2042 int argc, char **argv)
2044 IrTcl_SetObj *obj = o;
2048 return get_set_int (&obj->presentStatus, interp, argc, argv);
2052 * do_nextResultSetPosition: Get next result set position
2053 * (after search/present response)
2055 static int do_nextResultSetPosition (void *o, Tcl_Interp *interp,
2056 int argc, char **argv)
2058 IrTcl_SetObj *obj = o;
2062 obj->nextResultSetPosition = 0;
2065 return get_set_int (&obj->nextResultSetPosition, interp, argc, argv);
2069 * do_setName: Set result Set name
2071 static int do_setName (void *o, Tcl_Interp *interp,
2072 int argc, char **argv)
2074 IrTcl_SetObj *obj = o;
2077 return ir_tcl_strdup (interp, &obj->setName, "Default");
2078 else if (argc == -1)
2079 return ir_tcl_strdel (interp, &obj->setName);
2082 free (obj->setName);
2083 if (ir_tcl_strdup (interp, &obj->setName, argv[2])
2087 Tcl_AppendElement (interp, obj->setName);
2092 * do_numberOfRecordsReturned: Get number of records returned
2094 static int do_numberOfRecordsReturned (void *o, Tcl_Interp *interp,
2095 int argc, char **argv)
2097 IrTcl_SetObj *obj = o;
2101 obj->numberOfRecordsReturned = 0;
2104 return get_set_int (&obj->numberOfRecordsReturned, interp, argc, argv);
2108 * do_type: Return type (if any) at position.
2110 static int do_type (void *o, Tcl_Interp *interp, int argc, char **argv)
2112 IrTcl_SetObj *obj = o;
2114 IrTcl_RecordList *rl;
2118 obj->record_list = NULL;
2121 else if (argc == -1)
2123 delete_IR_records (obj);
2128 sprintf (interp->result, "wrong # args");
2131 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2133 rl = find_IR_record (obj, offset);
2136 logf (LOG_DEBUG, "No record at position %d", offset);
2141 case Z_NamePlusRecord_databaseRecord:
2142 interp->result = "DB";
2144 case Z_NamePlusRecord_surrogateDiagnostic:
2145 interp->result = "SD";
2153 * do_recordType: Return record type (if any) at position.
2155 static int do_recordType (void *o, Tcl_Interp *interp, int argc, char **argv)
2157 IrTcl_SetObj *obj = o;
2159 IrTcl_RecordList *rl;
2165 else if (argc == -1)
2171 sprintf (interp->result, "wrong # args");
2174 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2176 rl = find_IR_record (obj, offset);
2179 if (rl->which != Z_NamePlusRecord_databaseRecord)
2181 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2184 Tcl_AppendElement (interp, (char*)
2185 IrTcl_getRecordSyntaxStr (rl->u.dbrec.type));
2190 * set record elements (for record extraction)
2192 static int do_recordElements (void *o, Tcl_Interp *interp,
2193 int argc, char **argv)
2195 IrTcl_SetObj *obj = o;
2199 obj->recordElements = NULL;
2202 else if (argc == -1)
2203 return ir_tcl_strdel (NULL, &obj->recordElements);
2206 sprintf (interp->result, "wrong # args");
2211 free (obj->recordElements);
2212 return ir_tcl_strdup (NULL, &obj->recordElements,
2213 (*argv[2] ? argv[2] : NULL));
2215 Tcl_AppendResult (interp, obj->recordElements, NULL);
2222 static int ir_diagResult (Tcl_Interp *interp, IrTcl_Diagnostic *list, int num)
2228 for (i = 0; i<num; i++)
2230 logf (LOG_DEBUG, "Diagnostic, code %d", list[i].condition);
2231 sprintf (buf, "%d", list[i].condition);
2232 Tcl_AppendElement (interp, buf);
2233 cp = diagbib1_str (list[i].condition);
2235 Tcl_AppendElement (interp, (char*) cp);
2237 Tcl_AppendElement (interp, "");
2238 if (list[i].addinfo)
2239 Tcl_AppendElement (interp, (char*) list[i].addinfo);
2241 Tcl_AppendElement (interp, "");
2247 * do_diag: Return diagnostic record info
2249 static int do_diag (void *o, Tcl_Interp *interp, int argc, char **argv)
2251 IrTcl_SetObj *obj = o;
2253 IrTcl_RecordList *rl;
2259 sprintf (interp->result, "wrong # args");
2262 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2264 rl = find_IR_record (obj, offset);
2267 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2270 if (rl->which != Z_NamePlusRecord_surrogateDiagnostic)
2272 Tcl_AppendResult (interp, "No Diagnostic record at #", argv[2], NULL);
2275 return ir_diagResult (interp, rl->u.surrogateDiagnostics.list,
2276 rl->u.surrogateDiagnostics.num);
2280 * do_getMarc: Get ISO2709 Record lines/fields
2282 static int do_getMarc (void *o, Tcl_Interp *interp, int argc, char **argv)
2284 IrTcl_SetObj *obj = o;
2286 IrTcl_RecordList *rl;
2292 sprintf (interp->result, "wrong # args");
2295 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2297 rl = find_IR_record (obj, offset);
2300 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2303 if (rl->which != Z_NamePlusRecord_databaseRecord)
2305 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2308 return ir_tcl_get_marc (interp, rl->u.dbrec.buf, argc, argv);
2312 * do_getSutrs: Get SUTRS Record
2314 static int do_getSutrs (void *o, Tcl_Interp *interp, int argc, char **argv)
2316 IrTcl_SetObj *obj = o;
2318 IrTcl_RecordList *rl;
2324 sprintf (interp->result, "wrong # args");
2327 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2329 rl = find_IR_record (obj, offset);
2332 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2335 if (rl->which != Z_NamePlusRecord_databaseRecord)
2337 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2340 if (rl->u.dbrec.type != VAL_SUTRS)
2342 Tcl_AppendElement (interp, rl->u.dbrec.buf);
2348 * do_getGrs: Get a GRS1 Record
2350 static int do_getGrs (void *o, Tcl_Interp *interp, int argc, char **argv)
2352 IrTcl_SetObj *obj = o;
2354 IrTcl_RecordList *rl;
2360 sprintf (interp->result, "wrong # args");
2363 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2365 rl = find_IR_record (obj, offset);
2368 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2371 if (rl->which != Z_NamePlusRecord_databaseRecord)
2373 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2376 if (rl->u.dbrec.type != VAL_GRS1)
2378 return ir_tcl_get_grs (interp, rl->u.dbrec.u.grs1, argc, argv);
2383 * do_responseStatus: Return response status (present or search)
2385 static int do_responseStatus (void *o, Tcl_Interp *interp,
2386 int argc, char **argv)
2388 IrTcl_SetObj *obj = o;
2392 obj->recordFlag = 0;
2393 obj->nonSurrogateDiagnosticNum = 0;
2394 obj->nonSurrogateDiagnosticList = NULL;
2397 else if (argc == -1)
2399 ir_deleteDiags (&obj->nonSurrogateDiagnosticList,
2400 &obj->nonSurrogateDiagnosticNum);
2403 if (!obj->recordFlag)
2405 Tcl_AppendElement (interp, "OK");
2410 case Z_Records_DBOSD:
2411 Tcl_AppendElement (interp, "DBOSD");
2414 Tcl_AppendElement (interp, "NSD");
2415 return ir_diagResult (interp, obj->nonSurrogateDiagnosticList,
2416 obj->nonSurrogateDiagnosticNum);
2422 * do_present: Perform Present Request
2425 static int do_present (void *o, Tcl_Interp *interp, int argc, char **argv)
2427 IrTcl_SetObj *obj = o;
2430 Z_PresentRequest *req;
2438 if (Tcl_GetInt (interp, argv[2], &start) == TCL_ERROR)
2445 if (Tcl_GetInt (interp, argv[3], &number) == TCL_ERROR)
2453 interp->result = "present: not connected";
2458 obj->number = number;
2460 apdu = zget_APDU (p->odr_out, Z_APDU_presentRequest);
2461 req = apdu->u.presentRequest;
2463 set_referenceId (p->odr_out, &req->referenceId,
2464 obj->set_inher.referenceId);
2466 req->resultSetId = obj->setName ? obj->setName : "Default";
2468 req->resultSetStartPoint = &start;
2469 req->numberOfRecordsRequested = &number;
2470 if (obj->set_inher.preferredRecordSyntax)
2472 struct oident ident;
2474 ident.proto = p->protocol_type;
2475 ident.oclass = CLASS_RECSYN;
2476 ident.value = *obj->set_inher.preferredRecordSyntax;
2477 logf (LOG_DEBUG, "Preferred record syntax is %d", ident.value);
2478 req->preferredRecordSyntax = odr_oiddup (p->odr_out,
2479 oid_getoidbyent (&ident));
2482 req->preferredRecordSyntax = 0;
2484 if (obj->set_inher.elementSetNames && *obj->set_inher.elementSetNames)
2486 Z_ElementSetNames *esn = odr_malloc (p->odr_out, sizeof(*esn));
2487 Z_RecordComposition *compo = odr_malloc (p->odr_out, sizeof(*compo));
2489 esn->which = Z_ElementSetNames_generic;
2490 esn->u.generic = obj->set_inher.elementSetNames;
2492 req->recordComposition = compo;
2493 compo->which = Z_RecordComp_simple;
2494 compo->u.simple = esn;
2497 req->recordComposition = NULL;
2498 return ir_tcl_send_APDU (interp, p, apdu, "present", *argv);
2502 * do_loadFile: Load result set from file
2505 static int do_loadFile (void *o, Tcl_Interp *interp,
2506 int argc, char **argv)
2508 IrTcl_SetObj *setobj = o;
2518 interp->result = "wrong # args";
2521 inf = fopen (argv[2], "r");
2524 Tcl_AppendResult (interp, "Cannot open ", argv[2], NULL);
2527 while ((buf = ir_tcl_fread_marc (inf, &size)))
2529 IrTcl_RecordList *rl;
2531 rl = new_IR_record (setobj, no, Z_NamePlusRecord_databaseRecord, "F");
2532 rl->u.dbrec.type = VAL_USMARC;
2533 rl->u.dbrec.buf = buf;
2534 rl->u.dbrec.size = size;
2537 setobj->numberOfRecordsReturned = no-1;
2542 static IrTcl_Method ir_set_method_tab[] = {
2543 { 0, "search", do_search },
2544 { 0, "searchResponse", do_searchResponse },
2545 { 0, "presentResponse", do_presentResponse },
2546 { 0, "searchStatus", do_searchStatus },
2547 { 0, "presentStatus", do_presentStatus },
2548 { 0, "nextResultSetPosition", do_nextResultSetPosition },
2549 { 0, "setName", do_setName },
2550 { 0, "resultCount", do_resultCount },
2551 { 0, "numberOfRecordsReturned", do_numberOfRecordsReturned },
2552 { 0, "present", do_present },
2553 { 0, "type", do_type },
2554 { 0, "getMarc", do_getMarc },
2555 { 0, "getSutrs", do_getSutrs },
2556 { 0, "getGrs", do_getGrs },
2557 { 0, "recordType", do_recordType },
2558 { 0, "recordElements", do_recordElements },
2559 { 0, "diag", do_diag },
2560 { 0, "responseStatus", do_responseStatus },
2561 { 0, "loadFile", do_loadFile },
2566 * ir_set_obj_method: IR Set Object methods
2568 static int ir_set_obj_method (ClientData clientData, Tcl_Interp *interp,
2569 int argc, char **argv)
2571 IrTcl_Methods tabs[3];
2572 IrTcl_SetObj *p = clientData;
2576 interp->result = "wrong # args";
2579 tabs[0].tab = ir_set_method_tab;
2581 tabs[1].tab = ir_set_c_method_tab;
2582 tabs[1].obj = &p->set_inher;
2585 return ir_method (interp, argc, argv, tabs);
2589 * ir_set_obj_delete: IR Set Object disposal
2591 static void ir_set_obj_delete (ClientData clientData)
2593 IrTcl_Methods tabs[3];
2594 IrTcl_SetObj *p = clientData;
2596 logf (LOG_DEBUG, "ir set delete");
2598 tabs[0].tab = ir_set_method_tab;
2600 tabs[1].tab = ir_set_c_method_tab;
2601 tabs[1].obj = &p->set_inher;
2604 ir_method (NULL, -1, NULL, tabs);
2610 * ir_set_obj_mk: IR Set Object creation
2612 static int ir_set_obj_mk (ClientData clientData, Tcl_Interp *interp,
2613 int argc, char **argv)
2615 IrTcl_Methods tabs[3];
2618 if (argc < 2 || argc > 3)
2620 interp->result = "wrong # args";
2623 obj = ir_tcl_malloc (sizeof(*obj));
2624 logf (LOG_DEBUG, "ir set create");
2627 Tcl_CmdInfo parent_info;
2632 if (!Tcl_GetCommandInfo (interp, argv[2], &parent_info))
2634 interp->result = "No parent";
2637 obj->parent = (IrTcl_Obj *) parent_info.clientData;
2639 dst = &obj->set_inher;
2640 src = &obj->parent->set_inher;
2642 if ((dst->num_databaseNames = src->num_databaseNames))
2643 dst->databaseNames =
2644 ir_tcl_malloc (sizeof (*dst->databaseNames)
2645 * dst->num_databaseNames);
2647 dst->databaseNames = NULL;
2648 for (i = 0; i < dst->num_databaseNames; i++)
2649 if (ir_tcl_strdup (interp, &dst->databaseNames[i],
2650 src->databaseNames[i]) == TCL_ERROR)
2652 if (ir_tcl_strdup (interp, &dst->queryType, src->queryType)
2656 if (ir_tcl_strdup (interp, &dst->referenceId, src->referenceId)
2660 if (ir_tcl_strdup (interp, &dst->elementSetNames, src->elementSetNames)
2664 if (ir_tcl_strdup (interp, &dst->smallSetElementSetNames,
2665 src->smallSetElementSetNames)
2669 if (ir_tcl_strdup (interp, &dst->mediumSetElementSetNames,
2670 src->mediumSetElementSetNames)
2674 if (src->preferredRecordSyntax &&
2675 (dst->preferredRecordSyntax
2676 = ir_tcl_malloc (sizeof(*dst->preferredRecordSyntax))))
2677 *dst->preferredRecordSyntax = *src->preferredRecordSyntax;
2679 dst->preferredRecordSyntax = NULL;
2680 dst->replaceIndicator = src->replaceIndicator;
2681 dst->smallSetUpperBound = src->smallSetUpperBound;
2682 dst->largeSetLowerBound = src->largeSetLowerBound;
2683 dst->mediumSetPresentNumber = src->mediumSetPresentNumber;
2688 tabs[0].tab = ir_set_method_tab;
2692 if (ir_method (interp, 0, NULL, tabs) == TCL_ERROR)
2695 Tcl_CreateCommand (interp, argv[1], ir_set_obj_method,
2696 (ClientData) obj, ir_set_obj_delete);
2700 /* ------------------------------------------------------- */
2703 * do_scan: Perform scan
2705 static int do_scan (void *o, Tcl_Interp *interp, int argc, char **argv)
2709 IrTcl_ScanObj *obj = o;
2710 IrTcl_Obj *p = obj->parent;
2713 struct ccl_rpn_node *rpn;
2721 interp->result = "wrong # args";
2724 if (!p->set_inher.num_databaseNames)
2726 interp->result = "no databaseNames";
2731 interp->result = "scan: not connected";
2735 bib1.proto = p->protocol_type;
2736 bib1.oclass = CLASS_ATTSET;
2737 bib1.value = VAL_BIB1;
2739 apdu = zget_APDU (p->odr_out, Z_APDU_scanRequest);
2740 req = apdu->u.scanRequest;
2742 set_referenceId (p->odr_out, &req->referenceId, p->set_inher.referenceId);
2743 req->num_databaseNames = p->set_inher.num_databaseNames;
2744 req->databaseNames = p->set_inher.databaseNames;
2745 req->attributeSet = oid_getoidbyent (&bib1);
2748 if (!(req->termListAndStartPoint = p_query_scan (p->odr_out, argv[2])))
2750 Tcl_AppendResult (interp, "Syntax error in query", NULL);
2754 rpn = ccl_find_str(p->bibset, argv[2], &r, &pos);
2757 Tcl_AppendResult (interp, "CCL error: ", ccl_err_msg (r), NULL);
2760 ccl_pr_tree (rpn, stderr);
2761 fprintf (stderr, "\n");
2762 if (!(req->termListAndStartPoint = ccl_scan_query (rpn)))
2765 req->stepSize = &obj->stepSize;
2766 req->numberOfTermsRequested = &obj->numberOfTermsRequested;
2767 req->preferredPositionInResponse = &obj->preferredPositionInResponse;
2768 logf (LOG_DEBUG, "stepSize=%d", *req->stepSize);
2769 logf (LOG_DEBUG, "numberOfTermsRequested=%d",
2770 *req->numberOfTermsRequested);
2771 logf (LOG_DEBUG, "preferredPositionInResponse=%d",
2772 *req->preferredPositionInResponse);
2774 return ir_tcl_send_APDU (interp, p, apdu, "scan", *argv);
2778 * do_scanResponse: add scan response handler
2780 static int do_scanResponse (void *o, Tcl_Interp *interp,
2781 int argc, char **argv)
2783 IrTcl_ScanObj *obj = o;
2787 obj->scanResponse = NULL;
2790 else if (argc == -1)
2791 return ir_tcl_strdel (interp, &obj->scanResponse);
2794 free (obj->scanResponse);
2797 if (ir_tcl_strdup (interp, &obj->scanResponse, argv[2])
2802 obj->scanResponse = NULL;
2808 * do_stepSize: Set/get replace Step Size
2810 static int do_stepSize (void *obj, Tcl_Interp *interp,
2811 int argc, char **argv)
2813 IrTcl_ScanObj *p = obj;
2819 return get_set_int (&p->stepSize, interp, argc, argv);
2823 * do_numberOfTermsRequested: Set/get Number of Terms requested
2825 static int do_numberOfTermsRequested (void *obj, Tcl_Interp *interp,
2826 int argc, char **argv)
2828 IrTcl_ScanObj *p = obj;
2832 p->numberOfTermsRequested = 20;
2835 return get_set_int (&p->numberOfTermsRequested, interp, argc, argv);
2840 * do_preferredPositionInResponse: Set/get preferred Position
2842 static int do_preferredPositionInResponse (void *obj, Tcl_Interp *interp,
2843 int argc, char **argv)
2845 IrTcl_ScanObj *p = obj;
2849 p->preferredPositionInResponse = 1;
2852 return get_set_int (&p->preferredPositionInResponse, interp, argc, argv);
2856 * do_scanStatus: Get scan status
2858 static int do_scanStatus (void *obj, Tcl_Interp *interp,
2859 int argc, char **argv)
2861 IrTcl_ScanObj *p = obj;
2865 return get_set_int (&p->scanStatus, interp, argc, argv);
2869 * do_numberOfEntriesReturned: Get number of Entries returned
2871 static int do_numberOfEntriesReturned (void *obj, Tcl_Interp *interp,
2872 int argc, char **argv)
2874 IrTcl_ScanObj *p = obj;
2878 return get_set_int (&p->numberOfEntriesReturned, interp, argc, argv);
2882 * do_positionOfTerm: Get position of Term
2884 static int do_positionOfTerm (void *obj, Tcl_Interp *interp,
2885 int argc, char **argv)
2887 IrTcl_ScanObj *p = obj;
2891 return get_set_int (&p->positionOfTerm, interp, argc, argv);
2895 * do_scanLine: get Scan Line (surrogate or normal) after response
2897 static int do_scanLine (void *obj, Tcl_Interp *interp, int argc, char **argv)
2899 IrTcl_ScanObj *p = obj;
2905 p->entries_flag = 0;
2907 p->nonSurrogateDiagnosticNum = 0;
2908 p->nonSurrogateDiagnosticList = 0;
2911 else if (argc == -1)
2913 p->entries_flag = 0;
2914 /* release entries */
2917 ir_deleteDiags (&p->nonSurrogateDiagnosticList,
2918 &p->nonSurrogateDiagnosticNum);
2923 interp->result = "wrong # args";
2926 if (Tcl_GetInt (interp, argv[2], &i) == TCL_ERROR)
2928 if (!p->entries_flag || p->which != Z_ListEntries_entries || !p->entries
2929 || i >= p->num_entries || i < 0)
2931 switch (p->entries[i].which)
2933 case Z_Entry_termInfo:
2934 Tcl_AppendElement (interp, "T");
2935 if (p->entries[i].u.term.buf)
2936 Tcl_AppendElement (interp, p->entries[i].u.term.buf);
2938 Tcl_AppendElement (interp, "");
2939 sprintf (numstr, "%d", p->entries[i].u.term.globalOccurrences);
2940 Tcl_AppendElement (interp, numstr);
2942 case Z_Entry_surrogateDiagnostic:
2943 Tcl_AppendElement (interp, "SD");
2944 return ir_diagResult (interp, p->entries[i].u.diag.list,
2945 p->entries[i].u.diag.num);
2951 static IrTcl_Method ir_scan_method_tab[] = {
2952 { 0, "scan", do_scan },
2953 { 0, "scanResponse", do_scanResponse },
2954 { 0, "stepSize", do_stepSize },
2955 { 0, "numberOfTermsRequested", do_numberOfTermsRequested },
2956 { 0, "preferredPositionInResponse", do_preferredPositionInResponse },
2957 { 0, "scanStatus", do_scanStatus },
2958 { 0, "numberOfEntriesReturned", do_numberOfEntriesReturned },
2959 { 0, "positionOfTerm", do_positionOfTerm },
2960 { 0, "scanLine", do_scanLine },
2965 * ir_scan_obj_method: IR Scan Object methods
2967 static int ir_scan_obj_method (ClientData clientData, Tcl_Interp *interp,
2968 int argc, char **argv)
2970 IrTcl_Methods tabs[2];
2974 interp->result = "wrong # args";
2977 tabs[0].tab = ir_scan_method_tab;
2978 tabs[0].obj = clientData;
2981 return ir_method (interp, argc, argv, tabs);
2985 * ir_scan_obj_delete: IR Scan Object disposal
2987 static void ir_scan_obj_delete (ClientData clientData)
2989 IrTcl_Methods tabs[2];
2990 IrTcl_ScanObj *obj = clientData;
2992 tabs[0].tab = ir_scan_method_tab;
2996 ir_method (NULL, -1, NULL, tabs);
3001 * ir_scan_obj_mk: IR Scan Object creation
3003 static int ir_scan_obj_mk (ClientData clientData, Tcl_Interp *interp,
3004 int argc, char **argv)
3006 Tcl_CmdInfo parent_info;
3008 IrTcl_Methods tabs[2];
3012 interp->result = "wrong # args";
3015 if (!Tcl_GetCommandInfo (interp, argv[2], &parent_info))
3017 interp->result = "No parent";
3020 obj = ir_tcl_malloc (sizeof(*obj));
3021 obj->parent = (IrTcl_Obj *) parent_info.clientData;
3023 tabs[0].tab = ir_scan_method_tab;
3027 if (ir_method (interp, 0, NULL, tabs) == TCL_ERROR)
3029 Tcl_CreateCommand (interp, argv[1], ir_scan_obj_method,
3030 (ClientData) obj, ir_scan_obj_delete);
3034 /* ------------------------------------------------------- */
3036 static void ir_initResponse (void *obj, Z_InitResponse *initrs)
3040 p->initResult = *initrs->result ? 1 : 0;
3041 if (!*initrs->result)
3042 logf (LOG_DEBUG, "Connection rejected by target");
3044 logf (LOG_DEBUG, "Connection accepted by target");
3046 get_referenceId (&p->set_inher.referenceId, initrs->referenceId);
3048 free (p->targetImplementationId);
3049 ir_tcl_strdup (p->interp, &p->targetImplementationId,
3050 initrs->implementationId);
3051 free (p->targetImplementationName);
3052 ir_tcl_strdup (p->interp, &p->targetImplementationName,
3053 initrs->implementationName);
3054 free (p->targetImplementationVersion);
3055 ir_tcl_strdup (p->interp, &p->targetImplementationVersion,
3056 initrs->implementationVersion);
3058 p->maximumRecordSize = *initrs->maximumRecordSize;
3059 p->preferredMessageSize = *initrs->preferredMessageSize;
3061 memcpy (&p->options, initrs->options, sizeof(initrs->options));
3062 memcpy (&p->protocolVersion, initrs->protocolVersion,
3063 sizeof(initrs->protocolVersion));
3064 free (p->userInformationField);
3065 p->userInformationField = NULL;
3066 if (initrs->userInformationField)
3070 if (initrs->userInformationField->which == ODR_EXTERNAL_octet &&
3071 (p->userInformationField =
3072 ir_tcl_malloc ((len =
3073 initrs->userInformationField->
3074 u.octet_aligned->len) +1)))
3076 memcpy (p->userInformationField,
3077 initrs->userInformationField->u.octet_aligned->buf,
3079 (p->userInformationField)[len] = '\0';
3084 static void ir_deleteDiags (IrTcl_Diagnostic **dst_list, int *dst_num)
3087 for (i = 0; i<*dst_num; i++)
3088 free (dst_list[i]->addinfo);
3094 static void ir_handleDiags (IrTcl_Diagnostic **dst_list, int *dst_num,
3095 Z_DiagRec **list, int num)
3101 *dst_list = ir_tcl_malloc (sizeof(**dst_list) * num);
3102 for (i = 0; i<num; i++)
3104 switch (list[i]->which)
3106 case Z_DiagRec_defaultFormat:
3107 (*dst_list)[i].condition = *list[i]->u.defaultFormat->condition;
3108 addinfo = list[i]->u.defaultFormat->addinfo;
3110 ((*dst_list)[i].addinfo = ir_tcl_malloc (strlen(addinfo)+1)))
3111 strcpy ((*dst_list)[i].addinfo, addinfo);
3114 (*dst_list)[i].addinfo = NULL;
3115 (*dst_list)[i].condition = 0;
3120 static void ir_handleRecords (void *o, Z_Records *zrs, IrTcl_SetObj *setobj,
3121 const char *elements)
3126 IrTcl_RecordList *rl;
3128 setobj->which = zrs->which;
3129 setobj->recordFlag = 1;
3131 ir_deleteDiags (&setobj->nonSurrogateDiagnosticList,
3132 &setobj->nonSurrogateDiagnosticNum);
3133 if (zrs->which == Z_Records_DBOSD)
3135 setobj->numberOfRecordsReturned =
3136 zrs->u.databaseOrSurDiagnostics->num_records;
3137 logf (LOG_DEBUG, "Got %d records", setobj->numberOfRecordsReturned);
3138 for (offset = 0; offset<setobj->numberOfRecordsReturned; offset++)
3140 rl = new_IR_record (setobj, setobj->start + offset,
3141 zrs->u.databaseOrSurDiagnostics->
3142 records[offset]->which,
3144 if (rl->which == Z_NamePlusRecord_surrogateDiagnostic)
3146 ir_handleDiags (&rl->u.surrogateDiagnostics.list,
3147 &rl->u.surrogateDiagnostics.num,
3148 &zrs->u.databaseOrSurDiagnostics->
3149 records[offset]->u.surrogateDiagnostic,
3154 Z_DatabaseRecord *zr;
3156 struct oident *ident;
3158 zr = zrs->u.databaseOrSurDiagnostics->records[offset]
3160 oe = (Z_External*) zr;
3161 rl->u.dbrec.size = zr->u.octet_aligned->len;
3163 if ((ident = oid_getentbyoid (oe->direct_reference)))
3164 rl->u.dbrec.type = ident->value;
3166 rl->u.dbrec.type = VAL_USMARC;
3168 if (oe->which == ODR_EXTERNAL_octet && rl->u.dbrec.size > 0)
3170 char *buf = (char*) zr->u.octet_aligned->buf;
3171 if ((rl->u.dbrec.buf = ir_tcl_malloc (rl->u.dbrec.size)))
3172 memcpy (rl->u.dbrec.buf, buf, rl->u.dbrec.size);
3174 else if (rl->u.dbrec.type == VAL_SUTRS &&
3175 oe->which == Z_External_sutrs)
3177 odr_setbuf (p->odr_in, (char*) oe->u.single_ASN1_type->buf,
3178 oe->u.single_ASN1_type->len, 0);
3179 if ((rl->u.dbrec.buf = ir_tcl_malloc (oe->u.sutrs->len+1)))
3181 memcpy (rl->u.dbrec.buf, oe->u.sutrs->buf,
3183 rl->u.dbrec.buf[oe->u.sutrs->len] = '\0';
3185 rl->u.dbrec.size = oe->u.sutrs->len;
3187 else if (rl->u.dbrec.type == VAL_GRS1 &&
3188 oe->which == Z_External_grs1)
3190 ir_tcl_read_grs (oe->u.grs1, &rl->u.dbrec.u.grs1);
3191 rl->u.dbrec.buf = NULL;
3194 rl->u.dbrec.buf = NULL;
3198 else if (zrs->which == Z_Records_multipleNSD)
3200 logf (LOG_DEBUG, "multipleNonSurrogateDiagnostic %d",
3201 zrs->u.multipleNonSurDiagnostics->num_diagRecs);
3202 setobj->numberOfRecordsReturned = 0;
3203 ir_handleDiags (&setobj->nonSurrogateDiagnosticList,
3204 &setobj->nonSurrogateDiagnosticNum,
3205 zrs->u.multipleNonSurDiagnostics->diagRecs,
3206 zrs->u.multipleNonSurDiagnostics->num_diagRecs);
3210 logf (LOG_DEBUG, "NonSurrogateDiagnostic");
3211 setobj->numberOfRecordsReturned = 0;
3212 ir_handleDiags (&setobj->nonSurrogateDiagnosticList,
3213 &setobj->nonSurrogateDiagnosticNum,
3214 &zrs->u.nonSurrogateDiagnostic,
3219 static void ir_searchResponse (void *o, Z_SearchResponse *searchrs,
3220 IrTcl_SetObj *setobj)
3222 Z_Records *zrs = searchrs->records;
3224 logf (LOG_DEBUG, "Received search response");
3227 logf (LOG_DEBUG, "Search response, no object!");
3230 setobj->searchStatus = searchrs->searchStatus ? 1 : 0;
3231 get_referenceId (&setobj->set_inher.referenceId, searchrs->referenceId);
3232 setobj->resultCount = *searchrs->resultCount;
3233 if (searchrs->presentStatus)
3234 setobj->presentStatus = *searchrs->presentStatus;
3235 if (searchrs->nextResultSetPosition)
3236 setobj->nextResultSetPosition = *searchrs->nextResultSetPosition;
3238 logf (LOG_DEBUG, "Search response %d, %d hits",
3239 setobj->searchStatus, setobj->resultCount);
3243 if (setobj->resultCount <= setobj->set_inher.smallSetUpperBound)
3244 es = setobj->set_inher.smallSetElementSetNames;
3246 es = setobj->set_inher.mediumSetElementSetNames;
3247 ir_handleRecords (o, zrs, setobj, es);
3250 setobj->recordFlag = 0;
3254 static void ir_presentResponse (void *o, Z_PresentResponse *presrs,
3255 IrTcl_SetObj *setobj)
3257 Z_Records *zrs = presrs->records;
3259 logf (LOG_DEBUG, "Received present response");
3262 logf (LOG_DEBUG, "Present response, no object!");
3265 setobj->presentStatus = *presrs->presentStatus;
3266 get_referenceId (&setobj->set_inher.referenceId, presrs->referenceId);
3267 setobj->nextResultSetPosition = *presrs->nextResultSetPosition;
3269 ir_handleRecords (o, zrs, setobj, setobj->set_inher.elementSetNames);
3272 setobj->recordFlag = 0;
3273 logf (LOG_DEBUG, "No records!");
3277 static void ir_scanResponse (void *o, Z_ScanResponse *scanrs,
3278 IrTcl_ScanObj *scanobj)
3282 logf (LOG_DEBUG, "Received scanResponse");
3284 get_referenceId (&p->set_inher.referenceId, scanrs->referenceId);
3285 scanobj->scanStatus = *scanrs->scanStatus;
3286 logf (LOG_DEBUG, "scanStatus=%d", scanobj->scanStatus);
3288 if (scanrs->stepSize)
3289 scanobj->stepSize = *scanrs->stepSize;
3290 logf (LOG_DEBUG, "stepSize=%d", scanobj->stepSize);
3292 scanobj->numberOfEntriesReturned = *scanrs->numberOfEntriesReturned;
3293 logf (LOG_DEBUG, "numberOfEntriesReturned=%d",
3294 scanobj->numberOfEntriesReturned);
3296 if (scanrs->positionOfTerm)
3297 scanobj->positionOfTerm = *scanrs->positionOfTerm;
3299 scanobj->positionOfTerm = -1;
3300 logf (LOG_DEBUG, "positionOfTerm=%d", scanobj->positionOfTerm);
3302 free (scanobj->entries);
3303 scanobj->entries = NULL;
3305 ir_deleteDiags (&scanobj->nonSurrogateDiagnosticList,
3306 &scanobj->nonSurrogateDiagnosticNum);
3307 if (scanrs->entries)
3312 scanobj->entries_flag = 1;
3313 scanobj->which = scanrs->entries->which;
3314 switch (scanobj->which)
3316 case Z_ListEntries_entries:
3317 scanobj->num_entries = scanrs->entries->u.entries->num_entries;
3318 scanobj->entries = ir_tcl_malloc (scanobj->num_entries *
3319 sizeof(*scanobj->entries));
3320 for (i=0; i<scanobj->num_entries; i++)
3322 ze = scanrs->entries->u.entries->entries[i];
3323 scanobj->entries[i].which = ze->which;
3326 case Z_Entry_termInfo:
3327 if (ze->u.termInfo->term->which == Z_Term_general)
3329 int l = ze->u.termInfo->term->u.general->len;
3330 scanobj->entries[i].u.term.buf = ir_tcl_malloc (1+l);
3331 memcpy (scanobj->entries[i].u.term.buf,
3332 ze->u.termInfo->term->u.general->buf,
3334 scanobj->entries[i].u.term.buf[l] = '\0';
3337 scanobj->entries[i].u.term.buf = NULL;
3338 if (ze->u.termInfo->globalOccurrences)
3339 scanobj->entries[i].u.term.globalOccurrences =
3340 *ze->u.termInfo->globalOccurrences;
3342 scanobj->entries[i].u.term.globalOccurrences = 0;
3344 case Z_Entry_surrogateDiagnostic:
3345 ir_handleDiags (&scanobj->entries[i].u.diag.list,
3346 &scanobj->entries[i].u.diag.num,
3347 &ze->u.surrogateDiagnostic,
3353 case Z_ListEntries_nonSurrogateDiagnostics:
3354 ir_handleDiags (&scanobj->nonSurrogateDiagnosticList,
3355 &scanobj->nonSurrogateDiagnosticNum,
3356 scanrs->entries->u.nonSurrogateDiagnostics->
3358 scanrs->entries->u.nonSurrogateDiagnostics->
3364 scanobj->entries_flag = 0;
3368 * ir_select_read: handle incoming packages
3370 void ir_select_read (ClientData clientData)
3372 IrTcl_Obj *p = clientData;
3377 Tcl_CmdInfo cmd_info;
3378 const char *apdu_call;
3380 logf(LOG_DEBUG, "Read handler fd=%d", cs_fileno(p->cs_link));
3381 if (p->state == IR_TCL_R_Connecting)
3383 logf(LOG_DEBUG, "Connect handler");
3384 r = cs_rcvconnect (p->cs_link);
3387 logf (LOG_WARN, "cs_rcvconnect returned 1");
3390 p->state = IR_TCL_R_Idle;
3391 #if IRTCL_GENERIC_FILES
3392 ir_select_remove_write (p->csFile, p);
3394 ir_select_remove_write (cs_fileno (p->cs_link), p);
3398 logf (LOG_DEBUG, "cs_rcvconnect error");
3401 p->failInfo = IR_TCL_FAIL_CONNECT;
3402 IrTcl_eval (p->interp, p->failback);
3404 do_disconnect (p, NULL, 2, NULL);
3407 p->state = IR_TCL_R_Idle;
3409 IrTcl_eval (p->interp, p->callback);
3410 if (p->cs_link && p->request_queue && p->state == IR_TCL_R_Idle)
3411 ir_tcl_send_q (p, p->request_queue, "x");
3416 /* signal one more use of ir object - callbacks must not
3417 release the ir memory (p pointer) */
3418 p->state = IR_TCL_R_Reading;
3421 /* read incoming APDU */
3422 if ((r=cs_get (p->cs_link, &p->buf_in, &p->len_in)) <= 0)
3424 logf (LOG_DEBUG, "cs_get failed, code %d", r);
3425 #if IRTCL_GENERIC_FILES
3426 ir_select_remove (p->csFile, p);
3428 ir_select_remove (cs_fileno (p->cs_link), p);
3430 do_disconnect (p, NULL, 2, NULL);
3433 p->failInfo = IR_TCL_FAIL_READ;
3434 IrTcl_eval (p->interp, p->failback);
3436 /* release ir object now if callback deleted it */
3442 logf(LOG_DEBUG, "PDU Fraction read");
3445 /* got complete APDU. Now decode */
3448 odr_setbuf (p->odr_in, p->buf_in, r, 0);
3449 logf (LOG_DEBUG, "cs_get ok, total size %d", r);
3450 if (!z_APDU (p->odr_in, &apdu, 0))
3452 logf (LOG_DEBUG, "cs_get failed: %s",
3453 odr_errmsg (odr_geterror (p->odr_in)));
3454 do_disconnect (p, NULL, 2, NULL);
3457 p->failInfo = IR_TCL_FAIL_IN_APDU;
3458 p->apduOffset = odr_offset (p->odr_in);
3459 IrTcl_eval (p->interp, p->failback);
3461 /* release ir object now if failback deleted it */
3465 logf(LOG_DEBUG, "Decoded ok");
3466 /* handle APDU and invoke callback */
3467 rq = p->request_queue;
3470 logf (LOG_FATAL, "Internal error. No queue entry");
3473 object_name = rq->object_name;
3474 logf (LOG_DEBUG, "getCommandInfo (%s)", object_name);
3476 if (Tcl_GetCommandInfo (p->interp, object_name, &cmd_info))
3480 case Z_APDU_initResponse:
3481 p->eventType = "init";
3482 ir_initResponse (p, apdu->u.initResponse);
3483 apdu_call = p->initResponse;
3485 case Z_APDU_searchResponse:
3486 p->eventType = "search";
3487 ir_searchResponse (p, apdu->u.searchResponse,
3488 (IrTcl_SetObj *) cmd_info.clientData);
3489 apdu_call = ((IrTcl_SetObj *)
3490 cmd_info.clientData)->searchResponse;
3492 case Z_APDU_presentResponse:
3493 p->eventType = "present";
3494 ir_presentResponse (p, apdu->u.presentResponse,
3495 (IrTcl_SetObj *) cmd_info.clientData);
3496 apdu_call = ((IrTcl_SetObj *)
3497 cmd_info.clientData)->presentResponse;
3499 case Z_APDU_scanResponse:
3500 p->eventType = "scan";
3501 ir_scanResponse (p, apdu->u.scanResponse,
3502 (IrTcl_ScanObj *) cmd_info.clientData);
3503 apdu_call = ((IrTcl_ScanObj *)
3504 cmd_info.clientData)->scanResponse;
3507 logf (LOG_WARN, "Received unknown APDU type (%d)",
3509 do_disconnect (p, NULL, 2, NULL);
3512 p->failInfo = IR_TCL_FAIL_UNKNOWN_APDU;
3513 IrTcl_eval (p->interp, p->failback);
3518 p->request_queue = rq->next;
3519 p->state = IR_TCL_R_Idle;
3522 IrTcl_eval (p->interp, apdu_call);
3523 else if (rq->callback)
3524 IrTcl_eval (p->interp, rq->callback);
3526 free (rq->callback);
3527 free (rq->object_name);
3529 odr_reset (p->odr_in);
3530 if (p->ref_count == 1)
3536 } while (p->cs_link && cs_more (p->cs_link));
3537 if (p->cs_link && p->request_queue && p->state == IR_TCL_R_Idle)
3538 ir_tcl_send_q (p, p->request_queue, "x");
3542 * ir_select_write: handle outgoing packages - not yet written.
3544 void ir_select_write (ClientData clientData)
3546 IrTcl_Obj *p = clientData;
3550 logf (LOG_DEBUG, "Write handler fd=%d", cs_fileno(p->cs_link));
3551 if (p->state == IR_TCL_R_Connecting)
3553 logf(LOG_DEBUG, "Connect handler");
3554 r = cs_rcvconnect (p->cs_link);
3557 p->state = IR_TCL_R_Idle;
3560 logf (LOG_DEBUG, "cs_rcvconnect error");
3561 #if IRTCL_GENERIC_FILES
3562 ir_select_remove_write (p->csFile, p);
3564 ir_select_remove_write (cs_fileno (p->cs_link), p);
3568 p->failInfo = IR_TCL_FAIL_CONNECT;
3569 IrTcl_eval (p->interp, p->failback);
3571 do_disconnect (p, NULL, 2, NULL);
3574 #if IRTCL_GENERIC_FILES
3575 ir_select_remove_write (p->csFile, p);
3577 ir_select_remove_write (cs_fileno (p->cs_link), p);
3580 IrTcl_eval (p->interp, p->callback);
3583 rq = p->request_queue;
3584 if (!rq || !rq->buf_out)
3587 if ((r=cs_put (p->cs_link, rq->buf_out, rq->len_out)) < 0)
3589 logf (LOG_DEBUG, "cs_put write fail");
3592 p->failInfo = IR_TCL_FAIL_WRITE;
3593 IrTcl_eval (p->interp, p->failback);
3597 do_disconnect (p, NULL, 2, NULL);
3599 else if (r == 0) /* remove select bit */
3601 logf(LOG_DEBUG, "Write completed");
3602 p->state = IR_TCL_R_Waiting;
3603 #if IRTCL_GENERIC_FILES
3604 ir_select_remove_write (p->csFile, p);
3606 ir_select_remove_write (cs_fileno (p->cs_link), p);
3613 /* ------------------------------------------------------- */
3616 * Irtcl_init: Registration of TCL commands.
3618 int Irtcl_Init (Tcl_Interp *interp)
3620 Tcl_CreateCommand (interp, "ir", ir_obj_mk, (ClientData) NULL,
3621 (Tcl_CmdDeleteProc *) NULL);
3622 Tcl_CreateCommand (interp, "ir-set", ir_set_obj_mk,
3623 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
3624 Tcl_CreateCommand (interp, "ir-scan", ir_scan_obj_mk,
3625 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);