Update source headers for 2008. Omit CVS ID keyword subst.
[yaz-moved-to-github.git] / zoom / zoomtst9.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2008 Index Data
3  * See the file LICENSE for details.
4  */
5
6 #include <stdio.h>
7 #include <string.h>
8 #include <yaz/wrbuf.h>
9
10 #include <yaz/nmem.h>
11 #include <yaz/xmalloc.h>
12 #include <yaz/zoom.h>
13
14 static void usage(void)
15 {
16     fprintf(stderr, "usage:\n"
17             "zoomtst9 target [insert|delete|replace|update] id1 rec1 "
18             "id2 rec2 ..\n");
19
20     fprintf(stderr, "\nThis program illustrates the usage of"
21             " extended services Update from ZOOM.\n");
22     fprintf(stderr, "\nid "
23             "is optional opaque record Id and is omitted if empty.\n");
24     fprintf(stderr, "\nrec "
25             "is optional record data and is omitted if empty.\n");
26     exit (1);
27 }
28
29 int main(int argc, char **argv)
30 {
31     ZOOM_connection z;
32     ZOOM_options o = ZOOM_options_create ();
33     int error;
34     const char *errmsg, *addinfo;
35     
36     if (argc < 3)
37         usage();
38
39     z = ZOOM_connection_create (o);
40     
41     /* connect and init */
42     ZOOM_connection_connect (z, argv[1], 0);
43     
44     if ((error = ZOOM_connection_error(z, &errmsg, &addinfo)))
45     {
46         fprintf(stderr, "%s error: %s (%d) %s\n",
47                 ZOOM_connection_option_get(z, "host"),
48                 errmsg, error, addinfo);
49     }
50     else
51     {
52         ZOOM_package pkg = ZOOM_connection_package(z, 0);
53         const char *cmd = argv[2];
54         int i;
55
56         if (!strcmp(cmd, "insert"))
57             ZOOM_package_option_set(pkg, "action", "recordInsert");
58         else if (!strcmp(cmd, "update"))
59             ZOOM_package_option_set(pkg, "action", "specialUpdate");
60         else if (!strcmp(cmd, "replace"))
61             ZOOM_package_option_set(pkg, "action", "recordReplace");
62         else if (!strcmp(cmd, "delete"))
63             ZOOM_package_option_set(pkg, "action", "recordDelete");
64         else
65         {
66             fprintf(stderr, "Bad action %s\n", cmd);
67             usage();
68         }
69
70         i = 3;
71         while (i < argc-1)
72         {
73             ZOOM_package_option_set(pkg, "recordIdOpaque",
74                                     argv[i][0] ? argv[i] : 0);
75             i++;
76             if (!strcmp(argv[i], "-"))
77             {
78                 /* For -, read record buffer from stdin */
79                 WRBUF w = wrbuf_alloc();
80                 int ch;
81                 while ((ch = getchar()) != EOF)
82                     wrbuf_putc(w, ch);
83                 wrbuf_putc(w, '\0');
84                 ZOOM_package_option_set(pkg, "record", wrbuf_buf(w));
85             }
86             else
87             {
88                 ZOOM_package_option_set(pkg, "record",
89                                         argv[i][0] ? argv[i] : 0);
90             }
91             i++;
92             ZOOM_package_send(pkg, "update"); /* Update EXT service */
93
94             if ((error = ZOOM_connection_error(z, &errmsg, &addinfo)))
95             {
96                 fprintf(stderr, "%s error: %s (%d) %s\n",
97                         ZOOM_connection_option_get(z, "host"),
98                         errmsg, error, addinfo);
99             }
100         }
101     }
102     ZOOM_connection_destroy (z);
103     ZOOM_options_destroy(o);
104     exit (0);
105 }
106 /*
107  * Local variables:
108  * c-basic-offset: 4
109  * indent-tabs-mode: nil
110  * End:
111  * vim: shiftwidth=4 tabstop=8 expandtab
112  */
113