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