All of lore.kernel.org
 help / color / mirror / Atom feed
From: Andreas Fenkart <afenkart@gmail.com>
To: u-boot@lists.denx.de
Subject: [U-Boot] [PATCH 3/5] tools: env: introduce setenv/printenv argument structs
Date: Tue, 24 Nov 2015 14:21:13 +0100	[thread overview]
Message-ID: <1448371275-900-4-git-send-email-andreas.fenkart@dev.digitalstrom.org> (raw)
In-Reply-To: <1448371275-900-1-git-send-email-andreas.fenkart@dev.digitalstrom.org>

goal is to use getopt for all argument parsing instead of adhoc
parsing in fw_getenv/fw_setenv functions

Signed-off-by: Andreas Fenkart <andreas.fenkart@dev.digitalstrom.org>
---
 tools/env/fw_env.h      |   9 ++++
 tools/env/fw_env_main.c | 111 +++++++++++++++++++++++++++++++-----------------
 2 files changed, 81 insertions(+), 39 deletions(-)

diff --git a/tools/env/fw_env.h b/tools/env/fw_env.h
index aff471b..4ac7de4 100644
--- a/tools/env/fw_env.h
+++ b/tools/env/fw_env.h
@@ -52,6 +52,15 @@
 	"bootm"
 #endif
 
+struct printenv_args {
+};
+extern struct printenv_args printenv_args;
+
+struct setenv_args {
+	char *script_file;
+};
+extern struct setenv_args setenv_args;
+
 extern int   fw_printenv(int argc, char *argv[]);
 extern char *fw_getenv  (char *name);
 extern int fw_setenv  (int argc, char *argv[]);
diff --git a/tools/env/fw_env_main.c b/tools/env/fw_env_main.c
index ce50d58..f45c94a 100644
--- a/tools/env/fw_env_main.c
+++ b/tools/env/fw_env_main.c
@@ -45,6 +45,9 @@ static struct option long_options[] = {
 	{NULL, 0, NULL, 0}
 };
 
+struct printenv_args printenv_args;
+struct setenv_args setenv_args;
+
 void usage(void)
 {
 
@@ -72,33 +75,11 @@ void usage(void)
 	);
 }
 
