2 * Copyright (c) 1995, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.11 1995-06-14 15:26:40 quinn
8 * *** empty log message ***
10 * Revision 1.10 1995/06/06 14:56:58 quinn
13 * Revision 1.9 1995/06/06 08:15:19 quinn
16 * Revision 1.8 1995/06/05 10:52:22 quinn
19 * Revision 1.7 1995/06/02 09:50:09 quinn
22 * Revision 1.6 1995/05/31 08:29:21 quinn
23 * Nothing significant.
25 * Revision 1.5 1995/05/29 08:10:47 quinn
26 * Moved oid.c to util.
28 * Revision 1.4 1995/05/22 15:30:13 adam
29 * Client uses prefix query notation.
31 * Revision 1.3 1995/05/22 15:06:53 quinn
32 * *** empty log message ***
34 * Revision 1.2 1995/05/22 14:56:40 quinn
35 * *** empty log message ***
37 * Revision 1.1 1995/05/22 11:30:31 quinn
38 * Added prettier client.
44 * This is the obligatory little toy client, whose primary purpose is
45 * to illustrate the use of the YAZ service-level API.
53 #include <sys/select.h>
74 #define C_PROMPT "Z> "
76 static ODR out, in, print; /* encoding and decoding streams */
77 static COMSTACK conn = 0; /* our z-association */
78 static Z_IdAuthentication *auth = 0; /* our current auth definition */
79 static char database[512] = "Default"; /* Database name */
80 static int setnumber = 0; /* current result set number */
81 static int smallSetUpperBound = 0;
82 static int largeSetLowerBound = 1;
83 static int mediumSetPresentNumber = 0;
84 static int setno = 1; /* current set offset */
85 static int protocol = PROTO_Z3950; /* current app protocol */
86 static ODR_MEM session_mem; /* memory handle for init-response */
87 static Z_InitResponse *session = 0; /* session parameters */
88 static char last_scan[512] = "0";
89 static char last_cmd[100] = "?";
92 static CCL_bibset bibset; /* CCL bibset handle */
96 static void send_apdu(Z_APDU *a)
101 if (!z_APDU(out, &a, 0))
103 odr_perror(out, "Encoding APDU");
106 buf = odr_getbuf(out, &len, 0);
107 odr_reset(out); /* release the APDU */
108 if (cs_put(conn, buf, len) < 0)
110 fprintf(stderr, "cs_put: %s", cs_errlist[cs_errno(conn)]);
115 /* INIT SERVICE ------------------------------- */
117 static void send_initRequest()
119 Z_APDU *apdu = zget_APDU(out, Z_APDU_initRequest);
120 Z_InitRequest *req = apdu->u.initRequest;
122 ODR_MASK_SET(req->options, Z_Options_search);
123 ODR_MASK_SET(req->options, Z_Options_present);
124 ODR_MASK_SET(req->options, Z_Options_namedResultSets);
125 ODR_MASK_SET(req->options, Z_Options_triggerResourceCtrl);
126 ODR_MASK_SET(req->options, Z_Options_scan);
128 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_1);
129 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_2);
131 req->idAuthentication = auth;
134 printf("Sent initrequest.\n");
137 static int process_initResponse(Z_InitResponse *res)
139 /* save session parameters for later use */
140 session_mem = odr_extract_mem(in);
144 printf("Connection rejected by target.\n");
146 printf("Connection accepted by target.\n");
147 if (res->implementationId)
148 printf("ID : %s\n", res->implementationId);
149 if (res->implementationName)
150 printf("Name : %s\n", res->implementationName);
151 if (res->implementationVersion)
152 printf("Version: %s\n", res->implementationVersion);
153 if (res->userInformationField)
155 printf("UserInformationfield:\n");
156 if (!odr_external(print, (Odr_external**)&res-> userInformationField,
159 odr_perror(print, "Printing userinfo\n");
162 if (res->userInformationField->which == ODR_EXTERNAL_octet)
164 printf("Guessing visiblestring:\n");
165 printf("'%s'\n", res->userInformationField->u. octet_aligned->buf);
171 int cmd_open(char *arg)
174 char type[100], addr[100];
179 printf("Already connected.\n");
182 if (!*arg || sscanf(arg, "%[^:]:%s", type, addr) < 2)
184 fprintf(stderr, "Usage: open (osi|tcp) ':' [tsel '/']host[':'port]\n");
188 if (!strcmp(type, "osi"))
190 if (!(add = mosi_strtoaddr(addr)))
200 if (!strcmp(type, "tcp"))
202 if (!(add = tcpip_strtoaddr(addr)))
208 protocol = PROTO_Z3950;
212 fprintf(stderr, "Bad type: %s\n", type);
215 if (!(conn = cs_create(t, 1, protocol)))
220 printf("Connecting...");
222 if (cs_connect(conn, add) < 0)
234 int cmd_authentication(char *arg)
236 static Z_IdAuthentication au;
237 static char open[256];
241 printf("Auth field set to null\n");
246 au.which = Z_IdAuthentication_open;
252 /* SEARCH SERVICE ------------------------------ */
254 void display_record(Z_DatabaseRecord *p)
256 Odr_external *r = (Odr_external*) p;
258 if (r->direct_reference)
260 oident *ent = oid_getentbyoid(r->direct_reference);
262 printf("Record type: ");
264 printf("%s\n", ent->desc);
265 else if (!odr_oid(print, &r->direct_reference, 0))
267 odr_perror(print, "print oid");
272 if (r->which == ODR_EXTERNAL_octet && p->u.octet_aligned->len)
275 marc_display ((char*)p->u.octet_aligned->buf, stdout);
277 FILE *ofi = fopen("dump", "a");
279 fwrite(p->u.octet_aligned->buf, 1, p->u.octet_aligned->len, ofi);
281 printf("dumped record\n");
286 printf("Unknown record representation.\n");
287 if (!odr_external(print, &r, 0))
289 odr_perror(print, "Printing external");
296 static void display_diagrec(Z_DiagRec *p)
300 printf("Diagnostic message from database.\n");
301 if (!(ent = oid_getentbyoid(p->diagnosticSetId)) ||
302 ent->class != CLASS_DIAGSET || ent->value != VAL_BIB1)
303 printf("Missing or unknown diagset\n");
304 printf("Error condition: %d", *p->condition);
305 printf(" -- %s\n", p->addinfo ? p->addinfo : "");
308 static void display_nameplusrecord(Z_NamePlusRecord *p)
311 printf("[%s]", p->databaseName);
312 if (p->which == Z_NamePlusRecord_surrogateDiagnostic)
313 display_diagrec(p->u.surrogateDiagnostic);
315 display_record(p->u.databaseRecord);
318 static void display_records(Z_Records *p)
322 if (p->which == Z_Records_NSD)
323 display_diagrec(p->u.nonSurrogateDiagnostic);
326 printf("Records: %d\n", p->u.databaseOrSurDiagnostics->num_records);
327 for (i = 0; i < p->u.databaseOrSurDiagnostics->num_records; i++)
328 display_nameplusrecord(p->u.databaseOrSurDiagnostics->records[i]);
332 static int send_searchRequest(char *arg)
334 Z_APDU *apdu = zget_APDU(out, Z_APDU_searchRequest);
335 Z_SearchRequest *req = apdu->u.searchRequest;
336 char *databaseNames = database;
340 struct ccl_rpn_node *rpn;
346 Z_RPNQuery *RPNquery;
354 rpn = ccl_find_str(bibset, arg, &error, &pos);
357 printf("CCL ERROR: %s\n", ccl_err_msg(error));
363 if (!strcmp(arg, "@big")) /* strictly for troublemaking */
365 static unsigned char big[2100];
368 /* send a very big referenceid to test transport stack etc. */
369 memset(big, 'A', 2100);
370 bigo.len = bigo.size = 2100;
372 req->referenceId = &bigo;
377 sprintf(setstring, "%d", ++setnumber);
378 req->resultSetName = setstring;
380 *req->smallSetUpperBound = smallSetUpperBound;
381 *req->largeSetLowerBound = largeSetLowerBound;
382 *req->mediumSetPresentNumber = mediumSetPresentNumber;
383 req->num_databaseNames = 1;
384 req->databaseNames = &databaseNames;
389 query.which = Z_Query_type_1;
392 assert((RPNquery = ccl_rpn_query(rpn)));
394 RPNquery = p_query_rpn (out, arg);
397 printf("Prefix query error\n");
401 bib1.proto = protocol;
402 bib1.class = CLASS_ATTSET;
403 bib1.value = VAL_BIB1;
404 RPNquery->attributeSetId = oid_getoidbyent(&bib1);
405 query.u.type_1 = RPNquery;
407 query.which = Z_Query_type_2;
408 query.u.type_2 = &ccl_query;
409 ccl_query.buf = (unsigned char*) arg;
410 ccl_query.len = strlen(arg);
415 printf("Sent searchRequest.\n");
419 static int process_searchResponse(Z_SearchResponse *res)
421 if (*res->searchStatus)
422 printf("Search was a success.\n");
424 printf("Search was a bloomin' failure.\n");
425 printf("Number of hits: %d, setno %d\n",
426 *res->resultCount, setnumber);
427 printf("records returned: %d\n",
428 *res->numberOfRecordsReturned);
429 setno += *res->numberOfRecordsReturned;
431 display_records(res->records);
435 static int cmd_find(char *arg)
439 printf("Find what?\n");
444 printf("Not connected yet\n");
447 if (!send_searchRequest(arg))
452 static int cmd_ssub(char *arg)
454 if (!(smallSetUpperBound = atoi(arg)))
459 static int cmd_lslb(char *arg)
461 if (!(largeSetLowerBound = atoi(arg)))
466 static int cmd_mspn(char *arg)
468 if (!(mediumSetPresentNumber = atoi(arg)))
473 static int cmd_status(char *arg)
475 printf("smallSetUpperBound: %d\n", smallSetUpperBound);
476 printf("largeSetLowerBound: %d\n", largeSetLowerBound);
477 printf("mediumSetPresentNumber: %d\n", mediumSetPresentNumber);
481 static int cmd_base(char *arg)
485 printf("Usage: base <database>\n");
488 strcpy(database, arg);
492 static int cmd_setnames(char *arg)
496 printf("Set numbering enabled.\n");
501 printf("Set numbering disabled.\n");
507 /* PRESENT SERVICE ----------------------------- */
509 static int send_presentRequest(char *arg)
511 Z_APDU *apdu = zget_APDU(out, Z_APDU_presentRequest);
512 Z_PresentRequest *req = apdu->u.presentRequest;
517 if ((p = strchr(arg, '+')))
527 sprintf(setstring, "%d", setnumber);
528 req->resultSetId = setstring;
530 req->resultSetStartPoint = &setno;
531 req->numberOfRecordsRequested = &nos;
533 printf("Sent presentRequest (%d+%d).\n", setno, nos);
537 static int cmd_show(char *arg)
539 if (!send_presentRequest(arg))
544 int cmd_quit(char *arg)
546 printf("See you later, alligator.\n");
550 int cmd_cancel(char *arg)
552 Z_APDU *apdu = zget_APDU(out, Z_APDU_triggerResourceControlRequest);
553 Z_TriggerResourceControlRequest *req =
554 apdu->u.triggerResourceControlRequest;
559 printf("Session not initialized yet\n");
562 if (!ODR_MASK_GET(session->options, Z_Options_triggerResourceCtrl))
564 printf("Target doesn't support cancel (trigger resource ctrl)\n");
567 *req->requestedAction = Z_TriggerResourceCtrl_cancel;
568 req->resultSetWanted = &false;
571 printf("Sent cancel request\n");
575 int send_scanrequest(char *string, int pp, int num)
577 Z_APDU *apdu = zget_APDU(out, Z_APDU_scanRequest);
578 Z_ScanRequest *req = apdu->u.scanRequest;
582 req->num_databaseNames = 1;
583 req->databaseNames = &db;
584 attset.proto = protocol;
585 attset.class = CLASS_ATTSET;
586 attset.value = VAL_BIB1;
587 req->attributeSet = oid_getoidbyent(&attset);
588 req->termListAndStartPoint = p_query_scan(out, string);
589 req->numberOfTermsRequested = #
590 req->preferredPositionInResponse = &pp;
595 void display_term(Z_TermInfo *t)
597 if (t->term->which == Z_Term_general)
599 printf("%.*s (%d)\n", t->term->u.general->len, t->term->u.general->buf,
600 t->globalOccurrences ? *t->globalOccurrences : -1);
601 sprintf(last_scan, "%.*s", t->term->u.general->len,
602 t->term->u.general->buf);
605 printf("Term type not general.\n");
608 void process_scanResponse(Z_ScanResponse *res)
612 printf("SCAN: %d entries, position=%d\n", *res->numberOfEntriesReturned,
613 *res->positionOfTerm);
614 if (*res->scanStatus != Z_Scan_success)
615 printf("Scan returned code %d\n", *res->scanStatus);
618 if (res->entries->which == Z_ListEntries_entries)
620 Z_Entries *ent = res->entries->u.entries;
622 for (i = 0; i < ent->num_entries; i++)
623 if (ent->entries[i]->which == Z_Entry_termInfo)
625 printf("%c ", i + 1 == *res->positionOfTerm ? '*' : ' ');
626 display_term(ent->entries[i]->u.termInfo);
629 display_diagrec(ent->entries[i]->u.surrogateDiagnostic);
632 display_diagrec(res->entries->u.nonSurrogateDiagnostics->diagRecs[0]);
635 int cmd_scan(char *arg)
639 printf("Session not initialized yet\n");
642 if (!ODR_MASK_GET(session->options, Z_Options_scan))
644 printf("Target doesn't support scan\n");
649 if (send_scanrequest(arg, 5, 19) < 0)
653 if (send_scanrequest(last_scan, 1, 19) < 0)
658 static void initialize(void)
666 if (!(out = odr_createmem(ODR_ENCODE)) ||
667 !(in = odr_createmem(ODR_DECODE)) ||
668 !(print = odr_createmem(ODR_PRINT)))
670 fprintf(stderr, "failed to allocate ODR streams\n");
673 setvbuf(stdout, 0, _IONBF, 0);
677 bibset = ccl_qual_mk ();
678 inf = fopen ("default.bib", "r");
681 ccl_qual_file (bibset, inf);
688 static int client(void)
692 int (*fun)(char *arg);
695 {"open", cmd_open, "('tcp'|'osi')':'[<TSEL>'/']<HOST>[':'<PORT>]"},
696 {"quit", cmd_quit, ""},
697 {"find", cmd_find, "<CCL-QUERY>"},
698 {"base", cmd_base, "<BASE-NAME>"},
699 {"show", cmd_show, "<REC#>['+'<#RECS>]"},
700 {"scan", cmd_scan, "<TERM>"},
701 {"authentication", cmd_authentication, "<ACCTSTRING>"},
702 {"lslb", cmd_lslb, "<largeSetLowerBound>"},
703 {"ssub", cmd_ssub, "<smallSetUpperBound>"},
704 {"mspn", cmd_mspn, "<mediumSetPresentNumber>"},
705 {"status", cmd_status, ""},
706 {"setnames", cmd_setnames, ""},
707 {"cancel", cmd_cancel, ""},
711 int netbufferlen = 0;
719 char line[1024], word[1024], arg[1024];
724 FD_SET(cs_fileno(conn), &input);
725 if ((res = select(20, &input, 0, 0, 0)) < 0)
732 if (FD_ISSET(0, &input))
734 /* quick & dirty way to get a command line. */
737 if ((res = sscanf(line, "%s %[^;]", word, arg)) <= 0)
739 strcpy(word, last_cmd);
744 strcpy(last_cmd, word);
745 for (i = 0; cmd[i].cmd; i++)
746 if (!strncmp(cmd[i].cmd, word, strlen(word)))
748 res = (*cmd[i].fun)(arg);
751 if (!cmd[i].cmd) /* dump our help-screen */
753 printf("Unknown command: %s.\n", word);
754 printf("Currently recognized commands:\n");
755 for (i = 0; cmd[i].cmd; i++)
756 printf(" %s %s\n", cmd[i].cmd, cmd[i].ad);
762 if (conn && FD_ISSET(cs_fileno(conn), &input))
766 if ((res = cs_get(conn, &netbuffer, &netbufferlen)) < 0)
773 printf("Target closed connection.\n");
776 odr_reset(in); /* release APDU from last round */
777 odr_setbuf(in, netbuffer, res, 0);
778 if (!z_APDU(in, &apdu, 0))
780 odr_perror(in, "Decoding incoming APDU");
784 if (!z_APDU(print, &apdu, 0))
786 odr_perror(print, "Failed to print incoming APDU");
793 case Z_APDU_initResponse:
794 process_initResponse(apdu->u.initResponse);
796 case Z_APDU_searchResponse:
797 process_searchResponse(apdu->u.searchResponse);
799 case Z_APDU_scanResponse:
800 process_scanResponse(apdu->u.scanResponse);
802 case Z_APDU_presentResponse:
803 printf("Received presentResponse.\n");
805 *apdu->u.presentResponse->numberOfRecordsReturned;
806 if (apdu->u.presentResponse->records)
807 display_records(apdu->u.presentResponse->records);
809 printf("No records.\n");
812 printf("Received unknown APDU type (%d).\n",
819 while (cs_more(conn));
825 int main(int argc, char **argv)