2 * Copyright (c) 1995, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.18 1995-08-28 12:21:27 quinn
8 * Client can now ask for simple element set names.
10 * Revision 1.17 1995/08/17 12:45:02 quinn
11 * Fixed minor problems with GRS-1. Added support in c&s.
13 * Revision 1.16 1995/08/15 12:00:04 quinn
16 * Revision 1.15 1995/06/22 09:28:03 quinn
17 * Fixed bug in SUTRS processing.
19 * Revision 1.14 1995/06/19 12:37:41 quinn
22 * Revision 1.13 1995/06/16 10:29:11 quinn
23 * *** empty log message ***
25 * Revision 1.12 1995/06/15 07:44:57 quinn
28 * Revision 1.11 1995/06/14 15:26:40 quinn
29 * *** empty log message ***
31 * Revision 1.10 1995/06/06 14:56:58 quinn
34 * Revision 1.9 1995/06/06 08:15:19 quinn
37 * Revision 1.8 1995/06/05 10:52:22 quinn
40 * Revision 1.7 1995/06/02 09:50:09 quinn
43 * Revision 1.6 1995/05/31 08:29:21 quinn
44 * Nothing significant.
46 * Revision 1.5 1995/05/29 08:10:47 quinn
47 * Moved oid.c to util.
49 * Revision 1.4 1995/05/22 15:30:13 adam
50 * Client uses prefix query notation.
52 * Revision 1.3 1995/05/22 15:06:53 quinn
53 * *** empty log message ***
55 * Revision 1.2 1995/05/22 14:56:40 quinn
56 * *** empty log message ***
58 * Revision 1.1 1995/05/22 11:30:31 quinn
59 * Added prettier client.
65 * This is the obligatory little toy client, whose primary purpose is
66 * to illustrate the use of the YAZ service-level API.
74 #include <sys/select.h>
95 #define C_PROMPT "Z> "
97 static ODR out, in, print; /* encoding and decoding streams */
98 static COMSTACK conn = 0; /* our z-association */
99 static Z_IdAuthentication *auth = 0; /* our current auth definition */
100 static char database[512] = "Default"; /* Database name */
101 static int setnumber = 0; /* current result set number */
102 static int smallSetUpperBound = 0;
103 static int largeSetLowerBound = 1;
104 static int mediumSetPresentNumber = 0;
105 static Z_ElementSetNames *elementSetNames = 0;
106 static int setno = 1; /* current set offset */
107 static int protocol = PROTO_Z3950; /* current app protocol */
108 static int recordsyntax = VAL_USMARC;
109 static ODR_MEM session_mem; /* memory handle for init-response */
110 static Z_InitResponse *session = 0; /* session parameters */
111 static char last_scan[512] = "0";
112 static char last_cmd[100] = "?";
115 static CCL_bibset bibset; /* CCL bibset handle */
119 static void send_apdu(Z_APDU *a)
124 if (!z_APDU(out, &a, 0))
126 odr_perror(out, "Encoding APDU");
129 buf = odr_getbuf(out, &len, 0);
130 odr_reset(out); /* release the APDU */
131 if (cs_put(conn, buf, len) < 0)
133 fprintf(stderr, "cs_put: %s", cs_errlist[cs_errno(conn)]);
138 /* INIT SERVICE ------------------------------- */
140 static void send_initRequest()
142 Z_APDU *apdu = zget_APDU(out, Z_APDU_initRequest);
143 Z_InitRequest *req = apdu->u.initRequest;
145 ODR_MASK_SET(req->options, Z_Options_search);
146 ODR_MASK_SET(req->options, Z_Options_present);
147 ODR_MASK_SET(req->options, Z_Options_namedResultSets);
148 ODR_MASK_SET(req->options, Z_Options_triggerResourceCtrl);
149 ODR_MASK_SET(req->options, Z_Options_scan);
151 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_1);
152 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_2);
154 req->idAuthentication = auth;
157 printf("Sent initrequest.\n");
160 static int process_initResponse(Z_InitResponse *res)
162 /* save session parameters for later use */
163 session_mem = odr_extract_mem(in);
167 printf("Connection rejected by target.\n");
169 printf("Connection accepted by target.\n");
170 if (res->implementationId)
171 printf("ID : %s\n", res->implementationId);
172 if (res->implementationName)
173 printf("Name : %s\n", res->implementationName);
174 if (res->implementationVersion)
175 printf("Version: %s\n", res->implementationVersion);
176 if (res->userInformationField)
178 printf("UserInformationfield:\n");
179 if (!z_External(print, (Z_External**)&res-> userInformationField,
182 odr_perror(print, "Printing userinfo\n");
185 if (res->userInformationField->which == ODR_EXTERNAL_octet)
187 printf("Guessing visiblestring:\n");
188 printf("'%s'\n", res->userInformationField->u. octet_aligned->buf);
194 int cmd_open(char *arg)
197 char type[100], addr[100];
202 printf("Already connected.\n");
205 if (!*arg || sscanf(arg, "%[^:]:%s", type, addr) < 2)
207 fprintf(stderr, "Usage: open (osi|tcp) ':' [tsel '/']host[':'port]\n");
211 if (!strcmp(type, "osi"))
213 if (!(add = mosi_strtoaddr(addr)))
223 if (!strcmp(type, "tcp"))
225 if (!(add = tcpip_strtoaddr(addr)))
231 protocol = PROTO_Z3950;
235 fprintf(stderr, "Bad type: %s\n", type);
238 if (!(conn = cs_create(t, 1, protocol)))
243 printf("Connecting...");
245 if (cs_connect(conn, add) < 0)
257 int cmd_authentication(char *arg)
259 static Z_IdAuthentication au;
260 static char open[256];
264 printf("Auth field set to null\n");
269 au.which = Z_IdAuthentication_open;
275 /* SEARCH SERVICE ------------------------------ */
277 void display_grs1(Z_GenericRecord *r, int level)
283 for (i = 0; i < r->num_elements; i++)
287 printf("%*s", level * 4, "");
291 printf("%d,", *t->tagType);
294 if (t->tagValue->which == Z_StringOrNumeric_numeric)
295 printf("%d) ", *t->tagValue->u.numeric);
297 printf("%s) ", t->tagValue->u.string);
298 if (t->content->which == Z_ElementData_subtree)
301 display_grs1(t->content->u.subtree, level+1);
303 else if (t->content->which == Z_ElementData_string)
304 printf("%s\n", t->content->u.string);
310 void display_record(Z_DatabaseRecord *p)
312 Z_External *r = (Z_External*) p;
313 oident *ent = oid_getentbyoid(r->direct_reference);
315 if (r->direct_reference)
317 printf("Record type: ");
319 printf("%s\n", ent->desc);
320 else if (!odr_oid(print, &r->direct_reference, 0))
322 odr_perror(print, "print oid");
326 if (r->which == ODR_EXTERNAL_octet && p->u.octet_aligned->len)
327 marc_display ((char*)p->u.octet_aligned->buf, stdout);
328 else if (ent->value == VAL_SUTRS)
330 if (r->which != Z_External_sutrs)
332 printf("Expecting single SUTRS type for SUTRS.\n");
335 printf("%.*s", r->u.sutrs->len, r->u.sutrs->buf);
337 else if (ent->value == VAL_GRS1)
339 if (r->which != Z_External_grs1)
341 printf("Expecting single GRS type for GRS.\n");
344 display_grs1(r->u.grs1, 0);
348 printf("Unknown record representation.\n");
349 if (!z_External(print, &r, 0))
351 odr_perror(print, "Printing external");
357 static void display_diagrec(Z_DiagRec *p)
361 Z_DefaultDiagFormat *r;
366 printf("Diagnostic message from database:\n");
368 if (p->which != Z_DiagRec_defaultFormat)
370 printf("Diagnostic record not in default format.\n");
374 r = p->u.defaultFormat;
376 if (!(ent = oid_getentbyoid(r->diagnosticSetId)) ||
377 ent->class != CLASS_DIAGSET || ent->value != VAL_BIB1)
378 printf("Missing or unknown diagset\n");
379 printf(" [%d] %s", *r->condition, diagbib1_str(*r->condition));
380 if (r->addinfo && *r->addinfo)
381 printf(" -- %s\n", r->addinfo);
386 static void display_nameplusrecord(Z_NamePlusRecord *p)
389 printf("[%s]", p->databaseName);
390 if (p->which == Z_NamePlusRecord_surrogateDiagnostic)
391 display_diagrec(p->u.surrogateDiagnostic);
393 display_record(p->u.databaseRecord);
396 static void display_records(Z_Records *p)
400 if (p->which == Z_Records_NSD)
401 display_diagrec(p->u.nonSurrogateDiagnostic);
404 printf("Records: %d\n", p->u.databaseOrSurDiagnostics->num_records);
405 for (i = 0; i < p->u.databaseOrSurDiagnostics->num_records; i++)
406 display_nameplusrecord(p->u.databaseOrSurDiagnostics->records[i]);
410 static int send_searchRequest(char *arg)
412 Z_APDU *apdu = zget_APDU(out, Z_APDU_searchRequest);
413 Z_SearchRequest *req = apdu->u.searchRequest;
414 char *databaseNames = database;
418 struct ccl_rpn_node *rpn;
424 Z_RPNQuery *RPNquery;
432 rpn = ccl_find_str(bibset, arg, &error, &pos);
435 printf("CCL ERROR: %s\n", ccl_err_msg(error));
441 if (!strcmp(arg, "@big")) /* strictly for troublemaking */
443 static unsigned char big[2100];
446 /* send a very big referenceid to test transport stack etc. */
447 memset(big, 'A', 2100);
448 bigo.len = bigo.size = 2100;
450 req->referenceId = &bigo;
455 sprintf(setstring, "%d", ++setnumber);
456 req->resultSetName = setstring;
458 *req->smallSetUpperBound = smallSetUpperBound;
459 *req->largeSetLowerBound = largeSetLowerBound;
460 *req->mediumSetPresentNumber = mediumSetPresentNumber;
461 if (smallSetUpperBound > 0 || (largeSetLowerBound > 1 &&
462 mediumSetPresentNumber > 0))
466 prefsyn.proto = protocol;
467 prefsyn.class = CLASS_RECSYN;
468 prefsyn.value = recordsyntax;
469 req->preferredRecordSyntax = odr_oiddup(out, oid_getoidbyent(&prefsyn));
470 req->smallSetElementSetNames =
471 req->mediumSetElementSetNames = elementSetNames;
473 req->num_databaseNames = 1;
474 req->databaseNames = &databaseNames;
479 query.which = Z_Query_type_1;
482 assert((RPNquery = ccl_rpn_query(rpn)));
484 RPNquery = p_query_rpn (out, arg);
487 printf("Prefix query error\n");
491 bib1.proto = protocol;
492 bib1.class = CLASS_ATTSET;
493 bib1.value = VAL_BIB1;
494 RPNquery->attributeSetId = oid_getoidbyent(&bib1);
495 query.u.type_1 = RPNquery;
497 query.which = Z_Query_type_2;
498 query.u.type_2 = &ccl_query;
499 ccl_query.buf = (unsigned char*) arg;
500 ccl_query.len = strlen(arg);
505 printf("Sent searchRequest.\n");
509 static int process_searchResponse(Z_SearchResponse *res)
511 if (*res->searchStatus)
512 printf("Search was a success.\n");
514 printf("Search was a bloomin' failure.\n");
515 printf("Number of hits: %d, setno %d\n",
516 *res->resultCount, setnumber);
517 printf("records returned: %d\n",
518 *res->numberOfRecordsReturned);
519 setno += *res->numberOfRecordsReturned;
521 display_records(res->records);
525 static int cmd_find(char *arg)
529 printf("Find what?\n");
534 printf("Not connected yet\n");
537 if (!send_searchRequest(arg))
542 static int cmd_ssub(char *arg)
544 if (!(smallSetUpperBound = atoi(arg)))
549 static int cmd_lslb(char *arg)
551 if (!(largeSetLowerBound = atoi(arg)))
556 static int cmd_mspn(char *arg)
558 if (!(mediumSetPresentNumber = atoi(arg)))
563 static int cmd_status(char *arg)
565 printf("smallSetUpperBound: %d\n", smallSetUpperBound);
566 printf("largeSetLowerBound: %d\n", largeSetLowerBound);
567 printf("mediumSetPresentNumber: %d\n", mediumSetPresentNumber);
571 static int cmd_base(char *arg)
575 printf("Usage: base <database>\n");
578 strcpy(database, arg);
582 static int cmd_setnames(char *arg)
586 printf("Set numbering enabled.\n");
591 printf("Set numbering disabled.\n");
597 /* PRESENT SERVICE ----------------------------- */
599 static int send_presentRequest(char *arg)
601 Z_APDU *apdu = zget_APDU(out, Z_APDU_presentRequest);
602 Z_PresentRequest *req = apdu->u.presentRequest;
603 Z_RecordComposition compo;
609 if ((p = strchr(arg, '+')))
619 sprintf(setstring, "%d", setnumber);
620 req->resultSetId = setstring;
622 req->resultSetStartPoint = &setno;
623 req->numberOfRecordsRequested = &nos;
624 prefsyn.proto = protocol;
625 prefsyn.class = CLASS_RECSYN;
626 prefsyn.value = recordsyntax;
627 req->preferredRecordSyntax = oid_getoidbyent(&prefsyn);
630 req->recordComposition = &compo;
631 compo.which = Z_RecordComp_simple;
632 compo.u.simple = elementSetNames;
635 printf("Sent presentRequest (%d+%d).\n", setno, nos);
639 static int cmd_show(char *arg)
641 if (!send_presentRequest(arg))
646 int cmd_quit(char *arg)
648 printf("See you later, alligator.\n");
652 int cmd_cancel(char *arg)
654 Z_APDU *apdu = zget_APDU(out, Z_APDU_triggerResourceControlRequest);
655 Z_TriggerResourceControlRequest *req =
656 apdu->u.triggerResourceControlRequest;
661 printf("Session not initialized yet\n");
664 if (!ODR_MASK_GET(session->options, Z_Options_triggerResourceCtrl))
666 printf("Target doesn't support cancel (trigger resource ctrl)\n");
669 *req->requestedAction = Z_TriggerResourceCtrl_cancel;
670 req->resultSetWanted = &false;
673 printf("Sent cancel request\n");
677 int send_scanrequest(char *string, int pp, int num)
679 Z_APDU *apdu = zget_APDU(out, Z_APDU_scanRequest);
680 Z_ScanRequest *req = apdu->u.scanRequest;
684 req->num_databaseNames = 1;
685 req->databaseNames = &db;
686 attset.proto = protocol;
687 attset.class = CLASS_ATTSET;
688 attset.value = VAL_BIB1;
689 req->attributeSet = oid_getoidbyent(&attset);
690 req->termListAndStartPoint = p_query_scan(out, string);
691 req->numberOfTermsRequested = #
692 req->preferredPositionInResponse = &pp;
697 void display_term(Z_TermInfo *t)
699 if (t->term->which == Z_Term_general)
701 printf("%.*s (%d)\n", t->term->u.general->len, t->term->u.general->buf,
702 t->globalOccurrences ? *t->globalOccurrences : -1);
703 sprintf(last_scan, "%.*s", t->term->u.general->len,
704 t->term->u.general->buf);
707 printf("Term type not general.\n");
710 void process_scanResponse(Z_ScanResponse *res)
714 printf("SCAN: %d entries, position=%d\n", *res->numberOfEntriesReturned,
715 *res->positionOfTerm);
716 if (*res->scanStatus != Z_Scan_success)
717 printf("Scan returned code %d\n", *res->scanStatus);
720 if (res->entries->which == Z_ListEntries_entries)
722 Z_Entries *ent = res->entries->u.entries;
724 for (i = 0; i < ent->num_entries; i++)
725 if (ent->entries[i]->which == Z_Entry_termInfo)
727 printf("%c ", i + 1 == *res->positionOfTerm ? '*' : ' ');
728 display_term(ent->entries[i]->u.termInfo);
731 display_diagrec(ent->entries[i]->u.surrogateDiagnostic);
734 display_diagrec(res->entries->u.nonSurrogateDiagnostics->diagRecs[0]);
737 int cmd_scan(char *arg)
741 printf("Session not initialized yet\n");
744 if (!ODR_MASK_GET(session->options, Z_Options_scan))
746 printf("Target doesn't support scan\n");
751 if (send_scanrequest(arg, 5, 20) < 0)
755 if (send_scanrequest(last_scan, 1, 20) < 0)
760 int cmd_format(char *arg)
764 printf("Usage: format <recordsyntax>\n");
767 if (!strcmp(arg, "sutrs"))
769 printf("Preferred format is SUTRS.\n");
770 recordsyntax = VAL_SUTRS;
773 else if (!strcmp(arg, "usmarc"))
775 printf("Preferred format is USMARC\n");
776 recordsyntax = VAL_USMARC;
779 else if (!strcmp(arg, "danmarc"))
781 printf("Preferred format is DANMARC\n");
782 recordsyntax = VAL_DANMARC;
785 else if (!strcmp(arg, "grs1"))
787 printf("Preferred format is GRS1\n");
788 recordsyntax = VAL_GRS1;
793 printf("Specify one of {sutrs,usmarc,danmarc,grs1}.\n");
798 int cmd_elements(char *arg)
800 static Z_ElementSetNames esn;
801 static char what[100];
805 printf("Usage: elements <esn>\n");
809 esn.which = Z_ElementSetNames_generic;
810 esn.u.generic = what;
811 elementSetNames = &esn;
815 static void initialize(void)
823 if (!(out = odr_createmem(ODR_ENCODE)) ||
824 !(in = odr_createmem(ODR_DECODE)) ||
825 !(print = odr_createmem(ODR_PRINT)))
827 fprintf(stderr, "failed to allocate ODR streams\n");
830 setvbuf(stdout, 0, _IONBF, 0);
834 bibset = ccl_qual_mk ();
835 inf = fopen ("default.bib", "r");
838 ccl_qual_file (bibset, inf);
845 static int client(void)
849 int (*fun)(char *arg);
852 {"open", cmd_open, "('tcp'|'osi')':'[<TSEL>'/']<HOST>[':'<PORT>]"},
853 {"quit", cmd_quit, ""},
854 {"find", cmd_find, "<CCL-QUERY>"},
855 {"base", cmd_base, "<BASE-NAME>"},
856 {"show", cmd_show, "<REC#>['+'<#RECS>]"},
857 {"scan", cmd_scan, "<TERM>"},
858 {"authentication", cmd_authentication, "<ACCTSTRING>"},
859 {"lslb", cmd_lslb, "<largeSetLowerBound>"},
860 {"ssub", cmd_ssub, "<smallSetUpperBound>"},
861 {"mspn", cmd_mspn, "<mediumSetPresentNumber>"},
862 {"status", cmd_status, ""},
863 {"setnames", cmd_setnames, ""},
864 {"cancel", cmd_cancel, ""},
865 {"format", cmd_format, "<recordsyntax>"},
866 {"elements", cmd_elements, "<elementSetName>"},
870 int netbufferlen = 0;
878 char line[1024], word[1024], arg[1024];
883 FD_SET(cs_fileno(conn), &input);
884 if ((res = select(20, &input, 0, 0, 0)) < 0)
891 if (FD_ISSET(0, &input))
893 /* quick & dirty way to get a command line. */
896 if ((res = sscanf(line, "%s %[^;]", word, arg)) <= 0)
898 strcpy(word, last_cmd);
903 strcpy(last_cmd, word);
904 for (i = 0; cmd[i].cmd; i++)
905 if (!strncmp(cmd[i].cmd, word, strlen(word)))
907 res = (*cmd[i].fun)(arg);
910 if (!cmd[i].cmd) /* dump our help-screen */
912 printf("Unknown command: %s.\n", word);
913 printf("Currently recognized commands:\n");
914 for (i = 0; cmd[i].cmd; i++)
915 printf(" %s %s\n", cmd[i].cmd, cmd[i].ad);
921 if (conn && FD_ISSET(cs_fileno(conn), &input))
925 if ((res = cs_get(conn, &netbuffer, &netbufferlen)) < 0)
932 printf("Target closed connection.\n");
935 odr_reset(in); /* release APDU from last round */
936 odr_setbuf(in, netbuffer, res, 0);
937 if (!z_APDU(in, &apdu, 0))
939 odr_perror(in, "Decoding incoming APDU");
940 fprintf(stderr, "Packet dump:\n---------\n");
941 odr_dumpBER(stderr, netbuffer, res);
942 fprintf(stderr, "---------\n");
946 if (!z_APDU(print, &apdu, 0))
948 odr_perror(print, "Failed to print incoming APDU");
955 case Z_APDU_initResponse:
956 process_initResponse(apdu->u.initResponse);
958 case Z_APDU_searchResponse:
959 process_searchResponse(apdu->u.searchResponse);
961 case Z_APDU_scanResponse:
962 process_scanResponse(apdu->u.scanResponse);
964 case Z_APDU_presentResponse:
965 printf("Received presentResponse.\n");
967 *apdu->u.presentResponse->numberOfRecordsReturned;
968 if (apdu->u.presentResponse->records)
969 display_records(apdu->u.presentResponse->records);
971 printf("No records.\n");
974 printf("Received unknown APDU type (%d).\n",
981 while (cs_more(conn));
987 int main(int argc, char **argv)