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.110 1998-10-20 15:15:31 adam
9 * Changed scan response handler.
11 * Revision 1.109 1998/10/13 21:23:26 adam
12 * Fixed searchStatus method.
14 * Revision 1.108 1998/10/12 11:48:08 adam
15 * Removed printf call.
17 * Revision 1.107 1998/06/10 13:00:46 adam
18 * Added ir-version command.
20 * Revision 1.106 1998/05/20 12:25:35 adam
21 * Fixed bug that occurred in rare cases when encoding of incoming
24 * Revision 1.105 1998/04/02 14:31:08 adam
25 * This version works with compiled ASN.1 code.
27 * Revision 1.104 1998/02/27 14:26:07 adam
28 * Changed client so that it still works if target sets numberOfRecords
29 * in response to an illegal value.
31 * Revision 1.103 1997/11/19 11:22:10 adam
32 * Object identifiers can be accessed in GRS-1 records.
34 * Revision 1.102 1997/09/17 12:22:40 adam
35 * Changed to use YAZ version 1.4. The new comstack utility, cs_straddr,
38 * Revision 1.101 1997/09/09 10:19:53 adam
39 * New MSV5.0 port with fewer warnings.
41 * Revision 1.100 1997/05/01 15:04:05 adam
42 * Added ir-log command.
44 * Revision 1.99 1997/04/30 07:24:47 adam
45 * Spell fix of an error message.
47 * Revision 1.98 1997/04/13 18:57:20 adam
48 * Better error reporting and aligned with Tcl/Tk style.
49 * Rework of notifier code with Tcl_File handles.
51 * Revision 1.97 1996/11/14 17:11:07 adam
52 * Added Explain documentaion.
54 * Revision 1.96 1996/10/08 13:02:50 adam
55 * When dealing with records, odr_choice_enable_bias function is used to
56 * prevent decoding of externals.
58 * Revision 1.95 1996/09/13 10:51:49 adam
59 * Bug fix: ir_tcl_select_set called Tcl_GetFile at disconnect.
61 * Revision 1.94 1996/08/21 13:32:53 adam
62 * Implemented saveFile method and extended loadFile method to work with it.
64 * Revision 1.93 1996/08/16 15:07:45 adam
65 * First work on Explain.
67 * Revision 1.92 1996/08/09 15:33:07 adam
68 * Modified the code to use tk4.1/tcl7.5 patch level 1. The time-driven
69 * polling is no longer activated on Windows since asynchrounous I/O works
72 * Revision 1.91 1996/07/03 13:31:11 adam
73 * The xmalloc/xfree functions from YAZ are used to manage memory.
75 * Revision 1.90 1996/06/27 14:21:00 adam
76 * Yet another Windows port.
78 * Revision 1.89 1996/06/11 15:27:15 adam
79 * Event type set to connect a little earlier in the do_connect function.
81 * Revision 1.88 1996/06/03 09:04:22 adam
82 * Changed a few logf calls.
84 * Revision 1.87 1996/05/29 06:37:51 adam
85 * Function ir_tcl_get_grs_r enhanced so that specific elements can be
88 * Revision 1.86 1996/03/20 13:54:04 adam
89 * The Tcl_File structure is only manipulated in the Tk-event interface
92 * Revision 1.85 1996/03/15 11:15:48 adam
93 * Modified to use new prototypes for p_query_rpn and p_query_scan.
95 * Revision 1.84 1996/03/07 12:42:49 adam
96 * Better logging when callback is invoked.
98 * Revision 1.83 1996/03/05 09:21:09 adam
99 * Bug fix: memory used by GRS records wasn't freed.
100 * Rewrote some of the error handling code - the connection is always
101 * closed before failback is called.
102 * If failback is defined the send APDU methods (init, search, ...) will
103 * return OK but invoke failback (as is the case if the write operation
105 * Bug fix: ref_count in assoc object could grow if fraction of PDU was
108 * Revision 1.82 1996/02/29 15:30:21 adam
109 * Export of IrTcl functionality to extensions.
111 * Revision 1.81 1996/02/26 18:38:32 adam
112 * Work on export of set methods.
114 * Revision 1.80 1996/02/23 17:31:39 adam
115 * More functions made available to the wais tcl extension.
117 * Revision 1.79 1996/02/23 13:41:38 adam
118 * Work on public access to simple ir class system.
120 * Revision 1.78 1996/02/21 10:16:08 adam
121 * Simplified select handling. Only one function ir_tcl_select_set has
122 * to be externally defined.
124 * Revision 1.77 1996/02/20 17:52:58 adam
125 * Uses the YAZ oid system to name record syntax object identifiers.
127 * Revision 1.76 1996/02/20 16:09:51 adam
128 * Bug fix: didn't set element set names stamp correctly on result
129 * set records when element set names were set to the empty string.
131 * Revision 1.75 1996/02/19 15:41:53 adam
132 * Better log messages.
133 * Minor improvement of connect method.
135 * Revision 1.74 1996/02/05 17:58:03 adam
136 * Ported ir-tcl to use the beta releases of tcl7.5/tk4.1.
138 * Revision 1.73 1996/01/29 11:35:19 adam
139 * Bug fix: cs_type member renamed to comstackType to avoid conflict with
140 * cs_type macro defined by YAZ.
142 * Revision 1.72 1996/01/19 17:45:34 quinn
143 * Added debugging output
145 * Revision 1.71 1996/01/19 16:22:38 adam
146 * New method: apduDump - returns information about last incoming APDU.
148 * Revision 1.70 1996/01/10 09:18:34 adam
149 * PDU specific callbacks implemented: initRespnse, searchResponse,
150 * presentResponse and scanResponse.
151 * Bug fix in the command line shell (tclmain.c) - discovered on OSF/1.
153 * Revision 1.69 1996/01/04 16:12:12 adam
154 * Setting PDUType renamed to eventType.
156 * Revision 1.68 1996/01/04 11:05:22 adam
157 * New setting: PDUType - returns type of last PDU returned from the target.
158 * Fixed a bug in configure/Makefile.
160 * Revision 1.67 1996/01/03 09:00:51 adam
161 * Updated to use new version of Yaz (names changed to avoid C++ conflict).
163 * Revision 1.66 1995/11/28 17:26:39 adam
164 * Removed Carriage return from ir-tcl.c!
165 * Removed misc. debug logs.
167 * Revision 1.65 1995/11/28 13:53:00 quinn
170 * Revision 1.64 1995/11/13 15:39:18 adam
171 * Bug fix: {small,medium}SetElementSetNames weren't set correctly.
172 * Bug fix: idAuthentication weren't set correctly.
174 * Revision 1.63 1995/11/13 09:55:39 adam
175 * Multiple records at a position in a result-set with differnt
178 * Revision 1.62 1995/10/18 17:20:33 adam
179 * Work on target setup in client.tcl.
181 * Revision 1.61 1995/10/18 16:42:42 adam
182 * New settings: smallSetElementSetNames and mediumSetElementSetNames.
184 * Revision 1.60 1995/10/18 15:43:31 adam
185 * In search: mediumSetElementSetNames and smallSetElementSetNames are
186 * set to elementSetNames.
188 * Revision 1.59 1995/10/17 12:18:58 adam
189 * Bug fix: when target connection closed, the connection was not
190 * properly reestablished.
192 * Revision 1.58 1995/10/16 17:00:55 adam
193 * New setting: elementSetNames.
194 * Various client improvements. Medium presentation format looks better.
196 * Revision 1.57 1995/09/21 13:11:51 adam
197 * Support of dynamic loading.
198 * Test script uses load command if necessary.
200 * Revision 1.56 1995/08/29 15:30:14 adam
201 * Work on GRS records.
203 * Revision 1.55 1995/08/28 09:43:25 adam
204 * Minor changes. configure only searches for yaz beta 3 and versions after
207 * Revision 1.54 1995/08/24 12:25:16 adam
208 * Modified to work with yaz 1.0b3.
210 * Revision 1.53 1995/08/04 12:49:26 adam
211 * Bug fix: reading uninitialized variable p.
213 * Revision 1.52 1995/08/04 11:32:38 adam
214 * More work on output queue. Memory related routines moved
217 * Revision 1.51 1995/08/03 13:22:54 adam
220 * Revision 1.50 1995/07/20 08:09:49 adam
221 * client.tcl: Targets removed from hotTargets list when targets
222 * are removed/modified.
223 * ir-tcl.c: More work on triggerResourceControl.
225 * Revision 1.49 1995/06/30 12:39:21 adam
226 * Bug fix: loadFile didn't set record type.
227 * The MARC routines are a little less strict in the interpretation.
228 * Script display.tcl replaces the old marc.tcl.
229 * New interactive script: shell.tcl.
231 * Revision 1.48 1995/06/27 19:03:50 adam
232 * Bug fix in do_present in ir-tcl.c: p->set_child member weren't set.
233 * nextResultSetPosition used instead of setOffset.
235 * Revision 1.47 1995/06/25 10:25:04 adam
236 * Working on triggerResourceControl. Description of compile/install
237 * procedure moved to ir-tcl.sgml.
239 * Revision 1.46 1995/06/22 13:15:06 adam
240 * Feature: SUTRS. Setting getSutrs implemented.
241 * Work on display formats.
242 * Preferred record syntax can be set by the user.
244 * Revision 1.45 1995/06/20 08:07:30 adam
245 * New setting: failInfo.
246 * Working on better cancel mechanism.
248 * Revision 1.44 1995/06/19 17:01:20 adam
251 * Revision 1.43 1995/06/19 13:06:08 adam
252 * New define: IR_TCL_VERSION.
254 * Revision 1.42 1995/06/19 08:08:52 adam
255 * client.tcl: hotTargets now contain both database and target name.
256 * ir-tcl.c: setting protocol edited. Errors in callbacks are logged
257 * by logf(LOG_WARN, ...) calls.
259 * Revision 1.41 1995/06/16 12:28:16 adam
260 * Implemented preferredRecordSyntax.
261 * Minor changes in diagnostic handling.
262 * Record list deleted when connection closes.
264 * Revision 1.40 1995/06/14 13:37:18 adam
265 * Setting recordType implemented.
266 * Setting implementationVersion implemented.
267 * Settings implementationId / implementationName edited.
269 * Revision 1.39 1995/06/08 10:26:32 adam
270 * Bug fix in ir_strdup.
272 * Revision 1.38 1995/06/01 16:36:47 adam
273 * About buttons. Minor bug fixes.
275 * Revision 1.37 1995/06/01 07:31:20 adam
276 * Rename of many typedefs -> IrTcl_...
278 * Revision 1.36 1995/05/31 13:09:59 adam
279 * Client searches/presents may be interrupted.
280 * New moving book-logo.
282 * Revision 1.35 1995/05/31 08:36:33 adam
283 * Bug fix in client.tcl: didn't save options on clientrc.tcl.
284 * New method: referenceId. More work on scan.
286 * Revision 1.34 1995/05/29 10:33:42 adam
287 * README and rename of startup script.
289 * Revision 1.33 1995/05/29 09:15:11 quinn
290 * Changed CS_SR to PROTO_SR, etc.
292 * Revision 1.32 1995/05/29 08:44:16 adam
293 * Work on delete of objects.
295 * Revision 1.31 1995/05/26 11:44:10 adam
296 * Bugs fixed. More work on MARC utilities and queries. Test
297 * client is up-to-date again.
299 * Revision 1.30 1995/05/26 08:54:11 adam
300 * New MARC utilities. Uses prefix query.
302 * Revision 1.29 1995/05/24 14:10:22 adam
303 * Work on idAuthentication, protocolVersion and options.
305 * Revision 1.28 1995/05/23 15:34:48 adam
306 * Many new settings, userInformationField, smallSetUpperBound, etc.
307 * A number of settings are inherited when ir-set is executed.
308 * This version is incompatible with the graphical test client (client.tcl).
310 * Revision 1.27 1995/05/11 15:34:47 adam
311 * Scan request changed a bit. This version works with RLG.
313 * Revision 1.26 1995/04/18 16:11:51 adam
314 * First version of graphical Scan. Some work on query-by-form.
316 * Revision 1.25 1995/04/17 09:37:17 adam
317 * Further development of scan.
319 * Revision 1.24 1995/04/11 14:16:42 adam
320 * Further work on scan. Response works. Entries aren't saved yet.
322 * Revision 1.23 1995/04/10 10:50:27 adam
323 * Result-set name defaults to suffix of ir-set name.
324 * Started working on scan. Not finished at this point.
326 * Revision 1.22 1995/03/31 10:43:03 adam
327 * More robust when getting bad MARC records.
329 * Revision 1.21 1995/03/31 08:56:37 adam
330 * New button "Search".
332 * Revision 1.20 1995/03/29 16:07:09 adam
333 * Bug fix: Didn't use setName in present request.
335 * Revision 1.19 1995/03/28 12:45:23 adam
336 * New ir method failback: called on disconnect/protocol error.
337 * New ir set/get method: protocol: SR / Z3950.
338 * Simple popup and disconnect when failback is invoked.
340 * Revision 1.18 1995/03/21 15:50:12 adam
343 * Revision 1.17 1995/03/21 13:41:03 adam
344 * Comstack cs_create not used too often. Non-blocking connect.
346 * Revision 1.16 1995/03/21 08:26:06 adam
347 * New method, setName, to specify the result set name (other than Default).
348 * New method, responseStatus, which returns diagnostic info, if any, after
349 * present response / search response.
351 * Revision 1.15 1995/03/20 15:24:07 adam
352 * Diagnostic records saved on searchResponse.
354 * Revision 1.14 1995/03/20 08:53:22 adam
355 * Event loop in tclmain.c rewritten. New method searchStatus.
357 * Revision 1.13 1995/03/17 18:26:17 adam
358 * Non-blocking i/o used now. Database names popup as cascade items.
360 * Revision 1.12 1995/03/17 15:45:00 adam
361 * Improved target/database setup.
363 * Revision 1.11 1995/03/16 17:54:03 adam
364 * Minor changes really.
366 * Revision 1.10 1995/03/15 16:14:50 adam
367 * Blocking arg in cs_create changed.
369 * Revision 1.9 1995/03/15 13:59:24 adam
372 * Revision 1.8 1995/03/15 08:25:16 adam
373 * New method presentStatus to check for error on present. Misc. cleanup
374 * of IrTcl_RecordList manipulations. Full MARC record presentation in
377 * Revision 1.7 1995/03/14 17:32:29 adam
378 * Presentation of full Marc record in popup window.
380 * Revision 1.6 1995/03/12 19:31:55 adam
381 * Pattern matching implemented when retrieving MARC records. More
382 * diagnostic functions.
384 * Revision 1.5 1995/03/10 18:00:15 adam
385 * Actual presentation in line-by-line format. RPN query support.
387 * Revision 1.4 1995/03/09 16:15:08 adam
388 * First presentRequest attempts. Hot-target list.
406 #if defined(__WIN32__)
407 # define WIN32_LEAN_AND_MEAN
408 # include <windows.h>
409 # undef WIN32_LEAN_AND_MEAN
412 * VC++ has an alternate entry point called DllMain, so we need to rename
416 # if defined(_MSC_VER)
417 # define EXPORT(a,b) __declspec(dllexport) a b
418 # define DllEntryPoint DllMain
420 # if defined(__BORLANDC__)
421 # define EXPORT(a,b) a _export b
423 # define EXPORT(a,b) a b
427 # define EXPORT(a,b) a b
430 static char *wrongArgs = "wrong # args: should be \"";
432 static int ir_tcl_error_exec (Tcl_Interp *interp, int argc, char **argv)
435 Tcl_AppendResult (interp, " while executing ", NULL);
436 for (i = 0; i<argc; i++)
437 Tcl_AppendResult (interp, (i ? " " : "\""), argv[i], NULL);
438 Tcl_AppendResult (interp, "\"", NULL);
443 static void ir_deleteDiags (IrTcl_Diagnostic **dst_list, int *dst_num);
445 static void ir_select_notify (ClientData clientData, int r, int w, int e);
447 void ir_select_add (int fd, void *obj)
449 ir_tcl_select_set (ir_select_notify, fd, obj, 1, 0, 0);
452 void ir_select_add_write (int fd, void *obj)
454 ir_tcl_select_set (ir_select_notify, fd, obj, 1, 1, 0);
457 void ir_select_remove (int fd, void *obj)
459 ir_tcl_select_set (NULL, fd, obj, 0, 0, 0);
462 void ir_select_remove_write (int fd, void *obj)
464 ir_tcl_select_set (ir_select_notify, fd, obj, 1, 0, 0);
467 static void delete_IR_record (IrTcl_RecordList *rl)
471 case Z_NamePlusRecord_databaseRecord:
472 switch (rl->u.dbrec.type)
475 ir_tcl_grs_del (&rl->u.dbrec.u.grs1);
480 xfree (rl->u.dbrec.buf);
481 rl->u.dbrec.buf = NULL;
483 case Z_NamePlusRecord_surrogateDiagnostic:
484 ir_deleteDiags (&rl->u.surrogateDiagnostics.list,
485 &rl->u.surrogateDiagnostics.num);
488 xfree (rl->elements);
491 static IrTcl_RecordList *new_IR_record (IrTcl_SetObj *setobj,
493 const char *elements)
495 IrTcl_RecordList *rl;
497 if (elements && !*elements)
499 for (rl = setobj->record_list; rl; rl = rl->next)
501 if (no == rl->no && (!rl->elements || !elements ||
502 !strcmp(elements, rl->elements)))
504 delete_IR_record (rl);
510 rl = ir_tcl_malloc (sizeof(*rl));
511 rl->next = setobj->record_list;
513 setobj->record_list = rl;
516 ir_tcl_strdup (NULL, &rl->elements, elements);
523 int ir_tcl_eval (Tcl_Interp *interp, const char *command)
525 char *tmp = ir_tcl_malloc (strlen(command)+1);
528 logf (LOG_DEBUG, "Invoking %.23s ...", command);
529 strcpy (tmp, command);
530 r = Tcl_Eval (interp, tmp);
533 const char *errorInfo = Tcl_GetVar (interp, "errorInfo", 0);
534 logf (LOG_WARN, "Tcl error in line %d: %s\n%s", interp->errorLine,
535 interp->result, errorInfo ? errorInfo : "<null>");
537 Tcl_FreeResult (interp);
543 * IrTcl_getRecordSyntaxStr: Return record syntax name of object id
545 static char *IrTcl_getRecordSyntaxStr (enum oid_value value)
548 struct oident ent, *entp;
550 ent.proto = PROTO_Z3950;
551 ent.oclass = CLASS_RECSYN;
554 o = oid_getoidbyent (&ent);
555 entp = oid_getentbyoid (o);
563 * IrTcl_getRecordSyntaxVal: Return record syntax value of string
565 static enum oid_value IrTcl_getRecordSyntaxVal (const char *name)
567 return oid_getvalbyname (name);
570 static IrTcl_RecordList *find_IR_record (IrTcl_SetObj *setobj, int no)
572 IrTcl_RecordList *rl;
574 for (rl = setobj->record_list; rl; rl = rl->next)
576 (!setobj->recordElements || !rl->elements ||
577 !strcmp (setobj->recordElements, rl->elements)))
582 static void delete_IR_records (IrTcl_SetObj *setobj)
584 IrTcl_RecordList *rl, *rl1;
586 for (rl = setobj->record_list; rl; rl = rl1)
588 delete_IR_record (rl);
592 setobj->record_list = NULL;
596 * ir_tcl_get_set_int: Set/get integer value
598 int ir_tcl_get_set_int (int *val, Tcl_Interp *interp, int argc, char **argv)
604 if (Tcl_GetInt (interp, argv[2], val)==TCL_ERROR)
607 sprintf (buf, "%d", *val);
608 Tcl_AppendResult (interp, buf, NULL);
614 * ir_tcl_method_error
616 int ir_tcl_method_error (Tcl_Interp *interp, int argc, char **argv,
619 IrTcl_Methods *tab_i = tab;
622 Tcl_AppendResult (interp, "bad method: \"", *argv, " ", argv[1],
623 "\"\nmethod should be of:", NULL);
624 for (tab_i = tab; tab_i->tab; tab_i++)
625 for (t = tab_i->tab; t->name; t++)
626 Tcl_AppendResult (interp, " ", t->name, NULL);
631 * ir_tcl_method: Search for method in table and invoke method handler
633 int ir_tcl_method (Tcl_Interp *interp, int argc, char **argv,
634 IrTcl_Methods *tab, int *ret)
636 IrTcl_Methods *tab_i = tab;
639 for (tab_i = tab; tab_i->tab; tab_i++)
640 for (t = tab_i->tab; t->name; t++)
643 if ((*t->method)(tab_i->obj, interp, argc, argv) == TCL_ERROR)
647 if (!strcmp (t->name, argv[1]))
649 *ret = (*t->method)(tab_i->obj, interp, argc, argv);
660 * ir_tcl_named_bits: get/set named bits
662 int ir_tcl_named_bits (struct ir_named_entry *tab, Odr_bitmask *ob,
663 Tcl_Interp *interp, int argc, char **argv)
665 struct ir_named_entry *ti;
670 for (no = 0; no < argc; no++)
673 for (ti = tab; ti->name; ti++)
674 if (!strcmp(argv[no], "@all") || !strcmp (argv[no], ti->name))
676 ODR_MASK_SET (ob, ti->pos);
681 Tcl_AppendResult (interp, "bad bit mask ", argv[no], NULL);
682 return ir_tcl_error_exec (interp, argc, argv);
687 for (ti = tab; ti->name; ti++)
688 if (ODR_MASK_GET (ob, ti->pos))
689 Tcl_AppendElement (interp, ti->name);
693 static void set_referenceId (ODR o, Z_ReferenceId **dst, const char *src)
699 *dst = odr_malloc (o, sizeof(**dst));
700 (*dst)->size = (*dst)->len = strlen(src);
701 (*dst)->buf = odr_malloc (o, (*dst)->len);
702 memcpy ((*dst)->buf, src, (*dst)->len);
706 static void get_referenceId (char **dst, Z_ReferenceId *src)
714 *dst = ir_tcl_malloc (src->len+1);
715 memcpy (*dst, src->buf, src->len);
716 (*dst)[src->len] = '\0';
719 /* ------------------------------------------------------- */
722 * do_init_request: init method on IR object
724 static int do_init_request (void *obj, Tcl_Interp *interp,
725 int argc, char **argv)
733 logf (LOG_DEBUG, "init %s", *argv);
736 Tcl_AppendResult (interp, "not connected", NULL);
737 return ir_tcl_error_exec (interp, argc, argv);
739 apdu = zget_APDU (p->odr_out, Z_APDU_initRequest);
740 req = apdu->u.initRequest;
742 set_referenceId (p->odr_out, &req->referenceId, p->set_inher.referenceId);
743 req->options = &p->options;
744 req->protocolVersion = &p->protocolVersion;
745 req->preferredMessageSize = &p->preferredMessageSize;
746 req->maximumRecordSize = &p->maximumRecordSize;
748 if (p->idAuthenticationGroupId)
750 Z_IdPass *pass = odr_malloc (p->odr_out, sizeof(*pass));
751 Z_IdAuthentication *auth = odr_malloc (p->odr_out, sizeof(*auth));
753 logf (LOG_DEBUG, "using pass authentication");
755 auth->which = Z_IdAuthentication_idPass;
756 auth->u.idPass = pass;
757 if (p->idAuthenticationGroupId && *p->idAuthenticationGroupId)
758 pass->groupId = p->idAuthenticationGroupId;
760 pass->groupId = NULL;
761 if (p->idAuthenticationUserId && *p->idAuthenticationUserId)
762 pass->userId = p->idAuthenticationUserId;
765 if (p->idAuthenticationPassword && *p->idAuthenticationPassword)
766 pass->password = p->idAuthenticationPassword;
768 pass->password = NULL;
769 req->idAuthentication = auth;
771 else if (!p->idAuthenticationOpen || !*p->idAuthenticationOpen)
772 req->idAuthentication = NULL;
775 Z_IdAuthentication *auth = odr_malloc (p->odr_out, sizeof(*auth));
777 logf (LOG_DEBUG, "using open authentication");
778 auth->which = Z_IdAuthentication_open;
779 auth->u.open = p->idAuthenticationOpen;
780 req->idAuthentication = auth;
782 req->implementationId = p->implementationId;
783 req->implementationName = p->implementationName;
784 req->implementationVersion = p->implementationVersion;
785 req->userInformationField = 0;
787 return ir_tcl_send_APDU (interp, p, apdu, "init", *argv);
791 * do_protocolVersion: Set protocol Version
793 static int do_protocolVersion (void *obj, Tcl_Interp *interp,
794 int argc, char **argv)
802 ODR_MASK_ZERO (&p->protocolVersion);
803 ODR_MASK_SET (&p->protocolVersion, 0);
804 ODR_MASK_SET (&p->protocolVersion, 1);
809 if (Tcl_GetInt (interp, argv[2], &version)==TCL_ERROR)
811 ODR_MASK_ZERO (&p->protocolVersion);
812 for (i = 0; i<version; i++)
813 ODR_MASK_SET (&p->protocolVersion, i);
815 for (i = 4; --i >= 0; )
816 if (ODR_MASK_GET (&p->protocolVersion, i))
818 sprintf (buf, "%d", i+1);
819 interp->result = buf;
824 * do_options: Set options
826 static int do_options (void *obj, Tcl_Interp *interp,
827 int argc, char **argv)
829 static struct ir_named_entry options_tab[] = {
833 { "resourceReport", 3 },
834 { "triggerResourceCtrl", 4},
835 { "resourceCtrl", 5},
839 { "extendedServices", 10},
840 { "level-1Segmentation", 11},
841 { "level-2Segmentation", 12},
842 { "concurrentOperations", 13},
843 { "namedResultSets", 14},
850 ODR_MASK_ZERO (&p->options);
851 ODR_MASK_SET (&p->options, 0);
852 ODR_MASK_SET (&p->options, 1);
853 ODR_MASK_SET (&p->options, 4);
854 ODR_MASK_SET (&p->options, 7);
855 ODR_MASK_SET (&p->options, 14);
858 return ir_tcl_named_bits (options_tab, &p->options, interp, argc-2, argv+2);
862 * do_apduInfo: Get APDU information
864 static int do_apduInfo (void *obj, Tcl_Interp *interp, int argc, char **argv)
872 sprintf (buf, "%d", p->apduLen);
873 Tcl_AppendElement (interp, buf);
874 sprintf (buf, "%d", p->apduOffset);
875 Tcl_AppendElement (interp, buf);
878 Tcl_AppendElement (interp, "");
881 apduf = fopen ("apdu.tmp", "w");
884 Tcl_AppendElement (interp, "");
887 odr_dumpBER (apduf, p->buf_in, p->apduLen);
889 if (!(apduf = fopen ("apdu.tmp", "r")))
890 Tcl_AppendElement (interp, "");
895 Tcl_AppendResult (interp, " {", NULL);
896 while ((c = getc (apduf)) != EOF)
900 Tcl_AppendResult (interp, buf, NULL);
903 Tcl_AppendResult (interp, "}", NULL);
910 * do_failInfo: Get fail information
912 static int do_failInfo (void *obj, Tcl_Interp *interp, int argc, char **argv)
922 sprintf (buf, "%d", p->failInfo);
928 case IR_TCL_FAIL_CONNECT:
929 cp = "connect failed";
931 case IR_TCL_FAIL_READ:
932 cp = "connection closed";
934 case IR_TCL_FAIL_WRITE:
935 cp = "connection closed";
937 case IR_TCL_FAIL_IN_APDU:
938 cp = "failed to decode incoming APDU";
940 case IR_TCL_FAIL_UNKNOWN_APDU:
946 Tcl_AppendElement (interp, buf);
947 Tcl_AppendElement (interp, cp);
952 * do_preferredMessageSize: Set/get preferred message size
954 static int do_preferredMessageSize (void *obj, Tcl_Interp *interp,
955 int argc, char **argv)
961 p->preferredMessageSize = 30000;
964 return ir_tcl_get_set_int (&p->preferredMessageSize, interp, argc, argv);
968 * do_maximumRecordSize: Set/get maximum record size
970 static int do_maximumRecordSize (void *obj, Tcl_Interp *interp,
971 int argc, char **argv)
977 p->maximumRecordSize = 30000;
980 return ir_tcl_get_set_int (&p->maximumRecordSize, interp, argc, argv);
984 * do_initResult: Get init result
986 static int do_initResult (void *obj, Tcl_Interp *interp,
987 int argc, char **argv)
993 return ir_tcl_get_set_int (&p->initResult, interp, argc, argv);
998 * do_implementationName: Set/get Implementation Name.
1000 static int do_implementationName (void *obj, Tcl_Interp *interp,
1001 int argc, char **argv)
1006 return ir_tcl_strdup (interp, &p->implementationName,
1007 "Index Data/IrTcl on YAZ");
1008 else if (argc == -1)
1009 return ir_tcl_strdel (interp, &p->implementationName);
1012 xfree (p->implementationName);
1013 if (ir_tcl_strdup (interp, &p->implementationName, argv[2])
1017 Tcl_AppendResult (interp, p->implementationName, (char*) NULL);
1022 * do_implementationId: Get Implementation Id.
1024 static int do_implementationId (void *obj, Tcl_Interp *interp,
1025 int argc, char **argv)
1030 return ir_tcl_strdup (interp, &p->implementationId, "YAZ (id=81)");
1031 else if (argc == -1)
1032 return ir_tcl_strdel (interp, &p->implementationId);
1033 Tcl_AppendResult (interp, p->implementationId, (char*) NULL);
1038 * do_implementationVersion: get Implementation Version.
1040 static int do_implementationVersion (void *obj, Tcl_Interp *interp,
1041 int argc, char **argv)
1046 return ir_tcl_strdup (interp, &p->implementationVersion,
1048 #ifdef IR_TCL_VERSION
1049 " / Irtcl: " IR_TCL_VERSION
1052 else if (argc == -1)
1053 return ir_tcl_strdel (interp, &p->implementationVersion);
1054 Tcl_AppendResult (interp, p->implementationVersion, (char*) NULL);
1059 * do_targetImplementationName: Get Implementation Name of target.
1061 static int do_targetImplementationName (void *obj, Tcl_Interp *interp,
1062 int argc, char **argv)
1068 p->targetImplementationName = NULL;
1071 else if (argc == -1)
1072 return ir_tcl_strdel (interp, &p->targetImplementationName);
1073 Tcl_AppendResult (interp, p->targetImplementationName, (char*) NULL);
1078 * do_targetImplementationId: Get Implementation Id of target
1080 static int do_targetImplementationId (void *obj, Tcl_Interp *interp,
1081 int argc, char **argv)
1087 p->targetImplementationId = NULL;
1090 else if (argc == -1)
1091 return ir_tcl_strdel (interp, &p->targetImplementationId);
1092 Tcl_AppendResult (interp, p->targetImplementationId, (char*) NULL);
1097 * do_targetImplementationVersion: Get Implementation Version of target
1099 static int do_targetImplementationVersion (void *obj, Tcl_Interp *interp,
1100 int argc, char **argv)
1106 p->targetImplementationVersion = NULL;
1109 else if (argc == -1)
1110 return ir_tcl_strdel (interp, &p->targetImplementationVersion);
1111 Tcl_AppendResult (interp, p->targetImplementationVersion, (char*) NULL);
1116 * do_idAuthentication: Set/get id Authentication
1118 static int do_idAuthentication (void *obj, Tcl_Interp *interp,
1119 int argc, char **argv)
1123 if (argc >= 3 || argc == -1)
1125 xfree (p->idAuthenticationOpen);
1126 xfree (p->idAuthenticationGroupId);
1127 xfree (p->idAuthenticationUserId);
1128 xfree (p->idAuthenticationPassword);
1130 if (argc >= 3 || argc <= 0)
1132 p->idAuthenticationOpen = NULL;
1133 p->idAuthenticationGroupId = NULL;
1134 p->idAuthenticationUserId = NULL;
1135 p->idAuthenticationPassword = NULL;
1144 ir_tcl_strdup (interp, &p->idAuthenticationOpen, argv[2])
1151 ir_tcl_strdup (interp, &p->idAuthenticationGroupId, argv[2])
1155 ir_tcl_strdup (interp, &p->idAuthenticationUserId, argv[3])
1159 ir_tcl_strdup (interp, &p->idAuthenticationPassword, argv[4])
1164 if (p->idAuthenticationOpen)
1165 Tcl_AppendElement (interp, p->idAuthenticationOpen);
1166 else if (p->idAuthenticationGroupId)
1168 Tcl_AppendElement (interp, p->idAuthenticationGroupId);
1169 Tcl_AppendElement (interp, p->idAuthenticationUserId);
1170 Tcl_AppendElement (interp, p->idAuthenticationPassword);
1176 * do_connect: connect method on IR object
1178 static int do_connect (void *obj, Tcl_Interp *interp,
1179 int argc, char **argv)
1189 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
1190 " ?hostname?\"", NULL);
1195 Tcl_AppendResult (interp, p->hostname, NULL);
1199 logf (LOG_DEBUG, "connect %s %s", *argv, argv[2]);
1202 Tcl_AppendResult (interp, "already connected", NULL);
1203 return ir_tcl_error_exec (interp, argc, argv);
1205 if (!strcmp (p->comstackType, "tcpip"))
1207 p->cs_link = cs_create (tcpip_type, CS_BLOCK, p->protocol_type);
1208 logf (LOG_DEBUG, "tcp/ip connect %s", argv[2]);
1210 else if (!strcmp (p->comstackType, "mosi"))
1213 p->cs_link = cs_create (mosi_type, CS_BLOCK, p->protocol_type);
1214 logf (LOG_DEBUG, "mosi connect %s", argv[2]);
1216 Tcl_AppendResult (interp, "mosi not supported", NULL);
1217 return ir_tcl_error_exec (interp, argc, argv);
1222 Tcl_AppendResult (interp, "bad comstack type ",
1223 p->comstackType, NULL);
1224 return ir_tcl_error_exec (interp, argc, argv);
1226 if (ir_tcl_strdup (interp, &p->hostname, argv[2]) == TCL_ERROR)
1228 p->eventType = "connect";
1229 addr = cs_straddr (p->cs_link, argv[2]);
1232 ir_tcl_disconnect (p);
1233 Tcl_AppendResult (interp, "cs_straddr fail", NULL);
1234 return ir_tcl_error_exec (interp, argc, argv);
1236 if ((r=cs_connect (p->cs_link, addr)) < 0)
1238 ir_tcl_disconnect (p);
1239 Tcl_AppendResult (interp, "connect fail", NULL);
1240 return ir_tcl_error_exec (interp, argc, argv);
1242 ir_select_add (cs_fileno (p->cs_link), p);
1245 logf (LOG_DEBUG, "connect pending fd=%d", cs_fileno(p->cs_link));
1246 ir_select_add_write (cs_fileno (p->cs_link), p);
1247 p->state = IR_TCL_R_Connecting;
1251 logf (LOG_DEBUG, "connect ok fd=%d", cs_fileno(p->cs_link));
1252 p->state = IR_TCL_R_Idle;
1254 ir_tcl_eval (p->interp, p->callback);
1261 * ir_tcl_disconnect: close connection
1263 void ir_tcl_disconnect (IrTcl_Obj *p)
1267 logf(LOG_DEBUG, "Closing connection to %s", p->hostname);
1268 xfree (p->hostname);
1270 assert (p->cs_link);
1271 ir_select_remove (cs_fileno (p->cs_link), p);
1273 odr_reset (p->odr_in);
1275 #if TCL_MAJOR_VERSION == 8
1276 cs_fileno(p->cs_link) = -1;
1278 cs_close (p->cs_link);
1281 ODR_MASK_ZERO (&p->options);
1282 ODR_MASK_SET (&p->options, 0);
1283 ODR_MASK_SET (&p->options, 1);
1284 ODR_MASK_SET (&p->options, 4);
1285 ODR_MASK_SET (&p->options, 7);
1286 ODR_MASK_SET (&p->options, 14);
1288 ODR_MASK_ZERO (&p->protocolVersion);
1289 ODR_MASK_SET (&p->protocolVersion, 0);
1290 ODR_MASK_SET (&p->protocolVersion, 1);
1293 assert (!p->cs_link);
1297 * do_disconnect: disconnect method on IR object
1299 static int do_disconnect (void *obj, Tcl_Interp *interp,
1300 int argc, char **argv)
1306 p->state = IR_TCL_R_Idle;
1307 p->eventType = NULL;
1312 ir_tcl_disconnect (p);
1317 * do_comstack: Set/get comstack method on IR object
1319 static int do_comstack (void *o, Tcl_Interp *interp,
1320 int argc, char **argv)
1325 return ir_tcl_strdup (interp, &obj->comstackType, "tcpip");
1326 else if (argc == -1)
1327 return ir_tcl_strdel (interp, &obj->comstackType);
1330 xfree (obj->comstackType);
1331 if (ir_tcl_strdup (interp, &obj->comstackType, argv[2]) == TCL_ERROR)
1334 Tcl_AppendElement (interp, obj->comstackType);
1339 * do_logLevel: Set log level
1341 static int do_logLevel (void *o, Tcl_Interp *interp,
1342 int argc, char **argv)
1347 log_init (log_mask_str (argv[2]), "", NULL);
1349 log_init (log_mask_str (argv[2]), argv[3], NULL);
1351 log_init (log_mask_str (argv[2]), argv[3], argv[4]);
1357 * do_eventType: Return type of last event
1359 static int do_eventType (void *obj, Tcl_Interp *interp,
1360 int argc, char **argv)
1366 p->eventType = NULL;
1369 Tcl_AppendElement (interp, p->eventType ? p->eventType : "");
1375 * do_callback: add callback
1377 static int do_callback (void *obj, Tcl_Interp *interp,
1378 int argc, char **argv)
1387 else if (argc == -1)
1388 return ir_tcl_strdel (interp, &p->callback);
1391 xfree (p->callback);
1394 if (ir_tcl_strdup (interp, &p->callback, argv[2]) == TCL_ERROR)
1404 * do_failback: add error handle callback
1406 static int do_failback (void *obj, Tcl_Interp *interp,
1407 int argc, char **argv)
1416 else if (argc == -1)
1417 return ir_tcl_strdel (interp, &p->failback);
1420 xfree (p->failback);
1423 if (ir_tcl_strdup (interp, &p->failback, argv[2]) == TCL_ERROR)
1433 * do_initResponse: add init response handler
1435 static int do_initResponse (void *obj, Tcl_Interp *interp,
1436 int argc, char **argv)
1442 p->initResponse = NULL;
1445 else if (argc == -1)
1446 return ir_tcl_strdel (interp, &p->initResponse);
1449 xfree (p->initResponse);
1452 if (ir_tcl_strdup (interp, &p->initResponse, argv[2]) == TCL_ERROR)
1456 p->initResponse = NULL;
1461 * do_protocol: Set/get protocol method on IR object
1463 static int do_protocol (void *o, Tcl_Interp *interp, int argc, char **argv)
1469 p->protocol_type = PROTO_Z3950;
1474 if (!strcmp (argv[2], "Z39"))
1475 p->protocol_type = PROTO_Z3950;
1476 else if (!strcmp (argv[2], "SR"))
1477 p->protocol_type = PROTO_SR;
1480 Tcl_AppendResult (interp, "bad protocol ", argv[2], NULL);
1481 return ir_tcl_error_exec (interp, argc, argv);
1485 switch (p->protocol_type)
1488 Tcl_AppendElement (interp, "Z39");
1491 Tcl_AppendElement (interp, "SR");
1498 * do_triggerResourceControl:
1500 static int do_triggerResourceControl (void *obj, Tcl_Interp *interp,
1501 int argc, char **argv)
1505 Z_TriggerResourceControlRequest *req;
1506 bool_t is_false = 0;
1512 Tcl_AppendResult (interp, "not connected", NULL);
1513 return ir_tcl_error_exec (interp, argc, argv);
1515 apdu = zget_APDU (p->odr_out, Z_APDU_triggerResourceControlRequest);
1516 req = apdu->u.triggerResourceControlRequest;
1517 *req->requestedAction = Z_TriggerResourceCtrl_cancel;
1518 req->resultSetWanted = &is_false;
1520 return ir_tcl_send_APDU (interp, p, apdu, "triggerResourceControl",
1525 * do_databaseNames: specify database names
1527 static int do_databaseNames (void *obj, Tcl_Interp *interp,
1528 int argc, char **argv)
1531 IrTcl_SetCObj *p = obj;
1535 for (i=0; i<p->num_databaseNames; i++)
1536 xfree (p->databaseNames[i]);
1537 xfree (p->databaseNames);
1541 p->num_databaseNames = 0;
1542 p->databaseNames = NULL;
1547 for (i=0; i<p->num_databaseNames; i++)
1548 Tcl_AppendElement (interp, p->databaseNames[i]);
1551 if (p->databaseNames)
1553 for (i=0; i<p->num_databaseNames; i++)
1554 xfree (p->databaseNames[i]);
1555 xfree (p->databaseNames);
1557 p->num_databaseNames = argc - 2;
1559 ir_tcl_malloc (sizeof(*p->databaseNames) * (1+p->num_databaseNames));
1560 for (i=0; i<p->num_databaseNames; i++)
1562 if (ir_tcl_strdup (interp, &p->databaseNames[i], argv[2+i])
1566 p->databaseNames[i] = NULL;
1571 * do_replaceIndicator: Set/get replace Set indicator
1573 static int do_replaceIndicator (void *obj, Tcl_Interp *interp,
1574 int argc, char **argv)
1576 IrTcl_SetCObj *p = obj;
1580 p->replaceIndicator = 1;
1583 return ir_tcl_get_set_int (&p->replaceIndicator, interp, argc, argv);
1587 * do_queryType: Set/Get query method
1589 static int do_queryType (void *obj, Tcl_Interp *interp,
1590 int argc, char **argv)
1592 IrTcl_SetCObj *p = obj;
1595 return ir_tcl_strdup (interp, &p->queryType, "rpn");
1596 else if (argc == -1)
1597 return ir_tcl_strdel (interp, &p->queryType);
1600 xfree (p->queryType);
1601 if (ir_tcl_strdup (interp, &p->queryType, argv[2]) == TCL_ERROR)
1604 Tcl_AppendResult (interp, p->queryType, NULL);
1609 * do_userInformationField: Get User information field
1611 static int do_userInformationField (void *obj, Tcl_Interp *interp,
1612 int argc, char **argv)
1618 p->userInformationField = NULL;
1621 else if (argc == -1)
1622 return ir_tcl_strdel (interp, &p->userInformationField);
1623 Tcl_AppendResult (interp, p->userInformationField, NULL);
1628 * do_smallSetUpperBound: Set/get small set upper bound
1630 static int do_smallSetUpperBound (void *o, Tcl_Interp *interp,
1631 int argc, char **argv)
1633 IrTcl_SetCObj *p = o;
1637 p->smallSetUpperBound = 0;
1640 return ir_tcl_get_set_int (&p->smallSetUpperBound, interp, argc, argv);
1644 * do_largeSetLowerBound: Set/get large set lower bound
1646 static int do_largeSetLowerBound (void *o, Tcl_Interp *interp,
1647 int argc, char **argv)
1649 IrTcl_SetCObj *p = o;
1653 p->largeSetLowerBound = 2;
1656 return ir_tcl_get_set_int (&p->largeSetLowerBound, interp, argc, argv);
1660 * do_mediumSetPresentNumber: Set/get large set lower bound
1662 static int do_mediumSetPresentNumber (void *o, Tcl_Interp *interp,
1663 int argc, char **argv)
1665 IrTcl_SetCObj *p = o;
1669 p->mediumSetPresentNumber = 0;
1672 return ir_tcl_get_set_int (&p->mediumSetPresentNumber, interp, argc, argv);
1676 * do_referenceId: Set/Get referenceId
1678 static int do_referenceId (void *obj, Tcl_Interp *interp,
1679 int argc, char **argv)
1681 IrTcl_SetCObj *p = obj;
1685 p->referenceId = NULL;
1688 else if (argc == -1)
1689 return ir_tcl_strdel (interp, &p->referenceId);
1692 xfree (p->referenceId);
1693 if (ir_tcl_strdup (interp, &p->referenceId, argv[2]) == TCL_ERROR)
1696 Tcl_AppendResult (interp, p->referenceId, NULL);
1701 * do_preferredRecordSyntax: Set/get preferred record syntax
1703 static int do_preferredRecordSyntax (void *obj, Tcl_Interp *interp,
1704 int argc, char **argv)
1706 IrTcl_SetCObj *p = obj;
1710 p->preferredRecordSyntax = NULL;
1713 else if (argc == -1)
1715 xfree (p->preferredRecordSyntax);
1716 p->preferredRecordSyntax = NULL;
1721 xfree (p->preferredRecordSyntax);
1722 p->preferredRecordSyntax = NULL;
1723 if (argv[2][0] && (p->preferredRecordSyntax =
1724 ir_tcl_malloc (sizeof(*p->preferredRecordSyntax))))
1725 *p->preferredRecordSyntax = IrTcl_getRecordSyntaxVal (argv[2]);
1730 (interp,!p->preferredRecordSyntax ? "" :
1731 IrTcl_getRecordSyntaxStr(*p->preferredRecordSyntax));
1738 * do_elementSetNames: Set/Get element Set Names
1740 static int do_elementSetNames (void *obj, Tcl_Interp *interp,
1741 int argc, char **argv)
1743 IrTcl_SetCObj *p = obj;
1747 p->elementSetNames = NULL;
1750 else if (argc == -1)
1751 return ir_tcl_strdel (interp, &p->elementSetNames);
1754 xfree (p->elementSetNames);
1755 if (ir_tcl_strdup (interp, &p->elementSetNames, argv[2]) == TCL_ERROR)
1758 Tcl_AppendResult (interp, p->elementSetNames, NULL);
1763 * do_smallSetElementSetNames: Set/Get small Set Element Set Names
1765 static int do_smallSetElementSetNames (void *obj, Tcl_Interp *interp,
1766 int argc, char **argv)
1768 IrTcl_SetCObj *p = obj;
1772 p->smallSetElementSetNames = NULL;
1775 else if (argc == -1)
1776 return ir_tcl_strdel (interp, &p->smallSetElementSetNames);
1779 xfree (p->smallSetElementSetNames);
1780 if (ir_tcl_strdup (interp, &p->smallSetElementSetNames,
1781 argv[2]) == TCL_ERROR)
1784 Tcl_AppendResult (interp, p->smallSetElementSetNames, NULL);
1789 * do_mediumSetElementSetNames: Set/Get medium Set Element Set Names
1791 static int do_mediumSetElementSetNames (void *obj, Tcl_Interp *interp,
1792 int argc, char **argv)
1794 IrTcl_SetCObj *p = obj;
1798 p->mediumSetElementSetNames = NULL;
1801 else if (argc == -1)
1802 return ir_tcl_strdel (interp, &p->mediumSetElementSetNames);
1805 xfree (p->mediumSetElementSetNames);
1806 if (ir_tcl_strdup (interp, &p->mediumSetElementSetNames,
1807 argv[2]) == TCL_ERROR)
1810 Tcl_AppendResult (interp, p->mediumSetElementSetNames, NULL);
1814 static IrTcl_Method ir_method_tab[] = {
1815 { "comstack", do_comstack, NULL },
1816 { "protocol", do_protocol, NULL },
1817 { "failback", do_failback, NULL },
1818 { "failInfo", do_failInfo, NULL },
1819 { "apduInfo", do_apduInfo, NULL },
1820 { "logLevel", do_logLevel, NULL },
1822 { "eventType", do_eventType, NULL },
1823 { "connect", do_connect, NULL },
1824 { "protocolVersion", do_protocolVersion, NULL },
1825 { "preferredMessageSize", do_preferredMessageSize, NULL },
1826 { "maximumRecordSize", do_maximumRecordSize, NULL },
1827 { "implementationName", do_implementationName, NULL },
1828 { "implementationId", do_implementationId, NULL },
1829 { "implementationVersion", do_implementationVersion, NULL },
1830 { "targetImplementationName", do_targetImplementationName, NULL },
1831 { "targetImplementationId", do_targetImplementationId, NULL },
1832 { "targetImplementationVersion", do_targetImplementationVersion, NULL},
1833 { "userInformationField", do_userInformationField, NULL},
1834 { "idAuthentication", do_idAuthentication, NULL},
1835 { "options", do_options, NULL},
1836 { "init", do_init_request, NULL},
1837 { "initResult", do_initResult, NULL},
1838 { "disconnect", do_disconnect, NULL},
1839 { "callback", do_callback, NULL},
1840 { "initResponse", do_initResponse, NULL},
1841 { "triggerResourceControl", do_triggerResourceControl, NULL},
1842 { "initResponse", do_initResponse, NULL},
1846 static IrTcl_Method ir_set_c_method_tab[] = {
1847 { "databaseNames", do_databaseNames, NULL},
1848 { "replaceIndicator", do_replaceIndicator, NULL},
1849 { "queryType", do_queryType, NULL},
1850 { "preferredRecordSyntax", do_preferredRecordSyntax, NULL},
1851 { "smallSetUpperBound", do_smallSetUpperBound, NULL},
1852 { "largeSetLowerBound", do_largeSetLowerBound, NULL},
1853 { "mediumSetPresentNumber", do_mediumSetPresentNumber, NULL},
1854 { "referenceId", do_referenceId, NULL},
1855 { "elementSetNames", do_elementSetNames, NULL},
1856 { "smallSetElementSetNames", do_smallSetElementSetNames, NULL},
1857 { "mediumSetElementSetNames", do_mediumSetElementSetNames, NULL},
1862 * ir_obj_method: IR Object methods
1864 static int ir_obj_method (ClientData clientData, Tcl_Interp *interp,
1865 int argc, char **argv)
1867 IrTcl_Methods tab[3];
1868 IrTcl_Obj *p = (IrTcl_Obj *) clientData;
1873 Tcl_AppendResult (interp, wrongArgs, *argv, "method args...\"", NULL);
1877 tab[0].tab = ir_method_tab;
1879 tab[1].tab = ir_set_c_method_tab;
1880 tab[1].obj = &p->set_inher;
1883 if (ir_tcl_method (interp, argc, argv, tab, &r) == TCL_ERROR)
1884 return ir_tcl_method_error (interp, argc, argv, tab);
1889 * ir_obj_delete: IR Object disposal
1891 static void ir_obj_delete (ClientData clientData)
1893 IrTcl_Obj *obj = (IrTcl_Obj *) clientData;
1894 IrTcl_Methods tab[3];
1897 if (obj->ref_count > 0)
1899 assert (obj->ref_count == 0);
1901 logf (LOG_DEBUG, "ir object delete");
1902 tab[0].tab = ir_method_tab;
1904 tab[1].tab = ir_set_c_method_tab;
1905 tab[1].obj = &obj->set_inher;
1908 ir_tcl_method (NULL, -1, NULL, tab, NULL);
1911 odr_destroy (obj->odr_in);
1912 odr_destroy (obj->odr_out);
1913 odr_destroy (obj->odr_pr);
1918 * ir_obj_init: IR Object initialization
1920 int ir_obj_init (ClientData clientData, Tcl_Interp *interp,
1921 int argc, char **argv, ClientData *subData,
1922 ClientData parentData)
1924 IrTcl_Methods tab[3];
1932 Tcl_AppendResult (interp, wrongArgs, *argv, " objName\"", NULL);
1935 obj = ir_tcl_malloc (sizeof(*obj));
1938 obj->bibset = ccl_qual_mk ();
1939 if ((inf = fopen ("default.bib", "r")))
1941 ccl_qual_file (obj->bibset, inf);
1946 logf (LOG_DEBUG, "ir object create %s", argv[1]);
1947 obj->odr_in = odr_createmem (ODR_DECODE);
1948 odr_choice_enable_bias (obj->odr_in, 0);
1949 obj->odr_out = odr_createmem (ODR_ENCODE);
1950 obj->odr_pr = odr_createmem (ODR_PRINT);
1951 obj->state = IR_TCL_R_Idle;
1952 obj->interp = interp;
1956 obj->request_queue = NULL;
1958 tab[0].tab = ir_method_tab;
1960 tab[1].tab = ir_set_c_method_tab;
1961 tab[1].obj = &obj->set_inher;
1964 if (ir_tcl_method (interp, 0, NULL, tab, NULL) == TCL_ERROR)
1966 Tcl_AppendResult (interp, "Failed to initialize ", argv[1], NULL);
1969 *subData = (ClientData) obj;
1975 * ir_obj_mk: IR Object creation
1977 static int ir_obj_mk (ClientData clientData, Tcl_Interp *interp,
1978 int argc, char **argv)
1981 int r = ir_obj_init (clientData, interp, argc, argv, &subData, 0);
1985 Tcl_CreateCommand (interp, argv[1], ir_obj_method,
1986 subData, ir_obj_delete);
1990 IrTcl_Class ir_obj_class = {
1998 /* ------------------------------------------------------- */
2000 * do_search: Do search request
2002 static int do_search (void *o, Tcl_Interp *interp, int argc, char **argv)
2004 Z_SearchRequest *req;
2008 IrTcl_SetObj *obj = o;
2019 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1], "query\"",
2023 logf (LOG_DEBUG, "search %s %s", *argv, argv[2]);
2024 if (!obj->set_inher.num_databaseNames)
2026 Tcl_AppendResult (interp, "no databaseNames", NULL);
2027 return ir_tcl_error_exec (interp, argc, argv);
2031 Tcl_AppendResult (interp, "not connected", NULL);
2032 return ir_tcl_error_exec (interp, argc, argv);
2034 apdu = zget_APDU (p->odr_out, Z_APDU_searchRequest);
2035 req = apdu->u.searchRequest;
2039 set_referenceId (p->odr_out, &req->referenceId,
2040 obj->set_inher.referenceId);
2042 req->smallSetUpperBound = &obj->set_inher.smallSetUpperBound;
2043 req->largeSetLowerBound = &obj->set_inher.largeSetLowerBound;
2044 req->mediumSetPresentNumber = &obj->set_inher.mediumSetPresentNumber;
2045 req->replaceIndicator = &obj->set_inher.replaceIndicator;
2046 req->resultSetName = obj->setName ? obj->setName : "default";
2047 logf (LOG_DEBUG, "Search, resultSetName %s", req->resultSetName);
2048 req->num_databaseNames = obj->set_inher.num_databaseNames;
2049 req->databaseNames = obj->set_inher.databaseNames;
2050 for (r=0; r < obj->set_inher.num_databaseNames; r++)
2051 logf (LOG_DEBUG, " Database %s", obj->set_inher.databaseNames[r]);
2052 if (obj->set_inher.preferredRecordSyntax)
2054 struct oident ident;
2056 ident.proto = p->protocol_type;
2057 ident.oclass = CLASS_RECSYN;
2058 ident.value = *obj->set_inher.preferredRecordSyntax;
2059 logf (LOG_DEBUG, "Preferred record syntax is %d", ident.value);
2060 req->preferredRecordSyntax = odr_oiddup (p->odr_out,
2061 oid_getoidbyent (&ident));
2064 req->preferredRecordSyntax = 0;
2066 if (obj->set_inher.smallSetElementSetNames &&
2067 *obj->set_inher.smallSetElementSetNames)
2069 Z_ElementSetNames *esn = odr_malloc (p->odr_out, sizeof(*esn));
2071 esn->which = Z_ElementSetNames_generic;
2072 esn->u.generic = obj->set_inher.smallSetElementSetNames;
2073 req->smallSetElementSetNames = esn;
2076 req->smallSetElementSetNames = NULL;
2078 if (obj->set_inher.mediumSetElementSetNames &&
2079 *obj->set_inher.mediumSetElementSetNames)
2081 Z_ElementSetNames *esn = odr_malloc (p->odr_out, sizeof(*esn));
2083 esn->which = Z_ElementSetNames_generic;
2084 esn->u.generic = obj->set_inher.mediumSetElementSetNames;
2085 req->mediumSetElementSetNames = esn;
2088 req->mediumSetElementSetNames = NULL;
2090 req->query = &query;
2092 logf (LOG_DEBUG, "queryType %s", obj->set_inher.queryType);
2093 if (!strcmp (obj->set_inher.queryType, "rpn"))
2095 Z_RPNQuery *RPNquery;
2097 RPNquery = p_query_rpn (p->odr_out, p->protocol_type, argv[2]);
2100 Tcl_AppendResult (interp, "query syntax error", NULL);
2101 return ir_tcl_error_exec (interp, argc, argv);
2103 query.which = Z_Query_type_1;
2104 query.u.type_1 = RPNquery;
2107 else if (!strcmp (obj->set_inher.queryType, "cclrpn"))
2111 struct ccl_rpn_node *rpn;
2112 Z_RPNQuery *RPNquery;
2115 bib1.proto = p->protocol_type;
2116 bib1.oclass = CLASS_ATTSET;
2117 bib1.value = VAL_BIB1;
2119 rpn = ccl_find_str(p->bibset, argv[2], &error, &pos);
2122 Tcl_AppendResult (interp, "ccl syntax error ", ccl_err_msg(error),
2124 return ir_tcl_error_exec (interp, argc, argv);
2127 ccl_pr_tree (rpn, stderr);
2128 fprintf (stderr, "\n");
2130 assert((RPNquery = ccl_rpn_query(rpn)));
2131 RPNquery->attributeSetId = oid_getoidbyent (&bib1);
2132 query.which = Z_Query_type_1;
2133 query.u.type_1 = RPNquery;
2136 else if (!strcmp (obj->set_inher.queryType, "ccl"))
2138 query.which = Z_Query_type_2;
2139 query.u.type_2 = &ccl_query;
2140 ccl_query.buf = (unsigned char *) argv[2];
2141 ccl_query.len = strlen (argv[2]);
2145 Tcl_AppendResult (interp, "invalid query method ",
2146 obj->set_inher.queryType, NULL);
2147 return ir_tcl_error_exec (interp, argc, argv);
2149 return ir_tcl_send_APDU (interp, p, apdu, "search", *argv);
2153 * do_searchResponse: add search response handler
2155 static int do_searchResponse (void *o, Tcl_Interp *interp,
2156 int argc, char **argv)
2158 IrTcl_SetObj *obj = o;
2162 obj->searchResponse = NULL;
2165 else if (argc == -1)
2166 return ir_tcl_strdel (interp, &obj->searchResponse);
2169 xfree (obj->searchResponse);
2172 if (ir_tcl_strdup (interp, &obj->searchResponse, argv[2])
2177 obj->searchResponse = NULL;
2183 * do_presentResponse: add present response handler
2185 static int do_presentResponse (void *o, Tcl_Interp *interp,
2186 int argc, char **argv)
2188 IrTcl_SetObj *obj = o;
2192 obj->presentResponse = NULL;
2195 else if (argc == -1)
2196 return ir_tcl_strdel (interp, &obj->presentResponse);
2199 xfree (obj->presentResponse);
2202 if (ir_tcl_strdup (interp, &obj->presentResponse, argv[2])
2207 obj->presentResponse = NULL;
2213 * do_resultCount: Get number of hits
2215 static int do_resultCount (void *o, Tcl_Interp *interp,
2216 int argc, char **argv)
2218 IrTcl_SetObj *obj = o;
2222 obj->resultCount = 0;
2225 return ir_tcl_get_set_int (&obj->resultCount, interp, argc, argv);
2229 * do_searchStatus: Get search status (after search response)
2231 static int do_searchStatus (void *o, Tcl_Interp *interp,
2232 int argc, char **argv)
2234 IrTcl_SetObj *obj = o;
2238 return ir_tcl_get_set_int (&obj->searchStatus, interp, argc, argv);
2242 * do_presentStatus: Get search status (after search/present response)
2244 static int do_presentStatus (void *o, Tcl_Interp *interp,
2245 int argc, char **argv)
2247 IrTcl_SetObj *obj = o;
2251 return ir_tcl_get_set_int (&obj->presentStatus, interp, argc, argv);
2255 * do_nextResultSetPosition: Get next result set position
2256 * (after search/present response)
2258 static int do_nextResultSetPosition (void *o, Tcl_Interp *interp,
2259 int argc, char **argv)
2261 IrTcl_SetObj *obj = o;
2265 obj->nextResultSetPosition = 0;
2268 return ir_tcl_get_set_int (&obj->nextResultSetPosition, interp,
2273 * do_setName: Set result Set name
2275 static int do_setName (void *o, Tcl_Interp *interp,
2276 int argc, char **argv)
2278 IrTcl_SetObj *obj = o;
2281 return ir_tcl_strdup (interp, &obj->setName, "Default");
2282 else if (argc == -1)
2283 return ir_tcl_strdel (interp, &obj->setName);
2286 xfree (obj->setName);
2287 if (ir_tcl_strdup (interp, &obj->setName, argv[2])
2291 Tcl_AppendElement (interp, obj->setName);
2296 * do_numberOfRecordsReturned: Get number of records returned
2298 static int do_numberOfRecordsReturned (void *o, Tcl_Interp *interp,
2299 int argc, char **argv)
2301 IrTcl_SetObj *obj = o;
2305 obj->numberOfRecordsReturned = 0;
2308 return ir_tcl_get_set_int (&obj->numberOfRecordsReturned, interp,
2313 * do_type: Return type (if any) at position.
2315 static int do_type (void *o, Tcl_Interp *interp, int argc, char **argv)
2317 IrTcl_SetObj *obj = o;
2319 IrTcl_RecordList *rl;
2323 obj->record_list = NULL;
2326 else if (argc == -1)
2328 delete_IR_records (obj);
2333 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2334 " position\"", NULL);
2337 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2339 rl = find_IR_record (obj, offset);
2342 logf (LOG_DEBUG, "%s %s %s: no record", argv[0], argv[1], argv[2]);
2347 case Z_NamePlusRecord_databaseRecord:
2348 interp->result = "DB";
2350 case Z_NamePlusRecord_surrogateDiagnostic:
2351 interp->result = "SD";
2359 * do_recordType: Return record type (if any) at position.
2361 static int do_recordType (void *o, Tcl_Interp *interp, int argc, char **argv)
2363 IrTcl_SetObj *obj = o;
2365 IrTcl_RecordList *rl;
2371 else if (argc == -1)
2377 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2378 " position\"", NULL);
2381 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2383 rl = find_IR_record (obj, offset);
2386 logf (LOG_DEBUG, "%s %s %s: no record", argv[0], argv[1], argv[2]);
2389 if (rl->which != Z_NamePlusRecord_databaseRecord)
2391 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2394 Tcl_AppendElement (interp, (char*)
2395 IrTcl_getRecordSyntaxStr (rl->u.dbrec.type));
2400 * set record elements (for record extraction)
2402 static int do_recordElements (void *o, Tcl_Interp *interp,
2403 int argc, char **argv)
2405 IrTcl_SetObj *obj = o;
2409 obj->recordElements = NULL;
2412 else if (argc == -1)
2413 return ir_tcl_strdel (NULL, &obj->recordElements);
2416 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2417 " ?position?\"", NULL);
2422 xfree (obj->recordElements);
2423 return ir_tcl_strdup (NULL, &obj->recordElements,
2424 (*argv[2] ? argv[2] : NULL));
2426 Tcl_AppendResult (interp, obj->recordElements, NULL);
2433 static int ir_diagResult (Tcl_Interp *interp, IrTcl_Diagnostic *list, int num)
2439 for (i = 0; i<num; i++)
2441 sprintf (buf, "%d", list[i].condition);
2442 Tcl_AppendElement (interp, buf);
2443 cp = diagbib1_str (list[i].condition);
2445 Tcl_AppendElement (interp, (char*) cp);
2447 Tcl_AppendElement (interp, "");
2448 if (list[i].addinfo)
2449 Tcl_AppendElement (interp, (char*) list[i].addinfo);
2451 Tcl_AppendElement (interp, "");
2457 * do_diag: Return diagnostic record info
2459 static int do_diag (void *o, Tcl_Interp *interp, int argc, char **argv)
2461 IrTcl_SetObj *obj = o;
2463 IrTcl_RecordList *rl;
2469 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2470 " position\"", NULL);
2473 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2475 rl = find_IR_record (obj, offset);
2478 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2481 if (rl->which != Z_NamePlusRecord_surrogateDiagnostic)
2483 Tcl_AppendResult (interp, "No Diagnostic record at #", argv[2], NULL);
2486 return ir_diagResult (interp, rl->u.surrogateDiagnostics.list,
2487 rl->u.surrogateDiagnostics.num);
2491 * do_getMarc: Get ISO2709 Record lines/fields
2493 static int do_getMarc (void *o, Tcl_Interp *interp, int argc, char **argv)
2495 IrTcl_SetObj *obj = o;
2497 IrTcl_RecordList *rl;
2503 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2504 " position line|field tag ind field\"", NULL);
2507 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2509 rl = find_IR_record (obj, offset);
2512 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2515 if (rl->which != Z_NamePlusRecord_databaseRecord)
2517 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2520 return ir_tcl_get_marc (interp, rl->u.dbrec.buf, argc, argv);
2524 * do_getSutrs: Get SUTRS Record
2526 static int do_getSutrs (void *o, Tcl_Interp *interp, int argc, char **argv)
2528 IrTcl_SetObj *obj = o;
2530 IrTcl_RecordList *rl;
2536 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2537 " position\"", NULL);
2540 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2542 rl = find_IR_record (obj, offset);
2545 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2548 if (rl->which != Z_NamePlusRecord_databaseRecord)
2550 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2553 if (!rl->u.dbrec.buf || rl->u.dbrec.type != VAL_SUTRS)
2555 Tcl_AppendElement (interp, rl->u.dbrec.buf);
2561 * do_getGrs: Get a GRS-1 Record
2563 static int do_getGrs (void *o, Tcl_Interp *interp, int argc, char **argv)
2565 IrTcl_SetObj *obj = o;
2567 IrTcl_RecordList *rl;
2573 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2574 " position ?(set,tag) (set,tag) ...?\"", 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_GRS1)
2592 return ir_tcl_get_grs (interp, rl->u.dbrec.u.grs1, argc, argv);
2597 * do_getExplain: Get an Explain Record
2599 static int do_getExplain (void *o, Tcl_Interp *interp, int argc, char **argv)
2601 IrTcl_SetObj *obj = o;
2602 IrTcl_Obj *p = obj->parent;
2604 Z_ext_typeent *etype;
2606 IrTcl_RecordList *rl;
2612 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2613 " position ?mask? ...\"", NULL);
2616 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2618 rl = find_IR_record (obj, offset);
2621 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2624 if (rl->which != Z_NamePlusRecord_databaseRecord)
2626 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2629 if (!rl->u.dbrec.buf || rl->u.dbrec.type != VAL_EXPLAIN)
2632 if (!(etype = z_ext_getentbyref (VAL_EXPLAIN)))
2634 assert (rl->u.dbrec.buf);
2635 odr_setbuf (p->odr_in, rl->u.dbrec.buf, rl->u.dbrec.size, 0);
2636 if (!(*etype->fun)(p->odr_in, (char **) &rr, 0))
2639 if (etype->what != Z_External_explainRecord)
2642 return ir_tcl_get_explain (interp, rr, argc, argv);
2646 * do_responseStatus: Return response status (present or search)
2648 static int do_responseStatus (void *o, Tcl_Interp *interp,
2649 int argc, char **argv)
2651 IrTcl_SetObj *obj = o;
2655 obj->recordFlag = 0;
2656 obj->nonSurrogateDiagnosticNum = 0;
2657 obj->nonSurrogateDiagnosticList = NULL;
2660 else if (argc == -1)
2662 ir_deleteDiags (&obj->nonSurrogateDiagnosticList,
2663 &obj->nonSurrogateDiagnosticNum);
2666 if (!obj->recordFlag)
2668 Tcl_AppendElement (interp, "OK");
2673 case Z_Records_DBOSD:
2674 Tcl_AppendElement (interp, "DBOSD");
2677 Tcl_AppendElement (interp, "NSD");
2678 return ir_diagResult (interp, obj->nonSurrogateDiagnosticList,
2679 obj->nonSurrogateDiagnosticNum);
2685 * do_present: Perform Present Request
2688 static int do_present (void *o, Tcl_Interp *interp, int argc, char **argv)
2690 IrTcl_SetObj *obj = o;
2693 Z_PresentRequest *req;
2701 if (Tcl_GetInt (interp, argv[2], &start) == TCL_ERROR)
2708 if (Tcl_GetInt (interp, argv[3], &number) == TCL_ERROR)
2713 logf (LOG_DEBUG, "present %s %d %d", *argv, start, number);
2717 Tcl_AppendResult (interp, "not connected", NULL);
2718 return ir_tcl_error_exec (interp, argc, argv);
2721 obj->number = number;
2723 apdu = zget_APDU (p->odr_out, Z_APDU_presentRequest);
2724 req = apdu->u.presentRequest;
2726 set_referenceId (p->odr_out, &req->referenceId,
2727 obj->set_inher.referenceId);
2729 req->resultSetId = obj->setName ? obj->setName : "Default";
2731 req->resultSetStartPoint = &start;
2732 req->numberOfRecordsRequested = &number;
2733 if (obj->set_inher.preferredRecordSyntax)
2735 struct oident ident;
2737 ident.proto = p->protocol_type;
2738 ident.oclass = CLASS_RECSYN;
2739 ident.value = *obj->set_inher.preferredRecordSyntax;
2740 logf (LOG_DEBUG, "Preferred record syntax is %d", ident.value);
2741 req->preferredRecordSyntax = odr_oiddup (p->odr_out,
2742 oid_getoidbyent (&ident));
2745 req->preferredRecordSyntax = 0;
2747 if (obj->set_inher.elementSetNames && *obj->set_inher.elementSetNames)
2749 Z_ElementSetNames *esn = odr_malloc (p->odr_out, sizeof(*esn));
2750 Z_RecordComposition *compo = odr_malloc (p->odr_out, sizeof(*compo));
2752 esn->which = Z_ElementSetNames_generic;
2753 esn->u.generic = obj->set_inher.elementSetNames;
2755 req->recordComposition = compo;
2756 compo->which = Z_RecordComp_simple;
2757 compo->u.simple = esn;
2760 req->recordComposition = NULL;
2761 return ir_tcl_send_APDU (interp, p, apdu, "present", *argv);
2764 #define IR_TCL_RECORD_ENCODING_ISO2709 1
2765 #define IR_TCL_RECORD_ENCODING_RAW 2
2771 } IrTcl_FileRecordHead;
2774 * do_loadFile: Load result set from file
2776 static int do_loadFile (void *o, Tcl_Interp *interp,
2777 int argc, char **argv)
2779 IrTcl_SetObj *setobj = o;
2791 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2792 " filename ?start? ?number?\"", NULL);
2796 start = atoi (argv[3]);
2798 number = atoi (argv[4]);
2801 inf = fopen (argv[2], "r");
2804 Tcl_AppendResult (interp, "Cannot open ", argv[2], NULL);
2805 return ir_tcl_error_exec (interp, argc, argv);
2807 while (offset < (start+number))
2809 IrTcl_FileRecordHead head;
2810 IrTcl_RecordList *rl;
2812 if (fread (&head, sizeof(head), 1, inf) < 1)
2814 rl = new_IR_record (setobj, offset,
2815 Z_NamePlusRecord_databaseRecord,
2816 (argc > 5) ? argv[5] : NULL);
2817 rl->u.dbrec.type = head.syntax;
2818 if (head.encoding == IR_TCL_RECORD_ENCODING_ISO2709)
2820 if (!(buf = ir_tcl_fread_marc (inf, &size)))
2822 rl->u.dbrec.buf = buf;
2823 rl->u.dbrec.size = size;
2824 if (size != head.size)
2827 Tcl_AppendResult (interp, "bad ISO2709 encoding", NULL);
2828 return ir_tcl_error_exec (interp, argc, argv);
2831 else if (head.encoding == IR_TCL_RECORD_ENCODING_RAW)
2833 rl->u.dbrec.size = head.size;
2834 rl->u.dbrec.buf = ir_tcl_malloc (head.size + 1);
2835 if (fread (rl->u.dbrec.buf, rl->u.dbrec.size, 1, inf) < 1)
2838 Tcl_AppendResult (interp, "bad raw encoding", NULL);
2839 return ir_tcl_error_exec (interp, argc, argv);
2841 rl->u.dbrec.buf[rl->u.dbrec.size] = '\0';
2845 rl->u.dbrec.buf = NULL;
2846 rl->u.dbrec.size = 0;
2848 Tcl_AppendResult (interp, "bad encoding", NULL);
2849 return ir_tcl_error_exec (interp, argc, argv);
2853 setobj->numberOfRecordsReturned = offset - start;
2859 * do_saveFile: Save result set on file
2861 static int do_saveFile (void *o, Tcl_Interp *interp,
2862 int argc, char **argv)
2864 IrTcl_SetObj *setobj = o;
2869 IrTcl_RecordList *rl;
2875 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2876 " filename ?start? ?number?\"", NULL);
2880 start = atoi (argv[3]);
2882 number = atoi (argv[4]);
2885 outf = fopen (argv[2], "w");
2888 Tcl_AppendResult (interp, "cannot open file", NULL);
2889 return ir_tcl_error_exec (interp, argc, argv);
2891 while (offset < (start+number) && (rl = find_IR_record (setobj, offset)))
2893 if (rl->which == Z_NamePlusRecord_databaseRecord &&
2894 rl->u.dbrec.buf && rl->u.dbrec.size)
2896 IrTcl_FileRecordHead head;
2898 head.encoding = IR_TCL_RECORD_ENCODING_RAW;
2899 head.syntax = rl->u.dbrec.type;
2900 head.size = rl->u.dbrec.size;
2901 if (fwrite (&head, sizeof(head), 1, outf) < 1)
2903 Tcl_AppendResult (interp, "cannot write", NULL);
2904 return ir_tcl_error_exec (interp, argc, argv);
2906 if (fwrite (rl->u.dbrec.buf, rl->u.dbrec.size, 1, outf) < 1)
2908 Tcl_AppendResult (interp, "cannot write", NULL);
2909 return ir_tcl_error_exec (interp, argc, argv);
2916 Tcl_AppendResult (interp, "cannot write ", NULL);
2917 return ir_tcl_error_exec (interp, argc, argv);
2923 static IrTcl_Method ir_set_method_tab[] = {
2924 { "search", do_search, NULL},
2925 { "searchResponse", do_searchResponse, NULL},
2926 { "presentResponse", do_presentResponse, NULL},
2927 { "searchStatus", do_searchStatus, NULL},
2928 { "presentStatus", do_presentStatus, NULL},
2929 { "nextResultSetPosition", do_nextResultSetPosition, NULL},
2930 { "setName", do_setName, NULL},
2931 { "resultCount", do_resultCount, NULL},
2932 { "numberOfRecordsReturned", do_numberOfRecordsReturned, NULL},
2933 { "present", do_present, NULL},
2934 { "type", do_type, NULL},
2935 { "getMarc", do_getMarc, NULL},
2936 { "getSutrs", do_getSutrs, NULL},
2937 { "getGrs", do_getGrs, NULL},
2938 { "getExplain", do_getExplain, NULL},
2939 { "recordType", do_recordType, NULL},
2940 { "recordElements", do_recordElements, NULL},
2941 { "diag", do_diag, NULL},
2942 { "responseStatus", do_responseStatus, NULL},
2943 { "loadFile", do_loadFile, NULL},
2944 { "saveFile", do_saveFile, NULL},
2949 * ir_set_obj_method: IR Set Object methods
2951 static int ir_set_obj_method (ClientData clientData, Tcl_Interp *interp,
2952 int argc, char **argv)
2954 IrTcl_Methods tabs[3];
2955 IrTcl_SetObj *p = (IrTcl_SetObj *) clientData;
2960 Tcl_AppendResult (interp, wrongArgs, *argv, " method args...\"", NULL);
2963 tabs[0].tab = ir_set_method_tab;
2965 tabs[1].tab = ir_set_c_method_tab;
2966 tabs[1].obj = &p->set_inher;
2969 if (ir_tcl_method (interp, argc, argv, tabs, &r) == TCL_ERROR)
2970 return ir_tcl_method_error (interp, argc, argv, tabs);
2975 * ir_set_obj_delete: IR Set Object disposal
2977 static void ir_set_obj_delete (ClientData clientData)
2979 IrTcl_Methods tabs[3];
2980 IrTcl_SetObj *p = (IrTcl_SetObj *) clientData;
2982 logf (LOG_DEBUG, "ir set delete");
2984 tabs[0].tab = ir_set_method_tab;
2986 tabs[1].tab = ir_set_c_method_tab;
2987 tabs[1].obj = &p->set_inher;
2990 ir_tcl_method (NULL, -1, NULL, tabs, NULL);
2996 * ir_set_obj_init: IR Set Object initialization
2998 static int ir_set_obj_init (ClientData clientData, Tcl_Interp *interp,
2999 int argc, char **argv, ClientData *subData,
3000 ClientData parentData)
3002 IrTcl_Methods tabs[3];
3005 if (argc < 2 || argc > 3)
3007 Tcl_AppendResult (interp, wrongArgs, *argv,
3008 " objSetName ?objParent?\"", NULL);
3011 obj = ir_tcl_malloc (sizeof(*obj));
3012 logf (LOG_DEBUG, "ir set create %s", argv[1]);
3019 obj->parent = (IrTcl_Obj *) parentData;
3021 dst = &obj->set_inher;
3022 src = &obj->parent->set_inher;
3024 if ((dst->num_databaseNames = src->num_databaseNames))
3026 dst->databaseNames =
3027 ir_tcl_malloc (sizeof (*dst->databaseNames)
3028 * (1+dst->num_databaseNames));
3029 for (i = 0; i < dst->num_databaseNames; i++)
3030 if (ir_tcl_strdup (interp, &dst->databaseNames[i],
3031 src->databaseNames[i]) == TCL_ERROR)
3033 dst->databaseNames[i] = NULL;
3036 dst->databaseNames = NULL;
3037 if (ir_tcl_strdup (interp, &dst->queryType, src->queryType)
3041 if (ir_tcl_strdup (interp, &dst->referenceId, src->referenceId)
3045 if (ir_tcl_strdup (interp, &dst->elementSetNames, src->elementSetNames)
3049 if (ir_tcl_strdup (interp, &dst->smallSetElementSetNames,
3050 src->smallSetElementSetNames)
3054 if (ir_tcl_strdup (interp, &dst->mediumSetElementSetNames,
3055 src->mediumSetElementSetNames)
3059 if (src->preferredRecordSyntax &&
3060 (dst->preferredRecordSyntax
3061 = ir_tcl_malloc (sizeof(*dst->preferredRecordSyntax))))
3062 *dst->preferredRecordSyntax = *src->preferredRecordSyntax;
3064 dst->preferredRecordSyntax = NULL;
3065 dst->replaceIndicator = src->replaceIndicator;
3066 dst->smallSetUpperBound = src->smallSetUpperBound;
3067 dst->largeSetLowerBound = src->largeSetLowerBound;
3068 dst->mediumSetPresentNumber = src->mediumSetPresentNumber;
3073 tabs[0].tab = ir_set_method_tab;
3077 if (ir_tcl_method (interp, 0, NULL, tabs, NULL) == TCL_ERROR)
3080 *subData = (ClientData) obj;
3085 * ir_set_obj_mk: IR Set Object creation
3087 static int ir_set_obj_mk (ClientData clientData, Tcl_Interp *interp,
3088 int argc, char **argv)
3091 ClientData parentData = 0;
3096 Tcl_CmdInfo parent_info;
3097 if (!Tcl_GetCommandInfo (interp, argv[2], &parent_info))
3099 Tcl_AppendResult (interp, "no object parent", NULL);
3100 return ir_tcl_error_exec (interp, argc, argv);
3102 parentData = parent_info.clientData;
3104 r = ir_set_obj_init (clientData, interp, argc, argv, &subData, parentData);
3107 Tcl_CreateCommand (interp, argv[1], ir_set_obj_method,
3108 subData, ir_set_obj_delete);
3112 IrTcl_Class ir_set_obj_class = {
3119 /* ------------------------------------------------------- */
3122 * do_scan: Perform scan
3124 static int do_scan (void *o, Tcl_Interp *interp, int argc, char **argv)
3128 IrTcl_ScanObj *obj = o;
3129 IrTcl_Obj *p = obj->parent;
3132 struct ccl_rpn_node *rpn;
3140 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
3141 " scanQuery\"", NULL);
3144 logf (LOG_DEBUG, "scan %s %s", *argv, argv[2]);
3145 if (!p->set_inher.num_databaseNames)
3147 Tcl_AppendResult (interp, "no databaseNames", NULL);
3148 return ir_tcl_error_exec (interp, argc, argv);
3152 Tcl_AppendResult (interp, "not connected", NULL);
3153 return ir_tcl_error_exec (interp, argc, argv);
3156 apdu = zget_APDU (p->odr_out, Z_APDU_scanRequest);
3157 req = apdu->u.scanRequest;
3159 set_referenceId (p->odr_out, &req->referenceId, p->set_inher.referenceId);
3160 req->num_databaseNames = p->set_inher.num_databaseNames;
3161 req->databaseNames = p->set_inher.databaseNames;
3164 if (!(req->termListAndStartPoint =
3165 p_query_scan (p->odr_out, p->protocol_type,
3166 &req->attributeSet, argv[2])))
3168 Tcl_AppendResult (interp, "query syntax error", NULL);
3169 return ir_tcl_error_exec (interp, argc, argv);
3172 rpn = ccl_find_str(p->bibset, argv[2], &r, &pos);
3175 Tcl_AppendResult (interp, "ccl syntax error ", ccl_err_msg(r), NULL);
3176 return ir_tcl_error_exec (interp, argc, argv);
3178 bib1.proto = p->protocol_type;
3179 bib1.oclass = CLASS_ATTSET;
3180 bib1.value = VAL_BIB1;
3182 req->attributeSet = oid_getoidbyent (&bib1);
3183 if (!(req->termListAndStartPoint = ccl_scan_query (rpn)))
3186 req->stepSize = &obj->stepSize;
3187 req->numberOfTermsRequested = &obj->numberOfTermsRequested;
3188 req->preferredPositionInResponse = &obj->preferredPositionInResponse;
3189 logf (LOG_DEBUG, "stepSize=%d", *req->stepSize);
3190 logf (LOG_DEBUG, "numberOfTermsRequested=%d",
3191 *req->numberOfTermsRequested);
3192 logf (LOG_DEBUG, "preferredPositionInResponse=%d",
3193 *req->preferredPositionInResponse);
3195 return ir_tcl_send_APDU (interp, p, apdu, "scan", *argv);
3199 * do_scanResponse: add scan response handler
3201 static int do_scanResponse (void *o, Tcl_Interp *interp,
3202 int argc, char **argv)
3204 IrTcl_ScanObj *obj = o;
3208 obj->scanResponse = NULL;
3211 else if (argc == -1)
3212 return ir_tcl_strdel (interp, &obj->scanResponse);
3215 xfree (obj->scanResponse);
3218 if (ir_tcl_strdup (interp, &obj->scanResponse, argv[2])
3223 obj->scanResponse = NULL;
3229 * do_stepSize: Set/get replace Step Size
3231 static int do_stepSize (void *obj, Tcl_Interp *interp,
3232 int argc, char **argv)
3234 IrTcl_ScanObj *p = obj;
3240 return ir_tcl_get_set_int (&p->stepSize, interp, argc, argv);
3244 * do_numberOfTermsRequested: Set/get Number of Terms requested
3246 static int do_numberOfTermsRequested (void *obj, Tcl_Interp *interp,
3247 int argc, char **argv)
3249 IrTcl_ScanObj *p = obj;
3253 p->numberOfTermsRequested = 20;
3256 return ir_tcl_get_set_int (&p->numberOfTermsRequested, interp, argc, argv);
3261 * do_preferredPositionInResponse: Set/get preferred Position
3263 static int do_preferredPositionInResponse (void *obj, Tcl_Interp *interp,
3264 int argc, char **argv)
3266 IrTcl_ScanObj *p = obj;
3270 p->preferredPositionInResponse = 1;
3273 return ir_tcl_get_set_int (&p->preferredPositionInResponse, interp,
3278 * do_scanStatus: Get scan status
3280 static int do_scanStatus (void *obj, Tcl_Interp *interp,
3281 int argc, char **argv)
3283 IrTcl_ScanObj *p = obj;
3287 return ir_tcl_get_set_int (&p->scanStatus, interp, argc, argv);
3291 * do_numberOfEntriesReturned: Get number of Entries returned
3293 static int do_numberOfEntriesReturned (void *obj, Tcl_Interp *interp,
3294 int argc, char **argv)
3296 IrTcl_ScanObj *p = obj;
3300 return ir_tcl_get_set_int (&p->numberOfEntriesReturned, interp,
3305 * do_positionOfTerm: Get position of Term
3307 static int do_positionOfTerm (void *obj, Tcl_Interp *interp,
3308 int argc, char **argv)
3310 IrTcl_ScanObj *p = obj;
3314 return ir_tcl_get_set_int (&p->positionOfTerm, interp, argc, argv);
3318 * do_scanLine: get Scan Line (surrogate or normal) after response
3320 static int do_scanLine (void *obj, Tcl_Interp *interp, int argc, char **argv)
3322 IrTcl_ScanObj *p = obj;
3328 p->entries_flag = 0;
3330 p->nonSurrogateDiagnosticNum = 0;
3331 p->nonSurrogateDiagnosticList = 0;
3334 else if (argc == -1)
3336 p->entries_flag = 0;
3337 /* release entries */
3340 ir_deleteDiags (&p->nonSurrogateDiagnosticList,
3341 &p->nonSurrogateDiagnosticNum);
3346 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
3347 " position\"", NULL);
3350 if (Tcl_GetInt (interp, argv[2], &i) == TCL_ERROR)
3352 if (!p->entries_flag || !p->entries || i >= p->num_entries || i < 0)
3354 switch (p->entries[i].which)
3356 case Z_Entry_termInfo:
3357 Tcl_AppendElement (interp, "T");
3358 if (p->entries[i].u.term.buf)
3359 Tcl_AppendElement (interp, p->entries[i].u.term.buf);
3361 Tcl_AppendElement (interp, "");
3362 sprintf (numstr, "%d", p->entries[i].u.term.globalOccurrences);
3363 Tcl_AppendElement (interp, numstr);
3365 case Z_Entry_surrogateDiagnostic:
3366 Tcl_AppendElement (interp, "SD");
3367 return ir_diagResult (interp, p->entries[i].u.diag.list,
3368 p->entries[i].u.diag.num);
3374 static IrTcl_Method ir_scan_method_tab[] = {
3375 { "scan", do_scan, NULL},
3376 { "scanResponse", do_scanResponse, NULL},
3377 { "stepSize", do_stepSize, NULL},
3378 { "numberOfTermsRequested", do_numberOfTermsRequested, NULL},
3379 { "preferredPositionInResponse", do_preferredPositionInResponse, NULL},
3380 { "scanStatus", do_scanStatus, NULL},
3381 { "numberOfEntriesReturned", do_numberOfEntriesReturned, NULL},
3382 { "positionOfTerm", do_positionOfTerm, NULL},
3383 { "scanLine", do_scanLine, NULL},
3388 * ir_scan_obj_method: IR Scan Object methods
3390 static int ir_scan_obj_method (ClientData clientData, Tcl_Interp *interp,
3391 int argc, char **argv)
3393 IrTcl_Methods tabs[2];
3398 Tcl_AppendResult (interp, wrongArgs, *argv, " method args...\"", NULL);
3401 tabs[0].tab = ir_scan_method_tab;
3402 tabs[0].obj = clientData;
3405 if (ir_tcl_method (interp, argc, argv, tabs, &r) == TCL_ERROR)
3406 return ir_tcl_method_error (interp, argc, argv, tabs);
3411 * ir_scan_obj_delete: IR Scan Object disposal
3413 static void ir_scan_obj_delete (ClientData clientData)
3415 IrTcl_Methods tabs[2];
3416 IrTcl_ScanObj *obj = (IrTcl_ScanObj *) clientData;
3418 tabs[0].tab = ir_scan_method_tab;
3422 ir_tcl_method (NULL, -1, NULL, tabs, NULL);
3427 * ir_scan_obj_mk: IR Scan Object creation
3429 static int ir_scan_obj_mk (ClientData clientData, Tcl_Interp *interp,
3430 int argc, char **argv)
3432 Tcl_CmdInfo parent_info;
3434 IrTcl_Methods tabs[2];
3438 Tcl_AppendResult (interp, wrongArgs, *argv,
3439 "objScanName objParentName\"", NULL);
3442 logf (LOG_DEBUG, "ir scan create %s", argv[1]);
3443 if (!Tcl_GetCommandInfo (interp, argv[2], &parent_info))
3445 Tcl_AppendResult (interp, "no object parent", NULL);
3446 return ir_tcl_error_exec (interp, argc, argv);
3448 obj = ir_tcl_malloc (sizeof(*obj));
3449 obj->parent = (IrTcl_Obj *) parent_info.clientData;
3451 tabs[0].tab = ir_scan_method_tab;
3455 if (ir_tcl_method (interp, 0, NULL, tabs, NULL) == TCL_ERROR)
3457 Tcl_CreateCommand (interp, argv[1], ir_scan_obj_method,
3458 (ClientData) obj, ir_scan_obj_delete);
3462 /* ------------------------------------------------------- */
3465 * ir_log_init_proc: set yaz log level
3467 static int ir_log_init_proc (ClientData clientData, Tcl_Interp *interp,
3468 int argc, char **argv)
3470 if (argc <= 1 || argc > 4)
3472 Tcl_AppendResult (interp, wrongArgs, *argv,
3473 " ?level ?prefix ?filename\"", NULL);
3477 log_init (log_mask_str (argv[1]), "", NULL);
3479 log_init (log_mask_str (argv[1]), argv[2], NULL);
3481 log_init (log_mask_str (argv[1]), argv[2], argv[3]);
3486 * ir_log_proc: log yaz message
3488 static int ir_log_proc (ClientData clientData, Tcl_Interp *interp,
3489 int argc, char **argv)
3494 Tcl_AppendResult (interp, wrongArgs, *argv,
3495 " level string\"", NULL);
3498 mask = log_mask_str_x (argv[1], 0);
3499 logf (LOG_DEBUG, "%s", argv[2]);
3505 * ir_version: log ir version
3507 static int ir_version (ClientData clientData, Tcl_Interp *interp,
3508 int argc, char **argv)
3510 Tcl_AppendElement (interp, IR_TCL_VERSION);
3511 Tcl_AppendElement (interp, YAZ_VERSION);
3516 /* ------------------------------------------------------- */
3517 static void ir_initResponse (void *obj, Z_InitResponse *initrs)
3521 p->initResult = *initrs->result ? 1 : 0;
3522 if (!*initrs->result)
3523 logf (LOG_DEBUG, "Connection rejected by target");
3525 logf (LOG_DEBUG, "Connection accepted by target");
3527 get_referenceId (&p->set_inher.referenceId, initrs->referenceId);
3529 xfree (p->targetImplementationId);
3530 ir_tcl_strdup (p->interp, &p->targetImplementationId,
3531 initrs->implementationId);
3532 xfree (p->targetImplementationName);
3533 ir_tcl_strdup (p->interp, &p->targetImplementationName,
3534 initrs->implementationName);
3535 xfree (p->targetImplementationVersion);
3536 ir_tcl_strdup (p->interp, &p->targetImplementationVersion,
3537 initrs->implementationVersion);
3539 p->maximumRecordSize = *initrs->maximumRecordSize;
3540 p->preferredMessageSize = *initrs->preferredMessageSize;
3542 memcpy (&p->options, initrs->options, sizeof(initrs->options));
3543 memcpy (&p->protocolVersion, initrs->protocolVersion,
3544 sizeof(initrs->protocolVersion));
3545 xfree (p->userInformationField);
3546 p->userInformationField = NULL;
3547 if (initrs->userInformationField)
3551 if (initrs->userInformationField->which == ODR_EXTERNAL_octet &&
3552 (p->userInformationField =
3553 ir_tcl_malloc ((len =
3554 initrs->userInformationField->
3555 u.octet_aligned->len) +1)))
3557 memcpy (p->userInformationField,
3558 initrs->userInformationField->u.octet_aligned->buf,
3560 (p->userInformationField)[len] = '\0';
3565 static void ir_deleteDiags (IrTcl_Diagnostic **dst_list, int *dst_num)
3568 for (i = 0; i<*dst_num; i++)
3569 xfree (dst_list[i]->addinfo);
3575 static void ir_handleDiags (IrTcl_Diagnostic **dst_list, int *dst_num,
3576 Z_DiagRec **list, int num)
3579 char *addinfo = NULL;
3582 *dst_list = ir_tcl_malloc (sizeof(**dst_list) * num);
3583 for (i = 0; i<num; i++)
3586 switch (list[i]->which)
3588 case Z_DiagRec_defaultFormat:
3589 (*dst_list)[i].condition = *list[i]->u.defaultFormat->condition;
3591 switch (list[i]->u.defaultFormat->which)
3593 case Z_DefaultDiagFormat_v2Addinfo:
3594 addinfo = list[i]->u.defaultFormat->u.v2Addinfo;
3596 case Z_DefaultDiagFormat_v3Addinfo:
3597 addinfo = list[i]->u.defaultFormat->u.v3Addinfo;
3601 addinfo = list[i]->u.defaultFormat->addinfo;
3604 ((*dst_list)[i].addinfo = ir_tcl_malloc (strlen(addinfo)+1)))
3605 strcpy ((*dst_list)[i].addinfo, addinfo);
3606 cp = diagbib1_str ((*dst_list)[i].condition);
3607 logf (LOG_DEBUG, "Diag %d %s %s", (*dst_list)[i].condition,
3608 cp ? cp : "", addinfo ? addinfo : "");
3611 (*dst_list)[i].addinfo = NULL;
3612 (*dst_list)[i].condition = 0;
3617 static void ir_handleDBRecord (IrTcl_Obj *p, IrTcl_RecordList *rl,
3620 struct oident *ident;
3621 Z_ext_typeent *etype;
3623 logf (LOG_DEBUG, "handleDBRecord size=%d", oe->u.octet_aligned->len);
3624 rl->u.dbrec.size = oe->u.octet_aligned->len;
3625 rl->u.dbrec.buf = NULL;
3627 if ((ident = oid_getentbyoid (oe->direct_reference)))
3628 rl->u.dbrec.type = ident->value;
3630 rl->u.dbrec.type = VAL_USMARC;
3632 if (ident && (oe->which == Z_External_single ||
3633 oe->which == Z_External_octet)
3634 && (etype = z_ext_getentbyref (ident->value)))
3638 odr_setbuf (p->odr_in, (char*) oe->u.octet_aligned->buf,
3639 oe->u.octet_aligned->len, 0);
3640 if (!(*etype->fun)(p->odr_in, (char **) &rr, 0))
3642 rl->u.dbrec.type = VAL_NONE;
3645 switch (etype->what)
3647 case Z_External_sutrs:
3648 logf (LOG_DEBUG, "Z_External_sutrs");
3650 if ((rl->u.dbrec.buf = ir_tcl_malloc (oe->u.sutrs->len+1)))
3652 memcpy (rl->u.dbrec.buf, oe->u.sutrs->buf,
3654 rl->u.dbrec.buf[oe->u.sutrs->len] = '\0';
3656 rl->u.dbrec.size = oe->u.sutrs->len;
3658 case Z_External_grs1:
3659 logf (LOG_DEBUG, "Z_External_grs1");
3661 ir_tcl_grs_mk (oe->u.grs1, &rl->u.dbrec.u.grs1);
3663 case Z_External_explainRecord:
3664 logf (LOG_DEBUG, "Z_External_explainRecord");
3665 if ((rl->u.dbrec.buf = ir_tcl_malloc (rl->u.dbrec.size)))
3667 memcpy (rl->u.dbrec.buf, oe->u.octet_aligned->buf,
3675 if (oe->which == Z_External_octet && rl->u.dbrec.size > 0)
3677 char *buf = (char*) oe->u.octet_aligned->buf;
3678 if ((rl->u.dbrec.buf = ir_tcl_malloc (rl->u.dbrec.size)))
3679 memcpy (rl->u.dbrec.buf, buf, rl->u.dbrec.size);
3681 else if (rl->u.dbrec.type == VAL_SUTRS &&
3682 oe->which == Z_External_sutrs)
3684 if ((rl->u.dbrec.buf = ir_tcl_malloc (oe->u.sutrs->len+1)))
3686 memcpy (rl->u.dbrec.buf, oe->u.sutrs->buf,
3688 rl->u.dbrec.buf[oe->u.sutrs->len] = '\0';
3690 rl->u.dbrec.size = oe->u.sutrs->len;
3692 else if (rl->u.dbrec.type == VAL_GRS1 &&
3693 oe->which == Z_External_grs1)
3695 ir_tcl_grs_mk (oe->u.grs1, &rl->u.dbrec.u.grs1);
3700 static void ir_handleZRecords (void *o, Z_Records *zrs, IrTcl_SetObj *setobj,
3701 const char *elements)
3706 IrTcl_RecordList *rl;
3708 setobj->which = zrs->which;
3709 setobj->recordFlag = 1;
3711 ir_deleteDiags (&setobj->nonSurrogateDiagnosticList,
3712 &setobj->nonSurrogateDiagnosticNum);
3713 if (zrs->which == Z_Records_DBOSD)
3715 int num_rec = zrs->u.databaseOrSurDiagnostics->num_records;
3717 if (num_rec != setobj->numberOfRecordsReturned)
3719 logf (LOG_WARN, "numberOfRecordsReturned=%d but num records=%d",
3720 setobj->numberOfRecordsReturned, num_rec);
3721 setobj->numberOfRecordsReturned = num_rec;
3724 for (offset = 0; offset < num_rec; offset++)
3726 Z_NamePlusRecord *znpr = zrs->u.databaseOrSurDiagnostics->
3729 rl = new_IR_record (setobj, setobj->start + offset, znpr->which,
3731 if (rl->which == Z_NamePlusRecord_surrogateDiagnostic)
3732 ir_handleDiags (&rl->u.surrogateDiagnostics.list,
3733 &rl->u.surrogateDiagnostics.num,
3734 &znpr->u.surrogateDiagnostic,
3737 ir_handleDBRecord (p, rl,
3738 (Z_External*) (znpr->u.databaseRecord));
3741 else if (zrs->which == Z_Records_multipleNSD)
3743 logf (LOG_DEBUG, "multipleNonSurrogateDiagnostic %d",
3744 zrs->u.multipleNonSurDiagnostics->num_diagRecs);
3745 setobj->numberOfRecordsReturned = 0;
3746 ir_handleDiags (&setobj->nonSurrogateDiagnosticList,
3747 &setobj->nonSurrogateDiagnosticNum,
3748 zrs->u.multipleNonSurDiagnostics->diagRecs,
3749 zrs->u.multipleNonSurDiagnostics->num_diagRecs);
3754 Z_DiagRec dr, *dr_p = &dr;
3755 dr.which = Z_DiagRec_defaultFormat;
3756 dr.u.defaultFormat = zrs->u.nonSurrogateDiagnostic;
3758 Z_DiagRec *dr_p = zrs->u.nonSurrogateDiagnostic;
3760 logf (LOG_DEBUG, "NonSurrogateDiagnostic");
3762 setobj->numberOfRecordsReturned = 0;
3763 ir_handleDiags (&setobj->nonSurrogateDiagnosticList,
3764 &setobj->nonSurrogateDiagnosticNum,
3769 static void ir_searchResponse (void *o, Z_SearchResponse *searchrs,
3770 IrTcl_SetObj *setobj)
3772 Z_Records *zrs = searchrs->records;
3774 logf (LOG_DEBUG, "Received search response");
3777 logf (LOG_DEBUG, "Search response, no object!");
3780 setobj->searchStatus = *searchrs->searchStatus;
3781 get_referenceId (&setobj->set_inher.referenceId, searchrs->referenceId);
3782 setobj->resultCount = *searchrs->resultCount;
3783 if (searchrs->presentStatus)
3784 setobj->presentStatus = *searchrs->presentStatus;
3786 setobj->presentStatus = Z_RES_NONE;
3787 if (searchrs->nextResultSetPosition)
3788 setobj->nextResultSetPosition = *searchrs->nextResultSetPosition;
3790 logf (LOG_DEBUG, "status %d hits %d",
3791 setobj->searchStatus, setobj->resultCount);
3795 if (setobj->resultCount <= setobj->set_inher.smallSetUpperBound)
3796 es = setobj->set_inher.smallSetElementSetNames;
3798 es = setobj->set_inher.mediumSetElementSetNames;
3799 setobj->numberOfRecordsReturned = *searchrs->numberOfRecordsReturned;
3800 ir_handleZRecords (o, zrs, setobj, es);
3804 setobj->numberOfRecordsReturned = 0;
3805 setobj->recordFlag = 0;
3810 static void ir_presentResponse (void *o, Z_PresentResponse *presrs,
3811 IrTcl_SetObj *setobj)
3813 Z_Records *zrs = presrs->records;
3815 logf (LOG_DEBUG, "Received present response");
3818 logf (LOG_DEBUG, "Present response, no object!");
3821 setobj->presentStatus = *presrs->presentStatus;
3822 get_referenceId (&setobj->set_inher.referenceId, presrs->referenceId);
3823 setobj->nextResultSetPosition = *presrs->nextResultSetPosition;
3826 setobj->numberOfRecordsReturned = *presrs->numberOfRecordsReturned;
3827 ir_handleZRecords (o, zrs, setobj, setobj->set_inher.elementSetNames);
3831 setobj->numberOfRecordsReturned = 0;
3832 setobj->recordFlag = 0;
3833 logf (LOG_DEBUG, "No records!");
3837 static void ir_scanResponse (void *o, Z_ScanResponse *scanrs,
3838 IrTcl_ScanObj *scanobj)
3842 logf (LOG_DEBUG, "Received scanResponse");
3844 get_referenceId (&p->set_inher.referenceId, scanrs->referenceId);
3845 scanobj->scanStatus = *scanrs->scanStatus;
3846 logf (LOG_DEBUG, "scanStatus=%d", scanobj->scanStatus);
3848 if (scanrs->stepSize)
3849 scanobj->stepSize = *scanrs->stepSize;
3850 logf (LOG_DEBUG, "stepSize=%d", scanobj->stepSize);
3852 scanobj->numberOfEntriesReturned = *scanrs->numberOfEntriesReturned;
3853 logf (LOG_DEBUG, "numberOfEntriesReturned=%d",
3854 scanobj->numberOfEntriesReturned);
3856 if (scanrs->positionOfTerm)
3857 scanobj->positionOfTerm = *scanrs->positionOfTerm;
3859 scanobj->positionOfTerm = -1;
3860 logf (LOG_DEBUG, "positionOfTerm=%d", scanobj->positionOfTerm);
3862 xfree (scanobj->entries);
3863 scanobj->entries = NULL;
3864 scanobj->num_entries = 0;
3865 scanobj->entries_flag = 0;
3867 ir_deleteDiags (&scanobj->nonSurrogateDiagnosticList,
3868 &scanobj->nonSurrogateDiagnosticNum);
3869 if (scanrs->entries)
3874 scanobj->entries_flag = 1;
3875 if (scanrs->entries)
3877 scanobj->num_entries = scanrs->entries->num_entries;
3878 scanobj->entries = ir_tcl_malloc (scanobj->num_entries *
3879 sizeof(*scanobj->entries));
3880 ze = scanrs->entries->entries;
3882 for (i=0; i<scanobj->num_entries; i++, ze++)
3884 scanobj->entries[i].which = (*ze)->which;
3885 switch ((*ze)->which)
3887 case Z_Entry_termInfo:
3888 if ((*ze)->u.termInfo->term->which == Z_Term_general)
3890 int l = (*ze)->u.termInfo->term->u.general->len;
3891 scanobj->entries[i].u.term.buf = ir_tcl_malloc (1+l);
3892 memcpy (scanobj->entries[i].u.term.buf,
3893 (*ze)->u.termInfo->term->u.general->buf,
3895 scanobj->entries[i].u.term.buf[l] = '\0';
3898 scanobj->entries[i].u.term.buf = NULL;
3899 if ((*ze)->u.termInfo->globalOccurrences)
3900 scanobj->entries[i].u.term.globalOccurrences =
3901 *(*ze)->u.termInfo->globalOccurrences;
3903 scanobj->entries[i].u.term.globalOccurrences = 0;
3905 case Z_Entry_surrogateDiagnostic:
3906 ir_handleDiags (&scanobj->entries[i].u.diag.list,
3907 &scanobj->entries[i].u.diag.num,
3908 &(*ze)->u.surrogateDiagnostic,
3913 if (scanrs->entries->nonsurrogateDiagnostics)
3914 ir_handleDiags (&scanobj->nonSurrogateDiagnosticList,
3915 &scanobj->nonSurrogateDiagnosticNum,
3916 scanrs->entries->nonsurrogateDiagnostics,
3917 scanrs->entries->num_nonsurrogateDiagnostics);
3922 * ir_select_read: handle incoming packages
3924 static void ir_select_read (ClientData clientData)
3926 IrTcl_Obj *p = (IrTcl_Obj *) clientData;
3931 Tcl_CmdInfo cmd_info;
3932 const char *apdu_call;
3934 logf(LOG_DEBUG, "Read handler fd=%d", cs_fileno(p->cs_link));
3935 if (p->state == IR_TCL_R_Connecting)
3937 logf(LOG_DEBUG, "read: connect");
3938 r = cs_rcvconnect (p->cs_link);
3941 logf (LOG_WARN, "cs_rcvconnect returned 1");
3944 p->state = IR_TCL_R_Idle;
3946 ir_select_remove_write (cs_fileno (p->cs_link), p);
3949 logf (LOG_DEBUG, "cs_rcvconnect error");
3950 ir_tcl_disconnect (p);
3953 p->failInfo = IR_TCL_FAIL_CONNECT;
3954 ir_tcl_eval (p->interp, p->failback);
3956 ir_obj_delete ((ClientData) p);
3960 ir_tcl_eval (p->interp, p->callback);
3961 if (p->ref_count == 2 && p->cs_link && p->request_queue
3962 && p->state == IR_TCL_R_Idle)
3963 ir_tcl_send_q (p, p->request_queue, "x");
3964 ir_obj_delete ((ClientData) p);
3969 p->state = IR_TCL_R_Reading;
3971 /* read incoming APDU */
3972 if ((r=cs_get (p->cs_link, &p->buf_in, &p->len_in)) == 1)
3974 logf(LOG_DEBUG, "PDU Fraction read");
3977 /* signal one more use of ir object - callbacks must not
3978 release the ir memory (p pointer) */
3982 logf (LOG_DEBUG, "cs_get failed, code %d", r);
3983 ir_tcl_disconnect (p);
3986 p->failInfo = IR_TCL_FAIL_READ;
3987 ir_tcl_eval (p->interp, p->failback);
3989 /* release ir object now if callback deleted it */
3990 ir_obj_delete ((ClientData) p);
3993 /* got complete APDU. Now decode */
3996 odr_setbuf (p->odr_in, p->buf_in, r, 0);
3997 logf (LOG_DEBUG, "cs_get ok, total size %d", r);
3998 if (!z_APDU (p->odr_in, &apdu, 0))
4000 logf (LOG_DEBUG, "cs_get failed: %s",
4001 odr_errmsg (odr_geterror (p->odr_in)));
4002 ir_tcl_disconnect (p);
4005 p->failInfo = IR_TCL_FAIL_IN_APDU;
4006 p->apduOffset = odr_offset (p->odr_in);
4007 ir_tcl_eval (p->interp, p->failback);
4009 /* release ir object now if failback deleted it */
4010 ir_obj_delete ((ClientData) p);
4013 /* handle APDU and invoke callback */
4014 rq = p->request_queue;
4017 logf (LOG_FATAL, "Internal error. No queue entry");
4020 object_name = rq->object_name;
4021 logf (LOG_DEBUG, "Object %s", object_name);
4023 if (Tcl_GetCommandInfo (p->interp, object_name, &cmd_info))
4027 case Z_APDU_initResponse:
4028 p->eventType = "init";
4029 ir_initResponse (p, apdu->u.initResponse);
4030 apdu_call = p->initResponse;
4032 case Z_APDU_searchResponse:
4033 p->eventType = "search";
4034 ir_searchResponse (p, apdu->u.searchResponse,
4035 (IrTcl_SetObj *) cmd_info.clientData);
4036 apdu_call = ((IrTcl_SetObj *)
4037 cmd_info.clientData)->searchResponse;
4039 case Z_APDU_presentResponse:
4040 p->eventType = "present";
4041 ir_presentResponse (p, apdu->u.presentResponse,
4042 (IrTcl_SetObj *) cmd_info.clientData);
4043 apdu_call = ((IrTcl_SetObj *)
4044 cmd_info.clientData)->presentResponse;
4046 case Z_APDU_scanResponse:
4047 p->eventType = "scan";
4048 ir_scanResponse (p, apdu->u.scanResponse,
4049 (IrTcl_ScanObj *) cmd_info.clientData);
4050 apdu_call = ((IrTcl_ScanObj *)
4051 cmd_info.clientData)->scanResponse;
4054 logf (LOG_WARN, "Received unknown APDU type (%d)",
4056 ir_tcl_disconnect (p);
4059 p->failInfo = IR_TCL_FAIL_UNKNOWN_APDU;
4060 ir_tcl_eval (p->interp, p->failback);
4065 p->request_queue = rq->next;
4066 p->state = IR_TCL_R_Idle;
4069 ir_tcl_eval (p->interp, apdu_call);
4070 else if (rq->callback)
4071 ir_tcl_eval (p->interp, rq->callback);
4072 xfree (rq->buf_out);
4073 xfree (rq->callback);
4074 xfree (rq->object_name);
4076 odr_reset (p->odr_in);
4077 if (p->ref_count == 1)
4079 ir_obj_delete ((ClientData) p);
4082 ir_obj_delete ((ClientData) p);
4083 } while (p->cs_link && cs_more (p->cs_link));
4084 if (p->cs_link && p->request_queue && p->state == IR_TCL_R_Idle)
4085 ir_tcl_send_q (p, p->request_queue, "x");
4089 * ir_select_write: handle outgoing packages - not yet written.
4091 static int ir_select_write (ClientData clientData)
4093 IrTcl_Obj *p = (IrTcl_Obj *) clientData;
4097 logf (LOG_DEBUG, "Write handler fd=%d", cs_fileno(p->cs_link));
4098 if (p->state == IR_TCL_R_Connecting)
4100 logf(LOG_DEBUG, "write: connect");
4101 r = cs_rcvconnect (p->cs_link);
4104 logf (LOG_DEBUG, "cs_rcvconnect returned 1");
4107 p->state = IR_TCL_R_Idle;
4109 ir_select_remove_write (cs_fileno (p->cs_link), p);
4112 logf (LOG_DEBUG, "cs_rcvconnect error");
4113 ir_tcl_disconnect (p);
4116 p->failInfo = IR_TCL_FAIL_CONNECT;
4117 ir_tcl_eval (p->interp, p->failback);
4119 ir_obj_delete ((ClientData) p);
4123 ir_tcl_eval (p->interp, p->callback);
4124 ir_obj_delete ((ClientData) p);
4127 rq = p->request_queue;
4128 if (!rq || !rq->buf_out)
4131 if ((r=cs_put (p->cs_link, rq->buf_out, rq->len_out)) < 0)
4133 logf (LOG_DEBUG, "cs_put write fail");
4135 xfree (rq->buf_out);
4137 ir_tcl_disconnect (p);
4140 p->failInfo = IR_TCL_FAIL_WRITE;
4141 ir_tcl_eval (p->interp, p->failback);
4143 ir_obj_delete ((ClientData) p);
4145 else if (r == 0) /* remove select bit */
4147 logf (LOG_DEBUG, "Write completed");
4148 p->state = IR_TCL_R_Waiting;
4149 ir_select_remove_write (cs_fileno (p->cs_link), p);
4150 xfree (rq->buf_out);
4156 static void ir_select_notify (ClientData clientData, int r, int w, int e)
4160 if (!ir_select_write (clientData) && r)
4161 ir_select_read (clientData);
4165 ir_select_read (clientData);
4169 /*----------------------------------------------------------- */
4173 * This wrapper function is used by Windows to invoke the
4174 * initialization code for the DLL. If we are compiling
4175 * with Visual C++, this routine will be renamed to DllMain.
4187 DllEntryPoint(hInst, reason, reserved)
4188 HINSTANCE hInst; /* Library instance handle. */
4189 DWORD reason; /* Reason this function is being called. */
4190 LPVOID reserved; /* Not used. */
4196 /* ------------------------------------------------------- */
4198 * Irtcl_init: Registration of TCL commands.
4200 EXPORT (int,Irtcl_Init) (Tcl_Interp *interp)
4202 Tcl_CreateCommand (interp, "ir", ir_obj_mk, (ClientData) NULL,
4203 (Tcl_CmdDeleteProc *) NULL);
4204 Tcl_CreateCommand (interp, "ir-set", ir_set_obj_mk,
4205 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
4206 Tcl_CreateCommand (interp, "ir-scan", ir_scan_obj_mk,
4207 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
4208 Tcl_CreateCommand (interp, "ir-log-init", ir_log_init_proc,
4209 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
4210 Tcl_CreateCommand (interp, "ir-log", ir_log_proc,
4211 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
4212 Tcl_CreateCommand (interp, "ir-version", ir_version, (ClientData) NULL,
4213 (Tcl_CmdDeleteProc *) NULL);