2 * Copyright (c) 1995, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.16 1995-08-15 12:00:04 quinn
10 * Revision 1.15 1995/06/22 09:28:03 quinn
11 * Fixed bug in SUTRS processing.
13 * Revision 1.14 1995/06/19 12:37:41 quinn
16 * Revision 1.13 1995/06/16 10:29:11 quinn
17 * *** empty log message ***
19 * Revision 1.12 1995/06/15 07:44:57 quinn
22 * Revision 1.11 1995/06/14 15:26:40 quinn
23 * *** empty log message ***
25 * Revision 1.10 1995/06/06 14:56:58 quinn
28 * Revision 1.9 1995/06/06 08:15:19 quinn
31 * Revision 1.8 1995/06/05 10:52:22 quinn
34 * Revision 1.7 1995/06/02 09:50:09 quinn
37 * Revision 1.6 1995/05/31 08:29:21 quinn
38 * Nothing significant.
40 * Revision 1.5 1995/05/29 08:10:47 quinn
41 * Moved oid.c to util.
43 * Revision 1.4 1995/05/22 15:30:13 adam
44 * Client uses prefix query notation.
46 * Revision 1.3 1995/05/22 15:06:53 quinn
47 * *** empty log message ***
49 * Revision 1.2 1995/05/22 14:56:40 quinn
50 * *** empty log message ***
52 * Revision 1.1 1995/05/22 11:30:31 quinn
53 * Added prettier client.
59 * This is the obligatory little toy client, whose primary purpose is
60 * to illustrate the use of the YAZ service-level API.
68 #include <sys/select.h>
89 #define C_PROMPT "Z> "
91 static ODR out, in, print; /* encoding and decoding streams */
92 static COMSTACK conn = 0; /* our z-association */
93 static Z_IdAuthentication *auth = 0; /* our current auth definition */
94 static char database[512] = "Default"; /* Database name */
95 static int setnumber = 0; /* current result set number */
96 static int smallSetUpperBound = 0;
97 static int largeSetLowerBound = 1;
98 static int mediumSetPresentNumber = 0;
99 static int setno = 1; /* current set offset */
100 static int protocol = PROTO_Z3950; /* current app protocol */
101 static int recordsyntax = VAL_USMARC;
102 static ODR_MEM session_mem; /* memory handle for init-response */
103 static Z_InitResponse *session = 0; /* session parameters */
104 static char last_scan[512] = "0";
105 static char last_cmd[100] = "?";
108 static CCL_bibset bibset; /* CCL bibset handle */
112 static void send_apdu(Z_APDU *a)
117 if (!z_APDU(out, &a, 0))
119 odr_perror(out, "Encoding APDU");
122 buf = odr_getbuf(out, &len, 0);
123 odr_reset(out); /* release the APDU */
124 if (cs_put(conn, buf, len) < 0)
126 fprintf(stderr, "cs_put: %s", cs_errlist[cs_errno(conn)]);
131 /* INIT SERVICE ------------------------------- */
133 static void send_initRequest()
135 Z_APDU *apdu = zget_APDU(out, Z_APDU_initRequest);
136 Z_InitRequest *req = apdu->u.initRequest;
138 ODR_MASK_SET(req->options, Z_Options_search);
139 ODR_MASK_SET(req->options, Z_Options_present);
140 ODR_MASK_SET(req->options, Z_Options_namedResultSets);
141 ODR_MASK_SET(req->options, Z_Options_triggerResourceCtrl);
142 ODR_MASK_SET(req->options, Z_Options_scan);
144 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_1);
145 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_2);
147 req->idAuthentication = auth;
150 printf("Sent initrequest.\n");
153 static int process_initResponse(Z_InitResponse *res)
155 /* save session parameters for later use */
156 session_mem = odr_extract_mem(in);
160 printf("Connection rejected by target.\n");
162 printf("Connection accepted by target.\n");
163 if (res->implementationId)
164 printf("ID : %s\n", res->implementationId);
165 if (res->implementationName)
166 printf("Name : %s\n", res->implementationName);
167 if (res->implementationVersion)
168 printf("Version: %s\n", res->implementationVersion);
169 if (res->userInformationField)
171 printf("UserInformationfield:\n");
172 if (!z_External(print, (Z_External**)&res-> userInformationField,
175 odr_perror(print, "Printing userinfo\n");
178 if (res->userInformationField->which == ODR_EXTERNAL_octet)
180 printf("Guessing visiblestring:\n");
181 printf("'%s'\n", res->userInformationField->u. octet_aligned->buf);
187 int cmd_open(char *arg)
190 char type[100], addr[100];
195 printf("Already connected.\n");
198 if (!*arg || sscanf(arg, "%[^:]:%s", type, addr) < 2)
200 fprintf(stderr, "Usage: open (osi|tcp) ':' [tsel '/']host[':'port]\n");
204 if (!strcmp(type, "osi"))
206 if (!(add = mosi_strtoaddr(addr)))
216 if (!strcmp(type, "tcp"))
218 if (!(add = tcpip_strtoaddr(addr)))
224 protocol = PROTO_Z3950;
228 fprintf(stderr, "Bad type: %s\n", type);
231 if (!(conn = cs_create(t, 1, protocol)))
236 printf("Connecting...");
238 if (cs_connect(conn, add) < 0)
250 int cmd_authentication(char *arg)
252 static Z_IdAuthentication au;
253 static char open[256];
257 printf("Auth field set to null\n");
262 au.which = Z_IdAuthentication_open;
268 /* SEARCH SERVICE ------------------------------ */
270 void display_record(Z_DatabaseRecord *p)
272 Z_External *r = (Odr_external*) p;
273 oident *ent = oid_getentbyoid(r->direct_reference);
275 if (r->direct_reference)
277 printf("Record type: ");
279 printf("%s\n", ent->desc);
280 else if (!odr_oid(print, &r->direct_reference, 0))
282 odr_perror(print, "print oid");
286 if (r->which == ODR_EXTERNAL_octet && p->u.octet_aligned->len)
287 marc_display ((char*)p->u.octet_aligned->buf, stdout);
288 else if (ent->value == VAL_SUTRS)
290 if (r->which != Z_External_SUTRS)
292 printf("Expecting single SUTRS type for SUTRS.\n");
295 printf("%.*s", r->u.sutrs->len, r->u.sutrs->buf);
299 printf("Unknown record representation.\n");
300 if (!z_External(print, &r, 0))
302 odr_perror(print, "Printing external");
308 static void display_diagrec(Z_DiagRec *p)
312 Z_DefaultDiagFormat *r;
317 printf("Diagnostic message from database:\n");
319 if (p->which != Z_DiagRec_defaultFormat)
321 printf("Diagnostic record not in default format.\n");
325 r = p->u.defaultFormat;
327 if (!(ent = oid_getentbyoid(r->diagnosticSetId)) ||
328 ent->class != CLASS_DIAGSET || ent->value != VAL_BIB1)
329 printf("Missing or unknown diagset\n");
330 printf(" [%d] %s", *r->condition, diagbib1_str(*r->condition));
331 if (r->addinfo && *r->addinfo)
332 printf(" -- %s\n", r->addinfo);
337 static void display_nameplusrecord(Z_NamePlusRecord *p)
340 printf("[%s]", p->databaseName);
341 if (p->which == Z_NamePlusRecord_surrogateDiagnostic)
342 display_diagrec(p->u.surrogateDiagnostic);
344 display_record(p->u.databaseRecord);
347 static void display_records(Z_Records *p)
351 if (p->which == Z_Records_NSD)
352 display_diagrec(p->u.nonSurrogateDiagnostic);
355 printf("Records: %d\n", p->u.databaseOrSurDiagnostics->num_records);
356 for (i = 0; i < p->u.databaseOrSurDiagnostics->num_records; i++)
357 display_nameplusrecord(p->u.databaseOrSurDiagnostics->records[i]);
361 static int send_searchRequest(char *arg)
363 Z_APDU *apdu = zget_APDU(out, Z_APDU_searchRequest);
364 Z_SearchRequest *req = apdu->u.searchRequest;
365 char *databaseNames = database;
369 struct ccl_rpn_node *rpn;
375 Z_RPNQuery *RPNquery;
383 rpn = ccl_find_str(bibset, arg, &error, &pos);
386 printf("CCL ERROR: %s\n", ccl_err_msg(error));
392 if (!strcmp(arg, "@big")) /* strictly for troublemaking */
394 static unsigned char big[2100];
397 /* send a very big referenceid to test transport stack etc. */
398 memset(big, 'A', 2100);
399 bigo.len = bigo.size = 2100;
401 req->referenceId = &bigo;
406 sprintf(setstring, "%d", ++setnumber);
407 req->resultSetName = setstring;
409 *req->smallSetUpperBound = smallSetUpperBound;
410 *req->largeSetLowerBound = largeSetLowerBound;
411 *req->mediumSetPresentNumber = mediumSetPresentNumber;
412 if (smallSetUpperBound > 0 || (largeSetLowerBound > 1 &&
413 mediumSetPresentNumber > 0))
417 prefsyn.proto = protocol;
418 prefsyn.class = CLASS_RECSYN;
419 prefsyn.value = recordsyntax;
420 req->preferredRecordSyntax = odr_oiddup(out, oid_getoidbyent(&prefsyn));
422 req->num_databaseNames = 1;
423 req->databaseNames = &databaseNames;
428 query.which = Z_Query_type_1;
431 assert((RPNquery = ccl_rpn_query(rpn)));
433 RPNquery = p_query_rpn (out, arg);
436 printf("Prefix query error\n");
440 bib1.proto = protocol;
441 bib1.class = CLASS_ATTSET;
442 bib1.value = VAL_BIB1;
443 RPNquery->attributeSetId = oid_getoidbyent(&bib1);
444 query.u.type_1 = RPNquery;
446 query.which = Z_Query_type_2;
447 query.u.type_2 = &ccl_query;
448 ccl_query.buf = (unsigned char*) arg;
449 ccl_query.len = strlen(arg);
454 printf("Sent searchRequest.\n");
458 static int process_searchResponse(Z_SearchResponse *res)
460 if (*res->searchStatus)
461 printf("Search was a success.\n");
463 printf("Search was a bloomin' failure.\n");
464 printf("Number of hits: %d, setno %d\n",
465 *res->resultCount, setnumber);
466 printf("records returned: %d\n",
467 *res->numberOfRecordsReturned);
468 setno += *res->numberOfRecordsReturned;
470 display_records(res->records);
474 static int cmd_find(char *arg)
478 printf("Find what?\n");
483 printf("Not connected yet\n");
486 if (!send_searchRequest(arg))
491 static int cmd_ssub(char *arg)
493 if (!(smallSetUpperBound = atoi(arg)))
498 static int cmd_lslb(char *arg)
500 if (!(largeSetLowerBound = atoi(arg)))
505 static int cmd_mspn(char *arg)
507 if (!(mediumSetPresentNumber = atoi(arg)))
512 static int cmd_status(char *arg)
514 printf("smallSetUpperBound: %d\n", smallSetUpperBound);
515 printf("largeSetLowerBound: %d\n", largeSetLowerBound);
516 printf("mediumSetPresentNumber: %d\n", mediumSetPresentNumber);
520 static int cmd_base(char *arg)
524 printf("Usage: base <database>\n");
527 strcpy(database, arg);
531 static int cmd_setnames(char *arg)
535 printf("Set numbering enabled.\n");
540 printf("Set numbering disabled.\n");
546 /* PRESENT SERVICE ----------------------------- */
548 static int send_presentRequest(char *arg)
550 Z_APDU *apdu = zget_APDU(out, Z_APDU_presentRequest);
551 Z_PresentRequest *req = apdu->u.presentRequest;
557 if ((p = strchr(arg, '+')))
567 sprintf(setstring, "%d", setnumber);
568 req->resultSetId = setstring;
570 req->resultSetStartPoint = &setno;
571 req->numberOfRecordsRequested = &nos;
572 prefsyn.proto = protocol;
573 prefsyn.class = CLASS_RECSYN;
574 prefsyn.value = recordsyntax;
575 req->preferredRecordSyntax = oid_getoidbyent(&prefsyn);
577 printf("Sent presentRequest (%d+%d).\n", setno, nos);
581 static int cmd_show(char *arg)
583 if (!send_presentRequest(arg))
588 int cmd_quit(char *arg)
590 printf("See you later, alligator.\n");
594 int cmd_cancel(char *arg)
596 Z_APDU *apdu = zget_APDU(out, Z_APDU_triggerResourceControlRequest);
597 Z_TriggerResourceControlRequest *req =
598 apdu->u.triggerResourceControlRequest;
603 printf("Session not initialized yet\n");
606 if (!ODR_MASK_GET(session->options, Z_Options_triggerResourceCtrl))
608 printf("Target doesn't support cancel (trigger resource ctrl)\n");
611 *req->requestedAction = Z_TriggerResourceCtrl_cancel;
612 req->resultSetWanted = &false;
615 printf("Sent cancel request\n");
619 int send_scanrequest(char *string, int pp, int num)
621 Z_APDU *apdu = zget_APDU(out, Z_APDU_scanRequest);
622 Z_ScanRequest *req = apdu->u.scanRequest;
626 req->num_databaseNames = 1;
627 req->databaseNames = &db;
628 attset.proto = protocol;
629 attset.class = CLASS_ATTSET;
630 attset.value = VAL_BIB1;
631 req->attributeSet = oid_getoidbyent(&attset);
632 req->termListAndStartPoint = p_query_scan(out, string);
633 req->numberOfTermsRequested = #
634 req->preferredPositionInResponse = &pp;
639 void display_term(Z_TermInfo *t)
641 if (t->term->which == Z_Term_general)
643 printf("%.*s (%d)\n", t->term->u.general->len, t->term->u.general->buf,
644 t->globalOccurrences ? *t->globalOccurrences : -1);
645 sprintf(last_scan, "%.*s", t->term->u.general->len,
646 t->term->u.general->buf);
649 printf("Term type not general.\n");
652 void process_scanResponse(Z_ScanResponse *res)
656 printf("SCAN: %d entries, position=%d\n", *res->numberOfEntriesReturned,
657 *res->positionOfTerm);
658 if (*res->scanStatus != Z_Scan_success)
659 printf("Scan returned code %d\n", *res->scanStatus);
662 if (res->entries->which == Z_ListEntries_entries)
664 Z_Entries *ent = res->entries->u.entries;
666 for (i = 0; i < ent->num_entries; i++)
667 if (ent->entries[i]->which == Z_Entry_termInfo)
669 printf("%c ", i + 1 == *res->positionOfTerm ? '*' : ' ');
670 display_term(ent->entries[i]->u.termInfo);
673 display_diagrec(ent->entries[i]->u.surrogateDiagnostic);
676 display_diagrec(res->entries->u.nonSurrogateDiagnostics->diagRecs[0]);
679 int cmd_scan(char *arg)
683 printf("Session not initialized yet\n");
686 if (!ODR_MASK_GET(session->options, Z_Options_scan))
688 printf("Target doesn't support scan\n");
693 if (send_scanrequest(arg, 5, 20) < 0)
697 if (send_scanrequest(last_scan, 1, 20) < 0)
702 int cmd_format(char *arg)
706 printf("Usage: format <recordsyntax>\n");
709 if (!strcmp(arg, "sutrs"))
711 printf("Preferred format is SUTRS.\n");
712 recordsyntax = VAL_SUTRS;
715 else if (!strcmp(arg, "usmarc"))
717 printf("Preferred format is USMARC\n");
718 recordsyntax = VAL_USMARC;
721 else if (!strcmp(arg, "danmarc"))
723 printf("Preferred format is DANMARC\n");
724 recordsyntax = VAL_DANMARC;
729 printf("Specify one of {sutrs,usmarc,danmarc}.\n");
734 static void initialize(void)
742 if (!(out = odr_createmem(ODR_ENCODE)) ||
743 !(in = odr_createmem(ODR_DECODE)) ||
744 !(print = odr_createmem(ODR_PRINT)))
746 fprintf(stderr, "failed to allocate ODR streams\n");
749 setvbuf(stdout, 0, _IONBF, 0);
753 bibset = ccl_qual_mk ();
754 inf = fopen ("default.bib", "r");
757 ccl_qual_file (bibset, inf);
764 static int client(void)
768 int (*fun)(char *arg);
771 {"open", cmd_open, "('tcp'|'osi')':'[<TSEL>'/']<HOST>[':'<PORT>]"},
772 {"quit", cmd_quit, ""},
773 {"find", cmd_find, "<CCL-QUERY>"},
774 {"base", cmd_base, "<BASE-NAME>"},
775 {"show", cmd_show, "<REC#>['+'<#RECS>]"},
776 {"scan", cmd_scan, "<TERM>"},
777 {"authentication", cmd_authentication, "<ACCTSTRING>"},
778 {"lslb", cmd_lslb, "<largeSetLowerBound>"},
779 {"ssub", cmd_ssub, "<smallSetUpperBound>"},
780 {"mspn", cmd_mspn, "<mediumSetPresentNumber>"},
781 {"status", cmd_status, ""},
782 {"setnames", cmd_setnames, ""},
783 {"cancel", cmd_cancel, ""},
784 {"format", cmd_format, "<recordsyntax>"},
788 int netbufferlen = 0;
796 char line[1024], word[1024], arg[1024];
801 FD_SET(cs_fileno(conn), &input);
802 if ((res = select(20, &input, 0, 0, 0)) < 0)
809 if (FD_ISSET(0, &input))
811 /* quick & dirty way to get a command line. */
814 if ((res = sscanf(line, "%s %[^;]", word, arg)) <= 0)
816 strcpy(word, last_cmd);
821 strcpy(last_cmd, word);
822 for (i = 0; cmd[i].cmd; i++)
823 if (!strncmp(cmd[i].cmd, word, strlen(word)))
825 res = (*cmd[i].fun)(arg);
828 if (!cmd[i].cmd) /* dump our help-screen */
830 printf("Unknown command: %s.\n", word);
831 printf("Currently recognized commands:\n");
832 for (i = 0; cmd[i].cmd; i++)
833 printf(" %s %s\n", cmd[i].cmd, cmd[i].ad);
839 if (conn && FD_ISSET(cs_fileno(conn), &input))
843 if ((res = cs_get(conn, &netbuffer, &netbufferlen)) < 0)
850 printf("Target closed connection.\n");
853 odr_reset(in); /* release APDU from last round */
854 odr_setbuf(in, netbuffer, res, 0);
855 if (!z_APDU(in, &apdu, 0))
857 odr_perror(in, "Decoding incoming APDU");
858 fprintf(stderr, "Packet dump:\n---------\n");
859 odr_dumpBER(stderr, netbuffer, res);
860 fprintf(stderr, "---------\n");
864 if (!z_APDU(print, &apdu, 0))
866 odr_perror(print, "Failed to print incoming APDU");
873 case Z_APDU_initResponse:
874 process_initResponse(apdu->u.initResponse);
876 case Z_APDU_searchResponse:
877 process_searchResponse(apdu->u.searchResponse);
879 case Z_APDU_scanResponse:
880 process_scanResponse(apdu->u.scanResponse);
882 case Z_APDU_presentResponse:
883 printf("Received presentResponse.\n");
885 *apdu->u.presentResponse->numberOfRecordsReturned;
886 if (apdu->u.presentResponse->records)
887 display_records(apdu->u.presentResponse->records);
889 printf("No records.\n");
892 printf("Received unknown APDU type (%d).\n",
899 while (cs_more(conn));
905 int main(int argc, char **argv)