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