liboptcfg

diff src/optcfg.c @ 4:a6f127f3408d

implemented -no- and -disable- prefixes
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 27 Jul 2016 05:05:45 +0300
parents 9c73004c7af3
children aca7267dfee0
line diff
     1.1 --- a/src/optcfg.c	Wed Jul 27 04:31:40 2016 +0300
     1.2 +++ b/src/optcfg.c	Wed Jul 27 05:05:45 2016 +0300
     1.3 @@ -22,6 +22,7 @@
     1.4  	/* argument parsing state */
     1.5  	char **argv;
     1.6  	int argidx;
     1.7 +	int disable_opt;
     1.8  
     1.9  	/* config file parsing state */
    1.10  	const char *cfg_fname;
    1.11 @@ -29,7 +30,7 @@
    1.12  	char *cfg_value;
    1.13  };
    1.14  
    1.15 -static int get_opt(struct optcfg *oc, const char *s);
    1.16 +static int get_opt(struct optcfg *oc, const char *s, int *disable_opt);
    1.17  static char *skip_spaces(char *s);
    1.18  static void strip_comments(char *s);
    1.19  static void strip_trailing_spaces(char *s);
    1.20 @@ -86,9 +87,10 @@
    1.21  
    1.22  		if(argv[i][0] == '-') {
    1.23  			if(oc->opt_func) {
    1.24 -				int o = get_opt(oc, argv[i]);
    1.25 +				int o = get_opt(oc, argv[i], &oc->disable_opt);
    1.26  				if(o == -1 || oc->opt_func(oc, o, oc->opt_cls) == -1) {
    1.27  					if(oc->err_abort) {
    1.28 +						fprintf(stderr, "unexpected option: %s\n", argv[i]);
    1.29  						return -1;
    1.30  					}
    1.31  				}
    1.32 @@ -102,6 +104,7 @@
    1.33  			if(oc->arg_func) {
    1.34  				if(oc->arg_func(oc, argv[i], oc->arg_cls) == -1) {
    1.35  					if(oc->err_abort) {
    1.36 +						fprintf(stderr, "unexpected argument: %s\n", argv[i]);
    1.37  						return -1;
    1.38  					}
    1.39  				}
    1.40 @@ -173,7 +176,7 @@
    1.41  		return -1;
    1.42  	}
    1.43  	oc->cfg_value = val;
    1.44 -	if((opt = get_opt(oc, start)) == -1) {
    1.45 +	if((opt = get_opt(oc, start, 0)) == -1) {
    1.46  		fprintf(stderr, "error parsing %s line %d: unknown option: %s\n", oc->cfg_fname ? oc->cfg_fname : "",
    1.47  				oc->cfg_nline, start);
    1.48  		return -1;
    1.49 @@ -191,7 +194,7 @@
    1.50  int optcfg_enabled_value(struct optcfg *oc, int *enabledp)
    1.51  {
    1.52  	if(oc->argidx) {
    1.53 -		*enabledp = 1; /* TODO take -no- prefix into account */
    1.54 +		*enabledp = ~oc->disable_opt & 1;
    1.55  	} else {
    1.56  		char *val = optcfg_next_value(oc);
    1.57  		if(optcfg_bool_value(val, enabledp) == -1) {
    1.58 @@ -270,17 +273,27 @@
    1.59  
    1.60  
    1.61  
    1.62 -static int get_opt(struct optcfg *oc, const char *arg)
    1.63 +static int get_opt(struct optcfg *oc, const char *arg, int *disable_opt)
    1.64  {
    1.65  	int i, ndashes = 0;
    1.66  
    1.67  	while(*arg && *arg == '-') {
    1.68 -		ndashes++;
    1.69 -		arg++;
    1.70 +		++ndashes;
    1.71 +		++arg;
    1.72  	}
    1.73  
    1.74  	if(ndashes > 2) {
    1.75  		arg -= ndashes;
    1.76 +		ndashes = 0;
    1.77 +	}
    1.78 +
    1.79 +	if(disable_opt) {
    1.80 +		if(ndashes && (strstr(arg, "no-") == arg || strstr(arg, "disable-") == arg)) {
    1.81 +			*disable_opt = 1;
    1.82 +			arg = strchr(arg, '-') + 1;	/* guaranteed to exist at this point */
    1.83 +		} else {
    1.84 +			*disable_opt = 0;
    1.85 +		}
    1.86  	}
    1.87  
    1.88  	if(arg[1]) {	/* match long options */