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