2 * Copyright (c) 1995, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.2 1995-05-22 14:56:40 quinn
8 * *** empty log message ***
10 * Revision 1.1 1995/05/22 11:30:31 quinn
11 * Added prettier client.
17 * This is the obligatory little toy client, whose primary purpose is
18 * to illustrate the use of the YAZ service-level API.
37 #include "../version.h"
39 #define C_PROMPT "Z> "
41 static ODR out, in, print; /* encoding and decoding streams */
42 static COMSTACK conn = 0; /* our z-association */
43 static Z_IdAuthentication *auth = 0; /* our current auth definition */
44 static char database[512] = "Default"; /* Database name */
45 static int setnumber = 0; /* current result set number */
46 static int smallSetUpperBound = 0;
47 static int largeSetLowerBound = 1;
48 static int mediumSetPresentNumber = 0;
49 static int setno = 1; /* current set offset */
50 static int protocol = PROTO_Z3950; /* current app protocol */
51 static CCL_bibset bibset; /* CCL bibset handle */
53 static void send_apdu(Z_APDU *a)
58 if (!z_APDU(out, &a, 0))
60 odr_perror(out, "Encoding APDU");
63 buf = odr_getbuf(out, &len, 0);
64 odr_reset(out); /* release the APDU */
65 if (cs_put(conn, buf, len) < 0)
67 fprintf(stderr, "cs_put: %s", cs_errlist[cs_errno(conn)]);
72 /* INIT SERVICE ------------------------------- */
74 static void send_initRequest()
76 Z_APDU *apdu = zget_APDU(out, Z_APDU_initRequest);
77 Z_InitRequest *req = apdu->u.initRequest;
79 ODR_MASK_SET(req->options, Z_Options_search);
80 ODR_MASK_SET(req->options, Z_Options_present);
82 ODR_MASK_SET(req->protocolVersion, 0);
83 ODR_MASK_SET(req->protocolVersion, 1);
84 ODR_MASK_SET(req->protocolVersion, 2);
86 req->idAuthentication = auth;
89 printf("Sent initrequest.\n");
92 static int process_initResponse(Z_InitResponse *res)
95 printf("Connection rejected by target.\n");
97 printf("Connection accepted by target.\n");
98 if (res->implementationId)
99 printf("ID : %s\n", res->implementationId);
100 if (res->implementationName)
101 printf("Name : %s\n", res->implementationName);
102 if (res->implementationVersion)
103 printf("Version: %s\n", res->implementationVersion);
104 if (res->userInformationField)
106 printf("UserInformationfield:\n");
107 if (!odr_external(print, (Odr_external**)&res-> userInformationField,
110 odr_perror(print, "Printing userinfo\n");
113 if (res->userInformationField->which == ODR_EXTERNAL_octet)
115 printf("Guessing visiblestring:\n");
116 printf("'%s'\n", res->userInformationField->u. octet_aligned->buf);
122 int cmd_open(char *arg)
125 char type[100], addr[100];
130 printf("Already connected.\n");
133 if (!*arg || sscanf(arg, "%[^:]:%s", type, addr) < 2)
135 fprintf(stderr, "Usage: open (osi|tcp) ':' [tsel '/']host[':'port]\n");
139 if (!strcmp(type, "osi"))
141 if (!(add = mosi_strtoaddr(addr)))
151 if (!strcmp(type, "tcp"))
153 if (!(add = tcpip_strtoaddr(addr)))
159 protocol = PROTO_Z3950;
163 fprintf(stderr, "Bad type: %s\n", type);
166 if (!(conn = cs_create(t, 1, CS_Z3950)))
171 printf("Connecting...");
173 if (cs_connect(conn, add) < 0)
185 int cmd_authentication(char *arg)
187 static Z_IdAuthentication au;
188 static char open[256];
192 printf("Auth field set to null\n");
197 au.which = Z_IdAuthentication_open;
203 /* SEARCH SERVICE ------------------------------ */
205 void display_record(Z_DatabaseRecord *p)
207 Odr_external *r = (Odr_external*) p;
209 if (r->direct_reference)
211 oident *ent = oid_getentbyoid(r->direct_reference);
213 printf("Record type: ");
215 printf("%s\n", ent->desc);
216 else if (!odr_oid(print, &r->direct_reference, 0))
218 odr_perror(print, "print oid");
223 if (r->which == ODR_EXTERNAL_octet && p->u.octet_aligned->len)
226 marc_display ((char*)p->u.octet_aligned->buf, stdout);
228 FILE *ofi = fopen("dump", "a");
230 fwrite(p->u.octet_aligned->buf, 1, p->u.octet_aligned->len, ofi);
232 printf("dumped record\n");
237 printf("Unknown record representation.\n");
238 if (!odr_external(print, &r, 0))
240 odr_perror(print, "Printing external");
247 static void display_diagrec(Z_DiagRec *p)
251 printf("Diagnostic message from database.\n");
252 if (!(ent = oid_getentbyoid(p->diagnosticSetId)) ||
253 ent->class != CLASS_DIAGSET || ent->value != VAL_BIB1)
254 printf("Missing or unknown diagset\n");
255 printf("Error condition: %d", *p->condition);
256 printf(" -- %s\n", p->addinfo ? p->addinfo : "");
259 static void display_nameplusrecord(Z_NamePlusRecord *p)
262 printf("[%s]", p->databaseName);
263 if (p->which == Z_NamePlusRecord_surrogateDiagnostic)
264 display_diagrec(p->u.surrogateDiagnostic);
266 display_record(p->u.databaseRecord);
269 static void display_records(Z_Records *p)
273 if (p->which == Z_Records_NSD)
274 display_diagrec(p->u.nonSurrogateDiagnostic);
277 printf("Records: %d\n", p->u.databaseOrSurDiagnostics->num_records);
278 for (i = 0; i < p->u.databaseOrSurDiagnostics->num_records; i++)
279 display_nameplusrecord(p->u.databaseOrSurDiagnostics->records[i]);
283 static int send_searchRequest(char *arg)
285 Z_APDU *apdu = zget_APDU(out, Z_APDU_searchRequest);
286 Z_SearchRequest *req = apdu->u.searchRequest;
287 char *databaseNames = database;
291 Z_RPNQuery *RPNquery;
293 struct ccl_rpn_node *rpn;
299 rpn = ccl_find_str(bibset, arg, &error, &pos);
302 printf("CCL ERROR: %s\n", ccl_err_msg(error));
307 if (!strcmp(arg, "@big")) /* strictly for troublemaking */
309 static unsigned char big[2100];
312 /* send a very big referenceid to test transport stack etc. */
313 memset(big, 'A', 2100);
314 bigo.len = bigo.size = 2100;
316 req->referenceId = &bigo;
321 sprintf(setstring, "%d", ++setnumber);
322 req->resultSetName = setstring;
324 *req->smallSetUpperBound = smallSetUpperBound;
325 *req->largeSetLowerBound = largeSetLowerBound;
326 *req->mediumSetPresentNumber = mediumSetPresentNumber;
327 req->num_databaseNames = 1;
328 req->databaseNames = &databaseNames;
333 query.which = Z_Query_type_1;
334 assert((RPNquery = ccl_rpn_query(rpn)));
335 bib1.proto = protocol;
336 bib1.class = CLASS_ATTSET;
337 bib1.value = VAL_BIB1;
338 RPNquery->attributeSetId = oid_getoidbyent(&bib1);
339 query.u.type_1 = RPNquery;
341 query.which = Z_Query_type_2;
342 query.u.type_2 = &ccl_query;
343 ccl_query.buf = (unsigned char*) arg;
344 ccl_query.len = strlen(arg);
349 printf("Sent searchRequest.\n");
353 static int process_searchResponse(Z_SearchResponse *res)
355 if (res->searchStatus)
356 printf("Search was a success.\n");
358 printf("Search was a bloomin' failure.\n");
359 printf("Number of hits: %d, setno %d\n",
360 *res->resultCount, setnumber);
361 printf("records returned: %d\n",
362 *res->numberOfRecordsReturned);
363 setno += *res->numberOfRecordsReturned;
365 display_records(res->records);
369 static int cmd_find(char *arg)
373 printf("Find what?\n");
378 printf("Not connected yet\n");
381 if (!send_searchRequest(arg))
386 static int cmd_ssub(char *arg)
388 if (!(smallSetUpperBound = atoi(arg)))
393 static int cmd_lslb(char *arg)
395 if (!(largeSetLowerBound = atoi(arg)))
400 static int cmd_mspn(char *arg)
402 if (!(mediumSetPresentNumber = atoi(arg)))
407 static int cmd_status(char *arg)
409 printf("smallSetUpperBound: %d\n", smallSetUpperBound);
410 printf("largeSetLowerBound: %d\n", largeSetLowerBound);
411 printf("mediumSetPresentNumber: %d\n", mediumSetPresentNumber);
415 static int cmd_base(char *arg)
419 printf("Usage: base <database>\n");
422 strcpy(database, arg);
426 static int cmd_setnames(char *arg)
430 printf("Set numbering enabled.\n");
435 printf("Set numbering disabled.\n");
441 /* PRESENT SERVICE ----------------------------- */
443 static int send_presentRequest(char *arg)
445 Z_APDU *apdu = zget_APDU(out, Z_APDU_presentRequest);
446 Z_PresentRequest *req = apdu->u.presentRequest;
451 if ((p = strchr(arg, '+')))
461 sprintf(setstring, "%d", setnumber);
462 req->resultSetId = setstring;
464 req->resultSetStartPoint = &setno;
465 req->numberOfRecordsRequested = &nos;
467 printf("Sent presentRequest (%d+%d).\n", setno, nos);
471 static int cmd_show(char *arg)
473 if (!send_presentRequest(arg))
478 int cmd_quit(char *arg)
480 printf("See you later, alligator.\n");
484 static void initialize(void)
488 if (!(out = odr_createmem(ODR_ENCODE)) ||
489 !(in = odr_createmem(ODR_DECODE)) ||
490 !(print = odr_createmem(ODR_PRINT)))
492 fprintf(stderr, "failed to allocate ODR streams\n");
495 setvbuf(stdout, 0, _IONBF, 0);
497 bibset = ccl_qual_mk ();
498 inf = fopen ("default.bib", "r");
501 ccl_qual_file (bibset, inf);
506 static int client(void)
510 int (*fun)(char *arg);
513 {"open", cmd_open, "('tcp'|'osi')':'[<TSEL>'/']<HOST>[':'<PORT>]"},
514 {"quit", cmd_quit, ""},
515 {"find", cmd_find, "<CCL-QUERY>"},
516 {"base", cmd_base, "<BASE-NAME>"},
517 {"show", cmd_show, "<REC#>['+'<#RECS>]"},
518 {"authentication", cmd_authentication, "<ACCTSTRING>"},
519 {"lslb", cmd_lslb, "<largeSetLowerBound>"},
520 {"ssub", cmd_ssub, "<smallSetUpperBound>"},
521 {"mspn", cmd_mspn, "<mediumSetPresentNumber>"},
522 {"status", cmd_status, ""},
523 {"setnames", cmd_setnames, ""},
527 int netbufferlen = 0;
535 char line[1024], word[1024], arg[1024];
540 FD_SET(cs_fileno(conn), &input);
541 if ((res = select(20, &input, 0, 0, 0)) < 0)
548 if (FD_ISSET(0, &input))
550 /* quick & dirty way to get a command line. */
553 if ((res = sscanf(line, "%s %[^;]", word, arg)) <= 0)
560 for (i = 0; cmd[i].cmd; i++)
561 if (!strncmp(cmd[i].cmd, word, strlen(word)))
563 res = (*cmd[i].fun)(arg);
566 if (!cmd[i].cmd) /* dump our help-screen */
568 printf("Unknown command: %s.\n", word);
569 printf("Currently recognized commands:\n");
570 for (i = 0; cmd[i].cmd; i++)
571 printf(" %s %s\n", cmd[i].cmd, cmd[i].ad);
577 if (conn && FD_ISSET(cs_fileno(conn), &input))
581 if ((res = cs_get(conn, &netbuffer, &netbufferlen)) < 0)
588 printf("Target closed connection.\n");
591 odr_reset(in); /* release APDU from last round */
592 odr_setbuf(in, netbuffer, res, 0);
593 if (!z_APDU(in, &apdu, 0))
595 odr_perror(in, "Decoding incoming APDU");
599 if (!z_APDU(print, &apdu, 0))
601 odr_perror(print, "Failed to print incoming APDU");
608 case Z_APDU_initResponse:
609 process_initResponse(apdu->u.initResponse);
611 case Z_APDU_searchResponse:
612 process_searchResponse(apdu->u.searchResponse);
614 case Z_APDU_presentResponse:
615 printf("Received presentResponse.\n");
617 *apdu->u.presentResponse->numberOfRecordsReturned;
618 if (apdu->u.presentResponse->records)
619 display_records(apdu->u.presentResponse->records);
621 printf("No records.\n");
624 printf("Received unknown APDU type (%d).\n",
631 while (cs_more(conn));
637 int main(int argc, char **argv)