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