2 * Copyright (C) 2006-2009 Brian Aker
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
19 #include <libmemcached/memcached.h>
21 #include "utilities.h"
23 #define PROGRAM_NAME "memcat"
24 #define PROGRAM_DESCRIPTION "Cat a set of key values to stdout."
28 void options_parse(int argc
, char *argv
[]);
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
;
39 int main(int argc
, char *argv
[])
45 memcached_return_t rc
;
46 memcached_server_st
*servers
;
50 options_parse(argc
, argv
);
57 if ((temp
= getenv("MEMCACHED_SERVERS")))
58 opt_servers
= strdup(temp
);
61 fprintf(stderr
, "No Servers provided\n");
66 memc
= memcached_create(NULL
);
67 process_hash_option(memc
, opt_hash
);
69 servers
= memcached_servers_parse(opt_servers
);
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
);
76 if (opt_username
and LIBMEMCACHED_WITH_SASL_SUPPORT
== 0)
79 std::cerr
<< "--username was supplied, but binary was not built with SASL support." << std::endl
;
85 memcached_return_t ret
;
86 if (memcached_failed(ret
= memcached_set_sasl_auth_data(memc
, opt_username
, opt_passwd
)))
88 std::cerr
<< memcached_last_error_message(memc
) << std::endl
;
96 string
= memcached_get(memc
, argv
[optind
], strlen(argv
[optind
]),
97 &string_length
, &flags
, &rc
);
98 if (rc
== MEMCACHED_SUCCESS
)
103 printf("key: %s\nflags: ", argv
[optind
]);
104 printf("%x\n", flags
);
110 printf("key: %s\nflags: %x\nlength: %lu\nvalue: ",
111 argv
[optind
], flags
, (unsigned long)string_length
);
119 fp
= fopen(opt_file
, "w");
127 written
= fwrite(string
, 1, string_length
, fp
);
128 if (written
!= string_length
)
130 fprintf(stderr
, "error writing file (written %lu, should be %lu)\n", (unsigned long)written
, (unsigned long)string_length
);
137 fprintf(stderr
, "error closing file\n");
144 printf("%.*s\n", (int)string_length
, string
);
149 else if (rc
!= MEMCACHED_NOTFOUND
)
151 fprintf(stderr
, "memcat: %s: memcache error %s",
152 argv
[optind
], memcached_strerror(memc
, rc
));
153 if (memcached_last_error_errno(memc
))
155 fprintf(stderr
, " system error %s", strerror(memcached_last_error_errno(memc
)));
157 fprintf(stderr
, "\n");
162 else // Unknown Issue
164 fprintf(stderr
, "memcat: %s not found\n", argv
[optind
]);
170 memcached_free(memc
);
181 void options_parse(int argc
, char *argv
[])
186 memcached_programs_help_st help_options
[]=
191 static struct option long_options
[]=
193 {(OPTIONSTRING
)"version", no_argument
, NULL
, OPT_VERSION
},
194 {(OPTIONSTRING
)"help", no_argument
, NULL
, OPT_HELP
},
195 {(OPTIONSTRING
)"quiet", no_argument
, NULL
, OPT_QUIET
},
196 {(OPTIONSTRING
)"verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
197 {(OPTIONSTRING
)"debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
198 {(OPTIONSTRING
)"servers", required_argument
, NULL
, OPT_SERVERS
},
199 {(OPTIONSTRING
)"flag", no_argument
, &opt_displayflag
, OPT_FLAG
},
200 {(OPTIONSTRING
)"hash", required_argument
, NULL
, OPT_HASH
},
201 {(OPTIONSTRING
)"binary", no_argument
, NULL
, OPT_BINARY
},
202 {(OPTIONSTRING
)"username", required_argument
, NULL
, OPT_USERNAME
},
203 {(OPTIONSTRING
)"password", required_argument
, NULL
, OPT_PASSWD
},
204 {(OPTIONSTRING
)"file", required_argument
, NULL
, OPT_FILE
},
210 option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
211 if (option_rv
== -1) break;
219 case OPT_VERBOSE
: /* --verbose or -v */
220 opt_verbose
= OPT_VERBOSE
;
222 case OPT_DEBUG
: /* --debug or -d */
223 opt_verbose
= OPT_DEBUG
;
225 case OPT_VERSION
: /* --version or -V */
226 version_command(PROGRAM_NAME
);
228 case OPT_HELP
: /* --help or -h */
229 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);
231 case OPT_SERVERS
: /* --servers or -s */
232 opt_servers
= strdup(optarg
);
235 opt_hash
= strdup(optarg
);
238 opt_username
= optarg
;
252 /* getopt_long already printed an error message. */