2 * IR toolkit for tcl/tk
3 * (c) Index Data 1995-1996
4 * See the file LICENSE for details.
5 * Sebastian Hammer, Adam Dickmeiss
8 * Revision 1.100 1997-05-01 15:04:05 adam
9 * Added ir-log command.
11 * Revision 1.99 1997/04/30 07:24:47 adam
12 * Spell fix of an error message.
14 * Revision 1.98 1997/04/13 18:57:20 adam
15 * Better error reporting and aligned with Tcl/Tk style.
16 * Rework of notifier code with Tcl_File handles.
18 * Revision 1.97 1996/11/14 17:11:07 adam
19 * Added Explain documentaion.
21 * Revision 1.96 1996/10/08 13:02:50 adam
22 * When dealing with records, odr_choice_enable_bias function is used to
23 * prevent decoding of externals.
25 * Revision 1.95 1996/09/13 10:51:49 adam
26 * Bug fix: ir_tcl_select_set called Tcl_GetFile at disconnect.
28 * Revision 1.94 1996/08/21 13:32:53 adam
29 * Implemented saveFile method and extended loadFile method to work with it.
31 * Revision 1.93 1996/08/16 15:07:45 adam
32 * First work on Explain.
34 * Revision 1.92 1996/08/09 15:33:07 adam
35 * Modified the code to use tk4.1/tcl7.5 patch level 1. The time-driven
36 * polling is no longer activated on Windows since asynchrounous I/O works
39 * Revision 1.91 1996/07/03 13:31:11 adam
40 * The xmalloc/xfree functions from YAZ are used to manage memory.
42 * Revision 1.90 1996/06/27 14:21:00 adam
43 * Yet another Windows port.
45 * Revision 1.89 1996/06/11 15:27:15 adam
46 * Event type set to connect a little earlier in the do_connect function.
48 * Revision 1.88 1996/06/03 09:04:22 adam
49 * Changed a few logf calls.
51 * Revision 1.87 1996/05/29 06:37:51 adam
52 * Function ir_tcl_get_grs_r enhanced so that specific elements can be
55 * Revision 1.86 1996/03/20 13:54:04 adam
56 * The Tcl_File structure is only manipulated in the Tk-event interface
59 * Revision 1.85 1996/03/15 11:15:48 adam
60 * Modified to use new prototypes for p_query_rpn and p_query_scan.
62 * Revision 1.84 1996/03/07 12:42:49 adam
63 * Better logging when callback is invoked.
65 * Revision 1.83 1996/03/05 09:21:09 adam
66 * Bug fix: memory used by GRS records wasn't freed.
67 * Rewrote some of the error handling code - the connection is always
68 * closed before failback is called.
69 * If failback is defined the send APDU methods (init, search, ...) will
70 * return OK but invoke failback (as is the case if the write operation
72 * Bug fix: ref_count in assoc object could grow if fraction of PDU was
75 * Revision 1.82 1996/02/29 15:30:21 adam
76 * Export of IrTcl functionality to extensions.
78 * Revision 1.81 1996/02/26 18:38:32 adam
79 * Work on export of set methods.
81 * Revision 1.80 1996/02/23 17:31:39 adam
82 * More functions made available to the wais tcl extension.
84 * Revision 1.79 1996/02/23 13:41:38 adam
85 * Work on public access to simple ir class system.
87 * Revision 1.78 1996/02/21 10:16:08 adam
88 * Simplified select handling. Only one function ir_tcl_select_set has
89 * to be externally defined.
91 * Revision 1.77 1996/02/20 17:52:58 adam
92 * Uses the YAZ oid system to name record syntax object identifiers.
94 * Revision 1.76 1996/02/20 16:09:51 adam
95 * Bug fix: didn't set element set names stamp correctly on result
96 * set records when element set names were set to the empty string.
98 * Revision 1.75 1996/02/19 15:41:53 adam
99 * Better log messages.
100 * Minor improvement of connect method.
102 * Revision 1.74 1996/02/05 17:58:03 adam
103 * Ported ir-tcl to use the beta releases of tcl7.5/tk4.1.
105 * Revision 1.73 1996/01/29 11:35:19 adam
106 * Bug fix: cs_type member renamed to comstackType to avoid conflict with
107 * cs_type macro defined by YAZ.
109 * Revision 1.72 1996/01/19 17:45:34 quinn
110 * Added debugging output
112 * Revision 1.71 1996/01/19 16:22:38 adam
113 * New method: apduDump - returns information about last incoming APDU.
115 * Revision 1.70 1996/01/10 09:18:34 adam
116 * PDU specific callbacks implemented: initRespnse, searchResponse,
117 * presentResponse and scanResponse.
118 * Bug fix in the command line shell (tclmain.c) - discovered on OSF/1.
120 * Revision 1.69 1996/01/04 16:12:12 adam
121 * Setting PDUType renamed to eventType.
123 * Revision 1.68 1996/01/04 11:05:22 adam
124 * New setting: PDUType - returns type of last PDU returned from the target.
125 * Fixed a bug in configure/Makefile.
127 * Revision 1.67 1996/01/03 09:00:51 adam
128 * Updated to use new version of Yaz (names changed to avoid C++ conflict).
130 * Revision 1.66 1995/11/28 17:26:39 adam
131 * Removed Carriage return from ir-tcl.c!
132 * Removed misc. debug logs.
134 * Revision 1.65 1995/11/28 13:53:00 quinn
137 * Revision 1.64 1995/11/13 15:39:18 adam
138 * Bug fix: {small,medium}SetElementSetNames weren't set correctly.
139 * Bug fix: idAuthentication weren't set correctly.
141 * Revision 1.63 1995/11/13 09:55:39 adam
142 * Multiple records at a position in a result-set with differnt
145 * Revision 1.62 1995/10/18 17:20:33 adam
146 * Work on target setup in client.tcl.
148 * Revision 1.61 1995/10/18 16:42:42 adam
149 * New settings: smallSetElementSetNames and mediumSetElementSetNames.
151 * Revision 1.60 1995/10/18 15:43:31 adam
152 * In search: mediumSetElementSetNames and smallSetElementSetNames are
153 * set to elementSetNames.
155 * Revision 1.59 1995/10/17 12:18:58 adam
156 * Bug fix: when target connection closed, the connection was not
157 * properly reestablished.
159 * Revision 1.58 1995/10/16 17:00:55 adam
160 * New setting: elementSetNames.
161 * Various client improvements. Medium presentation format looks better.
163 * Revision 1.57 1995/09/21 13:11:51 adam
164 * Support of dynamic loading.
165 * Test script uses load command if necessary.
167 * Revision 1.56 1995/08/29 15:30:14 adam
168 * Work on GRS records.
170 * Revision 1.55 1995/08/28 09:43:25 adam
171 * Minor changes. configure only searches for yaz beta 3 and versions after
174 * Revision 1.54 1995/08/24 12:25:16 adam
175 * Modified to work with yaz 1.0b3.
177 * Revision 1.53 1995/08/04 12:49:26 adam
178 * Bug fix: reading uninitialized variable p.
180 * Revision 1.52 1995/08/04 11:32:38 adam
181 * More work on output queue. Memory related routines moved
184 * Revision 1.51 1995/08/03 13:22:54 adam
187 * Revision 1.50 1995/07/20 08:09:49 adam
188 * client.tcl: Targets removed from hotTargets list when targets
189 * are removed/modified.
190 * ir-tcl.c: More work on triggerResourceControl.
192 * Revision 1.49 1995/06/30 12:39:21 adam
193 * Bug fix: loadFile didn't set record type.
194 * The MARC routines are a little less strict in the interpretation.
195 * Script display.tcl replaces the old marc.tcl.
196 * New interactive script: shell.tcl.
198 * Revision 1.48 1995/06/27 19:03:50 adam
199 * Bug fix in do_present in ir-tcl.c: p->set_child member weren't set.
200 * nextResultSetPosition used instead of setOffset.
202 * Revision 1.47 1995/06/25 10:25:04 adam
203 * Working on triggerResourceControl. Description of compile/install
204 * procedure moved to ir-tcl.sgml.
206 * Revision 1.46 1995/06/22 13:15:06 adam
207 * Feature: SUTRS. Setting getSutrs implemented.
208 * Work on display formats.
209 * Preferred record syntax can be set by the user.
211 * Revision 1.45 1995/06/20 08:07:30 adam
212 * New setting: failInfo.
213 * Working on better cancel mechanism.
215 * Revision 1.44 1995/06/19 17:01:20 adam
218 * Revision 1.43 1995/06/19 13:06:08 adam
219 * New define: IR_TCL_VERSION.
221 * Revision 1.42 1995/06/19 08:08:52 adam
222 * client.tcl: hotTargets now contain both database and target name.
223 * ir-tcl.c: setting protocol edited. Errors in callbacks are logged
224 * by logf(LOG_WARN, ...) calls.
226 * Revision 1.41 1995/06/16 12:28:16 adam
227 * Implemented preferredRecordSyntax.
228 * Minor changes in diagnostic handling.
229 * Record list deleted when connection closes.
231 * Revision 1.40 1995/06/14 13:37:18 adam
232 * Setting recordType implemented.
233 * Setting implementationVersion implemented.
234 * Settings implementationId / implementationName edited.
236 * Revision 1.39 1995/06/08 10:26:32 adam
237 * Bug fix in ir_strdup.
239 * Revision 1.38 1995/06/01 16:36:47 adam
240 * About buttons. Minor bug fixes.
242 * Revision 1.37 1995/06/01 07:31:20 adam
243 * Rename of many typedefs -> IrTcl_...
245 * Revision 1.36 1995/05/31 13:09:59 adam
246 * Client searches/presents may be interrupted.
247 * New moving book-logo.
249 * Revision 1.35 1995/05/31 08:36:33 adam
250 * Bug fix in client.tcl: didn't save options on clientrc.tcl.
251 * New method: referenceId. More work on scan.
253 * Revision 1.34 1995/05/29 10:33:42 adam
254 * README and rename of startup script.
256 * Revision 1.33 1995/05/29 09:15:11 quinn
257 * Changed CS_SR to PROTO_SR, etc.
259 * Revision 1.32 1995/05/29 08:44:16 adam
260 * Work on delete of objects.
262 * Revision 1.31 1995/05/26 11:44:10 adam
263 * Bugs fixed. More work on MARC utilities and queries. Test
264 * client is up-to-date again.
266 * Revision 1.30 1995/05/26 08:54:11 adam
267 * New MARC utilities. Uses prefix query.
269 * Revision 1.29 1995/05/24 14:10:22 adam
270 * Work on idAuthentication, protocolVersion and options.
272 * Revision 1.28 1995/05/23 15:34:48 adam
273 * Many new settings, userInformationField, smallSetUpperBound, etc.
274 * A number of settings are inherited when ir-set is executed.
275 * This version is incompatible with the graphical test client (client.tcl).
277 * Revision 1.27 1995/05/11 15:34:47 adam
278 * Scan request changed a bit. This version works with RLG.
280 * Revision 1.26 1995/04/18 16:11:51 adam
281 * First version of graphical Scan. Some work on query-by-form.
283 * Revision 1.25 1995/04/17 09:37:17 adam
284 * Further development of scan.
286 * Revision 1.24 1995/04/11 14:16:42 adam
287 * Further work on scan. Response works. Entries aren't saved yet.
289 * Revision 1.23 1995/04/10 10:50:27 adam
290 * Result-set name defaults to suffix of ir-set name.
291 * Started working on scan. Not finished at this point.
293 * Revision 1.22 1995/03/31 10:43:03 adam
294 * More robust when getting bad MARC records.
296 * Revision 1.21 1995/03/31 08:56:37 adam
297 * New button "Search".
299 * Revision 1.20 1995/03/29 16:07:09 adam
300 * Bug fix: Didn't use setName in present request.
302 * Revision 1.19 1995/03/28 12:45:23 adam
303 * New ir method failback: called on disconnect/protocol error.
304 * New ir set/get method: protocol: SR / Z3950.
305 * Simple popup and disconnect when failback is invoked.
307 * Revision 1.18 1995/03/21 15:50:12 adam
310 * Revision 1.17 1995/03/21 13:41:03 adam
311 * Comstack cs_create not used too often. Non-blocking connect.
313 * Revision 1.16 1995/03/21 08:26:06 adam
314 * New method, setName, to specify the result set name (other than Default).
315 * New method, responseStatus, which returns diagnostic info, if any, after
316 * present response / search response.
318 * Revision 1.15 1995/03/20 15:24:07 adam
319 * Diagnostic records saved on searchResponse.
321 * Revision 1.14 1995/03/20 08:53:22 adam
322 * Event loop in tclmain.c rewritten. New method searchStatus.
324 * Revision 1.13 1995/03/17 18:26:17 adam
325 * Non-blocking i/o used now. Database names popup as cascade items.
327 * Revision 1.12 1995/03/17 15:45:00 adam
328 * Improved target/database setup.
330 * Revision 1.11 1995/03/16 17:54:03 adam
331 * Minor changes really.
333 * Revision 1.10 1995/03/15 16:14:50 adam
334 * Blocking arg in cs_create changed.
336 * Revision 1.9 1995/03/15 13:59:24 adam
339 * Revision 1.8 1995/03/15 08:25:16 adam
340 * New method presentStatus to check for error on present. Misc. cleanup
341 * of IrTcl_RecordList manipulations. Full MARC record presentation in
344 * Revision 1.7 1995/03/14 17:32:29 adam
345 * Presentation of full Marc record in popup window.
347 * Revision 1.6 1995/03/12 19:31:55 adam
348 * Pattern matching implemented when retrieving MARC records. More
349 * diagnostic functions.
351 * Revision 1.5 1995/03/10 18:00:15 adam
352 * Actual presentation in line-by-line format. RPN query support.
354 * Revision 1.4 1995/03/09 16:15:08 adam
355 * First presentRequest attempts. Hot-target list.
373 #if defined(__WIN32__)
374 # define WIN32_LEAN_AND_MEAN
375 # include <windows.h>
376 # undef WIN32_LEAN_AND_MEAN
379 * VC++ has an alternate entry point called DllMain, so we need to rename
383 # if defined(_MSC_VER)
384 # define EXPORT(a,b) __declspec(dllexport) a b
385 # define DllEntryPoint DllMain
387 # if defined(__BORLANDC__)
388 # define EXPORT(a,b) a _export b
390 # define EXPORT(a,b) a b
394 # define EXPORT(a,b) a b
397 static char *wrongArgs = "wrong # args: should be \"";
399 static int ir_tcl_error_exec (Tcl_Interp *interp, int argc, char **argv)
402 Tcl_AppendResult (interp, " while executing ", NULL);
403 for (i = 0; i<argc; i++)
404 Tcl_AppendResult (interp, (i ? " " : "\""), argv[i], NULL);
405 Tcl_AppendResult (interp, "\"", NULL);
410 static void ir_deleteDiags (IrTcl_Diagnostic **dst_list, int *dst_num);
412 static void ir_select_notify (ClientData clientData, int r, int w, int e);
414 void ir_select_add (int fd, void *obj)
416 ir_tcl_select_set (ir_select_notify, fd, obj, 1, 0, 0);
419 void ir_select_add_write (int fd, void *obj)
421 ir_tcl_select_set (ir_select_notify, fd, obj, 1, 1, 0);
424 void ir_select_remove (int fd, void *obj)
426 ir_tcl_select_set (NULL, fd, obj, 0, 0, 0);
429 void ir_select_remove_write (int fd, void *obj)
431 ir_tcl_select_set (ir_select_notify, fd, obj, 1, 0, 0);
434 static void delete_IR_record (IrTcl_RecordList *rl)
438 case Z_NamePlusRecord_databaseRecord:
439 switch (rl->u.dbrec.type)
442 ir_tcl_grs_del (&rl->u.dbrec.u.grs1);
447 xfree (rl->u.dbrec.buf);
448 rl->u.dbrec.buf = NULL;
450 case Z_NamePlusRecord_surrogateDiagnostic:
451 ir_deleteDiags (&rl->u.surrogateDiagnostics.list,
452 &rl->u.surrogateDiagnostics.num);
455 xfree (rl->elements);
458 static IrTcl_RecordList *new_IR_record (IrTcl_SetObj *setobj,
460 const char *elements)
462 IrTcl_RecordList *rl;
464 if (elements && !*elements)
466 for (rl = setobj->record_list; rl; rl = rl->next)
468 if (no == rl->no && (!rl->elements || !elements ||
469 !strcmp(elements, rl->elements)))
471 delete_IR_record (rl);
477 rl = ir_tcl_malloc (sizeof(*rl));
478 rl->next = setobj->record_list;
480 setobj->record_list = rl;
483 ir_tcl_strdup (NULL, &rl->elements, elements);
490 int ir_tcl_eval (Tcl_Interp *interp, const char *command)
492 char *tmp = ir_tcl_malloc (strlen(command)+1);
495 logf (LOG_DEBUG, "Invoking %.23s ...", command);
496 strcpy (tmp, command);
497 r = Tcl_Eval (interp, tmp);
500 logf (LOG_WARN, "Tcl error in line %d: %s", interp->errorLine,
503 Tcl_FreeResult (interp);
509 * IrTcl_getRecordSyntaxStr: Return record syntax name of object id
511 static char *IrTcl_getRecordSyntaxStr (enum oid_value value)
514 struct oident ent, *entp;
516 ent.proto = PROTO_Z3950;
517 ent.oclass = CLASS_RECSYN;
520 o = oid_getoidbyent (&ent);
521 entp = oid_getentbyoid (o);
529 * IrTcl_getRecordSyntaxVal: Return record syntax value of string
531 static enum oid_value IrTcl_getRecordSyntaxVal (const char *name)
533 return oid_getvalbyname (name);
536 static IrTcl_RecordList *find_IR_record (IrTcl_SetObj *setobj, int no)
538 IrTcl_RecordList *rl;
540 for (rl = setobj->record_list; rl; rl = rl->next)
542 (!setobj->recordElements || !rl->elements ||
543 !strcmp (setobj->recordElements, rl->elements)))
548 static void delete_IR_records (IrTcl_SetObj *setobj)
550 IrTcl_RecordList *rl, *rl1;
552 for (rl = setobj->record_list; rl; rl = rl1)
554 delete_IR_record (rl);
558 setobj->record_list = NULL;
562 * ir_tcl_get_set_int: Set/get integer value
564 int ir_tcl_get_set_int (int *val, Tcl_Interp *interp, int argc, char **argv)
570 if (Tcl_GetInt (interp, argv[2], val)==TCL_ERROR)
573 sprintf (buf, "%d", *val);
574 Tcl_AppendResult (interp, buf, NULL);
580 * ir_tcl_method_error
582 int ir_tcl_method_error (Tcl_Interp *interp, int argc, char **argv,
585 IrTcl_Methods *tab_i = tab;
588 Tcl_AppendResult (interp, "bad method: \"", *argv, " ", argv[1],
589 "\"\nmethod should be of:", NULL);
590 for (tab_i = tab; tab_i->tab; tab_i++)
591 for (t = tab_i->tab; t->name; t++)
592 Tcl_AppendResult (interp, " ", t->name, NULL);
597 * ir_tcl_method: Search for method in table and invoke method handler
599 int ir_tcl_method (Tcl_Interp *interp, int argc, char **argv,
600 IrTcl_Methods *tab, int *ret)
602 IrTcl_Methods *tab_i = tab;
605 for (tab_i = tab; tab_i->tab; tab_i++)
606 for (t = tab_i->tab; t->name; t++)
609 if ((*t->method)(tab_i->obj, interp, argc, argv) == TCL_ERROR)
613 if (!strcmp (t->name, argv[1]))
615 *ret = (*t->method)(tab_i->obj, interp, argc, argv);
626 * ir_tcl_named_bits: get/set named bits
628 int ir_tcl_named_bits (struct ir_named_entry *tab, Odr_bitmask *ob,
629 Tcl_Interp *interp, int argc, char **argv)
631 struct ir_named_entry *ti;
636 for (no = 0; no < argc; no++)
638 for (ti = tab; ti->name; ti++)
639 if (!strcmp (argv[no], ti->name))
641 ODR_MASK_SET (ob, ti->pos);
646 Tcl_AppendResult (interp, "bad bit mask ", argv[no], NULL);
647 return ir_tcl_error_exec (interp, argc, argv);
652 for (ti = tab; ti->name; ti++)
653 if (ODR_MASK_GET (ob, ti->pos))
654 Tcl_AppendElement (interp, ti->name);
658 static void set_referenceId (ODR o, Z_ReferenceId **dst, const char *src)
664 *dst = odr_malloc (o, sizeof(**dst));
665 (*dst)->size = (*dst)->len = strlen(src);
666 (*dst)->buf = odr_malloc (o, (*dst)->len);
667 memcpy ((*dst)->buf, src, (*dst)->len);
671 static void get_referenceId (char **dst, Z_ReferenceId *src)
679 *dst = ir_tcl_malloc (src->len+1);
680 memcpy (*dst, src->buf, src->len);
681 (*dst)[src->len] = '\0';
684 /* ------------------------------------------------------- */
687 * do_init_request: init method on IR object
689 static int do_init_request (void *obj, Tcl_Interp *interp,
690 int argc, char **argv)
698 logf (LOG_DEBUG, "init %s", *argv);
701 Tcl_AppendResult (interp, "not connected", NULL);
702 return ir_tcl_error_exec (interp, argc, argv);
704 apdu = zget_APDU (p->odr_out, Z_APDU_initRequest);
705 req = apdu->u.initRequest;
707 set_referenceId (p->odr_out, &req->referenceId, p->set_inher.referenceId);
708 req->options = &p->options;
709 req->protocolVersion = &p->protocolVersion;
710 req->preferredMessageSize = &p->preferredMessageSize;
711 req->maximumRecordSize = &p->maximumRecordSize;
713 if (p->idAuthenticationGroupId)
715 Z_IdPass *pass = odr_malloc (p->odr_out, sizeof(*pass));
716 Z_IdAuthentication *auth = odr_malloc (p->odr_out, sizeof(*auth));
718 logf (LOG_DEBUG, "using pass authentication");
720 auth->which = Z_IdAuthentication_idPass;
721 auth->u.idPass = pass;
722 if (p->idAuthenticationGroupId && *p->idAuthenticationGroupId)
723 pass->groupId = p->idAuthenticationGroupId;
725 pass->groupId = NULL;
726 if (p->idAuthenticationUserId && *p->idAuthenticationUserId)
727 pass->userId = p->idAuthenticationUserId;
730 if (p->idAuthenticationPassword && *p->idAuthenticationPassword)
731 pass->password = p->idAuthenticationPassword;
733 pass->password = NULL;
734 req->idAuthentication = auth;
736 else if (!p->idAuthenticationOpen || !*p->idAuthenticationOpen)
737 req->idAuthentication = NULL;
740 Z_IdAuthentication *auth = odr_malloc (p->odr_out, sizeof(*auth));
742 logf (LOG_DEBUG, "using open authentication");
743 auth->which = Z_IdAuthentication_open;
744 auth->u.open = p->idAuthenticationOpen;
745 req->idAuthentication = auth;
747 req->implementationId = p->implementationId;
748 req->implementationName = p->implementationName;
749 req->implementationVersion = p->implementationVersion;
750 req->userInformationField = 0;
752 return ir_tcl_send_APDU (interp, p, apdu, "init", *argv);
756 * do_protocolVersion: Set protocol Version
758 static int do_protocolVersion (void *obj, Tcl_Interp *interp,
759 int argc, char **argv)
767 ODR_MASK_ZERO (&p->protocolVersion);
768 ODR_MASK_SET (&p->protocolVersion, 0);
769 ODR_MASK_SET (&p->protocolVersion, 1);
774 if (Tcl_GetInt (interp, argv[2], &version)==TCL_ERROR)
776 ODR_MASK_ZERO (&p->protocolVersion);
777 for (i = 0; i<version; i++)
778 ODR_MASK_SET (&p->protocolVersion, i);
780 for (i = 4; --i >= 0; )
781 if (ODR_MASK_GET (&p->protocolVersion, i))
783 sprintf (buf, "%d", i+1);
784 interp->result = buf;
789 * do_options: Set options
791 static int do_options (void *obj, Tcl_Interp *interp,
792 int argc, char **argv)
794 static struct ir_named_entry options_tab[] = {
798 { "resourceReport", 3 },
799 { "triggerResourceCtrl", 4},
800 { "resourceCtrl", 5},
804 { "extendedServices", 10},
805 { "level-1Segmentation", 11},
806 { "level-2Segmentation", 12},
807 { "concurrentOperations", 13},
808 { "namedResultSets", 14},
815 ODR_MASK_ZERO (&p->options);
816 ODR_MASK_SET (&p->options, 0);
817 ODR_MASK_SET (&p->options, 1);
818 ODR_MASK_SET (&p->options, 4);
819 ODR_MASK_SET (&p->options, 7);
820 ODR_MASK_SET (&p->options, 14);
823 return ir_tcl_named_bits (options_tab, &p->options, interp, argc-2, argv+2);
827 * do_apduInfo: Get APDU information
829 static int do_apduInfo (void *obj, Tcl_Interp *interp, int argc, char **argv)
837 sprintf (buf, "%d", p->apduLen);
838 Tcl_AppendElement (interp, buf);
839 sprintf (buf, "%d", p->apduOffset);
840 Tcl_AppendElement (interp, buf);
843 Tcl_AppendElement (interp, "");
846 apduf = fopen ("apdu.tmp", "w");
849 Tcl_AppendElement (interp, "");
852 odr_dumpBER (apduf, p->buf_in, p->apduLen);
854 if (!(apduf = fopen ("apdu.tmp", "r")))
855 Tcl_AppendElement (interp, "");
860 Tcl_AppendResult (interp, " {", NULL);
861 while ((c = getc (apduf)) != EOF)
865 Tcl_AppendResult (interp, buf, NULL);
868 Tcl_AppendResult (interp, "}", NULL);
875 * do_failInfo: Get fail information
877 static int do_failInfo (void *obj, Tcl_Interp *interp, int argc, char **argv)
887 sprintf (buf, "%d", p->failInfo);
893 case IR_TCL_FAIL_CONNECT:
894 cp = "connect failed";
896 case IR_TCL_FAIL_READ:
897 cp = "connection closed";
899 case IR_TCL_FAIL_WRITE:
900 cp = "connection closed";
902 case IR_TCL_FAIL_IN_APDU:
903 cp = "failed to decode incoming APDU";
905 case IR_TCL_FAIL_UNKNOWN_APDU:
911 Tcl_AppendElement (interp, buf);
912 Tcl_AppendElement (interp, cp);
917 * do_preferredMessageSize: Set/get preferred message size
919 static int do_preferredMessageSize (void *obj, Tcl_Interp *interp,
920 int argc, char **argv)
926 p->preferredMessageSize = 30000;
929 return ir_tcl_get_set_int (&p->preferredMessageSize, interp, argc, argv);
933 * do_maximumRecordSize: Set/get maximum record size
935 static int do_maximumRecordSize (void *obj, Tcl_Interp *interp,
936 int argc, char **argv)
942 p->maximumRecordSize = 30000;
945 return ir_tcl_get_set_int (&p->maximumRecordSize, interp, argc, argv);
949 * do_initResult: Get init result
951 static int do_initResult (void *obj, Tcl_Interp *interp,
952 int argc, char **argv)
958 return ir_tcl_get_set_int (&p->initResult, interp, argc, argv);
963 * do_implementationName: Set/get Implementation Name.
965 static int do_implementationName (void *obj, Tcl_Interp *interp,
966 int argc, char **argv)
971 return ir_tcl_strdup (interp, &p->implementationName,
972 "Index Data/IrTcl on YAZ");
974 return ir_tcl_strdel (interp, &p->implementationName);
977 xfree (p->implementationName);
978 if (ir_tcl_strdup (interp, &p->implementationName, argv[2])
982 Tcl_AppendResult (interp, p->implementationName, (char*) NULL);
987 * do_implementationId: Get Implementation Id.
989 static int do_implementationId (void *obj, Tcl_Interp *interp,
990 int argc, char **argv)
995 return ir_tcl_strdup (interp, &p->implementationId, "YAZ (id=81)");
997 return ir_tcl_strdel (interp, &p->implementationId);
998 Tcl_AppendResult (interp, p->implementationId, (char*) NULL);
1003 * do_implementationVersion: get Implementation Version.
1005 static int do_implementationVersion (void *obj, Tcl_Interp *interp,
1006 int argc, char **argv)
1011 return ir_tcl_strdup (interp, &p->implementationVersion,
1013 #ifdef IR_TCL_VERSION
1014 " / Irtcl: " IR_TCL_VERSION
1017 else if (argc == -1)
1018 return ir_tcl_strdel (interp, &p->implementationVersion);
1019 Tcl_AppendResult (interp, p->implementationVersion, (char*) NULL);
1024 * do_targetImplementationName: Get Implementation Name of target.
1026 static int do_targetImplementationName (void *obj, Tcl_Interp *interp,
1027 int argc, char **argv)
1033 p->targetImplementationName = NULL;
1036 else if (argc == -1)
1037 return ir_tcl_strdel (interp, &p->targetImplementationName);
1038 Tcl_AppendResult (interp, p->targetImplementationName, (char*) NULL);
1043 * do_targetImplementationId: Get Implementation Id of target
1045 static int do_targetImplementationId (void *obj, Tcl_Interp *interp,
1046 int argc, char **argv)
1052 p->targetImplementationId = NULL;
1055 else if (argc == -1)
1056 return ir_tcl_strdel (interp, &p->targetImplementationId);
1057 Tcl_AppendResult (interp, p->targetImplementationId, (char*) NULL);
1062 * do_targetImplementationVersion: Get Implementation Version of target
1064 static int do_targetImplementationVersion (void *obj, Tcl_Interp *interp,
1065 int argc, char **argv)
1071 p->targetImplementationVersion = NULL;
1074 else if (argc == -1)
1075 return ir_tcl_strdel (interp, &p->targetImplementationVersion);
1076 Tcl_AppendResult (interp, p->targetImplementationVersion, (char*) NULL);
1081 * do_idAuthentication: Set/get id Authentication
1083 static int do_idAuthentication (void *obj, Tcl_Interp *interp,
1084 int argc, char **argv)
1088 if (argc >= 3 || argc == -1)
1090 xfree (p->idAuthenticationOpen);
1091 xfree (p->idAuthenticationGroupId);
1092 xfree (p->idAuthenticationUserId);
1093 xfree (p->idAuthenticationPassword);
1095 if (argc >= 3 || argc <= 0)
1097 p->idAuthenticationOpen = NULL;
1098 p->idAuthenticationGroupId = NULL;
1099 p->idAuthenticationUserId = NULL;
1100 p->idAuthenticationPassword = NULL;
1109 ir_tcl_strdup (interp, &p->idAuthenticationOpen, argv[2])
1116 ir_tcl_strdup (interp, &p->idAuthenticationGroupId, argv[2])
1120 ir_tcl_strdup (interp, &p->idAuthenticationUserId, argv[3])
1124 ir_tcl_strdup (interp, &p->idAuthenticationPassword, argv[4])
1129 if (p->idAuthenticationOpen)
1130 Tcl_AppendElement (interp, p->idAuthenticationOpen);
1131 else if (p->idAuthenticationGroupId)
1133 Tcl_AppendElement (interp, p->idAuthenticationGroupId);
1134 Tcl_AppendElement (interp, p->idAuthenticationUserId);
1135 Tcl_AppendElement (interp, p->idAuthenticationPassword);
1141 * do_connect: connect method on IR object
1143 static int do_connect (void *obj, Tcl_Interp *interp,
1144 int argc, char **argv)
1154 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
1155 " ?hostname?\"", NULL);
1160 Tcl_AppendResult (interp, p->hostname, NULL);
1164 logf (LOG_DEBUG, "connect %s %s", *argv, argv[2]);
1167 Tcl_AppendResult (interp, "already connected", NULL);
1168 return ir_tcl_error_exec (interp, argc, argv);
1170 if (!strcmp (p->comstackType, "tcpip"))
1172 p->cs_link = cs_create (tcpip_type, CS_BLOCK, p->protocol_type);
1173 addr = tcpip_strtoaddr (argv[2]);
1176 Tcl_AppendResult (interp, "tcpip_strtoaddr fail", NULL);
1177 return ir_tcl_error_exec (interp, argc, argv);
1179 logf (LOG_DEBUG, "tcp/ip connect %s", argv[2]);
1181 else if (!strcmp (p->comstackType, "mosi"))
1184 p->cs_link = cs_create (mosi_type, CS_BLOCK, p->protocol_type);
1185 addr = mosi_strtoaddr (argv[2]);
1188 Tcl_AppendResult (interp, "mosi_strtoaddr fail", NULL);
1189 return ir_tcl_error_exec (interp, argc, argv);
1191 logf (LOG_DEBUG, "mosi connect %s", argv[2]);
1193 Tcl_AppendResult (interp, "mosi not supported", NULL);
1194 return ir_tcl_error_exec (interp, argc, argv);
1199 Tcl_AppendResult (interp, "bad comstack type ",
1200 p->comstackType, NULL);
1201 return ir_tcl_error_exec (interp, argc, argv);
1203 if (ir_tcl_strdup (interp, &p->hostname, argv[2]) == TCL_ERROR)
1205 p->eventType = "connect";
1206 if ((r=cs_connect (p->cs_link, addr)) < 0)
1208 ir_tcl_disconnect (p);
1209 Tcl_AppendResult (interp, "connect fail", NULL);
1210 return ir_tcl_error_exec (interp, argc, argv);
1212 ir_select_add (cs_fileno (p->cs_link), p);
1215 logf (LOG_DEBUG, "connect pending fd=%d", cs_fileno(p->cs_link));
1216 ir_select_add_write (cs_fileno (p->cs_link), p);
1217 p->state = IR_TCL_R_Connecting;
1221 logf (LOG_DEBUG, "connect ok fd=%d", cs_fileno(p->cs_link));
1222 p->state = IR_TCL_R_Idle;
1224 ir_tcl_eval (p->interp, p->callback);
1231 * ir_tcl_disconnect: close connection
1233 void ir_tcl_disconnect (IrTcl_Obj *p)
1237 logf(LOG_DEBUG, "Closing connection to %s", p->hostname);
1238 xfree (p->hostname);
1240 assert (p->cs_link);
1241 ir_select_remove (cs_fileno (p->cs_link), p);
1243 odr_reset (p->odr_in);
1245 cs_close (p->cs_link);
1248 ODR_MASK_ZERO (&p->options);
1249 ODR_MASK_SET (&p->options, 0);
1250 ODR_MASK_SET (&p->options, 1);
1251 ODR_MASK_SET (&p->options, 4);
1252 ODR_MASK_SET (&p->options, 7);
1253 ODR_MASK_SET (&p->options, 14);
1255 ODR_MASK_ZERO (&p->protocolVersion);
1256 ODR_MASK_SET (&p->protocolVersion, 0);
1257 ODR_MASK_SET (&p->protocolVersion, 1);
1260 assert (!p->cs_link);
1264 * do_disconnect: disconnect method on IR object
1266 static int do_disconnect (void *obj, Tcl_Interp *interp,
1267 int argc, char **argv)
1273 p->state = IR_TCL_R_Idle;
1274 p->eventType = NULL;
1279 ir_tcl_disconnect (p);
1284 * do_comstack: Set/get comstack method on IR object
1286 static int do_comstack (void *o, Tcl_Interp *interp,
1287 int argc, char **argv)
1292 return ir_tcl_strdup (interp, &obj->comstackType, "tcpip");
1293 else if (argc == -1)
1294 return ir_tcl_strdel (interp, &obj->comstackType);
1297 xfree (obj->comstackType);
1298 if (ir_tcl_strdup (interp, &obj->comstackType, argv[2]) == TCL_ERROR)
1301 Tcl_AppendElement (interp, obj->comstackType);
1306 * do_logLevel: Set log level
1308 static int do_logLevel (void *o, Tcl_Interp *interp,
1309 int argc, char **argv)
1314 log_init (log_mask_str (argv[2]), "", NULL);
1316 log_init (log_mask_str (argv[2]), argv[3], NULL);
1318 log_init (log_mask_str (argv[2]), argv[3], argv[4]);
1324 * do_eventType: Return type of last event
1326 static int do_eventType (void *obj, Tcl_Interp *interp,
1327 int argc, char **argv)
1333 p->eventType = NULL;
1336 Tcl_AppendElement (interp, p->eventType ? p->eventType : "");
1342 * do_callback: add callback
1344 static int do_callback (void *obj, Tcl_Interp *interp,
1345 int argc, char **argv)
1354 else if (argc == -1)
1355 return ir_tcl_strdel (interp, &p->callback);
1358 xfree (p->callback);
1361 if (ir_tcl_strdup (interp, &p->callback, argv[2]) == TCL_ERROR)
1371 * do_failback: add error handle callback
1373 static int do_failback (void *obj, Tcl_Interp *interp,
1374 int argc, char **argv)
1383 else if (argc == -1)
1384 return ir_tcl_strdel (interp, &p->failback);
1387 xfree (p->failback);
1390 if (ir_tcl_strdup (interp, &p->failback, argv[2]) == TCL_ERROR)
1400 * do_initResponse: add init response handler
1402 static int do_initResponse (void *obj, Tcl_Interp *interp,
1403 int argc, char **argv)
1409 p->initResponse = NULL;
1412 else if (argc == -1)
1413 return ir_tcl_strdel (interp, &p->initResponse);
1416 xfree (p->initResponse);
1419 if (ir_tcl_strdup (interp, &p->initResponse, argv[2]) == TCL_ERROR)
1423 p->initResponse = NULL;
1428 * do_protocol: Set/get protocol method on IR object
1430 static int do_protocol (void *o, Tcl_Interp *interp, int argc, char **argv)
1436 p->protocol_type = PROTO_Z3950;
1441 if (!strcmp (argv[2], "Z39"))
1442 p->protocol_type = PROTO_Z3950;
1443 else if (!strcmp (argv[2], "SR"))
1444 p->protocol_type = PROTO_SR;
1447 Tcl_AppendResult (interp, "bad protocol ", argv[2], NULL);
1448 return ir_tcl_error_exec (interp, argc, argv);
1452 switch (p->protocol_type)
1455 Tcl_AppendElement (interp, "Z39");
1458 Tcl_AppendElement (interp, "SR");
1465 * do_triggerResourceControl:
1467 static int do_triggerResourceControl (void *obj, Tcl_Interp *interp,
1468 int argc, char **argv)
1472 Z_TriggerResourceControlRequest *req;
1473 bool_t is_false = 0;
1479 Tcl_AppendResult (interp, "not connected", NULL);
1480 return ir_tcl_error_exec (interp, argc, argv);
1482 apdu = zget_APDU (p->odr_out, Z_APDU_triggerResourceControlRequest);
1483 req = apdu->u.triggerResourceControlRequest;
1484 *req->requestedAction = Z_TriggerResourceCtrl_cancel;
1485 req->resultSetWanted = &is_false;
1487 return ir_tcl_send_APDU (interp, p, apdu, "triggerResourceControl",
1492 * do_databaseNames: specify database names
1494 static int do_databaseNames (void *obj, Tcl_Interp *interp,
1495 int argc, char **argv)
1498 IrTcl_SetCObj *p = obj;
1502 for (i=0; i<p->num_databaseNames; i++)
1503 xfree (p->databaseNames[i]);
1504 xfree (p->databaseNames);
1508 p->num_databaseNames = 0;
1509 p->databaseNames = NULL;
1514 for (i=0; i<p->num_databaseNames; i++)
1515 Tcl_AppendElement (interp, p->databaseNames[i]);
1518 if (p->databaseNames)
1520 for (i=0; i<p->num_databaseNames; i++)
1521 xfree (p->databaseNames[i]);
1522 xfree (p->databaseNames);
1524 p->num_databaseNames = argc - 2;
1526 ir_tcl_malloc (sizeof(*p->databaseNames) * (1+p->num_databaseNames));
1527 for (i=0; i<p->num_databaseNames; i++)
1529 if (ir_tcl_strdup (interp, &p->databaseNames[i], argv[2+i])
1533 p->databaseNames[i] = NULL;
1538 * do_replaceIndicator: Set/get replace Set indicator
1540 static int do_replaceIndicator (void *obj, Tcl_Interp *interp,
1541 int argc, char **argv)
1543 IrTcl_SetCObj *p = obj;
1547 p->replaceIndicator = 1;
1550 return ir_tcl_get_set_int (&p->replaceIndicator, interp, argc, argv);
1554 * do_queryType: Set/Get query method
1556 static int do_queryType (void *obj, Tcl_Interp *interp,
1557 int argc, char **argv)
1559 IrTcl_SetCObj *p = obj;
1562 return ir_tcl_strdup (interp, &p->queryType, "rpn");
1563 else if (argc == -1)
1564 return ir_tcl_strdel (interp, &p->queryType);
1567 xfree (p->queryType);
1568 if (ir_tcl_strdup (interp, &p->queryType, argv[2]) == TCL_ERROR)
1571 Tcl_AppendResult (interp, p->queryType, NULL);
1576 * do_userInformationField: Get User information field
1578 static int do_userInformationField (void *obj, Tcl_Interp *interp,
1579 int argc, char **argv)
1585 p->userInformationField = NULL;
1588 else if (argc == -1)
1589 return ir_tcl_strdel (interp, &p->userInformationField);
1590 Tcl_AppendResult (interp, p->userInformationField, NULL);
1595 * do_smallSetUpperBound: Set/get small set upper bound
1597 static int do_smallSetUpperBound (void *o, Tcl_Interp *interp,
1598 int argc, char **argv)
1600 IrTcl_SetCObj *p = o;
1604 p->smallSetUpperBound = 0;
1607 return ir_tcl_get_set_int (&p->smallSetUpperBound, interp, argc, argv);
1611 * do_largeSetLowerBound: Set/get large set lower bound
1613 static int do_largeSetLowerBound (void *o, Tcl_Interp *interp,
1614 int argc, char **argv)
1616 IrTcl_SetCObj *p = o;
1620 p->largeSetLowerBound = 2;
1623 return ir_tcl_get_set_int (&p->largeSetLowerBound, interp, argc, argv);
1627 * do_mediumSetPresentNumber: Set/get large set lower bound
1629 static int do_mediumSetPresentNumber (void *o, Tcl_Interp *interp,
1630 int argc, char **argv)
1632 IrTcl_SetCObj *p = o;
1636 p->mediumSetPresentNumber = 0;
1639 return ir_tcl_get_set_int (&p->mediumSetPresentNumber, interp, argc, argv);
1643 * do_referenceId: Set/Get referenceId
1645 static int do_referenceId (void *obj, Tcl_Interp *interp,
1646 int argc, char **argv)
1648 IrTcl_SetCObj *p = obj;
1652 p->referenceId = NULL;
1655 else if (argc == -1)
1656 return ir_tcl_strdel (interp, &p->referenceId);
1659 xfree (p->referenceId);
1660 if (ir_tcl_strdup (interp, &p->referenceId, argv[2]) == TCL_ERROR)
1663 Tcl_AppendResult (interp, p->referenceId, NULL);
1668 * do_preferredRecordSyntax: Set/get preferred record syntax
1670 static int do_preferredRecordSyntax (void *obj, Tcl_Interp *interp,
1671 int argc, char **argv)
1673 IrTcl_SetCObj *p = obj;
1677 p->preferredRecordSyntax = NULL;
1680 else if (argc == -1)
1682 xfree (p->preferredRecordSyntax);
1683 p->preferredRecordSyntax = NULL;
1688 xfree (p->preferredRecordSyntax);
1689 p->preferredRecordSyntax = NULL;
1690 if (argv[2][0] && (p->preferredRecordSyntax =
1691 ir_tcl_malloc (sizeof(*p->preferredRecordSyntax))))
1692 *p->preferredRecordSyntax = IrTcl_getRecordSyntaxVal (argv[2]);
1697 (interp,!p->preferredRecordSyntax ? "" :
1698 IrTcl_getRecordSyntaxStr(*p->preferredRecordSyntax));
1705 * do_elementSetNames: Set/Get element Set Names
1707 static int do_elementSetNames (void *obj, Tcl_Interp *interp,
1708 int argc, char **argv)
1710 IrTcl_SetCObj *p = obj;
1714 p->elementSetNames = NULL;
1717 else if (argc == -1)
1718 return ir_tcl_strdel (interp, &p->elementSetNames);
1721 xfree (p->elementSetNames);
1722 if (ir_tcl_strdup (interp, &p->elementSetNames, argv[2]) == TCL_ERROR)
1725 Tcl_AppendResult (interp, p->elementSetNames, NULL);
1730 * do_smallSetElementSetNames: Set/Get small Set Element Set Names
1732 static int do_smallSetElementSetNames (void *obj, Tcl_Interp *interp,
1733 int argc, char **argv)
1735 IrTcl_SetCObj *p = obj;
1739 p->smallSetElementSetNames = NULL;
1742 else if (argc == -1)
1743 return ir_tcl_strdel (interp, &p->smallSetElementSetNames);
1746 xfree (p->smallSetElementSetNames);
1747 if (ir_tcl_strdup (interp, &p->smallSetElementSetNames,
1748 argv[2]) == TCL_ERROR)
1751 Tcl_AppendResult (interp, p->smallSetElementSetNames, NULL);
1756 * do_mediumSetElementSetNames: Set/Get medium Set Element Set Names
1758 static int do_mediumSetElementSetNames (void *obj, Tcl_Interp *interp,
1759 int argc, char **argv)
1761 IrTcl_SetCObj *p = obj;
1765 p->mediumSetElementSetNames = NULL;
1768 else if (argc == -1)
1769 return ir_tcl_strdel (interp, &p->mediumSetElementSetNames);
1772 xfree (p->mediumSetElementSetNames);
1773 if (ir_tcl_strdup (interp, &p->mediumSetElementSetNames,
1774 argv[2]) == TCL_ERROR)
1777 Tcl_AppendResult (interp, p->mediumSetElementSetNames, NULL);
1781 static IrTcl_Method ir_method_tab[] = {
1782 { "comstack", do_comstack, NULL },
1783 { "protocol", do_protocol, NULL },
1784 { "failback", do_failback, NULL },
1785 { "failInfo", do_failInfo, NULL },
1786 { "apduInfo", do_apduInfo, NULL },
1787 { "logLevel", do_logLevel, NULL },
1789 { "eventType", do_eventType, NULL },
1790 { "connect", do_connect, NULL },
1791 { "protocolVersion", do_protocolVersion, NULL },
1792 { "preferredMessageSize", do_preferredMessageSize, NULL },
1793 { "maximumRecordSize", do_maximumRecordSize, NULL },
1794 { "implementationName", do_implementationName, NULL },
1795 { "implementationId", do_implementationId, NULL },
1796 { "implementationVersion", do_implementationVersion, NULL },
1797 { "targetImplementationName", do_targetImplementationName, NULL },
1798 { "targetImplementationId", do_targetImplementationId, NULL },
1799 { "targetImplementationVersion", do_targetImplementationVersion, NULL},
1800 { "userInformationField", do_userInformationField, NULL},
1801 { "idAuthentication", do_idAuthentication, NULL},
1802 { "options", do_options, NULL},
1803 { "init", do_init_request, NULL},
1804 { "initResult", do_initResult, NULL},
1805 { "disconnect", do_disconnect, NULL},
1806 { "callback", do_callback, NULL},
1807 { "initResponse", do_initResponse, NULL},
1808 { "triggerResourceControl", do_triggerResourceControl, NULL},
1809 { "initResponse", do_initResponse, NULL},
1813 static IrTcl_Method ir_set_c_method_tab[] = {
1814 { "databaseNames", do_databaseNames, NULL},
1815 { "replaceIndicator", do_replaceIndicator, NULL},
1816 { "queryType", do_queryType, NULL},
1817 { "preferredRecordSyntax", do_preferredRecordSyntax, NULL},
1818 { "smallSetUpperBound", do_smallSetUpperBound, NULL},
1819 { "largeSetLowerBound", do_largeSetLowerBound, NULL},
1820 { "mediumSetPresentNumber", do_mediumSetPresentNumber, NULL},
1821 { "referenceId", do_referenceId, NULL},
1822 { "elementSetNames", do_elementSetNames, NULL},
1823 { "smallSetElementSetNames", do_smallSetElementSetNames, NULL},
1824 { "mediumSetElementSetNames", do_mediumSetElementSetNames, NULL},
1829 * ir_obj_method: IR Object methods
1831 static int ir_obj_method (ClientData clientData, Tcl_Interp *interp,
1832 int argc, char **argv)
1834 IrTcl_Methods tab[3];
1835 IrTcl_Obj *p = clientData;
1840 Tcl_AppendResult (interp, wrongArgs, *argv, "method args...\"", NULL);
1844 tab[0].tab = ir_method_tab;
1846 tab[1].tab = ir_set_c_method_tab;
1847 tab[1].obj = &p->set_inher;
1850 if (ir_tcl_method (interp, argc, argv, tab, &r) == TCL_ERROR)
1851 return ir_tcl_method_error (interp, argc, argv, tab);
1856 * ir_obj_delete: IR Object disposal
1858 static void ir_obj_delete (ClientData clientData)
1860 IrTcl_Obj *obj = clientData;
1861 IrTcl_Methods tab[3];
1864 if (obj->ref_count > 0)
1866 assert (obj->ref_count == 0);
1868 logf (LOG_DEBUG, "ir object delete");
1869 tab[0].tab = ir_method_tab;
1871 tab[1].tab = ir_set_c_method_tab;
1872 tab[1].obj = &obj->set_inher;
1875 ir_tcl_method (NULL, -1, NULL, tab, NULL);
1878 odr_destroy (obj->odr_in);
1879 odr_destroy (obj->odr_out);
1880 odr_destroy (obj->odr_pr);
1885 * ir_obj_init: IR Object initialization
1887 int ir_obj_init (ClientData clientData, Tcl_Interp *interp,
1888 int argc, char **argv, ClientData *subData,
1889 ClientData parentData)
1891 IrTcl_Methods tab[3];
1899 Tcl_AppendResult (interp, wrongArgs, *argv, " objName\"", NULL);
1902 obj = ir_tcl_malloc (sizeof(*obj));
1905 obj->bibset = ccl_qual_mk ();
1906 if ((inf = fopen ("default.bib", "r")))
1908 ccl_qual_file (obj->bibset, inf);
1913 logf (LOG_DEBUG, "ir object create %s", argv[1]);
1914 obj->odr_in = odr_createmem (ODR_DECODE);
1915 odr_choice_enable_bias (obj->odr_in, 0);
1916 obj->odr_out = odr_createmem (ODR_ENCODE);
1917 obj->odr_pr = odr_createmem (ODR_PRINT);
1918 obj->state = IR_TCL_R_Idle;
1919 obj->interp = interp;
1923 obj->request_queue = NULL;
1925 tab[0].tab = ir_method_tab;
1927 tab[1].tab = ir_set_c_method_tab;
1928 tab[1].obj = &obj->set_inher;
1931 if (ir_tcl_method (interp, 0, NULL, tab, NULL) == TCL_ERROR)
1933 Tcl_AppendResult (interp, "Failed to initialize ", argv[1], NULL);
1942 * ir_obj_mk: IR Object creation
1944 static int ir_obj_mk (ClientData clientData, Tcl_Interp *interp,
1945 int argc, char **argv)
1948 int r = ir_obj_init (clientData, interp, argc, argv, &subData, 0);
1952 Tcl_CreateCommand (interp, argv[1], ir_obj_method,
1953 subData, ir_obj_delete);
1957 IrTcl_Class ir_obj_class = {
1965 /* ------------------------------------------------------- */
1967 * do_search: Do search request
1969 static int do_search (void *o, Tcl_Interp *interp, int argc, char **argv)
1971 Z_SearchRequest *req;
1975 IrTcl_SetObj *obj = o;
1986 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1], "query\"",
1990 logf (LOG_DEBUG, "search %s %s", *argv, argv[2]);
1991 if (!obj->set_inher.num_databaseNames)
1993 Tcl_AppendResult (interp, "no databaseNames", NULL);
1994 return ir_tcl_error_exec (interp, argc, argv);
1998 Tcl_AppendResult (interp, "not connected", NULL);
1999 return ir_tcl_error_exec (interp, argc, argv);
2001 apdu = zget_APDU (p->odr_out, Z_APDU_searchRequest);
2002 req = apdu->u.searchRequest;
2006 set_referenceId (p->odr_out, &req->referenceId,
2007 obj->set_inher.referenceId);
2009 req->smallSetUpperBound = &obj->set_inher.smallSetUpperBound;
2010 req->largeSetLowerBound = &obj->set_inher.largeSetLowerBound;
2011 req->mediumSetPresentNumber = &obj->set_inher.mediumSetPresentNumber;
2012 req->replaceIndicator = &obj->set_inher.replaceIndicator;
2013 req->resultSetName = obj->setName ? obj->setName : "default";
2014 logf (LOG_DEBUG, "Search, resultSetName %s", req->resultSetName);
2015 req->num_databaseNames = obj->set_inher.num_databaseNames;
2016 req->databaseNames = obj->set_inher.databaseNames;
2017 for (r=0; r < obj->set_inher.num_databaseNames; r++)
2018 logf (LOG_DEBUG, " Database %s", obj->set_inher.databaseNames[r]);
2019 if (obj->set_inher.preferredRecordSyntax)
2021 struct oident ident;
2023 ident.proto = p->protocol_type;
2024 ident.oclass = CLASS_RECSYN;
2025 ident.value = *obj->set_inher.preferredRecordSyntax;
2026 logf (LOG_DEBUG, "Preferred record syntax is %d", ident.value);
2027 req->preferredRecordSyntax = odr_oiddup (p->odr_out,
2028 oid_getoidbyent (&ident));
2031 req->preferredRecordSyntax = 0;
2033 if (obj->set_inher.smallSetElementSetNames &&
2034 *obj->set_inher.smallSetElementSetNames)
2036 Z_ElementSetNames *esn = odr_malloc (p->odr_out, sizeof(*esn));
2038 esn->which = Z_ElementSetNames_generic;
2039 esn->u.generic = obj->set_inher.smallSetElementSetNames;
2040 req->smallSetElementSetNames = esn;
2043 req->smallSetElementSetNames = NULL;
2045 if (obj->set_inher.mediumSetElementSetNames &&
2046 *obj->set_inher.mediumSetElementSetNames)
2048 Z_ElementSetNames *esn = odr_malloc (p->odr_out, sizeof(*esn));
2050 esn->which = Z_ElementSetNames_generic;
2051 esn->u.generic = obj->set_inher.mediumSetElementSetNames;
2052 req->mediumSetElementSetNames = esn;
2055 req->mediumSetElementSetNames = NULL;
2057 req->query = &query;
2059 logf (LOG_DEBUG, "queryType %s", obj->set_inher.queryType);
2060 if (!strcmp (obj->set_inher.queryType, "rpn"))
2062 Z_RPNQuery *RPNquery;
2064 RPNquery = p_query_rpn (p->odr_out, p->protocol_type, argv[2]);
2067 Tcl_AppendResult (interp, "query syntax error", NULL);
2068 return ir_tcl_error_exec (interp, argc, argv);
2070 query.which = Z_Query_type_1;
2071 query.u.type_1 = RPNquery;
2074 else if (!strcmp (obj->set_inher.queryType, "cclrpn"))
2078 struct ccl_rpn_node *rpn;
2079 Z_RPNQuery *RPNquery;
2082 bib1.proto = p->protocol_type;
2083 bib1.oclass = CLASS_ATTSET;
2084 bib1.value = VAL_BIB1;
2086 rpn = ccl_find_str(p->bibset, argv[2], &error, &pos);
2089 Tcl_AppendResult (interp, "ccl syntax error ", ccl_err_msg(error),
2091 return ir_tcl_error_exec (interp, argc, argv);
2094 ccl_pr_tree (rpn, stderr);
2095 fprintf (stderr, "\n");
2097 assert((RPNquery = ccl_rpn_query(rpn)));
2098 RPNquery->attributeSetId = oid_getoidbyent (&bib1);
2099 query.which = Z_Query_type_1;
2100 query.u.type_1 = RPNquery;
2103 else if (!strcmp (obj->set_inher.queryType, "ccl"))
2105 query.which = Z_Query_type_2;
2106 query.u.type_2 = &ccl_query;
2107 ccl_query.buf = (unsigned char *) argv[2];
2108 ccl_query.len = strlen (argv[2]);
2112 Tcl_AppendResult (interp, "invalid query method ",
2113 obj->set_inher.queryType, NULL);
2114 return ir_tcl_error_exec (interp, argc, argv);
2116 return ir_tcl_send_APDU (interp, p, apdu, "search", *argv);
2120 * do_searchResponse: add search response handler
2122 static int do_searchResponse (void *o, Tcl_Interp *interp,
2123 int argc, char **argv)
2125 IrTcl_SetObj *obj = o;
2129 obj->searchResponse = NULL;
2132 else if (argc == -1)
2133 return ir_tcl_strdel (interp, &obj->searchResponse);
2136 xfree (obj->searchResponse);
2139 if (ir_tcl_strdup (interp, &obj->searchResponse, argv[2])
2144 obj->searchResponse = NULL;
2150 * do_presentResponse: add present response handler
2152 static int do_presentResponse (void *o, Tcl_Interp *interp,
2153 int argc, char **argv)
2155 IrTcl_SetObj *obj = o;
2159 obj->presentResponse = NULL;
2162 else if (argc == -1)
2163 return ir_tcl_strdel (interp, &obj->presentResponse);
2166 xfree (obj->presentResponse);
2169 if (ir_tcl_strdup (interp, &obj->presentResponse, argv[2])
2174 obj->presentResponse = NULL;
2180 * do_resultCount: Get number of hits
2182 static int do_resultCount (void *o, Tcl_Interp *interp,
2183 int argc, char **argv)
2185 IrTcl_SetObj *obj = o;
2189 obj->resultCount = 0;
2192 return ir_tcl_get_set_int (&obj->resultCount, interp, argc, argv);
2196 * do_searchStatus: Get search status (after search response)
2198 static int do_searchStatus (void *o, Tcl_Interp *interp,
2199 int argc, char **argv)
2201 IrTcl_SetObj *obj = o;
2205 return ir_tcl_get_set_int (&obj->searchStatus, interp, argc, argv);
2209 * do_presentStatus: Get search status (after search/present response)
2211 static int do_presentStatus (void *o, Tcl_Interp *interp,
2212 int argc, char **argv)
2214 IrTcl_SetObj *obj = o;
2218 return ir_tcl_get_set_int (&obj->presentStatus, interp, argc, argv);
2222 * do_nextResultSetPosition: Get next result set position
2223 * (after search/present response)
2225 static int do_nextResultSetPosition (void *o, Tcl_Interp *interp,
2226 int argc, char **argv)
2228 IrTcl_SetObj *obj = o;
2232 obj->nextResultSetPosition = 0;
2235 return ir_tcl_get_set_int (&obj->nextResultSetPosition, interp,
2240 * do_setName: Set result Set name
2242 static int do_setName (void *o, Tcl_Interp *interp,
2243 int argc, char **argv)
2245 IrTcl_SetObj *obj = o;
2248 return ir_tcl_strdup (interp, &obj->setName, "Default");
2249 else if (argc == -1)
2250 return ir_tcl_strdel (interp, &obj->setName);
2253 xfree (obj->setName);
2254 if (ir_tcl_strdup (interp, &obj->setName, argv[2])
2258 Tcl_AppendElement (interp, obj->setName);
2263 * do_numberOfRecordsReturned: Get number of records returned
2265 static int do_numberOfRecordsReturned (void *o, Tcl_Interp *interp,
2266 int argc, char **argv)
2268 IrTcl_SetObj *obj = o;
2272 obj->numberOfRecordsReturned = 0;
2275 return ir_tcl_get_set_int (&obj->numberOfRecordsReturned, interp,
2280 * do_type: Return type (if any) at position.
2282 static int do_type (void *o, Tcl_Interp *interp, int argc, char **argv)
2284 IrTcl_SetObj *obj = o;
2286 IrTcl_RecordList *rl;
2290 obj->record_list = NULL;
2293 else if (argc == -1)
2295 delete_IR_records (obj);
2300 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2301 " position\"", NULL);
2304 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2306 rl = find_IR_record (obj, offset);
2309 logf (LOG_DEBUG, "%s %s %s: no record", argv[0], argv[1], argv[2]);
2314 case Z_NamePlusRecord_databaseRecord:
2315 interp->result = "DB";
2317 case Z_NamePlusRecord_surrogateDiagnostic:
2318 interp->result = "SD";
2326 * do_recordType: Return record type (if any) at position.
2328 static int do_recordType (void *o, Tcl_Interp *interp, int argc, char **argv)
2330 IrTcl_SetObj *obj = o;
2332 IrTcl_RecordList *rl;
2338 else if (argc == -1)
2344 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2345 " position\"", NULL);
2348 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2350 rl = find_IR_record (obj, offset);
2353 logf (LOG_DEBUG, "%s %s %s: no record", argv[0], argv[1], argv[2]);
2356 if (rl->which != Z_NamePlusRecord_databaseRecord)
2358 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2361 Tcl_AppendElement (interp, (char*)
2362 IrTcl_getRecordSyntaxStr (rl->u.dbrec.type));
2367 * set record elements (for record extraction)
2369 static int do_recordElements (void *o, Tcl_Interp *interp,
2370 int argc, char **argv)
2372 IrTcl_SetObj *obj = o;
2376 obj->recordElements = NULL;
2379 else if (argc == -1)
2380 return ir_tcl_strdel (NULL, &obj->recordElements);
2383 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2384 " ?position?\"", NULL);
2389 xfree (obj->recordElements);
2390 return ir_tcl_strdup (NULL, &obj->recordElements,
2391 (*argv[2] ? argv[2] : NULL));
2393 Tcl_AppendResult (interp, obj->recordElements, NULL);
2400 static int ir_diagResult (Tcl_Interp *interp, IrTcl_Diagnostic *list, int num)
2406 for (i = 0; i<num; i++)
2408 sprintf (buf, "%d", list[i].condition);
2409 Tcl_AppendElement (interp, buf);
2410 cp = diagbib1_str (list[i].condition);
2412 Tcl_AppendElement (interp, (char*) cp);
2414 Tcl_AppendElement (interp, "");
2415 if (list[i].addinfo)
2416 Tcl_AppendElement (interp, (char*) list[i].addinfo);
2418 Tcl_AppendElement (interp, "");
2424 * do_diag: Return diagnostic record info
2426 static int do_diag (void *o, Tcl_Interp *interp, int argc, char **argv)
2428 IrTcl_SetObj *obj = o;
2430 IrTcl_RecordList *rl;
2436 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2437 " position\"", NULL);
2440 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2442 rl = find_IR_record (obj, offset);
2445 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2448 if (rl->which != Z_NamePlusRecord_surrogateDiagnostic)
2450 Tcl_AppendResult (interp, "No Diagnostic record at #", argv[2], NULL);
2453 return ir_diagResult (interp, rl->u.surrogateDiagnostics.list,
2454 rl->u.surrogateDiagnostics.num);
2458 * do_getMarc: Get ISO2709 Record lines/fields
2460 static int do_getMarc (void *o, Tcl_Interp *interp, int argc, char **argv)
2462 IrTcl_SetObj *obj = o;
2464 IrTcl_RecordList *rl;
2470 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2471 " position line|field tag ind field\"", NULL);
2474 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2476 rl = find_IR_record (obj, offset);
2479 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2482 if (rl->which != Z_NamePlusRecord_databaseRecord)
2484 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2487 return ir_tcl_get_marc (interp, rl->u.dbrec.buf, argc, argv);
2491 * do_getSutrs: Get SUTRS Record
2493 static int do_getSutrs (void *o, Tcl_Interp *interp, int argc, char **argv)
2495 IrTcl_SetObj *obj = o;
2497 IrTcl_RecordList *rl;
2503 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2504 " position\"", NULL);
2507 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2509 rl = find_IR_record (obj, offset);
2512 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2515 if (rl->which != Z_NamePlusRecord_databaseRecord)
2517 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2520 if (rl->u.dbrec.type != VAL_SUTRS)
2522 Tcl_AppendElement (interp, rl->u.dbrec.buf);
2528 * do_getGrs: Get a GRS-1 Record
2530 static int do_getGrs (void *o, Tcl_Interp *interp, int argc, char **argv)
2532 IrTcl_SetObj *obj = o;
2534 IrTcl_RecordList *rl;
2540 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2541 " position ?(set,tag) (set,tag) ...?\"", NULL);
2544 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2546 rl = find_IR_record (obj, offset);
2549 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2552 if (rl->which != Z_NamePlusRecord_databaseRecord)
2554 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2557 if (rl->u.dbrec.type != VAL_GRS1)
2559 return ir_tcl_get_grs (interp, rl->u.dbrec.u.grs1, argc, argv);
2564 * do_getExplain: Get an Explain Record
2566 static int do_getExplain (void *o, Tcl_Interp *interp, int argc, char **argv)
2568 IrTcl_SetObj *obj = o;
2569 IrTcl_Obj *p = obj->parent;
2571 Z_ext_typeent *etype;
2573 IrTcl_RecordList *rl;
2579 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2580 " position ?mask? ...\"", NULL);
2583 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2585 rl = find_IR_record (obj, offset);
2588 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2591 if (rl->which != Z_NamePlusRecord_databaseRecord)
2593 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2596 if (rl->u.dbrec.type != VAL_EXPLAIN)
2599 if (!(etype = z_ext_getentbyref (VAL_EXPLAIN)))
2602 odr_setbuf (p->odr_in, rl->u.dbrec.buf, rl->u.dbrec.size, 0);
2603 if (!(*etype->fun)(p->odr_in, &rr, 0))
2606 if (etype->what != Z_External_explainRecord)
2609 return ir_tcl_get_explain (interp, rr, argc, argv);
2613 * do_responseStatus: Return response status (present or search)
2615 static int do_responseStatus (void *o, Tcl_Interp *interp,
2616 int argc, char **argv)
2618 IrTcl_SetObj *obj = o;
2622 obj->recordFlag = 0;
2623 obj->nonSurrogateDiagnosticNum = 0;
2624 obj->nonSurrogateDiagnosticList = NULL;
2627 else if (argc == -1)
2629 ir_deleteDiags (&obj->nonSurrogateDiagnosticList,
2630 &obj->nonSurrogateDiagnosticNum);
2633 if (!obj->recordFlag)
2635 Tcl_AppendElement (interp, "OK");
2640 case Z_Records_DBOSD:
2641 Tcl_AppendElement (interp, "DBOSD");
2644 Tcl_AppendElement (interp, "NSD");
2645 return ir_diagResult (interp, obj->nonSurrogateDiagnosticList,
2646 obj->nonSurrogateDiagnosticNum);
2652 * do_present: Perform Present Request
2655 static int do_present (void *o, Tcl_Interp *interp, int argc, char **argv)
2657 IrTcl_SetObj *obj = o;
2660 Z_PresentRequest *req;
2668 if (Tcl_GetInt (interp, argv[2], &start) == TCL_ERROR)
2675 if (Tcl_GetInt (interp, argv[3], &number) == TCL_ERROR)
2680 logf (LOG_DEBUG, "present %s %d %d", *argv, start, number);
2684 Tcl_AppendResult (interp, "not connected", NULL);
2685 return ir_tcl_error_exec (interp, argc, argv);
2688 obj->number = number;
2690 apdu = zget_APDU (p->odr_out, Z_APDU_presentRequest);
2691 req = apdu->u.presentRequest;
2693 set_referenceId (p->odr_out, &req->referenceId,
2694 obj->set_inher.referenceId);
2696 req->resultSetId = obj->setName ? obj->setName : "Default";
2698 req->resultSetStartPoint = &start;
2699 req->numberOfRecordsRequested = &number;
2700 if (obj->set_inher.preferredRecordSyntax)
2702 struct oident ident;
2704 ident.proto = p->protocol_type;
2705 ident.oclass = CLASS_RECSYN;
2706 ident.value = *obj->set_inher.preferredRecordSyntax;
2707 logf (LOG_DEBUG, "Preferred record syntax is %d", ident.value);
2708 req->preferredRecordSyntax = odr_oiddup (p->odr_out,
2709 oid_getoidbyent (&ident));
2712 req->preferredRecordSyntax = 0;
2714 if (obj->set_inher.elementSetNames && *obj->set_inher.elementSetNames)
2716 Z_ElementSetNames *esn = odr_malloc (p->odr_out, sizeof(*esn));
2717 Z_RecordComposition *compo = odr_malloc (p->odr_out, sizeof(*compo));
2719 esn->which = Z_ElementSetNames_generic;
2720 esn->u.generic = obj->set_inher.elementSetNames;
2722 req->recordComposition = compo;
2723 compo->which = Z_RecordComp_simple;
2724 compo->u.simple = esn;
2727 req->recordComposition = NULL;
2728 return ir_tcl_send_APDU (interp, p, apdu, "present", *argv);
2731 #define IR_TCL_RECORD_ENCODING_ISO2709 1
2732 #define IR_TCL_RECORD_ENCODING_RAW 2
2738 } IrTcl_FileRecordHead;
2741 * do_loadFile: Load result set from file
2743 static int do_loadFile (void *o, Tcl_Interp *interp,
2744 int argc, char **argv)
2746 IrTcl_SetObj *setobj = o;
2758 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2759 " filename ?start? ?number?\"", NULL);
2763 start = atoi (argv[3]);
2765 number = atoi (argv[4]);
2768 inf = fopen (argv[2], "r");
2771 Tcl_AppendResult (interp, "Cannot open ", argv[2], NULL);
2772 return ir_tcl_error_exec (interp, argc, argv);
2774 while (offset < (start+number))
2776 IrTcl_FileRecordHead head;
2777 IrTcl_RecordList *rl;
2779 if (fread (&head, sizeof(head), 1, inf) < 1)
2781 rl = new_IR_record (setobj, offset,
2782 Z_NamePlusRecord_databaseRecord,
2783 (argc > 5) ? argv[5] : NULL);
2784 rl->u.dbrec.type = head.syntax;
2785 if (head.encoding == IR_TCL_RECORD_ENCODING_ISO2709)
2787 if (!(buf = ir_tcl_fread_marc (inf, &size)))
2789 rl->u.dbrec.buf = buf;
2790 rl->u.dbrec.size = size;
2791 if (size != head.size)
2794 Tcl_AppendResult (interp, "bad ISO2709 encoding", NULL);
2795 return ir_tcl_error_exec (interp, argc, argv);
2798 else if (head.encoding == IR_TCL_RECORD_ENCODING_RAW)
2800 rl->u.dbrec.size = head.size;
2801 rl->u.dbrec.buf = ir_tcl_malloc (head.size + 1);
2802 if (fread (rl->u.dbrec.buf, rl->u.dbrec.size, 1, inf) < 1)
2805 Tcl_AppendResult (interp, "bad raw encoding", NULL);
2806 return ir_tcl_error_exec (interp, argc, argv);
2808 rl->u.dbrec.buf[rl->u.dbrec.size] = '\0';
2812 rl->u.dbrec.buf = NULL;
2813 rl->u.dbrec.size = 0;
2815 Tcl_AppendResult (interp, "bad encoding", NULL);
2816 return ir_tcl_error_exec (interp, argc, argv);
2820 setobj->numberOfRecordsReturned = offset - start;
2826 * do_saveFile: Save result set on file
2828 static int do_saveFile (void *o, Tcl_Interp *interp,
2829 int argc, char **argv)
2831 IrTcl_SetObj *setobj = o;
2836 IrTcl_RecordList *rl;
2842 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2843 " filename ?start? ?number?\"", NULL);
2847 start = atoi (argv[3]);
2849 number = atoi (argv[4]);
2852 outf = fopen (argv[2], "w");
2855 Tcl_AppendResult (interp, "cannot open file", NULL);
2856 return ir_tcl_error_exec (interp, argc, argv);
2858 while (offset < (start+number) && (rl = find_IR_record (setobj, offset)))
2860 if (rl->which == Z_NamePlusRecord_databaseRecord &&
2861 rl->u.dbrec.buf && rl->u.dbrec.size)
2863 IrTcl_FileRecordHead head;
2865 head.encoding = IR_TCL_RECORD_ENCODING_RAW;
2866 head.syntax = rl->u.dbrec.type;
2867 head.size = rl->u.dbrec.size;
2868 if (fwrite (&head, sizeof(head), 1, outf) < 1)
2870 Tcl_AppendResult (interp, "cannot write", NULL);
2871 return ir_tcl_error_exec (interp, argc, argv);
2873 if (fwrite (rl->u.dbrec.buf, rl->u.dbrec.size, 1, outf) < 1)
2875 Tcl_AppendResult (interp, "cannot write", NULL);
2876 return ir_tcl_error_exec (interp, argc, argv);
2883 Tcl_AppendResult (interp, "cannot write ", NULL);
2884 return ir_tcl_error_exec (interp, argc, argv);
2890 static IrTcl_Method ir_set_method_tab[] = {
2891 { "search", do_search, NULL},
2892 { "searchResponse", do_searchResponse, NULL},
2893 { "presentResponse", do_presentResponse, NULL},
2894 { "searchStatus", do_searchStatus, NULL},
2895 { "presentStatus", do_presentStatus, NULL},
2896 { "nextResultSetPosition", do_nextResultSetPosition, NULL},
2897 { "setName", do_setName, NULL},
2898 { "resultCount", do_resultCount, NULL},
2899 { "numberOfRecordsReturned", do_numberOfRecordsReturned, NULL},
2900 { "present", do_present, NULL},
2901 { "type", do_type, NULL},
2902 { "getMarc", do_getMarc, NULL},
2903 { "getSutrs", do_getSutrs, NULL},
2904 { "getGrs", do_getGrs, NULL},
2905 { "getExplain", do_getExplain, NULL},
2906 { "recordType", do_recordType, NULL},
2907 { "recordElements", do_recordElements, NULL},
2908 { "diag", do_diag, NULL},
2909 { "responseStatus", do_responseStatus, NULL},
2910 { "loadFile", do_loadFile, NULL},
2911 { "saveFile", do_saveFile, NULL},
2916 * ir_set_obj_method: IR Set Object methods
2918 static int ir_set_obj_method (ClientData clientData, Tcl_Interp *interp,
2919 int argc, char **argv)
2921 IrTcl_Methods tabs[3];
2922 IrTcl_SetObj *p = clientData;
2927 Tcl_AppendResult (interp, wrongArgs, *argv, " method args...\"", NULL);
2930 tabs[0].tab = ir_set_method_tab;
2932 tabs[1].tab = ir_set_c_method_tab;
2933 tabs[1].obj = &p->set_inher;
2936 if (ir_tcl_method (interp, argc, argv, tabs, &r) == TCL_ERROR)
2937 return ir_tcl_method_error (interp, argc, argv, tabs);
2942 * ir_set_obj_delete: IR Set Object disposal
2944 static void ir_set_obj_delete (ClientData clientData)
2946 IrTcl_Methods tabs[3];
2947 IrTcl_SetObj *p = clientData;
2949 logf (LOG_DEBUG, "ir set delete");
2951 tabs[0].tab = ir_set_method_tab;
2953 tabs[1].tab = ir_set_c_method_tab;
2954 tabs[1].obj = &p->set_inher;
2957 ir_tcl_method (NULL, -1, NULL, tabs, NULL);
2963 * ir_set_obj_init: IR Set Object initialization
2965 static int ir_set_obj_init (ClientData clientData, Tcl_Interp *interp,
2966 int argc, char **argv, ClientData *subData,
2967 ClientData parentData)
2969 IrTcl_Methods tabs[3];
2972 if (argc < 2 || argc > 3)
2974 Tcl_AppendResult (interp, wrongArgs, *argv,
2975 " objSetName ?objParent?\"", NULL);
2978 obj = ir_tcl_malloc (sizeof(*obj));
2979 logf (LOG_DEBUG, "ir set create %s", argv[1]);
2986 obj->parent = (IrTcl_Obj *) parentData;
2988 dst = &obj->set_inher;
2989 src = &obj->parent->set_inher;
2991 if ((dst->num_databaseNames = src->num_databaseNames))
2993 dst->databaseNames =
2994 ir_tcl_malloc (sizeof (*dst->databaseNames)
2995 * (1+dst->num_databaseNames));
2996 for (i = 0; i < dst->num_databaseNames; i++)
2997 if (ir_tcl_strdup (interp, &dst->databaseNames[i],
2998 src->databaseNames[i]) == TCL_ERROR)
3000 dst->databaseNames[i] = NULL;
3003 dst->databaseNames = NULL;
3004 if (ir_tcl_strdup (interp, &dst->queryType, src->queryType)
3008 if (ir_tcl_strdup (interp, &dst->referenceId, src->referenceId)
3012 if (ir_tcl_strdup (interp, &dst->elementSetNames, src->elementSetNames)
3016 if (ir_tcl_strdup (interp, &dst->smallSetElementSetNames,
3017 src->smallSetElementSetNames)
3021 if (ir_tcl_strdup (interp, &dst->mediumSetElementSetNames,
3022 src->mediumSetElementSetNames)
3026 if (src->preferredRecordSyntax &&
3027 (dst->preferredRecordSyntax
3028 = ir_tcl_malloc (sizeof(*dst->preferredRecordSyntax))))
3029 *dst->preferredRecordSyntax = *src->preferredRecordSyntax;
3031 dst->preferredRecordSyntax = NULL;
3032 dst->replaceIndicator = src->replaceIndicator;
3033 dst->smallSetUpperBound = src->smallSetUpperBound;
3034 dst->largeSetLowerBound = src->largeSetLowerBound;
3035 dst->mediumSetPresentNumber = src->mediumSetPresentNumber;
3040 tabs[0].tab = ir_set_method_tab;
3044 if (ir_tcl_method (interp, 0, NULL, tabs, NULL) == TCL_ERROR)
3052 * ir_set_obj_mk: IR Set Object creation
3054 static int ir_set_obj_mk (ClientData clientData, Tcl_Interp *interp,
3055 int argc, char **argv)
3058 ClientData parentData = 0;
3063 Tcl_CmdInfo parent_info;
3064 if (!Tcl_GetCommandInfo (interp, argv[2], &parent_info))
3066 Tcl_AppendResult (interp, "no object parent", NULL);
3067 return ir_tcl_error_exec (interp, argc, argv);
3069 parentData = parent_info.clientData;
3071 r = ir_set_obj_init (clientData, interp, argc, argv, &subData, parentData);
3074 Tcl_CreateCommand (interp, argv[1], ir_set_obj_method,
3075 subData, ir_set_obj_delete);
3079 IrTcl_Class ir_set_obj_class = {
3086 /* ------------------------------------------------------- */
3089 * do_scan: Perform scan
3091 static int do_scan (void *o, Tcl_Interp *interp, int argc, char **argv)
3095 IrTcl_ScanObj *obj = o;
3096 IrTcl_Obj *p = obj->parent;
3099 struct ccl_rpn_node *rpn;
3107 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
3108 " scanQuery\"", NULL);
3111 logf (LOG_DEBUG, "scan %s %s", *argv, argv[2]);
3112 if (!p->set_inher.num_databaseNames)
3114 Tcl_AppendResult (interp, "no databaseNames", NULL);
3115 return ir_tcl_error_exec (interp, argc, argv);
3119 Tcl_AppendResult (interp, "not connected", NULL);
3120 return ir_tcl_error_exec (interp, argc, argv);
3123 apdu = zget_APDU (p->odr_out, Z_APDU_scanRequest);
3124 req = apdu->u.scanRequest;
3126 set_referenceId (p->odr_out, &req->referenceId, p->set_inher.referenceId);
3127 req->num_databaseNames = p->set_inher.num_databaseNames;
3128 req->databaseNames = p->set_inher.databaseNames;
3131 if (!(req->termListAndStartPoint =
3132 p_query_scan (p->odr_out, p->protocol_type,
3133 &req->attributeSet, argv[2])))
3135 Tcl_AppendResult (interp, "query syntax error", NULL);
3136 return ir_tcl_error_exec (interp, argc, argv);
3139 rpn = ccl_find_str(p->bibset, argv[2], &r, &pos);
3142 Tcl_AppendResult (interp, "ccl syntax error ", ccl_err_msg(r), NULL);
3143 return ir_tcl_error_exec (interp, argc, argv);
3145 bib1.proto = p->protocol_type;
3146 bib1.oclass = CLASS_ATTSET;
3147 bib1.value = VAL_BIB1;
3149 req->attributeSet = oid_getoidbyent (&bib1);
3150 if (!(req->termListAndStartPoint = ccl_scan_query (rpn)))
3153 req->stepSize = &obj->stepSize;
3154 req->numberOfTermsRequested = &obj->numberOfTermsRequested;
3155 req->preferredPositionInResponse = &obj->preferredPositionInResponse;
3156 logf (LOG_DEBUG, "stepSize=%d", *req->stepSize);
3157 logf (LOG_DEBUG, "numberOfTermsRequested=%d",
3158 *req->numberOfTermsRequested);
3159 logf (LOG_DEBUG, "preferredPositionInResponse=%d",
3160 *req->preferredPositionInResponse);
3162 return ir_tcl_send_APDU (interp, p, apdu, "scan", *argv);
3166 * do_scanResponse: add scan response handler
3168 static int do_scanResponse (void *o, Tcl_Interp *interp,
3169 int argc, char **argv)
3171 IrTcl_ScanObj *obj = o;
3175 obj->scanResponse = NULL;
3178 else if (argc == -1)
3179 return ir_tcl_strdel (interp, &obj->scanResponse);
3182 xfree (obj->scanResponse);
3185 if (ir_tcl_strdup (interp, &obj->scanResponse, argv[2])
3190 obj->scanResponse = NULL;
3196 * do_stepSize: Set/get replace Step Size
3198 static int do_stepSize (void *obj, Tcl_Interp *interp,
3199 int argc, char **argv)
3201 IrTcl_ScanObj *p = obj;
3207 return ir_tcl_get_set_int (&p->stepSize, interp, argc, argv);
3211 * do_numberOfTermsRequested: Set/get Number of Terms requested
3213 static int do_numberOfTermsRequested (void *obj, Tcl_Interp *interp,
3214 int argc, char **argv)
3216 IrTcl_ScanObj *p = obj;
3220 p->numberOfTermsRequested = 20;
3223 return ir_tcl_get_set_int (&p->numberOfTermsRequested, interp, argc, argv);
3228 * do_preferredPositionInResponse: Set/get preferred Position
3230 static int do_preferredPositionInResponse (void *obj, Tcl_Interp *interp,
3231 int argc, char **argv)
3233 IrTcl_ScanObj *p = obj;
3237 p->preferredPositionInResponse = 1;
3240 return ir_tcl_get_set_int (&p->preferredPositionInResponse, interp,
3245 * do_scanStatus: Get scan status
3247 static int do_scanStatus (void *obj, Tcl_Interp *interp,
3248 int argc, char **argv)
3250 IrTcl_ScanObj *p = obj;
3254 return ir_tcl_get_set_int (&p->scanStatus, interp, argc, argv);
3258 * do_numberOfEntriesReturned: Get number of Entries returned
3260 static int do_numberOfEntriesReturned (void *obj, Tcl_Interp *interp,
3261 int argc, char **argv)
3263 IrTcl_ScanObj *p = obj;
3267 return ir_tcl_get_set_int (&p->numberOfEntriesReturned, interp,
3272 * do_positionOfTerm: Get position of Term
3274 static int do_positionOfTerm (void *obj, Tcl_Interp *interp,
3275 int argc, char **argv)
3277 IrTcl_ScanObj *p = obj;
3281 return ir_tcl_get_set_int (&p->positionOfTerm, interp, argc, argv);
3285 * do_scanLine: get Scan Line (surrogate or normal) after response
3287 static int do_scanLine (void *obj, Tcl_Interp *interp, int argc, char **argv)
3289 IrTcl_ScanObj *p = obj;
3295 p->entries_flag = 0;
3297 p->nonSurrogateDiagnosticNum = 0;
3298 p->nonSurrogateDiagnosticList = 0;
3301 else if (argc == -1)
3303 p->entries_flag = 0;
3304 /* release entries */
3307 ir_deleteDiags (&p->nonSurrogateDiagnosticList,
3308 &p->nonSurrogateDiagnosticNum);
3313 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
3314 " position\"", NULL);
3317 if (Tcl_GetInt (interp, argv[2], &i) == TCL_ERROR)
3319 if (!p->entries_flag || p->which != Z_ListEntries_entries || !p->entries
3320 || i >= p->num_entries || i < 0)
3322 switch (p->entries[i].which)
3324 case Z_Entry_termInfo:
3325 Tcl_AppendElement (interp, "T");
3326 if (p->entries[i].u.term.buf)
3327 Tcl_AppendElement (interp, p->entries[i].u.term.buf);
3329 Tcl_AppendElement (interp, "");
3330 sprintf (numstr, "%d", p->entries[i].u.term.globalOccurrences);
3331 Tcl_AppendElement (interp, numstr);
3333 case Z_Entry_surrogateDiagnostic:
3334 Tcl_AppendElement (interp, "SD");
3335 return ir_diagResult (interp, p->entries[i].u.diag.list,
3336 p->entries[i].u.diag.num);
3342 static IrTcl_Method ir_scan_method_tab[] = {
3343 { "scan", do_scan, NULL},
3344 { "scanResponse", do_scanResponse, NULL},
3345 { "stepSize", do_stepSize, NULL},
3346 { "numberOfTermsRequested", do_numberOfTermsRequested, NULL},
3347 { "preferredPositionInResponse", do_preferredPositionInResponse, NULL},
3348 { "scanStatus", do_scanStatus, NULL},
3349 { "numberOfEntriesReturned", do_numberOfEntriesReturned, NULL},
3350 { "positionOfTerm", do_positionOfTerm, NULL},
3351 { "scanLine", do_scanLine, NULL},
3356 * ir_scan_obj_method: IR Scan Object methods
3358 static int ir_scan_obj_method (ClientData clientData, Tcl_Interp *interp,
3359 int argc, char **argv)
3361 IrTcl_Methods tabs[2];
3366 Tcl_AppendResult (interp, wrongArgs, *argv, " method args...\"", NULL);
3369 tabs[0].tab = ir_scan_method_tab;
3370 tabs[0].obj = clientData;
3373 if (ir_tcl_method (interp, argc, argv, tabs, &r) == TCL_ERROR)
3374 return ir_tcl_method_error (interp, argc, argv, tabs);
3379 * ir_scan_obj_delete: IR Scan Object disposal
3381 static void ir_scan_obj_delete (ClientData clientData)
3383 IrTcl_Methods tabs[2];
3384 IrTcl_ScanObj *obj = clientData;
3386 tabs[0].tab = ir_scan_method_tab;
3390 ir_tcl_method (NULL, -1, NULL, tabs, NULL);
3395 * ir_scan_obj_mk: IR Scan Object creation
3397 static int ir_scan_obj_mk (ClientData clientData, Tcl_Interp *interp,
3398 int argc, char **argv)
3400 Tcl_CmdInfo parent_info;
3402 IrTcl_Methods tabs[2];
3406 Tcl_AppendResult (interp, wrongArgs, *argv,
3407 "objScanName objParentName\"", NULL);
3410 logf (LOG_DEBUG, "ir scan create %s", argv[1]);
3411 if (!Tcl_GetCommandInfo (interp, argv[2], &parent_info))
3413 Tcl_AppendResult (interp, "no object parent", NULL);
3414 return ir_tcl_error_exec (interp, argc, argv);
3416 obj = ir_tcl_malloc (sizeof(*obj));
3417 obj->parent = (IrTcl_Obj *) parent_info.clientData;
3419 tabs[0].tab = ir_scan_method_tab;
3423 if (ir_tcl_method (interp, 0, NULL, tabs, NULL) == TCL_ERROR)
3425 Tcl_CreateCommand (interp, argv[1], ir_scan_obj_method,
3426 (ClientData) obj, ir_scan_obj_delete);
3430 /* ------------------------------------------------------- */
3433 * ir_log_init_proc: set yaz log level
3435 static int ir_log_init_proc (ClientData clientData, Tcl_Interp *interp,
3436 int argc, char **argv)
3438 if (argc <= 1 || argc > 4)
3440 Tcl_AppendResult (interp, wrongArgs, *argv,
3441 " ?level ?prefix ?filename\"", NULL);
3445 log_init (log_mask_str (argv[1]), "", NULL);
3447 log_init (log_mask_str (argv[1]), argv[2], NULL);
3449 log_init (log_mask_str (argv[1]), argv[2], argv[3]);
3454 * ir_log_proc: log yaz message
3456 static int ir_log_proc (ClientData clientData, Tcl_Interp *interp,
3457 int argc, char **argv)
3462 Tcl_AppendResult (interp, wrongArgs, *argv,
3463 " level string\"", NULL);
3466 mask = log_mask_str_x (argv[1], 0);
3467 logf (mask, "%s", argv[1], mask, argv[2]);
3472 /* ------------------------------------------------------- */
3473 static void ir_initResponse (void *obj, Z_InitResponse *initrs)
3477 p->initResult = *initrs->result ? 1 : 0;
3478 if (!*initrs->result)
3479 logf (LOG_DEBUG, "Connection rejected by target");
3481 logf (LOG_DEBUG, "Connection accepted by target");
3483 get_referenceId (&p->set_inher.referenceId, initrs->referenceId);
3485 xfree (p->targetImplementationId);
3486 ir_tcl_strdup (p->interp, &p->targetImplementationId,
3487 initrs->implementationId);
3488 xfree (p->targetImplementationName);
3489 ir_tcl_strdup (p->interp, &p->targetImplementationName,
3490 initrs->implementationName);
3491 xfree (p->targetImplementationVersion);
3492 ir_tcl_strdup (p->interp, &p->targetImplementationVersion,
3493 initrs->implementationVersion);
3495 p->maximumRecordSize = *initrs->maximumRecordSize;
3496 p->preferredMessageSize = *initrs->preferredMessageSize;
3498 memcpy (&p->options, initrs->options, sizeof(initrs->options));
3499 memcpy (&p->protocolVersion, initrs->protocolVersion,
3500 sizeof(initrs->protocolVersion));
3501 xfree (p->userInformationField);
3502 p->userInformationField = NULL;
3503 if (initrs->userInformationField)
3507 if (initrs->userInformationField->which == ODR_EXTERNAL_octet &&
3508 (p->userInformationField =
3509 ir_tcl_malloc ((len =
3510 initrs->userInformationField->
3511 u.octet_aligned->len) +1)))
3513 memcpy (p->userInformationField,
3514 initrs->userInformationField->u.octet_aligned->buf,
3516 (p->userInformationField)[len] = '\0';
3521 static void ir_deleteDiags (IrTcl_Diagnostic **dst_list, int *dst_num)
3524 for (i = 0; i<*dst_num; i++)
3525 xfree (dst_list[i]->addinfo);
3531 static void ir_handleDiags (IrTcl_Diagnostic **dst_list, int *dst_num,
3532 Z_DiagRec **list, int num)
3538 *dst_list = ir_tcl_malloc (sizeof(**dst_list) * num);
3539 for (i = 0; i<num; i++)
3542 switch (list[i]->which)
3544 case Z_DiagRec_defaultFormat:
3545 (*dst_list)[i].condition = *list[i]->u.defaultFormat->condition;
3546 addinfo = list[i]->u.defaultFormat->addinfo;
3548 ((*dst_list)[i].addinfo = ir_tcl_malloc (strlen(addinfo)+1)))
3549 strcpy ((*dst_list)[i].addinfo, addinfo);
3550 cp = diagbib1_str ((*dst_list)[i].condition);
3551 logf (LOG_DEBUG, "Diag %d %s %s", (*dst_list)[i].condition,
3552 cp ? cp : "", addinfo ? addinfo : "");
3555 (*dst_list)[i].addinfo = NULL;
3556 (*dst_list)[i].condition = 0;
3561 static void ir_handleDBRecord (IrTcl_Obj *p, IrTcl_RecordList *rl,
3564 struct oident *ident;
3565 Z_ext_typeent *etype;
3567 rl->u.dbrec.size = oe->u.octet_aligned->len;
3568 rl->u.dbrec.buf = NULL;
3570 if ((ident = oid_getentbyoid (oe->direct_reference)))
3571 rl->u.dbrec.type = ident->value;
3573 rl->u.dbrec.type = VAL_USMARC;
3575 if (ident && (oe->which == Z_External_single ||
3576 oe->which == Z_External_octet)
3577 && (etype = z_ext_getentbyref (ident->value)))
3581 odr_setbuf (p->odr_in, (char*) oe->u.octet_aligned->buf,
3582 oe->u.octet_aligned->len, 0);
3583 if (!(*etype->fun)(p->odr_in, &rr, 0))
3585 switch (etype->what)
3587 case Z_External_sutrs:
3588 logf (LOG_DEBUG, "Z_External_sutrs");
3590 if ((rl->u.dbrec.buf = ir_tcl_malloc (oe->u.sutrs->len+1)))
3592 memcpy (rl->u.dbrec.buf, oe->u.sutrs->buf,
3594 rl->u.dbrec.buf[oe->u.sutrs->len] = '\0';
3596 rl->u.dbrec.size = oe->u.sutrs->len;
3598 case Z_External_grs1:
3599 logf (LOG_DEBUG, "Z_External_grs1");
3601 ir_tcl_grs_mk (oe->u.grs1, &rl->u.dbrec.u.grs1);
3603 case Z_External_explainRecord:
3604 logf (LOG_DEBUG, "Z_External_explainRecord");
3605 if ((rl->u.dbrec.buf = ir_tcl_malloc (rl->u.dbrec.size)))
3607 memcpy (rl->u.dbrec.buf, oe->u.octet_aligned->buf,
3615 if (oe->which == Z_External_octet && rl->u.dbrec.size > 0)
3617 char *buf = (char*) oe->u.octet_aligned->buf;
3618 if ((rl->u.dbrec.buf = ir_tcl_malloc (rl->u.dbrec.size)))
3619 memcpy (rl->u.dbrec.buf, buf, rl->u.dbrec.size);
3621 else if (rl->u.dbrec.type == VAL_SUTRS &&
3622 oe->which == Z_External_sutrs)
3624 if ((rl->u.dbrec.buf = ir_tcl_malloc (oe->u.sutrs->len+1)))
3626 memcpy (rl->u.dbrec.buf, oe->u.sutrs->buf,
3628 rl->u.dbrec.buf[oe->u.sutrs->len] = '\0';
3630 rl->u.dbrec.size = oe->u.sutrs->len;
3632 else if (rl->u.dbrec.type == VAL_GRS1 &&
3633 oe->which == Z_External_grs1)
3635 ir_tcl_grs_mk (oe->u.grs1, &rl->u.dbrec.u.grs1);
3640 static void ir_handleZRecords (void *o, Z_Records *zrs, IrTcl_SetObj *setobj,
3641 const char *elements)
3646 IrTcl_RecordList *rl;
3648 setobj->which = zrs->which;
3649 setobj->recordFlag = 1;
3651 ir_deleteDiags (&setobj->nonSurrogateDiagnosticList,
3652 &setobj->nonSurrogateDiagnosticNum);
3653 if (zrs->which == Z_Records_DBOSD)
3655 setobj->numberOfRecordsReturned =
3656 zrs->u.databaseOrSurDiagnostics->num_records;
3657 logf (LOG_DEBUG, "Got %d records", setobj->numberOfRecordsReturned);
3658 for (offset = 0; offset < setobj->numberOfRecordsReturned; offset++)
3660 Z_NamePlusRecord *znpr = zrs->u.databaseOrSurDiagnostics->
3663 rl = new_IR_record (setobj, setobj->start + offset, znpr->which,
3665 if (rl->which == Z_NamePlusRecord_surrogateDiagnostic)
3666 ir_handleDiags (&rl->u.surrogateDiagnostics.list,
3667 &rl->u.surrogateDiagnostics.num,
3668 &znpr->u.surrogateDiagnostic,
3671 ir_handleDBRecord (p, rl,
3672 (Z_External*) (znpr->u.databaseRecord));
3675 else if (zrs->which == Z_Records_multipleNSD)
3677 logf (LOG_DEBUG, "multipleNonSurrogateDiagnostic %d",
3678 zrs->u.multipleNonSurDiagnostics->num_diagRecs);
3679 setobj->numberOfRecordsReturned = 0;
3680 ir_handleDiags (&setobj->nonSurrogateDiagnosticList,
3681 &setobj->nonSurrogateDiagnosticNum,
3682 zrs->u.multipleNonSurDiagnostics->diagRecs,
3683 zrs->u.multipleNonSurDiagnostics->num_diagRecs);
3687 logf (LOG_DEBUG, "NonSurrogateDiagnostic");
3688 setobj->numberOfRecordsReturned = 0;
3689 ir_handleDiags (&setobj->nonSurrogateDiagnosticList,
3690 &setobj->nonSurrogateDiagnosticNum,
3691 &zrs->u.nonSurrogateDiagnostic,
3696 static void ir_searchResponse (void *o, Z_SearchResponse *searchrs,
3697 IrTcl_SetObj *setobj)
3699 Z_Records *zrs = searchrs->records;
3701 logf (LOG_DEBUG, "Received search response");
3704 logf (LOG_DEBUG, "Search response, no object!");
3707 setobj->searchStatus = searchrs->searchStatus ? 1 : 0;
3708 get_referenceId (&setobj->set_inher.referenceId, searchrs->referenceId);
3709 setobj->resultCount = *searchrs->resultCount;
3710 if (searchrs->presentStatus)
3711 setobj->presentStatus = *searchrs->presentStatus;
3712 if (searchrs->nextResultSetPosition)
3713 setobj->nextResultSetPosition = *searchrs->nextResultSetPosition;
3715 logf (LOG_DEBUG, "status %d hits %d",
3716 setobj->searchStatus, setobj->resultCount);
3720 if (setobj->resultCount <= setobj->set_inher.smallSetUpperBound)
3721 es = setobj->set_inher.smallSetElementSetNames;
3723 es = setobj->set_inher.mediumSetElementSetNames;
3724 ir_handleZRecords (o, zrs, setobj, es);
3727 setobj->recordFlag = 0;
3731 static void ir_presentResponse (void *o, Z_PresentResponse *presrs,
3732 IrTcl_SetObj *setobj)
3734 Z_Records *zrs = presrs->records;
3736 logf (LOG_DEBUG, "Received present response");
3739 logf (LOG_DEBUG, "Present response, no object!");
3742 setobj->presentStatus = *presrs->presentStatus;
3743 get_referenceId (&setobj->set_inher.referenceId, presrs->referenceId);
3744 setobj->nextResultSetPosition = *presrs->nextResultSetPosition;
3746 ir_handleZRecords (o, zrs, setobj, setobj->set_inher.elementSetNames);
3749 setobj->recordFlag = 0;
3750 logf (LOG_DEBUG, "No records!");
3754 static void ir_scanResponse (void *o, Z_ScanResponse *scanrs,
3755 IrTcl_ScanObj *scanobj)
3759 logf (LOG_DEBUG, "Received scanResponse");
3761 get_referenceId (&p->set_inher.referenceId, scanrs->referenceId);
3762 scanobj->scanStatus = *scanrs->scanStatus;
3763 logf (LOG_DEBUG, "scanStatus=%d", scanobj->scanStatus);
3765 if (scanrs->stepSize)
3766 scanobj->stepSize = *scanrs->stepSize;
3767 logf (LOG_DEBUG, "stepSize=%d", scanobj->stepSize);
3769 scanobj->numberOfEntriesReturned = *scanrs->numberOfEntriesReturned;
3770 logf (LOG_DEBUG, "numberOfEntriesReturned=%d",
3771 scanobj->numberOfEntriesReturned);
3773 if (scanrs->positionOfTerm)
3774 scanobj->positionOfTerm = *scanrs->positionOfTerm;
3776 scanobj->positionOfTerm = -1;
3777 logf (LOG_DEBUG, "positionOfTerm=%d", scanobj->positionOfTerm);
3779 xfree (scanobj->entries);
3780 scanobj->entries = NULL;
3782 ir_deleteDiags (&scanobj->nonSurrogateDiagnosticList,
3783 &scanobj->nonSurrogateDiagnosticNum);
3784 if (scanrs->entries)
3789 scanobj->entries_flag = 1;
3790 scanobj->which = scanrs->entries->which;
3791 switch (scanobj->which)
3793 case Z_ListEntries_entries:
3794 scanobj->num_entries = scanrs->entries->u.entries->num_entries;
3795 scanobj->entries = ir_tcl_malloc (scanobj->num_entries *
3796 sizeof(*scanobj->entries));
3797 for (i=0; i<scanobj->num_entries; i++)
3799 ze = scanrs->entries->u.entries->entries[i];
3800 scanobj->entries[i].which = ze->which;
3803 case Z_Entry_termInfo:
3804 if (ze->u.termInfo->term->which == Z_Term_general)
3806 int l = ze->u.termInfo->term->u.general->len;
3807 scanobj->entries[i].u.term.buf = ir_tcl_malloc (1+l);
3808 memcpy (scanobj->entries[i].u.term.buf,
3809 ze->u.termInfo->term->u.general->buf,
3811 scanobj->entries[i].u.term.buf[l] = '\0';
3814 scanobj->entries[i].u.term.buf = NULL;
3815 if (ze->u.termInfo->globalOccurrences)
3816 scanobj->entries[i].u.term.globalOccurrences =
3817 *ze->u.termInfo->globalOccurrences;
3819 scanobj->entries[i].u.term.globalOccurrences = 0;
3821 case Z_Entry_surrogateDiagnostic:
3822 ir_handleDiags (&scanobj->entries[i].u.diag.list,
3823 &scanobj->entries[i].u.diag.num,
3824 &ze->u.surrogateDiagnostic,
3830 case Z_ListEntries_nonSurrogateDiagnostics:
3831 ir_handleDiags (&scanobj->nonSurrogateDiagnosticList,
3832 &scanobj->nonSurrogateDiagnosticNum,
3833 scanrs->entries->u.nonSurrogateDiagnostics->
3835 scanrs->entries->u.nonSurrogateDiagnostics->
3841 scanobj->entries_flag = 0;
3845 * ir_select_read: handle incoming packages
3847 static void ir_select_read (ClientData clientData)
3849 IrTcl_Obj *p = clientData;
3854 Tcl_CmdInfo cmd_info;
3855 const char *apdu_call;
3857 logf(LOG_DEBUG, "Read handler fd=%d", cs_fileno(p->cs_link));
3858 if (p->state == IR_TCL_R_Connecting)
3860 logf(LOG_DEBUG, "read: connect");
3861 r = cs_rcvconnect (p->cs_link);
3864 logf (LOG_WARN, "cs_rcvconnect returned 1");
3867 p->state = IR_TCL_R_Idle;
3869 ir_select_remove_write (cs_fileno (p->cs_link), p);
3872 logf (LOG_DEBUG, "cs_rcvconnect error");
3873 ir_tcl_disconnect (p);
3876 p->failInfo = IR_TCL_FAIL_CONNECT;
3877 ir_tcl_eval (p->interp, p->failback);
3883 ir_tcl_eval (p->interp, p->callback);
3884 if (p->ref_count == 2 && p->cs_link && p->request_queue
3885 && p->state == IR_TCL_R_Idle)
3886 ir_tcl_send_q (p, p->request_queue, "x");
3892 p->state = IR_TCL_R_Reading;
3894 /* read incoming APDU */
3895 if ((r=cs_get (p->cs_link, &p->buf_in, &p->len_in)) == 1)
3897 logf(LOG_DEBUG, "PDU Fraction read");
3900 /* signal one more use of ir object - callbacks must not
3901 release the ir memory (p pointer) */
3905 logf (LOG_DEBUG, "cs_get failed, code %d", r);
3906 ir_select_remove (cs_fileno (p->cs_link), p);
3907 ir_tcl_disconnect (p);
3910 p->failInfo = IR_TCL_FAIL_READ;
3911 ir_tcl_eval (p->interp, p->failback);
3913 /* release ir object now if callback deleted it */
3917 /* got complete APDU. Now decode */
3920 odr_setbuf (p->odr_in, p->buf_in, r, 0);
3921 logf (LOG_DEBUG, "cs_get ok, total size %d", r);
3922 if (!z_APDU (p->odr_in, &apdu, 0))
3924 logf (LOG_DEBUG, "cs_get failed: %s",
3925 odr_errmsg (odr_geterror (p->odr_in)));
3926 ir_tcl_disconnect (p);
3929 p->failInfo = IR_TCL_FAIL_IN_APDU;
3930 p->apduOffset = odr_offset (p->odr_in);
3931 ir_tcl_eval (p->interp, p->failback);
3933 /* release ir object now if failback deleted it */
3937 /* handle APDU and invoke callback */
3938 rq = p->request_queue;
3941 logf (LOG_FATAL, "Internal error. No queue entry");
3944 object_name = rq->object_name;
3945 logf (LOG_DEBUG, "Object %s", object_name);
3947 if (Tcl_GetCommandInfo (p->interp, object_name, &cmd_info))
3951 case Z_APDU_initResponse:
3952 p->eventType = "init";
3953 ir_initResponse (p, apdu->u.initResponse);
3954 apdu_call = p->initResponse;
3956 case Z_APDU_searchResponse:
3957 p->eventType = "search";
3958 ir_searchResponse (p, apdu->u.searchResponse,
3959 (IrTcl_SetObj *) cmd_info.clientData);
3960 apdu_call = ((IrTcl_SetObj *)
3961 cmd_info.clientData)->searchResponse;
3963 case Z_APDU_presentResponse:
3964 p->eventType = "present";
3965 ir_presentResponse (p, apdu->u.presentResponse,
3966 (IrTcl_SetObj *) cmd_info.clientData);
3967 apdu_call = ((IrTcl_SetObj *)
3968 cmd_info.clientData)->presentResponse;
3970 case Z_APDU_scanResponse:
3971 p->eventType = "scan";
3972 ir_scanResponse (p, apdu->u.scanResponse,
3973 (IrTcl_ScanObj *) cmd_info.clientData);
3974 apdu_call = ((IrTcl_ScanObj *)
3975 cmd_info.clientData)->scanResponse;
3978 logf (LOG_WARN, "Received unknown APDU type (%d)",
3980 ir_tcl_disconnect (p);
3983 p->failInfo = IR_TCL_FAIL_UNKNOWN_APDU;
3984 ir_tcl_eval (p->interp, p->failback);
3989 p->request_queue = rq->next;
3990 p->state = IR_TCL_R_Idle;
3993 ir_tcl_eval (p->interp, apdu_call);
3994 else if (rq->callback)
3995 ir_tcl_eval (p->interp, rq->callback);
3996 xfree (rq->buf_out);
3997 xfree (rq->callback);
3998 xfree (rq->object_name);
4000 odr_reset (p->odr_in);
4001 if (p->ref_count == 1)
4007 } while (p->cs_link && cs_more (p->cs_link));
4008 if (p->cs_link && p->request_queue && p->state == IR_TCL_R_Idle)
4009 ir_tcl_send_q (p, p->request_queue, "x");
4013 * ir_select_write: handle outgoing packages - not yet written.
4015 static int ir_select_write (ClientData clientData)
4017 IrTcl_Obj *p = clientData;
4021 logf (LOG_DEBUG, "Write handler fd=%d", cs_fileno(p->cs_link));
4022 if (p->state == IR_TCL_R_Connecting)
4024 logf(LOG_DEBUG, "write: connect");
4025 r = cs_rcvconnect (p->cs_link);
4028 logf (LOG_DEBUG, "cs_rcvconnect returned 1");
4031 p->state = IR_TCL_R_Idle;
4033 ir_select_remove_write (cs_fileno (p->cs_link), p);
4036 logf (LOG_DEBUG, "cs_rcvconnect error");
4037 ir_tcl_disconnect (p);
4040 p->failInfo = IR_TCL_FAIL_CONNECT;
4041 ir_tcl_eval (p->interp, p->failback);
4047 ir_tcl_eval (p->interp, p->callback);
4051 rq = p->request_queue;
4052 if (!rq || !rq->buf_out)
4055 if ((r=cs_put (p->cs_link, rq->buf_out, rq->len_out)) < 0)
4057 logf (LOG_DEBUG, "cs_put write fail");
4059 xfree (rq->buf_out);
4061 ir_tcl_disconnect (p);
4064 p->failInfo = IR_TCL_FAIL_WRITE;
4065 ir_tcl_eval (p->interp, p->failback);
4069 else if (r == 0) /* remove select bit */
4071 logf (LOG_DEBUG, "Write completed");
4072 p->state = IR_TCL_R_Waiting;
4073 ir_select_remove_write (cs_fileno (p->cs_link), p);
4074 xfree (rq->buf_out);
4080 static void ir_select_notify (ClientData clientData, int r, int w, int e)
4084 if (!ir_select_write (clientData) && r)
4085 ir_select_read (clientData);
4089 ir_select_read (clientData);
4093 /*----------------------------------------------------------- */
4097 * This wrapper function is used by Windows to invoke the
4098 * initialization code for the DLL. If we are compiling
4099 * with Visual C++, this routine will be renamed to DllMain.
4111 DllEntryPoint(hInst, reason, reserved)
4112 HINSTANCE hInst; /* Library instance handle. */
4113 DWORD reason; /* Reason this function is being called. */
4114 LPVOID reserved; /* Not used. */
4120 /* ------------------------------------------------------- */
4122 * Irtcl_init: Registration of TCL commands.
4124 EXPORT (int,Irtcl_Init) (Tcl_Interp *interp)
4126 Tcl_CreateCommand (interp, "ir", ir_obj_mk, (ClientData) NULL,
4127 (Tcl_CmdDeleteProc *) NULL);
4128 Tcl_CreateCommand (interp, "ir-set", ir_set_obj_mk,
4129 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
4130 Tcl_CreateCommand (interp, "ir-scan", ir_scan_obj_mk,
4131 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
4132 Tcl_CreateCommand (interp, "ir-log-init", ir_log_init_proc,
4133 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
4134 Tcl_CreateCommand (interp, "ir-log", ir_log_proc,
4135 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);