Browse Source

Macros for shell

Serj Kalichev 5 years ago
parent
commit
d3119677da

+ 2 - 2
bin/clish.c

@@ -294,7 +294,7 @@ int main(int argc, char **argv)
 	/* Set logging */
 	if (log) {
 		clish_shell__set_log(shell, log);
-		clish_shell__set_facility(shell, log_facility);
+		clish_shell__set_log_facility(shell, log_facility);
 	}
 	/* Set dry-run */
 	if (dryrun)
@@ -304,7 +304,7 @@ int main(int argc, char **argv)
 		clish_shell__set_canon_out(shell, canon_out);
 	/* Set idle timeout */
 	if (istimeout)
-		clish_shell__set_timeout(shell, timeout);
+		clish_shell__set_idle_timeout(shell, timeout);
 	/* Set history settings */
 	clish_shell__stifle_history(shell, histsize);
 	if (histfile)

+ 147 - 0
clish/param/paramv.c

@@ -0,0 +1,147 @@
+/*
+ * paramv.c
+ *
+ */
+#include "private.h"
+#include "lub/string.h"
+#include "clish/types.h"
+
+#include <assert.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+
+/*--------------------------------------------------------- */
+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,
+	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_defval(res);
+
+	return NULL;
+}
+
+/*--------------------------------------------------------- */
+unsigned int clish_paramv__get_count(const clish_paramv_t * this)
+{
+	return this->paramc;
+}
+

+ 30 - 0
clish/pargv/parg.c

@@ -0,0 +1,30 @@
+/*
+ * parg.c
+ */
+#include "private.h"
+#include "lub/string.h"
+#include "lub/argv.h"
+#include "lub/system.h"
+
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+#include <assert.h>
+
+CLISH_GET_STR(parg, value);
+
+/*--------------------------------------------------------- */
+_CLISH_GET_STR(parg, name)
+{
+	if (!inst)
+		return NULL;
+	return clish_param__get_name(inst->param);
+}
+
+/*--------------------------------------------------------- */
+_CLISH_GET(parg, clish_ptype_t *, ptype)
+{
+	if (!inst)
+		return NULL;
+	return clish_param__get_ptype(inst->param);
+}

+ 79 - 0
clish/plugin/sym.c

@@ -0,0 +1,79 @@
+/*
+ * plugin.c
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif /* HAVE_CONFIG_H */
+
+#include "private.h"
+#include "lub/string.h"
+#include "lub/list.h"
+
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+#include <assert.h>
+#ifdef HAVE_DLFCN_H
+#include <dlfcn.h>
+#endif
+
+/*--------------------------------------------------------- */
+int clish_sym_compare(const void *first, const void *second)
+{
+	const clish_sym_t *f = (const clish_sym_t *)first;
+	const clish_sym_t *s = (const clish_sym_t *)second;
+
+	return strcmp(f->name, s->name);
+}
+
+/*--------------------------------------------------------- */
+clish_sym_t *clish_sym_new(const char *name, void *func, int type)
+{
+	clish_sym_t *this;
+
+	this = malloc(sizeof(*this));
+	this->name = lub_string_dup(name);
+	this->func = func;
+	this->type = type;
+	this->api = CLISH_SYM_API_SIMPLE;
+	this->permanent = BOOL_FALSE;
+
+	return this;
+}
+
+/*--------------------------------------------------------- */
+void clish_sym_free(clish_sym_t *this)
+{
+	if (!this)
+		return;
+	lub_string_free(this->name);
+	free(this);
+}
+
+/*--------------------------------------------------------- */
+int clish_sym_clone(clish_sym_t *dst, clish_sym_t *src)
+{
+	char *name;
+
+	if (!dst || !src)
+		return -1;
+	name = dst->name;
+	*dst = *src;
+	dst->name = name;
+
+	return 0;
+}
+
+CLISH_SET(sym, const void *, func);
+CLISH_GET(sym, const void *, func);
+CLISH_SET(sym, bool_t, permanent);
+CLISH_GET(sym, bool_t, permanent);
+CLISH_SET_STR(sym, name);
+CLISH_GET_STR(sym, name);
+CLISH_SET(sym, clish_plugin_t *, plugin);
+CLISH_GET(sym, clish_plugin_t *, plugin);
+CLISH_SET(sym, int, type);
+CLISH_GET(sym, int, type);
+CLISH_SET(sym, clish_sym_api_e, api);
+CLISH_GET(sym, clish_sym_api_e, api);

