1 #include "libmemcached/common.h"
7 #include <libmemcached/memcached.h>
11 #define PROGRAM_NAME "memcat"
12 #define PROGRAM_DESCRIPTION "Cat a set of key values to stdout."
16 void options_parse(int argc
, char *argv
[]);
18 static int opt_binary
= 0;
19 static int opt_verbose
= 0;
20 static int opt_displayflag
= 0;
21 static char *opt_servers
= NULL
;
22 static char *opt_hash
= NULL
;
24 int main(int argc
, char *argv
[])
31 memcached_server_st
*servers
;
33 options_parse(argc
, argv
);
39 if ((temp
= getenv("MEMCACHED_SERVERS")))
40 opt_servers
= strdup(temp
);
43 fprintf(stderr
, "No Servers provided\n");
48 memc
= memcached_create(NULL
);
49 process_hash_option(memc
, opt_hash
);
51 servers
= memcached_servers_parse(opt_servers
);
53 memcached_server_push(memc
, servers
);
54 memcached_server_list_free(servers
);
55 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, opt_binary
);
59 string
= memcached_get(memc
, argv
[optind
], strlen(argv
[optind
]),
60 &string_length
, &flags
, &rc
);
61 if (rc
== MEMCACHED_SUCCESS
)
66 printf("key: %s\nflags: ", argv
[optind
]);
67 printf("%x\n", flags
);
72 printf("key: %s\nflags: %x\nlength: %zu\nvalue: ",
73 argv
[optind
], flags
, string_length
);
74 printf("%.*s\n", (int)string_length
, string
);
78 else if (rc
!= MEMCACHED_NOTFOUND
)
80 fprintf(stderr
, "memcat: %s: memcache error %s",
81 argv
[optind
], memcached_strerror(memc
, rc
));
82 if (memc
->cached_errno
)
83 fprintf(stderr
, " system error %s", strerror(memc
->cached_errno
));
84 fprintf(stderr
, "\n");
100 void options_parse(int argc
, char *argv
[])
105 memcached_programs_help_st help_options
[]=
110 static struct option long_options
[]=
112 {"version", no_argument
, NULL
, OPT_VERSION
},
113 {"help", no_argument
, NULL
, OPT_HELP
},
114 {"verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
115 {"debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
116 {"servers", required_argument
, NULL
, OPT_SERVERS
},
117 {"flag", no_argument
, &opt_displayflag
, OPT_FLAG
},
118 {"hash", required_argument
, NULL
, OPT_HASH
},
119 {"binary", no_argument
, NULL
, OPT_BINARY
},
125 option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
126 if (option_rv
== -1) break;
134 case OPT_VERBOSE
: /* --verbose or -v */
135 opt_verbose
= OPT_VERBOSE
;
137 case OPT_DEBUG
: /* --debug or -d */
138 opt_verbose
= OPT_DEBUG
;
140 case OPT_VERSION
: /* --version or -V */
141 version_command(PROGRAM_NAME
);
143 case OPT_HELP
: /* --help or -h */
144 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);
146 case OPT_SERVERS
: /* --servers or -s */
147 opt_servers
= strdup(optarg
);
150 opt_hash
= strdup(optarg
);
153 /* getopt_long already printed an error message. */