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