2 * IR toolkit for tcl/tk
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.26 1995-04-18 16:11:51 adam
8 * First version of graphical Scan. Some work on query-by-form.
10 * Revision 1.25 1995/04/17 09:37:17 adam
11 * Further development of scan.
13 * Revision 1.24 1995/04/11 14:16:42 adam
14 * Further work on scan. Response works. Entries aren't saved yet.
16 * Revision 1.23 1995/04/10 10:50:27 adam
17 * Result-set name defaults to suffix of ir-set name.
18 * Started working on scan. Not finished at this point.
20 * Revision 1.22 1995/03/31 10:43:03 adam
21 * More robust when getting bad MARC records.
23 * Revision 1.21 1995/03/31 08:56:37 adam
24 * New button "Search".
26 * Revision 1.20 1995/03/29 16:07:09 adam
27 * Bug fix: Didn't use setName in present request.
29 * Revision 1.19 1995/03/28 12:45:23 adam
30 * New ir method failback: called on disconnect/protocol error.
31 * New ir set/get method: protocol: SR / Z3950.
32 * Simple popup and disconnect when failback is invoked.
34 * Revision 1.18 1995/03/21 15:50:12 adam
37 * Revision 1.17 1995/03/21 13:41:03 adam
38 * Comstack cs_create not used too often. Non-blocking connect.
40 * Revision 1.16 1995/03/21 08:26:06 adam
41 * New method, setName, to specify the result set name (other than Default).
42 * New method, responseStatus, which returns diagnostic info, if any, after
43 * present response / search response.
45 * Revision 1.15 1995/03/20 15:24:07 adam
46 * Diagnostic records saved on searchResponse.
48 * Revision 1.14 1995/03/20 08:53:22 adam
49 * Event loop in tclmain.c rewritten. New method searchStatus.
51 * Revision 1.13 1995/03/17 18:26:17 adam
52 * Non-blocking i/o used now. Database names popup as cascade items.
54 * Revision 1.12 1995/03/17 15:45:00 adam
55 * Improved target/database setup.
57 * Revision 1.11 1995/03/16 17:54:03 adam
58 * Minor changes really.
60 * Revision 1.10 1995/03/15 16:14:50 adam
61 * Blocking arg in cs_create changed.
63 * Revision 1.9 1995/03/15 13:59:24 adam
66 * Revision 1.8 1995/03/15 08:25:16 adam
67 * New method presentStatus to check for error on present. Misc. cleanup
68 * of IRRecordList manipulations. Full MARC record presentation in
71 * Revision 1.7 1995/03/14 17:32:29 adam
72 * Presentation of full Marc record in popup window.
74 * Revision 1.6 1995/03/12 19:31:55 adam
75 * Pattern matching implemented when retrieving MARC records. More
76 * diagnostic functions.
78 * Revision 1.5 1995/03/10 18:00:15 adam
79 * Actual presentation in line-by-line format. RPN query support.
81 * Revision 1.4 1995/03/09 16:15:08 adam
82 * First presentRequest attempts. Hot-target list.
105 #include <diagbib1.h>
117 int preferredMessageSize;
118 int maximumRecordSize;
120 Odr_bitmask protocolVersion;
121 char *idAuthentication;
122 char *implementationName;
123 char *implementationId;
144 int smallSetUpperBound;
145 int largeSetLowerBound;
146 int mediumSetPresentNumber;
147 int replaceIndicator;
148 char **databaseNames;
149 int num_databaseNames;
155 struct IRSetObj_ *set_child;
156 struct IRScanObj_ *scan_child;
159 typedef struct IRRecordList_ {
171 struct IRRecordList_ *next;
174 typedef struct IRSetObj_ {
180 int numberOfRecordsReturned;
186 IRRecordList *record_list;
189 typedef struct IRScanEntry_ {
194 int globalOccurrences;
203 typedef struct IRScanDiag_ {
207 typedef struct IRScanObj_ {
210 int numberOfTermsRequested;
211 int preferredPositionInResponse;
214 int numberOfEntriesReturned;
223 IRScanEntry *entries;
224 IRScanDiag *nonSurrogateDiagnostics;
230 int (*method) (void *obj, Tcl_Interp *interp, int argc, char **argv);
233 static int do_disconnect (void *obj,Tcl_Interp *interp, int argc, char **argv);
235 static IRRecordList *new_IR_record (IRSetObj *setobj, int no, int which)
239 for (rl = setobj->record_list; rl; rl = rl->next)
245 case Z_NamePlusRecord_databaseRecord:
246 iso2709_rm (rl->u.marc.rec);
248 case Z_NamePlusRecord_surrogateDiagnostic:
249 free (rl->u.diag.addinfo);
250 rl->u.diag.addinfo = NULL;
258 rl = malloc (sizeof(*rl));
260 rl->next = setobj->record_list;
262 setobj->record_list = rl;
268 static IRRecordList *find_IR_record (IRSetObj *setobj, int no)
272 for (rl = setobj->record_list; rl; rl = rl->next)
279 * getsetint: Set/get integer value
281 static int get_set_int (int *val, Tcl_Interp *interp, int argc, char **argv)
287 if (Tcl_GetInt (interp, argv[2], val)==TCL_ERROR)
290 sprintf (buf, "%d", *val);
291 Tcl_AppendResult (interp, buf, NULL);
296 * mk_nonSurrogateDiagnostics: Make Tcl result with diagnostic info
298 static int mk_nonSurrogateDiagnostics (Tcl_Interp *interp,
305 Tcl_AppendElement (interp, "NSD");
306 sprintf (buf, "%d", condition);
307 Tcl_AppendElement (interp, buf);
308 cp = diagbib1_str (condition);
310 Tcl_AppendElement (interp, (char*) cp);
312 Tcl_AppendElement (interp, "");
314 Tcl_AppendElement (interp, (char*) addinfo);
316 Tcl_AppendElement (interp, "");
321 * get_parent_info: Returns information about parent object.
323 static int get_parent_info (Tcl_Interp *interp, const char *name,
324 Tcl_CmdInfo *parent_info,
327 char parent_name[128];
328 const char *csep = strrchr (name, '.');
333 interp->result = "missing .";
341 memcpy (parent_name, name, pos);
342 parent_name[pos] = '\0';
343 if (!Tcl_GetCommandInfo (interp, parent_name, parent_info))
345 interp->result = "No parent";
352 * ir_method: Search for method in table and invoke method handler
354 int ir_method (void *obj, Tcl_Interp *interp, int argc, char **argv,
358 for (t = tab; t->name; t++)
359 if (!strcmp (t->name, argv[1]))
360 return (*t->method)(obj, interp, argc, argv);
361 Tcl_AppendResult (interp, "Bad method. Possible values:", NULL);
362 for (t = tab; t->name; t++)
363 Tcl_AppendResult (interp, " ", t->name, NULL);
368 * ir_method_r: Get status for all readable elements
370 int ir_method_r (void *obj, Tcl_Interp *interp, int argc, char **argv,
378 for (; tab->name; tab++)
381 argv_n[1] = tab->name;
382 Tcl_AppendResult (interp, "{", NULL);
383 (*tab->method)(obj, interp, argc_n, argv_n);
384 Tcl_AppendResult (interp, "} ", NULL);
390 * ir_asc2bitmask: Ascii to ODR bitmask conversion
392 int ir_asc2bitmask (const char *asc, Odr_bitmask *ob)
394 const char *cp = asc + strlen(asc);
401 ODR_MASK_SET (ob, bitno);
408 * ir_strdup: Duplicate string
410 int ir_strdup (Tcl_Interp *interp, char** p, const char *s)
412 *p = malloc (strlen(s)+1);
415 interp->result = "strdup fail";
423 * ir_malloc: Malloc function
425 void *ir_malloc (Tcl_Interp *interp, size_t size)
427 static char buf[128];
428 void *p = malloc (size);
432 sprintf (buf, "Malloc fail. %ld bytes requested", (long) size);
433 interp->result = buf;
439 /* ------------------------------------------------------- */
442 * do_init_request: init method on IR object
444 static int do_init_request (void *obj, Tcl_Interp *interp,
445 int argc, char **argv)
447 Z_APDU apdu, *apdup = &apdu;
454 interp->result = "not connected";
458 req.options = &p->options;
459 req.protocolVersion = &p->protocolVersion;
460 req.preferredMessageSize = &p->preferredMessageSize;
461 req.maximumRecordSize = &p->maximumRecordSize;
463 req.idAuthentication = p->idAuthentication;
464 req.implementationId = p->implementationId;
465 req.implementationName = p->implementationName;
466 req.implementationVersion = "0.1";
467 req.userInformationField = 0;
469 apdu.u.initRequest = &req;
470 apdu.which = Z_APDU_initRequest;
472 if (!z_APDU (p->odr_out, &apdup, 0))
474 Tcl_AppendResult (interp, odr_errlist [odr_geterror (p->odr_out)],
476 odr_reset (p->odr_out);
479 p->sbuf = odr_getbuf (p->odr_out, &p->slen);
480 if ((r=cs_put (p->cs_link, p->sbuf, p->slen)) < 0)
482 interp->result = "cs_put failed in init";
483 do_disconnect (p, NULL, 0, NULL);
488 ir_select_add_write (cs_fileno(p->cs_link), p);
489 printf("Sent part of initializeRequest (%d bytes).\n", p->slen);
492 printf("Sent whole initializeRequest (%d bytes).\n", p->slen);
497 * do_protocolVersion: Set protocol Version
499 static int do_protocolVersion (void *obj, Tcl_Interp *interp,
500 int argc, char **argv)
503 ir_asc2bitmask (argv[2], &((IRObj *) obj)->protocolVersion);
508 * do_options: Set options
510 static int do_options (void *obj, Tcl_Interp *interp,
511 int argc, char **argv)
514 ir_asc2bitmask (argv[2], &((IRObj *) obj)->options);
519 * do_preferredMessageSize: Set/get preferred message size
521 static int do_preferredMessageSize (void *obj, Tcl_Interp *interp,
522 int argc, char **argv)
525 return get_set_int (&p->preferredMessageSize, interp, argc, argv);
529 * do_maximumRecordSize: Set/get maximum record size
531 static int do_maximumRecordSize (void *obj, Tcl_Interp *interp,
532 int argc, char **argv)
535 return get_set_int (&p->maximumRecordSize, interp, argc, argv);
540 * do_implementationName: Set/get Implementation Name.
542 static int do_implementationName (void *obj, Tcl_Interp *interp,
543 int argc, char **argv)
547 free (((IRObj*)obj)->implementationName);
548 if (ir_strdup (interp, &((IRObj*) obj)->implementationName, argv[2])
552 Tcl_AppendResult (interp, ((IRObj*)obj)->implementationName,
558 * do_implementationId: Set/get Implementation Id.
560 static int do_implementationId (void *obj, Tcl_Interp *interp,
561 int argc, char **argv)
565 free (((IRObj*)obj)->implementationId);
566 if (ir_strdup (interp, &((IRObj*) obj)->implementationId, argv[2])
570 Tcl_AppendResult (interp, ((IRObj*)obj)->implementationId,
576 * do_idAuthentication: Set/get id Authentication
578 static int do_idAuthentication (void *obj, Tcl_Interp *interp,
579 int argc, char **argv)
583 free (((IRObj*)obj)->idAuthentication);
584 if (ir_strdup (interp, &((IRObj*) obj)->idAuthentication, argv[2])
588 Tcl_AppendResult (interp, ((IRObj*)obj)->idAuthentication,
594 * do_connect: connect method on IR object
596 static int do_connect (void *obj, Tcl_Interp *interp,
597 int argc, char **argv)
602 int protocol_type = CS_Z3950;
608 interp->result = "already connected";
611 if (!strcmp (p->protocol_type, "Z3950"))
612 protocol_type = CS_Z3950;
613 else if (!strcmp (p->protocol_type, "SR"))
614 protocol_type = CS_SR;
617 interp->result = "bad protocol type";
620 if (!strcmp (p->cs_type, "tcpip"))
622 p->cs_link = cs_create (tcpip_type, CS_BLOCK, protocol_type);
623 addr = tcpip_strtoaddr (argv[2]);
626 interp->result = "tcpip_strtoaddr fail";
629 printf ("tcp/ip connect %s\n", argv[2]);
632 else if (!strcmp (p->cs_type, "mosi"))
634 p->cs_link = cs_create (mosi_type, CS_BLOCK, protocol_type);
635 addr = mosi_strtoaddr (argv[2]);
638 interp->result = "mosi_strtoaddr fail";
641 printf ("mosi connect %s\n", argv[2]);
646 interp->result = "unknown comstack type";
649 if (ir_strdup (interp, &p->hostname, argv[2]) == TCL_ERROR)
651 if ((r=cs_connect (p->cs_link, addr)) < 0)
653 interp->result = "cs_connect fail";
654 do_disconnect (p, NULL, 0, NULL);
657 ir_select_add (cs_fileno (p->cs_link), p);
660 ir_select_add_write (cs_fileno (p->cs_link), p);
667 Tcl_Eval (p->interp, p->callback);
671 Tcl_AppendElement (interp, p->hostname);
676 * do_disconnect: disconnect method on IR object
678 static int do_disconnect (void *obj, Tcl_Interp *interp,
679 int argc, char **argv)
687 ir_select_remove_write (cs_fileno (p->cs_link), p);
688 ir_select_remove (cs_fileno (p->cs_link), p);
691 cs_close (p->cs_link);
694 assert (!p->cs_link);
699 * do_comstack: Set/get comstack method on IR object
701 static int do_comstack (void *o, Tcl_Interp *interp,
702 int argc, char **argv)
709 if (ir_strdup (interp, &obj->cs_type, argv[2]) == TCL_ERROR)
712 Tcl_AppendElement (interp, obj->cs_type);
717 * do_protocol: Set/get protocol method on IR object
719 static int do_protocol (void *o, Tcl_Interp *interp,
720 int argc, char **argv)
726 free (obj->protocol_type);
727 if (ir_strdup (interp, &obj->protocol_type, argv[2]) == TCL_ERROR)
730 Tcl_AppendElement (interp, obj->protocol_type);
735 * do_callback: add callback
737 static int do_callback (void *obj, Tcl_Interp *interp,
738 int argc, char **argv)
745 if (ir_strdup (interp, &p->callback, argv[2]) == TCL_ERROR)
753 * do_failback: add error handle callback
755 static int do_failback (void *obj, Tcl_Interp *interp,
756 int argc, char **argv)
763 if (ir_strdup (interp, &p->failback, argv[2]) == TCL_ERROR)
771 * do_databaseNames: specify database names
773 static int do_databaseNames (void *obj, Tcl_Interp *interp,
774 int argc, char **argv)
781 for (i=0; i<p->num_databaseNames; i++)
782 Tcl_AppendElement (interp, p->databaseNames[i]);
785 if (p->databaseNames)
787 for (i=0; i<p->num_databaseNames; i++)
788 free (p->databaseNames[i]);
789 free (p->databaseNames);
791 p->num_databaseNames = argc - 2;
792 if (!(p->databaseNames = ir_malloc (interp,
793 sizeof(*p->databaseNames) * p->num_databaseNames)))
795 for (i=0; i<p->num_databaseNames; i++)
797 if (ir_strdup (interp, &p->databaseNames[i], argv[2+i])
805 * do_query: Set/Get query mothod
807 static int do_query (void *obj, Tcl_Interp *interp,
808 int argc, char **argv)
813 free (p->query_method);
814 if (ir_strdup (interp, &p->query_method, argv[2]) == TCL_ERROR)
817 Tcl_AppendResult (interp, p->query_method, NULL);
822 * do_replaceIndicator: Set/get replace Set indicator
824 static int do_replaceIndicator (void *obj, Tcl_Interp *interp,
825 int argc, char **argv)
828 return get_set_int (&p->replaceIndicator, interp, argc, argv);
832 * ir_obj_method: IR Object methods
834 static int ir_obj_method (ClientData clientData, Tcl_Interp *interp,
835 int argc, char **argv)
837 static IRMethod tab[] = {
838 { 1, "comstack", do_comstack },
839 { 1, "protocol", do_protocol },
840 { 1, "connect", do_connect },
841 { 0, "protocolVersion", do_protocolVersion },
842 { 0, "options", do_options },
843 { 1, "preferredMessageSize", do_preferredMessageSize },
844 { 1, "maximumRecordSize", do_maximumRecordSize },
845 { 1, "implementationName", do_implementationName },
846 { 1, "implementationId", do_implementationId },
847 { 1, "idAuthentication", do_idAuthentication },
848 { 0, "init", do_init_request },
849 { 0, "disconnect", do_disconnect },
850 { 0, "callback", do_callback },
851 { 0, "failback", do_failback },
852 { 1, "databaseNames", do_databaseNames},
853 { 1, "replaceIndicator", do_replaceIndicator},
854 { 1, "query", do_query },
858 return ir_method_r (clientData, interp, argc, argv, tab);
859 return ir_method (clientData, interp, argc, argv, tab);
863 * ir_obj_delete: IR Object disposal
865 static void ir_obj_delete (ClientData clientData)
867 free ( (void*) clientData);
871 * ir_obj_mk: IR Object creation
873 static int ir_obj_mk (ClientData clientData, Tcl_Interp *interp,
874 int argc, char **argv)
881 interp->result = "wrong # args";
884 if (!(obj = ir_malloc (interp, sizeof(*obj))))
886 if (ir_strdup (interp, &obj->cs_type, "tcpip") == TCL_ERROR)
888 if (ir_strdup (interp, &obj->protocol_type, "Z3950") == TCL_ERROR)
891 obj->bib1.proto = PROTO_Z3950;
892 obj->bib1.class = CLASS_ATTSET;
893 obj->bib1.value = VAL_BIB1;
895 obj->maximumRecordSize = 32768;
896 obj->preferredMessageSize = 4096;
897 obj->connectFlag = 0;
899 obj->idAuthentication = NULL;
901 if (ir_strdup (interp, &obj->implementationName, "TCL/TK on YAZ")
905 if (ir_strdup (interp, &obj->implementationId, "TCL/TK/YAZ")
909 obj->smallSetUpperBound = 0;
910 obj->largeSetLowerBound = 2;
911 obj->mediumSetPresentNumber = 0;
912 obj->replaceIndicator = 1;
914 obj->databaseNames = NULL;
915 obj->num_databaseNames = 0;
917 obj->num_databaseNames = 1;
918 if (!(obj->databaseNames = ir_malloc (interp,
919 sizeof(*obj->databaseNames))))
921 if (ir_strdup (interp, &obj->databaseNames[0], "Default") == TCL_ERROR)
925 obj->hostname = NULL;
927 if (ir_strdup (interp, &obj->query_method, "rpn") == TCL_ERROR)
929 obj->bibset = ccl_qual_mk ();
930 if ((inf = fopen ("default.bib", "r")))
932 ccl_qual_file (obj->bibset, inf);
935 ODR_MASK_ZERO (&obj->protocolVersion);
936 ODR_MASK_SET (&obj->protocolVersion, 0);
937 ODR_MASK_SET (&obj->protocolVersion, 1);
939 ODR_MASK_ZERO (&obj->options);
940 ODR_MASK_SET (&obj->options, 0);
942 obj->odr_in = odr_createmem (ODR_DECODE);
943 obj->odr_out = odr_createmem (ODR_ENCODE);
944 obj->odr_pr = odr_createmem (ODR_PRINT);
946 obj->len_out = 10000;
947 if (!(obj->buf_out = ir_malloc (interp, obj->len_out)))
949 odr_setbuf (obj->odr_out, obj->buf_out, obj->len_out);
954 obj->callback = NULL;
955 obj->failback = NULL;
956 Tcl_CreateCommand (interp, argv[1], ir_obj_method,
957 (ClientData) obj, ir_obj_delete);
961 /* ------------------------------------------------------- */
963 * do_search: Do search request
965 static int do_search (void *o, Tcl_Interp *interp,
966 int argc, char **argv)
970 Z_APDU apdu, *apdup = &apdu;
973 IRObj *p = obj->parent;
979 interp->result = "wrong # args";
982 if (!p->num_databaseNames)
984 interp->result = "no databaseNames";
989 interp->result = "not connected";
992 apdu.which = Z_APDU_searchRequest;
993 apdu.u.searchRequest = &req;
996 req.smallSetUpperBound = &p->smallSetUpperBound;
997 req.largeSetLowerBound = &p->largeSetLowerBound;
998 req.mediumSetPresentNumber = &p->mediumSetPresentNumber;
999 req.replaceIndicator = &p->replaceIndicator;
1000 req.resultSetName = obj->setName ? obj->setName : "Default";
1001 req.num_databaseNames = p->num_databaseNames;
1002 req.databaseNames = p->databaseNames;
1004 for (r=0; r<p->num_databaseNames; r++)
1006 printf (" %s", p->databaseNames[r]);
1008 req.smallSetElementSetNames = 0;
1009 req.mediumSetElementSetNames = 0;
1010 req.preferredRecordSyntax = 0;
1013 if (!strcmp (p->query_method, "rpn"))
1017 struct ccl_rpn_node *rpn;
1018 Z_RPNQuery *RPNquery;
1020 rpn = ccl_find_str(p->bibset, argv[2], &error, &pos);
1023 Tcl_AppendResult (interp, "CCL error: ", ccl_err_msg(error),NULL);
1026 query.which = Z_Query_type_1;
1027 assert((RPNquery = ccl_rpn_query(rpn)));
1028 RPNquery->attributeSetId = oid_getoidbyent (&p->bib1);
1029 query.u.type_1 = RPNquery;
1032 else if (!strcmp (p->query_method, "ccl"))
1034 query.which = Z_Query_type_2;
1035 query.u.type_2 = &ccl_query;
1036 ccl_query.buf = (unsigned char *) argv[2];
1037 ccl_query.len = strlen (argv[2]);
1042 interp->result = "unknown query method";
1045 if (!z_APDU (p->odr_out, &apdup, 0))
1047 interp->result = odr_errlist [odr_geterror (p->odr_out)];
1048 odr_reset (p->odr_out);
1051 p->sbuf = odr_getbuf (p->odr_out, &p->slen);
1052 if ((r=cs_put (p->cs_link, p->sbuf, p->slen)) < 0)
1054 interp->result = "cs_put failed in search";
1059 ir_select_add_write (cs_fileno(p->cs_link), p);
1060 printf("Sent part of searchRequest (%d bytes).\n", p->slen);
1064 printf ("Whole search request\n");
1070 * do_resultCount: Get number of hits
1072 static int do_resultCount (void *o, Tcl_Interp *interp,
1073 int argc, char **argv)
1077 return get_set_int (&obj->resultCount, interp, argc, argv);
1081 * do_searchStatus: Get search status (after search response)
1083 static int do_searchStatus (void *o, Tcl_Interp *interp,
1084 int argc, char **argv)
1088 return get_set_int (&obj->searchStatus, interp, argc, argv);
1092 * do_setName: Set result Set name
1094 static int do_setName (void *o, Tcl_Interp *interp,
1095 int argc, char **argv)
1101 free (obj->setName);
1102 if (ir_strdup (interp, &obj->setName, argv[2])
1106 Tcl_AppendElement (interp, obj->setName);
1111 * do_numberOfRecordsReturned: Get number of records returned
1113 static int do_numberOfRecordsReturned (void *o, Tcl_Interp *interp,
1114 int argc, char **argv)
1118 return get_set_int (&obj->numberOfRecordsReturned, interp, argc, argv);
1121 static int get_marc_fields(Tcl_Interp *interp, Iso2709Rec rec,
1122 int argc, char **argv)
1129 a = iso2709_a_mk (rec);
1130 while (iso2709_a_search (a, argv[4], argv[5], argv[6]))
1132 if (!(iso2709_a_info_field (a, NULL, NULL, NULL, &data)))
1134 Tcl_AppendElement (interp, data);
1142 static int get_marc_lines(Tcl_Interp *interp, Iso2709Rec rec,
1143 int argc, char **argv)
1154 a = iso2709_a_mk (rec);
1155 while (iso2709_a_search (a, argv[4], argv[5], argv[6]))
1157 if (!(iso2709_a_info_field (a, &tag, &indicator, &identifier, &data)))
1159 if (strcmp (tag, ptag))
1162 Tcl_AppendResult (interp, "}} ", NULL);
1164 Tcl_AppendResult (interp, "{", tag, " {} {", NULL);
1166 Tcl_AppendResult (interp, "{", tag, " {", indicator,
1171 Tcl_AppendResult (interp, "{{}", NULL);
1173 Tcl_AppendResult (interp, "{", identifier, NULL);
1174 Tcl_AppendElement (interp, data);
1175 Tcl_AppendResult (interp, "} ", NULL);
1179 Tcl_AppendResult (interp, "}} ", NULL);
1185 * do_recordType: Return record type (if any) at position.
1187 static int do_recordType (void *o, Tcl_Interp *interp, int argc, char **argv)
1195 sprintf (interp->result, "wrong # args");
1198 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
1200 rl = find_IR_record (obj, offset);
1205 case Z_NamePlusRecord_databaseRecord:
1206 interp->result = "databaseRecord";
1208 case Z_NamePlusRecord_surrogateDiagnostic:
1209 interp->result = "surrogateDiagnostic";
1216 * do_recordDiag: Return diagnostic record info
1218 static int do_recordDiag (void *o, Tcl_Interp *interp, int argc, char **argv)
1227 sprintf (interp->result, "wrong # args");
1230 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
1232 rl = find_IR_record (obj, offset);
1235 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
1238 if (rl->which != Z_NamePlusRecord_surrogateDiagnostic)
1240 Tcl_AppendResult (interp, "No Diagnostic record at #", argv[2], NULL);
1243 sprintf (buf, "%d", rl->u.diag.condition);
1244 Tcl_AppendResult (interp, buf, " {",
1245 (rl->u.diag.addinfo ? rl->u.diag.addinfo : ""),
1251 * do_recordMarc: Get ISO2709 Record lines/fields
1253 static int do_recordMarc (void *o, Tcl_Interp *interp, int argc, char **argv)
1261 sprintf (interp->result, "wrong # args");
1264 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
1266 rl = find_IR_record (obj, offset);
1269 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
1272 if (rl->which != Z_NamePlusRecord_databaseRecord)
1274 Tcl_AppendResult (interp, "No MARC record at #", argv[2], NULL);
1277 if (!strcmp (argv[3], "field"))
1278 return get_marc_fields (interp, rl->u.marc.rec, argc, argv);
1279 else if (!strcmp (argv[3], "line"))
1280 return get_marc_lines (interp, rl->u.marc.rec, argc, argv);
1283 Tcl_AppendResult (interp, "field/line expected", NULL);
1290 * do_responseStatus: Return response status (present or search)
1292 static int do_responseStatus (void *o, Tcl_Interp *interp,
1293 int argc, char **argv)
1297 if (!obj->recordFlag)
1301 case Z_Records_DBOSD:
1302 Tcl_AppendElement (interp, "DBOSD");
1305 return mk_nonSurrogateDiagnostics (interp, obj->condition,
1312 * do_present: Perform Present Request
1315 static int do_present (void *o, Tcl_Interp *interp,
1316 int argc, char **argv)
1319 IRObj *p = obj->parent;
1320 Z_APDU apdu, *apdup = &apdu;
1321 Z_PresentRequest req;
1328 if (Tcl_GetInt (interp, argv[2], &start) == TCL_ERROR)
1335 if (Tcl_GetInt (interp, argv[3], &number) == TCL_ERROR)
1342 interp->result = "not connected";
1346 obj->number = number;
1348 apdu.which = Z_APDU_presentRequest;
1349 apdu.u.presentRequest = &req;
1350 req.referenceId = 0;
1351 /* sprintf(setstring, "%d", setnumber); */
1353 req.resultSetId = obj->setName ? obj->setName : "Default";
1355 req.resultSetStartPoint = &start;
1356 req.numberOfRecordsRequested = &number;
1357 req.elementSetNames = 0;
1358 req.preferredRecordSyntax = 0;
1360 if (!z_APDU (p->odr_out, &apdup, 0))
1362 interp->result = odr_errlist [odr_geterror (p->odr_out)];
1363 odr_reset (p->odr_out);
1366 p->sbuf = odr_getbuf (p->odr_out, &p->slen);
1367 if ((r=cs_put (p->cs_link, p->sbuf, p->slen)) < 0)
1369 interp->result = "cs_put failed in present";
1374 ir_select_add_write (cs_fileno(p->cs_link), p);
1375 printf ("Part of present request, start=%d, num=%d (%d bytes)\n",
1376 start, number, p->slen);
1380 printf ("Whole present request, start=%d, num=%d (%d bytes)\n",
1381 start, number, p->slen);
1387 * do_loadFile: Load result set from file
1390 static int do_loadFile (void *o, Tcl_Interp *interp,
1391 int argc, char **argv)
1393 IRSetObj *setobj = o;
1400 interp->result = "wrong # args";
1403 inf = fopen (argv[2], "r");
1406 Tcl_AppendResult (interp, "Cannot open ", argv[2], NULL);
1409 while ((buf = iso2709_read (inf)))
1414 rec = iso2709_cvt (buf);
1417 rl = new_IR_record (setobj, no, Z_NamePlusRecord_databaseRecord);
1418 rl->u.marc.rec = rec;
1421 setobj->numberOfRecordsReturned = no-1;
1427 * ir_set_obj_method: IR Set Object methods
1429 static int ir_set_obj_method (ClientData clientData, Tcl_Interp *interp,
1430 int argc, char **argv)
1432 static IRMethod tab[] = {
1433 { 0, "search", do_search },
1434 { 0, "searchStatus", do_searchStatus },
1435 { 0, "setName", do_setName },
1436 { 0, "resultCount", do_resultCount },
1437 { 0, "numberOfRecordsReturned", do_numberOfRecordsReturned },
1438 { 0, "present", do_present },
1439 { 0, "recordType", do_recordType },
1440 { 0, "recordMarc", do_recordMarc },
1441 { 0, "recordDiag", do_recordDiag },
1442 { 0, "responseStatus", do_responseStatus },
1443 { 0, "loadFile", do_loadFile },
1449 interp->result = "wrong # args";
1452 return ir_method (clientData, interp, argc, argv, tab);
1456 * ir_set_obj_delete: IR Set Object disposal
1458 static void ir_set_obj_delete (ClientData clientData)
1460 free ( (void*) clientData);
1464 * ir_set_obj_mk: IR Set Object creation
1466 static int ir_set_obj_mk (ClientData clientData, Tcl_Interp *interp,
1467 int argc, char **argv)
1469 Tcl_CmdInfo parent_info;
1475 interp->result = "wrong # args";
1478 if (get_parent_info (interp, argv[1], &parent_info, &suffix) == TCL_ERROR)
1480 if (!(obj = ir_malloc (interp, sizeof(*obj))))
1482 if (ir_strdup (interp, &obj->setName, suffix) == TCL_ERROR)
1484 obj->record_list = NULL;
1485 obj->addinfo = NULL;
1486 obj->parent = (IRObj *) parent_info.clientData;
1487 Tcl_CreateCommand (interp, argv[1], ir_set_obj_method,
1488 (ClientData) obj, ir_set_obj_delete);
1492 /* ------------------------------------------------------- */
1495 * do_scan: Perform scan
1497 static int do_scan (void *o, Tcl_Interp *interp, int argc, char **argv)
1500 Z_APDU apdu, *apdup = &apdu;
1502 IRObj *p = obj->parent;
1508 interp->result = "wrong # args";
1511 if (!p->num_databaseNames)
1513 interp->result = "no databaseNames";
1518 interp->result = "not connected";
1521 apdu.which = Z_APDU_scanRequest;
1522 apdu.u.scanRequest = &req;
1523 req.referenceId = NULL;
1524 req.num_databaseNames = p->num_databaseNames;
1525 req.databaseNames = p->databaseNames;
1526 req.attributeSet = oid_getoidbyent (&p->bib1);
1528 if (!(req.termListAndStartPoint =
1529 ir_malloc (interp, sizeof(*req.termListAndStartPoint))))
1531 req.termListAndStartPoint->num_attributes = 0;
1532 req.termListAndStartPoint->attributeList = NULL;
1533 if (!(req.termListAndStartPoint->term = ir_malloc (interp,
1536 req.termListAndStartPoint->term->which = Z_Term_general;
1537 if (!(req.termListAndStartPoint->term->u.general =
1538 ir_malloc (interp, sizeof(*req.termListAndStartPoint->
1541 if (ir_strdup (interp, &req.termListAndStartPoint->term->u.general->buf,
1542 argv[2]) == TCL_ERROR)
1544 req.termListAndStartPoint->term->u.general->len =
1545 req.termListAndStartPoint->term->u.general->size = strlen(argv[2]);
1546 req.stepSize = &obj->stepSize;
1547 req.numberOfTermsRequested = &obj->numberOfTermsRequested;
1548 req.preferredPositionInResponse = &obj->preferredPositionInResponse;
1549 printf ("stepSize=%d\n", *req.stepSize);
1550 printf ("numberOfTermsRequested=%d\n", *req.numberOfTermsRequested);
1551 printf ("preferredPositionInResponse=%d\n",
1552 *req.preferredPositionInResponse);
1554 if (!z_APDU (p->odr_out, &apdup, 0))
1556 interp->result = odr_errlist [odr_geterror (p->odr_out)];
1557 odr_reset (p->odr_out);
1560 p->sbuf = odr_getbuf (p->odr_out, &p->slen);
1561 if ((r=cs_put (p->cs_link, p->sbuf, p->slen)) < 0)
1563 interp->result = "cs_put failed in scan";
1568 ir_select_add_write (cs_fileno(p->cs_link), p);
1569 printf("Sent part of scanRequest (%d bytes).\n", p->slen);
1573 printf ("Whole scan request\n");
1579 * do_stepSize: Set/get replace Step Size
1581 static int do_stepSize (void *obj, Tcl_Interp *interp,
1582 int argc, char **argv)
1585 return get_set_int (&p->stepSize, interp, argc, argv);
1589 * do_numberOfTermsRequested: Set/get Number of Terms requested
1591 static int do_numberOfTermsRequested (void *obj, Tcl_Interp *interp,
1592 int argc, char **argv)
1595 return get_set_int (&p->numberOfTermsRequested, interp, argc, argv);
1600 * do_preferredPositionInResponse: Set/get preferred Position
1602 static int do_preferredPositionInResponse (void *obj, Tcl_Interp *interp,
1603 int argc, char **argv)
1606 return get_set_int (&p->preferredPositionInResponse, interp, argc, argv);
1610 * do_scanStatus: Get scan status
1612 static int do_scanStatus (void *obj, Tcl_Interp *interp,
1613 int argc, char **argv)
1616 return get_set_int (&p->scanStatus, interp, argc, argv);
1620 * do_numberOfEntriesReturned: Get number of Entries returned
1622 static int do_numberOfEntriesReturned (void *obj, Tcl_Interp *interp,
1623 int argc, char **argv)
1626 return get_set_int (&p->numberOfEntriesReturned, interp, argc, argv);
1630 * do_positionOfTerm: Get position of Term
1632 static int do_positionOfTerm (void *obj, Tcl_Interp *interp,
1633 int argc, char **argv)
1636 return get_set_int (&p->positionOfTerm, interp, argc, argv);
1640 * do_scanLine: get Scan Line (surrogate or normal) after response
1642 static int do_scanLine (void *obj, Tcl_Interp *interp, int argc, char **argv)
1650 interp->result = "wrong # args";
1653 if (Tcl_GetInt (interp, argv[2], &i) == TCL_ERROR)
1655 if (!p->entries_flag || p->which != Z_ListEntries_entries || !p->entries
1656 || i >= p->num_entries || i < 0)
1658 switch (p->entries[i].which)
1660 case Z_Entry_termInfo:
1661 Tcl_AppendElement (interp, "T");
1662 if (p->entries[i].u.term.buf)
1663 Tcl_AppendElement (interp, p->entries[i].u.term.buf);
1665 Tcl_AppendElement (interp, "");
1666 sprintf (numstr, "%d", p->entries[i].u.term.globalOccurrences);
1667 Tcl_AppendElement (interp, numstr);
1669 case Z_Entry_surrogateDiagnostic:
1671 mk_nonSurrogateDiagnostics (interp, p->entries[i].u.diag.condition,
1672 p->entries[i].u.diag.addinfo);
1679 * ir_scan_obj_method: IR Scan Object methods
1681 static int ir_scan_obj_method (ClientData clientData, Tcl_Interp *interp,
1682 int argc, char **argv)
1684 static IRMethod tab[] = {
1685 { 0, "scan", do_scan },
1686 { 0, "stepSize", do_stepSize },
1687 { 0, "numberOfTermsRequested", do_numberOfTermsRequested },
1688 { 0, "preferredPositionInResponse", do_preferredPositionInResponse },
1689 { 0, "scanStatus", do_scanStatus },
1690 { 0, "numberOfEntriesReturned", do_numberOfEntriesReturned },
1691 { 0, "positionOfTerm", do_positionOfTerm },
1692 { 0, "scanLine", do_scanLine },
1698 interp->result = "wrong # args";
1701 return ir_method (clientData, interp, argc, argv, tab);
1705 * ir_scan_obj_delete: IR Scan Object disposal
1707 static void ir_scan_obj_delete (ClientData clientData)
1709 free ( (void*) clientData);
1713 * ir_scan_obj_mk: IR Scan Object creation
1715 static int ir_scan_obj_mk (ClientData clientData, Tcl_Interp *interp,
1716 int argc, char **argv)
1718 Tcl_CmdInfo parent_info;
1723 interp->result = "wrong # args";
1726 if (get_parent_info (interp, argv[1], &parent_info, NULL) == TCL_ERROR)
1728 if (!(obj = ir_malloc (interp, sizeof(*obj))))
1732 obj->numberOfTermsRequested = 20;
1733 obj->preferredPositionInResponse = 1;
1735 obj->entries = NULL;
1736 obj->nonSurrogateDiagnostics = NULL;
1738 obj->parent = (IRObj *) parent_info.clientData;
1739 Tcl_CreateCommand (interp, argv[1], ir_scan_obj_method,
1740 (ClientData) obj, ir_scan_obj_delete);
1744 /* ------------------------------------------------------- */
1746 static void ir_initResponse (void *obj, Z_InitResponse *initrs)
1748 if (!*initrs->result)
1749 printf("Connection rejected by target.\n");
1751 printf("Connection accepted by target.\n");
1752 if (initrs->implementationId)
1753 printf("ID : %s\n", initrs->implementationId);
1754 if (initrs->implementationName)
1755 printf("Name : %s\n", initrs->implementationName);
1756 if (initrs->implementationVersion)
1757 printf("Version: %s\n", initrs->implementationVersion);
1758 if (initrs->maximumRecordSize)
1759 printf ("MaximumRecordSize=%d\n", *initrs->maximumRecordSize);
1760 if (initrs->preferredMessageSize)
1761 printf ("PreferredMessageSize=%d\n", *initrs->preferredMessageSize);
1763 if (initrs->userInformationField)
1765 printf("UserInformationfield:\n");
1766 odr_external(&print, (Odr_external**)&initrs->
1767 userInformationField, 0);
1772 static void ir_handleRecords (void *o, Z_Records *zrs)
1775 IRSetObj *setobj = p->set_child;
1777 setobj->which = zrs->which;
1778 setobj->recordFlag = 1;
1779 if (zrs->which == Z_Records_NSD)
1781 const char *addinfo;
1783 setobj->numberOfRecordsReturned = 0;
1784 setobj->condition = *zrs->u.nonSurrogateDiagnostic->condition;
1785 free (setobj->addinfo);
1786 setobj->addinfo = NULL;
1787 addinfo = zrs->u.nonSurrogateDiagnostic->addinfo;
1788 if (addinfo && (setobj->addinfo = malloc (strlen(addinfo) + 1)))
1789 strcpy (setobj->addinfo, addinfo);
1790 printf ("Diagnostic response. %s (%d): %s\n",
1791 diagbib1_str (setobj->condition),
1793 setobj->addinfo ? setobj->addinfo : "");
1800 setobj->numberOfRecordsReturned =
1801 zrs->u.databaseOrSurDiagnostics->num_records;
1802 printf ("Got %d records\n", setobj->numberOfRecordsReturned);
1803 for (offset = 0; offset<setobj->numberOfRecordsReturned; offset++)
1805 rl = new_IR_record (setobj, setobj->start + offset,
1806 zrs->u.databaseOrSurDiagnostics->
1807 records[offset]->which);
1808 if (rl->which == Z_NamePlusRecord_surrogateDiagnostic)
1812 diagrec = zrs->u.databaseOrSurDiagnostics->
1813 records[offset]->u.surrogateDiagnostic;
1815 rl->u.diag.condition = *diagrec->condition;
1816 if (diagrec->addinfo && (rl->u.diag.addinfo =
1817 malloc (strlen (diagrec->addinfo)+1)))
1818 strcpy (rl->u.diag.addinfo, diagrec->addinfo);
1822 Z_DatabaseRecord *zr;
1825 zr = zrs->u.databaseOrSurDiagnostics->records[offset]
1827 oe = (Odr_external*) zr;
1828 if (oe->which == ODR_EXTERNAL_octet
1829 && zr->u.octet_aligned->len)
1831 const char *buf = (char*) zr->u.octet_aligned->buf;
1832 rl->u.marc.rec = iso2709_cvt (buf);
1835 rl->u.marc.rec = NULL;
1841 static void ir_searchResponse (void *o, Z_SearchResponse *searchrs)
1844 IRSetObj *setobj = p->set_child;
1845 Z_Records *zrs = searchrs->records;
1849 setobj->searchStatus = searchrs->searchStatus ? 1 : 0;
1850 setobj->resultCount = *searchrs->resultCount;
1851 printf ("Search response %d, %d hits\n",
1852 setobj->searchStatus, setobj->resultCount);
1854 ir_handleRecords (o, zrs);
1856 setobj->recordFlag = 0;
1859 printf ("Search response, no object!\n");
1863 static void ir_presentResponse (void *o, Z_PresentResponse *presrs)
1866 IRSetObj *setobj = p->set_child;
1867 Z_Records *zrs = presrs->records;
1869 printf ("Received presentResponse\n");
1871 ir_handleRecords (o, zrs);
1874 setobj->recordFlag = 0;
1875 printf ("No records!\n");
1879 static void ir_scanResponse (void *o, Z_ScanResponse *scanrs)
1882 IRScanObj *scanobj = p->scan_child;
1884 printf ("Received scanResponse\n");
1886 scanobj->scanStatus = *scanrs->scanStatus;
1887 printf ("scanStatus=%d\n", scanobj->scanStatus);
1889 if (scanrs->stepSize)
1890 scanobj->stepSize = *scanrs->stepSize;
1891 printf ("stepSize=%d\n", scanobj->stepSize);
1893 scanobj->numberOfEntriesReturned = *scanrs->numberOfEntriesReturned;
1894 printf ("numberOfEntriesReturned=%d\n", scanobj->numberOfEntriesReturned);
1896 if (scanrs->positionOfTerm)
1897 scanobj->positionOfTerm = *scanrs->positionOfTerm;
1899 scanobj->positionOfTerm = -1;
1900 printf ("positionOfTerm=%d\n", scanobj->positionOfTerm);
1902 free (scanobj->entries);
1903 scanobj->entries = NULL;
1904 free (scanobj->nonSurrogateDiagnostics);
1905 scanobj->nonSurrogateDiagnostics = NULL;
1907 if (scanrs->entries)
1912 scanobj->entries_flag = 1;
1913 scanobj->which = scanrs->entries->which;
1914 switch (scanobj->which)
1916 case Z_ListEntries_entries:
1917 scanobj->num_entries = scanrs->entries->u.entries->num_entries;
1918 scanobj->entries = malloc (scanobj->num_entries *
1919 sizeof(*scanobj->entries));
1920 for (i=0; i<scanobj->num_entries; i++)
1922 ze = scanrs->entries->u.entries->entries[i];
1923 scanobj->entries[i].which = ze->which;
1926 case Z_Entry_termInfo:
1927 if (ze->u.termInfo->term->which == Z_Term_general)
1929 int l = ze->u.termInfo->term->u.general->len;
1930 scanobj->entries[i].u.term.buf = malloc (1+l);
1931 memcpy (scanobj->entries[i].u.term.buf,
1932 ze->u.termInfo->term->u.general->buf,
1934 scanobj->entries[i].u.term.buf[l] = '\0';
1937 scanobj->entries[i].u.term.buf = NULL;
1938 if (ze->u.termInfo->globalOccurrences)
1939 scanobj->entries[i].u.term.globalOccurrences =
1940 *ze->u.termInfo->globalOccurrences;
1942 scanobj->entries[i].u.term.globalOccurrences = 0;
1944 case Z_Entry_surrogateDiagnostic:
1945 scanobj->entries[i].u.diag.addinfo =
1946 malloc (1+strlen(ze->u.surrogateDiagnostic->
1948 strcpy (scanobj->entries[i].u.diag.addinfo,
1949 ze->u.surrogateDiagnostic->addinfo);
1950 scanobj->entries[i].u.diag.condition =
1951 *ze->u.surrogateDiagnostic->condition;
1956 case Z_ListEntries_nonSurrogateDiagnostics:
1957 scanobj->num_diagRecs = scanrs->entries->
1958 u.nonSurrogateDiagnostics->num_diagRecs;
1959 scanobj->nonSurrogateDiagnostics = malloc (scanobj->num_diagRecs *
1960 sizeof(*scanobj->nonSurrogateDiagnostics));
1965 scanobj->entries_flag = 0;
1969 * ir_select_read: handle incoming packages
1971 void ir_select_read (ClientData clientData)
1973 IRObj *p = clientData;
1979 r = cs_rcvconnect (p->cs_link);
1983 ir_select_remove_write (cs_fileno (p->cs_link), p);
1986 printf ("cs_rcvconnect error\n");
1988 Tcl_Eval (p->interp, p->failback);
1989 do_disconnect (p, NULL, 0, NULL);
1993 Tcl_Eval (p->interp, p->callback);
1998 if ((r=cs_get (p->cs_link, &p->buf_in, &p->len_in)) <= 0)
2000 printf ("cs_get failed\n");
2001 ir_select_remove (cs_fileno (p->cs_link), p);
2003 Tcl_Eval (p->interp, p->failback);
2004 do_disconnect (p, NULL, 0, NULL);
2009 odr_setbuf (p->odr_in, p->buf_in, r);
2010 printf ("cs_get ok, got %d\n", r);
2011 if (!z_APDU (p->odr_in, &apdu, 0))
2013 printf ("%s\n", odr_errlist [odr_geterror (p->odr_in)]);
2015 Tcl_Eval (p->interp, p->failback);
2016 do_disconnect (p, NULL, 0, NULL);
2021 case Z_APDU_initResponse:
2022 ir_initResponse (p, apdu->u.initResponse);
2024 case Z_APDU_searchResponse:
2025 ir_searchResponse (p, apdu->u.searchResponse);
2027 case Z_APDU_presentResponse:
2028 ir_presentResponse (p, apdu->u.presentResponse);
2030 case Z_APDU_scanResponse:
2031 ir_scanResponse (p, apdu->u.scanResponse);
2034 printf("Received unknown APDU type (%d).\n",
2037 Tcl_Eval (p->interp, p->failback);
2038 do_disconnect (p, NULL, 0, NULL);
2041 Tcl_Eval (p->interp, p->callback);
2042 } while (cs_more (p->cs_link));
2046 * ir_select_write: handle outgoing packages - not yet written.
2048 void ir_select_write (ClientData clientData)
2050 IRObj *p = clientData;
2053 printf ("In write handler.....\n");
2056 r = cs_rcvconnect (p->cs_link);
2062 printf ("cs_rcvconnect error\n");
2063 ir_select_remove_write (cs_fileno (p->cs_link), p);
2065 Tcl_Eval (p->interp, p->failback);
2066 do_disconnect (p, NULL, 0, NULL);
2069 ir_select_remove_write (cs_fileno (p->cs_link), p);
2071 Tcl_Eval (p->interp, p->callback);
2074 if ((r=cs_put (p->cs_link, p->sbuf, p->slen)) < 0)
2076 printf ("select write fail\n");
2078 Tcl_Eval (p->interp, p->failback);
2079 do_disconnect (p, NULL, 0, NULL);
2081 else if (r == 0) /* remove select bit */
2083 ir_select_remove_write (cs_fileno (p->cs_link), p);
2087 /* ------------------------------------------------------- */
2090 * ir_tcl_init: Registration of TCL commands.
2092 int ir_tcl_init (Tcl_Interp *interp)
2094 Tcl_CreateCommand (interp, "ir", ir_obj_mk, (ClientData) NULL,
2095 (Tcl_CmdDeleteProc *) NULL);
2096 Tcl_CreateCommand (interp, "ir-set", ir_set_obj_mk,
2097 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
2098 Tcl_CreateCommand (interp, "ir-scan", ir_scan_obj_mk,
2099 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);