Merge pull request #7301 from poettering/loginctl-ellipsize

Fix loginctl seat sysfs tree ellipsation logic.

Simple reproducer:
loginctl --full seat-status seat0|cat
→ after this PR, all lines are shown in full. Before, lines were ellipsized to terminal width.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2017-11-12 16:25:54 +01:00 committed by GitHub
commit edc3eff50f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 49 additions and 32 deletions

View file

@ -472,6 +472,10 @@ char *ellipsize_mem(const char *s, size_t old_length, size_t new_length, unsigne
assert(s);
assert(percent <= 100);
if (new_length == (size_t) -1)
return strndup(s, old_length);
assert(new_length >= 3);
/* if no multibyte characters use ascii_ellipsize_mem for speed */
@ -539,6 +543,10 @@ char *ellipsize_mem(const char *s, size_t old_length, size_t new_length, unsigne
}
char *ellipsize(const char *s, size_t length, unsigned percent) {
if (length == (size_t) -1)
return strdup(s);
return ellipsize_mem(s, strlen(s), length, percent);
}

View file

@ -2010,7 +2010,7 @@ static int busctl_main(sd_bus *bus, int argc, char *argv[]) {
}
int main(int argc, char *argv[]) {
sd_bus *bus = NULL;
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
int r;
log_parse_environment();
@ -2100,7 +2100,6 @@ int main(int argc, char *argv[]) {
r = busctl_main(bus, argc, argv);
finish:
sd_bus_flush_close_unref(bus);
pager_close();
strv_free(arg_matches);

View file

@ -594,7 +594,7 @@ static int localectl_main(sd_bus *bus, int argc, char *argv[]) {
}
int main(int argc, char*argv[]) {
sd_bus *bus = NULL;
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
int r;
setlocale(LC_ALL, "");
@ -614,7 +614,6 @@ int main(int argc, char*argv[]) {
r = localectl_main(bus, argc, argv);
finish:
sd_bus_flush_close_unref(bus);
pager_close();
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;

View file

@ -37,6 +37,7 @@
#include "pager.h"
#include "parse-util.h"
#include "process-util.h"
#include "sigbus.h"
#include "signal-util.h"
#include "spawn-polkit-agent.h"
#include "strv.h"
@ -65,8 +66,7 @@ static OutputFlags get_output_flags(void) {
return
arg_all * OUTPUT_SHOW_ALL |
arg_full * OUTPUT_FULL_WIDTH |
(!on_tty() || pager_have()) * OUTPUT_FULL_WIDTH |
(arg_full || !on_tty() || pager_have()) * OUTPUT_FULL_WIDTH |
colors_enabled() * OUTPUT_COLOR;
}
@ -714,7 +714,7 @@ static int print_seat_status_info(sd_bus *bus, const char *path, bool *new_line)
printf("\t Devices:\n");
show_sysfs(i.id, "\t\t ", c);
show_sysfs(i.id, "\t\t ", c, get_output_flags());
}
return 0;
@ -1583,12 +1583,13 @@ static int loginctl_main(int argc, char *argv[], sd_bus *bus) {
}
int main(int argc, char *argv[]) {
sd_bus *bus = NULL;
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
int r;
setlocale(LC_ALL, "");
log_parse_environment();
log_open();
sigbus_install();
r = parse_argv(argc, argv);
if (r <= 0)
@ -1605,8 +1606,6 @@ int main(int argc, char *argv[]) {
r = loginctl_main(argc, argv, bus);
finish:
sd_bus_flush_close_unref(bus);
pager_close();
polkit_agent_close();

View file

@ -37,13 +37,23 @@ static int show_sysfs_one(
struct udev_list_entry **item,
const char *sub,
const char *prefix,
unsigned n_columns) {
unsigned n_columns,
OutputFlags flags) {
size_t max_width;
assert(udev);
assert(seat);
assert(item);
assert(prefix);
if (flags & OUTPUT_FULL_WIDTH)
max_width = (size_t) -1;
else if (n_columns < 10)
max_width = 10;
else
max_width = n_columns;
while (*item) {
_cleanup_udev_device_unref_ struct udev_device *d = NULL;
struct udev_list_entry *next, *lookahead;
@ -106,7 +116,7 @@ static int show_sysfs_one(
lookahead = udev_list_entry_get_next(lookahead);
}
k = ellipsize(sysfs, n_columns, 20);
k = ellipsize(sysfs, max_width, 20);
if (!k)
return -ENOMEM;
@ -120,7 +130,7 @@ static int show_sysfs_one(
return -ENOMEM;
free(k);
k = ellipsize(l, n_columns, 70);
k = ellipsize(l, max_width, 70);
if (!k)
return -ENOMEM;
@ -134,14 +144,16 @@ static int show_sysfs_one(
if (!p)
return -ENOMEM;
show_sysfs_one(udev, seat, item, sysfs, p, n_columns - 2);
show_sysfs_one(udev, seat, item, sysfs, p,
n_columns == (unsigned) -1 || n_columns < 2 ? n_columns : n_columns - 2,
flags);
}
}
return 0;
}
int show_sysfs(const char *seat, const char *prefix, unsigned n_columns) {
int show_sysfs(const char *seat, const char *prefix, unsigned n_columns, OutputFlags flags) {
_cleanup_udev_enumerate_unref_ struct udev_enumerate *e = NULL;
_cleanup_udev_unref_ struct udev *udev = NULL;
struct udev_list_entry *first = NULL;
@ -150,8 +162,7 @@ int show_sysfs(const char *seat, const char *prefix, unsigned n_columns) {
if (n_columns <= 0)
n_columns = columns();
if (!prefix)
prefix = "";
prefix = strempty(prefix);
if (isempty(seat))
seat = "seat0";
@ -181,7 +192,7 @@ int show_sysfs(const char *seat, const char *prefix, unsigned n_columns) {
first = udev_enumerate_get_list_entry(e);
if (first)
show_sysfs_one(udev, seat, &first, "/", prefix, n_columns);
show_sysfs_one(udev, seat, &first, "/", prefix, n_columns, flags);
else
printf("%s%s%s\n", prefix, special_glyph(TREE_RIGHT), "(none)");

View file

@ -19,4 +19,8 @@
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
int show_sysfs(const char *seat, const char *prefix, unsigned columns);
#include <sys/types.h>
#include "output-mode.h"
int show_sysfs(const char *seat, const char *prefix, unsigned columns, OutputFlags flags);

View file

@ -52,15 +52,16 @@
#include "path-util.h"
#include "process-util.h"
#include "ptyfwd.h"
#include "sigbus.h"
#include "signal-util.h"
#include "spawn-polkit-agent.h"
#include "stdio-util.h"
#include "strv.h"
#include "terminal-util.h"
#include "unit-name.h"
#include "util.h"
#include "verbs.h"
#include "web-util.h"
#include "stdio-util.h"
#define ALL_IP_ADDRESSES -1
@ -92,8 +93,7 @@ static int print_addresses(sd_bus *bus, const char *name, int, const char *pr1,
static OutputFlags get_output_flags(void) {
return
arg_all * OUTPUT_SHOW_ALL |
arg_full * OUTPUT_FULL_WIDTH |
(!on_tty() || pager_have()) * OUTPUT_FULL_WIDTH |
(arg_full || !on_tty() || pager_have()) * OUTPUT_FULL_WIDTH |
colors_enabled() * OUTPUT_COLOR |
!arg_quiet * OUTPUT_WARN_CUTOFF;
}
@ -3045,12 +3045,13 @@ static int machinectl_main(int argc, char *argv[], sd_bus *bus) {
}
int main(int argc, char*argv[]) {
sd_bus *bus = NULL;
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
int r;
setlocale(LC_ALL, "");
log_parse_environment();
log_open();
sigbus_install();
r = parse_argv(argc, argv);
if (r <= 0)
@ -3067,7 +3068,6 @@ int main(int argc, char*argv[]) {
r = machinectl_main(argc, argv, bus);
finish:
sd_bus_flush_close_unref(bus);
pager_close();
polkit_agent_close();

View file

@ -1619,8 +1619,6 @@ int main(int argc, char* argv[]) {
}
finish:
bus = sd_bus_flush_close_unref(bus);
pager_close();
free(arg_mount_what);

View file

@ -80,9 +80,10 @@ int pager_open(bool no_pager, bool jump_to_end) {
if (pager && STR_IN_SET(pager, "", "cat"))
return 0;
/* Determine and cache number of columns before we spawn the
* pager so that we get the value from the actual tty */
/* Determine and cache number of columns/lines before we spawn the pager so that we get the value from the
* actual tty */
(void) columns();
(void) lines();
if (pipe2(fd, O_CLOEXEC) < 0)
return log_error_errno(errno, "Failed to create pager pipe: %m");

View file

@ -280,8 +280,7 @@ static void polkit_agent_open_maybe(void) {
static OutputFlags get_output_flags(void) {
return
arg_all * OUTPUT_SHOW_ALL |
arg_full * OUTPUT_FULL_WIDTH |
(!on_tty() || pager_have()) * OUTPUT_FULL_WIDTH |
(arg_full || !on_tty() || pager_have()) * OUTPUT_FULL_WIDTH |
colors_enabled() * OUTPUT_COLOR |
!arg_quiet * OUTPUT_WARN_CUTOFF;
}

View file

@ -472,7 +472,7 @@ static int timedatectl_main(sd_bus *bus, int argc, char *argv[]) {
}
int main(int argc, char *argv[]) {
sd_bus *bus = NULL;
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
int r;
setlocale(LC_ALL, "");
@ -492,7 +492,6 @@ int main(int argc, char *argv[]) {
r = timedatectl_main(bus, argc, argv);
finish:
sd_bus_flush_close_unref(bus);
pager_close();
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;