cvs commit: ALFS/nALFS/src options.c options.h

kpfleming at linuxfromscratch.org kpfleming at linuxfromscratch.org
Mon Nov 3 19:45:27 PST 2003


kpfleming    03/11/03 20:45:27

  Modified:    nALFS/src options.c options.h
  Log:
  add validate_boolean_input and convert_to_boolean functions
  support all common boolean input (upper/lowercase, yes/no/y/n/true/false)
  
  Revision  Changes    Path
  1.10      +55 -12    ALFS/nALFS/src/options.c
  
  Index: options.c
  ===================================================================
  RCS file: /home/cvsroot/ALFS/nALFS/src/options.c,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- options.c	4 Nov 2003 03:27:30 -0000	1.9
  +++ options.c	4 Nov 2003 03:45:27 -0000	1.10
  @@ -187,8 +187,8 @@
   
   	if (!valid)
   		fprintf(stderr,
  -			"Option %s outside valid range, must be between"
  -			" %d and %d\n", option->name,
  +			"Option \"%s\" outside valid range, must be between"
  +			" %d and %d.\n", option->name,
   			option->val.num.min_value,
   			option->val.num.max_value);
   
  @@ -207,16 +207,16 @@
   				break;
   			case 's':
   				if (string_count++) {
  -					fprintf(stderr, "Option %s contains"
  +					fprintf(stderr, "Option \"%s\" contains"
   						" more than one string"
  -						" substitution\n",
  +						" substitution.\n",
   						option->name);
   					return 0;
   				}
   				break;
   			default:
  -				fprintf(stderr, "Option %s contains an"
  -					" invalid substitution specifier\n",
  +				fprintf(stderr, "Option \"%s\" contains an"
  +					" invalid substitution specifier.\n",
   					option->name);
   				return 0;
   			}
  @@ -226,6 +226,52 @@
   	return 1;
   }
   
  +static int convert_to_boolean(const char *input, BOOL *val)
  +{
  +	char *p;
  +	int status = 1;
  +	char *tmp = xstrdup(input);
  +
  +	p = tmp;
  +	while (*p) {
  +		*p = tolower(*p);
  +		++p;
  +	}
  +
  +	if (!strcmp(tmp, "yes"))
  +		*val = 1;
  +	else if (!strcmp(tmp, "y"))
  +		*val = 1;
  +	else if (!strcmp(tmp, "true"))
  +		*val = 1;
  +	else if (!strcmp(tmp, "no"))
  +		*val = 0;
  +	else if (!strcmp(tmp, "n"))
  +		*val = 0;
  +	else if (!strcmp(tmp, "false"))
  +		*val = 0;
  +	else
  +		status = 0;
  +
  +	xfree(tmp);
  +	return status;
  +}
  +
  +static int validate_boolean_input(const struct option_s *option,
  +				  const char *input)
  +{
  +	BOOL val;
  +
  +	if (!convert_to_boolean(input, &val)) {
  +		fprintf(stderr, "Option \"%s\" does not contain a valid"
  +			" boolean value,\nchoices are yes/y/true and"
  +			" no/n/false.\n", option->name);
  +		return 0;
  +	}
  +	else
  +		return 1;
  +}
  +
   set_opt_e set_yet_unknown_option(const char *opt, const char *val)
   {
   	int i;
  @@ -240,12 +286,9 @@
   
   		switch (options[i]->type) {
   		case O_BOOL:
  -			if (strcmp(val, BOOL_TRUE_VALUE) == 0) {
  -				options[i]->val.bool.value = 1;
  -				return OPTION_SET;
  -				
  -			} else if (strcmp(val, BOOL_FALSE_VALUE) == 0) {
  -				options[i]->val.bool.value = 0;
  +			if (validate_boolean_input(options[i], val)) {
  +				convert_to_boolean(val,
  +						   &options[i]->val.bool.value);
   				return OPTION_SET;
   			}
   			
  
  
  
  1.10      +0 -3      ALFS/nALFS/src/options.h
  
  Index: options.h
  ===================================================================
  RCS file: /home/cvsroot/ALFS/nALFS/src/options.h,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- options.h	4 Nov 2003 03:27:30 -0000	1.9
  +++ options.h	4 Nov 2003 03:45:27 -0000	1.10
  @@ -178,9 +178,6 @@
    * Setting option from RC file parser.
    */
   
  -#define BOOL_TRUE_VALUE "yes"
  -#define BOOL_FALSE_VALUE "no"
  -
   typedef enum set_opt_e {
   	OPTION_SET,
   	OPTION_UNKNOWN,
  
  
  



More information about the alfs-log mailing list