2 * Copyright (c) 1995, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.3 1995-05-22 15:06:53 quinn
8 * *** empty log message ***
10 * Revision 1.2 1995/05/22 14:56:40 quinn
11 * *** empty log message ***
13 * Revision 1.1 1995/05/22 11:30:31 quinn
14 * Added prettier client.
20 * This is the obligatory little toy client, whose primary purpose is
21 * to illustrate the use of the YAZ service-level API.
40 #include "../version.h"
42 #define C_PROMPT "Z> "
44 static ODR out, in, print; /* encoding and decoding streams */
45 static COMSTACK conn = 0; /* our z-association */
46 static Z_IdAuthentication *auth = 0; /* our current auth definition */
47 static char database[512] = "Default"; /* Database name */
48 static int setnumber = 0; /* current result set number */
49 static int smallSetUpperBound = 0;
50 static int largeSetLowerBound = 1;
51 static int mediumSetPresentNumber = 0;
52 static int setno = 1; /* current set offset */
53 static int protocol = PROTO_Z3950; /* current app protocol */
54 static CCL_bibset bibset; /* CCL bibset handle */
56 static void send_apdu(Z_APDU *a)
61 if (!z_APDU(out, &a, 0))
63 odr_perror(out, "Encoding APDU");
66 buf = odr_getbuf(out, &len, 0);
67 odr_reset(out); /* release the APDU */
68 if (cs_put(conn, buf, len) < 0)
70 fprintf(stderr, "cs_put: %s", cs_errlist[cs_errno(conn)]);
75 /* INIT SERVICE ------------------------------- */
77 static void send_initRequest()
79 Z_APDU *apdu = zget_APDU(out, Z_APDU_initRequest);
80 Z_InitRequest *req = apdu->u.initRequest;
82 ODR_MASK_SET(req->options, Z_Options_search);
83 ODR_MASK_SET(req->options, Z_Options_present);
85 ODR_MASK_SET(req->protocolVersion, 0);
86 ODR_MASK_SET(req->protocolVersion, 1);
87 ODR_MASK_SET(req->protocolVersion, 2);
89 req->idAuthentication = auth;
92 printf("Sent initrequest.\n");
95 static int process_initResponse(Z_InitResponse *res)
98 printf("Connection rejected by target.\n");
100 printf("Connection accepted by target.\n");
101 if (res->implementationId)
102 printf("ID : %s\n", res->implementationId);
103 if (res->implementationName)
104 printf("Name : %s\n", res->implementationName);
105 if (res->implementationVersion)
106 printf("Version: %s\n", res->implementationVersion);
107 if (res->userInformationField)
109 printf("UserInformationfield:\n");
110 if (!odr_external(print, (Odr_external**)&res-> userInformationField,
113 odr_perror(print, "Printing userinfo\n");
116 if (res->userInformationField->which == ODR_EXTERNAL_octet)
118 printf("Guessing visiblestring:\n");
119 printf("'%s'\n", res->userInformationField->u. octet_aligned->buf);
125 int cmd_open(char *arg)
128 char type[100], addr[100];
133 printf("Already connected.\n");
136 if (!*arg || sscanf(arg, "%[^:]:%s", type, addr) < 2)
138 fprintf(stderr, "Usage: open (osi|tcp) ':' [tsel '/']host[':'port]\n");
142 if (!strcmp(type, "osi"))
144 if (!(add = mosi_strtoaddr(addr)))
154 if (!strcmp(type, "tcp"))
156 if (!(add = tcpip_strtoaddr(addr)))
162 protocol = PROTO_Z3950;
166 fprintf(stderr, "Bad type: %s\n", type);
169 if (!(conn = cs_create(t, 1, CS_Z3950)))
174 printf("Connecting...");
176 if (cs_connect(conn, add) < 0)
188 int cmd_authentication(char *arg)
190 static Z_IdAuthentication au;
191 static char open[256];
195 printf("Auth field set to null\n");
200 au.which = Z_IdAuthentication_open;
206 /* SEARCH SERVICE ------------------------------ */
208 void display_record(Z_DatabaseRecord *p)
210 Odr_external *r = (Odr_external*) p;
212 if (r->direct_reference)
214 oident *ent = oid_getentbyoid(r->direct_reference);
216 printf("Record type: ");
218 printf("%s\n", ent->desc);
219 else if (!odr_oid(print, &r->direct_reference, 0))
221 odr_perror(print, "print oid");
226 if (r->which == ODR_EXTERNAL_octet && p->u.octet_aligned->len)
229 marc_display ((char*)p->u.octet_aligned->buf, stdout);
231 FILE *ofi = fopen("dump", "a");
233 fwrite(p->u.octet_aligned->buf, 1, p->u.octet_aligned->len, ofi);
235 printf("dumped record\n");
240 printf("Unknown record representation.\n");
241 if (!odr_external(print, &r, 0))
243 odr_perror(print, "Printing external");
250 static void display_diagrec(Z_DiagRec *p)
254 printf("Diagnostic message from database.\n");
255 if (!(ent = oid_getentbyoid(p->diagnosticSetId)) ||
256 ent->class != CLASS_DIAGSET || ent->value != VAL_BIB1)
257 printf("Missing or unknown diagset\n");
258 printf("Error condition: %d", *p->condition);
259 printf(" -- %s\n", p->addinfo ? p->addinfo : "");
262 static void display_nameplusrecord(Z_NamePlusRecord *p)
265 printf("[%s]", p->databaseName);
266 if (p->which == Z_NamePlusRecord_surrogateDiagnostic)
267 display_diagrec(p->u.surrogateDiagnostic);
269 display_record(p->u.databaseRecord);
272 static void display_records(Z_Records *p)
276 if (p->which == Z_Records_NSD)
277 display_diagrec(p->u.nonSurrogateDiagnostic);
280 printf("Records: %d\n", p->u.databaseOrSurDiagnostics->num_records);
281 for (i = 0; i < p->u.databaseOrSurDiagnostics->num_records; i++)
282 display_nameplusrecord(p->u.databaseOrSurDiagnostics->records[i]);
286 static int send_searchRequest(char *arg)
288 Z_APDU *apdu = zget_APDU(out, Z_APDU_searchRequest);
289 Z_SearchRequest *req = apdu->u.searchRequest;
290 char *databaseNames = database;
294 Z_RPNQuery *RPNquery;
296 struct ccl_rpn_node *rpn;
302 rpn = ccl_find_str(bibset, arg, &error, &pos);
305 printf("CCL ERROR: %s\n", ccl_err_msg(error));
310 if (!strcmp(arg, "@big")) /* strictly for troublemaking */
312 static unsigned char big[2100];
315 /* send a very big referenceid to test transport stack etc. */
316 memset(big, 'A', 2100);
317 bigo.len = bigo.size = 2100;
319 req->referenceId = &bigo;
324 sprintf(setstring, "%d", ++setnumber);
325 req->resultSetName = setstring;
327 *req->smallSetUpperBound = smallSetUpperBound;
328 *req->largeSetLowerBound = largeSetLowerBound;
329 *req->mediumSetPresentNumber = mediumSetPresentNumber;
330 req->num_databaseNames = 1;
331 req->databaseNames = &databaseNames;
336 query.which = Z_Query_type_1;
337 assert((RPNquery = ccl_rpn_query(rpn)));
338 bib1.proto = protocol;
339 bib1.class = CLASS_ATTSET;
340 bib1.value = VAL_BIB1;
341 RPNquery->attributeSetId = oid_getoidbyent(&bib1);
342 query.u.type_1 = RPNquery;
344 query.which = Z_Query_type_2;
345 query.u.type_2 = &ccl_query;
346 ccl_query.buf = (unsigned char*) arg;
347 ccl_query.len = strlen(arg);
352 printf("Sent searchRequest.\n");
356 static int process_searchResponse(Z_SearchResponse *res)
358 if (res->searchStatus)
359 printf("Search was a success.\n");
361 printf("Search was a bloomin' failure.\n");
362 printf("Number of hits: %d, setno %d\n",
363 *res->resultCount, setnumber);
364 printf("records returned: %d\n",
365 *res->numberOfRecordsReturned);
366 setno += *res->numberOfRecordsReturned;
368 display_records(res->records);
372 static int cmd_find(char *arg)
376 printf("Find what?\n");
381 printf("Not connected yet\n");
384 if (!send_searchRequest(arg))
389 static int cmd_ssub(char *arg)
391 if (!(smallSetUpperBound = atoi(arg)))
396 static int cmd_lslb(char *arg)
398 if (!(largeSetLowerBound = atoi(arg)))
403 static int cmd_mspn(char *arg)
405 if (!(mediumSetPresentNumber = atoi(arg)))
410 static int cmd_status(char *arg)
412 printf("smallSetUpperBound: %d\n", smallSetUpperBound);
413 printf("largeSetLowerBound: %d\n", largeSetLowerBound);
414 printf("mediumSetPresentNumber: %d\n", mediumSetPresentNumber);
418 static int cmd_base(char *arg)
422 printf("Usage: base <database>\n");
425 strcpy(database, arg);
429 static int cmd_setnames(char *arg)
433 printf("Set numbering enabled.\n");
438 printf("Set numbering disabled.\n");
444 /* PRESENT SERVICE ----------------------------- */
446 static int send_presentRequest(char *arg)
448 Z_APDU *apdu = zget_APDU(out, Z_APDU_presentRequest);
449 Z_PresentRequest *req = apdu->u.presentRequest;
454 if ((p = strchr(arg, '+')))
464 sprintf(setstring, "%d", setnumber);
465 req->resultSetId = setstring;
467 req->resultSetStartPoint = &setno;
468 req->numberOfRecordsRequested = &nos;
470 printf("Sent presentRequest (%d+%d).\n", setno, nos);
474 static int cmd_show(char *arg)
476 if (!send_presentRequest(arg))
481 int cmd_quit(char *arg)
483 printf("See you later, alligator.\n");
487 static void initialize(void)
491 if (!(out = odr_createmem(ODR_ENCODE)) ||
492 !(in = odr_createmem(ODR_DECODE)) ||
493 !(print = odr_createmem(ODR_PRINT)))
495 fprintf(stderr, "failed to allocate ODR streams\n");
498 setvbuf(stdout, 0, _IONBF, 0);
500 bibset = ccl_qual_mk ();
501 inf = fopen ("default.bib", "r");
504 ccl_qual_file (bibset, inf);
509 static int client(void)
513 int (*fun)(char *arg);
516 {"open", cmd_open, "('tcp'|'osi')':'[<TSEL>'/']<HOST>[':'<PORT>]"},
517 {"quit", cmd_quit, ""},
518 {"find", cmd_find, "<CCL-QUERY>"},
519 {"base", cmd_base, "<BASE-NAME>"},
520 {"show", cmd_show, "<REC#>['+'<#RECS>]"},
521 {"authentication", cmd_authentication, "<ACCTSTRING>"},
522 {"lslb", cmd_lslb, "<largeSetLowerBound>"},
523 {"ssub", cmd_ssub, "<smallSetUpperBound>"},
524 {"mspn", cmd_mspn, "<mediumSetPresentNumber>"},
525 {"status", cmd_status, ""},
526 {"setnames", cmd_setnames, ""},
530 int netbufferlen = 0;
538 char line[1024], word[1024], arg[1024];
543 FD_SET(cs_fileno(conn), &input);
544 if ((res = select(20, &input, 0, 0, 0)) < 0)
551 if (FD_ISSET(0, &input))
553 /* quick & dirty way to get a command line. */
556 if ((res = sscanf(line, "%s %[^;]", word, arg)) <= 0)
563 for (i = 0; cmd[i].cmd; i++)
564 if (!strncmp(cmd[i].cmd, word, strlen(word)))
566 res = (*cmd[i].fun)(arg);
569 if (!cmd[i].cmd) /* dump our help-screen */
571 printf("Unknown command: %s.\n", word);
572 printf("Currently recognized commands:\n");
573 for (i = 0; cmd[i].cmd; i++)
574 printf(" %s %s\n", cmd[i].cmd, cmd[i].ad);
580 if (conn && FD_ISSET(cs_fileno(conn), &input))
584 if ((res = cs_get(conn, &netbuffer, &netbufferlen)) < 0)
591 printf("Target closed connection.\n");
594 odr_reset(in); /* release APDU from last round */
595 odr_setbuf(in, netbuffer, res, 0);
596 if (!z_APDU(in, &apdu, 0))
598 odr_perror(in, "Decoding incoming APDU");
602 if (!z_APDU(print, &apdu, 0))
604 odr_perror(print, "Failed to print incoming APDU");
611 case Z_APDU_initResponse:
612 process_initResponse(apdu->u.initResponse);
614 case Z_APDU_searchResponse:
615 process_searchResponse(apdu->u.searchResponse);
617 case Z_APDU_presentResponse:
618 printf("Received presentResponse.\n");
620 *apdu->u.presentResponse->numberOfRecordsReturned;
621 if (apdu->u.presentResponse->records)
622 display_records(apdu->u.presentResponse->records);
624 printf("No records.\n");
627 printf("Received unknown APDU type (%d).\n",
634 while (cs_more(conn));
640 int main(int argc, char **argv)