2 * Copyright (c) 1995, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.13 1995-06-16 10:29:11 quinn
8 * *** empty log message ***
10 * Revision 1.12 1995/06/15 07:44:57 quinn
13 * Revision 1.11 1995/06/14 15:26:40 quinn
14 * *** empty log message ***
16 * Revision 1.10 1995/06/06 14:56:58 quinn
19 * Revision 1.9 1995/06/06 08:15:19 quinn
22 * Revision 1.8 1995/06/05 10:52:22 quinn
25 * Revision 1.7 1995/06/02 09:50:09 quinn
28 * Revision 1.6 1995/05/31 08:29:21 quinn
29 * Nothing significant.
31 * Revision 1.5 1995/05/29 08:10:47 quinn
32 * Moved oid.c to util.
34 * Revision 1.4 1995/05/22 15:30:13 adam
35 * Client uses prefix query notation.
37 * Revision 1.3 1995/05/22 15:06:53 quinn
38 * *** empty log message ***
40 * Revision 1.2 1995/05/22 14:56:40 quinn
41 * *** empty log message ***
43 * Revision 1.1 1995/05/22 11:30:31 quinn
44 * Added prettier client.
50 * This is the obligatory little toy client, whose primary purpose is
51 * to illustrate the use of the YAZ service-level API.
59 #include <sys/select.h>
79 #define C_PROMPT "Z> "
81 static ODR out, in, print; /* encoding and decoding streams */
82 static COMSTACK conn = 0; /* our z-association */
83 static Z_IdAuthentication *auth = 0; /* our current auth definition */
84 static char database[512] = "Default"; /* Database name */
85 static int setnumber = 0; /* current result set number */
86 static int smallSetUpperBound = 0;
87 static int largeSetLowerBound = 1;
88 static int mediumSetPresentNumber = 0;
89 static int setno = 1; /* current set offset */
90 static int protocol = PROTO_Z3950; /* current app protocol */
91 static ODR_MEM session_mem; /* memory handle for init-response */
92 static Z_InitResponse *session = 0; /* session parameters */
93 static char last_scan[512] = "0";
94 static char last_cmd[100] = "?";
97 static CCL_bibset bibset; /* CCL bibset handle */
101 static void send_apdu(Z_APDU *a)
106 if (!z_APDU(out, &a, 0))
108 odr_perror(out, "Encoding APDU");
111 buf = odr_getbuf(out, &len, 0);
112 odr_reset(out); /* release the APDU */
113 if (cs_put(conn, buf, len) < 0)
115 fprintf(stderr, "cs_put: %s", cs_errlist[cs_errno(conn)]);
120 /* INIT SERVICE ------------------------------- */
122 static void send_initRequest()
124 Z_APDU *apdu = zget_APDU(out, Z_APDU_initRequest);
125 Z_InitRequest *req = apdu->u.initRequest;
127 ODR_MASK_SET(req->options, Z_Options_search);
128 ODR_MASK_SET(req->options, Z_Options_present);
129 ODR_MASK_SET(req->options, Z_Options_namedResultSets);
130 ODR_MASK_SET(req->options, Z_Options_triggerResourceCtrl);
131 ODR_MASK_SET(req->options, Z_Options_scan);
133 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_1);
134 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_2);
136 req->idAuthentication = auth;
139 printf("Sent initrequest.\n");
142 static int process_initResponse(Z_InitResponse *res)
144 /* save session parameters for later use */
145 session_mem = odr_extract_mem(in);
149 printf("Connection rejected by target.\n");
151 printf("Connection accepted by target.\n");
152 if (res->implementationId)
153 printf("ID : %s\n", res->implementationId);
154 if (res->implementationName)
155 printf("Name : %s\n", res->implementationName);
156 if (res->implementationVersion)
157 printf("Version: %s\n", res->implementationVersion);
158 if (res->userInformationField)
160 printf("UserInformationfield:\n");
161 if (!odr_external(print, (Odr_external**)&res-> userInformationField,
164 odr_perror(print, "Printing userinfo\n");
167 if (res->userInformationField->which == ODR_EXTERNAL_octet)
169 printf("Guessing visiblestring:\n");
170 printf("'%s'\n", res->userInformationField->u. octet_aligned->buf);
176 int cmd_open(char *arg)
179 char type[100], addr[100];
184 printf("Already connected.\n");
187 if (!*arg || sscanf(arg, "%[^:]:%s", type, addr) < 2)
189 fprintf(stderr, "Usage: open (osi|tcp) ':' [tsel '/']host[':'port]\n");
193 if (!strcmp(type, "osi"))
195 if (!(add = mosi_strtoaddr(addr)))
205 if (!strcmp(type, "tcp"))
207 if (!(add = tcpip_strtoaddr(addr)))
213 protocol = PROTO_Z3950;
217 fprintf(stderr, "Bad type: %s\n", type);
220 if (!(conn = cs_create(t, 1, protocol)))
225 printf("Connecting...");
227 if (cs_connect(conn, add) < 0)
239 int cmd_authentication(char *arg)
241 static Z_IdAuthentication au;
242 static char open[256];
246 printf("Auth field set to null\n");
251 au.which = Z_IdAuthentication_open;
257 /* SEARCH SERVICE ------------------------------ */
259 void display_record(Z_DatabaseRecord *p)
261 Odr_external *r = (Odr_external*) p;
263 if (r->direct_reference)
265 oident *ent = oid_getentbyoid(r->direct_reference);
267 printf("Record type: ");
269 printf("%s\n", ent->desc);
270 else if (!odr_oid(print, &r->direct_reference, 0))
272 odr_perror(print, "print oid");
276 if (r->which == ODR_EXTERNAL_octet && p->u.octet_aligned->len)
278 marc_display ((char*)p->u.octet_aligned->buf, stdout);
282 printf("Unknown record representation.\n");
283 if (!odr_external(print, &r, 0))
285 odr_perror(print, "Printing external");
291 static void display_diagrec(Z_DiagRec *p)
295 Z_DefaultDiagFormat *r;
300 printf("Diagnostic message from database.\n");
302 if (p->which != Z_DiagRec_defaultFormat)
304 printf("Diagnostic record not in default format.\n");
308 r = p->u.defaultFormat;
310 if (!(ent = oid_getentbyoid(r->diagnosticSetId)) ||
311 ent->class != CLASS_DIAGSET || ent->value != VAL_BIB1)
312 printf("Missing or unknown diagset\n");
313 printf("Error condition: %d", *r->condition);
314 printf(" -- %s\n", r->addinfo ? r->addinfo : "");
317 static void display_nameplusrecord(Z_NamePlusRecord *p)
320 printf("[%s]", p->databaseName);
321 if (p->which == Z_NamePlusRecord_surrogateDiagnostic)
322 display_diagrec(p->u.surrogateDiagnostic);
324 display_record(p->u.databaseRecord);
327 static void display_records(Z_Records *p)
331 if (p->which == Z_Records_NSD)
332 display_diagrec(p->u.nonSurrogateDiagnostic);
335 printf("Records: %d\n", p->u.databaseOrSurDiagnostics->num_records);
336 for (i = 0; i < p->u.databaseOrSurDiagnostics->num_records; i++)
337 display_nameplusrecord(p->u.databaseOrSurDiagnostics->records[i]);
341 static int send_searchRequest(char *arg)
343 Z_APDU *apdu = zget_APDU(out, Z_APDU_searchRequest);
344 Z_SearchRequest *req = apdu->u.searchRequest;
345 char *databaseNames = database;
349 struct ccl_rpn_node *rpn;
355 Z_RPNQuery *RPNquery;
363 rpn = ccl_find_str(bibset, arg, &error, &pos);
366 printf("CCL ERROR: %s\n", ccl_err_msg(error));
372 if (!strcmp(arg, "@big")) /* strictly for troublemaking */
374 static unsigned char big[2100];
377 /* send a very big referenceid to test transport stack etc. */
378 memset(big, 'A', 2100);
379 bigo.len = bigo.size = 2100;
381 req->referenceId = &bigo;
386 sprintf(setstring, "%d", ++setnumber);
387 req->resultSetName = setstring;
389 *req->smallSetUpperBound = smallSetUpperBound;
390 *req->largeSetLowerBound = largeSetLowerBound;
391 *req->mediumSetPresentNumber = mediumSetPresentNumber;
392 req->num_databaseNames = 1;
393 req->databaseNames = &databaseNames;
398 query.which = Z_Query_type_1;
401 assert((RPNquery = ccl_rpn_query(rpn)));
403 RPNquery = p_query_rpn (out, arg);
406 printf("Prefix query error\n");
410 bib1.proto = protocol;
411 bib1.class = CLASS_ATTSET;
412 bib1.value = VAL_BIB1;
413 RPNquery->attributeSetId = oid_getoidbyent(&bib1);
414 query.u.type_1 = RPNquery;
416 query.which = Z_Query_type_2;
417 query.u.type_2 = &ccl_query;
418 ccl_query.buf = (unsigned char*) arg;
419 ccl_query.len = strlen(arg);
424 printf("Sent searchRequest.\n");
428 static int process_searchResponse(Z_SearchResponse *res)
430 if (*res->searchStatus)
431 printf("Search was a success.\n");
433 printf("Search was a bloomin' failure.\n");
434 printf("Number of hits: %d, setno %d\n",
435 *res->resultCount, setnumber);
436 printf("records returned: %d\n",
437 *res->numberOfRecordsReturned);
438 setno += *res->numberOfRecordsReturned;
440 display_records(res->records);
444 static int cmd_find(char *arg)
448 printf("Find what?\n");
453 printf("Not connected yet\n");
456 if (!send_searchRequest(arg))
461 static int cmd_ssub(char *arg)
463 if (!(smallSetUpperBound = atoi(arg)))
468 static int cmd_lslb(char *arg)
470 if (!(largeSetLowerBound = atoi(arg)))
475 static int cmd_mspn(char *arg)
477 if (!(mediumSetPresentNumber = atoi(arg)))
482 static int cmd_status(char *arg)
484 printf("smallSetUpperBound: %d\n", smallSetUpperBound);
485 printf("largeSetLowerBound: %d\n", largeSetLowerBound);
486 printf("mediumSetPresentNumber: %d\n", mediumSetPresentNumber);
490 static int cmd_base(char *arg)
494 printf("Usage: base <database>\n");
497 strcpy(database, arg);
501 static int cmd_setnames(char *arg)
505 printf("Set numbering enabled.\n");
510 printf("Set numbering disabled.\n");
516 /* PRESENT SERVICE ----------------------------- */
518 static int send_presentRequest(char *arg)
520 Z_APDU *apdu = zget_APDU(out, Z_APDU_presentRequest);
521 Z_PresentRequest *req = apdu->u.presentRequest;
526 if ((p = strchr(arg, '+')))
536 sprintf(setstring, "%d", setnumber);
537 req->resultSetId = setstring;
539 req->resultSetStartPoint = &setno;
540 req->numberOfRecordsRequested = &nos;
542 printf("Sent presentRequest (%d+%d).\n", setno, nos);
546 static int cmd_show(char *arg)
548 if (!send_presentRequest(arg))
553 int cmd_quit(char *arg)
555 printf("See you later, alligator.\n");
559 int cmd_cancel(char *arg)
561 Z_APDU *apdu = zget_APDU(out, Z_APDU_triggerResourceControlRequest);
562 Z_TriggerResourceControlRequest *req =
563 apdu->u.triggerResourceControlRequest;
568 printf("Session not initialized yet\n");
571 if (!ODR_MASK_GET(session->options, Z_Options_triggerResourceCtrl))
573 printf("Target doesn't support cancel (trigger resource ctrl)\n");
576 *req->requestedAction = Z_TriggerResourceCtrl_cancel;
577 req->resultSetWanted = &false;
580 printf("Sent cancel request\n");
584 int send_scanrequest(char *string, int pp, int num)
586 Z_APDU *apdu = zget_APDU(out, Z_APDU_scanRequest);
587 Z_ScanRequest *req = apdu->u.scanRequest;
591 req->num_databaseNames = 1;
592 req->databaseNames = &db;
593 attset.proto = protocol;
594 attset.class = CLASS_ATTSET;
595 attset.value = VAL_BIB1;
596 req->attributeSet = oid_getoidbyent(&attset);
597 req->termListAndStartPoint = p_query_scan(out, string);
598 req->numberOfTermsRequested = #
599 req->preferredPositionInResponse = &pp;
604 void display_term(Z_TermInfo *t)
606 if (t->term->which == Z_Term_general)
608 printf("%.*s (%d)\n", t->term->u.general->len, t->term->u.general->buf,
609 t->globalOccurrences ? *t->globalOccurrences : -1);
610 sprintf(last_scan, "%.*s", t->term->u.general->len,
611 t->term->u.general->buf);
614 printf("Term type not general.\n");
617 void process_scanResponse(Z_ScanResponse *res)
621 printf("SCAN: %d entries, position=%d\n", *res->numberOfEntriesReturned,
622 *res->positionOfTerm);
623 if (*res->scanStatus != Z_Scan_success)
624 printf("Scan returned code %d\n", *res->scanStatus);
627 if (res->entries->which == Z_ListEntries_entries)
629 Z_Entries *ent = res->entries->u.entries;
631 for (i = 0; i < ent->num_entries; i++)
632 if (ent->entries[i]->which == Z_Entry_termInfo)
634 printf("%c ", i + 1 == *res->positionOfTerm ? '*' : ' ');
635 display_term(ent->entries[i]->u.termInfo);
638 display_diagrec(ent->entries[i]->u.surrogateDiagnostic);
641 display_diagrec(res->entries->u.nonSurrogateDiagnostics->diagRecs[0]);
644 int cmd_scan(char *arg)
648 printf("Session not initialized yet\n");
651 if (!ODR_MASK_GET(session->options, Z_Options_scan))
653 printf("Target doesn't support scan\n");
658 if (send_scanrequest(arg, 5, 20) < 0)
662 if (send_scanrequest(last_scan, 1, 20) < 0)
667 static void initialize(void)
675 if (!(out = odr_createmem(ODR_ENCODE)) ||
676 !(in = odr_createmem(ODR_DECODE)) ||
677 !(print = odr_createmem(ODR_PRINT)))
679 fprintf(stderr, "failed to allocate ODR streams\n");
682 setvbuf(stdout, 0, _IONBF, 0);
686 bibset = ccl_qual_mk ();
687 inf = fopen ("default.bib", "r");
690 ccl_qual_file (bibset, inf);
697 static int client(void)
701 int (*fun)(char *arg);
704 {"open", cmd_open, "('tcp'|'osi')':'[<TSEL>'/']<HOST>[':'<PORT>]"},
705 {"quit", cmd_quit, ""},
706 {"find", cmd_find, "<CCL-QUERY>"},
707 {"base", cmd_base, "<BASE-NAME>"},
708 {"show", cmd_show, "<REC#>['+'<#RECS>]"},
709 {"scan", cmd_scan, "<TERM>"},
710 {"authentication", cmd_authentication, "<ACCTSTRING>"},
711 {"lslb", cmd_lslb, "<largeSetLowerBound>"},
712 {"ssub", cmd_ssub, "<smallSetUpperBound>"},
713 {"mspn", cmd_mspn, "<mediumSetPresentNumber>"},
714 {"status", cmd_status, ""},
715 {"setnames", cmd_setnames, ""},
716 {"cancel", cmd_cancel, ""},
720 int netbufferlen = 0;
728 char line[1024], word[1024], arg[1024];
733 FD_SET(cs_fileno(conn), &input);
734 if ((res = select(20, &input, 0, 0, 0)) < 0)
741 if (FD_ISSET(0, &input))
743 /* quick & dirty way to get a command line. */
746 if ((res = sscanf(line, "%s %[^;]", word, arg)) <= 0)
748 strcpy(word, last_cmd);
753 strcpy(last_cmd, word);
754 for (i = 0; cmd[i].cmd; i++)
755 if (!strncmp(cmd[i].cmd, word, strlen(word)))
757 res = (*cmd[i].fun)(arg);
760 if (!cmd[i].cmd) /* dump our help-screen */
762 printf("Unknown command: %s.\n", word);
763 printf("Currently recognized commands:\n");
764 for (i = 0; cmd[i].cmd; i++)
765 printf(" %s %s\n", cmd[i].cmd, cmd[i].ad);
771 if (conn && FD_ISSET(cs_fileno(conn), &input))
775 if ((res = cs_get(conn, &netbuffer, &netbufferlen)) < 0)
782 printf("Target closed connection.\n");
785 odr_reset(in); /* release APDU from last round */
786 odr_setbuf(in, netbuffer, res, 0);
787 if (!z_APDU(in, &apdu, 0))
789 odr_perror(in, "Decoding incoming APDU");
793 if (!z_APDU(print, &apdu, 0))
795 odr_perror(print, "Failed to print incoming APDU");
802 case Z_APDU_initResponse:
803 process_initResponse(apdu->u.initResponse);
805 case Z_APDU_searchResponse:
806 process_searchResponse(apdu->u.searchResponse);
808 case Z_APDU_scanResponse:
809 process_scanResponse(apdu->u.scanResponse);
811 case Z_APDU_presentResponse:
812 printf("Received presentResponse.\n");
814 *apdu->u.presentResponse->numberOfRecordsReturned;
815 if (apdu->u.presentResponse->records)
816 display_records(apdu->u.presentResponse->records);
818 printf("No records.\n");
821 printf("Received unknown APDU type (%d).\n",
828 while (cs_more(conn));
834 int main(int argc, char **argv)