On 04.11.21 10:24, Michael Adler wrote:
> Adding --raw to the bg_printenv command-line enables raw output mode
> which can be used for shell scripting purposes.
>
> Examples:
>
> * Bring the fields as variables into your shell:
>
> $ source <(bg_printenv --current --output kernel,ustate --raw)
> $ echo "kernel: $KERNEL, ustate: $USTATE"
> kernel: C:BOOTA:linux.efi, ustate: 0
>
> * Assing the current ustate to a variable:
>
> $ USTATE=$(bg_printenv --current --output ustate --raw)
> $ echo "$USTATE"
> USTATE=0
>
> Signed-off-by: Michael Adler <[email protected]>
> ---
>  tests/bg_setenv.bats |  20 ++++++
>  tools/bg_setenv.c    | 153 +++++++++++++++++++++++++++++--------------
>  2 files changed, 125 insertions(+), 48 deletions(-)
>
> diff --git a/tests/bg_setenv.bats b/tests/bg_setenv.bats
> index f4739fd..67cee8f 100755
> --- a/tests/bg_setenv.bats
> +++ b/tests/bg_setenv.bats
> @@ -134,3 +134,23 @@ ustate:           0 (OK)" ]]
>      run bg_printenv "--filepath=$envfile" --output 
> in_progress,revision,kernel,kernelargs,watchdog_timeout,ustate,user
>      [[ "$output" = "$expected_output" ]]
>  }
> +
> +@test "bg_printenv ustate raw" {
> +    local envfile
> +    envfile="$(mktemp -d)/BGENV.DAT"
> +
> +    create_sample_bgenv "$envfile"
> +    run bg_printenv "--filepath=$envfile" --output ustate --raw
> +    [[ "$output" = "USTATE=0" ]]
> +}
> +
> +@test "bg_printenv multiple fields raw" {
> +    local envfile
> +    envfile="$(mktemp -d)/BGENV.DAT"
> +
> +    create_sample_bgenv "$envfile"
> +    run bg_printenv "--filepath=$envfile" --output ustate,kernel,kernelargs 
> --raw
> +    [[ "$output" = "KERNEL=C:BOOT:kernel.efi
> +KERNELARGS=root=/dev/sda
> +USTATE=0" ]]
> +}
> diff --git a/tools/bg_setenv.c b/tools/bg_setenv.c
> index 148078e..7a234cf 100644
> --- a/tools/bg_setenv.c
> +++ b/tools/bg_setenv.c
> @@ -70,6 +70,7 @@ static struct argp_option options_printenv[] = {
>           "Available fields: in_progress, revision, kernel, kernelargs, "
>           "watchdog_timeout, ustate, user. "
>           "If omitted, all available fields are printed."),
> +     OPT("raw", 'r', 0, 0, "Raw output mode, e.g. for shell scripting"),
>       {},
>  };
>
> @@ -112,6 +113,7 @@ struct arguments_printenv {
>       bool current;
>       /* a bitset to decide which fields are printed */
>       struct fields output_fields;
> +     bool raw;
>  };
>
>  typedef enum { ENV_TASK_SET, ENV_TASK_DEL } BGENV_TASK;
> @@ -524,6 +526,9 @@ static error_t parse_printenv_opt(int key, char *arg, 
> struct argp_state *state)
>       case 'o':
>               e = parse_output_fields(arg, &arguments->output_fields);
>               break;
> +     case 'r':
> +             arguments->raw = true;
> +             break;
>       case ARGP_KEY_ARG:
>               /* too many arguments - program terminates with call to
>                * argp_usage with non-zero return code */
> @@ -536,7 +541,7 @@ static error_t parse_printenv_opt(int key, char *arg, 
> struct argp_state *state)
>       return e;
>  }
>
> -static void dump_uservars(uint8_t *udata)
> +static void dump_uservars(uint8_t *udata, bool raw)
>  {
>       char *key, *value;
>       uint64_t type;
> @@ -547,10 +552,10 @@ static void dump_uservars(uint8_t *udata)
>       while (*udata) {
>               bgenv_map_uservar(udata, &key, &type, (uint8_t **)&value,
>                                 &rsize, &dsize);
> -             fprintf(stdout, "%s ", key);
> +             fprintf(stdout, "%s", key);
>               type &= USERVAR_STANDARD_TYPE_MASK;
>               if (type == USERVAR_TYPE_STRING_ASCII) {
> -                     fprintf(stdout, "= %s\n", value);
> +                     fprintf(stdout, raw ? "=%s\n" : " = %s\n", value);
>               } else if (type >= USERVAR_TYPE_UINT8 &&
>                          type <= USERVAR_TYPE_UINT64) {
>                       switch(type) {
> @@ -567,8 +572,8 @@ static void dump_uservars(uint8_t *udata)
>                               val_unum = *((uint64_t *) value);
>                               break;
>                       }
> -                     fprintf(stdout, "= %llu\n",
> -                             (long long unsigned int) val_unum);
> +                     fprintf(stdout, raw ? "=%llu\n" : " = %llu\n",
> +                             (long long unsigned int)val_unum);
>               } else if (type >= USERVAR_TYPE_SINT8 &&
>                          type <= USERVAR_TYPE_SINT64) {
>                       switch(type) {
> @@ -585,19 +590,20 @@ static void dump_uservars(uint8_t *udata)
>                               val_snum = *((int64_t *) value);
>                               break;
>                       }
> -                     fprintf(stdout, "= %lld\n",
> -                             (long long signed int) val_snum);
> +                     fprintf(stdout, raw ? "=%lld\n" : " = %lld\n",
> +                             (long long signed int)val_snum);
>               } else {
>                       switch(type) {
>                       case USERVAR_TYPE_CHAR:
> -                             fprintf(stdout, "= %c\n", (char) *value);
> +                             fprintf(stdout, raw ? "=%c\n" : " = %c\n",
> +                                     (char)*value);
>                               break;
>                       case USERVAR_TYPE_BOOL:
> -                             fprintf(stdout, "= %s\n",
> -                                    (bool) *value ? "true" : "false");
> +                             fprintf(stdout, raw ? "=%s\n" : " = %s\n",
> +                                     (bool)*value ? "true" : "false");
>                               break;
>                       default:
> -                             fprintf(stdout, "( Type is not printable )\n");
> +                             fprintf(stdout, " ( Type is not printable )\n");
>                       }
>               }
>
> @@ -605,39 +611,71 @@ static void dump_uservars(uint8_t *udata)
>       }
>  }
>
> -static void dump_env(BG_ENVDATA *env, struct fields output_fields)
> +static void dump_env(BG_ENVDATA *env, struct fields output_fields, bool raw)
>  {
>       char buffer[ENV_STRING_LENGTH];
> -     fprintf(stdout, "Values:\n");
> +     if (!raw) {
> +             fprintf(stdout, "Values:\n");
> +     }
>       if (output_fields.in_progress) {
> -             fprintf(stdout, "in_progress:      %s\n",
> -                     env->in_progress ? "yes" : "no");
> +             if (raw) {
> +                     fprintf(stdout, "IN_PROGRESS=%d\n", env->in_progress);
> +             } else {
> +                     fprintf(stdout, "in_progress:      %s\n",
> +                             env->in_progress ? "yes" : "no");
> +             }
>       }
>       if (output_fields.revision) {
> -             fprintf(stdout, "revision:         %u\n", env->revision);
> +             if (raw) {
> +                     fprintf(stdout, "REVISION=%d\n", env->revision);

%u - fixed in next.

Jan

-- 
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/5599c425-877a-facc-5249-3a6b741375aa%40web.de.

Reply via email to