libsmartcols: sanitize variable names on export output

The shells are very restrictive about variable names, only [:alnum:]
chars are allowed (and alphabetic chars as the first char).  The
library will replace "bad" chars with "_". The char '%' at the end is
replaced by _PCT.

Addresses: https://github.com/karelzak/util-linux/issues/1201
Signed-off-by: Karel Zak <kzak@redhat.com>
This commit is contained in:
Karel Zak 2020-12-03 12:14:10 +01:00
parent 77d44c1e51
commit 58b510e580
7 changed files with 36 additions and 6 deletions

View File

@ -151,5 +151,21 @@ static inline void fputs_nonblank(const char *data, FILE *out)
}
}
static inline void fputs_shell_ident(const char *data, FILE *out)
{
const char *p = data;
/* convert "1FOO" to "_1FOO" */
if (p && !isalpha(*p))
fputc('_', out);
/* replace all "bad" chars with "_" */
for (p = data; p && *p; p++) {
if (!isalnum(*p))
fputc('_', out);
else
fputc(*p, out);
}
}
#endif /* _CAREFULPUTC_H */

View File

@ -472,7 +472,10 @@ static int print_data(struct libscols_table *tb,
return 0;
case SCOLS_FMT_EXPORT:
fprintf(tb->out, "%s=", name);
fputs_shell_ident(name, tb->out);
if (endswith(name, "%"))
fputs("PCT", tb->out);
fputc('=', tb->out);
fputs_quoted(data, tb->out);
if (!is_last)
fputs(colsep(tb), tb->out);

View File

@ -1076,6 +1076,10 @@ int scols_table_enable_json(struct libscols_table *tb, int enable)
* Enable/disable export output format (COLUMNAME="value" ...).
* The parsable output formats (export and raw) are mutually exclusive.
*
* Note that COLUMNAME maybe be modified on output to contains only chars
* allowed as shell variable identifiers, for example MIN-IO and FSUSE% will be
* MIN_IO and FSUSE_PCT.
*
* Returns: 0 on success, negative number in case of an error.
*/
int scols_table_enable_export(struct libscols_table *tb, int enable)

View File

@ -156,7 +156,9 @@ Output almost all available columns. The columns that require
are not included.
.TP
.BR \-P , " \-\-pairs"
Use key="value" output format. All potentially unsafe characters are hex-escaped (\\x<code>).
Produce output in the form of key="value" pairs. All potentially unsafe value characters are hex-escaped (\\x<code>).
The key (variable name) will be modified to contain only characters allowed for a shell variable
identifiers, for example, FS_OPTIONS and USE_PCT instead of FS-OPTIONS and USE%.
.TP
.BR \-p , " \-\-poll\fR[\fI=list\fR]"
Monitor changes in the /proc/self/mountinfo file. Supported actions are: mount,

View File

@ -128,7 +128,9 @@ Output all available columns.
.TP
.BR \-P , " \-\-pairs"
Produce output in the form of key="value" pairs. The output lines are still ordered by
dependencies. All potentially unsafe characters are hex-escaped (\\x<code>).
dependencies. All potentially unsafe value characters are hex-escaped (\\x<code>).
The key (variable name) will be modified to contain only characters allowed for a shell variable
identifiers, for example, MIN_IO and FSUSE_PCT instead of MIN-IO and FSUSE%.
.TP
.BR \-p , " \-\-paths"
Print full device paths.

View File

@ -51,7 +51,10 @@ Write information about active semaphore sets.
Show creator and owner.
.TP
\fB\-e\fR, \fB\-\-export\fR
Output data in the format of NAME=VALUE.
Produce output in the form of key="value" pairs. All potentially unsafe value
characters are hex-escaped (\\x<code>). The key (variable name) will be
modified to contain only characters allowed for a shell variable identifiers,
for example, USE_PCT instead of USE%.
.TP
\fB\-J\fR, \fB\-\-json\fR
Use the JSON output format.

View File

@ -61,8 +61,8 @@ specified in the format \fB+\fIlist\fP (e.g., \fBlsmem \-o +NODE\fP).
Output all available columns.
.TP
.BR \-P , " \-\-pairs"
Produce output in the form of key="value" pairs.
All potentially unsafe characters are hex-escaped (\\x<code>).
Produce output in the form of key="value" pairs. All potentially unsafe value
characters are hex-escaped (\\x<code>).
.TP
.BR \-r , " \-\-raw"
Produce output in raw format. All potentially unsafe characters are hex-escaped