2 * IR toolkit for tcl/tk
3 * (c) Index Data 1995-1997
4 * See the file LICENSE for details.
5 * Sebastian Hammer, Adam Dickmeiss
8 * Revision 1.101 1997-09-09 10:19:53 adam
9 * New MSV5.0 port with fewer warnings.
11 * Revision 1.100 1997/05/01 15:04:05 adam
12 * Added ir-log command.
14 * Revision 1.99 1997/04/30 07:24:47 adam
15 * Spell fix of an error message.
17 * Revision 1.98 1997/04/13 18:57:20 adam
18 * Better error reporting and aligned with Tcl/Tk style.
19 * Rework of notifier code with Tcl_File handles.
21 * Revision 1.97 1996/11/14 17:11:07 adam
22 * Added Explain documentaion.
24 * Revision 1.96 1996/10/08 13:02:50 adam
25 * When dealing with records, odr_choice_enable_bias function is used to
26 * prevent decoding of externals.
28 * Revision 1.95 1996/09/13 10:51:49 adam
29 * Bug fix: ir_tcl_select_set called Tcl_GetFile at disconnect.
31 * Revision 1.94 1996/08/21 13:32:53 adam
32 * Implemented saveFile method and extended loadFile method to work with it.
34 * Revision 1.93 1996/08/16 15:07:45 adam
35 * First work on Explain.
37 * Revision 1.92 1996/08/09 15:33:07 adam
38 * Modified the code to use tk4.1/tcl7.5 patch level 1. The time-driven
39 * polling is no longer activated on Windows since asynchrounous I/O works
42 * Revision 1.91 1996/07/03 13:31:11 adam
43 * The xmalloc/xfree functions from YAZ are used to manage memory.
45 * Revision 1.90 1996/06/27 14:21:00 adam
46 * Yet another Windows port.
48 * Revision 1.89 1996/06/11 15:27:15 adam
49 * Event type set to connect a little earlier in the do_connect function.
51 * Revision 1.88 1996/06/03 09:04:22 adam
52 * Changed a few logf calls.
54 * Revision 1.87 1996/05/29 06:37:51 adam
55 * Function ir_tcl_get_grs_r enhanced so that specific elements can be
58 * Revision 1.86 1996/03/20 13:54:04 adam
59 * The Tcl_File structure is only manipulated in the Tk-event interface
62 * Revision 1.85 1996/03/15 11:15:48 adam
63 * Modified to use new prototypes for p_query_rpn and p_query_scan.
65 * Revision 1.84 1996/03/07 12:42:49 adam
66 * Better logging when callback is invoked.
68 * Revision 1.83 1996/03/05 09:21:09 adam
69 * Bug fix: memory used by GRS records wasn't freed.
70 * Rewrote some of the error handling code - the connection is always
71 * closed before failback is called.
72 * If failback is defined the send APDU methods (init, search, ...) will
73 * return OK but invoke failback (as is the case if the write operation
75 * Bug fix: ref_count in assoc object could grow if fraction of PDU was
78 * Revision 1.82 1996/02/29 15:30:21 adam
79 * Export of IrTcl functionality to extensions.
81 * Revision 1.81 1996/02/26 18:38:32 adam
82 * Work on export of set methods.
84 * Revision 1.80 1996/02/23 17:31:39 adam
85 * More functions made available to the wais tcl extension.
87 * Revision 1.79 1996/02/23 13:41:38 adam
88 * Work on public access to simple ir class system.
90 * Revision 1.78 1996/02/21 10:16:08 adam
91 * Simplified select handling. Only one function ir_tcl_select_set has
92 * to be externally defined.
94 * Revision 1.77 1996/02/20 17:52:58 adam
95 * Uses the YAZ oid system to name record syntax object identifiers.
97 * Revision 1.76 1996/02/20 16:09:51 adam
98 * Bug fix: didn't set element set names stamp correctly on result
99 * set records when element set names were set to the empty string.
101 * Revision 1.75 1996/02/19 15:41:53 adam
102 * Better log messages.
103 * Minor improvement of connect method.
105 * Revision 1.74 1996/02/05 17:58:03 adam
106 * Ported ir-tcl to use the beta releases of tcl7.5/tk4.1.
108 * Revision 1.73 1996/01/29 11:35:19 adam
109 * Bug fix: cs_type member renamed to comstackType to avoid conflict with
110 * cs_type macro defined by YAZ.
112 * Revision 1.72 1996/01/19 17:45:34 quinn
113 * Added debugging output
115 * Revision 1.71 1996/01/19 16:22:38 adam
116 * New method: apduDump - returns information about last incoming APDU.
118 * Revision 1.70 1996/01/10 09:18:34 adam
119 * PDU specific callbacks implemented: initRespnse, searchResponse,
120 * presentResponse and scanResponse.
121 * Bug fix in the command line shell (tclmain.c) - discovered on OSF/1.
123 * Revision 1.69 1996/01/04 16:12:12 adam
124 * Setting PDUType renamed to eventType.
126 * Revision 1.68 1996/01/04 11:05:22 adam
127 * New setting: PDUType - returns type of last PDU returned from the target.
128 * Fixed a bug in configure/Makefile.
130 * Revision 1.67 1996/01/03 09:00:51 adam
131 * Updated to use new version of Yaz (names changed to avoid C++ conflict).
133 * Revision 1.66 1995/11/28 17:26:39 adam
134 * Removed Carriage return from ir-tcl.c!
135 * Removed misc. debug logs.
137 * Revision 1.65 1995/11/28 13:53:00 quinn
140 * Revision 1.64 1995/11/13 15:39:18 adam
141 * Bug fix: {small,medium}SetElementSetNames weren't set correctly.
142 * Bug fix: idAuthentication weren't set correctly.
144 * Revision 1.63 1995/11/13 09:55:39 adam
145 * Multiple records at a position in a result-set with differnt
148 * Revision 1.62 1995/10/18 17:20:33 adam
149 * Work on target setup in client.tcl.
151 * Revision 1.61 1995/10/18 16:42:42 adam
152 * New settings: smallSetElementSetNames and mediumSetElementSetNames.
154 * Revision 1.60 1995/10/18 15:43:31 adam
155 * In search: mediumSetElementSetNames and smallSetElementSetNames are
156 * set to elementSetNames.
158 * Revision 1.59 1995/10/17 12:18:58 adam
159 * Bug fix: when target connection closed, the connection was not
160 * properly reestablished.
162 * Revision 1.58 1995/10/16 17:00:55 adam
163 * New setting: elementSetNames.
164 * Various client improvements. Medium presentation format looks better.
166 * Revision 1.57 1995/09/21 13:11:51 adam
167 * Support of dynamic loading.
168 * Test script uses load command if necessary.
170 * Revision 1.56 1995/08/29 15:30:14 adam
171 * Work on GRS records.
173 * Revision 1.55 1995/08/28 09:43:25 adam
174 * Minor changes. configure only searches for yaz beta 3 and versions after
177 * Revision 1.54 1995/08/24 12:25:16 adam
178 * Modified to work with yaz 1.0b3.
180 * Revision 1.53 1995/08/04 12:49:26 adam
181 * Bug fix: reading uninitialized variable p.
183 * Revision 1.52 1995/08/04 11:32:38 adam
184 * More work on output queue. Memory related routines moved
187 * Revision 1.51 1995/08/03 13:22:54 adam
190 * Revision 1.50 1995/07/20 08:09:49 adam
191 * client.tcl: Targets removed from hotTargets list when targets
192 * are removed/modified.
193 * ir-tcl.c: More work on triggerResourceControl.
195 * Revision 1.49 1995/06/30 12:39:21 adam
196 * Bug fix: loadFile didn't set record type.
197 * The MARC routines are a little less strict in the interpretation.
198 * Script display.tcl replaces the old marc.tcl.
199 * New interactive script: shell.tcl.
201 * Revision 1.48 1995/06/27 19:03:50 adam
202 * Bug fix in do_present in ir-tcl.c: p->set_child member weren't set.
203 * nextResultSetPosition used instead of setOffset.
205 * Revision 1.47 1995/06/25 10:25:04 adam
206 * Working on triggerResourceControl. Description of compile/install
207 * procedure moved to ir-tcl.sgml.
209 * Revision 1.46 1995/06/22 13:15:06 adam
210 * Feature: SUTRS. Setting getSutrs implemented.
211 * Work on display formats.
212 * Preferred record syntax can be set by the user.
214 * Revision 1.45 1995/06/20 08:07:30 adam
215 * New setting: failInfo.
216 * Working on better cancel mechanism.
218 * Revision 1.44 1995/06/19 17:01:20 adam
221 * Revision 1.43 1995/06/19 13:06:08 adam
222 * New define: IR_TCL_VERSION.
224 * Revision 1.42 1995/06/19 08:08:52 adam
225 * client.tcl: hotTargets now contain both database and target name.
226 * ir-tcl.c: setting protocol edited. Errors in callbacks are logged
227 * by logf(LOG_WARN, ...) calls.
229 * Revision 1.41 1995/06/16 12:28:16 adam
230 * Implemented preferredRecordSyntax.
231 * Minor changes in diagnostic handling.
232 * Record list deleted when connection closes.
234 * Revision 1.40 1995/06/14 13:37:18 adam
235 * Setting recordType implemented.
236 * Setting implementationVersion implemented.
237 * Settings implementationId / implementationName edited.
239 * Revision 1.39 1995/06/08 10:26:32 adam
240 * Bug fix in ir_strdup.
242 * Revision 1.38 1995/06/01 16:36:47 adam
243 * About buttons. Minor bug fixes.
245 * Revision 1.37 1995/06/01 07:31:20 adam
246 * Rename of many typedefs -> IrTcl_...
248 * Revision 1.36 1995/05/31 13:09:59 adam
249 * Client searches/presents may be interrupted.
250 * New moving book-logo.
252 * Revision 1.35 1995/05/31 08:36:33 adam
253 * Bug fix in client.tcl: didn't save options on clientrc.tcl.
254 * New method: referenceId. More work on scan.
256 * Revision 1.34 1995/05/29 10:33:42 adam
257 * README and rename of startup script.
259 * Revision 1.33 1995/05/29 09:15:11 quinn
260 * Changed CS_SR to PROTO_SR, etc.
262 * Revision 1.32 1995/05/29 08:44:16 adam
263 * Work on delete of objects.
265 * Revision 1.31 1995/05/26 11:44:10 adam
266 * Bugs fixed. More work on MARC utilities and queries. Test
267 * client is up-to-date again.
269 * Revision 1.30 1995/05/26 08:54:11 adam
270 * New MARC utilities. Uses prefix query.
272 * Revision 1.29 1995/05/24 14:10:22 adam
273 * Work on idAuthentication, protocolVersion and options.
275 * Revision 1.28 1995/05/23 15:34:48 adam
276 * Many new settings, userInformationField, smallSetUpperBound, etc.
277 * A number of settings are inherited when ir-set is executed.
278 * This version is incompatible with the graphical test client (client.tcl).
280 * Revision 1.27 1995/05/11 15:34:47 adam
281 * Scan request changed a bit. This version works with RLG.
283 * Revision 1.26 1995/04/18 16:11:51 adam
284 * First version of graphical Scan. Some work on query-by-form.
286 * Revision 1.25 1995/04/17 09:37:17 adam
287 * Further development of scan.
289 * Revision 1.24 1995/04/11 14:16:42 adam
290 * Further work on scan. Response works. Entries aren't saved yet.
292 * Revision 1.23 1995/04/10 10:50:27 adam
293 * Result-set name defaults to suffix of ir-set name.
294 * Started working on scan. Not finished at this point.
296 * Revision 1.22 1995/03/31 10:43:03 adam
297 * More robust when getting bad MARC records.
299 * Revision 1.21 1995/03/31 08:56:37 adam
300 * New button "Search".
302 * Revision 1.20 1995/03/29 16:07:09 adam
303 * Bug fix: Didn't use setName in present request.
305 * Revision 1.19 1995/03/28 12:45:23 adam
306 * New ir method failback: called on disconnect/protocol error.
307 * New ir set/get method: protocol: SR / Z3950.
308 * Simple popup and disconnect when failback is invoked.
310 * Revision 1.18 1995/03/21 15:50:12 adam
313 * Revision 1.17 1995/03/21 13:41:03 adam
314 * Comstack cs_create not used too often. Non-blocking connect.
316 * Revision 1.16 1995/03/21 08:26:06 adam
317 * New method, setName, to specify the result set name (other than Default).
318 * New method, responseStatus, which returns diagnostic info, if any, after
319 * present response / search response.
321 * Revision 1.15 1995/03/20 15:24:07 adam
322 * Diagnostic records saved on searchResponse.
324 * Revision 1.14 1995/03/20 08:53:22 adam
325 * Event loop in tclmain.c rewritten. New method searchStatus.
327 * Revision 1.13 1995/03/17 18:26:17 adam
328 * Non-blocking i/o used now. Database names popup as cascade items.
330 * Revision 1.12 1995/03/17 15:45:00 adam
331 * Improved target/database setup.
333 * Revision 1.11 1995/03/16 17:54:03 adam
334 * Minor changes really.
336 * Revision 1.10 1995/03/15 16:14:50 adam
337 * Blocking arg in cs_create changed.
339 * Revision 1.9 1995/03/15 13:59:24 adam
342 * Revision 1.8 1995/03/15 08:25:16 adam
343 * New method presentStatus to check for error on present. Misc. cleanup
344 * of IrTcl_RecordList manipulations. Full MARC record presentation in
347 * Revision 1.7 1995/03/14 17:32:29 adam
348 * Presentation of full Marc record in popup window.
350 * Revision 1.6 1995/03/12 19:31:55 adam
351 * Pattern matching implemented when retrieving MARC records. More
352 * diagnostic functions.
354 * Revision 1.5 1995/03/10 18:00:15 adam
355 * Actual presentation in line-by-line format. RPN query support.
357 * Revision 1.4 1995/03/09 16:15:08 adam
358 * First presentRequest attempts. Hot-target list.
376 #if defined(__WIN32__)
377 # define WIN32_LEAN_AND_MEAN
378 # include <windows.h>
379 # undef WIN32_LEAN_AND_MEAN
382 * VC++ has an alternate entry point called DllMain, so we need to rename
386 # if defined(_MSC_VER)
387 # define EXPORT(a,b) __declspec(dllexport) a b
388 # define DllEntryPoint DllMain
390 # if defined(__BORLANDC__)
391 # define EXPORT(a,b) a _export b
393 # define EXPORT(a,b) a b
397 # define EXPORT(a,b) a b
400 static char *wrongArgs = "wrong # args: should be \"";
402 static int ir_tcl_error_exec (Tcl_Interp *interp, int argc, char **argv)
405 Tcl_AppendResult (interp, " while executing ", NULL);
406 for (i = 0; i<argc; i++)
407 Tcl_AppendResult (interp, (i ? " " : "\""), argv[i], NULL);
408 Tcl_AppendResult (interp, "\"", NULL);
413 static void ir_deleteDiags (IrTcl_Diagnostic **dst_list, int *dst_num);
415 static void ir_select_notify (ClientData clientData, int r, int w, int e);
417 void ir_select_add (int fd, void *obj)
419 ir_tcl_select_set (ir_select_notify, fd, obj, 1, 0, 0);
422 void ir_select_add_write (int fd, void *obj)
424 ir_tcl_select_set (ir_select_notify, fd, obj, 1, 1, 0);
427 void ir_select_remove (int fd, void *obj)
429 ir_tcl_select_set (NULL, fd, obj, 0, 0, 0);
432 void ir_select_remove_write (int fd, void *obj)
434 ir_tcl_select_set (ir_select_notify, fd, obj, 1, 0, 0);
437 static void delete_IR_record (IrTcl_RecordList *rl)
441 case Z_NamePlusRecord_databaseRecord:
442 switch (rl->u.dbrec.type)
445 ir_tcl_grs_del (&rl->u.dbrec.u.grs1);
450 xfree (rl->u.dbrec.buf);
451 rl->u.dbrec.buf = NULL;
453 case Z_NamePlusRecord_surrogateDiagnostic:
454 ir_deleteDiags (&rl->u.surrogateDiagnostics.list,
455 &rl->u.surrogateDiagnostics.num);
458 xfree (rl->elements);
461 static IrTcl_RecordList *new_IR_record (IrTcl_SetObj *setobj,
463 const char *elements)
465 IrTcl_RecordList *rl;
467 if (elements && !*elements)
469 for (rl = setobj->record_list; rl; rl = rl->next)
471 if (no == rl->no && (!rl->elements || !elements ||
472 !strcmp(elements, rl->elements)))
474 delete_IR_record (rl);
480 rl = ir_tcl_malloc (sizeof(*rl));
481 rl->next = setobj->record_list;
483 setobj->record_list = rl;
486 ir_tcl_strdup (NULL, &rl->elements, elements);
493 int ir_tcl_eval (Tcl_Interp *interp, const char *command)
495 char *tmp = ir_tcl_malloc (strlen(command)+1);
498 logf (LOG_DEBUG, "Invoking %.23s ...", command);
499 strcpy (tmp, command);
500 r = Tcl_Eval (interp, tmp);
503 logf (LOG_WARN, "Tcl error in line %d: %s", interp->errorLine,
506 Tcl_FreeResult (interp);
512 * IrTcl_getRecordSyntaxStr: Return record syntax name of object id
514 static char *IrTcl_getRecordSyntaxStr (enum oid_value value)
517 struct oident ent, *entp;
519 ent.proto = PROTO_Z3950;
520 ent.oclass = CLASS_RECSYN;
523 o = oid_getoidbyent (&ent);
524 entp = oid_getentbyoid (o);
532 * IrTcl_getRecordSyntaxVal: Return record syntax value of string
534 static enum oid_value IrTcl_getRecordSyntaxVal (const char *name)
536 return oid_getvalbyname (name);
539 static IrTcl_RecordList *find_IR_record (IrTcl_SetObj *setobj, int no)
541 IrTcl_RecordList *rl;
543 for (rl = setobj->record_list; rl; rl = rl->next)
545 (!setobj->recordElements || !rl->elements ||
546 !strcmp (setobj->recordElements, rl->elements)))
551 static void delete_IR_records (IrTcl_SetObj *setobj)
553 IrTcl_RecordList *rl, *rl1;
555 for (rl = setobj->record_list; rl; rl = rl1)
557 delete_IR_record (rl);
561 setobj->record_list = NULL;
565 * ir_tcl_get_set_int: Set/get integer value
567 int ir_tcl_get_set_int (int *val, Tcl_Interp *interp, int argc, char **argv)
573 if (Tcl_GetInt (interp, argv[2], val)==TCL_ERROR)
576 sprintf (buf, "%d", *val);
577 Tcl_AppendResult (interp, buf, NULL);
583 * ir_tcl_method_error
585 int ir_tcl_method_error (Tcl_Interp *interp, int argc, char **argv,
588 IrTcl_Methods *tab_i = tab;
591 Tcl_AppendResult (interp, "bad method: \"", *argv, " ", argv[1],
592 "\"\nmethod should be of:", NULL);
593 for (tab_i = tab; tab_i->tab; tab_i++)
594 for (t = tab_i->tab; t->name; t++)
595 Tcl_AppendResult (interp, " ", t->name, NULL);
600 * ir_tcl_method: Search for method in table and invoke method handler
602 int ir_tcl_method (Tcl_Interp *interp, int argc, char **argv,
603 IrTcl_Methods *tab, int *ret)
605 IrTcl_Methods *tab_i = tab;
608 for (tab_i = tab; tab_i->tab; tab_i++)
609 for (t = tab_i->tab; t->name; t++)
612 if ((*t->method)(tab_i->obj, interp, argc, argv) == TCL_ERROR)
616 if (!strcmp (t->name, argv[1]))
618 *ret = (*t->method)(tab_i->obj, interp, argc, argv);
629 * ir_tcl_named_bits: get/set named bits
631 int ir_tcl_named_bits (struct ir_named_entry *tab, Odr_bitmask *ob,
632 Tcl_Interp *interp, int argc, char **argv)
634 struct ir_named_entry *ti;
639 for (no = 0; no < argc; no++)
641 for (ti = tab; ti->name; ti++)
642 if (!strcmp (argv[no], ti->name))
644 ODR_MASK_SET (ob, ti->pos);
649 Tcl_AppendResult (interp, "bad bit mask ", argv[no], NULL);
650 return ir_tcl_error_exec (interp, argc, argv);
655 for (ti = tab; ti->name; ti++)
656 if (ODR_MASK_GET (ob, ti->pos))
657 Tcl_AppendElement (interp, ti->name);
661 static void set_referenceId (ODR o, Z_ReferenceId **dst, const char *src)
667 *dst = odr_malloc (o, sizeof(**dst));
668 (*dst)->size = (*dst)->len = strlen(src);
669 (*dst)->buf = odr_malloc (o, (*dst)->len);
670 memcpy ((*dst)->buf, src, (*dst)->len);
674 static void get_referenceId (char **dst, Z_ReferenceId *src)
682 *dst = ir_tcl_malloc (src->len+1);
683 memcpy (*dst, src->buf, src->len);
684 (*dst)[src->len] = '\0';
687 /* ------------------------------------------------------- */
690 * do_init_request: init method on IR object
692 static int do_init_request (void *obj, Tcl_Interp *interp,
693 int argc, char **argv)
701 logf (LOG_DEBUG, "init %s", *argv);
704 Tcl_AppendResult (interp, "not connected", NULL);
705 return ir_tcl_error_exec (interp, argc, argv);
707 apdu = zget_APDU (p->odr_out, Z_APDU_initRequest);
708 req = apdu->u.initRequest;
710 set_referenceId (p->odr_out, &req->referenceId, p->set_inher.referenceId);
711 req->options = &p->options;
712 req->protocolVersion = &p->protocolVersion;
713 req->preferredMessageSize = &p->preferredMessageSize;
714 req->maximumRecordSize = &p->maximumRecordSize;
716 if (p->idAuthenticationGroupId)
718 Z_IdPass *pass = odr_malloc (p->odr_out, sizeof(*pass));
719 Z_IdAuthentication *auth = odr_malloc (p->odr_out, sizeof(*auth));
721 logf (LOG_DEBUG, "using pass authentication");
723 auth->which = Z_IdAuthentication_idPass;
724 auth->u.idPass = pass;
725 if (p->idAuthenticationGroupId && *p->idAuthenticationGroupId)
726 pass->groupId = p->idAuthenticationGroupId;
728 pass->groupId = NULL;
729 if (p->idAuthenticationUserId && *p->idAuthenticationUserId)
730 pass->userId = p->idAuthenticationUserId;
733 if (p->idAuthenticationPassword && *p->idAuthenticationPassword)
734 pass->password = p->idAuthenticationPassword;
736 pass->password = NULL;
737 req->idAuthentication = auth;
739 else if (!p->idAuthenticationOpen || !*p->idAuthenticationOpen)
740 req->idAuthentication = NULL;
743 Z_IdAuthentication *auth = odr_malloc (p->odr_out, sizeof(*auth));
745 logf (LOG_DEBUG, "using open authentication");
746 auth->which = Z_IdAuthentication_open;
747 auth->u.open = p->idAuthenticationOpen;
748 req->idAuthentication = auth;
750 req->implementationId = p->implementationId;
751 req->implementationName = p->implementationName;
752 req->implementationVersion = p->implementationVersion;
753 req->userInformationField = 0;
755 return ir_tcl_send_APDU (interp, p, apdu, "init", *argv);
759 * do_protocolVersion: Set protocol Version
761 static int do_protocolVersion (void *obj, Tcl_Interp *interp,
762 int argc, char **argv)
770 ODR_MASK_ZERO (&p->protocolVersion);
771 ODR_MASK_SET (&p->protocolVersion, 0);
772 ODR_MASK_SET (&p->protocolVersion, 1);
777 if (Tcl_GetInt (interp, argv[2], &version)==TCL_ERROR)
779 ODR_MASK_ZERO (&p->protocolVersion);
780 for (i = 0; i<version; i++)
781 ODR_MASK_SET (&p->protocolVersion, i);
783 for (i = 4; --i >= 0; )
784 if (ODR_MASK_GET (&p->protocolVersion, i))
786 sprintf (buf, "%d", i+1);
787 interp->result = buf;
792 * do_options: Set options
794 static int do_options (void *obj, Tcl_Interp *interp,
795 int argc, char **argv)
797 static struct ir_named_entry options_tab[] = {
801 { "resourceReport", 3 },
802 { "triggerResourceCtrl", 4},
803 { "resourceCtrl", 5},
807 { "extendedServices", 10},
808 { "level-1Segmentation", 11},
809 { "level-2Segmentation", 12},
810 { "concurrentOperations", 13},
811 { "namedResultSets", 14},
818 ODR_MASK_ZERO (&p->options);
819 ODR_MASK_SET (&p->options, 0);
820 ODR_MASK_SET (&p->options, 1);
821 ODR_MASK_SET (&p->options, 4);
822 ODR_MASK_SET (&p->options, 7);
823 ODR_MASK_SET (&p->options, 14);
826 return ir_tcl_named_bits (options_tab, &p->options, interp, argc-2, argv+2);
830 * do_apduInfo: Get APDU information
832 static int do_apduInfo (void *obj, Tcl_Interp *interp, int argc, char **argv)
840 sprintf (buf, "%d", p->apduLen);
841 Tcl_AppendElement (interp, buf);
842 sprintf (buf, "%d", p->apduOffset);
843 Tcl_AppendElement (interp, buf);
846 Tcl_AppendElement (interp, "");
849 apduf = fopen ("apdu.tmp", "w");
852 Tcl_AppendElement (interp, "");
855 odr_dumpBER (apduf, p->buf_in, p->apduLen);
857 if (!(apduf = fopen ("apdu.tmp", "r")))
858 Tcl_AppendElement (interp, "");
863 Tcl_AppendResult (interp, " {", NULL);
864 while ((c = getc (apduf)) != EOF)
868 Tcl_AppendResult (interp, buf, NULL);
871 Tcl_AppendResult (interp, "}", NULL);
878 * do_failInfo: Get fail information
880 static int do_failInfo (void *obj, Tcl_Interp *interp, int argc, char **argv)
890 sprintf (buf, "%d", p->failInfo);
896 case IR_TCL_FAIL_CONNECT:
897 cp = "connect failed";
899 case IR_TCL_FAIL_READ:
900 cp = "connection closed";
902 case IR_TCL_FAIL_WRITE:
903 cp = "connection closed";
905 case IR_TCL_FAIL_IN_APDU:
906 cp = "failed to decode incoming APDU";
908 case IR_TCL_FAIL_UNKNOWN_APDU:
914 Tcl_AppendElement (interp, buf);
915 Tcl_AppendElement (interp, cp);
920 * do_preferredMessageSize: Set/get preferred message size
922 static int do_preferredMessageSize (void *obj, Tcl_Interp *interp,
923 int argc, char **argv)
929 p->preferredMessageSize = 30000;
932 return ir_tcl_get_set_int (&p->preferredMessageSize, interp, argc, argv);
936 * do_maximumRecordSize: Set/get maximum record size
938 static int do_maximumRecordSize (void *obj, Tcl_Interp *interp,
939 int argc, char **argv)
945 p->maximumRecordSize = 30000;
948 return ir_tcl_get_set_int (&p->maximumRecordSize, interp, argc, argv);
952 * do_initResult: Get init result
954 static int do_initResult (void *obj, Tcl_Interp *interp,
955 int argc, char **argv)
961 return ir_tcl_get_set_int (&p->initResult, interp, argc, argv);
966 * do_implementationName: Set/get Implementation Name.
968 static int do_implementationName (void *obj, Tcl_Interp *interp,
969 int argc, char **argv)
974 return ir_tcl_strdup (interp, &p->implementationName,
975 "Index Data/IrTcl on YAZ");
977 return ir_tcl_strdel (interp, &p->implementationName);
980 xfree (p->implementationName);
981 if (ir_tcl_strdup (interp, &p->implementationName, argv[2])
985 Tcl_AppendResult (interp, p->implementationName, (char*) NULL);
990 * do_implementationId: Get Implementation Id.
992 static int do_implementationId (void *obj, Tcl_Interp *interp,
993 int argc, char **argv)
998 return ir_tcl_strdup (interp, &p->implementationId, "YAZ (id=81)");
1000 return ir_tcl_strdel (interp, &p->implementationId);
1001 Tcl_AppendResult (interp, p->implementationId, (char*) NULL);
1006 * do_implementationVersion: get Implementation Version.
1008 static int do_implementationVersion (void *obj, Tcl_Interp *interp,
1009 int argc, char **argv)
1014 return ir_tcl_strdup (interp, &p->implementationVersion,
1016 #ifdef IR_TCL_VERSION
1017 " / Irtcl: " IR_TCL_VERSION
1020 else if (argc == -1)
1021 return ir_tcl_strdel (interp, &p->implementationVersion);
1022 Tcl_AppendResult (interp, p->implementationVersion, (char*) NULL);
1027 * do_targetImplementationName: Get Implementation Name of target.
1029 static int do_targetImplementationName (void *obj, Tcl_Interp *interp,
1030 int argc, char **argv)
1036 p->targetImplementationName = NULL;
1039 else if (argc == -1)
1040 return ir_tcl_strdel (interp, &p->targetImplementationName);
1041 Tcl_AppendResult (interp, p->targetImplementationName, (char*) NULL);
1046 * do_targetImplementationId: Get Implementation Id of target
1048 static int do_targetImplementationId (void *obj, Tcl_Interp *interp,
1049 int argc, char **argv)
1055 p->targetImplementationId = NULL;
1058 else if (argc == -1)
1059 return ir_tcl_strdel (interp, &p->targetImplementationId);
1060 Tcl_AppendResult (interp, p->targetImplementationId, (char*) NULL);
1065 * do_targetImplementationVersion: Get Implementation Version of target
1067 static int do_targetImplementationVersion (void *obj, Tcl_Interp *interp,
1068 int argc, char **argv)
1074 p->targetImplementationVersion = NULL;
1077 else if (argc == -1)
1078 return ir_tcl_strdel (interp, &p->targetImplementationVersion);
1079 Tcl_AppendResult (interp, p->targetImplementationVersion, (char*) NULL);
1084 * do_idAuthentication: Set/get id Authentication
1086 static int do_idAuthentication (void *obj, Tcl_Interp *interp,
1087 int argc, char **argv)
1091 if (argc >= 3 || argc == -1)
1093 xfree (p->idAuthenticationOpen);
1094 xfree (p->idAuthenticationGroupId);
1095 xfree (p->idAuthenticationUserId);
1096 xfree (p->idAuthenticationPassword);
1098 if (argc >= 3 || argc <= 0)
1100 p->idAuthenticationOpen = NULL;
1101 p->idAuthenticationGroupId = NULL;
1102 p->idAuthenticationUserId = NULL;
1103 p->idAuthenticationPassword = NULL;
1112 ir_tcl_strdup (interp, &p->idAuthenticationOpen, argv[2])
1119 ir_tcl_strdup (interp, &p->idAuthenticationGroupId, argv[2])
1123 ir_tcl_strdup (interp, &p->idAuthenticationUserId, argv[3])
1127 ir_tcl_strdup (interp, &p->idAuthenticationPassword, argv[4])
1132 if (p->idAuthenticationOpen)
1133 Tcl_AppendElement (interp, p->idAuthenticationOpen);
1134 else if (p->idAuthenticationGroupId)
1136 Tcl_AppendElement (interp, p->idAuthenticationGroupId);
1137 Tcl_AppendElement (interp, p->idAuthenticationUserId);
1138 Tcl_AppendElement (interp, p->idAuthenticationPassword);
1144 * do_connect: connect method on IR object
1146 static int do_connect (void *obj, Tcl_Interp *interp,
1147 int argc, char **argv)
1157 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
1158 " ?hostname?\"", NULL);
1163 Tcl_AppendResult (interp, p->hostname, NULL);
1167 logf (LOG_DEBUG, "connect %s %s", *argv, argv[2]);
1170 Tcl_AppendResult (interp, "already connected", NULL);
1171 return ir_tcl_error_exec (interp, argc, argv);
1173 if (!strcmp (p->comstackType, "tcpip"))
1175 p->cs_link = cs_create (tcpip_type, CS_BLOCK, p->protocol_type);
1176 addr = tcpip_strtoaddr (argv[2]);
1179 Tcl_AppendResult (interp, "tcpip_strtoaddr fail", NULL);
1180 return ir_tcl_error_exec (interp, argc, argv);
1182 logf (LOG_DEBUG, "tcp/ip connect %s", argv[2]);
1184 else if (!strcmp (p->comstackType, "mosi"))
1187 p->cs_link = cs_create (mosi_type, CS_BLOCK, p->protocol_type);
1188 addr = mosi_strtoaddr (argv[2]);
1191 Tcl_AppendResult (interp, "mosi_strtoaddr fail", NULL);
1192 return ir_tcl_error_exec (interp, argc, argv);
1194 logf (LOG_DEBUG, "mosi connect %s", argv[2]);
1196 Tcl_AppendResult (interp, "mosi not supported", NULL);
1197 return ir_tcl_error_exec (interp, argc, argv);
1202 Tcl_AppendResult (interp, "bad comstack type ",
1203 p->comstackType, NULL);
1204 return ir_tcl_error_exec (interp, argc, argv);
1206 if (ir_tcl_strdup (interp, &p->hostname, argv[2]) == TCL_ERROR)
1208 p->eventType = "connect";
1209 if ((r=cs_connect (p->cs_link, addr)) < 0)
1211 ir_tcl_disconnect (p);
1212 Tcl_AppendResult (interp, "connect fail", NULL);
1213 return ir_tcl_error_exec (interp, argc, argv);
1215 ir_select_add (cs_fileno (p->cs_link), p);
1218 logf (LOG_DEBUG, "connect pending fd=%d", cs_fileno(p->cs_link));
1219 ir_select_add_write (cs_fileno (p->cs_link), p);
1220 p->state = IR_TCL_R_Connecting;
1224 logf (LOG_DEBUG, "connect ok fd=%d", cs_fileno(p->cs_link));
1225 p->state = IR_TCL_R_Idle;
1227 ir_tcl_eval (p->interp, p->callback);
1234 * ir_tcl_disconnect: close connection
1236 void ir_tcl_disconnect (IrTcl_Obj *p)
1240 logf(LOG_DEBUG, "Closing connection to %s", p->hostname);
1241 xfree (p->hostname);
1243 assert (p->cs_link);
1244 ir_select_remove (cs_fileno (p->cs_link), p);
1246 odr_reset (p->odr_in);
1248 #if TCL_MAJOR_VERSION == 8
1249 cs_fileno(p->cs_link) = -1;
1251 cs_close (p->cs_link);
1254 ODR_MASK_ZERO (&p->options);
1255 ODR_MASK_SET (&p->options, 0);
1256 ODR_MASK_SET (&p->options, 1);
1257 ODR_MASK_SET (&p->options, 4);
1258 ODR_MASK_SET (&p->options, 7);
1259 ODR_MASK_SET (&p->options, 14);
1261 ODR_MASK_ZERO (&p->protocolVersion);
1262 ODR_MASK_SET (&p->protocolVersion, 0);
1263 ODR_MASK_SET (&p->protocolVersion, 1);
1266 assert (!p->cs_link);
1270 * do_disconnect: disconnect method on IR object
1272 static int do_disconnect (void *obj, Tcl_Interp *interp,
1273 int argc, char **argv)
1279 p->state = IR_TCL_R_Idle;
1280 p->eventType = NULL;
1285 ir_tcl_disconnect (p);
1290 * do_comstack: Set/get comstack method on IR object
1292 static int do_comstack (void *o, Tcl_Interp *interp,
1293 int argc, char **argv)
1298 return ir_tcl_strdup (interp, &obj->comstackType, "tcpip");
1299 else if (argc == -1)
1300 return ir_tcl_strdel (interp, &obj->comstackType);
1303 xfree (obj->comstackType);
1304 if (ir_tcl_strdup (interp, &obj->comstackType, argv[2]) == TCL_ERROR)
1307 Tcl_AppendElement (interp, obj->comstackType);
1312 * do_logLevel: Set log level
1314 static int do_logLevel (void *o, Tcl_Interp *interp,
1315 int argc, char **argv)
1320 log_init (log_mask_str (argv[2]), "", NULL);
1322 log_init (log_mask_str (argv[2]), argv[3], NULL);
1324 log_init (log_mask_str (argv[2]), argv[3], argv[4]);
1330 * do_eventType: Return type of last event
1332 static int do_eventType (void *obj, Tcl_Interp *interp,
1333 int argc, char **argv)
1339 p->eventType = NULL;
1342 Tcl_AppendElement (interp, p->eventType ? p->eventType : "");
1348 * do_callback: add callback
1350 static int do_callback (void *obj, Tcl_Interp *interp,
1351 int argc, char **argv)
1360 else if (argc == -1)
1361 return ir_tcl_strdel (interp, &p->callback);
1364 xfree (p->callback);
1367 if (ir_tcl_strdup (interp, &p->callback, argv[2]) == TCL_ERROR)
1377 * do_failback: add error handle callback
1379 static int do_failback (void *obj, Tcl_Interp *interp,
1380 int argc, char **argv)
1389 else if (argc == -1)
1390 return ir_tcl_strdel (interp, &p->failback);
1393 xfree (p->failback);
1396 if (ir_tcl_strdup (interp, &p->failback, argv[2]) == TCL_ERROR)
1406 * do_initResponse: add init response handler
1408 static int do_initResponse (void *obj, Tcl_Interp *interp,
1409 int argc, char **argv)
1415 p->initResponse = NULL;
1418 else if (argc == -1)
1419 return ir_tcl_strdel (interp, &p->initResponse);
1422 xfree (p->initResponse);
1425 if (ir_tcl_strdup (interp, &p->initResponse, argv[2]) == TCL_ERROR)
1429 p->initResponse = NULL;
1434 * do_protocol: Set/get protocol method on IR object
1436 static int do_protocol (void *o, Tcl_Interp *interp, int argc, char **argv)
1442 p->protocol_type = PROTO_Z3950;
1447 if (!strcmp (argv[2], "Z39"))
1448 p->protocol_type = PROTO_Z3950;
1449 else if (!strcmp (argv[2], "SR"))
1450 p->protocol_type = PROTO_SR;
1453 Tcl_AppendResult (interp, "bad protocol ", argv[2], NULL);
1454 return ir_tcl_error_exec (interp, argc, argv);
1458 switch (p->protocol_type)
1461 Tcl_AppendElement (interp, "Z39");
1464 Tcl_AppendElement (interp, "SR");
1471 * do_triggerResourceControl:
1473 static int do_triggerResourceControl (void *obj, Tcl_Interp *interp,
1474 int argc, char **argv)
1478 Z_TriggerResourceControlRequest *req;
1479 bool_t is_false = 0;
1485 Tcl_AppendResult (interp, "not connected", NULL);
1486 return ir_tcl_error_exec (interp, argc, argv);
1488 apdu = zget_APDU (p->odr_out, Z_APDU_triggerResourceControlRequest);
1489 req = apdu->u.triggerResourceControlRequest;
1490 *req->requestedAction = Z_TriggerResourceCtrl_cancel;
1491 req->resultSetWanted = &is_false;
1493 return ir_tcl_send_APDU (interp, p, apdu, "triggerResourceControl",
1498 * do_databaseNames: specify database names
1500 static int do_databaseNames (void *obj, Tcl_Interp *interp,
1501 int argc, char **argv)
1504 IrTcl_SetCObj *p = obj;
1508 for (i=0; i<p->num_databaseNames; i++)
1509 xfree (p->databaseNames[i]);
1510 xfree (p->databaseNames);
1514 p->num_databaseNames = 0;
1515 p->databaseNames = NULL;
1520 for (i=0; i<p->num_databaseNames; i++)
1521 Tcl_AppendElement (interp, p->databaseNames[i]);
1524 if (p->databaseNames)
1526 for (i=0; i<p->num_databaseNames; i++)
1527 xfree (p->databaseNames[i]);
1528 xfree (p->databaseNames);
1530 p->num_databaseNames = argc - 2;
1532 ir_tcl_malloc (sizeof(*p->databaseNames) * (1+p->num_databaseNames));
1533 for (i=0; i<p->num_databaseNames; i++)
1535 if (ir_tcl_strdup (interp, &p->databaseNames[i], argv[2+i])
1539 p->databaseNames[i] = NULL;
1544 * do_replaceIndicator: Set/get replace Set indicator
1546 static int do_replaceIndicator (void *obj, Tcl_Interp *interp,
1547 int argc, char **argv)
1549 IrTcl_SetCObj *p = obj;
1553 p->replaceIndicator = 1;
1556 return ir_tcl_get_set_int (&p->replaceIndicator, interp, argc, argv);
1560 * do_queryType: Set/Get query method
1562 static int do_queryType (void *obj, Tcl_Interp *interp,
1563 int argc, char **argv)
1565 IrTcl_SetCObj *p = obj;
1568 return ir_tcl_strdup (interp, &p->queryType, "rpn");
1569 else if (argc == -1)
1570 return ir_tcl_strdel (interp, &p->queryType);
1573 xfree (p->queryType);
1574 if (ir_tcl_strdup (interp, &p->queryType, argv[2]) == TCL_ERROR)
1577 Tcl_AppendResult (interp, p->queryType, NULL);
1582 * do_userInformationField: Get User information field
1584 static int do_userInformationField (void *obj, Tcl_Interp *interp,
1585 int argc, char **argv)
1591 p->userInformationField = NULL;
1594 else if (argc == -1)
1595 return ir_tcl_strdel (interp, &p->userInformationField);
1596 Tcl_AppendResult (interp, p->userInformationField, NULL);
1601 * do_smallSetUpperBound: Set/get small set upper bound
1603 static int do_smallSetUpperBound (void *o, Tcl_Interp *interp,
1604 int argc, char **argv)
1606 IrTcl_SetCObj *p = o;
1610 p->smallSetUpperBound = 0;
1613 return ir_tcl_get_set_int (&p->smallSetUpperBound, interp, argc, argv);
1617 * do_largeSetLowerBound: Set/get large set lower bound
1619 static int do_largeSetLowerBound (void *o, Tcl_Interp *interp,
1620 int argc, char **argv)
1622 IrTcl_SetCObj *p = o;
1626 p->largeSetLowerBound = 2;
1629 return ir_tcl_get_set_int (&p->largeSetLowerBound, interp, argc, argv);
1633 * do_mediumSetPresentNumber: Set/get large set lower bound
1635 static int do_mediumSetPresentNumber (void *o, Tcl_Interp *interp,
1636 int argc, char **argv)
1638 IrTcl_SetCObj *p = o;
1642 p->mediumSetPresentNumber = 0;
1645 return ir_tcl_get_set_int (&p->mediumSetPresentNumber, interp, argc, argv);
1649 * do_referenceId: Set/Get referenceId
1651 static int do_referenceId (void *obj, Tcl_Interp *interp,
1652 int argc, char **argv)
1654 IrTcl_SetCObj *p = obj;
1658 p->referenceId = NULL;
1661 else if (argc == -1)
1662 return ir_tcl_strdel (interp, &p->referenceId);
1665 xfree (p->referenceId);
1666 if (ir_tcl_strdup (interp, &p->referenceId, argv[2]) == TCL_ERROR)
1669 Tcl_AppendResult (interp, p->referenceId, NULL);
1674 * do_preferredRecordSyntax: Set/get preferred record syntax
1676 static int do_preferredRecordSyntax (void *obj, Tcl_Interp *interp,
1677 int argc, char **argv)
1679 IrTcl_SetCObj *p = obj;
1683 p->preferredRecordSyntax = NULL;
1686 else if (argc == -1)
1688 xfree (p->preferredRecordSyntax);
1689 p->preferredRecordSyntax = NULL;
1694 xfree (p->preferredRecordSyntax);
1695 p->preferredRecordSyntax = NULL;
1696 if (argv[2][0] && (p->preferredRecordSyntax =
1697 ir_tcl_malloc (sizeof(*p->preferredRecordSyntax))))
1698 *p->preferredRecordSyntax = IrTcl_getRecordSyntaxVal (argv[2]);
1703 (interp,!p->preferredRecordSyntax ? "" :
1704 IrTcl_getRecordSyntaxStr(*p->preferredRecordSyntax));
1711 * do_elementSetNames: Set/Get element Set Names
1713 static int do_elementSetNames (void *obj, Tcl_Interp *interp,
1714 int argc, char **argv)
1716 IrTcl_SetCObj *p = obj;
1720 p->elementSetNames = NULL;
1723 else if (argc == -1)
1724 return ir_tcl_strdel (interp, &p->elementSetNames);
1727 xfree (p->elementSetNames);
1728 if (ir_tcl_strdup (interp, &p->elementSetNames, argv[2]) == TCL_ERROR)
1731 Tcl_AppendResult (interp, p->elementSetNames, NULL);
1736 * do_smallSetElementSetNames: Set/Get small Set Element Set Names
1738 static int do_smallSetElementSetNames (void *obj, Tcl_Interp *interp,
1739 int argc, char **argv)
1741 IrTcl_SetCObj *p = obj;
1745 p->smallSetElementSetNames = NULL;
1748 else if (argc == -1)
1749 return ir_tcl_strdel (interp, &p->smallSetElementSetNames);
1752 xfree (p->smallSetElementSetNames);
1753 if (ir_tcl_strdup (interp, &p->smallSetElementSetNames,
1754 argv[2]) == TCL_ERROR)
1757 Tcl_AppendResult (interp, p->smallSetElementSetNames, NULL);
1762 * do_mediumSetElementSetNames: Set/Get medium Set Element Set Names
1764 static int do_mediumSetElementSetNames (void *obj, Tcl_Interp *interp,
1765 int argc, char **argv)
1767 IrTcl_SetCObj *p = obj;
1771 p->mediumSetElementSetNames = NULL;
1774 else if (argc == -1)
1775 return ir_tcl_strdel (interp, &p->mediumSetElementSetNames);
1778 xfree (p->mediumSetElementSetNames);
1779 if (ir_tcl_strdup (interp, &p->mediumSetElementSetNames,
1780 argv[2]) == TCL_ERROR)
1783 Tcl_AppendResult (interp, p->mediumSetElementSetNames, NULL);
1787 static IrTcl_Method ir_method_tab[] = {
1788 { "comstack", do_comstack, NULL },
1789 { "protocol", do_protocol, NULL },
1790 { "failback", do_failback, NULL },
1791 { "failInfo", do_failInfo, NULL },
1792 { "apduInfo", do_apduInfo, NULL },
1793 { "logLevel", do_logLevel, NULL },
1795 { "eventType", do_eventType, NULL },
1796 { "connect", do_connect, NULL },
1797 { "protocolVersion", do_protocolVersion, NULL },
1798 { "preferredMessageSize", do_preferredMessageSize, NULL },
1799 { "maximumRecordSize", do_maximumRecordSize, NULL },
1800 { "implementationName", do_implementationName, NULL },
1801 { "implementationId", do_implementationId, NULL },
1802 { "implementationVersion", do_implementationVersion, NULL },
1803 { "targetImplementationName", do_targetImplementationName, NULL },
1804 { "targetImplementationId", do_targetImplementationId, NULL },
1805 { "targetImplementationVersion", do_targetImplementationVersion, NULL},
1806 { "userInformationField", do_userInformationField, NULL},
1807 { "idAuthentication", do_idAuthentication, NULL},
1808 { "options", do_options, NULL},
1809 { "init", do_init_request, NULL},
1810 { "initResult", do_initResult, NULL},
1811 { "disconnect", do_disconnect, NULL},
1812 { "callback", do_callback, NULL},
1813 { "initResponse", do_initResponse, NULL},
1814 { "triggerResourceControl", do_triggerResourceControl, NULL},
1815 { "initResponse", do_initResponse, NULL},
1819 static IrTcl_Method ir_set_c_method_tab[] = {
1820 { "databaseNames", do_databaseNames, NULL},
1821 { "replaceIndicator", do_replaceIndicator, NULL},
1822 { "queryType", do_queryType, NULL},
1823 { "preferredRecordSyntax", do_preferredRecordSyntax, NULL},
1824 { "smallSetUpperBound", do_smallSetUpperBound, NULL},
1825 { "largeSetLowerBound", do_largeSetLowerBound, NULL},
1826 { "mediumSetPresentNumber", do_mediumSetPresentNumber, NULL},
1827 { "referenceId", do_referenceId, NULL},
1828 { "elementSetNames", do_elementSetNames, NULL},
1829 { "smallSetElementSetNames", do_smallSetElementSetNames, NULL},
1830 { "mediumSetElementSetNames", do_mediumSetElementSetNames, NULL},
1835 * ir_obj_method: IR Object methods
1837 static int ir_obj_method (ClientData clientData, Tcl_Interp *interp,
1838 int argc, char **argv)
1840 IrTcl_Methods tab[3];
1841 IrTcl_Obj *p = (IrTcl_Obj *) clientData;
1846 Tcl_AppendResult (interp, wrongArgs, *argv, "method args...\"", NULL);
1850 tab[0].tab = ir_method_tab;
1852 tab[1].tab = ir_set_c_method_tab;
1853 tab[1].obj = &p->set_inher;
1856 if (ir_tcl_method (interp, argc, argv, tab, &r) == TCL_ERROR)
1857 return ir_tcl_method_error (interp, argc, argv, tab);
1862 * ir_obj_delete: IR Object disposal
1864 static void ir_obj_delete (ClientData clientData)
1866 IrTcl_Obj *obj = (IrTcl_Obj *) clientData;
1867 IrTcl_Methods tab[3];
1870 if (obj->ref_count > 0)
1872 assert (obj->ref_count == 0);
1874 logf (LOG_DEBUG, "ir object delete");
1875 tab[0].tab = ir_method_tab;
1877 tab[1].tab = ir_set_c_method_tab;
1878 tab[1].obj = &obj->set_inher;
1881 ir_tcl_method (NULL, -1, NULL, tab, NULL);
1884 odr_destroy (obj->odr_in);
1885 odr_destroy (obj->odr_out);
1886 odr_destroy (obj->odr_pr);
1891 * ir_obj_init: IR Object initialization
1893 int ir_obj_init (ClientData clientData, Tcl_Interp *interp,
1894 int argc, char **argv, ClientData *subData,
1895 ClientData parentData)
1897 IrTcl_Methods tab[3];
1905 Tcl_AppendResult (interp, wrongArgs, *argv, " objName\"", NULL);
1908 obj = ir_tcl_malloc (sizeof(*obj));
1911 obj->bibset = ccl_qual_mk ();
1912 if ((inf = fopen ("default.bib", "r")))
1914 ccl_qual_file (obj->bibset, inf);
1919 logf (LOG_DEBUG, "ir object create %s", argv[1]);
1920 obj->odr_in = odr_createmem (ODR_DECODE);
1921 odr_choice_enable_bias (obj->odr_in, 0);
1922 obj->odr_out = odr_createmem (ODR_ENCODE);
1923 obj->odr_pr = odr_createmem (ODR_PRINT);
1924 obj->state = IR_TCL_R_Idle;
1925 obj->interp = interp;
1929 obj->request_queue = NULL;
1931 tab[0].tab = ir_method_tab;
1933 tab[1].tab = ir_set_c_method_tab;
1934 tab[1].obj = &obj->set_inher;
1937 if (ir_tcl_method (interp, 0, NULL, tab, NULL) == TCL_ERROR)
1939 Tcl_AppendResult (interp, "Failed to initialize ", argv[1], NULL);
1942 *subData = (ClientData) obj;
1948 * ir_obj_mk: IR Object creation
1950 static int ir_obj_mk (ClientData clientData, Tcl_Interp *interp,
1951 int argc, char **argv)
1954 int r = ir_obj_init (clientData, interp, argc, argv, &subData, 0);
1958 Tcl_CreateCommand (interp, argv[1], ir_obj_method,
1959 subData, ir_obj_delete);
1963 IrTcl_Class ir_obj_class = {
1971 /* ------------------------------------------------------- */
1973 * do_search: Do search request
1975 static int do_search (void *o, Tcl_Interp *interp, int argc, char **argv)
1977 Z_SearchRequest *req;
1981 IrTcl_SetObj *obj = o;
1992 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1], "query\"",
1996 logf (LOG_DEBUG, "search %s %s", *argv, argv[2]);
1997 if (!obj->set_inher.num_databaseNames)
1999 Tcl_AppendResult (interp, "no databaseNames", NULL);
2000 return ir_tcl_error_exec (interp, argc, argv);
2004 Tcl_AppendResult (interp, "not connected", NULL);
2005 return ir_tcl_error_exec (interp, argc, argv);
2007 apdu = zget_APDU (p->odr_out, Z_APDU_searchRequest);
2008 req = apdu->u.searchRequest;
2012 set_referenceId (p->odr_out, &req->referenceId,
2013 obj->set_inher.referenceId);
2015 req->smallSetUpperBound = &obj->set_inher.smallSetUpperBound;
2016 req->largeSetLowerBound = &obj->set_inher.largeSetLowerBound;
2017 req->mediumSetPresentNumber = &obj->set_inher.mediumSetPresentNumber;
2018 req->replaceIndicator = &obj->set_inher.replaceIndicator;
2019 req->resultSetName = obj->setName ? obj->setName : "default";
2020 logf (LOG_DEBUG, "Search, resultSetName %s", req->resultSetName);
2021 req->num_databaseNames = obj->set_inher.num_databaseNames;
2022 req->databaseNames = obj->set_inher.databaseNames;
2023 for (r=0; r < obj->set_inher.num_databaseNames; r++)
2024 logf (LOG_DEBUG, " Database %s", obj->set_inher.databaseNames[r]);
2025 if (obj->set_inher.preferredRecordSyntax)
2027 struct oident ident;
2029 ident.proto = p->protocol_type;
2030 ident.oclass = CLASS_RECSYN;
2031 ident.value = *obj->set_inher.preferredRecordSyntax;
2032 logf (LOG_DEBUG, "Preferred record syntax is %d", ident.value);
2033 req->preferredRecordSyntax = odr_oiddup (p->odr_out,
2034 oid_getoidbyent (&ident));
2037 req->preferredRecordSyntax = 0;
2039 if (obj->set_inher.smallSetElementSetNames &&
2040 *obj->set_inher.smallSetElementSetNames)
2042 Z_ElementSetNames *esn = odr_malloc (p->odr_out, sizeof(*esn));
2044 esn->which = Z_ElementSetNames_generic;
2045 esn->u.generic = obj->set_inher.smallSetElementSetNames;
2046 req->smallSetElementSetNames = esn;
2049 req->smallSetElementSetNames = NULL;
2051 if (obj->set_inher.mediumSetElementSetNames &&
2052 *obj->set_inher.mediumSetElementSetNames)
2054 Z_ElementSetNames *esn = odr_malloc (p->odr_out, sizeof(*esn));
2056 esn->which = Z_ElementSetNames_generic;
2057 esn->u.generic = obj->set_inher.mediumSetElementSetNames;
2058 req->mediumSetElementSetNames = esn;
2061 req->mediumSetElementSetNames = NULL;
2063 req->query = &query;
2065 logf (LOG_DEBUG, "queryType %s", obj->set_inher.queryType);
2066 if (!strcmp (obj->set_inher.queryType, "rpn"))
2068 Z_RPNQuery *RPNquery;
2070 RPNquery = p_query_rpn (p->odr_out, p->protocol_type, argv[2]);
2073 Tcl_AppendResult (interp, "query syntax error", NULL);
2074 return ir_tcl_error_exec (interp, argc, argv);
2076 query.which = Z_Query_type_1;
2077 query.u.type_1 = RPNquery;
2080 else if (!strcmp (obj->set_inher.queryType, "cclrpn"))
2084 struct ccl_rpn_node *rpn;
2085 Z_RPNQuery *RPNquery;
2088 bib1.proto = p->protocol_type;
2089 bib1.oclass = CLASS_ATTSET;
2090 bib1.value = VAL_BIB1;
2092 rpn = ccl_find_str(p->bibset, argv[2], &error, &pos);
2095 Tcl_AppendResult (interp, "ccl syntax error ", ccl_err_msg(error),
2097 return ir_tcl_error_exec (interp, argc, argv);
2100 ccl_pr_tree (rpn, stderr);
2101 fprintf (stderr, "\n");
2103 assert((RPNquery = ccl_rpn_query(rpn)));
2104 RPNquery->attributeSetId = oid_getoidbyent (&bib1);
2105 query.which = Z_Query_type_1;
2106 query.u.type_1 = RPNquery;
2109 else if (!strcmp (obj->set_inher.queryType, "ccl"))
2111 query.which = Z_Query_type_2;
2112 query.u.type_2 = &ccl_query;
2113 ccl_query.buf = (unsigned char *) argv[2];
2114 ccl_query.len = strlen (argv[2]);
2118 Tcl_AppendResult (interp, "invalid query method ",
2119 obj->set_inher.queryType, NULL);
2120 return ir_tcl_error_exec (interp, argc, argv);
2122 return ir_tcl_send_APDU (interp, p, apdu, "search", *argv);
2126 * do_searchResponse: add search response handler
2128 static int do_searchResponse (void *o, Tcl_Interp *interp,
2129 int argc, char **argv)
2131 IrTcl_SetObj *obj = o;
2135 obj->searchResponse = NULL;
2138 else if (argc == -1)
2139 return ir_tcl_strdel (interp, &obj->searchResponse);
2142 xfree (obj->searchResponse);
2145 if (ir_tcl_strdup (interp, &obj->searchResponse, argv[2])
2150 obj->searchResponse = NULL;
2156 * do_presentResponse: add present response handler
2158 static int do_presentResponse (void *o, Tcl_Interp *interp,
2159 int argc, char **argv)
2161 IrTcl_SetObj *obj = o;
2165 obj->presentResponse = NULL;
2168 else if (argc == -1)
2169 return ir_tcl_strdel (interp, &obj->presentResponse);
2172 xfree (obj->presentResponse);
2175 if (ir_tcl_strdup (interp, &obj->presentResponse, argv[2])
2180 obj->presentResponse = NULL;
2186 * do_resultCount: Get number of hits
2188 static int do_resultCount (void *o, Tcl_Interp *interp,
2189 int argc, char **argv)
2191 IrTcl_SetObj *obj = o;
2195 obj->resultCount = 0;
2198 return ir_tcl_get_set_int (&obj->resultCount, interp, argc, argv);
2202 * do_searchStatus: Get search status (after search response)
2204 static int do_searchStatus (void *o, Tcl_Interp *interp,
2205 int argc, char **argv)
2207 IrTcl_SetObj *obj = o;
2211 return ir_tcl_get_set_int (&obj->searchStatus, interp, argc, argv);
2215 * do_presentStatus: Get search status (after search/present response)
2217 static int do_presentStatus (void *o, Tcl_Interp *interp,
2218 int argc, char **argv)
2220 IrTcl_SetObj *obj = o;
2224 return ir_tcl_get_set_int (&obj->presentStatus, interp, argc, argv);
2228 * do_nextResultSetPosition: Get next result set position
2229 * (after search/present response)
2231 static int do_nextResultSetPosition (void *o, Tcl_Interp *interp,
2232 int argc, char **argv)
2234 IrTcl_SetObj *obj = o;
2238 obj->nextResultSetPosition = 0;
2241 return ir_tcl_get_set_int (&obj->nextResultSetPosition, interp,
2246 * do_setName: Set result Set name
2248 static int do_setName (void *o, Tcl_Interp *interp,
2249 int argc, char **argv)
2251 IrTcl_SetObj *obj = o;
2254 return ir_tcl_strdup (interp, &obj->setName, "Default");
2255 else if (argc == -1)
2256 return ir_tcl_strdel (interp, &obj->setName);
2259 xfree (obj->setName);
2260 if (ir_tcl_strdup (interp, &obj->setName, argv[2])
2264 Tcl_AppendElement (interp, obj->setName);
2269 * do_numberOfRecordsReturned: Get number of records returned
2271 static int do_numberOfRecordsReturned (void *o, Tcl_Interp *interp,
2272 int argc, char **argv)
2274 IrTcl_SetObj *obj = o;
2278 obj->numberOfRecordsReturned = 0;
2281 return ir_tcl_get_set_int (&obj->numberOfRecordsReturned, interp,
2286 * do_type: Return type (if any) at position.
2288 static int do_type (void *o, Tcl_Interp *interp, int argc, char **argv)
2290 IrTcl_SetObj *obj = o;
2292 IrTcl_RecordList *rl;
2296 obj->record_list = NULL;
2299 else if (argc == -1)
2301 delete_IR_records (obj);
2306 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2307 " position\"", NULL);
2310 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2312 rl = find_IR_record (obj, offset);
2315 logf (LOG_DEBUG, "%s %s %s: no record", argv[0], argv[1], argv[2]);
2320 case Z_NamePlusRecord_databaseRecord:
2321 interp->result = "DB";
2323 case Z_NamePlusRecord_surrogateDiagnostic:
2324 interp->result = "SD";
2332 * do_recordType: Return record type (if any) at position.
2334 static int do_recordType (void *o, Tcl_Interp *interp, int argc, char **argv)
2336 IrTcl_SetObj *obj = o;
2338 IrTcl_RecordList *rl;
2344 else if (argc == -1)
2350 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2351 " position\"", NULL);
2354 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2356 rl = find_IR_record (obj, offset);
2359 logf (LOG_DEBUG, "%s %s %s: no record", argv[0], argv[1], argv[2]);
2362 if (rl->which != Z_NamePlusRecord_databaseRecord)
2364 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2367 Tcl_AppendElement (interp, (char*)
2368 IrTcl_getRecordSyntaxStr (rl->u.dbrec.type));
2373 * set record elements (for record extraction)
2375 static int do_recordElements (void *o, Tcl_Interp *interp,
2376 int argc, char **argv)
2378 IrTcl_SetObj *obj = o;
2382 obj->recordElements = NULL;
2385 else if (argc == -1)
2386 return ir_tcl_strdel (NULL, &obj->recordElements);
2389 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2390 " ?position?\"", NULL);
2395 xfree (obj->recordElements);
2396 return ir_tcl_strdup (NULL, &obj->recordElements,
2397 (*argv[2] ? argv[2] : NULL));
2399 Tcl_AppendResult (interp, obj->recordElements, NULL);
2406 static int ir_diagResult (Tcl_Interp *interp, IrTcl_Diagnostic *list, int num)
2412 for (i = 0; i<num; i++)
2414 sprintf (buf, "%d", list[i].condition);
2415 Tcl_AppendElement (interp, buf);
2416 cp = diagbib1_str (list[i].condition);
2418 Tcl_AppendElement (interp, (char*) cp);
2420 Tcl_AppendElement (interp, "");
2421 if (list[i].addinfo)
2422 Tcl_AppendElement (interp, (char*) list[i].addinfo);
2424 Tcl_AppendElement (interp, "");
2430 * do_diag: Return diagnostic record info
2432 static int do_diag (void *o, Tcl_Interp *interp, int argc, char **argv)
2434 IrTcl_SetObj *obj = o;
2436 IrTcl_RecordList *rl;
2442 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2443 " position\"", NULL);
2446 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2448 rl = find_IR_record (obj, offset);
2451 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2454 if (rl->which != Z_NamePlusRecord_surrogateDiagnostic)
2456 Tcl_AppendResult (interp, "No Diagnostic record at #", argv[2], NULL);
2459 return ir_diagResult (interp, rl->u.surrogateDiagnostics.list,
2460 rl->u.surrogateDiagnostics.num);
2464 * do_getMarc: Get ISO2709 Record lines/fields
2466 static int do_getMarc (void *o, Tcl_Interp *interp, int argc, char **argv)
2468 IrTcl_SetObj *obj = o;
2470 IrTcl_RecordList *rl;
2476 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2477 " position line|field tag ind field\"", NULL);
2480 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2482 rl = find_IR_record (obj, offset);
2485 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2488 if (rl->which != Z_NamePlusRecord_databaseRecord)
2490 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2493 return ir_tcl_get_marc (interp, rl->u.dbrec.buf, argc, argv);
2497 * do_getSutrs: Get SUTRS Record
2499 static int do_getSutrs (void *o, Tcl_Interp *interp, int argc, char **argv)
2501 IrTcl_SetObj *obj = o;
2503 IrTcl_RecordList *rl;
2509 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2510 " position\"", NULL);
2513 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2515 rl = find_IR_record (obj, offset);
2518 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2521 if (rl->which != Z_NamePlusRecord_databaseRecord)
2523 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2526 if (!rl->u.dbrec.buf || rl->u.dbrec.type != VAL_SUTRS)
2528 Tcl_AppendElement (interp, rl->u.dbrec.buf);
2534 * do_getGrs: Get a GRS-1 Record
2536 static int do_getGrs (void *o, Tcl_Interp *interp, int argc, char **argv)
2538 IrTcl_SetObj *obj = o;
2540 IrTcl_RecordList *rl;
2546 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2547 " position ?(set,tag) (set,tag) ...?\"", NULL);
2550 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2552 rl = find_IR_record (obj, offset);
2555 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2558 if (rl->which != Z_NamePlusRecord_databaseRecord)
2560 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2563 if (rl->u.dbrec.type != VAL_GRS1)
2565 return ir_tcl_get_grs (interp, rl->u.dbrec.u.grs1, argc, argv);
2570 * do_getExplain: Get an Explain Record
2572 static int do_getExplain (void *o, Tcl_Interp *interp, int argc, char **argv)
2574 IrTcl_SetObj *obj = o;
2575 IrTcl_Obj *p = obj->parent;
2577 Z_ext_typeent *etype;
2579 IrTcl_RecordList *rl;
2585 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2586 " position ?mask? ...\"", NULL);
2589 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2591 rl = find_IR_record (obj, offset);
2594 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2597 if (rl->which != Z_NamePlusRecord_databaseRecord)
2599 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2602 if (!rl->u.dbrec.buf || rl->u.dbrec.type != VAL_EXPLAIN)
2605 if (!(etype = z_ext_getentbyref (VAL_EXPLAIN)))
2607 assert (rl->u.dbrec.buf);
2608 odr_setbuf (p->odr_in, rl->u.dbrec.buf, rl->u.dbrec.size, 0);
2609 if (!(*etype->fun)(p->odr_in, &rr, 0))
2612 if (etype->what != Z_External_explainRecord)
2615 return ir_tcl_get_explain (interp, rr, argc, argv);
2619 * do_responseStatus: Return response status (present or search)
2621 static int do_responseStatus (void *o, Tcl_Interp *interp,
2622 int argc, char **argv)
2624 IrTcl_SetObj *obj = o;
2628 obj->recordFlag = 0;
2629 obj->nonSurrogateDiagnosticNum = 0;
2630 obj->nonSurrogateDiagnosticList = NULL;
2633 else if (argc == -1)
2635 ir_deleteDiags (&obj->nonSurrogateDiagnosticList,
2636 &obj->nonSurrogateDiagnosticNum);
2639 if (!obj->recordFlag)
2641 Tcl_AppendElement (interp, "OK");
2646 case Z_Records_DBOSD:
2647 Tcl_AppendElement (interp, "DBOSD");
2650 Tcl_AppendElement (interp, "NSD");
2651 return ir_diagResult (interp, obj->nonSurrogateDiagnosticList,
2652 obj->nonSurrogateDiagnosticNum);
2658 * do_present: Perform Present Request
2661 static int do_present (void *o, Tcl_Interp *interp, int argc, char **argv)
2663 IrTcl_SetObj *obj = o;
2666 Z_PresentRequest *req;
2674 if (Tcl_GetInt (interp, argv[2], &start) == TCL_ERROR)
2681 if (Tcl_GetInt (interp, argv[3], &number) == TCL_ERROR)
2686 logf (LOG_DEBUG, "present %s %d %d", *argv, start, number);
2690 Tcl_AppendResult (interp, "not connected", NULL);
2691 return ir_tcl_error_exec (interp, argc, argv);
2694 obj->number = number;
2696 apdu = zget_APDU (p->odr_out, Z_APDU_presentRequest);
2697 req = apdu->u.presentRequest;
2699 set_referenceId (p->odr_out, &req->referenceId,
2700 obj->set_inher.referenceId);
2702 req->resultSetId = obj->setName ? obj->setName : "Default";
2704 req->resultSetStartPoint = &start;
2705 req->numberOfRecordsRequested = &number;
2706 if (obj->set_inher.preferredRecordSyntax)
2708 struct oident ident;
2710 ident.proto = p->protocol_type;
2711 ident.oclass = CLASS_RECSYN;
2712 ident.value = *obj->set_inher.preferredRecordSyntax;
2713 logf (LOG_DEBUG, "Preferred record syntax is %d", ident.value);
2714 req->preferredRecordSyntax = odr_oiddup (p->odr_out,
2715 oid_getoidbyent (&ident));
2718 req->preferredRecordSyntax = 0;
2720 if (obj->set_inher.elementSetNames && *obj->set_inher.elementSetNames)
2722 Z_ElementSetNames *esn = odr_malloc (p->odr_out, sizeof(*esn));
2723 Z_RecordComposition *compo = odr_malloc (p->odr_out, sizeof(*compo));
2725 esn->which = Z_ElementSetNames_generic;
2726 esn->u.generic = obj->set_inher.elementSetNames;
2728 req->recordComposition = compo;
2729 compo->which = Z_RecordComp_simple;
2730 compo->u.simple = esn;
2733 req->recordComposition = NULL;
2734 return ir_tcl_send_APDU (interp, p, apdu, "present", *argv);
2737 #define IR_TCL_RECORD_ENCODING_ISO2709 1
2738 #define IR_TCL_RECORD_ENCODING_RAW 2
2744 } IrTcl_FileRecordHead;
2747 * do_loadFile: Load result set from file
2749 static int do_loadFile (void *o, Tcl_Interp *interp,
2750 int argc, char **argv)
2752 IrTcl_SetObj *setobj = o;
2764 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2765 " filename ?start? ?number?\"", NULL);
2769 start = atoi (argv[3]);
2771 number = atoi (argv[4]);
2774 inf = fopen (argv[2], "r");
2777 Tcl_AppendResult (interp, "Cannot open ", argv[2], NULL);
2778 return ir_tcl_error_exec (interp, argc, argv);
2780 while (offset < (start+number))
2782 IrTcl_FileRecordHead head;
2783 IrTcl_RecordList *rl;
2785 if (fread (&head, sizeof(head), 1, inf) < 1)
2787 rl = new_IR_record (setobj, offset,
2788 Z_NamePlusRecord_databaseRecord,
2789 (argc > 5) ? argv[5] : NULL);
2790 rl->u.dbrec.type = head.syntax;
2791 if (head.encoding == IR_TCL_RECORD_ENCODING_ISO2709)
2793 if (!(buf = ir_tcl_fread_marc (inf, &size)))
2795 rl->u.dbrec.buf = buf;
2796 rl->u.dbrec.size = size;
2797 if (size != head.size)
2800 Tcl_AppendResult (interp, "bad ISO2709 encoding", NULL);
2801 return ir_tcl_error_exec (interp, argc, argv);
2804 else if (head.encoding == IR_TCL_RECORD_ENCODING_RAW)
2806 rl->u.dbrec.size = head.size;
2807 rl->u.dbrec.buf = ir_tcl_malloc (head.size + 1);
2808 if (fread (rl->u.dbrec.buf, rl->u.dbrec.size, 1, inf) < 1)
2811 Tcl_AppendResult (interp, "bad raw encoding", NULL);
2812 return ir_tcl_error_exec (interp, argc, argv);
2814 rl->u.dbrec.buf[rl->u.dbrec.size] = '\0';
2818 rl->u.dbrec.buf = NULL;
2819 rl->u.dbrec.size = 0;
2821 Tcl_AppendResult (interp, "bad encoding", NULL);
2822 return ir_tcl_error_exec (interp, argc, argv);
2826 setobj->numberOfRecordsReturned = offset - start;
2832 * do_saveFile: Save result set on file
2834 static int do_saveFile (void *o, Tcl_Interp *interp,
2835 int argc, char **argv)
2837 IrTcl_SetObj *setobj = o;
2842 IrTcl_RecordList *rl;
2848 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2849 " filename ?start? ?number?\"", NULL);
2853 start = atoi (argv[3]);
2855 number = atoi (argv[4]);
2858 outf = fopen (argv[2], "w");
2861 Tcl_AppendResult (interp, "cannot open file", NULL);
2862 return ir_tcl_error_exec (interp, argc, argv);
2864 while (offset < (start+number) && (rl = find_IR_record (setobj, offset)))
2866 if (rl->which == Z_NamePlusRecord_databaseRecord &&
2867 rl->u.dbrec.buf && rl->u.dbrec.size)
2869 IrTcl_FileRecordHead head;
2871 head.encoding = IR_TCL_RECORD_ENCODING_RAW;
2872 head.syntax = rl->u.dbrec.type;
2873 head.size = rl->u.dbrec.size;
2874 if (fwrite (&head, sizeof(head), 1, outf) < 1)
2876 Tcl_AppendResult (interp, "cannot write", NULL);
2877 return ir_tcl_error_exec (interp, argc, argv);
2879 if (fwrite (rl->u.dbrec.buf, rl->u.dbrec.size, 1, outf) < 1)
2881 Tcl_AppendResult (interp, "cannot write", NULL);
2882 return ir_tcl_error_exec (interp, argc, argv);
2889 Tcl_AppendResult (interp, "cannot write ", NULL);
2890 return ir_tcl_error_exec (interp, argc, argv);
2896 static IrTcl_Method ir_set_method_tab[] = {
2897 { "search", do_search, NULL},
2898 { "searchResponse", do_searchResponse, NULL},
2899 { "presentResponse", do_presentResponse, NULL},
2900 { "searchStatus", do_searchStatus, NULL},
2901 { "presentStatus", do_presentStatus, NULL},
2902 { "nextResultSetPosition", do_nextResultSetPosition, NULL},
2903 { "setName", do_setName, NULL},
2904 { "resultCount", do_resultCount, NULL},
2905 { "numberOfRecordsReturned", do_numberOfRecordsReturned, NULL},
2906 { "present", do_present, NULL},
2907 { "type", do_type, NULL},
2908 { "getMarc", do_getMarc, NULL},
2909 { "getSutrs", do_getSutrs, NULL},
2910 { "getGrs", do_getGrs, NULL},
2911 { "getExplain", do_getExplain, NULL},
2912 { "recordType", do_recordType, NULL},
2913 { "recordElements", do_recordElements, NULL},
2914 { "diag", do_diag, NULL},
2915 { "responseStatus", do_responseStatus, NULL},
2916 { "loadFile", do_loadFile, NULL},
2917 { "saveFile", do_saveFile, NULL},
2922 * ir_set_obj_method: IR Set Object methods
2924 static int ir_set_obj_method (ClientData clientData, Tcl_Interp *interp,
2925 int argc, char **argv)
2927 IrTcl_Methods tabs[3];
2928 IrTcl_SetObj *p = (IrTcl_SetObj *) clientData;
2933 Tcl_AppendResult (interp, wrongArgs, *argv, " method args...\"", NULL);
2936 tabs[0].tab = ir_set_method_tab;
2938 tabs[1].tab = ir_set_c_method_tab;
2939 tabs[1].obj = &p->set_inher;
2942 if (ir_tcl_method (interp, argc, argv, tabs, &r) == TCL_ERROR)
2943 return ir_tcl_method_error (interp, argc, argv, tabs);
2948 * ir_set_obj_delete: IR Set Object disposal
2950 static void ir_set_obj_delete (ClientData clientData)
2952 IrTcl_Methods tabs[3];
2953 IrTcl_SetObj *p = (IrTcl_SetObj *) clientData;
2955 logf (LOG_DEBUG, "ir set delete");
2957 tabs[0].tab = ir_set_method_tab;
2959 tabs[1].tab = ir_set_c_method_tab;
2960 tabs[1].obj = &p->set_inher;
2963 ir_tcl_method (NULL, -1, NULL, tabs, NULL);
2969 * ir_set_obj_init: IR Set Object initialization
2971 static int ir_set_obj_init (ClientData clientData, Tcl_Interp *interp,
2972 int argc, char **argv, ClientData *subData,
2973 ClientData parentData)
2975 IrTcl_Methods tabs[3];
2978 if (argc < 2 || argc > 3)
2980 Tcl_AppendResult (interp, wrongArgs, *argv,
2981 " objSetName ?objParent?\"", NULL);
2984 obj = ir_tcl_malloc (sizeof(*obj));
2985 logf (LOG_DEBUG, "ir set create %s", argv[1]);
2992 obj->parent = (IrTcl_Obj *) parentData;
2994 dst = &obj->set_inher;
2995 src = &obj->parent->set_inher;
2997 if ((dst->num_databaseNames = src->num_databaseNames))
2999 dst->databaseNames =
3000 ir_tcl_malloc (sizeof (*dst->databaseNames)
3001 * (1+dst->num_databaseNames));
3002 for (i = 0; i < dst->num_databaseNames; i++)
3003 if (ir_tcl_strdup (interp, &dst->databaseNames[i],
3004 src->databaseNames[i]) == TCL_ERROR)
3006 dst->databaseNames[i] = NULL;
3009 dst->databaseNames = NULL;
3010 if (ir_tcl_strdup (interp, &dst->queryType, src->queryType)
3014 if (ir_tcl_strdup (interp, &dst->referenceId, src->referenceId)
3018 if (ir_tcl_strdup (interp, &dst->elementSetNames, src->elementSetNames)
3022 if (ir_tcl_strdup (interp, &dst->smallSetElementSetNames,
3023 src->smallSetElementSetNames)
3027 if (ir_tcl_strdup (interp, &dst->mediumSetElementSetNames,
3028 src->mediumSetElementSetNames)
3032 if (src->preferredRecordSyntax &&
3033 (dst->preferredRecordSyntax
3034 = ir_tcl_malloc (sizeof(*dst->preferredRecordSyntax))))
3035 *dst->preferredRecordSyntax = *src->preferredRecordSyntax;
3037 dst->preferredRecordSyntax = NULL;
3038 dst->replaceIndicator = src->replaceIndicator;
3039 dst->smallSetUpperBound = src->smallSetUpperBound;
3040 dst->largeSetLowerBound = src->largeSetLowerBound;
3041 dst->mediumSetPresentNumber = src->mediumSetPresentNumber;
3046 tabs[0].tab = ir_set_method_tab;
3050 if (ir_tcl_method (interp, 0, NULL, tabs, NULL) == TCL_ERROR)
3053 *subData = (ClientData) obj;
3058 * ir_set_obj_mk: IR Set Object creation
3060 static int ir_set_obj_mk (ClientData clientData, Tcl_Interp *interp,
3061 int argc, char **argv)
3064 ClientData parentData = 0;
3069 Tcl_CmdInfo parent_info;
3070 if (!Tcl_GetCommandInfo (interp, argv[2], &parent_info))
3072 Tcl_AppendResult (interp, "no object parent", NULL);
3073 return ir_tcl_error_exec (interp, argc, argv);
3075 parentData = parent_info.clientData;
3077 r = ir_set_obj_init (clientData, interp, argc, argv, &subData, parentData);
3080 Tcl_CreateCommand (interp, argv[1], ir_set_obj_method,
3081 subData, ir_set_obj_delete);
3085 IrTcl_Class ir_set_obj_class = {
3092 /* ------------------------------------------------------- */
3095 * do_scan: Perform scan
3097 static int do_scan (void *o, Tcl_Interp *interp, int argc, char **argv)
3101 IrTcl_ScanObj *obj = o;
3102 IrTcl_Obj *p = obj->parent;
3105 struct ccl_rpn_node *rpn;
3113 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
3114 " scanQuery\"", NULL);
3117 logf (LOG_DEBUG, "scan %s %s", *argv, argv[2]);
3118 if (!p->set_inher.num_databaseNames)
3120 Tcl_AppendResult (interp, "no databaseNames", NULL);
3121 return ir_tcl_error_exec (interp, argc, argv);
3125 Tcl_AppendResult (interp, "not connected", NULL);
3126 return ir_tcl_error_exec (interp, argc, argv);
3129 apdu = zget_APDU (p->odr_out, Z_APDU_scanRequest);
3130 req = apdu->u.scanRequest;
3132 set_referenceId (p->odr_out, &req->referenceId, p->set_inher.referenceId);
3133 req->num_databaseNames = p->set_inher.num_databaseNames;
3134 req->databaseNames = p->set_inher.databaseNames;
3137 if (!(req->termListAndStartPoint =
3138 p_query_scan (p->odr_out, p->protocol_type,
3139 &req->attributeSet, argv[2])))
3141 Tcl_AppendResult (interp, "query syntax error", NULL);
3142 return ir_tcl_error_exec (interp, argc, argv);
3145 rpn = ccl_find_str(p->bibset, argv[2], &r, &pos);
3148 Tcl_AppendResult (interp, "ccl syntax error ", ccl_err_msg(r), NULL);
3149 return ir_tcl_error_exec (interp, argc, argv);
3151 bib1.proto = p->protocol_type;
3152 bib1.oclass = CLASS_ATTSET;
3153 bib1.value = VAL_BIB1;
3155 req->attributeSet = oid_getoidbyent (&bib1);
3156 if (!(req->termListAndStartPoint = ccl_scan_query (rpn)))
3159 req->stepSize = &obj->stepSize;
3160 req->numberOfTermsRequested = &obj->numberOfTermsRequested;
3161 req->preferredPositionInResponse = &obj->preferredPositionInResponse;
3162 logf (LOG_DEBUG, "stepSize=%d", *req->stepSize);
3163 logf (LOG_DEBUG, "numberOfTermsRequested=%d",
3164 *req->numberOfTermsRequested);
3165 logf (LOG_DEBUG, "preferredPositionInResponse=%d",
3166 *req->preferredPositionInResponse);
3168 return ir_tcl_send_APDU (interp, p, apdu, "scan", *argv);
3172 * do_scanResponse: add scan response handler
3174 static int do_scanResponse (void *o, Tcl_Interp *interp,
3175 int argc, char **argv)
3177 IrTcl_ScanObj *obj = o;
3181 obj->scanResponse = NULL;
3184 else if (argc == -1)
3185 return ir_tcl_strdel (interp, &obj->scanResponse);
3188 xfree (obj->scanResponse);
3191 if (ir_tcl_strdup (interp, &obj->scanResponse, argv[2])
3196 obj->scanResponse = NULL;
3202 * do_stepSize: Set/get replace Step Size
3204 static int do_stepSize (void *obj, Tcl_Interp *interp,
3205 int argc, char **argv)
3207 IrTcl_ScanObj *p = obj;
3213 return ir_tcl_get_set_int (&p->stepSize, interp, argc, argv);
3217 * do_numberOfTermsRequested: Set/get Number of Terms requested
3219 static int do_numberOfTermsRequested (void *obj, Tcl_Interp *interp,
3220 int argc, char **argv)
3222 IrTcl_ScanObj *p = obj;
3226 p->numberOfTermsRequested = 20;
3229 return ir_tcl_get_set_int (&p->numberOfTermsRequested, interp, argc, argv);
3234 * do_preferredPositionInResponse: Set/get preferred Position
3236 static int do_preferredPositionInResponse (void *obj, Tcl_Interp *interp,
3237 int argc, char **argv)
3239 IrTcl_ScanObj *p = obj;
3243 p->preferredPositionInResponse = 1;
3246 return ir_tcl_get_set_int (&p->preferredPositionInResponse, interp,
3251 * do_scanStatus: Get scan status
3253 static int do_scanStatus (void *obj, Tcl_Interp *interp,
3254 int argc, char **argv)
3256 IrTcl_ScanObj *p = obj;
3260 return ir_tcl_get_set_int (&p->scanStatus, interp, argc, argv);
3264 * do_numberOfEntriesReturned: Get number of Entries returned
3266 static int do_numberOfEntriesReturned (void *obj, Tcl_Interp *interp,
3267 int argc, char **argv)
3269 IrTcl_ScanObj *p = obj;
3273 return ir_tcl_get_set_int (&p->numberOfEntriesReturned, interp,
3278 * do_positionOfTerm: Get position of Term
3280 static int do_positionOfTerm (void *obj, Tcl_Interp *interp,
3281 int argc, char **argv)
3283 IrTcl_ScanObj *p = obj;
3287 return ir_tcl_get_set_int (&p->positionOfTerm, interp, argc, argv);
3291 * do_scanLine: get Scan Line (surrogate or normal) after response
3293 static int do_scanLine (void *obj, Tcl_Interp *interp, int argc, char **argv)
3295 IrTcl_ScanObj *p = obj;
3301 p->entries_flag = 0;
3303 p->nonSurrogateDiagnosticNum = 0;
3304 p->nonSurrogateDiagnosticList = 0;
3307 else if (argc == -1)
3309 p->entries_flag = 0;
3310 /* release entries */
3313 ir_deleteDiags (&p->nonSurrogateDiagnosticList,
3314 &p->nonSurrogateDiagnosticNum);
3319 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
3320 " position\"", NULL);
3323 if (Tcl_GetInt (interp, argv[2], &i) == TCL_ERROR)
3325 if (!p->entries_flag || p->which != Z_ListEntries_entries || !p->entries
3326 || i >= p->num_entries || i < 0)
3328 switch (p->entries[i].which)
3330 case Z_Entry_termInfo:
3331 Tcl_AppendElement (interp, "T");
3332 if (p->entries[i].u.term.buf)
3333 Tcl_AppendElement (interp, p->entries[i].u.term.buf);
3335 Tcl_AppendElement (interp, "");
3336 sprintf (numstr, "%d", p->entries[i].u.term.globalOccurrences);
3337 Tcl_AppendElement (interp, numstr);
3339 case Z_Entry_surrogateDiagnostic:
3340 Tcl_AppendElement (interp, "SD");
3341 return ir_diagResult (interp, p->entries[i].u.diag.list,
3342 p->entries[i].u.diag.num);
3348 static IrTcl_Method ir_scan_method_tab[] = {
3349 { "scan", do_scan, NULL},
3350 { "scanResponse", do_scanResponse, NULL},
3351 { "stepSize", do_stepSize, NULL},
3352 { "numberOfTermsRequested", do_numberOfTermsRequested, NULL},
3353 { "preferredPositionInResponse", do_preferredPositionInResponse, NULL},
3354 { "scanStatus", do_scanStatus, NULL},
3355 { "numberOfEntriesReturned", do_numberOfEntriesReturned, NULL},
3356 { "positionOfTerm", do_positionOfTerm, NULL},
3357 { "scanLine", do_scanLine, NULL},
3362 * ir_scan_obj_method: IR Scan Object methods
3364 static int ir_scan_obj_method (ClientData clientData, Tcl_Interp *interp,
3365 int argc, char **argv)
3367 IrTcl_Methods tabs[2];
3372 Tcl_AppendResult (interp, wrongArgs, *argv, " method args...\"", NULL);
3375 tabs[0].tab = ir_scan_method_tab;
3376 tabs[0].obj = clientData;
3379 if (ir_tcl_method (interp, argc, argv, tabs, &r) == TCL_ERROR)
3380 return ir_tcl_method_error (interp, argc, argv, tabs);
3385 * ir_scan_obj_delete: IR Scan Object disposal
3387 static void ir_scan_obj_delete (ClientData clientData)
3389 IrTcl_Methods tabs[2];
3390 IrTcl_ScanObj *obj = (IrTcl_ScanObj *) clientData;
3392 tabs[0].tab = ir_scan_method_tab;
3396 ir_tcl_method (NULL, -1, NULL, tabs, NULL);
3401 * ir_scan_obj_mk: IR Scan Object creation
3403 static int ir_scan_obj_mk (ClientData clientData, Tcl_Interp *interp,
3404 int argc, char **argv)
3406 Tcl_CmdInfo parent_info;
3408 IrTcl_Methods tabs[2];
3412 Tcl_AppendResult (interp, wrongArgs, *argv,
3413 "objScanName objParentName\"", NULL);
3416 logf (LOG_DEBUG, "ir scan create %s", argv[1]);
3417 if (!Tcl_GetCommandInfo (interp, argv[2], &parent_info))
3419 Tcl_AppendResult (interp, "no object parent", NULL);
3420 return ir_tcl_error_exec (interp, argc, argv);
3422 obj = ir_tcl_malloc (sizeof(*obj));
3423 obj->parent = (IrTcl_Obj *) parent_info.clientData;
3425 tabs[0].tab = ir_scan_method_tab;
3429 if (ir_tcl_method (interp, 0, NULL, tabs, NULL) == TCL_ERROR)
3431 Tcl_CreateCommand (interp, argv[1], ir_scan_obj_method,
3432 (ClientData) obj, ir_scan_obj_delete);
3436 /* ------------------------------------------------------- */
3439 * ir_log_init_proc: set yaz log level
3441 static int ir_log_init_proc (ClientData clientData, Tcl_Interp *interp,
3442 int argc, char **argv)
3444 if (argc <= 1 || argc > 4)
3446 Tcl_AppendResult (interp, wrongArgs, *argv,
3447 " ?level ?prefix ?filename\"", NULL);
3451 log_init (log_mask_str (argv[1]), "", NULL);
3453 log_init (log_mask_str (argv[1]), argv[2], NULL);
3455 log_init (log_mask_str (argv[1]), argv[2], argv[3]);
3460 * ir_log_proc: log yaz message
3462 static int ir_log_proc (ClientData clientData, Tcl_Interp *interp,
3463 int argc, char **argv)
3468 Tcl_AppendResult (interp, wrongArgs, *argv,
3469 " level string\"", NULL);
3472 mask = log_mask_str_x (argv[1], 0);
3473 logf (mask, "%s", argv[1], mask, argv[2]);
3478 /* ------------------------------------------------------- */
3479 static void ir_initResponse (void *obj, Z_InitResponse *initrs)
3483 p->initResult = *initrs->result ? 1 : 0;
3484 if (!*initrs->result)
3485 logf (LOG_DEBUG, "Connection rejected by target");
3487 logf (LOG_DEBUG, "Connection accepted by target");
3489 get_referenceId (&p->set_inher.referenceId, initrs->referenceId);
3491 xfree (p->targetImplementationId);
3492 ir_tcl_strdup (p->interp, &p->targetImplementationId,
3493 initrs->implementationId);
3494 xfree (p->targetImplementationName);
3495 ir_tcl_strdup (p->interp, &p->targetImplementationName,
3496 initrs->implementationName);
3497 xfree (p->targetImplementationVersion);
3498 ir_tcl_strdup (p->interp, &p->targetImplementationVersion,
3499 initrs->implementationVersion);
3501 p->maximumRecordSize = *initrs->maximumRecordSize;
3502 p->preferredMessageSize = *initrs->preferredMessageSize;
3504 memcpy (&p->options, initrs->options, sizeof(initrs->options));
3505 memcpy (&p->protocolVersion, initrs->protocolVersion,
3506 sizeof(initrs->protocolVersion));
3507 xfree (p->userInformationField);
3508 p->userInformationField = NULL;
3509 if (initrs->userInformationField)
3513 if (initrs->userInformationField->which == ODR_EXTERNAL_octet &&
3514 (p->userInformationField =
3515 ir_tcl_malloc ((len =
3516 initrs->userInformationField->
3517 u.octet_aligned->len) +1)))
3519 memcpy (p->userInformationField,
3520 initrs->userInformationField->u.octet_aligned->buf,
3522 (p->userInformationField)[len] = '\0';
3527 static void ir_deleteDiags (IrTcl_Diagnostic **dst_list, int *dst_num)
3530 for (i = 0; i<*dst_num; i++)
3531 xfree (dst_list[i]->addinfo);
3537 static void ir_handleDiags (IrTcl_Diagnostic **dst_list, int *dst_num,
3538 Z_DiagRec **list, int num)
3544 *dst_list = ir_tcl_malloc (sizeof(**dst_list) * num);
3545 for (i = 0; i<num; i++)
3548 switch (list[i]->which)
3550 case Z_DiagRec_defaultFormat:
3551 (*dst_list)[i].condition = *list[i]->u.defaultFormat->condition;
3552 addinfo = list[i]->u.defaultFormat->addinfo;
3554 ((*dst_list)[i].addinfo = ir_tcl_malloc (strlen(addinfo)+1)))
3555 strcpy ((*dst_list)[i].addinfo, addinfo);
3556 cp = diagbib1_str ((*dst_list)[i].condition);
3557 logf (LOG_DEBUG, "Diag %d %s %s", (*dst_list)[i].condition,
3558 cp ? cp : "", addinfo ? addinfo : "");
3561 (*dst_list)[i].addinfo = NULL;
3562 (*dst_list)[i].condition = 0;
3567 static void ir_handleDBRecord (IrTcl_Obj *p, IrTcl_RecordList *rl,
3570 struct oident *ident;
3571 Z_ext_typeent *etype;
3573 rl->u.dbrec.size = oe->u.octet_aligned->len;
3574 rl->u.dbrec.buf = NULL;
3576 if ((ident = oid_getentbyoid (oe->direct_reference)))
3577 rl->u.dbrec.type = ident->value;
3579 rl->u.dbrec.type = VAL_USMARC;
3581 if (ident && (oe->which == Z_External_single ||
3582 oe->which == Z_External_octet)
3583 && (etype = z_ext_getentbyref (ident->value)))
3587 odr_setbuf (p->odr_in, (char*) oe->u.octet_aligned->buf,
3588 oe->u.octet_aligned->len, 0);
3589 if (!(*etype->fun)(p->odr_in, &rr, 0))
3591 switch (etype->what)
3593 case Z_External_sutrs:
3594 logf (LOG_DEBUG, "Z_External_sutrs");
3596 if ((rl->u.dbrec.buf = ir_tcl_malloc (oe->u.sutrs->len+1)))
3598 memcpy (rl->u.dbrec.buf, oe->u.sutrs->buf,
3600 rl->u.dbrec.buf[oe->u.sutrs->len] = '\0';
3602 rl->u.dbrec.size = oe->u.sutrs->len;
3604 case Z_External_grs1:
3605 logf (LOG_DEBUG, "Z_External_grs1");
3607 ir_tcl_grs_mk (oe->u.grs1, &rl->u.dbrec.u.grs1);
3609 case Z_External_explainRecord:
3610 logf (LOG_DEBUG, "Z_External_explainRecord");
3611 if ((rl->u.dbrec.buf = ir_tcl_malloc (rl->u.dbrec.size)))
3613 memcpy (rl->u.dbrec.buf, oe->u.octet_aligned->buf,
3621 if (oe->which == Z_External_octet && rl->u.dbrec.size > 0)
3623 char *buf = (char*) oe->u.octet_aligned->buf;
3624 if ((rl->u.dbrec.buf = ir_tcl_malloc (rl->u.dbrec.size)))
3625 memcpy (rl->u.dbrec.buf, buf, rl->u.dbrec.size);
3627 else if (rl->u.dbrec.type == VAL_SUTRS &&
3628 oe->which == Z_External_sutrs)
3630 if ((rl->u.dbrec.buf = ir_tcl_malloc (oe->u.sutrs->len+1)))
3632 memcpy (rl->u.dbrec.buf, oe->u.sutrs->buf,
3634 rl->u.dbrec.buf[oe->u.sutrs->len] = '\0';
3636 rl->u.dbrec.size = oe->u.sutrs->len;
3638 else if (rl->u.dbrec.type == VAL_GRS1 &&
3639 oe->which == Z_External_grs1)
3641 ir_tcl_grs_mk (oe->u.grs1, &rl->u.dbrec.u.grs1);
3646 static void ir_handleZRecords (void *o, Z_Records *zrs, IrTcl_SetObj *setobj,
3647 const char *elements)
3652 IrTcl_RecordList *rl;
3654 setobj->which = zrs->which;
3655 setobj->recordFlag = 1;
3657 ir_deleteDiags (&setobj->nonSurrogateDiagnosticList,
3658 &setobj->nonSurrogateDiagnosticNum);
3659 if (zrs->which == Z_Records_DBOSD)
3661 setobj->numberOfRecordsReturned =
3662 zrs->u.databaseOrSurDiagnostics->num_records;
3663 logf (LOG_DEBUG, "Got %d records", setobj->numberOfRecordsReturned);
3664 for (offset = 0; offset < setobj->numberOfRecordsReturned; offset++)
3666 Z_NamePlusRecord *znpr = zrs->u.databaseOrSurDiagnostics->
3669 rl = new_IR_record (setobj, setobj->start + offset, znpr->which,
3671 if (rl->which == Z_NamePlusRecord_surrogateDiagnostic)
3672 ir_handleDiags (&rl->u.surrogateDiagnostics.list,
3673 &rl->u.surrogateDiagnostics.num,
3674 &znpr->u.surrogateDiagnostic,
3677 ir_handleDBRecord (p, rl,
3678 (Z_External*) (znpr->u.databaseRecord));
3681 else if (zrs->which == Z_Records_multipleNSD)
3683 logf (LOG_DEBUG, "multipleNonSurrogateDiagnostic %d",
3684 zrs->u.multipleNonSurDiagnostics->num_diagRecs);
3685 setobj->numberOfRecordsReturned = 0;
3686 ir_handleDiags (&setobj->nonSurrogateDiagnosticList,
3687 &setobj->nonSurrogateDiagnosticNum,
3688 zrs->u.multipleNonSurDiagnostics->diagRecs,
3689 zrs->u.multipleNonSurDiagnostics->num_diagRecs);
3693 logf (LOG_DEBUG, "NonSurrogateDiagnostic");
3694 setobj->numberOfRecordsReturned = 0;
3695 ir_handleDiags (&setobj->nonSurrogateDiagnosticList,
3696 &setobj->nonSurrogateDiagnosticNum,
3697 &zrs->u.nonSurrogateDiagnostic,
3702 static void ir_searchResponse (void *o, Z_SearchResponse *searchrs,
3703 IrTcl_SetObj *setobj)
3705 Z_Records *zrs = searchrs->records;
3707 logf (LOG_DEBUG, "Received search response");
3710 logf (LOG_DEBUG, "Search response, no object!");
3713 setobj->searchStatus = searchrs->searchStatus ? 1 : 0;
3714 get_referenceId (&setobj->set_inher.referenceId, searchrs->referenceId);
3715 setobj->resultCount = *searchrs->resultCount;
3716 if (searchrs->presentStatus)
3717 setobj->presentStatus = *searchrs->presentStatus;
3718 if (searchrs->nextResultSetPosition)
3719 setobj->nextResultSetPosition = *searchrs->nextResultSetPosition;
3721 logf (LOG_DEBUG, "status %d hits %d",
3722 setobj->searchStatus, setobj->resultCount);
3726 if (setobj->resultCount <= setobj->set_inher.smallSetUpperBound)
3727 es = setobj->set_inher.smallSetElementSetNames;
3729 es = setobj->set_inher.mediumSetElementSetNames;
3730 ir_handleZRecords (o, zrs, setobj, es);
3733 setobj->recordFlag = 0;
3737 static void ir_presentResponse (void *o, Z_PresentResponse *presrs,
3738 IrTcl_SetObj *setobj)
3740 Z_Records *zrs = presrs->records;
3742 logf (LOG_DEBUG, "Received present response");
3745 logf (LOG_DEBUG, "Present response, no object!");
3748 setobj->presentStatus = *presrs->presentStatus;
3749 get_referenceId (&setobj->set_inher.referenceId, presrs->referenceId);
3750 setobj->nextResultSetPosition = *presrs->nextResultSetPosition;
3752 ir_handleZRecords (o, zrs, setobj, setobj->set_inher.elementSetNames);
3755 setobj->recordFlag = 0;
3756 logf (LOG_DEBUG, "No records!");
3760 static void ir_scanResponse (void *o, Z_ScanResponse *scanrs,
3761 IrTcl_ScanObj *scanobj)
3765 logf (LOG_DEBUG, "Received scanResponse");
3767 get_referenceId (&p->set_inher.referenceId, scanrs->referenceId);
3768 scanobj->scanStatus = *scanrs->scanStatus;
3769 logf (LOG_DEBUG, "scanStatus=%d", scanobj->scanStatus);
3771 if (scanrs->stepSize)
3772 scanobj->stepSize = *scanrs->stepSize;
3773 logf (LOG_DEBUG, "stepSize=%d", scanobj->stepSize);
3775 scanobj->numberOfEntriesReturned = *scanrs->numberOfEntriesReturned;
3776 logf (LOG_DEBUG, "numberOfEntriesReturned=%d",
3777 scanobj->numberOfEntriesReturned);
3779 if (scanrs->positionOfTerm)
3780 scanobj->positionOfTerm = *scanrs->positionOfTerm;
3782 scanobj->positionOfTerm = -1;
3783 logf (LOG_DEBUG, "positionOfTerm=%d", scanobj->positionOfTerm);
3785 xfree (scanobj->entries);
3786 scanobj->entries = NULL;
3788 ir_deleteDiags (&scanobj->nonSurrogateDiagnosticList,
3789 &scanobj->nonSurrogateDiagnosticNum);
3790 if (scanrs->entries)
3795 scanobj->entries_flag = 1;
3796 scanobj->which = scanrs->entries->which;
3797 switch (scanobj->which)
3799 case Z_ListEntries_entries:
3800 scanobj->num_entries = scanrs->entries->u.entries->num_entries;
3801 scanobj->entries = ir_tcl_malloc (scanobj->num_entries *
3802 sizeof(*scanobj->entries));
3803 for (i=0; i<scanobj->num_entries; i++)
3805 ze = scanrs->entries->u.entries->entries[i];
3806 scanobj->entries[i].which = ze->which;
3809 case Z_Entry_termInfo:
3810 if (ze->u.termInfo->term->which == Z_Term_general)
3812 int l = ze->u.termInfo->term->u.general->len;
3813 scanobj->entries[i].u.term.buf = ir_tcl_malloc (1+l);
3814 memcpy (scanobj->entries[i].u.term.buf,
3815 ze->u.termInfo->term->u.general->buf,
3817 scanobj->entries[i].u.term.buf[l] = '\0';
3820 scanobj->entries[i].u.term.buf = NULL;
3821 if (ze->u.termInfo->globalOccurrences)
3822 scanobj->entries[i].u.term.globalOccurrences =
3823 *ze->u.termInfo->globalOccurrences;
3825 scanobj->entries[i].u.term.globalOccurrences = 0;
3827 case Z_Entry_surrogateDiagnostic:
3828 ir_handleDiags (&scanobj->entries[i].u.diag.list,
3829 &scanobj->entries[i].u.diag.num,
3830 &ze->u.surrogateDiagnostic,
3836 case Z_ListEntries_nonSurrogateDiagnostics:
3837 ir_handleDiags (&scanobj->nonSurrogateDiagnosticList,
3838 &scanobj->nonSurrogateDiagnosticNum,
3839 scanrs->entries->u.nonSurrogateDiagnostics->
3841 scanrs->entries->u.nonSurrogateDiagnostics->
3847 scanobj->entries_flag = 0;
3851 * ir_select_read: handle incoming packages
3853 static void ir_select_read (ClientData clientData)
3855 IrTcl_Obj *p = (IrTcl_Obj *) clientData;
3860 Tcl_CmdInfo cmd_info;
3861 const char *apdu_call;
3863 logf(LOG_DEBUG, "Read handler fd=%d", cs_fileno(p->cs_link));
3864 if (p->state == IR_TCL_R_Connecting)
3866 logf(LOG_DEBUG, "read: connect");
3867 r = cs_rcvconnect (p->cs_link);
3870 logf (LOG_WARN, "cs_rcvconnect returned 1");
3873 p->state = IR_TCL_R_Idle;
3875 ir_select_remove_write (cs_fileno (p->cs_link), p);
3878 logf (LOG_DEBUG, "cs_rcvconnect error");
3879 ir_tcl_disconnect (p);
3882 p->failInfo = IR_TCL_FAIL_CONNECT;
3883 ir_tcl_eval (p->interp, p->failback);
3885 ir_obj_delete ((ClientData) p);
3889 ir_tcl_eval (p->interp, p->callback);
3890 if (p->ref_count == 2 && p->cs_link && p->request_queue
3891 && p->state == IR_TCL_R_Idle)
3892 ir_tcl_send_q (p, p->request_queue, "x");
3893 ir_obj_delete ((ClientData) p);
3898 p->state = IR_TCL_R_Reading;
3900 /* read incoming APDU */
3901 if ((r=cs_get (p->cs_link, &p->buf_in, &p->len_in)) == 1)
3903 logf(LOG_DEBUG, "PDU Fraction read");
3906 /* signal one more use of ir object - callbacks must not
3907 release the ir memory (p pointer) */
3911 logf (LOG_DEBUG, "cs_get failed, code %d", r);
3912 ir_tcl_disconnect (p);
3915 p->failInfo = IR_TCL_FAIL_READ;
3916 ir_tcl_eval (p->interp, p->failback);
3918 /* release ir object now if callback deleted it */
3919 ir_obj_delete ((ClientData) p);
3922 /* got complete APDU. Now decode */
3925 odr_setbuf (p->odr_in, p->buf_in, r, 0);
3926 logf (LOG_DEBUG, "cs_get ok, total size %d", r);
3927 if (!z_APDU (p->odr_in, &apdu, 0))
3929 logf (LOG_DEBUG, "cs_get failed: %s",
3930 odr_errmsg (odr_geterror (p->odr_in)));
3931 ir_tcl_disconnect (p);
3934 p->failInfo = IR_TCL_FAIL_IN_APDU;
3935 p->apduOffset = odr_offset (p->odr_in);
3936 ir_tcl_eval (p->interp, p->failback);
3938 /* release ir object now if failback deleted it */
3939 ir_obj_delete ((ClientData) p);
3942 /* handle APDU and invoke callback */
3943 rq = p->request_queue;
3946 logf (LOG_FATAL, "Internal error. No queue entry");
3949 object_name = rq->object_name;
3950 logf (LOG_DEBUG, "Object %s", object_name);
3952 if (Tcl_GetCommandInfo (p->interp, object_name, &cmd_info))
3956 case Z_APDU_initResponse:
3957 p->eventType = "init";
3958 ir_initResponse (p, apdu->u.initResponse);
3959 apdu_call = p->initResponse;
3961 case Z_APDU_searchResponse:
3962 p->eventType = "search";
3963 ir_searchResponse (p, apdu->u.searchResponse,
3964 (IrTcl_SetObj *) cmd_info.clientData);
3965 apdu_call = ((IrTcl_SetObj *)
3966 cmd_info.clientData)->searchResponse;
3968 case Z_APDU_presentResponse:
3969 p->eventType = "present";
3970 ir_presentResponse (p, apdu->u.presentResponse,
3971 (IrTcl_SetObj *) cmd_info.clientData);
3972 apdu_call = ((IrTcl_SetObj *)
3973 cmd_info.clientData)->presentResponse;
3975 case Z_APDU_scanResponse:
3976 p->eventType = "scan";
3977 ir_scanResponse (p, apdu->u.scanResponse,
3978 (IrTcl_ScanObj *) cmd_info.clientData);
3979 apdu_call = ((IrTcl_ScanObj *)
3980 cmd_info.clientData)->scanResponse;
3983 logf (LOG_WARN, "Received unknown APDU type (%d)",
3985 ir_tcl_disconnect (p);
3988 p->failInfo = IR_TCL_FAIL_UNKNOWN_APDU;
3989 ir_tcl_eval (p->interp, p->failback);
3994 p->request_queue = rq->next;
3995 p->state = IR_TCL_R_Idle;
3998 ir_tcl_eval (p->interp, apdu_call);
3999 else if (rq->callback)
4000 ir_tcl_eval (p->interp, rq->callback);
4001 xfree (rq->buf_out);
4002 xfree (rq->callback);
4003 xfree (rq->object_name);
4005 odr_reset (p->odr_in);
4006 if (p->ref_count == 1)
4008 ir_obj_delete ((ClientData) p);
4011 ir_obj_delete ((ClientData) p);
4012 } while (p->cs_link && cs_more (p->cs_link));
4013 if (p->cs_link && p->request_queue && p->state == IR_TCL_R_Idle)
4014 ir_tcl_send_q (p, p->request_queue, "x");
4018 * ir_select_write: handle outgoing packages - not yet written.
4020 static int ir_select_write (ClientData clientData)
4022 IrTcl_Obj *p = (IrTcl_Obj *) clientData;
4026 logf (LOG_DEBUG, "Write handler fd=%d", cs_fileno(p->cs_link));
4027 if (p->state == IR_TCL_R_Connecting)
4029 logf(LOG_DEBUG, "write: connect");
4030 r = cs_rcvconnect (p->cs_link);
4033 logf (LOG_DEBUG, "cs_rcvconnect returned 1");
4036 p->state = IR_TCL_R_Idle;
4038 ir_select_remove_write (cs_fileno (p->cs_link), p);
4041 logf (LOG_DEBUG, "cs_rcvconnect error");
4042 ir_tcl_disconnect (p);
4045 p->failInfo = IR_TCL_FAIL_CONNECT;
4046 ir_tcl_eval (p->interp, p->failback);
4048 ir_obj_delete ((ClientData) p);
4052 ir_tcl_eval (p->interp, p->callback);
4053 ir_obj_delete ((ClientData) p);
4056 rq = p->request_queue;
4057 if (!rq || !rq->buf_out)
4060 if ((r=cs_put (p->cs_link, rq->buf_out, rq->len_out)) < 0)
4062 logf (LOG_DEBUG, "cs_put write fail");
4064 xfree (rq->buf_out);
4066 ir_tcl_disconnect (p);
4069 p->failInfo = IR_TCL_FAIL_WRITE;
4070 ir_tcl_eval (p->interp, p->failback);
4072 ir_obj_delete ((ClientData) p);
4074 else if (r == 0) /* remove select bit */
4076 logf (LOG_DEBUG, "Write completed");
4077 p->state = IR_TCL_R_Waiting;
4078 ir_select_remove_write (cs_fileno (p->cs_link), p);
4079 xfree (rq->buf_out);
4085 static void ir_select_notify (ClientData clientData, int r, int w, int e)
4089 if (!ir_select_write (clientData) && r)
4090 ir_select_read (clientData);
4094 ir_select_read (clientData);
4098 /*----------------------------------------------------------- */
4102 * This wrapper function is used by Windows to invoke the
4103 * initialization code for the DLL. If we are compiling
4104 * with Visual C++, this routine will be renamed to DllMain.
4116 DllEntryPoint(hInst, reason, reserved)
4117 HINSTANCE hInst; /* Library instance handle. */
4118 DWORD reason; /* Reason this function is being called. */
4119 LPVOID reserved; /* Not used. */
4125 /* ------------------------------------------------------- */
4127 * Irtcl_init: Registration of TCL commands.
4129 EXPORT (int,Irtcl_Init) (Tcl_Interp *interp)
4131 Tcl_CreateCommand (interp, "ir", ir_obj_mk, (ClientData) NULL,
4132 (Tcl_CmdDeleteProc *) NULL);
4133 Tcl_CreateCommand (interp, "ir-set", ir_set_obj_mk,
4134 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
4135 Tcl_CreateCommand (interp, "ir-scan", ir_scan_obj_mk,
4136 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
4137 Tcl_CreateCommand (interp, "ir-log-init", ir_log_init_proc,
4138 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
4139 Tcl_CreateCommand (interp, "ir-log", ir_log_proc,
4140 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);