This extends the splitting of tools started at commit 15aa3731.

The resulting dependency graph is as follows:

- bg_envtools: depends on bg_envtools.h
- bg_printenv: depends on bg_envtools.h, bg_printenv.h
- bg_setenv: depends on bg_envtools.h, bg_printenv.h, bg_setenv.h
- main: depends on bg_printenv.h, bg_setenv.h

This makes it clear from the dependency graph what each tool does and
speeds up incremental compilation.

Signed-off-by: Michael Adler <[email protected]>
---
 Makefile.am         |   4 +-
 tools/bg_envtools.c | 158 ++++++++++++++++++++++++++++++++++++++++++
 tools/bg_envtools.h |  21 ++----
 tools/bg_printenv.c |   1 +
 tools/bg_printenv.h |  38 ++++++++++
 tools/bg_setenv.c   | 165 +++-----------------------------------------
 tools/bg_setenv.h   |  23 ++++++
 tools/main.c        |  26 +++++++
 8 files changed, 264 insertions(+), 172 deletions(-)
 create mode 100644 tools/bg_envtools.c
 create mode 100644 tools/bg_printenv.h
 create mode 100644 tools/bg_setenv.h
 create mode 100644 tools/main.c

diff --git a/Makefile.am b/Makefile.am
index 452a2e0..8081839 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -105,7 +105,9 @@ bin_PROGRAMS = bg_setenv
 
 bg_setenv_SOURCES = \
        tools/bg_setenv.c \
-       tools/bg_printenv.c
+       tools/bg_printenv.c \
+       tools/bg_envtools.c \
+       tools/main.c
 
 bg_setenv_CFLAGS = \
        $(AM_CFLAGS) -static
diff --git a/tools/bg_envtools.c b/tools/bg_envtools.c
new file mode 100644
index 0000000..b81ffed
--- /dev/null
+++ b/tools/bg_envtools.c
@@ -0,0 +1,158 @@
+/*
+ * EFI Boot Guard
+ *
+ * Copyright (c) Siemens AG, 2017-2021
+ *
+ * Authors:
+ *  Andreas Reichel <[email protected]>
+ *  Michael Adler <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2.  See
+ * the COPYING file in the top-level directory.
+ *
+ * SPDX-License-Identifier:    GPL-2.0
+ */
+
+#include <sys/stat.h>
+#include "env_config_file.h"
+#include "version.h"
+
+#include "bg_envtools.h"
+
+static char *ustatemap[] = {"OK", "INSTALLED", "TESTING", "FAILED", "UNKNOWN"};
+
+char *ustate2str(uint8_t ustate)
+{
+       if (ustate > USTATE_MAX) {
+               ustate = USTATE_MAX;
+       }
+       return ustatemap[ustate];
+}
+
+uint8_t str2ustate(char *str)
+{
+       uint8_t i;
+
+       if (!str) {
+               return USTATE_UNKNOWN;
+       }
+       for (i = USTATE_MIN; i < USTATE_MAX; i++) {
+               if (strncasecmp(str, ustatemap[i], strlen(ustatemap[i])) == 0) {
+                       return i;
+               }
+       }
+       return USTATE_UNKNOWN;
+}
+
+int parse_int(char *arg)
+{
+       char *tmp;
+       long i;
+
+       errno = 0;
+       i = strtol(arg, &tmp, 10);
+       if (errno == ERANGE ||            /* out of range */
+           (errno != 0 && i == 0) ||     /* no conversion was performed */
+           tmp == arg || *tmp != '\0' || /* invalid input */
+           i < INT_MIN || i > INT_MAX) { /* not a valid int */
+               errno = EINVAL;
+               return -1;
+       }
+       return (int)i;
+}
+
+error_t parse_common_opt(int key, char *arg, bool compat_mode,
+                        struct arguments_common *arguments)
+{
+       bool found = false;
+       int i;
+       switch (key) {
+       case 'f':
+               found = true;
+               free(arguments->envfilepath);
+               arguments->envfilepath = NULL;
+
+               if (compat_mode) {
+                       /* compat mode, permitting "bg_setenv -f <dir>" */
+                       struct stat sb;
+
+                       int res = stat(arg, &sb);
+                       if (res == 0 && S_ISDIR(sb.st_mode)) {
+                               fprintf(stderr,
+                                       "WARNING: Using -f to specify only the "
+                                       "ouptut directory is deprecated.\n");
+                               res = asprintf(&arguments->envfilepath, "%s/%s",
+                                              arg, FAT_ENV_FILENAME);
+                               if (res == -1) {
+                                       return ENOMEM;
+                               }
+                       }
+               }
+
+               if (!arguments->envfilepath) {
+                       arguments->envfilepath = strdup(arg);
+                       if (!arguments->envfilepath) {
+                               return ENOMEM;
+                       }
+               }
+               break;
+       case 'p':
+               found = true;
+               i = parse_int(arg);
+               if (errno) {
+                       fprintf(stderr, "Invalid number specified for -p.\n");
+                       return 1;
+               }
+               if (i >= 0 && i < ENV_NUM_CONFIG_PARTS) {
+                       arguments->which_part = i;
+                       arguments->part_specified = true;
+               } else {
+                       fprintf(stderr,
+                               "Selected partition out of range. Valid range: "
+                               "0..%d.\n",
+                               ENV_NUM_CONFIG_PARTS - 1);
+                       return 1;
+               }
+               break;
+       case 'v':
+               found = true;
+               /* Set verbosity in this program */
+               arguments->verbosity = true;
+               /* Set verbosity in the library */
+               bgenv_be_verbose(true);
+               break;
+       case 'V':
+               found = true;
+               fprintf(stdout, "EFI Boot Guard %s\n", EFIBOOTGUARD_VERSION);
+               exit(0);
+       }
+       if (!found) {
+               return ARGP_ERR_UNKNOWN;
+       }
+       return 0;
+}
+
+bool get_env(char *configfilepath, BG_ENVDATA *data)
+{
+       FILE *config;
+       bool result = true;
+
+       if (!(config = open_config_file(configfilepath, "rb"))) {
+               return false;
+       }
+
+       if (!(fread(data, sizeof(BG_ENVDATA), 1, config) == 1)) {
+               VERBOSE(stderr, "Error reading environment data from %s\n",
+                       configfilepath);
+               if (feof(config)) {
+                       VERBOSE(stderr, "End of file encountered.\n");
+               }
+               result = false;
+       }
+
+       if (close_config_file(config)) {
+               VERBOSE(stderr,
+                       "Error closing environment file after reading.\n");
+       };
+       return result;
+}
diff --git a/tools/bg_envtools.h b/tools/bg_envtools.h
index fb48ab1..a397ca4 100644
--- a/tools/bg_envtools.h
+++ b/tools/bg_envtools.h
@@ -13,6 +13,9 @@
  * SPDX-License-Identifier:    GPL-2.0
  */
 
