Browse Source

klish: Can process multiply -c options

Serj Kalichev 2 years ago
parent
commit
2356aa28d9
3 changed files with 17 additions and 7 deletions
  1. 6 2
      bin/klish/klish.c
  2. 8 4
      bin/klish/opts.c
  3. 3 1
      bin/klish/private.h

+ 6 - 2
bin/klish/klish.c

@@ -15,6 +15,7 @@
 #include <faux/faux.h>
 #include <faux/str.h>
 #include <faux/msg.h>
+#include <faux/list.h>
 
 #include <klish/ktp.h>
 #include <klish/ktp_session.h>
@@ -34,6 +35,8 @@ int main(int argc, char **argv)
 	struct options *opts = NULL;
 	int unix_sock = -1;
 	ktp_session_t *ktp = NULL;
+	faux_list_node_t *iter = NULL;
+	const char *line = NULL;
 
 	// Parse command line options
 	opts = opts_init();
@@ -57,11 +60,12 @@ int main(int argc, char **argv)
 	ktp_session_set_stdout_cb(ktp, stdout_cb, NULL);
 	ktp_session_set_stderr_cb(ktp, stderr_cb, NULL);
 
-	{
+	iter = faux_list_head(opts->commands);
+	while ((line = faux_list_each(&iter))) {
 		faux_error_t *error = faux_error_new();
 		int retcode = -1;
 
-		if (ktp_session_req_cmd(ktp, opts->line, &retcode, error)) {
+		if (ktp_session_req_cmd(ktp, line, &retcode, error)) {
 			fprintf(stderr, "Retcode: %d\n", retcode);
 		} else {
 			fprintf(stderr, "Error:\n");

+ 8 - 4
bin/klish/opts.c

@@ -12,6 +12,7 @@
 
 #include <faux/faux.h>
 #include <faux/str.h>
+#include <faux/list.h>
 
 #include <klish/ktp_session.h>
 
@@ -30,7 +31,11 @@ struct options *opts_init(void)
 	// Initialize
 	opts->verbose = BOOL_FALSE;
 	opts->unix_socket_path = faux_str_dup(KLISH_DEFAULT_UNIX_SOCKET_PATH);
-	opts->line = NULL;
+
+	// Don't free command list because elements are the pointers to
+	// command line options and don't need to be freed().
+	opts->commands = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_NONUNIQUE,
+		NULL, NULL, NULL);
 
 	return opts;
 }
@@ -43,7 +48,7 @@ void opts_free(struct options *opts)
 	if (!opts)
 		return;
 	faux_str_free(opts->unix_socket_path);
-	faux_str_free(opts->line);
+	faux_list_free(opts->commands);
 
 	faux_free(opts);
 }
@@ -82,8 +87,7 @@ int opts_parse(int argc, char *argv[], struct options *opts)
 			_exit(0);
 			break;
 		case 'c':
-			faux_str_free(opts->line);
-			opts->line = faux_str_dup(optarg);
+			faux_list_add(opts->commands, optarg);
 			break;
 		default:
 			help(-1, argv[0]);

+ 3 - 1
bin/klish/private.h

@@ -1,3 +1,5 @@
+#include <faux/list.h>
+
 
 #ifndef VERSION
 #define VERSION "1.0.0"
@@ -9,7 +11,7 @@
 struct options {
 	bool_t verbose;
 	char *unix_socket_path;
-	char *line;
+	faux_list_t *commands;
 };
 
 // Options