Skip to content
Snippets Groups Projects
Commit e2a9f170 authored by pjwilliams's avatar pjwilliams
Browse files

Print valid arguments if user omits a required option argument in smartctl

git-svn-id: https://smartmontools.svn.sourceforge.net/svnroot/smartmontools/trunk@437 4ea69e1a-61f1-4043-bf83-b5c94c648137
parent 8068d43e
No related branches found
No related tags found
No related merge requests found
...@@ -42,7 +42,7 @@ ...@@ -42,7 +42,7 @@
#include "extern.h" #include "extern.h"
extern const char *CVSid1, *CVSid2, *CVSid3, *CVSid4; extern const char *CVSid1, *CVSid2, *CVSid3, *CVSid4;
const char* CVSid5="$Id: smartctl.c,v 1.43 2003/01/05 20:05:32 pjwilliams Exp $" const char* CVSid5="$Id: smartctl.c,v 1.44 2003/01/07 00:27:16 pjwilliams Exp $"
CVSID1 CVSID2 CVSID3 CVSID4 CVSID5 CVSID6; CVSID1 CVSID2 CVSID3 CVSID4 CVSID5 CVSID6;
// This is a block containing all the "control variables". We declare // This is a block containing all the "control variables". We declare
...@@ -198,46 +198,42 @@ void Usage (void){ ...@@ -198,46 +198,42 @@ void Usage (void){
#endif #endif
} }
/* Print an appropriate error message for option opt when given an invalid argument, optarg */ /* Returns a pointer to a static string containing a formatted list of the valid
void printbadargmessage(int opt, const char *optarg) { arguments to the option opt or NULL on failure. */
const char *getvalidarglist(char opt) {
static char *v_list = NULL;
char *s; char *s;
pout("=======> INVALID ARGUMENT TO -%c: %s <======= \n", opt, optarg);
pout("=======> VALID ARGUMENTS ARE: ");
switch (opt) { switch (opt) {
case 'q': case 'q':
pout("errorsonly, silent"); return "errorsonly, silent";
break;
case 'd': case 'd':
pout("ata, scsi"); return "ata, scsi";
break;
case 'T': case 'T':
pout("normal, conservative, permissive"); return "normal, conservative, permissive";
break;
case 'b': case 'b':
pout("warn, exit, ignore"); return "warn, exit, ignore";
break;
case 's': case 's':
case 'o': case 'o':
case 'S': case 'S':
pout("on, off"); return "on, off";
break;
case 'l': case 'l':
pout("error, selftest"); return "error, selftest";
break;
case 'v': case 'v':
if (!(s = create_vendor_attribute_arg_list())) { if (v_list)
pout("\nInsufficient memory to construct argument list\n"); return v_list;
break; if (!(s = create_vendor_attribute_arg_list()))
} return NULL;
pout("\"help\", %s", s); // Allocate space for "\"help\", " + s + terminating 0
v_list = (char *)malloc(9+strlen(s));
sprintf(v_list, "\"help\", %s", s);
free(s); free(s);
break; return v_list;
case 't': case 't':
pout("offline, short, long"); return "offline, short, long";
break; default:
return NULL;
} }
pout(" <======= \n\n");
} }
unsigned char tryata=0,tryscsi=0; unsigned char tryata=0,tryscsi=0;
...@@ -249,9 +245,11 @@ void ParseOpts (int argc, char** argv){ ...@@ -249,9 +245,11 @@ void ParseOpts (int argc, char** argv){
int captive; int captive;
extern char *optarg; extern char *optarg;
extern int optopt, optind, opterr; extern int optopt, optind, opterr;
// Please update getvalidarglist() if you edit shortopts
const char *shortopts = "h?Vq:d:T:b:s:o:S:HcAl:iav:t:CX"; const char *shortopts = "h?Vq:d:T:b:s:o:S:HcAl:iav:t:CX";
#ifdef HAVE_GETOPT_LONG #ifdef HAVE_GETOPT_LONG
char *arg; char *arg;
// Please update getvalidarglist() if you edit longopts
struct option longopts[] = { struct option longopts[] = {
{ "help", no_argument, 0, 'h' }, { "help", no_argument, 0, 'h' },
{ "usage", no_argument, 0, 'h' }, { "usage", no_argument, 0, 'h' },
...@@ -460,37 +458,43 @@ void ParseOpts (int argc, char** argv){ ...@@ -460,37 +458,43 @@ void ParseOpts (int argc, char** argv){
// Check whether the option is a long option that doesn't map to -h. // Check whether the option is a long option that doesn't map to -h.
if (arg[1] == '-' && optchar != 'h') { if (arg[1] == '-' && optchar != 'h') {
// Iff optopt holds a valid option then argument must be missing. // Iff optopt holds a valid option then argument must be missing.
if (optopt && (strchr(shortopts, optopt) != NULL)) if (optopt && (strchr(shortopts, optopt) != NULL)) {
pout("=======> ARGUMENT REQUIRED FOR OPTION: %s <=======\n", arg+2); pout("=======> ARGUMENT REQUIRED FOR OPTION: %s <=======\n", arg+2);
else pout("=======> VALID ARGUMENTS ARE: %s <=======\n", getvalidarglist(optopt));
} else
pout("=======> UNRECOGNIZED OPTION: %s <=======\n",arg+2); pout("=======> UNRECOGNIZED OPTION: %s <=======\n",arg+2);
pout("Use smartctl --help to get a usage summary\n\n"); pout("\nUse smartctl --help to get a usage summary\n\n");
exit(FAILCMD); exit(FAILCMD);
} }
#endif #endif
if (optopt) { if (optopt) {
// Iff optopt holds a valid option then argument must be missing. // Iff optopt holds a valid option then argument must be missing.
if (strchr(shortopts, optopt) != NULL) if (strchr(shortopts, optopt) != NULL) {
pout("=======> ARGUMENT REQUIRED FOR OPTION: %c <=======\n\n",optopt); pout("=======> ARGUMENT REQUIRED FOR OPTION: %c <=======\n", optopt);
else pout("=======> VALID ARGUMENTS ARE: %s <=======\n", getvalidarglist(optopt));
pout("=======> UNRECOGNIZED OPTION: %c <=======\n\n",optopt); } else
pout("Use smartctl -h to get a usage summary\n\n"); pout("=======> UNRECOGNIZED OPTION: %c <=======\n",optopt);
pout("\nUse smartctl -h to get a usage summary\n\n");
exit(FAILCMD); exit(FAILCMD);
} }
Usage(); Usage();
exit(0); exit(0);
} // closes switch statement to process command-line options } // closes switch statement to process command-line options
// At this point we have processed all command-line options. Now // Check to see if option had an unrecognized or incorrect argument.
// check to see if any of those options had unrecognized or
// incorrect arguments.
if (badarg) { if (badarg) {
printslogan(); printslogan();
printbadargmessage(optchar, optarg); // It would be nice to print the actual option name given by the user
pout("Use smartctl -h to get a usage summary\n\n"); // here, but we just print the short form. Please fix this if you know
// a clean way to do it.
pout("=======> INVALID ARGUMENT TO -%c: %s <======= \n", optchar, optarg);
pout("=======> VALID ARGUMENTS ARE: %s <=======\n", getvalidarglist(optchar));
pout("\nUse smartctl -h to get a usage summary\n\n");
exit(FAILCMD); exit(FAILCMD);
} }
} }
// At this point we have processed all command-line options.
// Do this here, so results are independent of argument order // Do this here, so results are independent of argument order
if (con->quietmode) if (con->quietmode)
con->veryquietmode=TRUE; con->veryquietmode=TRUE;
......
...@@ -42,7 +42,7 @@ ...@@ -42,7 +42,7 @@
#include "extern.h" #include "extern.h"
extern const char *CVSid1, *CVSid2, *CVSid3, *CVSid4; extern const char *CVSid1, *CVSid2, *CVSid3, *CVSid4;
const char* CVSid5="$Id: smartctl.cpp,v 1.43 2003/01/05 20:05:32 pjwilliams Exp $" const char* CVSid5="$Id: smartctl.cpp,v 1.44 2003/01/07 00:27:16 pjwilliams Exp $"
CVSID1 CVSID2 CVSID3 CVSID4 CVSID5 CVSID6; CVSID1 CVSID2 CVSID3 CVSID4 CVSID5 CVSID6;
// This is a block containing all the "control variables". We declare // This is a block containing all the "control variables". We declare
...@@ -198,46 +198,42 @@ void Usage (void){ ...@@ -198,46 +198,42 @@ void Usage (void){
#endif #endif
} }
/* Print an appropriate error message for option opt when given an invalid argument, optarg */ /* Returns a pointer to a static string containing a formatted list of the valid
void printbadargmessage(int opt, const char *optarg) { arguments to the option opt or NULL on failure. */
const char *getvalidarglist(char opt) {
static char *v_list = NULL;
char *s; char *s;
pout("=======> INVALID ARGUMENT TO -%c: %s <======= \n", opt, optarg);
pout("=======> VALID ARGUMENTS ARE: ");
switch (opt) { switch (opt) {
case 'q': case 'q':
pout("errorsonly, silent"); return "errorsonly, silent";
break;
case 'd': case 'd':
pout("ata, scsi"); return "ata, scsi";
break;
case 'T': case 'T':
pout("normal, conservative, permissive"); return "normal, conservative, permissive";
break;
case 'b': case 'b':
pout("warn, exit, ignore"); return "warn, exit, ignore";
break;
case 's': case 's':
case 'o': case 'o':
case 'S': case 'S':
pout("on, off"); return "on, off";
break;
case 'l': case 'l':
pout("error, selftest"); return "error, selftest";
break;
case 'v': case 'v':
if (!(s = create_vendor_attribute_arg_list())) { if (v_list)
pout("\nInsufficient memory to construct argument list\n"); return v_list;
break; if (!(s = create_vendor_attribute_arg_list()))
} return NULL;
pout("\"help\", %s", s); // Allocate space for "\"help\", " + s + terminating 0
v_list = (char *)malloc(9+strlen(s));
sprintf(v_list, "\"help\", %s", s);
free(s); free(s);
break; return v_list;
case 't': case 't':
pout("offline, short, long"); return "offline, short, long";
break; default:
return NULL;
} }
pout(" <======= \n\n");
} }
unsigned char tryata=0,tryscsi=0; unsigned char tryata=0,tryscsi=0;
...@@ -249,9 +245,11 @@ void ParseOpts (int argc, char** argv){ ...@@ -249,9 +245,11 @@ void ParseOpts (int argc, char** argv){
int captive; int captive;
extern char *optarg; extern char *optarg;
extern int optopt, optind, opterr; extern int optopt, optind, opterr;
// Please update getvalidarglist() if you edit shortopts
const char *shortopts = "h?Vq:d:T:b:s:o:S:HcAl:iav:t:CX"; const char *shortopts = "h?Vq:d:T:b:s:o:S:HcAl:iav:t:CX";
#ifdef HAVE_GETOPT_LONG #ifdef HAVE_GETOPT_LONG
char *arg; char *arg;
// Please update getvalidarglist() if you edit longopts
struct option longopts[] = { struct option longopts[] = {
{ "help", no_argument, 0, 'h' }, { "help", no_argument, 0, 'h' },
{ "usage", no_argument, 0, 'h' }, { "usage", no_argument, 0, 'h' },
...@@ -460,37 +458,43 @@ void ParseOpts (int argc, char** argv){ ...@@ -460,37 +458,43 @@ void ParseOpts (int argc, char** argv){
// Check whether the option is a long option that doesn't map to -h. // Check whether the option is a long option that doesn't map to -h.
if (arg[1] == '-' && optchar != 'h') { if (arg[1] == '-' && optchar != 'h') {
// Iff optopt holds a valid option then argument must be missing. // Iff optopt holds a valid option then argument must be missing.
if (optopt && (strchr(shortopts, optopt) != NULL)) if (optopt && (strchr(shortopts, optopt) != NULL)) {
pout("=======> ARGUMENT REQUIRED FOR OPTION: %s <=======\n", arg+2); pout("=======> ARGUMENT REQUIRED FOR OPTION: %s <=======\n", arg+2);
else pout("=======> VALID ARGUMENTS ARE: %s <=======\n", getvalidarglist(optopt));
} else
pout("=======> UNRECOGNIZED OPTION: %s <=======\n",arg+2); pout("=======> UNRECOGNIZED OPTION: %s <=======\n",arg+2);
pout("Use smartctl --help to get a usage summary\n\n"); pout("\nUse smartctl --help to get a usage summary\n\n");
exit(FAILCMD); exit(FAILCMD);
} }
#endif #endif
if (optopt) { if (optopt) {
// Iff optopt holds a valid option then argument must be missing. // Iff optopt holds a valid option then argument must be missing.
if (strchr(shortopts, optopt) != NULL) if (strchr(shortopts, optopt) != NULL) {
pout("=======> ARGUMENT REQUIRED FOR OPTION: %c <=======\n\n",optopt); pout("=======> ARGUMENT REQUIRED FOR OPTION: %c <=======\n", optopt);
else pout("=======> VALID ARGUMENTS ARE: %s <=======\n", getvalidarglist(optopt));
pout("=======> UNRECOGNIZED OPTION: %c <=======\n\n",optopt); } else
pout("Use smartctl -h to get a usage summary\n\n"); pout("=======> UNRECOGNIZED OPTION: %c <=======\n",optopt);
pout("\nUse smartctl -h to get a usage summary\n\n");
exit(FAILCMD); exit(FAILCMD);
} }
Usage(); Usage();
exit(0); exit(0);
} // closes switch statement to process command-line options } // closes switch statement to process command-line options
// At this point we have processed all command-line options. Now // Check to see if option had an unrecognized or incorrect argument.
// check to see if any of those options had unrecognized or
// incorrect arguments.
if (badarg) { if (badarg) {
printslogan(); printslogan();
printbadargmessage(optchar, optarg); // It would be nice to print the actual option name given by the user
pout("Use smartctl -h to get a usage summary\n\n"); // here, but we just print the short form. Please fix this if you know
// a clean way to do it.
pout("=======> INVALID ARGUMENT TO -%c: %s <======= \n", optchar, optarg);
pout("=======> VALID ARGUMENTS ARE: %s <=======\n", getvalidarglist(optchar));
pout("\nUse smartctl -h to get a usage summary\n\n");
exit(FAILCMD); exit(FAILCMD);
} }
} }
// At this point we have processed all command-line options.
// Do this here, so results are independent of argument order // Do this here, so results are independent of argument order
if (con->quietmode) if (con->quietmode)
con->veryquietmode=TRUE; con->veryquietmode=TRUE;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment