2 * Copyright (c) 1995-1998, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.59 1998-01-29 13:17:56 adam
10 * Revision 1.58 1998/01/07 13:51:45 adam
13 * Revision 1.57 1998/01/07 12:58:22 adam
14 * Using fgets instead of gets.
16 * Revision 1.56 1997/11/05 09:18:31 adam
17 * The client handles records with no associated syntax.
19 * Revision 1.55 1997/10/31 12:20:08 adam
20 * Improved memory debugging for xmalloc/nmem.c. References to NMEM
21 * instead of ODR in n ESPEC-1 handling in source d1_espec.c.
22 * Bug fix: missing fclose in data1_read_espec1.
24 * Revision 1.54 1997/10/27 13:52:46 adam
25 * Header yaz-util includes all YAZ utility header files.
27 * Revision 1.53 1997/09/29 13:18:59 adam
28 * Added function, oid_ent_to_oid, to replace the function
29 * oid_getoidbyent, which is not thread safe.
31 * Revision 1.52 1997/09/29 07:20:31 adam
32 * Client code uses nmem_init.
34 * Revision 1.51 1997/09/26 09:41:55 adam
35 * Updated client to handle multiple diagnostics.
37 * Revision 1.50 1997/09/17 12:10:29 adam
40 * Revision 1.49 1997/09/04 13:45:17 adam
41 * Added UNImarc to list of available syntaxes.
43 * Revision 1.48 1997/09/01 08:48:44 adam
44 * New windows NT/95 port using MSV5.0. Only a few changes made
45 * to avoid warnings. Sub project created: client.dsp.
47 * Revision 1.47 1997/07/01 13:49:56 adam
48 * Take care of case when invalid target is specified on command line.
50 * Revision 1.46 1997/06/23 10:30:18 adam
51 * Added call to ccl_rpn_delete in search. Added ODR stream "out"
52 * as parameter to ccl_rpn_query to release RPN query.
54 * Revision 1.45 1997/05/14 06:53:29 adam
57 * Revision 1.44 1997/05/05 11:20:35 adam
58 * Client uses "options" utility and marc dump filename may be specified
59 * as an option (-m <file>).
61 * Revision 1.43 1996/11/08 11:03:26 adam
62 * Client accepts multiple database names.
64 * Revision 1.42 1996/10/08 10:44:57 quinn
67 * Revision 1.41 1996/10/07 15:29:03 quinn
70 * Revision 1.40 1996/08/29 14:19:34 quinn
71 * Fixed conflict (CVS)
73 * Revision 1.39 1996/08/27 10:43:22 quinn
74 * Made select() optional
76 * Revision 1.38 1996/08/12 14:09:11 adam
77 * Default prefix query attribute set defined by using p_query_attset.
79 * Revision 1.37 1996/07/06 19:58:29 quinn
80 * System headerfiles gathered in yconfig
82 * Revision 1.36 1996/06/10 08:53:47 quinn
85 * Revision 1.35 1996/06/03 09:45:50 quinn
86 * Added display of OIDs in the GRS routine.
88 * Revision 1.34 1996/05/09 07:26:49 quinn
89 * *** empty log message ***
91 * Revision 1.33 1996/05/09 07:25:22 quinn
94 * Revision 1.32 1996/03/15 11:05:33 adam
95 * The user can set the preferred query type (prefix, ccl, ..) with the
98 * Revision 1.31 1996/02/20 12:51:54 quinn
99 * Fixed problems with EXTERNAL.
101 * Revision 1.30 1996/02/12 18:18:09 quinn
104 * Revision 1.29 1996/01/02 08:57:25 quinn
105 * Changed enums in the ASN.1 .h files to #defines. Changed oident.class to oclass
107 * Revision 1.28 1995/12/14 11:09:31 quinn
108 * Added Explain record syntax to the format command.
110 * Revision 1.27 1995/12/12 16:37:02 quinn
111 * Added destroy element to data1_node.
113 * Revision 1.26 1995/12/12 14:11:00 quinn
116 * Revision 1.25 1995/11/13 09:27:22 quinn
117 * Fiddling with the variant stuff.
119 * Revision 1.24 1995/10/30 12:41:13 quinn
120 * Added hostname lookup for server.
122 * Revision 1.23 1995/10/18 16:12:30 quinn
123 * Better diagnostics.
125 * Revision 1.22 1995/10/11 14:49:12 quinn
128 * Revision 1.21 1995/09/29 17:01:47 quinn
131 * Revision 1.20 1995/08/29 14:24:13 quinn
132 * Added second half of close-handshake
134 * Revision 1.19 1995/08/29 11:17:28 quinn
135 * Added code to receive close
137 * Revision 1.18 1995/08/28 12:21:27 quinn
138 * Client can now ask for simple element set names.
140 * Revision 1.17 1995/08/17 12:45:02 quinn
141 * Fixed minor problems with GRS-1. Added support in c&s.
143 * Revision 1.16 1995/08/15 12:00:04 quinn
146 * Revision 1.15 1995/06/22 09:28:03 quinn
147 * Fixed bug in SUTRS processing.
149 * Revision 1.14 1995/06/19 12:37:41 quinn
152 * Revision 1.13 1995/06/16 10:29:11 quinn
153 * *** empty log message ***
155 * Revision 1.12 1995/06/15 07:44:57 quinn
158 * Revision 1.11 1995/06/14 15:26:40 quinn
159 * *** empty log message ***
161 * Revision 1.10 1995/06/06 14:56:58 quinn
162 * Better diagnostics.
164 * Revision 1.9 1995/06/06 08:15:19 quinn
167 * Revision 1.8 1995/06/05 10:52:22 quinn
170 * Revision 1.7 1995/06/02 09:50:09 quinn
173 * Revision 1.6 1995/05/31 08:29:21 quinn
174 * Nothing significant.
176 * Revision 1.5 1995/05/29 08:10:47 quinn
177 * Moved oid.c to util.
179 * Revision 1.4 1995/05/22 15:30:13 adam
180 * Client uses prefix query notation.
182 * Revision 1.3 1995/05/22 15:06:53 quinn
183 * *** empty log message ***
185 * Revision 1.2 1995/05/22 14:56:40 quinn
186 * *** empty log message ***
188 * Revision 1.1 1995/05/22 11:30:31 quinn
189 * Added prettier client.
195 * This is the obligatory little toy client, whose primary purpose is
196 * to illustrate the use of the YAZ service-level API.
204 #include <yaz-util.h>
206 #include <comstack.h>
213 #include <marcdisp.h>
214 #include <diagbib1.h>
222 #define C_PROMPT "Z> "
224 static ODR out, in, print; /* encoding and decoding streams */
225 static COMSTACK conn = 0; /* our z-association */
226 static Z_IdAuthentication *auth = 0; /* our current auth definition */
227 static char *databaseNames[128];
228 static int num_databaseNames = 0;
229 static int setnumber = 0; /* current result set number */
230 static int smallSetUpperBound = 0;
231 static int largeSetLowerBound = 1;
232 static int mediumSetPresentNumber = 0;
233 static Z_ElementSetNames *elementSetNames = 0;
234 static int setno = 1; /* current set offset */
235 static int protocol = PROTO_Z3950; /* current app protocol */
236 static int recordsyntax = VAL_USMARC;
237 static int sent_close = 0;
238 static ODR_MEM session_mem; /* memory handle for init-response */
239 static Z_InitResponse *session = 0; /* session parameters */
240 static char last_scan[512] = "0";
241 static char last_cmd[100] = "?";
242 static FILE *marcdump = 0;
250 static QueryType queryType = QueryType_Prefix;
253 static CCL_bibset bibset; /* CCL bibset handle */
256 static void send_apdu(Z_APDU *a)
261 if (!z_APDU(out, &a, 0))
263 odr_perror(out, "Encoding APDU");
266 buf = odr_getbuf(out, &len, 0);
267 odr_reset(out); /* release the APDU structure */
268 if (cs_put(conn, buf, len) < 0)
270 fprintf(stderr, "cs_put: %s", cs_errmsg(cs_errno(conn)));
275 /* INIT SERVICE ------------------------------- */
277 static void send_initRequest()
279 Z_APDU *apdu = zget_APDU(out, Z_APDU_initRequest);
280 Z_InitRequest *req = apdu->u.initRequest;
282 ODR_MASK_SET(req->options, Z_Options_search);
283 ODR_MASK_SET(req->options, Z_Options_present);
284 ODR_MASK_SET(req->options, Z_Options_namedResultSets);
285 ODR_MASK_SET(req->options, Z_Options_triggerResourceCtrl);
286 ODR_MASK_SET(req->options, Z_Options_scan);
287 ODR_MASK_SET(req->options, Z_Options_sort);
289 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_1);
290 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_2);
291 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_3);
293 *req->maximumRecordSize = 1024*1024;
295 req->idAuthentication = auth;
298 printf("Sent initrequest.\n");
301 static int process_initResponse(Z_InitResponse *res)
303 /* save session parameters for later use */
304 session_mem = odr_extract_mem(in);
308 printf("Connection rejected by target.\n");
310 printf("Connection accepted by target.\n");
311 if (res->implementationId)
312 printf("ID : %s\n", res->implementationId);
313 if (res->implementationName)
314 printf("Name : %s\n", res->implementationName);
315 if (res->implementationVersion)
316 printf("Version: %s\n", res->implementationVersion);
317 if (res->userInformationField)
319 printf("UserInformationfield:\n");
320 if (!z_External(print, (Z_External**)&res-> userInformationField,
323 odr_perror(print, "Printing userinfo\n");
326 if (res->userInformationField->which == Z_External_octet)
328 printf("Guessing visiblestring:\n");
329 printf("'%s'\n", res->userInformationField->u. octet_aligned->buf);
335 int cmd_open(char *arg)
338 char type[100], addr[100];
343 printf("Already connected.\n");
346 if (!*arg || sscanf(arg, "%[^:]:%s", type, addr) < 2)
348 fprintf(stderr, "Usage: open (osi|tcp) ':' [tsel '/']host[':'port]\n");
351 if (!strcmp(type, "tcp"))
354 protocol = PROTO_Z3950;
358 if (!strcmp(type, "osi"))
366 fprintf(stderr, "Bad type: %s\n", type);
369 if (!(conn = cs_create(t, 1, protocol)))
374 if (!(add = cs_straddr(conn, addr)))
379 printf("Connecting...");
381 if (cs_connect(conn, add) < 0)
393 int cmd_authentication(char *arg)
395 static Z_IdAuthentication au;
396 static char open[256];
400 printf("Auth field set to null\n");
405 au.which = Z_IdAuthentication_open;
411 /* SEARCH SERVICE ------------------------------ */
413 static void display_variant(Z_Variant *v, int level)
417 for (i = 0; i < v->num_triples; i++)
419 printf("%*sclass=%d,type=%d", level * 4, "", *v->triples[i]->zclass,
420 *v->triples[i]->type);
421 if (v->triples[i]->which == Z_Triple_internationalString)
422 printf(",value=%s\n", v->triples[i]->value.internationalString);
428 static void display_grs1(Z_GenericRecord *r, int level)
434 for (i = 0; i < r->num_elements; i++)
438 printf("%*s", level * 4, "");
442 printf("%d,", *t->tagType);
445 if (t->tagValue->which == Z_StringOrNumeric_numeric)
446 printf("%d) ", *t->tagValue->u.numeric);
448 printf("%s) ", t->tagValue->u.string);
449 if (t->content->which == Z_ElementData_subtree)
452 display_grs1(t->content->u.subtree, level+1);
454 else if (t->content->which == Z_ElementData_string)
455 printf("%s\n", t->content->u.string);
456 else if (t->content->which == Z_ElementData_numeric)
457 printf("%d\n", *t->content->u.numeric);
458 else if (t->content->which == Z_ElementData_oid)
460 int *ip = t->content->u.oid;
463 if ((oent = oid_getentbyoid(t->content->u.oid)))
464 printf("OID: %s\n", oent->desc);
468 while (ip && *ip >= 0)
469 printf(" %d", *(ip++));
473 else if (t->content->which == Z_ElementData_noDataRequested)
474 printf("[No data requested]\n");
475 else if (t->content->which == Z_ElementData_elementEmpty)
476 printf("[Element empty]\n");
477 else if (t->content->which == Z_ElementData_elementNotThere)
478 printf("[Element not there]\n");
481 if (t->appliedVariant)
482 display_variant(t->appliedVariant, level+1);
483 if (t->metaData && t->metaData->supportedVariants)
487 printf("%*s---- variant list\n", (level+1)*4, "");
488 for (c = 0; c < t->metaData->num_supportedVariants; c++)
490 printf("%*svariant #%d\n", (level+1)*4, "", c);
491 display_variant(t->metaData->supportedVariants[c], level + 2);
497 static void display_record(Z_DatabaseRecord *p)
499 Z_External *r = (Z_External*) p;
500 oident *ent = oid_getentbyoid(r->direct_reference);
503 * Tell the user what we got.
505 if (r->direct_reference)
507 printf("Record type: ");
509 printf("%s\n", ent->desc);
510 else if (!odr_oid(print, &r->direct_reference, 0))
512 odr_perror(print, "print oid");
516 /* Check if this is a known, ASN.1 type tucked away in an octet string */
517 if (ent && r->which == Z_External_octet)
519 Z_ext_typeent *type = z_ext_getentbyref(ent->value);
525 * Call the given decoder to process the record.
527 odr_setbuf(in, (char*)p->u.octet_aligned->buf,
528 p->u.octet_aligned->len, 0);
529 if (!(*type->fun)(in, &rr, 0))
531 odr_perror(in, "Decoding constructed record.");
532 fprintf(stderr, "[Near %d]\n", odr_offset(in));
533 fprintf(stderr, "Packet dump:\n---------\n");
534 odr_dumpBER(stderr, (char*)p->u.octet_aligned->buf,
535 p->u.octet_aligned->len);
536 fprintf(stderr, "---------\n");
540 * Note: we throw away the original, BER-encoded record here.
541 * Do something else with it if you want to keep it.
543 r->u.sutrs = rr; /* we don't actually check the type here. */
544 r->which = type->what;
547 if (ent && ent->value == VAL_SOIF)
548 printf("%.*s", r->u.octet_aligned->len, r->u.octet_aligned->buf);
549 else if (r->which == Z_External_octet && p->u.octet_aligned->len)
551 const char *marc_buf = (char*)p->u.octet_aligned->buf;
552 marc_display (marc_buf, NULL);
554 fwrite (marc_buf, strlen (marc_buf), 1, marcdump);
556 else if (ent && ent->value == VAL_SUTRS)
558 if (r->which != Z_External_sutrs)
560 printf("Expecting single SUTRS type for SUTRS.\n");
563 printf("%.*s", r->u.sutrs->len, r->u.sutrs->buf);
565 else if (ent && ent->value == VAL_GRS1)
567 if (r->which != Z_External_grs1)
569 printf("Expecting single GRS type for GRS.\n");
572 display_grs1(r->u.grs1, 0);
576 printf("Unknown record representation.\n");
577 if (!z_External(print, &r, 0))
579 odr_perror(print, "Printing external");
586 static void display_diagrecs(Z_DiagRec **pp, int num)
590 Z_DefaultDiagFormat *r;
592 printf("Diagnostic message(s) from database:\n");
593 for (i = 0; i<num; i++)
595 Z_DiagRec *p = pp[i];
596 if (p->which != Z_DiagRec_defaultFormat)
598 printf("Diagnostic record not in default format.\n");
602 r = p->u.defaultFormat;
603 if (!(ent = oid_getentbyoid(r->diagnosticSetId)) ||
604 ent->oclass != CLASS_DIAGSET || ent->value != VAL_BIB1)
605 printf("Missing or unknown diagset\n");
606 printf(" [%d] %s", *r->condition, diagbib1_str(*r->condition));
607 if (r->addinfo && *r->addinfo)
608 printf(" -- '%s'\n", r->addinfo);
615 static void display_nameplusrecord(Z_NamePlusRecord *p)
618 printf("[%s]", p->databaseName);
619 if (p->which == Z_NamePlusRecord_surrogateDiagnostic)
620 display_diagrecs(&p->u.surrogateDiagnostic, 1);
622 display_record(p->u.databaseRecord);
625 static void display_records(Z_Records *p)
629 if (p->which == Z_Records_NSD)
630 display_diagrecs (&p->u.nonSurrogateDiagnostic, 1);
631 else if (p->which == Z_Records_multipleNSD)
632 display_diagrecs (p->u.multipleNonSurDiagnostics->diagRecs,
633 p->u.multipleNonSurDiagnostics->num_diagRecs);
636 printf("Records: %d\n", p->u.databaseOrSurDiagnostics->num_records);
637 for (i = 0; i < p->u.databaseOrSurDiagnostics->num_records; i++)
638 display_nameplusrecord(p->u.databaseOrSurDiagnostics->records[i]);
642 static int send_searchRequest(char *arg)
644 Z_APDU *apdu = zget_APDU(out, Z_APDU_searchRequest);
645 Z_SearchRequest *req = apdu->u.searchRequest;
649 struct ccl_rpn_node *rpn;
654 Z_RPNQuery *RPNquery;
658 if (queryType == QueryType_CCL2RPN)
660 rpn = ccl_find_str(bibset, arg, &error, &pos);
663 printf("CCL ERROR: %s\n", ccl_err_msg(error));
669 if (!strcmp(arg, "@big")) /* strictly for troublemaking */
671 static unsigned char big[2100];
674 /* send a very big referenceid to test transport stack etc. */
675 memset(big, 'A', 2100);
676 bigo.len = bigo.size = 2100;
678 req->referenceId = &bigo;
683 sprintf(setstring, "%d", ++setnumber);
684 req->resultSetName = setstring;
686 *req->smallSetUpperBound = smallSetUpperBound;
687 *req->largeSetLowerBound = largeSetLowerBound;
688 *req->mediumSetPresentNumber = mediumSetPresentNumber;
689 if (smallSetUpperBound > 0 || (largeSetLowerBound > 1 &&
690 mediumSetPresentNumber > 0))
694 prefsyn.proto = protocol;
695 prefsyn.oclass = CLASS_RECSYN;
696 prefsyn.value = recordsyntax;
697 req->preferredRecordSyntax =
698 odr_oiddup(out, oid_ent_to_oid(&prefsyn, oid));
699 req->smallSetElementSetNames =
700 req->mediumSetElementSetNames = elementSetNames;
702 req->num_databaseNames = num_databaseNames;
703 req->databaseNames = databaseNames;
709 case QueryType_Prefix:
710 query.which = Z_Query_type_1;
711 RPNquery = p_query_rpn (out, protocol, arg);
714 printf("Prefix query error\n");
717 query.u.type_1 = RPNquery;
720 query.which = Z_Query_type_2;
721 query.u.type_2 = &ccl_query;
722 ccl_query.buf = (unsigned char*) arg;
723 ccl_query.len = strlen(arg);
726 case QueryType_CCL2RPN:
727 query.which = Z_Query_type_1;
728 assert((RPNquery = ccl_rpn_query(out, rpn)));
729 bib1.proto = protocol;
730 bib1.oclass = CLASS_ATTSET;
731 bib1.value = VAL_BIB1;
732 RPNquery->attributeSetId = oid_ent_to_oid(&bib1, oid);
733 query.u.type_1 = RPNquery;
734 ccl_rpn_delete (rpn);
738 printf ("Unsupported query type\n");
743 printf("Sent searchRequest.\n");
747 static int process_searchResponse(Z_SearchResponse *res)
749 if (*res->searchStatus)
750 printf("Search was a success.\n");
752 printf("Search was a bloomin' failure.\n");
753 printf("Number of hits: %d, setno %d\n",
754 *res->resultCount, setnumber);
755 printf("records returned: %d\n",
756 *res->numberOfRecordsReturned);
757 setno += *res->numberOfRecordsReturned;
759 display_records(res->records);
763 static int cmd_find(char *arg)
767 printf("Find what?\n");
772 printf("Not connected yet\n");
775 if (!send_searchRequest(arg))
780 static int cmd_ssub(char *arg)
782 if (!(smallSetUpperBound = atoi(arg)))
787 static int cmd_lslb(char *arg)
789 if (!(largeSetLowerBound = atoi(arg)))
794 static int cmd_mspn(char *arg)
796 if (!(mediumSetPresentNumber = atoi(arg)))
801 static int cmd_status(char *arg)
803 printf("smallSetUpperBound: %d\n", smallSetUpperBound);
804 printf("largeSetLowerBound: %d\n", largeSetLowerBound);
805 printf("mediumSetPresentNumber: %d\n", mediumSetPresentNumber);
809 static int cmd_base(char *arg)
816 printf("Usage: base <database> <database> ...\n");
819 for (i = 0; i<num_databaseNames; i++)
820 xfree (databaseNames[i]);
821 num_databaseNames = 0;
824 if (!(cp = strchr(arg, ' ')))
825 cp = arg + strlen(arg);
828 databaseNames[num_databaseNames] = xmalloc (1 + cp - arg);
829 memcpy (databaseNames[num_databaseNames], arg, cp - arg);
830 databaseNames[num_databaseNames++][cp - arg] = '\0';
838 static int cmd_setnames(char *arg)
842 printf("Set numbering enabled.\n");
847 printf("Set numbering disabled.\n");
853 /* PRESENT SERVICE ----------------------------- */
855 static int send_presentRequest(char *arg)
857 Z_APDU *apdu = zget_APDU(out, Z_APDU_presentRequest);
858 Z_PresentRequest *req = apdu->u.presentRequest;
859 Z_RecordComposition compo;
866 if ((p = strchr(arg, '+')))
873 if (p && (p=strchr(p+1, '+')))
875 strcpy (setstring, p+1);
876 req->resultSetId = setstring;
878 else if (setnumber >= 0)
880 sprintf(setstring, "%d", setnumber);
881 req->resultSetId = setstring;
886 static Z_Range range;
887 static Z_Range *rangep = ⦥
890 req->resultSetStartPoint = &setno;
891 req->numberOfRecordsRequested = &nos;
892 prefsyn.proto = protocol;
893 prefsyn.oclass = CLASS_RECSYN;
894 prefsyn.value = recordsyntax;
895 req->preferredRecordSyntax = oid_ent_to_oid(&prefsyn, oid);
899 req->recordComposition = &compo;
900 compo.which = Z_RecordComp_simple;
901 compo.u.simple = elementSetNames;
904 printf("Sent presentRequest (%d+%d).\n", setno, nos);
908 void process_close(Z_Close *req)
910 Z_APDU *apdu = zget_APDU(out, Z_APDU_close);
911 Z_Close *res = apdu->u.close;
913 static char *reasons[] =
918 "cost limit reached",
920 "security violation",
927 printf("Reason: %s, message: %s\n", reasons[*req->closeReason],
928 req->diagnosticInformation ? req->diagnosticInformation : "NULL");
931 printf("Goodbye.\n");
934 *res->closeReason = Z_Close_finished;
936 printf("Sent response.\n");
940 static int cmd_show(char *arg)
942 if (!send_presentRequest(arg))
947 int cmd_quit(char *arg)
949 printf("See you later, alligator.\n");
954 int cmd_cancel(char *arg)
956 Z_APDU *apdu = zget_APDU(out, Z_APDU_triggerResourceControlRequest);
957 Z_TriggerResourceControlRequest *req =
958 apdu->u.triggerResourceControlRequest;
963 printf("Session not initialized yet\n");
966 if (!ODR_MASK_GET(session->options, Z_Options_triggerResourceCtrl))
968 printf("Target doesn't support cancel (trigger resource ctrl)\n");
971 *req->requestedAction = Z_TriggerResourceCtrl_cancel;
972 req->resultSetWanted = &rfalse;
975 printf("Sent cancel request\n");
979 int send_scanrequest(char *string, int pp, int num)
981 Z_APDU *apdu = zget_APDU(out, Z_APDU_scanRequest);
982 Z_ScanRequest *req = apdu->u.scanRequest;
984 req->num_databaseNames = num_databaseNames;
985 req->databaseNames = databaseNames;
986 req->termListAndStartPoint = p_query_scan(out, protocol,
987 &req->attributeSet, string);
988 req->numberOfTermsRequested = #
989 req->preferredPositionInResponse = &pp;
994 int send_sortrequest(char *arg, int newset)
996 Z_APDU *apdu = zget_APDU(out, Z_APDU_sortRequest);
997 Z_SortRequest *req = apdu->u.sortRequest;
998 Z_SortKeySpecList *sksl = odr_malloc (out, sizeof(*sksl));
1000 char sort_string[32], sort_flags[32];
1006 sprintf (setstring, "%d", setnumber);
1008 sprintf (setstring, "default");
1010 req->inputResultSetNames =
1011 odr_malloc (out, sizeof(*req->inputResultSetNames));
1012 req->inputResultSetNames->num_strings = 1;
1013 req->inputResultSetNames->strings =
1014 odr_malloc (out, sizeof(*req->inputResultSetNames->strings));
1015 req->inputResultSetNames->strings[0] =
1016 odr_malloc (out, strlen(setstring)+1);
1017 strcpy (req->inputResultSetNames->strings[0], setstring);
1019 if (newset && setnumber >= 0)
1020 sprintf (setstring, "%d", ++setnumber);
1022 req->sortedResultSetName = odr_malloc (out, strlen(setstring)+1);
1023 strcpy (req->sortedResultSetName, setstring);
1025 req->sortSequence = sksl;
1026 sksl->num_specs = 0;
1027 sksl->specs = odr_malloc (out, sizeof(sksl->specs) * 20);
1029 bib1.proto = protocol;
1030 bib1.oclass = CLASS_ATTSET;
1031 bib1.value = VAL_BIB1;
1032 while ((sscanf (arg, "%31s %31s%n", sort_string, sort_flags, &off)) == 2)
1035 char *sort_string_sep;
1036 Z_SortKeySpec *sks = odr_malloc (out, sizeof(*sks));
1037 Z_SortKey *sk = odr_malloc (out, sizeof(*sk));
1040 sksl->specs[sksl->num_specs++] = sks;
1041 sks->sortElement = odr_malloc (out, sizeof(*sks->sortElement));
1042 sks->sortElement->which = Z_SortElement_generic;
1043 sks->sortElement->u.generic = sk;
1045 if ((sort_string_sep = strchr (sort_string, '=')))
1047 Z_AttributeElement *el = odr_malloc (out, sizeof(*el));
1048 sk->which = Z_SortKey_sortAttributes;
1049 sk->u.sortAttributes =
1050 odr_malloc (out, sizeof(*sk->u.sortAttributes));
1051 sk->u.sortAttributes->id = oid_ent_to_oid(&bib1, oid);
1052 sk->u.sortAttributes->list =
1053 odr_malloc (out, sizeof(*sk->u.sortAttributes->list));
1054 sk->u.sortAttributes->list->num_attributes = 1;
1055 sk->u.sortAttributes->list->attributes =
1057 sizeof(*sk->u.sortAttributes->list->attributes));
1058 sk->u.sortAttributes->list->attributes[0] = el;
1059 el->attributeSet = 0;
1060 el->attributeType = odr_malloc (out, sizeof(*el->attributeType));
1061 *el->attributeType = atoi (sort_string);
1062 el->which = Z_AttributeValue_numeric;
1063 el->value.numeric = odr_malloc (out, sizeof(*el->value.numeric));
1064 *el->value.numeric = atoi (sort_string_sep + 1);
1068 sk->which = Z_SortKey_sortField;
1069 sk->u.sortField = odr_malloc (out, strlen(sort_string)+1);
1070 strcpy (sk->u.sortField, sort_string);
1072 sks->sortRelation = odr_malloc (out, sizeof(*sks->sortRelation));
1073 *sks->sortRelation = Z_SortRelation_ascending;
1074 sks->caseSensitivity = odr_malloc (out, sizeof(*sks->caseSensitivity));
1075 *sks->caseSensitivity = Z_SortCase_caseSensitive;
1077 sks->missingValueAction = NULL;
1079 for (i = 0; sort_flags[i]; i++)
1081 switch (sort_flags[i])
1086 *sks->sortRelation = Z_SortRelation_ascending;
1091 *sks->sortRelation = Z_SortRelation_descending;
1095 *sks->caseSensitivity = Z_SortCase_caseInsensitive;
1099 *sks->caseSensitivity = Z_SortCase_caseSensitive;
1104 if (!sksl->num_specs)
1110 void display_term(Z_TermInfo *t)
1112 if (t->term->which == Z_Term_general)
1114 printf("%.*s (%d)\n", t->term->u.general->len, t->term->u.general->buf,
1115 t->globalOccurrences ? *t->globalOccurrences : -1);
1116 sprintf(last_scan, "%.*s", t->term->u.general->len,
1117 t->term->u.general->buf);
1120 printf("Term type not general.\n");
1123 void process_scanResponse(Z_ScanResponse *res)
1127 printf("SCAN: %d entries, position=%d\n", *res->numberOfEntriesReturned,
1128 *res->positionOfTerm);
1129 if (*res->scanStatus != Z_Scan_success)
1130 printf("Scan returned code %d\n", *res->scanStatus);
1133 if (res->entries->which == Z_ListEntries_entries)
1135 Z_Entries *ent = res->entries->u.entries;
1137 for (i = 0; i < ent->num_entries; i++)
1138 if (ent->entries[i]->which == Z_Entry_termInfo)
1140 printf("%c ", i + 1 == *res->positionOfTerm ? '*' : ' ');
1141 display_term(ent->entries[i]->u.termInfo);
1144 display_diagrecs(&ent->entries[i]->u.surrogateDiagnostic, 1);
1147 display_diagrecs(&res->entries->
1148 u.nonSurrogateDiagnostics->diagRecs[0], 1);
1151 void process_sortResponse(Z_SortResponse *res)
1153 printf("Sort: status=");
1154 switch (*res->sortStatus)
1156 case Z_SortStatus_success:
1157 printf ("success"); break;
1158 case Z_SortStatus_partial_1:
1159 printf ("partial"); break;
1160 case Z_SortStatus_failure:
1161 printf ("failure"); break;
1163 printf ("unknown (%d)", *res->sortStatus);
1166 if (res->diagnostics)
1167 display_diagrecs(res->diagnostics->diagRecs,
1168 res->diagnostics->num_diagRecs);
1171 int cmd_sort_generic(char *arg, int newset)
1175 printf("Session not initialized yet\n");
1178 if (!ODR_MASK_GET(session->options, Z_Options_sort))
1180 printf("Target doesn't support sort\n");
1185 if (send_sortrequest(arg, newset) < 0)
1192 int cmd_sort(char *arg)
1194 return cmd_sort_generic (arg, 0);
1197 int cmd_sort_newset (char *arg)
1199 return cmd_sort_generic (arg, 1);
1202 int cmd_scan(char *arg)
1206 printf("Session not initialized yet\n");
1209 if (!ODR_MASK_GET(session->options, Z_Options_scan))
1211 printf("Target doesn't support scan\n");
1216 if (send_scanrequest(arg, 5, 20) < 0)
1220 if (send_scanrequest(last_scan, 1, 20) < 0)
1225 int cmd_format(char *arg)
1229 printf("Usage: format <recordsyntax>\n");
1232 recordsyntax = oid_getvalbyname (arg);
1233 if (recordsyntax == VAL_NONE)
1235 printf ("unknown record syntax\n");
1241 int cmd_elements(char *arg)
1243 static Z_ElementSetNames esn;
1244 static char what[100];
1248 printf("Usage: elements <esn>\n");
1252 esn.which = Z_ElementSetNames_generic;
1253 esn.u.generic = what;
1254 elementSetNames = &esn;
1258 int cmd_attributeset(char *arg)
1264 printf("Usage: attributeset <setname>\n");
1267 sscanf(arg, "%s", what);
1268 if (p_query_attset (what))
1270 printf("Unknown attribute set name\n");
1276 int cmd_querytype (char *arg)
1278 if (!strcmp (arg, "ccl"))
1279 queryType = QueryType_CCL;
1280 else if (!strcmp (arg, "prefix"))
1281 queryType = QueryType_Prefix;
1283 else if (!strcmp (arg, "ccl2rpn") || !strcmp (arg, "cclrpn"))
1284 queryType = QueryType_CCL2RPN;
1288 printf ("Querytype must be one of:\n");
1289 printf (" prefix - Prefix query\n");
1290 printf (" ccl - CCL query\n");
1292 printf (" ccl2rpn - CCL query converted to RPN\n");
1299 int cmd_close(char *arg)
1301 Z_APDU *apdu = zget_APDU(out, Z_APDU_close);
1302 Z_Close *req = apdu->u.close;
1304 *req->closeReason = Z_Close_finished;
1306 printf("Sent close request.\n");
1311 static void initialize(void)
1317 if (!(out = odr_createmem(ODR_ENCODE)) ||
1318 !(in = odr_createmem(ODR_DECODE)) ||
1319 !(print = odr_createmem(ODR_PRINT)))
1321 fprintf(stderr, "failed to allocate ODR streams\n");
1324 setvbuf(stdout, 0, _IONBF, 0);
1327 bibset = ccl_qual_mk ();
1328 inf = fopen ("default.bib", "r");
1331 ccl_qual_file (bibset, inf);
1337 static int client(int wait)
1341 int (*fun)(char *arg);
1344 {"open", cmd_open, "('tcp'|'osi')':'[<tsel>'/']<host>[':'<port>]"},
1345 {"quit", cmd_quit, ""},
1346 {"find", cmd_find, "<query>"},
1347 {"base", cmd_base, "<base-name>"},
1348 {"show", cmd_show, "<rec#>['+'<#recs>['+'<setname>]]"},
1349 {"scan", cmd_scan, "<term>"},
1350 {"sort", cmd_sort, "<sortkey> <flag> <sortkey> <flag> ..."},
1351 {"sort+", cmd_sort_newset, "<sortkey> <flag> <sortkey> <flag> ..."},
1352 {"authentication", cmd_authentication, "<acctstring>"},
1353 {"lslb", cmd_lslb, "<largeSetLowerBound>"},
1354 {"ssub", cmd_ssub, "<smallSetUpperBound>"},
1355 {"mspn", cmd_mspn, "<mediumSetPresentNumber>"},
1356 {"status", cmd_status, ""},
1357 {"setnames", cmd_setnames, ""},
1358 {"cancel", cmd_cancel, ""},
1359 {"format", cmd_format, "<recordsyntax>"},
1360 {"elements", cmd_elements, "<elementSetName>"},
1361 {"close", cmd_close, ""},
1362 {"attributeset", cmd_attributeset, "<attrset>"},
1363 {"querytype", cmd_querytype, "<type>"},
1367 int netbufferlen = 0;
1377 char line[1024], word[1024], arg[1024];
1383 FD_SET(cs_fileno(conn), &input);
1384 if ((res = select(20, &input, 0, 0, 0)) < 0)
1391 if (!wait && FD_ISSET(0, &input))
1396 /* quick & dirty way to get a command line. */
1398 if (!fgets(line, 1023, stdin))
1400 if ((end_p = strchr (line, '\n')))
1402 if ((res = sscanf(line, "%s %[^;]", word, arg)) <= 0)
1404 strcpy(word, last_cmd);
1409 strcpy(last_cmd, word);
1410 for (i = 0; cmd[i].cmd; i++)
1411 if (!strncmp(cmd[i].cmd, word, strlen(word)))
1413 res = (*cmd[i].fun)(arg);
1416 if (!cmd[i].cmd) /* dump our help-screen */
1418 printf("Unknown command: %s.\n", word);
1419 printf("Currently recognized commands:\n");
1420 for (i = 0; cmd[i].cmd; i++)
1421 printf(" %s %s\n", cmd[i].cmd, cmd[i].ad);
1431 if (conn && FD_ISSET(cs_fileno(conn), &input))
1436 if ((res = cs_get(conn, &netbuffer, &netbufferlen)) < 0)
1443 printf("Target closed connection.\n");
1446 odr_reset(in); /* release APDU from last round */
1447 odr_setbuf(in, netbuffer, res, 0);
1448 if (!z_APDU(in, &apdu, 0))
1450 odr_perror(in, "Decoding incoming APDU");
1451 fprintf(stderr, "[Near %d]\n", odr_offset(in));
1452 fprintf(stderr, "Packet dump:\n---------\n");
1453 odr_dumpBER(stderr, netbuffer, res);
1454 fprintf(stderr, "---------\n");
1458 if (!z_APDU(print, &apdu, 0))
1460 odr_perror(print, "Failed to print incoming APDU");
1467 case Z_APDU_initResponse:
1468 process_initResponse(apdu->u.initResponse);
1470 case Z_APDU_searchResponse:
1471 process_searchResponse(apdu->u.searchResponse);
1473 case Z_APDU_scanResponse:
1474 process_scanResponse(apdu->u.scanResponse);
1476 case Z_APDU_presentResponse:
1478 *apdu->u.presentResponse->numberOfRecordsReturned;
1479 if (apdu->u.presentResponse->records)
1480 display_records(apdu->u.presentResponse->records);
1482 printf("No records.\n");
1484 case Z_APDU_sortResponse:
1485 process_sortResponse(apdu->u.sortResponse);
1488 printf("Target has closed the association.\n");
1489 process_close(apdu->u.close);
1492 printf("Received unknown APDU type (%d).\n",
1499 while (cs_more(conn));
1506 int main(int argc, char **argv)
1514 cmd_base("Default");
1516 while ((ret = options("m:", argv, argc, &arg)) != -2)
1521 if (cmd_open (arg) == 2)
1525 if (!(marcdump = fopen (arg, "a")))
1532 fprintf (stderr, "Usage: %s [-m <marclog>] [<server-addr>]\n",
1539 return client (opened);