Browse Source

Macros for clish_param_t

Serj Kalichev 5 years ago
parent
commit
f9aa3d7b43
6 changed files with 85 additions and 382 deletions
  1. 40 66
      clish/param.h
  2. 1 0
      clish/param/module.am
  3. 41 313
      clish/param/param.c
  4. 1 1
      clish/shell/shell_command.c
  5. 1 1
      clish/shell/shell_parse.c
  6. 1 1
      clish/shell/shell_xml.c

+ 40 - 66
clish/param.h

@@ -1,17 +1,9 @@
 /*
  * param.h
+ * Parameter instances are assocated with a command line and used to validate the
+ * the arguments which a user is inputing for a command.
  */
-/**
-\ingroup clish
-\defgroup clish_param param
-@{
 
-\brief This class represents an instance of a parameter type.
-
-Parameter instances are assocated with a command line and used to validate the
-the arguments which a user is inputing for a command.
-
-*/
 #ifndef _clish_param_h
 #define _clish_param_h
 
@@ -23,78 +15,61 @@ typedef struct clish_param_s clish_param_t;
 #include "clish/pargv.h"
 #include "clish/var.h"
 
-/**
- * The means by which the param is interpreted.
- */
+/* The means by which the param is interpreted */
 typedef enum {
-    /**
-     * A common parameter.
-    */
+	/* A common parameter */
 	CLISH_PARAM_COMMON,
-    /**
-     * A swich parameter.
-     * Choose the only one of nested parameters.
-     */
+	/* A swich parameter. Choose the only one of nested parameters. */
 	CLISH_PARAM_SWITCH,
-    /**
-     * A subcomand.
-     * Identified by it's name.
-     */
+	/* A subcomand. Identified by it's name. */
 	CLISH_PARAM_SUBCOMMAND
 } clish_param_mode_e;
 
-/*=====================================
- * PARAM INTERFACE
- *===================================== */
-/*-----------------
- * meta functions
- *----------------- */
+/* Class param */
+
 clish_param_t *clish_param_new(const char *name,
 	const char *text, const char *ptype_name);
-/*-----------------
- * methods
- *----------------- */
+
 void clish_param_delete(clish_param_t * instance);
 void clish_param_help(const clish_param_t * instance, clish_help_t *help);
 void clish_param_help_arrow(const clish_param_t * instance, size_t offset);
 char *clish_param_validate(const clish_param_t * instance, const char *text);
 void clish_param_dump(const clish_param_t * instance);
 void clish_param_insert_param(clish_param_t * instance, clish_param_t * param);
