changeset 31:9be355e742e5

Distinguish between unknown argument and missing value
author Guido Berhoerster <guido+pwm@berhoerster.name>
date Tue, 28 Nov 2017 16:48:45 +0100
parents 2552eec9b913
children b5ebed168e59
files cmd.c
diffstat 1 files changed, 31 insertions(+), 29 deletions(-) [+]
line wrap: on
line diff
--- a/cmd.c	Tue Nov 28 15:09:19 2017 +0100
+++ b/cmd.c	Tue Nov 28 16:48:45 2017 +0100
@@ -201,16 +201,18 @@
 static int
 parse_arg(char *arg, const char *namev[], int sep, char **valuep)
 {
+	char	*p;
 	size_t	i;
-	size_t	name_len;
+
+	if ((sep != '\0') && ((p = strchr(arg, sep)) != NULL)) {
+		*p++ = '\0';
+	}
+	if (valuep != NULL) {
+		*valuep = p;
+	}
 
 	for (i = 0; namev[i] != NULL; i++) {
-		name_len = strlen(namev[i]);
-		if ((strncmp(namev[i], arg, name_len) == 0) &&
-		    (arg[name_len] == sep)) {
-			if (valuep != NULL) {
-				*valuep = arg + name_len + 1;
-			}
+		if (strcmp(namev[i], arg) == 0) {
 			return (i);
 		}
 	}
@@ -256,6 +258,10 @@
 	}
 
 	type = parse_arg(argv[1], optionv, '=', &value);
+	if ((type >= 0) && (value == NULL)) {
+		pwm_err(ctx, "missing value for \"%s\"", argv[1]);
+		return (CMD_ERR);
+	}
 	switch (type) {
 	case OPTION_FILENAME:
 		free(ctx->filename);
@@ -267,7 +273,7 @@
 		ctx->pipecmd = (value[0] != '\0') ? xstrdup(value) : NULL;
 		break;
 	default:
-		pwm_err(ctx, "bad option name \"%s\"", argv[1]);
+		pwm_err(ctx, "unknown option \"%s\"", argv[1]);
 		return (CMD_ERR);
 	}
 
@@ -424,11 +430,7 @@
 
 	for (i = 1; i < argc; i++) {
 		type = parse_arg(argv[i], field_namev, '~', &value);
-		if (type == FIELD_UNKNOWN) {
-			pwm_err(ctx, "bad field name \"%s\"", argv[i]);
-			goto out;
-		}
-		if (value[0] == '\0') {
+		if ((type >= 0) && ((value == NULL) || (value[0] == '\0'))) {
 			/* skip empty expressions */
 			continue;
 		}
@@ -449,7 +451,7 @@
 			repp = &url_re;
 			break;
 		default:
-			pwm_err(ctx, "bad field name \"%s\"", argv[i]);
+			pwm_err(ctx, "unknown field name \"%s\"", argv[i]);
 			goto out;
 		}
 
@@ -634,10 +636,7 @@
 
 	for (i = 1; i < argc; i++) {
 		type = parse_arg(argv[i], field_namev, '=', &value);
-		if (type == FIELD_UNKNOWN) {
-			pwm_err(ctx, "bad field assignment \"%s\"", argv[i]);
-		}
-		if (value[0] == '\0') {
+		if ((type >= 0) && ((value == NULL) || (value[0] == '\0'))) {
 			/* skip empty assignments */
 			continue;
 		}
@@ -667,7 +666,7 @@
 			record->url = xstrdup(value);
 			break;
 		default:
-			pwm_err(ctx, "bad field name \"%s\"", argv[i]);
+			pwm_err(ctx, "unknown field name \"%s\"", argv[i]);
 			goto out;
 		}
 	}
@@ -716,11 +715,7 @@
 
 	for (i = 2; i < argc; i++) {
 		type = parse_arg(argv[i], field_namev, '=', &value);
-		if (type == FIELD_UNKNOWN) {
-			pwm_err(ctx, "bad field assignment \"%s\"", argv[i]);
-			goto out;
-		}
-		if (value[0] == '\0') {
+		if ((type >= 0) && ((value == NULL) || (value[0] == '\0'))) {
 			/* skip empty assignments */
 			continue;
 		}
@@ -750,7 +745,7 @@
 			record->url = xstrdup(value);
 			break;
 		default:
-			pwm_err(ctx, "bad field name \"%s\"", argv[i]);
+			pwm_err(ctx, "unknown field name \"%s\"", argv[i]);
 			goto out;
 		}
 	}
@@ -776,6 +771,7 @@
 	enum cmd_return	retval = CMD_ERR;
 	unsigned int	id = 0;
 	int		i = 1;
+	enum cmd_generatepassword_arg_type type;
 	char		*value = NULL;
 	long		x;
 	char		*p;
@@ -798,8 +794,14 @@
 	}
 
 	for (; i < argc; i++) {
-		switch (parse_arg(argv[i], cmd_generatepassword_argv, '=',
-		    &value)) {
+		type = parse_arg(argv[i], cmd_generatepassword_argv, '=',
+		    &value);
+		if ((type >= 0) && ((value == NULL) || (value[0] == '\0'))) {
+			pwm_err(ctx, "invalid value for \"%s\"", argv[i]);
+			retval = CMD_USAGE;
+			goto out;
+		}
+		switch (type) {
 		case CMD_GP_ARG_LEN:
 			errno = 0;
 			x = strtol(value, &p, 10);
@@ -1032,7 +1034,7 @@
 	for (i = 2; i < argc; i++) {
 		type = parse_arg(argv[i], field_namev, '\0', NULL);
 		if (type < 0) {
-			pwm_err(ctx, "bad field name \"%s\"", argv[i]);
+			pwm_err(ctx, "unknown field name \"%s\"", argv[i]);
 			return (CMD_ERR);
 		}
 		fields[type] = 1;
@@ -1074,7 +1076,7 @@
 
 	type = parse_arg(argv[2], field_namev, '\0', NULL);
 	if (type < 0) {
-		pwm_err(ctx, "bad field name \"%s\"", argv[2]);
+		pwm_err(ctx, "unknown field name \"%s\"", argv[2]);
 		return (CMD_ERR);
 	}
 	fields[type] = 1;