2 * IR toolkit for tcl/tk
4 * See the file LICENSE for details.
5 * Sebastian Hammer, Adam Dickmeiss
8 * Revision 1.59 1995-10-17 12:18:58 adam
9 * Bug fix: when target connection closed, the connection was not
10 * properly reestablished.
12 * Revision 1.58 1995/10/16 17:00:55 adam
13 * New setting: elementSetNames.
14 * Various client improvements. Medium presentation format looks better.
16 * Revision 1.57 1995/09/21 13:11:51 adam
17 * Support of dynamic loading.
18 * Test script uses load command if necessary.
20 * Revision 1.56 1995/08/29 15:30:14 adam
21 * Work on GRS records.
23 * Revision 1.55 1995/08/28 09:43:25 adam
24 * Minor changes. configure only searches for yaz beta 3 and versions after
27 * Revision 1.54 1995/08/24 12:25:16 adam
28 * Modified to work with yaz 1.0b3.
30 * Revision 1.53 1995/08/04 12:49:26 adam
31 * Bug fix: reading uninitialized variable p.
33 * Revision 1.52 1995/08/04 11:32:38 adam
34 * More work on output queue. Memory related routines moved
37 * Revision 1.51 1995/08/03 13:22:54 adam
40 * Revision 1.50 1995/07/20 08:09:49 adam
41 * client.tcl: Targets removed from hotTargets list when targets
42 * are removed/modified.
43 * ir-tcl.c: More work on triggerResourceControl.
45 * Revision 1.49 1995/06/30 12:39:21 adam
46 * Bug fix: loadFile didn't set record type.
47 * The MARC routines are a little less strict in the interpretation.
48 * Script display.tcl replaces the old marc.tcl.
49 * New interactive script: shell.tcl.
51 * Revision 1.48 1995/06/27 19:03:50 adam
52 * Bug fix in do_present in ir-tcl.c: p->set_child member weren't set.
53 * nextResultSetPosition used instead of setOffset.
55 * Revision 1.47 1995/06/25 10:25:04 adam
56 * Working on triggerResourceControl. Description of compile/install
57 * procedure moved to ir-tcl.sgml.
59 * Revision 1.46 1995/06/22 13:15:06 adam
60 * Feature: SUTRS. Setting getSutrs implemented.
61 * Work on display formats.
62 * Preferred record syntax can be set by the user.
64 * Revision 1.45 1995/06/20 08:07:30 adam
65 * New setting: failInfo.
66 * Working on better cancel mechanism.
68 * Revision 1.44 1995/06/19 17:01:20 adam
71 * Revision 1.43 1995/06/19 13:06:08 adam
72 * New define: IR_TCL_VERSION.
74 * Revision 1.42 1995/06/19 08:08:52 adam
75 * client.tcl: hotTargets now contain both database and target name.
76 * ir-tcl.c: setting protocol edited. Errors in callbacks are logged
77 * by logf(LOG_WARN, ...) calls.
79 * Revision 1.41 1995/06/16 12:28:16 adam
80 * Implemented preferredRecordSyntax.
81 * Minor changes in diagnostic handling.
82 * Record list deleted when connection closes.
84 * Revision 1.40 1995/06/14 13:37:18 adam
85 * Setting recordType implemented.
86 * Setting implementationVersion implemented.
87 * Settings implementationId / implementationName edited.
89 * Revision 1.39 1995/06/08 10:26:32 adam
90 * Bug fix in ir_strdup.
92 * Revision 1.38 1995/06/01 16:36:47 adam
93 * About buttons. Minor bug fixes.
95 * Revision 1.37 1995/06/01 07:31:20 adam
96 * Rename of many typedefs -> IrTcl_...
98 * Revision 1.36 1995/05/31 13:09:59 adam
99 * Client searches/presents may be interrupted.
100 * New moving book-logo.
102 * Revision 1.35 1995/05/31 08:36:33 adam
103 * Bug fix in client.tcl: didn't save options on clientrc.tcl.
104 * New method: referenceId. More work on scan.
106 * Revision 1.34 1995/05/29 10:33:42 adam
107 * README and rename of startup script.
109 * Revision 1.33 1995/05/29 09:15:11 quinn
110 * Changed CS_SR to PROTO_SR, etc.
112 * Revision 1.32 1995/05/29 08:44:16 adam
113 * Work on delete of objects.
115 * Revision 1.31 1995/05/26 11:44:10 adam
116 * Bugs fixed. More work on MARC utilities and queries. Test
117 * client is up-to-date again.
119 * Revision 1.30 1995/05/26 08:54:11 adam
120 * New MARC utilities. Uses prefix query.
122 * Revision 1.29 1995/05/24 14:10:22 adam
123 * Work on idAuthentication, protocolVersion and options.
125 * Revision 1.28 1995/05/23 15:34:48 adam
126 * Many new settings, userInformationField, smallSetUpperBound, etc.
127 * A number of settings are inherited when ir-set is executed.
128 * This version is incompatible with the graphical test client (client.tcl).
130 * Revision 1.27 1995/05/11 15:34:47 adam
131 * Scan request changed a bit. This version works with RLG.
133 * Revision 1.26 1995/04/18 16:11:51 adam
134 * First version of graphical Scan. Some work on query-by-form.
136 * Revision 1.25 1995/04/17 09:37:17 adam
137 * Further development of scan.
139 * Revision 1.24 1995/04/11 14:16:42 adam
140 * Further work on scan. Response works. Entries aren't saved yet.
142 * Revision 1.23 1995/04/10 10:50:27 adam
143 * Result-set name defaults to suffix of ir-set name.
144 * Started working on scan. Not finished at this point.
146 * Revision 1.22 1995/03/31 10:43:03 adam
147 * More robust when getting bad MARC records.
149 * Revision 1.21 1995/03/31 08:56:37 adam
150 * New button "Search".
152 * Revision 1.20 1995/03/29 16:07:09 adam
153 * Bug fix: Didn't use setName in present request.
155 * Revision 1.19 1995/03/28 12:45:23 adam
156 * New ir method failback: called on disconnect/protocol error.
157 * New ir set/get method: protocol: SR / Z3950.
158 * Simple popup and disconnect when failback is invoked.
160 * Revision 1.18 1995/03/21 15:50:12 adam
163 * Revision 1.17 1995/03/21 13:41:03 adam
164 * Comstack cs_create not used too often. Non-blocking connect.
166 * Revision 1.16 1995/03/21 08:26:06 adam
167 * New method, setName, to specify the result set name (other than Default).
168 * New method, responseStatus, which returns diagnostic info, if any, after
169 * present response / search response.
171 * Revision 1.15 1995/03/20 15:24:07 adam
172 * Diagnostic records saved on searchResponse.
174 * Revision 1.14 1995/03/20 08:53:22 adam
175 * Event loop in tclmain.c rewritten. New method searchStatus.
177 * Revision 1.13 1995/03/17 18:26:17 adam
178 * Non-blocking i/o used now. Database names popup as cascade items.
180 * Revision 1.12 1995/03/17 15:45:00 adam
181 * Improved target/database setup.
183 * Revision 1.11 1995/03/16 17:54:03 adam
184 * Minor changes really.
186 * Revision 1.10 1995/03/15 16:14:50 adam
187 * Blocking arg in cs_create changed.
189 * Revision 1.9 1995/03/15 13:59:24 adam
192 * Revision 1.8 1995/03/15 08:25:16 adam
193 * New method presentStatus to check for error on present. Misc. cleanup
194 * of IrTcl_RecordList manipulations. Full MARC record presentation in
197 * Revision 1.7 1995/03/14 17:32:29 adam
198 * Presentation of full Marc record in popup window.
200 * Revision 1.6 1995/03/12 19:31:55 adam
201 * Pattern matching implemented when retrieving MARC records. More
202 * diagnostic functions.
204 * Revision 1.5 1995/03/10 18:00:15 adam
205 * Actual presentation in line-by-line format. RPN query support.
207 * Revision 1.4 1995/03/09 16:15:08 adam
208 * First presentRequest attempts. Hot-target list.
214 #include <sys/time.h>
224 int (*method) (void *obj, Tcl_Interp *interp, int argc, char **argv);
232 static void ir_deleteDiags (IrTcl_Diagnostic **dst_list, int *dst_num);
233 static int do_disconnect (void *obj, Tcl_Interp *interp,
234 int argc, char **argv);
236 static IrTcl_RecordList *new_IR_record (IrTcl_SetObj *setobj,
239 IrTcl_RecordList *rl;
241 for (rl = setobj->record_list; rl; rl = rl->next)
247 case Z_NamePlusRecord_databaseRecord:
248 free (rl->u.dbrec.buf);
249 rl->u.dbrec.buf = NULL;
251 case Z_NamePlusRecord_surrogateDiagnostic:
252 ir_deleteDiags (&rl->u.surrogateDiagnostics.list,
253 &rl->u.surrogateDiagnostics.num);
261 rl = ir_tcl_malloc (sizeof(*rl));
262 rl->next = setobj->record_list;
264 setobj->record_list = rl;
271 enum oid_value value;
273 } IrTcl_recordSyntaxTab[] = {
274 { VAL_UNIMARC, "UNIMARC" },
275 { VAL_INTERMARC, "INTERMARC" },
277 { VAL_USMARC, "USMARC" },
278 { VAL_UKMARC, "UKMARC" },
279 { VAL_NORMARC, "NORMARC" },
280 { VAL_LIBRISMARC, "LIBRISMARC" },
281 { VAL_DANMARC, "DANMARC" },
282 { VAL_FINMARC, "FINMARC" },
284 { VAL_CANMARC, "CANMARC" },
286 { VAL_PICAMARC, "PICAMARC" },
287 { VAL_AUSMARC, "AUSMARC" },
288 { VAL_IBERMARC, "IBERMARC" },
289 { VAL_SUTRS, "SUTRS" },
290 { VAL_GRS1, "GRS1" },
297 int IrTcl_eval (Tcl_Interp *interp, const char *command)
299 char *tmp = ir_tcl_malloc (strlen(command)+1);
302 strcpy (tmp, command);
303 r = Tcl_Eval (interp, tmp);
305 logf (LOG_WARN, "Tcl error in line %d: %s", interp->errorLine,
307 Tcl_FreeResult (interp);
313 * IrTcl_getRecordSyntaxStr: Return record syntax name of object id
315 static const char *IrTcl_getRecordSyntaxStr (enum oid_value value)
318 for (i = 0; IrTcl_recordSyntaxTab[i].name; i++)
319 if (IrTcl_recordSyntaxTab[i].value == value)
320 return IrTcl_recordSyntaxTab[i].name;
325 * IrTcl_getRecordSyntaxVal: Return record syntax value of string
327 static enum oid_value IrTcl_getRecordSyntaxVal (const char *name)
330 for (i = 0; IrTcl_recordSyntaxTab[i].name; i++)
331 if (!strcmp (IrTcl_recordSyntaxTab[i].name, name))
332 return IrTcl_recordSyntaxTab[i].value;
336 static IrTcl_RecordList *find_IR_record (IrTcl_SetObj *setobj, int no)
338 IrTcl_RecordList *rl;
340 for (rl = setobj->record_list; rl; rl = rl->next)
346 static void delete_IR_records (IrTcl_SetObj *setobj)
348 IrTcl_RecordList *rl, *rl1;
350 for (rl = setobj->record_list; rl; rl = rl1)
354 case Z_NamePlusRecord_databaseRecord:
355 free (rl->u.dbrec.buf);
357 case Z_NamePlusRecord_surrogateDiagnostic:
358 ir_deleteDiags (&rl->u.surrogateDiagnostics.list,
359 &rl->u.surrogateDiagnostics.num);
365 setobj->record_list = NULL;
369 * get_set_int: Set/get integer value
371 static int get_set_int (int *val, Tcl_Interp *interp, int argc, char **argv)
377 if (Tcl_GetInt (interp, argv[2], val)==TCL_ERROR)
380 sprintf (buf, "%d", *val);
381 Tcl_AppendResult (interp, buf, NULL);
386 * ir_method: Search for method in table and invoke method handler
388 int ir_method (Tcl_Interp *interp, int argc, char **argv, IrTcl_Methods *tab)
390 IrTcl_Methods *tab_i = tab;
393 for (tab_i = tab; tab_i->tab; tab_i++)
394 for (t = tab_i->tab; t->name; t++)
397 if ((*t->method)(tab_i->obj, interp, argc, argv) == TCL_ERROR)
401 if (!strcmp (t->name, argv[1]))
402 return (*t->method)(tab_i->obj, interp, argc, argv);
406 Tcl_AppendResult (interp, "Bad method: ", argv[1],
407 ". Possible methods:", NULL);
408 for (tab_i = tab; tab_i->tab; tab_i++)
409 for (t = tab_i->tab; t->name; t++)
410 Tcl_AppendResult (interp, " ", t->name, NULL);
415 * ir_method_r: Get status for all readable elements
417 int ir_method_r (void *obj, Tcl_Interp *interp, int argc, char **argv,
425 for (; tab->name; tab++)
428 argv_n[1] = tab->name;
429 Tcl_AppendResult (interp, "{", NULL);
430 (*tab->method)(obj, interp, argc_n, argv_n);
431 Tcl_AppendResult (interp, "} ", NULL);
437 * ir_named_bits: get/set named bits
439 int ir_named_bits (struct ir_named_entry *tab, Odr_bitmask *ob,
440 Tcl_Interp *interp, int argc, char **argv)
442 struct ir_named_entry *ti;
447 for (no = 0; no < argc; no++)
449 for (ti = tab; ti->name; ti++)
450 if (!strcmp (argv[no], ti->name))
452 ODR_MASK_SET (ob, ti->pos);
457 Tcl_AppendResult (interp, "Bad bit mask: ", argv[no], NULL);
463 for (ti = tab; ti->name; ti++)
464 if (ODR_MASK_GET (ob, ti->pos))
465 Tcl_AppendElement (interp, ti->name);
469 static void set_referenceId (ODR o, Z_ReferenceId **dst, const char *src)
475 *dst = odr_malloc (o, sizeof(**dst));
476 (*dst)->size = (*dst)->len = strlen(src);
477 (*dst)->buf = odr_malloc (o, (*dst)->len);
478 memcpy ((*dst)->buf, src, (*dst)->len);
482 static void get_referenceId (char **dst, Z_ReferenceId *src)
490 *dst = ir_tcl_malloc (src->len+1);
491 memcpy (*dst, src->buf, src->len);
492 (*dst)[src->len] = '\0';
495 /* ------------------------------------------------------- */
498 * do_init_request: init method on IR object
500 static int do_init_request (void *obj, Tcl_Interp *interp,
501 int argc, char **argv)
511 interp->result = "init: not connected";
514 apdu = zget_APDU (p->odr_out, Z_APDU_initRequest);
515 req = apdu->u.initRequest;
517 set_referenceId (p->odr_out, &req->referenceId, p->set_inher.referenceId);
518 req->options = &p->options;
519 req->protocolVersion = &p->protocolVersion;
520 req->preferredMessageSize = &p->preferredMessageSize;
521 req->maximumRecordSize = &p->maximumRecordSize;
523 if (p->idAuthenticationGroupId)
525 Z_IdPass *pass = odr_malloc (p->odr_out, sizeof(*pass));
526 Z_IdAuthentication *auth = odr_malloc (p->odr_out, sizeof(*auth));
528 auth->which = Z_IdAuthentication_idPass;
529 auth->u.idPass = pass;
530 if (p->idAuthenticationGroupId && *p->idAuthenticationGroupId)
531 pass->groupId = p->idAuthenticationGroupId;
533 pass->groupId = NULL;
534 if (p->idAuthenticationUserId && *p->idAuthenticationUserId)
535 pass->userId = p->idAuthenticationUserId;
538 if (p->idAuthenticationPassword && *p->idAuthenticationPassword)
539 pass->password = p->idAuthenticationPassword;
541 pass->password = NULL;
542 req->idAuthentication = auth;
544 else if (!p->idAuthenticationOpen || !*p->idAuthenticationOpen)
545 req->idAuthentication = NULL;
548 Z_IdAuthentication *auth = odr_malloc (p->odr_out, sizeof(*auth));
550 auth->which = Z_IdAuthentication_open;
551 auth->u.open = p->idAuthenticationOpen;
552 req->idAuthentication = auth;
554 req->implementationId = p->implementationId;
555 req->implementationName = p->implementationName;
556 req->implementationVersion = p->implementationVersion;
557 req->userInformationField = 0;
559 return ir_tcl_send_APDU (interp, p, apdu, "init", argv[0]);
563 * do_protocolVersion: Set protocol Version
565 static int do_protocolVersion (void *obj, Tcl_Interp *interp,
566 int argc, char **argv)
574 ODR_MASK_ZERO (&p->protocolVersion);
575 ODR_MASK_SET (&p->protocolVersion, 0);
576 ODR_MASK_SET (&p->protocolVersion, 1);
581 if (Tcl_GetInt (interp, argv[2], &version)==TCL_ERROR)
583 ODR_MASK_ZERO (&p->protocolVersion);
584 for (i = 0; i<version; i++)
585 ODR_MASK_SET (&p->protocolVersion, i);
587 for (i = 4; --i >= 0; )
588 if (ODR_MASK_GET (&p->protocolVersion, i))
590 sprintf (buf, "%d", i+1);
591 interp->result = buf;
596 * do_options: Set options
598 static int do_options (void *obj, Tcl_Interp *interp,
599 int argc, char **argv)
601 static struct ir_named_entry options_tab[] = {
605 { "resourceReport", 3 },
606 { "triggerResourceCtrl", 4},
607 { "resourceCtrl", 5},
611 { "extendedServices", 10},
612 { "level-1Segmentation", 11},
613 { "level-2Segmentation", 12},
614 { "concurrentOperations", 13},
615 { "namedResultSets", 14},
622 ODR_MASK_ZERO (&p->options);
623 ODR_MASK_SET (&p->options, 0);
624 ODR_MASK_SET (&p->options, 1);
625 ODR_MASK_SET (&p->options, 4);
626 ODR_MASK_SET (&p->options, 7);
627 ODR_MASK_SET (&p->options, 14);
630 return ir_named_bits (options_tab, &p->options, interp, argc-2, argv+2);
634 * do_failInfo: Get fail information
636 static int do_failInfo (void *obj, Tcl_Interp *interp, int argc, char **argv)
646 sprintf (buf, "%d", p->failInfo);
652 case IR_TCL_FAIL_CONNECT:
653 cp = "connect failed";
655 case IR_TCL_FAIL_READ:
656 cp = "connection closed";
658 case IR_TCL_FAIL_WRITE:
659 cp = "connection closed";
661 case IR_TCL_FAIL_IN_APDU:
662 cp = "failed to decode incoming APDU";
664 case IR_TCL_FAIL_UNKNOWN_APDU:
670 Tcl_AppendElement (interp, buf);
671 Tcl_AppendElement (interp, cp);
676 * do_preferredMessageSize: Set/get preferred message size
678 static int do_preferredMessageSize (void *obj, Tcl_Interp *interp,
679 int argc, char **argv)
685 p->preferredMessageSize = 30000;
688 return get_set_int (&p->preferredMessageSize, interp, argc, argv);
692 * do_maximumRecordSize: Set/get maximum record size
694 static int do_maximumRecordSize (void *obj, Tcl_Interp *interp,
695 int argc, char **argv)
701 p->maximumRecordSize = 30000;
704 return get_set_int (&p->maximumRecordSize, interp, argc, argv);
708 * do_initResult: Get init result
710 static int do_initResult (void *obj, Tcl_Interp *interp,
711 int argc, char **argv)
717 return get_set_int (&p->initResult, interp, argc, argv);
722 * do_implementationName: Set/get Implementation Name.
724 static int do_implementationName (void *obj, Tcl_Interp *interp,
725 int argc, char **argv)
730 return ir_tcl_strdup (interp, &p->implementationName,
731 "Index Data/IrTcl on YAZ");
733 return ir_tcl_strdel (interp, &p->implementationName);
736 free (p->implementationName);
737 if (ir_tcl_strdup (interp, &p->implementationName, argv[2])
741 Tcl_AppendResult (interp, p->implementationName, (char*) NULL);
746 * do_implementationId: Get Implementation Id.
748 static int do_implementationId (void *obj, Tcl_Interp *interp,
749 int argc, char **argv)
754 return ir_tcl_strdup (interp, &p->implementationId, "YAZ (id=81)");
756 return ir_tcl_strdel (interp, &p->implementationId);
757 Tcl_AppendResult (interp, p->implementationId, (char*) NULL);
762 * do_implementationVersion: get Implementation Version.
764 static int do_implementationVersion (void *obj, Tcl_Interp *interp,
765 int argc, char **argv)
770 return ir_tcl_strdup (interp, &p->implementationVersion,
771 "YAZ: " YAZ_VERSION " / IrTcl: " IR_TCL_VERSION);
773 return ir_tcl_strdel (interp, &p->implementationVersion);
774 Tcl_AppendResult (interp, p->implementationVersion, (char*) NULL);
779 * do_targetImplementationName: Get Implementation Name of target.
781 static int do_targetImplementationName (void *obj, Tcl_Interp *interp,
782 int argc, char **argv)
788 p->targetImplementationName = NULL;
792 return ir_tcl_strdel (interp, &p->targetImplementationName);
793 Tcl_AppendResult (interp, p->targetImplementationName, (char*) NULL);
798 * do_targetImplementationId: Get Implementation Id of target
800 static int do_targetImplementationId (void *obj, Tcl_Interp *interp,
801 int argc, char **argv)
807 p->targetImplementationId = NULL;
811 return ir_tcl_strdel (interp, &p->targetImplementationId);
812 Tcl_AppendResult (interp, p->targetImplementationId, (char*) NULL);
817 * do_targetImplementationVersion: Get Implementation Version of target
819 static int do_targetImplementationVersion (void *obj, Tcl_Interp *interp,
820 int argc, char **argv)
826 p->targetImplementationVersion = NULL;
830 return ir_tcl_strdel (interp, &p->targetImplementationVersion);
831 Tcl_AppendResult (interp, p->targetImplementationVersion, (char*) NULL);
836 * do_idAuthentication: Set/get id Authentication
838 static int do_idAuthentication (void *obj, Tcl_Interp *interp,
839 int argc, char **argv)
843 if (argc >= 3 || argc == -1)
845 free (p->idAuthenticationOpen);
846 free (p->idAuthenticationGroupId);
847 free (p->idAuthenticationUserId);
848 free (p->idAuthenticationPassword);
850 if (argc >= 3 || argc <= 0)
852 p->idAuthenticationOpen = NULL;
853 p->idAuthenticationGroupId = NULL;
854 p->idAuthenticationUserId = NULL;
855 p->idAuthenticationPassword = NULL;
863 if (ir_tcl_strdup (interp, &p->idAuthenticationOpen, argv[2])
869 if (ir_tcl_strdup (interp, &p->idAuthenticationGroupId, argv[2])
872 if (ir_tcl_strdup (interp, &p->idAuthenticationUserId, argv[3])
875 if (ir_tcl_strdup (interp, &p->idAuthenticationPassword, argv[4])
880 if (p->idAuthenticationOpen)
881 Tcl_AppendElement (interp, p->idAuthenticationOpen);
882 else if (p->idAuthenticationGroupId)
884 Tcl_AppendElement (interp, p->idAuthenticationGroupId);
885 Tcl_AppendElement (interp, p->idAuthenticationUserId);
886 Tcl_AppendElement (interp, p->idAuthenticationPassword);
892 * do_connect: connect method on IR object
894 static int do_connect (void *obj, Tcl_Interp *interp,
895 int argc, char **argv)
907 interp->result = "already connected";
910 if (ir_tcl_strdup (interp, &p->hostname, argv[2]) == TCL_ERROR)
912 if (!strcmp (p->cs_type, "tcpip"))
914 p->cs_link = cs_create (tcpip_type, CS_BLOCK, p->protocol_type);
915 addr = tcpip_strtoaddr (argv[2]);
918 interp->result = "tcpip_strtoaddr fail";
921 logf (LOG_DEBUG, "tcp/ip connect %s", argv[2]);
923 else if (!strcmp (p->cs_type, "mosi"))
926 p->cs_link = cs_create (mosi_type, CS_BLOCK, p->protocol_type);
927 addr = mosi_strtoaddr (argv[2]);
930 interp->result = "mosi_strtoaddr fail";
933 logf (LOG_DEBUG, "mosi connect %s", argv[2]);
935 interp->result = "MOSI support not there";
941 Tcl_AppendResult (interp, "Bad comstack type: ",
945 if ((r=cs_connect (p->cs_link, addr)) < 0)
947 interp->result = "connect fail";
948 do_disconnect (p, NULL, 2, NULL);
951 ir_select_add (cs_fileno (p->cs_link), p);
954 ir_select_add_write (cs_fileno (p->cs_link), p);
955 p->state = IR_TCL_R_Connecting;
959 p->state = IR_TCL_R_Idle;
961 IrTcl_eval (p->interp, p->callback);
965 Tcl_AppendResult (interp, p->hostname, NULL);
970 * do_disconnect: disconnect method on IR object
972 static int do_disconnect (void *obj, Tcl_Interp *interp,
973 int argc, char **argv)
979 p->state = IR_TCL_R_Idle;
988 ir_select_remove_write (cs_fileno (p->cs_link), p);
989 ir_select_remove (cs_fileno (p->cs_link), p);
991 odr_reset (p->odr_in);
994 cs_close (p->cs_link);
997 ODR_MASK_ZERO (&p->options);
998 ODR_MASK_SET (&p->options, 0);
999 ODR_MASK_SET (&p->options, 1);
1000 ODR_MASK_SET (&p->options, 4);
1001 ODR_MASK_SET (&p->options, 7);
1002 ODR_MASK_SET (&p->options, 14);
1004 ODR_MASK_ZERO (&p->protocolVersion);
1005 ODR_MASK_SET (&p->protocolVersion, 0);
1006 ODR_MASK_SET (&p->protocolVersion, 1);
1009 assert (!p->cs_link);
1014 * do_comstack: Set/get comstack method on IR object
1016 static int do_comstack (void *o, Tcl_Interp *interp,
1017 int argc, char **argv)
1022 return ir_tcl_strdup (interp, &obj->cs_type, "tcpip");
1023 else if (argc == -1)
1024 return ir_tcl_strdel (interp, &obj->cs_type);
1027 free (obj->cs_type);
1028 if (ir_tcl_strdup (interp, &obj->cs_type, argv[2]) == TCL_ERROR)
1031 Tcl_AppendElement (interp, obj->cs_type);
1036 * do_logLevel: Set log level
1038 static int do_logLevel (void *o, Tcl_Interp *interp,
1039 int argc, char **argv)
1044 log_init (log_mask_str (argv[2]), "", NULL);
1046 log_init (log_mask_str (argv[2]), argv[3], NULL);
1048 log_init (log_mask_str (argv[2]), argv[3], argv[4]);
1054 * do_callback: add callback
1056 static int do_callback (void *obj, Tcl_Interp *interp,
1057 int argc, char **argv)
1066 else if (argc == -1)
1067 return ir_tcl_strdel (interp, &p->callback);
1073 if (ir_tcl_strdup (interp, &p->callback, argv[2]) == TCL_ERROR)
1083 * do_failback: add error handle callback
1085 static int do_failback (void *obj, Tcl_Interp *interp,
1086 int argc, char **argv)
1095 else if (argc == -1)
1096 return ir_tcl_strdel (interp, &p->failback);
1102 if (ir_tcl_strdup (interp, &p->failback, argv[2]) == TCL_ERROR)
1112 * do_protocol: Set/get protocol method on IR object
1114 static int do_protocol (void *o, Tcl_Interp *interp, int argc, char **argv)
1120 p->protocol_type = PROTO_Z3950;
1125 if (!strcmp (argv[2], "Z39"))
1126 p->protocol_type = PROTO_Z3950;
1127 else if (!strcmp (argv[2], "SR"))
1128 p->protocol_type = PROTO_SR;
1131 Tcl_AppendResult (interp, "Bad protocol: ", argv[2], NULL);
1136 switch (p->protocol_type)
1139 Tcl_AppendElement (interp, "Z39");
1142 Tcl_AppendElement (interp, "SR");
1149 * do_triggerResourceControl:
1151 static int do_triggerResourceControl (void *obj, Tcl_Interp *interp,
1152 int argc, char **argv)
1156 Z_TriggerResourceControlRequest *req;
1157 bool_t is_false = 0;
1163 interp->result = "triggerResourceControl: not connected";
1166 apdu = zget_APDU (p->odr_out, Z_APDU_triggerResourceControlRequest);
1167 req = apdu->u.triggerResourceControlRequest;
1168 *req->requestedAction = Z_TriggerResourceCtrl_cancel;
1169 req->resultSetWanted = &is_false;
1171 return ir_tcl_send_APDU (interp, p, apdu, "triggerResourceControl",
1176 * do_databaseNames: specify database names
1178 static int do_databaseNames (void *obj, Tcl_Interp *interp,
1179 int argc, char **argv)
1182 IrTcl_SetCObj *p = obj;
1186 for (i=0; i<p->num_databaseNames; i++)
1187 free (p->databaseNames[i]);
1188 free (p->databaseNames);
1192 p->num_databaseNames = 0;
1193 p->databaseNames = NULL;
1198 for (i=0; i<p->num_databaseNames; i++)
1199 Tcl_AppendElement (interp, p->databaseNames[i]);
1202 if (p->databaseNames)
1204 for (i=0; i<p->num_databaseNames; i++)
1205 free (p->databaseNames[i]);
1206 free (p->databaseNames);
1208 p->num_databaseNames = argc - 2;
1210 ir_tcl_malloc (sizeof(*p->databaseNames) * p->num_databaseNames);
1211 for (i=0; i<p->num_databaseNames; i++)
1213 if (ir_tcl_strdup (interp, &p->databaseNames[i], argv[2+i])
1221 * do_replaceIndicator: Set/get replace Set indicator
1223 static int do_replaceIndicator (void *obj, Tcl_Interp *interp,
1224 int argc, char **argv)
1226 IrTcl_SetCObj *p = obj;
1230 p->replaceIndicator = 1;
1233 return get_set_int (&p->replaceIndicator, interp, argc, argv);
1237 * do_queryType: Set/Get query method
1239 static int do_queryType (void *obj, Tcl_Interp *interp,
1240 int argc, char **argv)
1242 IrTcl_SetCObj *p = obj;
1245 return ir_tcl_strdup (interp, &p->queryType, "rpn");
1246 else if (argc == -1)
1247 return ir_tcl_strdel (interp, &p->queryType);
1250 free (p->queryType);
1251 if (ir_tcl_strdup (interp, &p->queryType, argv[2]) == TCL_ERROR)
1254 Tcl_AppendResult (interp, p->queryType, NULL);
1259 * do_userInformationField: Get User information field
1261 static int do_userInformationField (void *obj, Tcl_Interp *interp,
1262 int argc, char **argv)
1268 p->userInformationField = NULL;
1271 else if (argc == -1)
1272 return ir_tcl_strdel (interp, &p->userInformationField);
1273 Tcl_AppendResult (interp, p->userInformationField, NULL);
1278 * do_smallSetUpperBound: Set/get small set upper bound
1280 static int do_smallSetUpperBound (void *o, Tcl_Interp *interp,
1281 int argc, char **argv)
1283 IrTcl_SetCObj *p = o;
1287 p->smallSetUpperBound = 0;
1290 return get_set_int (&p->smallSetUpperBound, interp, argc, argv);
1294 * do_largeSetLowerBound: Set/get large set lower bound
1296 static int do_largeSetLowerBound (void *o, Tcl_Interp *interp,
1297 int argc, char **argv)
1299 IrTcl_SetCObj *p = o;
1303 p->largeSetLowerBound = 2;
1306 return get_set_int (&p->largeSetLowerBound, interp, argc, argv);
1310 * do_mediumSetPresentNumber: Set/get large set lower bound
1312 static int do_mediumSetPresentNumber (void *o, Tcl_Interp *interp,
1313 int argc, char **argv)
1315 IrTcl_SetCObj *p = o;
1319 p->mediumSetPresentNumber = 0;
1322 return get_set_int (&p->mediumSetPresentNumber, interp, argc, argv);
1326 * do_referenceId: Set/Get referenceId
1328 static int do_referenceId (void *obj, Tcl_Interp *interp,
1329 int argc, char **argv)
1331 IrTcl_SetCObj *p = obj;
1335 p->referenceId = NULL;
1338 else if (argc == -1)
1339 return ir_tcl_strdel (interp, &p->referenceId);
1342 free (p->referenceId);
1343 if (ir_tcl_strdup (interp, &p->referenceId, argv[2]) == TCL_ERROR)
1346 Tcl_AppendResult (interp, p->referenceId, NULL);
1351 * do_preferredRecordSyntax: Set/get preferred record syntax
1353 static int do_preferredRecordSyntax (void *obj, Tcl_Interp *interp,
1354 int argc, char **argv)
1356 IrTcl_SetCObj *p = obj;
1360 p->preferredRecordSyntax = NULL;
1363 else if (argc == -1)
1365 free (p->preferredRecordSyntax);
1366 p->preferredRecordSyntax = NULL;
1371 free (p->preferredRecordSyntax);
1372 p->preferredRecordSyntax = NULL;
1373 if (argv[2][0] && (p->preferredRecordSyntax =
1374 ir_tcl_malloc (sizeof(*p->preferredRecordSyntax))))
1375 *p->preferredRecordSyntax = IrTcl_getRecordSyntaxVal (argv[2]);
1382 * do_elementSetNames: Set/Get element Set Names
1384 static int do_elementSetNames (void *obj, Tcl_Interp *interp,
1385 int argc, char **argv)
1387 IrTcl_SetCObj *p = obj;
1391 p->elementSetNames = NULL;
1394 else if (argc == -1)
1395 return ir_tcl_strdel (interp, &p->elementSetNames);
1398 free (p->elementSetNames);
1399 if (ir_tcl_strdup (interp, &p->elementSetNames, argv[2]) == TCL_ERROR)
1402 Tcl_AppendResult (interp, p->elementSetNames, NULL);
1407 static IrTcl_Method ir_method_tab[] = {
1408 { 1, "comstack", do_comstack },
1409 { 1, "protocol", do_protocol },
1410 { 0, "failback", do_failback },
1411 { 0, "failInfo", do_failInfo },
1412 { 0, "logLevel", do_logLevel },
1414 { 1, "connect", do_connect },
1415 { 0, "protocolVersion", do_protocolVersion },
1416 { 1, "preferredMessageSize", do_preferredMessageSize },
1417 { 1, "maximumRecordSize", do_maximumRecordSize },
1418 { 1, "implementationName", do_implementationName },
1419 { 1, "implementationId", do_implementationId },
1420 { 1, "implementationVersion", do_implementationVersion },
1421 { 0, "targetImplementationName", do_targetImplementationName },
1422 { 0, "targetImplementationId", do_targetImplementationId },
1423 { 0, "targetImplementationVersion", do_targetImplementationVersion },
1424 { 0, "userInformationField", do_userInformationField },
1425 { 1, "idAuthentication", do_idAuthentication },
1426 { 0, "options", do_options },
1427 { 0, "init", do_init_request },
1428 { 0, "initResult", do_initResult },
1429 { 0, "disconnect", do_disconnect },
1430 { 0, "callback", do_callback },
1431 { 0, "triggerResourceControl", do_triggerResourceControl },
1435 static IrTcl_Method ir_set_c_method_tab[] = {
1436 { 0, "databaseNames", do_databaseNames},
1437 { 0, "replaceIndicator", do_replaceIndicator},
1438 { 0, "queryType", do_queryType },
1439 { 0, "preferredRecordSyntax", do_preferredRecordSyntax },
1440 { 0, "smallSetUpperBound", do_smallSetUpperBound},
1441 { 0, "largeSetLowerBound", do_largeSetLowerBound},
1442 { 0, "mediumSetPresentNumber", do_mediumSetPresentNumber},
1443 { 0, "referenceId", do_referenceId },
1444 { 0, "elementSetNames", do_elementSetNames },
1449 * ir_obj_method: IR Object methods
1451 static int ir_obj_method (ClientData clientData, Tcl_Interp *interp,
1452 int argc, char **argv)
1454 IrTcl_Methods tab[3];
1455 IrTcl_Obj *p = clientData;
1458 return ir_method_r (clientData, interp, argc, argv, ir_method_tab);
1460 tab[0].tab = ir_method_tab;
1462 tab[1].tab = ir_set_c_method_tab;
1463 tab[1].obj = &p->set_inher;
1466 return ir_method (interp, argc, argv, tab);
1470 * ir_obj_delete: IR Object disposal
1472 static void ir_obj_delete (ClientData clientData)
1474 IrTcl_Obj *obj = clientData;
1475 IrTcl_Methods tab[3];
1478 if (obj->ref_count > 0)
1480 assert (obj->ref_count == 0);
1482 logf (LOG_DEBUG, "ir object delete");
1483 tab[0].tab = ir_method_tab;
1485 tab[1].tab = ir_set_c_method_tab;
1486 tab[1].obj = &obj->set_inher;
1489 ir_method (NULL, -1, NULL, tab);
1492 odr_destroy (obj->odr_in);
1493 odr_destroy (obj->odr_out);
1494 odr_destroy (obj->odr_pr);
1499 * ir_obj_mk: IR Object creation
1501 static int ir_obj_mk (ClientData clientData, Tcl_Interp *interp,
1502 int argc, char **argv)
1504 IrTcl_Methods tab[3];
1512 interp->result = "wrong # args";
1515 obj = ir_tcl_malloc (sizeof(*obj));
1518 obj->bibset = ccl_qual_mk ();
1519 if ((inf = fopen ("default.bib", "r")))
1521 ccl_qual_file (obj->bibset, inf);
1526 logf (LOG_DEBUG, "ir object create");
1527 obj->odr_in = odr_createmem (ODR_DECODE);
1528 obj->odr_out = odr_createmem (ODR_ENCODE);
1529 obj->odr_pr = odr_createmem (ODR_PRINT);
1530 obj->state = IR_TCL_R_Idle;
1531 obj->interp = interp;
1535 obj->request_queue = NULL;
1537 tab[0].tab = ir_method_tab;
1539 tab[1].tab = ir_set_c_method_tab;
1540 tab[1].obj = &obj->set_inher;
1543 if (ir_method (interp, 0, NULL, tab) == TCL_ERROR)
1545 Tcl_CreateCommand (interp, argv[1], ir_obj_method,
1546 (ClientData) obj, ir_obj_delete);
1550 /* ------------------------------------------------------- */
1552 * do_search: Do search request
1554 static int do_search (void *o, Tcl_Interp *interp, int argc, char **argv)
1556 Z_SearchRequest *req;
1560 IrTcl_SetObj *obj = o;
1571 interp->result = "wrong # args";
1574 if (!obj->set_inher.num_databaseNames)
1576 interp->result = "no databaseNames";
1581 interp->result = "search: not connected";
1584 apdu = zget_APDU (p->odr_out, Z_APDU_searchRequest);
1585 req = apdu->u.searchRequest;
1587 bib1.proto = p->protocol_type;
1588 bib1.class = CLASS_ATTSET;
1589 bib1.value = VAL_BIB1;
1591 set_referenceId (p->odr_out, &req->referenceId,
1592 obj->set_inher.referenceId);
1594 req->smallSetUpperBound = &obj->set_inher.smallSetUpperBound;
1595 req->largeSetLowerBound = &obj->set_inher.largeSetLowerBound;
1596 req->mediumSetPresentNumber = &obj->set_inher.mediumSetPresentNumber;
1597 req->replaceIndicator = &obj->set_inher.replaceIndicator;
1598 req->resultSetName = obj->setName ? obj->setName : "Default";
1599 logf (LOG_DEBUG, "Search, resultSetName %s", req->resultSetName);
1600 req->num_databaseNames = obj->set_inher.num_databaseNames;
1601 req->databaseNames = obj->set_inher.databaseNames;
1602 for (r=0; r < obj->set_inher.num_databaseNames; r++)
1603 logf (LOG_DEBUG, " Database %s", obj->set_inher.databaseNames[r]);
1604 req->smallSetElementSetNames = 0;
1605 req->mediumSetElementSetNames = 0;
1606 if (obj->set_inher.preferredRecordSyntax)
1608 struct oident ident;
1610 ident.proto = p->protocol_type;
1611 ident.class = CLASS_RECSYN;
1612 ident.value = *obj->set_inher.preferredRecordSyntax;
1613 logf (LOG_DEBUG, "Preferred record syntax is %d", ident.value);
1614 req->preferredRecordSyntax = odr_oiddup (p->odr_out,
1615 oid_getoidbyent (&ident));
1618 req->preferredRecordSyntax = 0;
1620 if (obj->set_inher.elementSetNames && *obj->set_inher.elementSetNames)
1622 Z_ElementSetNames *esn = odr_malloc (p->odr_out, sizeof(*esn));
1624 esn->which = Z_ElementSetNames_generic;
1625 esn->u.generic = obj->set_inher.elementSetNames;
1626 req->mediumSetElementSetNames = esn;
1629 req->mediumSetElementSetNames = NULL;
1631 req->query = &query;
1633 if (!strcmp (obj->set_inher.queryType, "rpn"))
1635 Z_RPNQuery *RPNquery;
1637 RPNquery = p_query_rpn (p->odr_out, argv[2]);
1640 Tcl_AppendResult (interp, "Syntax error in query", NULL);
1643 RPNquery->attributeSetId = oid_getoidbyent (&bib1);
1644 query.which = Z_Query_type_1;
1645 query.u.type_1 = RPNquery;
1646 logf (LOG_DEBUG, "RPN");
1649 else if (!strcmp (obj->set_inher.queryType, "cclrpn"))
1653 struct ccl_rpn_node *rpn;
1654 Z_RPNQuery *RPNquery;
1656 rpn = ccl_find_str(p->bibset, argv[2], &error, &pos);
1659 Tcl_AppendResult (interp, "CCL error: ",
1660 ccl_err_msg(error), NULL);
1663 ccl_pr_tree (rpn, stderr);
1664 fprintf (stderr, "\n");
1665 assert((RPNquery = ccl_rpn_query(rpn)));
1666 RPNquery->attributeSetId = oid_getoidbyent (&bib1);
1667 query.which = Z_Query_type_1;
1668 query.u.type_1 = RPNquery;
1669 logf (LOG_DEBUG, "CCLRPN");
1672 else if (!strcmp (obj->set_inher.queryType, "ccl"))
1674 query.which = Z_Query_type_2;
1675 query.u.type_2 = &ccl_query;
1676 ccl_query.buf = (unsigned char *) argv[2];
1677 ccl_query.len = strlen (argv[2]);
1678 logf (LOG_DEBUG, "CCL");
1682 interp->result = "unknown query method";
1685 return ir_tcl_send_APDU (interp, p, apdu, "search", argv[0]);
1689 * do_resultCount: Get number of hits
1691 static int do_resultCount (void *o, Tcl_Interp *interp,
1692 int argc, char **argv)
1694 IrTcl_SetObj *obj = o;
1698 return get_set_int (&obj->resultCount, interp, argc, argv);
1702 * do_searchStatus: Get search status (after search response)
1704 static int do_searchStatus (void *o, Tcl_Interp *interp,
1705 int argc, char **argv)
1707 IrTcl_SetObj *obj = o;
1711 return get_set_int (&obj->searchStatus, interp, argc, argv);
1715 * do_presentStatus: Get search status (after search/present response)
1717 static int do_presentStatus (void *o, Tcl_Interp *interp,
1718 int argc, char **argv)
1720 IrTcl_SetObj *obj = o;
1724 return get_set_int (&obj->presentStatus, interp, argc, argv);
1728 * do_nextResultSetPosition: Get next result set position
1729 * (after search/present response)
1731 static int do_nextResultSetPosition (void *o, Tcl_Interp *interp,
1732 int argc, char **argv)
1734 IrTcl_SetObj *obj = o;
1738 obj->nextResultSetPosition = 0;
1741 return get_set_int (&obj->nextResultSetPosition, interp, argc, argv);
1745 * do_setName: Set result Set name
1747 static int do_setName (void *o, Tcl_Interp *interp,
1748 int argc, char **argv)
1750 IrTcl_SetObj *obj = o;
1753 return ir_tcl_strdup (interp, &obj->setName, "Default");
1754 else if (argc == -1)
1755 return ir_tcl_strdel (interp, &obj->setName);
1758 free (obj->setName);
1759 if (ir_tcl_strdup (interp, &obj->setName, argv[2])
1763 Tcl_AppendElement (interp, obj->setName);
1768 * do_numberOfRecordsReturned: Get number of records returned
1770 static int do_numberOfRecordsReturned (void *o, Tcl_Interp *interp,
1771 int argc, char **argv)
1773 IrTcl_SetObj *obj = o;
1777 obj->numberOfRecordsReturned = 0;
1780 return get_set_int (&obj->numberOfRecordsReturned, interp, argc, argv);
1784 * do_type: Return type (if any) at position.
1786 static int do_type (void *o, Tcl_Interp *interp, int argc, char **argv)
1788 IrTcl_SetObj *obj = o;
1790 IrTcl_RecordList *rl;
1794 obj->record_list = NULL;
1797 else if (argc == -1)
1799 delete_IR_records (obj);
1804 sprintf (interp->result, "wrong # args");
1807 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
1809 rl = find_IR_record (obj, offset);
1814 case Z_NamePlusRecord_databaseRecord:
1815 interp->result = "DB";
1817 case Z_NamePlusRecord_surrogateDiagnostic:
1818 interp->result = "SD";
1826 * do_recordType: Return record type (if any) at position.
1828 static int do_recordType (void *o, Tcl_Interp *interp, int argc, char **argv)
1830 IrTcl_SetObj *obj = o;
1832 IrTcl_RecordList *rl;
1838 else if (argc == -1)
1844 sprintf (interp->result, "wrong # args");
1847 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
1849 rl = find_IR_record (obj, offset);
1852 if (rl->which != Z_NamePlusRecord_databaseRecord)
1854 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
1857 Tcl_AppendElement (interp, (char*)
1858 IrTcl_getRecordSyntaxStr (rl->u.dbrec.type));
1865 static int ir_diagResult (Tcl_Interp *interp, IrTcl_Diagnostic *list, int num)
1871 for (i = 0; i<num; i++)
1873 logf (LOG_DEBUG, "Diagnostic, code %d", list[i].condition);
1874 sprintf (buf, "%d", list[i].condition);
1875 Tcl_AppendElement (interp, buf);
1876 cp = diagbib1_str (list[i].condition);
1878 Tcl_AppendElement (interp, (char*) cp);
1880 Tcl_AppendElement (interp, "");
1881 if (list[i].addinfo)
1882 Tcl_AppendElement (interp, (char*) list[i].addinfo);
1884 Tcl_AppendElement (interp, "");
1890 * do_diag: Return diagnostic record info
1892 static int do_diag (void *o, Tcl_Interp *interp, int argc, char **argv)
1894 IrTcl_SetObj *obj = o;
1896 IrTcl_RecordList *rl;
1902 sprintf (interp->result, "wrong # args");
1905 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
1907 rl = find_IR_record (obj, offset);
1910 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
1913 if (rl->which != Z_NamePlusRecord_surrogateDiagnostic)
1915 Tcl_AppendResult (interp, "No Diagnostic record at #", argv[2], NULL);
1918 return ir_diagResult (interp, rl->u.surrogateDiagnostics.list,
1919 rl->u.surrogateDiagnostics.num);
1923 * do_getMarc: Get ISO2709 Record lines/fields
1925 static int do_getMarc (void *o, Tcl_Interp *interp, int argc, char **argv)
1927 IrTcl_SetObj *obj = o;
1929 IrTcl_RecordList *rl;
1935 sprintf (interp->result, "wrong # args");
1938 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
1940 rl = find_IR_record (obj, offset);
1943 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
1946 if (rl->which != Z_NamePlusRecord_databaseRecord)
1948 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
1951 return ir_tcl_get_marc (interp, rl->u.dbrec.buf, argc, argv);
1955 * do_getSutrs: Get SUTRS Record
1957 static int do_getSutrs (void *o, Tcl_Interp *interp, int argc, char **argv)
1959 IrTcl_SetObj *obj = o;
1961 IrTcl_RecordList *rl;
1967 sprintf (interp->result, "wrong # args");
1970 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
1972 rl = find_IR_record (obj, offset);
1975 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
1978 if (rl->which != Z_NamePlusRecord_databaseRecord)
1980 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
1983 if (rl->u.dbrec.type != VAL_SUTRS)
1985 Tcl_AppendElement (interp, rl->u.dbrec.buf);
1991 * do_getGrs: Get a GRS1 Record
1993 static int do_getGrs (void *o, Tcl_Interp *interp, int argc, char **argv)
1995 IrTcl_SetObj *obj = o;
1997 IrTcl_RecordList *rl;
2003 sprintf (interp->result, "wrong # args");
2006 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2008 rl = find_IR_record (obj, offset);
2011 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2014 if (rl->which != Z_NamePlusRecord_databaseRecord)
2016 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2019 if (rl->u.dbrec.type != VAL_GRS1)
2021 return ir_tcl_get_grs (interp, rl->u.dbrec.u.grs1, argc, argv);
2026 * do_responseStatus: Return response status (present or search)
2028 static int do_responseStatus (void *o, Tcl_Interp *interp,
2029 int argc, char **argv)
2031 IrTcl_SetObj *obj = o;
2035 obj->recordFlag = 0;
2036 obj->nonSurrogateDiagnosticNum = 0;
2037 obj->nonSurrogateDiagnosticList = NULL;
2040 else if (argc == -1)
2042 ir_deleteDiags (&obj->nonSurrogateDiagnosticList,
2043 &obj->nonSurrogateDiagnosticNum);
2046 if (!obj->recordFlag)
2048 Tcl_AppendElement (interp, "OK");
2053 case Z_Records_DBOSD:
2054 Tcl_AppendElement (interp, "DBOSD");
2057 Tcl_AppendElement (interp, "NSD");
2058 return ir_diagResult (interp, obj->nonSurrogateDiagnosticList,
2059 obj->nonSurrogateDiagnosticNum);
2065 * do_present: Perform Present Request
2068 static int do_present (void *o, Tcl_Interp *interp, int argc, char **argv)
2070 IrTcl_SetObj *obj = o;
2073 Z_PresentRequest *req;
2081 if (Tcl_GetInt (interp, argv[2], &start) == TCL_ERROR)
2088 if (Tcl_GetInt (interp, argv[3], &number) == TCL_ERROR)
2096 interp->result = "present: not connected";
2101 obj->number = number;
2103 apdu = zget_APDU (p->odr_out, Z_APDU_presentRequest);
2104 req = apdu->u.presentRequest;
2106 set_referenceId (p->odr_out, &req->referenceId,
2107 obj->set_inher.referenceId);
2109 req->resultSetId = obj->setName ? obj->setName : "Default";
2111 req->resultSetStartPoint = &start;
2112 req->numberOfRecordsRequested = &number;
2113 if (obj->set_inher.preferredRecordSyntax)
2115 struct oident ident;
2117 ident.proto = p->protocol_type;
2118 ident.class = CLASS_RECSYN;
2119 ident.value = *obj->set_inher.preferredRecordSyntax;
2120 logf (LOG_DEBUG, "Preferred record syntax is %d", ident.value);
2121 req->preferredRecordSyntax = odr_oiddup (p->odr_out,
2122 oid_getoidbyent (&ident));
2125 req->preferredRecordSyntax = 0;
2126 if (obj->set_inher.elementSetNames && *obj->set_inher.elementSetNames)
2128 Z_ElementSetNames *esn = odr_malloc (p->odr_out, sizeof(*esn));
2129 Z_RecordComposition *compo = odr_malloc (p->odr_out, sizeof(*compo));
2131 esn->which = Z_ElementSetNames_generic;
2132 esn->u.generic = obj->set_inher.elementSetNames;
2134 req->recordComposition = compo;
2135 compo->which = Z_RecordComp_simple;
2136 compo->u.simple = esn;
2139 req->recordComposition = NULL;
2140 return ir_tcl_send_APDU (interp, p, apdu, "present", argv[0]);
2144 * do_loadFile: Load result set from file
2147 static int do_loadFile (void *o, Tcl_Interp *interp,
2148 int argc, char **argv)
2150 IrTcl_SetObj *setobj = o;
2160 interp->result = "wrong # args";
2163 inf = fopen (argv[2], "r");
2166 Tcl_AppendResult (interp, "Cannot open ", argv[2], NULL);
2169 while ((buf = ir_tcl_fread_marc (inf, &size)))
2171 IrTcl_RecordList *rl;
2173 rl = new_IR_record (setobj, no, Z_NamePlusRecord_databaseRecord);
2174 rl->u.dbrec.type = VAL_USMARC;
2175 rl->u.dbrec.buf = buf;
2176 rl->u.dbrec.size = size;
2179 setobj->numberOfRecordsReturned = no-1;
2184 static IrTcl_Method ir_set_method_tab[] = {
2185 { 0, "search", do_search },
2186 { 0, "searchStatus", do_searchStatus },
2187 { 0, "presentStatus", do_presentStatus },
2188 { 0, "nextResultSetPosition", do_nextResultSetPosition },
2189 { 0, "setName", do_setName },
2190 { 0, "resultCount", do_resultCount },
2191 { 0, "numberOfRecordsReturned", do_numberOfRecordsReturned },
2192 { 0, "present", do_present },
2193 { 0, "type", do_type },
2194 { 0, "getMarc", do_getMarc },
2195 { 0, "getSutrs", do_getSutrs },
2196 { 0, "getGrs", do_getGrs },
2197 { 0, "recordType", do_recordType },
2198 { 0, "diag", do_diag },
2199 { 0, "responseStatus", do_responseStatus },
2200 { 0, "loadFile", do_loadFile },
2205 * ir_set_obj_method: IR Set Object methods
2207 static int ir_set_obj_method (ClientData clientData, Tcl_Interp *interp,
2208 int argc, char **argv)
2210 IrTcl_Methods tabs[3];
2211 IrTcl_SetObj *p = clientData;
2215 interp->result = "wrong # args";
2218 tabs[0].tab = ir_set_method_tab;
2220 tabs[1].tab = ir_set_c_method_tab;
2221 tabs[1].obj = &p->set_inher;
2224 return ir_method (interp, argc, argv, tabs);
2228 * ir_set_obj_delete: IR Set Object disposal
2230 static void ir_set_obj_delete (ClientData clientData)
2232 IrTcl_Methods tabs[3];
2233 IrTcl_SetObj *p = clientData;
2235 logf (LOG_DEBUG, "ir set delete");
2237 tabs[0].tab = ir_set_method_tab;
2239 tabs[1].tab = ir_set_c_method_tab;
2240 tabs[1].obj = &p->set_inher;
2243 ir_method (NULL, -1, NULL, tabs);
2249 * ir_set_obj_mk: IR Set Object creation
2251 static int ir_set_obj_mk (ClientData clientData, Tcl_Interp *interp,
2252 int argc, char **argv)
2254 IrTcl_Methods tabs[3];
2257 if (argc < 2 || argc > 3)
2259 interp->result = "wrong # args";
2262 obj = ir_tcl_malloc (sizeof(*obj));
2263 logf (LOG_DEBUG, "ir set create");
2266 Tcl_CmdInfo parent_info;
2271 if (!Tcl_GetCommandInfo (interp, argv[2], &parent_info))
2273 interp->result = "No parent";
2276 obj->parent = (IrTcl_Obj *) parent_info.clientData;
2278 dst = &obj->set_inher;
2279 src = &obj->parent->set_inher;
2281 if ((dst->num_databaseNames = src->num_databaseNames))
2282 dst->databaseNames =
2283 ir_tcl_malloc (sizeof (*dst->databaseNames)
2284 * dst->num_databaseNames);
2286 dst->databaseNames = NULL;
2287 for (i = 0; i < dst->num_databaseNames; i++)
2288 if (ir_tcl_strdup (interp, &dst->databaseNames[i],
2289 src->databaseNames[i]) == TCL_ERROR)
2291 if (ir_tcl_strdup (interp, &dst->queryType, src->queryType)
2295 if (ir_tcl_strdup (interp, &dst->referenceId, src->referenceId)
2299 if (ir_tcl_strdup (interp, &dst->elementSetNames, src->elementSetNames)
2303 if (src->preferredRecordSyntax &&
2304 (dst->preferredRecordSyntax
2305 = ir_tcl_malloc (sizeof(*dst->preferredRecordSyntax))))
2306 *dst->preferredRecordSyntax = *src->preferredRecordSyntax;
2308 dst->preferredRecordSyntax = NULL;
2309 dst->replaceIndicator = src->replaceIndicator;
2310 dst->smallSetUpperBound = src->smallSetUpperBound;
2311 dst->largeSetLowerBound = src->largeSetLowerBound;
2312 dst->mediumSetPresentNumber = src->mediumSetPresentNumber;
2317 tabs[0].tab = ir_set_method_tab;
2321 if (ir_method (interp, 0, NULL, tabs) == TCL_ERROR)
2324 Tcl_CreateCommand (interp, argv[1], ir_set_obj_method,
2325 (ClientData) obj, ir_set_obj_delete);
2329 /* ------------------------------------------------------- */
2332 * do_scan: Perform scan
2334 static int do_scan (void *o, Tcl_Interp *interp, int argc, char **argv)
2338 IrTcl_ScanObj *obj = o;
2339 IrTcl_Obj *p = obj->parent;
2342 struct ccl_rpn_node *rpn;
2350 interp->result = "wrong # args";
2353 if (!p->set_inher.num_databaseNames)
2355 interp->result = "no databaseNames";
2360 interp->result = "scan: not connected";
2364 bib1.proto = p->protocol_type;
2365 bib1.class = CLASS_ATTSET;
2366 bib1.value = VAL_BIB1;
2368 apdu = zget_APDU (p->odr_out, Z_APDU_scanRequest);
2369 req = apdu->u.scanRequest;
2371 set_referenceId (p->odr_out, &req->referenceId, p->set_inher.referenceId);
2372 req->num_databaseNames = p->set_inher.num_databaseNames;
2373 req->databaseNames = p->set_inher.databaseNames;
2374 req->attributeSet = oid_getoidbyent (&bib1);
2377 if (!(req->termListAndStartPoint = p_query_scan (p->odr_out, argv[2])))
2379 Tcl_AppendResult (interp, "Syntax error in query", NULL);
2383 rpn = ccl_find_str(p->bibset, argv[2], &r, &pos);
2386 Tcl_AppendResult (interp, "CCL error: ", ccl_err_msg (r), NULL);
2389 ccl_pr_tree (rpn, stderr);
2390 fprintf (stderr, "\n");
2391 if (!(req->termListAndStartPoint = ccl_scan_query (rpn)))
2394 req->stepSize = &obj->stepSize;
2395 req->numberOfTermsRequested = &obj->numberOfTermsRequested;
2396 req->preferredPositionInResponse = &obj->preferredPositionInResponse;
2397 logf (LOG_DEBUG, "stepSize=%d", *req->stepSize);
2398 logf (LOG_DEBUG, "numberOfTermsRequested=%d",
2399 *req->numberOfTermsRequested);
2400 logf (LOG_DEBUG, "preferredPositionInResponse=%d",
2401 *req->preferredPositionInResponse);
2403 return ir_tcl_send_APDU (interp, p, apdu, "scan", argv[0]);
2407 * do_stepSize: Set/get replace Step Size
2409 static int do_stepSize (void *obj, Tcl_Interp *interp,
2410 int argc, char **argv)
2412 IrTcl_ScanObj *p = obj;
2418 return get_set_int (&p->stepSize, interp, argc, argv);
2422 * do_numberOfTermsRequested: Set/get Number of Terms requested
2424 static int do_numberOfTermsRequested (void *obj, Tcl_Interp *interp,
2425 int argc, char **argv)
2427 IrTcl_ScanObj *p = obj;
2431 p->numberOfTermsRequested = 20;
2434 return get_set_int (&p->numberOfTermsRequested, interp, argc, argv);
2439 * do_preferredPositionInResponse: Set/get preferred Position
2441 static int do_preferredPositionInResponse (void *obj, Tcl_Interp *interp,
2442 int argc, char **argv)
2444 IrTcl_ScanObj *p = obj;
2448 p->preferredPositionInResponse = 1;
2451 return get_set_int (&p->preferredPositionInResponse, interp, argc, argv);
2455 * do_scanStatus: Get scan status
2457 static int do_scanStatus (void *obj, Tcl_Interp *interp,
2458 int argc, char **argv)
2460 IrTcl_ScanObj *p = obj;
2464 return get_set_int (&p->scanStatus, interp, argc, argv);
2468 * do_numberOfEntriesReturned: Get number of Entries returned
2470 static int do_numberOfEntriesReturned (void *obj, Tcl_Interp *interp,
2471 int argc, char **argv)
2473 IrTcl_ScanObj *p = obj;
2477 return get_set_int (&p->numberOfEntriesReturned, interp, argc, argv);
2481 * do_positionOfTerm: Get position of Term
2483 static int do_positionOfTerm (void *obj, Tcl_Interp *interp,
2484 int argc, char **argv)
2486 IrTcl_ScanObj *p = obj;
2490 return get_set_int (&p->positionOfTerm, interp, argc, argv);
2494 * do_scanLine: get Scan Line (surrogate or normal) after response
2496 static int do_scanLine (void *obj, Tcl_Interp *interp, int argc, char **argv)
2498 IrTcl_ScanObj *p = obj;
2504 p->entries_flag = 0;
2506 p->nonSurrogateDiagnosticNum = 0;
2507 p->nonSurrogateDiagnosticList = 0;
2510 else if (argc == -1)
2512 p->entries_flag = 0;
2513 /* release entries */
2516 ir_deleteDiags (&p->nonSurrogateDiagnosticList,
2517 &p->nonSurrogateDiagnosticNum);
2522 interp->result = "wrong # args";
2525 if (Tcl_GetInt (interp, argv[2], &i) == TCL_ERROR)
2527 if (!p->entries_flag || p->which != Z_ListEntries_entries || !p->entries
2528 || i >= p->num_entries || i < 0)
2530 switch (p->entries[i].which)
2532 case Z_Entry_termInfo:
2533 Tcl_AppendElement (interp, "T");
2534 if (p->entries[i].u.term.buf)
2535 Tcl_AppendElement (interp, p->entries[i].u.term.buf);
2537 Tcl_AppendElement (interp, "");
2538 sprintf (numstr, "%d", p->entries[i].u.term.globalOccurrences);
2539 Tcl_AppendElement (interp, numstr);
2541 case Z_Entry_surrogateDiagnostic:
2542 Tcl_AppendElement (interp, "SD");
2543 return ir_diagResult (interp, p->entries[i].u.diag.list,
2544 p->entries[i].u.diag.num);
2550 static IrTcl_Method ir_scan_method_tab[] = {
2551 { 0, "scan", do_scan },
2552 { 0, "stepSize", do_stepSize },
2553 { 0, "numberOfTermsRequested", do_numberOfTermsRequested },
2554 { 0, "preferredPositionInResponse", do_preferredPositionInResponse },
2555 { 0, "scanStatus", do_scanStatus },
2556 { 0, "numberOfEntriesReturned", do_numberOfEntriesReturned },
2557 { 0, "positionOfTerm", do_positionOfTerm },
2558 { 0, "scanLine", do_scanLine },
2563 * ir_scan_obj_method: IR Scan Object methods
2565 static int ir_scan_obj_method (ClientData clientData, Tcl_Interp *interp,
2566 int argc, char **argv)
2568 IrTcl_Methods tabs[2];
2572 interp->result = "wrong # args";
2575 tabs[0].tab = ir_scan_method_tab;
2576 tabs[0].obj = clientData;
2579 return ir_method (interp, argc, argv, tabs);
2583 * ir_scan_obj_delete: IR Scan Object disposal
2585 static void ir_scan_obj_delete (ClientData clientData)
2587 IrTcl_Methods tabs[2];
2588 IrTcl_ScanObj *obj = clientData;
2590 tabs[0].tab = ir_scan_method_tab;
2594 ir_method (NULL, -1, NULL, tabs);
2599 * ir_scan_obj_mk: IR Scan Object creation
2601 static int ir_scan_obj_mk (ClientData clientData, Tcl_Interp *interp,
2602 int argc, char **argv)
2604 Tcl_CmdInfo parent_info;
2606 IrTcl_Methods tabs[2];
2610 interp->result = "wrong # args";
2613 if (!Tcl_GetCommandInfo (interp, argv[2], &parent_info))
2615 interp->result = "No parent";
2618 obj = ir_tcl_malloc (sizeof(*obj));
2619 obj->parent = (IrTcl_Obj *) parent_info.clientData;
2621 tabs[0].tab = ir_scan_method_tab;
2625 if (ir_method (interp, 0, NULL, tabs) == TCL_ERROR)
2627 Tcl_CreateCommand (interp, argv[1], ir_scan_obj_method,
2628 (ClientData) obj, ir_scan_obj_delete);
2632 /* ------------------------------------------------------- */
2634 static void ir_initResponse (void *obj, Z_InitResponse *initrs)
2638 p->initResult = *initrs->result ? 1 : 0;
2639 if (!*initrs->result)
2640 logf (LOG_DEBUG, "Connection rejected by target");
2642 logf (LOG_DEBUG, "Connection accepted by target");
2644 get_referenceId (&p->set_inher.referenceId, initrs->referenceId);
2646 free (p->targetImplementationId);
2647 ir_tcl_strdup (p->interp, &p->targetImplementationId,
2648 initrs->implementationId);
2649 free (p->targetImplementationName);
2650 ir_tcl_strdup (p->interp, &p->targetImplementationName,
2651 initrs->implementationName);
2652 free (p->targetImplementationVersion);
2653 ir_tcl_strdup (p->interp, &p->targetImplementationVersion,
2654 initrs->implementationVersion);
2656 p->maximumRecordSize = *initrs->maximumRecordSize;
2657 p->preferredMessageSize = *initrs->preferredMessageSize;
2659 memcpy (&p->options, initrs->options, sizeof(initrs->options));
2660 memcpy (&p->protocolVersion, initrs->protocolVersion,
2661 sizeof(initrs->protocolVersion));
2662 free (p->userInformationField);
2663 p->userInformationField = NULL;
2664 if (initrs->userInformationField)
2668 if (initrs->userInformationField->which == ODR_EXTERNAL_octet &&
2669 (p->userInformationField =
2670 ir_tcl_malloc ((len =
2671 initrs->userInformationField->
2672 u.octet_aligned->len) +1)))
2674 memcpy (p->userInformationField,
2675 initrs->userInformationField->u.octet_aligned->buf,
2677 (p->userInformationField)[len] = '\0';
2682 static void ir_deleteDiags (IrTcl_Diagnostic **dst_list, int *dst_num)
2685 for (i = 0; i<*dst_num; i++)
2686 free (dst_list[i]->addinfo);
2692 static void ir_handleDiags (IrTcl_Diagnostic **dst_list, int *dst_num,
2693 Z_DiagRec **list, int num)
2699 *dst_list = ir_tcl_malloc (sizeof(**dst_list) * num);
2700 for (i = 0; i<num; i++)
2702 switch (list[i]->which)
2704 case Z_DiagRec_defaultFormat:
2705 (*dst_list)[i].condition = *list[i]->u.defaultFormat->condition;
2706 addinfo = list[i]->u.defaultFormat->addinfo;
2708 ((*dst_list)[i].addinfo = ir_tcl_malloc (strlen(addinfo)+1)))
2709 strcpy ((*dst_list)[i].addinfo, addinfo);
2712 (*dst_list)[i].addinfo = NULL;
2713 (*dst_list)[i].condition = 0;
2718 static void ir_handleRecords (void *o, Z_Records *zrs, IrTcl_SetObj *setobj)
2723 IrTcl_RecordList *rl;
2725 setobj->which = zrs->which;
2726 setobj->recordFlag = 1;
2728 ir_deleteDiags (&setobj->nonSurrogateDiagnosticList,
2729 &setobj->nonSurrogateDiagnosticNum);
2730 if (zrs->which == Z_Records_DBOSD)
2732 setobj->numberOfRecordsReturned =
2733 zrs->u.databaseOrSurDiagnostics->num_records;
2734 logf (LOG_DEBUG, "Got %d records", setobj->numberOfRecordsReturned);
2735 for (offset = 0; offset<setobj->numberOfRecordsReturned; offset++)
2737 rl = new_IR_record (setobj, setobj->start + offset,
2738 zrs->u.databaseOrSurDiagnostics->
2739 records[offset]->which);
2740 if (rl->which == Z_NamePlusRecord_surrogateDiagnostic)
2742 ir_handleDiags (&rl->u.surrogateDiagnostics.list,
2743 &rl->u.surrogateDiagnostics.num,
2744 &zrs->u.databaseOrSurDiagnostics->
2745 records[offset]->u.surrogateDiagnostic,
2750 Z_DatabaseRecord *zr;
2752 struct oident *ident;
2754 zr = zrs->u.databaseOrSurDiagnostics->records[offset]
2756 oe = (Z_External*) zr;
2757 rl->u.dbrec.size = zr->u.octet_aligned->len;
2759 if ((ident = oid_getentbyoid (oe->direct_reference)))
2760 rl->u.dbrec.type = ident->value;
2762 rl->u.dbrec.type = VAL_USMARC;
2764 if (oe->which == ODR_EXTERNAL_octet && rl->u.dbrec.size > 0)
2766 char *buf = (char*) zr->u.octet_aligned->buf;
2767 if ((rl->u.dbrec.buf = ir_tcl_malloc (rl->u.dbrec.size)))
2768 memcpy (rl->u.dbrec.buf, buf, rl->u.dbrec.size);
2770 else if (rl->u.dbrec.type == VAL_SUTRS &&
2771 oe->which == Z_External_sutrs)
2773 odr_setbuf (p->odr_in, (char*) oe->u.single_ASN1_type->buf,
2774 oe->u.single_ASN1_type->len, 0);
2775 if ((rl->u.dbrec.buf = ir_tcl_malloc (oe->u.sutrs->len+1)))
2777 memcpy (rl->u.dbrec.buf, oe->u.sutrs->buf,
2779 rl->u.dbrec.buf[oe->u.sutrs->len] = '\0';
2781 rl->u.dbrec.size = oe->u.sutrs->len;
2783 else if (rl->u.dbrec.type == VAL_GRS1 &&
2784 oe->which == Z_External_grs1)
2786 ir_tcl_read_grs (oe->u.grs1, &rl->u.dbrec.u.grs1);
2787 rl->u.dbrec.buf = NULL;
2790 rl->u.dbrec.buf = NULL;
2794 else if (zrs->which == Z_Records_multipleNSD)
2796 logf (LOG_DEBUG, "multipleNonSurrogateDiagnostic %d",
2797 zrs->u.multipleNonSurDiagnostics->num_diagRecs);
2798 setobj->numberOfRecordsReturned = 0;
2799 ir_handleDiags (&setobj->nonSurrogateDiagnosticList,
2800 &setobj->nonSurrogateDiagnosticNum,
2801 zrs->u.multipleNonSurDiagnostics->diagRecs,
2802 zrs->u.multipleNonSurDiagnostics->num_diagRecs);
2806 logf (LOG_DEBUG, "NonSurrogateDiagnostic");
2807 setobj->numberOfRecordsReturned = 0;
2808 ir_handleDiags (&setobj->nonSurrogateDiagnosticList,
2809 &setobj->nonSurrogateDiagnosticNum,
2810 &zrs->u.nonSurrogateDiagnostic,
2815 static void ir_searchResponse (void *o, Z_SearchResponse *searchrs,
2816 IrTcl_SetObj *setobj)
2818 Z_Records *zrs = searchrs->records;
2820 logf (LOG_DEBUG, "Received search response");
2823 logf (LOG_DEBUG, "Search response, no object!");
2826 setobj->searchStatus = searchrs->searchStatus ? 1 : 0;
2827 get_referenceId (&setobj->set_inher.referenceId, searchrs->referenceId);
2828 setobj->resultCount = *searchrs->resultCount;
2829 if (searchrs->presentStatus)
2830 setobj->presentStatus = *searchrs->presentStatus;
2831 if (searchrs->nextResultSetPosition)
2832 setobj->nextResultSetPosition = *searchrs->nextResultSetPosition;
2834 logf (LOG_DEBUG, "Search response %d, %d hits",
2835 setobj->searchStatus, setobj->resultCount);
2837 ir_handleRecords (o, zrs, setobj);
2839 setobj->recordFlag = 0;
2843 static void ir_presentResponse (void *o, Z_PresentResponse *presrs,
2844 IrTcl_SetObj *setobj)
2846 Z_Records *zrs = presrs->records;
2848 logf (LOG_DEBUG, "Received present response");
2851 logf (LOG_DEBUG, "Present response, no object!");
2854 setobj->presentStatus = *presrs->presentStatus;
2855 get_referenceId (&setobj->set_inher.referenceId, presrs->referenceId);
2856 setobj->nextResultSetPosition = *presrs->nextResultSetPosition;
2858 ir_handleRecords (o, zrs, setobj);
2861 setobj->recordFlag = 0;
2862 logf (LOG_DEBUG, "No records!");
2866 static void ir_scanResponse (void *o, Z_ScanResponse *scanrs,
2867 IrTcl_ScanObj *scanobj)
2871 logf (LOG_DEBUG, "Received scanResponse");
2873 get_referenceId (&p->set_inher.referenceId, scanrs->referenceId);
2874 scanobj->scanStatus = *scanrs->scanStatus;
2875 logf (LOG_DEBUG, "scanStatus=%d", scanobj->scanStatus);
2877 if (scanrs->stepSize)
2878 scanobj->stepSize = *scanrs->stepSize;
2879 logf (LOG_DEBUG, "stepSize=%d", scanobj->stepSize);
2881 scanobj->numberOfEntriesReturned = *scanrs->numberOfEntriesReturned;
2882 logf (LOG_DEBUG, "numberOfEntriesReturned=%d",
2883 scanobj->numberOfEntriesReturned);
2885 if (scanrs->positionOfTerm)
2886 scanobj->positionOfTerm = *scanrs->positionOfTerm;
2888 scanobj->positionOfTerm = -1;
2889 logf (LOG_DEBUG, "positionOfTerm=%d", scanobj->positionOfTerm);
2891 free (scanobj->entries);
2892 scanobj->entries = NULL;
2894 ir_deleteDiags (&scanobj->nonSurrogateDiagnosticList,
2895 &scanobj->nonSurrogateDiagnosticNum);
2896 if (scanrs->entries)
2901 scanobj->entries_flag = 1;
2902 scanobj->which = scanrs->entries->which;
2903 switch (scanobj->which)
2905 case Z_ListEntries_entries:
2906 scanobj->num_entries = scanrs->entries->u.entries->num_entries;
2907 scanobj->entries = ir_tcl_malloc (scanobj->num_entries *
2908 sizeof(*scanobj->entries));
2909 for (i=0; i<scanobj->num_entries; i++)
2911 ze = scanrs->entries->u.entries->entries[i];
2912 scanobj->entries[i].which = ze->which;
2915 case Z_Entry_termInfo:
2916 if (ze->u.termInfo->term->which == Z_Term_general)
2918 int l = ze->u.termInfo->term->u.general->len;
2919 scanobj->entries[i].u.term.buf = ir_tcl_malloc (1+l);
2920 memcpy (scanobj->entries[i].u.term.buf,
2921 ze->u.termInfo->term->u.general->buf,
2923 scanobj->entries[i].u.term.buf[l] = '\0';
2926 scanobj->entries[i].u.term.buf = NULL;
2927 if (ze->u.termInfo->globalOccurrences)
2928 scanobj->entries[i].u.term.globalOccurrences =
2929 *ze->u.termInfo->globalOccurrences;
2931 scanobj->entries[i].u.term.globalOccurrences = 0;
2933 case Z_Entry_surrogateDiagnostic:
2934 ir_handleDiags (&scanobj->entries[i].u.diag.list,
2935 &scanobj->entries[i].u.diag.num,
2936 &ze->u.surrogateDiagnostic,
2942 case Z_ListEntries_nonSurrogateDiagnostics:
2943 ir_handleDiags (&scanobj->nonSurrogateDiagnosticList,
2944 &scanobj->nonSurrogateDiagnosticNum,
2945 scanrs->entries->u.nonSurrogateDiagnostics->
2947 scanrs->entries->u.nonSurrogateDiagnostics->
2953 scanobj->entries_flag = 0;
2957 * ir_select_read: handle incoming packages
2959 void ir_select_read (ClientData clientData)
2961 IrTcl_Obj *p = clientData;
2966 Tcl_CmdInfo cmd_info;
2968 if (p->state == IR_TCL_R_Connecting)
2970 r = cs_rcvconnect (p->cs_link);
2973 logf (LOG_WARN, "cs_rcvconnect returned 1");
2976 p->state = IR_TCL_R_Idle;
2977 ir_select_remove_write (cs_fileno (p->cs_link), p);
2980 logf (LOG_DEBUG, "cs_rcvconnect error");
2983 p->failInfo = IR_TCL_FAIL_CONNECT;
2984 IrTcl_eval (p->interp, p->failback);
2986 do_disconnect (p, NULL, 2, NULL);
2989 p->state = IR_TCL_R_Idle;
2991 IrTcl_eval (p->interp, p->callback);
2992 if (p->cs_link && p->request_queue && p->state == IR_TCL_R_Idle)
2993 ir_tcl_send_q (p, p->request_queue, "x");
2998 /* signal one more use of ir object - callbacks must not
2999 release the ir memory (p pointer) */
3000 p->state = IR_TCL_R_Reading;
3003 /* read incoming APDU */
3004 if ((r=cs_get (p->cs_link, &p->buf_in, &p->len_in)) <= 0)
3006 logf (LOG_DEBUG, "cs_get failed, code %d", r);
3007 ir_select_remove (cs_fileno (p->cs_link), p);
3008 do_disconnect (p, NULL, 2, NULL);
3011 p->failInfo = IR_TCL_FAIL_READ;
3012 IrTcl_eval (p->interp, p->failback);
3014 /* release ir object now if callback deleted it */
3020 /* got complete APDU. Now decode */
3021 odr_setbuf (p->odr_in, p->buf_in, r, 0);
3022 logf (LOG_DEBUG, "cs_get ok, got %d", r);
3023 if (!z_APDU (p->odr_in, &apdu, 0))
3025 logf (LOG_DEBUG, "%s", odr_errlist [odr_geterror (p->odr_in)]);
3026 do_disconnect (p, NULL, 2, NULL);
3029 p->failInfo = IR_TCL_FAIL_IN_APDU;
3030 IrTcl_eval (p->interp, p->failback);
3032 /* release ir object now if failback deleted it */
3036 /* handle APDU and invoke callback */
3037 rq = p->request_queue;
3040 logf (LOG_FATAL, "Internal error. No queue entry");
3043 object_name = rq->object_name;
3044 logf (LOG_DEBUG, "getCommandInfo (%s)", object_name);
3045 if (Tcl_GetCommandInfo (p->interp, object_name, &cmd_info))
3049 case Z_APDU_initResponse:
3050 ir_initResponse (p, apdu->u.initResponse);
3052 case Z_APDU_searchResponse:
3053 ir_searchResponse (p, apdu->u.searchResponse,
3054 (IrTcl_SetObj *) cmd_info.clientData);
3056 case Z_APDU_presentResponse:
3057 ir_presentResponse (p, apdu->u.presentResponse,
3058 (IrTcl_SetObj *) cmd_info.clientData);
3060 case Z_APDU_scanResponse:
3061 ir_scanResponse (p, apdu->u.scanResponse,
3062 (IrTcl_ScanObj *) cmd_info.clientData);
3065 logf (LOG_WARN, "Received unknown APDU type (%d)",
3067 do_disconnect (p, NULL, 2, NULL);
3070 p->failInfo = IR_TCL_FAIL_UNKNOWN_APDU;
3071 IrTcl_eval (p->interp, p->failback);
3076 p->request_queue = rq->next;
3077 p->state = IR_TCL_R_Idle;
3080 IrTcl_eval (p->interp, rq->callback);
3082 free (rq->callback);
3083 free (rq->object_name);
3085 odr_reset (p->odr_in);
3086 if (p->ref_count == 1)
3092 } while (p->cs_link && cs_more (p->cs_link));
3093 if (p->cs_link && p->request_queue && p->state == IR_TCL_R_Idle)
3094 ir_tcl_send_q (p, p->request_queue, "x");
3098 * ir_select_write: handle outgoing packages - not yet written.
3100 void ir_select_write (ClientData clientData)
3102 IrTcl_Obj *p = clientData;
3106 logf (LOG_DEBUG, "In write handler");
3107 if (p->state == IR_TCL_R_Connecting)
3109 r = cs_rcvconnect (p->cs_link);
3112 p->state = IR_TCL_R_Idle;
3115 logf (LOG_DEBUG, "cs_rcvconnect error");
3116 ir_select_remove_write (cs_fileno (p->cs_link), p);
3119 p->failInfo = IR_TCL_FAIL_CONNECT;
3120 IrTcl_eval (p->interp, p->failback);
3122 do_disconnect (p, NULL, 2, NULL);
3125 ir_select_remove_write (cs_fileno (p->cs_link), p);
3127 IrTcl_eval (p->interp, p->callback);
3130 rq = p->request_queue;
3132 if ((r=cs_put (p->cs_link, rq->buf_out, rq->len_out)) < 0)
3134 logf (LOG_DEBUG, "select write fail");
3137 p->failInfo = IR_TCL_FAIL_WRITE;
3138 IrTcl_eval (p->interp, p->failback);
3142 do_disconnect (p, NULL, 2, NULL);
3144 else if (r == 0) /* remove select bit */
3146 p->state = IR_TCL_R_Waiting;
3147 ir_select_remove_write (cs_fileno (p->cs_link), p);
3153 /* ------------------------------------------------------- */
3156 * Irtcl_init: Registration of TCL commands.
3158 int Irtcl_Init (Tcl_Interp *interp)
3160 Tcl_CreateCommand (interp, "ir", ir_obj_mk, (ClientData) NULL,
3161 (Tcl_CmdDeleteProc *) NULL);
3162 Tcl_CreateCommand (interp, "ir-set", ir_set_obj_mk,
3163 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
3164 Tcl_CreateCommand (interp, "ir-scan", ir_scan_obj_mk,
3165 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);