Serj Kalichev 6 anni fa
parent
commit
d077de1323
3 ha cambiato i file con 43 aggiunte e 35 eliminazioni
  1. 17 19
      clish/ptype.h
  2. 19 13
      clish/ptype/ptype.c
  3. 7 3
      clish/shell/shell_xml.c

+ 17 - 19
clish/ptype.h

@@ -16,29 +16,27 @@ typedef struct clish_ptype_s clish_ptype_t;
 
 #include <stddef.h>
 
-/**
- * The means by which the pattern is interpreted and 
- * validated.
- */
+/* The means by which the pattern is interpreted and validated. */
 typedef enum {
-    /**
-     * [default] - A POSIX regular expression.
-    */
+	/* [default] - A POSIX regular expression. */
 	CLISH_PTYPE_REGEXP,
-    /**
-     * A numeric definition "min..max" signed and unsigned versions
-     */
+	/* A numeric definition "min..max" signed and unsigned versions */
 	CLISH_PTYPE_INTEGER,
 	CLISH_PTYPE_UNSIGNEDINTEGER,
-    /**
-     * A list of possible values. 
-     * The syntax of the string is of the form: 
-     *  "valueOne(ONE) valueTwo(TWO) valueThree(THREE)"
-     * where the text before the parethesis defines the syntax 
-     * that the user must use, and the value within the parenthesis 
-     * is the result expanded as a parameter value. 
-     */
-	CLISH_PTYPE_SELECT
+	/**
+	* A list of possible values. 
+	* The syntax of the string is of the form: 
+	*  "valueOne(ONE) valueTwo(TWO) valueThree(THREE)"
+	* where the text before the parethesis defines the syntax 
+	* that the user must use, and the value within the parenthesis 
+	* is the result expanded as a parameter value. 
+	*/
+	CLISH_PTYPE_SELECT,
+	/* User-defined code in ACTION */
+	CLISH_PTYPE_CODE,
+	/* Used to detect errors */
+	CLISH_PTYPE_MAX
+
 } clish_ptype_method_e;
 /**
  * This defines the pre processing which is to be

+ 19 - 13
clish/ptype/ptype.c

@@ -97,6 +97,9 @@ static void clish_ptype__set_range(clish_ptype_t * this)
 		break;
 	}
 	/*------------------------------------------------- */
+	default:
+		break;
+	/*------------------------------------------------- */
 	}
 }
 
@@ -129,7 +132,8 @@ static const char *method_names[] = {
 	"regexp",
 	"integer",
 	"unsignedInteger",
-	"select"
+	"select",
+	"code"
 };
 
 /*--------------------------------------------------------- */
@@ -143,21 +147,19 @@ const char *clish_ptype_method__get_name(clish_ptype_method_e method)
 }
 
 /*--------------------------------------------------------- */
+/* Return value CLISH_PTYPE_MAX indicates an illegal method */
 clish_ptype_method_e clish_ptype_method_resolve(const char *name)
 {
-	clish_ptype_method_e result = CLISH_PTYPE_REGEXP;
-	if (NULL != name) {
-		unsigned i;
-		for (i = 0; i < CLISH_PTYPE_SELECT + 1; i++) {
-			if (0 == strcmp(name, method_names[i])) {
-				result = (clish_ptype_method_e) i;
-				break;
-			}
-		}
-		/* error for incorrect type spec */
-		assert(i <= CLISH_PTYPE_SELECT);
+	unsigned int i;
+
+	if (NULL == name)
+		return CLISH_PTYPE_REGEXP;
+	for (i = 0; i < CLISH_PTYPE_MAX; i++) {
+		if (!strcmp(name, method_names[i]))
+			break;
 	}
-	return result;
+
+	return (clish_ptype_method_e)i;
 }
 
 /*--------------------------------------------------------- */
@@ -431,6 +433,8 @@ static void clish_ptype_fini(clish_ptype_t * this)
 		case CLISH_PTYPE_SELECT:
 			lub_argv_delete(this->u.select.items);
 			break;
+		default:
+			break;
 		}
 	}
 
@@ -511,6 +515,8 @@ void clish_ptype__set_pattern(clish_ptype_t * this,
 		this->u.select.items = lub_argv_new(this->pattern, 0);
 		break;
 	/*------------------------------------------------- */
+	default:
+		break;
 	}
 	/* now set up the range details */
 	clish_ptype__set_range(this);

+ 7 - 3
clish/shell/shell_xml.c

@@ -386,13 +386,17 @@ static int process_ptype(clish_shell_t *shell, clish_xmlnode_t *element,
 		fprintf(stderr, CLISH_XML_ERROR_ATTR("name"));
 		goto error;
 	}
-	if (!pattern) {
+
+	method = clish_ptype_method_resolve(method_name);
+	if (CLISH_PTYPE_MAX == method) {
+		fprintf(stderr, CLISH_XML_ERROR_ATTR("method"));
+		goto error;
+	}
+	if ((method != CLISH_PTYPE_CODE) && !pattern) {
 		fprintf(stderr, CLISH_XML_ERROR_ATTR("pattern"));
 		goto error;
 	}
 
-	method = clish_ptype_method_resolve(method_name);
-
 	preprocess = clish_ptype_preprocess_resolve(preprocess_name);
 	ptype = clish_shell_find_create_ptype(shell,
 		name, help, pattern, method, preprocess);