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