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