File: cql2pqf.c

package info (click to toggle)
yaz 5.27.1-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 16,184 kB
  • sloc: xml: 123,414; ansic: 72,530; sh: 5,007; tcl: 2,169; makefile: 1,321; yacc: 382
file content (179 lines) | stat: -rw-r--r-- 4,479 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/* This file is part of the YAZ toolkit.
 * Copyright (C) Index Data
 * See the file LICENSE for details.
 */
#if HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdlib.h>
#include <stdio.h>

#include <yaz/rpn2cql.h>
#include <yaz/rpn2solr.h>
#include <yaz/pquery.h>
#include <yaz/options.h>

static void usage(void)
{
    fprintf(stderr, "usage\n cql2pqf [-n <n>] [-r] [-s] [-S] <properties> "
            "[<query>]\n");
    fprintf(stderr, " -r  reverse conversion (RPN to Solr/CQL)\n");
    fprintf(stderr, " -s  Solr instead of CQL\n");
    fprintf(stderr, " -S  strict CQL 2.0\n");
    exit(1);
}

int main(int argc, char **argv)
{
    cql_transform_t ct;
    int i, iterations = 1;
    char *query = 0;
    char *fname = 0;
    int reverse = 0;
    int solr = 0;
    int verbose = 0;
    int do_strict = 0;

    int ret;
    char *arg;

    while ((ret = options("n:rsSv", argv, argc, &arg)) != -2)
    {
        switch (ret)
        {
        case 0:
            if (!fname)
                fname = arg;
            else
                query = arg;
            break;
        case 'n':
            iterations = atoi(arg);
            break;
        case 'r':
            reverse = 1;
            break;
        case 'S':
            do_strict = 1;
            break;
        case 's':
            solr = 1;
            break;
        case 'v':
            verbose = 1;
            break;
        default:
            usage();
        }
    }
    if (fname)
    {
        ct = cql_transform_open_fname(fname);
        if (!ct)
        {
            fprintf(stderr, "failed to read properties %s\n", fname);
            exit(1);
        }
    }
    else
        ct = cql_transform_create();

    if (reverse)
    {
        char buf[1024];

        if (!query)
        {
            if (fgets(buf, sizeof buf, stdin))
            {
                if (*buf && buf[strlen(buf)-1] == '\n')
                    buf[strlen(buf)-1] = '\0';
                query = buf;
            }
        }
        if (query)
        {
            ODR odr = odr_createmem(ODR_ENCODE);
            YAZ_PQF_Parser pp = yaz_pqf_create();
            Z_RPNQuery *rpn = yaz_pqf_parse(pp, odr, query);
            if (!rpn)
            {
                fprintf(stderr, "PQF syntax error\n");
            }
            else
            {
                int ret = 0;
                if (solr)
                    ret = solr_transform_rpn2solr_stream(ct, cql_fputs,
                                                         stdout, rpn);
                else
                    ret = cql_transform_rpn2cql_stream(ct, cql_fputs,
                                                       stdout, rpn);
                if (ret)
                {
                    const char *addinfo;
                    int r = cql_transform_error(ct, &addinfo);
                    printf("Transform error %d %s\n", r, addinfo ? addinfo : "");
                }
                else
                    printf("\n");
            }
            yaz_pqf_destroy(pp);
            odr_destroy(odr);
        }
    }
    else
    {
        CQL_parser cp = cql_parser_create();
        int r = 0;

        cql_parser_strict(cp, do_strict);
        if (query)
        {
            if (verbose)
                printf("Parsing CQL %s\n", query);
            for (i = 0; i<iterations; i++)
                r = cql_parser_string(cp, query);
        }
        else
            r = cql_parser_stdio(cp, stdin);

        if (r)
            fprintf(stderr, "Syntax error\n");
        else if (solr)
        {
            printf("CQL to Solr not supported (supported is reverse -r)\n");
        }
        else
        {
            r = cql_transform_FILE(ct, cql_parser_result(cp), stdout);
            printf("\n");
            if (r)
            {
                const char *addinfo;
                r = cql_transform_error(ct, &addinfo);
                printf("Transform error %d %s\n", r, addinfo ? addinfo : "");
            }
            else
            {
                FILE *null = fopen("/dev/null", "w");
                for (i = 1; i<iterations; i++)
                    cql_transform_FILE(ct, cql_parser_result(cp), null);
                fclose(null);
            }
        }
        cql_parser_destroy(cp);
    }
    cql_transform_close(ct);
    return 0;
}
/*
 * Local variables:
 * c-basic-offset: 4
 * c-file-style: "Stroustrup"
 * indent-tabs-mode: nil
 * End:
 * vim: shiftwidth=4 tabstop=8 expandtab
 */