Patch by Marc Rossi to add --hash to command line tools.
[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 exit(1);
41 }
42
43 memc= memcached_create(NULL);
44 process_hash_option(memc, opt_hash);
45
46 servers= memcached_servers_parse(opt_servers);
47
48 memcached_server_push(memc, servers);
49 memcached_server_list_free(servers);
50
51 while (optind < argc)
52 {
53 string= memcached_get(memc, argv[optind], strlen(argv[optind]),
54 &string_length, &flags, &rc);
55 if (rc == MEMCACHED_SUCCESS)
56 {
57 if (opt_displayflag)
58 {
59 if (opt_verbose)
60 printf("key: %s\nflags: ", argv[optind]);
61 printf("%x\n", flags);
62 }
63 else
64 {
65 if (opt_verbose)
66 printf("key: %s\nflags: %x\nlength: %zu\nvalue: ",
67 argv[optind], flags, string_length);
68 printf("%.*s\n", (int)string_length, string);
69 free(string);
70 }
71 }
72 else if (rc != MEMCACHED_NOTFOUND)
73 {
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");
79 }
80 optind++;
81 }
82
83 memcached_free(memc);
84
85 if (opt_servers)
86 free(opt_servers);
87 if (opt_hash)
88 free(opt_hash);
89
90 return 0;
91 }
92
93
94 void options_parse(int argc, char *argv[])
95 {
96 int option_index= 0;
97 int option_rv;
98
99 memcached_programs_help_st help_options[]=
100 {
101 {0},
102 };
103
104 static struct option long_options[]=
105 {
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},
113 {0, 0, 0, 0},
114 };
115
116 while (1)
117 {
118 option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
119 if (option_rv == -1) break;
120 switch (option_rv)
121 {
122 case 0:
123 break;
124 case OPT_VERBOSE: /* --verbose or -v */
125 opt_verbose = OPT_VERBOSE;
126 break;
127 case OPT_DEBUG: /* --debug or -d */
128 opt_verbose = OPT_DEBUG;
129 break;
130 case OPT_VERSION: /* --version or -V */
131 version_command(PROGRAM_NAME);
132 break;
133 case OPT_HELP: /* --help or -h */
134 help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options);
135 break;
136 case OPT_SERVERS: /* --servers or -s */
137 opt_servers= strdup(optarg);
138 break;
139 case OPT_HASH:
140 opt_hash= strdup(optarg);
141 break;
142 case '?':
143 /* getopt_long already printed an error message. */
144 exit(1);
145 default:
146 abort();
147 }
148 }
149 }