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