Browse Source

Macros for clish_action_t

Serj Kalichev 6 years ago
parent
commit
c28cfc5d28
9 changed files with 54 additions and 60 deletions
  1. 6 7
      clish/action.h
  2. 10 30
      clish/action/action.c
  3. 1 1
      clish/action/private.h
  4. 24 5
      clish/macros.h
  5. 3 2
      clish/plugin.h
  6. 2 10
      clish/plugin/plugin.c
  7. 1 1
      clish/shell.h
  8. 4 4
      clish/shell/shell_execute.c
  9. 3 0
      lub/string/string.c

+ 6 - 7
clish/action.h

@@ -15,12 +15,11 @@ clish_action_t *clish_action_new(void);
 void clish_action_delete(clish_action_t *instance);
 void clish_action_dump(const clish_action_t *instance);
 
-void clish_action__set_script(clish_action_t *instance, const char *script);
-_CLISH_GET(action, const char *, script);
-_CLISH_GET(action, clish_sym_t *, builtin);
-void clish_action__set_builtin(clish_action_t *instance, clish_sym_t *builtin);
-//clish_sym_t *clish_action__get_builtin(const clish_action_t *instance);
-void clish_action__set_shebang(clish_action_t *instance, const char *shebang);
-const char *clish_action__get_shebang(const clish_action_t *instance);
+_CLISH_SET_STR(action, script);
+_CLISH_GET_STR(action, script);
+_CLISH_SET(action, const clish_sym_t *, builtin);
+_CLISH_GET(action, const clish_sym_t *, builtin);
+_CLISH_SET_STR(action, shebang);
+_CLISH_GET_STR(action, shebang);
 
 #endif // _clish_action_h

+ 10 - 30
clish/action/action.c

@@ -46,40 +46,20 @@ void clish_action_delete(clish_action_t *this)
 	free(this);
 }
 
-/*--------------------------------------------------------- */
-void clish_action__set_script(clish_action_t *this, const char *script)
-{
-	if (this->script)
-		lub_string_free(this->script);
-	this->script = lub_string_dup(script);
-}
-
-CLISH_GET(action, const char *, script);
+CLISH_SET_STR(action, script);
+CLISH_GET_STR(action, script);
+CLISH_SET(action, const clish_sym_t *, builtin);
+CLISH_GET(action, const clish_sym_t *, builtin);
 
-/*--------------------------------------------------------- */
-void clish_action__set_builtin(clish_action_t *this, clish_sym_t *builtin)
+_CLISH_SET_STR(action, shebang)
 {
-	this->builtin = builtin;
-}
-
-CLISH_GET(action, clish_sym_t *, builtin);
-
-/*--------------------------------------------------------- */
-void clish_action__set_shebang(clish_action_t *this, const char *shebang)
-{
-	const char *prog = shebang;
+	const char *prog = val;
 	const char *prefix = "#!";
 
-	if (this->shebang)
-		lub_string_free(this->shebang);
-	if (lub_string_nocasestr(shebang, prefix) == shebang)
+	lub_string_free(inst->shebang);
+	if (lub_string_nocasestr(val, prefix) == val)
 		prog += strlen(prefix);
-	this->shebang = lub_string_dup(prog);
+	inst->shebang = lub_string_dup(prog);
 }
 
-//CLISH_GET(clish_action__get_script, clish_action_t, const char *, script);
-/*--------------------------------------------------------- */
-const char *clish_action__get_shebang(const clish_action_t *this)
-{
-	return this->shebang;
-}
+CLISH_GET_STR(action, shebang);

+ 1 - 1
clish/action/private.h

@@ -9,6 +9,6 @@
  *--------------------------------------------------------- */
 struct clish_action_s {
 	char *script;
-	clish_sym_t *builtin;
+	const clish_sym_t *builtin;
 	char *shebang;
 };

+ 24 - 5
clish/macros.h

@@ -7,14 +7,33 @@
 
 /* Function to get value from structure by name */
 #define _CLISH_GET(obj, type, name) \
-	type clish_##obj##__get_##name(const clish_##obj##_t *obj)
+	type clish_##obj##__get_##name(const clish_##obj##_t *inst)
 #define CLISH_GET(obj, type, name) \
 	_CLISH_GET(obj, type, name) { \
-		assert(obj); \
-		return obj->name; \
+		assert(inst); \
+		return inst->name; \
+	}
+#define _CLISH_GET_STR(obj, name) \
+	_CLISH_GET(obj, const char *, name)
+#define CLISH_GET_STR(obj, name) \
+	CLISH_GET(obj, const char *, name)
+
+/* Function to set value to structure by name */
+#define _CLISH_SET(obj, type, name) \
+	void clish_##obj##__set_##name(clish_##obj##_t *inst, type val)
+#define CLISH_SET(obj, type, name) \
+	_CLISH_SET(obj, type, name) { \
+		assert(inst); \
+		inst->name = val; \
+	}
+#define _CLISH_SET_STR(obj, name) \
+	_CLISH_SET(obj, const char *, name)
+#define CLISH_SET_STR(obj, name) \
+	_CLISH_SET_STR(obj, name) { \
+		assert(inst); \
+		lub_string_free(inst->name); \
+		inst->name = lub_string_dup(val); \
 	}
