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