2 * Copyright (c) 1995, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.5 1995-05-29 08:10:47 quinn
10 * Revision 1.4 1995/05/22 15:30:13 adam
11 * Client uses prefix query notation.
13 * Revision 1.3 1995/05/22 15:06:53 quinn
14 * *** empty log message ***
16 * Revision 1.2 1995/05/22 14:56:40 quinn
17 * *** empty log message ***
19 * Revision 1.1 1995/05/22 11:30:31 quinn
20 * Added prettier client.
26 * This is the obligatory little toy client, whose primary purpose is
27 * to illustrate the use of the YAZ service-level API.
54 #include "../version.h"
56 #define C_PROMPT "Z> "
58 static ODR out, in, print; /* encoding and decoding streams */
59 static COMSTACK conn = 0; /* our z-association */
60 static Z_IdAuthentication *auth = 0; /* our current auth definition */
61 static char database[512] = "Default"; /* Database name */
62 static int setnumber = 0; /* current result set number */
63 static int smallSetUpperBound = 0;
64 static int largeSetLowerBound = 1;
65 static int mediumSetPresentNumber = 0;
66 static int setno = 1; /* current set offset */
67 static int protocol = PROTO_Z3950; /* current app protocol */
70 static CCL_bibset bibset; /* CCL bibset handle */
74 static void send_apdu(Z_APDU *a)
79 if (!z_APDU(out, &a, 0))
81 odr_perror(out, "Encoding APDU");
84 buf = odr_getbuf(out, &len, 0);
85 odr_reset(out); /* release the APDU */
86 if (cs_put(conn, buf, len) < 0)
88 fprintf(stderr, "cs_put: %s", cs_errlist[cs_errno(conn)]);
93 /* INIT SERVICE ------------------------------- */
95 static void send_initRequest()
97 Z_APDU *apdu = zget_APDU(out, Z_APDU_initRequest);
98 Z_InitRequest *req = apdu->u.initRequest;
100 ODR_MASK_SET(req->options, Z_Options_search);
101 ODR_MASK_SET(req->options, Z_Options_present);
103 ODR_MASK_SET(req->protocolVersion, 0);
104 ODR_MASK_SET(req->protocolVersion, 1);
105 ODR_MASK_SET(req->protocolVersion, 2);
107 req->idAuthentication = auth;
110 printf("Sent initrequest.\n");
113 static int process_initResponse(Z_InitResponse *res)
116 printf("Connection rejected by target.\n");
118 printf("Connection accepted by target.\n");
119 if (res->implementationId)
120 printf("ID : %s\n", res->implementationId);
121 if (res->implementationName)
122 printf("Name : %s\n", res->implementationName);
123 if (res->implementationVersion)
124 printf("Version: %s\n", res->implementationVersion);
125 if (res->userInformationField)
127 printf("UserInformationfield:\n");
128 if (!odr_external(print, (Odr_external**)&res-> userInformationField,
131 odr_perror(print, "Printing userinfo\n");
134 if (res->userInformationField->which == ODR_EXTERNAL_octet)
136 printf("Guessing visiblestring:\n");
137 printf("'%s'\n", res->userInformationField->u. octet_aligned->buf);
143 int cmd_open(char *arg)
146 char type[100], addr[100];
151 printf("Already connected.\n");
154 if (!*arg || sscanf(arg, "%[^:]:%s", type, addr) < 2)
156 fprintf(stderr, "Usage: open (osi|tcp) ':' [tsel '/']host[':'port]\n");
160 if (!strcmp(type, "osi"))
162 if (!(add = mosi_strtoaddr(addr)))
172 if (!strcmp(type, "tcp"))
174 if (!(add = tcpip_strtoaddr(addr)))
180 protocol = PROTO_Z3950;
184 fprintf(stderr, "Bad type: %s\n", type);
187 if (!(conn = cs_create(t, 1, protocol)))
192 printf("Connecting...");
194 if (cs_connect(conn, add) < 0)
206 int cmd_authentication(char *arg)
208 static Z_IdAuthentication au;
209 static char open[256];
213 printf("Auth field set to null\n");
218 au.which = Z_IdAuthentication_open;
224 /* SEARCH SERVICE ------------------------------ */
226 void display_record(Z_DatabaseRecord *p)
228 Odr_external *r = (Odr_external*) p;
230 if (r->direct_reference)
232 oident *ent = oid_getentbyoid(r->direct_reference);
234 printf("Record type: ");
236 printf("%s\n", ent->desc);
237 else if (!odr_oid(print, &r->direct_reference, 0))
239 odr_perror(print, "print oid");
244 if (r->which == ODR_EXTERNAL_octet && p->u.octet_aligned->len)
247 marc_display ((char*)p->u.octet_aligned->buf, stdout);
249 FILE *ofi = fopen("dump", "a");
251 fwrite(p->u.octet_aligned->buf, 1, p->u.octet_aligned->len, ofi);
253 printf("dumped record\n");
258 printf("Unknown record representation.\n");
259 if (!odr_external(print, &r, 0))
261 odr_perror(print, "Printing external");
268 static void display_diagrec(Z_DiagRec *p)
272 printf("Diagnostic message from database.\n");
273 if (!(ent = oid_getentbyoid(p->diagnosticSetId)) ||
274 ent->class != CLASS_DIAGSET || ent->value != VAL_BIB1)
275 printf("Missing or unknown diagset\n");
276 printf("Error condition: %d", *p->condition);
277 printf(" -- %s\n", p->addinfo ? p->addinfo : "");
280 static void display_nameplusrecord(Z_NamePlusRecord *p)
283 printf("[%s]", p->databaseName);
284 if (p->which == Z_NamePlusRecord_surrogateDiagnostic)
285 display_diagrec(p->u.surrogateDiagnostic);
287 display_record(p->u.databaseRecord);
290 static void display_records(Z_Records *p)
294 if (p->which == Z_Records_NSD)
295 display_diagrec(p->u.nonSurrogateDiagnostic);
298 printf("Records: %d\n", p->u.databaseOrSurDiagnostics->num_records);
299 for (i = 0; i < p->u.databaseOrSurDiagnostics->num_records; i++)
300 display_nameplusrecord(p->u.databaseOrSurDiagnostics->records[i]);
304 static int send_searchRequest(char *arg)
306 Z_APDU *apdu = zget_APDU(out, Z_APDU_searchRequest);
307 Z_SearchRequest *req = apdu->u.searchRequest;
308 char *databaseNames = database;
312 struct ccl_rpn_node *rpn;
318 Z_RPNQuery *RPNquery;
326 rpn = ccl_find_str(bibset, arg, &error, &pos);
329 printf("CCL ERROR: %s\n", ccl_err_msg(error));
335 if (!strcmp(arg, "@big")) /* strictly for troublemaking */
337 static unsigned char big[2100];
340 /* send a very big referenceid to test transport stack etc. */
341 memset(big, 'A', 2100);
342 bigo.len = bigo.size = 2100;
344 req->referenceId = &bigo;
349 sprintf(setstring, "%d", ++setnumber);
350 req->resultSetName = setstring;
352 *req->smallSetUpperBound = smallSetUpperBound;
353 *req->largeSetLowerBound = largeSetLowerBound;
354 *req->mediumSetPresentNumber = mediumSetPresentNumber;
355 req->num_databaseNames = 1;
356 req->databaseNames = &databaseNames;
361 query.which = Z_Query_type_1;
364 assert((RPNquery = ccl_rpn_query(rpn)));
366 RPNquery = p_query_rpn (out, arg);
369 printf("Prefix query error\n");
373 bib1.proto = protocol;
374 bib1.class = CLASS_ATTSET;
375 bib1.value = VAL_BIB1;
376 RPNquery->attributeSetId = oid_getoidbyent(&bib1);
377 query.u.type_1 = RPNquery;
379 query.which = Z_Query_type_2;
380 query.u.type_2 = &ccl_query;
381 ccl_query.buf = (unsigned char*) arg;
382 ccl_query.len = strlen(arg);
387 printf("Sent searchRequest.\n");
391 static int process_searchResponse(Z_SearchResponse *res)
393 if (res->searchStatus)
394 printf("Search was a success.\n");
396 printf("Search was a bloomin' failure.\n");
397 printf("Number of hits: %d, setno %d\n",
398 *res->resultCount, setnumber);
399 printf("records returned: %d\n",
400 *res->numberOfRecordsReturned);
401 setno += *res->numberOfRecordsReturned;
403 display_records(res->records);
407 static int cmd_find(char *arg)
411 printf("Find what?\n");
416 printf("Not connected yet\n");
419 if (!send_searchRequest(arg))
424 static int cmd_ssub(char *arg)
426 if (!(smallSetUpperBound = atoi(arg)))
431 static int cmd_lslb(char *arg)
433 if (!(largeSetLowerBound = atoi(arg)))
438 static int cmd_mspn(char *arg)
440 if (!(mediumSetPresentNumber = atoi(arg)))
445 static int cmd_status(char *arg)
447 printf("smallSetUpperBound: %d\n", smallSetUpperBound);
448 printf("largeSetLowerBound: %d\n", largeSetLowerBound);
449 printf("mediumSetPresentNumber: %d\n", mediumSetPresentNumber);
453 static int cmd_base(char *arg)
457 printf("Usage: base <database>\n");
460 strcpy(database, arg);
464 static int cmd_setnames(char *arg)
468 printf("Set numbering enabled.\n");
473 printf("Set numbering disabled.\n");
479 /* PRESENT SERVICE ----------------------------- */
481 static int send_presentRequest(char *arg)
483 Z_APDU *apdu = zget_APDU(out, Z_APDU_presentRequest);
484 Z_PresentRequest *req = apdu->u.presentRequest;
489 if ((p = strchr(arg, '+')))
499 sprintf(setstring, "%d", setnumber);
500 req->resultSetId = setstring;
502 req->resultSetStartPoint = &setno;
503 req->numberOfRecordsRequested = &nos;
505 printf("Sent presentRequest (%d+%d).\n", setno, nos);
509 static int cmd_show(char *arg)
511 if (!send_presentRequest(arg))
516 int cmd_quit(char *arg)
518 printf("See you later, alligator.\n");
522 static void initialize(void)
530 if (!(out = odr_createmem(ODR_ENCODE)) ||
531 !(in = odr_createmem(ODR_DECODE)) ||
532 !(print = odr_createmem(ODR_PRINT)))
534 fprintf(stderr, "failed to allocate ODR streams\n");
537 setvbuf(stdout, 0, _IONBF, 0);
541 bibset = ccl_qual_mk ();
542 inf = fopen ("default.bib", "r");
545 ccl_qual_file (bibset, inf);
552 static int client(void)
556 int (*fun)(char *arg);
559 {"open", cmd_open, "('tcp'|'osi')':'[<TSEL>'/']<HOST>[':'<PORT>]"},
560 {"quit", cmd_quit, ""},
561 {"find", cmd_find, "<CCL-QUERY>"},
562 {"base", cmd_base, "<BASE-NAME>"},
563 {"show", cmd_show, "<REC#>['+'<#RECS>]"},
564 {"authentication", cmd_authentication, "<ACCTSTRING>"},
565 {"lslb", cmd_lslb, "<largeSetLowerBound>"},
566 {"ssub", cmd_ssub, "<smallSetUpperBound>"},
567 {"mspn", cmd_mspn, "<mediumSetPresentNumber>"},
568 {"status", cmd_status, ""},
569 {"setnames", cmd_setnames, ""},
573 int netbufferlen = 0;
581 char line[1024], word[1024], arg[1024];
586 FD_SET(cs_fileno(conn), &input);
587 if ((res = select(20, &input, 0, 0, 0)) < 0)
594 if (FD_ISSET(0, &input))
596 /* quick & dirty way to get a command line. */
599 if ((res = sscanf(line, "%s %[^;]", word, arg)) <= 0)
606 for (i = 0; cmd[i].cmd; i++)
607 if (!strncmp(cmd[i].cmd, word, strlen(word)))
609 res = (*cmd[i].fun)(arg);
612 if (!cmd[i].cmd) /* dump our help-screen */
614 printf("Unknown command: %s.\n", word);
615 printf("Currently recognized commands:\n");
616 for (i = 0; cmd[i].cmd; i++)
617 printf(" %s %s\n", cmd[i].cmd, cmd[i].ad);
623 if (conn && FD_ISSET(cs_fileno(conn), &input))
627 if ((res = cs_get(conn, &netbuffer, &netbufferlen)) < 0)
634 printf("Target closed connection.\n");
637 odr_reset(in); /* release APDU from last round */
638 odr_setbuf(in, netbuffer, res, 0);
639 if (!z_APDU(in, &apdu, 0))
641 odr_perror(in, "Decoding incoming APDU");
645 if (!z_APDU(print, &apdu, 0))
647 odr_perror(print, "Failed to print incoming APDU");
654 case Z_APDU_initResponse:
655 process_initResponse(apdu->u.initResponse);
657 case Z_APDU_searchResponse:
658 process_searchResponse(apdu->u.searchResponse);
660 case Z_APDU_presentResponse:
661 printf("Received presentResponse.\n");
663 *apdu->u.presentResponse->numberOfRecordsReturned;
664 if (apdu->u.presentResponse->records)
665 display_records(apdu->u.presentResponse->records);
667 printf("No records.\n");
670 printf("Received unknown APDU type (%d).\n",
677 while (cs_more(conn));
683 int main(int argc, char **argv)