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