2 * Copyright (c) 1995-1999, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.77 1999-03-11 15:26:51 adam
8 * Fixed bug (introduced by previous commit).
10 * Revision 1.76 1999/03/11 11:12:07 adam
11 * Added GNU readline support. HTML display in client.
13 * Revision 1.75 1999/02/01 15:37:32 adam
14 * Fixed minor bug introduced by previous commit.
16 * Revision 1.74 1999/02/01 15:35:21 adam
19 * Revision 1.73 1998/10/20 13:55:43 quinn
20 * Fixed Scan bug in asn and client
22 * Revision 1.72 1998/10/20 13:23:15 quinn
23 * changed preferred pos to 1
25 * Revision 1.71 1998/10/20 13:21:43 adam
26 * Fixed scan response handler.
28 * Revision 1.70 1998/09/22 09:40:37 adam
29 * Minor changes in sort spec.
31 * Revision 1.69 1998/08/19 16:10:06 adam
32 * Changed som member names of DeleteResultSetRequest/Response.
34 * Revision 1.68 1998/07/20 12:37:06 adam
35 * Added 'delete <resultset>' command. Changed open command so that
36 * it reconnects if already connected.
38 * Revision 1.67 1998/06/09 13:55:06 adam
41 * Revision 1.66 1998/05/18 13:06:53 adam
42 * Changed the way attribute sets are handled by the retriaval module.
43 * Extended Explain conversion / schema.
44 * Modified server and client to work with ASN.1 compiled protocol handlers.
46 * Revision 1.65 1998/03/31 15:13:19 adam
47 * Development towards compiled ASN.1.
49 * Revision 1.64 1998/03/31 11:07:44 adam
50 * Furhter work on UNIverse resource report.
51 * Added Extended Services handling in frontend server.
53 * Revision 1.63 1998/03/05 08:05:10 adam
54 * Added a few casts to make C++ happy.
56 * Revision 1.62 1998/02/11 11:53:33 adam
57 * Changed code so that it compiles as C++.
59 * Revision 1.61 1998/02/10 11:03:06 adam
60 * Implemented command refid. Client prints reference-ID's, when present,
63 * Revision 1.60 1998/01/29 14:08:52 adam
64 * Better sort diagnostics.
66 * Revision 1.59 1998/01/29 13:17:56 adam
69 * Revision 1.58 1998/01/07 13:51:45 adam
72 * Revision 1.57 1998/01/07 12:58:22 adam
73 * Using fgets instead of gets.
75 * Revision 1.56 1997/11/05 09:18:31 adam
76 * The client handles records with no associated syntax.
78 * Revision 1.55 1997/10/31 12:20:08 adam
79 * Improved memory debugging for xmalloc/nmem.c. References to NMEM
80 * instead of ODR in n ESPEC-1 handling in source d1_espec.c.
81 * Bug fix: missing fclose in data1_read_espec1.
83 * Revision 1.54 1997/10/27 13:52:46 adam
84 * Header yaz-util includes all YAZ utility header files.
86 * Revision 1.53 1997/09/29 13:18:59 adam
87 * Added function, oid_ent_to_oid, to replace the function
88 * oid_getoidbyent, which is not thread safe.
90 * Revision 1.52 1997/09/29 07:20:31 adam
91 * Client code uses nmem_init.
93 * Revision 1.51 1997/09/26 09:41:55 adam
94 * Updated client to handle multiple diagnostics.
96 * Revision 1.50 1997/09/17 12:10:29 adam
99 * Revision 1.49 1997/09/04 13:45:17 adam
100 * Added UNImarc to list of available syntaxes.
102 * Revision 1.48 1997/09/01 08:48:44 adam
103 * New windows NT/95 port using MSV5.0. Only a few changes made
104 * to avoid warnings. Sub project created: client.dsp.
106 * Revision 1.47 1997/07/01 13:49:56 adam
107 * Take care of case when invalid target is specified on command line.
109 * Revision 1.46 1997/06/23 10:30:18 adam
110 * Added call to ccl_rpn_delete in search. Added ODR stream "out"
111 * as parameter to ccl_rpn_query to release RPN query.
113 * Revision 1.45 1997/05/14 06:53:29 adam
116 * Revision 1.44 1997/05/05 11:20:35 adam
117 * Client uses "options" utility and marc dump filename may be specified
118 * as an option (-m <file>).
120 * Revision 1.43 1996/11/08 11:03:26 adam
121 * Client accepts multiple database names.
123 * Revision 1.42 1996/10/08 10:44:57 quinn
124 * Resolved conflicts.
126 * Revision 1.41 1996/10/07 15:29:03 quinn
129 * Revision 1.40 1996/08/29 14:19:34 quinn
130 * Fixed conflict (CVS)
132 * Revision 1.39 1996/08/27 10:43:22 quinn
133 * Made select() optional
135 * Revision 1.38 1996/08/12 14:09:11 adam
136 * Default prefix query attribute set defined by using p_query_attset.
138 * Revision 1.37 1996/07/06 19:58:29 quinn
139 * System headerfiles gathered in yconfig
141 * Revision 1.36 1996/06/10 08:53:47 quinn
144 * Revision 1.35 1996/06/03 09:45:50 quinn
145 * Added display of OIDs in the GRS routine.
147 * Revision 1.34 1996/05/09 07:26:49 quinn
148 * *** empty log message ***
150 * Revision 1.33 1996/05/09 07:25:22 quinn
153 * Revision 1.32 1996/03/15 11:05:33 adam
154 * The user can set the preferred query type (prefix, ccl, ..) with the
157 * Revision 1.31 1996/02/20 12:51:54 quinn
158 * Fixed problems with EXTERNAL.
160 * Revision 1.30 1996/02/12 18:18:09 quinn
163 * Revision 1.29 1996/01/02 08:57:25 quinn
164 * Changed enums in the ASN.1 .h files to #defines. Changed oident.class to oclass
166 * Revision 1.28 1995/12/14 11:09:31 quinn
167 * Added Explain record syntax to the format command.
169 * Revision 1.27 1995/12/12 16:37:02 quinn
170 * Added destroy element to data1_node.
172 * Revision 1.26 1995/12/12 14:11:00 quinn
175 * Revision 1.25 1995/11/13 09:27:22 quinn
176 * Fiddling with the variant stuff.
178 * Revision 1.24 1995/10/30 12:41:13 quinn
179 * Added hostname lookup for server.
181 * Revision 1.23 1995/10/18 16:12:30 quinn
182 * Better diagnostics.
184 * Revision 1.22 1995/10/11 14:49:12 quinn
187 * Revision 1.21 1995/09/29 17:01:47 quinn
190 * Revision 1.20 1995/08/29 14:24:13 quinn
191 * Added second half of close-handshake
193 * Revision 1.19 1995/08/29 11:17:28 quinn
194 * Added code to receive close
196 * Revision 1.18 1995/08/28 12:21:27 quinn
197 * Client can now ask for simple element set names.
199 * Revision 1.17 1995/08/17 12:45:02 quinn
200 * Fixed minor problems with GRS-1. Added support in c&s.
202 * Revision 1.16 1995/08/15 12:00:04 quinn
205 * Revision 1.15 1995/06/22 09:28:03 quinn
206 * Fixed bug in SUTRS processing.
208 * Revision 1.14 1995/06/19 12:37:41 quinn
211 * Revision 1.13 1995/06/16 10:29:11 quinn
212 * *** empty log message ***
214 * Revision 1.12 1995/06/15 07:44:57 quinn
217 * Revision 1.11 1995/06/14 15:26:40 quinn
218 * *** empty log message ***
220 * Revision 1.10 1995/06/06 14:56:58 quinn
221 * Better diagnostics.
223 * Revision 1.9 1995/06/06 08:15:19 quinn
226 * Revision 1.8 1995/06/05 10:52:22 quinn
229 * Revision 1.7 1995/06/02 09:50:09 quinn
232 * Revision 1.6 1995/05/31 08:29:21 quinn
233 * Nothing significant.
235 * Revision 1.5 1995/05/29 08:10:47 quinn
236 * Moved oid.c to util.
238 * Revision 1.4 1995/05/22 15:30:13 adam
239 * Client uses prefix query notation.
241 * Revision 1.3 1995/05/22 15:06:53 quinn
242 * *** empty log message ***
244 * Revision 1.2 1995/05/22 14:56:40 quinn
245 * *** empty log message ***
247 * Revision 1.1 1995/05/22 11:30:31 quinn
248 * Added prettier client.
254 * This is the obligatory little toy client, whose primary purpose is
255 * to illustrate the use of the YAZ service-level API.
263 #include <yaz-util.h>
265 #include <comstack.h>
272 #include <marcdisp.h>
273 #include <diagbib1.h>
281 #if HAVE_READLINE_READLINE_H
282 #include <readline/readline.h>
284 #if HAVE_READLINE_HISTORY_H
285 #include <readline/history.h>
288 #define C_PROMPT "Z> "
290 static ODR out, in, print; /* encoding and decoding streams */
291 static COMSTACK conn = 0; /* our z-association */
292 static Z_IdAuthentication *auth = 0; /* our current auth definition */
293 static char *databaseNames[128];
294 static int num_databaseNames = 0;
295 static int setnumber = 0; /* current result set number */
296 static int smallSetUpperBound = 0;
297 static int largeSetLowerBound = 1;
298 static int mediumSetPresentNumber = 0;
299 static Z_ElementSetNames *elementSetNames = 0;
300 static int setno = 1; /* current set offset */
301 static enum oid_proto protocol = PROTO_Z3950; /* current app protocol */
302 static enum oid_value recordsyntax = VAL_USMARC;
303 static int sent_close = 0;
304 static NMEM session_mem = NULL; /* memory handle for init-response */
305 static Z_InitResponse *session = 0; /* session parameters */
306 static char last_scan[512] = "0";
307 static char last_cmd[100] = "?";
308 static FILE *marcdump = 0;
309 static char *refid = NULL;
317 static QueryType queryType = QueryType_Prefix;
320 static CCL_bibset bibset; /* CCL bibset handle */
323 static void send_apdu(Z_APDU *a)
328 if (!z_APDU(out, &a, 0))
330 odr_perror(out, "Encoding APDU");
333 buf = odr_getbuf(out, &len, 0);
334 if (cs_put(conn, buf, len) < 0)
336 fprintf(stderr, "cs_put: %s", cs_errmsg(cs_errno(conn)));
339 odr_reset(out); /* release the APDU structure */
342 static void print_refid (Z_ReferenceId *id)
346 printf ("ReferenceId: '%.*s'\n", id->len, id->buf);
350 static Z_ReferenceId *set_refid (ODR out)
355 id = (Z_ReferenceId *) odr_malloc (out, sizeof(*id));
356 id->size = id->len = strlen(refid);
357 id->buf = (unsigned char *) odr_malloc (out, id->len);
358 memcpy (id->buf, refid, id->len);
362 /* INIT SERVICE ------------------------------- */
364 static void send_initRequest()
366 Z_APDU *apdu = zget_APDU(out, Z_APDU_initRequest);
367 Z_InitRequest *req = apdu->u.initRequest;
369 ODR_MASK_SET(req->options, Z_Options_search);
370 ODR_MASK_SET(req->options, Z_Options_present);
371 ODR_MASK_SET(req->options, Z_Options_namedResultSets);
372 ODR_MASK_SET(req->options, Z_Options_triggerResourceCtrl);
373 ODR_MASK_SET(req->options, Z_Options_scan);
374 ODR_MASK_SET(req->options, Z_Options_sort);
375 ODR_MASK_SET(req->options, Z_Options_extendedServices);
376 ODR_MASK_SET(req->options, Z_Options_delSet);
378 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_1);
379 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_2);
380 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_3);
382 *req->maximumRecordSize = 1024*1024;
384 req->idAuthentication = auth;
387 printf("Sent initrequest.\n");
390 static int process_initResponse(Z_InitResponse *res)
392 /* save session parameters for later use */
393 session_mem = odr_extract_mem(in);
397 printf("Connection rejected by target.\n");
399 printf("Connection accepted by target.\n");
400 if (res->implementationId)
401 printf("ID : %s\n", res->implementationId);
402 if (res->implementationName)
403 printf("Name : %s\n", res->implementationName);
404 if (res->implementationVersion)
405 printf("Version: %s\n", res->implementationVersion);
406 if (res->userInformationField)
408 printf("UserInformationfield:\n");
409 if (!z_External(print, (Z_External**)&res-> userInformationField,
412 odr_perror(print, "Printing userinfo\n");
415 if (res->userInformationField->which == Z_External_octet)
417 printf("Guessing visiblestring:\n");
418 printf("'%s'\n", res->userInformationField->u. octet_aligned->buf);
425 static int cmd_base(char *arg)
432 printf("Usage: base <database> <database> ...\n");
435 for (i = 0; i<num_databaseNames; i++)
436 xfree (databaseNames[i]);
437 num_databaseNames = 0;
440 if (!(cp = strchr(arg, ' ')))
441 cp = arg + strlen(arg);
444 databaseNames[num_databaseNames] = (char *)xmalloc (1 + cp - arg);
445 memcpy (databaseNames[num_databaseNames], arg, cp - arg);
446 databaseNames[num_databaseNames++][cp - arg] = '\0';
455 int cmd_open(char *arg)
458 char type[100], addr[100], base[100];
463 printf("Already connected.\n");
469 nmem_destroy (session_mem);
474 if (!*arg || sscanf(arg, "%[^:]:%[^/]/%s", type, addr, base) < 2)
476 fprintf(stderr, "Usage: open (osi|tcp) ':' [tsel '/']host[':'port]\n");
481 if (!strcmp(type, "tcp"))
484 protocol = PROTO_Z3950;
488 if (!strcmp(type, "osi"))
496 fprintf(stderr, "Bad type: %s\n", type);
499 if (!(conn = cs_create(t, 1, protocol)))
504 if (!(add = cs_straddr(conn, addr)))
509 printf("Connecting...");
511 if (cs_connect(conn, add) < 0)
523 int cmd_authentication(char *arg)
525 static Z_IdAuthentication au;
526 static char open[256];
530 printf("Auth field set to null\n");
535 au.which = Z_IdAuthentication_open;
541 /* SEARCH SERVICE ------------------------------ */
543 static void display_variant(Z_Variant *v, int level)
547 for (i = 0; i < v->num_triples; i++)
549 printf("%*sclass=%d,type=%d", level * 4, "", *v->triples[i]->zclass,
550 *v->triples[i]->type);
551 if (v->triples[i]->which == Z_Triple_internationalString)
552 printf(",value=%s\n", v->triples[i]->value.internationalString);
558 static void display_grs1(Z_GenericRecord *r, int level)
564 for (i = 0; i < r->num_elements; i++)
568 printf("%*s", level * 4, "");
572 printf("%d,", *t->tagType);
575 if (t->tagValue->which == Z_StringOrNumeric_numeric)
576 printf("%d) ", *t->tagValue->u.numeric);
578 printf("%s) ", t->tagValue->u.string);
579 if (t->content->which == Z_ElementData_subtree)
582 display_grs1(t->content->u.subtree, level+1);
584 else if (t->content->which == Z_ElementData_string)
585 printf("%s\n", t->content->u.string);
586 else if (t->content->which == Z_ElementData_numeric)
587 printf("%d\n", *t->content->u.numeric);
588 else if (t->content->which == Z_ElementData_oid)
590 int *ip = t->content->u.oid;
593 if ((oent = oid_getentbyoid(t->content->u.oid)))
594 printf("OID: %s\n", oent->desc);
598 while (ip && *ip >= 0)
599 printf(" %d", *(ip++));
603 else if (t->content->which == Z_ElementData_noDataRequested)
604 printf("[No data requested]\n");
605 else if (t->content->which == Z_ElementData_elementEmpty)
606 printf("[Element empty]\n");
607 else if (t->content->which == Z_ElementData_elementNotThere)
608 printf("[Element not there]\n");
611 if (t->appliedVariant)
612 display_variant(t->appliedVariant, level+1);
613 if (t->metaData && t->metaData->supportedVariants)
617 printf("%*s---- variant list\n", (level+1)*4, "");
618 for (c = 0; c < t->metaData->num_supportedVariants; c++)
620 printf("%*svariant #%d\n", (level+1)*4, "", c);
621 display_variant(t->metaData->supportedVariants[c], level + 2);
627 static void display_record(Z_DatabaseRecord *p)
629 Z_External *r = (Z_External*) p;
630 oident *ent = oid_getentbyoid(r->direct_reference);
633 * Tell the user what we got.
635 if (r->direct_reference)
637 printf("Record type: ");
639 printf("%s\n", ent->desc);
640 else if (!odr_oid(print, &r->direct_reference, 0))
642 odr_perror(print, "print oid");
646 /* Check if this is a known, ASN.1 type tucked away in an octet string */
647 if (ent && r->which == Z_External_octet)
649 Z_ext_typeent *type = z_ext_getentbyref(ent->value);
655 * Call the given decoder to process the record.
657 odr_setbuf(in, (char*)p->u.octet_aligned->buf,
658 p->u.octet_aligned->len, 0);
659 if (!(*type->fun)(in, (char **)&rr, 0))
661 odr_perror(in, "Decoding constructed record.");
662 fprintf(stderr, "[Near %d]\n", odr_offset(in));
663 fprintf(stderr, "Packet dump:\n---------\n");
664 odr_dumpBER(stderr, (char*)p->u.octet_aligned->buf,
665 p->u.octet_aligned->len);
666 fprintf(stderr, "---------\n");
670 * Note: we throw away the original, BER-encoded record here.
671 * Do something else with it if you want to keep it.
673 r->u.sutrs = (Z_SUTRS *) rr; /* we don't actually check the type here. */
674 r->which = type->what;
677 if (ent && ent->value == VAL_SOIF)
678 printf("%.*s", r->u.octet_aligned->len, r->u.octet_aligned->buf);
679 else if (r->which == Z_External_octet && p->u.octet_aligned->len)
681 const char *octet_buf = (char*)p->u.octet_aligned->buf;
682 if (ent->value == VAL_TEXT_XML || ent->value == VAL_APPLICATION_XML ||
683 ent->value == VAL_HTML)
686 for (i = 0; i<p->u.octet_aligned->len; i++)
687 if (octet_buf[i] > 126 || octet_buf[i] < 7)
688 printf ("<%02X>", octet_buf[i]);
690 fputc (octet_buf[i], stdout);
694 marc_display (octet_buf, NULL);
696 fwrite (octet_buf, 1, p->u.octet_aligned->len, marcdump);
698 else if (ent && ent->value == VAL_SUTRS)
700 if (r->which != Z_External_sutrs)
702 printf("Expecting single SUTRS type for SUTRS.\n");
705 printf("%.*s", r->u.sutrs->len, r->u.sutrs->buf);
707 else if (ent && ent->value == VAL_GRS1)
709 if (r->which != Z_External_grs1)
711 printf("Expecting single GRS type for GRS.\n");
714 display_grs1(r->u.grs1, 0);
718 printf("Unknown record representation.\n");
719 if (!z_External(print, &r, 0))
721 odr_perror(print, "Printing external");
728 static void display_diagrecs(Z_DiagRec **pp, int num)
732 Z_DefaultDiagFormat *r;
734 printf("Diagnostic message(s) from database:\n");
735 for (i = 0; i<num; i++)
737 Z_DiagRec *p = pp[i];
738 if (p->which != Z_DiagRec_defaultFormat)
740 printf("Diagnostic record not in default format.\n");
744 r = p->u.defaultFormat;
745 if (!(ent = oid_getentbyoid(r->diagnosticSetId)) ||
746 ent->oclass != CLASS_DIAGSET || ent->value != VAL_BIB1)
747 printf("Missing or unknown diagset\n");
748 printf(" [%d] %s", *r->condition, diagbib1_str(*r->condition));
752 case Z_DefaultDiagFormat_v2Addinfo:
753 printf (" -- v2 addinfo '%s'\n", r->u.v2Addinfo);
755 case Z_DefaultDiagFormat_v3Addinfo:
756 printf (" -- v3 addinfo '%s'\n", r->u.v3Addinfo);
760 if (r->addinfo && *r->addinfo)
761 printf(" -- '%s'\n", r->addinfo);
769 static void display_nameplusrecord(Z_NamePlusRecord *p)
772 printf("[%s]", p->databaseName);
773 if (p->which == Z_NamePlusRecord_surrogateDiagnostic)
774 display_diagrecs(&p->u.surrogateDiagnostic, 1);
776 display_record(p->u.databaseRecord);
779 static void display_records(Z_Records *p)
783 if (p->which == Z_Records_NSD)
786 Z_DiagRec dr, *dr_p = &dr;
787 dr.which = Z_DiagRec_defaultFormat;
788 dr.u.defaultFormat = p->u.nonSurrogateDiagnostic;
789 display_diagrecs (&dr_p, 1);
791 display_diagrecs (&p->u.nonSurrogateDiagnostic, 1);
794 else if (p->which == Z_Records_multipleNSD)
795 display_diagrecs (p->u.multipleNonSurDiagnostics->diagRecs,
796 p->u.multipleNonSurDiagnostics->num_diagRecs);
799 printf("Records: %d\n", p->u.databaseOrSurDiagnostics->num_records);
800 for (i = 0; i < p->u.databaseOrSurDiagnostics->num_records; i++)
801 display_nameplusrecord(p->u.databaseOrSurDiagnostics->records[i]);
805 static int send_deleteResultSetRequest(char *arg)
807 Z_APDU *apdu = zget_APDU(out, Z_APDU_deleteResultSetRequest);
808 Z_DeleteResultSetRequest *req = apdu->u.deleteResultSetRequest;
810 req->referenceId = set_refid (out);
812 req->num_resultSetList = 1;
813 req->resultSetList = (char **)
814 odr_malloc (out, sizeof(*req->resultSetList));
815 *req->resultSetList = arg;
816 req->deleteFunction = (int *)
817 odr_malloc (out, sizeof(*req->deleteFunction));
818 *req->deleteFunction = Z_DeleteRequest_list;
821 printf("Sent deleteResultSetRequest.\n");
825 static int send_searchRequest(char *arg)
827 Z_APDU *apdu = zget_APDU(out, Z_APDU_searchRequest);
828 Z_SearchRequest *req = apdu->u.searchRequest;
832 struct ccl_rpn_node *rpn = NULL;
837 Z_RPNQuery *RPNquery;
841 if (queryType == QueryType_CCL2RPN)
843 rpn = ccl_find_str(bibset, arg, &error, &pos);
846 printf("CCL ERROR: %s\n", ccl_err_msg(error));
851 req->referenceId = set_refid (out);
852 if (!strcmp(arg, "@big")) /* strictly for troublemaking */
854 static unsigned char big[2100];
857 /* send a very big referenceid to test transport stack etc. */
858 memset(big, 'A', 2100);
859 bigo.len = bigo.size = 2100;
861 req->referenceId = &bigo;
866 sprintf(setstring, "%d", ++setnumber);
867 req->resultSetName = setstring;
869 *req->smallSetUpperBound = smallSetUpperBound;
870 *req->largeSetLowerBound = largeSetLowerBound;
871 *req->mediumSetPresentNumber = mediumSetPresentNumber;
872 if (smallSetUpperBound > 0 || (largeSetLowerBound > 1 &&
873 mediumSetPresentNumber > 0))
877 prefsyn.proto = protocol;
878 prefsyn.oclass = CLASS_RECSYN;
879 prefsyn.value = recordsyntax;
880 req->preferredRecordSyntax =
881 odr_oiddup(out, oid_ent_to_oid(&prefsyn, oid));
882 req->smallSetElementSetNames =
883 req->mediumSetElementSetNames = elementSetNames;
885 req->num_databaseNames = num_databaseNames;
886 req->databaseNames = databaseNames;
892 case QueryType_Prefix:
893 query.which = Z_Query_type_1;
894 RPNquery = p_query_rpn (out, protocol, arg);
897 printf("Prefix query error\n");
900 query.u.type_1 = RPNquery;
903 query.which = Z_Query_type_2;
904 query.u.type_2 = &ccl_query;
905 ccl_query.buf = (unsigned char*) arg;
906 ccl_query.len = strlen(arg);
909 case QueryType_CCL2RPN:
910 query.which = Z_Query_type_1;
911 RPNquery = ccl_rpn_query(out, rpn);
913 bib1.proto = protocol;
914 bib1.oclass = CLASS_ATTSET;
915 bib1.value = VAL_BIB1;
916 RPNquery->attributeSetId = oid_ent_to_oid(&bib1, oid);
917 query.u.type_1 = RPNquery;
918 ccl_rpn_delete (rpn);
922 printf ("Unsupported query type\n");
927 printf("Sent searchRequest.\n");
931 static int process_searchResponse(Z_SearchResponse *res)
933 printf ("Received SearchResponse.\n");
934 print_refid (res->referenceId);
935 if (*res->searchStatus)
936 printf("Search was a success.\n");
938 printf("Search was a bloomin' failure.\n");
939 printf("Number of hits: %d, setno %d\n",
940 *res->resultCount, setnumber);
941 printf("records returned: %d\n",
942 *res->numberOfRecordsReturned);
943 setno += *res->numberOfRecordsReturned;
945 display_records(res->records);
949 static void print_level(int iLevel)
952 for (i = 0; i < iLevel * 4; i++)
956 static void print_int(int iLevel, const char *pTag, int *pInt)
961 printf("%s: %d\n", pTag, *pInt);
965 static void print_string(int iLevel, const char *pTag, const char *pString)
970 printf("%s: %s\n", pTag, pString);
974 static void print_oid(int iLevel, const char *pTag, Odr_oid *pOid)
982 for (; *pInt != -1; pInt++)
983 printf(" %d", *pInt);
988 static void print_referenceId(int iLevel, Z_ReferenceId *referenceId)
990 if (referenceId != NULL)
995 printf("Ref Id (%d, %d): ", referenceId->len, referenceId->size);
996 for (i = 0; i < referenceId->len; i++)
997 printf("%c", referenceId->buf[i]);
1002 static void print_string_or_numeric(int iLevel, const char *pTag, Z_StringOrNumeric *pStringNumeric)
1004 if (pStringNumeric != NULL)
1006 switch (pStringNumeric->which)
1008 case Z_StringOrNumeric_string:
1009 print_string(iLevel, pTag, pStringNumeric->u.string);
1012 case Z_StringOrNumeric_numeric:
1013 print_int(iLevel, pTag, pStringNumeric->u.numeric);
1017 print_level(iLevel);
1018 printf("%s: valid type for Z_StringOrNumeric\n", pTag);
1024 static void print_universe_report_duplicate(int iLevel, Z_UniverseReportDuplicate *pUniverseReportDuplicate)
1026 if (pUniverseReportDuplicate != NULL)
1028 print_level(iLevel);
1029 printf("Universe Report Duplicate: \n");
1031 print_string_or_numeric(iLevel, "Hit No", pUniverseReportDuplicate->hitno);
1035 static void print_universe_report_hits(int iLevel, Z_UniverseReportHits *pUniverseReportHits)
1037 if (pUniverseReportHits != NULL)
1039 print_level(iLevel);
1040 printf("Universe Report Hits: \n");
1042 print_string_or_numeric(iLevel, "Database", pUniverseReportHits->database);
1043 print_string_or_numeric(iLevel, "Hits", pUniverseReportHits->hits);
1047 static void print_universe_report(int iLevel, Z_UniverseReport *pUniverseReport)
1049 if (pUniverseReport != NULL)
1051 print_level(iLevel);
1052 printf("Universe Report: \n");
1054 print_int(iLevel, "Total Hits", pUniverseReport->totalHits);
1055 switch (pUniverseReport->which)
1057 case Z_UniverseReport_databaseHits:
1058 print_universe_report_hits(iLevel, pUniverseReport->u.databaseHits);
1061 case Z_UniverseReport_duplicate:
1062 print_universe_report_duplicate(iLevel, pUniverseReport->u.duplicate);
1066 print_level(iLevel);
1067 printf("Type: %d\n", pUniverseReport->which);
1073 static void print_external(int iLevel, Z_External *pExternal)
1075 if (pExternal != NULL)
1077 print_level(iLevel);
1078 printf("External: \n");
1080 print_oid(iLevel, "Direct Reference", pExternal->direct_reference);
1081 print_int(iLevel, "InDirect Reference", pExternal->indirect_reference);
1082 print_string(iLevel, "Descriptor", pExternal->descriptor);
1083 switch (pExternal->which)
1085 case Z_External_universeReport:
1086 print_universe_report(iLevel, pExternal->u.universeReport);
1090 print_level(iLevel);
1091 printf("Type: %d\n", pExternal->which);
1097 static int process_resourceControlRequest (Z_ResourceControlRequest *req)
1099 printf ("Received ResourceControlRequest.\n");
1100 print_referenceId(1, req->referenceId);
1101 print_int(1, "Suspended Flag", req->suspendedFlag);
1102 print_int(1, "Partial Results Available", req->partialResultsAvailable);
1103 print_int(1, "Response Required", req->responseRequired);
1104 print_int(1, "Triggered Request Flag", req->triggeredRequestFlag);
1105 print_external(1, req->resourceReport);
1109 void process_ESResponse(Z_ExtendedServicesResponse *res)
1111 printf("process_ESResponse\n");
1114 static Z_External *CreateItemOrderExternal(int itemno)
1116 Z_External *r = (Z_External *) odr_malloc(out, sizeof(Z_External));
1117 oident ItemOrderRequest;
1119 ItemOrderRequest.proto = PROTO_Z3950;
1120 ItemOrderRequest.oclass = CLASS_EXTSERV;
1121 ItemOrderRequest.value = VAL_ITEMORDER;
1123 r->direct_reference = odr_oiddup(out,oid_getoidbyent(&ItemOrderRequest));
1124 r->indirect_reference = (int *) odr_malloc(out,sizeof(int));
1125 *r->indirect_reference = 0;
1127 r->descriptor = "Extended services item order";
1129 r->which = Z_External_itemOrder;
1131 r->u.itemOrder = (Z_ItemOrder *) odr_malloc(out,sizeof(Z_ItemOrder));
1132 memset(r->u.itemOrder, 0, sizeof(Z_ItemOrder));
1134 r->u.itemOrder->which=Z_IOItemOrder_esRequest;
1136 r->u.itemOrder->which=Z_ItemOrder_esRequest;
1139 r->u.itemOrder->u.esRequest = (Z_IORequest *)
1140 odr_malloc(out,sizeof(Z_IORequest));
1141 memset(r->u.itemOrder->u.esRequest, 0, sizeof(Z_IORequest));
1143 r->u.itemOrder->u.esRequest->toKeep = (Z_IOOriginPartToKeep *)
1144 odr_malloc(out,sizeof(Z_IOOriginPartToKeep));
1145 memset(r->u.itemOrder->u.esRequest->toKeep, 0, sizeof(Z_IOOriginPartToKeep));
1146 r->u.itemOrder->u.esRequest->notToKeep = (Z_IOOriginPartNotToKeep *)
1147 odr_malloc(out,sizeof(Z_IOOriginPartNotToKeep));
1148 memset(r->u.itemOrder->u.esRequest->notToKeep, 0, sizeof(Z_IOOriginPartNotToKeep));
1150 r->u.itemOrder->u.esRequest->toKeep->supplDescription = NULL;
1151 r->u.itemOrder->u.esRequest->toKeep->contact = NULL;
1152 r->u.itemOrder->u.esRequest->toKeep->addlBilling = NULL;
1154 r->u.itemOrder->u.esRequest->notToKeep->resultSetItem =
1155 (Z_IOResultSetItem *) odr_malloc(out, sizeof(Z_IOResultSetItem));
1156 memset(r->u.itemOrder->u.esRequest->notToKeep->resultSetItem, 0, sizeof(Z_IOResultSetItem));
1157 r->u.itemOrder->u.esRequest->notToKeep->resultSetItem->resultSetId = "1";
1159 r->u.itemOrder->u.esRequest->notToKeep->resultSetItem->item =
1160 (int *) odr_malloc(out, sizeof(int));
1161 *r->u.itemOrder->u.esRequest->notToKeep->resultSetItem->item = itemno;
1163 r->u.itemOrder->u.esRequest->notToKeep->itemRequest = NULL;
1167 /* II : Added to do DALI Item Order Extended services request */
1168 static int send_itemorder(char *arg)
1171 Z_APDU *apdu = zget_APDU(out, Z_APDU_extendedServicesRequest );
1172 Z_ExtendedServicesRequest *req = apdu->u.extendedServicesRequest;
1173 oident ItemOrderRequest;
1178 /* Set up item order request */
1180 /* Function being performed by this extended services request */
1181 req->function = (int *) odr_malloc(out, sizeof(int));
1182 *req->function = Z_ExtendedServicesRequest_create;
1184 /* Package type, Using protocol ILL ( But that's not in the oid.h file yet */
1185 /* create an object of class Extended Service, value Item Order */
1186 ItemOrderRequest.proto = PROTO_Z3950;
1187 ItemOrderRequest.oclass = CLASS_EXTSERV;
1188 ItemOrderRequest.value = VAL_ITEMORDER;
1189 req->packageType = odr_oiddup(out,oid_getoidbyent(&ItemOrderRequest));
1190 req->packageName = "1.Extendedserveq";
1192 /* ** taskSpecificParameters ** */
1193 req->taskSpecificParameters = CreateItemOrderExternal(itemno);
1195 /* waitAction - Create the ILL request and that's it */
1196 *req->waitAction = Z_ExtendedServicesRequest_wait;
1202 /* II : Added to do DALI Item Order Extended services request */
1203 static int cmd_itemorder(char *arg)
1205 printf("Item order request\n");
1208 send_itemorder(arg);
1212 static int cmd_find(char *arg)
1216 printf("Find what?\n");
1221 printf("Not connected yet\n");
1224 if (!send_searchRequest(arg))
1229 static int cmd_delete(char *arg)
1233 printf("Delete what?\n");
1238 printf("Not connected yet\n");
1241 if (!send_deleteResultSetRequest(arg))
1246 static int cmd_ssub(char *arg)
1248 if (!(smallSetUpperBound = atoi(arg)))
1253 static int cmd_lslb(char *arg)
1255 if (!(largeSetLowerBound = atoi(arg)))
1260 static int cmd_mspn(char *arg)
1262 if (!(mediumSetPresentNumber = atoi(arg)))
1267 static int cmd_status(char *arg)
1269 printf("smallSetUpperBound: %d\n", smallSetUpperBound);
1270 printf("largeSetLowerBound: %d\n", largeSetLowerBound);
1271 printf("mediumSetPresentNumber: %d\n", mediumSetPresentNumber);
1275 static int cmd_setnames(char *arg)
1279 printf("Set numbering enabled.\n");
1284 printf("Set numbering disabled.\n");
1290 /* PRESENT SERVICE ----------------------------- */
1292 static int send_presentRequest(char *arg)
1294 Z_APDU *apdu = zget_APDU(out, Z_APDU_presentRequest);
1295 Z_PresentRequest *req = apdu->u.presentRequest;
1296 Z_RecordComposition compo;
1301 char setstring[100];
1303 req->referenceId = set_refid (out);
1304 if ((p = strchr(arg, '+')))
1311 if (p && (p=strchr(p+1, '+')))
1313 strcpy (setstring, p+1);
1314 req->resultSetId = setstring;
1316 else if (setnumber >= 0)
1318 sprintf(setstring, "%d", setnumber);
1319 req->resultSetId = setstring;
1324 static Z_Range range;
1325 static Z_Range *rangep = ⦥
1326 req->num_ranges = 1;
1328 req->resultSetStartPoint = &setno;
1329 req->numberOfRecordsRequested = &nos;
1330 prefsyn.proto = protocol;
1331 prefsyn.oclass = CLASS_RECSYN;
1332 prefsyn.value = recordsyntax;
1333 req->preferredRecordSyntax = oid_ent_to_oid(&prefsyn, oid);
1335 if (elementSetNames)
1337 req->recordComposition = &compo;
1338 compo.which = Z_RecordComp_simple;
1339 compo.u.simple = elementSetNames;
1342 printf("Sent presentRequest (%d+%d).\n", setno, nos);
1346 void process_close(Z_Close *req)
1348 Z_APDU *apdu = zget_APDU(out, Z_APDU_close);
1349 Z_Close *res = apdu->u.close;
1351 static char *reasons[] =
1356 "cost limit reached",
1358 "security violation",
1365 printf("Reason: %s, message: %s\n", reasons[*req->closeReason],
1366 req->diagnosticInformation ? req->diagnosticInformation : "NULL");
1373 nmem_destroy (session_mem);
1380 *res->closeReason = Z_Close_finished;
1382 printf("Sent response.\n");
1387 static int cmd_show(char *arg)
1391 printf("Not connected yet\n");
1394 if (!send_presentRequest(arg))
1399 int cmd_quit(char *arg)
1401 printf("See you later, alligator.\n");
1406 int cmd_cancel(char *arg)
1408 Z_APDU *apdu = zget_APDU(out, Z_APDU_triggerResourceControlRequest);
1409 Z_TriggerResourceControlRequest *req =
1410 apdu->u.triggerResourceControlRequest;
1415 printf("Session not initialized yet\n");
1418 if (!ODR_MASK_GET(session->options, Z_Options_triggerResourceCtrl))
1420 printf("Target doesn't support cancel (trigger resource ctrl)\n");
1423 *req->requestedAction = Z_TriggerResourceCtrl_cancel;
1424 req->resultSetWanted = &rfalse;
1427 printf("Sent cancel request\n");
1431 int send_scanrequest(char *string, int pp, int num)
1433 Z_APDU *apdu = zget_APDU(out, Z_APDU_scanRequest);
1434 Z_ScanRequest *req = apdu->u.scanRequest;
1436 if (!(req->termListAndStartPoint =
1437 p_query_scan(out, protocol, &req->attributeSet, string)))
1439 printf("Prefix query error\n");
1442 req->referenceId = set_refid (out);
1443 req->num_databaseNames = num_databaseNames;
1444 req->databaseNames = databaseNames;
1445 req->numberOfTermsRequested = #
1446 req->preferredPositionInResponse = &pp;
1451 int send_sortrequest(char *arg, int newset)
1453 Z_APDU *apdu = zget_APDU(out, Z_APDU_sortRequest);
1454 Z_SortRequest *req = apdu->u.sortRequest;
1455 Z_SortKeySpecList *sksl = (Z_SortKeySpecList *)odr_malloc (out, sizeof(*sksl));
1457 char sort_string[32], sort_flags[32];
1463 sprintf (setstring, "%d", setnumber);
1465 sprintf (setstring, "default");
1467 req->referenceId = set_refid (out);
1470 req->num_inputResultSetNames = 1;
1471 req->inputResultSetNames = (Z_InternationalString **)
1472 odr_malloc (out, sizeof(*req->inputResultSetNames));
1473 req->inputResultSetNames[0] = (char *)
1474 odr_malloc (out, strlen(setstring)+1);
1475 strcpy (req->inputResultSetNames[0], setstring);
1477 req->inputResultSetNames =
1478 (Z_StringList *)odr_malloc (out, sizeof(*req->inputResultSetNames));
1479 req->inputResultSetNames->num_strings = 1;
1480 req->inputResultSetNames->strings =
1481 (char **)odr_malloc (out, sizeof(*req->inputResultSetNames->strings));
1482 req->inputResultSetNames->strings[0] =
1483 (char *)odr_malloc (out, strlen(setstring)+1);
1484 strcpy (req->inputResultSetNames->strings[0], setstring);
1487 if (newset && setnumber >= 0)
1488 sprintf (setstring, "%d", ++setnumber);
1490 req->sortedResultSetName = (char *)odr_malloc (out, strlen(setstring)+1);
1491 strcpy (req->sortedResultSetName, setstring);
1493 req->sortSequence = sksl;
1494 sksl->num_specs = 0;
1495 sksl->specs = (Z_SortKeySpec **)odr_malloc (out, sizeof(sksl->specs) * 20);
1497 bib1.proto = protocol;
1498 bib1.oclass = CLASS_ATTSET;
1499 bib1.value = VAL_BIB1;
1500 while ((sscanf (arg, "%31s %31s%n", sort_string, sort_flags, &off)) == 2
1504 char *sort_string_sep;
1505 Z_SortKeySpec *sks = (Z_SortKeySpec *)odr_malloc (out, sizeof(*sks));
1506 Z_SortKey *sk = (Z_SortKey *)odr_malloc (out, sizeof(*sk));
1509 sksl->specs[sksl->num_specs++] = sks;
1510 sks->sortElement = (Z_SortElement *)odr_malloc (out, sizeof(*sks->sortElement));
1511 sks->sortElement->which = Z_SortElement_generic;
1512 sks->sortElement->u.generic = sk;
1514 if ((sort_string_sep = strchr (sort_string, '=')))
1516 Z_AttributeElement *el = (Z_AttributeElement *)odr_malloc (out, sizeof(*el));
1517 sk->which = Z_SortKey_sortAttributes;
1518 sk->u.sortAttributes =
1519 (Z_SortAttributes *)odr_malloc (out, sizeof(*sk->u.sortAttributes));
1520 sk->u.sortAttributes->id = oid_ent_to_oid(&bib1, oid);
1521 sk->u.sortAttributes->list =
1522 (Z_AttributeList *)odr_malloc (out, sizeof(*sk->u.sortAttributes->list));
1523 sk->u.sortAttributes->list->num_attributes = 1;
1524 sk->u.sortAttributes->list->attributes =
1525 (Z_AttributeElement **)odr_malloc (out,
1526 sizeof(*sk->u.sortAttributes->list->attributes));
1527 sk->u.sortAttributes->list->attributes[0] = el;
1528 el->attributeSet = 0;
1529 el->attributeType = (int *)odr_malloc (out, sizeof(*el->attributeType));
1530 *el->attributeType = atoi (sort_string);
1531 el->which = Z_AttributeValue_numeric;
1532 el->value.numeric = (int *)odr_malloc (out, sizeof(*el->value.numeric));
1533 *el->value.numeric = atoi (sort_string_sep + 1);
1537 sk->which = Z_SortKey_sortField;
1538 sk->u.sortField = (char *)odr_malloc (out, strlen(sort_string)+1);
1539 strcpy (sk->u.sortField, sort_string);
1541 sks->sortRelation = (int *)odr_malloc (out, sizeof(*sks->sortRelation));
1542 *sks->sortRelation = Z_SortRelation_ascending;
1543 sks->caseSensitivity = (int *)odr_malloc (out, sizeof(*sks->caseSensitivity));
1544 *sks->caseSensitivity = Z_SortCase_caseSensitive;
1547 sks->which = Z_SortKeySpec_null;
1548 sks->u.null = odr_nullval ();
1550 sks->missingValueAction = NULL;
1553 for (i = 0; sort_flags[i]; i++)
1555 switch (sort_flags[i])
1560 *sks->sortRelation = Z_SortRelation_descending;
1565 *sks->sortRelation = Z_SortRelation_ascending;
1569 *sks->caseSensitivity = Z_SortCase_caseInsensitive;
1573 *sks->caseSensitivity = Z_SortCase_caseSensitive;
1578 if (!sksl->num_specs)
1580 printf ("Missing sort specifications\n");
1587 void display_term(Z_TermInfo *t)
1589 if (t->term->which == Z_Term_general)
1591 printf("%.*s (%d)\n", t->term->u.general->len, t->term->u.general->buf,
1592 t->globalOccurrences ? *t->globalOccurrences : -1);
1593 sprintf(last_scan, "%.*s", t->term->u.general->len,
1594 t->term->u.general->buf);
1597 printf("Term type not general.\n");
1600 void process_scanResponse(Z_ScanResponse *res)
1603 Z_Entry **entries = NULL;
1604 int num_entries = 0;
1606 printf("Received ScanResponse\n");
1607 print_refid (res->referenceId);
1608 printf("%d entries", *res->numberOfEntriesReturned);
1609 if (res->positionOfTerm)
1610 printf (", position=%d", *res->positionOfTerm);
1612 if (*res->scanStatus != Z_Scan_success)
1613 printf("Scan returned code %d\n", *res->scanStatus);
1616 if ((entries = res->entries->entries))
1617 num_entries = res->entries->num_entries;
1618 for (i = 0; i < num_entries; i++)
1620 int pos_term = res->positionOfTerm ? *res->positionOfTerm : -1;
1621 if (entries[i]->which == Z_Entry_termInfo)
1623 printf("%c ", i + 1 == pos_term ? '*' : ' ');
1624 display_term(entries[i]->u.termInfo);
1627 display_diagrecs(&entries[i]->u.surrogateDiagnostic, 1);
1629 if (res->entries->nonsurrogateDiagnostics)
1630 display_diagrecs (res->entries->nonsurrogateDiagnostics,
1631 res->entries->num_nonsurrogateDiagnostics);
1634 void process_sortResponse(Z_SortResponse *res)
1636 printf("Received SortResponse: status=");
1637 switch (*res->sortStatus)
1639 case Z_SortStatus_success:
1640 printf ("success"); break;
1641 case Z_SortStatus_partial_1:
1642 printf ("partial"); break;
1643 case Z_SortStatus_failure:
1644 printf ("failure"); break;
1646 printf ("unknown (%d)", *res->sortStatus);
1649 print_refid (res->referenceId);
1651 if (res->diagnostics)
1652 display_diagrecs(res->diagnostics,
1653 res->num_diagnostics);
1655 if (res->diagnostics)
1656 display_diagrecs(res->diagnostics->diagRecs,
1657 res->diagnostics->num_diagRecs);
1661 int cmd_sort_generic(char *arg, int newset)
1665 printf("Session not initialized yet\n");
1668 if (!ODR_MASK_GET(session->options, Z_Options_sort))
1670 printf("Target doesn't support sort\n");
1675 if (send_sortrequest(arg, newset) < 0)
1682 int cmd_sort(char *arg)
1684 return cmd_sort_generic (arg, 0);
1687 int cmd_sort_newset (char *arg)
1689 return cmd_sort_generic (arg, 1);
1692 int cmd_scan(char *arg)
1696 printf("Session not initialized yet\n");
1699 if (!ODR_MASK_GET(session->options, Z_Options_scan))
1701 printf("Target doesn't support scan\n");
1706 if (send_scanrequest(arg, 1, 20) < 0)
1710 if (send_scanrequest(last_scan, 1, 20) < 0)
1715 int cmd_format(char *arg)
1719 printf("Usage: format <recordsyntax>\n");
1722 recordsyntax = oid_getvalbyname (arg);
1723 if (recordsyntax == VAL_NONE)
1725 printf ("unknown record syntax\n");
1731 int cmd_elements(char *arg)
1733 static Z_ElementSetNames esn;
1734 static char what[100];
1738 printf("Usage: elements <esn>\n");
1742 esn.which = Z_ElementSetNames_generic;
1743 esn.u.generic = what;
1744 elementSetNames = &esn;
1748 int cmd_attributeset(char *arg)
1754 printf("Usage: attributeset <setname>\n");
1757 sscanf(arg, "%s", what);
1758 if (p_query_attset (what))
1760 printf("Unknown attribute set name\n");
1766 int cmd_querytype (char *arg)
1768 if (!strcmp (arg, "ccl"))
1769 queryType = QueryType_CCL;
1770 else if (!strcmp (arg, "prefix"))
1771 queryType = QueryType_Prefix;
1773 else if (!strcmp (arg, "ccl2rpn") || !strcmp (arg, "cclrpn"))
1774 queryType = QueryType_CCL2RPN;
1778 printf ("Querytype must be one of:\n");
1779 printf (" prefix - Prefix query\n");
1780 printf (" ccl - CCL query\n");
1782 printf (" ccl2rpn - CCL query converted to RPN\n");
1789 int cmd_refid (char *arg)
1795 refid = (char *) xmalloc (strlen(arg)+1);
1796 strcpy (refid, arg);
1801 int cmd_close(char *arg)
1808 apdu = zget_APDU(out, Z_APDU_close);
1809 req = apdu->u.close;
1810 *req->closeReason = Z_Close_finished;
1812 printf("Sent close request.\n");
1817 static void initialize(void)
1823 if (!(out = odr_createmem(ODR_ENCODE)) ||
1824 !(in = odr_createmem(ODR_DECODE)) ||
1825 !(print = odr_createmem(ODR_PRINT)))
1827 fprintf(stderr, "failed to allocate ODR streams\n");
1830 setvbuf(stdout, 0, _IONBF, 0);
1833 bibset = ccl_qual_mk ();
1834 inf = fopen ("default.bib", "r");
1837 ccl_qual_file (bibset, inf);
1841 cmd_base("Default");
1844 static int client(int wait)
1848 int (*fun)(char *arg);
1851 {"open", cmd_open, "('tcp'|'osi')':'[<tsel>'/']<host>[':'<port>]"},
1852 {"quit", cmd_quit, ""},
1853 {"find", cmd_find, "<query>"},
1854 {"delete", cmd_delete, "<setname>"},
1855 {"base", cmd_base, "<base-name>"},
1856 {"show", cmd_show, "<rec#>['+'<#recs>['+'<setname>]]"},
1857 {"scan", cmd_scan, "<term>"},
1858 {"sort", cmd_sort, "<sortkey> <flag> <sortkey> <flag> ..."},
1859 {"sort+", cmd_sort_newset, "<sortkey> <flag> <sortkey> <flag> ..."},
1860 {"authentication", cmd_authentication, "<acctstring>"},
1861 {"lslb", cmd_lslb, "<largeSetLowerBound>"},
1862 {"ssub", cmd_ssub, "<smallSetUpperBound>"},
1863 {"mspn", cmd_mspn, "<mediumSetPresentNumber>"},
1864 {"status", cmd_status, ""},
1865 {"setnames", cmd_setnames, ""},
1866 {"cancel", cmd_cancel, ""},
1867 {"format", cmd_format, "<recordsyntax>"},
1868 {"elements", cmd_elements, "<elementSetName>"},
1869 {"close", cmd_close, ""},
1870 {"attributeset", cmd_attributeset, "<attrset>"},
1871 {"querytype", cmd_querytype, "<type>"},
1872 {"refid", cmd_refid, "<id>"},
1873 {"itemorder", cmd_itemorder, "<item>"},
1877 int netbufferlen = 0;
1887 char line[1024], word[1024], arg[1024];
1893 FD_SET(cs_fileno(conn), &input);
1894 if ((res = select(20, &input, 0, 0, 0)) < 0)
1901 if (!wait && FD_ISSET(0, &input))
1906 #if HAVE_READLINE_READLINE_H
1908 line_in=readline(C_PROMPT);
1909 #if HAVE_READLINE_HISTORY_H
1910 if (line && *line_in)
1911 add_history(line_in);
1913 strcpy(line,line_in);
1919 if (!fgets(line, 1023, stdin))
1921 if ((end_p = strchr (line, '\n')))
1924 if ((res = sscanf(line, "%s %[^;]", word, arg)) <= 0)
1926 strcpy(word, last_cmd);
1931 strcpy(last_cmd, word);
1932 for (i = 0; cmd[i].cmd; i++)
1933 if (!strncmp(cmd[i].cmd, word, strlen(word)))
1935 res = (*cmd[i].fun)(arg);
1938 if (!cmd[i].cmd) /* dump our help-screen */
1940 printf("Unknown command: %s.\n", word);
1941 printf("Currently recognized commands:\n");
1942 for (i = 0; cmd[i].cmd; i++)
1943 printf(" %s %s\n", cmd[i].cmd, cmd[i].ad);
1953 if (conn && FD_ISSET(cs_fileno(conn), &input))
1958 if ((res = cs_get(conn, &netbuffer, &netbufferlen)) < 0)
1965 printf("Target closed connection.\n");
1968 odr_reset(in); /* release APDU from last round */
1969 odr_setbuf(in, netbuffer, res, 0);
1970 if (!z_APDU(in, &apdu, 0))
1972 odr_perror(in, "Decoding incoming APDU");
1973 fprintf(stderr, "[Near %d]\n", odr_offset(in));
1974 fprintf(stderr, "Packet dump:\n---------\n");
1975 odr_dumpBER(stderr, netbuffer, res);
1976 fprintf(stderr, "---------\n");
1980 if (!z_APDU(print, &apdu, 0))
1982 odr_perror(print, "Failed to print incoming APDU");
1989 case Z_APDU_initResponse:
1990 process_initResponse(apdu->u.initResponse);
1992 case Z_APDU_searchResponse:
1993 process_searchResponse(apdu->u.searchResponse);
1995 case Z_APDU_scanResponse:
1996 process_scanResponse(apdu->u.scanResponse);
1998 case Z_APDU_presentResponse:
1999 print_refid (apdu->u.presentResponse->referenceId);
2001 *apdu->u.presentResponse->numberOfRecordsReturned;
2002 if (apdu->u.presentResponse->records)
2003 display_records(apdu->u.presentResponse->records);
2005 printf("No records.\n");
2006 printf ("nextResultSetPosition = %d\n",
2007 *apdu->u.presentResponse->nextResultSetPosition);
2009 case Z_APDU_sortResponse:
2010 process_sortResponse(apdu->u.sortResponse);
2012 case Z_APDU_extendedServicesResponse:
2013 printf("Got extended services response\n");
2014 process_ESResponse(apdu->u.extendedServicesResponse);
2017 printf("Target has closed the association.\n");
2018 process_close(apdu->u.close);
2020 case Z_APDU_resourceControlRequest:
2021 process_resourceControlRequest
2022 (apdu->u.resourceControlRequest);
2024 case Z_APDU_deleteResultSetResponse:
2025 printf("Got deleteResultSetResponse status=%d\n",
2026 *apdu->u.deleteResultSetResponse->
2027 deleteOperationStatus);
2030 printf("Received unknown APDU type (%d).\n",
2035 while (conn && cs_more(conn));
2041 int main(int argc, char **argv)
2048 while ((ret = options("m:v:", argv, argc, &arg)) != -2)
2056 if (cmd_open (arg) == 2)
2061 if (!(marcdump = fopen (arg, "a")))
2068 log_init (log_mask_str(arg), "", NULL);
2071 fprintf (stderr, "Usage: %s [-m <marclog>] [<server-addr>]\n",
2078 return client (opened);