2 * IR toolkit for tcl/tk
4 * See the file LICENSE for details.
5 * Sebastian Hammer, Adam Dickmeiss
8 * Revision 1.75 1996-02-19 15:41:53 adam
10 * Minor improvement of connect method.
12 * Revision 1.74 1996/02/05 17:58:03 adam
13 * Ported ir-tcl to use the beta releases of tcl7.5/tk4.1.
15 * Revision 1.73 1996/01/29 11:35:19 adam
16 * Bug fix: cs_type member renamed to comstackType to avoid conflict with
17 * cs_type macro defined by YAZ.
19 * Revision 1.72 1996/01/19 17:45:34 quinn
20 * Added debugging output
22 * Revision 1.71 1996/01/19 16:22:38 adam
23 * New method: apduDump - returns information about last incoming APDU.
25 * Revision 1.70 1996/01/10 09:18:34 adam
26 * PDU specific callbacks implemented: initRespnse, searchResponse,
27 * presentResponse and scanResponse.
28 * Bug fix in the command line shell (tclmain.c) - discovered on OSF/1.
30 * Revision 1.69 1996/01/04 16:12:12 adam
31 * Setting PDUType renamed to eventType.
33 * Revision 1.68 1996/01/04 11:05:22 adam
34 * New setting: PDUType - returns type of last PDU returned from the target.
35 * Fixed a bug in configure/Makefile.
37 * Revision 1.67 1996/01/03 09:00:51 adam
38 * Updated to use new version of Yaz (names changed to avoid C++ conflict).
40 * Revision 1.66 1995/11/28 17:26:39 adam
41 * Removed Carriage return from ir-tcl.c!
42 * Removed misc. debug logs.
44 * Revision 1.65 1995/11/28 13:53:00 quinn
47 * Revision 1.64 1995/11/13 15:39:18 adam
48 * Bug fix: {small,medium}SetElementSetNames weren't set correctly.
49 * Bug fix: idAuthentication weren't set correctly.
51 * Revision 1.63 1995/11/13 09:55:39 adam
52 * Multiple records at a position in a result-set with differnt
55 * Revision 1.62 1995/10/18 17:20:33 adam
56 * Work on target setup in client.tcl.
58 * Revision 1.61 1995/10/18 16:42:42 adam
59 * New settings: smallSetElementSetNames and mediumSetElementSetNames.
61 * Revision 1.60 1995/10/18 15:43:31 adam
62 * In search: mediumSetElementSetNames and smallSetElementSetNames are
63 * set to elementSetNames.
65 * Revision 1.59 1995/10/17 12:18:58 adam
66 * Bug fix: when target connection closed, the connection was not
67 * properly reestablished.
69 * Revision 1.58 1995/10/16 17:00:55 adam
70 * New setting: elementSetNames.
71 * Various client improvements. Medium presentation format looks better.
73 * Revision 1.57 1995/09/21 13:11:51 adam
74 * Support of dynamic loading.
75 * Test script uses load command if necessary.
77 * Revision 1.56 1995/08/29 15:30:14 adam
78 * Work on GRS records.
80 * Revision 1.55 1995/08/28 09:43:25 adam
81 * Minor changes. configure only searches for yaz beta 3 and versions after
84 * Revision 1.54 1995/08/24 12:25:16 adam
85 * Modified to work with yaz 1.0b3.
87 * Revision 1.53 1995/08/04 12:49:26 adam
88 * Bug fix: reading uninitialized variable p.
90 * Revision 1.52 1995/08/04 11:32:38 adam
91 * More work on output queue. Memory related routines moved
94 * Revision 1.51 1995/08/03 13:22:54 adam
97 * Revision 1.50 1995/07/20 08:09:49 adam
98 * client.tcl: Targets removed from hotTargets list when targets
99 * are removed/modified.
100 * ir-tcl.c: More work on triggerResourceControl.
102 * Revision 1.49 1995/06/30 12:39:21 adam
103 * Bug fix: loadFile didn't set record type.
104 * The MARC routines are a little less strict in the interpretation.
105 * Script display.tcl replaces the old marc.tcl.
106 * New interactive script: shell.tcl.
108 * Revision 1.48 1995/06/27 19:03:50 adam
109 * Bug fix in do_present in ir-tcl.c: p->set_child member weren't set.
110 * nextResultSetPosition used instead of setOffset.
112 * Revision 1.47 1995/06/25 10:25:04 adam
113 * Working on triggerResourceControl. Description of compile/install
114 * procedure moved to ir-tcl.sgml.
116 * Revision 1.46 1995/06/22 13:15:06 adam
117 * Feature: SUTRS. Setting getSutrs implemented.
118 * Work on display formats.
119 * Preferred record syntax can be set by the user.
121 * Revision 1.45 1995/06/20 08:07:30 adam
122 * New setting: failInfo.
123 * Working on better cancel mechanism.
125 * Revision 1.44 1995/06/19 17:01:20 adam
128 * Revision 1.43 1995/06/19 13:06:08 adam
129 * New define: IR_TCL_VERSION.
131 * Revision 1.42 1995/06/19 08:08:52 adam
132 * client.tcl: hotTargets now contain both database and target name.
133 * ir-tcl.c: setting protocol edited. Errors in callbacks are logged
134 * by logf(LOG_WARN, ...) calls.
136 * Revision 1.41 1995/06/16 12:28:16 adam
137 * Implemented preferredRecordSyntax.
138 * Minor changes in diagnostic handling.
139 * Record list deleted when connection closes.
141 * Revision 1.40 1995/06/14 13:37:18 adam
142 * Setting recordType implemented.
143 * Setting implementationVersion implemented.
144 * Settings implementationId / implementationName edited.
146 * Revision 1.39 1995/06/08 10:26:32 adam
147 * Bug fix in ir_strdup.
149 * Revision 1.38 1995/06/01 16:36:47 adam
150 * About buttons. Minor bug fixes.
152 * Revision 1.37 1995/06/01 07:31:20 adam
153 * Rename of many typedefs -> IrTcl_...
155 * Revision 1.36 1995/05/31 13:09:59 adam
156 * Client searches/presents may be interrupted.
157 * New moving book-logo.
159 * Revision 1.35 1995/05/31 08:36:33 adam
160 * Bug fix in client.tcl: didn't save options on clientrc.tcl.
161 * New method: referenceId. More work on scan.
163 * Revision 1.34 1995/05/29 10:33:42 adam
164 * README and rename of startup script.
166 * Revision 1.33 1995/05/29 09:15:11 quinn
167 * Changed CS_SR to PROTO_SR, etc.
169 * Revision 1.32 1995/05/29 08:44:16 adam
170 * Work on delete of objects.
172 * Revision 1.31 1995/05/26 11:44:10 adam
173 * Bugs fixed. More work on MARC utilities and queries. Test
174 * client is up-to-date again.
176 * Revision 1.30 1995/05/26 08:54:11 adam
177 * New MARC utilities. Uses prefix query.
179 * Revision 1.29 1995/05/24 14:10:22 adam
180 * Work on idAuthentication, protocolVersion and options.
182 * Revision 1.28 1995/05/23 15:34:48 adam
183 * Many new settings, userInformationField, smallSetUpperBound, etc.
184 * A number of settings are inherited when ir-set is executed.
185 * This version is incompatible with the graphical test client (client.tcl).
187 * Revision 1.27 1995/05/11 15:34:47 adam
188 * Scan request changed a bit. This version works with RLG.
190 * Revision 1.26 1995/04/18 16:11:51 adam
191 * First version of graphical Scan. Some work on query-by-form.
193 * Revision 1.25 1995/04/17 09:37:17 adam
194 * Further development of scan.
196 * Revision 1.24 1995/04/11 14:16:42 adam
197 * Further work on scan. Response works. Entries aren't saved yet.
199 * Revision 1.23 1995/04/10 10:50:27 adam
200 * Result-set name defaults to suffix of ir-set name.
201 * Started working on scan. Not finished at this point.
203 * Revision 1.22 1995/03/31 10:43:03 adam
204 * More robust when getting bad MARC records.
206 * Revision 1.21 1995/03/31 08:56:37 adam
207 * New button "Search".
209 * Revision 1.20 1995/03/29 16:07:09 adam
210 * Bug fix: Didn't use setName in present request.
212 * Revision 1.19 1995/03/28 12:45:23 adam
213 * New ir method failback: called on disconnect/protocol error.
214 * New ir set/get method: protocol: SR / Z3950.
215 * Simple popup and disconnect when failback is invoked.
217 * Revision 1.18 1995/03/21 15:50:12 adam
220 * Revision 1.17 1995/03/21 13:41:03 adam
221 * Comstack cs_create not used too often. Non-blocking connect.
223 * Revision 1.16 1995/03/21 08:26:06 adam
224 * New method, setName, to specify the result set name (other than Default).
225 * New method, responseStatus, which returns diagnostic info, if any, after
226 * present response / search response.
228 * Revision 1.15 1995/03/20 15:24:07 adam
229 * Diagnostic records saved on searchResponse.
231 * Revision 1.14 1995/03/20 08:53:22 adam
232 * Event loop in tclmain.c rewritten. New method searchStatus.
234 * Revision 1.13 1995/03/17 18:26:17 adam
235 * Non-blocking i/o used now. Database names popup as cascade items.
237 * Revision 1.12 1995/03/17 15:45:00 adam
238 * Improved target/database setup.
240 * Revision 1.11 1995/03/16 17:54:03 adam
241 * Minor changes really.
243 * Revision 1.10 1995/03/15 16:14:50 adam
244 * Blocking arg in cs_create changed.
246 * Revision 1.9 1995/03/15 13:59:24 adam
249 * Revision 1.8 1995/03/15 08:25:16 adam
250 * New method presentStatus to check for error on present. Misc. cleanup
251 * of IrTcl_RecordList manipulations. Full MARC record presentation in
254 * Revision 1.7 1995/03/14 17:32:29 adam
255 * Presentation of full Marc record in popup window.
257 * Revision 1.6 1995/03/12 19:31:55 adam
258 * Pattern matching implemented when retrieving MARC records. More
259 * diagnostic functions.
261 * Revision 1.5 1995/03/10 18:00:15 adam
262 * Actual presentation in line-by-line format. RPN query support.
264 * Revision 1.4 1995/03/09 16:15:08 adam
265 * First presentRequest attempts. Hot-target list.
282 int (*method) (void *obj, Tcl_Interp *interp, int argc, char **argv);
290 static void ir_deleteDiags (IrTcl_Diagnostic **dst_list, int *dst_num);
291 static int do_disconnect (void *obj, Tcl_Interp *interp,
292 int argc, char **argv);
294 static IrTcl_RecordList *new_IR_record (IrTcl_SetObj *setobj,
296 const char *elements)
298 IrTcl_RecordList *rl;
300 for (rl = setobj->record_list; rl; rl = rl->next)
302 if (no == rl->no && (!rl->elements || !elements ||
303 !strcmp(elements, rl->elements)))
308 case Z_NamePlusRecord_databaseRecord:
309 free (rl->u.dbrec.buf);
310 rl->u.dbrec.buf = NULL;
312 case Z_NamePlusRecord_surrogateDiagnostic:
313 ir_deleteDiags (&rl->u.surrogateDiagnostics.list,
314 &rl->u.surrogateDiagnostics.num);
322 rl = ir_tcl_malloc (sizeof(*rl));
323 rl->next = setobj->record_list;
325 setobj->record_list = rl;
328 ir_tcl_strdup (NULL, &rl->elements, elements);
333 enum oid_value value;
335 } IrTcl_recordSyntaxTab[] = {
336 { VAL_UNIMARC, "UNIMARC" },
337 { VAL_INTERMARC, "INTERMARC" },
339 { VAL_USMARC, "USMARC" },
340 { VAL_UKMARC, "UKMARC" },
341 { VAL_NORMARC, "NORMARC" },
342 { VAL_LIBRISMARC, "LIBRISMARC" },
343 { VAL_DANMARC, "DANMARC" },
344 { VAL_FINMARC, "FINMARC" },
346 { VAL_CANMARC, "CANMARC" },
348 { VAL_PICAMARC, "PICAMARC" },
349 { VAL_AUSMARC, "AUSMARC" },
350 { VAL_IBERMARC, "IBERMARC" },
351 { VAL_SUTRS, "SUTRS" },
352 { VAL_GRS1, "GRS1" },
359 int IrTcl_eval (Tcl_Interp *interp, const char *command)
361 char *tmp = ir_tcl_malloc (strlen(command)+1);
364 strcpy (tmp, command);
365 r = Tcl_Eval (interp, tmp);
367 logf (LOG_WARN, "Tcl error in line %d: %s", interp->errorLine,
369 Tcl_FreeResult (interp);
375 * IrTcl_getRecordSyntaxStr: Return record syntax name of object id
377 static const char *IrTcl_getRecordSyntaxStr (enum oid_value value)
380 for (i = 0; IrTcl_recordSyntaxTab[i].name; i++)
381 if (IrTcl_recordSyntaxTab[i].value == value)
382 return IrTcl_recordSyntaxTab[i].name;
387 * IrTcl_getRecordSyntaxVal: Return record syntax value of string
389 static enum oid_value IrTcl_getRecordSyntaxVal (const char *name)
392 for (i = 0; IrTcl_recordSyntaxTab[i].name; i++)
393 if (!strcmp (IrTcl_recordSyntaxTab[i].name, name))
394 return IrTcl_recordSyntaxTab[i].value;
398 static IrTcl_RecordList *find_IR_record (IrTcl_SetObj *setobj, int no)
400 IrTcl_RecordList *rl;
402 for (rl = setobj->record_list; rl; rl = rl->next)
404 (!setobj->recordElements || !rl->elements ||
405 !strcmp (setobj->recordElements, rl->elements)))
410 static void delete_IR_records (IrTcl_SetObj *setobj)
412 IrTcl_RecordList *rl, *rl1;
414 for (rl = setobj->record_list; rl; rl = rl1)
418 case Z_NamePlusRecord_databaseRecord:
419 free (rl->u.dbrec.buf);
421 case Z_NamePlusRecord_surrogateDiagnostic:
422 ir_deleteDiags (&rl->u.surrogateDiagnostics.list,
423 &rl->u.surrogateDiagnostics.num);
429 setobj->record_list = NULL;
433 * get_set_int: Set/get integer value
435 static int get_set_int (int *val, Tcl_Interp *interp, int argc, char **argv)
441 if (Tcl_GetInt (interp, argv[2], val)==TCL_ERROR)
444 sprintf (buf, "%d", *val);
445 Tcl_AppendResult (interp, buf, NULL);
450 * ir_method: Search for method in table and invoke method handler
452 int ir_method (Tcl_Interp *interp, int argc, char **argv, IrTcl_Methods *tab)
454 IrTcl_Methods *tab_i = tab;
457 for (tab_i = tab; tab_i->tab; tab_i++)
458 for (t = tab_i->tab; t->name; t++)
461 if ((*t->method)(tab_i->obj, interp, argc, argv) == TCL_ERROR)
465 if (!strcmp (t->name, argv[1]))
466 return (*t->method)(tab_i->obj, interp, argc, argv);
470 Tcl_AppendResult (interp, "Bad method: ", argv[1],
471 ". Possible methods:", NULL);
472 for (tab_i = tab; tab_i->tab; tab_i++)
473 for (t = tab_i->tab; t->name; t++)
474 Tcl_AppendResult (interp, " ", t->name, NULL);
479 * ir_method_r: Get status for all readable elements
481 int ir_method_r (void *obj, Tcl_Interp *interp, int argc, char **argv,
489 for (; tab->name; tab++)
492 argv_n[1] = tab->name;
493 Tcl_AppendResult (interp, "{", NULL);
494 (*tab->method)(obj, interp, argc_n, argv_n);
495 Tcl_AppendResult (interp, "} ", NULL);
501 * ir_named_bits: get/set named bits
503 int ir_named_bits (struct ir_named_entry *tab, Odr_bitmask *ob,
504 Tcl_Interp *interp, int argc, char **argv)
506 struct ir_named_entry *ti;
511 for (no = 0; no < argc; no++)
513 for (ti = tab; ti->name; ti++)
514 if (!strcmp (argv[no], ti->name))
516 ODR_MASK_SET (ob, ti->pos);
521 Tcl_AppendResult (interp, "Bad bit mask: ", argv[no], NULL);
527 for (ti = tab; ti->name; ti++)
528 if (ODR_MASK_GET (ob, ti->pos))
529 Tcl_AppendElement (interp, ti->name);
533 static void set_referenceId (ODR o, Z_ReferenceId **dst, const char *src)
539 *dst = odr_malloc (o, sizeof(**dst));
540 (*dst)->size = (*dst)->len = strlen(src);
541 (*dst)->buf = odr_malloc (o, (*dst)->len);
542 memcpy ((*dst)->buf, src, (*dst)->len);
546 static void get_referenceId (char **dst, Z_ReferenceId *src)
554 *dst = ir_tcl_malloc (src->len+1);
555 memcpy (*dst, src->buf, src->len);
556 (*dst)[src->len] = '\0';
559 /* ------------------------------------------------------- */
562 * do_init_request: init method on IR object
564 static int do_init_request (void *obj, Tcl_Interp *interp,
565 int argc, char **argv)
575 interp->result = "init: not connected";
578 apdu = zget_APDU (p->odr_out, Z_APDU_initRequest);
579 req = apdu->u.initRequest;
581 set_referenceId (p->odr_out, &req->referenceId, p->set_inher.referenceId);
582 req->options = &p->options;
583 req->protocolVersion = &p->protocolVersion;
584 req->preferredMessageSize = &p->preferredMessageSize;
585 req->maximumRecordSize = &p->maximumRecordSize;
587 if (p->idAuthenticationGroupId)
589 Z_IdPass *pass = odr_malloc (p->odr_out, sizeof(*pass));
590 Z_IdAuthentication *auth = odr_malloc (p->odr_out, sizeof(*auth));
592 logf (LOG_DEBUG, "using pass authentication");
594 auth->which = Z_IdAuthentication_idPass;
595 auth->u.idPass = pass;
596 if (p->idAuthenticationGroupId && *p->idAuthenticationGroupId)
597 pass->groupId = p->idAuthenticationGroupId;
599 pass->groupId = NULL;
600 if (p->idAuthenticationUserId && *p->idAuthenticationUserId)
601 pass->userId = p->idAuthenticationUserId;
604 if (p->idAuthenticationPassword && *p->idAuthenticationPassword)
605 pass->password = p->idAuthenticationPassword;
607 pass->password = NULL;
608 req->idAuthentication = auth;
610 else if (!p->idAuthenticationOpen || !*p->idAuthenticationOpen)
611 req->idAuthentication = NULL;
614 Z_IdAuthentication *auth = odr_malloc (p->odr_out, sizeof(*auth));
616 logf (LOG_DEBUG, "using open authentication");
617 auth->which = Z_IdAuthentication_open;
618 auth->u.open = p->idAuthenticationOpen;
619 req->idAuthentication = auth;
621 req->implementationId = p->implementationId;
622 req->implementationName = p->implementationName;
623 req->implementationVersion = p->implementationVersion;
624 req->userInformationField = 0;
626 return ir_tcl_send_APDU (interp, p, apdu, "init", *argv);
630 * do_protocolVersion: Set protocol Version
632 static int do_protocolVersion (void *obj, Tcl_Interp *interp,
633 int argc, char **argv)
641 ODR_MASK_ZERO (&p->protocolVersion);
642 ODR_MASK_SET (&p->protocolVersion, 0);
643 ODR_MASK_SET (&p->protocolVersion, 1);
648 if (Tcl_GetInt (interp, argv[2], &version)==TCL_ERROR)
650 ODR_MASK_ZERO (&p->protocolVersion);
651 for (i = 0; i<version; i++)
652 ODR_MASK_SET (&p->protocolVersion, i);
654 for (i = 4; --i >= 0; )
655 if (ODR_MASK_GET (&p->protocolVersion, i))
657 sprintf (buf, "%d", i+1);
658 interp->result = buf;
663 * do_options: Set options
665 static int do_options (void *obj, Tcl_Interp *interp,
666 int argc, char **argv)
668 static struct ir_named_entry options_tab[] = {
672 { "resourceReport", 3 },
673 { "triggerResourceCtrl", 4},
674 { "resourceCtrl", 5},
678 { "extendedServices", 10},
679 { "level-1Segmentation", 11},
680 { "level-2Segmentation", 12},
681 { "concurrentOperations", 13},
682 { "namedResultSets", 14},
689 ODR_MASK_ZERO (&p->options);
690 ODR_MASK_SET (&p->options, 0);
691 ODR_MASK_SET (&p->options, 1);
692 ODR_MASK_SET (&p->options, 4);
693 ODR_MASK_SET (&p->options, 7);
694 ODR_MASK_SET (&p->options, 14);
697 return ir_named_bits (options_tab, &p->options, interp, argc-2, argv+2);
701 * do_apduInfo: Get APDU information
703 static int do_apduInfo (void *obj, Tcl_Interp *interp, int argc, char **argv)
711 sprintf (buf, "%d", p->apduLen);
712 Tcl_AppendElement (interp, buf);
713 sprintf (buf, "%d", p->apduOffset);
714 Tcl_AppendElement (interp, buf);
717 Tcl_AppendElement (interp, "");
720 apduf = fopen ("apdu.tmp", "w");
723 Tcl_AppendElement (interp, "");
726 odr_dumpBER (apduf, p->buf_in, p->apduLen);
728 if (!(apduf = fopen ("apdu.tmp", "r")))
729 Tcl_AppendElement (interp, "");
734 Tcl_AppendResult (interp, " {", NULL);
735 while ((c = getc (apduf)) != EOF)
739 Tcl_AppendResult (interp, buf, NULL);
742 Tcl_AppendResult (interp, "}", NULL);
749 * do_failInfo: Get fail information
751 static int do_failInfo (void *obj, Tcl_Interp *interp, int argc, char **argv)
761 sprintf (buf, "%d", p->failInfo);
767 case IR_TCL_FAIL_CONNECT:
768 cp = "connect failed";
770 case IR_TCL_FAIL_READ:
771 cp = "connection closed";
773 case IR_TCL_FAIL_WRITE:
774 cp = "connection closed";
776 case IR_TCL_FAIL_IN_APDU:
777 cp = "failed to decode incoming APDU";
779 case IR_TCL_FAIL_UNKNOWN_APDU:
785 Tcl_AppendElement (interp, buf);
786 Tcl_AppendElement (interp, cp);
791 * do_preferredMessageSize: Set/get preferred message size
793 static int do_preferredMessageSize (void *obj, Tcl_Interp *interp,
794 int argc, char **argv)
800 p->preferredMessageSize = 30000;
803 return get_set_int (&p->preferredMessageSize, interp, argc, argv);
807 * do_maximumRecordSize: Set/get maximum record size
809 static int do_maximumRecordSize (void *obj, Tcl_Interp *interp,
810 int argc, char **argv)
816 p->maximumRecordSize = 30000;
819 return get_set_int (&p->maximumRecordSize, interp, argc, argv);
823 * do_initResult: Get init result
825 static int do_initResult (void *obj, Tcl_Interp *interp,
826 int argc, char **argv)
832 return get_set_int (&p->initResult, interp, argc, argv);
837 * do_implementationName: Set/get Implementation Name.
839 static int do_implementationName (void *obj, Tcl_Interp *interp,
840 int argc, char **argv)
845 return ir_tcl_strdup (interp, &p->implementationName,
846 "Index Data/IrTcl on YAZ");
848 return ir_tcl_strdel (interp, &p->implementationName);
851 free (p->implementationName);
852 if (ir_tcl_strdup (interp, &p->implementationName, argv[2])
856 Tcl_AppendResult (interp, p->implementationName, (char*) NULL);
861 * do_implementationId: Get Implementation Id.
863 static int do_implementationId (void *obj, Tcl_Interp *interp,
864 int argc, char **argv)
869 return ir_tcl_strdup (interp, &p->implementationId, "YAZ (id=81)");
871 return ir_tcl_strdel (interp, &p->implementationId);
872 Tcl_AppendResult (interp, p->implementationId, (char*) NULL);
877 * do_implementationVersion: get Implementation Version.
879 static int do_implementationVersion (void *obj, Tcl_Interp *interp,
880 int argc, char **argv)
885 return ir_tcl_strdup (interp, &p->implementationVersion,
886 "YAZ: " YAZ_VERSION " / IrTcl: " IR_TCL_VERSION);
888 return ir_tcl_strdel (interp, &p->implementationVersion);
889 Tcl_AppendResult (interp, p->implementationVersion, (char*) NULL);
894 * do_targetImplementationName: Get Implementation Name of target.
896 static int do_targetImplementationName (void *obj, Tcl_Interp *interp,
897 int argc, char **argv)
903 p->targetImplementationName = NULL;
907 return ir_tcl_strdel (interp, &p->targetImplementationName);
908 Tcl_AppendResult (interp, p->targetImplementationName, (char*) NULL);
913 * do_targetImplementationId: Get Implementation Id of target
915 static int do_targetImplementationId (void *obj, Tcl_Interp *interp,
916 int argc, char **argv)
922 p->targetImplementationId = NULL;
926 return ir_tcl_strdel (interp, &p->targetImplementationId);
927 Tcl_AppendResult (interp, p->targetImplementationId, (char*) NULL);
932 * do_targetImplementationVersion: Get Implementation Version of target
934 static int do_targetImplementationVersion (void *obj, Tcl_Interp *interp,
935 int argc, char **argv)
941 p->targetImplementationVersion = NULL;
945 return ir_tcl_strdel (interp, &p->targetImplementationVersion);
946 Tcl_AppendResult (interp, p->targetImplementationVersion, (char*) NULL);
951 * do_idAuthentication: Set/get id Authentication
953 static int do_idAuthentication (void *obj, Tcl_Interp *interp,
954 int argc, char **argv)
958 if (argc >= 3 || argc == -1)
960 free (p->idAuthenticationOpen);
961 free (p->idAuthenticationGroupId);
962 free (p->idAuthenticationUserId);
963 free (p->idAuthenticationPassword);
965 if (argc >= 3 || argc <= 0)
967 p->idAuthenticationOpen = NULL;
968 p->idAuthenticationGroupId = NULL;
969 p->idAuthenticationUserId = NULL;
970 p->idAuthenticationPassword = NULL;
979 ir_tcl_strdup (interp, &p->idAuthenticationOpen, argv[2])
986 ir_tcl_strdup (interp, &p->idAuthenticationGroupId, argv[2])
990 ir_tcl_strdup (interp, &p->idAuthenticationUserId, argv[3])
994 ir_tcl_strdup (interp, &p->idAuthenticationPassword, argv[4])
999 if (p->idAuthenticationOpen)
1000 Tcl_AppendElement (interp, p->idAuthenticationOpen);
1001 else if (p->idAuthenticationGroupId)
1003 Tcl_AppendElement (interp, p->idAuthenticationGroupId);
1004 Tcl_AppendElement (interp, p->idAuthenticationUserId);
1005 Tcl_AppendElement (interp, p->idAuthenticationPassword);
1011 * do_connect: connect method on IR object
1013 static int do_connect (void *obj, Tcl_Interp *interp,
1014 int argc, char **argv)
1026 interp->result = "already connected";
1029 if (!strcmp (p->comstackType, "tcpip"))
1031 p->cs_link = cs_create (tcpip_type, CS_BLOCK, p->protocol_type);
1032 addr = tcpip_strtoaddr (argv[2]);
1035 interp->result = "tcpip_strtoaddr fail";
1038 logf (LOG_DEBUG, "tcp/ip connect %s", argv[2]);
1040 else if (!strcmp (p->comstackType, "mosi"))
1043 p->cs_link = cs_create (mosi_type, CS_BLOCK, p->protocol_type);
1044 addr = mosi_strtoaddr (argv[2]);
1047 interp->result = "mosi_strtoaddr fail";
1050 logf (LOG_DEBUG, "mosi connect %s", argv[2]);
1052 interp->result = "MOSI support not there";
1058 Tcl_AppendResult (interp, "Bad comstack type: ",
1059 p->comstackType, NULL);
1062 if (ir_tcl_strdup (interp, &p->hostname, argv[2]) == TCL_ERROR)
1064 #if IRTCL_GENERIC_FILES
1066 p->csFile = Tcl_GetFile (cs_fileno(p->cs_link), TCL_WIN_SOCKET);
1068 p->csFile = Tcl_GetFile (cs_fileno(p->cs_link), TCL_UNIX_FD);
1071 if ((r=cs_connect (p->cs_link, addr)) < 0)
1073 interp->result = "connect fail";
1074 do_disconnect (p, NULL, 2, NULL);
1077 logf(LOG_DEBUG, "cs_connect() returned %d fd=%d", r,
1078 cs_fileno(p->cs_link));
1079 p->eventType = "connect";
1080 #if IRTCL_GENERIC_FILES
1081 ir_select_add (p->csFile, p);
1083 ir_select_add (cs_fileno (p->cs_link), p);
1087 #if IRTCL_GENERIC_FILES
1088 ir_select_add_write (p->csFile, p);
1090 ir_select_add_write (cs_fileno (p->cs_link), p);
1092 p->state = IR_TCL_R_Connecting;
1096 p->state = IR_TCL_R_Idle;
1098 IrTcl_eval (p->interp, p->callback);
1102 Tcl_AppendResult (interp, p->hostname, NULL);
1107 * do_disconnect: disconnect method on IR object
1109 static int do_disconnect (void *obj, Tcl_Interp *interp,
1110 int argc, char **argv)
1116 p->state = IR_TCL_R_Idle;
1117 p->eventType = NULL;
1120 #if IRTCL_GENERIC_FILES
1127 logf(LOG_DEBUG, "Closing connection to %s", p->hostname);
1130 #if IRTCL_GENERIC_FILES
1131 ir_select_remove_write (p->csFile, p);
1132 ir_select_remove (p->csFile, p);
1134 ir_select_remove_write (cs_fileno (p->cs_link), p);
1135 ir_select_remove (cs_fileno (p->cs_link), p);
1138 odr_reset (p->odr_in);
1140 assert (p->cs_link);
1141 cs_close (p->cs_link);
1143 #if IRTCL_GENERIC_FILES
1144 Tcl_FreeFile (p->csFile);
1148 ODR_MASK_ZERO (&p->options);
1149 ODR_MASK_SET (&p->options, 0);
1150 ODR_MASK_SET (&p->options, 1);
1151 ODR_MASK_SET (&p->options, 4);
1152 ODR_MASK_SET (&p->options, 7);
1153 ODR_MASK_SET (&p->options, 14);
1155 ODR_MASK_ZERO (&p->protocolVersion);
1156 ODR_MASK_SET (&p->protocolVersion, 0);
1157 ODR_MASK_SET (&p->protocolVersion, 1);
1160 assert (!p->cs_link);
1165 * do_comstack: Set/get comstack method on IR object
1167 static int do_comstack (void *o, Tcl_Interp *interp,
1168 int argc, char **argv)
1173 return ir_tcl_strdup (interp, &obj->comstackType, "tcpip");
1174 else if (argc == -1)
1175 return ir_tcl_strdel (interp, &obj->comstackType);
1178 free (obj->comstackType);
1179 if (ir_tcl_strdup (interp, &obj->comstackType, argv[2]) == TCL_ERROR)
1182 Tcl_AppendElement (interp, obj->comstackType);
1187 * do_logLevel: Set log level
1189 static int do_logLevel (void *o, Tcl_Interp *interp,
1190 int argc, char **argv)
1195 log_init (log_mask_str (argv[2]), "", NULL);
1197 log_init (log_mask_str (argv[2]), argv[3], NULL);
1199 log_init (log_mask_str (argv[2]), argv[3], argv[4]);
1205 * do_eventType: Return type of last event
1207 static int do_eventType (void *obj, Tcl_Interp *interp,
1208 int argc, char **argv)
1214 p->eventType = NULL;
1217 Tcl_AppendElement (interp, p->eventType ? p->eventType : "");
1223 * do_callback: add callback
1225 static int do_callback (void *obj, Tcl_Interp *interp,
1226 int argc, char **argv)
1235 else if (argc == -1)
1236 return ir_tcl_strdel (interp, &p->callback);
1242 if (ir_tcl_strdup (interp, &p->callback, argv[2]) == TCL_ERROR)
1252 * do_failback: add error handle callback
1254 static int do_failback (void *obj, Tcl_Interp *interp,
1255 int argc, char **argv)
1264 else if (argc == -1)
1265 return ir_tcl_strdel (interp, &p->failback);
1271 if (ir_tcl_strdup (interp, &p->failback, argv[2]) == TCL_ERROR)
1281 * do_initResponse: add init response handler
1283 static int do_initResponse (void *obj, Tcl_Interp *interp,
1284 int argc, char **argv)
1290 p->initResponse = NULL;
1293 else if (argc == -1)
1294 return ir_tcl_strdel (interp, &p->initResponse);
1297 free (p->initResponse);
1300 if (ir_tcl_strdup (interp, &p->initResponse, argv[2]) == TCL_ERROR)
1304 p->initResponse = NULL;
1309 * do_protocol: Set/get protocol method on IR object
1311 static int do_protocol (void *o, Tcl_Interp *interp, int argc, char **argv)
1317 p->protocol_type = PROTO_Z3950;
1322 if (!strcmp (argv[2], "Z39"))
1323 p->protocol_type = PROTO_Z3950;
1324 else if (!strcmp (argv[2], "SR"))
1325 p->protocol_type = PROTO_SR;
1328 Tcl_AppendResult (interp, "Bad protocol: ", argv[2], NULL);
1333 switch (p->protocol_type)
1336 Tcl_AppendElement (interp, "Z39");
1339 Tcl_AppendElement (interp, "SR");
1346 * do_triggerResourceControl:
1348 static int do_triggerResourceControl (void *obj, Tcl_Interp *interp,
1349 int argc, char **argv)
1353 Z_TriggerResourceControlRequest *req;
1354 bool_t is_false = 0;
1360 interp->result = "triggerResourceControl: not connected";
1363 apdu = zget_APDU (p->odr_out, Z_APDU_triggerResourceControlRequest);
1364 req = apdu->u.triggerResourceControlRequest;
1365 *req->requestedAction = Z_TriggerResourceCtrl_cancel;
1366 req->resultSetWanted = &is_false;
1368 return ir_tcl_send_APDU (interp, p, apdu, "triggerResourceControl",
1373 * do_databaseNames: specify database names
1375 static int do_databaseNames (void *obj, Tcl_Interp *interp,
1376 int argc, char **argv)
1379 IrTcl_SetCObj *p = obj;
1383 for (i=0; i<p->num_databaseNames; i++)
1384 free (p->databaseNames[i]);
1385 free (p->databaseNames);
1389 p->num_databaseNames = 0;
1390 p->databaseNames = NULL;
1395 for (i=0; i<p->num_databaseNames; i++)
1396 Tcl_AppendElement (interp, p->databaseNames[i]);
1399 if (p->databaseNames)
1401 for (i=0; i<p->num_databaseNames; i++)
1402 free (p->databaseNames[i]);
1403 free (p->databaseNames);
1405 p->num_databaseNames = argc - 2;
1407 ir_tcl_malloc (sizeof(*p->databaseNames) * p->num_databaseNames);
1408 for (i=0; i<p->num_databaseNames; i++)
1410 if (ir_tcl_strdup (interp, &p->databaseNames[i], argv[2+i])
1418 * do_replaceIndicator: Set/get replace Set indicator
1420 static int do_replaceIndicator (void *obj, Tcl_Interp *interp,
1421 int argc, char **argv)
1423 IrTcl_SetCObj *p = obj;
1427 p->replaceIndicator = 1;
1430 return get_set_int (&p->replaceIndicator, interp, argc, argv);
1434 * do_queryType: Set/Get query method
1436 static int do_queryType (void *obj, Tcl_Interp *interp,
1437 int argc, char **argv)
1439 IrTcl_SetCObj *p = obj;
1442 return ir_tcl_strdup (interp, &p->queryType, "rpn");
1443 else if (argc == -1)
1444 return ir_tcl_strdel (interp, &p->queryType);
1447 free (p->queryType);
1448 if (ir_tcl_strdup (interp, &p->queryType, argv[2]) == TCL_ERROR)
1451 Tcl_AppendResult (interp, p->queryType, NULL);
1456 * do_userInformationField: Get User information field
1458 static int do_userInformationField (void *obj, Tcl_Interp *interp,
1459 int argc, char **argv)
1465 p->userInformationField = NULL;
1468 else if (argc == -1)
1469 return ir_tcl_strdel (interp, &p->userInformationField);
1470 Tcl_AppendResult (interp, p->userInformationField, NULL);
1475 * do_smallSetUpperBound: Set/get small set upper bound
1477 static int do_smallSetUpperBound (void *o, Tcl_Interp *interp,
1478 int argc, char **argv)
1480 IrTcl_SetCObj *p = o;
1484 p->smallSetUpperBound = 0;
1487 return get_set_int (&p->smallSetUpperBound, interp, argc, argv);
1491 * do_largeSetLowerBound: Set/get large set lower bound
1493 static int do_largeSetLowerBound (void *o, Tcl_Interp *interp,
1494 int argc, char **argv)
1496 IrTcl_SetCObj *p = o;
1500 p->largeSetLowerBound = 2;
1503 return get_set_int (&p->largeSetLowerBound, interp, argc, argv);
1507 * do_mediumSetPresentNumber: Set/get large set lower bound
1509 static int do_mediumSetPresentNumber (void *o, Tcl_Interp *interp,
1510 int argc, char **argv)
1512 IrTcl_SetCObj *p = o;
1516 p->mediumSetPresentNumber = 0;
1519 return get_set_int (&p->mediumSetPresentNumber, interp, argc, argv);
1523 * do_referenceId: Set/Get referenceId
1525 static int do_referenceId (void *obj, Tcl_Interp *interp,
1526 int argc, char **argv)
1528 IrTcl_SetCObj *p = obj;
1532 p->referenceId = NULL;
1535 else if (argc == -1)
1536 return ir_tcl_strdel (interp, &p->referenceId);
1539 free (p->referenceId);
1540 if (ir_tcl_strdup (interp, &p->referenceId, argv[2]) == TCL_ERROR)
1543 Tcl_AppendResult (interp, p->referenceId, NULL);
1548 * do_preferredRecordSyntax: Set/get preferred record syntax
1550 static int do_preferredRecordSyntax (void *obj, Tcl_Interp *interp,
1551 int argc, char **argv)
1553 IrTcl_SetCObj *p = obj;
1557 p->preferredRecordSyntax = NULL;
1560 else if (argc == -1)
1562 free (p->preferredRecordSyntax);
1563 p->preferredRecordSyntax = NULL;
1568 free (p->preferredRecordSyntax);
1569 p->preferredRecordSyntax = NULL;
1570 if (argv[2][0] && (p->preferredRecordSyntax =
1571 ir_tcl_malloc (sizeof(*p->preferredRecordSyntax))))
1572 *p->preferredRecordSyntax = IrTcl_getRecordSyntaxVal (argv[2]);
1579 * do_elementSetNames: Set/Get element Set Names
1581 static int do_elementSetNames (void *obj, Tcl_Interp *interp,
1582 int argc, char **argv)
1584 IrTcl_SetCObj *p = obj;
1588 p->elementSetNames = NULL;
1591 else if (argc == -1)
1592 return ir_tcl_strdel (interp, &p->elementSetNames);
1595 free (p->elementSetNames);
1596 if (ir_tcl_strdup (interp, &p->elementSetNames, argv[2]) == TCL_ERROR)
1599 Tcl_AppendResult (interp, p->elementSetNames, NULL);
1604 * do_smallSetElementSetNames: Set/Get small Set Element Set Names
1606 static int do_smallSetElementSetNames (void *obj, Tcl_Interp *interp,
1607 int argc, char **argv)
1609 IrTcl_SetCObj *p = obj;
1613 p->smallSetElementSetNames = NULL;
1616 else if (argc == -1)
1617 return ir_tcl_strdel (interp, &p->smallSetElementSetNames);
1620 free (p->smallSetElementSetNames);
1621 if (ir_tcl_strdup (interp, &p->smallSetElementSetNames,
1622 argv[2]) == TCL_ERROR)
1625 Tcl_AppendResult (interp, p->smallSetElementSetNames, NULL);
1630 * do_mediumSetElementSetNames: Set/Get medium Set Element Set Names
1632 static int do_mediumSetElementSetNames (void *obj, Tcl_Interp *interp,
1633 int argc, char **argv)
1635 IrTcl_SetCObj *p = obj;
1639 p->mediumSetElementSetNames = NULL;
1642 else if (argc == -1)
1643 return ir_tcl_strdel (interp, &p->mediumSetElementSetNames);
1646 free (p->mediumSetElementSetNames);
1647 if (ir_tcl_strdup (interp, &p->mediumSetElementSetNames,
1648 argv[2]) == TCL_ERROR)
1651 Tcl_AppendResult (interp, p->mediumSetElementSetNames, NULL);
1655 static IrTcl_Method ir_method_tab[] = {
1656 { 1, "comstack", do_comstack },
1657 { 1, "protocol", do_protocol },
1658 { 0, "failback", do_failback },
1659 { 0, "failInfo", do_failInfo },
1660 { 0, "apduInfo", do_apduInfo },
1661 { 0, "logLevel", do_logLevel },
1663 { 0, "eventType", do_eventType },
1664 { 1, "connect", do_connect },
1665 { 0, "protocolVersion", do_protocolVersion },
1666 { 1, "preferredMessageSize", do_preferredMessageSize },
1667 { 1, "maximumRecordSize", do_maximumRecordSize },
1668 { 1, "implementationName", do_implementationName },
1669 { 1, "implementationId", do_implementationId },
1670 { 1, "implementationVersion", do_implementationVersion },
1671 { 0, "targetImplementationName", do_targetImplementationName },
1672 { 0, "targetImplementationId", do_targetImplementationId },
1673 { 0, "targetImplementationVersion", do_targetImplementationVersion },
1674 { 0, "userInformationField", do_userInformationField },
1675 { 1, "idAuthentication", do_idAuthentication },
1676 { 0, "options", do_options },
1677 { 0, "init", do_init_request },
1678 { 0, "initResult", do_initResult },
1679 { 0, "disconnect", do_disconnect },
1680 { 0, "callback", do_callback },
1681 { 0, "initResponse", do_initResponse },
1682 { 0, "triggerResourceControl", do_triggerResourceControl },
1683 { 0, "initResponse", do_initResponse },
1687 static IrTcl_Method ir_set_c_method_tab[] = {
1688 { 0, "databaseNames", do_databaseNames},
1689 { 0, "replaceIndicator", do_replaceIndicator},
1690 { 0, "queryType", do_queryType },
1691 { 0, "preferredRecordSyntax", do_preferredRecordSyntax },
1692 { 0, "smallSetUpperBound", do_smallSetUpperBound},
1693 { 0, "largeSetLowerBound", do_largeSetLowerBound},
1694 { 0, "mediumSetPresentNumber", do_mediumSetPresentNumber},
1695 { 0, "referenceId", do_referenceId },
1696 { 0, "elementSetNames", do_elementSetNames },
1697 { 0, "smallSetElementSetNames", do_smallSetElementSetNames },
1698 { 0, "mediumSetElementSetNames", do_mediumSetElementSetNames },
1703 * ir_obj_method: IR Object methods
1705 static int ir_obj_method (ClientData clientData, Tcl_Interp *interp,
1706 int argc, char **argv)
1708 IrTcl_Methods tab[3];
1709 IrTcl_Obj *p = clientData;
1712 return ir_method_r (clientData, interp, argc, argv, ir_method_tab);
1714 tab[0].tab = ir_method_tab;
1716 tab[1].tab = ir_set_c_method_tab;
1717 tab[1].obj = &p->set_inher;
1720 return ir_method (interp, argc, argv, tab);
1724 * ir_obj_delete: IR Object disposal
1726 static void ir_obj_delete (ClientData clientData)
1728 IrTcl_Obj *obj = clientData;
1729 IrTcl_Methods tab[3];
1732 if (obj->ref_count > 0)
1734 assert (obj->ref_count == 0);
1736 logf (LOG_DEBUG, "ir object delete");
1737 tab[0].tab = ir_method_tab;
1739 tab[1].tab = ir_set_c_method_tab;
1740 tab[1].obj = &obj->set_inher;
1743 ir_method (NULL, -1, NULL, tab);
1746 odr_destroy (obj->odr_in);
1747 odr_destroy (obj->odr_out);
1748 odr_destroy (obj->odr_pr);
1753 * ir_obj_mk: IR Object creation
1755 static int ir_obj_mk (ClientData clientData, Tcl_Interp *interp,
1756 int argc, char **argv)
1758 IrTcl_Methods tab[3];
1766 interp->result = "wrong # args";
1769 obj = ir_tcl_malloc (sizeof(*obj));
1772 obj->bibset = ccl_qual_mk ();
1773 if ((inf = fopen ("default.bib", "r")))
1775 ccl_qual_file (obj->bibset, inf);
1780 logf (LOG_DEBUG, "ir object create %s", argv[1]);
1781 obj->odr_in = odr_createmem (ODR_DECODE);
1782 obj->odr_out = odr_createmem (ODR_ENCODE);
1783 obj->odr_pr = odr_createmem (ODR_PRINT);
1784 obj->state = IR_TCL_R_Idle;
1785 obj->interp = interp;
1789 obj->request_queue = NULL;
1791 tab[0].tab = ir_method_tab;
1793 tab[1].tab = ir_set_c_method_tab;
1794 tab[1].obj = &obj->set_inher;
1797 if (ir_method (interp, 0, NULL, tab) == TCL_ERROR)
1799 Tcl_CreateCommand (interp, argv[1], ir_obj_method,
1800 (ClientData) obj, ir_obj_delete);
1804 /* ------------------------------------------------------- */
1806 * do_search: Do search request
1808 static int do_search (void *o, Tcl_Interp *interp, int argc, char **argv)
1810 Z_SearchRequest *req;
1814 IrTcl_SetObj *obj = o;
1825 interp->result = "wrong # args";
1828 if (!obj->set_inher.num_databaseNames)
1830 interp->result = "no databaseNames";
1835 interp->result = "search: not connected";
1838 apdu = zget_APDU (p->odr_out, Z_APDU_searchRequest);
1839 req = apdu->u.searchRequest;
1843 bib1.proto = p->protocol_type;
1844 bib1.oclass = CLASS_ATTSET;
1845 bib1.value = VAL_BIB1;
1847 set_referenceId (p->odr_out, &req->referenceId,
1848 obj->set_inher.referenceId);
1850 req->smallSetUpperBound = &obj->set_inher.smallSetUpperBound;
1851 req->largeSetLowerBound = &obj->set_inher.largeSetLowerBound;
1852 req->mediumSetPresentNumber = &obj->set_inher.mediumSetPresentNumber;
1853 req->replaceIndicator = &obj->set_inher.replaceIndicator;
1854 req->resultSetName = obj->setName ? obj->setName : "Default";
1855 logf (LOG_DEBUG, "Search, resultSetName %s", req->resultSetName);
1856 req->num_databaseNames = obj->set_inher.num_databaseNames;
1857 req->databaseNames = obj->set_inher.databaseNames;
1858 for (r=0; r < obj->set_inher.num_databaseNames; r++)
1859 logf (LOG_DEBUG, " Database %s", obj->set_inher.databaseNames[r]);
1860 if (obj->set_inher.preferredRecordSyntax)
1862 struct oident ident;
1864 ident.proto = p->protocol_type;
1865 ident.oclass = CLASS_RECSYN;
1866 ident.value = *obj->set_inher.preferredRecordSyntax;
1867 logf (LOG_DEBUG, "Preferred record syntax is %d", ident.value);
1868 req->preferredRecordSyntax = odr_oiddup (p->odr_out,
1869 oid_getoidbyent (&ident));
1872 req->preferredRecordSyntax = 0;
1874 if (obj->set_inher.smallSetElementSetNames &&
1875 *obj->set_inher.smallSetElementSetNames)
1877 Z_ElementSetNames *esn = odr_malloc (p->odr_out, sizeof(*esn));
1879 esn->which = Z_ElementSetNames_generic;
1880 esn->u.generic = obj->set_inher.smallSetElementSetNames;
1881 req->smallSetElementSetNames = esn;
1884 req->smallSetElementSetNames = NULL;
1886 if (obj->set_inher.mediumSetElementSetNames &&
1887 *obj->set_inher.mediumSetElementSetNames)
1889 Z_ElementSetNames *esn = odr_malloc (p->odr_out, sizeof(*esn));
1891 esn->which = Z_ElementSetNames_generic;
1892 esn->u.generic = obj->set_inher.mediumSetElementSetNames;
1893 req->mediumSetElementSetNames = esn;
1896 req->mediumSetElementSetNames = NULL;
1898 req->query = &query;
1900 if (!strcmp (obj->set_inher.queryType, "rpn"))
1902 Z_RPNQuery *RPNquery;
1904 RPNquery = p_query_rpn (p->odr_out, argv[2]);
1907 Tcl_AppendResult (interp, "Syntax error in query", NULL);
1910 RPNquery->attributeSetId = oid_getoidbyent (&bib1);
1911 query.which = Z_Query_type_1;
1912 query.u.type_1 = RPNquery;
1913 logf (LOG_DEBUG, "RPN");
1916 else if (!strcmp (obj->set_inher.queryType, "cclrpn"))
1920 struct ccl_rpn_node *rpn;
1921 Z_RPNQuery *RPNquery;
1923 rpn = ccl_find_str(p->bibset, argv[2], &error, &pos);
1926 Tcl_AppendResult (interp, "CCL error: ",
1927 ccl_err_msg(error), NULL);
1930 ccl_pr_tree (rpn, stderr);
1931 fprintf (stderr, "\n");
1932 assert((RPNquery = ccl_rpn_query(rpn)));
1933 RPNquery->attributeSetId = oid_getoidbyent (&bib1);
1934 query.which = Z_Query_type_1;
1935 query.u.type_1 = RPNquery;
1936 logf (LOG_DEBUG, "CCLRPN");
1939 else if (!strcmp (obj->set_inher.queryType, "ccl"))
1941 query.which = Z_Query_type_2;
1942 query.u.type_2 = &ccl_query;
1943 ccl_query.buf = (unsigned char *) argv[2];
1944 ccl_query.len = strlen (argv[2]);
1945 logf (LOG_DEBUG, "CCL");
1949 interp->result = "unknown query method";
1952 return ir_tcl_send_APDU (interp, p, apdu, "search", *argv);
1956 * do_searchResponse: add search response handler
1958 static int do_searchResponse (void *o, Tcl_Interp *interp,
1959 int argc, char **argv)
1961 IrTcl_SetObj *obj = o;
1965 obj->searchResponse = NULL;
1968 else if (argc == -1)
1969 return ir_tcl_strdel (interp, &obj->searchResponse);
1972 free (obj->searchResponse);
1975 if (ir_tcl_strdup (interp, &obj->searchResponse, argv[2])
1980 obj->searchResponse = NULL;
1986 * do_presentResponse: add present response handler
1988 static int do_presentResponse (void *o, Tcl_Interp *interp,
1989 int argc, char **argv)
1991 IrTcl_SetObj *obj = o;
1995 obj->presentResponse = NULL;
1998 else if (argc == -1)
1999 return ir_tcl_strdel (interp, &obj->presentResponse);
2002 free (obj->presentResponse);
2005 if (ir_tcl_strdup (interp, &obj->presentResponse, argv[2])
2010 obj->presentResponse = NULL;
2016 * do_resultCount: Get number of hits
2018 static int do_resultCount (void *o, Tcl_Interp *interp,
2019 int argc, char **argv)
2021 IrTcl_SetObj *obj = o;
2025 return get_set_int (&obj->resultCount, interp, argc, argv);
2029 * do_searchStatus: Get search status (after search response)
2031 static int do_searchStatus (void *o, Tcl_Interp *interp,
2032 int argc, char **argv)
2034 IrTcl_SetObj *obj = o;
2038 return get_set_int (&obj->searchStatus, interp, argc, argv);
2042 * do_presentStatus: Get search status (after search/present response)
2044 static int do_presentStatus (void *o, Tcl_Interp *interp,
2045 int argc, char **argv)
2047 IrTcl_SetObj *obj = o;
2051 return get_set_int (&obj->presentStatus, interp, argc, argv);
2055 * do_nextResultSetPosition: Get next result set position
2056 * (after search/present response)
2058 static int do_nextResultSetPosition (void *o, Tcl_Interp *interp,
2059 int argc, char **argv)
2061 IrTcl_SetObj *obj = o;
2065 obj->nextResultSetPosition = 0;
2068 return get_set_int (&obj->nextResultSetPosition, interp, argc, argv);
2072 * do_setName: Set result Set name
2074 static int do_setName (void *o, Tcl_Interp *interp,
2075 int argc, char **argv)
2077 IrTcl_SetObj *obj = o;
2080 return ir_tcl_strdup (interp, &obj->setName, "Default");
2081 else if (argc == -1)
2082 return ir_tcl_strdel (interp, &obj->setName);
2085 free (obj->setName);
2086 if (ir_tcl_strdup (interp, &obj->setName, argv[2])
2090 Tcl_AppendElement (interp, obj->setName);
2095 * do_numberOfRecordsReturned: Get number of records returned
2097 static int do_numberOfRecordsReturned (void *o, Tcl_Interp *interp,
2098 int argc, char **argv)
2100 IrTcl_SetObj *obj = o;
2104 obj->numberOfRecordsReturned = 0;
2107 return get_set_int (&obj->numberOfRecordsReturned, interp, argc, argv);
2111 * do_type: Return type (if any) at position.
2113 static int do_type (void *o, Tcl_Interp *interp, int argc, char **argv)
2115 IrTcl_SetObj *obj = o;
2117 IrTcl_RecordList *rl;
2121 obj->record_list = NULL;
2124 else if (argc == -1)
2126 delete_IR_records (obj);
2131 sprintf (interp->result, "wrong # args");
2134 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2136 rl = find_IR_record (obj, offset);
2139 logf (LOG_DEBUG, "No record at position %d", offset);
2144 case Z_NamePlusRecord_databaseRecord:
2145 interp->result = "DB";
2147 case Z_NamePlusRecord_surrogateDiagnostic:
2148 interp->result = "SD";
2156 * do_recordType: Return record type (if any) at position.
2158 static int do_recordType (void *o, Tcl_Interp *interp, int argc, char **argv)
2160 IrTcl_SetObj *obj = o;
2162 IrTcl_RecordList *rl;
2168 else if (argc == -1)
2174 sprintf (interp->result, "wrong # args");
2177 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2179 rl = find_IR_record (obj, offset);
2182 if (rl->which != Z_NamePlusRecord_databaseRecord)
2184 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2187 Tcl_AppendElement (interp, (char*)
2188 IrTcl_getRecordSyntaxStr (rl->u.dbrec.type));
2193 * set record elements (for record extraction)
2195 static int do_recordElements (void *o, Tcl_Interp *interp,
2196 int argc, char **argv)
2198 IrTcl_SetObj *obj = o;
2202 obj->recordElements = NULL;
2205 else if (argc == -1)
2206 return ir_tcl_strdel (NULL, &obj->recordElements);
2209 sprintf (interp->result, "wrong # args");
2214 free (obj->recordElements);
2215 return ir_tcl_strdup (NULL, &obj->recordElements,
2216 (*argv[2] ? argv[2] : NULL));
2218 Tcl_AppendResult (interp, obj->recordElements, NULL);
2225 static int ir_diagResult (Tcl_Interp *interp, IrTcl_Diagnostic *list, int num)
2231 for (i = 0; i<num; i++)
2233 logf (LOG_DEBUG, "Diagnostic, code %d", list[i].condition);
2234 sprintf (buf, "%d", list[i].condition);
2235 Tcl_AppendElement (interp, buf);
2236 cp = diagbib1_str (list[i].condition);
2238 Tcl_AppendElement (interp, (char*) cp);
2240 Tcl_AppendElement (interp, "");
2241 if (list[i].addinfo)
2242 Tcl_AppendElement (interp, (char*) list[i].addinfo);
2244 Tcl_AppendElement (interp, "");
2250 * do_diag: Return diagnostic record info
2252 static int do_diag (void *o, Tcl_Interp *interp, int argc, char **argv)
2254 IrTcl_SetObj *obj = o;
2256 IrTcl_RecordList *rl;
2262 sprintf (interp->result, "wrong # args");
2265 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2267 rl = find_IR_record (obj, offset);
2270 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2273 if (rl->which != Z_NamePlusRecord_surrogateDiagnostic)
2275 Tcl_AppendResult (interp, "No Diagnostic record at #", argv[2], NULL);
2278 return ir_diagResult (interp, rl->u.surrogateDiagnostics.list,
2279 rl->u.surrogateDiagnostics.num);
2283 * do_getMarc: Get ISO2709 Record lines/fields
2285 static int do_getMarc (void *o, Tcl_Interp *interp, int argc, char **argv)
2287 IrTcl_SetObj *obj = o;
2289 IrTcl_RecordList *rl;
2295 sprintf (interp->result, "wrong # args");
2298 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2300 rl = find_IR_record (obj, offset);
2303 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2306 if (rl->which != Z_NamePlusRecord_databaseRecord)
2308 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2311 return ir_tcl_get_marc (interp, rl->u.dbrec.buf, argc, argv);
2315 * do_getSutrs: Get SUTRS Record
2317 static int do_getSutrs (void *o, Tcl_Interp *interp, int argc, char **argv)
2319 IrTcl_SetObj *obj = o;
2321 IrTcl_RecordList *rl;
2327 sprintf (interp->result, "wrong # args");
2330 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2332 rl = find_IR_record (obj, offset);
2335 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2338 if (rl->which != Z_NamePlusRecord_databaseRecord)
2340 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2343 if (rl->u.dbrec.type != VAL_SUTRS)
2345 Tcl_AppendElement (interp, rl->u.dbrec.buf);
2351 * do_getGrs: Get a GRS1 Record
2353 static int do_getGrs (void *o, Tcl_Interp *interp, int argc, char **argv)
2355 IrTcl_SetObj *obj = o;
2357 IrTcl_RecordList *rl;
2363 sprintf (interp->result, "wrong # args");
2366 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2368 rl = find_IR_record (obj, offset);
2371 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2374 if (rl->which != Z_NamePlusRecord_databaseRecord)
2376 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2379 if (rl->u.dbrec.type != VAL_GRS1)
2381 return ir_tcl_get_grs (interp, rl->u.dbrec.u.grs1, argc, argv);
2386 * do_responseStatus: Return response status (present or search)
2388 static int do_responseStatus (void *o, Tcl_Interp *interp,
2389 int argc, char **argv)
2391 IrTcl_SetObj *obj = o;
2395 obj->recordFlag = 0;
2396 obj->nonSurrogateDiagnosticNum = 0;
2397 obj->nonSurrogateDiagnosticList = NULL;
2400 else if (argc == -1)
2402 ir_deleteDiags (&obj->nonSurrogateDiagnosticList,
2403 &obj->nonSurrogateDiagnosticNum);
2406 if (!obj->recordFlag)
2408 Tcl_AppendElement (interp, "OK");
2413 case Z_Records_DBOSD:
2414 Tcl_AppendElement (interp, "DBOSD");
2417 Tcl_AppendElement (interp, "NSD");
2418 return ir_diagResult (interp, obj->nonSurrogateDiagnosticList,
2419 obj->nonSurrogateDiagnosticNum);
2425 * do_present: Perform Present Request
2428 static int do_present (void *o, Tcl_Interp *interp, int argc, char **argv)
2430 IrTcl_SetObj *obj = o;
2433 Z_PresentRequest *req;
2441 if (Tcl_GetInt (interp, argv[2], &start) == TCL_ERROR)
2448 if (Tcl_GetInt (interp, argv[3], &number) == TCL_ERROR)
2456 interp->result = "present: not connected";
2461 obj->number = number;
2463 apdu = zget_APDU (p->odr_out, Z_APDU_presentRequest);
2464 req = apdu->u.presentRequest;
2466 set_referenceId (p->odr_out, &req->referenceId,
2467 obj->set_inher.referenceId);
2469 req->resultSetId = obj->setName ? obj->setName : "Default";
2471 req->resultSetStartPoint = &start;
2472 req->numberOfRecordsRequested = &number;
2473 if (obj->set_inher.preferredRecordSyntax)
2475 struct oident ident;
2477 ident.proto = p->protocol_type;
2478 ident.oclass = CLASS_RECSYN;
2479 ident.value = *obj->set_inher.preferredRecordSyntax;
2480 logf (LOG_DEBUG, "Preferred record syntax is %d", ident.value);
2481 req->preferredRecordSyntax = odr_oiddup (p->odr_out,
2482 oid_getoidbyent (&ident));
2485 req->preferredRecordSyntax = 0;
2487 if (obj->set_inher.elementSetNames && *obj->set_inher.elementSetNames)
2489 Z_ElementSetNames *esn = odr_malloc (p->odr_out, sizeof(*esn));
2490 Z_RecordComposition *compo = odr_malloc (p->odr_out, sizeof(*compo));
2492 esn->which = Z_ElementSetNames_generic;
2493 esn->u.generic = obj->set_inher.elementSetNames;
2495 req->recordComposition = compo;
2496 compo->which = Z_RecordComp_simple;
2497 compo->u.simple = esn;
2500 req->recordComposition = NULL;
2501 return ir_tcl_send_APDU (interp, p, apdu, "present", *argv);
2505 * do_loadFile: Load result set from file
2508 static int do_loadFile (void *o, Tcl_Interp *interp,
2509 int argc, char **argv)
2511 IrTcl_SetObj *setobj = o;
2521 interp->result = "wrong # args";
2524 inf = fopen (argv[2], "r");
2527 Tcl_AppendResult (interp, "Cannot open ", argv[2], NULL);
2530 while ((buf = ir_tcl_fread_marc (inf, &size)))
2532 IrTcl_RecordList *rl;
2534 rl = new_IR_record (setobj, no, Z_NamePlusRecord_databaseRecord, "F");
2535 rl->u.dbrec.type = VAL_USMARC;
2536 rl->u.dbrec.buf = buf;
2537 rl->u.dbrec.size = size;
2540 setobj->numberOfRecordsReturned = no-1;
2545 static IrTcl_Method ir_set_method_tab[] = {
2546 { 0, "search", do_search },
2547 { 0, "searchResponse", do_searchResponse },
2548 { 0, "presentResponse", do_presentResponse },
2549 { 0, "searchStatus", do_searchStatus },
2550 { 0, "presentStatus", do_presentStatus },
2551 { 0, "nextResultSetPosition", do_nextResultSetPosition },
2552 { 0, "setName", do_setName },
2553 { 0, "resultCount", do_resultCount },
2554 { 0, "numberOfRecordsReturned", do_numberOfRecordsReturned },
2555 { 0, "present", do_present },
2556 { 0, "type", do_type },
2557 { 0, "getMarc", do_getMarc },
2558 { 0, "getSutrs", do_getSutrs },
2559 { 0, "getGrs", do_getGrs },
2560 { 0, "recordType", do_recordType },
2561 { 0, "recordElements", do_recordElements },
2562 { 0, "diag", do_diag },
2563 { 0, "responseStatus", do_responseStatus },
2564 { 0, "loadFile", do_loadFile },
2569 * ir_set_obj_method: IR Set Object methods
2571 static int ir_set_obj_method (ClientData clientData, Tcl_Interp *interp,
2572 int argc, char **argv)
2574 IrTcl_Methods tabs[3];
2575 IrTcl_SetObj *p = clientData;
2579 interp->result = "wrong # args";
2582 tabs[0].tab = ir_set_method_tab;
2584 tabs[1].tab = ir_set_c_method_tab;
2585 tabs[1].obj = &p->set_inher;
2588 return ir_method (interp, argc, argv, tabs);
2592 * ir_set_obj_delete: IR Set Object disposal
2594 static void ir_set_obj_delete (ClientData clientData)
2596 IrTcl_Methods tabs[3];
2597 IrTcl_SetObj *p = clientData;
2599 logf (LOG_DEBUG, "ir set delete");
2601 tabs[0].tab = ir_set_method_tab;
2603 tabs[1].tab = ir_set_c_method_tab;
2604 tabs[1].obj = &p->set_inher;
2607 ir_method (NULL, -1, NULL, tabs);
2613 * ir_set_obj_mk: IR Set Object creation
2615 static int ir_set_obj_mk (ClientData clientData, Tcl_Interp *interp,
2616 int argc, char **argv)
2618 IrTcl_Methods tabs[3];
2621 if (argc < 2 || argc > 3)
2623 interp->result = "wrong # args";
2626 obj = ir_tcl_malloc (sizeof(*obj));
2627 logf (LOG_DEBUG, "ir set create");
2630 Tcl_CmdInfo parent_info;
2635 if (!Tcl_GetCommandInfo (interp, argv[2], &parent_info))
2637 interp->result = "No parent";
2640 obj->parent = (IrTcl_Obj *) parent_info.clientData;
2642 dst = &obj->set_inher;
2643 src = &obj->parent->set_inher;
2645 if ((dst->num_databaseNames = src->num_databaseNames))
2646 dst->databaseNames =
2647 ir_tcl_malloc (sizeof (*dst->databaseNames)
2648 * dst->num_databaseNames);
2650 dst->databaseNames = NULL;
2651 for (i = 0; i < dst->num_databaseNames; i++)
2652 if (ir_tcl_strdup (interp, &dst->databaseNames[i],
2653 src->databaseNames[i]) == TCL_ERROR)
2655 if (ir_tcl_strdup (interp, &dst->queryType, src->queryType)
2659 if (ir_tcl_strdup (interp, &dst->referenceId, src->referenceId)
2663 if (ir_tcl_strdup (interp, &dst->elementSetNames, src->elementSetNames)
2667 if (ir_tcl_strdup (interp, &dst->smallSetElementSetNames,
2668 src->smallSetElementSetNames)
2672 if (ir_tcl_strdup (interp, &dst->mediumSetElementSetNames,
2673 src->mediumSetElementSetNames)
2677 if (src->preferredRecordSyntax &&
2678 (dst->preferredRecordSyntax
2679 = ir_tcl_malloc (sizeof(*dst->preferredRecordSyntax))))
2680 *dst->preferredRecordSyntax = *src->preferredRecordSyntax;
2682 dst->preferredRecordSyntax = NULL;
2683 dst->replaceIndicator = src->replaceIndicator;
2684 dst->smallSetUpperBound = src->smallSetUpperBound;
2685 dst->largeSetLowerBound = src->largeSetLowerBound;
2686 dst->mediumSetPresentNumber = src->mediumSetPresentNumber;
2691 tabs[0].tab = ir_set_method_tab;
2695 if (ir_method (interp, 0, NULL, tabs) == TCL_ERROR)
2698 Tcl_CreateCommand (interp, argv[1], ir_set_obj_method,
2699 (ClientData) obj, ir_set_obj_delete);
2703 /* ------------------------------------------------------- */
2706 * do_scan: Perform scan
2708 static int do_scan (void *o, Tcl_Interp *interp, int argc, char **argv)
2712 IrTcl_ScanObj *obj = o;
2713 IrTcl_Obj *p = obj->parent;
2716 struct ccl_rpn_node *rpn;
2724 interp->result = "wrong # args";
2727 if (!p->set_inher.num_databaseNames)
2729 interp->result = "no databaseNames";
2734 interp->result = "scan: not connected";
2738 bib1.proto = p->protocol_type;
2739 bib1.oclass = CLASS_ATTSET;
2740 bib1.value = VAL_BIB1;
2742 apdu = zget_APDU (p->odr_out, Z_APDU_scanRequest);
2743 req = apdu->u.scanRequest;
2745 set_referenceId (p->odr_out, &req->referenceId, p->set_inher.referenceId);
2746 req->num_databaseNames = p->set_inher.num_databaseNames;
2747 req->databaseNames = p->set_inher.databaseNames;
2748 req->attributeSet = oid_getoidbyent (&bib1);
2751 if (!(req->termListAndStartPoint = p_query_scan (p->odr_out, argv[2])))
2753 Tcl_AppendResult (interp, "Syntax error in query", NULL);
2757 rpn = ccl_find_str(p->bibset, argv[2], &r, &pos);
2760 Tcl_AppendResult (interp, "CCL error: ", ccl_err_msg (r), NULL);
2763 ccl_pr_tree (rpn, stderr);
2764 fprintf (stderr, "\n");
2765 if (!(req->termListAndStartPoint = ccl_scan_query (rpn)))
2768 req->stepSize = &obj->stepSize;
2769 req->numberOfTermsRequested = &obj->numberOfTermsRequested;
2770 req->preferredPositionInResponse = &obj->preferredPositionInResponse;
2771 logf (LOG_DEBUG, "stepSize=%d", *req->stepSize);
2772 logf (LOG_DEBUG, "numberOfTermsRequested=%d",
2773 *req->numberOfTermsRequested);
2774 logf (LOG_DEBUG, "preferredPositionInResponse=%d",
2775 *req->preferredPositionInResponse);
2777 return ir_tcl_send_APDU (interp, p, apdu, "scan", *argv);
2781 * do_scanResponse: add scan response handler
2783 static int do_scanResponse (void *o, Tcl_Interp *interp,
2784 int argc, char **argv)
2786 IrTcl_ScanObj *obj = o;
2790 obj->scanResponse = NULL;
2793 else if (argc == -1)
2794 return ir_tcl_strdel (interp, &obj->scanResponse);
2797 free (obj->scanResponse);
2800 if (ir_tcl_strdup (interp, &obj->scanResponse, argv[2])
2805 obj->scanResponse = NULL;
2811 * do_stepSize: Set/get replace Step Size
2813 static int do_stepSize (void *obj, Tcl_Interp *interp,
2814 int argc, char **argv)
2816 IrTcl_ScanObj *p = obj;
2822 return get_set_int (&p->stepSize, interp, argc, argv);
2826 * do_numberOfTermsRequested: Set/get Number of Terms requested
2828 static int do_numberOfTermsRequested (void *obj, Tcl_Interp *interp,
2829 int argc, char **argv)
2831 IrTcl_ScanObj *p = obj;
2835 p->numberOfTermsRequested = 20;
2838 return get_set_int (&p->numberOfTermsRequested, interp, argc, argv);
2843 * do_preferredPositionInResponse: Set/get preferred Position
2845 static int do_preferredPositionInResponse (void *obj, Tcl_Interp *interp,
2846 int argc, char **argv)
2848 IrTcl_ScanObj *p = obj;
2852 p->preferredPositionInResponse = 1;
2855 return get_set_int (&p->preferredPositionInResponse, interp, argc, argv);
2859 * do_scanStatus: Get scan status
2861 static int do_scanStatus (void *obj, Tcl_Interp *interp,
2862 int argc, char **argv)
2864 IrTcl_ScanObj *p = obj;
2868 return get_set_int (&p->scanStatus, interp, argc, argv);
2872 * do_numberOfEntriesReturned: Get number of Entries returned
2874 static int do_numberOfEntriesReturned (void *obj, Tcl_Interp *interp,
2875 int argc, char **argv)
2877 IrTcl_ScanObj *p = obj;
2881 return get_set_int (&p->numberOfEntriesReturned, interp, argc, argv);
2885 * do_positionOfTerm: Get position of Term
2887 static int do_positionOfTerm (void *obj, Tcl_Interp *interp,
2888 int argc, char **argv)
2890 IrTcl_ScanObj *p = obj;
2894 return get_set_int (&p->positionOfTerm, interp, argc, argv);
2898 * do_scanLine: get Scan Line (surrogate or normal) after response
2900 static int do_scanLine (void *obj, Tcl_Interp *interp, int argc, char **argv)
2902 IrTcl_ScanObj *p = obj;
2908 p->entries_flag = 0;
2910 p->nonSurrogateDiagnosticNum = 0;
2911 p->nonSurrogateDiagnosticList = 0;
2914 else if (argc == -1)
2916 p->entries_flag = 0;
2917 /* release entries */
2920 ir_deleteDiags (&p->nonSurrogateDiagnosticList,
2921 &p->nonSurrogateDiagnosticNum);
2926 interp->result = "wrong # args";
2929 if (Tcl_GetInt (interp, argv[2], &i) == TCL_ERROR)
2931 if (!p->entries_flag || p->which != Z_ListEntries_entries || !p->entries
2932 || i >= p->num_entries || i < 0)
2934 switch (p->entries[i].which)
2936 case Z_Entry_termInfo:
2937 Tcl_AppendElement (interp, "T");
2938 if (p->entries[i].u.term.buf)
2939 Tcl_AppendElement (interp, p->entries[i].u.term.buf);
2941 Tcl_AppendElement (interp, "");
2942 sprintf (numstr, "%d", p->entries[i].u.term.globalOccurrences);
2943 Tcl_AppendElement (interp, numstr);
2945 case Z_Entry_surrogateDiagnostic:
2946 Tcl_AppendElement (interp, "SD");
2947 return ir_diagResult (interp, p->entries[i].u.diag.list,
2948 p->entries[i].u.diag.num);
2954 static IrTcl_Method ir_scan_method_tab[] = {
2955 { 0, "scan", do_scan },
2956 { 0, "scanResponse", do_scanResponse },
2957 { 0, "stepSize", do_stepSize },
2958 { 0, "numberOfTermsRequested", do_numberOfTermsRequested },
2959 { 0, "preferredPositionInResponse", do_preferredPositionInResponse },
2960 { 0, "scanStatus", do_scanStatus },
2961 { 0, "numberOfEntriesReturned", do_numberOfEntriesReturned },
2962 { 0, "positionOfTerm", do_positionOfTerm },
2963 { 0, "scanLine", do_scanLine },
2968 * ir_scan_obj_method: IR Scan Object methods
2970 static int ir_scan_obj_method (ClientData clientData, Tcl_Interp *interp,
2971 int argc, char **argv)
2973 IrTcl_Methods tabs[2];
2977 interp->result = "wrong # args";
2980 tabs[0].tab = ir_scan_method_tab;
2981 tabs[0].obj = clientData;
2984 return ir_method (interp, argc, argv, tabs);
2988 * ir_scan_obj_delete: IR Scan Object disposal
2990 static void ir_scan_obj_delete (ClientData clientData)
2992 IrTcl_Methods tabs[2];
2993 IrTcl_ScanObj *obj = clientData;
2995 tabs[0].tab = ir_scan_method_tab;
2999 ir_method (NULL, -1, NULL, tabs);
3004 * ir_scan_obj_mk: IR Scan Object creation
3006 static int ir_scan_obj_mk (ClientData clientData, Tcl_Interp *interp,
3007 int argc, char **argv)
3009 Tcl_CmdInfo parent_info;
3011 IrTcl_Methods tabs[2];
3015 interp->result = "wrong # args";
3018 if (!Tcl_GetCommandInfo (interp, argv[2], &parent_info))
3020 interp->result = "No parent";
3023 obj = ir_tcl_malloc (sizeof(*obj));
3024 obj->parent = (IrTcl_Obj *) parent_info.clientData;
3026 tabs[0].tab = ir_scan_method_tab;
3030 if (ir_method (interp, 0, NULL, tabs) == TCL_ERROR)
3032 Tcl_CreateCommand (interp, argv[1], ir_scan_obj_method,
3033 (ClientData) obj, ir_scan_obj_delete);
3037 /* ------------------------------------------------------- */
3039 static void ir_initResponse (void *obj, Z_InitResponse *initrs)
3043 p->initResult = *initrs->result ? 1 : 0;
3044 if (!*initrs->result)
3045 logf (LOG_DEBUG, "Connection rejected by target");
3047 logf (LOG_DEBUG, "Connection accepted by target");
3049 get_referenceId (&p->set_inher.referenceId, initrs->referenceId);
3051 free (p->targetImplementationId);
3052 ir_tcl_strdup (p->interp, &p->targetImplementationId,
3053 initrs->implementationId);
3054 free (p->targetImplementationName);
3055 ir_tcl_strdup (p->interp, &p->targetImplementationName,
3056 initrs->implementationName);
3057 free (p->targetImplementationVersion);
3058 ir_tcl_strdup (p->interp, &p->targetImplementationVersion,
3059 initrs->implementationVersion);
3061 p->maximumRecordSize = *initrs->maximumRecordSize;
3062 p->preferredMessageSize = *initrs->preferredMessageSize;
3064 memcpy (&p->options, initrs->options, sizeof(initrs->options));
3065 memcpy (&p->protocolVersion, initrs->protocolVersion,
3066 sizeof(initrs->protocolVersion));
3067 free (p->userInformationField);
3068 p->userInformationField = NULL;
3069 if (initrs->userInformationField)
3073 if (initrs->userInformationField->which == ODR_EXTERNAL_octet &&
3074 (p->userInformationField =
3075 ir_tcl_malloc ((len =
3076 initrs->userInformationField->
3077 u.octet_aligned->len) +1)))
3079 memcpy (p->userInformationField,
3080 initrs->userInformationField->u.octet_aligned->buf,
3082 (p->userInformationField)[len] = '\0';
3087 static void ir_deleteDiags (IrTcl_Diagnostic **dst_list, int *dst_num)
3090 for (i = 0; i<*dst_num; i++)
3091 free (dst_list[i]->addinfo);
3097 static void ir_handleDiags (IrTcl_Diagnostic **dst_list, int *dst_num,
3098 Z_DiagRec **list, int num)
3104 *dst_list = ir_tcl_malloc (sizeof(**dst_list) * num);
3105 for (i = 0; i<num; i++)
3107 switch (list[i]->which)
3109 case Z_DiagRec_defaultFormat:
3110 (*dst_list)[i].condition = *list[i]->u.defaultFormat->condition;
3111 addinfo = list[i]->u.defaultFormat->addinfo;
3113 ((*dst_list)[i].addinfo = ir_tcl_malloc (strlen(addinfo)+1)))
3114 strcpy ((*dst_list)[i].addinfo, addinfo);
3117 (*dst_list)[i].addinfo = NULL;
3118 (*dst_list)[i].condition = 0;
3123 static void ir_handleRecords (void *o, Z_Records *zrs, IrTcl_SetObj *setobj,
3124 const char *elements)
3129 IrTcl_RecordList *rl;
3131 setobj->which = zrs->which;
3132 setobj->recordFlag = 1;
3134 ir_deleteDiags (&setobj->nonSurrogateDiagnosticList,
3135 &setobj->nonSurrogateDiagnosticNum);
3136 if (zrs->which == Z_Records_DBOSD)
3138 setobj->numberOfRecordsReturned =
3139 zrs->u.databaseOrSurDiagnostics->num_records;
3140 logf (LOG_DEBUG, "Got %d records", setobj->numberOfRecordsReturned);
3141 for (offset = 0; offset<setobj->numberOfRecordsReturned; offset++)
3143 rl = new_IR_record (setobj, setobj->start + offset,
3144 zrs->u.databaseOrSurDiagnostics->
3145 records[offset]->which,
3147 if (rl->which == Z_NamePlusRecord_surrogateDiagnostic)
3149 ir_handleDiags (&rl->u.surrogateDiagnostics.list,
3150 &rl->u.surrogateDiagnostics.num,
3151 &zrs->u.databaseOrSurDiagnostics->
3152 records[offset]->u.surrogateDiagnostic,
3157 Z_DatabaseRecord *zr;
3159 struct oident *ident;
3161 zr = zrs->u.databaseOrSurDiagnostics->records[offset]
3163 oe = (Z_External*) zr;
3164 rl->u.dbrec.size = zr->u.octet_aligned->len;
3166 if ((ident = oid_getentbyoid (oe->direct_reference)))
3167 rl->u.dbrec.type = ident->value;
3169 rl->u.dbrec.type = VAL_USMARC;
3171 if (oe->which == ODR_EXTERNAL_octet && rl->u.dbrec.size > 0)
3173 char *buf = (char*) zr->u.octet_aligned->buf;
3174 if ((rl->u.dbrec.buf = ir_tcl_malloc (rl->u.dbrec.size)))
3175 memcpy (rl->u.dbrec.buf, buf, rl->u.dbrec.size);
3177 else if (rl->u.dbrec.type == VAL_SUTRS &&
3178 oe->which == Z_External_sutrs)
3180 odr_setbuf (p->odr_in, (char*) oe->u.single_ASN1_type->buf,
3181 oe->u.single_ASN1_type->len, 0);
3182 if ((rl->u.dbrec.buf = ir_tcl_malloc (oe->u.sutrs->len+1)))
3184 memcpy (rl->u.dbrec.buf, oe->u.sutrs->buf,
3186 rl->u.dbrec.buf[oe->u.sutrs->len] = '\0';
3188 rl->u.dbrec.size = oe->u.sutrs->len;
3190 else if (rl->u.dbrec.type == VAL_GRS1 &&
3191 oe->which == Z_External_grs1)
3193 ir_tcl_read_grs (oe->u.grs1, &rl->u.dbrec.u.grs1);
3194 rl->u.dbrec.buf = NULL;
3197 rl->u.dbrec.buf = NULL;
3201 else if (zrs->which == Z_Records_multipleNSD)
3203 logf (LOG_DEBUG, "multipleNonSurrogateDiagnostic %d",
3204 zrs->u.multipleNonSurDiagnostics->num_diagRecs);
3205 setobj->numberOfRecordsReturned = 0;
3206 ir_handleDiags (&setobj->nonSurrogateDiagnosticList,
3207 &setobj->nonSurrogateDiagnosticNum,
3208 zrs->u.multipleNonSurDiagnostics->diagRecs,
3209 zrs->u.multipleNonSurDiagnostics->num_diagRecs);
3213 logf (LOG_DEBUG, "NonSurrogateDiagnostic");
3214 setobj->numberOfRecordsReturned = 0;
3215 ir_handleDiags (&setobj->nonSurrogateDiagnosticList,
3216 &setobj->nonSurrogateDiagnosticNum,
3217 &zrs->u.nonSurrogateDiagnostic,
3222 static void ir_searchResponse (void *o, Z_SearchResponse *searchrs,
3223 IrTcl_SetObj *setobj)
3225 Z_Records *zrs = searchrs->records;
3227 logf (LOG_DEBUG, "Received search response");
3230 logf (LOG_DEBUG, "Search response, no object!");
3233 setobj->searchStatus = searchrs->searchStatus ? 1 : 0;
3234 get_referenceId (&setobj->set_inher.referenceId, searchrs->referenceId);
3235 setobj->resultCount = *searchrs->resultCount;
3236 if (searchrs->presentStatus)
3237 setobj->presentStatus = *searchrs->presentStatus;
3238 if (searchrs->nextResultSetPosition)
3239 setobj->nextResultSetPosition = *searchrs->nextResultSetPosition;
3241 logf (LOG_DEBUG, "Search response %d, %d hits",
3242 setobj->searchStatus, setobj->resultCount);
3246 if (setobj->resultCount <= setobj->set_inher.smallSetUpperBound)
3247 es = setobj->set_inher.smallSetElementSetNames;
3249 es = setobj->set_inher.mediumSetElementSetNames;
3250 ir_handleRecords (o, zrs, setobj, es);
3253 setobj->recordFlag = 0;
3257 static void ir_presentResponse (void *o, Z_PresentResponse *presrs,
3258 IrTcl_SetObj *setobj)
3260 Z_Records *zrs = presrs->records;
3262 logf (LOG_DEBUG, "Received present response");
3265 logf (LOG_DEBUG, "Present response, no object!");
3268 setobj->presentStatus = *presrs->presentStatus;
3269 get_referenceId (&setobj->set_inher.referenceId, presrs->referenceId);
3270 setobj->nextResultSetPosition = *presrs->nextResultSetPosition;
3272 ir_handleRecords (o, zrs, setobj, setobj->set_inher.elementSetNames);
3275 setobj->recordFlag = 0;
3276 logf (LOG_DEBUG, "No records!");
3280 static void ir_scanResponse (void *o, Z_ScanResponse *scanrs,
3281 IrTcl_ScanObj *scanobj)
3285 logf (LOG_DEBUG, "Received scanResponse");
3287 get_referenceId (&p->set_inher.referenceId, scanrs->referenceId);
3288 scanobj->scanStatus = *scanrs->scanStatus;
3289 logf (LOG_DEBUG, "scanStatus=%d", scanobj->scanStatus);
3291 if (scanrs->stepSize)
3292 scanobj->stepSize = *scanrs->stepSize;
3293 logf (LOG_DEBUG, "stepSize=%d", scanobj->stepSize);
3295 scanobj->numberOfEntriesReturned = *scanrs->numberOfEntriesReturned;
3296 logf (LOG_DEBUG, "numberOfEntriesReturned=%d",
3297 scanobj->numberOfEntriesReturned);
3299 if (scanrs->positionOfTerm)
3300 scanobj->positionOfTerm = *scanrs->positionOfTerm;
3302 scanobj->positionOfTerm = -1;
3303 logf (LOG_DEBUG, "positionOfTerm=%d", scanobj->positionOfTerm);
3305 free (scanobj->entries);
3306 scanobj->entries = NULL;
3308 ir_deleteDiags (&scanobj->nonSurrogateDiagnosticList,
3309 &scanobj->nonSurrogateDiagnosticNum);
3310 if (scanrs->entries)
3315 scanobj->entries_flag = 1;
3316 scanobj->which = scanrs->entries->which;
3317 switch (scanobj->which)
3319 case Z_ListEntries_entries:
3320 scanobj->num_entries = scanrs->entries->u.entries->num_entries;
3321 scanobj->entries = ir_tcl_malloc (scanobj->num_entries *
3322 sizeof(*scanobj->entries));
3323 for (i=0; i<scanobj->num_entries; i++)
3325 ze = scanrs->entries->u.entries->entries[i];
3326 scanobj->entries[i].which = ze->which;
3329 case Z_Entry_termInfo:
3330 if (ze->u.termInfo->term->which == Z_Term_general)
3332 int l = ze->u.termInfo->term->u.general->len;
3333 scanobj->entries[i].u.term.buf = ir_tcl_malloc (1+l);
3334 memcpy (scanobj->entries[i].u.term.buf,
3335 ze->u.termInfo->term->u.general->buf,
3337 scanobj->entries[i].u.term.buf[l] = '\0';
3340 scanobj->entries[i].u.term.buf = NULL;
3341 if (ze->u.termInfo->globalOccurrences)
3342 scanobj->entries[i].u.term.globalOccurrences =
3343 *ze->u.termInfo->globalOccurrences;
3345 scanobj->entries[i].u.term.globalOccurrences = 0;
3347 case Z_Entry_surrogateDiagnostic:
3348 ir_handleDiags (&scanobj->entries[i].u.diag.list,
3349 &scanobj->entries[i].u.diag.num,
3350 &ze->u.surrogateDiagnostic,
3356 case Z_ListEntries_nonSurrogateDiagnostics:
3357 ir_handleDiags (&scanobj->nonSurrogateDiagnosticList,
3358 &scanobj->nonSurrogateDiagnosticNum,
3359 scanrs->entries->u.nonSurrogateDiagnostics->
3361 scanrs->entries->u.nonSurrogateDiagnostics->
3367 scanobj->entries_flag = 0;
3371 * ir_select_read: handle incoming packages
3373 void ir_select_read (ClientData clientData)
3375 IrTcl_Obj *p = clientData;
3380 Tcl_CmdInfo cmd_info;
3381 const char *apdu_call;
3383 logf(LOG_DEBUG, "Read handler fd=%d", cs_fileno(p->cs_link));
3384 if (p->state == IR_TCL_R_Connecting)
3386 logf(LOG_DEBUG, "Connect handler");
3387 r = cs_rcvconnect (p->cs_link);
3390 logf (LOG_WARN, "cs_rcvconnect returned 1");
3393 p->state = IR_TCL_R_Idle;
3394 #if IRTCL_GENERIC_FILES
3395 ir_select_remove_write (p->csFile, p);
3397 ir_select_remove_write (cs_fileno (p->cs_link), p);
3401 logf (LOG_DEBUG, "cs_rcvconnect error");
3404 p->failInfo = IR_TCL_FAIL_CONNECT;
3405 IrTcl_eval (p->interp, p->failback);
3407 do_disconnect (p, NULL, 2, NULL);
3410 p->state = IR_TCL_R_Idle;
3412 IrTcl_eval (p->interp, p->callback);
3413 if (p->cs_link && p->request_queue && p->state == IR_TCL_R_Idle)
3414 ir_tcl_send_q (p, p->request_queue, "x");
3419 /* signal one more use of ir object - callbacks must not
3420 release the ir memory (p pointer) */
3421 p->state = IR_TCL_R_Reading;
3424 /* read incoming APDU */
3425 if ((r=cs_get (p->cs_link, &p->buf_in, &p->len_in)) <= 0)
3427 logf (LOG_DEBUG, "cs_get failed, code %d", r);
3428 #if IRTCL_GENERIC_FILES
3429 ir_select_remove (p->csFile, p);
3431 ir_select_remove (cs_fileno (p->cs_link), p);
3433 do_disconnect (p, NULL, 2, NULL);
3436 p->failInfo = IR_TCL_FAIL_READ;
3437 IrTcl_eval (p->interp, p->failback);
3439 /* release ir object now if callback deleted it */
3445 logf(LOG_DEBUG, "PDU Fraction read");
3448 /* got complete APDU. Now decode */
3451 odr_setbuf (p->odr_in, p->buf_in, r, 0);
3452 logf (LOG_DEBUG, "cs_get ok, total size %d", r);
3453 if (!z_APDU (p->odr_in, &apdu, 0))
3455 logf (LOG_DEBUG, "cs_get failed: %s",
3456 odr_errmsg (odr_geterror (p->odr_in)));
3457 do_disconnect (p, NULL, 2, NULL);
3460 p->failInfo = IR_TCL_FAIL_IN_APDU;
3461 p->apduOffset = odr_offset (p->odr_in);
3462 IrTcl_eval (p->interp, p->failback);
3464 /* release ir object now if failback deleted it */
3468 logf(LOG_DEBUG, "Decoded ok");
3469 /* handle APDU and invoke callback */
3470 rq = p->request_queue;
3473 logf (LOG_FATAL, "Internal error. No queue entry");
3476 object_name = rq->object_name;
3477 logf (LOG_DEBUG, "getCommandInfo (%s)", object_name);
3479 if (Tcl_GetCommandInfo (p->interp, object_name, &cmd_info))
3483 case Z_APDU_initResponse:
3484 p->eventType = "init";
3485 ir_initResponse (p, apdu->u.initResponse);
3486 apdu_call = p->initResponse;
3488 case Z_APDU_searchResponse:
3489 p->eventType = "search";
3490 ir_searchResponse (p, apdu->u.searchResponse,
3491 (IrTcl_SetObj *) cmd_info.clientData);
3492 apdu_call = ((IrTcl_SetObj *)
3493 cmd_info.clientData)->searchResponse;
3495 case Z_APDU_presentResponse:
3496 p->eventType = "present";
3497 ir_presentResponse (p, apdu->u.presentResponse,
3498 (IrTcl_SetObj *) cmd_info.clientData);
3499 apdu_call = ((IrTcl_SetObj *)
3500 cmd_info.clientData)->presentResponse;
3502 case Z_APDU_scanResponse:
3503 p->eventType = "scan";
3504 ir_scanResponse (p, apdu->u.scanResponse,
3505 (IrTcl_ScanObj *) cmd_info.clientData);
3506 apdu_call = ((IrTcl_ScanObj *)
3507 cmd_info.clientData)->scanResponse;
3510 logf (LOG_WARN, "Received unknown APDU type (%d)",
3512 do_disconnect (p, NULL, 2, NULL);
3515 p->failInfo = IR_TCL_FAIL_UNKNOWN_APDU;
3516 IrTcl_eval (p->interp, p->failback);
3521 p->request_queue = rq->next;
3522 p->state = IR_TCL_R_Idle;
3525 IrTcl_eval (p->interp, apdu_call);
3526 else if (rq->callback)
3527 IrTcl_eval (p->interp, rq->callback);
3529 free (rq->callback);
3530 free (rq->object_name);
3532 odr_reset (p->odr_in);
3533 if (p->ref_count == 1)
3539 } while (p->cs_link && cs_more (p->cs_link));
3540 if (p->cs_link && p->request_queue && p->state == IR_TCL_R_Idle)
3541 ir_tcl_send_q (p, p->request_queue, "x");
3545 * ir_select_write: handle outgoing packages - not yet written.
3547 void ir_select_write (ClientData clientData)
3549 IrTcl_Obj *p = clientData;
3553 logf (LOG_DEBUG, "Write handler fd=%d", cs_fileno(p->cs_link));
3554 if (p->state == IR_TCL_R_Connecting)
3556 logf(LOG_DEBUG, "Connect handler");
3557 r = cs_rcvconnect (p->cs_link);
3560 p->state = IR_TCL_R_Idle;
3563 logf (LOG_DEBUG, "cs_rcvconnect error");
3564 #if IRTCL_GENERIC_FILES
3565 ir_select_remove_write (p->csFile, p);
3567 ir_select_remove_write (cs_fileno (p->cs_link), p);
3571 p->failInfo = IR_TCL_FAIL_CONNECT;
3572 IrTcl_eval (p->interp, p->failback);
3574 do_disconnect (p, NULL, 2, NULL);
3577 #if IRTCL_GENERIC_FILES
3578 ir_select_remove_write (p->csFile, p);
3580 ir_select_remove_write (cs_fileno (p->cs_link), p);
3583 IrTcl_eval (p->interp, p->callback);
3586 rq = p->request_queue;
3587 if (!rq || !rq->buf_out)
3590 if ((r=cs_put (p->cs_link, rq->buf_out, rq->len_out)) < 0)
3592 logf (LOG_DEBUG, "cs_put write fail");
3595 p->failInfo = IR_TCL_FAIL_WRITE;
3596 IrTcl_eval (p->interp, p->failback);
3600 do_disconnect (p, NULL, 2, NULL);
3602 else if (r == 0) /* remove select bit */
3604 logf(LOG_DEBUG, "Write completed");
3605 p->state = IR_TCL_R_Waiting;
3606 #if IRTCL_GENERIC_FILES
3607 ir_select_remove_write (p->csFile, p);
3609 ir_select_remove_write (cs_fileno (p->cs_link), p);
3616 /* ------------------------------------------------------- */
3619 * Irtcl_init: Registration of TCL commands.
3621 int Irtcl_Init (Tcl_Interp *interp)
3623 Tcl_CreateCommand (interp, "ir", ir_obj_mk, (ClientData) NULL,
3624 (Tcl_CmdDeleteProc *) NULL);
3625 Tcl_CreateCommand (interp, "ir-set", ir_set_obj_mk,
3626 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
3627 Tcl_CreateCommand (interp, "ir-scan", ir_scan_obj_mk,
3628 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);