ia64/xen-unstable

view tools/xenstore/xenstore_client.c @ 6768:282d5698ea40

Add xenstore-list and xenstore-exists clients
Signed-off-by: Christian Limpach <Christian.Limpach@cl.cam.ac.uk>
author cl349@firebug.cl.cam.ac.uk
date Mon Sep 12 20:46:37 2005 +0000 (2005-09-12)
parents b6c98fe62e1a
children 4d899a738d59 8ca0f98ba8e2
line source
1 /*
2 * This file is subject to the terms and conditions of the GNU General
3 * Public License. See the file "COPYING" in the main directory of
4 * this archive for more details.
5 *
6 * Copyright (C) 2005 by Christian Limpach
7 *
8 */
10 #include <err.h>
11 #include <fcntl.h>
12 #include <getopt.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <xs.h>
18 static void
19 usage(const char *progname)
20 {
21 #if defined(CLIENT_read)
22 errx(1, "Usage: %s [-h] [-p] key [...]", progname);
23 #elif defined(CLIENT_write)
24 errx(1, "Usage: %s [-h] key value [...]", progname);
25 #elif defined(CLIENT_rm) || defined(CLIENT_exists) || defined(CLIENT_list)
26 errx(1, "Usage: %s [-h] key [...]", progname);
27 #endif
28 }
30 int
31 main(int argc, char **argv)
32 {
33 struct xs_handle *xsh;
34 bool success;
35 int ret = 0;
36 #if defined(CLIENT_read) || defined(CLIENT_list)
37 int prefix = 0;
38 #endif
40 xsh = xs_domain_open();
41 if (xsh == NULL)
42 err(1, "xs_domain_open");
44 while (1) {
45 int c, index = 0;
46 static struct option long_options[] = {
47 {"help", 0, 0, 'h'},
48 #if defined(CLIENT_read) || defined(CLIENT_list)
49 {"prefix", 0, 0, 'p'},
50 #endif
51 {0, 0, 0, 0}
52 };
54 c = getopt_long(argc, argv, "h"
55 #if defined(CLIENT_read) || defined(CLIENT_list)
56 "p"
57 #endif
58 , long_options, &index);
59 if (c == -1)
60 break;
62 switch (c) {
63 case 'h':
64 usage(argv[0]);
65 /* NOTREACHED */
66 #if defined(CLIENT_read) || defined(CLIENT_list)
67 case 'p':
68 prefix = 1;
69 break;
70 #endif
71 }
72 }
74 if (optind == argc) {
75 usage(argv[0]);
76 /* NOTREACHED */
77 }
78 #if defined(CLIENT_write)
79 if ((argc - optind) % 1) {
80 usage(argv[0]);
81 /* NOTREACHED */
82 }
83 #endif
85 /* XXX maybe find longest common prefix */
86 success = xs_transaction_start(xsh, "/");
87 if (!success)
88 errx(1, "couldn't start transaction");
90 while (optind < argc) {
91 #if defined(CLIENT_read)
92 char *val = xs_read(xsh, argv[optind], NULL);
93 if (val == NULL) {
94 warnx("couldn't read path %s", argv[optind]);
95 ret = 1;
96 goto out;
97 }
98 if (prefix)
99 printf("%s: ", argv[optind]);
100 printf("%s\n", val);
101 free(val);
102 optind++;
103 #elif defined(CLIENT_write)
104 success = xs_write(xsh, argv[optind], argv[optind + 1],
105 strlen(argv[optind + 1]), O_CREAT);
106 if (!success) {
107 warnx("could not write path %s", argv[optind]);
108 ret = 1;
109 goto out;
110 }
111 optind += 2;
112 #elif defined(CLIENT_rm)
113 success = xs_rm(xsh, argv[optind]);
114 if (!success) {
115 warnx("could not remove path %s", argv[optind]);
116 ret = 1;
117 goto out;
118 }
119 optind++;
120 #elif defined(CLIENT_exists)
121 char *val = xs_read(xsh, argv[optind], NULL);
122 if (val == NULL) {
123 ret = 1;
124 goto out;
125 }
126 free(val);
127 optind++;
128 #elif defined(CLIENT_list)
129 unsigned int i, num;
130 char **list = xs_directory(xsh, argv[optind], &num);
131 if (list == NULL) {
132 warnx("could not list path %s", argv[optind]);
133 ret = 1;
134 goto out;
135 }
136 for (i = 0; i < num; i++) {
137 if (prefix)
138 printf("%s/", argv[optind]);
139 printf("%s\n", list[i]);
140 }
141 free(list);
142 optind++;
143 #endif
144 }
146 out:
147 success = xs_transaction_end(xsh, ret ? true : false);
148 if (!success)
149 errx(1, "couldn't end transaction");
151 return ret;
152 }