Merge lp:~tangent-org/libmemcached/1.0-build/ Build: jenkins-Libmemcached-187
[m6w6/libmemcached] / clients / memcat.cc
1 /* LibMemcached
2 * Copyright (C) 2011-2012 Data Differential, http://datadifferential.com/
3 * Copyright (C) 2006-2009 Brian Aker
4 * All rights reserved.
5 *
6 * Use and distribution licensed under the BSD license. See
7 * the COPYING file in the parent directory for full text.
8 *
9 * Summary:
10 *
11 */
12
13 #include <mem_config.h>
14
15 #include <cstdio>
16 #include <cstring>
17 #include <getopt.h>
18 #include <iostream>
19 #include <unistd.h>
20 #include <libmemcached-1.0/memcached.h>
21
22 #include "utilities.h"
23
24 #define PROGRAM_NAME "memcat"
25 #define PROGRAM_DESCRIPTION "Cat a set of key values to stdout."
26
27
28 /* Prototypes */
29 void options_parse(int argc, char *argv[]);
30
31 static int opt_binary= 0;
32 static int opt_verbose= 0;
33 static int opt_displayflag= 0;
34 static char *opt_servers= NULL;
35 static char *opt_hash= NULL;
36 static char *opt_username;
37 static char *opt_passwd;
38 static char *opt_file;
39
40 int main(int argc, char *argv[])
41 {
42 memcached_st *memc;
43 char *string;
44 size_t string_length;
45 uint32_t flags;
46 memcached_return_t rc;
47 memcached_server_st *servers;
48
49 int return_code= EXIT_SUCCESS;
50
51 options_parse(argc, argv);
52 initialize_sockets();
53
54 if (!opt_servers)
55 {
56 char *temp;
57
58 if ((temp= getenv("MEMCACHED_SERVERS")))
59 {
60 opt_servers= strdup(temp);
61 }
62 else
63 {
64 std::cerr << "No servers provied" << std::endl;
65 exit(EXIT_FAILURE);
66 }
67 }
68
69 memc= memcached_create(NULL);
70 process_hash_option(memc, opt_hash);
71
72 servers= memcached_servers_parse(opt_servers);
73
74 memcached_server_push(memc, servers);
75 memcached_server_list_free(servers);
76 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL,
77 (uint64_t)opt_binary);
78
79 if (opt_username and LIBMEMCACHED_WITH_SASL_SUPPORT == 0)
80 {
81 memcached_free(memc);
82 std::cerr << "--username was supplied, but binary was not built with SASL support." << std::endl;
83 return EXIT_FAILURE;
84 }
85
86 if (opt_username)
87 {
88 memcached_return_t ret;
89 if (memcached_failed(ret= memcached_set_sasl_auth_data(memc, opt_username, opt_passwd)))
90 {
91 std::cerr << memcached_last_error_message(memc) << std::endl;
92 memcached_free(memc);
93 return EXIT_FAILURE;
94 }
95 }
96
97 while (optind < argc)
98 {
99 string= memcached_get(memc, argv[optind], strlen(argv[optind]),
100 &string_length, &flags, &rc);
101 if (rc == MEMCACHED_SUCCESS)
102 {
103 if (opt_displayflag)
104 {
105 if (opt_verbose)
106 {
107 std::cout << "key: " << argv[optind] << std::endl << "flags: " << flags << std::endl;
108 }
109 }
110 else
111 {
112 if (opt_verbose)
113 {
114 std::cout << "key: " << argv[optind] << std::endl << "flags: " << flags << "length: " << string_length << std::endl << "value: ";
115 }
116
117 if (opt_file)
118 {
119 FILE *fp= fopen(opt_file, "w");
120 if (fp == NULL)
121 {
122 perror("fopen");
123 return_code= EXIT_FAILURE;
124 break;
125 }
126
127 size_t written= fwrite(string, 1, string_length, fp);
128 if (written != string_length)
129 {
130 std::cerr << "error writing file to file " << opt_file << " wrote " << written << ", should have written" << string_length << std::endl;
131 return_code= EXIT_FAILURE;
132 break;
133 }
134
135 if (fclose(fp))
136 {
137 std::cerr << "error closing " << opt_file << std::endl;
138 return_code= EXIT_FAILURE;
139 break;
140 }
141 }
142 else
143 {
144 std::cout.write(string, string_length);
145 std::cout << std::endl;
146 }
147 free(string);
148 }
149 }
150 else if (rc != MEMCACHED_NOTFOUND)
151 {
152 std::cerr << "error on " << argv[optind] << "(" << memcached_strerror(memc, rc) << ")";
153 if (memcached_last_error_errno(memc))
154 {
155 std::cerr << " system error (" << strerror(memcached_last_error_errno(memc)) << ")" << std::endl;
156 }
157 std::cerr << std::endl;
158
159 return_code= EXIT_FAILURE;
160 break;
161 }
162 else // Unknown Issue
163 {
164 std::cerr << "error on " << argv[optind] << "("<< memcached_strerror(NULL, rc) << ")" << std::endl;
165 return_code= EXIT_FAILURE;
166 }
167 optind++;
168 }
169
170 memcached_free(memc);
171
172 if (opt_servers)
173 {
174 free(opt_servers);
175 }
176 if (opt_hash)
177 {
178 free(opt_hash);
179 }
180
181 return return_code;
182 }
183
184
185 void options_parse(int argc, char *argv[])
186 {
187 int option_index= 0;
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 int 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 }