2 * IR toolkit for tcl/tk
4 * See the file LICENSE for details.
5 * Sebastian Hammer, Adam Dickmeiss
8 * Revision 1.78 1996-02-21 10:16:08 adam
9 * Simplified select handling. Only one function ir_tcl_select_set has
10 * to be externally defined.
12 * Revision 1.77 1996/02/20 17:52:58 adam
13 * Uses the YAZ oid system to name record syntax object identifiers.
15 * Revision 1.76 1996/02/20 16:09:51 adam
16 * Bug fix: didn't set element set names stamp correctly on result
17 * set records when element set names were set to the empty string.
19 * Revision 1.75 1996/02/19 15:41:53 adam
20 * Better log messages.
21 * Minor improvement of connect method.
23 * Revision 1.74 1996/02/05 17:58:03 adam
24 * Ported ir-tcl to use the beta releases of tcl7.5/tk4.1.
26 * Revision 1.73 1996/01/29 11:35:19 adam
27 * Bug fix: cs_type member renamed to comstackType to avoid conflict with
28 * cs_type macro defined by YAZ.
30 * Revision 1.72 1996/01/19 17:45:34 quinn
31 * Added debugging output
33 * Revision 1.71 1996/01/19 16:22:38 adam
34 * New method: apduDump - returns information about last incoming APDU.
36 * Revision 1.70 1996/01/10 09:18:34 adam
37 * PDU specific callbacks implemented: initRespnse, searchResponse,
38 * presentResponse and scanResponse.
39 * Bug fix in the command line shell (tclmain.c) - discovered on OSF/1.
41 * Revision 1.69 1996/01/04 16:12:12 adam
42 * Setting PDUType renamed to eventType.
44 * Revision 1.68 1996/01/04 11:05:22 adam
45 * New setting: PDUType - returns type of last PDU returned from the target.
46 * Fixed a bug in configure/Makefile.
48 * Revision 1.67 1996/01/03 09:00:51 adam
49 * Updated to use new version of Yaz (names changed to avoid C++ conflict).
51 * Revision 1.66 1995/11/28 17:26:39 adam
52 * Removed Carriage return from ir-tcl.c!
53 * Removed misc. debug logs.
55 * Revision 1.65 1995/11/28 13:53:00 quinn
58 * Revision 1.64 1995/11/13 15:39:18 adam
59 * Bug fix: {small,medium}SetElementSetNames weren't set correctly.
60 * Bug fix: idAuthentication weren't set correctly.
62 * Revision 1.63 1995/11/13 09:55:39 adam
63 * Multiple records at a position in a result-set with differnt
66 * Revision 1.62 1995/10/18 17:20:33 adam
67 * Work on target setup in client.tcl.
69 * Revision 1.61 1995/10/18 16:42:42 adam
70 * New settings: smallSetElementSetNames and mediumSetElementSetNames.
72 * Revision 1.60 1995/10/18 15:43:31 adam
73 * In search: mediumSetElementSetNames and smallSetElementSetNames are
74 * set to elementSetNames.
76 * Revision 1.59 1995/10/17 12:18:58 adam
77 * Bug fix: when target connection closed, the connection was not
78 * properly reestablished.
80 * Revision 1.58 1995/10/16 17:00:55 adam
81 * New setting: elementSetNames.
82 * Various client improvements. Medium presentation format looks better.
84 * Revision 1.57 1995/09/21 13:11:51 adam
85 * Support of dynamic loading.
86 * Test script uses load command if necessary.
88 * Revision 1.56 1995/08/29 15:30:14 adam
89 * Work on GRS records.
91 * Revision 1.55 1995/08/28 09:43:25 adam
92 * Minor changes. configure only searches for yaz beta 3 and versions after
95 * Revision 1.54 1995/08/24 12:25:16 adam
96 * Modified to work with yaz 1.0b3.
98 * Revision 1.53 1995/08/04 12:49:26 adam
99 * Bug fix: reading uninitialized variable p.
101 * Revision 1.52 1995/08/04 11:32:38 adam
102 * More work on output queue. Memory related routines moved
105 * Revision 1.51 1995/08/03 13:22:54 adam
108 * Revision 1.50 1995/07/20 08:09:49 adam
109 * client.tcl: Targets removed from hotTargets list when targets
110 * are removed/modified.
111 * ir-tcl.c: More work on triggerResourceControl.
113 * Revision 1.49 1995/06/30 12:39:21 adam
114 * Bug fix: loadFile didn't set record type.
115 * The MARC routines are a little less strict in the interpretation.
116 * Script display.tcl replaces the old marc.tcl.
117 * New interactive script: shell.tcl.
119 * Revision 1.48 1995/06/27 19:03:50 adam
120 * Bug fix in do_present in ir-tcl.c: p->set_child member weren't set.
121 * nextResultSetPosition used instead of setOffset.
123 * Revision 1.47 1995/06/25 10:25:04 adam
124 * Working on triggerResourceControl. Description of compile/install
125 * procedure moved to ir-tcl.sgml.
127 * Revision 1.46 1995/06/22 13:15:06 adam
128 * Feature: SUTRS. Setting getSutrs implemented.
129 * Work on display formats.
130 * Preferred record syntax can be set by the user.
132 * Revision 1.45 1995/06/20 08:07:30 adam
133 * New setting: failInfo.
134 * Working on better cancel mechanism.
136 * Revision 1.44 1995/06/19 17:01:20 adam
139 * Revision 1.43 1995/06/19 13:06:08 adam
140 * New define: IR_TCL_VERSION.
142 * Revision 1.42 1995/06/19 08:08:52 adam
143 * client.tcl: hotTargets now contain both database and target name.
144 * ir-tcl.c: setting protocol edited. Errors in callbacks are logged
145 * by logf(LOG_WARN, ...) calls.
147 * Revision 1.41 1995/06/16 12:28:16 adam
148 * Implemented preferredRecordSyntax.
149 * Minor changes in diagnostic handling.
150 * Record list deleted when connection closes.
152 * Revision 1.40 1995/06/14 13:37:18 adam
153 * Setting recordType implemented.
154 * Setting implementationVersion implemented.
155 * Settings implementationId / implementationName edited.
157 * Revision 1.39 1995/06/08 10:26:32 adam
158 * Bug fix in ir_strdup.
160 * Revision 1.38 1995/06/01 16:36:47 adam
161 * About buttons. Minor bug fixes.
163 * Revision 1.37 1995/06/01 07:31:20 adam
164 * Rename of many typedefs -> IrTcl_...
166 * Revision 1.36 1995/05/31 13:09:59 adam
167 * Client searches/presents may be interrupted.
168 * New moving book-logo.
170 * Revision 1.35 1995/05/31 08:36:33 adam
171 * Bug fix in client.tcl: didn't save options on clientrc.tcl.
172 * New method: referenceId. More work on scan.
174 * Revision 1.34 1995/05/29 10:33:42 adam
175 * README and rename of startup script.
177 * Revision 1.33 1995/05/29 09:15:11 quinn
178 * Changed CS_SR to PROTO_SR, etc.
180 * Revision 1.32 1995/05/29 08:44:16 adam
181 * Work on delete of objects.
183 * Revision 1.31 1995/05/26 11:44:10 adam
184 * Bugs fixed. More work on MARC utilities and queries. Test
185 * client is up-to-date again.
187 * Revision 1.30 1995/05/26 08:54:11 adam
188 * New MARC utilities. Uses prefix query.
190 * Revision 1.29 1995/05/24 14:10:22 adam
191 * Work on idAuthentication, protocolVersion and options.
193 * Revision 1.28 1995/05/23 15:34:48 adam
194 * Many new settings, userInformationField, smallSetUpperBound, etc.
195 * A number of settings are inherited when ir-set is executed.
196 * This version is incompatible with the graphical test client (client.tcl).
198 * Revision 1.27 1995/05/11 15:34:47 adam
199 * Scan request changed a bit. This version works with RLG.
201 * Revision 1.26 1995/04/18 16:11:51 adam
202 * First version of graphical Scan. Some work on query-by-form.
204 * Revision 1.25 1995/04/17 09:37:17 adam
205 * Further development of scan.
207 * Revision 1.24 1995/04/11 14:16:42 adam
208 * Further work on scan. Response works. Entries aren't saved yet.
210 * Revision 1.23 1995/04/10 10:50:27 adam
211 * Result-set name defaults to suffix of ir-set name.
212 * Started working on scan. Not finished at this point.
214 * Revision 1.22 1995/03/31 10:43:03 adam
215 * More robust when getting bad MARC records.
217 * Revision 1.21 1995/03/31 08:56:37 adam
218 * New button "Search".
220 * Revision 1.20 1995/03/29 16:07:09 adam
221 * Bug fix: Didn't use setName in present request.
223 * Revision 1.19 1995/03/28 12:45:23 adam
224 * New ir method failback: called on disconnect/protocol error.
225 * New ir set/get method: protocol: SR / Z3950.
226 * Simple popup and disconnect when failback is invoked.
228 * Revision 1.18 1995/03/21 15:50:12 adam
231 * Revision 1.17 1995/03/21 13:41:03 adam
232 * Comstack cs_create not used too often. Non-blocking connect.
234 * Revision 1.16 1995/03/21 08:26:06 adam
235 * New method, setName, to specify the result set name (other than Default).
236 * New method, responseStatus, which returns diagnostic info, if any, after
237 * present response / search response.
239 * Revision 1.15 1995/03/20 15:24:07 adam
240 * Diagnostic records saved on searchResponse.
242 * Revision 1.14 1995/03/20 08:53:22 adam
243 * Event loop in tclmain.c rewritten. New method searchStatus.
245 * Revision 1.13 1995/03/17 18:26:17 adam
246 * Non-blocking i/o used now. Database names popup as cascade items.
248 * Revision 1.12 1995/03/17 15:45:00 adam
249 * Improved target/database setup.
251 * Revision 1.11 1995/03/16 17:54:03 adam
252 * Minor changes really.
254 * Revision 1.10 1995/03/15 16:14:50 adam
255 * Blocking arg in cs_create changed.
257 * Revision 1.9 1995/03/15 13:59:24 adam
260 * Revision 1.8 1995/03/15 08:25:16 adam
261 * New method presentStatus to check for error on present. Misc. cleanup
262 * of IrTcl_RecordList manipulations. Full MARC record presentation in
265 * Revision 1.7 1995/03/14 17:32:29 adam
266 * Presentation of full Marc record in popup window.
268 * Revision 1.6 1995/03/12 19:31:55 adam
269 * Pattern matching implemented when retrieving MARC records. More
270 * diagnostic functions.
272 * Revision 1.5 1995/03/10 18:00:15 adam
273 * Actual presentation in line-by-line format. RPN query support.
275 * Revision 1.4 1995/03/09 16:15:08 adam
276 * First presentRequest attempts. Hot-target list.
288 #define IRTCL_GENERIC_FILES 0
295 int (*method) (void *obj, Tcl_Interp *interp, int argc, char **argv);
303 static void ir_deleteDiags (IrTcl_Diagnostic **dst_list, int *dst_num);
304 static int do_disconnect (void *obj, Tcl_Interp *interp,
305 int argc, char **argv);
307 static void ir_select_notify (ClientData clientData, int r, int w, int e);
309 void ir_select_add (int fd, void *obj)
311 ir_tcl_select_set (ir_select_notify, fd, obj, 1, 0, 0);
314 void ir_select_add_write (int fd, void *obj)
316 ir_tcl_select_set (ir_select_notify, fd, obj, 1, 1, 0);
319 void ir_select_remove (int fd, void *obj)
321 ir_tcl_select_set (NULL, fd, obj, 0, 0, 0);
324 void ir_select_remove_write (int fd, void *obj)
326 ir_tcl_select_set (ir_select_notify, fd, obj, 1, 0, 0);
329 static IrTcl_RecordList *new_IR_record (IrTcl_SetObj *setobj,
331 const char *elements)
333 IrTcl_RecordList *rl;
335 if (elements && !*elements)
337 for (rl = setobj->record_list; rl; rl = rl->next)
339 if (no == rl->no && (!rl->elements || !elements ||
340 !strcmp(elements, rl->elements)))
345 case Z_NamePlusRecord_databaseRecord:
346 free (rl->u.dbrec.buf);
347 rl->u.dbrec.buf = NULL;
349 case Z_NamePlusRecord_surrogateDiagnostic:
350 ir_deleteDiags (&rl->u.surrogateDiagnostics.list,
351 &rl->u.surrogateDiagnostics.num);
359 rl = ir_tcl_malloc (sizeof(*rl));
360 rl->next = setobj->record_list;
362 setobj->record_list = rl;
365 ir_tcl_strdup (NULL, &rl->elements, elements);
372 int IrTcl_eval (Tcl_Interp *interp, const char *command)
374 char *tmp = ir_tcl_malloc (strlen(command)+1);
377 strcpy (tmp, command);
378 r = Tcl_Eval (interp, tmp);
380 logf (LOG_WARN, "Tcl error in line %d: %s", interp->errorLine,
382 Tcl_FreeResult (interp);
388 * IrTcl_getRecordSyntaxStr: Return record syntax name of object id
390 static char *IrTcl_getRecordSyntaxStr (enum oid_value value)
393 struct oident ent, *entp;
395 ent.proto = PROTO_Z3950;
396 ent.oclass = CLASS_RECSYN;
399 o = oid_getoidbyent (&ent);
400 entp = oid_getentbyoid (o);
408 * IrTcl_getRecordSyntaxVal: Return record syntax value of string
410 static enum oid_value IrTcl_getRecordSyntaxVal (const char *name)
412 return oid_getvalbyname (name);
415 static IrTcl_RecordList *find_IR_record (IrTcl_SetObj *setobj, int no)
417 IrTcl_RecordList *rl;
419 for (rl = setobj->record_list; rl; rl = rl->next)
421 (!setobj->recordElements || !rl->elements ||
422 !strcmp (setobj->recordElements, rl->elements)))
427 static void delete_IR_records (IrTcl_SetObj *setobj)
429 IrTcl_RecordList *rl, *rl1;
431 for (rl = setobj->record_list; rl; rl = rl1)
435 case Z_NamePlusRecord_databaseRecord:
436 free (rl->u.dbrec.buf);
438 case Z_NamePlusRecord_surrogateDiagnostic:
439 ir_deleteDiags (&rl->u.surrogateDiagnostics.list,
440 &rl->u.surrogateDiagnostics.num);
446 setobj->record_list = NULL;
450 * get_set_int: Set/get integer value
452 static int get_set_int (int *val, Tcl_Interp *interp, int argc, char **argv)
458 if (Tcl_GetInt (interp, argv[2], val)==TCL_ERROR)
461 sprintf (buf, "%d", *val);
462 Tcl_AppendResult (interp, buf, NULL);
467 * ir_method: Search for method in table and invoke method handler
469 int ir_method (Tcl_Interp *interp, int argc, char **argv, IrTcl_Methods *tab)
471 IrTcl_Methods *tab_i = tab;
474 for (tab_i = tab; tab_i->tab; tab_i++)
475 for (t = tab_i->tab; t->name; t++)
478 if ((*t->method)(tab_i->obj, interp, argc, argv) == TCL_ERROR)
482 if (!strcmp (t->name, argv[1]))
483 return (*t->method)(tab_i->obj, interp, argc, argv);
487 Tcl_AppendResult (interp, "Bad method: ", argv[1],
488 ". Possible methods:", NULL);
489 for (tab_i = tab; tab_i->tab; tab_i++)
490 for (t = tab_i->tab; t->name; t++)
491 Tcl_AppendResult (interp, " ", t->name, NULL);
496 * ir_method_r: Get status for all readable elements
498 int ir_method_r (void *obj, Tcl_Interp *interp, int argc, char **argv,
506 for (; tab->name; tab++)
509 argv_n[1] = tab->name;
510 Tcl_AppendResult (interp, "{", NULL);
511 (*tab->method)(obj, interp, argc_n, argv_n);
512 Tcl_AppendResult (interp, "} ", NULL);
518 * ir_named_bits: get/set named bits
520 int ir_named_bits (struct ir_named_entry *tab, Odr_bitmask *ob,
521 Tcl_Interp *interp, int argc, char **argv)
523 struct ir_named_entry *ti;
528 for (no = 0; no < argc; no++)
530 for (ti = tab; ti->name; ti++)
531 if (!strcmp (argv[no], ti->name))
533 ODR_MASK_SET (ob, ti->pos);
538 Tcl_AppendResult (interp, "Bad bit mask: ", argv[no], NULL);
544 for (ti = tab; ti->name; ti++)
545 if (ODR_MASK_GET (ob, ti->pos))
546 Tcl_AppendElement (interp, ti->name);
550 static void set_referenceId (ODR o, Z_ReferenceId **dst, const char *src)
556 *dst = odr_malloc (o, sizeof(**dst));
557 (*dst)->size = (*dst)->len = strlen(src);
558 (*dst)->buf = odr_malloc (o, (*dst)->len);
559 memcpy ((*dst)->buf, src, (*dst)->len);
563 static void get_referenceId (char **dst, Z_ReferenceId *src)
571 *dst = ir_tcl_malloc (src->len+1);
572 memcpy (*dst, src->buf, src->len);
573 (*dst)[src->len] = '\0';
576 /* ------------------------------------------------------- */
579 * do_init_request: init method on IR object
581 static int do_init_request (void *obj, Tcl_Interp *interp,
582 int argc, char **argv)
592 interp->result = "init: not connected";
595 apdu = zget_APDU (p->odr_out, Z_APDU_initRequest);
596 req = apdu->u.initRequest;
598 set_referenceId (p->odr_out, &req->referenceId, p->set_inher.referenceId);
599 req->options = &p->options;
600 req->protocolVersion = &p->protocolVersion;
601 req->preferredMessageSize = &p->preferredMessageSize;
602 req->maximumRecordSize = &p->maximumRecordSize;
604 if (p->idAuthenticationGroupId)
606 Z_IdPass *pass = odr_malloc (p->odr_out, sizeof(*pass));
607 Z_IdAuthentication *auth = odr_malloc (p->odr_out, sizeof(*auth));
609 logf (LOG_DEBUG, "using pass authentication");
611 auth->which = Z_IdAuthentication_idPass;
612 auth->u.idPass = pass;
613 if (p->idAuthenticationGroupId && *p->idAuthenticationGroupId)
614 pass->groupId = p->idAuthenticationGroupId;
616 pass->groupId = NULL;
617 if (p->idAuthenticationUserId && *p->idAuthenticationUserId)
618 pass->userId = p->idAuthenticationUserId;
621 if (p->idAuthenticationPassword && *p->idAuthenticationPassword)
622 pass->password = p->idAuthenticationPassword;
624 pass->password = NULL;
625 req->idAuthentication = auth;
627 else if (!p->idAuthenticationOpen || !*p->idAuthenticationOpen)
628 req->idAuthentication = NULL;
631 Z_IdAuthentication *auth = odr_malloc (p->odr_out, sizeof(*auth));
633 logf (LOG_DEBUG, "using open authentication");
634 auth->which = Z_IdAuthentication_open;
635 auth->u.open = p->idAuthenticationOpen;
636 req->idAuthentication = auth;
638 req->implementationId = p->implementationId;
639 req->implementationName = p->implementationName;
640 req->implementationVersion = p->implementationVersion;
641 req->userInformationField = 0;
643 return ir_tcl_send_APDU (interp, p, apdu, "init", *argv);
647 * do_protocolVersion: Set protocol Version
649 static int do_protocolVersion (void *obj, Tcl_Interp *interp,
650 int argc, char **argv)
658 ODR_MASK_ZERO (&p->protocolVersion);
659 ODR_MASK_SET (&p->protocolVersion, 0);
660 ODR_MASK_SET (&p->protocolVersion, 1);
665 if (Tcl_GetInt (interp, argv[2], &version)==TCL_ERROR)
667 ODR_MASK_ZERO (&p->protocolVersion);
668 for (i = 0; i<version; i++)
669 ODR_MASK_SET (&p->protocolVersion, i);
671 for (i = 4; --i >= 0; )
672 if (ODR_MASK_GET (&p->protocolVersion, i))
674 sprintf (buf, "%d", i+1);
675 interp->result = buf;
680 * do_options: Set options
682 static int do_options (void *obj, Tcl_Interp *interp,
683 int argc, char **argv)
685 static struct ir_named_entry options_tab[] = {
689 { "resourceReport", 3 },
690 { "triggerResourceCtrl", 4},
691 { "resourceCtrl", 5},
695 { "extendedServices", 10},
696 { "level-1Segmentation", 11},
697 { "level-2Segmentation", 12},
698 { "concurrentOperations", 13},
699 { "namedResultSets", 14},
706 ODR_MASK_ZERO (&p->options);
707 ODR_MASK_SET (&p->options, 0);
708 ODR_MASK_SET (&p->options, 1);
709 ODR_MASK_SET (&p->options, 4);
710 ODR_MASK_SET (&p->options, 7);
711 ODR_MASK_SET (&p->options, 14);
714 return ir_named_bits (options_tab, &p->options, interp, argc-2, argv+2);
718 * do_apduInfo: Get APDU information
720 static int do_apduInfo (void *obj, Tcl_Interp *interp, int argc, char **argv)
728 sprintf (buf, "%d", p->apduLen);
729 Tcl_AppendElement (interp, buf);
730 sprintf (buf, "%d", p->apduOffset);
731 Tcl_AppendElement (interp, buf);
734 Tcl_AppendElement (interp, "");
737 apduf = fopen ("apdu.tmp", "w");
740 Tcl_AppendElement (interp, "");
743 odr_dumpBER (apduf, p->buf_in, p->apduLen);
745 if (!(apduf = fopen ("apdu.tmp", "r")))
746 Tcl_AppendElement (interp, "");
751 Tcl_AppendResult (interp, " {", NULL);
752 while ((c = getc (apduf)) != EOF)
756 Tcl_AppendResult (interp, buf, NULL);
759 Tcl_AppendResult (interp, "}", NULL);
766 * do_failInfo: Get fail information
768 static int do_failInfo (void *obj, Tcl_Interp *interp, int argc, char **argv)
778 sprintf (buf, "%d", p->failInfo);
784 case IR_TCL_FAIL_CONNECT:
785 cp = "connect failed";
787 case IR_TCL_FAIL_READ:
788 cp = "connection closed";
790 case IR_TCL_FAIL_WRITE:
791 cp = "connection closed";
793 case IR_TCL_FAIL_IN_APDU:
794 cp = "failed to decode incoming APDU";
796 case IR_TCL_FAIL_UNKNOWN_APDU:
802 Tcl_AppendElement (interp, buf);
803 Tcl_AppendElement (interp, cp);
808 * do_preferredMessageSize: Set/get preferred message size
810 static int do_preferredMessageSize (void *obj, Tcl_Interp *interp,
811 int argc, char **argv)
817 p->preferredMessageSize = 30000;
820 return get_set_int (&p->preferredMessageSize, interp, argc, argv);
824 * do_maximumRecordSize: Set/get maximum record size
826 static int do_maximumRecordSize (void *obj, Tcl_Interp *interp,
827 int argc, char **argv)
833 p->maximumRecordSize = 30000;
836 return get_set_int (&p->maximumRecordSize, interp, argc, argv);
840 * do_initResult: Get init result
842 static int do_initResult (void *obj, Tcl_Interp *interp,
843 int argc, char **argv)
849 return get_set_int (&p->initResult, interp, argc, argv);
854 * do_implementationName: Set/get Implementation Name.
856 static int do_implementationName (void *obj, Tcl_Interp *interp,
857 int argc, char **argv)
862 return ir_tcl_strdup (interp, &p->implementationName,
863 "Index Data/IrTcl on YAZ");
865 return ir_tcl_strdel (interp, &p->implementationName);
868 free (p->implementationName);
869 if (ir_tcl_strdup (interp, &p->implementationName, argv[2])
873 Tcl_AppendResult (interp, p->implementationName, (char*) NULL);
878 * do_implementationId: Get Implementation Id.
880 static int do_implementationId (void *obj, Tcl_Interp *interp,
881 int argc, char **argv)
886 return ir_tcl_strdup (interp, &p->implementationId, "YAZ (id=81)");
888 return ir_tcl_strdel (interp, &p->implementationId);
889 Tcl_AppendResult (interp, p->implementationId, (char*) NULL);
894 * do_implementationVersion: get Implementation Version.
896 static int do_implementationVersion (void *obj, Tcl_Interp *interp,
897 int argc, char **argv)
902 return ir_tcl_strdup (interp, &p->implementationVersion,
903 "YAZ: " YAZ_VERSION " / IrTcl: " IR_TCL_VERSION);
905 return ir_tcl_strdel (interp, &p->implementationVersion);
906 Tcl_AppendResult (interp, p->implementationVersion, (char*) NULL);
911 * do_targetImplementationName: Get Implementation Name of target.
913 static int do_targetImplementationName (void *obj, Tcl_Interp *interp,
914 int argc, char **argv)
920 p->targetImplementationName = NULL;
924 return ir_tcl_strdel (interp, &p->targetImplementationName);
925 Tcl_AppendResult (interp, p->targetImplementationName, (char*) NULL);
930 * do_targetImplementationId: Get Implementation Id of target
932 static int do_targetImplementationId (void *obj, Tcl_Interp *interp,
933 int argc, char **argv)
939 p->targetImplementationId = NULL;
943 return ir_tcl_strdel (interp, &p->targetImplementationId);
944 Tcl_AppendResult (interp, p->targetImplementationId, (char*) NULL);
949 * do_targetImplementationVersion: Get Implementation Version of target
951 static int do_targetImplementationVersion (void *obj, Tcl_Interp *interp,
952 int argc, char **argv)
958 p->targetImplementationVersion = NULL;
962 return ir_tcl_strdel (interp, &p->targetImplementationVersion);
963 Tcl_AppendResult (interp, p->targetImplementationVersion, (char*) NULL);
968 * do_idAuthentication: Set/get id Authentication
970 static int do_idAuthentication (void *obj, Tcl_Interp *interp,
971 int argc, char **argv)
975 if (argc >= 3 || argc == -1)
977 free (p->idAuthenticationOpen);
978 free (p->idAuthenticationGroupId);
979 free (p->idAuthenticationUserId);
980 free (p->idAuthenticationPassword);
982 if (argc >= 3 || argc <= 0)
984 p->idAuthenticationOpen = NULL;
985 p->idAuthenticationGroupId = NULL;
986 p->idAuthenticationUserId = NULL;
987 p->idAuthenticationPassword = NULL;
996 ir_tcl_strdup (interp, &p->idAuthenticationOpen, argv[2])
1003 ir_tcl_strdup (interp, &p->idAuthenticationGroupId, argv[2])
1007 ir_tcl_strdup (interp, &p->idAuthenticationUserId, argv[3])
1011 ir_tcl_strdup (interp, &p->idAuthenticationPassword, argv[4])
1016 if (p->idAuthenticationOpen)
1017 Tcl_AppendElement (interp, p->idAuthenticationOpen);
1018 else if (p->idAuthenticationGroupId)
1020 Tcl_AppendElement (interp, p->idAuthenticationGroupId);
1021 Tcl_AppendElement (interp, p->idAuthenticationUserId);
1022 Tcl_AppendElement (interp, p->idAuthenticationPassword);
1028 * do_connect: connect method on IR object
1030 static int do_connect (void *obj, Tcl_Interp *interp,
1031 int argc, char **argv)
1043 interp->result = "already connected";
1046 if (!strcmp (p->comstackType, "tcpip"))
1048 p->cs_link = cs_create (tcpip_type, CS_BLOCK, p->protocol_type);
1049 addr = tcpip_strtoaddr (argv[2]);
1052 interp->result = "tcpip_strtoaddr fail";
1055 logf (LOG_DEBUG, "tcp/ip connect %s", argv[2]);
1057 else if (!strcmp (p->comstackType, "mosi"))
1060 p->cs_link = cs_create (mosi_type, CS_BLOCK, p->protocol_type);
1061 addr = mosi_strtoaddr (argv[2]);
1064 interp->result = "mosi_strtoaddr fail";
1067 logf (LOG_DEBUG, "mosi connect %s", argv[2]);
1069 interp->result = "MOSI support not there";
1075 Tcl_AppendResult (interp, "Bad comstack type: ",
1076 p->comstackType, NULL);
1079 if (ir_tcl_strdup (interp, &p->hostname, argv[2]) == TCL_ERROR)
1081 #if IRTCL_GENERIC_FILES
1083 p->csFile = Tcl_GetFile (cs_fileno(p->cs_link), TCL_WIN_SOCKET);
1085 p->csFile = Tcl_GetFile (cs_fileno(p->cs_link), TCL_UNIX_FD);
1088 if ((r=cs_connect (p->cs_link, addr)) < 0)
1090 interp->result = "connect fail";
1091 do_disconnect (p, NULL, 2, NULL);
1094 logf(LOG_DEBUG, "cs_connect() returned %d fd=%d", r,
1095 cs_fileno(p->cs_link));
1096 p->eventType = "connect";
1097 #if IRTCL_GENERIC_FILES
1098 ir_select_add (p->csFile, p);
1100 ir_select_add (cs_fileno (p->cs_link), p);
1104 #if IRTCL_GENERIC_FILES
1105 ir_select_add_write (p->csFile, p);
1107 ir_select_add_write (cs_fileno (p->cs_link), p);
1109 p->state = IR_TCL_R_Connecting;
1113 p->state = IR_TCL_R_Idle;
1115 IrTcl_eval (p->interp, p->callback);
1119 Tcl_AppendResult (interp, p->hostname, NULL);
1124 * do_disconnect: disconnect method on IR object
1126 static int do_disconnect (void *obj, Tcl_Interp *interp,
1127 int argc, char **argv)
1133 p->state = IR_TCL_R_Idle;
1134 p->eventType = NULL;
1137 #if IRTCL_GENERIC_FILES
1144 logf(LOG_DEBUG, "Closing connection to %s", p->hostname);
1147 #if IRTCL_GENERIC_FILES
1148 ir_select_remove_write (p->csFile, p);
1149 ir_select_remove (p->csFile, p);
1151 ir_select_remove_write (cs_fileno (p->cs_link), p);
1152 ir_select_remove (cs_fileno (p->cs_link), p);
1155 odr_reset (p->odr_in);
1157 assert (p->cs_link);
1158 cs_close (p->cs_link);
1160 #if IRTCL_GENERIC_FILES
1161 Tcl_FreeFile (p->csFile);
1165 ODR_MASK_ZERO (&p->options);
1166 ODR_MASK_SET (&p->options, 0);
1167 ODR_MASK_SET (&p->options, 1);
1168 ODR_MASK_SET (&p->options, 4);
1169 ODR_MASK_SET (&p->options, 7);
1170 ODR_MASK_SET (&p->options, 14);
1172 ODR_MASK_ZERO (&p->protocolVersion);
1173 ODR_MASK_SET (&p->protocolVersion, 0);
1174 ODR_MASK_SET (&p->protocolVersion, 1);
1177 assert (!p->cs_link);
1182 * do_comstack: Set/get comstack method on IR object
1184 static int do_comstack (void *o, Tcl_Interp *interp,
1185 int argc, char **argv)
1190 return ir_tcl_strdup (interp, &obj->comstackType, "tcpip");
1191 else if (argc == -1)
1192 return ir_tcl_strdel (interp, &obj->comstackType);
1195 free (obj->comstackType);
1196 if (ir_tcl_strdup (interp, &obj->comstackType, argv[2]) == TCL_ERROR)
1199 Tcl_AppendElement (interp, obj->comstackType);
1204 * do_logLevel: Set log level
1206 static int do_logLevel (void *o, Tcl_Interp *interp,
1207 int argc, char **argv)
1212 log_init (log_mask_str (argv[2]), "", NULL);
1214 log_init (log_mask_str (argv[2]), argv[3], NULL);
1216 log_init (log_mask_str (argv[2]), argv[3], argv[4]);
1222 * do_eventType: Return type of last event
1224 static int do_eventType (void *obj, Tcl_Interp *interp,
1225 int argc, char **argv)
1231 p->eventType = NULL;
1234 Tcl_AppendElement (interp, p->eventType ? p->eventType : "");
1240 * do_callback: add callback
1242 static int do_callback (void *obj, Tcl_Interp *interp,
1243 int argc, char **argv)
1252 else if (argc == -1)
1253 return ir_tcl_strdel (interp, &p->callback);
1259 if (ir_tcl_strdup (interp, &p->callback, argv[2]) == TCL_ERROR)
1269 * do_failback: add error handle callback
1271 static int do_failback (void *obj, Tcl_Interp *interp,
1272 int argc, char **argv)
1281 else if (argc == -1)
1282 return ir_tcl_strdel (interp, &p->failback);
1288 if (ir_tcl_strdup (interp, &p->failback, argv[2]) == TCL_ERROR)
1298 * do_initResponse: add init response handler
1300 static int do_initResponse (void *obj, Tcl_Interp *interp,
1301 int argc, char **argv)
1307 p->initResponse = NULL;
1310 else if (argc == -1)
1311 return ir_tcl_strdel (interp, &p->initResponse);
1314 free (p->initResponse);
1317 if (ir_tcl_strdup (interp, &p->initResponse, argv[2]) == TCL_ERROR)
1321 p->initResponse = NULL;
1326 * do_protocol: Set/get protocol method on IR object
1328 static int do_protocol (void *o, Tcl_Interp *interp, int argc, char **argv)
1334 p->protocol_type = PROTO_Z3950;
1339 if (!strcmp (argv[2], "Z39"))
1340 p->protocol_type = PROTO_Z3950;
1341 else if (!strcmp (argv[2], "SR"))
1342 p->protocol_type = PROTO_SR;
1345 Tcl_AppendResult (interp, "Bad protocol: ", argv[2], NULL);
1350 switch (p->protocol_type)
1353 Tcl_AppendElement (interp, "Z39");
1356 Tcl_AppendElement (interp, "SR");
1363 * do_triggerResourceControl:
1365 static int do_triggerResourceControl (void *obj, Tcl_Interp *interp,
1366 int argc, char **argv)
1370 Z_TriggerResourceControlRequest *req;
1371 bool_t is_false = 0;
1377 interp->result = "triggerResourceControl: not connected";
1380 apdu = zget_APDU (p->odr_out, Z_APDU_triggerResourceControlRequest);
1381 req = apdu->u.triggerResourceControlRequest;
1382 *req->requestedAction = Z_TriggerResourceCtrl_cancel;
1383 req->resultSetWanted = &is_false;
1385 return ir_tcl_send_APDU (interp, p, apdu, "triggerResourceControl",
1390 * do_databaseNames: specify database names
1392 static int do_databaseNames (void *obj, Tcl_Interp *interp,
1393 int argc, char **argv)
1396 IrTcl_SetCObj *p = obj;
1400 for (i=0; i<p->num_databaseNames; i++)
1401 free (p->databaseNames[i]);
1402 free (p->databaseNames);
1406 p->num_databaseNames = 0;
1407 p->databaseNames = NULL;
1412 for (i=0; i<p->num_databaseNames; i++)
1413 Tcl_AppendElement (interp, p->databaseNames[i]);
1416 if (p->databaseNames)
1418 for (i=0; i<p->num_databaseNames; i++)
1419 free (p->databaseNames[i]);
1420 free (p->databaseNames);
1422 p->num_databaseNames = argc - 2;
1424 ir_tcl_malloc (sizeof(*p->databaseNames) * p->num_databaseNames);
1425 for (i=0; i<p->num_databaseNames; i++)
1427 if (ir_tcl_strdup (interp, &p->databaseNames[i], argv[2+i])
1435 * do_replaceIndicator: Set/get replace Set indicator
1437 static int do_replaceIndicator (void *obj, Tcl_Interp *interp,
1438 int argc, char **argv)
1440 IrTcl_SetCObj *p = obj;
1444 p->replaceIndicator = 1;
1447 return get_set_int (&p->replaceIndicator, interp, argc, argv);
1451 * do_queryType: Set/Get query method
1453 static int do_queryType (void *obj, Tcl_Interp *interp,
1454 int argc, char **argv)
1456 IrTcl_SetCObj *p = obj;
1459 return ir_tcl_strdup (interp, &p->queryType, "rpn");
1460 else if (argc == -1)
1461 return ir_tcl_strdel (interp, &p->queryType);
1464 free (p->queryType);
1465 if (ir_tcl_strdup (interp, &p->queryType, argv[2]) == TCL_ERROR)
1468 Tcl_AppendResult (interp, p->queryType, NULL);
1473 * do_userInformationField: Get User information field
1475 static int do_userInformationField (void *obj, Tcl_Interp *interp,
1476 int argc, char **argv)
1482 p->userInformationField = NULL;
1485 else if (argc == -1)
1486 return ir_tcl_strdel (interp, &p->userInformationField);
1487 Tcl_AppendResult (interp, p->userInformationField, NULL);
1492 * do_smallSetUpperBound: Set/get small set upper bound
1494 static int do_smallSetUpperBound (void *o, Tcl_Interp *interp,
1495 int argc, char **argv)
1497 IrTcl_SetCObj *p = o;
1501 p->smallSetUpperBound = 0;
1504 return get_set_int (&p->smallSetUpperBound, interp, argc, argv);
1508 * do_largeSetLowerBound: Set/get large set lower bound
1510 static int do_largeSetLowerBound (void *o, Tcl_Interp *interp,
1511 int argc, char **argv)
1513 IrTcl_SetCObj *p = o;
1517 p->largeSetLowerBound = 2;
1520 return get_set_int (&p->largeSetLowerBound, interp, argc, argv);
1524 * do_mediumSetPresentNumber: Set/get large set lower bound
1526 static int do_mediumSetPresentNumber (void *o, Tcl_Interp *interp,
1527 int argc, char **argv)
1529 IrTcl_SetCObj *p = o;
1533 p->mediumSetPresentNumber = 0;
1536 return get_set_int (&p->mediumSetPresentNumber, interp, argc, argv);
1540 * do_referenceId: Set/Get referenceId
1542 static int do_referenceId (void *obj, Tcl_Interp *interp,
1543 int argc, char **argv)
1545 IrTcl_SetCObj *p = obj;
1549 p->referenceId = NULL;
1552 else if (argc == -1)
1553 return ir_tcl_strdel (interp, &p->referenceId);
1556 free (p->referenceId);
1557 if (ir_tcl_strdup (interp, &p->referenceId, argv[2]) == TCL_ERROR)
1560 Tcl_AppendResult (interp, p->referenceId, NULL);
1565 * do_preferredRecordSyntax: Set/get preferred record syntax
1567 static int do_preferredRecordSyntax (void *obj, Tcl_Interp *interp,
1568 int argc, char **argv)
1570 IrTcl_SetCObj *p = obj;
1574 p->preferredRecordSyntax = NULL;
1577 else if (argc == -1)
1579 free (p->preferredRecordSyntax);
1580 p->preferredRecordSyntax = NULL;
1585 free (p->preferredRecordSyntax);
1586 p->preferredRecordSyntax = NULL;
1587 if (argv[2][0] && (p->preferredRecordSyntax =
1588 ir_tcl_malloc (sizeof(*p->preferredRecordSyntax))))
1589 *p->preferredRecordSyntax = IrTcl_getRecordSyntaxVal (argv[2]);
1593 Tcl_AppendElement (interp, IrTcl_getRecordSyntaxStr
1594 (*p->preferredRecordSyntax));
1601 * do_elementSetNames: Set/Get element Set Names
1603 static int do_elementSetNames (void *obj, Tcl_Interp *interp,
1604 int argc, char **argv)
1606 IrTcl_SetCObj *p = obj;
1610 p->elementSetNames = NULL;
1613 else if (argc == -1)
1614 return ir_tcl_strdel (interp, &p->elementSetNames);
1617 free (p->elementSetNames);
1618 if (ir_tcl_strdup (interp, &p->elementSetNames, argv[2]) == TCL_ERROR)
1621 Tcl_AppendResult (interp, p->elementSetNames, NULL);
1626 * do_smallSetElementSetNames: Set/Get small Set Element Set Names
1628 static int do_smallSetElementSetNames (void *obj, Tcl_Interp *interp,
1629 int argc, char **argv)
1631 IrTcl_SetCObj *p = obj;
1635 p->smallSetElementSetNames = NULL;
1638 else if (argc == -1)
1639 return ir_tcl_strdel (interp, &p->smallSetElementSetNames);
1642 free (p->smallSetElementSetNames);
1643 if (ir_tcl_strdup (interp, &p->smallSetElementSetNames,
1644 argv[2]) == TCL_ERROR)
1647 Tcl_AppendResult (interp, p->smallSetElementSetNames, NULL);
1652 * do_mediumSetElementSetNames: Set/Get medium Set Element Set Names
1654 static int do_mediumSetElementSetNames (void *obj, Tcl_Interp *interp,
1655 int argc, char **argv)
1657 IrTcl_SetCObj *p = obj;
1661 p->mediumSetElementSetNames = NULL;
1664 else if (argc == -1)
1665 return ir_tcl_strdel (interp, &p->mediumSetElementSetNames);
1668 free (p->mediumSetElementSetNames);
1669 if (ir_tcl_strdup (interp, &p->mediumSetElementSetNames,
1670 argv[2]) == TCL_ERROR)
1673 Tcl_AppendResult (interp, p->mediumSetElementSetNames, NULL);
1677 static IrTcl_Method ir_method_tab[] = {
1678 { 1, "comstack", do_comstack },
1679 { 1, "protocol", do_protocol },
1680 { 0, "failback", do_failback },
1681 { 0, "failInfo", do_failInfo },
1682 { 0, "apduInfo", do_apduInfo },
1683 { 0, "logLevel", do_logLevel },
1685 { 0, "eventType", do_eventType },
1686 { 1, "connect", do_connect },
1687 { 0, "protocolVersion", do_protocolVersion },
1688 { 1, "preferredMessageSize", do_preferredMessageSize },
1689 { 1, "maximumRecordSize", do_maximumRecordSize },
1690 { 1, "implementationName", do_implementationName },
1691 { 1, "implementationId", do_implementationId },
1692 { 1, "implementationVersion", do_implementationVersion },
1693 { 0, "targetImplementationName", do_targetImplementationName },
1694 { 0, "targetImplementationId", do_targetImplementationId },
1695 { 0, "targetImplementationVersion", do_targetImplementationVersion },
1696 { 0, "userInformationField", do_userInformationField },
1697 { 1, "idAuthentication", do_idAuthentication },
1698 { 0, "options", do_options },
1699 { 0, "init", do_init_request },
1700 { 0, "initResult", do_initResult },
1701 { 0, "disconnect", do_disconnect },
1702 { 0, "callback", do_callback },
1703 { 0, "initResponse", do_initResponse },
1704 { 0, "triggerResourceControl", do_triggerResourceControl },
1705 { 0, "initResponse", do_initResponse },
1709 static IrTcl_Method ir_set_c_method_tab[] = {
1710 { 0, "databaseNames", do_databaseNames},
1711 { 0, "replaceIndicator", do_replaceIndicator},
1712 { 0, "queryType", do_queryType },
1713 { 0, "preferredRecordSyntax", do_preferredRecordSyntax },
1714 { 0, "smallSetUpperBound", do_smallSetUpperBound},
1715 { 0, "largeSetLowerBound", do_largeSetLowerBound},
1716 { 0, "mediumSetPresentNumber", do_mediumSetPresentNumber},
1717 { 0, "referenceId", do_referenceId },
1718 { 0, "elementSetNames", do_elementSetNames },
1719 { 0, "smallSetElementSetNames", do_smallSetElementSetNames },
1720 { 0, "mediumSetElementSetNames", do_mediumSetElementSetNames },
1725 * ir_obj_method: IR Object methods
1727 static int ir_obj_method (ClientData clientData, Tcl_Interp *interp,
1728 int argc, char **argv)
1730 IrTcl_Methods tab[3];
1731 IrTcl_Obj *p = clientData;
1734 return ir_method_r (clientData, interp, argc, argv, ir_method_tab);
1736 tab[0].tab = ir_method_tab;
1738 tab[1].tab = ir_set_c_method_tab;
1739 tab[1].obj = &p->set_inher;
1742 return ir_method (interp, argc, argv, tab);
1746 * ir_obj_delete: IR Object disposal
1748 static void ir_obj_delete (ClientData clientData)
1750 IrTcl_Obj *obj = clientData;
1751 IrTcl_Methods tab[3];
1754 if (obj->ref_count > 0)
1756 assert (obj->ref_count == 0);
1758 logf (LOG_DEBUG, "ir object delete");
1759 tab[0].tab = ir_method_tab;
1761 tab[1].tab = ir_set_c_method_tab;
1762 tab[1].obj = &obj->set_inher;
1765 ir_method (NULL, -1, NULL, tab);
1768 odr_destroy (obj->odr_in);
1769 odr_destroy (obj->odr_out);
1770 odr_destroy (obj->odr_pr);
1775 * ir_obj_mk: IR Object creation
1777 static int ir_obj_mk (ClientData clientData, Tcl_Interp *interp,
1778 int argc, char **argv)
1780 IrTcl_Methods tab[3];
1788 interp->result = "wrong # args";
1791 obj = ir_tcl_malloc (sizeof(*obj));
1794 obj->bibset = ccl_qual_mk ();
1795 if ((inf = fopen ("default.bib", "r")))
1797 ccl_qual_file (obj->bibset, inf);
1802 logf (LOG_DEBUG, "ir object create %s", argv[1]);
1803 obj->odr_in = odr_createmem (ODR_DECODE);
1804 obj->odr_out = odr_createmem (ODR_ENCODE);
1805 obj->odr_pr = odr_createmem (ODR_PRINT);
1806 obj->state = IR_TCL_R_Idle;
1807 obj->interp = interp;
1811 obj->request_queue = NULL;
1813 tab[0].tab = ir_method_tab;
1815 tab[1].tab = ir_set_c_method_tab;
1816 tab[1].obj = &obj->set_inher;
1819 if (ir_method (interp, 0, NULL, tab) == TCL_ERROR)
1821 Tcl_CreateCommand (interp, argv[1], ir_obj_method,
1822 (ClientData) obj, ir_obj_delete);
1826 /* ------------------------------------------------------- */
1828 * do_search: Do search request
1830 static int do_search (void *o, Tcl_Interp *interp, int argc, char **argv)
1832 Z_SearchRequest *req;
1836 IrTcl_SetObj *obj = o;
1847 interp->result = "wrong # args";
1850 if (!obj->set_inher.num_databaseNames)
1852 interp->result = "no databaseNames";
1857 interp->result = "search: not connected";
1860 apdu = zget_APDU (p->odr_out, Z_APDU_searchRequest);
1861 req = apdu->u.searchRequest;
1865 bib1.proto = p->protocol_type;
1866 bib1.oclass = CLASS_ATTSET;
1867 bib1.value = VAL_BIB1;
1869 set_referenceId (p->odr_out, &req->referenceId,
1870 obj->set_inher.referenceId);
1872 req->smallSetUpperBound = &obj->set_inher.smallSetUpperBound;
1873 req->largeSetLowerBound = &obj->set_inher.largeSetLowerBound;
1874 req->mediumSetPresentNumber = &obj->set_inher.mediumSetPresentNumber;
1875 req->replaceIndicator = &obj->set_inher.replaceIndicator;
1876 req->resultSetName = obj->setName ? obj->setName : "Default";
1877 logf (LOG_DEBUG, "Search, resultSetName %s", req->resultSetName);
1878 req->num_databaseNames = obj->set_inher.num_databaseNames;
1879 req->databaseNames = obj->set_inher.databaseNames;
1880 for (r=0; r < obj->set_inher.num_databaseNames; r++)
1881 logf (LOG_DEBUG, " Database %s", obj->set_inher.databaseNames[r]);
1882 if (obj->set_inher.preferredRecordSyntax)
1884 struct oident ident;
1886 ident.proto = p->protocol_type;
1887 ident.oclass = CLASS_RECSYN;
1888 ident.value = *obj->set_inher.preferredRecordSyntax;
1889 logf (LOG_DEBUG, "Preferred record syntax is %d", ident.value);
1890 req->preferredRecordSyntax = odr_oiddup (p->odr_out,
1891 oid_getoidbyent (&ident));
1894 req->preferredRecordSyntax = 0;
1896 if (obj->set_inher.smallSetElementSetNames &&
1897 *obj->set_inher.smallSetElementSetNames)
1899 Z_ElementSetNames *esn = odr_malloc (p->odr_out, sizeof(*esn));
1901 esn->which = Z_ElementSetNames_generic;
1902 esn->u.generic = obj->set_inher.smallSetElementSetNames;
1903 req->smallSetElementSetNames = esn;
1906 req->smallSetElementSetNames = NULL;
1908 if (obj->set_inher.mediumSetElementSetNames &&
1909 *obj->set_inher.mediumSetElementSetNames)
1911 Z_ElementSetNames *esn = odr_malloc (p->odr_out, sizeof(*esn));
1913 esn->which = Z_ElementSetNames_generic;
1914 esn->u.generic = obj->set_inher.mediumSetElementSetNames;
1915 req->mediumSetElementSetNames = esn;
1918 req->mediumSetElementSetNames = NULL;
1920 req->query = &query;
1922 if (!strcmp (obj->set_inher.queryType, "rpn"))
1924 Z_RPNQuery *RPNquery;
1926 RPNquery = p_query_rpn (p->odr_out, argv[2]);
1929 Tcl_AppendResult (interp, "Syntax error in query", NULL);
1932 RPNquery->attributeSetId = oid_getoidbyent (&bib1);
1933 query.which = Z_Query_type_1;
1934 query.u.type_1 = RPNquery;
1935 logf (LOG_DEBUG, "RPN");
1938 else if (!strcmp (obj->set_inher.queryType, "cclrpn"))
1942 struct ccl_rpn_node *rpn;
1943 Z_RPNQuery *RPNquery;
1945 rpn = ccl_find_str(p->bibset, argv[2], &error, &pos);
1948 Tcl_AppendResult (interp, "CCL error: ",
1949 ccl_err_msg(error), NULL);
1952 ccl_pr_tree (rpn, stderr);
1953 fprintf (stderr, "\n");
1954 assert((RPNquery = ccl_rpn_query(rpn)));
1955 RPNquery->attributeSetId = oid_getoidbyent (&bib1);
1956 query.which = Z_Query_type_1;
1957 query.u.type_1 = RPNquery;
1958 logf (LOG_DEBUG, "CCLRPN");
1961 else if (!strcmp (obj->set_inher.queryType, "ccl"))
1963 query.which = Z_Query_type_2;
1964 query.u.type_2 = &ccl_query;
1965 ccl_query.buf = (unsigned char *) argv[2];
1966 ccl_query.len = strlen (argv[2]);
1967 logf (LOG_DEBUG, "CCL");
1971 interp->result = "unknown query method";
1974 return ir_tcl_send_APDU (interp, p, apdu, "search", *argv);
1978 * do_searchResponse: add search response handler
1980 static int do_searchResponse (void *o, Tcl_Interp *interp,
1981 int argc, char **argv)
1983 IrTcl_SetObj *obj = o;
1987 obj->searchResponse = NULL;
1990 else if (argc == -1)
1991 return ir_tcl_strdel (interp, &obj->searchResponse);
1994 free (obj->searchResponse);
1997 if (ir_tcl_strdup (interp, &obj->searchResponse, argv[2])
2002 obj->searchResponse = NULL;
2008 * do_presentResponse: add present response handler
2010 static int do_presentResponse (void *o, Tcl_Interp *interp,
2011 int argc, char **argv)
2013 IrTcl_SetObj *obj = o;
2017 obj->presentResponse = NULL;
2020 else if (argc == -1)
2021 return ir_tcl_strdel (interp, &obj->presentResponse);
2024 free (obj->presentResponse);
2027 if (ir_tcl_strdup (interp, &obj->presentResponse, argv[2])
2032 obj->presentResponse = NULL;
2038 * do_resultCount: Get number of hits
2040 static int do_resultCount (void *o, Tcl_Interp *interp,
2041 int argc, char **argv)
2043 IrTcl_SetObj *obj = o;
2047 obj->resultCount = 0;
2050 return get_set_int (&obj->resultCount, interp, argc, argv);
2054 * do_searchStatus: Get search status (after search response)
2056 static int do_searchStatus (void *o, Tcl_Interp *interp,
2057 int argc, char **argv)
2059 IrTcl_SetObj *obj = o;
2063 return get_set_int (&obj->searchStatus, interp, argc, argv);
2067 * do_presentStatus: Get search status (after search/present response)
2069 static int do_presentStatus (void *o, Tcl_Interp *interp,
2070 int argc, char **argv)
2072 IrTcl_SetObj *obj = o;
2076 return get_set_int (&obj->presentStatus, interp, argc, argv);
2080 * do_nextResultSetPosition: Get next result set position
2081 * (after search/present response)
2083 static int do_nextResultSetPosition (void *o, Tcl_Interp *interp,
2084 int argc, char **argv)
2086 IrTcl_SetObj *obj = o;
2090 obj->nextResultSetPosition = 0;
2093 return get_set_int (&obj->nextResultSetPosition, interp, argc, argv);
2097 * do_setName: Set result Set name
2099 static int do_setName (void *o, Tcl_Interp *interp,
2100 int argc, char **argv)
2102 IrTcl_SetObj *obj = o;
2105 return ir_tcl_strdup (interp, &obj->setName, "Default");
2106 else if (argc == -1)
2107 return ir_tcl_strdel (interp, &obj->setName);
2110 free (obj->setName);
2111 if (ir_tcl_strdup (interp, &obj->setName, argv[2])
2115 Tcl_AppendElement (interp, obj->setName);
2120 * do_numberOfRecordsReturned: Get number of records returned
2122 static int do_numberOfRecordsReturned (void *o, Tcl_Interp *interp,
2123 int argc, char **argv)
2125 IrTcl_SetObj *obj = o;
2129 obj->numberOfRecordsReturned = 0;
2132 return get_set_int (&obj->numberOfRecordsReturned, interp, argc, argv);
2136 * do_type: Return type (if any) at position.
2138 static int do_type (void *o, Tcl_Interp *interp, int argc, char **argv)
2140 IrTcl_SetObj *obj = o;
2142 IrTcl_RecordList *rl;
2146 obj->record_list = NULL;
2149 else if (argc == -1)
2151 delete_IR_records (obj);
2156 sprintf (interp->result, "wrong # args");
2159 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2161 rl = find_IR_record (obj, offset);
2164 logf (LOG_DEBUG, "No record at position %d", offset);
2169 case Z_NamePlusRecord_databaseRecord:
2170 interp->result = "DB";
2172 case Z_NamePlusRecord_surrogateDiagnostic:
2173 interp->result = "SD";
2181 * do_recordType: Return record type (if any) at position.
2183 static int do_recordType (void *o, Tcl_Interp *interp, int argc, char **argv)
2185 IrTcl_SetObj *obj = o;
2187 IrTcl_RecordList *rl;
2193 else if (argc == -1)
2199 sprintf (interp->result, "wrong # args");
2202 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2204 rl = find_IR_record (obj, offset);
2207 if (rl->which != Z_NamePlusRecord_databaseRecord)
2209 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2212 Tcl_AppendElement (interp, (char*)
2213 IrTcl_getRecordSyntaxStr (rl->u.dbrec.type));
2218 * set record elements (for record extraction)
2220 static int do_recordElements (void *o, Tcl_Interp *interp,
2221 int argc, char **argv)
2223 IrTcl_SetObj *obj = o;
2227 obj->recordElements = NULL;
2230 else if (argc == -1)
2231 return ir_tcl_strdel (NULL, &obj->recordElements);
2234 sprintf (interp->result, "wrong # args");
2239 free (obj->recordElements);
2240 return ir_tcl_strdup (NULL, &obj->recordElements,
2241 (*argv[2] ? argv[2] : NULL));
2243 Tcl_AppendResult (interp, obj->recordElements, NULL);
2250 static int ir_diagResult (Tcl_Interp *interp, IrTcl_Diagnostic *list, int num)
2256 for (i = 0; i<num; i++)
2258 logf (LOG_DEBUG, "Diagnostic, code %d", list[i].condition);
2259 sprintf (buf, "%d", list[i].condition);
2260 Tcl_AppendElement (interp, buf);
2261 cp = diagbib1_str (list[i].condition);
2263 Tcl_AppendElement (interp, (char*) cp);
2265 Tcl_AppendElement (interp, "");
2266 if (list[i].addinfo)
2267 Tcl_AppendElement (interp, (char*) list[i].addinfo);
2269 Tcl_AppendElement (interp, "");
2275 * do_diag: Return diagnostic record info
2277 static int do_diag (void *o, Tcl_Interp *interp, int argc, char **argv)
2279 IrTcl_SetObj *obj = o;
2281 IrTcl_RecordList *rl;
2287 sprintf (interp->result, "wrong # args");
2290 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2292 rl = find_IR_record (obj, offset);
2295 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2298 if (rl->which != Z_NamePlusRecord_surrogateDiagnostic)
2300 Tcl_AppendResult (interp, "No Diagnostic record at #", argv[2], NULL);
2303 return ir_diagResult (interp, rl->u.surrogateDiagnostics.list,
2304 rl->u.surrogateDiagnostics.num);
2308 * do_getMarc: Get ISO2709 Record lines/fields
2310 static int do_getMarc (void *o, Tcl_Interp *interp, int argc, char **argv)
2312 IrTcl_SetObj *obj = o;
2314 IrTcl_RecordList *rl;
2320 sprintf (interp->result, "wrong # args");
2323 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2325 rl = find_IR_record (obj, offset);
2328 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2331 if (rl->which != Z_NamePlusRecord_databaseRecord)
2333 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2336 return ir_tcl_get_marc (interp, rl->u.dbrec.buf, argc, argv);
2340 * do_getSutrs: Get SUTRS Record
2342 static int do_getSutrs (void *o, Tcl_Interp *interp, int argc, char **argv)
2344 IrTcl_SetObj *obj = o;
2346 IrTcl_RecordList *rl;
2352 sprintf (interp->result, "wrong # args");
2355 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2357 rl = find_IR_record (obj, offset);
2360 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2363 if (rl->which != Z_NamePlusRecord_databaseRecord)
2365 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2368 if (rl->u.dbrec.type != VAL_SUTRS)
2370 Tcl_AppendElement (interp, rl->u.dbrec.buf);
2376 * do_getGrs: Get a GRS1 Record
2378 static int do_getGrs (void *o, Tcl_Interp *interp, int argc, char **argv)
2380 IrTcl_SetObj *obj = o;
2382 IrTcl_RecordList *rl;
2388 sprintf (interp->result, "wrong # args");
2391 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2393 rl = find_IR_record (obj, offset);
2396 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2399 if (rl->which != Z_NamePlusRecord_databaseRecord)
2401 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2404 if (rl->u.dbrec.type != VAL_GRS1)
2406 return ir_tcl_get_grs (interp, rl->u.dbrec.u.grs1, argc, argv);
2411 * do_responseStatus: Return response status (present or search)
2413 static int do_responseStatus (void *o, Tcl_Interp *interp,
2414 int argc, char **argv)
2416 IrTcl_SetObj *obj = o;
2420 obj->recordFlag = 0;
2421 obj->nonSurrogateDiagnosticNum = 0;
2422 obj->nonSurrogateDiagnosticList = NULL;
2425 else if (argc == -1)
2427 ir_deleteDiags (&obj->nonSurrogateDiagnosticList,
2428 &obj->nonSurrogateDiagnosticNum);
2431 if (!obj->recordFlag)
2433 Tcl_AppendElement (interp, "OK");
2438 case Z_Records_DBOSD:
2439 Tcl_AppendElement (interp, "DBOSD");
2442 Tcl_AppendElement (interp, "NSD");
2443 return ir_diagResult (interp, obj->nonSurrogateDiagnosticList,
2444 obj->nonSurrogateDiagnosticNum);
2450 * do_present: Perform Present Request
2453 static int do_present (void *o, Tcl_Interp *interp, int argc, char **argv)
2455 IrTcl_SetObj *obj = o;
2458 Z_PresentRequest *req;
2466 if (Tcl_GetInt (interp, argv[2], &start) == TCL_ERROR)
2473 if (Tcl_GetInt (interp, argv[3], &number) == TCL_ERROR)
2481 interp->result = "present: not connected";
2486 obj->number = number;
2488 apdu = zget_APDU (p->odr_out, Z_APDU_presentRequest);
2489 req = apdu->u.presentRequest;
2491 set_referenceId (p->odr_out, &req->referenceId,
2492 obj->set_inher.referenceId);
2494 req->resultSetId = obj->setName ? obj->setName : "Default";
2496 req->resultSetStartPoint = &start;
2497 req->numberOfRecordsRequested = &number;
2498 if (obj->set_inher.preferredRecordSyntax)
2500 struct oident ident;
2502 ident.proto = p->protocol_type;
2503 ident.oclass = CLASS_RECSYN;
2504 ident.value = *obj->set_inher.preferredRecordSyntax;
2505 logf (LOG_DEBUG, "Preferred record syntax is %d", ident.value);
2506 req->preferredRecordSyntax = odr_oiddup (p->odr_out,
2507 oid_getoidbyent (&ident));
2510 req->preferredRecordSyntax = 0;
2512 if (obj->set_inher.elementSetNames && *obj->set_inher.elementSetNames)
2514 Z_ElementSetNames *esn = odr_malloc (p->odr_out, sizeof(*esn));
2515 Z_RecordComposition *compo = odr_malloc (p->odr_out, sizeof(*compo));
2517 esn->which = Z_ElementSetNames_generic;
2518 esn->u.generic = obj->set_inher.elementSetNames;
2520 req->recordComposition = compo;
2521 compo->which = Z_RecordComp_simple;
2522 compo->u.simple = esn;
2525 req->recordComposition = NULL;
2526 return ir_tcl_send_APDU (interp, p, apdu, "present", *argv);
2530 * do_loadFile: Load result set from file
2533 static int do_loadFile (void *o, Tcl_Interp *interp,
2534 int argc, char **argv)
2536 IrTcl_SetObj *setobj = o;
2546 interp->result = "wrong # args";
2549 inf = fopen (argv[2], "r");
2552 Tcl_AppendResult (interp, "Cannot open ", argv[2], NULL);
2555 while ((buf = ir_tcl_fread_marc (inf, &size)))
2557 IrTcl_RecordList *rl;
2559 rl = new_IR_record (setobj, no, Z_NamePlusRecord_databaseRecord, "F");
2560 rl->u.dbrec.type = VAL_USMARC;
2561 rl->u.dbrec.buf = buf;
2562 rl->u.dbrec.size = size;
2565 setobj->numberOfRecordsReturned = no-1;
2570 static IrTcl_Method ir_set_method_tab[] = {
2571 { 0, "search", do_search },
2572 { 0, "searchResponse", do_searchResponse },
2573 { 0, "presentResponse", do_presentResponse },
2574 { 0, "searchStatus", do_searchStatus },
2575 { 0, "presentStatus", do_presentStatus },
2576 { 0, "nextResultSetPosition", do_nextResultSetPosition },
2577 { 0, "setName", do_setName },
2578 { 0, "resultCount", do_resultCount },
2579 { 0, "numberOfRecordsReturned", do_numberOfRecordsReturned },
2580 { 0, "present", do_present },
2581 { 0, "type", do_type },
2582 { 0, "getMarc", do_getMarc },
2583 { 0, "getSutrs", do_getSutrs },
2584 { 0, "getGrs", do_getGrs },
2585 { 0, "recordType", do_recordType },
2586 { 0, "recordElements", do_recordElements },
2587 { 0, "diag", do_diag },
2588 { 0, "responseStatus", do_responseStatus },
2589 { 0, "loadFile", do_loadFile },
2594 * ir_set_obj_method: IR Set Object methods
2596 static int ir_set_obj_method (ClientData clientData, Tcl_Interp *interp,
2597 int argc, char **argv)
2599 IrTcl_Methods tabs[3];
2600 IrTcl_SetObj *p = clientData;
2604 interp->result = "wrong # args";
2607 tabs[0].tab = ir_set_method_tab;
2609 tabs[1].tab = ir_set_c_method_tab;
2610 tabs[1].obj = &p->set_inher;
2613 return ir_method (interp, argc, argv, tabs);
2617 * ir_set_obj_delete: IR Set Object disposal
2619 static void ir_set_obj_delete (ClientData clientData)
2621 IrTcl_Methods tabs[3];
2622 IrTcl_SetObj *p = clientData;
2624 logf (LOG_DEBUG, "ir set delete");
2626 tabs[0].tab = ir_set_method_tab;
2628 tabs[1].tab = ir_set_c_method_tab;
2629 tabs[1].obj = &p->set_inher;
2632 ir_method (NULL, -1, NULL, tabs);
2638 * ir_set_obj_mk: IR Set Object creation
2640 static int ir_set_obj_mk (ClientData clientData, Tcl_Interp *interp,
2641 int argc, char **argv)
2643 IrTcl_Methods tabs[3];
2646 if (argc < 2 || argc > 3)
2648 interp->result = "wrong # args";
2651 obj = ir_tcl_malloc (sizeof(*obj));
2652 logf (LOG_DEBUG, "ir set create");
2655 Tcl_CmdInfo parent_info;
2660 if (!Tcl_GetCommandInfo (interp, argv[2], &parent_info))
2662 interp->result = "No parent";
2665 obj->parent = (IrTcl_Obj *) parent_info.clientData;
2667 dst = &obj->set_inher;
2668 src = &obj->parent->set_inher;
2670 if ((dst->num_databaseNames = src->num_databaseNames))
2671 dst->databaseNames =
2672 ir_tcl_malloc (sizeof (*dst->databaseNames)
2673 * dst->num_databaseNames);
2675 dst->databaseNames = NULL;
2676 for (i = 0; i < dst->num_databaseNames; i++)
2677 if (ir_tcl_strdup (interp, &dst->databaseNames[i],
2678 src->databaseNames[i]) == TCL_ERROR)
2680 if (ir_tcl_strdup (interp, &dst->queryType, src->queryType)
2684 if (ir_tcl_strdup (interp, &dst->referenceId, src->referenceId)
2688 if (ir_tcl_strdup (interp, &dst->elementSetNames, src->elementSetNames)
2692 if (ir_tcl_strdup (interp, &dst->smallSetElementSetNames,
2693 src->smallSetElementSetNames)
2697 if (ir_tcl_strdup (interp, &dst->mediumSetElementSetNames,
2698 src->mediumSetElementSetNames)
2702 if (src->preferredRecordSyntax &&
2703 (dst->preferredRecordSyntax
2704 = ir_tcl_malloc (sizeof(*dst->preferredRecordSyntax))))
2705 *dst->preferredRecordSyntax = *src->preferredRecordSyntax;
2707 dst->preferredRecordSyntax = NULL;
2708 dst->replaceIndicator = src->replaceIndicator;
2709 dst->smallSetUpperBound = src->smallSetUpperBound;
2710 dst->largeSetLowerBound = src->largeSetLowerBound;
2711 dst->mediumSetPresentNumber = src->mediumSetPresentNumber;
2716 tabs[0].tab = ir_set_method_tab;
2720 if (ir_method (interp, 0, NULL, tabs) == TCL_ERROR)
2723 Tcl_CreateCommand (interp, argv[1], ir_set_obj_method,
2724 (ClientData) obj, ir_set_obj_delete);
2728 /* ------------------------------------------------------- */
2731 * do_scan: Perform scan
2733 static int do_scan (void *o, Tcl_Interp *interp, int argc, char **argv)
2737 IrTcl_ScanObj *obj = o;
2738 IrTcl_Obj *p = obj->parent;
2741 struct ccl_rpn_node *rpn;
2749 interp->result = "wrong # args";
2752 if (!p->set_inher.num_databaseNames)
2754 interp->result = "no databaseNames";
2759 interp->result = "scan: not connected";
2763 bib1.proto = p->protocol_type;
2764 bib1.oclass = CLASS_ATTSET;
2765 bib1.value = VAL_BIB1;
2767 apdu = zget_APDU (p->odr_out, Z_APDU_scanRequest);
2768 req = apdu->u.scanRequest;
2770 set_referenceId (p->odr_out, &req->referenceId, p->set_inher.referenceId);
2771 req->num_databaseNames = p->set_inher.num_databaseNames;
2772 req->databaseNames = p->set_inher.databaseNames;
2773 req->attributeSet = oid_getoidbyent (&bib1);
2776 if (!(req->termListAndStartPoint = p_query_scan (p->odr_out, argv[2])))
2778 Tcl_AppendResult (interp, "Syntax error in query", NULL);
2782 rpn = ccl_find_str(p->bibset, argv[2], &r, &pos);
2785 Tcl_AppendResult (interp, "CCL error: ", ccl_err_msg (r), NULL);
2788 ccl_pr_tree (rpn, stderr);
2789 fprintf (stderr, "\n");
2790 if (!(req->termListAndStartPoint = ccl_scan_query (rpn)))
2793 req->stepSize = &obj->stepSize;
2794 req->numberOfTermsRequested = &obj->numberOfTermsRequested;
2795 req->preferredPositionInResponse = &obj->preferredPositionInResponse;
2796 logf (LOG_DEBUG, "stepSize=%d", *req->stepSize);
2797 logf (LOG_DEBUG, "numberOfTermsRequested=%d",
2798 *req->numberOfTermsRequested);
2799 logf (LOG_DEBUG, "preferredPositionInResponse=%d",
2800 *req->preferredPositionInResponse);
2802 return ir_tcl_send_APDU (interp, p, apdu, "scan", *argv);
2806 * do_scanResponse: add scan response handler
2808 static int do_scanResponse (void *o, Tcl_Interp *interp,
2809 int argc, char **argv)
2811 IrTcl_ScanObj *obj = o;
2815 obj->scanResponse = NULL;
2818 else if (argc == -1)
2819 return ir_tcl_strdel (interp, &obj->scanResponse);
2822 free (obj->scanResponse);
2825 if (ir_tcl_strdup (interp, &obj->scanResponse, argv[2])
2830 obj->scanResponse = NULL;
2836 * do_stepSize: Set/get replace Step Size
2838 static int do_stepSize (void *obj, Tcl_Interp *interp,
2839 int argc, char **argv)
2841 IrTcl_ScanObj *p = obj;
2847 return get_set_int (&p->stepSize, interp, argc, argv);
2851 * do_numberOfTermsRequested: Set/get Number of Terms requested
2853 static int do_numberOfTermsRequested (void *obj, Tcl_Interp *interp,
2854 int argc, char **argv)
2856 IrTcl_ScanObj *p = obj;
2860 p->numberOfTermsRequested = 20;
2863 return get_set_int (&p->numberOfTermsRequested, interp, argc, argv);
2868 * do_preferredPositionInResponse: Set/get preferred Position
2870 static int do_preferredPositionInResponse (void *obj, Tcl_Interp *interp,
2871 int argc, char **argv)
2873 IrTcl_ScanObj *p = obj;
2877 p->preferredPositionInResponse = 1;
2880 return get_set_int (&p->preferredPositionInResponse, interp, argc, argv);
2884 * do_scanStatus: Get scan status
2886 static int do_scanStatus (void *obj, Tcl_Interp *interp,
2887 int argc, char **argv)
2889 IrTcl_ScanObj *p = obj;
2893 return get_set_int (&p->scanStatus, interp, argc, argv);
2897 * do_numberOfEntriesReturned: Get number of Entries returned
2899 static int do_numberOfEntriesReturned (void *obj, Tcl_Interp *interp,
2900 int argc, char **argv)
2902 IrTcl_ScanObj *p = obj;
2906 return get_set_int (&p->numberOfEntriesReturned, interp, argc, argv);
2910 * do_positionOfTerm: Get position of Term
2912 static int do_positionOfTerm (void *obj, Tcl_Interp *interp,
2913 int argc, char **argv)
2915 IrTcl_ScanObj *p = obj;
2919 return get_set_int (&p->positionOfTerm, interp, argc, argv);
2923 * do_scanLine: get Scan Line (surrogate or normal) after response
2925 static int do_scanLine (void *obj, Tcl_Interp *interp, int argc, char **argv)
2927 IrTcl_ScanObj *p = obj;
2933 p->entries_flag = 0;
2935 p->nonSurrogateDiagnosticNum = 0;
2936 p->nonSurrogateDiagnosticList = 0;
2939 else if (argc == -1)
2941 p->entries_flag = 0;
2942 /* release entries */
2945 ir_deleteDiags (&p->nonSurrogateDiagnosticList,
2946 &p->nonSurrogateDiagnosticNum);
2951 interp->result = "wrong # args";
2954 if (Tcl_GetInt (interp, argv[2], &i) == TCL_ERROR)
2956 if (!p->entries_flag || p->which != Z_ListEntries_entries || !p->entries
2957 || i >= p->num_entries || i < 0)
2959 switch (p->entries[i].which)
2961 case Z_Entry_termInfo:
2962 Tcl_AppendElement (interp, "T");
2963 if (p->entries[i].u.term.buf)
2964 Tcl_AppendElement (interp, p->entries[i].u.term.buf);
2966 Tcl_AppendElement (interp, "");
2967 sprintf (numstr, "%d", p->entries[i].u.term.globalOccurrences);
2968 Tcl_AppendElement (interp, numstr);
2970 case Z_Entry_surrogateDiagnostic:
2971 Tcl_AppendElement (interp, "SD");
2972 return ir_diagResult (interp, p->entries[i].u.diag.list,
2973 p->entries[i].u.diag.num);
2979 static IrTcl_Method ir_scan_method_tab[] = {
2980 { 0, "scan", do_scan },
2981 { 0, "scanResponse", do_scanResponse },
2982 { 0, "stepSize", do_stepSize },
2983 { 0, "numberOfTermsRequested", do_numberOfTermsRequested },
2984 { 0, "preferredPositionInResponse", do_preferredPositionInResponse },
2985 { 0, "scanStatus", do_scanStatus },
2986 { 0, "numberOfEntriesReturned", do_numberOfEntriesReturned },
2987 { 0, "positionOfTerm", do_positionOfTerm },
2988 { 0, "scanLine", do_scanLine },
2993 * ir_scan_obj_method: IR Scan Object methods
2995 static int ir_scan_obj_method (ClientData clientData, Tcl_Interp *interp,
2996 int argc, char **argv)
2998 IrTcl_Methods tabs[2];
3002 interp->result = "wrong # args";
3005 tabs[0].tab = ir_scan_method_tab;
3006 tabs[0].obj = clientData;
3009 return ir_method (interp, argc, argv, tabs);
3013 * ir_scan_obj_delete: IR Scan Object disposal
3015 static void ir_scan_obj_delete (ClientData clientData)
3017 IrTcl_Methods tabs[2];
3018 IrTcl_ScanObj *obj = clientData;
3020 tabs[0].tab = ir_scan_method_tab;
3024 ir_method (NULL, -1, NULL, tabs);
3029 * ir_scan_obj_mk: IR Scan Object creation
3031 static int ir_scan_obj_mk (ClientData clientData, Tcl_Interp *interp,
3032 int argc, char **argv)
3034 Tcl_CmdInfo parent_info;
3036 IrTcl_Methods tabs[2];
3040 interp->result = "wrong # args";
3043 if (!Tcl_GetCommandInfo (interp, argv[2], &parent_info))
3045 interp->result = "No parent";
3048 obj = ir_tcl_malloc (sizeof(*obj));
3049 obj->parent = (IrTcl_Obj *) parent_info.clientData;
3051 tabs[0].tab = ir_scan_method_tab;
3055 if (ir_method (interp, 0, NULL, tabs) == TCL_ERROR)
3057 Tcl_CreateCommand (interp, argv[1], ir_scan_obj_method,
3058 (ClientData) obj, ir_scan_obj_delete);
3062 /* ------------------------------------------------------- */
3064 static void ir_initResponse (void *obj, Z_InitResponse *initrs)
3068 p->initResult = *initrs->result ? 1 : 0;
3069 if (!*initrs->result)
3070 logf (LOG_DEBUG, "Connection rejected by target");
3072 logf (LOG_DEBUG, "Connection accepted by target");
3074 get_referenceId (&p->set_inher.referenceId, initrs->referenceId);
3076 free (p->targetImplementationId);
3077 ir_tcl_strdup (p->interp, &p->targetImplementationId,
3078 initrs->implementationId);
3079 free (p->targetImplementationName);
3080 ir_tcl_strdup (p->interp, &p->targetImplementationName,
3081 initrs->implementationName);
3082 free (p->targetImplementationVersion);
3083 ir_tcl_strdup (p->interp, &p->targetImplementationVersion,
3084 initrs->implementationVersion);
3086 p->maximumRecordSize = *initrs->maximumRecordSize;
3087 p->preferredMessageSize = *initrs->preferredMessageSize;
3089 memcpy (&p->options, initrs->options, sizeof(initrs->options));
3090 memcpy (&p->protocolVersion, initrs->protocolVersion,
3091 sizeof(initrs->protocolVersion));
3092 free (p->userInformationField);
3093 p->userInformationField = NULL;
3094 if (initrs->userInformationField)
3098 if (initrs->userInformationField->which == ODR_EXTERNAL_octet &&
3099 (p->userInformationField =
3100 ir_tcl_malloc ((len =
3101 initrs->userInformationField->
3102 u.octet_aligned->len) +1)))
3104 memcpy (p->userInformationField,
3105 initrs->userInformationField->u.octet_aligned->buf,
3107 (p->userInformationField)[len] = '\0';
3112 static void ir_deleteDiags (IrTcl_Diagnostic **dst_list, int *dst_num)
3115 for (i = 0; i<*dst_num; i++)
3116 free (dst_list[i]->addinfo);
3122 static void ir_handleDiags (IrTcl_Diagnostic **dst_list, int *dst_num,
3123 Z_DiagRec **list, int num)
3129 *dst_list = ir_tcl_malloc (sizeof(**dst_list) * num);
3130 for (i = 0; i<num; i++)
3132 switch (list[i]->which)
3134 case Z_DiagRec_defaultFormat:
3135 (*dst_list)[i].condition = *list[i]->u.defaultFormat->condition;
3136 addinfo = list[i]->u.defaultFormat->addinfo;
3138 ((*dst_list)[i].addinfo = ir_tcl_malloc (strlen(addinfo)+1)))
3139 strcpy ((*dst_list)[i].addinfo, addinfo);
3142 (*dst_list)[i].addinfo = NULL;
3143 (*dst_list)[i].condition = 0;
3148 static void ir_handleRecords (void *o, Z_Records *zrs, IrTcl_SetObj *setobj,
3149 const char *elements)
3154 IrTcl_RecordList *rl;
3156 setobj->which = zrs->which;
3157 setobj->recordFlag = 1;
3159 ir_deleteDiags (&setobj->nonSurrogateDiagnosticList,
3160 &setobj->nonSurrogateDiagnosticNum);
3161 if (zrs->which == Z_Records_DBOSD)
3163 setobj->numberOfRecordsReturned =
3164 zrs->u.databaseOrSurDiagnostics->num_records;
3165 logf (LOG_DEBUG, "Got %d records", setobj->numberOfRecordsReturned);
3166 for (offset = 0; offset<setobj->numberOfRecordsReturned; offset++)
3168 rl = new_IR_record (setobj, setobj->start + offset,
3169 zrs->u.databaseOrSurDiagnostics->
3170 records[offset]->which,
3172 if (rl->which == Z_NamePlusRecord_surrogateDiagnostic)
3174 ir_handleDiags (&rl->u.surrogateDiagnostics.list,
3175 &rl->u.surrogateDiagnostics.num,
3176 &zrs->u.databaseOrSurDiagnostics->
3177 records[offset]->u.surrogateDiagnostic,
3182 Z_DatabaseRecord *zr;
3184 struct oident *ident;
3186 zr = zrs->u.databaseOrSurDiagnostics->records[offset]
3188 oe = (Z_External*) zr;
3189 rl->u.dbrec.size = zr->u.octet_aligned->len;
3191 if ((ident = oid_getentbyoid (oe->direct_reference)))
3192 rl->u.dbrec.type = ident->value;
3194 rl->u.dbrec.type = VAL_USMARC;
3196 if (oe->which == ODR_EXTERNAL_octet && rl->u.dbrec.size > 0)
3198 char *buf = (char*) zr->u.octet_aligned->buf;
3199 if ((rl->u.dbrec.buf = ir_tcl_malloc (rl->u.dbrec.size)))
3200 memcpy (rl->u.dbrec.buf, buf, rl->u.dbrec.size);
3202 else if (rl->u.dbrec.type == VAL_SUTRS &&
3203 oe->which == Z_External_sutrs)
3205 odr_setbuf (p->odr_in, (char*) oe->u.single_ASN1_type->buf,
3206 oe->u.single_ASN1_type->len, 0);
3207 if ((rl->u.dbrec.buf = ir_tcl_malloc (oe->u.sutrs->len+1)))
3209 memcpy (rl->u.dbrec.buf, oe->u.sutrs->buf,
3211 rl->u.dbrec.buf[oe->u.sutrs->len] = '\0';
3213 rl->u.dbrec.size = oe->u.sutrs->len;
3215 else if (rl->u.dbrec.type == VAL_GRS1 &&
3216 oe->which == Z_External_grs1)
3218 ir_tcl_read_grs (oe->u.grs1, &rl->u.dbrec.u.grs1);
3219 rl->u.dbrec.buf = NULL;
3222 rl->u.dbrec.buf = NULL;
3226 else if (zrs->which == Z_Records_multipleNSD)
3228 logf (LOG_DEBUG, "multipleNonSurrogateDiagnostic %d",
3229 zrs->u.multipleNonSurDiagnostics->num_diagRecs);
3230 setobj->numberOfRecordsReturned = 0;
3231 ir_handleDiags (&setobj->nonSurrogateDiagnosticList,
3232 &setobj->nonSurrogateDiagnosticNum,
3233 zrs->u.multipleNonSurDiagnostics->diagRecs,
3234 zrs->u.multipleNonSurDiagnostics->num_diagRecs);
3238 logf (LOG_DEBUG, "NonSurrogateDiagnostic");
3239 setobj->numberOfRecordsReturned = 0;
3240 ir_handleDiags (&setobj->nonSurrogateDiagnosticList,
3241 &setobj->nonSurrogateDiagnosticNum,
3242 &zrs->u.nonSurrogateDiagnostic,
3247 static void ir_searchResponse (void *o, Z_SearchResponse *searchrs,
3248 IrTcl_SetObj *setobj)
3250 Z_Records *zrs = searchrs->records;
3252 logf (LOG_DEBUG, "Received search response");
3255 logf (LOG_DEBUG, "Search response, no object!");
3258 setobj->searchStatus = searchrs->searchStatus ? 1 : 0;
3259 get_referenceId (&setobj->set_inher.referenceId, searchrs->referenceId);
3260 setobj->resultCount = *searchrs->resultCount;
3261 if (searchrs->presentStatus)
3262 setobj->presentStatus = *searchrs->presentStatus;
3263 if (searchrs->nextResultSetPosition)
3264 setobj->nextResultSetPosition = *searchrs->nextResultSetPosition;
3266 logf (LOG_DEBUG, "Search response %d, %d hits",
3267 setobj->searchStatus, setobj->resultCount);
3271 if (setobj->resultCount <= setobj->set_inher.smallSetUpperBound)
3272 es = setobj->set_inher.smallSetElementSetNames;
3274 es = setobj->set_inher.mediumSetElementSetNames;
3275 ir_handleRecords (o, zrs, setobj, es);
3278 setobj->recordFlag = 0;
3282 static void ir_presentResponse (void *o, Z_PresentResponse *presrs,
3283 IrTcl_SetObj *setobj)
3285 Z_Records *zrs = presrs->records;
3287 logf (LOG_DEBUG, "Received present response");
3290 logf (LOG_DEBUG, "Present response, no object!");
3293 setobj->presentStatus = *presrs->presentStatus;
3294 get_referenceId (&setobj->set_inher.referenceId, presrs->referenceId);
3295 setobj->nextResultSetPosition = *presrs->nextResultSetPosition;
3297 ir_handleRecords (o, zrs, setobj, setobj->set_inher.elementSetNames);
3300 setobj->recordFlag = 0;
3301 logf (LOG_DEBUG, "No records!");
3305 static void ir_scanResponse (void *o, Z_ScanResponse *scanrs,
3306 IrTcl_ScanObj *scanobj)
3310 logf (LOG_DEBUG, "Received scanResponse");
3312 get_referenceId (&p->set_inher.referenceId, scanrs->referenceId);
3313 scanobj->scanStatus = *scanrs->scanStatus;
3314 logf (LOG_DEBUG, "scanStatus=%d", scanobj->scanStatus);
3316 if (scanrs->stepSize)
3317 scanobj->stepSize = *scanrs->stepSize;
3318 logf (LOG_DEBUG, "stepSize=%d", scanobj->stepSize);
3320 scanobj->numberOfEntriesReturned = *scanrs->numberOfEntriesReturned;
3321 logf (LOG_DEBUG, "numberOfEntriesReturned=%d",
3322 scanobj->numberOfEntriesReturned);
3324 if (scanrs->positionOfTerm)
3325 scanobj->positionOfTerm = *scanrs->positionOfTerm;
3327 scanobj->positionOfTerm = -1;
3328 logf (LOG_DEBUG, "positionOfTerm=%d", scanobj->positionOfTerm);
3330 free (scanobj->entries);
3331 scanobj->entries = NULL;
3333 ir_deleteDiags (&scanobj->nonSurrogateDiagnosticList,
3334 &scanobj->nonSurrogateDiagnosticNum);
3335 if (scanrs->entries)
3340 scanobj->entries_flag = 1;
3341 scanobj->which = scanrs->entries->which;
3342 switch (scanobj->which)
3344 case Z_ListEntries_entries:
3345 scanobj->num_entries = scanrs->entries->u.entries->num_entries;
3346 scanobj->entries = ir_tcl_malloc (scanobj->num_entries *
3347 sizeof(*scanobj->entries));
3348 for (i=0; i<scanobj->num_entries; i++)
3350 ze = scanrs->entries->u.entries->entries[i];
3351 scanobj->entries[i].which = ze->which;
3354 case Z_Entry_termInfo:
3355 if (ze->u.termInfo->term->which == Z_Term_general)
3357 int l = ze->u.termInfo->term->u.general->len;
3358 scanobj->entries[i].u.term.buf = ir_tcl_malloc (1+l);
3359 memcpy (scanobj->entries[i].u.term.buf,
3360 ze->u.termInfo->term->u.general->buf,
3362 scanobj->entries[i].u.term.buf[l] = '\0';
3365 scanobj->entries[i].u.term.buf = NULL;
3366 if (ze->u.termInfo->globalOccurrences)
3367 scanobj->entries[i].u.term.globalOccurrences =
3368 *ze->u.termInfo->globalOccurrences;
3370 scanobj->entries[i].u.term.globalOccurrences = 0;
3372 case Z_Entry_surrogateDiagnostic:
3373 ir_handleDiags (&scanobj->entries[i].u.diag.list,
3374 &scanobj->entries[i].u.diag.num,
3375 &ze->u.surrogateDiagnostic,
3381 case Z_ListEntries_nonSurrogateDiagnostics:
3382 ir_handleDiags (&scanobj->nonSurrogateDiagnosticList,
3383 &scanobj->nonSurrogateDiagnosticNum,
3384 scanrs->entries->u.nonSurrogateDiagnostics->
3386 scanrs->entries->u.nonSurrogateDiagnostics->
3392 scanobj->entries_flag = 0;
3396 * ir_select_read: handle incoming packages
3398 static void ir_select_read (ClientData clientData)
3400 IrTcl_Obj *p = clientData;
3405 Tcl_CmdInfo cmd_info;
3406 const char *apdu_call;
3408 logf(LOG_DEBUG, "Read handler fd=%d", cs_fileno(p->cs_link));
3409 if (p->state == IR_TCL_R_Connecting)
3411 logf(LOG_DEBUG, "Connect handler");
3412 r = cs_rcvconnect (p->cs_link);
3415 logf (LOG_WARN, "cs_rcvconnect returned 1");
3418 p->state = IR_TCL_R_Idle;
3419 #if IRTCL_GENERIC_FILES
3420 ir_select_remove_write (p->csFile, p);
3422 ir_select_remove_write (cs_fileno (p->cs_link), p);
3426 logf (LOG_DEBUG, "cs_rcvconnect error");
3429 p->failInfo = IR_TCL_FAIL_CONNECT;
3430 IrTcl_eval (p->interp, p->failback);
3432 do_disconnect (p, NULL, 2, NULL);
3435 p->state = IR_TCL_R_Idle;
3437 IrTcl_eval (p->interp, p->callback);
3438 if (p->cs_link && p->request_queue && p->state == IR_TCL_R_Idle)
3439 ir_tcl_send_q (p, p->request_queue, "x");
3444 /* signal one more use of ir object - callbacks must not
3445 release the ir memory (p pointer) */
3446 p->state = IR_TCL_R_Reading;
3449 /* read incoming APDU */
3450 if ((r=cs_get (p->cs_link, &p->buf_in, &p->len_in)) <= 0)
3452 logf (LOG_DEBUG, "cs_get failed, code %d", r);
3453 #if IRTCL_GENERIC_FILES
3454 ir_select_remove (p->csFile, p);
3456 ir_select_remove (cs_fileno (p->cs_link), p);
3458 do_disconnect (p, NULL, 2, NULL);
3461 p->failInfo = IR_TCL_FAIL_READ;
3462 IrTcl_eval (p->interp, p->failback);
3464 /* release ir object now if callback deleted it */
3470 logf(LOG_DEBUG, "PDU Fraction read");
3473 /* got complete APDU. Now decode */
3476 odr_setbuf (p->odr_in, p->buf_in, r, 0);
3477 logf (LOG_DEBUG, "cs_get ok, total size %d", r);
3478 if (!z_APDU (p->odr_in, &apdu, 0))
3480 logf (LOG_DEBUG, "cs_get failed: %s",
3481 odr_errmsg (odr_geterror (p->odr_in)));
3482 do_disconnect (p, NULL, 2, NULL);
3485 p->failInfo = IR_TCL_FAIL_IN_APDU;
3486 p->apduOffset = odr_offset (p->odr_in);
3487 IrTcl_eval (p->interp, p->failback);
3489 /* release ir object now if failback deleted it */
3493 logf(LOG_DEBUG, "Decoded ok");
3494 /* handle APDU and invoke callback */
3495 rq = p->request_queue;
3498 logf (LOG_FATAL, "Internal error. No queue entry");
3501 object_name = rq->object_name;
3502 logf (LOG_DEBUG, "getCommandInfo (%s)", object_name);
3504 if (Tcl_GetCommandInfo (p->interp, object_name, &cmd_info))
3508 case Z_APDU_initResponse:
3509 p->eventType = "init";
3510 ir_initResponse (p, apdu->u.initResponse);
3511 apdu_call = p->initResponse;
3513 case Z_APDU_searchResponse:
3514 p->eventType = "search";
3515 ir_searchResponse (p, apdu->u.searchResponse,
3516 (IrTcl_SetObj *) cmd_info.clientData);
3517 apdu_call = ((IrTcl_SetObj *)
3518 cmd_info.clientData)->searchResponse;
3520 case Z_APDU_presentResponse:
3521 p->eventType = "present";
3522 ir_presentResponse (p, apdu->u.presentResponse,
3523 (IrTcl_SetObj *) cmd_info.clientData);
3524 apdu_call = ((IrTcl_SetObj *)
3525 cmd_info.clientData)->presentResponse;
3527 case Z_APDU_scanResponse:
3528 p->eventType = "scan";
3529 ir_scanResponse (p, apdu->u.scanResponse,
3530 (IrTcl_ScanObj *) cmd_info.clientData);
3531 apdu_call = ((IrTcl_ScanObj *)
3532 cmd_info.clientData)->scanResponse;
3535 logf (LOG_WARN, "Received unknown APDU type (%d)",
3537 do_disconnect (p, NULL, 2, NULL);
3540 p->failInfo = IR_TCL_FAIL_UNKNOWN_APDU;
3541 IrTcl_eval (p->interp, p->failback);
3546 p->request_queue = rq->next;
3547 p->state = IR_TCL_R_Idle;
3550 IrTcl_eval (p->interp, apdu_call);
3551 else if (rq->callback)
3552 IrTcl_eval (p->interp, rq->callback);
3554 free (rq->callback);
3555 free (rq->object_name);
3557 odr_reset (p->odr_in);
3558 if (p->ref_count == 1)
3564 } while (p->cs_link && cs_more (p->cs_link));
3565 if (p->cs_link && p->request_queue && p->state == IR_TCL_R_Idle)
3566 ir_tcl_send_q (p, p->request_queue, "x");
3570 * ir_select_write: handle outgoing packages - not yet written.
3572 static void ir_select_write (ClientData clientData)
3574 IrTcl_Obj *p = clientData;
3578 logf (LOG_DEBUG, "Write handler fd=%d", cs_fileno(p->cs_link));
3579 if (p->state == IR_TCL_R_Connecting)
3581 logf(LOG_DEBUG, "Connect handler");
3582 r = cs_rcvconnect (p->cs_link);
3585 p->state = IR_TCL_R_Idle;
3588 logf (LOG_DEBUG, "cs_rcvconnect error");
3589 #if IRTCL_GENERIC_FILES
3590 ir_select_remove_write (p->csFile, p);
3592 ir_select_remove_write (cs_fileno (p->cs_link), p);
3596 p->failInfo = IR_TCL_FAIL_CONNECT;
3597 IrTcl_eval (p->interp, p->failback);
3599 do_disconnect (p, NULL, 2, NULL);
3602 #if IRTCL_GENERIC_FILES
3603 ir_select_remove_write (p->csFile, p);
3605 ir_select_remove_write (cs_fileno (p->cs_link), p);
3608 IrTcl_eval (p->interp, p->callback);
3611 rq = p->request_queue;
3612 if (!rq || !rq->buf_out)
3615 if ((r=cs_put (p->cs_link, rq->buf_out, rq->len_out)) < 0)
3617 logf (LOG_DEBUG, "cs_put write fail");
3620 p->failInfo = IR_TCL_FAIL_WRITE;
3621 IrTcl_eval (p->interp, p->failback);
3625 do_disconnect (p, NULL, 2, NULL);
3627 else if (r == 0) /* remove select bit */
3629 logf(LOG_DEBUG, "Write completed");
3630 p->state = IR_TCL_R_Waiting;
3631 #if IRTCL_GENERIC_FILES
3632 ir_select_remove_write (p->csFile, p);
3634 ir_select_remove_write (cs_fileno (p->cs_link), p);
3641 static void ir_select_notify (ClientData clientData, int r, int w, int e)
3644 ir_select_read (clientData);
3646 ir_select_write (clientData);
3649 /* ------------------------------------------------------- */
3652 * Irtcl_init: Registration of TCL commands.
3654 int Irtcl_Init (Tcl_Interp *interp)
3656 Tcl_CreateCommand (interp, "ir", ir_obj_mk, (ClientData) NULL,
3657 (Tcl_CmdDeleteProc *) NULL);
3658 Tcl_CreateCommand (interp, "ir-set", ir_set_obj_mk,
3659 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
3660 Tcl_CreateCommand (interp, "ir-scan", ir_scan_obj_mk,
3661 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);