6 #include <libmemcached/memcached.h>
10 #define PROGRAM_NAME "memcat"
11 #define PROGRAM_DESCRIPTION "Cat a set of key values to stdout."
15 void options_parse(int argc
, char *argv
[]);
17 static int opt_binary
= 0;
18 static int opt_verbose
= 0;
19 static int opt_displayflag
= 0;
20 static char *opt_servers
= NULL
;
21 static char *opt_hash
= NULL
;
23 int main(int argc
, char *argv
[])
30 memcached_server_st
*servers
;
32 options_parse(argc
, argv
);
38 if ((temp
= getenv("MEMCACHED_SERVERS")))
39 opt_servers
= strdup(temp
);
42 fprintf(stderr
, "No Servers provided\n");
47 memc
= memcached_create(NULL
);
48 process_hash_option(memc
, opt_hash
);
50 servers
= memcached_servers_parse(opt_servers
);
52 memcached_server_push(memc
, servers
);
53 memcached_server_list_free(servers
);
54 memcached_behavior_set(memc
, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL
, opt_binary
);
58 string
= memcached_get(memc
, argv
[optind
], strlen(argv
[optind
]),
59 &string_length
, &flags
, &rc
);
60 if (rc
== MEMCACHED_SUCCESS
)
65 printf("key: %s\nflags: ", argv
[optind
]);
66 printf("%x\n", flags
);
71 printf("key: %s\nflags: %x\nlength: %zu\nvalue: ",
72 argv
[optind
], flags
, string_length
);
73 printf("%.*s\n", (int)string_length
, string
);
77 else if (rc
!= MEMCACHED_NOTFOUND
)
79 fprintf(stderr
, "memcat: %s: memcache error %s",
80 argv
[optind
], memcached_strerror(memc
, rc
));
81 if (memc
->cached_errno
)
82 fprintf(stderr
, " system error %s", strerror(memc
->cached_errno
));
83 fprintf(stderr
, "\n");
99 void options_parse(int argc
, char *argv
[])
104 memcached_programs_help_st help_options
[]=
109 static struct option long_options
[]=
111 {"version", no_argument
, NULL
, OPT_VERSION
},
112 {"help", no_argument
, NULL
, OPT_HELP
},
113 {"verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
114 {"debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
115 {"servers", required_argument
, NULL
, OPT_SERVERS
},
116 {"flag", no_argument
, &opt_displayflag
, OPT_FLAG
},
117 {"hash", required_argument
, NULL
, OPT_HASH
},
118 {"binary", no_argument
, NULL
, OPT_BINARY
},
124 option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
125 if (option_rv
== -1) break;
133 case OPT_VERBOSE
: /* --verbose or -v */
134 opt_verbose
= OPT_VERBOSE
;
136 case OPT_DEBUG
: /* --debug or -d */
137 opt_verbose
= OPT_DEBUG
;
139 case OPT_VERSION
: /* --version or -V */
140 version_command(PROGRAM_NAME
);
142 case OPT_HELP
: /* --help or -h */
143 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);
145 case OPT_SERVERS
: /* --servers or -s */
146 opt_servers
= strdup(optarg
);
149 opt_hash
= strdup(optarg
);
152 /* getopt_long already printed an error message. */