2 * IR toolkit for tcl/tk
4 * See the file LICENSE for details.
5 * Sebastian Hammer, Adam Dickmeiss
8 * Revision 1.84 1996-03-07 12:42:49 adam
9 * Better logging when callback is invoked.
11 * Revision 1.83 1996/03/05 09:21:09 adam
12 * Bug fix: memory used by GRS records wasn't freed.
13 * Rewrote some of the error handling code - the connection is always
14 * closed before failback is called.
15 * If failback is defined the send APDU methods (init, search, ...) will
16 * return OK but invoke failback (as is the case if the write operation
18 * Bug fix: ref_count in assoc object could grow if fraction of PDU was
21 * Revision 1.82 1996/02/29 15:30:21 adam
22 * Export of IrTcl functionality to extensions.
24 * Revision 1.81 1996/02/26 18:38:32 adam
25 * Work on export of set methods.
27 * Revision 1.80 1996/02/23 17:31:39 adam
28 * More functions made available to the wais tcl extension.
30 * Revision 1.79 1996/02/23 13:41:38 adam
31 * Work on public access to simple ir class system.
33 * Revision 1.78 1996/02/21 10:16:08 adam
34 * Simplified select handling. Only one function ir_tcl_select_set has
35 * to be externally defined.
37 * Revision 1.77 1996/02/20 17:52:58 adam
38 * Uses the YAZ oid system to name record syntax object identifiers.
40 * Revision 1.76 1996/02/20 16:09:51 adam
41 * Bug fix: didn't set element set names stamp correctly on result
42 * set records when element set names were set to the empty string.
44 * Revision 1.75 1996/02/19 15:41:53 adam
45 * Better log messages.
46 * Minor improvement of connect method.
48 * Revision 1.74 1996/02/05 17:58:03 adam
49 * Ported ir-tcl to use the beta releases of tcl7.5/tk4.1.
51 * Revision 1.73 1996/01/29 11:35:19 adam
52 * Bug fix: cs_type member renamed to comstackType to avoid conflict with
53 * cs_type macro defined by YAZ.
55 * Revision 1.72 1996/01/19 17:45:34 quinn
56 * Added debugging output
58 * Revision 1.71 1996/01/19 16:22:38 adam
59 * New method: apduDump - returns information about last incoming APDU.
61 * Revision 1.70 1996/01/10 09:18:34 adam
62 * PDU specific callbacks implemented: initRespnse, searchResponse,
63 * presentResponse and scanResponse.
64 * Bug fix in the command line shell (tclmain.c) - discovered on OSF/1.
66 * Revision 1.69 1996/01/04 16:12:12 adam
67 * Setting PDUType renamed to eventType.
69 * Revision 1.68 1996/01/04 11:05:22 adam
70 * New setting: PDUType - returns type of last PDU returned from the target.
71 * Fixed a bug in configure/Makefile.
73 * Revision 1.67 1996/01/03 09:00:51 adam
74 * Updated to use new version of Yaz (names changed to avoid C++ conflict).
76 * Revision 1.66 1995/11/28 17:26:39 adam
77 * Removed Carriage return from ir-tcl.c!
78 * Removed misc. debug logs.
80 * Revision 1.65 1995/11/28 13:53:00 quinn
83 * Revision 1.64 1995/11/13 15:39:18 adam
84 * Bug fix: {small,medium}SetElementSetNames weren't set correctly.
85 * Bug fix: idAuthentication weren't set correctly.
87 * Revision 1.63 1995/11/13 09:55:39 adam
88 * Multiple records at a position in a result-set with differnt
91 * Revision 1.62 1995/10/18 17:20:33 adam
92 * Work on target setup in client.tcl.
94 * Revision 1.61 1995/10/18 16:42:42 adam
95 * New settings: smallSetElementSetNames and mediumSetElementSetNames.
97 * Revision 1.60 1995/10/18 15:43:31 adam
98 * In search: mediumSetElementSetNames and smallSetElementSetNames are
99 * set to elementSetNames.
101 * Revision 1.59 1995/10/17 12:18:58 adam
102 * Bug fix: when target connection closed, the connection was not
103 * properly reestablished.
105 * Revision 1.58 1995/10/16 17:00:55 adam
106 * New setting: elementSetNames.
107 * Various client improvements. Medium presentation format looks better.
109 * Revision 1.57 1995/09/21 13:11:51 adam
110 * Support of dynamic loading.
111 * Test script uses load command if necessary.
113 * Revision 1.56 1995/08/29 15:30:14 adam
114 * Work on GRS records.
116 * Revision 1.55 1995/08/28 09:43:25 adam
117 * Minor changes. configure only searches for yaz beta 3 and versions after
120 * Revision 1.54 1995/08/24 12:25:16 adam
121 * Modified to work with yaz 1.0b3.
123 * Revision 1.53 1995/08/04 12:49:26 adam
124 * Bug fix: reading uninitialized variable p.
126 * Revision 1.52 1995/08/04 11:32:38 adam
127 * More work on output queue. Memory related routines moved
130 * Revision 1.51 1995/08/03 13:22:54 adam
133 * Revision 1.50 1995/07/20 08:09:49 adam
134 * client.tcl: Targets removed from hotTargets list when targets
135 * are removed/modified.
136 * ir-tcl.c: More work on triggerResourceControl.
138 * Revision 1.49 1995/06/30 12:39:21 adam
139 * Bug fix: loadFile didn't set record type.
140 * The MARC routines are a little less strict in the interpretation.
141 * Script display.tcl replaces the old marc.tcl.
142 * New interactive script: shell.tcl.
144 * Revision 1.48 1995/06/27 19:03:50 adam
145 * Bug fix in do_present in ir-tcl.c: p->set_child member weren't set.
146 * nextResultSetPosition used instead of setOffset.
148 * Revision 1.47 1995/06/25 10:25:04 adam
149 * Working on triggerResourceControl. Description of compile/install
150 * procedure moved to ir-tcl.sgml.
152 * Revision 1.46 1995/06/22 13:15:06 adam
153 * Feature: SUTRS. Setting getSutrs implemented.
154 * Work on display formats.
155 * Preferred record syntax can be set by the user.
157 * Revision 1.45 1995/06/20 08:07:30 adam
158 * New setting: failInfo.
159 * Working on better cancel mechanism.
161 * Revision 1.44 1995/06/19 17:01:20 adam
164 * Revision 1.43 1995/06/19 13:06:08 adam
165 * New define: IR_TCL_VERSION.
167 * Revision 1.42 1995/06/19 08:08:52 adam
168 * client.tcl: hotTargets now contain both database and target name.
169 * ir-tcl.c: setting protocol edited. Errors in callbacks are logged
170 * by logf(LOG_WARN, ...) calls.
172 * Revision 1.41 1995/06/16 12:28:16 adam
173 * Implemented preferredRecordSyntax.
174 * Minor changes in diagnostic handling.
175 * Record list deleted when connection closes.
177 * Revision 1.40 1995/06/14 13:37:18 adam
178 * Setting recordType implemented.
179 * Setting implementationVersion implemented.
180 * Settings implementationId / implementationName edited.
182 * Revision 1.39 1995/06/08 10:26:32 adam
183 * Bug fix in ir_strdup.
185 * Revision 1.38 1995/06/01 16:36:47 adam
186 * About buttons. Minor bug fixes.
188 * Revision 1.37 1995/06/01 07:31:20 adam
189 * Rename of many typedefs -> IrTcl_...
191 * Revision 1.36 1995/05/31 13:09:59 adam
192 * Client searches/presents may be interrupted.
193 * New moving book-logo.
195 * Revision 1.35 1995/05/31 08:36:33 adam
196 * Bug fix in client.tcl: didn't save options on clientrc.tcl.
197 * New method: referenceId. More work on scan.
199 * Revision 1.34 1995/05/29 10:33:42 adam
200 * README and rename of startup script.
202 * Revision 1.33 1995/05/29 09:15:11 quinn
203 * Changed CS_SR to PROTO_SR, etc.
205 * Revision 1.32 1995/05/29 08:44:16 adam
206 * Work on delete of objects.
208 * Revision 1.31 1995/05/26 11:44:10 adam
209 * Bugs fixed. More work on MARC utilities and queries. Test
210 * client is up-to-date again.
212 * Revision 1.30 1995/05/26 08:54:11 adam
213 * New MARC utilities. Uses prefix query.
215 * Revision 1.29 1995/05/24 14:10:22 adam
216 * Work on idAuthentication, protocolVersion and options.
218 * Revision 1.28 1995/05/23 15:34:48 adam
219 * Many new settings, userInformationField, smallSetUpperBound, etc.
220 * A number of settings are inherited when ir-set is executed.
221 * This version is incompatible with the graphical test client (client.tcl).
223 * Revision 1.27 1995/05/11 15:34:47 adam
224 * Scan request changed a bit. This version works with RLG.
226 * Revision 1.26 1995/04/18 16:11:51 adam
227 * First version of graphical Scan. Some work on query-by-form.
229 * Revision 1.25 1995/04/17 09:37:17 adam
230 * Further development of scan.
232 * Revision 1.24 1995/04/11 14:16:42 adam
233 * Further work on scan. Response works. Entries aren't saved yet.
235 * Revision 1.23 1995/04/10 10:50:27 adam
236 * Result-set name defaults to suffix of ir-set name.
237 * Started working on scan. Not finished at this point.
239 * Revision 1.22 1995/03/31 10:43:03 adam
240 * More robust when getting bad MARC records.
242 * Revision 1.21 1995/03/31 08:56:37 adam
243 * New button "Search".
245 * Revision 1.20 1995/03/29 16:07:09 adam
246 * Bug fix: Didn't use setName in present request.
248 * Revision 1.19 1995/03/28 12:45:23 adam
249 * New ir method failback: called on disconnect/protocol error.
250 * New ir set/get method: protocol: SR / Z3950.
251 * Simple popup and disconnect when failback is invoked.
253 * Revision 1.18 1995/03/21 15:50:12 adam
256 * Revision 1.17 1995/03/21 13:41:03 adam
257 * Comstack cs_create not used too often. Non-blocking connect.
259 * Revision 1.16 1995/03/21 08:26:06 adam
260 * New method, setName, to specify the result set name (other than Default).
261 * New method, responseStatus, which returns diagnostic info, if any, after
262 * present response / search response.
264 * Revision 1.15 1995/03/20 15:24:07 adam
265 * Diagnostic records saved on searchResponse.
267 * Revision 1.14 1995/03/20 08:53:22 adam
268 * Event loop in tclmain.c rewritten. New method searchStatus.
270 * Revision 1.13 1995/03/17 18:26:17 adam
271 * Non-blocking i/o used now. Database names popup as cascade items.
273 * Revision 1.12 1995/03/17 15:45:00 adam
274 * Improved target/database setup.
276 * Revision 1.11 1995/03/16 17:54:03 adam
277 * Minor changes really.
279 * Revision 1.10 1995/03/15 16:14:50 adam
280 * Blocking arg in cs_create changed.
282 * Revision 1.9 1995/03/15 13:59:24 adam
285 * Revision 1.8 1995/03/15 08:25:16 adam
286 * New method presentStatus to check for error on present. Misc. cleanup
287 * of IrTcl_RecordList manipulations. Full MARC record presentation in
290 * Revision 1.7 1995/03/14 17:32:29 adam
291 * Presentation of full Marc record in popup window.
293 * Revision 1.6 1995/03/12 19:31:55 adam
294 * Pattern matching implemented when retrieving MARC records. More
295 * diagnostic functions.
297 * Revision 1.5 1995/03/10 18:00:15 adam
298 * Actual presentation in line-by-line format. RPN query support.
300 * Revision 1.4 1995/03/09 16:15:08 adam
301 * First presentRequest attempts. Hot-target list.
313 #define IRTCL_GENERIC_FILES 0
317 static void ir_deleteDiags (IrTcl_Diagnostic **dst_list, int *dst_num);
319 static void ir_select_notify (ClientData clientData, int r, int w, int e);
321 void ir_select_add (int fd, void *obj)
323 ir_tcl_select_set (ir_select_notify, fd, obj, 1, 0, 0);
326 void ir_select_add_write (int fd, void *obj)
328 ir_tcl_select_set (ir_select_notify, fd, obj, 1, 1, 0);
331 void ir_select_remove (int fd, void *obj)
333 ir_tcl_select_set (NULL, fd, obj, 0, 0, 0);
336 void ir_select_remove_write (int fd, void *obj)
338 ir_tcl_select_set (ir_select_notify, fd, obj, 1, 0, 0);
341 static void delete_IR_record (IrTcl_RecordList *rl)
345 case Z_NamePlusRecord_databaseRecord:
346 switch (rl->u.dbrec.type)
349 ir_tcl_grs_del (&rl->u.dbrec.u.grs1);
354 free (rl->u.dbrec.buf);
356 case Z_NamePlusRecord_surrogateDiagnostic:
357 ir_deleteDiags (&rl->u.surrogateDiagnostics.list,
358 &rl->u.surrogateDiagnostics.num);
364 static IrTcl_RecordList *new_IR_record (IrTcl_SetObj *setobj,
366 const char *elements)
368 IrTcl_RecordList *rl;
370 if (elements && !*elements)
372 for (rl = setobj->record_list; rl; rl = rl->next)
374 if (no == rl->no && (!rl->elements || !elements ||
375 !strcmp(elements, rl->elements)))
377 delete_IR_record (rl);
383 rl = ir_tcl_malloc (sizeof(*rl));
384 rl->next = setobj->record_list;
386 setobj->record_list = rl;
389 ir_tcl_strdup (NULL, &rl->elements, elements);
396 int ir_tcl_eval (Tcl_Interp *interp, const char *command)
398 char *tmp = ir_tcl_malloc (strlen(command)+1);
401 logf (LOG_DEBUG, "Invoking %.23s ...", command);
402 strcpy (tmp, command);
403 r = Tcl_Eval (interp, tmp);
405 logf (LOG_WARN, "Tcl error in line %d: %s", interp->errorLine,
407 Tcl_FreeResult (interp);
413 * IrTcl_getRecordSyntaxStr: Return record syntax name of object id
415 static char *IrTcl_getRecordSyntaxStr (enum oid_value value)
418 struct oident ent, *entp;
420 ent.proto = PROTO_Z3950;
421 ent.oclass = CLASS_RECSYN;
424 o = oid_getoidbyent (&ent);
425 entp = oid_getentbyoid (o);
433 * IrTcl_getRecordSyntaxVal: Return record syntax value of string
435 static enum oid_value IrTcl_getRecordSyntaxVal (const char *name)
437 return oid_getvalbyname (name);
440 static IrTcl_RecordList *find_IR_record (IrTcl_SetObj *setobj, int no)
442 IrTcl_RecordList *rl;
444 for (rl = setobj->record_list; rl; rl = rl->next)
446 (!setobj->recordElements || !rl->elements ||
447 !strcmp (setobj->recordElements, rl->elements)))
452 static void delete_IR_records (IrTcl_SetObj *setobj)
454 IrTcl_RecordList *rl, *rl1;
456 for (rl = setobj->record_list; rl; rl = rl1)
458 delete_IR_record (rl);
462 setobj->record_list = NULL;
466 * ir_tcl_get_set_int: Set/get integer value
468 int ir_tcl_get_set_int (int *val, Tcl_Interp *interp, int argc, char **argv)
474 if (Tcl_GetInt (interp, argv[2], val)==TCL_ERROR)
477 sprintf (buf, "%d", *val);
478 Tcl_AppendResult (interp, buf, NULL);
483 * ir_tcl_method: Search for method in table and invoke method handler
485 int ir_tcl_method (Tcl_Interp *interp, int argc, char **argv,
486 IrTcl_Methods *tab, int *ret)
488 IrTcl_Methods *tab_i = tab;
491 for (tab_i = tab; tab_i->tab; tab_i++)
492 for (t = tab_i->tab; t->name; t++)
495 if ((*t->method)(tab_i->obj, interp, argc, argv) == TCL_ERROR)
499 if (!strcmp (t->name, argv[1]))
501 *ret = (*t->method)(tab_i->obj, interp, argc, argv);
508 Tcl_AppendResult (interp, "Bad method: ", argv[1],
509 ". Possible methods:", NULL);
510 for (tab_i = tab; tab_i->tab; tab_i++)
511 for (t = tab_i->tab; t->name; t++)
512 Tcl_AppendResult (interp, " ", t->name, NULL);
519 * ir_tcl_named_bits: get/set named bits
521 int ir_tcl_named_bits (struct ir_named_entry *tab, Odr_bitmask *ob,
522 Tcl_Interp *interp, int argc, char **argv)
524 struct ir_named_entry *ti;
529 for (no = 0; no < argc; no++)
531 for (ti = tab; ti->name; ti++)
532 if (!strcmp (argv[no], ti->name))
534 ODR_MASK_SET (ob, ti->pos);
539 Tcl_AppendResult (interp, "Bad bit mask: ", argv[no], NULL);
545 for (ti = tab; ti->name; ti++)
546 if (ODR_MASK_GET (ob, ti->pos))
547 Tcl_AppendElement (interp, ti->name);
551 static void set_referenceId (ODR o, Z_ReferenceId **dst, const char *src)
557 *dst = odr_malloc (o, sizeof(**dst));
558 (*dst)->size = (*dst)->len = strlen(src);
559 (*dst)->buf = odr_malloc (o, (*dst)->len);
560 memcpy ((*dst)->buf, src, (*dst)->len);
564 static void get_referenceId (char **dst, Z_ReferenceId *src)
572 *dst = ir_tcl_malloc (src->len+1);
573 memcpy (*dst, src->buf, src->len);
574 (*dst)[src->len] = '\0';
577 /* ------------------------------------------------------- */
580 * do_init_request: init method on IR object
582 static int do_init_request (void *obj, Tcl_Interp *interp,
583 int argc, char **argv)
593 interp->result = "init: not connected";
596 apdu = zget_APDU (p->odr_out, Z_APDU_initRequest);
597 req = apdu->u.initRequest;
599 set_referenceId (p->odr_out, &req->referenceId, p->set_inher.referenceId);
600 req->options = &p->options;
601 req->protocolVersion = &p->protocolVersion;
602 req->preferredMessageSize = &p->preferredMessageSize;
603 req->maximumRecordSize = &p->maximumRecordSize;
605 if (p->idAuthenticationGroupId)
607 Z_IdPass *pass = odr_malloc (p->odr_out, sizeof(*pass));
608 Z_IdAuthentication *auth = odr_malloc (p->odr_out, sizeof(*auth));
610 logf (LOG_DEBUG, "using pass authentication");
612 auth->which = Z_IdAuthentication_idPass;
613 auth->u.idPass = pass;
614 if (p->idAuthenticationGroupId && *p->idAuthenticationGroupId)
615 pass->groupId = p->idAuthenticationGroupId;
617 pass->groupId = NULL;
618 if (p->idAuthenticationUserId && *p->idAuthenticationUserId)
619 pass->userId = p->idAuthenticationUserId;
622 if (p->idAuthenticationPassword && *p->idAuthenticationPassword)
623 pass->password = p->idAuthenticationPassword;
625 pass->password = NULL;
626 req->idAuthentication = auth;
628 else if (!p->idAuthenticationOpen || !*p->idAuthenticationOpen)
629 req->idAuthentication = NULL;
632 Z_IdAuthentication *auth = odr_malloc (p->odr_out, sizeof(*auth));
634 logf (LOG_DEBUG, "using open authentication");
635 auth->which = Z_IdAuthentication_open;
636 auth->u.open = p->idAuthenticationOpen;
637 req->idAuthentication = auth;
639 req->implementationId = p->implementationId;
640 req->implementationName = p->implementationName;
641 req->implementationVersion = p->implementationVersion;
642 req->userInformationField = 0;
644 return ir_tcl_send_APDU (interp, p, apdu, "init", *argv);
648 * do_protocolVersion: Set protocol Version
650 static int do_protocolVersion (void *obj, Tcl_Interp *interp,
651 int argc, char **argv)
659 ODR_MASK_ZERO (&p->protocolVersion);
660 ODR_MASK_SET (&p->protocolVersion, 0);
661 ODR_MASK_SET (&p->protocolVersion, 1);
666 if (Tcl_GetInt (interp, argv[2], &version)==TCL_ERROR)
668 ODR_MASK_ZERO (&p->protocolVersion);
669 for (i = 0; i<version; i++)
670 ODR_MASK_SET (&p->protocolVersion, i);
672 for (i = 4; --i >= 0; )
673 if (ODR_MASK_GET (&p->protocolVersion, i))
675 sprintf (buf, "%d", i+1);
676 interp->result = buf;
681 * do_options: Set options
683 static int do_options (void *obj, Tcl_Interp *interp,
684 int argc, char **argv)
686 static struct ir_named_entry options_tab[] = {
690 { "resourceReport", 3 },
691 { "triggerResourceCtrl", 4},
692 { "resourceCtrl", 5},
696 { "extendedServices", 10},
697 { "level-1Segmentation", 11},
698 { "level-2Segmentation", 12},
699 { "concurrentOperations", 13},
700 { "namedResultSets", 14},
707 ODR_MASK_ZERO (&p->options);
708 ODR_MASK_SET (&p->options, 0);
709 ODR_MASK_SET (&p->options, 1);
710 ODR_MASK_SET (&p->options, 4);
711 ODR_MASK_SET (&p->options, 7);
712 ODR_MASK_SET (&p->options, 14);
715 return ir_tcl_named_bits (options_tab, &p->options, interp, argc-2, argv+2);
719 * do_apduInfo: Get APDU information
721 static int do_apduInfo (void *obj, Tcl_Interp *interp, int argc, char **argv)
729 sprintf (buf, "%d", p->apduLen);
730 Tcl_AppendElement (interp, buf);
731 sprintf (buf, "%d", p->apduOffset);
732 Tcl_AppendElement (interp, buf);
735 Tcl_AppendElement (interp, "");
738 apduf = fopen ("apdu.tmp", "w");
741 Tcl_AppendElement (interp, "");
744 odr_dumpBER (apduf, p->buf_in, p->apduLen);
746 if (!(apduf = fopen ("apdu.tmp", "r")))
747 Tcl_AppendElement (interp, "");
752 Tcl_AppendResult (interp, " {", NULL);
753 while ((c = getc (apduf)) != EOF)
757 Tcl_AppendResult (interp, buf, NULL);
760 Tcl_AppendResult (interp, "}", NULL);
767 * do_failInfo: Get fail information
769 static int do_failInfo (void *obj, Tcl_Interp *interp, int argc, char **argv)
779 sprintf (buf, "%d", p->failInfo);
785 case IR_TCL_FAIL_CONNECT:
786 cp = "connect failed";
788 case IR_TCL_FAIL_READ:
789 cp = "connection closed";
791 case IR_TCL_FAIL_WRITE:
792 cp = "connection closed";
794 case IR_TCL_FAIL_IN_APDU:
795 cp = "failed to decode incoming APDU";
797 case IR_TCL_FAIL_UNKNOWN_APDU:
803 Tcl_AppendElement (interp, buf);
804 Tcl_AppendElement (interp, cp);
809 * do_preferredMessageSize: Set/get preferred message size
811 static int do_preferredMessageSize (void *obj, Tcl_Interp *interp,
812 int argc, char **argv)
818 p->preferredMessageSize = 30000;
821 return ir_tcl_get_set_int (&p->preferredMessageSize, interp, argc, argv);
825 * do_maximumRecordSize: Set/get maximum record size
827 static int do_maximumRecordSize (void *obj, Tcl_Interp *interp,
828 int argc, char **argv)
834 p->maximumRecordSize = 30000;
837 return ir_tcl_get_set_int (&p->maximumRecordSize, interp, argc, argv);
841 * do_initResult: Get init result
843 static int do_initResult (void *obj, Tcl_Interp *interp,
844 int argc, char **argv)
850 return ir_tcl_get_set_int (&p->initResult, interp, argc, argv);
855 * do_implementationName: Set/get Implementation Name.
857 static int do_implementationName (void *obj, Tcl_Interp *interp,
858 int argc, char **argv)
863 return ir_tcl_strdup (interp, &p->implementationName,
864 "Index Data/IrTcl on YAZ");
866 return ir_tcl_strdel (interp, &p->implementationName);
869 free (p->implementationName);
870 if (ir_tcl_strdup (interp, &p->implementationName, argv[2])
874 Tcl_AppendResult (interp, p->implementationName, (char*) NULL);
879 * do_implementationId: Get Implementation Id.
881 static int do_implementationId (void *obj, Tcl_Interp *interp,
882 int argc, char **argv)
887 return ir_tcl_strdup (interp, &p->implementationId, "YAZ (id=81)");
889 return ir_tcl_strdel (interp, &p->implementationId);
890 Tcl_AppendResult (interp, p->implementationId, (char*) NULL);
895 * do_implementationVersion: get Implementation Version.
897 static int do_implementationVersion (void *obj, Tcl_Interp *interp,
898 int argc, char **argv)
903 return ir_tcl_strdup (interp, &p->implementationVersion,
904 "YAZ: " YAZ_VERSION " / IrTcl: " IR_TCL_VERSION);
906 return ir_tcl_strdel (interp, &p->implementationVersion);
907 Tcl_AppendResult (interp, p->implementationVersion, (char*) NULL);
912 * do_targetImplementationName: Get Implementation Name of target.
914 static int do_targetImplementationName (void *obj, Tcl_Interp *interp,
915 int argc, char **argv)
921 p->targetImplementationName = NULL;
925 return ir_tcl_strdel (interp, &p->targetImplementationName);
926 Tcl_AppendResult (interp, p->targetImplementationName, (char*) NULL);
931 * do_targetImplementationId: Get Implementation Id of target
933 static int do_targetImplementationId (void *obj, Tcl_Interp *interp,
934 int argc, char **argv)
940 p->targetImplementationId = NULL;
944 return ir_tcl_strdel (interp, &p->targetImplementationId);
945 Tcl_AppendResult (interp, p->targetImplementationId, (char*) NULL);
950 * do_targetImplementationVersion: Get Implementation Version of target
952 static int do_targetImplementationVersion (void *obj, Tcl_Interp *interp,
953 int argc, char **argv)
959 p->targetImplementationVersion = NULL;
963 return ir_tcl_strdel (interp, &p->targetImplementationVersion);
964 Tcl_AppendResult (interp, p->targetImplementationVersion, (char*) NULL);
969 * do_idAuthentication: Set/get id Authentication
971 static int do_idAuthentication (void *obj, Tcl_Interp *interp,
972 int argc, char **argv)
976 if (argc >= 3 || argc == -1)
978 free (p->idAuthenticationOpen);
979 free (p->idAuthenticationGroupId);
980 free (p->idAuthenticationUserId);
981 free (p->idAuthenticationPassword);
983 if (argc >= 3 || argc <= 0)
985 p->idAuthenticationOpen = NULL;
986 p->idAuthenticationGroupId = NULL;
987 p->idAuthenticationUserId = NULL;
988 p->idAuthenticationPassword = NULL;
997 ir_tcl_strdup (interp, &p->idAuthenticationOpen, argv[2])
1004 ir_tcl_strdup (interp, &p->idAuthenticationGroupId, argv[2])
1008 ir_tcl_strdup (interp, &p->idAuthenticationUserId, argv[3])
1012 ir_tcl_strdup (interp, &p->idAuthenticationPassword, argv[4])
1017 if (p->idAuthenticationOpen)
1018 Tcl_AppendElement (interp, p->idAuthenticationOpen);
1019 else if (p->idAuthenticationGroupId)
1021 Tcl_AppendElement (interp, p->idAuthenticationGroupId);
1022 Tcl_AppendElement (interp, p->idAuthenticationUserId);
1023 Tcl_AppendElement (interp, p->idAuthenticationPassword);
1029 * do_connect: connect method on IR object
1031 static int do_connect (void *obj, Tcl_Interp *interp,
1032 int argc, char **argv)
1044 interp->result = "already connected";
1047 if (!strcmp (p->comstackType, "tcpip"))
1049 p->cs_link = cs_create (tcpip_type, CS_BLOCK, p->protocol_type);
1050 addr = tcpip_strtoaddr (argv[2]);
1053 interp->result = "tcpip_strtoaddr fail";
1056 logf (LOG_DEBUG, "tcp/ip connect %s", argv[2]);
1058 else if (!strcmp (p->comstackType, "mosi"))
1061 p->cs_link = cs_create (mosi_type, CS_BLOCK, p->protocol_type);
1062 addr = mosi_strtoaddr (argv[2]);
1065 interp->result = "mosi_strtoaddr fail";
1068 logf (LOG_DEBUG, "mosi connect %s", argv[2]);
1070 interp->result = "MOSI support not there";
1076 Tcl_AppendResult (interp, "Bad comstack type: ",
1077 p->comstackType, NULL);
1080 if (ir_tcl_strdup (interp, &p->hostname, argv[2]) == TCL_ERROR)
1082 #if IRTCL_GENERIC_FILES
1084 p->csFile = Tcl_GetFile (cs_fileno(p->cs_link), TCL_WIN_SOCKET);
1086 p->csFile = Tcl_GetFile (cs_fileno(p->cs_link), TCL_UNIX_FD);
1089 if ((r=cs_connect (p->cs_link, addr)) < 0)
1091 interp->result = "connect fail";
1092 ir_tcl_disconnect (p);
1095 logf(LOG_DEBUG, "cs_connect() returned %d fd=%d", r,
1096 cs_fileno(p->cs_link));
1097 p->eventType = "connect";
1098 #if IRTCL_GENERIC_FILES
1099 ir_select_add (p->csFile, p);
1101 ir_select_add (cs_fileno (p->cs_link), p);
1105 #if IRTCL_GENERIC_FILES
1106 ir_select_add_write (p->csFile, p);
1108 ir_select_add_write (cs_fileno (p->cs_link), p);
1110 p->state = IR_TCL_R_Connecting;
1114 p->state = IR_TCL_R_Idle;
1116 ir_tcl_eval (p->interp, p->callback);
1120 Tcl_AppendResult (interp, p->hostname, NULL);
1125 * ir_tcl_disconnect: close connection
1127 void ir_tcl_disconnect (IrTcl_Obj *p)
1131 logf(LOG_DEBUG, "Closing connection to %s", p->hostname);
1134 #if IRTCL_GENERIC_FILES
1135 ir_select_remove_write (p->csFile, p);
1136 ir_select_remove (p->csFile, p);
1138 ir_select_remove_write (cs_fileno (p->cs_link), p);
1139 ir_select_remove (cs_fileno (p->cs_link), p);
1142 odr_reset (p->odr_in);
1144 assert (p->cs_link);
1145 cs_close (p->cs_link);
1147 #if IRTCL_GENERIC_FILES
1148 Tcl_FreeFile (p->csFile);
1152 ODR_MASK_ZERO (&p->options);
1153 ODR_MASK_SET (&p->options, 0);
1154 ODR_MASK_SET (&p->options, 1);
1155 ODR_MASK_SET (&p->options, 4);
1156 ODR_MASK_SET (&p->options, 7);
1157 ODR_MASK_SET (&p->options, 14);
1159 ODR_MASK_ZERO (&p->protocolVersion);
1160 ODR_MASK_SET (&p->protocolVersion, 0);
1161 ODR_MASK_SET (&p->protocolVersion, 1);
1164 assert (!p->cs_link);
1168 * do_disconnect: disconnect method on IR object
1170 static int do_disconnect (void *obj, Tcl_Interp *interp,
1171 int argc, char **argv)
1177 p->state = IR_TCL_R_Idle;
1178 p->eventType = NULL;
1181 #if IRTCL_GENERIC_FILES
1186 ir_tcl_disconnect (p);
1191 * do_comstack: Set/get comstack method on IR object
1193 static int do_comstack (void *o, Tcl_Interp *interp,
1194 int argc, char **argv)
1199 return ir_tcl_strdup (interp, &obj->comstackType, "tcpip");
1200 else if (argc == -1)
1201 return ir_tcl_strdel (interp, &obj->comstackType);
1204 free (obj->comstackType);
1205 if (ir_tcl_strdup (interp, &obj->comstackType, argv[2]) == TCL_ERROR)
1208 Tcl_AppendElement (interp, obj->comstackType);
1213 * do_logLevel: Set log level
1215 static int do_logLevel (void *o, Tcl_Interp *interp,
1216 int argc, char **argv)
1221 log_init (log_mask_str (argv[2]), "", NULL);
1223 log_init (log_mask_str (argv[2]), argv[3], NULL);
1225 log_init (log_mask_str (argv[2]), argv[3], argv[4]);
1231 * do_eventType: Return type of last event
1233 static int do_eventType (void *obj, Tcl_Interp *interp,
1234 int argc, char **argv)
1240 p->eventType = NULL;
1243 Tcl_AppendElement (interp, p->eventType ? p->eventType : "");
1249 * do_callback: add callback
1251 static int do_callback (void *obj, Tcl_Interp *interp,
1252 int argc, char **argv)
1261 else if (argc == -1)
1262 return ir_tcl_strdel (interp, &p->callback);
1268 if (ir_tcl_strdup (interp, &p->callback, argv[2]) == TCL_ERROR)
1278 * do_failback: add error handle callback
1280 static int do_failback (void *obj, Tcl_Interp *interp,
1281 int argc, char **argv)
1290 else if (argc == -1)
1291 return ir_tcl_strdel (interp, &p->failback);
1297 if (ir_tcl_strdup (interp, &p->failback, argv[2]) == TCL_ERROR)
1307 * do_initResponse: add init response handler
1309 static int do_initResponse (void *obj, Tcl_Interp *interp,
1310 int argc, char **argv)
1316 p->initResponse = NULL;
1319 else if (argc == -1)
1320 return ir_tcl_strdel (interp, &p->initResponse);
1323 free (p->initResponse);
1326 if (ir_tcl_strdup (interp, &p->initResponse, argv[2]) == TCL_ERROR)
1330 p->initResponse = NULL;
1335 * do_protocol: Set/get protocol method on IR object
1337 static int do_protocol (void *o, Tcl_Interp *interp, int argc, char **argv)
1343 p->protocol_type = PROTO_Z3950;
1348 if (!strcmp (argv[2], "Z39"))
1349 p->protocol_type = PROTO_Z3950;
1350 else if (!strcmp (argv[2], "SR"))
1351 p->protocol_type = PROTO_SR;
1354 Tcl_AppendResult (interp, "Bad protocol: ", argv[2], NULL);
1359 switch (p->protocol_type)
1362 Tcl_AppendElement (interp, "Z39");
1365 Tcl_AppendElement (interp, "SR");
1372 * do_triggerResourceControl:
1374 static int do_triggerResourceControl (void *obj, Tcl_Interp *interp,
1375 int argc, char **argv)
1379 Z_TriggerResourceControlRequest *req;
1380 bool_t is_false = 0;
1386 interp->result = "triggerResourceControl: not connected";
1389 apdu = zget_APDU (p->odr_out, Z_APDU_triggerResourceControlRequest);
1390 req = apdu->u.triggerResourceControlRequest;
1391 *req->requestedAction = Z_TriggerResourceCtrl_cancel;
1392 req->resultSetWanted = &is_false;
1394 return ir_tcl_send_APDU (interp, p, apdu, "triggerResourceControl",
1399 * do_databaseNames: specify database names
1401 static int do_databaseNames (void *obj, Tcl_Interp *interp,
1402 int argc, char **argv)
1405 IrTcl_SetCObj *p = obj;
1409 for (i=0; i<p->num_databaseNames; i++)
1410 free (p->databaseNames[i]);
1411 free (p->databaseNames);
1415 p->num_databaseNames = 0;
1416 p->databaseNames = NULL;
1421 for (i=0; i<p->num_databaseNames; i++)
1422 Tcl_AppendElement (interp, p->databaseNames[i]);
1425 if (p->databaseNames)
1427 for (i=0; i<p->num_databaseNames; i++)
1428 free (p->databaseNames[i]);
1429 free (p->databaseNames);
1431 p->num_databaseNames = argc - 2;
1433 ir_tcl_malloc (sizeof(*p->databaseNames) * (1+p->num_databaseNames));
1434 for (i=0; i<p->num_databaseNames; i++)
1436 if (ir_tcl_strdup (interp, &p->databaseNames[i], argv[2+i])
1440 p->databaseNames[i] = NULL;
1445 * do_replaceIndicator: Set/get replace Set indicator
1447 static int do_replaceIndicator (void *obj, Tcl_Interp *interp,
1448 int argc, char **argv)
1450 IrTcl_SetCObj *p = obj;
1454 p->replaceIndicator = 1;
1457 return ir_tcl_get_set_int (&p->replaceIndicator, interp, argc, argv);
1461 * do_queryType: Set/Get query method
1463 static int do_queryType (void *obj, Tcl_Interp *interp,
1464 int argc, char **argv)
1466 IrTcl_SetCObj *p = obj;
1469 return ir_tcl_strdup (interp, &p->queryType, "rpn");
1470 else if (argc == -1)
1471 return ir_tcl_strdel (interp, &p->queryType);
1474 free (p->queryType);
1475 if (ir_tcl_strdup (interp, &p->queryType, argv[2]) == TCL_ERROR)
1478 Tcl_AppendResult (interp, p->queryType, NULL);
1483 * do_userInformationField: Get User information field
1485 static int do_userInformationField (void *obj, Tcl_Interp *interp,
1486 int argc, char **argv)
1492 p->userInformationField = NULL;
1495 else if (argc == -1)
1496 return ir_tcl_strdel (interp, &p->userInformationField);
1497 Tcl_AppendResult (interp, p->userInformationField, NULL);
1502 * do_smallSetUpperBound: Set/get small set upper bound
1504 static int do_smallSetUpperBound (void *o, Tcl_Interp *interp,
1505 int argc, char **argv)
1507 IrTcl_SetCObj *p = o;
1511 p->smallSetUpperBound = 0;
1514 return ir_tcl_get_set_int (&p->smallSetUpperBound, interp, argc, argv);
1518 * do_largeSetLowerBound: Set/get large set lower bound
1520 static int do_largeSetLowerBound (void *o, Tcl_Interp *interp,
1521 int argc, char **argv)
1523 IrTcl_SetCObj *p = o;
1527 p->largeSetLowerBound = 2;
1530 return ir_tcl_get_set_int (&p->largeSetLowerBound, interp, argc, argv);
1534 * do_mediumSetPresentNumber: Set/get large set lower bound
1536 static int do_mediumSetPresentNumber (void *o, Tcl_Interp *interp,
1537 int argc, char **argv)
1539 IrTcl_SetCObj *p = o;
1543 p->mediumSetPresentNumber = 0;
1546 return ir_tcl_get_set_int (&p->mediumSetPresentNumber, interp, argc, argv);
1550 * do_referenceId: Set/Get referenceId
1552 static int do_referenceId (void *obj, Tcl_Interp *interp,
1553 int argc, char **argv)
1555 IrTcl_SetCObj *p = obj;
1559 p->referenceId = NULL;
1562 else if (argc == -1)
1563 return ir_tcl_strdel (interp, &p->referenceId);
1566 free (p->referenceId);
1567 if (ir_tcl_strdup (interp, &p->referenceId, argv[2]) == TCL_ERROR)
1570 Tcl_AppendResult (interp, p->referenceId, NULL);
1575 * do_preferredRecordSyntax: Set/get preferred record syntax
1577 static int do_preferredRecordSyntax (void *obj, Tcl_Interp *interp,
1578 int argc, char **argv)
1580 IrTcl_SetCObj *p = obj;
1584 p->preferredRecordSyntax = NULL;
1587 else if (argc == -1)
1589 free (p->preferredRecordSyntax);
1590 p->preferredRecordSyntax = NULL;
1595 free (p->preferredRecordSyntax);
1596 p->preferredRecordSyntax = NULL;
1597 if (argv[2][0] && (p->preferredRecordSyntax =
1598 ir_tcl_malloc (sizeof(*p->preferredRecordSyntax))))
1599 *p->preferredRecordSyntax = IrTcl_getRecordSyntaxVal (argv[2]);
1603 Tcl_AppendElement (interp, IrTcl_getRecordSyntaxStr
1604 (*p->preferredRecordSyntax));
1611 * do_elementSetNames: Set/Get element Set Names
1613 static int do_elementSetNames (void *obj, Tcl_Interp *interp,
1614 int argc, char **argv)
1616 IrTcl_SetCObj *p = obj;
1620 p->elementSetNames = NULL;
1623 else if (argc == -1)
1624 return ir_tcl_strdel (interp, &p->elementSetNames);
1627 free (p->elementSetNames);
1628 if (ir_tcl_strdup (interp, &p->elementSetNames, argv[2]) == TCL_ERROR)
1631 Tcl_AppendResult (interp, p->elementSetNames, NULL);
1636 * do_smallSetElementSetNames: Set/Get small Set Element Set Names
1638 static int do_smallSetElementSetNames (void *obj, Tcl_Interp *interp,
1639 int argc, char **argv)
1641 IrTcl_SetCObj *p = obj;
1645 p->smallSetElementSetNames = NULL;
1648 else if (argc == -1)
1649 return ir_tcl_strdel (interp, &p->smallSetElementSetNames);
1652 free (p->smallSetElementSetNames);
1653 if (ir_tcl_strdup (interp, &p->smallSetElementSetNames,
1654 argv[2]) == TCL_ERROR)
1657 Tcl_AppendResult (interp, p->smallSetElementSetNames, NULL);
1662 * do_mediumSetElementSetNames: Set/Get medium Set Element Set Names
1664 static int do_mediumSetElementSetNames (void *obj, Tcl_Interp *interp,
1665 int argc, char **argv)
1667 IrTcl_SetCObj *p = obj;
1671 p->mediumSetElementSetNames = NULL;
1674 else if (argc == -1)
1675 return ir_tcl_strdel (interp, &p->mediumSetElementSetNames);
1678 free (p->mediumSetElementSetNames);
1679 if (ir_tcl_strdup (interp, &p->mediumSetElementSetNames,
1680 argv[2]) == TCL_ERROR)
1683 Tcl_AppendResult (interp, p->mediumSetElementSetNames, NULL);
1687 static IrTcl_Method ir_method_tab[] = {
1688 { "comstack", do_comstack, NULL },
1689 { "protocol", do_protocol, NULL },
1690 { "failback", do_failback, NULL },
1691 { "failInfo", do_failInfo, NULL },
1692 { "apduInfo", do_apduInfo, NULL },
1693 { "logLevel", do_logLevel, NULL },
1695 { "eventType", do_eventType, NULL },
1696 { "connect", do_connect, NULL },
1697 { "protocolVersion", do_protocolVersion, NULL },
1698 { "preferredMessageSize", do_preferredMessageSize, NULL },
1699 { "maximumRecordSize", do_maximumRecordSize, NULL },
1700 { "implementationName", do_implementationName, NULL },
1701 { "implementationId", do_implementationId, NULL },
1702 { "implementationVersion", do_implementationVersion, NULL },
1703 { "targetImplementationName", do_targetImplementationName, NULL },
1704 { "targetImplementationId", do_targetImplementationId, NULL },
1705 { "targetImplementationVersion", do_targetImplementationVersion, NULL},
1706 { "userInformationField", do_userInformationField, NULL},
1707 { "idAuthentication", do_idAuthentication, NULL},
1708 { "options", do_options, NULL},
1709 { "init", do_init_request, NULL},
1710 { "initResult", do_initResult, NULL},
1711 { "disconnect", do_disconnect, NULL},
1712 { "callback", do_callback, NULL},
1713 { "initResponse", do_initResponse, NULL},
1714 { "triggerResourceControl", do_triggerResourceControl, NULL},
1715 { "initResponse", do_initResponse, NULL},
1719 static IrTcl_Method ir_set_c_method_tab[] = {
1720 { "databaseNames", do_databaseNames, NULL},
1721 { "replaceIndicator", do_replaceIndicator, NULL},
1722 { "queryType", do_queryType, NULL},
1723 { "preferredRecordSyntax", do_preferredRecordSyntax, NULL},
1724 { "smallSetUpperBound", do_smallSetUpperBound, NULL},
1725 { "largeSetLowerBound", do_largeSetLowerBound, NULL},
1726 { "mediumSetPresentNumber", do_mediumSetPresentNumber, NULL},
1727 { "referenceId", do_referenceId, NULL},
1728 { "elementSetNames", do_elementSetNames, NULL},
1729 { "smallSetElementSetNames", do_smallSetElementSetNames, NULL},
1730 { "mediumSetElementSetNames", do_mediumSetElementSetNames, NULL},
1735 * ir_obj_method: IR Object methods
1737 static int ir_obj_method (ClientData clientData, Tcl_Interp *interp,
1738 int argc, char **argv)
1740 IrTcl_Methods tab[3];
1741 IrTcl_Obj *p = clientData;
1747 tab[0].tab = ir_method_tab;
1749 tab[1].tab = ir_set_c_method_tab;
1750 tab[1].obj = &p->set_inher;
1753 ir_tcl_method (interp, argc, argv, tab, &r);
1758 * ir_obj_delete: IR Object disposal
1760 static void ir_obj_delete (ClientData clientData)
1762 IrTcl_Obj *obj = clientData;
1763 IrTcl_Methods tab[3];
1766 if (obj->ref_count > 0)
1768 assert (obj->ref_count == 0);
1770 logf (LOG_DEBUG, "ir object delete");
1771 tab[0].tab = ir_method_tab;
1773 tab[1].tab = ir_set_c_method_tab;
1774 tab[1].obj = &obj->set_inher;
1777 ir_tcl_method (NULL, -1, NULL, tab, NULL);
1780 odr_destroy (obj->odr_in);
1781 odr_destroy (obj->odr_out);
1782 odr_destroy (obj->odr_pr);
1787 * ir_obj_init: IR Object initialization
1789 int ir_obj_init (ClientData clientData, Tcl_Interp *interp,
1790 int argc, char **argv, ClientData *subData,
1791 ClientData parentData)
1793 IrTcl_Methods tab[3];
1801 interp->result = "wrong # args";
1804 obj = ir_tcl_malloc (sizeof(*obj));
1807 obj->bibset = ccl_qual_mk ();
1808 if ((inf = fopen ("default.bib", "r")))
1810 ccl_qual_file (obj->bibset, inf);
1815 logf (LOG_DEBUG, "ir object create %s", argv[1]);
1816 obj->odr_in = odr_createmem (ODR_DECODE);
1817 obj->odr_out = odr_createmem (ODR_ENCODE);
1818 obj->odr_pr = odr_createmem (ODR_PRINT);
1819 obj->state = IR_TCL_R_Idle;
1820 obj->interp = interp;
1824 obj->request_queue = NULL;
1826 tab[0].tab = ir_method_tab;
1828 tab[1].tab = ir_set_c_method_tab;
1829 tab[1].obj = &obj->set_inher;
1832 if (ir_tcl_method (interp, 0, NULL, tab, NULL) == TCL_ERROR)
1834 Tcl_AppendResult (interp, "Failed to initialize ", argv[1], NULL);
1843 * ir_obj_mk: IR Object creation
1845 static int ir_obj_mk (ClientData clientData, Tcl_Interp *interp,
1846 int argc, char **argv)
1849 int r = ir_obj_init (clientData, interp, argc, argv, &subData, 0);
1853 Tcl_CreateCommand (interp, argv[1], ir_obj_method,
1854 subData, ir_obj_delete);
1858 IrTcl_Class ir_obj_class = {
1866 /* ------------------------------------------------------- */
1868 * do_search: Do search request
1870 static int do_search (void *o, Tcl_Interp *interp, int argc, char **argv)
1872 Z_SearchRequest *req;
1876 IrTcl_SetObj *obj = o;
1887 interp->result = "wrong # args";
1890 if (!obj->set_inher.num_databaseNames)
1892 interp->result = "no databaseNames";
1897 interp->result = "search: not connected";
1900 apdu = zget_APDU (p->odr_out, Z_APDU_searchRequest);
1901 req = apdu->u.searchRequest;
1905 bib1.proto = p->protocol_type;
1906 bib1.oclass = CLASS_ATTSET;
1907 bib1.value = VAL_BIB1;
1909 set_referenceId (p->odr_out, &req->referenceId,
1910 obj->set_inher.referenceId);
1912 req->smallSetUpperBound = &obj->set_inher.smallSetUpperBound;
1913 req->largeSetLowerBound = &obj->set_inher.largeSetLowerBound;
1914 req->mediumSetPresentNumber = &obj->set_inher.mediumSetPresentNumber;
1915 req->replaceIndicator = &obj->set_inher.replaceIndicator;
1916 req->resultSetName = obj->setName ? obj->setName : "Default";
1917 logf (LOG_DEBUG, "Search, resultSetName %s", req->resultSetName);
1918 req->num_databaseNames = obj->set_inher.num_databaseNames;
1919 req->databaseNames = obj->set_inher.databaseNames;
1920 for (r=0; r < obj->set_inher.num_databaseNames; r++)
1921 logf (LOG_DEBUG, " Database %s", obj->set_inher.databaseNames[r]);
1922 if (obj->set_inher.preferredRecordSyntax)
1924 struct oident ident;
1926 ident.proto = p->protocol_type;
1927 ident.oclass = CLASS_RECSYN;
1928 ident.value = *obj->set_inher.preferredRecordSyntax;
1929 logf (LOG_DEBUG, "Preferred record syntax is %d", ident.value);
1930 req->preferredRecordSyntax = odr_oiddup (p->odr_out,
1931 oid_getoidbyent (&ident));
1934 req->preferredRecordSyntax = 0;
1936 if (obj->set_inher.smallSetElementSetNames &&
1937 *obj->set_inher.smallSetElementSetNames)
1939 Z_ElementSetNames *esn = odr_malloc (p->odr_out, sizeof(*esn));
1941 esn->which = Z_ElementSetNames_generic;
1942 esn->u.generic = obj->set_inher.smallSetElementSetNames;
1943 req->smallSetElementSetNames = esn;
1946 req->smallSetElementSetNames = NULL;
1948 if (obj->set_inher.mediumSetElementSetNames &&
1949 *obj->set_inher.mediumSetElementSetNames)
1951 Z_ElementSetNames *esn = odr_malloc (p->odr_out, sizeof(*esn));
1953 esn->which = Z_ElementSetNames_generic;
1954 esn->u.generic = obj->set_inher.mediumSetElementSetNames;
1955 req->mediumSetElementSetNames = esn;
1958 req->mediumSetElementSetNames = NULL;
1960 req->query = &query;
1962 if (!strcmp (obj->set_inher.queryType, "rpn"))
1964 Z_RPNQuery *RPNquery;
1966 RPNquery = p_query_rpn (p->odr_out, argv[2]);
1969 Tcl_AppendResult (interp, "Syntax error in query", NULL);
1972 RPNquery->attributeSetId = oid_getoidbyent (&bib1);
1973 query.which = Z_Query_type_1;
1974 query.u.type_1 = RPNquery;
1975 logf (LOG_DEBUG, "RPN");
1978 else if (!strcmp (obj->set_inher.queryType, "cclrpn"))
1982 struct ccl_rpn_node *rpn;
1983 Z_RPNQuery *RPNquery;
1985 rpn = ccl_find_str(p->bibset, argv[2], &error, &pos);
1988 Tcl_AppendResult (interp, "CCL error: ",
1989 ccl_err_msg(error), NULL);
1992 ccl_pr_tree (rpn, stderr);
1993 fprintf (stderr, "\n");
1994 assert((RPNquery = ccl_rpn_query(rpn)));
1995 RPNquery->attributeSetId = oid_getoidbyent (&bib1);
1996 query.which = Z_Query_type_1;
1997 query.u.type_1 = RPNquery;
1998 logf (LOG_DEBUG, "CCLRPN");
2001 else if (!strcmp (obj->set_inher.queryType, "ccl"))
2003 query.which = Z_Query_type_2;
2004 query.u.type_2 = &ccl_query;
2005 ccl_query.buf = (unsigned char *) argv[2];
2006 ccl_query.len = strlen (argv[2]);
2007 logf (LOG_DEBUG, "CCL");
2011 interp->result = "unknown query method";
2014 return ir_tcl_send_APDU (interp, p, apdu, "search", *argv);
2018 * do_searchResponse: add search response handler
2020 static int do_searchResponse (void *o, Tcl_Interp *interp,
2021 int argc, char **argv)
2023 IrTcl_SetObj *obj = o;
2027 obj->searchResponse = NULL;
2030 else if (argc == -1)
2031 return ir_tcl_strdel (interp, &obj->searchResponse);
2034 free (obj->searchResponse);
2037 if (ir_tcl_strdup (interp, &obj->searchResponse, argv[2])
2042 obj->searchResponse = NULL;
2048 * do_presentResponse: add present response handler
2050 static int do_presentResponse (void *o, Tcl_Interp *interp,
2051 int argc, char **argv)
2053 IrTcl_SetObj *obj = o;
2057 obj->presentResponse = NULL;
2060 else if (argc == -1)
2061 return ir_tcl_strdel (interp, &obj->presentResponse);
2064 free (obj->presentResponse);
2067 if (ir_tcl_strdup (interp, &obj->presentResponse, argv[2])
2072 obj->presentResponse = NULL;
2078 * do_resultCount: Get number of hits
2080 static int do_resultCount (void *o, Tcl_Interp *interp,
2081 int argc, char **argv)
2083 IrTcl_SetObj *obj = o;
2087 obj->resultCount = 0;
2090 return ir_tcl_get_set_int (&obj->resultCount, interp, argc, argv);
2094 * do_searchStatus: Get search status (after search response)
2096 static int do_searchStatus (void *o, Tcl_Interp *interp,
2097 int argc, char **argv)
2099 IrTcl_SetObj *obj = o;
2103 return ir_tcl_get_set_int (&obj->searchStatus, interp, argc, argv);
2107 * do_presentStatus: Get search status (after search/present response)
2109 static int do_presentStatus (void *o, Tcl_Interp *interp,
2110 int argc, char **argv)
2112 IrTcl_SetObj *obj = o;
2116 return ir_tcl_get_set_int (&obj->presentStatus, interp, argc, argv);
2120 * do_nextResultSetPosition: Get next result set position
2121 * (after search/present response)
2123 static int do_nextResultSetPosition (void *o, Tcl_Interp *interp,
2124 int argc, char **argv)
2126 IrTcl_SetObj *obj = o;
2130 obj->nextResultSetPosition = 0;
2133 return ir_tcl_get_set_int (&obj->nextResultSetPosition, interp,
2138 * do_setName: Set result Set name
2140 static int do_setName (void *o, Tcl_Interp *interp,
2141 int argc, char **argv)
2143 IrTcl_SetObj *obj = o;
2146 return ir_tcl_strdup (interp, &obj->setName, "Default");
2147 else if (argc == -1)
2148 return ir_tcl_strdel (interp, &obj->setName);
2151 free (obj->setName);
2152 if (ir_tcl_strdup (interp, &obj->setName, argv[2])
2156 Tcl_AppendElement (interp, obj->setName);
2161 * do_numberOfRecordsReturned: Get number of records returned
2163 static int do_numberOfRecordsReturned (void *o, Tcl_Interp *interp,
2164 int argc, char **argv)
2166 IrTcl_SetObj *obj = o;
2170 obj->numberOfRecordsReturned = 0;
2173 return ir_tcl_get_set_int (&obj->numberOfRecordsReturned, interp,
2178 * do_type: Return type (if any) at position.
2180 static int do_type (void *o, Tcl_Interp *interp, int argc, char **argv)
2182 IrTcl_SetObj *obj = o;
2184 IrTcl_RecordList *rl;
2188 obj->record_list = NULL;
2191 else if (argc == -1)
2193 delete_IR_records (obj);
2198 sprintf (interp->result, "wrong # args");
2201 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2203 rl = find_IR_record (obj, offset);
2206 logf (LOG_DEBUG, "No record at position %d", offset);
2211 case Z_NamePlusRecord_databaseRecord:
2212 interp->result = "DB";
2214 case Z_NamePlusRecord_surrogateDiagnostic:
2215 interp->result = "SD";
2223 * do_recordType: Return record type (if any) at position.
2225 static int do_recordType (void *o, Tcl_Interp *interp, int argc, char **argv)
2227 IrTcl_SetObj *obj = o;
2229 IrTcl_RecordList *rl;
2235 else if (argc == -1)
2241 sprintf (interp->result, "wrong # args");
2244 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2246 rl = find_IR_record (obj, offset);
2249 if (rl->which != Z_NamePlusRecord_databaseRecord)
2251 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2254 Tcl_AppendElement (interp, (char*)
2255 IrTcl_getRecordSyntaxStr (rl->u.dbrec.type));
2260 * set record elements (for record extraction)
2262 static int do_recordElements (void *o, Tcl_Interp *interp,
2263 int argc, char **argv)
2265 IrTcl_SetObj *obj = o;
2269 obj->recordElements = NULL;
2272 else if (argc == -1)
2273 return ir_tcl_strdel (NULL, &obj->recordElements);
2276 sprintf (interp->result, "wrong # args");
2281 free (obj->recordElements);
2282 return ir_tcl_strdup (NULL, &obj->recordElements,
2283 (*argv[2] ? argv[2] : NULL));
2285 Tcl_AppendResult (interp, obj->recordElements, NULL);
2292 static int ir_diagResult (Tcl_Interp *interp, IrTcl_Diagnostic *list, int num)
2298 for (i = 0; i<num; i++)
2300 logf (LOG_DEBUG, "Diagnostic, code %d", list[i].condition);
2301 sprintf (buf, "%d", list[i].condition);
2302 Tcl_AppendElement (interp, buf);
2303 cp = diagbib1_str (list[i].condition);
2305 Tcl_AppendElement (interp, (char*) cp);
2307 Tcl_AppendElement (interp, "");
2308 if (list[i].addinfo)
2309 Tcl_AppendElement (interp, (char*) list[i].addinfo);
2311 Tcl_AppendElement (interp, "");
2317 * do_diag: Return diagnostic record info
2319 static int do_diag (void *o, Tcl_Interp *interp, int argc, char **argv)
2321 IrTcl_SetObj *obj = o;
2323 IrTcl_RecordList *rl;
2329 sprintf (interp->result, "wrong # args");
2332 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2334 rl = find_IR_record (obj, offset);
2337 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2340 if (rl->which != Z_NamePlusRecord_surrogateDiagnostic)
2342 Tcl_AppendResult (interp, "No Diagnostic record at #", argv[2], NULL);
2345 return ir_diagResult (interp, rl->u.surrogateDiagnostics.list,
2346 rl->u.surrogateDiagnostics.num);
2350 * do_getMarc: Get ISO2709 Record lines/fields
2352 static int do_getMarc (void *o, Tcl_Interp *interp, int argc, char **argv)
2354 IrTcl_SetObj *obj = o;
2356 IrTcl_RecordList *rl;
2362 sprintf (interp->result, "wrong # args");
2365 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2367 rl = find_IR_record (obj, offset);
2370 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2373 if (rl->which != Z_NamePlusRecord_databaseRecord)
2375 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2378 return ir_tcl_get_marc (interp, rl->u.dbrec.buf, argc, argv);
2382 * do_getSutrs: Get SUTRS Record
2384 static int do_getSutrs (void *o, Tcl_Interp *interp, int argc, char **argv)
2386 IrTcl_SetObj *obj = o;
2388 IrTcl_RecordList *rl;
2394 sprintf (interp->result, "wrong # args");
2397 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2399 rl = find_IR_record (obj, offset);
2402 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2405 if (rl->which != Z_NamePlusRecord_databaseRecord)
2407 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2410 if (rl->u.dbrec.type != VAL_SUTRS)
2412 Tcl_AppendElement (interp, rl->u.dbrec.buf);
2418 * do_getGrs: Get a GRS1 Record
2420 static int do_getGrs (void *o, Tcl_Interp *interp, int argc, char **argv)
2422 IrTcl_SetObj *obj = o;
2424 IrTcl_RecordList *rl;
2430 sprintf (interp->result, "wrong # args");
2433 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2435 rl = find_IR_record (obj, offset);
2438 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2441 if (rl->which != Z_NamePlusRecord_databaseRecord)
2443 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2446 if (rl->u.dbrec.type != VAL_GRS1)
2448 return ir_tcl_get_grs (interp, rl->u.dbrec.u.grs1, argc, argv);
2453 * do_responseStatus: Return response status (present or search)
2455 static int do_responseStatus (void *o, Tcl_Interp *interp,
2456 int argc, char **argv)
2458 IrTcl_SetObj *obj = o;
2462 obj->recordFlag = 0;
2463 obj->nonSurrogateDiagnosticNum = 0;
2464 obj->nonSurrogateDiagnosticList = NULL;
2467 else if (argc == -1)
2469 ir_deleteDiags (&obj->nonSurrogateDiagnosticList,
2470 &obj->nonSurrogateDiagnosticNum);
2473 if (!obj->recordFlag)
2475 Tcl_AppendElement (interp, "OK");
2480 case Z_Records_DBOSD:
2481 Tcl_AppendElement (interp, "DBOSD");
2484 Tcl_AppendElement (interp, "NSD");
2485 return ir_diagResult (interp, obj->nonSurrogateDiagnosticList,
2486 obj->nonSurrogateDiagnosticNum);
2492 * do_present: Perform Present Request
2495 static int do_present (void *o, Tcl_Interp *interp, int argc, char **argv)
2497 IrTcl_SetObj *obj = o;
2500 Z_PresentRequest *req;
2508 if (Tcl_GetInt (interp, argv[2], &start) == TCL_ERROR)
2515 if (Tcl_GetInt (interp, argv[3], &number) == TCL_ERROR)
2523 interp->result = "present: not connected";
2528 obj->number = number;
2530 apdu = zget_APDU (p->odr_out, Z_APDU_presentRequest);
2531 req = apdu->u.presentRequest;
2533 set_referenceId (p->odr_out, &req->referenceId,
2534 obj->set_inher.referenceId);
2536 req->resultSetId = obj->setName ? obj->setName : "Default";
2538 req->resultSetStartPoint = &start;
2539 req->numberOfRecordsRequested = &number;
2540 if (obj->set_inher.preferredRecordSyntax)
2542 struct oident ident;
2544 ident.proto = p->protocol_type;
2545 ident.oclass = CLASS_RECSYN;
2546 ident.value = *obj->set_inher.preferredRecordSyntax;
2547 logf (LOG_DEBUG, "Preferred record syntax is %d", ident.value);
2548 req->preferredRecordSyntax = odr_oiddup (p->odr_out,
2549 oid_getoidbyent (&ident));
2552 req->preferredRecordSyntax = 0;
2554 if (obj->set_inher.elementSetNames && *obj->set_inher.elementSetNames)
2556 Z_ElementSetNames *esn = odr_malloc (p->odr_out, sizeof(*esn));
2557 Z_RecordComposition *compo = odr_malloc (p->odr_out, sizeof(*compo));
2559 esn->which = Z_ElementSetNames_generic;
2560 esn->u.generic = obj->set_inher.elementSetNames;
2562 req->recordComposition = compo;
2563 compo->which = Z_RecordComp_simple;
2564 compo->u.simple = esn;
2567 req->recordComposition = NULL;
2568 return ir_tcl_send_APDU (interp, p, apdu, "present", *argv);
2572 * do_loadFile: Load result set from file
2575 static int do_loadFile (void *o, Tcl_Interp *interp,
2576 int argc, char **argv)
2578 IrTcl_SetObj *setobj = o;
2588 interp->result = "wrong # args";
2591 inf = fopen (argv[2], "r");
2594 Tcl_AppendResult (interp, "Cannot open ", argv[2], NULL);
2597 while ((buf = ir_tcl_fread_marc (inf, &size)))
2599 IrTcl_RecordList *rl;
2601 rl = new_IR_record (setobj, no, Z_NamePlusRecord_databaseRecord, "F");
2602 rl->u.dbrec.type = VAL_USMARC;
2603 rl->u.dbrec.buf = buf;
2604 rl->u.dbrec.size = size;
2607 setobj->numberOfRecordsReturned = no-1;
2612 static IrTcl_Method ir_set_method_tab[] = {
2613 { "search", do_search, NULL},
2614 { "searchResponse", do_searchResponse, NULL},
2615 { "presentResponse", do_presentResponse, NULL},
2616 { "searchStatus", do_searchStatus, NULL},
2617 { "presentStatus", do_presentStatus, NULL},
2618 { "nextResultSetPosition", do_nextResultSetPosition, NULL},
2619 { "setName", do_setName, NULL},
2620 { "resultCount", do_resultCount, NULL},
2621 { "numberOfRecordsReturned", do_numberOfRecordsReturned, NULL},
2622 { "present", do_present, NULL},
2623 { "type", do_type, NULL},
2624 { "getMarc", do_getMarc, NULL},
2625 { "getSutrs", do_getSutrs, NULL},
2626 { "getGrs", do_getGrs, NULL},
2627 { "recordType", do_recordType, NULL},
2628 { "recordElements", do_recordElements, NULL},
2629 { "diag", do_diag, NULL},
2630 { "responseStatus", do_responseStatus, NULL},
2631 { "loadFile", do_loadFile, NULL},
2636 * ir_set_obj_method: IR Set Object methods
2638 static int ir_set_obj_method (ClientData clientData, Tcl_Interp *interp,
2639 int argc, char **argv)
2641 IrTcl_Methods tabs[3];
2642 IrTcl_SetObj *p = clientData;
2647 interp->result = "wrong # args";
2650 tabs[0].tab = ir_set_method_tab;
2652 tabs[1].tab = ir_set_c_method_tab;
2653 tabs[1].obj = &p->set_inher;
2656 ir_tcl_method (interp, argc, argv, tabs, &r);
2661 * ir_set_obj_delete: IR Set Object disposal
2663 static void ir_set_obj_delete (ClientData clientData)
2665 IrTcl_Methods tabs[3];
2666 IrTcl_SetObj *p = clientData;
2668 logf (LOG_DEBUG, "ir set delete");
2670 tabs[0].tab = ir_set_method_tab;
2672 tabs[1].tab = ir_set_c_method_tab;
2673 tabs[1].obj = &p->set_inher;
2676 ir_tcl_method (NULL, -1, NULL, tabs, NULL);
2682 * ir_set_obj_init: IR Set Object initialization
2684 static int ir_set_obj_init (ClientData clientData, Tcl_Interp *interp,
2685 int argc, char **argv, ClientData *subData,
2686 ClientData parentData)
2688 IrTcl_Methods tabs[3];
2691 if (argc < 2 || argc > 3)
2693 interp->result = "wrong # args";
2696 obj = ir_tcl_malloc (sizeof(*obj));
2697 logf (LOG_DEBUG, "ir set create");
2704 obj->parent = (IrTcl_Obj *) parentData;
2706 dst = &obj->set_inher;
2707 src = &obj->parent->set_inher;
2709 if ((dst->num_databaseNames = src->num_databaseNames))
2711 dst->databaseNames =
2712 ir_tcl_malloc (sizeof (*dst->databaseNames)
2713 * (1+dst->num_databaseNames));
2714 for (i = 0; i < dst->num_databaseNames; i++)
2715 if (ir_tcl_strdup (interp, &dst->databaseNames[i],
2716 src->databaseNames[i]) == TCL_ERROR)
2718 dst->databaseNames[i] = NULL;
2721 dst->databaseNames = NULL;
2722 if (ir_tcl_strdup (interp, &dst->queryType, src->queryType)
2726 if (ir_tcl_strdup (interp, &dst->referenceId, src->referenceId)
2730 if (ir_tcl_strdup (interp, &dst->elementSetNames, src->elementSetNames)
2734 if (ir_tcl_strdup (interp, &dst->smallSetElementSetNames,
2735 src->smallSetElementSetNames)
2739 if (ir_tcl_strdup (interp, &dst->mediumSetElementSetNames,
2740 src->mediumSetElementSetNames)
2744 if (src->preferredRecordSyntax &&
2745 (dst->preferredRecordSyntax
2746 = ir_tcl_malloc (sizeof(*dst->preferredRecordSyntax))))
2747 *dst->preferredRecordSyntax = *src->preferredRecordSyntax;
2749 dst->preferredRecordSyntax = NULL;
2750 dst->replaceIndicator = src->replaceIndicator;
2751 dst->smallSetUpperBound = src->smallSetUpperBound;
2752 dst->largeSetLowerBound = src->largeSetLowerBound;
2753 dst->mediumSetPresentNumber = src->mediumSetPresentNumber;
2758 tabs[0].tab = ir_set_method_tab;
2762 if (ir_tcl_method (interp, 0, NULL, tabs, NULL) == TCL_ERROR)
2770 * ir_set_obj_mk: IR Set Object creation
2772 static int ir_set_obj_mk (ClientData clientData, Tcl_Interp *interp,
2773 int argc, char **argv)
2776 ClientData parentData = 0;
2781 Tcl_CmdInfo parent_info;
2782 if (!Tcl_GetCommandInfo (interp, argv[2], &parent_info))
2784 interp->result = "No parent";
2787 parentData = parent_info.clientData;
2789 r = ir_set_obj_init (clientData, interp, argc, argv, &subData, parentData);
2792 Tcl_CreateCommand (interp, argv[1], ir_set_obj_method,
2793 subData, ir_set_obj_delete);
2797 IrTcl_Class ir_set_obj_class = {
2804 /* ------------------------------------------------------- */
2807 * do_scan: Perform scan
2809 static int do_scan (void *o, Tcl_Interp *interp, int argc, char **argv)
2813 IrTcl_ScanObj *obj = o;
2814 IrTcl_Obj *p = obj->parent;
2817 struct ccl_rpn_node *rpn;
2825 interp->result = "wrong # args";
2828 if (!p->set_inher.num_databaseNames)
2830 interp->result = "no databaseNames";
2835 interp->result = "scan: not connected";
2839 bib1.proto = p->protocol_type;
2840 bib1.oclass = CLASS_ATTSET;
2841 bib1.value = VAL_BIB1;
2843 apdu = zget_APDU (p->odr_out, Z_APDU_scanRequest);
2844 req = apdu->u.scanRequest;
2846 set_referenceId (p->odr_out, &req->referenceId, p->set_inher.referenceId);
2847 req->num_databaseNames = p->set_inher.num_databaseNames;
2848 req->databaseNames = p->set_inher.databaseNames;
2849 req->attributeSet = oid_getoidbyent (&bib1);
2852 if (!(req->termListAndStartPoint = p_query_scan (p->odr_out, argv[2])))
2854 Tcl_AppendResult (interp, "Syntax error in query", NULL);
2858 rpn = ccl_find_str(p->bibset, argv[2], &r, &pos);
2861 Tcl_AppendResult (interp, "CCL error: ", ccl_err_msg (r), NULL);
2864 ccl_pr_tree (rpn, stderr);
2865 fprintf (stderr, "\n");
2866 if (!(req->termListAndStartPoint = ccl_scan_query (rpn)))
2869 req->stepSize = &obj->stepSize;
2870 req->numberOfTermsRequested = &obj->numberOfTermsRequested;
2871 req->preferredPositionInResponse = &obj->preferredPositionInResponse;
2872 logf (LOG_DEBUG, "stepSize=%d", *req->stepSize);
2873 logf (LOG_DEBUG, "numberOfTermsRequested=%d",
2874 *req->numberOfTermsRequested);
2875 logf (LOG_DEBUG, "preferredPositionInResponse=%d",
2876 *req->preferredPositionInResponse);
2878 return ir_tcl_send_APDU (interp, p, apdu, "scan", *argv);
2882 * do_scanResponse: add scan response handler
2884 static int do_scanResponse (void *o, Tcl_Interp *interp,
2885 int argc, char **argv)
2887 IrTcl_ScanObj *obj = o;
2891 obj->scanResponse = NULL;
2894 else if (argc == -1)
2895 return ir_tcl_strdel (interp, &obj->scanResponse);
2898 free (obj->scanResponse);
2901 if (ir_tcl_strdup (interp, &obj->scanResponse, argv[2])
2906 obj->scanResponse = NULL;
2912 * do_stepSize: Set/get replace Step Size
2914 static int do_stepSize (void *obj, Tcl_Interp *interp,
2915 int argc, char **argv)
2917 IrTcl_ScanObj *p = obj;
2923 return ir_tcl_get_set_int (&p->stepSize, interp, argc, argv);
2927 * do_numberOfTermsRequested: Set/get Number of Terms requested
2929 static int do_numberOfTermsRequested (void *obj, Tcl_Interp *interp,
2930 int argc, char **argv)
2932 IrTcl_ScanObj *p = obj;
2936 p->numberOfTermsRequested = 20;
2939 return ir_tcl_get_set_int (&p->numberOfTermsRequested, interp, argc, argv);
2944 * do_preferredPositionInResponse: Set/get preferred Position
2946 static int do_preferredPositionInResponse (void *obj, Tcl_Interp *interp,
2947 int argc, char **argv)
2949 IrTcl_ScanObj *p = obj;
2953 p->preferredPositionInResponse = 1;
2956 return ir_tcl_get_set_int (&p->preferredPositionInResponse, interp,
2961 * do_scanStatus: Get scan status
2963 static int do_scanStatus (void *obj, Tcl_Interp *interp,
2964 int argc, char **argv)
2966 IrTcl_ScanObj *p = obj;
2970 return ir_tcl_get_set_int (&p->scanStatus, interp, argc, argv);
2974 * do_numberOfEntriesReturned: Get number of Entries returned
2976 static int do_numberOfEntriesReturned (void *obj, Tcl_Interp *interp,
2977 int argc, char **argv)
2979 IrTcl_ScanObj *p = obj;
2983 return ir_tcl_get_set_int (&p->numberOfEntriesReturned, interp,
2988 * do_positionOfTerm: Get position of Term
2990 static int do_positionOfTerm (void *obj, Tcl_Interp *interp,
2991 int argc, char **argv)
2993 IrTcl_ScanObj *p = obj;
2997 return ir_tcl_get_set_int (&p->positionOfTerm, interp, argc, argv);
3001 * do_scanLine: get Scan Line (surrogate or normal) after response
3003 static int do_scanLine (void *obj, Tcl_Interp *interp, int argc, char **argv)
3005 IrTcl_ScanObj *p = obj;
3011 p->entries_flag = 0;
3013 p->nonSurrogateDiagnosticNum = 0;
3014 p->nonSurrogateDiagnosticList = 0;
3017 else if (argc == -1)
3019 p->entries_flag = 0;
3020 /* release entries */
3023 ir_deleteDiags (&p->nonSurrogateDiagnosticList,
3024 &p->nonSurrogateDiagnosticNum);
3029 interp->result = "wrong # args";
3032 if (Tcl_GetInt (interp, argv[2], &i) == TCL_ERROR)
3034 if (!p->entries_flag || p->which != Z_ListEntries_entries || !p->entries
3035 || i >= p->num_entries || i < 0)
3037 switch (p->entries[i].which)
3039 case Z_Entry_termInfo:
3040 Tcl_AppendElement (interp, "T");
3041 if (p->entries[i].u.term.buf)
3042 Tcl_AppendElement (interp, p->entries[i].u.term.buf);
3044 Tcl_AppendElement (interp, "");
3045 sprintf (numstr, "%d", p->entries[i].u.term.globalOccurrences);
3046 Tcl_AppendElement (interp, numstr);
3048 case Z_Entry_surrogateDiagnostic:
3049 Tcl_AppendElement (interp, "SD");
3050 return ir_diagResult (interp, p->entries[i].u.diag.list,
3051 p->entries[i].u.diag.num);
3057 static IrTcl_Method ir_scan_method_tab[] = {
3058 { "scan", do_scan, NULL},
3059 { "scanResponse", do_scanResponse, NULL},
3060 { "stepSize", do_stepSize, NULL},
3061 { "numberOfTermsRequested", do_numberOfTermsRequested, NULL},
3062 { "preferredPositionInResponse", do_preferredPositionInResponse, NULL},
3063 { "scanStatus", do_scanStatus, NULL},
3064 { "numberOfEntriesReturned", do_numberOfEntriesReturned, NULL},
3065 { "positionOfTerm", do_positionOfTerm, NULL},
3066 { "scanLine", do_scanLine, NULL},
3071 * ir_scan_obj_method: IR Scan Object methods
3073 static int ir_scan_obj_method (ClientData clientData, Tcl_Interp *interp,
3074 int argc, char **argv)
3076 IrTcl_Methods tabs[2];
3081 interp->result = "wrong # args";
3084 tabs[0].tab = ir_scan_method_tab;
3085 tabs[0].obj = clientData;
3088 ir_tcl_method (interp, argc, argv, tabs, &r);
3093 * ir_scan_obj_delete: IR Scan Object disposal
3095 static void ir_scan_obj_delete (ClientData clientData)
3097 IrTcl_Methods tabs[2];
3098 IrTcl_ScanObj *obj = clientData;
3100 tabs[0].tab = ir_scan_method_tab;
3104 ir_tcl_method (NULL, -1, NULL, tabs, NULL);
3109 * ir_scan_obj_mk: IR Scan Object creation
3111 static int ir_scan_obj_mk (ClientData clientData, Tcl_Interp *interp,
3112 int argc, char **argv)
3114 Tcl_CmdInfo parent_info;
3116 IrTcl_Methods tabs[2];
3120 interp->result = "wrong # args";
3123 if (!Tcl_GetCommandInfo (interp, argv[2], &parent_info))
3125 interp->result = "No parent";
3128 obj = ir_tcl_malloc (sizeof(*obj));
3129 obj->parent = (IrTcl_Obj *) parent_info.clientData;
3131 tabs[0].tab = ir_scan_method_tab;
3135 if (ir_tcl_method (interp, 0, NULL, tabs, NULL) == TCL_ERROR)
3137 Tcl_CreateCommand (interp, argv[1], ir_scan_obj_method,
3138 (ClientData) obj, ir_scan_obj_delete);
3142 /* ------------------------------------------------------- */
3144 static void ir_initResponse (void *obj, Z_InitResponse *initrs)
3148 p->initResult = *initrs->result ? 1 : 0;
3149 if (!*initrs->result)
3150 logf (LOG_DEBUG, "Connection rejected by target");
3152 logf (LOG_DEBUG, "Connection accepted by target");
3154 get_referenceId (&p->set_inher.referenceId, initrs->referenceId);
3156 free (p->targetImplementationId);
3157 ir_tcl_strdup (p->interp, &p->targetImplementationId,
3158 initrs->implementationId);
3159 free (p->targetImplementationName);
3160 ir_tcl_strdup (p->interp, &p->targetImplementationName,
3161 initrs->implementationName);
3162 free (p->targetImplementationVersion);
3163 ir_tcl_strdup (p->interp, &p->targetImplementationVersion,
3164 initrs->implementationVersion);
3166 p->maximumRecordSize = *initrs->maximumRecordSize;
3167 p->preferredMessageSize = *initrs->preferredMessageSize;
3169 memcpy (&p->options, initrs->options, sizeof(initrs->options));
3170 memcpy (&p->protocolVersion, initrs->protocolVersion,
3171 sizeof(initrs->protocolVersion));
3172 free (p->userInformationField);
3173 p->userInformationField = NULL;
3174 if (initrs->userInformationField)
3178 if (initrs->userInformationField->which == ODR_EXTERNAL_octet &&
3179 (p->userInformationField =
3180 ir_tcl_malloc ((len =
3181 initrs->userInformationField->
3182 u.octet_aligned->len) +1)))
3184 memcpy (p->userInformationField,
3185 initrs->userInformationField->u.octet_aligned->buf,
3187 (p->userInformationField)[len] = '\0';
3192 static void ir_deleteDiags (IrTcl_Diagnostic **dst_list, int *dst_num)
3195 for (i = 0; i<*dst_num; i++)
3196 free (dst_list[i]->addinfo);
3202 static void ir_handleDiags (IrTcl_Diagnostic **dst_list, int *dst_num,
3203 Z_DiagRec **list, int num)
3209 *dst_list = ir_tcl_malloc (sizeof(**dst_list) * num);
3210 for (i = 0; i<num; i++)
3212 switch (list[i]->which)
3214 case Z_DiagRec_defaultFormat:
3215 (*dst_list)[i].condition = *list[i]->u.defaultFormat->condition;
3216 addinfo = list[i]->u.defaultFormat->addinfo;
3218 ((*dst_list)[i].addinfo = ir_tcl_malloc (strlen(addinfo)+1)))
3219 strcpy ((*dst_list)[i].addinfo, addinfo);
3222 (*dst_list)[i].addinfo = NULL;
3223 (*dst_list)[i].condition = 0;
3228 static void ir_handleRecords (void *o, Z_Records *zrs, IrTcl_SetObj *setobj,
3229 const char *elements)
3234 IrTcl_RecordList *rl;
3236 setobj->which = zrs->which;
3237 setobj->recordFlag = 1;
3239 ir_deleteDiags (&setobj->nonSurrogateDiagnosticList,
3240 &setobj->nonSurrogateDiagnosticNum);
3241 if (zrs->which == Z_Records_DBOSD)
3243 setobj->numberOfRecordsReturned =
3244 zrs->u.databaseOrSurDiagnostics->num_records;
3245 logf (LOG_DEBUG, "Got %d records", setobj->numberOfRecordsReturned);
3246 for (offset = 0; offset<setobj->numberOfRecordsReturned; offset++)
3248 rl = new_IR_record (setobj, setobj->start + offset,
3249 zrs->u.databaseOrSurDiagnostics->
3250 records[offset]->which,
3252 if (rl->which == Z_NamePlusRecord_surrogateDiagnostic)
3254 ir_handleDiags (&rl->u.surrogateDiagnostics.list,
3255 &rl->u.surrogateDiagnostics.num,
3256 &zrs->u.databaseOrSurDiagnostics->
3257 records[offset]->u.surrogateDiagnostic,
3262 Z_DatabaseRecord *zr;
3264 struct oident *ident;
3266 zr = zrs->u.databaseOrSurDiagnostics->records[offset]
3268 oe = (Z_External*) zr;
3269 rl->u.dbrec.size = zr->u.octet_aligned->len;
3271 if ((ident = oid_getentbyoid (oe->direct_reference)))
3272 rl->u.dbrec.type = ident->value;
3274 rl->u.dbrec.type = VAL_USMARC;
3276 if (oe->which == ODR_EXTERNAL_octet && rl->u.dbrec.size > 0)
3278 char *buf = (char*) zr->u.octet_aligned->buf;
3279 if ((rl->u.dbrec.buf = ir_tcl_malloc (rl->u.dbrec.size)))
3280 memcpy (rl->u.dbrec.buf, buf, rl->u.dbrec.size);
3282 else if (rl->u.dbrec.type == VAL_SUTRS &&
3283 oe->which == Z_External_sutrs)
3285 odr_setbuf (p->odr_in, (char*) oe->u.single_ASN1_type->buf,
3286 oe->u.single_ASN1_type->len, 0);
3287 if ((rl->u.dbrec.buf = ir_tcl_malloc (oe->u.sutrs->len+1)))
3289 memcpy (rl->u.dbrec.buf, oe->u.sutrs->buf,
3291 rl->u.dbrec.buf[oe->u.sutrs->len] = '\0';
3293 rl->u.dbrec.size = oe->u.sutrs->len;
3295 else if (rl->u.dbrec.type == VAL_GRS1 &&
3296 oe->which == Z_External_grs1)
3298 ir_tcl_grs_mk (oe->u.grs1, &rl->u.dbrec.u.grs1);
3299 rl->u.dbrec.buf = NULL;
3302 rl->u.dbrec.buf = NULL;
3306 else if (zrs->which == Z_Records_multipleNSD)
3308 logf (LOG_DEBUG, "multipleNonSurrogateDiagnostic %d",
3309 zrs->u.multipleNonSurDiagnostics->num_diagRecs);
3310 setobj->numberOfRecordsReturned = 0;
3311 ir_handleDiags (&setobj->nonSurrogateDiagnosticList,
3312 &setobj->nonSurrogateDiagnosticNum,
3313 zrs->u.multipleNonSurDiagnostics->diagRecs,
3314 zrs->u.multipleNonSurDiagnostics->num_diagRecs);
3318 logf (LOG_DEBUG, "NonSurrogateDiagnostic");
3319 setobj->numberOfRecordsReturned = 0;
3320 ir_handleDiags (&setobj->nonSurrogateDiagnosticList,
3321 &setobj->nonSurrogateDiagnosticNum,
3322 &zrs->u.nonSurrogateDiagnostic,
3327 static void ir_searchResponse (void *o, Z_SearchResponse *searchrs,
3328 IrTcl_SetObj *setobj)
3330 Z_Records *zrs = searchrs->records;
3332 logf (LOG_DEBUG, "Received search response");
3335 logf (LOG_DEBUG, "Search response, no object!");
3338 setobj->searchStatus = searchrs->searchStatus ? 1 : 0;
3339 get_referenceId (&setobj->set_inher.referenceId, searchrs->referenceId);
3340 setobj->resultCount = *searchrs->resultCount;
3341 if (searchrs->presentStatus)
3342 setobj->presentStatus = *searchrs->presentStatus;
3343 if (searchrs->nextResultSetPosition)
3344 setobj->nextResultSetPosition = *searchrs->nextResultSetPosition;
3346 logf (LOG_DEBUG, "Search response %d, %d hits",
3347 setobj->searchStatus, setobj->resultCount);
3351 if (setobj->resultCount <= setobj->set_inher.smallSetUpperBound)
3352 es = setobj->set_inher.smallSetElementSetNames;
3354 es = setobj->set_inher.mediumSetElementSetNames;
3355 ir_handleRecords (o, zrs, setobj, es);
3358 setobj->recordFlag = 0;
3362 static void ir_presentResponse (void *o, Z_PresentResponse *presrs,
3363 IrTcl_SetObj *setobj)
3365 Z_Records *zrs = presrs->records;
3367 logf (LOG_DEBUG, "Received present response");
3370 logf (LOG_DEBUG, "Present response, no object!");
3373 setobj->presentStatus = *presrs->presentStatus;
3374 get_referenceId (&setobj->set_inher.referenceId, presrs->referenceId);
3375 setobj->nextResultSetPosition = *presrs->nextResultSetPosition;
3377 ir_handleRecords (o, zrs, setobj, setobj->set_inher.elementSetNames);
3380 setobj->recordFlag = 0;
3381 logf (LOG_DEBUG, "No records!");
3385 static void ir_scanResponse (void *o, Z_ScanResponse *scanrs,
3386 IrTcl_ScanObj *scanobj)
3390 logf (LOG_DEBUG, "Received scanResponse");
3392 get_referenceId (&p->set_inher.referenceId, scanrs->referenceId);
3393 scanobj->scanStatus = *scanrs->scanStatus;
3394 logf (LOG_DEBUG, "scanStatus=%d", scanobj->scanStatus);
3396 if (scanrs->stepSize)
3397 scanobj->stepSize = *scanrs->stepSize;
3398 logf (LOG_DEBUG, "stepSize=%d", scanobj->stepSize);
3400 scanobj->numberOfEntriesReturned = *scanrs->numberOfEntriesReturned;
3401 logf (LOG_DEBUG, "numberOfEntriesReturned=%d",
3402 scanobj->numberOfEntriesReturned);
3404 if (scanrs->positionOfTerm)
3405 scanobj->positionOfTerm = *scanrs->positionOfTerm;
3407 scanobj->positionOfTerm = -1;
3408 logf (LOG_DEBUG, "positionOfTerm=%d", scanobj->positionOfTerm);
3410 free (scanobj->entries);
3411 scanobj->entries = NULL;
3413 ir_deleteDiags (&scanobj->nonSurrogateDiagnosticList,
3414 &scanobj->nonSurrogateDiagnosticNum);
3415 if (scanrs->entries)
3420 scanobj->entries_flag = 1;
3421 scanobj->which = scanrs->entries->which;
3422 switch (scanobj->which)
3424 case Z_ListEntries_entries:
3425 scanobj->num_entries = scanrs->entries->u.entries->num_entries;
3426 scanobj->entries = ir_tcl_malloc (scanobj->num_entries *
3427 sizeof(*scanobj->entries));
3428 for (i=0; i<scanobj->num_entries; i++)
3430 ze = scanrs->entries->u.entries->entries[i];
3431 scanobj->entries[i].which = ze->which;
3434 case Z_Entry_termInfo:
3435 if (ze->u.termInfo->term->which == Z_Term_general)
3437 int l = ze->u.termInfo->term->u.general->len;
3438 scanobj->entries[i].u.term.buf = ir_tcl_malloc (1+l);
3439 memcpy (scanobj->entries[i].u.term.buf,
3440 ze->u.termInfo->term->u.general->buf,
3442 scanobj->entries[i].u.term.buf[l] = '\0';
3445 scanobj->entries[i].u.term.buf = NULL;
3446 if (ze->u.termInfo->globalOccurrences)
3447 scanobj->entries[i].u.term.globalOccurrences =
3448 *ze->u.termInfo->globalOccurrences;
3450 scanobj->entries[i].u.term.globalOccurrences = 0;
3452 case Z_Entry_surrogateDiagnostic:
3453 ir_handleDiags (&scanobj->entries[i].u.diag.list,
3454 &scanobj->entries[i].u.diag.num,
3455 &ze->u.surrogateDiagnostic,
3461 case Z_ListEntries_nonSurrogateDiagnostics:
3462 ir_handleDiags (&scanobj->nonSurrogateDiagnosticList,
3463 &scanobj->nonSurrogateDiagnosticNum,
3464 scanrs->entries->u.nonSurrogateDiagnostics->
3466 scanrs->entries->u.nonSurrogateDiagnostics->
3472 scanobj->entries_flag = 0;
3476 * ir_select_read: handle incoming packages
3478 static void ir_select_read (ClientData clientData)
3480 IrTcl_Obj *p = clientData;
3485 Tcl_CmdInfo cmd_info;
3486 const char *apdu_call;
3488 logf(LOG_DEBUG, "Read handler fd=%d", cs_fileno(p->cs_link));
3489 if (p->state == IR_TCL_R_Connecting)
3491 logf(LOG_DEBUG, "read: connect");
3492 r = cs_rcvconnect (p->cs_link);
3495 logf (LOG_WARN, "cs_rcvconnect returned 1");
3498 p->state = IR_TCL_R_Idle;
3500 #if IRTCL_GENERIC_FILES
3501 ir_select_remove_write (p->csFile, p);
3503 ir_select_remove_write (cs_fileno (p->cs_link), p);
3507 logf (LOG_DEBUG, "cs_rcvconnect error");
3508 ir_tcl_disconnect (p);
3511 p->failInfo = IR_TCL_FAIL_CONNECT;
3512 ir_tcl_eval (p->interp, p->failback);
3518 ir_tcl_eval (p->interp, p->callback);
3519 if (p->ref_count == 2 && p->cs_link && p->request_queue
3520 && p->state == IR_TCL_R_Idle)
3521 ir_tcl_send_q (p, p->request_queue, "x");
3527 p->state = IR_TCL_R_Reading;
3529 /* read incoming APDU */
3530 if ((r=cs_get (p->cs_link, &p->buf_in, &p->len_in)) == 1)
3532 logf(LOG_DEBUG, "PDU Fraction read");
3535 /* signal one more use of ir object - callbacks must not
3536 release the ir memory (p pointer) */
3540 logf (LOG_DEBUG, "cs_get failed, code %d", r);
3541 #if IRTCL_GENERIC_FILES
3542 ir_select_remove (p->csFile, p);
3544 ir_select_remove (cs_fileno (p->cs_link), p);
3546 ir_tcl_disconnect (p);
3549 p->failInfo = IR_TCL_FAIL_READ;
3550 ir_tcl_eval (p->interp, p->failback);
3552 /* release ir object now if callback deleted it */
3556 /* got complete APDU. Now decode */
3559 odr_setbuf (p->odr_in, p->buf_in, r, 0);
3560 logf (LOG_DEBUG, "cs_get ok, total size %d", r);
3561 if (!z_APDU (p->odr_in, &apdu, 0))
3563 logf (LOG_DEBUG, "cs_get failed: %s",
3564 odr_errmsg (odr_geterror (p->odr_in)));
3565 ir_tcl_disconnect (p);
3568 p->failInfo = IR_TCL_FAIL_IN_APDU;
3569 p->apduOffset = odr_offset (p->odr_in);
3570 ir_tcl_eval (p->interp, p->failback);
3572 /* release ir object now if failback deleted it */
3576 logf(LOG_DEBUG, "Decoded ok");
3577 /* handle APDU and invoke callback */
3578 rq = p->request_queue;
3581 logf (LOG_FATAL, "Internal error. No queue entry");
3584 object_name = rq->object_name;
3585 logf (LOG_DEBUG, "getCommandInfo (%s)", object_name);
3587 if (Tcl_GetCommandInfo (p->interp, object_name, &cmd_info))
3591 case Z_APDU_initResponse:
3592 p->eventType = "init";
3593 ir_initResponse (p, apdu->u.initResponse);
3594 apdu_call = p->initResponse;
3596 case Z_APDU_searchResponse:
3597 p->eventType = "search";
3598 ir_searchResponse (p, apdu->u.searchResponse,
3599 (IrTcl_SetObj *) cmd_info.clientData);
3600 apdu_call = ((IrTcl_SetObj *)
3601 cmd_info.clientData)->searchResponse;
3603 case Z_APDU_presentResponse:
3604 p->eventType = "present";
3605 ir_presentResponse (p, apdu->u.presentResponse,
3606 (IrTcl_SetObj *) cmd_info.clientData);
3607 apdu_call = ((IrTcl_SetObj *)
3608 cmd_info.clientData)->presentResponse;
3610 case Z_APDU_scanResponse:
3611 p->eventType = "scan";
3612 ir_scanResponse (p, apdu->u.scanResponse,
3613 (IrTcl_ScanObj *) cmd_info.clientData);
3614 apdu_call = ((IrTcl_ScanObj *)
3615 cmd_info.clientData)->scanResponse;
3618 logf (LOG_WARN, "Received unknown APDU type (%d)",
3620 ir_tcl_disconnect (p);
3623 p->failInfo = IR_TCL_FAIL_UNKNOWN_APDU;
3624 ir_tcl_eval (p->interp, p->failback);
3629 p->request_queue = rq->next;
3630 p->state = IR_TCL_R_Idle;
3633 ir_tcl_eval (p->interp, apdu_call);
3634 else if (rq->callback)
3635 ir_tcl_eval (p->interp, rq->callback);
3637 free (rq->callback);
3638 free (rq->object_name);
3640 odr_reset (p->odr_in);
3641 if (p->ref_count == 1)
3647 } while (p->cs_link && cs_more (p->cs_link));
3648 if (p->cs_link && p->request_queue && p->state == IR_TCL_R_Idle)
3649 ir_tcl_send_q (p, p->request_queue, "x");
3653 * ir_select_write: handle outgoing packages - not yet written.
3655 static void ir_select_write (ClientData clientData)
3657 IrTcl_Obj *p = clientData;
3661 logf (LOG_DEBUG, "Write handler fd=%d", cs_fileno(p->cs_link));
3662 if (p->state == IR_TCL_R_Connecting)
3664 logf(LOG_DEBUG, "write: connect");
3665 r = cs_rcvconnect (p->cs_link);
3668 logf (LOG_DEBUG, "cs_rcvconnect returned 1");
3671 p->state = IR_TCL_R_Idle;
3673 #if IRTCL_GENERIC_FILES
3674 ir_select_remove_write (p->csFile, p);
3676 ir_select_remove_write (cs_fileno (p->cs_link), p);
3680 logf (LOG_DEBUG, "cs_rcvconnect error");
3681 ir_tcl_disconnect (p);
3684 p->failInfo = IR_TCL_FAIL_CONNECT;
3685 ir_tcl_eval (p->interp, p->failback);
3691 ir_tcl_eval (p->interp, p->callback);
3695 rq = p->request_queue;
3696 if (!rq || !rq->buf_out)
3699 if ((r=cs_put (p->cs_link, rq->buf_out, rq->len_out)) < 0)
3701 logf (LOG_DEBUG, "cs_put write fail");
3705 ir_tcl_disconnect (p);
3708 p->failInfo = IR_TCL_FAIL_WRITE;
3709 ir_tcl_eval (p->interp, p->failback);
3713 else if (r == 0) /* remove select bit */
3715 logf (LOG_DEBUG, "Write completed");
3716 p->state = IR_TCL_R_Waiting;
3717 #if IRTCL_GENERIC_FILES
3718 ir_select_remove_write (p->csFile, p);
3720 ir_select_remove_write (cs_fileno (p->cs_link), p);
3727 static void ir_select_notify (ClientData clientData, int r, int w, int e)
3730 ir_select_read (clientData);
3732 ir_select_write (clientData);
3735 /* ------------------------------------------------------- */
3738 * Irtcl_init: Registration of TCL commands.
3740 int Irtcl_Init (Tcl_Interp *interp)
3742 Tcl_CreateCommand (interp, "ir", ir_obj_mk, (ClientData) NULL,
3743 (Tcl_CmdDeleteProc *) NULL);
3744 Tcl_CreateCommand (interp, "ir-set", ir_set_obj_mk,
3745 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
3746 Tcl_CreateCommand (interp, "ir-scan", ir_scan_obj_mk,
3747 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);