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