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