+ 24 - 27
clish/shell.h

@@ -143,11 +143,33 @@ int clish_shell_rmfifo(clish_shell_t * instance, const char *name);
 /*-----------------
  * attributes
  *----------------- */
+_CLISH_GET_STR(shell, overview);
+_CLISH_SET_STR(shell, lockfile);
+_CLISH_GET_STR(shell, lockfile);
+_CLISH_SET_STR(shell, default_shebang);
+_CLISH_GET_STR(shell, default_shebang);
+_CLISH_SET(shell, unsigned int, idle_timeout);
+_CLISH_SET(shell, unsigned int, wdog_timeout);
+_CLISH_GET(shell, unsigned int, wdog_timeout);
+_CLISH_GET(shell, unsigned int, depth);
+_CLISH_SET(shell, int, log_facility);
+_CLISH_GET(shell, int, log_facility);
+_CLISH_GET(shell, konf_client_t *, client);
+_CLISH_GET(shell, struct passwd *, user);
+_CLISH_SET(shell, clish_shell_state_e, state);
+_CLISH_GET(shell, clish_shell_state_e, state);
+_CLISH_SET(shell, bool_t, interactive);
+_CLISH_GET(shell, bool_t, interactive);
+_CLISH_SET(shell, bool_t, log);
+_CLISH_GET(shell, bool_t, log);
+_CLISH_SET(shell, bool_t, dryrun);
+_CLISH_GET(shell, bool_t, dryrun);
+_CLISH_SET(shell, bool_t, canon_out);
+_CLISH_GET(shell, bool_t, canon_out);
+
 clish_view_t *clish_shell__get_view(const clish_shell_t * instance);
-unsigned int clish_shell__get_depth(const clish_shell_t * instance);
 clish_view_t *clish_shell__set_depth(clish_shell_t *instance, unsigned int depth);
 const char *clish_shell__get_viewid(const clish_shell_t * instance);
-const char *clish_shell__get_overview(const clish_shell_t * instance);
 tinyrl_t *clish_shell__get_tinyrl(const clish_shell_t * instance);
 void clish_shell__set_pwd(clish_shell_t *instance, const char * line,
 	clish_view_t * view, const char * viewid, clish_context_t *context);
@@ -163,47 +185,22 @@ char *clish_shell__get_pwd_full(const clish_shell_t * instance,
 	unsigned int depth);
 clish_view_t *clish_shell__get_pwd_view(const clish_shell_t * instance,
 	unsigned int index);
-konf_client_t *clish_shell__get_client(const clish_shell_t * instance);
 FILE *clish_shell__get_istream(const clish_shell_t * instance);
 FILE *clish_shell__get_ostream(const clish_shell_t * instance);
-void clish_shell__set_lockfile(clish_shell_t * instance, const char * path);
-char * clish_shell__get_lockfile(clish_shell_t * instance);
 int clish_shell__set_socket(clish_shell_t * instance, const char * path);
 int clish_shell_load_scheme(clish_shell_t * instance, const char * xml_path, const char *xslt_path);
 int clish_shell_loop(clish_shell_t * instance);
-clish_shell_state_e clish_shell__get_state(const clish_shell_t * instance);
-void clish_shell__set_state(clish_shell_t * instance,
-	clish_shell_state_e state);
 void clish_shell__set_startup_view(clish_shell_t * instance, const char * viewname);
 void clish_shell__set_startup_viewid(clish_shell_t * instance, const char * viewid);
