btrfs-progs: drop feature defines from C files, in favour of CFLAGS defines
[platform/upstream/btrfs-progs.git] / btrfs.c
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public
4  * License v2 as published by the Free Software Foundation.
5  *
6  * This program is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
9  * General Public License for more details.
10  *
11  * You should have received a copy of the GNU General Public
12  * License along with this program; if not, write to the
13  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
14  * Boston, MA 021110-1307, USA.
15  */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20
21 #include "crc32c.h"
22 #include "commands.h"
23 #include "version.h"
24 #include "utils.h"
25
26 static const char * const btrfs_cmd_group_usage[] = {
27         "btrfs [--help] [--version] <group> [<group>...] <command> [<args>]",
28         NULL
29 };
30
31 static const char btrfs_cmd_group_info[] =
32         "Use --help as an argument for information on a specific group or command.";
33
34 static inline const char *skip_prefix(const char *str, const char *prefix)
35 {
36         size_t len = strlen(prefix);
37         return strncmp(str, prefix, len) ? NULL : str + len;
38 }
39
40 int prefixcmp(const char *str, const char *prefix)
41 {
42         for (; ; str++, prefix++)
43                 if (!*prefix)
44                         return 0;
45                 else if (*str != *prefix)
46                         return (unsigned char)*prefix - (unsigned char)*str;
47 }
48
49 static int parse_one_token(const char *arg, const struct cmd_group *grp,
50                            const struct cmd_struct **cmd_ret)
51 {
52         const struct cmd_struct *cmd = grp->commands;
53         const struct cmd_struct *abbrev_cmd = NULL, *ambiguous_cmd = NULL;
54
55         for (; cmd->token; cmd++) {
56                 const char *rest;
57
58                 rest = skip_prefix(arg, cmd->token);
59                 if (!rest) {
60                         if (!prefixcmp(cmd->token, arg)) {
61                                 if (abbrev_cmd) {
62                                         /*
63                                          * If this is abbreviated, it is
64                                          * ambiguous. So when there is no
65                                          * exact match later, we need to
66                                          * error out.
67                                          */
68                                         ambiguous_cmd = abbrev_cmd;
69                                 }
70                                 abbrev_cmd = cmd;
71                         }
72                         continue;
73                 }
74                 if (*rest)
75                         continue;
76
77                 *cmd_ret = cmd;
78                 return 0;
79         }
80
81         if (ambiguous_cmd)
82                 return -2;
83
84         if (abbrev_cmd) {
85                 *cmd_ret = abbrev_cmd;
86                 return 0;
87         }
88
89         return -1;
90 }
91
92 static const struct cmd_struct *
93 parse_command_token(const char *arg, const struct cmd_group *grp)
94 {
95         const struct cmd_struct *cmd = NULL;
96
97         switch(parse_one_token(arg, grp, &cmd)) {
98         case -1:
99                 help_unknown_token(arg, grp);
100         case -2:
101                 help_ambiguous_token(arg, grp);
102         }
103
104         return cmd;
105 }
106
107 static void handle_help_options_next_level(const struct cmd_struct *cmd,
108                 int argc, char **argv)
109 {
110         if (argc < 2)
111                 return;
112
113         if (!strcmp(argv[1], "--help")) {
114                 if (cmd->next) {
115                         argc--;
116                         argv++;
117                         help_command_group(cmd->next, argc, argv);
118                 } else {
119                         usage_command(cmd, 1, 0);
120                 }
121
122                 exit(0);
123         }
124 }
125
126 int handle_command_group(const struct cmd_group *grp, int argc,
127                          char **argv)
128
129 {
130         const struct cmd_struct *cmd;
131
132         argc--;
133         argv++;
134         if (argc < 1) {
135                 usage_command_group(grp, 0, 0);
136                 exit(1);
137         }
138
139         cmd = parse_command_token(argv[0], grp);
140
141         handle_help_options_next_level(cmd, argc, argv);
142
143         fixup_argv0(argv, cmd->token);
144         return cmd->fn(argc, argv);
145 }
146
147 static const struct cmd_group btrfs_cmd_group;
148
149 static const char * const cmd_help_usage[] = {
150         "btrfs help [--full]",
151         "Display help information",
152         "",
153         "--full     display detailed help on every command",
154         NULL
155 };
156
157 static int cmd_help(int argc, char **argv)
158 {
159         help_command_group(&btrfs_cmd_group, argc, argv);
160         return 0;
161 }
162
163 static const char * const cmd_version_usage[] = {
164         "btrfs version",
165         "Display btrfs-progs version",
166         NULL
167 };
168
169 static int cmd_version(int argc, char **argv)
170 {
171         printf("%s\n", BTRFS_BUILD_VERSION);
172         return 0;
173 }
174
175 static void handle_options(int *argc, char ***argv)
176 {
177         if (*argc > 0) {
178                 const char *arg = (*argv)[0];
179                 if (arg[0] != '-' ||
180                     !strcmp(arg, "--help") ||
181                     !strcmp(arg, "--version"))
182                         return;
183                 fprintf(stderr, "Unknown option: %s\n", arg);
184                 fprintf(stderr, "usage: %s\n",
185                         btrfs_cmd_group.usagestr[0]);
186                 exit(129);
187         }
188         return;
189 }
190
191 static const struct cmd_group btrfs_cmd_group = {
192         btrfs_cmd_group_usage, btrfs_cmd_group_info, {
193                 { "subvolume", cmd_subvolume, NULL, &subvolume_cmd_group, 0 },
194                 { "filesystem", cmd_filesystem, NULL, &filesystem_cmd_group, 0 },
195                 { "balance", cmd_balance, NULL, &balance_cmd_group, 0 },
196                 { "device", cmd_device, NULL, &device_cmd_group, 0 },
197                 { "scrub", cmd_scrub, NULL, &scrub_cmd_group, 0 },
198                 { "check", cmd_check, cmd_check_usage, NULL, 0 },
199                 { "rescue", cmd_rescue, NULL, &rescue_cmd_group, 0 },
200                 { "restore", cmd_restore, cmd_restore_usage, NULL, 0 },
201                 { "inspect-internal", cmd_inspect, NULL, &inspect_cmd_group, 0 },
202                 { "property", cmd_property, NULL, &property_cmd_group, 0 },
203                 { "send", cmd_send, cmd_send_usage, NULL, 0 },
204                 { "receive", cmd_receive, cmd_receive_usage, NULL, 0 },
205                 { "quota", cmd_quota, NULL, &quota_cmd_group, 0 },
206                 { "qgroup", cmd_qgroup, NULL, &qgroup_cmd_group, 0 },
207                 { "replace", cmd_replace, NULL, &replace_cmd_group, 0 },
208                 { "help", cmd_help, cmd_help_usage, NULL, 0 },
209                 { "version", cmd_version, cmd_version_usage, NULL, 0 },
210                 NULL_CMD_STRUCT
211         },
212 };
213
214 int main(int argc, char **argv)
215 {
216         const struct cmd_struct *cmd;
217         const char *bname;
218
219         if ((bname = strrchr(argv[0], '/')) != NULL)
220                 bname++;
221         else
222                 bname = argv[0];
223
224         if (!strcmp(bname, "btrfsck")) {
225                 argv[0] = "check";
226         } else {
227                 argc--;
228                 argv++;
229                 handle_options(&argc, &argv);
230                 if (argc > 0) {
231                         if (!prefixcmp(argv[0], "--"))
232                                 argv[0] += 2;
233                 } else {
234                         usage_command_group(&btrfs_cmd_group, 0, 0);
235                         exit(1);
236                 }
237         }
238
239         cmd = parse_command_token(argv[0], &btrfs_cmd_group);
240
241         handle_help_options_next_level(cmd, argc, argv);
242
243         crc32c_optimization_init();
244
245         fixup_argv0(argv, cmd->token);
246         exit(cmd->fn(argc, argv));
247 }