Browse Source

klish: User can specify input files on command line

Serj Kalichev 2 years ago
parent
commit
4766721c0d
2 changed files with 21 additions and 0 deletions
  1. 20 0
      bin/klish/opts.c
  2. 1 0
      bin/klish/private.h

+ 20 - 0
bin/klish/opts.c

@@ -36,6 +36,8 @@ struct options *opts_init(void)
 	// command line options and don't need to be freed().
 	opts->commands = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_NONUNIQUE,
 		NULL, NULL, NULL);
+	opts->files = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_NONUNIQUE,
+		NULL, NULL, NULL);
 
 	return opts;
 }
@@ -49,6 +51,7 @@ void opts_free(struct options *opts)
 		return;
 	faux_str_free(opts->unix_socket_path);
 	faux_list_free(opts->commands);
+	faux_list_free(opts->files);
 
 	faux_free(opts);
 }
@@ -96,6 +99,23 @@ int opts_parse(int argc, char *argv[], struct options *opts)
 		}
 	}
 
+	// Input files
+	if(optind < argc) {
+		int i;
+		for (i = argc - 1; i >= optind; i--)
+			faux_list_add(opts->files, argv[i]);
+	}
+
+	// Validate options
+
+	// Commands specified by '-c' option can't coexist with input files
+	if (!faux_list_is_empty(opts->commands) &&
+		!faux_list_is_empty(opts->files)) {
+		fprintf(stderr, "Error: Commands specified by '-c' can't coexist "
+			"with input files\n");
+		_exit(-1);
+	}
+
 	return 0;
 }
 

+ 1 - 0
bin/klish/private.h

@@ -12,6 +12,7 @@ struct options {
 	bool_t verbose;
 	char *unix_socket_path;
 	faux_list_t *commands;
+	faux_list_t *files;
 };
 
 // Options