-void clish_shell__set_default_shebang(clish_shell_t * instance, const char * shebang);
-const char * clish_shell__get_default_shebang(const clish_shell_t * instance);
-void clish_shell__set_interactive(clish_shell_t * instance, bool_t interactive);
-bool_t clish_shell__get_interactive(const clish_shell_t * instance);
 bool_t clish_shell__get_utf8(const clish_shell_t * instance);
 void clish_shell__set_utf8(clish_shell_t * instance, bool_t utf8);
-void clish_shell__set_timeout(clish_shell_t *instance, unsigned int timeout);
 char *clish_shell__get_line(clish_context_t *context);
 char *clish_shell__get_full_line(clish_context_t *context);
 char *clish_shell__get_params(clish_context_t *context);
-
-/* Log functions */
-void clish_shell__set_log(clish_shell_t *instance, bool_t log);
-bool_t clish_shell__get_log(const clish_shell_t *instance);
-void clish_shell__set_facility(clish_shell_t *instance, int facility);
-int clish_shell__get_facility(clish_shell_t *instance);
-
 int clish_shell_wdog(clish_shell_t *instance);
-_CLISH_SET(shell, unsigned int, wdog_timeout);
-_CLISH_GET(shell, unsigned int, wdog_timeout);
 int clish_shell__save_history(const clish_shell_t *instance, const char *fname);
 int clish_shell__restore_history(clish_shell_t *instance, const char *fname);
 void clish_shell__stifle_history(clish_shell_t *instance, unsigned int stifle);
-struct passwd *clish_shell__get_user(clish_shell_t *instance);
-void clish_shell__set_dryrun(clish_shell_t *instance, bool_t dryrun);
-bool_t clish_shell__get_dryrun(const clish_shell_t *instance);
-void clish_shell__set_canon_out(clish_shell_t *instance, bool_t canon_out);
-bool_t clish_shell__get_canon_out(const clish_shell_t *instance);
 
 /* Plugin functions */
 clish_plugin_t * clish_shell_find_plugin(clish_shell_t *instance,

+ 7 - 40
clish/shell/shell_execute.c

@@ -92,7 +92,7 @@ int clish_shell_execute(clish_context_t *context, char **out)
 	clish_shell_t *this = clish_context__get_shell(context);
 	const clish_command_t *cmd = clish_context__get_cmd(context);
 	int result = 0;
-	char *lock_path = clish_shell__get_lockfile(this);
+	const char *lock_path = clish_shell__get_lockfile(this);
 	int lock_fd = -1;
 	clish_view_t *cur_view = clish_shell__get_view(this);
 	unsigned int saved_wdog_timeout = this->wdog_timeout;
@@ -437,42 +437,9 @@ int clish_shell_rmfifo(clish_shell_t * this, const char *name)
 	return unlink(name);
 }
 
-/*-------------------------------------------------------- */
-void clish_shell__set_log(clish_shell_t *this, bool_t log)
-{
-	assert(this);
-	this->log = log;
-}
-
-/*-------------------------------------------------------- */
-bool_t clish_shell__get_log(const clish_shell_t *this)
-{
-	assert(this);
-	return this->log;
-}
-
-/*-------------------------------------------------------- */
-void clish_shell__set_dryrun(clish_shell_t *this, bool_t dryrun)
-{
-	this->dryrun = dryrun;
-}
-
-/*-------------------------------------------------------- */
-bool_t clish_shell__get_dryrun(const clish_shell_t *this)
-{
-	return this->dryrun;
-}
-
-/*-------------------------------------------------------- */
-void clish_shell__set_canon_out(clish_shell_t *this, bool_t canon_out)
-{
-	this->canon_out = canon_out;
-}
-
-/*-------------------------------------------------------- */
-bool_t clish_shell__get_canon_out(const clish_shell_t *this)
-{
-	return this->canon_out;
-}
-
-/*----------------------------------------------------------- */
+CLISH_SET(shell, bool_t, log);
+CLISH_GET(shell, bool_t, log);
+CLISH_SET(shell, bool_t, dryrun);
+CLISH_GET(shell, bool_t, dryrun);
+CLISH_SET(shell, bool_t, canon_out);
+CLISH_GET(shell, bool_t, canon_out);

