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_verbose
= 0;
18 static int opt_displayflag
= 0;
19 static char *opt_servers
= NULL
;
20 static char *opt_hash
= NULL
;
22 int main(int argc
, char *argv
[])
29 memcached_server_st
*servers
;
31 options_parse(argc
, argv
);
37 if ((temp
= getenv("MEMCACHED_SERVERS")))
38 opt_servers
= strdup(temp
);
41 fprintf(stderr
, "No Servers provided\n");
46 memc
= memcached_create(NULL
);
47 process_hash_option(memc
, opt_hash
);
49 servers
= memcached_servers_parse(opt_servers
);
51 memcached_server_push(memc
, servers
);
52 memcached_server_list_free(servers
);
56 string
= memcached_get(memc
, argv
[optind
], strlen(argv
[optind
]),
57 &string_length
, &flags
, &rc
);
58 if (rc
== MEMCACHED_SUCCESS
)
63 printf("key: %s\nflags: ", argv
[optind
]);
64 printf("%x\n", flags
);
69 printf("key: %s\nflags: %x\nlength: %zu\nvalue: ",
70 argv
[optind
], flags
, string_length
);
71 printf("%.*s\n", (int)string_length
, string
);
75 else if (rc
!= MEMCACHED_NOTFOUND
)
77 fprintf(stderr
, "memcat: %s: memcache error %s",
78 argv
[optind
], memcached_strerror(memc
, rc
));
79 if (memc
->cached_errno
)
80 fprintf(stderr
, " system error %s", strerror(memc
->cached_errno
));
81 fprintf(stderr
, "\n");
97 void options_parse(int argc
, char *argv
[])
102 memcached_programs_help_st help_options
[]=
107 static struct option long_options
[]=
109 {"version", no_argument
, NULL
, OPT_VERSION
},
110 {"help", no_argument
, NULL
, OPT_HELP
},
111 {"verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
112 {"debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
113 {"servers", required_argument
, NULL
, OPT_SERVERS
},
114 {"flag", no_argument
, &opt_displayflag
, OPT_FLAG
},
115 {"hash", required_argument
, NULL
, OPT_HASH
},
121 option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
122 if (option_rv
== -1) break;
127 case OPT_VERBOSE
: /* --verbose or -v */
128 opt_verbose
= OPT_VERBOSE
;
130 case OPT_DEBUG
: /* --debug or -d */
131 opt_verbose
= OPT_DEBUG
;
133 case OPT_VERSION
: /* --version or -V */
134 version_command(PROGRAM_NAME
);
136 case OPT_HELP
: /* --help or -h */
137 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);
139 case OPT_SERVERS
: /* --servers or -s */
140 opt_servers
= strdup(optarg
);
143 opt_hash
= strdup(optarg
);
146 /* getopt_long already printed an error message. */