2 * Copyright (c) 1995, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.31 1996-02-20 12:51:54 quinn
8 * Fixed problems with EXTERNAL.
10 * Revision 1.30 1996/02/12 18:18:09 quinn
13 * Revision 1.29 1996/01/02 08:57:25 quinn
14 * Changed enums in the ASN.1 .h files to #defines. Changed oident.class to oclass
16 * Revision 1.28 1995/12/14 11:09:31 quinn
17 * Added Explain record syntax to the format command.
19 * Revision 1.27 1995/12/12 16:37:02 quinn
20 * Added destroy element to data1_node.
22 * Revision 1.26 1995/12/12 14:11:00 quinn
25 * Revision 1.25 1995/11/13 09:27:22 quinn
26 * Fiddling with the variant stuff.
28 * Revision 1.24 1995/10/30 12:41:13 quinn
29 * Added hostname lookup for server.
31 * Revision 1.23 1995/10/18 16:12:30 quinn
34 * Revision 1.22 1995/10/11 14:49:12 quinn
37 * Revision 1.21 1995/09/29 17:01:47 quinn
40 * Revision 1.20 1995/08/29 14:24:13 quinn
41 * Added second half of close-handshake
43 * Revision 1.19 1995/08/29 11:17:28 quinn
44 * Added code to receive close
46 * Revision 1.18 1995/08/28 12:21:27 quinn
47 * Client can now ask for simple element set names.
49 * Revision 1.17 1995/08/17 12:45:02 quinn
50 * Fixed minor problems with GRS-1. Added support in c&s.
52 * Revision 1.16 1995/08/15 12:00:04 quinn
55 * Revision 1.15 1995/06/22 09:28:03 quinn
56 * Fixed bug in SUTRS processing.
58 * Revision 1.14 1995/06/19 12:37:41 quinn
61 * Revision 1.13 1995/06/16 10:29:11 quinn
62 * *** empty log message ***
64 * Revision 1.12 1995/06/15 07:44:57 quinn
67 * Revision 1.11 1995/06/14 15:26:40 quinn
68 * *** empty log message ***
70 * Revision 1.10 1995/06/06 14:56:58 quinn
73 * Revision 1.9 1995/06/06 08:15:19 quinn
76 * Revision 1.8 1995/06/05 10:52:22 quinn
79 * Revision 1.7 1995/06/02 09:50:09 quinn
82 * Revision 1.6 1995/05/31 08:29:21 quinn
83 * Nothing significant.
85 * Revision 1.5 1995/05/29 08:10:47 quinn
86 * Moved oid.c to util.
88 * Revision 1.4 1995/05/22 15:30:13 adam
89 * Client uses prefix query notation.
91 * Revision 1.3 1995/05/22 15:06:53 quinn
92 * *** empty log message ***
94 * Revision 1.2 1995/05/22 14:56:40 quinn
95 * *** empty log message ***
97 * Revision 1.1 1995/05/22 11:30:31 quinn
98 * Added prettier client.
104 * This is the obligatory little toy client, whose primary purpose is
105 * to illustrate the use of the YAZ service-level API.
113 #include <sys/time.h>
117 #include <sys/select.h>
120 #include <comstack.h>
127 #include <marcdisp.h>
128 #include <diagbib1.h>
138 #define C_PROMPT "Z> "
140 static ODR out, in, print; /* encoding and decoding streams */
141 static COMSTACK conn = 0; /* our z-association */
142 static Z_IdAuthentication *auth = 0; /* our current auth definition */
143 static char database[512] = "Default"; /* Database name */
144 static int setnumber = 0; /* current result set number */
145 static int smallSetUpperBound = 0;
146 static int largeSetLowerBound = 1;
147 static int mediumSetPresentNumber = 0;
148 static Z_ElementSetNames *elementSetNames = 0;
149 static int setno = 1; /* current set offset */
150 static int protocol = PROTO_Z3950; /* current app protocol */
151 static int recordsyntax = VAL_USMARC;
152 static int sent_close = 0;
153 static ODR_MEM session_mem; /* memory handle for init-response */
154 static Z_InitResponse *session = 0; /* session parameters */
155 static char last_scan[512] = "0";
156 static char last_cmd[100] = "?";
157 static oid_value attributeset = VAL_BIB1;
158 static FILE *marcdump = 0;
159 static char marcdump_file[512] = "marc.out";
163 static CCL_bibset bibset; /* CCL bibset handle */
167 static void send_apdu(Z_APDU *a)
172 if (!z_APDU(out, &a, 0))
174 odr_perror(out, "Encoding APDU");
177 buf = odr_getbuf(out, &len, 0);
178 odr_reset(out); /* release the APDU structure */
179 if (cs_put(conn, buf, len) < 0)
181 fprintf(stderr, "cs_put: %s", cs_errmsg(cs_errno(conn)));
186 /* INIT SERVICE ------------------------------- */
188 static void send_initRequest()
190 Z_APDU *apdu = zget_APDU(out, Z_APDU_initRequest);
191 Z_InitRequest *req = apdu->u.initRequest;
193 ODR_MASK_SET(req->options, Z_Options_search);
194 ODR_MASK_SET(req->options, Z_Options_present);
195 ODR_MASK_SET(req->options, Z_Options_namedResultSets);
196 ODR_MASK_SET(req->options, Z_Options_triggerResourceCtrl);
197 ODR_MASK_SET(req->options, Z_Options_scan);
199 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_1);
200 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_2);
201 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_3);
203 *req->maximumRecordSize = 1024*1024;
205 req->idAuthentication = auth;
208 printf("Sent initrequest.\n");
211 static int process_initResponse(Z_InitResponse *res)
213 /* save session parameters for later use */
214 session_mem = odr_extract_mem(in);
218 printf("Connection rejected by target.\n");
220 printf("Connection accepted by target.\n");
221 if (res->implementationId)
222 printf("ID : %s\n", res->implementationId);
223 if (res->implementationName)
224 printf("Name : %s\n", res->implementationName);
225 if (res->implementationVersion)
226 printf("Version: %s\n", res->implementationVersion);
227 if (res->userInformationField)
229 printf("UserInformationfield:\n");
230 if (!z_External(print, (Z_External**)&res-> userInformationField,
233 odr_perror(print, "Printing userinfo\n");
236 if (res->userInformationField->which == Z_External_octet)
238 printf("Guessing visiblestring:\n");
239 printf("'%s'\n", res->userInformationField->u. octet_aligned->buf);
245 int cmd_open(char *arg)
248 char type[100], addr[100];
253 printf("Already connected.\n");
256 if (!*arg || sscanf(arg, "%[^:]:%s", type, addr) < 2)
258 fprintf(stderr, "Usage: open (osi|tcp) ':' [tsel '/']host[':'port]\n");
262 if (!strcmp(type, "osi"))
264 if (!(add = mosi_strtoaddr(addr)))
274 if (!strcmp(type, "tcp"))
276 if (!(add = tcpip_strtoaddr(addr)))
282 protocol = PROTO_Z3950;
286 fprintf(stderr, "Bad type: %s\n", type);
289 if (!(conn = cs_create(t, 1, protocol)))
294 printf("Connecting...");
296 if (cs_connect(conn, add) < 0)
308 int cmd_authentication(char *arg)
310 static Z_IdAuthentication au;
311 static char open[256];
315 printf("Auth field set to null\n");
320 au.which = Z_IdAuthentication_open;
326 /* SEARCH SERVICE ------------------------------ */
328 static void display_variant(Z_Variant *v, int level)
332 for (i = 0; i < v->num_triples; i++)
334 printf("%*sclass=%d,type=%d", level * 4, "", *v->triples[i]->class,
335 *v->triples[i]->type);
336 if (v->triples[i]->which == Z_Triple_internationalString)
337 printf(",value=%s\n", v->triples[i]->value.internationalString);
343 static void display_grs1(Z_GenericRecord *r, int level)
349 for (i = 0; i < r->num_elements; i++)
353 printf("%*s", level * 4, "");
357 printf("%d,", *t->tagType);
360 if (t->tagValue->which == Z_StringOrNumeric_numeric)
361 printf("%d) ", *t->tagValue->u.numeric);
363 printf("%s) ", t->tagValue->u.string);
364 if (t->content->which == Z_ElementData_subtree)
367 display_grs1(t->content->u.subtree, level+1);
369 else if (t->content->which == Z_ElementData_string)
370 printf("%s\n", t->content->u.string);
371 else if (t->content->which == Z_ElementData_numeric)
372 printf("%d\n", *t->content->u.numeric);
373 else if (t->content->which == Z_ElementData_noDataRequested)
374 printf("[No data requested]\n");
375 else if (t->content->which == Z_ElementData_elementEmpty)
376 printf("[Element empty]\n");
377 else if (t->content->which == Z_ElementData_elementNotThere)
378 printf("[Element not there]\n");
381 if (t->appliedVariant)
382 display_variant(t->appliedVariant, level+1);
383 if (t->metaData && t->metaData->supportedVariants)
387 printf("%*s---- variant list\n", (level+1)*4, "");
388 for (c = 0; c < t->metaData->num_supportedVariants; c++)
390 printf("%*svariant #%d\n", (level+1)*4, "", c);
391 display_variant(t->metaData->supportedVariants[c], level + 2);
397 static void display_record(Z_DatabaseRecord *p)
399 Z_External *r = (Z_External*) p;
400 oident *ent = oid_getentbyoid(r->direct_reference);
403 * Tell the user what we got.
405 if (r->direct_reference)
407 printf("Record type: ");
409 printf("%s\n", ent->desc);
410 else if (!odr_oid(print, &r->direct_reference, 0))
412 odr_perror(print, "print oid");
417 /* Check if this is a known, ASN.1 type tucked away in an octet string */
418 if (ent && r->which == Z_External_octet)
420 Z_ext_typeent *type = z_ext_getentbyref(ent->value);
426 * Call the given decoder to process the record.
428 odr_setbuf(in, (char*)p->u.octet_aligned->buf,
429 p->u.octet_aligned->len, 0);
430 if (!(*type->fun)(in, &rr, 0))
432 odr_perror(in, "Decoding constructed record.");
433 fprintf(stderr, "[Near %d]\n", odr_offset(in));
434 fprintf(stderr, "Packet dump:\n---------\n");
435 odr_dumpBER(stderr, (char*)p->u.octet_aligned->buf,
436 p->u.octet_aligned->len);
437 fprintf(stderr, "---------\n");
441 * Note: we throw away the original, BER-encoded record here.
442 * Do something else with it if you want to keep it.
444 r->u.sutrs = rr; /* we don't actually check the type here. */
445 r->which = type->what;
448 if (r->which == Z_External_octet && p->u.octet_aligned->len)
449 marc_display ((char*)p->u.octet_aligned->buf, stdout);
450 else if (ent->value == VAL_SUTRS)
452 if (r->which != Z_External_sutrs)
454 printf("Expecting single SUTRS type for SUTRS.\n");
457 printf("%.*s", r->u.sutrs->len, r->u.sutrs->buf);
459 else if (ent->value == VAL_GRS1)
461 if (r->which != Z_External_grs1)
463 printf("Expecting single GRS type for GRS.\n");
466 display_grs1(r->u.grs1, 0);
470 printf("Unknown record representation.\n");
471 if (!z_External(print, &r, 0))
473 odr_perror(print, "Printing external");
479 static void display_diagrec(Z_DiagRec *p)
483 Z_DefaultDiagFormat *r;
488 printf("Diagnostic message from database:\n");
490 if (p->which != Z_DiagRec_defaultFormat)
492 printf("Diagnostic record not in default format.\n");
496 r = p->u.defaultFormat;
498 if (!(ent = oid_getentbyoid(r->diagnosticSetId)) ||
499 ent->oclass != CLASS_DIAGSET || ent->value != VAL_BIB1)
500 printf("Missing or unknown diagset\n");
501 printf(" [%d] %s", *r->condition, diagbib1_str(*r->condition));
502 if (r->addinfo && *r->addinfo)
503 printf(" -- %s\n", r->addinfo);
508 static void display_nameplusrecord(Z_NamePlusRecord *p)
511 printf("[%s]", p->databaseName);
512 if (p->which == Z_NamePlusRecord_surrogateDiagnostic)
513 display_diagrec(p->u.surrogateDiagnostic);
515 display_record(p->u.databaseRecord);
518 static void display_records(Z_Records *p)
522 if (p->which == Z_Records_NSD)
523 display_diagrec(p->u.nonSurrogateDiagnostic);
526 printf("Records: %d\n", p->u.databaseOrSurDiagnostics->num_records);
527 for (i = 0; i < p->u.databaseOrSurDiagnostics->num_records; i++)
528 display_nameplusrecord(p->u.databaseOrSurDiagnostics->records[i]);
532 static int send_searchRequest(char *arg)
534 Z_APDU *apdu = zget_APDU(out, Z_APDU_searchRequest);
535 Z_SearchRequest *req = apdu->u.searchRequest;
536 char *databaseNames = database;
540 struct ccl_rpn_node *rpn;
546 Z_RPNQuery *RPNquery;
554 rpn = ccl_find_str(bibset, arg, &error, &pos);
557 printf("CCL ERROR: %s\n", ccl_err_msg(error));
563 if (!strcmp(arg, "@big")) /* strictly for troublemaking */
565 static unsigned char big[2100];
568 /* send a very big referenceid to test transport stack etc. */
569 memset(big, 'A', 2100);
570 bigo.len = bigo.size = 2100;
572 req->referenceId = &bigo;
577 sprintf(setstring, "%d", ++setnumber);
578 req->resultSetName = setstring;
580 *req->smallSetUpperBound = smallSetUpperBound;
581 *req->largeSetLowerBound = largeSetLowerBound;
582 *req->mediumSetPresentNumber = mediumSetPresentNumber;
583 if (smallSetUpperBound > 0 || (largeSetLowerBound > 1 &&
584 mediumSetPresentNumber > 0))
588 prefsyn.proto = protocol;
589 prefsyn.oclass = CLASS_RECSYN;
590 prefsyn.value = recordsyntax;
591 req->preferredRecordSyntax = odr_oiddup(out, oid_getoidbyent(&prefsyn));
592 req->smallSetElementSetNames =
593 req->mediumSetElementSetNames = elementSetNames;
595 req->num_databaseNames = 1;
596 req->databaseNames = &databaseNames;
601 query.which = Z_Query_type_1;
604 assert((RPNquery = ccl_rpn_query(rpn)));
606 RPNquery = p_query_rpn (out, arg);
609 printf("Prefix query error\n");
613 bib1.proto = protocol;
614 bib1.oclass = CLASS_ATTSET;
615 bib1.value = VAL_BIB1;
616 RPNquery->attributeSetId = oid_getoidbyent(&bib1);
617 query.u.type_1 = RPNquery;
619 query.which = Z_Query_type_2;
620 query.u.type_2 = &ccl_query;
621 ccl_query.buf = (unsigned char*) arg;
622 ccl_query.len = strlen(arg);
627 printf("Sent searchRequest.\n");
631 static int process_searchResponse(Z_SearchResponse *res)
633 if (*res->searchStatus)
634 printf("Search was a success.\n");
636 printf("Search was a bloomin' failure.\n");
637 printf("Number of hits: %d, setno %d\n",
638 *res->resultCount, setnumber);
639 printf("records returned: %d\n",
640 *res->numberOfRecordsReturned);
641 setno += *res->numberOfRecordsReturned;
643 display_records(res->records);
647 static int cmd_find(char *arg)
651 printf("Find what?\n");
656 printf("Not connected yet\n");
659 if (!send_searchRequest(arg))
664 static int cmd_ssub(char *arg)
666 if (!(smallSetUpperBound = atoi(arg)))
671 static int cmd_lslb(char *arg)
673 if (!(largeSetLowerBound = atoi(arg)))
678 static int cmd_mspn(char *arg)
680 if (!(mediumSetPresentNumber = atoi(arg)))
685 static int cmd_status(char *arg)
687 printf("smallSetUpperBound: %d\n", smallSetUpperBound);
688 printf("largeSetLowerBound: %d\n", largeSetLowerBound);
689 printf("mediumSetPresentNumber: %d\n", mediumSetPresentNumber);
693 static int cmd_base(char *arg)
697 printf("Usage: base <database>\n");
700 strcpy(database, arg);
704 static int cmd_setnames(char *arg)
708 printf("Set numbering enabled.\n");
713 printf("Set numbering disabled.\n");
719 /* PRESENT SERVICE ----------------------------- */
721 static int send_presentRequest(char *arg)
723 Z_APDU *apdu = zget_APDU(out, Z_APDU_presentRequest);
724 Z_PresentRequest *req = apdu->u.presentRequest;
725 Z_RecordComposition compo;
731 if ((p = strchr(arg, '+')))
741 sprintf(setstring, "%d", setnumber);
742 req->resultSetId = setstring;
744 req->resultSetStartPoint = &setno;
745 req->numberOfRecordsRequested = &nos;
746 prefsyn.proto = protocol;
747 prefsyn.oclass = CLASS_RECSYN;
748 prefsyn.value = recordsyntax;
749 req->preferredRecordSyntax = oid_getoidbyent(&prefsyn);
752 req->recordComposition = &compo;
753 compo.which = Z_RecordComp_simple;
754 compo.u.simple = elementSetNames;
757 printf("Sent presentRequest (%d+%d).\n", setno, nos);
761 void process_close(Z_Close *req)
763 Z_APDU *apdu = zget_APDU(out, Z_APDU_close);
764 Z_Close *res = apdu->u.close;
766 static char *reasons[] =
771 "cost limit reached",
773 "security violation",
780 printf("Reason: %s, message: %s\n", reasons[*req->closeReason],
781 req->diagnosticInformation ? req->diagnosticInformation : "NULL");
784 printf("Goodbye.\n");
787 *res->closeReason = Z_Close_finished;
789 printf("Sent response.\n");
793 static int cmd_show(char *arg)
795 if (!send_presentRequest(arg))
800 int cmd_quit(char *arg)
802 printf("See you later, alligator.\n");
806 int cmd_cancel(char *arg)
808 Z_APDU *apdu = zget_APDU(out, Z_APDU_triggerResourceControlRequest);
809 Z_TriggerResourceControlRequest *req =
810 apdu->u.triggerResourceControlRequest;
815 printf("Session not initialized yet\n");
818 if (!ODR_MASK_GET(session->options, Z_Options_triggerResourceCtrl))
820 printf("Target doesn't support cancel (trigger resource ctrl)\n");
823 *req->requestedAction = Z_TriggerResourceCtrl_cancel;
824 req->resultSetWanted = &false;
827 printf("Sent cancel request\n");
831 int send_scanrequest(char *string, int pp, int num)
833 Z_APDU *apdu = zget_APDU(out, Z_APDU_scanRequest);
834 Z_ScanRequest *req = apdu->u.scanRequest;
838 req->num_databaseNames = 1;
839 req->databaseNames = &db;
840 attset.proto = protocol;
841 attset.oclass = CLASS_ATTSET;
842 attset.value = VAL_BIB1;
843 req->attributeSet = oid_getoidbyent(&attset);
844 req->termListAndStartPoint = p_query_scan(out, string);
845 req->numberOfTermsRequested = #
846 req->preferredPositionInResponse = &pp;
851 void display_term(Z_TermInfo *t)
853 if (t->term->which == Z_Term_general)
855 printf("%.*s (%d)\n", t->term->u.general->len, t->term->u.general->buf,
856 t->globalOccurrences ? *t->globalOccurrences : -1);
857 sprintf(last_scan, "%.*s", t->term->u.general->len,
858 t->term->u.general->buf);
861 printf("Term type not general.\n");
864 void process_scanResponse(Z_ScanResponse *res)
868 printf("SCAN: %d entries, position=%d\n", *res->numberOfEntriesReturned,
869 *res->positionOfTerm);
870 if (*res->scanStatus != Z_Scan_success)
871 printf("Scan returned code %d\n", *res->scanStatus);
874 if (res->entries->which == Z_ListEntries_entries)
876 Z_Entries *ent = res->entries->u.entries;
878 for (i = 0; i < ent->num_entries; i++)
879 if (ent->entries[i]->which == Z_Entry_termInfo)
881 printf("%c ", i + 1 == *res->positionOfTerm ? '*' : ' ');
882 display_term(ent->entries[i]->u.termInfo);
885 display_diagrec(ent->entries[i]->u.surrogateDiagnostic);
888 display_diagrec(res->entries->u.nonSurrogateDiagnostics->diagRecs[0]);
891 int cmd_scan(char *arg)
895 printf("Session not initialized yet\n");
898 if (!ODR_MASK_GET(session->options, Z_Options_scan))
900 printf("Target doesn't support scan\n");
905 if (send_scanrequest(arg, 5, 20) < 0)
909 if (send_scanrequest(last_scan, 1, 20) < 0)
914 int cmd_format(char *arg)
918 printf("Usage: format <recordsyntax>\n");
921 if (!strcmp(arg, "sutrs"))
923 printf("Preferred format is SUTRS.\n");
924 recordsyntax = VAL_SUTRS;
927 else if (!strcmp(arg, "usmarc"))
929 printf("Preferred format is USMARC\n");
930 recordsyntax = VAL_USMARC;
933 else if (!strcmp(arg, "danmarc"))
935 printf("Preferred format is DANMARC\n");
936 recordsyntax = VAL_DANMARC;
939 else if (!strcmp(arg, "grs1"))
941 printf("Preferred format is GRS1\n");
942 recordsyntax = VAL_GRS1;
945 else if (!strcmp(arg, "explain"))
947 printf("Preferred format is Explain\n");
948 recordsyntax = VAL_EXPLAIN;
953 printf("Specify one of {sutrs,usmarc,danmarc,grs1}.\n");
958 int cmd_elements(char *arg)
960 static Z_ElementSetNames esn;
961 static char what[100];
965 printf("Usage: elements <esn>\n");
969 esn.which = Z_ElementSetNames_generic;
970 esn.u.generic = what;
971 elementSetNames = &esn;
975 int cmd_attributeset(char *arg)
982 printf("Usage: attributeset <setname>\n");
985 sscanf(arg, "%s", what);
986 if ((v = oid_getvalbyname(what)) == VAL_NONE)
988 printf("Unknown attribute set name\n");
995 int cmd_close(char *arg)
997 Z_APDU *apdu = zget_APDU(out, Z_APDU_close);
998 Z_Close *req = apdu->u.close;
1000 *req->closeReason = Z_Close_finished;
1002 printf("Sent close request.\n");
1007 static void initialize(void)
1010 #ifndef PREFIX_QUERY
1015 if (!(out = odr_createmem(ODR_ENCODE)) ||
1016 !(in = odr_createmem(ODR_DECODE)) ||
1017 !(print = odr_createmem(ODR_PRINT)))
1019 fprintf(stderr, "failed to allocate ODR streams\n");
1022 setvbuf(stdout, 0, _IONBF, 0);
1025 #ifndef PREFIX_QUERY
1026 bibset = ccl_qual_mk ();
1027 inf = fopen ("default.bib", "r");
1030 ccl_qual_file (bibset, inf);
1037 static int client(void)
1041 int (*fun)(char *arg);
1044 {"open", cmd_open, "('tcp'|'osi')':'[<TSEL>'/']<HOST>[':'<PORT>]"},
1045 {"quit", cmd_quit, ""},
1046 {"find", cmd_find, "<CCL-QUERY>"},
1047 {"base", cmd_base, "<BASE-NAME>"},
1048 {"show", cmd_show, "<REC#>['+'<#RECS>]"},
1049 {"scan", cmd_scan, "<TERM>"},
1050 {"authentication", cmd_authentication, "<ACCTSTRING>"},
1051 {"lslb", cmd_lslb, "<largeSetLowerBound>"},
1052 {"ssub", cmd_ssub, "<smallSetUpperBound>"},
1053 {"mspn", cmd_mspn, "<mediumSetPresentNumber>"},
1054 {"status", cmd_status, ""},
1055 {"setnames", cmd_setnames, ""},
1056 {"cancel", cmd_cancel, ""},
1057 {"format", cmd_format, "<recordsyntax>"},
1058 {"elements", cmd_elements, "<elementSetName>"},
1059 {"close", cmd_close, ""},
1060 {"attributeset", cmd_attributeset, "<attrset>"},
1064 int netbufferlen = 0;
1072 char line[1024], word[1024], arg[1024];
1077 FD_SET(cs_fileno(conn), &input);
1078 if ((res = select(20, &input, 0, 0, 0)) < 0)
1085 if (FD_ISSET(0, &input))
1087 /* quick & dirty way to get a command line. */
1090 if ((res = sscanf(line, "%s %[^;]", word, arg)) <= 0)
1092 strcpy(word, last_cmd);
1097 strcpy(last_cmd, word);
1098 for (i = 0; cmd[i].cmd; i++)
1099 if (!strncmp(cmd[i].cmd, word, strlen(word)))
1101 res = (*cmd[i].fun)(arg);
1104 if (!cmd[i].cmd) /* dump our help-screen */
1106 printf("Unknown command: %s.\n", word);
1107 printf("Currently recognized commands:\n");
1108 for (i = 0; cmd[i].cmd; i++)
1109 printf(" %s %s\n", cmd[i].cmd, cmd[i].ad);
1115 if (conn && FD_ISSET(cs_fileno(conn), &input))
1119 if ((res = cs_get(conn, &netbuffer, &netbufferlen)) < 0)
1126 printf("Target closed connection.\n");
1129 odr_reset(in); /* release APDU from last round */
1130 odr_setbuf(in, netbuffer, res, 0);
1131 if (!z_APDU(in, &apdu, 0))
1133 odr_perror(in, "Decoding incoming APDU");
1134 fprintf(stderr, "[Near %d]\n", odr_offset(in));
1135 fprintf(stderr, "Packet dump:\n---------\n");
1136 odr_dumpBER(stderr, netbuffer, res);
1137 fprintf(stderr, "---------\n");
1141 if (!z_APDU(print, &apdu, 0))
1143 odr_perror(print, "Failed to print incoming APDU");
1150 case Z_APDU_initResponse:
1151 process_initResponse(apdu->u.initResponse);
1153 case Z_APDU_searchResponse:
1154 process_searchResponse(apdu->u.searchResponse);
1156 case Z_APDU_scanResponse:
1157 process_scanResponse(apdu->u.scanResponse);
1159 case Z_APDU_presentResponse:
1160 printf("Received presentResponse.\n");
1162 *apdu->u.presentResponse->numberOfRecordsReturned;
1163 if (apdu->u.presentResponse->records)
1164 display_records(apdu->u.presentResponse->records);
1166 printf("No records.\n");
1169 printf("Target has closed the association.\n");
1170 process_close(apdu->u.close);
1173 printf("Received unknown APDU type (%d).\n",
1180 while (cs_more(conn));
1186 int main(int argc, char **argv)
1193 if (*marcdump_file && !(marcdump = fopen(marcdump_file, "a")))
1195 perror(marcdump_file);