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
);
43 memc
= memcached_create(NULL
);
44 process_hash_option(memc
, opt_hash
);
46 servers
= memcached_servers_parse(opt_servers
);
48 memcached_server_push(memc
, servers
);
49 memcached_server_list_free(servers
);
53 string
= memcached_get(memc
, argv
[optind
], strlen(argv
[optind
]),
54 &string_length
, &flags
, &rc
);
55 if (rc
== MEMCACHED_SUCCESS
)
60 printf("key: %s\nflags: ", argv
[optind
]);
61 printf("%x\n", flags
);
66 printf("key: %s\nflags: %x\nlength: %zu\nvalue: ",
67 argv
[optind
], flags
, string_length
);
68 printf("%.*s\n", (int)string_length
, string
);
72 else if (rc
!= MEMCACHED_NOTFOUND
)
74 fprintf(stderr
, "memcat: %s: memcache error %s",
75 argv
[optind
], memcached_strerror(memc
, rc
));
76 if (memc
->cached_errno
)
77 fprintf(stderr
, " system error %s", strerror(memc
->cached_errno
));
78 fprintf(stderr
, "\n");
94 void options_parse(int argc
, char *argv
[])
99 memcached_programs_help_st help_options
[]=
104 static struct option long_options
[]=
106 {"version", no_argument
, NULL
, OPT_VERSION
},
107 {"help", no_argument
, NULL
, OPT_HELP
},
108 {"verbose", no_argument
, &opt_verbose
, OPT_VERBOSE
},
109 {"debug", no_argument
, &opt_verbose
, OPT_DEBUG
},
110 {"servers", required_argument
, NULL
, OPT_SERVERS
},
111 {"flag", no_argument
, &opt_displayflag
, OPT_FLAG
},
112 {"hash", required_argument
, NULL
, OPT_HASH
},
118 option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
119 if (option_rv
== -1) break;
124 case OPT_VERBOSE
: /* --verbose or -v */
125 opt_verbose
= OPT_VERBOSE
;
127 case OPT_DEBUG
: /* --debug or -d */
128 opt_verbose
= OPT_DEBUG
;
130 case OPT_VERSION
: /* --version or -V */
131 version_command(PROGRAM_NAME
);
133 case OPT_HELP
: /* --help or -h */
134 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, help_options
);
136 case OPT_SERVERS
: /* --servers or -s */
137 opt_servers
= strdup(optarg
);
140 opt_hash
= strdup(optarg
);
143 /* getopt_long already printed an error message. */