command line error reporting, use my_errno
[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",
73 argv[optind], memcached_strerror(memc, rc));
74 if (memc->my_errno)
75 fprintf(stderr, " system error %s", strerror(memc->my_errno));
76 fprintf(stderr, "\n");
77 }
78 optind++;
79 }
80
81 memcached_free(memc);
82
83 free(opt_servers);
84
85 return 0;
86 }
87
88
89 void options_parse(int argc, char *argv[])
90 {
91 int option_index= 0;
92 int option_rv;
93
94 memcached_programs_help_st help_options[]=
95 {
96 {0},
97 };
98
99 static struct option long_options[]=
100 {
101 {"version", no_argument, NULL, OPT_VERSION},
102 {"help", no_argument, NULL, OPT_HELP},
103 {"verbose", no_argument, &opt_verbose, OPT_VERBOSE},
104 {"debug", no_argument, &opt_verbose, OPT_DEBUG},
105 {"servers", required_argument, NULL, OPT_SERVERS},
106 {"flag", no_argument, &opt_displayflag, OPT_FLAG},
107 {0, 0, 0, 0},
108 };
109
110 while (1)
111 {
112 option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
113 if (option_rv == -1) break;
114 switch (option_rv)
115 {
116 case 0:
117 break;
118 case OPT_VERBOSE: /* --verbose or -v */
119 opt_verbose = OPT_VERBOSE;
120 break;
121 case OPT_DEBUG: /* --debug or -d */
122 opt_verbose = OPT_DEBUG;
123 break;
124 case OPT_VERSION: /* --version or -V */
125 version_command(PROGRAM_NAME);
126 break;
127 case OPT_HELP: /* --help or -h */
128 help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options);
129 break;
130 case OPT_SERVERS: /* --servers or -s */
131 opt_servers= strdup(optarg);
132 break;
133 case '?':
134 /* getopt_long already printed an error message. */
135 exit(1);
136 default:
137 abort();
138 }
139 }
140 }