9c23715e7f13ca63e2afe10d1d0eec49f866f337
[yaz-moved-to-github.git] / util / cclsh.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2009 Index Data
3  * See the file LICENSE for details.
4  */
5
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9
10 #include <yaz/ccl_xml.h>
11 #include <yaz/options.h>
12
13 #if HAVE_READLINE_READLINE_H
14 #include <readline/readline.h> 
15 #endif
16 #if HAVE_READLINE_HISTORY_H
17 #include <readline/history.h>
18 #endif
19
20
21 static int debug = 0;
22 static char *prog;
23
24 void usage(const char *prog)
25 {
26     fprintf(stderr, "%s: [-d] [-b configfile] [-x xmlconfig]\n", prog);
27     exit(1);
28 }
29
30 int main(int argc, char **argv)
31 {
32     CCL_bibset bibset;
33     FILE *bib_inf;
34     char *bib_fname;
35     int ret;
36     char *arg;
37 #if YAZ_HAVE_XML2
38     xmlDocPtr doc;
39     const char *addinfo;
40 #endif
41     WRBUF q_wrbuf = 0;
42
43     prog = *argv;
44     bibset = ccl_qual_mk();    
45     
46     while ((ret = options("db:x:", argv, argc, &arg)) != -2)
47     {
48         switch(ret)
49         {
50         case 'd':
51             debug = 1;
52             break;
53         case 'b':
54             bib_fname = arg;
55             bib_inf = fopen(bib_fname, "r");
56             if (!bib_inf)
57             {
58                 fprintf(stderr, "%s: cannot open %s\n", prog,
59                          bib_fname);
60                 exit(1);
61             }
62             ccl_qual_file(bibset, bib_inf);
63             fclose(bib_inf);
64             break;
65 #if YAZ_HAVE_XML2
66         case 'x':
67             doc = xmlParseFile(arg);
68             if (!doc)
69             {
70                 fprintf(stderr, "%s: could not read %s\n", prog, arg);
71                 exit(1);
72             }
73             if (ccl_xml_config(bibset, xmlDocGetRootElement(doc), &addinfo))
74             {
75                 fprintf(stderr, "%s: error in %s: %s\n", prog, arg, addinfo);
76                 exit(1);
77             }
78             xmlFreeDoc(doc);
79             break;
80 #endif
81         case 0:
82             if (q_wrbuf)
83                 wrbuf_puts(q_wrbuf, " ");
84             else
85                 q_wrbuf = wrbuf_alloc();
86             wrbuf_puts(q_wrbuf, arg);
87             break;
88         default:
89             usage(prog);
90         }
91     }
92     if (q_wrbuf)
93     {
94         CCL_parser cclp = ccl_parser_create(bibset);
95         int error;
96         struct ccl_rpn_node *rpn;
97         
98         rpn = ccl_parser_find_str(cclp, wrbuf_cstr(q_wrbuf));
99         
100         error = ccl_parser_get_error(cclp, 0);
101         
102         if (error)
103         {
104             printf("%s\n", ccl_err_msg(error));
105         }
106         else
107         {
108             if (rpn)
109             {
110                 ccl_pr_tree(rpn, stdout);
111                 printf("\n");
112             }
113         }
114         ccl_parser_destroy(cclp);
115         if (rpn)
116             ccl_rpn_delete(rpn);
117         wrbuf_destroy(q_wrbuf);
118         exit(0);
119     }
120     while (1)
121     {
122         char buf[1000];
123         int i, error;
124         struct ccl_rpn_node *rpn;
125
126 #if HAVE_READLINE_READLINE_H
127             char* line_in;
128             line_in=readline("CCLSH>");
129             if (!line_in)
130                 break;
131 #if HAVE_READLINE_HISTORY_H
132             if (*line_in)
133                 add_history(line_in);
134 #endif
135             if (strlen(line_in) > 999) {
136                 fprintf(stderr,"Input line to long\n");
137                 break;
138             }
139             strcpy(buf,line_in);
140             free(line_in);
141 #else    
142         printf("CCLSH>"); fflush(stdout);
143         if (!fgets(buf, 999, stdin))
144             break;
145 #endif 
146
147         for (i = 0; i<1; i++)
148         {
149             CCL_parser cclp = ccl_parser_create(bibset);
150             int pos;
151             
152             rpn = ccl_parser_find_str(cclp, buf);
153             
154             error = ccl_parser_get_error(cclp, &pos);
155
156             if (error)
157             {
158                 printf("%*s^ - ", 6+pos, " ");
159                 printf("%s\n", ccl_err_msg(error));
160             }
161             else
162             {
163                 if (rpn && i == 0)
164                 {
165                     ccl_stop_words_t csw = ccl_stop_words_create();
166                     int idx = 0;
167                     printf("First:\n");
168                     ccl_pr_tree(rpn, stdout);
169                     if (ccl_stop_words_tree(csw, bibset, &rpn))
170                     {
171                         printf("Second:\n");
172                         ccl_pr_tree(rpn, stdout);
173                         printf("\n");
174                         
175                         for (idx = 0; ; idx++)
176                         {
177                             const char *qname;
178                             const char *term;
179                             if (!ccl_stop_words_info(csw, idx,
180                                                      &qname, &term))
181                                 break;
182                             printf("Removed from %s: %s\n", 
183                                    qname ? qname : "none", term);
184                         }
185                     }
186                     ccl_stop_words_destroy(csw);
187                 }
188             }
189             ccl_parser_destroy(cclp);
190             if (rpn)
191                 ccl_rpn_delete(rpn);
192         }
193     }
194     printf("\n");
195     ccl_qual_rm(&bibset);
196     return 0;
197 }
198 /*
199  * Local variables:
200  * c-basic-offset: 4
201  * indent-tabs-mode: nil
202  * End:
203  * vim: shiftwidth=4 tabstop=8 expandtab
204  */
205