-/*-----------------
- * attributes
- *----------------- */
-void clish_param__set_ptype_name(clish_param_t *instance, const char *ptype_name);
-const char * clish_param__get_ptype_name(const clish_param_t *instance);
-const char *clish_param__get_name(const clish_param_t * instance);
-const char *clish_param__get_text(const clish_param_t * instance);
-const char *clish_param__get_range(const clish_param_t * instance);
-const char *clish_param__get_default(const clish_param_t * instance);
-clish_ptype_t *clish_param__get_ptype(const clish_param_t * instance);
-void clish_param__set_ptype(clish_param_t *instance, clish_ptype_t *ptype);
-void clish_param__set_default(clish_param_t * instance, const char *defval);
-void clish_param__set_mode(clish_param_t * instance, clish_param_mode_e mode);
-clish_param_mode_e clish_param__get_mode(const clish_param_t * instance);
+
+_CLISH_SET_STR(param, ptype_name);
+_CLISH_GET_STR(param, ptype_name);
+_CLISH_SET_STR(param, access);
+_CLISH_GET_STR(param, access);
+_CLISH_GET_STR(param, name);
+_CLISH_GET_STR(param, text);
+_CLISH_GET_STR(param, range);
+_CLISH_SET_STR_ONCE(param, value);
+_CLISH_GET_STR(param, value);
+_CLISH_SET(param, clish_ptype_t *, ptype);
+_CLISH_GET(param, clish_ptype_t *, ptype);
+_CLISH_SET_STR_ONCE(param, defval);
+_CLISH_GET_STR(param, defval);
+_CLISH_SET_STR_ONCE(param, test);
+_CLISH_GET_STR(param, test);
+_CLISH_SET_STR_ONCE(param, completion);
+_CLISH_GET_STR(param, completion);
+_CLISH_SET(param, clish_param_mode_e, mode);
+_CLISH_GET(param, clish_param_mode_e, mode);
+_CLISH_GET(param, clish_paramv_t *, paramv);
+_CLISH_SET(param, bool_t, optional);
+_CLISH_GET(param, bool_t, optional);
+_CLISH_SET(param, bool_t, order);
+_CLISH_GET(param, bool_t, order);
+_CLISH_SET(param, bool_t, hidden);
+_CLISH_GET(param, bool_t, hidden);
+_CLISH_GET(param, unsigned int, param_count);
 
 clish_param_t *clish_param__get_param(const clish_param_t * instance,
-				unsigned index);
-unsigned int clish_param__get_param_count(const clish_param_t * instance);
-clish_paramv_t *clish_param__get_paramv(clish_param_t * instance);
-void clish_param__set_optional(clish_param_t * instance, bool_t optional);
-bool_t clish_param__get_optional(const clish_param_t * instance);
-void clish_param__set_order(clish_param_t * instance, bool_t order);
-bool_t clish_param__get_order(const clish_param_t * instance);
-void clish_param__set_value(clish_param_t * instance, const char * value);
-char *clish_param__get_value(const clish_param_t * instance);
-void clish_param__set_hidden(clish_param_t * instance, bool_t hidden);
-bool_t clish_param__get_hidden(const clish_param_t * instance);
-void clish_param__set_test(clish_param_t * instance, const char *test);
-char *clish_param__get_test(const clish_param_t *instance);
-void clish_param__set_completion(clish_param_t *instance, const char *completion);
-char *clish_param__get_completion(const clish_param_t *instance);
-void clish_param__set_access(clish_param_t *instance, const char *access);
-char *clish_param__get_access(const clish_param_t *instance);
+	unsigned int index);
+
+/* Class paramv */
 
-/* paramv methods */
 clish_paramv_t *clish_paramv_new(void);
 void clish_paramv_delete(clish_paramv_t * instance);
 void clish_paramv_insert(clish_paramv_t * instance, clish_param_t * param);
@@ -108,4 +83,3 @@ const char *clish_paramv_find_default(const clish_paramv_t * instance,
 	const char *name);
 
 #endif				/* _clish_param_h */
-/** @} clish_param */

+ 1 - 0
clish/param/module.am

@@ -1,4 +1,5 @@
 libclish_la_SOURCES += \
 	clish/param/param.c \
+	clish/param/paramv.c \
 	clish/param/param_dump.c \
 	clish/param/private.h

+ 41 - 313
clish/param/param.c

@@ -12,9 +12,7 @@
 #include <stdio.h>
 #include <string.h>
 
-/*---------------------------------------------------------
- * PRIVATE METHODS
- *--------------------------------------------------------- */
+/*--------------------------------------------------------- */
 static void clish_param_init(clish_param_t *this, const char *name,
 	const char *text, const char *ptype_name)
 {
@@ -53,9 +51,7 @@ static void clish_param_fini(clish_param_t * this)
 	clish_paramv_delete(this->paramv);
 }
 
