2 * Copyright (c) 1995, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.7 1995-06-02 09:50:09 quinn
10 * Revision 1.6 1995/05/31 08:29:21 quinn
11 * Nothing significant.
13 * Revision 1.5 1995/05/29 08:10:47 quinn
14 * Moved oid.c to util.
16 * Revision 1.4 1995/05/22 15:30:13 adam
17 * Client uses prefix query notation.
19 * Revision 1.3 1995/05/22 15:06:53 quinn
20 * *** empty log message ***
22 * Revision 1.2 1995/05/22 14:56:40 quinn
23 * *** empty log message ***
25 * Revision 1.1 1995/05/22 11:30:31 quinn
26 * Added prettier client.
32 * This is the obligatory little toy client, whose primary purpose is
33 * to illustrate the use of the YAZ service-level API.
59 #include "../version.h"
61 #define C_PROMPT "Z> "
63 static ODR out, in, print; /* encoding and decoding streams */
64 static COMSTACK conn = 0; /* our z-association */
65 static Z_IdAuthentication *auth = 0; /* our current auth definition */
66 static char database[512] = "Default"; /* Database name */
67 static int setnumber = 0; /* current result set number */
68 static int smallSetUpperBound = 0;
69 static int largeSetLowerBound = 1;
70 static int mediumSetPresentNumber = 0;
71 static int setno = 1; /* current set offset */
72 static int protocol = PROTO_Z3950; /* current app protocol */
75 static CCL_bibset bibset; /* CCL bibset handle */
79 static void send_apdu(Z_APDU *a)
84 if (!z_APDU(out, &a, 0))
86 odr_perror(out, "Encoding APDU");
89 buf = odr_getbuf(out, &len, 0);
90 odr_reset(out); /* release the APDU */
91 if (cs_put(conn, buf, len) < 0)
93 fprintf(stderr, "cs_put: %s", cs_errlist[cs_errno(conn)]);
98 /* INIT SERVICE ------------------------------- */
100 static void send_initRequest()
102 Z_APDU *apdu = zget_APDU(out, Z_APDU_initRequest);
103 Z_InitRequest *req = apdu->u.initRequest;
105 ODR_MASK_SET(req->options, Z_Options_search);
106 ODR_MASK_SET(req->options, Z_Options_present);
108 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_1);
109 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_2);
111 req->idAuthentication = auth;
114 printf("Sent initrequest.\n");
117 static int process_initResponse(Z_InitResponse *res)
120 printf("Connection rejected by target.\n");
122 printf("Connection accepted by target.\n");
123 if (res->implementationId)
124 printf("ID : %s\n", res->implementationId);
125 if (res->implementationName)
126 printf("Name : %s\n", res->implementationName);
127 if (res->implementationVersion)
128 printf("Version: %s\n", res->implementationVersion);
129 if (res->userInformationField)
131 printf("UserInformationfield:\n");
132 if (!odr_external(print, (Odr_external**)&res-> userInformationField,
135 odr_perror(print, "Printing userinfo\n");
138 if (res->userInformationField->which == ODR_EXTERNAL_octet)
140 printf("Guessing visiblestring:\n");
141 printf("'%s'\n", res->userInformationField->u. octet_aligned->buf);
147 int cmd_open(char *arg)
150 char type[100], addr[100];
155 printf("Already connected.\n");
158 if (!*arg || sscanf(arg, "%[^:]:%s", type, addr) < 2)
160 fprintf(stderr, "Usage: open (osi|tcp) ':' [tsel '/']host[':'port]\n");
164 if (!strcmp(type, "osi"))
166 if (!(add = mosi_strtoaddr(addr)))
176 if (!strcmp(type, "tcp"))
178 if (!(add = tcpip_strtoaddr(addr)))
184 protocol = PROTO_Z3950;
188 fprintf(stderr, "Bad type: %s\n", type);
191 if (!(conn = cs_create(t, 1, protocol)))
196 printf("Connecting...");
198 if (cs_connect(conn, add) < 0)
210 int cmd_authentication(char *arg)
212 static Z_IdAuthentication au;
213 static char open[256];
217 printf("Auth field set to null\n");
222 au.which = Z_IdAuthentication_open;
228 /* SEARCH SERVICE ------------------------------ */
230 void display_record(Z_DatabaseRecord *p)
232 Odr_external *r = (Odr_external*) p;
234 if (r->direct_reference)
236 oident *ent = oid_getentbyoid(r->direct_reference);
238 printf("Record type: ");
240 printf("%s\n", ent->desc);
241 else if (!odr_oid(print, &r->direct_reference, 0))
243 odr_perror(print, "print oid");
248 if (r->which == ODR_EXTERNAL_octet && p->u.octet_aligned->len)
251 marc_display ((char*)p->u.octet_aligned->buf, stdout);
253 FILE *ofi = fopen("dump", "a");
255 fwrite(p->u.octet_aligned->buf, 1, p->u.octet_aligned->len, ofi);
257 printf("dumped record\n");
262 printf("Unknown record representation.\n");
263 if (!odr_external(print, &r, 0))
265 odr_perror(print, "Printing external");
272 static void display_diagrec(Z_DiagRec *p)
276 printf("Diagnostic message from database.\n");
277 if (!(ent = oid_getentbyoid(p->diagnosticSetId)) ||
278 ent->class != CLASS_DIAGSET || ent->value != VAL_BIB1)
279 printf("Missing or unknown diagset\n");
280 printf("Error condition: %d", *p->condition);
281 printf(" -- %s\n", p->addinfo ? p->addinfo : "");
284 static void display_nameplusrecord(Z_NamePlusRecord *p)
287 printf("[%s]", p->databaseName);
288 if (p->which == Z_NamePlusRecord_surrogateDiagnostic)
289 display_diagrec(p->u.surrogateDiagnostic);
291 display_record(p->u.databaseRecord);
294 static void display_records(Z_Records *p)
298 if (p->which == Z_Records_NSD)
299 display_diagrec(p->u.nonSurrogateDiagnostic);
302 printf("Records: %d\n", p->u.databaseOrSurDiagnostics->num_records);
303 for (i = 0; i < p->u.databaseOrSurDiagnostics->num_records; i++)
304 display_nameplusrecord(p->u.databaseOrSurDiagnostics->records[i]);
308 static int send_searchRequest(char *arg)
310 Z_APDU *apdu = zget_APDU(out, Z_APDU_searchRequest);
311 Z_SearchRequest *req = apdu->u.searchRequest;
312 char *databaseNames = database;
316 struct ccl_rpn_node *rpn;
322 Z_RPNQuery *RPNquery;
330 rpn = ccl_find_str(bibset, arg, &error, &pos);
333 printf("CCL ERROR: %s\n", ccl_err_msg(error));
339 if (!strcmp(arg, "@big")) /* strictly for troublemaking */
341 static unsigned char big[2100];
344 /* send a very big referenceid to test transport stack etc. */
345 memset(big, 'A', 2100);
346 bigo.len = bigo.size = 2100;
348 req->referenceId = &bigo;
353 sprintf(setstring, "%d", ++setnumber);
354 req->resultSetName = setstring;
356 *req->smallSetUpperBound = smallSetUpperBound;
357 *req->largeSetLowerBound = largeSetLowerBound;
358 *req->mediumSetPresentNumber = mediumSetPresentNumber;
359 req->num_databaseNames = 1;
360 req->databaseNames = &databaseNames;
365 query.which = Z_Query_type_1;
368 assert((RPNquery = ccl_rpn_query(rpn)));
370 RPNquery = p_query_rpn (out, arg);
373 printf("Prefix query error\n");
377 bib1.proto = protocol;
378 bib1.class = CLASS_ATTSET;
379 bib1.value = VAL_BIB1;
380 RPNquery->attributeSetId = oid_getoidbyent(&bib1);
381 query.u.type_1 = RPNquery;
383 query.which = Z_Query_type_2;
384 query.u.type_2 = &ccl_query;
385 ccl_query.buf = (unsigned char*) arg;
386 ccl_query.len = strlen(arg);
391 printf("Sent searchRequest.\n");
395 static int process_searchResponse(Z_SearchResponse *res)
397 if (res->searchStatus)
398 printf("Search was a success.\n");
400 printf("Search was a bloomin' failure.\n");
401 printf("Number of hits: %d, setno %d\n",
402 *res->resultCount, setnumber);
403 printf("records returned: %d\n",
404 *res->numberOfRecordsReturned);
405 setno += *res->numberOfRecordsReturned;
407 display_records(res->records);
411 static int cmd_find(char *arg)
415 printf("Find what?\n");
420 printf("Not connected yet\n");
423 if (!send_searchRequest(arg))
428 static int cmd_ssub(char *arg)
430 if (!(smallSetUpperBound = atoi(arg)))
435 static int cmd_lslb(char *arg)
437 if (!(largeSetLowerBound = atoi(arg)))
442 static int cmd_mspn(char *arg)
444 if (!(mediumSetPresentNumber = atoi(arg)))
449 static int cmd_status(char *arg)
451 printf("smallSetUpperBound: %d\n", smallSetUpperBound);
452 printf("largeSetLowerBound: %d\n", largeSetLowerBound);
453 printf("mediumSetPresentNumber: %d\n", mediumSetPresentNumber);
457 static int cmd_base(char *arg)
461 printf("Usage: base <database>\n");
464 strcpy(database, arg);
468 static int cmd_setnames(char *arg)
472 printf("Set numbering enabled.\n");
477 printf("Set numbering disabled.\n");
483 /* PRESENT SERVICE ----------------------------- */
485 static int send_presentRequest(char *arg)
487 Z_APDU *apdu = zget_APDU(out, Z_APDU_presentRequest);
488 Z_PresentRequest *req = apdu->u.presentRequest;
493 if ((p = strchr(arg, '+')))
503 sprintf(setstring, "%d", setnumber);
504 req->resultSetId = setstring;
506 req->resultSetStartPoint = &setno;
507 req->numberOfRecordsRequested = &nos;
509 printf("Sent presentRequest (%d+%d).\n", setno, nos);
513 static int cmd_show(char *arg)
515 if (!send_presentRequest(arg))
520 int cmd_quit(char *arg)
522 printf("See you later, alligator.\n");
526 static void initialize(void)
534 if (!(out = odr_createmem(ODR_ENCODE)) ||
535 !(in = odr_createmem(ODR_DECODE)) ||
536 !(print = odr_createmem(ODR_PRINT)))
538 fprintf(stderr, "failed to allocate ODR streams\n");
541 setvbuf(stdout, 0, _IONBF, 0);
545 bibset = ccl_qual_mk ();
546 inf = fopen ("default.bib", "r");
549 ccl_qual_file (bibset, inf);
556 static int client(void)
560 int (*fun)(char *arg);
563 {"open", cmd_open, "('tcp'|'osi')':'[<TSEL>'/']<HOST>[':'<PORT>]"},
564 {"quit", cmd_quit, ""},
565 {"find", cmd_find, "<CCL-QUERY>"},
566 {"base", cmd_base, "<BASE-NAME>"},
567 {"show", cmd_show, "<REC#>['+'<#RECS>]"},
568 {"authentication", cmd_authentication, "<ACCTSTRING>"},
569 {"lslb", cmd_lslb, "<largeSetLowerBound>"},
570 {"ssub", cmd_ssub, "<smallSetUpperBound>"},
571 {"mspn", cmd_mspn, "<mediumSetPresentNumber>"},
572 {"status", cmd_status, ""},
573 {"setnames", cmd_setnames, ""},
577 int netbufferlen = 0;
585 char line[1024], word[1024], arg[1024];
590 FD_SET(cs_fileno(conn), &input);
591 if ((res = select(20, &input, 0, 0, 0)) < 0)
598 if (FD_ISSET(0, &input))
600 /* quick & dirty way to get a command line. */
603 if ((res = sscanf(line, "%s %[^;]", word, arg)) <= 0)
610 for (i = 0; cmd[i].cmd; i++)
611 if (!strncmp(cmd[i].cmd, word, strlen(word)))
613 res = (*cmd[i].fun)(arg);
616 if (!cmd[i].cmd) /* dump our help-screen */
618 printf("Unknown command: %s.\n", word);
619 printf("Currently recognized commands:\n");
620 for (i = 0; cmd[i].cmd; i++)
621 printf(" %s %s\n", cmd[i].cmd, cmd[i].ad);
627 if (conn && FD_ISSET(cs_fileno(conn), &input))
631 if ((res = cs_get(conn, &netbuffer, &netbufferlen)) < 0)
638 printf("Target closed connection.\n");
641 odr_reset(in); /* release APDU from last round */
642 odr_setbuf(in, netbuffer, res, 0);
643 if (!z_APDU(in, &apdu, 0))
645 odr_perror(in, "Decoding incoming APDU");
649 if (!z_APDU(print, &apdu, 0))
651 odr_perror(print, "Failed to print incoming APDU");
658 case Z_APDU_initResponse:
659 process_initResponse(apdu->u.initResponse);
661 case Z_APDU_searchResponse:
662 process_searchResponse(apdu->u.searchResponse);
664 case Z_APDU_presentResponse:
665 printf("Received presentResponse.\n");
667 *apdu->u.presentResponse->numberOfRecordsReturned;
668 if (apdu->u.presentResponse->records)
669 display_records(apdu->u.presentResponse->records);
671 printf("No records.\n");
674 printf("Received unknown APDU type (%d).\n",
681 while (cs_more(conn));
687 int main(int argc, char **argv)