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