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