blob: 787df1db239f0d23785c8719f713e2a18c0624cf [file] [log] [blame]
Goffredo Baroncelli80d26602012-02-12 11:43:14 -05001/*
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 * (This code is based on btrfs-progs/btrfs.c.)
17 */
18
19#define _GNU_SOURCE
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23
24#define MMC_VERSION "0.1"
25
26#define BASIC_HELP 0
27#define ADVANCED_HELP 1
28
29typedef int (*CommandFunction)(int argc, char **argv);
30
31struct Command {
32 CommandFunction func; /* function which implements the command */
33 int nargs; /* if == 999, any number of arguments
34 if >= 0, number of arguments,
35 if < 0, _minimum_ number of arguments */
36 char *verb; /* verb */
37 char *help; /* help lines; from the 2nd line onward they
38 are automatically indented */
39 char *adv_help; /* advanced help message; from the 2nd line
40 onward they are automatically indented */
41
42 /* the following fields are run-time filled by the program */
43 char **cmds; /* array of subcommands */
44 int ncmds; /* number of subcommand */
45};
46
47static struct Command commands[] = {
48 /*
49 * avoid short commands different for the case only
50 */
51 { 0, 0, 0, 0 }
52};
53
54static char *get_prgname(char *programname)
55{
56 char *np;
57 np = strrchr(programname,'/');
58 if(!np)
59 np = programname;
60 else
61 np++;
62
63 return np;
64}
65
66static void print_help(char *programname, struct Command *cmd, int helptype)
67{
68 char *pc;
69
70 printf("\t%s %s ", programname, cmd->verb );
71
72 if (helptype == ADVANCED_HELP && cmd->adv_help)
73 for(pc = cmd->adv_help; *pc; pc++){
74 putchar(*pc);
75 if(*pc == '\n')
76 printf("\t\t");
77 }
78 else
79 for(pc = cmd->help; *pc; pc++){
80 putchar(*pc);
81 if(*pc == '\n')
82 printf("\t\t");
83 }
84
85 putchar('\n');
86}
87
88static void help(char *np)
89{
90 struct Command *cp;
91
92 printf("Usage:\n");
93 for( cp = commands; cp->verb; cp++ )
94 print_help(np, cp, BASIC_HELP);
95
96 printf("\n\t%s help|--help|-h\n\t\tShow the help.\n",np);
97 printf("\n\t%s <cmd> --help\n\t\tShow detailed help for a command or subset of commands.\n",np);
98 printf("\n%s\n", MMC_VERSION);
99}
100
101static int split_command(char *cmd, char ***commands)
102{
103 int c, l;
104 char *p, *s;
105
106 for( *commands = 0, l = c = 0, p = s = cmd ; ; p++, l++ ){
107 if ( *p && *p != ' ' )
108 continue;
109
110 /* c + 2 so that we have room for the null */
111 (*commands) = realloc( (*commands), sizeof(char *)*(c + 2));
112 (*commands)[c] = strndup(s, l);
113 c++;
114 l = 0;
115 s = p+1;
116 if( !*p ) break;
117 }
118
119 (*commands)[c] = 0;
120 return c;
121}
122
123/*
124 This function checks if the passed command is ambiguous
125*/
126static int check_ambiguity(struct Command *cmd, char **argv){
127 int i;
128 struct Command *cp;
129 /* check for ambiguity */
130 for( i = 0 ; i < cmd->ncmds ; i++ ){
131 int match;
132 for( match = 0, cp = commands; cp->verb; cp++ ){
133 int j, skip;
134 char *s1, *s2;
135
136 if( cp->ncmds < i )
137 continue;
138
139 for( skip = 0, j = 0 ; j < i ; j++ )
140 if( strcmp(cmd->cmds[j], cp->cmds[j])){
141 skip=1;
142 break;
143 }
144 if(skip)
145 continue;
146
147 if( !strcmp(cmd->cmds[i], cp->cmds[i]))
148 continue;
149 for(s2 = cp->cmds[i], s1 = argv[i+1];
150 *s1 == *s2 && *s1; s1++, s2++ ) ;
151 if( !*s1 )
152 match++;
153 }
154 if(match){
155 int j;
156 fprintf(stderr, "ERROR: in command '");
157 for( j = 0 ; j <= i ; j++ )
158 fprintf(stderr, "%s%s",j?" ":"", argv[j+1]);
159 fprintf(stderr, "', '%s' is ambiguous\n",argv[j]);
160 return -2;
161 }
162 }
163 return 0;
164}
165
166/*
167 * This function, compacts the program name and the command in the first
168 * element of the '*av' array
169 */
170static int prepare_args(int *ac, char ***av, char *prgname, struct Command *cmd ){
171
172 char **ret;
173 int i;
174 char *newname;
175
176 ret = (char **)malloc(sizeof(char*)*(*ac+1));
177 newname = (char*)malloc(strlen(prgname)+strlen(cmd->verb)+2);
178 if( !ret || !newname ){
179 free(ret);
180 free(newname);
181 return -1;
182 }
183
184 ret[0] = newname;
185 for(i=0; i < *ac ; i++ )
186 ret[i+1] = (*av)[i];
187
188 strcpy(newname, prgname);
189 strcat(newname, " ");
190 strcat(newname, cmd->verb);
191
192 (*ac)++;
193 *av = ret;
194
195 return 0;
196
197}
198
199/*
200 This function performs the following jobs:
201 - show the help if '--help' or 'help' or '-h' are passed
202 - verify that a command is not ambiguous, otherwise show which
203 part of the command is ambiguous
204 - if after a (even partial) command there is '--help' show detailed help
205 for all the matching commands
206 - if the command doesn't match show an error
207 - finally, if a command matches, they return which command matched and
208 the arguments
209
210 The function return 0 in case of help is requested; <0 in case
211 of uncorrect command; >0 in case of matching commands
212 argc, argv are the arg-counter and arg-vector (input)
213 *nargs_ is the number of the arguments after the command (output)
214 **cmd_ is the invoked command (output)
215 ***args_ are the arguments after the command
216
217*/
218static int parse_args(int argc, char **argv,
219 CommandFunction *func_,
220 int *nargs_, char **cmd_, char ***args_ )
221{
222 struct Command *cp;
223 struct Command *matchcmd=0;
224 char *prgname = get_prgname(argv[0]);
225 int i=0, helprequested=0;
226
227 if( argc < 2 || !strcmp(argv[1], "help") ||
228 !strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")){
229 help(prgname);
230 return 0;
231 }
232
233 for( cp = commands; cp->verb; cp++ )
234 if( !cp->ncmds)
235 cp->ncmds = split_command(cp->verb, &(cp->cmds));
236
237 for( cp = commands; cp->verb; cp++ ){
238 int match;
239
240 if( argc-1 < cp->ncmds )
241 continue;
242 for( match = 1, i = 0 ; i < cp->ncmds ; i++ ){
243 char *s1, *s2;
244 s1 = cp->cmds[i];
245 s2 = argv[i+1];
246
247 for(s2 = cp->cmds[i], s1 = argv[i+1];
248 *s1 == *s2 && *s1;
249 s1++, s2++ ) ;
250 if( *s1 ){
251 match=0;
252 break;
253 }
254 }
255
256 /* If you understand why this code works ...
257 you are a genious !! */
258 if(argc>i+1 && !strcmp(argv[i+1],"--help")){
259 if(!helprequested)
260 printf("Usage:\n");
261 print_help(prgname, cp, ADVANCED_HELP);
262 helprequested=1;
263 continue;
264 }
265
266 if(!match)
267 continue;
268
269 matchcmd = cp;
270 *nargs_ = argc-matchcmd->ncmds-1;
271 *cmd_ = matchcmd->verb;
272 *args_ = argv+matchcmd->ncmds+1;
273 *func_ = cp->func;
274
275 break;
276 }
277
278 if(helprequested){
279 printf("\n%s\n", MMC_VERSION);
280 return 0;
281 }
282
283 if(!matchcmd){
284 fprintf( stderr, "ERROR: unknown command '%s'\n",argv[1]);
285 help(prgname);
286 return -1;
287 }
288
289 if(check_ambiguity(matchcmd, argv))
290 return -2;
291
292 /* check the number of argument */
293 if (matchcmd->nargs < 0 && matchcmd->nargs < -*nargs_ ){
294 fprintf(stderr, "ERROR: '%s' requires minimum %d arg(s)\n",
295 matchcmd->verb, -matchcmd->nargs);
296 return -2;
297 }
298 if(matchcmd->nargs >= 0 && matchcmd->nargs != *nargs_ && matchcmd->nargs != 999){
299 fprintf(stderr, "ERROR: '%s' requires %d arg(s)\n",
300 matchcmd->verb, matchcmd->nargs);
301 return -2;
302 }
303
304 if (prepare_args( nargs_, args_, prgname, matchcmd )){
305 fprintf(stderr, "ERROR: not enough memory\\n");
306 return -20;
307 }
308
309
310 return 1;
311}
312int main(int ac, char **av )
313{
314 char *cmd=0, **args=0;
315 int nargs=0, r;
316 CommandFunction func=0;
317
318 r = parse_args(ac, av, &func, &nargs, &cmd, &args);
319 if( r <= 0 ){
320 /* error or no command to parse*/
321 exit(-r);
322 }
323
324 exit(func(nargs, args));
325}
326