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.
12 #include "libmemcached/common.h"
18 #include <libmemcached/memcached.h>
20 #include "utilities.h"
22 #define PROGRAM_NAME "memcat"
23 #define PROGRAM_DESCRIPTION "Cat a set of key values to stdout."
27 void options_parse(int argc
, char *argv
[]);
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
;
35 int main(int argc
, char *argv
[])
41 memcached_return_t rc
;
42 memcached_server_st
*servers
;
46 options_parse(argc
, argv
);
52 if ((temp
= getenv("MEMCACHED_SERVERS")))
53 opt_servers
= strdup(temp
);
56 fprintf(stderr
, "No Servers provided\n");
61 memc
= memcached_create(NULL
);
62 process_hash_option(memc
, opt_hash
);
64 servers
= memcached_servers_parse(opt_servers
);
66 memcached_server_push(memc
, servers
);
67 memcached_server_list_free(servers
);
68 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
,
69 (uint64_t)opt_binary
);
73 string
= memcached_get(memc
, argv
[optind
], strlen(argv
[optind
]),
74 &string_length
, &flags
, &rc
);
75 if (rc
== MEMCACHED_SUCCESS
)
80 printf("key: %s\nflags: ", argv
[optind
]);
81 printf("%x\n", flags
);
86 printf("key: %s\nflags: %x\nlength: %zu\nvalue: ",
87 argv
[optind
], flags
, string_length
);
88 printf("%.*s\n", (int)string_length
, string
);
92 else if (rc
!= MEMCACHED_NOTFOUND
)
94 fprintf(stderr
, "memcat: %s: memcache error %s",
95 argv
[optind
], memcached_strerror(memc
, rc
));
96 if (memc
->cached_errno
)
97 fprintf(stderr
, " system error %s", strerror(memc
->cached_errno
));
98 fprintf(stderr
, "\n");
103 else // Unknown Issue
105 fprintf(stderr
, "memcat: %s not found\n", argv
[optind
]);
111 memcached_free(memc
);
122 void options_parse(int argc
, char *argv
[])
127 memcached_programs_help_st help_options
[]=
132 static struct option long_options
[]=
134 {(OPTIONSTRING
)"version", no_argument
, NULL
, OPT_VERSION
},
135 {(OPTIONSTRING
)"help", no_argument
, NULL
, OPT_HELP
},
136 {(OPTIONSTRING
)"verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
137 {(OPTIONSTRING
)"debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
138 {(OPTIONSTRING
)"servers", required_argument
, NULL
, OPT_SERVERS
},
139 {(OPTIONSTRING
)"flag", no_argument
, &opt_displayflag
, OPT_FLAG
},
140 {(OPTIONSTRING
)"hash", required_argument
, NULL
, OPT_HASH
},
141 {(OPTIONSTRING
)"binary", no_argument
, NULL
, OPT_BINARY
},
147 option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
148 if (option_rv
== -1) break;
156 case OPT_VERBOSE
: /* --verbose or -v */
157 opt_verbose
= OPT_VERBOSE
;
159 case OPT_DEBUG
: /* --debug or -d */
160 opt_verbose
= OPT_DEBUG
;
162 case OPT_VERSION
: /* --version or -V */
163 version_command(PROGRAM_NAME
);
165 case OPT_HELP
: /* --help or -h */
166 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);
168 case OPT_SERVERS
: /* --servers or -s */
169 opt_servers
= strdup(optarg
);
172 opt_hash
= strdup(optarg
);
175 /* getopt_long already printed an error message. */