2 * Copyright (c) 1995, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.9 1995-06-06 08:15:19 quinn
10 * Revision 1.8 1995/06/05 10:52:22 quinn
13 * Revision 1.7 1995/06/02 09:50:09 quinn
16 * Revision 1.6 1995/05/31 08:29:21 quinn
17 * Nothing significant.
19 * Revision 1.5 1995/05/29 08:10:47 quinn
20 * Moved oid.c to util.
22 * Revision 1.4 1995/05/22 15:30:13 adam
23 * Client uses prefix query notation.
25 * Revision 1.3 1995/05/22 15:06:53 quinn
26 * *** empty log message ***
28 * Revision 1.2 1995/05/22 14:56:40 quinn
29 * *** empty log message ***
31 * Revision 1.1 1995/05/22 11:30:31 quinn
32 * Added prettier client.
38 * This is the obligatory little toy client, whose primary purpose is
39 * to illustrate the use of the YAZ service-level API.
47 #include <sys/select.h>
68 #include "../version.h"
70 #define C_PROMPT "Z> "
72 static ODR out, in, print; /* encoding and decoding streams */
73 static COMSTACK conn = 0; /* our z-association */
74 static Z_IdAuthentication *auth = 0; /* our current auth definition */
75 static char database[512] = "Default"; /* Database name */
76 static int setnumber = 0; /* current result set number */
77 static int smallSetUpperBound = 0;
78 static int largeSetLowerBound = 1;
79 static int mediumSetPresentNumber = 0;
80 static int setno = 1; /* current set offset */
81 static int protocol = PROTO_Z3950; /* current app protocol */
82 static ODR_MEM session_mem; /* memory handle for init-response */
83 static Z_InitResponse *session = 0; /* session parameters */
84 static char last_scan[512] = "0";
85 static char last_cmd[100] = "?";
88 static CCL_bibset bibset; /* CCL bibset handle */
92 static void send_apdu(Z_APDU *a)
97 if (!z_APDU(out, &a, 0))
99 odr_perror(out, "Encoding APDU");
102 buf = odr_getbuf(out, &len, 0);
103 odr_reset(out); /* release the APDU */
104 if (cs_put(conn, buf, len) < 0)
106 fprintf(stderr, "cs_put: %s", cs_errlist[cs_errno(conn)]);
111 /* INIT SERVICE ------------------------------- */
113 static void send_initRequest()
115 Z_APDU *apdu = zget_APDU(out, Z_APDU_initRequest);
116 Z_InitRequest *req = apdu->u.initRequest;
118 ODR_MASK_SET(req->options, Z_Options_search);
119 ODR_MASK_SET(req->options, Z_Options_present);
120 ODR_MASK_SET(req->options, Z_Options_namedResultSets);
121 ODR_MASK_SET(req->options, Z_Options_triggerResourceCtrl);
122 ODR_MASK_SET(req->options, Z_Options_scan);
124 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_1);
125 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_2);
127 req->idAuthentication = auth;
130 printf("Sent initrequest.\n");
133 static int process_initResponse(Z_InitResponse *res)
135 /* save session parameters for later use */
136 session_mem = odr_extract_mem(in);
140 printf("Connection rejected by target.\n");
142 printf("Connection accepted by target.\n");
143 if (res->implementationId)
144 printf("ID : %s\n", res->implementationId);
145 if (res->implementationName)
146 printf("Name : %s\n", res->implementationName);
147 if (res->implementationVersion)
148 printf("Version: %s\n", res->implementationVersion);
149 if (res->userInformationField)
151 printf("UserInformationfield:\n");
152 if (!odr_external(print, (Odr_external**)&res-> userInformationField,
155 odr_perror(print, "Printing userinfo\n");
158 if (res->userInformationField->which == ODR_EXTERNAL_octet)
160 printf("Guessing visiblestring:\n");
161 printf("'%s'\n", res->userInformationField->u. octet_aligned->buf);
167 int cmd_open(char *arg)
170 char type[100], addr[100];
175 printf("Already connected.\n");
178 if (!*arg || sscanf(arg, "%[^:]:%s", type, addr) < 2)
180 fprintf(stderr, "Usage: open (osi|tcp) ':' [tsel '/']host[':'port]\n");
184 if (!strcmp(type, "osi"))
186 if (!(add = mosi_strtoaddr(addr)))
196 if (!strcmp(type, "tcp"))
198 if (!(add = tcpip_strtoaddr(addr)))
204 protocol = PROTO_Z3950;
208 fprintf(stderr, "Bad type: %s\n", type);
211 if (!(conn = cs_create(t, 1, protocol)))
216 printf("Connecting...");
218 if (cs_connect(conn, add) < 0)
230 int cmd_authentication(char *arg)
232 static Z_IdAuthentication au;
233 static char open[256];
237 printf("Auth field set to null\n");
242 au.which = Z_IdAuthentication_open;
248 /* SEARCH SERVICE ------------------------------ */
250 void display_record(Z_DatabaseRecord *p)
252 Odr_external *r = (Odr_external*) p;
254 if (r->direct_reference)
256 oident *ent = oid_getentbyoid(r->direct_reference);
258 printf("Record type: ");
260 printf("%s\n", ent->desc);
261 else if (!odr_oid(print, &r->direct_reference, 0))
263 odr_perror(print, "print oid");
268 if (r->which == ODR_EXTERNAL_octet && p->u.octet_aligned->len)
271 marc_display ((char*)p->u.octet_aligned->buf, stdout);
273 FILE *ofi = fopen("dump", "a");
275 fwrite(p->u.octet_aligned->buf, 1, p->u.octet_aligned->len, ofi);
277 printf("dumped record\n");
282 printf("Unknown record representation.\n");
283 if (!odr_external(print, &r, 0))
285 odr_perror(print, "Printing external");
292 static void display_diagrec(Z_DiagRec *p)
296 printf("Diagnostic message from database.\n");
297 if (!(ent = oid_getentbyoid(p->diagnosticSetId)) ||
298 ent->class != CLASS_DIAGSET || ent->value != VAL_BIB1)
299 printf("Missing or unknown diagset\n");
300 printf("Error condition: %d", *p->condition);
301 printf(" -- %s\n", p->addinfo ? p->addinfo : "");
304 static void display_nameplusrecord(Z_NamePlusRecord *p)
307 printf("[%s]", p->databaseName);
308 if (p->which == Z_NamePlusRecord_surrogateDiagnostic)
309 display_diagrec(p->u.surrogateDiagnostic);
311 display_record(p->u.databaseRecord);
314 static void display_records(Z_Records *p)
318 if (p->which == Z_Records_NSD)
319 display_diagrec(p->u.nonSurrogateDiagnostic);
322 printf("Records: %d\n", p->u.databaseOrSurDiagnostics->num_records);
323 for (i = 0; i < p->u.databaseOrSurDiagnostics->num_records; i++)
324 display_nameplusrecord(p->u.databaseOrSurDiagnostics->records[i]);
328 static int send_searchRequest(char *arg)
330 Z_APDU *apdu = zget_APDU(out, Z_APDU_searchRequest);
331 Z_SearchRequest *req = apdu->u.searchRequest;
332 char *databaseNames = database;
336 struct ccl_rpn_node *rpn;
342 Z_RPNQuery *RPNquery;
350 rpn = ccl_find_str(bibset, arg, &error, &pos);
353 printf("CCL ERROR: %s\n", ccl_err_msg(error));
359 if (!strcmp(arg, "@big")) /* strictly for troublemaking */
361 static unsigned char big[2100];
364 /* send a very big referenceid to test transport stack etc. */
365 memset(big, 'A', 2100);
366 bigo.len = bigo.size = 2100;
368 req->referenceId = &bigo;
373 sprintf(setstring, "%d", ++setnumber);
374 req->resultSetName = setstring;
376 *req->smallSetUpperBound = smallSetUpperBound;
377 *req->largeSetLowerBound = largeSetLowerBound;
378 *req->mediumSetPresentNumber = mediumSetPresentNumber;
379 req->num_databaseNames = 1;
380 req->databaseNames = &databaseNames;
385 query.which = Z_Query_type_1;
388 assert((RPNquery = ccl_rpn_query(rpn)));
390 RPNquery = p_query_rpn (out, arg);
393 printf("Prefix query error\n");
397 bib1.proto = protocol;
398 bib1.class = CLASS_ATTSET;
399 bib1.value = VAL_BIB1;
400 RPNquery->attributeSetId = oid_getoidbyent(&bib1);
401 query.u.type_1 = RPNquery;
403 query.which = Z_Query_type_2;
404 query.u.type_2 = &ccl_query;
405 ccl_query.buf = (unsigned char*) arg;
406 ccl_query.len = strlen(arg);
411 printf("Sent searchRequest.\n");
415 static int process_searchResponse(Z_SearchResponse *res)
417 if (res->searchStatus)
418 printf("Search was a success.\n");
420 printf("Search was a bloomin' failure.\n");
421 printf("Number of hits: %d, setno %d\n",
422 *res->resultCount, setnumber);
423 printf("records returned: %d\n",
424 *res->numberOfRecordsReturned);
425 setno += *res->numberOfRecordsReturned;
427 display_records(res->records);
431 static int cmd_find(char *arg)
435 printf("Find what?\n");
440 printf("Not connected yet\n");
443 if (!send_searchRequest(arg))
448 static int cmd_ssub(char *arg)
450 if (!(smallSetUpperBound = atoi(arg)))
455 static int cmd_lslb(char *arg)
457 if (!(largeSetLowerBound = atoi(arg)))
462 static int cmd_mspn(char *arg)
464 if (!(mediumSetPresentNumber = atoi(arg)))
469 static int cmd_status(char *arg)
471 printf("smallSetUpperBound: %d\n", smallSetUpperBound);
472 printf("largeSetLowerBound: %d\n", largeSetLowerBound);
473 printf("mediumSetPresentNumber: %d\n", mediumSetPresentNumber);
477 static int cmd_base(char *arg)
481 printf("Usage: base <database>\n");
484 strcpy(database, arg);
488 static int cmd_setnames(char *arg)
492 printf("Set numbering enabled.\n");
497 printf("Set numbering disabled.\n");
503 /* PRESENT SERVICE ----------------------------- */
505 static int send_presentRequest(char *arg)
507 Z_APDU *apdu = zget_APDU(out, Z_APDU_presentRequest);
508 Z_PresentRequest *req = apdu->u.presentRequest;
513 if ((p = strchr(arg, '+')))
523 sprintf(setstring, "%d", setnumber);
524 req->resultSetId = setstring;
526 req->resultSetStartPoint = &setno;
527 req->numberOfRecordsRequested = &nos;
529 printf("Sent presentRequest (%d+%d).\n", setno, nos);
533 static int cmd_show(char *arg)
535 if (!send_presentRequest(arg))
540 int cmd_quit(char *arg)
542 printf("See you later, alligator.\n");
546 int cmd_cancel(char *arg)
548 Z_APDU *apdu = zget_APDU(out, Z_APDU_triggerResourceControlRequest);
549 Z_TriggerResourceControlRequest *req =
550 apdu->u.triggerResourceControlRequest;
555 printf("Session not initialized yet\n");
558 if (!ODR_MASK_GET(session->options, Z_Options_triggerResourceCtrl))
560 printf("Target doesn't support cancel (trigger resource ctrl)\n");
563 *req->requestedAction = Z_TriggerResourceCtrl_cancel;
564 req->resultSetWanted = &false;
567 printf("Sent cancel request\n");
571 int send_scanrequest(char *string, int pp, int num)
573 Z_APDU *apdu = zget_APDU(out, Z_APDU_scanRequest);
574 Z_ScanRequest *req = apdu->u.scanRequest;
578 req->num_databaseNames = 1;
579 req->databaseNames = &db;
580 attset.proto = protocol;
581 attset.class = CLASS_ATTSET;
582 attset.value = VAL_BIB1;
583 req->attributeSet = oid_getoidbyent(&attset);
584 req->termListAndStartPoint = p_query_scan(out, string);
585 req->numberOfTermsRequested = #
586 req->preferredPositionInResponse = &pp;
591 void display_term(Z_TermInfo *t)
593 if (t->term->which == Z_Term_general)
595 printf("%.*s (%d)\n", t->term->u.general->len, t->term->u.general->buf,
596 t->globalOccurrences ? *t->globalOccurrences : -1);
597 sprintf(last_scan, "%.*s", t->term->u.general->len,
598 t->term->u.general->buf);
601 printf("Term type not general.\n");
604 void process_scanResponse(Z_ScanResponse *res)
608 printf("SCAN: %d entries, position=%d\n", *res->numberOfEntriesReturned,
609 *res->positionOfTerm);
610 if (*res->scanStatus != Z_Scan_success)
611 printf("Scan returned code %d\n", *res->scanStatus);
614 if (res->entries->which == Z_ListEntries_entries)
616 Z_Entries *ent = res->entries->u.entries;
618 for (i = 0; i < ent->num_entries; i++)
619 if (ent->entries[i]->which == Z_Entry_termInfo)
621 printf("%c ", i + 1 == *res->positionOfTerm ? '*' : ' ');
622 display_term(ent->entries[i]->u.termInfo);
625 display_diagrec(ent->entries[i]->u.surrogateDiagnostic);
628 display_diagrec(res->entries->u.nonSurrogateDiagnostics->diagRecs[0]);
631 int cmd_scan(char *arg)
635 printf("Session not initialized yet\n");
638 if (!ODR_MASK_GET(session->options, Z_Options_scan))
640 printf("Target doesn't support scan\n");
645 if (send_scanrequest(arg, 5, 19) < 0)
649 if (send_scanrequest(last_scan, 1, 19) < 0)
654 static void initialize(void)
662 if (!(out = odr_createmem(ODR_ENCODE)) ||
663 !(in = odr_createmem(ODR_DECODE)) ||
664 !(print = odr_createmem(ODR_PRINT)))
666 fprintf(stderr, "failed to allocate ODR streams\n");
669 setvbuf(stdout, 0, _IONBF, 0);
673 bibset = ccl_qual_mk ();
674 inf = fopen ("default.bib", "r");
677 ccl_qual_file (bibset, inf);
684 static int client(void)
688 int (*fun)(char *arg);
691 {"open", cmd_open, "('tcp'|'osi')':'[<TSEL>'/']<HOST>[':'<PORT>]"},
692 {"quit", cmd_quit, ""},
693 {"find", cmd_find, "<CCL-QUERY>"},
694 {"base", cmd_base, "<BASE-NAME>"},
695 {"show", cmd_show, "<REC#>['+'<#RECS>]"},
696 {"scan", cmd_scan, "<TERM>"},
697 {"authentication", cmd_authentication, "<ACCTSTRING>"},
698 {"lslb", cmd_lslb, "<largeSetLowerBound>"},
699 {"ssub", cmd_ssub, "<smallSetUpperBound>"},
700 {"mspn", cmd_mspn, "<mediumSetPresentNumber>"},
701 {"status", cmd_status, ""},
702 {"setnames", cmd_setnames, ""},
703 {"cancel", cmd_cancel, ""},
707 int netbufferlen = 0;
715 char line[1024], word[1024], arg[1024];
720 FD_SET(cs_fileno(conn), &input);
721 if ((res = select(20, &input, 0, 0, 0)) < 0)
728 if (FD_ISSET(0, &input))
730 /* quick & dirty way to get a command line. */
733 if ((res = sscanf(line, "%s %[^;]", word, arg)) <= 0)
735 strcpy(word, last_cmd);
740 strcpy(last_cmd, word);
741 for (i = 0; cmd[i].cmd; i++)
742 if (!strncmp(cmd[i].cmd, word, strlen(word)))
744 res = (*cmd[i].fun)(arg);
747 if (!cmd[i].cmd) /* dump our help-screen */
749 printf("Unknown command: %s.\n", word);
750 printf("Currently recognized commands:\n");
751 for (i = 0; cmd[i].cmd; i++)
752 printf(" %s %s\n", cmd[i].cmd, cmd[i].ad);
758 if (conn && FD_ISSET(cs_fileno(conn), &input))
762 if ((res = cs_get(conn, &netbuffer, &netbufferlen)) < 0)
769 printf("Target closed connection.\n");
772 odr_reset(in); /* release APDU from last round */
773 odr_setbuf(in, netbuffer, res, 0);
774 if (!z_APDU(in, &apdu, 0))
776 odr_perror(in, "Decoding incoming APDU");
780 if (!z_APDU(print, &apdu, 0))
782 odr_perror(print, "Failed to print incoming APDU");
789 case Z_APDU_initResponse:
790 process_initResponse(apdu->u.initResponse);
792 case Z_APDU_searchResponse:
793 process_searchResponse(apdu->u.searchResponse);
795 case Z_APDU_scanResponse:
796 process_scanResponse(apdu->u.scanResponse);
798 case Z_APDU_presentResponse:
799 printf("Received presentResponse.\n");
801 *apdu->u.presentResponse->numberOfRecordsReturned;
802 if (apdu->u.presentResponse->records)
803 display_records(apdu->u.presentResponse->records);
805 printf("No records.\n");
808 printf("Received unknown APDU type (%d).\n",
815 while (cs_more(conn));
821 int main(int argc, char **argv)