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