645b54ac3d762f7459823e835f6d5b1fda16e47c
[awesomized/libmemcached] / src / memcat.c
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <getopt.h>
4 #include <memcached.h>
5 #include "client_options.h"
6
7 static int opt_verbose;
8 static int opt_displayflag;
9 static char *opt_servers;
10
11 struct memcached_st *parse_opt_servers (struct memcached_st *m,
12 char *opt_servers)
13 {
14 char *s, *hostname;
15 unsigned int portnum;
16 while (s = strsep(&opt_servers, ",")) {
17 hostname = strsep(&s, ":");
18 portnum = atoi(s);
19 memcached_server_add(m, hostname, portnum);
20 }
21 return m;
22 }
23
24 int main(int argc, char *argv[])
25 {
26 memcached_st *memc;
27 char *string;
28 size_t string_length;
29 uint16_t flags;
30 memcached_return rc;
31 unsigned int x;
32
33 static struct option long_options[] =
34 {
35 {"version", no_argument, NULL, OPT_VERSION},
36 {"help", no_argument, NULL, OPT_HELP},
37 {"verbose", no_argument, &opt_verbose, OPT_VERBOSE},
38 {"debug", no_argument, &opt_verbose, OPT_DEBUG},
39 {"servers", required_argument, NULL, OPT_SERVERS},
40 {"flag", no_argument, &opt_displayflag, OPT_FLAG},
41 {0, 0, 0, 0},
42 };
43 int option_index = 0;
44 int option_rv;
45
46 while (1)
47 {
48 option_rv = getopt_long(argc, argv, "", long_options, &option_index);
49 if (option_rv == -1) break;
50 switch (option_rv) {
51 case 0:
52 break;
53 case OPT_VERSION: /* --version */
54 printf("memcache tools, memcat, v1.0\n");
55 exit(0);
56 break;
57 case OPT_HELP: /* --help */
58 printf("useful help messages go here\n");
59 exit(0);
60 break;
61 case OPT_SERVERS: /* --servers */
62 opt_servers = strdup(optarg);
63 break;
64 case '?':
65 /* getopt_long already printed an error message. */
66 exit(1);
67 default:
68 abort();
69 }
70 }
71
72 memc = malloc(sizeof(struct memcached_st));
73 memcached_init(memc);
74 memc= parse_opt_servers(memc, opt_servers);
75 memc= memcached_init(memc);
76
77 for (x= 1; x < argc; x++)
78 {
79 string= memcached_get(memc, argv[x], strlen(argv[x]),
80 &string_length, &flags, &rc);
81 if (rc == MEMCACHED_SUCCESS) {
82 if (opt_displayflag) {
83 printf("%d\n", flags);
84 } else {
85 if (string)
86 {
87 printf("%.*s\n", string_length, string);
88 free(string);
89 }
90 }
91 }
92 }
93
94 memcached_deinit(memc);
95
96 return 0;
97 };