1073abf60f074653420d186702d46b703d21284b
[yaz-moved-to-github.git] / util / cql2pqf.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2013 Index Data
3  * See the file LICENSE for details.
4  */
5 #if HAVE_CONFIG_H
6 #include <config.h>
7 #endif
8
9 #include <stdlib.h>
10 #include <stdio.h>
11
12 #include <yaz/rpn2cql.h>
13 #include <yaz/pquery.h>
14 #include <yaz/options.h>
15
16 static void usage(void)
17 {
18     fprintf(stderr, "usage\n cql2pqf [-n <n>] [-r] [-S] <properties> "
19             "[<query>]\n");
20     exit(1);
21 }
22
23 int main(int argc, char **argv)
24 {
25     cql_transform_t ct;
26     int i, iterations = 1;
27     char *query = 0;
28     char *fname = 0;
29     int reverse = 0;
30     int verbose = 0
31     int do_strict = 0;
32
33     int ret;
34     char *arg;
35
36     while ((ret = options("n:rSv", argv, argc, &arg)) != -2)
37     {
38         switch (ret)
39         {
40         case 0:
41             if (!fname)
42                 fname = arg;
43             else
44                 query = arg;
45             break;
46         case 'n':
47             iterations = atoi(arg);
48             break;
49         case 'r':
50             reverse = 1;
51             break;
52         case 'S':
53             do_strict = 1;
54             break;
55         case 'v':
56             verbose = 1;
57             break;
58         default:
59             usage();
60         }
61     }
62     if (!fname)
63         usage();
64     if (!strcmp(fname, "-"))
65         ct = cql_transform_create();
66     else
67         ct = cql_transform_open_fname(fname);
68     if (!ct)
69     {
70         fprintf(stderr, "failed to read properties %s\n", fname);
71         exit(1);
72     }
73
74     if (reverse)
75     {
76         char buf[1024];
77
78         if (!query)
79         {
80             if (fgets(buf, sizeof buf, stdin))
81                 query = buf;
82         }
83         if (query)
84         {
85             ODR odr = odr_createmem(ODR_ENCODE);
86             YAZ_PQF_Parser pp = yaz_pqf_create();
87             Z_RPNQuery *rpn = yaz_pqf_parse(pp, odr, query);
88             if (!rpn)
89             {
90                 fprintf(stderr, "PQF syntax error\n");
91             }
92             else
93             {
94                 int ret = cql_transform_rpn2cql_stream(ct, cql_fputs,
95                                                        stdout, rpn);
96
97                 if (ret)
98                 {
99                     const char *addinfo;
100                     int r = cql_transform_error(ct, &addinfo);
101                     printf("Transform error %d %s\n", r, addinfo ? addinfo : "");
102                 }
103                 else
104                     printf("\n");
105             }
106             yaz_pqf_destroy(pp);
107             odr_destroy(odr);
108         }
109     }
110     else
111     {
112         CQL_parser cp = cql_parser_create();
113         int r = 0;
114
115         cql_parser_strict(cp, do_strict);
116         if (query)
117         {
118             if (verbose)
119                 printf("Parsing CQL %s\n", query);
120             for (i = 0; i<iterations; i++)
121                 r = cql_parser_string(cp, query);
122         }
123         else
124             r = cql_parser_stdio(cp, stdin);
125
126         if (r)
127             fprintf(stderr, "Syntax error\n");
128         else
129         {
130             r = cql_transform_FILE(ct, cql_parser_result(cp), stdout);
131             printf("\n");
132             if (r)
133             {
134                 const char *addinfo;
135                 r = cql_transform_error(ct, &addinfo);
136                 printf("Transform error %d %s\n", r, addinfo ? addinfo : "");
137             }
138             else
139             {
140                 FILE *null = fopen("/dev/null", "w");
141                 for (i = 1; i<iterations; i++)
142                     cql_transform_FILE(ct, cql_parser_result(cp), null);
143                 fclose(null);
144             }
145         }
146         cql_parser_destroy(cp);
147     }
148     cql_transform_close(ct);
149     return 0;
150 }
151 /*
152  * Local variables:
153  * c-basic-offset: 4
154  * c-file-style: "Stroustrup"
155  * indent-tabs-mode: nil
156  * End:
157  * vim: shiftwidth=4 tabstop=8 expandtab
158  */
159