180d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli/* 280d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli * This program is free software; you can redistribute it and/or 380d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli * modify it under the terms of the GNU General Public 480d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli * License v2 as published by the Free Software Foundation. 580d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli * 680d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli * This program is distributed in the hope that it will be useful, 780d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli * but WITHOUT ANY WARRANTY; without even the implied warranty of 880d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 980d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli * General Public License for more details. 1080d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli * 1180d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli * You should have received a copy of the GNU General Public 1280d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli * License along with this program; if not, write to the 1380d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli * Free Software Foundation, Inc., 59 Temple Place - Suite 330, 1480d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli * Boston, MA 021110-1307, USA. 1580d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli * 1680d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli * (This code is based on btrfs-progs/btrfs.c.) 1780d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli */ 1880d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli 1980d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli#define _GNU_SOURCE 2080d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli#include <stdio.h> 2180d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli#include <stdlib.h> 2280d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli#include <string.h> 2380d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli 2445541d55fa50e385894d22960f2526b6c7f68d52Chris Ball#include "mmc_cmds.h" 2545541d55fa50e385894d22960f2526b6c7f68d52Chris Ball 2680d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli#define MMC_VERSION "0.1" 2780d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli 2880d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli#define BASIC_HELP 0 2980d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli#define ADVANCED_HELP 1 3080d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli 3180d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncellitypedef int (*CommandFunction)(int argc, char **argv); 3280d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli 3380d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncellistruct Command { 3480d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli CommandFunction func; /* function which implements the command */ 3580d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli int nargs; /* if == 999, any number of arguments 3680d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli if >= 0, number of arguments, 3780d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli if < 0, _minimum_ number of arguments */ 3880d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli char *verb; /* verb */ 3980d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli char *help; /* help lines; from the 2nd line onward they 4080d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli are automatically indented */ 4180d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli char *adv_help; /* advanced help message; from the 2nd line 4280d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli onward they are automatically indented */ 4380d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli 4480d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli /* the following fields are run-time filled by the program */ 4580d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli char **cmds; /* array of subcommands */ 4680d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli int ncmds; /* number of subcommand */ 4780d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli}; 4880d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli 4980d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncellistatic struct Command commands[] = { 5080d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli /* 5180d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli * avoid short commands different for the case only 5280d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli */ 5345541d55fa50e385894d22960f2526b6c7f68d52Chris Ball { do_read_extcsd, -1, 5445541d55fa50e385894d22960f2526b6c7f68d52Chris Ball "extcsd read", "<device>\n" 5545541d55fa50e385894d22960f2526b6c7f68d52Chris Ball "Print extcsd data from <device>.", 56358f4f5dba8c5336011d61d163bf3e0900147672Aurimas Liutikas NULL, NULL, 0 5745541d55fa50e385894d22960f2526b6c7f68d52Chris Ball }, 58b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball { do_writeprotect_get, -1, 59b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball "writeprotect get", "<device>\n" 60b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball "Determine the eMMC writeprotect status of <device>.", 61358f4f5dba8c5336011d61d163bf3e0900147672Aurimas Liutikas NULL, NULL, 0 62b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball }, 63b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball { do_writeprotect_set, -1, 64b9c7a17fce190f085bb4eb6e6535a22e2c69de68Chris Ball "writeprotect set", "<device>\n" 656599780fda6ef3caf5927deb3036f1b646605008Chris Ball "Set the eMMC writeprotect status of <device>.\nThis sets the eMMC to be write-protected until next boot.", 66358f4f5dba8c5336011d61d163bf3e0900147672Aurimas Liutikas NULL, NULL, 0 6745541d55fa50e385894d22960f2526b6c7f68d52Chris Ball }, 68b7e2599c67408c38e57e91d2426c077a4541dc8cSaugata Das { do_disable_512B_emulation, -1, 69b7e2599c67408c38e57e91d2426c077a4541dc8cSaugata Das "disable 512B emulation", "<device>\n" 706599780fda6ef3caf5927deb3036f1b646605008Chris Ball "Set the eMMC data sector size to 4KB by disabling emulation on\n<device>.", 71358f4f5dba8c5336011d61d163bf3e0900147672Aurimas Liutikas NULL, NULL, 0 72b7e2599c67408c38e57e91d2426c077a4541dc8cSaugata Das }, 737891236d62ccd201054324b5298dd9529c6a764fBalaji T K { do_create_gp_partition, -6, 747891236d62ccd201054324b5298dd9529c6a764fBalaji T K "gp create", "<-y|-n> " "<length KiB> " "<partition> " "<enh_attr> " "<ext_attr> " "<device>\n" 757891236d62ccd201054324b5298dd9529c6a764fBalaji T K "create general purpose partition for the <device>.\nDry-run only unless -y is passed.\nNOTE! This is a one-time programmable (unreversible) change.\nTo set enhanced attribute to general partition being created set\n <enh_attr> to 1 else set it to 0.\nTo set extended attribute to general partition\n set <ext_attr> to 1,2 else set it to 0", 76358f4f5dba8c5336011d61d163bf3e0900147672Aurimas Liutikas NULL, NULL, 0 777891236d62ccd201054324b5298dd9529c6a764fBalaji T K }, 78d91d3698c6464a83b7c301eb84da109f9f94b54cBen Gardiner { do_enh_area_set, -4, 79d91d3698c6464a83b7c301eb84da109f9f94b54cBen Gardiner "enh_area set", "<-y|-n> " "<start KiB> " "<length KiB> " "<device>\n" 80d91d3698c6464a83b7c301eb84da109f9f94b54cBen Gardiner "Enable the enhanced user area for the <device>.\nDry-run only unless -y is passed.\nNOTE! This is a one-time programmable (unreversible) change.", 81358f4f5dba8c5336011d61d163bf3e0900147672Aurimas Liutikas NULL, NULL, 0 82d91d3698c6464a83b7c301eb84da109f9f94b54cBen Gardiner }, 83196d0d29588867bed50cd28b8f03cbbb5e0e6608Ben Gardiner { do_write_reliability_set, -2, 84196d0d29588867bed50cd28b8f03cbbb5e0e6608Ben Gardiner "write_reliability set", "<-y|-n> " "<partition> " "<device>\n" 85196d0d29588867bed50cd28b8f03cbbb5e0e6608Ben Gardiner "Enable write reliability per partition for the <device>.\nDry-run only unless -y is passed.\nNOTE! This is a one-time programmable (unreversible) change.", 86358f4f5dba8c5336011d61d163bf3e0900147672Aurimas Liutikas NULL, NULL, 0 87196d0d29588867bed50cd28b8f03cbbb5e0e6608Ben Gardiner }, 8827c357db04350b75b0fceaae8bfb9ce99c50866bBen Gardiner { do_status_get, -1, 8927c357db04350b75b0fceaae8bfb9ce99c50866bBen Gardiner "status get", "<device>\n" 9027c357db04350b75b0fceaae8bfb9ce99c50866bBen Gardiner "Print the response to STATUS_SEND (CMD13).", 91358f4f5dba8c5336011d61d163bf3e0900147672Aurimas Liutikas NULL, NULL, 0 9227c357db04350b75b0fceaae8bfb9ce99c50866bBen Gardiner }, 937bd1320b2cb38f040ab5cf017d17e283496690bfGiuseppe CAVALLARO { do_write_boot_en, -3, 947bd1320b2cb38f040ab5cf017d17e283496690bfGiuseppe CAVALLARO "bootpart enable", "<boot_partition> " "<send_ack> " "<device>\n" 957bd1320b2cb38f040ab5cf017d17e283496690bfGiuseppe CAVALLARO "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.", 96358f4f5dba8c5336011d61d163bf3e0900147672Aurimas Liutikas NULL, NULL, 0 977bd1320b2cb38f040ab5cf017d17e283496690bfGiuseppe CAVALLARO }, 9864c2de8b1476c42ef9e9729b7ca0e436b5d90170Al Cooper { do_boot_bus_conditions_set, -4, 9964c2de8b1476c42ef9e9729b7ca0e436b5d90170Al Cooper "bootbus set", "<boot_mode> " "<reset_boot_bus_conditions> " "<boot_bus_width> " "<device>\n" 10064c2de8b1476c42ef9e9729b7ca0e436b5d90170Al Cooper "Set Boot Bus Conditions.\n" 10164c2de8b1476c42ef9e9729b7ca0e436b5d90170Al Cooper "<boot_mode> must be \"single_backward|single_hs|dual\"\n" 10264c2de8b1476c42ef9e9729b7ca0e436b5d90170Al Cooper "<reset_boot_bus_conditions> must be \"x1|retain\"\n" 10364c2de8b1476c42ef9e9729b7ca0e436b5d90170Al Cooper "<boot_bus_width> must be \"x1|x4|x8\"", 104358f4f5dba8c5336011d61d163bf3e0900147672Aurimas Liutikas NULL, NULL, 0 10564c2de8b1476c42ef9e9729b7ca0e436b5d90170Al Cooper }, 1068649651b743a5d7c290ea0f8058794f8d127736eJaehoon Chung { do_write_bkops_en, -1, 1078649651b743a5d7c290ea0f8058794f8d127736eJaehoon Chung "bkops enable", "<device>\n" 1088649651b743a5d7c290ea0f8058794f8d127736eJaehoon Chung "Enable the eMMC BKOPS feature on <device>.\nNOTE! This is a one-time programmable (unreversible) change.", 109358f4f5dba8c5336011d61d163bf3e0900147672Aurimas Liutikas NULL, NULL, 0 1108649651b743a5d7c290ea0f8058794f8d127736eJaehoon Chung }, 111f74dfe23cd00894aa9f235374468e05acb793e17Chris Ball { do_hwreset_en, -1, 112f74dfe23cd00894aa9f235374468e05acb793e17Chris Ball "hwreset enable", "<device>\n" 113f74dfe23cd00894aa9f235374468e05acb793e17Chris Ball "Permanently enable the eMMC H/W Reset feature on <device>.\nNOTE! This is a one-time programmable (unreversible) change.", 114358f4f5dba8c5336011d61d163bf3e0900147672Aurimas Liutikas NULL, NULL, 0 115f74dfe23cd00894aa9f235374468e05acb793e17Chris Ball }, 116f74dfe23cd00894aa9f235374468e05acb793e17Chris Ball { do_hwreset_dis, -1, 117f74dfe23cd00894aa9f235374468e05acb793e17Chris Ball "hwreset disable", "<device>\n" 118f74dfe23cd00894aa9f235374468e05acb793e17Chris Ball "Permanently disable the eMMC H/W Reset feature on <device>.\nNOTE! This is a one-time programmable (unreversible) change.", 119358f4f5dba8c5336011d61d163bf3e0900147672Aurimas Liutikas NULL, NULL, 0 120f74dfe23cd00894aa9f235374468e05acb793e17Chris Ball }, 12121bb473fc58366b872efe31e1da7831cad4b92faYaniv Gardi { do_sanitize, -1, 12221bb473fc58366b872efe31e1da7831cad4b92faYaniv Gardi "sanitize", "<device>\n" 12321bb473fc58366b872efe31e1da7831cad4b92faYaniv Gardi "Send Sanitize command to the <device>.\nThis will delete the unmapped memory region of the device.", 124358f4f5dba8c5336011d61d163bf3e0900147672Aurimas Liutikas NULL, NULL, 0 12521bb473fc58366b872efe31e1da7831cad4b92faYaniv Gardi }, 126c6cb053ec59e7667e2140c320e2b7d5a90592a20Roman Peniaev { do_rpmb_write_key, -1, 127c6cb053ec59e7667e2140c320e2b7d5a90592a20Roman Peniaev "rpmb write-key", "<rpmb device> <key file>\n" 128f4eb241519f8d500ce6068a70d2389be39ac5189Chris Ball "Program authentication key which is 32 bytes length and stored\n" 129f4eb241519f8d500ce6068a70d2389be39ac5189Chris Ball "in the specified file. Also you can specify '-' instead of\n" 130f4eb241519f8d500ce6068a70d2389be39ac5189Chris Ball "key file path to read the key from stdin.\n" 131f4eb241519f8d500ce6068a70d2389be39ac5189Chris Ball "NOTE! This is a one-time programmable (unreversible) change.\n" 132c6cb053ec59e7667e2140c320e2b7d5a90592a20Roman Peniaev "Example:\n" 133f4eb241519f8d500ce6068a70d2389be39ac5189Chris Ball " $ echo -n AAAABBBBCCCCDDDDEEEEFFFFGGGGHHHH | \\\n" 134f4eb241519f8d500ce6068a70d2389be39ac5189Chris Ball " mmc rpmb write-key /dev/mmcblk0rpmb -", 135358f4f5dba8c5336011d61d163bf3e0900147672Aurimas Liutikas NULL, NULL, 0 136c6cb053ec59e7667e2140c320e2b7d5a90592a20Roman Peniaev }, 137c6cb053ec59e7667e2140c320e2b7d5a90592a20Roman Peniaev { do_rpmb_read_counter, -1, 138c6cb053ec59e7667e2140c320e2b7d5a90592a20Roman Peniaev "rpmb read-counter", "<rpmb device>\n" 139c6cb053ec59e7667e2140c320e2b7d5a90592a20Roman Peniaev "Counter value for the <rpmb device> will be read to stdout.", 140358f4f5dba8c5336011d61d163bf3e0900147672Aurimas Liutikas NULL, NULL, 0 141c6cb053ec59e7667e2140c320e2b7d5a90592a20Roman Peniaev }, 142c6cb053ec59e7667e2140c320e2b7d5a90592a20Roman Peniaev { do_rpmb_read_block, -1, 143c6cb053ec59e7667e2140c320e2b7d5a90592a20Roman Peniaev "rpmb read-block", "<rpmb device> <address> <blocks count> <output file> [key file]\n" 144f4eb241519f8d500ce6068a70d2389be39ac5189Chris Ball "Blocks of 256 bytes will be read from <rpmb device> to output\n" 145f4eb241519f8d500ce6068a70d2389be39ac5189Chris Ball "file or stdout if '-' is specified. If key is specified - read\n" 146f4eb241519f8d500ce6068a70d2389be39ac5189Chris Ball "data will be verified. Instead of regular path you can specify\n" 147f4eb241519f8d500ce6068a70d2389be39ac5189Chris Ball "'-' to read key from stdin.\n" 148c6cb053ec59e7667e2140c320e2b7d5a90592a20Roman Peniaev "Example:\n" 149f4eb241519f8d500ce6068a70d2389be39ac5189Chris Ball " $ echo -n AAAABBBBCCCCDDDDEEEEFFFFGGGGHHHH | \\\n" 150f4eb241519f8d500ce6068a70d2389be39ac5189Chris Ball " mmc rpmb read-block /dev/mmcblk0rpmb 0x02 2 /tmp/block -\n" 151c6cb053ec59e7667e2140c320e2b7d5a90592a20Roman Peniaev "or read two blocks without verification\n" 152c6cb053ec59e7667e2140c320e2b7d5a90592a20Roman Peniaev " $ mmc rpmb read-block /dev/mmcblk0rpmb 0x02 2 /tmp/block", 153358f4f5dba8c5336011d61d163bf3e0900147672Aurimas Liutikas NULL, NULL, 0 154c6cb053ec59e7667e2140c320e2b7d5a90592a20Roman Peniaev }, 155c6cb053ec59e7667e2140c320e2b7d5a90592a20Roman Peniaev { do_rpmb_write_block, -1, 156c6cb053ec59e7667e2140c320e2b7d5a90592a20Roman Peniaev "rpmb write-block", "<rpmb device> <address> <256 byte data file> <key file>\n" 157f4eb241519f8d500ce6068a70d2389be39ac5189Chris Ball "Block of 256 bytes will be written from data file to\n" 158f4eb241519f8d500ce6068a70d2389be39ac5189Chris Ball "<rpmb device>. Also you can specify '-' instead of key\n" 159f4eb241519f8d500ce6068a70d2389be39ac5189Chris Ball "file path or data file to read the data from stdin.\n" 160c6cb053ec59e7667e2140c320e2b7d5a90592a20Roman Peniaev "Example:\n" 161f4eb241519f8d500ce6068a70d2389be39ac5189Chris Ball " $ (awk 'BEGIN {while (c++<256) printf \"a\"}' | \\\n" 162f4eb241519f8d500ce6068a70d2389be39ac5189Chris Ball " echo -n AAAABBBBCCCCDDDDEEEEFFFFGGGGHHHH) | \\\n" 163c6cb053ec59e7667e2140c320e2b7d5a90592a20Roman Peniaev " mmc rpmb write-block /dev/mmcblk0rpmb 0x02 - -", 164358f4f5dba8c5336011d61d163bf3e0900147672Aurimas Liutikas NULL, NULL, 0 165c6cb053ec59e7667e2140c320e2b7d5a90592a20Roman Peniaev }, 166d0b46442b50794217e53b2455c1344c548d9d088Al Cooper { do_cache_en, -1, 167d0b46442b50794217e53b2455c1344c548d9d088Al Cooper "cache enable", "<device>\n" 168d0b46442b50794217e53b2455c1344c548d9d088Al Cooper "Enable the eMMC cache feature on <device>.\n" 169d0b46442b50794217e53b2455c1344c548d9d088Al Cooper "NOTE! The cache is an optional feature on devices >= eMMC4.5.", 170358f4f5dba8c5336011d61d163bf3e0900147672Aurimas Liutikas NULL, NULL, 0 171d0b46442b50794217e53b2455c1344c548d9d088Al Cooper }, 172d0b46442b50794217e53b2455c1344c548d9d088Al Cooper { do_cache_dis, -1, 173d0b46442b50794217e53b2455c1344c548d9d088Al Cooper "cache disable", "<device>\n" 174d0b46442b50794217e53b2455c1344c548d9d088Al Cooper "Disable the eMMC cache feature on <device>.\n" 175d0b46442b50794217e53b2455c1344c548d9d088Al Cooper "NOTE! The cache is an optional feature on devices >= eMMC4.5.", 176358f4f5dba8c5336011d61d163bf3e0900147672Aurimas Liutikas NULL, NULL, 0 177d0b46442b50794217e53b2455c1344c548d9d088Al Cooper }, 178358f4f5dba8c5336011d61d163bf3e0900147672Aurimas Liutikas { 0, 0, 0, 0, 0, 0, 0 } 17980d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli}; 18080d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli 18180d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncellistatic char *get_prgname(char *programname) 18280d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli{ 18380d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli char *np; 18480d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli np = strrchr(programname,'/'); 18580d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli if(!np) 18680d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli np = programname; 18780d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli else 18880d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli np++; 18980d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli 19080d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli return np; 19180d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli} 19280d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli 19380d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncellistatic void print_help(char *programname, struct Command *cmd, int helptype) 19480d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli{ 19580d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli char *pc; 19680d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli 19780d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli printf("\t%s %s ", programname, cmd->verb ); 19880d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli 19980d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli if (helptype == ADVANCED_HELP && cmd->adv_help) 20080d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli for(pc = cmd->adv_help; *pc; pc++){ 20180d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli putchar(*pc); 20280d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli if(*pc == '\n') 20380d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli printf("\t\t"); 20480d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli } 20580d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli else 20680d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli for(pc = cmd->help; *pc; pc++){ 20780d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli putchar(*pc); 20880d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli if(*pc == '\n') 20980d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli printf("\t\t"); 21080d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli } 21180d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli 21280d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli putchar('\n'); 21380d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli} 21480d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli 21580d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncellistatic void help(char *np) 21680d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli{ 21780d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli struct Command *cp; 21880d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli 21980d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli printf("Usage:\n"); 22080d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli for( cp = commands; cp->verb; cp++ ) 22180d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli print_help(np, cp, BASIC_HELP); 22280d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli 22380d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli printf("\n\t%s help|--help|-h\n\t\tShow the help.\n",np); 22480d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli printf("\n\t%s <cmd> --help\n\t\tShow detailed help for a command or subset of commands.\n",np); 22580d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli printf("\n%s\n", MMC_VERSION); 22680d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli} 22780d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli 22880d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncellistatic int split_command(char *cmd, char ***commands) 22980d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli{ 23080d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli int c, l; 23180d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli char *p, *s; 23280d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli 23380d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli for( *commands = 0, l = c = 0, p = s = cmd ; ; p++, l++ ){ 23480d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli if ( *p && *p != ' ' ) 23580d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli continue; 23680d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli 23780d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli /* c + 2 so that we have room for the null */ 23880d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli (*commands) = realloc( (*commands), sizeof(char *)*(c + 2)); 23980d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli (*commands)[c] = strndup(s, l); 24080d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli c++; 24180d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli l = 0; 24280d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli s = p+1; 24380d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli if( !*p ) break; 24480d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli } 24580d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli 24680d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli (*commands)[c] = 0; 24780d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli return c; 24880d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli} 24980d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli 25080d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli/* 25180d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli This function checks if the passed command is ambiguous 25280d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli*/ 25380d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncellistatic int check_ambiguity(struct Command *cmd, char **argv){ 25480d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli int i; 25580d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli struct Command *cp; 25680d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli /* check for ambiguity */ 25780d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli for( i = 0 ; i < cmd->ncmds ; i++ ){ 25880d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli int match; 25980d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli for( match = 0, cp = commands; cp->verb; cp++ ){ 26080d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli int j, skip; 26180d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli char *s1, *s2; 26280d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli 26380d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli if( cp->ncmds < i ) 26480d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli continue; 26580d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli 26680d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli for( skip = 0, j = 0 ; j < i ; j++ ) 26780d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli if( strcmp(cmd->cmds[j], cp->cmds[j])){ 26880d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli skip=1; 26980d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli break; 27080d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli } 27180d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli if(skip) 27280d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli continue; 27380d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli 27480d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli if( !strcmp(cmd->cmds[i], cp->cmds[i])) 27580d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli continue; 27680d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli for(s2 = cp->cmds[i], s1 = argv[i+1]; 27780d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli *s1 == *s2 && *s1; s1++, s2++ ) ; 27880d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli if( !*s1 ) 27980d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli match++; 28080d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli } 28180d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli if(match){ 28280d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli int j; 28380d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli fprintf(stderr, "ERROR: in command '"); 28480d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli for( j = 0 ; j <= i ; j++ ) 28580d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli fprintf(stderr, "%s%s",j?" ":"", argv[j+1]); 28680d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli fprintf(stderr, "', '%s' is ambiguous\n",argv[j]); 28780d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli return -2; 28880d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli } 28980d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli } 29080d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli return 0; 29180d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli} 29280d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli 29380d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli/* 29480d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli * This function, compacts the program name and the command in the first 29580d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli * element of the '*av' array 29680d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli */ 29780d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncellistatic int prepare_args(int *ac, char ***av, char *prgname, struct Command *cmd ){ 29880d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli 29980d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli char **ret; 30080d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli int i; 30180d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli char *newname; 30280d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli 30380d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli ret = (char **)malloc(sizeof(char*)*(*ac+1)); 30480d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli newname = (char*)malloc(strlen(prgname)+strlen(cmd->verb)+2); 30580d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli if( !ret || !newname ){ 30680d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli free(ret); 30780d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli free(newname); 30880d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli return -1; 30980d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli } 31080d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli 31180d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli ret[0] = newname; 31280d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli for(i=0; i < *ac ; i++ ) 31380d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli ret[i+1] = (*av)[i]; 31480d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli 31580d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli strcpy(newname, prgname); 31680d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli strcat(newname, " "); 31780d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli strcat(newname, cmd->verb); 31880d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli 31980d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli (*ac)++; 32080d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli *av = ret; 32180d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli 32280d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli return 0; 32380d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli 32480d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli} 32580d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli 32680d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli/* 32780d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli This function performs the following jobs: 32880d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli - show the help if '--help' or 'help' or '-h' are passed 32980d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli - verify that a command is not ambiguous, otherwise show which 33080d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli part of the command is ambiguous 33180d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli - if after a (even partial) command there is '--help' show detailed help 33280d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli for all the matching commands 33380d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli - if the command doesn't match show an error 33480d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli - finally, if a command matches, they return which command matched and 33580d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli the arguments 33680d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli 33780d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli The function return 0 in case of help is requested; <0 in case 33880d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli of uncorrect command; >0 in case of matching commands 33980d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli argc, argv are the arg-counter and arg-vector (input) 34080d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli *nargs_ is the number of the arguments after the command (output) 34180d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli **cmd_ is the invoked command (output) 34280d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli ***args_ are the arguments after the command 34380d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli 34480d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli*/ 34580d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncellistatic int parse_args(int argc, char **argv, 34680d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli CommandFunction *func_, 34780d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli int *nargs_, char **cmd_, char ***args_ ) 34880d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli{ 34980d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli struct Command *cp; 35080d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli struct Command *matchcmd=0; 35180d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli char *prgname = get_prgname(argv[0]); 35280d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli int i=0, helprequested=0; 35380d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli 35480d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli if( argc < 2 || !strcmp(argv[1], "help") || 35580d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli !strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")){ 35680d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli help(prgname); 35780d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli return 0; 35880d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli } 35980d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli 36080d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli for( cp = commands; cp->verb; cp++ ) 36180d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli if( !cp->ncmds) 36280d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli cp->ncmds = split_command(cp->verb, &(cp->cmds)); 36380d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli 36480d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli for( cp = commands; cp->verb; cp++ ){ 36580d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli int match; 36680d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli 36780d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli if( argc-1 < cp->ncmds ) 36880d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli continue; 36980d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli for( match = 1, i = 0 ; i < cp->ncmds ; i++ ){ 37080d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli char *s1, *s2; 37180d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli s1 = cp->cmds[i]; 37280d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli s2 = argv[i+1]; 37380d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli 37480d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli for(s2 = cp->cmds[i], s1 = argv[i+1]; 37580d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli *s1 == *s2 && *s1; 37680d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli s1++, s2++ ) ; 37780d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli if( *s1 ){ 37880d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli match=0; 37980d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli break; 38080d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli } 38180d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli } 38280d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli 38380d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli /* If you understand why this code works ... 38480d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli you are a genious !! */ 38580d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli if(argc>i+1 && !strcmp(argv[i+1],"--help")){ 38680d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli if(!helprequested) 38780d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli printf("Usage:\n"); 38880d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli print_help(prgname, cp, ADVANCED_HELP); 38980d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli helprequested=1; 39080d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli continue; 39180d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli } 39280d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli 39380d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli if(!match) 39480d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli continue; 39580d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli 39680d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli matchcmd = cp; 39780d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli *nargs_ = argc-matchcmd->ncmds-1; 39880d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli *cmd_ = matchcmd->verb; 39980d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli *args_ = argv+matchcmd->ncmds+1; 40080d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli *func_ = cp->func; 40180d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli 40280d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli break; 40380d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli } 40480d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli 40580d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli if(helprequested){ 40680d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli printf("\n%s\n", MMC_VERSION); 40780d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli return 0; 40880d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli } 40980d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli 41080d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli if(!matchcmd){ 41180d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli fprintf( stderr, "ERROR: unknown command '%s'\n",argv[1]); 41280d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli help(prgname); 41380d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli return -1; 41480d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli } 41580d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli 41680d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli if(check_ambiguity(matchcmd, argv)) 41780d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli return -2; 41880d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli 41980d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli /* check the number of argument */ 42080d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli if (matchcmd->nargs < 0 && matchcmd->nargs < -*nargs_ ){ 42180d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli fprintf(stderr, "ERROR: '%s' requires minimum %d arg(s)\n", 42280d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli matchcmd->verb, -matchcmd->nargs); 42380d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli return -2; 42480d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli } 42580d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli if(matchcmd->nargs >= 0 && matchcmd->nargs != *nargs_ && matchcmd->nargs != 999){ 42680d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli fprintf(stderr, "ERROR: '%s' requires %d arg(s)\n", 42780d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli matchcmd->verb, matchcmd->nargs); 42880d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli return -2; 42980d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli } 43080d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli 43180d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli if (prepare_args( nargs_, args_, prgname, matchcmd )){ 43280d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli fprintf(stderr, "ERROR: not enough memory\\n"); 43380d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli return -20; 43480d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli } 43580d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli 43680d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli 43780d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli return 1; 43880d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli} 43980d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelliint main(int ac, char **av ) 44080d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli{ 44180d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli char *cmd=0, **args=0; 44280d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli int nargs=0, r; 44380d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli CommandFunction func=0; 44480d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli 44580d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli r = parse_args(ac, av, &func, &nargs, &cmd, &args); 44680d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli if( r <= 0 ){ 44780d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli /* error or no command to parse*/ 44880d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli exit(-r); 44980d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli } 45080d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli 45180d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli exit(func(nargs, args)); 45280d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli} 45380d2660fa95615f3bc77a9b258414f2c533175cGoffredo Baroncelli 454