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 (!initialize_sasl(memc
, opt_username
, opt_passwd
))
84 string
= memcached_get(memc
, argv
[optind
], strlen(argv
[optind
]),
85 &string_length
, &flags
, &rc
);
86 if (rc
== MEMCACHED_SUCCESS
)
91 printf("key: %s\nflags: ", argv
[optind
]);
92 printf("%x\n", flags
);
98 printf("key: %s\nflags: %x\nlength: %zu\nvalue: ",
99 argv
[optind
], flags
, string_length
);
107 fp
= fopen(opt_file
, "w");
115 written
= fwrite(string
, 1, string_length
, fp
);
116 if (written
!= string_length
)
118 fprintf(stderr
, "error writing file (written %zu, should be %zu)\n", written
, string_length
);
125 fprintf(stderr
, "error closing file\n");
132 printf("%.*s\n", (int)string_length
, string
);
137 else if (rc
!= MEMCACHED_NOTFOUND
)
139 fprintf(stderr
, "memcat: %s: memcache error %s",
140 argv
[optind
], memcached_strerror(memc
, rc
));
141 if (memc
->cached_errno
)
143 fprintf(stderr
, " system error %s", strerror(memc
->cached_errno
));
145 fprintf(stderr
, "\n");
150 else // Unknown Issue
152 fprintf(stderr
, "memcat: %s not found\n", argv
[optind
]);
158 memcached_free(memc
);
171 void options_parse(int argc
, char *argv
[])
176 memcached_programs_help_st help_options
[]=
181 static struct option long_options
[]=
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
},
199 option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
200 if (option_rv
== -1) break;
208 case OPT_VERBOSE
: /* --verbose or -v */
209 opt_verbose
= OPT_VERBOSE
;
211 case OPT_DEBUG
: /* --debug or -d */
212 opt_verbose
= OPT_DEBUG
;
214 case OPT_VERSION
: /* --version or -V */
215 version_command(PROGRAM_NAME
);
217 case OPT_HELP
: /* --help or -h */
218 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);
220 case OPT_SERVERS
: /* --servers or -s */
221 opt_servers
= strdup(optarg
);
224 opt_hash
= strdup(optarg
);
227 opt_username
= optarg
;
236 /* getopt_long already printed an error message. */