Better diagnostics.
[yaz-moved-to-github.git] / client / client.c
1 /*
2  * Copyright (c) 1995, Index Data.
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: client.c,v $
7  * Revision 1.10  1995-06-06 14:56:58  quinn
8  * Better diagnostics.
9  *
10  * Revision 1.9  1995/06/06  08:15:19  quinn
11  * Cosmetic.
12  *
13  * Revision 1.8  1995/06/05  10:52:22  quinn
14  * Added SCAN.
15  *
16  * Revision 1.7  1995/06/02  09:50:09  quinn
17  * Smallish.
18  *
19  * Revision 1.6  1995/05/31  08:29:21  quinn
20  * Nothing significant.
21  *
22  * Revision 1.5  1995/05/29  08:10:47  quinn
23  * Moved oid.c to util.
24  *
25  * Revision 1.4  1995/05/22  15:30:13  adam
26  * Client uses prefix query notation.
27  *
28  * Revision 1.3  1995/05/22  15:06:53  quinn
29  * *** empty log message ***
30  *
31  * Revision 1.2  1995/05/22  14:56:40  quinn
32  * *** empty log message ***
33  *
34  * Revision 1.1  1995/05/22  11:30:31  quinn
35  * Added prettier client.
36  *
37  *
38  */
39
40 /*
41  * This is the obligatory little toy client, whose primary purpose is
42  * to illustrate the use of the YAZ service-level API.
43  */
44
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <sys/time.h>
48 #include <assert.h>
49 #ifdef _AIX
50 #include <sys/select.h>
51 #endif
52
53 #include <comstack.h>
54 #include <tcpip.h>
55 #ifdef USE_XTIMOSI
56 #include <xmosi.h>
57 #endif
58
59 #include <proto.h>
60
61 #include <marcdisp.h>
62
63 #ifdef RPN_QUERY
64 #ifdef PREFIX_QUERY
65 #include <pquery.h>
66 #else
67 #include <yaz-ccl.h>
68 #endif
69 #endif
70
71 #include "../version.h"
72
73 #define C_PROMPT "Z> "
74
75 static ODR out, in, print;              /* encoding and decoding streams */
76 static COMSTACK conn = 0;               /* our z-association */
77 static Z_IdAuthentication *auth = 0;    /* our current auth definition */
78 static char database[512] = "Default";  /* Database name */
79 static int setnumber = 0;               /* current result set number */
80 static int smallSetUpperBound = 0;
81 static int largeSetLowerBound = 1;
82 static int mediumSetPresentNumber = 0;
83 static int setno = 1;                   /* current set offset */
84 static int protocol = PROTO_Z3950;      /* current app protocol */
85 static ODR_MEM session_mem;                /* memory handle for init-response */
86 static Z_InitResponse *session = 0;        /* session parameters */
87 static char last_scan[512] = "0";
88 static char last_cmd[100] = "?";
89 #ifdef RPN_QUERY
90 #ifndef PREFIX_QUERY
91 static CCL_bibset bibset;               /* CCL bibset handle */
92 #endif
93 #endif
94
95 static void send_apdu(Z_APDU *a)
96 {
97     char *buf;
98     int len;
99
100     if (!z_APDU(out, &a, 0))
101     {
102         odr_perror(out, "Encoding APDU");
103         exit(1);
104     }
105     buf = odr_getbuf(out, &len, 0);
106     odr_reset(out); /* release the APDU */
107     if (cs_put(conn, buf, len) < 0)
108     {
109         fprintf(stderr, "cs_put: %s", cs_errlist[cs_errno(conn)]);
110         exit(1);
111     }
112 }
113
114 /* INIT SERVICE ------------------------------- */
115
116 static void send_initRequest()
117 {
118     Z_APDU *apdu = zget_APDU(out, Z_APDU_initRequest);
119     Z_InitRequest *req = apdu->u.initRequest;
120
121     ODR_MASK_SET(req->options, Z_Options_search);
122     ODR_MASK_SET(req->options, Z_Options_present);
123     ODR_MASK_SET(req->options, Z_Options_namedResultSets);
124     ODR_MASK_SET(req->options, Z_Options_triggerResourceCtrl);
125     ODR_MASK_SET(req->options, Z_Options_scan);
126
127     ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_1);
128     ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_2);
129
130     req->idAuthentication = auth;
131
132     send_apdu(apdu);
133     printf("Sent initrequest.\n");
134 }
135
136 static int process_initResponse(Z_InitResponse *res)
137 {
138     /* save session parameters for later use */
139     session_mem = odr_extract_mem(in);
140     session = res;
141
142     if (!*res->result)
143         printf("Connection rejected by target.\n");
144     else
145         printf("Connection accepted by target.\n");
146     if (res->implementationId)
147         printf("ID     : %s\n", res->implementationId);
148     if (res->implementationName)
149         printf("Name   : %s\n", res->implementationName);
150     if (res->implementationVersion)
151         printf("Version: %s\n", res->implementationVersion);
152     if (res->userInformationField)
153     {
154         printf("UserInformationfield:\n");
155         if (!odr_external(print, (Odr_external**)&res-> userInformationField,
156             0))
157         {
158             odr_perror(print, "Printing userinfo\n");
159             odr_reset(print);
160         }
161         if (res->userInformationField->which == ODR_EXTERNAL_octet)
162         {
163             printf("Guessing visiblestring:\n");
164             printf("'%s'\n", res->userInformationField->u. octet_aligned->buf);
165         }
166     }
167     return 0;
168 }
169
170 int cmd_open(char *arg)
171 {
172     void *add;
173     char type[100], addr[100];
174     CS_TYPE t;
175
176     if (conn)
177     {
178         printf("Already connected.\n");
179         return 0;
180     }
181     if (!*arg || sscanf(arg, "%[^:]:%s", type, addr) < 2)
182     {
183         fprintf(stderr, "Usage: open (osi|tcp) ':' [tsel '/']host[':'port]\n");
184         return 0;
185     }
186 #ifdef USE_XTIMOSI
187     if (!strcmp(type, "osi"))
188     {
189         if (!(add = mosi_strtoaddr(addr)))
190         {
191             perror(arg);
192             return 0;
193         }
194         t = mosi_type;
195         protocol = PROTO_SR;
196     }
197     else
198 #endif
199     if (!strcmp(type, "tcp"))
200     {
201         if (!(add = tcpip_strtoaddr(addr)))
202         {
203             perror(arg);
204             return 0;
205         }
206         t = tcpip_type;
207         protocol = PROTO_Z3950;
208     }
209     else
210     {
211         fprintf(stderr, "Bad type: %s\n", type);
212         return 0;
213     }
214     if (!(conn = cs_create(t, 1, protocol)))
215     {
216         perror("cs_create");
217         return 0;
218     }
219     printf("Connecting...");
220     fflush(stdout);
221     if (cs_connect(conn, add) < 0)
222     {
223         perror("connect");
224         cs_close(conn);
225         conn = 0;
226         return 0;
227     }
228     printf("Ok.\n");
229     send_initRequest();
230     return 2;
231 }
232
233 int cmd_authentication(char *arg)
234 {
235     static Z_IdAuthentication au;
236     static char open[256];
237
238     if (!*arg)
239     {
240         printf("Auth field set to null\n");
241         auth = 0;
242         return 1;
243     }
244     auth = &au;
245     au.which = Z_IdAuthentication_open;
246     au.u.open = open;
247     strcpy(open, arg);
248     return 1;
249 }
250
251 /* SEARCH SERVICE ------------------------------ */
252
253 void display_record(Z_DatabaseRecord *p)
254 {
255     Odr_external *r = (Odr_external*) p;
256
257     if (r->direct_reference)
258     {
259         oident *ent = oid_getentbyoid(r->direct_reference);
260
261         printf("Record type: ");
262         if (ent)
263             printf("%s\n", ent->desc);
264         else if (!odr_oid(print, &r->direct_reference, 0))
265         {
266             odr_perror(print, "print oid");
267             odr_reset(print);
268         }
269     }
270 #if 1
271     if (r->which == ODR_EXTERNAL_octet && p->u.octet_aligned->len)
272     {
273 #if 1
274         marc_display ((char*)p->u.octet_aligned->buf, stdout);
275 #else
276         FILE *ofi = fopen("dump", "a");
277         assert(ofi);
278         fwrite(p->u.octet_aligned->buf, 1, p->u.octet_aligned->len, ofi);
279         fclose(ofi);
280         printf("dumped record\n");
281 #endif
282     }
283     else
284     {
285         printf("Unknown record representation.\n");
286         if (!odr_external(print, &r, 0))
287         {
288             odr_perror(print, "Printing external");
289             odr_reset(print);
290         }
291     }
292 #endif
293 }
294
295 static void display_diagrec(Z_DiagRec *p)
296 {
297     oident *ent;
298
299     printf("Diagnostic message from database.\n");
300     if (!(ent = oid_getentbyoid(p->diagnosticSetId)) ||
301         ent->class != CLASS_DIAGSET || ent->value != VAL_BIB1)
302         printf("Missing or unknown diagset\n");
303     printf("Error condition: %d", *p->condition);
304     printf(" -- %s\n", p->addinfo ? p->addinfo : "");
305 }
306
307 static void display_nameplusrecord(Z_NamePlusRecord *p)
308 {
309     if (p->databaseName)
310         printf("[%s]", p->databaseName);
311     if (p->which == Z_NamePlusRecord_surrogateDiagnostic)
312         display_diagrec(p->u.surrogateDiagnostic);
313     else
314         display_record(p->u.databaseRecord);
315 }
316
317 static void display_records(Z_Records *p)
318 {
319     int i;
320
321     if (p->which == Z_Records_NSD)
322         display_diagrec(p->u.nonSurrogateDiagnostic);
323     else
324     {
325         printf("Records: %d\n", p->u.databaseOrSurDiagnostics->num_records);
326         for (i = 0; i < p->u.databaseOrSurDiagnostics->num_records; i++)
327             display_nameplusrecord(p->u.databaseOrSurDiagnostics->records[i]);
328     }
329 }
330
331 static int send_searchRequest(char *arg)
332 {
333     Z_APDU *apdu = zget_APDU(out, Z_APDU_searchRequest);
334     Z_SearchRequest *req = apdu->u.searchRequest;
335     char *databaseNames = database;
336     Z_Query query;
337 #ifdef RPN_QUERY
338 #ifndef PREFIX_QUERY
339     struct ccl_rpn_node *rpn;
340     int error, pos;
341 #endif
342 #endif
343     char setstring[100];
344 #ifdef RPN_QUERY
345     Z_RPNQuery *RPNquery;
346     oident bib1;
347 #else
348     Odr_oct ccl_query;
349 #endif
350
351 #ifdef RPN_QUERY
352 #ifndef PREFIX_QUERY
353     rpn = ccl_find_str(bibset, arg, &error, &pos);
354     if (error)
355     {
356         printf("CCL ERROR: %s\n", ccl_err_msg(error));
357         return 0;
358     }
359 #endif
360 #endif
361
362     if (!strcmp(arg, "@big")) /* strictly for troublemaking */
363     {
364         static unsigned char big[2100];
365         static Odr_oct bigo;
366
367         /* send a very big referenceid to test transport stack etc. */
368         memset(big, 'A', 2100);
369         bigo.len = bigo.size = 2100;
370         bigo.buf = big;
371         req->referenceId = &bigo;
372     }
373
374     if (setnumber >= 0)
375     {
376         sprintf(setstring, "%d", ++setnumber);
377         req->resultSetName = setstring;
378     }
379     *req->smallSetUpperBound = smallSetUpperBound;
380     *req->largeSetLowerBound = largeSetLowerBound;
381     *req->mediumSetPresentNumber = mediumSetPresentNumber;
382     req->num_databaseNames = 1;
383     req->databaseNames = &databaseNames;
384
385     req->query = &query;
386
387 #ifdef RPN_QUERY
388     query.which = Z_Query_type_1;
389
390 #ifndef PREFIX_QUERY
391     assert((RPNquery = ccl_rpn_query(rpn)));
392 #else
393     RPNquery = p_query_rpn (out, arg);
394     if (!RPNquery)
395     {
396         printf("Prefix query error\n");
397         return 0;
398     }
399 #endif
400     bib1.proto = protocol;
401     bib1.class = CLASS_ATTSET;
402     bib1.value = VAL_BIB1;
403     RPNquery->attributeSetId = oid_getoidbyent(&bib1);
404     query.u.type_1 = RPNquery;
405 #else
406     query.which = Z_Query_type_2;
407     query.u.type_2 = &ccl_query;
408     ccl_query.buf = (unsigned char*) arg;
409     ccl_query.len = strlen(arg);
410 #endif
411
412     send_apdu(apdu);
413     setno = 1;
414     printf("Sent searchRequest.\n");
415     return 2;
416 }
417
418 static int process_searchResponse(Z_SearchResponse *res)
419 {
420     if (*res->searchStatus)
421         printf("Search was a success.\n");
422     else
423         printf("Search was a bloomin' failure.\n");
424     printf("Number of hits: %d, setno %d\n",
425         *res->resultCount, setnumber);
426     printf("records returned: %d\n",
427         *res->numberOfRecordsReturned);
428     setno += *res->numberOfRecordsReturned;
429     if (res->records)
430         display_records(res->records);
431     return 0;
432 }
433
434 static int cmd_find(char *arg)
435 {
436     if (!*arg)
437     {
438         printf("Find what?\n");
439         return 0;
440     }
441     if (!conn)
442     {
443         printf("Not connected yet\n");
444         return 0;
445     }
446     if (!send_searchRequest(arg))
447         return 0;
448     return 2;
449 }
450
451 static int cmd_ssub(char *arg)
452 {
453     if (!(smallSetUpperBound = atoi(arg)))
454         return 0;
455     return 1;
456 }
457
458 static int cmd_lslb(char *arg)
459 {
460     if (!(largeSetLowerBound = atoi(arg)))
461         return 0;
462     return 1;
463 }
464
465 static int cmd_mspn(char *arg)
466 {
467     if (!(mediumSetPresentNumber = atoi(arg)))
468         return 0;
469     return 1;
470 }
471
472 static int cmd_status(char *arg)
473 {
474     printf("smallSetUpperBound: %d\n", smallSetUpperBound);
475     printf("largeSetLowerBound: %d\n", largeSetLowerBound);
476     printf("mediumSetPresentNumber: %d\n", mediumSetPresentNumber);
477     return 1;
478 }
479
480 static int cmd_base(char *arg)
481 {
482     if (!*arg)
483     {
484         printf("Usage: base <database>\n");
485         return 0;
486     }
487     strcpy(database, arg);
488     return 1;
489 }
490
491 static int cmd_setnames(char *arg)
492 {
493     if (setnumber < 0)
494     {
495         printf("Set numbering enabled.\n");
496         setnumber = 0;
497     }
498     else
499     {
500         printf("Set numbering disabled.\n");
501         setnumber = -1;
502     }
503     return 1;
504 }
505
506 /* PRESENT SERVICE ----------------------------- */
507
508 static int send_presentRequest(char *arg)
509 {
510     Z_APDU *apdu = zget_APDU(out, Z_APDU_presentRequest);
511     Z_PresentRequest *req = apdu->u.presentRequest;
512     int nos = 1;
513     char *p;
514     char setstring[100];
515
516     if ((p = strchr(arg, '+')))
517     {
518         nos = atoi(p + 1);
519         *p = 0;
520     }
521     if (*arg)
522         setno = atoi(arg);
523
524     if (setnumber >= 0)
525     {
526         sprintf(setstring, "%d", setnumber);
527         req->resultSetId = setstring;
528     }
529     req->resultSetStartPoint = &setno;
530     req->numberOfRecordsRequested = &nos;
531     send_apdu(apdu);
532     printf("Sent presentRequest (%d+%d).\n", setno, nos);
533     return 2;
534 }
535
536 static int cmd_show(char *arg)
537 {
538     if (!send_presentRequest(arg))
539         return 0;
540     return 2;
541 }
542
543 int cmd_quit(char *arg)
544 {
545     printf("See you later, alligator.\n");
546     exit(0);
547 }
548
549 int cmd_cancel(char *arg)
550 {
551     Z_APDU *apdu = zget_APDU(out, Z_APDU_triggerResourceControlRequest);
552     Z_TriggerResourceControlRequest *req =
553         apdu->u.triggerResourceControlRequest;
554     bool_t false = 0;
555     
556     if (!session)
557     {
558         printf("Session not initialized yet\n");
559         return 0;
560     }
561     if (!ODR_MASK_GET(session->options, Z_Options_triggerResourceCtrl))
562     {
563         printf("Target doesn't support cancel (trigger resource ctrl)\n");
564         return 0;
565     }
566     *req->requestedAction = Z_TriggerResourceCtrl_cancel;
567     req->resultSetWanted = &false;
568
569     send_apdu(apdu);
570     printf("Sent cancel request\n");
571     return 2;
572 }
573
574 int send_scanrequest(char *string, int pp, int num)
575 {
576     Z_APDU *apdu = zget_APDU(out, Z_APDU_scanRequest);
577     Z_ScanRequest *req = apdu->u.scanRequest;
578     char *db = database;
579     oident attset;
580
581     req->num_databaseNames = 1;
582     req->databaseNames = &db;
583     attset.proto = protocol;
584     attset.class = CLASS_ATTSET;
585     attset.value = VAL_BIB1;
586     req->attributeSet = oid_getoidbyent(&attset);
587     req->termListAndStartPoint = p_query_scan(out, string);
588     req->numberOfTermsRequested = &num;
589     req->preferredPositionInResponse = &pp;
590     send_apdu(apdu);
591     return 2;
592 }
593
594 void display_term(Z_TermInfo *t)
595 {
596     if (t->term->which == Z_Term_general)
597     {
598         printf("%.*s (%d)\n", t->term->u.general->len, t->term->u.general->buf,
599             t->globalOccurrences ? *t->globalOccurrences : -1);
600         sprintf(last_scan, "%.*s", t->term->u.general->len,
601             t->term->u.general->buf);
602     }
603     else
604         printf("Term type not general.\n");
605 }
606
607 void process_scanResponse(Z_ScanResponse *res)
608 {
609     int i;
610
611     printf("SCAN: %d entries, position=%d\n", *res->numberOfEntriesReturned,
612         *res->positionOfTerm);
613     if (*res->scanStatus != Z_Scan_success)
614         printf("Scan returned code %d\n", *res->scanStatus);
615     if (!res->entries)
616         return;
617     if (res->entries->which == Z_ListEntries_entries)
618     {
619         Z_Entries *ent = res->entries->u.entries;
620
621         for (i = 0; i < ent->num_entries; i++)
622             if (ent->entries[i]->which == Z_Entry_termInfo)
623             {
624                 printf("%c ", i + 1 == *res->positionOfTerm ? '*' : ' ');
625                 display_term(ent->entries[i]->u.termInfo);
626             }
627             else
628                 display_diagrec(ent->entries[i]->u.surrogateDiagnostic);
629     }
630     else
631         display_diagrec(res->entries->u.nonSurrogateDiagnostics->diagRecs[0]);
632 }
633
634 int cmd_scan(char *arg)
635 {
636     if (!session)
637     {
638         printf("Session not initialized yet\n");
639         return 0;
640     }
641     if (!ODR_MASK_GET(session->options, Z_Options_scan))
642     {
643         printf("Target doesn't support scan\n");
644         return 0;
645     }
646     if (*arg)
647     {
648         if (send_scanrequest(arg, 5, 19) < 0)
649             return 0;
650     }
651     else
652         if (send_scanrequest(last_scan, 1, 19) < 0)
653             return 0;
654     return 2;
655 }
656
657 static void initialize(void)
658 {
659 #ifdef RPN_QUERY
660 #ifndef PREFIX_QUERY
661     FILE *inf;
662 #endif
663 #endif
664
665     if (!(out = odr_createmem(ODR_ENCODE)) ||
666         !(in = odr_createmem(ODR_DECODE)) ||
667         !(print = odr_createmem(ODR_PRINT)))
668     {
669         fprintf(stderr, "failed to allocate ODR streams\n");
670         exit(1);
671     }
672     setvbuf(stdout, 0, _IONBF, 0);
673
674 #ifdef RPN_QUERY
675 #ifndef PREFIX_QUERY
676     bibset = ccl_qual_mk (); 
677     inf = fopen ("default.bib", "r");
678     if (inf)
679     {
680         ccl_qual_file (bibset, inf);
681         fclose (inf);
682     }
683 #endif
684 #endif
685 }
686
687 static int client(void)
688 {
689     static struct {
690         char *cmd;
691         int (*fun)(char *arg);
692         char *ad;
693     } cmd[] = {
694         {"open", cmd_open, "('tcp'|'osi')':'[<TSEL>'/']<HOST>[':'<PORT>]"},
695         {"quit", cmd_quit, ""},
696         {"find", cmd_find, "<CCL-QUERY>"},
697         {"base", cmd_base, "<BASE-NAME>"},
698         {"show", cmd_show, "<REC#>['+'<#RECS>]"},
699         {"scan", cmd_scan, "<TERM>"},
700         {"authentication", cmd_authentication, "<ACCTSTRING>"},
701         {"lslb", cmd_lslb, "<largeSetLowerBound>"},
702         {"ssub", cmd_ssub, "<smallSetUpperBound>"},
703         {"mspn", cmd_mspn, "<mediumSetPresentNumber>"},
704         {"status", cmd_status, ""},
705         {"setnames", cmd_setnames, ""},
706         {"cancel", cmd_cancel, ""},
707         {0,0}
708     };
709     char *netbuffer= 0;
710     int netbufferlen = 0;
711     int i;
712     Z_APDU *apdu;
713
714     while (1)
715     {
716         int res;
717         fd_set input;
718         char line[1024], word[1024], arg[1024];
719
720         FD_ZERO(&input);
721         FD_SET(0, &input);
722         if (conn)
723             FD_SET(cs_fileno(conn), &input);
724         if ((res = select(20, &input, 0, 0, 0)) < 0)
725         {
726             perror("select");
727             exit(1);
728         }
729         if (!res)
730             continue;
731         if (FD_ISSET(0, &input))
732         {
733             /* quick & dirty way to get a command line. */
734             if (!gets(line))
735                 break;
736             if ((res = sscanf(line, "%s %[^;]", word, arg)) <= 0)
737             {
738                 strcpy(word, last_cmd);
739                 *arg = '\0';
740             }
741             else if (res == 1)
742                 *arg = 0;
743             strcpy(last_cmd, word);
744             for (i = 0; cmd[i].cmd; i++)
745                 if (!strncmp(cmd[i].cmd, word, strlen(word)))
746                 {
747                     res = (*cmd[i].fun)(arg);
748                     break;
749                 }
750             if (!cmd[i].cmd) /* dump our help-screen */
751             {
752                 printf("Unknown command: %s.\n", word);
753                 printf("Currently recognized commands:\n");
754                 for (i = 0; cmd[i].cmd; i++)
755                     printf("   %s %s\n", cmd[i].cmd, cmd[i].ad);
756                 res = 1;
757             }
758             if (res < 2)
759                 printf(C_PROMPT);
760         }
761         if (conn && FD_ISSET(cs_fileno(conn), &input))
762         {
763             do
764             {
765                 if ((res = cs_get(conn, &netbuffer, &netbufferlen)) < 0)
766                 {
767                     perror("cs_get");
768                     exit(1);
769                 }
770                 if (!res)
771                 {
772                     printf("Target closed connection.\n");
773                     exit(1);
774                 }
775                 odr_reset(in); /* release APDU from last round */
776                 odr_setbuf(in, netbuffer, res, 0);
777                 if (!z_APDU(in, &apdu, 0))
778                 {
779                     odr_perror(in, "Decoding incoming APDU");
780                     exit(1);
781                 }
782 #if 0
783                 if (!z_APDU(print, &apdu, 0))
784                 {
785                     odr_perror(print, "Failed to print incoming APDU");
786                     odr_reset(print);
787                     continue;
788                 }
789 #endif
790                 switch(apdu->which)
791                 {
792                     case Z_APDU_initResponse:
793                         process_initResponse(apdu->u.initResponse);
794                         break;
795                     case Z_APDU_searchResponse:
796                         process_searchResponse(apdu->u.searchResponse);
797                         break;
798                     case Z_APDU_scanResponse:
799                         process_scanResponse(apdu->u.scanResponse);
800                         break;
801                     case Z_APDU_presentResponse:
802                         printf("Received presentResponse.\n");
803                         setno +=
804                             *apdu->u.presentResponse->numberOfRecordsReturned;
805                         if (apdu->u.presentResponse->records)
806                             display_records(apdu->u.presentResponse->records);
807                         else
808                             printf("No records.\n");
809                         break;
810                     default:
811                         printf("Received unknown APDU type (%d).\n", 
812                             apdu->which);
813                         exit(1);
814                 }
815                 printf("Z> ");
816                 fflush(stdout);
817             }
818             while (cs_more(conn));
819         }
820     }
821     return 0;
822 }
823
824 int main(int argc, char **argv)
825 {
826     initialize();
827     if (argc > 1)
828         cmd_open(argv[1]);
829     else
830         printf(C_PROMPT);
831     return client();
832 }