2 * IR toolkit for tcl/tk
3 * (c) Index Data 1995-1998
4 * See the file LICENSE for details.
5 * Sebastian Hammer, Adam Dickmeiss
8 * Revision 1.104 1998-02-27 14:26:07 adam
9 * Changed client so that it still works if target sets numberOfRecords
10 * in response to an illegal value.
12 * Revision 1.103 1997/11/19 11:22:10 adam
13 * Object identifiers can be accessed in GRS-1 records.
15 * Revision 1.102 1997/09/17 12:22:40 adam
16 * Changed to use YAZ version 1.4. The new comstack utility, cs_straddr,
19 * Revision 1.101 1997/09/09 10:19:53 adam
20 * New MSV5.0 port with fewer warnings.
22 * Revision 1.100 1997/05/01 15:04:05 adam
23 * Added ir-log command.
25 * Revision 1.99 1997/04/30 07:24:47 adam
26 * Spell fix of an error message.
28 * Revision 1.98 1997/04/13 18:57:20 adam
29 * Better error reporting and aligned with Tcl/Tk style.
30 * Rework of notifier code with Tcl_File handles.
32 * Revision 1.97 1996/11/14 17:11:07 adam
33 * Added Explain documentaion.
35 * Revision 1.96 1996/10/08 13:02:50 adam
36 * When dealing with records, odr_choice_enable_bias function is used to
37 * prevent decoding of externals.
39 * Revision 1.95 1996/09/13 10:51:49 adam
40 * Bug fix: ir_tcl_select_set called Tcl_GetFile at disconnect.
42 * Revision 1.94 1996/08/21 13:32:53 adam
43 * Implemented saveFile method and extended loadFile method to work with it.
45 * Revision 1.93 1996/08/16 15:07:45 adam
46 * First work on Explain.
48 * Revision 1.92 1996/08/09 15:33:07 adam
49 * Modified the code to use tk4.1/tcl7.5 patch level 1. The time-driven
50 * polling is no longer activated on Windows since asynchrounous I/O works
53 * Revision 1.91 1996/07/03 13:31:11 adam
54 * The xmalloc/xfree functions from YAZ are used to manage memory.
56 * Revision 1.90 1996/06/27 14:21:00 adam
57 * Yet another Windows port.
59 * Revision 1.89 1996/06/11 15:27:15 adam
60 * Event type set to connect a little earlier in the do_connect function.
62 * Revision 1.88 1996/06/03 09:04:22 adam
63 * Changed a few logf calls.
65 * Revision 1.87 1996/05/29 06:37:51 adam
66 * Function ir_tcl_get_grs_r enhanced so that specific elements can be
69 * Revision 1.86 1996/03/20 13:54:04 adam
70 * The Tcl_File structure is only manipulated in the Tk-event interface
73 * Revision 1.85 1996/03/15 11:15:48 adam
74 * Modified to use new prototypes for p_query_rpn and p_query_scan.
76 * Revision 1.84 1996/03/07 12:42:49 adam
77 * Better logging when callback is invoked.
79 * Revision 1.83 1996/03/05 09:21:09 adam
80 * Bug fix: memory used by GRS records wasn't freed.
81 * Rewrote some of the error handling code - the connection is always
82 * closed before failback is called.
83 * If failback is defined the send APDU methods (init, search, ...) will
84 * return OK but invoke failback (as is the case if the write operation
86 * Bug fix: ref_count in assoc object could grow if fraction of PDU was
89 * Revision 1.82 1996/02/29 15:30:21 adam
90 * Export of IrTcl functionality to extensions.
92 * Revision 1.81 1996/02/26 18:38:32 adam
93 * Work on export of set methods.
95 * Revision 1.80 1996/02/23 17:31:39 adam
96 * More functions made available to the wais tcl extension.
98 * Revision 1.79 1996/02/23 13:41:38 adam
99 * Work on public access to simple ir class system.
101 * Revision 1.78 1996/02/21 10:16:08 adam
102 * Simplified select handling. Only one function ir_tcl_select_set has
103 * to be externally defined.
105 * Revision 1.77 1996/02/20 17:52:58 adam
106 * Uses the YAZ oid system to name record syntax object identifiers.
108 * Revision 1.76 1996/02/20 16:09:51 adam
109 * Bug fix: didn't set element set names stamp correctly on result
110 * set records when element set names were set to the empty string.
112 * Revision 1.75 1996/02/19 15:41:53 adam
113 * Better log messages.
114 * Minor improvement of connect method.
116 * Revision 1.74 1996/02/05 17:58:03 adam
117 * Ported ir-tcl to use the beta releases of tcl7.5/tk4.1.
119 * Revision 1.73 1996/01/29 11:35:19 adam
120 * Bug fix: cs_type member renamed to comstackType to avoid conflict with
121 * cs_type macro defined by YAZ.
123 * Revision 1.72 1996/01/19 17:45:34 quinn
124 * Added debugging output
126 * Revision 1.71 1996/01/19 16:22:38 adam
127 * New method: apduDump - returns information about last incoming APDU.
129 * Revision 1.70 1996/01/10 09:18:34 adam
130 * PDU specific callbacks implemented: initRespnse, searchResponse,
131 * presentResponse and scanResponse.
132 * Bug fix in the command line shell (tclmain.c) - discovered on OSF/1.
134 * Revision 1.69 1996/01/04 16:12:12 adam
135 * Setting PDUType renamed to eventType.
137 * Revision 1.68 1996/01/04 11:05:22 adam
138 * New setting: PDUType - returns type of last PDU returned from the target.
139 * Fixed a bug in configure/Makefile.
141 * Revision 1.67 1996/01/03 09:00:51 adam
142 * Updated to use new version of Yaz (names changed to avoid C++ conflict).
144 * Revision 1.66 1995/11/28 17:26:39 adam
145 * Removed Carriage return from ir-tcl.c!
146 * Removed misc. debug logs.
148 * Revision 1.65 1995/11/28 13:53:00 quinn
151 * Revision 1.64 1995/11/13 15:39:18 adam
152 * Bug fix: {small,medium}SetElementSetNames weren't set correctly.
153 * Bug fix: idAuthentication weren't set correctly.
155 * Revision 1.63 1995/11/13 09:55:39 adam
156 * Multiple records at a position in a result-set with differnt
159 * Revision 1.62 1995/10/18 17:20:33 adam
160 * Work on target setup in client.tcl.
162 * Revision 1.61 1995/10/18 16:42:42 adam
163 * New settings: smallSetElementSetNames and mediumSetElementSetNames.
165 * Revision 1.60 1995/10/18 15:43:31 adam
166 * In search: mediumSetElementSetNames and smallSetElementSetNames are
167 * set to elementSetNames.
169 * Revision 1.59 1995/10/17 12:18:58 adam
170 * Bug fix: when target connection closed, the connection was not
171 * properly reestablished.
173 * Revision 1.58 1995/10/16 17:00:55 adam
174 * New setting: elementSetNames.
175 * Various client improvements. Medium presentation format looks better.
177 * Revision 1.57 1995/09/21 13:11:51 adam
178 * Support of dynamic loading.
179 * Test script uses load command if necessary.
181 * Revision 1.56 1995/08/29 15:30:14 adam
182 * Work on GRS records.
184 * Revision 1.55 1995/08/28 09:43:25 adam
185 * Minor changes. configure only searches for yaz beta 3 and versions after
188 * Revision 1.54 1995/08/24 12:25:16 adam
189 * Modified to work with yaz 1.0b3.
191 * Revision 1.53 1995/08/04 12:49:26 adam
192 * Bug fix: reading uninitialized variable p.
194 * Revision 1.52 1995/08/04 11:32:38 adam
195 * More work on output queue. Memory related routines moved
198 * Revision 1.51 1995/08/03 13:22:54 adam
201 * Revision 1.50 1995/07/20 08:09:49 adam
202 * client.tcl: Targets removed from hotTargets list when targets
203 * are removed/modified.
204 * ir-tcl.c: More work on triggerResourceControl.
206 * Revision 1.49 1995/06/30 12:39:21 adam
207 * Bug fix: loadFile didn't set record type.
208 * The MARC routines are a little less strict in the interpretation.
209 * Script display.tcl replaces the old marc.tcl.
210 * New interactive script: shell.tcl.
212 * Revision 1.48 1995/06/27 19:03:50 adam
213 * Bug fix in do_present in ir-tcl.c: p->set_child member weren't set.
214 * nextResultSetPosition used instead of setOffset.
216 * Revision 1.47 1995/06/25 10:25:04 adam
217 * Working on triggerResourceControl. Description of compile/install
218 * procedure moved to ir-tcl.sgml.
220 * Revision 1.46 1995/06/22 13:15:06 adam
221 * Feature: SUTRS. Setting getSutrs implemented.
222 * Work on display formats.
223 * Preferred record syntax can be set by the user.
225 * Revision 1.45 1995/06/20 08:07:30 adam
226 * New setting: failInfo.
227 * Working on better cancel mechanism.
229 * Revision 1.44 1995/06/19 17:01:20 adam
232 * Revision 1.43 1995/06/19 13:06:08 adam
233 * New define: IR_TCL_VERSION.
235 * Revision 1.42 1995/06/19 08:08:52 adam
236 * client.tcl: hotTargets now contain both database and target name.
237 * ir-tcl.c: setting protocol edited. Errors in callbacks are logged
238 * by logf(LOG_WARN, ...) calls.
240 * Revision 1.41 1995/06/16 12:28:16 adam
241 * Implemented preferredRecordSyntax.
242 * Minor changes in diagnostic handling.
243 * Record list deleted when connection closes.
245 * Revision 1.40 1995/06/14 13:37:18 adam
246 * Setting recordType implemented.
247 * Setting implementationVersion implemented.
248 * Settings implementationId / implementationName edited.
250 * Revision 1.39 1995/06/08 10:26:32 adam
251 * Bug fix in ir_strdup.
253 * Revision 1.38 1995/06/01 16:36:47 adam
254 * About buttons. Minor bug fixes.
256 * Revision 1.37 1995/06/01 07:31:20 adam
257 * Rename of many typedefs -> IrTcl_...
259 * Revision 1.36 1995/05/31 13:09:59 adam
260 * Client searches/presents may be interrupted.
261 * New moving book-logo.
263 * Revision 1.35 1995/05/31 08:36:33 adam
264 * Bug fix in client.tcl: didn't save options on clientrc.tcl.
265 * New method: referenceId. More work on scan.
267 * Revision 1.34 1995/05/29 10:33:42 adam
268 * README and rename of startup script.
270 * Revision 1.33 1995/05/29 09:15:11 quinn
271 * Changed CS_SR to PROTO_SR, etc.
273 * Revision 1.32 1995/05/29 08:44:16 adam
274 * Work on delete of objects.
276 * Revision 1.31 1995/05/26 11:44:10 adam
277 * Bugs fixed. More work on MARC utilities and queries. Test
278 * client is up-to-date again.
280 * Revision 1.30 1995/05/26 08:54:11 adam
281 * New MARC utilities. Uses prefix query.
283 * Revision 1.29 1995/05/24 14:10:22 adam
284 * Work on idAuthentication, protocolVersion and options.
286 * Revision 1.28 1995/05/23 15:34:48 adam
287 * Many new settings, userInformationField, smallSetUpperBound, etc.
288 * A number of settings are inherited when ir-set is executed.
289 * This version is incompatible with the graphical test client (client.tcl).
291 * Revision 1.27 1995/05/11 15:34:47 adam
292 * Scan request changed a bit. This version works with RLG.
294 * Revision 1.26 1995/04/18 16:11:51 adam
295 * First version of graphical Scan. Some work on query-by-form.
297 * Revision 1.25 1995/04/17 09:37:17 adam
298 * Further development of scan.
300 * Revision 1.24 1995/04/11 14:16:42 adam
301 * Further work on scan. Response works. Entries aren't saved yet.
303 * Revision 1.23 1995/04/10 10:50:27 adam
304 * Result-set name defaults to suffix of ir-set name.
305 * Started working on scan. Not finished at this point.
307 * Revision 1.22 1995/03/31 10:43:03 adam
308 * More robust when getting bad MARC records.
310 * Revision 1.21 1995/03/31 08:56:37 adam
311 * New button "Search".
313 * Revision 1.20 1995/03/29 16:07:09 adam
314 * Bug fix: Didn't use setName in present request.
316 * Revision 1.19 1995/03/28 12:45:23 adam
317 * New ir method failback: called on disconnect/protocol error.
318 * New ir set/get method: protocol: SR / Z3950.
319 * Simple popup and disconnect when failback is invoked.
321 * Revision 1.18 1995/03/21 15:50:12 adam
324 * Revision 1.17 1995/03/21 13:41:03 adam
325 * Comstack cs_create not used too often. Non-blocking connect.
327 * Revision 1.16 1995/03/21 08:26:06 adam
328 * New method, setName, to specify the result set name (other than Default).
329 * New method, responseStatus, which returns diagnostic info, if any, after
330 * present response / search response.
332 * Revision 1.15 1995/03/20 15:24:07 adam
333 * Diagnostic records saved on searchResponse.
335 * Revision 1.14 1995/03/20 08:53:22 adam
336 * Event loop in tclmain.c rewritten. New method searchStatus.
338 * Revision 1.13 1995/03/17 18:26:17 adam
339 * Non-blocking i/o used now. Database names popup as cascade items.
341 * Revision 1.12 1995/03/17 15:45:00 adam
342 * Improved target/database setup.
344 * Revision 1.11 1995/03/16 17:54:03 adam
345 * Minor changes really.
347 * Revision 1.10 1995/03/15 16:14:50 adam
348 * Blocking arg in cs_create changed.
350 * Revision 1.9 1995/03/15 13:59:24 adam
353 * Revision 1.8 1995/03/15 08:25:16 adam
354 * New method presentStatus to check for error on present. Misc. cleanup
355 * of IrTcl_RecordList manipulations. Full MARC record presentation in
358 * Revision 1.7 1995/03/14 17:32:29 adam
359 * Presentation of full Marc record in popup window.
361 * Revision 1.6 1995/03/12 19:31:55 adam
362 * Pattern matching implemented when retrieving MARC records. More
363 * diagnostic functions.
365 * Revision 1.5 1995/03/10 18:00:15 adam
366 * Actual presentation in line-by-line format. RPN query support.
368 * Revision 1.4 1995/03/09 16:15:08 adam
369 * First presentRequest attempts. Hot-target list.
387 #if defined(__WIN32__)
388 # define WIN32_LEAN_AND_MEAN
389 # include <windows.h>
390 # undef WIN32_LEAN_AND_MEAN
393 * VC++ has an alternate entry point called DllMain, so we need to rename
397 # if defined(_MSC_VER)
398 # define EXPORT(a,b) __declspec(dllexport) a b
399 # define DllEntryPoint DllMain
401 # if defined(__BORLANDC__)
402 # define EXPORT(a,b) a _export b
404 # define EXPORT(a,b) a b
408 # define EXPORT(a,b) a b
411 static char *wrongArgs = "wrong # args: should be \"";
413 static int ir_tcl_error_exec (Tcl_Interp *interp, int argc, char **argv)
416 Tcl_AppendResult (interp, " while executing ", NULL);
417 for (i = 0; i<argc; i++)
418 Tcl_AppendResult (interp, (i ? " " : "\""), argv[i], NULL);
419 Tcl_AppendResult (interp, "\"", NULL);
424 static void ir_deleteDiags (IrTcl_Diagnostic **dst_list, int *dst_num);
426 static void ir_select_notify (ClientData clientData, int r, int w, int e);
428 void ir_select_add (int fd, void *obj)
430 ir_tcl_select_set (ir_select_notify, fd, obj, 1, 0, 0);
433 void ir_select_add_write (int fd, void *obj)
435 ir_tcl_select_set (ir_select_notify, fd, obj, 1, 1, 0);
438 void ir_select_remove (int fd, void *obj)
440 ir_tcl_select_set (NULL, fd, obj, 0, 0, 0);
443 void ir_select_remove_write (int fd, void *obj)
445 ir_tcl_select_set (ir_select_notify, fd, obj, 1, 0, 0);
448 static void delete_IR_record (IrTcl_RecordList *rl)
452 case Z_NamePlusRecord_databaseRecord:
453 switch (rl->u.dbrec.type)
456 ir_tcl_grs_del (&rl->u.dbrec.u.grs1);
461 xfree (rl->u.dbrec.buf);
462 rl->u.dbrec.buf = NULL;
464 case Z_NamePlusRecord_surrogateDiagnostic:
465 ir_deleteDiags (&rl->u.surrogateDiagnostics.list,
466 &rl->u.surrogateDiagnostics.num);
469 xfree (rl->elements);
472 static IrTcl_RecordList *new_IR_record (IrTcl_SetObj *setobj,
474 const char *elements)
476 IrTcl_RecordList *rl;
478 if (elements && !*elements)
480 for (rl = setobj->record_list; rl; rl = rl->next)
482 if (no == rl->no && (!rl->elements || !elements ||
483 !strcmp(elements, rl->elements)))
485 delete_IR_record (rl);
491 rl = ir_tcl_malloc (sizeof(*rl));
492 rl->next = setobj->record_list;
494 setobj->record_list = rl;
497 ir_tcl_strdup (NULL, &rl->elements, elements);
504 int ir_tcl_eval (Tcl_Interp *interp, const char *command)
506 char *tmp = ir_tcl_malloc (strlen(command)+1);
509 logf (LOG_DEBUG, "Invoking %.23s ...", command);
510 strcpy (tmp, command);
511 r = Tcl_Eval (interp, tmp);
514 const char *errorInfo = Tcl_GetVar (interp, "errorInfo", 0);
515 logf (LOG_WARN, "Tcl error in line %d: %s\n%s", interp->errorLine,
516 interp->result, errorInfo ? errorInfo : "<null>");
518 Tcl_FreeResult (interp);
524 * IrTcl_getRecordSyntaxStr: Return record syntax name of object id
526 static char *IrTcl_getRecordSyntaxStr (enum oid_value value)
529 struct oident ent, *entp;
531 ent.proto = PROTO_Z3950;
532 ent.oclass = CLASS_RECSYN;
535 o = oid_getoidbyent (&ent);
536 entp = oid_getentbyoid (o);
544 * IrTcl_getRecordSyntaxVal: Return record syntax value of string
546 static enum oid_value IrTcl_getRecordSyntaxVal (const char *name)
548 return oid_getvalbyname (name);
551 static IrTcl_RecordList *find_IR_record (IrTcl_SetObj *setobj, int no)
553 IrTcl_RecordList *rl;
555 for (rl = setobj->record_list; rl; rl = rl->next)
557 (!setobj->recordElements || !rl->elements ||
558 !strcmp (setobj->recordElements, rl->elements)))
563 static void delete_IR_records (IrTcl_SetObj *setobj)
565 IrTcl_RecordList *rl, *rl1;
567 for (rl = setobj->record_list; rl; rl = rl1)
569 delete_IR_record (rl);
573 setobj->record_list = NULL;
577 * ir_tcl_get_set_int: Set/get integer value
579 int ir_tcl_get_set_int (int *val, Tcl_Interp *interp, int argc, char **argv)
585 if (Tcl_GetInt (interp, argv[2], val)==TCL_ERROR)
588 sprintf (buf, "%d", *val);
589 Tcl_AppendResult (interp, buf, NULL);
595 * ir_tcl_method_error
597 int ir_tcl_method_error (Tcl_Interp *interp, int argc, char **argv,
600 IrTcl_Methods *tab_i = tab;
603 Tcl_AppendResult (interp, "bad method: \"", *argv, " ", argv[1],
604 "\"\nmethod should be of:", NULL);
605 for (tab_i = tab; tab_i->tab; tab_i++)
606 for (t = tab_i->tab; t->name; t++)
607 Tcl_AppendResult (interp, " ", t->name, NULL);
612 * ir_tcl_method: Search for method in table and invoke method handler
614 int ir_tcl_method (Tcl_Interp *interp, int argc, char **argv,
615 IrTcl_Methods *tab, int *ret)
617 IrTcl_Methods *tab_i = tab;
620 for (tab_i = tab; tab_i->tab; tab_i++)
621 for (t = tab_i->tab; t->name; t++)
624 if ((*t->method)(tab_i->obj, interp, argc, argv) == TCL_ERROR)
628 if (!strcmp (t->name, argv[1]))
630 *ret = (*t->method)(tab_i->obj, interp, argc, argv);
641 * ir_tcl_named_bits: get/set named bits
643 int ir_tcl_named_bits (struct ir_named_entry *tab, Odr_bitmask *ob,
644 Tcl_Interp *interp, int argc, char **argv)
646 struct ir_named_entry *ti;
651 for (no = 0; no < argc; no++)
653 for (ti = tab; ti->name; ti++)
654 if (!strcmp (argv[no], ti->name))
656 ODR_MASK_SET (ob, ti->pos);
661 Tcl_AppendResult (interp, "bad bit mask ", argv[no], NULL);
662 return ir_tcl_error_exec (interp, argc, argv);
667 for (ti = tab; ti->name; ti++)
668 if (ODR_MASK_GET (ob, ti->pos))
669 Tcl_AppendElement (interp, ti->name);
673 static void set_referenceId (ODR o, Z_ReferenceId **dst, const char *src)
679 *dst = odr_malloc (o, sizeof(**dst));
680 (*dst)->size = (*dst)->len = strlen(src);
681 (*dst)->buf = odr_malloc (o, (*dst)->len);
682 memcpy ((*dst)->buf, src, (*dst)->len);
686 static void get_referenceId (char **dst, Z_ReferenceId *src)
694 *dst = ir_tcl_malloc (src->len+1);
695 memcpy (*dst, src->buf, src->len);
696 (*dst)[src->len] = '\0';
699 /* ------------------------------------------------------- */
702 * do_init_request: init method on IR object
704 static int do_init_request (void *obj, Tcl_Interp *interp,
705 int argc, char **argv)
713 logf (LOG_DEBUG, "init %s", *argv);
716 Tcl_AppendResult (interp, "not connected", NULL);
717 return ir_tcl_error_exec (interp, argc, argv);
719 apdu = zget_APDU (p->odr_out, Z_APDU_initRequest);
720 req = apdu->u.initRequest;
722 set_referenceId (p->odr_out, &req->referenceId, p->set_inher.referenceId);
723 req->options = &p->options;
724 req->protocolVersion = &p->protocolVersion;
725 req->preferredMessageSize = &p->preferredMessageSize;
726 req->maximumRecordSize = &p->maximumRecordSize;
728 if (p->idAuthenticationGroupId)
730 Z_IdPass *pass = odr_malloc (p->odr_out, sizeof(*pass));
731 Z_IdAuthentication *auth = odr_malloc (p->odr_out, sizeof(*auth));
733 logf (LOG_DEBUG, "using pass authentication");
735 auth->which = Z_IdAuthentication_idPass;
736 auth->u.idPass = pass;
737 if (p->idAuthenticationGroupId && *p->idAuthenticationGroupId)
738 pass->groupId = p->idAuthenticationGroupId;
740 pass->groupId = NULL;
741 if (p->idAuthenticationUserId && *p->idAuthenticationUserId)
742 pass->userId = p->idAuthenticationUserId;
745 if (p->idAuthenticationPassword && *p->idAuthenticationPassword)
746 pass->password = p->idAuthenticationPassword;
748 pass->password = NULL;
749 req->idAuthentication = auth;
751 else if (!p->idAuthenticationOpen || !*p->idAuthenticationOpen)
752 req->idAuthentication = NULL;
755 Z_IdAuthentication *auth = odr_malloc (p->odr_out, sizeof(*auth));
757 logf (LOG_DEBUG, "using open authentication");
758 auth->which = Z_IdAuthentication_open;
759 auth->u.open = p->idAuthenticationOpen;
760 req->idAuthentication = auth;
762 req->implementationId = p->implementationId;
763 req->implementationName = p->implementationName;
764 req->implementationVersion = p->implementationVersion;
765 req->userInformationField = 0;
767 return ir_tcl_send_APDU (interp, p, apdu, "init", *argv);
771 * do_protocolVersion: Set protocol Version
773 static int do_protocolVersion (void *obj, Tcl_Interp *interp,
774 int argc, char **argv)
782 ODR_MASK_ZERO (&p->protocolVersion);
783 ODR_MASK_SET (&p->protocolVersion, 0);
784 ODR_MASK_SET (&p->protocolVersion, 1);
789 if (Tcl_GetInt (interp, argv[2], &version)==TCL_ERROR)
791 ODR_MASK_ZERO (&p->protocolVersion);
792 for (i = 0; i<version; i++)
793 ODR_MASK_SET (&p->protocolVersion, i);
795 for (i = 4; --i >= 0; )
796 if (ODR_MASK_GET (&p->protocolVersion, i))
798 sprintf (buf, "%d", i+1);
799 interp->result = buf;
804 * do_options: Set options
806 static int do_options (void *obj, Tcl_Interp *interp,
807 int argc, char **argv)
809 static struct ir_named_entry options_tab[] = {
813 { "resourceReport", 3 },
814 { "triggerResourceCtrl", 4},
815 { "resourceCtrl", 5},
819 { "extendedServices", 10},
820 { "level-1Segmentation", 11},
821 { "level-2Segmentation", 12},
822 { "concurrentOperations", 13},
823 { "namedResultSets", 14},
830 ODR_MASK_ZERO (&p->options);
831 ODR_MASK_SET (&p->options, 0);
832 ODR_MASK_SET (&p->options, 1);
833 ODR_MASK_SET (&p->options, 4);
834 ODR_MASK_SET (&p->options, 7);
835 ODR_MASK_SET (&p->options, 14);
838 return ir_tcl_named_bits (options_tab, &p->options, interp, argc-2, argv+2);
842 * do_apduInfo: Get APDU information
844 static int do_apduInfo (void *obj, Tcl_Interp *interp, int argc, char **argv)
852 sprintf (buf, "%d", p->apduLen);
853 Tcl_AppendElement (interp, buf);
854 sprintf (buf, "%d", p->apduOffset);
855 Tcl_AppendElement (interp, buf);
858 Tcl_AppendElement (interp, "");
861 apduf = fopen ("apdu.tmp", "w");
864 Tcl_AppendElement (interp, "");
867 odr_dumpBER (apduf, p->buf_in, p->apduLen);
869 if (!(apduf = fopen ("apdu.tmp", "r")))
870 Tcl_AppendElement (interp, "");
875 Tcl_AppendResult (interp, " {", NULL);
876 while ((c = getc (apduf)) != EOF)
880 Tcl_AppendResult (interp, buf, NULL);
883 Tcl_AppendResult (interp, "}", NULL);
890 * do_failInfo: Get fail information
892 static int do_failInfo (void *obj, Tcl_Interp *interp, int argc, char **argv)
902 sprintf (buf, "%d", p->failInfo);
908 case IR_TCL_FAIL_CONNECT:
909 cp = "connect failed";
911 case IR_TCL_FAIL_READ:
912 cp = "connection closed";
914 case IR_TCL_FAIL_WRITE:
915 cp = "connection closed";
917 case IR_TCL_FAIL_IN_APDU:
918 cp = "failed to decode incoming APDU";
920 case IR_TCL_FAIL_UNKNOWN_APDU:
926 Tcl_AppendElement (interp, buf);
927 Tcl_AppendElement (interp, cp);
932 * do_preferredMessageSize: Set/get preferred message size
934 static int do_preferredMessageSize (void *obj, Tcl_Interp *interp,
935 int argc, char **argv)
941 p->preferredMessageSize = 30000;
944 return ir_tcl_get_set_int (&p->preferredMessageSize, interp, argc, argv);
948 * do_maximumRecordSize: Set/get maximum record size
950 static int do_maximumRecordSize (void *obj, Tcl_Interp *interp,
951 int argc, char **argv)
957 p->maximumRecordSize = 30000;
960 return ir_tcl_get_set_int (&p->maximumRecordSize, interp, argc, argv);
964 * do_initResult: Get init result
966 static int do_initResult (void *obj, Tcl_Interp *interp,
967 int argc, char **argv)
973 return ir_tcl_get_set_int (&p->initResult, interp, argc, argv);
978 * do_implementationName: Set/get Implementation Name.
980 static int do_implementationName (void *obj, Tcl_Interp *interp,
981 int argc, char **argv)
986 return ir_tcl_strdup (interp, &p->implementationName,
987 "Index Data/IrTcl on YAZ");
989 return ir_tcl_strdel (interp, &p->implementationName);
992 xfree (p->implementationName);
993 if (ir_tcl_strdup (interp, &p->implementationName, argv[2])
997 Tcl_AppendResult (interp, p->implementationName, (char*) NULL);
1002 * do_implementationId: Get Implementation Id.
1004 static int do_implementationId (void *obj, Tcl_Interp *interp,
1005 int argc, char **argv)
1010 return ir_tcl_strdup (interp, &p->implementationId, "YAZ (id=81)");
1011 else if (argc == -1)
1012 return ir_tcl_strdel (interp, &p->implementationId);
1013 Tcl_AppendResult (interp, p->implementationId, (char*) NULL);
1018 * do_implementationVersion: get Implementation Version.
1020 static int do_implementationVersion (void *obj, Tcl_Interp *interp,
1021 int argc, char **argv)
1026 return ir_tcl_strdup (interp, &p->implementationVersion,
1028 #ifdef IR_TCL_VERSION
1029 " / Irtcl: " IR_TCL_VERSION
1032 else if (argc == -1)
1033 return ir_tcl_strdel (interp, &p->implementationVersion);
1034 Tcl_AppendResult (interp, p->implementationVersion, (char*) NULL);
1039 * do_targetImplementationName: Get Implementation Name of target.
1041 static int do_targetImplementationName (void *obj, Tcl_Interp *interp,
1042 int argc, char **argv)
1048 p->targetImplementationName = NULL;
1051 else if (argc == -1)
1052 return ir_tcl_strdel (interp, &p->targetImplementationName);
1053 Tcl_AppendResult (interp, p->targetImplementationName, (char*) NULL);
1058 * do_targetImplementationId: Get Implementation Id of target
1060 static int do_targetImplementationId (void *obj, Tcl_Interp *interp,
1061 int argc, char **argv)
1067 p->targetImplementationId = NULL;
1070 else if (argc == -1)
1071 return ir_tcl_strdel (interp, &p->targetImplementationId);
1072 Tcl_AppendResult (interp, p->targetImplementationId, (char*) NULL);
1077 * do_targetImplementationVersion: Get Implementation Version of target
1079 static int do_targetImplementationVersion (void *obj, Tcl_Interp *interp,
1080 int argc, char **argv)
1086 p->targetImplementationVersion = NULL;
1089 else if (argc == -1)
1090 return ir_tcl_strdel (interp, &p->targetImplementationVersion);
1091 Tcl_AppendResult (interp, p->targetImplementationVersion, (char*) NULL);
1096 * do_idAuthentication: Set/get id Authentication
1098 static int do_idAuthentication (void *obj, Tcl_Interp *interp,
1099 int argc, char **argv)
1103 if (argc >= 3 || argc == -1)
1105 xfree (p->idAuthenticationOpen);
1106 xfree (p->idAuthenticationGroupId);
1107 xfree (p->idAuthenticationUserId);
1108 xfree (p->idAuthenticationPassword);
1110 if (argc >= 3 || argc <= 0)
1112 p->idAuthenticationOpen = NULL;
1113 p->idAuthenticationGroupId = NULL;
1114 p->idAuthenticationUserId = NULL;
1115 p->idAuthenticationPassword = NULL;
1124 ir_tcl_strdup (interp, &p->idAuthenticationOpen, argv[2])
1131 ir_tcl_strdup (interp, &p->idAuthenticationGroupId, argv[2])
1135 ir_tcl_strdup (interp, &p->idAuthenticationUserId, argv[3])
1139 ir_tcl_strdup (interp, &p->idAuthenticationPassword, argv[4])
1144 if (p->idAuthenticationOpen)
1145 Tcl_AppendElement (interp, p->idAuthenticationOpen);
1146 else if (p->idAuthenticationGroupId)
1148 Tcl_AppendElement (interp, p->idAuthenticationGroupId);
1149 Tcl_AppendElement (interp, p->idAuthenticationUserId);
1150 Tcl_AppendElement (interp, p->idAuthenticationPassword);
1156 * do_connect: connect method on IR object
1158 static int do_connect (void *obj, Tcl_Interp *interp,
1159 int argc, char **argv)
1169 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
1170 " ?hostname?\"", NULL);
1175 Tcl_AppendResult (interp, p->hostname, NULL);
1179 logf (LOG_DEBUG, "connect %s %s", *argv, argv[2]);
1182 Tcl_AppendResult (interp, "already connected", NULL);
1183 return ir_tcl_error_exec (interp, argc, argv);
1185 if (!strcmp (p->comstackType, "tcpip"))
1187 p->cs_link = cs_create (tcpip_type, CS_BLOCK, p->protocol_type);
1188 logf (LOG_DEBUG, "tcp/ip connect %s", argv[2]);
1190 else if (!strcmp (p->comstackType, "mosi"))
1193 p->cs_link = cs_create (mosi_type, CS_BLOCK, p->protocol_type);
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 addr = cs_straddr (p->cs_link, argv[2]);
1212 ir_tcl_disconnect (p);
1213 Tcl_AppendResult (interp, "cs_straddr fail", NULL);
1214 return ir_tcl_error_exec (interp, argc, argv);
1216 if ((r=cs_connect (p->cs_link, addr)) < 0)
1218 ir_tcl_disconnect (p);
1219 Tcl_AppendResult (interp, "connect fail", NULL);
1220 return ir_tcl_error_exec (interp, argc, argv);
1222 ir_select_add (cs_fileno (p->cs_link), p);
1225 logf (LOG_DEBUG, "connect pending fd=%d", cs_fileno(p->cs_link));
1226 ir_select_add_write (cs_fileno (p->cs_link), p);
1227 p->state = IR_TCL_R_Connecting;
1231 logf (LOG_DEBUG, "connect ok fd=%d", cs_fileno(p->cs_link));
1232 p->state = IR_TCL_R_Idle;
1234 ir_tcl_eval (p->interp, p->callback);
1241 * ir_tcl_disconnect: close connection
1243 void ir_tcl_disconnect (IrTcl_Obj *p)
1247 logf(LOG_DEBUG, "Closing connection to %s", p->hostname);
1248 xfree (p->hostname);
1250 assert (p->cs_link);
1251 ir_select_remove (cs_fileno (p->cs_link), p);
1253 odr_reset (p->odr_in);
1255 #if TCL_MAJOR_VERSION == 8
1256 cs_fileno(p->cs_link) = -1;
1258 cs_close (p->cs_link);
1261 ODR_MASK_ZERO (&p->options);
1262 ODR_MASK_SET (&p->options, 0);
1263 ODR_MASK_SET (&p->options, 1);
1264 ODR_MASK_SET (&p->options, 4);
1265 ODR_MASK_SET (&p->options, 7);
1266 ODR_MASK_SET (&p->options, 14);
1268 ODR_MASK_ZERO (&p->protocolVersion);
1269 ODR_MASK_SET (&p->protocolVersion, 0);
1270 ODR_MASK_SET (&p->protocolVersion, 1);
1273 assert (!p->cs_link);
1277 * do_disconnect: disconnect method on IR object
1279 static int do_disconnect (void *obj, Tcl_Interp *interp,
1280 int argc, char **argv)
1286 p->state = IR_TCL_R_Idle;
1287 p->eventType = NULL;
1292 ir_tcl_disconnect (p);
1297 * do_comstack: Set/get comstack method on IR object
1299 static int do_comstack (void *o, Tcl_Interp *interp,
1300 int argc, char **argv)
1305 return ir_tcl_strdup (interp, &obj->comstackType, "tcpip");
1306 else if (argc == -1)
1307 return ir_tcl_strdel (interp, &obj->comstackType);
1310 xfree (obj->comstackType);
1311 if (ir_tcl_strdup (interp, &obj->comstackType, argv[2]) == TCL_ERROR)
1314 Tcl_AppendElement (interp, obj->comstackType);
1319 * do_logLevel: Set log level
1321 static int do_logLevel (void *o, Tcl_Interp *interp,
1322 int argc, char **argv)
1327 log_init (log_mask_str (argv[2]), "", NULL);
1329 log_init (log_mask_str (argv[2]), argv[3], NULL);
1331 log_init (log_mask_str (argv[2]), argv[3], argv[4]);
1337 * do_eventType: Return type of last event
1339 static int do_eventType (void *obj, Tcl_Interp *interp,
1340 int argc, char **argv)
1346 p->eventType = NULL;
1349 Tcl_AppendElement (interp, p->eventType ? p->eventType : "");
1355 * do_callback: add callback
1357 static int do_callback (void *obj, Tcl_Interp *interp,
1358 int argc, char **argv)
1367 else if (argc == -1)
1368 return ir_tcl_strdel (interp, &p->callback);
1371 xfree (p->callback);
1374 if (ir_tcl_strdup (interp, &p->callback, argv[2]) == TCL_ERROR)
1384 * do_failback: add error handle callback
1386 static int do_failback (void *obj, Tcl_Interp *interp,
1387 int argc, char **argv)
1396 else if (argc == -1)
1397 return ir_tcl_strdel (interp, &p->failback);
1400 xfree (p->failback);
1403 if (ir_tcl_strdup (interp, &p->failback, argv[2]) == TCL_ERROR)
1413 * do_initResponse: add init response handler
1415 static int do_initResponse (void *obj, Tcl_Interp *interp,
1416 int argc, char **argv)
1422 p->initResponse = NULL;
1425 else if (argc == -1)
1426 return ir_tcl_strdel (interp, &p->initResponse);
1429 xfree (p->initResponse);
1432 if (ir_tcl_strdup (interp, &p->initResponse, argv[2]) == TCL_ERROR)
1436 p->initResponse = NULL;
1441 * do_protocol: Set/get protocol method on IR object
1443 static int do_protocol (void *o, Tcl_Interp *interp, int argc, char **argv)
1449 p->protocol_type = PROTO_Z3950;
1454 if (!strcmp (argv[2], "Z39"))
1455 p->protocol_type = PROTO_Z3950;
1456 else if (!strcmp (argv[2], "SR"))
1457 p->protocol_type = PROTO_SR;
1460 Tcl_AppendResult (interp, "bad protocol ", argv[2], NULL);
1461 return ir_tcl_error_exec (interp, argc, argv);
1465 switch (p->protocol_type)
1468 Tcl_AppendElement (interp, "Z39");
1471 Tcl_AppendElement (interp, "SR");
1478 * do_triggerResourceControl:
1480 static int do_triggerResourceControl (void *obj, Tcl_Interp *interp,
1481 int argc, char **argv)
1485 Z_TriggerResourceControlRequest *req;
1486 bool_t is_false = 0;
1492 Tcl_AppendResult (interp, "not connected", NULL);
1493 return ir_tcl_error_exec (interp, argc, argv);
1495 apdu = zget_APDU (p->odr_out, Z_APDU_triggerResourceControlRequest);
1496 req = apdu->u.triggerResourceControlRequest;
1497 *req->requestedAction = Z_TriggerResourceCtrl_cancel;
1498 req->resultSetWanted = &is_false;
1500 return ir_tcl_send_APDU (interp, p, apdu, "triggerResourceControl",
1505 * do_databaseNames: specify database names
1507 static int do_databaseNames (void *obj, Tcl_Interp *interp,
1508 int argc, char **argv)
1511 IrTcl_SetCObj *p = obj;
1515 for (i=0; i<p->num_databaseNames; i++)
1516 xfree (p->databaseNames[i]);
1517 xfree (p->databaseNames);
1521 p->num_databaseNames = 0;
1522 p->databaseNames = NULL;
1527 for (i=0; i<p->num_databaseNames; i++)
1528 Tcl_AppendElement (interp, p->databaseNames[i]);
1531 if (p->databaseNames)
1533 for (i=0; i<p->num_databaseNames; i++)
1534 xfree (p->databaseNames[i]);
1535 xfree (p->databaseNames);
1537 p->num_databaseNames = argc - 2;
1539 ir_tcl_malloc (sizeof(*p->databaseNames) * (1+p->num_databaseNames));
1540 for (i=0; i<p->num_databaseNames; i++)
1542 if (ir_tcl_strdup (interp, &p->databaseNames[i], argv[2+i])
1546 p->databaseNames[i] = NULL;
1551 * do_replaceIndicator: Set/get replace Set indicator
1553 static int do_replaceIndicator (void *obj, Tcl_Interp *interp,
1554 int argc, char **argv)
1556 IrTcl_SetCObj *p = obj;
1560 p->replaceIndicator = 1;
1563 return ir_tcl_get_set_int (&p->replaceIndicator, interp, argc, argv);
1567 * do_queryType: Set/Get query method
1569 static int do_queryType (void *obj, Tcl_Interp *interp,
1570 int argc, char **argv)
1572 IrTcl_SetCObj *p = obj;
1575 return ir_tcl_strdup (interp, &p->queryType, "rpn");
1576 else if (argc == -1)
1577 return ir_tcl_strdel (interp, &p->queryType);
1580 xfree (p->queryType);
1581 if (ir_tcl_strdup (interp, &p->queryType, argv[2]) == TCL_ERROR)
1584 Tcl_AppendResult (interp, p->queryType, NULL);
1589 * do_userInformationField: Get User information field
1591 static int do_userInformationField (void *obj, Tcl_Interp *interp,
1592 int argc, char **argv)
1598 p->userInformationField = NULL;
1601 else if (argc == -1)
1602 return ir_tcl_strdel (interp, &p->userInformationField);
1603 Tcl_AppendResult (interp, p->userInformationField, NULL);
1608 * do_smallSetUpperBound: Set/get small set upper bound
1610 static int do_smallSetUpperBound (void *o, Tcl_Interp *interp,
1611 int argc, char **argv)
1613 IrTcl_SetCObj *p = o;
1617 p->smallSetUpperBound = 0;
1620 return ir_tcl_get_set_int (&p->smallSetUpperBound, interp, argc, argv);
1624 * do_largeSetLowerBound: Set/get large set lower bound
1626 static int do_largeSetLowerBound (void *o, Tcl_Interp *interp,
1627 int argc, char **argv)
1629 IrTcl_SetCObj *p = o;
1633 p->largeSetLowerBound = 2;
1636 return ir_tcl_get_set_int (&p->largeSetLowerBound, interp, argc, argv);
1640 * do_mediumSetPresentNumber: Set/get large set lower bound
1642 static int do_mediumSetPresentNumber (void *o, Tcl_Interp *interp,
1643 int argc, char **argv)
1645 IrTcl_SetCObj *p = o;
1649 p->mediumSetPresentNumber = 0;
1652 return ir_tcl_get_set_int (&p->mediumSetPresentNumber, interp, argc, argv);
1656 * do_referenceId: Set/Get referenceId
1658 static int do_referenceId (void *obj, Tcl_Interp *interp,
1659 int argc, char **argv)
1661 IrTcl_SetCObj *p = obj;
1665 p->referenceId = NULL;
1668 else if (argc == -1)
1669 return ir_tcl_strdel (interp, &p->referenceId);
1672 xfree (p->referenceId);
1673 if (ir_tcl_strdup (interp, &p->referenceId, argv[2]) == TCL_ERROR)
1676 Tcl_AppendResult (interp, p->referenceId, NULL);
1681 * do_preferredRecordSyntax: Set/get preferred record syntax
1683 static int do_preferredRecordSyntax (void *obj, Tcl_Interp *interp,
1684 int argc, char **argv)
1686 IrTcl_SetCObj *p = obj;
1690 p->preferredRecordSyntax = NULL;
1693 else if (argc == -1)
1695 xfree (p->preferredRecordSyntax);
1696 p->preferredRecordSyntax = NULL;
1701 xfree (p->preferredRecordSyntax);
1702 p->preferredRecordSyntax = NULL;
1703 if (argv[2][0] && (p->preferredRecordSyntax =
1704 ir_tcl_malloc (sizeof(*p->preferredRecordSyntax))))
1705 *p->preferredRecordSyntax = IrTcl_getRecordSyntaxVal (argv[2]);
1710 (interp,!p->preferredRecordSyntax ? "" :
1711 IrTcl_getRecordSyntaxStr(*p->preferredRecordSyntax));
1718 * do_elementSetNames: Set/Get element Set Names
1720 static int do_elementSetNames (void *obj, Tcl_Interp *interp,
1721 int argc, char **argv)
1723 IrTcl_SetCObj *p = obj;
1727 p->elementSetNames = NULL;
1730 else if (argc == -1)
1731 return ir_tcl_strdel (interp, &p->elementSetNames);
1734 xfree (p->elementSetNames);
1735 if (ir_tcl_strdup (interp, &p->elementSetNames, argv[2]) == TCL_ERROR)
1738 Tcl_AppendResult (interp, p->elementSetNames, NULL);
1743 * do_smallSetElementSetNames: Set/Get small Set Element Set Names
1745 static int do_smallSetElementSetNames (void *obj, Tcl_Interp *interp,
1746 int argc, char **argv)
1748 IrTcl_SetCObj *p = obj;
1752 p->smallSetElementSetNames = NULL;
1755 else if (argc == -1)
1756 return ir_tcl_strdel (interp, &p->smallSetElementSetNames);
1759 xfree (p->smallSetElementSetNames);
1760 if (ir_tcl_strdup (interp, &p->smallSetElementSetNames,
1761 argv[2]) == TCL_ERROR)
1764 Tcl_AppendResult (interp, p->smallSetElementSetNames, NULL);
1769 * do_mediumSetElementSetNames: Set/Get medium Set Element Set Names
1771 static int do_mediumSetElementSetNames (void *obj, Tcl_Interp *interp,
1772 int argc, char **argv)
1774 IrTcl_SetCObj *p = obj;
1778 p->mediumSetElementSetNames = NULL;
1781 else if (argc == -1)
1782 return ir_tcl_strdel (interp, &p->mediumSetElementSetNames);
1785 xfree (p->mediumSetElementSetNames);
1786 if (ir_tcl_strdup (interp, &p->mediumSetElementSetNames,
1787 argv[2]) == TCL_ERROR)
1790 Tcl_AppendResult (interp, p->mediumSetElementSetNames, NULL);
1794 static IrTcl_Method ir_method_tab[] = {
1795 { "comstack", do_comstack, NULL },
1796 { "protocol", do_protocol, NULL },
1797 { "failback", do_failback, NULL },
1798 { "failInfo", do_failInfo, NULL },
1799 { "apduInfo", do_apduInfo, NULL },
1800 { "logLevel", do_logLevel, NULL },
1802 { "eventType", do_eventType, NULL },
1803 { "connect", do_connect, NULL },
1804 { "protocolVersion", do_protocolVersion, NULL },
1805 { "preferredMessageSize", do_preferredMessageSize, NULL },
1806 { "maximumRecordSize", do_maximumRecordSize, NULL },
1807 { "implementationName", do_implementationName, NULL },
1808 { "implementationId", do_implementationId, NULL },
1809 { "implementationVersion", do_implementationVersion, NULL },
1810 { "targetImplementationName", do_targetImplementationName, NULL },
1811 { "targetImplementationId", do_targetImplementationId, NULL },
1812 { "targetImplementationVersion", do_targetImplementationVersion, NULL},
1813 { "userInformationField", do_userInformationField, NULL},
1814 { "idAuthentication", do_idAuthentication, NULL},
1815 { "options", do_options, NULL},
1816 { "init", do_init_request, NULL},
1817 { "initResult", do_initResult, NULL},
1818 { "disconnect", do_disconnect, NULL},
1819 { "callback", do_callback, NULL},
1820 { "initResponse", do_initResponse, NULL},
1821 { "triggerResourceControl", do_triggerResourceControl, NULL},
1822 { "initResponse", do_initResponse, NULL},
1826 static IrTcl_Method ir_set_c_method_tab[] = {
1827 { "databaseNames", do_databaseNames, NULL},
1828 { "replaceIndicator", do_replaceIndicator, NULL},
1829 { "queryType", do_queryType, NULL},
1830 { "preferredRecordSyntax", do_preferredRecordSyntax, NULL},
1831 { "smallSetUpperBound", do_smallSetUpperBound, NULL},
1832 { "largeSetLowerBound", do_largeSetLowerBound, NULL},
1833 { "mediumSetPresentNumber", do_mediumSetPresentNumber, NULL},
1834 { "referenceId", do_referenceId, NULL},
1835 { "elementSetNames", do_elementSetNames, NULL},
1836 { "smallSetElementSetNames", do_smallSetElementSetNames, NULL},
1837 { "mediumSetElementSetNames", do_mediumSetElementSetNames, NULL},
1842 * ir_obj_method: IR Object methods
1844 static int ir_obj_method (ClientData clientData, Tcl_Interp *interp,
1845 int argc, char **argv)
1847 IrTcl_Methods tab[3];
1848 IrTcl_Obj *p = (IrTcl_Obj *) clientData;
1853 Tcl_AppendResult (interp, wrongArgs, *argv, "method args...\"", NULL);
1857 tab[0].tab = ir_method_tab;
1859 tab[1].tab = ir_set_c_method_tab;
1860 tab[1].obj = &p->set_inher;
1863 if (ir_tcl_method (interp, argc, argv, tab, &r) == TCL_ERROR)
1864 return ir_tcl_method_error (interp, argc, argv, tab);
1869 * ir_obj_delete: IR Object disposal
1871 static void ir_obj_delete (ClientData clientData)
1873 IrTcl_Obj *obj = (IrTcl_Obj *) clientData;
1874 IrTcl_Methods tab[3];
1877 if (obj->ref_count > 0)
1879 assert (obj->ref_count == 0);
1881 logf (LOG_DEBUG, "ir object delete");
1882 tab[0].tab = ir_method_tab;
1884 tab[1].tab = ir_set_c_method_tab;
1885 tab[1].obj = &obj->set_inher;
1888 ir_tcl_method (NULL, -1, NULL, tab, NULL);
1891 odr_destroy (obj->odr_in);
1892 odr_destroy (obj->odr_out);
1893 odr_destroy (obj->odr_pr);
1898 * ir_obj_init: IR Object initialization
1900 int ir_obj_init (ClientData clientData, Tcl_Interp *interp,
1901 int argc, char **argv, ClientData *subData,
1902 ClientData parentData)
1904 IrTcl_Methods tab[3];
1912 Tcl_AppendResult (interp, wrongArgs, *argv, " objName\"", NULL);
1915 obj = ir_tcl_malloc (sizeof(*obj));
1918 obj->bibset = ccl_qual_mk ();
1919 if ((inf = fopen ("default.bib", "r")))
1921 ccl_qual_file (obj->bibset, inf);
1926 logf (LOG_DEBUG, "ir object create %s", argv[1]);
1927 obj->odr_in = odr_createmem (ODR_DECODE);
1928 odr_choice_enable_bias (obj->odr_in, 0);
1929 obj->odr_out = odr_createmem (ODR_ENCODE);
1930 obj->odr_pr = odr_createmem (ODR_PRINT);
1931 obj->state = IR_TCL_R_Idle;
1932 obj->interp = interp;
1936 obj->request_queue = NULL;
1938 tab[0].tab = ir_method_tab;
1940 tab[1].tab = ir_set_c_method_tab;
1941 tab[1].obj = &obj->set_inher;
1944 if (ir_tcl_method (interp, 0, NULL, tab, NULL) == TCL_ERROR)
1946 Tcl_AppendResult (interp, "Failed to initialize ", argv[1], NULL);
1949 *subData = (ClientData) obj;
1955 * ir_obj_mk: IR Object creation
1957 static int ir_obj_mk (ClientData clientData, Tcl_Interp *interp,
1958 int argc, char **argv)
1961 int r = ir_obj_init (clientData, interp, argc, argv, &subData, 0);
1965 Tcl_CreateCommand (interp, argv[1], ir_obj_method,
1966 subData, ir_obj_delete);
1970 IrTcl_Class ir_obj_class = {
1978 /* ------------------------------------------------------- */
1980 * do_search: Do search request
1982 static int do_search (void *o, Tcl_Interp *interp, int argc, char **argv)
1984 Z_SearchRequest *req;
1988 IrTcl_SetObj *obj = o;
1999 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1], "query\"",
2003 logf (LOG_DEBUG, "search %s %s", *argv, argv[2]);
2004 if (!obj->set_inher.num_databaseNames)
2006 Tcl_AppendResult (interp, "no databaseNames", NULL);
2007 return ir_tcl_error_exec (interp, argc, argv);
2011 Tcl_AppendResult (interp, "not connected", NULL);
2012 return ir_tcl_error_exec (interp, argc, argv);
2014 apdu = zget_APDU (p->odr_out, Z_APDU_searchRequest);
2015 req = apdu->u.searchRequest;
2019 set_referenceId (p->odr_out, &req->referenceId,
2020 obj->set_inher.referenceId);
2022 req->smallSetUpperBound = &obj->set_inher.smallSetUpperBound;
2023 req->largeSetLowerBound = &obj->set_inher.largeSetLowerBound;
2024 req->mediumSetPresentNumber = &obj->set_inher.mediumSetPresentNumber;
2025 req->replaceIndicator = &obj->set_inher.replaceIndicator;
2026 req->resultSetName = obj->setName ? obj->setName : "default";
2027 logf (LOG_DEBUG, "Search, resultSetName %s", req->resultSetName);
2028 req->num_databaseNames = obj->set_inher.num_databaseNames;
2029 req->databaseNames = obj->set_inher.databaseNames;
2030 for (r=0; r < obj->set_inher.num_databaseNames; r++)
2031 logf (LOG_DEBUG, " Database %s", obj->set_inher.databaseNames[r]);
2032 if (obj->set_inher.preferredRecordSyntax)
2034 struct oident ident;
2036 ident.proto = p->protocol_type;
2037 ident.oclass = CLASS_RECSYN;
2038 ident.value = *obj->set_inher.preferredRecordSyntax;
2039 logf (LOG_DEBUG, "Preferred record syntax is %d", ident.value);
2040 req->preferredRecordSyntax = odr_oiddup (p->odr_out,
2041 oid_getoidbyent (&ident));
2044 req->preferredRecordSyntax = 0;
2046 if (obj->set_inher.smallSetElementSetNames &&
2047 *obj->set_inher.smallSetElementSetNames)
2049 Z_ElementSetNames *esn = odr_malloc (p->odr_out, sizeof(*esn));
2051 esn->which = Z_ElementSetNames_generic;
2052 esn->u.generic = obj->set_inher.smallSetElementSetNames;
2053 req->smallSetElementSetNames = esn;
2056 req->smallSetElementSetNames = NULL;
2058 if (obj->set_inher.mediumSetElementSetNames &&
2059 *obj->set_inher.mediumSetElementSetNames)
2061 Z_ElementSetNames *esn = odr_malloc (p->odr_out, sizeof(*esn));
2063 esn->which = Z_ElementSetNames_generic;
2064 esn->u.generic = obj->set_inher.mediumSetElementSetNames;
2065 req->mediumSetElementSetNames = esn;
2068 req->mediumSetElementSetNames = NULL;
2070 req->query = &query;
2072 logf (LOG_DEBUG, "queryType %s", obj->set_inher.queryType);
2073 if (!strcmp (obj->set_inher.queryType, "rpn"))
2075 Z_RPNQuery *RPNquery;
2077 RPNquery = p_query_rpn (p->odr_out, p->protocol_type, argv[2]);
2080 Tcl_AppendResult (interp, "query syntax error", NULL);
2081 return ir_tcl_error_exec (interp, argc, argv);
2083 query.which = Z_Query_type_1;
2084 query.u.type_1 = RPNquery;
2087 else if (!strcmp (obj->set_inher.queryType, "cclrpn"))
2091 struct ccl_rpn_node *rpn;
2092 Z_RPNQuery *RPNquery;
2095 bib1.proto = p->protocol_type;
2096 bib1.oclass = CLASS_ATTSET;
2097 bib1.value = VAL_BIB1;
2099 rpn = ccl_find_str(p->bibset, argv[2], &error, &pos);
2102 Tcl_AppendResult (interp, "ccl syntax error ", ccl_err_msg(error),
2104 return ir_tcl_error_exec (interp, argc, argv);
2107 ccl_pr_tree (rpn, stderr);
2108 fprintf (stderr, "\n");
2110 assert((RPNquery = ccl_rpn_query(rpn)));
2111 RPNquery->attributeSetId = oid_getoidbyent (&bib1);
2112 query.which = Z_Query_type_1;
2113 query.u.type_1 = RPNquery;
2116 else if (!strcmp (obj->set_inher.queryType, "ccl"))
2118 query.which = Z_Query_type_2;
2119 query.u.type_2 = &ccl_query;
2120 ccl_query.buf = (unsigned char *) argv[2];
2121 ccl_query.len = strlen (argv[2]);
2125 Tcl_AppendResult (interp, "invalid query method ",
2126 obj->set_inher.queryType, NULL);
2127 return ir_tcl_error_exec (interp, argc, argv);
2129 return ir_tcl_send_APDU (interp, p, apdu, "search", *argv);
2133 * do_searchResponse: add search response handler
2135 static int do_searchResponse (void *o, Tcl_Interp *interp,
2136 int argc, char **argv)
2138 IrTcl_SetObj *obj = o;
2142 obj->searchResponse = NULL;
2145 else if (argc == -1)
2146 return ir_tcl_strdel (interp, &obj->searchResponse);
2149 xfree (obj->searchResponse);
2152 if (ir_tcl_strdup (interp, &obj->searchResponse, argv[2])
2157 obj->searchResponse = NULL;
2163 * do_presentResponse: add present response handler
2165 static int do_presentResponse (void *o, Tcl_Interp *interp,
2166 int argc, char **argv)
2168 IrTcl_SetObj *obj = o;
2172 obj->presentResponse = NULL;
2175 else if (argc == -1)
2176 return ir_tcl_strdel (interp, &obj->presentResponse);
2179 xfree (obj->presentResponse);
2182 if (ir_tcl_strdup (interp, &obj->presentResponse, argv[2])
2187 obj->presentResponse = NULL;
2193 * do_resultCount: Get number of hits
2195 static int do_resultCount (void *o, Tcl_Interp *interp,
2196 int argc, char **argv)
2198 IrTcl_SetObj *obj = o;
2202 obj->resultCount = 0;
2205 return ir_tcl_get_set_int (&obj->resultCount, interp, argc, argv);
2209 * do_searchStatus: Get search status (after search response)
2211 static int do_searchStatus (void *o, Tcl_Interp *interp,
2212 int argc, char **argv)
2214 IrTcl_SetObj *obj = o;
2218 return ir_tcl_get_set_int (&obj->searchStatus, interp, argc, argv);
2222 * do_presentStatus: Get search status (after search/present response)
2224 static int do_presentStatus (void *o, Tcl_Interp *interp,
2225 int argc, char **argv)
2227 IrTcl_SetObj *obj = o;
2231 return ir_tcl_get_set_int (&obj->presentStatus, interp, argc, argv);
2235 * do_nextResultSetPosition: Get next result set position
2236 * (after search/present response)
2238 static int do_nextResultSetPosition (void *o, Tcl_Interp *interp,
2239 int argc, char **argv)
2241 IrTcl_SetObj *obj = o;
2245 obj->nextResultSetPosition = 0;
2248 return ir_tcl_get_set_int (&obj->nextResultSetPosition, interp,
2253 * do_setName: Set result Set name
2255 static int do_setName (void *o, Tcl_Interp *interp,
2256 int argc, char **argv)
2258 IrTcl_SetObj *obj = o;
2261 return ir_tcl_strdup (interp, &obj->setName, "Default");
2262 else if (argc == -1)
2263 return ir_tcl_strdel (interp, &obj->setName);
2266 xfree (obj->setName);
2267 if (ir_tcl_strdup (interp, &obj->setName, argv[2])
2271 Tcl_AppendElement (interp, obj->setName);
2276 * do_numberOfRecordsReturned: Get number of records returned
2278 static int do_numberOfRecordsReturned (void *o, Tcl_Interp *interp,
2279 int argc, char **argv)
2281 IrTcl_SetObj *obj = o;
2285 obj->numberOfRecordsReturned = 0;
2288 return ir_tcl_get_set_int (&obj->numberOfRecordsReturned, interp,
2293 * do_type: Return type (if any) at position.
2295 static int do_type (void *o, Tcl_Interp *interp, int argc, char **argv)
2297 IrTcl_SetObj *obj = o;
2299 IrTcl_RecordList *rl;
2303 obj->record_list = NULL;
2306 else if (argc == -1)
2308 delete_IR_records (obj);
2313 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2314 " position\"", NULL);
2317 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2319 rl = find_IR_record (obj, offset);
2322 logf (LOG_DEBUG, "%s %s %s: no record", argv[0], argv[1], argv[2]);
2327 case Z_NamePlusRecord_databaseRecord:
2328 interp->result = "DB";
2330 case Z_NamePlusRecord_surrogateDiagnostic:
2331 interp->result = "SD";
2339 * do_recordType: Return record type (if any) at position.
2341 static int do_recordType (void *o, Tcl_Interp *interp, int argc, char **argv)
2343 IrTcl_SetObj *obj = o;
2345 IrTcl_RecordList *rl;
2351 else if (argc == -1)
2357 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2358 " position\"", NULL);
2361 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2363 rl = find_IR_record (obj, offset);
2366 logf (LOG_DEBUG, "%s %s %s: no record", argv[0], argv[1], argv[2]);
2369 if (rl->which != Z_NamePlusRecord_databaseRecord)
2371 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2374 Tcl_AppendElement (interp, (char*)
2375 IrTcl_getRecordSyntaxStr (rl->u.dbrec.type));
2380 * set record elements (for record extraction)
2382 static int do_recordElements (void *o, Tcl_Interp *interp,
2383 int argc, char **argv)
2385 IrTcl_SetObj *obj = o;
2389 obj->recordElements = NULL;
2392 else if (argc == -1)
2393 return ir_tcl_strdel (NULL, &obj->recordElements);
2396 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2397 " ?position?\"", NULL);
2402 xfree (obj->recordElements);
2403 return ir_tcl_strdup (NULL, &obj->recordElements,
2404 (*argv[2] ? argv[2] : NULL));
2406 Tcl_AppendResult (interp, obj->recordElements, NULL);
2413 static int ir_diagResult (Tcl_Interp *interp, IrTcl_Diagnostic *list, int num)
2419 for (i = 0; i<num; i++)
2421 sprintf (buf, "%d", list[i].condition);
2422 Tcl_AppendElement (interp, buf);
2423 cp = diagbib1_str (list[i].condition);
2425 Tcl_AppendElement (interp, (char*) cp);
2427 Tcl_AppendElement (interp, "");
2428 if (list[i].addinfo)
2429 Tcl_AppendElement (interp, (char*) list[i].addinfo);
2431 Tcl_AppendElement (interp, "");
2437 * do_diag: Return diagnostic record info
2439 static int do_diag (void *o, Tcl_Interp *interp, int argc, char **argv)
2441 IrTcl_SetObj *obj = o;
2443 IrTcl_RecordList *rl;
2449 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2450 " position\"", NULL);
2453 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2455 rl = find_IR_record (obj, offset);
2458 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2461 if (rl->which != Z_NamePlusRecord_surrogateDiagnostic)
2463 Tcl_AppendResult (interp, "No Diagnostic record at #", argv[2], NULL);
2466 return ir_diagResult (interp, rl->u.surrogateDiagnostics.list,
2467 rl->u.surrogateDiagnostics.num);
2471 * do_getMarc: Get ISO2709 Record lines/fields
2473 static int do_getMarc (void *o, Tcl_Interp *interp, int argc, char **argv)
2475 IrTcl_SetObj *obj = o;
2477 IrTcl_RecordList *rl;
2483 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2484 " position line|field tag ind field\"", NULL);
2487 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2489 rl = find_IR_record (obj, offset);
2492 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2495 if (rl->which != Z_NamePlusRecord_databaseRecord)
2497 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2500 return ir_tcl_get_marc (interp, rl->u.dbrec.buf, argc, argv);
2504 * do_getSutrs: Get SUTRS Record
2506 static int do_getSutrs (void *o, Tcl_Interp *interp, int argc, char **argv)
2508 IrTcl_SetObj *obj = o;
2510 IrTcl_RecordList *rl;
2516 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2517 " position\"", NULL);
2520 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2522 rl = find_IR_record (obj, offset);
2525 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2528 if (rl->which != Z_NamePlusRecord_databaseRecord)
2530 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2533 if (!rl->u.dbrec.buf || rl->u.dbrec.type != VAL_SUTRS)
2535 Tcl_AppendElement (interp, rl->u.dbrec.buf);
2541 * do_getGrs: Get a GRS-1 Record
2543 static int do_getGrs (void *o, Tcl_Interp *interp, int argc, char **argv)
2545 IrTcl_SetObj *obj = o;
2547 IrTcl_RecordList *rl;
2553 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2554 " position ?(set,tag) (set,tag) ...?\"", NULL);
2557 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2559 rl = find_IR_record (obj, offset);
2562 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2565 if (rl->which != Z_NamePlusRecord_databaseRecord)
2567 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2570 if (rl->u.dbrec.type != VAL_GRS1)
2572 return ir_tcl_get_grs (interp, rl->u.dbrec.u.grs1, argc, argv);
2577 * do_getExplain: Get an Explain Record
2579 static int do_getExplain (void *o, Tcl_Interp *interp, int argc, char **argv)
2581 IrTcl_SetObj *obj = o;
2582 IrTcl_Obj *p = obj->parent;
2584 Z_ext_typeent *etype;
2586 IrTcl_RecordList *rl;
2592 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2593 " position ?mask? ...\"", NULL);
2596 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2598 rl = find_IR_record (obj, offset);
2601 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2604 if (rl->which != Z_NamePlusRecord_databaseRecord)
2606 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2609 if (!rl->u.dbrec.buf || rl->u.dbrec.type != VAL_EXPLAIN)
2612 if (!(etype = z_ext_getentbyref (VAL_EXPLAIN)))
2614 assert (rl->u.dbrec.buf);
2615 odr_setbuf (p->odr_in, rl->u.dbrec.buf, rl->u.dbrec.size, 0);
2616 if (!(*etype->fun)(p->odr_in, (char **) &rr, 0))
2619 if (etype->what != Z_External_explainRecord)
2622 return ir_tcl_get_explain (interp, rr, argc, argv);
2626 * do_responseStatus: Return response status (present or search)
2628 static int do_responseStatus (void *o, Tcl_Interp *interp,
2629 int argc, char **argv)
2631 IrTcl_SetObj *obj = o;
2635 obj->recordFlag = 0;
2636 obj->nonSurrogateDiagnosticNum = 0;
2637 obj->nonSurrogateDiagnosticList = NULL;
2640 else if (argc == -1)
2642 ir_deleteDiags (&obj->nonSurrogateDiagnosticList,
2643 &obj->nonSurrogateDiagnosticNum);
2646 if (!obj->recordFlag)
2648 Tcl_AppendElement (interp, "OK");
2653 case Z_Records_DBOSD:
2654 Tcl_AppendElement (interp, "DBOSD");
2657 Tcl_AppendElement (interp, "NSD");
2658 return ir_diagResult (interp, obj->nonSurrogateDiagnosticList,
2659 obj->nonSurrogateDiagnosticNum);
2665 * do_present: Perform Present Request
2668 static int do_present (void *o, Tcl_Interp *interp, int argc, char **argv)
2670 IrTcl_SetObj *obj = o;
2673 Z_PresentRequest *req;
2681 if (Tcl_GetInt (interp, argv[2], &start) == TCL_ERROR)
2688 if (Tcl_GetInt (interp, argv[3], &number) == TCL_ERROR)
2693 logf (LOG_DEBUG, "present %s %d %d", *argv, start, number);
2697 Tcl_AppendResult (interp, "not connected", NULL);
2698 return ir_tcl_error_exec (interp, argc, argv);
2701 obj->number = number;
2703 apdu = zget_APDU (p->odr_out, Z_APDU_presentRequest);
2704 req = apdu->u.presentRequest;
2706 set_referenceId (p->odr_out, &req->referenceId,
2707 obj->set_inher.referenceId);
2709 req->resultSetId = obj->setName ? obj->setName : "Default";
2711 req->resultSetStartPoint = &start;
2712 req->numberOfRecordsRequested = &number;
2713 if (obj->set_inher.preferredRecordSyntax)
2715 struct oident ident;
2717 ident.proto = p->protocol_type;
2718 ident.oclass = CLASS_RECSYN;
2719 ident.value = *obj->set_inher.preferredRecordSyntax;
2720 logf (LOG_DEBUG, "Preferred record syntax is %d", ident.value);
2721 req->preferredRecordSyntax = odr_oiddup (p->odr_out,
2722 oid_getoidbyent (&ident));
2725 req->preferredRecordSyntax = 0;
2727 if (obj->set_inher.elementSetNames && *obj->set_inher.elementSetNames)
2729 Z_ElementSetNames *esn = odr_malloc (p->odr_out, sizeof(*esn));
2730 Z_RecordComposition *compo = odr_malloc (p->odr_out, sizeof(*compo));
2732 esn->which = Z_ElementSetNames_generic;
2733 esn->u.generic = obj->set_inher.elementSetNames;
2735 req->recordComposition = compo;
2736 compo->which = Z_RecordComp_simple;
2737 compo->u.simple = esn;
2740 req->recordComposition = NULL;
2741 return ir_tcl_send_APDU (interp, p, apdu, "present", *argv);
2744 #define IR_TCL_RECORD_ENCODING_ISO2709 1
2745 #define IR_TCL_RECORD_ENCODING_RAW 2
2751 } IrTcl_FileRecordHead;
2754 * do_loadFile: Load result set from file
2756 static int do_loadFile (void *o, Tcl_Interp *interp,
2757 int argc, char **argv)
2759 IrTcl_SetObj *setobj = o;
2771 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2772 " filename ?start? ?number?\"", NULL);
2776 start = atoi (argv[3]);
2778 number = atoi (argv[4]);
2781 inf = fopen (argv[2], "r");
2784 Tcl_AppendResult (interp, "Cannot open ", argv[2], NULL);
2785 return ir_tcl_error_exec (interp, argc, argv);
2787 while (offset < (start+number))
2789 IrTcl_FileRecordHead head;
2790 IrTcl_RecordList *rl;
2792 if (fread (&head, sizeof(head), 1, inf) < 1)
2794 rl = new_IR_record (setobj, offset,
2795 Z_NamePlusRecord_databaseRecord,
2796 (argc > 5) ? argv[5] : NULL);
2797 rl->u.dbrec.type = head.syntax;
2798 if (head.encoding == IR_TCL_RECORD_ENCODING_ISO2709)
2800 if (!(buf = ir_tcl_fread_marc (inf, &size)))
2802 rl->u.dbrec.buf = buf;
2803 rl->u.dbrec.size = size;
2804 if (size != head.size)
2807 Tcl_AppendResult (interp, "bad ISO2709 encoding", NULL);
2808 return ir_tcl_error_exec (interp, argc, argv);
2811 else if (head.encoding == IR_TCL_RECORD_ENCODING_RAW)
2813 rl->u.dbrec.size = head.size;
2814 rl->u.dbrec.buf = ir_tcl_malloc (head.size + 1);
2815 if (fread (rl->u.dbrec.buf, rl->u.dbrec.size, 1, inf) < 1)
2818 Tcl_AppendResult (interp, "bad raw encoding", NULL);
2819 return ir_tcl_error_exec (interp, argc, argv);
2821 rl->u.dbrec.buf[rl->u.dbrec.size] = '\0';
2825 rl->u.dbrec.buf = NULL;
2826 rl->u.dbrec.size = 0;
2828 Tcl_AppendResult (interp, "bad encoding", NULL);
2829 return ir_tcl_error_exec (interp, argc, argv);
2833 setobj->numberOfRecordsReturned = offset - start;
2839 * do_saveFile: Save result set on file
2841 static int do_saveFile (void *o, Tcl_Interp *interp,
2842 int argc, char **argv)
2844 IrTcl_SetObj *setobj = o;
2849 IrTcl_RecordList *rl;
2855 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2856 " filename ?start? ?number?\"", NULL);
2860 start = atoi (argv[3]);
2862 number = atoi (argv[4]);
2865 outf = fopen (argv[2], "w");
2868 Tcl_AppendResult (interp, "cannot open file", NULL);
2869 return ir_tcl_error_exec (interp, argc, argv);
2871 while (offset < (start+number) && (rl = find_IR_record (setobj, offset)))
2873 if (rl->which == Z_NamePlusRecord_databaseRecord &&
2874 rl->u.dbrec.buf && rl->u.dbrec.size)
2876 IrTcl_FileRecordHead head;
2878 head.encoding = IR_TCL_RECORD_ENCODING_RAW;
2879 head.syntax = rl->u.dbrec.type;
2880 head.size = rl->u.dbrec.size;
2881 if (fwrite (&head, sizeof(head), 1, outf) < 1)
2883 Tcl_AppendResult (interp, "cannot write", NULL);
2884 return ir_tcl_error_exec (interp, argc, argv);
2886 if (fwrite (rl->u.dbrec.buf, rl->u.dbrec.size, 1, outf) < 1)
2888 Tcl_AppendResult (interp, "cannot write", NULL);
2889 return ir_tcl_error_exec (interp, argc, argv);
2896 Tcl_AppendResult (interp, "cannot write ", NULL);
2897 return ir_tcl_error_exec (interp, argc, argv);
2903 static IrTcl_Method ir_set_method_tab[] = {
2904 { "search", do_search, NULL},
2905 { "searchResponse", do_searchResponse, NULL},
2906 { "presentResponse", do_presentResponse, NULL},
2907 { "searchStatus", do_searchStatus, NULL},
2908 { "presentStatus", do_presentStatus, NULL},
2909 { "nextResultSetPosition", do_nextResultSetPosition, NULL},
2910 { "setName", do_setName, NULL},
2911 { "resultCount", do_resultCount, NULL},
2912 { "numberOfRecordsReturned", do_numberOfRecordsReturned, NULL},
2913 { "present", do_present, NULL},
2914 { "type", do_type, NULL},
2915 { "getMarc", do_getMarc, NULL},
2916 { "getSutrs", do_getSutrs, NULL},
2917 { "getGrs", do_getGrs, NULL},
2918 { "getExplain", do_getExplain, NULL},
2919 { "recordType", do_recordType, NULL},
2920 { "recordElements", do_recordElements, NULL},
2921 { "diag", do_diag, NULL},
2922 { "responseStatus", do_responseStatus, NULL},
2923 { "loadFile", do_loadFile, NULL},
2924 { "saveFile", do_saveFile, NULL},
2929 * ir_set_obj_method: IR Set Object methods
2931 static int ir_set_obj_method (ClientData clientData, Tcl_Interp *interp,
2932 int argc, char **argv)
2934 IrTcl_Methods tabs[3];
2935 IrTcl_SetObj *p = (IrTcl_SetObj *) clientData;
2940 Tcl_AppendResult (interp, wrongArgs, *argv, " method args...\"", NULL);
2943 tabs[0].tab = ir_set_method_tab;
2945 tabs[1].tab = ir_set_c_method_tab;
2946 tabs[1].obj = &p->set_inher;
2949 if (ir_tcl_method (interp, argc, argv, tabs, &r) == TCL_ERROR)
2950 return ir_tcl_method_error (interp, argc, argv, tabs);
2955 * ir_set_obj_delete: IR Set Object disposal
2957 static void ir_set_obj_delete (ClientData clientData)
2959 IrTcl_Methods tabs[3];
2960 IrTcl_SetObj *p = (IrTcl_SetObj *) clientData;
2962 logf (LOG_DEBUG, "ir set delete");
2964 tabs[0].tab = ir_set_method_tab;
2966 tabs[1].tab = ir_set_c_method_tab;
2967 tabs[1].obj = &p->set_inher;
2970 ir_tcl_method (NULL, -1, NULL, tabs, NULL);
2976 * ir_set_obj_init: IR Set Object initialization
2978 static int ir_set_obj_init (ClientData clientData, Tcl_Interp *interp,
2979 int argc, char **argv, ClientData *subData,
2980 ClientData parentData)
2982 IrTcl_Methods tabs[3];
2985 if (argc < 2 || argc > 3)
2987 Tcl_AppendResult (interp, wrongArgs, *argv,
2988 " objSetName ?objParent?\"", NULL);
2991 obj = ir_tcl_malloc (sizeof(*obj));
2992 logf (LOG_DEBUG, "ir set create %s", argv[1]);
2999 obj->parent = (IrTcl_Obj *) parentData;
3001 dst = &obj->set_inher;
3002 src = &obj->parent->set_inher;
3004 if ((dst->num_databaseNames = src->num_databaseNames))
3006 dst->databaseNames =
3007 ir_tcl_malloc (sizeof (*dst->databaseNames)
3008 * (1+dst->num_databaseNames));
3009 for (i = 0; i < dst->num_databaseNames; i++)
3010 if (ir_tcl_strdup (interp, &dst->databaseNames[i],
3011 src->databaseNames[i]) == TCL_ERROR)
3013 dst->databaseNames[i] = NULL;
3016 dst->databaseNames = NULL;
3017 if (ir_tcl_strdup (interp, &dst->queryType, src->queryType)
3021 if (ir_tcl_strdup (interp, &dst->referenceId, src->referenceId)
3025 if (ir_tcl_strdup (interp, &dst->elementSetNames, src->elementSetNames)
3029 if (ir_tcl_strdup (interp, &dst->smallSetElementSetNames,
3030 src->smallSetElementSetNames)
3034 if (ir_tcl_strdup (interp, &dst->mediumSetElementSetNames,
3035 src->mediumSetElementSetNames)
3039 if (src->preferredRecordSyntax &&
3040 (dst->preferredRecordSyntax
3041 = ir_tcl_malloc (sizeof(*dst->preferredRecordSyntax))))
3042 *dst->preferredRecordSyntax = *src->preferredRecordSyntax;
3044 dst->preferredRecordSyntax = NULL;
3045 dst->replaceIndicator = src->replaceIndicator;
3046 dst->smallSetUpperBound = src->smallSetUpperBound;
3047 dst->largeSetLowerBound = src->largeSetLowerBound;
3048 dst->mediumSetPresentNumber = src->mediumSetPresentNumber;
3053 tabs[0].tab = ir_set_method_tab;
3057 if (ir_tcl_method (interp, 0, NULL, tabs, NULL) == TCL_ERROR)
3060 *subData = (ClientData) obj;
3065 * ir_set_obj_mk: IR Set Object creation
3067 static int ir_set_obj_mk (ClientData clientData, Tcl_Interp *interp,
3068 int argc, char **argv)
3071 ClientData parentData = 0;
3076 Tcl_CmdInfo parent_info;
3077 if (!Tcl_GetCommandInfo (interp, argv[2], &parent_info))
3079 Tcl_AppendResult (interp, "no object parent", NULL);
3080 return ir_tcl_error_exec (interp, argc, argv);
3082 parentData = parent_info.clientData;
3084 r = ir_set_obj_init (clientData, interp, argc, argv, &subData, parentData);
3087 Tcl_CreateCommand (interp, argv[1], ir_set_obj_method,
3088 subData, ir_set_obj_delete);
3092 IrTcl_Class ir_set_obj_class = {
3099 /* ------------------------------------------------------- */
3102 * do_scan: Perform scan
3104 static int do_scan (void *o, Tcl_Interp *interp, int argc, char **argv)
3108 IrTcl_ScanObj *obj = o;
3109 IrTcl_Obj *p = obj->parent;
3112 struct ccl_rpn_node *rpn;
3120 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
3121 " scanQuery\"", NULL);
3124 logf (LOG_DEBUG, "scan %s %s", *argv, argv[2]);
3125 if (!p->set_inher.num_databaseNames)
3127 Tcl_AppendResult (interp, "no databaseNames", NULL);
3128 return ir_tcl_error_exec (interp, argc, argv);
3132 Tcl_AppendResult (interp, "not connected", NULL);
3133 return ir_tcl_error_exec (interp, argc, argv);
3136 apdu = zget_APDU (p->odr_out, Z_APDU_scanRequest);
3137 req = apdu->u.scanRequest;
3139 set_referenceId (p->odr_out, &req->referenceId, p->set_inher.referenceId);
3140 req->num_databaseNames = p->set_inher.num_databaseNames;
3141 req->databaseNames = p->set_inher.databaseNames;
3144 if (!(req->termListAndStartPoint =
3145 p_query_scan (p->odr_out, p->protocol_type,
3146 &req->attributeSet, argv[2])))
3148 Tcl_AppendResult (interp, "query syntax error", NULL);
3149 return ir_tcl_error_exec (interp, argc, argv);
3152 rpn = ccl_find_str(p->bibset, argv[2], &r, &pos);
3155 Tcl_AppendResult (interp, "ccl syntax error ", ccl_err_msg(r), NULL);
3156 return ir_tcl_error_exec (interp, argc, argv);
3158 bib1.proto = p->protocol_type;
3159 bib1.oclass = CLASS_ATTSET;
3160 bib1.value = VAL_BIB1;
3162 req->attributeSet = oid_getoidbyent (&bib1);
3163 if (!(req->termListAndStartPoint = ccl_scan_query (rpn)))
3166 req->stepSize = &obj->stepSize;
3167 req->numberOfTermsRequested = &obj->numberOfTermsRequested;
3168 req->preferredPositionInResponse = &obj->preferredPositionInResponse;
3169 logf (LOG_DEBUG, "stepSize=%d", *req->stepSize);
3170 logf (LOG_DEBUG, "numberOfTermsRequested=%d",
3171 *req->numberOfTermsRequested);
3172 logf (LOG_DEBUG, "preferredPositionInResponse=%d",
3173 *req->preferredPositionInResponse);
3175 return ir_tcl_send_APDU (interp, p, apdu, "scan", *argv);
3179 * do_scanResponse: add scan response handler
3181 static int do_scanResponse (void *o, Tcl_Interp *interp,
3182 int argc, char **argv)
3184 IrTcl_ScanObj *obj = o;
3188 obj->scanResponse = NULL;
3191 else if (argc == -1)
3192 return ir_tcl_strdel (interp, &obj->scanResponse);
3195 xfree (obj->scanResponse);
3198 if (ir_tcl_strdup (interp, &obj->scanResponse, argv[2])
3203 obj->scanResponse = NULL;
3209 * do_stepSize: Set/get replace Step Size
3211 static int do_stepSize (void *obj, Tcl_Interp *interp,
3212 int argc, char **argv)
3214 IrTcl_ScanObj *p = obj;
3220 return ir_tcl_get_set_int (&p->stepSize, interp, argc, argv);
3224 * do_numberOfTermsRequested: Set/get Number of Terms requested
3226 static int do_numberOfTermsRequested (void *obj, Tcl_Interp *interp,
3227 int argc, char **argv)
3229 IrTcl_ScanObj *p = obj;
3233 p->numberOfTermsRequested = 20;
3236 return ir_tcl_get_set_int (&p->numberOfTermsRequested, interp, argc, argv);
3241 * do_preferredPositionInResponse: Set/get preferred Position
3243 static int do_preferredPositionInResponse (void *obj, Tcl_Interp *interp,
3244 int argc, char **argv)
3246 IrTcl_ScanObj *p = obj;
3250 p->preferredPositionInResponse = 1;
3253 return ir_tcl_get_set_int (&p->preferredPositionInResponse, interp,
3258 * do_scanStatus: Get scan status
3260 static int do_scanStatus (void *obj, Tcl_Interp *interp,
3261 int argc, char **argv)
3263 IrTcl_ScanObj *p = obj;
3267 return ir_tcl_get_set_int (&p->scanStatus, interp, argc, argv);
3271 * do_numberOfEntriesReturned: Get number of Entries returned
3273 static int do_numberOfEntriesReturned (void *obj, Tcl_Interp *interp,
3274 int argc, char **argv)
3276 IrTcl_ScanObj *p = obj;
3280 return ir_tcl_get_set_int (&p->numberOfEntriesReturned, interp,
3285 * do_positionOfTerm: Get position of Term
3287 static int do_positionOfTerm (void *obj, Tcl_Interp *interp,
3288 int argc, char **argv)
3290 IrTcl_ScanObj *p = obj;
3294 return ir_tcl_get_set_int (&p->positionOfTerm, interp, argc, argv);
3298 * do_scanLine: get Scan Line (surrogate or normal) after response
3300 static int do_scanLine (void *obj, Tcl_Interp *interp, int argc, char **argv)
3302 IrTcl_ScanObj *p = obj;
3308 p->entries_flag = 0;
3310 p->nonSurrogateDiagnosticNum = 0;
3311 p->nonSurrogateDiagnosticList = 0;
3314 else if (argc == -1)
3316 p->entries_flag = 0;
3317 /* release entries */
3320 ir_deleteDiags (&p->nonSurrogateDiagnosticList,
3321 &p->nonSurrogateDiagnosticNum);
3326 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
3327 " position\"", NULL);
3330 if (Tcl_GetInt (interp, argv[2], &i) == TCL_ERROR)
3332 if (!p->entries_flag || p->which != Z_ListEntries_entries || !p->entries
3333 || i >= p->num_entries || i < 0)
3335 switch (p->entries[i].which)
3337 case Z_Entry_termInfo:
3338 Tcl_AppendElement (interp, "T");
3339 if (p->entries[i].u.term.buf)
3340 Tcl_AppendElement (interp, p->entries[i].u.term.buf);
3342 Tcl_AppendElement (interp, "");
3343 sprintf (numstr, "%d", p->entries[i].u.term.globalOccurrences);
3344 Tcl_AppendElement (interp, numstr);
3346 case Z_Entry_surrogateDiagnostic:
3347 Tcl_AppendElement (interp, "SD");
3348 return ir_diagResult (interp, p->entries[i].u.diag.list,
3349 p->entries[i].u.diag.num);
3355 static IrTcl_Method ir_scan_method_tab[] = {
3356 { "scan", do_scan, NULL},
3357 { "scanResponse", do_scanResponse, NULL},
3358 { "stepSize", do_stepSize, NULL},
3359 { "numberOfTermsRequested", do_numberOfTermsRequested, NULL},
3360 { "preferredPositionInResponse", do_preferredPositionInResponse, NULL},
3361 { "scanStatus", do_scanStatus, NULL},
3362 { "numberOfEntriesReturned", do_numberOfEntriesReturned, NULL},
3363 { "positionOfTerm", do_positionOfTerm, NULL},
3364 { "scanLine", do_scanLine, NULL},
3369 * ir_scan_obj_method: IR Scan Object methods
3371 static int ir_scan_obj_method (ClientData clientData, Tcl_Interp *interp,
3372 int argc, char **argv)
3374 IrTcl_Methods tabs[2];
3379 Tcl_AppendResult (interp, wrongArgs, *argv, " method args...\"", NULL);
3382 tabs[0].tab = ir_scan_method_tab;
3383 tabs[0].obj = clientData;
3386 if (ir_tcl_method (interp, argc, argv, tabs, &r) == TCL_ERROR)
3387 return ir_tcl_method_error (interp, argc, argv, tabs);
3392 * ir_scan_obj_delete: IR Scan Object disposal
3394 static void ir_scan_obj_delete (ClientData clientData)
3396 IrTcl_Methods tabs[2];
3397 IrTcl_ScanObj *obj = (IrTcl_ScanObj *) clientData;
3399 tabs[0].tab = ir_scan_method_tab;
3403 ir_tcl_method (NULL, -1, NULL, tabs, NULL);
3408 * ir_scan_obj_mk: IR Scan Object creation
3410 static int ir_scan_obj_mk (ClientData clientData, Tcl_Interp *interp,
3411 int argc, char **argv)
3413 Tcl_CmdInfo parent_info;
3415 IrTcl_Methods tabs[2];
3419 Tcl_AppendResult (interp, wrongArgs, *argv,
3420 "objScanName objParentName\"", NULL);
3423 logf (LOG_DEBUG, "ir scan create %s", argv[1]);
3424 if (!Tcl_GetCommandInfo (interp, argv[2], &parent_info))
3426 Tcl_AppendResult (interp, "no object parent", NULL);
3427 return ir_tcl_error_exec (interp, argc, argv);
3429 obj = ir_tcl_malloc (sizeof(*obj));
3430 obj->parent = (IrTcl_Obj *) parent_info.clientData;
3432 tabs[0].tab = ir_scan_method_tab;
3436 if (ir_tcl_method (interp, 0, NULL, tabs, NULL) == TCL_ERROR)
3438 Tcl_CreateCommand (interp, argv[1], ir_scan_obj_method,
3439 (ClientData) obj, ir_scan_obj_delete);
3443 /* ------------------------------------------------------- */
3446 * ir_log_init_proc: set yaz log level
3448 static int ir_log_init_proc (ClientData clientData, Tcl_Interp *interp,
3449 int argc, char **argv)
3451 if (argc <= 1 || argc > 4)
3453 Tcl_AppendResult (interp, wrongArgs, *argv,
3454 " ?level ?prefix ?filename\"", NULL);
3458 log_init (log_mask_str (argv[1]), "", NULL);
3460 log_init (log_mask_str (argv[1]), argv[2], NULL);
3462 log_init (log_mask_str (argv[1]), argv[2], argv[3]);
3467 * ir_log_proc: log yaz message
3469 static int ir_log_proc (ClientData clientData, Tcl_Interp *interp,
3470 int argc, char **argv)
3475 Tcl_AppendResult (interp, wrongArgs, *argv,
3476 " level string\"", NULL);
3479 mask = log_mask_str_x (argv[1], 0);
3480 logf (LOG_DEBUG, "%s", argv[2]);
3485 /* ------------------------------------------------------- */
3486 static void ir_initResponse (void *obj, Z_InitResponse *initrs)
3490 p->initResult = *initrs->result ? 1 : 0;
3491 if (!*initrs->result)
3492 logf (LOG_DEBUG, "Connection rejected by target");
3494 logf (LOG_DEBUG, "Connection accepted by target");
3496 get_referenceId (&p->set_inher.referenceId, initrs->referenceId);
3498 xfree (p->targetImplementationId);
3499 ir_tcl_strdup (p->interp, &p->targetImplementationId,
3500 initrs->implementationId);
3501 xfree (p->targetImplementationName);
3502 ir_tcl_strdup (p->interp, &p->targetImplementationName,
3503 initrs->implementationName);
3504 xfree (p->targetImplementationVersion);
3505 ir_tcl_strdup (p->interp, &p->targetImplementationVersion,
3506 initrs->implementationVersion);
3508 p->maximumRecordSize = *initrs->maximumRecordSize;
3509 p->preferredMessageSize = *initrs->preferredMessageSize;
3511 memcpy (&p->options, initrs->options, sizeof(initrs->options));
3512 memcpy (&p->protocolVersion, initrs->protocolVersion,
3513 sizeof(initrs->protocolVersion));
3514 xfree (p->userInformationField);
3515 p->userInformationField = NULL;
3516 if (initrs->userInformationField)
3520 if (initrs->userInformationField->which == ODR_EXTERNAL_octet &&
3521 (p->userInformationField =
3522 ir_tcl_malloc ((len =
3523 initrs->userInformationField->
3524 u.octet_aligned->len) +1)))
3526 memcpy (p->userInformationField,
3527 initrs->userInformationField->u.octet_aligned->buf,
3529 (p->userInformationField)[len] = '\0';
3534 static void ir_deleteDiags (IrTcl_Diagnostic **dst_list, int *dst_num)
3537 for (i = 0; i<*dst_num; i++)
3538 xfree (dst_list[i]->addinfo);
3544 static void ir_handleDiags (IrTcl_Diagnostic **dst_list, int *dst_num,
3545 Z_DiagRec **list, int num)
3551 *dst_list = ir_tcl_malloc (sizeof(**dst_list) * num);
3552 for (i = 0; i<num; i++)
3555 switch (list[i]->which)
3557 case Z_DiagRec_defaultFormat:
3558 (*dst_list)[i].condition = *list[i]->u.defaultFormat->condition;
3559 addinfo = list[i]->u.defaultFormat->addinfo;
3561 ((*dst_list)[i].addinfo = ir_tcl_malloc (strlen(addinfo)+1)))
3562 strcpy ((*dst_list)[i].addinfo, addinfo);
3563 cp = diagbib1_str ((*dst_list)[i].condition);
3564 logf (LOG_DEBUG, "Diag %d %s %s", (*dst_list)[i].condition,
3565 cp ? cp : "", addinfo ? addinfo : "");
3568 (*dst_list)[i].addinfo = NULL;
3569 (*dst_list)[i].condition = 0;
3574 static void ir_handleDBRecord (IrTcl_Obj *p, IrTcl_RecordList *rl,
3577 struct oident *ident;
3578 Z_ext_typeent *etype;
3580 rl->u.dbrec.size = oe->u.octet_aligned->len;
3581 rl->u.dbrec.buf = NULL;
3583 if ((ident = oid_getentbyoid (oe->direct_reference)))
3584 rl->u.dbrec.type = ident->value;
3586 rl->u.dbrec.type = VAL_USMARC;
3588 if (ident && (oe->which == Z_External_single ||
3589 oe->which == Z_External_octet)
3590 && (etype = z_ext_getentbyref (ident->value)))
3594 odr_setbuf (p->odr_in, (char*) oe->u.octet_aligned->buf,
3595 oe->u.octet_aligned->len, 0);
3596 if (!(*etype->fun)(p->odr_in, (char **) &rr, 0))
3598 switch (etype->what)
3600 case Z_External_sutrs:
3601 logf (LOG_DEBUG, "Z_External_sutrs");
3603 if ((rl->u.dbrec.buf = ir_tcl_malloc (oe->u.sutrs->len+1)))
3605 memcpy (rl->u.dbrec.buf, oe->u.sutrs->buf,
3607 rl->u.dbrec.buf[oe->u.sutrs->len] = '\0';
3609 rl->u.dbrec.size = oe->u.sutrs->len;
3611 case Z_External_grs1:
3612 logf (LOG_DEBUG, "Z_External_grs1");
3614 ir_tcl_grs_mk (oe->u.grs1, &rl->u.dbrec.u.grs1);
3616 case Z_External_explainRecord:
3617 logf (LOG_DEBUG, "Z_External_explainRecord");
3618 if ((rl->u.dbrec.buf = ir_tcl_malloc (rl->u.dbrec.size)))
3620 memcpy (rl->u.dbrec.buf, oe->u.octet_aligned->buf,
3628 if (oe->which == Z_External_octet && rl->u.dbrec.size > 0)
3630 char *buf = (char*) oe->u.octet_aligned->buf;
3631 if ((rl->u.dbrec.buf = ir_tcl_malloc (rl->u.dbrec.size)))
3632 memcpy (rl->u.dbrec.buf, buf, rl->u.dbrec.size);
3634 else if (rl->u.dbrec.type == VAL_SUTRS &&
3635 oe->which == Z_External_sutrs)
3637 if ((rl->u.dbrec.buf = ir_tcl_malloc (oe->u.sutrs->len+1)))
3639 memcpy (rl->u.dbrec.buf, oe->u.sutrs->buf,
3641 rl->u.dbrec.buf[oe->u.sutrs->len] = '\0';
3643 rl->u.dbrec.size = oe->u.sutrs->len;
3645 else if (rl->u.dbrec.type == VAL_GRS1 &&
3646 oe->which == Z_External_grs1)
3648 ir_tcl_grs_mk (oe->u.grs1, &rl->u.dbrec.u.grs1);
3653 static void ir_handleZRecords (void *o, Z_Records *zrs, IrTcl_SetObj *setobj,
3654 const char *elements)
3659 IrTcl_RecordList *rl;
3661 setobj->which = zrs->which;
3662 setobj->recordFlag = 1;
3664 ir_deleteDiags (&setobj->nonSurrogateDiagnosticList,
3665 &setobj->nonSurrogateDiagnosticNum);
3666 if (zrs->which == Z_Records_DBOSD)
3668 int num_rec = zrs->u.databaseOrSurDiagnostics->num_records;
3670 if (num_rec != setobj->numberOfRecordsReturned)
3672 logf (LOG_WARN, "numberOfRecordsReturned=%d but num records=%d",
3673 setobj->numberOfRecordsReturned, num_rec);
3674 setobj->numberOfRecordsReturned = num_rec;
3677 for (offset = 0; offset < num_rec; offset++)
3679 Z_NamePlusRecord *znpr = zrs->u.databaseOrSurDiagnostics->
3682 rl = new_IR_record (setobj, setobj->start + offset, znpr->which,
3684 if (rl->which == Z_NamePlusRecord_surrogateDiagnostic)
3685 ir_handleDiags (&rl->u.surrogateDiagnostics.list,
3686 &rl->u.surrogateDiagnostics.num,
3687 &znpr->u.surrogateDiagnostic,
3690 ir_handleDBRecord (p, rl,
3691 (Z_External*) (znpr->u.databaseRecord));
3694 else if (zrs->which == Z_Records_multipleNSD)
3696 logf (LOG_DEBUG, "multipleNonSurrogateDiagnostic %d",
3697 zrs->u.multipleNonSurDiagnostics->num_diagRecs);
3698 setobj->numberOfRecordsReturned = 0;
3699 ir_handleDiags (&setobj->nonSurrogateDiagnosticList,
3700 &setobj->nonSurrogateDiagnosticNum,
3701 zrs->u.multipleNonSurDiagnostics->diagRecs,
3702 zrs->u.multipleNonSurDiagnostics->num_diagRecs);
3706 logf (LOG_DEBUG, "NonSurrogateDiagnostic");
3707 setobj->numberOfRecordsReturned = 0;
3708 ir_handleDiags (&setobj->nonSurrogateDiagnosticList,
3709 &setobj->nonSurrogateDiagnosticNum,
3710 &zrs->u.nonSurrogateDiagnostic,
3715 static void ir_searchResponse (void *o, Z_SearchResponse *searchrs,
3716 IrTcl_SetObj *setobj)
3718 Z_Records *zrs = searchrs->records;
3720 logf (LOG_DEBUG, "Received search response");
3723 logf (LOG_DEBUG, "Search response, no object!");
3726 setobj->searchStatus = searchrs->searchStatus ? 1 : 0;
3727 get_referenceId (&setobj->set_inher.referenceId, searchrs->referenceId);
3728 setobj->resultCount = *searchrs->resultCount;
3729 if (searchrs->presentStatus)
3730 setobj->presentStatus = *searchrs->presentStatus;
3731 if (searchrs->nextResultSetPosition)
3732 setobj->nextResultSetPosition = *searchrs->nextResultSetPosition;
3734 logf (LOG_DEBUG, "status %d hits %d",
3735 setobj->searchStatus, setobj->resultCount);
3739 if (setobj->resultCount <= setobj->set_inher.smallSetUpperBound)
3740 es = setobj->set_inher.smallSetElementSetNames;
3742 es = setobj->set_inher.mediumSetElementSetNames;
3743 setobj->numberOfRecordsReturned = *searchrs->numberOfRecordsReturned;
3744 ir_handleZRecords (o, zrs, setobj, es);
3748 setobj->numberOfRecordsReturned = 0;
3749 setobj->recordFlag = 0;
3754 static void ir_presentResponse (void *o, Z_PresentResponse *presrs,
3755 IrTcl_SetObj *setobj)
3757 Z_Records *zrs = presrs->records;
3759 logf (LOG_DEBUG, "Received present response");
3762 logf (LOG_DEBUG, "Present response, no object!");
3765 setobj->presentStatus = *presrs->presentStatus;
3766 get_referenceId (&setobj->set_inher.referenceId, presrs->referenceId);
3767 setobj->nextResultSetPosition = *presrs->nextResultSetPosition;
3770 setobj->numberOfRecordsReturned = *presrs->numberOfRecordsReturned;
3771 ir_handleZRecords (o, zrs, setobj, setobj->set_inher.elementSetNames);
3775 setobj->numberOfRecordsReturned = 0;
3776 setobj->recordFlag = 0;
3777 logf (LOG_DEBUG, "No records!");
3781 static void ir_scanResponse (void *o, Z_ScanResponse *scanrs,
3782 IrTcl_ScanObj *scanobj)
3786 logf (LOG_DEBUG, "Received scanResponse");
3788 get_referenceId (&p->set_inher.referenceId, scanrs->referenceId);
3789 scanobj->scanStatus = *scanrs->scanStatus;
3790 logf (LOG_DEBUG, "scanStatus=%d", scanobj->scanStatus);
3792 if (scanrs->stepSize)
3793 scanobj->stepSize = *scanrs->stepSize;
3794 logf (LOG_DEBUG, "stepSize=%d", scanobj->stepSize);
3796 scanobj->numberOfEntriesReturned = *scanrs->numberOfEntriesReturned;
3797 logf (LOG_DEBUG, "numberOfEntriesReturned=%d",
3798 scanobj->numberOfEntriesReturned);
3800 if (scanrs->positionOfTerm)
3801 scanobj->positionOfTerm = *scanrs->positionOfTerm;
3803 scanobj->positionOfTerm = -1;
3804 logf (LOG_DEBUG, "positionOfTerm=%d", scanobj->positionOfTerm);
3806 xfree (scanobj->entries);
3807 scanobj->entries = NULL;
3809 ir_deleteDiags (&scanobj->nonSurrogateDiagnosticList,
3810 &scanobj->nonSurrogateDiagnosticNum);
3811 if (scanrs->entries)
3816 scanobj->entries_flag = 1;
3817 scanobj->which = scanrs->entries->which;
3818 switch (scanobj->which)
3820 case Z_ListEntries_entries:
3821 scanobj->num_entries = scanrs->entries->u.entries->num_entries;
3822 scanobj->entries = ir_tcl_malloc (scanobj->num_entries *
3823 sizeof(*scanobj->entries));
3824 for (i=0; i<scanobj->num_entries; i++)
3826 ze = scanrs->entries->u.entries->entries[i];
3827 scanobj->entries[i].which = ze->which;
3830 case Z_Entry_termInfo:
3831 if (ze->u.termInfo->term->which == Z_Term_general)
3833 int l = ze->u.termInfo->term->u.general->len;
3834 scanobj->entries[i].u.term.buf = ir_tcl_malloc (1+l);
3835 memcpy (scanobj->entries[i].u.term.buf,
3836 ze->u.termInfo->term->u.general->buf,
3838 scanobj->entries[i].u.term.buf[l] = '\0';
3841 scanobj->entries[i].u.term.buf = NULL;
3842 if (ze->u.termInfo->globalOccurrences)
3843 scanobj->entries[i].u.term.globalOccurrences =
3844 *ze->u.termInfo->globalOccurrences;
3846 scanobj->entries[i].u.term.globalOccurrences = 0;
3848 case Z_Entry_surrogateDiagnostic:
3849 ir_handleDiags (&scanobj->entries[i].u.diag.list,
3850 &scanobj->entries[i].u.diag.num,
3851 &ze->u.surrogateDiagnostic,
3857 case Z_ListEntries_nonSurrogateDiagnostics:
3858 ir_handleDiags (&scanobj->nonSurrogateDiagnosticList,
3859 &scanobj->nonSurrogateDiagnosticNum,
3860 scanrs->entries->u.nonSurrogateDiagnostics->
3862 scanrs->entries->u.nonSurrogateDiagnostics->
3868 scanobj->entries_flag = 0;
3872 * ir_select_read: handle incoming packages
3874 static void ir_select_read (ClientData clientData)
3876 IrTcl_Obj *p = (IrTcl_Obj *) clientData;
3881 Tcl_CmdInfo cmd_info;
3882 const char *apdu_call;
3884 logf(LOG_DEBUG, "Read handler fd=%d", cs_fileno(p->cs_link));
3885 if (p->state == IR_TCL_R_Connecting)
3887 logf(LOG_DEBUG, "read: connect");
3888 r = cs_rcvconnect (p->cs_link);
3891 logf (LOG_WARN, "cs_rcvconnect returned 1");
3894 p->state = IR_TCL_R_Idle;
3896 ir_select_remove_write (cs_fileno (p->cs_link), p);
3899 logf (LOG_DEBUG, "cs_rcvconnect error");
3900 ir_tcl_disconnect (p);
3903 p->failInfo = IR_TCL_FAIL_CONNECT;
3904 ir_tcl_eval (p->interp, p->failback);
3906 ir_obj_delete ((ClientData) p);
3910 ir_tcl_eval (p->interp, p->callback);
3911 if (p->ref_count == 2 && p->cs_link && p->request_queue
3912 && p->state == IR_TCL_R_Idle)
3913 ir_tcl_send_q (p, p->request_queue, "x");
3914 ir_obj_delete ((ClientData) p);
3919 p->state = IR_TCL_R_Reading;
3921 /* read incoming APDU */
3922 if ((r=cs_get (p->cs_link, &p->buf_in, &p->len_in)) == 1)
3924 logf(LOG_DEBUG, "PDU Fraction read");
3927 /* signal one more use of ir object - callbacks must not
3928 release the ir memory (p pointer) */
3932 logf (LOG_DEBUG, "cs_get failed, code %d", r);
3933 ir_tcl_disconnect (p);
3936 p->failInfo = IR_TCL_FAIL_READ;
3937 ir_tcl_eval (p->interp, p->failback);
3939 /* release ir object now if callback deleted it */
3940 ir_obj_delete ((ClientData) p);
3943 /* got complete APDU. Now decode */
3946 odr_setbuf (p->odr_in, p->buf_in, r, 0);
3947 logf (LOG_DEBUG, "cs_get ok, total size %d", r);
3948 if (!z_APDU (p->odr_in, &apdu, 0))
3950 logf (LOG_DEBUG, "cs_get failed: %s",
3951 odr_errmsg (odr_geterror (p->odr_in)));
3952 ir_tcl_disconnect (p);
3955 p->failInfo = IR_TCL_FAIL_IN_APDU;
3956 p->apduOffset = odr_offset (p->odr_in);
3957 ir_tcl_eval (p->interp, p->failback);
3959 /* release ir object now if failback deleted it */
3960 ir_obj_delete ((ClientData) p);
3963 /* handle APDU and invoke callback */
3964 rq = p->request_queue;
3967 logf (LOG_FATAL, "Internal error. No queue entry");
3970 object_name = rq->object_name;
3971 logf (LOG_DEBUG, "Object %s", object_name);
3973 if (Tcl_GetCommandInfo (p->interp, object_name, &cmd_info))
3977 case Z_APDU_initResponse:
3978 p->eventType = "init";
3979 ir_initResponse (p, apdu->u.initResponse);
3980 apdu_call = p->initResponse;
3982 case Z_APDU_searchResponse:
3983 p->eventType = "search";
3984 ir_searchResponse (p, apdu->u.searchResponse,
3985 (IrTcl_SetObj *) cmd_info.clientData);
3986 apdu_call = ((IrTcl_SetObj *)
3987 cmd_info.clientData)->searchResponse;
3989 case Z_APDU_presentResponse:
3990 p->eventType = "present";
3991 ir_presentResponse (p, apdu->u.presentResponse,
3992 (IrTcl_SetObj *) cmd_info.clientData);
3993 apdu_call = ((IrTcl_SetObj *)
3994 cmd_info.clientData)->presentResponse;
3996 case Z_APDU_scanResponse:
3997 p->eventType = "scan";
3998 ir_scanResponse (p, apdu->u.scanResponse,
3999 (IrTcl_ScanObj *) cmd_info.clientData);
4000 apdu_call = ((IrTcl_ScanObj *)
4001 cmd_info.clientData)->scanResponse;
4004 logf (LOG_WARN, "Received unknown APDU type (%d)",
4006 ir_tcl_disconnect (p);
4009 p->failInfo = IR_TCL_FAIL_UNKNOWN_APDU;
4010 ir_tcl_eval (p->interp, p->failback);
4015 p->request_queue = rq->next;
4016 p->state = IR_TCL_R_Idle;
4019 ir_tcl_eval (p->interp, apdu_call);
4020 else if (rq->callback)
4021 ir_tcl_eval (p->interp, rq->callback);
4022 xfree (rq->buf_out);
4023 xfree (rq->callback);
4024 xfree (rq->object_name);
4026 odr_reset (p->odr_in);
4027 if (p->ref_count == 1)
4029 ir_obj_delete ((ClientData) p);
4032 ir_obj_delete ((ClientData) p);
4033 } while (p->cs_link && cs_more (p->cs_link));
4034 if (p->cs_link && p->request_queue && p->state == IR_TCL_R_Idle)
4035 ir_tcl_send_q (p, p->request_queue, "x");
4039 * ir_select_write: handle outgoing packages - not yet written.
4041 static int ir_select_write (ClientData clientData)
4043 IrTcl_Obj *p = (IrTcl_Obj *) clientData;
4047 logf (LOG_DEBUG, "Write handler fd=%d", cs_fileno(p->cs_link));
4048 if (p->state == IR_TCL_R_Connecting)
4050 logf(LOG_DEBUG, "write: connect");
4051 r = cs_rcvconnect (p->cs_link);
4054 logf (LOG_DEBUG, "cs_rcvconnect returned 1");
4057 p->state = IR_TCL_R_Idle;
4059 ir_select_remove_write (cs_fileno (p->cs_link), p);
4062 logf (LOG_DEBUG, "cs_rcvconnect error");
4063 ir_tcl_disconnect (p);
4066 p->failInfo = IR_TCL_FAIL_CONNECT;
4067 ir_tcl_eval (p->interp, p->failback);
4069 ir_obj_delete ((ClientData) p);
4073 ir_tcl_eval (p->interp, p->callback);
4074 ir_obj_delete ((ClientData) p);
4077 rq = p->request_queue;
4078 if (!rq || !rq->buf_out)
4081 if ((r=cs_put (p->cs_link, rq->buf_out, rq->len_out)) < 0)
4083 logf (LOG_DEBUG, "cs_put write fail");
4085 xfree (rq->buf_out);
4087 ir_tcl_disconnect (p);
4090 p->failInfo = IR_TCL_FAIL_WRITE;
4091 ir_tcl_eval (p->interp, p->failback);
4093 ir_obj_delete ((ClientData) p);
4095 else if (r == 0) /* remove select bit */
4097 logf (LOG_DEBUG, "Write completed");
4098 p->state = IR_TCL_R_Waiting;
4099 ir_select_remove_write (cs_fileno (p->cs_link), p);
4100 xfree (rq->buf_out);
4106 static void ir_select_notify (ClientData clientData, int r, int w, int e)
4110 if (!ir_select_write (clientData) && r)
4111 ir_select_read (clientData);
4115 ir_select_read (clientData);
4119 /*----------------------------------------------------------- */
4123 * This wrapper function is used by Windows to invoke the
4124 * initialization code for the DLL. If we are compiling
4125 * with Visual C++, this routine will be renamed to DllMain.
4137 DllEntryPoint(hInst, reason, reserved)
4138 HINSTANCE hInst; /* Library instance handle. */
4139 DWORD reason; /* Reason this function is being called. */
4140 LPVOID reserved; /* Not used. */
4146 /* ------------------------------------------------------- */
4148 * Irtcl_init: Registration of TCL commands.
4150 EXPORT (int,Irtcl_Init) (Tcl_Interp *interp)
4152 Tcl_CreateCommand (interp, "ir", ir_obj_mk, (ClientData) NULL,
4153 (Tcl_CmdDeleteProc *) NULL);
4154 Tcl_CreateCommand (interp, "ir-set", ir_set_obj_mk,
4155 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
4156 Tcl_CreateCommand (interp, "ir-scan", ir_scan_obj_mk,
4157 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
4158 Tcl_CreateCommand (interp, "ir-log-init", ir_log_init_proc,
4159 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
4160 Tcl_CreateCommand (interp, "ir-log", ir_log_proc,
4161 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);