-int main(int argc, char *argv[])
+int parse_printenv_args(int argc, char *argv[])
 {
-	char *p;
-	char *cmdname = *argv;
-	char *script_file = NULL;
 	int c;
-	const char *lockname = "/var/lock/" CMD_PRINTENV ".lock";
-	int lockfd = -1;
-	int retval = EXIT_SUCCESS;
-
-	lockfd = open(lockname, O_WRONLY | O_CREAT | O_TRUNC, 0666);
-	if (-1 == lockfd) {
-		fprintf(stderr, "Error opening lock file %s\n", lockname);
-		return EXIT_FAILURE;
-	}
-
-	if (-1 == flock(lockfd, LOCK_EX)) {
-		fprintf(stderr, "Error locking file %s\n", lockname);
-		close(lockfd);
-		return EXIT_FAILURE;
-	}
 
-	if ((p = strrchr (cmdname, '/')) != NULL) {
-		cmdname = p + 1;
-	}
-
-	while ((c = getopt_long (argc, argv, "a:ns:h",
+	while ((c = getopt_long (argc, argv, "a:nh",
 		long_options, NULL)) != EOF) {
 		switch (c) {
 		case 'a':
@@ -107,40 +88,92 @@ int main(int argc, char *argv[])
 		case 'n':
 			/* handled in fw_printenv */
 			break;
+		case 'h':
+			usage();
+			exit(EXIT_SUCCESS);
+			break;
+		default: /* '?' */
+			usage();
+			exit(EXIT_FAILURE);
+			break;
+		}
+	}
+	return 0;
+}
+
+int parse_setenv_args(int argc, char *argv[])
+{
+	int c;
+	while ((c = getopt_long (argc, argv, "a:s:h",
+		long_options, NULL)) != EOF) {
+		switch (c) {
+		case 'a':
+			/* AES key, handled later */
+			break;
 		case 's':
-			script_file = optarg;
+			setenv_args.script_file = optarg;
 			break;
 		case 'h':
 			usage();
-			goto exit;
+			exit(EXIT_SUCCESS);
+			break;
 		default: /* '?' */
-			fprintf(stderr, "Try `%s --help' for more information."
-				"\n", cmdname);
-			retval = EXIT_FAILURE;
-			goto exit;
+			usage();
+			exit(EXIT_FAILURE);
+			break;
 		}
 	}
+	return 0;
+}
+
+int main(int argc, char *argv[])
+{
+	char *cmdname = *argv;
+	const char *lockname = "/var/lock/" CMD_PRINTENV ".lock";
+	int lockfd = -1;
+	int retval = EXIT_SUCCESS;
+
+	if (strrchr(cmdname, '/') != NULL)
+		cmdname = strrchr(cmdname, '/') + 1;
+
+	if (strcmp(cmdname, CMD_PRINTENV) == 0) {
+		if (parse_printenv_args(argc, argv))
+			exit(EXIT_FAILURE);
+	} else if (strcmp(cmdname, CMD_SETENV) == 0) {
+		if (parse_setenv_args(argc, argv))
+			exit(EXIT_FAILURE);
+	} else {
+		fprintf(stderr,
+			"Identity crisis - may be called as `%s' or as `%s' but not as `%s'\n",
+			CMD_PRINTENV, CMD_SETENV, cmdname);
+		exit(EXIT_FAILURE);
+	}
+
+	lockfd = open(lockname, O_WRONLY | O_CREAT | O_TRUNC, 0666);
+	if (-1 == lockfd) {
+		fprintf(stderr, "Error opening lock file %s\n", lockname);
+		return EXIT_FAILURE;
+	}
+
+	if (-1 == flock(lockfd, LOCK_EX)) {
+		fprintf(stderr, "Error locking file %s\n", lockname);
+		close(lockfd);
+		return EXIT_FAILURE;
+	}
 
 	if (strcmp(cmdname, CMD_PRINTENV) == 0) {
 		if (fw_printenv(argc, argv) != 0)
 			retval = EXIT_FAILURE;
 	} else if (strcmp(cmdname, CMD_SETENV) == 0) {
-		if (!script_file) {
+		if (!setenv_args.script_file) {
 			if (fw_setenv(argc, argv) != 0)
 				retval = EXIT_FAILURE;
 		} else {
-			if (fw_parse_script(script_file) != 0)
+			if (fw_parse_script(setenv_args.script_file) != 0)
 				retval = EXIT_FAILURE;
 		}
-	} else {
-		fprintf(stderr,
-			"Identity crisis - may be called as `" CMD_PRINTENV
-			"' or as `" CMD_SETENV "' but not as `%s'\n",
-			cmdname);
-		retval = EXIT_FAILURE;
 	}
 
-exit:
 	flock(lockfd, LOCK_UN);
 	close(lockfd);
 	return retval;
-- 
2.6.2

  parent reply	other threads:[~2015-11-24 13:21 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-11-24 13:21 [U-Boot] [PATCH 0/5] tools: env: simplify argument parsing Andreas Fenkart
2015-11-24 13:21 ` [U-Boot] [PATCH 1/5] tools: env validate: pass values as 0-based array Andreas Fenkart
2015-11-24 13:21 ` [U-Boot] [PATCH 2/5] tools: env: make parse_aes_key stateless Andreas Fenkart
2015-11-24 13:21 ` Andreas Fenkart [this message]
2015-11-24 13:21 ` [U-Boot] [PATCH 4/5] tools: env: parse aes key / suppress flag into argument struct Andreas Fenkart
2015-11-24 13:21 ` [U-Boot] [PATCH 5/5] tools: env: shift optind arguments and fix argument indices Andreas Fenkart
2015-11-24 19:26 ` [U-Boot] [PATCH 0/5] tools: env: simplify argument parsing Michael Heimpold

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1448371275-900-4-git-send-email-andreas.fenkart@dev.digitalstrom.org \
    --to=afenkart@gmail.com \
    --cc=u-boot@lists.denx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.