zdev: implement --shell command line switch

Fix issue in the lszdev command where column names if using the --pairs
command line switch can contain characters that are not allowed to be
used as variable names in a shell environment.

Replace bad characters in column names by an underscore if the --shell
command line switch is specified along with the --pairs switch.

The additional --shell switch mimics what is already available in the
lsblk command and thus it shall be used along with the --pairs switch.

Signed-off-by: Daniel S. Haischt <modean@linux.ibm.com>
Reviewed-by: Peter Oberparleiter <oberpar@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
This commit is contained in:
Daniel S. Haischt
2022-12-05 18:42:15 +01:00
committed by Steffen Eiden
parent 0c5b9f5d11
commit 889293e557
9 changed files with 81 additions and 13 deletions

View File

@@ -66,7 +66,7 @@ typedef char *(*table_value_cb_t)(void *item, int id, const char *heading,
struct column *table_get_column(struct column *, const char *);
exit_code_t table_print(struct column *, table_value_cb_t, void *,
struct util_list *, struct util_list *, int, int, int,
int);
int, int);
exit_code_t table_check_columns(struct column *, struct util_list *);
void table_print_columns(struct column *, struct util_list *, int, int);
void table_set_default(struct column *, int, int);

View File

@@ -23,7 +23,7 @@ struct table_attrib {
};
struct table_attrib *table_attrib_new(struct subtype *, struct attrib *);
void table_attribs_show(struct util_list *, int, int, struct devtype *);
void table_attribs_show(struct util_list *, int, int, int, struct devtype *);
void table_attribs_show_details(struct util_list *, struct devtype *);
#endif /* TABLE_ATTRIBS_H */

View File

@@ -423,6 +423,17 @@ with the name of the corresponding column. Values are enclosed in double
quotation marks. Any quotation marks or slashes in the value string are
escaped by placing a slash in front of it.
.PP
.OD shell "" ""
Produces KEY="VALUE" format usable as shell variables.
You can use this option together with the --pairs option to generate
output that is suitable to be sourced in a shell environment. This
option generates similar output as it would be provided by the --pairs
option but it would take care of replacing any character of a column
name that would not be appropriate to be used as part of a shell
variable name by an underscore.
.PP
.
.OD persistent "p" ""
List information from the persistent configuration only.

View File

@@ -2532,7 +2532,7 @@ static exit_code_t do_list_attribs(struct options *opts)
remove_duplicate_attribs(attribs, devtype_count_subtypes(dt));
if (attribs) {
table_attribs_show(attribs, 1, 0, dt);
table_attribs_show(attribs, 1, 0, 0, dt);
ptrlist_free(attribs, 1);
} else
rc = EXIT_ATTRIB_NOT_FOUND;

View File

