Updated for help commands (and a little bit on ketama)
[awesomized/libmemcached] / src / memcat.c
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <getopt.h>
4 #include <memcached.h>
5
6 #include "utilities.h"
7
8 #define PROGRAM_NAME "memcat"
9 #define PROGRAM_DESCRIPTION "Cat a set of key values to stdout."
10
11
12 /* Prototypes */
13 void options_parse(int argc, char *argv[]);
14
15 static int opt_verbose= 0;
16 static int opt_displayflag= 0;
17 static char *opt_servers;
18
19 int main(int argc, char *argv[])
20 {
21 memcached_st *memc;
22 char *string;
23 size_t string_length;
24 uint16_t flags;
25 memcached_return rc;
26 memcached_server_st *servers;
27
28 options_parse(argc, argv);
29
30 if (!opt_servers)
31 {
32 char *temp;
33
34 if ((temp= getenv("MEMCACHED_SERVERS")))
35 opt_servers= strdup(temp);
36 else
37 exit(1);
38 }
39
40 memc= memcached_create(NULL);
41
42 servers= memcached_servers_parse(opt_servers);
43
44 memcached_server_push(memc, servers);
45 memcached_server_list_free(servers);
46
47 while (optind < argc)
48 {
49 string= memcached_get(memc, argv[optind], strlen(argv[optind]),
50 &string_length, &flags, &rc);
51 if (rc == MEMCACHED_SUCCESS)
52 {
53 if (opt_displayflag)
54 {
55 if (opt_verbose)
56 printf("key: %s\nflags: ", argv[optind]);
57 printf("%x\n", flags);
58 }
59 else
60 {
61 if (opt_verbose)
62 printf("key: %s\nflags: %x\nlength: %zu\nvalue: ",
63 argv[optind], flags, string_length);
64 printf("%.*s\n", (int)string_length, string);
65 free(string);
66 }
67 }
68 else if (rc != MEMCACHED_NOTFOUND)
69 {
70 fprintf(stderr, "memcat: %s: memcache error %s\n",
71 argv[optind], memcached_strerror(memc, rc));
72 }
73 optind++;
74 }
75
76 memcached_free(memc);
77
78 free(opt_servers);
79
80 return 0;
81 }
82
83
84 void options_parse(int argc, char *argv[])
85 {
86 int option_index= 0;
87 int option_rv;
88
89 memcached_programs_help_st help_options[]=
90 {
91 {0},
92 };
93
94 static struct option long_options[]=
95 {
96 {"version", no_argument, NULL, OPT_VERSION},
97 {"help", no_argument, NULL, OPT_HELP},
98 {"verbose", no_argument, &opt_verbose, OPT_VERBOSE},
99 {"debug", no_argument, &opt_verbose, OPT_DEBUG},
100 {"servers", required_argument, NULL, OPT_SERVERS},
101 {"flag", no_argument, &opt_displayflag, OPT_FLAG},
102 {0, 0, 0, 0},
103 };
104
105 while (1)
106 {
107 option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
108 if (option_rv == -1) break;
109 switch (option_rv)
110 {
111 case 0:
112 break;
113 case OPT_VERBOSE: /* --verbose or -v */
114 opt_verbose = OPT_VERBOSE;
115 break;
116 case OPT_DEBUG: /* --debug or -d */
117 opt_verbose = OPT_DEBUG;
118 break;
119 case OPT_VERSION: /* --version or -V */
120 version_command(PROGRAM_NAME);
121 break;
122 case OPT_HELP: /* --help or -h */
123 help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options);
124 break;
125 case OPT_SERVERS: /* --servers or -s */
126 opt_servers= strdup(optarg);
127 break;
128 case '?':
129 /* getopt_long already printed an error message. */
130 exit(1);
131 default:
132 abort();
133 }
134 }
135 }