2 * Copyright (c) 1995, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.10 1995-06-06 14:56:58 quinn
10 * Revision 1.9 1995/06/06 08:15:19 quinn
13 * Revision 1.8 1995/06/05 10:52:22 quinn
16 * Revision 1.7 1995/06/02 09:50:09 quinn
19 * Revision 1.6 1995/05/31 08:29:21 quinn
20 * Nothing significant.
22 * Revision 1.5 1995/05/29 08:10:47 quinn
23 * Moved oid.c to util.
25 * Revision 1.4 1995/05/22 15:30:13 adam
26 * Client uses prefix query notation.
28 * Revision 1.3 1995/05/22 15:06:53 quinn
29 * *** empty log message ***
31 * Revision 1.2 1995/05/22 14:56:40 quinn
32 * *** empty log message ***
34 * Revision 1.1 1995/05/22 11:30:31 quinn
35 * Added prettier client.
41 * This is the obligatory little toy client, whose primary purpose is
42 * to illustrate the use of the YAZ service-level API.
50 #include <sys/select.h>
71 #include "../version.h"
73 #define C_PROMPT "Z> "
75 static ODR out, in, print; /* encoding and decoding streams */
76 static COMSTACK conn = 0; /* our z-association */
77 static Z_IdAuthentication *auth = 0; /* our current auth definition */
78 static char database[512] = "Default"; /* Database name */
79 static int setnumber = 0; /* current result set number */
80 static int smallSetUpperBound = 0;
81 static int largeSetLowerBound = 1;
82 static int mediumSetPresentNumber = 0;
83 static int setno = 1; /* current set offset */
84 static int protocol = PROTO_Z3950; /* current app protocol */
85 static ODR_MEM session_mem; /* memory handle for init-response */
86 static Z_InitResponse *session = 0; /* session parameters */
87 static char last_scan[512] = "0";
88 static char last_cmd[100] = "?";
91 static CCL_bibset bibset; /* CCL bibset handle */
95 static void send_apdu(Z_APDU *a)
100 if (!z_APDU(out, &a, 0))
102 odr_perror(out, "Encoding APDU");
105 buf = odr_getbuf(out, &len, 0);
106 odr_reset(out); /* release the APDU */
107 if (cs_put(conn, buf, len) < 0)
109 fprintf(stderr, "cs_put: %s", cs_errlist[cs_errno(conn)]);
114 /* INIT SERVICE ------------------------------- */
116 static void send_initRequest()
118 Z_APDU *apdu = zget_APDU(out, Z_APDU_initRequest);
119 Z_InitRequest *req = apdu->u.initRequest;
121 ODR_MASK_SET(req->options, Z_Options_search);
122 ODR_MASK_SET(req->options, Z_Options_present);
123 ODR_MASK_SET(req->options, Z_Options_namedResultSets);
124 ODR_MASK_SET(req->options, Z_Options_triggerResourceCtrl);
125 ODR_MASK_SET(req->options, Z_Options_scan);
127 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_1);
128 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_2);
130 req->idAuthentication = auth;
133 printf("Sent initrequest.\n");
136 static int process_initResponse(Z_InitResponse *res)
138 /* save session parameters for later use */
139 session_mem = odr_extract_mem(in);
143 printf("Connection rejected by target.\n");
145 printf("Connection accepted by target.\n");
146 if (res->implementationId)
147 printf("ID : %s\n", res->implementationId);
148 if (res->implementationName)
149 printf("Name : %s\n", res->implementationName);
150 if (res->implementationVersion)
151 printf("Version: %s\n", res->implementationVersion);
152 if (res->userInformationField)
154 printf("UserInformationfield:\n");
155 if (!odr_external(print, (Odr_external**)&res-> userInformationField,
158 odr_perror(print, "Printing userinfo\n");
161 if (res->userInformationField->which == ODR_EXTERNAL_octet)
163 printf("Guessing visiblestring:\n");
164 printf("'%s'\n", res->userInformationField->u. octet_aligned->buf);
170 int cmd_open(char *arg)
173 char type[100], addr[100];
178 printf("Already connected.\n");
181 if (!*arg || sscanf(arg, "%[^:]:%s", type, addr) < 2)
183 fprintf(stderr, "Usage: open (osi|tcp) ':' [tsel '/']host[':'port]\n");
187 if (!strcmp(type, "osi"))
189 if (!(add = mosi_strtoaddr(addr)))
199 if (!strcmp(type, "tcp"))
201 if (!(add = tcpip_strtoaddr(addr)))
207 protocol = PROTO_Z3950;
211 fprintf(stderr, "Bad type: %s\n", type);
214 if (!(conn = cs_create(t, 1, protocol)))
219 printf("Connecting...");
221 if (cs_connect(conn, add) < 0)
233 int cmd_authentication(char *arg)
235 static Z_IdAuthentication au;
236 static char open[256];
240 printf("Auth field set to null\n");
245 au.which = Z_IdAuthentication_open;
251 /* SEARCH SERVICE ------------------------------ */
253 void display_record(Z_DatabaseRecord *p)
255 Odr_external *r = (Odr_external*) p;
257 if (r->direct_reference)
259 oident *ent = oid_getentbyoid(r->direct_reference);
261 printf("Record type: ");
263 printf("%s\n", ent->desc);
264 else if (!odr_oid(print, &r->direct_reference, 0))
266 odr_perror(print, "print oid");
271 if (r->which == ODR_EXTERNAL_octet && p->u.octet_aligned->len)
274 marc_display ((char*)p->u.octet_aligned->buf, stdout);
276 FILE *ofi = fopen("dump", "a");
278 fwrite(p->u.octet_aligned->buf, 1, p->u.octet_aligned->len, ofi);
280 printf("dumped record\n");
285 printf("Unknown record representation.\n");
286 if (!odr_external(print, &r, 0))
288 odr_perror(print, "Printing external");
295 static void display_diagrec(Z_DiagRec *p)
299 printf("Diagnostic message from database.\n");
300 if (!(ent = oid_getentbyoid(p->diagnosticSetId)) ||
301 ent->class != CLASS_DIAGSET || ent->value != VAL_BIB1)
302 printf("Missing or unknown diagset\n");
303 printf("Error condition: %d", *p->condition);
304 printf(" -- %s\n", p->addinfo ? p->addinfo : "");
307 static void display_nameplusrecord(Z_NamePlusRecord *p)
310 printf("[%s]", p->databaseName);
311 if (p->which == Z_NamePlusRecord_surrogateDiagnostic)
312 display_diagrec(p->u.surrogateDiagnostic);
314 display_record(p->u.databaseRecord);
317 static void display_records(Z_Records *p)
321 if (p->which == Z_Records_NSD)
322 display_diagrec(p->u.nonSurrogateDiagnostic);
325 printf("Records: %d\n", p->u.databaseOrSurDiagnostics->num_records);
326 for (i = 0; i < p->u.databaseOrSurDiagnostics->num_records; i++)
327 display_nameplusrecord(p->u.databaseOrSurDiagnostics->records[i]);
331 static int send_searchRequest(char *arg)
333 Z_APDU *apdu = zget_APDU(out, Z_APDU_searchRequest);
334 Z_SearchRequest *req = apdu->u.searchRequest;
335 char *databaseNames = database;
339 struct ccl_rpn_node *rpn;
345 Z_RPNQuery *RPNquery;
353 rpn = ccl_find_str(bibset, arg, &error, &pos);
356 printf("CCL ERROR: %s\n", ccl_err_msg(error));
362 if (!strcmp(arg, "@big")) /* strictly for troublemaking */
364 static unsigned char big[2100];
367 /* send a very big referenceid to test transport stack etc. */
368 memset(big, 'A', 2100);
369 bigo.len = bigo.size = 2100;
371 req->referenceId = &bigo;
376 sprintf(setstring, "%d", ++setnumber);
377 req->resultSetName = setstring;
379 *req->smallSetUpperBound = smallSetUpperBound;
380 *req->largeSetLowerBound = largeSetLowerBound;
381 *req->mediumSetPresentNumber = mediumSetPresentNumber;
382 req->num_databaseNames = 1;
383 req->databaseNames = &databaseNames;
388 query.which = Z_Query_type_1;
391 assert((RPNquery = ccl_rpn_query(rpn)));
393 RPNquery = p_query_rpn (out, arg);
396 printf("Prefix query error\n");
400 bib1.proto = protocol;
401 bib1.class = CLASS_ATTSET;
402 bib1.value = VAL_BIB1;
403 RPNquery->attributeSetId = oid_getoidbyent(&bib1);
404 query.u.type_1 = RPNquery;
406 query.which = Z_Query_type_2;
407 query.u.type_2 = &ccl_query;
408 ccl_query.buf = (unsigned char*) arg;
409 ccl_query.len = strlen(arg);
414 printf("Sent searchRequest.\n");
418 static int process_searchResponse(Z_SearchResponse *res)
420 if (*res->searchStatus)
421 printf("Search was a success.\n");
423 printf("Search was a bloomin' failure.\n");
424 printf("Number of hits: %d, setno %d\n",
425 *res->resultCount, setnumber);
426 printf("records returned: %d\n",
427 *res->numberOfRecordsReturned);
428 setno += *res->numberOfRecordsReturned;
430 display_records(res->records);
434 static int cmd_find(char *arg)
438 printf("Find what?\n");
443 printf("Not connected yet\n");
446 if (!send_searchRequest(arg))
451 static int cmd_ssub(char *arg)
453 if (!(smallSetUpperBound = atoi(arg)))
458 static int cmd_lslb(char *arg)
460 if (!(largeSetLowerBound = atoi(arg)))
465 static int cmd_mspn(char *arg)
467 if (!(mediumSetPresentNumber = atoi(arg)))
472 static int cmd_status(char *arg)
474 printf("smallSetUpperBound: %d\n", smallSetUpperBound);
475 printf("largeSetLowerBound: %d\n", largeSetLowerBound);
476 printf("mediumSetPresentNumber: %d\n", mediumSetPresentNumber);
480 static int cmd_base(char *arg)
484 printf("Usage: base <database>\n");
487 strcpy(database, arg);
491 static int cmd_setnames(char *arg)
495 printf("Set numbering enabled.\n");
500 printf("Set numbering disabled.\n");
506 /* PRESENT SERVICE ----------------------------- */
508 static int send_presentRequest(char *arg)
510 Z_APDU *apdu = zget_APDU(out, Z_APDU_presentRequest);
511 Z_PresentRequest *req = apdu->u.presentRequest;
516 if ((p = strchr(arg, '+')))
526 sprintf(setstring, "%d", setnumber);
527 req->resultSetId = setstring;
529 req->resultSetStartPoint = &setno;
530 req->numberOfRecordsRequested = &nos;
532 printf("Sent presentRequest (%d+%d).\n", setno, nos);
536 static int cmd_show(char *arg)
538 if (!send_presentRequest(arg))
543 int cmd_quit(char *arg)
545 printf("See you later, alligator.\n");
549 int cmd_cancel(char *arg)
551 Z_APDU *apdu = zget_APDU(out, Z_APDU_triggerResourceControlRequest);
552 Z_TriggerResourceControlRequest *req =
553 apdu->u.triggerResourceControlRequest;
558 printf("Session not initialized yet\n");
561 if (!ODR_MASK_GET(session->options, Z_Options_triggerResourceCtrl))
563 printf("Target doesn't support cancel (trigger resource ctrl)\n");
566 *req->requestedAction = Z_TriggerResourceCtrl_cancel;
567 req->resultSetWanted = &false;
570 printf("Sent cancel request\n");
574 int send_scanrequest(char *string, int pp, int num)
576 Z_APDU *apdu = zget_APDU(out, Z_APDU_scanRequest);
577 Z_ScanRequest *req = apdu->u.scanRequest;
581 req->num_databaseNames = 1;
582 req->databaseNames = &db;
583 attset.proto = protocol;
584 attset.class = CLASS_ATTSET;
585 attset.value = VAL_BIB1;
586 req->attributeSet = oid_getoidbyent(&attset);
587 req->termListAndStartPoint = p_query_scan(out, string);
588 req->numberOfTermsRequested = #
589 req->preferredPositionInResponse = &pp;
594 void display_term(Z_TermInfo *t)
596 if (t->term->which == Z_Term_general)
598 printf("%.*s (%d)\n", t->term->u.general->len, t->term->u.general->buf,
599 t->globalOccurrences ? *t->globalOccurrences : -1);
600 sprintf(last_scan, "%.*s", t->term->u.general->len,
601 t->term->u.general->buf);
604 printf("Term type not general.\n");
607 void process_scanResponse(Z_ScanResponse *res)
611 printf("SCAN: %d entries, position=%d\n", *res->numberOfEntriesReturned,
612 *res->positionOfTerm);
613 if (*res->scanStatus != Z_Scan_success)
614 printf("Scan returned code %d\n", *res->scanStatus);
617 if (res->entries->which == Z_ListEntries_entries)
619 Z_Entries *ent = res->entries->u.entries;
621 for (i = 0; i < ent->num_entries; i++)
622 if (ent->entries[i]->which == Z_Entry_termInfo)
624 printf("%c ", i + 1 == *res->positionOfTerm ? '*' : ' ');
625 display_term(ent->entries[i]->u.termInfo);
628 display_diagrec(ent->entries[i]->u.surrogateDiagnostic);
631 display_diagrec(res->entries->u.nonSurrogateDiagnostics->diagRecs[0]);
634 int cmd_scan(char *arg)
638 printf("Session not initialized yet\n");
641 if (!ODR_MASK_GET(session->options, Z_Options_scan))
643 printf("Target doesn't support scan\n");
648 if (send_scanrequest(arg, 5, 19) < 0)
652 if (send_scanrequest(last_scan, 1, 19) < 0)
657 static void initialize(void)
665 if (!(out = odr_createmem(ODR_ENCODE)) ||
666 !(in = odr_createmem(ODR_DECODE)) ||
667 !(print = odr_createmem(ODR_PRINT)))
669 fprintf(stderr, "failed to allocate ODR streams\n");
672 setvbuf(stdout, 0, _IONBF, 0);
676 bibset = ccl_qual_mk ();
677 inf = fopen ("default.bib", "r");
680 ccl_qual_file (bibset, inf);
687 static int client(void)
691 int (*fun)(char *arg);
694 {"open", cmd_open, "('tcp'|'osi')':'[<TSEL>'/']<HOST>[':'<PORT>]"},
695 {"quit", cmd_quit, ""},
696 {"find", cmd_find, "<CCL-QUERY>"},
697 {"base", cmd_base, "<BASE-NAME>"},
698 {"show", cmd_show, "<REC#>['+'<#RECS>]"},
699 {"scan", cmd_scan, "<TERM>"},
700 {"authentication", cmd_authentication, "<ACCTSTRING>"},
701 {"lslb", cmd_lslb, "<largeSetLowerBound>"},
702 {"ssub", cmd_ssub, "<smallSetUpperBound>"},
703 {"mspn", cmd_mspn, "<mediumSetPresentNumber>"},
704 {"status", cmd_status, ""},
705 {"setnames", cmd_setnames, ""},
706 {"cancel", cmd_cancel, ""},
710 int netbufferlen = 0;
718 char line[1024], word[1024], arg[1024];
723 FD_SET(cs_fileno(conn), &input);
724 if ((res = select(20, &input, 0, 0, 0)) < 0)
731 if (FD_ISSET(0, &input))
733 /* quick & dirty way to get a command line. */
736 if ((res = sscanf(line, "%s %[^;]", word, arg)) <= 0)
738 strcpy(word, last_cmd);
743 strcpy(last_cmd, word);
744 for (i = 0; cmd[i].cmd; i++)
745 if (!strncmp(cmd[i].cmd, word, strlen(word)))
747 res = (*cmd[i].fun)(arg);
750 if (!cmd[i].cmd) /* dump our help-screen */
752 printf("Unknown command: %s.\n", word);
753 printf("Currently recognized commands:\n");
754 for (i = 0; cmd[i].cmd; i++)
755 printf(" %s %s\n", cmd[i].cmd, cmd[i].ad);
761 if (conn && FD_ISSET(cs_fileno(conn), &input))
765 if ((res = cs_get(conn, &netbuffer, &netbufferlen)) < 0)
772 printf("Target closed connection.\n");
775 odr_reset(in); /* release APDU from last round */
776 odr_setbuf(in, netbuffer, res, 0);
777 if (!z_APDU(in, &apdu, 0))
779 odr_perror(in, "Decoding incoming APDU");
783 if (!z_APDU(print, &apdu, 0))
785 odr_perror(print, "Failed to print incoming APDU");
792 case Z_APDU_initResponse:
793 process_initResponse(apdu->u.initResponse);
795 case Z_APDU_searchResponse:
796 process_searchResponse(apdu->u.searchResponse);
798 case Z_APDU_scanResponse:
799 process_scanResponse(apdu->u.scanResponse);
801 case Z_APDU_presentResponse:
802 printf("Received presentResponse.\n");
804 *apdu->u.presentResponse->numberOfRecordsReturned;
805 if (apdu->u.presentResponse->records)
806 display_records(apdu->u.presentResponse->records);
808 printf("No records.\n");
811 printf("Received unknown APDU type (%d).\n",
818 while (cs_more(conn));
824 int main(int argc, char **argv)