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