projects/pwm

changeset 31:9be355e742e5

Distinguish between unknown argument and missing value
author Guido Berhoerster <guido+pwm@berhoerster.name>
date Tue Nov 28 16:48:45 2017 +0100 (2017-11-28)
parents 2552eec9b913
children b5ebed168e59
files cmd.c
line diff
     1.1 --- a/cmd.c	Tue Nov 28 15:09:19 2017 +0100
     1.2 +++ b/cmd.c	Tue Nov 28 16:48:45 2017 +0100
     1.3 @@ -201,16 +201,18 @@
     1.4  static int
     1.5  parse_arg(char *arg, const char *namev[], int sep, char **valuep)
     1.6  {
     1.7 +	char	*p;
     1.8  	size_t	i;
     1.9 -	size_t	name_len;
    1.10 +
    1.11 +	if ((sep != '\0') && ((p = strchr(arg, sep)) != NULL)) {
    1.12 +		*p++ = '\0';
    1.13 +	}
    1.14 +	if (valuep != NULL) {
    1.15 +		*valuep = p;
    1.16 +	}
    1.17  
    1.18  	for (i = 0; namev[i] != NULL; i++) {
    1.19 -		name_len = strlen(namev[i]);
    1.20 -		if ((strncmp(namev[i], arg, name_len) == 0) &&
    1.21 -		    (arg[name_len] == sep)) {
    1.22 -			if (valuep != NULL) {
    1.23 -				*valuep = arg + name_len + 1;
    1.24 -			}
    1.25 +		if (strcmp(namev[i], arg) == 0) {
    1.26  			return (i);
    1.27  		}
    1.28  	}
    1.29 @@ -256,6 +258,10 @@
    1.30  	}
    1.31  
    1.32  	type = parse_arg(argv[1], optionv, '=', &value);
    1.33 +	if ((type >= 0) && (value == NULL)) {
    1.34 +		pwm_err(ctx, "missing value for \"%s\"", argv[1]);
    1.35 +		return (CMD_ERR);
    1.36 +	}
    1.37  	switch (type) {
    1.38  	case OPTION_FILENAME:
    1.39  		free(ctx->filename);
    1.40 @@ -267,7 +273,7 @@
    1.41  		ctx->pipecmd = (value[0] != '\0') ? xstrdup(value) : NULL;
    1.42  		break;
    1.43  	default:
    1.44 -		pwm_err(ctx, "bad option name \"%s\"", argv[1]);
    1.45 +		pwm_err(ctx, "unknown option \"%s\"", argv[1]);
    1.46  		return (CMD_ERR);
    1.47  	}
    1.48  
    1.49 @@ -424,11 +430,7 @@
    1.50  
    1.51  	for (i = 1; i < argc; i++) {
    1.52  		type = parse_arg(argv[i], field_namev, '~', &value);
    1.53 -		if (type == FIELD_UNKNOWN) {
    1.54 -			pwm_err(ctx, "bad field name \"%s\"", argv[i]);
    1.55 -			goto out;
    1.56 -		}
    1.57 -		if (value[0] == '\0') {
    1.58 +		if ((type >= 0) && ((value == NULL) || (value[0] == '\0'))) {
    1.59  			/* skip empty expressions */
    1.60  			continue;
    1.61  		}
    1.62 @@ -449,7 +451,7 @@
    1.63  			repp = &url_re;
    1.64  			break;
    1.65  		default:
    1.66 -			pwm_err(ctx, "bad field name \"%s\"", argv[i]);
    1.67 +			pwm_err(ctx, "unknown field name \"%s\"", argv[i]);
    1.68  			goto out;
    1.69  		}
    1.70  
    1.71 @@ -634,10 +636,7 @@
    1.72  
    1.73  	for (i = 1; i < argc; i++) {
    1.74  		type = parse_arg(argv[i], field_namev, '=', &value);
    1.75 -		if (type == FIELD_UNKNOWN) {
    1.76 -			pwm_err(ctx, "bad field assignment \"%s\"", argv[i]);
    1.77 -		}
    1.78 -		if (value[0] == '\0') {
    1.79 +		if ((type >= 0) && ((value == NULL) || (value[0] == '\0'))) {
    1.80  			/* skip empty assignments */
    1.81  			continue;
    1.82  		}
    1.83 @@ -667,7 +666,7 @@
    1.84  			record->url = xstrdup(value);
    1.85  			break;
    1.86  		default:
    1.87 -			pwm_err(ctx, "bad field name \"%s\"", argv[i]);
    1.88 +			pwm_err(ctx, "unknown field name \"%s\"", argv[i]);
    1.89  			goto out;
    1.90  		}
    1.91  	}
    1.92 @@ -716,11 +715,7 @@
    1.93  
    1.94  	for (i = 2; i < argc; i++) {
    1.95  		type = parse_arg(argv[i], field_namev, '=', &value);
    1.96 -		if (type == FIELD_UNKNOWN) {
    1.97 -			pwm_err(ctx, "bad field assignment \"%s\"", argv[i]);
    1.98 -			goto out;
    1.99 -		}
   1.100 -		if (value[0] == '\0') {
   1.101 +		if ((type >= 0) && ((value == NULL) || (value[0] == '\0'))) {
   1.102  			/* skip empty assignments */
   1.103  			continue;
   1.104  		}
   1.105 @@ -750,7 +745,7 @@
   1.106  			record->url = xstrdup(value);
   1.107  			break;
   1.108  		default:
   1.109 -			pwm_err(ctx, "bad field name \"%s\"", argv[i]);
   1.110 +			pwm_err(ctx, "unknown field name \"%s\"", argv[i]);
   1.111  			goto out;
   1.112  		}
   1.113  	}
   1.114 @@ -776,6 +771,7 @@
   1.115  	enum cmd_return	retval = CMD_ERR;
   1.116  	unsigned int	id = 0;
   1.117  	int		i = 1;
   1.118 +	enum cmd_generatepassword_arg_type type;
   1.119  	char		*value = NULL;
   1.120  	long		x;
   1.121  	char		*p;
   1.122 @@ -798,8 +794,14 @@
   1.123  	}
   1.124  
   1.125  	for (; i < argc; i++) {
   1.126 -		switch (parse_arg(argv[i], cmd_generatepassword_argv, '=',
   1.127 -		    &value)) {
   1.128 +		type = parse_arg(argv[i], cmd_generatepassword_argv, '=',
   1.129 +		    &value);
   1.130 +		if ((type >= 0) && ((value == NULL) || (value[0] == '\0'))) {
   1.131 +			pwm_err(ctx, "invalid value for \"%s\"", argv[i]);
   1.132 +			retval = CMD_USAGE;
   1.133 +			goto out;
   1.134 +		}
   1.135 +		switch (type) {
   1.136  		case CMD_GP_ARG_LEN:
   1.137  			errno = 0;
   1.138  			x = strtol(value, &p, 10);
   1.139 @@ -1032,7 +1034,7 @@
   1.140  	for (i = 2; i < argc; i++) {
   1.141  		type = parse_arg(argv[i], field_namev, '\0', NULL);
   1.142  		if (type < 0) {
   1.143 -			pwm_err(ctx, "bad field name \"%s\"", argv[i]);
   1.144 +			pwm_err(ctx, "unknown field name \"%s\"", argv[i]);
   1.145  			return (CMD_ERR);
   1.146  		}
   1.147  		fields[type] = 1;
   1.148 @@ -1074,7 +1076,7 @@
   1.149  
   1.150  	type = parse_arg(argv[2], field_namev, '\0', NULL);
   1.151  	if (type < 0) {
   1.152 -		pwm_err(ctx, "bad field name \"%s\"", argv[2]);
   1.153 +		pwm_err(ctx, "unknown field name \"%s\"", argv[2]);
   1.154  		return (CMD_ERR);
   1.155  	}
   1.156  	fields[type] = 1;