Removed Z3950_connection_host.
[yaz-moved-to-github.git] / zoom / zoomtst5.c
1 /*
2  * $Id: zoomtst5.c,v 1.5 2001-11-16 09:52:39 adam Exp $
3  *
4  * Asynchronous multi-target client doing search, sort and present
5  */
6
7 #include <stdio.h>
8 #include <string.h>
9
10 #include <yaz/nmem.h>
11 #include <yaz/xmalloc.h>
12 #include <yaz/zoom.h>
13
14 const char *my_callback (void *handle, const char *name)
15 {
16     if (!strcmp (name, "async"))
17         return "1";
18     return 0;
19 }
20
21 int main(int argc, char **argv)
22 {
23     int i;
24     int no = argc-3;
25     Z3950_connection z[500]; /* allow at most 500 connections */
26     Z3950_resultset r[500];  /* and result sets .. */
27     Z3950_query q;
28     Z3950_options o;
29
30     o = Z3950_options_create ();
31     if (argc < 4)
32     {
33         fprintf (stderr, "usage:\n%s target1 .. targetN query sort\n",
34                  *argv);
35         exit (2);
36     }
37     if (no > 500)
38         no = 500;
39
40     /* function my_callback called when reading options .. */
41     Z3950_options_set_callback (o, my_callback, 0);
42
43     /* get 20 (at most) records from beginning */
44     Z3950_options_set (o, "count", "20");
45
46     Z3950_options_set (o, "implementationName", "sortapp");
47     Z3950_options_set (o, "preferredRecordSyntax", "usmarc");
48     Z3950_options_set (o, "elementSetName", "B");
49
50     /* create query */
51     q = Z3950_query_create ();
52     if (Z3950_query_prefix (q, argv[argc-2]))
53     {
54         printf ("bad PQF: %s\n", argv[argc-2]);
55         exit (1);
56     }
57     if (Z3950_query_sortby (q, argv[argc-1]))
58     {
59         printf ("bad sort spec: %s\n", argv[argc-1]);
60         exit (1);
61     }
62     /* connect - and search all */
63     for (i = 0; i<no; i++)
64     {
65         z[i] = Z3950_connection_create (o);
66         Z3950_connection_connect (z[i], argv[i+1], 0);
67         r[i] = Z3950_connection_search (z[i], q);
68     }
69
70     /* network I/O */
71     while (Z3950_event (no, z))
72         ;
73
74     /* handle errors */
75     for (i = 0; i<no; i++)
76     {
77         int error;
78         const char *errmsg, *addinfo;
79         if ((error = Z3950_connection_error(z[i], &errmsg, &addinfo)))
80             fprintf (stderr, "%s error: %s (%d) %s\n",
81                      Z3950_connection_option_get(z[i], "host"),
82                      errmsg, error, addinfo);
83         else
84         {
85             /* OK, no major errors. Look at the result count */
86             int pos;
87             printf ("%s: %d hits\n", Z3950_connection_option_get(z[i], "host"),
88                     Z3950_resultset_size(r[i]));
89             /* go through all records at target */
90             for (pos = 0; pos < 20; pos++)
91             {
92                 Z3950_record rec;
93                 const char *db, *syntax, *str;
94                 int len;
95
96                 rec = Z3950_resultset_record (r[i], pos);
97                 /* get database for record and record itself at pos */
98
99                 db = Z3950_record_get (rec,  "database", 0);
100                 str = Z3950_record_get (rec, "render", &len);
101                 syntax = Z3950_record_get (rec, "syntax", &len);
102                 /* if rec is non-null, we got a record for display */
103                 if (str)
104                 {
105                     printf ("%d %s %s\n", pos+1, syntax, 
106                             (db ? db : "unknown"));
107                     if (rec)
108                         fwrite (str, 1, len, stdout);
109                     putchar ('\n');
110                 }
111             }
112         }
113     }
114
115     /* destroy stuff and exit */
116     Z3950_query_destroy (q);
117     for (i = 0; i<no; i++)
118     {
119         Z3950_resultset_destroy (r[i]);
120         Z3950_connection_destroy (z[i]);
121     }
122     Z3950_options_destroy(o);
123     exit(0);
124 }