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

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


kpfleming    03/11/03 20:27:30

  Modified:    nALFS    bootstrap.option-list
               nALFS/src options.c options.h
  Log:
  implement support for per-option validation functions
  remove O_COMMAND option type as it is no longer necessary
  add validate_command function for validating all command options
  add validate_number_minmax function for validating numeric options with lower/upper bounds
  remove trailing semicolons from option macros so usage of them looks more normal
  add error output to validate_command/validate_number_minmax tell user what is wrong with their input
  
  Revision  Changes    Path
  1.2       +0 -1      ALFS/nALFS/bootstrap.option-list
  
  Index: bootstrap.option-list
  ===================================================================
  RCS file: /home/cvsroot/ALFS/nALFS/bootstrap.option-list,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- bootstrap.option-list	3 Nov 2003 21:45:03 -0000	1.1
  +++ bootstrap.option-list	4 Nov 2003 03:27:29 -0000	1.2
  @@ -30,7 +30,6 @@
   grep -e "^STRING_OPTION" src/options.h | sed "s/STRING_OPTION(/\&real_opt_/" | sed "s/,.*$/,/"
   grep -e "^BOOL_OPTION" src/options.h | sed "s/BOOL_OPTION(/\&real_opt_/" | sed "s/,.*$/,/"
   grep -e "^NUMBER_OPTION" src/options.h | sed "s/NUMBER_OPTION(/\&real_opt_/" | sed "s/,.*$/,/"
  -grep -e "^COMMAND_OPTION" src/options.h | sed "s/COMMAND_OPTION(/\&real_opt_/" | sed "s/,.*$/,/"
   
   cat <<"EOF"
   NULL
  
  
  
  1.9       +71 -73    ALFS/nALFS/src/options.c
  
  Index: options.c
  ===================================================================
  RCS file: /home/cvsroot/ALFS/nALFS/src/options.c,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- options.c	4 Nov 2003 02:35:02 -0000	1.8
  +++ options.c	4 Nov 2003 03:27:30 -0000	1.9
  @@ -37,34 +37,31 @@
      be allocated storage in this module.
   */
   
  -#define STRING_OPTION(opt_name, opt_def_value) \
  +#define STRING_OPTION(opt_name, opt_def_value, opt_other...) \
   		static struct option_s real_opt_##opt_name = { \
   			.name = #opt_name, \
   			.type = O_STRING, \
  -			.val.str = { .def_value = opt_def_value } \
  +			.val.str = { .def_value = opt_def_value, \
  +				     ## opt_other } \
   		}; \
  -		STRING * const opt_##opt_name = &real_opt_##opt_name .val.str.value;
  -#define BOOL_OPTION(opt_name, opt_def_value) \
  +		STRING * const opt_##opt_name = &real_opt_##opt_name .val.str.value
  +#define BOOL_OPTION(opt_name, opt_def_value, opt_other...) \
   		static struct option_s real_opt_##opt_name = { \
   			.name = #opt_name, \
   			.type = O_BOOL, \
  -			.val.bool = { .def_value = opt_def_value } \
  +			.val.bool = { .def_value = opt_def_value, \
  +				      ## opt_other } \
   		}; \
  -		BOOL * const opt_##opt_name = &real_opt_##opt_name .val.bool.value;
  -#define NUMBER_OPTION(opt_name, opt_def_value) \
  +		BOOL * const opt_##opt_name = &real_opt_##opt_name .val.bool.value
  +#define NUMBER_OPTION(opt_name, opt_def_value, opt_other...) \
   		static struct option_s real_opt_##opt_name = { \
   			.name = #opt_name, \
   			.type = O_NUMBER, \
  -			.val.num = { .def_value= opt_def_value } \
  +			.val.num = { .def_value = opt_def_value, \
  +				     ## opt_other } \
   		}; \
  -		NUMBER * const opt_##opt_name = &real_opt_##opt_name .val.num.value;
  -#define COMMAND_OPTION(opt_name, opt_def_value) \
  -		static struct option_s real_opt_##opt_name = { \
  -			.name = #opt_name, \
  -			.type = O_COMMAND, \
  -			.val.str = { .def_value = opt_def_value } \
  -		}; \
  -		STRING * const opt_##opt_name = &real_opt_##opt_name .val.str.value;
  +		NUMBER * const opt_##opt_name = &real_opt_##opt_name .val.num.value
  +
   
   #include "options.h"
   #include "option-list.h"
  @@ -112,7 +109,6 @@
   		break;
   		
   	case O_STRING:
  -	case O_COMMAND:
   		if (option->val.str.value)
   			xfree(option->val.str.value);
   		option->val.str.value = xstrdup(option->val.str.def_value);
  @@ -181,48 +177,53 @@
   	return s;
   }
   
  -static INLINE int not_correct_number(const struct option_s *option, NUMBER num)
  +static int validate_number_minmax(const struct option_s *option,
  +				  const NUMBER value)
   {
  -	if (option == &real_opt_display_timer) {
  -		if (num < TIMER_NONE || num > TIMER_CURRENT) {
  -			return 1;
  -		}
  +	int valid;
   
  -	} else if (option == &real_opt_jumpto_element) {
  -		if (num < JUMP_TO_FAILED || num > JUMP_TO_PACKAGE) {
  -			return 1;
  -		}
  +	valid = (value >= option->val.num.min_value &&
  +		 value <= option->val.num.max_value);
   
  -	} else if (option == &real_opt_logging_method) {
  -		if (num < 0 || num > LAST_LOGGING_METHOD) {
  -			return 1;
  -		}
  -	}
  +	if (!valid)
  +		fprintf(stderr,
  +			"Option %s outside valid range, must be between"
  +			" %d and %d\n", option->name,
  +			option->val.num.min_value,
  +			option->val.num.max_value);
   
  -	return 0;
  +	return valid;
   }
   
  -static int not_valid_command(const char *command)
  +static int validate_command(const struct option_s *option, const STRING value)
   {
   	const char *tmp;
   	int string_count = 0;
   
  -	for (tmp = command; *tmp; ++tmp) {
  +	for (tmp = value; *tmp; ++tmp) {
   		if (*tmp == '%') {
   			switch (*(++tmp)) {
   			case '%':
   				break;
   			case 's':
  -				if (string_count++)
  -					return 1;
  +				if (string_count++) {
  +					fprintf(stderr, "Option %s contains"
  +						" more than one string"
  +						" substitution\n",
  +						option->name);
  +					return 0;
  +				}
   				break;
   			default:
  -				return 1;
  +				fprintf(stderr, "Option %s contains an"
  +					" invalid substitution specifier\n",
  +					option->name);
  +				return 0;
   			}
   		}
   	}
   
  -	return 0;
  +	return 1;
   }
   
   set_opt_e set_yet_unknown_option(const char *opt, const char *val)
  @@ -238,43 +239,40 @@
   		}
   
   		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;
  -					return OPTION_SET;
  -				}
  -
  -				return OPTION_INVALID_VALUE;
  -
  -			case O_NUMBER:
  -				num = strtol(val, &s, 10);
  -
  -				if (s != NULL && *s)
  -					return OPTION_INVALID_VALUE;
  -
  -				if (not_correct_number(options[i], num))
  -					return OPTION_INVALID_VALUE;
  -
  -				options[i]->val.num.value = num;
  -
  +		case O_BOOL:
  +			if (strcmp(val, BOOL_TRUE_VALUE) == 0) {
  +				options[i]->val.bool.value = 1;
   				return OPTION_SET;
  -
  -			case O_STRING:
  -				set_string_option(&options[i]->val.str.value, val);
  -
  -				return OPTION_SET;
  -
  -			case O_COMMAND:
  -				if (not_valid_command(val))
  -					return OPTION_INVALID_VALUE;
  -
  -				set_string_option(&options[i]->val.str.value, val);
  -
  +				
  +			} else if (strcmp(val, BOOL_FALSE_VALUE) == 0) {
  +				options[i]->val.bool.value = 0;
   				return OPTION_SET;
  +			}
  +			
  +			return OPTION_INVALID_VALUE;
  +			
  +		case O_NUMBER:
  +			num = strtol(val, &s, 10);
  +			
  +			if (s != NULL && *s)
  +				return OPTION_INVALID_VALUE;
  +			
  +			if (options[i]->val.num.validate &&
  +			    !options[i]->val.num.validate(options[i], num))
  +				return OPTION_INVALID_VALUE;
  +			
  +			options[i]->val.num.value = num;
  +			
  +			return OPTION_SET;
  +			
  +		case O_STRING:
  +			if (options[i]->val.str.validate &&
  +			    !options[i]->val.str.validate(options[i], val))
  +				return OPTION_INVALID_VALUE;
  +			
  +			set_string_option(&options[i]->val.str.value, val);
  +			
  +			return OPTION_SET;
   		}
   	}
   
  
  
  
  1.9       +69 -63    ALFS/nALFS/src/options.h
  
  Index: options.h
  ===================================================================
  RCS file: /home/cvsroot/ALFS/nALFS/src/options.h,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- options.h	4 Nov 2003 02:35:02 -0000	1.8
  +++ options.h	4 Nov 2003 03:27:30 -0000	1.9
  @@ -63,8 +63,7 @@
   enum option_type {
   	O_BOOL,
   	O_NUMBER,
  -	O_STRING,
  -	O_COMMAND
  +	O_STRING
   };
   
   struct option_s {
  @@ -76,88 +75,95 @@
   		struct {
   			STRING value;
   			STRING const def_value;
  -			int (*validate)(STRING value);
  +			int (*validate)(const struct option_s *option,
  +					const STRING value);
   		} str;
   		struct {
   			BOOL value;
   			BOOL const def_value;
  -			int (*validate)(BOOL value);
  +			int (*validate)(const struct option_s *option,
  +					const BOOL value);
   		} bool;
   		struct {
   			NUMBER value;
   			NUMBER const def_value;
  -			int (*validate)(NUMBER value);
  +			NUMBER const min_value;
  +			NUMBER const max_value;
  +			int (*validate)(const struct option_s *option,
  +					const NUMBER value);
   		} num;
   	} val;
   };
   
   #ifndef STRING_OPTION
  -#define STRING_OPTION(opt_name, opt_def_value) \
  -		extern STRING * const opt_##opt_name;
  +#define STRING_OPTION(opt_name, opt_def_value, opt_other...) \
  +		extern STRING * const opt_##opt_name
   #endif /* STRING_OPTION */
   
   #ifndef BOOL_OPTION
  -#define BOOL_OPTION(opt_name, opt_def_value) \
  -		extern BOOL * const opt_##opt_name;
  +#define BOOL_OPTION(opt_name, opt_def_value, opt_other...) \
  +		extern BOOL * const opt_##opt_name
   #endif /* BOOL_OPTION */
   
   #ifndef NUMBER_OPTION
  -#define NUMBER_OPTION(opt_name, opt_def_value) \
  -		extern NUMBER * const opt_##opt_name;
  +#define NUMBER_OPTION(opt_name, opt_def_value, opt_other...) \
  +		extern NUMBER * const opt_##opt_name
   #endif /* NUMBER_OPTION */
   
  -#ifndef COMMAND_OPTION
  -#define COMMAND_OPTION(opt_name, opt_def_value) \
  -		extern STRING * const opt_##opt_name;
  -#endif /* COMMAND_OPTION */
  -
  -
  -STRING_OPTION(alfs_directory,"")
  -BOOL_OPTION(start_immediately,0)
  -STRING_OPTION(packages_directory,"packages")
  -STRING_OPTION(default_syntax,"3.0")
  -STRING_OPTION(cursor,"->")
  -NUMBER_OPTION(indentation_size,4)
  -NUMBER_OPTION(sleep_after_stamp,1)
  -BOOL_OPTION(beep_when_done,0)
  -BOOL_OPTION(display_profile,1)
  -BOOL_OPTION(display_options_line,1)
  -NUMBER_OPTION(display_timer,TIMER_TOTAL)
  -BOOL_OPTION(expand_profiles,0)
  -BOOL_OPTION(use_relative_dirs,0)
  -BOOL_OPTION(log_status_window,0)
  -STRING_OPTION(status_logfile,"log_file")
  -BOOL_OPTION(show_system_output,1)
  -BOOL_OPTION(be_verbose,0)
  -BOOL_OPTION(display_alfs,0)
  -BOOL_OPTION(display_doctype,0)
  -BOOL_OPTION(display_comments,0)
  -BOOL_OPTION(run_interactive,1)
  -NUMBER_OPTION(jumpto_element,JUMP_TO_RUNNING)
  -NUMBER_OPTION(logging_method,LOG_OFF)
  -BOOL_OPTION(log_handlers,1)
  -BOOL_OPTION(log_backend,1)
  -BOOL_OPTION(stamp_packages,0)
  -STRING_OPTION(stamp_directory,"stamps")
  -BOOL_OPTION(display_stage_header,0)
  -STRING_OPTION(find_base,"/")
  -STRING_OPTION(find_prunes,"")
  -STRING_OPTION(find_prunes_file,"")
  -STRING_OPTION(profiles_directory,"/")
  -BOOL_OPTION(warn_if_set,0)
  -BOOL_OPTION(follow_running,0)
  -STRING_OPTION(warn_if_set_variables,"CPPFLAGS CXXFLAGS CFLAGS LDFLAGS")
  -BOOL_OPTION(print_startup_help,1)
  -NUMBER_OPTION(windows_relation,50)
  -NUMBER_OPTION(status_history,500)
  -STRING_OPTION(editor,"")
  -COMMAND_OPTION(bunzip2_command,"bunzip2 -dc %s")
  -COMMAND_OPTION(gunzip_command,"zcat %s")
  -COMMAND_OPTION(uncompress_command,"zcat %s")
  -COMMAND_OPTION(untar_command,"tar xv")
  -COMMAND_OPTION(unpax_command,"pax -rv")
  -COMMAND_OPTION(uncpio_command,"cpio -idv")
  -COMMAND_OPTION(unzip_command,"unzip %s")
  +
  +static int validate_command(const struct option_s *option, const STRING value);
  +static int validate_number_minmax(const struct option_s *option, const NUMBER value);
  +
  +
  +STRING_OPTION(alfs_directory,"");
  +BOOL_OPTION(start_immediately,0);
  +STRING_OPTION(packages_directory,"packages");
  +STRING_OPTION(default_syntax,"3.0");
  +STRING_OPTION(cursor,"->");
  +NUMBER_OPTION(indentation_size,4);
  +NUMBER_OPTION(sleep_after_stamp,1);
  +BOOL_OPTION(beep_when_done,0);
  +BOOL_OPTION(display_profile,1);
  +BOOL_OPTION(display_options_line,1);
  +NUMBER_OPTION(display_timer,TIMER_TOTAL,.validate=validate_number_minmax,
  +	      .min_value=TIMER_NONE, .max_value=TIMER_CURRENT);
  +BOOL_OPTION(expand_profiles,0);
  +BOOL_OPTION(use_relative_dirs,0);
  +BOOL_OPTION(log_status_window,0);
  +STRING_OPTION(status_logfile,"log_file");
  +BOOL_OPTION(show_system_output,1);
  +BOOL_OPTION(be_verbose,0);
  +BOOL_OPTION(display_alfs,0);
  +BOOL_OPTION(display_doctype,0);
  +BOOL_OPTION(display_comments,0);
  +BOOL_OPTION(run_interactive,1);
  +NUMBER_OPTION(jumpto_element,JUMP_TO_RUNNING,.validate=validate_number_minmax,
  +	      .min_value=JUMP_TO_FAILED, .max_value=JUMP_TO_PACKAGE);
  +NUMBER_OPTION(logging_method,LOG_OFF,.validate=validate_number_minmax,
  +	      .min_value=LOG_OFF,.max_value=LAST_LOGGING_METHOD);
  +BOOL_OPTION(log_handlers,1);
  +BOOL_OPTION(log_backend,1);
  +BOOL_OPTION(stamp_packages,0);
  +STRING_OPTION(stamp_directory,"stamps");
  +BOOL_OPTION(display_stage_header,0);
  +STRING_OPTION(find_base,"/");
  +STRING_OPTION(find_prunes,"");
  +STRING_OPTION(find_prunes_file,"");
  +STRING_OPTION(profiles_directory,"/");
  +BOOL_OPTION(warn_if_set,0);
  +BOOL_OPTION(follow_running,0);
  +STRING_OPTION(warn_if_set_variables,"CPPFLAGS CXXFLAGS CFLAGS LDFLAGS");
  +BOOL_OPTION(print_startup_help,1);
  +NUMBER_OPTION(windows_relation,50);
  +NUMBER_OPTION(status_history,500);
  +STRING_OPTION(editor,"");
  +STRING_OPTION(bunzip2_command,"bunzip2 -dc %s",validate_command);
  +STRING_OPTION(gunzip_command,"zcat %s",validate_command);
  +STRING_OPTION(uncompress_command,"zcat %s",validate_command);
  +STRING_OPTION(untar_command,"tar xv",validate_command);
  +STRING_OPTION(unpax_command,"pax -rv",validate_command);
  +STRING_OPTION(uncpio_command,"cpio -idv",validate_command);
  +STRING_OPTION(unzip_command,"unzip %s",validate_command);
   
   
   void set_string_option(STRING * const var, const STRING value);
  
  
  



More information about the alfs-log mailing list