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