2 * Copyright (c) 1995, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.8 1995-06-05 10:52:22 quinn
10 * Revision 1.7 1995/06/02 09:50:09 quinn
13 * Revision 1.6 1995/05/31 08:29:21 quinn
14 * Nothing significant.
16 * Revision 1.5 1995/05/29 08:10:47 quinn
17 * Moved oid.c to util.
19 * Revision 1.4 1995/05/22 15:30:13 adam
20 * Client uses prefix query notation.
22 * Revision 1.3 1995/05/22 15:06:53 quinn
23 * *** empty log message ***
25 * Revision 1.2 1995/05/22 14:56:40 quinn
26 * *** empty log message ***
28 * Revision 1.1 1995/05/22 11:30:31 quinn
29 * Added prettier client.
35 * This is the obligatory little toy client, whose primary purpose is
36 * to illustrate the use of the YAZ service-level API.
44 #include <sys/select.h>
65 #include "../version.h"
67 #define C_PROMPT "Z> "
69 static ODR out, in, print; /* encoding and decoding streams */
70 static COMSTACK conn = 0; /* our z-association */
71 static Z_IdAuthentication *auth = 0; /* our current auth definition */
72 static char database[512] = "Default"; /* Database name */
73 static int setnumber = 0; /* current result set number */
74 static int smallSetUpperBound = 0;
75 static int largeSetLowerBound = 1;
76 static int mediumSetPresentNumber = 0;
77 static int setno = 1; /* current set offset */
78 static int protocol = PROTO_Z3950; /* current app protocol */
79 static ODR_MEM session_mem; /* memory handle for init-response */
80 static Z_InitResponse *session = 0; /* session parameters */
83 static CCL_bibset bibset; /* CCL bibset handle */
87 static void send_apdu(Z_APDU *a)
92 if (!z_APDU(out, &a, 0))
94 odr_perror(out, "Encoding APDU");
97 buf = odr_getbuf(out, &len, 0);
98 odr_reset(out); /* release the APDU */
99 if (cs_put(conn, buf, len) < 0)
101 fprintf(stderr, "cs_put: %s", cs_errlist[cs_errno(conn)]);
106 /* INIT SERVICE ------------------------------- */
108 static void send_initRequest()
110 Z_APDU *apdu = zget_APDU(out, Z_APDU_initRequest);
111 Z_InitRequest *req = apdu->u.initRequest;
113 ODR_MASK_SET(req->options, Z_Options_search);
114 ODR_MASK_SET(req->options, Z_Options_present);
115 ODR_MASK_SET(req->options, Z_Options_namedResultSets);
116 ODR_MASK_SET(req->options, Z_Options_triggerResourceCtrl);
117 ODR_MASK_SET(req->options, Z_Options_scan);
119 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_1);
120 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_2);
122 req->idAuthentication = auth;
125 printf("Sent initrequest.\n");
128 static int process_initResponse(Z_InitResponse *res)
130 /* save parameters for later use */
131 session_mem = odr_extract_mem(in);
135 printf("Connection rejected by target.\n");
137 printf("Connection accepted by target.\n");
138 if (res->implementationId)
139 printf("ID : %s\n", res->implementationId);
140 if (res->implementationName)
141 printf("Name : %s\n", res->implementationName);
142 if (res->implementationVersion)
143 printf("Version: %s\n", res->implementationVersion);
144 if (res->userInformationField)
146 printf("UserInformationfield:\n");
147 if (!odr_external(print, (Odr_external**)&res-> userInformationField,
150 odr_perror(print, "Printing userinfo\n");
153 if (res->userInformationField->which == ODR_EXTERNAL_octet)
155 printf("Guessing visiblestring:\n");
156 printf("'%s'\n", res->userInformationField->u. octet_aligned->buf);
162 int cmd_open(char *arg)
165 char type[100], addr[100];
170 printf("Already connected.\n");
173 if (!*arg || sscanf(arg, "%[^:]:%s", type, addr) < 2)
175 fprintf(stderr, "Usage: open (osi|tcp) ':' [tsel '/']host[':'port]\n");
179 if (!strcmp(type, "osi"))
181 if (!(add = mosi_strtoaddr(addr)))
191 if (!strcmp(type, "tcp"))
193 if (!(add = tcpip_strtoaddr(addr)))
199 protocol = PROTO_Z3950;
203 fprintf(stderr, "Bad type: %s\n", type);
206 if (!(conn = cs_create(t, 1, protocol)))
211 printf("Connecting...");
213 if (cs_connect(conn, add) < 0)
225 int cmd_authentication(char *arg)
227 static Z_IdAuthentication au;
228 static char open[256];
232 printf("Auth field set to null\n");
237 au.which = Z_IdAuthentication_open;
243 /* SEARCH SERVICE ------------------------------ */
245 void display_record(Z_DatabaseRecord *p)
247 Odr_external *r = (Odr_external*) p;
249 if (r->direct_reference)
251 oident *ent = oid_getentbyoid(r->direct_reference);
253 printf("Record type: ");
255 printf("%s\n", ent->desc);
256 else if (!odr_oid(print, &r->direct_reference, 0))
258 odr_perror(print, "print oid");
263 if (r->which == ODR_EXTERNAL_octet && p->u.octet_aligned->len)
266 marc_display ((char*)p->u.octet_aligned->buf, stdout);
268 FILE *ofi = fopen("dump", "a");
270 fwrite(p->u.octet_aligned->buf, 1, p->u.octet_aligned->len, ofi);
272 printf("dumped record\n");
277 printf("Unknown record representation.\n");
278 if (!odr_external(print, &r, 0))
280 odr_perror(print, "Printing external");
287 static void display_diagrec(Z_DiagRec *p)
291 printf("Diagnostic message from database.\n");
292 if (!(ent = oid_getentbyoid(p->diagnosticSetId)) ||
293 ent->class != CLASS_DIAGSET || ent->value != VAL_BIB1)
294 printf("Missing or unknown diagset\n");
295 printf("Error condition: %d", *p->condition);
296 printf(" -- %s\n", p->addinfo ? p->addinfo : "");
299 static void display_nameplusrecord(Z_NamePlusRecord *p)
302 printf("[%s]", p->databaseName);
303 if (p->which == Z_NamePlusRecord_surrogateDiagnostic)
304 display_diagrec(p->u.surrogateDiagnostic);
306 display_record(p->u.databaseRecord);
309 static void display_records(Z_Records *p)
313 if (p->which == Z_Records_NSD)
314 display_diagrec(p->u.nonSurrogateDiagnostic);
317 printf("Records: %d\n", p->u.databaseOrSurDiagnostics->num_records);
318 for (i = 0; i < p->u.databaseOrSurDiagnostics->num_records; i++)
319 display_nameplusrecord(p->u.databaseOrSurDiagnostics->records[i]);
323 static int send_searchRequest(char *arg)
325 Z_APDU *apdu = zget_APDU(out, Z_APDU_searchRequest);
326 Z_SearchRequest *req = apdu->u.searchRequest;
327 char *databaseNames = database;
331 struct ccl_rpn_node *rpn;
337 Z_RPNQuery *RPNquery;
345 rpn = ccl_find_str(bibset, arg, &error, &pos);
348 printf("CCL ERROR: %s\n", ccl_err_msg(error));
354 if (!strcmp(arg, "@big")) /* strictly for troublemaking */
356 static unsigned char big[2100];
359 /* send a very big referenceid to test transport stack etc. */
360 memset(big, 'A', 2100);
361 bigo.len = bigo.size = 2100;
363 req->referenceId = &bigo;
368 sprintf(setstring, "%d", ++setnumber);
369 req->resultSetName = setstring;
371 *req->smallSetUpperBound = smallSetUpperBound;
372 *req->largeSetLowerBound = largeSetLowerBound;
373 *req->mediumSetPresentNumber = mediumSetPresentNumber;
374 req->num_databaseNames = 1;
375 req->databaseNames = &databaseNames;
380 query.which = Z_Query_type_1;
383 assert((RPNquery = ccl_rpn_query(rpn)));
385 RPNquery = p_query_rpn (out, arg);
388 printf("Prefix query error\n");
392 bib1.proto = protocol;
393 bib1.class = CLASS_ATTSET;
394 bib1.value = VAL_BIB1;
395 RPNquery->attributeSetId = oid_getoidbyent(&bib1);
396 query.u.type_1 = RPNquery;
398 query.which = Z_Query_type_2;
399 query.u.type_2 = &ccl_query;
400 ccl_query.buf = (unsigned char*) arg;
401 ccl_query.len = strlen(arg);
406 printf("Sent searchRequest.\n");
410 static int process_searchResponse(Z_SearchResponse *res)
412 if (res->searchStatus)
413 printf("Search was a success.\n");
415 printf("Search was a bloomin' failure.\n");
416 printf("Number of hits: %d, setno %d\n",
417 *res->resultCount, setnumber);
418 printf("records returned: %d\n",
419 *res->numberOfRecordsReturned);
420 setno += *res->numberOfRecordsReturned;
422 display_records(res->records);
426 static int cmd_find(char *arg)
430 printf("Find what?\n");
435 printf("Not connected yet\n");
438 if (!send_searchRequest(arg))
443 static int cmd_ssub(char *arg)
445 if (!(smallSetUpperBound = atoi(arg)))
450 static int cmd_lslb(char *arg)
452 if (!(largeSetLowerBound = atoi(arg)))
457 static int cmd_mspn(char *arg)
459 if (!(mediumSetPresentNumber = atoi(arg)))
464 static int cmd_status(char *arg)
466 printf("smallSetUpperBound: %d\n", smallSetUpperBound);
467 printf("largeSetLowerBound: %d\n", largeSetLowerBound);
468 printf("mediumSetPresentNumber: %d\n", mediumSetPresentNumber);
472 static int cmd_base(char *arg)
476 printf("Usage: base <database>\n");
479 strcpy(database, arg);
483 static int cmd_setnames(char *arg)
487 printf("Set numbering enabled.\n");
492 printf("Set numbering disabled.\n");
498 /* PRESENT SERVICE ----------------------------- */
500 static int send_presentRequest(char *arg)
502 Z_APDU *apdu = zget_APDU(out, Z_APDU_presentRequest);
503 Z_PresentRequest *req = apdu->u.presentRequest;
508 if ((p = strchr(arg, '+')))
518 sprintf(setstring, "%d", setnumber);
519 req->resultSetId = setstring;
521 req->resultSetStartPoint = &setno;
522 req->numberOfRecordsRequested = &nos;
524 printf("Sent presentRequest (%d+%d).\n", setno, nos);
528 static int cmd_show(char *arg)
530 if (!send_presentRequest(arg))
535 int cmd_quit(char *arg)
537 printf("See you later, alligator.\n");
541 int cmd_cancel(char *arg)
543 Z_APDU *apdu = zget_APDU(out, Z_APDU_triggerResourceControlRequest);
544 Z_TriggerResourceControlRequest *req =
545 apdu->u.triggerResourceControlRequest;
550 printf("Session not initialized yet\n");
553 if (!ODR_MASK_GET(session->options, Z_Options_triggerResourceCtrl))
555 printf("Target doesn't support cancel (trigger resource ctrl)\n");
558 *req->requestedAction = Z_TriggerResourceCtrl_cancel;
559 req->resultSetWanted = &false;
562 printf("Sent cancel request\n");
566 int send_scanrequest(char *string, int pp, int num)
568 Z_APDU *apdu = zget_APDU(out, Z_APDU_scanRequest);
569 Z_ScanRequest *req = apdu->u.scanRequest;
572 Z_AttributesPlusTerm sp;
576 req->num_databaseNames = 1;
577 req->databaseNames = &db;
578 attset.proto = protocol;
579 attset.class = CLASS_ATTSET;
580 attset.value = VAL_BIB1;
581 req->attributeSet = oid_getoidbyent(&attset);
582 req->termListAndStartPoint = &sp;
583 sp.num_attributes = 0;
584 sp.attributeList = ODR_NULLVAL;
586 term.which = Z_Term_general;
587 term.u.general = &trm;
588 trm.buf = (unsigned char*) string;
589 trm.len = trm.size = strlen(string);
590 req->numberOfTermsRequested = #
591 req->preferredPositionInResponse = &pp;
596 void display_term(Z_TermInfo *t)
598 if (t->term->which == Z_Term_general)
599 printf("%.*s (%d)\n", t->term->u.general->len, t->term->u.general->buf,
600 t->globalOccurrences ? *t->globalOccurrences : -1);
602 printf("Term type not general.\n");
605 void process_scanResponse(Z_ScanResponse *res)
609 printf("SCAN: %d entries, position=%d\n", *res->numberOfEntriesReturned,
610 *res->positionOfTerm);
611 if (*res->scanStatus != Z_Scan_success)
612 printf("Scan returned code %d\n", *res->scanStatus);
615 if (res->entries->which == Z_ListEntries_entries)
617 Z_Entries *ent = res->entries->u.entries;
619 for (i = 0; i < ent->num_entries; i++)
620 if (ent->entries[i]->which == Z_Entry_termInfo)
622 printf("%c ", i + 1 == *res->positionOfTerm ? '*' : ' ');
623 display_term(ent->entries[i]->u.termInfo);
626 display_diagrec(ent->entries[i]->u.surrogateDiagnostic);
629 display_diagrec(res->entries->u.nonSurrogateDiagnostics->diagRecs[0]);
632 int cmd_scan(char *arg)
636 printf("Session not initialized yet\n");
639 if (!ODR_MASK_GET(session->options, Z_Options_scan))
641 printf("Target doesn't support scan\n");
645 if (send_scanrequest(arg, 5, 19) < 0)
650 static void initialize(void)
658 if (!(out = odr_createmem(ODR_ENCODE)) ||
659 !(in = odr_createmem(ODR_DECODE)) ||
660 !(print = odr_createmem(ODR_PRINT)))
662 fprintf(stderr, "failed to allocate ODR streams\n");
665 setvbuf(stdout, 0, _IONBF, 0);
669 bibset = ccl_qual_mk ();
670 inf = fopen ("default.bib", "r");
673 ccl_qual_file (bibset, inf);
680 static int client(void)
684 int (*fun)(char *arg);
687 {"open", cmd_open, "('tcp'|'osi')':'[<TSEL>'/']<HOST>[':'<PORT>]"},
688 {"quit", cmd_quit, ""},
689 {"find", cmd_find, "<CCL-QUERY>"},
690 {"base", cmd_base, "<BASE-NAME>"},
691 {"show", cmd_show, "<REC#>['+'<#RECS>]"},
692 {"scan", cmd_scan, "<TERM>"},
693 {"authentication", cmd_authentication, "<ACCTSTRING>"},
694 {"lslb", cmd_lslb, "<largeSetLowerBound>"},
695 {"ssub", cmd_ssub, "<smallSetUpperBound>"},
696 {"mspn", cmd_mspn, "<mediumSetPresentNumber>"},
697 {"status", cmd_status, ""},
698 {"setnames", cmd_setnames, ""},
699 {"cancel", cmd_cancel, ""},
703 int netbufferlen = 0;
711 char line[1024], word[1024], arg[1024];
716 FD_SET(cs_fileno(conn), &input);
717 if ((res = select(20, &input, 0, 0, 0)) < 0)
724 if (FD_ISSET(0, &input))
726 /* quick & dirty way to get a command line. */
729 if ((res = sscanf(line, "%s %[^;]", word, arg)) <= 0)
736 for (i = 0; cmd[i].cmd; i++)
737 if (!strncmp(cmd[i].cmd, word, strlen(word)))
739 res = (*cmd[i].fun)(arg);
742 if (!cmd[i].cmd) /* dump our help-screen */
744 printf("Unknown command: %s.\n", word);
745 printf("Currently recognized commands:\n");
746 for (i = 0; cmd[i].cmd; i++)
747 printf(" %s %s\n", cmd[i].cmd, cmd[i].ad);
753 if (conn && FD_ISSET(cs_fileno(conn), &input))
757 if ((res = cs_get(conn, &netbuffer, &netbufferlen)) < 0)
764 printf("Target closed connection.\n");
767 odr_reset(in); /* release APDU from last round */
768 odr_setbuf(in, netbuffer, res, 0);
769 if (!z_APDU(in, &apdu, 0))
771 odr_perror(in, "Decoding incoming APDU");
775 if (!z_APDU(print, &apdu, 0))
777 odr_perror(print, "Failed to print incoming APDU");
784 case Z_APDU_initResponse:
785 process_initResponse(apdu->u.initResponse);
787 case Z_APDU_searchResponse:
788 process_searchResponse(apdu->u.searchResponse);
790 case Z_APDU_scanResponse:
791 process_scanResponse(apdu->u.scanResponse);
793 case Z_APDU_presentResponse:
794 printf("Received presentResponse.\n");
796 *apdu->u.presentResponse->numberOfRecordsReturned;
797 if (apdu->u.presentResponse->records)
798 display_records(apdu->u.presentResponse->records);
800 printf("No records.\n");
803 printf("Received unknown APDU type (%d).\n",
810 while (cs_more(conn));
816 int main(int argc, char **argv)