Merge in build trunk.
[awesomized/libmemcached] / clients / memcat.cc
1 /* LibMemcached
2 * Copyright (C) 2006-2009 Brian Aker
3 * All rights reserved.
4 *
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
7 *
8 * Summary:
9 *
10 */
11
12 #include <config.h>
13
14 #include <cstdio>
15 #include <cstring>
16 #include <getopt.h>
17 #include <iostream>
18 #include <unistd.h>
19 #include <libmemcached/memcached.h>
20
21 #include "utilities.h"
22
23 #define PROGRAM_NAME "memcat"
24 #define PROGRAM_DESCRIPTION "Cat a set of key values to stdout."
25
26
27 /* Prototypes */
28 void options_parse(int argc, char *argv[]);
29
30 static int opt_binary= 0;
31 static int opt_verbose= 0;
32 static int opt_displayflag= 0;
33 static char *opt_servers= NULL;
34 static char *opt_hash= NULL;
35 static char *opt_username;
36 static char *opt_passwd;
37 static char *opt_file;
38
39 int main(int argc, char *argv[])
40 {
41 memcached_st *memc;
42 char *string;
43 size_t string_length;
44 uint32_t flags;
45 memcached_return_t rc;
46 memcached_server_st *servers;
47
48 int return_code= EXIT_SUCCESS;
49
50 options_parse(argc, argv);
51 initialize_sockets();
52
53 if (!opt_servers)
54 {
55 char *temp;
56
57 if ((temp= getenv("MEMCACHED_SERVERS")))
58 {
59 opt_servers= strdup(temp);
60 }
61 else
62 {
63 std::cerr << "No servers provied" << std::endl;
64 exit(EXIT_FAILURE);
65 }
66 }
67
68 memc= memcached_create(NULL);
69 process_hash_option(memc, opt_hash);
70
71 servers= memcached_servers_parse(opt_servers);
72
73 memcached_server_push(memc, servers);
74 memcached_server_list_free(servers);
75 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL,
76 (uint64_t)opt_binary);
77
78 if (opt_username and LIBMEMCACHED_WITH_SASL_SUPPORT == 0)
79 {
80 memcached_free(memc);
81 std::cerr << "--username was supplied, but binary was not built with SASL support." << std::endl;
82 return EXIT_FAILURE;
83 }
84
85 if (opt_username)
86 {
87 memcached_return_t ret;
88 if (memcached_failed(ret= memcached_set_sasl_auth_data(memc, opt_username, opt_passwd)))
89 {
90 std::cerr << memcached_last_error_message(memc) << std::endl;
91 memcached_free(memc);
92 return EXIT_FAILURE;
93 }
94 }
95
96 while (optind < argc)
97 {
98 string= memcached_get(memc, argv[optind], strlen(argv[optind]),
99 &string_length, &flags, &rc);
100 if (rc == MEMCACHED_SUCCESS)
101 {
102 if (opt_displayflag)
103 {
104 if (opt_verbose)
105 {
106 std::cout << "key: " << argv[optind] << std::endl << "flags: " << flags << std::endl;
107 }
108 }
109 else
110 {
111 if (opt_verbose)
112 {
113 std::cout << "key: " << argv[optind] << std::endl << "flags: " << flags << "length: " << string_length << std::endl << "value: ";
114 }
115
116 if (opt_file)
117 {
118 FILE *fp= fopen(opt_file, "w");
119 if (fp == NULL)
120 {
121 perror("fopen");
122 return_code= EXIT_FAILURE;
123 break;
124 }
125
126 size_t written= fwrite(string, 1, string_length, fp);
127 if (written != string_length)
128 {
129 std::cerr << "error writing file to file " << opt_file << " wrote " << written << ", should have written" << string_length << std::endl;
130 return_code= EXIT_FAILURE;
131 break;
132 }
133
134 if (fclose(fp))
135 {
136 std::cerr << "error closing " << opt_file << std::endl;
137 return_code= EXIT_FAILURE;
138 break;
139 }
140 }
141 else
142 {
143 std::cout.write(string, string_length);
144 std::cout << std::endl;
145 }
146 free(string);
147 }
148 }
149 else if (rc != MEMCACHED_NOTFOUND)
150 {
151 std::cerr << "error on " << argv[optind] << "(" << memcached_strerror(memc, rc) << ")";
152 if (memcached_last_error_errno(memc))
153 {
154 std::cerr << " system error (" << strerror(memcached_last_error_errno(memc)) << ")" << std::endl;
155 }
156 std::cerr << std::endl;
157
158 return_code= EXIT_FAILURE;
159 break;
160 }
161 else // Unknown Issue
162 {
163 std::cerr << "error on " << argv[optind] << "("<< memcached_strerror(NULL, rc) << ")" << std::endl;
164 return_code= EXIT_FAILURE;
165 }
166 optind++;
167 }
168
169 memcached_free(memc);
170
171 if (opt_servers)
172 {
173 free(opt_servers);
174 }
175 if (opt_hash)
176 {
177 free(opt_hash);
178 }
179
180 return return_code;
181 }
182
183
184 void options_parse(int argc, char *argv[])
185 {
186 int option_index= 0;
187 int option_rv;
188
189 memcached_programs_help_st help_options[]=
190 {
191 {0},
192 };
193
194 static struct option long_options[]=
195 {
196 {(OPTIONSTRING)"version", no_argument, NULL, OPT_VERSION},
197 {(OPTIONSTRING)"help", no_argument, NULL, OPT_HELP},
198 {(OPTIONSTRING)"quiet", no_argument, NULL, OPT_QUIET},
199 {(OPTIONSTRING)"verbose", no_argument, &opt_verbose, OPT_VERBOSE},
200 {(OPTIONSTRING)"debug", no_argument, &opt_verbose, OPT_DEBUG},
201 {(OPTIONSTRING)"servers", required_argument, NULL, OPT_SERVERS},
202 {(OPTIONSTRING)"flag", no_argument, &opt_displayflag, OPT_FLAG},
203 {(OPTIONSTRING)"hash", required_argument, NULL, OPT_HASH},
204 {(OPTIONSTRING)"binary", no_argument, NULL, OPT_BINARY},
205 {(OPTIONSTRING)"username", required_argument, NULL, OPT_USERNAME},
206 {(OPTIONSTRING)"password", required_argument, NULL, OPT_PASSWD},
207 {(OPTIONSTRING)"file", required_argument, NULL, OPT_FILE},
208 {0, 0, 0, 0},
209 };
210
211 while (1)
212 {
213 option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
214 if (option_rv == -1) break;
215 switch (option_rv)
216 {
217 case 0:
218 break;
219 case OPT_BINARY:
220 opt_binary = 1;
221 break;
222 case OPT_VERBOSE: /* --verbose or -v */
223 opt_verbose = OPT_VERBOSE;
224 break;
225 case OPT_DEBUG: /* --debug or -d */
226 opt_verbose = OPT_DEBUG;
227 break;
228 case OPT_VERSION: /* --version or -V */
229 version_command(PROGRAM_NAME);
230 break;
231 case OPT_HELP: /* --help or -h */
232 help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options);
233 break;
234 case OPT_SERVERS: /* --servers or -s */
235 opt_servers= strdup(optarg);
236 break;
237 case OPT_HASH:
238 opt_hash= strdup(optarg);
239 break;
240 case OPT_USERNAME:
241 opt_username= optarg;
242 break;
243 case OPT_PASSWD:
244 opt_passwd= optarg;
245 break;
246 case OPT_FILE:
247 opt_file= optarg;
248 break;
249
250 case OPT_QUIET:
251 close_stdio();
252 break;
253
254 case '?':
255 /* getopt_long already printed an error message. */
256 exit(1);
257 default:
258 abort();
259 }
260 }
261 }