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