2 * Copyright (c) 1995, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.19 1995-08-29 11:17:28 quinn
8 * Added code to receive close
10 * Revision 1.18 1995/08/28 12:21:27 quinn
11 * Client can now ask for simple element set names.
13 * Revision 1.17 1995/08/17 12:45:02 quinn
14 * Fixed minor problems with GRS-1. Added support in c&s.
16 * Revision 1.16 1995/08/15 12:00:04 quinn
19 * Revision 1.15 1995/06/22 09:28:03 quinn
20 * Fixed bug in SUTRS processing.
22 * Revision 1.14 1995/06/19 12:37:41 quinn
25 * Revision 1.13 1995/06/16 10:29:11 quinn
26 * *** empty log message ***
28 * Revision 1.12 1995/06/15 07:44:57 quinn
31 * Revision 1.11 1995/06/14 15:26:40 quinn
32 * *** empty log message ***
34 * Revision 1.10 1995/06/06 14:56:58 quinn
37 * Revision 1.9 1995/06/06 08:15:19 quinn
40 * Revision 1.8 1995/06/05 10:52:22 quinn
43 * Revision 1.7 1995/06/02 09:50:09 quinn
46 * Revision 1.6 1995/05/31 08:29:21 quinn
47 * Nothing significant.
49 * Revision 1.5 1995/05/29 08:10:47 quinn
50 * Moved oid.c to util.
52 * Revision 1.4 1995/05/22 15:30:13 adam
53 * Client uses prefix query notation.
55 * Revision 1.3 1995/05/22 15:06:53 quinn
56 * *** empty log message ***
58 * Revision 1.2 1995/05/22 14:56:40 quinn
59 * *** empty log message ***
61 * Revision 1.1 1995/05/22 11:30:31 quinn
62 * Added prettier client.
68 * This is the obligatory little toy client, whose primary purpose is
69 * to illustrate the use of the YAZ service-level API.
77 #include <sys/select.h>
98 #define C_PROMPT "Z> "
100 static ODR out, in, print; /* encoding and decoding streams */
101 static COMSTACK conn = 0; /* our z-association */
102 static Z_IdAuthentication *auth = 0; /* our current auth definition */
103 static char database[512] = "Default"; /* Database name */
104 static int setnumber = 0; /* current result set number */
105 static int smallSetUpperBound = 0;
106 static int largeSetLowerBound = 1;
107 static int mediumSetPresentNumber = 0;
108 static Z_ElementSetNames *elementSetNames = 0;
109 static int setno = 1; /* current set offset */
110 static int protocol = PROTO_Z3950; /* current app protocol */
111 static int recordsyntax = VAL_USMARC;
112 static ODR_MEM session_mem; /* memory handle for init-response */
113 static Z_InitResponse *session = 0; /* session parameters */
114 static char last_scan[512] = "0";
115 static char last_cmd[100] = "?";
118 static CCL_bibset bibset; /* CCL bibset handle */
122 static void send_apdu(Z_APDU *a)
127 if (!z_APDU(out, &a, 0))
129 odr_perror(out, "Encoding APDU");
132 buf = odr_getbuf(out, &len, 0);
133 odr_reset(out); /* release the APDU */
134 if (cs_put(conn, buf, len) < 0)
136 fprintf(stderr, "cs_put: %s", cs_errlist[cs_errno(conn)]);
141 /* INIT SERVICE ------------------------------- */
143 static void send_initRequest()
145 Z_APDU *apdu = zget_APDU(out, Z_APDU_initRequest);
146 Z_InitRequest *req = apdu->u.initRequest;
148 ODR_MASK_SET(req->options, Z_Options_search);
149 ODR_MASK_SET(req->options, Z_Options_present);
150 ODR_MASK_SET(req->options, Z_Options_namedResultSets);
151 ODR_MASK_SET(req->options, Z_Options_triggerResourceCtrl);
152 ODR_MASK_SET(req->options, Z_Options_scan);
154 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_1);
155 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_2);
156 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_3);
158 req->idAuthentication = auth;
161 printf("Sent initrequest.\n");
164 static int process_initResponse(Z_InitResponse *res)
166 /* save session parameters for later use */
167 session_mem = odr_extract_mem(in);
171 printf("Connection rejected by target.\n");
173 printf("Connection accepted by target.\n");
174 if (res->implementationId)
175 printf("ID : %s\n", res->implementationId);
176 if (res->implementationName)
177 printf("Name : %s\n", res->implementationName);
178 if (res->implementationVersion)
179 printf("Version: %s\n", res->implementationVersion);
180 if (res->userInformationField)
182 printf("UserInformationfield:\n");
183 if (!z_External(print, (Z_External**)&res-> userInformationField,
186 odr_perror(print, "Printing userinfo\n");
189 if (res->userInformationField->which == Z_External_octet)
191 printf("Guessing visiblestring:\n");
192 printf("'%s'\n", res->userInformationField->u. octet_aligned->buf);
198 int cmd_open(char *arg)
201 char type[100], addr[100];
206 printf("Already connected.\n");
209 if (!*arg || sscanf(arg, "%[^:]:%s", type, addr) < 2)
211 fprintf(stderr, "Usage: open (osi|tcp) ':' [tsel '/']host[':'port]\n");
215 if (!strcmp(type, "osi"))
217 if (!(add = mosi_strtoaddr(addr)))
227 if (!strcmp(type, "tcp"))
229 if (!(add = tcpip_strtoaddr(addr)))
235 protocol = PROTO_Z3950;
239 fprintf(stderr, "Bad type: %s\n", type);
242 if (!(conn = cs_create(t, 1, protocol)))
247 printf("Connecting...");
249 if (cs_connect(conn, add) < 0)
261 int cmd_authentication(char *arg)
263 static Z_IdAuthentication au;
264 static char open[256];
268 printf("Auth field set to null\n");
273 au.which = Z_IdAuthentication_open;
279 /* SEARCH SERVICE ------------------------------ */
281 void display_grs1(Z_GenericRecord *r, int level)
287 for (i = 0; i < r->num_elements; i++)
291 printf("%*s", level * 4, "");
295 printf("%d,", *t->tagType);
298 if (t->tagValue->which == Z_StringOrNumeric_numeric)
299 printf("%d) ", *t->tagValue->u.numeric);
301 printf("%s) ", t->tagValue->u.string);
302 if (t->content->which == Z_ElementData_subtree)
305 display_grs1(t->content->u.subtree, level+1);
307 else if (t->content->which == Z_ElementData_string)
308 printf("%s\n", t->content->u.string);
314 void display_record(Z_DatabaseRecord *p)
316 Z_External *r = (Z_External*) p;
317 oident *ent = oid_getentbyoid(r->direct_reference);
319 if (r->direct_reference)
321 printf("Record type: ");
323 printf("%s\n", ent->desc);
324 else if (!odr_oid(print, &r->direct_reference, 0))
326 odr_perror(print, "print oid");
330 if (r->which == Z_External_octet && p->u.octet_aligned->len)
331 marc_display ((char*)p->u.octet_aligned->buf, stdout);
332 else if (ent->value == VAL_SUTRS)
334 if (r->which != Z_External_sutrs)
336 printf("Expecting single SUTRS type for SUTRS.\n");
339 printf("%.*s", r->u.sutrs->len, r->u.sutrs->buf);
341 else if (ent->value == VAL_GRS1)
343 if (r->which != Z_External_grs1)
345 printf("Expecting single GRS type for GRS.\n");
348 display_grs1(r->u.grs1, 0);
352 printf("Unknown record representation.\n");
353 if (!z_External(print, &r, 0))
355 odr_perror(print, "Printing external");
361 static void display_diagrec(Z_DiagRec *p)
365 Z_DefaultDiagFormat *r;
370 printf("Diagnostic message from database:\n");
372 if (p->which != Z_DiagRec_defaultFormat)
374 printf("Diagnostic record not in default format.\n");
378 r = p->u.defaultFormat;
380 if (!(ent = oid_getentbyoid(r->diagnosticSetId)) ||
381 ent->class != CLASS_DIAGSET || ent->value != VAL_BIB1)
382 printf("Missing or unknown diagset\n");
383 printf(" [%d] %s", *r->condition, diagbib1_str(*r->condition));
384 if (r->addinfo && *r->addinfo)
385 printf(" -- %s\n", r->addinfo);
390 static void display_nameplusrecord(Z_NamePlusRecord *p)
393 printf("[%s]", p->databaseName);
394 if (p->which == Z_NamePlusRecord_surrogateDiagnostic)
395 display_diagrec(p->u.surrogateDiagnostic);
397 display_record(p->u.databaseRecord);
400 static void display_records(Z_Records *p)
404 if (p->which == Z_Records_NSD)
405 display_diagrec(p->u.nonSurrogateDiagnostic);
408 printf("Records: %d\n", p->u.databaseOrSurDiagnostics->num_records);
409 for (i = 0; i < p->u.databaseOrSurDiagnostics->num_records; i++)
410 display_nameplusrecord(p->u.databaseOrSurDiagnostics->records[i]);
414 static int send_searchRequest(char *arg)
416 Z_APDU *apdu = zget_APDU(out, Z_APDU_searchRequest);
417 Z_SearchRequest *req = apdu->u.searchRequest;
418 char *databaseNames = database;
422 struct ccl_rpn_node *rpn;
428 Z_RPNQuery *RPNquery;
436 rpn = ccl_find_str(bibset, arg, &error, &pos);
439 printf("CCL ERROR: %s\n", ccl_err_msg(error));
445 if (!strcmp(arg, "@big")) /* strictly for troublemaking */
447 static unsigned char big[2100];
450 /* send a very big referenceid to test transport stack etc. */
451 memset(big, 'A', 2100);
452 bigo.len = bigo.size = 2100;
454 req->referenceId = &bigo;
459 sprintf(setstring, "%d", ++setnumber);
460 req->resultSetName = setstring;
462 *req->smallSetUpperBound = smallSetUpperBound;
463 *req->largeSetLowerBound = largeSetLowerBound;
464 *req->mediumSetPresentNumber = mediumSetPresentNumber;
465 if (smallSetUpperBound > 0 || (largeSetLowerBound > 1 &&
466 mediumSetPresentNumber > 0))
470 prefsyn.proto = protocol;
471 prefsyn.class = CLASS_RECSYN;
472 prefsyn.value = recordsyntax;
473 req->preferredRecordSyntax = odr_oiddup(out, oid_getoidbyent(&prefsyn));
474 req->smallSetElementSetNames =
475 req->mediumSetElementSetNames = elementSetNames;
477 req->num_databaseNames = 1;
478 req->databaseNames = &databaseNames;
483 query.which = Z_Query_type_1;
486 assert((RPNquery = ccl_rpn_query(rpn)));
488 RPNquery = p_query_rpn (out, arg);
491 printf("Prefix query error\n");
495 bib1.proto = protocol;
496 bib1.class = CLASS_ATTSET;
497 bib1.value = VAL_BIB1;
498 RPNquery->attributeSetId = oid_getoidbyent(&bib1);
499 query.u.type_1 = RPNquery;
501 query.which = Z_Query_type_2;
502 query.u.type_2 = &ccl_query;
503 ccl_query.buf = (unsigned char*) arg;
504 ccl_query.len = strlen(arg);
509 printf("Sent searchRequest.\n");
513 static int process_searchResponse(Z_SearchResponse *res)
515 if (*res->searchStatus)
516 printf("Search was a success.\n");
518 printf("Search was a bloomin' failure.\n");
519 printf("Number of hits: %d, setno %d\n",
520 *res->resultCount, setnumber);
521 printf("records returned: %d\n",
522 *res->numberOfRecordsReturned);
523 setno += *res->numberOfRecordsReturned;
525 display_records(res->records);
529 static int cmd_find(char *arg)
533 printf("Find what?\n");
538 printf("Not connected yet\n");
541 if (!send_searchRequest(arg))
546 static int cmd_ssub(char *arg)
548 if (!(smallSetUpperBound = atoi(arg)))
553 static int cmd_lslb(char *arg)
555 if (!(largeSetLowerBound = atoi(arg)))
560 static int cmd_mspn(char *arg)
562 if (!(mediumSetPresentNumber = atoi(arg)))
567 static int cmd_status(char *arg)
569 printf("smallSetUpperBound: %d\n", smallSetUpperBound);
570 printf("largeSetLowerBound: %d\n", largeSetLowerBound);
571 printf("mediumSetPresentNumber: %d\n", mediumSetPresentNumber);
575 static int cmd_base(char *arg)
579 printf("Usage: base <database>\n");
582 strcpy(database, arg);
586 static int cmd_setnames(char *arg)
590 printf("Set numbering enabled.\n");
595 printf("Set numbering disabled.\n");
601 /* PRESENT SERVICE ----------------------------- */
603 static int send_presentRequest(char *arg)
605 Z_APDU *apdu = zget_APDU(out, Z_APDU_presentRequest);
606 Z_PresentRequest *req = apdu->u.presentRequest;
607 Z_RecordComposition compo;
613 if ((p = strchr(arg, '+')))
623 sprintf(setstring, "%d", setnumber);
624 req->resultSetId = setstring;
626 req->resultSetStartPoint = &setno;
627 req->numberOfRecordsRequested = &nos;
628 prefsyn.proto = protocol;
629 prefsyn.class = CLASS_RECSYN;
630 prefsyn.value = recordsyntax;
631 req->preferredRecordSyntax = oid_getoidbyent(&prefsyn);
634 req->recordComposition = &compo;
635 compo.which = Z_RecordComp_simple;
636 compo.u.simple = elementSetNames;
639 printf("Sent presentRequest (%d+%d).\n", setno, nos);
643 void process_close(Z_Close *req)
645 static char *reasons[] =
659 printf("Reason: %s, message: %s\n", reasons[*req->closeReason],
660 req->diagnosticInformation ? req->diagnosticInformation : "NULL");
663 static int cmd_show(char *arg)
665 if (!send_presentRequest(arg))
670 int cmd_quit(char *arg)
672 printf("See you later, alligator.\n");
676 int cmd_cancel(char *arg)
678 Z_APDU *apdu = zget_APDU(out, Z_APDU_triggerResourceControlRequest);
679 Z_TriggerResourceControlRequest *req =
680 apdu->u.triggerResourceControlRequest;
685 printf("Session not initialized yet\n");
688 if (!ODR_MASK_GET(session->options, Z_Options_triggerResourceCtrl))
690 printf("Target doesn't support cancel (trigger resource ctrl)\n");
693 *req->requestedAction = Z_TriggerResourceCtrl_cancel;
694 req->resultSetWanted = &false;
697 printf("Sent cancel request\n");
701 int send_scanrequest(char *string, int pp, int num)
703 Z_APDU *apdu = zget_APDU(out, Z_APDU_scanRequest);
704 Z_ScanRequest *req = apdu->u.scanRequest;
708 req->num_databaseNames = 1;
709 req->databaseNames = &db;
710 attset.proto = protocol;
711 attset.class = CLASS_ATTSET;
712 attset.value = VAL_BIB1;
713 req->attributeSet = oid_getoidbyent(&attset);
714 req->termListAndStartPoint = p_query_scan(out, string);
715 req->numberOfTermsRequested = #
716 req->preferredPositionInResponse = &pp;
721 void display_term(Z_TermInfo *t)
723 if (t->term->which == Z_Term_general)
725 printf("%.*s (%d)\n", t->term->u.general->len, t->term->u.general->buf,
726 t->globalOccurrences ? *t->globalOccurrences : -1);
727 sprintf(last_scan, "%.*s", t->term->u.general->len,
728 t->term->u.general->buf);
731 printf("Term type not general.\n");
734 void process_scanResponse(Z_ScanResponse *res)
738 printf("SCAN: %d entries, position=%d\n", *res->numberOfEntriesReturned,
739 *res->positionOfTerm);
740 if (*res->scanStatus != Z_Scan_success)
741 printf("Scan returned code %d\n", *res->scanStatus);
744 if (res->entries->which == Z_ListEntries_entries)
746 Z_Entries *ent = res->entries->u.entries;
748 for (i = 0; i < ent->num_entries; i++)
749 if (ent->entries[i]->which == Z_Entry_termInfo)
751 printf("%c ", i + 1 == *res->positionOfTerm ? '*' : ' ');
752 display_term(ent->entries[i]->u.termInfo);
755 display_diagrec(ent->entries[i]->u.surrogateDiagnostic);
758 display_diagrec(res->entries->u.nonSurrogateDiagnostics->diagRecs[0]);
761 int cmd_scan(char *arg)
765 printf("Session not initialized yet\n");
768 if (!ODR_MASK_GET(session->options, Z_Options_scan))
770 printf("Target doesn't support scan\n");
775 if (send_scanrequest(arg, 5, 20) < 0)
779 if (send_scanrequest(last_scan, 1, 20) < 0)
784 int cmd_format(char *arg)
788 printf("Usage: format <recordsyntax>\n");
791 if (!strcmp(arg, "sutrs"))
793 printf("Preferred format is SUTRS.\n");
794 recordsyntax = VAL_SUTRS;
797 else if (!strcmp(arg, "usmarc"))
799 printf("Preferred format is USMARC\n");
800 recordsyntax = VAL_USMARC;
803 else if (!strcmp(arg, "danmarc"))
805 printf("Preferred format is DANMARC\n");
806 recordsyntax = VAL_DANMARC;
809 else if (!strcmp(arg, "grs1"))
811 printf("Preferred format is GRS1\n");
812 recordsyntax = VAL_GRS1;
817 printf("Specify one of {sutrs,usmarc,danmarc,grs1}.\n");
822 int cmd_elements(char *arg)
824 static Z_ElementSetNames esn;
825 static char what[100];
829 printf("Usage: elements <esn>\n");
833 esn.which = Z_ElementSetNames_generic;
834 esn.u.generic = what;
835 elementSetNames = &esn;
839 static void initialize(void)
847 if (!(out = odr_createmem(ODR_ENCODE)) ||
848 !(in = odr_createmem(ODR_DECODE)) ||
849 !(print = odr_createmem(ODR_PRINT)))
851 fprintf(stderr, "failed to allocate ODR streams\n");
854 setvbuf(stdout, 0, _IONBF, 0);
858 bibset = ccl_qual_mk ();
859 inf = fopen ("default.bib", "r");
862 ccl_qual_file (bibset, inf);
869 static int client(void)
873 int (*fun)(char *arg);
876 {"open", cmd_open, "('tcp'|'osi')':'[<TSEL>'/']<HOST>[':'<PORT>]"},
877 {"quit", cmd_quit, ""},
878 {"find", cmd_find, "<CCL-QUERY>"},
879 {"base", cmd_base, "<BASE-NAME>"},
880 {"show", cmd_show, "<REC#>['+'<#RECS>]"},
881 {"scan", cmd_scan, "<TERM>"},
882 {"authentication", cmd_authentication, "<ACCTSTRING>"},
883 {"lslb", cmd_lslb, "<largeSetLowerBound>"},
884 {"ssub", cmd_ssub, "<smallSetUpperBound>"},
885 {"mspn", cmd_mspn, "<mediumSetPresentNumber>"},
886 {"status", cmd_status, ""},
887 {"setnames", cmd_setnames, ""},
888 {"cancel", cmd_cancel, ""},
889 {"format", cmd_format, "<recordsyntax>"},
890 {"elements", cmd_elements, "<elementSetName>"},
894 int netbufferlen = 0;
902 char line[1024], word[1024], arg[1024];
907 FD_SET(cs_fileno(conn), &input);
908 if ((res = select(20, &input, 0, 0, 0)) < 0)
915 if (FD_ISSET(0, &input))
917 /* quick & dirty way to get a command line. */
920 if ((res = sscanf(line, "%s %[^;]", word, arg)) <= 0)
922 strcpy(word, last_cmd);
927 strcpy(last_cmd, word);
928 for (i = 0; cmd[i].cmd; i++)
929 if (!strncmp(cmd[i].cmd, word, strlen(word)))
931 res = (*cmd[i].fun)(arg);
934 if (!cmd[i].cmd) /* dump our help-screen */
936 printf("Unknown command: %s.\n", word);
937 printf("Currently recognized commands:\n");
938 for (i = 0; cmd[i].cmd; i++)
939 printf(" %s %s\n", cmd[i].cmd, cmd[i].ad);
945 if (conn && FD_ISSET(cs_fileno(conn), &input))
949 if ((res = cs_get(conn, &netbuffer, &netbufferlen)) < 0)
956 printf("Target closed connection.\n");
959 odr_reset(in); /* release APDU from last round */
960 odr_setbuf(in, netbuffer, res, 0);
961 if (!z_APDU(in, &apdu, 0))
963 odr_perror(in, "Decoding incoming APDU");
964 fprintf(stderr, "Packet dump:\n---------\n");
965 odr_dumpBER(stderr, netbuffer, res);
966 fprintf(stderr, "---------\n");
970 if (!z_APDU(print, &apdu, 0))
972 odr_perror(print, "Failed to print incoming APDU");
979 case Z_APDU_initResponse:
980 process_initResponse(apdu->u.initResponse);
982 case Z_APDU_searchResponse:
983 process_searchResponse(apdu->u.searchResponse);
985 case Z_APDU_scanResponse:
986 process_scanResponse(apdu->u.scanResponse);
988 case Z_APDU_presentResponse:
989 printf("Received presentResponse.\n");
991 *apdu->u.presentResponse->numberOfRecordsReturned;
992 if (apdu->u.presentResponse->records)
993 display_records(apdu->u.presentResponse->records);
995 printf("No records.\n");
998 printf("Target has closed the association.\n");
999 process_close(apdu->u.close);
1002 printf("Received unknown APDU type (%d).\n",
1009 while (cs_more(conn));
1015 int main(int argc, char **argv)