2 * IR toolkit for tcl/tk
4 * See the file LICENSE for details.
5 * Sebastian Hammer, Adam Dickmeiss
8 * Revision 1.70 1996-01-10 09:18:34 adam
9 * PDU specific callbacks implemented: initRespnse, searchResponse,
10 * presentResponse and scanResponse.
11 * Bug fix in the command line shell (tclmain.c) - discovered on OSF/1.
13 * Revision 1.69 1996/01/04 16:12:12 adam
14 * Setting PDUType renamed to eventType.
16 * Revision 1.68 1996/01/04 11:05:22 adam
17 * New setting: PDUType - returns type of last PDU returned from the target.
18 * Fixed a bug in configure/Makefile.
20 * Revision 1.67 1996/01/03 09:00:51 adam
21 * Updated to use new version of Yaz (names changed to avoid C++ conflict).
23 * Revision 1.66 1995/11/28 17:26:39 adam
24 * Removed Carriage return from ir-tcl.c!
25 * Removed misc. debug logs.
27 * Revision 1.65 1995/11/28 13:53:00 quinn
30 * Revision 1.64 1995/11/13 15:39:18 adam
31 * Bug fix: {small,medium}SetElementSetNames weren't set correctly.
32 * Bug fix: idAuthentication weren't set correctly.
34 * Revision 1.63 1995/11/13 09:55:39 adam
35 * Multiple records at a position in a result-set with differnt
38 * Revision 1.62 1995/10/18 17:20:33 adam
39 * Work on target setup in client.tcl.
41 * Revision 1.61 1995/10/18 16:42:42 adam
42 * New settings: smallSetElementSetNames and mediumSetElementSetNames.
44 * Revision 1.60 1995/10/18 15:43:31 adam
45 * In search: mediumSetElementSetNames and smallSetElementSetNames are
46 * set to elementSetNames.
48 * Revision 1.59 1995/10/17 12:18:58 adam
49 * Bug fix: when target connection closed, the connection was not
50 * properly reestablished.
52 * Revision 1.58 1995/10/16 17:00:55 adam
53 * New setting: elementSetNames.
54 * Various client improvements. Medium presentation format looks better.
56 * Revision 1.57 1995/09/21 13:11:51 adam
57 * Support of dynamic loading.
58 * Test script uses load command if necessary.
60 * Revision 1.56 1995/08/29 15:30:14 adam
61 * Work on GRS records.
63 * Revision 1.55 1995/08/28 09:43:25 adam
64 * Minor changes. configure only searches for yaz beta 3 and versions after
67 * Revision 1.54 1995/08/24 12:25:16 adam
68 * Modified to work with yaz 1.0b3.
70 * Revision 1.53 1995/08/04 12:49:26 adam
71 * Bug fix: reading uninitialized variable p.
73 * Revision 1.52 1995/08/04 11:32:38 adam
74 * More work on output queue. Memory related routines moved
77 * Revision 1.51 1995/08/03 13:22:54 adam
80 * Revision 1.50 1995/07/20 08:09:49 adam
81 * client.tcl: Targets removed from hotTargets list when targets
82 * are removed/modified.
83 * ir-tcl.c: More work on triggerResourceControl.
85 * Revision 1.49 1995/06/30 12:39:21 adam
86 * Bug fix: loadFile didn't set record type.
87 * The MARC routines are a little less strict in the interpretation.
88 * Script display.tcl replaces the old marc.tcl.
89 * New interactive script: shell.tcl.
91 * Revision 1.48 1995/06/27 19:03:50 adam
92 * Bug fix in do_present in ir-tcl.c: p->set_child member weren't set.
93 * nextResultSetPosition used instead of setOffset.
95 * Revision 1.47 1995/06/25 10:25:04 adam
96 * Working on triggerResourceControl. Description of compile/install
97 * procedure moved to ir-tcl.sgml.
99 * Revision 1.46 1995/06/22 13:15:06 adam
100 * Feature: SUTRS. Setting getSutrs implemented.
101 * Work on display formats.
102 * Preferred record syntax can be set by the user.
104 * Revision 1.45 1995/06/20 08:07:30 adam
105 * New setting: failInfo.
106 * Working on better cancel mechanism.
108 * Revision 1.44 1995/06/19 17:01:20 adam
111 * Revision 1.43 1995/06/19 13:06:08 adam
112 * New define: IR_TCL_VERSION.
114 * Revision 1.42 1995/06/19 08:08:52 adam
115 * client.tcl: hotTargets now contain both database and target name.
116 * ir-tcl.c: setting protocol edited. Errors in callbacks are logged
117 * by logf(LOG_WARN, ...) calls.
119 * Revision 1.41 1995/06/16 12:28:16 adam
120 * Implemented preferredRecordSyntax.
121 * Minor changes in diagnostic handling.
122 * Record list deleted when connection closes.
124 * Revision 1.40 1995/06/14 13:37:18 adam
125 * Setting recordType implemented.
126 * Setting implementationVersion implemented.
127 * Settings implementationId / implementationName edited.
129 * Revision 1.39 1995/06/08 10:26:32 adam
130 * Bug fix in ir_strdup.
132 * Revision 1.38 1995/06/01 16:36:47 adam
133 * About buttons. Minor bug fixes.
135 * Revision 1.37 1995/06/01 07:31:20 adam
136 * Rename of many typedefs -> IrTcl_...
138 * Revision 1.36 1995/05/31 13:09:59 adam
139 * Client searches/presents may be interrupted.
140 * New moving book-logo.
142 * Revision 1.35 1995/05/31 08:36:33 adam
143 * Bug fix in client.tcl: didn't save options on clientrc.tcl.
144 * New method: referenceId. More work on scan.
146 * Revision 1.34 1995/05/29 10:33:42 adam
147 * README and rename of startup script.
149 * Revision 1.33 1995/05/29 09:15:11 quinn
150 * Changed CS_SR to PROTO_SR, etc.
152 * Revision 1.32 1995/05/29 08:44:16 adam
153 * Work on delete of objects.
155 * Revision 1.31 1995/05/26 11:44:10 adam
156 * Bugs fixed. More work on MARC utilities and queries. Test
157 * client is up-to-date again.
159 * Revision 1.30 1995/05/26 08:54:11 adam
160 * New MARC utilities. Uses prefix query.
162 * Revision 1.29 1995/05/24 14:10:22 adam
163 * Work on idAuthentication, protocolVersion and options.
165 * Revision 1.28 1995/05/23 15:34:48 adam
166 * Many new settings, userInformationField, smallSetUpperBound, etc.
167 * A number of settings are inherited when ir-set is executed.
168 * This version is incompatible with the graphical test client (client.tcl).
170 * Revision 1.27 1995/05/11 15:34:47 adam
171 * Scan request changed a bit. This version works with RLG.
173 * Revision 1.26 1995/04/18 16:11:51 adam
174 * First version of graphical Scan. Some work on query-by-form.
176 * Revision 1.25 1995/04/17 09:37:17 adam
177 * Further development of scan.
179 * Revision 1.24 1995/04/11 14:16:42 adam
180 * Further work on scan. Response works. Entries aren't saved yet.
182 * Revision 1.23 1995/04/10 10:50:27 adam
183 * Result-set name defaults to suffix of ir-set name.
184 * Started working on scan. Not finished at this point.
186 * Revision 1.22 1995/03/31 10:43:03 adam
187 * More robust when getting bad MARC records.
189 * Revision 1.21 1995/03/31 08:56:37 adam
190 * New button "Search".
192 * Revision 1.20 1995/03/29 16:07:09 adam
193 * Bug fix: Didn't use setName in present request.
195 * Revision 1.19 1995/03/28 12:45:23 adam
196 * New ir method failback: called on disconnect/protocol error.
197 * New ir set/get method: protocol: SR / Z3950.
198 * Simple popup and disconnect when failback is invoked.
200 * Revision 1.18 1995/03/21 15:50:12 adam
203 * Revision 1.17 1995/03/21 13:41:03 adam
204 * Comstack cs_create not used too often. Non-blocking connect.
206 * Revision 1.16 1995/03/21 08:26:06 adam
207 * New method, setName, to specify the result set name (other than Default).
208 * New method, responseStatus, which returns diagnostic info, if any, after
209 * present response / search response.
211 * Revision 1.15 1995/03/20 15:24:07 adam
212 * Diagnostic records saved on searchResponse.
214 * Revision 1.14 1995/03/20 08:53:22 adam
215 * Event loop in tclmain.c rewritten. New method searchStatus.
217 * Revision 1.13 1995/03/17 18:26:17 adam
218 * Non-blocking i/o used now. Database names popup as cascade items.
220 * Revision 1.12 1995/03/17 15:45:00 adam
221 * Improved target/database setup.
223 * Revision 1.11 1995/03/16 17:54:03 adam
224 * Minor changes really.
226 * Revision 1.10 1995/03/15 16:14:50 adam
227 * Blocking arg in cs_create changed.
229 * Revision 1.9 1995/03/15 13:59:24 adam
232 * Revision 1.8 1995/03/15 08:25:16 adam
233 * New method presentStatus to check for error on present. Misc. cleanup
234 * of IrTcl_RecordList manipulations. Full MARC record presentation in
237 * Revision 1.7 1995/03/14 17:32:29 adam
238 * Presentation of full Marc record in popup window.
240 * Revision 1.6 1995/03/12 19:31:55 adam
241 * Pattern matching implemented when retrieving MARC records. More
242 * diagnostic functions.
244 * Revision 1.5 1995/03/10 18:00:15 adam
245 * Actual presentation in line-by-line format. RPN query support.
247 * Revision 1.4 1995/03/09 16:15:08 adam
248 * First presentRequest attempts. Hot-target list.
257 #include <sys/time.h>
268 int (*method) (void *obj, Tcl_Interp *interp, int argc, char **argv);
276 static void ir_deleteDiags (IrTcl_Diagnostic **dst_list, int *dst_num);
277 static int do_disconnect (void *obj, Tcl_Interp *interp,
278 int argc, char **argv);
280 static IrTcl_RecordList *new_IR_record (IrTcl_SetObj *setobj,
282 const char *elements)
284 IrTcl_RecordList *rl;
286 for (rl = setobj->record_list; rl; rl = rl->next)
288 if (no == rl->no && (!rl->elements || !elements ||
289 !strcmp(elements, rl->elements)))
294 case Z_NamePlusRecord_databaseRecord:
295 free (rl->u.dbrec.buf);
296 rl->u.dbrec.buf = NULL;
298 case Z_NamePlusRecord_surrogateDiagnostic:
299 ir_deleteDiags (&rl->u.surrogateDiagnostics.list,
300 &rl->u.surrogateDiagnostics.num);
308 rl = ir_tcl_malloc (sizeof(*rl));
309 rl->next = setobj->record_list;
311 setobj->record_list = rl;
314 ir_tcl_strdup (NULL, &rl->elements, elements);
319 enum oid_value value;
321 } IrTcl_recordSyntaxTab[] = {
322 { VAL_UNIMARC, "UNIMARC" },
323 { VAL_INTERMARC, "INTERMARC" },
325 { VAL_USMARC, "USMARC" },
326 { VAL_UKMARC, "UKMARC" },
327 { VAL_NORMARC, "NORMARC" },
328 { VAL_LIBRISMARC, "LIBRISMARC" },
329 { VAL_DANMARC, "DANMARC" },
330 { VAL_FINMARC, "FINMARC" },
332 { VAL_CANMARC, "CANMARC" },
334 { VAL_PICAMARC, "PICAMARC" },
335 { VAL_AUSMARC, "AUSMARC" },
336 { VAL_IBERMARC, "IBERMARC" },
337 { VAL_SUTRS, "SUTRS" },
338 { VAL_GRS1, "GRS1" },
345 int IrTcl_eval (Tcl_Interp *interp, const char *command)
347 char *tmp = ir_tcl_malloc (strlen(command)+1);
350 strcpy (tmp, command);
351 r = Tcl_Eval (interp, tmp);
353 logf (LOG_WARN, "Tcl error in line %d: %s", interp->errorLine,
355 Tcl_FreeResult (interp);
361 * IrTcl_getRecordSyntaxStr: Return record syntax name of object id
363 static const char *IrTcl_getRecordSyntaxStr (enum oid_value value)
366 for (i = 0; IrTcl_recordSyntaxTab[i].name; i++)
367 if (IrTcl_recordSyntaxTab[i].value == value)
368 return IrTcl_recordSyntaxTab[i].name;
373 * IrTcl_getRecordSyntaxVal: Return record syntax value of string
375 static enum oid_value IrTcl_getRecordSyntaxVal (const char *name)
378 for (i = 0; IrTcl_recordSyntaxTab[i].name; i++)
379 if (!strcmp (IrTcl_recordSyntaxTab[i].name, name))
380 return IrTcl_recordSyntaxTab[i].value;
384 static IrTcl_RecordList *find_IR_record (IrTcl_SetObj *setobj, int no)
386 IrTcl_RecordList *rl;
388 for (rl = setobj->record_list; rl; rl = rl->next)
390 (!setobj->recordElements || !rl->elements ||
391 !strcmp (setobj->recordElements, rl->elements)))
396 static void delete_IR_records (IrTcl_SetObj *setobj)
398 IrTcl_RecordList *rl, *rl1;
400 for (rl = setobj->record_list; rl; rl = rl1)
404 case Z_NamePlusRecord_databaseRecord:
405 free (rl->u.dbrec.buf);
407 case Z_NamePlusRecord_surrogateDiagnostic:
408 ir_deleteDiags (&rl->u.surrogateDiagnostics.list,
409 &rl->u.surrogateDiagnostics.num);
415 setobj->record_list = NULL;
419 * get_set_int: Set/get integer value
421 static int get_set_int (int *val, Tcl_Interp *interp, int argc, char **argv)
427 if (Tcl_GetInt (interp, argv[2], val)==TCL_ERROR)
430 sprintf (buf, "%d", *val);
431 Tcl_AppendResult (interp, buf, NULL);
436 * ir_method: Search for method in table and invoke method handler
438 int ir_method (Tcl_Interp *interp, int argc, char **argv, IrTcl_Methods *tab)
440 IrTcl_Methods *tab_i = tab;
443 for (tab_i = tab; tab_i->tab; tab_i++)
444 for (t = tab_i->tab; t->name; t++)
447 if ((*t->method)(tab_i->obj, interp, argc, argv) == TCL_ERROR)
451 if (!strcmp (t->name, argv[1]))
452 return (*t->method)(tab_i->obj, interp, argc, argv);
456 Tcl_AppendResult (interp, "Bad method: ", argv[1],
457 ". Possible methods:", NULL);
458 for (tab_i = tab; tab_i->tab; tab_i++)
459 for (t = tab_i->tab; t->name; t++)
460 Tcl_AppendResult (interp, " ", t->name, NULL);
465 * ir_method_r: Get status for all readable elements
467 int ir_method_r (void *obj, Tcl_Interp *interp, int argc, char **argv,
475 for (; tab->name; tab++)
478 argv_n[1] = tab->name;
479 Tcl_AppendResult (interp, "{", NULL);
480 (*tab->method)(obj, interp, argc_n, argv_n);
481 Tcl_AppendResult (interp, "} ", NULL);
487 * ir_named_bits: get/set named bits
489 int ir_named_bits (struct ir_named_entry *tab, Odr_bitmask *ob,
490 Tcl_Interp *interp, int argc, char **argv)
492 struct ir_named_entry *ti;
497 for (no = 0; no < argc; no++)
499 for (ti = tab; ti->name; ti++)
500 if (!strcmp (argv[no], ti->name))
502 ODR_MASK_SET (ob, ti->pos);
507 Tcl_AppendResult (interp, "Bad bit mask: ", argv[no], NULL);
513 for (ti = tab; ti->name; ti++)
514 if (ODR_MASK_GET (ob, ti->pos))
515 Tcl_AppendElement (interp, ti->name);
519 static void set_referenceId (ODR o, Z_ReferenceId **dst, const char *src)
525 *dst = odr_malloc (o, sizeof(**dst));
526 (*dst)->size = (*dst)->len = strlen(src);
527 (*dst)->buf = odr_malloc (o, (*dst)->len);
528 memcpy ((*dst)->buf, src, (*dst)->len);
532 static void get_referenceId (char **dst, Z_ReferenceId *src)
540 *dst = ir_tcl_malloc (src->len+1);
541 memcpy (*dst, src->buf, src->len);
542 (*dst)[src->len] = '\0';
545 /* ------------------------------------------------------- */
548 * do_init_request: init method on IR object
550 static int do_init_request (void *obj, Tcl_Interp *interp,
551 int argc, char **argv)
561 interp->result = "init: not connected";
564 apdu = zget_APDU (p->odr_out, Z_APDU_initRequest);
565 req = apdu->u.initRequest;
567 set_referenceId (p->odr_out, &req->referenceId, p->set_inher.referenceId);
568 req->options = &p->options;
569 req->protocolVersion = &p->protocolVersion;
570 req->preferredMessageSize = &p->preferredMessageSize;
571 req->maximumRecordSize = &p->maximumRecordSize;
573 if (p->idAuthenticationGroupId)
575 Z_IdPass *pass = odr_malloc (p->odr_out, sizeof(*pass));
576 Z_IdAuthentication *auth = odr_malloc (p->odr_out, sizeof(*auth));
578 logf (LOG_DEBUG, "using pass authentication");
580 auth->which = Z_IdAuthentication_idPass;
581 auth->u.idPass = pass;
582 if (p->idAuthenticationGroupId && *p->idAuthenticationGroupId)
583 pass->groupId = p->idAuthenticationGroupId;
585 pass->groupId = NULL;
586 if (p->idAuthenticationUserId && *p->idAuthenticationUserId)
587 pass->userId = p->idAuthenticationUserId;
590 if (p->idAuthenticationPassword && *p->idAuthenticationPassword)
591 pass->password = p->idAuthenticationPassword;
593 pass->password = NULL;
594 req->idAuthentication = auth;
596 else if (!p->idAuthenticationOpen || !*p->idAuthenticationOpen)
597 req->idAuthentication = NULL;
600 Z_IdAuthentication *auth = odr_malloc (p->odr_out, sizeof(*auth));
602 logf (LOG_DEBUG, "using open authentication");
603 auth->which = Z_IdAuthentication_open;
604 auth->u.open = p->idAuthenticationOpen;
605 req->idAuthentication = auth;
607 req->implementationId = p->implementationId;
608 req->implementationName = p->implementationName;
609 req->implementationVersion = p->implementationVersion;
610 req->userInformationField = 0;
612 return ir_tcl_send_APDU (interp, p, apdu, "init", *argv);
616 * do_protocolVersion: Set protocol Version
618 static int do_protocolVersion (void *obj, Tcl_Interp *interp,
619 int argc, char **argv)
627 ODR_MASK_ZERO (&p->protocolVersion);
628 ODR_MASK_SET (&p->protocolVersion, 0);
629 ODR_MASK_SET (&p->protocolVersion, 1);
634 if (Tcl_GetInt (interp, argv[2], &version)==TCL_ERROR)
636 ODR_MASK_ZERO (&p->protocolVersion);
637 for (i = 0; i<version; i++)
638 ODR_MASK_SET (&p->protocolVersion, i);
640 for (i = 4; --i >= 0; )
641 if (ODR_MASK_GET (&p->protocolVersion, i))
643 sprintf (buf, "%d", i+1);
644 interp->result = buf;
649 * do_options: Set options
651 static int do_options (void *obj, Tcl_Interp *interp,
652 int argc, char **argv)
654 static struct ir_named_entry options_tab[] = {
658 { "resourceReport", 3 },
659 { "triggerResourceCtrl", 4},
660 { "resourceCtrl", 5},
664 { "extendedServices", 10},
665 { "level-1Segmentation", 11},
666 { "level-2Segmentation", 12},
667 { "concurrentOperations", 13},
668 { "namedResultSets", 14},
675 ODR_MASK_ZERO (&p->options);
676 ODR_MASK_SET (&p->options, 0);
677 ODR_MASK_SET (&p->options, 1);
678 ODR_MASK_SET (&p->options, 4);
679 ODR_MASK_SET (&p->options, 7);
680 ODR_MASK_SET (&p->options, 14);
683 return ir_named_bits (options_tab, &p->options, interp, argc-2, argv+2);
687 * do_failInfo: Get fail information
689 static int do_failInfo (void *obj, Tcl_Interp *interp, int argc, char **argv)
699 sprintf (buf, "%d", p->failInfo);
705 case IR_TCL_FAIL_CONNECT:
706 cp = "connect failed";
708 case IR_TCL_FAIL_READ:
709 cp = "connection closed";
711 case IR_TCL_FAIL_WRITE:
712 cp = "connection closed";
714 case IR_TCL_FAIL_IN_APDU:
715 cp = "failed to decode incoming APDU";
717 case IR_TCL_FAIL_UNKNOWN_APDU:
723 Tcl_AppendElement (interp, buf);
724 Tcl_AppendElement (interp, cp);
729 * do_preferredMessageSize: Set/get preferred message size
731 static int do_preferredMessageSize (void *obj, Tcl_Interp *interp,
732 int argc, char **argv)
738 p->preferredMessageSize = 30000;
741 return get_set_int (&p->preferredMessageSize, interp, argc, argv);
745 * do_maximumRecordSize: Set/get maximum record size
747 static int do_maximumRecordSize (void *obj, Tcl_Interp *interp,
748 int argc, char **argv)
754 p->maximumRecordSize = 30000;
757 return get_set_int (&p->maximumRecordSize, interp, argc, argv);
761 * do_initResult: Get init result
763 static int do_initResult (void *obj, Tcl_Interp *interp,
764 int argc, char **argv)
770 return get_set_int (&p->initResult, interp, argc, argv);
775 * do_implementationName: Set/get Implementation Name.
777 static int do_implementationName (void *obj, Tcl_Interp *interp,
778 int argc, char **argv)
783 return ir_tcl_strdup (interp, &p->implementationName,
784 "Index Data/IrTcl on YAZ");
786 return ir_tcl_strdel (interp, &p->implementationName);
789 free (p->implementationName);
790 if (ir_tcl_strdup (interp, &p->implementationName, argv[2])
794 Tcl_AppendResult (interp, p->implementationName, (char*) NULL);
799 * do_implementationId: Get Implementation Id.
801 static int do_implementationId (void *obj, Tcl_Interp *interp,
802 int argc, char **argv)
807 return ir_tcl_strdup (interp, &p->implementationId, "YAZ (id=81)");
809 return ir_tcl_strdel (interp, &p->implementationId);
810 Tcl_AppendResult (interp, p->implementationId, (char*) NULL);
815 * do_implementationVersion: get Implementation Version.
817 static int do_implementationVersion (void *obj, Tcl_Interp *interp,
818 int argc, char **argv)
823 return ir_tcl_strdup (interp, &p->implementationVersion,
824 "YAZ: " YAZ_VERSION " / IrTcl: " IR_TCL_VERSION);
826 return ir_tcl_strdel (interp, &p->implementationVersion);
827 Tcl_AppendResult (interp, p->implementationVersion, (char*) NULL);
832 * do_targetImplementationName: Get Implementation Name of target.
834 static int do_targetImplementationName (void *obj, Tcl_Interp *interp,
835 int argc, char **argv)
841 p->targetImplementationName = NULL;
845 return ir_tcl_strdel (interp, &p->targetImplementationName);
846 Tcl_AppendResult (interp, p->targetImplementationName, (char*) NULL);
851 * do_targetImplementationId: Get Implementation Id of target
853 static int do_targetImplementationId (void *obj, Tcl_Interp *interp,
854 int argc, char **argv)
860 p->targetImplementationId = NULL;
864 return ir_tcl_strdel (interp, &p->targetImplementationId);
865 Tcl_AppendResult (interp, p->targetImplementationId, (char*) NULL);
870 * do_targetImplementationVersion: Get Implementation Version of target
872 static int do_targetImplementationVersion (void *obj, Tcl_Interp *interp,
873 int argc, char **argv)
879 p->targetImplementationVersion = NULL;
883 return ir_tcl_strdel (interp, &p->targetImplementationVersion);
884 Tcl_AppendResult (interp, p->targetImplementationVersion, (char*) NULL);
889 * do_idAuthentication: Set/get id Authentication
891 static int do_idAuthentication (void *obj, Tcl_Interp *interp,
892 int argc, char **argv)
896 if (argc >= 3 || argc == -1)
898 free (p->idAuthenticationOpen);
899 free (p->idAuthenticationGroupId);
900 free (p->idAuthenticationUserId);
901 free (p->idAuthenticationPassword);
903 if (argc >= 3 || argc <= 0)
905 p->idAuthenticationOpen = NULL;
906 p->idAuthenticationGroupId = NULL;
907 p->idAuthenticationUserId = NULL;
908 p->idAuthenticationPassword = NULL;
917 ir_tcl_strdup (interp, &p->idAuthenticationOpen, argv[2])
924 ir_tcl_strdup (interp, &p->idAuthenticationGroupId, argv[2])
928 ir_tcl_strdup (interp, &p->idAuthenticationUserId, argv[3])
932 ir_tcl_strdup (interp, &p->idAuthenticationPassword, argv[4])
937 if (p->idAuthenticationOpen)
938 Tcl_AppendElement (interp, p->idAuthenticationOpen);
939 else if (p->idAuthenticationGroupId)
941 Tcl_AppendElement (interp, p->idAuthenticationGroupId);
942 Tcl_AppendElement (interp, p->idAuthenticationUserId);
943 Tcl_AppendElement (interp, p->idAuthenticationPassword);
949 * do_connect: connect method on IR object
951 static int do_connect (void *obj, Tcl_Interp *interp,
952 int argc, char **argv)
964 interp->result = "already connected";
967 if (ir_tcl_strdup (interp, &p->hostname, argv[2]) == TCL_ERROR)
969 if (!strcmp (p->cs_type, "tcpip"))
971 p->cs_link = cs_create (tcpip_type, CS_BLOCK, p->protocol_type);
972 addr = tcpip_strtoaddr (argv[2]);
975 interp->result = "tcpip_strtoaddr fail";
978 logf (LOG_DEBUG, "tcp/ip connect %s", argv[2]);
980 else if (!strcmp (p->cs_type, "mosi"))
983 p->cs_link = cs_create (mosi_type, CS_BLOCK, p->protocol_type);
984 addr = mosi_strtoaddr (argv[2]);
987 interp->result = "mosi_strtoaddr fail";
990 logf (LOG_DEBUG, "mosi connect %s", argv[2]);
992 interp->result = "MOSI support not there";
998 Tcl_AppendResult (interp, "Bad comstack type: ",
1002 if ((r=cs_connect (p->cs_link, addr)) < 0)
1004 interp->result = "connect fail";
1005 do_disconnect (p, NULL, 2, NULL);
1008 p->eventType = "connect";
1009 ir_select_add (cs_fileno (p->cs_link), p);
1012 ir_select_add_write (cs_fileno (p->cs_link), p);
1013 p->state = IR_TCL_R_Connecting;
1017 p->state = IR_TCL_R_Idle;
1019 IrTcl_eval (p->interp, p->callback);
1023 Tcl_AppendResult (interp, p->hostname, NULL);
1028 * do_disconnect: disconnect method on IR object
1030 static int do_disconnect (void *obj, Tcl_Interp *interp,
1031 int argc, char **argv)
1037 p->state = IR_TCL_R_Idle;
1038 p->eventType = NULL;
1047 ir_select_remove_write (cs_fileno (p->cs_link), p);
1048 ir_select_remove (cs_fileno (p->cs_link), p);
1050 odr_reset (p->odr_in);
1052 assert (p->cs_link);
1053 cs_close (p->cs_link);
1056 ODR_MASK_ZERO (&p->options);
1057 ODR_MASK_SET (&p->options, 0);
1058 ODR_MASK_SET (&p->options, 1);
1059 ODR_MASK_SET (&p->options, 4);
1060 ODR_MASK_SET (&p->options, 7);
1061 ODR_MASK_SET (&p->options, 14);
1063 ODR_MASK_ZERO (&p->protocolVersion);
1064 ODR_MASK_SET (&p->protocolVersion, 0);
1065 ODR_MASK_SET (&p->protocolVersion, 1);
1068 assert (!p->cs_link);
1073 * do_comstack: Set/get comstack method on IR object
1075 static int do_comstack (void *o, Tcl_Interp *interp,
1076 int argc, char **argv)
1081 return ir_tcl_strdup (interp, &obj->cs_type, "tcpip");
1082 else if (argc == -1)
1083 return ir_tcl_strdel (interp, &obj->cs_type);
1086 free (obj->cs_type);
1087 if (ir_tcl_strdup (interp, &obj->cs_type, argv[2]) == TCL_ERROR)
1090 Tcl_AppendElement (interp, obj->cs_type);
1095 * do_logLevel: Set log level
1097 static int do_logLevel (void *o, Tcl_Interp *interp,
1098 int argc, char **argv)
1103 log_init (log_mask_str (argv[2]), "", NULL);
1105 log_init (log_mask_str (argv[2]), argv[3], NULL);
1107 log_init (log_mask_str (argv[2]), argv[3], argv[4]);
1113 * do_eventType: Return type of last event
1115 static int do_eventType (void *obj, Tcl_Interp *interp,
1116 int argc, char **argv)
1122 p->eventType = NULL;
1125 Tcl_AppendElement (interp, p->eventType ? p->eventType : "");
1131 * do_callback: add callback
1133 static int do_callback (void *obj, Tcl_Interp *interp,
1134 int argc, char **argv)
1143 else if (argc == -1)
1144 return ir_tcl_strdel (interp, &p->callback);
1150 if (ir_tcl_strdup (interp, &p->callback, argv[2]) == TCL_ERROR)
1160 * do_failback: add error handle callback
1162 static int do_failback (void *obj, Tcl_Interp *interp,
1163 int argc, char **argv)
1172 else if (argc == -1)
1173 return ir_tcl_strdel (interp, &p->failback);
1179 if (ir_tcl_strdup (interp, &p->failback, argv[2]) == TCL_ERROR)
1189 * do_initResponse: add init response handler
1191 static int do_initResponse (void *obj, Tcl_Interp *interp,
1192 int argc, char **argv)
1198 p->initResponse = NULL;
1201 else if (argc == -1)
1202 return ir_tcl_strdel (interp, &p->initResponse);
1205 free (p->initResponse);
1208 if (ir_tcl_strdup (interp, &p->initResponse, argv[2]) == TCL_ERROR)
1212 p->initResponse = NULL;
1217 * do_protocol: Set/get protocol method on IR object
1219 static int do_protocol (void *o, Tcl_Interp *interp, int argc, char **argv)
1225 p->protocol_type = PROTO_Z3950;
1230 if (!strcmp (argv[2], "Z39"))
1231 p->protocol_type = PROTO_Z3950;
1232 else if (!strcmp (argv[2], "SR"))
1233 p->protocol_type = PROTO_SR;
1236 Tcl_AppendResult (interp, "Bad protocol: ", argv[2], NULL);
1241 switch (p->protocol_type)
1244 Tcl_AppendElement (interp, "Z39");
1247 Tcl_AppendElement (interp, "SR");
1254 * do_triggerResourceControl:
1256 static int do_triggerResourceControl (void *obj, Tcl_Interp *interp,
1257 int argc, char **argv)
1261 Z_TriggerResourceControlRequest *req;
1262 bool_t is_false = 0;
1268 interp->result = "triggerResourceControl: not connected";
1271 apdu = zget_APDU (p->odr_out, Z_APDU_triggerResourceControlRequest);
1272 req = apdu->u.triggerResourceControlRequest;
1273 *req->requestedAction = Z_TriggerResourceCtrl_cancel;
1274 req->resultSetWanted = &is_false;
1276 return ir_tcl_send_APDU (interp, p, apdu, "triggerResourceControl",
1281 * do_databaseNames: specify database names
1283 static int do_databaseNames (void *obj, Tcl_Interp *interp,
1284 int argc, char **argv)
1287 IrTcl_SetCObj *p = obj;
1291 for (i=0; i<p->num_databaseNames; i++)
1292 free (p->databaseNames[i]);
1293 free (p->databaseNames);
1297 p->num_databaseNames = 0;
1298 p->databaseNames = NULL;
1303 for (i=0; i<p->num_databaseNames; i++)
1304 Tcl_AppendElement (interp, p->databaseNames[i]);
1307 if (p->databaseNames)
1309 for (i=0; i<p->num_databaseNames; i++)
1310 free (p->databaseNames[i]);
1311 free (p->databaseNames);
1313 p->num_databaseNames = argc - 2;
1315 ir_tcl_malloc (sizeof(*p->databaseNames) * p->num_databaseNames);
1316 for (i=0; i<p->num_databaseNames; i++)
1318 if (ir_tcl_strdup (interp, &p->databaseNames[i], argv[2+i])
1326 * do_replaceIndicator: Set/get replace Set indicator
1328 static int do_replaceIndicator (void *obj, Tcl_Interp *interp,
1329 int argc, char **argv)
1331 IrTcl_SetCObj *p = obj;
1335 p->replaceIndicator = 1;
1338 return get_set_int (&p->replaceIndicator, interp, argc, argv);
1342 * do_queryType: Set/Get query method
1344 static int do_queryType (void *obj, Tcl_Interp *interp,
1345 int argc, char **argv)
1347 IrTcl_SetCObj *p = obj;
1350 return ir_tcl_strdup (interp, &p->queryType, "rpn");
1351 else if (argc == -1)
1352 return ir_tcl_strdel (interp, &p->queryType);
1355 free (p->queryType);
1356 if (ir_tcl_strdup (interp, &p->queryType, argv[2]) == TCL_ERROR)
1359 Tcl_AppendResult (interp, p->queryType, NULL);
1364 * do_userInformationField: Get User information field
1366 static int do_userInformationField (void *obj, Tcl_Interp *interp,
1367 int argc, char **argv)
1373 p->userInformationField = NULL;
1376 else if (argc == -1)
1377 return ir_tcl_strdel (interp, &p->userInformationField);
1378 Tcl_AppendResult (interp, p->userInformationField, NULL);
1383 * do_smallSetUpperBound: Set/get small set upper bound
1385 static int do_smallSetUpperBound (void *o, Tcl_Interp *interp,
1386 int argc, char **argv)
1388 IrTcl_SetCObj *p = o;
1392 p->smallSetUpperBound = 0;
1395 return get_set_int (&p->smallSetUpperBound, interp, argc, argv);
1399 * do_largeSetLowerBound: Set/get large set lower bound
1401 static int do_largeSetLowerBound (void *o, Tcl_Interp *interp,
1402 int argc, char **argv)
1404 IrTcl_SetCObj *p = o;
1408 p->largeSetLowerBound = 2;
1411 return get_set_int (&p->largeSetLowerBound, interp, argc, argv);
1415 * do_mediumSetPresentNumber: Set/get large set lower bound
1417 static int do_mediumSetPresentNumber (void *o, Tcl_Interp *interp,
1418 int argc, char **argv)
1420 IrTcl_SetCObj *p = o;
1424 p->mediumSetPresentNumber = 0;
1427 return get_set_int (&p->mediumSetPresentNumber, interp, argc, argv);
1431 * do_referenceId: Set/Get referenceId
1433 static int do_referenceId (void *obj, Tcl_Interp *interp,
1434 int argc, char **argv)
1436 IrTcl_SetCObj *p = obj;
1440 p->referenceId = NULL;
1443 else if (argc == -1)
1444 return ir_tcl_strdel (interp, &p->referenceId);
1447 free (p->referenceId);
1448 if (ir_tcl_strdup (interp, &p->referenceId, argv[2]) == TCL_ERROR)
1451 Tcl_AppendResult (interp, p->referenceId, NULL);
1456 * do_preferredRecordSyntax: Set/get preferred record syntax
1458 static int do_preferredRecordSyntax (void *obj, Tcl_Interp *interp,
1459 int argc, char **argv)
1461 IrTcl_SetCObj *p = obj;
1465 p->preferredRecordSyntax = NULL;
1468 else if (argc == -1)
1470 free (p->preferredRecordSyntax);
1471 p->preferredRecordSyntax = NULL;
1476 free (p->preferredRecordSyntax);
1477 p->preferredRecordSyntax = NULL;
1478 if (argv[2][0] && (p->preferredRecordSyntax =
1479 ir_tcl_malloc (sizeof(*p->preferredRecordSyntax))))
1480 *p->preferredRecordSyntax = IrTcl_getRecordSyntaxVal (argv[2]);
1487 * do_elementSetNames: Set/Get element Set Names
1489 static int do_elementSetNames (void *obj, Tcl_Interp *interp,
1490 int argc, char **argv)
1492 IrTcl_SetCObj *p = obj;
1496 p->elementSetNames = NULL;
1499 else if (argc == -1)
1500 return ir_tcl_strdel (interp, &p->elementSetNames);
1503 free (p->elementSetNames);
1504 if (ir_tcl_strdup (interp, &p->elementSetNames, argv[2]) == TCL_ERROR)
1507 Tcl_AppendResult (interp, p->elementSetNames, NULL);
1512 * do_smallSetElementSetNames: Set/Get small Set Element Set Names
1514 static int do_smallSetElementSetNames (void *obj, Tcl_Interp *interp,
1515 int argc, char **argv)
1517 IrTcl_SetCObj *p = obj;
1521 p->smallSetElementSetNames = NULL;
1524 else if (argc == -1)
1525 return ir_tcl_strdel (interp, &p->smallSetElementSetNames);
1528 free (p->smallSetElementSetNames);
1529 if (ir_tcl_strdup (interp, &p->smallSetElementSetNames,
1530 argv[2]) == TCL_ERROR)
1533 Tcl_AppendResult (interp, p->smallSetElementSetNames, NULL);
1538 * do_mediumSetElementSetNames: Set/Get medium Set Element Set Names
1540 static int do_mediumSetElementSetNames (void *obj, Tcl_Interp *interp,
1541 int argc, char **argv)
1543 IrTcl_SetCObj *p = obj;
1547 p->mediumSetElementSetNames = NULL;
1550 else if (argc == -1)
1551 return ir_tcl_strdel (interp, &p->mediumSetElementSetNames);
1554 free (p->mediumSetElementSetNames);
1555 if (ir_tcl_strdup (interp, &p->mediumSetElementSetNames,
1556 argv[2]) == TCL_ERROR)
1559 Tcl_AppendResult (interp, p->mediumSetElementSetNames, NULL);
1563 static IrTcl_Method ir_method_tab[] = {
1564 { 1, "comstack", do_comstack },
1565 { 1, "protocol", do_protocol },
1566 { 0, "failback", do_failback },
1567 { 0, "failInfo", do_failInfo },
1568 { 0, "logLevel", do_logLevel },
1570 { 0, "eventType", do_eventType },
1571 { 1, "connect", do_connect },
1572 { 0, "protocolVersion", do_protocolVersion },
1573 { 1, "preferredMessageSize", do_preferredMessageSize },
1574 { 1, "maximumRecordSize", do_maximumRecordSize },
1575 { 1, "implementationName", do_implementationName },
1576 { 1, "implementationId", do_implementationId },
1577 { 1, "implementationVersion", do_implementationVersion },
1578 { 0, "targetImplementationName", do_targetImplementationName },
1579 { 0, "targetImplementationId", do_targetImplementationId },
1580 { 0, "targetImplementationVersion", do_targetImplementationVersion },
1581 { 0, "userInformationField", do_userInformationField },
1582 { 1, "idAuthentication", do_idAuthentication },
1583 { 0, "options", do_options },
1584 { 0, "init", do_init_request },
1585 { 0, "initResult", do_initResult },
1586 { 0, "disconnect", do_disconnect },
1587 { 0, "callback", do_callback },
1588 { 0, "initResponse", do_initResponse },
1589 { 0, "triggerResourceControl", do_triggerResourceControl },
1590 { 0, "initResponse", do_initResponse },
1594 static IrTcl_Method ir_set_c_method_tab[] = {
1595 { 0, "databaseNames", do_databaseNames},
1596 { 0, "replaceIndicator", do_replaceIndicator},
1597 { 0, "queryType", do_queryType },
1598 { 0, "preferredRecordSyntax", do_preferredRecordSyntax },
1599 { 0, "smallSetUpperBound", do_smallSetUpperBound},
1600 { 0, "largeSetLowerBound", do_largeSetLowerBound},
1601 { 0, "mediumSetPresentNumber", do_mediumSetPresentNumber},
1602 { 0, "referenceId", do_referenceId },
1603 { 0, "elementSetNames", do_elementSetNames },
1604 { 0, "smallSetElementSetNames", do_smallSetElementSetNames },
1605 { 0, "mediumSetElementSetNames", do_mediumSetElementSetNames },
1610 * ir_obj_method: IR Object methods
1612 static int ir_obj_method (ClientData clientData, Tcl_Interp *interp,
1613 int argc, char **argv)
1615 IrTcl_Methods tab[3];
1616 IrTcl_Obj *p = clientData;
1619 return ir_method_r (clientData, interp, argc, argv, ir_method_tab);
1621 tab[0].tab = ir_method_tab;
1623 tab[1].tab = ir_set_c_method_tab;
1624 tab[1].obj = &p->set_inher;
1627 return ir_method (interp, argc, argv, tab);
1631 * ir_obj_delete: IR Object disposal
1633 static void ir_obj_delete (ClientData clientData)
1635 IrTcl_Obj *obj = clientData;
1636 IrTcl_Methods tab[3];
1639 if (obj->ref_count > 0)
1641 assert (obj->ref_count == 0);
1643 logf (LOG_DEBUG, "ir object delete");
1644 tab[0].tab = ir_method_tab;
1646 tab[1].tab = ir_set_c_method_tab;
1647 tab[1].obj = &obj->set_inher;
1650 ir_method (NULL, -1, NULL, tab);
1653 odr_destroy (obj->odr_in);
1654 odr_destroy (obj->odr_out);
1655 odr_destroy (obj->odr_pr);
1660 * ir_obj_mk: IR Object creation
1662 static int ir_obj_mk (ClientData clientData, Tcl_Interp *interp,
1663 int argc, char **argv)
1665 IrTcl_Methods tab[3];
1673 interp->result = "wrong # args";
1676 obj = ir_tcl_malloc (sizeof(*obj));
1679 obj->bibset = ccl_qual_mk ();
1680 if ((inf = fopen ("default.bib", "r")))
1682 ccl_qual_file (obj->bibset, inf);
1687 logf (LOG_DEBUG, "ir object create");
1688 obj->odr_in = odr_createmem (ODR_DECODE);
1689 obj->odr_out = odr_createmem (ODR_ENCODE);
1690 obj->odr_pr = odr_createmem (ODR_PRINT);
1691 obj->state = IR_TCL_R_Idle;
1692 obj->interp = interp;
1696 obj->request_queue = NULL;
1698 tab[0].tab = ir_method_tab;
1700 tab[1].tab = ir_set_c_method_tab;
1701 tab[1].obj = &obj->set_inher;
1704 if (ir_method (interp, 0, NULL, tab) == TCL_ERROR)
1706 Tcl_CreateCommand (interp, argv[1], ir_obj_method,
1707 (ClientData) obj, ir_obj_delete);
1711 /* ------------------------------------------------------- */
1713 * do_search: Do search request
1715 static int do_search (void *o, Tcl_Interp *interp, int argc, char **argv)
1717 Z_SearchRequest *req;
1721 IrTcl_SetObj *obj = o;
1732 interp->result = "wrong # args";
1735 if (!obj->set_inher.num_databaseNames)
1737 interp->result = "no databaseNames";
1742 interp->result = "search: not connected";
1745 apdu = zget_APDU (p->odr_out, Z_APDU_searchRequest);
1746 req = apdu->u.searchRequest;
1750 bib1.proto = p->protocol_type;
1751 bib1.oclass = CLASS_ATTSET;
1752 bib1.value = VAL_BIB1;
1754 set_referenceId (p->odr_out, &req->referenceId,
1755 obj->set_inher.referenceId);
1757 req->smallSetUpperBound = &obj->set_inher.smallSetUpperBound;
1758 req->largeSetLowerBound = &obj->set_inher.largeSetLowerBound;
1759 req->mediumSetPresentNumber = &obj->set_inher.mediumSetPresentNumber;
1760 req->replaceIndicator = &obj->set_inher.replaceIndicator;
1761 req->resultSetName = obj->setName ? obj->setName : "Default";
1762 logf (LOG_DEBUG, "Search, resultSetName %s", req->resultSetName);
1763 req->num_databaseNames = obj->set_inher.num_databaseNames;
1764 req->databaseNames = obj->set_inher.databaseNames;
1765 for (r=0; r < obj->set_inher.num_databaseNames; r++)
1766 logf (LOG_DEBUG, " Database %s", obj->set_inher.databaseNames[r]);
1767 if (obj->set_inher.preferredRecordSyntax)
1769 struct oident ident;
1771 ident.proto = p->protocol_type;
1772 ident.oclass = CLASS_RECSYN;
1773 ident.value = *obj->set_inher.preferredRecordSyntax;
1774 logf (LOG_DEBUG, "Preferred record syntax is %d", ident.value);
1775 req->preferredRecordSyntax = odr_oiddup (p->odr_out,
1776 oid_getoidbyent (&ident));
1779 req->preferredRecordSyntax = 0;
1781 if (obj->set_inher.smallSetElementSetNames &&
1782 *obj->set_inher.smallSetElementSetNames)
1784 Z_ElementSetNames *esn = odr_malloc (p->odr_out, sizeof(*esn));
1786 esn->which = Z_ElementSetNames_generic;
1787 esn->u.generic = obj->set_inher.smallSetElementSetNames;
1788 req->smallSetElementSetNames = esn;
1791 req->smallSetElementSetNames = NULL;
1793 if (obj->set_inher.mediumSetElementSetNames &&
1794 *obj->set_inher.mediumSetElementSetNames)
1796 Z_ElementSetNames *esn = odr_malloc (p->odr_out, sizeof(*esn));
1798 esn->which = Z_ElementSetNames_generic;
1799 esn->u.generic = obj->set_inher.mediumSetElementSetNames;
1800 req->mediumSetElementSetNames = esn;
1803 req->mediumSetElementSetNames = NULL;
1805 req->query = &query;
1807 if (!strcmp (obj->set_inher.queryType, "rpn"))
1809 Z_RPNQuery *RPNquery;
1811 RPNquery = p_query_rpn (p->odr_out, argv[2]);
1814 Tcl_AppendResult (interp, "Syntax error in query", NULL);
1817 RPNquery->attributeSetId = oid_getoidbyent (&bib1);
1818 query.which = Z_Query_type_1;
1819 query.u.type_1 = RPNquery;
1820 logf (LOG_DEBUG, "RPN");
1823 else if (!strcmp (obj->set_inher.queryType, "cclrpn"))
1827 struct ccl_rpn_node *rpn;
1828 Z_RPNQuery *RPNquery;
1830 rpn = ccl_find_str(p->bibset, argv[2], &error, &pos);
1833 Tcl_AppendResult (interp, "CCL error: ",
1834 ccl_err_msg(error), NULL);
1837 ccl_pr_tree (rpn, stderr);
1838 fprintf (stderr, "\n");
1839 assert((RPNquery = ccl_rpn_query(rpn)));
1840 RPNquery->attributeSetId = oid_getoidbyent (&bib1);
1841 query.which = Z_Query_type_1;
1842 query.u.type_1 = RPNquery;
1843 logf (LOG_DEBUG, "CCLRPN");
1846 else if (!strcmp (obj->set_inher.queryType, "ccl"))
1848 query.which = Z_Query_type_2;
1849 query.u.type_2 = &ccl_query;
1850 ccl_query.buf = (unsigned char *) argv[2];
1851 ccl_query.len = strlen (argv[2]);
1852 logf (LOG_DEBUG, "CCL");
1856 interp->result = "unknown query method";
1859 return ir_tcl_send_APDU (interp, p, apdu, "search", *argv);
1863 * do_searchResponse: add search response handler
1865 static int do_searchResponse (void *o, Tcl_Interp *interp,
1866 int argc, char **argv)
1868 IrTcl_SetObj *obj = o;
1872 obj->searchResponse = NULL;
1875 else if (argc == -1)
1876 return ir_tcl_strdel (interp, &obj->searchResponse);
1879 free (obj->searchResponse);
1882 if (ir_tcl_strdup (interp, &obj->searchResponse, argv[2])
1887 obj->searchResponse = NULL;
1893 * do_presentResponse: add present response handler
1895 static int do_presentResponse (void *o, Tcl_Interp *interp,
1896 int argc, char **argv)
1898 IrTcl_SetObj *obj = o;
1902 obj->presentResponse = NULL;
1905 else if (argc == -1)
1906 return ir_tcl_strdel (interp, &obj->presentResponse);
1909 free (obj->presentResponse);
1912 if (ir_tcl_strdup (interp, &obj->presentResponse, argv[2])
1917 obj->presentResponse = NULL;
1923 * do_resultCount: Get number of hits
1925 static int do_resultCount (void *o, Tcl_Interp *interp,
1926 int argc, char **argv)
1928 IrTcl_SetObj *obj = o;
1932 return get_set_int (&obj->resultCount, interp, argc, argv);
1936 * do_searchStatus: Get search status (after search response)
1938 static int do_searchStatus (void *o, Tcl_Interp *interp,
1939 int argc, char **argv)
1941 IrTcl_SetObj *obj = o;
1945 return get_set_int (&obj->searchStatus, interp, argc, argv);
1949 * do_presentStatus: Get search status (after search/present response)
1951 static int do_presentStatus (void *o, Tcl_Interp *interp,
1952 int argc, char **argv)
1954 IrTcl_SetObj *obj = o;
1958 return get_set_int (&obj->presentStatus, interp, argc, argv);
1962 * do_nextResultSetPosition: Get next result set position
1963 * (after search/present response)
1965 static int do_nextResultSetPosition (void *o, Tcl_Interp *interp,
1966 int argc, char **argv)
1968 IrTcl_SetObj *obj = o;
1972 obj->nextResultSetPosition = 0;
1975 return get_set_int (&obj->nextResultSetPosition, interp, argc, argv);
1979 * do_setName: Set result Set name
1981 static int do_setName (void *o, Tcl_Interp *interp,
1982 int argc, char **argv)
1984 IrTcl_SetObj *obj = o;
1987 return ir_tcl_strdup (interp, &obj->setName, "Default");
1988 else if (argc == -1)
1989 return ir_tcl_strdel (interp, &obj->setName);
1992 free (obj->setName);
1993 if (ir_tcl_strdup (interp, &obj->setName, argv[2])
1997 Tcl_AppendElement (interp, obj->setName);
2002 * do_numberOfRecordsReturned: Get number of records returned
2004 static int do_numberOfRecordsReturned (void *o, Tcl_Interp *interp,
2005 int argc, char **argv)
2007 IrTcl_SetObj *obj = o;
2011 obj->numberOfRecordsReturned = 0;
2014 return get_set_int (&obj->numberOfRecordsReturned, interp, argc, argv);
2018 * do_type: Return type (if any) at position.
2020 static int do_type (void *o, Tcl_Interp *interp, int argc, char **argv)
2022 IrTcl_SetObj *obj = o;
2024 IrTcl_RecordList *rl;
2028 obj->record_list = NULL;
2031 else if (argc == -1)
2033 delete_IR_records (obj);
2038 sprintf (interp->result, "wrong # args");
2041 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2043 rl = find_IR_record (obj, offset);
2046 logf (LOG_DEBUG, "No record at position %d", offset);
2051 case Z_NamePlusRecord_databaseRecord:
2052 interp->result = "DB";
2054 case Z_NamePlusRecord_surrogateDiagnostic:
2055 interp->result = "SD";
2063 * do_recordType: Return record type (if any) at position.
2065 static int do_recordType (void *o, Tcl_Interp *interp, int argc, char **argv)
2067 IrTcl_SetObj *obj = o;
2069 IrTcl_RecordList *rl;
2075 else if (argc == -1)
2081 sprintf (interp->result, "wrong # args");
2084 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2086 rl = find_IR_record (obj, offset);
2089 if (rl->which != Z_NamePlusRecord_databaseRecord)
2091 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2094 Tcl_AppendElement (interp, (char*)
2095 IrTcl_getRecordSyntaxStr (rl->u.dbrec.type));
2100 * set record elements (for record extraction)
2102 static int do_recordElements (void *o, Tcl_Interp *interp,
2103 int argc, char **argv)
2105 IrTcl_SetObj *obj = o;
2109 obj->recordElements = NULL;
2112 else if (argc == -1)
2113 return ir_tcl_strdel (NULL, &obj->recordElements);
2116 sprintf (interp->result, "wrong # args");
2121 free (obj->recordElements);
2122 return ir_tcl_strdup (NULL, &obj->recordElements,
2123 (*argv[2] ? argv[2] : NULL));
2125 Tcl_AppendResult (interp, obj->recordElements, NULL);
2132 static int ir_diagResult (Tcl_Interp *interp, IrTcl_Diagnostic *list, int num)
2138 for (i = 0; i<num; i++)
2140 logf (LOG_DEBUG, "Diagnostic, code %d", list[i].condition);
2141 sprintf (buf, "%d", list[i].condition);
2142 Tcl_AppendElement (interp, buf);
2143 cp = diagbib1_str (list[i].condition);
2145 Tcl_AppendElement (interp, (char*) cp);
2147 Tcl_AppendElement (interp, "");
2148 if (list[i].addinfo)
2149 Tcl_AppendElement (interp, (char*) list[i].addinfo);
2151 Tcl_AppendElement (interp, "");
2157 * do_diag: Return diagnostic record info
2159 static int do_diag (void *o, Tcl_Interp *interp, int argc, char **argv)
2161 IrTcl_SetObj *obj = o;
2163 IrTcl_RecordList *rl;
2169 sprintf (interp->result, "wrong # args");
2172 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2174 rl = find_IR_record (obj, offset);
2177 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2180 if (rl->which != Z_NamePlusRecord_surrogateDiagnostic)
2182 Tcl_AppendResult (interp, "No Diagnostic record at #", argv[2], NULL);
2185 return ir_diagResult (interp, rl->u.surrogateDiagnostics.list,
2186 rl->u.surrogateDiagnostics.num);
2190 * do_getMarc: Get ISO2709 Record lines/fields
2192 static int do_getMarc (void *o, Tcl_Interp *interp, int argc, char **argv)
2194 IrTcl_SetObj *obj = o;
2196 IrTcl_RecordList *rl;
2202 sprintf (interp->result, "wrong # args");
2205 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2207 rl = find_IR_record (obj, offset);
2210 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2213 if (rl->which != Z_NamePlusRecord_databaseRecord)
2215 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2218 return ir_tcl_get_marc (interp, rl->u.dbrec.buf, argc, argv);
2222 * do_getSutrs: Get SUTRS Record
2224 static int do_getSutrs (void *o, Tcl_Interp *interp, int argc, char **argv)
2226 IrTcl_SetObj *obj = o;
2228 IrTcl_RecordList *rl;
2234 sprintf (interp->result, "wrong # args");
2237 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2239 rl = find_IR_record (obj, offset);
2242 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2245 if (rl->which != Z_NamePlusRecord_databaseRecord)
2247 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2250 if (rl->u.dbrec.type != VAL_SUTRS)
2252 Tcl_AppendElement (interp, rl->u.dbrec.buf);
2258 * do_getGrs: Get a GRS1 Record
2260 static int do_getGrs (void *o, Tcl_Interp *interp, int argc, char **argv)
2262 IrTcl_SetObj *obj = o;
2264 IrTcl_RecordList *rl;
2270 sprintf (interp->result, "wrong # args");
2273 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2275 rl = find_IR_record (obj, offset);
2278 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2281 if (rl->which != Z_NamePlusRecord_databaseRecord)
2283 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2286 if (rl->u.dbrec.type != VAL_GRS1)
2288 return ir_tcl_get_grs (interp, rl->u.dbrec.u.grs1, argc, argv);
2293 * do_responseStatus: Return response status (present or search)
2295 static int do_responseStatus (void *o, Tcl_Interp *interp,
2296 int argc, char **argv)
2298 IrTcl_SetObj *obj = o;
2302 obj->recordFlag = 0;
2303 obj->nonSurrogateDiagnosticNum = 0;
2304 obj->nonSurrogateDiagnosticList = NULL;
2307 else if (argc == -1)
2309 ir_deleteDiags (&obj->nonSurrogateDiagnosticList,
2310 &obj->nonSurrogateDiagnosticNum);
2313 if (!obj->recordFlag)
2315 Tcl_AppendElement (interp, "OK");
2320 case Z_Records_DBOSD:
2321 Tcl_AppendElement (interp, "DBOSD");
2324 Tcl_AppendElement (interp, "NSD");
2325 return ir_diagResult (interp, obj->nonSurrogateDiagnosticList,
2326 obj->nonSurrogateDiagnosticNum);
2332 * do_present: Perform Present Request
2335 static int do_present (void *o, Tcl_Interp *interp, int argc, char **argv)
2337 IrTcl_SetObj *obj = o;
2340 Z_PresentRequest *req;
2348 if (Tcl_GetInt (interp, argv[2], &start) == TCL_ERROR)
2355 if (Tcl_GetInt (interp, argv[3], &number) == TCL_ERROR)
2363 interp->result = "present: not connected";
2368 obj->number = number;
2370 apdu = zget_APDU (p->odr_out, Z_APDU_presentRequest);
2371 req = apdu->u.presentRequest;
2373 set_referenceId (p->odr_out, &req->referenceId,
2374 obj->set_inher.referenceId);
2376 req->resultSetId = obj->setName ? obj->setName : "Default";
2378 req->resultSetStartPoint = &start;
2379 req->numberOfRecordsRequested = &number;
2380 if (obj->set_inher.preferredRecordSyntax)
2382 struct oident ident;
2384 ident.proto = p->protocol_type;
2385 ident.oclass = CLASS_RECSYN;
2386 ident.value = *obj->set_inher.preferredRecordSyntax;
2387 logf (LOG_DEBUG, "Preferred record syntax is %d", ident.value);
2388 req->preferredRecordSyntax = odr_oiddup (p->odr_out,
2389 oid_getoidbyent (&ident));
2392 req->preferredRecordSyntax = 0;
2394 if (obj->set_inher.elementSetNames && *obj->set_inher.elementSetNames)
2396 Z_ElementSetNames *esn = odr_malloc (p->odr_out, sizeof(*esn));
2397 Z_RecordComposition *compo = odr_malloc (p->odr_out, sizeof(*compo));
2399 esn->which = Z_ElementSetNames_generic;
2400 esn->u.generic = obj->set_inher.elementSetNames;
2402 req->recordComposition = compo;
2403 compo->which = Z_RecordComp_simple;
2404 compo->u.simple = esn;
2407 req->recordComposition = NULL;
2408 return ir_tcl_send_APDU (interp, p, apdu, "present", *argv);
2412 * do_loadFile: Load result set from file
2415 static int do_loadFile (void *o, Tcl_Interp *interp,
2416 int argc, char **argv)
2418 IrTcl_SetObj *setobj = o;
2428 interp->result = "wrong # args";
2431 inf = fopen (argv[2], "r");
2434 Tcl_AppendResult (interp, "Cannot open ", argv[2], NULL);
2437 while ((buf = ir_tcl_fread_marc (inf, &size)))
2439 IrTcl_RecordList *rl;
2441 rl = new_IR_record (setobj, no, Z_NamePlusRecord_databaseRecord, "F");
2442 rl->u.dbrec.type = VAL_USMARC;
2443 rl->u.dbrec.buf = buf;
2444 rl->u.dbrec.size = size;
2447 setobj->numberOfRecordsReturned = no-1;
2452 static IrTcl_Method ir_set_method_tab[] = {
2453 { 0, "search", do_search },
2454 { 0, "searchResponse", do_searchResponse },
2455 { 0, "presentResponse", do_presentResponse },
2456 { 0, "searchStatus", do_searchStatus },
2457 { 0, "presentStatus", do_presentStatus },
2458 { 0, "nextResultSetPosition", do_nextResultSetPosition },
2459 { 0, "setName", do_setName },
2460 { 0, "resultCount", do_resultCount },
2461 { 0, "numberOfRecordsReturned", do_numberOfRecordsReturned },
2462 { 0, "present", do_present },
2463 { 0, "type", do_type },
2464 { 0, "getMarc", do_getMarc },
2465 { 0, "getSutrs", do_getSutrs },
2466 { 0, "getGrs", do_getGrs },
2467 { 0, "recordType", do_recordType },
2468 { 0, "recordElements", do_recordElements },
2469 { 0, "diag", do_diag },
2470 { 0, "responseStatus", do_responseStatus },
2471 { 0, "loadFile", do_loadFile },
2476 * ir_set_obj_method: IR Set Object methods
2478 static int ir_set_obj_method (ClientData clientData, Tcl_Interp *interp,
2479 int argc, char **argv)
2481 IrTcl_Methods tabs[3];
2482 IrTcl_SetObj *p = clientData;
2486 interp->result = "wrong # args";
2489 tabs[0].tab = ir_set_method_tab;
2491 tabs[1].tab = ir_set_c_method_tab;
2492 tabs[1].obj = &p->set_inher;
2495 return ir_method (interp, argc, argv, tabs);
2499 * ir_set_obj_delete: IR Set Object disposal
2501 static void ir_set_obj_delete (ClientData clientData)
2503 IrTcl_Methods tabs[3];
2504 IrTcl_SetObj *p = clientData;
2506 logf (LOG_DEBUG, "ir set delete");
2508 tabs[0].tab = ir_set_method_tab;
2510 tabs[1].tab = ir_set_c_method_tab;
2511 tabs[1].obj = &p->set_inher;
2514 ir_method (NULL, -1, NULL, tabs);
2520 * ir_set_obj_mk: IR Set Object creation
2522 static int ir_set_obj_mk (ClientData clientData, Tcl_Interp *interp,
2523 int argc, char **argv)
2525 IrTcl_Methods tabs[3];
2528 if (argc < 2 || argc > 3)
2530 interp->result = "wrong # args";
2533 obj = ir_tcl_malloc (sizeof(*obj));
2534 logf (LOG_DEBUG, "ir set create");
2537 Tcl_CmdInfo parent_info;
2542 if (!Tcl_GetCommandInfo (interp, argv[2], &parent_info))
2544 interp->result = "No parent";
2547 obj->parent = (IrTcl_Obj *) parent_info.clientData;
2549 dst = &obj->set_inher;
2550 src = &obj->parent->set_inher;
2552 if ((dst->num_databaseNames = src->num_databaseNames))
2553 dst->databaseNames =
2554 ir_tcl_malloc (sizeof (*dst->databaseNames)
2555 * dst->num_databaseNames);
2557 dst->databaseNames = NULL;
2558 for (i = 0; i < dst->num_databaseNames; i++)
2559 if (ir_tcl_strdup (interp, &dst->databaseNames[i],
2560 src->databaseNames[i]) == TCL_ERROR)
2562 if (ir_tcl_strdup (interp, &dst->queryType, src->queryType)
2566 if (ir_tcl_strdup (interp, &dst->referenceId, src->referenceId)
2570 if (ir_tcl_strdup (interp, &dst->elementSetNames, src->elementSetNames)
2574 if (ir_tcl_strdup (interp, &dst->smallSetElementSetNames,
2575 src->smallSetElementSetNames)
2579 if (ir_tcl_strdup (interp, &dst->mediumSetElementSetNames,
2580 src->mediumSetElementSetNames)
2584 if (src->preferredRecordSyntax &&
2585 (dst->preferredRecordSyntax
2586 = ir_tcl_malloc (sizeof(*dst->preferredRecordSyntax))))
2587 *dst->preferredRecordSyntax = *src->preferredRecordSyntax;
2589 dst->preferredRecordSyntax = NULL;
2590 dst->replaceIndicator = src->replaceIndicator;
2591 dst->smallSetUpperBound = src->smallSetUpperBound;
2592 dst->largeSetLowerBound = src->largeSetLowerBound;
2593 dst->mediumSetPresentNumber = src->mediumSetPresentNumber;
2598 tabs[0].tab = ir_set_method_tab;
2602 if (ir_method (interp, 0, NULL, tabs) == TCL_ERROR)
2605 Tcl_CreateCommand (interp, argv[1], ir_set_obj_method,
2606 (ClientData) obj, ir_set_obj_delete);
2610 /* ------------------------------------------------------- */
2613 * do_scan: Perform scan
2615 static int do_scan (void *o, Tcl_Interp *interp, int argc, char **argv)
2619 IrTcl_ScanObj *obj = o;
2620 IrTcl_Obj *p = obj->parent;
2623 struct ccl_rpn_node *rpn;
2631 interp->result = "wrong # args";
2634 if (!p->set_inher.num_databaseNames)
2636 interp->result = "no databaseNames";
2641 interp->result = "scan: not connected";
2645 bib1.proto = p->protocol_type;
2646 bib1.oclass = CLASS_ATTSET;
2647 bib1.value = VAL_BIB1;
2649 apdu = zget_APDU (p->odr_out, Z_APDU_scanRequest);
2650 req = apdu->u.scanRequest;
2652 set_referenceId (p->odr_out, &req->referenceId, p->set_inher.referenceId);
2653 req->num_databaseNames = p->set_inher.num_databaseNames;
2654 req->databaseNames = p->set_inher.databaseNames;
2655 req->attributeSet = oid_getoidbyent (&bib1);
2658 if (!(req->termListAndStartPoint = p_query_scan (p->odr_out, argv[2])))
2660 Tcl_AppendResult (interp, "Syntax error in query", NULL);
2664 rpn = ccl_find_str(p->bibset, argv[2], &r, &pos);
2667 Tcl_AppendResult (interp, "CCL error: ", ccl_err_msg (r), NULL);
2670 ccl_pr_tree (rpn, stderr);
2671 fprintf (stderr, "\n");
2672 if (!(req->termListAndStartPoint = ccl_scan_query (rpn)))
2675 req->stepSize = &obj->stepSize;
2676 req->numberOfTermsRequested = &obj->numberOfTermsRequested;
2677 req->preferredPositionInResponse = &obj->preferredPositionInResponse;
2678 logf (LOG_DEBUG, "stepSize=%d", *req->stepSize);
2679 logf (LOG_DEBUG, "numberOfTermsRequested=%d",
2680 *req->numberOfTermsRequested);
2681 logf (LOG_DEBUG, "preferredPositionInResponse=%d",
2682 *req->preferredPositionInResponse);
2684 return ir_tcl_send_APDU (interp, p, apdu, "scan", *argv);
2688 * do_scanResponse: add scan response handler
2690 static int do_scanResponse (void *o, Tcl_Interp *interp,
2691 int argc, char **argv)
2693 IrTcl_ScanObj *obj = o;
2697 obj->scanResponse = NULL;
2700 else if (argc == -1)
2701 return ir_tcl_strdel (interp, &obj->scanResponse);
2704 free (obj->scanResponse);
2707 if (ir_tcl_strdup (interp, &obj->scanResponse, argv[2])
2712 obj->scanResponse = NULL;
2718 * do_stepSize: Set/get replace Step Size
2720 static int do_stepSize (void *obj, Tcl_Interp *interp,
2721 int argc, char **argv)
2723 IrTcl_ScanObj *p = obj;
2729 return get_set_int (&p->stepSize, interp, argc, argv);
2733 * do_numberOfTermsRequested: Set/get Number of Terms requested
2735 static int do_numberOfTermsRequested (void *obj, Tcl_Interp *interp,
2736 int argc, char **argv)
2738 IrTcl_ScanObj *p = obj;
2742 p->numberOfTermsRequested = 20;
2745 return get_set_int (&p->numberOfTermsRequested, interp, argc, argv);
2750 * do_preferredPositionInResponse: Set/get preferred Position
2752 static int do_preferredPositionInResponse (void *obj, Tcl_Interp *interp,
2753 int argc, char **argv)
2755 IrTcl_ScanObj *p = obj;
2759 p->preferredPositionInResponse = 1;
2762 return get_set_int (&p->preferredPositionInResponse, interp, argc, argv);
2766 * do_scanStatus: Get scan status
2768 static int do_scanStatus (void *obj, Tcl_Interp *interp,
2769 int argc, char **argv)
2771 IrTcl_ScanObj *p = obj;
2775 return get_set_int (&p->scanStatus, interp, argc, argv);
2779 * do_numberOfEntriesReturned: Get number of Entries returned
2781 static int do_numberOfEntriesReturned (void *obj, Tcl_Interp *interp,
2782 int argc, char **argv)
2784 IrTcl_ScanObj *p = obj;
2788 return get_set_int (&p->numberOfEntriesReturned, interp, argc, argv);
2792 * do_positionOfTerm: Get position of Term
2794 static int do_positionOfTerm (void *obj, Tcl_Interp *interp,
2795 int argc, char **argv)
2797 IrTcl_ScanObj *p = obj;
2801 return get_set_int (&p->positionOfTerm, interp, argc, argv);
2805 * do_scanLine: get Scan Line (surrogate or normal) after response
2807 static int do_scanLine (void *obj, Tcl_Interp *interp, int argc, char **argv)
2809 IrTcl_ScanObj *p = obj;
2815 p->entries_flag = 0;
2817 p->nonSurrogateDiagnosticNum = 0;
2818 p->nonSurrogateDiagnosticList = 0;
2821 else if (argc == -1)
2823 p->entries_flag = 0;
2824 /* release entries */
2827 ir_deleteDiags (&p->nonSurrogateDiagnosticList,
2828 &p->nonSurrogateDiagnosticNum);
2833 interp->result = "wrong # args";
2836 if (Tcl_GetInt (interp, argv[2], &i) == TCL_ERROR)
2838 if (!p->entries_flag || p->which != Z_ListEntries_entries || !p->entries
2839 || i >= p->num_entries || i < 0)
2841 switch (p->entries[i].which)
2843 case Z_Entry_termInfo:
2844 Tcl_AppendElement (interp, "T");
2845 if (p->entries[i].u.term.buf)
2846 Tcl_AppendElement (interp, p->entries[i].u.term.buf);
2848 Tcl_AppendElement (interp, "");
2849 sprintf (numstr, "%d", p->entries[i].u.term.globalOccurrences);
2850 Tcl_AppendElement (interp, numstr);
2852 case Z_Entry_surrogateDiagnostic:
2853 Tcl_AppendElement (interp, "SD");
2854 return ir_diagResult (interp, p->entries[i].u.diag.list,
2855 p->entries[i].u.diag.num);
2861 static IrTcl_Method ir_scan_method_tab[] = {
2862 { 0, "scan", do_scan },
2863 { 0, "scanResponse", do_scanResponse },
2864 { 0, "stepSize", do_stepSize },
2865 { 0, "numberOfTermsRequested", do_numberOfTermsRequested },
2866 { 0, "preferredPositionInResponse", do_preferredPositionInResponse },
2867 { 0, "scanStatus", do_scanStatus },
2868 { 0, "numberOfEntriesReturned", do_numberOfEntriesReturned },
2869 { 0, "positionOfTerm", do_positionOfTerm },
2870 { 0, "scanLine", do_scanLine },
2875 * ir_scan_obj_method: IR Scan Object methods
2877 static int ir_scan_obj_method (ClientData clientData, Tcl_Interp *interp,
2878 int argc, char **argv)
2880 IrTcl_Methods tabs[2];
2884 interp->result = "wrong # args";
2887 tabs[0].tab = ir_scan_method_tab;
2888 tabs[0].obj = clientData;
2891 return ir_method (interp, argc, argv, tabs);
2895 * ir_scan_obj_delete: IR Scan Object disposal
2897 static void ir_scan_obj_delete (ClientData clientData)
2899 IrTcl_Methods tabs[2];
2900 IrTcl_ScanObj *obj = clientData;
2902 tabs[0].tab = ir_scan_method_tab;
2906 ir_method (NULL, -1, NULL, tabs);
2911 * ir_scan_obj_mk: IR Scan Object creation
2913 static int ir_scan_obj_mk (ClientData clientData, Tcl_Interp *interp,
2914 int argc, char **argv)
2916 Tcl_CmdInfo parent_info;
2918 IrTcl_Methods tabs[2];
2922 interp->result = "wrong # args";
2925 if (!Tcl_GetCommandInfo (interp, argv[2], &parent_info))
2927 interp->result = "No parent";
2930 obj = ir_tcl_malloc (sizeof(*obj));
2931 obj->parent = (IrTcl_Obj *) parent_info.clientData;
2933 tabs[0].tab = ir_scan_method_tab;
2937 if (ir_method (interp, 0, NULL, tabs) == TCL_ERROR)
2939 Tcl_CreateCommand (interp, argv[1], ir_scan_obj_method,
2940 (ClientData) obj, ir_scan_obj_delete);
2944 /* ------------------------------------------------------- */
2946 static void ir_initResponse (void *obj, Z_InitResponse *initrs)
2950 p->initResult = *initrs->result ? 1 : 0;
2951 if (!*initrs->result)
2952 logf (LOG_DEBUG, "Connection rejected by target");
2954 logf (LOG_DEBUG, "Connection accepted by target");
2956 get_referenceId (&p->set_inher.referenceId, initrs->referenceId);
2958 free (p->targetImplementationId);
2959 ir_tcl_strdup (p->interp, &p->targetImplementationId,
2960 initrs->implementationId);
2961 free (p->targetImplementationName);
2962 ir_tcl_strdup (p->interp, &p->targetImplementationName,
2963 initrs->implementationName);
2964 free (p->targetImplementationVersion);
2965 ir_tcl_strdup (p->interp, &p->targetImplementationVersion,
2966 initrs->implementationVersion);
2968 p->maximumRecordSize = *initrs->maximumRecordSize;
2969 p->preferredMessageSize = *initrs->preferredMessageSize;
2971 memcpy (&p->options, initrs->options, sizeof(initrs->options));
2972 memcpy (&p->protocolVersion, initrs->protocolVersion,
2973 sizeof(initrs->protocolVersion));
2974 free (p->userInformationField);
2975 p->userInformationField = NULL;
2976 if (initrs->userInformationField)
2980 if (initrs->userInformationField->which == ODR_EXTERNAL_octet &&
2981 (p->userInformationField =
2982 ir_tcl_malloc ((len =
2983 initrs->userInformationField->
2984 u.octet_aligned->len) +1)))
2986 memcpy (p->userInformationField,
2987 initrs->userInformationField->u.octet_aligned->buf,
2989 (p->userInformationField)[len] = '\0';
2994 static void ir_deleteDiags (IrTcl_Diagnostic **dst_list, int *dst_num)
2997 for (i = 0; i<*dst_num; i++)
2998 free (dst_list[i]->addinfo);
3004 static void ir_handleDiags (IrTcl_Diagnostic **dst_list, int *dst_num,
3005 Z_DiagRec **list, int num)
3011 *dst_list = ir_tcl_malloc (sizeof(**dst_list) * num);
3012 for (i = 0; i<num; i++)
3014 switch (list[i]->which)
3016 case Z_DiagRec_defaultFormat:
3017 (*dst_list)[i].condition = *list[i]->u.defaultFormat->condition;
3018 addinfo = list[i]->u.defaultFormat->addinfo;
3020 ((*dst_list)[i].addinfo = ir_tcl_malloc (strlen(addinfo)+1)))
3021 strcpy ((*dst_list)[i].addinfo, addinfo);
3024 (*dst_list)[i].addinfo = NULL;
3025 (*dst_list)[i].condition = 0;
3030 static void ir_handleRecords (void *o, Z_Records *zrs, IrTcl_SetObj *setobj,
3031 const char *elements)
3036 IrTcl_RecordList *rl;
3038 setobj->which = zrs->which;
3039 setobj->recordFlag = 1;
3041 ir_deleteDiags (&setobj->nonSurrogateDiagnosticList,
3042 &setobj->nonSurrogateDiagnosticNum);
3043 if (zrs->which == Z_Records_DBOSD)
3045 setobj->numberOfRecordsReturned =
3046 zrs->u.databaseOrSurDiagnostics->num_records;
3047 logf (LOG_DEBUG, "Got %d records", setobj->numberOfRecordsReturned);
3048 for (offset = 0; offset<setobj->numberOfRecordsReturned; offset++)
3050 rl = new_IR_record (setobj, setobj->start + offset,
3051 zrs->u.databaseOrSurDiagnostics->
3052 records[offset]->which,
3054 if (rl->which == Z_NamePlusRecord_surrogateDiagnostic)
3056 ir_handleDiags (&rl->u.surrogateDiagnostics.list,
3057 &rl->u.surrogateDiagnostics.num,
3058 &zrs->u.databaseOrSurDiagnostics->
3059 records[offset]->u.surrogateDiagnostic,
3064 Z_DatabaseRecord *zr;
3066 struct oident *ident;
3068 zr = zrs->u.databaseOrSurDiagnostics->records[offset]
3070 oe = (Z_External*) zr;
3071 rl->u.dbrec.size = zr->u.octet_aligned->len;
3073 if ((ident = oid_getentbyoid (oe->direct_reference)))
3074 rl->u.dbrec.type = ident->value;
3076 rl->u.dbrec.type = VAL_USMARC;
3078 if (oe->which == ODR_EXTERNAL_octet && rl->u.dbrec.size > 0)
3080 char *buf = (char*) zr->u.octet_aligned->buf;
3081 if ((rl->u.dbrec.buf = ir_tcl_malloc (rl->u.dbrec.size)))
3082 memcpy (rl->u.dbrec.buf, buf, rl->u.dbrec.size);
3084 else if (rl->u.dbrec.type == VAL_SUTRS &&
3085 oe->which == Z_External_sutrs)
3087 odr_setbuf (p->odr_in, (char*) oe->u.single_ASN1_type->buf,
3088 oe->u.single_ASN1_type->len, 0);
3089 if ((rl->u.dbrec.buf = ir_tcl_malloc (oe->u.sutrs->len+1)))
3091 memcpy (rl->u.dbrec.buf, oe->u.sutrs->buf,
3093 rl->u.dbrec.buf[oe->u.sutrs->len] = '\0';
3095 rl->u.dbrec.size = oe->u.sutrs->len;
3097 else if (rl->u.dbrec.type == VAL_GRS1 &&
3098 oe->which == Z_External_grs1)
3100 ir_tcl_read_grs (oe->u.grs1, &rl->u.dbrec.u.grs1);
3101 rl->u.dbrec.buf = NULL;
3104 rl->u.dbrec.buf = NULL;
3108 else if (zrs->which == Z_Records_multipleNSD)
3110 logf (LOG_DEBUG, "multipleNonSurrogateDiagnostic %d",
3111 zrs->u.multipleNonSurDiagnostics->num_diagRecs);
3112 setobj->numberOfRecordsReturned = 0;
3113 ir_handleDiags (&setobj->nonSurrogateDiagnosticList,
3114 &setobj->nonSurrogateDiagnosticNum,
3115 zrs->u.multipleNonSurDiagnostics->diagRecs,
3116 zrs->u.multipleNonSurDiagnostics->num_diagRecs);
3120 logf (LOG_DEBUG, "NonSurrogateDiagnostic");
3121 setobj->numberOfRecordsReturned = 0;
3122 ir_handleDiags (&setobj->nonSurrogateDiagnosticList,
3123 &setobj->nonSurrogateDiagnosticNum,
3124 &zrs->u.nonSurrogateDiagnostic,
3129 static void ir_searchResponse (void *o, Z_SearchResponse *searchrs,
3130 IrTcl_SetObj *setobj)
3132 Z_Records *zrs = searchrs->records;
3134 logf (LOG_DEBUG, "Received search response");
3137 logf (LOG_DEBUG, "Search response, no object!");
3140 setobj->searchStatus = searchrs->searchStatus ? 1 : 0;
3141 get_referenceId (&setobj->set_inher.referenceId, searchrs->referenceId);
3142 setobj->resultCount = *searchrs->resultCount;
3143 if (searchrs->presentStatus)
3144 setobj->presentStatus = *searchrs->presentStatus;
3145 if (searchrs->nextResultSetPosition)
3146 setobj->nextResultSetPosition = *searchrs->nextResultSetPosition;
3148 logf (LOG_DEBUG, "Search response %d, %d hits",
3149 setobj->searchStatus, setobj->resultCount);
3153 if (setobj->resultCount <= setobj->set_inher.smallSetUpperBound)
3154 es = setobj->set_inher.smallSetElementSetNames;
3156 es = setobj->set_inher.mediumSetElementSetNames;
3157 ir_handleRecords (o, zrs, setobj, es);
3160 setobj->recordFlag = 0;
3164 static void ir_presentResponse (void *o, Z_PresentResponse *presrs,
3165 IrTcl_SetObj *setobj)
3167 Z_Records *zrs = presrs->records;
3169 logf (LOG_DEBUG, "Received present response");
3172 logf (LOG_DEBUG, "Present response, no object!");
3175 setobj->presentStatus = *presrs->presentStatus;
3176 get_referenceId (&setobj->set_inher.referenceId, presrs->referenceId);
3177 setobj->nextResultSetPosition = *presrs->nextResultSetPosition;
3179 ir_handleRecords (o, zrs, setobj, setobj->set_inher.elementSetNames);
3182 setobj->recordFlag = 0;
3183 logf (LOG_DEBUG, "No records!");
3187 static void ir_scanResponse (void *o, Z_ScanResponse *scanrs,
3188 IrTcl_ScanObj *scanobj)
3192 logf (LOG_DEBUG, "Received scanResponse");
3194 get_referenceId (&p->set_inher.referenceId, scanrs->referenceId);
3195 scanobj->scanStatus = *scanrs->scanStatus;
3196 logf (LOG_DEBUG, "scanStatus=%d", scanobj->scanStatus);
3198 if (scanrs->stepSize)
3199 scanobj->stepSize = *scanrs->stepSize;
3200 logf (LOG_DEBUG, "stepSize=%d", scanobj->stepSize);
3202 scanobj->numberOfEntriesReturned = *scanrs->numberOfEntriesReturned;
3203 logf (LOG_DEBUG, "numberOfEntriesReturned=%d",
3204 scanobj->numberOfEntriesReturned);
3206 if (scanrs->positionOfTerm)
3207 scanobj->positionOfTerm = *scanrs->positionOfTerm;
3209 scanobj->positionOfTerm = -1;
3210 logf (LOG_DEBUG, "positionOfTerm=%d", scanobj->positionOfTerm);
3212 free (scanobj->entries);
3213 scanobj->entries = NULL;
3215 ir_deleteDiags (&scanobj->nonSurrogateDiagnosticList,
3216 &scanobj->nonSurrogateDiagnosticNum);
3217 if (scanrs->entries)
3222 scanobj->entries_flag = 1;
3223 scanobj->which = scanrs->entries->which;
3224 switch (scanobj->which)
3226 case Z_ListEntries_entries:
3227 scanobj->num_entries = scanrs->entries->u.entries->num_entries;
3228 scanobj->entries = ir_tcl_malloc (scanobj->num_entries *
3229 sizeof(*scanobj->entries));
3230 for (i=0; i<scanobj->num_entries; i++)
3232 ze = scanrs->entries->u.entries->entries[i];
3233 scanobj->entries[i].which = ze->which;
3236 case Z_Entry_termInfo:
3237 if (ze->u.termInfo->term->which == Z_Term_general)
3239 int l = ze->u.termInfo->term->u.general->len;
3240 scanobj->entries[i].u.term.buf = ir_tcl_malloc (1+l);
3241 memcpy (scanobj->entries[i].u.term.buf,
3242 ze->u.termInfo->term->u.general->buf,
3244 scanobj->entries[i].u.term.buf[l] = '\0';
3247 scanobj->entries[i].u.term.buf = NULL;
3248 if (ze->u.termInfo->globalOccurrences)
3249 scanobj->entries[i].u.term.globalOccurrences =
3250 *ze->u.termInfo->globalOccurrences;
3252 scanobj->entries[i].u.term.globalOccurrences = 0;
3254 case Z_Entry_surrogateDiagnostic:
3255 ir_handleDiags (&scanobj->entries[i].u.diag.list,
3256 &scanobj->entries[i].u.diag.num,
3257 &ze->u.surrogateDiagnostic,
3263 case Z_ListEntries_nonSurrogateDiagnostics:
3264 ir_handleDiags (&scanobj->nonSurrogateDiagnosticList,
3265 &scanobj->nonSurrogateDiagnosticNum,
3266 scanrs->entries->u.nonSurrogateDiagnostics->
3268 scanrs->entries->u.nonSurrogateDiagnostics->
3274 scanobj->entries_flag = 0;
3278 * ir_select_read: handle incoming packages
3280 void ir_select_read (ClientData clientData)
3282 IrTcl_Obj *p = clientData;
3287 Tcl_CmdInfo cmd_info;
3288 const char *apdu_call;
3290 if (p->state == IR_TCL_R_Connecting)
3292 r = cs_rcvconnect (p->cs_link);
3295 logf (LOG_WARN, "cs_rcvconnect returned 1");
3298 p->state = IR_TCL_R_Idle;
3299 ir_select_remove_write (cs_fileno (p->cs_link), p);
3302 logf (LOG_DEBUG, "cs_rcvconnect error");
3305 p->failInfo = IR_TCL_FAIL_CONNECT;
3306 IrTcl_eval (p->interp, p->failback);
3308 do_disconnect (p, NULL, 2, NULL);
3311 p->state = IR_TCL_R_Idle;
3313 IrTcl_eval (p->interp, p->callback);
3314 if (p->cs_link && p->request_queue && p->state == IR_TCL_R_Idle)
3315 ir_tcl_send_q (p, p->request_queue, "x");
3320 /* signal one more use of ir object - callbacks must not
3321 release the ir memory (p pointer) */
3322 p->state = IR_TCL_R_Reading;
3325 /* read incoming APDU */
3326 if ((r=cs_get (p->cs_link, &p->buf_in, &p->len_in)) <= 0)
3328 logf (LOG_DEBUG, "cs_get failed, code %d", r);
3329 ir_select_remove (cs_fileno (p->cs_link), p);
3330 do_disconnect (p, NULL, 2, NULL);
3333 p->failInfo = IR_TCL_FAIL_READ;
3334 IrTcl_eval (p->interp, p->failback);
3336 /* release ir object now if callback deleted it */
3342 /* got complete APDU. Now decode */
3343 odr_setbuf (p->odr_in, p->buf_in, r, 0);
3344 logf (LOG_DEBUG, "cs_get ok, got %d", r);
3345 if (!z_APDU (p->odr_in, &apdu, 0))
3347 logf (LOG_DEBUG, "%s", odr_errmsg (odr_geterror (p->odr_in)));
3348 do_disconnect (p, NULL, 2, NULL);
3351 p->failInfo = IR_TCL_FAIL_IN_APDU;
3352 IrTcl_eval (p->interp, p->failback);
3354 /* release ir object now if failback deleted it */
3358 /* handle APDU and invoke callback */
3359 rq = p->request_queue;
3362 logf (LOG_FATAL, "Internal error. No queue entry");
3365 object_name = rq->object_name;
3366 logf (LOG_DEBUG, "getCommandInfo (%s)", object_name);
3368 if (Tcl_GetCommandInfo (p->interp, object_name, &cmd_info))
3372 case Z_APDU_initResponse:
3373 p->eventType = "init";
3374 ir_initResponse (p, apdu->u.initResponse);
3375 apdu_call = p->initResponse;
3377 case Z_APDU_searchResponse:
3378 p->eventType = "search";
3379 ir_searchResponse (p, apdu->u.searchResponse,
3380 (IrTcl_SetObj *) cmd_info.clientData);
3381 apdu_call = ((IrTcl_SetObj *)
3382 cmd_info.clientData)->searchResponse;
3384 case Z_APDU_presentResponse:
3385 p->eventType = "present";
3386 ir_presentResponse (p, apdu->u.presentResponse,
3387 (IrTcl_SetObj *) cmd_info.clientData);
3388 apdu_call = ((IrTcl_SetObj *)
3389 cmd_info.clientData)->presentResponse;
3391 case Z_APDU_scanResponse:
3392 p->eventType = "scan";
3393 ir_scanResponse (p, apdu->u.scanResponse,
3394 (IrTcl_ScanObj *) cmd_info.clientData);
3395 apdu_call = ((IrTcl_ScanObj *)
3396 cmd_info.clientData)->scanResponse;
3399 logf (LOG_WARN, "Received unknown APDU type (%d)",
3401 do_disconnect (p, NULL, 2, NULL);
3404 p->failInfo = IR_TCL_FAIL_UNKNOWN_APDU;
3405 IrTcl_eval (p->interp, p->failback);
3410 p->request_queue = rq->next;
3411 p->state = IR_TCL_R_Idle;
3414 IrTcl_eval (p->interp, apdu_call);
3415 else if (rq->callback)
3416 IrTcl_eval (p->interp, rq->callback);
3418 free (rq->callback);
3419 free (rq->object_name);
3421 odr_reset (p->odr_in);
3422 if (p->ref_count == 1)
3428 } while (p->cs_link && cs_more (p->cs_link));
3429 if (p->cs_link && p->request_queue && p->state == IR_TCL_R_Idle)
3430 ir_tcl_send_q (p, p->request_queue, "x");
3434 * ir_select_write: handle outgoing packages - not yet written.
3436 void ir_select_write (ClientData clientData)
3438 IrTcl_Obj *p = clientData;
3442 logf (LOG_DEBUG, "In write handler");
3443 if (p->state == IR_TCL_R_Connecting)
3445 r = cs_rcvconnect (p->cs_link);
3448 p->state = IR_TCL_R_Idle;
3451 logf (LOG_DEBUG, "cs_rcvconnect error");
3452 ir_select_remove_write (cs_fileno (p->cs_link), p);
3455 p->failInfo = IR_TCL_FAIL_CONNECT;
3456 IrTcl_eval (p->interp, p->failback);
3458 do_disconnect (p, NULL, 2, NULL);
3461 ir_select_remove_write (cs_fileno (p->cs_link), p);
3463 IrTcl_eval (p->interp, p->callback);
3466 rq = p->request_queue;
3467 if (!rq || !rq->buf_out)
3470 if ((r=cs_put (p->cs_link, rq->buf_out, rq->len_out)) < 0)
3472 logf (LOG_DEBUG, "select write fail");
3475 p->failInfo = IR_TCL_FAIL_WRITE;
3476 IrTcl_eval (p->interp, p->failback);
3480 do_disconnect (p, NULL, 2, NULL);
3482 else if (r == 0) /* remove select bit */
3484 p->state = IR_TCL_R_Waiting;
3485 ir_select_remove_write (cs_fileno (p->cs_link), p);
3491 /* ------------------------------------------------------- */
3494 * Irtcl_init: Registration of TCL commands.
3496 int Irtcl_Init (Tcl_Interp *interp)
3498 Tcl_CreateCommand (interp, "ir", ir_obj_mk, (ClientData) NULL,
3499 (Tcl_CmdDeleteProc *) NULL);
3500 Tcl_CreateCommand (interp, "ir-set", ir_set_obj_mk,
3501 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
3502 Tcl_CreateCommand (interp, "ir-scan", ir_scan_obj_mk,
3503 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);