2 * Copyright (c) 1995-1996, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.44 1997-05-05 11:20:35 adam
8 * Client uses "options" utility and marc dump filename may be specified
9 * as an option (-m <file>).
11 * Revision 1.43 1996/11/08 11:03:26 adam
12 * Client accepts multiple database names.
14 * Revision 1.42 1996/10/08 10:44:57 quinn
17 * Revision 1.41 1996/10/07 15:29:03 quinn
20 * Revision 1.40 1996/08/29 14:19:34 quinn
21 * Fixed conflict (CVS)
23 * Revision 1.39 1996/08/27 10:43:22 quinn
24 * Made select() optional
26 * Revision 1.38 1996/08/12 14:09:11 adam
27 * Default prefix query attribute set defined by using p_query_attset.
29 * Revision 1.37 1996/07/06 19:58:29 quinn
30 * System headerfiles gathered in yconfig
32 * Revision 1.36 1996/06/10 08:53:47 quinn
35 * Revision 1.35 1996/06/03 09:45:50 quinn
36 * Added display of OIDs in the GRS routine.
38 * Revision 1.34 1996/05/09 07:26:49 quinn
39 * *** empty log message ***
41 * Revision 1.33 1996/05/09 07:25:22 quinn
44 * Revision 1.32 1996/03/15 11:05:33 adam
45 * The user can set the preferred query type (prefix, ccl, ..) with the
48 * Revision 1.31 1996/02/20 12:51:54 quinn
49 * Fixed problems with EXTERNAL.
51 * Revision 1.30 1996/02/12 18:18:09 quinn
54 * Revision 1.29 1996/01/02 08:57:25 quinn
55 * Changed enums in the ASN.1 .h files to #defines. Changed oident.class to oclass
57 * Revision 1.28 1995/12/14 11:09:31 quinn
58 * Added Explain record syntax to the format command.
60 * Revision 1.27 1995/12/12 16:37:02 quinn
61 * Added destroy element to data1_node.
63 * Revision 1.26 1995/12/12 14:11:00 quinn
66 * Revision 1.25 1995/11/13 09:27:22 quinn
67 * Fiddling with the variant stuff.
69 * Revision 1.24 1995/10/30 12:41:13 quinn
70 * Added hostname lookup for server.
72 * Revision 1.23 1995/10/18 16:12:30 quinn
75 * Revision 1.22 1995/10/11 14:49:12 quinn
78 * Revision 1.21 1995/09/29 17:01:47 quinn
81 * Revision 1.20 1995/08/29 14:24:13 quinn
82 * Added second half of close-handshake
84 * Revision 1.19 1995/08/29 11:17:28 quinn
85 * Added code to receive close
87 * Revision 1.18 1995/08/28 12:21:27 quinn
88 * Client can now ask for simple element set names.
90 * Revision 1.17 1995/08/17 12:45:02 quinn
91 * Fixed minor problems with GRS-1. Added support in c&s.
93 * Revision 1.16 1995/08/15 12:00:04 quinn
96 * Revision 1.15 1995/06/22 09:28:03 quinn
97 * Fixed bug in SUTRS processing.
99 * Revision 1.14 1995/06/19 12:37:41 quinn
102 * Revision 1.13 1995/06/16 10:29:11 quinn
103 * *** empty log message ***
105 * Revision 1.12 1995/06/15 07:44:57 quinn
108 * Revision 1.11 1995/06/14 15:26:40 quinn
109 * *** empty log message ***
111 * Revision 1.10 1995/06/06 14:56:58 quinn
112 * Better diagnostics.
114 * Revision 1.9 1995/06/06 08:15:19 quinn
117 * Revision 1.8 1995/06/05 10:52:22 quinn
120 * Revision 1.7 1995/06/02 09:50:09 quinn
123 * Revision 1.6 1995/05/31 08:29:21 quinn
124 * Nothing significant.
126 * Revision 1.5 1995/05/29 08:10:47 quinn
127 * Moved oid.c to util.
129 * Revision 1.4 1995/05/22 15:30:13 adam
130 * Client uses prefix query notation.
132 * Revision 1.3 1995/05/22 15:06:53 quinn
133 * *** empty log message ***
135 * Revision 1.2 1995/05/22 14:56:40 quinn
136 * *** empty log message ***
138 * Revision 1.1 1995/05/22 11:30:31 quinn
139 * Added prettier client.
145 * This is the obligatory little toy client, whose primary purpose is
146 * to illustrate the use of the YAZ service-level API.
155 #include <comstack.h>
162 #include <marcdisp.h>
163 #include <diagbib1.h>
172 #define C_PROMPT "Z> "
174 static ODR out, in, print; /* encoding and decoding streams */
175 static COMSTACK conn = 0; /* our z-association */
176 static Z_IdAuthentication *auth = 0; /* our current auth definition */
177 static char *databaseNames[128];
178 static int num_databaseNames = 0;
179 static int setnumber = 0; /* current result set number */
180 static int smallSetUpperBound = 0;
181 static int largeSetLowerBound = 1;
182 static int mediumSetPresentNumber = 0;
183 static Z_ElementSetNames *elementSetNames = 0;
184 static int setno = 1; /* current set offset */
185 static int protocol = PROTO_Z3950; /* current app protocol */
186 static int recordsyntax = VAL_USMARC;
187 static int sent_close = 0;
188 static ODR_MEM session_mem; /* memory handle for init-response */
189 static Z_InitResponse *session = 0; /* session parameters */
190 static char last_scan[512] = "0";
191 static char last_cmd[100] = "?";
192 static FILE *marcdump = 0;
200 static QueryType queryType = QueryType_Prefix;
203 static CCL_bibset bibset; /* CCL bibset handle */
206 static void send_apdu(Z_APDU *a)
211 if (!z_APDU(out, &a, 0))
213 odr_perror(out, "Encoding APDU");
216 buf = odr_getbuf(out, &len, 0);
217 odr_reset(out); /* release the APDU structure */
218 if (cs_put(conn, buf, len) < 0)
220 fprintf(stderr, "cs_put: %s", cs_errmsg(cs_errno(conn)));
225 /* INIT SERVICE ------------------------------- */
227 static void send_initRequest()
229 Z_APDU *apdu = zget_APDU(out, Z_APDU_initRequest);
230 Z_InitRequest *req = apdu->u.initRequest;
232 ODR_MASK_SET(req->options, Z_Options_search);
233 ODR_MASK_SET(req->options, Z_Options_present);
234 ODR_MASK_SET(req->options, Z_Options_namedResultSets);
235 ODR_MASK_SET(req->options, Z_Options_triggerResourceCtrl);
236 ODR_MASK_SET(req->options, Z_Options_scan);
238 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_1);
239 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_2);
240 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_3);
242 *req->maximumRecordSize = 1024*1024;
244 req->idAuthentication = auth;
247 printf("Sent initrequest.\n");
250 static int process_initResponse(Z_InitResponse *res)
252 /* save session parameters for later use */
253 session_mem = odr_extract_mem(in);
257 printf("Connection rejected by target.\n");
259 printf("Connection accepted by target.\n");
260 if (res->implementationId)
261 printf("ID : %s\n", res->implementationId);
262 if (res->implementationName)
263 printf("Name : %s\n", res->implementationName);
264 if (res->implementationVersion)
265 printf("Version: %s\n", res->implementationVersion);
266 if (res->userInformationField)
268 printf("UserInformationfield:\n");
269 if (!z_External(print, (Z_External**)&res-> userInformationField,
272 odr_perror(print, "Printing userinfo\n");
275 if (res->userInformationField->which == Z_External_octet)
277 printf("Guessing visiblestring:\n");
278 printf("'%s'\n", res->userInformationField->u. octet_aligned->buf);
284 int cmd_open(char *arg)
287 char type[100], addr[100];
292 printf("Already connected.\n");
295 if (!*arg || sscanf(arg, "%[^:]:%s", type, addr) < 2)
297 fprintf(stderr, "Usage: open (osi|tcp) ':' [tsel '/']host[':'port]\n");
301 if (!strcmp(type, "osi"))
303 if (!(add = mosi_strtoaddr(addr)))
313 if (!strcmp(type, "tcp"))
315 if (!(add = tcpip_strtoaddr(addr)))
321 protocol = PROTO_Z3950;
325 fprintf(stderr, "Bad type: %s\n", type);
328 if (!(conn = cs_create(t, 1, protocol)))
333 printf("Connecting...");
335 if (cs_connect(conn, add) < 0)
347 int cmd_authentication(char *arg)
349 static Z_IdAuthentication au;
350 static char open[256];
354 printf("Auth field set to null\n");
359 au.which = Z_IdAuthentication_open;
365 /* SEARCH SERVICE ------------------------------ */
367 static void display_variant(Z_Variant *v, int level)
371 for (i = 0; i < v->num_triples; i++)
373 printf("%*sclass=%d,type=%d", level * 4, "", *v->triples[i]->class,
374 *v->triples[i]->type);
375 if (v->triples[i]->which == Z_Triple_internationalString)
376 printf(",value=%s\n", v->triples[i]->value.internationalString);
382 static void display_grs1(Z_GenericRecord *r, int level)
388 for (i = 0; i < r->num_elements; i++)
392 printf("%*s", level * 4, "");
396 printf("%d,", *t->tagType);
399 if (t->tagValue->which == Z_StringOrNumeric_numeric)
400 printf("%d) ", *t->tagValue->u.numeric);
402 printf("%s) ", t->tagValue->u.string);
403 if (t->content->which == Z_ElementData_subtree)
406 display_grs1(t->content->u.subtree, level+1);
408 else if (t->content->which == Z_ElementData_string)
409 printf("%s\n", t->content->u.string);
410 else if (t->content->which == Z_ElementData_numeric)
411 printf("%d\n", *t->content->u.numeric);
412 else if (t->content->which == Z_ElementData_oid)
414 int *ip = t->content->u.oid;
417 if ((oent = oid_getentbyoid(t->content->u.oid)))
418 printf("OID: %s\n", oent->desc);
422 while (ip && *ip >= 0)
423 printf(" %d", *(ip++));
427 else if (t->content->which == Z_ElementData_noDataRequested)
428 printf("[No data requested]\n");
429 else if (t->content->which == Z_ElementData_elementEmpty)
430 printf("[Element empty]\n");
431 else if (t->content->which == Z_ElementData_elementNotThere)
432 printf("[Element not there]\n");
435 if (t->appliedVariant)
436 display_variant(t->appliedVariant, level+1);
437 if (t->metaData && t->metaData->supportedVariants)
441 printf("%*s---- variant list\n", (level+1)*4, "");
442 for (c = 0; c < t->metaData->num_supportedVariants; c++)
444 printf("%*svariant #%d\n", (level+1)*4, "", c);
445 display_variant(t->metaData->supportedVariants[c], level + 2);
451 static void display_record(Z_DatabaseRecord *p)
453 Z_External *r = (Z_External*) p;
454 oident *ent = oid_getentbyoid(r->direct_reference);
457 * Tell the user what we got.
459 if (r->direct_reference)
461 printf("Record type: ");
463 printf("%s\n", ent->desc);
464 else if (!odr_oid(print, &r->direct_reference, 0))
466 odr_perror(print, "print oid");
471 /* Check if this is a known, ASN.1 type tucked away in an octet string */
472 if (ent && r->which == Z_External_octet)
474 Z_ext_typeent *type = z_ext_getentbyref(ent->value);
480 * Call the given decoder to process the record.
482 odr_setbuf(in, (char*)p->u.octet_aligned->buf,
483 p->u.octet_aligned->len, 0);
484 if (!(*type->fun)(in, &rr, 0))
486 odr_perror(in, "Decoding constructed record.");
487 fprintf(stderr, "[Near %d]\n", odr_offset(in));
488 fprintf(stderr, "Packet dump:\n---------\n");
489 odr_dumpBER(stderr, (char*)p->u.octet_aligned->buf,
490 p->u.octet_aligned->len);
491 fprintf(stderr, "---------\n");
495 * Note: we throw away the original, BER-encoded record here.
496 * Do something else with it if you want to keep it.
498 r->u.sutrs = rr; /* we don't actually check the type here. */
499 r->which = type->what;
502 if (ent->value == VAL_SOIF)
503 printf("%.*s", r->u.octet_aligned->len, r->u.octet_aligned->buf);
504 else if (r->which == Z_External_octet && p->u.octet_aligned->len)
506 const char *marc_buf = (char*)p->u.octet_aligned->buf;
507 marc_display (marc_buf, stdout);
509 fwrite (marc_buf, strlen (marc_buf), 1, marcdump);
511 else if (ent->value == VAL_SUTRS)
513 if (r->which != Z_External_sutrs)
515 printf("Expecting single SUTRS type for SUTRS.\n");
518 printf("%.*s", r->u.sutrs->len, r->u.sutrs->buf);
520 else if (ent->value == VAL_GRS1)
522 if (r->which != Z_External_grs1)
524 printf("Expecting single GRS type for GRS.\n");
527 display_grs1(r->u.grs1, 0);
531 printf("Unknown record representation.\n");
532 if (!z_External(print, &r, 0))
534 odr_perror(print, "Printing external");
540 static void display_diagrec(Z_DiagRec *p)
544 Z_DefaultDiagFormat *r;
549 printf("Diagnostic message from database:\n");
551 if (p->which != Z_DiagRec_defaultFormat)
553 printf("Diagnostic record not in default format.\n");
557 r = p->u.defaultFormat;
559 if (!(ent = oid_getentbyoid(r->diagnosticSetId)) ||
560 ent->oclass != CLASS_DIAGSET || ent->value != VAL_BIB1)
561 printf("Missing or unknown diagset\n");
562 printf(" [%d] %s", *r->condition, diagbib1_str(*r->condition));
563 if (r->addinfo && *r->addinfo)
564 printf(" -- '%s'\n", r->addinfo);
569 static void display_nameplusrecord(Z_NamePlusRecord *p)
572 printf("[%s]", p->databaseName);
573 if (p->which == Z_NamePlusRecord_surrogateDiagnostic)
574 display_diagrec(p->u.surrogateDiagnostic);
576 display_record(p->u.databaseRecord);
579 static void display_records(Z_Records *p)
583 if (p->which == Z_Records_NSD)
584 display_diagrec(p->u.nonSurrogateDiagnostic);
587 printf("Records: %d\n", p->u.databaseOrSurDiagnostics->num_records);
588 for (i = 0; i < p->u.databaseOrSurDiagnostics->num_records; i++)
589 display_nameplusrecord(p->u.databaseOrSurDiagnostics->records[i]);
593 static int send_searchRequest(char *arg)
595 Z_APDU *apdu = zget_APDU(out, Z_APDU_searchRequest);
596 Z_SearchRequest *req = apdu->u.searchRequest;
599 struct ccl_rpn_node *rpn;
604 Z_RPNQuery *RPNquery;
608 if (queryType == QueryType_CCL2RPN)
610 rpn = ccl_find_str(bibset, arg, &error, &pos);
613 printf("CCL ERROR: %s\n", ccl_err_msg(error));
619 if (!strcmp(arg, "@big")) /* strictly for troublemaking */
621 static unsigned char big[2100];
624 /* send a very big referenceid to test transport stack etc. */
625 memset(big, 'A', 2100);
626 bigo.len = bigo.size = 2100;
628 req->referenceId = &bigo;
633 sprintf(setstring, "%d", ++setnumber);
634 req->resultSetName = setstring;
636 *req->smallSetUpperBound = smallSetUpperBound;
637 *req->largeSetLowerBound = largeSetLowerBound;
638 *req->mediumSetPresentNumber = mediumSetPresentNumber;
639 if (smallSetUpperBound > 0 || (largeSetLowerBound > 1 &&
640 mediumSetPresentNumber > 0))
644 prefsyn.proto = protocol;
645 prefsyn.oclass = CLASS_RECSYN;
646 prefsyn.value = recordsyntax;
647 req->preferredRecordSyntax =
648 odr_oiddup(out, oid_getoidbyent(&prefsyn));
649 req->smallSetElementSetNames =
650 req->mediumSetElementSetNames = elementSetNames;
652 req->num_databaseNames = num_databaseNames;
653 req->databaseNames = databaseNames;
659 case QueryType_Prefix:
660 query.which = Z_Query_type_1;
661 RPNquery = p_query_rpn (out, protocol, arg);
664 printf("Prefix query error\n");
667 query.u.type_1 = RPNquery;
670 query.which = Z_Query_type_2;
671 query.u.type_2 = &ccl_query;
672 ccl_query.buf = (unsigned char*) arg;
673 ccl_query.len = strlen(arg);
676 case QueryType_CCL2RPN:
677 query.which = Z_Query_type_1;
678 assert((RPNquery = ccl_rpn_query(rpn)));
679 bib1.proto = protocol;
680 bib1.oclass = CLASS_ATTSET;
681 bib1.value = VAL_BIB1;
682 RPNquery->attributeSetId = oid_getoidbyent(&bib1);
683 query.u.type_1 = RPNquery;
687 printf ("Unsupported query type\n");
692 printf("Sent searchRequest.\n");
696 static int process_searchResponse(Z_SearchResponse *res)
698 if (*res->searchStatus)
699 printf("Search was a success.\n");
701 printf("Search was a bloomin' failure.\n");
702 printf("Number of hits: %d, setno %d\n",
703 *res->resultCount, setnumber);
704 printf("records returned: %d\n",
705 *res->numberOfRecordsReturned);
706 setno += *res->numberOfRecordsReturned;
708 display_records(res->records);
712 static int cmd_find(char *arg)
716 printf("Find what?\n");
721 printf("Not connected yet\n");
724 if (!send_searchRequest(arg))
729 static int cmd_ssub(char *arg)
731 if (!(smallSetUpperBound = atoi(arg)))
736 static int cmd_lslb(char *arg)
738 if (!(largeSetLowerBound = atoi(arg)))
743 static int cmd_mspn(char *arg)
745 if (!(mediumSetPresentNumber = atoi(arg)))
750 static int cmd_status(char *arg)
752 printf("smallSetUpperBound: %d\n", smallSetUpperBound);
753 printf("largeSetLowerBound: %d\n", largeSetLowerBound);
754 printf("mediumSetPresentNumber: %d\n", mediumSetPresentNumber);
758 static int cmd_base(char *arg)
765 printf("Usage: base <database> <database> ...\n");
768 for (i = 0; i<num_databaseNames; i++)
769 xfree (databaseNames[i]);
770 num_databaseNames = 0;
773 if (!(cp = strchr(arg, ' ')))
774 cp = arg + strlen(arg);
777 databaseNames[num_databaseNames] = xmalloc (1 + cp - arg);
778 memcpy (databaseNames[num_databaseNames], arg, cp - arg);
779 databaseNames[num_databaseNames++][cp - arg] = '\0';
787 static int cmd_setnames(char *arg)
791 printf("Set numbering enabled.\n");
796 printf("Set numbering disabled.\n");
802 /* PRESENT SERVICE ----------------------------- */
804 static int send_presentRequest(char *arg)
806 Z_APDU *apdu = zget_APDU(out, Z_APDU_presentRequest);
807 Z_PresentRequest *req = apdu->u.presentRequest;
808 Z_RecordComposition compo;
814 if ((p = strchr(arg, '+')))
824 sprintf(setstring, "%d", setnumber);
825 req->resultSetId = setstring;
832 static Z_Range range;
833 static Z_Range *rangep = ⦥
836 req->resultSetStartPoint = &setno;
837 req->numberOfRecordsRequested = &nos;
838 prefsyn.proto = protocol;
839 prefsyn.oclass = CLASS_RECSYN;
840 prefsyn.value = recordsyntax;
841 req->preferredRecordSyntax = oid_getoidbyent(&prefsyn);
844 req->recordComposition = &compo;
845 compo.which = Z_RecordComp_simple;
846 compo.u.simple = elementSetNames;
849 printf("Sent presentRequest (%d+%d).\n", setno, nos);
853 void process_close(Z_Close *req)
855 Z_APDU *apdu = zget_APDU(out, Z_APDU_close);
856 Z_Close *res = apdu->u.close;
858 static char *reasons[] =
863 "cost limit reached",
865 "security violation",
872 printf("Reason: %s, message: %s\n", reasons[*req->closeReason],
873 req->diagnosticInformation ? req->diagnosticInformation : "NULL");
876 printf("Goodbye.\n");
879 *res->closeReason = Z_Close_finished;
881 printf("Sent response.\n");
885 static int cmd_show(char *arg)
887 if (!send_presentRequest(arg))
892 int cmd_quit(char *arg)
894 printf("See you later, alligator.\n");
898 int cmd_cancel(char *arg)
900 Z_APDU *apdu = zget_APDU(out, Z_APDU_triggerResourceControlRequest);
901 Z_TriggerResourceControlRequest *req =
902 apdu->u.triggerResourceControlRequest;
907 printf("Session not initialized yet\n");
910 if (!ODR_MASK_GET(session->options, Z_Options_triggerResourceCtrl))
912 printf("Target doesn't support cancel (trigger resource ctrl)\n");
915 *req->requestedAction = Z_TriggerResourceCtrl_cancel;
916 req->resultSetWanted = &false;
919 printf("Sent cancel request\n");
923 int send_scanrequest(char *string, int pp, int num)
925 Z_APDU *apdu = zget_APDU(out, Z_APDU_scanRequest);
926 Z_ScanRequest *req = apdu->u.scanRequest;
928 req->num_databaseNames = num_databaseNames;
929 req->databaseNames = databaseNames;
930 req->termListAndStartPoint = p_query_scan(out, protocol,
931 &req->attributeSet, string);
932 req->numberOfTermsRequested = #
933 req->preferredPositionInResponse = &pp;
938 void display_term(Z_TermInfo *t)
940 if (t->term->which == Z_Term_general)
942 printf("%.*s (%d)\n", t->term->u.general->len, t->term->u.general->buf,
943 t->globalOccurrences ? *t->globalOccurrences : -1);
944 sprintf(last_scan, "%.*s", t->term->u.general->len,
945 t->term->u.general->buf);
948 printf("Term type not general.\n");
951 void process_scanResponse(Z_ScanResponse *res)
955 printf("SCAN: %d entries, position=%d\n", *res->numberOfEntriesReturned,
956 *res->positionOfTerm);
957 if (*res->scanStatus != Z_Scan_success)
958 printf("Scan returned code %d\n", *res->scanStatus);
961 if (res->entries->which == Z_ListEntries_entries)
963 Z_Entries *ent = res->entries->u.entries;
965 for (i = 0; i < ent->num_entries; i++)
966 if (ent->entries[i]->which == Z_Entry_termInfo)
968 printf("%c ", i + 1 == *res->positionOfTerm ? '*' : ' ');
969 display_term(ent->entries[i]->u.termInfo);
972 display_diagrec(ent->entries[i]->u.surrogateDiagnostic);
975 display_diagrec(res->entries->u.nonSurrogateDiagnostics->diagRecs[0]);
978 int cmd_scan(char *arg)
982 printf("Session not initialized yet\n");
985 if (!ODR_MASK_GET(session->options, Z_Options_scan))
987 printf("Target doesn't support scan\n");
992 if (send_scanrequest(arg, 5, 20) < 0)
996 if (send_scanrequest(last_scan, 1, 20) < 0)
1001 int cmd_format(char *arg)
1005 printf("Usage: format <recordsyntax>\n");
1008 if (!strcmp(arg, "sutrs"))
1010 printf("Preferred format is SUTRS.\n");
1011 recordsyntax = VAL_SUTRS;
1014 else if (!strcmp(arg, "usmarc"))
1016 printf("Preferred format is USMARC\n");
1017 recordsyntax = VAL_USMARC;
1020 else if (!strcmp(arg, "danmarc"))
1022 printf("Preferred format is DANMARC\n");
1023 recordsyntax = VAL_DANMARC;
1026 else if (!strcmp(arg, "ukmarc"))
1028 printf("Preferred format is UKMARC\n");
1029 recordsyntax = VAL_UKMARC;
1032 else if (!strcmp(arg, "grs1"))
1034 printf("Preferred format is GRS1\n");
1035 recordsyntax = VAL_GRS1;
1038 else if (!strcmp(arg, "soif"))
1040 printf("Preferred format is SOIF\n");
1041 recordsyntax = VAL_SOIF;
1044 else if (!strcmp(arg, "summary"))
1046 printf("Preferred format is Summary\n");
1047 recordsyntax = VAL_SUMMARY;
1050 else if (!strcmp(arg, "explain"))
1052 printf("Preferred format is Explain\n");
1053 recordsyntax = VAL_EXPLAIN;
1058 printf("Specify one of {sutrs,usmarc,danmarc,ukmarc,grs1,summary,explain}.\n");
1063 int cmd_elements(char *arg)
1065 static Z_ElementSetNames esn;
1066 static char what[100];
1070 printf("Usage: elements <esn>\n");
1074 esn.which = Z_ElementSetNames_generic;
1075 esn.u.generic = what;
1076 elementSetNames = &esn;
1080 int cmd_attributeset(char *arg)
1086 printf("Usage: attributeset <setname>\n");
1089 sscanf(arg, "%s", what);
1090 if (p_query_attset (what))
1092 printf("Unknown attribute set name\n");
1098 int cmd_querytype (char *arg)
1100 if (!strcmp (arg, "ccl"))
1101 queryType = QueryType_CCL;
1102 else if (!strcmp (arg, "prefix"))
1103 queryType = QueryType_Prefix;
1105 else if (!strcmp (arg, "ccl2rpn") || !strcmp (arg, "cclrpn"))
1106 queryType = QueryType_CCL2RPN;
1110 printf ("Querytype must be one of:\n");
1111 printf (" prefix - Prefix query\n");
1112 printf (" ccl - CCL query\n");
1114 printf (" ccl2rpn - CCL query converted to RPN\n");
1121 int cmd_close(char *arg)
1123 Z_APDU *apdu = zget_APDU(out, Z_APDU_close);
1124 Z_Close *req = apdu->u.close;
1126 *req->closeReason = Z_Close_finished;
1128 printf("Sent close request.\n");
1133 static void initialize(void)
1139 if (!(out = odr_createmem(ODR_ENCODE)) ||
1140 !(in = odr_createmem(ODR_DECODE)) ||
1141 !(print = odr_createmem(ODR_PRINT)))
1143 fprintf(stderr, "failed to allocate ODR streams\n");
1146 setvbuf(stdout, 0, _IONBF, 0);
1149 bibset = ccl_qual_mk ();
1150 inf = fopen ("default.bib", "r");
1153 ccl_qual_file (bibset, inf);
1159 static int client(int wait)
1163 int (*fun)(char *arg);
1166 {"open", cmd_open, "('tcp'|'osi')':'[<tsel>'/']<host>[':'<port>]"},
1167 {"quit", cmd_quit, ""},
1168 {"find", cmd_find, "<query>"},
1169 {"base", cmd_base, "<base-name>"},
1170 {"show", cmd_show, "<rec#>['+'<#recs>]"},
1171 {"scan", cmd_scan, "<term>"},
1172 {"authentication", cmd_authentication, "<acctstring>"},
1173 {"lslb", cmd_lslb, "<largeSetLowerBound>"},
1174 {"ssub", cmd_ssub, "<smallSetUpperBound>"},
1175 {"mspn", cmd_mspn, "<mediumSetPresentNumber>"},
1176 {"status", cmd_status, ""},
1177 {"setnames", cmd_setnames, ""},
1178 {"cancel", cmd_cancel, ""},
1179 {"format", cmd_format, "<recordsyntax>"},
1180 {"elements", cmd_elements, "<elementSetName>"},
1181 {"close", cmd_close, ""},
1182 {"attributeset", cmd_attributeset, "<attrset>"},
1183 {"querytype", cmd_querytype, "<type>"},
1187 int netbufferlen = 0;
1197 char line[1024], word[1024], arg[1024];
1203 FD_SET(cs_fileno(conn), &input);
1204 if ((res = select(20, &input, 0, 0, 0)) < 0)
1211 if (!wait && FD_ISSET(0, &input))
1216 /* quick & dirty way to get a command line. */
1219 if ((res = sscanf(line, "%s %[^;]", word, arg)) <= 0)
1221 strcpy(word, last_cmd);
1226 strcpy(last_cmd, word);
1227 for (i = 0; cmd[i].cmd; i++)
1228 if (!strncmp(cmd[i].cmd, word, strlen(word)))
1230 res = (*cmd[i].fun)(arg);
1233 if (!cmd[i].cmd) /* dump our help-screen */
1235 printf("Unknown command: %s.\n", word);
1236 printf("Currently recognized commands:\n");
1237 for (i = 0; cmd[i].cmd; i++)
1238 printf(" %s %s\n", cmd[i].cmd, cmd[i].ad);
1248 if (conn && FD_ISSET(cs_fileno(conn), &input))
1253 if ((res = cs_get(conn, &netbuffer, &netbufferlen)) < 0)
1260 printf("Target closed connection.\n");
1263 odr_reset(in); /* release APDU from last round */
1264 odr_setbuf(in, netbuffer, res, 0);
1265 if (!z_APDU(in, &apdu, 0))
1267 odr_perror(in, "Decoding incoming APDU");
1268 fprintf(stderr, "[Near %d]\n", odr_offset(in));
1269 fprintf(stderr, "Packet dump:\n---------\n");
1270 odr_dumpBER(stderr, netbuffer, res);
1271 fprintf(stderr, "---------\n");
1275 if (!z_APDU(print, &apdu, 0))
1277 odr_perror(print, "Failed to print incoming APDU");
1284 case Z_APDU_initResponse:
1285 process_initResponse(apdu->u.initResponse);
1287 case Z_APDU_searchResponse:
1288 process_searchResponse(apdu->u.searchResponse);
1290 case Z_APDU_scanResponse:
1291 process_scanResponse(apdu->u.scanResponse);
1293 case Z_APDU_presentResponse:
1294 printf("Received presentResponse.\n");
1296 *apdu->u.presentResponse->numberOfRecordsReturned;
1297 if (apdu->u.presentResponse->records)
1298 display_records(apdu->u.presentResponse->records);
1300 printf("No records.\n");
1303 printf("Target has closed the association.\n");
1304 process_close(apdu->u.close);
1307 printf("Received unknown APDU type (%d).\n",
1314 while (cs_more(conn));
1321 int main(int argc, char **argv)
1329 cmd_base("Default");
1331 while ((ret = options("m:", argv, argc, &arg)) != -2)
1340 if (!(marcdump = fopen (arg, "a")))
1347 fprintf (stderr, "Usage: %s [-m <marclog>] [<server-addr>]\n",
1354 return client (opened);