Browse Source

clish has -c option to execute commands from command line

git-svn-id: https://klish.googlecode.com/svn/trunk@563 0eaa4687-2ee9-07dd-09d9-bcdd2d2dd5fb
Serj Kalichev 12 years ago
parent
commit
c40e54fc75
2 changed files with 28 additions and 10 deletions
  1. 23 5
      bin/clish.cpp
  2. 5 5
      lub/list.h

+ 23 - 5
bin/clish.cpp

@@ -70,6 +70,7 @@ int main(int argc, char **argv)
 	FILE *outfd = stdout;
 	bool_t istimeout = BOOL_FALSE;
 	int timeout = 0;
+	bool_t cmd = BOOL_FALSE; /* -c option */
 	lub_list_t *cmds; /* Commands defined by -c */
 	lub_list_node_t *iter;
 
@@ -174,6 +175,13 @@ int main(int argc, char **argv)
 			istimeout = BOOL_TRUE;
 			timeout = atoi(optarg);
 			break;
+		case 'c':
+			char *str;
+			cmd = BOOL_TRUE;
+			quiet = BOOL_TRUE;
+			str = strdup(optarg);
+			lub_list_add(cmds, str);
+			break;
 		case 'h':
 			help(0, argv[0]);
 			exit(0);
@@ -184,7 +192,7 @@ int main(int argc, char **argv)
 			break;
 		default:
 			help(-1, argv[0]);
-			exit(-1);
+			goto end;
 			break;
 		}
 	}
@@ -258,8 +266,17 @@ int main(int argc, char **argv)
 		goto end;
 	}
 
-	/* Main loop */
-	result = clish_shell_loop(shell);
+	if (cmd) {
+		/* Iterate cmds */
+		for(iter = lub_list__get_head(cmds);
+			iter; iter = lub_list_node__get_next(iter)) {
+			char *str = (char *)lub_list_node__get_data(iter);
+			clish_shell_forceline(shell, str, NULL);
+		}
+	} else {
+		/* Main loop */
+		result = clish_shell_loop(shell);
+	}
 
 end:
 	/* Cleanup */
@@ -306,11 +323,11 @@ static void help(int status, const char *argv0)
 		printf("\t-v, --version\tPrint version.\n");
 		printf("\t-h, --help\tPrint this help.\n");
 		printf("\t-s <path>, --socket=<path>\tSpecify listen socket "
-			"of the konfd daemon.\n");
+			"\n\t\tof the konfd daemon.\n");
 		printf("\t-l, --lockless\tDon't use locking mechanism.\n");
 		printf("\t-e, --stop-on-error\tStop script execution on error.\n");
 		printf("\t-b, --background\tStart shell using non-interactive mode.\n");
-		printf("\t-q, --quiet\tDisable echo while executing commands from the file stream.\n");
+		printf("\t-q, --quiet\tDisable echo while executing commands\n\t\tfrom the file stream.\n");
 		printf("\t-d, --dry-run\tDon't actually execute ACTION scripts.\n");
 		printf("\t-x <path>, --xml-path=<path>\tPath to XML scheme files.\n");
 		printf("\t-w <view_name>, --view=<view_name>\tSet the startup view.\n");
@@ -320,5 +337,6 @@ static void help(int status, const char *argv0)
 		printf("\t-o, --log\tEnable command logging to syslog's local0.\n");
 		printf("\t-k, --check\tCheck input files for syntax errors only.\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");
 	}
 }

+ 5 - 5
lub/list.h

@@ -45,17 +45,17 @@ lub_list_t *lub_list_new(lub_list_compare_fn compareFn);
 lub_list_node_t *lub_list_node_new(void *data);
 void lub_list_free(lub_list_t *list);
 void lub_list_node_free(lub_list_node_t *node);
-inline lub_list_node_t *lub_list__get_head(lub_list_t *list);
-inline lub_list_node_t *lub_list__get_tail(lub_list_t *list);
-inline lub_list_node_t *lub_list_node__get_prev(lub_list_node_t *node);
-inline lub_list_node_t *lub_list_node__get_next(lub_list_node_t *node);
+lub_list_node_t *lub_list__get_head(lub_list_t *list);
+lub_list_node_t *lub_list__get_tail(lub_list_t *list);
+lub_list_node_t *lub_list_node__get_prev(lub_list_node_t *node);
+lub_list_node_t *lub_list_node__get_next(lub_list_node_t *node);
 void *lub_list_node__get_data(lub_list_node_t *node);
 lub_list_node_t *lub_list_iterator_init(lub_list_t *list);
 lub_list_node_t *lub_list_iterator_next(lub_list_node_t *node);
 lub_list_node_t *lub_list_iterator_prev(lub_list_node_t *node);
 lub_list_node_t *lub_list_add(lub_list_t *list, void *data);
 void lub_list_del(lub_list_t *list, lub_list_node_t *node);
-inline void lub_list_node_copy(lub_list_node_t *dst, lub_list_node_t *src);
+void lub_list_node_copy(lub_list_node_t *dst, lub_list_node_t *src);
 
 _END_C_DECL
 #endif				/* _lub_list_h */