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