aboutsummaryrefslogtreecommitdiffstats
path: root/sys-utils
diff options
context:
space:
mode:
authorKarel Zak <kzak@redhat.com>2025-09-03 09:56:26 +0200
committerKarel Zak <kzak@redhat.com>2025-09-03 09:56:26 +0200
commitfc535b1e3be1cfed696d4b2614e69b33cfa67154 (patch)
treee8ecf48f70387b2f1df6778032c202316fe82d96 /sys-utils
parent7eb91dc080c980c94f15c9a5f9e3452b6a1f1fc1 (diff)
parentf5c2a28647291891f38a5991c6393f63f91f4db7 (diff)
downloadutil-linux-fc535b1e3be1cfed696d4b2614e69b33cfa67154.tar.gz
Merge branch 'feat/add_columns_env_ls_cmds' of https://github.com/cgoesche/util-linux-fork
* 'feat/add_columns_env_ls_cmds' of https://github.com/cgoesche/util-linux-fork: lscpu: (man) document the LSCPU_{CACHES_}COLUMNS environment variables bash-completion: (lscpu) add -H/--list-columns options lscpu: add --list-columns option and declutter --help output lscpu: add support for LSCPU_{CACHES_}COLUMNS environment variables lsclocks: add support for LSCLOCKS_COLUMNS environmental variable tests: (lsmem) update expected/lscpu/* with 'ZONES' column lsmem: add support for LSMEM_COLUMNS environmental variable
Diffstat (limited to 'sys-utils')
-rw-r--r--sys-utils/lscpu.1.adoc14
-rw-r--r--sys-utils/lscpu.c49
-rw-r--r--sys-utils/lsmem.1.adoc5
-rw-r--r--sys-utils/lsmem.c2
4 files changed, 59 insertions, 11 deletions
diff --git a/sys-utils/lscpu.1.adoc b/sys-utils/lscpu.1.adoc
index c8694e767a..ce48b9ff45 100644
--- a/sys-utils/lscpu.1.adoc
+++ b/sys-utils/lscpu.1.adoc
@@ -104,11 +104,23 @@ Display physical IDs for all columns with topology elements (core, socket, etc.)
+
The CPU logical numbers are not affected by this option.
+== ENVIRONMENT
+
+LSCPU_COLUMNS=::
+Specifies a comma-separated list of output columns to print. All columns listed by *--list-columns* for the options *-e* and *-p* can be used.
+
+LSCPU_CACHES_COLUMNS=::
+Same as *LSCPU_COLUMNS* with the difference that columns only listed for the *-C* option can be used.
+
include::man-common/help-version.adoc[]
== COLUMNS
-A list of valid column labels can be viewed with the *--help* option.
+A list of valid column labels can be viewed with the *--list-columns* option.
+
+== NOTES
+
+The default output is subject to change. So whenever possible, you should avoid using default output in your scripts.
== BUGS
diff --git a/sys-utils/lscpu.c b/sys-utils/lscpu.c
index 99799594e7..fe996ebf93 100644
--- a/sys-utils/lscpu.c
+++ b/sys-utils/lscpu.c
@@ -34,6 +34,7 @@
#include "optutils.h"
#include "c_strtod.h"
#include "sysfs.h"
+#include "column-list-table.h"
#include "lscpu.h"
@@ -1182,7 +1183,6 @@ static void print_summary(struct lscpu_cxt *cxt)
static void __attribute__((__noreturn__)) usage(void)
{
FILE *out = stdout;
- size_t i;
fputs(USAGE_HEADER, out);
fprintf(out, _(" %s [options]\n"), program_invocation_short_name);
@@ -1206,17 +1206,34 @@ static void __attribute__((__noreturn__)) usage(void)
fputs(_(" --hierarchic[=when] use subsections in summary (auto, never, always)\n"), out);
fputs(_(" --output-all print all available columns for -e, -p or -C\n"), out);
fputs(USAGE_SEPARATOR, out);
+ fputs(_(" -H, --list-columns list the available columns\n"), out);
fprintf(out, USAGE_HELP_OPTIONS(25));
- fputs(_("\nAvailable output columns for -e or -p:\n"), out);
- for (i = 0; i < ARRAY_SIZE(coldescs_cpu); i++)
- fprintf(out, " %13s %s\n", coldescs_cpu[i].name, _(coldescs_cpu[i].help));
+ fprintf(out, USAGE_MAN_TAIL("lscpu(1)"));
- fputs(_("\nAvailable output columns for -C:\n"), out);
- for (i = 0; i < ARRAY_SIZE(coldescs_cache); i++)
- fprintf(out, " %13s %s\n", coldescs_cache[i].name, _(coldescs_cache[i].help));
+ exit(EXIT_SUCCESS);
+}
- fprintf(out, USAGE_MAN_TAIL("lscpu(1)"));
+static void __attribute__((__noreturn__)) list_columns(struct lscpu_cxt *cxt)
+{
+ struct libscols_table *col_tb = xcolumn_list_table_new("lscpu-columns", stdout, cxt->raw, cxt->json);
+ struct libscols_table *col_caches_tb = xcolumn_list_table_new("lscpu-caches-columns", stdout, cxt->raw, cxt->json);
+
+ fputs(_("Available output columns for -e or -p:\n"), stdout);
+ for (size_t i = 0; i < ARRAY_SIZE(coldescs_cpu); i++)
+ xcolumn_list_table_append_line(col_tb, coldescs_cpu[i].name,
+ coldescs_cpu[i].json_type, NULL,
+ _(coldescs_cpu[i].help));
+ scols_print_table(col_tb);
+ scols_unref_table(col_tb);
+
+ fputs(_("\nAvailable output columns for -C:\n"), stdout);
+ for (size_t i = 0; i < ARRAY_SIZE(coldescs_cache); i++)
+ xcolumn_list_table_append_line(col_caches_tb, coldescs_cache[i].name,
+ coldescs_cache[i].json_type, NULL,
+ _(coldescs_cache[i].help));
+ scols_print_table(col_caches_tb);
+ scols_unref_table(col_caches_tb);
exit(EXIT_SUCCESS);
}
@@ -1224,7 +1241,7 @@ static void __attribute__((__noreturn__)) usage(void)
int main(int argc, char *argv[])
{
struct lscpu_cxt *cxt;
- int c, all = 0;
+ int c, all = 0, collist = 0;
int columns[ARRAY_SIZE(coldescs_cpu)];
int cpu_modifier_specified = 0;
char *outarg = NULL;
@@ -1250,6 +1267,7 @@ int main(int argc, char *argv[])
{ "version", no_argument, NULL, 'V' },
{ "output-all", no_argument, NULL, OPT_OUTPUT_ALL },
{ "hierarchic", optional_argument, NULL, OPT_HIERARCHIC },
+ { "list-columns", no_argument, NULL, 'H' },
{ NULL, 0, NULL, 0 }
};
@@ -1267,7 +1285,7 @@ int main(int argc, char *argv[])
cxt = lscpu_new_context();
- while ((c = getopt_long(argc, argv, "aBbC::ce::hJp::rs:xyV", longopts, NULL)) != -1) {
+ while ((c = getopt_long(argc, argv, "aBbC::ce::HhJp::rs:xyV", longopts, NULL)) != -1) {
err_exclusive_options(c, longopts, excl, excl_st);
@@ -1336,6 +1354,9 @@ int main(int argc, char *argv[])
} else
hierarchic = 1;
break;
+ case 'H':
+ collist = 1;
+ break;
case 'h':
usage();
case 'V':
@@ -1345,6 +1366,9 @@ int main(int argc, char *argv[])
}
}
+ if (collist)
+ list_columns(cxt);
+
if (all && ncolumns == 0) {
size_t maxsz = cxt->mode == LSCPU_OUTPUT_CACHES ?
ARRAY_SIZE(coldescs_cache) :
@@ -1395,6 +1419,11 @@ int main(int argc, char *argv[])
if (hierarchic == -1)
hierarchic = isatty(STDOUT_FILENO); /* default */
+ if (!outarg && (cxt->mode == LSCPU_OUTPUT_CACHES))
+ outarg = getenv("LSCPU_CACHES_COLUMNS");
+ if (!outarg && ((cxt->mode == LSCPU_OUTPUT_PARSABLE) || (cxt->mode == LSCPU_OUTPUT_READABLE)))
+ outarg = getenv("LSCPU_COLUMNS");
+
switch(cxt->mode) {
case LSCPU_OUTPUT_SUMMARY:
print_summary(cxt);
diff --git a/sys-utils/lsmem.1.adoc b/sys-utils/lsmem.1.adoc
index 337c8b4b75..d588051a81 100644
--- a/sys-utils/lsmem.1.adoc
+++ b/sys-utils/lsmem.1.adoc
@@ -65,6 +65,11 @@ This option controls summary lines output. The optional argument _when_ can be *
include::man-common/help-version.adoc[]
+== ENVIRONMENT
+
+LSMEM_COLUMNS=::
+Specifies a comma-separated list of output columns to print. All columns listed in *--help* can be used.
+
== AUTHORS
*lsmem* was originally written by Gerald Schaefer for s390-tools in Perl. The C version for util-linux was written by Clemens von Mann, Heiko Carstens and Karel Zak.
diff --git a/sys-utils/lsmem.c b/sys-utils/lsmem.c
index f36e2b1e30..39967bfc96 100644
--- a/sys-utils/lsmem.c
+++ b/sys-utils/lsmem.c
@@ -691,6 +691,8 @@ int main(int argc, char **argv)
add_column(columns, ncolumns++, COL_BLOCK);
}
+ if (!outarg)
+ outarg = getenv("LSMEM_COLUMNS");
if (outarg && string_add_to_idarray(outarg, columns, ARRAY_SIZE(columns),
&ncolumns, column_name_to_id) < 0)
return EXIT_FAILURE;