2 * Copyright (c) 1995, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.33 1996-05-09 07:25:22 quinn
10 * Revision 1.32 1996/03/15 11:05:33 adam
11 * The user can set the preferred query type (prefix, ccl, ..) with the
14 * Revision 1.31 1996/02/20 12:51:54 quinn
15 * Fixed problems with EXTERNAL.
17 * Revision 1.30 1996/02/12 18:18:09 quinn
20 * Revision 1.29 1996/01/02 08:57:25 quinn
21 * Changed enums in the ASN.1 .h files to #defines. Changed oident.class to oclass
23 * Revision 1.28 1995/12/14 11:09:31 quinn
24 * Added Explain record syntax to the format command.
26 * Revision 1.27 1995/12/12 16:37:02 quinn
27 * Added destroy element to data1_node.
29 * Revision 1.26 1995/12/12 14:11:00 quinn
32 * Revision 1.25 1995/11/13 09:27:22 quinn
33 * Fiddling with the variant stuff.
35 * Revision 1.24 1995/10/30 12:41:13 quinn
36 * Added hostname lookup for server.
38 * Revision 1.23 1995/10/18 16:12:30 quinn
41 * Revision 1.22 1995/10/11 14:49:12 quinn
44 * Revision 1.21 1995/09/29 17:01:47 quinn
47 * Revision 1.20 1995/08/29 14:24:13 quinn
48 * Added second half of close-handshake
50 * Revision 1.19 1995/08/29 11:17:28 quinn
51 * Added code to receive close
53 * Revision 1.18 1995/08/28 12:21:27 quinn
54 * Client can now ask for simple element set names.
56 * Revision 1.17 1995/08/17 12:45:02 quinn
57 * Fixed minor problems with GRS-1. Added support in c&s.
59 * Revision 1.16 1995/08/15 12:00:04 quinn
62 * Revision 1.15 1995/06/22 09:28:03 quinn
63 * Fixed bug in SUTRS processing.
65 * Revision 1.14 1995/06/19 12:37:41 quinn
68 * Revision 1.13 1995/06/16 10:29:11 quinn
69 * *** empty log message ***
71 * Revision 1.12 1995/06/15 07:44:57 quinn
74 * Revision 1.11 1995/06/14 15:26:40 quinn
75 * *** empty log message ***
77 * Revision 1.10 1995/06/06 14:56:58 quinn
80 * Revision 1.9 1995/06/06 08:15:19 quinn
83 * Revision 1.8 1995/06/05 10:52:22 quinn
86 * Revision 1.7 1995/06/02 09:50:09 quinn
89 * Revision 1.6 1995/05/31 08:29:21 quinn
90 * Nothing significant.
92 * Revision 1.5 1995/05/29 08:10:47 quinn
93 * Moved oid.c to util.
95 * Revision 1.4 1995/05/22 15:30:13 adam
96 * Client uses prefix query notation.
98 * Revision 1.3 1995/05/22 15:06:53 quinn
99 * *** empty log message ***
101 * Revision 1.2 1995/05/22 14:56:40 quinn
102 * *** empty log message ***
104 * Revision 1.1 1995/05/22 11:30:31 quinn
105 * Added prettier client.
111 * This is the obligatory little toy client, whose primary purpose is
112 * to illustrate the use of the YAZ service-level API.
120 #include <sys/time.h>
124 #include <sys/select.h>
127 #include <comstack.h>
134 #include <marcdisp.h>
135 #include <diagbib1.h>
143 #define C_PROMPT "Z> "
145 static ODR out, in, print; /* encoding and decoding streams */
146 static COMSTACK conn = 0; /* our z-association */
147 static Z_IdAuthentication *auth = 0; /* our current auth definition */
148 static char database[512] = "Default"; /* Database name */
149 static int setnumber = 0; /* current result set number */
150 static int smallSetUpperBound = 0;
151 static int largeSetLowerBound = 1;
152 static int mediumSetPresentNumber = 0;
153 static Z_ElementSetNames *elementSetNames = 0;
154 static int setno = 1; /* current set offset */
155 static int protocol = PROTO_Z3950; /* current app protocol */
156 static int recordsyntax = VAL_USMARC;
157 static int sent_close = 0;
158 static ODR_MEM session_mem; /* memory handle for init-response */
159 static Z_InitResponse *session = 0; /* session parameters */
160 static char last_scan[512] = "0";
161 static char last_cmd[100] = "?";
162 static oid_value attributeset = VAL_BIB1;
163 static FILE *marcdump = 0;
164 static char marcdump_file[512] = "marc.out";
172 static QueryType queryType = QueryType_Prefix;
175 static CCL_bibset bibset; /* CCL bibset handle */
178 static void send_apdu(Z_APDU *a)
183 if (!z_APDU(out, &a, 0))
185 odr_perror(out, "Encoding APDU");
188 buf = odr_getbuf(out, &len, 0);
189 odr_reset(out); /* release the APDU structure */
190 if (cs_put(conn, buf, len) < 0)
192 fprintf(stderr, "cs_put: %s", cs_errmsg(cs_errno(conn)));
197 /* INIT SERVICE ------------------------------- */
199 static void send_initRequest()
201 Z_APDU *apdu = zget_APDU(out, Z_APDU_initRequest);
202 Z_InitRequest *req = apdu->u.initRequest;
204 ODR_MASK_SET(req->options, Z_Options_search);
205 ODR_MASK_SET(req->options, Z_Options_present);
206 ODR_MASK_SET(req->options, Z_Options_namedResultSets);
207 ODR_MASK_SET(req->options, Z_Options_triggerResourceCtrl);
208 ODR_MASK_SET(req->options, Z_Options_scan);
210 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_1);
211 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_2);
212 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_3);
214 *req->maximumRecordSize = 1024*1024;
216 req->idAuthentication = auth;
219 printf("Sent initrequest.\n");
222 static int process_initResponse(Z_InitResponse *res)
224 /* save session parameters for later use */
225 session_mem = odr_extract_mem(in);
229 printf("Connection rejected by target.\n");
231 printf("Connection accepted by target.\n");
232 if (res->implementationId)
233 printf("ID : %s\n", res->implementationId);
234 if (res->implementationName)
235 printf("Name : %s\n", res->implementationName);
236 if (res->implementationVersion)
237 printf("Version: %s\n", res->implementationVersion);
238 if (res->userInformationField)
240 printf("UserInformationfield:\n");
241 if (!z_External(print, (Z_External**)&res-> userInformationField,
244 odr_perror(print, "Printing userinfo\n");
247 if (res->userInformationField->which == Z_External_octet)
249 printf("Guessing visiblestring:\n");
250 printf("'%s'\n", res->userInformationField->u. octet_aligned->buf);
256 int cmd_open(char *arg)
259 char type[100], addr[100];
264 printf("Already connected.\n");
267 if (!*arg || sscanf(arg, "%[^:]:%s", type, addr) < 2)
269 fprintf(stderr, "Usage: open (osi|tcp) ':' [tsel '/']host[':'port]\n");
273 if (!strcmp(type, "osi"))
275 if (!(add = mosi_strtoaddr(addr)))
285 if (!strcmp(type, "tcp"))
287 if (!(add = tcpip_strtoaddr(addr)))
293 protocol = PROTO_Z3950;
297 fprintf(stderr, "Bad type: %s\n", type);
300 if (!(conn = cs_create(t, 1, protocol)))
305 printf("Connecting...");
307 if (cs_connect(conn, add) < 0)
319 int cmd_authentication(char *arg)
321 static Z_IdAuthentication au;
322 static char open[256];
326 printf("Auth field set to null\n");
331 au.which = Z_IdAuthentication_open;
337 /* SEARCH SERVICE ------------------------------ */
339 static void display_variant(Z_Variant *v, int level)
343 for (i = 0; i < v->num_triples; i++)
345 printf("%*sclass=%d,type=%d", level * 4, "", *v->triples[i]->class,
346 *v->triples[i]->type);
347 if (v->triples[i]->which == Z_Triple_internationalString)
348 printf(",value=%s\n", v->triples[i]->value.internationalString);
354 static void display_grs1(Z_GenericRecord *r, int level)
360 for (i = 0; i < r->num_elements; i++)
364 printf("%*s", level * 4, "");
368 printf("%d,", *t->tagType);
371 if (t->tagValue->which == Z_StringOrNumeric_numeric)
372 printf("%d) ", *t->tagValue->u.numeric);
374 printf("%s) ", t->tagValue->u.string);
375 if (t->content->which == Z_ElementData_subtree)
378 display_grs1(t->content->u.subtree, level+1);
380 else if (t->content->which == Z_ElementData_string)
381 printf("%s\n", t->content->u.string);
382 else if (t->content->which == Z_ElementData_numeric)
383 printf("%d\n", *t->content->u.numeric);
384 else if (t->content->which == Z_ElementData_noDataRequested)
385 printf("[No data requested]\n");
386 else if (t->content->which == Z_ElementData_elementEmpty)
387 printf("[Element empty]\n");
388 else if (t->content->which == Z_ElementData_elementNotThere)
389 printf("[Element not there]\n");
392 if (t->appliedVariant)
393 display_variant(t->appliedVariant, level+1);
394 if (t->metaData && t->metaData->supportedVariants)
398 printf("%*s---- variant list\n", (level+1)*4, "");
399 for (c = 0; c < t->metaData->num_supportedVariants; c++)
401 printf("%*svariant #%d\n", (level+1)*4, "", c);
402 display_variant(t->metaData->supportedVariants[c], level + 2);
408 static void display_record(Z_DatabaseRecord *p)
410 Z_External *r = (Z_External*) p;
411 oident *ent = oid_getentbyoid(r->direct_reference);
414 * Tell the user what we got.
416 if (r->direct_reference)
418 printf("Record type: ");
420 printf("%s\n", ent->desc);
421 else if (!odr_oid(print, &r->direct_reference, 0))
423 odr_perror(print, "print oid");
428 /* Check if this is a known, ASN.1 type tucked away in an octet string */
429 if (ent && r->which == Z_External_octet)
431 Z_ext_typeent *type = z_ext_getentbyref(ent->value);
437 * Call the given decoder to process the record.
439 odr_setbuf(in, (char*)p->u.octet_aligned->buf,
440 p->u.octet_aligned->len, 0);
441 if (!(*type->fun)(in, &rr, 0))
443 odr_perror(in, "Decoding constructed record.");
444 fprintf(stderr, "[Near %d]\n", odr_offset(in));
445 fprintf(stderr, "Packet dump:\n---------\n");
446 odr_dumpBER(stderr, (char*)p->u.octet_aligned->buf,
447 p->u.octet_aligned->len);
448 fprintf(stderr, "---------\n");
452 * Note: we throw away the original, BER-encoded record here.
453 * Do something else with it if you want to keep it.
455 r->u.sutrs = rr; /* we don't actually check the type here. */
456 r->which = type->what;
459 if (r->which == Z_External_octet && p->u.octet_aligned->len)
460 marc_display ((char*)p->u.octet_aligned->buf, stdout);
461 else if (ent->value == VAL_SUTRS)
463 if (r->which != Z_External_sutrs)
465 printf("Expecting single SUTRS type for SUTRS.\n");
468 printf("%.*s", r->u.sutrs->len, r->u.sutrs->buf);
470 else if (ent->value == VAL_GRS1)
472 if (r->which != Z_External_grs1)
474 printf("Expecting single GRS type for GRS.\n");
477 display_grs1(r->u.grs1, 0);
481 printf("Unknown record representation.\n");
482 if (!z_External(print, &r, 0))
484 odr_perror(print, "Printing external");
490 static void display_diagrec(Z_DiagRec *p)
494 Z_DefaultDiagFormat *r;
499 printf("Diagnostic message from database:\n");
501 if (p->which != Z_DiagRec_defaultFormat)
503 printf("Diagnostic record not in default format.\n");
507 r = p->u.defaultFormat;
509 if (!(ent = oid_getentbyoid(r->diagnosticSetId)) ||
510 ent->oclass != CLASS_DIAGSET || ent->value != VAL_BIB1)
511 printf("Missing or unknown diagset\n");
512 printf(" [%d] %s", *r->condition, diagbib1_str(*r->condition));
513 if (r->addinfo && *r->addinfo)
514 printf(" -- '%s'\n", r->addinfo);
519 static void display_nameplusrecord(Z_NamePlusRecord *p)
522 printf("[%s]", p->databaseName);
523 if (p->which == Z_NamePlusRecord_surrogateDiagnostic)
524 display_diagrec(p->u.surrogateDiagnostic);
526 display_record(p->u.databaseRecord);
529 static void display_records(Z_Records *p)
533 if (p->which == Z_Records_NSD)
534 display_diagrec(p->u.nonSurrogateDiagnostic);
537 printf("Records: %d\n", p->u.databaseOrSurDiagnostics->num_records);
538 for (i = 0; i < p->u.databaseOrSurDiagnostics->num_records; i++)
539 display_nameplusrecord(p->u.databaseOrSurDiagnostics->records[i]);
543 static int send_searchRequest(char *arg)
545 Z_APDU *apdu = zget_APDU(out, Z_APDU_searchRequest);
546 Z_SearchRequest *req = apdu->u.searchRequest;
547 char *databaseNames = database;
550 struct ccl_rpn_node *rpn;
555 Z_RPNQuery *RPNquery;
559 if (queryType == QueryType_CCL2RPN)
561 rpn = ccl_find_str(bibset, arg, &error, &pos);
564 printf("CCL ERROR: %s\n", ccl_err_msg(error));
570 if (!strcmp(arg, "@big")) /* strictly for troublemaking */
572 static unsigned char big[2100];
575 /* send a very big referenceid to test transport stack etc. */
576 memset(big, 'A', 2100);
577 bigo.len = bigo.size = 2100;
579 req->referenceId = &bigo;
584 sprintf(setstring, "%d", ++setnumber);
585 req->resultSetName = setstring;
587 *req->smallSetUpperBound = smallSetUpperBound;
588 *req->largeSetLowerBound = largeSetLowerBound;
589 *req->mediumSetPresentNumber = mediumSetPresentNumber;
590 if (smallSetUpperBound > 0 || (largeSetLowerBound > 1 &&
591 mediumSetPresentNumber > 0))
595 prefsyn.proto = protocol;
596 prefsyn.oclass = CLASS_RECSYN;
597 prefsyn.value = recordsyntax;
598 req->preferredRecordSyntax =
599 odr_oiddup(out, oid_getoidbyent(&prefsyn));
600 req->smallSetElementSetNames =
601 req->mediumSetElementSetNames = elementSetNames;
603 req->num_databaseNames = 1;
604 req->databaseNames = &databaseNames;
610 case QueryType_Prefix:
611 query.which = Z_Query_type_1;
612 RPNquery = p_query_rpn (out, protocol, arg);
615 printf("Prefix query error\n");
618 query.u.type_1 = RPNquery;
621 query.which = Z_Query_type_2;
622 query.u.type_2 = &ccl_query;
623 ccl_query.buf = (unsigned char*) arg;
624 ccl_query.len = strlen(arg);
627 case QueryType_CCL2RPN:
628 query.which = Z_Query_type_1;
629 assert((RPNquery = ccl_rpn_query(rpn)));
630 bib1.proto = protocol;
631 bib1.oclass = CLASS_ATTSET;
632 bib1.value = VAL_BIB1;
633 RPNquery->attributeSetId = oid_getoidbyent(&bib1);
634 query.u.type_1 = RPNquery;
638 printf ("Unsupported query type\n");
643 printf("Sent searchRequest.\n");
647 static int process_searchResponse(Z_SearchResponse *res)
649 if (*res->searchStatus)
650 printf("Search was a success.\n");
652 printf("Search was a bloomin' failure.\n");
653 printf("Number of hits: %d, setno %d\n",
654 *res->resultCount, setnumber);
655 printf("records returned: %d\n",
656 *res->numberOfRecordsReturned);
657 setno += *res->numberOfRecordsReturned;
659 display_records(res->records);
663 static int cmd_find(char *arg)
667 printf("Find what?\n");
672 printf("Not connected yet\n");
675 if (!send_searchRequest(arg))
680 static int cmd_ssub(char *arg)
682 if (!(smallSetUpperBound = atoi(arg)))
687 static int cmd_lslb(char *arg)
689 if (!(largeSetLowerBound = atoi(arg)))
694 static int cmd_mspn(char *arg)
696 if (!(mediumSetPresentNumber = atoi(arg)))
701 static int cmd_status(char *arg)
703 printf("smallSetUpperBound: %d\n", smallSetUpperBound);
704 printf("largeSetLowerBound: %d\n", largeSetLowerBound);
705 printf("mediumSetPresentNumber: %d\n", mediumSetPresentNumber);
709 static int cmd_base(char *arg)
713 printf("Usage: base <database>\n");
716 strcpy(database, arg);
720 static int cmd_setnames(char *arg)
724 printf("Set numbering enabled.\n");
729 printf("Set numbering disabled.\n");
735 /* PRESENT SERVICE ----------------------------- */
737 static int send_presentRequest(char *arg)
739 Z_APDU *apdu = zget_APDU(out, Z_APDU_presentRequest);
740 Z_PresentRequest *req = apdu->u.presentRequest;
741 Z_RecordComposition compo;
747 if ((p = strchr(arg, '+')))
757 sprintf(setstring, "%d", setnumber);
758 req->resultSetId = setstring;
760 req->resultSetStartPoint = &setno;
761 req->numberOfRecordsRequested = &nos;
762 prefsyn.proto = protocol;
763 prefsyn.oclass = CLASS_RECSYN;
764 prefsyn.value = recordsyntax;
765 req->preferredRecordSyntax = oid_getoidbyent(&prefsyn);
768 req->recordComposition = &compo;
769 compo.which = Z_RecordComp_simple;
770 compo.u.simple = elementSetNames;
773 printf("Sent presentRequest (%d+%d).\n", setno, nos);
777 void process_close(Z_Close *req)
779 Z_APDU *apdu = zget_APDU(out, Z_APDU_close);
780 Z_Close *res = apdu->u.close;
782 static char *reasons[] =
787 "cost limit reached",
789 "security violation",
796 printf("Reason: %s, message: %s\n", reasons[*req->closeReason],
797 req->diagnosticInformation ? req->diagnosticInformation : "NULL");
800 printf("Goodbye.\n");
803 *res->closeReason = Z_Close_finished;
805 printf("Sent response.\n");
809 static int cmd_show(char *arg)
811 if (!send_presentRequest(arg))
816 int cmd_quit(char *arg)
818 printf("See you later, alligator.\n");
822 int cmd_cancel(char *arg)
824 Z_APDU *apdu = zget_APDU(out, Z_APDU_triggerResourceControlRequest);
825 Z_TriggerResourceControlRequest *req =
826 apdu->u.triggerResourceControlRequest;
831 printf("Session not initialized yet\n");
834 if (!ODR_MASK_GET(session->options, Z_Options_triggerResourceCtrl))
836 printf("Target doesn't support cancel (trigger resource ctrl)\n");
839 *req->requestedAction = Z_TriggerResourceCtrl_cancel;
840 req->resultSetWanted = &false;
843 printf("Sent cancel request\n");
847 int send_scanrequest(char *string, int pp, int num)
849 Z_APDU *apdu = zget_APDU(out, Z_APDU_scanRequest);
850 Z_ScanRequest *req = apdu->u.scanRequest;
853 req->num_databaseNames = 1;
854 req->databaseNames = &db;
855 req->termListAndStartPoint = p_query_scan(out, protocol,
856 &req->attributeSet, string);
857 req->numberOfTermsRequested = #
858 req->preferredPositionInResponse = &pp;
863 void display_term(Z_TermInfo *t)
865 if (t->term->which == Z_Term_general)
867 printf("%.*s (%d)\n", t->term->u.general->len, t->term->u.general->buf,
868 t->globalOccurrences ? *t->globalOccurrences : -1);
869 sprintf(last_scan, "%.*s", t->term->u.general->len,
870 t->term->u.general->buf);
873 printf("Term type not general.\n");
876 void process_scanResponse(Z_ScanResponse *res)
880 printf("SCAN: %d entries, position=%d\n", *res->numberOfEntriesReturned,
881 *res->positionOfTerm);
882 if (*res->scanStatus != Z_Scan_success)
883 printf("Scan returned code %d\n", *res->scanStatus);
886 if (res->entries->which == Z_ListEntries_entries)
888 Z_Entries *ent = res->entries->u.entries;
890 for (i = 0; i < ent->num_entries; i++)
891 if (ent->entries[i]->which == Z_Entry_termInfo)
893 printf("%c ", i + 1 == *res->positionOfTerm ? '*' : ' ');
894 display_term(ent->entries[i]->u.termInfo);
897 display_diagrec(ent->entries[i]->u.surrogateDiagnostic);
900 display_diagrec(res->entries->u.nonSurrogateDiagnostics->diagRecs[0]);
903 int cmd_scan(char *arg)
907 printf("Session not initialized yet\n");
910 if (!ODR_MASK_GET(session->options, Z_Options_scan))
912 printf("Target doesn't support scan\n");
917 if (send_scanrequest(arg, 5, 20) < 0)
921 if (send_scanrequest(last_scan, 1, 20) < 0)
926 int cmd_format(char *arg)
930 printf("Usage: format <recordsyntax>\n");
933 if (!strcmp(arg, "sutrs"))
935 printf("Preferred format is SUTRS.\n");
936 recordsyntax = VAL_SUTRS;
939 else if (!strcmp(arg, "usmarc"))
941 printf("Preferred format is USMARC\n");
942 recordsyntax = VAL_USMARC;
945 else if (!strcmp(arg, "danmarc"))
947 printf("Preferred format is DANMARC\n");
948 recordsyntax = VAL_DANMARC;
951 else if (!strcmp(arg, "grs1"))
953 printf("Preferred format is GRS1\n");
954 recordsyntax = VAL_GRS1;
957 else if (!strcmp(arg, "explain"))
959 printf("Preferred format is Explain\n");
960 recordsyntax = VAL_EXPLAIN;
965 printf("Specify one of {sutrs,usmarc,danmarc,grs1}.\n");
970 int cmd_elements(char *arg)
972 static Z_ElementSetNames esn;
973 static char what[100];
977 printf("Usage: elements <esn>\n");
981 esn.which = Z_ElementSetNames_generic;
982 esn.u.generic = what;
983 elementSetNames = &esn;
987 int cmd_attributeset(char *arg)
994 printf("Usage: attributeset <setname>\n");
997 sscanf(arg, "%s", what);
998 if ((v = oid_getvalbyname(what)) == VAL_NONE)
1000 printf("Unknown attribute set name\n");
1007 int cmd_querytype (char *arg)
1009 if (!strcmp (arg, "ccl"))
1010 queryType = QueryType_CCL;
1011 else if (!strcmp (arg, "prefix"))
1012 queryType = QueryType_Prefix;
1014 else if (!strcmp (arg, "ccl2rpn") || !strcmp (arg, "cclrpn"))
1015 queryType = QueryType_CCL2RPN;
1019 printf ("Querytype must be one of:\n");
1020 printf (" prefix - Prefix query\n");
1021 printf (" ccl - CCL query\n");
1023 printf (" ccl2rpn - CCL query converted to RPN\n");
1030 int cmd_close(char *arg)
1032 Z_APDU *apdu = zget_APDU(out, Z_APDU_close);
1033 Z_Close *req = apdu->u.close;
1035 *req->closeReason = Z_Close_finished;
1037 printf("Sent close request.\n");
1042 static void initialize(void)
1048 if (!(out = odr_createmem(ODR_ENCODE)) ||
1049 !(in = odr_createmem(ODR_DECODE)) ||
1050 !(print = odr_createmem(ODR_PRINT)))
1052 fprintf(stderr, "failed to allocate ODR streams\n");
1055 setvbuf(stdout, 0, _IONBF, 0);
1058 bibset = ccl_qual_mk ();
1059 inf = fopen ("default.bib", "r");
1062 ccl_qual_file (bibset, inf);
1068 static int client(void)
1072 int (*fun)(char *arg);
1075 {"open", cmd_open, "('tcp'|'osi')':'[<tsel>'/']<host>[':'<port>]"},
1076 {"quit", cmd_quit, ""},
1077 {"find", cmd_find, "<query>"},
1078 {"base", cmd_base, "<base-name>"},
1079 {"show", cmd_show, "<rec#>['+'<#recs>]"},
1080 {"scan", cmd_scan, "<term>"},
1081 {"authentication", cmd_authentication, "<acctstring>"},
1082 {"lslb", cmd_lslb, "<largeSetLowerBound>"},
1083 {"ssub", cmd_ssub, "<smallSetUpperBound>"},
1084 {"mspn", cmd_mspn, "<mediumSetPresentNumber>"},
1085 {"status", cmd_status, ""},
1086 {"setnames", cmd_setnames, ""},
1087 {"cancel", cmd_cancel, ""},
1088 {"format", cmd_format, "<recordsyntax>"},
1089 {"elements", cmd_elements, "<elementSetName>"},
1090 {"close", cmd_close, ""},
1091 {"attributeset", cmd_attributeset, "<attrset>"},
1092 {"querytype", cmd_querytype, "<type>"},
1096 int netbufferlen = 0;
1104 char line[1024], word[1024], arg[1024];
1109 FD_SET(cs_fileno(conn), &input);
1110 if ((res = select(20, &input, 0, 0, 0)) < 0)
1117 if (FD_ISSET(0, &input))
1119 /* quick & dirty way to get a command line. */
1122 if ((res = sscanf(line, "%s %[^;]", word, arg)) <= 0)
1124 strcpy(word, last_cmd);
1129 strcpy(last_cmd, word);
1130 for (i = 0; cmd[i].cmd; i++)
1131 if (!strncmp(cmd[i].cmd, word, strlen(word)))
1133 res = (*cmd[i].fun)(arg);
1136 if (!cmd[i].cmd) /* dump our help-screen */
1138 printf("Unknown command: %s.\n", word);
1139 printf("Currently recognized commands:\n");
1140 for (i = 0; cmd[i].cmd; i++)
1141 printf(" %s %s\n", cmd[i].cmd, cmd[i].ad);
1147 if (conn && FD_ISSET(cs_fileno(conn), &input))
1151 if ((res = cs_get(conn, &netbuffer, &netbufferlen)) < 0)
1158 printf("Target closed connection.\n");
1161 odr_reset(in); /* release APDU from last round */
1162 odr_setbuf(in, netbuffer, res, 0);
1163 if (!z_APDU(in, &apdu, 0))
1165 odr_perror(in, "Decoding incoming APDU");
1166 fprintf(stderr, "[Near %d]\n", odr_offset(in));
1167 fprintf(stderr, "Packet dump:\n---------\n");
1168 odr_dumpBER(stderr, netbuffer, res);
1169 fprintf(stderr, "---------\n");
1173 if (!z_APDU(print, &apdu, 0))
1175 odr_perror(print, "Failed to print incoming APDU");
1182 case Z_APDU_initResponse:
1183 process_initResponse(apdu->u.initResponse);
1185 case Z_APDU_searchResponse:
1186 process_searchResponse(apdu->u.searchResponse);
1188 case Z_APDU_scanResponse:
1189 process_scanResponse(apdu->u.scanResponse);
1191 case Z_APDU_presentResponse:
1192 printf("Received presentResponse.\n");
1194 *apdu->u.presentResponse->numberOfRecordsReturned;
1195 if (apdu->u.presentResponse->records)
1196 display_records(apdu->u.presentResponse->records);
1198 printf("No records.\n");
1201 printf("Target has closed the association.\n");
1202 process_close(apdu->u.close);
1205 printf("Received unknown APDU type (%d).\n",
1212 while (cs_more(conn));
1218 int main(int argc, char **argv)
1225 if (*marcdump_file && !(marcdump = fopen(marcdump_file, "a")))
1227 perror(marcdump_file);