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.
20 #include <libmemcached/memcached.h>
22 #include "utilities.h"
24 #define PROGRAM_NAME "memcat"
25 #define PROGRAM_DESCRIPTION "Cat a set of key values to stdout."
29 void options_parse(int argc
, char *argv
[]);
31 static int opt_binary
= 0;
32 static int opt_verbose
= 0;
33 static int opt_displayflag
= 0;
34 static char *opt_servers
= NULL
;
35 static char *opt_hash
= NULL
;
36 static char *opt_username
;
37 static char *opt_passwd
;
38 static char *opt_file
;
40 int main(int argc
, char *argv
[])
46 memcached_return_t rc
;
47 memcached_server_st
*servers
;
51 options_parse(argc
, argv
);
58 if ((temp
= getenv("MEMCACHED_SERVERS")))
59 opt_servers
= strdup(temp
);
62 fprintf(stderr
, "No Servers provided\n");
67 memc
= memcached_create(NULL
);
68 process_hash_option(memc
, opt_hash
);
70 servers
= memcached_servers_parse(opt_servers
);
72 memcached_server_push(memc
, servers
);
73 memcached_server_list_free(servers
);
74 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
,
75 (uint64_t)opt_binary
);
77 if (opt_username
and LIBMEMCACHED_WITH_SASL_SUPPORT
== 0)
80 std::cerr
<< "--username was supplied, but binary was not built with SASL support." << std::endl
;
84 if (opt_username
and initialize_sasl(memc
, opt_username
, opt_passwd
) == false)
92 string
= memcached_get(memc
, argv
[optind
], strlen(argv
[optind
]),
93 &string_length
, &flags
, &rc
);
94 if (rc
== MEMCACHED_SUCCESS
)
99 printf("key: %s\nflags: ", argv
[optind
]);
100 printf("%x\n", flags
);
106 printf("key: %s\nflags: %x\nlength: %lu\nvalue: ",
107 argv
[optind
], flags
, (unsigned long)string_length
);
115 fp
= fopen(opt_file
, "w");
123 written
= fwrite(string
, 1, string_length
, fp
);
124 if (written
!= string_length
)
126 fprintf(stderr
, "error writing file (written %lu, should be %lu)\n", (unsigned long)written
, (unsigned long)string_length
);
133 fprintf(stderr
, "error closing file\n");
140 printf("%.*s\n", (int)string_length
, string
);
145 else if (rc
!= MEMCACHED_NOTFOUND
)
147 fprintf(stderr
, "memcat: %s: memcache error %s",
148 argv
[optind
], memcached_strerror(memc
, rc
));
149 if (memcached_last_error_errno(memc
))
151 fprintf(stderr
, " system error %s", strerror(memcached_last_error_errno(memc
)));
153 fprintf(stderr
, "\n");
158 else // Unknown Issue
160 fprintf(stderr
, "memcat: %s not found\n", argv
[optind
]);
166 memcached_free(memc
);
179 void options_parse(int argc
, char *argv
[])
184 memcached_programs_help_st help_options
[]=
189 static struct option long_options
[]=
191 {(OPTIONSTRING
)"version", no_argument
, NULL
, OPT_VERSION
},
192 {(OPTIONSTRING
)"help", no_argument
, NULL
, OPT_HELP
},
193 {(OPTIONSTRING
)"verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
194 {(OPTIONSTRING
)"debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
195 {(OPTIONSTRING
)"servers", required_argument
, NULL
, OPT_SERVERS
},
196 {(OPTIONSTRING
)"flag", no_argument
, &opt_displayflag
, OPT_FLAG
},
197 {(OPTIONSTRING
)"hash", required_argument
, NULL
, OPT_HASH
},
198 {(OPTIONSTRING
)"binary", no_argument
, NULL
, OPT_BINARY
},
199 {(OPTIONSTRING
)"username", required_argument
, NULL
, OPT_USERNAME
},
200 {(OPTIONSTRING
)"password", required_argument
, NULL
, OPT_PASSWD
},
201 {(OPTIONSTRING
)"file", required_argument
, NULL
, OPT_FILE
},
207 option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
208 if (option_rv
== -1) break;
216 case OPT_VERBOSE
: /* --verbose or -v */
217 opt_verbose
= OPT_VERBOSE
;
219 case OPT_DEBUG
: /* --debug or -d */
220 opt_verbose
= OPT_DEBUG
;
222 case OPT_VERSION
: /* --version or -V */
223 version_command(PROGRAM_NAME
);
225 case OPT_HELP
: /* --help or -h */
226 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);
228 case OPT_SERVERS
: /* --servers or -s */
229 opt_servers
= strdup(optarg
);
232 opt_hash
= strdup(optarg
);
235 opt_username
= optarg
;
244 /* getopt_long already printed an error message. */