Handle NULL SRU addinfo in printing
[yaz-moved-to-github.git] / zoom / zoomsh.c
1 /*
2  * Copyright (C) 1995-2007, Index Data ApS
3  * See the file LICENSE for details.
4  *
5  * $Id: zoomsh.c,v 1.50 2007-09-11 15:16:20 adam Exp $
6  */
7
8 /** \file zoomsh.c
9     \brief ZOOM C command line tool (shell)
10 */
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <ctype.h>
16
17 #include <yaz/comstack.h>
18
19 #if HAVE_READLINE_READLINE_H
20 #include <readline/readline.h> 
21 #endif
22 #if HAVE_READLINE_HISTORY_H
23 #include <readline/history.h>
24 #endif
25
26 #include <yaz/xmalloc.h>
27
28 #include <yaz/log.h>
29 #include <yaz/nmem.h>
30 #include <yaz/zoom.h>
31
32 #define MAX_CON 100
33
34 static int next_token(const char **cpp, const char **t_start)
35 {
36     int len = 0;
37     const char *cp = *cpp;
38     while (*cp == ' ')
39         cp++;
40     if (*cp == '"')
41     {
42         cp++;
43         *t_start = cp;
44         while (*cp && *cp != '"')
45         {
46             cp++;
47             len++;
48         }
49         if (*cp)
50             cp++;
51     }
52     else
53     {
54         *t_start = cp;
55         while (*cp && *cp != ' ' && *cp != '\r' && *cp != '\n')
56         {
57             cp++;
58             len++;
59         }
60         if (len == 0)
61             len = -1;
62     }
63     *cpp = cp;
64     return len;  /* return -1 if no token was read .. */
65 }
66
67 static int next_token_copy(const char **cpp, char *buf_out, int buf_max)
68 {
69     const char *start;
70     int len = next_token(cpp, &start);
71     if (len < 0)
72     {
73         *buf_out = 0;
74         return len;
75     }
76     if (len >= buf_max)
77         len = buf_max-1;
78     memcpy(buf_out, start, len);
79     buf_out[len] = '\0';
80     return len;
81 }
82
83 static int is_command(const char *cmd_str, const char *this_str, int this_len)
84 {
85     int cmd_len = strlen(cmd_str);
86     if (cmd_len != this_len)
87         return 0;
88     if (memcmp(cmd_str, this_str, cmd_len))
89         return 0;
90     return 1;
91 }
92
93 static void cmd_set(ZOOM_connection *c, ZOOM_resultset *r,
94                     ZOOM_options options,
95                     const char **args)
96 {
97     char key[40], val[80];
98
99     if (next_token_copy(args, key, sizeof(key)) < 0)
100     {
101         printf("missing argument for set\n");
102         return ;
103     }
104     if (next_token_copy(args, val, sizeof(val)) < 0)
105         ZOOM_options_set(options, key, 0);
106     else
107         ZOOM_options_set(options, key, val);
108 }
109
110 static void cmd_get(ZOOM_connection *c, ZOOM_resultset *r,
111                     ZOOM_options options,
112                     const char **args)
113 {
114     char key[40];
115     if (next_token_copy(args, key, sizeof(key)) < 0)
116     {
117         printf("missing argument for get\n");
118     }
119     else
120     {
121         const char *val = ZOOM_options_get(options, key);
122         printf("%s = %s\n", key, val ? val : "<null>");
123     }
124 }
125
126 static void cmd_rget(ZOOM_connection *c, ZOOM_resultset *r,
127                      ZOOM_options options,
128                      const char **args)
129 {
130     char key[40];
131     if (next_token_copy(args, key, sizeof(key)) < 0)
132     {
133         printf("missing argument for get\n");
134     }
135     else
136     {
137         int i;
138         for (i = 0; i<MAX_CON; i++)
139         {
140             const char *val;
141             if (!r[i])
142                 continue;
143             
144             val = ZOOM_resultset_option_get(r[i], key);
145             printf("%s = %s\n", key, val ? val : "<null>");
146         }
147     }
148 }
149
150 static void cmd_close(ZOOM_connection *c, ZOOM_resultset *r,
151                       ZOOM_options options,
152                       const char **args)
153 {
154     char host[60];
155     int i;
156     next_token_copy(args, host, sizeof(host));
157     for (i = 0; i<MAX_CON; i++)
158     {
159         const char *h;
160         if (!c[i])
161             continue;
162         if ((h = ZOOM_connection_option_get(c[i], "host"))
163             && !strcmp(h, host))
164         {
165             ZOOM_connection_destroy(c[i]);
166             c[i] = 0;
167         }
168         else if (*host == '\0')
169         {
170             ZOOM_connection_destroy(c[i]);
171             c[i] = 0;
172         }
173     }
174 }
175
176 static void display_records(ZOOM_connection c,
177                             ZOOM_resultset r,
178                             int start, int count)
179 {
180     int i;
181     for (i = 0; i<count; i++)
182     {
183         int pos = i + start;
184         ZOOM_record rec = ZOOM_resultset_record(r, pos);
185         const char *db = ZOOM_record_get(rec, "database", 0);
186         
187         if (ZOOM_record_error(rec, 0, 0, 0))
188         {
189             const char *msg;
190             const char *addinfo;
191             const char *diagset;
192             int error = ZOOM_record_error(rec, &msg, &addinfo, &diagset);
193             
194             printf("%d %s: %s (%s:%d) %s\n", pos, (db ? db : "unknown"),
195                    msg, diagset, error, addinfo ? addinfo : "none");
196         }
197         else
198         {
199             int len, opac_len;
200             const char *render = ZOOM_record_get(rec, "render", &len);
201             const char *opac_render = ZOOM_record_get(rec, "opac", &opac_len);
202             const char *syntax = ZOOM_record_get(rec, "syntax", 0);
203             const char *schema = ZOOM_record_get(rec, "schema", 0);
204             /* if rec is non-null, we got a record for display */
205             if (rec)
206             {
207                 printf("%d database=%s syntax=%s schema=%s\n",
208                        pos, (db ? db : "unknown"), syntax,
209                        schema ? schema : "unknown");
210                 if (render)
211                     fwrite(render, 1, len, stdout);
212                 printf("\n");
213                 if (opac_render)
214                     fwrite(opac_render, 1, opac_len, stdout);
215             }
216         }
217     }
218 }
219
220 static void cmd_show(ZOOM_connection *c, ZOOM_resultset *r,
221                      ZOOM_options options,
222                      const char **args)
223 {
224     int i;
225     char start_str[10], count_str[10];
226
227     if (next_token_copy(args, start_str, sizeof(start_str)) >= 0)
228         ZOOM_options_set(options, "start", start_str);
229
230     if (next_token_copy(args, count_str, sizeof(count_str)) >= 0)
231         ZOOM_options_set(options, "count", count_str);
232
233     for (i = 0; i<MAX_CON; i++)
234         ZOOM_resultset_records(r[i], 0, atoi(start_str), atoi(count_str));
235     while (ZOOM_event(MAX_CON, c))
236         ;
237
238     for (i = 0; i<MAX_CON; i++)
239     {
240         int error;
241         const char *errmsg, *addinfo, *dset;
242         /* display errors if any */
243         if (!c[i])
244             continue;
245         if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
246             printf("%s error: %s (%s:%d) %s\n",
247                    ZOOM_connection_option_get(c[i], "host"), errmsg,
248                    dset, error, addinfo);
249         else if (r[i])
250         {
251             /* OK, no major errors. Display records... */
252             int start = ZOOM_options_get_int(options, "start", 0);
253             int count = ZOOM_options_get_int(options, "count", 0);
254             display_records(c[i], r[i], start, count);
255         }
256     }
257     ZOOM_options_set(options, "count", "0");
258     ZOOM_options_set(options, "start", "0");
259 }
260
261 static void cmd_ext(ZOOM_connection *c, ZOOM_resultset *r,
262                     ZOOM_options options,
263                     const char **args)
264 {
265     ZOOM_package p[MAX_CON];
266     char ext_type_str[10];
267     
268     int i;
269
270     if (next_token_copy(args, ext_type_str, sizeof(ext_type_str)) < 0)
271         return;
272     
273     for (i = 0; i<MAX_CON; i++)
274     {
275         if (c[i])
276         {
277             p[i] = ZOOM_connection_package(c[i], 0);
278             ZOOM_package_send(p[i], ext_type_str);
279         }
280         else
281             p[i] = 0;
282     }
283
284     while (ZOOM_event(MAX_CON, c))
285         ;
286
287     for (i = 0; i<MAX_CON; i++)
288     {
289         int error;
290         const char *errmsg, *addinfo, *dset;
291         /* display errors if any */
292         if (!p[i])
293             continue;
294         if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
295             printf("%s error: %s (%s:%d) %s\n",
296                    ZOOM_connection_option_get(c[i], "host"), errmsg,
297                    dset, error, addinfo);
298         else if (p[i])
299         {
300             const char *v;
301             printf("ok\n");
302             v = ZOOM_package_option_get(p[i], "targetReference");
303             if (v)
304                 printf("targetReference: %s\n", v);
305             v = ZOOM_package_option_get(p[i], "xmlUpdateDoc");
306             if (v)
307                 printf("xmlUpdateDoc: %s\n", v);
308         }
309         ZOOM_package_destroy(p[i]);
310     }
311 }
312
313 static void cmd_debug(ZOOM_connection *c, ZOOM_resultset *r,
314                       ZOOM_options options,
315                       const char **args)
316 {
317     yaz_log_init_level(YLOG_ALL);
318 }
319
320 static void cmd_search(ZOOM_connection *c, ZOOM_resultset *r,
321                        ZOOM_options options,
322                        const char **args)
323 {
324     ZOOM_query s;
325     const char *query_str = *args;
326     int i;
327     
328     s = ZOOM_query_create();
329     while (*query_str == ' ')
330         query_str++;
331     if (memcmp(query_str, "cql:", 4) == 0)
332     {
333         ZOOM_query_cql(s, query_str + 4);
334     }
335     else if (ZOOM_query_prefix(s, query_str))
336     {
337         printf("Bad PQF: %s\n", query_str);
338         return;
339     }
340     for (i = 0; i<MAX_CON; i++)
341     {
342
343         if (c[i])
344         {
345             ZOOM_resultset_destroy(r[i]);
346             r[i] = 0;
347         }
348         if (c[i])
349             r[i] = ZOOM_connection_search(c[i], s);
350     }
351     ZOOM_query_destroy(s);
352
353     while (ZOOM_event(MAX_CON, c))
354         ;
355
356     for (i = 0; i<MAX_CON; i++)
357     {
358         int error;
359         const char *errmsg, *addinfo, *dset;
360         /* display errors if any */
361         if (!c[i])
362             continue;
363         if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
364             printf("%s error: %s (%s:%d) %s\n",
365                    ZOOM_connection_option_get(c[i], "host"), errmsg,
366                    dset, error, addinfo);
367         else if (r[i])
368         {
369             /* OK, no major errors. Look at the result count */
370             int start = ZOOM_options_get_int(options, "start", 0);
371             int count = ZOOM_options_get_int(options, "count", 0);
372
373             printf("%s: %ld hits\n", ZOOM_connection_option_get(c[i], "host"),
374                    (long) ZOOM_resultset_size(r[i]));
375             /* and display */
376             display_records(c[i], r[i], start, count);
377         }
378     }
379 }
380
381 static void cmd_scan(ZOOM_connection *c, ZOOM_resultset *r,
382                      ZOOM_options options,
383                      const char **args)
384 {
385     const char *query_str = *args;
386     ZOOM_query query = ZOOM_query_create();
387     int i;
388     ZOOM_scanset s[MAX_CON];
389     
390     while (*query_str == ' ')
391         query_str++;
392
393     if (memcmp(query_str, "cql:", 4) == 0)
394     {
395         ZOOM_query_cql(query, query_str + 4);
396     }
397     else if (ZOOM_query_prefix(query, query_str))
398     {
399         printf("Bad PQF: %s\n", query_str);
400         return;
401     }
402
403     for (i = 0; i<MAX_CON; i++)
404     {
405         if (c[i])
406             s[i] = ZOOM_connection_scan1(c[i], query);
407         else
408             s[i] = 0;
409     }
410     ZOOM_query_destroy(query);
411
412     while (ZOOM_event(MAX_CON, c))
413         ;
414     for (i = 0; i<MAX_CON; i++)
415     {
416         int error;
417         const char *errmsg, *addinfo, *dset;
418         /* display errors if any */
419         if (!c[i])
420             continue;
421         if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
422             printf("%s error: %s (%s:%d) %s\n",
423                    ZOOM_connection_option_get(c[i], "host"), errmsg,
424                    dset, error, addinfo);
425
426         if (s[i]) {
427             size_t p, sz = ZOOM_scanset_size(s[i]);
428             for (p = 0; p < sz; p++)
429             {
430                 int occ = 0;
431                 int len = 0;
432                 const char *term = ZOOM_scanset_display_term(s[i], p,
433                                                              &occ, &len);
434                 fwrite(term, 1, len, stdout);
435                 printf(" %d\n", occ);
436             }            
437             ZOOM_scanset_destroy(s[i]);
438         }
439     }
440 }
441
442 static void cmd_sort(ZOOM_connection *c, ZOOM_resultset *r,
443                      ZOOM_options options,
444                      const char **args)
445 {
446     const char *sort_spec = *args;
447     int i;
448     
449     while (*sort_spec == ' ')
450         sort_spec++;
451     
452     for (i = 0; i<MAX_CON; i++)
453     {
454         if (r[i])
455             ZOOM_resultset_sort(r[i], "yaz", sort_spec);
456     }
457     while (ZOOM_event(MAX_CON, c))
458         ;
459 }
460
461 static void cmd_help(ZOOM_connection *c, ZOOM_resultset *r,
462                      ZOOM_options options,
463                      const char **args)
464 {
465     printf("connect <zurl>\n");
466     printf("search <pqf>\n");
467     printf("show [<start> [<count>]\n");
468     printf("scan <term>\n");
469     printf("quit\n");
470     printf("close <zurl>\n");
471     printf("ext <type>\n");
472     printf("set <option> [<value>]\n");
473     printf("get <option>\n");
474     printf("\n");
475     printf("options:\n");
476     printf(" start\n");
477     printf(" count\n");
478     printf(" databaseName\n");
479     printf(" preferredRecordSyntax\n");
480     printf(" proxy\n");
481     printf(" elementSetName\n");
482     printf(" maximumRecordSize\n");
483     printf(" preferredRecordSize\n");
484     printf(" async\n");
485     printf(" piggyback\n");
486     printf(" group\n");
487     printf(" user\n");
488     printf(" password\n");
489     printf(" implementationName\n");
490     printf(" charset\n");
491     printf(" lang\n");
492 }
493
494 static void cmd_connect(ZOOM_connection *c, ZOOM_resultset *r,
495                         ZOOM_options options,
496                         const char **args)
497 {
498     int error;
499     const char *errmsg, *addinfo, *dset;
500     char host[60];
501     int j, i;
502     if (next_token_copy(args, host, sizeof(host)) < 0)
503     {
504         printf("missing host after connect\n");
505         return ;
506     }
507     for (j = -1, i = 0; i<MAX_CON; i++)
508     {
509         const char *h;
510         if (c[i] && (h = ZOOM_connection_option_get(c[i], "host")) &&
511             !strcmp(h, host))
512         {
513             ZOOM_connection_destroy(c[i]);
514             break;
515         }
516         else if (c[i] == 0 && j == -1)
517             j = i;
518     }
519     if (i == MAX_CON)  /* no match .. */
520     {
521         if (j == -1)
522         {
523             printf("no more connection available\n");
524             return;
525         }
526         i = j;   /* OK, use this one is available */
527     }
528     c[i] = ZOOM_connection_create(options);
529     ZOOM_connection_connect(c[i], host, 0);
530         
531     if ((error = ZOOM_connection_error_x(c[i], &errmsg, &addinfo, &dset)))
532         printf("%s error: %s (%s:%d) %s\n",
533                ZOOM_connection_option_get(c[i], "host"), errmsg,
534                dset, error, addinfo);
535 }
536
537 static int cmd_parse(ZOOM_connection *c, ZOOM_resultset *r,
538                      ZOOM_options options, 
539                      const char **buf)
540 {
541     int cmd_len;
542     const char *cmd_str;
543
544     cmd_len = next_token(buf, &cmd_str);
545     if (cmd_len < 0)
546         return 1;
547     if (is_command("quit", cmd_str, cmd_len))
548         return 0;
549     else if (is_command("set", cmd_str, cmd_len))
550         cmd_set(c, r, options, buf);
551     else if (is_command("get", cmd_str, cmd_len))
552         cmd_get(c, r, options, buf);
553     else if (is_command("rget", cmd_str, cmd_len))
554         cmd_rget(c, r, options, buf);
555     else if (is_command("connect", cmd_str, cmd_len))
556         cmd_connect(c, r, options, buf);
557     else if (is_command("open", cmd_str, cmd_len))
558         cmd_connect(c, r, options, buf);
559     else if (is_command("search", cmd_str, cmd_len))
560         cmd_search(c, r, options, buf);
561     else if (is_command("find", cmd_str, cmd_len))
562         cmd_search(c, r, options, buf);
563     else if (is_command("show", cmd_str, cmd_len))
564         cmd_show(c, r, options, buf);
565     else if (is_command("close", cmd_str, cmd_len))
566         cmd_close(c, r, options, buf);
567     else if (is_command("help", cmd_str, cmd_len))
568         cmd_help(c, r, options, buf);
569     else if (is_command("ext", cmd_str, cmd_len))
570         cmd_ext(c, r, options, buf);
571     else if (is_command("debug", cmd_str, cmd_len))
572         cmd_debug(c, r, options, buf);
573     else if (is_command("scan", cmd_str, cmd_len))
574         cmd_scan(c, r, options, buf);
575     else if (is_command("sort", cmd_str, cmd_len))
576         cmd_sort(c, r, options, buf);
577     else
578         printf("unknown command %.*s\n", cmd_len, cmd_str);
579     return 2;
580 }
581
582 void shell(ZOOM_connection *c, ZOOM_resultset *r,
583            ZOOM_options options)
584 {
585     while (1)
586     {
587         char buf[1000];
588         char *cp;
589         const char *bp = buf;
590 #if HAVE_READLINE_READLINE_H
591         char* line_in;
592         line_in=readline("ZOOM>");
593         if (!line_in)
594             break;
595 #if HAVE_READLINE_HISTORY_H
596         if (*line_in)
597             add_history(line_in);
598 #endif
599         if(strlen(line_in) > 999) {
600             printf("Input line too long\n");
601             break;
602         };
603         strcpy(buf,line_in);
604         free(line_in);
605 #else    
606         printf("ZOOM>"); fflush(stdout);
607         if (!fgets(buf, 999, stdin))
608             break;
609 #endif 
610         if ((cp = strchr(buf, '\n')))
611             *cp = '\0';
612         if (!cmd_parse(c, r, options, &bp))
613             break;
614     }
615 }
616
617 static void zoomsh(int argc, char **argv)
618 {
619     ZOOM_options options = ZOOM_options_create();
620     int i, res;
621     ZOOM_connection z39_con[MAX_CON];
622     ZOOM_resultset  z39_res[MAX_CON];
623
624     for (i = 0; i<MAX_CON; i++)
625     {
626         z39_con[i] = 0;
627         z39_res[i] = 0;
628     }
629
630     for (i = 0; i<MAX_CON; i++)
631         z39_con[i] = 0;
632
633     res = 1;
634     for (i = 1; i<argc; i++)
635     {
636         const char *bp = argv[i];
637         res = cmd_parse(z39_con, z39_res, options, &bp);
638         if (res == 0)  /* received quit */
639             break;
640     }
641     if (res)  /* do cmdline shell only if not quitting */
642         shell(z39_con, z39_res, options);
643     ZOOM_options_destroy(options);
644
645     for (i = 0; i<MAX_CON; i++)
646     {
647         ZOOM_connection_destroy(z39_con[i]);
648         ZOOM_resultset_destroy(z39_res[i]);
649     }
650 }
651
652 int main(int argc, char **argv)
653 {
654     const char *maskstr = 0;
655     if (argc > 2 && !strcmp(argv[1], "-v"))
656     {
657         maskstr = argv[2];
658         argv += 2;
659         argc -= 2;
660     }
661     else if (argc > 1 && !strncmp(argv[1], "-v", 2))
662     {
663         maskstr = argv[1]+2;
664         argv++;
665         argc--;
666     }
667     if (maskstr)
668     {
669         int mask = yaz_log_mask_str(maskstr);
670         yaz_log_init_level(mask);
671     }
672     zoomsh(argc, argv);
673     exit(0);
674 }
675 /*
676  * Local variables:
677  * c-basic-offset: 4
678  * indent-tabs-mode: nil
679  * End:
680  * vim: shiftwidth=4 tabstop=8 expandtab
681  */
682