2 * IR toolkit for tcl/tk
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.24 1995-04-11 14:16:42 adam
8 * Further work on scan. Response works. Entries aren't saved yet.
10 * Revision 1.23 1995/04/10 10:50:27 adam
11 * Result-set name defaults to suffix of ir-set name.
12 * Started working on scan. Not finished at this point.
14 * Revision 1.22 1995/03/31 10:43:03 adam
15 * More robust when getting bad MARC records.
17 * Revision 1.21 1995/03/31 08:56:37 adam
18 * New button "Search".
20 * Revision 1.20 1995/03/29 16:07:09 adam
21 * Bug fix: Didn't use setName in present request.
23 * Revision 1.19 1995/03/28 12:45:23 adam
24 * New ir method failback: called on disconnect/protocol error.
25 * New ir set/get method: protocol: SR / Z3950.
26 * Simple popup and disconnect when failback is invoked.
28 * Revision 1.18 1995/03/21 15:50:12 adam
31 * Revision 1.17 1995/03/21 13:41:03 adam
32 * Comstack cs_create not used too often. Non-blocking connect.
34 * Revision 1.16 1995/03/21 08:26:06 adam
35 * New method, setName, to specify the result set name (other than Default).
36 * New method, responseStatus, which returns diagnostic info, if any, after
37 * present response / search response.
39 * Revision 1.15 1995/03/20 15:24:07 adam
40 * Diagnostic records saved on searchResponse.
42 * Revision 1.14 1995/03/20 08:53:22 adam
43 * Event loop in tclmain.c rewritten. New method searchStatus.
45 * Revision 1.13 1995/03/17 18:26:17 adam
46 * Non-blocking i/o used now. Database names popup as cascade items.
48 * Revision 1.12 1995/03/17 15:45:00 adam
49 * Improved target/database setup.
51 * Revision 1.11 1995/03/16 17:54:03 adam
52 * Minor changes really.
54 * Revision 1.10 1995/03/15 16:14:50 adam
55 * Blocking arg in cs_create changed.
57 * Revision 1.9 1995/03/15 13:59:24 adam
60 * Revision 1.8 1995/03/15 08:25:16 adam
61 * New method presentStatus to check for error on present. Misc. cleanup
62 * of IRRecordList manipulations. Full MARC record presentation in
65 * Revision 1.7 1995/03/14 17:32:29 adam
66 * Presentation of full Marc record in popup window.
68 * Revision 1.6 1995/03/12 19:31:55 adam
69 * Pattern matching implemented when retrieving MARC records. More
70 * diagnostic functions.
72 * Revision 1.5 1995/03/10 18:00:15 adam
73 * Actual presentation in line-by-line format. RPN query support.
75 * Revision 1.4 1995/03/09 16:15:08 adam
76 * First presentRequest attempts. Hot-target list.
111 int preferredMessageSize;
112 int maximumRecordSize;
114 Odr_bitmask protocolVersion;
115 char *idAuthentication;
116 char *implementationName;
117 char *implementationId;
138 int smallSetUpperBound;
139 int largeSetLowerBound;
140 int mediumSetPresentNumber;
141 int replaceIndicator;
142 char **databaseNames;
143 int num_databaseNames;
149 struct IRSetObj_ *set_child;
150 struct IRScanObj_ *scan_child;
153 typedef struct IRRecordList_ {
165 struct IRRecordList_ *next;
168 typedef struct IRSetObj_ {
174 int numberOfRecordsReturned;
180 IRRecordList *record_list;
183 typedef struct IRScanObj_ {
186 int numberOfTermsRequested;
187 int preferredPositionInResponse;
190 int numberOfEntriesReturned;
200 int (*method) (void *obj, Tcl_Interp *interp, int argc, char **argv);
203 static int do_disconnect (void *obj,Tcl_Interp *interp, int argc, char **argv);
205 static IRRecordList *new_IR_record (IRSetObj *setobj, int no, int which)
209 for (rl = setobj->record_list; rl; rl = rl->next)
215 case Z_NamePlusRecord_databaseRecord:
216 iso2709_rm (rl->u.marc.rec);
218 case Z_NamePlusRecord_surrogateDiagnostic:
219 free (rl->u.diag.addinfo);
220 rl->u.diag.addinfo = NULL;
228 rl = malloc (sizeof(*rl));
230 rl->next = setobj->record_list;
232 setobj->record_list = rl;
238 static IRRecordList *find_IR_record (IRSetObj *setobj, int no)
242 for (rl = setobj->record_list; rl; rl = rl->next)
249 * getsetint: Set/get integer value
251 static int get_set_int (int *val, Tcl_Interp *interp, int argc, char **argv)
257 if (Tcl_GetInt (interp, argv[2], val)==TCL_ERROR)
260 sprintf (buf, "%d", *val);
261 Tcl_AppendResult (interp, buf, NULL);
266 * get_parent_info: Returns information about parent object.
268 static int get_parent_info (Tcl_Interp *interp, const char *name,
269 Tcl_CmdInfo *parent_info,
272 char parent_name[128];
273 const char *csep = strrchr (name, '.');
278 interp->result = "missing .";
286 memcpy (parent_name, name, pos);
287 parent_name[pos] = '\0';
288 if (!Tcl_GetCommandInfo (interp, parent_name, parent_info))
290 interp->result = "No parent";
297 * ir_method: Search for method in table and invoke method handler
299 int ir_method (void *obj, Tcl_Interp *interp, int argc, char **argv,
303 for (t = tab; t->name; t++)
304 if (!strcmp (t->name, argv[1]))
305 return (*t->method)(obj, interp, argc, argv);
306 Tcl_AppendResult (interp, "Bad method. Possible values:", NULL);
307 for (t = tab; t->name; t++)
308 Tcl_AppendResult (interp, " ", t->name, NULL);
313 * ir_method_r: Get status for all readable elements
315 int ir_method_r (void *obj, Tcl_Interp *interp, int argc, char **argv,
323 for (; tab->name; tab++)
326 argv_n[1] = tab->name;
327 Tcl_AppendResult (interp, "{", NULL);
328 (*tab->method)(obj, interp, argc_n, argv_n);
329 Tcl_AppendResult (interp, "} ", NULL);
335 * ir_asc2bitmask: Ascii to ODR bitmask conversion
337 int ir_asc2bitmask (const char *asc, Odr_bitmask *ob)
339 const char *cp = asc + strlen(asc);
346 ODR_MASK_SET (ob, bitno);
353 * ir_strdup: Duplicate string
355 int ir_strdup (Tcl_Interp *interp, char** p, const char *s)
357 *p = malloc (strlen(s)+1);
360 interp->result = "strdup fail";
368 * ir_malloc: Malloc function
370 void *ir_malloc (Tcl_Interp *interp, size_t size)
372 static char buf[128];
373 void *p = malloc (size);
377 sprintf (buf, "Malloc fail. %ld bytes requested", (long) size);
378 interp->result = buf;
384 /* ------------------------------------------------------- */
387 * do_init_request: init method on IR object
389 static int do_init_request (void *obj, Tcl_Interp *interp,
390 int argc, char **argv)
392 Z_APDU apdu, *apdup = &apdu;
399 interp->result = "not connected";
403 req.options = &p->options;
404 req.protocolVersion = &p->protocolVersion;
405 req.preferredMessageSize = &p->preferredMessageSize;
406 req.maximumRecordSize = &p->maximumRecordSize;
408 req.idAuthentication = p->idAuthentication;
409 req.implementationId = p->implementationId;
410 req.implementationName = p->implementationName;
411 req.implementationVersion = "0.1";
412 req.userInformationField = 0;
414 apdu.u.initRequest = &req;
415 apdu.which = Z_APDU_initRequest;
417 if (!z_APDU (p->odr_out, &apdup, 0))
419 Tcl_AppendResult (interp, odr_errlist [odr_geterror (p->odr_out)],
421 odr_reset (p->odr_out);
424 p->sbuf = odr_getbuf (p->odr_out, &p->slen);
425 if ((r=cs_put (p->cs_link, p->sbuf, p->slen)) < 0)
427 interp->result = "cs_put failed in init";
428 do_disconnect (p, NULL, 0, NULL);
433 ir_select_add_write (cs_fileno(p->cs_link), p);
434 printf("Sent part of initializeRequest (%d bytes).\n", p->slen);
437 printf("Sent whole initializeRequest (%d bytes).\n", p->slen);
442 * do_protocolVersion: Set protocol Version
444 static int do_protocolVersion (void *obj, Tcl_Interp *interp,
445 int argc, char **argv)
448 ir_asc2bitmask (argv[2], &((IRObj *) obj)->protocolVersion);
453 * do_options: Set options
455 static int do_options (void *obj, Tcl_Interp *interp,
456 int argc, char **argv)
459 ir_asc2bitmask (argv[2], &((IRObj *) obj)->options);
464 * do_preferredMessageSize: Set/get preferred message size
466 static int do_preferredMessageSize (void *obj, Tcl_Interp *interp,
467 int argc, char **argv)
470 return get_set_int (&p->preferredMessageSize, interp, argc, argv);
474 * do_maximumRecordSize: Set/get maximum record size
476 static int do_maximumRecordSize (void *obj, Tcl_Interp *interp,
477 int argc, char **argv)
480 return get_set_int (&p->maximumRecordSize, interp, argc, argv);
485 * do_implementationName: Set/get Implementation Name.
487 static int do_implementationName (void *obj, Tcl_Interp *interp,
488 int argc, char **argv)
492 free (((IRObj*)obj)->implementationName);
493 if (ir_strdup (interp, &((IRObj*) obj)->implementationName, argv[2])
497 Tcl_AppendResult (interp, ((IRObj*)obj)->implementationName,
503 * do_implementationId: Set/get Implementation Id.
505 static int do_implementationId (void *obj, Tcl_Interp *interp,
506 int argc, char **argv)
510 free (((IRObj*)obj)->implementationId);
511 if (ir_strdup (interp, &((IRObj*) obj)->implementationId, argv[2])
515 Tcl_AppendResult (interp, ((IRObj*)obj)->implementationId,
521 * do_idAuthentication: Set/get id Authentication
523 static int do_idAuthentication (void *obj, Tcl_Interp *interp,
524 int argc, char **argv)
528 free (((IRObj*)obj)->idAuthentication);
529 if (ir_strdup (interp, &((IRObj*) obj)->idAuthentication, argv[2])
533 Tcl_AppendResult (interp, ((IRObj*)obj)->idAuthentication,
539 * do_connect: connect method on IR object
541 static int do_connect (void *obj, Tcl_Interp *interp,
542 int argc, char **argv)
547 int protocol_type = CS_Z3950;
553 interp->result = "already connected";
556 if (!strcmp (p->protocol_type, "Z3950"))
557 protocol_type = CS_Z3950;
558 else if (!strcmp (p->protocol_type, "SR"))
559 protocol_type = CS_SR;
562 interp->result = "bad protocol type";
565 if (!strcmp (p->cs_type, "tcpip"))
567 p->cs_link = cs_create (tcpip_type, CS_BLOCK, protocol_type);
568 addr = tcpip_strtoaddr (argv[2]);
571 interp->result = "tcpip_strtoaddr fail";
574 printf ("tcp/ip connect %s\n", argv[2]);
577 else if (!strcmp (p->cs_type, "mosi"))
579 p->cs_link = cs_create (mosi_type, CS_BLOCK, protocol_type);
580 addr = mosi_strtoaddr (argv[2]);
583 interp->result = "mosi_strtoaddr fail";
586 printf ("mosi connect %s\n", argv[2]);
591 interp->result = "unknown comstack type";
594 if (ir_strdup (interp, &p->hostname, argv[2]) == TCL_ERROR)
596 if ((r=cs_connect (p->cs_link, addr)) < 0)
598 interp->result = "cs_connect fail";
599 do_disconnect (p, NULL, 0, NULL);
602 ir_select_add (cs_fileno (p->cs_link), p);
605 ir_select_add_write (cs_fileno (p->cs_link), p);
612 Tcl_Eval (p->interp, p->callback);
616 Tcl_AppendElement (interp, p->hostname);
621 * do_disconnect: disconnect method on IR object
623 static int do_disconnect (void *obj, Tcl_Interp *interp,
624 int argc, char **argv)
632 ir_select_remove_write (cs_fileno (p->cs_link), p);
633 ir_select_remove (cs_fileno (p->cs_link), p);
636 cs_close (p->cs_link);
639 assert (!p->cs_link);
644 * do_comstack: Set/get comstack method on IR object
646 static int do_comstack (void *o, Tcl_Interp *interp,
647 int argc, char **argv)
654 if (ir_strdup (interp, &obj->cs_type, argv[2]) == TCL_ERROR)
657 Tcl_AppendElement (interp, obj->cs_type);
662 * do_protocol: Set/get protocol method on IR object
664 static int do_protocol (void *o, Tcl_Interp *interp,
665 int argc, char **argv)
671 free (obj->protocol_type);
672 if (ir_strdup (interp, &obj->protocol_type, argv[2]) == TCL_ERROR)
675 Tcl_AppendElement (interp, obj->protocol_type);
680 * do_callback: add callback
682 static int do_callback (void *obj, Tcl_Interp *interp,
683 int argc, char **argv)
690 if (ir_strdup (interp, &p->callback, argv[2]) == TCL_ERROR)
698 * do_failback: add error handle callback
700 static int do_failback (void *obj, Tcl_Interp *interp,
701 int argc, char **argv)
708 if (ir_strdup (interp, &p->failback, argv[2]) == TCL_ERROR)
716 * do_databaseNames: specify database names
718 static int do_databaseNames (void *obj, Tcl_Interp *interp,
719 int argc, char **argv)
726 for (i=0; i<p->num_databaseNames; i++)
727 Tcl_AppendElement (interp, p->databaseNames[i]);
730 if (p->databaseNames)
732 for (i=0; i<p->num_databaseNames; i++)
733 free (p->databaseNames[i]);
734 free (p->databaseNames);
736 p->num_databaseNames = argc - 2;
737 if (!(p->databaseNames = ir_malloc (interp,
738 sizeof(*p->databaseNames) * p->num_databaseNames)))
740 for (i=0; i<p->num_databaseNames; i++)
742 if (ir_strdup (interp, &p->databaseNames[i], argv[2+i])
750 * do_query: Set/Get query mothod
752 static int do_query (void *obj, Tcl_Interp *interp,
753 int argc, char **argv)
758 free (p->query_method);
759 if (ir_strdup (interp, &p->query_method, argv[2]) == TCL_ERROR)
762 Tcl_AppendResult (interp, p->query_method, NULL);
767 * do_replaceIndicator: Set/get replace Set indicator
769 static int do_replaceIndicator (void *obj, Tcl_Interp *interp,
770 int argc, char **argv)
773 return get_set_int (&p->replaceIndicator, interp, argc, argv);
777 * ir_obj_method: IR Object methods
779 static int ir_obj_method (ClientData clientData, Tcl_Interp *interp,
780 int argc, char **argv)
782 static IRMethod tab[] = {
783 { 1, "comstack", do_comstack },
784 { 1, "protocol", do_protocol },
785 { 1, "connect", do_connect },
786 { 0, "protocolVersion", do_protocolVersion },
787 { 0, "options", do_options },
788 { 1, "preferredMessageSize", do_preferredMessageSize },
789 { 1, "maximumRecordSize", do_maximumRecordSize },
790 { 1, "implementationName", do_implementationName },
791 { 1, "implementationId", do_implementationId },
792 { 1, "idAuthentication", do_idAuthentication },
793 { 0, "init", do_init_request },
794 { 0, "disconnect", do_disconnect },
795 { 0, "callback", do_callback },
796 { 0, "failback", do_failback },
797 { 1, "databaseNames", do_databaseNames},
798 { 1, "replaceIndicator", do_replaceIndicator},
799 { 1, "query", do_query },
803 return ir_method_r (clientData, interp, argc, argv, tab);
804 return ir_method (clientData, interp, argc, argv, tab);
808 * ir_obj_delete: IR Object disposal
810 static void ir_obj_delete (ClientData clientData)
812 free ( (void*) clientData);
816 * ir_obj_mk: IR Object creation
818 static int ir_obj_mk (ClientData clientData, Tcl_Interp *interp,
819 int argc, char **argv)
826 interp->result = "wrong # args";
829 if (!(obj = ir_malloc (interp, sizeof(*obj))))
831 if (ir_strdup (interp, &obj->cs_type, "tcpip") == TCL_ERROR)
833 if (ir_strdup (interp, &obj->protocol_type, "Z3950") == TCL_ERROR)
836 obj->bib1.proto = PROTO_Z3950;
837 obj->bib1.class = CLASS_ATTSET;
838 obj->bib1.value = VAL_BIB1;
840 obj->maximumRecordSize = 32768;
841 obj->preferredMessageSize = 4096;
842 obj->connectFlag = 0;
844 obj->idAuthentication = NULL;
846 if (ir_strdup (interp, &obj->implementationName, "TCL/TK on YAZ")
850 if (ir_strdup (interp, &obj->implementationId, "TCL/TK/YAZ")
854 obj->smallSetUpperBound = 0;
855 obj->largeSetLowerBound = 2;
856 obj->mediumSetPresentNumber = 0;
857 obj->replaceIndicator = 1;
859 obj->databaseNames = NULL;
860 obj->num_databaseNames = 0;
862 obj->num_databaseNames = 1;
863 if (!(obj->databaseNames = ir_malloc (interp,
864 sizeof(*obj->databaseNames))))
866 if (ir_strdup (interp, &obj->databaseNames[0], "Default") == TCL_ERROR)
870 obj->hostname = NULL;
872 if (ir_strdup (interp, &obj->query_method, "rpn") == TCL_ERROR)
874 obj->bibset = ccl_qual_mk ();
875 if ((inf = fopen ("default.bib", "r")))
877 ccl_qual_file (obj->bibset, inf);
880 ODR_MASK_ZERO (&obj->protocolVersion);
881 ODR_MASK_SET (&obj->protocolVersion, 0);
882 ODR_MASK_SET (&obj->protocolVersion, 1);
884 ODR_MASK_ZERO (&obj->options);
885 ODR_MASK_SET (&obj->options, 0);
887 obj->odr_in = odr_createmem (ODR_DECODE);
888 obj->odr_out = odr_createmem (ODR_ENCODE);
889 obj->odr_pr = odr_createmem (ODR_PRINT);
891 obj->len_out = 10000;
892 if (!(obj->buf_out = ir_malloc (interp, obj->len_out)))
894 odr_setbuf (obj->odr_out, obj->buf_out, obj->len_out);
899 obj->callback = NULL;
900 obj->failback = NULL;
901 Tcl_CreateCommand (interp, argv[1], ir_obj_method,
902 (ClientData) obj, ir_obj_delete);
906 /* ------------------------------------------------------- */
908 * do_search: Do search request
910 static int do_search (void *o, Tcl_Interp *interp,
911 int argc, char **argv)
915 Z_APDU apdu, *apdup = &apdu;
918 IRObj *p = obj->parent;
924 interp->result = "wrong # args";
927 if (!p->num_databaseNames)
929 interp->result = "no databaseNames";
934 interp->result = "not connected";
937 apdu.which = Z_APDU_searchRequest;
938 apdu.u.searchRequest = &req;
941 req.smallSetUpperBound = &p->smallSetUpperBound;
942 req.largeSetLowerBound = &p->largeSetLowerBound;
943 req.mediumSetPresentNumber = &p->mediumSetPresentNumber;
944 req.replaceIndicator = &p->replaceIndicator;
945 req.resultSetName = obj->setName ? obj->setName : "Default";
946 req.num_databaseNames = p->num_databaseNames;
947 req.databaseNames = p->databaseNames;
949 for (r=0; r<p->num_databaseNames; r++)
951 printf (" %s", p->databaseNames[r]);
953 req.smallSetElementSetNames = 0;
954 req.mediumSetElementSetNames = 0;
955 req.preferredRecordSyntax = 0;
958 if (!strcmp (p->query_method, "rpn"))
962 struct ccl_rpn_node *rpn;
963 Z_RPNQuery *RPNquery;
965 rpn = ccl_find_str(p->bibset, argv[2], &error, &pos);
968 Tcl_AppendResult (interp, "CCL error: ", ccl_err_msg(error),NULL);
971 query.which = Z_Query_type_1;
972 assert((RPNquery = ccl_rpn_query(rpn)));
973 RPNquery->attributeSetId = oid_getoidbyent (&p->bib1);
974 query.u.type_1 = RPNquery;
977 else if (!strcmp (p->query_method, "ccl"))
979 query.which = Z_Query_type_2;
980 query.u.type_2 = &ccl_query;
981 ccl_query.buf = (unsigned char *) argv[2];
982 ccl_query.len = strlen (argv[2]);
987 interp->result = "unknown query method";
990 if (!z_APDU (p->odr_out, &apdup, 0))
992 interp->result = odr_errlist [odr_geterror (p->odr_out)];
993 odr_reset (p->odr_out);
996 p->sbuf = odr_getbuf (p->odr_out, &p->slen);
997 if ((r=cs_put (p->cs_link, p->sbuf, p->slen)) < 0)
999 interp->result = "cs_put failed in search";
1004 ir_select_add_write (cs_fileno(p->cs_link), p);
1005 printf("Sent part of searchRequest (%d bytes).\n", p->slen);
1009 printf ("Whole search request\n");
1015 * do_resultCount: Get number of hits
1017 static int do_resultCount (void *o, Tcl_Interp *interp,
1018 int argc, char **argv)
1022 return get_set_int (&obj->resultCount, interp, argc, argv);
1026 * do_searchStatus: Get search status (after search response)
1028 static int do_searchStatus (void *o, Tcl_Interp *interp,
1029 int argc, char **argv)
1033 return get_set_int (&obj->searchStatus, interp, argc, argv);
1037 * do_setName: Set result Set name
1039 static int do_setName (void *o, Tcl_Interp *interp,
1040 int argc, char **argv)
1046 free (obj->setName);
1047 if (ir_strdup (interp, &obj->setName, argv[2])
1051 Tcl_AppendElement (interp, obj->setName);
1056 * do_numberOfRecordsReturned: Get number of records returned
1058 static int do_numberOfRecordsReturned (void *o, Tcl_Interp *interp,
1059 int argc, char **argv)
1063 return get_set_int (&obj->numberOfRecordsReturned, interp, argc, argv);
1066 static int get_marc_fields(Tcl_Interp *interp, Iso2709Rec rec,
1067 int argc, char **argv)
1074 a = iso2709_a_mk (rec);
1075 while (iso2709_a_search (a, argv[4], argv[5], argv[6]))
1077 if (!(iso2709_a_info_field (a, NULL, NULL, NULL, &data)))
1079 Tcl_AppendElement (interp, data);
1087 static int get_marc_lines(Tcl_Interp *interp, Iso2709Rec rec,
1088 int argc, char **argv)
1099 a = iso2709_a_mk (rec);
1100 while (iso2709_a_search (a, argv[4], argv[5], argv[6]))
1102 if (!(iso2709_a_info_field (a, &tag, &indicator, &identifier, &data)))
1104 if (strcmp (tag, ptag))
1107 Tcl_AppendResult (interp, "}} ", NULL);
1109 Tcl_AppendResult (interp, "{", tag, " {} {", NULL);
1111 Tcl_AppendResult (interp, "{", tag, " {", indicator,
1116 Tcl_AppendResult (interp, "{{}", NULL);
1118 Tcl_AppendResult (interp, "{", identifier, NULL);
1119 Tcl_AppendElement (interp, data);
1120 Tcl_AppendResult (interp, "} ", NULL);
1124 Tcl_AppendResult (interp, "}} ", NULL);
1130 * do_recordType: Return record type (if any) at position.
1132 static int do_recordType (void *o, Tcl_Interp *interp, int argc, char **argv)
1140 sprintf (interp->result, "wrong # args");
1143 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
1145 rl = find_IR_record (obj, offset);
1150 case Z_NamePlusRecord_databaseRecord:
1151 interp->result = "databaseRecord";
1153 case Z_NamePlusRecord_surrogateDiagnostic:
1154 interp->result = "surrogateDiagnostic";
1161 * do_recordDiag: Return diagnostic record info
1163 static int do_recordDiag (void *o, Tcl_Interp *interp, int argc, char **argv)
1172 sprintf (interp->result, "wrong # args");
1175 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
1177 rl = find_IR_record (obj, offset);
1180 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
1183 if (rl->which != Z_NamePlusRecord_surrogateDiagnostic)
1185 Tcl_AppendResult (interp, "No Diagnostic record at #", argv[2], NULL);
1188 sprintf (buf, "%d", rl->u.diag.condition);
1189 Tcl_AppendResult (interp, buf, " {",
1190 (rl->u.diag.addinfo ? rl->u.diag.addinfo : ""),
1196 * do_recordMarc: Get ISO2709 Record lines/fields
1198 static int do_recordMarc (void *o, Tcl_Interp *interp, int argc, char **argv)
1206 sprintf (interp->result, "wrong # args");
1209 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
1211 rl = find_IR_record (obj, offset);
1214 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
1217 if (rl->which != Z_NamePlusRecord_databaseRecord)
1219 Tcl_AppendResult (interp, "No MARC record at #", argv[2], NULL);
1222 if (!strcmp (argv[3], "field"))
1223 return get_marc_fields (interp, rl->u.marc.rec, argc, argv);
1224 else if (!strcmp (argv[3], "line"))
1225 return get_marc_lines (interp, rl->u.marc.rec, argc, argv);
1228 Tcl_AppendResult (interp, "field/line expected", NULL);
1234 * do_responseStatus: Return response status (present or search)
1236 static int do_responseStatus (void *o, Tcl_Interp *interp,
1237 int argc, char **argv)
1243 if (!obj->recordFlag)
1247 case Z_Records_DBOSD:
1248 Tcl_AppendElement (interp, "DBOSD");
1251 Tcl_AppendElement (interp, "NSD");
1252 sprintf (buf, "%d", obj->condition);
1253 Tcl_AppendElement (interp, buf);
1254 cp = diagbib1_str (obj->condition);
1256 Tcl_AppendElement (interp, (char*) cp);
1258 Tcl_AppendElement (interp, "");
1260 Tcl_AppendElement (interp, obj->addinfo);
1262 Tcl_AppendElement (interp, "");
1269 * do_present: Perform Present Request
1272 static int do_present (void *o, Tcl_Interp *interp,
1273 int argc, char **argv)
1276 IRObj *p = obj->parent;
1277 Z_APDU apdu, *apdup = &apdu;
1278 Z_PresentRequest req;
1285 if (Tcl_GetInt (interp, argv[2], &start) == TCL_ERROR)
1292 if (Tcl_GetInt (interp, argv[3], &number) == TCL_ERROR)
1299 interp->result = "not connected";
1303 obj->number = number;
1305 apdu.which = Z_APDU_presentRequest;
1306 apdu.u.presentRequest = &req;
1307 req.referenceId = 0;
1308 /* sprintf(setstring, "%d", setnumber); */
1310 req.resultSetId = obj->setName ? obj->setName : "Default";
1312 req.resultSetStartPoint = &start;
1313 req.numberOfRecordsRequested = &number;
1314 req.elementSetNames = 0;
1315 req.preferredRecordSyntax = 0;
1317 if (!z_APDU (p->odr_out, &apdup, 0))
1319 interp->result = odr_errlist [odr_geterror (p->odr_out)];
1320 odr_reset (p->odr_out);
1323 p->sbuf = odr_getbuf (p->odr_out, &p->slen);
1324 if ((r=cs_put (p->cs_link, p->sbuf, p->slen)) < 0)
1326 interp->result = "cs_put failed in present";
1331 ir_select_add_write (cs_fileno(p->cs_link), p);
1332 printf ("Part of present request, start=%d, num=%d (%d bytes)\n",
1333 start, number, p->slen);
1337 printf ("Whole present request, start=%d, num=%d (%d bytes)\n",
1338 start, number, p->slen);
1344 * do_loadFile: Load result set from file
1347 static int do_loadFile (void *o, Tcl_Interp *interp,
1348 int argc, char **argv)
1350 IRSetObj *setobj = o;
1357 interp->result = "wrong # args";
1360 inf = fopen (argv[2], "r");
1363 Tcl_AppendResult (interp, "Cannot open ", argv[2], NULL);
1366 while ((buf = iso2709_read (inf)))
1371 rec = iso2709_cvt (buf);
1374 rl = new_IR_record (setobj, no, Z_NamePlusRecord_databaseRecord);
1375 rl->u.marc.rec = rec;
1378 setobj->numberOfRecordsReturned = no-1;
1384 * ir_set_obj_method: IR Set Object methods
1386 static int ir_set_obj_method (ClientData clientData, Tcl_Interp *interp,
1387 int argc, char **argv)
1389 static IRMethod tab[] = {
1390 { 0, "search", do_search },
1391 { 0, "searchStatus", do_searchStatus },
1392 { 0, "setName", do_setName },
1393 { 0, "resultCount", do_resultCount },
1394 { 0, "numberOfRecordsReturned", do_numberOfRecordsReturned },
1395 { 0, "present", do_present },
1396 { 0, "recordType", do_recordType },
1397 { 0, "recordMarc", do_recordMarc },
1398 { 0, "recordDiag", do_recordDiag },
1399 { 0, "responseStatus", do_responseStatus },
1400 { 0, "loadFile", do_loadFile },
1406 interp->result = "wrong # args";
1409 return ir_method (clientData, interp, argc, argv, tab);
1413 * ir_set_obj_delete: IR Set Object disposal
1415 static void ir_set_obj_delete (ClientData clientData)
1417 free ( (void*) clientData);
1421 * ir_set_obj_mk: IR Set Object creation
1423 static int ir_set_obj_mk (ClientData clientData, Tcl_Interp *interp,
1424 int argc, char **argv)
1426 Tcl_CmdInfo parent_info;
1432 interp->result = "wrong # args";
1435 if (get_parent_info (interp, argv[1], &parent_info, &suffix) == TCL_ERROR)
1437 if (!(obj = ir_malloc (interp, sizeof(*obj))))
1439 if (ir_strdup (interp, &obj->setName, suffix) == TCL_ERROR)
1441 obj->record_list = NULL;
1442 obj->addinfo = NULL;
1443 obj->parent = (IRObj *) parent_info.clientData;
1444 Tcl_CreateCommand (interp, argv[1], ir_set_obj_method,
1445 (ClientData) obj, ir_set_obj_delete);
1449 /* ------------------------------------------------------- */
1452 * do_scan: Perform scan
1454 static int do_scan (void *o, Tcl_Interp *interp, int argc, char **argv)
1457 Z_APDU apdu, *apdup = &apdu;
1459 IRObj *p = obj->parent;
1465 interp->result = "wrong # args";
1468 if (!p->num_databaseNames)
1470 interp->result = "no databaseNames";
1475 interp->result = "not connected";
1478 apdu.which = Z_APDU_scanRequest;
1479 apdu.u.scanRequest = &req;
1480 req.referenceId = NULL;
1481 req.num_databaseNames = p->num_databaseNames;
1482 req.databaseNames = p->databaseNames;
1483 req.attributeSet = oid_getoidbyent (&p->bib1);
1485 if (!(req.termListAndStartPoint =
1486 ir_malloc (interp, sizeof(*req.termListAndStartPoint))))
1488 req.termListAndStartPoint->num_attributes = 0;
1489 req.termListAndStartPoint->attributeList = NULL;
1490 if (!(req.termListAndStartPoint->term = ir_malloc (interp,
1493 req.termListAndStartPoint->term->which = Z_Term_general;
1494 if (!(req.termListAndStartPoint->term->u.general =
1495 ir_malloc (interp, sizeof(*req.termListAndStartPoint->
1498 if (ir_strdup (interp, &req.termListAndStartPoint->term->u.general->buf,
1499 argv[2]) == TCL_ERROR)
1501 req.termListAndStartPoint->term->u.general->len =
1502 req.termListAndStartPoint->term->u.general->size = strlen(argv[2]);
1503 req.stepSize = &obj->stepSize;
1504 req.numberOfTermsRequested = &obj->numberOfTermsRequested;
1505 req.preferredPositionInResponse = &obj->preferredPositionInResponse;
1506 printf ("stepSize=%d\n", *req.stepSize);
1507 printf ("numberOfTermsRequested=%d\n", *req.numberOfTermsRequested);
1508 printf ("preferredPositionInResponse=%d\n",
1509 *req.preferredPositionInResponse);
1511 if (!z_APDU (p->odr_out, &apdup, 0))
1513 interp->result = odr_errlist [odr_geterror (p->odr_out)];
1514 odr_reset (p->odr_out);
1517 p->sbuf = odr_getbuf (p->odr_out, &p->slen);
1518 if ((r=cs_put (p->cs_link, p->sbuf, p->slen)) < 0)
1520 interp->result = "cs_put failed in scan";
1525 ir_select_add_write (cs_fileno(p->cs_link), p);
1526 printf("Sent part of scanRequest (%d bytes).\n", p->slen);
1530 printf ("Whole scan request\n");
1536 * do_stepSize: Set/get replace Step Size
1538 static int do_stepSize (void *obj, Tcl_Interp *interp,
1539 int argc, char **argv)
1542 return get_set_int (&p->stepSize, interp, argc, argv);
1546 * do_numberOfTermsRequested: Set/get Number of Terms requested
1548 static int do_numberOfTermsRequested (void *obj, Tcl_Interp *interp,
1549 int argc, char **argv)
1552 return get_set_int (&p->numberOfTermsRequested, interp, argc, argv);
1557 * do_preferredPositionInResponse: Set/get preferred Position
1559 static int do_preferredPositionInResponse (void *obj, Tcl_Interp *interp,
1560 int argc, char **argv)
1563 return get_set_int (&p->preferredPositionInResponse, interp, argc, argv);
1567 * do_scanStatus: Get scan status
1569 static int do_scanStatus (void *obj, Tcl_Interp *interp,
1570 int argc, char **argv)
1573 return get_set_int (&p->scanStatus, interp, argc, argv);
1577 * do_numberOfEntriesReturned: Get number of Entries returned
1579 static int do_numberOfEntriesReturned (void *obj, Tcl_Interp *interp,
1580 int argc, char **argv)
1583 return get_set_int (&p->numberOfEntriesReturned, interp, argc, argv);
1587 * do_positionOfTerm: Get position of Term
1589 static int do_positionOfTerm (void *obj, Tcl_Interp *interp,
1590 int argc, char **argv)
1593 return get_set_int (&p->positionOfTerm, interp, argc, argv);
1597 * ir_scan_obj_method: IR Scan Object methods
1599 static int ir_scan_obj_method (ClientData clientData, Tcl_Interp *interp,
1600 int argc, char **argv)
1602 static IRMethod tab[] = {
1603 { 0, "scan", do_scan },
1604 { 0, "stepSize", do_stepSize },
1605 { 0, "numberOfTermsRequested", do_numberOfTermsRequested },
1606 { 0, "preferredPositionInResponse", do_preferredPositionInResponse },
1607 { 0, "scanStatus", do_scanStatus },
1608 { 0, "numberOfEntriesReturned", do_numberOfEntriesReturned },
1609 { 0, "positionOfTerm", do_positionOfTerm },
1615 interp->result = "wrong # args";
1618 return ir_method (clientData, interp, argc, argv, tab);
1622 * ir_scan_obj_delete: IR Scan Object disposal
1624 static void ir_scan_obj_delete (ClientData clientData)
1626 free ( (void*) clientData);
1630 * ir_scan_obj_mk: IR Scan Object creation
1632 static int ir_scan_obj_mk (ClientData clientData, Tcl_Interp *interp,
1633 int argc, char **argv)
1635 Tcl_CmdInfo parent_info;
1640 interp->result = "wrong # args";
1643 if (get_parent_info (interp, argv[1], &parent_info, NULL) == TCL_ERROR)
1645 if (!(obj = ir_malloc (interp, sizeof(*obj))))
1649 obj->numberOfTermsRequested = 20;
1650 obj->preferredPositionInResponse = 1;
1652 obj->parent = (IRObj *) parent_info.clientData;
1653 Tcl_CreateCommand (interp, argv[1], ir_scan_obj_method,
1654 (ClientData) obj, ir_scan_obj_delete);
1658 /* ------------------------------------------------------- */
1660 static void ir_initResponse (void *obj, Z_InitResponse *initrs)
1662 if (!*initrs->result)
1663 printf("Connection rejected by target.\n");
1665 printf("Connection accepted by target.\n");
1666 if (initrs->implementationId)
1667 printf("ID : %s\n", initrs->implementationId);
1668 if (initrs->implementationName)
1669 printf("Name : %s\n", initrs->implementationName);
1670 if (initrs->implementationVersion)
1671 printf("Version: %s\n", initrs->implementationVersion);
1672 if (initrs->maximumRecordSize)
1673 printf ("MaximumRecordSize=%d\n", *initrs->maximumRecordSize);
1674 if (initrs->preferredMessageSize)
1675 printf ("PreferredMessageSize=%d\n", *initrs->preferredMessageSize);
1677 if (initrs->userInformationField)
1679 printf("UserInformationfield:\n");
1680 odr_external(&print, (Odr_external**)&initrs->
1681 userInformationField, 0);
1686 static void ir_handleRecords (void *o, Z_Records *zrs)
1689 IRSetObj *setobj = p->set_child;
1691 setobj->which = zrs->which;
1692 setobj->recordFlag = 1;
1693 if (zrs->which == Z_Records_NSD)
1695 const char *addinfo;
1697 setobj->numberOfRecordsReturned = 0;
1698 setobj->condition = *zrs->u.nonSurrogateDiagnostic->condition;
1699 free (setobj->addinfo);
1700 setobj->addinfo = NULL;
1701 addinfo = zrs->u.nonSurrogateDiagnostic->addinfo;
1702 if (addinfo && (setobj->addinfo = malloc (strlen(addinfo) + 1)))
1703 strcpy (setobj->addinfo, addinfo);
1704 printf ("Diagnostic response. %s (%d): %s\n",
1705 diagbib1_str (setobj->condition),
1707 setobj->addinfo ? setobj->addinfo : "");
1714 setobj->numberOfRecordsReturned =
1715 zrs->u.databaseOrSurDiagnostics->num_records;
1716 printf ("Got %d records\n", setobj->numberOfRecordsReturned);
1717 for (offset = 0; offset<setobj->numberOfRecordsReturned; offset++)
1719 rl = new_IR_record (setobj, setobj->start + offset,
1720 zrs->u.databaseOrSurDiagnostics->
1721 records[offset]->which);
1722 if (rl->which == Z_NamePlusRecord_surrogateDiagnostic)
1726 diagrec = zrs->u.databaseOrSurDiagnostics->
1727 records[offset]->u.surrogateDiagnostic;
1729 rl->u.diag.condition = *diagrec->condition;
1730 if (diagrec->addinfo && (rl->u.diag.addinfo =
1731 malloc (strlen (diagrec->addinfo)+1)))
1732 strcpy (rl->u.diag.addinfo, diagrec->addinfo);
1736 Z_DatabaseRecord *zr;
1739 zr = zrs->u.databaseOrSurDiagnostics->records[offset]
1741 oe = (Odr_external*) zr;
1742 if (oe->which == ODR_EXTERNAL_octet
1743 && zr->u.octet_aligned->len)
1745 const char *buf = (char*) zr->u.octet_aligned->buf;
1746 rl->u.marc.rec = iso2709_cvt (buf);
1749 rl->u.marc.rec = NULL;
1755 static void ir_searchResponse (void *o, Z_SearchResponse *searchrs)
1758 IRSetObj *setobj = p->set_child;
1759 Z_Records *zrs = searchrs->records;
1763 setobj->searchStatus = searchrs->searchStatus ? 1 : 0;
1764 setobj->resultCount = *searchrs->resultCount;
1765 printf ("Search response %d, %d hits\n",
1766 setobj->searchStatus, setobj->resultCount);
1768 ir_handleRecords (o, zrs);
1770 setobj->recordFlag = 0;
1773 printf ("Search response, no object!\n");
1777 static void ir_presentResponse (void *o, Z_PresentResponse *presrs)
1780 IRSetObj *setobj = p->set_child;
1781 Z_Records *zrs = presrs->records;
1783 printf ("Received presentResponse\n");
1785 ir_handleRecords (o, zrs);
1788 setobj->recordFlag = 0;
1789 printf ("No records!\n");
1793 static void ir_scanResponse (void *o, Z_ScanResponse *scanrs)
1796 IRScanObj *scanobj = p->scan_child;
1798 printf ("Received scanResponse\n");
1800 scanobj->scanStatus = *scanrs->scanStatus;
1801 printf ("scanStatus=%d\n", scanobj->scanStatus);
1803 if (scanrs->stepSize)
1804 scanobj->stepSize = *scanrs->stepSize;
1805 printf ("stepSize=%d\n", scanobj->stepSize);
1807 scanobj->numberOfEntriesReturned = *scanrs->numberOfEntriesReturned;
1808 printf ("numberOfEntriesReturned=%d\n", scanobj->numberOfEntriesReturned);
1810 if (scanrs->positionOfTerm)
1811 scanobj->positionOfTerm = *scanrs->positionOfTerm;
1813 scanobj->positionOfTerm = -1;
1814 printf ("positionOfTerm=%d\n", scanobj->positionOfTerm);
1816 if (scanrs->entries)
1818 scanobj->entries_flag = 1;
1819 scanobj->which = scanrs->entries->which;
1822 scanobj->entries_flag = 0;
1826 * ir_select_read: handle incoming packages
1828 void ir_select_read (ClientData clientData)
1830 IRObj *p = clientData;
1836 r = cs_rcvconnect (p->cs_link);
1840 ir_select_remove_write (cs_fileno (p->cs_link), p);
1843 printf ("cs_rcvconnect error\n");
1845 Tcl_Eval (p->interp, p->failback);
1846 do_disconnect (p, NULL, 0, NULL);
1850 Tcl_Eval (p->interp, p->callback);
1855 if ((r=cs_get (p->cs_link, &p->buf_in, &p->len_in)) <= 0)
1857 printf ("cs_get failed\n");
1858 ir_select_remove (cs_fileno (p->cs_link), p);
1860 Tcl_Eval (p->interp, p->failback);
1861 do_disconnect (p, NULL, 0, NULL);
1866 odr_setbuf (p->odr_in, p->buf_in, r);
1867 printf ("cs_get ok, got %d\n", r);
1868 if (!z_APDU (p->odr_in, &apdu, 0))
1870 printf ("%s\n", odr_errlist [odr_geterror (p->odr_in)]);
1872 Tcl_Eval (p->interp, p->failback);
1873 do_disconnect (p, NULL, 0, NULL);
1878 case Z_APDU_initResponse:
1879 ir_initResponse (p, apdu->u.initResponse);
1881 case Z_APDU_searchResponse:
1882 ir_searchResponse (p, apdu->u.searchResponse);
1884 case Z_APDU_presentResponse:
1885 ir_presentResponse (p, apdu->u.presentResponse);
1887 case Z_APDU_scanResponse:
1888 ir_scanResponse (p, apdu->u.scanResponse);
1891 printf("Received unknown APDU type (%d).\n",
1894 Tcl_Eval (p->interp, p->failback);
1895 do_disconnect (p, NULL, 0, NULL);
1898 Tcl_Eval (p->interp, p->callback);
1899 } while (cs_more (p->cs_link));
1903 * ir_select_write: handle outgoing packages - not yet written.
1905 void ir_select_write (ClientData clientData)
1907 IRObj *p = clientData;
1910 printf ("In write handler.....\n");
1913 r = cs_rcvconnect (p->cs_link);
1919 printf ("cs_rcvconnect error\n");
1920 ir_select_remove_write (cs_fileno (p->cs_link), p);
1922 Tcl_Eval (p->interp, p->failback);
1923 do_disconnect (p, NULL, 0, NULL);
1926 ir_select_remove_write (cs_fileno (p->cs_link), p);
1928 Tcl_Eval (p->interp, p->callback);
1931 if ((r=cs_put (p->cs_link, p->sbuf, p->slen)) < 0)
1933 printf ("select write fail\n");
1935 Tcl_Eval (p->interp, p->failback);
1936 do_disconnect (p, NULL, 0, NULL);
1938 else if (r == 0) /* remove select bit */
1940 ir_select_remove_write (cs_fileno (p->cs_link), p);
1944 /* ------------------------------------------------------- */
1947 * ir_tcl_init: Registration of TCL commands.
1949 int ir_tcl_init (Tcl_Interp *interp)
1951 Tcl_CreateCommand (interp, "ir", ir_obj_mk, (ClientData) NULL,
1952 (Tcl_CmdDeleteProc *) NULL);
1953 Tcl_CreateCommand (interp, "ir-set", ir_set_obj_mk,
1954 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
1955 Tcl_CreateCommand (interp, "ir-scan", ir_scan_obj_mk,
1956 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);