+#ifndef __bg_envtools_h_
+#define __bg_envtools_h_
+
 #include "env_api.h"
 
 #define OPT(name, key, arg, flags, doc)                                        
\
@@ -42,26 +45,14 @@ struct arguments_common {
        bool part_specified;
 };
 
-struct fields {
-       unsigned int in_progress : 1;
-       unsigned int revision : 1;
-       unsigned int kernel : 1;
-       unsigned int kernelargs : 1;
-       unsigned int wdog_timeout : 1;
-       unsigned int ustate : 1;
-       unsigned int user : 1;
-};
-
-extern const struct fields ALL_FIELDS;
+int parse_int(char *arg);
 
 char *ustate2str(uint8_t ustate);
+uint8_t str2ustate(char *str);
 
 error_t parse_common_opt(int key, char *arg, bool compat_mode,
                         struct arguments_common *arguments);
 
 bool get_env(char *configfilepath, BG_ENVDATA *data);
 
-void dump_env(BG_ENVDATA *env, struct fields output_fields, bool raw);
-void dump_envs(struct fields output_fields, bool raw);
-
-error_t bg_printenv(int argc, char **argv);
+#endif
diff --git a/tools/bg_printenv.c b/tools/bg_printenv.c
index bc515a4..61152dc 100644
--- a/tools/bg_printenv.c
+++ b/tools/bg_printenv.c
@@ -16,6 +16,7 @@
 #include "uservars.h"
 
 #include "bg_envtools.h"
+#include "bg_printenv.h"
 
 static char tool_doc[] =
        "bg_printenv - Environment tool for the EFI Boot Guard";
diff --git a/tools/bg_printenv.h b/tools/bg_printenv.h
new file mode 100644
index 0000000..309ff99
--- /dev/null
+++ b/tools/bg_printenv.h
@@ -0,0 +1,38 @@
+/*
+ * EFI Boot Guard
+ *
+ * Copyright (c) Siemens AG, 2017-2021
+ *
+ * Authors:
+ *  Andreas Reichel <[email protected]>
+ *  Michael Adler <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2.  See
+ * the COPYING file in the top-level directory.
+ *
+ * SPDX-License-Identifier:    GPL-2.0
+ */
+
+#ifndef __bg_printenv_h_
+#define __bg_printenv_h_
+
+#include "env_api.h"
+
+struct fields {
+       unsigned int in_progress : 1;
+       unsigned int revision : 1;
+       unsigned int kernel : 1;
+       unsigned int kernelargs : 1;
+       unsigned int wdog_timeout : 1;
+       unsigned int ustate : 1;
+       unsigned int user : 1;
+};
+
+extern const struct fields ALL_FIELDS;
+
+void dump_envs(struct fields output_fields, bool raw);
+void dump_env(BG_ENVDATA *env, struct fields output_fields, bool raw);
+
+error_t bg_printenv(int argc, char **argv);
+
+#endif
diff --git a/tools/bg_setenv.c b/tools/bg_setenv.c
index a9c451c..ab9673e 100644
--- a/tools/bg_setenv.c
+++ b/tools/bg_setenv.c
@@ -17,10 +17,10 @@
 #include <sys/stat.h>
 
 #include "ebgenv.h"
