2 * IR toolkit for tcl/tk
3 * (c) Index Data 1995-1996
4 * See the file LICENSE for details.
5 * Sebastian Hammer, Adam Dickmeiss
8 * Revision 1.98 1997-04-13 18:57:20 adam
9 * Better error reporting and aligned with Tcl/Tk style.
10 * Rework of notifier code with Tcl_File handles.
12 * Revision 1.97 1996/11/14 17:11:07 adam
13 * Added Explain documentaion.
15 * Revision 1.96 1996/10/08 13:02:50 adam
16 * When dealing with records, odr_choice_enable_bias function is used to
17 * prevent decoding of externals.
19 * Revision 1.95 1996/09/13 10:51:49 adam
20 * Bug fix: ir_tcl_select_set called Tcl_GetFile at disconnect.
22 * Revision 1.94 1996/08/21 13:32:53 adam
23 * Implemented saveFile method and extended loadFile method to work with it.
25 * Revision 1.93 1996/08/16 15:07:45 adam
26 * First work on Explain.
28 * Revision 1.92 1996/08/09 15:33:07 adam
29 * Modified the code to use tk4.1/tcl7.5 patch level 1. The time-driven
30 * polling is no longer activated on Windows since asynchrounous I/O works
33 * Revision 1.91 1996/07/03 13:31:11 adam
34 * The xmalloc/xfree functions from YAZ are used to manage memory.
36 * Revision 1.90 1996/06/27 14:21:00 adam
37 * Yet another Windows port.
39 * Revision 1.89 1996/06/11 15:27:15 adam
40 * Event type set to connect a little earlier in the do_connect function.
42 * Revision 1.88 1996/06/03 09:04:22 adam
43 * Changed a few logf calls.
45 * Revision 1.87 1996/05/29 06:37:51 adam
46 * Function ir_tcl_get_grs_r enhanced so that specific elements can be
49 * Revision 1.86 1996/03/20 13:54:04 adam
50 * The Tcl_File structure is only manipulated in the Tk-event interface
53 * Revision 1.85 1996/03/15 11:15:48 adam
54 * Modified to use new prototypes for p_query_rpn and p_query_scan.
56 * Revision 1.84 1996/03/07 12:42:49 adam
57 * Better logging when callback is invoked.
59 * Revision 1.83 1996/03/05 09:21:09 adam
60 * Bug fix: memory used by GRS records wasn't freed.
61 * Rewrote some of the error handling code - the connection is always
62 * closed before failback is called.
63 * If failback is defined the send APDU methods (init, search, ...) will
64 * return OK but invoke failback (as is the case if the write operation
66 * Bug fix: ref_count in assoc object could grow if fraction of PDU was
69 * Revision 1.82 1996/02/29 15:30:21 adam
70 * Export of IrTcl functionality to extensions.
72 * Revision 1.81 1996/02/26 18:38:32 adam
73 * Work on export of set methods.
75 * Revision 1.80 1996/02/23 17:31:39 adam
76 * More functions made available to the wais tcl extension.
78 * Revision 1.79 1996/02/23 13:41:38 adam
79 * Work on public access to simple ir class system.
81 * Revision 1.78 1996/02/21 10:16:08 adam
82 * Simplified select handling. Only one function ir_tcl_select_set has
83 * to be externally defined.
85 * Revision 1.77 1996/02/20 17:52:58 adam
86 * Uses the YAZ oid system to name record syntax object identifiers.
88 * Revision 1.76 1996/02/20 16:09:51 adam
89 * Bug fix: didn't set element set names stamp correctly on result
90 * set records when element set names were set to the empty string.
92 * Revision 1.75 1996/02/19 15:41:53 adam
93 * Better log messages.
94 * Minor improvement of connect method.
96 * Revision 1.74 1996/02/05 17:58:03 adam
97 * Ported ir-tcl to use the beta releases of tcl7.5/tk4.1.
99 * Revision 1.73 1996/01/29 11:35:19 adam
100 * Bug fix: cs_type member renamed to comstackType to avoid conflict with
101 * cs_type macro defined by YAZ.
103 * Revision 1.72 1996/01/19 17:45:34 quinn
104 * Added debugging output
106 * Revision 1.71 1996/01/19 16:22:38 adam
107 * New method: apduDump - returns information about last incoming APDU.
109 * Revision 1.70 1996/01/10 09:18:34 adam
110 * PDU specific callbacks implemented: initRespnse, searchResponse,
111 * presentResponse and scanResponse.
112 * Bug fix in the command line shell (tclmain.c) - discovered on OSF/1.
114 * Revision 1.69 1996/01/04 16:12:12 adam
115 * Setting PDUType renamed to eventType.
117 * Revision 1.68 1996/01/04 11:05:22 adam
118 * New setting: PDUType - returns type of last PDU returned from the target.
119 * Fixed a bug in configure/Makefile.
121 * Revision 1.67 1996/01/03 09:00:51 adam
122 * Updated to use new version of Yaz (names changed to avoid C++ conflict).
124 * Revision 1.66 1995/11/28 17:26:39 adam
125 * Removed Carriage return from ir-tcl.c!
126 * Removed misc. debug logs.
128 * Revision 1.65 1995/11/28 13:53:00 quinn
131 * Revision 1.64 1995/11/13 15:39:18 adam
132 * Bug fix: {small,medium}SetElementSetNames weren't set correctly.
133 * Bug fix: idAuthentication weren't set correctly.
135 * Revision 1.63 1995/11/13 09:55:39 adam
136 * Multiple records at a position in a result-set with differnt
139 * Revision 1.62 1995/10/18 17:20:33 adam
140 * Work on target setup in client.tcl.
142 * Revision 1.61 1995/10/18 16:42:42 adam
143 * New settings: smallSetElementSetNames and mediumSetElementSetNames.
145 * Revision 1.60 1995/10/18 15:43:31 adam
146 * In search: mediumSetElementSetNames and smallSetElementSetNames are
147 * set to elementSetNames.
149 * Revision 1.59 1995/10/17 12:18:58 adam
150 * Bug fix: when target connection closed, the connection was not
151 * properly reestablished.
153 * Revision 1.58 1995/10/16 17:00:55 adam
154 * New setting: elementSetNames.
155 * Various client improvements. Medium presentation format looks better.
157 * Revision 1.57 1995/09/21 13:11:51 adam
158 * Support of dynamic loading.
159 * Test script uses load command if necessary.
161 * Revision 1.56 1995/08/29 15:30:14 adam
162 * Work on GRS records.
164 * Revision 1.55 1995/08/28 09:43:25 adam
165 * Minor changes. configure only searches for yaz beta 3 and versions after
168 * Revision 1.54 1995/08/24 12:25:16 adam
169 * Modified to work with yaz 1.0b3.
171 * Revision 1.53 1995/08/04 12:49:26 adam
172 * Bug fix: reading uninitialized variable p.
174 * Revision 1.52 1995/08/04 11:32:38 adam
175 * More work on output queue. Memory related routines moved
178 * Revision 1.51 1995/08/03 13:22:54 adam
181 * Revision 1.50 1995/07/20 08:09:49 adam
182 * client.tcl: Targets removed from hotTargets list when targets
183 * are removed/modified.
184 * ir-tcl.c: More work on triggerResourceControl.
186 * Revision 1.49 1995/06/30 12:39:21 adam
187 * Bug fix: loadFile didn't set record type.
188 * The MARC routines are a little less strict in the interpretation.
189 * Script display.tcl replaces the old marc.tcl.
190 * New interactive script: shell.tcl.
192 * Revision 1.48 1995/06/27 19:03:50 adam
193 * Bug fix in do_present in ir-tcl.c: p->set_child member weren't set.
194 * nextResultSetPosition used instead of setOffset.
196 * Revision 1.47 1995/06/25 10:25:04 adam
197 * Working on triggerResourceControl. Description of compile/install
198 * procedure moved to ir-tcl.sgml.
200 * Revision 1.46 1995/06/22 13:15:06 adam
201 * Feature: SUTRS. Setting getSutrs implemented.
202 * Work on display formats.
203 * Preferred record syntax can be set by the user.
205 * Revision 1.45 1995/06/20 08:07:30 adam
206 * New setting: failInfo.
207 * Working on better cancel mechanism.
209 * Revision 1.44 1995/06/19 17:01:20 adam
212 * Revision 1.43 1995/06/19 13:06:08 adam
213 * New define: IR_TCL_VERSION.
215 * Revision 1.42 1995/06/19 08:08:52 adam
216 * client.tcl: hotTargets now contain both database and target name.
217 * ir-tcl.c: setting protocol edited. Errors in callbacks are logged
218 * by logf(LOG_WARN, ...) calls.
220 * Revision 1.41 1995/06/16 12:28:16 adam
221 * Implemented preferredRecordSyntax.
222 * Minor changes in diagnostic handling.
223 * Record list deleted when connection closes.
225 * Revision 1.40 1995/06/14 13:37:18 adam
226 * Setting recordType implemented.
227 * Setting implementationVersion implemented.
228 * Settings implementationId / implementationName edited.
230 * Revision 1.39 1995/06/08 10:26:32 adam
231 * Bug fix in ir_strdup.
233 * Revision 1.38 1995/06/01 16:36:47 adam
234 * About buttons. Minor bug fixes.
236 * Revision 1.37 1995/06/01 07:31:20 adam
237 * Rename of many typedefs -> IrTcl_...
239 * Revision 1.36 1995/05/31 13:09:59 adam
240 * Client searches/presents may be interrupted.
241 * New moving book-logo.
243 * Revision 1.35 1995/05/31 08:36:33 adam
244 * Bug fix in client.tcl: didn't save options on clientrc.tcl.
245 * New method: referenceId. More work on scan.
247 * Revision 1.34 1995/05/29 10:33:42 adam
248 * README and rename of startup script.
250 * Revision 1.33 1995/05/29 09:15:11 quinn
251 * Changed CS_SR to PROTO_SR, etc.
253 * Revision 1.32 1995/05/29 08:44:16 adam
254 * Work on delete of objects.
256 * Revision 1.31 1995/05/26 11:44:10 adam
257 * Bugs fixed. More work on MARC utilities and queries. Test
258 * client is up-to-date again.
260 * Revision 1.30 1995/05/26 08:54:11 adam
261 * New MARC utilities. Uses prefix query.
263 * Revision 1.29 1995/05/24 14:10:22 adam
264 * Work on idAuthentication, protocolVersion and options.
266 * Revision 1.28 1995/05/23 15:34:48 adam
267 * Many new settings, userInformationField, smallSetUpperBound, etc.
268 * A number of settings are inherited when ir-set is executed.
269 * This version is incompatible with the graphical test client (client.tcl).
271 * Revision 1.27 1995/05/11 15:34:47 adam
272 * Scan request changed a bit. This version works with RLG.
274 * Revision 1.26 1995/04/18 16:11:51 adam
275 * First version of graphical Scan. Some work on query-by-form.
277 * Revision 1.25 1995/04/17 09:37:17 adam
278 * Further development of scan.
280 * Revision 1.24 1995/04/11 14:16:42 adam
281 * Further work on scan. Response works. Entries aren't saved yet.
283 * Revision 1.23 1995/04/10 10:50:27 adam
284 * Result-set name defaults to suffix of ir-set name.
285 * Started working on scan. Not finished at this point.
287 * Revision 1.22 1995/03/31 10:43:03 adam
288 * More robust when getting bad MARC records.
290 * Revision 1.21 1995/03/31 08:56:37 adam
291 * New button "Search".
293 * Revision 1.20 1995/03/29 16:07:09 adam
294 * Bug fix: Didn't use setName in present request.
296 * Revision 1.19 1995/03/28 12:45:23 adam
297 * New ir method failback: called on disconnect/protocol error.
298 * New ir set/get method: protocol: SR / Z3950.
299 * Simple popup and disconnect when failback is invoked.
301 * Revision 1.18 1995/03/21 15:50:12 adam
304 * Revision 1.17 1995/03/21 13:41:03 adam
305 * Comstack cs_create not used too often. Non-blocking connect.
307 * Revision 1.16 1995/03/21 08:26:06 adam
308 * New method, setName, to specify the result set name (other than Default).
309 * New method, responseStatus, which returns diagnostic info, if any, after
310 * present response / search response.
312 * Revision 1.15 1995/03/20 15:24:07 adam
313 * Diagnostic records saved on searchResponse.
315 * Revision 1.14 1995/03/20 08:53:22 adam
316 * Event loop in tclmain.c rewritten. New method searchStatus.
318 * Revision 1.13 1995/03/17 18:26:17 adam
319 * Non-blocking i/o used now. Database names popup as cascade items.
321 * Revision 1.12 1995/03/17 15:45:00 adam
322 * Improved target/database setup.
324 * Revision 1.11 1995/03/16 17:54:03 adam
325 * Minor changes really.
327 * Revision 1.10 1995/03/15 16:14:50 adam
328 * Blocking arg in cs_create changed.
330 * Revision 1.9 1995/03/15 13:59:24 adam
333 * Revision 1.8 1995/03/15 08:25:16 adam
334 * New method presentStatus to check for error on present. Misc. cleanup
335 * of IrTcl_RecordList manipulations. Full MARC record presentation in
338 * Revision 1.7 1995/03/14 17:32:29 adam
339 * Presentation of full Marc record in popup window.
341 * Revision 1.6 1995/03/12 19:31:55 adam
342 * Pattern matching implemented when retrieving MARC records. More
343 * diagnostic functions.
345 * Revision 1.5 1995/03/10 18:00:15 adam
346 * Actual presentation in line-by-line format. RPN query support.
348 * Revision 1.4 1995/03/09 16:15:08 adam
349 * First presentRequest attempts. Hot-target list.
367 #if defined(__WIN32__)
368 # define WIN32_LEAN_AND_MEAN
369 # include <windows.h>
370 # undef WIN32_LEAN_AND_MEAN
373 * VC++ has an alternate entry point called DllMain, so we need to rename
377 # if defined(_MSC_VER)
378 # define EXPORT(a,b) __declspec(dllexport) a b
379 # define DllEntryPoint DllMain
381 # if defined(__BORLANDC__)
382 # define EXPORT(a,b) a _export b
384 # define EXPORT(a,b) a b
388 # define EXPORT(a,b) a b
391 static char *wrongArgs = "wrong # args: should be \"";
393 static int ir_tcl_error_exec (Tcl_Interp *interp, int argc, char **argv)
396 Tcl_AppendResult (interp, " while executing ", NULL);
397 for (i = 0; i<argc; i++)
398 Tcl_AppendResult (interp, (i ? " " : "\""), argv[i], NULL);
399 Tcl_AppendResult (interp, "\"", NULL);
404 static void ir_deleteDiags (IrTcl_Diagnostic **dst_list, int *dst_num);
406 static void ir_select_notify (ClientData clientData, int r, int w, int e);
408 void ir_select_add (int fd, void *obj)
410 ir_tcl_select_set (ir_select_notify, fd, obj, 1, 0, 0);
413 void ir_select_add_write (int fd, void *obj)
415 ir_tcl_select_set (ir_select_notify, fd, obj, 1, 1, 0);
418 void ir_select_remove (int fd, void *obj)
420 ir_tcl_select_set (NULL, fd, obj, 0, 0, 0);
423 void ir_select_remove_write (int fd, void *obj)
425 ir_tcl_select_set (ir_select_notify, fd, obj, 1, 0, 0);
428 static void delete_IR_record (IrTcl_RecordList *rl)
432 case Z_NamePlusRecord_databaseRecord:
433 switch (rl->u.dbrec.type)
436 ir_tcl_grs_del (&rl->u.dbrec.u.grs1);
441 xfree (rl->u.dbrec.buf);
442 rl->u.dbrec.buf = NULL;
444 case Z_NamePlusRecord_surrogateDiagnostic:
445 ir_deleteDiags (&rl->u.surrogateDiagnostics.list,
446 &rl->u.surrogateDiagnostics.num);
449 xfree (rl->elements);
452 static IrTcl_RecordList *new_IR_record (IrTcl_SetObj *setobj,
454 const char *elements)
456 IrTcl_RecordList *rl;
458 if (elements && !*elements)
460 for (rl = setobj->record_list; rl; rl = rl->next)
462 if (no == rl->no && (!rl->elements || !elements ||
463 !strcmp(elements, rl->elements)))
465 delete_IR_record (rl);
471 rl = ir_tcl_malloc (sizeof(*rl));
472 rl->next = setobj->record_list;
474 setobj->record_list = rl;
477 ir_tcl_strdup (NULL, &rl->elements, elements);
484 int ir_tcl_eval (Tcl_Interp *interp, const char *command)
486 char *tmp = ir_tcl_malloc (strlen(command)+1);
489 logf (LOG_DEBUG, "Invoking %.23s ...", command);
490 strcpy (tmp, command);
491 r = Tcl_Eval (interp, tmp);
494 logf (LOG_WARN, "Tcl error in line %d: %s", interp->errorLine,
497 Tcl_FreeResult (interp);
503 * IrTcl_getRecordSyntaxStr: Return record syntax name of object id
505 static char *IrTcl_getRecordSyntaxStr (enum oid_value value)
508 struct oident ent, *entp;
510 ent.proto = PROTO_Z3950;
511 ent.oclass = CLASS_RECSYN;
514 o = oid_getoidbyent (&ent);
515 entp = oid_getentbyoid (o);
523 * IrTcl_getRecordSyntaxVal: Return record syntax value of string
525 static enum oid_value IrTcl_getRecordSyntaxVal (const char *name)
527 return oid_getvalbyname (name);
530 static IrTcl_RecordList *find_IR_record (IrTcl_SetObj *setobj, int no)
532 IrTcl_RecordList *rl;
534 for (rl = setobj->record_list; rl; rl = rl->next)
536 (!setobj->recordElements || !rl->elements ||
537 !strcmp (setobj->recordElements, rl->elements)))
542 static void delete_IR_records (IrTcl_SetObj *setobj)
544 IrTcl_RecordList *rl, *rl1;
546 for (rl = setobj->record_list; rl; rl = rl1)
548 delete_IR_record (rl);
552 setobj->record_list = NULL;
556 * ir_tcl_get_set_int: Set/get integer value
558 int ir_tcl_get_set_int (int *val, Tcl_Interp *interp, int argc, char **argv)
564 if (Tcl_GetInt (interp, argv[2], val)==TCL_ERROR)
567 sprintf (buf, "%d", *val);
568 Tcl_AppendResult (interp, buf, NULL);
574 * ir_tcl_method_error
576 int ir_tcl_method_error (Tcl_Interp *interp, int argc, char **argv,
579 IrTcl_Methods *tab_i = tab;
582 Tcl_AppendResult (interp, "bad method: \"", *argv, " ", argv[1],
583 "\"\nmethod should be of:", NULL);
584 for (tab_i = tab; tab_i->tab; tab_i++)
585 for (t = tab_i->tab; t->name; t++)
586 Tcl_AppendResult (interp, " ", t->name, NULL);
591 * ir_tcl_method: Search for method in table and invoke method handler
593 int ir_tcl_method (Tcl_Interp *interp, int argc, char **argv,
594 IrTcl_Methods *tab, int *ret)
596 IrTcl_Methods *tab_i = tab;
599 for (tab_i = tab; tab_i->tab; tab_i++)
600 for (t = tab_i->tab; t->name; t++)
603 if ((*t->method)(tab_i->obj, interp, argc, argv) == TCL_ERROR)
607 if (!strcmp (t->name, argv[1]))
609 *ret = (*t->method)(tab_i->obj, interp, argc, argv);
620 * ir_tcl_named_bits: get/set named bits
622 int ir_tcl_named_bits (struct ir_named_entry *tab, Odr_bitmask *ob,
623 Tcl_Interp *interp, int argc, char **argv)
625 struct ir_named_entry *ti;
630 for (no = 0; no < argc; no++)
632 for (ti = tab; ti->name; ti++)
633 if (!strcmp (argv[no], ti->name))
635 ODR_MASK_SET (ob, ti->pos);
640 Tcl_AppendResult (interp, "bad bit mask ", argv[no], NULL);
641 return ir_tcl_error_exec (interp, argc, argv);
646 for (ti = tab; ti->name; ti++)
647 if (ODR_MASK_GET (ob, ti->pos))
648 Tcl_AppendElement (interp, ti->name);
652 static void set_referenceId (ODR o, Z_ReferenceId **dst, const char *src)
658 *dst = odr_malloc (o, sizeof(**dst));
659 (*dst)->size = (*dst)->len = strlen(src);
660 (*dst)->buf = odr_malloc (o, (*dst)->len);
661 memcpy ((*dst)->buf, src, (*dst)->len);
665 static void get_referenceId (char **dst, Z_ReferenceId *src)
673 *dst = ir_tcl_malloc (src->len+1);
674 memcpy (*dst, src->buf, src->len);
675 (*dst)[src->len] = '\0';
678 /* ------------------------------------------------------- */
681 * do_init_request: init method on IR object
683 static int do_init_request (void *obj, Tcl_Interp *interp,
684 int argc, char **argv)
692 logf (LOG_DEBUG, "init %s", *argv);
695 Tcl_AppendResult (interp, "not connected", NULL);
696 return ir_tcl_error_exec (interp, argc, argv);
698 apdu = zget_APDU (p->odr_out, Z_APDU_initRequest);
699 req = apdu->u.initRequest;
701 set_referenceId (p->odr_out, &req->referenceId, p->set_inher.referenceId);
702 req->options = &p->options;
703 req->protocolVersion = &p->protocolVersion;
704 req->preferredMessageSize = &p->preferredMessageSize;
705 req->maximumRecordSize = &p->maximumRecordSize;
707 if (p->idAuthenticationGroupId)
709 Z_IdPass *pass = odr_malloc (p->odr_out, sizeof(*pass));
710 Z_IdAuthentication *auth = odr_malloc (p->odr_out, sizeof(*auth));
712 logf (LOG_DEBUG, "using pass authentication");
714 auth->which = Z_IdAuthentication_idPass;
715 auth->u.idPass = pass;
716 if (p->idAuthenticationGroupId && *p->idAuthenticationGroupId)
717 pass->groupId = p->idAuthenticationGroupId;
719 pass->groupId = NULL;
720 if (p->idAuthenticationUserId && *p->idAuthenticationUserId)
721 pass->userId = p->idAuthenticationUserId;
724 if (p->idAuthenticationPassword && *p->idAuthenticationPassword)
725 pass->password = p->idAuthenticationPassword;
727 pass->password = NULL;
728 req->idAuthentication = auth;
730 else if (!p->idAuthenticationOpen || !*p->idAuthenticationOpen)
731 req->idAuthentication = NULL;
734 Z_IdAuthentication *auth = odr_malloc (p->odr_out, sizeof(*auth));
736 logf (LOG_DEBUG, "using open authentication");
737 auth->which = Z_IdAuthentication_open;
738 auth->u.open = p->idAuthenticationOpen;
739 req->idAuthentication = auth;
741 req->implementationId = p->implementationId;
742 req->implementationName = p->implementationName;
743 req->implementationVersion = p->implementationVersion;
744 req->userInformationField = 0;
746 return ir_tcl_send_APDU (interp, p, apdu, "init", *argv);
750 * do_protocolVersion: Set protocol Version
752 static int do_protocolVersion (void *obj, Tcl_Interp *interp,
753 int argc, char **argv)
761 ODR_MASK_ZERO (&p->protocolVersion);
762 ODR_MASK_SET (&p->protocolVersion, 0);
763 ODR_MASK_SET (&p->protocolVersion, 1);
768 if (Tcl_GetInt (interp, argv[2], &version)==TCL_ERROR)
770 ODR_MASK_ZERO (&p->protocolVersion);
771 for (i = 0; i<version; i++)
772 ODR_MASK_SET (&p->protocolVersion, i);
774 for (i = 4; --i >= 0; )
775 if (ODR_MASK_GET (&p->protocolVersion, i))
777 sprintf (buf, "%d", i+1);
778 interp->result = buf;
783 * do_options: Set options
785 static int do_options (void *obj, Tcl_Interp *interp,
786 int argc, char **argv)
788 static struct ir_named_entry options_tab[] = {
792 { "resourceReport", 3 },
793 { "triggerResourceCtrl", 4},
794 { "resourceCtrl", 5},
798 { "extendedServices", 10},
799 { "level-1Segmentation", 11},
800 { "level-2Segmentation", 12},
801 { "concurrentOperations", 13},
802 { "namedResultSets", 14},
809 ODR_MASK_ZERO (&p->options);
810 ODR_MASK_SET (&p->options, 0);
811 ODR_MASK_SET (&p->options, 1);
812 ODR_MASK_SET (&p->options, 4);
813 ODR_MASK_SET (&p->options, 7);
814 ODR_MASK_SET (&p->options, 14);
817 return ir_tcl_named_bits (options_tab, &p->options, interp, argc-2, argv+2);
821 * do_apduInfo: Get APDU information
823 static int do_apduInfo (void *obj, Tcl_Interp *interp, int argc, char **argv)
831 sprintf (buf, "%d", p->apduLen);
832 Tcl_AppendElement (interp, buf);
833 sprintf (buf, "%d", p->apduOffset);
834 Tcl_AppendElement (interp, buf);
837 Tcl_AppendElement (interp, "");
840 apduf = fopen ("apdu.tmp", "w");
843 Tcl_AppendElement (interp, "");
846 odr_dumpBER (apduf, p->buf_in, p->apduLen);
848 if (!(apduf = fopen ("apdu.tmp", "r")))
849 Tcl_AppendElement (interp, "");
854 Tcl_AppendResult (interp, " {", NULL);
855 while ((c = getc (apduf)) != EOF)
859 Tcl_AppendResult (interp, buf, NULL);
862 Tcl_AppendResult (interp, "}", NULL);
869 * do_failInfo: Get fail information
871 static int do_failInfo (void *obj, Tcl_Interp *interp, int argc, char **argv)
881 sprintf (buf, "%d", p->failInfo);
887 case IR_TCL_FAIL_CONNECT:
888 cp = "connect failed";
890 case IR_TCL_FAIL_READ:
891 cp = "connection closed";
893 case IR_TCL_FAIL_WRITE:
894 cp = "connection closed";
896 case IR_TCL_FAIL_IN_APDU:
897 cp = "failed to decode incoming APDU";
899 case IR_TCL_FAIL_UNKNOWN_APDU:
905 Tcl_AppendElement (interp, buf);
906 Tcl_AppendElement (interp, cp);
911 * do_preferredMessageSize: Set/get preferred message size
913 static int do_preferredMessageSize (void *obj, Tcl_Interp *interp,
914 int argc, char **argv)
920 p->preferredMessageSize = 30000;
923 return ir_tcl_get_set_int (&p->preferredMessageSize, interp, argc, argv);
927 * do_maximumRecordSize: Set/get maximum record size
929 static int do_maximumRecordSize (void *obj, Tcl_Interp *interp,
930 int argc, char **argv)
936 p->maximumRecordSize = 30000;
939 return ir_tcl_get_set_int (&p->maximumRecordSize, interp, argc, argv);
943 * do_initResult: Get init result
945 static int do_initResult (void *obj, Tcl_Interp *interp,
946 int argc, char **argv)
952 return ir_tcl_get_set_int (&p->initResult, interp, argc, argv);
957 * do_implementationName: Set/get Implementation Name.
959 static int do_implementationName (void *obj, Tcl_Interp *interp,
960 int argc, char **argv)
965 return ir_tcl_strdup (interp, &p->implementationName,
966 "Index Data/IrTcl on YAZ");
968 return ir_tcl_strdel (interp, &p->implementationName);
971 xfree (p->implementationName);
972 if (ir_tcl_strdup (interp, &p->implementationName, argv[2])
976 Tcl_AppendResult (interp, p->implementationName, (char*) NULL);
981 * do_implementationId: Get Implementation Id.
983 static int do_implementationId (void *obj, Tcl_Interp *interp,
984 int argc, char **argv)
989 return ir_tcl_strdup (interp, &p->implementationId, "YAZ (id=81)");
991 return ir_tcl_strdel (interp, &p->implementationId);
992 Tcl_AppendResult (interp, p->implementationId, (char*) NULL);
997 * do_implementationVersion: get Implementation Version.
999 static int do_implementationVersion (void *obj, Tcl_Interp *interp,
1000 int argc, char **argv)
1005 return ir_tcl_strdup (interp, &p->implementationVersion,
1007 #ifdef IR_TCL_VERSION
1008 " / Irtcl: " IR_TCL_VERSION
1011 else if (argc == -1)
1012 return ir_tcl_strdel (interp, &p->implementationVersion);
1013 Tcl_AppendResult (interp, p->implementationVersion, (char*) NULL);
1018 * do_targetImplementationName: Get Implementation Name of target.
1020 static int do_targetImplementationName (void *obj, Tcl_Interp *interp,
1021 int argc, char **argv)
1027 p->targetImplementationName = NULL;
1030 else if (argc == -1)
1031 return ir_tcl_strdel (interp, &p->targetImplementationName);
1032 Tcl_AppendResult (interp, p->targetImplementationName, (char*) NULL);
1037 * do_targetImplementationId: Get Implementation Id of target
1039 static int do_targetImplementationId (void *obj, Tcl_Interp *interp,
1040 int argc, char **argv)
1046 p->targetImplementationId = NULL;
1049 else if (argc == -1)
1050 return ir_tcl_strdel (interp, &p->targetImplementationId);
1051 Tcl_AppendResult (interp, p->targetImplementationId, (char*) NULL);
1056 * do_targetImplementationVersion: Get Implementation Version of target
1058 static int do_targetImplementationVersion (void *obj, Tcl_Interp *interp,
1059 int argc, char **argv)
1065 p->targetImplementationVersion = NULL;
1068 else if (argc == -1)
1069 return ir_tcl_strdel (interp, &p->targetImplementationVersion);
1070 Tcl_AppendResult (interp, p->targetImplementationVersion, (char*) NULL);
1075 * do_idAuthentication: Set/get id Authentication
1077 static int do_idAuthentication (void *obj, Tcl_Interp *interp,
1078 int argc, char **argv)
1082 if (argc >= 3 || argc == -1)
1084 xfree (p->idAuthenticationOpen);
1085 xfree (p->idAuthenticationGroupId);
1086 xfree (p->idAuthenticationUserId);
1087 xfree (p->idAuthenticationPassword);
1089 if (argc >= 3 || argc <= 0)
1091 p->idAuthenticationOpen = NULL;
1092 p->idAuthenticationGroupId = NULL;
1093 p->idAuthenticationUserId = NULL;
1094 p->idAuthenticationPassword = NULL;
1103 ir_tcl_strdup (interp, &p->idAuthenticationOpen, argv[2])
1110 ir_tcl_strdup (interp, &p->idAuthenticationGroupId, argv[2])
1114 ir_tcl_strdup (interp, &p->idAuthenticationUserId, argv[3])
1118 ir_tcl_strdup (interp, &p->idAuthenticationPassword, argv[4])
1123 if (p->idAuthenticationOpen)
1124 Tcl_AppendElement (interp, p->idAuthenticationOpen);
1125 else if (p->idAuthenticationGroupId)
1127 Tcl_AppendElement (interp, p->idAuthenticationGroupId);
1128 Tcl_AppendElement (interp, p->idAuthenticationUserId);
1129 Tcl_AppendElement (interp, p->idAuthenticationPassword);
1135 * do_connect: connect method on IR object
1137 static int do_connect (void *obj, Tcl_Interp *interp,
1138 int argc, char **argv)
1148 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
1149 " ?hostname?\"", NULL);
1154 Tcl_AppendResult (interp, p->hostname, NULL);
1158 logf (LOG_DEBUG, "connect %s %s", *argv, argv[2]);
1161 Tcl_AppendResult (interp, "already connected", NULL);
1162 return ir_tcl_error_exec (interp, argc, argv);
1164 if (!strcmp (p->comstackType, "tcpip"))
1166 p->cs_link = cs_create (tcpip_type, CS_BLOCK, p->protocol_type);
1167 addr = tcpip_strtoaddr (argv[2]);
1170 Tcl_AppendResult (interp, "tcpip_strtoaddr fail", NULL);
1171 return ir_tcl_error_exec (interp, argc, argv);
1173 logf (LOG_DEBUG, "tcp/ip connect %s", argv[2]);
1175 else if (!strcmp (p->comstackType, "mosi"))
1178 p->cs_link = cs_create (mosi_type, CS_BLOCK, p->protocol_type);
1179 addr = mosi_strtoaddr (argv[2]);
1182 Tcl_AppendResult (interp, "mosi_strtoaddr fail", NULL);
1183 return ir_tcl_error_exec (interp, argc, argv);
1185 logf (LOG_DEBUG, "mosi connect %s", argv[2]);
1187 Tcl_AppendResult (interp, "mosi not supported", NULL);
1188 return ir_tcl_error_exec (interp, argc, argv);
1193 Tcl_AppendResult (interp, "bad comstack type ",
1194 p->comstackType, NULL);
1195 return ir_tcl_error_exec (interp, argc, argv);
1197 if (ir_tcl_strdup (interp, &p->hostname, argv[2]) == TCL_ERROR)
1199 p->eventType = "connect";
1200 if ((r=cs_connect (p->cs_link, addr)) < 0)
1202 ir_tcl_disconnect (p);
1203 Tcl_AppendResult (interp, "conncet fail", NULL);
1204 return ir_tcl_error_exec (interp, argc, argv);
1206 ir_select_add (cs_fileno (p->cs_link), p);
1209 logf (LOG_DEBUG, "connect pending fd=%d", cs_fileno(p->cs_link));
1210 ir_select_add_write (cs_fileno (p->cs_link), p);
1211 p->state = IR_TCL_R_Connecting;
1215 logf (LOG_DEBUG, "connect ok fd=%d", cs_fileno(p->cs_link));
1216 p->state = IR_TCL_R_Idle;
1218 ir_tcl_eval (p->interp, p->callback);
1225 * ir_tcl_disconnect: close connection
1227 void ir_tcl_disconnect (IrTcl_Obj *p)
1231 logf(LOG_DEBUG, "Closing connection to %s", p->hostname);
1232 xfree (p->hostname);
1234 assert (p->cs_link);
1235 ir_select_remove (cs_fileno (p->cs_link), p);
1237 odr_reset (p->odr_in);
1239 cs_close (p->cs_link);
1242 ODR_MASK_ZERO (&p->options);
1243 ODR_MASK_SET (&p->options, 0);
1244 ODR_MASK_SET (&p->options, 1);
1245 ODR_MASK_SET (&p->options, 4);
1246 ODR_MASK_SET (&p->options, 7);
1247 ODR_MASK_SET (&p->options, 14);
1249 ODR_MASK_ZERO (&p->protocolVersion);
1250 ODR_MASK_SET (&p->protocolVersion, 0);
1251 ODR_MASK_SET (&p->protocolVersion, 1);
1254 assert (!p->cs_link);
1258 * do_disconnect: disconnect method on IR object
1260 static int do_disconnect (void *obj, Tcl_Interp *interp,
1261 int argc, char **argv)
1267 p->state = IR_TCL_R_Idle;
1268 p->eventType = NULL;
1273 ir_tcl_disconnect (p);
1278 * do_comstack: Set/get comstack method on IR object
1280 static int do_comstack (void *o, Tcl_Interp *interp,
1281 int argc, char **argv)
1286 return ir_tcl_strdup (interp, &obj->comstackType, "tcpip");
1287 else if (argc == -1)
1288 return ir_tcl_strdel (interp, &obj->comstackType);
1291 xfree (obj->comstackType);
1292 if (ir_tcl_strdup (interp, &obj->comstackType, argv[2]) == TCL_ERROR)
1295 Tcl_AppendElement (interp, obj->comstackType);
1300 * do_logLevel: Set log level
1302 static int do_logLevel (void *o, Tcl_Interp *interp,
1303 int argc, char **argv)
1308 log_init (log_mask_str (argv[2]), "", NULL);
1310 log_init (log_mask_str (argv[2]), argv[3], NULL);
1312 log_init (log_mask_str (argv[2]), argv[3], argv[4]);
1318 * do_eventType: Return type of last event
1320 static int do_eventType (void *obj, Tcl_Interp *interp,
1321 int argc, char **argv)
1327 p->eventType = NULL;
1330 Tcl_AppendElement (interp, p->eventType ? p->eventType : "");
1336 * do_callback: add callback
1338 static int do_callback (void *obj, Tcl_Interp *interp,
1339 int argc, char **argv)
1348 else if (argc == -1)
1349 return ir_tcl_strdel (interp, &p->callback);
1352 xfree (p->callback);
1355 if (ir_tcl_strdup (interp, &p->callback, argv[2]) == TCL_ERROR)
1365 * do_failback: add error handle callback
1367 static int do_failback (void *obj, Tcl_Interp *interp,
1368 int argc, char **argv)
1377 else if (argc == -1)
1378 return ir_tcl_strdel (interp, &p->failback);
1381 xfree (p->failback);
1384 if (ir_tcl_strdup (interp, &p->failback, argv[2]) == TCL_ERROR)
1394 * do_initResponse: add init response handler
1396 static int do_initResponse (void *obj, Tcl_Interp *interp,
1397 int argc, char **argv)
1403 p->initResponse = NULL;
1406 else if (argc == -1)
1407 return ir_tcl_strdel (interp, &p->initResponse);
1410 xfree (p->initResponse);
1413 if (ir_tcl_strdup (interp, &p->initResponse, argv[2]) == TCL_ERROR)
1417 p->initResponse = NULL;
1422 * do_protocol: Set/get protocol method on IR object
1424 static int do_protocol (void *o, Tcl_Interp *interp, int argc, char **argv)
1430 p->protocol_type = PROTO_Z3950;
1435 if (!strcmp (argv[2], "Z39"))
1436 p->protocol_type = PROTO_Z3950;
1437 else if (!strcmp (argv[2], "SR"))
1438 p->protocol_type = PROTO_SR;
1441 Tcl_AppendResult (interp, "bad protocol ", argv[2], NULL);
1442 return ir_tcl_error_exec (interp, argc, argv);
1446 switch (p->protocol_type)
1449 Tcl_AppendElement (interp, "Z39");
1452 Tcl_AppendElement (interp, "SR");
1459 * do_triggerResourceControl:
1461 static int do_triggerResourceControl (void *obj, Tcl_Interp *interp,
1462 int argc, char **argv)
1466 Z_TriggerResourceControlRequest *req;
1467 bool_t is_false = 0;
1473 Tcl_AppendResult (interp, "not connected", NULL);
1474 return ir_tcl_error_exec (interp, argc, argv);
1476 apdu = zget_APDU (p->odr_out, Z_APDU_triggerResourceControlRequest);
1477 req = apdu->u.triggerResourceControlRequest;
1478 *req->requestedAction = Z_TriggerResourceCtrl_cancel;
1479 req->resultSetWanted = &is_false;
1481 return ir_tcl_send_APDU (interp, p, apdu, "triggerResourceControl",
1486 * do_databaseNames: specify database names
1488 static int do_databaseNames (void *obj, Tcl_Interp *interp,
1489 int argc, char **argv)
1492 IrTcl_SetCObj *p = obj;
1496 for (i=0; i<p->num_databaseNames; i++)
1497 xfree (p->databaseNames[i]);
1498 xfree (p->databaseNames);
1502 p->num_databaseNames = 0;
1503 p->databaseNames = NULL;
1508 for (i=0; i<p->num_databaseNames; i++)
1509 Tcl_AppendElement (interp, p->databaseNames[i]);
1512 if (p->databaseNames)
1514 for (i=0; i<p->num_databaseNames; i++)
1515 xfree (p->databaseNames[i]);
1516 xfree (p->databaseNames);
1518 p->num_databaseNames = argc - 2;
1520 ir_tcl_malloc (sizeof(*p->databaseNames) * (1+p->num_databaseNames));
1521 for (i=0; i<p->num_databaseNames; i++)
1523 if (ir_tcl_strdup (interp, &p->databaseNames[i], argv[2+i])
1527 p->databaseNames[i] = NULL;
1532 * do_replaceIndicator: Set/get replace Set indicator
1534 static int do_replaceIndicator (void *obj, Tcl_Interp *interp,
1535 int argc, char **argv)
1537 IrTcl_SetCObj *p = obj;
1541 p->replaceIndicator = 1;
1544 return ir_tcl_get_set_int (&p->replaceIndicator, interp, argc, argv);
1548 * do_queryType: Set/Get query method
1550 static int do_queryType (void *obj, Tcl_Interp *interp,
1551 int argc, char **argv)
1553 IrTcl_SetCObj *p = obj;
1556 return ir_tcl_strdup (interp, &p->queryType, "rpn");
1557 else if (argc == -1)
1558 return ir_tcl_strdel (interp, &p->queryType);
1561 xfree (p->queryType);
1562 if (ir_tcl_strdup (interp, &p->queryType, argv[2]) == TCL_ERROR)
1565 Tcl_AppendResult (interp, p->queryType, NULL);
1570 * do_userInformationField: Get User information field
1572 static int do_userInformationField (void *obj, Tcl_Interp *interp,
1573 int argc, char **argv)
1579 p->userInformationField = NULL;
1582 else if (argc == -1)
1583 return ir_tcl_strdel (interp, &p->userInformationField);
1584 Tcl_AppendResult (interp, p->userInformationField, NULL);
1589 * do_smallSetUpperBound: Set/get small set upper bound
1591 static int do_smallSetUpperBound (void *o, Tcl_Interp *interp,
1592 int argc, char **argv)
1594 IrTcl_SetCObj *p = o;
1598 p->smallSetUpperBound = 0;
1601 return ir_tcl_get_set_int (&p->smallSetUpperBound, interp, argc, argv);
1605 * do_largeSetLowerBound: Set/get large set lower bound
1607 static int do_largeSetLowerBound (void *o, Tcl_Interp *interp,
1608 int argc, char **argv)
1610 IrTcl_SetCObj *p = o;
1614 p->largeSetLowerBound = 2;
1617 return ir_tcl_get_set_int (&p->largeSetLowerBound, interp, argc, argv);
1621 * do_mediumSetPresentNumber: Set/get large set lower bound
1623 static int do_mediumSetPresentNumber (void *o, Tcl_Interp *interp,
1624 int argc, char **argv)
1626 IrTcl_SetCObj *p = o;
1630 p->mediumSetPresentNumber = 0;
1633 return ir_tcl_get_set_int (&p->mediumSetPresentNumber, interp, argc, argv);
1637 * do_referenceId: Set/Get referenceId
1639 static int do_referenceId (void *obj, Tcl_Interp *interp,
1640 int argc, char **argv)
1642 IrTcl_SetCObj *p = obj;
1646 p->referenceId = NULL;
1649 else if (argc == -1)
1650 return ir_tcl_strdel (interp, &p->referenceId);
1653 xfree (p->referenceId);
1654 if (ir_tcl_strdup (interp, &p->referenceId, argv[2]) == TCL_ERROR)
1657 Tcl_AppendResult (interp, p->referenceId, NULL);
1662 * do_preferredRecordSyntax: Set/get preferred record syntax
1664 static int do_preferredRecordSyntax (void *obj, Tcl_Interp *interp,
1665 int argc, char **argv)
1667 IrTcl_SetCObj *p = obj;
1671 p->preferredRecordSyntax = NULL;
1674 else if (argc == -1)
1676 xfree (p->preferredRecordSyntax);
1677 p->preferredRecordSyntax = NULL;
1682 xfree (p->preferredRecordSyntax);
1683 p->preferredRecordSyntax = NULL;
1684 if (argv[2][0] && (p->preferredRecordSyntax =
1685 ir_tcl_malloc (sizeof(*p->preferredRecordSyntax))))
1686 *p->preferredRecordSyntax = IrTcl_getRecordSyntaxVal (argv[2]);
1691 (interp,!p->preferredRecordSyntax ? "" :
1692 IrTcl_getRecordSyntaxStr(*p->preferredRecordSyntax));
1699 * do_elementSetNames: Set/Get element Set Names
1701 static int do_elementSetNames (void *obj, Tcl_Interp *interp,
1702 int argc, char **argv)
1704 IrTcl_SetCObj *p = obj;
1708 p->elementSetNames = NULL;
1711 else if (argc == -1)
1712 return ir_tcl_strdel (interp, &p->elementSetNames);
1715 xfree (p->elementSetNames);
1716 if (ir_tcl_strdup (interp, &p->elementSetNames, argv[2]) == TCL_ERROR)
1719 Tcl_AppendResult (interp, p->elementSetNames, NULL);
1724 * do_smallSetElementSetNames: Set/Get small Set Element Set Names
1726 static int do_smallSetElementSetNames (void *obj, Tcl_Interp *interp,
1727 int argc, char **argv)
1729 IrTcl_SetCObj *p = obj;
1733 p->smallSetElementSetNames = NULL;
1736 else if (argc == -1)
1737 return ir_tcl_strdel (interp, &p->smallSetElementSetNames);
1740 xfree (p->smallSetElementSetNames);
1741 if (ir_tcl_strdup (interp, &p->smallSetElementSetNames,
1742 argv[2]) == TCL_ERROR)
1745 Tcl_AppendResult (interp, p->smallSetElementSetNames, NULL);
1750 * do_mediumSetElementSetNames: Set/Get medium Set Element Set Names
1752 static int do_mediumSetElementSetNames (void *obj, Tcl_Interp *interp,
1753 int argc, char **argv)
1755 IrTcl_SetCObj *p = obj;
1759 p->mediumSetElementSetNames = NULL;
1762 else if (argc == -1)
1763 return ir_tcl_strdel (interp, &p->mediumSetElementSetNames);
1766 xfree (p->mediumSetElementSetNames);
1767 if (ir_tcl_strdup (interp, &p->mediumSetElementSetNames,
1768 argv[2]) == TCL_ERROR)
1771 Tcl_AppendResult (interp, p->mediumSetElementSetNames, NULL);
1775 static IrTcl_Method ir_method_tab[] = {
1776 { "comstack", do_comstack, NULL },
1777 { "protocol", do_protocol, NULL },
1778 { "failback", do_failback, NULL },
1779 { "failInfo", do_failInfo, NULL },
1780 { "apduInfo", do_apduInfo, NULL },
1781 { "logLevel", do_logLevel, NULL },
1783 { "eventType", do_eventType, NULL },
1784 { "connect", do_connect, NULL },
1785 { "protocolVersion", do_protocolVersion, NULL },
1786 { "preferredMessageSize", do_preferredMessageSize, NULL },
1787 { "maximumRecordSize", do_maximumRecordSize, NULL },
1788 { "implementationName", do_implementationName, NULL },
1789 { "implementationId", do_implementationId, NULL },
1790 { "implementationVersion", do_implementationVersion, NULL },
1791 { "targetImplementationName", do_targetImplementationName, NULL },
1792 { "targetImplementationId", do_targetImplementationId, NULL },
1793 { "targetImplementationVersion", do_targetImplementationVersion, NULL},
1794 { "userInformationField", do_userInformationField, NULL},
1795 { "idAuthentication", do_idAuthentication, NULL},
1796 { "options", do_options, NULL},
1797 { "init", do_init_request, NULL},
1798 { "initResult", do_initResult, NULL},
1799 { "disconnect", do_disconnect, NULL},
1800 { "callback", do_callback, NULL},
1801 { "initResponse", do_initResponse, NULL},
1802 { "triggerResourceControl", do_triggerResourceControl, NULL},
1803 { "initResponse", do_initResponse, NULL},
1807 static IrTcl_Method ir_set_c_method_tab[] = {
1808 { "databaseNames", do_databaseNames, NULL},
1809 { "replaceIndicator", do_replaceIndicator, NULL},
1810 { "queryType", do_queryType, NULL},
1811 { "preferredRecordSyntax", do_preferredRecordSyntax, NULL},
1812 { "smallSetUpperBound", do_smallSetUpperBound, NULL},
1813 { "largeSetLowerBound", do_largeSetLowerBound, NULL},
1814 { "mediumSetPresentNumber", do_mediumSetPresentNumber, NULL},
1815 { "referenceId", do_referenceId, NULL},
1816 { "elementSetNames", do_elementSetNames, NULL},
1817 { "smallSetElementSetNames", do_smallSetElementSetNames, NULL},
1818 { "mediumSetElementSetNames", do_mediumSetElementSetNames, NULL},
1823 * ir_obj_method: IR Object methods
1825 static int ir_obj_method (ClientData clientData, Tcl_Interp *interp,
1826 int argc, char **argv)
1828 IrTcl_Methods tab[3];
1829 IrTcl_Obj *p = clientData;
1834 Tcl_AppendResult (interp, wrongArgs, *argv, "method args...\"", NULL);
1838 tab[0].tab = ir_method_tab;
1840 tab[1].tab = ir_set_c_method_tab;
1841 tab[1].obj = &p->set_inher;
1844 if (ir_tcl_method (interp, argc, argv, tab, &r) == TCL_ERROR)
1845 return ir_tcl_method_error (interp, argc, argv, tab);
1850 * ir_obj_delete: IR Object disposal
1852 static void ir_obj_delete (ClientData clientData)
1854 IrTcl_Obj *obj = clientData;
1855 IrTcl_Methods tab[3];
1858 if (obj->ref_count > 0)
1860 assert (obj->ref_count == 0);
1862 logf (LOG_DEBUG, "ir object delete");
1863 tab[0].tab = ir_method_tab;
1865 tab[1].tab = ir_set_c_method_tab;
1866 tab[1].obj = &obj->set_inher;
1869 ir_tcl_method (NULL, -1, NULL, tab, NULL);
1872 odr_destroy (obj->odr_in);
1873 odr_destroy (obj->odr_out);
1874 odr_destroy (obj->odr_pr);
1879 * ir_obj_init: IR Object initialization
1881 int ir_obj_init (ClientData clientData, Tcl_Interp *interp,
1882 int argc, char **argv, ClientData *subData,
1883 ClientData parentData)
1885 IrTcl_Methods tab[3];
1893 Tcl_AppendResult (interp, wrongArgs, *argv, " objName\"", NULL);
1896 obj = ir_tcl_malloc (sizeof(*obj));
1899 obj->bibset = ccl_qual_mk ();
1900 if ((inf = fopen ("default.bib", "r")))
1902 ccl_qual_file (obj->bibset, inf);
1907 logf (LOG_DEBUG, "ir object create %s", argv[1]);
1908 obj->odr_in = odr_createmem (ODR_DECODE);
1909 odr_choice_enable_bias (obj->odr_in, 0);
1910 obj->odr_out = odr_createmem (ODR_ENCODE);
1911 obj->odr_pr = odr_createmem (ODR_PRINT);
1912 obj->state = IR_TCL_R_Idle;
1913 obj->interp = interp;
1917 obj->request_queue = NULL;
1919 tab[0].tab = ir_method_tab;
1921 tab[1].tab = ir_set_c_method_tab;
1922 tab[1].obj = &obj->set_inher;
1925 if (ir_tcl_method (interp, 0, NULL, tab, NULL) == TCL_ERROR)
1927 Tcl_AppendResult (interp, "Failed to initialize ", argv[1], NULL);
1936 * ir_obj_mk: IR Object creation
1938 static int ir_obj_mk (ClientData clientData, Tcl_Interp *interp,
1939 int argc, char **argv)
1942 int r = ir_obj_init (clientData, interp, argc, argv, &subData, 0);
1946 Tcl_CreateCommand (interp, argv[1], ir_obj_method,
1947 subData, ir_obj_delete);
1951 IrTcl_Class ir_obj_class = {
1959 /* ------------------------------------------------------- */
1961 * do_search: Do search request
1963 static int do_search (void *o, Tcl_Interp *interp, int argc, char **argv)
1965 Z_SearchRequest *req;
1969 IrTcl_SetObj *obj = o;
1980 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1], "query\"",
1984 logf (LOG_DEBUG, "search %s %s", *argv, argv[2]);
1985 if (!obj->set_inher.num_databaseNames)
1987 Tcl_AppendResult (interp, "no databaseNames", NULL);
1988 return ir_tcl_error_exec (interp, argc, argv);
1992 Tcl_AppendResult (interp, "not connected", NULL);
1993 return ir_tcl_error_exec (interp, argc, argv);
1995 apdu = zget_APDU (p->odr_out, Z_APDU_searchRequest);
1996 req = apdu->u.searchRequest;
2000 set_referenceId (p->odr_out, &req->referenceId,
2001 obj->set_inher.referenceId);
2003 req->smallSetUpperBound = &obj->set_inher.smallSetUpperBound;
2004 req->largeSetLowerBound = &obj->set_inher.largeSetLowerBound;
2005 req->mediumSetPresentNumber = &obj->set_inher.mediumSetPresentNumber;
2006 req->replaceIndicator = &obj->set_inher.replaceIndicator;
2007 req->resultSetName = obj->setName ? obj->setName : "default";
2008 logf (LOG_DEBUG, "Search, resultSetName %s", req->resultSetName);
2009 req->num_databaseNames = obj->set_inher.num_databaseNames;
2010 req->databaseNames = obj->set_inher.databaseNames;
2011 for (r=0; r < obj->set_inher.num_databaseNames; r++)
2012 logf (LOG_DEBUG, " Database %s", obj->set_inher.databaseNames[r]);
2013 if (obj->set_inher.preferredRecordSyntax)
2015 struct oident ident;
2017 ident.proto = p->protocol_type;
2018 ident.oclass = CLASS_RECSYN;
2019 ident.value = *obj->set_inher.preferredRecordSyntax;
2020 logf (LOG_DEBUG, "Preferred record syntax is %d", ident.value);
2021 req->preferredRecordSyntax = odr_oiddup (p->odr_out,
2022 oid_getoidbyent (&ident));
2025 req->preferredRecordSyntax = 0;
2027 if (obj->set_inher.smallSetElementSetNames &&
2028 *obj->set_inher.smallSetElementSetNames)
2030 Z_ElementSetNames *esn = odr_malloc (p->odr_out, sizeof(*esn));
2032 esn->which = Z_ElementSetNames_generic;
2033 esn->u.generic = obj->set_inher.smallSetElementSetNames;
2034 req->smallSetElementSetNames = esn;
2037 req->smallSetElementSetNames = NULL;
2039 if (obj->set_inher.mediumSetElementSetNames &&
2040 *obj->set_inher.mediumSetElementSetNames)
2042 Z_ElementSetNames *esn = odr_malloc (p->odr_out, sizeof(*esn));
2044 esn->which = Z_ElementSetNames_generic;
2045 esn->u.generic = obj->set_inher.mediumSetElementSetNames;
2046 req->mediumSetElementSetNames = esn;
2049 req->mediumSetElementSetNames = NULL;
2051 req->query = &query;
2053 logf (LOG_DEBUG, "queryType %s", obj->set_inher.queryType);
2054 if (!strcmp (obj->set_inher.queryType, "rpn"))
2056 Z_RPNQuery *RPNquery;
2058 RPNquery = p_query_rpn (p->odr_out, p->protocol_type, argv[2]);
2061 Tcl_AppendResult (interp, "query syntax error", NULL);
2062 return ir_tcl_error_exec (interp, argc, argv);
2064 query.which = Z_Query_type_1;
2065 query.u.type_1 = RPNquery;
2068 else if (!strcmp (obj->set_inher.queryType, "cclrpn"))
2072 struct ccl_rpn_node *rpn;
2073 Z_RPNQuery *RPNquery;
2076 bib1.proto = p->protocol_type;
2077 bib1.oclass = CLASS_ATTSET;
2078 bib1.value = VAL_BIB1;
2080 rpn = ccl_find_str(p->bibset, argv[2], &error, &pos);
2083 Tcl_AppendResult (interp, "ccl syntax error ", ccl_err_msg(error),
2085 return ir_tcl_error_exec (interp, argc, argv);
2088 ccl_pr_tree (rpn, stderr);
2089 fprintf (stderr, "\n");
2091 assert((RPNquery = ccl_rpn_query(rpn)));
2092 RPNquery->attributeSetId = oid_getoidbyent (&bib1);
2093 query.which = Z_Query_type_1;
2094 query.u.type_1 = RPNquery;
2097 else if (!strcmp (obj->set_inher.queryType, "ccl"))
2099 query.which = Z_Query_type_2;
2100 query.u.type_2 = &ccl_query;
2101 ccl_query.buf = (unsigned char *) argv[2];
2102 ccl_query.len = strlen (argv[2]);
2106 Tcl_AppendResult (interp, "invalid query method ",
2107 obj->set_inher.queryType, NULL);
2108 return ir_tcl_error_exec (interp, argc, argv);
2110 return ir_tcl_send_APDU (interp, p, apdu, "search", *argv);
2114 * do_searchResponse: add search response handler
2116 static int do_searchResponse (void *o, Tcl_Interp *interp,
2117 int argc, char **argv)
2119 IrTcl_SetObj *obj = o;
2123 obj->searchResponse = NULL;
2126 else if (argc == -1)
2127 return ir_tcl_strdel (interp, &obj->searchResponse);
2130 xfree (obj->searchResponse);
2133 if (ir_tcl_strdup (interp, &obj->searchResponse, argv[2])
2138 obj->searchResponse = NULL;
2144 * do_presentResponse: add present response handler
2146 static int do_presentResponse (void *o, Tcl_Interp *interp,
2147 int argc, char **argv)
2149 IrTcl_SetObj *obj = o;
2153 obj->presentResponse = NULL;
2156 else if (argc == -1)
2157 return ir_tcl_strdel (interp, &obj->presentResponse);
2160 xfree (obj->presentResponse);
2163 if (ir_tcl_strdup (interp, &obj->presentResponse, argv[2])
2168 obj->presentResponse = NULL;
2174 * do_resultCount: Get number of hits
2176 static int do_resultCount (void *o, Tcl_Interp *interp,
2177 int argc, char **argv)
2179 IrTcl_SetObj *obj = o;
2183 obj->resultCount = 0;
2186 return ir_tcl_get_set_int (&obj->resultCount, interp, argc, argv);
2190 * do_searchStatus: Get search status (after search response)
2192 static int do_searchStatus (void *o, Tcl_Interp *interp,
2193 int argc, char **argv)
2195 IrTcl_SetObj *obj = o;
2199 return ir_tcl_get_set_int (&obj->searchStatus, interp, argc, argv);
2203 * do_presentStatus: Get search status (after search/present response)
2205 static int do_presentStatus (void *o, Tcl_Interp *interp,
2206 int argc, char **argv)
2208 IrTcl_SetObj *obj = o;
2212 return ir_tcl_get_set_int (&obj->presentStatus, interp, argc, argv);
2216 * do_nextResultSetPosition: Get next result set position
2217 * (after search/present response)
2219 static int do_nextResultSetPosition (void *o, Tcl_Interp *interp,
2220 int argc, char **argv)
2222 IrTcl_SetObj *obj = o;
2226 obj->nextResultSetPosition = 0;
2229 return ir_tcl_get_set_int (&obj->nextResultSetPosition, interp,
2234 * do_setName: Set result Set name
2236 static int do_setName (void *o, Tcl_Interp *interp,
2237 int argc, char **argv)
2239 IrTcl_SetObj *obj = o;
2242 return ir_tcl_strdup (interp, &obj->setName, "Default");
2243 else if (argc == -1)
2244 return ir_tcl_strdel (interp, &obj->setName);
2247 xfree (obj->setName);
2248 if (ir_tcl_strdup (interp, &obj->setName, argv[2])
2252 Tcl_AppendElement (interp, obj->setName);
2257 * do_numberOfRecordsReturned: Get number of records returned
2259 static int do_numberOfRecordsReturned (void *o, Tcl_Interp *interp,
2260 int argc, char **argv)
2262 IrTcl_SetObj *obj = o;
2266 obj->numberOfRecordsReturned = 0;
2269 return ir_tcl_get_set_int (&obj->numberOfRecordsReturned, interp,
2274 * do_type: Return type (if any) at position.
2276 static int do_type (void *o, Tcl_Interp *interp, int argc, char **argv)
2278 IrTcl_SetObj *obj = o;
2280 IrTcl_RecordList *rl;
2284 obj->record_list = NULL;
2287 else if (argc == -1)
2289 delete_IR_records (obj);
2294 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2295 " position\"", NULL);
2298 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2300 rl = find_IR_record (obj, offset);
2303 logf (LOG_DEBUG, "%s %s %s: no record", argv[0], argv[1], argv[2]);
2308 case Z_NamePlusRecord_databaseRecord:
2309 interp->result = "DB";
2311 case Z_NamePlusRecord_surrogateDiagnostic:
2312 interp->result = "SD";
2320 * do_recordType: Return record type (if any) at position.
2322 static int do_recordType (void *o, Tcl_Interp *interp, int argc, char **argv)
2324 IrTcl_SetObj *obj = o;
2326 IrTcl_RecordList *rl;
2332 else if (argc == -1)
2338 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2339 " position\"", NULL);
2342 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2344 rl = find_IR_record (obj, offset);
2347 logf (LOG_DEBUG, "%s %s %s: no record", argv[0], argv[1], argv[2]);
2350 if (rl->which != Z_NamePlusRecord_databaseRecord)
2352 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2355 Tcl_AppendElement (interp, (char*)
2356 IrTcl_getRecordSyntaxStr (rl->u.dbrec.type));
2361 * set record elements (for record extraction)
2363 static int do_recordElements (void *o, Tcl_Interp *interp,
2364 int argc, char **argv)
2366 IrTcl_SetObj *obj = o;
2370 obj->recordElements = NULL;
2373 else if (argc == -1)
2374 return ir_tcl_strdel (NULL, &obj->recordElements);
2377 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2378 " ?position?\"", NULL);
2383 xfree (obj->recordElements);
2384 return ir_tcl_strdup (NULL, &obj->recordElements,
2385 (*argv[2] ? argv[2] : NULL));
2387 Tcl_AppendResult (interp, obj->recordElements, NULL);
2394 static int ir_diagResult (Tcl_Interp *interp, IrTcl_Diagnostic *list, int num)
2400 for (i = 0; i<num; i++)
2402 sprintf (buf, "%d", list[i].condition);
2403 Tcl_AppendElement (interp, buf);
2404 cp = diagbib1_str (list[i].condition);
2406 Tcl_AppendElement (interp, (char*) cp);
2408 Tcl_AppendElement (interp, "");
2409 if (list[i].addinfo)
2410 Tcl_AppendElement (interp, (char*) list[i].addinfo);
2412 Tcl_AppendElement (interp, "");
2418 * do_diag: Return diagnostic record info
2420 static int do_diag (void *o, Tcl_Interp *interp, int argc, char **argv)
2422 IrTcl_SetObj *obj = o;
2424 IrTcl_RecordList *rl;
2430 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2431 " position\"", NULL);
2434 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2436 rl = find_IR_record (obj, offset);
2439 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2442 if (rl->which != Z_NamePlusRecord_surrogateDiagnostic)
2444 Tcl_AppendResult (interp, "No Diagnostic record at #", argv[2], NULL);
2447 return ir_diagResult (interp, rl->u.surrogateDiagnostics.list,
2448 rl->u.surrogateDiagnostics.num);
2452 * do_getMarc: Get ISO2709 Record lines/fields
2454 static int do_getMarc (void *o, Tcl_Interp *interp, int argc, char **argv)
2456 IrTcl_SetObj *obj = o;
2458 IrTcl_RecordList *rl;
2464 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2465 " position line|field tag ind field\"", NULL);
2468 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2470 rl = find_IR_record (obj, offset);
2473 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2476 if (rl->which != Z_NamePlusRecord_databaseRecord)
2478 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2481 return ir_tcl_get_marc (interp, rl->u.dbrec.buf, argc, argv);
2485 * do_getSutrs: Get SUTRS Record
2487 static int do_getSutrs (void *o, Tcl_Interp *interp, int argc, char **argv)
2489 IrTcl_SetObj *obj = o;
2491 IrTcl_RecordList *rl;
2497 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2498 " position\"", NULL);
2501 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2503 rl = find_IR_record (obj, offset);
2506 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2509 if (rl->which != Z_NamePlusRecord_databaseRecord)
2511 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2514 if (rl->u.dbrec.type != VAL_SUTRS)
2516 Tcl_AppendElement (interp, rl->u.dbrec.buf);
2522 * do_getGrs: Get a GRS-1 Record
2524 static int do_getGrs (void *o, Tcl_Interp *interp, int argc, char **argv)
2526 IrTcl_SetObj *obj = o;
2528 IrTcl_RecordList *rl;
2534 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2535 " position ?(set,tag) (set,tag) ...?\"", NULL);
2538 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2540 rl = find_IR_record (obj, offset);
2543 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2546 if (rl->which != Z_NamePlusRecord_databaseRecord)
2548 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2551 if (rl->u.dbrec.type != VAL_GRS1)
2553 return ir_tcl_get_grs (interp, rl->u.dbrec.u.grs1, argc, argv);
2558 * do_getExplain: Get an Explain Record
2560 static int do_getExplain (void *o, Tcl_Interp *interp, int argc, char **argv)
2562 IrTcl_SetObj *obj = o;
2563 IrTcl_Obj *p = obj->parent;
2565 Z_ext_typeent *etype;
2567 IrTcl_RecordList *rl;
2573 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2574 " position ?mask? ...\"", NULL);
2577 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2579 rl = find_IR_record (obj, offset);
2582 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2585 if (rl->which != Z_NamePlusRecord_databaseRecord)
2587 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2590 if (rl->u.dbrec.type != VAL_EXPLAIN)
2593 if (!(etype = z_ext_getentbyref (VAL_EXPLAIN)))
2596 odr_setbuf (p->odr_in, rl->u.dbrec.buf, rl->u.dbrec.size, 0);
2597 if (!(*etype->fun)(p->odr_in, &rr, 0))
2600 if (etype->what != Z_External_explainRecord)
2603 return ir_tcl_get_explain (interp, rr, argc, argv);
2607 * do_responseStatus: Return response status (present or search)
2609 static int do_responseStatus (void *o, Tcl_Interp *interp,
2610 int argc, char **argv)
2612 IrTcl_SetObj *obj = o;
2616 obj->recordFlag = 0;
2617 obj->nonSurrogateDiagnosticNum = 0;
2618 obj->nonSurrogateDiagnosticList = NULL;
2621 else if (argc == -1)
2623 ir_deleteDiags (&obj->nonSurrogateDiagnosticList,
2624 &obj->nonSurrogateDiagnosticNum);
2627 if (!obj->recordFlag)
2629 Tcl_AppendElement (interp, "OK");
2634 case Z_Records_DBOSD:
2635 Tcl_AppendElement (interp, "DBOSD");
2638 Tcl_AppendElement (interp, "NSD");
2639 return ir_diagResult (interp, obj->nonSurrogateDiagnosticList,
2640 obj->nonSurrogateDiagnosticNum);
2646 * do_present: Perform Present Request
2649 static int do_present (void *o, Tcl_Interp *interp, int argc, char **argv)
2651 IrTcl_SetObj *obj = o;
2654 Z_PresentRequest *req;
2662 if (Tcl_GetInt (interp, argv[2], &start) == TCL_ERROR)
2669 if (Tcl_GetInt (interp, argv[3], &number) == TCL_ERROR)
2674 logf (LOG_DEBUG, "present %s %d %d", *argv, start, number);
2678 Tcl_AppendResult (interp, "not connected", NULL);
2679 return ir_tcl_error_exec (interp, argc, argv);
2682 obj->number = number;
2684 apdu = zget_APDU (p->odr_out, Z_APDU_presentRequest);
2685 req = apdu->u.presentRequest;
2687 set_referenceId (p->odr_out, &req->referenceId,
2688 obj->set_inher.referenceId);
2690 req->resultSetId = obj->setName ? obj->setName : "Default";
2692 req->resultSetStartPoint = &start;
2693 req->numberOfRecordsRequested = &number;
2694 if (obj->set_inher.preferredRecordSyntax)
2696 struct oident ident;
2698 ident.proto = p->protocol_type;
2699 ident.oclass = CLASS_RECSYN;
2700 ident.value = *obj->set_inher.preferredRecordSyntax;
2701 logf (LOG_DEBUG, "Preferred record syntax is %d", ident.value);
2702 req->preferredRecordSyntax = odr_oiddup (p->odr_out,
2703 oid_getoidbyent (&ident));
2706 req->preferredRecordSyntax = 0;
2708 if (obj->set_inher.elementSetNames && *obj->set_inher.elementSetNames)
2710 Z_ElementSetNames *esn = odr_malloc (p->odr_out, sizeof(*esn));
2711 Z_RecordComposition *compo = odr_malloc (p->odr_out, sizeof(*compo));
2713 esn->which = Z_ElementSetNames_generic;
2714 esn->u.generic = obj->set_inher.elementSetNames;
2716 req->recordComposition = compo;
2717 compo->which = Z_RecordComp_simple;
2718 compo->u.simple = esn;
2721 req->recordComposition = NULL;
2722 return ir_tcl_send_APDU (interp, p, apdu, "present", *argv);
2725 #define IR_TCL_RECORD_ENCODING_ISO2709 1
2726 #define IR_TCL_RECORD_ENCODING_RAW 2
2732 } IrTcl_FileRecordHead;
2735 * do_loadFile: Load result set from file
2737 static int do_loadFile (void *o, Tcl_Interp *interp,
2738 int argc, char **argv)
2740 IrTcl_SetObj *setobj = o;
2752 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2753 " filename ?start? ?number?\"", NULL);
2757 start = atoi (argv[3]);
2759 number = atoi (argv[4]);
2762 inf = fopen (argv[2], "r");
2765 Tcl_AppendResult (interp, "Cannot open ", argv[2], NULL);
2766 return ir_tcl_error_exec (interp, argc, argv);
2768 while (offset < (start+number))
2770 IrTcl_FileRecordHead head;
2771 IrTcl_RecordList *rl;
2773 if (fread (&head, sizeof(head), 1, inf) < 1)
2775 rl = new_IR_record (setobj, offset,
2776 Z_NamePlusRecord_databaseRecord,
2777 (argc > 5) ? argv[5] : NULL);
2778 rl->u.dbrec.type = head.syntax;
2779 if (head.encoding == IR_TCL_RECORD_ENCODING_ISO2709)
2781 if (!(buf = ir_tcl_fread_marc (inf, &size)))
2783 rl->u.dbrec.buf = buf;
2784 rl->u.dbrec.size = size;
2785 if (size != head.size)
2788 Tcl_AppendResult (interp, "bad ISO2709 encoding", NULL);
2789 return ir_tcl_error_exec (interp, argc, argv);
2792 else if (head.encoding == IR_TCL_RECORD_ENCODING_RAW)
2794 rl->u.dbrec.size = head.size;
2795 rl->u.dbrec.buf = ir_tcl_malloc (head.size + 1);
2796 if (fread (rl->u.dbrec.buf, rl->u.dbrec.size, 1, inf) < 1)
2799 Tcl_AppendResult (interp, "bad raw encoding", NULL);
2800 return ir_tcl_error_exec (interp, argc, argv);
2802 rl->u.dbrec.buf[rl->u.dbrec.size] = '\0';
2806 rl->u.dbrec.buf = NULL;
2807 rl->u.dbrec.size = 0;
2809 Tcl_AppendResult (interp, "bad encoding", NULL);
2810 return ir_tcl_error_exec (interp, argc, argv);
2814 setobj->numberOfRecordsReturned = offset - start;
2820 * do_saveFile: Save result set on file
2822 static int do_saveFile (void *o, Tcl_Interp *interp,
2823 int argc, char **argv)
2825 IrTcl_SetObj *setobj = o;
2830 IrTcl_RecordList *rl;
2836 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2837 " filename ?start? ?number?\"", NULL);
2841 start = atoi (argv[3]);
2843 number = atoi (argv[4]);
2846 outf = fopen (argv[2], "w");
2849 Tcl_AppendResult (interp, "cannot open file", NULL);
2850 return ir_tcl_error_exec (interp, argc, argv);
2852 while (offset < (start+number) && (rl = find_IR_record (setobj, offset)))
2854 if (rl->which == Z_NamePlusRecord_databaseRecord &&
2855 rl->u.dbrec.buf && rl->u.dbrec.size)
2857 IrTcl_FileRecordHead head;
2859 head.encoding = IR_TCL_RECORD_ENCODING_RAW;
2860 head.syntax = rl->u.dbrec.type;
2861 head.size = rl->u.dbrec.size;
2862 if (fwrite (&head, sizeof(head), 1, outf) < 1)
2864 Tcl_AppendResult (interp, "cannot write", NULL);
2865 return ir_tcl_error_exec (interp, argc, argv);
2867 if (fwrite (rl->u.dbrec.buf, rl->u.dbrec.size, 1, outf) < 1)
2869 Tcl_AppendResult (interp, "cannot write", NULL);
2870 return ir_tcl_error_exec (interp, argc, argv);
2877 Tcl_AppendResult (interp, "cannot write ", NULL);
2878 return ir_tcl_error_exec (interp, argc, argv);
2884 static IrTcl_Method ir_set_method_tab[] = {
2885 { "search", do_search, NULL},
2886 { "searchResponse", do_searchResponse, NULL},
2887 { "presentResponse", do_presentResponse, NULL},
2888 { "searchStatus", do_searchStatus, NULL},
2889 { "presentStatus", do_presentStatus, NULL},
2890 { "nextResultSetPosition", do_nextResultSetPosition, NULL},
2891 { "setName", do_setName, NULL},
2892 { "resultCount", do_resultCount, NULL},
2893 { "numberOfRecordsReturned", do_numberOfRecordsReturned, NULL},
2894 { "present", do_present, NULL},
2895 { "type", do_type, NULL},
2896 { "getMarc", do_getMarc, NULL},
2897 { "getSutrs", do_getSutrs, NULL},
2898 { "getGrs", do_getGrs, NULL},
2899 { "getExplain", do_getExplain, NULL},
2900 { "recordType", do_recordType, NULL},
2901 { "recordElements", do_recordElements, NULL},
2902 { "diag", do_diag, NULL},
2903 { "responseStatus", do_responseStatus, NULL},
2904 { "loadFile", do_loadFile, NULL},
2905 { "saveFile", do_saveFile, NULL},
2910 * ir_set_obj_method: IR Set Object methods
2912 static int ir_set_obj_method (ClientData clientData, Tcl_Interp *interp,
2913 int argc, char **argv)
2915 IrTcl_Methods tabs[3];
2916 IrTcl_SetObj *p = clientData;
2921 Tcl_AppendResult (interp, wrongArgs, *argv, " method args...\"", NULL);
2924 tabs[0].tab = ir_set_method_tab;
2926 tabs[1].tab = ir_set_c_method_tab;
2927 tabs[1].obj = &p->set_inher;
2930 if (ir_tcl_method (interp, argc, argv, tabs, &r) == TCL_ERROR)
2931 return ir_tcl_method_error (interp, argc, argv, tabs);
2936 * ir_set_obj_delete: IR Set Object disposal
2938 static void ir_set_obj_delete (ClientData clientData)
2940 IrTcl_Methods tabs[3];
2941 IrTcl_SetObj *p = clientData;
2943 logf (LOG_DEBUG, "ir set delete");
2945 tabs[0].tab = ir_set_method_tab;
2947 tabs[1].tab = ir_set_c_method_tab;
2948 tabs[1].obj = &p->set_inher;
2951 ir_tcl_method (NULL, -1, NULL, tabs, NULL);
2957 * ir_set_obj_init: IR Set Object initialization
2959 static int ir_set_obj_init (ClientData clientData, Tcl_Interp *interp,
2960 int argc, char **argv, ClientData *subData,
2961 ClientData parentData)
2963 IrTcl_Methods tabs[3];
2966 if (argc < 2 || argc > 3)
2968 Tcl_AppendResult (interp, wrongArgs, *argv,
2969 " objSetName ?objParent?\"", NULL);
2972 obj = ir_tcl_malloc (sizeof(*obj));
2973 logf (LOG_DEBUG, "ir set create %s", argv[1]);
2980 obj->parent = (IrTcl_Obj *) parentData;
2982 dst = &obj->set_inher;
2983 src = &obj->parent->set_inher;
2985 if ((dst->num_databaseNames = src->num_databaseNames))
2987 dst->databaseNames =
2988 ir_tcl_malloc (sizeof (*dst->databaseNames)
2989 * (1+dst->num_databaseNames));
2990 for (i = 0; i < dst->num_databaseNames; i++)
2991 if (ir_tcl_strdup (interp, &dst->databaseNames[i],
2992 src->databaseNames[i]) == TCL_ERROR)
2994 dst->databaseNames[i] = NULL;
2997 dst->databaseNames = NULL;
2998 if (ir_tcl_strdup (interp, &dst->queryType, src->queryType)
3002 if (ir_tcl_strdup (interp, &dst->referenceId, src->referenceId)
3006 if (ir_tcl_strdup (interp, &dst->elementSetNames, src->elementSetNames)
3010 if (ir_tcl_strdup (interp, &dst->smallSetElementSetNames,
3011 src->smallSetElementSetNames)
3015 if (ir_tcl_strdup (interp, &dst->mediumSetElementSetNames,
3016 src->mediumSetElementSetNames)
3020 if (src->preferredRecordSyntax &&
3021 (dst->preferredRecordSyntax
3022 = ir_tcl_malloc (sizeof(*dst->preferredRecordSyntax))))
3023 *dst->preferredRecordSyntax = *src->preferredRecordSyntax;
3025 dst->preferredRecordSyntax = NULL;
3026 dst->replaceIndicator = src->replaceIndicator;
3027 dst->smallSetUpperBound = src->smallSetUpperBound;
3028 dst->largeSetLowerBound = src->largeSetLowerBound;
3029 dst->mediumSetPresentNumber = src->mediumSetPresentNumber;
3034 tabs[0].tab = ir_set_method_tab;
3038 if (ir_tcl_method (interp, 0, NULL, tabs, NULL) == TCL_ERROR)
3046 * ir_set_obj_mk: IR Set Object creation
3048 static int ir_set_obj_mk (ClientData clientData, Tcl_Interp *interp,
3049 int argc, char **argv)
3052 ClientData parentData = 0;
3057 Tcl_CmdInfo parent_info;
3058 if (!Tcl_GetCommandInfo (interp, argv[2], &parent_info))
3060 Tcl_AppendResult (interp, "no object parent", NULL);
3061 return ir_tcl_error_exec (interp, argc, argv);
3063 parentData = parent_info.clientData;
3065 r = ir_set_obj_init (clientData, interp, argc, argv, &subData, parentData);
3068 Tcl_CreateCommand (interp, argv[1], ir_set_obj_method,
3069 subData, ir_set_obj_delete);
3073 IrTcl_Class ir_set_obj_class = {
3080 /* ------------------------------------------------------- */
3083 * do_scan: Perform scan
3085 static int do_scan (void *o, Tcl_Interp *interp, int argc, char **argv)
3089 IrTcl_ScanObj *obj = o;
3090 IrTcl_Obj *p = obj->parent;
3093 struct ccl_rpn_node *rpn;
3101 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
3102 " scanQuery\"", NULL);
3105 logf (LOG_DEBUG, "scan %s %s", *argv, argv[2]);
3106 if (!p->set_inher.num_databaseNames)
3108 Tcl_AppendResult (interp, "no databaseNames", NULL);
3109 return ir_tcl_error_exec (interp, argc, argv);
3113 Tcl_AppendResult (interp, "not connected", NULL);
3114 return ir_tcl_error_exec (interp, argc, argv);
3117 apdu = zget_APDU (p->odr_out, Z_APDU_scanRequest);
3118 req = apdu->u.scanRequest;
3120 set_referenceId (p->odr_out, &req->referenceId, p->set_inher.referenceId);
3121 req->num_databaseNames = p->set_inher.num_databaseNames;
3122 req->databaseNames = p->set_inher.databaseNames;
3125 if (!(req->termListAndStartPoint =
3126 p_query_scan (p->odr_out, p->protocol_type,
3127 &req->attributeSet, argv[2])))
3129 Tcl_AppendResult (interp, "query syntax error", NULL);
3130 return ir_tcl_error_exec (interp, argc, argv);
3133 rpn = ccl_find_str(p->bibset, argv[2], &r, &pos);
3136 Tcl_AppendResult (interp, "ccl syntax error ", ccl_err_msg(r), NULL);
3137 return ir_tcl_error_exec (interp, argc, argv);
3139 bib1.proto = p->protocol_type;
3140 bib1.oclass = CLASS_ATTSET;
3141 bib1.value = VAL_BIB1;
3143 req->attributeSet = oid_getoidbyent (&bib1);
3144 if (!(req->termListAndStartPoint = ccl_scan_query (rpn)))
3147 req->stepSize = &obj->stepSize;
3148 req->numberOfTermsRequested = &obj->numberOfTermsRequested;
3149 req->preferredPositionInResponse = &obj->preferredPositionInResponse;
3150 logf (LOG_DEBUG, "stepSize=%d", *req->stepSize);
3151 logf (LOG_DEBUG, "numberOfTermsRequested=%d",
3152 *req->numberOfTermsRequested);
3153 logf (LOG_DEBUG, "preferredPositionInResponse=%d",
3154 *req->preferredPositionInResponse);
3156 return ir_tcl_send_APDU (interp, p, apdu, "scan", *argv);
3160 * do_scanResponse: add scan response handler
3162 static int do_scanResponse (void *o, Tcl_Interp *interp,
3163 int argc, char **argv)
3165 IrTcl_ScanObj *obj = o;
3169 obj->scanResponse = NULL;
3172 else if (argc == -1)
3173 return ir_tcl_strdel (interp, &obj->scanResponse);
3176 xfree (obj->scanResponse);
3179 if (ir_tcl_strdup (interp, &obj->scanResponse, argv[2])
3184 obj->scanResponse = NULL;
3190 * do_stepSize: Set/get replace Step Size
3192 static int do_stepSize (void *obj, Tcl_Interp *interp,
3193 int argc, char **argv)
3195 IrTcl_ScanObj *p = obj;
3201 return ir_tcl_get_set_int (&p->stepSize, interp, argc, argv);
3205 * do_numberOfTermsRequested: Set/get Number of Terms requested
3207 static int do_numberOfTermsRequested (void *obj, Tcl_Interp *interp,
3208 int argc, char **argv)
3210 IrTcl_ScanObj *p = obj;
3214 p->numberOfTermsRequested = 20;
3217 return ir_tcl_get_set_int (&p->numberOfTermsRequested, interp, argc, argv);
3222 * do_preferredPositionInResponse: Set/get preferred Position
3224 static int do_preferredPositionInResponse (void *obj, Tcl_Interp *interp,
3225 int argc, char **argv)
3227 IrTcl_ScanObj *p = obj;
3231 p->preferredPositionInResponse = 1;
3234 return ir_tcl_get_set_int (&p->preferredPositionInResponse, interp,
3239 * do_scanStatus: Get scan status
3241 static int do_scanStatus (void *obj, Tcl_Interp *interp,
3242 int argc, char **argv)
3244 IrTcl_ScanObj *p = obj;
3248 return ir_tcl_get_set_int (&p->scanStatus, interp, argc, argv);
3252 * do_numberOfEntriesReturned: Get number of Entries returned
3254 static int do_numberOfEntriesReturned (void *obj, Tcl_Interp *interp,
3255 int argc, char **argv)
3257 IrTcl_ScanObj *p = obj;
3261 return ir_tcl_get_set_int (&p->numberOfEntriesReturned, interp,
3266 * do_positionOfTerm: Get position of Term
3268 static int do_positionOfTerm (void *obj, Tcl_Interp *interp,
3269 int argc, char **argv)
3271 IrTcl_ScanObj *p = obj;
3275 return ir_tcl_get_set_int (&p->positionOfTerm, interp, argc, argv);
3279 * do_scanLine: get Scan Line (surrogate or normal) after response
3281 static int do_scanLine (void *obj, Tcl_Interp *interp, int argc, char **argv)
3283 IrTcl_ScanObj *p = obj;
3289 p->entries_flag = 0;
3291 p->nonSurrogateDiagnosticNum = 0;
3292 p->nonSurrogateDiagnosticList = 0;
3295 else if (argc == -1)
3297 p->entries_flag = 0;
3298 /* release entries */
3301 ir_deleteDiags (&p->nonSurrogateDiagnosticList,
3302 &p->nonSurrogateDiagnosticNum);
3307 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
3308 " position\"", NULL);
3311 if (Tcl_GetInt (interp, argv[2], &i) == TCL_ERROR)
3313 if (!p->entries_flag || p->which != Z_ListEntries_entries || !p->entries
3314 || i >= p->num_entries || i < 0)
3316 switch (p->entries[i].which)
3318 case Z_Entry_termInfo:
3319 Tcl_AppendElement (interp, "T");
3320 if (p->entries[i].u.term.buf)
3321 Tcl_AppendElement (interp, p->entries[i].u.term.buf);
3323 Tcl_AppendElement (interp, "");
3324 sprintf (numstr, "%d", p->entries[i].u.term.globalOccurrences);
3325 Tcl_AppendElement (interp, numstr);
3327 case Z_Entry_surrogateDiagnostic:
3328 Tcl_AppendElement (interp, "SD");
3329 return ir_diagResult (interp, p->entries[i].u.diag.list,
3330 p->entries[i].u.diag.num);
3336 static IrTcl_Method ir_scan_method_tab[] = {
3337 { "scan", do_scan, NULL},
3338 { "scanResponse", do_scanResponse, NULL},
3339 { "stepSize", do_stepSize, NULL},
3340 { "numberOfTermsRequested", do_numberOfTermsRequested, NULL},
3341 { "preferredPositionInResponse", do_preferredPositionInResponse, NULL},
3342 { "scanStatus", do_scanStatus, NULL},
3343 { "numberOfEntriesReturned", do_numberOfEntriesReturned, NULL},
3344 { "positionOfTerm", do_positionOfTerm, NULL},
3345 { "scanLine", do_scanLine, NULL},
3350 * ir_scan_obj_method: IR Scan Object methods
3352 static int ir_scan_obj_method (ClientData clientData, Tcl_Interp *interp,
3353 int argc, char **argv)
3355 IrTcl_Methods tabs[2];
3360 Tcl_AppendResult (interp, wrongArgs, *argv, " method args...\"", NULL);
3363 tabs[0].tab = ir_scan_method_tab;
3364 tabs[0].obj = clientData;
3367 if (ir_tcl_method (interp, argc, argv, tabs, &r) == TCL_ERROR)
3368 return ir_tcl_method_error (interp, argc, argv, tabs);
3373 * ir_scan_obj_delete: IR Scan Object disposal
3375 static void ir_scan_obj_delete (ClientData clientData)
3377 IrTcl_Methods tabs[2];
3378 IrTcl_ScanObj *obj = clientData;
3380 tabs[0].tab = ir_scan_method_tab;
3384 ir_tcl_method (NULL, -1, NULL, tabs, NULL);
3389 * ir_scan_obj_mk: IR Scan Object creation
3391 static int ir_scan_obj_mk (ClientData clientData, Tcl_Interp *interp,
3392 int argc, char **argv)
3394 Tcl_CmdInfo parent_info;
3396 IrTcl_Methods tabs[2];
3400 Tcl_AppendResult (interp, wrongArgs, *argv,
3401 "objScanName objParentName\"", NULL);
3404 logf (LOG_DEBUG, "ir scan create %s", argv[1]);
3405 if (!Tcl_GetCommandInfo (interp, argv[2], &parent_info))
3407 Tcl_AppendResult (interp, "no object parent", NULL);
3408 return ir_tcl_error_exec (interp, argc, argv);
3410 obj = ir_tcl_malloc (sizeof(*obj));
3411 obj->parent = (IrTcl_Obj *) parent_info.clientData;
3413 tabs[0].tab = ir_scan_method_tab;
3417 if (ir_tcl_method (interp, 0, NULL, tabs, NULL) == TCL_ERROR)
3419 Tcl_CreateCommand (interp, argv[1], ir_scan_obj_method,
3420 (ClientData) obj, ir_scan_obj_delete);
3424 /* ------------------------------------------------------- */
3427 * ir_log_proc: set yaz log level
3429 static int ir_log_init_proc (ClientData clientData, Tcl_Interp *interp,
3430 int argc, char **argv)
3432 if (argc <= 1 || argc > 4)
3434 Tcl_AppendResult (interp, wrongArgs, *argv,
3435 " ?level ?prefix ?filename\"", NULL);
3439 log_init (log_mask_str (argv[1]), "", NULL);
3441 log_init (log_mask_str (argv[1]), argv[2], NULL);
3443 log_init (log_mask_str (argv[1]), argv[2], argv[3]);
3447 /* ------------------------------------------------------- */
3448 static void ir_initResponse (void *obj, Z_InitResponse *initrs)
3452 p->initResult = *initrs->result ? 1 : 0;
3453 if (!*initrs->result)
3454 logf (LOG_DEBUG, "Connection rejected by target");
3456 logf (LOG_DEBUG, "Connection accepted by target");
3458 get_referenceId (&p->set_inher.referenceId, initrs->referenceId);
3460 xfree (p->targetImplementationId);
3461 ir_tcl_strdup (p->interp, &p->targetImplementationId,
3462 initrs->implementationId);
3463 xfree (p->targetImplementationName);
3464 ir_tcl_strdup (p->interp, &p->targetImplementationName,
3465 initrs->implementationName);
3466 xfree (p->targetImplementationVersion);
3467 ir_tcl_strdup (p->interp, &p->targetImplementationVersion,
3468 initrs->implementationVersion);
3470 p->maximumRecordSize = *initrs->maximumRecordSize;
3471 p->preferredMessageSize = *initrs->preferredMessageSize;
3473 memcpy (&p->options, initrs->options, sizeof(initrs->options));
3474 memcpy (&p->protocolVersion, initrs->protocolVersion,
3475 sizeof(initrs->protocolVersion));
3476 xfree (p->userInformationField);
3477 p->userInformationField = NULL;
3478 if (initrs->userInformationField)
3482 if (initrs->userInformationField->which == ODR_EXTERNAL_octet &&
3483 (p->userInformationField =
3484 ir_tcl_malloc ((len =
3485 initrs->userInformationField->
3486 u.octet_aligned->len) +1)))
3488 memcpy (p->userInformationField,
3489 initrs->userInformationField->u.octet_aligned->buf,
3491 (p->userInformationField)[len] = '\0';
3496 static void ir_deleteDiags (IrTcl_Diagnostic **dst_list, int *dst_num)
3499 for (i = 0; i<*dst_num; i++)
3500 xfree (dst_list[i]->addinfo);
3506 static void ir_handleDiags (IrTcl_Diagnostic **dst_list, int *dst_num,
3507 Z_DiagRec **list, int num)
3513 *dst_list = ir_tcl_malloc (sizeof(**dst_list) * num);
3514 for (i = 0; i<num; i++)
3517 switch (list[i]->which)
3519 case Z_DiagRec_defaultFormat:
3520 (*dst_list)[i].condition = *list[i]->u.defaultFormat->condition;
3521 addinfo = list[i]->u.defaultFormat->addinfo;
3523 ((*dst_list)[i].addinfo = ir_tcl_malloc (strlen(addinfo)+1)))
3524 strcpy ((*dst_list)[i].addinfo, addinfo);
3525 cp = diagbib1_str ((*dst_list)[i].condition);
3526 logf (LOG_DEBUG, "Diag %d %s %s", (*dst_list)[i].condition,
3527 cp ? cp : "", addinfo ? addinfo : "");
3530 (*dst_list)[i].addinfo = NULL;
3531 (*dst_list)[i].condition = 0;
3536 static void ir_handleDBRecord (IrTcl_Obj *p, IrTcl_RecordList *rl,
3539 struct oident *ident;
3540 Z_ext_typeent *etype;
3542 rl->u.dbrec.size = oe->u.octet_aligned->len;
3543 rl->u.dbrec.buf = NULL;
3545 if ((ident = oid_getentbyoid (oe->direct_reference)))
3546 rl->u.dbrec.type = ident->value;
3548 rl->u.dbrec.type = VAL_USMARC;
3550 if (ident && (oe->which == Z_External_single ||
3551 oe->which == Z_External_octet)
3552 && (etype = z_ext_getentbyref (ident->value)))
3556 odr_setbuf (p->odr_in, (char*) oe->u.octet_aligned->buf,
3557 oe->u.octet_aligned->len, 0);
3558 if (!(*etype->fun)(p->odr_in, &rr, 0))
3560 switch (etype->what)
3562 case Z_External_sutrs:
3563 logf (LOG_DEBUG, "Z_External_sutrs");
3565 if ((rl->u.dbrec.buf = ir_tcl_malloc (oe->u.sutrs->len+1)))
3567 memcpy (rl->u.dbrec.buf, oe->u.sutrs->buf,
3569 rl->u.dbrec.buf[oe->u.sutrs->len] = '\0';
3571 rl->u.dbrec.size = oe->u.sutrs->len;
3573 case Z_External_grs1:
3574 logf (LOG_DEBUG, "Z_External_grs1");
3576 ir_tcl_grs_mk (oe->u.grs1, &rl->u.dbrec.u.grs1);
3578 case Z_External_explainRecord:
3579 logf (LOG_DEBUG, "Z_External_explainRecord");
3580 if ((rl->u.dbrec.buf = ir_tcl_malloc (rl->u.dbrec.size)))
3582 memcpy (rl->u.dbrec.buf, oe->u.octet_aligned->buf,
3590 if (oe->which == Z_External_octet && rl->u.dbrec.size > 0)
3592 char *buf = (char*) oe->u.octet_aligned->buf;
3593 if ((rl->u.dbrec.buf = ir_tcl_malloc (rl->u.dbrec.size)))
3594 memcpy (rl->u.dbrec.buf, buf, rl->u.dbrec.size);
3596 else if (rl->u.dbrec.type == VAL_SUTRS &&
3597 oe->which == Z_External_sutrs)
3599 if ((rl->u.dbrec.buf = ir_tcl_malloc (oe->u.sutrs->len+1)))
3601 memcpy (rl->u.dbrec.buf, oe->u.sutrs->buf,
3603 rl->u.dbrec.buf[oe->u.sutrs->len] = '\0';
3605 rl->u.dbrec.size = oe->u.sutrs->len;
3607 else if (rl->u.dbrec.type == VAL_GRS1 &&
3608 oe->which == Z_External_grs1)
3610 ir_tcl_grs_mk (oe->u.grs1, &rl->u.dbrec.u.grs1);
3615 static void ir_handleZRecords (void *o, Z_Records *zrs, IrTcl_SetObj *setobj,
3616 const char *elements)
3621 IrTcl_RecordList *rl;
3623 setobj->which = zrs->which;
3624 setobj->recordFlag = 1;
3626 ir_deleteDiags (&setobj->nonSurrogateDiagnosticList,
3627 &setobj->nonSurrogateDiagnosticNum);
3628 if (zrs->which == Z_Records_DBOSD)
3630 setobj->numberOfRecordsReturned =
3631 zrs->u.databaseOrSurDiagnostics->num_records;
3632 logf (LOG_DEBUG, "Got %d records", setobj->numberOfRecordsReturned);
3633 for (offset = 0; offset < setobj->numberOfRecordsReturned; offset++)
3635 Z_NamePlusRecord *znpr = zrs->u.databaseOrSurDiagnostics->
3638 rl = new_IR_record (setobj, setobj->start + offset, znpr->which,
3640 if (rl->which == Z_NamePlusRecord_surrogateDiagnostic)
3641 ir_handleDiags (&rl->u.surrogateDiagnostics.list,
3642 &rl->u.surrogateDiagnostics.num,
3643 &znpr->u.surrogateDiagnostic,
3646 ir_handleDBRecord (p, rl,
3647 (Z_External*) (znpr->u.databaseRecord));
3650 else if (zrs->which == Z_Records_multipleNSD)
3652 logf (LOG_DEBUG, "multipleNonSurrogateDiagnostic %d",
3653 zrs->u.multipleNonSurDiagnostics->num_diagRecs);
3654 setobj->numberOfRecordsReturned = 0;
3655 ir_handleDiags (&setobj->nonSurrogateDiagnosticList,
3656 &setobj->nonSurrogateDiagnosticNum,
3657 zrs->u.multipleNonSurDiagnostics->diagRecs,
3658 zrs->u.multipleNonSurDiagnostics->num_diagRecs);
3662 logf (LOG_DEBUG, "NonSurrogateDiagnostic");
3663 setobj->numberOfRecordsReturned = 0;
3664 ir_handleDiags (&setobj->nonSurrogateDiagnosticList,
3665 &setobj->nonSurrogateDiagnosticNum,
3666 &zrs->u.nonSurrogateDiagnostic,
3671 static void ir_searchResponse (void *o, Z_SearchResponse *searchrs,
3672 IrTcl_SetObj *setobj)
3674 Z_Records *zrs = searchrs->records;
3676 logf (LOG_DEBUG, "Received search response");
3679 logf (LOG_DEBUG, "Search response, no object!");
3682 setobj->searchStatus = searchrs->searchStatus ? 1 : 0;
3683 get_referenceId (&setobj->set_inher.referenceId, searchrs->referenceId);
3684 setobj->resultCount = *searchrs->resultCount;
3685 if (searchrs->presentStatus)
3686 setobj->presentStatus = *searchrs->presentStatus;
3687 if (searchrs->nextResultSetPosition)
3688 setobj->nextResultSetPosition = *searchrs->nextResultSetPosition;
3690 logf (LOG_DEBUG, "status %d hits %d",
3691 setobj->searchStatus, setobj->resultCount);
3695 if (setobj->resultCount <= setobj->set_inher.smallSetUpperBound)
3696 es = setobj->set_inher.smallSetElementSetNames;
3698 es = setobj->set_inher.mediumSetElementSetNames;
3699 ir_handleZRecords (o, zrs, setobj, es);
3702 setobj->recordFlag = 0;
3706 static void ir_presentResponse (void *o, Z_PresentResponse *presrs,
3707 IrTcl_SetObj *setobj)
3709 Z_Records *zrs = presrs->records;
3711 logf (LOG_DEBUG, "Received present response");
3714 logf (LOG_DEBUG, "Present response, no object!");
3717 setobj->presentStatus = *presrs->presentStatus;
3718 get_referenceId (&setobj->set_inher.referenceId, presrs->referenceId);
3719 setobj->nextResultSetPosition = *presrs->nextResultSetPosition;
3721 ir_handleZRecords (o, zrs, setobj, setobj->set_inher.elementSetNames);
3724 setobj->recordFlag = 0;
3725 logf (LOG_DEBUG, "No records!");
3729 static void ir_scanResponse (void *o, Z_ScanResponse *scanrs,
3730 IrTcl_ScanObj *scanobj)
3734 logf (LOG_DEBUG, "Received scanResponse");
3736 get_referenceId (&p->set_inher.referenceId, scanrs->referenceId);
3737 scanobj->scanStatus = *scanrs->scanStatus;
3738 logf (LOG_DEBUG, "scanStatus=%d", scanobj->scanStatus);
3740 if (scanrs->stepSize)
3741 scanobj->stepSize = *scanrs->stepSize;
3742 logf (LOG_DEBUG, "stepSize=%d", scanobj->stepSize);
3744 scanobj->numberOfEntriesReturned = *scanrs->numberOfEntriesReturned;
3745 logf (LOG_DEBUG, "numberOfEntriesReturned=%d",
3746 scanobj->numberOfEntriesReturned);
3748 if (scanrs->positionOfTerm)
3749 scanobj->positionOfTerm = *scanrs->positionOfTerm;
3751 scanobj->positionOfTerm = -1;
3752 logf (LOG_DEBUG, "positionOfTerm=%d", scanobj->positionOfTerm);
3754 xfree (scanobj->entries);
3755 scanobj->entries = NULL;
3757 ir_deleteDiags (&scanobj->nonSurrogateDiagnosticList,
3758 &scanobj->nonSurrogateDiagnosticNum);
3759 if (scanrs->entries)
3764 scanobj->entries_flag = 1;
3765 scanobj->which = scanrs->entries->which;
3766 switch (scanobj->which)
3768 case Z_ListEntries_entries:
3769 scanobj->num_entries = scanrs->entries->u.entries->num_entries;
3770 scanobj->entries = ir_tcl_malloc (scanobj->num_entries *
3771 sizeof(*scanobj->entries));
3772 for (i=0; i<scanobj->num_entries; i++)
3774 ze = scanrs->entries->u.entries->entries[i];
3775 scanobj->entries[i].which = ze->which;
3778 case Z_Entry_termInfo:
3779 if (ze->u.termInfo->term->which == Z_Term_general)
3781 int l = ze->u.termInfo->term->u.general->len;
3782 scanobj->entries[i].u.term.buf = ir_tcl_malloc (1+l);
3783 memcpy (scanobj->entries[i].u.term.buf,
3784 ze->u.termInfo->term->u.general->buf,
3786 scanobj->entries[i].u.term.buf[l] = '\0';
3789 scanobj->entries[i].u.term.buf = NULL;
3790 if (ze->u.termInfo->globalOccurrences)
3791 scanobj->entries[i].u.term.globalOccurrences =
3792 *ze->u.termInfo->globalOccurrences;
3794 scanobj->entries[i].u.term.globalOccurrences = 0;
3796 case Z_Entry_surrogateDiagnostic:
3797 ir_handleDiags (&scanobj->entries[i].u.diag.list,
3798 &scanobj->entries[i].u.diag.num,
3799 &ze->u.surrogateDiagnostic,
3805 case Z_ListEntries_nonSurrogateDiagnostics:
3806 ir_handleDiags (&scanobj->nonSurrogateDiagnosticList,
3807 &scanobj->nonSurrogateDiagnosticNum,
3808 scanrs->entries->u.nonSurrogateDiagnostics->
3810 scanrs->entries->u.nonSurrogateDiagnostics->
3816 scanobj->entries_flag = 0;
3820 * ir_select_read: handle incoming packages
3822 static void ir_select_read (ClientData clientData)
3824 IrTcl_Obj *p = clientData;
3829 Tcl_CmdInfo cmd_info;
3830 const char *apdu_call;
3832 logf(LOG_DEBUG, "Read handler fd=%d", cs_fileno(p->cs_link));
3833 if (p->state == IR_TCL_R_Connecting)
3835 logf(LOG_DEBUG, "read: connect");
3836 r = cs_rcvconnect (p->cs_link);
3839 logf (LOG_WARN, "cs_rcvconnect returned 1");
3842 p->state = IR_TCL_R_Idle;
3844 ir_select_remove_write (cs_fileno (p->cs_link), p);
3847 logf (LOG_DEBUG, "cs_rcvconnect error");
3848 ir_tcl_disconnect (p);
3851 p->failInfo = IR_TCL_FAIL_CONNECT;
3852 ir_tcl_eval (p->interp, p->failback);
3858 ir_tcl_eval (p->interp, p->callback);
3859 if (p->ref_count == 2 && p->cs_link && p->request_queue
3860 && p->state == IR_TCL_R_Idle)
3861 ir_tcl_send_q (p, p->request_queue, "x");
3867 p->state = IR_TCL_R_Reading;
3869 /* read incoming APDU */
3870 if ((r=cs_get (p->cs_link, &p->buf_in, &p->len_in)) == 1)
3872 logf(LOG_DEBUG, "PDU Fraction read");
3875 /* signal one more use of ir object - callbacks must not
3876 release the ir memory (p pointer) */
3880 logf (LOG_DEBUG, "cs_get failed, code %d", r);
3881 ir_select_remove (cs_fileno (p->cs_link), p);
3882 ir_tcl_disconnect (p);
3885 p->failInfo = IR_TCL_FAIL_READ;
3886 ir_tcl_eval (p->interp, p->failback);
3888 /* release ir object now if callback deleted it */
3892 /* got complete APDU. Now decode */
3895 odr_setbuf (p->odr_in, p->buf_in, r, 0);
3896 logf (LOG_DEBUG, "cs_get ok, total size %d", r);
3897 if (!z_APDU (p->odr_in, &apdu, 0))
3899 logf (LOG_DEBUG, "cs_get failed: %s",
3900 odr_errmsg (odr_geterror (p->odr_in)));
3901 ir_tcl_disconnect (p);
3904 p->failInfo = IR_TCL_FAIL_IN_APDU;
3905 p->apduOffset = odr_offset (p->odr_in);
3906 ir_tcl_eval (p->interp, p->failback);
3908 /* release ir object now if failback deleted it */
3912 /* handle APDU and invoke callback */
3913 rq = p->request_queue;
3916 logf (LOG_FATAL, "Internal error. No queue entry");
3919 object_name = rq->object_name;
3920 logf (LOG_DEBUG, "Object %s", object_name);
3922 if (Tcl_GetCommandInfo (p->interp, object_name, &cmd_info))
3926 case Z_APDU_initResponse:
3927 p->eventType = "init";
3928 ir_initResponse (p, apdu->u.initResponse);
3929 apdu_call = p->initResponse;
3931 case Z_APDU_searchResponse:
3932 p->eventType = "search";
3933 ir_searchResponse (p, apdu->u.searchResponse,
3934 (IrTcl_SetObj *) cmd_info.clientData);
3935 apdu_call = ((IrTcl_SetObj *)
3936 cmd_info.clientData)->searchResponse;
3938 case Z_APDU_presentResponse:
3939 p->eventType = "present";
3940 ir_presentResponse (p, apdu->u.presentResponse,
3941 (IrTcl_SetObj *) cmd_info.clientData);
3942 apdu_call = ((IrTcl_SetObj *)
3943 cmd_info.clientData)->presentResponse;
3945 case Z_APDU_scanResponse:
3946 p->eventType = "scan";
3947 ir_scanResponse (p, apdu->u.scanResponse,
3948 (IrTcl_ScanObj *) cmd_info.clientData);
3949 apdu_call = ((IrTcl_ScanObj *)
3950 cmd_info.clientData)->scanResponse;
3953 logf (LOG_WARN, "Received unknown APDU type (%d)",
3955 ir_tcl_disconnect (p);
3958 p->failInfo = IR_TCL_FAIL_UNKNOWN_APDU;
3959 ir_tcl_eval (p->interp, p->failback);
3964 p->request_queue = rq->next;
3965 p->state = IR_TCL_R_Idle;
3968 ir_tcl_eval (p->interp, apdu_call);
3969 else if (rq->callback)
3970 ir_tcl_eval (p->interp, rq->callback);
3971 xfree (rq->buf_out);
3972 xfree (rq->callback);
3973 xfree (rq->object_name);
3975 odr_reset (p->odr_in);
3976 if (p->ref_count == 1)
3982 } while (p->cs_link && cs_more (p->cs_link));
3983 if (p->cs_link && p->request_queue && p->state == IR_TCL_R_Idle)
3984 ir_tcl_send_q (p, p->request_queue, "x");
3988 * ir_select_write: handle outgoing packages - not yet written.
3990 static int ir_select_write (ClientData clientData)
3992 IrTcl_Obj *p = clientData;
3996 logf (LOG_DEBUG, "Write handler fd=%d", cs_fileno(p->cs_link));
3997 if (p->state == IR_TCL_R_Connecting)
3999 logf(LOG_DEBUG, "write: connect");
4000 r = cs_rcvconnect (p->cs_link);
4003 logf (LOG_DEBUG, "cs_rcvconnect returned 1");
4006 p->state = IR_TCL_R_Idle;
4008 ir_select_remove_write (cs_fileno (p->cs_link), p);
4011 logf (LOG_DEBUG, "cs_rcvconnect error");
4012 ir_tcl_disconnect (p);
4015 p->failInfo = IR_TCL_FAIL_CONNECT;
4016 ir_tcl_eval (p->interp, p->failback);
4022 ir_tcl_eval (p->interp, p->callback);
4026 rq = p->request_queue;
4027 if (!rq || !rq->buf_out)
4030 if ((r=cs_put (p->cs_link, rq->buf_out, rq->len_out)) < 0)
4032 logf (LOG_DEBUG, "cs_put write fail");
4034 xfree (rq->buf_out);
4036 ir_tcl_disconnect (p);
4039 p->failInfo = IR_TCL_FAIL_WRITE;
4040 ir_tcl_eval (p->interp, p->failback);
4044 else if (r == 0) /* remove select bit */
4046 logf (LOG_DEBUG, "Write completed");
4047 p->state = IR_TCL_R_Waiting;
4048 ir_select_remove_write (cs_fileno (p->cs_link), p);
4049 xfree (rq->buf_out);
4055 static void ir_select_notify (ClientData clientData, int r, int w, int e)
4059 if (!ir_select_write (clientData) && r)
4060 ir_select_read (clientData);
4064 ir_select_read (clientData);
4068 /*----------------------------------------------------------- */
4072 * This wrapper function is used by Windows to invoke the
4073 * initialization code for the DLL. If we are compiling
4074 * with Visual C++, this routine will be renamed to DllMain.
4086 DllEntryPoint(hInst, reason, reserved)
4087 HINSTANCE hInst; /* Library instance handle. */
4088 DWORD reason; /* Reason this function is being called. */
4089 LPVOID reserved; /* Not used. */
4095 /* ------------------------------------------------------- */
4097 * Irtcl_init: Registration of TCL commands.
4099 EXPORT (int,Irtcl_Init) (Tcl_Interp *interp)
4101 Tcl_CreateCommand (interp, "ir", ir_obj_mk, (ClientData) NULL,
4102 (Tcl_CmdDeleteProc *) NULL);
4103 Tcl_CreateCommand (interp, "ir-set", ir_set_obj_mk,
4104 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
4105 Tcl_CreateCommand (interp, "ir-scan", ir_scan_obj_mk,
4106 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
4107 Tcl_CreateCommand (interp, "ir-log-init", ir_log_init_proc,
4108 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);