2 * Copyright (c) 1995, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.21 1995-09-29 17:01:47 quinn
10 * Revision 1.20 1995/08/29 14:24:13 quinn
11 * Added second half of close-handshake
13 * Revision 1.19 1995/08/29 11:17:28 quinn
14 * Added code to receive close
16 * Revision 1.18 1995/08/28 12:21:27 quinn
17 * Client can now ask for simple element set names.
19 * Revision 1.17 1995/08/17 12:45:02 quinn
20 * Fixed minor problems with GRS-1. Added support in c&s.
22 * Revision 1.16 1995/08/15 12:00:04 quinn
25 * Revision 1.15 1995/06/22 09:28:03 quinn
26 * Fixed bug in SUTRS processing.
28 * Revision 1.14 1995/06/19 12:37:41 quinn
31 * Revision 1.13 1995/06/16 10:29:11 quinn
32 * *** empty log message ***
34 * Revision 1.12 1995/06/15 07:44:57 quinn
37 * Revision 1.11 1995/06/14 15:26:40 quinn
38 * *** empty log message ***
40 * Revision 1.10 1995/06/06 14:56:58 quinn
43 * Revision 1.9 1995/06/06 08:15:19 quinn
46 * Revision 1.8 1995/06/05 10:52:22 quinn
49 * Revision 1.7 1995/06/02 09:50:09 quinn
52 * Revision 1.6 1995/05/31 08:29:21 quinn
53 * Nothing significant.
55 * Revision 1.5 1995/05/29 08:10:47 quinn
56 * Moved oid.c to util.
58 * Revision 1.4 1995/05/22 15:30:13 adam
59 * Client uses prefix query notation.
61 * Revision 1.3 1995/05/22 15:06:53 quinn
62 * *** empty log message ***
64 * Revision 1.2 1995/05/22 14:56:40 quinn
65 * *** empty log message ***
67 * Revision 1.1 1995/05/22 11:30:31 quinn
68 * Added prettier client.
74 * This is the obligatory little toy client, whose primary purpose is
75 * to illustrate the use of the YAZ service-level API.
87 #include <sys/select.h>
108 #define C_PROMPT "Z> "
110 static ODR out, in, print; /* encoding and decoding streams */
111 static COMSTACK conn = 0; /* our z-association */
112 static Z_IdAuthentication *auth = 0; /* our current auth definition */
113 static char database[512] = "Default"; /* Database name */
114 static int setnumber = 0; /* current result set number */
115 static int smallSetUpperBound = 0;
116 static int largeSetLowerBound = 1;
117 static int mediumSetPresentNumber = 0;
118 static Z_ElementSetNames *elementSetNames = 0;
119 static int setno = 1; /* current set offset */
120 static int protocol = PROTO_Z3950; /* current app protocol */
121 static int recordsyntax = VAL_USMARC;
122 static int sent_close = 0;
123 static ODR_MEM session_mem; /* memory handle for init-response */
124 static Z_InitResponse *session = 0; /* session parameters */
125 static char last_scan[512] = "0";
126 static char last_cmd[100] = "?";
129 static CCL_bibset bibset; /* CCL bibset handle */
133 static void send_apdu(Z_APDU *a)
138 if (!z_APDU(out, &a, 0))
140 odr_perror(out, "Encoding APDU");
143 buf = odr_getbuf(out, &len, 0);
144 odr_reset(out); /* release the APDU */
145 if (cs_put(conn, buf, len) < 0)
147 fprintf(stderr, "cs_put: %s", cs_errmsg(cs_errno(conn)));
152 /* INIT SERVICE ------------------------------- */
154 static void send_initRequest()
156 Z_APDU *apdu = zget_APDU(out, Z_APDU_initRequest);
157 Z_InitRequest *req = apdu->u.initRequest;
159 ODR_MASK_SET(req->options, Z_Options_search);
160 ODR_MASK_SET(req->options, Z_Options_present);
161 ODR_MASK_SET(req->options, Z_Options_namedResultSets);
162 ODR_MASK_SET(req->options, Z_Options_triggerResourceCtrl);
163 ODR_MASK_SET(req->options, Z_Options_scan);
165 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_1);
166 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_2);
167 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_3);
169 req->idAuthentication = auth;
172 printf("Sent initrequest.\n");
175 static int process_initResponse(Z_InitResponse *res)
177 /* save session parameters for later use */
178 session_mem = odr_extract_mem(in);
182 printf("Connection rejected by target.\n");
184 printf("Connection accepted by target.\n");
185 if (res->implementationId)
186 printf("ID : %s\n", res->implementationId);
187 if (res->implementationName)
188 printf("Name : %s\n", res->implementationName);
189 if (res->implementationVersion)
190 printf("Version: %s\n", res->implementationVersion);
191 if (res->userInformationField)
193 printf("UserInformationfield:\n");
194 if (!z_External(print, (Z_External**)&res-> userInformationField,
197 odr_perror(print, "Printing userinfo\n");
200 if (res->userInformationField->which == Z_External_octet)
202 printf("Guessing visiblestring:\n");
203 printf("'%s'\n", res->userInformationField->u. octet_aligned->buf);
209 int cmd_open(char *arg)
212 char type[100], addr[100];
217 printf("Already connected.\n");
220 if (!*arg || sscanf(arg, "%[^:]:%s", type, addr) < 2)
222 fprintf(stderr, "Usage: open (osi|tcp) ':' [tsel '/']host[':'port]\n");
226 if (!strcmp(type, "osi"))
228 if (!(add = mosi_strtoaddr(addr)))
238 if (!strcmp(type, "tcp"))
240 if (!(add = tcpip_strtoaddr(addr)))
246 protocol = PROTO_Z3950;
250 fprintf(stderr, "Bad type: %s\n", type);
253 if (!(conn = cs_create(t, 1, protocol)))
258 printf("Connecting...");
260 if (cs_connect(conn, add) < 0)
272 int cmd_authentication(char *arg)
274 static Z_IdAuthentication au;
275 static char open[256];
279 printf("Auth field set to null\n");
284 au.which = Z_IdAuthentication_open;
290 /* SEARCH SERVICE ------------------------------ */
292 void display_grs1(Z_GenericRecord *r, int level)
298 for (i = 0; i < r->num_elements; i++)
302 printf("%*s", level * 4, "");
306 printf("%d,", *t->tagType);
309 if (t->tagValue->which == Z_StringOrNumeric_numeric)
310 printf("%d) ", *t->tagValue->u.numeric);
312 printf("%s) ", t->tagValue->u.string);
313 if (t->content->which == Z_ElementData_subtree)
316 display_grs1(t->content->u.subtree, level+1);
318 else if (t->content->which == Z_ElementData_string)
319 printf("%s\n", t->content->u.string);
325 void display_record(Z_DatabaseRecord *p)
327 Z_External *r = (Z_External*) p;
328 oident *ent = oid_getentbyoid(r->direct_reference);
330 if (r->direct_reference)
332 printf("Record type: ");
334 printf("%s\n", ent->desc);
335 else if (!odr_oid(print, &r->direct_reference, 0))
337 odr_perror(print, "print oid");
341 if (r->which == Z_External_octet && p->u.octet_aligned->len)
342 marc_display ((char*)p->u.octet_aligned->buf, stdout);
343 else if (ent->value == VAL_SUTRS)
345 if (r->which != Z_External_sutrs)
347 printf("Expecting single SUTRS type for SUTRS.\n");
350 printf("%.*s", r->u.sutrs->len, r->u.sutrs->buf);
352 else if (ent->value == VAL_GRS1)
354 if (r->which != Z_External_grs1)
356 printf("Expecting single GRS type for GRS.\n");
359 display_grs1(r->u.grs1, 0);
363 printf("Unknown record representation.\n");
364 if (!z_External(print, &r, 0))
366 odr_perror(print, "Printing external");
372 static void display_diagrec(Z_DiagRec *p)
376 Z_DefaultDiagFormat *r;
381 printf("Diagnostic message from database:\n");
383 if (p->which != Z_DiagRec_defaultFormat)
385 printf("Diagnostic record not in default format.\n");
389 r = p->u.defaultFormat;
391 if (!(ent = oid_getentbyoid(r->diagnosticSetId)) ||
392 ent->class != CLASS_DIAGSET || ent->value != VAL_BIB1)
393 printf("Missing or unknown diagset\n");
394 printf(" [%d] %s", *r->condition, diagbib1_str(*r->condition));
395 if (r->addinfo && *r->addinfo)
396 printf(" -- %s\n", r->addinfo);
401 static void display_nameplusrecord(Z_NamePlusRecord *p)
404 printf("[%s]", p->databaseName);
405 if (p->which == Z_NamePlusRecord_surrogateDiagnostic)
406 display_diagrec(p->u.surrogateDiagnostic);
408 display_record(p->u.databaseRecord);
411 static void display_records(Z_Records *p)
415 if (p->which == Z_Records_NSD)
416 display_diagrec(p->u.nonSurrogateDiagnostic);
419 printf("Records: %d\n", p->u.databaseOrSurDiagnostics->num_records);
420 for (i = 0; i < p->u.databaseOrSurDiagnostics->num_records; i++)
421 display_nameplusrecord(p->u.databaseOrSurDiagnostics->records[i]);
425 static int send_searchRequest(char *arg)
427 Z_APDU *apdu = zget_APDU(out, Z_APDU_searchRequest);
428 Z_SearchRequest *req = apdu->u.searchRequest;
429 char *databaseNames = database;
433 struct ccl_rpn_node *rpn;
439 Z_RPNQuery *RPNquery;
447 rpn = ccl_find_str(bibset, arg, &error, &pos);
450 printf("CCL ERROR: %s\n", ccl_err_msg(error));
456 if (!strcmp(arg, "@big")) /* strictly for troublemaking */
458 static unsigned char big[2100];
461 /* send a very big referenceid to test transport stack etc. */
462 memset(big, 'A', 2100);
463 bigo.len = bigo.size = 2100;
465 req->referenceId = &bigo;
470 sprintf(setstring, "%d", ++setnumber);
471 req->resultSetName = setstring;
473 *req->smallSetUpperBound = smallSetUpperBound;
474 *req->largeSetLowerBound = largeSetLowerBound;
475 *req->mediumSetPresentNumber = mediumSetPresentNumber;
476 if (smallSetUpperBound > 0 || (largeSetLowerBound > 1 &&
477 mediumSetPresentNumber > 0))
481 prefsyn.proto = protocol;
482 prefsyn.class = CLASS_RECSYN;
483 prefsyn.value = recordsyntax;
484 req->preferredRecordSyntax = odr_oiddup(out, oid_getoidbyent(&prefsyn));
485 req->smallSetElementSetNames =
486 req->mediumSetElementSetNames = elementSetNames;
488 req->num_databaseNames = 1;
489 req->databaseNames = &databaseNames;
494 query.which = Z_Query_type_1;
497 assert((RPNquery = ccl_rpn_query(rpn)));
499 RPNquery = p_query_rpn (out, arg);
502 printf("Prefix query error\n");
506 bib1.proto = protocol;
507 bib1.class = CLASS_ATTSET;
508 bib1.value = VAL_BIB1;
509 RPNquery->attributeSetId = oid_getoidbyent(&bib1);
510 query.u.type_1 = RPNquery;
512 query.which = Z_Query_type_2;
513 query.u.type_2 = &ccl_query;
514 ccl_query.buf = (unsigned char*) arg;
515 ccl_query.len = strlen(arg);
520 printf("Sent searchRequest.\n");
524 static int process_searchResponse(Z_SearchResponse *res)
526 if (*res->searchStatus)
527 printf("Search was a success.\n");
529 printf("Search was a bloomin' failure.\n");
530 printf("Number of hits: %d, setno %d\n",
531 *res->resultCount, setnumber);
532 printf("records returned: %d\n",
533 *res->numberOfRecordsReturned);
534 setno += *res->numberOfRecordsReturned;
536 display_records(res->records);
540 static int cmd_find(char *arg)
544 printf("Find what?\n");
549 printf("Not connected yet\n");
552 if (!send_searchRequest(arg))
557 static int cmd_ssub(char *arg)
559 if (!(smallSetUpperBound = atoi(arg)))
564 static int cmd_lslb(char *arg)
566 if (!(largeSetLowerBound = atoi(arg)))
571 static int cmd_mspn(char *arg)
573 if (!(mediumSetPresentNumber = atoi(arg)))
578 static int cmd_status(char *arg)
580 printf("smallSetUpperBound: %d\n", smallSetUpperBound);
581 printf("largeSetLowerBound: %d\n", largeSetLowerBound);
582 printf("mediumSetPresentNumber: %d\n", mediumSetPresentNumber);
586 static int cmd_base(char *arg)
590 printf("Usage: base <database>\n");
593 strcpy(database, arg);
597 static int cmd_setnames(char *arg)
601 printf("Set numbering enabled.\n");
606 printf("Set numbering disabled.\n");
612 /* PRESENT SERVICE ----------------------------- */
614 static int send_presentRequest(char *arg)
616 Z_APDU *apdu = zget_APDU(out, Z_APDU_presentRequest);
617 Z_PresentRequest *req = apdu->u.presentRequest;
618 Z_RecordComposition compo;
624 if ((p = strchr(arg, '+')))
634 sprintf(setstring, "%d", setnumber);
635 req->resultSetId = setstring;
637 req->resultSetStartPoint = &setno;
638 req->numberOfRecordsRequested = &nos;
639 prefsyn.proto = protocol;
640 prefsyn.class = CLASS_RECSYN;
641 prefsyn.value = recordsyntax;
642 req->preferredRecordSyntax = oid_getoidbyent(&prefsyn);
645 req->recordComposition = &compo;
646 compo.which = Z_RecordComp_simple;
647 compo.u.simple = elementSetNames;
650 printf("Sent presentRequest (%d+%d).\n", setno, nos);
654 void process_close(Z_Close *req)
656 Z_APDU *apdu = zget_APDU(out, Z_APDU_close);
657 Z_Close *res = apdu->u.close;
659 static char *reasons[] =
673 printf("Reason: %s, message: %s\n", reasons[*req->closeReason],
674 req->diagnosticInformation ? req->diagnosticInformation : "NULL");
677 printf("Goodbye.\n");
680 *res->closeReason = Z_Close_finished;
682 printf("Sent response.\n");
686 static int cmd_show(char *arg)
688 if (!send_presentRequest(arg))
693 int cmd_quit(char *arg)
695 printf("See you later, alligator.\n");
699 int cmd_cancel(char *arg)
701 Z_APDU *apdu = zget_APDU(out, Z_APDU_triggerResourceControlRequest);
702 Z_TriggerResourceControlRequest *req =
703 apdu->u.triggerResourceControlRequest;
708 printf("Session not initialized yet\n");
711 if (!ODR_MASK_GET(session->options, Z_Options_triggerResourceCtrl))
713 printf("Target doesn't support cancel (trigger resource ctrl)\n");
716 *req->requestedAction = Z_TriggerResourceCtrl_cancel;
717 req->resultSetWanted = &false;
720 printf("Sent cancel request\n");
724 int send_scanrequest(char *string, int pp, int num)
726 Z_APDU *apdu = zget_APDU(out, Z_APDU_scanRequest);
727 Z_ScanRequest *req = apdu->u.scanRequest;
731 req->num_databaseNames = 1;
732 req->databaseNames = &db;
733 attset.proto = protocol;
734 attset.class = CLASS_ATTSET;
735 attset.value = VAL_BIB1;
736 req->attributeSet = oid_getoidbyent(&attset);
737 req->termListAndStartPoint = p_query_scan(out, string);
738 req->numberOfTermsRequested = #
739 req->preferredPositionInResponse = &pp;
744 void display_term(Z_TermInfo *t)
746 if (t->term->which == Z_Term_general)
748 printf("%.*s (%d)\n", t->term->u.general->len, t->term->u.general->buf,
749 t->globalOccurrences ? *t->globalOccurrences : -1);
750 sprintf(last_scan, "%.*s", t->term->u.general->len,
751 t->term->u.general->buf);
754 printf("Term type not general.\n");
757 void process_scanResponse(Z_ScanResponse *res)
761 printf("SCAN: %d entries, position=%d\n", *res->numberOfEntriesReturned,
762 *res->positionOfTerm);
763 if (*res->scanStatus != Z_Scan_success)
764 printf("Scan returned code %d\n", *res->scanStatus);
767 if (res->entries->which == Z_ListEntries_entries)
769 Z_Entries *ent = res->entries->u.entries;
771 for (i = 0; i < ent->num_entries; i++)
772 if (ent->entries[i]->which == Z_Entry_termInfo)
774 printf("%c ", i + 1 == *res->positionOfTerm ? '*' : ' ');
775 display_term(ent->entries[i]->u.termInfo);
778 display_diagrec(ent->entries[i]->u.surrogateDiagnostic);
781 display_diagrec(res->entries->u.nonSurrogateDiagnostics->diagRecs[0]);
784 int cmd_scan(char *arg)
788 printf("Session not initialized yet\n");
791 if (!ODR_MASK_GET(session->options, Z_Options_scan))
793 printf("Target doesn't support scan\n");
798 if (send_scanrequest(arg, 5, 20) < 0)
802 if (send_scanrequest(last_scan, 1, 20) < 0)
807 int cmd_format(char *arg)
811 printf("Usage: format <recordsyntax>\n");
814 if (!strcmp(arg, "sutrs"))
816 printf("Preferred format is SUTRS.\n");
817 recordsyntax = VAL_SUTRS;
820 else if (!strcmp(arg, "usmarc"))
822 printf("Preferred format is USMARC\n");
823 recordsyntax = VAL_USMARC;
826 else if (!strcmp(arg, "danmarc"))
828 printf("Preferred format is DANMARC\n");
829 recordsyntax = VAL_DANMARC;
832 else if (!strcmp(arg, "grs1"))
834 printf("Preferred format is GRS1\n");
835 recordsyntax = VAL_GRS1;
840 printf("Specify one of {sutrs,usmarc,danmarc,grs1}.\n");
845 int cmd_elements(char *arg)
847 static Z_ElementSetNames esn;
848 static char what[100];
852 printf("Usage: elements <esn>\n");
856 esn.which = Z_ElementSetNames_generic;
857 esn.u.generic = what;
858 elementSetNames = &esn;
862 int cmd_close(char *arg)
864 Z_APDU *apdu = zget_APDU(out, Z_APDU_close);
865 Z_Close *req = apdu->u.close;
867 *req->closeReason = Z_Close_finished;
869 printf("Sent close request.\n");
874 static void initialize(void)
882 if (!(out = odr_createmem(ODR_ENCODE)) ||
883 !(in = odr_createmem(ODR_DECODE)) ||
884 !(print = odr_createmem(ODR_PRINT)))
886 fprintf(stderr, "failed to allocate ODR streams\n");
889 setvbuf(stdout, 0, _IONBF, 0);
893 bibset = ccl_qual_mk ();
894 inf = fopen ("default.bib", "r");
897 ccl_qual_file (bibset, inf);
904 static int client(void)
908 int (*fun)(char *arg);
911 {"open", cmd_open, "('tcp'|'osi')':'[<TSEL>'/']<HOST>[':'<PORT>]"},
912 {"quit", cmd_quit, ""},
913 {"find", cmd_find, "<CCL-QUERY>"},
914 {"base", cmd_base, "<BASE-NAME>"},
915 {"show", cmd_show, "<REC#>['+'<#RECS>]"},
916 {"scan", cmd_scan, "<TERM>"},
917 {"authentication", cmd_authentication, "<ACCTSTRING>"},
918 {"lslb", cmd_lslb, "<largeSetLowerBound>"},
919 {"ssub", cmd_ssub, "<smallSetUpperBound>"},
920 {"mspn", cmd_mspn, "<mediumSetPresentNumber>"},
921 {"status", cmd_status, ""},
922 {"setnames", cmd_setnames, ""},
923 {"cancel", cmd_cancel, ""},
924 {"format", cmd_format, "<recordsyntax>"},
925 {"elements", cmd_elements, "<elementSetName>"},
926 {"close", cmd_close, ""},
930 int netbufferlen = 0;
938 char line[1024], word[1024], arg[1024];
943 FD_SET(cs_fileno(conn), &input);
944 if ((res = select(20, &input, 0, 0, 0)) < 0)
951 if (FD_ISSET(0, &input))
953 /* quick & dirty way to get a command line. */
956 if ((res = sscanf(line, "%s %[^;]", word, arg)) <= 0)
958 strcpy(word, last_cmd);
963 strcpy(last_cmd, word);
964 for (i = 0; cmd[i].cmd; i++)
965 if (!strncmp(cmd[i].cmd, word, strlen(word)))
967 res = (*cmd[i].fun)(arg);
970 if (!cmd[i].cmd) /* dump our help-screen */
972 printf("Unknown command: %s.\n", word);
973 printf("Currently recognized commands:\n");
974 for (i = 0; cmd[i].cmd; i++)
975 printf(" %s %s\n", cmd[i].cmd, cmd[i].ad);
981 if (conn && FD_ISSET(cs_fileno(conn), &input))
985 if ((res = cs_get(conn, &netbuffer, &netbufferlen)) < 0)
992 printf("Target closed connection.\n");
995 odr_reset(in); /* release APDU from last round */
996 odr_setbuf(in, netbuffer, res, 0);
997 if (!z_APDU(in, &apdu, 0))
999 odr_perror(in, "Decoding incoming APDU");
1000 fprintf(stderr, "Packet dump:\n---------\n");
1001 odr_dumpBER(stderr, netbuffer, res);
1002 fprintf(stderr, "---------\n");
1006 if (!z_APDU(print, &apdu, 0))
1008 odr_perror(print, "Failed to print incoming APDU");
1015 case Z_APDU_initResponse:
1016 process_initResponse(apdu->u.initResponse);
1018 case Z_APDU_searchResponse:
1019 process_searchResponse(apdu->u.searchResponse);
1021 case Z_APDU_scanResponse:
1022 process_scanResponse(apdu->u.scanResponse);
1024 case Z_APDU_presentResponse:
1025 printf("Received presentResponse.\n");
1027 *apdu->u.presentResponse->numberOfRecordsReturned;
1028 if (apdu->u.presentResponse->records)
1029 display_records(apdu->u.presentResponse->records);
1031 printf("No records.\n");
1034 printf("Target has closed the association.\n");
1035 process_close(apdu->u.close);
1038 printf("Received unknown APDU type (%d).\n",
1045 while (cs_more(conn));
1051 int main(int argc, char **argv)