-#include "version.h"
-#include "env_config_file.h"
 
 #include "bg_envtools.h"
+#include "bg_setenv.h"
+#include "bg_printenv.h"
 
 static char tool_doc[] =
        "bg_setenv - Environment tool for the EFI Boot Guard";
@@ -155,31 +155,6 @@ static void journal_process_action(BGENV *env, struct 
env_action *action)
        }
 }
 
-static char *ustatemap[] = {"OK", "INSTALLED", "TESTING", "FAILED", "UNKNOWN"};
-
-static uint8_t str2ustate(char *str)
-{
-       uint8_t i;
-
-       if (!str) {
-               return USTATE_UNKNOWN;
-       }
-       for (i = USTATE_MIN; i < USTATE_MAX; i++) {
-               if (strncasecmp(str, ustatemap[i], strlen(ustatemap[i])) == 0) {
-                       return i;
-               }
-       }
-       return USTATE_UNKNOWN;
-}
-
-char *ustate2str(uint8_t ustate)
-{
-       if (ustate > USTATE_MAX) {
-               ustate = USTATE_MAX;
-       }
-       return ustatemap[ustate];
-}
-
 static error_t set_uservars(char *arg)
 {
        char *key, *value;
@@ -200,94 +175,6 @@ static error_t set_uservars(char *arg)
                                  (uint8_t *)value, strlen(value) + 1);
 }
 
-static int parse_int(char *arg)
-{
-       char *tmp;
-       long i;
-
-       errno = 0;
-       i = strtol(arg, &tmp, 10);
-       if (errno == ERANGE ||             /* out of range */
-           (errno != 0 && i == 0) ||      /* no conversion was performed */
-           tmp == arg || *tmp != '\0' ||  /* invalid input */
-           i < INT_MIN || i > INT_MAX) {  /* not a valid int */
-               errno = EINVAL;
-               return -1;
-       }
-       return (int) i;
-}
-
-error_t parse_common_opt(int key, char *arg, bool compat_mode,
-                        struct arguments_common *arguments)
-{
-       bool found = false;
-       int i;
-       switch (key) {
-       case 'f':
-               found = true;
-               free(arguments->envfilepath);
-               arguments->envfilepath = NULL;
-
-               if (compat_mode) {
-                       /* compat mode, permitting "bg_setenv -f <dir>" */
-                       struct stat sb;
-
-                       int res = stat(arg, &sb);
-                       if (res == 0 && S_ISDIR(sb.st_mode)) {
-                               fprintf(stderr,
-                                       "WARNING: Using -f to specify only the "
-                                       "ouptut directory is deprecated.\n");
-                               res = asprintf(&arguments->envfilepath, "%s/%s",
-                                              arg, FAT_ENV_FILENAME);
-                               if (res == -1) {
-                                       return ENOMEM;
-                               }
-                       }
-               }
-
-               if (!arguments->envfilepath) {
-                       arguments->envfilepath = strdup(arg);
-                       if (!arguments->envfilepath) {
-                               return ENOMEM;
-                       }
-               }
-               break;
-       case 'p':
-               found = true;
-               i = parse_int(arg);
-               if (errno) {
-                       fprintf(stderr, "Invalid number specified for -p.\n");
-                       return 1;
-               }
-               if (i >= 0 && i < ENV_NUM_CONFIG_PARTS) {
-                       arguments->which_part = i;
-                       arguments->part_specified = true;
-               } else {
-                       fprintf(stderr,
-                               "Selected partition out of range. Valid range: "
-                               "0..%d.\n",
-                               ENV_NUM_CONFIG_PARTS - 1);
-                       return 1;
-               }
-               break;
-       case 'v':
-               found = true;
-               /* Set verbosity in this program */
-               arguments->verbosity = true;
-               /* Set verbosity in the library */
-               bgenv_be_verbose(true);
-               break;
-       case 'V':
-               found = true;
-               fprintf(stdout, "EFI Boot Guard %s\n", EFIBOOTGUARD_VERSION);
-               exit(0);
-       }
-       if (!found) {
-               return ARGP_ERR_UNKNOWN;
-       }
-       return 0;
-}
-
 static error_t parse_setenv_opt(int key, char *arg, struct argp_state *state)
 {
        struct arguments_setenv *arguments = state->input;
@@ -329,12 +216,12 @@ static error_t parse_setenv_opt(int key, char *arg, 
struct argp_state *state)
                        }
                }
                if (i < 0 || i > 3) {
-                       fprintf(
-                           stderr,
-                           "Invalid ustate value specified. Possible values: "
-                           "0 (%s), 1 (%s), 2 (%s), 3 (%s)\n",
-                           ustatemap[0], ustatemap[1], ustatemap[2],
-                           ustatemap[3]);
+                       fprintf(stderr,
+                               "Invalid ustate value specified. Possible "
+                               "values: "
+                               "0 (%s), 1 (%s), 2 (%s), 3 (%s)\n",
+                               ustate2str(0), ustate2str(1), ustate2str(2),
+                               ustate2str(3));
                        return 1;
                } else {
                        res = asprintf(&tmp, "%u", i);
@@ -444,31 +331,6 @@ static void update_environment(BGENV *env, bool verbosity)
 
 }
 
