c93f1a21d7812fe63824e2879d0d283356fe7725
[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
188 memcached_programs_help_st help_options[]=
189 {
190 {0},
191 };
192
193 static struct option long_options[]=
194 {
195 {(OPTIONSTRING)"version", no_argument, NULL, OPT_VERSION},
196 {(OPTIONSTRING)"help", no_argument, NULL, OPT_HELP},
197 {(OPTIONSTRING)"quiet", no_argument, NULL, OPT_QUIET},
198 {(OPTIONSTRING)"verbose", no_argument, &opt_verbose, OPT_VERBOSE},
199 {(OPTIONSTRING)"debug", no_argument, &opt_verbose, OPT_DEBUG},
200 {(OPTIONSTRING)"servers", required_argument, NULL, OPT_SERVERS},
201 {(OPTIONSTRING)"flag", no_argument, &opt_displayflag, OPT_FLAG},
202 {(OPTIONSTRING)"hash", required_argument, NULL, OPT_HASH},
203 {(OPTIONSTRING)"binary", no_argument, NULL, OPT_BINARY},
204 {(OPTIONSTRING)"username", required_argument, NULL, OPT_USERNAME},
205 {(OPTIONSTRING)"password", required_argument, NULL, OPT_PASSWD},
206 {(OPTIONSTRING)"file", required_argument, NULL, OPT_FILE},
207 {0, 0, 0, 0},
208 };
209
210 while (1)
211 {
212 int option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
213 if (option_rv == -1) break;
214 switch (option_rv)
215 {
216 case 0:
217 break;
218 case OPT_BINARY:
219 opt_binary = 1;
220 break;
221 case OPT_VERBOSE: /* --verbose or -v */
222 opt_verbose = OPT_VERBOSE;
223 break;
224 case OPT_DEBUG: /* --debug or -d */
225 opt_verbose = OPT_DEBUG;
226 break;
227 case OPT_VERSION: /* --version or -V */
228 version_command(PROGRAM_NAME);
229 break;
230 case OPT_HELP: /* --help or -h */
231 help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options);
232 break;
233 case OPT_SERVERS: /* --servers or -s */
234 opt_servers= strdup(optarg);
235 break;
236 case OPT_HASH:
237 opt_hash= strdup(optarg);
238 break;
239 case OPT_USERNAME:
240 opt_username= optarg;
241 break;
242 case OPT_PASSWD:
243 opt_passwd= optarg;
244 break;
245 case OPT_FILE:
246 opt_file= optarg;
247 break;
248
249 case OPT_QUIET:
250 close_stdio();
251 break;
252
253 case '?':
254 /* getopt_long already printed an error message. */
255 exit(1);
256 default:
257 abort();
258 }
259 }
260 }