+ 1 - 8
clish/shell/shell_misc.c

@@ -7,11 +7,4 @@
 
 #include "private.h"
 
-/*--------------------------------------------------------- */
-const char *clish_shell__get_overview(const clish_shell_t *this)
-{
-	assert(this);
-	return this->overview;
-}
-
-/*--------------------------------------------------------- */
+CLISH_GET_STR(shell, overview);

+ 1 - 7
clish/shell/shell_new.c

@@ -215,10 +215,4 @@ void clish_shell_delete(clish_shell_t *this)
 	free(this);
 }
 
-/*--------------------------------------------------------- */
-struct passwd *clish_shell__get_user(clish_shell_t * this)
-{
-	return this->user;
-}
-
-/*-------------------------------------------------------- */
+CLISH_GET(shell, struct passwd *, user);

+ 2 - 15
clish/shell/shell_parse.c

@@ -320,18 +320,5 @@ clish_pargv_status_e clish_shell_parse_pargv(clish_pargv_t *pargv,
 	return CLISH_LINE_OK;
 }
 
-/*----------------------------------------------------------- */
-clish_shell_state_e clish_shell__get_state(const clish_shell_t *this)
-{
-	return this->state;
-}
-
-/*----------------------------------------------------------- */
-void clish_shell__set_state(clish_shell_t *this,
-	clish_shell_state_e state)
-{
-	assert(this);
-	this->state = state;
-}
-
-/*----------------------------------------------------------- */
+CLISH_SET(shell, clish_shell_state_e, state);
+CLISH_GET(shell, clish_shell_state_e, state);

+ 5 - 43
clish/shell/shell_pwd.c

@@ -164,33 +164,6 @@ clish_view_t *clish_shell__get_pwd_view(const clish_shell_t * this, unsigned int
 	return this->pwdv[index]->view;
 }
 
-/*--------------------------------------------------------- */
-konf_client_t *clish_shell__get_client(const clish_shell_t * this)
-{
-	return this->client;
-}
-
-/*--------------------------------------------------------- */
-void clish_shell__set_lockfile(clish_shell_t * this, const char * path)
-{
-	if (!this)
-		return;
-
-	lub_string_free(this->lockfile);
-	this->lockfile = NULL;
-	if (path)
-		this->lockfile = lub_string_dup(path);
-}
-
-/*--------------------------------------------------------- */
-char * clish_shell__get_lockfile(clish_shell_t * this)
-{
-	if (!this)
-		return NULL;
-
-	return this->lockfile;
-}
-
 /*--------------------------------------------------------- */
 int clish_shell__set_socket(clish_shell_t * this, const char * path)
 {
@@ -203,19 +176,8 @@ int clish_shell__set_socket(clish_shell_t * this, const char * path)
 	return 0;
 }
 
-/*--------------------------------------------------------- */
-void clish_shell__set_facility(clish_shell_t *this, int facility)
-{
-	if (!this)
-		return;
-	this->log_facility = facility;
-}
-
-/*--------------------------------------------------------- */
-int clish_shell__get_facility(clish_shell_t *this)
-{
-	if (!this)
-		return LOG_LOCAL0;
-
-	return this->log_facility;
-}
+CLISH_SET_STR(shell, lockfile);
+CLISH_GET_STR(shell, lockfile);
+CLISH_SET(shell, int, log_facility);
+CLISH_GET(shell, int, log_facility);
+CLISH_GET(shell, konf_client_t *, client);

+ 4 - 17
clish/shell/shell_startup.c

@@ -64,22 +64,6 @@ void clish_shell__set_startup_viewid(clish_shell_t *this, const char *viewid)
 	clish_command__force_viewid(this->startup, viewid);
 }
 
