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.107 1998-06-10 13:00:46 adam
9 * Added ir-version command.
11 * Revision 1.106 1998/05/20 12:25:35 adam
12 * Fixed bug that occurred in rare cases when encoding of incoming
15 * Revision 1.105 1998/04/02 14:31:08 adam
16 * This version works with compiled ASN.1 code.
18 * Revision 1.104 1998/02/27 14:26:07 adam
19 * Changed client so that it still works if target sets numberOfRecords
20 * in response to an illegal value.
22 * Revision 1.103 1997/11/19 11:22:10 adam
23 * Object identifiers can be accessed in GRS-1 records.
25 * Revision 1.102 1997/09/17 12:22:40 adam
26 * Changed to use YAZ version 1.4. The new comstack utility, cs_straddr,
29 * Revision 1.101 1997/09/09 10:19:53 adam
30 * New MSV5.0 port with fewer warnings.
32 * Revision 1.100 1997/05/01 15:04:05 adam
33 * Added ir-log command.
35 * Revision 1.99 1997/04/30 07:24:47 adam
36 * Spell fix of an error message.
38 * Revision 1.98 1997/04/13 18:57:20 adam
39 * Better error reporting and aligned with Tcl/Tk style.
40 * Rework of notifier code with Tcl_File handles.
42 * Revision 1.97 1996/11/14 17:11:07 adam
43 * Added Explain documentaion.
45 * Revision 1.96 1996/10/08 13:02:50 adam
46 * When dealing with records, odr_choice_enable_bias function is used to
47 * prevent decoding of externals.
49 * Revision 1.95 1996/09/13 10:51:49 adam
50 * Bug fix: ir_tcl_select_set called Tcl_GetFile at disconnect.
52 * Revision 1.94 1996/08/21 13:32:53 adam
53 * Implemented saveFile method and extended loadFile method to work with it.
55 * Revision 1.93 1996/08/16 15:07:45 adam
56 * First work on Explain.
58 * Revision 1.92 1996/08/09 15:33:07 adam
59 * Modified the code to use tk4.1/tcl7.5 patch level 1. The time-driven
60 * polling is no longer activated on Windows since asynchrounous I/O works
63 * Revision 1.91 1996/07/03 13:31:11 adam
64 * The xmalloc/xfree functions from YAZ are used to manage memory.
66 * Revision 1.90 1996/06/27 14:21:00 adam
67 * Yet another Windows port.
69 * Revision 1.89 1996/06/11 15:27:15 adam
70 * Event type set to connect a little earlier in the do_connect function.
72 * Revision 1.88 1996/06/03 09:04:22 adam
73 * Changed a few logf calls.
75 * Revision 1.87 1996/05/29 06:37:51 adam
76 * Function ir_tcl_get_grs_r enhanced so that specific elements can be
79 * Revision 1.86 1996/03/20 13:54:04 adam
80 * The Tcl_File structure is only manipulated in the Tk-event interface
83 * Revision 1.85 1996/03/15 11:15:48 adam
84 * Modified to use new prototypes for p_query_rpn and p_query_scan.
86 * Revision 1.84 1996/03/07 12:42:49 adam
87 * Better logging when callback is invoked.
89 * Revision 1.83 1996/03/05 09:21:09 adam
90 * Bug fix: memory used by GRS records wasn't freed.
91 * Rewrote some of the error handling code - the connection is always
92 * closed before failback is called.
93 * If failback is defined the send APDU methods (init, search, ...) will
94 * return OK but invoke failback (as is the case if the write operation
96 * Bug fix: ref_count in assoc object could grow if fraction of PDU was
99 * Revision 1.82 1996/02/29 15:30:21 adam
100 * Export of IrTcl functionality to extensions.
102 * Revision 1.81 1996/02/26 18:38:32 adam
103 * Work on export of set methods.
105 * Revision 1.80 1996/02/23 17:31:39 adam
106 * More functions made available to the wais tcl extension.
108 * Revision 1.79 1996/02/23 13:41:38 adam
109 * Work on public access to simple ir class system.
111 * Revision 1.78 1996/02/21 10:16:08 adam
112 * Simplified select handling. Only one function ir_tcl_select_set has
113 * to be externally defined.
115 * Revision 1.77 1996/02/20 17:52:58 adam
116 * Uses the YAZ oid system to name record syntax object identifiers.
118 * Revision 1.76 1996/02/20 16:09:51 adam
119 * Bug fix: didn't set element set names stamp correctly on result
120 * set records when element set names were set to the empty string.
122 * Revision 1.75 1996/02/19 15:41:53 adam
123 * Better log messages.
124 * Minor improvement of connect method.
126 * Revision 1.74 1996/02/05 17:58:03 adam
127 * Ported ir-tcl to use the beta releases of tcl7.5/tk4.1.
129 * Revision 1.73 1996/01/29 11:35:19 adam
130 * Bug fix: cs_type member renamed to comstackType to avoid conflict with
131 * cs_type macro defined by YAZ.
133 * Revision 1.72 1996/01/19 17:45:34 quinn
134 * Added debugging output
136 * Revision 1.71 1996/01/19 16:22:38 adam
137 * New method: apduDump - returns information about last incoming APDU.
139 * Revision 1.70 1996/01/10 09:18:34 adam
140 * PDU specific callbacks implemented: initRespnse, searchResponse,
141 * presentResponse and scanResponse.
142 * Bug fix in the command line shell (tclmain.c) - discovered on OSF/1.
144 * Revision 1.69 1996/01/04 16:12:12 adam
145 * Setting PDUType renamed to eventType.
147 * Revision 1.68 1996/01/04 11:05:22 adam
148 * New setting: PDUType - returns type of last PDU returned from the target.
149 * Fixed a bug in configure/Makefile.
151 * Revision 1.67 1996/01/03 09:00:51 adam
152 * Updated to use new version of Yaz (names changed to avoid C++ conflict).
154 * Revision 1.66 1995/11/28 17:26:39 adam
155 * Removed Carriage return from ir-tcl.c!
156 * Removed misc. debug logs.
158 * Revision 1.65 1995/11/28 13:53:00 quinn
161 * Revision 1.64 1995/11/13 15:39:18 adam
162 * Bug fix: {small,medium}SetElementSetNames weren't set correctly.
163 * Bug fix: idAuthentication weren't set correctly.
165 * Revision 1.63 1995/11/13 09:55:39 adam
166 * Multiple records at a position in a result-set with differnt
169 * Revision 1.62 1995/10/18 17:20:33 adam
170 * Work on target setup in client.tcl.
172 * Revision 1.61 1995/10/18 16:42:42 adam
173 * New settings: smallSetElementSetNames and mediumSetElementSetNames.
175 * Revision 1.60 1995/10/18 15:43:31 adam
176 * In search: mediumSetElementSetNames and smallSetElementSetNames are
177 * set to elementSetNames.
179 * Revision 1.59 1995/10/17 12:18:58 adam
180 * Bug fix: when target connection closed, the connection was not
181 * properly reestablished.
183 * Revision 1.58 1995/10/16 17:00:55 adam
184 * New setting: elementSetNames.
185 * Various client improvements. Medium presentation format looks better.
187 * Revision 1.57 1995/09/21 13:11:51 adam
188 * Support of dynamic loading.
189 * Test script uses load command if necessary.
191 * Revision 1.56 1995/08/29 15:30:14 adam
192 * Work on GRS records.
194 * Revision 1.55 1995/08/28 09:43:25 adam
195 * Minor changes. configure only searches for yaz beta 3 and versions after
198 * Revision 1.54 1995/08/24 12:25:16 adam
199 * Modified to work with yaz 1.0b3.
201 * Revision 1.53 1995/08/04 12:49:26 adam
202 * Bug fix: reading uninitialized variable p.
204 * Revision 1.52 1995/08/04 11:32:38 adam
205 * More work on output queue. Memory related routines moved
208 * Revision 1.51 1995/08/03 13:22:54 adam
211 * Revision 1.50 1995/07/20 08:09:49 adam
212 * client.tcl: Targets removed from hotTargets list when targets
213 * are removed/modified.
214 * ir-tcl.c: More work on triggerResourceControl.
216 * Revision 1.49 1995/06/30 12:39:21 adam
217 * Bug fix: loadFile didn't set record type.
218 * The MARC routines are a little less strict in the interpretation.
219 * Script display.tcl replaces the old marc.tcl.
220 * New interactive script: shell.tcl.
222 * Revision 1.48 1995/06/27 19:03:50 adam
223 * Bug fix in do_present in ir-tcl.c: p->set_child member weren't set.
224 * nextResultSetPosition used instead of setOffset.
226 * Revision 1.47 1995/06/25 10:25:04 adam
227 * Working on triggerResourceControl. Description of compile/install
228 * procedure moved to ir-tcl.sgml.
230 * Revision 1.46 1995/06/22 13:15:06 adam
231 * Feature: SUTRS. Setting getSutrs implemented.
232 * Work on display formats.
233 * Preferred record syntax can be set by the user.
235 * Revision 1.45 1995/06/20 08:07:30 adam
236 * New setting: failInfo.
237 * Working on better cancel mechanism.
239 * Revision 1.44 1995/06/19 17:01:20 adam
242 * Revision 1.43 1995/06/19 13:06:08 adam
243 * New define: IR_TCL_VERSION.
245 * Revision 1.42 1995/06/19 08:08:52 adam
246 * client.tcl: hotTargets now contain both database and target name.
247 * ir-tcl.c: setting protocol edited. Errors in callbacks are logged
248 * by logf(LOG_WARN, ...) calls.
250 * Revision 1.41 1995/06/16 12:28:16 adam
251 * Implemented preferredRecordSyntax.
252 * Minor changes in diagnostic handling.
253 * Record list deleted when connection closes.
255 * Revision 1.40 1995/06/14 13:37:18 adam
256 * Setting recordType implemented.
257 * Setting implementationVersion implemented.
258 * Settings implementationId / implementationName edited.
260 * Revision 1.39 1995/06/08 10:26:32 adam
261 * Bug fix in ir_strdup.
263 * Revision 1.38 1995/06/01 16:36:47 adam
264 * About buttons. Minor bug fixes.
266 * Revision 1.37 1995/06/01 07:31:20 adam
267 * Rename of many typedefs -> IrTcl_...
269 * Revision 1.36 1995/05/31 13:09:59 adam
270 * Client searches/presents may be interrupted.
271 * New moving book-logo.
273 * Revision 1.35 1995/05/31 08:36:33 adam
274 * Bug fix in client.tcl: didn't save options on clientrc.tcl.
275 * New method: referenceId. More work on scan.
277 * Revision 1.34 1995/05/29 10:33:42 adam
278 * README and rename of startup script.
280 * Revision 1.33 1995/05/29 09:15:11 quinn
281 * Changed CS_SR to PROTO_SR, etc.
283 * Revision 1.32 1995/05/29 08:44:16 adam
284 * Work on delete of objects.
286 * Revision 1.31 1995/05/26 11:44:10 adam
287 * Bugs fixed. More work on MARC utilities and queries. Test
288 * client is up-to-date again.
290 * Revision 1.30 1995/05/26 08:54:11 adam
291 * New MARC utilities. Uses prefix query.
293 * Revision 1.29 1995/05/24 14:10:22 adam
294 * Work on idAuthentication, protocolVersion and options.
296 * Revision 1.28 1995/05/23 15:34:48 adam
297 * Many new settings, userInformationField, smallSetUpperBound, etc.
298 * A number of settings are inherited when ir-set is executed.
299 * This version is incompatible with the graphical test client (client.tcl).
301 * Revision 1.27 1995/05/11 15:34:47 adam
302 * Scan request changed a bit. This version works with RLG.
304 * Revision 1.26 1995/04/18 16:11:51 adam
305 * First version of graphical Scan. Some work on query-by-form.
307 * Revision 1.25 1995/04/17 09:37:17 adam
308 * Further development of scan.
310 * Revision 1.24 1995/04/11 14:16:42 adam
311 * Further work on scan. Response works. Entries aren't saved yet.
313 * Revision 1.23 1995/04/10 10:50:27 adam
314 * Result-set name defaults to suffix of ir-set name.
315 * Started working on scan. Not finished at this point.
317 * Revision 1.22 1995/03/31 10:43:03 adam
318 * More robust when getting bad MARC records.
320 * Revision 1.21 1995/03/31 08:56:37 adam
321 * New button "Search".
323 * Revision 1.20 1995/03/29 16:07:09 adam
324 * Bug fix: Didn't use setName in present request.
326 * Revision 1.19 1995/03/28 12:45:23 adam
327 * New ir method failback: called on disconnect/protocol error.
328 * New ir set/get method: protocol: SR / Z3950.
329 * Simple popup and disconnect when failback is invoked.
331 * Revision 1.18 1995/03/21 15:50:12 adam
334 * Revision 1.17 1995/03/21 13:41:03 adam
335 * Comstack cs_create not used too often. Non-blocking connect.
337 * Revision 1.16 1995/03/21 08:26:06 adam
338 * New method, setName, to specify the result set name (other than Default).
339 * New method, responseStatus, which returns diagnostic info, if any, after
340 * present response / search response.
342 * Revision 1.15 1995/03/20 15:24:07 adam
343 * Diagnostic records saved on searchResponse.
345 * Revision 1.14 1995/03/20 08:53:22 adam
346 * Event loop in tclmain.c rewritten. New method searchStatus.
348 * Revision 1.13 1995/03/17 18:26:17 adam
349 * Non-blocking i/o used now. Database names popup as cascade items.
351 * Revision 1.12 1995/03/17 15:45:00 adam
352 * Improved target/database setup.
354 * Revision 1.11 1995/03/16 17:54:03 adam
355 * Minor changes really.
357 * Revision 1.10 1995/03/15 16:14:50 adam
358 * Blocking arg in cs_create changed.
360 * Revision 1.9 1995/03/15 13:59:24 adam
363 * Revision 1.8 1995/03/15 08:25:16 adam
364 * New method presentStatus to check for error on present. Misc. cleanup
365 * of IrTcl_RecordList manipulations. Full MARC record presentation in
368 * Revision 1.7 1995/03/14 17:32:29 adam
369 * Presentation of full Marc record in popup window.
371 * Revision 1.6 1995/03/12 19:31:55 adam
372 * Pattern matching implemented when retrieving MARC records. More
373 * diagnostic functions.
375 * Revision 1.5 1995/03/10 18:00:15 adam
376 * Actual presentation in line-by-line format. RPN query support.
378 * Revision 1.4 1995/03/09 16:15:08 adam
379 * First presentRequest attempts. Hot-target list.
397 #if defined(__WIN32__)
398 # define WIN32_LEAN_AND_MEAN
399 # include <windows.h>
400 # undef WIN32_LEAN_AND_MEAN
403 * VC++ has an alternate entry point called DllMain, so we need to rename
407 # if defined(_MSC_VER)
408 # define EXPORT(a,b) __declspec(dllexport) a b
409 # define DllEntryPoint DllMain
411 # if defined(__BORLANDC__)
412 # define EXPORT(a,b) a _export b
414 # define EXPORT(a,b) a b
418 # define EXPORT(a,b) a b
421 static char *wrongArgs = "wrong # args: should be \"";
423 static int ir_tcl_error_exec (Tcl_Interp *interp, int argc, char **argv)
426 Tcl_AppendResult (interp, " while executing ", NULL);
427 for (i = 0; i<argc; i++)
428 Tcl_AppendResult (interp, (i ? " " : "\""), argv[i], NULL);
429 Tcl_AppendResult (interp, "\"", NULL);
434 static void ir_deleteDiags (IrTcl_Diagnostic **dst_list, int *dst_num);
436 static void ir_select_notify (ClientData clientData, int r, int w, int e);
438 void ir_select_add (int fd, void *obj)
440 ir_tcl_select_set (ir_select_notify, fd, obj, 1, 0, 0);
443 void ir_select_add_write (int fd, void *obj)
445 ir_tcl_select_set (ir_select_notify, fd, obj, 1, 1, 0);
448 void ir_select_remove (int fd, void *obj)
450 ir_tcl_select_set (NULL, fd, obj, 0, 0, 0);
453 void ir_select_remove_write (int fd, void *obj)
455 ir_tcl_select_set (ir_select_notify, fd, obj, 1, 0, 0);
458 static void delete_IR_record (IrTcl_RecordList *rl)
462 case Z_NamePlusRecord_databaseRecord:
463 switch (rl->u.dbrec.type)
466 ir_tcl_grs_del (&rl->u.dbrec.u.grs1);
471 xfree (rl->u.dbrec.buf);
472 rl->u.dbrec.buf = NULL;
474 case Z_NamePlusRecord_surrogateDiagnostic:
475 ir_deleteDiags (&rl->u.surrogateDiagnostics.list,
476 &rl->u.surrogateDiagnostics.num);
479 xfree (rl->elements);
482 static IrTcl_RecordList *new_IR_record (IrTcl_SetObj *setobj,
484 const char *elements)
486 IrTcl_RecordList *rl;
488 if (elements && !*elements)
490 for (rl = setobj->record_list; rl; rl = rl->next)
492 if (no == rl->no && (!rl->elements || !elements ||
493 !strcmp(elements, rl->elements)))
495 delete_IR_record (rl);
501 rl = ir_tcl_malloc (sizeof(*rl));
502 rl->next = setobj->record_list;
504 setobj->record_list = rl;
507 ir_tcl_strdup (NULL, &rl->elements, elements);
514 int ir_tcl_eval (Tcl_Interp *interp, const char *command)
516 char *tmp = ir_tcl_malloc (strlen(command)+1);
519 logf (LOG_DEBUG, "Invoking %.23s ...", command);
520 strcpy (tmp, command);
521 r = Tcl_Eval (interp, tmp);
524 const char *errorInfo = Tcl_GetVar (interp, "errorInfo", 0);
525 logf (LOG_WARN, "Tcl error in line %d: %s\n%s", interp->errorLine,
526 interp->result, errorInfo ? errorInfo : "<null>");
528 Tcl_FreeResult (interp);
534 * IrTcl_getRecordSyntaxStr: Return record syntax name of object id
536 static char *IrTcl_getRecordSyntaxStr (enum oid_value value)
539 struct oident ent, *entp;
541 ent.proto = PROTO_Z3950;
542 ent.oclass = CLASS_RECSYN;
545 o = oid_getoidbyent (&ent);
546 entp = oid_getentbyoid (o);
554 * IrTcl_getRecordSyntaxVal: Return record syntax value of string
556 static enum oid_value IrTcl_getRecordSyntaxVal (const char *name)
558 return oid_getvalbyname (name);
561 static IrTcl_RecordList *find_IR_record (IrTcl_SetObj *setobj, int no)
563 IrTcl_RecordList *rl;
565 for (rl = setobj->record_list; rl; rl = rl->next)
567 (!setobj->recordElements || !rl->elements ||
568 !strcmp (setobj->recordElements, rl->elements)))
573 static void delete_IR_records (IrTcl_SetObj *setobj)
575 IrTcl_RecordList *rl, *rl1;
577 for (rl = setobj->record_list; rl; rl = rl1)
579 delete_IR_record (rl);
583 setobj->record_list = NULL;
587 * ir_tcl_get_set_int: Set/get integer value
589 int ir_tcl_get_set_int (int *val, Tcl_Interp *interp, int argc, char **argv)
595 if (Tcl_GetInt (interp, argv[2], val)==TCL_ERROR)
598 sprintf (buf, "%d", *val);
599 Tcl_AppendResult (interp, buf, NULL);
605 * ir_tcl_method_error
607 int ir_tcl_method_error (Tcl_Interp *interp, int argc, char **argv,
610 IrTcl_Methods *tab_i = tab;
613 Tcl_AppendResult (interp, "bad method: \"", *argv, " ", argv[1],
614 "\"\nmethod should be of:", NULL);
615 for (tab_i = tab; tab_i->tab; tab_i++)
616 for (t = tab_i->tab; t->name; t++)
617 Tcl_AppendResult (interp, " ", t->name, NULL);
622 * ir_tcl_method: Search for method in table and invoke method handler
624 int ir_tcl_method (Tcl_Interp *interp, int argc, char **argv,
625 IrTcl_Methods *tab, int *ret)
627 IrTcl_Methods *tab_i = tab;
630 for (tab_i = tab; tab_i->tab; tab_i++)
631 for (t = tab_i->tab; t->name; t++)
634 if ((*t->method)(tab_i->obj, interp, argc, argv) == TCL_ERROR)
638 if (!strcmp (t->name, argv[1]))
640 *ret = (*t->method)(tab_i->obj, interp, argc, argv);
651 * ir_tcl_named_bits: get/set named bits
653 int ir_tcl_named_bits (struct ir_named_entry *tab, Odr_bitmask *ob,
654 Tcl_Interp *interp, int argc, char **argv)
656 struct ir_named_entry *ti;
661 for (no = 0; no < argc; no++)
664 for (ti = tab; ti->name; ti++)
665 if (!strcmp(argv[no], "@all") || !strcmp (argv[no], ti->name))
667 ODR_MASK_SET (ob, ti->pos);
672 Tcl_AppendResult (interp, "bad bit mask ", argv[no], NULL);
673 return ir_tcl_error_exec (interp, argc, argv);
678 for (ti = tab; ti->name; ti++)
679 if (ODR_MASK_GET (ob, ti->pos))
680 Tcl_AppendElement (interp, ti->name);
684 static void set_referenceId (ODR o, Z_ReferenceId **dst, const char *src)
690 *dst = odr_malloc (o, sizeof(**dst));
691 (*dst)->size = (*dst)->len = strlen(src);
692 (*dst)->buf = odr_malloc (o, (*dst)->len);
693 memcpy ((*dst)->buf, src, (*dst)->len);
697 static void get_referenceId (char **dst, Z_ReferenceId *src)
705 *dst = ir_tcl_malloc (src->len+1);
706 memcpy (*dst, src->buf, src->len);
707 (*dst)[src->len] = '\0';
710 /* ------------------------------------------------------- */
713 * do_init_request: init method on IR object
715 static int do_init_request (void *obj, Tcl_Interp *interp,
716 int argc, char **argv)
724 logf (LOG_DEBUG, "init %s", *argv);
727 Tcl_AppendResult (interp, "not connected", NULL);
728 return ir_tcl_error_exec (interp, argc, argv);
730 apdu = zget_APDU (p->odr_out, Z_APDU_initRequest);
731 req = apdu->u.initRequest;
733 set_referenceId (p->odr_out, &req->referenceId, p->set_inher.referenceId);
734 req->options = &p->options;
735 req->protocolVersion = &p->protocolVersion;
736 req->preferredMessageSize = &p->preferredMessageSize;
737 req->maximumRecordSize = &p->maximumRecordSize;
739 if (p->idAuthenticationGroupId)
741 Z_IdPass *pass = odr_malloc (p->odr_out, sizeof(*pass));
742 Z_IdAuthentication *auth = odr_malloc (p->odr_out, sizeof(*auth));
744 logf (LOG_DEBUG, "using pass authentication");
746 auth->which = Z_IdAuthentication_idPass;
747 auth->u.idPass = pass;
748 if (p->idAuthenticationGroupId && *p->idAuthenticationGroupId)
749 pass->groupId = p->idAuthenticationGroupId;
751 pass->groupId = NULL;
752 if (p->idAuthenticationUserId && *p->idAuthenticationUserId)
753 pass->userId = p->idAuthenticationUserId;
756 if (p->idAuthenticationPassword && *p->idAuthenticationPassword)
757 pass->password = p->idAuthenticationPassword;
759 pass->password = NULL;
760 req->idAuthentication = auth;
762 else if (!p->idAuthenticationOpen || !*p->idAuthenticationOpen)
763 req->idAuthentication = NULL;
766 Z_IdAuthentication *auth = odr_malloc (p->odr_out, sizeof(*auth));
768 logf (LOG_DEBUG, "using open authentication");
769 auth->which = Z_IdAuthentication_open;
770 auth->u.open = p->idAuthenticationOpen;
771 req->idAuthentication = auth;
773 req->implementationId = p->implementationId;
774 req->implementationName = p->implementationName;
775 req->implementationVersion = p->implementationVersion;
776 req->userInformationField = 0;
778 return ir_tcl_send_APDU (interp, p, apdu, "init", *argv);
782 * do_protocolVersion: Set protocol Version
784 static int do_protocolVersion (void *obj, Tcl_Interp *interp,
785 int argc, char **argv)
793 ODR_MASK_ZERO (&p->protocolVersion);
794 ODR_MASK_SET (&p->protocolVersion, 0);
795 ODR_MASK_SET (&p->protocolVersion, 1);
800 if (Tcl_GetInt (interp, argv[2], &version)==TCL_ERROR)
802 ODR_MASK_ZERO (&p->protocolVersion);
803 for (i = 0; i<version; i++)
804 ODR_MASK_SET (&p->protocolVersion, i);
806 for (i = 4; --i >= 0; )
807 if (ODR_MASK_GET (&p->protocolVersion, i))
809 sprintf (buf, "%d", i+1);
810 interp->result = buf;
815 * do_options: Set options
817 static int do_options (void *obj, Tcl_Interp *interp,
818 int argc, char **argv)
820 static struct ir_named_entry options_tab[] = {
824 { "resourceReport", 3 },
825 { "triggerResourceCtrl", 4},
826 { "resourceCtrl", 5},
830 { "extendedServices", 10},
831 { "level-1Segmentation", 11},
832 { "level-2Segmentation", 12},
833 { "concurrentOperations", 13},
834 { "namedResultSets", 14},
841 ODR_MASK_ZERO (&p->options);
842 ODR_MASK_SET (&p->options, 0);
843 ODR_MASK_SET (&p->options, 1);
844 ODR_MASK_SET (&p->options, 4);
845 ODR_MASK_SET (&p->options, 7);
846 ODR_MASK_SET (&p->options, 14);
849 return ir_tcl_named_bits (options_tab, &p->options, interp, argc-2, argv+2);
853 * do_apduInfo: Get APDU information
855 static int do_apduInfo (void *obj, Tcl_Interp *interp, int argc, char **argv)
863 sprintf (buf, "%d", p->apduLen);
864 Tcl_AppendElement (interp, buf);
865 sprintf (buf, "%d", p->apduOffset);
866 Tcl_AppendElement (interp, buf);
869 Tcl_AppendElement (interp, "");
872 apduf = fopen ("apdu.tmp", "w");
875 Tcl_AppendElement (interp, "");
878 odr_dumpBER (apduf, p->buf_in, p->apduLen);
880 if (!(apduf = fopen ("apdu.tmp", "r")))
881 Tcl_AppendElement (interp, "");
886 Tcl_AppendResult (interp, " {", NULL);
887 while ((c = getc (apduf)) != EOF)
891 Tcl_AppendResult (interp, buf, NULL);
894 Tcl_AppendResult (interp, "}", NULL);
901 * do_failInfo: Get fail information
903 static int do_failInfo (void *obj, Tcl_Interp *interp, int argc, char **argv)
913 sprintf (buf, "%d", p->failInfo);
919 case IR_TCL_FAIL_CONNECT:
920 cp = "connect failed";
922 case IR_TCL_FAIL_READ:
923 cp = "connection closed";
925 case IR_TCL_FAIL_WRITE:
926 cp = "connection closed";
928 case IR_TCL_FAIL_IN_APDU:
929 cp = "failed to decode incoming APDU";
931 case IR_TCL_FAIL_UNKNOWN_APDU:
937 Tcl_AppendElement (interp, buf);
938 Tcl_AppendElement (interp, cp);
943 * do_preferredMessageSize: Set/get preferred message size
945 static int do_preferredMessageSize (void *obj, Tcl_Interp *interp,
946 int argc, char **argv)
952 p->preferredMessageSize = 30000;
955 return ir_tcl_get_set_int (&p->preferredMessageSize, interp, argc, argv);
959 * do_maximumRecordSize: Set/get maximum record size
961 static int do_maximumRecordSize (void *obj, Tcl_Interp *interp,
962 int argc, char **argv)
968 p->maximumRecordSize = 30000;
971 return ir_tcl_get_set_int (&p->maximumRecordSize, interp, argc, argv);
975 * do_initResult: Get init result
977 static int do_initResult (void *obj, Tcl_Interp *interp,
978 int argc, char **argv)
984 return ir_tcl_get_set_int (&p->initResult, interp, argc, argv);
989 * do_implementationName: Set/get Implementation Name.
991 static int do_implementationName (void *obj, Tcl_Interp *interp,
992 int argc, char **argv)
997 return ir_tcl_strdup (interp, &p->implementationName,
998 "Index Data/IrTcl on YAZ");
1000 return ir_tcl_strdel (interp, &p->implementationName);
1003 xfree (p->implementationName);
1004 if (ir_tcl_strdup (interp, &p->implementationName, argv[2])
1008 Tcl_AppendResult (interp, p->implementationName, (char*) NULL);
1013 * do_implementationId: Get Implementation Id.
1015 static int do_implementationId (void *obj, Tcl_Interp *interp,
1016 int argc, char **argv)
1021 return ir_tcl_strdup (interp, &p->implementationId, "YAZ (id=81)");
1022 else if (argc == -1)
1023 return ir_tcl_strdel (interp, &p->implementationId);
1024 Tcl_AppendResult (interp, p->implementationId, (char*) NULL);
1029 * do_implementationVersion: get Implementation Version.
1031 static int do_implementationVersion (void *obj, Tcl_Interp *interp,
1032 int argc, char **argv)
1037 return ir_tcl_strdup (interp, &p->implementationVersion,
1039 #ifdef IR_TCL_VERSION
1040 " / Irtcl: " IR_TCL_VERSION
1043 else if (argc == -1)
1044 return ir_tcl_strdel (interp, &p->implementationVersion);
1045 Tcl_AppendResult (interp, p->implementationVersion, (char*) NULL);
1050 * do_targetImplementationName: Get Implementation Name of target.
1052 static int do_targetImplementationName (void *obj, Tcl_Interp *interp,
1053 int argc, char **argv)
1059 p->targetImplementationName = NULL;
1062 else if (argc == -1)
1063 return ir_tcl_strdel (interp, &p->targetImplementationName);
1064 Tcl_AppendResult (interp, p->targetImplementationName, (char*) NULL);
1069 * do_targetImplementationId: Get Implementation Id of target
1071 static int do_targetImplementationId (void *obj, Tcl_Interp *interp,
1072 int argc, char **argv)
1078 p->targetImplementationId = NULL;
1081 else if (argc == -1)
1082 return ir_tcl_strdel (interp, &p->targetImplementationId);
1083 Tcl_AppendResult (interp, p->targetImplementationId, (char*) NULL);
1088 * do_targetImplementationVersion: Get Implementation Version of target
1090 static int do_targetImplementationVersion (void *obj, Tcl_Interp *interp,
1091 int argc, char **argv)
1097 p->targetImplementationVersion = NULL;
1100 else if (argc == -1)
1101 return ir_tcl_strdel (interp, &p->targetImplementationVersion);
1102 Tcl_AppendResult (interp, p->targetImplementationVersion, (char*) NULL);
1107 * do_idAuthentication: Set/get id Authentication
1109 static int do_idAuthentication (void *obj, Tcl_Interp *interp,
1110 int argc, char **argv)
1114 if (argc >= 3 || argc == -1)
1116 xfree (p->idAuthenticationOpen);
1117 xfree (p->idAuthenticationGroupId);
1118 xfree (p->idAuthenticationUserId);
1119 xfree (p->idAuthenticationPassword);
1121 if (argc >= 3 || argc <= 0)
1123 p->idAuthenticationOpen = NULL;
1124 p->idAuthenticationGroupId = NULL;
1125 p->idAuthenticationUserId = NULL;
1126 p->idAuthenticationPassword = NULL;
1135 ir_tcl_strdup (interp, &p->idAuthenticationOpen, argv[2])
1142 ir_tcl_strdup (interp, &p->idAuthenticationGroupId, argv[2])
1146 ir_tcl_strdup (interp, &p->idAuthenticationUserId, argv[3])
1150 ir_tcl_strdup (interp, &p->idAuthenticationPassword, argv[4])
1155 if (p->idAuthenticationOpen)
1156 Tcl_AppendElement (interp, p->idAuthenticationOpen);
1157 else if (p->idAuthenticationGroupId)
1159 Tcl_AppendElement (interp, p->idAuthenticationGroupId);
1160 Tcl_AppendElement (interp, p->idAuthenticationUserId);
1161 Tcl_AppendElement (interp, p->idAuthenticationPassword);
1167 * do_connect: connect method on IR object
1169 static int do_connect (void *obj, Tcl_Interp *interp,
1170 int argc, char **argv)
1180 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
1181 " ?hostname?\"", NULL);
1186 Tcl_AppendResult (interp, p->hostname, NULL);
1190 logf (LOG_DEBUG, "connect %s %s", *argv, argv[2]);
1193 Tcl_AppendResult (interp, "already connected", NULL);
1194 return ir_tcl_error_exec (interp, argc, argv);
1196 if (!strcmp (p->comstackType, "tcpip"))
1198 p->cs_link = cs_create (tcpip_type, CS_BLOCK, p->protocol_type);
1199 logf (LOG_DEBUG, "tcp/ip connect %s", argv[2]);
1201 else if (!strcmp (p->comstackType, "mosi"))
1204 p->cs_link = cs_create (mosi_type, CS_BLOCK, p->protocol_type);
1205 logf (LOG_DEBUG, "mosi connect %s", argv[2]);
1207 Tcl_AppendResult (interp, "mosi not supported", NULL);
1208 return ir_tcl_error_exec (interp, argc, argv);
1213 Tcl_AppendResult (interp, "bad comstack type ",
1214 p->comstackType, NULL);
1215 return ir_tcl_error_exec (interp, argc, argv);
1217 if (ir_tcl_strdup (interp, &p->hostname, argv[2]) == TCL_ERROR)
1219 p->eventType = "connect";
1220 addr = cs_straddr (p->cs_link, argv[2]);
1223 ir_tcl_disconnect (p);
1224 Tcl_AppendResult (interp, "cs_straddr fail", NULL);
1225 return ir_tcl_error_exec (interp, argc, argv);
1227 if ((r=cs_connect (p->cs_link, addr)) < 0)
1229 ir_tcl_disconnect (p);
1230 Tcl_AppendResult (interp, "connect fail", NULL);
1231 return ir_tcl_error_exec (interp, argc, argv);
1233 ir_select_add (cs_fileno (p->cs_link), p);
1236 logf (LOG_DEBUG, "connect pending fd=%d", cs_fileno(p->cs_link));
1237 ir_select_add_write (cs_fileno (p->cs_link), p);
1238 p->state = IR_TCL_R_Connecting;
1242 logf (LOG_DEBUG, "connect ok fd=%d", cs_fileno(p->cs_link));
1243 p->state = IR_TCL_R_Idle;
1245 ir_tcl_eval (p->interp, p->callback);
1252 * ir_tcl_disconnect: close connection
1254 void ir_tcl_disconnect (IrTcl_Obj *p)
1258 logf(LOG_DEBUG, "Closing connection to %s", p->hostname);
1259 xfree (p->hostname);
1261 assert (p->cs_link);
1262 ir_select_remove (cs_fileno (p->cs_link), p);
1264 odr_reset (p->odr_in);
1266 #if TCL_MAJOR_VERSION == 8
1267 cs_fileno(p->cs_link) = -1;
1269 cs_close (p->cs_link);
1272 ODR_MASK_ZERO (&p->options);
1273 ODR_MASK_SET (&p->options, 0);
1274 ODR_MASK_SET (&p->options, 1);
1275 ODR_MASK_SET (&p->options, 4);
1276 ODR_MASK_SET (&p->options, 7);
1277 ODR_MASK_SET (&p->options, 14);
1279 ODR_MASK_ZERO (&p->protocolVersion);
1280 ODR_MASK_SET (&p->protocolVersion, 0);
1281 ODR_MASK_SET (&p->protocolVersion, 1);
1284 assert (!p->cs_link);
1288 * do_disconnect: disconnect method on IR object
1290 static int do_disconnect (void *obj, Tcl_Interp *interp,
1291 int argc, char **argv)
1297 p->state = IR_TCL_R_Idle;
1298 p->eventType = NULL;
1303 ir_tcl_disconnect (p);
1308 * do_comstack: Set/get comstack method on IR object
1310 static int do_comstack (void *o, Tcl_Interp *interp,
1311 int argc, char **argv)
1316 return ir_tcl_strdup (interp, &obj->comstackType, "tcpip");
1317 else if (argc == -1)
1318 return ir_tcl_strdel (interp, &obj->comstackType);
1321 xfree (obj->comstackType);
1322 if (ir_tcl_strdup (interp, &obj->comstackType, argv[2]) == TCL_ERROR)
1325 Tcl_AppendElement (interp, obj->comstackType);
1330 * do_logLevel: Set log level
1332 static int do_logLevel (void *o, Tcl_Interp *interp,
1333 int argc, char **argv)
1338 log_init (log_mask_str (argv[2]), "", NULL);
1340 log_init (log_mask_str (argv[2]), argv[3], NULL);
1342 log_init (log_mask_str (argv[2]), argv[3], argv[4]);
1348 * do_eventType: Return type of last event
1350 static int do_eventType (void *obj, Tcl_Interp *interp,
1351 int argc, char **argv)
1357 p->eventType = NULL;
1360 Tcl_AppendElement (interp, p->eventType ? p->eventType : "");
1366 * do_callback: add callback
1368 static int do_callback (void *obj, Tcl_Interp *interp,
1369 int argc, char **argv)
1378 else if (argc == -1)
1379 return ir_tcl_strdel (interp, &p->callback);
1382 xfree (p->callback);
1385 if (ir_tcl_strdup (interp, &p->callback, argv[2]) == TCL_ERROR)
1395 * do_failback: add error handle callback
1397 static int do_failback (void *obj, Tcl_Interp *interp,
1398 int argc, char **argv)
1407 else if (argc == -1)
1408 return ir_tcl_strdel (interp, &p->failback);
1411 xfree (p->failback);
1414 if (ir_tcl_strdup (interp, &p->failback, argv[2]) == TCL_ERROR)
1424 * do_initResponse: add init response handler
1426 static int do_initResponse (void *obj, Tcl_Interp *interp,
1427 int argc, char **argv)
1433 p->initResponse = NULL;
1436 else if (argc == -1)
1437 return ir_tcl_strdel (interp, &p->initResponse);
1440 xfree (p->initResponse);
1443 if (ir_tcl_strdup (interp, &p->initResponse, argv[2]) == TCL_ERROR)
1447 p->initResponse = NULL;
1452 * do_protocol: Set/get protocol method on IR object
1454 static int do_protocol (void *o, Tcl_Interp *interp, int argc, char **argv)
1460 p->protocol_type = PROTO_Z3950;
1465 if (!strcmp (argv[2], "Z39"))
1466 p->protocol_type = PROTO_Z3950;
1467 else if (!strcmp (argv[2], "SR"))
1468 p->protocol_type = PROTO_SR;
1471 Tcl_AppendResult (interp, "bad protocol ", argv[2], NULL);
1472 return ir_tcl_error_exec (interp, argc, argv);
1476 switch (p->protocol_type)
1479 Tcl_AppendElement (interp, "Z39");
1482 Tcl_AppendElement (interp, "SR");
1489 * do_triggerResourceControl:
1491 static int do_triggerResourceControl (void *obj, Tcl_Interp *interp,
1492 int argc, char **argv)
1496 Z_TriggerResourceControlRequest *req;
1497 bool_t is_false = 0;
1503 Tcl_AppendResult (interp, "not connected", NULL);
1504 return ir_tcl_error_exec (interp, argc, argv);
1506 apdu = zget_APDU (p->odr_out, Z_APDU_triggerResourceControlRequest);
1507 req = apdu->u.triggerResourceControlRequest;
1508 *req->requestedAction = Z_TriggerResourceCtrl_cancel;
1509 req->resultSetWanted = &is_false;
1511 return ir_tcl_send_APDU (interp, p, apdu, "triggerResourceControl",
1516 * do_databaseNames: specify database names
1518 static int do_databaseNames (void *obj, Tcl_Interp *interp,
1519 int argc, char **argv)
1522 IrTcl_SetCObj *p = obj;
1526 for (i=0; i<p->num_databaseNames; i++)
1527 xfree (p->databaseNames[i]);
1528 xfree (p->databaseNames);
1532 p->num_databaseNames = 0;
1533 p->databaseNames = NULL;
1538 for (i=0; i<p->num_databaseNames; i++)
1539 Tcl_AppendElement (interp, p->databaseNames[i]);
1542 if (p->databaseNames)
1544 for (i=0; i<p->num_databaseNames; i++)
1545 xfree (p->databaseNames[i]);
1546 xfree (p->databaseNames);
1548 p->num_databaseNames = argc - 2;
1550 ir_tcl_malloc (sizeof(*p->databaseNames) * (1+p->num_databaseNames));
1551 for (i=0; i<p->num_databaseNames; i++)
1553 if (ir_tcl_strdup (interp, &p->databaseNames[i], argv[2+i])
1557 p->databaseNames[i] = NULL;
1562 * do_replaceIndicator: Set/get replace Set indicator
1564 static int do_replaceIndicator (void *obj, Tcl_Interp *interp,
1565 int argc, char **argv)
1567 IrTcl_SetCObj *p = obj;
1571 p->replaceIndicator = 1;
1574 return ir_tcl_get_set_int (&p->replaceIndicator, interp, argc, argv);
1578 * do_queryType: Set/Get query method
1580 static int do_queryType (void *obj, Tcl_Interp *interp,
1581 int argc, char **argv)
1583 IrTcl_SetCObj *p = obj;
1586 return ir_tcl_strdup (interp, &p->queryType, "rpn");
1587 else if (argc == -1)
1588 return ir_tcl_strdel (interp, &p->queryType);
1591 xfree (p->queryType);
1592 if (ir_tcl_strdup (interp, &p->queryType, argv[2]) == TCL_ERROR)
1595 Tcl_AppendResult (interp, p->queryType, NULL);
1600 * do_userInformationField: Get User information field
1602 static int do_userInformationField (void *obj, Tcl_Interp *interp,
1603 int argc, char **argv)
1609 p->userInformationField = NULL;
1612 else if (argc == -1)
1613 return ir_tcl_strdel (interp, &p->userInformationField);
1614 Tcl_AppendResult (interp, p->userInformationField, NULL);
1619 * do_smallSetUpperBound: Set/get small set upper bound
1621 static int do_smallSetUpperBound (void *o, Tcl_Interp *interp,
1622 int argc, char **argv)
1624 IrTcl_SetCObj *p = o;
1628 p->smallSetUpperBound = 0;
1631 return ir_tcl_get_set_int (&p->smallSetUpperBound, interp, argc, argv);
1635 * do_largeSetLowerBound: Set/get large set lower bound
1637 static int do_largeSetLowerBound (void *o, Tcl_Interp *interp,
1638 int argc, char **argv)
1640 IrTcl_SetCObj *p = o;
1644 p->largeSetLowerBound = 2;
1647 return ir_tcl_get_set_int (&p->largeSetLowerBound, interp, argc, argv);
1651 * do_mediumSetPresentNumber: Set/get large set lower bound
1653 static int do_mediumSetPresentNumber (void *o, Tcl_Interp *interp,
1654 int argc, char **argv)
1656 IrTcl_SetCObj *p = o;
1660 p->mediumSetPresentNumber = 0;
1663 return ir_tcl_get_set_int (&p->mediumSetPresentNumber, interp, argc, argv);
1667 * do_referenceId: Set/Get referenceId
1669 static int do_referenceId (void *obj, Tcl_Interp *interp,
1670 int argc, char **argv)
1672 IrTcl_SetCObj *p = obj;
1676 p->referenceId = NULL;
1679 else if (argc == -1)
1680 return ir_tcl_strdel (interp, &p->referenceId);
1683 xfree (p->referenceId);
1684 if (ir_tcl_strdup (interp, &p->referenceId, argv[2]) == TCL_ERROR)
1687 Tcl_AppendResult (interp, p->referenceId, NULL);
1692 * do_preferredRecordSyntax: Set/get preferred record syntax
1694 static int do_preferredRecordSyntax (void *obj, Tcl_Interp *interp,
1695 int argc, char **argv)
1697 IrTcl_SetCObj *p = obj;
1701 p->preferredRecordSyntax = NULL;
1704 else if (argc == -1)
1706 xfree (p->preferredRecordSyntax);
1707 p->preferredRecordSyntax = NULL;
1712 xfree (p->preferredRecordSyntax);
1713 p->preferredRecordSyntax = NULL;
1714 if (argv[2][0] && (p->preferredRecordSyntax =
1715 ir_tcl_malloc (sizeof(*p->preferredRecordSyntax))))
1716 *p->preferredRecordSyntax = IrTcl_getRecordSyntaxVal (argv[2]);
1721 (interp,!p->preferredRecordSyntax ? "" :
1722 IrTcl_getRecordSyntaxStr(*p->preferredRecordSyntax));
1729 * do_elementSetNames: Set/Get element Set Names
1731 static int do_elementSetNames (void *obj, Tcl_Interp *interp,
1732 int argc, char **argv)
1734 IrTcl_SetCObj *p = obj;
1738 p->elementSetNames = NULL;
1741 else if (argc == -1)
1742 return ir_tcl_strdel (interp, &p->elementSetNames);
1745 xfree (p->elementSetNames);
1746 if (ir_tcl_strdup (interp, &p->elementSetNames, argv[2]) == TCL_ERROR)
1749 Tcl_AppendResult (interp, p->elementSetNames, NULL);
1754 * do_smallSetElementSetNames: Set/Get small Set Element Set Names
1756 static int do_smallSetElementSetNames (void *obj, Tcl_Interp *interp,
1757 int argc, char **argv)
1759 IrTcl_SetCObj *p = obj;
1763 p->smallSetElementSetNames = NULL;
1766 else if (argc == -1)
1767 return ir_tcl_strdel (interp, &p->smallSetElementSetNames);
1770 xfree (p->smallSetElementSetNames);
1771 if (ir_tcl_strdup (interp, &p->smallSetElementSetNames,
1772 argv[2]) == TCL_ERROR)
1775 Tcl_AppendResult (interp, p->smallSetElementSetNames, NULL);
1780 * do_mediumSetElementSetNames: Set/Get medium Set Element Set Names
1782 static int do_mediumSetElementSetNames (void *obj, Tcl_Interp *interp,
1783 int argc, char **argv)
1785 IrTcl_SetCObj *p = obj;
1789 p->mediumSetElementSetNames = NULL;
1792 else if (argc == -1)
1793 return ir_tcl_strdel (interp, &p->mediumSetElementSetNames);
1796 xfree (p->mediumSetElementSetNames);
1797 if (ir_tcl_strdup (interp, &p->mediumSetElementSetNames,
1798 argv[2]) == TCL_ERROR)
1801 Tcl_AppendResult (interp, p->mediumSetElementSetNames, NULL);
1805 static IrTcl_Method ir_method_tab[] = {
1806 { "comstack", do_comstack, NULL },
1807 { "protocol", do_protocol, NULL },
1808 { "failback", do_failback, NULL },
1809 { "failInfo", do_failInfo, NULL },
1810 { "apduInfo", do_apduInfo, NULL },
1811 { "logLevel", do_logLevel, NULL },
1813 { "eventType", do_eventType, NULL },
1814 { "connect", do_connect, NULL },
1815 { "protocolVersion", do_protocolVersion, NULL },
1816 { "preferredMessageSize", do_preferredMessageSize, NULL },
1817 { "maximumRecordSize", do_maximumRecordSize, NULL },
1818 { "implementationName", do_implementationName, NULL },
1819 { "implementationId", do_implementationId, NULL },
1820 { "implementationVersion", do_implementationVersion, NULL },
1821 { "targetImplementationName", do_targetImplementationName, NULL },
1822 { "targetImplementationId", do_targetImplementationId, NULL },
1823 { "targetImplementationVersion", do_targetImplementationVersion, NULL},
1824 { "userInformationField", do_userInformationField, NULL},
1825 { "idAuthentication", do_idAuthentication, NULL},
1826 { "options", do_options, NULL},
1827 { "init", do_init_request, NULL},
1828 { "initResult", do_initResult, NULL},
1829 { "disconnect", do_disconnect, NULL},
1830 { "callback", do_callback, NULL},
1831 { "initResponse", do_initResponse, NULL},
1832 { "triggerResourceControl", do_triggerResourceControl, NULL},
1833 { "initResponse", do_initResponse, NULL},
1837 static IrTcl_Method ir_set_c_method_tab[] = {
1838 { "databaseNames", do_databaseNames, NULL},
1839 { "replaceIndicator", do_replaceIndicator, NULL},
1840 { "queryType", do_queryType, NULL},
1841 { "preferredRecordSyntax", do_preferredRecordSyntax, NULL},
1842 { "smallSetUpperBound", do_smallSetUpperBound, NULL},
1843 { "largeSetLowerBound", do_largeSetLowerBound, NULL},
1844 { "mediumSetPresentNumber", do_mediumSetPresentNumber, NULL},
1845 { "referenceId", do_referenceId, NULL},
1846 { "elementSetNames", do_elementSetNames, NULL},
1847 { "smallSetElementSetNames", do_smallSetElementSetNames, NULL},
1848 { "mediumSetElementSetNames", do_mediumSetElementSetNames, NULL},
1853 * ir_obj_method: IR Object methods
1855 static int ir_obj_method (ClientData clientData, Tcl_Interp *interp,
1856 int argc, char **argv)
1858 IrTcl_Methods tab[3];
1859 IrTcl_Obj *p = (IrTcl_Obj *) clientData;
1864 Tcl_AppendResult (interp, wrongArgs, *argv, "method args...\"", NULL);
1868 tab[0].tab = ir_method_tab;
1870 tab[1].tab = ir_set_c_method_tab;
1871 tab[1].obj = &p->set_inher;
1874 if (ir_tcl_method (interp, argc, argv, tab, &r) == TCL_ERROR)
1875 return ir_tcl_method_error (interp, argc, argv, tab);
1880 * ir_obj_delete: IR Object disposal
1882 static void ir_obj_delete (ClientData clientData)
1884 IrTcl_Obj *obj = (IrTcl_Obj *) clientData;
1885 IrTcl_Methods tab[3];
1888 if (obj->ref_count > 0)
1890 assert (obj->ref_count == 0);
1892 logf (LOG_DEBUG, "ir object delete");
1893 tab[0].tab = ir_method_tab;
1895 tab[1].tab = ir_set_c_method_tab;
1896 tab[1].obj = &obj->set_inher;
1899 ir_tcl_method (NULL, -1, NULL, tab, NULL);
1902 odr_destroy (obj->odr_in);
1903 odr_destroy (obj->odr_out);
1904 odr_destroy (obj->odr_pr);
1909 * ir_obj_init: IR Object initialization
1911 int ir_obj_init (ClientData clientData, Tcl_Interp *interp,
1912 int argc, char **argv, ClientData *subData,
1913 ClientData parentData)
1915 IrTcl_Methods tab[3];
1923 Tcl_AppendResult (interp, wrongArgs, *argv, " objName\"", NULL);
1926 obj = ir_tcl_malloc (sizeof(*obj));
1929 obj->bibset = ccl_qual_mk ();
1930 if ((inf = fopen ("default.bib", "r")))
1932 ccl_qual_file (obj->bibset, inf);
1937 logf (LOG_DEBUG, "ir object create %s", argv[1]);
1938 obj->odr_in = odr_createmem (ODR_DECODE);
1939 odr_choice_enable_bias (obj->odr_in, 0);
1940 obj->odr_out = odr_createmem (ODR_ENCODE);
1941 obj->odr_pr = odr_createmem (ODR_PRINT);
1942 obj->state = IR_TCL_R_Idle;
1943 obj->interp = interp;
1947 obj->request_queue = NULL;
1949 tab[0].tab = ir_method_tab;
1951 tab[1].tab = ir_set_c_method_tab;
1952 tab[1].obj = &obj->set_inher;
1955 if (ir_tcl_method (interp, 0, NULL, tab, NULL) == TCL_ERROR)
1957 Tcl_AppendResult (interp, "Failed to initialize ", argv[1], NULL);
1960 *subData = (ClientData) obj;
1966 * ir_obj_mk: IR Object creation
1968 static int ir_obj_mk (ClientData clientData, Tcl_Interp *interp,
1969 int argc, char **argv)
1972 int r = ir_obj_init (clientData, interp, argc, argv, &subData, 0);
1976 Tcl_CreateCommand (interp, argv[1], ir_obj_method,
1977 subData, ir_obj_delete);
1981 IrTcl_Class ir_obj_class = {
1989 /* ------------------------------------------------------- */
1991 * do_search: Do search request
1993 static int do_search (void *o, Tcl_Interp *interp, int argc, char **argv)
1995 Z_SearchRequest *req;
1999 IrTcl_SetObj *obj = o;
2010 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1], "query\"",
2014 logf (LOG_DEBUG, "search %s %s", *argv, argv[2]);
2015 if (!obj->set_inher.num_databaseNames)
2017 Tcl_AppendResult (interp, "no databaseNames", NULL);
2018 return ir_tcl_error_exec (interp, argc, argv);
2022 Tcl_AppendResult (interp, "not connected", NULL);
2023 return ir_tcl_error_exec (interp, argc, argv);
2025 apdu = zget_APDU (p->odr_out, Z_APDU_searchRequest);
2026 req = apdu->u.searchRequest;
2030 set_referenceId (p->odr_out, &req->referenceId,
2031 obj->set_inher.referenceId);
2033 req->smallSetUpperBound = &obj->set_inher.smallSetUpperBound;
2034 req->largeSetLowerBound = &obj->set_inher.largeSetLowerBound;
2035 req->mediumSetPresentNumber = &obj->set_inher.mediumSetPresentNumber;
2036 req->replaceIndicator = &obj->set_inher.replaceIndicator;
2037 req->resultSetName = obj->setName ? obj->setName : "default";
2038 logf (LOG_DEBUG, "Search, resultSetName %s", req->resultSetName);
2039 req->num_databaseNames = obj->set_inher.num_databaseNames;
2040 req->databaseNames = obj->set_inher.databaseNames;
2041 for (r=0; r < obj->set_inher.num_databaseNames; r++)
2042 logf (LOG_DEBUG, " Database %s", obj->set_inher.databaseNames[r]);
2043 if (obj->set_inher.preferredRecordSyntax)
2045 struct oident ident;
2047 ident.proto = p->protocol_type;
2048 ident.oclass = CLASS_RECSYN;
2049 ident.value = *obj->set_inher.preferredRecordSyntax;
2050 logf (LOG_DEBUG, "Preferred record syntax is %d", ident.value);
2051 req->preferredRecordSyntax = odr_oiddup (p->odr_out,
2052 oid_getoidbyent (&ident));
2055 req->preferredRecordSyntax = 0;
2057 if (obj->set_inher.smallSetElementSetNames &&
2058 *obj->set_inher.smallSetElementSetNames)
2060 Z_ElementSetNames *esn = odr_malloc (p->odr_out, sizeof(*esn));
2062 esn->which = Z_ElementSetNames_generic;
2063 esn->u.generic = obj->set_inher.smallSetElementSetNames;
2064 req->smallSetElementSetNames = esn;
2067 req->smallSetElementSetNames = NULL;
2069 if (obj->set_inher.mediumSetElementSetNames &&
2070 *obj->set_inher.mediumSetElementSetNames)
2072 Z_ElementSetNames *esn = odr_malloc (p->odr_out, sizeof(*esn));
2074 esn->which = Z_ElementSetNames_generic;
2075 esn->u.generic = obj->set_inher.mediumSetElementSetNames;
2076 req->mediumSetElementSetNames = esn;
2079 req->mediumSetElementSetNames = NULL;
2081 req->query = &query;
2083 logf (LOG_DEBUG, "queryType %s", obj->set_inher.queryType);
2084 if (!strcmp (obj->set_inher.queryType, "rpn"))
2086 Z_RPNQuery *RPNquery;
2088 RPNquery = p_query_rpn (p->odr_out, p->protocol_type, argv[2]);
2091 Tcl_AppendResult (interp, "query syntax error", NULL);
2092 return ir_tcl_error_exec (interp, argc, argv);
2094 query.which = Z_Query_type_1;
2095 query.u.type_1 = RPNquery;
2098 else if (!strcmp (obj->set_inher.queryType, "cclrpn"))
2102 struct ccl_rpn_node *rpn;
2103 Z_RPNQuery *RPNquery;
2106 bib1.proto = p->protocol_type;
2107 bib1.oclass = CLASS_ATTSET;
2108 bib1.value = VAL_BIB1;
2110 rpn = ccl_find_str(p->bibset, argv[2], &error, &pos);
2113 Tcl_AppendResult (interp, "ccl syntax error ", ccl_err_msg(error),
2115 return ir_tcl_error_exec (interp, argc, argv);
2118 ccl_pr_tree (rpn, stderr);
2119 fprintf (stderr, "\n");
2121 assert((RPNquery = ccl_rpn_query(rpn)));
2122 RPNquery->attributeSetId = oid_getoidbyent (&bib1);
2123 query.which = Z_Query_type_1;
2124 query.u.type_1 = RPNquery;
2127 else if (!strcmp (obj->set_inher.queryType, "ccl"))
2129 query.which = Z_Query_type_2;
2130 query.u.type_2 = &ccl_query;
2131 ccl_query.buf = (unsigned char *) argv[2];
2132 ccl_query.len = strlen (argv[2]);
2136 Tcl_AppendResult (interp, "invalid query method ",
2137 obj->set_inher.queryType, NULL);
2138 return ir_tcl_error_exec (interp, argc, argv);
2140 return ir_tcl_send_APDU (interp, p, apdu, "search", *argv);
2144 * do_searchResponse: add search response handler
2146 static int do_searchResponse (void *o, Tcl_Interp *interp,
2147 int argc, char **argv)
2149 IrTcl_SetObj *obj = o;
2153 obj->searchResponse = NULL;
2156 else if (argc == -1)
2157 return ir_tcl_strdel (interp, &obj->searchResponse);
2160 xfree (obj->searchResponse);
2163 if (ir_tcl_strdup (interp, &obj->searchResponse, argv[2])
2168 obj->searchResponse = NULL;
2174 * do_presentResponse: add present response handler
2176 static int do_presentResponse (void *o, Tcl_Interp *interp,
2177 int argc, char **argv)
2179 IrTcl_SetObj *obj = o;
2183 obj->presentResponse = NULL;
2186 else if (argc == -1)
2187 return ir_tcl_strdel (interp, &obj->presentResponse);
2190 xfree (obj->presentResponse);
2193 if (ir_tcl_strdup (interp, &obj->presentResponse, argv[2])
2198 obj->presentResponse = NULL;
2204 * do_resultCount: Get number of hits
2206 static int do_resultCount (void *o, Tcl_Interp *interp,
2207 int argc, char **argv)
2209 IrTcl_SetObj *obj = o;
2213 obj->resultCount = 0;
2216 return ir_tcl_get_set_int (&obj->resultCount, interp, argc, argv);
2220 * do_searchStatus: Get search status (after search response)
2222 static int do_searchStatus (void *o, Tcl_Interp *interp,
2223 int argc, char **argv)
2225 IrTcl_SetObj *obj = o;
2229 return ir_tcl_get_set_int (&obj->searchStatus, interp, argc, argv);
2233 * do_presentStatus: Get search status (after search/present response)
2235 static int do_presentStatus (void *o, Tcl_Interp *interp,
2236 int argc, char **argv)
2238 IrTcl_SetObj *obj = o;
2242 return ir_tcl_get_set_int (&obj->presentStatus, interp, argc, argv);
2246 * do_nextResultSetPosition: Get next result set position
2247 * (after search/present response)
2249 static int do_nextResultSetPosition (void *o, Tcl_Interp *interp,
2250 int argc, char **argv)
2252 IrTcl_SetObj *obj = o;
2256 obj->nextResultSetPosition = 0;
2259 return ir_tcl_get_set_int (&obj->nextResultSetPosition, interp,
2264 * do_setName: Set result Set name
2266 static int do_setName (void *o, Tcl_Interp *interp,
2267 int argc, char **argv)
2269 IrTcl_SetObj *obj = o;
2272 return ir_tcl_strdup (interp, &obj->setName, "Default");
2273 else if (argc == -1)
2274 return ir_tcl_strdel (interp, &obj->setName);
2277 xfree (obj->setName);
2278 if (ir_tcl_strdup (interp, &obj->setName, argv[2])
2282 Tcl_AppendElement (interp, obj->setName);
2287 * do_numberOfRecordsReturned: Get number of records returned
2289 static int do_numberOfRecordsReturned (void *o, Tcl_Interp *interp,
2290 int argc, char **argv)
2292 IrTcl_SetObj *obj = o;
2296 obj->numberOfRecordsReturned = 0;
2299 return ir_tcl_get_set_int (&obj->numberOfRecordsReturned, interp,
2304 * do_type: Return type (if any) at position.
2306 static int do_type (void *o, Tcl_Interp *interp, int argc, char **argv)
2308 IrTcl_SetObj *obj = o;
2310 IrTcl_RecordList *rl;
2314 obj->record_list = NULL;
2317 else if (argc == -1)
2319 delete_IR_records (obj);
2324 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2325 " position\"", NULL);
2328 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2330 rl = find_IR_record (obj, offset);
2333 logf (LOG_DEBUG, "%s %s %s: no record", argv[0], argv[1], argv[2]);
2338 case Z_NamePlusRecord_databaseRecord:
2339 interp->result = "DB";
2341 case Z_NamePlusRecord_surrogateDiagnostic:
2342 interp->result = "SD";
2350 * do_recordType: Return record type (if any) at position.
2352 static int do_recordType (void *o, Tcl_Interp *interp, int argc, char **argv)
2354 IrTcl_SetObj *obj = o;
2356 IrTcl_RecordList *rl;
2362 else if (argc == -1)
2368 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2369 " position\"", NULL);
2372 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2374 rl = find_IR_record (obj, offset);
2377 logf (LOG_DEBUG, "%s %s %s: no record", argv[0], argv[1], argv[2]);
2380 if (rl->which != Z_NamePlusRecord_databaseRecord)
2382 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2385 Tcl_AppendElement (interp, (char*)
2386 IrTcl_getRecordSyntaxStr (rl->u.dbrec.type));
2391 * set record elements (for record extraction)
2393 static int do_recordElements (void *o, Tcl_Interp *interp,
2394 int argc, char **argv)
2396 IrTcl_SetObj *obj = o;
2400 obj->recordElements = NULL;
2403 else if (argc == -1)
2404 return ir_tcl_strdel (NULL, &obj->recordElements);
2407 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2408 " ?position?\"", NULL);
2413 xfree (obj->recordElements);
2414 return ir_tcl_strdup (NULL, &obj->recordElements,
2415 (*argv[2] ? argv[2] : NULL));
2417 Tcl_AppendResult (interp, obj->recordElements, NULL);
2424 static int ir_diagResult (Tcl_Interp *interp, IrTcl_Diagnostic *list, int num)
2430 for (i = 0; i<num; i++)
2432 sprintf (buf, "%d", list[i].condition);
2433 Tcl_AppendElement (interp, buf);
2434 cp = diagbib1_str (list[i].condition);
2436 Tcl_AppendElement (interp, (char*) cp);
2438 Tcl_AppendElement (interp, "");
2439 if (list[i].addinfo)
2440 Tcl_AppendElement (interp, (char*) list[i].addinfo);
2442 Tcl_AppendElement (interp, "");
2448 * do_diag: Return diagnostic record info
2450 static int do_diag (void *o, Tcl_Interp *interp, int argc, char **argv)
2452 IrTcl_SetObj *obj = o;
2454 IrTcl_RecordList *rl;
2460 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2461 " position\"", NULL);
2464 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2466 rl = find_IR_record (obj, offset);
2469 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2472 if (rl->which != Z_NamePlusRecord_surrogateDiagnostic)
2474 Tcl_AppendResult (interp, "No Diagnostic record at #", argv[2], NULL);
2477 return ir_diagResult (interp, rl->u.surrogateDiagnostics.list,
2478 rl->u.surrogateDiagnostics.num);
2482 * do_getMarc: Get ISO2709 Record lines/fields
2484 static int do_getMarc (void *o, Tcl_Interp *interp, int argc, char **argv)
2486 IrTcl_SetObj *obj = o;
2488 IrTcl_RecordList *rl;
2494 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2495 " position line|field tag ind field\"", NULL);
2498 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2500 rl = find_IR_record (obj, offset);
2503 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2506 if (rl->which != Z_NamePlusRecord_databaseRecord)
2508 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2511 return ir_tcl_get_marc (interp, rl->u.dbrec.buf, argc, argv);
2515 * do_getSutrs: Get SUTRS Record
2517 static int do_getSutrs (void *o, Tcl_Interp *interp, int argc, char **argv)
2519 IrTcl_SetObj *obj = o;
2521 IrTcl_RecordList *rl;
2527 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2528 " position\"", NULL);
2531 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2533 rl = find_IR_record (obj, offset);
2536 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2539 if (rl->which != Z_NamePlusRecord_databaseRecord)
2541 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2544 if (!rl->u.dbrec.buf || rl->u.dbrec.type != VAL_SUTRS)
2546 Tcl_AppendElement (interp, rl->u.dbrec.buf);
2552 * do_getGrs: Get a GRS-1 Record
2554 static int do_getGrs (void *o, Tcl_Interp *interp, int argc, char **argv)
2556 IrTcl_SetObj *obj = o;
2558 IrTcl_RecordList *rl;
2564 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2565 " position ?(set,tag) (set,tag) ...?\"", NULL);
2568 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2570 rl = find_IR_record (obj, offset);
2573 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2576 if (rl->which != Z_NamePlusRecord_databaseRecord)
2578 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2581 if (rl->u.dbrec.type != VAL_GRS1)
2583 return ir_tcl_get_grs (interp, rl->u.dbrec.u.grs1, argc, argv);
2588 * do_getExplain: Get an Explain Record
2590 static int do_getExplain (void *o, Tcl_Interp *interp, int argc, char **argv)
2592 IrTcl_SetObj *obj = o;
2593 IrTcl_Obj *p = obj->parent;
2595 Z_ext_typeent *etype;
2597 IrTcl_RecordList *rl;
2603 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2604 " position ?mask? ...\"", NULL);
2607 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2609 rl = find_IR_record (obj, offset);
2612 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2615 if (rl->which != Z_NamePlusRecord_databaseRecord)
2617 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2620 if (!rl->u.dbrec.buf || rl->u.dbrec.type != VAL_EXPLAIN)
2623 if (!(etype = z_ext_getentbyref (VAL_EXPLAIN)))
2625 assert (rl->u.dbrec.buf);
2626 odr_setbuf (p->odr_in, rl->u.dbrec.buf, rl->u.dbrec.size, 0);
2627 if (!(*etype->fun)(p->odr_in, (char **) &rr, 0))
2630 if (etype->what != Z_External_explainRecord)
2633 return ir_tcl_get_explain (interp, rr, argc, argv);
2637 * do_responseStatus: Return response status (present or search)
2639 static int do_responseStatus (void *o, Tcl_Interp *interp,
2640 int argc, char **argv)
2642 IrTcl_SetObj *obj = o;
2646 obj->recordFlag = 0;
2647 obj->nonSurrogateDiagnosticNum = 0;
2648 obj->nonSurrogateDiagnosticList = NULL;
2651 else if (argc == -1)
2653 ir_deleteDiags (&obj->nonSurrogateDiagnosticList,
2654 &obj->nonSurrogateDiagnosticNum);
2657 if (!obj->recordFlag)
2659 Tcl_AppendElement (interp, "OK");
2664 case Z_Records_DBOSD:
2665 Tcl_AppendElement (interp, "DBOSD");
2668 Tcl_AppendElement (interp, "NSD");
2669 return ir_diagResult (interp, obj->nonSurrogateDiagnosticList,
2670 obj->nonSurrogateDiagnosticNum);
2676 * do_present: Perform Present Request
2679 static int do_present (void *o, Tcl_Interp *interp, int argc, char **argv)
2681 IrTcl_SetObj *obj = o;
2684 Z_PresentRequest *req;
2692 if (Tcl_GetInt (interp, argv[2], &start) == TCL_ERROR)
2699 if (Tcl_GetInt (interp, argv[3], &number) == TCL_ERROR)
2704 logf (LOG_DEBUG, "present %s %d %d", *argv, start, number);
2708 Tcl_AppendResult (interp, "not connected", NULL);
2709 return ir_tcl_error_exec (interp, argc, argv);
2712 obj->number = number;
2714 apdu = zget_APDU (p->odr_out, Z_APDU_presentRequest);
2715 req = apdu->u.presentRequest;
2717 set_referenceId (p->odr_out, &req->referenceId,
2718 obj->set_inher.referenceId);
2720 req->resultSetId = obj->setName ? obj->setName : "Default";
2722 req->resultSetStartPoint = &start;
2723 req->numberOfRecordsRequested = &number;
2724 if (obj->set_inher.preferredRecordSyntax)
2726 struct oident ident;
2728 ident.proto = p->protocol_type;
2729 ident.oclass = CLASS_RECSYN;
2730 ident.value = *obj->set_inher.preferredRecordSyntax;
2731 logf (LOG_DEBUG, "Preferred record syntax is %d", ident.value);
2732 req->preferredRecordSyntax = odr_oiddup (p->odr_out,
2733 oid_getoidbyent (&ident));
2736 req->preferredRecordSyntax = 0;
2738 if (obj->set_inher.elementSetNames && *obj->set_inher.elementSetNames)
2740 Z_ElementSetNames *esn = odr_malloc (p->odr_out, sizeof(*esn));
2741 Z_RecordComposition *compo = odr_malloc (p->odr_out, sizeof(*compo));
2743 esn->which = Z_ElementSetNames_generic;
2744 esn->u.generic = obj->set_inher.elementSetNames;
2746 req->recordComposition = compo;
2747 compo->which = Z_RecordComp_simple;
2748 compo->u.simple = esn;
2751 req->recordComposition = NULL;
2752 return ir_tcl_send_APDU (interp, p, apdu, "present", *argv);
2755 #define IR_TCL_RECORD_ENCODING_ISO2709 1
2756 #define IR_TCL_RECORD_ENCODING_RAW 2
2762 } IrTcl_FileRecordHead;
2765 * do_loadFile: Load result set from file
2767 static int do_loadFile (void *o, Tcl_Interp *interp,
2768 int argc, char **argv)
2770 IrTcl_SetObj *setobj = o;
2782 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2783 " filename ?start? ?number?\"", NULL);
2787 start = atoi (argv[3]);
2789 number = atoi (argv[4]);
2792 inf = fopen (argv[2], "r");
2795 Tcl_AppendResult (interp, "Cannot open ", argv[2], NULL);
2796 return ir_tcl_error_exec (interp, argc, argv);
2798 while (offset < (start+number))
2800 IrTcl_FileRecordHead head;
2801 IrTcl_RecordList *rl;
2803 if (fread (&head, sizeof(head), 1, inf) < 1)
2805 rl = new_IR_record (setobj, offset,
2806 Z_NamePlusRecord_databaseRecord,
2807 (argc > 5) ? argv[5] : NULL);
2808 rl->u.dbrec.type = head.syntax;
2809 if (head.encoding == IR_TCL_RECORD_ENCODING_ISO2709)
2811 if (!(buf = ir_tcl_fread_marc (inf, &size)))
2813 rl->u.dbrec.buf = buf;
2814 rl->u.dbrec.size = size;
2815 if (size != head.size)
2818 Tcl_AppendResult (interp, "bad ISO2709 encoding", NULL);
2819 return ir_tcl_error_exec (interp, argc, argv);
2822 else if (head.encoding == IR_TCL_RECORD_ENCODING_RAW)
2824 rl->u.dbrec.size = head.size;
2825 rl->u.dbrec.buf = ir_tcl_malloc (head.size + 1);
2826 if (fread (rl->u.dbrec.buf, rl->u.dbrec.size, 1, inf) < 1)
2829 Tcl_AppendResult (interp, "bad raw encoding", NULL);
2830 return ir_tcl_error_exec (interp, argc, argv);
2832 rl->u.dbrec.buf[rl->u.dbrec.size] = '\0';
2836 rl->u.dbrec.buf = NULL;
2837 rl->u.dbrec.size = 0;
2839 Tcl_AppendResult (interp, "bad encoding", NULL);
2840 return ir_tcl_error_exec (interp, argc, argv);
2844 setobj->numberOfRecordsReturned = offset - start;
2850 * do_saveFile: Save result set on file
2852 static int do_saveFile (void *o, Tcl_Interp *interp,
2853 int argc, char **argv)
2855 IrTcl_SetObj *setobj = o;
2860 IrTcl_RecordList *rl;
2866 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2867 " filename ?start? ?number?\"", NULL);
2871 start = atoi (argv[3]);
2873 number = atoi (argv[4]);
2876 outf = fopen (argv[2], "w");
2879 Tcl_AppendResult (interp, "cannot open file", NULL);
2880 return ir_tcl_error_exec (interp, argc, argv);
2882 while (offset < (start+number) && (rl = find_IR_record (setobj, offset)))
2884 if (rl->which == Z_NamePlusRecord_databaseRecord &&
2885 rl->u.dbrec.buf && rl->u.dbrec.size)
2887 IrTcl_FileRecordHead head;
2889 head.encoding = IR_TCL_RECORD_ENCODING_RAW;
2890 head.syntax = rl->u.dbrec.type;
2891 head.size = rl->u.dbrec.size;
2892 if (fwrite (&head, sizeof(head), 1, outf) < 1)
2894 Tcl_AppendResult (interp, "cannot write", NULL);
2895 return ir_tcl_error_exec (interp, argc, argv);
2897 if (fwrite (rl->u.dbrec.buf, rl->u.dbrec.size, 1, outf) < 1)
2899 Tcl_AppendResult (interp, "cannot write", NULL);
2900 return ir_tcl_error_exec (interp, argc, argv);
2907 Tcl_AppendResult (interp, "cannot write ", NULL);
2908 return ir_tcl_error_exec (interp, argc, argv);
2914 static IrTcl_Method ir_set_method_tab[] = {
2915 { "search", do_search, NULL},
2916 { "searchResponse", do_searchResponse, NULL},
2917 { "presentResponse", do_presentResponse, NULL},
2918 { "searchStatus", do_searchStatus, NULL},
2919 { "presentStatus", do_presentStatus, NULL},
2920 { "nextResultSetPosition", do_nextResultSetPosition, NULL},
2921 { "setName", do_setName, NULL},
2922 { "resultCount", do_resultCount, NULL},
2923 { "numberOfRecordsReturned", do_numberOfRecordsReturned, NULL},
2924 { "present", do_present, NULL},
2925 { "type", do_type, NULL},
2926 { "getMarc", do_getMarc, NULL},
2927 { "getSutrs", do_getSutrs, NULL},
2928 { "getGrs", do_getGrs, NULL},
2929 { "getExplain", do_getExplain, NULL},
2930 { "recordType", do_recordType, NULL},
2931 { "recordElements", do_recordElements, NULL},
2932 { "diag", do_diag, NULL},
2933 { "responseStatus", do_responseStatus, NULL},
2934 { "loadFile", do_loadFile, NULL},
2935 { "saveFile", do_saveFile, NULL},
2940 * ir_set_obj_method: IR Set Object methods
2942 static int ir_set_obj_method (ClientData clientData, Tcl_Interp *interp,
2943 int argc, char **argv)
2945 IrTcl_Methods tabs[3];
2946 IrTcl_SetObj *p = (IrTcl_SetObj *) clientData;
2951 Tcl_AppendResult (interp, wrongArgs, *argv, " method args...\"", NULL);
2954 tabs[0].tab = ir_set_method_tab;
2956 tabs[1].tab = ir_set_c_method_tab;
2957 tabs[1].obj = &p->set_inher;
2960 if (ir_tcl_method (interp, argc, argv, tabs, &r) == TCL_ERROR)
2961 return ir_tcl_method_error (interp, argc, argv, tabs);
2966 * ir_set_obj_delete: IR Set Object disposal
2968 static void ir_set_obj_delete (ClientData clientData)
2970 IrTcl_Methods tabs[3];
2971 IrTcl_SetObj *p = (IrTcl_SetObj *) clientData;
2973 logf (LOG_DEBUG, "ir set delete");
2975 tabs[0].tab = ir_set_method_tab;
2977 tabs[1].tab = ir_set_c_method_tab;
2978 tabs[1].obj = &p->set_inher;
2981 ir_tcl_method (NULL, -1, NULL, tabs, NULL);
2987 * ir_set_obj_init: IR Set Object initialization
2989 static int ir_set_obj_init (ClientData clientData, Tcl_Interp *interp,
2990 int argc, char **argv, ClientData *subData,
2991 ClientData parentData)
2993 IrTcl_Methods tabs[3];
2996 if (argc < 2 || argc > 3)
2998 Tcl_AppendResult (interp, wrongArgs, *argv,
2999 " objSetName ?objParent?\"", NULL);
3002 obj = ir_tcl_malloc (sizeof(*obj));
3003 logf (LOG_DEBUG, "ir set create %s", argv[1]);
3010 obj->parent = (IrTcl_Obj *) parentData;
3012 dst = &obj->set_inher;
3013 src = &obj->parent->set_inher;
3015 if ((dst->num_databaseNames = src->num_databaseNames))
3017 dst->databaseNames =
3018 ir_tcl_malloc (sizeof (*dst->databaseNames)
3019 * (1+dst->num_databaseNames));
3020 for (i = 0; i < dst->num_databaseNames; i++)
3021 if (ir_tcl_strdup (interp, &dst->databaseNames[i],
3022 src->databaseNames[i]) == TCL_ERROR)
3024 dst->databaseNames[i] = NULL;
3027 dst->databaseNames = NULL;
3028 if (ir_tcl_strdup (interp, &dst->queryType, src->queryType)
3032 if (ir_tcl_strdup (interp, &dst->referenceId, src->referenceId)
3036 if (ir_tcl_strdup (interp, &dst->elementSetNames, src->elementSetNames)
3040 if (ir_tcl_strdup (interp, &dst->smallSetElementSetNames,
3041 src->smallSetElementSetNames)
3045 if (ir_tcl_strdup (interp, &dst->mediumSetElementSetNames,
3046 src->mediumSetElementSetNames)
3050 if (src->preferredRecordSyntax &&
3051 (dst->preferredRecordSyntax
3052 = ir_tcl_malloc (sizeof(*dst->preferredRecordSyntax))))
3053 *dst->preferredRecordSyntax = *src->preferredRecordSyntax;
3055 dst->preferredRecordSyntax = NULL;
3056 dst->replaceIndicator = src->replaceIndicator;
3057 dst->smallSetUpperBound = src->smallSetUpperBound;
3058 dst->largeSetLowerBound = src->largeSetLowerBound;
3059 dst->mediumSetPresentNumber = src->mediumSetPresentNumber;
3064 tabs[0].tab = ir_set_method_tab;
3068 if (ir_tcl_method (interp, 0, NULL, tabs, NULL) == TCL_ERROR)
3071 *subData = (ClientData) obj;
3076 * ir_set_obj_mk: IR Set Object creation
3078 static int ir_set_obj_mk (ClientData clientData, Tcl_Interp *interp,
3079 int argc, char **argv)
3082 ClientData parentData = 0;
3087 Tcl_CmdInfo parent_info;
3088 if (!Tcl_GetCommandInfo (interp, argv[2], &parent_info))
3090 Tcl_AppendResult (interp, "no object parent", NULL);
3091 return ir_tcl_error_exec (interp, argc, argv);
3093 parentData = parent_info.clientData;
3095 r = ir_set_obj_init (clientData, interp, argc, argv, &subData, parentData);
3098 Tcl_CreateCommand (interp, argv[1], ir_set_obj_method,
3099 subData, ir_set_obj_delete);
3103 IrTcl_Class ir_set_obj_class = {
3110 /* ------------------------------------------------------- */
3113 * do_scan: Perform scan
3115 static int do_scan (void *o, Tcl_Interp *interp, int argc, char **argv)
3119 IrTcl_ScanObj *obj = o;
3120 IrTcl_Obj *p = obj->parent;
3123 struct ccl_rpn_node *rpn;
3131 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
3132 " scanQuery\"", NULL);
3135 logf (LOG_DEBUG, "scan %s %s", *argv, argv[2]);
3136 if (!p->set_inher.num_databaseNames)
3138 Tcl_AppendResult (interp, "no databaseNames", NULL);
3139 return ir_tcl_error_exec (interp, argc, argv);
3143 Tcl_AppendResult (interp, "not connected", NULL);
3144 return ir_tcl_error_exec (interp, argc, argv);
3147 apdu = zget_APDU (p->odr_out, Z_APDU_scanRequest);
3148 req = apdu->u.scanRequest;
3150 set_referenceId (p->odr_out, &req->referenceId, p->set_inher.referenceId);
3151 req->num_databaseNames = p->set_inher.num_databaseNames;
3152 req->databaseNames = p->set_inher.databaseNames;
3155 if (!(req->termListAndStartPoint =
3156 p_query_scan (p->odr_out, p->protocol_type,
3157 &req->attributeSet, argv[2])))
3159 Tcl_AppendResult (interp, "query syntax error", NULL);
3160 return ir_tcl_error_exec (interp, argc, argv);
3163 rpn = ccl_find_str(p->bibset, argv[2], &r, &pos);
3166 Tcl_AppendResult (interp, "ccl syntax error ", ccl_err_msg(r), NULL);
3167 return ir_tcl_error_exec (interp, argc, argv);
3169 bib1.proto = p->protocol_type;
3170 bib1.oclass = CLASS_ATTSET;
3171 bib1.value = VAL_BIB1;
3173 req->attributeSet = oid_getoidbyent (&bib1);
3174 if (!(req->termListAndStartPoint = ccl_scan_query (rpn)))
3177 req->stepSize = &obj->stepSize;
3178 req->numberOfTermsRequested = &obj->numberOfTermsRequested;
3179 req->preferredPositionInResponse = &obj->preferredPositionInResponse;
3180 logf (LOG_DEBUG, "stepSize=%d", *req->stepSize);
3181 logf (LOG_DEBUG, "numberOfTermsRequested=%d",
3182 *req->numberOfTermsRequested);
3183 logf (LOG_DEBUG, "preferredPositionInResponse=%d",
3184 *req->preferredPositionInResponse);
3186 return ir_tcl_send_APDU (interp, p, apdu, "scan", *argv);
3190 * do_scanResponse: add scan response handler
3192 static int do_scanResponse (void *o, Tcl_Interp *interp,
3193 int argc, char **argv)
3195 IrTcl_ScanObj *obj = o;
3199 obj->scanResponse = NULL;
3202 else if (argc == -1)
3203 return ir_tcl_strdel (interp, &obj->scanResponse);
3206 xfree (obj->scanResponse);
3209 if (ir_tcl_strdup (interp, &obj->scanResponse, argv[2])
3214 obj->scanResponse = NULL;
3220 * do_stepSize: Set/get replace Step Size
3222 static int do_stepSize (void *obj, Tcl_Interp *interp,
3223 int argc, char **argv)
3225 IrTcl_ScanObj *p = obj;
3231 return ir_tcl_get_set_int (&p->stepSize, interp, argc, argv);
3235 * do_numberOfTermsRequested: Set/get Number of Terms requested
3237 static int do_numberOfTermsRequested (void *obj, Tcl_Interp *interp,
3238 int argc, char **argv)
3240 IrTcl_ScanObj *p = obj;
3244 p->numberOfTermsRequested = 20;
3247 return ir_tcl_get_set_int (&p->numberOfTermsRequested, interp, argc, argv);
3252 * do_preferredPositionInResponse: Set/get preferred Position
3254 static int do_preferredPositionInResponse (void *obj, Tcl_Interp *interp,
3255 int argc, char **argv)
3257 IrTcl_ScanObj *p = obj;
3261 p->preferredPositionInResponse = 1;
3264 return ir_tcl_get_set_int (&p->preferredPositionInResponse, interp,
3269 * do_scanStatus: Get scan status
3271 static int do_scanStatus (void *obj, Tcl_Interp *interp,
3272 int argc, char **argv)
3274 IrTcl_ScanObj *p = obj;
3278 return ir_tcl_get_set_int (&p->scanStatus, interp, argc, argv);
3282 * do_numberOfEntriesReturned: Get number of Entries returned
3284 static int do_numberOfEntriesReturned (void *obj, Tcl_Interp *interp,
3285 int argc, char **argv)
3287 IrTcl_ScanObj *p = obj;
3291 return ir_tcl_get_set_int (&p->numberOfEntriesReturned, interp,
3296 * do_positionOfTerm: Get position of Term
3298 static int do_positionOfTerm (void *obj, Tcl_Interp *interp,
3299 int argc, char **argv)
3301 IrTcl_ScanObj *p = obj;
3305 return ir_tcl_get_set_int (&p->positionOfTerm, interp, argc, argv);
3309 * do_scanLine: get Scan Line (surrogate or normal) after response
3311 static int do_scanLine (void *obj, Tcl_Interp *interp, int argc, char **argv)
3313 IrTcl_ScanObj *p = obj;
3319 p->entries_flag = 0;
3321 p->nonSurrogateDiagnosticNum = 0;
3322 p->nonSurrogateDiagnosticList = 0;
3325 else if (argc == -1)
3327 p->entries_flag = 0;
3328 /* release entries */
3331 ir_deleteDiags (&p->nonSurrogateDiagnosticList,
3332 &p->nonSurrogateDiagnosticNum);
3337 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
3338 " position\"", NULL);
3341 printf ("argv[2]=%s\n", argv[2]);
3342 if (Tcl_GetInt (interp, argv[2], &i) == TCL_ERROR)
3344 if (!p->entries_flag || !p->entries || i >= p->num_entries || i < 0)
3346 switch (p->entries[i].which)
3348 case Z_Entry_termInfo:
3349 Tcl_AppendElement (interp, "T");
3350 if (p->entries[i].u.term.buf)
3351 Tcl_AppendElement (interp, p->entries[i].u.term.buf);
3353 Tcl_AppendElement (interp, "");
3354 sprintf (numstr, "%d", p->entries[i].u.term.globalOccurrences);
3355 Tcl_AppendElement (interp, numstr);
3357 case Z_Entry_surrogateDiagnostic:
3358 Tcl_AppendElement (interp, "SD");
3359 return ir_diagResult (interp, p->entries[i].u.diag.list,
3360 p->entries[i].u.diag.num);
3366 static IrTcl_Method ir_scan_method_tab[] = {
3367 { "scan", do_scan, NULL},
3368 { "scanResponse", do_scanResponse, NULL},
3369 { "stepSize", do_stepSize, NULL},
3370 { "numberOfTermsRequested", do_numberOfTermsRequested, NULL},
3371 { "preferredPositionInResponse", do_preferredPositionInResponse, NULL},
3372 { "scanStatus", do_scanStatus, NULL},
3373 { "numberOfEntriesReturned", do_numberOfEntriesReturned, NULL},
3374 { "positionOfTerm", do_positionOfTerm, NULL},
3375 { "scanLine", do_scanLine, NULL},
3380 * ir_scan_obj_method: IR Scan Object methods
3382 static int ir_scan_obj_method (ClientData clientData, Tcl_Interp *interp,
3383 int argc, char **argv)
3385 IrTcl_Methods tabs[2];
3390 Tcl_AppendResult (interp, wrongArgs, *argv, " method args...\"", NULL);
3393 tabs[0].tab = ir_scan_method_tab;
3394 tabs[0].obj = clientData;
3397 if (ir_tcl_method (interp, argc, argv, tabs, &r) == TCL_ERROR)
3398 return ir_tcl_method_error (interp, argc, argv, tabs);
3403 * ir_scan_obj_delete: IR Scan Object disposal
3405 static void ir_scan_obj_delete (ClientData clientData)
3407 IrTcl_Methods tabs[2];
3408 IrTcl_ScanObj *obj = (IrTcl_ScanObj *) clientData;
3410 tabs[0].tab = ir_scan_method_tab;
3414 ir_tcl_method (NULL, -1, NULL, tabs, NULL);
3419 * ir_scan_obj_mk: IR Scan Object creation
3421 static int ir_scan_obj_mk (ClientData clientData, Tcl_Interp *interp,
3422 int argc, char **argv)
3424 Tcl_CmdInfo parent_info;
3426 IrTcl_Methods tabs[2];
3430 Tcl_AppendResult (interp, wrongArgs, *argv,
3431 "objScanName objParentName\"", NULL);
3434 logf (LOG_DEBUG, "ir scan create %s", argv[1]);
3435 if (!Tcl_GetCommandInfo (interp, argv[2], &parent_info))
3437 Tcl_AppendResult (interp, "no object parent", NULL);
3438 return ir_tcl_error_exec (interp, argc, argv);
3440 obj = ir_tcl_malloc (sizeof(*obj));
3441 obj->parent = (IrTcl_Obj *) parent_info.clientData;
3443 tabs[0].tab = ir_scan_method_tab;
3447 if (ir_tcl_method (interp, 0, NULL, tabs, NULL) == TCL_ERROR)
3449 Tcl_CreateCommand (interp, argv[1], ir_scan_obj_method,
3450 (ClientData) obj, ir_scan_obj_delete);
3454 /* ------------------------------------------------------- */
3457 * ir_log_init_proc: set yaz log level
3459 static int ir_log_init_proc (ClientData clientData, Tcl_Interp *interp,
3460 int argc, char **argv)
3462 if (argc <= 1 || argc > 4)
3464 Tcl_AppendResult (interp, wrongArgs, *argv,
3465 " ?level ?prefix ?filename\"", NULL);
3469 log_init (log_mask_str (argv[1]), "", NULL);
3471 log_init (log_mask_str (argv[1]), argv[2], NULL);
3473 log_init (log_mask_str (argv[1]), argv[2], argv[3]);
3478 * ir_log_proc: log yaz message
3480 static int ir_log_proc (ClientData clientData, Tcl_Interp *interp,
3481 int argc, char **argv)
3486 Tcl_AppendResult (interp, wrongArgs, *argv,
3487 " level string\"", NULL);
3490 mask = log_mask_str_x (argv[1], 0);
3491 logf (LOG_DEBUG, "%s", argv[2]);
3497 * ir_version: log ir version
3499 static int ir_version (ClientData clientData, Tcl_Interp *interp,
3500 int argc, char **argv)
3502 Tcl_AppendElement (interp, IR_TCL_VERSION);
3503 Tcl_AppendElement (interp, YAZ_VERSION);
3508 /* ------------------------------------------------------- */
3509 static void ir_initResponse (void *obj, Z_InitResponse *initrs)
3513 p->initResult = *initrs->result ? 1 : 0;
3514 if (!*initrs->result)
3515 logf (LOG_DEBUG, "Connection rejected by target");
3517 logf (LOG_DEBUG, "Connection accepted by target");
3519 get_referenceId (&p->set_inher.referenceId, initrs->referenceId);
3521 xfree (p->targetImplementationId);
3522 ir_tcl_strdup (p->interp, &p->targetImplementationId,
3523 initrs->implementationId);
3524 xfree (p->targetImplementationName);
3525 ir_tcl_strdup (p->interp, &p->targetImplementationName,
3526 initrs->implementationName);
3527 xfree (p->targetImplementationVersion);
3528 ir_tcl_strdup (p->interp, &p->targetImplementationVersion,
3529 initrs->implementationVersion);
3531 p->maximumRecordSize = *initrs->maximumRecordSize;
3532 p->preferredMessageSize = *initrs->preferredMessageSize;
3534 memcpy (&p->options, initrs->options, sizeof(initrs->options));
3535 memcpy (&p->protocolVersion, initrs->protocolVersion,
3536 sizeof(initrs->protocolVersion));
3537 xfree (p->userInformationField);
3538 p->userInformationField = NULL;
3539 if (initrs->userInformationField)
3543 if (initrs->userInformationField->which == ODR_EXTERNAL_octet &&
3544 (p->userInformationField =
3545 ir_tcl_malloc ((len =
3546 initrs->userInformationField->
3547 u.octet_aligned->len) +1)))
3549 memcpy (p->userInformationField,
3550 initrs->userInformationField->u.octet_aligned->buf,
3552 (p->userInformationField)[len] = '\0';
3557 static void ir_deleteDiags (IrTcl_Diagnostic **dst_list, int *dst_num)
3560 for (i = 0; i<*dst_num; i++)
3561 xfree (dst_list[i]->addinfo);
3567 static void ir_handleDiags (IrTcl_Diagnostic **dst_list, int *dst_num,
3568 Z_DiagRec **list, int num)
3571 char *addinfo = NULL;
3574 *dst_list = ir_tcl_malloc (sizeof(**dst_list) * num);
3575 for (i = 0; i<num; i++)
3578 switch (list[i]->which)
3580 case Z_DiagRec_defaultFormat:
3581 (*dst_list)[i].condition = *list[i]->u.defaultFormat->condition;
3583 switch (list[i]->u.defaultFormat->which)
3585 case Z_DefaultDiagFormat_v2Addinfo:
3586 addinfo = list[i]->u.defaultFormat->u.v2Addinfo;
3588 case Z_DefaultDiagFormat_v3Addinfo:
3589 addinfo = list[i]->u.defaultFormat->u.v3Addinfo;
3593 addinfo = list[i]->u.defaultFormat->addinfo;
3596 ((*dst_list)[i].addinfo = ir_tcl_malloc (strlen(addinfo)+1)))
3597 strcpy ((*dst_list)[i].addinfo, addinfo);
3598 cp = diagbib1_str ((*dst_list)[i].condition);
3599 logf (LOG_DEBUG, "Diag %d %s %s", (*dst_list)[i].condition,
3600 cp ? cp : "", addinfo ? addinfo : "");
3603 (*dst_list)[i].addinfo = NULL;
3604 (*dst_list)[i].condition = 0;
3609 static void ir_handleDBRecord (IrTcl_Obj *p, IrTcl_RecordList *rl,
3612 struct oident *ident;
3613 Z_ext_typeent *etype;
3615 logf (LOG_DEBUG, "handleDBRecord size=%d", oe->u.octet_aligned->len);
3616 rl->u.dbrec.size = oe->u.octet_aligned->len;
3617 rl->u.dbrec.buf = NULL;
3619 if ((ident = oid_getentbyoid (oe->direct_reference)))
3620 rl->u.dbrec.type = ident->value;
3622 rl->u.dbrec.type = VAL_USMARC;
3624 if (ident && (oe->which == Z_External_single ||
3625 oe->which == Z_External_octet)
3626 && (etype = z_ext_getentbyref (ident->value)))
3630 odr_setbuf (p->odr_in, (char*) oe->u.octet_aligned->buf,
3631 oe->u.octet_aligned->len, 0);
3632 if (!(*etype->fun)(p->odr_in, (char **) &rr, 0))
3634 rl->u.dbrec.type = VAL_NONE;
3637 switch (etype->what)
3639 case Z_External_sutrs:
3640 logf (LOG_DEBUG, "Z_External_sutrs");
3642 if ((rl->u.dbrec.buf = ir_tcl_malloc (oe->u.sutrs->len+1)))
3644 memcpy (rl->u.dbrec.buf, oe->u.sutrs->buf,
3646 rl->u.dbrec.buf[oe->u.sutrs->len] = '\0';
3648 rl->u.dbrec.size = oe->u.sutrs->len;
3650 case Z_External_grs1:
3651 logf (LOG_DEBUG, "Z_External_grs1");
3653 ir_tcl_grs_mk (oe->u.grs1, &rl->u.dbrec.u.grs1);
3655 case Z_External_explainRecord:
3656 logf (LOG_DEBUG, "Z_External_explainRecord");
3657 if ((rl->u.dbrec.buf = ir_tcl_malloc (rl->u.dbrec.size)))
3659 memcpy (rl->u.dbrec.buf, oe->u.octet_aligned->buf,
3667 if (oe->which == Z_External_octet && rl->u.dbrec.size > 0)
3669 char *buf = (char*) oe->u.octet_aligned->buf;
3670 if ((rl->u.dbrec.buf = ir_tcl_malloc (rl->u.dbrec.size)))
3671 memcpy (rl->u.dbrec.buf, buf, rl->u.dbrec.size);
3673 else if (rl->u.dbrec.type == VAL_SUTRS &&
3674 oe->which == Z_External_sutrs)
3676 if ((rl->u.dbrec.buf = ir_tcl_malloc (oe->u.sutrs->len+1)))
3678 memcpy (rl->u.dbrec.buf, oe->u.sutrs->buf,
3680 rl->u.dbrec.buf[oe->u.sutrs->len] = '\0';
3682 rl->u.dbrec.size = oe->u.sutrs->len;
3684 else if (rl->u.dbrec.type == VAL_GRS1 &&
3685 oe->which == Z_External_grs1)
3687 ir_tcl_grs_mk (oe->u.grs1, &rl->u.dbrec.u.grs1);
3692 static void ir_handleZRecords (void *o, Z_Records *zrs, IrTcl_SetObj *setobj,
3693 const char *elements)
3698 IrTcl_RecordList *rl;
3700 setobj->which = zrs->which;
3701 setobj->recordFlag = 1;
3703 ir_deleteDiags (&setobj->nonSurrogateDiagnosticList,
3704 &setobj->nonSurrogateDiagnosticNum);
3705 if (zrs->which == Z_Records_DBOSD)
3707 int num_rec = zrs->u.databaseOrSurDiagnostics->num_records;
3709 if (num_rec != setobj->numberOfRecordsReturned)
3711 logf (LOG_WARN, "numberOfRecordsReturned=%d but num records=%d",
3712 setobj->numberOfRecordsReturned, num_rec);
3713 setobj->numberOfRecordsReturned = num_rec;
3716 for (offset = 0; offset < num_rec; offset++)
3718 Z_NamePlusRecord *znpr = zrs->u.databaseOrSurDiagnostics->
3721 rl = new_IR_record (setobj, setobj->start + offset, znpr->which,
3723 if (rl->which == Z_NamePlusRecord_surrogateDiagnostic)
3724 ir_handleDiags (&rl->u.surrogateDiagnostics.list,
3725 &rl->u.surrogateDiagnostics.num,
3726 &znpr->u.surrogateDiagnostic,
3729 ir_handleDBRecord (p, rl,
3730 (Z_External*) (znpr->u.databaseRecord));
3733 else if (zrs->which == Z_Records_multipleNSD)
3735 logf (LOG_DEBUG, "multipleNonSurrogateDiagnostic %d",
3736 zrs->u.multipleNonSurDiagnostics->num_diagRecs);
3737 setobj->numberOfRecordsReturned = 0;
3738 ir_handleDiags (&setobj->nonSurrogateDiagnosticList,
3739 &setobj->nonSurrogateDiagnosticNum,
3740 zrs->u.multipleNonSurDiagnostics->diagRecs,
3741 zrs->u.multipleNonSurDiagnostics->num_diagRecs);
3746 Z_DiagRec dr, *dr_p = &dr;
3747 dr.which = Z_DiagRec_defaultFormat;
3748 dr.u.defaultFormat = zrs->u.nonSurrogateDiagnostic;
3750 Z_DiagRec *dr_p = zrs->u.nonSurrogateDiagnostic;
3752 logf (LOG_DEBUG, "NonSurrogateDiagnostic");
3754 setobj->numberOfRecordsReturned = 0;
3755 ir_handleDiags (&setobj->nonSurrogateDiagnosticList,
3756 &setobj->nonSurrogateDiagnosticNum,
3761 static void ir_searchResponse (void *o, Z_SearchResponse *searchrs,
3762 IrTcl_SetObj *setobj)
3764 Z_Records *zrs = searchrs->records;
3766 logf (LOG_DEBUG, "Received search response");
3769 logf (LOG_DEBUG, "Search response, no object!");
3772 setobj->searchStatus = searchrs->searchStatus ? 1 : 0;
3773 get_referenceId (&setobj->set_inher.referenceId, searchrs->referenceId);
3774 setobj->resultCount = *searchrs->resultCount;
3775 if (searchrs->presentStatus)
3776 setobj->presentStatus = *searchrs->presentStatus;
3777 if (searchrs->nextResultSetPosition)
3778 setobj->nextResultSetPosition = *searchrs->nextResultSetPosition;
3780 logf (LOG_DEBUG, "status %d hits %d",
3781 setobj->searchStatus, setobj->resultCount);
3785 if (setobj->resultCount <= setobj->set_inher.smallSetUpperBound)
3786 es = setobj->set_inher.smallSetElementSetNames;
3788 es = setobj->set_inher.mediumSetElementSetNames;
3789 setobj->numberOfRecordsReturned = *searchrs->numberOfRecordsReturned;
3790 ir_handleZRecords (o, zrs, setobj, es);
3794 setobj->numberOfRecordsReturned = 0;
3795 setobj->recordFlag = 0;
3800 static void ir_presentResponse (void *o, Z_PresentResponse *presrs,
3801 IrTcl_SetObj *setobj)
3803 Z_Records *zrs = presrs->records;
3805 logf (LOG_DEBUG, "Received present response");
3808 logf (LOG_DEBUG, "Present response, no object!");
3811 setobj->presentStatus = *presrs->presentStatus;
3812 get_referenceId (&setobj->set_inher.referenceId, presrs->referenceId);
3813 setobj->nextResultSetPosition = *presrs->nextResultSetPosition;
3816 setobj->numberOfRecordsReturned = *presrs->numberOfRecordsReturned;
3817 ir_handleZRecords (o, zrs, setobj, setobj->set_inher.elementSetNames);
3821 setobj->numberOfRecordsReturned = 0;
3822 setobj->recordFlag = 0;
3823 logf (LOG_DEBUG, "No records!");
3827 static void ir_scanResponse (void *o, Z_ScanResponse *scanrs,
3828 IrTcl_ScanObj *scanobj)
3832 logf (LOG_DEBUG, "Received scanResponse");
3834 get_referenceId (&p->set_inher.referenceId, scanrs->referenceId);
3835 scanobj->scanStatus = *scanrs->scanStatus;
3836 logf (LOG_DEBUG, "scanStatus=%d", scanobj->scanStatus);
3838 if (scanrs->stepSize)
3839 scanobj->stepSize = *scanrs->stepSize;
3840 logf (LOG_DEBUG, "stepSize=%d", scanobj->stepSize);
3842 scanobj->numberOfEntriesReturned = *scanrs->numberOfEntriesReturned;
3843 logf (LOG_DEBUG, "numberOfEntriesReturned=%d",
3844 scanobj->numberOfEntriesReturned);
3846 if (scanrs->positionOfTerm)
3847 scanobj->positionOfTerm = *scanrs->positionOfTerm;
3849 scanobj->positionOfTerm = -1;
3850 logf (LOG_DEBUG, "positionOfTerm=%d", scanobj->positionOfTerm);
3852 xfree (scanobj->entries);
3853 scanobj->entries = NULL;
3854 scanobj->num_entries = 0;
3855 scanobj->entries_flag = 0;
3857 ir_deleteDiags (&scanobj->nonSurrogateDiagnosticList,
3858 &scanobj->nonSurrogateDiagnosticNum);
3859 if (scanrs->entries)
3864 scanobj->entries_flag = 1;
3866 if (scanrs->entries)
3868 scanobj->num_entries = scanrs->entries->num_entries;
3869 scanobj->entries = ir_tcl_malloc (scanobj->num_entries *
3870 sizeof(*scanobj->entries));
3871 ze = scanrs->entries->entries;
3874 if (scanrs->entries->which == Z_ListEntries_entries)
3876 scanobj->num_entries = scanrs->entries->u.entries->num_entries;
3877 scanobj->entries = ir_tcl_malloc (scanobj->num_entries *
3878 sizeof(*scanobj->entries));
3879 ze = scanrs->entries->u.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,
3914 if (scanrs->entries->nonsurrogateDiagnostics)
3915 ir_handleDiags (&scanobj->nonSurrogateDiagnosticList,
3916 &scanobj->nonSurrogateDiagnosticNum,
3917 scanrs->entries->nonsurrogateDiagnostics,
3918 scanrs->entries->num_nonsurrogateDiagnostics);
3920 if (scanrs->entries->which == Z_ListEntries_nonSurrogateDiagnostics)
3921 ir_handleDiags (&scanobj->nonSurrogateDiagnosticList,
3922 &scanobj->nonSurrogateDiagnosticNum,
3923 scanrs->entries->u.nonSurrogateDiagnostics->
3925 scanrs->entries->u.nonSurrogateDiagnostics->
3932 * ir_select_read: handle incoming packages
3934 static void ir_select_read (ClientData clientData)
3936 IrTcl_Obj *p = (IrTcl_Obj *) clientData;
3941 Tcl_CmdInfo cmd_info;
3942 const char *apdu_call;
3944 logf(LOG_DEBUG, "Read handler fd=%d", cs_fileno(p->cs_link));
3945 if (p->state == IR_TCL_R_Connecting)
3947 logf(LOG_DEBUG, "read: connect");
3948 r = cs_rcvconnect (p->cs_link);
3951 logf (LOG_WARN, "cs_rcvconnect returned 1");
3954 p->state = IR_TCL_R_Idle;
3956 ir_select_remove_write (cs_fileno (p->cs_link), p);
3959 logf (LOG_DEBUG, "cs_rcvconnect error");
3960 ir_tcl_disconnect (p);
3963 p->failInfo = IR_TCL_FAIL_CONNECT;
3964 ir_tcl_eval (p->interp, p->failback);
3966 ir_obj_delete ((ClientData) p);
3970 ir_tcl_eval (p->interp, p->callback);
3971 if (p->ref_count == 2 && p->cs_link && p->request_queue
3972 && p->state == IR_TCL_R_Idle)
3973 ir_tcl_send_q (p, p->request_queue, "x");
3974 ir_obj_delete ((ClientData) p);
3979 p->state = IR_TCL_R_Reading;
3981 /* read incoming APDU */
3982 if ((r=cs_get (p->cs_link, &p->buf_in, &p->len_in)) == 1)
3984 logf(LOG_DEBUG, "PDU Fraction read");
3987 /* signal one more use of ir object - callbacks must not
3988 release the ir memory (p pointer) */
3992 logf (LOG_DEBUG, "cs_get failed, code %d", r);
3993 ir_tcl_disconnect (p);
3996 p->failInfo = IR_TCL_FAIL_READ;
3997 ir_tcl_eval (p->interp, p->failback);
3999 /* release ir object now if callback deleted it */
4000 ir_obj_delete ((ClientData) p);
4003 /* got complete APDU. Now decode */
4006 odr_setbuf (p->odr_in, p->buf_in, r, 0);
4007 logf (LOG_DEBUG, "cs_get ok, total size %d", r);
4008 if (!z_APDU (p->odr_in, &apdu, 0))
4010 logf (LOG_DEBUG, "cs_get failed: %s",
4011 odr_errmsg (odr_geterror (p->odr_in)));
4012 ir_tcl_disconnect (p);
4015 p->failInfo = IR_TCL_FAIL_IN_APDU;
4016 p->apduOffset = odr_offset (p->odr_in);
4017 ir_tcl_eval (p->interp, p->failback);
4019 /* release ir object now if failback deleted it */
4020 ir_obj_delete ((ClientData) p);
4023 /* handle APDU and invoke callback */
4024 rq = p->request_queue;
4027 logf (LOG_FATAL, "Internal error. No queue entry");
4030 object_name = rq->object_name;
4031 logf (LOG_DEBUG, "Object %s", object_name);
4033 if (Tcl_GetCommandInfo (p->interp, object_name, &cmd_info))
4037 case Z_APDU_initResponse:
4038 p->eventType = "init";
4039 ir_initResponse (p, apdu->u.initResponse);
4040 apdu_call = p->initResponse;
4042 case Z_APDU_searchResponse:
4043 p->eventType = "search";
4044 ir_searchResponse (p, apdu->u.searchResponse,
4045 (IrTcl_SetObj *) cmd_info.clientData);
4046 apdu_call = ((IrTcl_SetObj *)
4047 cmd_info.clientData)->searchResponse;
4049 case Z_APDU_presentResponse:
4050 p->eventType = "present";
4051 ir_presentResponse (p, apdu->u.presentResponse,
4052 (IrTcl_SetObj *) cmd_info.clientData);
4053 apdu_call = ((IrTcl_SetObj *)
4054 cmd_info.clientData)->presentResponse;
4056 case Z_APDU_scanResponse:
4057 p->eventType = "scan";
4058 ir_scanResponse (p, apdu->u.scanResponse,
4059 (IrTcl_ScanObj *) cmd_info.clientData);
4060 apdu_call = ((IrTcl_ScanObj *)
4061 cmd_info.clientData)->scanResponse;
4064 logf (LOG_WARN, "Received unknown APDU type (%d)",
4066 ir_tcl_disconnect (p);
4069 p->failInfo = IR_TCL_FAIL_UNKNOWN_APDU;
4070 ir_tcl_eval (p->interp, p->failback);
4075 p->request_queue = rq->next;
4076 p->state = IR_TCL_R_Idle;
4079 ir_tcl_eval (p->interp, apdu_call);
4080 else if (rq->callback)
4081 ir_tcl_eval (p->interp, rq->callback);
4082 xfree (rq->buf_out);
4083 xfree (rq->callback);
4084 xfree (rq->object_name);
4086 odr_reset (p->odr_in);
4087 if (p->ref_count == 1)
4089 ir_obj_delete ((ClientData) p);
4092 ir_obj_delete ((ClientData) p);
4093 } while (p->cs_link && cs_more (p->cs_link));
4094 if (p->cs_link && p->request_queue && p->state == IR_TCL_R_Idle)
4095 ir_tcl_send_q (p, p->request_queue, "x");
4099 * ir_select_write: handle outgoing packages - not yet written.
4101 static int ir_select_write (ClientData clientData)
4103 IrTcl_Obj *p = (IrTcl_Obj *) clientData;
4107 logf (LOG_DEBUG, "Write handler fd=%d", cs_fileno(p->cs_link));
4108 if (p->state == IR_TCL_R_Connecting)
4110 logf(LOG_DEBUG, "write: connect");
4111 r = cs_rcvconnect (p->cs_link);
4114 logf (LOG_DEBUG, "cs_rcvconnect returned 1");
4117 p->state = IR_TCL_R_Idle;
4119 ir_select_remove_write (cs_fileno (p->cs_link), p);
4122 logf (LOG_DEBUG, "cs_rcvconnect error");
4123 ir_tcl_disconnect (p);
4126 p->failInfo = IR_TCL_FAIL_CONNECT;
4127 ir_tcl_eval (p->interp, p->failback);
4129 ir_obj_delete ((ClientData) p);
4133 ir_tcl_eval (p->interp, p->callback);
4134 ir_obj_delete ((ClientData) p);
4137 rq = p->request_queue;
4138 if (!rq || !rq->buf_out)
4141 if ((r=cs_put (p->cs_link, rq->buf_out, rq->len_out)) < 0)
4143 logf (LOG_DEBUG, "cs_put write fail");
4145 xfree (rq->buf_out);
4147 ir_tcl_disconnect (p);
4150 p->failInfo = IR_TCL_FAIL_WRITE;
4151 ir_tcl_eval (p->interp, p->failback);
4153 ir_obj_delete ((ClientData) p);
4155 else if (r == 0) /* remove select bit */
4157 logf (LOG_DEBUG, "Write completed");
4158 p->state = IR_TCL_R_Waiting;
4159 ir_select_remove_write (cs_fileno (p->cs_link), p);
4160 xfree (rq->buf_out);
4166 static void ir_select_notify (ClientData clientData, int r, int w, int e)
4170 if (!ir_select_write (clientData) && r)
4171 ir_select_read (clientData);
4175 ir_select_read (clientData);
4179 /*----------------------------------------------------------- */
4183 * This wrapper function is used by Windows to invoke the
4184 * initialization code for the DLL. If we are compiling
4185 * with Visual C++, this routine will be renamed to DllMain.
4197 DllEntryPoint(hInst, reason, reserved)
4198 HINSTANCE hInst; /* Library instance handle. */
4199 DWORD reason; /* Reason this function is being called. */
4200 LPVOID reserved; /* Not used. */
4206 /* ------------------------------------------------------- */
4208 * Irtcl_init: Registration of TCL commands.
4210 EXPORT (int,Irtcl_Init) (Tcl_Interp *interp)
4212 Tcl_CreateCommand (interp, "ir", ir_obj_mk, (ClientData) NULL,
4213 (Tcl_CmdDeleteProc *) NULL);
4214 Tcl_CreateCommand (interp, "ir-set", ir_set_obj_mk,
4215 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
4216 Tcl_CreateCommand (interp, "ir-scan", ir_scan_obj_mk,
4217 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
4218 Tcl_CreateCommand (interp, "ir-log-init", ir_log_init_proc,
4219 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
4220 Tcl_CreateCommand (interp, "ir-log", ir_log_proc,
4221 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
4222 Tcl_CreateCommand (interp, "ir-version", ir_version, (ClientData) NULL,
4223 (Tcl_CmdDeleteProc *) NULL);