2 * Copyright (c) 1995, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.23 1995-10-18 16:12:30 quinn
10 * Revision 1.22 1995/10/11 14:49:12 quinn
13 * Revision 1.21 1995/09/29 17:01:47 quinn
16 * Revision 1.20 1995/08/29 14:24:13 quinn
17 * Added second half of close-handshake
19 * Revision 1.19 1995/08/29 11:17:28 quinn
20 * Added code to receive close
22 * Revision 1.18 1995/08/28 12:21:27 quinn
23 * Client can now ask for simple element set names.
25 * Revision 1.17 1995/08/17 12:45:02 quinn
26 * Fixed minor problems with GRS-1. Added support in c&s.
28 * Revision 1.16 1995/08/15 12:00:04 quinn
31 * Revision 1.15 1995/06/22 09:28:03 quinn
32 * Fixed bug in SUTRS processing.
34 * Revision 1.14 1995/06/19 12:37:41 quinn
37 * Revision 1.13 1995/06/16 10:29:11 quinn
38 * *** empty log message ***
40 * Revision 1.12 1995/06/15 07:44:57 quinn
43 * Revision 1.11 1995/06/14 15:26:40 quinn
44 * *** empty log message ***
46 * Revision 1.10 1995/06/06 14:56:58 quinn
49 * Revision 1.9 1995/06/06 08:15:19 quinn
52 * Revision 1.8 1995/06/05 10:52:22 quinn
55 * Revision 1.7 1995/06/02 09:50:09 quinn
58 * Revision 1.6 1995/05/31 08:29:21 quinn
59 * Nothing significant.
61 * Revision 1.5 1995/05/29 08:10:47 quinn
62 * Moved oid.c to util.
64 * Revision 1.4 1995/05/22 15:30:13 adam
65 * Client uses prefix query notation.
67 * Revision 1.3 1995/05/22 15:06:53 quinn
68 * *** empty log message ***
70 * Revision 1.2 1995/05/22 14:56:40 quinn
71 * *** empty log message ***
73 * Revision 1.1 1995/05/22 11:30:31 quinn
74 * Added prettier client.
80 * This is the obligatory little toy client, whose primary purpose is
81 * to illustrate the use of the YAZ service-level API.
93 #include <sys/select.h>
103 #include <marcdisp.h>
104 #include <diagbib1.h>
114 #define C_PROMPT "Z> "
116 static ODR out, in, print; /* encoding and decoding streams */
117 static COMSTACK conn = 0; /* our z-association */
118 static Z_IdAuthentication *auth = 0; /* our current auth definition */
119 static char database[512] = "Default"; /* Database name */
120 static int setnumber = 0; /* current result set number */
121 static int smallSetUpperBound = 0;
122 static int largeSetLowerBound = 1;
123 static int mediumSetPresentNumber = 0;
124 static Z_ElementSetNames *elementSetNames = 0;
125 static int setno = 1; /* current set offset */
126 static int protocol = PROTO_Z3950; /* current app protocol */
127 static int recordsyntax = VAL_USMARC;
128 static int sent_close = 0;
129 static ODR_MEM session_mem; /* memory handle for init-response */
130 static Z_InitResponse *session = 0; /* session parameters */
131 static char last_scan[512] = "0";
132 static char last_cmd[100] = "?";
135 static CCL_bibset bibset; /* CCL bibset handle */
139 static void send_apdu(Z_APDU *a)
144 if (!z_APDU(out, &a, 0))
146 odr_perror(out, "Encoding APDU");
149 buf = odr_getbuf(out, &len, 0);
150 odr_reset(out); /* release the APDU */
151 if (cs_put(conn, buf, len) < 0)
153 fprintf(stderr, "cs_put: %s", cs_errmsg(cs_errno(conn)));
158 /* INIT SERVICE ------------------------------- */
160 static void send_initRequest()
162 Z_APDU *apdu = zget_APDU(out, Z_APDU_initRequest);
163 Z_InitRequest *req = apdu->u.initRequest;
165 ODR_MASK_SET(req->options, Z_Options_search);
166 ODR_MASK_SET(req->options, Z_Options_present);
167 ODR_MASK_SET(req->options, Z_Options_namedResultSets);
168 ODR_MASK_SET(req->options, Z_Options_triggerResourceCtrl);
169 ODR_MASK_SET(req->options, Z_Options_scan);
171 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_1);
172 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_2);
173 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_3);
175 req->idAuthentication = auth;
178 printf("Sent initrequest.\n");
181 static int process_initResponse(Z_InitResponse *res)
183 /* save session parameters for later use */
184 session_mem = odr_extract_mem(in);
188 printf("Connection rejected by target.\n");
190 printf("Connection accepted by target.\n");
191 if (res->implementationId)
192 printf("ID : %s\n", res->implementationId);
193 if (res->implementationName)
194 printf("Name : %s\n", res->implementationName);
195 if (res->implementationVersion)
196 printf("Version: %s\n", res->implementationVersion);
197 if (res->userInformationField)
199 printf("UserInformationfield:\n");
200 if (!z_External(print, (Z_External**)&res-> userInformationField,
203 odr_perror(print, "Printing userinfo\n");
206 if (res->userInformationField->which == Z_External_octet)
208 printf("Guessing visiblestring:\n");
209 printf("'%s'\n", res->userInformationField->u. octet_aligned->buf);
215 int cmd_open(char *arg)
218 char type[100], addr[100];
223 printf("Already connected.\n");
226 if (!*arg || sscanf(arg, "%[^:]:%s", type, addr) < 2)
228 fprintf(stderr, "Usage: open (osi|tcp) ':' [tsel '/']host[':'port]\n");
232 if (!strcmp(type, "osi"))
234 if (!(add = mosi_strtoaddr(addr)))
244 if (!strcmp(type, "tcp"))
246 if (!(add = tcpip_strtoaddr(addr)))
252 protocol = PROTO_Z3950;
256 fprintf(stderr, "Bad type: %s\n", type);
259 if (!(conn = cs_create(t, 1, protocol)))
264 printf("Connecting...");
266 if (cs_connect(conn, add) < 0)
278 int cmd_authentication(char *arg)
280 static Z_IdAuthentication au;
281 static char open[256];
285 printf("Auth field set to null\n");
290 au.which = Z_IdAuthentication_open;
296 /* SEARCH SERVICE ------------------------------ */
298 void display_grs1(Z_GenericRecord *r, int level)
304 for (i = 0; i < r->num_elements; i++)
308 printf("%*s", level * 4, "");
312 printf("%d,", *t->tagType);
315 if (t->tagValue->which == Z_StringOrNumeric_numeric)
316 printf("%d) ", *t->tagValue->u.numeric);
318 printf("%s) ", t->tagValue->u.string);
319 if (t->content->which == Z_ElementData_subtree)
322 display_grs1(t->content->u.subtree, level+1);
324 else if (t->content->which == Z_ElementData_string)
325 printf("%s\n", t->content->u.string);
326 else if (t->content->which == Z_ElementData_numeric)
327 printf("%d\n", *t->content->u.numeric);
333 void display_record(Z_DatabaseRecord *p)
335 Z_External *r = (Z_External*) p;
336 oident *ent = oid_getentbyoid(r->direct_reference);
338 if (r->direct_reference)
340 printf("Record type: ");
342 printf("%s\n", ent->desc);
343 else if (!odr_oid(print, &r->direct_reference, 0))
345 odr_perror(print, "print oid");
349 if (r->which == Z_External_octet && p->u.octet_aligned->len)
350 marc_display ((char*)p->u.octet_aligned->buf, stdout);
351 else if (ent->value == VAL_SUTRS)
353 if (r->which != Z_External_sutrs)
355 printf("Expecting single SUTRS type for SUTRS.\n");
358 printf("%.*s", r->u.sutrs->len, r->u.sutrs->buf);
360 else if (ent->value == VAL_GRS1)
362 if (r->which != Z_External_grs1)
364 printf("Expecting single GRS type for GRS.\n");
367 display_grs1(r->u.grs1, 0);
371 printf("Unknown record representation.\n");
372 if (!z_External(print, &r, 0))
374 odr_perror(print, "Printing external");
380 static void display_diagrec(Z_DiagRec *p)
384 Z_DefaultDiagFormat *r;
389 printf("Diagnostic message from database:\n");
391 if (p->which != Z_DiagRec_defaultFormat)
393 printf("Diagnostic record not in default format.\n");
397 r = p->u.defaultFormat;
399 if (!(ent = oid_getentbyoid(r->diagnosticSetId)) ||
400 ent->class != CLASS_DIAGSET || ent->value != VAL_BIB1)
401 printf("Missing or unknown diagset\n");
402 printf(" [%d] %s", *r->condition, diagbib1_str(*r->condition));
403 if (r->addinfo && *r->addinfo)
404 printf(" -- %s\n", r->addinfo);
409 static void display_nameplusrecord(Z_NamePlusRecord *p)
412 printf("[%s]", p->databaseName);
413 if (p->which == Z_NamePlusRecord_surrogateDiagnostic)
414 display_diagrec(p->u.surrogateDiagnostic);
416 display_record(p->u.databaseRecord);
419 static void display_records(Z_Records *p)
423 if (p->which == Z_Records_NSD)
424 display_diagrec(p->u.nonSurrogateDiagnostic);
427 printf("Records: %d\n", p->u.databaseOrSurDiagnostics->num_records);
428 for (i = 0; i < p->u.databaseOrSurDiagnostics->num_records; i++)
429 display_nameplusrecord(p->u.databaseOrSurDiagnostics->records[i]);
433 static int send_searchRequest(char *arg)
435 Z_APDU *apdu = zget_APDU(out, Z_APDU_searchRequest);
436 Z_SearchRequest *req = apdu->u.searchRequest;
437 char *databaseNames = database;
441 struct ccl_rpn_node *rpn;
447 Z_RPNQuery *RPNquery;
455 rpn = ccl_find_str(bibset, arg, &error, &pos);
458 printf("CCL ERROR: %s\n", ccl_err_msg(error));
464 if (!strcmp(arg, "@big")) /* strictly for troublemaking */
466 static unsigned char big[2100];
469 /* send a very big referenceid to test transport stack etc. */
470 memset(big, 'A', 2100);
471 bigo.len = bigo.size = 2100;
473 req->referenceId = &bigo;
478 sprintf(setstring, "%d", ++setnumber);
479 req->resultSetName = setstring;
481 *req->smallSetUpperBound = smallSetUpperBound;
482 *req->largeSetLowerBound = largeSetLowerBound;
483 *req->mediumSetPresentNumber = mediumSetPresentNumber;
484 if (smallSetUpperBound > 0 || (largeSetLowerBound > 1 &&
485 mediumSetPresentNumber > 0))
489 prefsyn.proto = protocol;
490 prefsyn.class = CLASS_RECSYN;
491 prefsyn.value = recordsyntax;
492 req->preferredRecordSyntax = odr_oiddup(out, oid_getoidbyent(&prefsyn));
493 req->smallSetElementSetNames =
494 req->mediumSetElementSetNames = elementSetNames;
496 req->num_databaseNames = 1;
497 req->databaseNames = &databaseNames;
502 query.which = Z_Query_type_1;
505 assert((RPNquery = ccl_rpn_query(rpn)));
507 RPNquery = p_query_rpn (out, arg);
510 printf("Prefix query error\n");
514 bib1.proto = protocol;
515 bib1.class = CLASS_ATTSET;
516 bib1.value = VAL_BIB1;
517 RPNquery->attributeSetId = oid_getoidbyent(&bib1);
518 query.u.type_1 = RPNquery;
520 query.which = Z_Query_type_2;
521 query.u.type_2 = &ccl_query;
522 ccl_query.buf = (unsigned char*) arg;
523 ccl_query.len = strlen(arg);
528 printf("Sent searchRequest.\n");
532 static int process_searchResponse(Z_SearchResponse *res)
534 if (*res->searchStatus)
535 printf("Search was a success.\n");
537 printf("Search was a bloomin' failure.\n");
538 printf("Number of hits: %d, setno %d\n",
539 *res->resultCount, setnumber);
540 printf("records returned: %d\n",
541 *res->numberOfRecordsReturned);
542 setno += *res->numberOfRecordsReturned;
544 display_records(res->records);
548 static int cmd_find(char *arg)
552 printf("Find what?\n");
557 printf("Not connected yet\n");
560 if (!send_searchRequest(arg))
565 static int cmd_ssub(char *arg)
567 if (!(smallSetUpperBound = atoi(arg)))
572 static int cmd_lslb(char *arg)
574 if (!(largeSetLowerBound = atoi(arg)))
579 static int cmd_mspn(char *arg)
581 if (!(mediumSetPresentNumber = atoi(arg)))
586 static int cmd_status(char *arg)
588 printf("smallSetUpperBound: %d\n", smallSetUpperBound);
589 printf("largeSetLowerBound: %d\n", largeSetLowerBound);
590 printf("mediumSetPresentNumber: %d\n", mediumSetPresentNumber);
594 static int cmd_base(char *arg)
598 printf("Usage: base <database>\n");
601 strcpy(database, arg);
605 static int cmd_setnames(char *arg)
609 printf("Set numbering enabled.\n");
614 printf("Set numbering disabled.\n");
620 /* PRESENT SERVICE ----------------------------- */
622 static int send_presentRequest(char *arg)
624 Z_APDU *apdu = zget_APDU(out, Z_APDU_presentRequest);
625 Z_PresentRequest *req = apdu->u.presentRequest;
626 Z_RecordComposition compo;
632 if ((p = strchr(arg, '+')))
642 sprintf(setstring, "%d", setnumber);
643 req->resultSetId = setstring;
645 req->resultSetStartPoint = &setno;
646 req->numberOfRecordsRequested = &nos;
647 prefsyn.proto = protocol;
648 prefsyn.class = CLASS_RECSYN;
649 prefsyn.value = recordsyntax;
650 req->preferredRecordSyntax = oid_getoidbyent(&prefsyn);
653 req->recordComposition = &compo;
654 compo.which = Z_RecordComp_simple;
655 compo.u.simple = elementSetNames;
658 printf("Sent presentRequest (%d+%d).\n", setno, nos);
662 void process_close(Z_Close *req)
664 Z_APDU *apdu = zget_APDU(out, Z_APDU_close);
665 Z_Close *res = apdu->u.close;
667 static char *reasons[] =
681 printf("Reason: %s, message: %s\n", reasons[*req->closeReason],
682 req->diagnosticInformation ? req->diagnosticInformation : "NULL");
685 printf("Goodbye.\n");
688 *res->closeReason = Z_Close_finished;
690 printf("Sent response.\n");
694 static int cmd_show(char *arg)
696 if (!send_presentRequest(arg))
701 int cmd_quit(char *arg)
703 printf("See you later, alligator.\n");
707 int cmd_cancel(char *arg)
709 Z_APDU *apdu = zget_APDU(out, Z_APDU_triggerResourceControlRequest);
710 Z_TriggerResourceControlRequest *req =
711 apdu->u.triggerResourceControlRequest;
716 printf("Session not initialized yet\n");
719 if (!ODR_MASK_GET(session->options, Z_Options_triggerResourceCtrl))
721 printf("Target doesn't support cancel (trigger resource ctrl)\n");
724 *req->requestedAction = Z_TriggerResourceCtrl_cancel;
725 req->resultSetWanted = &false;
728 printf("Sent cancel request\n");
732 int send_scanrequest(char *string, int pp, int num)
734 Z_APDU *apdu = zget_APDU(out, Z_APDU_scanRequest);
735 Z_ScanRequest *req = apdu->u.scanRequest;
739 req->num_databaseNames = 1;
740 req->databaseNames = &db;
741 attset.proto = protocol;
742 attset.class = CLASS_ATTSET;
743 attset.value = VAL_BIB1;
744 req->attributeSet = oid_getoidbyent(&attset);
745 req->termListAndStartPoint = p_query_scan(out, string);
746 req->numberOfTermsRequested = #
747 req->preferredPositionInResponse = &pp;
752 void display_term(Z_TermInfo *t)
754 if (t->term->which == Z_Term_general)
756 printf("%.*s (%d)\n", t->term->u.general->len, t->term->u.general->buf,
757 t->globalOccurrences ? *t->globalOccurrences : -1);
758 sprintf(last_scan, "%.*s", t->term->u.general->len,
759 t->term->u.general->buf);
762 printf("Term type not general.\n");
765 void process_scanResponse(Z_ScanResponse *res)
769 printf("SCAN: %d entries, position=%d\n", *res->numberOfEntriesReturned,
770 *res->positionOfTerm);
771 if (*res->scanStatus != Z_Scan_success)
772 printf("Scan returned code %d\n", *res->scanStatus);
775 if (res->entries->which == Z_ListEntries_entries)
777 Z_Entries *ent = res->entries->u.entries;
779 for (i = 0; i < ent->num_entries; i++)
780 if (ent->entries[i]->which == Z_Entry_termInfo)
782 printf("%c ", i + 1 == *res->positionOfTerm ? '*' : ' ');
783 display_term(ent->entries[i]->u.termInfo);
786 display_diagrec(ent->entries[i]->u.surrogateDiagnostic);
789 display_diagrec(res->entries->u.nonSurrogateDiagnostics->diagRecs[0]);
792 int cmd_scan(char *arg)
796 printf("Session not initialized yet\n");
799 if (!ODR_MASK_GET(session->options, Z_Options_scan))
801 printf("Target doesn't support scan\n");
806 if (send_scanrequest(arg, 5, 20) < 0)
810 if (send_scanrequest(last_scan, 1, 20) < 0)
815 int cmd_format(char *arg)
819 printf("Usage: format <recordsyntax>\n");
822 if (!strcmp(arg, "sutrs"))
824 printf("Preferred format is SUTRS.\n");
825 recordsyntax = VAL_SUTRS;
828 else if (!strcmp(arg, "usmarc"))
830 printf("Preferred format is USMARC\n");
831 recordsyntax = VAL_USMARC;
834 else if (!strcmp(arg, "danmarc"))
836 printf("Preferred format is DANMARC\n");
837 recordsyntax = VAL_DANMARC;
840 else if (!strcmp(arg, "grs1"))
842 printf("Preferred format is GRS1\n");
843 recordsyntax = VAL_GRS1;
848 printf("Specify one of {sutrs,usmarc,danmarc,grs1}.\n");
853 int cmd_elements(char *arg)
855 static Z_ElementSetNames esn;
856 static char what[100];
860 printf("Usage: elements <esn>\n");
864 esn.which = Z_ElementSetNames_generic;
865 esn.u.generic = what;
866 elementSetNames = &esn;
870 int cmd_close(char *arg)
872 Z_APDU *apdu = zget_APDU(out, Z_APDU_close);
873 Z_Close *req = apdu->u.close;
875 *req->closeReason = Z_Close_finished;
877 printf("Sent close request.\n");
882 static void initialize(void)
890 if (!(out = odr_createmem(ODR_ENCODE)) ||
891 !(in = odr_createmem(ODR_DECODE)) ||
892 !(print = odr_createmem(ODR_PRINT)))
894 fprintf(stderr, "failed to allocate ODR streams\n");
897 setvbuf(stdout, 0, _IONBF, 0);
901 bibset = ccl_qual_mk ();
902 inf = fopen ("default.bib", "r");
905 ccl_qual_file (bibset, inf);
912 static int client(void)
916 int (*fun)(char *arg);
919 {"open", cmd_open, "('tcp'|'osi')':'[<TSEL>'/']<HOST>[':'<PORT>]"},
920 {"quit", cmd_quit, ""},
921 {"find", cmd_find, "<CCL-QUERY>"},
922 {"base", cmd_base, "<BASE-NAME>"},
923 {"show", cmd_show, "<REC#>['+'<#RECS>]"},
924 {"scan", cmd_scan, "<TERM>"},
925 {"authentication", cmd_authentication, "<ACCTSTRING>"},
926 {"lslb", cmd_lslb, "<largeSetLowerBound>"},
927 {"ssub", cmd_ssub, "<smallSetUpperBound>"},
928 {"mspn", cmd_mspn, "<mediumSetPresentNumber>"},
929 {"status", cmd_status, ""},
930 {"setnames", cmd_setnames, ""},
931 {"cancel", cmd_cancel, ""},
932 {"format", cmd_format, "<recordsyntax>"},
933 {"elements", cmd_elements, "<elementSetName>"},
934 {"close", cmd_close, ""},
938 int netbufferlen = 0;
946 char line[1024], word[1024], arg[1024];
951 FD_SET(cs_fileno(conn), &input);
952 if ((res = select(20, &input, 0, 0, 0)) < 0)
959 if (FD_ISSET(0, &input))
961 /* quick & dirty way to get a command line. */
964 if ((res = sscanf(line, "%s %[^;]", word, arg)) <= 0)
966 strcpy(word, last_cmd);
971 strcpy(last_cmd, word);
972 for (i = 0; cmd[i].cmd; i++)
973 if (!strncmp(cmd[i].cmd, word, strlen(word)))
975 res = (*cmd[i].fun)(arg);
978 if (!cmd[i].cmd) /* dump our help-screen */
980 printf("Unknown command: %s.\n", word);
981 printf("Currently recognized commands:\n");
982 for (i = 0; cmd[i].cmd; i++)
983 printf(" %s %s\n", cmd[i].cmd, cmd[i].ad);
989 if (conn && FD_ISSET(cs_fileno(conn), &input))
993 if ((res = cs_get(conn, &netbuffer, &netbufferlen)) < 0)
1000 printf("Target closed connection.\n");
1003 odr_reset(in); /* release APDU from last round */
1004 odr_setbuf(in, netbuffer, res, 0);
1005 if (!z_APDU(in, &apdu, 0))
1007 odr_perror(in, "Decoding incoming APDU");
1008 fprintf(stderr, "[Near %d]\n", odr_offset(in));
1009 fprintf(stderr, "Packet dump:\n---------\n");
1010 odr_dumpBER(stderr, netbuffer, res);
1011 fprintf(stderr, "---------\n");
1015 if (!z_APDU(print, &apdu, 0))
1017 odr_perror(print, "Failed to print incoming APDU");
1024 case Z_APDU_initResponse:
1025 process_initResponse(apdu->u.initResponse);
1027 case Z_APDU_searchResponse:
1028 process_searchResponse(apdu->u.searchResponse);
1030 case Z_APDU_scanResponse:
1031 process_scanResponse(apdu->u.scanResponse);
1033 case Z_APDU_presentResponse:
1034 printf("Received presentResponse.\n");
1036 *apdu->u.presentResponse->numberOfRecordsReturned;
1037 if (apdu->u.presentResponse->records)
1038 display_records(apdu->u.presentResponse->records);
1040 printf("No records.\n");
1043 printf("Target has closed the association.\n");
1044 process_close(apdu->u.close);
1047 printf("Received unknown APDU type (%d).\n",
1054 while (cs_more(conn));
1060 int main(int argc, char **argv)