cql2pqf: fix syntax error
[yaz-moved-to-github.git] / util / cql2pqf.c
index 3c5df41..203cdae 100644 (file)
@@ -1,37 +1,39 @@
-/* $Id: cql2pqf.c,v 1.7 2005-06-25 15:46:07 adam Exp $
-   Copyright (C) 1995-2005, Index Data ApS
-   Index Data Aps
-
-This file is part of the YAZ toolkit.
-
-See the file LICENSE.
-*/
+/* This file is part of the YAZ toolkit.
+ * Copyright (C) 1995-2013 Index Data
+ * See the file LICENSE for details.
+ */
+#if HAVE_CONFIG_H
+#include <config.h>
+#endif
 
 #include <stdlib.h>
 #include <stdio.h>
 
-#include <yaz/cql.h>
+#include <yaz/rpn2cql.h>
+#include <yaz/pquery.h>
 #include <yaz/options.h>
 
-static void usage()
+static void usage(void)
 {
-    fprintf (stderr, "usage\n cql2pqf [-n <n>] <properties> [<query>]\n");
-    exit (1);
+    fprintf(stderr, "usage\n cql2pqf [-n <n>] [-r] [-S] <properties> "
+            "[<query>]\n");
+    exit(1);
 }
 
 int main(int argc, char **argv)
 {
     cql_transform_t ct;
-    int r = 0;
     int i, iterations = 1;
-    CQL_parser cp = cql_parser_create();
     char *query = 0;
     char *fname = 0;
+    int reverse = 0;
+    int verbose = 0;
+    int do_strict = 0;
 
     int ret;
     char *arg;
 
-    while ((ret = options("n:", argv, argc, &arg)) != -2)
+    while ((ret = options("n:rSv", argv, argc, &arg)) != -2)
     {
         switch (ret)
         {
@@ -44,54 +46,112 @@ int main(int argc, char **argv)
         case 'n':
             iterations = atoi(arg);
             break;
+        case 'r':
+            reverse = 1;
+            break;
+        case 'S':
+            do_strict = 1;
+            break;
+        case 'v':
+            verbose = 1;
+            break;
         default:
             usage();
         }
     }
     if (!fname)
         usage();
-    ct = cql_transform_open_fname(fname);
+    if (!strcmp(fname, "-"))
+        ct = cql_transform_create();
+    else
+        ct = cql_transform_open_fname(fname);
     if (!ct)
     {
-        fprintf (stderr, "failed to read properties %s\n", fname);
-        exit (1);
+        fprintf(stderr, "failed to read properties %s\n", fname);
+        exit(1);
     }
 
-    if (query)
+    if (reverse)
     {
-        for (i = 0; i<iterations; i++)
-            r = cql_parser_string(cp, query);
-    }
-    else
-        r = cql_parser_stdio(cp, stdin);
+        char buf[1024];
+
+        if (!query)
+        {
+            if (fgets(buf, sizeof buf, stdin))
+                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 = cql_transform_rpn2cql_stream(ct, cql_fputs,
+                                                       stdout, rpn);
 
-    if (r)
-        fprintf (stderr, "Syntax error\n");
+                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
     {
-        r = cql_transform_FILE(ct, cql_parser_result(cp), stdout);
-        printf("\n");
-        if (r)
+        CQL_parser cp = cql_parser_create();
+        int r = 0;
+
+        cql_parser_strict(cp, do_strict);
+        if (query)
         {
-            const char *addinfo;
-            cql_transform_error(ct, &addinfo);
-            printf ("Transform error %d %s\n", r, addinfo ? addinfo : "");
+            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
         {
-            FILE *null = fopen("/dev/null", "w");
-            for (i = 1; i<iterations; i++)
-                cql_transform_FILE(ct, cql_parser_result(cp), null);
-            fclose(null);
+            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);
-    cql_parser_destroy(cp);
     return 0;
 }
 /*
  * Local variables:
  * c-basic-offset: 4
+ * c-file-style: "Stroustrup"
  * indent-tabs-mode: nil
  * End:
  * vim: shiftwidth=4 tabstop=8 expandtab