2 * Copyright (c) 1995, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.6 1995-05-31 08:29:21 quinn
10 * Revision 1.5 1995/05/29 08:10:47 quinn
11 * Moved oid.c to util.
13 * Revision 1.4 1995/05/22 15:30:13 adam
14 * Client uses prefix query notation.
16 * Revision 1.3 1995/05/22 15:06:53 quinn
17 * *** empty log message ***
19 * Revision 1.2 1995/05/22 14:56:40 quinn
20 * *** empty log message ***
22 * Revision 1.1 1995/05/22 11:30:31 quinn
23 * Added prettier client.
29 * This is the obligatory little toy client, whose primary purpose is
30 * to illustrate the use of the YAZ service-level API.
56 #include "../version.h"
58 #define C_PROMPT "Z> "
60 static ODR out, in, print; /* encoding and decoding streams */
61 static COMSTACK conn = 0; /* our z-association */
62 static Z_IdAuthentication *auth = 0; /* our current auth definition */
63 static char database[512] = "Default"; /* Database name */
64 static int setnumber = 0; /* current result set number */
65 static int smallSetUpperBound = 0;
66 static int largeSetLowerBound = 1;
67 static int mediumSetPresentNumber = 0;
68 static int setno = 1; /* current set offset */
69 static int protocol = PROTO_Z3950; /* current app protocol */
72 static CCL_bibset bibset; /* CCL bibset handle */
76 static void send_apdu(Z_APDU *a)
81 if (!z_APDU(out, &a, 0))
83 odr_perror(out, "Encoding APDU");
86 buf = odr_getbuf(out, &len, 0);
87 odr_reset(out); /* release the APDU */
88 if (cs_put(conn, buf, len) < 0)
90 fprintf(stderr, "cs_put: %s", cs_errlist[cs_errno(conn)]);
95 /* INIT SERVICE ------------------------------- */
97 static void send_initRequest()
99 Z_APDU *apdu = zget_APDU(out, Z_APDU_initRequest);
100 Z_InitRequest *req = apdu->u.initRequest;
102 ODR_MASK_SET(req->options, Z_Options_search);
103 ODR_MASK_SET(req->options, Z_Options_present);
105 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_1);
106 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_2);
108 req->idAuthentication = auth;
111 printf("Sent initrequest.\n");
114 static int process_initResponse(Z_InitResponse *res)
117 printf("Connection rejected by target.\n");
119 printf("Connection accepted by target.\n");
120 if (res->implementationId)
121 printf("ID : %s\n", res->implementationId);
122 if (res->implementationName)
123 printf("Name : %s\n", res->implementationName);
124 if (res->implementationVersion)
125 printf("Version: %s\n", res->implementationVersion);
126 if (res->userInformationField)
128 printf("UserInformationfield:\n");
129 if (!odr_external(print, (Odr_external**)&res-> userInformationField,
132 odr_perror(print, "Printing userinfo\n");
135 if (res->userInformationField->which == ODR_EXTERNAL_octet)
137 printf("Guessing visiblestring:\n");
138 printf("'%s'\n", res->userInformationField->u. octet_aligned->buf);
144 int cmd_open(char *arg)
147 char type[100], addr[100];
152 printf("Already connected.\n");
155 if (!*arg || sscanf(arg, "%[^:]:%s", type, addr) < 2)
157 fprintf(stderr, "Usage: open (osi|tcp) ':' [tsel '/']host[':'port]\n");
161 if (!strcmp(type, "osi"))
163 if (!(add = mosi_strtoaddr(addr)))
173 if (!strcmp(type, "tcp"))
175 if (!(add = tcpip_strtoaddr(addr)))
181 protocol = PROTO_Z3950;
185 fprintf(stderr, "Bad type: %s\n", type);
188 if (!(conn = cs_create(t, 1, protocol)))
193 printf("Connecting...");
195 if (cs_connect(conn, add) < 0)
207 int cmd_authentication(char *arg)
209 static Z_IdAuthentication au;
210 static char open[256];
214 printf("Auth field set to null\n");
219 au.which = Z_IdAuthentication_open;
225 /* SEARCH SERVICE ------------------------------ */
227 void display_record(Z_DatabaseRecord *p)
229 Odr_external *r = (Odr_external*) p;
231 if (r->direct_reference)
233 oident *ent = oid_getentbyoid(r->direct_reference);
235 printf("Record type: ");
237 printf("%s\n", ent->desc);
238 else if (!odr_oid(print, &r->direct_reference, 0))
240 odr_perror(print, "print oid");
245 if (r->which == ODR_EXTERNAL_octet && p->u.octet_aligned->len)
248 marc_display ((char*)p->u.octet_aligned->buf, stdout);
250 FILE *ofi = fopen("dump", "a");
252 fwrite(p->u.octet_aligned->buf, 1, p->u.octet_aligned->len, ofi);
254 printf("dumped record\n");
259 printf("Unknown record representation.\n");
260 if (!odr_external(print, &r, 0))
262 odr_perror(print, "Printing external");
269 static void display_diagrec(Z_DiagRec *p)
273 printf("Diagnostic message from database.\n");
274 if (!(ent = oid_getentbyoid(p->diagnosticSetId)) ||
275 ent->class != CLASS_DIAGSET || ent->value != VAL_BIB1)
276 printf("Missing or unknown diagset\n");
277 printf("Error condition: %d", *p->condition);
278 printf(" -- %s\n", p->addinfo ? p->addinfo : "");
281 static void display_nameplusrecord(Z_NamePlusRecord *p)
284 printf("[%s]", p->databaseName);
285 if (p->which == Z_NamePlusRecord_surrogateDiagnostic)
286 display_diagrec(p->u.surrogateDiagnostic);
288 display_record(p->u.databaseRecord);
291 static void display_records(Z_Records *p)
295 if (p->which == Z_Records_NSD)
296 display_diagrec(p->u.nonSurrogateDiagnostic);
299 printf("Records: %d\n", p->u.databaseOrSurDiagnostics->num_records);
300 for (i = 0; i < p->u.databaseOrSurDiagnostics->num_records; i++)
301 display_nameplusrecord(p->u.databaseOrSurDiagnostics->records[i]);
305 static int send_searchRequest(char *arg)
307 Z_APDU *apdu = zget_APDU(out, Z_APDU_searchRequest);
308 Z_SearchRequest *req = apdu->u.searchRequest;
309 char *databaseNames = database;
313 struct ccl_rpn_node *rpn;
319 Z_RPNQuery *RPNquery;
327 rpn = ccl_find_str(bibset, arg, &error, &pos);
330 printf("CCL ERROR: %s\n", ccl_err_msg(error));
336 if (!strcmp(arg, "@big")) /* strictly for troublemaking */
338 static unsigned char big[2100];
341 /* send a very big referenceid to test transport stack etc. */
342 memset(big, 'A', 2100);
343 bigo.len = bigo.size = 2100;
345 req->referenceId = &bigo;
350 sprintf(setstring, "%d", ++setnumber);
351 req->resultSetName = setstring;
353 *req->smallSetUpperBound = smallSetUpperBound;
354 *req->largeSetLowerBound = largeSetLowerBound;
355 *req->mediumSetPresentNumber = mediumSetPresentNumber;
356 req->num_databaseNames = 1;
357 req->databaseNames = &databaseNames;
362 query.which = Z_Query_type_1;
365 assert((RPNquery = ccl_rpn_query(rpn)));
367 RPNquery = p_query_rpn (out, arg);
370 printf("Prefix query error\n");
374 bib1.proto = protocol;
375 bib1.class = CLASS_ATTSET;
376 bib1.value = VAL_BIB1;
377 RPNquery->attributeSetId = oid_getoidbyent(&bib1);
378 query.u.type_1 = RPNquery;
380 query.which = Z_Query_type_2;
381 query.u.type_2 = &ccl_query;
382 ccl_query.buf = (unsigned char*) arg;
383 ccl_query.len = strlen(arg);
388 printf("Sent searchRequest.\n");
392 static int process_searchResponse(Z_SearchResponse *res)
394 if (res->searchStatus)
395 printf("Search was a success.\n");
397 printf("Search was a bloomin' failure.\n");
398 printf("Number of hits: %d, setno %d\n",
399 *res->resultCount, setnumber);
400 printf("records returned: %d\n",
401 *res->numberOfRecordsReturned);
402 setno += *res->numberOfRecordsReturned;
404 display_records(res->records);
408 static int cmd_find(char *arg)
412 printf("Find what?\n");
417 printf("Not connected yet\n");
420 if (!send_searchRequest(arg))
425 static int cmd_ssub(char *arg)
427 if (!(smallSetUpperBound = atoi(arg)))
432 static int cmd_lslb(char *arg)
434 if (!(largeSetLowerBound = atoi(arg)))
439 static int cmd_mspn(char *arg)
441 if (!(mediumSetPresentNumber = atoi(arg)))
446 static int cmd_status(char *arg)
448 printf("smallSetUpperBound: %d\n", smallSetUpperBound);
449 printf("largeSetLowerBound: %d\n", largeSetLowerBound);
450 printf("mediumSetPresentNumber: %d\n", mediumSetPresentNumber);
454 static int cmd_base(char *arg)
458 printf("Usage: base <database>\n");
461 strcpy(database, arg);
465 static int cmd_setnames(char *arg)
469 printf("Set numbering enabled.\n");
474 printf("Set numbering disabled.\n");
480 /* PRESENT SERVICE ----------------------------- */
482 static int send_presentRequest(char *arg)
484 Z_APDU *apdu = zget_APDU(out, Z_APDU_presentRequest);
485 Z_PresentRequest *req = apdu->u.presentRequest;
490 if ((p = strchr(arg, '+')))
500 sprintf(setstring, "%d", setnumber);
501 req->resultSetId = setstring;
503 req->resultSetStartPoint = &setno;
504 req->numberOfRecordsRequested = &nos;
506 printf("Sent presentRequest (%d+%d).\n", setno, nos);
510 static int cmd_show(char *arg)
512 if (!send_presentRequest(arg))
517 int cmd_quit(char *arg)
519 printf("See you later, alligator.\n");
523 static void initialize(void)
531 if (!(out = odr_createmem(ODR_ENCODE)) ||
532 !(in = odr_createmem(ODR_DECODE)) ||
533 !(print = odr_createmem(ODR_PRINT)))
535 fprintf(stderr, "failed to allocate ODR streams\n");
538 setvbuf(stdout, 0, _IONBF, 0);
542 bibset = ccl_qual_mk ();
543 inf = fopen ("default.bib", "r");
546 ccl_qual_file (bibset, inf);
553 static int client(void)
557 int (*fun)(char *arg);
560 {"open", cmd_open, "('tcp'|'osi')':'[<TSEL>'/']<HOST>[':'<PORT>]"},
561 {"quit", cmd_quit, ""},
562 {"find", cmd_find, "<CCL-QUERY>"},
563 {"base", cmd_base, "<BASE-NAME>"},
564 {"show", cmd_show, "<REC#>['+'<#RECS>]"},
565 {"authentication", cmd_authentication, "<ACCTSTRING>"},
566 {"lslb", cmd_lslb, "<largeSetLowerBound>"},
567 {"ssub", cmd_ssub, "<smallSetUpperBound>"},
568 {"mspn", cmd_mspn, "<mediumSetPresentNumber>"},
569 {"status", cmd_status, ""},
570 {"setnames", cmd_setnames, ""},
574 int netbufferlen = 0;
582 char line[1024], word[1024], arg[1024];
587 FD_SET(cs_fileno(conn), &input);
588 if ((res = select(20, &input, 0, 0, 0)) < 0)
595 if (FD_ISSET(0, &input))
597 /* quick & dirty way to get a command line. */
600 if ((res = sscanf(line, "%s %[^;]", word, arg)) <= 0)
607 for (i = 0; cmd[i].cmd; i++)
608 if (!strncmp(cmd[i].cmd, word, strlen(word)))
610 res = (*cmd[i].fun)(arg);
613 if (!cmd[i].cmd) /* dump our help-screen */
615 printf("Unknown command: %s.\n", word);
616 printf("Currently recognized commands:\n");
617 for (i = 0; cmd[i].cmd; i++)
618 printf(" %s %s\n", cmd[i].cmd, cmd[i].ad);
624 if (conn && FD_ISSET(cs_fileno(conn), &input))
628 if ((res = cs_get(conn, &netbuffer, &netbufferlen)) < 0)
635 printf("Target closed connection.\n");
638 odr_reset(in); /* release APDU from last round */
639 odr_setbuf(in, netbuffer, res, 0);
640 if (!z_APDU(in, &apdu, 0))
642 odr_perror(in, "Decoding incoming APDU");
646 if (!z_APDU(print, &apdu, 0))
648 odr_perror(print, "Failed to print incoming APDU");
655 case Z_APDU_initResponse:
656 process_initResponse(apdu->u.initResponse);
658 case Z_APDU_searchResponse:
659 process_searchResponse(apdu->u.searchResponse);
661 case Z_APDU_presentResponse:
662 printf("Received presentResponse.\n");
664 *apdu->u.presentResponse->numberOfRecordsReturned;
665 if (apdu->u.presentResponse->records)
666 display_records(apdu->u.presentResponse->records);
668 printf("No records.\n");
671 printf("Received unknown APDU type (%d).\n",
678 while (cs_more(conn));
684 int main(int argc, char **argv)