-bool get_env(char *configfilepath, BG_ENVDATA *data)
-{
-       FILE *config;
-       bool result = true;
-
-       if (!(config = open_config_file(configfilepath, "rb"))) {
-               return false;
-       }
-
-       if (!(fread(data, sizeof(BG_ENVDATA), 1, config) == 1)) {
-               VERBOSE(stderr, "Error reading environment data from %s\n",
-                       configfilepath);
-               if (feof(config)) {
-                       VERBOSE(stderr, "End of file encountered.\n");
-               }
-               result = false;
-       }
-
-       if (close_config_file(config)) {
-               VERBOSE(stderr,
-                       "Error closing environment file after reading.\n");
-       };
-       return result;
-}
-
 static int dumpenv_to_file(char *envfilepath, bool verbosity, bool 
preserve_env)
 {
        /* execute journal and write to file */
@@ -512,7 +374,7 @@ static int dumpenv_to_file(char *envfilepath, bool 
verbosity, bool preserve_env)
 }
 
 /* This is the entrypoint for the command bg_setenv. */
-static error_t bg_setenv(int argc, char **argv)
+error_t bg_setenv(int argc, char **argv)
 {
        if (argc < 2) {
                printf("No task to perform. Please specify at least one"
@@ -643,12 +505,3 @@ cleanup:
        bgenv_finalize();
        return result;
 }
-
-int main(int argc, char **argv)
-{
-       if (strstr(argv[0], "bg_setenv")) {
-               return bg_setenv(argc, argv);
-       } else {
-               return bg_printenv(argc, argv);
-       }
-}
diff --git a/tools/bg_setenv.h b/tools/bg_setenv.h
new file mode 100644
index 0000000..82a8023
--- /dev/null
+++ b/tools/bg_setenv.h
@@ -0,0 +1,23 @@
+/*
+ * EFI Boot Guard
+ *
+ * Copyright (c) Siemens AG, 2017-2021
+ *
+ * Authors:
+ *  Andreas Reichel <[email protected]>
+ *  Michael Adler <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2.  See
+ * the COPYING file in the top-level directory.
+ *
+ * SPDX-License-Identifier:    GPL-2.0
+ */
+
+#ifndef __bg_setenv_h_
+#define __bg_setenv_h_
+
+#include <errno.h>
+
+error_t bg_setenv(int argc, char **argv);
+
+#endif
diff --git a/tools/main.c b/tools/main.c
new file mode 100644
index 0000000..42e7755
--- /dev/null
+++ b/tools/main.c
@@ -0,0 +1,26 @@
+/*
+ * EFI Boot Guard
+ *
+ * Copyright (c) Siemens AG, 2017-2021
+ *
+ * Authors:
+ *  Andreas Reichel <[email protected]>
+ *  Michael Adler <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2.  See
+ * the COPYING file in the top-level directory.
+ *
+ * SPDX-License-Identifier:    GPL-2.0
+ */
+
+#include "bg_printenv.h"
+#include "bg_setenv.h"
+
+int main(int argc, char **argv)
+{
+       if (strstr(argv[0], "bg_setenv")) {
+               return bg_setenv(argc, argv);
+       } else {
+               return bg_printenv(argc, argv);
+       }
+}
-- 
2.33.1

-- 
You received this message because you are subscribed to the Google Groups "EFI 
Boot Guard" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/efibootguard-dev/20211108091246.598133-1-michael.adler%40siemens.com.

Reply via email to