Goffredo Baroncelli | 80d2660 | 2012-02-12 11:43:14 -0500 | [diff] [blame] | 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 | * (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 Ball | 45541d5 | 2012-02-12 11:49:53 -0500 | [diff] [blame] | 24 | #include "mmc_cmds.h" |
| 25 | |
Goffredo Baroncelli | 80d2660 | 2012-02-12 11:43:14 -0500 | [diff] [blame] | 26 | #define MMC_VERSION "0.1" |
| 27 | |
| 28 | #define BASIC_HELP 0 |
| 29 | #define ADVANCED_HELP 1 |
| 30 | |
| 31 | typedef int (*CommandFunction)(int argc, char **argv); |
| 32 | |
| 33 | struct 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 */ |
Gwendal Grignou | eb1cd01 | 2015-01-08 15:34:55 -0800 | [diff] [blame] | 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 */ |
Goffredo Baroncelli | 80d2660 | 2012-02-12 11:43:14 -0500 | [diff] [blame] | 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 | |
| 49 | static struct Command commands[] = { |
| 50 | /* |
| 51 | * avoid short commands different for the case only |
| 52 | */ |
Chris Ball | 45541d5 | 2012-02-12 11:49:53 -0500 | [diff] [blame] | 53 | { do_read_extcsd, -1, |
| 54 | "extcsd read", "<device>\n" |
| 55 | "Print extcsd data from <device>.", |
| 56 | NULL |
| 57 | }, |
Nick Sanders | 9d57aa7 | 2014-03-05 21:38:54 -0800 | [diff] [blame] | 58 | { do_dump_extcsd, -1, |
| 59 | "extcsd dump", "<device>\n" |
| 60 | "Print raw extcsd data from <device>.", |
| 61 | NULL |
| 62 | }, |
Chris Ball | b9c7a17 | 2012-02-20 12:34:25 -0500 | [diff] [blame] | 63 | { do_writeprotect_get, -1, |
| 64 | "writeprotect get", "<device>\n" |
| 65 | "Determine the eMMC writeprotect status of <device>.", |
| 66 | NULL |
| 67 | }, |
| 68 | { do_writeprotect_set, -1, |
| 69 | "writeprotect set", "<device>\n" |
Chris Ball | 6599780 | 2012-09-21 18:19:25 +0800 | [diff] [blame] | 70 | "Set the eMMC writeprotect status of <device>.\nThis sets the eMMC to be write-protected until next boot.", |
Chris Ball | 45541d5 | 2012-02-12 11:49:53 -0500 | [diff] [blame] | 71 | NULL |
| 72 | }, |
Saugata Das | b7e2599 | 2012-05-17 09:26:34 -0400 | [diff] [blame] | 73 | { do_disable_512B_emulation, -1, |
| 74 | "disable 512B emulation", "<device>\n" |
Chris Ball | 6599780 | 2012-09-21 18:19:25 +0800 | [diff] [blame] | 75 | "Set the eMMC data sector size to 4KB by disabling emulation on\n<device>.", |
Saugata Das | b7e2599 | 2012-05-17 09:26:34 -0400 | [diff] [blame] | 76 | NULL |
| 77 | }, |
Ben Gardiner | d91d369 | 2013-05-30 17:12:51 -0400 | [diff] [blame] | 78 | { do_enh_area_set, -4, |
| 79 | "enh_area set", "<-y|-n> " "<start KiB> " "<length KiB> " "<device>\n" |
| 80 | "Enable the enhanced user area for the <device>.\nDry-run only unless -y is passed.\nNOTE! This is a one-time programmable (unreversible) change.", |
| 81 | NULL |
| 82 | }, |
Ben Gardiner | 196d0d2 | 2013-09-19 11:14:29 -0400 | [diff] [blame] | 83 | { do_write_reliability_set, -2, |
| 84 | "write_reliability set", "<-y|-n> " "<partition> " "<device>\n" |
| 85 | "Enable write reliability per partition for the <device>.\nDry-run only unless -y is passed.\nNOTE! This is a one-time programmable (unreversible) change.", |
| 86 | NULL |
| 87 | }, |
Ben Gardiner | 27c357d | 2013-05-30 17:12:47 -0400 | [diff] [blame] | 88 | { do_status_get, -1, |
| 89 | "status get", "<device>\n" |
| 90 | "Print the response to STATUS_SEND (CMD13).", |
| 91 | NULL |
| 92 | }, |
Giuseppe CAVALLARO | 7bd1320 | 2012-04-19 10:58:37 +0200 | [diff] [blame] | 93 | { do_write_boot_en, -3, |
| 94 | "bootpart enable", "<boot_partition> " "<send_ack> " "<device>\n" |
| 95 | "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.", |
| 96 | NULL |
| 97 | }, |
Jaehoon Chung | 8649651 | 2012-09-21 10:08:05 +0000 | [diff] [blame] | 98 | { do_write_bkops_en, -1, |
| 99 | "bkops enable", "<device>\n" |
| 100 | "Enable the eMMC BKOPS feature on <device>.\nNOTE! This is a one-time programmable (unreversible) change.", |
| 101 | NULL |
| 102 | }, |
Chris Ball | f74dfe2 | 2012-10-19 16:49:55 -0400 | [diff] [blame] | 103 | { do_hwreset_en, -1, |
| 104 | "hwreset enable", "<device>\n" |
| 105 | "Permanently enable the eMMC H/W Reset feature on <device>.\nNOTE! This is a one-time programmable (unreversible) change.", |
| 106 | NULL |
| 107 | }, |
| 108 | { do_hwreset_dis, -1, |
| 109 | "hwreset disable", "<device>\n" |
| 110 | "Permanently disable the eMMC H/W Reset feature on <device>.\nNOTE! This is a one-time programmable (unreversible) change.", |
| 111 | NULL |
| 112 | }, |
Yaniv Gardi | 21bb473 | 2013-05-26 13:25:33 -0400 | [diff] [blame] | 113 | { do_sanitize, -1, |
| 114 | "sanitize", "<device>\n" |
| 115 | "Send Sanitize command to the <device>.\nThis will delete the unmapped memory region of the device.", |
| 116 | NULL |
| 117 | }, |
Gwendal Grignou | 771984c | 2014-07-01 12:46:18 -0700 | [diff] [blame] | 118 | { do_emmc50_ffu, -2, |
Gwendal Grignou | 0da2c51 | 2015-01-08 15:36:03 -0800 | [diff] [blame] | 119 | "ffu", "[-k hack_type[:hack_value]] <image name> <device>\n" |
| 120 | "run eMMC 5.0 Field firmware update.\n" |
| 121 | "Device specific hacks can be specificied.", |
Gwendal Grignou | 771984c | 2014-07-01 12:46:18 -0700 | [diff] [blame] | 122 | NULL |
| 123 | }, |
Roman Peniaev | 023cc7c | 2014-08-12 23:25:45 +0900 | [diff] [blame] | 124 | { do_rpmb_write_key, -1, |
| 125 | "rpmb write-key", "<rpmb device> <key file>\n" |
Chris Ball | d186ab5 | 2014-08-12 10:44:52 -0400 | [diff] [blame^] | 126 | "Program authentication key which is 32 bytes length and stored\n" |
| 127 | "in the specified file. Also you can specify '-' instead of\n" |
| 128 | "key file path to read the key from stdin.\n" |
| 129 | "NOTE! This is a one-time programmable (unreversible) change.\n" |
Roman Peniaev | 023cc7c | 2014-08-12 23:25:45 +0900 | [diff] [blame] | 130 | "Example:\n" |
Chris Ball | d186ab5 | 2014-08-12 10:44:52 -0400 | [diff] [blame^] | 131 | " $ echo -n AAAABBBBCCCCDDDDEEEEFFFFGGGGHHHH | \\\n" |
| 132 | " mmc rpmb write-key /dev/mmcblk0rpmb -", |
Roman Peniaev | 023cc7c | 2014-08-12 23:25:45 +0900 | [diff] [blame] | 133 | NULL |
| 134 | }, |
| 135 | { do_rpmb_read_counter, -1, |
| 136 | "rpmb read-counter", "<rpmb device>\n" |
| 137 | "Counter value for the <rpmb device> will be read to stdout.", |
| 138 | NULL |
| 139 | }, |
| 140 | { do_rpmb_read_block, -1, |
| 141 | "rpmb read-block", "<rpmb device> <address> <blocks count> <output file> [key file]\n" |
Chris Ball | d186ab5 | 2014-08-12 10:44:52 -0400 | [diff] [blame^] | 142 | "Blocks of 256 bytes will be read from <rpmb device> to output\n" |
| 143 | "file or stdout if '-' is specified. If key is specified - read\n" |
| 144 | "data will be verified. Instead of regular path you can specify\n" |
| 145 | "'-' to read key from stdin.\n" |
Roman Peniaev | 023cc7c | 2014-08-12 23:25:45 +0900 | [diff] [blame] | 146 | "Example:\n" |
Chris Ball | d186ab5 | 2014-08-12 10:44:52 -0400 | [diff] [blame^] | 147 | " $ echo -n AAAABBBBCCCCDDDDEEEEFFFFGGGGHHHH | \\\n" |
| 148 | " mmc rpmb read-block /dev/mmcblk0rpmb 0x02 2 /tmp/block -\n" |
Roman Peniaev | 023cc7c | 2014-08-12 23:25:45 +0900 | [diff] [blame] | 149 | "or read two blocks without verification\n" |
| 150 | " $ mmc rpmb read-block /dev/mmcblk0rpmb 0x02 2 /tmp/block", |
| 151 | NULL |
| 152 | }, |
| 153 | { do_rpmb_write_block, -1, |
| 154 | "rpmb write-block", "<rpmb device> <address> <256 byte data file> <key file>\n" |
Chris Ball | d186ab5 | 2014-08-12 10:44:52 -0400 | [diff] [blame^] | 155 | "Block of 256 bytes will be written from data file to\n" |
| 156 | "<rpmb device>. Also you can specify '-' instead of key\n" |
| 157 | "file path or data file to read the data from stdin.\n" |
Roman Peniaev | 023cc7c | 2014-08-12 23:25:45 +0900 | [diff] [blame] | 158 | "Example:\n" |
Chris Ball | d186ab5 | 2014-08-12 10:44:52 -0400 | [diff] [blame^] | 159 | " $ (awk 'BEGIN {while (c++<256) printf \"a\"}' | \\\n" |
| 160 | " echo -n AAAABBBBCCCCDDDDEEEEFFFFGGGGHHHH) | \\\n" |
Roman Peniaev | 023cc7c | 2014-08-12 23:25:45 +0900 | [diff] [blame] | 161 | " mmc rpmb write-block /dev/mmcblk0rpmb 0x02 - -", |
| 162 | NULL |
| 163 | }, |
Goffredo Baroncelli | 80d2660 | 2012-02-12 11:43:14 -0500 | [diff] [blame] | 164 | { 0, 0, 0, 0 } |
| 165 | }; |
| 166 | |
| 167 | static char *get_prgname(char *programname) |
| 168 | { |
| 169 | char *np; |
| 170 | np = strrchr(programname,'/'); |
Gwendal Grignou | eb1cd01 | 2015-01-08 15:34:55 -0800 | [diff] [blame] | 171 | if (!np) |
Goffredo Baroncelli | 80d2660 | 2012-02-12 11:43:14 -0500 | [diff] [blame] | 172 | np = programname; |
| 173 | else |
| 174 | np++; |
| 175 | |
| 176 | return np; |
| 177 | } |
| 178 | |
| 179 | static void print_help(char *programname, struct Command *cmd, int helptype) |
| 180 | { |
| 181 | char *pc; |
| 182 | |
Gwendal Grignou | eb1cd01 | 2015-01-08 15:34:55 -0800 | [diff] [blame] | 183 | printf("\t%s %s ", programname, cmd->verb); |
Goffredo Baroncelli | 80d2660 | 2012-02-12 11:43:14 -0500 | [diff] [blame] | 184 | |
| 185 | if (helptype == ADVANCED_HELP && cmd->adv_help) |
Gwendal Grignou | eb1cd01 | 2015-01-08 15:34:55 -0800 | [diff] [blame] | 186 | for (pc = cmd->adv_help; *pc; pc++) { |
Goffredo Baroncelli | 80d2660 | 2012-02-12 11:43:14 -0500 | [diff] [blame] | 187 | putchar(*pc); |
Gwendal Grignou | eb1cd01 | 2015-01-08 15:34:55 -0800 | [diff] [blame] | 188 | if (*pc == '\n') |
Goffredo Baroncelli | 80d2660 | 2012-02-12 11:43:14 -0500 | [diff] [blame] | 189 | printf("\t\t"); |
| 190 | } |
| 191 | else |
Gwendal Grignou | eb1cd01 | 2015-01-08 15:34:55 -0800 | [diff] [blame] | 192 | for (pc = cmd->help; *pc; pc++) { |
Goffredo Baroncelli | 80d2660 | 2012-02-12 11:43:14 -0500 | [diff] [blame] | 193 | putchar(*pc); |
Gwendal Grignou | eb1cd01 | 2015-01-08 15:34:55 -0800 | [diff] [blame] | 194 | if (*pc == '\n') |
Goffredo Baroncelli | 80d2660 | 2012-02-12 11:43:14 -0500 | [diff] [blame] | 195 | printf("\t\t"); |
| 196 | } |
| 197 | |
| 198 | putchar('\n'); |
| 199 | } |
| 200 | |
| 201 | static void help(char *np) |
| 202 | { |
| 203 | struct Command *cp; |
| 204 | |
| 205 | printf("Usage:\n"); |
Gwendal Grignou | eb1cd01 | 2015-01-08 15:34:55 -0800 | [diff] [blame] | 206 | for (cp = commands; cp->verb; cp++) |
Goffredo Baroncelli | 80d2660 | 2012-02-12 11:43:14 -0500 | [diff] [blame] | 207 | print_help(np, cp, BASIC_HELP); |
| 208 | |
| 209 | printf("\n\t%s help|--help|-h\n\t\tShow the help.\n",np); |
| 210 | printf("\n\t%s <cmd> --help\n\t\tShow detailed help for a command or subset of commands.\n",np); |
| 211 | printf("\n%s\n", MMC_VERSION); |
| 212 | } |
| 213 | |
| 214 | static int split_command(char *cmd, char ***commands) |
| 215 | { |
| 216 | int c, l; |
| 217 | char *p, *s; |
| 218 | |
Gwendal Grignou | eb1cd01 | 2015-01-08 15:34:55 -0800 | [diff] [blame] | 219 | for (*commands = 0, l = c = 0, p = s = cmd ; ; p++, l++) { |
| 220 | if (*p && *p != ' ') |
Goffredo Baroncelli | 80d2660 | 2012-02-12 11:43:14 -0500 | [diff] [blame] | 221 | continue; |
| 222 | |
| 223 | /* c + 2 so that we have room for the null */ |
Gwendal Grignou | eb1cd01 | 2015-01-08 15:34:55 -0800 | [diff] [blame] | 224 | (*commands) = realloc((*commands), sizeof(char *)*(c + 2)); |
Goffredo Baroncelli | 80d2660 | 2012-02-12 11:43:14 -0500 | [diff] [blame] | 225 | (*commands)[c] = strndup(s, l); |
| 226 | c++; |
| 227 | l = 0; |
| 228 | s = p+1; |
Gwendal Grignou | eb1cd01 | 2015-01-08 15:34:55 -0800 | [diff] [blame] | 229 | if (!*p) break; |
Goffredo Baroncelli | 80d2660 | 2012-02-12 11:43:14 -0500 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | (*commands)[c] = 0; |
| 233 | return c; |
| 234 | } |
| 235 | |
| 236 | /* |
| 237 | This function checks if the passed command is ambiguous |
| 238 | */ |
Gwendal Grignou | eb1cd01 | 2015-01-08 15:34:55 -0800 | [diff] [blame] | 239 | static int check_ambiguity(struct Command *cmd, char **argv) { |
Goffredo Baroncelli | 80d2660 | 2012-02-12 11:43:14 -0500 | [diff] [blame] | 240 | int i; |
| 241 | struct Command *cp; |
| 242 | /* check for ambiguity */ |
Gwendal Grignou | eb1cd01 | 2015-01-08 15:34:55 -0800 | [diff] [blame] | 243 | for (i = 0 ; i < cmd->ncmds ; i++) { |
Goffredo Baroncelli | 80d2660 | 2012-02-12 11:43:14 -0500 | [diff] [blame] | 244 | int match; |
Gwendal Grignou | eb1cd01 | 2015-01-08 15:34:55 -0800 | [diff] [blame] | 245 | for (match = 0, cp = commands; cp->verb; cp++) { |
Goffredo Baroncelli | 80d2660 | 2012-02-12 11:43:14 -0500 | [diff] [blame] | 246 | int j, skip; |
| 247 | char *s1, *s2; |
| 248 | |
Gwendal Grignou | eb1cd01 | 2015-01-08 15:34:55 -0800 | [diff] [blame] | 249 | if (cp->ncmds < i) |
Goffredo Baroncelli | 80d2660 | 2012-02-12 11:43:14 -0500 | [diff] [blame] | 250 | continue; |
| 251 | |
Gwendal Grignou | eb1cd01 | 2015-01-08 15:34:55 -0800 | [diff] [blame] | 252 | for (skip = 0, j = 0 ; j < i ; j++) |
| 253 | if (strcmp(cmd->cmds[j], cp->cmds[j])) { |
Goffredo Baroncelli | 80d2660 | 2012-02-12 11:43:14 -0500 | [diff] [blame] | 254 | skip=1; |
| 255 | break; |
| 256 | } |
Gwendal Grignou | eb1cd01 | 2015-01-08 15:34:55 -0800 | [diff] [blame] | 257 | if (skip) |
Goffredo Baroncelli | 80d2660 | 2012-02-12 11:43:14 -0500 | [diff] [blame] | 258 | continue; |
| 259 | |
Gwendal Grignou | eb1cd01 | 2015-01-08 15:34:55 -0800 | [diff] [blame] | 260 | if (!strcmp(cmd->cmds[i], cp->cmds[i])) |
Goffredo Baroncelli | 80d2660 | 2012-02-12 11:43:14 -0500 | [diff] [blame] | 261 | continue; |
Gwendal Grignou | eb1cd01 | 2015-01-08 15:34:55 -0800 | [diff] [blame] | 262 | for (s2 = cp->cmds[i], s1 = argv[i+1]; |
| 263 | *s1 == *s2 && *s1; s1++, s2++) ; |
| 264 | if (!*s1) |
Goffredo Baroncelli | 80d2660 | 2012-02-12 11:43:14 -0500 | [diff] [blame] | 265 | match++; |
| 266 | } |
Gwendal Grignou | eb1cd01 | 2015-01-08 15:34:55 -0800 | [diff] [blame] | 267 | if (match) { |
Goffredo Baroncelli | 80d2660 | 2012-02-12 11:43:14 -0500 | [diff] [blame] | 268 | int j; |
| 269 | fprintf(stderr, "ERROR: in command '"); |
Gwendal Grignou | eb1cd01 | 2015-01-08 15:34:55 -0800 | [diff] [blame] | 270 | for (j = 0 ; j <= i ; j++) |
Goffredo Baroncelli | 80d2660 | 2012-02-12 11:43:14 -0500 | [diff] [blame] | 271 | fprintf(stderr, "%s%s",j?" ":"", argv[j+1]); |
| 272 | fprintf(stderr, "', '%s' is ambiguous\n",argv[j]); |
| 273 | return -2; |
| 274 | } |
| 275 | } |
| 276 | return 0; |
| 277 | } |
| 278 | |
| 279 | /* |
| 280 | * This function, compacts the program name and the command in the first |
| 281 | * element of the '*av' array |
| 282 | */ |
Gwendal Grignou | eb1cd01 | 2015-01-08 15:34:55 -0800 | [diff] [blame] | 283 | static int prepare_args(int *ac, char ***av, char *prgname, |
| 284 | struct Command *cmd) { |
Goffredo Baroncelli | 80d2660 | 2012-02-12 11:43:14 -0500 | [diff] [blame] | 285 | |
| 286 | char **ret; |
| 287 | int i; |
| 288 | char *newname; |
| 289 | |
| 290 | ret = (char **)malloc(sizeof(char*)*(*ac+1)); |
| 291 | newname = (char*)malloc(strlen(prgname)+strlen(cmd->verb)+2); |
Gwendal Grignou | eb1cd01 | 2015-01-08 15:34:55 -0800 | [diff] [blame] | 292 | if (!ret || !newname) { |
Goffredo Baroncelli | 80d2660 | 2012-02-12 11:43:14 -0500 | [diff] [blame] | 293 | free(ret); |
| 294 | free(newname); |
| 295 | return -1; |
| 296 | } |
| 297 | |
| 298 | ret[0] = newname; |
Gwendal Grignou | eb1cd01 | 2015-01-08 15:34:55 -0800 | [diff] [blame] | 299 | for (i=0; i < *ac ; i++) |
Goffredo Baroncelli | 80d2660 | 2012-02-12 11:43:14 -0500 | [diff] [blame] | 300 | ret[i+1] = (*av)[i]; |
| 301 | |
| 302 | strcpy(newname, prgname); |
| 303 | strcat(newname, " "); |
| 304 | strcat(newname, cmd->verb); |
| 305 | |
| 306 | (*ac)++; |
| 307 | *av = ret; |
| 308 | |
| 309 | return 0; |
| 310 | |
| 311 | } |
| 312 | |
| 313 | /* |
| 314 | This function performs the following jobs: |
| 315 | - show the help if '--help' or 'help' or '-h' are passed |
| 316 | - verify that a command is not ambiguous, otherwise show which |
| 317 | part of the command is ambiguous |
| 318 | - if after a (even partial) command there is '--help' show detailed help |
| 319 | for all the matching commands |
| 320 | - if the command doesn't match show an error |
| 321 | - finally, if a command matches, they return which command matched and |
| 322 | the arguments |
| 323 | |
| 324 | The function return 0 in case of help is requested; <0 in case |
| 325 | of uncorrect command; >0 in case of matching commands |
| 326 | argc, argv are the arg-counter and arg-vector (input) |
| 327 | *nargs_ is the number of the arguments after the command (output) |
| 328 | **cmd_ is the invoked command (output) |
| 329 | ***args_ are the arguments after the command |
| 330 | |
| 331 | */ |
| 332 | static int parse_args(int argc, char **argv, |
| 333 | CommandFunction *func_, |
Gwendal Grignou | eb1cd01 | 2015-01-08 15:34:55 -0800 | [diff] [blame] | 334 | int *nargs_, char **cmd_, char ***args_) |
Goffredo Baroncelli | 80d2660 | 2012-02-12 11:43:14 -0500 | [diff] [blame] | 335 | { |
| 336 | struct Command *cp; |
| 337 | struct Command *matchcmd=0; |
| 338 | char *prgname = get_prgname(argv[0]); |
| 339 | int i=0, helprequested=0; |
| 340 | |
Gwendal Grignou | eb1cd01 | 2015-01-08 15:34:55 -0800 | [diff] [blame] | 341 | if (argc < 2 || !strcmp(argv[1], "help") || |
| 342 | !strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) { |
Goffredo Baroncelli | 80d2660 | 2012-02-12 11:43:14 -0500 | [diff] [blame] | 343 | help(prgname); |
| 344 | return 0; |
| 345 | } |
| 346 | |
Gwendal Grignou | eb1cd01 | 2015-01-08 15:34:55 -0800 | [diff] [blame] | 347 | for (cp = commands; cp->verb; cp++) |
| 348 | if (!cp->ncmds) |
Goffredo Baroncelli | 80d2660 | 2012-02-12 11:43:14 -0500 | [diff] [blame] | 349 | cp->ncmds = split_command(cp->verb, &(cp->cmds)); |
| 350 | |
Gwendal Grignou | eb1cd01 | 2015-01-08 15:34:55 -0800 | [diff] [blame] | 351 | for (cp = commands; cp->verb; cp++) { |
Goffredo Baroncelli | 80d2660 | 2012-02-12 11:43:14 -0500 | [diff] [blame] | 352 | int match; |
| 353 | |
Gwendal Grignou | eb1cd01 | 2015-01-08 15:34:55 -0800 | [diff] [blame] | 354 | if (argc-1 < cp->ncmds) |
Goffredo Baroncelli | 80d2660 | 2012-02-12 11:43:14 -0500 | [diff] [blame] | 355 | continue; |
Gwendal Grignou | eb1cd01 | 2015-01-08 15:34:55 -0800 | [diff] [blame] | 356 | for (match = 1, i = 0 ; i < cp->ncmds ; i++) { |
Goffredo Baroncelli | 80d2660 | 2012-02-12 11:43:14 -0500 | [diff] [blame] | 357 | char *s1, *s2; |
| 358 | s1 = cp->cmds[i]; |
| 359 | s2 = argv[i+1]; |
| 360 | |
Gwendal Grignou | eb1cd01 | 2015-01-08 15:34:55 -0800 | [diff] [blame] | 361 | for (s2 = cp->cmds[i], s1 = argv[i+1]; |
Goffredo Baroncelli | 80d2660 | 2012-02-12 11:43:14 -0500 | [diff] [blame] | 362 | *s1 == *s2 && *s1; |
Gwendal Grignou | eb1cd01 | 2015-01-08 15:34:55 -0800 | [diff] [blame] | 363 | s1++, s2++) ; |
| 364 | if (*s1) { |
Goffredo Baroncelli | 80d2660 | 2012-02-12 11:43:14 -0500 | [diff] [blame] | 365 | match=0; |
| 366 | break; |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | /* If you understand why this code works ... |
| 371 | you are a genious !! */ |
Gwendal Grignou | eb1cd01 | 2015-01-08 15:34:55 -0800 | [diff] [blame] | 372 | if (argc>i+1 && !strcmp(argv[i+1],"--help")) { |
| 373 | if (!helprequested) |
Goffredo Baroncelli | 80d2660 | 2012-02-12 11:43:14 -0500 | [diff] [blame] | 374 | printf("Usage:\n"); |
| 375 | print_help(prgname, cp, ADVANCED_HELP); |
| 376 | helprequested=1; |
| 377 | continue; |
| 378 | } |
| 379 | |
Gwendal Grignou | eb1cd01 | 2015-01-08 15:34:55 -0800 | [diff] [blame] | 380 | if (!match) |
Goffredo Baroncelli | 80d2660 | 2012-02-12 11:43:14 -0500 | [diff] [blame] | 381 | continue; |
| 382 | |
| 383 | matchcmd = cp; |
| 384 | *nargs_ = argc-matchcmd->ncmds-1; |
| 385 | *cmd_ = matchcmd->verb; |
| 386 | *args_ = argv+matchcmd->ncmds+1; |
| 387 | *func_ = cp->func; |
| 388 | |
| 389 | break; |
| 390 | } |
| 391 | |
Gwendal Grignou | eb1cd01 | 2015-01-08 15:34:55 -0800 | [diff] [blame] | 392 | if (helprequested) { |
Goffredo Baroncelli | 80d2660 | 2012-02-12 11:43:14 -0500 | [diff] [blame] | 393 | printf("\n%s\n", MMC_VERSION); |
| 394 | return 0; |
| 395 | } |
| 396 | |
Gwendal Grignou | eb1cd01 | 2015-01-08 15:34:55 -0800 | [diff] [blame] | 397 | if (!matchcmd) { |
| 398 | fprintf(stderr, "ERROR: unknown command '%s'\n",argv[1]); |
Goffredo Baroncelli | 80d2660 | 2012-02-12 11:43:14 -0500 | [diff] [blame] | 399 | help(prgname); |
| 400 | return -1; |
| 401 | } |
| 402 | |
Gwendal Grignou | eb1cd01 | 2015-01-08 15:34:55 -0800 | [diff] [blame] | 403 | if (check_ambiguity(matchcmd, argv)) |
Goffredo Baroncelli | 80d2660 | 2012-02-12 11:43:14 -0500 | [diff] [blame] | 404 | return -2; |
| 405 | |
| 406 | /* check the number of argument */ |
Gwendal Grignou | eb1cd01 | 2015-01-08 15:34:55 -0800 | [diff] [blame] | 407 | if (matchcmd->nargs < 0 && matchcmd->nargs < -*nargs_) { |
Goffredo Baroncelli | 80d2660 | 2012-02-12 11:43:14 -0500 | [diff] [blame] | 408 | fprintf(stderr, "ERROR: '%s' requires minimum %d arg(s)\n", |
| 409 | matchcmd->verb, -matchcmd->nargs); |
| 410 | return -2; |
| 411 | } |
Gwendal Grignou | eb1cd01 | 2015-01-08 15:34:55 -0800 | [diff] [blame] | 412 | if (matchcmd->nargs >= 0 && matchcmd->nargs != *nargs_ && matchcmd->nargs != 999) { |
Goffredo Baroncelli | 80d2660 | 2012-02-12 11:43:14 -0500 | [diff] [blame] | 413 | fprintf(stderr, "ERROR: '%s' requires %d arg(s)\n", |
| 414 | matchcmd->verb, matchcmd->nargs); |
| 415 | return -2; |
| 416 | } |
Gwendal Grignou | eb1cd01 | 2015-01-08 15:34:55 -0800 | [diff] [blame] | 417 | if (prepare_args(nargs_, args_, prgname, matchcmd)) { |
| 418 | fprintf(stderr, "ERROR: not enough memory\\n"); |
Goffredo Baroncelli | 80d2660 | 2012-02-12 11:43:14 -0500 | [diff] [blame] | 419 | return -20; |
Gwendal Grignou | eb1cd01 | 2015-01-08 15:34:55 -0800 | [diff] [blame] | 420 | } |
Goffredo Baroncelli | 80d2660 | 2012-02-12 11:43:14 -0500 | [diff] [blame] | 421 | |
| 422 | |
| 423 | return 1; |
| 424 | } |
Gwendal Grignou | eb1cd01 | 2015-01-08 15:34:55 -0800 | [diff] [blame] | 425 | int main(int ac, char **av) |
Goffredo Baroncelli | 80d2660 | 2012-02-12 11:43:14 -0500 | [diff] [blame] | 426 | { |
| 427 | char *cmd=0, **args=0; |
| 428 | int nargs=0, r; |
| 429 | CommandFunction func=0; |
| 430 | |
| 431 | r = parse_args(ac, av, &func, &nargs, &cmd, &args); |
Gwendal Grignou | eb1cd01 | 2015-01-08 15:34:55 -0800 | [diff] [blame] | 432 | if (r <= 0) { |
Goffredo Baroncelli | 80d2660 | 2012-02-12 11:43:14 -0500 | [diff] [blame] | 433 | /* error or no command to parse*/ |
| 434 | exit(-r); |
| 435 | } |
| 436 | |
| 437 | exit(func(nargs, args)); |
| 438 | } |
| 439 | |