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