Browse Source

Print commands in canonical form - with prepended spaces indicates depth

Serj Kalichev 6 years ago
parent
commit
47a3e43610

+ 13 - 1
bin/clish.c

@@ -65,6 +65,7 @@ int main(int argc, char **argv)
 	int log_facility = LOG_LOCAL0;
 	bool_t dryrun = BOOL_FALSE;
 	bool_t dryrun_config = BOOL_FALSE;
+	bool_t canon_out = BOOL_FALSE;
 	const char *xml_path = getenv("CLISH_PATH");
 	const char *view = getenv("CLISH_VIEW");
 	const char *viewid = getenv("CLISH_VIEWID");
@@ -85,7 +86,7 @@ int main(int argc, char **argv)
 	struct sigaction sigpipe_act;
 	sigset_t sigpipe_set;
 
-	static const char *shortopts = "hvs:ledx:w:i:bqu8oO:kt:c:f:z:p:";
+	static const char *shortopts = "hvs:ledx:w:i:bqu8oO:kKt:c:f:z:p:";
 #ifdef HAVE_GETOPT_LONG
 	static const struct option longopts[] = {
 		{"help",	0, NULL, 'h'},
@@ -104,6 +105,7 @@ int main(int argc, char **argv)
 		{"log",		0, NULL, 'o'},
 		{"facility",	1, NULL, 'O'},
 		{"check",	0, NULL, 'k'},
+		{"canon-out",	0, NULL, 'K'},
 		{"timeout",	1, NULL, 't'},
 		{"command",	1, NULL, 'c'},
 		{"histfile",	1, NULL, 'f'},
@@ -188,6 +190,12 @@ int main(int argc, char **argv)
 			dryrun = BOOL_TRUE;
 			dryrun_config = BOOL_TRUE;
 			break;
+		case 'K':
+			lockless = BOOL_TRUE;
+			dryrun = BOOL_TRUE;
+			dryrun_config = BOOL_TRUE;
+			canon_out = BOOL_TRUE;
+			break;
 		case 't':
 			istimeout = BOOL_TRUE;
 			timeout = 0;
@@ -291,6 +299,9 @@ int main(int argc, char **argv)
 	/* Set dry-run */
 	if (dryrun)
 		clish_shell__set_dryrun(shell, dryrun);
+	/* Set canonical output */
+	if (canon_out)
+		clish_shell__set_canon_out(shell, canon_out);
 	/* Set idle timeout */
 	if (istimeout)
 		clish_shell__set_timeout(shell, timeout);
@@ -420,6 +431,7 @@ static void help(int status, const char *argv0)
 		printf("\t-o, --log\tEnable command logging to syslog's.\n");
 		printf("\t-O, --facility\tSyslog facility. Default is LOCAL0.\n");
 		printf("\t-k, --check\tCheck input files for syntax errors only.\n");
+		printf("\t-K, --canon-out\tCheck input files for syntax and print commands\n\t\tin canonical form - prepended with spaces indicates depth.\n");
 		printf("\t-t <timeout>, --timeout=<timeout>\tIdle timeout in seconds.\n");
 		printf("\t-c <command>, --command=<command>\tExecute specified command(s).\n\t\tMultiple options are possible.\n");
 		printf("\t-f <path>, --histfile=<path>\tFile to save command history.\n");

+ 2 - 0
clish/command.h

@@ -85,6 +85,8 @@ void clish_command__set_alias_view(clish_command_t * instance,
 const char * clish_command__get_alias_view(const clish_command_t * instance);
 void clish_command__set_dynamic(clish_command_t * instance, bool_t dynamic);
 bool_t clish_command__get_dynamic(const clish_command_t * instance);
+void clish_command__set_internal(clish_command_t * instance, bool_t internal);
+bool_t clish_command__get_internal(const clish_command_t * instance);
 bool_t clish_command__get_interrupt(const clish_command_t * instance);
 void clish_command__set_interrupt(clish_command_t * instance, bool_t interrupt);
 void clish_command__set_access(clish_command_t *instance, const char *access);

+ 13 - 0
clish/command/command.c

@@ -43,6 +43,7 @@ clish_command_init(clish_command_t *this, const char *name, const char *text)
 	this->lock = BOOL_TRUE;
 	this->interrupt = BOOL_FALSE;
 	this->dynamic = BOOL_FALSE;
+	this->internal = BOOL_FALSE;
 	this->access = NULL;
 }
 
@@ -451,6 +452,18 @@ bool_t clish_command__get_dynamic(const clish_command_t * this)
 	return this->dynamic;
 }
 
