cli: Extract log argument parsing into cli_common

Move log argument parsing into `cli_common.c` as it's also useful for
other CLIs. Also add a NULL-check for the strdup() return value.

Change-Id: I9b1c9ae2e490edd3560b11b84fddd79e4d396e1d
Signed-off-by: Nico Huber <nico.h@gmx.de>
Reviewed-on: https://review.sourcearcade.org/c/flashprog/+/72986
diff --git a/cli_common.c b/cli_common.c
index a772057..787e47d 100644
--- a/cli_common.c
+++ b/cli_common.c
@@ -35,6 +35,34 @@
 	return 0;
 }
 
+int cli_parse_log_args(struct log_args *const args, const int opt, const char *const optarg)
+{
+	switch (opt) {
+	case OPTION_VERBOSE:
+		args->screen_level++;
+		if (args->screen_level > args->logfile_level)
+			args->logfile_level = args->screen_level;
+		break;
+	case OPTION_LOGFILE:
+		if (cli_check_filename(optarg, "log"))
+			return 1;
+
+		if (args->logfile) {
+			fprintf(stderr, "Warning: -o/--output specified multiple times.\n");
+			free(args->logfile);
+		}
+
+		args->logfile = strdup(optarg);
+		if (!args->logfile) {
+			fprintf(stderr, "Out of memory!\n");
+			return 2;
+		}
+		break;
+	}
+
+	return 0;
+}
+
 int cli_parse_flash_args(struct flash_args *const args, const int opt, const char *const optarg)
 {
 	switch (opt) {