2 * IR toolkit for tcl/tk
4 * See the file LICENSE for details.
5 * Sebastian Hammer, Adam Dickmeiss
8 * Revision 1.79 1996-02-23 13:41:38 adam
9 * Work on public access to simple ir class system.
11 * Revision 1.78 1996/02/21 10:16:08 adam
12 * Simplified select handling. Only one function ir_tcl_select_set has
13 * to be externally defined.
15 * Revision 1.77 1996/02/20 17:52:58 adam
16 * Uses the YAZ oid system to name record syntax object identifiers.
18 * Revision 1.76 1996/02/20 16:09:51 adam
19 * Bug fix: didn't set element set names stamp correctly on result
20 * set records when element set names were set to the empty string.
22 * Revision 1.75 1996/02/19 15:41:53 adam
23 * Better log messages.
24 * Minor improvement of connect method.
26 * Revision 1.74 1996/02/05 17:58:03 adam
27 * Ported ir-tcl to use the beta releases of tcl7.5/tk4.1.
29 * Revision 1.73 1996/01/29 11:35:19 adam
30 * Bug fix: cs_type member renamed to comstackType to avoid conflict with
31 * cs_type macro defined by YAZ.
33 * Revision 1.72 1996/01/19 17:45:34 quinn
34 * Added debugging output
36 * Revision 1.71 1996/01/19 16:22:38 adam
37 * New method: apduDump - returns information about last incoming APDU.
39 * Revision 1.70 1996/01/10 09:18:34 adam
40 * PDU specific callbacks implemented: initRespnse, searchResponse,
41 * presentResponse and scanResponse.
42 * Bug fix in the command line shell (tclmain.c) - discovered on OSF/1.
44 * Revision 1.69 1996/01/04 16:12:12 adam
45 * Setting PDUType renamed to eventType.
47 * Revision 1.68 1996/01/04 11:05:22 adam
48 * New setting: PDUType - returns type of last PDU returned from the target.
49 * Fixed a bug in configure/Makefile.
51 * Revision 1.67 1996/01/03 09:00:51 adam
52 * Updated to use new version of Yaz (names changed to avoid C++ conflict).
54 * Revision 1.66 1995/11/28 17:26:39 adam
55 * Removed Carriage return from ir-tcl.c!
56 * Removed misc. debug logs.
58 * Revision 1.65 1995/11/28 13:53:00 quinn
61 * Revision 1.64 1995/11/13 15:39:18 adam
62 * Bug fix: {small,medium}SetElementSetNames weren't set correctly.
63 * Bug fix: idAuthentication weren't set correctly.
65 * Revision 1.63 1995/11/13 09:55:39 adam
66 * Multiple records at a position in a result-set with differnt
69 * Revision 1.62 1995/10/18 17:20:33 adam
70 * Work on target setup in client.tcl.
72 * Revision 1.61 1995/10/18 16:42:42 adam
73 * New settings: smallSetElementSetNames and mediumSetElementSetNames.
75 * Revision 1.60 1995/10/18 15:43:31 adam
76 * In search: mediumSetElementSetNames and smallSetElementSetNames are
77 * set to elementSetNames.
79 * Revision 1.59 1995/10/17 12:18:58 adam
80 * Bug fix: when target connection closed, the connection was not
81 * properly reestablished.
83 * Revision 1.58 1995/10/16 17:00:55 adam
84 * New setting: elementSetNames.
85 * Various client improvements. Medium presentation format looks better.
87 * Revision 1.57 1995/09/21 13:11:51 adam
88 * Support of dynamic loading.
89 * Test script uses load command if necessary.
91 * Revision 1.56 1995/08/29 15:30:14 adam
92 * Work on GRS records.
94 * Revision 1.55 1995/08/28 09:43:25 adam
95 * Minor changes. configure only searches for yaz beta 3 and versions after
98 * Revision 1.54 1995/08/24 12:25:16 adam
99 * Modified to work with yaz 1.0b3.
101 * Revision 1.53 1995/08/04 12:49:26 adam
102 * Bug fix: reading uninitialized variable p.
104 * Revision 1.52 1995/08/04 11:32:38 adam
105 * More work on output queue. Memory related routines moved
108 * Revision 1.51 1995/08/03 13:22:54 adam
111 * Revision 1.50 1995/07/20 08:09:49 adam
112 * client.tcl: Targets removed from hotTargets list when targets
113 * are removed/modified.
114 * ir-tcl.c: More work on triggerResourceControl.
116 * Revision 1.49 1995/06/30 12:39:21 adam
117 * Bug fix: loadFile didn't set record type.
118 * The MARC routines are a little less strict in the interpretation.
119 * Script display.tcl replaces the old marc.tcl.
120 * New interactive script: shell.tcl.
122 * Revision 1.48 1995/06/27 19:03:50 adam
123 * Bug fix in do_present in ir-tcl.c: p->set_child member weren't set.
124 * nextResultSetPosition used instead of setOffset.
126 * Revision 1.47 1995/06/25 10:25:04 adam
127 * Working on triggerResourceControl. Description of compile/install
128 * procedure moved to ir-tcl.sgml.
130 * Revision 1.46 1995/06/22 13:15:06 adam
131 * Feature: SUTRS. Setting getSutrs implemented.
132 * Work on display formats.
133 * Preferred record syntax can be set by the user.
135 * Revision 1.45 1995/06/20 08:07:30 adam
136 * New setting: failInfo.
137 * Working on better cancel mechanism.
139 * Revision 1.44 1995/06/19 17:01:20 adam
142 * Revision 1.43 1995/06/19 13:06:08 adam
143 * New define: IR_TCL_VERSION.
145 * Revision 1.42 1995/06/19 08:08:52 adam
146 * client.tcl: hotTargets now contain both database and target name.
147 * ir-tcl.c: setting protocol edited. Errors in callbacks are logged
148 * by logf(LOG_WARN, ...) calls.
150 * Revision 1.41 1995/06/16 12:28:16 adam
151 * Implemented preferredRecordSyntax.
152 * Minor changes in diagnostic handling.
153 * Record list deleted when connection closes.
155 * Revision 1.40 1995/06/14 13:37:18 adam
156 * Setting recordType implemented.
157 * Setting implementationVersion implemented.
158 * Settings implementationId / implementationName edited.
160 * Revision 1.39 1995/06/08 10:26:32 adam
161 * Bug fix in ir_strdup.
163 * Revision 1.38 1995/06/01 16:36:47 adam
164 * About buttons. Minor bug fixes.
166 * Revision 1.37 1995/06/01 07:31:20 adam
167 * Rename of many typedefs -> IrTcl_...
169 * Revision 1.36 1995/05/31 13:09:59 adam
170 * Client searches/presents may be interrupted.
171 * New moving book-logo.
173 * Revision 1.35 1995/05/31 08:36:33 adam
174 * Bug fix in client.tcl: didn't save options on clientrc.tcl.
175 * New method: referenceId. More work on scan.
177 * Revision 1.34 1995/05/29 10:33:42 adam
178 * README and rename of startup script.
180 * Revision 1.33 1995/05/29 09:15:11 quinn
181 * Changed CS_SR to PROTO_SR, etc.
183 * Revision 1.32 1995/05/29 08:44:16 adam
184 * Work on delete of objects.
186 * Revision 1.31 1995/05/26 11:44:10 adam
187 * Bugs fixed. More work on MARC utilities and queries. Test
188 * client is up-to-date again.
190 * Revision 1.30 1995/05/26 08:54:11 adam
191 * New MARC utilities. Uses prefix query.
193 * Revision 1.29 1995/05/24 14:10:22 adam
194 * Work on idAuthentication, protocolVersion and options.
196 * Revision 1.28 1995/05/23 15:34:48 adam
197 * Many new settings, userInformationField, smallSetUpperBound, etc.
198 * A number of settings are inherited when ir-set is executed.
199 * This version is incompatible with the graphical test client (client.tcl).
201 * Revision 1.27 1995/05/11 15:34:47 adam
202 * Scan request changed a bit. This version works with RLG.
204 * Revision 1.26 1995/04/18 16:11:51 adam
205 * First version of graphical Scan. Some work on query-by-form.
207 * Revision 1.25 1995/04/17 09:37:17 adam
208 * Further development of scan.
210 * Revision 1.24 1995/04/11 14:16:42 adam
211 * Further work on scan. Response works. Entries aren't saved yet.
213 * Revision 1.23 1995/04/10 10:50:27 adam
214 * Result-set name defaults to suffix of ir-set name.
215 * Started working on scan. Not finished at this point.
217 * Revision 1.22 1995/03/31 10:43:03 adam
218 * More robust when getting bad MARC records.
220 * Revision 1.21 1995/03/31 08:56:37 adam
221 * New button "Search".
223 * Revision 1.20 1995/03/29 16:07:09 adam
224 * Bug fix: Didn't use setName in present request.
226 * Revision 1.19 1995/03/28 12:45:23 adam
227 * New ir method failback: called on disconnect/protocol error.
228 * New ir set/get method: protocol: SR / Z3950.
229 * Simple popup and disconnect when failback is invoked.
231 * Revision 1.18 1995/03/21 15:50:12 adam
234 * Revision 1.17 1995/03/21 13:41:03 adam
235 * Comstack cs_create not used too often. Non-blocking connect.
237 * Revision 1.16 1995/03/21 08:26:06 adam
238 * New method, setName, to specify the result set name (other than Default).
239 * New method, responseStatus, which returns diagnostic info, if any, after
240 * present response / search response.
242 * Revision 1.15 1995/03/20 15:24:07 adam
243 * Diagnostic records saved on searchResponse.
245 * Revision 1.14 1995/03/20 08:53:22 adam
246 * Event loop in tclmain.c rewritten. New method searchStatus.
248 * Revision 1.13 1995/03/17 18:26:17 adam
249 * Non-blocking i/o used now. Database names popup as cascade items.
251 * Revision 1.12 1995/03/17 15:45:00 adam
252 * Improved target/database setup.
254 * Revision 1.11 1995/03/16 17:54:03 adam
255 * Minor changes really.
257 * Revision 1.10 1995/03/15 16:14:50 adam
258 * Blocking arg in cs_create changed.
260 * Revision 1.9 1995/03/15 13:59:24 adam
263 * Revision 1.8 1995/03/15 08:25:16 adam
264 * New method presentStatus to check for error on present. Misc. cleanup
265 * of IrTcl_RecordList manipulations. Full MARC record presentation in
268 * Revision 1.7 1995/03/14 17:32:29 adam
269 * Presentation of full Marc record in popup window.
271 * Revision 1.6 1995/03/12 19:31:55 adam
272 * Pattern matching implemented when retrieving MARC records. More
273 * diagnostic functions.
275 * Revision 1.5 1995/03/10 18:00:15 adam
276 * Actual presentation in line-by-line format. RPN query support.
278 * Revision 1.4 1995/03/09 16:15:08 adam
279 * First presentRequest attempts. Hot-target list.
291 #define IRTCL_GENERIC_FILES 0
295 static void ir_deleteDiags (IrTcl_Diagnostic **dst_list, int *dst_num);
296 static int do_disconnect (void *obj, Tcl_Interp *interp,
297 int argc, char **argv);
299 static void ir_select_notify (ClientData clientData, int r, int w, int e);
301 void ir_select_add (int fd, void *obj)
303 ir_tcl_select_set (ir_select_notify, fd, obj, 1, 0, 0);
306 void ir_select_add_write (int fd, void *obj)
308 ir_tcl_select_set (ir_select_notify, fd, obj, 1, 1, 0);
311 void ir_select_remove (int fd, void *obj)
313 ir_tcl_select_set (NULL, fd, obj, 0, 0, 0);
316 void ir_select_remove_write (int fd, void *obj)
318 ir_tcl_select_set (ir_select_notify, fd, obj, 1, 0, 0);
321 static IrTcl_RecordList *new_IR_record (IrTcl_SetObj *setobj,
323 const char *elements)
325 IrTcl_RecordList *rl;
327 if (elements && !*elements)
329 for (rl = setobj->record_list; rl; rl = rl->next)
331 if (no == rl->no && (!rl->elements || !elements ||
332 !strcmp(elements, rl->elements)))
337 case Z_NamePlusRecord_databaseRecord:
338 free (rl->u.dbrec.buf);
339 rl->u.dbrec.buf = NULL;
341 case Z_NamePlusRecord_surrogateDiagnostic:
342 ir_deleteDiags (&rl->u.surrogateDiagnostics.list,
343 &rl->u.surrogateDiagnostics.num);
351 rl = ir_tcl_malloc (sizeof(*rl));
352 rl->next = setobj->record_list;
354 setobj->record_list = rl;
357 ir_tcl_strdup (NULL, &rl->elements, elements);
364 int IrTcl_eval (Tcl_Interp *interp, const char *command)
366 char *tmp = ir_tcl_malloc (strlen(command)+1);
369 strcpy (tmp, command);
370 r = Tcl_Eval (interp, tmp);
372 logf (LOG_WARN, "Tcl error in line %d: %s", interp->errorLine,
374 Tcl_FreeResult (interp);
380 * IrTcl_getRecordSyntaxStr: Return record syntax name of object id
382 static char *IrTcl_getRecordSyntaxStr (enum oid_value value)
385 struct oident ent, *entp;
387 ent.proto = PROTO_Z3950;
388 ent.oclass = CLASS_RECSYN;
391 o = oid_getoidbyent (&ent);
392 entp = oid_getentbyoid (o);
400 * IrTcl_getRecordSyntaxVal: Return record syntax value of string
402 static enum oid_value IrTcl_getRecordSyntaxVal (const char *name)
404 return oid_getvalbyname (name);
407 static IrTcl_RecordList *find_IR_record (IrTcl_SetObj *setobj, int no)
409 IrTcl_RecordList *rl;
411 for (rl = setobj->record_list; rl; rl = rl->next)
413 (!setobj->recordElements || !rl->elements ||
414 !strcmp (setobj->recordElements, rl->elements)))
419 static void delete_IR_records (IrTcl_SetObj *setobj)
421 IrTcl_RecordList *rl, *rl1;
423 for (rl = setobj->record_list; rl; rl = rl1)
427 case Z_NamePlusRecord_databaseRecord:
428 free (rl->u.dbrec.buf);
430 case Z_NamePlusRecord_surrogateDiagnostic:
431 ir_deleteDiags (&rl->u.surrogateDiagnostics.list,
432 &rl->u.surrogateDiagnostics.num);
438 setobj->record_list = NULL;
442 * get_set_int: Set/get integer value
444 static int get_set_int (int *val, Tcl_Interp *interp, int argc, char **argv)
450 if (Tcl_GetInt (interp, argv[2], val)==TCL_ERROR)
453 sprintf (buf, "%d", *val);
454 Tcl_AppendResult (interp, buf, NULL);
459 * ir_tcl_method: Search for method in table and invoke method handler
461 int ir_tcl_method (Tcl_Interp *interp, int argc, char **argv, IrTcl_Methods *tab)
463 IrTcl_Methods *tab_i = tab;
466 for (tab_i = tab; tab_i->tab; tab_i++)
467 for (t = tab_i->tab; t->name; t++)
470 if ((*t->method)(tab_i->obj, interp, argc, argv) == TCL_ERROR)
474 if (!strcmp (t->name, argv[1]))
475 return (*t->method)(tab_i->obj, interp, argc, argv);
480 Tcl_AppendResult (interp, "Bad method: ", argv[1],
481 ". Possible methods:", NULL);
482 for (tab_i = tab; tab_i->tab; tab_i++)
483 for (t = tab_i->tab; t->name; t++)
484 Tcl_AppendResult (interp, " ", t->name, 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 { "comstack", do_comstack, NULL },
1651 { "protocol", do_protocol, NULL },
1652 { "failback", do_failback, NULL },
1653 { "failInfo", do_failInfo, NULL },
1654 { "apduInfo", do_apduInfo, NULL },
1655 { "logLevel", do_logLevel, NULL },
1657 { "eventType", do_eventType, NULL },
1658 { "connect", do_connect, NULL },
1659 { "protocolVersion", do_protocolVersion, NULL },
1660 { "preferredMessageSize", do_preferredMessageSize, NULL },
1661 { "maximumRecordSize", do_maximumRecordSize, NULL },
1662 { "implementationName", do_implementationName, NULL },
1663 { "implementationId", do_implementationId, NULL },
1664 { "implementationVersion", do_implementationVersion, NULL },
1665 { "targetImplementationName", do_targetImplementationName, NULL },
1666 { "targetImplementationId", do_targetImplementationId, NULL },
1667 { "targetImplementationVersion", do_targetImplementationVersion, NULL},
1668 { "userInformationField", do_userInformationField, NULL},
1669 { "idAuthentication", do_idAuthentication, NULL},
1670 { "options", do_options, NULL},
1671 { "init", do_init_request, NULL},
1672 { "initResult", do_initResult, NULL},
1673 { "disconnect", do_disconnect, NULL},
1674 { "callback", do_callback, NULL},
1675 { "initResponse", do_initResponse, NULL},
1676 { "triggerResourceControl", do_triggerResourceControl, NULL},
1677 { "initResponse", do_initResponse, NULL},
1681 static IrTcl_Method ir_set_c_method_tab[] = {
1682 { "databaseNames", do_databaseNames, NULL},
1683 { "replaceIndicator", do_replaceIndicator, NULL},
1684 { "queryType", do_queryType, NULL},
1685 { "preferredRecordSyntax", do_preferredRecordSyntax, NULL},
1686 { "smallSetUpperBound", do_smallSetUpperBound, NULL},
1687 { "largeSetLowerBound", do_largeSetLowerBound, NULL},
1688 { "mediumSetPresentNumber", do_mediumSetPresentNumber, NULL},
1689 { "referenceId", do_referenceId, NULL},
1690 { "elementSetNames", do_elementSetNames, NULL},
1691 { "smallSetElementSetNames", do_smallSetElementSetNames, NULL},
1692 { "mediumSetElementSetNames", do_mediumSetElementSetNames, NULL},
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;
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_tcl_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_tcl_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_init: IR Object initialization
1749 int ir_obj_init (ClientData clientData, Tcl_Interp *interp,
1750 int argc, char **argv, ClientData *subData)
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_tcl_method (interp, 0, NULL, tab) == TCL_ERROR)
1793 Tcl_AppendResult (interp, "Failed to initialize ", argv[1], NULL);
1802 * ir_obj_mk: IR Object creation
1804 static int ir_obj_mk (ClientData clientData, Tcl_Interp *interp,
1805 int argc, char **argv)
1808 int r = ir_obj_init (clientData, interp, argc, argv, &subData);
1812 Tcl_CreateCommand (interp, argv[1], ir_obj_method,
1813 subData, ir_obj_delete);
1817 IrTcl_Class ir_obj_class = {
1825 /* ------------------------------------------------------- */
1827 * do_search: Do search request
1829 static int do_search (void *o, Tcl_Interp *interp, int argc, char **argv)
1831 Z_SearchRequest *req;
1835 IrTcl_SetObj *obj = o;
1846 interp->result = "wrong # args";
1849 if (!obj->set_inher.num_databaseNames)
1851 interp->result = "no databaseNames";
1856 interp->result = "search: not connected";
1859 apdu = zget_APDU (p->odr_out, Z_APDU_searchRequest);
1860 req = apdu->u.searchRequest;
1864 bib1.proto = p->protocol_type;
1865 bib1.oclass = CLASS_ATTSET;
1866 bib1.value = VAL_BIB1;
1868 set_referenceId (p->odr_out, &req->referenceId,
1869 obj->set_inher.referenceId);
1871 req->smallSetUpperBound = &obj->set_inher.smallSetUpperBound;
1872 req->largeSetLowerBound = &obj->set_inher.largeSetLowerBound;
1873 req->mediumSetPresentNumber = &obj->set_inher.mediumSetPresentNumber;
1874 req->replaceIndicator = &obj->set_inher.replaceIndicator;
1875 req->resultSetName = obj->setName ? obj->setName : "Default";
1876 logf (LOG_DEBUG, "Search, resultSetName %s", req->resultSetName);
1877 req->num_databaseNames = obj->set_inher.num_databaseNames;
1878 req->databaseNames = obj->set_inher.databaseNames;
1879 for (r=0; r < obj->set_inher.num_databaseNames; r++)
1880 logf (LOG_DEBUG, " Database %s", obj->set_inher.databaseNames[r]);
1881 if (obj->set_inher.preferredRecordSyntax)
1883 struct oident ident;
1885 ident.proto = p->protocol_type;
1886 ident.oclass = CLASS_RECSYN;
1887 ident.value = *obj->set_inher.preferredRecordSyntax;
1888 logf (LOG_DEBUG, "Preferred record syntax is %d", ident.value);
1889 req->preferredRecordSyntax = odr_oiddup (p->odr_out,
1890 oid_getoidbyent (&ident));
1893 req->preferredRecordSyntax = 0;
1895 if (obj->set_inher.smallSetElementSetNames &&
1896 *obj->set_inher.smallSetElementSetNames)
1898 Z_ElementSetNames *esn = odr_malloc (p->odr_out, sizeof(*esn));
1900 esn->which = Z_ElementSetNames_generic;
1901 esn->u.generic = obj->set_inher.smallSetElementSetNames;
1902 req->smallSetElementSetNames = esn;
1905 req->smallSetElementSetNames = NULL;
1907 if (obj->set_inher.mediumSetElementSetNames &&
1908 *obj->set_inher.mediumSetElementSetNames)
1910 Z_ElementSetNames *esn = odr_malloc (p->odr_out, sizeof(*esn));
1912 esn->which = Z_ElementSetNames_generic;
1913 esn->u.generic = obj->set_inher.mediumSetElementSetNames;
1914 req->mediumSetElementSetNames = esn;
1917 req->mediumSetElementSetNames = NULL;
1919 req->query = &query;
1921 if (!strcmp (obj->set_inher.queryType, "rpn"))
1923 Z_RPNQuery *RPNquery;
1925 RPNquery = p_query_rpn (p->odr_out, argv[2]);
1928 Tcl_AppendResult (interp, "Syntax error in query", NULL);
1931 RPNquery->attributeSetId = oid_getoidbyent (&bib1);
1932 query.which = Z_Query_type_1;
1933 query.u.type_1 = RPNquery;
1934 logf (LOG_DEBUG, "RPN");
1937 else if (!strcmp (obj->set_inher.queryType, "cclrpn"))
1941 struct ccl_rpn_node *rpn;
1942 Z_RPNQuery *RPNquery;
1944 rpn = ccl_find_str(p->bibset, argv[2], &error, &pos);
1947 Tcl_AppendResult (interp, "CCL error: ",
1948 ccl_err_msg(error), NULL);
1951 ccl_pr_tree (rpn, stderr);
1952 fprintf (stderr, "\n");
1953 assert((RPNquery = ccl_rpn_query(rpn)));
1954 RPNquery->attributeSetId = oid_getoidbyent (&bib1);
1955 query.which = Z_Query_type_1;
1956 query.u.type_1 = RPNquery;
1957 logf (LOG_DEBUG, "CCLRPN");
1960 else if (!strcmp (obj->set_inher.queryType, "ccl"))
1962 query.which = Z_Query_type_2;
1963 query.u.type_2 = &ccl_query;
1964 ccl_query.buf = (unsigned char *) argv[2];
1965 ccl_query.len = strlen (argv[2]);
1966 logf (LOG_DEBUG, "CCL");
1970 interp->result = "unknown query method";
1973 return ir_tcl_send_APDU (interp, p, apdu, "search", *argv);
1977 * do_searchResponse: add search response handler
1979 static int do_searchResponse (void *o, Tcl_Interp *interp,
1980 int argc, char **argv)
1982 IrTcl_SetObj *obj = o;
1986 obj->searchResponse = NULL;
1989 else if (argc == -1)
1990 return ir_tcl_strdel (interp, &obj->searchResponse);
1993 free (obj->searchResponse);
1996 if (ir_tcl_strdup (interp, &obj->searchResponse, argv[2])
2001 obj->searchResponse = NULL;
2007 * do_presentResponse: add present response handler
2009 static int do_presentResponse (void *o, Tcl_Interp *interp,
2010 int argc, char **argv)
2012 IrTcl_SetObj *obj = o;
2016 obj->presentResponse = NULL;
2019 else if (argc == -1)
2020 return ir_tcl_strdel (interp, &obj->presentResponse);
2023 free (obj->presentResponse);
2026 if (ir_tcl_strdup (interp, &obj->presentResponse, argv[2])
2031 obj->presentResponse = NULL;
2037 * do_resultCount: Get number of hits
2039 static int do_resultCount (void *o, Tcl_Interp *interp,
2040 int argc, char **argv)
2042 IrTcl_SetObj *obj = o;
2046 obj->resultCount = 0;
2049 return get_set_int (&obj->resultCount, interp, argc, argv);
2053 * do_searchStatus: Get search status (after search response)
2055 static int do_searchStatus (void *o, Tcl_Interp *interp,
2056 int argc, char **argv)
2058 IrTcl_SetObj *obj = o;
2062 return get_set_int (&obj->searchStatus, interp, argc, argv);
2066 * do_presentStatus: Get search status (after search/present response)
2068 static int do_presentStatus (void *o, Tcl_Interp *interp,
2069 int argc, char **argv)
2071 IrTcl_SetObj *obj = o;
2075 return get_set_int (&obj->presentStatus, interp, argc, argv);
2079 * do_nextResultSetPosition: Get next result set position
2080 * (after search/present response)
2082 static int do_nextResultSetPosition (void *o, Tcl_Interp *interp,
2083 int argc, char **argv)
2085 IrTcl_SetObj *obj = o;
2089 obj->nextResultSetPosition = 0;
2092 return get_set_int (&obj->nextResultSetPosition, interp, argc, argv);
2096 * do_setName: Set result Set name
2098 static int do_setName (void *o, Tcl_Interp *interp,
2099 int argc, char **argv)
2101 IrTcl_SetObj *obj = o;
2104 return ir_tcl_strdup (interp, &obj->setName, "Default");
2105 else if (argc == -1)
2106 return ir_tcl_strdel (interp, &obj->setName);
2109 free (obj->setName);
2110 if (ir_tcl_strdup (interp, &obj->setName, argv[2])
2114 Tcl_AppendElement (interp, obj->setName);
2119 * do_numberOfRecordsReturned: Get number of records returned
2121 static int do_numberOfRecordsReturned (void *o, Tcl_Interp *interp,
2122 int argc, char **argv)
2124 IrTcl_SetObj *obj = o;
2128 obj->numberOfRecordsReturned = 0;
2131 return get_set_int (&obj->numberOfRecordsReturned, interp, argc, argv);
2135 * do_type: Return type (if any) at position.
2137 static int do_type (void *o, Tcl_Interp *interp, int argc, char **argv)
2139 IrTcl_SetObj *obj = o;
2141 IrTcl_RecordList *rl;
2145 obj->record_list = NULL;
2148 else if (argc == -1)
2150 delete_IR_records (obj);
2155 sprintf (interp->result, "wrong # args");
2158 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2160 rl = find_IR_record (obj, offset);
2163 logf (LOG_DEBUG, "No record at position %d", offset);
2168 case Z_NamePlusRecord_databaseRecord:
2169 interp->result = "DB";
2171 case Z_NamePlusRecord_surrogateDiagnostic:
2172 interp->result = "SD";
2180 * do_recordType: Return record type (if any) at position.
2182 static int do_recordType (void *o, Tcl_Interp *interp, int argc, char **argv)
2184 IrTcl_SetObj *obj = o;
2186 IrTcl_RecordList *rl;
2192 else if (argc == -1)
2198 sprintf (interp->result, "wrong # args");
2201 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2203 rl = find_IR_record (obj, offset);
2206 if (rl->which != Z_NamePlusRecord_databaseRecord)
2208 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2211 Tcl_AppendElement (interp, (char*)
2212 IrTcl_getRecordSyntaxStr (rl->u.dbrec.type));
2217 * set record elements (for record extraction)
2219 static int do_recordElements (void *o, Tcl_Interp *interp,
2220 int argc, char **argv)
2222 IrTcl_SetObj *obj = o;
2226 obj->recordElements = NULL;
2229 else if (argc == -1)
2230 return ir_tcl_strdel (NULL, &obj->recordElements);
2233 sprintf (interp->result, "wrong # args");
2238 free (obj->recordElements);
2239 return ir_tcl_strdup (NULL, &obj->recordElements,
2240 (*argv[2] ? argv[2] : NULL));
2242 Tcl_AppendResult (interp, obj->recordElements, NULL);
2249 static int ir_diagResult (Tcl_Interp *interp, IrTcl_Diagnostic *list, int num)
2255 for (i = 0; i<num; i++)
2257 logf (LOG_DEBUG, "Diagnostic, code %d", list[i].condition);
2258 sprintf (buf, "%d", list[i].condition);
2259 Tcl_AppendElement (interp, buf);
2260 cp = diagbib1_str (list[i].condition);
2262 Tcl_AppendElement (interp, (char*) cp);
2264 Tcl_AppendElement (interp, "");
2265 if (list[i].addinfo)
2266 Tcl_AppendElement (interp, (char*) list[i].addinfo);
2268 Tcl_AppendElement (interp, "");
2274 * do_diag: Return diagnostic record info
2276 static int do_diag (void *o, Tcl_Interp *interp, int argc, char **argv)
2278 IrTcl_SetObj *obj = o;
2280 IrTcl_RecordList *rl;
2286 sprintf (interp->result, "wrong # args");
2289 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2291 rl = find_IR_record (obj, offset);
2294 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2297 if (rl->which != Z_NamePlusRecord_surrogateDiagnostic)
2299 Tcl_AppendResult (interp, "No Diagnostic record at #", argv[2], NULL);
2302 return ir_diagResult (interp, rl->u.surrogateDiagnostics.list,
2303 rl->u.surrogateDiagnostics.num);
2307 * do_getMarc: Get ISO2709 Record lines/fields
2309 static int do_getMarc (void *o, Tcl_Interp *interp, int argc, char **argv)
2311 IrTcl_SetObj *obj = o;
2313 IrTcl_RecordList *rl;
2319 sprintf (interp->result, "wrong # args");
2322 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2324 rl = find_IR_record (obj, offset);
2327 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2330 if (rl->which != Z_NamePlusRecord_databaseRecord)
2332 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2335 return ir_tcl_get_marc (interp, rl->u.dbrec.buf, argc, argv);
2339 * do_getSutrs: Get SUTRS Record
2341 static int do_getSutrs (void *o, Tcl_Interp *interp, int argc, char **argv)
2343 IrTcl_SetObj *obj = o;
2345 IrTcl_RecordList *rl;
2351 sprintf (interp->result, "wrong # args");
2354 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2356 rl = find_IR_record (obj, offset);
2359 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2362 if (rl->which != Z_NamePlusRecord_databaseRecord)
2364 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2367 if (rl->u.dbrec.type != VAL_SUTRS)
2369 Tcl_AppendElement (interp, rl->u.dbrec.buf);
2375 * do_getGrs: Get a GRS1 Record
2377 static int do_getGrs (void *o, Tcl_Interp *interp, int argc, char **argv)
2379 IrTcl_SetObj *obj = o;
2381 IrTcl_RecordList *rl;
2387 sprintf (interp->result, "wrong # args");
2390 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2392 rl = find_IR_record (obj, offset);
2395 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2398 if (rl->which != Z_NamePlusRecord_databaseRecord)
2400 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2403 if (rl->u.dbrec.type != VAL_GRS1)
2405 return ir_tcl_get_grs (interp, rl->u.dbrec.u.grs1, argc, argv);
2410 * do_responseStatus: Return response status (present or search)
2412 static int do_responseStatus (void *o, Tcl_Interp *interp,
2413 int argc, char **argv)
2415 IrTcl_SetObj *obj = o;
2419 obj->recordFlag = 0;
2420 obj->nonSurrogateDiagnosticNum = 0;
2421 obj->nonSurrogateDiagnosticList = NULL;
2424 else if (argc == -1)
2426 ir_deleteDiags (&obj->nonSurrogateDiagnosticList,
2427 &obj->nonSurrogateDiagnosticNum);
2430 if (!obj->recordFlag)
2432 Tcl_AppendElement (interp, "OK");
2437 case Z_Records_DBOSD:
2438 Tcl_AppendElement (interp, "DBOSD");
2441 Tcl_AppendElement (interp, "NSD");
2442 return ir_diagResult (interp, obj->nonSurrogateDiagnosticList,
2443 obj->nonSurrogateDiagnosticNum);
2449 * do_present: Perform Present Request
2452 static int do_present (void *o, Tcl_Interp *interp, int argc, char **argv)
2454 IrTcl_SetObj *obj = o;
2457 Z_PresentRequest *req;
2465 if (Tcl_GetInt (interp, argv[2], &start) == TCL_ERROR)
2472 if (Tcl_GetInt (interp, argv[3], &number) == TCL_ERROR)
2480 interp->result = "present: not connected";
2485 obj->number = number;
2487 apdu = zget_APDU (p->odr_out, Z_APDU_presentRequest);
2488 req = apdu->u.presentRequest;
2490 set_referenceId (p->odr_out, &req->referenceId,
2491 obj->set_inher.referenceId);
2493 req->resultSetId = obj->setName ? obj->setName : "Default";
2495 req->resultSetStartPoint = &start;
2496 req->numberOfRecordsRequested = &number;
2497 if (obj->set_inher.preferredRecordSyntax)
2499 struct oident ident;
2501 ident.proto = p->protocol_type;
2502 ident.oclass = CLASS_RECSYN;
2503 ident.value = *obj->set_inher.preferredRecordSyntax;
2504 logf (LOG_DEBUG, "Preferred record syntax is %d", ident.value);
2505 req->preferredRecordSyntax = odr_oiddup (p->odr_out,
2506 oid_getoidbyent (&ident));
2509 req->preferredRecordSyntax = 0;
2511 if (obj->set_inher.elementSetNames && *obj->set_inher.elementSetNames)
2513 Z_ElementSetNames *esn = odr_malloc (p->odr_out, sizeof(*esn));
2514 Z_RecordComposition *compo = odr_malloc (p->odr_out, sizeof(*compo));
2516 esn->which = Z_ElementSetNames_generic;
2517 esn->u.generic = obj->set_inher.elementSetNames;
2519 req->recordComposition = compo;
2520 compo->which = Z_RecordComp_simple;
2521 compo->u.simple = esn;
2524 req->recordComposition = NULL;
2525 return ir_tcl_send_APDU (interp, p, apdu, "present", *argv);
2529 * do_loadFile: Load result set from file
2532 static int do_loadFile (void *o, Tcl_Interp *interp,
2533 int argc, char **argv)
2535 IrTcl_SetObj *setobj = o;
2545 interp->result = "wrong # args";
2548 inf = fopen (argv[2], "r");
2551 Tcl_AppendResult (interp, "Cannot open ", argv[2], NULL);
2554 while ((buf = ir_tcl_fread_marc (inf, &size)))
2556 IrTcl_RecordList *rl;
2558 rl = new_IR_record (setobj, no, Z_NamePlusRecord_databaseRecord, "F");
2559 rl->u.dbrec.type = VAL_USMARC;
2560 rl->u.dbrec.buf = buf;
2561 rl->u.dbrec.size = size;
2564 setobj->numberOfRecordsReturned = no-1;
2569 static IrTcl_Method ir_set_method_tab[] = {
2570 { "search", do_search, NULL},
2571 { "searchResponse", do_searchResponse, NULL},
2572 { "presentResponse", do_presentResponse, NULL},
2573 { "searchStatus", do_searchStatus, NULL},
2574 { "presentStatus", do_presentStatus, NULL},
2575 { "nextResultSetPosition", do_nextResultSetPosition, NULL},
2576 { "setName", do_setName, NULL},
2577 { "resultCount", do_resultCount, NULL},
2578 { "numberOfRecordsReturned", do_numberOfRecordsReturned, NULL},
2579 { "present", do_present, NULL},
2580 { "type", do_type, NULL},
2581 { "getMarc", do_getMarc, NULL},
2582 { "getSutrs", do_getSutrs, NULL},
2583 { "getGrs", do_getGrs, NULL},
2584 { "recordType", do_recordType, NULL},
2585 { "recordElements", do_recordElements, NULL},
2586 { "diag", do_diag, NULL},
2587 { "responseStatus", do_responseStatus, NULL},
2588 { "loadFile", do_loadFile, NULL},
2593 * ir_set_obj_method: IR Set Object methods
2595 static int ir_set_obj_method (ClientData clientData, Tcl_Interp *interp,
2596 int argc, char **argv)
2598 IrTcl_Methods tabs[3];
2599 IrTcl_SetObj *p = clientData;
2603 interp->result = "wrong # args";
2606 tabs[0].tab = ir_set_method_tab;
2608 tabs[1].tab = ir_set_c_method_tab;
2609 tabs[1].obj = &p->set_inher;
2612 return ir_tcl_method (interp, argc, argv, tabs);
2616 * ir_set_obj_delete: IR Set Object disposal
2618 static void ir_set_obj_delete (ClientData clientData)
2620 IrTcl_Methods tabs[3];
2621 IrTcl_SetObj *p = clientData;
2623 logf (LOG_DEBUG, "ir set delete");
2625 tabs[0].tab = ir_set_method_tab;
2627 tabs[1].tab = ir_set_c_method_tab;
2628 tabs[1].obj = &p->set_inher;
2631 ir_tcl_method (NULL, -1, NULL, tabs);
2637 * ir_set_obj_mk: IR Set Object creation
2639 static int ir_set_obj_mk (ClientData clientData, Tcl_Interp *interp,
2640 int argc, char **argv)
2642 IrTcl_Methods tabs[3];
2645 if (argc < 2 || argc > 3)
2647 interp->result = "wrong # args";
2650 obj = ir_tcl_malloc (sizeof(*obj));
2651 logf (LOG_DEBUG, "ir set create");
2654 Tcl_CmdInfo parent_info;
2659 if (!Tcl_GetCommandInfo (interp, argv[2], &parent_info))
2661 interp->result = "No parent";
2664 obj->parent = (IrTcl_Obj *) parent_info.clientData;
2666 dst = &obj->set_inher;
2667 src = &obj->parent->set_inher;
2669 if ((dst->num_databaseNames = src->num_databaseNames))
2670 dst->databaseNames =
2671 ir_tcl_malloc (sizeof (*dst->databaseNames)
2672 * dst->num_databaseNames);
2674 dst->databaseNames = NULL;
2675 for (i = 0; i < dst->num_databaseNames; i++)
2676 if (ir_tcl_strdup (interp, &dst->databaseNames[i],
2677 src->databaseNames[i]) == TCL_ERROR)
2679 if (ir_tcl_strdup (interp, &dst->queryType, src->queryType)
2683 if (ir_tcl_strdup (interp, &dst->referenceId, src->referenceId)
2687 if (ir_tcl_strdup (interp, &dst->elementSetNames, src->elementSetNames)
2691 if (ir_tcl_strdup (interp, &dst->smallSetElementSetNames,
2692 src->smallSetElementSetNames)
2696 if (ir_tcl_strdup (interp, &dst->mediumSetElementSetNames,
2697 src->mediumSetElementSetNames)
2701 if (src->preferredRecordSyntax &&
2702 (dst->preferredRecordSyntax
2703 = ir_tcl_malloc (sizeof(*dst->preferredRecordSyntax))))
2704 *dst->preferredRecordSyntax = *src->preferredRecordSyntax;
2706 dst->preferredRecordSyntax = NULL;
2707 dst->replaceIndicator = src->replaceIndicator;
2708 dst->smallSetUpperBound = src->smallSetUpperBound;
2709 dst->largeSetLowerBound = src->largeSetLowerBound;
2710 dst->mediumSetPresentNumber = src->mediumSetPresentNumber;
2715 tabs[0].tab = ir_set_method_tab;
2719 if (ir_tcl_method (interp, 0, NULL, tabs) == TCL_ERROR)
2722 Tcl_CreateCommand (interp, argv[1], ir_set_obj_method,
2723 (ClientData) obj, ir_set_obj_delete);
2727 /* ------------------------------------------------------- */
2730 * do_scan: Perform scan
2732 static int do_scan (void *o, Tcl_Interp *interp, int argc, char **argv)
2736 IrTcl_ScanObj *obj = o;
2737 IrTcl_Obj *p = obj->parent;
2740 struct ccl_rpn_node *rpn;
2748 interp->result = "wrong # args";
2751 if (!p->set_inher.num_databaseNames)
2753 interp->result = "no databaseNames";
2758 interp->result = "scan: not connected";
2762 bib1.proto = p->protocol_type;
2763 bib1.oclass = CLASS_ATTSET;
2764 bib1.value = VAL_BIB1;
2766 apdu = zget_APDU (p->odr_out, Z_APDU_scanRequest);
2767 req = apdu->u.scanRequest;
2769 set_referenceId (p->odr_out, &req->referenceId, p->set_inher.referenceId);
2770 req->num_databaseNames = p->set_inher.num_databaseNames;
2771 req->databaseNames = p->set_inher.databaseNames;
2772 req->attributeSet = oid_getoidbyent (&bib1);
2775 if (!(req->termListAndStartPoint = p_query_scan (p->odr_out, argv[2])))
2777 Tcl_AppendResult (interp, "Syntax error in query", NULL);
2781 rpn = ccl_find_str(p->bibset, argv[2], &r, &pos);
2784 Tcl_AppendResult (interp, "CCL error: ", ccl_err_msg (r), NULL);
2787 ccl_pr_tree (rpn, stderr);
2788 fprintf (stderr, "\n");
2789 if (!(req->termListAndStartPoint = ccl_scan_query (rpn)))
2792 req->stepSize = &obj->stepSize;
2793 req->numberOfTermsRequested = &obj->numberOfTermsRequested;
2794 req->preferredPositionInResponse = &obj->preferredPositionInResponse;
2795 logf (LOG_DEBUG, "stepSize=%d", *req->stepSize);
2796 logf (LOG_DEBUG, "numberOfTermsRequested=%d",
2797 *req->numberOfTermsRequested);
2798 logf (LOG_DEBUG, "preferredPositionInResponse=%d",
2799 *req->preferredPositionInResponse);
2801 return ir_tcl_send_APDU (interp, p, apdu, "scan", *argv);
2805 * do_scanResponse: add scan response handler
2807 static int do_scanResponse (void *o, Tcl_Interp *interp,
2808 int argc, char **argv)
2810 IrTcl_ScanObj *obj = o;
2814 obj->scanResponse = NULL;
2817 else if (argc == -1)
2818 return ir_tcl_strdel (interp, &obj->scanResponse);
2821 free (obj->scanResponse);
2824 if (ir_tcl_strdup (interp, &obj->scanResponse, argv[2])
2829 obj->scanResponse = NULL;
2835 * do_stepSize: Set/get replace Step Size
2837 static int do_stepSize (void *obj, Tcl_Interp *interp,
2838 int argc, char **argv)
2840 IrTcl_ScanObj *p = obj;
2846 return get_set_int (&p->stepSize, interp, argc, argv);
2850 * do_numberOfTermsRequested: Set/get Number of Terms requested
2852 static int do_numberOfTermsRequested (void *obj, Tcl_Interp *interp,
2853 int argc, char **argv)
2855 IrTcl_ScanObj *p = obj;
2859 p->numberOfTermsRequested = 20;
2862 return get_set_int (&p->numberOfTermsRequested, interp, argc, argv);
2867 * do_preferredPositionInResponse: Set/get preferred Position
2869 static int do_preferredPositionInResponse (void *obj, Tcl_Interp *interp,
2870 int argc, char **argv)
2872 IrTcl_ScanObj *p = obj;
2876 p->preferredPositionInResponse = 1;
2879 return get_set_int (&p->preferredPositionInResponse, interp, argc, argv);
2883 * do_scanStatus: Get scan status
2885 static int do_scanStatus (void *obj, Tcl_Interp *interp,
2886 int argc, char **argv)
2888 IrTcl_ScanObj *p = obj;
2892 return get_set_int (&p->scanStatus, interp, argc, argv);
2896 * do_numberOfEntriesReturned: Get number of Entries returned
2898 static int do_numberOfEntriesReturned (void *obj, Tcl_Interp *interp,
2899 int argc, char **argv)
2901 IrTcl_ScanObj *p = obj;
2905 return get_set_int (&p->numberOfEntriesReturned, interp, argc, argv);
2909 * do_positionOfTerm: Get position of Term
2911 static int do_positionOfTerm (void *obj, Tcl_Interp *interp,
2912 int argc, char **argv)
2914 IrTcl_ScanObj *p = obj;
2918 return get_set_int (&p->positionOfTerm, interp, argc, argv);
2922 * do_scanLine: get Scan Line (surrogate or normal) after response
2924 static int do_scanLine (void *obj, Tcl_Interp *interp, int argc, char **argv)
2926 IrTcl_ScanObj *p = obj;
2932 p->entries_flag = 0;
2934 p->nonSurrogateDiagnosticNum = 0;
2935 p->nonSurrogateDiagnosticList = 0;
2938 else if (argc == -1)
2940 p->entries_flag = 0;
2941 /* release entries */
2944 ir_deleteDiags (&p->nonSurrogateDiagnosticList,
2945 &p->nonSurrogateDiagnosticNum);
2950 interp->result = "wrong # args";
2953 if (Tcl_GetInt (interp, argv[2], &i) == TCL_ERROR)
2955 if (!p->entries_flag || p->which != Z_ListEntries_entries || !p->entries
2956 || i >= p->num_entries || i < 0)
2958 switch (p->entries[i].which)
2960 case Z_Entry_termInfo:
2961 Tcl_AppendElement (interp, "T");
2962 if (p->entries[i].u.term.buf)
2963 Tcl_AppendElement (interp, p->entries[i].u.term.buf);
2965 Tcl_AppendElement (interp, "");
2966 sprintf (numstr, "%d", p->entries[i].u.term.globalOccurrences);
2967 Tcl_AppendElement (interp, numstr);
2969 case Z_Entry_surrogateDiagnostic:
2970 Tcl_AppendElement (interp, "SD");
2971 return ir_diagResult (interp, p->entries[i].u.diag.list,
2972 p->entries[i].u.diag.num);
2978 static IrTcl_Method ir_scan_method_tab[] = {
2979 { "scan", do_scan, NULL},
2980 { "scanResponse", do_scanResponse, NULL},
2981 { "stepSize", do_stepSize, NULL},
2982 { "numberOfTermsRequested", do_numberOfTermsRequested, NULL},
2983 { "preferredPositionInResponse", do_preferredPositionInResponse, NULL},
2984 { "scanStatus", do_scanStatus, NULL},
2985 { "numberOfEntriesReturned", do_numberOfEntriesReturned, NULL},
2986 { "positionOfTerm", do_positionOfTerm, NULL},
2987 { "scanLine", do_scanLine, NULL},
2992 * ir_scan_obj_method: IR Scan Object methods
2994 static int ir_scan_obj_method (ClientData clientData, Tcl_Interp *interp,
2995 int argc, char **argv)
2997 IrTcl_Methods tabs[2];
3001 interp->result = "wrong # args";
3004 tabs[0].tab = ir_scan_method_tab;
3005 tabs[0].obj = clientData;
3008 return ir_tcl_method (interp, argc, argv, tabs);
3012 * ir_scan_obj_delete: IR Scan Object disposal
3014 static void ir_scan_obj_delete (ClientData clientData)
3016 IrTcl_Methods tabs[2];
3017 IrTcl_ScanObj *obj = clientData;
3019 tabs[0].tab = ir_scan_method_tab;
3023 ir_tcl_method (NULL, -1, NULL, tabs);
3028 * ir_scan_obj_mk: IR Scan Object creation
3030 static int ir_scan_obj_mk (ClientData clientData, Tcl_Interp *interp,
3031 int argc, char **argv)
3033 Tcl_CmdInfo parent_info;
3035 IrTcl_Methods tabs[2];
3039 interp->result = "wrong # args";
3042 if (!Tcl_GetCommandInfo (interp, argv[2], &parent_info))
3044 interp->result = "No parent";
3047 obj = ir_tcl_malloc (sizeof(*obj));
3048 obj->parent = (IrTcl_Obj *) parent_info.clientData;
3050 tabs[0].tab = ir_scan_method_tab;
3054 if (ir_tcl_method (interp, 0, NULL, tabs) == TCL_ERROR)
3056 Tcl_CreateCommand (interp, argv[1], ir_scan_obj_method,
3057 (ClientData) obj, ir_scan_obj_delete);
3061 /* ------------------------------------------------------- */
3063 static void ir_initResponse (void *obj, Z_InitResponse *initrs)
3067 p->initResult = *initrs->result ? 1 : 0;
3068 if (!*initrs->result)
3069 logf (LOG_DEBUG, "Connection rejected by target");
3071 logf (LOG_DEBUG, "Connection accepted by target");
3073 get_referenceId (&p->set_inher.referenceId, initrs->referenceId);
3075 free (p->targetImplementationId);
3076 ir_tcl_strdup (p->interp, &p->targetImplementationId,
3077 initrs->implementationId);
3078 free (p->targetImplementationName);
3079 ir_tcl_strdup (p->interp, &p->targetImplementationName,
3080 initrs->implementationName);
3081 free (p->targetImplementationVersion);
3082 ir_tcl_strdup (p->interp, &p->targetImplementationVersion,
3083 initrs->implementationVersion);
3085 p->maximumRecordSize = *initrs->maximumRecordSize;
3086 p->preferredMessageSize = *initrs->preferredMessageSize;
3088 memcpy (&p->options, initrs->options, sizeof(initrs->options));
3089 memcpy (&p->protocolVersion, initrs->protocolVersion,
3090 sizeof(initrs->protocolVersion));
3091 free (p->userInformationField);
3092 p->userInformationField = NULL;
3093 if (initrs->userInformationField)
3097 if (initrs->userInformationField->which == ODR_EXTERNAL_octet &&
3098 (p->userInformationField =
3099 ir_tcl_malloc ((len =
3100 initrs->userInformationField->
3101 u.octet_aligned->len) +1)))
3103 memcpy (p->userInformationField,
3104 initrs->userInformationField->u.octet_aligned->buf,
3106 (p->userInformationField)[len] = '\0';
3111 static void ir_deleteDiags (IrTcl_Diagnostic **dst_list, int *dst_num)
3114 for (i = 0; i<*dst_num; i++)
3115 free (dst_list[i]->addinfo);
3121 static void ir_handleDiags (IrTcl_Diagnostic **dst_list, int *dst_num,
3122 Z_DiagRec **list, int num)
3128 *dst_list = ir_tcl_malloc (sizeof(**dst_list) * num);
3129 for (i = 0; i<num; i++)
3131 switch (list[i]->which)
3133 case Z_DiagRec_defaultFormat:
3134 (*dst_list)[i].condition = *list[i]->u.defaultFormat->condition;
3135 addinfo = list[i]->u.defaultFormat->addinfo;
3137 ((*dst_list)[i].addinfo = ir_tcl_malloc (strlen(addinfo)+1)))
3138 strcpy ((*dst_list)[i].addinfo, addinfo);
3141 (*dst_list)[i].addinfo = NULL;
3142 (*dst_list)[i].condition = 0;
3147 static void ir_handleRecords (void *o, Z_Records *zrs, IrTcl_SetObj *setobj,
3148 const char *elements)
3153 IrTcl_RecordList *rl;
3155 setobj->which = zrs->which;
3156 setobj->recordFlag = 1;
3158 ir_deleteDiags (&setobj->nonSurrogateDiagnosticList,
3159 &setobj->nonSurrogateDiagnosticNum);
3160 if (zrs->which == Z_Records_DBOSD)
3162 setobj->numberOfRecordsReturned =
3163 zrs->u.databaseOrSurDiagnostics->num_records;
3164 logf (LOG_DEBUG, "Got %d records", setobj->numberOfRecordsReturned);
3165 for (offset = 0; offset<setobj->numberOfRecordsReturned; offset++)
3167 rl = new_IR_record (setobj, setobj->start + offset,
3168 zrs->u.databaseOrSurDiagnostics->
3169 records[offset]->which,
3171 if (rl->which == Z_NamePlusRecord_surrogateDiagnostic)
3173 ir_handleDiags (&rl->u.surrogateDiagnostics.list,
3174 &rl->u.surrogateDiagnostics.num,
3175 &zrs->u.databaseOrSurDiagnostics->
3176 records[offset]->u.surrogateDiagnostic,
3181 Z_DatabaseRecord *zr;
3183 struct oident *ident;
3185 zr = zrs->u.databaseOrSurDiagnostics->records[offset]
3187 oe = (Z_External*) zr;
3188 rl->u.dbrec.size = zr->u.octet_aligned->len;
3190 if ((ident = oid_getentbyoid (oe->direct_reference)))
3191 rl->u.dbrec.type = ident->value;
3193 rl->u.dbrec.type = VAL_USMARC;
3195 if (oe->which == ODR_EXTERNAL_octet && rl->u.dbrec.size > 0)
3197 char *buf = (char*) zr->u.octet_aligned->buf;
3198 if ((rl->u.dbrec.buf = ir_tcl_malloc (rl->u.dbrec.size)))
3199 memcpy (rl->u.dbrec.buf, buf, rl->u.dbrec.size);
3201 else if (rl->u.dbrec.type == VAL_SUTRS &&
3202 oe->which == Z_External_sutrs)
3204 odr_setbuf (p->odr_in, (char*) oe->u.single_ASN1_type->buf,
3205 oe->u.single_ASN1_type->len, 0);
3206 if ((rl->u.dbrec.buf = ir_tcl_malloc (oe->u.sutrs->len+1)))
3208 memcpy (rl->u.dbrec.buf, oe->u.sutrs->buf,
3210 rl->u.dbrec.buf[oe->u.sutrs->len] = '\0';
3212 rl->u.dbrec.size = oe->u.sutrs->len;
3214 else if (rl->u.dbrec.type == VAL_GRS1 &&
3215 oe->which == Z_External_grs1)
3217 ir_tcl_read_grs (oe->u.grs1, &rl->u.dbrec.u.grs1);
3218 rl->u.dbrec.buf = NULL;
3221 rl->u.dbrec.buf = NULL;
3225 else if (zrs->which == Z_Records_multipleNSD)
3227 logf (LOG_DEBUG, "multipleNonSurrogateDiagnostic %d",
3228 zrs->u.multipleNonSurDiagnostics->num_diagRecs);
3229 setobj->numberOfRecordsReturned = 0;
3230 ir_handleDiags (&setobj->nonSurrogateDiagnosticList,
3231 &setobj->nonSurrogateDiagnosticNum,
3232 zrs->u.multipleNonSurDiagnostics->diagRecs,
3233 zrs->u.multipleNonSurDiagnostics->num_diagRecs);
3237 logf (LOG_DEBUG, "NonSurrogateDiagnostic");
3238 setobj->numberOfRecordsReturned = 0;
3239 ir_handleDiags (&setobj->nonSurrogateDiagnosticList,
3240 &setobj->nonSurrogateDiagnosticNum,
3241 &zrs->u.nonSurrogateDiagnostic,
3246 static void ir_searchResponse (void *o, Z_SearchResponse *searchrs,
3247 IrTcl_SetObj *setobj)
3249 Z_Records *zrs = searchrs->records;
3251 logf (LOG_DEBUG, "Received search response");
3254 logf (LOG_DEBUG, "Search response, no object!");
3257 setobj->searchStatus = searchrs->searchStatus ? 1 : 0;
3258 get_referenceId (&setobj->set_inher.referenceId, searchrs->referenceId);
3259 setobj->resultCount = *searchrs->resultCount;
3260 if (searchrs->presentStatus)
3261 setobj->presentStatus = *searchrs->presentStatus;
3262 if (searchrs->nextResultSetPosition)
3263 setobj->nextResultSetPosition = *searchrs->nextResultSetPosition;
3265 logf (LOG_DEBUG, "Search response %d, %d hits",
3266 setobj->searchStatus, setobj->resultCount);
3270 if (setobj->resultCount <= setobj->set_inher.smallSetUpperBound)
3271 es = setobj->set_inher.smallSetElementSetNames;
3273 es = setobj->set_inher.mediumSetElementSetNames;
3274 ir_handleRecords (o, zrs, setobj, es);
3277 setobj->recordFlag = 0;
3281 static void ir_presentResponse (void *o, Z_PresentResponse *presrs,
3282 IrTcl_SetObj *setobj)
3284 Z_Records *zrs = presrs->records;
3286 logf (LOG_DEBUG, "Received present response");
3289 logf (LOG_DEBUG, "Present response, no object!");
3292 setobj->presentStatus = *presrs->presentStatus;
3293 get_referenceId (&setobj->set_inher.referenceId, presrs->referenceId);
3294 setobj->nextResultSetPosition = *presrs->nextResultSetPosition;
3296 ir_handleRecords (o, zrs, setobj, setobj->set_inher.elementSetNames);
3299 setobj->recordFlag = 0;
3300 logf (LOG_DEBUG, "No records!");
3304 static void ir_scanResponse (void *o, Z_ScanResponse *scanrs,
3305 IrTcl_ScanObj *scanobj)
3309 logf (LOG_DEBUG, "Received scanResponse");
3311 get_referenceId (&p->set_inher.referenceId, scanrs->referenceId);
3312 scanobj->scanStatus = *scanrs->scanStatus;
3313 logf (LOG_DEBUG, "scanStatus=%d", scanobj->scanStatus);
3315 if (scanrs->stepSize)
3316 scanobj->stepSize = *scanrs->stepSize;
3317 logf (LOG_DEBUG, "stepSize=%d", scanobj->stepSize);
3319 scanobj->numberOfEntriesReturned = *scanrs->numberOfEntriesReturned;
3320 logf (LOG_DEBUG, "numberOfEntriesReturned=%d",
3321 scanobj->numberOfEntriesReturned);
3323 if (scanrs->positionOfTerm)
3324 scanobj->positionOfTerm = *scanrs->positionOfTerm;
3326 scanobj->positionOfTerm = -1;
3327 logf (LOG_DEBUG, "positionOfTerm=%d", scanobj->positionOfTerm);
3329 free (scanobj->entries);
3330 scanobj->entries = NULL;
3332 ir_deleteDiags (&scanobj->nonSurrogateDiagnosticList,
3333 &scanobj->nonSurrogateDiagnosticNum);
3334 if (scanrs->entries)
3339 scanobj->entries_flag = 1;
3340 scanobj->which = scanrs->entries->which;
3341 switch (scanobj->which)
3343 case Z_ListEntries_entries:
3344 scanobj->num_entries = scanrs->entries->u.entries->num_entries;
3345 scanobj->entries = ir_tcl_malloc (scanobj->num_entries *
3346 sizeof(*scanobj->entries));
3347 for (i=0; i<scanobj->num_entries; i++)
3349 ze = scanrs->entries->u.entries->entries[i];
3350 scanobj->entries[i].which = ze->which;
3353 case Z_Entry_termInfo:
3354 if (ze->u.termInfo->term->which == Z_Term_general)
3356 int l = ze->u.termInfo->term->u.general->len;
3357 scanobj->entries[i].u.term.buf = ir_tcl_malloc (1+l);
3358 memcpy (scanobj->entries[i].u.term.buf,
3359 ze->u.termInfo->term->u.general->buf,
3361 scanobj->entries[i].u.term.buf[l] = '\0';
3364 scanobj->entries[i].u.term.buf = NULL;
3365 if (ze->u.termInfo->globalOccurrences)
3366 scanobj->entries[i].u.term.globalOccurrences =
3367 *ze->u.termInfo->globalOccurrences;
3369 scanobj->entries[i].u.term.globalOccurrences = 0;
3371 case Z_Entry_surrogateDiagnostic:
3372 ir_handleDiags (&scanobj->entries[i].u.diag.list,
3373 &scanobj->entries[i].u.diag.num,
3374 &ze->u.surrogateDiagnostic,
3380 case Z_ListEntries_nonSurrogateDiagnostics:
3381 ir_handleDiags (&scanobj->nonSurrogateDiagnosticList,
3382 &scanobj->nonSurrogateDiagnosticNum,
3383 scanrs->entries->u.nonSurrogateDiagnostics->
3385 scanrs->entries->u.nonSurrogateDiagnostics->
3391 scanobj->entries_flag = 0;
3395 * ir_select_read: handle incoming packages
3397 static void ir_select_read (ClientData clientData)
3399 IrTcl_Obj *p = clientData;
3404 Tcl_CmdInfo cmd_info;
3405 const char *apdu_call;
3407 logf(LOG_DEBUG, "Read handler fd=%d", cs_fileno(p->cs_link));
3408 if (p->state == IR_TCL_R_Connecting)
3410 logf(LOG_DEBUG, "Connect handler");
3411 r = cs_rcvconnect (p->cs_link);
3414 logf (LOG_WARN, "cs_rcvconnect returned 1");
3417 p->state = IR_TCL_R_Idle;
3418 #if IRTCL_GENERIC_FILES
3419 ir_select_remove_write (p->csFile, p);
3421 ir_select_remove_write (cs_fileno (p->cs_link), p);
3425 logf (LOG_DEBUG, "cs_rcvconnect error");
3428 p->failInfo = IR_TCL_FAIL_CONNECT;
3429 IrTcl_eval (p->interp, p->failback);
3431 do_disconnect (p, NULL, 2, NULL);
3434 p->state = IR_TCL_R_Idle;
3436 IrTcl_eval (p->interp, p->callback);
3437 if (p->cs_link && p->request_queue && p->state == IR_TCL_R_Idle)
3438 ir_tcl_send_q (p, p->request_queue, "x");
3443 /* signal one more use of ir object - callbacks must not
3444 release the ir memory (p pointer) */
3445 p->state = IR_TCL_R_Reading;
3448 /* read incoming APDU */
3449 if ((r=cs_get (p->cs_link, &p->buf_in, &p->len_in)) <= 0)
3451 logf (LOG_DEBUG, "cs_get failed, code %d", r);
3452 #if IRTCL_GENERIC_FILES
3453 ir_select_remove (p->csFile, p);
3455 ir_select_remove (cs_fileno (p->cs_link), p);
3457 do_disconnect (p, NULL, 2, NULL);
3460 p->failInfo = IR_TCL_FAIL_READ;
3461 IrTcl_eval (p->interp, p->failback);
3463 /* release ir object now if callback deleted it */
3469 logf(LOG_DEBUG, "PDU Fraction read");
3472 /* got complete APDU. Now decode */
3475 odr_setbuf (p->odr_in, p->buf_in, r, 0);
3476 logf (LOG_DEBUG, "cs_get ok, total size %d", r);
3477 if (!z_APDU (p->odr_in, &apdu, 0))
3479 logf (LOG_DEBUG, "cs_get failed: %s",
3480 odr_errmsg (odr_geterror (p->odr_in)));
3481 do_disconnect (p, NULL, 2, NULL);
3484 p->failInfo = IR_TCL_FAIL_IN_APDU;
3485 p->apduOffset = odr_offset (p->odr_in);
3486 IrTcl_eval (p->interp, p->failback);
3488 /* release ir object now if failback deleted it */
3492 logf(LOG_DEBUG, "Decoded ok");
3493 /* handle APDU and invoke callback */
3494 rq = p->request_queue;
3497 logf (LOG_FATAL, "Internal error. No queue entry");
3500 object_name = rq->object_name;
3501 logf (LOG_DEBUG, "getCommandInfo (%s)", object_name);
3503 if (Tcl_GetCommandInfo (p->interp, object_name, &cmd_info))
3507 case Z_APDU_initResponse:
3508 p->eventType = "init";
3509 ir_initResponse (p, apdu->u.initResponse);
3510 apdu_call = p->initResponse;
3512 case Z_APDU_searchResponse:
3513 p->eventType = "search";
3514 ir_searchResponse (p, apdu->u.searchResponse,
3515 (IrTcl_SetObj *) cmd_info.clientData);
3516 apdu_call = ((IrTcl_SetObj *)
3517 cmd_info.clientData)->searchResponse;
3519 case Z_APDU_presentResponse:
3520 p->eventType = "present";
3521 ir_presentResponse (p, apdu->u.presentResponse,
3522 (IrTcl_SetObj *) cmd_info.clientData);
3523 apdu_call = ((IrTcl_SetObj *)
3524 cmd_info.clientData)->presentResponse;
3526 case Z_APDU_scanResponse:
3527 p->eventType = "scan";
3528 ir_scanResponse (p, apdu->u.scanResponse,
3529 (IrTcl_ScanObj *) cmd_info.clientData);
3530 apdu_call = ((IrTcl_ScanObj *)
3531 cmd_info.clientData)->scanResponse;
3534 logf (LOG_WARN, "Received unknown APDU type (%d)",
3536 do_disconnect (p, NULL, 2, NULL);
3539 p->failInfo = IR_TCL_FAIL_UNKNOWN_APDU;
3540 IrTcl_eval (p->interp, p->failback);
3545 p->request_queue = rq->next;
3546 p->state = IR_TCL_R_Idle;
3549 IrTcl_eval (p->interp, apdu_call);
3550 else if (rq->callback)
3551 IrTcl_eval (p->interp, rq->callback);
3553 free (rq->callback);
3554 free (rq->object_name);
3556 odr_reset (p->odr_in);
3557 if (p->ref_count == 1)
3563 } while (p->cs_link && cs_more (p->cs_link));
3564 if (p->cs_link && p->request_queue && p->state == IR_TCL_R_Idle)
3565 ir_tcl_send_q (p, p->request_queue, "x");
3569 * ir_select_write: handle outgoing packages - not yet written.
3571 static void ir_select_write (ClientData clientData)
3573 IrTcl_Obj *p = clientData;
3577 logf (LOG_DEBUG, "Write handler fd=%d", cs_fileno(p->cs_link));
3578 if (p->state == IR_TCL_R_Connecting)
3580 logf(LOG_DEBUG, "Connect handler");
3581 r = cs_rcvconnect (p->cs_link);
3584 p->state = IR_TCL_R_Idle;
3587 logf (LOG_DEBUG, "cs_rcvconnect error");
3588 #if IRTCL_GENERIC_FILES
3589 ir_select_remove_write (p->csFile, p);
3591 ir_select_remove_write (cs_fileno (p->cs_link), p);
3595 p->failInfo = IR_TCL_FAIL_CONNECT;
3596 IrTcl_eval (p->interp, p->failback);
3598 do_disconnect (p, NULL, 2, NULL);
3601 #if IRTCL_GENERIC_FILES
3602 ir_select_remove_write (p->csFile, p);
3604 ir_select_remove_write (cs_fileno (p->cs_link), p);
3607 IrTcl_eval (p->interp, p->callback);
3610 rq = p->request_queue;
3611 if (!rq || !rq->buf_out)
3614 if ((r=cs_put (p->cs_link, rq->buf_out, rq->len_out)) < 0)
3616 logf (LOG_DEBUG, "cs_put write fail");
3619 p->failInfo = IR_TCL_FAIL_WRITE;
3620 IrTcl_eval (p->interp, p->failback);
3624 do_disconnect (p, NULL, 2, NULL);
3626 else if (r == 0) /* remove select bit */
3628 logf(LOG_DEBUG, "Write completed");
3629 p->state = IR_TCL_R_Waiting;
3630 #if IRTCL_GENERIC_FILES
3631 ir_select_remove_write (p->csFile, p);
3633 ir_select_remove_write (cs_fileno (p->cs_link), p);
3640 static void ir_select_notify (ClientData clientData, int r, int w, int e)
3643 ir_select_read (clientData);
3645 ir_select_write (clientData);
3648 /* ------------------------------------------------------- */
3651 * Irtcl_init: Registration of TCL commands.
3653 int Irtcl_Init (Tcl_Interp *interp)
3655 Tcl_CreateCommand (interp, "ir", ir_obj_mk, (ClientData) NULL,
3656 (Tcl_CmdDeleteProc *) NULL);
3657 Tcl_CreateCommand (interp, "ir-set", ir_set_obj_mk,
3658 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
3659 Tcl_CreateCommand (interp, "ir-scan", ir_scan_obj_mk,
3660 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);