2 * IR toolkit for tcl/tk
4 * See the file LICENSE for details.
5 * Sebastian Hammer, Adam Dickmeiss
8 * Revision 1.88 1996-06-03 09:04:22 adam
9 * Changed a few logf calls.
11 * Revision 1.87 1996/05/29 06:37:51 adam
12 * Function ir_tcl_get_grs_r enhanced so that specific elements can be
15 * Revision 1.86 1996/03/20 13:54:04 adam
16 * The Tcl_File structure is only manipulated in the Tk-event interface
19 * Revision 1.85 1996/03/15 11:15:48 adam
20 * Modified to use new prototypes for p_query_rpn and p_query_scan.
22 * Revision 1.84 1996/03/07 12:42:49 adam
23 * Better logging when callback is invoked.
25 * Revision 1.83 1996/03/05 09:21:09 adam
26 * Bug fix: memory used by GRS records wasn't freed.
27 * Rewrote some of the error handling code - the connection is always
28 * closed before failback is called.
29 * If failback is defined the send APDU methods (init, search, ...) will
30 * return OK but invoke failback (as is the case if the write operation
32 * Bug fix: ref_count in assoc object could grow if fraction of PDU was
35 * Revision 1.82 1996/02/29 15:30:21 adam
36 * Export of IrTcl functionality to extensions.
38 * Revision 1.81 1996/02/26 18:38:32 adam
39 * Work on export of set methods.
41 * Revision 1.80 1996/02/23 17:31:39 adam
42 * More functions made available to the wais tcl extension.
44 * Revision 1.79 1996/02/23 13:41:38 adam
45 * Work on public access to simple ir class system.
47 * Revision 1.78 1996/02/21 10:16:08 adam
48 * Simplified select handling. Only one function ir_tcl_select_set has
49 * to be externally defined.
51 * Revision 1.77 1996/02/20 17:52:58 adam
52 * Uses the YAZ oid system to name record syntax object identifiers.
54 * Revision 1.76 1996/02/20 16:09:51 adam
55 * Bug fix: didn't set element set names stamp correctly on result
56 * set records when element set names were set to the empty string.
58 * Revision 1.75 1996/02/19 15:41:53 adam
59 * Better log messages.
60 * Minor improvement of connect method.
62 * Revision 1.74 1996/02/05 17:58:03 adam
63 * Ported ir-tcl to use the beta releases of tcl7.5/tk4.1.
65 * Revision 1.73 1996/01/29 11:35:19 adam
66 * Bug fix: cs_type member renamed to comstackType to avoid conflict with
67 * cs_type macro defined by YAZ.
69 * Revision 1.72 1996/01/19 17:45:34 quinn
70 * Added debugging output
72 * Revision 1.71 1996/01/19 16:22:38 adam
73 * New method: apduDump - returns information about last incoming APDU.
75 * Revision 1.70 1996/01/10 09:18:34 adam
76 * PDU specific callbacks implemented: initRespnse, searchResponse,
77 * presentResponse and scanResponse.
78 * Bug fix in the command line shell (tclmain.c) - discovered on OSF/1.
80 * Revision 1.69 1996/01/04 16:12:12 adam
81 * Setting PDUType renamed to eventType.
83 * Revision 1.68 1996/01/04 11:05:22 adam
84 * New setting: PDUType - returns type of last PDU returned from the target.
85 * Fixed a bug in configure/Makefile.
87 * Revision 1.67 1996/01/03 09:00:51 adam
88 * Updated to use new version of Yaz (names changed to avoid C++ conflict).
90 * Revision 1.66 1995/11/28 17:26:39 adam
91 * Removed Carriage return from ir-tcl.c!
92 * Removed misc. debug logs.
94 * Revision 1.65 1995/11/28 13:53:00 quinn
97 * Revision 1.64 1995/11/13 15:39:18 adam
98 * Bug fix: {small,medium}SetElementSetNames weren't set correctly.
99 * Bug fix: idAuthentication weren't set correctly.
101 * Revision 1.63 1995/11/13 09:55:39 adam
102 * Multiple records at a position in a result-set with differnt
105 * Revision 1.62 1995/10/18 17:20:33 adam
106 * Work on target setup in client.tcl.
108 * Revision 1.61 1995/10/18 16:42:42 adam
109 * New settings: smallSetElementSetNames and mediumSetElementSetNames.
111 * Revision 1.60 1995/10/18 15:43:31 adam
112 * In search: mediumSetElementSetNames and smallSetElementSetNames are
113 * set to elementSetNames.
115 * Revision 1.59 1995/10/17 12:18:58 adam
116 * Bug fix: when target connection closed, the connection was not
117 * properly reestablished.
119 * Revision 1.58 1995/10/16 17:00:55 adam
120 * New setting: elementSetNames.
121 * Various client improvements. Medium presentation format looks better.
123 * Revision 1.57 1995/09/21 13:11:51 adam
124 * Support of dynamic loading.
125 * Test script uses load command if necessary.
127 * Revision 1.56 1995/08/29 15:30:14 adam
128 * Work on GRS records.
130 * Revision 1.55 1995/08/28 09:43:25 adam
131 * Minor changes. configure only searches for yaz beta 3 and versions after
134 * Revision 1.54 1995/08/24 12:25:16 adam
135 * Modified to work with yaz 1.0b3.
137 * Revision 1.53 1995/08/04 12:49:26 adam
138 * Bug fix: reading uninitialized variable p.
140 * Revision 1.52 1995/08/04 11:32:38 adam
141 * More work on output queue. Memory related routines moved
144 * Revision 1.51 1995/08/03 13:22:54 adam
147 * Revision 1.50 1995/07/20 08:09:49 adam
148 * client.tcl: Targets removed from hotTargets list when targets
149 * are removed/modified.
150 * ir-tcl.c: More work on triggerResourceControl.
152 * Revision 1.49 1995/06/30 12:39:21 adam
153 * Bug fix: loadFile didn't set record type.
154 * The MARC routines are a little less strict in the interpretation.
155 * Script display.tcl replaces the old marc.tcl.
156 * New interactive script: shell.tcl.
158 * Revision 1.48 1995/06/27 19:03:50 adam
159 * Bug fix in do_present in ir-tcl.c: p->set_child member weren't set.
160 * nextResultSetPosition used instead of setOffset.
162 * Revision 1.47 1995/06/25 10:25:04 adam
163 * Working on triggerResourceControl. Description of compile/install
164 * procedure moved to ir-tcl.sgml.
166 * Revision 1.46 1995/06/22 13:15:06 adam
167 * Feature: SUTRS. Setting getSutrs implemented.
168 * Work on display formats.
169 * Preferred record syntax can be set by the user.
171 * Revision 1.45 1995/06/20 08:07:30 adam
172 * New setting: failInfo.
173 * Working on better cancel mechanism.
175 * Revision 1.44 1995/06/19 17:01:20 adam
178 * Revision 1.43 1995/06/19 13:06:08 adam
179 * New define: IR_TCL_VERSION.
181 * Revision 1.42 1995/06/19 08:08:52 adam
182 * client.tcl: hotTargets now contain both database and target name.
183 * ir-tcl.c: setting protocol edited. Errors in callbacks are logged
184 * by logf(LOG_WARN, ...) calls.
186 * Revision 1.41 1995/06/16 12:28:16 adam
187 * Implemented preferredRecordSyntax.
188 * Minor changes in diagnostic handling.
189 * Record list deleted when connection closes.
191 * Revision 1.40 1995/06/14 13:37:18 adam
192 * Setting recordType implemented.
193 * Setting implementationVersion implemented.
194 * Settings implementationId / implementationName edited.
196 * Revision 1.39 1995/06/08 10:26:32 adam
197 * Bug fix in ir_strdup.
199 * Revision 1.38 1995/06/01 16:36:47 adam
200 * About buttons. Minor bug fixes.
202 * Revision 1.37 1995/06/01 07:31:20 adam
203 * Rename of many typedefs -> IrTcl_...
205 * Revision 1.36 1995/05/31 13:09:59 adam
206 * Client searches/presents may be interrupted.
207 * New moving book-logo.
209 * Revision 1.35 1995/05/31 08:36:33 adam
210 * Bug fix in client.tcl: didn't save options on clientrc.tcl.
211 * New method: referenceId. More work on scan.
213 * Revision 1.34 1995/05/29 10:33:42 adam
214 * README and rename of startup script.
216 * Revision 1.33 1995/05/29 09:15:11 quinn
217 * Changed CS_SR to PROTO_SR, etc.
219 * Revision 1.32 1995/05/29 08:44:16 adam
220 * Work on delete of objects.
222 * Revision 1.31 1995/05/26 11:44:10 adam
223 * Bugs fixed. More work on MARC utilities and queries. Test
224 * client is up-to-date again.
226 * Revision 1.30 1995/05/26 08:54:11 adam
227 * New MARC utilities. Uses prefix query.
229 * Revision 1.29 1995/05/24 14:10:22 adam
230 * Work on idAuthentication, protocolVersion and options.
232 * Revision 1.28 1995/05/23 15:34:48 adam
233 * Many new settings, userInformationField, smallSetUpperBound, etc.
234 * A number of settings are inherited when ir-set is executed.
235 * This version is incompatible with the graphical test client (client.tcl).
237 * Revision 1.27 1995/05/11 15:34:47 adam
238 * Scan request changed a bit. This version works with RLG.
240 * Revision 1.26 1995/04/18 16:11:51 adam
241 * First version of graphical Scan. Some work on query-by-form.
243 * Revision 1.25 1995/04/17 09:37:17 adam
244 * Further development of scan.
246 * Revision 1.24 1995/04/11 14:16:42 adam
247 * Further work on scan. Response works. Entries aren't saved yet.
249 * Revision 1.23 1995/04/10 10:50:27 adam
250 * Result-set name defaults to suffix of ir-set name.
251 * Started working on scan. Not finished at this point.
253 * Revision 1.22 1995/03/31 10:43:03 adam
254 * More robust when getting bad MARC records.
256 * Revision 1.21 1995/03/31 08:56:37 adam
257 * New button "Search".
259 * Revision 1.20 1995/03/29 16:07:09 adam
260 * Bug fix: Didn't use setName in present request.
262 * Revision 1.19 1995/03/28 12:45:23 adam
263 * New ir method failback: called on disconnect/protocol error.
264 * New ir set/get method: protocol: SR / Z3950.
265 * Simple popup and disconnect when failback is invoked.
267 * Revision 1.18 1995/03/21 15:50:12 adam
270 * Revision 1.17 1995/03/21 13:41:03 adam
271 * Comstack cs_create not used too often. Non-blocking connect.
273 * Revision 1.16 1995/03/21 08:26:06 adam
274 * New method, setName, to specify the result set name (other than Default).
275 * New method, responseStatus, which returns diagnostic info, if any, after
276 * present response / search response.
278 * Revision 1.15 1995/03/20 15:24:07 adam
279 * Diagnostic records saved on searchResponse.
281 * Revision 1.14 1995/03/20 08:53:22 adam
282 * Event loop in tclmain.c rewritten. New method searchStatus.
284 * Revision 1.13 1995/03/17 18:26:17 adam
285 * Non-blocking i/o used now. Database names popup as cascade items.
287 * Revision 1.12 1995/03/17 15:45:00 adam
288 * Improved target/database setup.
290 * Revision 1.11 1995/03/16 17:54:03 adam
291 * Minor changes really.
293 * Revision 1.10 1995/03/15 16:14:50 adam
294 * Blocking arg in cs_create changed.
296 * Revision 1.9 1995/03/15 13:59:24 adam
299 * Revision 1.8 1995/03/15 08:25:16 adam
300 * New method presentStatus to check for error on present. Misc. cleanup
301 * of IrTcl_RecordList manipulations. Full MARC record presentation in
304 * Revision 1.7 1995/03/14 17:32:29 adam
305 * Presentation of full Marc record in popup window.
307 * Revision 1.6 1995/03/12 19:31:55 adam
308 * Pattern matching implemented when retrieving MARC records. More
309 * diagnostic functions.
311 * Revision 1.5 1995/03/10 18:00:15 adam
312 * Actual presentation in line-by-line format. RPN query support.
314 * Revision 1.4 1995/03/09 16:15:08 adam
315 * First presentRequest attempts. Hot-target list.
329 static void ir_deleteDiags (IrTcl_Diagnostic **dst_list, int *dst_num);
331 static void ir_select_notify (ClientData clientData, int r, int w, int e);
333 void ir_select_add (int fd, void *obj)
335 ir_tcl_select_set (ir_select_notify, fd, obj, 1, 0, 0);
338 void ir_select_add_write (int fd, void *obj)
340 ir_tcl_select_set (ir_select_notify, fd, obj, 1, 1, 0);
343 void ir_select_remove (int fd, void *obj)
345 ir_tcl_select_set (NULL, fd, obj, 0, 0, 0);
348 void ir_select_remove_write (int fd, void *obj)
350 ir_tcl_select_set (ir_select_notify, fd, obj, 1, 0, 0);
353 static void delete_IR_record (IrTcl_RecordList *rl)
357 case Z_NamePlusRecord_databaseRecord:
358 switch (rl->u.dbrec.type)
361 ir_tcl_grs_del (&rl->u.dbrec.u.grs1);
366 free (rl->u.dbrec.buf);
368 case Z_NamePlusRecord_surrogateDiagnostic:
369 ir_deleteDiags (&rl->u.surrogateDiagnostics.list,
370 &rl->u.surrogateDiagnostics.num);
376 static IrTcl_RecordList *new_IR_record (IrTcl_SetObj *setobj,
378 const char *elements)
380 IrTcl_RecordList *rl;
382 if (elements && !*elements)
384 for (rl = setobj->record_list; rl; rl = rl->next)
386 if (no == rl->no && (!rl->elements || !elements ||
387 !strcmp(elements, rl->elements)))
389 delete_IR_record (rl);
395 rl = ir_tcl_malloc (sizeof(*rl));
396 rl->next = setobj->record_list;
398 setobj->record_list = rl;
401 ir_tcl_strdup (NULL, &rl->elements, elements);
408 int ir_tcl_eval (Tcl_Interp *interp, const char *command)
410 char *tmp = ir_tcl_malloc (strlen(command)+1);
413 logf (LOG_DEBUG, "Invoking %.23s ...", command);
414 strcpy (tmp, command);
415 r = Tcl_Eval (interp, tmp);
418 logf (LOG_WARN, "Tcl error in line %d: %s", interp->errorLine,
421 Tcl_FreeResult (interp);
427 * IrTcl_getRecordSyntaxStr: Return record syntax name of object id
429 static char *IrTcl_getRecordSyntaxStr (enum oid_value value)
432 struct oident ent, *entp;
434 ent.proto = PROTO_Z3950;
435 ent.oclass = CLASS_RECSYN;
438 o = oid_getoidbyent (&ent);
439 entp = oid_getentbyoid (o);
447 * IrTcl_getRecordSyntaxVal: Return record syntax value of string
449 static enum oid_value IrTcl_getRecordSyntaxVal (const char *name)
451 return oid_getvalbyname (name);
454 static IrTcl_RecordList *find_IR_record (IrTcl_SetObj *setobj, int no)
456 IrTcl_RecordList *rl;
458 for (rl = setobj->record_list; rl; rl = rl->next)
460 (!setobj->recordElements || !rl->elements ||
461 !strcmp (setobj->recordElements, rl->elements)))
466 static void delete_IR_records (IrTcl_SetObj *setobj)
468 IrTcl_RecordList *rl, *rl1;
470 for (rl = setobj->record_list; rl; rl = rl1)
472 delete_IR_record (rl);
476 setobj->record_list = NULL;
480 * ir_tcl_get_set_int: Set/get integer value
482 int ir_tcl_get_set_int (int *val, Tcl_Interp *interp, int argc, char **argv)
488 if (Tcl_GetInt (interp, argv[2], val)==TCL_ERROR)
491 sprintf (buf, "%d", *val);
492 Tcl_AppendResult (interp, buf, NULL);
497 * ir_tcl_method: Search for method in table and invoke method handler
499 int ir_tcl_method (Tcl_Interp *interp, int argc, char **argv,
500 IrTcl_Methods *tab, int *ret)
502 IrTcl_Methods *tab_i = tab;
505 for (tab_i = tab; tab_i->tab; tab_i++)
506 for (t = tab_i->tab; t->name; t++)
509 if ((*t->method)(tab_i->obj, interp, argc, argv) == TCL_ERROR)
513 if (!strcmp (t->name, argv[1]))
515 *ret = (*t->method)(tab_i->obj, interp, argc, argv);
522 Tcl_AppendResult (interp, "Bad method: ", argv[1],
523 ". Possible methods:", NULL);
524 for (tab_i = tab; tab_i->tab; tab_i++)
525 for (t = tab_i->tab; t->name; t++)
526 Tcl_AppendResult (interp, " ", t->name, NULL);
533 * ir_tcl_named_bits: get/set named bits
535 int ir_tcl_named_bits (struct ir_named_entry *tab, Odr_bitmask *ob,
536 Tcl_Interp *interp, int argc, char **argv)
538 struct ir_named_entry *ti;
543 for (no = 0; no < argc; no++)
545 for (ti = tab; ti->name; ti++)
546 if (!strcmp (argv[no], ti->name))
548 ODR_MASK_SET (ob, ti->pos);
553 Tcl_AppendResult (interp, "Bad bit mask: ", argv[no], NULL);
559 for (ti = tab; ti->name; ti++)
560 if (ODR_MASK_GET (ob, ti->pos))
561 Tcl_AppendElement (interp, ti->name);
565 static void set_referenceId (ODR o, Z_ReferenceId **dst, const char *src)
571 *dst = odr_malloc (o, sizeof(**dst));
572 (*dst)->size = (*dst)->len = strlen(src);
573 (*dst)->buf = odr_malloc (o, (*dst)->len);
574 memcpy ((*dst)->buf, src, (*dst)->len);
578 static void get_referenceId (char **dst, Z_ReferenceId *src)
586 *dst = ir_tcl_malloc (src->len+1);
587 memcpy (*dst, src->buf, src->len);
588 (*dst)[src->len] = '\0';
591 /* ------------------------------------------------------- */
594 * do_init_request: init method on IR object
596 static int do_init_request (void *obj, Tcl_Interp *interp,
597 int argc, char **argv)
605 logf (LOG_DEBUG, "init %s", *argv);
608 interp->result = "init: not connected";
611 apdu = zget_APDU (p->odr_out, Z_APDU_initRequest);
612 req = apdu->u.initRequest;
614 set_referenceId (p->odr_out, &req->referenceId, p->set_inher.referenceId);
615 req->options = &p->options;
616 req->protocolVersion = &p->protocolVersion;
617 req->preferredMessageSize = &p->preferredMessageSize;
618 req->maximumRecordSize = &p->maximumRecordSize;
620 if (p->idAuthenticationGroupId)
622 Z_IdPass *pass = odr_malloc (p->odr_out, sizeof(*pass));
623 Z_IdAuthentication *auth = odr_malloc (p->odr_out, sizeof(*auth));
625 logf (LOG_DEBUG, "using pass authentication");
627 auth->which = Z_IdAuthentication_idPass;
628 auth->u.idPass = pass;
629 if (p->idAuthenticationGroupId && *p->idAuthenticationGroupId)
630 pass->groupId = p->idAuthenticationGroupId;
632 pass->groupId = NULL;
633 if (p->idAuthenticationUserId && *p->idAuthenticationUserId)
634 pass->userId = p->idAuthenticationUserId;
637 if (p->idAuthenticationPassword && *p->idAuthenticationPassword)
638 pass->password = p->idAuthenticationPassword;
640 pass->password = NULL;
641 req->idAuthentication = auth;
643 else if (!p->idAuthenticationOpen || !*p->idAuthenticationOpen)
644 req->idAuthentication = NULL;
647 Z_IdAuthentication *auth = odr_malloc (p->odr_out, sizeof(*auth));
649 logf (LOG_DEBUG, "using open authentication");
650 auth->which = Z_IdAuthentication_open;
651 auth->u.open = p->idAuthenticationOpen;
652 req->idAuthentication = auth;
654 req->implementationId = p->implementationId;
655 req->implementationName = p->implementationName;
656 req->implementationVersion = p->implementationVersion;
657 req->userInformationField = 0;
659 return ir_tcl_send_APDU (interp, p, apdu, "init", *argv);
663 * do_protocolVersion: Set protocol Version
665 static int do_protocolVersion (void *obj, Tcl_Interp *interp,
666 int argc, char **argv)
674 ODR_MASK_ZERO (&p->protocolVersion);
675 ODR_MASK_SET (&p->protocolVersion, 0);
676 ODR_MASK_SET (&p->protocolVersion, 1);
681 if (Tcl_GetInt (interp, argv[2], &version)==TCL_ERROR)
683 ODR_MASK_ZERO (&p->protocolVersion);
684 for (i = 0; i<version; i++)
685 ODR_MASK_SET (&p->protocolVersion, i);
687 for (i = 4; --i >= 0; )
688 if (ODR_MASK_GET (&p->protocolVersion, i))
690 sprintf (buf, "%d", i+1);
691 interp->result = buf;
696 * do_options: Set options
698 static int do_options (void *obj, Tcl_Interp *interp,
699 int argc, char **argv)
701 static struct ir_named_entry options_tab[] = {
705 { "resourceReport", 3 },
706 { "triggerResourceCtrl", 4},
707 { "resourceCtrl", 5},
711 { "extendedServices", 10},
712 { "level-1Segmentation", 11},
713 { "level-2Segmentation", 12},
714 { "concurrentOperations", 13},
715 { "namedResultSets", 14},
722 ODR_MASK_ZERO (&p->options);
723 ODR_MASK_SET (&p->options, 0);
724 ODR_MASK_SET (&p->options, 1);
725 ODR_MASK_SET (&p->options, 4);
726 ODR_MASK_SET (&p->options, 7);
727 ODR_MASK_SET (&p->options, 14);
730 return ir_tcl_named_bits (options_tab, &p->options, interp, argc-2, argv+2);
734 * do_apduInfo: Get APDU information
736 static int do_apduInfo (void *obj, Tcl_Interp *interp, int argc, char **argv)
744 sprintf (buf, "%d", p->apduLen);
745 Tcl_AppendElement (interp, buf);
746 sprintf (buf, "%d", p->apduOffset);
747 Tcl_AppendElement (interp, buf);
750 Tcl_AppendElement (interp, "");
753 apduf = fopen ("apdu.tmp", "w");
756 Tcl_AppendElement (interp, "");
759 odr_dumpBER (apduf, p->buf_in, p->apduLen);
761 if (!(apduf = fopen ("apdu.tmp", "r")))
762 Tcl_AppendElement (interp, "");
767 Tcl_AppendResult (interp, " {", NULL);
768 while ((c = getc (apduf)) != EOF)
772 Tcl_AppendResult (interp, buf, NULL);
775 Tcl_AppendResult (interp, "}", NULL);
782 * do_failInfo: Get fail information
784 static int do_failInfo (void *obj, Tcl_Interp *interp, int argc, char **argv)
794 sprintf (buf, "%d", p->failInfo);
800 case IR_TCL_FAIL_CONNECT:
801 cp = "connect failed";
803 case IR_TCL_FAIL_READ:
804 cp = "connection closed";
806 case IR_TCL_FAIL_WRITE:
807 cp = "connection closed";
809 case IR_TCL_FAIL_IN_APDU:
810 cp = "failed to decode incoming APDU";
812 case IR_TCL_FAIL_UNKNOWN_APDU:
818 Tcl_AppendElement (interp, buf);
819 Tcl_AppendElement (interp, cp);
824 * do_preferredMessageSize: Set/get preferred message size
826 static int do_preferredMessageSize (void *obj, Tcl_Interp *interp,
827 int argc, char **argv)
833 p->preferredMessageSize = 30000;
836 return ir_tcl_get_set_int (&p->preferredMessageSize, interp, argc, argv);
840 * do_maximumRecordSize: Set/get maximum record size
842 static int do_maximumRecordSize (void *obj, Tcl_Interp *interp,
843 int argc, char **argv)
849 p->maximumRecordSize = 30000;
852 return ir_tcl_get_set_int (&p->maximumRecordSize, interp, argc, argv);
856 * do_initResult: Get init result
858 static int do_initResult (void *obj, Tcl_Interp *interp,
859 int argc, char **argv)
865 return ir_tcl_get_set_int (&p->initResult, interp, argc, argv);
870 * do_implementationName: Set/get Implementation Name.
872 static int do_implementationName (void *obj, Tcl_Interp *interp,
873 int argc, char **argv)
878 return ir_tcl_strdup (interp, &p->implementationName,
879 "Index Data/IrTcl on YAZ");
881 return ir_tcl_strdel (interp, &p->implementationName);
884 free (p->implementationName);
885 if (ir_tcl_strdup (interp, &p->implementationName, argv[2])
889 Tcl_AppendResult (interp, p->implementationName, (char*) NULL);
894 * do_implementationId: Get Implementation Id.
896 static int do_implementationId (void *obj, Tcl_Interp *interp,
897 int argc, char **argv)
902 return ir_tcl_strdup (interp, &p->implementationId, "YAZ (id=81)");
904 return ir_tcl_strdel (interp, &p->implementationId);
905 Tcl_AppendResult (interp, p->implementationId, (char*) NULL);
910 * do_implementationVersion: get Implementation Version.
912 static int do_implementationVersion (void *obj, Tcl_Interp *interp,
913 int argc, char **argv)
918 return ir_tcl_strdup (interp, &p->implementationVersion,
919 "YAZ: " YAZ_VERSION " / IrTcl: " IR_TCL_VERSION);
921 return ir_tcl_strdel (interp, &p->implementationVersion);
922 Tcl_AppendResult (interp, p->implementationVersion, (char*) NULL);
927 * do_targetImplementationName: Get Implementation Name of target.
929 static int do_targetImplementationName (void *obj, Tcl_Interp *interp,
930 int argc, char **argv)
936 p->targetImplementationName = NULL;
940 return ir_tcl_strdel (interp, &p->targetImplementationName);
941 Tcl_AppendResult (interp, p->targetImplementationName, (char*) NULL);
946 * do_targetImplementationId: Get Implementation Id of target
948 static int do_targetImplementationId (void *obj, Tcl_Interp *interp,
949 int argc, char **argv)
955 p->targetImplementationId = NULL;
959 return ir_tcl_strdel (interp, &p->targetImplementationId);
960 Tcl_AppendResult (interp, p->targetImplementationId, (char*) NULL);
965 * do_targetImplementationVersion: Get Implementation Version of target
967 static int do_targetImplementationVersion (void *obj, Tcl_Interp *interp,
968 int argc, char **argv)
974 p->targetImplementationVersion = NULL;
978 return ir_tcl_strdel (interp, &p->targetImplementationVersion);
979 Tcl_AppendResult (interp, p->targetImplementationVersion, (char*) NULL);
984 * do_idAuthentication: Set/get id Authentication
986 static int do_idAuthentication (void *obj, Tcl_Interp *interp,
987 int argc, char **argv)
991 if (argc >= 3 || argc == -1)
993 free (p->idAuthenticationOpen);
994 free (p->idAuthenticationGroupId);
995 free (p->idAuthenticationUserId);
996 free (p->idAuthenticationPassword);
998 if (argc >= 3 || argc <= 0)
1000 p->idAuthenticationOpen = NULL;
1001 p->idAuthenticationGroupId = NULL;
1002 p->idAuthenticationUserId = NULL;
1003 p->idAuthenticationPassword = NULL;
1012 ir_tcl_strdup (interp, &p->idAuthenticationOpen, argv[2])
1019 ir_tcl_strdup (interp, &p->idAuthenticationGroupId, argv[2])
1023 ir_tcl_strdup (interp, &p->idAuthenticationUserId, argv[3])
1027 ir_tcl_strdup (interp, &p->idAuthenticationPassword, argv[4])
1032 if (p->idAuthenticationOpen)
1033 Tcl_AppendElement (interp, p->idAuthenticationOpen);
1034 else if (p->idAuthenticationGroupId)
1036 Tcl_AppendElement (interp, p->idAuthenticationGroupId);
1037 Tcl_AppendElement (interp, p->idAuthenticationUserId);
1038 Tcl_AppendElement (interp, p->idAuthenticationPassword);
1044 * do_connect: connect method on IR object
1046 static int do_connect (void *obj, Tcl_Interp *interp,
1047 int argc, char **argv)
1057 logf (LOG_DEBUG, "connect %s %s", *argv, argv[2]);
1060 interp->result = "already connected";
1063 if (!strcmp (p->comstackType, "tcpip"))
1065 p->cs_link = cs_create (tcpip_type, CS_BLOCK, p->protocol_type);
1066 addr = tcpip_strtoaddr (argv[2]);
1069 interp->result = "tcpip_strtoaddr fail";
1072 logf (LOG_DEBUG, "tcp/ip connect %s", argv[2]);
1074 else if (!strcmp (p->comstackType, "mosi"))
1077 p->cs_link = cs_create (mosi_type, CS_BLOCK, p->protocol_type);
1078 addr = mosi_strtoaddr (argv[2]);
1081 interp->result = "mosi_strtoaddr fail";
1084 logf (LOG_DEBUG, "mosi connect %s", argv[2]);
1086 interp->result = "MOSI support not there";
1092 Tcl_AppendResult (interp, "Bad comstack type: ",
1093 p->comstackType, NULL);
1096 if (ir_tcl_strdup (interp, &p->hostname, argv[2]) == TCL_ERROR)
1098 if ((r=cs_connect (p->cs_link, addr)) < 0)
1100 interp->result = "connect fail";
1101 ir_tcl_disconnect (p);
1104 p->eventType = "connect";
1105 ir_select_add (cs_fileno (p->cs_link), p);
1108 logf (LOG_DEBUG, "connect pending fd=%d", cs_fileno(p->cs_link));
1109 ir_select_add_write (cs_fileno (p->cs_link), p);
1110 p->state = IR_TCL_R_Connecting;
1114 logf (LOG_DEBUG, "connect ok fd=%d", cs_fileno(p->cs_link));
1115 p->state = IR_TCL_R_Idle;
1117 ir_tcl_eval (p->interp, p->callback);
1121 Tcl_AppendResult (interp, p->hostname, NULL);
1126 * ir_tcl_disconnect: close connection
1128 void ir_tcl_disconnect (IrTcl_Obj *p)
1132 logf(LOG_DEBUG, "Closing connection to %s", p->hostname);
1135 ir_select_remove_write (cs_fileno (p->cs_link), p);
1136 ir_select_remove (cs_fileno (p->cs_link), p);
1138 odr_reset (p->odr_in);
1140 assert (p->cs_link);
1141 cs_close (p->cs_link);
1144 ODR_MASK_ZERO (&p->options);
1145 ODR_MASK_SET (&p->options, 0);
1146 ODR_MASK_SET (&p->options, 1);
1147 ODR_MASK_SET (&p->options, 4);
1148 ODR_MASK_SET (&p->options, 7);
1149 ODR_MASK_SET (&p->options, 14);
1151 ODR_MASK_ZERO (&p->protocolVersion);
1152 ODR_MASK_SET (&p->protocolVersion, 0);
1153 ODR_MASK_SET (&p->protocolVersion, 1);
1156 assert (!p->cs_link);
1160 * do_disconnect: disconnect method on IR object
1162 static int do_disconnect (void *obj, Tcl_Interp *interp,
1163 int argc, char **argv)
1169 p->state = IR_TCL_R_Idle;
1170 p->eventType = NULL;
1175 ir_tcl_disconnect (p);
1180 * do_comstack: Set/get comstack method on IR object
1182 static int do_comstack (void *o, Tcl_Interp *interp,
1183 int argc, char **argv)
1188 return ir_tcl_strdup (interp, &obj->comstackType, "tcpip");
1189 else if (argc == -1)
1190 return ir_tcl_strdel (interp, &obj->comstackType);
1193 free (obj->comstackType);
1194 if (ir_tcl_strdup (interp, &obj->comstackType, argv[2]) == TCL_ERROR)
1197 Tcl_AppendElement (interp, obj->comstackType);
1202 * do_logLevel: Set log level
1204 static int do_logLevel (void *o, Tcl_Interp *interp,
1205 int argc, char **argv)
1210 log_init (log_mask_str (argv[2]), "", NULL);
1212 log_init (log_mask_str (argv[2]), argv[3], NULL);
1214 log_init (log_mask_str (argv[2]), argv[3], argv[4]);
1220 * do_eventType: Return type of last event
1222 static int do_eventType (void *obj, Tcl_Interp *interp,
1223 int argc, char **argv)
1229 p->eventType = NULL;
1232 Tcl_AppendElement (interp, p->eventType ? p->eventType : "");
1238 * do_callback: add callback
1240 static int do_callback (void *obj, Tcl_Interp *interp,
1241 int argc, char **argv)
1250 else if (argc == -1)
1251 return ir_tcl_strdel (interp, &p->callback);
1257 if (ir_tcl_strdup (interp, &p->callback, argv[2]) == TCL_ERROR)
1267 * do_failback: add error handle callback
1269 static int do_failback (void *obj, Tcl_Interp *interp,
1270 int argc, char **argv)
1279 else if (argc == -1)
1280 return ir_tcl_strdel (interp, &p->failback);
1286 if (ir_tcl_strdup (interp, &p->failback, argv[2]) == TCL_ERROR)
1296 * do_initResponse: add init response handler
1298 static int do_initResponse (void *obj, Tcl_Interp *interp,
1299 int argc, char **argv)
1305 p->initResponse = NULL;
1308 else if (argc == -1)
1309 return ir_tcl_strdel (interp, &p->initResponse);
1312 free (p->initResponse);
1315 if (ir_tcl_strdup (interp, &p->initResponse, argv[2]) == TCL_ERROR)
1319 p->initResponse = NULL;
1324 * do_protocol: Set/get protocol method on IR object
1326 static int do_protocol (void *o, Tcl_Interp *interp, int argc, char **argv)
1332 p->protocol_type = PROTO_Z3950;
1337 if (!strcmp (argv[2], "Z39"))
1338 p->protocol_type = PROTO_Z3950;
1339 else if (!strcmp (argv[2], "SR"))
1340 p->protocol_type = PROTO_SR;
1343 Tcl_AppendResult (interp, "Bad protocol: ", argv[2], NULL);
1348 switch (p->protocol_type)
1351 Tcl_AppendElement (interp, "Z39");
1354 Tcl_AppendElement (interp, "SR");
1361 * do_triggerResourceControl:
1363 static int do_triggerResourceControl (void *obj, Tcl_Interp *interp,
1364 int argc, char **argv)
1368 Z_TriggerResourceControlRequest *req;
1369 bool_t is_false = 0;
1375 interp->result = "triggerResourceControl: not connected";
1378 apdu = zget_APDU (p->odr_out, Z_APDU_triggerResourceControlRequest);
1379 req = apdu->u.triggerResourceControlRequest;
1380 *req->requestedAction = Z_TriggerResourceCtrl_cancel;
1381 req->resultSetWanted = &is_false;
1383 return ir_tcl_send_APDU (interp, p, apdu, "triggerResourceControl",
1388 * do_databaseNames: specify database names
1390 static int do_databaseNames (void *obj, Tcl_Interp *interp,
1391 int argc, char **argv)
1394 IrTcl_SetCObj *p = obj;
1398 for (i=0; i<p->num_databaseNames; i++)
1399 free (p->databaseNames[i]);
1400 free (p->databaseNames);
1404 p->num_databaseNames = 0;
1405 p->databaseNames = NULL;
1410 for (i=0; i<p->num_databaseNames; i++)
1411 Tcl_AppendElement (interp, p->databaseNames[i]);
1414 if (p->databaseNames)
1416 for (i=0; i<p->num_databaseNames; i++)
1417 free (p->databaseNames[i]);
1418 free (p->databaseNames);
1420 p->num_databaseNames = argc - 2;
1422 ir_tcl_malloc (sizeof(*p->databaseNames) * (1+p->num_databaseNames));
1423 for (i=0; i<p->num_databaseNames; i++)
1425 if (ir_tcl_strdup (interp, &p->databaseNames[i], argv[2+i])
1429 p->databaseNames[i] = NULL;
1434 * do_replaceIndicator: Set/get replace Set indicator
1436 static int do_replaceIndicator (void *obj, Tcl_Interp *interp,
1437 int argc, char **argv)
1439 IrTcl_SetCObj *p = obj;
1443 p->replaceIndicator = 1;
1446 return ir_tcl_get_set_int (&p->replaceIndicator, interp, argc, argv);
1450 * do_queryType: Set/Get query method
1452 static int do_queryType (void *obj, Tcl_Interp *interp,
1453 int argc, char **argv)
1455 IrTcl_SetCObj *p = obj;
1458 return ir_tcl_strdup (interp, &p->queryType, "rpn");
1459 else if (argc == -1)
1460 return ir_tcl_strdel (interp, &p->queryType);
1463 free (p->queryType);
1464 if (ir_tcl_strdup (interp, &p->queryType, argv[2]) == TCL_ERROR)
1467 Tcl_AppendResult (interp, p->queryType, NULL);
1472 * do_userInformationField: Get User information field
1474 static int do_userInformationField (void *obj, Tcl_Interp *interp,
1475 int argc, char **argv)
1481 p->userInformationField = NULL;
1484 else if (argc == -1)
1485 return ir_tcl_strdel (interp, &p->userInformationField);
1486 Tcl_AppendResult (interp, p->userInformationField, NULL);
1491 * do_smallSetUpperBound: Set/get small set upper bound
1493 static int do_smallSetUpperBound (void *o, Tcl_Interp *interp,
1494 int argc, char **argv)
1496 IrTcl_SetCObj *p = o;
1500 p->smallSetUpperBound = 0;
1503 return ir_tcl_get_set_int (&p->smallSetUpperBound, interp, argc, argv);
1507 * do_largeSetLowerBound: Set/get large set lower bound
1509 static int do_largeSetLowerBound (void *o, Tcl_Interp *interp,
1510 int argc, char **argv)
1512 IrTcl_SetCObj *p = o;
1516 p->largeSetLowerBound = 2;
1519 return ir_tcl_get_set_int (&p->largeSetLowerBound, interp, argc, argv);
1523 * do_mediumSetPresentNumber: Set/get large set lower bound
1525 static int do_mediumSetPresentNumber (void *o, Tcl_Interp *interp,
1526 int argc, char **argv)
1528 IrTcl_SetCObj *p = o;
1532 p->mediumSetPresentNumber = 0;
1535 return ir_tcl_get_set_int (&p->mediumSetPresentNumber, interp, argc, argv);
1539 * do_referenceId: Set/Get referenceId
1541 static int do_referenceId (void *obj, Tcl_Interp *interp,
1542 int argc, char **argv)
1544 IrTcl_SetCObj *p = obj;
1548 p->referenceId = NULL;
1551 else if (argc == -1)
1552 return ir_tcl_strdel (interp, &p->referenceId);
1555 free (p->referenceId);
1556 if (ir_tcl_strdup (interp, &p->referenceId, argv[2]) == TCL_ERROR)
1559 Tcl_AppendResult (interp, p->referenceId, NULL);
1564 * do_preferredRecordSyntax: Set/get preferred record syntax
1566 static int do_preferredRecordSyntax (void *obj, Tcl_Interp *interp,
1567 int argc, char **argv)
1569 IrTcl_SetCObj *p = obj;
1573 p->preferredRecordSyntax = NULL;
1576 else if (argc == -1)
1578 free (p->preferredRecordSyntax);
1579 p->preferredRecordSyntax = NULL;
1584 free (p->preferredRecordSyntax);
1585 p->preferredRecordSyntax = NULL;
1586 if (argv[2][0] && (p->preferredRecordSyntax =
1587 ir_tcl_malloc (sizeof(*p->preferredRecordSyntax))))
1588 *p->preferredRecordSyntax = IrTcl_getRecordSyntaxVal (argv[2]);
1593 (interp,!p->preferredRecordSyntax ? "" :
1594 IrTcl_getRecordSyntaxStr(*p->preferredRecordSyntax));
1601 * do_elementSetNames: Set/Get element Set Names
1603 static int do_elementSetNames (void *obj, Tcl_Interp *interp,
1604 int argc, char **argv)
1606 IrTcl_SetCObj *p = obj;
1610 p->elementSetNames = NULL;
1613 else if (argc == -1)
1614 return ir_tcl_strdel (interp, &p->elementSetNames);
1617 free (p->elementSetNames);
1618 if (ir_tcl_strdup (interp, &p->elementSetNames, argv[2]) == TCL_ERROR)
1621 Tcl_AppendResult (interp, p->elementSetNames, NULL);
1626 * do_smallSetElementSetNames: Set/Get small Set Element Set Names
1628 static int do_smallSetElementSetNames (void *obj, Tcl_Interp *interp,
1629 int argc, char **argv)
1631 IrTcl_SetCObj *p = obj;
1635 p->smallSetElementSetNames = NULL;
1638 else if (argc == -1)
1639 return ir_tcl_strdel (interp, &p->smallSetElementSetNames);
1642 free (p->smallSetElementSetNames);
1643 if (ir_tcl_strdup (interp, &p->smallSetElementSetNames,
1644 argv[2]) == TCL_ERROR)
1647 Tcl_AppendResult (interp, p->smallSetElementSetNames, NULL);
1652 * do_mediumSetElementSetNames: Set/Get medium Set Element Set Names
1654 static int do_mediumSetElementSetNames (void *obj, Tcl_Interp *interp,
1655 int argc, char **argv)
1657 IrTcl_SetCObj *p = obj;
1661 p->mediumSetElementSetNames = NULL;
1664 else if (argc == -1)
1665 return ir_tcl_strdel (interp, &p->mediumSetElementSetNames);
1668 free (p->mediumSetElementSetNames);
1669 if (ir_tcl_strdup (interp, &p->mediumSetElementSetNames,
1670 argv[2]) == TCL_ERROR)
1673 Tcl_AppendResult (interp, p->mediumSetElementSetNames, NULL);
1677 static IrTcl_Method ir_method_tab[] = {
1678 { "comstack", do_comstack, NULL },
1679 { "protocol", do_protocol, NULL },
1680 { "failback", do_failback, NULL },
1681 { "failInfo", do_failInfo, NULL },
1682 { "apduInfo", do_apduInfo, NULL },
1683 { "logLevel", do_logLevel, NULL },
1685 { "eventType", do_eventType, NULL },
1686 { "connect", do_connect, NULL },
1687 { "protocolVersion", do_protocolVersion, NULL },
1688 { "preferredMessageSize", do_preferredMessageSize, NULL },
1689 { "maximumRecordSize", do_maximumRecordSize, NULL },
1690 { "implementationName", do_implementationName, NULL },
1691 { "implementationId", do_implementationId, NULL },
1692 { "implementationVersion", do_implementationVersion, NULL },
1693 { "targetImplementationName", do_targetImplementationName, NULL },
1694 { "targetImplementationId", do_targetImplementationId, NULL },
1695 { "targetImplementationVersion", do_targetImplementationVersion, NULL},
1696 { "userInformationField", do_userInformationField, NULL},
1697 { "idAuthentication", do_idAuthentication, NULL},
1698 { "options", do_options, NULL},
1699 { "init", do_init_request, NULL},
1700 { "initResult", do_initResult, NULL},
1701 { "disconnect", do_disconnect, NULL},
1702 { "callback", do_callback, NULL},
1703 { "initResponse", do_initResponse, NULL},
1704 { "triggerResourceControl", do_triggerResourceControl, NULL},
1705 { "initResponse", do_initResponse, NULL},
1709 static IrTcl_Method ir_set_c_method_tab[] = {
1710 { "databaseNames", do_databaseNames, NULL},
1711 { "replaceIndicator", do_replaceIndicator, NULL},
1712 { "queryType", do_queryType, NULL},
1713 { "preferredRecordSyntax", do_preferredRecordSyntax, NULL},
1714 { "smallSetUpperBound", do_smallSetUpperBound, NULL},
1715 { "largeSetLowerBound", do_largeSetLowerBound, NULL},
1716 { "mediumSetPresentNumber", do_mediumSetPresentNumber, NULL},
1717 { "referenceId", do_referenceId, NULL},
1718 { "elementSetNames", do_elementSetNames, NULL},
1719 { "smallSetElementSetNames", do_smallSetElementSetNames, NULL},
1720 { "mediumSetElementSetNames", do_mediumSetElementSetNames, NULL},
1725 * ir_obj_method: IR Object methods
1727 static int ir_obj_method (ClientData clientData, Tcl_Interp *interp,
1728 int argc, char **argv)
1730 IrTcl_Methods tab[3];
1731 IrTcl_Obj *p = clientData;
1737 tab[0].tab = ir_method_tab;
1739 tab[1].tab = ir_set_c_method_tab;
1740 tab[1].obj = &p->set_inher;
1743 ir_tcl_method (interp, argc, argv, tab, &r);
1748 * ir_obj_delete: IR Object disposal
1750 static void ir_obj_delete (ClientData clientData)
1752 IrTcl_Obj *obj = clientData;
1753 IrTcl_Methods tab[3];
1756 if (obj->ref_count > 0)
1758 assert (obj->ref_count == 0);
1760 logf (LOG_DEBUG, "ir object delete");
1761 tab[0].tab = ir_method_tab;
1763 tab[1].tab = ir_set_c_method_tab;
1764 tab[1].obj = &obj->set_inher;
1767 ir_tcl_method (NULL, -1, NULL, tab, NULL);
1770 odr_destroy (obj->odr_in);
1771 odr_destroy (obj->odr_out);
1772 odr_destroy (obj->odr_pr);
1777 * ir_obj_init: IR Object initialization
1779 int ir_obj_init (ClientData clientData, Tcl_Interp *interp,
1780 int argc, char **argv, ClientData *subData,
1781 ClientData parentData)
1783 IrTcl_Methods tab[3];
1791 interp->result = "wrong # args";
1794 obj = ir_tcl_malloc (sizeof(*obj));
1797 obj->bibset = ccl_qual_mk ();
1798 if ((inf = fopen ("default.bib", "r")))
1800 ccl_qual_file (obj->bibset, inf);
1805 logf (LOG_DEBUG, "ir object create %s", argv[1]);
1806 obj->odr_in = odr_createmem (ODR_DECODE);
1807 obj->odr_out = odr_createmem (ODR_ENCODE);
1808 obj->odr_pr = odr_createmem (ODR_PRINT);
1809 obj->state = IR_TCL_R_Idle;
1810 obj->interp = interp;
1814 obj->request_queue = NULL;
1816 tab[0].tab = ir_method_tab;
1818 tab[1].tab = ir_set_c_method_tab;
1819 tab[1].obj = &obj->set_inher;
1822 if (ir_tcl_method (interp, 0, NULL, tab, NULL) == TCL_ERROR)
1824 Tcl_AppendResult (interp, "Failed to initialize ", argv[1], NULL);
1833 * ir_obj_mk: IR Object creation
1835 static int ir_obj_mk (ClientData clientData, Tcl_Interp *interp,
1836 int argc, char **argv)
1839 int r = ir_obj_init (clientData, interp, argc, argv, &subData, 0);
1843 Tcl_CreateCommand (interp, argv[1], ir_obj_method,
1844 subData, ir_obj_delete);
1848 IrTcl_Class ir_obj_class = {
1856 /* ------------------------------------------------------- */
1858 * do_search: Do search request
1860 static int do_search (void *o, Tcl_Interp *interp, int argc, char **argv)
1862 Z_SearchRequest *req;
1866 IrTcl_SetObj *obj = o;
1876 logf (LOG_DEBUG, "search %s", *argv);
1877 interp->result = "wrong # args";
1880 logf (LOG_DEBUG, "search %s %s", *argv, argv[2]);
1881 if (!obj->set_inher.num_databaseNames)
1883 interp->result = "no databaseNames";
1888 interp->result = "search: not connected";
1891 apdu = zget_APDU (p->odr_out, Z_APDU_searchRequest);
1892 req = apdu->u.searchRequest;
1896 set_referenceId (p->odr_out, &req->referenceId,
1897 obj->set_inher.referenceId);
1899 req->smallSetUpperBound = &obj->set_inher.smallSetUpperBound;
1900 req->largeSetLowerBound = &obj->set_inher.largeSetLowerBound;
1901 req->mediumSetPresentNumber = &obj->set_inher.mediumSetPresentNumber;
1902 req->replaceIndicator = &obj->set_inher.replaceIndicator;
1903 req->resultSetName = obj->setName ? obj->setName : "Default";
1904 logf (LOG_DEBUG, "Search, resultSetName %s", req->resultSetName);
1905 req->num_databaseNames = obj->set_inher.num_databaseNames;
1906 req->databaseNames = obj->set_inher.databaseNames;
1907 for (r=0; r < obj->set_inher.num_databaseNames; r++)
1908 logf (LOG_DEBUG, " Database %s", obj->set_inher.databaseNames[r]);
1909 if (obj->set_inher.preferredRecordSyntax)
1911 struct oident ident;
1913 ident.proto = p->protocol_type;
1914 ident.oclass = CLASS_RECSYN;
1915 ident.value = *obj->set_inher.preferredRecordSyntax;
1916 logf (LOG_DEBUG, "Preferred record syntax is %d", ident.value);
1917 req->preferredRecordSyntax = odr_oiddup (p->odr_out,
1918 oid_getoidbyent (&ident));
1921 req->preferredRecordSyntax = 0;
1923 if (obj->set_inher.smallSetElementSetNames &&
1924 *obj->set_inher.smallSetElementSetNames)
1926 Z_ElementSetNames *esn = odr_malloc (p->odr_out, sizeof(*esn));
1928 esn->which = Z_ElementSetNames_generic;
1929 esn->u.generic = obj->set_inher.smallSetElementSetNames;
1930 req->smallSetElementSetNames = esn;
1933 req->smallSetElementSetNames = NULL;
1935 if (obj->set_inher.mediumSetElementSetNames &&
1936 *obj->set_inher.mediumSetElementSetNames)
1938 Z_ElementSetNames *esn = odr_malloc (p->odr_out, sizeof(*esn));
1940 esn->which = Z_ElementSetNames_generic;
1941 esn->u.generic = obj->set_inher.mediumSetElementSetNames;
1942 req->mediumSetElementSetNames = esn;
1945 req->mediumSetElementSetNames = NULL;
1947 req->query = &query;
1949 logf (LOG_DEBUG, "queryType %s", obj->set_inher.queryType);
1950 if (!strcmp (obj->set_inher.queryType, "rpn"))
1952 Z_RPNQuery *RPNquery;
1954 RPNquery = p_query_rpn (p->odr_out, p->protocol_type, argv[2]);
1957 Tcl_AppendResult (interp, "Syntax error in query", NULL);
1960 query.which = Z_Query_type_1;
1961 query.u.type_1 = RPNquery;
1964 else if (!strcmp (obj->set_inher.queryType, "cclrpn"))
1968 struct ccl_rpn_node *rpn;
1969 Z_RPNQuery *RPNquery;
1972 bib1.proto = p->protocol_type;
1973 bib1.oclass = CLASS_ATTSET;
1974 bib1.value = VAL_BIB1;
1976 rpn = ccl_find_str(p->bibset, argv[2], &error, &pos);
1979 Tcl_AppendResult (interp, "CCL error: ",
1980 ccl_err_msg(error), NULL);
1984 ccl_pr_tree (rpn, stderr);
1985 fprintf (stderr, "\n");
1987 assert((RPNquery = ccl_rpn_query(rpn)));
1988 RPNquery->attributeSetId = oid_getoidbyent (&bib1);
1989 query.which = Z_Query_type_1;
1990 query.u.type_1 = RPNquery;
1993 else if (!strcmp (obj->set_inher.queryType, "ccl"))
1995 query.which = Z_Query_type_2;
1996 query.u.type_2 = &ccl_query;
1997 ccl_query.buf = (unsigned char *) argv[2];
1998 ccl_query.len = strlen (argv[2]);
2002 interp->result = "unknown query method";
2005 return ir_tcl_send_APDU (interp, p, apdu, "search", *argv);
2009 * do_searchResponse: add search response handler
2011 static int do_searchResponse (void *o, Tcl_Interp *interp,
2012 int argc, char **argv)
2014 IrTcl_SetObj *obj = o;
2018 obj->searchResponse = NULL;
2021 else if (argc == -1)
2022 return ir_tcl_strdel (interp, &obj->searchResponse);
2025 free (obj->searchResponse);
2028 if (ir_tcl_strdup (interp, &obj->searchResponse, argv[2])
2033 obj->searchResponse = NULL;
2039 * do_presentResponse: add present response handler
2041 static int do_presentResponse (void *o, Tcl_Interp *interp,
2042 int argc, char **argv)
2044 IrTcl_SetObj *obj = o;
2048 obj->presentResponse = NULL;
2051 else if (argc == -1)
2052 return ir_tcl_strdel (interp, &obj->presentResponse);
2055 free (obj->presentResponse);
2058 if (ir_tcl_strdup (interp, &obj->presentResponse, argv[2])
2063 obj->presentResponse = NULL;
2069 * do_resultCount: Get number of hits
2071 static int do_resultCount (void *o, Tcl_Interp *interp,
2072 int argc, char **argv)
2074 IrTcl_SetObj *obj = o;
2078 obj->resultCount = 0;
2081 return ir_tcl_get_set_int (&obj->resultCount, interp, argc, argv);
2085 * do_searchStatus: Get search status (after search response)
2087 static int do_searchStatus (void *o, Tcl_Interp *interp,
2088 int argc, char **argv)
2090 IrTcl_SetObj *obj = o;
2094 return ir_tcl_get_set_int (&obj->searchStatus, interp, argc, argv);
2098 * do_presentStatus: Get search status (after search/present response)
2100 static int do_presentStatus (void *o, Tcl_Interp *interp,
2101 int argc, char **argv)
2103 IrTcl_SetObj *obj = o;
2107 return ir_tcl_get_set_int (&obj->presentStatus, interp, argc, argv);
2111 * do_nextResultSetPosition: Get next result set position
2112 * (after search/present response)
2114 static int do_nextResultSetPosition (void *o, Tcl_Interp *interp,
2115 int argc, char **argv)
2117 IrTcl_SetObj *obj = o;
2121 obj->nextResultSetPosition = 0;
2124 return ir_tcl_get_set_int (&obj->nextResultSetPosition, interp,
2129 * do_setName: Set result Set name
2131 static int do_setName (void *o, Tcl_Interp *interp,
2132 int argc, char **argv)
2134 IrTcl_SetObj *obj = o;
2137 return ir_tcl_strdup (interp, &obj->setName, "Default");
2138 else if (argc == -1)
2139 return ir_tcl_strdel (interp, &obj->setName);
2142 free (obj->setName);
2143 if (ir_tcl_strdup (interp, &obj->setName, argv[2])
2147 Tcl_AppendElement (interp, obj->setName);
2152 * do_numberOfRecordsReturned: Get number of records returned
2154 static int do_numberOfRecordsReturned (void *o, Tcl_Interp *interp,
2155 int argc, char **argv)
2157 IrTcl_SetObj *obj = o;
2161 obj->numberOfRecordsReturned = 0;
2164 return ir_tcl_get_set_int (&obj->numberOfRecordsReturned, interp,
2169 * do_type: Return type (if any) at position.
2171 static int do_type (void *o, Tcl_Interp *interp, int argc, char **argv)
2173 IrTcl_SetObj *obj = o;
2175 IrTcl_RecordList *rl;
2179 obj->record_list = NULL;
2182 else if (argc == -1)
2184 delete_IR_records (obj);
2189 sprintf (interp->result, "wrong # args");
2192 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2194 rl = find_IR_record (obj, offset);
2197 logf (LOG_DEBUG, "No record at position %d", offset);
2202 case Z_NamePlusRecord_databaseRecord:
2203 interp->result = "DB";
2205 case Z_NamePlusRecord_surrogateDiagnostic:
2206 interp->result = "SD";
2214 * do_recordType: Return record type (if any) at position.
2216 static int do_recordType (void *o, Tcl_Interp *interp, int argc, char **argv)
2218 IrTcl_SetObj *obj = o;
2220 IrTcl_RecordList *rl;
2226 else if (argc == -1)
2232 sprintf (interp->result, "wrong # args");
2235 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2237 rl = find_IR_record (obj, offset);
2240 if (rl->which != Z_NamePlusRecord_databaseRecord)
2242 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2245 Tcl_AppendElement (interp, (char*)
2246 IrTcl_getRecordSyntaxStr (rl->u.dbrec.type));
2251 * set record elements (for record extraction)
2253 static int do_recordElements (void *o, Tcl_Interp *interp,
2254 int argc, char **argv)
2256 IrTcl_SetObj *obj = o;
2260 obj->recordElements = NULL;
2263 else if (argc == -1)
2264 return ir_tcl_strdel (NULL, &obj->recordElements);
2267 sprintf (interp->result, "wrong # args");
2272 free (obj->recordElements);
2273 return ir_tcl_strdup (NULL, &obj->recordElements,
2274 (*argv[2] ? argv[2] : NULL));
2276 Tcl_AppendResult (interp, obj->recordElements, NULL);
2283 static int ir_diagResult (Tcl_Interp *interp, IrTcl_Diagnostic *list, int num)
2289 for (i = 0; i<num; i++)
2291 sprintf (buf, "%d", list[i].condition);
2292 Tcl_AppendElement (interp, buf);
2293 cp = diagbib1_str (list[i].condition);
2295 Tcl_AppendElement (interp, (char*) cp);
2297 Tcl_AppendElement (interp, "");
2298 if (list[i].addinfo)
2299 Tcl_AppendElement (interp, (char*) list[i].addinfo);
2301 Tcl_AppendElement (interp, "");
2307 * do_diag: Return diagnostic record info
2309 static int do_diag (void *o, Tcl_Interp *interp, int argc, char **argv)
2311 IrTcl_SetObj *obj = o;
2313 IrTcl_RecordList *rl;
2319 sprintf (interp->result, "wrong # args");
2322 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2324 rl = find_IR_record (obj, offset);
2327 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2330 if (rl->which != Z_NamePlusRecord_surrogateDiagnostic)
2332 Tcl_AppendResult (interp, "No Diagnostic record at #", argv[2], NULL);
2335 return ir_diagResult (interp, rl->u.surrogateDiagnostics.list,
2336 rl->u.surrogateDiagnostics.num);
2340 * do_getMarc: Get ISO2709 Record lines/fields
2342 static int do_getMarc (void *o, Tcl_Interp *interp, int argc, char **argv)
2344 IrTcl_SetObj *obj = o;
2346 IrTcl_RecordList *rl;
2352 sprintf (interp->result, "wrong # args");
2355 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2357 rl = find_IR_record (obj, offset);
2360 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2363 if (rl->which != Z_NamePlusRecord_databaseRecord)
2365 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2368 return ir_tcl_get_marc (interp, rl->u.dbrec.buf, argc, argv);
2372 * do_getSutrs: Get SUTRS Record
2374 static int do_getSutrs (void *o, Tcl_Interp *interp, int argc, char **argv)
2376 IrTcl_SetObj *obj = o;
2378 IrTcl_RecordList *rl;
2384 sprintf (interp->result, "wrong # args");
2387 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2389 rl = find_IR_record (obj, offset);
2392 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2395 if (rl->which != Z_NamePlusRecord_databaseRecord)
2397 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2400 if (rl->u.dbrec.type != VAL_SUTRS)
2402 Tcl_AppendElement (interp, rl->u.dbrec.buf);
2408 * do_getGrs: Get a GRS1 Record
2410 static int do_getGrs (void *o, Tcl_Interp *interp, int argc, char **argv)
2412 IrTcl_SetObj *obj = o;
2414 IrTcl_RecordList *rl;
2420 sprintf (interp->result, "wrong # args");
2423 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2425 rl = find_IR_record (obj, offset);
2428 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2431 if (rl->which != Z_NamePlusRecord_databaseRecord)
2433 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2436 if (rl->u.dbrec.type != VAL_GRS1)
2438 return ir_tcl_get_grs (interp, rl->u.dbrec.u.grs1, argc, argv);
2443 * do_responseStatus: Return response status (present or search)
2445 static int do_responseStatus (void *o, Tcl_Interp *interp,
2446 int argc, char **argv)
2448 IrTcl_SetObj *obj = o;
2452 obj->recordFlag = 0;
2453 obj->nonSurrogateDiagnosticNum = 0;
2454 obj->nonSurrogateDiagnosticList = NULL;
2457 else if (argc == -1)
2459 ir_deleteDiags (&obj->nonSurrogateDiagnosticList,
2460 &obj->nonSurrogateDiagnosticNum);
2463 if (!obj->recordFlag)
2465 Tcl_AppendElement (interp, "OK");
2470 case Z_Records_DBOSD:
2471 Tcl_AppendElement (interp, "DBOSD");
2474 Tcl_AppendElement (interp, "NSD");
2475 return ir_diagResult (interp, obj->nonSurrogateDiagnosticList,
2476 obj->nonSurrogateDiagnosticNum);
2482 * do_present: Perform Present Request
2485 static int do_present (void *o, Tcl_Interp *interp, int argc, char **argv)
2487 IrTcl_SetObj *obj = o;
2490 Z_PresentRequest *req;
2498 if (Tcl_GetInt (interp, argv[2], &start) == TCL_ERROR)
2505 if (Tcl_GetInt (interp, argv[3], &number) == TCL_ERROR)
2510 logf (LOG_DEBUG, "present %s %d %d", *argv, start, number);
2514 interp->result = "present: not connected";
2519 obj->number = number;
2521 apdu = zget_APDU (p->odr_out, Z_APDU_presentRequest);
2522 req = apdu->u.presentRequest;
2524 set_referenceId (p->odr_out, &req->referenceId,
2525 obj->set_inher.referenceId);
2527 req->resultSetId = obj->setName ? obj->setName : "Default";
2529 req->resultSetStartPoint = &start;
2530 req->numberOfRecordsRequested = &number;
2531 if (obj->set_inher.preferredRecordSyntax)
2533 struct oident ident;
2535 ident.proto = p->protocol_type;
2536 ident.oclass = CLASS_RECSYN;
2537 ident.value = *obj->set_inher.preferredRecordSyntax;
2538 logf (LOG_DEBUG, "Preferred record syntax is %d", ident.value);
2539 req->preferredRecordSyntax = odr_oiddup (p->odr_out,
2540 oid_getoidbyent (&ident));
2543 req->preferredRecordSyntax = 0;
2545 if (obj->set_inher.elementSetNames && *obj->set_inher.elementSetNames)
2547 Z_ElementSetNames *esn = odr_malloc (p->odr_out, sizeof(*esn));
2548 Z_RecordComposition *compo = odr_malloc (p->odr_out, sizeof(*compo));
2550 esn->which = Z_ElementSetNames_generic;
2551 esn->u.generic = obj->set_inher.elementSetNames;
2553 req->recordComposition = compo;
2554 compo->which = Z_RecordComp_simple;
2555 compo->u.simple = esn;
2558 req->recordComposition = NULL;
2559 return ir_tcl_send_APDU (interp, p, apdu, "present", *argv);
2563 * do_loadFile: Load result set from file
2566 static int do_loadFile (void *o, Tcl_Interp *interp,
2567 int argc, char **argv)
2569 IrTcl_SetObj *setobj = o;
2579 interp->result = "wrong # args";
2582 inf = fopen (argv[2], "r");
2585 Tcl_AppendResult (interp, "Cannot open ", argv[2], NULL);
2588 while ((buf = ir_tcl_fread_marc (inf, &size)))
2590 IrTcl_RecordList *rl;
2592 rl = new_IR_record (setobj, no, Z_NamePlusRecord_databaseRecord, "F");
2593 rl->u.dbrec.type = VAL_USMARC;
2594 rl->u.dbrec.buf = buf;
2595 rl->u.dbrec.size = size;
2598 setobj->numberOfRecordsReturned = no-1;
2603 static IrTcl_Method ir_set_method_tab[] = {
2604 { "search", do_search, NULL},
2605 { "searchResponse", do_searchResponse, NULL},
2606 { "presentResponse", do_presentResponse, NULL},
2607 { "searchStatus", do_searchStatus, NULL},
2608 { "presentStatus", do_presentStatus, NULL},
2609 { "nextResultSetPosition", do_nextResultSetPosition, NULL},
2610 { "setName", do_setName, NULL},
2611 { "resultCount", do_resultCount, NULL},
2612 { "numberOfRecordsReturned", do_numberOfRecordsReturned, NULL},
2613 { "present", do_present, NULL},
2614 { "type", do_type, NULL},
2615 { "getMarc", do_getMarc, NULL},
2616 { "getSutrs", do_getSutrs, NULL},
2617 { "getGrs", do_getGrs, NULL},
2618 { "recordType", do_recordType, NULL},
2619 { "recordElements", do_recordElements, NULL},
2620 { "diag", do_diag, NULL},
2621 { "responseStatus", do_responseStatus, NULL},
2622 { "loadFile", do_loadFile, NULL},
2627 * ir_set_obj_method: IR Set Object methods
2629 static int ir_set_obj_method (ClientData clientData, Tcl_Interp *interp,
2630 int argc, char **argv)
2632 IrTcl_Methods tabs[3];
2633 IrTcl_SetObj *p = clientData;
2638 interp->result = "wrong # args";
2641 tabs[0].tab = ir_set_method_tab;
2643 tabs[1].tab = ir_set_c_method_tab;
2644 tabs[1].obj = &p->set_inher;
2647 ir_tcl_method (interp, argc, argv, tabs, &r);
2652 * ir_set_obj_delete: IR Set Object disposal
2654 static void ir_set_obj_delete (ClientData clientData)
2656 IrTcl_Methods tabs[3];
2657 IrTcl_SetObj *p = clientData;
2659 logf (LOG_DEBUG, "ir set delete");
2661 tabs[0].tab = ir_set_method_tab;
2663 tabs[1].tab = ir_set_c_method_tab;
2664 tabs[1].obj = &p->set_inher;
2667 ir_tcl_method (NULL, -1, NULL, tabs, NULL);
2673 * ir_set_obj_init: IR Set Object initialization
2675 static int ir_set_obj_init (ClientData clientData, Tcl_Interp *interp,
2676 int argc, char **argv, ClientData *subData,
2677 ClientData parentData)
2679 IrTcl_Methods tabs[3];
2682 if (argc < 2 || argc > 3)
2684 interp->result = "wrong # args";
2687 obj = ir_tcl_malloc (sizeof(*obj));
2688 logf (LOG_DEBUG, "ir set create %s", argv[1]);
2695 obj->parent = (IrTcl_Obj *) parentData;
2697 dst = &obj->set_inher;
2698 src = &obj->parent->set_inher;
2700 if ((dst->num_databaseNames = src->num_databaseNames))
2702 dst->databaseNames =
2703 ir_tcl_malloc (sizeof (*dst->databaseNames)
2704 * (1+dst->num_databaseNames));
2705 for (i = 0; i < dst->num_databaseNames; i++)
2706 if (ir_tcl_strdup (interp, &dst->databaseNames[i],
2707 src->databaseNames[i]) == TCL_ERROR)
2709 dst->databaseNames[i] = NULL;
2712 dst->databaseNames = NULL;
2713 if (ir_tcl_strdup (interp, &dst->queryType, src->queryType)
2717 if (ir_tcl_strdup (interp, &dst->referenceId, src->referenceId)
2721 if (ir_tcl_strdup (interp, &dst->elementSetNames, src->elementSetNames)
2725 if (ir_tcl_strdup (interp, &dst->smallSetElementSetNames,
2726 src->smallSetElementSetNames)
2730 if (ir_tcl_strdup (interp, &dst->mediumSetElementSetNames,
2731 src->mediumSetElementSetNames)
2735 if (src->preferredRecordSyntax &&
2736 (dst->preferredRecordSyntax
2737 = ir_tcl_malloc (sizeof(*dst->preferredRecordSyntax))))
2738 *dst->preferredRecordSyntax = *src->preferredRecordSyntax;
2740 dst->preferredRecordSyntax = NULL;
2741 dst->replaceIndicator = src->replaceIndicator;
2742 dst->smallSetUpperBound = src->smallSetUpperBound;
2743 dst->largeSetLowerBound = src->largeSetLowerBound;
2744 dst->mediumSetPresentNumber = src->mediumSetPresentNumber;
2749 tabs[0].tab = ir_set_method_tab;
2753 if (ir_tcl_method (interp, 0, NULL, tabs, NULL) == TCL_ERROR)
2761 * ir_set_obj_mk: IR Set Object creation
2763 static int ir_set_obj_mk (ClientData clientData, Tcl_Interp *interp,
2764 int argc, char **argv)
2767 ClientData parentData = 0;
2772 Tcl_CmdInfo parent_info;
2773 if (!Tcl_GetCommandInfo (interp, argv[2], &parent_info))
2775 interp->result = "No parent";
2778 parentData = parent_info.clientData;
2780 r = ir_set_obj_init (clientData, interp, argc, argv, &subData, parentData);
2783 Tcl_CreateCommand (interp, argv[1], ir_set_obj_method,
2784 subData, ir_set_obj_delete);
2788 IrTcl_Class ir_set_obj_class = {
2795 /* ------------------------------------------------------- */
2798 * do_scan: Perform scan
2800 static int do_scan (void *o, Tcl_Interp *interp, int argc, char **argv)
2804 IrTcl_ScanObj *obj = o;
2805 IrTcl_Obj *p = obj->parent;
2808 struct ccl_rpn_node *rpn;
2816 interp->result = "wrong # args";
2819 logf (LOG_DEBUG, "scan %s %s", *argv, argv[2]);
2820 if (!p->set_inher.num_databaseNames)
2822 interp->result = "no databaseNames";
2827 interp->result = "scan: not connected";
2831 apdu = zget_APDU (p->odr_out, Z_APDU_scanRequest);
2832 req = apdu->u.scanRequest;
2834 set_referenceId (p->odr_out, &req->referenceId, p->set_inher.referenceId);
2835 req->num_databaseNames = p->set_inher.num_databaseNames;
2836 req->databaseNames = p->set_inher.databaseNames;
2839 if (!(req->termListAndStartPoint =
2840 p_query_scan (p->odr_out, p->protocol_type,
2841 &req->attributeSet, argv[2])))
2843 Tcl_AppendResult (interp, "Syntax error in query", NULL);
2847 rpn = ccl_find_str(p->bibset, argv[2], &r, &pos);
2850 Tcl_AppendResult (interp, "CCL error: ", ccl_err_msg (r), NULL);
2853 bib1.proto = p->protocol_type;
2854 bib1.oclass = CLASS_ATTSET;
2855 bib1.value = VAL_BIB1;
2857 req->attributeSet = oid_getoidbyent (&bib1);
2858 if (!(req->termListAndStartPoint = ccl_scan_query (rpn)))
2861 req->stepSize = &obj->stepSize;
2862 req->numberOfTermsRequested = &obj->numberOfTermsRequested;
2863 req->preferredPositionInResponse = &obj->preferredPositionInResponse;
2864 logf (LOG_DEBUG, "stepSize=%d", *req->stepSize);
2865 logf (LOG_DEBUG, "numberOfTermsRequested=%d",
2866 *req->numberOfTermsRequested);
2867 logf (LOG_DEBUG, "preferredPositionInResponse=%d",
2868 *req->preferredPositionInResponse);
2870 return ir_tcl_send_APDU (interp, p, apdu, "scan", *argv);
2874 * do_scanResponse: add scan response handler
2876 static int do_scanResponse (void *o, Tcl_Interp *interp,
2877 int argc, char **argv)
2879 IrTcl_ScanObj *obj = o;
2883 obj->scanResponse = NULL;
2886 else if (argc == -1)
2887 return ir_tcl_strdel (interp, &obj->scanResponse);
2890 free (obj->scanResponse);
2893 if (ir_tcl_strdup (interp, &obj->scanResponse, argv[2])
2898 obj->scanResponse = NULL;
2904 * do_stepSize: Set/get replace Step Size
2906 static int do_stepSize (void *obj, Tcl_Interp *interp,
2907 int argc, char **argv)
2909 IrTcl_ScanObj *p = obj;
2915 return ir_tcl_get_set_int (&p->stepSize, interp, argc, argv);
2919 * do_numberOfTermsRequested: Set/get Number of Terms requested
2921 static int do_numberOfTermsRequested (void *obj, Tcl_Interp *interp,
2922 int argc, char **argv)
2924 IrTcl_ScanObj *p = obj;
2928 p->numberOfTermsRequested = 20;
2931 return ir_tcl_get_set_int (&p->numberOfTermsRequested, interp, argc, argv);
2936 * do_preferredPositionInResponse: Set/get preferred Position
2938 static int do_preferredPositionInResponse (void *obj, Tcl_Interp *interp,
2939 int argc, char **argv)
2941 IrTcl_ScanObj *p = obj;
2945 p->preferredPositionInResponse = 1;
2948 return ir_tcl_get_set_int (&p->preferredPositionInResponse, interp,
2953 * do_scanStatus: Get scan status
2955 static int do_scanStatus (void *obj, Tcl_Interp *interp,
2956 int argc, char **argv)
2958 IrTcl_ScanObj *p = obj;
2962 return ir_tcl_get_set_int (&p->scanStatus, interp, argc, argv);
2966 * do_numberOfEntriesReturned: Get number of Entries returned
2968 static int do_numberOfEntriesReturned (void *obj, Tcl_Interp *interp,
2969 int argc, char **argv)
2971 IrTcl_ScanObj *p = obj;
2975 return ir_tcl_get_set_int (&p->numberOfEntriesReturned, interp,
2980 * do_positionOfTerm: Get position of Term
2982 static int do_positionOfTerm (void *obj, Tcl_Interp *interp,
2983 int argc, char **argv)
2985 IrTcl_ScanObj *p = obj;
2989 return ir_tcl_get_set_int (&p->positionOfTerm, interp, argc, argv);
2993 * do_scanLine: get Scan Line (surrogate or normal) after response
2995 static int do_scanLine (void *obj, Tcl_Interp *interp, int argc, char **argv)
2997 IrTcl_ScanObj *p = obj;
3003 p->entries_flag = 0;
3005 p->nonSurrogateDiagnosticNum = 0;
3006 p->nonSurrogateDiagnosticList = 0;
3009 else if (argc == -1)
3011 p->entries_flag = 0;
3012 /* release entries */
3015 ir_deleteDiags (&p->nonSurrogateDiagnosticList,
3016 &p->nonSurrogateDiagnosticNum);
3021 interp->result = "wrong # args";
3024 if (Tcl_GetInt (interp, argv[2], &i) == TCL_ERROR)
3026 if (!p->entries_flag || p->which != Z_ListEntries_entries || !p->entries
3027 || i >= p->num_entries || i < 0)
3029 switch (p->entries[i].which)
3031 case Z_Entry_termInfo:
3032 Tcl_AppendElement (interp, "T");
3033 if (p->entries[i].u.term.buf)
3034 Tcl_AppendElement (interp, p->entries[i].u.term.buf);
3036 Tcl_AppendElement (interp, "");
3037 sprintf (numstr, "%d", p->entries[i].u.term.globalOccurrences);
3038 Tcl_AppendElement (interp, numstr);
3040 case Z_Entry_surrogateDiagnostic:
3041 Tcl_AppendElement (interp, "SD");
3042 return ir_diagResult (interp, p->entries[i].u.diag.list,
3043 p->entries[i].u.diag.num);
3049 static IrTcl_Method ir_scan_method_tab[] = {
3050 { "scan", do_scan, NULL},
3051 { "scanResponse", do_scanResponse, NULL},
3052 { "stepSize", do_stepSize, NULL},
3053 { "numberOfTermsRequested", do_numberOfTermsRequested, NULL},
3054 { "preferredPositionInResponse", do_preferredPositionInResponse, NULL},
3055 { "scanStatus", do_scanStatus, NULL},
3056 { "numberOfEntriesReturned", do_numberOfEntriesReturned, NULL},
3057 { "positionOfTerm", do_positionOfTerm, NULL},
3058 { "scanLine", do_scanLine, NULL},
3063 * ir_scan_obj_method: IR Scan Object methods
3065 static int ir_scan_obj_method (ClientData clientData, Tcl_Interp *interp,
3066 int argc, char **argv)
3068 IrTcl_Methods tabs[2];
3073 interp->result = "wrong # args";
3076 tabs[0].tab = ir_scan_method_tab;
3077 tabs[0].obj = clientData;
3080 ir_tcl_method (interp, argc, argv, tabs, &r);
3085 * ir_scan_obj_delete: IR Scan Object disposal
3087 static void ir_scan_obj_delete (ClientData clientData)
3089 IrTcl_Methods tabs[2];
3090 IrTcl_ScanObj *obj = clientData;
3092 tabs[0].tab = ir_scan_method_tab;
3096 ir_tcl_method (NULL, -1, NULL, tabs, NULL);
3101 * ir_scan_obj_mk: IR Scan Object creation
3103 static int ir_scan_obj_mk (ClientData clientData, Tcl_Interp *interp,
3104 int argc, char **argv)
3106 Tcl_CmdInfo parent_info;
3108 IrTcl_Methods tabs[2];
3112 interp->result = "wrong # args";
3115 logf (LOG_DEBUG, "ir scan create %s", argv[1]);
3116 if (!Tcl_GetCommandInfo (interp, argv[2], &parent_info))
3118 interp->result = "No parent";
3121 obj = ir_tcl_malloc (sizeof(*obj));
3122 obj->parent = (IrTcl_Obj *) parent_info.clientData;
3124 tabs[0].tab = ir_scan_method_tab;
3128 if (ir_tcl_method (interp, 0, NULL, tabs, NULL) == TCL_ERROR)
3130 Tcl_CreateCommand (interp, argv[1], ir_scan_obj_method,
3131 (ClientData) obj, ir_scan_obj_delete);
3135 /* ------------------------------------------------------- */
3137 static void ir_initResponse (void *obj, Z_InitResponse *initrs)
3141 p->initResult = *initrs->result ? 1 : 0;
3142 if (!*initrs->result)
3143 logf (LOG_DEBUG, "Connection rejected by target");
3145 logf (LOG_DEBUG, "Connection accepted by target");
3147 get_referenceId (&p->set_inher.referenceId, initrs->referenceId);
3149 free (p->targetImplementationId);
3150 ir_tcl_strdup (p->interp, &p->targetImplementationId,
3151 initrs->implementationId);
3152 free (p->targetImplementationName);
3153 ir_tcl_strdup (p->interp, &p->targetImplementationName,
3154 initrs->implementationName);
3155 free (p->targetImplementationVersion);
3156 ir_tcl_strdup (p->interp, &p->targetImplementationVersion,
3157 initrs->implementationVersion);
3159 p->maximumRecordSize = *initrs->maximumRecordSize;
3160 p->preferredMessageSize = *initrs->preferredMessageSize;
3162 memcpy (&p->options, initrs->options, sizeof(initrs->options));
3163 memcpy (&p->protocolVersion, initrs->protocolVersion,
3164 sizeof(initrs->protocolVersion));
3165 free (p->userInformationField);
3166 p->userInformationField = NULL;
3167 if (initrs->userInformationField)
3171 if (initrs->userInformationField->which == ODR_EXTERNAL_octet &&
3172 (p->userInformationField =
3173 ir_tcl_malloc ((len =
3174 initrs->userInformationField->
3175 u.octet_aligned->len) +1)))
3177 memcpy (p->userInformationField,
3178 initrs->userInformationField->u.octet_aligned->buf,
3180 (p->userInformationField)[len] = '\0';
3185 static void ir_deleteDiags (IrTcl_Diagnostic **dst_list, int *dst_num)
3188 for (i = 0; i<*dst_num; i++)
3189 free (dst_list[i]->addinfo);
3195 static void ir_handleDiags (IrTcl_Diagnostic **dst_list, int *dst_num,
3196 Z_DiagRec **list, int num)
3202 *dst_list = ir_tcl_malloc (sizeof(**dst_list) * num);
3203 for (i = 0; i<num; i++)
3206 switch (list[i]->which)
3208 case Z_DiagRec_defaultFormat:
3209 (*dst_list)[i].condition = *list[i]->u.defaultFormat->condition;
3210 addinfo = list[i]->u.defaultFormat->addinfo;
3212 ((*dst_list)[i].addinfo = ir_tcl_malloc (strlen(addinfo)+1)))
3213 strcpy ((*dst_list)[i].addinfo, addinfo);
3214 cp = diagbib1_str ((*dst_list)[i].condition);
3215 logf (LOG_DEBUG, "Diag %d %s %s", (*dst_list)[i].condition,
3216 cp ? cp : "", addinfo ? addinfo : "");
3219 (*dst_list)[i].addinfo = NULL;
3220 (*dst_list)[i].condition = 0;
3225 static void ir_handleRecords (void *o, Z_Records *zrs, IrTcl_SetObj *setobj,
3226 const char *elements)
3231 IrTcl_RecordList *rl;
3233 setobj->which = zrs->which;
3234 setobj->recordFlag = 1;
3236 ir_deleteDiags (&setobj->nonSurrogateDiagnosticList,
3237 &setobj->nonSurrogateDiagnosticNum);
3238 if (zrs->which == Z_Records_DBOSD)
3240 setobj->numberOfRecordsReturned =
3241 zrs->u.databaseOrSurDiagnostics->num_records;
3242 logf (LOG_DEBUG, "Got %d records", setobj->numberOfRecordsReturned);
3243 for (offset = 0; offset<setobj->numberOfRecordsReturned; offset++)
3245 rl = new_IR_record (setobj, setobj->start + offset,
3246 zrs->u.databaseOrSurDiagnostics->
3247 records[offset]->which,
3249 if (rl->which == Z_NamePlusRecord_surrogateDiagnostic)
3251 ir_handleDiags (&rl->u.surrogateDiagnostics.list,
3252 &rl->u.surrogateDiagnostics.num,
3253 &zrs->u.databaseOrSurDiagnostics->
3254 records[offset]->u.surrogateDiagnostic,
3259 Z_DatabaseRecord *zr;
3261 struct oident *ident;
3263 zr = zrs->u.databaseOrSurDiagnostics->records[offset]
3265 oe = (Z_External*) zr;
3266 rl->u.dbrec.size = zr->u.octet_aligned->len;
3268 if ((ident = oid_getentbyoid (oe->direct_reference)))
3269 rl->u.dbrec.type = ident->value;
3271 rl->u.dbrec.type = VAL_USMARC;
3273 if (oe->which == ODR_EXTERNAL_octet && rl->u.dbrec.size > 0)
3275 char *buf = (char*) zr->u.octet_aligned->buf;
3276 if ((rl->u.dbrec.buf = ir_tcl_malloc (rl->u.dbrec.size)))
3277 memcpy (rl->u.dbrec.buf, buf, rl->u.dbrec.size);
3279 else if (rl->u.dbrec.type == VAL_SUTRS &&
3280 oe->which == Z_External_sutrs)
3282 odr_setbuf (p->odr_in, (char*) oe->u.single_ASN1_type->buf,
3283 oe->u.single_ASN1_type->len, 0);
3284 if ((rl->u.dbrec.buf = ir_tcl_malloc (oe->u.sutrs->len+1)))
3286 memcpy (rl->u.dbrec.buf, oe->u.sutrs->buf,
3288 rl->u.dbrec.buf[oe->u.sutrs->len] = '\0';
3290 rl->u.dbrec.size = oe->u.sutrs->len;
3292 else if (rl->u.dbrec.type == VAL_GRS1 &&
3293 oe->which == Z_External_grs1)
3295 ir_tcl_grs_mk (oe->u.grs1, &rl->u.dbrec.u.grs1);
3296 rl->u.dbrec.buf = NULL;
3299 rl->u.dbrec.buf = NULL;
3303 else if (zrs->which == Z_Records_multipleNSD)
3305 logf (LOG_DEBUG, "multipleNonSurrogateDiagnostic %d",
3306 zrs->u.multipleNonSurDiagnostics->num_diagRecs);
3307 setobj->numberOfRecordsReturned = 0;
3308 ir_handleDiags (&setobj->nonSurrogateDiagnosticList,
3309 &setobj->nonSurrogateDiagnosticNum,
3310 zrs->u.multipleNonSurDiagnostics->diagRecs,
3311 zrs->u.multipleNonSurDiagnostics->num_diagRecs);
3315 logf (LOG_DEBUG, "NonSurrogateDiagnostic");
3316 setobj->numberOfRecordsReturned = 0;
3317 ir_handleDiags (&setobj->nonSurrogateDiagnosticList,
3318 &setobj->nonSurrogateDiagnosticNum,
3319 &zrs->u.nonSurrogateDiagnostic,
3324 static void ir_searchResponse (void *o, Z_SearchResponse *searchrs,
3325 IrTcl_SetObj *setobj)
3327 Z_Records *zrs = searchrs->records;
3329 logf (LOG_DEBUG, "Received search response");
3332 logf (LOG_DEBUG, "Search response, no object!");
3335 setobj->searchStatus = searchrs->searchStatus ? 1 : 0;
3336 get_referenceId (&setobj->set_inher.referenceId, searchrs->referenceId);
3337 setobj->resultCount = *searchrs->resultCount;
3338 if (searchrs->presentStatus)
3339 setobj->presentStatus = *searchrs->presentStatus;
3340 if (searchrs->nextResultSetPosition)
3341 setobj->nextResultSetPosition = *searchrs->nextResultSetPosition;
3343 logf (LOG_DEBUG, "status %d hits %d",
3344 setobj->searchStatus, setobj->resultCount);
3348 if (setobj->resultCount <= setobj->set_inher.smallSetUpperBound)
3349 es = setobj->set_inher.smallSetElementSetNames;
3351 es = setobj->set_inher.mediumSetElementSetNames;
3352 ir_handleRecords (o, zrs, setobj, es);
3355 setobj->recordFlag = 0;
3359 static void ir_presentResponse (void *o, Z_PresentResponse *presrs,
3360 IrTcl_SetObj *setobj)
3362 Z_Records *zrs = presrs->records;
3364 logf (LOG_DEBUG, "Received present response");
3367 logf (LOG_DEBUG, "Present response, no object!");
3370 setobj->presentStatus = *presrs->presentStatus;
3371 get_referenceId (&setobj->set_inher.referenceId, presrs->referenceId);
3372 setobj->nextResultSetPosition = *presrs->nextResultSetPosition;
3374 ir_handleRecords (o, zrs, setobj, setobj->set_inher.elementSetNames);
3377 setobj->recordFlag = 0;
3378 logf (LOG_DEBUG, "No records!");
3382 static void ir_scanResponse (void *o, Z_ScanResponse *scanrs,
3383 IrTcl_ScanObj *scanobj)
3387 logf (LOG_DEBUG, "Received scanResponse");
3389 get_referenceId (&p->set_inher.referenceId, scanrs->referenceId);
3390 scanobj->scanStatus = *scanrs->scanStatus;
3391 logf (LOG_DEBUG, "scanStatus=%d", scanobj->scanStatus);
3393 if (scanrs->stepSize)
3394 scanobj->stepSize = *scanrs->stepSize;
3395 logf (LOG_DEBUG, "stepSize=%d", scanobj->stepSize);
3397 scanobj->numberOfEntriesReturned = *scanrs->numberOfEntriesReturned;
3398 logf (LOG_DEBUG, "numberOfEntriesReturned=%d",
3399 scanobj->numberOfEntriesReturned);
3401 if (scanrs->positionOfTerm)
3402 scanobj->positionOfTerm = *scanrs->positionOfTerm;
3404 scanobj->positionOfTerm = -1;
3405 logf (LOG_DEBUG, "positionOfTerm=%d", scanobj->positionOfTerm);
3407 free (scanobj->entries);
3408 scanobj->entries = NULL;
3410 ir_deleteDiags (&scanobj->nonSurrogateDiagnosticList,
3411 &scanobj->nonSurrogateDiagnosticNum);
3412 if (scanrs->entries)
3417 scanobj->entries_flag = 1;
3418 scanobj->which = scanrs->entries->which;
3419 switch (scanobj->which)
3421 case Z_ListEntries_entries:
3422 scanobj->num_entries = scanrs->entries->u.entries->num_entries;
3423 scanobj->entries = ir_tcl_malloc (scanobj->num_entries *
3424 sizeof(*scanobj->entries));
3425 for (i=0; i<scanobj->num_entries; i++)
3427 ze = scanrs->entries->u.entries->entries[i];
3428 scanobj->entries[i].which = ze->which;
3431 case Z_Entry_termInfo:
3432 if (ze->u.termInfo->term->which == Z_Term_general)
3434 int l = ze->u.termInfo->term->u.general->len;
3435 scanobj->entries[i].u.term.buf = ir_tcl_malloc (1+l);
3436 memcpy (scanobj->entries[i].u.term.buf,
3437 ze->u.termInfo->term->u.general->buf,
3439 scanobj->entries[i].u.term.buf[l] = '\0';
3442 scanobj->entries[i].u.term.buf = NULL;
3443 if (ze->u.termInfo->globalOccurrences)
3444 scanobj->entries[i].u.term.globalOccurrences =
3445 *ze->u.termInfo->globalOccurrences;
3447 scanobj->entries[i].u.term.globalOccurrences = 0;
3449 case Z_Entry_surrogateDiagnostic:
3450 ir_handleDiags (&scanobj->entries[i].u.diag.list,
3451 &scanobj->entries[i].u.diag.num,
3452 &ze->u.surrogateDiagnostic,
3458 case Z_ListEntries_nonSurrogateDiagnostics:
3459 ir_handleDiags (&scanobj->nonSurrogateDiagnosticList,
3460 &scanobj->nonSurrogateDiagnosticNum,
3461 scanrs->entries->u.nonSurrogateDiagnostics->
3463 scanrs->entries->u.nonSurrogateDiagnostics->
3469 scanobj->entries_flag = 0;
3473 * ir_select_read: handle incoming packages
3475 static void ir_select_read (ClientData clientData)
3477 IrTcl_Obj *p = clientData;
3482 Tcl_CmdInfo cmd_info;
3483 const char *apdu_call;
3485 logf(LOG_DEBUG, "Read handler fd=%d", cs_fileno(p->cs_link));
3486 if (p->state == IR_TCL_R_Connecting)
3488 logf(LOG_DEBUG, "read: connect");
3489 r = cs_rcvconnect (p->cs_link);
3492 logf (LOG_WARN, "cs_rcvconnect returned 1");
3495 p->state = IR_TCL_R_Idle;
3497 ir_select_remove_write (cs_fileno (p->cs_link), p);
3500 logf (LOG_DEBUG, "cs_rcvconnect error");
3501 ir_tcl_disconnect (p);
3504 p->failInfo = IR_TCL_FAIL_CONNECT;
3505 ir_tcl_eval (p->interp, p->failback);
3511 ir_tcl_eval (p->interp, p->callback);
3512 if (p->ref_count == 2 && p->cs_link && p->request_queue
3513 && p->state == IR_TCL_R_Idle)
3514 ir_tcl_send_q (p, p->request_queue, "x");
3520 p->state = IR_TCL_R_Reading;
3522 /* read incoming APDU */
3523 if ((r=cs_get (p->cs_link, &p->buf_in, &p->len_in)) == 1)
3525 logf(LOG_DEBUG, "PDU Fraction read");
3528 /* signal one more use of ir object - callbacks must not
3529 release the ir memory (p pointer) */
3533 logf (LOG_DEBUG, "cs_get failed, code %d", r);
3534 ir_select_remove (cs_fileno (p->cs_link), p);
3535 ir_tcl_disconnect (p);
3538 p->failInfo = IR_TCL_FAIL_READ;
3539 ir_tcl_eval (p->interp, p->failback);
3541 /* release ir object now if callback deleted it */
3545 /* got complete APDU. Now decode */
3548 odr_setbuf (p->odr_in, p->buf_in, r, 0);
3549 logf (LOG_DEBUG, "cs_get ok, total size %d", r);
3550 if (!z_APDU (p->odr_in, &apdu, 0))
3552 logf (LOG_DEBUG, "cs_get failed: %s",
3553 odr_errmsg (odr_geterror (p->odr_in)));
3554 ir_tcl_disconnect (p);
3557 p->failInfo = IR_TCL_FAIL_IN_APDU;
3558 p->apduOffset = odr_offset (p->odr_in);
3559 ir_tcl_eval (p->interp, p->failback);
3561 /* release ir object now if failback deleted it */
3565 /* handle APDU and invoke callback */
3566 rq = p->request_queue;
3569 logf (LOG_FATAL, "Internal error. No queue entry");
3572 object_name = rq->object_name;
3573 logf (LOG_DEBUG, "Object %s", object_name);
3575 if (Tcl_GetCommandInfo (p->interp, object_name, &cmd_info))
3579 case Z_APDU_initResponse:
3580 p->eventType = "init";
3581 ir_initResponse (p, apdu->u.initResponse);
3582 apdu_call = p->initResponse;
3584 case Z_APDU_searchResponse:
3585 p->eventType = "search";
3586 ir_searchResponse (p, apdu->u.searchResponse,
3587 (IrTcl_SetObj *) cmd_info.clientData);
3588 apdu_call = ((IrTcl_SetObj *)
3589 cmd_info.clientData)->searchResponse;
3591 case Z_APDU_presentResponse:
3592 p->eventType = "present";
3593 ir_presentResponse (p, apdu->u.presentResponse,
3594 (IrTcl_SetObj *) cmd_info.clientData);
3595 apdu_call = ((IrTcl_SetObj *)
3596 cmd_info.clientData)->presentResponse;
3598 case Z_APDU_scanResponse:
3599 p->eventType = "scan";
3600 ir_scanResponse (p, apdu->u.scanResponse,
3601 (IrTcl_ScanObj *) cmd_info.clientData);
3602 apdu_call = ((IrTcl_ScanObj *)
3603 cmd_info.clientData)->scanResponse;
3606 logf (LOG_WARN, "Received unknown APDU type (%d)",
3608 ir_tcl_disconnect (p);
3611 p->failInfo = IR_TCL_FAIL_UNKNOWN_APDU;
3612 ir_tcl_eval (p->interp, p->failback);
3617 p->request_queue = rq->next;
3618 p->state = IR_TCL_R_Idle;
3621 ir_tcl_eval (p->interp, apdu_call);
3622 else if (rq->callback)
3623 ir_tcl_eval (p->interp, rq->callback);
3625 free (rq->callback);
3626 free (rq->object_name);
3628 odr_reset (p->odr_in);
3629 if (p->ref_count == 1)
3635 } while (p->cs_link && cs_more (p->cs_link));
3636 if (p->cs_link && p->request_queue && p->state == IR_TCL_R_Idle)
3637 ir_tcl_send_q (p, p->request_queue, "x");
3641 * ir_select_write: handle outgoing packages - not yet written.
3643 static void ir_select_write (ClientData clientData)
3645 IrTcl_Obj *p = clientData;
3649 logf (LOG_DEBUG, "Write handler fd=%d", cs_fileno(p->cs_link));
3650 if (p->state == IR_TCL_R_Connecting)
3652 logf(LOG_DEBUG, "write: connect");
3653 r = cs_rcvconnect (p->cs_link);
3656 logf (LOG_DEBUG, "cs_rcvconnect returned 1");
3659 p->state = IR_TCL_R_Idle;
3661 ir_select_remove_write (cs_fileno (p->cs_link), p);
3664 logf (LOG_DEBUG, "cs_rcvconnect error");
3665 ir_tcl_disconnect (p);
3668 p->failInfo = IR_TCL_FAIL_CONNECT;
3669 ir_tcl_eval (p->interp, p->failback);
3675 ir_tcl_eval (p->interp, p->callback);
3679 rq = p->request_queue;
3680 if (!rq || !rq->buf_out)
3683 if ((r=cs_put (p->cs_link, rq->buf_out, rq->len_out)) < 0)
3685 logf (LOG_DEBUG, "cs_put write fail");
3689 ir_tcl_disconnect (p);
3692 p->failInfo = IR_TCL_FAIL_WRITE;
3693 ir_tcl_eval (p->interp, p->failback);
3697 else if (r == 0) /* remove select bit */
3699 logf (LOG_DEBUG, "Write completed");
3700 p->state = IR_TCL_R_Waiting;
3701 ir_select_remove_write (cs_fileno (p->cs_link), p);
3707 static void ir_select_notify (ClientData clientData, int r, int w, int e)
3710 ir_select_read (clientData);
3712 ir_select_write (clientData);
3715 /* ------------------------------------------------------- */
3718 * Irtcl_init: Registration of TCL commands.
3720 int Irtcl_Init (Tcl_Interp *interp)
3722 Tcl_CreateCommand (interp, "ir", ir_obj_mk, (ClientData) NULL,
3723 (Tcl_CmdDeleteProc *) NULL);
3724 Tcl_CreateCommand (interp, "ir-set", ir_set_obj_mk,
3725 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
3726 Tcl_CreateCommand (interp, "ir-scan", ir_scan_obj_mk,
3727 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);