13c62b0b5d67bc241e7a86bd08878ea53c1ddd74
[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 = 1;
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         if (!query)
77             usage();
78         else
79         {
80             ODR odr = odr_createmem(ODR_ENCODE);
81             YAZ_PQF_Parser pp = yaz_pqf_create();
82             Z_RPNQuery *rpn = yaz_pqf_parse(pp, odr, query);
83             if (!rpn)
84             {
85                 fprintf(stderr, "PQF syntax error\n");
86             }
87             else
88             {
89                 int ret = cql_transform_rpn2cql_stream(ct, cql_fputs,
90                                                        stdout, rpn);
91
92                 if (ret)
93                 {
94                     const char *addinfo;
95                     int r = cql_transform_error(ct, &addinfo);
96                     printf("Transform error %d %s\n", r, addinfo ? addinfo : "");
97                 }
98                 else
99                     printf("\n");
100             }
101             yaz_pqf_destroy(pp);
102             odr_destroy(odr);
103         }
104     }
105     else
106     {
107         CQL_parser cp = cql_parser_create();
108         int r = 0;
109
110         cql_parser_strict(cp, do_strict);
111         if (query)
112         {
113             if (verbose)
114                 printf("Parsing CQL %s\n", query);
115             for (i = 0; i<iterations; i++)
116                 r = cql_parser_string(cp, query);
117         }
118         else
119             r = cql_parser_stdio(cp, stdin);
120
121         if (r)
122             fprintf(stderr, "Syntax error\n");
123         else
124         {
125             r = cql_transform_FILE(ct, cql_parser_result(cp), stdout);
126             printf("\n");
127             if (r)
128             {
129                 const char *addinfo;
130                 r = cql_transform_error(ct, &addinfo);
131                 printf("Transform error %d %s\n", r, addinfo ? addinfo : "");
132             }
133             else
134             {
135                 FILE *null = fopen("/dev/null", "w");
136                 for (i = 1; i<iterations; i++)
137                     cql_transform_FILE(ct, cql_parser_result(cp), null);
138                 fclose(null);
139             }
140         }
141         cql_parser_destroy(cp);
142     }
143     cql_transform_close(ct);
144     return 0;
145 }
146 /*
147  * Local variables:
148  * c-basic-offset: 4
149  * c-file-style: "Stroustrup"
150  * indent-tabs-mode: nil
151  * End:
152  * vim: shiftwidth=4 tabstop=8 expandtab
153  */
154