Merge in tests for SASL
[m6w6/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 <iostream>
15 #include <stdio.h>
16 #include <inttypes.h>
17 #include <string.h>
18 #include <unistd.h>
19 #include <getopt.h>
20 #include <libmemcached/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= 0;
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 opt_servers= strdup(temp);
60 else
61 {
62 fprintf(stderr, "No Servers provided\n");
63 exit(1);
64 }
65 }
66
67 memc= memcached_create(NULL);
68 process_hash_option(memc, opt_hash);
69
70 servers= memcached_servers_parse(opt_servers);
71
72 memcached_server_push(memc, servers);
73 memcached_server_list_free(servers);
74 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL,
75 (uint64_t)opt_binary);
76
77 if (opt_username and LIBMEMCACHED_WITH_SASL_SUPPORT == 0)
78 {
79 memcached_free(memc);
80 std::cerr << "--username was supplied, but binary was not built with SASL support." << std::endl;
81 return EXIT_FAILURE;
82 }
83
84 if (opt_username)
85 {
86 memcached_return_t ret;
87 if (memcached_failed(ret= memcached_set_sasl_auth_data(memc, opt_username, opt_passwd)))
88 {
89 std::cerr << memcached_last_error_message(memc) << std::endl;
90 memcached_free(memc);
91 return EXIT_FAILURE;
92 }
93 }
94
95 while (optind < argc)
96 {
97 string= memcached_get(memc, argv[optind], strlen(argv[optind]),
98 &string_length, &flags, &rc);
99 if (rc == MEMCACHED_SUCCESS)
100 {
101 if (opt_displayflag)
102 {
103 if (opt_verbose)
104 printf("key: %s\nflags: ", argv[optind]);
105 printf("%x\n", flags);
106 }
107 else
108 {
109 if (opt_verbose)
110 {
111 printf("key: %s\nflags: %x\nlength: %lu\nvalue: ",
112 argv[optind], flags, (unsigned long)string_length);
113 }
114
115 if (opt_file)
116 {
117 FILE *fp;
118 size_t written;
119
120 fp= fopen(opt_file, "w");
121 if (!fp)
122 {
123 perror("fopen");
124 return_code= -1;
125 break;
126 }
127
128 written= fwrite(string, 1, string_length, fp);
129 if (written != string_length)
130 {
131 fprintf(stderr, "error writing file (written %lu, should be %lu)\n", (unsigned long)written, (unsigned long)string_length);
132 return_code= -1;
133 break;
134 }
135
136 if (fclose(fp))
137 {
138 fprintf(stderr, "error closing file\n");
139 return_code= -1;
140 break;
141 }
142 }
143 else
144 {
145 printf("%.*s\n", (int)string_length, string);
146 }
147 free(string);
148 }
149 }
150 else if (rc != MEMCACHED_NOTFOUND)
151 {
152 fprintf(stderr, "memcat: %s: memcache error %s",
153 argv[optind], memcached_strerror(memc, rc));
154 if (memcached_last_error_errno(memc))
155 {
156 fprintf(stderr, " system error %s", strerror(memcached_last_error_errno(memc)));
157 }
158 fprintf(stderr, "\n");
159
160 return_code= -1;
161 break;
162 }
163 else // Unknown Issue
164 {
165 fprintf(stderr, "memcat: %s not found\n", argv[optind]);
166 return_code= -1;
167 }
168 optind++;
169 }
170
171 memcached_free(memc);
172
173 if (opt_servers)
174 free(opt_servers);
175 if (opt_hash)
176 free(opt_hash);
177
178 return return_code;
179 }
180
181
182 void options_parse(int argc, char *argv[])
183 {
184 int option_index= 0;
185 int option_rv;
186
187 memcached_programs_help_st help_options[]=
188 {
189 {0},
190 };
191
192 static struct option long_options[]=
193 {
194 {(OPTIONSTRING)"version", no_argument, NULL, OPT_VERSION},
195 {(OPTIONSTRING)"help", no_argument, NULL, OPT_HELP},
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 case '?':
247 /* getopt_long already printed an error message. */
248 exit(1);
249 default:
250 abort();
251 }
252 }
253 }