2 * Copyright (c) 1995, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.1 1995-05-22 11:30:31 quinn
8 * Added prettier client.
14 * This is the obligatory little toy client, whose primary purpose is
15 * to illustrate the use of the YAZ service-level API.
34 #include "../version.h"
36 #define C_PROMPT "Z> "
38 static ODR out, in, print; /* encoding and decoding streams */
39 static COMSTACK conn = 0; /* our z-association */
40 static Z_IdAuthentication *auth = 0; /* our current auth definition */
41 static char database[512] = "Default"; /* Database name */
42 static int setnumber = 0; /* current result set number */
43 static int smallSetUpperBound = 0;
44 static int largeSetLowerBound = 1;
45 static int mediumSetPresentNumber = 0;
46 static int setno = 1; /* current set offset */
47 static int protocol = PROTO_Z3950; /* current app protocol */
48 static CCL_bibset bibset; /* CCL bibset handle */
50 static void send_apdu(Z_APDU *a)
55 if (!z_APDU(out, &a, 0))
57 odr_perror(out, "Encoding APDU");
60 buf = odr_getbuf(out, &len, 0);
61 odr_reset(out); /* release the APDU */
62 if (cs_put(conn, buf, len) < 0)
64 fprintf(stderr, "cs_put: %s", cs_errlist[cs_errno(conn)]);
69 /* INIT SERVICE ------------------------------- */
71 static void send_initRequest()
73 Z_APDU *apdu = zget_APDU(out, Z_APDU_initRequest);
74 Z_InitRequest *req = apdu->u.initRequest;
76 ODR_MASK_SET(req->options, Z_Options_search);
77 ODR_MASK_SET(req->options, Z_Options_present);
79 ODR_MASK_SET(req->protocolVersion, 0);
80 ODR_MASK_SET(req->protocolVersion, 1);
81 ODR_MASK_SET(req->protocolVersion, 2);
83 req->idAuthentication = auth;
86 printf("Sent initrequest.\n");
89 static int process_initResponse(Z_InitResponse *res)
92 printf("Connection rejected by target.\n");
94 printf("Connection accepted by target.\n");
95 if (res->implementationId)
96 printf("ID : %s\n", res->implementationId);
97 if (res->implementationName)
98 printf("Name : %s\n", res->implementationName);
99 if (res->implementationVersion)
100 printf("Version: %s\n", res->implementationVersion);
101 if (res->userInformationField)
103 printf("UserInformationfield:\n");
104 if (!odr_external(print, (Odr_external**)&res-> userInformationField,
107 odr_perror(print, "Printing userinfo\n");
110 if (res->userInformationField->which == ODR_EXTERNAL_octet)
112 printf("Guessing visiblestring:\n");
113 printf("'%s'\n", res->userInformationField->u. octet_aligned->buf);
119 int cmd_open(char *arg)
122 char type[100], addr[100];
127 printf("Already connected.\n");
130 if (!*arg || sscanf(arg, "%[^:]:%s", type, addr) < 2)
132 fprintf(stderr, "Usage: open (osi|tcp) ':' [tsel '/']host[':'port]\n");
136 if (!strcmp(type, "osi"))
138 if (!(add = mosi_strtoaddr(addr)))
148 if (!strcmp(type, "tcp"))
150 if (!(add = tcpip_strtoaddr(addr)))
156 protocol = PROTO_Z3950;
160 fprintf(stderr, "Bad type: %s\n", type);
163 if (!(conn = cs_create(t, 1, CS_Z3950)))
168 printf("Connecting...");
170 if (cs_connect(conn, add) < 0)
182 int cmd_authentication(char *arg)
184 static Z_IdAuthentication au;
185 static char open[256];
189 printf("Auth field set to null\n");
194 au.which = Z_IdAuthentication_open;
200 /* SEARCH SERVICE ------------------------------ */
202 void display_record(Z_DatabaseRecord *p)
204 Odr_external *r = (Odr_external*) p;
206 if (r->direct_reference)
208 oident *ent = oid_getentbyoid(r->direct_reference);
210 printf("Record type: ");
212 printf("%s\n", ent->desc);
213 else if (!odr_oid(print, &r->direct_reference, 0))
215 odr_perror(print, "print oid");
220 if (r->which == ODR_EXTERNAL_octet && p->u.octet_aligned->len)
223 marc_display ((char*)p->u.octet_aligned->buf, stdout);
225 FILE *ofi = fopen("dump", "a");
227 fwrite(p->u.octet_aligned->buf, 1, p->u.octet_aligned->len, ofi);
229 printf("dumped record\n");
234 printf("Unknown record representation.\n");
235 if (!odr_external(print, &r, 0))
237 odr_perror(print, "Printing external");
244 static void display_diagrec(Z_DiagRec *p)
248 printf("Diagnostic message from database.\n");
249 if (!(ent = oid_getentbyoid(p->diagnosticSetId)) ||
250 ent->class != CLASS_DIAGSET || ent->value != VAL_BIB1)
251 printf("Missing or unknown diagset\n");
252 printf("Error condition: %d", *p->condition);
253 printf(" -- %s\n", p->addinfo ? p->addinfo : "");
256 static void display_nameplusrecord(Z_NamePlusRecord *p)
259 printf("[%s]", p->databaseName);
260 if (p->which == Z_NamePlusRecord_surrogateDiagnostic)
261 display_diagrec(p->u.surrogateDiagnostic);
263 display_record(p->u.databaseRecord);
266 static void display_records(Z_Records *p)
270 if (p->which == Z_Records_NSD)
271 display_diagrec(p->u.nonSurrogateDiagnostic);
274 printf("Records: %d\n", p->u.databaseOrSurDiagnostics->num_records);
275 for (i = 0; i < p->u.databaseOrSurDiagnostics->num_records; i++)
276 display_nameplusrecord(p->u.databaseOrSurDiagnostics->records[i]);
280 static int send_searchRequest(char *arg)
282 Z_APDU *apdu = zget_APDU(out, Z_APDU_searchRequest);
283 Z_SearchRequest *req = apdu->u.searchRequest;
284 char *databaseNames = database;
288 Z_RPNQuery *RPNquery;
290 struct ccl_rpn_node *rpn;
296 rpn = ccl_find_str(bibset, arg, &error, &pos);
299 printf("CCL ERROR: %s\n", ccl_err_msg(error));
304 if (!strcmp(arg, "@big")) /* strictly for troublemaking */
306 static unsigned char big[2100];
309 /* send a very big referenceid to test transport stack etc. */
310 memset(big, 'A', 2100);
311 bigo.len = bigo.size = 2100;
313 req->referenceId = &bigo;
318 sprintf(setstring, "%d", ++setnumber);
319 req->resultSetName = setstring;
321 *req->smallSetUpperBound = smallSetUpperBound;
322 *req->largeSetLowerBound = largeSetLowerBound;
323 *req->mediumSetPresentNumber = mediumSetPresentNumber;
324 req->num_databaseNames = 1;
325 req->databaseNames = &databaseNames;
330 query.which = Z_Query_type_1;
331 assert((RPNquery = ccl_rpn_query(rpn)));
332 bib1.proto = protocol;
333 bib1.class = CLASS_ATTSET;
334 bib1.value = VAL_BIB1;
335 RPNquery->attributeSetId = oid_getoidbyent(&bib1);
336 query.u.type_1 = RPNquery;
338 query.which = Z_Query_type_2;
339 query.u.type_2 = &ccl_query;
340 ccl_query.buf = (unsigned char*) arg;
341 ccl_query.len = strlen(arg);
346 printf("Sent searchRequest.\n");
350 static int process_searchResponse(Z_SearchResponse *res)
352 if (res->searchStatus)
353 printf("Search was a success.\n");
355 printf("Search was a bloomin' failure.\n");
356 printf("Number of hits: %d, setno %d\n",
357 *res->resultCount, setnumber);
358 printf("records returned: %d\n",
359 *res->numberOfRecordsReturned);
360 setno += *res->numberOfRecordsReturned;
362 display_records(res->records);
366 static int cmd_find(char *arg)
370 printf("Find what?\n");
375 printf("Not connected yet\n");
378 if (!send_searchRequest(arg))
383 static int cmd_ssub(char *arg)
385 if (!(smallSetUpperBound = atoi(arg)))
390 static int cmd_lslb(char *arg)
392 if (!(largeSetLowerBound = atoi(arg)))
397 static int cmd_mspn(char *arg)
399 if (!(mediumSetPresentNumber = atoi(arg)))
404 static int cmd_status(char *arg)
406 printf("smallSetUpperBound: %d\n", smallSetUpperBound);
407 printf("largeSetLowerBound: %d\n", largeSetLowerBound);
408 printf("mediumSetPresentNumber: %d\n", mediumSetPresentNumber);
412 static int cmd_base(char *arg)
416 printf("Usage: base <database>\n");
419 strcpy(database, arg);
423 static int cmd_setnames(char *arg)
427 printf("Set numbering enabled.\n");
432 printf("Set numbering disabled.\n");
438 /* PRESENT SERVICE ----------------------------- */
440 static int send_presentRequest(char *arg)
442 Z_APDU *apdu = zget_APDU(out, Z_APDU_presentRequest);
443 Z_PresentRequest *req = apdu->u.presentRequest;
448 if ((p = strchr(arg, '+')))
458 sprintf(setstring, "%d", setnumber);
459 req->resultSetId = setstring;
461 req->resultSetStartPoint = &setno;
462 req->numberOfRecordsRequested = &nos;
464 printf("Sent presentRequest (%d+%d).\n", setno, nos);
468 static int cmd_show(char *arg)
470 if (!send_presentRequest(arg))
475 int cmd_quit(char *arg)
477 printf("See you later, alligator.\n");
481 static void initialize(void)
485 if (!(out = odr_createmem(ODR_ENCODE)) ||
486 !(in = odr_createmem(ODR_DECODE)) ||
487 !(print = odr_createmem(ODR_PRINT)))
489 fprintf(stderr, "failed to allocate ODR streams\n");
492 setvbuf(stdout, 0, _IONBF, 0);
494 bibset = ccl_qual_mk ();
495 inf = fopen ("default.bib", "r");
498 ccl_qual_file (bibset, inf);
503 static int client(void)
507 int (*fun)(char *arg);
510 {"open", cmd_open, "('tcp'|'osi')':'[<TSEL>'/']<HOST>[':'<PORT>]"},
511 {"quit", cmd_quit, ""},
512 {"find", cmd_find, "<CCL-QUERY>"},
513 {"base", cmd_base, "<BASE-NAME>"},
514 {"show", cmd_show, "<REC#>['+'<#RECS>]"},
515 {"authentication", cmd_authentication, "<ACCTSTRING>"},
516 {"lslb", cmd_lslb, "<largeSetLowerBound>"},
517 {"ssub", cmd_ssub, "<smallSetUpperBound>"},
518 {"mspn", cmd_mspn, "<mediumSetPresentNumber>"},
519 {"status", cmd_status, ""},
520 {"setnames", cmd_setnames, ""},
524 int netbufferlen = 0;
533 char line[1024], word[1024], arg[1024];
538 FD_SET(cs_fileno(conn), &input);
539 if ((res = select(20, &input, 0, 0, 0)) < 0)
546 if (FD_ISSET(0, &input))
548 /* quick & dirty way to get a command line. */
551 if ((res = sscanf(line, "%s %[^;]", word, arg)) <= 0)
558 for (i = 0; cmd[i].cmd; i++)
559 if (!strncmp(cmd[i].cmd, word, strlen(word)))
561 res = (*cmd[i].fun)(arg);
564 if (!cmd[i].cmd) /* dump our help-screen */
566 printf("Unknown command: %s.\n", word);
567 printf("Currently recognized commands:\n");
568 for (i = 0; cmd[i].cmd; i++)
569 printf(" %s %s\n", cmd[i].cmd, cmd[i].ad);
575 if (conn && FD_ISSET(cs_fileno(conn), &input))
579 if ((res = cs_get(conn, &netbuffer, &netbufferlen)) < 0)
586 printf("Target closed connection.\n");
589 odr_reset(in); /* release APDU from last round */
590 odr_setbuf(in, netbuffer, res, 0);
591 if (!z_APDU(in, &apdu, 0))
593 odr_perror(in, "Decoding incoming APDU");
597 if (!z_APDU(print, &apdu, 0))
599 odr_perror(print, "Failed to print incoming APDU");
606 case Z_APDU_initResponse:
607 process_initResponse(apdu->u.initResponse);
609 case Z_APDU_searchResponse:
610 process_searchResponse(apdu->u.searchResponse);
612 case Z_APDU_presentResponse:
613 printf("Received presentResponse.\n");
615 *apdu->u.presentResponse->numberOfRecordsReturned;
616 if (apdu->u.presentResponse->records)
617 display_records(apdu->u.presentResponse->records);
619 printf("No records.\n");
622 printf("Received unknown APDU type (%d).\n",
629 while (cs_more(conn));
635 int main(int argc, char **argv)