2 * Copyright (c) 1995-1996, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.40 1996-08-29 14:19:34 quinn
10 * Revision 1.39 1996/08/27 10:43:22 quinn
11 * Made select() optional
13 * Revision 1.38 1996/08/12 14:09:11 adam
14 * Default prefix query attribute set defined by using p_query_attset.
16 * Revision 1.37 1996/07/06 19:58:29 quinn
17 * System headerfiles gathered in yconfig
19 * Revision 1.36 1996/06/10 08:53:47 quinn
22 * Revision 1.35 1996/06/03 09:45:50 quinn
23 * Added display of OIDs in the GRS routine.
25 * Revision 1.34 1996/05/09 07:26:49 quinn
26 * *** empty log message ***
28 * Revision 1.33 1996/05/09 07:25:22 quinn
31 * Revision 1.32 1996/03/15 11:05:33 adam
32 * The user can set the preferred query type (prefix, ccl, ..) with the
35 * Revision 1.31 1996/02/20 12:51:54 quinn
36 * Fixed problems with EXTERNAL.
38 * Revision 1.30 1996/02/12 18:18:09 quinn
41 * Revision 1.29 1996/01/02 08:57:25 quinn
42 * Changed enums in the ASN.1 .h files to #defines. Changed oident.class to oclass
44 * Revision 1.28 1995/12/14 11:09:31 quinn
45 * Added Explain record syntax to the format command.
47 * Revision 1.27 1995/12/12 16:37:02 quinn
48 * Added destroy element to data1_node.
50 * Revision 1.26 1995/12/12 14:11:00 quinn
53 * Revision 1.25 1995/11/13 09:27:22 quinn
54 * Fiddling with the variant stuff.
56 * Revision 1.24 1995/10/30 12:41:13 quinn
57 * Added hostname lookup for server.
59 * Revision 1.23 1995/10/18 16:12:30 quinn
62 * Revision 1.22 1995/10/11 14:49:12 quinn
65 * Revision 1.21 1995/09/29 17:01:47 quinn
68 * Revision 1.20 1995/08/29 14:24:13 quinn
69 * Added second half of close-handshake
71 * Revision 1.19 1995/08/29 11:17:28 quinn
72 * Added code to receive close
74 * Revision 1.18 1995/08/28 12:21:27 quinn
75 * Client can now ask for simple element set names.
77 * Revision 1.17 1995/08/17 12:45:02 quinn
78 * Fixed minor problems with GRS-1. Added support in c&s.
80 * Revision 1.16 1995/08/15 12:00:04 quinn
83 * Revision 1.15 1995/06/22 09:28:03 quinn
84 * Fixed bug in SUTRS processing.
86 * Revision 1.14 1995/06/19 12:37:41 quinn
89 * Revision 1.13 1995/06/16 10:29:11 quinn
90 * *** empty log message ***
92 * Revision 1.12 1995/06/15 07:44:57 quinn
95 * Revision 1.11 1995/06/14 15:26:40 quinn
96 * *** empty log message ***
98 * Revision 1.10 1995/06/06 14:56:58 quinn
101 * Revision 1.9 1995/06/06 08:15:19 quinn
104 * Revision 1.8 1995/06/05 10:52:22 quinn
107 * Revision 1.7 1995/06/02 09:50:09 quinn
110 * Revision 1.6 1995/05/31 08:29:21 quinn
111 * Nothing significant.
113 * Revision 1.5 1995/05/29 08:10:47 quinn
114 * Moved oid.c to util.
116 * Revision 1.4 1995/05/22 15:30:13 adam
117 * Client uses prefix query notation.
119 * Revision 1.3 1995/05/22 15:06:53 quinn
120 * *** empty log message ***
122 * Revision 1.2 1995/05/22 14:56:40 quinn
123 * *** empty log message ***
125 * Revision 1.1 1995/05/22 11:30:31 quinn
126 * Added prettier client.
132 * This is the obligatory little toy client, whose primary purpose is
133 * to illustrate the use of the YAZ service-level API.
142 #include <comstack.h>
149 #include <marcdisp.h>
150 #include <diagbib1.h>
158 #define C_PROMPT "Z> "
160 static ODR out, in, print; /* encoding and decoding streams */
161 static COMSTACK conn = 0; /* our z-association */
162 static Z_IdAuthentication *auth = 0; /* our current auth definition */
163 static char database[512] = "Default"; /* Database name */
164 static int setnumber = 0; /* current result set number */
165 static int smallSetUpperBound = 0;
166 static int largeSetLowerBound = 1;
167 static int mediumSetPresentNumber = 0;
168 static Z_ElementSetNames *elementSetNames = 0;
169 static int setno = 1; /* current set offset */
170 static int protocol = PROTO_Z3950; /* current app protocol */
171 static int recordsyntax = VAL_USMARC;
172 static int sent_close = 0;
173 static ODR_MEM session_mem; /* memory handle for init-response */
174 static Z_InitResponse *session = 0; /* session parameters */
175 static char last_scan[512] = "0";
176 static char last_cmd[100] = "?";
177 static FILE *marcdump = 0;
178 static char marcdump_file[512] = "marc.out";
186 static QueryType queryType = QueryType_Prefix;
189 static CCL_bibset bibset; /* CCL bibset handle */
192 static void send_apdu(Z_APDU *a)
197 if (!z_APDU(out, &a, 0))
199 odr_perror(out, "Encoding APDU");
202 buf = odr_getbuf(out, &len, 0);
203 odr_reset(out); /* release the APDU structure */
204 if (cs_put(conn, buf, len) < 0)
206 fprintf(stderr, "cs_put: %s", cs_errmsg(cs_errno(conn)));
211 /* INIT SERVICE ------------------------------- */
213 static void send_initRequest()
215 Z_APDU *apdu = zget_APDU(out, Z_APDU_initRequest);
216 Z_InitRequest *req = apdu->u.initRequest;
218 ODR_MASK_SET(req->options, Z_Options_search);
219 ODR_MASK_SET(req->options, Z_Options_present);
220 ODR_MASK_SET(req->options, Z_Options_namedResultSets);
221 ODR_MASK_SET(req->options, Z_Options_triggerResourceCtrl);
222 ODR_MASK_SET(req->options, Z_Options_scan);
224 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_1);
225 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_2);
226 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_3);
228 *req->maximumRecordSize = 1024*1024;
230 req->idAuthentication = auth;
233 printf("Sent initrequest.\n");
236 static int process_initResponse(Z_InitResponse *res)
238 /* save session parameters for later use */
239 session_mem = odr_extract_mem(in);
243 printf("Connection rejected by target.\n");
245 printf("Connection accepted by target.\n");
246 if (res->implementationId)
247 printf("ID : %s\n", res->implementationId);
248 if (res->implementationName)
249 printf("Name : %s\n", res->implementationName);
250 if (res->implementationVersion)
251 printf("Version: %s\n", res->implementationVersion);
252 if (res->userInformationField)
254 printf("UserInformationfield:\n");
255 if (!z_External(print, (Z_External**)&res-> userInformationField,
258 odr_perror(print, "Printing userinfo\n");
261 if (res->userInformationField->which == Z_External_octet)
263 printf("Guessing visiblestring:\n");
264 printf("'%s'\n", res->userInformationField->u. octet_aligned->buf);
270 int cmd_open(char *arg)
273 char type[100], addr[100];
278 printf("Already connected.\n");
281 if (!*arg || sscanf(arg, "%[^:]:%s", type, addr) < 2)
283 fprintf(stderr, "Usage: open (osi|tcp) ':' [tsel '/']host[':'port]\n");
287 if (!strcmp(type, "osi"))
289 if (!(add = mosi_strtoaddr(addr)))
299 if (!strcmp(type, "tcp"))
301 if (!(add = tcpip_strtoaddr(addr)))
307 protocol = PROTO_Z3950;
311 fprintf(stderr, "Bad type: %s\n", type);
314 if (!(conn = cs_create(t, 1, protocol)))
319 printf("Connecting...");
321 if (cs_connect(conn, add) < 0)
333 int cmd_authentication(char *arg)
335 static Z_IdAuthentication au;
336 static char open[256];
340 printf("Auth field set to null\n");
345 au.which = Z_IdAuthentication_open;
351 /* SEARCH SERVICE ------------------------------ */
353 static void display_variant(Z_Variant *v, int level)
357 for (i = 0; i < v->num_triples; i++)
359 printf("%*sclass=%d,type=%d", level * 4, "", *v->triples[i]->class,
360 *v->triples[i]->type);
361 if (v->triples[i]->which == Z_Triple_internationalString)
362 printf(",value=%s\n", v->triples[i]->value.internationalString);
368 static void display_grs1(Z_GenericRecord *r, int level)
374 for (i = 0; i < r->num_elements; i++)
378 printf("%*s", level * 4, "");
382 printf("%d,", *t->tagType);
385 if (t->tagValue->which == Z_StringOrNumeric_numeric)
386 printf("%d) ", *t->tagValue->u.numeric);
388 printf("%s) ", t->tagValue->u.string);
389 if (t->content->which == Z_ElementData_subtree)
392 display_grs1(t->content->u.subtree, level+1);
394 else if (t->content->which == Z_ElementData_string)
395 printf("%s\n", t->content->u.string);
396 else if (t->content->which == Z_ElementData_numeric)
397 printf("%d\n", *t->content->u.numeric);
398 else if (t->content->which == Z_ElementData_oid)
400 int *ip = t->content->u.oid;
403 if ((oent = oid_getentbyoid(t->content->u.oid)))
404 printf("OID: %s\n", oent->desc);
408 while (ip && *ip >= 0)
409 printf(" %d", *(ip++));
413 else if (t->content->which == Z_ElementData_noDataRequested)
414 printf("[No data requested]\n");
415 else if (t->content->which == Z_ElementData_elementEmpty)
416 printf("[Element empty]\n");
417 else if (t->content->which == Z_ElementData_elementNotThere)
418 printf("[Element not there]\n");
421 if (t->appliedVariant)
422 display_variant(t->appliedVariant, level+1);
423 if (t->metaData && t->metaData->supportedVariants)
427 printf("%*s---- variant list\n", (level+1)*4, "");
428 for (c = 0; c < t->metaData->num_supportedVariants; c++)
430 printf("%*svariant #%d\n", (level+1)*4, "", c);
431 display_variant(t->metaData->supportedVariants[c], level + 2);
437 static void display_record(Z_DatabaseRecord *p)
439 Z_External *r = (Z_External*) p;
440 oident *ent = oid_getentbyoid(r->direct_reference);
443 * Tell the user what we got.
445 if (r->direct_reference)
447 printf("Record type: ");
449 printf("%s\n", ent->desc);
450 else if (!odr_oid(print, &r->direct_reference, 0))
452 odr_perror(print, "print oid");
457 /* Check if this is a known, ASN.1 type tucked away in an octet string */
458 if (ent && r->which == Z_External_octet)
460 Z_ext_typeent *type = z_ext_getentbyref(ent->value);
466 * Call the given decoder to process the record.
468 odr_setbuf(in, (char*)p->u.octet_aligned->buf,
469 p->u.octet_aligned->len, 0);
470 if (!(*type->fun)(in, &rr, 0))
472 odr_perror(in, "Decoding constructed record.");
473 fprintf(stderr, "[Near %d]\n", odr_offset(in));
474 fprintf(stderr, "Packet dump:\n---------\n");
475 odr_dumpBER(stderr, (char*)p->u.octet_aligned->buf,
476 p->u.octet_aligned->len);
477 fprintf(stderr, "---------\n");
481 * Note: we throw away the original, BER-encoded record here.
482 * Do something else with it if you want to keep it.
484 r->u.sutrs = rr; /* we don't actually check the type here. */
485 r->which = type->what;
488 if (r->which == Z_External_octet && p->u.octet_aligned->len)
489 marc_display ((char*)p->u.octet_aligned->buf, stdout);
490 else if (ent->value == VAL_SUTRS)
492 if (r->which != Z_External_sutrs)
494 printf("Expecting single SUTRS type for SUTRS.\n");
497 printf("%.*s", r->u.sutrs->len, r->u.sutrs->buf);
499 else if (ent->value == VAL_GRS1)
501 if (r->which != Z_External_grs1)
503 printf("Expecting single GRS type for GRS.\n");
506 display_grs1(r->u.grs1, 0);
510 printf("Unknown record representation.\n");
511 if (!z_External(print, &r, 0))
513 odr_perror(print, "Printing external");
519 static void display_diagrec(Z_DiagRec *p)
523 Z_DefaultDiagFormat *r;
528 printf("Diagnostic message from database:\n");
530 if (p->which != Z_DiagRec_defaultFormat)
532 printf("Diagnostic record not in default format.\n");
536 r = p->u.defaultFormat;
538 if (!(ent = oid_getentbyoid(r->diagnosticSetId)) ||
539 ent->oclass != CLASS_DIAGSET || ent->value != VAL_BIB1)
540 printf("Missing or unknown diagset\n");
541 printf(" [%d] %s", *r->condition, diagbib1_str(*r->condition));
542 if (r->addinfo && *r->addinfo)
543 printf(" -- '%s'\n", r->addinfo);
548 static void display_nameplusrecord(Z_NamePlusRecord *p)
551 printf("[%s]", p->databaseName);
552 if (p->which == Z_NamePlusRecord_surrogateDiagnostic)
553 display_diagrec(p->u.surrogateDiagnostic);
555 display_record(p->u.databaseRecord);
558 static void display_records(Z_Records *p)
562 if (p->which == Z_Records_NSD)
563 display_diagrec(p->u.nonSurrogateDiagnostic);
566 printf("Records: %d\n", p->u.databaseOrSurDiagnostics->num_records);
567 for (i = 0; i < p->u.databaseOrSurDiagnostics->num_records; i++)
568 display_nameplusrecord(p->u.databaseOrSurDiagnostics->records[i]);
572 static int send_searchRequest(char *arg)
574 Z_APDU *apdu = zget_APDU(out, Z_APDU_searchRequest);
575 Z_SearchRequest *req = apdu->u.searchRequest;
576 char *databaseNames = database;
579 struct ccl_rpn_node *rpn;
584 Z_RPNQuery *RPNquery;
588 if (queryType == QueryType_CCL2RPN)
590 rpn = ccl_find_str(bibset, arg, &error, &pos);
593 printf("CCL ERROR: %s\n", ccl_err_msg(error));
599 if (!strcmp(arg, "@big")) /* strictly for troublemaking */
601 static unsigned char big[2100];
604 /* send a very big referenceid to test transport stack etc. */
605 memset(big, 'A', 2100);
606 bigo.len = bigo.size = 2100;
608 req->referenceId = &bigo;
613 sprintf(setstring, "%d", ++setnumber);
614 req->resultSetName = setstring;
616 *req->smallSetUpperBound = smallSetUpperBound;
617 *req->largeSetLowerBound = largeSetLowerBound;
618 *req->mediumSetPresentNumber = mediumSetPresentNumber;
619 if (smallSetUpperBound > 0 || (largeSetLowerBound > 1 &&
620 mediumSetPresentNumber > 0))
624 prefsyn.proto = protocol;
625 prefsyn.oclass = CLASS_RECSYN;
626 prefsyn.value = recordsyntax;
627 req->preferredRecordSyntax =
628 odr_oiddup(out, oid_getoidbyent(&prefsyn));
629 req->smallSetElementSetNames =
630 req->mediumSetElementSetNames = elementSetNames;
632 req->num_databaseNames = 1;
633 req->databaseNames = &databaseNames;
639 case QueryType_Prefix:
640 query.which = Z_Query_type_1;
641 RPNquery = p_query_rpn (out, protocol, arg);
644 printf("Prefix query error\n");
647 query.u.type_1 = RPNquery;
650 query.which = Z_Query_type_2;
651 query.u.type_2 = &ccl_query;
652 ccl_query.buf = (unsigned char*) arg;
653 ccl_query.len = strlen(arg);
656 case QueryType_CCL2RPN:
657 query.which = Z_Query_type_1;
658 assert((RPNquery = ccl_rpn_query(rpn)));
659 bib1.proto = protocol;
660 bib1.oclass = CLASS_ATTSET;
661 bib1.value = VAL_BIB1;
662 RPNquery->attributeSetId = oid_getoidbyent(&bib1);
663 query.u.type_1 = RPNquery;
667 printf ("Unsupported query type\n");
672 printf("Sent searchRequest.\n");
676 static int process_searchResponse(Z_SearchResponse *res)
678 if (*res->searchStatus)
679 printf("Search was a success.\n");
681 printf("Search was a bloomin' failure.\n");
682 printf("Number of hits: %d, setno %d\n",
683 *res->resultCount, setnumber);
684 printf("records returned: %d\n",
685 *res->numberOfRecordsReturned);
686 setno += *res->numberOfRecordsReturned;
688 display_records(res->records);
692 static int cmd_find(char *arg)
696 printf("Find what?\n");
701 printf("Not connected yet\n");
704 if (!send_searchRequest(arg))
709 static int cmd_ssub(char *arg)
711 if (!(smallSetUpperBound = atoi(arg)))
716 static int cmd_lslb(char *arg)
718 if (!(largeSetLowerBound = atoi(arg)))
723 static int cmd_mspn(char *arg)
725 if (!(mediumSetPresentNumber = atoi(arg)))
730 static int cmd_status(char *arg)
732 printf("smallSetUpperBound: %d\n", smallSetUpperBound);
733 printf("largeSetLowerBound: %d\n", largeSetLowerBound);
734 printf("mediumSetPresentNumber: %d\n", mediumSetPresentNumber);
738 static int cmd_base(char *arg)
742 printf("Usage: base <database>\n");
745 strcpy(database, arg);
749 static int cmd_setnames(char *arg)
753 printf("Set numbering enabled.\n");
758 printf("Set numbering disabled.\n");
764 /* PRESENT SERVICE ----------------------------- */
766 static int send_presentRequest(char *arg)
768 Z_APDU *apdu = zget_APDU(out, Z_APDU_presentRequest);
769 Z_PresentRequest *req = apdu->u.presentRequest;
770 Z_RecordComposition compo;
776 if ((p = strchr(arg, '+')))
786 sprintf(setstring, "%d", setnumber);
787 req->resultSetId = setstring;
794 static Z_Range range;
795 static Z_Range *rangep = ⦥
807 req->resultSetStartPoint = &setno;
808 req->numberOfRecordsRequested = &nos;
809 prefsyn.proto = protocol;
810 prefsyn.oclass = CLASS_RECSYN;
811 prefsyn.value = recordsyntax;
812 req->preferredRecordSyntax = oid_getoidbyent(&prefsyn);
815 req->recordComposition = &compo;
816 compo.which = Z_RecordComp_simple;
817 compo.u.simple = elementSetNames;
820 printf("Sent presentRequest (%d+%d).\n", setno, nos);
824 void process_close(Z_Close *req)
826 Z_APDU *apdu = zget_APDU(out, Z_APDU_close);
827 Z_Close *res = apdu->u.close;
829 static char *reasons[] =
834 "cost limit reached",
836 "security violation",
843 printf("Reason: %s, message: %s\n", reasons[*req->closeReason],
844 req->diagnosticInformation ? req->diagnosticInformation : "NULL");
847 printf("Goodbye.\n");
850 *res->closeReason = Z_Close_finished;
852 printf("Sent response.\n");
856 static int cmd_show(char *arg)
858 if (!send_presentRequest(arg))
863 int cmd_quit(char *arg)
865 printf("See you later, alligator.\n");
869 int cmd_cancel(char *arg)
871 Z_APDU *apdu = zget_APDU(out, Z_APDU_triggerResourceControlRequest);
872 Z_TriggerResourceControlRequest *req =
873 apdu->u.triggerResourceControlRequest;
878 printf("Session not initialized yet\n");
881 if (!ODR_MASK_GET(session->options, Z_Options_triggerResourceCtrl))
883 printf("Target doesn't support cancel (trigger resource ctrl)\n");
886 *req->requestedAction = Z_TriggerResourceCtrl_cancel;
887 req->resultSetWanted = &false;
890 printf("Sent cancel request\n");
894 int send_scanrequest(char *string, int pp, int num)
896 Z_APDU *apdu = zget_APDU(out, Z_APDU_scanRequest);
897 Z_ScanRequest *req = apdu->u.scanRequest;
900 req->num_databaseNames = 1;
901 req->databaseNames = &db;
902 req->termListAndStartPoint = p_query_scan(out, protocol,
903 &req->attributeSet, string);
904 req->numberOfTermsRequested = #
905 req->preferredPositionInResponse = &pp;
910 void display_term(Z_TermInfo *t)
912 if (t->term->which == Z_Term_general)
914 printf("%.*s (%d)\n", t->term->u.general->len, t->term->u.general->buf,
915 t->globalOccurrences ? *t->globalOccurrences : -1);
916 sprintf(last_scan, "%.*s", t->term->u.general->len,
917 t->term->u.general->buf);
920 printf("Term type not general.\n");
923 void process_scanResponse(Z_ScanResponse *res)
927 printf("SCAN: %d entries, position=%d\n", *res->numberOfEntriesReturned,
928 *res->positionOfTerm);
929 if (*res->scanStatus != Z_Scan_success)
930 printf("Scan returned code %d\n", *res->scanStatus);
933 if (res->entries->which == Z_ListEntries_entries)
935 Z_Entries *ent = res->entries->u.entries;
937 for (i = 0; i < ent->num_entries; i++)
938 if (ent->entries[i]->which == Z_Entry_termInfo)
940 printf("%c ", i + 1 == *res->positionOfTerm ? '*' : ' ');
941 display_term(ent->entries[i]->u.termInfo);
944 display_diagrec(ent->entries[i]->u.surrogateDiagnostic);
947 display_diagrec(res->entries->u.nonSurrogateDiagnostics->diagRecs[0]);
950 int cmd_scan(char *arg)
954 printf("Session not initialized yet\n");
957 if (!ODR_MASK_GET(session->options, Z_Options_scan))
959 printf("Target doesn't support scan\n");
964 if (send_scanrequest(arg, 5, 20) < 0)
968 if (send_scanrequest(last_scan, 1, 20) < 0)
973 int cmd_format(char *arg)
977 printf("Usage: format <recordsyntax>\n");
980 if (!strcmp(arg, "sutrs"))
982 printf("Preferred format is SUTRS.\n");
983 recordsyntax = VAL_SUTRS;
986 else if (!strcmp(arg, "usmarc"))
988 printf("Preferred format is USMARC\n");
989 recordsyntax = VAL_USMARC;
992 else if (!strcmp(arg, "danmarc"))
994 printf("Preferred format is DANMARC\n");
995 recordsyntax = VAL_DANMARC;
998 else if (!strcmp(arg, "ukmarc"))
1000 printf("Preferred format is UKMARC\n");
1001 recordsyntax = VAL_UKMARC;
1004 else if (!strcmp(arg, "grs1"))
1006 printf("Preferred format is GRS1\n");
1007 recordsyntax = VAL_GRS1;
1010 else if (!strcmp(arg, "summary"))
1012 printf("Preferred format is Summary\n");
1013 recordsyntax = VAL_SUMMARY;
1016 else if (!strcmp(arg, "explain"))
1018 printf("Preferred format is Explain\n");
1019 recordsyntax = VAL_EXPLAIN;
1024 printf("Specify one of {sutrs,usmarc,danmarc,ukmarc,grs1,summary,explain}.\n");
1029 int cmd_elements(char *arg)
1031 static Z_ElementSetNames esn;
1032 static char what[100];
1036 printf("Usage: elements <esn>\n");
1040 esn.which = Z_ElementSetNames_generic;
1041 esn.u.generic = what;
1042 elementSetNames = &esn;
1046 int cmd_attributeset(char *arg)
1052 printf("Usage: attributeset <setname>\n");
1055 sscanf(arg, "%s", what);
1056 if (p_query_attset (what))
1058 printf("Unknown attribute set name\n");
1064 int cmd_querytype (char *arg)
1066 if (!strcmp (arg, "ccl"))
1067 queryType = QueryType_CCL;
1068 else if (!strcmp (arg, "prefix"))
1069 queryType = QueryType_Prefix;
1071 else if (!strcmp (arg, "ccl2rpn") || !strcmp (arg, "cclrpn"))
1072 queryType = QueryType_CCL2RPN;
1076 printf ("Querytype must be one of:\n");
1077 printf (" prefix - Prefix query\n");
1078 printf (" ccl - CCL query\n");
1080 printf (" ccl2rpn - CCL query converted to RPN\n");
1087 int cmd_close(char *arg)
1089 Z_APDU *apdu = zget_APDU(out, Z_APDU_close);
1090 Z_Close *req = apdu->u.close;
1092 *req->closeReason = Z_Close_finished;
1094 printf("Sent close request.\n");
1099 static void initialize(void)
1105 if (!(out = odr_createmem(ODR_ENCODE)) ||
1106 !(in = odr_createmem(ODR_DECODE)) ||
1107 !(print = odr_createmem(ODR_PRINT)))
1109 fprintf(stderr, "failed to allocate ODR streams\n");
1112 setvbuf(stdout, 0, _IONBF, 0);
1115 bibset = ccl_qual_mk ();
1116 inf = fopen ("default.bib", "r");
1119 ccl_qual_file (bibset, inf);
1125 static int client(int wait)
1129 int (*fun)(char *arg);
1132 {"open", cmd_open, "('tcp'|'osi')':'[<tsel>'/']<host>[':'<port>]"},
1133 {"quit", cmd_quit, ""},
1134 {"find", cmd_find, "<query>"},
1135 {"base", cmd_base, "<base-name>"},
1136 {"show", cmd_show, "<rec#>['+'<#recs>]"},
1137 {"scan", cmd_scan, "<term>"},
1138 {"authentication", cmd_authentication, "<acctstring>"},
1139 {"lslb", cmd_lslb, "<largeSetLowerBound>"},
1140 {"ssub", cmd_ssub, "<smallSetUpperBound>"},
1141 {"mspn", cmd_mspn, "<mediumSetPresentNumber>"},
1142 {"status", cmd_status, ""},
1143 {"setnames", cmd_setnames, ""},
1144 {"cancel", cmd_cancel, ""},
1145 {"format", cmd_format, "<recordsyntax>"},
1146 {"elements", cmd_elements, "<elementSetName>"},
1147 {"close", cmd_close, ""},
1148 {"attributeset", cmd_attributeset, "<attrset>"},
1149 {"querytype", cmd_querytype, "<type>"},
1153 int netbufferlen = 0;
1163 char line[1024], word[1024], arg[1024];
1169 FD_SET(cs_fileno(conn), &input);
1170 if ((res = select(20, &input, 0, 0, 0)) < 0)
1177 if (!wait && FD_ISSET(0, &input))
1182 /* quick & dirty way to get a command line. */
1185 if ((res = sscanf(line, "%s %[^;]", word, arg)) <= 0)
1187 strcpy(word, last_cmd);
1192 strcpy(last_cmd, word);
1193 for (i = 0; cmd[i].cmd; i++)
1194 if (!strncmp(cmd[i].cmd, word, strlen(word)))
1196 res = (*cmd[i].fun)(arg);
1199 if (!cmd[i].cmd) /* dump our help-screen */
1201 printf("Unknown command: %s.\n", word);
1202 printf("Currently recognized commands:\n");
1203 for (i = 0; cmd[i].cmd; i++)
1204 printf(" %s %s\n", cmd[i].cmd, cmd[i].ad);
1214 if (conn && FD_ISSET(cs_fileno(conn), &input))
1219 if ((res = cs_get(conn, &netbuffer, &netbufferlen)) < 0)
1226 printf("Target closed connection.\n");
1229 odr_reset(in); /* release APDU from last round */
1230 odr_setbuf(in, netbuffer, res, 0);
1231 if (!z_APDU(in, &apdu, 0))
1233 odr_perror(in, "Decoding incoming APDU");
1234 fprintf(stderr, "[Near %d]\n", odr_offset(in));
1235 fprintf(stderr, "Packet dump:\n---------\n");
1236 odr_dumpBER(stderr, netbuffer, res);
1237 fprintf(stderr, "---------\n");
1241 if (!z_APDU(print, &apdu, 0))
1243 odr_perror(print, "Failed to print incoming APDU");
1250 case Z_APDU_initResponse:
1251 process_initResponse(apdu->u.initResponse);
1253 case Z_APDU_searchResponse:
1254 process_searchResponse(apdu->u.searchResponse);
1256 case Z_APDU_scanResponse:
1257 process_scanResponse(apdu->u.scanResponse);
1259 case Z_APDU_presentResponse:
1260 printf("Received presentResponse.\n");
1262 *apdu->u.presentResponse->numberOfRecordsReturned;
1263 if (apdu->u.presentResponse->records)
1264 display_records(apdu->u.presentResponse->records);
1266 printf("No records.\n");
1269 printf("Target has closed the association.\n");
1270 process_close(apdu->u.close);
1273 printf("Received unknown APDU type (%d).\n",
1280 while (cs_more(conn));
1287 int main(int argc, char **argv)
1294 if (*marcdump_file && !(marcdump = fopen(marcdump_file, "a")))
1296 perror(marcdump_file);
1299 return client((argc > 1));