-/*---------------------------------------------------------
- * PUBLIC META FUNCTIONS
- *--------------------------------------------------------- */
+/*--------------------------------------------------------- */
 clish_param_t *clish_param_new(const char *name, const char *text,
 	const char *ptype_name)
 {
@@ -66,9 +62,7 @@ clish_param_t *clish_param_new(const char *name, const char *text,
 	return this;
 }
 
-/*---------------------------------------------------------
- * PUBLIC METHODS
- *--------------------------------------------------------- */
+/*--------------------------------------------------------- */
 void clish_param_delete(clish_param_t * this)
 {
 	clish_param_fini(this);
@@ -81,80 +75,6 @@ void clish_param_insert_param(clish_param_t * this, clish_param_t * param)
 	return clish_paramv_insert(this->paramv, param);
 }
 
-/*---------------------------------------------------------
- * PUBLIC ATTRIBUTES
- *--------------------------------------------------------- */
-void clish_param__set_ptype_name(clish_param_t *this, const char *ptype_name)
-{
-	if (this->ptype_name)
-		lub_string_free(this->ptype_name);
-	this->ptype_name = lub_string_dup(ptype_name);
-}
-
-/*--------------------------------------------------------- */
-const char * clish_param__get_ptype_name(const clish_param_t *this)
-{
-	return this->ptype_name;
-}
-
-/*--------------------------------------------------------- */
-const char *clish_param__get_name(const clish_param_t * this)
-{
-	if (!this)
-		return NULL;
-	return this->name;
-}
-
-/*--------------------------------------------------------- */
-const char *clish_param__get_text(const clish_param_t * this)
-{
-	return this->text;
-}
-
-/*--------------------------------------------------------- */
-const char *clish_param__get_range(const clish_param_t * this)
-{
-	return clish_ptype__get_range(this->ptype);
-}
-
-/*--------------------------------------------------------- */
-clish_ptype_t *clish_param__get_ptype(const clish_param_t * this)
-{
-	return this->ptype;
-}
-
-/*--------------------------------------------------------- */
-void clish_param__set_ptype(clish_param_t *this, clish_ptype_t *ptype)
-{
-	this->ptype = ptype;
-}
-
-/*--------------------------------------------------------- */
-void clish_param__set_default(clish_param_t * this, const char *defval)
-{
-	assert(!this->defval);
-	this->defval = lub_string_dup(defval);
-}
-
-/*--------------------------------------------------------- */
-const char *clish_param__get_default(const clish_param_t * this)
-{
-	return this->defval;
-}
-
-/*--------------------------------------------------------- */
-void clish_param__set_mode(clish_param_t * this, clish_param_mode_e mode)
-{
-	assert(this);
-	this->mode = mode;
-}
-
-/*--------------------------------------------------------- */
-clish_param_mode_e clish_param__get_mode(const clish_param_t * this)
-{
-	return this->mode;
-}
-
 /*--------------------------------------------------------- */
 char *clish_param_validate(const clish_param_t * this, const char *text)
 {
@@ -212,250 +132,58 @@ void clish_param_help_arrow(const clish_param_t * this, size_t offset)
 	this = this; /* Happy compiler */
 }
 
-/*--------------------------------------------------------- */
-clish_param_t *clish_param__get_param(const clish_param_t * this,
-	unsigned index)
-{
-	return clish_paramv__get_param(this->paramv, index);
-}
+CLISH_SET_STR(param, ptype_name);
+CLISH_GET_STR(param, ptype_name);
+CLISH_SET_STR(param, access);
+CLISH_GET_STR(param, access);
+CLISH_GET_STR(param, name);
+CLISH_GET_STR(param, text);
+CLISH_SET_STR_ONCE(param, value);
+CLISH_SET(param, clish_ptype_t *, ptype);
+CLISH_GET(param, clish_ptype_t *, ptype);
+CLISH_SET_STR_ONCE(param, defval);
+CLISH_GET_STR(param, defval);
+CLISH_SET_STR_ONCE(param, test);
+CLISH_GET_STR(param, test);
+CLISH_SET_STR_ONCE(param, completion);
+CLISH_GET_STR(param, completion);
+CLISH_SET(param, clish_param_mode_e, mode);
+CLISH_GET(param, clish_param_mode_e, mode);
+CLISH_GET(param, clish_paramv_t *, paramv);
+CLISH_SET(param, bool_t, optional);
+CLISH_GET(param, bool_t, optional);
+CLISH_SET(param, bool_t, order);
+CLISH_GET(param, bool_t, order);
+CLISH_SET(param, bool_t, hidden);
+CLISH_GET(param, bool_t, hidden);
 
 /*--------------------------------------------------------- */
-clish_paramv_t *clish_param__get_paramv(clish_param_t * this)
+_CLISH_GET_STR(param, value)
 {
-	return this->paramv;
+	assert(inst);
+	if (inst->value)
+		return inst->value;
+	return inst->name;
 }
 
 /*--------------------------------------------------------- */
-unsigned int clish_param__get_param_count(const clish_param_t * this)
+_CLISH_GET_STR(param, range)
 {
-	return clish_paramv__get_count(this->paramv);
+	assert(inst);
+	return clish_ptype__get_range(inst->ptype);
 }
 
 /*--------------------------------------------------------- */
-void clish_param__set_optional(clish_param_t * this, bool_t optional)
-{
-	this->optional = optional;
-}
-
-/*--------------------------------------------------------- */
-bool_t clish_param__get_optional(const clish_param_t * this)
-{
-	return this->optional;
-}
-
-/*--------------------------------------------------------- */
-void clish_param__set_order(clish_param_t * this, bool_t order)
-{
-	this->order = order;
-}
-
-/*--------------------------------------------------------- */
-bool_t clish_param__get_order(const clish_param_t * this)
-{
-	return this->order;
-}
-
-/*--------------------------------------------------------- */
-
-/* paramv methods */
-
-/*--------------------------------------------------------- */
-static void clish_paramv_init(clish_paramv_t * this)
-{
-	this->paramc = 0;
-	this->paramv = NULL;
-}
-
-/*--------------------------------------------------------- */
-static void clish_paramv_fini(clish_paramv_t * this)
-{
-	unsigned i;
-
-	/* finalize each of the parameter instances */
-	for (i = 0; i < this->paramc; i++) {
-		clish_param_delete(this->paramv[i]);
-	}
-	/* free the parameter vector */
-	free(this->paramv);
-	this->paramc = 0;
-}
-
-/*--------------------------------------------------------- */
-clish_paramv_t *clish_paramv_new(void)
-{
-	clish_paramv_t *this = malloc(sizeof(clish_paramv_t));
-
-	if (this)
-		clish_paramv_init(this);
-	return this;
-}
-
-/*--------------------------------------------------------- */
-void clish_paramv_delete(clish_paramv_t * this)
-{
-	clish_paramv_fini(this);
-	free(this);
-}
-
-/*--------------------------------------------------------- */
-void clish_paramv_insert(clish_paramv_t * this, clish_param_t * param)
-{
-	size_t new_size = ((this->paramc + 1) * sizeof(clish_param_t *));
-	clish_param_t **tmp;
-
-	/* resize the parameter vector */
-	tmp = realloc(this->paramv, new_size);
-	if (tmp) {
-		this->paramv = tmp;
-		/* insert reference to the parameter */
-		this->paramv[this->paramc++] = param;
-	}
-}
-
-/*--------------------------------------------------------- */
-int clish_paramv_remove(clish_paramv_t *this, unsigned int index)
-{
-	size_t new_size;
-	clish_param_t **tmp;
-	clish_param_t **dst, **src;
-	size_t n;
-
-	if (this->paramc < 1)
-		return -1;
-	if (index >= this->paramc)
-		return -1;
-
-	new_size = ((this->paramc - 1) * sizeof(clish_param_t *));
-	dst = this->paramv + index;
-	src = dst + 1;
-	n = this->paramc - index - 1;
-	if (n)
-		memmove(dst, src, n * sizeof(clish_param_t *));
-	/* Resize the parameter vector */
-	if (new_size) {
-		tmp = realloc(this->paramv, new_size);
-		if (!tmp)
-			return -1;
-		this->paramv = tmp;
-	} else {
-		free(this->paramv);
-		this->paramv = NULL;
-	}
-	this->paramc--;
-
-	return 0;
-}
-
-/*--------------------------------------------------------- */
-clish_param_t *clish_paramv__get_param(const clish_paramv_t * this,
+clish_param_t *clish_param__get_param(const clish_param_t * this,
 	unsigned int index)
 {
-	clish_param_t *result = NULL;
-
-	if (index < this->paramc)
-		result = this->paramv[index];
-	return result;
-}
-
-/*--------------------------------------------------------- */
-clish_param_t *clish_paramv_find_param(const clish_paramv_t * this,
-	const char *name)
-{
-	clish_param_t *res = NULL;
-	unsigned int i;
-
-	for (i = 0; i < this->paramc; i++) {
-		if (!strcmp(clish_param__get_name(this->paramv[i]), name))
-			return this->paramv[i];
-		if ((res = clish_paramv_find_param(
-			clish_param__get_paramv(this->paramv[i]), name)))
-			return res;
-	}
-
-	return res;
-}
-
-/*--------------------------------------------------------- */
-const char *clish_paramv_find_default(const clish_paramv_t * this,
-	const char *name)
-{
-	clish_param_t *res = clish_paramv_find_param(this, name);
-
-	if (res)
-		return clish_param__get_default(res);
-
-	return NULL;
-}
-
-/*--------------------------------------------------------- */
-unsigned int clish_paramv__get_count(const clish_paramv_t * this)
-{
-	return this->paramc;
-}
-
-/*--------------------------------------------------------- */
-void clish_param__set_value(clish_param_t * this, const char * value)
-{
-	assert(!this->value);
-	this->value = lub_string_dup(value);
-}
-
-/*--------------------------------------------------------- */
-char *clish_param__get_value(const clish_param_t * this)
-{
-	if (this->value)
-		return this->value;
-	return this->name;
-}
-
-/*--------------------------------------------------------- */
-void clish_param__set_hidden(clish_param_t * this, bool_t hidden)
-{
-	this->hidden = hidden;
-}
-
-/*--------------------------------------------------------- */
-bool_t clish_param__get_hidden(const clish_param_t * this)
-{
-	return this->hidden;
-}
-
-/*--------------------------------------------------------- */
-void clish_param__set_test(clish_param_t * this, const char *test)
-{
-	assert(!this->test);
-	this->test = lub_string_dup(test);
-}
-
-/*--------------------------------------------------------- */
-char *clish_param__get_test(const clish_param_t *this)
-{
-	return this->test;
-}
-
-/*--------------------------------------------------------- */
-void clish_param__set_completion(clish_param_t *this, const char *completion)
-{
-	assert(!this->completion);
-	this->completion = lub_string_dup(completion);
-}
-
-/*--------------------------------------------------------- */
-char *clish_param__get_completion(const clish_param_t *this)
-{
-	return this->completion;
-}
-
-/*--------------------------------------------------------- */
-void clish_param__set_access(clish_param_t *this, const char *access)
-{
-	if (this->access)
-		lub_string_free(this->access);
-	this->access = lub_string_dup(access);
+	assert(this);
+	return clish_paramv__get_param(this->paramv, index);
 }
 
 /*--------------------------------------------------------- */
-char *clish_param__get_access(const clish_param_t *this)
+_CLISH_GET(param, unsigned int, param_count)
 {
-	return this->access;
+	assert(inst);
+	return clish_paramv__get_count(inst->paramv);
 }

+ 1 - 1
clish/shell/shell_command.c

@@ -107,7 +107,7 @@ void clish_shell_param_generator(clish_shell_t *this, lub_argv_t *matches,
 
 		while ((param = clish_pargv__get_param(completion,
 			completion_index++))) {
-			char *result;
+			const char *result;
 			/* The param is args so it has no completion */
 			if (param == clish_command__get_args(cmd))
 				continue;

+ 1 - 1
clish/shell/shell_parse.c

@@ -49,7 +49,7 @@ clish_pargv_status_e clish_shell_parse(
 static bool_t line_test(const clish_param_t *param, void *context)
 {
 	char *str = NULL;
-	char *teststr = NULL;
+	const char *teststr = NULL;
 	bool_t res;
 
 	if (!param)

+ 1 - 1
clish/shell/shell_xml.c

@@ -756,7 +756,7 @@ static int process_param(clish_shell_t *shell, clish_xmlnode_t *element,
 	}
 
 	if (defval)
-		clish_param__set_default(param, defval);
+		clish_param__set_defval(param, defval);
 
 	if (hidden && lub_string_nocasecmp(hidden, "true") == 0)
 		clish_param__set_hidden(param, BOOL_TRUE);