2 * Copyright (c) 1995, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.15 1995-06-22 09:28:03 quinn
8 * Fixed bug in SUTRS processing.
10 * Revision 1.14 1995/06/19 12:37:41 quinn
13 * Revision 1.13 1995/06/16 10:29:11 quinn
14 * *** empty log message ***
16 * Revision 1.12 1995/06/15 07:44:57 quinn
19 * Revision 1.11 1995/06/14 15:26:40 quinn
20 * *** empty log message ***
22 * Revision 1.10 1995/06/06 14:56:58 quinn
25 * Revision 1.9 1995/06/06 08:15:19 quinn
28 * Revision 1.8 1995/06/05 10:52:22 quinn
31 * Revision 1.7 1995/06/02 09:50:09 quinn
34 * Revision 1.6 1995/05/31 08:29:21 quinn
35 * Nothing significant.
37 * Revision 1.5 1995/05/29 08:10:47 quinn
38 * Moved oid.c to util.
40 * Revision 1.4 1995/05/22 15:30:13 adam
41 * Client uses prefix query notation.
43 * Revision 1.3 1995/05/22 15:06:53 quinn
44 * *** empty log message ***
46 * Revision 1.2 1995/05/22 14:56:40 quinn
47 * *** empty log message ***
49 * Revision 1.1 1995/05/22 11:30:31 quinn
50 * Added prettier client.
56 * This is the obligatory little toy client, whose primary purpose is
57 * to illustrate the use of the YAZ service-level API.
65 #include <sys/select.h>
86 #define C_PROMPT "Z> "
88 static ODR out, in, print; /* encoding and decoding streams */
89 static COMSTACK conn = 0; /* our z-association */
90 static Z_IdAuthentication *auth = 0; /* our current auth definition */
91 static char database[512] = "Default"; /* Database name */
92 static int setnumber = 0; /* current result set number */
93 static int smallSetUpperBound = 0;
94 static int largeSetLowerBound = 1;
95 static int mediumSetPresentNumber = 0;
96 static int setno = 1; /* current set offset */
97 static int protocol = PROTO_Z3950; /* current app protocol */
98 static int recordsyntax = VAL_USMARC;
99 static ODR_MEM session_mem; /* memory handle for init-response */
100 static Z_InitResponse *session = 0; /* session parameters */
101 static char last_scan[512] = "0";
102 static char last_cmd[100] = "?";
105 static CCL_bibset bibset; /* CCL bibset handle */
109 static void send_apdu(Z_APDU *a)
114 if (!z_APDU(out, &a, 0))
116 odr_perror(out, "Encoding APDU");
119 buf = odr_getbuf(out, &len, 0);
120 odr_reset(out); /* release the APDU */
121 if (cs_put(conn, buf, len) < 0)
123 fprintf(stderr, "cs_put: %s", cs_errlist[cs_errno(conn)]);
128 /* INIT SERVICE ------------------------------- */
130 static void send_initRequest()
132 Z_APDU *apdu = zget_APDU(out, Z_APDU_initRequest);
133 Z_InitRequest *req = apdu->u.initRequest;
135 ODR_MASK_SET(req->options, Z_Options_search);
136 ODR_MASK_SET(req->options, Z_Options_present);
137 ODR_MASK_SET(req->options, Z_Options_namedResultSets);
138 ODR_MASK_SET(req->options, Z_Options_triggerResourceCtrl);
139 ODR_MASK_SET(req->options, Z_Options_scan);
141 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_1);
142 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_2);
144 req->idAuthentication = auth;
147 printf("Sent initrequest.\n");
150 static int process_initResponse(Z_InitResponse *res)
152 /* save session parameters for later use */
153 session_mem = odr_extract_mem(in);
157 printf("Connection rejected by target.\n");
159 printf("Connection accepted by target.\n");
160 if (res->implementationId)
161 printf("ID : %s\n", res->implementationId);
162 if (res->implementationName)
163 printf("Name : %s\n", res->implementationName);
164 if (res->implementationVersion)
165 printf("Version: %s\n", res->implementationVersion);
166 if (res->userInformationField)
168 printf("UserInformationfield:\n");
169 if (!odr_external(print, (Odr_external**)&res-> userInformationField,
172 odr_perror(print, "Printing userinfo\n");
175 if (res->userInformationField->which == ODR_EXTERNAL_octet)
177 printf("Guessing visiblestring:\n");
178 printf("'%s'\n", res->userInformationField->u. octet_aligned->buf);
184 int cmd_open(char *arg)
187 char type[100], addr[100];
192 printf("Already connected.\n");
195 if (!*arg || sscanf(arg, "%[^:]:%s", type, addr) < 2)
197 fprintf(stderr, "Usage: open (osi|tcp) ':' [tsel '/']host[':'port]\n");
201 if (!strcmp(type, "osi"))
203 if (!(add = mosi_strtoaddr(addr)))
213 if (!strcmp(type, "tcp"))
215 if (!(add = tcpip_strtoaddr(addr)))
221 protocol = PROTO_Z3950;
225 fprintf(stderr, "Bad type: %s\n", type);
228 if (!(conn = cs_create(t, 1, protocol)))
233 printf("Connecting...");
235 if (cs_connect(conn, add) < 0)
247 int cmd_authentication(char *arg)
249 static Z_IdAuthentication au;
250 static char open[256];
254 printf("Auth field set to null\n");
259 au.which = Z_IdAuthentication_open;
265 /* SEARCH SERVICE ------------------------------ */
267 void display_record(Z_DatabaseRecord *p)
269 Odr_external *r = (Odr_external*) p;
270 oident *ent = oid_getentbyoid(r->direct_reference);
272 if (r->direct_reference)
274 printf("Record type: ");
276 printf("%s\n", ent->desc);
277 else if (!odr_oid(print, &r->direct_reference, 0))
279 odr_perror(print, "print oid");
283 if (r->which == ODR_EXTERNAL_octet && p->u.octet_aligned->len)
284 marc_display ((char*)p->u.octet_aligned->buf, stdout);
285 else if (ent->value == VAL_SUTRS)
289 if (r->which != ODR_EXTERNAL_single)
291 printf("Expecting single ASN.1 type for SUTRS.\n");
294 odr_setbuf(in, (char*)r->u.single_ASN1_type->buf,
295 r->u.single_ASN1_type->len, 0);
296 if (!z_SUTRS(in, &rc, 0))
298 odr_perror(in, "decoding SUTRS");
302 printf("%.*s", rc->len, rc->buf);
306 printf("Unknown record representation.\n");
307 if (!odr_external(print, &r, 0))
309 odr_perror(print, "Printing external");
315 static void display_diagrec(Z_DiagRec *p)
319 Z_DefaultDiagFormat *r;
324 printf("Diagnostic message from database:\n");
326 if (p->which != Z_DiagRec_defaultFormat)
328 printf("Diagnostic record not in default format.\n");
332 r = p->u.defaultFormat;
334 if (!(ent = oid_getentbyoid(r->diagnosticSetId)) ||
335 ent->class != CLASS_DIAGSET || ent->value != VAL_BIB1)
336 printf("Missing or unknown diagset\n");
337 printf(" [%d] %s", *r->condition, diagbib1_str(*r->condition));
338 if (r->addinfo && *r->addinfo)
339 printf(" -- %s\n", r->addinfo);
344 static void display_nameplusrecord(Z_NamePlusRecord *p)
347 printf("[%s]", p->databaseName);
348 if (p->which == Z_NamePlusRecord_surrogateDiagnostic)
349 display_diagrec(p->u.surrogateDiagnostic);
351 display_record(p->u.databaseRecord);
354 static void display_records(Z_Records *p)
358 if (p->which == Z_Records_NSD)
359 display_diagrec(p->u.nonSurrogateDiagnostic);
362 printf("Records: %d\n", p->u.databaseOrSurDiagnostics->num_records);
363 for (i = 0; i < p->u.databaseOrSurDiagnostics->num_records; i++)
364 display_nameplusrecord(p->u.databaseOrSurDiagnostics->records[i]);
368 static int send_searchRequest(char *arg)
370 Z_APDU *apdu = zget_APDU(out, Z_APDU_searchRequest);
371 Z_SearchRequest *req = apdu->u.searchRequest;
372 char *databaseNames = database;
376 struct ccl_rpn_node *rpn;
382 Z_RPNQuery *RPNquery;
390 rpn = ccl_find_str(bibset, arg, &error, &pos);
393 printf("CCL ERROR: %s\n", ccl_err_msg(error));
399 if (!strcmp(arg, "@big")) /* strictly for troublemaking */
401 static unsigned char big[2100];
404 /* send a very big referenceid to test transport stack etc. */
405 memset(big, 'A', 2100);
406 bigo.len = bigo.size = 2100;
408 req->referenceId = &bigo;
413 sprintf(setstring, "%d", ++setnumber);
414 req->resultSetName = setstring;
416 *req->smallSetUpperBound = smallSetUpperBound;
417 *req->largeSetLowerBound = largeSetLowerBound;
418 *req->mediumSetPresentNumber = mediumSetPresentNumber;
419 if (smallSetUpperBound > 0 || (largeSetLowerBound > 1 &&
420 mediumSetPresentNumber > 0))
424 prefsyn.proto = protocol;
425 prefsyn.class = CLASS_RECSYN;
426 prefsyn.value = recordsyntax;
427 req->preferredRecordSyntax = odr_oiddup(out, oid_getoidbyent(&prefsyn));
429 req->num_databaseNames = 1;
430 req->databaseNames = &databaseNames;
435 query.which = Z_Query_type_1;
438 assert((RPNquery = ccl_rpn_query(rpn)));
440 RPNquery = p_query_rpn (out, arg);
443 printf("Prefix query error\n");
447 bib1.proto = protocol;
448 bib1.class = CLASS_ATTSET;
449 bib1.value = VAL_BIB1;
450 RPNquery->attributeSetId = oid_getoidbyent(&bib1);
451 query.u.type_1 = RPNquery;
453 query.which = Z_Query_type_2;
454 query.u.type_2 = &ccl_query;
455 ccl_query.buf = (unsigned char*) arg;
456 ccl_query.len = strlen(arg);
461 printf("Sent searchRequest.\n");
465 static int process_searchResponse(Z_SearchResponse *res)
467 if (*res->searchStatus)
468 printf("Search was a success.\n");
470 printf("Search was a bloomin' failure.\n");
471 printf("Number of hits: %d, setno %d\n",
472 *res->resultCount, setnumber);
473 printf("records returned: %d\n",
474 *res->numberOfRecordsReturned);
475 setno += *res->numberOfRecordsReturned;
477 display_records(res->records);
481 static int cmd_find(char *arg)
485 printf("Find what?\n");
490 printf("Not connected yet\n");
493 if (!send_searchRequest(arg))
498 static int cmd_ssub(char *arg)
500 if (!(smallSetUpperBound = atoi(arg)))
505 static int cmd_lslb(char *arg)
507 if (!(largeSetLowerBound = atoi(arg)))
512 static int cmd_mspn(char *arg)
514 if (!(mediumSetPresentNumber = atoi(arg)))
519 static int cmd_status(char *arg)
521 printf("smallSetUpperBound: %d\n", smallSetUpperBound);
522 printf("largeSetLowerBound: %d\n", largeSetLowerBound);
523 printf("mediumSetPresentNumber: %d\n", mediumSetPresentNumber);
527 static int cmd_base(char *arg)
531 printf("Usage: base <database>\n");
534 strcpy(database, arg);
538 static int cmd_setnames(char *arg)
542 printf("Set numbering enabled.\n");
547 printf("Set numbering disabled.\n");
553 /* PRESENT SERVICE ----------------------------- */
555 static int send_presentRequest(char *arg)
557 Z_APDU *apdu = zget_APDU(out, Z_APDU_presentRequest);
558 Z_PresentRequest *req = apdu->u.presentRequest;
564 if ((p = strchr(arg, '+')))
574 sprintf(setstring, "%d", setnumber);
575 req->resultSetId = setstring;
577 req->resultSetStartPoint = &setno;
578 req->numberOfRecordsRequested = &nos;
579 prefsyn.proto = protocol;
580 prefsyn.class = CLASS_RECSYN;
581 prefsyn.value = recordsyntax;
582 req->preferredRecordSyntax = oid_getoidbyent(&prefsyn);
584 printf("Sent presentRequest (%d+%d).\n", setno, nos);
588 static int cmd_show(char *arg)
590 if (!send_presentRequest(arg))
595 int cmd_quit(char *arg)
597 printf("See you later, alligator.\n");
601 int cmd_cancel(char *arg)
603 Z_APDU *apdu = zget_APDU(out, Z_APDU_triggerResourceControlRequest);
604 Z_TriggerResourceControlRequest *req =
605 apdu->u.triggerResourceControlRequest;
610 printf("Session not initialized yet\n");
613 if (!ODR_MASK_GET(session->options, Z_Options_triggerResourceCtrl))
615 printf("Target doesn't support cancel (trigger resource ctrl)\n");
618 *req->requestedAction = Z_TriggerResourceCtrl_cancel;
619 req->resultSetWanted = &false;
622 printf("Sent cancel request\n");
626 int send_scanrequest(char *string, int pp, int num)
628 Z_APDU *apdu = zget_APDU(out, Z_APDU_scanRequest);
629 Z_ScanRequest *req = apdu->u.scanRequest;
633 req->num_databaseNames = 1;
634 req->databaseNames = &db;
635 attset.proto = protocol;
636 attset.class = CLASS_ATTSET;
637 attset.value = VAL_BIB1;
638 req->attributeSet = oid_getoidbyent(&attset);
639 req->termListAndStartPoint = p_query_scan(out, string);
640 req->numberOfTermsRequested = #
641 req->preferredPositionInResponse = &pp;
646 void display_term(Z_TermInfo *t)
648 if (t->term->which == Z_Term_general)
650 printf("%.*s (%d)\n", t->term->u.general->len, t->term->u.general->buf,
651 t->globalOccurrences ? *t->globalOccurrences : -1);
652 sprintf(last_scan, "%.*s", t->term->u.general->len,
653 t->term->u.general->buf);
656 printf("Term type not general.\n");
659 void process_scanResponse(Z_ScanResponse *res)
663 printf("SCAN: %d entries, position=%d\n", *res->numberOfEntriesReturned,
664 *res->positionOfTerm);
665 if (*res->scanStatus != Z_Scan_success)
666 printf("Scan returned code %d\n", *res->scanStatus);
669 if (res->entries->which == Z_ListEntries_entries)
671 Z_Entries *ent = res->entries->u.entries;
673 for (i = 0; i < ent->num_entries; i++)
674 if (ent->entries[i]->which == Z_Entry_termInfo)
676 printf("%c ", i + 1 == *res->positionOfTerm ? '*' : ' ');
677 display_term(ent->entries[i]->u.termInfo);
680 display_diagrec(ent->entries[i]->u.surrogateDiagnostic);
683 display_diagrec(res->entries->u.nonSurrogateDiagnostics->diagRecs[0]);
686 int cmd_scan(char *arg)
690 printf("Session not initialized yet\n");
693 if (!ODR_MASK_GET(session->options, Z_Options_scan))
695 printf("Target doesn't support scan\n");
700 if (send_scanrequest(arg, 5, 20) < 0)
704 if (send_scanrequest(last_scan, 1, 20) < 0)
709 int cmd_format(char *arg)
713 printf("Usage: format <recordsyntax>\n");
716 if (!strcmp(arg, "sutrs"))
718 printf("Preferred format is SUTRS.\n");
719 recordsyntax = VAL_SUTRS;
722 else if (!strcmp(arg, "usmarc"))
724 printf("Preferred format is USMARC\n");
725 recordsyntax = VAL_USMARC;
728 else if (!strcmp(arg, "danmarc"))
730 printf("Preferred format is DANMARC\n");
731 recordsyntax = VAL_DANMARC;
736 printf("Specify one of {sutrs,usmarc,danmarc}.\n");
741 static void initialize(void)
749 if (!(out = odr_createmem(ODR_ENCODE)) ||
750 !(in = odr_createmem(ODR_DECODE)) ||
751 !(print = odr_createmem(ODR_PRINT)))
753 fprintf(stderr, "failed to allocate ODR streams\n");
756 setvbuf(stdout, 0, _IONBF, 0);
760 bibset = ccl_qual_mk ();
761 inf = fopen ("default.bib", "r");
764 ccl_qual_file (bibset, inf);
771 static int client(void)
775 int (*fun)(char *arg);
778 {"open", cmd_open, "('tcp'|'osi')':'[<TSEL>'/']<HOST>[':'<PORT>]"},
779 {"quit", cmd_quit, ""},
780 {"find", cmd_find, "<CCL-QUERY>"},
781 {"base", cmd_base, "<BASE-NAME>"},
782 {"show", cmd_show, "<REC#>['+'<#RECS>]"},
783 {"scan", cmd_scan, "<TERM>"},
784 {"authentication", cmd_authentication, "<ACCTSTRING>"},
785 {"lslb", cmd_lslb, "<largeSetLowerBound>"},
786 {"ssub", cmd_ssub, "<smallSetUpperBound>"},
787 {"mspn", cmd_mspn, "<mediumSetPresentNumber>"},
788 {"status", cmd_status, ""},
789 {"setnames", cmd_setnames, ""},
790 {"cancel", cmd_cancel, ""},
791 {"format", cmd_format, "<recordsyntax>"},
795 int netbufferlen = 0;
803 char line[1024], word[1024], arg[1024];
808 FD_SET(cs_fileno(conn), &input);
809 if ((res = select(20, &input, 0, 0, 0)) < 0)
816 if (FD_ISSET(0, &input))
818 /* quick & dirty way to get a command line. */
821 if ((res = sscanf(line, "%s %[^;]", word, arg)) <= 0)
823 strcpy(word, last_cmd);
828 strcpy(last_cmd, word);
829 for (i = 0; cmd[i].cmd; i++)
830 if (!strncmp(cmd[i].cmd, word, strlen(word)))
832 res = (*cmd[i].fun)(arg);
835 if (!cmd[i].cmd) /* dump our help-screen */
837 printf("Unknown command: %s.\n", word);
838 printf("Currently recognized commands:\n");
839 for (i = 0; cmd[i].cmd; i++)
840 printf(" %s %s\n", cmd[i].cmd, cmd[i].ad);
846 if (conn && FD_ISSET(cs_fileno(conn), &input))
850 if ((res = cs_get(conn, &netbuffer, &netbufferlen)) < 0)
857 printf("Target closed connection.\n");
860 odr_reset(in); /* release APDU from last round */
861 odr_setbuf(in, netbuffer, res, 0);
862 if (!z_APDU(in, &apdu, 0))
864 odr_perror(in, "Decoding incoming APDU");
865 fprintf(stderr, "Packet dump:\n---------\n");
866 odr_dumpBER(stderr, netbuffer, res);
867 fprintf(stderr, "---------\n");
871 if (!z_APDU(print, &apdu, 0))
873 odr_perror(print, "Failed to print incoming APDU");
880 case Z_APDU_initResponse:
881 process_initResponse(apdu->u.initResponse);
883 case Z_APDU_searchResponse:
884 process_searchResponse(apdu->u.searchResponse);
886 case Z_APDU_scanResponse:
887 process_scanResponse(apdu->u.scanResponse);
889 case Z_APDU_presentResponse:
890 printf("Received presentResponse.\n");
892 *apdu->u.presentResponse->numberOfRecordsReturned;
893 if (apdu->u.presentResponse->records)
894 display_records(apdu->u.presentResponse->records);
896 printf("No records.\n");
899 printf("Received unknown APDU type (%d).\n",
906 while (cs_more(conn));
912 int main(int argc, char **argv)