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