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