2 * Copyright (c) 1995, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.4 1995-05-22 15:30:13 adam
8 * Client uses prefix query notation.
10 * Revision 1.3 1995/05/22 15:06:53 quinn
11 * *** empty log message ***
13 * Revision 1.2 1995/05/22 14:56:40 quinn
14 * *** empty log message ***
16 * Revision 1.1 1995/05/22 11:30:31 quinn
17 * Added prettier client.
23 * This is the obligatory little toy client, whose primary purpose is
24 * to illustrate the use of the YAZ service-level API.
51 #include "../version.h"
53 #define C_PROMPT "Z> "
55 static ODR out, in, print; /* encoding and decoding streams */
56 static COMSTACK conn = 0; /* our z-association */
57 static Z_IdAuthentication *auth = 0; /* our current auth definition */
58 static char database[512] = "Default"; /* Database name */
59 static int setnumber = 0; /* current result set number */
60 static int smallSetUpperBound = 0;
61 static int largeSetLowerBound = 1;
62 static int mediumSetPresentNumber = 0;
63 static int setno = 1; /* current set offset */
64 static int protocol = PROTO_Z3950; /* current app protocol */
67 static CCL_bibset bibset; /* CCL bibset handle */
71 static void send_apdu(Z_APDU *a)
76 if (!z_APDU(out, &a, 0))
78 odr_perror(out, "Encoding APDU");
81 buf = odr_getbuf(out, &len, 0);
82 odr_reset(out); /* release the APDU */
83 if (cs_put(conn, buf, len) < 0)
85 fprintf(stderr, "cs_put: %s", cs_errlist[cs_errno(conn)]);
90 /* INIT SERVICE ------------------------------- */
92 static void send_initRequest()
94 Z_APDU *apdu = zget_APDU(out, Z_APDU_initRequest);
95 Z_InitRequest *req = apdu->u.initRequest;
97 ODR_MASK_SET(req->options, Z_Options_search);
98 ODR_MASK_SET(req->options, Z_Options_present);
100 ODR_MASK_SET(req->protocolVersion, 0);
101 ODR_MASK_SET(req->protocolVersion, 1);
102 ODR_MASK_SET(req->protocolVersion, 2);
104 req->idAuthentication = auth;
107 printf("Sent initrequest.\n");
110 static int process_initResponse(Z_InitResponse *res)
113 printf("Connection rejected by target.\n");
115 printf("Connection accepted by target.\n");
116 if (res->implementationId)
117 printf("ID : %s\n", res->implementationId);
118 if (res->implementationName)
119 printf("Name : %s\n", res->implementationName);
120 if (res->implementationVersion)
121 printf("Version: %s\n", res->implementationVersion);
122 if (res->userInformationField)
124 printf("UserInformationfield:\n");
125 if (!odr_external(print, (Odr_external**)&res-> userInformationField,
128 odr_perror(print, "Printing userinfo\n");
131 if (res->userInformationField->which == ODR_EXTERNAL_octet)
133 printf("Guessing visiblestring:\n");
134 printf("'%s'\n", res->userInformationField->u. octet_aligned->buf);
140 int cmd_open(char *arg)
143 char type[100], addr[100];
148 printf("Already connected.\n");
151 if (!*arg || sscanf(arg, "%[^:]:%s", type, addr) < 2)
153 fprintf(stderr, "Usage: open (osi|tcp) ':' [tsel '/']host[':'port]\n");
157 if (!strcmp(type, "osi"))
159 if (!(add = mosi_strtoaddr(addr)))
169 if (!strcmp(type, "tcp"))
171 if (!(add = tcpip_strtoaddr(addr)))
177 protocol = PROTO_Z3950;
181 fprintf(stderr, "Bad type: %s\n", type);
184 if (!(conn = cs_create(t, 1, CS_Z3950)))
189 printf("Connecting...");
191 if (cs_connect(conn, add) < 0)
203 int cmd_authentication(char *arg)
205 static Z_IdAuthentication au;
206 static char open[256];
210 printf("Auth field set to null\n");
215 au.which = Z_IdAuthentication_open;
221 /* SEARCH SERVICE ------------------------------ */
223 void display_record(Z_DatabaseRecord *p)
225 Odr_external *r = (Odr_external*) p;
227 if (r->direct_reference)
229 oident *ent = oid_getentbyoid(r->direct_reference);
231 printf("Record type: ");
233 printf("%s\n", ent->desc);
234 else if (!odr_oid(print, &r->direct_reference, 0))
236 odr_perror(print, "print oid");
241 if (r->which == ODR_EXTERNAL_octet && p->u.octet_aligned->len)
244 marc_display ((char*)p->u.octet_aligned->buf, stdout);
246 FILE *ofi = fopen("dump", "a");
248 fwrite(p->u.octet_aligned->buf, 1, p->u.octet_aligned->len, ofi);
250 printf("dumped record\n");
255 printf("Unknown record representation.\n");
256 if (!odr_external(print, &r, 0))
258 odr_perror(print, "Printing external");
265 static void display_diagrec(Z_DiagRec *p)
269 printf("Diagnostic message from database.\n");
270 if (!(ent = oid_getentbyoid(p->diagnosticSetId)) ||
271 ent->class != CLASS_DIAGSET || ent->value != VAL_BIB1)
272 printf("Missing or unknown diagset\n");
273 printf("Error condition: %d", *p->condition);
274 printf(" -- %s\n", p->addinfo ? p->addinfo : "");
277 static void display_nameplusrecord(Z_NamePlusRecord *p)
280 printf("[%s]", p->databaseName);
281 if (p->which == Z_NamePlusRecord_surrogateDiagnostic)
282 display_diagrec(p->u.surrogateDiagnostic);
284 display_record(p->u.databaseRecord);
287 static void display_records(Z_Records *p)
291 if (p->which == Z_Records_NSD)
292 display_diagrec(p->u.nonSurrogateDiagnostic);
295 printf("Records: %d\n", p->u.databaseOrSurDiagnostics->num_records);
296 for (i = 0; i < p->u.databaseOrSurDiagnostics->num_records; i++)
297 display_nameplusrecord(p->u.databaseOrSurDiagnostics->records[i]);
301 static int send_searchRequest(char *arg)
303 Z_APDU *apdu = zget_APDU(out, Z_APDU_searchRequest);
304 Z_SearchRequest *req = apdu->u.searchRequest;
305 char *databaseNames = database;
309 struct ccl_rpn_node *rpn;
315 Z_RPNQuery *RPNquery;
323 rpn = ccl_find_str(bibset, arg, &error, &pos);
326 printf("CCL ERROR: %s\n", ccl_err_msg(error));
332 if (!strcmp(arg, "@big")) /* strictly for troublemaking */
334 static unsigned char big[2100];
337 /* send a very big referenceid to test transport stack etc. */
338 memset(big, 'A', 2100);
339 bigo.len = bigo.size = 2100;
341 req->referenceId = &bigo;
346 sprintf(setstring, "%d", ++setnumber);
347 req->resultSetName = setstring;
349 *req->smallSetUpperBound = smallSetUpperBound;
350 *req->largeSetLowerBound = largeSetLowerBound;
351 *req->mediumSetPresentNumber = mediumSetPresentNumber;
352 req->num_databaseNames = 1;
353 req->databaseNames = &databaseNames;
358 query.which = Z_Query_type_1;
361 assert((RPNquery = ccl_rpn_query(rpn)));
363 RPNquery = p_query_rpn (out, arg);
366 printf("Prefix query error\n");
370 bib1.proto = protocol;
371 bib1.class = CLASS_ATTSET;
372 bib1.value = VAL_BIB1;
373 RPNquery->attributeSetId = oid_getoidbyent(&bib1);
374 query.u.type_1 = RPNquery;
376 query.which = Z_Query_type_2;
377 query.u.type_2 = &ccl_query;
378 ccl_query.buf = (unsigned char*) arg;
379 ccl_query.len = strlen(arg);
384 printf("Sent searchRequest.\n");
388 static int process_searchResponse(Z_SearchResponse *res)
390 if (res->searchStatus)
391 printf("Search was a success.\n");
393 printf("Search was a bloomin' failure.\n");
394 printf("Number of hits: %d, setno %d\n",
395 *res->resultCount, setnumber);
396 printf("records returned: %d\n",
397 *res->numberOfRecordsReturned);
398 setno += *res->numberOfRecordsReturned;
400 display_records(res->records);
404 static int cmd_find(char *arg)
408 printf("Find what?\n");
413 printf("Not connected yet\n");
416 if (!send_searchRequest(arg))
421 static int cmd_ssub(char *arg)
423 if (!(smallSetUpperBound = atoi(arg)))
428 static int cmd_lslb(char *arg)
430 if (!(largeSetLowerBound = atoi(arg)))
435 static int cmd_mspn(char *arg)
437 if (!(mediumSetPresentNumber = atoi(arg)))
442 static int cmd_status(char *arg)
444 printf("smallSetUpperBound: %d\n", smallSetUpperBound);
445 printf("largeSetLowerBound: %d\n", largeSetLowerBound);
446 printf("mediumSetPresentNumber: %d\n", mediumSetPresentNumber);
450 static int cmd_base(char *arg)
454 printf("Usage: base <database>\n");
457 strcpy(database, arg);
461 static int cmd_setnames(char *arg)
465 printf("Set numbering enabled.\n");
470 printf("Set numbering disabled.\n");
476 /* PRESENT SERVICE ----------------------------- */
478 static int send_presentRequest(char *arg)
480 Z_APDU *apdu = zget_APDU(out, Z_APDU_presentRequest);
481 Z_PresentRequest *req = apdu->u.presentRequest;
486 if ((p = strchr(arg, '+')))
496 sprintf(setstring, "%d", setnumber);
497 req->resultSetId = setstring;
499 req->resultSetStartPoint = &setno;
500 req->numberOfRecordsRequested = &nos;
502 printf("Sent presentRequest (%d+%d).\n", setno, nos);
506 static int cmd_show(char *arg)
508 if (!send_presentRequest(arg))
513 int cmd_quit(char *arg)
515 printf("See you later, alligator.\n");
519 static void initialize(void)
523 if (!(out = odr_createmem(ODR_ENCODE)) ||
524 !(in = odr_createmem(ODR_DECODE)) ||
525 !(print = odr_createmem(ODR_PRINT)))
527 fprintf(stderr, "failed to allocate ODR streams\n");
530 setvbuf(stdout, 0, _IONBF, 0);
534 bibset = ccl_qual_mk ();
535 inf = fopen ("default.bib", "r");
538 ccl_qual_file (bibset, inf);
545 static int client(void)
549 int (*fun)(char *arg);
552 {"open", cmd_open, "('tcp'|'osi')':'[<TSEL>'/']<HOST>[':'<PORT>]"},
553 {"quit", cmd_quit, ""},
554 {"find", cmd_find, "<CCL-QUERY>"},
555 {"base", cmd_base, "<BASE-NAME>"},
556 {"show", cmd_show, "<REC#>['+'<#RECS>]"},
557 {"authentication", cmd_authentication, "<ACCTSTRING>"},
558 {"lslb", cmd_lslb, "<largeSetLowerBound>"},
559 {"ssub", cmd_ssub, "<smallSetUpperBound>"},
560 {"mspn", cmd_mspn, "<mediumSetPresentNumber>"},
561 {"status", cmd_status, ""},
562 {"setnames", cmd_setnames, ""},
566 int netbufferlen = 0;
574 char line[1024], word[1024], arg[1024];
579 FD_SET(cs_fileno(conn), &input);
580 if ((res = select(20, &input, 0, 0, 0)) < 0)
587 if (FD_ISSET(0, &input))
589 /* quick & dirty way to get a command line. */
592 if ((res = sscanf(line, "%s %[^;]", word, arg)) <= 0)
599 for (i = 0; cmd[i].cmd; i++)
600 if (!strncmp(cmd[i].cmd, word, strlen(word)))
602 res = (*cmd[i].fun)(arg);
605 if (!cmd[i].cmd) /* dump our help-screen */
607 printf("Unknown command: %s.\n", word);
608 printf("Currently recognized commands:\n");
609 for (i = 0; cmd[i].cmd; i++)
610 printf(" %s %s\n", cmd[i].cmd, cmd[i].ad);
616 if (conn && FD_ISSET(cs_fileno(conn), &input))
620 if ((res = cs_get(conn, &netbuffer, &netbufferlen)) < 0)
627 printf("Target closed connection.\n");
630 odr_reset(in); /* release APDU from last round */
631 odr_setbuf(in, netbuffer, res, 0);
632 if (!z_APDU(in, &apdu, 0))
634 odr_perror(in, "Decoding incoming APDU");
638 if (!z_APDU(print, &apdu, 0))
640 odr_perror(print, "Failed to print incoming APDU");
647 case Z_APDU_initResponse:
648 process_initResponse(apdu->u.initResponse);
650 case Z_APDU_searchResponse:
651 process_searchResponse(apdu->u.searchResponse);
653 case Z_APDU_presentResponse:
654 printf("Received presentResponse.\n");
656 *apdu->u.presentResponse->numberOfRecordsReturned;
657 if (apdu->u.presentResponse->records)
658 display_records(apdu->u.presentResponse->records);
660 printf("No records.\n");
663 printf("Received unknown APDU type (%d).\n",
670 while (cs_more(conn));
676 int main(int argc, char **argv)