-/*----------------------------------------------------------- */
-void clish_shell__set_default_shebang(clish_shell_t *this, const char *shebang)
-{
-	assert(this);
-	lub_string_free(this->default_shebang);
-	this->default_shebang = lub_string_dup(shebang);
-}
-
-/*----------------------------------------------------------- */
-const char * clish_shell__get_default_shebang(const clish_shell_t *this)
-{
-	assert(this);
-	return this->default_shebang;
-}
-
-
 
 /*-------------------------------------------------------- */
 /* Resolve PTYPE for given PARAM.
@@ -347,4 +331,7 @@ int clish_shell_prepare(clish_shell_t *this)
 	return 0;
 }
 
-/*----------------------------------------------------------- */
+CLISH_SET_STR(shell, default_shebang);
+CLISH_GET_STR(shell, default_shebang);
+
+

+ 3 - 21
clish/shell/shell_tinyrl.c

@@ -558,19 +558,6 @@ FILE * clish_shell__get_ostream(const clish_shell_t * this)
 	return tinyrl__get_ostream(this->tinyrl);
 }
 
-/*-------------------------------------------------------- */
-void clish_shell__set_interactive(clish_shell_t * this, bool_t interactive)
-{
-	assert(this);
-	this->interactive = interactive;
-}
-
-/*-------------------------------------------------------- */
-bool_t clish_shell__get_interactive(const clish_shell_t * this)
-{
-	assert(this);
-	return this->interactive;
-}
 
 /*-------------------------------------------------------- */
 bool_t clish_shell__get_utf8(const clish_shell_t * this)
@@ -586,13 +573,6 @@ void clish_shell__set_utf8(clish_shell_t * this, bool_t utf8)
 	tinyrl__set_utf8(this->tinyrl, utf8);
 }
 
-/*-------------------------------------------------------- */
-void clish_shell__set_timeout(clish_shell_t *this, unsigned int timeout)
-{
-	assert(this);
-	this->idle_timeout = timeout;
-}
-
 /*--------------------------------------------------------- */
 tinyrl_t *clish_shell__get_tinyrl(const clish_shell_t * this)
 {
@@ -617,4 +597,6 @@ void clish_shell__stifle_history(clish_shell_t *this, unsigned int stifle)
 	tinyrl__stifle_history(this->tinyrl, stifle);
 }
 
-/*-------------------------------------------------------- */
+CLISH_SET(shell, unsigned int, idle_timeout);
+CLISH_SET(shell, bool_t, interactive);
+CLISH_GET(shell, bool_t, interactive);

+ 2 - 8
clish/shell/shell_view.c

@@ -46,13 +46,6 @@ clish_view_t *clish_shell__get_view(const clish_shell_t * this)
 	return this->pwdv[this->depth]->view;
 }
 
-/*--------------------------------------------------------- */
-unsigned int clish_shell__get_depth(const clish_shell_t *this)
-{
-	assert(this);
-	return this->depth;
-}
-
 /*--------------------------------------------------------- */
 clish_view_t *clish_shell__set_depth(clish_shell_t *this, unsigned int depth)
 {
@@ -67,4 +60,5 @@ clish_view_t *clish_shell__set_depth(clish_shell_t *this, unsigned int depth)
 	return tmp;
 }
 
-/*--------------------------------------------------------- */
+CLISH_GET(shell, unsigned int, depth);
+

+ 1 - 1
clish/shell/shell_xml.c

@@ -619,7 +619,7 @@ static int process_startup(clish_shell_t *shell, clish_xmlnode_t *element,
 	if (timeout) {
 		unsigned int to = 0;
 		lub_conv_atoui(timeout, &to, 0);
-		clish_shell__set_timeout(shell, to);
+		clish_shell__set_idle_timeout(shell, to);
 	}
 
 	/* lock field */

+ 1 - 1
plugins/clish/hook_log.c

@@ -24,7 +24,7 @@ CLISH_HOOK_LOG(clish_hook_log)
 	/* Initialization */
 	if (!line) {
 		openlog(SYSLOG_IDENT, LOG_PID,
-			clish_shell__get_facility(this));
+			clish_shell__get_log_facility(this));
 		return 0;
 	}