2 * IR toolkit for tcl/tk
4 * See the file LICENSE for details.
5 * Sebastian Hammer, Adam Dickmeiss
8 * Revision 1.73 1996-01-29 11:35:19 adam
9 * Bug fix: cs_type member renamed to comstackType to avoid conflict with
10 * cs_type macro defined by YAZ.
12 * Revision 1.72 1996/01/19 17:45:34 quinn
13 * Added debugging output
15 * Revision 1.71 1996/01/19 16:22:38 adam
16 * New method: apduDump - returns information about last incoming APDU.
18 * Revision 1.70 1996/01/10 09:18:34 adam
19 * PDU specific callbacks implemented: initRespnse, searchResponse,
20 * presentResponse and scanResponse.
21 * Bug fix in the command line shell (tclmain.c) - discovered on OSF/1.
23 * Revision 1.69 1996/01/04 16:12:12 adam
24 * Setting PDUType renamed to eventType.
26 * Revision 1.68 1996/01/04 11:05:22 adam
27 * New setting: PDUType - returns type of last PDU returned from the target.
28 * Fixed a bug in configure/Makefile.
30 * Revision 1.67 1996/01/03 09:00:51 adam
31 * Updated to use new version of Yaz (names changed to avoid C++ conflict).
33 * Revision 1.66 1995/11/28 17:26:39 adam
34 * Removed Carriage return from ir-tcl.c!
35 * Removed misc. debug logs.
37 * Revision 1.65 1995/11/28 13:53:00 quinn
40 * Revision 1.64 1995/11/13 15:39:18 adam
41 * Bug fix: {small,medium}SetElementSetNames weren't set correctly.
42 * Bug fix: idAuthentication weren't set correctly.
44 * Revision 1.63 1995/11/13 09:55:39 adam
45 * Multiple records at a position in a result-set with differnt
48 * Revision 1.62 1995/10/18 17:20:33 adam
49 * Work on target setup in client.tcl.
51 * Revision 1.61 1995/10/18 16:42:42 adam
52 * New settings: smallSetElementSetNames and mediumSetElementSetNames.
54 * Revision 1.60 1995/10/18 15:43:31 adam
55 * In search: mediumSetElementSetNames and smallSetElementSetNames are
56 * set to elementSetNames.
58 * Revision 1.59 1995/10/17 12:18:58 adam
59 * Bug fix: when target connection closed, the connection was not
60 * properly reestablished.
62 * Revision 1.58 1995/10/16 17:00:55 adam
63 * New setting: elementSetNames.
64 * Various client improvements. Medium presentation format looks better.
66 * Revision 1.57 1995/09/21 13:11:51 adam
67 * Support of dynamic loading.
68 * Test script uses load command if necessary.
70 * Revision 1.56 1995/08/29 15:30:14 adam
71 * Work on GRS records.
73 * Revision 1.55 1995/08/28 09:43:25 adam
74 * Minor changes. configure only searches for yaz beta 3 and versions after
77 * Revision 1.54 1995/08/24 12:25:16 adam
78 * Modified to work with yaz 1.0b3.
80 * Revision 1.53 1995/08/04 12:49:26 adam
81 * Bug fix: reading uninitialized variable p.
83 * Revision 1.52 1995/08/04 11:32:38 adam
84 * More work on output queue. Memory related routines moved
87 * Revision 1.51 1995/08/03 13:22:54 adam
90 * Revision 1.50 1995/07/20 08:09:49 adam
91 * client.tcl: Targets removed from hotTargets list when targets
92 * are removed/modified.
93 * ir-tcl.c: More work on triggerResourceControl.
95 * Revision 1.49 1995/06/30 12:39:21 adam
96 * Bug fix: loadFile didn't set record type.
97 * The MARC routines are a little less strict in the interpretation.
98 * Script display.tcl replaces the old marc.tcl.
99 * New interactive script: shell.tcl.
101 * Revision 1.48 1995/06/27 19:03:50 adam
102 * Bug fix in do_present in ir-tcl.c: p->set_child member weren't set.
103 * nextResultSetPosition used instead of setOffset.
105 * Revision 1.47 1995/06/25 10:25:04 adam
106 * Working on triggerResourceControl. Description of compile/install
107 * procedure moved to ir-tcl.sgml.
109 * Revision 1.46 1995/06/22 13:15:06 adam
110 * Feature: SUTRS. Setting getSutrs implemented.
111 * Work on display formats.
112 * Preferred record syntax can be set by the user.
114 * Revision 1.45 1995/06/20 08:07:30 adam
115 * New setting: failInfo.
116 * Working on better cancel mechanism.
118 * Revision 1.44 1995/06/19 17:01:20 adam
121 * Revision 1.43 1995/06/19 13:06:08 adam
122 * New define: IR_TCL_VERSION.
124 * Revision 1.42 1995/06/19 08:08:52 adam
125 * client.tcl: hotTargets now contain both database and target name.
126 * ir-tcl.c: setting protocol edited. Errors in callbacks are logged
127 * by logf(LOG_WARN, ...) calls.
129 * Revision 1.41 1995/06/16 12:28:16 adam
130 * Implemented preferredRecordSyntax.
131 * Minor changes in diagnostic handling.
132 * Record list deleted when connection closes.
134 * Revision 1.40 1995/06/14 13:37:18 adam
135 * Setting recordType implemented.
136 * Setting implementationVersion implemented.
137 * Settings implementationId / implementationName edited.
139 * Revision 1.39 1995/06/08 10:26:32 adam
140 * Bug fix in ir_strdup.
142 * Revision 1.38 1995/06/01 16:36:47 adam
143 * About buttons. Minor bug fixes.
145 * Revision 1.37 1995/06/01 07:31:20 adam
146 * Rename of many typedefs -> IrTcl_...
148 * Revision 1.36 1995/05/31 13:09:59 adam
149 * Client searches/presents may be interrupted.
150 * New moving book-logo.
152 * Revision 1.35 1995/05/31 08:36:33 adam
153 * Bug fix in client.tcl: didn't save options on clientrc.tcl.
154 * New method: referenceId. More work on scan.
156 * Revision 1.34 1995/05/29 10:33:42 adam
157 * README and rename of startup script.
159 * Revision 1.33 1995/05/29 09:15:11 quinn
160 * Changed CS_SR to PROTO_SR, etc.
162 * Revision 1.32 1995/05/29 08:44:16 adam
163 * Work on delete of objects.
165 * Revision 1.31 1995/05/26 11:44:10 adam
166 * Bugs fixed. More work on MARC utilities and queries. Test
167 * client is up-to-date again.
169 * Revision 1.30 1995/05/26 08:54:11 adam
170 * New MARC utilities. Uses prefix query.
172 * Revision 1.29 1995/05/24 14:10:22 adam
173 * Work on idAuthentication, protocolVersion and options.
175 * Revision 1.28 1995/05/23 15:34:48 adam
176 * Many new settings, userInformationField, smallSetUpperBound, etc.
177 * A number of settings are inherited when ir-set is executed.
178 * This version is incompatible with the graphical test client (client.tcl).
180 * Revision 1.27 1995/05/11 15:34:47 adam
181 * Scan request changed a bit. This version works with RLG.
183 * Revision 1.26 1995/04/18 16:11:51 adam
184 * First version of graphical Scan. Some work on query-by-form.
186 * Revision 1.25 1995/04/17 09:37:17 adam
187 * Further development of scan.
189 * Revision 1.24 1995/04/11 14:16:42 adam
190 * Further work on scan. Response works. Entries aren't saved yet.
192 * Revision 1.23 1995/04/10 10:50:27 adam
193 * Result-set name defaults to suffix of ir-set name.
194 * Started working on scan. Not finished at this point.
196 * Revision 1.22 1995/03/31 10:43:03 adam
197 * More robust when getting bad MARC records.
199 * Revision 1.21 1995/03/31 08:56:37 adam
200 * New button "Search".
202 * Revision 1.20 1995/03/29 16:07:09 adam
203 * Bug fix: Didn't use setName in present request.
205 * Revision 1.19 1995/03/28 12:45:23 adam
206 * New ir method failback: called on disconnect/protocol error.
207 * New ir set/get method: protocol: SR / Z3950.
208 * Simple popup and disconnect when failback is invoked.
210 * Revision 1.18 1995/03/21 15:50:12 adam
213 * Revision 1.17 1995/03/21 13:41:03 adam
214 * Comstack cs_create not used too often. Non-blocking connect.
216 * Revision 1.16 1995/03/21 08:26:06 adam
217 * New method, setName, to specify the result set name (other than Default).
218 * New method, responseStatus, which returns diagnostic info, if any, after
219 * present response / search response.
221 * Revision 1.15 1995/03/20 15:24:07 adam
222 * Diagnostic records saved on searchResponse.
224 * Revision 1.14 1995/03/20 08:53:22 adam
225 * Event loop in tclmain.c rewritten. New method searchStatus.
227 * Revision 1.13 1995/03/17 18:26:17 adam
228 * Non-blocking i/o used now. Database names popup as cascade items.
230 * Revision 1.12 1995/03/17 15:45:00 adam
231 * Improved target/database setup.
233 * Revision 1.11 1995/03/16 17:54:03 adam
234 * Minor changes really.
236 * Revision 1.10 1995/03/15 16:14:50 adam
237 * Blocking arg in cs_create changed.
239 * Revision 1.9 1995/03/15 13:59:24 adam
242 * Revision 1.8 1995/03/15 08:25:16 adam
243 * New method presentStatus to check for error on present. Misc. cleanup
244 * of IrTcl_RecordList manipulations. Full MARC record presentation in
247 * Revision 1.7 1995/03/14 17:32:29 adam
248 * Presentation of full Marc record in popup window.
250 * Revision 1.6 1995/03/12 19:31:55 adam
251 * Pattern matching implemented when retrieving MARC records. More
252 * diagnostic functions.
254 * Revision 1.5 1995/03/10 18:00:15 adam
255 * Actual presentation in line-by-line format. RPN query support.
257 * Revision 1.4 1995/03/09 16:15:08 adam
258 * First presentRequest attempts. Hot-target list.
268 #include <sys/time.h>
279 int (*method) (void *obj, Tcl_Interp *interp, int argc, char **argv);
287 static void ir_deleteDiags (IrTcl_Diagnostic **dst_list, int *dst_num);
288 static int do_disconnect (void *obj, Tcl_Interp *interp,
289 int argc, char **argv);
291 static IrTcl_RecordList *new_IR_record (IrTcl_SetObj *setobj,
293 const char *elements)
295 IrTcl_RecordList *rl;
297 for (rl = setobj->record_list; rl; rl = rl->next)
299 if (no == rl->no && (!rl->elements || !elements ||
300 !strcmp(elements, rl->elements)))
305 case Z_NamePlusRecord_databaseRecord:
306 free (rl->u.dbrec.buf);
307 rl->u.dbrec.buf = NULL;
309 case Z_NamePlusRecord_surrogateDiagnostic:
310 ir_deleteDiags (&rl->u.surrogateDiagnostics.list,
311 &rl->u.surrogateDiagnostics.num);
319 rl = ir_tcl_malloc (sizeof(*rl));
320 rl->next = setobj->record_list;
322 setobj->record_list = rl;
325 ir_tcl_strdup (NULL, &rl->elements, elements);
330 enum oid_value value;
332 } IrTcl_recordSyntaxTab[] = {
333 { VAL_UNIMARC, "UNIMARC" },
334 { VAL_INTERMARC, "INTERMARC" },
336 { VAL_USMARC, "USMARC" },
337 { VAL_UKMARC, "UKMARC" },
338 { VAL_NORMARC, "NORMARC" },
339 { VAL_LIBRISMARC, "LIBRISMARC" },
340 { VAL_DANMARC, "DANMARC" },
341 { VAL_FINMARC, "FINMARC" },
343 { VAL_CANMARC, "CANMARC" },
345 { VAL_PICAMARC, "PICAMARC" },
346 { VAL_AUSMARC, "AUSMARC" },
347 { VAL_IBERMARC, "IBERMARC" },
348 { VAL_SUTRS, "SUTRS" },
349 { VAL_GRS1, "GRS1" },
356 int IrTcl_eval (Tcl_Interp *interp, const char *command)
358 char *tmp = ir_tcl_malloc (strlen(command)+1);
361 strcpy (tmp, command);
362 r = Tcl_Eval (interp, tmp);
364 logf (LOG_WARN, "Tcl error in line %d: %s", interp->errorLine,
366 Tcl_FreeResult (interp);
372 * IrTcl_getRecordSyntaxStr: Return record syntax name of object id
374 static const char *IrTcl_getRecordSyntaxStr (enum oid_value value)
377 for (i = 0; IrTcl_recordSyntaxTab[i].name; i++)
378 if (IrTcl_recordSyntaxTab[i].value == value)
379 return IrTcl_recordSyntaxTab[i].name;
384 * IrTcl_getRecordSyntaxVal: Return record syntax value of string
386 static enum oid_value IrTcl_getRecordSyntaxVal (const char *name)
389 for (i = 0; IrTcl_recordSyntaxTab[i].name; i++)
390 if (!strcmp (IrTcl_recordSyntaxTab[i].name, name))
391 return IrTcl_recordSyntaxTab[i].value;
395 static IrTcl_RecordList *find_IR_record (IrTcl_SetObj *setobj, int no)
397 IrTcl_RecordList *rl;
399 for (rl = setobj->record_list; rl; rl = rl->next)
401 (!setobj->recordElements || !rl->elements ||
402 !strcmp (setobj->recordElements, rl->elements)))
407 static void delete_IR_records (IrTcl_SetObj *setobj)
409 IrTcl_RecordList *rl, *rl1;
411 for (rl = setobj->record_list; rl; rl = rl1)
415 case Z_NamePlusRecord_databaseRecord:
416 free (rl->u.dbrec.buf);
418 case Z_NamePlusRecord_surrogateDiagnostic:
419 ir_deleteDiags (&rl->u.surrogateDiagnostics.list,
420 &rl->u.surrogateDiagnostics.num);
426 setobj->record_list = NULL;
430 * get_set_int: Set/get integer value
432 static int get_set_int (int *val, Tcl_Interp *interp, int argc, char **argv)
438 if (Tcl_GetInt (interp, argv[2], val)==TCL_ERROR)
441 sprintf (buf, "%d", *val);
442 Tcl_AppendResult (interp, buf, NULL);
447 * ir_method: Search for method in table and invoke method handler
449 int ir_method (Tcl_Interp *interp, int argc, char **argv, IrTcl_Methods *tab)
451 IrTcl_Methods *tab_i = tab;
454 for (tab_i = tab; tab_i->tab; tab_i++)
455 for (t = tab_i->tab; t->name; t++)
458 if ((*t->method)(tab_i->obj, interp, argc, argv) == TCL_ERROR)
462 if (!strcmp (t->name, argv[1]))
463 return (*t->method)(tab_i->obj, interp, argc, argv);
467 Tcl_AppendResult (interp, "Bad method: ", argv[1],
468 ". Possible methods:", NULL);
469 for (tab_i = tab; tab_i->tab; tab_i++)
470 for (t = tab_i->tab; t->name; t++)
471 Tcl_AppendResult (interp, " ", t->name, NULL);
476 * ir_method_r: Get status for all readable elements
478 int ir_method_r (void *obj, Tcl_Interp *interp, int argc, char **argv,
486 for (; tab->name; tab++)
489 argv_n[1] = tab->name;
490 Tcl_AppendResult (interp, "{", NULL);
491 (*tab->method)(obj, interp, argc_n, argv_n);
492 Tcl_AppendResult (interp, "} ", NULL);
498 * ir_named_bits: get/set named bits
500 int ir_named_bits (struct ir_named_entry *tab, Odr_bitmask *ob,
501 Tcl_Interp *interp, int argc, char **argv)
503 struct ir_named_entry *ti;
508 for (no = 0; no < argc; no++)
510 for (ti = tab; ti->name; ti++)
511 if (!strcmp (argv[no], ti->name))
513 ODR_MASK_SET (ob, ti->pos);
518 Tcl_AppendResult (interp, "Bad bit mask: ", argv[no], NULL);
524 for (ti = tab; ti->name; ti++)
525 if (ODR_MASK_GET (ob, ti->pos))
526 Tcl_AppendElement (interp, ti->name);
530 static void set_referenceId (ODR o, Z_ReferenceId **dst, const char *src)
536 *dst = odr_malloc (o, sizeof(**dst));
537 (*dst)->size = (*dst)->len = strlen(src);
538 (*dst)->buf = odr_malloc (o, (*dst)->len);
539 memcpy ((*dst)->buf, src, (*dst)->len);
543 static void get_referenceId (char **dst, Z_ReferenceId *src)
551 *dst = ir_tcl_malloc (src->len+1);
552 memcpy (*dst, src->buf, src->len);
553 (*dst)[src->len] = '\0';
556 /* ------------------------------------------------------- */
559 * do_init_request: init method on IR object
561 static int do_init_request (void *obj, Tcl_Interp *interp,
562 int argc, char **argv)
572 interp->result = "init: not connected";
575 apdu = zget_APDU (p->odr_out, Z_APDU_initRequest);
576 req = apdu->u.initRequest;
578 set_referenceId (p->odr_out, &req->referenceId, p->set_inher.referenceId);
579 req->options = &p->options;
580 req->protocolVersion = &p->protocolVersion;
581 req->preferredMessageSize = &p->preferredMessageSize;
582 req->maximumRecordSize = &p->maximumRecordSize;
584 if (p->idAuthenticationGroupId)
586 Z_IdPass *pass = odr_malloc (p->odr_out, sizeof(*pass));
587 Z_IdAuthentication *auth = odr_malloc (p->odr_out, sizeof(*auth));
589 logf (LOG_DEBUG, "using pass authentication");
591 auth->which = Z_IdAuthentication_idPass;
592 auth->u.idPass = pass;
593 if (p->idAuthenticationGroupId && *p->idAuthenticationGroupId)
594 pass->groupId = p->idAuthenticationGroupId;
596 pass->groupId = NULL;
597 if (p->idAuthenticationUserId && *p->idAuthenticationUserId)
598 pass->userId = p->idAuthenticationUserId;
601 if (p->idAuthenticationPassword && *p->idAuthenticationPassword)
602 pass->password = p->idAuthenticationPassword;
604 pass->password = NULL;
605 req->idAuthentication = auth;
607 else if (!p->idAuthenticationOpen || !*p->idAuthenticationOpen)
608 req->idAuthentication = NULL;
611 Z_IdAuthentication *auth = odr_malloc (p->odr_out, sizeof(*auth));
613 logf (LOG_DEBUG, "using open authentication");
614 auth->which = Z_IdAuthentication_open;
615 auth->u.open = p->idAuthenticationOpen;
616 req->idAuthentication = auth;
618 req->implementationId = p->implementationId;
619 req->implementationName = p->implementationName;
620 req->implementationVersion = p->implementationVersion;
621 req->userInformationField = 0;
623 return ir_tcl_send_APDU (interp, p, apdu, "init", *argv);
627 * do_protocolVersion: Set protocol Version
629 static int do_protocolVersion (void *obj, Tcl_Interp *interp,
630 int argc, char **argv)
638 ODR_MASK_ZERO (&p->protocolVersion);
639 ODR_MASK_SET (&p->protocolVersion, 0);
640 ODR_MASK_SET (&p->protocolVersion, 1);
645 if (Tcl_GetInt (interp, argv[2], &version)==TCL_ERROR)
647 ODR_MASK_ZERO (&p->protocolVersion);
648 for (i = 0; i<version; i++)
649 ODR_MASK_SET (&p->protocolVersion, i);
651 for (i = 4; --i >= 0; )
652 if (ODR_MASK_GET (&p->protocolVersion, i))
654 sprintf (buf, "%d", i+1);
655 interp->result = buf;
660 * do_options: Set options
662 static int do_options (void *obj, Tcl_Interp *interp,
663 int argc, char **argv)
665 static struct ir_named_entry options_tab[] = {
669 { "resourceReport", 3 },
670 { "triggerResourceCtrl", 4},
671 { "resourceCtrl", 5},
675 { "extendedServices", 10},
676 { "level-1Segmentation", 11},
677 { "level-2Segmentation", 12},
678 { "concurrentOperations", 13},
679 { "namedResultSets", 14},
686 ODR_MASK_ZERO (&p->options);
687 ODR_MASK_SET (&p->options, 0);
688 ODR_MASK_SET (&p->options, 1);
689 ODR_MASK_SET (&p->options, 4);
690 ODR_MASK_SET (&p->options, 7);
691 ODR_MASK_SET (&p->options, 14);
694 return ir_named_bits (options_tab, &p->options, interp, argc-2, argv+2);
698 * do_apduInfo: Get APDU information
700 static int do_apduInfo (void *obj, Tcl_Interp *interp, int argc, char **argv)
708 sprintf (buf, "%d", p->apduLen);
709 Tcl_AppendElement (interp, buf);
710 sprintf (buf, "%d", p->apduOffset);
711 Tcl_AppendElement (interp, buf);
714 Tcl_AppendElement (interp, "");
717 apduf = fopen ("apdu.tmp", "w");
720 Tcl_AppendElement (interp, "");
723 odr_dumpBER (apduf, p->buf_in, p->apduLen);
725 if (!(apduf = fopen ("apdu.tmp", "r")))
726 Tcl_AppendElement (interp, "");
731 Tcl_AppendResult (interp, " {", NULL);
732 while ((c = getc (apduf)) != EOF)
736 Tcl_AppendResult (interp, buf, NULL);
739 Tcl_AppendResult (interp, "}", NULL);
746 * do_failInfo: Get fail information
748 static int do_failInfo (void *obj, Tcl_Interp *interp, int argc, char **argv)
758 sprintf (buf, "%d", p->failInfo);
764 case IR_TCL_FAIL_CONNECT:
765 cp = "connect failed";
767 case IR_TCL_FAIL_READ:
768 cp = "connection closed";
770 case IR_TCL_FAIL_WRITE:
771 cp = "connection closed";
773 case IR_TCL_FAIL_IN_APDU:
774 cp = "failed to decode incoming APDU";
776 case IR_TCL_FAIL_UNKNOWN_APDU:
782 Tcl_AppendElement (interp, buf);
783 Tcl_AppendElement (interp, cp);
788 * do_preferredMessageSize: Set/get preferred message size
790 static int do_preferredMessageSize (void *obj, Tcl_Interp *interp,
791 int argc, char **argv)
797 p->preferredMessageSize = 30000;
800 return get_set_int (&p->preferredMessageSize, interp, argc, argv);
804 * do_maximumRecordSize: Set/get maximum record size
806 static int do_maximumRecordSize (void *obj, Tcl_Interp *interp,
807 int argc, char **argv)
813 p->maximumRecordSize = 30000;
816 return get_set_int (&p->maximumRecordSize, interp, argc, argv);
820 * do_initResult: Get init result
822 static int do_initResult (void *obj, Tcl_Interp *interp,
823 int argc, char **argv)
829 return get_set_int (&p->initResult, interp, argc, argv);
834 * do_implementationName: Set/get Implementation Name.
836 static int do_implementationName (void *obj, Tcl_Interp *interp,
837 int argc, char **argv)
842 return ir_tcl_strdup (interp, &p->implementationName,
843 "Index Data/IrTcl on YAZ");
845 return ir_tcl_strdel (interp, &p->implementationName);
848 free (p->implementationName);
849 if (ir_tcl_strdup (interp, &p->implementationName, argv[2])
853 Tcl_AppendResult (interp, p->implementationName, (char*) NULL);
858 * do_implementationId: Get Implementation Id.
860 static int do_implementationId (void *obj, Tcl_Interp *interp,
861 int argc, char **argv)
866 return ir_tcl_strdup (interp, &p->implementationId, "YAZ (id=81)");
868 return ir_tcl_strdel (interp, &p->implementationId);
869 Tcl_AppendResult (interp, p->implementationId, (char*) NULL);
874 * do_implementationVersion: get Implementation Version.
876 static int do_implementationVersion (void *obj, Tcl_Interp *interp,
877 int argc, char **argv)
882 return ir_tcl_strdup (interp, &p->implementationVersion,
883 "YAZ: " YAZ_VERSION " / IrTcl: " IR_TCL_VERSION);
885 return ir_tcl_strdel (interp, &p->implementationVersion);
886 Tcl_AppendResult (interp, p->implementationVersion, (char*) NULL);
891 * do_targetImplementationName: Get Implementation Name of target.
893 static int do_targetImplementationName (void *obj, Tcl_Interp *interp,
894 int argc, char **argv)
900 p->targetImplementationName = NULL;
904 return ir_tcl_strdel (interp, &p->targetImplementationName);
905 Tcl_AppendResult (interp, p->targetImplementationName, (char*) NULL);
910 * do_targetImplementationId: Get Implementation Id of target
912 static int do_targetImplementationId (void *obj, Tcl_Interp *interp,
913 int argc, char **argv)
919 p->targetImplementationId = NULL;
923 return ir_tcl_strdel (interp, &p->targetImplementationId);
924 Tcl_AppendResult (interp, p->targetImplementationId, (char*) NULL);
929 * do_targetImplementationVersion: Get Implementation Version of target
931 static int do_targetImplementationVersion (void *obj, Tcl_Interp *interp,
932 int argc, char **argv)
938 p->targetImplementationVersion = NULL;
942 return ir_tcl_strdel (interp, &p->targetImplementationVersion);
943 Tcl_AppendResult (interp, p->targetImplementationVersion, (char*) NULL);
948 * do_idAuthentication: Set/get id Authentication
950 static int do_idAuthentication (void *obj, Tcl_Interp *interp,
951 int argc, char **argv)
955 if (argc >= 3 || argc == -1)
957 free (p->idAuthenticationOpen);
958 free (p->idAuthenticationGroupId);
959 free (p->idAuthenticationUserId);
960 free (p->idAuthenticationPassword);
962 if (argc >= 3 || argc <= 0)
964 p->idAuthenticationOpen = NULL;
965 p->idAuthenticationGroupId = NULL;
966 p->idAuthenticationUserId = NULL;
967 p->idAuthenticationPassword = NULL;
976 ir_tcl_strdup (interp, &p->idAuthenticationOpen, argv[2])
983 ir_tcl_strdup (interp, &p->idAuthenticationGroupId, argv[2])
987 ir_tcl_strdup (interp, &p->idAuthenticationUserId, argv[3])
991 ir_tcl_strdup (interp, &p->idAuthenticationPassword, argv[4])
996 if (p->idAuthenticationOpen)
997 Tcl_AppendElement (interp, p->idAuthenticationOpen);
998 else if (p->idAuthenticationGroupId)
1000 Tcl_AppendElement (interp, p->idAuthenticationGroupId);
1001 Tcl_AppendElement (interp, p->idAuthenticationUserId);
1002 Tcl_AppendElement (interp, p->idAuthenticationPassword);
1008 * do_connect: connect method on IR object
1010 static int do_connect (void *obj, Tcl_Interp *interp,
1011 int argc, char **argv)
1023 interp->result = "already connected";
1026 if (ir_tcl_strdup (interp, &p->hostname, argv[2]) == TCL_ERROR)
1028 if (!strcmp (p->comstackType, "tcpip"))
1030 p->cs_link = cs_create (tcpip_type, CS_BLOCK, p->protocol_type);
1031 addr = tcpip_strtoaddr (argv[2]);
1034 interp->result = "tcpip_strtoaddr fail";
1037 logf (LOG_DEBUG, "tcp/ip connect %s", argv[2]);
1039 else if (!strcmp (p->comstackType, "mosi"))
1042 p->cs_link = cs_create (mosi_type, CS_BLOCK, p->protocol_type);
1043 addr = mosi_strtoaddr (argv[2]);
1046 interp->result = "mosi_strtoaddr fail";
1049 logf (LOG_DEBUG, "mosi connect %s", argv[2]);
1051 interp->result = "MOSI support not there";
1057 Tcl_AppendResult (interp, "Bad comstack type: ",
1058 p->comstackType, NULL);
1061 if ((r=cs_connect (p->cs_link, addr)) < 0)
1063 interp->result = "connect fail";
1064 do_disconnect (p, NULL, 2, NULL);
1067 logf(LOG_DEBUG, "cs_connect() returned %d", r);
1068 p->eventType = "connect";
1069 ir_select_add (cs_fileno (p->cs_link), p);
1072 ir_select_add_write (cs_fileno (p->cs_link), p);
1073 p->state = IR_TCL_R_Connecting;
1077 p->state = IR_TCL_R_Idle;
1079 IrTcl_eval (p->interp, p->callback);
1083 Tcl_AppendResult (interp, p->hostname, NULL);
1088 * do_disconnect: disconnect method on IR object
1090 static int do_disconnect (void *obj, Tcl_Interp *interp,
1091 int argc, char **argv)
1097 p->state = IR_TCL_R_Idle;
1098 p->eventType = NULL;
1107 ir_select_remove_write (cs_fileno (p->cs_link), p);
1108 ir_select_remove (cs_fileno (p->cs_link), p);
1110 odr_reset (p->odr_in);
1112 assert (p->cs_link);
1113 logf(LOG_DEBUG, "Closing connection");
1114 cs_close (p->cs_link);
1117 ODR_MASK_ZERO (&p->options);
1118 ODR_MASK_SET (&p->options, 0);
1119 ODR_MASK_SET (&p->options, 1);
1120 ODR_MASK_SET (&p->options, 4);
1121 ODR_MASK_SET (&p->options, 7);
1122 ODR_MASK_SET (&p->options, 14);
1124 ODR_MASK_ZERO (&p->protocolVersion);
1125 ODR_MASK_SET (&p->protocolVersion, 0);
1126 ODR_MASK_SET (&p->protocolVersion, 1);
1129 assert (!p->cs_link);
1134 * do_comstack: Set/get comstack method on IR object
1136 static int do_comstack (void *o, Tcl_Interp *interp,
1137 int argc, char **argv)
1142 return ir_tcl_strdup (interp, &obj->comstackType, "tcpip");
1143 else if (argc == -1)
1144 return ir_tcl_strdel (interp, &obj->comstackType);
1147 free (obj->comstackType);
1148 if (ir_tcl_strdup (interp, &obj->comstackType, argv[2]) == TCL_ERROR)
1151 Tcl_AppendElement (interp, obj->comstackType);
1156 * do_logLevel: Set log level
1158 static int do_logLevel (void *o, Tcl_Interp *interp,
1159 int argc, char **argv)
1164 log_init (log_mask_str (argv[2]), "", NULL);
1166 log_init (log_mask_str (argv[2]), argv[3], NULL);
1168 log_init (log_mask_str (argv[2]), argv[3], argv[4]);
1174 * do_eventType: Return type of last event
1176 static int do_eventType (void *obj, Tcl_Interp *interp,
1177 int argc, char **argv)
1183 p->eventType = NULL;
1186 Tcl_AppendElement (interp, p->eventType ? p->eventType : "");
1192 * do_callback: add callback
1194 static int do_callback (void *obj, Tcl_Interp *interp,
1195 int argc, char **argv)
1204 else if (argc == -1)
1205 return ir_tcl_strdel (interp, &p->callback);
1211 if (ir_tcl_strdup (interp, &p->callback, argv[2]) == TCL_ERROR)
1221 * do_failback: add error handle callback
1223 static int do_failback (void *obj, Tcl_Interp *interp,
1224 int argc, char **argv)
1233 else if (argc == -1)
1234 return ir_tcl_strdel (interp, &p->failback);
1240 if (ir_tcl_strdup (interp, &p->failback, argv[2]) == TCL_ERROR)
1250 * do_initResponse: add init response handler
1252 static int do_initResponse (void *obj, Tcl_Interp *interp,
1253 int argc, char **argv)
1259 p->initResponse = NULL;
1262 else if (argc == -1)
1263 return ir_tcl_strdel (interp, &p->initResponse);
1266 free (p->initResponse);
1269 if (ir_tcl_strdup (interp, &p->initResponse, argv[2]) == TCL_ERROR)
1273 p->initResponse = NULL;
1278 * do_protocol: Set/get protocol method on IR object
1280 static int do_protocol (void *o, Tcl_Interp *interp, int argc, char **argv)
1286 p->protocol_type = PROTO_Z3950;
1291 if (!strcmp (argv[2], "Z39"))
1292 p->protocol_type = PROTO_Z3950;
1293 else if (!strcmp (argv[2], "SR"))
1294 p->protocol_type = PROTO_SR;
1297 Tcl_AppendResult (interp, "Bad protocol: ", argv[2], NULL);
1302 switch (p->protocol_type)
1305 Tcl_AppendElement (interp, "Z39");
1308 Tcl_AppendElement (interp, "SR");
1315 * do_triggerResourceControl:
1317 static int do_triggerResourceControl (void *obj, Tcl_Interp *interp,
1318 int argc, char **argv)
1322 Z_TriggerResourceControlRequest *req;
1323 bool_t is_false = 0;
1329 interp->result = "triggerResourceControl: not connected";
1332 apdu = zget_APDU (p->odr_out, Z_APDU_triggerResourceControlRequest);
1333 req = apdu->u.triggerResourceControlRequest;
1334 *req->requestedAction = Z_TriggerResourceCtrl_cancel;
1335 req->resultSetWanted = &is_false;
1337 return ir_tcl_send_APDU (interp, p, apdu, "triggerResourceControl",
1342 * do_databaseNames: specify database names
1344 static int do_databaseNames (void *obj, Tcl_Interp *interp,
1345 int argc, char **argv)
1348 IrTcl_SetCObj *p = obj;
1352 for (i=0; i<p->num_databaseNames; i++)
1353 free (p->databaseNames[i]);
1354 free (p->databaseNames);
1358 p->num_databaseNames = 0;
1359 p->databaseNames = NULL;
1364 for (i=0; i<p->num_databaseNames; i++)
1365 Tcl_AppendElement (interp, p->databaseNames[i]);
1368 if (p->databaseNames)
1370 for (i=0; i<p->num_databaseNames; i++)
1371 free (p->databaseNames[i]);
1372 free (p->databaseNames);
1374 p->num_databaseNames = argc - 2;
1376 ir_tcl_malloc (sizeof(*p->databaseNames) * p->num_databaseNames);
1377 for (i=0; i<p->num_databaseNames; i++)
1379 if (ir_tcl_strdup (interp, &p->databaseNames[i], argv[2+i])
1387 * do_replaceIndicator: Set/get replace Set indicator
1389 static int do_replaceIndicator (void *obj, Tcl_Interp *interp,
1390 int argc, char **argv)
1392 IrTcl_SetCObj *p = obj;
1396 p->replaceIndicator = 1;
1399 return get_set_int (&p->replaceIndicator, interp, argc, argv);
1403 * do_queryType: Set/Get query method
1405 static int do_queryType (void *obj, Tcl_Interp *interp,
1406 int argc, char **argv)
1408 IrTcl_SetCObj *p = obj;
1411 return ir_tcl_strdup (interp, &p->queryType, "rpn");
1412 else if (argc == -1)
1413 return ir_tcl_strdel (interp, &p->queryType);
1416 free (p->queryType);
1417 if (ir_tcl_strdup (interp, &p->queryType, argv[2]) == TCL_ERROR)
1420 Tcl_AppendResult (interp, p->queryType, NULL);
1425 * do_userInformationField: Get User information field
1427 static int do_userInformationField (void *obj, Tcl_Interp *interp,
1428 int argc, char **argv)
1434 p->userInformationField = NULL;
1437 else if (argc == -1)
1438 return ir_tcl_strdel (interp, &p->userInformationField);
1439 Tcl_AppendResult (interp, p->userInformationField, NULL);
1444 * do_smallSetUpperBound: Set/get small set upper bound
1446 static int do_smallSetUpperBound (void *o, Tcl_Interp *interp,
1447 int argc, char **argv)
1449 IrTcl_SetCObj *p = o;
1453 p->smallSetUpperBound = 0;
1456 return get_set_int (&p->smallSetUpperBound, interp, argc, argv);
1460 * do_largeSetLowerBound: Set/get large set lower bound
1462 static int do_largeSetLowerBound (void *o, Tcl_Interp *interp,
1463 int argc, char **argv)
1465 IrTcl_SetCObj *p = o;
1469 p->largeSetLowerBound = 2;
1472 return get_set_int (&p->largeSetLowerBound, interp, argc, argv);
1476 * do_mediumSetPresentNumber: Set/get large set lower bound
1478 static int do_mediumSetPresentNumber (void *o, Tcl_Interp *interp,
1479 int argc, char **argv)
1481 IrTcl_SetCObj *p = o;
1485 p->mediumSetPresentNumber = 0;
1488 return get_set_int (&p->mediumSetPresentNumber, interp, argc, argv);
1492 * do_referenceId: Set/Get referenceId
1494 static int do_referenceId (void *obj, Tcl_Interp *interp,
1495 int argc, char **argv)
1497 IrTcl_SetCObj *p = obj;
1501 p->referenceId = NULL;
1504 else if (argc == -1)
1505 return ir_tcl_strdel (interp, &p->referenceId);
1508 free (p->referenceId);
1509 if (ir_tcl_strdup (interp, &p->referenceId, argv[2]) == TCL_ERROR)
1512 Tcl_AppendResult (interp, p->referenceId, NULL);
1517 * do_preferredRecordSyntax: Set/get preferred record syntax
1519 static int do_preferredRecordSyntax (void *obj, Tcl_Interp *interp,
1520 int argc, char **argv)
1522 IrTcl_SetCObj *p = obj;
1526 p->preferredRecordSyntax = NULL;
1529 else if (argc == -1)
1531 free (p->preferredRecordSyntax);
1532 p->preferredRecordSyntax = NULL;
1537 free (p->preferredRecordSyntax);
1538 p->preferredRecordSyntax = NULL;
1539 if (argv[2][0] && (p->preferredRecordSyntax =
1540 ir_tcl_malloc (sizeof(*p->preferredRecordSyntax))))
1541 *p->preferredRecordSyntax = IrTcl_getRecordSyntaxVal (argv[2]);
1548 * do_elementSetNames: Set/Get element Set Names
1550 static int do_elementSetNames (void *obj, Tcl_Interp *interp,
1551 int argc, char **argv)
1553 IrTcl_SetCObj *p = obj;
1557 p->elementSetNames = NULL;
1560 else if (argc == -1)
1561 return ir_tcl_strdel (interp, &p->elementSetNames);
1564 free (p->elementSetNames);
1565 if (ir_tcl_strdup (interp, &p->elementSetNames, argv[2]) == TCL_ERROR)
1568 Tcl_AppendResult (interp, p->elementSetNames, NULL);
1573 * do_smallSetElementSetNames: Set/Get small Set Element Set Names
1575 static int do_smallSetElementSetNames (void *obj, Tcl_Interp *interp,
1576 int argc, char **argv)
1578 IrTcl_SetCObj *p = obj;
1582 p->smallSetElementSetNames = NULL;
1585 else if (argc == -1)
1586 return ir_tcl_strdel (interp, &p->smallSetElementSetNames);
1589 free (p->smallSetElementSetNames);
1590 if (ir_tcl_strdup (interp, &p->smallSetElementSetNames,
1591 argv[2]) == TCL_ERROR)
1594 Tcl_AppendResult (interp, p->smallSetElementSetNames, NULL);
1599 * do_mediumSetElementSetNames: Set/Get medium Set Element Set Names
1601 static int do_mediumSetElementSetNames (void *obj, Tcl_Interp *interp,
1602 int argc, char **argv)
1604 IrTcl_SetCObj *p = obj;
1608 p->mediumSetElementSetNames = NULL;
1611 else if (argc == -1)
1612 return ir_tcl_strdel (interp, &p->mediumSetElementSetNames);
1615 free (p->mediumSetElementSetNames);
1616 if (ir_tcl_strdup (interp, &p->mediumSetElementSetNames,
1617 argv[2]) == TCL_ERROR)
1620 Tcl_AppendResult (interp, p->mediumSetElementSetNames, NULL);
1624 static IrTcl_Method ir_method_tab[] = {
1625 { 1, "comstack", do_comstack },
1626 { 1, "protocol", do_protocol },
1627 { 0, "failback", do_failback },
1628 { 0, "failInfo", do_failInfo },
1629 { 0, "apduInfo", do_apduInfo },
1630 { 0, "logLevel", do_logLevel },
1632 { 0, "eventType", do_eventType },
1633 { 1, "connect", do_connect },
1634 { 0, "protocolVersion", do_protocolVersion },
1635 { 1, "preferredMessageSize", do_preferredMessageSize },
1636 { 1, "maximumRecordSize", do_maximumRecordSize },
1637 { 1, "implementationName", do_implementationName },
1638 { 1, "implementationId", do_implementationId },
1639 { 1, "implementationVersion", do_implementationVersion },
1640 { 0, "targetImplementationName", do_targetImplementationName },
1641 { 0, "targetImplementationId", do_targetImplementationId },
1642 { 0, "targetImplementationVersion", do_targetImplementationVersion },
1643 { 0, "userInformationField", do_userInformationField },
1644 { 1, "idAuthentication", do_idAuthentication },
1645 { 0, "options", do_options },
1646 { 0, "init", do_init_request },
1647 { 0, "initResult", do_initResult },
1648 { 0, "disconnect", do_disconnect },
1649 { 0, "callback", do_callback },
1650 { 0, "initResponse", do_initResponse },
1651 { 0, "triggerResourceControl", do_triggerResourceControl },
1652 { 0, "initResponse", do_initResponse },
1656 static IrTcl_Method ir_set_c_method_tab[] = {
1657 { 0, "databaseNames", do_databaseNames},
1658 { 0, "replaceIndicator", do_replaceIndicator},
1659 { 0, "queryType", do_queryType },
1660 { 0, "preferredRecordSyntax", do_preferredRecordSyntax },
1661 { 0, "smallSetUpperBound", do_smallSetUpperBound},
1662 { 0, "largeSetLowerBound", do_largeSetLowerBound},
1663 { 0, "mediumSetPresentNumber", do_mediumSetPresentNumber},
1664 { 0, "referenceId", do_referenceId },
1665 { 0, "elementSetNames", do_elementSetNames },
1666 { 0, "smallSetElementSetNames", do_smallSetElementSetNames },
1667 { 0, "mediumSetElementSetNames", do_mediumSetElementSetNames },
1672 * ir_obj_method: IR Object methods
1674 static int ir_obj_method (ClientData clientData, Tcl_Interp *interp,
1675 int argc, char **argv)
1677 IrTcl_Methods tab[3];
1678 IrTcl_Obj *p = clientData;
1681 return ir_method_r (clientData, interp, argc, argv, ir_method_tab);
1683 tab[0].tab = ir_method_tab;
1685 tab[1].tab = ir_set_c_method_tab;
1686 tab[1].obj = &p->set_inher;
1689 return ir_method (interp, argc, argv, tab);
1693 * ir_obj_delete: IR Object disposal
1695 static void ir_obj_delete (ClientData clientData)
1697 IrTcl_Obj *obj = clientData;
1698 IrTcl_Methods tab[3];
1701 if (obj->ref_count > 0)
1703 assert (obj->ref_count == 0);
1705 logf (LOG_DEBUG, "ir object delete");
1706 tab[0].tab = ir_method_tab;
1708 tab[1].tab = ir_set_c_method_tab;
1709 tab[1].obj = &obj->set_inher;
1712 ir_method (NULL, -1, NULL, tab);
1715 odr_destroy (obj->odr_in);
1716 odr_destroy (obj->odr_out);
1717 odr_destroy (obj->odr_pr);
1722 * ir_obj_mk: IR Object creation
1724 static int ir_obj_mk (ClientData clientData, Tcl_Interp *interp,
1725 int argc, char **argv)
1727 IrTcl_Methods tab[3];
1735 interp->result = "wrong # args";
1738 obj = ir_tcl_malloc (sizeof(*obj));
1741 obj->bibset = ccl_qual_mk ();
1742 if ((inf = fopen ("default.bib", "r")))
1744 ccl_qual_file (obj->bibset, inf);
1749 logf (LOG_DEBUG, "ir object create");
1750 obj->odr_in = odr_createmem (ODR_DECODE);
1751 obj->odr_out = odr_createmem (ODR_ENCODE);
1752 obj->odr_pr = odr_createmem (ODR_PRINT);
1753 obj->state = IR_TCL_R_Idle;
1754 obj->interp = interp;
1758 obj->request_queue = NULL;
1760 tab[0].tab = ir_method_tab;
1762 tab[1].tab = ir_set_c_method_tab;
1763 tab[1].obj = &obj->set_inher;
1766 if (ir_method (interp, 0, NULL, tab) == TCL_ERROR)
1768 Tcl_CreateCommand (interp, argv[1], ir_obj_method,
1769 (ClientData) obj, ir_obj_delete);
1773 /* ------------------------------------------------------- */
1775 * do_search: Do search request
1777 static int do_search (void *o, Tcl_Interp *interp, int argc, char **argv)
1779 Z_SearchRequest *req;
1783 IrTcl_SetObj *obj = o;
1794 interp->result = "wrong # args";
1797 if (!obj->set_inher.num_databaseNames)
1799 interp->result = "no databaseNames";
1804 interp->result = "search: not connected";
1807 apdu = zget_APDU (p->odr_out, Z_APDU_searchRequest);
1808 req = apdu->u.searchRequest;
1812 bib1.proto = p->protocol_type;
1813 bib1.oclass = CLASS_ATTSET;
1814 bib1.value = VAL_BIB1;
1816 set_referenceId (p->odr_out, &req->referenceId,
1817 obj->set_inher.referenceId);
1819 req->smallSetUpperBound = &obj->set_inher.smallSetUpperBound;
1820 req->largeSetLowerBound = &obj->set_inher.largeSetLowerBound;
1821 req->mediumSetPresentNumber = &obj->set_inher.mediumSetPresentNumber;
1822 req->replaceIndicator = &obj->set_inher.replaceIndicator;
1823 req->resultSetName = obj->setName ? obj->setName : "Default";
1824 logf (LOG_DEBUG, "Search, resultSetName %s", req->resultSetName);
1825 req->num_databaseNames = obj->set_inher.num_databaseNames;
1826 req->databaseNames = obj->set_inher.databaseNames;
1827 for (r=0; r < obj->set_inher.num_databaseNames; r++)
1828 logf (LOG_DEBUG, " Database %s", obj->set_inher.databaseNames[r]);
1829 if (obj->set_inher.preferredRecordSyntax)
1831 struct oident ident;
1833 ident.proto = p->protocol_type;
1834 ident.oclass = CLASS_RECSYN;
1835 ident.value = *obj->set_inher.preferredRecordSyntax;
1836 logf (LOG_DEBUG, "Preferred record syntax is %d", ident.value);
1837 req->preferredRecordSyntax = odr_oiddup (p->odr_out,
1838 oid_getoidbyent (&ident));
1841 req->preferredRecordSyntax = 0;
1843 if (obj->set_inher.smallSetElementSetNames &&
1844 *obj->set_inher.smallSetElementSetNames)
1846 Z_ElementSetNames *esn = odr_malloc (p->odr_out, sizeof(*esn));
1848 esn->which = Z_ElementSetNames_generic;
1849 esn->u.generic = obj->set_inher.smallSetElementSetNames;
1850 req->smallSetElementSetNames = esn;
1853 req->smallSetElementSetNames = NULL;
1855 if (obj->set_inher.mediumSetElementSetNames &&
1856 *obj->set_inher.mediumSetElementSetNames)
1858 Z_ElementSetNames *esn = odr_malloc (p->odr_out, sizeof(*esn));
1860 esn->which = Z_ElementSetNames_generic;
1861 esn->u.generic = obj->set_inher.mediumSetElementSetNames;
1862 req->mediumSetElementSetNames = esn;
1865 req->mediumSetElementSetNames = NULL;
1867 req->query = &query;
1869 if (!strcmp (obj->set_inher.queryType, "rpn"))
1871 Z_RPNQuery *RPNquery;
1873 RPNquery = p_query_rpn (p->odr_out, argv[2]);
1876 Tcl_AppendResult (interp, "Syntax error in query", NULL);
1879 RPNquery->attributeSetId = oid_getoidbyent (&bib1);
1880 query.which = Z_Query_type_1;
1881 query.u.type_1 = RPNquery;
1882 logf (LOG_DEBUG, "RPN");
1885 else if (!strcmp (obj->set_inher.queryType, "cclrpn"))
1889 struct ccl_rpn_node *rpn;
1890 Z_RPNQuery *RPNquery;
1892 rpn = ccl_find_str(p->bibset, argv[2], &error, &pos);
1895 Tcl_AppendResult (interp, "CCL error: ",
1896 ccl_err_msg(error), NULL);
1899 ccl_pr_tree (rpn, stderr);
1900 fprintf (stderr, "\n");
1901 assert((RPNquery = ccl_rpn_query(rpn)));
1902 RPNquery->attributeSetId = oid_getoidbyent (&bib1);
1903 query.which = Z_Query_type_1;
1904 query.u.type_1 = RPNquery;
1905 logf (LOG_DEBUG, "CCLRPN");
1908 else if (!strcmp (obj->set_inher.queryType, "ccl"))
1910 query.which = Z_Query_type_2;
1911 query.u.type_2 = &ccl_query;
1912 ccl_query.buf = (unsigned char *) argv[2];
1913 ccl_query.len = strlen (argv[2]);
1914 logf (LOG_DEBUG, "CCL");
1918 interp->result = "unknown query method";
1921 return ir_tcl_send_APDU (interp, p, apdu, "search", *argv);
1925 * do_searchResponse: add search response handler
1927 static int do_searchResponse (void *o, Tcl_Interp *interp,
1928 int argc, char **argv)
1930 IrTcl_SetObj *obj = o;
1934 obj->searchResponse = NULL;
1937 else if (argc == -1)
1938 return ir_tcl_strdel (interp, &obj->searchResponse);
1941 free (obj->searchResponse);
1944 if (ir_tcl_strdup (interp, &obj->searchResponse, argv[2])
1949 obj->searchResponse = NULL;
1955 * do_presentResponse: add present response handler
1957 static int do_presentResponse (void *o, Tcl_Interp *interp,
1958 int argc, char **argv)
1960 IrTcl_SetObj *obj = o;
1964 obj->presentResponse = NULL;
1967 else if (argc == -1)
1968 return ir_tcl_strdel (interp, &obj->presentResponse);
1971 free (obj->presentResponse);
1974 if (ir_tcl_strdup (interp, &obj->presentResponse, argv[2])
1979 obj->presentResponse = NULL;
1985 * do_resultCount: Get number of hits
1987 static int do_resultCount (void *o, Tcl_Interp *interp,
1988 int argc, char **argv)
1990 IrTcl_SetObj *obj = o;
1994 return get_set_int (&obj->resultCount, interp, argc, argv);
1998 * do_searchStatus: Get search status (after search response)
2000 static int do_searchStatus (void *o, Tcl_Interp *interp,
2001 int argc, char **argv)
2003 IrTcl_SetObj *obj = o;
2007 return get_set_int (&obj->searchStatus, interp, argc, argv);
2011 * do_presentStatus: Get search status (after search/present response)
2013 static int do_presentStatus (void *o, Tcl_Interp *interp,
2014 int argc, char **argv)
2016 IrTcl_SetObj *obj = o;
2020 return get_set_int (&obj->presentStatus, interp, argc, argv);
2024 * do_nextResultSetPosition: Get next result set position
2025 * (after search/present response)
2027 static int do_nextResultSetPosition (void *o, Tcl_Interp *interp,
2028 int argc, char **argv)
2030 IrTcl_SetObj *obj = o;
2034 obj->nextResultSetPosition = 0;
2037 return get_set_int (&obj->nextResultSetPosition, interp, argc, argv);
2041 * do_setName: Set result Set name
2043 static int do_setName (void *o, Tcl_Interp *interp,
2044 int argc, char **argv)
2046 IrTcl_SetObj *obj = o;
2049 return ir_tcl_strdup (interp, &obj->setName, "Default");
2050 else if (argc == -1)
2051 return ir_tcl_strdel (interp, &obj->setName);
2054 free (obj->setName);
2055 if (ir_tcl_strdup (interp, &obj->setName, argv[2])
2059 Tcl_AppendElement (interp, obj->setName);
2064 * do_numberOfRecordsReturned: Get number of records returned
2066 static int do_numberOfRecordsReturned (void *o, Tcl_Interp *interp,
2067 int argc, char **argv)
2069 IrTcl_SetObj *obj = o;
2073 obj->numberOfRecordsReturned = 0;
2076 return get_set_int (&obj->numberOfRecordsReturned, interp, argc, argv);
2080 * do_type: Return type (if any) at position.
2082 static int do_type (void *o, Tcl_Interp *interp, int argc, char **argv)
2084 IrTcl_SetObj *obj = o;
2086 IrTcl_RecordList *rl;
2090 obj->record_list = NULL;
2093 else if (argc == -1)
2095 delete_IR_records (obj);
2100 sprintf (interp->result, "wrong # args");
2103 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2105 rl = find_IR_record (obj, offset);
2108 logf (LOG_DEBUG, "No record at position %d", offset);
2113 case Z_NamePlusRecord_databaseRecord:
2114 interp->result = "DB";
2116 case Z_NamePlusRecord_surrogateDiagnostic:
2117 interp->result = "SD";
2125 * do_recordType: Return record type (if any) at position.
2127 static int do_recordType (void *o, Tcl_Interp *interp, int argc, char **argv)
2129 IrTcl_SetObj *obj = o;
2131 IrTcl_RecordList *rl;
2137 else if (argc == -1)
2143 sprintf (interp->result, "wrong # args");
2146 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2148 rl = find_IR_record (obj, offset);
2151 if (rl->which != Z_NamePlusRecord_databaseRecord)
2153 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2156 Tcl_AppendElement (interp, (char*)
2157 IrTcl_getRecordSyntaxStr (rl->u.dbrec.type));
2162 * set record elements (for record extraction)
2164 static int do_recordElements (void *o, Tcl_Interp *interp,
2165 int argc, char **argv)
2167 IrTcl_SetObj *obj = o;
2171 obj->recordElements = NULL;
2174 else if (argc == -1)
2175 return ir_tcl_strdel (NULL, &obj->recordElements);
2178 sprintf (interp->result, "wrong # args");
2183 free (obj->recordElements);
2184 return ir_tcl_strdup (NULL, &obj->recordElements,
2185 (*argv[2] ? argv[2] : NULL));
2187 Tcl_AppendResult (interp, obj->recordElements, NULL);
2194 static int ir_diagResult (Tcl_Interp *interp, IrTcl_Diagnostic *list, int num)
2200 for (i = 0; i<num; i++)
2202 logf (LOG_DEBUG, "Diagnostic, code %d", list[i].condition);
2203 sprintf (buf, "%d", list[i].condition);
2204 Tcl_AppendElement (interp, buf);
2205 cp = diagbib1_str (list[i].condition);
2207 Tcl_AppendElement (interp, (char*) cp);
2209 Tcl_AppendElement (interp, "");
2210 if (list[i].addinfo)
2211 Tcl_AppendElement (interp, (char*) list[i].addinfo);
2213 Tcl_AppendElement (interp, "");
2219 * do_diag: Return diagnostic record info
2221 static int do_diag (void *o, Tcl_Interp *interp, int argc, char **argv)
2223 IrTcl_SetObj *obj = o;
2225 IrTcl_RecordList *rl;
2231 sprintf (interp->result, "wrong # args");
2234 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2236 rl = find_IR_record (obj, offset);
2239 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2242 if (rl->which != Z_NamePlusRecord_surrogateDiagnostic)
2244 Tcl_AppendResult (interp, "No Diagnostic record at #", argv[2], NULL);
2247 return ir_diagResult (interp, rl->u.surrogateDiagnostics.list,
2248 rl->u.surrogateDiagnostics.num);
2252 * do_getMarc: Get ISO2709 Record lines/fields
2254 static int do_getMarc (void *o, Tcl_Interp *interp, int argc, char **argv)
2256 IrTcl_SetObj *obj = o;
2258 IrTcl_RecordList *rl;
2264 sprintf (interp->result, "wrong # args");
2267 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2269 rl = find_IR_record (obj, offset);
2272 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2275 if (rl->which != Z_NamePlusRecord_databaseRecord)
2277 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2280 return ir_tcl_get_marc (interp, rl->u.dbrec.buf, argc, argv);
2284 * do_getSutrs: Get SUTRS Record
2286 static int do_getSutrs (void *o, Tcl_Interp *interp, int argc, char **argv)
2288 IrTcl_SetObj *obj = o;
2290 IrTcl_RecordList *rl;
2296 sprintf (interp->result, "wrong # args");
2299 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2301 rl = find_IR_record (obj, offset);
2304 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2307 if (rl->which != Z_NamePlusRecord_databaseRecord)
2309 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2312 if (rl->u.dbrec.type != VAL_SUTRS)
2314 Tcl_AppendElement (interp, rl->u.dbrec.buf);
2320 * do_getGrs: Get a GRS1 Record
2322 static int do_getGrs (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_databaseRecord)
2345 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2348 if (rl->u.dbrec.type != VAL_GRS1)
2350 return ir_tcl_get_grs (interp, rl->u.dbrec.u.grs1, argc, argv);
2355 * do_responseStatus: Return response status (present or search)
2357 static int do_responseStatus (void *o, Tcl_Interp *interp,
2358 int argc, char **argv)
2360 IrTcl_SetObj *obj = o;
2364 obj->recordFlag = 0;
2365 obj->nonSurrogateDiagnosticNum = 0;
2366 obj->nonSurrogateDiagnosticList = NULL;
2369 else if (argc == -1)
2371 ir_deleteDiags (&obj->nonSurrogateDiagnosticList,
2372 &obj->nonSurrogateDiagnosticNum);
2375 if (!obj->recordFlag)
2377 Tcl_AppendElement (interp, "OK");
2382 case Z_Records_DBOSD:
2383 Tcl_AppendElement (interp, "DBOSD");
2386 Tcl_AppendElement (interp, "NSD");
2387 return ir_diagResult (interp, obj->nonSurrogateDiagnosticList,
2388 obj->nonSurrogateDiagnosticNum);
2394 * do_present: Perform Present Request
2397 static int do_present (void *o, Tcl_Interp *interp, int argc, char **argv)
2399 IrTcl_SetObj *obj = o;
2402 Z_PresentRequest *req;
2410 if (Tcl_GetInt (interp, argv[2], &start) == TCL_ERROR)
2417 if (Tcl_GetInt (interp, argv[3], &number) == TCL_ERROR)
2425 interp->result = "present: not connected";
2430 obj->number = number;
2432 apdu = zget_APDU (p->odr_out, Z_APDU_presentRequest);
2433 req = apdu->u.presentRequest;
2435 set_referenceId (p->odr_out, &req->referenceId,
2436 obj->set_inher.referenceId);
2438 req->resultSetId = obj->setName ? obj->setName : "Default";
2440 req->resultSetStartPoint = &start;
2441 req->numberOfRecordsRequested = &number;
2442 if (obj->set_inher.preferredRecordSyntax)
2444 struct oident ident;
2446 ident.proto = p->protocol_type;
2447 ident.oclass = CLASS_RECSYN;
2448 ident.value = *obj->set_inher.preferredRecordSyntax;
2449 logf (LOG_DEBUG, "Preferred record syntax is %d", ident.value);
2450 req->preferredRecordSyntax = odr_oiddup (p->odr_out,
2451 oid_getoidbyent (&ident));
2454 req->preferredRecordSyntax = 0;
2456 if (obj->set_inher.elementSetNames && *obj->set_inher.elementSetNames)
2458 Z_ElementSetNames *esn = odr_malloc (p->odr_out, sizeof(*esn));
2459 Z_RecordComposition *compo = odr_malloc (p->odr_out, sizeof(*compo));
2461 esn->which = Z_ElementSetNames_generic;
2462 esn->u.generic = obj->set_inher.elementSetNames;
2464 req->recordComposition = compo;
2465 compo->which = Z_RecordComp_simple;
2466 compo->u.simple = esn;
2469 req->recordComposition = NULL;
2470 return ir_tcl_send_APDU (interp, p, apdu, "present", *argv);
2474 * do_loadFile: Load result set from file
2477 static int do_loadFile (void *o, Tcl_Interp *interp,
2478 int argc, char **argv)
2480 IrTcl_SetObj *setobj = o;
2490 interp->result = "wrong # args";
2493 inf = fopen (argv[2], "r");
2496 Tcl_AppendResult (interp, "Cannot open ", argv[2], NULL);
2499 while ((buf = ir_tcl_fread_marc (inf, &size)))
2501 IrTcl_RecordList *rl;
2503 rl = new_IR_record (setobj, no, Z_NamePlusRecord_databaseRecord, "F");
2504 rl->u.dbrec.type = VAL_USMARC;
2505 rl->u.dbrec.buf = buf;
2506 rl->u.dbrec.size = size;
2509 setobj->numberOfRecordsReturned = no-1;
2514 static IrTcl_Method ir_set_method_tab[] = {
2515 { 0, "search", do_search },
2516 { 0, "searchResponse", do_searchResponse },
2517 { 0, "presentResponse", do_presentResponse },
2518 { 0, "searchStatus", do_searchStatus },
2519 { 0, "presentStatus", do_presentStatus },
2520 { 0, "nextResultSetPosition", do_nextResultSetPosition },
2521 { 0, "setName", do_setName },
2522 { 0, "resultCount", do_resultCount },
2523 { 0, "numberOfRecordsReturned", do_numberOfRecordsReturned },
2524 { 0, "present", do_present },
2525 { 0, "type", do_type },
2526 { 0, "getMarc", do_getMarc },
2527 { 0, "getSutrs", do_getSutrs },
2528 { 0, "getGrs", do_getGrs },
2529 { 0, "recordType", do_recordType },
2530 { 0, "recordElements", do_recordElements },
2531 { 0, "diag", do_diag },
2532 { 0, "responseStatus", do_responseStatus },
2533 { 0, "loadFile", do_loadFile },
2538 * ir_set_obj_method: IR Set Object methods
2540 static int ir_set_obj_method (ClientData clientData, Tcl_Interp *interp,
2541 int argc, char **argv)
2543 IrTcl_Methods tabs[3];
2544 IrTcl_SetObj *p = clientData;
2548 interp->result = "wrong # args";
2551 tabs[0].tab = ir_set_method_tab;
2553 tabs[1].tab = ir_set_c_method_tab;
2554 tabs[1].obj = &p->set_inher;
2557 return ir_method (interp, argc, argv, tabs);
2561 * ir_set_obj_delete: IR Set Object disposal
2563 static void ir_set_obj_delete (ClientData clientData)
2565 IrTcl_Methods tabs[3];
2566 IrTcl_SetObj *p = clientData;
2568 logf (LOG_DEBUG, "ir set delete");
2570 tabs[0].tab = ir_set_method_tab;
2572 tabs[1].tab = ir_set_c_method_tab;
2573 tabs[1].obj = &p->set_inher;
2576 ir_method (NULL, -1, NULL, tabs);
2582 * ir_set_obj_mk: IR Set Object creation
2584 static int ir_set_obj_mk (ClientData clientData, Tcl_Interp *interp,
2585 int argc, char **argv)
2587 IrTcl_Methods tabs[3];
2590 if (argc < 2 || argc > 3)
2592 interp->result = "wrong # args";
2595 obj = ir_tcl_malloc (sizeof(*obj));
2596 logf (LOG_DEBUG, "ir set create");
2599 Tcl_CmdInfo parent_info;
2604 if (!Tcl_GetCommandInfo (interp, argv[2], &parent_info))
2606 interp->result = "No parent";
2609 obj->parent = (IrTcl_Obj *) parent_info.clientData;
2611 dst = &obj->set_inher;
2612 src = &obj->parent->set_inher;
2614 if ((dst->num_databaseNames = src->num_databaseNames))
2615 dst->databaseNames =
2616 ir_tcl_malloc (sizeof (*dst->databaseNames)
2617 * dst->num_databaseNames);
2619 dst->databaseNames = NULL;
2620 for (i = 0; i < dst->num_databaseNames; i++)
2621 if (ir_tcl_strdup (interp, &dst->databaseNames[i],
2622 src->databaseNames[i]) == TCL_ERROR)
2624 if (ir_tcl_strdup (interp, &dst->queryType, src->queryType)
2628 if (ir_tcl_strdup (interp, &dst->referenceId, src->referenceId)
2632 if (ir_tcl_strdup (interp, &dst->elementSetNames, src->elementSetNames)
2636 if (ir_tcl_strdup (interp, &dst->smallSetElementSetNames,
2637 src->smallSetElementSetNames)
2641 if (ir_tcl_strdup (interp, &dst->mediumSetElementSetNames,
2642 src->mediumSetElementSetNames)
2646 if (src->preferredRecordSyntax &&
2647 (dst->preferredRecordSyntax
2648 = ir_tcl_malloc (sizeof(*dst->preferredRecordSyntax))))
2649 *dst->preferredRecordSyntax = *src->preferredRecordSyntax;
2651 dst->preferredRecordSyntax = NULL;
2652 dst->replaceIndicator = src->replaceIndicator;
2653 dst->smallSetUpperBound = src->smallSetUpperBound;
2654 dst->largeSetLowerBound = src->largeSetLowerBound;
2655 dst->mediumSetPresentNumber = src->mediumSetPresentNumber;
2660 tabs[0].tab = ir_set_method_tab;
2664 if (ir_method (interp, 0, NULL, tabs) == TCL_ERROR)
2667 Tcl_CreateCommand (interp, argv[1], ir_set_obj_method,
2668 (ClientData) obj, ir_set_obj_delete);
2672 /* ------------------------------------------------------- */
2675 * do_scan: Perform scan
2677 static int do_scan (void *o, Tcl_Interp *interp, int argc, char **argv)
2681 IrTcl_ScanObj *obj = o;
2682 IrTcl_Obj *p = obj->parent;
2685 struct ccl_rpn_node *rpn;
2693 interp->result = "wrong # args";
2696 if (!p->set_inher.num_databaseNames)
2698 interp->result = "no databaseNames";
2703 interp->result = "scan: not connected";
2707 bib1.proto = p->protocol_type;
2708 bib1.oclass = CLASS_ATTSET;
2709 bib1.value = VAL_BIB1;
2711 apdu = zget_APDU (p->odr_out, Z_APDU_scanRequest);
2712 req = apdu->u.scanRequest;
2714 set_referenceId (p->odr_out, &req->referenceId, p->set_inher.referenceId);
2715 req->num_databaseNames = p->set_inher.num_databaseNames;
2716 req->databaseNames = p->set_inher.databaseNames;
2717 req->attributeSet = oid_getoidbyent (&bib1);
2720 if (!(req->termListAndStartPoint = p_query_scan (p->odr_out, argv[2])))
2722 Tcl_AppendResult (interp, "Syntax error in query", NULL);
2726 rpn = ccl_find_str(p->bibset, argv[2], &r, &pos);
2729 Tcl_AppendResult (interp, "CCL error: ", ccl_err_msg (r), NULL);
2732 ccl_pr_tree (rpn, stderr);
2733 fprintf (stderr, "\n");
2734 if (!(req->termListAndStartPoint = ccl_scan_query (rpn)))
2737 req->stepSize = &obj->stepSize;
2738 req->numberOfTermsRequested = &obj->numberOfTermsRequested;
2739 req->preferredPositionInResponse = &obj->preferredPositionInResponse;
2740 logf (LOG_DEBUG, "stepSize=%d", *req->stepSize);
2741 logf (LOG_DEBUG, "numberOfTermsRequested=%d",
2742 *req->numberOfTermsRequested);
2743 logf (LOG_DEBUG, "preferredPositionInResponse=%d",
2744 *req->preferredPositionInResponse);
2746 return ir_tcl_send_APDU (interp, p, apdu, "scan", *argv);
2750 * do_scanResponse: add scan response handler
2752 static int do_scanResponse (void *o, Tcl_Interp *interp,
2753 int argc, char **argv)
2755 IrTcl_ScanObj *obj = o;
2759 obj->scanResponse = NULL;
2762 else if (argc == -1)
2763 return ir_tcl_strdel (interp, &obj->scanResponse);
2766 free (obj->scanResponse);
2769 if (ir_tcl_strdup (interp, &obj->scanResponse, argv[2])
2774 obj->scanResponse = NULL;
2780 * do_stepSize: Set/get replace Step Size
2782 static int do_stepSize (void *obj, Tcl_Interp *interp,
2783 int argc, char **argv)
2785 IrTcl_ScanObj *p = obj;
2791 return get_set_int (&p->stepSize, interp, argc, argv);
2795 * do_numberOfTermsRequested: Set/get Number of Terms requested
2797 static int do_numberOfTermsRequested (void *obj, Tcl_Interp *interp,
2798 int argc, char **argv)
2800 IrTcl_ScanObj *p = obj;
2804 p->numberOfTermsRequested = 20;
2807 return get_set_int (&p->numberOfTermsRequested, interp, argc, argv);
2812 * do_preferredPositionInResponse: Set/get preferred Position
2814 static int do_preferredPositionInResponse (void *obj, Tcl_Interp *interp,
2815 int argc, char **argv)
2817 IrTcl_ScanObj *p = obj;
2821 p->preferredPositionInResponse = 1;
2824 return get_set_int (&p->preferredPositionInResponse, interp, argc, argv);
2828 * do_scanStatus: Get scan status
2830 static int do_scanStatus (void *obj, Tcl_Interp *interp,
2831 int argc, char **argv)
2833 IrTcl_ScanObj *p = obj;
2837 return get_set_int (&p->scanStatus, interp, argc, argv);
2841 * do_numberOfEntriesReturned: Get number of Entries returned
2843 static int do_numberOfEntriesReturned (void *obj, Tcl_Interp *interp,
2844 int argc, char **argv)
2846 IrTcl_ScanObj *p = obj;
2850 return get_set_int (&p->numberOfEntriesReturned, interp, argc, argv);
2854 * do_positionOfTerm: Get position of Term
2856 static int do_positionOfTerm (void *obj, Tcl_Interp *interp,
2857 int argc, char **argv)
2859 IrTcl_ScanObj *p = obj;
2863 return get_set_int (&p->positionOfTerm, interp, argc, argv);
2867 * do_scanLine: get Scan Line (surrogate or normal) after response
2869 static int do_scanLine (void *obj, Tcl_Interp *interp, int argc, char **argv)
2871 IrTcl_ScanObj *p = obj;
2877 p->entries_flag = 0;
2879 p->nonSurrogateDiagnosticNum = 0;
2880 p->nonSurrogateDiagnosticList = 0;
2883 else if (argc == -1)
2885 p->entries_flag = 0;
2886 /* release entries */
2889 ir_deleteDiags (&p->nonSurrogateDiagnosticList,
2890 &p->nonSurrogateDiagnosticNum);
2895 interp->result = "wrong # args";
2898 if (Tcl_GetInt (interp, argv[2], &i) == TCL_ERROR)
2900 if (!p->entries_flag || p->which != Z_ListEntries_entries || !p->entries
2901 || i >= p->num_entries || i < 0)
2903 switch (p->entries[i].which)
2905 case Z_Entry_termInfo:
2906 Tcl_AppendElement (interp, "T");
2907 if (p->entries[i].u.term.buf)
2908 Tcl_AppendElement (interp, p->entries[i].u.term.buf);
2910 Tcl_AppendElement (interp, "");
2911 sprintf (numstr, "%d", p->entries[i].u.term.globalOccurrences);
2912 Tcl_AppendElement (interp, numstr);
2914 case Z_Entry_surrogateDiagnostic:
2915 Tcl_AppendElement (interp, "SD");
2916 return ir_diagResult (interp, p->entries[i].u.diag.list,
2917 p->entries[i].u.diag.num);
2923 static IrTcl_Method ir_scan_method_tab[] = {
2924 { 0, "scan", do_scan },
2925 { 0, "scanResponse", do_scanResponse },
2926 { 0, "stepSize", do_stepSize },
2927 { 0, "numberOfTermsRequested", do_numberOfTermsRequested },
2928 { 0, "preferredPositionInResponse", do_preferredPositionInResponse },
2929 { 0, "scanStatus", do_scanStatus },
2930 { 0, "numberOfEntriesReturned", do_numberOfEntriesReturned },
2931 { 0, "positionOfTerm", do_positionOfTerm },
2932 { 0, "scanLine", do_scanLine },
2937 * ir_scan_obj_method: IR Scan Object methods
2939 static int ir_scan_obj_method (ClientData clientData, Tcl_Interp *interp,
2940 int argc, char **argv)
2942 IrTcl_Methods tabs[2];
2946 interp->result = "wrong # args";
2949 tabs[0].tab = ir_scan_method_tab;
2950 tabs[0].obj = clientData;
2953 return ir_method (interp, argc, argv, tabs);
2957 * ir_scan_obj_delete: IR Scan Object disposal
2959 static void ir_scan_obj_delete (ClientData clientData)
2961 IrTcl_Methods tabs[2];
2962 IrTcl_ScanObj *obj = clientData;
2964 tabs[0].tab = ir_scan_method_tab;
2968 ir_method (NULL, -1, NULL, tabs);
2973 * ir_scan_obj_mk: IR Scan Object creation
2975 static int ir_scan_obj_mk (ClientData clientData, Tcl_Interp *interp,
2976 int argc, char **argv)
2978 Tcl_CmdInfo parent_info;
2980 IrTcl_Methods tabs[2];
2984 interp->result = "wrong # args";
2987 if (!Tcl_GetCommandInfo (interp, argv[2], &parent_info))
2989 interp->result = "No parent";
2992 obj = ir_tcl_malloc (sizeof(*obj));
2993 obj->parent = (IrTcl_Obj *) parent_info.clientData;
2995 tabs[0].tab = ir_scan_method_tab;
2999 if (ir_method (interp, 0, NULL, tabs) == TCL_ERROR)
3001 Tcl_CreateCommand (interp, argv[1], ir_scan_obj_method,
3002 (ClientData) obj, ir_scan_obj_delete);
3006 /* ------------------------------------------------------- */
3008 static void ir_initResponse (void *obj, Z_InitResponse *initrs)
3012 p->initResult = *initrs->result ? 1 : 0;
3013 if (!*initrs->result)
3014 logf (LOG_DEBUG, "Connection rejected by target");
3016 logf (LOG_DEBUG, "Connection accepted by target");
3018 get_referenceId (&p->set_inher.referenceId, initrs->referenceId);
3020 free (p->targetImplementationId);
3021 ir_tcl_strdup (p->interp, &p->targetImplementationId,
3022 initrs->implementationId);
3023 free (p->targetImplementationName);
3024 ir_tcl_strdup (p->interp, &p->targetImplementationName,
3025 initrs->implementationName);
3026 free (p->targetImplementationVersion);
3027 ir_tcl_strdup (p->interp, &p->targetImplementationVersion,
3028 initrs->implementationVersion);
3030 p->maximumRecordSize = *initrs->maximumRecordSize;
3031 p->preferredMessageSize = *initrs->preferredMessageSize;
3033 memcpy (&p->options, initrs->options, sizeof(initrs->options));
3034 memcpy (&p->protocolVersion, initrs->protocolVersion,
3035 sizeof(initrs->protocolVersion));
3036 free (p->userInformationField);
3037 p->userInformationField = NULL;
3038 if (initrs->userInformationField)
3042 if (initrs->userInformationField->which == ODR_EXTERNAL_octet &&
3043 (p->userInformationField =
3044 ir_tcl_malloc ((len =
3045 initrs->userInformationField->
3046 u.octet_aligned->len) +1)))
3048 memcpy (p->userInformationField,
3049 initrs->userInformationField->u.octet_aligned->buf,
3051 (p->userInformationField)[len] = '\0';
3056 static void ir_deleteDiags (IrTcl_Diagnostic **dst_list, int *dst_num)
3059 for (i = 0; i<*dst_num; i++)
3060 free (dst_list[i]->addinfo);
3066 static void ir_handleDiags (IrTcl_Diagnostic **dst_list, int *dst_num,
3067 Z_DiagRec **list, int num)
3073 *dst_list = ir_tcl_malloc (sizeof(**dst_list) * num);
3074 for (i = 0; i<num; i++)
3076 switch (list[i]->which)
3078 case Z_DiagRec_defaultFormat:
3079 (*dst_list)[i].condition = *list[i]->u.defaultFormat->condition;
3080 addinfo = list[i]->u.defaultFormat->addinfo;
3082 ((*dst_list)[i].addinfo = ir_tcl_malloc (strlen(addinfo)+1)))
3083 strcpy ((*dst_list)[i].addinfo, addinfo);
3086 (*dst_list)[i].addinfo = NULL;
3087 (*dst_list)[i].condition = 0;
3092 static void ir_handleRecords (void *o, Z_Records *zrs, IrTcl_SetObj *setobj,
3093 const char *elements)
3098 IrTcl_RecordList *rl;
3100 setobj->which = zrs->which;
3101 setobj->recordFlag = 1;
3103 ir_deleteDiags (&setobj->nonSurrogateDiagnosticList,
3104 &setobj->nonSurrogateDiagnosticNum);
3105 if (zrs->which == Z_Records_DBOSD)
3107 setobj->numberOfRecordsReturned =
3108 zrs->u.databaseOrSurDiagnostics->num_records;
3109 logf (LOG_DEBUG, "Got %d records", setobj->numberOfRecordsReturned);
3110 for (offset = 0; offset<setobj->numberOfRecordsReturned; offset++)
3112 rl = new_IR_record (setobj, setobj->start + offset,
3113 zrs->u.databaseOrSurDiagnostics->
3114 records[offset]->which,
3116 if (rl->which == Z_NamePlusRecord_surrogateDiagnostic)
3118 ir_handleDiags (&rl->u.surrogateDiagnostics.list,
3119 &rl->u.surrogateDiagnostics.num,
3120 &zrs->u.databaseOrSurDiagnostics->
3121 records[offset]->u.surrogateDiagnostic,
3126 Z_DatabaseRecord *zr;
3128 struct oident *ident;
3130 zr = zrs->u.databaseOrSurDiagnostics->records[offset]
3132 oe = (Z_External*) zr;
3133 rl->u.dbrec.size = zr->u.octet_aligned->len;
3135 if ((ident = oid_getentbyoid (oe->direct_reference)))
3136 rl->u.dbrec.type = ident->value;
3138 rl->u.dbrec.type = VAL_USMARC;
3140 if (oe->which == ODR_EXTERNAL_octet && rl->u.dbrec.size > 0)
3142 char *buf = (char*) zr->u.octet_aligned->buf;
3143 if ((rl->u.dbrec.buf = ir_tcl_malloc (rl->u.dbrec.size)))
3144 memcpy (rl->u.dbrec.buf, buf, rl->u.dbrec.size);
3146 else if (rl->u.dbrec.type == VAL_SUTRS &&
3147 oe->which == Z_External_sutrs)
3149 odr_setbuf (p->odr_in, (char*) oe->u.single_ASN1_type->buf,
3150 oe->u.single_ASN1_type->len, 0);
3151 if ((rl->u.dbrec.buf = ir_tcl_malloc (oe->u.sutrs->len+1)))
3153 memcpy (rl->u.dbrec.buf, oe->u.sutrs->buf,
3155 rl->u.dbrec.buf[oe->u.sutrs->len] = '\0';
3157 rl->u.dbrec.size = oe->u.sutrs->len;
3159 else if (rl->u.dbrec.type == VAL_GRS1 &&
3160 oe->which == Z_External_grs1)
3162 ir_tcl_read_grs (oe->u.grs1, &rl->u.dbrec.u.grs1);
3163 rl->u.dbrec.buf = NULL;
3166 rl->u.dbrec.buf = NULL;
3170 else if (zrs->which == Z_Records_multipleNSD)
3172 logf (LOG_DEBUG, "multipleNonSurrogateDiagnostic %d",
3173 zrs->u.multipleNonSurDiagnostics->num_diagRecs);
3174 setobj->numberOfRecordsReturned = 0;
3175 ir_handleDiags (&setobj->nonSurrogateDiagnosticList,
3176 &setobj->nonSurrogateDiagnosticNum,
3177 zrs->u.multipleNonSurDiagnostics->diagRecs,
3178 zrs->u.multipleNonSurDiagnostics->num_diagRecs);
3182 logf (LOG_DEBUG, "NonSurrogateDiagnostic");
3183 setobj->numberOfRecordsReturned = 0;
3184 ir_handleDiags (&setobj->nonSurrogateDiagnosticList,
3185 &setobj->nonSurrogateDiagnosticNum,
3186 &zrs->u.nonSurrogateDiagnostic,
3191 static void ir_searchResponse (void *o, Z_SearchResponse *searchrs,
3192 IrTcl_SetObj *setobj)
3194 Z_Records *zrs = searchrs->records;
3196 logf (LOG_DEBUG, "Received search response");
3199 logf (LOG_DEBUG, "Search response, no object!");
3202 setobj->searchStatus = searchrs->searchStatus ? 1 : 0;
3203 get_referenceId (&setobj->set_inher.referenceId, searchrs->referenceId);
3204 setobj->resultCount = *searchrs->resultCount;
3205 if (searchrs->presentStatus)
3206 setobj->presentStatus = *searchrs->presentStatus;
3207 if (searchrs->nextResultSetPosition)
3208 setobj->nextResultSetPosition = *searchrs->nextResultSetPosition;
3210 logf (LOG_DEBUG, "Search response %d, %d hits",
3211 setobj->searchStatus, setobj->resultCount);
3215 if (setobj->resultCount <= setobj->set_inher.smallSetUpperBound)
3216 es = setobj->set_inher.smallSetElementSetNames;
3218 es = setobj->set_inher.mediumSetElementSetNames;
3219 ir_handleRecords (o, zrs, setobj, es);
3222 setobj->recordFlag = 0;
3226 static void ir_presentResponse (void *o, Z_PresentResponse *presrs,
3227 IrTcl_SetObj *setobj)
3229 Z_Records *zrs = presrs->records;
3231 logf (LOG_DEBUG, "Received present response");
3234 logf (LOG_DEBUG, "Present response, no object!");
3237 setobj->presentStatus = *presrs->presentStatus;
3238 get_referenceId (&setobj->set_inher.referenceId, presrs->referenceId);
3239 setobj->nextResultSetPosition = *presrs->nextResultSetPosition;
3241 ir_handleRecords (o, zrs, setobj, setobj->set_inher.elementSetNames);
3244 setobj->recordFlag = 0;
3245 logf (LOG_DEBUG, "No records!");
3249 static void ir_scanResponse (void *o, Z_ScanResponse *scanrs,
3250 IrTcl_ScanObj *scanobj)
3254 logf (LOG_DEBUG, "Received scanResponse");
3256 get_referenceId (&p->set_inher.referenceId, scanrs->referenceId);
3257 scanobj->scanStatus = *scanrs->scanStatus;
3258 logf (LOG_DEBUG, "scanStatus=%d", scanobj->scanStatus);
3260 if (scanrs->stepSize)
3261 scanobj->stepSize = *scanrs->stepSize;
3262 logf (LOG_DEBUG, "stepSize=%d", scanobj->stepSize);
3264 scanobj->numberOfEntriesReturned = *scanrs->numberOfEntriesReturned;
3265 logf (LOG_DEBUG, "numberOfEntriesReturned=%d",
3266 scanobj->numberOfEntriesReturned);
3268 if (scanrs->positionOfTerm)
3269 scanobj->positionOfTerm = *scanrs->positionOfTerm;
3271 scanobj->positionOfTerm = -1;
3272 logf (LOG_DEBUG, "positionOfTerm=%d", scanobj->positionOfTerm);
3274 free (scanobj->entries);
3275 scanobj->entries = NULL;
3277 ir_deleteDiags (&scanobj->nonSurrogateDiagnosticList,
3278 &scanobj->nonSurrogateDiagnosticNum);
3279 if (scanrs->entries)
3284 scanobj->entries_flag = 1;
3285 scanobj->which = scanrs->entries->which;
3286 switch (scanobj->which)
3288 case Z_ListEntries_entries:
3289 scanobj->num_entries = scanrs->entries->u.entries->num_entries;
3290 scanobj->entries = ir_tcl_malloc (scanobj->num_entries *
3291 sizeof(*scanobj->entries));
3292 for (i=0; i<scanobj->num_entries; i++)
3294 ze = scanrs->entries->u.entries->entries[i];
3295 scanobj->entries[i].which = ze->which;
3298 case Z_Entry_termInfo:
3299 if (ze->u.termInfo->term->which == Z_Term_general)
3301 int l = ze->u.termInfo->term->u.general->len;
3302 scanobj->entries[i].u.term.buf = ir_tcl_malloc (1+l);
3303 memcpy (scanobj->entries[i].u.term.buf,
3304 ze->u.termInfo->term->u.general->buf,
3306 scanobj->entries[i].u.term.buf[l] = '\0';
3309 scanobj->entries[i].u.term.buf = NULL;
3310 if (ze->u.termInfo->globalOccurrences)
3311 scanobj->entries[i].u.term.globalOccurrences =
3312 *ze->u.termInfo->globalOccurrences;
3314 scanobj->entries[i].u.term.globalOccurrences = 0;
3316 case Z_Entry_surrogateDiagnostic:
3317 ir_handleDiags (&scanobj->entries[i].u.diag.list,
3318 &scanobj->entries[i].u.diag.num,
3319 &ze->u.surrogateDiagnostic,
3325 case Z_ListEntries_nonSurrogateDiagnostics:
3326 ir_handleDiags (&scanobj->nonSurrogateDiagnosticList,
3327 &scanobj->nonSurrogateDiagnosticNum,
3328 scanrs->entries->u.nonSurrogateDiagnostics->
3330 scanrs->entries->u.nonSurrogateDiagnostics->
3336 scanobj->entries_flag = 0;
3340 * ir_select_read: handle incoming packages
3342 void ir_select_read (ClientData clientData)
3344 IrTcl_Obj *p = clientData;
3349 Tcl_CmdInfo cmd_info;
3350 const char *apdu_call;
3352 logf(LOG_DEBUG, "Read handler");
3353 if (p->state == IR_TCL_R_Connecting)
3355 logf(LOG_DEBUG, "Connect handler");
3356 r = cs_rcvconnect (p->cs_link);
3359 logf (LOG_WARN, "cs_rcvconnect returned 1");
3362 p->state = IR_TCL_R_Idle;
3363 ir_select_remove_write (cs_fileno (p->cs_link), p);
3366 logf (LOG_DEBUG, "cs_rcvconnect error");
3369 p->failInfo = IR_TCL_FAIL_CONNECT;
3370 IrTcl_eval (p->interp, p->failback);
3372 do_disconnect (p, NULL, 2, NULL);
3375 p->state = IR_TCL_R_Idle;
3377 IrTcl_eval (p->interp, p->callback);
3378 if (p->cs_link && p->request_queue && p->state == IR_TCL_R_Idle)
3379 ir_tcl_send_q (p, p->request_queue, "x");
3384 /* signal one more use of ir object - callbacks must not
3385 release the ir memory (p pointer) */
3386 p->state = IR_TCL_R_Reading;
3389 /* read incoming APDU */
3390 if ((r=cs_get (p->cs_link, &p->buf_in, &p->len_in)) <= 0)
3392 logf (LOG_DEBUG, "cs_get failed, code %d", r);
3393 ir_select_remove (cs_fileno (p->cs_link), p);
3394 do_disconnect (p, NULL, 2, NULL);
3397 p->failInfo = IR_TCL_FAIL_READ;
3398 IrTcl_eval (p->interp, p->failback);
3400 /* release ir object now if callback deleted it */
3406 logf(LOG_DEBUG, "PDU Fraction read");
3409 /* got complete APDU. Now decode */
3412 odr_setbuf (p->odr_in, p->buf_in, r, 0);
3413 logf (LOG_DEBUG, "cs_get ok, total size %d", r);
3414 if (!z_APDU (p->odr_in, &apdu, 0))
3416 logf (LOG_DEBUG, "cs_get failed: %s",
3417 odr_errmsg (odr_geterror (p->odr_in)));
3418 do_disconnect (p, NULL, 2, NULL);
3421 p->failInfo = IR_TCL_FAIL_IN_APDU;
3422 p->apduOffset = odr_offset (p->odr_in);
3423 IrTcl_eval (p->interp, p->failback);
3425 /* release ir object now if failback deleted it */
3429 logf(LOG_DEBUG, "Decoded ok");
3430 /* handle APDU and invoke callback */
3431 rq = p->request_queue;
3434 logf (LOG_FATAL, "Internal error. No queue entry");
3437 object_name = rq->object_name;
3438 logf (LOG_DEBUG, "getCommandInfo (%s)", object_name);
3440 if (Tcl_GetCommandInfo (p->interp, object_name, &cmd_info))
3444 case Z_APDU_initResponse:
3445 p->eventType = "init";
3446 ir_initResponse (p, apdu->u.initResponse);
3447 apdu_call = p->initResponse;
3449 case Z_APDU_searchResponse:
3450 p->eventType = "search";
3451 ir_searchResponse (p, apdu->u.searchResponse,
3452 (IrTcl_SetObj *) cmd_info.clientData);
3453 apdu_call = ((IrTcl_SetObj *)
3454 cmd_info.clientData)->searchResponse;
3456 case Z_APDU_presentResponse:
3457 p->eventType = "present";
3458 ir_presentResponse (p, apdu->u.presentResponse,
3459 (IrTcl_SetObj *) cmd_info.clientData);
3460 apdu_call = ((IrTcl_SetObj *)
3461 cmd_info.clientData)->presentResponse;
3463 case Z_APDU_scanResponse:
3464 p->eventType = "scan";
3465 ir_scanResponse (p, apdu->u.scanResponse,
3466 (IrTcl_ScanObj *) cmd_info.clientData);
3467 apdu_call = ((IrTcl_ScanObj *)
3468 cmd_info.clientData)->scanResponse;
3471 logf (LOG_WARN, "Received unknown APDU type (%d)",
3473 do_disconnect (p, NULL, 2, NULL);
3476 p->failInfo = IR_TCL_FAIL_UNKNOWN_APDU;
3477 IrTcl_eval (p->interp, p->failback);
3482 p->request_queue = rq->next;
3483 p->state = IR_TCL_R_Idle;
3486 IrTcl_eval (p->interp, apdu_call);
3487 else if (rq->callback)
3488 IrTcl_eval (p->interp, rq->callback);
3490 free (rq->callback);
3491 free (rq->object_name);
3493 odr_reset (p->odr_in);
3494 if (p->ref_count == 1)
3500 } while (p->cs_link && cs_more (p->cs_link));
3501 if (p->cs_link && p->request_queue && p->state == IR_TCL_R_Idle)
3502 ir_tcl_send_q (p, p->request_queue, "x");
3506 * ir_select_write: handle outgoing packages - not yet written.
3508 void ir_select_write (ClientData clientData)
3510 IrTcl_Obj *p = clientData;
3514 logf (LOG_DEBUG, "Write handler");
3515 if (p->state == IR_TCL_R_Connecting)
3517 logf(LOG_DEBUG, "Connect handler");
3518 r = cs_rcvconnect (p->cs_link);
3521 p->state = IR_TCL_R_Idle;
3524 logf (LOG_DEBUG, "cs_rcvconnect error");
3525 ir_select_remove_write (cs_fileno (p->cs_link), p);
3528 p->failInfo = IR_TCL_FAIL_CONNECT;
3529 IrTcl_eval (p->interp, p->failback);
3531 do_disconnect (p, NULL, 2, NULL);
3534 ir_select_remove_write (cs_fileno (p->cs_link), p);
3536 IrTcl_eval (p->interp, p->callback);
3539 rq = p->request_queue;
3540 if (!rq || !rq->buf_out)
3543 if ((r=cs_put (p->cs_link, rq->buf_out, rq->len_out)) < 0)
3545 logf (LOG_DEBUG, "cs_put write fail");
3548 p->failInfo = IR_TCL_FAIL_WRITE;
3549 IrTcl_eval (p->interp, p->failback);
3553 do_disconnect (p, NULL, 2, NULL);
3555 else if (r == 0) /* remove select bit */
3557 logf(LOG_DEBUG, "Write completed");
3558 p->state = IR_TCL_R_Waiting;
3559 ir_select_remove_write (cs_fileno (p->cs_link), p);
3565 /* ------------------------------------------------------- */
3568 * Irtcl_init: Registration of TCL commands.
3570 int Irtcl_Init (Tcl_Interp *interp)
3572 Tcl_CreateCommand (interp, "ir", ir_obj_mk, (ClientData) NULL,
3573 (Tcl_CmdDeleteProc *) NULL);
3574 Tcl_CreateCommand (interp, "ir-set", ir_set_obj_mk,
3575 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
3576 Tcl_CreateCommand (interp, "ir-scan", ir_scan_obj_mk,
3577 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);