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