2 * Copyright (c) 1995, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.12 1995-06-15 07:44:57 quinn
10 * Revision 1.11 1995/06/14 15:26:40 quinn
11 * *** empty log message ***
13 * Revision 1.10 1995/06/06 14:56:58 quinn
16 * Revision 1.9 1995/06/06 08:15:19 quinn
19 * Revision 1.8 1995/06/05 10:52:22 quinn
22 * Revision 1.7 1995/06/02 09:50:09 quinn
25 * Revision 1.6 1995/05/31 08:29:21 quinn
26 * Nothing significant.
28 * Revision 1.5 1995/05/29 08:10:47 quinn
29 * Moved oid.c to util.
31 * Revision 1.4 1995/05/22 15:30:13 adam
32 * Client uses prefix query notation.
34 * Revision 1.3 1995/05/22 15:06:53 quinn
35 * *** empty log message ***
37 * Revision 1.2 1995/05/22 14:56:40 quinn
38 * *** empty log message ***
40 * Revision 1.1 1995/05/22 11:30:31 quinn
41 * Added prettier client.
47 * This is the obligatory little toy client, whose primary purpose is
48 * to illustrate the use of the YAZ service-level API.
56 #include <sys/select.h>
77 #define C_PROMPT "Z> "
79 static ODR out, in, print; /* encoding and decoding streams */
80 static COMSTACK conn = 0; /* our z-association */
81 static Z_IdAuthentication *auth = 0; /* our current auth definition */
82 static char database[512] = "Default"; /* Database name */
83 static int setnumber = 0; /* current result set number */
84 static int smallSetUpperBound = 0;
85 static int largeSetLowerBound = 1;
86 static int mediumSetPresentNumber = 0;
87 static int setno = 1; /* current set offset */
88 static int protocol = PROTO_Z3950; /* current app protocol */
89 static ODR_MEM session_mem; /* memory handle for init-response */
90 static Z_InitResponse *session = 0; /* session parameters */
91 static char last_scan[512] = "0";
92 static char last_cmd[100] = "?";
95 static CCL_bibset bibset; /* CCL bibset handle */
99 static void send_apdu(Z_APDU *a)
104 if (!z_APDU(out, &a, 0))
106 odr_perror(out, "Encoding APDU");
109 buf = odr_getbuf(out, &len, 0);
110 odr_reset(out); /* release the APDU */
111 if (cs_put(conn, buf, len) < 0)
113 fprintf(stderr, "cs_put: %s", cs_errlist[cs_errno(conn)]);
118 /* INIT SERVICE ------------------------------- */
120 static void send_initRequest()
122 Z_APDU *apdu = zget_APDU(out, Z_APDU_initRequest);
123 Z_InitRequest *req = apdu->u.initRequest;
125 ODR_MASK_SET(req->options, Z_Options_search);
126 ODR_MASK_SET(req->options, Z_Options_present);
127 ODR_MASK_SET(req->options, Z_Options_namedResultSets);
128 ODR_MASK_SET(req->options, Z_Options_triggerResourceCtrl);
129 ODR_MASK_SET(req->options, Z_Options_scan);
131 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_1);
132 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_2);
134 req->idAuthentication = auth;
137 printf("Sent initrequest.\n");
140 static int process_initResponse(Z_InitResponse *res)
142 /* save session parameters for later use */
143 session_mem = odr_extract_mem(in);
147 printf("Connection rejected by target.\n");
149 printf("Connection accepted by target.\n");
150 if (res->implementationId)
151 printf("ID : %s\n", res->implementationId);
152 if (res->implementationName)
153 printf("Name : %s\n", res->implementationName);
154 if (res->implementationVersion)
155 printf("Version: %s\n", res->implementationVersion);
156 if (res->userInformationField)
158 printf("UserInformationfield:\n");
159 if (!odr_external(print, (Odr_external**)&res-> userInformationField,
162 odr_perror(print, "Printing userinfo\n");
165 if (res->userInformationField->which == ODR_EXTERNAL_octet)
167 printf("Guessing visiblestring:\n");
168 printf("'%s'\n", res->userInformationField->u. octet_aligned->buf);
174 int cmd_open(char *arg)
177 char type[100], addr[100];
182 printf("Already connected.\n");
185 if (!*arg || sscanf(arg, "%[^:]:%s", type, addr) < 2)
187 fprintf(stderr, "Usage: open (osi|tcp) ':' [tsel '/']host[':'port]\n");
191 if (!strcmp(type, "osi"))
193 if (!(add = mosi_strtoaddr(addr)))
203 if (!strcmp(type, "tcp"))
205 if (!(add = tcpip_strtoaddr(addr)))
211 protocol = PROTO_Z3950;
215 fprintf(stderr, "Bad type: %s\n", type);
218 if (!(conn = cs_create(t, 1, protocol)))
223 printf("Connecting...");
225 if (cs_connect(conn, add) < 0)
237 int cmd_authentication(char *arg)
239 static Z_IdAuthentication au;
240 static char open[256];
244 printf("Auth field set to null\n");
249 au.which = Z_IdAuthentication_open;
255 /* SEARCH SERVICE ------------------------------ */
257 void display_record(Z_DatabaseRecord *p)
259 Odr_external *r = (Odr_external*) p;
261 if (r->direct_reference)
263 oident *ent = oid_getentbyoid(r->direct_reference);
265 printf("Record type: ");
267 printf("%s\n", ent->desc);
268 else if (!odr_oid(print, &r->direct_reference, 0))
270 odr_perror(print, "print oid");
275 if (r->which == ODR_EXTERNAL_octet && p->u.octet_aligned->len)
278 marc_display ((char*)p->u.octet_aligned->buf, stdout);
280 FILE *ofi = fopen("dump", "a");
282 fwrite(p->u.octet_aligned->buf, 1, p->u.octet_aligned->len, ofi);
284 printf("dumped record\n");
289 printf("Unknown record representation.\n");
290 if (!odr_external(print, &r, 0))
292 odr_perror(print, "Printing external");
299 static void display_diagrec(Z_DiagRec *p)
303 Z_DefaultDiagFormat *r;
308 printf("Diagnostic message from database.\n");
310 if (p->which != Z_DiagRec_defaultFormat)
312 printf("Diagnostic record not in default format.\n");
316 r = p->u.defaultFormat;
318 if (!(ent = oid_getentbyoid(r->diagnosticSetId)) ||
319 ent->class != CLASS_DIAGSET || ent->value != VAL_BIB1)
320 printf("Missing or unknown diagset\n");
321 printf("Error condition: %d", *r->condition);
322 printf(" -- %s\n", r->addinfo ? r->addinfo : "");
325 static void display_nameplusrecord(Z_NamePlusRecord *p)
328 printf("[%s]", p->databaseName);
329 if (p->which == Z_NamePlusRecord_surrogateDiagnostic)
330 display_diagrec(p->u.surrogateDiagnostic);
332 display_record(p->u.databaseRecord);
335 static void display_records(Z_Records *p)
339 if (p->which == Z_Records_NSD)
340 display_diagrec(p->u.nonSurrogateDiagnostic);
343 printf("Records: %d\n", p->u.databaseOrSurDiagnostics->num_records);
344 for (i = 0; i < p->u.databaseOrSurDiagnostics->num_records; i++)
345 display_nameplusrecord(p->u.databaseOrSurDiagnostics->records[i]);
349 static int send_searchRequest(char *arg)
351 Z_APDU *apdu = zget_APDU(out, Z_APDU_searchRequest);
352 Z_SearchRequest *req = apdu->u.searchRequest;
353 char *databaseNames = database;
357 struct ccl_rpn_node *rpn;
363 Z_RPNQuery *RPNquery;
371 rpn = ccl_find_str(bibset, arg, &error, &pos);
374 printf("CCL ERROR: %s\n", ccl_err_msg(error));
380 if (!strcmp(arg, "@big")) /* strictly for troublemaking */
382 static unsigned char big[2100];
385 /* send a very big referenceid to test transport stack etc. */
386 memset(big, 'A', 2100);
387 bigo.len = bigo.size = 2100;
389 req->referenceId = &bigo;
394 sprintf(setstring, "%d", ++setnumber);
395 req->resultSetName = setstring;
397 *req->smallSetUpperBound = smallSetUpperBound;
398 *req->largeSetLowerBound = largeSetLowerBound;
399 *req->mediumSetPresentNumber = mediumSetPresentNumber;
400 req->num_databaseNames = 1;
401 req->databaseNames = &databaseNames;
406 query.which = Z_Query_type_1;
409 assert((RPNquery = ccl_rpn_query(rpn)));
411 RPNquery = p_query_rpn (out, arg);
414 printf("Prefix query error\n");
418 bib1.proto = protocol;
419 bib1.class = CLASS_ATTSET;
420 bib1.value = VAL_BIB1;
421 RPNquery->attributeSetId = oid_getoidbyent(&bib1);
422 query.u.type_1 = RPNquery;
424 query.which = Z_Query_type_2;
425 query.u.type_2 = &ccl_query;
426 ccl_query.buf = (unsigned char*) arg;
427 ccl_query.len = strlen(arg);
432 printf("Sent searchRequest.\n");
436 static int process_searchResponse(Z_SearchResponse *res)
438 if (*res->searchStatus)
439 printf("Search was a success.\n");
441 printf("Search was a bloomin' failure.\n");
442 printf("Number of hits: %d, setno %d\n",
443 *res->resultCount, setnumber);
444 printf("records returned: %d\n",
445 *res->numberOfRecordsReturned);
446 setno += *res->numberOfRecordsReturned;
448 display_records(res->records);
452 static int cmd_find(char *arg)
456 printf("Find what?\n");
461 printf("Not connected yet\n");
464 if (!send_searchRequest(arg))
469 static int cmd_ssub(char *arg)
471 if (!(smallSetUpperBound = atoi(arg)))
476 static int cmd_lslb(char *arg)
478 if (!(largeSetLowerBound = atoi(arg)))
483 static int cmd_mspn(char *arg)
485 if (!(mediumSetPresentNumber = atoi(arg)))
490 static int cmd_status(char *arg)
492 printf("smallSetUpperBound: %d\n", smallSetUpperBound);
493 printf("largeSetLowerBound: %d\n", largeSetLowerBound);
494 printf("mediumSetPresentNumber: %d\n", mediumSetPresentNumber);
498 static int cmd_base(char *arg)
502 printf("Usage: base <database>\n");
505 strcpy(database, arg);
509 static int cmd_setnames(char *arg)
513 printf("Set numbering enabled.\n");
518 printf("Set numbering disabled.\n");
524 /* PRESENT SERVICE ----------------------------- */
526 static int send_presentRequest(char *arg)
528 Z_APDU *apdu = zget_APDU(out, Z_APDU_presentRequest);
529 Z_PresentRequest *req = apdu->u.presentRequest;
534 if ((p = strchr(arg, '+')))
544 sprintf(setstring, "%d", setnumber);
545 req->resultSetId = setstring;
547 req->resultSetStartPoint = &setno;
548 req->numberOfRecordsRequested = &nos;
550 printf("Sent presentRequest (%d+%d).\n", setno, nos);
554 static int cmd_show(char *arg)
556 if (!send_presentRequest(arg))
561 int cmd_quit(char *arg)
563 printf("See you later, alligator.\n");
567 int cmd_cancel(char *arg)
569 Z_APDU *apdu = zget_APDU(out, Z_APDU_triggerResourceControlRequest);
570 Z_TriggerResourceControlRequest *req =
571 apdu->u.triggerResourceControlRequest;
576 printf("Session not initialized yet\n");
579 if (!ODR_MASK_GET(session->options, Z_Options_triggerResourceCtrl))
581 printf("Target doesn't support cancel (trigger resource ctrl)\n");
584 *req->requestedAction = Z_TriggerResourceCtrl_cancel;
585 req->resultSetWanted = &false;
588 printf("Sent cancel request\n");
592 int send_scanrequest(char *string, int pp, int num)
594 Z_APDU *apdu = zget_APDU(out, Z_APDU_scanRequest);
595 Z_ScanRequest *req = apdu->u.scanRequest;
599 req->num_databaseNames = 1;
600 req->databaseNames = &db;
601 attset.proto = protocol;
602 attset.class = CLASS_ATTSET;
603 attset.value = VAL_BIB1;
604 req->attributeSet = oid_getoidbyent(&attset);
605 req->termListAndStartPoint = p_query_scan(out, string);
606 req->numberOfTermsRequested = #
607 req->preferredPositionInResponse = &pp;
612 void display_term(Z_TermInfo *t)
614 if (t->term->which == Z_Term_general)
616 printf("%.*s (%d)\n", t->term->u.general->len, t->term->u.general->buf,
617 t->globalOccurrences ? *t->globalOccurrences : -1);
618 sprintf(last_scan, "%.*s", t->term->u.general->len,
619 t->term->u.general->buf);
622 printf("Term type not general.\n");
625 void process_scanResponse(Z_ScanResponse *res)
629 printf("SCAN: %d entries, position=%d\n", *res->numberOfEntriesReturned,
630 *res->positionOfTerm);
631 if (*res->scanStatus != Z_Scan_success)
632 printf("Scan returned code %d\n", *res->scanStatus);
635 if (res->entries->which == Z_ListEntries_entries)
637 Z_Entries *ent = res->entries->u.entries;
639 for (i = 0; i < ent->num_entries; i++)
640 if (ent->entries[i]->which == Z_Entry_termInfo)
642 printf("%c ", i + 1 == *res->positionOfTerm ? '*' : ' ');
643 display_term(ent->entries[i]->u.termInfo);
646 display_diagrec(ent->entries[i]->u.surrogateDiagnostic);
649 display_diagrec(res->entries->u.nonSurrogateDiagnostics->diagRecs[0]);
652 int cmd_scan(char *arg)
656 printf("Session not initialized yet\n");
659 if (!ODR_MASK_GET(session->options, Z_Options_scan))
661 printf("Target doesn't support scan\n");
666 if (send_scanrequest(arg, 5, 20) < 0)
670 if (send_scanrequest(last_scan, 1, 20) < 0)
675 static void initialize(void)
683 if (!(out = odr_createmem(ODR_ENCODE)) ||
684 !(in = odr_createmem(ODR_DECODE)) ||
685 !(print = odr_createmem(ODR_PRINT)))
687 fprintf(stderr, "failed to allocate ODR streams\n");
690 setvbuf(stdout, 0, _IONBF, 0);
694 bibset = ccl_qual_mk ();
695 inf = fopen ("default.bib", "r");
698 ccl_qual_file (bibset, inf);
705 static int client(void)
709 int (*fun)(char *arg);
712 {"open", cmd_open, "('tcp'|'osi')':'[<TSEL>'/']<HOST>[':'<PORT>]"},
713 {"quit", cmd_quit, ""},
714 {"find", cmd_find, "<CCL-QUERY>"},
715 {"base", cmd_base, "<BASE-NAME>"},
716 {"show", cmd_show, "<REC#>['+'<#RECS>]"},
717 {"scan", cmd_scan, "<TERM>"},
718 {"authentication", cmd_authentication, "<ACCTSTRING>"},
719 {"lslb", cmd_lslb, "<largeSetLowerBound>"},
720 {"ssub", cmd_ssub, "<smallSetUpperBound>"},
721 {"mspn", cmd_mspn, "<mediumSetPresentNumber>"},
722 {"status", cmd_status, ""},
723 {"setnames", cmd_setnames, ""},
724 {"cancel", cmd_cancel, ""},
728 int netbufferlen = 0;
736 char line[1024], word[1024], arg[1024];
741 FD_SET(cs_fileno(conn), &input);
742 if ((res = select(20, &input, 0, 0, 0)) < 0)
749 if (FD_ISSET(0, &input))
751 /* quick & dirty way to get a command line. */
754 if ((res = sscanf(line, "%s %[^;]", word, arg)) <= 0)
756 strcpy(word, last_cmd);
761 strcpy(last_cmd, word);
762 for (i = 0; cmd[i].cmd; i++)
763 if (!strncmp(cmd[i].cmd, word, strlen(word)))
765 res = (*cmd[i].fun)(arg);
768 if (!cmd[i].cmd) /* dump our help-screen */
770 printf("Unknown command: %s.\n", word);
771 printf("Currently recognized commands:\n");
772 for (i = 0; cmd[i].cmd; i++)
773 printf(" %s %s\n", cmd[i].cmd, cmd[i].ad);
779 if (conn && FD_ISSET(cs_fileno(conn), &input))
783 if ((res = cs_get(conn, &netbuffer, &netbufferlen)) < 0)
790 printf("Target closed connection.\n");
793 odr_reset(in); /* release APDU from last round */
794 odr_setbuf(in, netbuffer, res, 0);
795 if (!z_APDU(in, &apdu, 0))
797 odr_perror(in, "Decoding incoming APDU");
801 if (!z_APDU(print, &apdu, 0))
803 odr_perror(print, "Failed to print incoming APDU");
810 case Z_APDU_initResponse:
811 process_initResponse(apdu->u.initResponse);
813 case Z_APDU_searchResponse:
814 process_searchResponse(apdu->u.searchResponse);
816 case Z_APDU_scanResponse:
817 process_scanResponse(apdu->u.scanResponse);
819 case Z_APDU_presentResponse:
820 printf("Received presentResponse.\n");
822 *apdu->u.presentResponse->numberOfRecordsReturned;
823 if (apdu->u.presentResponse->records)
824 display_records(apdu->u.presentResponse->records);
826 printf("No records.\n");
829 printf("Received unknown APDU type (%d).\n",
836 while (cs_more(conn));
842 int main(int argc, char **argv)