2 * IR toolkit for tcl/tk
3 * (c) Index Data 1995-1996
4 * See the file LICENSE for details.
5 * Sebastian Hammer, Adam Dickmeiss
8 * Revision 1.95 1996-09-13 10:51:49 adam
9 * Bug fix: ir_tcl_select_set called Tcl_GetFile at disconnect.
11 * Revision 1.94 1996/08/21 13:32:53 adam
12 * Implemented saveFile method and extended loadFile method to work with it.
14 * Revision 1.93 1996/08/16 15:07:45 adam
15 * First work on Explain.
17 * Revision 1.92 1996/08/09 15:33:07 adam
18 * Modified the code to use tk4.1/tcl7.5 patch level 1. The time-driven
19 * polling is no longer activated on Windows since asynchrounous I/O works
22 * Revision 1.91 1996/07/03 13:31:11 adam
23 * The xmalloc/xfree functions from YAZ are used to manage memory.
25 * Revision 1.90 1996/06/27 14:21:00 adam
26 * Yet another Windows port.
28 * Revision 1.89 1996/06/11 15:27:15 adam
29 * Event type set to connect a little earlier in the do_connect function.
31 * Revision 1.88 1996/06/03 09:04:22 adam
32 * Changed a few logf calls.
34 * Revision 1.87 1996/05/29 06:37:51 adam
35 * Function ir_tcl_get_grs_r enhanced so that specific elements can be
38 * Revision 1.86 1996/03/20 13:54:04 adam
39 * The Tcl_File structure is only manipulated in the Tk-event interface
42 * Revision 1.85 1996/03/15 11:15:48 adam
43 * Modified to use new prototypes for p_query_rpn and p_query_scan.
45 * Revision 1.84 1996/03/07 12:42:49 adam
46 * Better logging when callback is invoked.
48 * Revision 1.83 1996/03/05 09:21:09 adam
49 * Bug fix: memory used by GRS records wasn't freed.
50 * Rewrote some of the error handling code - the connection is always
51 * closed before failback is called.
52 * If failback is defined the send APDU methods (init, search, ...) will
53 * return OK but invoke failback (as is the case if the write operation
55 * Bug fix: ref_count in assoc object could grow if fraction of PDU was
58 * Revision 1.82 1996/02/29 15:30:21 adam
59 * Export of IrTcl functionality to extensions.
61 * Revision 1.81 1996/02/26 18:38:32 adam
62 * Work on export of set methods.
64 * Revision 1.80 1996/02/23 17:31:39 adam
65 * More functions made available to the wais tcl extension.
67 * Revision 1.79 1996/02/23 13:41:38 adam
68 * Work on public access to simple ir class system.
70 * Revision 1.78 1996/02/21 10:16:08 adam
71 * Simplified select handling. Only one function ir_tcl_select_set has
72 * to be externally defined.
74 * Revision 1.77 1996/02/20 17:52:58 adam
75 * Uses the YAZ oid system to name record syntax object identifiers.
77 * Revision 1.76 1996/02/20 16:09:51 adam
78 * Bug fix: didn't set element set names stamp correctly on result
79 * set records when element set names were set to the empty string.
81 * Revision 1.75 1996/02/19 15:41:53 adam
82 * Better log messages.
83 * Minor improvement of connect method.
85 * Revision 1.74 1996/02/05 17:58:03 adam
86 * Ported ir-tcl to use the beta releases of tcl7.5/tk4.1.
88 * Revision 1.73 1996/01/29 11:35:19 adam
89 * Bug fix: cs_type member renamed to comstackType to avoid conflict with
90 * cs_type macro defined by YAZ.
92 * Revision 1.72 1996/01/19 17:45:34 quinn
93 * Added debugging output
95 * Revision 1.71 1996/01/19 16:22:38 adam
96 * New method: apduDump - returns information about last incoming APDU.
98 * Revision 1.70 1996/01/10 09:18:34 adam
99 * PDU specific callbacks implemented: initRespnse, searchResponse,
100 * presentResponse and scanResponse.
101 * Bug fix in the command line shell (tclmain.c) - discovered on OSF/1.
103 * Revision 1.69 1996/01/04 16:12:12 adam
104 * Setting PDUType renamed to eventType.
106 * Revision 1.68 1996/01/04 11:05:22 adam
107 * New setting: PDUType - returns type of last PDU returned from the target.
108 * Fixed a bug in configure/Makefile.
110 * Revision 1.67 1996/01/03 09:00:51 adam
111 * Updated to use new version of Yaz (names changed to avoid C++ conflict).
113 * Revision 1.66 1995/11/28 17:26:39 adam
114 * Removed Carriage return from ir-tcl.c!
115 * Removed misc. debug logs.
117 * Revision 1.65 1995/11/28 13:53:00 quinn
120 * Revision 1.64 1995/11/13 15:39:18 adam
121 * Bug fix: {small,medium}SetElementSetNames weren't set correctly.
122 * Bug fix: idAuthentication weren't set correctly.
124 * Revision 1.63 1995/11/13 09:55:39 adam
125 * Multiple records at a position in a result-set with differnt
128 * Revision 1.62 1995/10/18 17:20:33 adam
129 * Work on target setup in client.tcl.
131 * Revision 1.61 1995/10/18 16:42:42 adam
132 * New settings: smallSetElementSetNames and mediumSetElementSetNames.
134 * Revision 1.60 1995/10/18 15:43:31 adam
135 * In search: mediumSetElementSetNames and smallSetElementSetNames are
136 * set to elementSetNames.
138 * Revision 1.59 1995/10/17 12:18:58 adam
139 * Bug fix: when target connection closed, the connection was not
140 * properly reestablished.
142 * Revision 1.58 1995/10/16 17:00:55 adam
143 * New setting: elementSetNames.
144 * Various client improvements. Medium presentation format looks better.
146 * Revision 1.57 1995/09/21 13:11:51 adam
147 * Support of dynamic loading.
148 * Test script uses load command if necessary.
150 * Revision 1.56 1995/08/29 15:30:14 adam
151 * Work on GRS records.
153 * Revision 1.55 1995/08/28 09:43:25 adam
154 * Minor changes. configure only searches for yaz beta 3 and versions after
157 * Revision 1.54 1995/08/24 12:25:16 adam
158 * Modified to work with yaz 1.0b3.
160 * Revision 1.53 1995/08/04 12:49:26 adam
161 * Bug fix: reading uninitialized variable p.
163 * Revision 1.52 1995/08/04 11:32:38 adam
164 * More work on output queue. Memory related routines moved
167 * Revision 1.51 1995/08/03 13:22:54 adam
170 * Revision 1.50 1995/07/20 08:09:49 adam
171 * client.tcl: Targets removed from hotTargets list when targets
172 * are removed/modified.
173 * ir-tcl.c: More work on triggerResourceControl.
175 * Revision 1.49 1995/06/30 12:39:21 adam
176 * Bug fix: loadFile didn't set record type.
177 * The MARC routines are a little less strict in the interpretation.
178 * Script display.tcl replaces the old marc.tcl.
179 * New interactive script: shell.tcl.
181 * Revision 1.48 1995/06/27 19:03:50 adam
182 * Bug fix in do_present in ir-tcl.c: p->set_child member weren't set.
183 * nextResultSetPosition used instead of setOffset.
185 * Revision 1.47 1995/06/25 10:25:04 adam
186 * Working on triggerResourceControl. Description of compile/install
187 * procedure moved to ir-tcl.sgml.
189 * Revision 1.46 1995/06/22 13:15:06 adam
190 * Feature: SUTRS. Setting getSutrs implemented.
191 * Work on display formats.
192 * Preferred record syntax can be set by the user.
194 * Revision 1.45 1995/06/20 08:07:30 adam
195 * New setting: failInfo.
196 * Working on better cancel mechanism.
198 * Revision 1.44 1995/06/19 17:01:20 adam
201 * Revision 1.43 1995/06/19 13:06:08 adam
202 * New define: IR_TCL_VERSION.
204 * Revision 1.42 1995/06/19 08:08:52 adam
205 * client.tcl: hotTargets now contain both database and target name.
206 * ir-tcl.c: setting protocol edited. Errors in callbacks are logged
207 * by logf(LOG_WARN, ...) calls.
209 * Revision 1.41 1995/06/16 12:28:16 adam
210 * Implemented preferredRecordSyntax.
211 * Minor changes in diagnostic handling.
212 * Record list deleted when connection closes.
214 * Revision 1.40 1995/06/14 13:37:18 adam
215 * Setting recordType implemented.
216 * Setting implementationVersion implemented.
217 * Settings implementationId / implementationName edited.
219 * Revision 1.39 1995/06/08 10:26:32 adam
220 * Bug fix in ir_strdup.
222 * Revision 1.38 1995/06/01 16:36:47 adam
223 * About buttons. Minor bug fixes.
225 * Revision 1.37 1995/06/01 07:31:20 adam
226 * Rename of many typedefs -> IrTcl_...
228 * Revision 1.36 1995/05/31 13:09:59 adam
229 * Client searches/presents may be interrupted.
230 * New moving book-logo.
232 * Revision 1.35 1995/05/31 08:36:33 adam
233 * Bug fix in client.tcl: didn't save options on clientrc.tcl.
234 * New method: referenceId. More work on scan.
236 * Revision 1.34 1995/05/29 10:33:42 adam
237 * README and rename of startup script.
239 * Revision 1.33 1995/05/29 09:15:11 quinn
240 * Changed CS_SR to PROTO_SR, etc.
242 * Revision 1.32 1995/05/29 08:44:16 adam
243 * Work on delete of objects.
245 * Revision 1.31 1995/05/26 11:44:10 adam
246 * Bugs fixed. More work on MARC utilities and queries. Test
247 * client is up-to-date again.
249 * Revision 1.30 1995/05/26 08:54:11 adam
250 * New MARC utilities. Uses prefix query.
252 * Revision 1.29 1995/05/24 14:10:22 adam
253 * Work on idAuthentication, protocolVersion and options.
255 * Revision 1.28 1995/05/23 15:34:48 adam
256 * Many new settings, userInformationField, smallSetUpperBound, etc.
257 * A number of settings are inherited when ir-set is executed.
258 * This version is incompatible with the graphical test client (client.tcl).
260 * Revision 1.27 1995/05/11 15:34:47 adam
261 * Scan request changed a bit. This version works with RLG.
263 * Revision 1.26 1995/04/18 16:11:51 adam
264 * First version of graphical Scan. Some work on query-by-form.
266 * Revision 1.25 1995/04/17 09:37:17 adam
267 * Further development of scan.
269 * Revision 1.24 1995/04/11 14:16:42 adam
270 * Further work on scan. Response works. Entries aren't saved yet.
272 * Revision 1.23 1995/04/10 10:50:27 adam
273 * Result-set name defaults to suffix of ir-set name.
274 * Started working on scan. Not finished at this point.
276 * Revision 1.22 1995/03/31 10:43:03 adam
277 * More robust when getting bad MARC records.
279 * Revision 1.21 1995/03/31 08:56:37 adam
280 * New button "Search".
282 * Revision 1.20 1995/03/29 16:07:09 adam
283 * Bug fix: Didn't use setName in present request.
285 * Revision 1.19 1995/03/28 12:45:23 adam
286 * New ir method failback: called on disconnect/protocol error.
287 * New ir set/get method: protocol: SR / Z3950.
288 * Simple popup and disconnect when failback is invoked.
290 * Revision 1.18 1995/03/21 15:50:12 adam
293 * Revision 1.17 1995/03/21 13:41:03 adam
294 * Comstack cs_create not used too often. Non-blocking connect.
296 * Revision 1.16 1995/03/21 08:26:06 adam
297 * New method, setName, to specify the result set name (other than Default).
298 * New method, responseStatus, which returns diagnostic info, if any, after
299 * present response / search response.
301 * Revision 1.15 1995/03/20 15:24:07 adam
302 * Diagnostic records saved on searchResponse.
304 * Revision 1.14 1995/03/20 08:53:22 adam
305 * Event loop in tclmain.c rewritten. New method searchStatus.
307 * Revision 1.13 1995/03/17 18:26:17 adam
308 * Non-blocking i/o used now. Database names popup as cascade items.
310 * Revision 1.12 1995/03/17 15:45:00 adam
311 * Improved target/database setup.
313 * Revision 1.11 1995/03/16 17:54:03 adam
314 * Minor changes really.
316 * Revision 1.10 1995/03/15 16:14:50 adam
317 * Blocking arg in cs_create changed.
319 * Revision 1.9 1995/03/15 13:59:24 adam
322 * Revision 1.8 1995/03/15 08:25:16 adam
323 * New method presentStatus to check for error on present. Misc. cleanup
324 * of IrTcl_RecordList manipulations. Full MARC record presentation in
327 * Revision 1.7 1995/03/14 17:32:29 adam
328 * Presentation of full Marc record in popup window.
330 * Revision 1.6 1995/03/12 19:31:55 adam
331 * Pattern matching implemented when retrieving MARC records. More
332 * diagnostic functions.
334 * Revision 1.5 1995/03/10 18:00:15 adam
335 * Actual presentation in line-by-line format. RPN query support.
337 * Revision 1.4 1995/03/09 16:15:08 adam
338 * First presentRequest attempts. Hot-target list.
356 static void ir_deleteDiags (IrTcl_Diagnostic **dst_list, int *dst_num);
358 static void ir_select_notify (ClientData clientData, int r, int w, int e);
360 void ir_select_add (int fd, void *obj)
362 ir_tcl_select_set (ir_select_notify, fd, obj, 1, 0, 0);
365 void ir_select_add_write (int fd, void *obj)
367 ir_tcl_select_set (ir_select_notify, fd, obj, 1, 1, 0);
370 void ir_select_remove (int fd, void *obj)
372 ir_tcl_select_set (NULL, fd, obj, 0, 0, 0);
375 void ir_select_remove_write (int fd, void *obj)
377 ir_tcl_select_set (ir_select_notify, fd, obj, 1, 0, 0);
380 static void delete_IR_record (IrTcl_RecordList *rl)
384 case Z_NamePlusRecord_databaseRecord:
385 switch (rl->u.dbrec.type)
388 ir_tcl_grs_del (&rl->u.dbrec.u.grs1);
393 xfree (rl->u.dbrec.buf);
394 rl->u.dbrec.buf = NULL;
396 case Z_NamePlusRecord_surrogateDiagnostic:
397 ir_deleteDiags (&rl->u.surrogateDiagnostics.list,
398 &rl->u.surrogateDiagnostics.num);
401 xfree (rl->elements);
404 static IrTcl_RecordList *new_IR_record (IrTcl_SetObj *setobj,
406 const char *elements)
408 IrTcl_RecordList *rl;
410 if (elements && !*elements)
412 for (rl = setobj->record_list; rl; rl = rl->next)
414 if (no == rl->no && (!rl->elements || !elements ||
415 !strcmp(elements, rl->elements)))
417 delete_IR_record (rl);
423 rl = ir_tcl_malloc (sizeof(*rl));
424 rl->next = setobj->record_list;
426 setobj->record_list = rl;
429 ir_tcl_strdup (NULL, &rl->elements, elements);
436 int ir_tcl_eval (Tcl_Interp *interp, const char *command)
438 char *tmp = ir_tcl_malloc (strlen(command)+1);
441 logf (LOG_DEBUG, "Invoking %.23s ...", command);
442 strcpy (tmp, command);
443 r = Tcl_Eval (interp, tmp);
446 logf (LOG_WARN, "Tcl error in line %d: %s", interp->errorLine,
449 Tcl_FreeResult (interp);
455 * IrTcl_getRecordSyntaxStr: Return record syntax name of object id
457 static char *IrTcl_getRecordSyntaxStr (enum oid_value value)
460 struct oident ent, *entp;
462 ent.proto = PROTO_Z3950;
463 ent.oclass = CLASS_RECSYN;
466 o = oid_getoidbyent (&ent);
467 entp = oid_getentbyoid (o);
475 * IrTcl_getRecordSyntaxVal: Return record syntax value of string
477 static enum oid_value IrTcl_getRecordSyntaxVal (const char *name)
479 return oid_getvalbyname (name);
482 static IrTcl_RecordList *find_IR_record (IrTcl_SetObj *setobj, int no)
484 IrTcl_RecordList *rl;
486 for (rl = setobj->record_list; rl; rl = rl->next)
488 (!setobj->recordElements || !rl->elements ||
489 !strcmp (setobj->recordElements, rl->elements)))
494 static void delete_IR_records (IrTcl_SetObj *setobj)
496 IrTcl_RecordList *rl, *rl1;
498 for (rl = setobj->record_list; rl; rl = rl1)
500 delete_IR_record (rl);
504 setobj->record_list = NULL;
508 * ir_tcl_get_set_int: Set/get integer value
510 int ir_tcl_get_set_int (int *val, Tcl_Interp *interp, int argc, char **argv)
516 if (Tcl_GetInt (interp, argv[2], val)==TCL_ERROR)
519 sprintf (buf, "%d", *val);
520 Tcl_AppendResult (interp, buf, NULL);
525 * ir_tcl_method: Search for method in table and invoke method handler
527 int ir_tcl_method (Tcl_Interp *interp, int argc, char **argv,
528 IrTcl_Methods *tab, int *ret)
530 IrTcl_Methods *tab_i = tab;
533 for (tab_i = tab; tab_i->tab; tab_i++)
534 for (t = tab_i->tab; t->name; t++)
537 if ((*t->method)(tab_i->obj, interp, argc, argv) == TCL_ERROR)
541 if (!strcmp (t->name, argv[1]))
543 *ret = (*t->method)(tab_i->obj, interp, argc, argv);
550 Tcl_AppendResult (interp, "Bad method: ", argv[1],
551 ". Possible methods:", NULL);
552 for (tab_i = tab; tab_i->tab; tab_i++)
553 for (t = tab_i->tab; t->name; t++)
554 Tcl_AppendResult (interp, " ", t->name, NULL);
561 * ir_tcl_named_bits: get/set named bits
563 int ir_tcl_named_bits (struct ir_named_entry *tab, Odr_bitmask *ob,
564 Tcl_Interp *interp, int argc, char **argv)
566 struct ir_named_entry *ti;
571 for (no = 0; no < argc; no++)
573 for (ti = tab; ti->name; ti++)
574 if (!strcmp (argv[no], ti->name))
576 ODR_MASK_SET (ob, ti->pos);
581 Tcl_AppendResult (interp, "Bad bit mask: ", argv[no], NULL);
587 for (ti = tab; ti->name; ti++)
588 if (ODR_MASK_GET (ob, ti->pos))
589 Tcl_AppendElement (interp, ti->name);
593 static void set_referenceId (ODR o, Z_ReferenceId **dst, const char *src)
599 *dst = odr_malloc (o, sizeof(**dst));
600 (*dst)->size = (*dst)->len = strlen(src);
601 (*dst)->buf = odr_malloc (o, (*dst)->len);
602 memcpy ((*dst)->buf, src, (*dst)->len);
606 static void get_referenceId (char **dst, Z_ReferenceId *src)
614 *dst = ir_tcl_malloc (src->len+1);
615 memcpy (*dst, src->buf, src->len);
616 (*dst)[src->len] = '\0';
619 /* ------------------------------------------------------- */
622 * do_init_request: init method on IR object
624 static int do_init_request (void *obj, Tcl_Interp *interp,
625 int argc, char **argv)
633 logf (LOG_DEBUG, "init %s", *argv);
636 interp->result = "init: not connected";
639 apdu = zget_APDU (p->odr_out, Z_APDU_initRequest);
640 req = apdu->u.initRequest;
642 set_referenceId (p->odr_out, &req->referenceId, p->set_inher.referenceId);
643 req->options = &p->options;
644 req->protocolVersion = &p->protocolVersion;
645 req->preferredMessageSize = &p->preferredMessageSize;
646 req->maximumRecordSize = &p->maximumRecordSize;
648 if (p->idAuthenticationGroupId)
650 Z_IdPass *pass = odr_malloc (p->odr_out, sizeof(*pass));
651 Z_IdAuthentication *auth = odr_malloc (p->odr_out, sizeof(*auth));
653 logf (LOG_DEBUG, "using pass authentication");
655 auth->which = Z_IdAuthentication_idPass;
656 auth->u.idPass = pass;
657 if (p->idAuthenticationGroupId && *p->idAuthenticationGroupId)
658 pass->groupId = p->idAuthenticationGroupId;
660 pass->groupId = NULL;
661 if (p->idAuthenticationUserId && *p->idAuthenticationUserId)
662 pass->userId = p->idAuthenticationUserId;
665 if (p->idAuthenticationPassword && *p->idAuthenticationPassword)
666 pass->password = p->idAuthenticationPassword;
668 pass->password = NULL;
669 req->idAuthentication = auth;
671 else if (!p->idAuthenticationOpen || !*p->idAuthenticationOpen)
672 req->idAuthentication = NULL;
675 Z_IdAuthentication *auth = odr_malloc (p->odr_out, sizeof(*auth));
677 logf (LOG_DEBUG, "using open authentication");
678 auth->which = Z_IdAuthentication_open;
679 auth->u.open = p->idAuthenticationOpen;
680 req->idAuthentication = auth;
682 req->implementationId = p->implementationId;
683 req->implementationName = p->implementationName;
684 req->implementationVersion = p->implementationVersion;
685 req->userInformationField = 0;
687 return ir_tcl_send_APDU (interp, p, apdu, "init", *argv);
691 * do_protocolVersion: Set protocol Version
693 static int do_protocolVersion (void *obj, Tcl_Interp *interp,
694 int argc, char **argv)
702 ODR_MASK_ZERO (&p->protocolVersion);
703 ODR_MASK_SET (&p->protocolVersion, 0);
704 ODR_MASK_SET (&p->protocolVersion, 1);
709 if (Tcl_GetInt (interp, argv[2], &version)==TCL_ERROR)
711 ODR_MASK_ZERO (&p->protocolVersion);
712 for (i = 0; i<version; i++)
713 ODR_MASK_SET (&p->protocolVersion, i);
715 for (i = 4; --i >= 0; )
716 if (ODR_MASK_GET (&p->protocolVersion, i))
718 sprintf (buf, "%d", i+1);
719 interp->result = buf;
724 * do_options: Set options
726 static int do_options (void *obj, Tcl_Interp *interp,
727 int argc, char **argv)
729 static struct ir_named_entry options_tab[] = {
733 { "resourceReport", 3 },
734 { "triggerResourceCtrl", 4},
735 { "resourceCtrl", 5},
739 { "extendedServices", 10},
740 { "level-1Segmentation", 11},
741 { "level-2Segmentation", 12},
742 { "concurrentOperations", 13},
743 { "namedResultSets", 14},
750 ODR_MASK_ZERO (&p->options);
751 ODR_MASK_SET (&p->options, 0);
752 ODR_MASK_SET (&p->options, 1);
753 ODR_MASK_SET (&p->options, 4);
754 ODR_MASK_SET (&p->options, 7);
755 ODR_MASK_SET (&p->options, 14);
758 return ir_tcl_named_bits (options_tab, &p->options, interp, argc-2, argv+2);
762 * do_apduInfo: Get APDU information
764 static int do_apduInfo (void *obj, Tcl_Interp *interp, int argc, char **argv)
772 sprintf (buf, "%d", p->apduLen);
773 Tcl_AppendElement (interp, buf);
774 sprintf (buf, "%d", p->apduOffset);
775 Tcl_AppendElement (interp, buf);
778 Tcl_AppendElement (interp, "");
781 apduf = fopen ("apdu.tmp", "w");
784 Tcl_AppendElement (interp, "");
787 odr_dumpBER (apduf, p->buf_in, p->apduLen);
789 if (!(apduf = fopen ("apdu.tmp", "r")))
790 Tcl_AppendElement (interp, "");
795 Tcl_AppendResult (interp, " {", NULL);
796 while ((c = getc (apduf)) != EOF)
800 Tcl_AppendResult (interp, buf, NULL);
803 Tcl_AppendResult (interp, "}", NULL);
810 * do_failInfo: Get fail information
812 static int do_failInfo (void *obj, Tcl_Interp *interp, int argc, char **argv)
822 sprintf (buf, "%d", p->failInfo);
828 case IR_TCL_FAIL_CONNECT:
829 cp = "connect failed";
831 case IR_TCL_FAIL_READ:
832 cp = "connection closed";
834 case IR_TCL_FAIL_WRITE:
835 cp = "connection closed";
837 case IR_TCL_FAIL_IN_APDU:
838 cp = "failed to decode incoming APDU";
840 case IR_TCL_FAIL_UNKNOWN_APDU:
846 Tcl_AppendElement (interp, buf);
847 Tcl_AppendElement (interp, cp);
852 * do_preferredMessageSize: Set/get preferred message size
854 static int do_preferredMessageSize (void *obj, Tcl_Interp *interp,
855 int argc, char **argv)
861 p->preferredMessageSize = 30000;
864 return ir_tcl_get_set_int (&p->preferredMessageSize, interp, argc, argv);
868 * do_maximumRecordSize: Set/get maximum record size
870 static int do_maximumRecordSize (void *obj, Tcl_Interp *interp,
871 int argc, char **argv)
877 p->maximumRecordSize = 30000;
880 return ir_tcl_get_set_int (&p->maximumRecordSize, interp, argc, argv);
884 * do_initResult: Get init result
886 static int do_initResult (void *obj, Tcl_Interp *interp,
887 int argc, char **argv)
893 return ir_tcl_get_set_int (&p->initResult, interp, argc, argv);
898 * do_implementationName: Set/get Implementation Name.
900 static int do_implementationName (void *obj, Tcl_Interp *interp,
901 int argc, char **argv)
906 return ir_tcl_strdup (interp, &p->implementationName,
907 "Index Data/IrTcl on YAZ");
909 return ir_tcl_strdel (interp, &p->implementationName);
912 xfree (p->implementationName);
913 if (ir_tcl_strdup (interp, &p->implementationName, argv[2])
917 Tcl_AppendResult (interp, p->implementationName, (char*) NULL);
922 * do_implementationId: Get Implementation Id.
924 static int do_implementationId (void *obj, Tcl_Interp *interp,
925 int argc, char **argv)
930 return ir_tcl_strdup (interp, &p->implementationId, "YAZ (id=81)");
932 return ir_tcl_strdel (interp, &p->implementationId);
933 Tcl_AppendResult (interp, p->implementationId, (char*) NULL);
938 * do_implementationVersion: get Implementation Version.
940 static int do_implementationVersion (void *obj, Tcl_Interp *interp,
941 int argc, char **argv)
946 return ir_tcl_strdup (interp, &p->implementationVersion,
948 #ifdef IR_TCL_VERSION
949 " / Irtcl: " IR_TCL_VERSION
953 return ir_tcl_strdel (interp, &p->implementationVersion);
954 Tcl_AppendResult (interp, p->implementationVersion, (char*) NULL);
959 * do_targetImplementationName: Get Implementation Name of target.
961 static int do_targetImplementationName (void *obj, Tcl_Interp *interp,
962 int argc, char **argv)
968 p->targetImplementationName = NULL;
972 return ir_tcl_strdel (interp, &p->targetImplementationName);
973 Tcl_AppendResult (interp, p->targetImplementationName, (char*) NULL);
978 * do_targetImplementationId: Get Implementation Id of target
980 static int do_targetImplementationId (void *obj, Tcl_Interp *interp,
981 int argc, char **argv)
987 p->targetImplementationId = NULL;
991 return ir_tcl_strdel (interp, &p->targetImplementationId);
992 Tcl_AppendResult (interp, p->targetImplementationId, (char*) NULL);
997 * do_targetImplementationVersion: Get Implementation Version of target
999 static int do_targetImplementationVersion (void *obj, Tcl_Interp *interp,
1000 int argc, char **argv)
1006 p->targetImplementationVersion = NULL;
1009 else if (argc == -1)
1010 return ir_tcl_strdel (interp, &p->targetImplementationVersion);
1011 Tcl_AppendResult (interp, p->targetImplementationVersion, (char*) NULL);
1016 * do_idAuthentication: Set/get id Authentication
1018 static int do_idAuthentication (void *obj, Tcl_Interp *interp,
1019 int argc, char **argv)
1023 if (argc >= 3 || argc == -1)
1025 xfree (p->idAuthenticationOpen);
1026 xfree (p->idAuthenticationGroupId);
1027 xfree (p->idAuthenticationUserId);
1028 xfree (p->idAuthenticationPassword);
1030 if (argc >= 3 || argc <= 0)
1032 p->idAuthenticationOpen = NULL;
1033 p->idAuthenticationGroupId = NULL;
1034 p->idAuthenticationUserId = NULL;
1035 p->idAuthenticationPassword = NULL;
1044 ir_tcl_strdup (interp, &p->idAuthenticationOpen, argv[2])
1051 ir_tcl_strdup (interp, &p->idAuthenticationGroupId, argv[2])
1055 ir_tcl_strdup (interp, &p->idAuthenticationUserId, argv[3])
1059 ir_tcl_strdup (interp, &p->idAuthenticationPassword, argv[4])
1064 if (p->idAuthenticationOpen)
1065 Tcl_AppendElement (interp, p->idAuthenticationOpen);
1066 else if (p->idAuthenticationGroupId)
1068 Tcl_AppendElement (interp, p->idAuthenticationGroupId);
1069 Tcl_AppendElement (interp, p->idAuthenticationUserId);
1070 Tcl_AppendElement (interp, p->idAuthenticationPassword);
1076 * do_connect: connect method on IR object
1078 static int do_connect (void *obj, Tcl_Interp *interp,
1079 int argc, char **argv)
1089 logf (LOG_DEBUG, "connect %s %s", *argv, argv[2]);
1092 interp->result = "already connected";
1095 if (!strcmp (p->comstackType, "tcpip"))
1097 p->cs_link = cs_create (tcpip_type, CS_BLOCK, p->protocol_type);
1098 addr = tcpip_strtoaddr (argv[2]);
1101 interp->result = "tcpip_strtoaddr fail";
1104 logf (LOG_DEBUG, "tcp/ip connect %s", argv[2]);
1106 else if (!strcmp (p->comstackType, "mosi"))
1109 p->cs_link = cs_create (mosi_type, CS_BLOCK, p->protocol_type);
1110 addr = mosi_strtoaddr (argv[2]);
1113 interp->result = "mosi_strtoaddr fail";
1116 logf (LOG_DEBUG, "mosi connect %s", argv[2]);
1118 interp->result = "MOSI support not there";
1124 Tcl_AppendResult (interp, "Bad comstack type: ",
1125 p->comstackType, NULL);
1128 if (ir_tcl_strdup (interp, &p->hostname, argv[2]) == TCL_ERROR)
1130 p->eventType = "connect";
1131 if ((r=cs_connect (p->cs_link, addr)) < 0)
1133 interp->result = "connect fail";
1134 ir_tcl_disconnect (p);
1137 ir_select_add (cs_fileno (p->cs_link), p);
1140 logf (LOG_DEBUG, "connect pending fd=%d", cs_fileno(p->cs_link));
1141 ir_select_add_write (cs_fileno (p->cs_link), p);
1142 p->state = IR_TCL_R_Connecting;
1146 logf (LOG_DEBUG, "connect ok fd=%d", cs_fileno(p->cs_link));
1147 p->state = IR_TCL_R_Idle;
1149 ir_tcl_eval (p->interp, p->callback);
1153 Tcl_AppendResult (interp, p->hostname, NULL);
1158 * ir_tcl_disconnect: close connection
1160 void ir_tcl_disconnect (IrTcl_Obj *p)
1164 logf(LOG_DEBUG, "Closing connection to %s", p->hostname);
1165 xfree (p->hostname);
1167 assert (p->cs_link);
1168 ir_select_remove (cs_fileno (p->cs_link), p);
1170 odr_reset (p->odr_in);
1172 cs_close (p->cs_link);
1175 ODR_MASK_ZERO (&p->options);
1176 ODR_MASK_SET (&p->options, 0);
1177 ODR_MASK_SET (&p->options, 1);
1178 ODR_MASK_SET (&p->options, 4);
1179 ODR_MASK_SET (&p->options, 7);
1180 ODR_MASK_SET (&p->options, 14);
1182 ODR_MASK_ZERO (&p->protocolVersion);
1183 ODR_MASK_SET (&p->protocolVersion, 0);
1184 ODR_MASK_SET (&p->protocolVersion, 1);
1187 assert (!p->cs_link);
1191 * do_disconnect: disconnect method on IR object
1193 static int do_disconnect (void *obj, Tcl_Interp *interp,
1194 int argc, char **argv)
1200 p->state = IR_TCL_R_Idle;
1201 p->eventType = NULL;
1206 ir_tcl_disconnect (p);
1211 * do_comstack: Set/get comstack method on IR object
1213 static int do_comstack (void *o, Tcl_Interp *interp,
1214 int argc, char **argv)
1219 return ir_tcl_strdup (interp, &obj->comstackType, "tcpip");
1220 else if (argc == -1)
1221 return ir_tcl_strdel (interp, &obj->comstackType);
1224 xfree (obj->comstackType);
1225 if (ir_tcl_strdup (interp, &obj->comstackType, argv[2]) == TCL_ERROR)
1228 Tcl_AppendElement (interp, obj->comstackType);
1233 * do_logLevel: Set log level
1235 static int do_logLevel (void *o, Tcl_Interp *interp,
1236 int argc, char **argv)
1241 log_init (log_mask_str (argv[2]), "", NULL);
1243 log_init (log_mask_str (argv[2]), argv[3], NULL);
1245 log_init (log_mask_str (argv[2]), argv[3], argv[4]);
1251 * do_eventType: Return type of last event
1253 static int do_eventType (void *obj, Tcl_Interp *interp,
1254 int argc, char **argv)
1260 p->eventType = NULL;
1263 Tcl_AppendElement (interp, p->eventType ? p->eventType : "");
1269 * do_callback: add callback
1271 static int do_callback (void *obj, Tcl_Interp *interp,
1272 int argc, char **argv)
1281 else if (argc == -1)
1282 return ir_tcl_strdel (interp, &p->callback);
1285 xfree (p->callback);
1288 if (ir_tcl_strdup (interp, &p->callback, argv[2]) == TCL_ERROR)
1298 * do_failback: add error handle callback
1300 static int do_failback (void *obj, Tcl_Interp *interp,
1301 int argc, char **argv)
1310 else if (argc == -1)
1311 return ir_tcl_strdel (interp, &p->failback);
1314 xfree (p->failback);
1317 if (ir_tcl_strdup (interp, &p->failback, argv[2]) == TCL_ERROR)
1327 * do_initResponse: add init response handler
1329 static int do_initResponse (void *obj, Tcl_Interp *interp,
1330 int argc, char **argv)
1336 p->initResponse = NULL;
1339 else if (argc == -1)
1340 return ir_tcl_strdel (interp, &p->initResponse);
1343 xfree (p->initResponse);
1346 if (ir_tcl_strdup (interp, &p->initResponse, argv[2]) == TCL_ERROR)
1350 p->initResponse = NULL;
1355 * do_protocol: Set/get protocol method on IR object
1357 static int do_protocol (void *o, Tcl_Interp *interp, int argc, char **argv)
1363 p->protocol_type = PROTO_Z3950;
1368 if (!strcmp (argv[2], "Z39"))
1369 p->protocol_type = PROTO_Z3950;
1370 else if (!strcmp (argv[2], "SR"))
1371 p->protocol_type = PROTO_SR;
1374 Tcl_AppendResult (interp, "Bad protocol: ", argv[2], NULL);
1379 switch (p->protocol_type)
1382 Tcl_AppendElement (interp, "Z39");
1385 Tcl_AppendElement (interp, "SR");
1392 * do_triggerResourceControl:
1394 static int do_triggerResourceControl (void *obj, Tcl_Interp *interp,
1395 int argc, char **argv)
1399 Z_TriggerResourceControlRequest *req;
1400 bool_t is_false = 0;
1406 interp->result = "triggerResourceControl: not connected";
1409 apdu = zget_APDU (p->odr_out, Z_APDU_triggerResourceControlRequest);
1410 req = apdu->u.triggerResourceControlRequest;
1411 *req->requestedAction = Z_TriggerResourceCtrl_cancel;
1412 req->resultSetWanted = &is_false;
1414 return ir_tcl_send_APDU (interp, p, apdu, "triggerResourceControl",
1419 * do_databaseNames: specify database names
1421 static int do_databaseNames (void *obj, Tcl_Interp *interp,
1422 int argc, char **argv)
1425 IrTcl_SetCObj *p = obj;
1429 for (i=0; i<p->num_databaseNames; i++)
1430 xfree (p->databaseNames[i]);
1431 xfree (p->databaseNames);
1435 p->num_databaseNames = 0;
1436 p->databaseNames = NULL;
1441 for (i=0; i<p->num_databaseNames; i++)
1442 Tcl_AppendElement (interp, p->databaseNames[i]);
1445 if (p->databaseNames)
1447 for (i=0; i<p->num_databaseNames; i++)
1448 xfree (p->databaseNames[i]);
1449 xfree (p->databaseNames);
1451 p->num_databaseNames = argc - 2;
1453 ir_tcl_malloc (sizeof(*p->databaseNames) * (1+p->num_databaseNames));
1454 for (i=0; i<p->num_databaseNames; i++)
1456 if (ir_tcl_strdup (interp, &p->databaseNames[i], argv[2+i])
1460 p->databaseNames[i] = NULL;
1465 * do_replaceIndicator: Set/get replace Set indicator
1467 static int do_replaceIndicator (void *obj, Tcl_Interp *interp,
1468 int argc, char **argv)
1470 IrTcl_SetCObj *p = obj;
1474 p->replaceIndicator = 1;
1477 return ir_tcl_get_set_int (&p->replaceIndicator, interp, argc, argv);
1481 * do_queryType: Set/Get query method
1483 static int do_queryType (void *obj, Tcl_Interp *interp,
1484 int argc, char **argv)
1486 IrTcl_SetCObj *p = obj;
1489 return ir_tcl_strdup (interp, &p->queryType, "rpn");
1490 else if (argc == -1)
1491 return ir_tcl_strdel (interp, &p->queryType);
1494 xfree (p->queryType);
1495 if (ir_tcl_strdup (interp, &p->queryType, argv[2]) == TCL_ERROR)
1498 Tcl_AppendResult (interp, p->queryType, NULL);
1503 * do_userInformationField: Get User information field
1505 static int do_userInformationField (void *obj, Tcl_Interp *interp,
1506 int argc, char **argv)
1512 p->userInformationField = NULL;
1515 else if (argc == -1)
1516 return ir_tcl_strdel (interp, &p->userInformationField);
1517 Tcl_AppendResult (interp, p->userInformationField, NULL);
1522 * do_smallSetUpperBound: Set/get small set upper bound
1524 static int do_smallSetUpperBound (void *o, Tcl_Interp *interp,
1525 int argc, char **argv)
1527 IrTcl_SetCObj *p = o;
1531 p->smallSetUpperBound = 0;
1534 return ir_tcl_get_set_int (&p->smallSetUpperBound, interp, argc, argv);
1538 * do_largeSetLowerBound: Set/get large set lower bound
1540 static int do_largeSetLowerBound (void *o, Tcl_Interp *interp,
1541 int argc, char **argv)
1543 IrTcl_SetCObj *p = o;
1547 p->largeSetLowerBound = 2;
1550 return ir_tcl_get_set_int (&p->largeSetLowerBound, interp, argc, argv);
1554 * do_mediumSetPresentNumber: Set/get large set lower bound
1556 static int do_mediumSetPresentNumber (void *o, Tcl_Interp *interp,
1557 int argc, char **argv)
1559 IrTcl_SetCObj *p = o;
1563 p->mediumSetPresentNumber = 0;
1566 return ir_tcl_get_set_int (&p->mediumSetPresentNumber, interp, argc, argv);
1570 * do_referenceId: Set/Get referenceId
1572 static int do_referenceId (void *obj, Tcl_Interp *interp,
1573 int argc, char **argv)
1575 IrTcl_SetCObj *p = obj;
1579 p->referenceId = NULL;
1582 else if (argc == -1)
1583 return ir_tcl_strdel (interp, &p->referenceId);
1586 xfree (p->referenceId);
1587 if (ir_tcl_strdup (interp, &p->referenceId, argv[2]) == TCL_ERROR)
1590 Tcl_AppendResult (interp, p->referenceId, NULL);
1595 * do_preferredRecordSyntax: Set/get preferred record syntax
1597 static int do_preferredRecordSyntax (void *obj, Tcl_Interp *interp,
1598 int argc, char **argv)
1600 IrTcl_SetCObj *p = obj;
1604 p->preferredRecordSyntax = NULL;
1607 else if (argc == -1)
1609 xfree (p->preferredRecordSyntax);
1610 p->preferredRecordSyntax = NULL;
1615 xfree (p->preferredRecordSyntax);
1616 p->preferredRecordSyntax = NULL;
1617 if (argv[2][0] && (p->preferredRecordSyntax =
1618 ir_tcl_malloc (sizeof(*p->preferredRecordSyntax))))
1619 *p->preferredRecordSyntax = IrTcl_getRecordSyntaxVal (argv[2]);
1624 (interp,!p->preferredRecordSyntax ? "" :
1625 IrTcl_getRecordSyntaxStr(*p->preferredRecordSyntax));
1632 * do_elementSetNames: Set/Get element Set Names
1634 static int do_elementSetNames (void *obj, Tcl_Interp *interp,
1635 int argc, char **argv)
1637 IrTcl_SetCObj *p = obj;
1641 p->elementSetNames = NULL;
1644 else if (argc == -1)
1645 return ir_tcl_strdel (interp, &p->elementSetNames);
1648 xfree (p->elementSetNames);
1649 if (ir_tcl_strdup (interp, &p->elementSetNames, argv[2]) == TCL_ERROR)
1652 Tcl_AppendResult (interp, p->elementSetNames, NULL);
1657 * do_smallSetElementSetNames: Set/Get small Set Element Set Names
1659 static int do_smallSetElementSetNames (void *obj, Tcl_Interp *interp,
1660 int argc, char **argv)
1662 IrTcl_SetCObj *p = obj;
1666 p->smallSetElementSetNames = NULL;
1669 else if (argc == -1)
1670 return ir_tcl_strdel (interp, &p->smallSetElementSetNames);
1673 xfree (p->smallSetElementSetNames);
1674 if (ir_tcl_strdup (interp, &p->smallSetElementSetNames,
1675 argv[2]) == TCL_ERROR)
1678 Tcl_AppendResult (interp, p->smallSetElementSetNames, NULL);
1683 * do_mediumSetElementSetNames: Set/Get medium Set Element Set Names
1685 static int do_mediumSetElementSetNames (void *obj, Tcl_Interp *interp,
1686 int argc, char **argv)
1688 IrTcl_SetCObj *p = obj;
1692 p->mediumSetElementSetNames = NULL;
1695 else if (argc == -1)
1696 return ir_tcl_strdel (interp, &p->mediumSetElementSetNames);
1699 xfree (p->mediumSetElementSetNames);
1700 if (ir_tcl_strdup (interp, &p->mediumSetElementSetNames,
1701 argv[2]) == TCL_ERROR)
1704 Tcl_AppendResult (interp, p->mediumSetElementSetNames, NULL);
1708 static IrTcl_Method ir_method_tab[] = {
1709 { "comstack", do_comstack, NULL },
1710 { "protocol", do_protocol, NULL },
1711 { "failback", do_failback, NULL },
1712 { "failInfo", do_failInfo, NULL },
1713 { "apduInfo", do_apduInfo, NULL },
1714 { "logLevel", do_logLevel, NULL },
1716 { "eventType", do_eventType, NULL },
1717 { "connect", do_connect, NULL },
1718 { "protocolVersion", do_protocolVersion, NULL },
1719 { "preferredMessageSize", do_preferredMessageSize, NULL },
1720 { "maximumRecordSize", do_maximumRecordSize, NULL },
1721 { "implementationName", do_implementationName, NULL },
1722 { "implementationId", do_implementationId, NULL },
1723 { "implementationVersion", do_implementationVersion, NULL },
1724 { "targetImplementationName", do_targetImplementationName, NULL },
1725 { "targetImplementationId", do_targetImplementationId, NULL },
1726 { "targetImplementationVersion", do_targetImplementationVersion, NULL},
1727 { "userInformationField", do_userInformationField, NULL},
1728 { "idAuthentication", do_idAuthentication, NULL},
1729 { "options", do_options, NULL},
1730 { "init", do_init_request, NULL},
1731 { "initResult", do_initResult, NULL},
1732 { "disconnect", do_disconnect, NULL},
1733 { "callback", do_callback, NULL},
1734 { "initResponse", do_initResponse, NULL},
1735 { "triggerResourceControl", do_triggerResourceControl, NULL},
1736 { "initResponse", do_initResponse, NULL},
1740 static IrTcl_Method ir_set_c_method_tab[] = {
1741 { "databaseNames", do_databaseNames, NULL},
1742 { "replaceIndicator", do_replaceIndicator, NULL},
1743 { "queryType", do_queryType, NULL},
1744 { "preferredRecordSyntax", do_preferredRecordSyntax, NULL},
1745 { "smallSetUpperBound", do_smallSetUpperBound, NULL},
1746 { "largeSetLowerBound", do_largeSetLowerBound, NULL},
1747 { "mediumSetPresentNumber", do_mediumSetPresentNumber, NULL},
1748 { "referenceId", do_referenceId, NULL},
1749 { "elementSetNames", do_elementSetNames, NULL},
1750 { "smallSetElementSetNames", do_smallSetElementSetNames, NULL},
1751 { "mediumSetElementSetNames", do_mediumSetElementSetNames, NULL},
1756 * ir_obj_method: IR Object methods
1758 static int ir_obj_method (ClientData clientData, Tcl_Interp *interp,
1759 int argc, char **argv)
1761 IrTcl_Methods tab[3];
1762 IrTcl_Obj *p = clientData;
1768 tab[0].tab = ir_method_tab;
1770 tab[1].tab = ir_set_c_method_tab;
1771 tab[1].obj = &p->set_inher;
1774 ir_tcl_method (interp, argc, argv, tab, &r);
1779 * ir_obj_delete: IR Object disposal
1781 static void ir_obj_delete (ClientData clientData)
1783 IrTcl_Obj *obj = clientData;
1784 IrTcl_Methods tab[3];
1787 if (obj->ref_count > 0)
1789 assert (obj->ref_count == 0);
1791 logf (LOG_DEBUG, "ir object delete");
1792 tab[0].tab = ir_method_tab;
1794 tab[1].tab = ir_set_c_method_tab;
1795 tab[1].obj = &obj->set_inher;
1798 ir_tcl_method (NULL, -1, NULL, tab, NULL);
1801 odr_destroy (obj->odr_in);
1802 odr_destroy (obj->odr_out);
1803 odr_destroy (obj->odr_pr);
1808 * ir_obj_init: IR Object initialization
1810 int ir_obj_init (ClientData clientData, Tcl_Interp *interp,
1811 int argc, char **argv, ClientData *subData,
1812 ClientData parentData)
1814 IrTcl_Methods tab[3];
1822 interp->result = "wrong # args";
1825 obj = ir_tcl_malloc (sizeof(*obj));
1828 obj->bibset = ccl_qual_mk ();
1829 if ((inf = fopen ("default.bib", "r")))
1831 ccl_qual_file (obj->bibset, inf);
1836 logf (LOG_DEBUG, "ir object create %s", argv[1]);
1837 obj->odr_in = odr_createmem (ODR_DECODE);
1838 obj->odr_out = odr_createmem (ODR_ENCODE);
1839 obj->odr_pr = odr_createmem (ODR_PRINT);
1840 obj->state = IR_TCL_R_Idle;
1841 obj->interp = interp;
1845 obj->request_queue = NULL;
1847 tab[0].tab = ir_method_tab;
1849 tab[1].tab = ir_set_c_method_tab;
1850 tab[1].obj = &obj->set_inher;
1853 if (ir_tcl_method (interp, 0, NULL, tab, NULL) == TCL_ERROR)
1855 Tcl_AppendResult (interp, "Failed to initialize ", argv[1], NULL);
1864 * ir_obj_mk: IR Object creation
1866 static int ir_obj_mk (ClientData clientData, Tcl_Interp *interp,
1867 int argc, char **argv)
1870 int r = ir_obj_init (clientData, interp, argc, argv, &subData, 0);
1874 Tcl_CreateCommand (interp, argv[1], ir_obj_method,
1875 subData, ir_obj_delete);
1879 IrTcl_Class ir_obj_class = {
1887 /* ------------------------------------------------------- */
1889 * do_search: Do search request
1891 static int do_search (void *o, Tcl_Interp *interp, int argc, char **argv)
1893 Z_SearchRequest *req;
1897 IrTcl_SetObj *obj = o;
1907 logf (LOG_DEBUG, "search %s", *argv);
1908 interp->result = "wrong # args";
1911 logf (LOG_DEBUG, "search %s %s", *argv, argv[2]);
1912 if (!obj->set_inher.num_databaseNames)
1914 interp->result = "no databaseNames";
1919 interp->result = "search: not connected";
1922 apdu = zget_APDU (p->odr_out, Z_APDU_searchRequest);
1923 req = apdu->u.searchRequest;
1927 set_referenceId (p->odr_out, &req->referenceId,
1928 obj->set_inher.referenceId);
1930 req->smallSetUpperBound = &obj->set_inher.smallSetUpperBound;
1931 req->largeSetLowerBound = &obj->set_inher.largeSetLowerBound;
1932 req->mediumSetPresentNumber = &obj->set_inher.mediumSetPresentNumber;
1933 req->replaceIndicator = &obj->set_inher.replaceIndicator;
1934 req->resultSetName = obj->setName ? obj->setName : "Default";
1935 logf (LOG_DEBUG, "Search, resultSetName %s", req->resultSetName);
1936 req->num_databaseNames = obj->set_inher.num_databaseNames;
1937 req->databaseNames = obj->set_inher.databaseNames;
1938 for (r=0; r < obj->set_inher.num_databaseNames; r++)
1939 logf (LOG_DEBUG, " Database %s", obj->set_inher.databaseNames[r]);
1940 if (obj->set_inher.preferredRecordSyntax)
1942 struct oident ident;
1944 ident.proto = p->protocol_type;
1945 ident.oclass = CLASS_RECSYN;
1946 ident.value = *obj->set_inher.preferredRecordSyntax;
1947 logf (LOG_DEBUG, "Preferred record syntax is %d", ident.value);
1948 req->preferredRecordSyntax = odr_oiddup (p->odr_out,
1949 oid_getoidbyent (&ident));
1952 req->preferredRecordSyntax = 0;
1954 if (obj->set_inher.smallSetElementSetNames &&
1955 *obj->set_inher.smallSetElementSetNames)
1957 Z_ElementSetNames *esn = odr_malloc (p->odr_out, sizeof(*esn));
1959 esn->which = Z_ElementSetNames_generic;
1960 esn->u.generic = obj->set_inher.smallSetElementSetNames;
1961 req->smallSetElementSetNames = esn;
1964 req->smallSetElementSetNames = NULL;
1966 if (obj->set_inher.mediumSetElementSetNames &&
1967 *obj->set_inher.mediumSetElementSetNames)
1969 Z_ElementSetNames *esn = odr_malloc (p->odr_out, sizeof(*esn));
1971 esn->which = Z_ElementSetNames_generic;
1972 esn->u.generic = obj->set_inher.mediumSetElementSetNames;
1973 req->mediumSetElementSetNames = esn;
1976 req->mediumSetElementSetNames = NULL;
1978 req->query = &query;
1980 logf (LOG_DEBUG, "queryType %s", obj->set_inher.queryType);
1981 if (!strcmp (obj->set_inher.queryType, "rpn"))
1983 Z_RPNQuery *RPNquery;
1985 RPNquery = p_query_rpn (p->odr_out, p->protocol_type, argv[2]);
1988 Tcl_AppendResult (interp, "Syntax error in query", NULL);
1991 query.which = Z_Query_type_1;
1992 query.u.type_1 = RPNquery;
1995 else if (!strcmp (obj->set_inher.queryType, "cclrpn"))
1999 struct ccl_rpn_node *rpn;
2000 Z_RPNQuery *RPNquery;
2003 bib1.proto = p->protocol_type;
2004 bib1.oclass = CLASS_ATTSET;
2005 bib1.value = VAL_BIB1;
2007 rpn = ccl_find_str(p->bibset, argv[2], &error, &pos);
2010 Tcl_AppendResult (interp, "CCL error: ",
2011 ccl_err_msg(error), NULL);
2015 ccl_pr_tree (rpn, stderr);
2016 fprintf (stderr, "\n");
2018 assert((RPNquery = ccl_rpn_query(rpn)));
2019 RPNquery->attributeSetId = oid_getoidbyent (&bib1);
2020 query.which = Z_Query_type_1;
2021 query.u.type_1 = RPNquery;
2024 else if (!strcmp (obj->set_inher.queryType, "ccl"))
2026 query.which = Z_Query_type_2;
2027 query.u.type_2 = &ccl_query;
2028 ccl_query.buf = (unsigned char *) argv[2];
2029 ccl_query.len = strlen (argv[2]);
2033 interp->result = "unknown query method";
2036 return ir_tcl_send_APDU (interp, p, apdu, "search", *argv);
2040 * do_searchResponse: add search response handler
2042 static int do_searchResponse (void *o, Tcl_Interp *interp,
2043 int argc, char **argv)
2045 IrTcl_SetObj *obj = o;
2049 obj->searchResponse = NULL;
2052 else if (argc == -1)
2053 return ir_tcl_strdel (interp, &obj->searchResponse);
2056 xfree (obj->searchResponse);
2059 if (ir_tcl_strdup (interp, &obj->searchResponse, argv[2])
2064 obj->searchResponse = NULL;
2070 * do_presentResponse: add present response handler
2072 static int do_presentResponse (void *o, Tcl_Interp *interp,
2073 int argc, char **argv)
2075 IrTcl_SetObj *obj = o;
2079 obj->presentResponse = NULL;
2082 else if (argc == -1)
2083 return ir_tcl_strdel (interp, &obj->presentResponse);
2086 xfree (obj->presentResponse);
2089 if (ir_tcl_strdup (interp, &obj->presentResponse, argv[2])
2094 obj->presentResponse = NULL;
2100 * do_resultCount: Get number of hits
2102 static int do_resultCount (void *o, Tcl_Interp *interp,
2103 int argc, char **argv)
2105 IrTcl_SetObj *obj = o;
2109 obj->resultCount = 0;
2112 return ir_tcl_get_set_int (&obj->resultCount, interp, argc, argv);
2116 * do_searchStatus: Get search status (after search response)
2118 static int do_searchStatus (void *o, Tcl_Interp *interp,
2119 int argc, char **argv)
2121 IrTcl_SetObj *obj = o;
2125 return ir_tcl_get_set_int (&obj->searchStatus, interp, argc, argv);
2129 * do_presentStatus: Get search status (after search/present response)
2131 static int do_presentStatus (void *o, Tcl_Interp *interp,
2132 int argc, char **argv)
2134 IrTcl_SetObj *obj = o;
2138 return ir_tcl_get_set_int (&obj->presentStatus, interp, argc, argv);
2142 * do_nextResultSetPosition: Get next result set position
2143 * (after search/present response)
2145 static int do_nextResultSetPosition (void *o, Tcl_Interp *interp,
2146 int argc, char **argv)
2148 IrTcl_SetObj *obj = o;
2152 obj->nextResultSetPosition = 0;
2155 return ir_tcl_get_set_int (&obj->nextResultSetPosition, interp,
2160 * do_setName: Set result Set name
2162 static int do_setName (void *o, Tcl_Interp *interp,
2163 int argc, char **argv)
2165 IrTcl_SetObj *obj = o;
2168 return ir_tcl_strdup (interp, &obj->setName, "Default");
2169 else if (argc == -1)
2170 return ir_tcl_strdel (interp, &obj->setName);
2173 xfree (obj->setName);
2174 if (ir_tcl_strdup (interp, &obj->setName, argv[2])
2178 Tcl_AppendElement (interp, obj->setName);
2183 * do_numberOfRecordsReturned: Get number of records returned
2185 static int do_numberOfRecordsReturned (void *o, Tcl_Interp *interp,
2186 int argc, char **argv)
2188 IrTcl_SetObj *obj = o;
2192 obj->numberOfRecordsReturned = 0;
2195 return ir_tcl_get_set_int (&obj->numberOfRecordsReturned, interp,
2200 * do_type: Return type (if any) at position.
2202 static int do_type (void *o, Tcl_Interp *interp, int argc, char **argv)
2204 IrTcl_SetObj *obj = o;
2206 IrTcl_RecordList *rl;
2210 obj->record_list = NULL;
2213 else if (argc == -1)
2215 delete_IR_records (obj);
2220 sprintf (interp->result, "wrong # args");
2223 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2225 rl = find_IR_record (obj, offset);
2228 logf (LOG_DEBUG, "No record at position %d", offset);
2233 case Z_NamePlusRecord_databaseRecord:
2234 interp->result = "DB";
2236 case Z_NamePlusRecord_surrogateDiagnostic:
2237 interp->result = "SD";
2245 * do_recordType: Return record type (if any) at position.
2247 static int do_recordType (void *o, Tcl_Interp *interp, int argc, char **argv)
2249 IrTcl_SetObj *obj = o;
2251 IrTcl_RecordList *rl;
2257 else if (argc == -1)
2263 sprintf (interp->result, "wrong # args");
2266 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2268 rl = find_IR_record (obj, offset);
2271 if (rl->which != Z_NamePlusRecord_databaseRecord)
2273 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2276 Tcl_AppendElement (interp, (char*)
2277 IrTcl_getRecordSyntaxStr (rl->u.dbrec.type));
2282 * set record elements (for record extraction)
2284 static int do_recordElements (void *o, Tcl_Interp *interp,
2285 int argc, char **argv)
2287 IrTcl_SetObj *obj = o;
2291 obj->recordElements = NULL;
2294 else if (argc == -1)
2295 return ir_tcl_strdel (NULL, &obj->recordElements);
2298 sprintf (interp->result, "wrong # args");
2303 xfree (obj->recordElements);
2304 return ir_tcl_strdup (NULL, &obj->recordElements,
2305 (*argv[2] ? argv[2] : NULL));
2307 Tcl_AppendResult (interp, obj->recordElements, NULL);
2314 static int ir_diagResult (Tcl_Interp *interp, IrTcl_Diagnostic *list, int num)
2320 for (i = 0; i<num; i++)
2322 sprintf (buf, "%d", list[i].condition);
2323 Tcl_AppendElement (interp, buf);
2324 cp = diagbib1_str (list[i].condition);
2326 Tcl_AppendElement (interp, (char*) cp);
2328 Tcl_AppendElement (interp, "");
2329 if (list[i].addinfo)
2330 Tcl_AppendElement (interp, (char*) list[i].addinfo);
2332 Tcl_AppendElement (interp, "");
2338 * do_diag: Return diagnostic record info
2340 static int do_diag (void *o, Tcl_Interp *interp, int argc, char **argv)
2342 IrTcl_SetObj *obj = o;
2344 IrTcl_RecordList *rl;
2350 sprintf (interp->result, "wrong # args");
2353 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2355 rl = find_IR_record (obj, offset);
2358 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2361 if (rl->which != Z_NamePlusRecord_surrogateDiagnostic)
2363 Tcl_AppendResult (interp, "No Diagnostic record at #", argv[2], NULL);
2366 return ir_diagResult (interp, rl->u.surrogateDiagnostics.list,
2367 rl->u.surrogateDiagnostics.num);
2371 * do_getMarc: Get ISO2709 Record lines/fields
2373 static int do_getMarc (void *o, Tcl_Interp *interp, int argc, char **argv)
2375 IrTcl_SetObj *obj = o;
2377 IrTcl_RecordList *rl;
2383 sprintf (interp->result, "wrong # args");
2386 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2388 rl = find_IR_record (obj, offset);
2391 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2394 if (rl->which != Z_NamePlusRecord_databaseRecord)
2396 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2399 return ir_tcl_get_marc (interp, rl->u.dbrec.buf, argc, argv);
2403 * do_getSutrs: Get SUTRS Record
2405 static int do_getSutrs (void *o, Tcl_Interp *interp, int argc, char **argv)
2407 IrTcl_SetObj *obj = o;
2409 IrTcl_RecordList *rl;
2415 sprintf (interp->result, "wrong # args");
2418 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2420 rl = find_IR_record (obj, offset);
2423 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2426 if (rl->which != Z_NamePlusRecord_databaseRecord)
2428 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2431 if (rl->u.dbrec.type != VAL_SUTRS)
2433 Tcl_AppendElement (interp, rl->u.dbrec.buf);
2439 * do_getGrs: Get a GRS-1 Record
2441 static int do_getGrs (void *o, Tcl_Interp *interp, int argc, char **argv)
2443 IrTcl_SetObj *obj = o;
2445 IrTcl_RecordList *rl;
2451 sprintf (interp->result, "wrong # args");
2454 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2456 rl = find_IR_record (obj, offset);
2459 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2462 if (rl->which != Z_NamePlusRecord_databaseRecord)
2464 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2467 if (rl->u.dbrec.type != VAL_GRS1)
2469 return ir_tcl_get_grs (interp, rl->u.dbrec.u.grs1, argc, argv);
2474 * do_getExplain: Get an Explain Record
2476 static int do_getExplain (void *o, Tcl_Interp *interp, int argc, char **argv)
2478 IrTcl_SetObj *obj = o;
2479 IrTcl_Obj *p = obj->parent;
2481 Z_ext_typeent *etype;
2483 IrTcl_RecordList *rl;
2489 sprintf (interp->result, "wrong # args");
2492 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2494 rl = find_IR_record (obj, offset);
2497 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2500 if (rl->which != Z_NamePlusRecord_databaseRecord)
2502 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2505 if (rl->u.dbrec.type != VAL_EXPLAIN)
2508 if (!(etype = z_ext_getentbyref (VAL_EXPLAIN)))
2511 odr_setbuf (p->odr_in, rl->u.dbrec.buf, rl->u.dbrec.size, 0);
2512 if (!(*etype->fun)(p->odr_in, &rr, 0))
2515 if (etype->what != Z_External_explainRecord)
2518 return ir_tcl_get_explain (interp, rr, argc, argv);
2522 * do_responseStatus: Return response status (present or search)
2524 static int do_responseStatus (void *o, Tcl_Interp *interp,
2525 int argc, char **argv)
2527 IrTcl_SetObj *obj = o;
2531 obj->recordFlag = 0;
2532 obj->nonSurrogateDiagnosticNum = 0;
2533 obj->nonSurrogateDiagnosticList = NULL;
2536 else if (argc == -1)
2538 ir_deleteDiags (&obj->nonSurrogateDiagnosticList,
2539 &obj->nonSurrogateDiagnosticNum);
2542 if (!obj->recordFlag)
2544 Tcl_AppendElement (interp, "OK");
2549 case Z_Records_DBOSD:
2550 Tcl_AppendElement (interp, "DBOSD");
2553 Tcl_AppendElement (interp, "NSD");
2554 return ir_diagResult (interp, obj->nonSurrogateDiagnosticList,
2555 obj->nonSurrogateDiagnosticNum);
2561 * do_present: Perform Present Request
2564 static int do_present (void *o, Tcl_Interp *interp, int argc, char **argv)
2566 IrTcl_SetObj *obj = o;
2569 Z_PresentRequest *req;
2577 if (Tcl_GetInt (interp, argv[2], &start) == TCL_ERROR)
2584 if (Tcl_GetInt (interp, argv[3], &number) == TCL_ERROR)
2589 logf (LOG_DEBUG, "present %s %d %d", *argv, start, number);
2593 interp->result = "present: not connected";
2598 obj->number = number;
2600 apdu = zget_APDU (p->odr_out, Z_APDU_presentRequest);
2601 req = apdu->u.presentRequest;
2603 set_referenceId (p->odr_out, &req->referenceId,
2604 obj->set_inher.referenceId);
2606 req->resultSetId = obj->setName ? obj->setName : "Default";
2608 req->resultSetStartPoint = &start;
2609 req->numberOfRecordsRequested = &number;
2610 if (obj->set_inher.preferredRecordSyntax)
2612 struct oident ident;
2614 ident.proto = p->protocol_type;
2615 ident.oclass = CLASS_RECSYN;
2616 ident.value = *obj->set_inher.preferredRecordSyntax;
2617 logf (LOG_DEBUG, "Preferred record syntax is %d", ident.value);
2618 req->preferredRecordSyntax = odr_oiddup (p->odr_out,
2619 oid_getoidbyent (&ident));
2622 req->preferredRecordSyntax = 0;
2624 if (obj->set_inher.elementSetNames && *obj->set_inher.elementSetNames)
2626 Z_ElementSetNames *esn = odr_malloc (p->odr_out, sizeof(*esn));
2627 Z_RecordComposition *compo = odr_malloc (p->odr_out, sizeof(*compo));
2629 esn->which = Z_ElementSetNames_generic;
2630 esn->u.generic = obj->set_inher.elementSetNames;
2632 req->recordComposition = compo;
2633 compo->which = Z_RecordComp_simple;
2634 compo->u.simple = esn;
2637 req->recordComposition = NULL;
2638 return ir_tcl_send_APDU (interp, p, apdu, "present", *argv);
2641 #define IR_TCL_RECORD_ENCODING_ISO2709 1
2642 #define IR_TCL_RECORD_ENCODING_RAW 2
2648 } IrTcl_FileRecordHead;
2651 * do_loadFile: Load result set from file
2653 static int do_loadFile (void *o, Tcl_Interp *interp,
2654 int argc, char **argv)
2656 IrTcl_SetObj *setobj = o;
2668 interp->result = "wrong # args";
2672 start = atoi (argv[3]);
2674 number = atoi (argv[4]);
2677 inf = fopen (argv[2], "r");
2680 Tcl_AppendResult (interp, "Cannot open ", argv[2], NULL);
2683 while (offset < (start+number))
2685 IrTcl_FileRecordHead head;
2686 IrTcl_RecordList *rl;
2688 if (fread (&head, sizeof(head), 1, inf) < 1)
2690 rl = new_IR_record (setobj, offset,
2691 Z_NamePlusRecord_databaseRecord,
2692 (argc > 5) ? argv[5] : NULL);
2693 rl->u.dbrec.type = head.syntax;
2694 if (head.encoding == IR_TCL_RECORD_ENCODING_ISO2709)
2696 if (!(buf = ir_tcl_fread_marc (inf, &size)))
2698 rl->u.dbrec.buf = buf;
2699 rl->u.dbrec.size = size;
2700 if (size != head.size)
2702 Tcl_AppendResult (interp, "Bad ISO2709 encoding in file",
2708 else if (head.encoding == IR_TCL_RECORD_ENCODING_RAW)
2710 rl->u.dbrec.size = head.size;
2711 rl->u.dbrec.buf = ir_tcl_malloc (head.size + 1);
2712 if (fread (rl->u.dbrec.buf, rl->u.dbrec.size, 1, inf) < 1)
2714 Tcl_AppendResult (interp, "Bad RAW encoding in file",
2719 rl->u.dbrec.buf[rl->u.dbrec.size] = '\0';
2723 rl->u.dbrec.buf = NULL;
2724 rl->u.dbrec.size = 0;
2725 Tcl_AppendResult (interp, "Bad encoding in file", argv[2], NULL);
2731 setobj->numberOfRecordsReturned = offset - start;
2737 * do_saveFile: Save result set on file
2739 static int do_saveFile (void *o, Tcl_Interp *interp,
2740 int argc, char **argv)
2742 IrTcl_SetObj *setobj = o;
2747 IrTcl_RecordList *rl;
2753 interp->result = "wrong # args";
2757 start = atoi (argv[3]);
2759 number = atoi (argv[4]);
2762 outf = fopen (argv[2], "w");
2765 Tcl_AppendResult (interp, "Cannot open ", argv[2], NULL);
2768 while (offset < (start+number) && (rl = find_IR_record (setobj, offset)))
2770 if (rl->which == Z_NamePlusRecord_databaseRecord &&
2771 rl->u.dbrec.buf && rl->u.dbrec.size)
2773 IrTcl_FileRecordHead head;
2775 head.encoding = IR_TCL_RECORD_ENCODING_RAW;
2776 head.syntax = rl->u.dbrec.type;
2777 head.size = rl->u.dbrec.size;
2778 if (fwrite (&head, sizeof(head), 1, outf) < 1)
2780 Tcl_AppendResult (interp, "Cannot write ", argv[2], NULL);
2783 if (fwrite (rl->u.dbrec.buf, rl->u.dbrec.size, 1, outf) < 1)
2785 Tcl_AppendResult (interp, "Cannot write ", argv[2], NULL);
2793 Tcl_AppendResult (interp, "Cannot write ", argv[2], NULL);
2800 static IrTcl_Method ir_set_method_tab[] = {
2801 { "search", do_search, NULL},
2802 { "searchResponse", do_searchResponse, NULL},
2803 { "presentResponse", do_presentResponse, NULL},
2804 { "searchStatus", do_searchStatus, NULL},
2805 { "presentStatus", do_presentStatus, NULL},
2806 { "nextResultSetPosition", do_nextResultSetPosition, NULL},
2807 { "setName", do_setName, NULL},
2808 { "resultCount", do_resultCount, NULL},
2809 { "numberOfRecordsReturned", do_numberOfRecordsReturned, NULL},
2810 { "present", do_present, NULL},
2811 { "type", do_type, NULL},
2812 { "getMarc", do_getMarc, NULL},
2813 { "getSutrs", do_getSutrs, NULL},
2814 { "getGrs", do_getGrs, NULL},
2815 { "getExplain", do_getExplain, NULL},
2816 { "recordType", do_recordType, NULL},
2817 { "recordElements", do_recordElements, NULL},
2818 { "diag", do_diag, NULL},
2819 { "responseStatus", do_responseStatus, NULL},
2820 { "loadFile", do_loadFile, NULL},
2821 { "saveFile", do_saveFile, NULL},
2826 * ir_set_obj_method: IR Set Object methods
2828 static int ir_set_obj_method (ClientData clientData, Tcl_Interp *interp,
2829 int argc, char **argv)
2831 IrTcl_Methods tabs[3];
2832 IrTcl_SetObj *p = clientData;
2837 interp->result = "wrong # args";
2840 tabs[0].tab = ir_set_method_tab;
2842 tabs[1].tab = ir_set_c_method_tab;
2843 tabs[1].obj = &p->set_inher;
2846 ir_tcl_method (interp, argc, argv, tabs, &r);
2851 * ir_set_obj_delete: IR Set Object disposal
2853 static void ir_set_obj_delete (ClientData clientData)
2855 IrTcl_Methods tabs[3];
2856 IrTcl_SetObj *p = clientData;
2858 logf (LOG_DEBUG, "ir set delete");
2860 tabs[0].tab = ir_set_method_tab;
2862 tabs[1].tab = ir_set_c_method_tab;
2863 tabs[1].obj = &p->set_inher;
2866 ir_tcl_method (NULL, -1, NULL, tabs, NULL);
2872 * ir_set_obj_init: IR Set Object initialization
2874 static int ir_set_obj_init (ClientData clientData, Tcl_Interp *interp,
2875 int argc, char **argv, ClientData *subData,
2876 ClientData parentData)
2878 IrTcl_Methods tabs[3];
2881 if (argc < 2 || argc > 3)
2883 interp->result = "wrong # args";
2886 obj = ir_tcl_malloc (sizeof(*obj));
2887 logf (LOG_DEBUG, "ir set create %s", argv[1]);
2894 obj->parent = (IrTcl_Obj *) parentData;
2896 dst = &obj->set_inher;
2897 src = &obj->parent->set_inher;
2899 if ((dst->num_databaseNames = src->num_databaseNames))
2901 dst->databaseNames =
2902 ir_tcl_malloc (sizeof (*dst->databaseNames)
2903 * (1+dst->num_databaseNames));
2904 for (i = 0; i < dst->num_databaseNames; i++)
2905 if (ir_tcl_strdup (interp, &dst->databaseNames[i],
2906 src->databaseNames[i]) == TCL_ERROR)
2908 dst->databaseNames[i] = NULL;
2911 dst->databaseNames = NULL;
2912 if (ir_tcl_strdup (interp, &dst->queryType, src->queryType)
2916 if (ir_tcl_strdup (interp, &dst->referenceId, src->referenceId)
2920 if (ir_tcl_strdup (interp, &dst->elementSetNames, src->elementSetNames)
2924 if (ir_tcl_strdup (interp, &dst->smallSetElementSetNames,
2925 src->smallSetElementSetNames)
2929 if (ir_tcl_strdup (interp, &dst->mediumSetElementSetNames,
2930 src->mediumSetElementSetNames)
2934 if (src->preferredRecordSyntax &&
2935 (dst->preferredRecordSyntax
2936 = ir_tcl_malloc (sizeof(*dst->preferredRecordSyntax))))
2937 *dst->preferredRecordSyntax = *src->preferredRecordSyntax;
2939 dst->preferredRecordSyntax = NULL;
2940 dst->replaceIndicator = src->replaceIndicator;
2941 dst->smallSetUpperBound = src->smallSetUpperBound;
2942 dst->largeSetLowerBound = src->largeSetLowerBound;
2943 dst->mediumSetPresentNumber = src->mediumSetPresentNumber;
2948 tabs[0].tab = ir_set_method_tab;
2952 if (ir_tcl_method (interp, 0, NULL, tabs, NULL) == TCL_ERROR)
2960 * ir_set_obj_mk: IR Set Object creation
2962 static int ir_set_obj_mk (ClientData clientData, Tcl_Interp *interp,
2963 int argc, char **argv)
2966 ClientData parentData = 0;
2971 Tcl_CmdInfo parent_info;
2972 if (!Tcl_GetCommandInfo (interp, argv[2], &parent_info))
2974 interp->result = "No parent";
2977 parentData = parent_info.clientData;
2979 r = ir_set_obj_init (clientData, interp, argc, argv, &subData, parentData);
2982 Tcl_CreateCommand (interp, argv[1], ir_set_obj_method,
2983 subData, ir_set_obj_delete);
2987 IrTcl_Class ir_set_obj_class = {
2994 /* ------------------------------------------------------- */
2997 * do_scan: Perform scan
2999 static int do_scan (void *o, Tcl_Interp *interp, int argc, char **argv)
3003 IrTcl_ScanObj *obj = o;
3004 IrTcl_Obj *p = obj->parent;
3007 struct ccl_rpn_node *rpn;
3015 interp->result = "wrong # args";
3018 logf (LOG_DEBUG, "scan %s %s", *argv, argv[2]);
3019 if (!p->set_inher.num_databaseNames)
3021 interp->result = "no databaseNames";
3026 interp->result = "scan: not connected";
3030 apdu = zget_APDU (p->odr_out, Z_APDU_scanRequest);
3031 req = apdu->u.scanRequest;
3033 set_referenceId (p->odr_out, &req->referenceId, p->set_inher.referenceId);
3034 req->num_databaseNames = p->set_inher.num_databaseNames;
3035 req->databaseNames = p->set_inher.databaseNames;
3038 if (!(req->termListAndStartPoint =
3039 p_query_scan (p->odr_out, p->protocol_type,
3040 &req->attributeSet, argv[2])))
3042 Tcl_AppendResult (interp, "Syntax error in query", NULL);
3046 rpn = ccl_find_str(p->bibset, argv[2], &r, &pos);
3049 Tcl_AppendResult (interp, "CCL error: ", ccl_err_msg (r), NULL);
3052 bib1.proto = p->protocol_type;
3053 bib1.oclass = CLASS_ATTSET;
3054 bib1.value = VAL_BIB1;
3056 req->attributeSet = oid_getoidbyent (&bib1);
3057 if (!(req->termListAndStartPoint = ccl_scan_query (rpn)))
3060 req->stepSize = &obj->stepSize;
3061 req->numberOfTermsRequested = &obj->numberOfTermsRequested;
3062 req->preferredPositionInResponse = &obj->preferredPositionInResponse;
3063 logf (LOG_DEBUG, "stepSize=%d", *req->stepSize);
3064 logf (LOG_DEBUG, "numberOfTermsRequested=%d",
3065 *req->numberOfTermsRequested);
3066 logf (LOG_DEBUG, "preferredPositionInResponse=%d",
3067 *req->preferredPositionInResponse);
3069 return ir_tcl_send_APDU (interp, p, apdu, "scan", *argv);
3073 * do_scanResponse: add scan response handler
3075 static int do_scanResponse (void *o, Tcl_Interp *interp,
3076 int argc, char **argv)
3078 IrTcl_ScanObj *obj = o;
3082 obj->scanResponse = NULL;
3085 else if (argc == -1)
3086 return ir_tcl_strdel (interp, &obj->scanResponse);
3089 xfree (obj->scanResponse);
3092 if (ir_tcl_strdup (interp, &obj->scanResponse, argv[2])
3097 obj->scanResponse = NULL;
3103 * do_stepSize: Set/get replace Step Size
3105 static int do_stepSize (void *obj, Tcl_Interp *interp,
3106 int argc, char **argv)
3108 IrTcl_ScanObj *p = obj;
3114 return ir_tcl_get_set_int (&p->stepSize, interp, argc, argv);
3118 * do_numberOfTermsRequested: Set/get Number of Terms requested
3120 static int do_numberOfTermsRequested (void *obj, Tcl_Interp *interp,
3121 int argc, char **argv)
3123 IrTcl_ScanObj *p = obj;
3127 p->numberOfTermsRequested = 20;
3130 return ir_tcl_get_set_int (&p->numberOfTermsRequested, interp, argc, argv);
3135 * do_preferredPositionInResponse: Set/get preferred Position
3137 static int do_preferredPositionInResponse (void *obj, Tcl_Interp *interp,
3138 int argc, char **argv)
3140 IrTcl_ScanObj *p = obj;
3144 p->preferredPositionInResponse = 1;
3147 return ir_tcl_get_set_int (&p->preferredPositionInResponse, interp,
3152 * do_scanStatus: Get scan status
3154 static int do_scanStatus (void *obj, Tcl_Interp *interp,
3155 int argc, char **argv)
3157 IrTcl_ScanObj *p = obj;
3161 return ir_tcl_get_set_int (&p->scanStatus, interp, argc, argv);
3165 * do_numberOfEntriesReturned: Get number of Entries returned
3167 static int do_numberOfEntriesReturned (void *obj, Tcl_Interp *interp,
3168 int argc, char **argv)
3170 IrTcl_ScanObj *p = obj;
3174 return ir_tcl_get_set_int (&p->numberOfEntriesReturned, interp,
3179 * do_positionOfTerm: Get position of Term
3181 static int do_positionOfTerm (void *obj, Tcl_Interp *interp,
3182 int argc, char **argv)
3184 IrTcl_ScanObj *p = obj;
3188 return ir_tcl_get_set_int (&p->positionOfTerm, interp, argc, argv);
3192 * do_scanLine: get Scan Line (surrogate or normal) after response
3194 static int do_scanLine (void *obj, Tcl_Interp *interp, int argc, char **argv)
3196 IrTcl_ScanObj *p = obj;
3202 p->entries_flag = 0;
3204 p->nonSurrogateDiagnosticNum = 0;
3205 p->nonSurrogateDiagnosticList = 0;
3208 else if (argc == -1)
3210 p->entries_flag = 0;
3211 /* release entries */
3214 ir_deleteDiags (&p->nonSurrogateDiagnosticList,
3215 &p->nonSurrogateDiagnosticNum);
3220 interp->result = "wrong # args";
3223 if (Tcl_GetInt (interp, argv[2], &i) == TCL_ERROR)
3225 if (!p->entries_flag || p->which != Z_ListEntries_entries || !p->entries
3226 || i >= p->num_entries || i < 0)
3228 switch (p->entries[i].which)
3230 case Z_Entry_termInfo:
3231 Tcl_AppendElement (interp, "T");
3232 if (p->entries[i].u.term.buf)
3233 Tcl_AppendElement (interp, p->entries[i].u.term.buf);
3235 Tcl_AppendElement (interp, "");
3236 sprintf (numstr, "%d", p->entries[i].u.term.globalOccurrences);
3237 Tcl_AppendElement (interp, numstr);
3239 case Z_Entry_surrogateDiagnostic:
3240 Tcl_AppendElement (interp, "SD");
3241 return ir_diagResult (interp, p->entries[i].u.diag.list,
3242 p->entries[i].u.diag.num);
3248 static IrTcl_Method ir_scan_method_tab[] = {
3249 { "scan", do_scan, NULL},
3250 { "scanResponse", do_scanResponse, NULL},
3251 { "stepSize", do_stepSize, NULL},
3252 { "numberOfTermsRequested", do_numberOfTermsRequested, NULL},
3253 { "preferredPositionInResponse", do_preferredPositionInResponse, NULL},
3254 { "scanStatus", do_scanStatus, NULL},
3255 { "numberOfEntriesReturned", do_numberOfEntriesReturned, NULL},
3256 { "positionOfTerm", do_positionOfTerm, NULL},
3257 { "scanLine", do_scanLine, NULL},
3262 * ir_scan_obj_method: IR Scan Object methods
3264 static int ir_scan_obj_method (ClientData clientData, Tcl_Interp *interp,
3265 int argc, char **argv)
3267 IrTcl_Methods tabs[2];
3272 interp->result = "wrong # args";
3275 tabs[0].tab = ir_scan_method_tab;
3276 tabs[0].obj = clientData;
3279 ir_tcl_method (interp, argc, argv, tabs, &r);
3284 * ir_scan_obj_delete: IR Scan Object disposal
3286 static void ir_scan_obj_delete (ClientData clientData)
3288 IrTcl_Methods tabs[2];
3289 IrTcl_ScanObj *obj = clientData;
3291 tabs[0].tab = ir_scan_method_tab;
3295 ir_tcl_method (NULL, -1, NULL, tabs, NULL);
3300 * ir_scan_obj_mk: IR Scan Object creation
3302 static int ir_scan_obj_mk (ClientData clientData, Tcl_Interp *interp,
3303 int argc, char **argv)
3305 Tcl_CmdInfo parent_info;
3307 IrTcl_Methods tabs[2];
3311 interp->result = "wrong # args";
3314 logf (LOG_DEBUG, "ir scan create %s", argv[1]);
3315 if (!Tcl_GetCommandInfo (interp, argv[2], &parent_info))
3317 interp->result = "No parent";
3320 obj = ir_tcl_malloc (sizeof(*obj));
3321 obj->parent = (IrTcl_Obj *) parent_info.clientData;
3323 tabs[0].tab = ir_scan_method_tab;
3327 if (ir_tcl_method (interp, 0, NULL, tabs, NULL) == TCL_ERROR)
3329 Tcl_CreateCommand (interp, argv[1], ir_scan_obj_method,
3330 (ClientData) obj, ir_scan_obj_delete);
3334 /* ------------------------------------------------------- */
3336 static void ir_initResponse (void *obj, Z_InitResponse *initrs)
3340 p->initResult = *initrs->result ? 1 : 0;
3341 if (!*initrs->result)
3342 logf (LOG_DEBUG, "Connection rejected by target");
3344 logf (LOG_DEBUG, "Connection accepted by target");
3346 get_referenceId (&p->set_inher.referenceId, initrs->referenceId);
3348 xfree (p->targetImplementationId);
3349 ir_tcl_strdup (p->interp, &p->targetImplementationId,
3350 initrs->implementationId);
3351 xfree (p->targetImplementationName);
3352 ir_tcl_strdup (p->interp, &p->targetImplementationName,
3353 initrs->implementationName);
3354 xfree (p->targetImplementationVersion);
3355 ir_tcl_strdup (p->interp, &p->targetImplementationVersion,
3356 initrs->implementationVersion);
3358 p->maximumRecordSize = *initrs->maximumRecordSize;
3359 p->preferredMessageSize = *initrs->preferredMessageSize;
3361 memcpy (&p->options, initrs->options, sizeof(initrs->options));
3362 memcpy (&p->protocolVersion, initrs->protocolVersion,
3363 sizeof(initrs->protocolVersion));
3364 xfree (p->userInformationField);
3365 p->userInformationField = NULL;
3366 if (initrs->userInformationField)
3370 if (initrs->userInformationField->which == ODR_EXTERNAL_octet &&
3371 (p->userInformationField =
3372 ir_tcl_malloc ((len =
3373 initrs->userInformationField->
3374 u.octet_aligned->len) +1)))
3376 memcpy (p->userInformationField,
3377 initrs->userInformationField->u.octet_aligned->buf,
3379 (p->userInformationField)[len] = '\0';
3384 static void ir_deleteDiags (IrTcl_Diagnostic **dst_list, int *dst_num)
3387 for (i = 0; i<*dst_num; i++)
3388 xfree (dst_list[i]->addinfo);
3394 static void ir_handleDiags (IrTcl_Diagnostic **dst_list, int *dst_num,
3395 Z_DiagRec **list, int num)
3401 *dst_list = ir_tcl_malloc (sizeof(**dst_list) * num);
3402 for (i = 0; i<num; i++)
3405 switch (list[i]->which)
3407 case Z_DiagRec_defaultFormat:
3408 (*dst_list)[i].condition = *list[i]->u.defaultFormat->condition;
3409 addinfo = list[i]->u.defaultFormat->addinfo;
3411 ((*dst_list)[i].addinfo = ir_tcl_malloc (strlen(addinfo)+1)))
3412 strcpy ((*dst_list)[i].addinfo, addinfo);
3413 cp = diagbib1_str ((*dst_list)[i].condition);
3414 logf (LOG_DEBUG, "Diag %d %s %s", (*dst_list)[i].condition,
3415 cp ? cp : "", addinfo ? addinfo : "");
3418 (*dst_list)[i].addinfo = NULL;
3419 (*dst_list)[i].condition = 0;
3424 static void ir_handleDBRecord (IrTcl_Obj *p, IrTcl_RecordList *rl,
3427 struct oident *ident;
3428 Z_ext_typeent *etype;
3430 rl->u.dbrec.size = oe->u.octet_aligned->len;
3431 rl->u.dbrec.buf = NULL;
3433 if ((ident = oid_getentbyoid (oe->direct_reference)))
3434 rl->u.dbrec.type = ident->value;
3436 rl->u.dbrec.type = VAL_USMARC;
3438 if (ident && (oe->which == Z_External_single ||
3439 oe->which == Z_External_octet)
3440 && (etype = z_ext_getentbyref (ident->value)))
3444 odr_setbuf (p->odr_in, (char*) oe->u.octet_aligned->buf,
3445 oe->u.octet_aligned->len, 0);
3446 if (!(*etype->fun)(p->odr_in, &rr, 0))
3448 switch (etype->what)
3450 case Z_External_sutrs:
3451 logf (LOG_LOG, "Z_External_sutrs");
3453 if ((rl->u.dbrec.buf = ir_tcl_malloc (oe->u.sutrs->len+1)))
3455 memcpy (rl->u.dbrec.buf, oe->u.sutrs->buf,
3457 rl->u.dbrec.buf[oe->u.sutrs->len] = '\0';
3459 rl->u.dbrec.size = oe->u.sutrs->len;
3461 case Z_External_grs1:
3462 logf (LOG_LOG, "Z_External_grs1");
3464 ir_tcl_grs_mk (oe->u.grs1, &rl->u.dbrec.u.grs1);
3466 case Z_External_explainRecord:
3467 logf (LOG_LOG, "Z_External_explainRecord");
3468 if ((rl->u.dbrec.buf = ir_tcl_malloc (rl->u.dbrec.size)))
3470 memcpy (rl->u.dbrec.buf, oe->u.octet_aligned->buf,
3478 if (oe->which == Z_External_octet && rl->u.dbrec.size > 0)
3480 char *buf = (char*) oe->u.octet_aligned->buf;
3481 if ((rl->u.dbrec.buf = ir_tcl_malloc (rl->u.dbrec.size)))
3482 memcpy (rl->u.dbrec.buf, buf, rl->u.dbrec.size);
3484 else if (rl->u.dbrec.type == VAL_SUTRS &&
3485 oe->which == Z_External_sutrs)
3487 if ((rl->u.dbrec.buf = ir_tcl_malloc (oe->u.sutrs->len+1)))
3489 memcpy (rl->u.dbrec.buf, oe->u.sutrs->buf,
3491 rl->u.dbrec.buf[oe->u.sutrs->len] = '\0';
3493 rl->u.dbrec.size = oe->u.sutrs->len;
3495 else if (rl->u.dbrec.type == VAL_GRS1 &&
3496 oe->which == Z_External_grs1)
3498 ir_tcl_grs_mk (oe->u.grs1, &rl->u.dbrec.u.grs1);
3503 static void ir_handleZRecords (void *o, Z_Records *zrs, IrTcl_SetObj *setobj,
3504 const char *elements)
3509 IrTcl_RecordList *rl;
3511 setobj->which = zrs->which;
3512 setobj->recordFlag = 1;
3514 ir_deleteDiags (&setobj->nonSurrogateDiagnosticList,
3515 &setobj->nonSurrogateDiagnosticNum);
3516 if (zrs->which == Z_Records_DBOSD)
3518 setobj->numberOfRecordsReturned =
3519 zrs->u.databaseOrSurDiagnostics->num_records;
3520 logf (LOG_DEBUG, "Got %d records", setobj->numberOfRecordsReturned);
3521 for (offset = 0; offset < setobj->numberOfRecordsReturned; offset++)
3523 Z_NamePlusRecord *znpr = zrs->u.databaseOrSurDiagnostics->
3526 rl = new_IR_record (setobj, setobj->start + offset, znpr->which,
3528 if (rl->which == Z_NamePlusRecord_surrogateDiagnostic)
3529 ir_handleDiags (&rl->u.surrogateDiagnostics.list,
3530 &rl->u.surrogateDiagnostics.num,
3531 &znpr->u.surrogateDiagnostic,
3534 ir_handleDBRecord (p, rl,
3535 (Z_External*) (znpr->u.databaseRecord));
3538 else if (zrs->which == Z_Records_multipleNSD)
3540 logf (LOG_DEBUG, "multipleNonSurrogateDiagnostic %d",
3541 zrs->u.multipleNonSurDiagnostics->num_diagRecs);
3542 setobj->numberOfRecordsReturned = 0;
3543 ir_handleDiags (&setobj->nonSurrogateDiagnosticList,
3544 &setobj->nonSurrogateDiagnosticNum,
3545 zrs->u.multipleNonSurDiagnostics->diagRecs,
3546 zrs->u.multipleNonSurDiagnostics->num_diagRecs);
3550 logf (LOG_DEBUG, "NonSurrogateDiagnostic");
3551 setobj->numberOfRecordsReturned = 0;
3552 ir_handleDiags (&setobj->nonSurrogateDiagnosticList,
3553 &setobj->nonSurrogateDiagnosticNum,
3554 &zrs->u.nonSurrogateDiagnostic,
3559 static void ir_searchResponse (void *o, Z_SearchResponse *searchrs,
3560 IrTcl_SetObj *setobj)
3562 Z_Records *zrs = searchrs->records;
3564 logf (LOG_DEBUG, "Received search response");
3567 logf (LOG_DEBUG, "Search response, no object!");
3570 setobj->searchStatus = searchrs->searchStatus ? 1 : 0;
3571 get_referenceId (&setobj->set_inher.referenceId, searchrs->referenceId);
3572 setobj->resultCount = *searchrs->resultCount;
3573 if (searchrs->presentStatus)
3574 setobj->presentStatus = *searchrs->presentStatus;
3575 if (searchrs->nextResultSetPosition)
3576 setobj->nextResultSetPosition = *searchrs->nextResultSetPosition;
3578 logf (LOG_DEBUG, "status %d hits %d",
3579 setobj->searchStatus, setobj->resultCount);
3583 if (setobj->resultCount <= setobj->set_inher.smallSetUpperBound)
3584 es = setobj->set_inher.smallSetElementSetNames;
3586 es = setobj->set_inher.mediumSetElementSetNames;
3587 ir_handleZRecords (o, zrs, setobj, es);
3590 setobj->recordFlag = 0;
3594 static void ir_presentResponse (void *o, Z_PresentResponse *presrs,
3595 IrTcl_SetObj *setobj)
3597 Z_Records *zrs = presrs->records;
3599 logf (LOG_DEBUG, "Received present response");
3602 logf (LOG_DEBUG, "Present response, no object!");
3605 setobj->presentStatus = *presrs->presentStatus;
3606 get_referenceId (&setobj->set_inher.referenceId, presrs->referenceId);
3607 setobj->nextResultSetPosition = *presrs->nextResultSetPosition;
3609 ir_handleZRecords (o, zrs, setobj, setobj->set_inher.elementSetNames);
3612 setobj->recordFlag = 0;
3613 logf (LOG_DEBUG, "No records!");
3617 static void ir_scanResponse (void *o, Z_ScanResponse *scanrs,
3618 IrTcl_ScanObj *scanobj)
3622 logf (LOG_DEBUG, "Received scanResponse");
3624 get_referenceId (&p->set_inher.referenceId, scanrs->referenceId);
3625 scanobj->scanStatus = *scanrs->scanStatus;
3626 logf (LOG_DEBUG, "scanStatus=%d", scanobj->scanStatus);
3628 if (scanrs->stepSize)
3629 scanobj->stepSize = *scanrs->stepSize;
3630 logf (LOG_DEBUG, "stepSize=%d", scanobj->stepSize);
3632 scanobj->numberOfEntriesReturned = *scanrs->numberOfEntriesReturned;
3633 logf (LOG_DEBUG, "numberOfEntriesReturned=%d",
3634 scanobj->numberOfEntriesReturned);
3636 if (scanrs->positionOfTerm)
3637 scanobj->positionOfTerm = *scanrs->positionOfTerm;
3639 scanobj->positionOfTerm = -1;
3640 logf (LOG_DEBUG, "positionOfTerm=%d", scanobj->positionOfTerm);
3642 xfree (scanobj->entries);
3643 scanobj->entries = NULL;
3645 ir_deleteDiags (&scanobj->nonSurrogateDiagnosticList,
3646 &scanobj->nonSurrogateDiagnosticNum);
3647 if (scanrs->entries)
3652 scanobj->entries_flag = 1;
3653 scanobj->which = scanrs->entries->which;
3654 switch (scanobj->which)
3656 case Z_ListEntries_entries:
3657 scanobj->num_entries = scanrs->entries->u.entries->num_entries;
3658 scanobj->entries = ir_tcl_malloc (scanobj->num_entries *
3659 sizeof(*scanobj->entries));
3660 for (i=0; i<scanobj->num_entries; i++)
3662 ze = scanrs->entries->u.entries->entries[i];
3663 scanobj->entries[i].which = ze->which;
3666 case Z_Entry_termInfo:
3667 if (ze->u.termInfo->term->which == Z_Term_general)
3669 int l = ze->u.termInfo->term->u.general->len;
3670 scanobj->entries[i].u.term.buf = ir_tcl_malloc (1+l);
3671 memcpy (scanobj->entries[i].u.term.buf,
3672 ze->u.termInfo->term->u.general->buf,
3674 scanobj->entries[i].u.term.buf[l] = '\0';
3677 scanobj->entries[i].u.term.buf = NULL;
3678 if (ze->u.termInfo->globalOccurrences)
3679 scanobj->entries[i].u.term.globalOccurrences =
3680 *ze->u.termInfo->globalOccurrences;
3682 scanobj->entries[i].u.term.globalOccurrences = 0;
3684 case Z_Entry_surrogateDiagnostic:
3685 ir_handleDiags (&scanobj->entries[i].u.diag.list,
3686 &scanobj->entries[i].u.diag.num,
3687 &ze->u.surrogateDiagnostic,
3693 case Z_ListEntries_nonSurrogateDiagnostics:
3694 ir_handleDiags (&scanobj->nonSurrogateDiagnosticList,
3695 &scanobj->nonSurrogateDiagnosticNum,
3696 scanrs->entries->u.nonSurrogateDiagnostics->
3698 scanrs->entries->u.nonSurrogateDiagnostics->
3704 scanobj->entries_flag = 0;
3708 * ir_select_read: handle incoming packages
3710 static void ir_select_read (ClientData clientData)
3712 IrTcl_Obj *p = clientData;
3717 Tcl_CmdInfo cmd_info;
3718 const char *apdu_call;
3720 logf(LOG_DEBUG, "Read handler fd=%d", cs_fileno(p->cs_link));
3721 if (p->state == IR_TCL_R_Connecting)
3723 logf(LOG_DEBUG, "read: connect");
3724 r = cs_rcvconnect (p->cs_link);
3727 logf (LOG_WARN, "cs_rcvconnect returned 1");
3730 p->state = IR_TCL_R_Idle;
3732 ir_select_remove_write (cs_fileno (p->cs_link), p);
3735 logf (LOG_DEBUG, "cs_rcvconnect error");
3736 ir_tcl_disconnect (p);
3739 p->failInfo = IR_TCL_FAIL_CONNECT;
3740 ir_tcl_eval (p->interp, p->failback);
3746 ir_tcl_eval (p->interp, p->callback);
3747 if (p->ref_count == 2 && p->cs_link && p->request_queue
3748 && p->state == IR_TCL_R_Idle)
3749 ir_tcl_send_q (p, p->request_queue, "x");
3755 p->state = IR_TCL_R_Reading;
3757 /* read incoming APDU */
3758 if ((r=cs_get (p->cs_link, &p->buf_in, &p->len_in)) == 1)
3760 logf(LOG_DEBUG, "PDU Fraction read");
3763 /* signal one more use of ir object - callbacks must not
3764 release the ir memory (p pointer) */
3768 logf (LOG_DEBUG, "cs_get failed, code %d", r);
3769 ir_select_remove (cs_fileno (p->cs_link), p);
3770 ir_tcl_disconnect (p);
3773 p->failInfo = IR_TCL_FAIL_READ;
3774 ir_tcl_eval (p->interp, p->failback);
3776 /* release ir object now if callback deleted it */
3780 /* got complete APDU. Now decode */
3783 odr_setbuf (p->odr_in, p->buf_in, r, 0);
3784 logf (LOG_DEBUG, "cs_get ok, total size %d", r);
3785 if (!z_APDU (p->odr_in, &apdu, 0))
3787 logf (LOG_DEBUG, "cs_get failed: %s",
3788 odr_errmsg (odr_geterror (p->odr_in)));
3789 ir_tcl_disconnect (p);
3792 p->failInfo = IR_TCL_FAIL_IN_APDU;
3793 p->apduOffset = odr_offset (p->odr_in);
3794 ir_tcl_eval (p->interp, p->failback);
3796 /* release ir object now if failback deleted it */
3800 /* handle APDU and invoke callback */
3801 rq = p->request_queue;
3804 logf (LOG_FATAL, "Internal error. No queue entry");
3807 object_name = rq->object_name;
3808 logf (LOG_DEBUG, "Object %s", object_name);
3810 if (Tcl_GetCommandInfo (p->interp, object_name, &cmd_info))
3814 case Z_APDU_initResponse:
3815 p->eventType = "init";
3816 ir_initResponse (p, apdu->u.initResponse);
3817 apdu_call = p->initResponse;
3819 case Z_APDU_searchResponse:
3820 p->eventType = "search";
3821 ir_searchResponse (p, apdu->u.searchResponse,
3822 (IrTcl_SetObj *) cmd_info.clientData);
3823 apdu_call = ((IrTcl_SetObj *)
3824 cmd_info.clientData)->searchResponse;
3826 case Z_APDU_presentResponse:
3827 p->eventType = "present";
3828 ir_presentResponse (p, apdu->u.presentResponse,
3829 (IrTcl_SetObj *) cmd_info.clientData);
3830 apdu_call = ((IrTcl_SetObj *)
3831 cmd_info.clientData)->presentResponse;
3833 case Z_APDU_scanResponse:
3834 p->eventType = "scan";
3835 ir_scanResponse (p, apdu->u.scanResponse,
3836 (IrTcl_ScanObj *) cmd_info.clientData);
3837 apdu_call = ((IrTcl_ScanObj *)
3838 cmd_info.clientData)->scanResponse;
3841 logf (LOG_WARN, "Received unknown APDU type (%d)",
3843 ir_tcl_disconnect (p);
3846 p->failInfo = IR_TCL_FAIL_UNKNOWN_APDU;
3847 ir_tcl_eval (p->interp, p->failback);
3852 p->request_queue = rq->next;
3853 p->state = IR_TCL_R_Idle;
3856 ir_tcl_eval (p->interp, apdu_call);
3857 else if (rq->callback)
3858 ir_tcl_eval (p->interp, rq->callback);
3859 xfree (rq->buf_out);
3860 xfree (rq->callback);
3861 xfree (rq->object_name);
3863 odr_reset (p->odr_in);
3864 if (p->ref_count == 1)
3870 } while (p->cs_link && cs_more (p->cs_link));
3871 if (p->cs_link && p->request_queue && p->state == IR_TCL_R_Idle)
3872 ir_tcl_send_q (p, p->request_queue, "x");
3876 * ir_select_write: handle outgoing packages - not yet written.
3878 static int ir_select_write (ClientData clientData)
3880 IrTcl_Obj *p = clientData;
3884 logf (LOG_DEBUG, "Write handler fd=%d", cs_fileno(p->cs_link));
3885 if (p->state == IR_TCL_R_Connecting)
3887 logf(LOG_DEBUG, "write: connect");
3888 r = cs_rcvconnect (p->cs_link);
3891 logf (LOG_DEBUG, "cs_rcvconnect returned 1");
3894 p->state = IR_TCL_R_Idle;
3896 ir_select_remove_write (cs_fileno (p->cs_link), p);
3899 logf (LOG_DEBUG, "cs_rcvconnect error");
3900 ir_tcl_disconnect (p);
3903 p->failInfo = IR_TCL_FAIL_CONNECT;
3904 ir_tcl_eval (p->interp, p->failback);
3910 ir_tcl_eval (p->interp, p->callback);
3914 rq = p->request_queue;
3915 if (!rq || !rq->buf_out)
3918 if ((r=cs_put (p->cs_link, rq->buf_out, rq->len_out)) < 0)
3920 logf (LOG_DEBUG, "cs_put write fail");
3922 xfree (rq->buf_out);
3924 ir_tcl_disconnect (p);
3927 p->failInfo = IR_TCL_FAIL_WRITE;
3928 ir_tcl_eval (p->interp, p->failback);
3932 else if (r == 0) /* remove select bit */
3934 logf (LOG_DEBUG, "Write completed");
3935 p->state = IR_TCL_R_Waiting;
3936 ir_select_remove_write (cs_fileno (p->cs_link), p);
3937 xfree (rq->buf_out);
3943 static void ir_select_notify (ClientData clientData, int r, int w, int e)
3947 if (!ir_select_write (clientData) && r)
3948 ir_select_read (clientData);
3952 ir_select_read (clientData);
3956 /* ------------------------------------------------------- */
3959 * Irtcl_init: Registration of TCL commands.
3961 int Irtcl_Init (Tcl_Interp *interp)
3963 Tcl_CreateCommand (interp, "ir", ir_obj_mk, (ClientData) NULL,
3964 (Tcl_CmdDeleteProc *) NULL);
3965 Tcl_CreateCommand (interp, "ir-set", ir_set_obj_mk,
3966 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
3967 Tcl_CreateCommand (interp, "ir-scan", ir_scan_obj_mk,
3968 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);