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