2 * Copyright (c) 1995-1998, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.71 1998-10-20 13:21:43 adam
8 * Fixed scan response handler.
10 * Revision 1.70 1998/09/22 09:40:37 adam
11 * Minor changes in sort spec.
13 * Revision 1.69 1998/08/19 16:10:06 adam
14 * Changed som member names of DeleteResultSetRequest/Response.
16 * Revision 1.68 1998/07/20 12:37:06 adam
17 * Added 'delete <resultset>' command. Changed open command so that
18 * it reconnects if already connected.
20 * Revision 1.67 1998/06/09 13:55:06 adam
23 * Revision 1.66 1998/05/18 13:06:53 adam
24 * Changed the way attribute sets are handled by the retriaval module.
25 * Extended Explain conversion / schema.
26 * Modified server and client to work with ASN.1 compiled protocol handlers.
28 * Revision 1.65 1998/03/31 15:13:19 adam
29 * Development towards compiled ASN.1.
31 * Revision 1.64 1998/03/31 11:07:44 adam
32 * Furhter work on UNIverse resource report.
33 * Added Extended Services handling in frontend server.
35 * Revision 1.63 1998/03/05 08:05:10 adam
36 * Added a few casts to make C++ happy.
38 * Revision 1.62 1998/02/11 11:53:33 adam
39 * Changed code so that it compiles as C++.
41 * Revision 1.61 1998/02/10 11:03:06 adam
42 * Implemented command refid. Client prints reference-ID's, when present,
45 * Revision 1.60 1998/01/29 14:08:52 adam
46 * Better sort diagnostics.
48 * Revision 1.59 1998/01/29 13:17:56 adam
51 * Revision 1.58 1998/01/07 13:51:45 adam
54 * Revision 1.57 1998/01/07 12:58:22 adam
55 * Using fgets instead of gets.
57 * Revision 1.56 1997/11/05 09:18:31 adam
58 * The client handles records with no associated syntax.
60 * Revision 1.55 1997/10/31 12:20:08 adam
61 * Improved memory debugging for xmalloc/nmem.c. References to NMEM
62 * instead of ODR in n ESPEC-1 handling in source d1_espec.c.
63 * Bug fix: missing fclose in data1_read_espec1.
65 * Revision 1.54 1997/10/27 13:52:46 adam
66 * Header yaz-util includes all YAZ utility header files.
68 * Revision 1.53 1997/09/29 13:18:59 adam
69 * Added function, oid_ent_to_oid, to replace the function
70 * oid_getoidbyent, which is not thread safe.
72 * Revision 1.52 1997/09/29 07:20:31 adam
73 * Client code uses nmem_init.
75 * Revision 1.51 1997/09/26 09:41:55 adam
76 * Updated client to handle multiple diagnostics.
78 * Revision 1.50 1997/09/17 12:10:29 adam
81 * Revision 1.49 1997/09/04 13:45:17 adam
82 * Added UNImarc to list of available syntaxes.
84 * Revision 1.48 1997/09/01 08:48:44 adam
85 * New windows NT/95 port using MSV5.0. Only a few changes made
86 * to avoid warnings. Sub project created: client.dsp.
88 * Revision 1.47 1997/07/01 13:49:56 adam
89 * Take care of case when invalid target is specified on command line.
91 * Revision 1.46 1997/06/23 10:30:18 adam
92 * Added call to ccl_rpn_delete in search. Added ODR stream "out"
93 * as parameter to ccl_rpn_query to release RPN query.
95 * Revision 1.45 1997/05/14 06:53:29 adam
98 * Revision 1.44 1997/05/05 11:20:35 adam
99 * Client uses "options" utility and marc dump filename may be specified
100 * as an option (-m <file>).
102 * Revision 1.43 1996/11/08 11:03:26 adam
103 * Client accepts multiple database names.
105 * Revision 1.42 1996/10/08 10:44:57 quinn
106 * Resolved conflicts.
108 * Revision 1.41 1996/10/07 15:29:03 quinn
111 * Revision 1.40 1996/08/29 14:19:34 quinn
112 * Fixed conflict (CVS)
114 * Revision 1.39 1996/08/27 10:43:22 quinn
115 * Made select() optional
117 * Revision 1.38 1996/08/12 14:09:11 adam
118 * Default prefix query attribute set defined by using p_query_attset.
120 * Revision 1.37 1996/07/06 19:58:29 quinn
121 * System headerfiles gathered in yconfig
123 * Revision 1.36 1996/06/10 08:53:47 quinn
126 * Revision 1.35 1996/06/03 09:45:50 quinn
127 * Added display of OIDs in the GRS routine.
129 * Revision 1.34 1996/05/09 07:26:49 quinn
130 * *** empty log message ***
132 * Revision 1.33 1996/05/09 07:25:22 quinn
135 * Revision 1.32 1996/03/15 11:05:33 adam
136 * The user can set the preferred query type (prefix, ccl, ..) with the
139 * Revision 1.31 1996/02/20 12:51:54 quinn
140 * Fixed problems with EXTERNAL.
142 * Revision 1.30 1996/02/12 18:18:09 quinn
145 * Revision 1.29 1996/01/02 08:57:25 quinn
146 * Changed enums in the ASN.1 .h files to #defines. Changed oident.class to oclass
148 * Revision 1.28 1995/12/14 11:09:31 quinn
149 * Added Explain record syntax to the format command.
151 * Revision 1.27 1995/12/12 16:37:02 quinn
152 * Added destroy element to data1_node.
154 * Revision 1.26 1995/12/12 14:11:00 quinn
157 * Revision 1.25 1995/11/13 09:27:22 quinn
158 * Fiddling with the variant stuff.
160 * Revision 1.24 1995/10/30 12:41:13 quinn
161 * Added hostname lookup for server.
163 * Revision 1.23 1995/10/18 16:12:30 quinn
164 * Better diagnostics.
166 * Revision 1.22 1995/10/11 14:49:12 quinn
169 * Revision 1.21 1995/09/29 17:01:47 quinn
172 * Revision 1.20 1995/08/29 14:24:13 quinn
173 * Added second half of close-handshake
175 * Revision 1.19 1995/08/29 11:17:28 quinn
176 * Added code to receive close
178 * Revision 1.18 1995/08/28 12:21:27 quinn
179 * Client can now ask for simple element set names.
181 * Revision 1.17 1995/08/17 12:45:02 quinn
182 * Fixed minor problems with GRS-1. Added support in c&s.
184 * Revision 1.16 1995/08/15 12:00:04 quinn
187 * Revision 1.15 1995/06/22 09:28:03 quinn
188 * Fixed bug in SUTRS processing.
190 * Revision 1.14 1995/06/19 12:37:41 quinn
193 * Revision 1.13 1995/06/16 10:29:11 quinn
194 * *** empty log message ***
196 * Revision 1.12 1995/06/15 07:44:57 quinn
199 * Revision 1.11 1995/06/14 15:26:40 quinn
200 * *** empty log message ***
202 * Revision 1.10 1995/06/06 14:56:58 quinn
203 * Better diagnostics.
205 * Revision 1.9 1995/06/06 08:15:19 quinn
208 * Revision 1.8 1995/06/05 10:52:22 quinn
211 * Revision 1.7 1995/06/02 09:50:09 quinn
214 * Revision 1.6 1995/05/31 08:29:21 quinn
215 * Nothing significant.
217 * Revision 1.5 1995/05/29 08:10:47 quinn
218 * Moved oid.c to util.
220 * Revision 1.4 1995/05/22 15:30:13 adam
221 * Client uses prefix query notation.
223 * Revision 1.3 1995/05/22 15:06:53 quinn
224 * *** empty log message ***
226 * Revision 1.2 1995/05/22 14:56:40 quinn
227 * *** empty log message ***
229 * Revision 1.1 1995/05/22 11:30:31 quinn
230 * Added prettier client.
236 * This is the obligatory little toy client, whose primary purpose is
237 * to illustrate the use of the YAZ service-level API.
245 #include <yaz-util.h>
247 #include <comstack.h>
254 #include <marcdisp.h>
255 #include <diagbib1.h>
263 #define C_PROMPT "Z> "
265 static ODR out, in, print; /* encoding and decoding streams */
266 static COMSTACK conn = 0; /* our z-association */
267 static Z_IdAuthentication *auth = 0; /* our current auth definition */
268 static char *databaseNames[128];
269 static int num_databaseNames = 0;
270 static int setnumber = 0; /* current result set number */
271 static int smallSetUpperBound = 0;
272 static int largeSetLowerBound = 1;
273 static int mediumSetPresentNumber = 0;
274 static Z_ElementSetNames *elementSetNames = 0;
275 static int setno = 1; /* current set offset */
276 static enum oid_proto protocol = PROTO_Z3950; /* current app protocol */
277 static enum oid_value recordsyntax = VAL_USMARC;
278 static int sent_close = 0;
279 static NMEM session_mem = NULL; /* memory handle for init-response */
280 static Z_InitResponse *session = 0; /* session parameters */
281 static char last_scan[512] = "0";
282 static char last_cmd[100] = "?";
283 static FILE *marcdump = 0;
284 static char *refid = NULL;
292 static QueryType queryType = QueryType_Prefix;
295 static CCL_bibset bibset; /* CCL bibset handle */
298 static void send_apdu(Z_APDU *a)
303 if (!z_APDU(out, &a, 0))
305 odr_perror(out, "Encoding APDU");
308 buf = odr_getbuf(out, &len, 0);
309 if (cs_put(conn, buf, len) < 0)
311 fprintf(stderr, "cs_put: %s", cs_errmsg(cs_errno(conn)));
314 odr_reset(out); /* release the APDU structure */
317 static void print_refid (Z_ReferenceId *id)
321 printf ("ReferenceId: '%.*s'\n", id->len, id->buf);
325 static Z_ReferenceId *set_refid (ODR out)
330 id = (Z_ReferenceId *) odr_malloc (out, sizeof(*id));
331 id->size = id->len = strlen(refid);
332 id->buf = (unsigned char *) odr_malloc (out, id->len);
333 memcpy (id->buf, refid, id->len);
337 /* INIT SERVICE ------------------------------- */
339 static void send_initRequest()
341 Z_APDU *apdu = zget_APDU(out, Z_APDU_initRequest);
342 Z_InitRequest *req = apdu->u.initRequest;
344 ODR_MASK_SET(req->options, Z_Options_search);
345 ODR_MASK_SET(req->options, Z_Options_present);
346 ODR_MASK_SET(req->options, Z_Options_namedResultSets);
347 ODR_MASK_SET(req->options, Z_Options_triggerResourceCtrl);
348 ODR_MASK_SET(req->options, Z_Options_scan);
349 ODR_MASK_SET(req->options, Z_Options_sort);
350 ODR_MASK_SET(req->options, Z_Options_extendedServices);
351 ODR_MASK_SET(req->options, Z_Options_delSet);
353 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_1);
354 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_2);
355 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_3);
357 *req->maximumRecordSize = 1024*1024;
359 req->idAuthentication = auth;
362 printf("Sent initrequest.\n");
365 static int process_initResponse(Z_InitResponse *res)
367 /* save session parameters for later use */
368 session_mem = odr_extract_mem(in);
372 printf("Connection rejected by target.\n");
374 printf("Connection accepted by target.\n");
375 if (res->implementationId)
376 printf("ID : %s\n", res->implementationId);
377 if (res->implementationName)
378 printf("Name : %s\n", res->implementationName);
379 if (res->implementationVersion)
380 printf("Version: %s\n", res->implementationVersion);
381 if (res->userInformationField)
383 printf("UserInformationfield:\n");
384 if (!z_External(print, (Z_External**)&res-> userInformationField,
387 odr_perror(print, "Printing userinfo\n");
390 if (res->userInformationField->which == Z_External_octet)
392 printf("Guessing visiblestring:\n");
393 printf("'%s'\n", res->userInformationField->u. octet_aligned->buf);
400 static int cmd_base(char *arg)
407 printf("Usage: base <database> <database> ...\n");
410 for (i = 0; i<num_databaseNames; i++)
411 xfree (databaseNames[i]);
412 num_databaseNames = 0;
415 if (!(cp = strchr(arg, ' ')))
416 cp = arg + strlen(arg);
419 databaseNames[num_databaseNames] = (char *)xmalloc (1 + cp - arg);
420 memcpy (databaseNames[num_databaseNames], arg, cp - arg);
421 databaseNames[num_databaseNames++][cp - arg] = '\0';
430 int cmd_open(char *arg)
433 char type[100], addr[100], base[100];
438 printf("Already connected.\n");
444 nmem_destroy (session_mem);
449 if (!*arg || sscanf(arg, "%[^:]:%[^/]/%s", type, addr, base) < 2)
451 fprintf(stderr, "Usage: open (osi|tcp) ':' [tsel '/']host[':'port]\n");
456 if (!strcmp(type, "tcp"))
459 protocol = PROTO_Z3950;
463 if (!strcmp(type, "osi"))
471 fprintf(stderr, "Bad type: %s\n", type);
474 if (!(conn = cs_create(t, 1, protocol)))
479 if (!(add = cs_straddr(conn, addr)))
484 printf("Connecting...");
486 if (cs_connect(conn, add) < 0)
498 int cmd_authentication(char *arg)
500 static Z_IdAuthentication au;
501 static char open[256];
505 printf("Auth field set to null\n");
510 au.which = Z_IdAuthentication_open;
516 /* SEARCH SERVICE ------------------------------ */
518 static void display_variant(Z_Variant *v, int level)
522 for (i = 0; i < v->num_triples; i++)
524 printf("%*sclass=%d,type=%d", level * 4, "", *v->triples[i]->zclass,
525 *v->triples[i]->type);
526 if (v->triples[i]->which == Z_Triple_internationalString)
527 printf(",value=%s\n", v->triples[i]->value.internationalString);
533 static void display_grs1(Z_GenericRecord *r, int level)
539 for (i = 0; i < r->num_elements; i++)
543 printf("%*s", level * 4, "");
547 printf("%d,", *t->tagType);
550 if (t->tagValue->which == Z_StringOrNumeric_numeric)
551 printf("%d) ", *t->tagValue->u.numeric);
553 printf("%s) ", t->tagValue->u.string);
554 if (t->content->which == Z_ElementData_subtree)
557 display_grs1(t->content->u.subtree, level+1);
559 else if (t->content->which == Z_ElementData_string)
560 printf("%s\n", t->content->u.string);
561 else if (t->content->which == Z_ElementData_numeric)
562 printf("%d\n", *t->content->u.numeric);
563 else if (t->content->which == Z_ElementData_oid)
565 int *ip = t->content->u.oid;
568 if ((oent = oid_getentbyoid(t->content->u.oid)))
569 printf("OID: %s\n", oent->desc);
573 while (ip && *ip >= 0)
574 printf(" %d", *(ip++));
578 else if (t->content->which == Z_ElementData_noDataRequested)
579 printf("[No data requested]\n");
580 else if (t->content->which == Z_ElementData_elementEmpty)
581 printf("[Element empty]\n");
582 else if (t->content->which == Z_ElementData_elementNotThere)
583 printf("[Element not there]\n");
586 if (t->appliedVariant)
587 display_variant(t->appliedVariant, level+1);
588 if (t->metaData && t->metaData->supportedVariants)
592 printf("%*s---- variant list\n", (level+1)*4, "");
593 for (c = 0; c < t->metaData->num_supportedVariants; c++)
595 printf("%*svariant #%d\n", (level+1)*4, "", c);
596 display_variant(t->metaData->supportedVariants[c], level + 2);
602 static void display_record(Z_DatabaseRecord *p)
604 Z_External *r = (Z_External*) p;
605 oident *ent = oid_getentbyoid(r->direct_reference);
608 * Tell the user what we got.
610 if (r->direct_reference)
612 printf("Record type: ");
614 printf("%s\n", ent->desc);
615 else if (!odr_oid(print, &r->direct_reference, 0))
617 odr_perror(print, "print oid");
621 /* Check if this is a known, ASN.1 type tucked away in an octet string */
622 if (ent && r->which == Z_External_octet)
624 Z_ext_typeent *type = z_ext_getentbyref(ent->value);
630 * Call the given decoder to process the record.
632 odr_setbuf(in, (char*)p->u.octet_aligned->buf,
633 p->u.octet_aligned->len, 0);
634 if (!(*type->fun)(in, (char **)&rr, 0))
636 odr_perror(in, "Decoding constructed record.");
637 fprintf(stderr, "[Near %d]\n", odr_offset(in));
638 fprintf(stderr, "Packet dump:\n---------\n");
639 odr_dumpBER(stderr, (char*)p->u.octet_aligned->buf,
640 p->u.octet_aligned->len);
641 fprintf(stderr, "---------\n");
645 * Note: we throw away the original, BER-encoded record here.
646 * Do something else with it if you want to keep it.
648 r->u.sutrs = (Z_SUTRS *) rr; /* we don't actually check the type here. */
649 r->which = type->what;
652 if (ent && ent->value == VAL_SOIF)
653 printf("%.*s", r->u.octet_aligned->len, r->u.octet_aligned->buf);
654 else if (r->which == Z_External_octet && p->u.octet_aligned->len)
656 const char *marc_buf = (char*)p->u.octet_aligned->buf;
657 marc_display (marc_buf, NULL);
659 fwrite (marc_buf, strlen (marc_buf), 1, marcdump);
661 else if (ent && ent->value == VAL_SUTRS)
663 if (r->which != Z_External_sutrs)
665 printf("Expecting single SUTRS type for SUTRS.\n");
668 printf("%.*s", r->u.sutrs->len, r->u.sutrs->buf);
670 else if (ent && ent->value == VAL_GRS1)
672 if (r->which != Z_External_grs1)
674 printf("Expecting single GRS type for GRS.\n");
677 display_grs1(r->u.grs1, 0);
681 printf("Unknown record representation.\n");
682 if (!z_External(print, &r, 0))
684 odr_perror(print, "Printing external");
691 static void display_diagrecs(Z_DiagRec **pp, int num)
695 Z_DefaultDiagFormat *r;
697 printf("Diagnostic message(s) from database:\n");
698 for (i = 0; i<num; i++)
700 Z_DiagRec *p = pp[i];
701 if (p->which != Z_DiagRec_defaultFormat)
703 printf("Diagnostic record not in default format.\n");
707 r = p->u.defaultFormat;
708 if (!(ent = oid_getentbyoid(r->diagnosticSetId)) ||
709 ent->oclass != CLASS_DIAGSET || ent->value != VAL_BIB1)
710 printf("Missing or unknown diagset\n");
711 printf(" [%d] %s", *r->condition, diagbib1_str(*r->condition));
715 case Z_DefaultDiagFormat_v2Addinfo:
716 printf (" -- v2 addinfo '%s'\n", r->u.v2Addinfo);
718 case Z_DefaultDiagFormat_v3Addinfo:
719 printf (" -- v3 addinfo '%s'\n", r->u.v3Addinfo);
723 if (r->addinfo && *r->addinfo)
724 printf(" -- '%s'\n", r->addinfo);
732 static void display_nameplusrecord(Z_NamePlusRecord *p)
735 printf("[%s]", p->databaseName);
736 if (p->which == Z_NamePlusRecord_surrogateDiagnostic)
737 display_diagrecs(&p->u.surrogateDiagnostic, 1);
739 display_record(p->u.databaseRecord);
742 static void display_records(Z_Records *p)
746 if (p->which == Z_Records_NSD)
749 Z_DiagRec dr, *dr_p = &dr;
750 dr.which = Z_DiagRec_defaultFormat;
751 dr.u.defaultFormat = p->u.nonSurrogateDiagnostic;
752 display_diagrecs (&dr_p, 1);
754 display_diagrecs (&p->u.nonSurrogateDiagnostic, 1);
757 else if (p->which == Z_Records_multipleNSD)
758 display_diagrecs (p->u.multipleNonSurDiagnostics->diagRecs,
759 p->u.multipleNonSurDiagnostics->num_diagRecs);
762 printf("Records: %d\n", p->u.databaseOrSurDiagnostics->num_records);
763 for (i = 0; i < p->u.databaseOrSurDiagnostics->num_records; i++)
764 display_nameplusrecord(p->u.databaseOrSurDiagnostics->records[i]);
768 static int send_deleteResultSetRequest(char *arg)
770 Z_APDU *apdu = zget_APDU(out, Z_APDU_deleteResultSetRequest);
771 Z_DeleteResultSetRequest *req = apdu->u.deleteResultSetRequest;
773 req->referenceId = set_refid (out);
775 req->num_resultSetList = 1;
776 req->resultSetList = (char **)
777 odr_malloc (out, sizeof(*req->resultSetList));
778 *req->resultSetList = arg;
779 req->deleteFunction = (int *)
780 odr_malloc (out, sizeof(*req->deleteFunction));
781 *req->deleteFunction = Z_DeleteRequest_list;
784 printf("Sent deleteResultSetRequest.\n");
788 static int send_searchRequest(char *arg)
790 Z_APDU *apdu = zget_APDU(out, Z_APDU_searchRequest);
791 Z_SearchRequest *req = apdu->u.searchRequest;
795 struct ccl_rpn_node *rpn = NULL;
800 Z_RPNQuery *RPNquery;
804 if (queryType == QueryType_CCL2RPN)
806 rpn = ccl_find_str(bibset, arg, &error, &pos);
809 printf("CCL ERROR: %s\n", ccl_err_msg(error));
814 req->referenceId = set_refid (out);
815 if (!strcmp(arg, "@big")) /* strictly for troublemaking */
817 static unsigned char big[2100];
820 /* send a very big referenceid to test transport stack etc. */
821 memset(big, 'A', 2100);
822 bigo.len = bigo.size = 2100;
824 req->referenceId = &bigo;
829 sprintf(setstring, "%d", ++setnumber);
830 req->resultSetName = setstring;
832 *req->smallSetUpperBound = smallSetUpperBound;
833 *req->largeSetLowerBound = largeSetLowerBound;
834 *req->mediumSetPresentNumber = mediumSetPresentNumber;
835 if (smallSetUpperBound > 0 || (largeSetLowerBound > 1 &&
836 mediumSetPresentNumber > 0))
840 prefsyn.proto = protocol;
841 prefsyn.oclass = CLASS_RECSYN;
842 prefsyn.value = recordsyntax;
843 req->preferredRecordSyntax =
844 odr_oiddup(out, oid_ent_to_oid(&prefsyn, oid));
845 req->smallSetElementSetNames =
846 req->mediumSetElementSetNames = elementSetNames;
848 req->num_databaseNames = num_databaseNames;
849 req->databaseNames = databaseNames;
855 case QueryType_Prefix:
856 query.which = Z_Query_type_1;
857 RPNquery = p_query_rpn (out, protocol, arg);
860 printf("Prefix query error\n");
863 query.u.type_1 = RPNquery;
866 query.which = Z_Query_type_2;
867 query.u.type_2 = &ccl_query;
868 ccl_query.buf = (unsigned char*) arg;
869 ccl_query.len = strlen(arg);
872 case QueryType_CCL2RPN:
873 query.which = Z_Query_type_1;
874 RPNquery = ccl_rpn_query(out, rpn);
876 bib1.proto = protocol;
877 bib1.oclass = CLASS_ATTSET;
878 bib1.value = VAL_BIB1;
879 RPNquery->attributeSetId = oid_ent_to_oid(&bib1, oid);
880 query.u.type_1 = RPNquery;
881 ccl_rpn_delete (rpn);
885 printf ("Unsupported query type\n");
890 printf("Sent searchRequest.\n");
894 static int process_searchResponse(Z_SearchResponse *res)
896 printf ("Received SearchResponse.\n");
897 print_refid (res->referenceId);
898 if (*res->searchStatus)
899 printf("Search was a success.\n");
901 printf("Search was a bloomin' failure.\n");
902 printf("Number of hits: %d, setno %d\n",
903 *res->resultCount, setnumber);
904 printf("records returned: %d\n",
905 *res->numberOfRecordsReturned);
906 setno += *res->numberOfRecordsReturned;
908 display_records(res->records);
912 static void print_level(int iLevel)
915 for (i = 0; i < iLevel * 4; i++)
919 static void print_int(int iLevel, const char *pTag, int *pInt)
924 printf("%s: %d\n", pTag, *pInt);
928 static void print_string(int iLevel, const char *pTag, const char *pString)
933 printf("%s: %s\n", pTag, pString);
937 static void print_oid(int iLevel, const char *pTag, Odr_oid *pOid)
945 for (; *pInt != -1; pInt++)
946 printf(" %d", *pInt);
951 static void print_referenceId(int iLevel, Z_ReferenceId *referenceId)
953 if (referenceId != NULL)
958 printf("Ref Id (%d, %d): ", referenceId->len, referenceId->size);
959 for (i = 0; i < referenceId->len; i++)
960 printf("%c", referenceId->buf[i]);
965 static void print_string_or_numeric(int iLevel, const char *pTag, Z_StringOrNumeric *pStringNumeric)
967 if (pStringNumeric != NULL)
969 switch (pStringNumeric->which)
971 case Z_StringOrNumeric_string:
972 print_string(iLevel, pTag, pStringNumeric->u.string);
975 case Z_StringOrNumeric_numeric:
976 print_int(iLevel, pTag, pStringNumeric->u.numeric);
981 printf("%s: valid type for Z_StringOrNumeric\n", pTag);
987 static void print_universe_report_duplicate(int iLevel, Z_UniverseReportDuplicate *pUniverseReportDuplicate)
989 if (pUniverseReportDuplicate != NULL)
992 printf("Universe Report Duplicate: \n");
994 print_string_or_numeric(iLevel, "Hit No", pUniverseReportDuplicate->hitno);
998 static void print_universe_report_hits(int iLevel, Z_UniverseReportHits *pUniverseReportHits)
1000 if (pUniverseReportHits != NULL)
1002 print_level(iLevel);
1003 printf("Universe Report Hits: \n");
1005 print_string_or_numeric(iLevel, "Database", pUniverseReportHits->database);
1006 print_string_or_numeric(iLevel, "Hits", pUniverseReportHits->hits);
1010 static void print_universe_report(int iLevel, Z_UniverseReport *pUniverseReport)
1012 if (pUniverseReport != NULL)
1014 print_level(iLevel);
1015 printf("Universe Report: \n");
1017 print_int(iLevel, "Total Hits", pUniverseReport->totalHits);
1018 switch (pUniverseReport->which)
1020 case Z_UniverseReport_databaseHits:
1021 print_universe_report_hits(iLevel, pUniverseReport->u.databaseHits);
1024 case Z_UniverseReport_duplicate:
1025 print_universe_report_duplicate(iLevel, pUniverseReport->u.duplicate);
1029 print_level(iLevel);
1030 printf("Type: %d\n", pUniverseReport->which);
1036 static void print_external(int iLevel, Z_External *pExternal)
1038 if (pExternal != NULL)
1040 print_level(iLevel);
1041 printf("External: \n");
1043 print_oid(iLevel, "Direct Reference", pExternal->direct_reference);
1044 print_int(iLevel, "InDirect Reference", pExternal->indirect_reference);
1045 print_string(iLevel, "Descriptor", pExternal->descriptor);
1046 switch (pExternal->which)
1048 case Z_External_universeReport:
1049 print_universe_report(iLevel, pExternal->u.universeReport);
1053 print_level(iLevel);
1054 printf("Type: %d\n", pExternal->which);
1060 static int process_resourceControlRequest (Z_ResourceControlRequest *req)
1062 printf ("Received ResourceControlRequest.\n");
1063 print_referenceId(1, req->referenceId);
1064 print_int(1, "Suspended Flag", req->suspendedFlag);
1065 print_int(1, "Partial Results Available", req->partialResultsAvailable);
1066 print_int(1, "Response Required", req->responseRequired);
1067 print_int(1, "Triggered Request Flag", req->triggeredRequestFlag);
1068 print_external(1, req->resourceReport);
1072 void process_ESResponse(Z_ExtendedServicesResponse *res)
1074 printf("process_ESResponse\n");
1077 static Z_External *CreateItemOrderExternal(int itemno)
1079 Z_External *r = (Z_External *) odr_malloc(out, sizeof(Z_External));
1080 oident ItemOrderRequest;
1082 ItemOrderRequest.proto = PROTO_Z3950;
1083 ItemOrderRequest.oclass = CLASS_EXTSERV;
1084 ItemOrderRequest.value = VAL_ITEMORDER;
1086 r->direct_reference = odr_oiddup(out,oid_getoidbyent(&ItemOrderRequest));
1087 r->indirect_reference = (int *) odr_malloc(out,sizeof(int));
1088 *r->indirect_reference = 0;
1090 r->descriptor = "Extended services item order";
1092 r->which = Z_External_itemOrder;
1094 r->u.itemOrder = (Z_ItemOrder *) odr_malloc(out,sizeof(Z_ItemOrder));
1095 memset(r->u.itemOrder, 0, sizeof(Z_ItemOrder));
1097 r->u.itemOrder->which=Z_IOItemOrder_esRequest;
1099 r->u.itemOrder->which=Z_ItemOrder_esRequest;
1102 r->u.itemOrder->u.esRequest = (Z_IORequest *)
1103 odr_malloc(out,sizeof(Z_IORequest));
1104 memset(r->u.itemOrder->u.esRequest, 0, sizeof(Z_IORequest));
1106 r->u.itemOrder->u.esRequest->toKeep = (Z_IOOriginPartToKeep *)
1107 odr_malloc(out,sizeof(Z_IOOriginPartToKeep));
1108 memset(r->u.itemOrder->u.esRequest->toKeep, 0, sizeof(Z_IOOriginPartToKeep));
1109 r->u.itemOrder->u.esRequest->notToKeep = (Z_IOOriginPartNotToKeep *)
1110 odr_malloc(out,sizeof(Z_IOOriginPartNotToKeep));
1111 memset(r->u.itemOrder->u.esRequest->notToKeep, 0, sizeof(Z_IOOriginPartNotToKeep));
1113 r->u.itemOrder->u.esRequest->toKeep->supplDescription = NULL;
1114 r->u.itemOrder->u.esRequest->toKeep->contact = NULL;
1115 r->u.itemOrder->u.esRequest->toKeep->addlBilling = NULL;
1117 r->u.itemOrder->u.esRequest->notToKeep->resultSetItem =
1118 (Z_IOResultSetItem *) odr_malloc(out, sizeof(Z_IOResultSetItem));
1119 memset(r->u.itemOrder->u.esRequest->notToKeep->resultSetItem, 0, sizeof(Z_IOResultSetItem));
1120 r->u.itemOrder->u.esRequest->notToKeep->resultSetItem->resultSetId = "1";
1122 r->u.itemOrder->u.esRequest->notToKeep->resultSetItem->item =
1123 (int *) odr_malloc(out, sizeof(int));
1124 *r->u.itemOrder->u.esRequest->notToKeep->resultSetItem->item = itemno;
1126 r->u.itemOrder->u.esRequest->notToKeep->itemRequest = NULL;
1130 /* II : Added to do DALI Item Order Extended services request */
1131 static int send_itemorder(char *arg)
1134 Z_APDU *apdu = zget_APDU(out, Z_APDU_extendedServicesRequest );
1135 Z_ExtendedServicesRequest *req = apdu->u.extendedServicesRequest;
1136 oident ItemOrderRequest;
1141 /* Set up item order request */
1143 /* Function being performed by this extended services request */
1144 req->function = (int *) odr_malloc(out, sizeof(int));
1145 *req->function = Z_ExtendedServicesRequest_create;
1147 /* Package type, Using protocol ILL ( But that's not in the oid.h file yet */
1148 /* create an object of class Extended Service, value Item Order */
1149 ItemOrderRequest.proto = PROTO_Z3950;
1150 ItemOrderRequest.oclass = CLASS_EXTSERV;
1151 ItemOrderRequest.value = VAL_ITEMORDER;
1152 req->packageType = odr_oiddup(out,oid_getoidbyent(&ItemOrderRequest));
1153 req->packageName = "1.Extendedserveq";
1155 /* ** taskSpecificParameters ** */
1156 req->taskSpecificParameters = CreateItemOrderExternal(itemno);
1158 /* waitAction - Create the ILL request and that's it */
1159 *req->waitAction = Z_ExtendedServicesRequest_wait;
1165 /* II : Added to do DALI Item Order Extended services request */
1166 static int cmd_itemorder(char *arg)
1168 printf("Item order request\n");
1171 send_itemorder(arg);
1175 static int cmd_find(char *arg)
1179 printf("Find what?\n");
1184 printf("Not connected yet\n");
1187 if (!send_searchRequest(arg))
1192 static int cmd_delete(char *arg)
1196 printf("Delete what?\n");
1201 printf("Not connected yet\n");
1204 if (!send_deleteResultSetRequest(arg))
1209 static int cmd_ssub(char *arg)
1211 if (!(smallSetUpperBound = atoi(arg)))
1216 static int cmd_lslb(char *arg)
1218 if (!(largeSetLowerBound = atoi(arg)))
1223 static int cmd_mspn(char *arg)
1225 if (!(mediumSetPresentNumber = atoi(arg)))
1230 static int cmd_status(char *arg)
1232 printf("smallSetUpperBound: %d\n", smallSetUpperBound);
1233 printf("largeSetLowerBound: %d\n", largeSetLowerBound);
1234 printf("mediumSetPresentNumber: %d\n", mediumSetPresentNumber);
1238 static int cmd_setnames(char *arg)
1242 printf("Set numbering enabled.\n");
1247 printf("Set numbering disabled.\n");
1253 /* PRESENT SERVICE ----------------------------- */
1255 static int send_presentRequest(char *arg)
1257 Z_APDU *apdu = zget_APDU(out, Z_APDU_presentRequest);
1258 Z_PresentRequest *req = apdu->u.presentRequest;
1259 Z_RecordComposition compo;
1264 char setstring[100];
1266 req->referenceId = set_refid (out);
1267 if ((p = strchr(arg, '+')))
1274 if (p && (p=strchr(p+1, '+')))
1276 strcpy (setstring, p+1);
1277 req->resultSetId = setstring;
1279 else if (setnumber >= 0)
1281 sprintf(setstring, "%d", setnumber);
1282 req->resultSetId = setstring;
1287 static Z_Range range;
1288 static Z_Range *rangep = ⦥
1289 req->num_ranges = 1;
1291 req->resultSetStartPoint = &setno;
1292 req->numberOfRecordsRequested = &nos;
1293 prefsyn.proto = protocol;
1294 prefsyn.oclass = CLASS_RECSYN;
1295 prefsyn.value = recordsyntax;
1296 req->preferredRecordSyntax = oid_ent_to_oid(&prefsyn, oid);
1298 if (elementSetNames)
1300 req->recordComposition = &compo;
1301 compo.which = Z_RecordComp_simple;
1302 compo.u.simple = elementSetNames;
1305 printf("Sent presentRequest (%d+%d).\n", setno, nos);
1309 void process_close(Z_Close *req)
1311 Z_APDU *apdu = zget_APDU(out, Z_APDU_close);
1312 Z_Close *res = apdu->u.close;
1314 static char *reasons[] =
1319 "cost limit reached",
1321 "security violation",
1328 printf("Reason: %s, message: %s\n", reasons[*req->closeReason],
1329 req->diagnosticInformation ? req->diagnosticInformation : "NULL");
1336 nmem_destroy (session_mem);
1343 *res->closeReason = Z_Close_finished;
1345 printf("Sent response.\n");
1350 static int cmd_show(char *arg)
1354 printf("Not connected yet\n");
1357 if (!send_presentRequest(arg))
1362 int cmd_quit(char *arg)
1364 printf("See you later, alligator.\n");
1369 int cmd_cancel(char *arg)
1371 Z_APDU *apdu = zget_APDU(out, Z_APDU_triggerResourceControlRequest);
1372 Z_TriggerResourceControlRequest *req =
1373 apdu->u.triggerResourceControlRequest;
1378 printf("Session not initialized yet\n");
1381 if (!ODR_MASK_GET(session->options, Z_Options_triggerResourceCtrl))
1383 printf("Target doesn't support cancel (trigger resource ctrl)\n");
1386 *req->requestedAction = Z_TriggerResourceCtrl_cancel;
1387 req->resultSetWanted = &rfalse;
1390 printf("Sent cancel request\n");
1394 int send_scanrequest(char *string, int pp, int num)
1396 Z_APDU *apdu = zget_APDU(out, Z_APDU_scanRequest);
1397 Z_ScanRequest *req = apdu->u.scanRequest;
1399 if (!(req->termListAndStartPoint =
1400 p_query_scan(out, protocol, &req->attributeSet, string)))
1402 printf("Prefix query error\n");
1405 req->referenceId = set_refid (out);
1406 req->num_databaseNames = num_databaseNames;
1407 req->databaseNames = databaseNames;
1408 req->numberOfTermsRequested = #
1409 req->preferredPositionInResponse = &pp;
1414 int send_sortrequest(char *arg, int newset)
1416 Z_APDU *apdu = zget_APDU(out, Z_APDU_sortRequest);
1417 Z_SortRequest *req = apdu->u.sortRequest;
1418 Z_SortKeySpecList *sksl = (Z_SortKeySpecList *)odr_malloc (out, sizeof(*sksl));
1420 char sort_string[32], sort_flags[32];
1426 sprintf (setstring, "%d", setnumber);
1428 sprintf (setstring, "default");
1430 req->referenceId = set_refid (out);
1433 req->num_inputResultSetNames = 1;
1434 req->inputResultSetNames = (Z_InternationalString **)
1435 odr_malloc (out, sizeof(*req->inputResultSetNames));
1436 req->inputResultSetNames[0] = (char *)
1437 odr_malloc (out, strlen(setstring)+1);
1438 strcpy (req->inputResultSetNames[0], setstring);
1440 req->inputResultSetNames =
1441 (Z_StringList *)odr_malloc (out, sizeof(*req->inputResultSetNames));
1442 req->inputResultSetNames->num_strings = 1;
1443 req->inputResultSetNames->strings =
1444 (char **)odr_malloc (out, sizeof(*req->inputResultSetNames->strings));
1445 req->inputResultSetNames->strings[0] =
1446 (char *)odr_malloc (out, strlen(setstring)+1);
1447 strcpy (req->inputResultSetNames->strings[0], setstring);
1450 if (newset && setnumber >= 0)
1451 sprintf (setstring, "%d", ++setnumber);
1453 req->sortedResultSetName = (char *)odr_malloc (out, strlen(setstring)+1);
1454 strcpy (req->sortedResultSetName, setstring);
1456 req->sortSequence = sksl;
1457 sksl->num_specs = 0;
1458 sksl->specs = (Z_SortKeySpec **)odr_malloc (out, sizeof(sksl->specs) * 20);
1460 bib1.proto = protocol;
1461 bib1.oclass = CLASS_ATTSET;
1462 bib1.value = VAL_BIB1;
1463 while ((sscanf (arg, "%31s %31s%n", sort_string, sort_flags, &off)) == 2
1467 char *sort_string_sep;
1468 Z_SortKeySpec *sks = (Z_SortKeySpec *)odr_malloc (out, sizeof(*sks));
1469 Z_SortKey *sk = (Z_SortKey *)odr_malloc (out, sizeof(*sk));
1472 sksl->specs[sksl->num_specs++] = sks;
1473 sks->sortElement = (Z_SortElement *)odr_malloc (out, sizeof(*sks->sortElement));
1474 sks->sortElement->which = Z_SortElement_generic;
1475 sks->sortElement->u.generic = sk;
1477 if ((sort_string_sep = strchr (sort_string, '=')))
1479 Z_AttributeElement *el = (Z_AttributeElement *)odr_malloc (out, sizeof(*el));
1480 sk->which = Z_SortKey_sortAttributes;
1481 sk->u.sortAttributes =
1482 (Z_SortAttributes *)odr_malloc (out, sizeof(*sk->u.sortAttributes));
1483 sk->u.sortAttributes->id = oid_ent_to_oid(&bib1, oid);
1484 sk->u.sortAttributes->list =
1485 (Z_AttributeList *)odr_malloc (out, sizeof(*sk->u.sortAttributes->list));
1486 sk->u.sortAttributes->list->num_attributes = 1;
1487 sk->u.sortAttributes->list->attributes =
1488 (Z_AttributeElement **)odr_malloc (out,
1489 sizeof(*sk->u.sortAttributes->list->attributes));
1490 sk->u.sortAttributes->list->attributes[0] = el;
1491 el->attributeSet = 0;
1492 el->attributeType = (int *)odr_malloc (out, sizeof(*el->attributeType));
1493 *el->attributeType = atoi (sort_string);
1494 el->which = Z_AttributeValue_numeric;
1495 el->value.numeric = (int *)odr_malloc (out, sizeof(*el->value.numeric));
1496 *el->value.numeric = atoi (sort_string_sep + 1);
1500 sk->which = Z_SortKey_sortField;
1501 sk->u.sortField = (char *)odr_malloc (out, strlen(sort_string)+1);
1502 strcpy (sk->u.sortField, sort_string);
1504 sks->sortRelation = (int *)odr_malloc (out, sizeof(*sks->sortRelation));
1505 *sks->sortRelation = Z_SortRelation_ascending;
1506 sks->caseSensitivity = (int *)odr_malloc (out, sizeof(*sks->caseSensitivity));
1507 *sks->caseSensitivity = Z_SortCase_caseSensitive;
1510 sks->which = Z_SortKeySpec_null;
1511 sks->u.null = odr_nullval ();
1513 sks->missingValueAction = NULL;
1516 for (i = 0; sort_flags[i]; i++)
1518 switch (sort_flags[i])
1523 *sks->sortRelation = Z_SortRelation_descending;
1528 *sks->sortRelation = Z_SortRelation_ascending;
1532 *sks->caseSensitivity = Z_SortCase_caseInsensitive;
1536 *sks->caseSensitivity = Z_SortCase_caseSensitive;
1541 if (!sksl->num_specs)
1543 printf ("Missing sort specifications\n");
1550 void display_term(Z_TermInfo *t)
1552 if (t->term->which == Z_Term_general)
1554 printf("%.*s (%d)\n", t->term->u.general->len, t->term->u.general->buf,
1555 t->globalOccurrences ? *t->globalOccurrences : -1);
1556 sprintf(last_scan, "%.*s", t->term->u.general->len,
1557 t->term->u.general->buf);
1560 printf("Term type not general.\n");
1563 void process_scanResponse(Z_ScanResponse *res)
1566 Z_Entry **entries = NULL;
1567 int num_entries = 0;
1569 printf("Received ScanResponse\n");
1570 print_refid (res->referenceId);
1571 printf("%d entries", *res->numberOfEntriesReturned);
1572 if (res->positionOfTerm)
1573 printf (", position=%d", *res->positionOfTerm);
1575 if (*res->scanStatus != Z_Scan_success)
1576 printf("Scan returned code %d\n", *res->scanStatus);
1580 if ((entries = res->entries->entries))
1581 num_entries = res->entries->num_entries;
1583 if (res->entries->which == Z_ListEntries_entries)
1585 entries = res->entries->u.entries->entries;
1586 num_entries = res->entries->u.entries->num_entries;
1589 for (i = 0; i < num_entries; i++)
1591 int pos_term = res->positionOfTerm ? *res->positionOfTerm : -1;
1592 if (entries[i]->which == Z_Entry_termInfo)
1594 printf("%c ", i + 1 == pos_term ? '*' : ' ');
1595 display_term(entries[i]->u.termInfo);
1598 display_diagrecs(&entries[i]->u.surrogateDiagnostic, 1);
1601 if (res->entries->nonsurrogateDiagnostics)
1602 display_diagrecs (res->entries->nonsurrogateDiagnostics,
1603 res->entries->num_nonsurrogateDiagnostics);
1605 if (res->entries->which == Z_ListEntries_nonSurrogateDiagnostics)
1606 display_diagrecs(&res->entries->
1607 u.nonSurrogateDiagnostics->diagRecs[0], 1);
1611 void process_sortResponse(Z_SortResponse *res)
1613 printf("Received SortResponse: status=");
1614 switch (*res->sortStatus)
1616 case Z_SortStatus_success:
1617 printf ("success"); break;
1618 case Z_SortStatus_partial_1:
1619 printf ("partial"); break;
1620 case Z_SortStatus_failure:
1621 printf ("failure"); break;
1623 printf ("unknown (%d)", *res->sortStatus);
1626 print_refid (res->referenceId);
1628 if (res->diagnostics)
1629 display_diagrecs(res->diagnostics,
1630 res->num_diagnostics);
1632 if (res->diagnostics)
1633 display_diagrecs(res->diagnostics->diagRecs,
1634 res->diagnostics->num_diagRecs);
1638 int cmd_sort_generic(char *arg, int newset)
1642 printf("Session not initialized yet\n");
1645 if (!ODR_MASK_GET(session->options, Z_Options_sort))
1647 printf("Target doesn't support sort\n");
1652 if (send_sortrequest(arg, newset) < 0)
1659 int cmd_sort(char *arg)
1661 return cmd_sort_generic (arg, 0);
1664 int cmd_sort_newset (char *arg)
1666 return cmd_sort_generic (arg, 1);
1669 int cmd_scan(char *arg)
1673 printf("Session not initialized yet\n");
1676 if (!ODR_MASK_GET(session->options, Z_Options_scan))
1678 printf("Target doesn't support scan\n");
1683 if (send_scanrequest(arg, 5, 20) < 0)
1687 if (send_scanrequest(last_scan, 1, 20) < 0)
1692 int cmd_format(char *arg)
1696 printf("Usage: format <recordsyntax>\n");
1699 recordsyntax = oid_getvalbyname (arg);
1700 if (recordsyntax == VAL_NONE)
1702 printf ("unknown record syntax\n");
1708 int cmd_elements(char *arg)
1710 static Z_ElementSetNames esn;
1711 static char what[100];
1715 printf("Usage: elements <esn>\n");
1719 esn.which = Z_ElementSetNames_generic;
1720 esn.u.generic = what;
1721 elementSetNames = &esn;
1725 int cmd_attributeset(char *arg)
1731 printf("Usage: attributeset <setname>\n");
1734 sscanf(arg, "%s", what);
1735 if (p_query_attset (what))
1737 printf("Unknown attribute set name\n");
1743 int cmd_querytype (char *arg)
1745 if (!strcmp (arg, "ccl"))
1746 queryType = QueryType_CCL;
1747 else if (!strcmp (arg, "prefix"))
1748 queryType = QueryType_Prefix;
1750 else if (!strcmp (arg, "ccl2rpn") || !strcmp (arg, "cclrpn"))
1751 queryType = QueryType_CCL2RPN;
1755 printf ("Querytype must be one of:\n");
1756 printf (" prefix - Prefix query\n");
1757 printf (" ccl - CCL query\n");
1759 printf (" ccl2rpn - CCL query converted to RPN\n");
1766 int cmd_refid (char *arg)
1772 refid = (char *) xmalloc (strlen(arg)+1);
1773 strcpy (refid, arg);
1778 int cmd_close(char *arg)
1785 apdu = zget_APDU(out, Z_APDU_close);
1786 req = apdu->u.close;
1787 *req->closeReason = Z_Close_finished;
1789 printf("Sent close request.\n");
1794 static void initialize(void)
1800 if (!(out = odr_createmem(ODR_ENCODE)) ||
1801 !(in = odr_createmem(ODR_DECODE)) ||
1802 !(print = odr_createmem(ODR_PRINT)))
1804 fprintf(stderr, "failed to allocate ODR streams\n");
1807 setvbuf(stdout, 0, _IONBF, 0);
1810 bibset = ccl_qual_mk ();
1811 inf = fopen ("default.bib", "r");
1814 ccl_qual_file (bibset, inf);
1818 cmd_base("Default");
1821 static int client(int wait)
1825 int (*fun)(char *arg);
1828 {"open", cmd_open, "('tcp'|'osi')':'[<tsel>'/']<host>[':'<port>]"},
1829 {"quit", cmd_quit, ""},
1830 {"find", cmd_find, "<query>"},
1831 {"delete", cmd_delete, "<setname>"},
1832 {"base", cmd_base, "<base-name>"},
1833 {"show", cmd_show, "<rec#>['+'<#recs>['+'<setname>]]"},
1834 {"scan", cmd_scan, "<term>"},
1835 {"sort", cmd_sort, "<sortkey> <flag> <sortkey> <flag> ..."},
1836 {"sort+", cmd_sort_newset, "<sortkey> <flag> <sortkey> <flag> ..."},
1837 {"authentication", cmd_authentication, "<acctstring>"},
1838 {"lslb", cmd_lslb, "<largeSetLowerBound>"},
1839 {"ssub", cmd_ssub, "<smallSetUpperBound>"},
1840 {"mspn", cmd_mspn, "<mediumSetPresentNumber>"},
1841 {"status", cmd_status, ""},
1842 {"setnames", cmd_setnames, ""},
1843 {"cancel", cmd_cancel, ""},
1844 {"format", cmd_format, "<recordsyntax>"},
1845 {"elements", cmd_elements, "<elementSetName>"},
1846 {"close", cmd_close, ""},
1847 {"attributeset", cmd_attributeset, "<attrset>"},
1848 {"querytype", cmd_querytype, "<type>"},
1849 {"refid", cmd_refid, "<id>"},
1850 {"itemorder", cmd_itemorder, "<item>"},
1854 int netbufferlen = 0;
1864 char line[1024], word[1024], arg[1024];
1870 FD_SET(cs_fileno(conn), &input);
1871 if ((res = select(20, &input, 0, 0, 0)) < 0)
1878 if (!wait && FD_ISSET(0, &input))
1883 /* quick & dirty way to get a command line. */
1885 if (!fgets(line, 1023, stdin))
1887 if ((end_p = strchr (line, '\n')))
1889 if ((res = sscanf(line, "%s %[^;]", word, arg)) <= 0)
1891 strcpy(word, last_cmd);
1896 strcpy(last_cmd, word);
1897 for (i = 0; cmd[i].cmd; i++)
1898 if (!strncmp(cmd[i].cmd, word, strlen(word)))
1900 res = (*cmd[i].fun)(arg);
1903 if (!cmd[i].cmd) /* dump our help-screen */
1905 printf("Unknown command: %s.\n", word);
1906 printf("Currently recognized commands:\n");
1907 for (i = 0; cmd[i].cmd; i++)
1908 printf(" %s %s\n", cmd[i].cmd, cmd[i].ad);
1919 if (conn && FD_ISSET(cs_fileno(conn), &input))
1924 if ((res = cs_get(conn, &netbuffer, &netbufferlen)) < 0)
1931 printf("Target closed connection.\n");
1934 odr_reset(in); /* release APDU from last round */
1935 odr_setbuf(in, netbuffer, res, 0);
1936 if (!z_APDU(in, &apdu, 0))
1938 odr_perror(in, "Decoding incoming APDU");
1939 fprintf(stderr, "[Near %d]\n", odr_offset(in));
1940 fprintf(stderr, "Packet dump:\n---------\n");
1941 odr_dumpBER(stderr, netbuffer, res);
1942 fprintf(stderr, "---------\n");
1946 if (!z_APDU(print, &apdu, 0))
1948 odr_perror(print, "Failed to print incoming APDU");
1955 case Z_APDU_initResponse:
1956 process_initResponse(apdu->u.initResponse);
1958 case Z_APDU_searchResponse:
1959 process_searchResponse(apdu->u.searchResponse);
1961 case Z_APDU_scanResponse:
1962 process_scanResponse(apdu->u.scanResponse);
1964 case Z_APDU_presentResponse:
1965 print_refid (apdu->u.presentResponse->referenceId);
1967 *apdu->u.presentResponse->numberOfRecordsReturned;
1968 if (apdu->u.presentResponse->records)
1969 display_records(apdu->u.presentResponse->records);
1971 printf("No records.\n");
1972 printf ("nextResultSetPosition = %d\n",
1973 *apdu->u.presentResponse->nextResultSetPosition);
1975 case Z_APDU_sortResponse:
1976 process_sortResponse(apdu->u.sortResponse);
1978 case Z_APDU_extendedServicesResponse:
1979 printf("Got extended services response\n");
1980 process_ESResponse(apdu->u.extendedServicesResponse);
1983 printf("Target has closed the association.\n");
1984 process_close(apdu->u.close);
1986 case Z_APDU_resourceControlRequest:
1987 process_resourceControlRequest
1988 (apdu->u.resourceControlRequest);
1990 case Z_APDU_deleteResultSetResponse:
1991 printf("Got deleteResultSetResponse status=%d\n",
1992 *apdu->u.deleteResultSetResponse->
1993 deleteOperationStatus);
1996 printf("Received unknown APDU type (%d).\n",
2001 while (conn && cs_more(conn));
2009 int main(int argc, char **argv)
2016 while ((ret = options("m:v:", argv, argc, &arg)) != -2)
2024 if (cmd_open (arg) == 2)
2029 if (!(marcdump = fopen (arg, "a")))
2036 log_init (log_mask_str(arg), "", NULL);
2039 fprintf (stderr, "Usage: %s [-m <marclog>] [<server-addr>]\n",
2049 return client (opened);