@@ -70,6 +70,7 @@ struct options {
unsigned int no_headings:1;
struct util_list *base; /* List of struct strlist_node */
unsigned int pairs:1;
unsigned int shell:1;
unsigned int verbose:1;
unsigned int quiet:1;
unsigned int site_id;
@@ -106,6 +107,7 @@ enum {
OPT_VERBOSE = 'V',
OPT_QUIET = 'q',
OPT_PAIRS = 'P',
OPT_SHELL = (OPT_ANONYMOUS_BASE+__COUNTER__),
OPT_AUTO_CONF = (OPT_ANONYMOUS_BASE+__COUNTER__),
OPT_SITE = 's',
};
@@ -167,6 +169,7 @@ static const struct option opt_list[] = {
{ "no-headings", no_argument, NULL, OPT_NO_HEADINGS },
{ "base", required_argument, NULL, OPT_BASE },
{ "pairs", no_argument, NULL, OPT_PAIRS },
{ "shell", no_argument, NULL, OPT_SHELL },
{ "verbose", no_argument, NULL, OPT_VERBOSE },
{ "quiet", no_argument, NULL, OPT_QUIET },
{ "site", required_argument, NULL, OPT_SITE },
@@ -699,6 +702,11 @@ static exit_code_t parse_options(struct options *opts, int argc, char *argv[])
opts->pairs = 1;
break;
case OPT_SHELL:
/* --shell */
opts->shell = 1;
break;
case OPT_VERBOSE:
/* --verbose */
opts->verbose = 1;
@@ -761,6 +769,13 @@ static exit_code_t parse_options(struct options *opts, int argc, char *argv[])
if (rc)
goto out;
/* check whether --pairs and --columns is used */
if (opts->shell == 1 && (opts->pairs != 1 || util_list_is_empty(opts->columns))) {
syntax("'--shell' must be used together with "
"'--pairs' and '--columns'\n");
return EXIT_USAGE_ERROR;
}
/* Determine configuration set. */
if (!opts->active && !opts->persistent && !opts->auto_conf) {
/* Default display targets are active + persistent - note that
@@ -1209,7 +1224,7 @@ static exit_code_t do_list_devices(struct options *opts)
/* Display table. */
rc = table_print(dev_table, dev_table_get_value, opts, items,
opts->columns, !opts->no_headings, opts->pairs, 0,
util_list_is_empty(opts->columns));
util_list_is_empty(opts->columns), opts->shell);
ptrlist_free(items, 0);
return rc;
@@ -1443,7 +1458,7 @@ static void settings_table_print(struct setting_list *active,
data.pairs = opts->pairs;
table_print(settings_table, settings_table_get_value, &data, items,
NULL, 1, opts->pairs, ind, 0);
NULL, 1, opts->pairs, ind, 0, opts->shell);
out:
ptrlist_free(items, 1);

View File

@@ -41,6 +41,7 @@ OPTIONS
-n, --no-headings Do not print column headings
--base PATH Use PATH as base for accessing files
--pairs Produce output in KEY="VALUE" format
--shell Produces KEY="VALUE" format usable as shell variables
--auto-conf Only show auto-configuration data
-s, --site ID Only show data configured for the specified site
-V, --verbose Print additional run-time information

View File

@@ -7,6 +7,7 @@
* it under the terms of the MIT license. See LICENSE for details.
*/
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
@@ -116,6 +117,40 @@ static struct cell *cells_get_default(struct column *columns)
return cells;
}
bool is_shell_char(const char c)
{
/* check whether character is alphabetic */
if (tolower(c) >= 'a' && tolower(c) <= 'z')
return true;
if (isdigit(c) || c == '_')
return true;
return false;
}
/* replaces bad characters that won't work if using them
* as part of variable names in a shell environment
*/
char *replace_bad_chars(const char *value)
{
int i;
char *copy, c;
/* remove bad characters and replace them by an underscore */
copy = misc_strdup(value);
for (i = 0; (c = value[i]); i++) {
/*
* check whether character is alphabetic,
* first character of an env var can't be a digit
*/
if (!is_shell_char(c) || (i == 0 && isdigit(c)))
c = '_';
/* copy character into string copy */
copy[i] = c;
}
return copy;
}
/* Return a newly allocated array of struct cells containing a struct cell for
* each column definition whose name was specified in strlist @names. */
static struct cell *cells_get(struct column *columns, struct util_list *names)
@@ -320,17 +355,23 @@ do_print:
}
/* Print all cells in a row in pairs format. */
static void print_row_pairs(struct cell *cells)
static void print_row_pairs(struct cell *cells, int shell)
{
int i;
struct cell *c;
char *val;
char *heading;
for (i = 0; cells[i].heading; i++) {
c = &cells[i];
val = quote_str(c->value ? c->value : "", 1);
printf("%s%s=%s", i > 0 ? " " : "", c->heading, val);
if (shell)
heading = replace_bad_chars(c->heading);
else
heading = misc_strdup(c->heading);
printf("%s%s=%s", i > 0 ? " " : "", heading, val);
free(val);
free(heading);
}
printf("\n");
}
@@ -342,7 +383,7 @@ static void print_row_pairs(struct cell *cells)
exit_code_t table_print(struct column *columns, table_value_cb_t get_value_cb,
void *data, struct util_list *items,
struct util_list *names, int heading, int pairs,
int indent, int wrap)
int indent, int wrap, int shell)
{
struct cell *cells;
struct ptrlist_node *p;
@@ -372,7 +413,7 @@ exit_code_t table_print(struct column *columns, table_value_cb_t get_value_cb,
util_list_iterate(items, p) {
cells_get_values(cells, p->ptr, get_value_cb, data);
if (pairs)
print_row_pairs(cells);
print_row_pairs(cells, shell);
else
print_row(cells, space, indent, wrap);
}
@@ -434,7 +475,7 @@ void table_print_columns(struct column *columns, struct util_list *names,
for (i = 0; columns[i].name; i++)
ptrlist_add(items, &columns[i]);
table_print(columns_table, columns_table_get_value, NULL, items, names,
heading, pairs, 0, 0);
heading, pairs, 0, 0, 0);
ptrlist_free(items, 0);
}

View File

@@ -115,7 +115,7 @@ void print_type(struct devtype *dt, struct subtype *st, bool multiple)
/* Display table of attributes. */
void table_attribs_show(struct util_list *attribs, int headings, int pairs,
struct devtype *dt)
int shell, struct devtype *dt)
{
struct util_list *subtypes, *subattribs;
struct ptrlist_node *p;
@@ -145,7 +145,7 @@ void table_attribs_show(struct util_list *attribs, int headings, int pairs,
print_type(dt, st, multiple);
table_print(table_attribs, table_attribs_get_value, st,
subattribs, NULL, headings, pairs, indent, 0);
subattribs, NULL, headings, pairs, shell, indent, 0);
ptrlist_free(subattribs, 0);
}

View File

@@ -83,7 +83,7 @@ exit_code_t table_types_show(struct util_list *columns, int headings, int pairs)
items = table_types_build();
rc = table_print(table_types, table_types_get_value, NULL,
items, columns, headings, pairs, 0, 0);
items, columns, headings, pairs, 0, 0, 0);
ptrlist_free(items, 1);
return rc;