-
-#define CLISH_SET(obj_type, field_type, field_name, value)
 
 
 

+ 3 - 2
clish/plugin.h

@@ -8,6 +8,7 @@
 #include "config.h"
 #endif /* HAVE_CONFIG_H */
 
+#include "clish/macros.h"
 #include "lub/types.h"
 
 /* Symbol */
@@ -72,11 +73,11 @@ int clish_sym_compare(const void *first, const void *second);
 clish_sym_t *clish_sym_new(const char *name, void *func, int type);
 void clish_sym_free(clish_sym_t *instance);
 void clish_sym__set_func(clish_sym_t *instance, void *func);
-void *clish_sym__get_func(clish_sym_t *instance);
+_CLISH_GET(sym, const void *, func);
 void clish_sym__set_name(clish_sym_t *instance, const char *name);
 char *clish_sym__get_name(clish_sym_t *instance);
 void clish_sym__set_permanent(clish_sym_t *instance, bool_t permanent);
-bool_t clish_sym__get_permanent(clish_sym_t *instance);
+_CLISH_GET(sym, bool_t, permanent);
 void clish_sym__set_plugin(clish_sym_t *instance, clish_plugin_t *plugin);
 clish_plugin_t *clish_sym__get_plugin(clish_sym_t *instance);
 void clish_sym__set_type(clish_sym_t *instance, int type);

+ 2 - 10
clish/plugin/plugin.c

@@ -61,11 +61,7 @@ void clish_sym__set_func(clish_sym_t *this, void *func)
 	this->func = func;
 }
 
-/*--------------------------------------------------------- */
-void *clish_sym__get_func(clish_sym_t *this)
-{
-	return this->func;
-}
+CLISH_GET(sym, const void *, func);
 
 /*--------------------------------------------------------- */
 void clish_sym__set_permanent(clish_sym_t *this, bool_t permanent)
@@ -73,11 +69,7 @@ void clish_sym__set_permanent(clish_sym_t *this, bool_t permanent)
 	this->permanent = permanent;
 }
 
-/*--------------------------------------------------------- */
-bool_t clish_sym__get_permanent(clish_sym_t *this)
-{
-	return this->permanent;
-}
+CLISH_GET(sym, bool_t, permanent);
 
 /*--------------------------------------------------------- */
 void clish_sym__set_name(clish_sym_t *this, const char *name)

+ 1 - 1
clish/shell.h

@@ -220,7 +220,7 @@ clish_sym_t *clish_shell_add_unresolved_sym(clish_shell_t *instance,
 clish_sym_t *clish_shell_get_hook(const clish_shell_t *instance, int type);
 
 /* Hook wrappers */
-void *clish_shell_check_hook(const clish_context_t *clish_context, int type);
+const void *clish_shell_check_hook(const clish_context_t *clish_context, int type);
 CLISH_HOOK_CONFIG(clish_shell_exec_config);
 CLISH_HOOK_LOG(clish_shell_exec_log);
 

+ 4 - 4
clish/shell/shell_execute.c

@@ -302,9 +302,9 @@ stdout_error:
 int clish_shell_exec_action(clish_context_t *context, char **out, bool_t intr)
 {
 	int result = -1;
-	clish_sym_t *sym;
+	const clish_sym_t *sym;
 	char *script;
-	void *func = NULL; /* We don't know the func API at this time */
+	const void *func = NULL; /* We don't know the func API at this time */
 	const clish_action_t *action = clish_context__get_action(context);
 	clish_shell_t *shell = clish_context__get_shell(context);
 	/* Signal vars */
@@ -380,11 +380,11 @@ int clish_shell_exec_action(clish_context_t *context, char **out, bool_t intr)
 }
 
 /*----------------------------------------------------------- */
-void *clish_shell_check_hook(const clish_context_t *clish_context, int type)
+const void *clish_shell_check_hook(const clish_context_t *clish_context, int type)
 {
 	clish_sym_t *sym;
 	clish_shell_t *shell = clish_context__get_shell(clish_context);
-	void *func;
+	const void *func;
 
 	if (!(sym = shell->hooks[type]))
 		return NULL;

+ 3 - 0
lub/string/string.c

@@ -15,7 +15,10 @@ const char *lub_string_esc_quoted = "\\\"";
 /*--------------------------------------------------------- */
 void lub_string_free(char *ptr)
 {
+	if (!ptr)
+		return;
 	free(ptr);
+	ptr = NULL;
 }
 
 /*--------------------------------------------------------- */