2 * Copyright (c) 1995, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.14 1995-06-19 12:37:41 quinn
10 * Revision 1.13 1995/06/16 10:29:11 quinn
11 * *** empty log message ***
13 * Revision 1.12 1995/06/15 07:44:57 quinn
16 * Revision 1.11 1995/06/14 15:26:40 quinn
17 * *** empty log message ***
19 * Revision 1.10 1995/06/06 14:56:58 quinn
22 * Revision 1.9 1995/06/06 08:15:19 quinn
25 * Revision 1.8 1995/06/05 10:52:22 quinn
28 * Revision 1.7 1995/06/02 09:50:09 quinn
31 * Revision 1.6 1995/05/31 08:29:21 quinn
32 * Nothing significant.
34 * Revision 1.5 1995/05/29 08:10:47 quinn
35 * Moved oid.c to util.
37 * Revision 1.4 1995/05/22 15:30:13 adam
38 * Client uses prefix query notation.
40 * Revision 1.3 1995/05/22 15:06:53 quinn
41 * *** empty log message ***
43 * Revision 1.2 1995/05/22 14:56:40 quinn
44 * *** empty log message ***
46 * Revision 1.1 1995/05/22 11:30:31 quinn
47 * Added prettier client.
53 * This is the obligatory little toy client, whose primary purpose is
54 * to illustrate the use of the YAZ service-level API.
62 #include <sys/select.h>
83 #define C_PROMPT "Z> "
85 static ODR out, in, print; /* encoding and decoding streams */
86 static COMSTACK conn = 0; /* our z-association */
87 static Z_IdAuthentication *auth = 0; /* our current auth definition */
88 static char database[512] = "Default"; /* Database name */
89 static int setnumber = 0; /* current result set number */
90 static int smallSetUpperBound = 0;
91 static int largeSetLowerBound = 1;
92 static int mediumSetPresentNumber = 0;
93 static int setno = 1; /* current set offset */
94 static int protocol = PROTO_Z3950; /* current app protocol */
95 static int recordsyntax = VAL_USMARC;
96 static ODR_MEM session_mem; /* memory handle for init-response */
97 static Z_InitResponse *session = 0; /* session parameters */
98 static char last_scan[512] = "0";
99 static char last_cmd[100] = "?";
102 static CCL_bibset bibset; /* CCL bibset handle */
106 static void send_apdu(Z_APDU *a)
111 if (!z_APDU(out, &a, 0))
113 odr_perror(out, "Encoding APDU");
116 buf = odr_getbuf(out, &len, 0);
117 odr_reset(out); /* release the APDU */
118 if (cs_put(conn, buf, len) < 0)
120 fprintf(stderr, "cs_put: %s", cs_errlist[cs_errno(conn)]);
125 /* INIT SERVICE ------------------------------- */
127 static void send_initRequest()
129 Z_APDU *apdu = zget_APDU(out, Z_APDU_initRequest);
130 Z_InitRequest *req = apdu->u.initRequest;
132 ODR_MASK_SET(req->options, Z_Options_search);
133 ODR_MASK_SET(req->options, Z_Options_present);
134 ODR_MASK_SET(req->options, Z_Options_namedResultSets);
135 ODR_MASK_SET(req->options, Z_Options_triggerResourceCtrl);
136 ODR_MASK_SET(req->options, Z_Options_scan);
138 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_1);
139 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_2);
141 req->idAuthentication = auth;
144 printf("Sent initrequest.\n");
147 static int process_initResponse(Z_InitResponse *res)
149 /* save session parameters for later use */
150 session_mem = odr_extract_mem(in);
154 printf("Connection rejected by target.\n");
156 printf("Connection accepted by target.\n");
157 if (res->implementationId)
158 printf("ID : %s\n", res->implementationId);
159 if (res->implementationName)
160 printf("Name : %s\n", res->implementationName);
161 if (res->implementationVersion)
162 printf("Version: %s\n", res->implementationVersion);
163 if (res->userInformationField)
165 printf("UserInformationfield:\n");
166 if (!odr_external(print, (Odr_external**)&res-> userInformationField,
169 odr_perror(print, "Printing userinfo\n");
172 if (res->userInformationField->which == ODR_EXTERNAL_octet)
174 printf("Guessing visiblestring:\n");
175 printf("'%s'\n", res->userInformationField->u. octet_aligned->buf);
181 int cmd_open(char *arg)
184 char type[100], addr[100];
189 printf("Already connected.\n");
192 if (!*arg || sscanf(arg, "%[^:]:%s", type, addr) < 2)
194 fprintf(stderr, "Usage: open (osi|tcp) ':' [tsel '/']host[':'port]\n");
198 if (!strcmp(type, "osi"))
200 if (!(add = mosi_strtoaddr(addr)))
210 if (!strcmp(type, "tcp"))
212 if (!(add = tcpip_strtoaddr(addr)))
218 protocol = PROTO_Z3950;
222 fprintf(stderr, "Bad type: %s\n", type);
225 if (!(conn = cs_create(t, 1, protocol)))
230 printf("Connecting...");
232 if (cs_connect(conn, add) < 0)
244 int cmd_authentication(char *arg)
246 static Z_IdAuthentication au;
247 static char open[256];
251 printf("Auth field set to null\n");
256 au.which = Z_IdAuthentication_open;
262 /* SEARCH SERVICE ------------------------------ */
264 void display_record(Z_DatabaseRecord *p)
266 Odr_external *r = (Odr_external*) p;
267 oident *ent = oid_getentbyoid(r->direct_reference);
269 if (r->direct_reference)
271 printf("Record type: ");
273 printf("%s\n", ent->desc);
274 else if (!odr_oid(print, &r->direct_reference, 0))
276 odr_perror(print, "print oid");
280 if (r->which == ODR_EXTERNAL_octet && p->u.octet_aligned->len)
281 marc_display ((char*)p->u.octet_aligned->buf, stdout);
282 else if (ent->value == VAL_SUTRS)
286 if (!z_SUTRS(in, &rc, 0))
288 odr_perror(in, "decoding SUTRS");
292 printf("%.*s", rc->len, rc->buf);
296 printf("Unknown record representation.\n");
297 if (!odr_external(print, &r, 0))
299 odr_perror(print, "Printing external");
305 static void display_diagrec(Z_DiagRec *p)
309 Z_DefaultDiagFormat *r;
314 printf("Diagnostic message from database:\n");
316 if (p->which != Z_DiagRec_defaultFormat)
318 printf("Diagnostic record not in default format.\n");
322 r = p->u.defaultFormat;
324 if (!(ent = oid_getentbyoid(r->diagnosticSetId)) ||
325 ent->class != CLASS_DIAGSET || ent->value != VAL_BIB1)
326 printf("Missing or unknown diagset\n");
327 printf(" [%d] %s", *r->condition, diagbib1_str(*r->condition));
328 if (r->addinfo && *r->addinfo)
329 printf(" -- %s\n", r->addinfo);
334 static void display_nameplusrecord(Z_NamePlusRecord *p)
337 printf("[%s]", p->databaseName);
338 if (p->which == Z_NamePlusRecord_surrogateDiagnostic)
339 display_diagrec(p->u.surrogateDiagnostic);
341 display_record(p->u.databaseRecord);
344 static void display_records(Z_Records *p)
348 if (p->which == Z_Records_NSD)
349 display_diagrec(p->u.nonSurrogateDiagnostic);
352 printf("Records: %d\n", p->u.databaseOrSurDiagnostics->num_records);
353 for (i = 0; i < p->u.databaseOrSurDiagnostics->num_records; i++)
354 display_nameplusrecord(p->u.databaseOrSurDiagnostics->records[i]);
358 static int send_searchRequest(char *arg)
360 Z_APDU *apdu = zget_APDU(out, Z_APDU_searchRequest);
361 Z_SearchRequest *req = apdu->u.searchRequest;
362 char *databaseNames = database;
366 struct ccl_rpn_node *rpn;
372 Z_RPNQuery *RPNquery;
380 rpn = ccl_find_str(bibset, arg, &error, &pos);
383 printf("CCL ERROR: %s\n", ccl_err_msg(error));
389 if (!strcmp(arg, "@big")) /* strictly for troublemaking */
391 static unsigned char big[2100];
394 /* send a very big referenceid to test transport stack etc. */
395 memset(big, 'A', 2100);
396 bigo.len = bigo.size = 2100;
398 req->referenceId = &bigo;
403 sprintf(setstring, "%d", ++setnumber);
404 req->resultSetName = setstring;
406 *req->smallSetUpperBound = smallSetUpperBound;
407 *req->largeSetLowerBound = largeSetLowerBound;
408 *req->mediumSetPresentNumber = mediumSetPresentNumber;
409 if (smallSetUpperBound > 0 || (largeSetLowerBound > 1 &&
410 mediumSetPresentNumber > 0))
414 prefsyn.proto = protocol;
415 prefsyn.class = CLASS_RECSYN;
416 prefsyn.value = recordsyntax;
417 req->preferredRecordSyntax = odr_oiddup(out, oid_getoidbyent(&prefsyn));
419 req->num_databaseNames = 1;
420 req->databaseNames = &databaseNames;
425 query.which = Z_Query_type_1;
428 assert((RPNquery = ccl_rpn_query(rpn)));
430 RPNquery = p_query_rpn (out, arg);
433 printf("Prefix query error\n");
437 bib1.proto = protocol;
438 bib1.class = CLASS_ATTSET;
439 bib1.value = VAL_BIB1;
440 RPNquery->attributeSetId = oid_getoidbyent(&bib1);
441 query.u.type_1 = RPNquery;
443 query.which = Z_Query_type_2;
444 query.u.type_2 = &ccl_query;
445 ccl_query.buf = (unsigned char*) arg;
446 ccl_query.len = strlen(arg);
451 printf("Sent searchRequest.\n");
455 static int process_searchResponse(Z_SearchResponse *res)
457 if (*res->searchStatus)
458 printf("Search was a success.\n");
460 printf("Search was a bloomin' failure.\n");
461 printf("Number of hits: %d, setno %d\n",
462 *res->resultCount, setnumber);
463 printf("records returned: %d\n",
464 *res->numberOfRecordsReturned);
465 setno += *res->numberOfRecordsReturned;
467 display_records(res->records);
471 static int cmd_find(char *arg)
475 printf("Find what?\n");
480 printf("Not connected yet\n");
483 if (!send_searchRequest(arg))
488 static int cmd_ssub(char *arg)
490 if (!(smallSetUpperBound = atoi(arg)))
495 static int cmd_lslb(char *arg)
497 if (!(largeSetLowerBound = atoi(arg)))
502 static int cmd_mspn(char *arg)
504 if (!(mediumSetPresentNumber = atoi(arg)))
509 static int cmd_status(char *arg)
511 printf("smallSetUpperBound: %d\n", smallSetUpperBound);
512 printf("largeSetLowerBound: %d\n", largeSetLowerBound);
513 printf("mediumSetPresentNumber: %d\n", mediumSetPresentNumber);
517 static int cmd_base(char *arg)
521 printf("Usage: base <database>\n");
524 strcpy(database, arg);
528 static int cmd_setnames(char *arg)
532 printf("Set numbering enabled.\n");
537 printf("Set numbering disabled.\n");
543 /* PRESENT SERVICE ----------------------------- */
545 static int send_presentRequest(char *arg)
547 Z_APDU *apdu = zget_APDU(out, Z_APDU_presentRequest);
548 Z_PresentRequest *req = apdu->u.presentRequest;
554 if ((p = strchr(arg, '+')))
564 sprintf(setstring, "%d", setnumber);
565 req->resultSetId = setstring;
567 req->resultSetStartPoint = &setno;
568 req->numberOfRecordsRequested = &nos;
569 prefsyn.proto = protocol;
570 prefsyn.class = CLASS_RECSYN;
571 prefsyn.value = recordsyntax;
572 req->preferredRecordSyntax = oid_getoidbyent(&prefsyn);
574 printf("Sent presentRequest (%d+%d).\n", setno, nos);
578 static int cmd_show(char *arg)
580 if (!send_presentRequest(arg))
585 int cmd_quit(char *arg)
587 printf("See you later, alligator.\n");
591 int cmd_cancel(char *arg)
593 Z_APDU *apdu = zget_APDU(out, Z_APDU_triggerResourceControlRequest);
594 Z_TriggerResourceControlRequest *req =
595 apdu->u.triggerResourceControlRequest;
600 printf("Session not initialized yet\n");
603 if (!ODR_MASK_GET(session->options, Z_Options_triggerResourceCtrl))
605 printf("Target doesn't support cancel (trigger resource ctrl)\n");
608 *req->requestedAction = Z_TriggerResourceCtrl_cancel;
609 req->resultSetWanted = &false;
612 printf("Sent cancel request\n");
616 int send_scanrequest(char *string, int pp, int num)
618 Z_APDU *apdu = zget_APDU(out, Z_APDU_scanRequest);
619 Z_ScanRequest *req = apdu->u.scanRequest;
623 req->num_databaseNames = 1;
624 req->databaseNames = &db;
625 attset.proto = protocol;
626 attset.class = CLASS_ATTSET;
627 attset.value = VAL_BIB1;
628 req->attributeSet = oid_getoidbyent(&attset);
629 req->termListAndStartPoint = p_query_scan(out, string);
630 req->numberOfTermsRequested = #
631 req->preferredPositionInResponse = &pp;
636 void display_term(Z_TermInfo *t)
638 if (t->term->which == Z_Term_general)
640 printf("%.*s (%d)\n", t->term->u.general->len, t->term->u.general->buf,
641 t->globalOccurrences ? *t->globalOccurrences : -1);
642 sprintf(last_scan, "%.*s", t->term->u.general->len,
643 t->term->u.general->buf);
646 printf("Term type not general.\n");
649 void process_scanResponse(Z_ScanResponse *res)
653 printf("SCAN: %d entries, position=%d\n", *res->numberOfEntriesReturned,
654 *res->positionOfTerm);
655 if (*res->scanStatus != Z_Scan_success)
656 printf("Scan returned code %d\n", *res->scanStatus);
659 if (res->entries->which == Z_ListEntries_entries)
661 Z_Entries *ent = res->entries->u.entries;
663 for (i = 0; i < ent->num_entries; i++)
664 if (ent->entries[i]->which == Z_Entry_termInfo)
666 printf("%c ", i + 1 == *res->positionOfTerm ? '*' : ' ');
667 display_term(ent->entries[i]->u.termInfo);
670 display_diagrec(ent->entries[i]->u.surrogateDiagnostic);
673 display_diagrec(res->entries->u.nonSurrogateDiagnostics->diagRecs[0]);
676 int cmd_scan(char *arg)
680 printf("Session not initialized yet\n");
683 if (!ODR_MASK_GET(session->options, Z_Options_scan))
685 printf("Target doesn't support scan\n");
690 if (send_scanrequest(arg, 5, 20) < 0)
694 if (send_scanrequest(last_scan, 1, 20) < 0)
699 int cmd_format(char *arg)
703 printf("Usage: format <recordsyntax>\n");
706 if (!strcmp(arg, "sutrs"))
708 printf("Preferred format is SUTRS.\n");
709 recordsyntax = VAL_SUTRS;
712 else if (!strcmp(arg, "usmarc"))
714 printf("Preferred format is USMARC\n");
715 recordsyntax = VAL_USMARC;
718 else if (!strcmp(arg, "danmarc"))
720 printf("Preferred format is DANMARC\n");
721 recordsyntax = VAL_DANMARC;
726 printf("Specify one of {sutrs,usmarc,danmarc}.\n");
731 static void initialize(void)
739 if (!(out = odr_createmem(ODR_ENCODE)) ||
740 !(in = odr_createmem(ODR_DECODE)) ||
741 !(print = odr_createmem(ODR_PRINT)))
743 fprintf(stderr, "failed to allocate ODR streams\n");
746 setvbuf(stdout, 0, _IONBF, 0);
750 bibset = ccl_qual_mk ();
751 inf = fopen ("default.bib", "r");
754 ccl_qual_file (bibset, inf);
761 static int client(void)
765 int (*fun)(char *arg);
768 {"open", cmd_open, "('tcp'|'osi')':'[<TSEL>'/']<HOST>[':'<PORT>]"},
769 {"quit", cmd_quit, ""},
770 {"find", cmd_find, "<CCL-QUERY>"},
771 {"base", cmd_base, "<BASE-NAME>"},
772 {"show", cmd_show, "<REC#>['+'<#RECS>]"},
773 {"scan", cmd_scan, "<TERM>"},
774 {"authentication", cmd_authentication, "<ACCTSTRING>"},
775 {"lslb", cmd_lslb, "<largeSetLowerBound>"},
776 {"ssub", cmd_ssub, "<smallSetUpperBound>"},
777 {"mspn", cmd_mspn, "<mediumSetPresentNumber>"},
778 {"status", cmd_status, ""},
779 {"setnames", cmd_setnames, ""},
780 {"cancel", cmd_cancel, ""},
781 {"format", cmd_format, "<recordsyntax>"},
785 int netbufferlen = 0;
793 char line[1024], word[1024], arg[1024];
798 FD_SET(cs_fileno(conn), &input);
799 if ((res = select(20, &input, 0, 0, 0)) < 0)
806 if (FD_ISSET(0, &input))
808 /* quick & dirty way to get a command line. */
811 if ((res = sscanf(line, "%s %[^;]", word, arg)) <= 0)
813 strcpy(word, last_cmd);
818 strcpy(last_cmd, word);
819 for (i = 0; cmd[i].cmd; i++)
820 if (!strncmp(cmd[i].cmd, word, strlen(word)))
822 res = (*cmd[i].fun)(arg);
825 if (!cmd[i].cmd) /* dump our help-screen */
827 printf("Unknown command: %s.\n", word);
828 printf("Currently recognized commands:\n");
829 for (i = 0; cmd[i].cmd; i++)
830 printf(" %s %s\n", cmd[i].cmd, cmd[i].ad);
836 if (conn && FD_ISSET(cs_fileno(conn), &input))
840 if ((res = cs_get(conn, &netbuffer, &netbufferlen)) < 0)
847 printf("Target closed connection.\n");
850 odr_reset(in); /* release APDU from last round */
851 odr_setbuf(in, netbuffer, res, 0);
852 if (!z_APDU(in, &apdu, 0))
854 odr_perror(in, "Decoding incoming APDU");
855 fprintf(stderr, "Packet dump:\n---------\n");
856 odr_dumpBER(stderr, netbuffer, res);
857 fprintf(stderr, "---------\n");
861 if (!z_APDU(print, &apdu, 0))
863 odr_perror(print, "Failed to print incoming APDU");
870 case Z_APDU_initResponse:
871 process_initResponse(apdu->u.initResponse);
873 case Z_APDU_searchResponse:
874 process_searchResponse(apdu->u.searchResponse);
876 case Z_APDU_scanResponse:
877 process_scanResponse(apdu->u.scanResponse);
879 case Z_APDU_presentResponse:
880 printf("Received presentResponse.\n");
882 *apdu->u.presentResponse->numberOfRecordsReturned;
883 if (apdu->u.presentResponse->records)
884 display_records(apdu->u.presentResponse->records);
886 printf("No records.\n");
889 printf("Received unknown APDU type (%d).\n",
896 while (cs_more(conn));
902 int main(int argc, char **argv)