+/*--------------------------------------------------------- */
+void clish_command__set_internal(clish_command_t * this, bool_t internal)
+{
+	this->internal = internal;
+}
+
+/*--------------------------------------------------------- */
+bool_t clish_command__get_internal(const clish_command_t * this)
+{
+	return this->internal;
+}
+
 /*--------------------------------------------------------- */
 const clish_command_t * clish_command__get_cmd(const clish_command_t * this)
 {

+ 1 - 0
clish/command/private.h

@@ -28,4 +28,5 @@ struct clish_command_s {
 	bool_t lock;
 	bool_t interrupt;
 	bool_t dynamic; /* Is command dynamically created */
+	bool_t internal; /* Is command internal? Like the "startup" */
 };

+ 2 - 0
clish/shell.h

@@ -199,6 +199,8 @@ 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,

+ 1 - 0
clish/shell/private.h

@@ -90,6 +90,7 @@ struct clish_shell_s {
 	int log_facility; /* Syslog facility */
 	bool_t dryrun; /* Is this a dry-running */
 	bool_t default_plugin; /* Use or not default plugin */
+	bool_t canon_out; /* Output every command in canonical form (with pre-spaces) */
 
 	/* Plugins and symbols */
 	lub_list_t *plugins; /* List of plugins */

+ 27 - 0
clish/shell/shell_execute.c

@@ -138,6 +138,21 @@ int clish_shell_execute(clish_context_t *context, char **out)
 		lub_string_free(full_line);
 	}
 
+	if (clish_shell__get_canon_out(this) &&
+		!clish_command__get_internal(cmd)) {
+		char *space = NULL;
+		char *full_line = clish_shell__get_full_line(context);
+		if (this->depth > 0) {
+			space = malloc(this->depth + 1);
+			memset(space, ' ', this->depth);
+			space[this->depth] = '\0';
+		}
+		printf("%s%s\n", space ? space : "", full_line);
+		lub_string_free(full_line);
+		if (space)
+			free(space);
+	}
+
 	/* Unlock the lockfile */
 	if (lock_fd != -1)
 		clish_shell_unlock(lock_fd);
@@ -448,4 +463,16 @@ 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;
+}
+
 /*----------------------------------------------------------- */

+ 1 - 0
clish/shell/shell_new.c

@@ -77,6 +77,7 @@ static void clish_shell_init(clish_shell_t * this,
 	this->dryrun = BOOL_FALSE; /* Disable dry-run by default */
 	this->user = lub_db_getpwuid(getuid()); /* Get user information */
 	this->default_plugin = BOOL_TRUE; /* Load default plugin by default */
+	this->canon_out = BOOL_FALSE; /* A canonical output is needed in special cases only */
 
 	/* Create template (string) for FIFO name generation */
 	snprintf(template, sizeof(template),

+ 1 - 0
clish/shell/shell_xml.c

@@ -604,6 +604,7 @@ static int process_startup(clish_shell_t *shell, clish_xmlnode_t *element,
 	/* create a command with NULL help */
 	cmd = clish_view_new_command(v, "startup", NULL);
 	clish_command__set_lock(cmd, BOOL_FALSE);
+	clish_command__set_internal(cmd, BOOL_TRUE);
 
 	/* reference the next view */
 	clish_command__set_viewname(cmd, view);