From: Karel Zak Date: Wed, 24 Nov 2021 14:00:45 +0000 (+0100) Subject: lsfd: cleanup --summary semantic X-Git-Tag: v2.38-rc1~144^2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=0d681980c4efbcc7f280bae3bcbe8920ba8324eb;p=thirdparty%2Futil-linux.git lsfd: cleanup --summary semantic Signed-off-by: Karel Zak --- diff --git a/misc-utils/lsfd.1.adoc b/misc-utils/lsfd.1.adoc index 1a6149a985..a67fa4ab04 100644 --- a/misc-utils/lsfd.1.adoc +++ b/misc-utils/lsfd.1.adoc @@ -69,7 +69,7 @@ Print the files only satisfying with the condition represented by the _expr_. See also "FILTER EXAMPLES". *-C*, *--counter* _label_:_filter_expr_:: -Define a counter used in *--summary* output. *lsfd* makes a +Define a custom counter used in *--summary* output. *lsfd* makes a counter named _label_. During collect information, *lsfd* counts files satisfying _filter_expr_, and stores the counted number to the counter named _label_. *lsfd* applies filters defined with *--filter* @@ -83,7 +83,7 @@ See also "COUNTER EXAMPLES". *--summary*[=_when_]:: This option controls summary lines output. The optional argument _when_ -can be *never*, *always* or *only*. If the _when_ argument is omitted, +can be *only*, *append* or *never*. If the _when_ argument is omitted, it defaults to *"only"*. + The summary reports counters. A counter comes from a label and an diff --git a/misc-utils/lsfd.c b/misc-utils/lsfd.c index bd18749ccd..1e05958a62 100644 --- a/misc-utils/lsfd.c +++ b/misc-utils/lsfd.c @@ -263,11 +263,6 @@ static struct counter_spec default_counter_specs[] = { } }; -enum { - SUMMARY_EMIT = 1 << 0, - SUMMARY_ONLY = 1 << 1, -}; - struct lsfd_control { struct libscols_table *tb; /* output */ struct list_head procs; /* list of all processes */ @@ -277,7 +272,8 @@ struct lsfd_control { json : 1, notrunc : 1, threads : 1, - summary: 2; + show_main : 1, /* print main table */ + show_summary : 1; /* print summary/counters */ struct lsfd_filter *filter; struct lsfd_counter **counters; /* NULL terminated array. */ @@ -1144,9 +1140,9 @@ static void __attribute__((__noreturn__)) usage(void) fputs(_(" -Q, --filter apply display filter\n"), out); fputs(_(" --debug-filter dump the innternal data structure of filter and exit\n"), out); fputs(_(" -C, --counter :\n" - " make a counter used in --summary output\n"), out); + " define custom counter for --summary output\n"), out); fputs(_(" --dump-counters dump counter definitions\n"), out); - fputs(_(" --summary[=when] print summary information (never,always or only)\n"), out); + fputs(_(" --summary[=when] print summary information (only, append, or never)\n"), out); fputs(USAGE_SEPARATOR, out); printf(USAGE_HELP_OPTIONS(23)); @@ -1393,7 +1389,6 @@ int main(int argc, char *argv[]) int c; size_t i; char *outarg = NULL; - struct lsfd_control ctl = {}; char *filter_expr = NULL; bool debug_filter = false; bool dump_counters = false; @@ -1401,6 +1396,10 @@ int main(int argc, char *argv[]) int n_pids = 0; struct list_head counter_specs; + struct lsfd_control ctl = { + .show_main = 1 + }; + INIT_LIST_HEAD(&counter_specs); enum { @@ -1468,15 +1467,15 @@ int main(int argc, char *argv[]) case OPT_SUMMARY: if (optarg) { if (strcmp(optarg, "never") == 0) - ctl.summary = 0; + ctl.show_summary = 0, ctl.show_main = 1; else if (strcmp(optarg, "only") == 0) - ctl.summary |= (SUMMARY_ONLY|SUMMARY_EMIT); - else if (strcmp(optarg, "always") == 0) - ctl.summary |= SUMMARY_EMIT; + ctl.show_summary = 1, ctl.show_main = 0; + else if (strcmp(optarg, "append") == 0) + ctl.show_summary = 1, ctl.show_main = 1; else errx(EXIT_FAILURE, _("unsupported --summary argument")); } else - ctl.summary |= SUMMARY_EMIT; + ctl.show_summary = 1, ctl.show_main = 0; break; case OPT_DUMP_COUNTERS: dump_counters = true; @@ -1549,7 +1548,7 @@ int main(int argc, char *argv[]) } /* make counters */ - if (ctl.summary & SUMMARY_EMIT) { + if (ctl.show_summary) { if (list_empty(&counter_specs)) ctl.counters = new_default_counters(&ctl); else { @@ -1570,9 +1569,12 @@ int main(int argc, char *argv[]) free(pids); convert(&ctl.procs, &ctl); - if (!(ctl.summary & SUMMARY_ONLY)) + + /* print */ + if (ctl.show_main) emit(&ctl); - if (ctl.counters && (ctl.summary & SUMMARY_EMIT)) + + if (ctl.show_summary && ctl.counters) emit_summary(&ctl, ctl.counters); /* cleanup */