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