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