2 * IR toolkit for tcl/tk
4 * See the file LICENSE for details.
5 * Sebastian Hammer, Adam Dickmeiss
8 * Revision 1.85 1996-03-15 11:15:48 adam
9 * Modified to use new prototypes for p_query_rpn and p_query_scan.
11 * Revision 1.84 1996/03/07 12:42:49 adam
12 * Better logging when callback is invoked.
14 * Revision 1.83 1996/03/05 09:21:09 adam
15 * Bug fix: memory used by GRS records wasn't freed.
16 * Rewrote some of the error handling code - the connection is always
17 * closed before failback is called.
18 * If failback is defined the send APDU methods (init, search, ...) will
19 * return OK but invoke failback (as is the case if the write operation
21 * Bug fix: ref_count in assoc object could grow if fraction of PDU was
24 * Revision 1.82 1996/02/29 15:30:21 adam
25 * Export of IrTcl functionality to extensions.
27 * Revision 1.81 1996/02/26 18:38:32 adam
28 * Work on export of set methods.
30 * Revision 1.80 1996/02/23 17:31:39 adam
31 * More functions made available to the wais tcl extension.
33 * Revision 1.79 1996/02/23 13:41:38 adam
34 * Work on public access to simple ir class system.
36 * Revision 1.78 1996/02/21 10:16:08 adam
37 * Simplified select handling. Only one function ir_tcl_select_set has
38 * to be externally defined.
40 * Revision 1.77 1996/02/20 17:52:58 adam
41 * Uses the YAZ oid system to name record syntax object identifiers.
43 * Revision 1.76 1996/02/20 16:09:51 adam
44 * Bug fix: didn't set element set names stamp correctly on result
45 * set records when element set names were set to the empty string.
47 * Revision 1.75 1996/02/19 15:41:53 adam
48 * Better log messages.
49 * Minor improvement of connect method.
51 * Revision 1.74 1996/02/05 17:58:03 adam
52 * Ported ir-tcl to use the beta releases of tcl7.5/tk4.1.
54 * Revision 1.73 1996/01/29 11:35:19 adam
55 * Bug fix: cs_type member renamed to comstackType to avoid conflict with
56 * cs_type macro defined by YAZ.
58 * Revision 1.72 1996/01/19 17:45:34 quinn
59 * Added debugging output
61 * Revision 1.71 1996/01/19 16:22:38 adam
62 * New method: apduDump - returns information about last incoming APDU.
64 * Revision 1.70 1996/01/10 09:18:34 adam
65 * PDU specific callbacks implemented: initRespnse, searchResponse,
66 * presentResponse and scanResponse.
67 * Bug fix in the command line shell (tclmain.c) - discovered on OSF/1.
69 * Revision 1.69 1996/01/04 16:12:12 adam
70 * Setting PDUType renamed to eventType.
72 * Revision 1.68 1996/01/04 11:05:22 adam
73 * New setting: PDUType - returns type of last PDU returned from the target.
74 * Fixed a bug in configure/Makefile.
76 * Revision 1.67 1996/01/03 09:00:51 adam
77 * Updated to use new version of Yaz (names changed to avoid C++ conflict).
79 * Revision 1.66 1995/11/28 17:26:39 adam
80 * Removed Carriage return from ir-tcl.c!
81 * Removed misc. debug logs.
83 * Revision 1.65 1995/11/28 13:53:00 quinn
86 * Revision 1.64 1995/11/13 15:39:18 adam
87 * Bug fix: {small,medium}SetElementSetNames weren't set correctly.
88 * Bug fix: idAuthentication weren't set correctly.
90 * Revision 1.63 1995/11/13 09:55:39 adam
91 * Multiple records at a position in a result-set with differnt
94 * Revision 1.62 1995/10/18 17:20:33 adam
95 * Work on target setup in client.tcl.
97 * Revision 1.61 1995/10/18 16:42:42 adam
98 * New settings: smallSetElementSetNames and mediumSetElementSetNames.
100 * Revision 1.60 1995/10/18 15:43:31 adam
101 * In search: mediumSetElementSetNames and smallSetElementSetNames are
102 * set to elementSetNames.
104 * Revision 1.59 1995/10/17 12:18:58 adam
105 * Bug fix: when target connection closed, the connection was not
106 * properly reestablished.
108 * Revision 1.58 1995/10/16 17:00:55 adam
109 * New setting: elementSetNames.
110 * Various client improvements. Medium presentation format looks better.
112 * Revision 1.57 1995/09/21 13:11:51 adam
113 * Support of dynamic loading.
114 * Test script uses load command if necessary.
116 * Revision 1.56 1995/08/29 15:30:14 adam
117 * Work on GRS records.
119 * Revision 1.55 1995/08/28 09:43:25 adam
120 * Minor changes. configure only searches for yaz beta 3 and versions after
123 * Revision 1.54 1995/08/24 12:25:16 adam
124 * Modified to work with yaz 1.0b3.
126 * Revision 1.53 1995/08/04 12:49:26 adam
127 * Bug fix: reading uninitialized variable p.
129 * Revision 1.52 1995/08/04 11:32:38 adam
130 * More work on output queue. Memory related routines moved
133 * Revision 1.51 1995/08/03 13:22:54 adam
136 * Revision 1.50 1995/07/20 08:09:49 adam
137 * client.tcl: Targets removed from hotTargets list when targets
138 * are removed/modified.
139 * ir-tcl.c: More work on triggerResourceControl.
141 * Revision 1.49 1995/06/30 12:39:21 adam
142 * Bug fix: loadFile didn't set record type.
143 * The MARC routines are a little less strict in the interpretation.
144 * Script display.tcl replaces the old marc.tcl.
145 * New interactive script: shell.tcl.
147 * Revision 1.48 1995/06/27 19:03:50 adam
148 * Bug fix in do_present in ir-tcl.c: p->set_child member weren't set.
149 * nextResultSetPosition used instead of setOffset.
151 * Revision 1.47 1995/06/25 10:25:04 adam
152 * Working on triggerResourceControl. Description of compile/install
153 * procedure moved to ir-tcl.sgml.
155 * Revision 1.46 1995/06/22 13:15:06 adam
156 * Feature: SUTRS. Setting getSutrs implemented.
157 * Work on display formats.
158 * Preferred record syntax can be set by the user.
160 * Revision 1.45 1995/06/20 08:07:30 adam
161 * New setting: failInfo.
162 * Working on better cancel mechanism.
164 * Revision 1.44 1995/06/19 17:01:20 adam
167 * Revision 1.43 1995/06/19 13:06:08 adam
168 * New define: IR_TCL_VERSION.
170 * Revision 1.42 1995/06/19 08:08:52 adam
171 * client.tcl: hotTargets now contain both database and target name.
172 * ir-tcl.c: setting protocol edited. Errors in callbacks are logged
173 * by logf(LOG_WARN, ...) calls.
175 * Revision 1.41 1995/06/16 12:28:16 adam
176 * Implemented preferredRecordSyntax.
177 * Minor changes in diagnostic handling.
178 * Record list deleted when connection closes.
180 * Revision 1.40 1995/06/14 13:37:18 adam
181 * Setting recordType implemented.
182 * Setting implementationVersion implemented.
183 * Settings implementationId / implementationName edited.
185 * Revision 1.39 1995/06/08 10:26:32 adam
186 * Bug fix in ir_strdup.
188 * Revision 1.38 1995/06/01 16:36:47 adam
189 * About buttons. Minor bug fixes.
191 * Revision 1.37 1995/06/01 07:31:20 adam
192 * Rename of many typedefs -> IrTcl_...
194 * Revision 1.36 1995/05/31 13:09:59 adam
195 * Client searches/presents may be interrupted.
196 * New moving book-logo.
198 * Revision 1.35 1995/05/31 08:36:33 adam
199 * Bug fix in client.tcl: didn't save options on clientrc.tcl.
200 * New method: referenceId. More work on scan.
202 * Revision 1.34 1995/05/29 10:33:42 adam
203 * README and rename of startup script.
205 * Revision 1.33 1995/05/29 09:15:11 quinn
206 * Changed CS_SR to PROTO_SR, etc.
208 * Revision 1.32 1995/05/29 08:44:16 adam
209 * Work on delete of objects.
211 * Revision 1.31 1995/05/26 11:44:10 adam
212 * Bugs fixed. More work on MARC utilities and queries. Test
213 * client is up-to-date again.
215 * Revision 1.30 1995/05/26 08:54:11 adam
216 * New MARC utilities. Uses prefix query.
218 * Revision 1.29 1995/05/24 14:10:22 adam
219 * Work on idAuthentication, protocolVersion and options.
221 * Revision 1.28 1995/05/23 15:34:48 adam
222 * Many new settings, userInformationField, smallSetUpperBound, etc.
223 * A number of settings are inherited when ir-set is executed.
224 * This version is incompatible with the graphical test client (client.tcl).
226 * Revision 1.27 1995/05/11 15:34:47 adam
227 * Scan request changed a bit. This version works with RLG.
229 * Revision 1.26 1995/04/18 16:11:51 adam
230 * First version of graphical Scan. Some work on query-by-form.
232 * Revision 1.25 1995/04/17 09:37:17 adam
233 * Further development of scan.
235 * Revision 1.24 1995/04/11 14:16:42 adam
236 * Further work on scan. Response works. Entries aren't saved yet.
238 * Revision 1.23 1995/04/10 10:50:27 adam
239 * Result-set name defaults to suffix of ir-set name.
240 * Started working on scan. Not finished at this point.
242 * Revision 1.22 1995/03/31 10:43:03 adam
243 * More robust when getting bad MARC records.
245 * Revision 1.21 1995/03/31 08:56:37 adam
246 * New button "Search".
248 * Revision 1.20 1995/03/29 16:07:09 adam
249 * Bug fix: Didn't use setName in present request.
251 * Revision 1.19 1995/03/28 12:45:23 adam
252 * New ir method failback: called on disconnect/protocol error.
253 * New ir set/get method: protocol: SR / Z3950.
254 * Simple popup and disconnect when failback is invoked.
256 * Revision 1.18 1995/03/21 15:50:12 adam
259 * Revision 1.17 1995/03/21 13:41:03 adam
260 * Comstack cs_create not used too often. Non-blocking connect.
262 * Revision 1.16 1995/03/21 08:26:06 adam
263 * New method, setName, to specify the result set name (other than Default).
264 * New method, responseStatus, which returns diagnostic info, if any, after
265 * present response / search response.
267 * Revision 1.15 1995/03/20 15:24:07 adam
268 * Diagnostic records saved on searchResponse.
270 * Revision 1.14 1995/03/20 08:53:22 adam
271 * Event loop in tclmain.c rewritten. New method searchStatus.
273 * Revision 1.13 1995/03/17 18:26:17 adam
274 * Non-blocking i/o used now. Database names popup as cascade items.
276 * Revision 1.12 1995/03/17 15:45:00 adam
277 * Improved target/database setup.
279 * Revision 1.11 1995/03/16 17:54:03 adam
280 * Minor changes really.
282 * Revision 1.10 1995/03/15 16:14:50 adam
283 * Blocking arg in cs_create changed.
285 * Revision 1.9 1995/03/15 13:59:24 adam
288 * Revision 1.8 1995/03/15 08:25:16 adam
289 * New method presentStatus to check for error on present. Misc. cleanup
290 * of IrTcl_RecordList manipulations. Full MARC record presentation in
293 * Revision 1.7 1995/03/14 17:32:29 adam
294 * Presentation of full Marc record in popup window.
296 * Revision 1.6 1995/03/12 19:31:55 adam
297 * Pattern matching implemented when retrieving MARC records. More
298 * diagnostic functions.
300 * Revision 1.5 1995/03/10 18:00:15 adam
301 * Actual presentation in line-by-line format. RPN query support.
303 * Revision 1.4 1995/03/09 16:15:08 adam
304 * First presentRequest attempts. Hot-target list.
316 #define IRTCL_GENERIC_FILES 0
320 static void ir_deleteDiags (IrTcl_Diagnostic **dst_list, int *dst_num);
322 static void ir_select_notify (ClientData clientData, int r, int w, int e);
324 void ir_select_add (int fd, void *obj)
326 ir_tcl_select_set (ir_select_notify, fd, obj, 1, 0, 0);
329 void ir_select_add_write (int fd, void *obj)
331 ir_tcl_select_set (ir_select_notify, fd, obj, 1, 1, 0);
334 void ir_select_remove (int fd, void *obj)
336 ir_tcl_select_set (NULL, fd, obj, 0, 0, 0);
339 void ir_select_remove_write (int fd, void *obj)
341 ir_tcl_select_set (ir_select_notify, fd, obj, 1, 0, 0);
344 static void delete_IR_record (IrTcl_RecordList *rl)
348 case Z_NamePlusRecord_databaseRecord:
349 switch (rl->u.dbrec.type)
352 ir_tcl_grs_del (&rl->u.dbrec.u.grs1);
357 free (rl->u.dbrec.buf);
359 case Z_NamePlusRecord_surrogateDiagnostic:
360 ir_deleteDiags (&rl->u.surrogateDiagnostics.list,
361 &rl->u.surrogateDiagnostics.num);
367 static IrTcl_RecordList *new_IR_record (IrTcl_SetObj *setobj,
369 const char *elements)
371 IrTcl_RecordList *rl;
373 if (elements && !*elements)
375 for (rl = setobj->record_list; rl; rl = rl->next)
377 if (no == rl->no && (!rl->elements || !elements ||
378 !strcmp(elements, rl->elements)))
380 delete_IR_record (rl);
386 rl = ir_tcl_malloc (sizeof(*rl));
387 rl->next = setobj->record_list;
389 setobj->record_list = rl;
392 ir_tcl_strdup (NULL, &rl->elements, elements);
399 int ir_tcl_eval (Tcl_Interp *interp, const char *command)
401 char *tmp = ir_tcl_malloc (strlen(command)+1);
404 logf (LOG_DEBUG, "Invoking %.23s ...", command);
405 strcpy (tmp, command);
406 r = Tcl_Eval (interp, tmp);
408 logf (LOG_WARN, "Tcl error in line %d: %s", interp->errorLine,
410 Tcl_FreeResult (interp);
416 * IrTcl_getRecordSyntaxStr: Return record syntax name of object id
418 static char *IrTcl_getRecordSyntaxStr (enum oid_value value)
421 struct oident ent, *entp;
423 ent.proto = PROTO_Z3950;
424 ent.oclass = CLASS_RECSYN;
427 o = oid_getoidbyent (&ent);
428 entp = oid_getentbyoid (o);
436 * IrTcl_getRecordSyntaxVal: Return record syntax value of string
438 static enum oid_value IrTcl_getRecordSyntaxVal (const char *name)
440 return oid_getvalbyname (name);
443 static IrTcl_RecordList *find_IR_record (IrTcl_SetObj *setobj, int no)
445 IrTcl_RecordList *rl;
447 for (rl = setobj->record_list; rl; rl = rl->next)
449 (!setobj->recordElements || !rl->elements ||
450 !strcmp (setobj->recordElements, rl->elements)))
455 static void delete_IR_records (IrTcl_SetObj *setobj)
457 IrTcl_RecordList *rl, *rl1;
459 for (rl = setobj->record_list; rl; rl = rl1)
461 delete_IR_record (rl);
465 setobj->record_list = NULL;
469 * ir_tcl_get_set_int: Set/get integer value
471 int ir_tcl_get_set_int (int *val, Tcl_Interp *interp, int argc, char **argv)
477 if (Tcl_GetInt (interp, argv[2], val)==TCL_ERROR)
480 sprintf (buf, "%d", *val);
481 Tcl_AppendResult (interp, buf, NULL);
486 * ir_tcl_method: Search for method in table and invoke method handler
488 int ir_tcl_method (Tcl_Interp *interp, int argc, char **argv,
489 IrTcl_Methods *tab, int *ret)
491 IrTcl_Methods *tab_i = tab;
494 for (tab_i = tab; tab_i->tab; tab_i++)
495 for (t = tab_i->tab; t->name; t++)
498 if ((*t->method)(tab_i->obj, interp, argc, argv) == TCL_ERROR)
502 if (!strcmp (t->name, argv[1]))
504 *ret = (*t->method)(tab_i->obj, interp, argc, argv);
511 Tcl_AppendResult (interp, "Bad method: ", argv[1],
512 ". Possible methods:", NULL);
513 for (tab_i = tab; tab_i->tab; tab_i++)
514 for (t = tab_i->tab; t->name; t++)
515 Tcl_AppendResult (interp, " ", t->name, NULL);
522 * ir_tcl_named_bits: get/set named bits
524 int ir_tcl_named_bits (struct ir_named_entry *tab, Odr_bitmask *ob,
525 Tcl_Interp *interp, int argc, char **argv)
527 struct ir_named_entry *ti;
532 for (no = 0; no < argc; no++)
534 for (ti = tab; ti->name; ti++)
535 if (!strcmp (argv[no], ti->name))
537 ODR_MASK_SET (ob, ti->pos);
542 Tcl_AppendResult (interp, "Bad bit mask: ", argv[no], NULL);
548 for (ti = tab; ti->name; ti++)
549 if (ODR_MASK_GET (ob, ti->pos))
550 Tcl_AppendElement (interp, ti->name);
554 static void set_referenceId (ODR o, Z_ReferenceId **dst, const char *src)
560 *dst = odr_malloc (o, sizeof(**dst));
561 (*dst)->size = (*dst)->len = strlen(src);
562 (*dst)->buf = odr_malloc (o, (*dst)->len);
563 memcpy ((*dst)->buf, src, (*dst)->len);
567 static void get_referenceId (char **dst, Z_ReferenceId *src)
575 *dst = ir_tcl_malloc (src->len+1);
576 memcpy (*dst, src->buf, src->len);
577 (*dst)[src->len] = '\0';
580 /* ------------------------------------------------------- */
583 * do_init_request: init method on IR object
585 static int do_init_request (void *obj, Tcl_Interp *interp,
586 int argc, char **argv)
596 interp->result = "init: not connected";
599 apdu = zget_APDU (p->odr_out, Z_APDU_initRequest);
600 req = apdu->u.initRequest;
602 set_referenceId (p->odr_out, &req->referenceId, p->set_inher.referenceId);
603 req->options = &p->options;
604 req->protocolVersion = &p->protocolVersion;
605 req->preferredMessageSize = &p->preferredMessageSize;
606 req->maximumRecordSize = &p->maximumRecordSize;
608 if (p->idAuthenticationGroupId)
610 Z_IdPass *pass = odr_malloc (p->odr_out, sizeof(*pass));
611 Z_IdAuthentication *auth = odr_malloc (p->odr_out, sizeof(*auth));
613 logf (LOG_DEBUG, "using pass authentication");
615 auth->which = Z_IdAuthentication_idPass;
616 auth->u.idPass = pass;
617 if (p->idAuthenticationGroupId && *p->idAuthenticationGroupId)
618 pass->groupId = p->idAuthenticationGroupId;
620 pass->groupId = NULL;
621 if (p->idAuthenticationUserId && *p->idAuthenticationUserId)
622 pass->userId = p->idAuthenticationUserId;
625 if (p->idAuthenticationPassword && *p->idAuthenticationPassword)
626 pass->password = p->idAuthenticationPassword;
628 pass->password = NULL;
629 req->idAuthentication = auth;
631 else if (!p->idAuthenticationOpen || !*p->idAuthenticationOpen)
632 req->idAuthentication = NULL;
635 Z_IdAuthentication *auth = odr_malloc (p->odr_out, sizeof(*auth));
637 logf (LOG_DEBUG, "using open authentication");
638 auth->which = Z_IdAuthentication_open;
639 auth->u.open = p->idAuthenticationOpen;
640 req->idAuthentication = auth;
642 req->implementationId = p->implementationId;
643 req->implementationName = p->implementationName;
644 req->implementationVersion = p->implementationVersion;
645 req->userInformationField = 0;
647 return ir_tcl_send_APDU (interp, p, apdu, "init", *argv);
651 * do_protocolVersion: Set protocol Version
653 static int do_protocolVersion (void *obj, Tcl_Interp *interp,
654 int argc, char **argv)
662 ODR_MASK_ZERO (&p->protocolVersion);
663 ODR_MASK_SET (&p->protocolVersion, 0);
664 ODR_MASK_SET (&p->protocolVersion, 1);
669 if (Tcl_GetInt (interp, argv[2], &version)==TCL_ERROR)
671 ODR_MASK_ZERO (&p->protocolVersion);
672 for (i = 0; i<version; i++)
673 ODR_MASK_SET (&p->protocolVersion, i);
675 for (i = 4; --i >= 0; )
676 if (ODR_MASK_GET (&p->protocolVersion, i))
678 sprintf (buf, "%d", i+1);
679 interp->result = buf;
684 * do_options: Set options
686 static int do_options (void *obj, Tcl_Interp *interp,
687 int argc, char **argv)
689 static struct ir_named_entry options_tab[] = {
693 { "resourceReport", 3 },
694 { "triggerResourceCtrl", 4},
695 { "resourceCtrl", 5},
699 { "extendedServices", 10},
700 { "level-1Segmentation", 11},
701 { "level-2Segmentation", 12},
702 { "concurrentOperations", 13},
703 { "namedResultSets", 14},
710 ODR_MASK_ZERO (&p->options);
711 ODR_MASK_SET (&p->options, 0);
712 ODR_MASK_SET (&p->options, 1);
713 ODR_MASK_SET (&p->options, 4);
714 ODR_MASK_SET (&p->options, 7);
715 ODR_MASK_SET (&p->options, 14);
718 return ir_tcl_named_bits (options_tab, &p->options, interp, argc-2, argv+2);
722 * do_apduInfo: Get APDU information
724 static int do_apduInfo (void *obj, Tcl_Interp *interp, int argc, char **argv)
732 sprintf (buf, "%d", p->apduLen);
733 Tcl_AppendElement (interp, buf);
734 sprintf (buf, "%d", p->apduOffset);
735 Tcl_AppendElement (interp, buf);
738 Tcl_AppendElement (interp, "");
741 apduf = fopen ("apdu.tmp", "w");
744 Tcl_AppendElement (interp, "");
747 odr_dumpBER (apduf, p->buf_in, p->apduLen);
749 if (!(apduf = fopen ("apdu.tmp", "r")))
750 Tcl_AppendElement (interp, "");
755 Tcl_AppendResult (interp, " {", NULL);
756 while ((c = getc (apduf)) != EOF)
760 Tcl_AppendResult (interp, buf, NULL);
763 Tcl_AppendResult (interp, "}", NULL);
770 * do_failInfo: Get fail information
772 static int do_failInfo (void *obj, Tcl_Interp *interp, int argc, char **argv)
782 sprintf (buf, "%d", p->failInfo);
788 case IR_TCL_FAIL_CONNECT:
789 cp = "connect failed";
791 case IR_TCL_FAIL_READ:
792 cp = "connection closed";
794 case IR_TCL_FAIL_WRITE:
795 cp = "connection closed";
797 case IR_TCL_FAIL_IN_APDU:
798 cp = "failed to decode incoming APDU";
800 case IR_TCL_FAIL_UNKNOWN_APDU:
806 Tcl_AppendElement (interp, buf);
807 Tcl_AppendElement (interp, cp);
812 * do_preferredMessageSize: Set/get preferred message size
814 static int do_preferredMessageSize (void *obj, Tcl_Interp *interp,
815 int argc, char **argv)
821 p->preferredMessageSize = 30000;
824 return ir_tcl_get_set_int (&p->preferredMessageSize, interp, argc, argv);
828 * do_maximumRecordSize: Set/get maximum record size
830 static int do_maximumRecordSize (void *obj, Tcl_Interp *interp,
831 int argc, char **argv)
837 p->maximumRecordSize = 30000;
840 return ir_tcl_get_set_int (&p->maximumRecordSize, interp, argc, argv);
844 * do_initResult: Get init result
846 static int do_initResult (void *obj, Tcl_Interp *interp,
847 int argc, char **argv)
853 return ir_tcl_get_set_int (&p->initResult, interp, argc, argv);
858 * do_implementationName: Set/get Implementation Name.
860 static int do_implementationName (void *obj, Tcl_Interp *interp,
861 int argc, char **argv)
866 return ir_tcl_strdup (interp, &p->implementationName,
867 "Index Data/IrTcl on YAZ");
869 return ir_tcl_strdel (interp, &p->implementationName);
872 free (p->implementationName);
873 if (ir_tcl_strdup (interp, &p->implementationName, argv[2])
877 Tcl_AppendResult (interp, p->implementationName, (char*) NULL);
882 * do_implementationId: Get Implementation Id.
884 static int do_implementationId (void *obj, Tcl_Interp *interp,
885 int argc, char **argv)
890 return ir_tcl_strdup (interp, &p->implementationId, "YAZ (id=81)");
892 return ir_tcl_strdel (interp, &p->implementationId);
893 Tcl_AppendResult (interp, p->implementationId, (char*) NULL);
898 * do_implementationVersion: get Implementation Version.
900 static int do_implementationVersion (void *obj, Tcl_Interp *interp,
901 int argc, char **argv)
906 return ir_tcl_strdup (interp, &p->implementationVersion,
907 "YAZ: " YAZ_VERSION " / IrTcl: " IR_TCL_VERSION);
909 return ir_tcl_strdel (interp, &p->implementationVersion);
910 Tcl_AppendResult (interp, p->implementationVersion, (char*) NULL);
915 * do_targetImplementationName: Get Implementation Name of target.
917 static int do_targetImplementationName (void *obj, Tcl_Interp *interp,
918 int argc, char **argv)
924 p->targetImplementationName = NULL;
928 return ir_tcl_strdel (interp, &p->targetImplementationName);
929 Tcl_AppendResult (interp, p->targetImplementationName, (char*) NULL);
934 * do_targetImplementationId: Get Implementation Id of target
936 static int do_targetImplementationId (void *obj, Tcl_Interp *interp,
937 int argc, char **argv)
943 p->targetImplementationId = NULL;
947 return ir_tcl_strdel (interp, &p->targetImplementationId);
948 Tcl_AppendResult (interp, p->targetImplementationId, (char*) NULL);
953 * do_targetImplementationVersion: Get Implementation Version of target
955 static int do_targetImplementationVersion (void *obj, Tcl_Interp *interp,
956 int argc, char **argv)
962 p->targetImplementationVersion = NULL;
966 return ir_tcl_strdel (interp, &p->targetImplementationVersion);
967 Tcl_AppendResult (interp, p->targetImplementationVersion, (char*) NULL);
972 * do_idAuthentication: Set/get id Authentication
974 static int do_idAuthentication (void *obj, Tcl_Interp *interp,
975 int argc, char **argv)
979 if (argc >= 3 || argc == -1)
981 free (p->idAuthenticationOpen);
982 free (p->idAuthenticationGroupId);
983 free (p->idAuthenticationUserId);
984 free (p->idAuthenticationPassword);
986 if (argc >= 3 || argc <= 0)
988 p->idAuthenticationOpen = NULL;
989 p->idAuthenticationGroupId = NULL;
990 p->idAuthenticationUserId = NULL;
991 p->idAuthenticationPassword = NULL;
1000 ir_tcl_strdup (interp, &p->idAuthenticationOpen, argv[2])
1007 ir_tcl_strdup (interp, &p->idAuthenticationGroupId, argv[2])
1011 ir_tcl_strdup (interp, &p->idAuthenticationUserId, argv[3])
1015 ir_tcl_strdup (interp, &p->idAuthenticationPassword, argv[4])
1020 if (p->idAuthenticationOpen)
1021 Tcl_AppendElement (interp, p->idAuthenticationOpen);
1022 else if (p->idAuthenticationGroupId)
1024 Tcl_AppendElement (interp, p->idAuthenticationGroupId);
1025 Tcl_AppendElement (interp, p->idAuthenticationUserId);
1026 Tcl_AppendElement (interp, p->idAuthenticationPassword);
1032 * do_connect: connect method on IR object
1034 static int do_connect (void *obj, Tcl_Interp *interp,
1035 int argc, char **argv)
1047 interp->result = "already connected";
1050 if (!strcmp (p->comstackType, "tcpip"))
1052 p->cs_link = cs_create (tcpip_type, CS_BLOCK, p->protocol_type);
1053 addr = tcpip_strtoaddr (argv[2]);
1056 interp->result = "tcpip_strtoaddr fail";
1059 logf (LOG_DEBUG, "tcp/ip connect %s", argv[2]);
1061 else if (!strcmp (p->comstackType, "mosi"))
1064 p->cs_link = cs_create (mosi_type, CS_BLOCK, p->protocol_type);
1065 addr = mosi_strtoaddr (argv[2]);
1068 interp->result = "mosi_strtoaddr fail";
1071 logf (LOG_DEBUG, "mosi connect %s", argv[2]);
1073 interp->result = "MOSI support not there";
1079 Tcl_AppendResult (interp, "Bad comstack type: ",
1080 p->comstackType, NULL);
1083 if (ir_tcl_strdup (interp, &p->hostname, argv[2]) == TCL_ERROR)
1085 #if IRTCL_GENERIC_FILES
1087 p->csFile = Tcl_GetFile (cs_fileno(p->cs_link), TCL_WIN_SOCKET);
1089 p->csFile = Tcl_GetFile (cs_fileno(p->cs_link), TCL_UNIX_FD);
1092 if ((r=cs_connect (p->cs_link, addr)) < 0)
1094 interp->result = "connect fail";
1095 ir_tcl_disconnect (p);
1098 logf(LOG_DEBUG, "cs_connect() returned %d fd=%d", r,
1099 cs_fileno(p->cs_link));
1100 p->eventType = "connect";
1101 #if IRTCL_GENERIC_FILES
1102 ir_select_add (p->csFile, p);
1104 ir_select_add (cs_fileno (p->cs_link), p);
1108 #if IRTCL_GENERIC_FILES
1109 ir_select_add_write (p->csFile, p);
1111 ir_select_add_write (cs_fileno (p->cs_link), p);
1113 p->state = IR_TCL_R_Connecting;
1117 p->state = IR_TCL_R_Idle;
1119 ir_tcl_eval (p->interp, p->callback);
1123 Tcl_AppendResult (interp, p->hostname, NULL);
1128 * ir_tcl_disconnect: close connection
1130 void ir_tcl_disconnect (IrTcl_Obj *p)
1134 logf(LOG_DEBUG, "Closing connection to %s", p->hostname);
1137 #if IRTCL_GENERIC_FILES
1138 ir_select_remove_write (p->csFile, p);
1139 ir_select_remove (p->csFile, p);
1141 ir_select_remove_write (cs_fileno (p->cs_link), p);
1142 ir_select_remove (cs_fileno (p->cs_link), p);
1145 odr_reset (p->odr_in);
1147 assert (p->cs_link);
1148 cs_close (p->cs_link);
1150 #if IRTCL_GENERIC_FILES
1151 Tcl_FreeFile (p->csFile);
1155 ODR_MASK_ZERO (&p->options);
1156 ODR_MASK_SET (&p->options, 0);
1157 ODR_MASK_SET (&p->options, 1);
1158 ODR_MASK_SET (&p->options, 4);
1159 ODR_MASK_SET (&p->options, 7);
1160 ODR_MASK_SET (&p->options, 14);
1162 ODR_MASK_ZERO (&p->protocolVersion);
1163 ODR_MASK_SET (&p->protocolVersion, 0);
1164 ODR_MASK_SET (&p->protocolVersion, 1);
1167 assert (!p->cs_link);
1171 * do_disconnect: disconnect method on IR object
1173 static int do_disconnect (void *obj, Tcl_Interp *interp,
1174 int argc, char **argv)
1180 p->state = IR_TCL_R_Idle;
1181 p->eventType = NULL;
1184 #if IRTCL_GENERIC_FILES
1189 ir_tcl_disconnect (p);
1194 * do_comstack: Set/get comstack method on IR object
1196 static int do_comstack (void *o, Tcl_Interp *interp,
1197 int argc, char **argv)
1202 return ir_tcl_strdup (interp, &obj->comstackType, "tcpip");
1203 else if (argc == -1)
1204 return ir_tcl_strdel (interp, &obj->comstackType);
1207 free (obj->comstackType);
1208 if (ir_tcl_strdup (interp, &obj->comstackType, argv[2]) == TCL_ERROR)
1211 Tcl_AppendElement (interp, obj->comstackType);
1216 * do_logLevel: Set log level
1218 static int do_logLevel (void *o, Tcl_Interp *interp,
1219 int argc, char **argv)
1224 log_init (log_mask_str (argv[2]), "", NULL);
1226 log_init (log_mask_str (argv[2]), argv[3], NULL);
1228 log_init (log_mask_str (argv[2]), argv[3], argv[4]);
1234 * do_eventType: Return type of last event
1236 static int do_eventType (void *obj, Tcl_Interp *interp,
1237 int argc, char **argv)
1243 p->eventType = NULL;
1246 Tcl_AppendElement (interp, p->eventType ? p->eventType : "");
1252 * do_callback: add callback
1254 static int do_callback (void *obj, Tcl_Interp *interp,
1255 int argc, char **argv)
1264 else if (argc == -1)
1265 return ir_tcl_strdel (interp, &p->callback);
1271 if (ir_tcl_strdup (interp, &p->callback, argv[2]) == TCL_ERROR)
1281 * do_failback: add error handle callback
1283 static int do_failback (void *obj, Tcl_Interp *interp,
1284 int argc, char **argv)
1293 else if (argc == -1)
1294 return ir_tcl_strdel (interp, &p->failback);
1300 if (ir_tcl_strdup (interp, &p->failback, argv[2]) == TCL_ERROR)
1310 * do_initResponse: add init response handler
1312 static int do_initResponse (void *obj, Tcl_Interp *interp,
1313 int argc, char **argv)
1319 p->initResponse = NULL;
1322 else if (argc == -1)
1323 return ir_tcl_strdel (interp, &p->initResponse);
1326 free (p->initResponse);
1329 if (ir_tcl_strdup (interp, &p->initResponse, argv[2]) == TCL_ERROR)
1333 p->initResponse = NULL;
1338 * do_protocol: Set/get protocol method on IR object
1340 static int do_protocol (void *o, Tcl_Interp *interp, int argc, char **argv)
1346 p->protocol_type = PROTO_Z3950;
1351 if (!strcmp (argv[2], "Z39"))
1352 p->protocol_type = PROTO_Z3950;
1353 else if (!strcmp (argv[2], "SR"))
1354 p->protocol_type = PROTO_SR;
1357 Tcl_AppendResult (interp, "Bad protocol: ", argv[2], NULL);
1362 switch (p->protocol_type)
1365 Tcl_AppendElement (interp, "Z39");
1368 Tcl_AppendElement (interp, "SR");
1375 * do_triggerResourceControl:
1377 static int do_triggerResourceControl (void *obj, Tcl_Interp *interp,
1378 int argc, char **argv)
1382 Z_TriggerResourceControlRequest *req;
1383 bool_t is_false = 0;
1389 interp->result = "triggerResourceControl: not connected";
1392 apdu = zget_APDU (p->odr_out, Z_APDU_triggerResourceControlRequest);
1393 req = apdu->u.triggerResourceControlRequest;
1394 *req->requestedAction = Z_TriggerResourceCtrl_cancel;
1395 req->resultSetWanted = &is_false;
1397 return ir_tcl_send_APDU (interp, p, apdu, "triggerResourceControl",
1402 * do_databaseNames: specify database names
1404 static int do_databaseNames (void *obj, Tcl_Interp *interp,
1405 int argc, char **argv)
1408 IrTcl_SetCObj *p = obj;
1412 for (i=0; i<p->num_databaseNames; i++)
1413 free (p->databaseNames[i]);
1414 free (p->databaseNames);
1418 p->num_databaseNames = 0;
1419 p->databaseNames = NULL;
1424 for (i=0; i<p->num_databaseNames; i++)
1425 Tcl_AppendElement (interp, p->databaseNames[i]);
1428 if (p->databaseNames)
1430 for (i=0; i<p->num_databaseNames; i++)
1431 free (p->databaseNames[i]);
1432 free (p->databaseNames);
1434 p->num_databaseNames = argc - 2;
1436 ir_tcl_malloc (sizeof(*p->databaseNames) * (1+p->num_databaseNames));
1437 for (i=0; i<p->num_databaseNames; i++)
1439 if (ir_tcl_strdup (interp, &p->databaseNames[i], argv[2+i])
1443 p->databaseNames[i] = NULL;
1448 * do_replaceIndicator: Set/get replace Set indicator
1450 static int do_replaceIndicator (void *obj, Tcl_Interp *interp,
1451 int argc, char **argv)
1453 IrTcl_SetCObj *p = obj;
1457 p->replaceIndicator = 1;
1460 return ir_tcl_get_set_int (&p->replaceIndicator, interp, argc, argv);
1464 * do_queryType: Set/Get query method
1466 static int do_queryType (void *obj, Tcl_Interp *interp,
1467 int argc, char **argv)
1469 IrTcl_SetCObj *p = obj;
1472 return ir_tcl_strdup (interp, &p->queryType, "rpn");
1473 else if (argc == -1)
1474 return ir_tcl_strdel (interp, &p->queryType);
1477 free (p->queryType);
1478 if (ir_tcl_strdup (interp, &p->queryType, argv[2]) == TCL_ERROR)
1481 Tcl_AppendResult (interp, p->queryType, NULL);
1486 * do_userInformationField: Get User information field
1488 static int do_userInformationField (void *obj, Tcl_Interp *interp,
1489 int argc, char **argv)
1495 p->userInformationField = NULL;
1498 else if (argc == -1)
1499 return ir_tcl_strdel (interp, &p->userInformationField);
1500 Tcl_AppendResult (interp, p->userInformationField, NULL);
1505 * do_smallSetUpperBound: Set/get small set upper bound
1507 static int do_smallSetUpperBound (void *o, Tcl_Interp *interp,
1508 int argc, char **argv)
1510 IrTcl_SetCObj *p = o;
1514 p->smallSetUpperBound = 0;
1517 return ir_tcl_get_set_int (&p->smallSetUpperBound, interp, argc, argv);
1521 * do_largeSetLowerBound: Set/get large set lower bound
1523 static int do_largeSetLowerBound (void *o, Tcl_Interp *interp,
1524 int argc, char **argv)
1526 IrTcl_SetCObj *p = o;
1530 p->largeSetLowerBound = 2;
1533 return ir_tcl_get_set_int (&p->largeSetLowerBound, interp, argc, argv);
1537 * do_mediumSetPresentNumber: Set/get large set lower bound
1539 static int do_mediumSetPresentNumber (void *o, Tcl_Interp *interp,
1540 int argc, char **argv)
1542 IrTcl_SetCObj *p = o;
1546 p->mediumSetPresentNumber = 0;
1549 return ir_tcl_get_set_int (&p->mediumSetPresentNumber, interp, argc, argv);
1553 * do_referenceId: Set/Get referenceId
1555 static int do_referenceId (void *obj, Tcl_Interp *interp,
1556 int argc, char **argv)
1558 IrTcl_SetCObj *p = obj;
1562 p->referenceId = NULL;
1565 else if (argc == -1)
1566 return ir_tcl_strdel (interp, &p->referenceId);
1569 free (p->referenceId);
1570 if (ir_tcl_strdup (interp, &p->referenceId, argv[2]) == TCL_ERROR)
1573 Tcl_AppendResult (interp, p->referenceId, NULL);
1578 * do_preferredRecordSyntax: Set/get preferred record syntax
1580 static int do_preferredRecordSyntax (void *obj, Tcl_Interp *interp,
1581 int argc, char **argv)
1583 IrTcl_SetCObj *p = obj;
1587 p->preferredRecordSyntax = NULL;
1590 else if (argc == -1)
1592 free (p->preferredRecordSyntax);
1593 p->preferredRecordSyntax = NULL;
1598 free (p->preferredRecordSyntax);
1599 p->preferredRecordSyntax = NULL;
1600 if (argv[2][0] && (p->preferredRecordSyntax =
1601 ir_tcl_malloc (sizeof(*p->preferredRecordSyntax))))
1602 *p->preferredRecordSyntax = IrTcl_getRecordSyntaxVal (argv[2]);
1607 (interp,!p->preferredRecordSyntax ? "" :
1608 IrTcl_getRecordSyntaxStr(*p->preferredRecordSyntax));
1615 * do_elementSetNames: Set/Get element Set Names
1617 static int do_elementSetNames (void *obj, Tcl_Interp *interp,
1618 int argc, char **argv)
1620 IrTcl_SetCObj *p = obj;
1624 p->elementSetNames = NULL;
1627 else if (argc == -1)
1628 return ir_tcl_strdel (interp, &p->elementSetNames);
1631 free (p->elementSetNames);
1632 if (ir_tcl_strdup (interp, &p->elementSetNames, argv[2]) == TCL_ERROR)
1635 Tcl_AppendResult (interp, p->elementSetNames, NULL);
1640 * do_smallSetElementSetNames: Set/Get small Set Element Set Names
1642 static int do_smallSetElementSetNames (void *obj, Tcl_Interp *interp,
1643 int argc, char **argv)
1645 IrTcl_SetCObj *p = obj;
1649 p->smallSetElementSetNames = NULL;
1652 else if (argc == -1)
1653 return ir_tcl_strdel (interp, &p->smallSetElementSetNames);
1656 free (p->smallSetElementSetNames);
1657 if (ir_tcl_strdup (interp, &p->smallSetElementSetNames,
1658 argv[2]) == TCL_ERROR)
1661 Tcl_AppendResult (interp, p->smallSetElementSetNames, NULL);
1666 * do_mediumSetElementSetNames: Set/Get medium Set Element Set Names
1668 static int do_mediumSetElementSetNames (void *obj, Tcl_Interp *interp,
1669 int argc, char **argv)
1671 IrTcl_SetCObj *p = obj;
1675 p->mediumSetElementSetNames = NULL;
1678 else if (argc == -1)
1679 return ir_tcl_strdel (interp, &p->mediumSetElementSetNames);
1682 free (p->mediumSetElementSetNames);
1683 if (ir_tcl_strdup (interp, &p->mediumSetElementSetNames,
1684 argv[2]) == TCL_ERROR)
1687 Tcl_AppendResult (interp, p->mediumSetElementSetNames, NULL);
1691 static IrTcl_Method ir_method_tab[] = {
1692 { "comstack", do_comstack, NULL },
1693 { "protocol", do_protocol, NULL },
1694 { "failback", do_failback, NULL },
1695 { "failInfo", do_failInfo, NULL },
1696 { "apduInfo", do_apduInfo, NULL },
1697 { "logLevel", do_logLevel, NULL },
1699 { "eventType", do_eventType, NULL },
1700 { "connect", do_connect, NULL },
1701 { "protocolVersion", do_protocolVersion, NULL },
1702 { "preferredMessageSize", do_preferredMessageSize, NULL },
1703 { "maximumRecordSize", do_maximumRecordSize, NULL },
1704 { "implementationName", do_implementationName, NULL },
1705 { "implementationId", do_implementationId, NULL },
1706 { "implementationVersion", do_implementationVersion, NULL },
1707 { "targetImplementationName", do_targetImplementationName, NULL },
1708 { "targetImplementationId", do_targetImplementationId, NULL },
1709 { "targetImplementationVersion", do_targetImplementationVersion, NULL},
1710 { "userInformationField", do_userInformationField, NULL},
1711 { "idAuthentication", do_idAuthentication, NULL},
1712 { "options", do_options, NULL},
1713 { "init", do_init_request, NULL},
1714 { "initResult", do_initResult, NULL},
1715 { "disconnect", do_disconnect, NULL},
1716 { "callback", do_callback, NULL},
1717 { "initResponse", do_initResponse, NULL},
1718 { "triggerResourceControl", do_triggerResourceControl, NULL},
1719 { "initResponse", do_initResponse, NULL},
1723 static IrTcl_Method ir_set_c_method_tab[] = {
1724 { "databaseNames", do_databaseNames, NULL},
1725 { "replaceIndicator", do_replaceIndicator, NULL},
1726 { "queryType", do_queryType, NULL},
1727 { "preferredRecordSyntax", do_preferredRecordSyntax, NULL},
1728 { "smallSetUpperBound", do_smallSetUpperBound, NULL},
1729 { "largeSetLowerBound", do_largeSetLowerBound, NULL},
1730 { "mediumSetPresentNumber", do_mediumSetPresentNumber, NULL},
1731 { "referenceId", do_referenceId, NULL},
1732 { "elementSetNames", do_elementSetNames, NULL},
1733 { "smallSetElementSetNames", do_smallSetElementSetNames, NULL},
1734 { "mediumSetElementSetNames", do_mediumSetElementSetNames, NULL},
1739 * ir_obj_method: IR Object methods
1741 static int ir_obj_method (ClientData clientData, Tcl_Interp *interp,
1742 int argc, char **argv)
1744 IrTcl_Methods tab[3];
1745 IrTcl_Obj *p = clientData;
1751 tab[0].tab = ir_method_tab;
1753 tab[1].tab = ir_set_c_method_tab;
1754 tab[1].obj = &p->set_inher;
1757 ir_tcl_method (interp, argc, argv, tab, &r);
1762 * ir_obj_delete: IR Object disposal
1764 static void ir_obj_delete (ClientData clientData)
1766 IrTcl_Obj *obj = clientData;
1767 IrTcl_Methods tab[3];
1770 if (obj->ref_count > 0)
1772 assert (obj->ref_count == 0);
1774 logf (LOG_DEBUG, "ir object delete");
1775 tab[0].tab = ir_method_tab;
1777 tab[1].tab = ir_set_c_method_tab;
1778 tab[1].obj = &obj->set_inher;
1781 ir_tcl_method (NULL, -1, NULL, tab, NULL);
1784 odr_destroy (obj->odr_in);
1785 odr_destroy (obj->odr_out);
1786 odr_destroy (obj->odr_pr);
1791 * ir_obj_init: IR Object initialization
1793 int ir_obj_init (ClientData clientData, Tcl_Interp *interp,
1794 int argc, char **argv, ClientData *subData,
1795 ClientData parentData)
1797 IrTcl_Methods tab[3];
1805 interp->result = "wrong # args";
1808 obj = ir_tcl_malloc (sizeof(*obj));
1811 obj->bibset = ccl_qual_mk ();
1812 if ((inf = fopen ("default.bib", "r")))
1814 ccl_qual_file (obj->bibset, inf);
1819 logf (LOG_DEBUG, "ir object create %s", argv[1]);
1820 obj->odr_in = odr_createmem (ODR_DECODE);
1821 obj->odr_out = odr_createmem (ODR_ENCODE);
1822 obj->odr_pr = odr_createmem (ODR_PRINT);
1823 obj->state = IR_TCL_R_Idle;
1824 obj->interp = interp;
1828 obj->request_queue = NULL;
1830 tab[0].tab = ir_method_tab;
1832 tab[1].tab = ir_set_c_method_tab;
1833 tab[1].obj = &obj->set_inher;
1836 if (ir_tcl_method (interp, 0, NULL, tab, NULL) == TCL_ERROR)
1838 Tcl_AppendResult (interp, "Failed to initialize ", argv[1], NULL);
1847 * ir_obj_mk: IR Object creation
1849 static int ir_obj_mk (ClientData clientData, Tcl_Interp *interp,
1850 int argc, char **argv)
1853 int r = ir_obj_init (clientData, interp, argc, argv, &subData, 0);
1857 Tcl_CreateCommand (interp, argv[1], ir_obj_method,
1858 subData, ir_obj_delete);
1862 IrTcl_Class ir_obj_class = {
1870 /* ------------------------------------------------------- */
1872 * do_search: Do search request
1874 static int do_search (void *o, Tcl_Interp *interp, int argc, char **argv)
1876 Z_SearchRequest *req;
1880 IrTcl_SetObj *obj = o;
1890 interp->result = "wrong # args";
1893 if (!obj->set_inher.num_databaseNames)
1895 interp->result = "no databaseNames";
1900 interp->result = "search: not connected";
1903 apdu = zget_APDU (p->odr_out, Z_APDU_searchRequest);
1904 req = apdu->u.searchRequest;
1908 set_referenceId (p->odr_out, &req->referenceId,
1909 obj->set_inher.referenceId);
1911 req->smallSetUpperBound = &obj->set_inher.smallSetUpperBound;
1912 req->largeSetLowerBound = &obj->set_inher.largeSetLowerBound;
1913 req->mediumSetPresentNumber = &obj->set_inher.mediumSetPresentNumber;
1914 req->replaceIndicator = &obj->set_inher.replaceIndicator;
1915 req->resultSetName = obj->setName ? obj->setName : "Default";
1916 logf (LOG_DEBUG, "Search, resultSetName %s", req->resultSetName);
1917 req->num_databaseNames = obj->set_inher.num_databaseNames;
1918 req->databaseNames = obj->set_inher.databaseNames;
1919 for (r=0; r < obj->set_inher.num_databaseNames; r++)
1920 logf (LOG_DEBUG, " Database %s", obj->set_inher.databaseNames[r]);
1921 if (obj->set_inher.preferredRecordSyntax)
1923 struct oident ident;
1925 ident.proto = p->protocol_type;
1926 ident.oclass = CLASS_RECSYN;
1927 ident.value = *obj->set_inher.preferredRecordSyntax;
1928 logf (LOG_DEBUG, "Preferred record syntax is %d", ident.value);
1929 req->preferredRecordSyntax = odr_oiddup (p->odr_out,
1930 oid_getoidbyent (&ident));
1933 req->preferredRecordSyntax = 0;
1935 if (obj->set_inher.smallSetElementSetNames &&
1936 *obj->set_inher.smallSetElementSetNames)
1938 Z_ElementSetNames *esn = odr_malloc (p->odr_out, sizeof(*esn));
1940 esn->which = Z_ElementSetNames_generic;
1941 esn->u.generic = obj->set_inher.smallSetElementSetNames;
1942 req->smallSetElementSetNames = esn;
1945 req->smallSetElementSetNames = NULL;
1947 if (obj->set_inher.mediumSetElementSetNames &&
1948 *obj->set_inher.mediumSetElementSetNames)
1950 Z_ElementSetNames *esn = odr_malloc (p->odr_out, sizeof(*esn));
1952 esn->which = Z_ElementSetNames_generic;
1953 esn->u.generic = obj->set_inher.mediumSetElementSetNames;
1954 req->mediumSetElementSetNames = esn;
1957 req->mediumSetElementSetNames = NULL;
1959 req->query = &query;
1961 if (!strcmp (obj->set_inher.queryType, "rpn"))
1963 Z_RPNQuery *RPNquery;
1965 RPNquery = p_query_rpn (p->odr_out, p->protocol_type, argv[2]);
1968 Tcl_AppendResult (interp, "Syntax error in query", NULL);
1971 query.which = Z_Query_type_1;
1972 query.u.type_1 = RPNquery;
1973 logf (LOG_DEBUG, "RPN");
1976 else if (!strcmp (obj->set_inher.queryType, "cclrpn"))
1980 struct ccl_rpn_node *rpn;
1981 Z_RPNQuery *RPNquery;
1984 bib1.proto = p->protocol_type;
1985 bib1.oclass = CLASS_ATTSET;
1986 bib1.value = VAL_BIB1;
1988 rpn = ccl_find_str(p->bibset, argv[2], &error, &pos);
1991 Tcl_AppendResult (interp, "CCL error: ",
1992 ccl_err_msg(error), NULL);
1995 ccl_pr_tree (rpn, stderr);
1996 fprintf (stderr, "\n");
1997 assert((RPNquery = ccl_rpn_query(rpn)));
1998 RPNquery->attributeSetId = oid_getoidbyent (&bib1);
1999 query.which = Z_Query_type_1;
2000 query.u.type_1 = RPNquery;
2001 logf (LOG_DEBUG, "CCLRPN");
2004 else if (!strcmp (obj->set_inher.queryType, "ccl"))
2006 query.which = Z_Query_type_2;
2007 query.u.type_2 = &ccl_query;
2008 ccl_query.buf = (unsigned char *) argv[2];
2009 ccl_query.len = strlen (argv[2]);
2010 logf (LOG_DEBUG, "CCL");
2014 interp->result = "unknown query method";
2017 return ir_tcl_send_APDU (interp, p, apdu, "search", *argv);
2021 * do_searchResponse: add search response handler
2023 static int do_searchResponse (void *o, Tcl_Interp *interp,
2024 int argc, char **argv)
2026 IrTcl_SetObj *obj = o;
2030 obj->searchResponse = NULL;
2033 else if (argc == -1)
2034 return ir_tcl_strdel (interp, &obj->searchResponse);
2037 free (obj->searchResponse);
2040 if (ir_tcl_strdup (interp, &obj->searchResponse, argv[2])
2045 obj->searchResponse = NULL;
2051 * do_presentResponse: add present response handler
2053 static int do_presentResponse (void *o, Tcl_Interp *interp,
2054 int argc, char **argv)
2056 IrTcl_SetObj *obj = o;
2060 obj->presentResponse = NULL;
2063 else if (argc == -1)
2064 return ir_tcl_strdel (interp, &obj->presentResponse);
2067 free (obj->presentResponse);
2070 if (ir_tcl_strdup (interp, &obj->presentResponse, argv[2])
2075 obj->presentResponse = NULL;
2081 * do_resultCount: Get number of hits
2083 static int do_resultCount (void *o, Tcl_Interp *interp,
2084 int argc, char **argv)
2086 IrTcl_SetObj *obj = o;
2090 obj->resultCount = 0;
2093 return ir_tcl_get_set_int (&obj->resultCount, interp, argc, argv);
2097 * do_searchStatus: Get search status (after search response)
2099 static int do_searchStatus (void *o, Tcl_Interp *interp,
2100 int argc, char **argv)
2102 IrTcl_SetObj *obj = o;
2106 return ir_tcl_get_set_int (&obj->searchStatus, interp, argc, argv);
2110 * do_presentStatus: Get search status (after search/present response)
2112 static int do_presentStatus (void *o, Tcl_Interp *interp,
2113 int argc, char **argv)
2115 IrTcl_SetObj *obj = o;
2119 return ir_tcl_get_set_int (&obj->presentStatus, interp, argc, argv);
2123 * do_nextResultSetPosition: Get next result set position
2124 * (after search/present response)
2126 static int do_nextResultSetPosition (void *o, Tcl_Interp *interp,
2127 int argc, char **argv)
2129 IrTcl_SetObj *obj = o;
2133 obj->nextResultSetPosition = 0;
2136 return ir_tcl_get_set_int (&obj->nextResultSetPosition, interp,
2141 * do_setName: Set result Set name
2143 static int do_setName (void *o, Tcl_Interp *interp,
2144 int argc, char **argv)
2146 IrTcl_SetObj *obj = o;
2149 return ir_tcl_strdup (interp, &obj->setName, "Default");
2150 else if (argc == -1)
2151 return ir_tcl_strdel (interp, &obj->setName);
2154 free (obj->setName);
2155 if (ir_tcl_strdup (interp, &obj->setName, argv[2])
2159 Tcl_AppendElement (interp, obj->setName);
2164 * do_numberOfRecordsReturned: Get number of records returned
2166 static int do_numberOfRecordsReturned (void *o, Tcl_Interp *interp,
2167 int argc, char **argv)
2169 IrTcl_SetObj *obj = o;
2173 obj->numberOfRecordsReturned = 0;
2176 return ir_tcl_get_set_int (&obj->numberOfRecordsReturned, interp,
2181 * do_type: Return type (if any) at position.
2183 static int do_type (void *o, Tcl_Interp *interp, int argc, char **argv)
2185 IrTcl_SetObj *obj = o;
2187 IrTcl_RecordList *rl;
2191 obj->record_list = NULL;
2194 else if (argc == -1)
2196 delete_IR_records (obj);
2201 sprintf (interp->result, "wrong # args");
2204 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2206 rl = find_IR_record (obj, offset);
2209 logf (LOG_DEBUG, "No record at position %d", offset);
2214 case Z_NamePlusRecord_databaseRecord:
2215 interp->result = "DB";
2217 case Z_NamePlusRecord_surrogateDiagnostic:
2218 interp->result = "SD";
2226 * do_recordType: Return record type (if any) at position.
2228 static int do_recordType (void *o, Tcl_Interp *interp, int argc, char **argv)
2230 IrTcl_SetObj *obj = o;
2232 IrTcl_RecordList *rl;
2238 else if (argc == -1)
2244 sprintf (interp->result, "wrong # args");
2247 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2249 rl = find_IR_record (obj, offset);
2252 if (rl->which != Z_NamePlusRecord_databaseRecord)
2254 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2257 Tcl_AppendElement (interp, (char*)
2258 IrTcl_getRecordSyntaxStr (rl->u.dbrec.type));
2263 * set record elements (for record extraction)
2265 static int do_recordElements (void *o, Tcl_Interp *interp,
2266 int argc, char **argv)
2268 IrTcl_SetObj *obj = o;
2272 obj->recordElements = NULL;
2275 else if (argc == -1)
2276 return ir_tcl_strdel (NULL, &obj->recordElements);
2279 sprintf (interp->result, "wrong # args");
2284 free (obj->recordElements);
2285 return ir_tcl_strdup (NULL, &obj->recordElements,
2286 (*argv[2] ? argv[2] : NULL));
2288 Tcl_AppendResult (interp, obj->recordElements, NULL);
2295 static int ir_diagResult (Tcl_Interp *interp, IrTcl_Diagnostic *list, int num)
2301 for (i = 0; i<num; i++)
2303 logf (LOG_DEBUG, "Diagnostic, code %d", list[i].condition);
2304 sprintf (buf, "%d", list[i].condition);
2305 Tcl_AppendElement (interp, buf);
2306 cp = diagbib1_str (list[i].condition);
2308 Tcl_AppendElement (interp, (char*) cp);
2310 Tcl_AppendElement (interp, "");
2311 if (list[i].addinfo)
2312 Tcl_AppendElement (interp, (char*) list[i].addinfo);
2314 Tcl_AppendElement (interp, "");
2320 * do_diag: Return diagnostic record info
2322 static int do_diag (void *o, Tcl_Interp *interp, int argc, char **argv)
2324 IrTcl_SetObj *obj = o;
2326 IrTcl_RecordList *rl;
2332 sprintf (interp->result, "wrong # args");
2335 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2337 rl = find_IR_record (obj, offset);
2340 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2343 if (rl->which != Z_NamePlusRecord_surrogateDiagnostic)
2345 Tcl_AppendResult (interp, "No Diagnostic record at #", argv[2], NULL);
2348 return ir_diagResult (interp, rl->u.surrogateDiagnostics.list,
2349 rl->u.surrogateDiagnostics.num);
2353 * do_getMarc: Get ISO2709 Record lines/fields
2355 static int do_getMarc (void *o, Tcl_Interp *interp, int argc, char **argv)
2357 IrTcl_SetObj *obj = o;
2359 IrTcl_RecordList *rl;
2365 sprintf (interp->result, "wrong # args");
2368 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2370 rl = find_IR_record (obj, offset);
2373 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2376 if (rl->which != Z_NamePlusRecord_databaseRecord)
2378 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2381 return ir_tcl_get_marc (interp, rl->u.dbrec.buf, argc, argv);
2385 * do_getSutrs: Get SUTRS Record
2387 static int do_getSutrs (void *o, Tcl_Interp *interp, int argc, char **argv)
2389 IrTcl_SetObj *obj = o;
2391 IrTcl_RecordList *rl;
2397 sprintf (interp->result, "wrong # args");
2400 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2402 rl = find_IR_record (obj, offset);
2405 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2408 if (rl->which != Z_NamePlusRecord_databaseRecord)
2410 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2413 if (rl->u.dbrec.type != VAL_SUTRS)
2415 Tcl_AppendElement (interp, rl->u.dbrec.buf);
2421 * do_getGrs: Get a GRS1 Record
2423 static int do_getGrs (void *o, Tcl_Interp *interp, int argc, char **argv)
2425 IrTcl_SetObj *obj = o;
2427 IrTcl_RecordList *rl;
2433 sprintf (interp->result, "wrong # args");
2436 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2438 rl = find_IR_record (obj, offset);
2441 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2444 if (rl->which != Z_NamePlusRecord_databaseRecord)
2446 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2449 if (rl->u.dbrec.type != VAL_GRS1)
2451 return ir_tcl_get_grs (interp, rl->u.dbrec.u.grs1, argc, argv);
2456 * do_responseStatus: Return response status (present or search)
2458 static int do_responseStatus (void *o, Tcl_Interp *interp,
2459 int argc, char **argv)
2461 IrTcl_SetObj *obj = o;
2465 obj->recordFlag = 0;
2466 obj->nonSurrogateDiagnosticNum = 0;
2467 obj->nonSurrogateDiagnosticList = NULL;
2470 else if (argc == -1)
2472 ir_deleteDiags (&obj->nonSurrogateDiagnosticList,
2473 &obj->nonSurrogateDiagnosticNum);
2476 if (!obj->recordFlag)
2478 Tcl_AppendElement (interp, "OK");
2483 case Z_Records_DBOSD:
2484 Tcl_AppendElement (interp, "DBOSD");
2487 Tcl_AppendElement (interp, "NSD");
2488 return ir_diagResult (interp, obj->nonSurrogateDiagnosticList,
2489 obj->nonSurrogateDiagnosticNum);
2495 * do_present: Perform Present Request
2498 static int do_present (void *o, Tcl_Interp *interp, int argc, char **argv)
2500 IrTcl_SetObj *obj = o;
2503 Z_PresentRequest *req;
2511 if (Tcl_GetInt (interp, argv[2], &start) == TCL_ERROR)
2518 if (Tcl_GetInt (interp, argv[3], &number) == TCL_ERROR)
2526 interp->result = "present: not connected";
2531 obj->number = number;
2533 apdu = zget_APDU (p->odr_out, Z_APDU_presentRequest);
2534 req = apdu->u.presentRequest;
2536 set_referenceId (p->odr_out, &req->referenceId,
2537 obj->set_inher.referenceId);
2539 req->resultSetId = obj->setName ? obj->setName : "Default";
2541 req->resultSetStartPoint = &start;
2542 req->numberOfRecordsRequested = &number;
2543 if (obj->set_inher.preferredRecordSyntax)
2545 struct oident ident;
2547 ident.proto = p->protocol_type;
2548 ident.oclass = CLASS_RECSYN;
2549 ident.value = *obj->set_inher.preferredRecordSyntax;
2550 logf (LOG_DEBUG, "Preferred record syntax is %d", ident.value);
2551 req->preferredRecordSyntax = odr_oiddup (p->odr_out,
2552 oid_getoidbyent (&ident));
2555 req->preferredRecordSyntax = 0;
2557 if (obj->set_inher.elementSetNames && *obj->set_inher.elementSetNames)
2559 Z_ElementSetNames *esn = odr_malloc (p->odr_out, sizeof(*esn));
2560 Z_RecordComposition *compo = odr_malloc (p->odr_out, sizeof(*compo));
2562 esn->which = Z_ElementSetNames_generic;
2563 esn->u.generic = obj->set_inher.elementSetNames;
2565 req->recordComposition = compo;
2566 compo->which = Z_RecordComp_simple;
2567 compo->u.simple = esn;
2570 req->recordComposition = NULL;
2571 return ir_tcl_send_APDU (interp, p, apdu, "present", *argv);
2575 * do_loadFile: Load result set from file
2578 static int do_loadFile (void *o, Tcl_Interp *interp,
2579 int argc, char **argv)
2581 IrTcl_SetObj *setobj = o;
2591 interp->result = "wrong # args";
2594 inf = fopen (argv[2], "r");
2597 Tcl_AppendResult (interp, "Cannot open ", argv[2], NULL);
2600 while ((buf = ir_tcl_fread_marc (inf, &size)))
2602 IrTcl_RecordList *rl;
2604 rl = new_IR_record (setobj, no, Z_NamePlusRecord_databaseRecord, "F");
2605 rl->u.dbrec.type = VAL_USMARC;
2606 rl->u.dbrec.buf = buf;
2607 rl->u.dbrec.size = size;
2610 setobj->numberOfRecordsReturned = no-1;
2615 static IrTcl_Method ir_set_method_tab[] = {
2616 { "search", do_search, NULL},
2617 { "searchResponse", do_searchResponse, NULL},
2618 { "presentResponse", do_presentResponse, NULL},
2619 { "searchStatus", do_searchStatus, NULL},
2620 { "presentStatus", do_presentStatus, NULL},
2621 { "nextResultSetPosition", do_nextResultSetPosition, NULL},
2622 { "setName", do_setName, NULL},
2623 { "resultCount", do_resultCount, NULL},
2624 { "numberOfRecordsReturned", do_numberOfRecordsReturned, NULL},
2625 { "present", do_present, NULL},
2626 { "type", do_type, NULL},
2627 { "getMarc", do_getMarc, NULL},
2628 { "getSutrs", do_getSutrs, NULL},
2629 { "getGrs", do_getGrs, NULL},
2630 { "recordType", do_recordType, NULL},
2631 { "recordElements", do_recordElements, NULL},
2632 { "diag", do_diag, NULL},
2633 { "responseStatus", do_responseStatus, NULL},
2634 { "loadFile", do_loadFile, NULL},
2639 * ir_set_obj_method: IR Set Object methods
2641 static int ir_set_obj_method (ClientData clientData, Tcl_Interp *interp,
2642 int argc, char **argv)
2644 IrTcl_Methods tabs[3];
2645 IrTcl_SetObj *p = clientData;
2650 interp->result = "wrong # args";
2653 tabs[0].tab = ir_set_method_tab;
2655 tabs[1].tab = ir_set_c_method_tab;
2656 tabs[1].obj = &p->set_inher;
2659 ir_tcl_method (interp, argc, argv, tabs, &r);
2664 * ir_set_obj_delete: IR Set Object disposal
2666 static void ir_set_obj_delete (ClientData clientData)
2668 IrTcl_Methods tabs[3];
2669 IrTcl_SetObj *p = clientData;
2671 logf (LOG_DEBUG, "ir set delete");
2673 tabs[0].tab = ir_set_method_tab;
2675 tabs[1].tab = ir_set_c_method_tab;
2676 tabs[1].obj = &p->set_inher;
2679 ir_tcl_method (NULL, -1, NULL, tabs, NULL);
2685 * ir_set_obj_init: IR Set Object initialization
2687 static int ir_set_obj_init (ClientData clientData, Tcl_Interp *interp,
2688 int argc, char **argv, ClientData *subData,
2689 ClientData parentData)
2691 IrTcl_Methods tabs[3];
2694 if (argc < 2 || argc > 3)
2696 interp->result = "wrong # args";
2699 obj = ir_tcl_malloc (sizeof(*obj));
2700 logf (LOG_DEBUG, "ir set create");
2707 obj->parent = (IrTcl_Obj *) parentData;
2709 dst = &obj->set_inher;
2710 src = &obj->parent->set_inher;
2712 if ((dst->num_databaseNames = src->num_databaseNames))
2714 dst->databaseNames =
2715 ir_tcl_malloc (sizeof (*dst->databaseNames)
2716 * (1+dst->num_databaseNames));
2717 for (i = 0; i < dst->num_databaseNames; i++)
2718 if (ir_tcl_strdup (interp, &dst->databaseNames[i],
2719 src->databaseNames[i]) == TCL_ERROR)
2721 dst->databaseNames[i] = NULL;
2724 dst->databaseNames = NULL;
2725 if (ir_tcl_strdup (interp, &dst->queryType, src->queryType)
2729 if (ir_tcl_strdup (interp, &dst->referenceId, src->referenceId)
2733 if (ir_tcl_strdup (interp, &dst->elementSetNames, src->elementSetNames)
2737 if (ir_tcl_strdup (interp, &dst->smallSetElementSetNames,
2738 src->smallSetElementSetNames)
2742 if (ir_tcl_strdup (interp, &dst->mediumSetElementSetNames,
2743 src->mediumSetElementSetNames)
2747 if (src->preferredRecordSyntax &&
2748 (dst->preferredRecordSyntax
2749 = ir_tcl_malloc (sizeof(*dst->preferredRecordSyntax))))
2750 *dst->preferredRecordSyntax = *src->preferredRecordSyntax;
2752 dst->preferredRecordSyntax = NULL;
2753 dst->replaceIndicator = src->replaceIndicator;
2754 dst->smallSetUpperBound = src->smallSetUpperBound;
2755 dst->largeSetLowerBound = src->largeSetLowerBound;
2756 dst->mediumSetPresentNumber = src->mediumSetPresentNumber;
2761 tabs[0].tab = ir_set_method_tab;
2765 if (ir_tcl_method (interp, 0, NULL, tabs, NULL) == TCL_ERROR)
2773 * ir_set_obj_mk: IR Set Object creation
2775 static int ir_set_obj_mk (ClientData clientData, Tcl_Interp *interp,
2776 int argc, char **argv)
2779 ClientData parentData = 0;
2784 Tcl_CmdInfo parent_info;
2785 if (!Tcl_GetCommandInfo (interp, argv[2], &parent_info))
2787 interp->result = "No parent";
2790 parentData = parent_info.clientData;
2792 r = ir_set_obj_init (clientData, interp, argc, argv, &subData, parentData);
2795 Tcl_CreateCommand (interp, argv[1], ir_set_obj_method,
2796 subData, ir_set_obj_delete);
2800 IrTcl_Class ir_set_obj_class = {
2807 /* ------------------------------------------------------- */
2810 * do_scan: Perform scan
2812 static int do_scan (void *o, Tcl_Interp *interp, int argc, char **argv)
2816 IrTcl_ScanObj *obj = o;
2817 IrTcl_Obj *p = obj->parent;
2820 struct ccl_rpn_node *rpn;
2828 interp->result = "wrong # args";
2831 if (!p->set_inher.num_databaseNames)
2833 interp->result = "no databaseNames";
2838 interp->result = "scan: not connected";
2842 apdu = zget_APDU (p->odr_out, Z_APDU_scanRequest);
2843 req = apdu->u.scanRequest;
2845 set_referenceId (p->odr_out, &req->referenceId, p->set_inher.referenceId);
2846 req->num_databaseNames = p->set_inher.num_databaseNames;
2847 req->databaseNames = p->set_inher.databaseNames;
2850 if (!(req->termListAndStartPoint =
2851 p_query_scan (p->odr_out, p->protocol_type,
2852 &req->attributeSet, 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 bib1.proto = p->protocol_type;
2865 bib1.oclass = CLASS_ATTSET;
2866 bib1.value = VAL_BIB1;
2868 req->attributeSet = oid_getoidbyent (&bib1);
2869 ccl_pr_tree (rpn, stderr);
2870 fprintf (stderr, "\n");
2871 if (!(req->termListAndStartPoint = ccl_scan_query (rpn)))
2874 req->stepSize = &obj->stepSize;
2875 req->numberOfTermsRequested = &obj->numberOfTermsRequested;
2876 req->preferredPositionInResponse = &obj->preferredPositionInResponse;
2877 logf (LOG_DEBUG, "stepSize=%d", *req->stepSize);
2878 logf (LOG_DEBUG, "numberOfTermsRequested=%d",
2879 *req->numberOfTermsRequested);
2880 logf (LOG_DEBUG, "preferredPositionInResponse=%d",
2881 *req->preferredPositionInResponse);
2883 return ir_tcl_send_APDU (interp, p, apdu, "scan", *argv);
2887 * do_scanResponse: add scan response handler
2889 static int do_scanResponse (void *o, Tcl_Interp *interp,
2890 int argc, char **argv)
2892 IrTcl_ScanObj *obj = o;
2896 obj->scanResponse = NULL;
2899 else if (argc == -1)
2900 return ir_tcl_strdel (interp, &obj->scanResponse);
2903 free (obj->scanResponse);
2906 if (ir_tcl_strdup (interp, &obj->scanResponse, argv[2])
2911 obj->scanResponse = NULL;
2917 * do_stepSize: Set/get replace Step Size
2919 static int do_stepSize (void *obj, Tcl_Interp *interp,
2920 int argc, char **argv)
2922 IrTcl_ScanObj *p = obj;
2928 return ir_tcl_get_set_int (&p->stepSize, interp, argc, argv);
2932 * do_numberOfTermsRequested: Set/get Number of Terms requested
2934 static int do_numberOfTermsRequested (void *obj, Tcl_Interp *interp,
2935 int argc, char **argv)
2937 IrTcl_ScanObj *p = obj;
2941 p->numberOfTermsRequested = 20;
2944 return ir_tcl_get_set_int (&p->numberOfTermsRequested, interp, argc, argv);
2949 * do_preferredPositionInResponse: Set/get preferred Position
2951 static int do_preferredPositionInResponse (void *obj, Tcl_Interp *interp,
2952 int argc, char **argv)
2954 IrTcl_ScanObj *p = obj;
2958 p->preferredPositionInResponse = 1;
2961 return ir_tcl_get_set_int (&p->preferredPositionInResponse, interp,
2966 * do_scanStatus: Get scan status
2968 static int do_scanStatus (void *obj, Tcl_Interp *interp,
2969 int argc, char **argv)
2971 IrTcl_ScanObj *p = obj;
2975 return ir_tcl_get_set_int (&p->scanStatus, interp, argc, argv);
2979 * do_numberOfEntriesReturned: Get number of Entries returned
2981 static int do_numberOfEntriesReturned (void *obj, Tcl_Interp *interp,
2982 int argc, char **argv)
2984 IrTcl_ScanObj *p = obj;
2988 return ir_tcl_get_set_int (&p->numberOfEntriesReturned, interp,
2993 * do_positionOfTerm: Get position of Term
2995 static int do_positionOfTerm (void *obj, Tcl_Interp *interp,
2996 int argc, char **argv)
2998 IrTcl_ScanObj *p = obj;
3002 return ir_tcl_get_set_int (&p->positionOfTerm, interp, argc, argv);
3006 * do_scanLine: get Scan Line (surrogate or normal) after response
3008 static int do_scanLine (void *obj, Tcl_Interp *interp, int argc, char **argv)
3010 IrTcl_ScanObj *p = obj;
3016 p->entries_flag = 0;
3018 p->nonSurrogateDiagnosticNum = 0;
3019 p->nonSurrogateDiagnosticList = 0;
3022 else if (argc == -1)
3024 p->entries_flag = 0;
3025 /* release entries */
3028 ir_deleteDiags (&p->nonSurrogateDiagnosticList,
3029 &p->nonSurrogateDiagnosticNum);
3034 interp->result = "wrong # args";
3037 if (Tcl_GetInt (interp, argv[2], &i) == TCL_ERROR)
3039 if (!p->entries_flag || p->which != Z_ListEntries_entries || !p->entries
3040 || i >= p->num_entries || i < 0)
3042 switch (p->entries[i].which)
3044 case Z_Entry_termInfo:
3045 Tcl_AppendElement (interp, "T");
3046 if (p->entries[i].u.term.buf)
3047 Tcl_AppendElement (interp, p->entries[i].u.term.buf);
3049 Tcl_AppendElement (interp, "");
3050 sprintf (numstr, "%d", p->entries[i].u.term.globalOccurrences);
3051 Tcl_AppendElement (interp, numstr);
3053 case Z_Entry_surrogateDiagnostic:
3054 Tcl_AppendElement (interp, "SD");
3055 return ir_diagResult (interp, p->entries[i].u.diag.list,
3056 p->entries[i].u.diag.num);
3062 static IrTcl_Method ir_scan_method_tab[] = {
3063 { "scan", do_scan, NULL},
3064 { "scanResponse", do_scanResponse, NULL},
3065 { "stepSize", do_stepSize, NULL},
3066 { "numberOfTermsRequested", do_numberOfTermsRequested, NULL},
3067 { "preferredPositionInResponse", do_preferredPositionInResponse, NULL},
3068 { "scanStatus", do_scanStatus, NULL},
3069 { "numberOfEntriesReturned", do_numberOfEntriesReturned, NULL},
3070 { "positionOfTerm", do_positionOfTerm, NULL},
3071 { "scanLine", do_scanLine, NULL},
3076 * ir_scan_obj_method: IR Scan Object methods
3078 static int ir_scan_obj_method (ClientData clientData, Tcl_Interp *interp,
3079 int argc, char **argv)
3081 IrTcl_Methods tabs[2];
3086 interp->result = "wrong # args";
3089 tabs[0].tab = ir_scan_method_tab;
3090 tabs[0].obj = clientData;
3093 ir_tcl_method (interp, argc, argv, tabs, &r);
3098 * ir_scan_obj_delete: IR Scan Object disposal
3100 static void ir_scan_obj_delete (ClientData clientData)
3102 IrTcl_Methods tabs[2];
3103 IrTcl_ScanObj *obj = clientData;
3105 tabs[0].tab = ir_scan_method_tab;
3109 ir_tcl_method (NULL, -1, NULL, tabs, NULL);
3114 * ir_scan_obj_mk: IR Scan Object creation
3116 static int ir_scan_obj_mk (ClientData clientData, Tcl_Interp *interp,
3117 int argc, char **argv)
3119 Tcl_CmdInfo parent_info;
3121 IrTcl_Methods tabs[2];
3125 interp->result = "wrong # args";
3128 if (!Tcl_GetCommandInfo (interp, argv[2], &parent_info))
3130 interp->result = "No parent";
3133 obj = ir_tcl_malloc (sizeof(*obj));
3134 obj->parent = (IrTcl_Obj *) parent_info.clientData;
3136 tabs[0].tab = ir_scan_method_tab;
3140 if (ir_tcl_method (interp, 0, NULL, tabs, NULL) == TCL_ERROR)
3142 Tcl_CreateCommand (interp, argv[1], ir_scan_obj_method,
3143 (ClientData) obj, ir_scan_obj_delete);
3147 /* ------------------------------------------------------- */
3149 static void ir_initResponse (void *obj, Z_InitResponse *initrs)
3153 p->initResult = *initrs->result ? 1 : 0;
3154 if (!*initrs->result)
3155 logf (LOG_DEBUG, "Connection rejected by target");
3157 logf (LOG_DEBUG, "Connection accepted by target");
3159 get_referenceId (&p->set_inher.referenceId, initrs->referenceId);
3161 free (p->targetImplementationId);
3162 ir_tcl_strdup (p->interp, &p->targetImplementationId,
3163 initrs->implementationId);
3164 free (p->targetImplementationName);
3165 ir_tcl_strdup (p->interp, &p->targetImplementationName,
3166 initrs->implementationName);
3167 free (p->targetImplementationVersion);
3168 ir_tcl_strdup (p->interp, &p->targetImplementationVersion,
3169 initrs->implementationVersion);
3171 p->maximumRecordSize = *initrs->maximumRecordSize;
3172 p->preferredMessageSize = *initrs->preferredMessageSize;
3174 memcpy (&p->options, initrs->options, sizeof(initrs->options));
3175 memcpy (&p->protocolVersion, initrs->protocolVersion,
3176 sizeof(initrs->protocolVersion));
3177 free (p->userInformationField);
3178 p->userInformationField = NULL;
3179 if (initrs->userInformationField)
3183 if (initrs->userInformationField->which == ODR_EXTERNAL_octet &&
3184 (p->userInformationField =
3185 ir_tcl_malloc ((len =
3186 initrs->userInformationField->
3187 u.octet_aligned->len) +1)))
3189 memcpy (p->userInformationField,
3190 initrs->userInformationField->u.octet_aligned->buf,
3192 (p->userInformationField)[len] = '\0';
3197 static void ir_deleteDiags (IrTcl_Diagnostic **dst_list, int *dst_num)
3200 for (i = 0; i<*dst_num; i++)
3201 free (dst_list[i]->addinfo);
3207 static void ir_handleDiags (IrTcl_Diagnostic **dst_list, int *dst_num,
3208 Z_DiagRec **list, int num)
3214 *dst_list = ir_tcl_malloc (sizeof(**dst_list) * num);
3215 for (i = 0; i<num; i++)
3217 switch (list[i]->which)
3219 case Z_DiagRec_defaultFormat:
3220 (*dst_list)[i].condition = *list[i]->u.defaultFormat->condition;
3221 addinfo = list[i]->u.defaultFormat->addinfo;
3223 ((*dst_list)[i].addinfo = ir_tcl_malloc (strlen(addinfo)+1)))
3224 strcpy ((*dst_list)[i].addinfo, addinfo);
3227 (*dst_list)[i].addinfo = NULL;
3228 (*dst_list)[i].condition = 0;
3233 static void ir_handleRecords (void *o, Z_Records *zrs, IrTcl_SetObj *setobj,
3234 const char *elements)
3239 IrTcl_RecordList *rl;
3241 setobj->which = zrs->which;
3242 setobj->recordFlag = 1;
3244 ir_deleteDiags (&setobj->nonSurrogateDiagnosticList,
3245 &setobj->nonSurrogateDiagnosticNum);
3246 if (zrs->which == Z_Records_DBOSD)
3248 setobj->numberOfRecordsReturned =
3249 zrs->u.databaseOrSurDiagnostics->num_records;
3250 logf (LOG_DEBUG, "Got %d records", setobj->numberOfRecordsReturned);
3251 for (offset = 0; offset<setobj->numberOfRecordsReturned; offset++)
3253 rl = new_IR_record (setobj, setobj->start + offset,
3254 zrs->u.databaseOrSurDiagnostics->
3255 records[offset]->which,
3257 if (rl->which == Z_NamePlusRecord_surrogateDiagnostic)
3259 ir_handleDiags (&rl->u.surrogateDiagnostics.list,
3260 &rl->u.surrogateDiagnostics.num,
3261 &zrs->u.databaseOrSurDiagnostics->
3262 records[offset]->u.surrogateDiagnostic,
3267 Z_DatabaseRecord *zr;
3269 struct oident *ident;
3271 zr = zrs->u.databaseOrSurDiagnostics->records[offset]
3273 oe = (Z_External*) zr;
3274 rl->u.dbrec.size = zr->u.octet_aligned->len;
3276 if ((ident = oid_getentbyoid (oe->direct_reference)))
3277 rl->u.dbrec.type = ident->value;
3279 rl->u.dbrec.type = VAL_USMARC;
3281 if (oe->which == ODR_EXTERNAL_octet && rl->u.dbrec.size > 0)
3283 char *buf = (char*) zr->u.octet_aligned->buf;
3284 if ((rl->u.dbrec.buf = ir_tcl_malloc (rl->u.dbrec.size)))
3285 memcpy (rl->u.dbrec.buf, buf, rl->u.dbrec.size);
3287 else if (rl->u.dbrec.type == VAL_SUTRS &&
3288 oe->which == Z_External_sutrs)
3290 odr_setbuf (p->odr_in, (char*) oe->u.single_ASN1_type->buf,
3291 oe->u.single_ASN1_type->len, 0);
3292 if ((rl->u.dbrec.buf = ir_tcl_malloc (oe->u.sutrs->len+1)))
3294 memcpy (rl->u.dbrec.buf, oe->u.sutrs->buf,
3296 rl->u.dbrec.buf[oe->u.sutrs->len] = '\0';
3298 rl->u.dbrec.size = oe->u.sutrs->len;
3300 else if (rl->u.dbrec.type == VAL_GRS1 &&
3301 oe->which == Z_External_grs1)
3303 ir_tcl_grs_mk (oe->u.grs1, &rl->u.dbrec.u.grs1);
3304 rl->u.dbrec.buf = NULL;
3307 rl->u.dbrec.buf = NULL;
3311 else if (zrs->which == Z_Records_multipleNSD)
3313 logf (LOG_DEBUG, "multipleNonSurrogateDiagnostic %d",
3314 zrs->u.multipleNonSurDiagnostics->num_diagRecs);
3315 setobj->numberOfRecordsReturned = 0;
3316 ir_handleDiags (&setobj->nonSurrogateDiagnosticList,
3317 &setobj->nonSurrogateDiagnosticNum,
3318 zrs->u.multipleNonSurDiagnostics->diagRecs,
3319 zrs->u.multipleNonSurDiagnostics->num_diagRecs);
3323 logf (LOG_DEBUG, "NonSurrogateDiagnostic");
3324 setobj->numberOfRecordsReturned = 0;
3325 ir_handleDiags (&setobj->nonSurrogateDiagnosticList,
3326 &setobj->nonSurrogateDiagnosticNum,
3327 &zrs->u.nonSurrogateDiagnostic,
3332 static void ir_searchResponse (void *o, Z_SearchResponse *searchrs,
3333 IrTcl_SetObj *setobj)
3335 Z_Records *zrs = searchrs->records;
3337 logf (LOG_DEBUG, "Received search response");
3340 logf (LOG_DEBUG, "Search response, no object!");
3343 setobj->searchStatus = searchrs->searchStatus ? 1 : 0;
3344 get_referenceId (&setobj->set_inher.referenceId, searchrs->referenceId);
3345 setobj->resultCount = *searchrs->resultCount;
3346 if (searchrs->presentStatus)
3347 setobj->presentStatus = *searchrs->presentStatus;
3348 if (searchrs->nextResultSetPosition)
3349 setobj->nextResultSetPosition = *searchrs->nextResultSetPosition;
3351 logf (LOG_DEBUG, "Search response %d, %d hits",
3352 setobj->searchStatus, setobj->resultCount);
3356 if (setobj->resultCount <= setobj->set_inher.smallSetUpperBound)
3357 es = setobj->set_inher.smallSetElementSetNames;
3359 es = setobj->set_inher.mediumSetElementSetNames;
3360 ir_handleRecords (o, zrs, setobj, es);
3363 setobj->recordFlag = 0;
3367 static void ir_presentResponse (void *o, Z_PresentResponse *presrs,
3368 IrTcl_SetObj *setobj)
3370 Z_Records *zrs = presrs->records;
3372 logf (LOG_DEBUG, "Received present response");
3375 logf (LOG_DEBUG, "Present response, no object!");
3378 setobj->presentStatus = *presrs->presentStatus;
3379 get_referenceId (&setobj->set_inher.referenceId, presrs->referenceId);
3380 setobj->nextResultSetPosition = *presrs->nextResultSetPosition;
3382 ir_handleRecords (o, zrs, setobj, setobj->set_inher.elementSetNames);
3385 setobj->recordFlag = 0;
3386 logf (LOG_DEBUG, "No records!");
3390 static void ir_scanResponse (void *o, Z_ScanResponse *scanrs,
3391 IrTcl_ScanObj *scanobj)
3395 logf (LOG_DEBUG, "Received scanResponse");
3397 get_referenceId (&p->set_inher.referenceId, scanrs->referenceId);
3398 scanobj->scanStatus = *scanrs->scanStatus;
3399 logf (LOG_DEBUG, "scanStatus=%d", scanobj->scanStatus);
3401 if (scanrs->stepSize)
3402 scanobj->stepSize = *scanrs->stepSize;
3403 logf (LOG_DEBUG, "stepSize=%d", scanobj->stepSize);
3405 scanobj->numberOfEntriesReturned = *scanrs->numberOfEntriesReturned;
3406 logf (LOG_DEBUG, "numberOfEntriesReturned=%d",
3407 scanobj->numberOfEntriesReturned);
3409 if (scanrs->positionOfTerm)
3410 scanobj->positionOfTerm = *scanrs->positionOfTerm;
3412 scanobj->positionOfTerm = -1;
3413 logf (LOG_DEBUG, "positionOfTerm=%d", scanobj->positionOfTerm);
3415 free (scanobj->entries);
3416 scanobj->entries = NULL;
3418 ir_deleteDiags (&scanobj->nonSurrogateDiagnosticList,
3419 &scanobj->nonSurrogateDiagnosticNum);
3420 if (scanrs->entries)
3425 scanobj->entries_flag = 1;
3426 scanobj->which = scanrs->entries->which;
3427 switch (scanobj->which)
3429 case Z_ListEntries_entries:
3430 scanobj->num_entries = scanrs->entries->u.entries->num_entries;
3431 scanobj->entries = ir_tcl_malloc (scanobj->num_entries *
3432 sizeof(*scanobj->entries));
3433 for (i=0; i<scanobj->num_entries; i++)
3435 ze = scanrs->entries->u.entries->entries[i];
3436 scanobj->entries[i].which = ze->which;
3439 case Z_Entry_termInfo:
3440 if (ze->u.termInfo->term->which == Z_Term_general)
3442 int l = ze->u.termInfo->term->u.general->len;
3443 scanobj->entries[i].u.term.buf = ir_tcl_malloc (1+l);
3444 memcpy (scanobj->entries[i].u.term.buf,
3445 ze->u.termInfo->term->u.general->buf,
3447 scanobj->entries[i].u.term.buf[l] = '\0';
3450 scanobj->entries[i].u.term.buf = NULL;
3451 if (ze->u.termInfo->globalOccurrences)
3452 scanobj->entries[i].u.term.globalOccurrences =
3453 *ze->u.termInfo->globalOccurrences;
3455 scanobj->entries[i].u.term.globalOccurrences = 0;
3457 case Z_Entry_surrogateDiagnostic:
3458 ir_handleDiags (&scanobj->entries[i].u.diag.list,
3459 &scanobj->entries[i].u.diag.num,
3460 &ze->u.surrogateDiagnostic,
3466 case Z_ListEntries_nonSurrogateDiagnostics:
3467 ir_handleDiags (&scanobj->nonSurrogateDiagnosticList,
3468 &scanobj->nonSurrogateDiagnosticNum,
3469 scanrs->entries->u.nonSurrogateDiagnostics->
3471 scanrs->entries->u.nonSurrogateDiagnostics->
3477 scanobj->entries_flag = 0;
3481 * ir_select_read: handle incoming packages
3483 static void ir_select_read (ClientData clientData)
3485 IrTcl_Obj *p = clientData;
3490 Tcl_CmdInfo cmd_info;
3491 const char *apdu_call;
3493 logf(LOG_DEBUG, "Read handler fd=%d", cs_fileno(p->cs_link));
3494 if (p->state == IR_TCL_R_Connecting)
3496 logf(LOG_DEBUG, "read: connect");
3497 r = cs_rcvconnect (p->cs_link);
3500 logf (LOG_WARN, "cs_rcvconnect returned 1");
3503 p->state = IR_TCL_R_Idle;
3505 #if IRTCL_GENERIC_FILES
3506 ir_select_remove_write (p->csFile, p);
3508 ir_select_remove_write (cs_fileno (p->cs_link), p);
3512 logf (LOG_DEBUG, "cs_rcvconnect error");
3513 ir_tcl_disconnect (p);
3516 p->failInfo = IR_TCL_FAIL_CONNECT;
3517 ir_tcl_eval (p->interp, p->failback);
3523 ir_tcl_eval (p->interp, p->callback);
3524 if (p->ref_count == 2 && p->cs_link && p->request_queue
3525 && p->state == IR_TCL_R_Idle)
3526 ir_tcl_send_q (p, p->request_queue, "x");
3532 p->state = IR_TCL_R_Reading;
3534 /* read incoming APDU */
3535 if ((r=cs_get (p->cs_link, &p->buf_in, &p->len_in)) == 1)
3537 logf(LOG_DEBUG, "PDU Fraction read");
3540 /* signal one more use of ir object - callbacks must not
3541 release the ir memory (p pointer) */
3545 logf (LOG_DEBUG, "cs_get failed, code %d", r);
3546 #if IRTCL_GENERIC_FILES
3547 ir_select_remove (p->csFile, p);
3549 ir_select_remove (cs_fileno (p->cs_link), p);
3551 ir_tcl_disconnect (p);
3554 p->failInfo = IR_TCL_FAIL_READ;
3555 ir_tcl_eval (p->interp, p->failback);
3557 /* release ir object now if callback deleted it */
3561 /* got complete APDU. Now decode */
3564 odr_setbuf (p->odr_in, p->buf_in, r, 0);
3565 logf (LOG_DEBUG, "cs_get ok, total size %d", r);
3566 if (!z_APDU (p->odr_in, &apdu, 0))
3568 logf (LOG_DEBUG, "cs_get failed: %s",
3569 odr_errmsg (odr_geterror (p->odr_in)));
3570 ir_tcl_disconnect (p);
3573 p->failInfo = IR_TCL_FAIL_IN_APDU;
3574 p->apduOffset = odr_offset (p->odr_in);
3575 ir_tcl_eval (p->interp, p->failback);
3577 /* release ir object now if failback deleted it */
3581 logf(LOG_DEBUG, "Decoded ok");
3582 /* handle APDU and invoke callback */
3583 rq = p->request_queue;
3586 logf (LOG_FATAL, "Internal error. No queue entry");
3589 object_name = rq->object_name;
3590 logf (LOG_DEBUG, "getCommandInfo (%s)", object_name);
3592 if (Tcl_GetCommandInfo (p->interp, object_name, &cmd_info))
3596 case Z_APDU_initResponse:
3597 p->eventType = "init";
3598 ir_initResponse (p, apdu->u.initResponse);
3599 apdu_call = p->initResponse;
3601 case Z_APDU_searchResponse:
3602 p->eventType = "search";
3603 ir_searchResponse (p, apdu->u.searchResponse,
3604 (IrTcl_SetObj *) cmd_info.clientData);
3605 apdu_call = ((IrTcl_SetObj *)
3606 cmd_info.clientData)->searchResponse;
3608 case Z_APDU_presentResponse:
3609 p->eventType = "present";
3610 ir_presentResponse (p, apdu->u.presentResponse,
3611 (IrTcl_SetObj *) cmd_info.clientData);
3612 apdu_call = ((IrTcl_SetObj *)
3613 cmd_info.clientData)->presentResponse;
3615 case Z_APDU_scanResponse:
3616 p->eventType = "scan";
3617 ir_scanResponse (p, apdu->u.scanResponse,
3618 (IrTcl_ScanObj *) cmd_info.clientData);
3619 apdu_call = ((IrTcl_ScanObj *)
3620 cmd_info.clientData)->scanResponse;
3623 logf (LOG_WARN, "Received unknown APDU type (%d)",
3625 ir_tcl_disconnect (p);
3628 p->failInfo = IR_TCL_FAIL_UNKNOWN_APDU;
3629 ir_tcl_eval (p->interp, p->failback);
3634 p->request_queue = rq->next;
3635 p->state = IR_TCL_R_Idle;
3638 ir_tcl_eval (p->interp, apdu_call);
3639 else if (rq->callback)
3640 ir_tcl_eval (p->interp, rq->callback);
3642 free (rq->callback);
3643 free (rq->object_name);
3645 odr_reset (p->odr_in);
3646 if (p->ref_count == 1)
3652 } while (p->cs_link && cs_more (p->cs_link));
3653 if (p->cs_link && p->request_queue && p->state == IR_TCL_R_Idle)
3654 ir_tcl_send_q (p, p->request_queue, "x");
3658 * ir_select_write: handle outgoing packages - not yet written.
3660 static void ir_select_write (ClientData clientData)
3662 IrTcl_Obj *p = clientData;
3666 logf (LOG_DEBUG, "Write handler fd=%d", cs_fileno(p->cs_link));
3667 if (p->state == IR_TCL_R_Connecting)
3669 logf(LOG_DEBUG, "write: connect");
3670 r = cs_rcvconnect (p->cs_link);
3673 logf (LOG_DEBUG, "cs_rcvconnect returned 1");
3676 p->state = IR_TCL_R_Idle;
3678 #if IRTCL_GENERIC_FILES
3679 ir_select_remove_write (p->csFile, p);
3681 ir_select_remove_write (cs_fileno (p->cs_link), p);
3685 logf (LOG_DEBUG, "cs_rcvconnect error");
3686 ir_tcl_disconnect (p);
3689 p->failInfo = IR_TCL_FAIL_CONNECT;
3690 ir_tcl_eval (p->interp, p->failback);
3696 ir_tcl_eval (p->interp, p->callback);
3700 rq = p->request_queue;
3701 if (!rq || !rq->buf_out)
3704 if ((r=cs_put (p->cs_link, rq->buf_out, rq->len_out)) < 0)
3706 logf (LOG_DEBUG, "cs_put write fail");
3710 ir_tcl_disconnect (p);
3713 p->failInfo = IR_TCL_FAIL_WRITE;
3714 ir_tcl_eval (p->interp, p->failback);
3718 else if (r == 0) /* remove select bit */
3720 logf (LOG_DEBUG, "Write completed");
3721 p->state = IR_TCL_R_Waiting;
3722 #if IRTCL_GENERIC_FILES
3723 ir_select_remove_write (p->csFile, p);
3725 ir_select_remove_write (cs_fileno (p->cs_link), p);
3732 static void ir_select_notify (ClientData clientData, int r, int w, int e)
3735 ir_select_read (clientData);
3737 ir_select_write (clientData);
3740 /* ------------------------------------------------------- */
3743 * Irtcl_init: Registration of TCL commands.
3745 int Irtcl_Init (Tcl_Interp *interp)
3747 Tcl_CreateCommand (interp, "ir", ir_obj_mk, (ClientData) NULL,
3748 (Tcl_CmdDeleteProc *) NULL);
3749 Tcl_CreateCommand (interp, "ir-set", ir_set_obj_mk,
3750 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
3751 Tcl_CreateCommand (interp, "ir-scan", ir_scan_obj_mk,
3752 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);