MARC8 decoding for yaz_iconv.
[yaz-moved-to-github.git] / client / client.c
1 /* 
2  * Copyright (c) 1995-2002, Index Data
3  * See the file LICENSE for details.
4  *
5  * $Id: client.c,v 1.176 2002-12-16 13:13:53 adam Exp $
6  */
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #if HAVE_LOCALE_H
11 #include <locale.h>
12 #endif
13
14 #if HAVE_LANGINFO_H
15 #include <langinfo.h>
16 #endif
17
18 #include <time.h>
19 #include <ctype.h>
20
21 #ifdef WIN32
22 #include <io.h>
23 #define S_ISREG(x) (x & _S_IFREG)
24 #define S_ISDIR(x) (x & _S_IFDIR)
25 #endif
26
27 #include <yaz/yaz-util.h>
28
29 #include <yaz/comstack.h>
30
31 #include <yaz/proto.h>
32 #include <yaz/marcdisp.h>
33 #include <yaz/diagbib1.h>
34 #include <yaz/otherinfo.h>
35 #include <yaz/charneg.h>
36
37 #include <yaz/pquery.h>
38 #include <yaz/sortspec.h>
39
40 #include <yaz/ill.h>
41
42 #include <yaz/yaz-ccl.h>
43
44 #if HAVE_READLINE_READLINE_H
45 #include <readline/readline.h>
46 #include <unistd.h>
47 #endif
48 #if HAVE_READLINE_HISTORY_H
49 #include <readline/history.h>
50 #endif
51
52 #include <sys/stat.h>
53
54
55 #include "admin.h"
56 #include "tabcomplete.h"
57
58 #define C_PROMPT "Z> "
59
60 static char *codeset = 0;               /* character set for output */
61
62 static ODR out, in, print;              /* encoding and decoding streams */
63 static FILE *apdu_file = 0;
64 static COMSTACK conn = 0;               /* our z-association */
65 static Z_IdAuthentication *auth = 0;    /* our current auth definition */
66 char *databaseNames[128];
67 int num_databaseNames = 0;
68 static Z_External *record_last = 0;
69 static int setnumber = -1;              /* current result set number */
70 static int smallSetUpperBound = 0;
71 static int largeSetLowerBound = 1;
72 static int mediumSetPresentNumber = 0;
73 static Z_ElementSetNames *elementSetNames = 0; 
74 static int setno = 1;                   /* current set offset */
75 static enum oid_proto protocol = PROTO_Z3950;      /* current app protocol */
76 static enum oid_value recordsyntax = VAL_USMARC;
77 static enum oid_value schema = VAL_NONE;
78 static int sent_close = 0;
79 static NMEM session_mem = NULL;         /* memory handle for init-response */
80 static Z_InitResponse *session = 0;     /* session parameters */
81 static char last_scan_line[512] = "0";
82 static char last_scan_query[512] = "0";
83 static char ccl_fields[512] = "default.bib";
84 static char* esPackageName = 0;
85 static char* yazProxy = 0;
86 static int kilobytes = 1024;
87 static char* yazCharset = 0;
88 static char* yazLang = 0;
89
90
91 static char last_cmd[32] = "?";
92 static FILE *marcdump = 0;
93 static char *refid = NULL;
94 static char *last_open_command = NULL;
95 static int auto_reconnect = 0;
96
97 typedef enum {
98     QueryType_Prefix,
99     QueryType_CCL,
100     QueryType_CCL2RPN
101 } QueryType;
102
103 static QueryType queryType = QueryType_Prefix;
104
105 static CCL_bibset bibset;               /* CCL bibset handle */
106
107 #if HAVE_READLINE_COMPLETION_OVER
108
109 #else
110 /* readline doesn't have this var. Define it ourselves. */
111 int rl_attempted_completion_over = 0;
112 #endif
113
114 /* set this one to 1, to avoid decode of unknown MARCs  */
115 #define AVOID_MARC_DECODE 1
116
117 /* nice helper macro as extensive tabbing gives spaces at the en of the args lines */
118 #define REMOVE_TAILING_BLANKS(a) {\
119   char* args_end=(a)+strlen(a)-1; \
120   while(isspace(*args_end)) {*args_end=0;--args_end;}; \
121   }
122
123 #define maxOtherInfosSupported 10
124 struct {
125     int oidval;
126     char* value;
127 } extraOtherInfos[maxOtherInfosSupported];
128         
129
130 void process_cmd_line(char* line);
131 char ** readline_completer(char *text, int start, int end);
132 char *command_generator(const char *text, int state);
133 char** curret_global_list=NULL;
134 int cmd_register_tab(char* arg);
135
136 ODR getODROutputStream()
137 {
138     return out;
139 }
140
141 const char* query_type_as_string(QueryType q) 
142 {
143     switch (q) { 
144     case QueryType_Prefix: return "prefix (RPN sent to server)";
145     case QueryType_CCL: return "CCL (CCL sent to server) ";
146     case QueryType_CCL2RPN: return "CCL -> RPN (RPN sent to server)";
147     default: 
148         return "unknown Query type internal yaz-client error";
149     }
150 }
151
152
153 void do_hex_dump(char* buf,int len) 
154 {
155 #if 0 
156     int i,x;
157     for( i=0; i<len ; i=i+16 ) 
158     {                   
159         printf(" %4.4d ",i);
160         for(x=0 ; i+x<len && x<16; ++x) 
161         {
162             printf("%2.2X ",(unsigned int)((unsigned char)buf[i+x]));
163         }
164         printf("\n");
165     }
166 #endif
167 }
168
169 void add_otherInfos(Z_APDU *a) 
170 {
171     Z_OtherInformation **oi;
172     int i;
173                 
174     yaz_oi_APDU(a, &oi);
175     for(i=0; i<maxOtherInfosSupported; ++i) 
176     {
177         if(extraOtherInfos[i].oidval != -1) 
178             yaz_oi_set_string_oidval(oi, out, extraOtherInfos[i].oidval, 1, extraOtherInfos[i].value);
179     }   
180 }
181
182 void send_apdu(Z_APDU *a)
183 {
184     char *buf;
185     int len;
186     
187     add_otherInfos(a);
188     
189     if (apdu_file)
190     {
191         z_APDU(print, &a, 0, 0);
192         odr_reset(print);
193     }
194     if (!z_APDU(out, &a, 0, 0))
195     {
196         odr_perror(out, "Encoding APDU");
197         exit(1);
198     }
199     buf = odr_getbuf(out, &len, 0);
200     /* printf ("sending APDU of size %d\n", len); */
201     if (cs_put(conn, buf, len) < 0)
202     {
203         fprintf(stderr, "cs_put: %s", cs_errmsg(cs_errno(conn)));
204         exit(1);
205     }
206     
207     do_hex_dump(buf,len);
208     odr_reset(out); /* release the APDU structure  */
209 }
210
211 static void print_stringn(const unsigned char *buf, size_t len)
212 {
213     size_t i;
214     for (i = 0; i<len; i++)
215         if ((buf[i] <= 126 && buf[i] >= 32) || strchr ("\n\r\t\f", buf[i]))
216             printf ("%c", buf[i]);
217         else
218             printf ("\\X%02X", buf[i]);
219 }
220
221 static void print_refid (Z_ReferenceId *id)
222 {
223     if (id)
224     {
225         printf ("Reference Id: ");
226         print_stringn (id->buf, id->len);
227         printf ("\n");
228     }
229 }
230
231 static Z_ReferenceId *set_refid (ODR out)
232 {
233     Z_ReferenceId *id;
234     if (!refid)
235         return 0;
236     id = (Z_ReferenceId *) odr_malloc (out, sizeof(*id));
237     id->size = id->len = strlen(refid);
238     id->buf = (unsigned char *) odr_malloc (out, id->len);
239     memcpy (id->buf, refid, id->len);
240     return id;
241 }   
242
243 /* INIT SERVICE ------------------------------- */
244
245 static void send_initRequest(const char* type_and_host)
246 {
247     Z_APDU *apdu = zget_APDU(out, Z_APDU_initRequest);
248     Z_InitRequest *req = apdu->u.initRequest;
249
250     ODR_MASK_SET(req->options, Z_Options_search);
251     ODR_MASK_SET(req->options, Z_Options_present);
252     ODR_MASK_SET(req->options, Z_Options_namedResultSets);
253     ODR_MASK_SET(req->options, Z_Options_triggerResourceCtrl);
254     ODR_MASK_SET(req->options, Z_Options_scan);
255     ODR_MASK_SET(req->options, Z_Options_sort);
256     ODR_MASK_SET(req->options, Z_Options_extendedServices);
257     ODR_MASK_SET(req->options, Z_Options_delSet);
258
259     ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_1);
260     ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_2);
261     ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_3);
262
263     *req->maximumRecordSize = 1024*kilobytes;
264     *req->preferredMessageSize = 1024*kilobytes;
265
266     req->idAuthentication = auth;
267
268     req->referenceId = set_refid (out);
269
270     if (yazProxy) 
271         yaz_oi_set_string_oidval(&req->otherInfo, out, VAL_PROXY,
272         1, type_and_host);
273     
274     if (yazCharset || yazLang) {
275         Z_OtherInformation **p;
276         Z_OtherInformationUnit *p0;
277         
278         yaz_oi_APDU(apdu, &p);
279         
280         if ((p0=yaz_oi_update(p, out, NULL, 0, 0))) {
281                 ODR_MASK_SET(req->options, Z_Options_negotiationModel);
282                 
283                 p0->which = Z_OtherInfo_externallyDefinedInfo;
284                 p0->information.externallyDefinedInfo =
285                         yaz_set_proposal_charneg(out,
286                                 (const char**)&yazCharset, (yazCharset)?1:0,
287                                 (const char**)&yazLang, (yazLang)?1:0, 1);
288         }
289     }
290     
291     send_apdu(apdu);
292     printf("Sent initrequest.\n");
293 }
294
295 static int process_initResponse(Z_InitResponse *res)
296 {
297     /* save session parameters for later use */
298     session_mem = odr_extract_mem(in);
299     session = res;
300
301     if (!*res->result)
302         printf("Connection rejected by target.\n");
303     else
304         printf("Connection accepted by target.\n");
305     if (res->implementationId)
306         printf("ID     : %s\n", res->implementationId);
307     if (res->implementationName)
308         printf("Name   : %s\n", res->implementationName);
309     if (res->implementationVersion)
310         printf("Version: %s\n", res->implementationVersion);
311     if (res->userInformationField)
312     {
313         printf("UserInformationfield:\n");
314         if (!z_External(print, (Z_External**)&res-> userInformationField,
315             0, 0))
316         {
317             odr_perror(print, "Printing userinfo\n");
318             odr_reset(print);
319         }
320         if (res->userInformationField->which == Z_External_octet)
321         {
322             printf("Guessing visiblestring:\n");
323             printf("'%s'\n", res->userInformationField->u. octet_aligned->buf);
324         }
325         odr_reset (print);
326     }
327     printf ("Options:");
328     if (ODR_MASK_GET(res->options, Z_Options_search))
329         printf (" search");
330     if (ODR_MASK_GET(res->options, Z_Options_present))
331         printf (" present");
332     if (ODR_MASK_GET(res->options, Z_Options_delSet))
333         printf (" delSet");
334     if (ODR_MASK_GET(res->options, Z_Options_resourceReport))
335         printf (" resourceReport");
336     if (ODR_MASK_GET(res->options, Z_Options_resourceCtrl))
337         printf (" resourceCtrl");
338     if (ODR_MASK_GET(res->options, Z_Options_accessCtrl))
339         printf (" accessCtrl");
340     if (ODR_MASK_GET(res->options, Z_Options_scan))
341         printf (" scan");
342     if (ODR_MASK_GET(res->options, Z_Options_sort))
343         printf (" sort");
344     if (ODR_MASK_GET(res->options, Z_Options_extendedServices))
345         printf (" extendedServices");
346     if (ODR_MASK_GET(res->options, Z_Options_level_1Segmentation))
347         printf (" level1Segmentation");
348     if (ODR_MASK_GET(res->options, Z_Options_level_2Segmentation))
349         printf (" level2Segmentation");
350     if (ODR_MASK_GET(res->options, Z_Options_concurrentOperations))
351         printf (" concurrentOperations");
352     if (ODR_MASK_GET(res->options, Z_Options_namedResultSets))
353     {
354         printf (" namedResultSets");
355         setnumber = 0;
356     }
357     if (ODR_MASK_GET(res->options, Z_Options_encapsulation))
358         printf (" encapsulation");
359     if (ODR_MASK_GET(res->options, Z_Options_resultCount))
360         printf (" resultCount");
361     if (ODR_MASK_GET(res->options, Z_Options_negotiationModel))
362         printf (" negotiationModel");
363     if (ODR_MASK_GET(res->options, Z_Options_duplicateDetection))
364         printf (" duplicateDetection");
365     if (ODR_MASK_GET(res->options, Z_Options_queryType104))
366         printf (" queryType104");
367     printf ("\n");
368     
369     if (ODR_MASK_GET(res->options, Z_Options_negotiationModel)) {
370     
371         Z_CharSetandLanguageNegotiation *p =
372                 yaz_get_charneg_record(res->otherInfo);
373         
374         if (p) {
375             
376             char *charset=NULL, *lang=NULL;
377             int selected;
378             
379             yaz_get_response_charneg(session_mem, p, &charset, &lang,
380                                      &selected);
381             
382             printf("Accepted character set : %s\n", charset);
383             printf("Accepted code language : %s\n", lang ? lang : "none");
384             printf("Accepted records in ...: %d\n", selected );
385         }
386     }
387     fflush (stdout);
388     return 0;
389 }
390
391 static int cmd_base(char *arg)
392 {
393     int i;
394     char *cp;
395
396     if (!*arg)
397     {
398         printf("Usage: base <database> <database> ...\n");
399         return 0;
400     }
401     for (i = 0; i<num_databaseNames; i++)
402         xfree (databaseNames[i]);
403     num_databaseNames = 0;
404     while (1)
405     {
406         char *cp1;
407         if (!(cp = strchr(arg, ' ')))
408             cp = arg + strlen(arg);
409         if (cp - arg < 1)
410             break;
411         databaseNames[num_databaseNames] = (char *)xmalloc (1 + cp - arg);
412         memcpy (databaseNames[num_databaseNames], arg, cp - arg);
413         databaseNames[num_databaseNames][cp - arg] = '\0';
414
415         for (cp1 = databaseNames[num_databaseNames]; *cp1 ; cp1++)
416             if (*cp1 == '+')
417                 *cp1 = ' ';
418         num_databaseNames++;
419
420         if (!*cp)
421             break;
422         arg = cp+1;
423     }
424     return 1;
425 }
426
427 void cmd_open_remember_last_open_command(char* arg, char* new_open_command)
428 {
429         if(last_open_command != arg) 
430         {
431                 if(last_open_command) xfree(last_open_command);
432                 last_open_command = xstrdup(new_open_command);
433         }
434 }
435
436 int cmd_open(char *arg)
437 {
438     void *add;
439     char type_and_host[101], base[101]; 
440     
441     if (conn)
442     {
443         printf("Already connected.\n");
444         
445         cs_close (conn);
446         conn = NULL;
447         if (session_mem)
448         {
449             nmem_destroy (session_mem);
450             session_mem = NULL;
451         }
452     }   
453
454     if (strncmp (arg, "unix:", 5) == 0)
455     {
456         base[0] = '\0';
457         conn = cs_create_host(arg, 1, &add);
458                 cmd_open_remember_last_open_command(arg,arg);
459     }
460     else
461     {
462         base[0] = '\0';
463         if (sscanf (arg, "%100[^/]/%100s", type_and_host, base) < 1)
464             return 0;
465                 
466                 cmd_open_remember_last_open_command(arg,type_and_host);
467         if (yazProxy) 
468             conn = cs_create_host(yazProxy, 1, &add);
469         else 
470             conn = cs_create_host(type_and_host, 1, &add);
471     }
472     if (!conn)
473     {
474         printf ("Couldn't create comstack\n");
475         return 0;
476     }
477     printf("Connecting...");
478     fflush(stdout);
479     if (cs_connect(conn, add) < 0)
480     {
481         printf ("error = %s\n", cs_strerror(conn));
482         if (conn->cerrno == CSYSERR)
483         {
484             char msg[256];
485             yaz_strerror(msg, sizeof(msg));
486             printf ("%s\n", msg);
487         }
488         cs_close(conn);
489         conn = 0;
490         return 0;
491     }
492     printf("Ok.\n");
493     
494     send_initRequest(type_and_host);
495     if (*base)
496         cmd_base (base);
497
498     return 2;
499 }
500
501
502 void try_reconnect() 
503 {
504
505         char* open_command;
506         
507         if(!( auto_reconnect && last_open_command) ) return ;
508
509         open_command = (char *) xmalloc (strlen(last_open_command)+6);
510         strcpy (open_command, "open ");
511         
512         strcat (open_command, last_open_command);
513
514         process_cmd_line(open_command);
515         
516         xfree(open_command);                            
517 }
518
519 int cmd_authentication(char *arg)
520 {
521     static Z_IdAuthentication au;
522     static char user[40], group[40], pass[40];
523     static Z_IdPass idPass;
524     int r;
525
526     if (!*arg)
527     {
528         printf("Auth field set to null\n");
529         auth = 0;
530         return 1;
531     }
532     r = sscanf (arg, "%39s %39s %39s", user, group, pass);
533     if (r == 0)
534     {
535         printf("Auth field set to null\n");
536         auth = 0;
537     }
538     if (r == 1)
539     {
540         auth = &au;
541         au.which = Z_IdAuthentication_open;
542         au.u.open = user;
543     }
544     if (r == 2)
545     {
546         auth = &au;
547         au.which = Z_IdAuthentication_idPass;
548         au.u.idPass = &idPass;
549         idPass.groupId = NULL;
550         idPass.userId = user;
551         idPass.password = group;
552     }
553     if (r == 3)
554     {
555         auth = &au;
556         au.which = Z_IdAuthentication_idPass;
557         au.u.idPass = &idPass;
558         idPass.groupId = group;
559         idPass.userId = user;
560         idPass.password = pass;
561     }
562     return 1;
563 }
564
565 /* SEARCH SERVICE ------------------------------ */
566 static void display_record(Z_External *r);
567
568 static void print_record(const unsigned char *buf, size_t len)
569 {
570     size_t i = len;
571     print_stringn (buf, len);
572     /* add newline if not already added ... */
573     if (i <= 0 || buf[i-1] != '\n')
574         printf ("\n");
575 }
576
577 static void display_record(Z_External *r)
578 {
579     oident *ent = oid_getentbyoid(r->direct_reference);
580
581     record_last = r;
582     /*
583      * Tell the user what we got.
584      */
585     if (r->direct_reference)
586     {
587         printf("Record type: ");
588         if (ent)
589             printf("%s\n", ent->desc);
590         else if (!odr_oid(print, &r->direct_reference, 0, 0))
591         {
592             odr_perror(print, "print oid");
593             odr_reset(print);
594         }
595     }
596     /* Check if this is a known, ASN.1 type tucked away in an octet string */
597     if (ent && r->which == Z_External_octet)
598     {
599         Z_ext_typeent *type = z_ext_getentbyref(ent->value);
600         void *rr;
601
602         if (type)
603         {
604             /*
605              * Call the given decoder to process the record.
606              */
607             odr_setbuf(in, (char*)r->u.octet_aligned->buf,
608                 r->u.octet_aligned->len, 0);
609             if (!(*type->fun)(in, (char **)&rr, 0, 0))
610             {
611                 odr_perror(in, "Decoding constructed record.");
612                 fprintf(stderr, "[Near %d]\n", odr_offset(in));
613                 fprintf(stderr, "Packet dump:\n---------\n");
614                 odr_dumpBER(stderr, (char*)r->u.octet_aligned->buf,
615                             r->u.octet_aligned->len);
616                 fprintf(stderr, "---------\n");
617                 
618                 /* note just ignores the error ant print the bytes form the octet_aligned later */
619             } else {
620                 /*
621                  * Note: we throw away the original, BER-encoded record here.
622                  * Do something else with it if you want to keep it.
623                  */
624                 r->u.sutrs = (Z_SUTRS *) rr; /* we don't actually check the type here. */
625                 r->which = type->what;
626             }
627         }
628     }
629     if (ent && ent->oclass != CLASS_RECSYN) 
630         return;
631     if (ent && ent->value == VAL_SOIF)
632         print_record((const unsigned char *) r->u.octet_aligned->buf,
633                      r->u.octet_aligned->len);
634     else if (r->which == Z_External_octet)
635     {
636         const char *octet_buf = (char*)r->u.octet_aligned->buf;
637         if (ent->value == VAL_TEXT_XML || ent->value == VAL_APPLICATION_XML ||
638             ent->value == VAL_HTML)
639         {
640             print_record((const unsigned char *) octet_buf,
641                          r->u.octet_aligned->len);
642         }
643         else if (ent->value == VAL_POSTSCRIPT)
644         {
645             int size = r->u.octet_aligned->len;
646             if (size > 100)
647                 size = 100;
648             print_record((const unsigned char *) octet_buf, size);
649         }
650         else
651         {
652             if ( 
653 #if AVOID_MARC_DECODE
654                 /* primitive check for a marc OID 5.1-29 except 16 */
655                 ent->oidsuffix[0] == 5 && ent->oidsuffix[1] < 30 &&
656                 ent->oidsuffix[1] != 16
657 #else
658                 1
659 #endif
660                 )
661             {
662                 char *result;
663                 int rlen;
664                 yaz_iconv_t cd = 0;
665                 yaz_marc_t mt = yaz_marc_create();
666                     
667                 if (yaz_marc_decode_buf(mt, octet_buf,r->u.octet_aligned->len,
668                                         &result, &rlen)> 0)
669                 {
670                     char *from = 0;
671                     if (ent->value == VAL_USMARC)
672                     {
673                         if (octet_buf[9] == 'a')
674                             from = "UTF-8";
675                         else
676                             from = "MARC8";
677                     }
678                     else
679                         from = "ISO-8859-1";
680
681                     if (codeset && from)
682                     {   
683                         printf ("convert from %s to %s\n", from, codeset);
684                         cd = yaz_iconv_open(codeset, from);
685                     }
686                     if (!cd)
687                         fwrite (result, 1, rlen, stdout);
688                     else
689                     {
690                         char outbuf[12];
691                         size_t inbytesleft = rlen;
692                         const char *inp = result;
693                         
694                         while (inbytesleft)
695                         {
696                             int i;
697                             size_t outbytesleft = sizeof(outbuf);
698                             char *outp = outbuf;
699                             size_t r = yaz_iconv (cd, (char**) &inp,
700                                                   &inbytesleft, 
701                                                   &outp, &outbytesleft);
702                             if (r == (size_t) (-1))
703                             {
704                                 int e = yaz_iconv_error(cd);
705                                 if (e != YAZ_ICONV_E2BIG)
706                                     break;
707                             }
708                             fwrite (outbuf, outp - outbuf, 1, stdout);
709                         }
710                     }
711                 }
712                 else
713                 {
714                     printf ("bad MARC. Dumping as it is:\n");
715                     print_record((const unsigned char*) octet_buf,
716                                   r->u.octet_aligned->len);
717                 }       
718                 yaz_marc_destroy(mt);
719                 if (cd)
720                     yaz_iconv_close(cd);
721             }
722             else
723             {
724                 print_record((const unsigned char*) octet_buf,
725                              r->u.octet_aligned->len);
726             }
727         }
728         if (marcdump)
729             fwrite (octet_buf, 1, r->u.octet_aligned->len, marcdump);
730     }
731     else if (ent && ent->value == VAL_SUTRS)
732     {
733         if (r->which != Z_External_sutrs)
734         {
735             printf("Expecting single SUTRS type for SUTRS.\n");
736             return;
737         }
738         print_record(r->u.sutrs->buf, r->u.sutrs->len);
739     }
740     else if (ent && ent->value == VAL_GRS1)
741     {
742         WRBUF w;
743         if (r->which != Z_External_grs1)
744         {
745             printf("Expecting single GRS type for GRS.\n");
746             return;
747         }
748         w = wrbuf_alloc();
749         yaz_display_grs1(w, r->u.grs1, 0);
750         puts (wrbuf_buf(w));
751         wrbuf_free(w, 1);
752     }
753     else 
754     {
755         printf("Unknown record representation.\n");
756         if (!z_External(print, &r, 0, 0))
757         {
758             odr_perror(print, "Printing external");
759             odr_reset(print);
760         }
761     }
762 }
763
764 static void display_diagrecs(Z_DiagRec **pp, int num)
765 {
766     int i;
767     oident *ent;
768     Z_DefaultDiagFormat *r;
769
770     printf("Diagnostic message(s) from database:\n");
771     for (i = 0; i<num; i++)
772     {
773         Z_DiagRec *p = pp[i];
774         if (p->which != Z_DiagRec_defaultFormat)
775         {
776             printf("Diagnostic record not in default format.\n");
777             return;
778         }
779         else
780             r = p->u.defaultFormat;
781         if (!(ent = oid_getentbyoid(r->diagnosticSetId)) ||
782             ent->oclass != CLASS_DIAGSET || ent->value != VAL_BIB1)
783             printf("Missing or unknown diagset\n");
784         printf("    [%d] %s", *r->condition, diagbib1_str(*r->condition));
785         switch (r->which)
786         {
787         case Z_DefaultDiagFormat_v2Addinfo:
788             printf (" -- v2 addinfo '%s'\n", r->u.v2Addinfo);
789             break;
790         case Z_DefaultDiagFormat_v3Addinfo:
791             printf (" -- v3 addinfo '%s'\n", r->u.v3Addinfo);
792             break;
793         }
794     }
795 }
796
797
798 static void display_nameplusrecord(Z_NamePlusRecord *p)
799 {
800     if (p->databaseName)
801         printf("[%s]", p->databaseName);
802     if (p->which == Z_NamePlusRecord_surrogateDiagnostic)
803         display_diagrecs(&p->u.surrogateDiagnostic, 1);
804     else if (p->which == Z_NamePlusRecord_databaseRecord)
805         display_record(p->u.databaseRecord);
806 }
807
808 static void display_records(Z_Records *p)
809 {
810     int i;
811
812     if (p->which == Z_Records_NSD)
813     {
814         Z_DiagRec dr, *dr_p = &dr;
815         dr.which = Z_DiagRec_defaultFormat;
816         dr.u.defaultFormat = p->u.nonSurrogateDiagnostic;
817         display_diagrecs (&dr_p, 1);
818     }
819     else if (p->which == Z_Records_multipleNSD)
820         display_diagrecs (p->u.multipleNonSurDiagnostics->diagRecs,
821                           p->u.multipleNonSurDiagnostics->num_diagRecs);
822     else 
823     {
824         printf("Records: %d\n", p->u.databaseOrSurDiagnostics->num_records);
825         for (i = 0; i < p->u.databaseOrSurDiagnostics->num_records; i++)
826             display_nameplusrecord(p->u.databaseOrSurDiagnostics->records[i]);
827     }
828 }
829
830 static int send_deleteResultSetRequest(char *arg)
831 {
832     char names[8][32];
833     int i;
834
835     Z_APDU *apdu = zget_APDU(out, Z_APDU_deleteResultSetRequest);
836     Z_DeleteResultSetRequest *req = apdu->u.deleteResultSetRequest;
837
838     req->referenceId = set_refid (out);
839
840     req->num_resultSetList =
841         sscanf (arg, "%30s %30s %30s %30s %30s %30s %30s %30s",
842                 names[0], names[1], names[2], names[3],
843                 names[4], names[5], names[6], names[7]);
844
845     req->deleteFunction = (int *)
846         odr_malloc (out, sizeof(*req->deleteFunction));
847     if (req->num_resultSetList > 0)
848     {
849         *req->deleteFunction = Z_DeleteRequest_list;
850         req->resultSetList = (char **)
851             odr_malloc (out, sizeof(*req->resultSetList)*
852                         req->num_resultSetList);
853         for (i = 0; i<req->num_resultSetList; i++)
854             req->resultSetList[i] = names[i];
855     }
856     else
857     {
858         *req->deleteFunction = Z_DeleteRequest_all;
859         req->resultSetList = 0;
860     }
861     
862     send_apdu(apdu);
863     printf("Sent deleteResultSetRequest.\n");
864     return 2;
865 }
866
867 static int send_searchRequest(char *arg)
868 {
869     Z_APDU *apdu = zget_APDU(out, Z_APDU_searchRequest);
870     Z_SearchRequest *req = apdu->u.searchRequest;
871     Z_Query query;
872     int oid[OID_SIZE];
873     struct ccl_rpn_node *rpn = NULL;
874     int error, pos;
875     char setstring[100];
876     Z_RPNQuery *RPNquery;
877     Odr_oct ccl_query;
878     YAZ_PQF_Parser pqf_parser;
879
880     if (queryType == QueryType_CCL2RPN)
881     {
882         rpn = ccl_find_str(bibset, arg, &error, &pos);
883         if (error)
884         {
885             printf("CCL ERROR: %s\n", ccl_err_msg(error));
886             return 0;
887         }
888     }
889     req->referenceId = set_refid (out);
890     if (!strcmp(arg, "@big")) /* strictly for troublemaking */
891     {
892         static unsigned char big[2100];
893         static Odr_oct bigo;
894
895         /* send a very big referenceid to test transport stack etc. */
896         memset(big, 'A', 2100);
897         bigo.len = bigo.size = 2100;
898         bigo.buf = big;
899         req->referenceId = &bigo;
900     }
901     
902     if (setnumber >= 0)
903     {
904         sprintf(setstring, "%d", ++setnumber);
905         req->resultSetName = setstring;
906     }
907     *req->smallSetUpperBound = smallSetUpperBound;
908     *req->largeSetLowerBound = largeSetLowerBound;
909     *req->mediumSetPresentNumber = mediumSetPresentNumber;
910     if (smallSetUpperBound > 0 || (largeSetLowerBound > 1 &&
911         mediumSetPresentNumber > 0))
912     {
913         oident prefsyn;
914
915         prefsyn.proto = protocol;
916         prefsyn.oclass = CLASS_RECSYN;
917         prefsyn.value = recordsyntax;
918         req->preferredRecordSyntax =
919             odr_oiddup(out, oid_ent_to_oid(&prefsyn, oid));
920         req->smallSetElementSetNames =
921             req->mediumSetElementSetNames = elementSetNames;
922     }
923     req->num_databaseNames = num_databaseNames;
924     req->databaseNames = databaseNames;
925
926     req->query = &query;
927
928     switch (queryType)
929     {
930     case QueryType_Prefix:
931         query.which = Z_Query_type_1;
932         pqf_parser = yaz_pqf_create ();
933         RPNquery = yaz_pqf_parse (pqf_parser, out, arg);
934         if (!RPNquery)
935         {
936             const char *pqf_msg;
937             size_t off;
938             int code = yaz_pqf_error (pqf_parser, &pqf_msg, &off);
939             printf("%*s^\n", off+4, "");
940             printf("Prefix query error: %s (code %d)\n", pqf_msg, code);
941             
942             yaz_pqf_destroy (pqf_parser);
943             return 0;
944         }
945         yaz_pqf_destroy (pqf_parser);
946         query.u.type_1 = RPNquery;
947         break;
948     case QueryType_CCL:
949         query.which = Z_Query_type_2;
950         query.u.type_2 = &ccl_query;
951         ccl_query.buf = (unsigned char*) arg;
952         ccl_query.len = strlen(arg);
953         break;
954     case QueryType_CCL2RPN:
955         query.which = Z_Query_type_1;
956         RPNquery = ccl_rpn_query(out, rpn);
957         if (!RPNquery)
958         {
959             printf ("Couldn't convert from CCL to RPN\n");
960             return 0;
961         }
962         query.u.type_1 = RPNquery;
963         ccl_rpn_delete (rpn);
964         break;
965     default:
966         printf ("Unsupported query type\n");
967         return 0;
968     }
969     send_apdu(apdu);
970     setno = 1;
971     printf("Sent searchRequest.\n");
972     return 2;
973 }
974
975 /* display Query Expression as part of searchResult-1 */
976 static void display_queryExpression (Z_QueryExpression *qe)
977 {
978     if (!qe)
979         return;
980     if (qe->which == Z_QueryExpression_term)
981     {
982         if (qe->u.term->queryTerm)
983         {
984             Z_Term *term = qe->u.term->queryTerm;
985             switch (term->which)
986             {
987             case Z_Term_general:
988                 printf (" %.*s", term->u.general->len, term->u.general->buf);
989                 break;
990             case Z_Term_characterString:
991                 printf (" %s", term->u.characterString);
992                 break;
993             case Z_Term_numeric:
994                 printf (" %d", *term->u.numeric);
995                 break;
996             case Z_Term_null:
997                 printf (" null");
998                 break;
999             }
1000         }
1001     }
1002 }
1003
1004 /* see if we can find USR:SearchResult-1 */
1005 static void display_searchResult (Z_OtherInformation *o)
1006 {
1007     int i;
1008     if (!o)
1009         return ;
1010     for (i = 0; i < o->num_elements; i++)
1011     {
1012         if (o->list[i]->which == Z_OtherInfo_externallyDefinedInfo)
1013         {
1014             Z_External *ext = o->list[i]->information.externallyDefinedInfo;
1015             
1016             if (ext->which == Z_External_searchResult1)
1017             {
1018                 int j;
1019                 Z_SearchInfoReport *sr = ext->u.searchResult1;
1020                 printf ("SearchResult-1:");
1021                 for (j = 0; j < sr->num; j++)
1022                 {
1023                     if (!sr->elements[j]->subqueryExpression)
1024                         printf (" %d", j);
1025                     display_queryExpression (
1026                         sr->elements[j]->subqueryExpression);
1027                     display_queryExpression (
1028                         sr->elements[j]->subqueryInterpretation);
1029                     display_queryExpression (
1030                         sr->elements[j]->subqueryRecommendation);
1031                     if (sr->elements[j]->subqueryCount)
1032                         printf ("(%d)", *sr->elements[j]->subqueryCount);
1033                 }
1034                 printf ("\n");
1035             }
1036         }
1037     }
1038 }
1039
1040 static int process_searchResponse(Z_SearchResponse *res)
1041 {
1042     printf ("Received SearchResponse.\n");
1043     print_refid (res->referenceId);
1044     if (*res->searchStatus)
1045         printf("Search was a success.\n");
1046     else
1047         printf("Search was a bloomin' failure.\n");
1048     printf("Number of hits: %d", *res->resultCount);
1049     if (setnumber >= 0)
1050         printf (", setno %d", setnumber);
1051     printf ("\n");
1052     display_searchResult (res->additionalSearchInfo);
1053     printf("records returned: %d\n",
1054            *res->numberOfRecordsReturned);
1055     setno += *res->numberOfRecordsReturned;
1056     if (res->records)
1057         display_records(res->records);
1058     return 0;
1059 }
1060
1061 static void print_level(int iLevel)
1062 {
1063     int i;
1064     for (i = 0; i < iLevel * 4; i++)
1065         printf(" ");
1066 }
1067
1068 static void print_int(int iLevel, const char *pTag, int *pInt)
1069 {
1070     if (pInt != NULL)
1071     {
1072         print_level(iLevel);
1073         printf("%s: %d\n", pTag, *pInt);
1074     }
1075 }
1076
1077 static void print_string(int iLevel, const char *pTag, const char *pString)
1078 {
1079     if (pString != NULL)
1080     {
1081         print_level(iLevel);
1082         printf("%s: %s\n", pTag, pString);
1083     }
1084 }
1085
1086 static void print_oid(int iLevel, const char *pTag, Odr_oid *pOid)
1087 {
1088     if (pOid != NULL)
1089     {
1090         int *pInt = pOid;
1091
1092         print_level(iLevel);
1093         printf("%s:", pTag);
1094         for (; *pInt != -1; pInt++)
1095             printf(" %d", *pInt);
1096         printf("\n");
1097     }
1098 }
1099
1100 static void print_referenceId(int iLevel, Z_ReferenceId *referenceId)
1101 {
1102     if (referenceId != NULL)
1103     {
1104         int i;
1105
1106         print_level(iLevel);
1107         printf("Ref Id (%d, %d): ", referenceId->len, referenceId->size);
1108         for (i = 0; i < referenceId->len; i++)
1109             printf("%c", referenceId->buf[i]);
1110         printf("\n");
1111     }
1112 }
1113
1114 static void print_string_or_numeric(int iLevel, const char *pTag, Z_StringOrNumeric *pStringNumeric)
1115 {
1116     if (pStringNumeric != NULL)
1117     {
1118         switch (pStringNumeric->which)
1119         {
1120         case Z_StringOrNumeric_string:
1121             print_string(iLevel, pTag, pStringNumeric->u.string);
1122             break;
1123             
1124         case Z_StringOrNumeric_numeric:
1125             print_int(iLevel, pTag, pStringNumeric->u.numeric);
1126             break;
1127             
1128         default:
1129             print_level(iLevel);
1130             printf("%s: valid type for Z_StringOrNumeric\n", pTag);
1131             break;
1132         }
1133     }
1134 }
1135
1136 static void print_universe_report_duplicate(
1137     int iLevel,
1138     Z_UniverseReportDuplicate *pUniverseReportDuplicate)
1139 {
1140     if (pUniverseReportDuplicate != NULL)
1141     {
1142         print_level(iLevel);
1143         printf("Universe Report Duplicate: \n");
1144         iLevel++;
1145         print_string_or_numeric(iLevel, "Hit No",
1146                                 pUniverseReportDuplicate->hitno);
1147     }
1148 }
1149
1150 static void print_universe_report_hits(
1151     int iLevel,
1152     Z_UniverseReportHits *pUniverseReportHits)
1153 {
1154     if (pUniverseReportHits != NULL)
1155     {
1156         print_level(iLevel);
1157         printf("Universe Report Hits: \n");
1158         iLevel++;
1159         print_string_or_numeric(iLevel, "Database",
1160                                 pUniverseReportHits->database);
1161         print_string_or_numeric(iLevel, "Hits", pUniverseReportHits->hits);
1162     }
1163 }
1164
1165 static void print_universe_report(int iLevel, Z_UniverseReport *pUniverseReport)
1166 {
1167     if (pUniverseReport != NULL)
1168     {
1169         print_level(iLevel);
1170         printf("Universe Report: \n");
1171         iLevel++;
1172         print_int(iLevel, "Total Hits", pUniverseReport->totalHits);
1173         switch (pUniverseReport->which)
1174         {
1175         case Z_UniverseReport_databaseHits:
1176             print_universe_report_hits(iLevel,
1177                                        pUniverseReport->u.databaseHits);
1178             break;
1179             
1180         case Z_UniverseReport_duplicate:
1181             print_universe_report_duplicate(iLevel,
1182                                             pUniverseReport->u.duplicate);
1183             break;
1184             
1185         default:
1186             print_level(iLevel);
1187             printf("Type: %d\n", pUniverseReport->which);
1188             break;
1189         }
1190     }
1191 }
1192
1193 static void print_external(int iLevel, Z_External *pExternal)
1194 {
1195     if (pExternal != NULL)
1196     {
1197         print_level(iLevel);
1198         printf("External: \n");
1199         iLevel++;
1200         print_oid(iLevel, "Direct Reference", pExternal->direct_reference);
1201         print_int(iLevel, "InDirect Reference", pExternal->indirect_reference);
1202         print_string(iLevel, "Descriptor", pExternal->descriptor);
1203         switch (pExternal->which)
1204         {
1205         case Z_External_universeReport:
1206             print_universe_report(iLevel, pExternal->u.universeReport);
1207             break;
1208             
1209         default:
1210             print_level(iLevel);
1211             printf("Type: %d\n", pExternal->which);
1212             break;
1213         }
1214     }
1215 }
1216
1217 static int process_resourceControlRequest (Z_ResourceControlRequest *req)
1218 {
1219     printf ("Received ResourceControlRequest.\n");
1220     print_referenceId(1, req->referenceId);
1221     print_int(1, "Suspended Flag", req->suspendedFlag);
1222     print_int(1, "Partial Results Available", req->partialResultsAvailable);
1223     print_int(1, "Response Required", req->responseRequired);
1224     print_int(1, "Triggered Request Flag", req->triggeredRequestFlag);
1225     print_external(1, req->resourceReport);
1226     return 0;
1227 }
1228
1229 void process_ESResponse(Z_ExtendedServicesResponse *res)
1230 {
1231     printf("Status: ");
1232     switch (*res->operationStatus)
1233     {
1234     case Z_ExtendedServicesResponse_done:
1235         printf ("done\n");
1236         break;
1237     case Z_ExtendedServicesResponse_accepted:
1238         printf ("accepted\n");
1239         break;
1240     case Z_ExtendedServicesResponse_failure:
1241         printf ("failure\n");
1242         display_diagrecs(res->diagnostics, res->num_diagnostics);
1243         break;
1244     default:
1245         printf ("unknown\n");
1246     }
1247     if ( (*res->operationStatus != Z_ExtendedServicesResponse_failure) &&
1248         (res->num_diagnostics != 0) ) {
1249         display_diagrecs(res->diagnostics, res->num_diagnostics);
1250     }
1251     print_refid (res->referenceId);
1252     if (res->taskPackage && 
1253         res->taskPackage->which == Z_External_extendedService)
1254     {
1255         Z_TaskPackage *taskPackage = res->taskPackage->u.extendedService;
1256         Odr_oct *id = taskPackage->targetReference;
1257         Z_External *ext = taskPackage->taskSpecificParameters;
1258         
1259         if (id)
1260         {
1261             printf ("Target Reference: ");
1262             print_stringn (id->buf, id->len);
1263             printf ("\n");
1264         }
1265         if (ext->which == Z_External_update)
1266         {
1267             Z_IUUpdateTaskPackage *utp = ext->u.update->u.taskPackage;
1268             if (utp && utp->targetPart)
1269             {
1270                 Z_IUTargetPart *targetPart = utp->targetPart;
1271                 int i;
1272
1273                 for (i = 0; i<targetPart->num_taskPackageRecords;  i++)
1274                 {
1275
1276                     Z_IUTaskPackageRecordStructure *tpr =
1277                         targetPart->taskPackageRecords[i];
1278                     printf ("task package record %d\n", i+1);
1279                     if (tpr->which == Z_IUTaskPackageRecordStructure_record)
1280                     {
1281                         display_record (tpr->u.record);
1282                     }
1283                     else
1284                     {
1285                         printf ("other type\n");
1286                     }
1287                 }
1288             }
1289         }
1290     }
1291 }
1292
1293 const char *get_ill_element (void *clientData, const char *element)
1294 {
1295     return 0;
1296 }
1297
1298 static Z_External *create_external_itemRequest()
1299 {
1300     struct ill_get_ctl ctl;
1301     ILL_ItemRequest *req;
1302     Z_External *r = 0;
1303     int item_request_size = 0;
1304     char *item_request_buf = 0;
1305
1306     ctl.odr = out;
1307     ctl.clientData = 0;
1308     ctl.f = get_ill_element;
1309     
1310     req = ill_get_ItemRequest(&ctl, "ill", 0);
1311     if (!req)
1312         printf ("ill_get_ItemRequest failed\n");
1313         
1314     if (!ill_ItemRequest (out, &req, 0, 0))
1315     {
1316         if (apdu_file)
1317         {
1318             ill_ItemRequest(print, &req, 0, 0);
1319             odr_reset(print);
1320         }
1321         item_request_buf = odr_getbuf (out, &item_request_size, 0);
1322         if (item_request_buf)
1323             odr_setbuf (out, item_request_buf, item_request_size, 1);
1324         printf ("Couldn't encode ItemRequest, size %d\n", item_request_size);
1325         return 0;
1326     }
1327     else
1328     {
1329         oident oid;
1330         
1331         item_request_buf = odr_getbuf (out, &item_request_size, 0);
1332         oid.proto = PROTO_GENERAL;
1333         oid.oclass = CLASS_GENERAL;
1334         oid.value = VAL_ISO_ILL_1;
1335         
1336         r = (Z_External *) odr_malloc (out, sizeof(*r));
1337         r->direct_reference = odr_oiddup(out,oid_getoidbyent(&oid)); 
1338         r->indirect_reference = 0;
1339         r->descriptor = 0;
1340         r->which = Z_External_single;
1341         
1342         r->u.single_ASN1_type = (Odr_oct *)
1343             odr_malloc (out, sizeof(*r->u.single_ASN1_type));
1344         r->u.single_ASN1_type->buf = (unsigned char *)
1345         odr_malloc (out, item_request_size);
1346         r->u.single_ASN1_type->len = item_request_size;
1347         r->u.single_ASN1_type->size = item_request_size;
1348         memcpy (r->u.single_ASN1_type->buf, item_request_buf,
1349                 item_request_size);
1350                 
1351                 do_hex_dump(item_request_buf,item_request_size);
1352     }
1353     return r;
1354 }
1355
1356 static Z_External *create_external_ILL_APDU(int which)
1357 {
1358     struct ill_get_ctl ctl;
1359     ILL_APDU *ill_apdu;
1360     Z_External *r = 0;
1361     int ill_request_size = 0;
1362     char *ill_request_buf = 0;
1363         
1364     ctl.odr = out;
1365     ctl.clientData = 0;
1366     ctl.f = get_ill_element;
1367
1368     ill_apdu = ill_get_APDU(&ctl, "ill", 0);
1369
1370     if (!ill_APDU (out, &ill_apdu, 0, 0))
1371     {
1372         if (apdu_file)
1373         {
1374             printf ("-------------------\n");
1375             ill_APDU(print, &ill_apdu, 0, 0);
1376             odr_reset(print);
1377             printf ("-------------------\n");
1378         }
1379         ill_request_buf = odr_getbuf (out, &ill_request_size, 0);
1380         if (ill_request_buf)
1381             odr_setbuf (out, ill_request_buf, ill_request_size, 1);
1382         printf ("Couldn't encode ILL-Request, size %d\n", ill_request_size);
1383         return 0;
1384     }
1385     else
1386     {
1387         oident oid;
1388         ill_request_buf = odr_getbuf (out, &ill_request_size, 0);
1389         
1390         oid.proto = PROTO_GENERAL;
1391         oid.oclass = CLASS_GENERAL;
1392         oid.value = VAL_ISO_ILL_1;
1393         
1394         r = (Z_External *) odr_malloc (out, sizeof(*r));
1395         r->direct_reference = odr_oiddup(out,oid_getoidbyent(&oid)); 
1396         r->indirect_reference = 0;
1397         r->descriptor = 0;
1398         r->which = Z_External_single;
1399         
1400         r->u.single_ASN1_type = (Odr_oct *)
1401             odr_malloc (out, sizeof(*r->u.single_ASN1_type));
1402         r->u.single_ASN1_type->buf = (unsigned char *)
1403         odr_malloc (out, ill_request_size);
1404         r->u.single_ASN1_type->len = ill_request_size;
1405         r->u.single_ASN1_type->size = ill_request_size;
1406         memcpy (r->u.single_ASN1_type->buf, ill_request_buf, ill_request_size);
1407 /*         printf ("len = %d\n", ill_request_size); */
1408 /*              do_hex_dump(ill_request_buf,ill_request_size); */
1409 /*              printf("--- end of extenal\n"); */
1410
1411     }
1412     return r;
1413 }
1414
1415
1416 static Z_External *create_ItemOrderExternal(const char *type, int itemno)
1417 {
1418     Z_External *r = (Z_External *) odr_malloc(out, sizeof(Z_External));
1419     oident ItemOrderRequest;
1420   
1421     ItemOrderRequest.proto = PROTO_Z3950;
1422     ItemOrderRequest.oclass = CLASS_EXTSERV;
1423     ItemOrderRequest.value = VAL_ITEMORDER;
1424  
1425     r->direct_reference = odr_oiddup(out,oid_getoidbyent(&ItemOrderRequest)); 
1426     r->indirect_reference = 0;
1427     r->descriptor = 0;
1428
1429     r->which = Z_External_itemOrder;
1430
1431     r->u.itemOrder = (Z_ItemOrder *) odr_malloc(out,sizeof(Z_ItemOrder));
1432     memset(r->u.itemOrder, 0, sizeof(Z_ItemOrder));
1433     r->u.itemOrder->which=Z_IOItemOrder_esRequest;
1434
1435     r->u.itemOrder->u.esRequest = (Z_IORequest *) 
1436         odr_malloc(out,sizeof(Z_IORequest));
1437     memset(r->u.itemOrder->u.esRequest, 0, sizeof(Z_IORequest));
1438
1439     r->u.itemOrder->u.esRequest->toKeep = (Z_IOOriginPartToKeep *)
1440         odr_malloc(out,sizeof(Z_IOOriginPartToKeep));
1441     memset(r->u.itemOrder->u.esRequest->toKeep, 0, sizeof(Z_IOOriginPartToKeep));
1442     r->u.itemOrder->u.esRequest->notToKeep = (Z_IOOriginPartNotToKeep *)
1443         odr_malloc(out,sizeof(Z_IOOriginPartNotToKeep));
1444     memset(r->u.itemOrder->u.esRequest->notToKeep, 0, sizeof(Z_IOOriginPartNotToKeep));
1445
1446     r->u.itemOrder->u.esRequest->toKeep->supplDescription = NULL;
1447     r->u.itemOrder->u.esRequest->toKeep->contact = NULL;
1448     r->u.itemOrder->u.esRequest->toKeep->addlBilling = NULL;
1449
1450     r->u.itemOrder->u.esRequest->notToKeep->resultSetItem =
1451         (Z_IOResultSetItem *) odr_malloc(out, sizeof(Z_IOResultSetItem));
1452     memset(r->u.itemOrder->u.esRequest->notToKeep->resultSetItem, 0, sizeof(Z_IOResultSetItem));
1453     r->u.itemOrder->u.esRequest->notToKeep->resultSetItem->resultSetId = "1";
1454
1455     r->u.itemOrder->u.esRequest->notToKeep->resultSetItem->item =
1456         (int *) odr_malloc(out, sizeof(int));
1457     *r->u.itemOrder->u.esRequest->notToKeep->resultSetItem->item = itemno;
1458
1459     if (!strcmp (type, "item") || !strcmp(type, "2"))
1460     {
1461         printf ("using item-request\n");
1462         r->u.itemOrder->u.esRequest->notToKeep->itemRequest = 
1463             create_external_itemRequest();
1464     }
1465     else if (!strcmp(type, "ill") || !strcmp(type, "1"))
1466     {
1467         printf ("using ILL-request\n");
1468         r->u.itemOrder->u.esRequest->notToKeep->itemRequest = 
1469             create_external_ILL_APDU(ILL_APDU_ILL_Request);
1470     }
1471     else if (!strcmp(type, "xml") || !strcmp(type, "3"))
1472     {
1473     const char *xml_buf =
1474         "<itemorder>\n"
1475         "  <type>request</type>\n"
1476         "  <libraryNo>000200</libraryNo>\n"
1477         "  <borrowerTicketNo> 1212 </borrowerTicketNo>\n"
1478         "</itemorder>";
1479         r->u.itemOrder->u.esRequest->notToKeep->itemRequest =
1480             z_ext_record (out, VAL_TEXT_XML, xml_buf, strlen(xml_buf));
1481     }
1482     else
1483         r->u.itemOrder->u.esRequest->notToKeep->itemRequest = 0;
1484
1485     return r;
1486 }
1487
1488 static int send_itemorder(const char *type, int itemno)
1489 {
1490     Z_APDU *apdu = zget_APDU(out, Z_APDU_extendedServicesRequest);
1491     Z_ExtendedServicesRequest *req = apdu->u.extendedServicesRequest;
1492     oident ItemOrderRequest;
1493
1494     ItemOrderRequest.proto = PROTO_Z3950;
1495     ItemOrderRequest.oclass = CLASS_EXTSERV;
1496     ItemOrderRequest.value = VAL_ITEMORDER;
1497     req->packageType = odr_oiddup(out,oid_getoidbyent(&ItemOrderRequest));
1498     req->packageName = esPackageName;
1499
1500     req->taskSpecificParameters = create_ItemOrderExternal(type, itemno);
1501
1502     send_apdu(apdu);
1503     return 0;
1504 }
1505
1506 static int cmd_update(char *arg)
1507 {
1508     Z_APDU *apdu = zget_APDU(out, Z_APDU_extendedServicesRequest );
1509     Z_ExtendedServicesRequest *req = apdu->u.extendedServicesRequest;
1510     Z_External *r;
1511     int oid[OID_SIZE];
1512     Z_IUOriginPartToKeep *toKeep;
1513     Z_IUSuppliedRecords *notToKeep;
1514     oident update_oid;
1515     char action[20], recid[20], fname[80];
1516     int action_no;
1517     Z_External *record_this = 0;
1518
1519     *action = 0;
1520     *recid = 0;
1521     *fname = 0;
1522     sscanf (arg, "%19s %19s %79s", action, recid, fname);
1523
1524     if (!strcmp (action, "insert"))
1525         action_no = Z_IUOriginPartToKeep_recordInsert;
1526     else if (!strcmp (action, "replace"))
1527         action_no = Z_IUOriginPartToKeep_recordReplace;
1528     else if (!strcmp (action, "delete"))
1529         action_no = Z_IUOriginPartToKeep_recordDelete;
1530     else if (!strcmp (action, "update"))
1531         action_no = Z_IUOriginPartToKeep_specialUpdate;
1532     else 
1533     {
1534         printf ("Bad action: %s\n", action);
1535         printf ("Possible values: insert, replace, delete, update\n");
1536         return 0;
1537     }
1538
1539     if (*fname)
1540     {
1541         FILE *inf;
1542         struct stat status;
1543         stat (fname, &status);
1544         if (S_ISREG(status.st_mode) && (inf = fopen(fname, "r")))
1545         {
1546             size_t len = status.st_size;
1547             char *buf = (char *) xmalloc (len);
1548
1549             fread (buf, 1, len, inf);
1550
1551             fclose (inf);
1552             
1553             record_this = z_ext_record (out, VAL_TEXT_XML, buf, len);
1554             
1555             xfree (buf);
1556         }
1557         else
1558         {
1559             printf ("File %s doesn't exist\n", fname);
1560             return 0;
1561         }
1562     }
1563     else
1564     {
1565         if (!record_last)
1566         {
1567             printf ("No last record (update ignored)\n");
1568             return 0;
1569         }
1570         record_this = record_last;
1571     }
1572
1573     update_oid.proto = PROTO_Z3950;
1574     update_oid.oclass = CLASS_EXTSERV;
1575     update_oid.value = VAL_DBUPDATE;
1576     oid_ent_to_oid (&update_oid, oid);
1577     req->packageType = odr_oiddup(out,oid);
1578     req->packageName = esPackageName;
1579     
1580     req->referenceId = set_refid (out);
1581
1582     r = req->taskSpecificParameters = (Z_External *)
1583         odr_malloc (out, sizeof(*r));
1584     r->direct_reference = odr_oiddup(out,oid);
1585     r->indirect_reference = 0;
1586     r->descriptor = 0;
1587     r->which = Z_External_update;
1588     r->u.update = (Z_IUUpdate *) odr_malloc(out, sizeof(*r->u.update));
1589     r->u.update->which = Z_IUUpdate_esRequest;
1590     r->u.update->u.esRequest = (Z_IUUpdateEsRequest *)
1591         odr_malloc(out, sizeof(*r->u.update->u.esRequest));
1592     toKeep = r->u.update->u.esRequest->toKeep = (Z_IUOriginPartToKeep *)
1593         odr_malloc(out, sizeof(*r->u.update->u.esRequest->toKeep));
1594     toKeep->databaseName = databaseNames[0];
1595     toKeep->schema = 0;
1596     toKeep->elementSetName = 0;
1597     toKeep->actionQualifier = 0;
1598     toKeep->action = (int *) odr_malloc(out, sizeof(*toKeep->action));
1599     *toKeep->action = action_no;
1600
1601     notToKeep = r->u.update->u.esRequest->notToKeep = (Z_IUSuppliedRecords *)
1602         odr_malloc(out, sizeof(*r->u.update->u.esRequest->notToKeep));
1603     notToKeep->num = 1;
1604     notToKeep->elements = (Z_IUSuppliedRecords_elem **)
1605         odr_malloc(out, sizeof(*notToKeep->elements));
1606     notToKeep->elements[0] = (Z_IUSuppliedRecords_elem *)
1607         odr_malloc(out, sizeof(**notToKeep->elements));
1608     notToKeep->elements[0]->which = Z_IUSuppliedRecords_elem_opaque;
1609     if (*recid)
1610     {
1611         notToKeep->elements[0]->u.opaque = (Odr_oct *)
1612             odr_malloc (out, sizeof(Odr_oct));
1613         notToKeep->elements[0]->u.opaque->buf = (unsigned char *) recid;
1614         notToKeep->elements[0]->u.opaque->size = strlen(recid);
1615         notToKeep->elements[0]->u.opaque->len = strlen(recid);
1616     }
1617     else
1618         notToKeep->elements[0]->u.opaque = 0;
1619     notToKeep->elements[0]->supplementalId = 0;
1620     notToKeep->elements[0]->correlationInfo = 0;
1621     notToKeep->elements[0]->record = record_this;
1622     
1623     send_apdu(apdu);
1624
1625     return 2;
1626 }
1627
1628 static int cmd_itemorder(char *arg)
1629 {
1630     char type[12];
1631     int itemno;
1632     
1633     if (sscanf (arg, "%10s %d", type, &itemno) != 2)
1634         return 0;
1635
1636     printf("Item order request\n");
1637     fflush(stdout);
1638     send_itemorder(type, itemno);
1639     return 2;
1640 }
1641
1642 static int cmd_find(char *arg)
1643 {
1644     if (!*arg)
1645     {
1646         printf("Find what?\n");
1647         return 0;
1648     }
1649     if (!conn)
1650     {
1651                 try_reconnect(); 
1652
1653                 if (!conn) {                                    
1654                         printf("Not connected yet\n");
1655                         return 0;
1656                 };
1657     }
1658     if (!send_searchRequest(arg))
1659         return 0;
1660     return 2;
1661 }
1662
1663 static int cmd_delete(char *arg)
1664 {
1665     if (!conn)
1666     {
1667         printf("Not connected yet\n");
1668         return 0;
1669     }
1670     if (!send_deleteResultSetRequest(arg))
1671         return 0;
1672     return 2;
1673 }
1674
1675 static int cmd_ssub(char *arg)
1676 {
1677     if (!(smallSetUpperBound = atoi(arg)))
1678         return 0;
1679     return 1;
1680 }
1681
1682 static int cmd_lslb(char *arg)
1683 {
1684     if (!(largeSetLowerBound = atoi(arg)))
1685         return 0;
1686     return 1;
1687 }
1688
1689 static int cmd_mspn(char *arg)
1690 {
1691     if (!(mediumSetPresentNumber = atoi(arg)))
1692         return 0;
1693     return 1;
1694 }
1695
1696 static int cmd_status(char *arg)
1697 {
1698     printf("smallSetUpperBound: %d\n", smallSetUpperBound);
1699     printf("largeSetLowerBound: %d\n", largeSetLowerBound);
1700     printf("mediumSetPresentNumber: %d\n", mediumSetPresentNumber);
1701     return 1;
1702 }
1703
1704 static int cmd_setnames(char *arg)
1705 {
1706     if (*arg == '1')         /* enable ? */
1707         setnumber = 0;
1708     else if (*arg == '0')    /* disable ? */
1709         setnumber = -1;
1710     else if (setnumber < 0)  /* no args, toggle .. */
1711         setnumber = 0;
1712     else
1713         setnumber = -1;
1714    
1715     if (setnumber >= 0)
1716         printf("Set numbering enabled.\n");
1717     else
1718         printf("Set numbering disabled.\n");
1719     return 1;
1720 }
1721
1722 /* PRESENT SERVICE ----------------------------- */
1723
1724 static int send_presentRequest(char *arg)
1725 {
1726     Z_APDU *apdu = zget_APDU(out, Z_APDU_presentRequest);
1727     Z_PresentRequest *req = apdu->u.presentRequest;
1728     Z_RecordComposition compo;
1729     oident prefsyn;
1730     int nos = 1;
1731     int oid[OID_SIZE];
1732     char *p;
1733     char setstring[100];
1734
1735     req->referenceId = set_refid (out);
1736     if ((p = strchr(arg, '+')))
1737     {
1738         nos = atoi(p + 1);
1739         *p = 0;
1740     }
1741     if (*arg)
1742         setno = atoi(arg);
1743     if (p && (p=strchr(p+1, '+')))
1744     {
1745         strcpy (setstring, p+1);
1746         req->resultSetId = setstring;
1747     }
1748     else if (setnumber >= 0)
1749     {
1750         sprintf(setstring, "%d", setnumber);
1751         req->resultSetId = setstring;
1752     }
1753     req->resultSetStartPoint = &setno;
1754     req->numberOfRecordsRequested = &nos;
1755     prefsyn.proto = protocol;
1756     prefsyn.oclass = CLASS_RECSYN;
1757     prefsyn.value = recordsyntax;
1758     req->preferredRecordSyntax =
1759         odr_oiddup (out, oid_ent_to_oid(&prefsyn, oid));
1760
1761     if (schema != VAL_NONE)
1762     {
1763         oident prefschema;
1764
1765         prefschema.proto = protocol;
1766         prefschema.oclass = CLASS_SCHEMA;
1767         prefschema.value = schema;
1768
1769         req->recordComposition = &compo;
1770         compo.which = Z_RecordComp_complex;
1771         compo.u.complex = (Z_CompSpec *)
1772             odr_malloc(out, sizeof(*compo.u.complex));
1773         compo.u.complex->selectAlternativeSyntax = (bool_t *) 
1774             odr_malloc(out, sizeof(bool_t));
1775         *compo.u.complex->selectAlternativeSyntax = 0;
1776
1777         compo.u.complex->generic = (Z_Specification *)
1778             odr_malloc(out, sizeof(*compo.u.complex->generic));
1779         compo.u.complex->generic->schema = (Odr_oid *)
1780             odr_oiddup(out, oid_ent_to_oid(&prefschema, oid));
1781         if (!compo.u.complex->generic->schema)
1782         {
1783             /* OID wasn't a schema! Try record syntax instead. */
1784             prefschema.oclass = CLASS_RECSYN;
1785             compo.u.complex->generic->schema = (Odr_oid *)
1786                 odr_oiddup(out, oid_ent_to_oid(&prefschema, oid));
1787         }
1788         if (!elementSetNames)
1789             compo.u.complex->generic->elementSpec = 0;
1790         else
1791         {
1792             compo.u.complex->generic->elementSpec = (Z_ElementSpec *)
1793                 odr_malloc(out, sizeof(Z_ElementSpec));
1794             compo.u.complex->generic->elementSpec->which =
1795                 Z_ElementSpec_elementSetName;
1796             compo.u.complex->generic->elementSpec->u.elementSetName =
1797                 elementSetNames->u.generic;
1798         }
1799         compo.u.complex->num_dbSpecific = 0;
1800         compo.u.complex->dbSpecific = 0;
1801         compo.u.complex->num_recordSyntax = 0;
1802         compo.u.complex->recordSyntax = 0;
1803     }
1804     else if (elementSetNames)
1805     {
1806         req->recordComposition = &compo;
1807         compo.which = Z_RecordComp_simple;
1808         compo.u.simple = elementSetNames;
1809     }
1810     send_apdu(apdu);
1811     printf("Sent presentRequest (%d+%d).\n", setno, nos);
1812     return 2;
1813 }
1814     
1815 static void close_session (void)
1816 {
1817     cs_close (conn);
1818     conn = 0;
1819     if (session_mem)
1820     {
1821         nmem_destroy (session_mem);
1822         session_mem = NULL;
1823     }
1824     sent_close = 0;
1825 }
1826
1827 void process_close(Z_Close *req)
1828 {
1829     Z_APDU *apdu = zget_APDU(out, Z_APDU_close);
1830     Z_Close *res = apdu->u.close;
1831
1832     static char *reasons[] =
1833     {
1834         "finished",
1835         "shutdown",
1836         "system problem",
1837         "cost limit reached",
1838         "resources",
1839         "security violation",
1840         "protocolError",
1841         "lack of activity",
1842         "peer abort",
1843         "unspecified"
1844     };
1845
1846     printf("Reason: %s, message: %s\n", reasons[*req->closeReason],
1847         req->diagnosticInformation ? req->diagnosticInformation : "NULL");
1848     if (sent_close)
1849         close_session ();
1850     else
1851     {
1852         *res->closeReason = Z_Close_finished;
1853         send_apdu(apdu);
1854         printf("Sent response.\n");
1855         sent_close = 1;
1856     }
1857 }
1858
1859 static int cmd_show(char *arg)
1860 {
1861     if (!conn)
1862     {
1863         printf("Not connected yet\n");
1864         return 0;
1865     }
1866     if (!send_presentRequest(arg))
1867         return 0;
1868     return 2;
1869 }
1870
1871 int cmd_quit(char *arg)
1872 {
1873     printf("See you later, alligator.\n");
1874     xmalloc_trav ("");
1875     exit(0);
1876     return 0;
1877 }
1878
1879 int cmd_cancel(char *arg)
1880 {
1881     Z_APDU *apdu = zget_APDU(out, Z_APDU_triggerResourceControlRequest);
1882     Z_TriggerResourceControlRequest *req =
1883         apdu->u.triggerResourceControlRequest;
1884     bool_t rfalse = 0;
1885     
1886     if (!conn)
1887     {
1888         printf("Session not initialized yet\n");
1889         return 0;
1890     }
1891     if (!ODR_MASK_GET(session->options, Z_Options_triggerResourceCtrl))
1892     {
1893         printf("Target doesn't support cancel (trigger resource ctrl)\n");
1894         return 0;
1895     }
1896     *req->requestedAction = Z_TriggerResourceCtrl_cancel;
1897     req->resultSetWanted = &rfalse;
1898
1899     send_apdu(apdu);
1900     printf("Sent cancel request\n");
1901     return 2;
1902 }
1903
1904 int send_scanrequest(const char *query, int pp, int num, const char *term)
1905 {
1906     Z_APDU *apdu = zget_APDU(out, Z_APDU_scanRequest);
1907     Z_ScanRequest *req = apdu->u.scanRequest;
1908     int oid[OID_SIZE];
1909     
1910     if (queryType == QueryType_CCL2RPN)
1911     {
1912         oident bib1;
1913         int error, pos;
1914         struct ccl_rpn_node *rpn;
1915
1916         rpn = ccl_find_str (bibset,  query, &error, &pos);
1917         if (error)
1918         {
1919             printf("CCL ERROR: %s\n", ccl_err_msg(error));
1920             return -1;
1921         }
1922         bib1.proto = PROTO_Z3950;
1923         bib1.oclass = CLASS_ATTSET;
1924         bib1.value = VAL_BIB1;
1925         req->attributeSet = oid_ent_to_oid (&bib1, oid);
1926         if (!(req->termListAndStartPoint = ccl_scan_query (out, rpn)))
1927         {
1928             printf("Couldn't convert CCL to Scan term\n");
1929             return -1;
1930         }
1931         ccl_rpn_delete (rpn);
1932     }
1933     else
1934     {
1935         YAZ_PQF_Parser pqf_parser = yaz_pqf_create ();
1936
1937         if (!(req->termListAndStartPoint =
1938               yaz_pqf_scan(pqf_parser, out, &req->attributeSet, query)))
1939         {
1940             const char *pqf_msg;
1941             size_t off;
1942             int code = yaz_pqf_error (pqf_parser, &pqf_msg, &off);
1943             printf("%*s^\n", off+7, "");
1944             printf("Prefix query error: %s (code %d)\n", pqf_msg, code);
1945             yaz_pqf_destroy (pqf_parser);
1946             return -1;
1947         }
1948         yaz_pqf_destroy (pqf_parser);
1949     }
1950     if (term && *term)
1951     {
1952         if (req->termListAndStartPoint->term &&
1953             req->termListAndStartPoint->term->which == Z_Term_general &&
1954             req->termListAndStartPoint->term->u.general)
1955         {
1956             req->termListAndStartPoint->term->u.general->buf =
1957                 (unsigned char *) odr_strdup(out, term);
1958             req->termListAndStartPoint->term->u.general->len =
1959                 req->termListAndStartPoint->term->u.general->size =
1960                 strlen(term);
1961         }
1962     }
1963     req->referenceId = set_refid (out);
1964     req->num_databaseNames = num_databaseNames;
1965     req->databaseNames = databaseNames;
1966     req->numberOfTermsRequested = &num;
1967     req->preferredPositionInResponse = &pp;
1968     send_apdu(apdu);
1969     return 2;
1970 }
1971
1972 int send_sortrequest(char *arg, int newset)
1973 {
1974     Z_APDU *apdu = zget_APDU(out, Z_APDU_sortRequest);
1975     Z_SortRequest *req = apdu->u.sortRequest;
1976     Z_SortKeySpecList *sksl = (Z_SortKeySpecList *)
1977         odr_malloc (out, sizeof(*sksl));
1978     char setstring[32];
1979
1980     if (setnumber >= 0)
1981         sprintf (setstring, "%d", setnumber);
1982     else
1983         sprintf (setstring, "default");
1984
1985     req->referenceId = set_refid (out);
1986
1987     req->num_inputResultSetNames = 1;
1988     req->inputResultSetNames = (Z_InternationalString **)
1989         odr_malloc (out, sizeof(*req->inputResultSetNames));
1990     req->inputResultSetNames[0] = odr_strdup (out, setstring);
1991
1992     if (newset && setnumber >= 0)
1993         sprintf (setstring, "%d", ++setnumber);
1994
1995     req->sortedResultSetName = odr_strdup (out, setstring);
1996
1997     req->sortSequence = yaz_sort_spec (out, arg);
1998     if (!req->sortSequence)
1999     {
2000         printf ("Missing sort specifications\n");
2001         return -1;
2002     }
2003     send_apdu(apdu);
2004     return 2;
2005 }
2006
2007 void display_term(Z_TermInfo *t)
2008 {
2009     if (t->term->which == Z_Term_general)
2010     {
2011         printf("%.*s", t->term->u.general->len, t->term->u.general->buf);
2012         sprintf(last_scan_line, "%.*s", t->term->u.general->len,
2013             t->term->u.general->buf);
2014     }
2015     else
2016         printf("Term (not general)");
2017     if (t->globalOccurrences)
2018         printf (" (%d)\n", *t->globalOccurrences);
2019     else
2020         printf ("\n");
2021 }
2022
2023 void process_scanResponse(Z_ScanResponse *res)
2024 {
2025     int i;
2026     Z_Entry **entries = NULL;
2027     int num_entries = 0;
2028    
2029     printf("Received ScanResponse\n"); 
2030     print_refid (res->referenceId);
2031     printf("%d entries", *res->numberOfEntriesReturned);
2032     if (res->positionOfTerm)
2033         printf (", position=%d", *res->positionOfTerm); 
2034     printf ("\n");
2035     if (*res->scanStatus != Z_Scan_success)
2036         printf("Scan returned code %d\n", *res->scanStatus);
2037     if (!res->entries)
2038         return;
2039     if ((entries = res->entries->entries))
2040         num_entries = res->entries->num_entries;
2041     for (i = 0; i < num_entries; i++)
2042     {
2043         int pos_term = res->positionOfTerm ? *res->positionOfTerm : -1;
2044         if (entries[i]->which == Z_Entry_termInfo)
2045         {
2046             printf("%c ", i + 1 == pos_term ? '*' : ' ');
2047             display_term(entries[i]->u.termInfo);
2048         }
2049         else
2050             display_diagrecs(&entries[i]->u.surrogateDiagnostic, 1);
2051     }
2052     if (res->entries->nonsurrogateDiagnostics)
2053         display_diagrecs (res->entries->nonsurrogateDiagnostics,
2054                           res->entries->num_nonsurrogateDiagnostics);
2055 }
2056
2057 void process_sortResponse(Z_SortResponse *res)
2058 {
2059     printf("Received SortResponse: status=");
2060     switch (*res->sortStatus)
2061     {
2062     case Z_SortStatus_success:
2063         printf ("success"); break;
2064     case Z_SortStatus_partial_1:
2065         printf ("partial"); break;
2066     case Z_SortStatus_failure:
2067         printf ("failure"); break;
2068     default:
2069         printf ("unknown (%d)", *res->sortStatus);
2070     }
2071     printf ("\n");
2072     print_refid (res->referenceId);
2073     if (res->diagnostics)
2074         display_diagrecs(res->diagnostics,
2075                          res->num_diagnostics);
2076 }
2077
2078 void process_deleteResultSetResponse (Z_DeleteResultSetResponse *res)
2079 {
2080     printf("Got deleteResultSetResponse status=%d\n",
2081            *res->deleteOperationStatus);
2082     if (res->deleteListStatuses)
2083     {
2084         int i;
2085         for (i = 0; i < res->deleteListStatuses->num; i++)
2086         {
2087             printf ("%s status=%d\n", res->deleteListStatuses->elements[i]->id,
2088                     *res->deleteListStatuses->elements[i]->status);
2089         }
2090     }
2091 }
2092
2093 int cmd_sort_generic(char *arg, int newset)
2094 {
2095     if (!conn)
2096     {
2097         printf("Session not initialized yet\n");
2098         return 0;
2099     }
2100     if (!ODR_MASK_GET(session->options, Z_Options_sort))
2101     {
2102         printf("Target doesn't support sort\n");
2103         return 0;
2104     }
2105     if (*arg)
2106     {
2107         if (send_sortrequest(arg, newset) < 0)
2108             return 0;
2109         return 2;
2110     }
2111     return 0;
2112 }
2113
2114 int cmd_sort(char *arg)
2115 {
2116     return cmd_sort_generic (arg, 0);
2117 }
2118
2119 int cmd_sort_newset (char *arg)
2120 {
2121     return cmd_sort_generic (arg, 1);
2122 }
2123
2124 int cmd_scan(char *arg)
2125 {
2126     if (!conn)
2127     {
2128                 try_reconnect();
2129                 
2130                 if (!conn) {                                                            
2131                         printf("Session not initialized yet\n");
2132                         return 0;
2133                 };
2134     }
2135     if (!ODR_MASK_GET(session->options, Z_Options_scan))
2136     {
2137         printf("Target doesn't support scan\n");
2138         return 0;
2139     }
2140     if (*arg)
2141     {
2142         strcpy (last_scan_query, arg);
2143         if (send_scanrequest(arg, 1, 20, 0) < 0)
2144             return 0;
2145     }
2146     else
2147     {
2148         if (send_scanrequest(last_scan_query, 1, 20, last_scan_line) < 0)
2149             return 0;
2150     }
2151     return 2;
2152 }
2153
2154 int cmd_schema(char *arg)
2155 {
2156     if (!arg || !*arg)
2157     {
2158         schema = VAL_NONE;
2159         return 1;
2160     }
2161     schema = oid_getvalbyname (arg);
2162     if (schema == VAL_NONE)
2163     {
2164         printf ("unknown schema\n");
2165         return 0;
2166     }
2167     return 1;
2168 }
2169
2170 int cmd_format(char *arg)
2171 {
2172     if (!arg || !*arg)
2173     {
2174         printf("Usage: format <recordsyntax>\n");
2175         return 0;
2176     }
2177     recordsyntax = oid_getvalbyname (arg);
2178     if (recordsyntax == VAL_NONE)
2179     {
2180         printf ("unknown record syntax\n");
2181         return 0;
2182     }
2183     return 1;
2184 }
2185
2186 int cmd_elements(char *arg)
2187 {
2188     static Z_ElementSetNames esn;
2189     static char what[100];
2190
2191     if (!arg || !*arg)
2192     {
2193         elementSetNames = 0;
2194         return 1;
2195     }
2196     strcpy(what, arg);
2197     esn.which = Z_ElementSetNames_generic;
2198     esn.u.generic = what;
2199     elementSetNames = &esn;
2200     return 1;
2201 }
2202
2203 int cmd_attributeset(char *arg)
2204 {
2205     char what[100];
2206
2207     if (!arg || !*arg)
2208     {
2209         printf("Usage: attributeset <setname>\n");
2210         return 0;
2211     }
2212     sscanf(arg, "%s", what);
2213     if (p_query_attset (what))
2214     {
2215         printf("Unknown attribute set name\n");
2216         return 0;
2217     }
2218     return 1;
2219 }
2220
2221 int cmd_querytype (char *arg)
2222 {
2223     if (!strcmp (arg, "ccl"))
2224         queryType = QueryType_CCL;
2225     else if (!strcmp (arg, "prefix") || !strcmp(arg, "rpn"))
2226         queryType = QueryType_Prefix;
2227     else if (!strcmp (arg, "ccl2rpn") || !strcmp (arg, "cclrpn"))
2228         queryType = QueryType_CCL2RPN;
2229     else
2230     {
2231         printf ("Querytype must be one of:\n");
2232         printf (" prefix         - Prefix query\n");
2233         printf (" ccl            - CCL query\n");
2234         printf (" ccl2rpn        - CCL query converted to RPN\n");
2235         return 0;
2236     }
2237     return 1;
2238 }
2239
2240 int cmd_refid (char *arg)
2241 {
2242     xfree (refid);
2243     refid = NULL;
2244     if (*arg)
2245     {
2246         refid = (char *) xmalloc (strlen(arg)+1);
2247         strcpy (refid, arg);
2248     }
2249     return 1;
2250 }
2251
2252 int cmd_close(char *arg)
2253 {
2254     Z_APDU *apdu;
2255     Z_Close *req;
2256     if (!conn)
2257         return 0;
2258
2259     apdu = zget_APDU(out, Z_APDU_close);
2260     req = apdu->u.close;
2261     *req->closeReason = Z_Close_finished;
2262     send_apdu(apdu);
2263     printf("Sent close request.\n");
2264     sent_close = 1;
2265     return 2;
2266 }
2267
2268 int cmd_packagename(char* arg)
2269 {
2270     xfree (esPackageName);
2271     esPackageName = NULL;
2272     if (*arg)
2273     {
2274         esPackageName = (char *) xmalloc (strlen(arg)+1);
2275         strcpy (esPackageName, arg);
2276     }
2277     return 1;
2278 }
2279
2280 int cmd_proxy(char* arg)
2281 {
2282     if (*arg == '\0') {
2283                 xfree (yazProxy);
2284                 yazProxy = NULL;
2285         
2286     }
2287     xfree (yazProxy);
2288     yazProxy = NULL;
2289     if (*arg)
2290     {
2291         yazProxy = (char *) xmalloc (strlen(arg)+1);
2292         strcpy (yazProxy, arg);
2293     } 
2294     return 1;
2295 }
2296
2297 int cmd_charset(char* arg)
2298 {
2299     char l1[30], l2[30];
2300
2301     *l1 = *l2 = 0;
2302     if (sscanf(arg, "%29s %29s", l1, l2) < 1)
2303     {
2304         printf("Current character set is `%s'\n", (yazCharset) ? yazCharset:NULL);
2305         return 1;
2306     }
2307     xfree (yazCharset);
2308     yazCharset = NULL;
2309     if (*l1)
2310         yazCharset = xstrdup(l1);
2311     if (*l2)
2312     {
2313         odr_set_charset (out, l1, l2);
2314         odr_set_charset (in, l2, l1);
2315     }
2316     return 1;
2317 }
2318
2319 int cmd_lang(char* arg)
2320 {
2321     if (*arg == '\0') {
2322         printf("Current language is `%s'\n", (yazLang)?yazLang:NULL);
2323         return 1;
2324     }
2325     xfree (yazLang);
2326     yazLang = NULL;
2327     if (*arg)
2328     {
2329         yazLang = (char *) xmalloc (strlen(arg)+1);
2330         strcpy (yazLang, arg);
2331     } 
2332     return 1;
2333 }
2334
2335 int cmd_source(char* arg) 
2336 {
2337     /* first should open the file and read one line at a time.. */
2338     FILE* includeFile;
2339     char line[1024], *cp;
2340
2341     {
2342         char* args_end=(arg)+strlen(arg)-1; 
2343         while(isspace(*args_end)) 
2344         {*args_end=0;
2345         --args_end;}; 
2346     }
2347
2348     REMOVE_TAILING_BLANKS(arg);
2349     
2350     if(strlen(arg)<1) {
2351         fprintf(stderr,"Error in source command use a filename\n");
2352         return -1;
2353     }
2354     
2355     includeFile = fopen (arg, "r");
2356     
2357     if(!includeFile) {
2358         fprintf(stderr,"Unable to open file %s for reading\n",arg);
2359         return -1;
2360     }
2361     
2362     while(!feof(includeFile)) {
2363         memset(line,0,sizeof(line));
2364         fgets(line,sizeof(line),includeFile);
2365         
2366         if(strlen(line) < 2) continue;
2367         if(line[0] == '#') continue;
2368         
2369         if ((cp = strrchr (line, '\n')))
2370             *cp = '\0';
2371         
2372         process_cmd_line(line);
2373     }
2374     
2375     if(fclose(includeFile)<0) {
2376         perror("unable to close include file");
2377         exit(1);
2378     }
2379     return 1;
2380 }
2381
2382 int cmd_subshell(char* args)
2383 {
2384     if(strlen(args)) 
2385         system(args);
2386     else 
2387         system(getenv("SHELL"));
2388     
2389     printf("\n");
2390     return 1;
2391 }
2392
2393 int cmd_set_apdufile(char* arg)
2394 {
2395     REMOVE_TAILING_BLANKS(arg);
2396   
2397     if(apdu_file && apdu_file != stderr) { /* don't close stdout*/
2398         perror("unable to close apdu log file");      
2399     }
2400     apdu_file=NULL;
2401   
2402     if(strlen(arg)<1) {
2403         return 1;
2404     }
2405   
2406     if(!strcmp(arg,"-")) 
2407         apdu_file=stderr;      
2408     else 
2409         apdu_file=fopen(arg, "a");
2410   
2411     if(!apdu_file) {
2412         perror("unable to open apdu log file no apdu log loaded");
2413     } else {
2414         odr_setprint(print, apdu_file); 
2415     }
2416   
2417     return 1;
2418 }
2419
2420 int cmd_set_cclfile(char* arg)
2421 {  
2422     FILE *inf;
2423
2424     REMOVE_TAILING_BLANKS(arg);
2425
2426     bibset = ccl_qual_mk (); 
2427     inf = fopen (arg, "r");
2428     if (inf)
2429     {
2430         ccl_qual_file (bibset, inf);
2431         fclose (inf);
2432     }
2433     strcpy(ccl_fields,arg);
2434     return 0;
2435 }
2436
2437
2438 int cmd_set_auto_reconnect(char* arg)
2439 {  
2440     REMOVE_TAILING_BLANKS(arg);
2441     
2442     if(strlen(arg)==0) {
2443         auto_reconnect = ! auto_reconnect;
2444     } else if(strcmp(arg,"on")==0) {
2445         auto_reconnect = 1;
2446     } else if(strcmp(arg,"off")==0) {
2447         auto_reconnect = 0;             
2448     } else {
2449         printf("Error use on or off\n");
2450         return 1;
2451     }
2452     
2453     if (auto_reconnect)
2454         printf("Set auto reconnect enabled.\n");
2455     else
2456         printf("Set auto reconnect disabled.\n");
2457     
2458     return 0;
2459 }
2460
2461 int cmd_set_marcdump(char* arg)
2462 {
2463     if(marcdump && marcdump != stderr) { /* don't close stdout*/
2464         perror("unable to close apdu log file");      
2465     }
2466     marcdump=NULL;
2467     
2468     if(strlen(arg)<1) {
2469         return 1;
2470     }
2471     
2472     if(!strcmp(arg,"-")) 
2473         marcdump=stderr;      
2474     else 
2475         marcdump=fopen(arg, "a");
2476     
2477     if(!marcdump) {
2478         perror("unable to open apdu marcdump file no marcdump done\n");
2479     }
2480     
2481     return 1;
2482 }
2483
2484 int cmd_set_proxy(char* arg)
2485 {
2486     if(yazProxy) free(yazProxy);
2487     yazProxy=NULL;
2488     
2489     if(strlen(arg) > 1) {
2490         yazProxy=strdup(arg);
2491     }
2492     return 1;
2493 }
2494
2495 /* 
2496    this command takes 3 arge {name class oid} 
2497 */
2498 int cmd_register_oid(char* args) {
2499     static struct {
2500         char* className;
2501         oid_class oclass;
2502     } oid_classes[] = {
2503         {"appctx",CLASS_APPCTX},
2504         {"absyn",CLASS_ABSYN},
2505         {"attset",CLASS_ATTSET},
2506         {"transyn",CLASS_TRANSYN},
2507         {"diagset",CLASS_DIAGSET},
2508         {"recsyn",CLASS_RECSYN},
2509         {"resform",CLASS_RESFORM},
2510         {"accform",CLASS_ACCFORM},
2511         {"extserv",CLASS_EXTSERV},
2512         {"userinfo",CLASS_USERINFO},
2513         {"elemspec",CLASS_ELEMSPEC},
2514         {"varset",CLASS_VARSET},
2515         {"schema",CLASS_SCHEMA},
2516         {"tagset",CLASS_TAGSET},
2517         {"general",CLASS_GENERAL},
2518         {0,(enum oid_class) 0}
2519     };
2520     char oname_str[101], oclass_str[101], oid_str[101];  
2521     char* name;
2522     int i;
2523     oid_class oidclass = CLASS_GENERAL;
2524     int val = 0, oid[OID_SIZE];
2525     struct oident * new_oident=NULL;
2526     
2527     if (sscanf (args, "%100[^ ] %100[^ ] %100s",
2528                 oname_str,oclass_str, oid_str) < 1) {
2529         printf("Error in regristrate command \n");
2530         return 0;
2531     }
2532     
2533     for (i = 0; oid_classes[i].className; i++) {
2534         if (!strcmp(oid_classes[i].className, oclass_str))
2535         {
2536             oidclass=oid_classes[i].oclass;
2537             break;
2538         }
2539     }
2540     
2541     if(!(oid_classes[i].className)) {
2542         printf("Unknonwn oid class %s\n",oclass_str);
2543         return 0;
2544     }
2545     
2546     i = 0;
2547     name = oid_str;
2548     val = 0;
2549     
2550     while (isdigit (*name))
2551     {
2552         val = val*10 + (*name - '0');
2553         name++;
2554         if (*name == '.')
2555         {
2556             if (i < OID_SIZE-1)
2557                 oid[i++] = val;
2558             val = 0;
2559             name++;
2560         }
2561     }
2562     oid[i] = val;
2563     oid[i+1] = -1;
2564     
2565     new_oident=oid_addent (oid,PROTO_GENERAL,oidclass,oname_str,VAL_DYNAMIC);  
2566     if(strcmp(new_oident->desc,oname_str)) {
2567         fprintf(stderr,"oid is already named as %s, regristration faild\n",
2568                 new_oident->desc);
2569     }
2570     return 1;  
2571 }
2572
2573 int cmd_push_command(char* arg) 
2574 {
2575 #if HAVE_READLINE_HISTORY_H
2576     if(strlen(arg)>1) 
2577         add_history(arg);
2578 #else 
2579     fprintf(stderr,"Not compiled with the readline/history module\n");
2580 #endif
2581     return 1;
2582 }
2583
2584 void source_rcfile() 
2585 {
2586     /*  Look for a $HOME/.yazclientrc and source it if it exists */
2587     struct stat statbuf;
2588     char buffer[1000];
2589     char* homedir=getenv("HOME");
2590     
2591     if(!homedir) return;
2592     
2593     sprintf(buffer,"%s/.yazclientrc",homedir);
2594     
2595     if(stat(buffer,&statbuf)==0) {
2596         cmd_source(buffer);
2597     }
2598     
2599     if(stat(".yazclientrc",&statbuf)==0) {
2600         cmd_source(".yazclientrc");
2601     }
2602 }
2603
2604
2605 static void initialize(void)
2606 {
2607     FILE *inf;
2608     int i;
2609     
2610     if (!(out = odr_createmem(ODR_ENCODE)) ||
2611         !(in = odr_createmem(ODR_DECODE)) ||
2612         !(print = odr_createmem(ODR_PRINT)))
2613     {
2614         fprintf(stderr, "failed to allocate ODR streams\n");
2615         exit(1);
2616     }
2617     oid_init();
2618     
2619     setvbuf(stdout, 0, _IONBF, 0);
2620     if (apdu_file)
2621         odr_setprint(print, apdu_file);
2622
2623     bibset = ccl_qual_mk (); 
2624     inf = fopen (ccl_fields, "r");
2625     if (inf)
2626     {
2627         ccl_qual_file (bibset, inf);
2628         fclose (inf);
2629     }
2630     cmd_base("Default");
2631
2632 #if HAVE_READLINE_READLINE_H
2633     rl_attempted_completion_function = (CPPFunction*)readline_completer;
2634 #endif
2635     
2636     
2637     for(i=0; i<maxOtherInfosSupported; ++i) {
2638         extraOtherInfos[i].oidval = -1;
2639     }
2640     
2641     source_rcfile();
2642 }
2643
2644
2645 #if HAVE_GETTIMEOFDAY
2646 struct timeval tv_start, tv_end;
2647 #endif
2648
2649 void wait_and_handle_responce() 
2650 {
2651     
2652     int res;
2653     char *netbuffer= 0;
2654     int netbufferlen = 0;
2655     Z_APDU *apdu;
2656     
2657     
2658     if (conn)
2659     {
2660         do
2661         {
2662             if ((res = cs_get(conn, &netbuffer, &netbufferlen)) < 0)
2663             {
2664                 printf("Target closed connection\n");
2665                 close_session ();
2666                 break;
2667             }
2668             if (!res)
2669             {
2670                 printf("Target closed connection.\n");
2671                 close_session ();
2672                 break;
2673             }
2674             odr_reset(in); /* release APDU from last round */
2675             record_last = 0;
2676             odr_setbuf(in, netbuffer, res, 0);
2677             if (!z_APDU(in, &apdu, 0, 0))
2678             {
2679                 odr_perror(in, "Decoding incoming APDU");
2680                 fprintf(stderr, "[Near %d]\n", odr_offset(in));
2681                 fprintf(stderr, "Packet dump:\n---------\n");
2682                 odr_dumpBER(stderr, netbuffer, res);
2683                 fprintf(stderr, "---------\n");
2684                 if (apdu_file)
2685                     z_APDU(print, &apdu, 0, 0);
2686                 close_session ();
2687                 break;
2688             }
2689             if (apdu_file && !z_APDU(print, &apdu, 0, 0))
2690             {
2691                 odr_perror(print, "Failed to print incoming APDU");
2692                 odr_reset(print);
2693                 continue;
2694             }
2695             switch(apdu->which)
2696             {
2697             case Z_APDU_initResponse:
2698                 process_initResponse(apdu->u.initResponse);
2699                 break;
2700             case Z_APDU_searchResponse:
2701                 process_searchResponse(apdu->u.searchResponse);
2702                 break;
2703             case Z_APDU_scanResponse:
2704                 process_scanResponse(apdu->u.scanResponse);
2705                 break;
2706             case Z_APDU_presentResponse:
2707                 print_refid (apdu->u.presentResponse->referenceId);
2708                 setno +=
2709                     *apdu->u.presentResponse->numberOfRecordsReturned;
2710                 if (apdu->u.presentResponse->records)
2711                     display_records(apdu->u.presentResponse->records);
2712                 else
2713                     printf("No records.\n");
2714                 printf ("nextResultSetPosition = %d\n",
2715                         *apdu->u.presentResponse->nextResultSetPosition);
2716                 break;
2717             case Z_APDU_sortResponse:
2718                 process_sortResponse(apdu->u.sortResponse);
2719                 break;
2720             case Z_APDU_extendedServicesResponse:
2721                 printf("Got extended services response\n");
2722                 process_ESResponse(apdu->u.extendedServicesResponse);
2723                 break;
2724             case Z_APDU_close:
2725                 printf("Target has closed the association.\n");
2726                 process_close(apdu->u.close);
2727                 break;
2728             case Z_APDU_resourceControlRequest:
2729                 process_resourceControlRequest
2730                     (apdu->u.resourceControlRequest);
2731                 break;
2732             case Z_APDU_deleteResultSetResponse:
2733                 process_deleteResultSetResponse(apdu->u.
2734                                                 deleteResultSetResponse);
2735                 break;
2736             default:
2737                 printf("Received unknown APDU type (%d).\n", 
2738                        apdu->which);
2739                 close_session ();
2740             }
2741         }
2742         while (conn && cs_more(conn));
2743 #if HAVE_GETTIMEOFDAY
2744         gettimeofday (&tv_end, 0);
2745 #if 0
2746         printf ("S/U S/U=%ld/%ld %ld/%ld",
2747                 (long) tv_start.tv_sec,
2748                 (long) tv_start.tv_usec,
2749                 (long) tv_end.tv_sec,
2750                 (long) tv_end.tv_usec);
2751 #endif
2752         printf ("Elapsed: %.6f\n",
2753                 (double) tv_end.tv_usec / 1e6 + tv_end.tv_sec -
2754                 ((double) tv_start.tv_usec / 1e6 + tv_start.tv_sec));
2755 #endif
2756     }
2757     xfree (netbuffer);
2758 }
2759
2760
2761 int cmd_cclparse(char* arg) 
2762 {
2763     int error, pos;
2764     struct ccl_rpn_node *rpn=NULL;
2765     
2766     
2767     rpn = ccl_find_str (bibset, arg, &error, &pos);
2768     
2769     if (error) {
2770         printf ("%*s^ - ", 3+strlen(last_cmd)+1+pos, " ");
2771         printf ("%s\n", ccl_err_msg (error));
2772     }
2773     else
2774     {
2775         if (rpn)
2776         {       
2777             ccl_pr_tree(rpn, stdout); 
2778         }
2779     }
2780     if (rpn)
2781         ccl_rpn_delete(rpn);
2782     
2783     printf ("\n");
2784     
2785     return 0;
2786 }
2787
2788
2789 int cmd_set_otherinfo(char* args)
2790 {
2791     char oid[101], otherinfoString[101];
2792     int otherinfoNo;
2793     int sscan_res;
2794     int oidval;
2795     
2796     sscan_res = sscanf (args, "%d %100[^ ] %100s", &otherinfoNo, oid, otherinfoString);
2797     if(sscan_res==1) {
2798         /* reset this otherinfo */
2799         if(otherinfoNo>=maxOtherInfosSupported) {
2800             printf("Error otherinfo index to large (%d>%d)\n",otherinfoNo,maxOtherInfosSupported);
2801         }
2802         extraOtherInfos[otherinfoNo].oidval = -1;
2803         if(extraOtherInfos[otherinfoNo].value) free(extraOtherInfos[otherinfoNo].value);                        
2804         return 0;
2805     }
2806     if (sscan_res<3) {
2807         printf("Error in set_otherinfo command \n");
2808         return 0;
2809     }
2810     
2811     if(otherinfoNo>=maxOtherInfosSupported) {
2812         printf("Error otherinfo index to large (%d>%d)\n",otherinfoNo,maxOtherInfosSupported);
2813     }
2814     
2815     
2816     oidval = oid_getvalbyname (oid);
2817     if(oidval == -1 ) {
2818         printf("Error in set_otherinfo command unknown oid %s \n",oid);
2819         return 0;
2820     }
2821     extraOtherInfos[otherinfoNo].oidval = oidval;
2822     if(extraOtherInfos[otherinfoNo].value) free(extraOtherInfos[otherinfoNo].value);
2823     extraOtherInfos[otherinfoNo].value = strdup(otherinfoString);
2824     
2825     return 0;
2826 }
2827
2828 int cmd_list_otherinfo(char* args)
2829 {
2830     int i;         
2831     
2832     if(strlen(args)>0) {
2833         i = atoi(args);
2834         if( i >= maxOtherInfosSupported ) {
2835             printf("Error otherinfo index to large (%d>%d)\n",i,maxOtherInfosSupported);
2836             return 0;
2837         }
2838
2839         if(extraOtherInfos[i].oidval != -1) 
2840             printf("  otherinfo %d %s %s\n",
2841                    i,
2842                    yaz_z3950_oid_value_to_str(
2843                        (enum oid_value) extraOtherInfos[i].oidval,
2844                        CLASS_RECSYN),
2845                    extraOtherInfos[i].value);
2846         
2847     } else {            
2848         for(i=0; i<maxOtherInfosSupported; ++i) {
2849             if(extraOtherInfos[i].oidval != -1) 
2850                 printf("  otherinfo %d %s %s\n",
2851                        i,
2852                        yaz_z3950_oid_value_to_str(
2853                            (enum oid_value) extraOtherInfos[i].oidval,
2854                            CLASS_RECSYN),
2855                        extraOtherInfos[i].value);
2856         }
2857         
2858     }
2859     return 0;
2860 }
2861
2862
2863 int cmd_list_all(char* args) {
2864     int i;
2865     
2866     /* connection options */
2867     if(conn) {
2868         printf("Connected to         : %s\n",last_open_command);
2869     } else {
2870         if(last_open_command) 
2871             printf("Not connected to     : %s\n",last_open_command);
2872         else 
2873             printf("Not connected        : \n");
2874         
2875     }
2876     if(yazProxy) printf("using proxy          : %s\n",yazProxy);                
2877     
2878     printf("auto_reconnect       : %s\n",auto_reconnect?"on":"off");
2879     
2880     if (!auth) {
2881         printf("Authentication       : none\n");
2882     } else {
2883         switch(auth->which) {
2884         case Z_IdAuthentication_idPass:
2885             printf("Authentication       : IdPass\n"); 
2886             printf("    Login User       : %s\n",auth->u.idPass->userId?auth->u.idPass->userId:"");
2887             printf("    Login Group      : %s\n",auth->u.idPass->groupId?auth->u.idPass->groupId:"");
2888             printf("    Password         : %s\n",auth->u.idPass->password?auth->u.idPass->password:"");
2889             break;
2890         case Z_IdAuthentication_open:
2891             printf("Authentication       : psOpen\n");                  
2892             printf("    Open string      : %s\n",auth->u.open); 
2893             break;
2894         default:
2895             printf("Authentication       : Unknown\n");
2896         }
2897     }
2898     if ( yazCharset ) printf("Character set        : `%s'\n", (yazCharset) ? yazCharset:NULL);
2899     
2900     /* bases */
2901     printf("Bases                : ");
2902     for (i = 0; i<num_databaseNames; i++) printf("%s ",databaseNames[i]);
2903     printf("\n");
2904     
2905     /* Query options */
2906     printf("CCL file             : %s\n",ccl_fields);
2907     printf("Query type           : %s\n",query_type_as_string(queryType));
2908     
2909     printf("Named Result Sets    : %s\n",setnumber==-1?"off":"on");
2910     
2911     /* piggy back options */
2912     printf("ssub/lslb/mspn       : %d/%d/%d\n",smallSetUpperBound,largeSetLowerBound,mediumSetPresentNumber);
2913     
2914     /* print present related options */
2915     printf("Format               : %s\n",yaz_z3950_oid_value_to_str(recordsyntax,CLASS_RECSYN));
2916     printf("Schema               : %s\n",yaz_z3950_oid_value_to_str(schema,CLASS_SCHEMA));
2917     printf("Elements             : %s\n",elementSetNames?elementSetNames->u.generic:"");
2918     
2919     /* loging options */
2920     printf("APDU log             : %s\n",apdu_file?"on":"off");
2921     printf("Record log           : %s\n",marcdump?"on":"off");
2922     
2923     /* other infos */
2924     printf("Other Info: \n");
2925     cmd_list_otherinfo("");
2926     
2927     return 0;
2928 }
2929
2930 int cmd_clear_otherinfo(char* args) 
2931 {
2932     if(strlen(args)>0) {
2933         int otherinfoNo;
2934         otherinfoNo = atoi(args);
2935         if( otherinfoNo >= maxOtherInfosSupported ) {
2936             printf("Error otherinfo index to large (%d>%d)\n",otherinfoNo,maxOtherInfosSupported);
2937             return 0;
2938         }
2939         
2940         if(extraOtherInfos[otherinfoNo].oidval != -1) {                 
2941             /* only clear if set. */
2942             extraOtherInfos[otherinfoNo].oidval=-1;
2943             free(extraOtherInfos[otherinfoNo].value);
2944         }
2945     } else {
2946         int i;
2947         
2948         for(i=0; i<maxOtherInfosSupported; ++i) {
2949             if (extraOtherInfos[i].oidval!=-1 ) {                               
2950                 extraOtherInfos[i].oidval=-1;
2951                 free(extraOtherInfos[i].value);
2952             }
2953         }
2954     }
2955     return 0;
2956 }
2957
2958 static int cmd_help (char *line);
2959
2960 typedef char *(*completerFunctionType)(const char *text, int state);
2961
2962 static struct {
2963     char *cmd;
2964     int (*fun)(char *arg);
2965     char *ad;
2966         completerFunctionType rl_completerfunction;
2967     int complete_filenames;
2968     char **local_tabcompletes;
2969 } cmd[] = {
2970     {"open", cmd_open, "('tcp'|'ssl')':<host>[':'<port>][/<db>]",NULL,0,NULL},
2971     {"quit", cmd_quit, "",NULL,0,NULL},
2972     {"find", cmd_find, "<query>",NULL,0,NULL},
2973     {"delete", cmd_delete, "<setname>",NULL,0,NULL},
2974     {"base", cmd_base, "<base-name>",NULL,0,NULL},
2975     {"show", cmd_show, "<rec#>['+'<#recs>['+'<setname>]]",NULL,0,NULL},
2976     {"scan", cmd_scan, "<term>",NULL,0,NULL},
2977     {"sort", cmd_sort, "<sortkey> <flag> <sortkey> <flag> ...",NULL,0,NULL},
2978     {"sort+", cmd_sort_newset, "<sortkey> <flag> <sortkey> <flag> ...",NULL,0,NULL},
2979     {"authentication", cmd_authentication, "<acctstring>",NULL,0,NULL},
2980     {"lslb", cmd_lslb, "<largeSetLowerBound>",NULL,0,NULL},
2981     {"ssub", cmd_ssub, "<smallSetUpperBound>",NULL,0,NULL},
2982     {"mspn", cmd_mspn, "<mediumSetPresentNumber>",NULL,0,NULL},
2983     {"status", cmd_status, "",NULL,0,NULL},
2984     {"setnames", cmd_setnames, "",NULL,0,NULL},
2985     {"cancel", cmd_cancel, "",NULL,0,NULL},
2986     {"format", cmd_format, "<recordsyntax>",complete_format,0,NULL},
2987     {"schema", cmd_schema, "<schema>",complete_schema,0,NULL},
2988     {"elements", cmd_elements, "<elementSetName>",NULL,0,NULL},
2989     {"close", cmd_close, "",NULL,0,NULL},
2990     {"attributeset", cmd_attributeset, "<attrset>",complete_attributeset,0,NULL},
2991     {"querytype", cmd_querytype, "<type>",complete_querytype,0,NULL},
2992     {"refid", cmd_refid, "<id>",NULL,0,NULL},
2993     {"itemorder", cmd_itemorder, "ill|item <itemno>",NULL,0,NULL},
2994     {"update", cmd_update, "<action> <recid> [<file>]",NULL,0,NULL},
2995     {"packagename", cmd_packagename, "<packagename>",NULL,0,NULL},
2996     {"proxy", cmd_proxy, "[('tcp'|'ssl')]<host>[':'<port>]",NULL,0,NULL},
2997     {"charset", cmd_charset, "<charset_name>",NULL,0,NULL},
2998     {"lang", cmd_lang, "<language_code>",NULL,0,NULL},
2999     {".", cmd_source, "<filename>",NULL,1,NULL},
3000     {"!", cmd_subshell, "Subshell command",NULL,1,NULL},
3001     {"set_apdufile", cmd_set_apdufile, "<filename>",NULL,1,NULL},
3002     {"set_marcdump", cmd_set_marcdump," <filename>",NULL,1,NULL},
3003     {"set_cclfile", cmd_set_cclfile," <filename>",NULL,1,NULL},
3004     {"set_auto_reconnect", cmd_set_auto_reconnect," on|off",complete_auto_reconnect,1,NULL},
3005         {"set_otherinfo", cmd_set_otherinfo,"<otherinfoinddex> <oid> <string>",NULL,0,NULL},
3006     {"register_oid", cmd_register_oid,"<name> <class> <oid>",NULL,0,NULL},
3007     {"push_command", cmd_push_command,"<command>",command_generator,0,NULL},
3008         {"register_tab", cmd_register_tab,"<commandname> <tab>",command_generator,0,NULL},
3009         {"cclparse", cmd_cclparse,"<ccl find command>",NULL,0,NULL},
3010         {"list_otherinfo",cmd_list_otherinfo,"[otherinfoinddex]",NULL,0,NULL},
3011         {"list_all",cmd_list_all,"",NULL,0,NULL},
3012         {"clear_otherinfo",cmd_clear_otherinfo,"",NULL,0,NULL},
3013     /* Server Admin Functions */
3014     {"adm-reindex", cmd_adm_reindex, "<database-name>",NULL,0,NULL},
3015     {"adm-truncate", cmd_adm_truncate, "('database'|'index')<object-name>",NULL,0,NULL},
3016     {"adm-create", cmd_adm_create, "",NULL,0,NULL},
3017     {"adm-drop", cmd_adm_drop, "('database'|'index')<object-name>",NULL,0,NULL},
3018     {"adm-import", cmd_adm_import, "<record-type> <dir> <pattern>",NULL,0,NULL},
3019     {"adm-refresh", cmd_adm_refresh, "",NULL,0,NULL},
3020     {"adm-commit", cmd_adm_commit, "",NULL,0,NULL},
3021     {"adm-shutdown", cmd_adm_shutdown, "",NULL,0,NULL},
3022     {"adm-startup", cmd_adm_startup, "",NULL,0,NULL},
3023     {"help", cmd_help, "", NULL,0,NULL},
3024     {0,0,0,0,0,0}
3025 };
3026
3027 static int cmd_help (char *line)
3028 {
3029     int i;
3030     char topic[21];
3031     
3032     *topic = 0;
3033     sscanf (line, "%20s", topic);
3034
3035     if (*topic == 0)
3036         printf("Commands:\n");
3037     for (i = 0; cmd[i].cmd; i++)
3038         if (*topic == 0 || strcmp (topic, cmd[i].cmd) == 0)
3039             printf("   %s %s\n", cmd[i].cmd, cmd[i].ad);
3040     if (strcmp (topic, "find") == 0)
3041     {
3042         printf ("RPN:\n");
3043         printf (" \"term\"                        Simple Term\n");
3044         printf (" @attr [attset] type=value op  Attribute\n");
3045         printf (" @and opl opr                  And\n");
3046         printf (" @or opl opr                   Or\n");
3047         printf (" @not opl opr                  And-Not\n");
3048         printf (" @set set                      Result set\n");
3049         printf ("\n");
3050         printf ("Bib-1 attribute types\n");
3051         printf ("1=Use:         ");
3052         printf ("4=Title 7=ISBN 8=ISSN 30=Date 62=Abstract 1003=Author 1016=Any\n");
3053         printf ("2=Relation:    ");
3054         printf ("1<   2<=  3=  4>=  5>  6!=  102=Relevance\n");
3055         printf ("3=Position:    ");
3056         printf ("1=First in Field  2=First in subfield  3=Any position\n");
3057         printf ("4=Structure:   ");
3058         printf ("1=Phrase  2=Word  3=Key  4=Year  5=Date  6=WordList\n");
3059         printf ("5=Truncation:  ");
3060         printf ("1=Right  2=Left  3=L&R  100=No  101=#  102=Re-1  103=Re-2\n");
3061         printf ("6=Completeness:");
3062         printf ("1=Incomplete subfield  2=Complete subfield  3=Complete field\n");
3063     }
3064     return 1;
3065 }
3066
3067 int cmd_register_tab(char* arg) {
3068         
3069     char command[101], tabargument[101];
3070     int i;
3071     int num_of_tabs;
3072     char** tabslist;
3073     
3074     if (sscanf (arg, "%100s %100s", command, tabargument) < 1) {
3075         return 0;
3076     }
3077     
3078     /* locate the amdn in the list */
3079     for (i = 0; cmd[i].cmd; i++) {
3080         if (!strncmp(cmd[i].cmd, command, strlen(command))) {
3081             break;
3082         }
3083     }
3084     
3085     if(!cmd[i].cmd) { 
3086         fprintf(stderr,"Unknown command %s\n",command);
3087         return 1;
3088     }
3089     
3090         
3091     if(!cmd[i].local_tabcompletes)
3092         cmd[i].local_tabcompletes = (char **) calloc(1,sizeof(char**));
3093     
3094     num_of_tabs=0;              
3095     
3096     tabslist = cmd[i].local_tabcompletes;
3097     for(;tabslist && *tabslist;tabslist++) {
3098         num_of_tabs++;
3099     }
3100     
3101     cmd[i].local_tabcompletes =  (char **)
3102         realloc(cmd[i].local_tabcompletes,(num_of_tabs+2)*sizeof(char**));
3103     tabslist=cmd[i].local_tabcompletes;
3104     tabslist[num_of_tabs]=strdup(tabargument);
3105     tabslist[num_of_tabs+1]=NULL;
3106     return 1;
3107 }
3108
3109
3110 void process_cmd_line(char* line)
3111 {  
3112     int i,res;
3113     char word[32], arg[1024];
3114     
3115 #if HAVE_GETTIMEOFDAY
3116     gettimeofday (&tv_start, 0);
3117 #endif
3118     
3119     if ((res = sscanf(line, "%31s %1023[^;]", word, arg)) <= 0)
3120     {
3121         strcpy(word, last_cmd);
3122         *arg = '\0';
3123     }
3124     else if (res == 1)
3125         *arg = 0;
3126     strcpy(last_cmd, word);
3127     
3128     /* removed tailing spaces from the arg command */
3129     { 
3130         char* p;
3131         char* lastnonspace=NULL;
3132         p = arg;
3133         
3134         for(;*p; ++p) {
3135             if(!isspace(*p)) {
3136                 lastnonspace = p;
3137             }
3138         }
3139         if(lastnonspace) 
3140             *(++lastnonspace) = 0;
3141     }
3142     
3143     
3144     for (i = 0; cmd[i].cmd; i++)
3145         if (!strncmp(cmd[i].cmd, word, strlen(word)))
3146         {
3147             res = (*cmd[i].fun)(arg);
3148             break;
3149         }
3150     
3151     if (!cmd[i].cmd) /* dump our help-screen */
3152     {
3153         printf("Unknown command: %s.\n", word);
3154         printf("use help for list of commands\n");
3155         /* cmd_help (""); */
3156         res = 1;
3157     }
3158     
3159     if(apdu_file) fflush(apdu_file);
3160     
3161     if (res >= 2)
3162         wait_and_handle_responce();
3163     
3164     if(apdu_file)
3165         fflush(apdu_file);
3166     if(marcdump)
3167         fflush(marcdump);
3168 }
3169
3170
3171 char *command_generator(const char *text, int state) 
3172 {
3173     static int idx; 
3174     if (state==0) {
3175         idx = 0;
3176     }
3177     for( ; cmd[idx].cmd; ++idx) {
3178         if (!strncmp(cmd[idx].cmd,text,strlen(text))) {
3179             ++idx;  /* skip this entry on the next run */
3180             return strdup(cmd[idx-1].cmd);
3181         }
3182     }
3183     return NULL;
3184 }
3185
3186
3187 /* 
3188    This function only known how to complete on the first word
3189 */
3190 char ** readline_completer(char *text, int start, int end) {
3191 #if HAVE_READLINE_READLINE_H
3192
3193         completerFunctionType completerToUse;
3194         
3195     if(start == 0) {
3196 #if HAVE_READLINE_RL_COMPLETION_MATCHES
3197         char** res=rl_completion_matches(text,
3198                                       command_generator); 
3199 #else
3200         char** res=completion_matches(text,
3201                                       (CPFunction*)command_generator); 
3202 #endif
3203         rl_attempted_completion_over = 1;
3204         return res;
3205     } else {
3206         char arg[1024],word[32];
3207         int i=0 ,res;
3208         if ((res = sscanf(rl_line_buffer, "%31s %1023[^;]", word, arg)) <= 0) {     
3209             rl_attempted_completion_over = 1;
3210             return NULL;
3211         }
3212         
3213         for (i = 0; cmd[i].cmd; i++) {
3214             if (!strncmp(cmd[i].cmd, word, strlen(word))) {
3215                 break;
3216             }
3217         }
3218         
3219         if(!cmd[i].cmd) return NULL;
3220         
3221         
3222         curret_global_list = cmd[i].local_tabcompletes;
3223         
3224         completerToUse = cmd[i].rl_completerfunction;
3225         if(completerToUse==NULL)  /* if no pr. command completer is defined use the default completer */
3226             completerToUse = default_completer;
3227         
3228         if(completerToUse) {
3229 #ifdef HAVE_READLINE_RL_COMPLETION_MATCHES
3230             char** res=
3231                 rl_completion_matches(text,
3232                                       completerToUse);
3233 #else
3234             char** res=
3235                 completion_matches(text,
3236                                    (CPFunction*)completerToUse);
3237 #endif
3238             if(!cmd[i].complete_filenames) 
3239                 rl_attempted_completion_over = 1;
3240             return res;
3241         } else {
3242             if(!cmd[i].complete_filenames) 
3243                 rl_attempted_completion_over = 1;
3244             return 0;
3245         }
3246     }
3247 #else 
3248     return 0;
3249 #endif 
3250 }
3251
3252
3253 static void client(void)
3254 {
3255     char line[1024];
3256
3257     line[1023] = '\0';
3258
3259 #if HAVE_GETTIMEOFDAY
3260     gettimeofday (&tv_start, 0);
3261 #endif
3262
3263     while (1)
3264     {
3265         char *line_in = NULL;
3266 #if HAVE_READLINE_READLINE_H
3267         if (isatty(0))
3268         {
3269             line_in=readline(C_PROMPT);
3270             if (!line_in)
3271                 break;
3272 #if HAVE_READLINE_HISTORY_H
3273             if (*line_in)
3274                 add_history(line_in);
3275 #endif
3276             strncpy(line, line_in, 1023);
3277             free (line_in);
3278         }
3279 #endif 
3280         if (!line_in)
3281         {
3282             char *end_p;
3283             printf (C_PROMPT);
3284             fflush(stdout);
3285             if (!fgets(line, 1023, stdin))
3286                 break;
3287             if ((end_p = strchr (line, '\n')))
3288                 *end_p = '\0';
3289         }
3290         process_cmd_line(line);
3291     }
3292 }
3293
3294 int main(int argc, char **argv)
3295 {
3296     char *prog = *argv;
3297     char *open_command = 0;
3298     char *auth_command = 0;
3299     char *arg;
3300     int ret;
3301     
3302 #if HAVE_LOCALE_H
3303     if (!setlocale(LC_CTYPE, ""))
3304         fprintf (stderr, "setlocale failed\n");
3305 #endif
3306 #if HAVE_LANGINFO_H
3307     codeset = nl_langinfo(CODESET);
3308 #endif
3309
3310     while ((ret = options("k:c:a:m:v:p:u:t:", argv, argc, &arg)) != -2)
3311     {
3312         switch (ret)
3313         {
3314         case 0:
3315             if (!open_command)
3316             {
3317                 open_command = (char *) xmalloc (strlen(arg)+6);
3318                 strcpy (open_command, "open ");
3319                 strcat (open_command, arg);
3320             }
3321             break;
3322         case 'k':
3323             kilobytes = atoi(arg);
3324             break;
3325         case 'm':
3326             if (!(marcdump = fopen (arg, "a")))
3327             {
3328                 perror (arg);
3329                 exit (1);
3330             }
3331             break;
3332         case 't':
3333             codeset = arg;
3334             break;
3335         case 'c':
3336             strncpy (ccl_fields, arg, sizeof(ccl_fields)-1);
3337             ccl_fields[sizeof(ccl_fields)-1] = '\0';
3338             break;
3339         case 'a':
3340             if (!strcmp(arg, "-"))
3341                 apdu_file=stderr;
3342             else
3343                 apdu_file=fopen(arg, "a");
3344             break;
3345         case 'p':
3346             yazProxy=strdup(arg);
3347             break;
3348         case 'u':
3349             if (!auth_command)
3350             {
3351                 auth_command = (char *) xmalloc (strlen(arg)+6);
3352                 strcpy (auth_command, "auth ");
3353                 strcat (auth_command, arg);
3354             }
3355             break;
3356         case 'v':
3357             yaz_log_init (yaz_log_mask_str(arg), "", NULL);
3358             break;
3359         default:
3360             fprintf (stderr, "Usage: %s [-m <marclog>] [ -a <apdulog>] "
3361                      "[-c cclfields]\n      [-p <proxy-addr>] [-u <auth>] "
3362                      "[-k size] [<server-addr>]\n",
3363                      prog);
3364             exit (1);
3365         }      
3366     }
3367     initialize();
3368     if (auth_command)
3369     {
3370 #ifdef HAVE_GETTIMEOFDAY
3371         gettimeofday (&tv_start, 0);
3372 #endif
3373         process_cmd_line (auth_command);
3374 #if HAVE_READLINE_HISTORY_H
3375         add_history(auth_command);
3376 #endif
3377         xfree(auth_command);
3378     }
3379     if (open_command)
3380     {
3381 #ifdef HAVE_GETTIMEOFDAY
3382         gettimeofday (&tv_start, 0);
3383 #endif
3384         process_cmd_line (open_command);
3385 #if HAVE_READLINE_HISTORY_H
3386         add_history(open_command);
3387 #endif
3388         xfree(open_command);
3389     }
3390     client ();
3391     exit (0);
3392 }
3393
3394 /*
3395  * Local variables:
3396  * tab-width: 8
3397  * c-basic-offset: 4
3398  * End:
3399  * vim600: sw=4 ts=8 fdm=marker
3400  * vim<600: sw=4 ts=8
3401  */