Update for mavericks
[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 char *string;
43 size_t string_length;
44 uint32_t flags;
45 memcached_return_t rc;
46
47 int return_code= EXIT_SUCCESS;
48
49 options_parse(argc, argv);
50 initialize_sockets();
51
52 if (opt_servers == NULL)
53 {
54 char *temp;
55
56 if ((temp= getenv("MEMCACHED_SERVERS")))
57 {
58 opt_servers= strdup(temp);
59 }
60
61 if (opt_servers == NULL)
62 {
63 std::cerr << "No servers provied" << std::endl;
64 exit(EXIT_FAILURE);
65 }
66 }
67
68 memcached_server_st* servers= memcached_servers_parse(opt_servers);
69 if (servers == NULL or memcached_server_list_count(servers) == 0)
70 {
71 std::cerr << "Invalid server list provided:" << opt_servers << std::endl;
72 return EXIT_FAILURE;
73 }
74
75 memcached_st* memc= memcached_create(NULL);
76 process_hash_option(memc, opt_hash);
77
78 memcached_server_push(memc, servers);
79 memcached_server_list_free(servers);
80 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL,
81 (uint64_t)opt_binary);
82
83 if (opt_username and LIBMEMCACHED_WITH_SASL_SUPPORT == 0)
84 {
85 memcached_free(memc);
86 std::cerr << "--username was supplied, but binary was not built with SASL support." << std::endl;
87 return EXIT_FAILURE;
88 }
89
90 if (opt_username)
91 {
92 memcached_return_t ret;
93 if (memcached_failed(ret= memcached_set_sasl_auth_data(memc, opt_username, opt_passwd)))
94 {
95 std::cerr << memcached_last_error_message(memc) << std::endl;
96 memcached_free(memc);
97 return EXIT_FAILURE;
98 }
99 }
100
101 while (optind < argc)
102 {
103 string= memcached_get(memc, argv[optind], strlen(argv[optind]),
104 &string_length, &flags, &rc);
105 if (rc == MEMCACHED_SUCCESS)
106 {
107 if (opt_displayflag)
108 {
109 if (opt_verbose)
110 {
111 std::cout << "key: " << argv[optind] << std::endl << "flags: " << flags << std::endl;
112 }
113 }
114 else
115 {
116 if (opt_verbose)
117 {
118 std::cout << "key: " << argv[optind] << std::endl << "flags: " << flags << "length: " << string_length << std::endl << "value: ";
119 }
120
121 if (opt_file)
122 {
123 FILE *fp= fopen(opt_file, "w");
124 if (fp == NULL)
125 {
126 perror("fopen");
127 return_code= EXIT_FAILURE;
128 break;
129 }
130
131 size_t written= fwrite(string, 1, string_length, fp);
132 if (written != string_length)
133 {
134 std::cerr << "error writing file to file " << opt_file << " wrote " << written << ", should have written" << string_length << std::endl;
135 return_code= EXIT_FAILURE;
136 break;
137 }
138
139 if (fclose(fp))
140 {
141 std::cerr << "error closing " << opt_file << std::endl;
142 return_code= EXIT_FAILURE;
143 break;
144 }
145 }
146 else
147 {
148 std::cout.write(string, string_length);
149 std::cout << std::endl;
150 }
151 free(string);
152 }
153 }
154 else if (rc != MEMCACHED_NOTFOUND)
155 {
156 std::cerr << "error on " << argv[optind] << "(" << memcached_strerror(memc, rc) << ")";
157 if (memcached_last_error_errno(memc))
158 {
159 std::cerr << " system error (" << strerror(memcached_last_error_errno(memc)) << ")" << std::endl;
160 }
161 std::cerr << std::endl;
162
163 return_code= EXIT_FAILURE;
164 break;
165 }
166 else // Unknown Issue
167 {
168 std::cerr << "error on " << argv[optind] << "("<< memcached_strerror(NULL, rc) << ")" << std::endl;
169 return_code= EXIT_FAILURE;
170 }
171 optind++;
172 }
173
174 memcached_free(memc);
175
176 if (opt_servers)
177 {
178 free(opt_servers);
179 }
180 if (opt_hash)
181 {
182 free(opt_hash);
183 }
184
185 return return_code;
186 }
187
188
189 void options_parse(int argc, char *argv[])
190 {
191 int option_index= 0;
192
193 memcached_programs_help_st help_options[]=
194 {
195 {0},
196 };
197
198 static struct option long_options[]=
199 {
200 {(OPTIONSTRING)"version", no_argument, NULL, OPT_VERSION},
201 {(OPTIONSTRING)"help", no_argument, NULL, OPT_HELP},
202 {(OPTIONSTRING)"quiet", no_argument, NULL, OPT_QUIET},
203 {(OPTIONSTRING)"verbose", no_argument, &opt_verbose, OPT_VERBOSE},
204 {(OPTIONSTRING)"debug", no_argument, &opt_verbose, OPT_DEBUG},
205 {(OPTIONSTRING)"servers", required_argument, NULL, OPT_SERVERS},
206 {(OPTIONSTRING)"flag", no_argument, &opt_displayflag, OPT_FLAG},
207 {(OPTIONSTRING)"hash", required_argument, NULL, OPT_HASH},
208 {(OPTIONSTRING)"binary", no_argument, NULL, OPT_BINARY},
209 {(OPTIONSTRING)"username", required_argument, NULL, OPT_USERNAME},
210 {(OPTIONSTRING)"password", required_argument, NULL, OPT_PASSWD},
211 {(OPTIONSTRING)"file", required_argument, NULL, OPT_FILE},
212 {0, 0, 0, 0},
213 };
214
215 while (1)
216 {
217 int option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
218 if (option_rv == -1) break;
219 switch (option_rv)
220 {
221 case 0:
222 break;
223 case OPT_BINARY:
224 opt_binary = 1;
225 break;
226 case OPT_VERBOSE: /* --verbose or -v */
227 opt_verbose = OPT_VERBOSE;
228 break;
229 case OPT_DEBUG: /* --debug or -d */
230 opt_verbose = OPT_DEBUG;
231 break;
232 case OPT_VERSION: /* --version or -V */
233 version_command(PROGRAM_NAME);
234 break;
235 case OPT_HELP: /* --help or -h */
236 help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options);
237 break;
238 case OPT_SERVERS: /* --servers or -s */
239 opt_servers= strdup(optarg);
240 break;
241 case OPT_HASH:
242 opt_hash= strdup(optarg);
243 break;
244 case OPT_USERNAME:
245 opt_username= optarg;
246 break;
247 case OPT_PASSWD:
248 opt_passwd= optarg;
249 break;
250 case OPT_FILE:
251 opt_file= optarg;
252 break;
253
254 case OPT_QUIET:
255 close_stdio();
256 break;
257
258 case '?':
259 /* getopt_long already printed an error message. */
260 exit(EXIT_FAILURE);
261 default:
262 abort();
263 }
264 }
265 }