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