2 * Copyright (c) 1995, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.17 1995-08-17 12:45:02 quinn
8 * Fixed minor problems with GRS-1. Added support in c&s.
10 * Revision 1.16 1995/08/15 12:00:04 quinn
13 * Revision 1.15 1995/06/22 09:28:03 quinn
14 * Fixed bug in SUTRS processing.
16 * Revision 1.14 1995/06/19 12:37:41 quinn
19 * Revision 1.13 1995/06/16 10:29:11 quinn
20 * *** empty log message ***
22 * Revision 1.12 1995/06/15 07:44:57 quinn
25 * Revision 1.11 1995/06/14 15:26:40 quinn
26 * *** empty log message ***
28 * Revision 1.10 1995/06/06 14:56:58 quinn
31 * Revision 1.9 1995/06/06 08:15:19 quinn
34 * Revision 1.8 1995/06/05 10:52:22 quinn
37 * Revision 1.7 1995/06/02 09:50:09 quinn
40 * Revision 1.6 1995/05/31 08:29:21 quinn
41 * Nothing significant.
43 * Revision 1.5 1995/05/29 08:10:47 quinn
44 * Moved oid.c to util.
46 * Revision 1.4 1995/05/22 15:30:13 adam
47 * Client uses prefix query notation.
49 * Revision 1.3 1995/05/22 15:06:53 quinn
50 * *** empty log message ***
52 * Revision 1.2 1995/05/22 14:56:40 quinn
53 * *** empty log message ***
55 * Revision 1.1 1995/05/22 11:30:31 quinn
56 * Added prettier client.
62 * This is the obligatory little toy client, whose primary purpose is
63 * to illustrate the use of the YAZ service-level API.
71 #include <sys/select.h>
92 #define C_PROMPT "Z> "
94 static ODR out, in, print; /* encoding and decoding streams */
95 static COMSTACK conn = 0; /* our z-association */
96 static Z_IdAuthentication *auth = 0; /* our current auth definition */
97 static char database[512] = "Default"; /* Database name */
98 static int setnumber = 0; /* current result set number */
99 static int smallSetUpperBound = 0;
100 static int largeSetLowerBound = 1;
101 static int mediumSetPresentNumber = 0;
102 static int setno = 1; /* current set offset */
103 static int protocol = PROTO_Z3950; /* current app protocol */
104 static int recordsyntax = VAL_USMARC;
105 static ODR_MEM session_mem; /* memory handle for init-response */
106 static Z_InitResponse *session = 0; /* session parameters */
107 static char last_scan[512] = "0";
108 static char last_cmd[100] = "?";
111 static CCL_bibset bibset; /* CCL bibset handle */
115 static void send_apdu(Z_APDU *a)
120 if (!z_APDU(out, &a, 0))
122 odr_perror(out, "Encoding APDU");
125 buf = odr_getbuf(out, &len, 0);
126 odr_reset(out); /* release the APDU */
127 if (cs_put(conn, buf, len) < 0)
129 fprintf(stderr, "cs_put: %s", cs_errlist[cs_errno(conn)]);
134 /* INIT SERVICE ------------------------------- */
136 static void send_initRequest()
138 Z_APDU *apdu = zget_APDU(out, Z_APDU_initRequest);
139 Z_InitRequest *req = apdu->u.initRequest;
141 ODR_MASK_SET(req->options, Z_Options_search);
142 ODR_MASK_SET(req->options, Z_Options_present);
143 ODR_MASK_SET(req->options, Z_Options_namedResultSets);
144 ODR_MASK_SET(req->options, Z_Options_triggerResourceCtrl);
145 ODR_MASK_SET(req->options, Z_Options_scan);
147 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_1);
148 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_2);
150 req->idAuthentication = auth;
153 printf("Sent initrequest.\n");
156 static int process_initResponse(Z_InitResponse *res)
158 /* save session parameters for later use */
159 session_mem = odr_extract_mem(in);
163 printf("Connection rejected by target.\n");
165 printf("Connection accepted by target.\n");
166 if (res->implementationId)
167 printf("ID : %s\n", res->implementationId);
168 if (res->implementationName)
169 printf("Name : %s\n", res->implementationName);
170 if (res->implementationVersion)
171 printf("Version: %s\n", res->implementationVersion);
172 if (res->userInformationField)
174 printf("UserInformationfield:\n");
175 if (!z_External(print, (Z_External**)&res-> userInformationField,
178 odr_perror(print, "Printing userinfo\n");
181 if (res->userInformationField->which == ODR_EXTERNAL_octet)
183 printf("Guessing visiblestring:\n");
184 printf("'%s'\n", res->userInformationField->u. octet_aligned->buf);
190 int cmd_open(char *arg)
193 char type[100], addr[100];
198 printf("Already connected.\n");
201 if (!*arg || sscanf(arg, "%[^:]:%s", type, addr) < 2)
203 fprintf(stderr, "Usage: open (osi|tcp) ':' [tsel '/']host[':'port]\n");
207 if (!strcmp(type, "osi"))
209 if (!(add = mosi_strtoaddr(addr)))
219 if (!strcmp(type, "tcp"))
221 if (!(add = tcpip_strtoaddr(addr)))
227 protocol = PROTO_Z3950;
231 fprintf(stderr, "Bad type: %s\n", type);
234 if (!(conn = cs_create(t, 1, protocol)))
239 printf("Connecting...");
241 if (cs_connect(conn, add) < 0)
253 int cmd_authentication(char *arg)
255 static Z_IdAuthentication au;
256 static char open[256];
260 printf("Auth field set to null\n");
265 au.which = Z_IdAuthentication_open;
271 /* SEARCH SERVICE ------------------------------ */
273 void display_grs1(Z_GenericRecord *r, int level)
279 for (i = 0; i < r->num_elements; i++)
283 printf("%*s", level * 4, "");
287 printf("%d,", *t->tagType);
290 if (t->tagValue->which == Z_StringOrNumeric_numeric)
291 printf("%d) ", *t->tagValue->u.numeric);
293 printf("%s) ", t->tagValue->u.string);
294 if (t->content->which == Z_ElementData_subtree)
297 display_grs1(t->content->u.subtree, level+1);
299 else if (t->content->which == Z_ElementData_string)
300 printf("%s\n", t->content->u.string);
306 void display_record(Z_DatabaseRecord *p)
308 Z_External *r = (Z_External*) p;
309 oident *ent = oid_getentbyoid(r->direct_reference);
311 if (r->direct_reference)
313 printf("Record type: ");
315 printf("%s\n", ent->desc);
316 else if (!odr_oid(print, &r->direct_reference, 0))
318 odr_perror(print, "print oid");
322 if (r->which == ODR_EXTERNAL_octet && p->u.octet_aligned->len)
323 marc_display ((char*)p->u.octet_aligned->buf, stdout);
324 else if (ent->value == VAL_SUTRS)
326 if (r->which != Z_External_sutrs)
328 printf("Expecting single SUTRS type for SUTRS.\n");
331 printf("%.*s", r->u.sutrs->len, r->u.sutrs->buf);
333 else if (ent->value == VAL_GRS1)
335 if (r->which != Z_External_grs1)
337 printf("Expecting single GRS type for GRS.\n");
340 display_grs1(r->u.grs1, 0);
344 printf("Unknown record representation.\n");
345 if (!z_External(print, &r, 0))
347 odr_perror(print, "Printing external");
353 static void display_diagrec(Z_DiagRec *p)
357 Z_DefaultDiagFormat *r;
362 printf("Diagnostic message from database:\n");
364 if (p->which != Z_DiagRec_defaultFormat)
366 printf("Diagnostic record not in default format.\n");
370 r = p->u.defaultFormat;
372 if (!(ent = oid_getentbyoid(r->diagnosticSetId)) ||
373 ent->class != CLASS_DIAGSET || ent->value != VAL_BIB1)
374 printf("Missing or unknown diagset\n");
375 printf(" [%d] %s", *r->condition, diagbib1_str(*r->condition));
376 if (r->addinfo && *r->addinfo)
377 printf(" -- %s\n", r->addinfo);
382 static void display_nameplusrecord(Z_NamePlusRecord *p)
385 printf("[%s]", p->databaseName);
386 if (p->which == Z_NamePlusRecord_surrogateDiagnostic)
387 display_diagrec(p->u.surrogateDiagnostic);
389 display_record(p->u.databaseRecord);
392 static void display_records(Z_Records *p)
396 if (p->which == Z_Records_NSD)
397 display_diagrec(p->u.nonSurrogateDiagnostic);
400 printf("Records: %d\n", p->u.databaseOrSurDiagnostics->num_records);
401 for (i = 0; i < p->u.databaseOrSurDiagnostics->num_records; i++)
402 display_nameplusrecord(p->u.databaseOrSurDiagnostics->records[i]);
406 static int send_searchRequest(char *arg)
408 Z_APDU *apdu = zget_APDU(out, Z_APDU_searchRequest);
409 Z_SearchRequest *req = apdu->u.searchRequest;
410 char *databaseNames = database;
414 struct ccl_rpn_node *rpn;
420 Z_RPNQuery *RPNquery;
428 rpn = ccl_find_str(bibset, arg, &error, &pos);
431 printf("CCL ERROR: %s\n", ccl_err_msg(error));
437 if (!strcmp(arg, "@big")) /* strictly for troublemaking */
439 static unsigned char big[2100];
442 /* send a very big referenceid to test transport stack etc. */
443 memset(big, 'A', 2100);
444 bigo.len = bigo.size = 2100;
446 req->referenceId = &bigo;
451 sprintf(setstring, "%d", ++setnumber);
452 req->resultSetName = setstring;
454 *req->smallSetUpperBound = smallSetUpperBound;
455 *req->largeSetLowerBound = largeSetLowerBound;
456 *req->mediumSetPresentNumber = mediumSetPresentNumber;
457 if (smallSetUpperBound > 0 || (largeSetLowerBound > 1 &&
458 mediumSetPresentNumber > 0))
462 prefsyn.proto = protocol;
463 prefsyn.class = CLASS_RECSYN;
464 prefsyn.value = recordsyntax;
465 req->preferredRecordSyntax = odr_oiddup(out, oid_getoidbyent(&prefsyn));
467 req->num_databaseNames = 1;
468 req->databaseNames = &databaseNames;
473 query.which = Z_Query_type_1;
476 assert((RPNquery = ccl_rpn_query(rpn)));
478 RPNquery = p_query_rpn (out, arg);
481 printf("Prefix query error\n");
485 bib1.proto = protocol;
486 bib1.class = CLASS_ATTSET;
487 bib1.value = VAL_BIB1;
488 RPNquery->attributeSetId = oid_getoidbyent(&bib1);
489 query.u.type_1 = RPNquery;
491 query.which = Z_Query_type_2;
492 query.u.type_2 = &ccl_query;
493 ccl_query.buf = (unsigned char*) arg;
494 ccl_query.len = strlen(arg);
499 printf("Sent searchRequest.\n");
503 static int process_searchResponse(Z_SearchResponse *res)
505 if (*res->searchStatus)
506 printf("Search was a success.\n");
508 printf("Search was a bloomin' failure.\n");
509 printf("Number of hits: %d, setno %d\n",
510 *res->resultCount, setnumber);
511 printf("records returned: %d\n",
512 *res->numberOfRecordsReturned);
513 setno += *res->numberOfRecordsReturned;
515 display_records(res->records);
519 static int cmd_find(char *arg)
523 printf("Find what?\n");
528 printf("Not connected yet\n");
531 if (!send_searchRequest(arg))
536 static int cmd_ssub(char *arg)
538 if (!(smallSetUpperBound = atoi(arg)))
543 static int cmd_lslb(char *arg)
545 if (!(largeSetLowerBound = atoi(arg)))
550 static int cmd_mspn(char *arg)
552 if (!(mediumSetPresentNumber = atoi(arg)))
557 static int cmd_status(char *arg)
559 printf("smallSetUpperBound: %d\n", smallSetUpperBound);
560 printf("largeSetLowerBound: %d\n", largeSetLowerBound);
561 printf("mediumSetPresentNumber: %d\n", mediumSetPresentNumber);
565 static int cmd_base(char *arg)
569 printf("Usage: base <database>\n");
572 strcpy(database, arg);
576 static int cmd_setnames(char *arg)
580 printf("Set numbering enabled.\n");
585 printf("Set numbering disabled.\n");
591 /* PRESENT SERVICE ----------------------------- */
593 static int send_presentRequest(char *arg)
595 Z_APDU *apdu = zget_APDU(out, Z_APDU_presentRequest);
596 Z_PresentRequest *req = apdu->u.presentRequest;
602 if ((p = strchr(arg, '+')))
612 sprintf(setstring, "%d", setnumber);
613 req->resultSetId = setstring;
615 req->resultSetStartPoint = &setno;
616 req->numberOfRecordsRequested = &nos;
617 prefsyn.proto = protocol;
618 prefsyn.class = CLASS_RECSYN;
619 prefsyn.value = recordsyntax;
620 req->preferredRecordSyntax = oid_getoidbyent(&prefsyn);
622 printf("Sent presentRequest (%d+%d).\n", setno, nos);
626 static int cmd_show(char *arg)
628 if (!send_presentRequest(arg))
633 int cmd_quit(char *arg)
635 printf("See you later, alligator.\n");
639 int cmd_cancel(char *arg)
641 Z_APDU *apdu = zget_APDU(out, Z_APDU_triggerResourceControlRequest);
642 Z_TriggerResourceControlRequest *req =
643 apdu->u.triggerResourceControlRequest;
648 printf("Session not initialized yet\n");
651 if (!ODR_MASK_GET(session->options, Z_Options_triggerResourceCtrl))
653 printf("Target doesn't support cancel (trigger resource ctrl)\n");
656 *req->requestedAction = Z_TriggerResourceCtrl_cancel;
657 req->resultSetWanted = &false;
660 printf("Sent cancel request\n");
664 int send_scanrequest(char *string, int pp, int num)
666 Z_APDU *apdu = zget_APDU(out, Z_APDU_scanRequest);
667 Z_ScanRequest *req = apdu->u.scanRequest;
671 req->num_databaseNames = 1;
672 req->databaseNames = &db;
673 attset.proto = protocol;
674 attset.class = CLASS_ATTSET;
675 attset.value = VAL_BIB1;
676 req->attributeSet = oid_getoidbyent(&attset);
677 req->termListAndStartPoint = p_query_scan(out, string);
678 req->numberOfTermsRequested = #
679 req->preferredPositionInResponse = &pp;
684 void display_term(Z_TermInfo *t)
686 if (t->term->which == Z_Term_general)
688 printf("%.*s (%d)\n", t->term->u.general->len, t->term->u.general->buf,
689 t->globalOccurrences ? *t->globalOccurrences : -1);
690 sprintf(last_scan, "%.*s", t->term->u.general->len,
691 t->term->u.general->buf);
694 printf("Term type not general.\n");
697 void process_scanResponse(Z_ScanResponse *res)
701 printf("SCAN: %d entries, position=%d\n", *res->numberOfEntriesReturned,
702 *res->positionOfTerm);
703 if (*res->scanStatus != Z_Scan_success)
704 printf("Scan returned code %d\n", *res->scanStatus);
707 if (res->entries->which == Z_ListEntries_entries)
709 Z_Entries *ent = res->entries->u.entries;
711 for (i = 0; i < ent->num_entries; i++)
712 if (ent->entries[i]->which == Z_Entry_termInfo)
714 printf("%c ", i + 1 == *res->positionOfTerm ? '*' : ' ');
715 display_term(ent->entries[i]->u.termInfo);
718 display_diagrec(ent->entries[i]->u.surrogateDiagnostic);
721 display_diagrec(res->entries->u.nonSurrogateDiagnostics->diagRecs[0]);
724 int cmd_scan(char *arg)
728 printf("Session not initialized yet\n");
731 if (!ODR_MASK_GET(session->options, Z_Options_scan))
733 printf("Target doesn't support scan\n");
738 if (send_scanrequest(arg, 5, 20) < 0)
742 if (send_scanrequest(last_scan, 1, 20) < 0)
747 int cmd_format(char *arg)
751 printf("Usage: format <recordsyntax>\n");
754 if (!strcmp(arg, "sutrs"))
756 printf("Preferred format is SUTRS.\n");
757 recordsyntax = VAL_SUTRS;
760 else if (!strcmp(arg, "usmarc"))
762 printf("Preferred format is USMARC\n");
763 recordsyntax = VAL_USMARC;
766 else if (!strcmp(arg, "danmarc"))
768 printf("Preferred format is DANMARC\n");
769 recordsyntax = VAL_DANMARC;
772 else if (!strcmp(arg, "grs1"))
774 printf("Preferred format is GRS1\n");
775 recordsyntax = VAL_GRS1;
780 printf("Specify one of {sutrs,usmarc,danmarc,grs1}.\n");
785 static void initialize(void)
793 if (!(out = odr_createmem(ODR_ENCODE)) ||
794 !(in = odr_createmem(ODR_DECODE)) ||
795 !(print = odr_createmem(ODR_PRINT)))
797 fprintf(stderr, "failed to allocate ODR streams\n");
800 setvbuf(stdout, 0, _IONBF, 0);
804 bibset = ccl_qual_mk ();
805 inf = fopen ("default.bib", "r");
808 ccl_qual_file (bibset, inf);
815 static int client(void)
819 int (*fun)(char *arg);
822 {"open", cmd_open, "('tcp'|'osi')':'[<TSEL>'/']<HOST>[':'<PORT>]"},
823 {"quit", cmd_quit, ""},
824 {"find", cmd_find, "<CCL-QUERY>"},
825 {"base", cmd_base, "<BASE-NAME>"},
826 {"show", cmd_show, "<REC#>['+'<#RECS>]"},
827 {"scan", cmd_scan, "<TERM>"},
828 {"authentication", cmd_authentication, "<ACCTSTRING>"},
829 {"lslb", cmd_lslb, "<largeSetLowerBound>"},
830 {"ssub", cmd_ssub, "<smallSetUpperBound>"},
831 {"mspn", cmd_mspn, "<mediumSetPresentNumber>"},
832 {"status", cmd_status, ""},
833 {"setnames", cmd_setnames, ""},
834 {"cancel", cmd_cancel, ""},
835 {"format", cmd_format, "<recordsyntax>"},
839 int netbufferlen = 0;
847 char line[1024], word[1024], arg[1024];
852 FD_SET(cs_fileno(conn), &input);
853 if ((res = select(20, &input, 0, 0, 0)) < 0)
860 if (FD_ISSET(0, &input))
862 /* quick & dirty way to get a command line. */
865 if ((res = sscanf(line, "%s %[^;]", word, arg)) <= 0)
867 strcpy(word, last_cmd);
872 strcpy(last_cmd, word);
873 for (i = 0; cmd[i].cmd; i++)
874 if (!strncmp(cmd[i].cmd, word, strlen(word)))
876 res = (*cmd[i].fun)(arg);
879 if (!cmd[i].cmd) /* dump our help-screen */
881 printf("Unknown command: %s.\n", word);
882 printf("Currently recognized commands:\n");
883 for (i = 0; cmd[i].cmd; i++)
884 printf(" %s %s\n", cmd[i].cmd, cmd[i].ad);
890 if (conn && FD_ISSET(cs_fileno(conn), &input))
894 if ((res = cs_get(conn, &netbuffer, &netbufferlen)) < 0)
901 printf("Target closed connection.\n");
904 odr_reset(in); /* release APDU from last round */
905 odr_setbuf(in, netbuffer, res, 0);
906 if (!z_APDU(in, &apdu, 0))
908 odr_perror(in, "Decoding incoming APDU");
909 fprintf(stderr, "Packet dump:\n---------\n");
910 odr_dumpBER(stderr, netbuffer, res);
911 fprintf(stderr, "---------\n");
915 if (!z_APDU(print, &apdu, 0))
917 odr_perror(print, "Failed to print incoming APDU");
924 case Z_APDU_initResponse:
925 process_initResponse(apdu->u.initResponse);
927 case Z_APDU_searchResponse:
928 process_searchResponse(apdu->u.searchResponse);
930 case Z_APDU_scanResponse:
931 process_scanResponse(apdu->u.scanResponse);
933 case Z_APDU_presentResponse:
934 printf("Received presentResponse.\n");
936 *apdu->u.presentResponse->numberOfRecordsReturned;
937 if (apdu->u.presentResponse->records)
938 display_records(apdu->u.presentResponse->records);
940 printf("No records.\n");
943 printf("Received unknown APDU type (%d).\n",
950 while (cs_more(conn));
956 int main(int argc, char **argv)