zoomsh: remove unused code for cmd facets
[yaz-moved-to-github.git] / zoom / zoomtst5.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
6 #include <stdio.h>
7 #include <string.h>
8
9 #include <yaz/nmem.h>
10 #include <yaz/xmalloc.h>
11 #include <yaz/zoom.h>
12
13 const char *my_callback (void *handle, const char *name)
14 {
15     if (!strcmp (name, "async"))
16         return "1";
17     return 0;
18 }
19
20 int main(int argc, char **argv)
21 {
22     int i;
23     int no = argc-3;
24     ZOOM_connection z[500]; /* allow at most 500 connections */
25     ZOOM_resultset r[500];  /* and result sets .. */
26     ZOOM_query q;
27     ZOOM_options o;
28
29     o = ZOOM_options_create ();
30     if (argc < 4)
31     {
32         fprintf (stderr, "usage:\n%s target1 .. targetN query sort\n",
33                  *argv);
34         exit (2);
35     }
36     if (no > 500)
37         no = 500;
38
39     /* function my_callback called when reading options .. */
40     ZOOM_options_set_callback (o, my_callback, 0);
41
42     /* get 20 (at most) records from beginning */
43     ZOOM_options_set (o, "count", "20");
44
45     ZOOM_options_set (o, "implementationName", "sortapp");
46     ZOOM_options_set (o, "preferredRecordSyntax", "usmarc");
47     ZOOM_options_set (o, "elementSetName", "B");
48
49     /* create query */
50     q = ZOOM_query_create ();
51     if (ZOOM_query_prefix (q, argv[argc-2]))
52     {
53         printf ("bad PQF: %s\n", argv[argc-2]);
54         exit (1);
55     }
56     if (ZOOM_query_sortby (q, argv[argc-1]))
57     {
58         printf ("bad sort spec: %s\n", argv[argc-1]);
59         exit (1);
60     }
61     /* connect - and search all */
62     for (i = 0; i<no; i++)
63     {
64         z[i] = ZOOM_connection_create (o);
65         ZOOM_connection_connect (z[i], argv[i+1], 0);
66         r[i] = ZOOM_connection_search (z[i], q);
67     }
68
69     /* network I/O */
70     while (ZOOM_event (no, z))
71         ;
72
73     /* handle errors */
74     for (i = 0; i<no; i++)
75     {
76         int error;
77         const char *errmsg, *addinfo;
78         if ((error = ZOOM_connection_error(z[i], &errmsg, &addinfo)))
79             fprintf (stderr, "%s error: %s (%d) %s\n",
80                      ZOOM_connection_option_get(z[i], "host"),
81                      errmsg, error, addinfo);
82         else
83         {
84             /* OK, no major errors. Look at the result count */
85             int pos;
86             printf ("%s: %ld hits\n", ZOOM_connection_option_get(z[i], "host"),
87                     (long) ZOOM_resultset_size(r[i]));
88             /* go through first 20 records at target */
89             for (pos = 0; pos < 20; pos++)
90             {
91                 ZOOM_record rec;
92                 const char *db, *syntax, *str;
93                 int len;
94
95                 rec = ZOOM_resultset_record (r[i], pos);
96                 /* get database for record and record itself at pos */
97
98                 db = ZOOM_record_get (rec,  "database", 0);
99                 str = ZOOM_record_get (rec, "xml", &len);
100                 syntax = ZOOM_record_get (rec, "syntax", &len);
101                 /* if rec is non-null, we got a record for display */
102                 if (str)
103                 {
104                     printf ("%d %s %s\n", pos+1, syntax, 
105                             (db ? db : "unknown"));
106                     if (rec)
107                     {
108                         if (fwrite (str, 1, len, stdout) != (size_t) len)
109                             printf("write to stdout failed\n");
110                     }
111                     printf ("\n");
112                 }
113             }
114         }
115     }
116
117     /* destroy stuff and exit */
118     ZOOM_query_destroy (q);
119     for (i = 0; i<no; i++)
120     {
121         ZOOM_resultset_destroy (r[i]);
122         ZOOM_connection_destroy (z[i]);
123     }
124     ZOOM_options_destroy(o);
125     exit(0);
126 }
127 /*
128  * Local variables:
129  * c-basic-offset: 4
130  * c-file-style: "Stroustrup"
131  * indent-tabs-mode: nil
132  * End:
133  * vim: shiftwidth=4 tabstop=8 expandtab
134  */
135