Merge pull request #8709 from poettering/format-table
generic table formatter
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,62 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1+ */
|
||||
#pragma once
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#include "macro.h"
|
||||
|
||||
typedef enum TableDataType {
|
||||
TABLE_EMPTY,
|
||||
TABLE_STRING,
|
||||
TABLE_BOOLEAN,
|
||||
TABLE_TIMESTAMP,
|
||||
TABLE_TIMESPAN,
|
||||
TABLE_SIZE,
|
||||
TABLE_UINT32,
|
||||
_TABLE_DATA_TYPE_MAX,
|
||||
_TABLE_DATA_TYPE_INVALID = -1,
|
||||
} TableDataType;
|
||||
|
||||
typedef struct Table Table;
|
||||
typedef struct TableCell TableCell;
|
||||
|
||||
Table *table_new_internal(const char *first_header, ...) _sentinel_;
|
||||
#define table_new(...) table_new_internal(__VA_ARGS__, NULL)
|
||||
Table *table_new_raw(size_t n_columns);
|
||||
Table *table_unref(Table *t);
|
||||
|
||||
DEFINE_TRIVIAL_CLEANUP_FUNC(Table*, table_unref);
|
||||
|
||||
int table_add_cell_full(Table *t, TableCell **ret_cell, TableDataType type, const void *data, size_t minimum_width, size_t maximum_width, unsigned weight, unsigned align_percent, unsigned ellipsize_percent);
|
||||
static inline int table_add_cell(Table *t, TableCell **ret_cell, TableDataType type, const void *data) {
|
||||
return table_add_cell_full(t, ret_cell, type, data, (size_t) -1, (size_t) -1, (unsigned) -1, (unsigned) -1, (unsigned) -1);
|
||||
}
|
||||
|
||||
int table_dup_cell(Table *t, TableCell *cell);
|
||||
|
||||
int table_set_minimum_width(Table *t, TableCell *cell, size_t minimum_width);
|
||||
int table_set_maximum_width(Table *t, TableCell *cell, size_t maximum_width);
|
||||
int table_set_weight(Table *t, TableCell *cell, unsigned weight);
|
||||
int table_set_align_percent(Table *t, TableCell *cell, unsigned percent);
|
||||
int table_set_ellipsize_percent(Table *t, TableCell *cell, unsigned percent);
|
||||
int table_set_color(Table *t, TableCell *cell, const char *color);
|
||||
|
||||
int table_add_many_internal(Table *t, TableDataType first_type, ...);
|
||||
#define table_add_many(t, ...) table_add_many_internal(t, __VA_ARGS__, _TABLE_DATA_TYPE_MAX)
|
||||
|
||||
void table_set_header(Table *table, bool b);
|
||||
void table_set_width(Table *t, size_t width);
|
||||
int table_set_display(Table *t, size_t first_column, ...);
|
||||
int table_set_sort(Table *t, size_t first_column, ...);
|
||||
|
||||
int table_print(Table *t, FILE *f);
|
||||
int table_format(Table *t, char **ret);
|
||||
|
||||
static inline TableCell* TABLE_HEADER_CELL(size_t i) {
|
||||
return SIZE_TO_PTR(i + 1);
|
||||
}
|
||||
|
||||
size_t table_get_rows(Table *t);
|
||||
size_t table_get_columns(Table *t);
|
||||
@@ -365,10 +365,11 @@ const char *special_glyph(SpecialGlyph code) {
|
||||
[BLACK_CIRCLE] = "*",
|
||||
[ARROW] = "->",
|
||||
[MDASH] = "-",
|
||||
[ELLIPSIS] = "..."
|
||||
},
|
||||
|
||||
/* UTF-8 */
|
||||
[ true ] = {
|
||||
[true] = {
|
||||
[TREE_VERTICAL] = "\342\224\202 ", /* │ */
|
||||
[TREE_BRANCH] = "\342\224\234\342\224\200", /* ├─ */
|
||||
[TREE_RIGHT] = "\342\224\224\342\224\200", /* └─ */
|
||||
@@ -377,6 +378,7 @@ const char *special_glyph(SpecialGlyph code) {
|
||||
[BLACK_CIRCLE] = "\342\227\217", /* ● */
|
||||
[ARROW] = "\342\206\222", /* → */
|
||||
[MDASH] = "\342\200\223", /* – */
|
||||
[ELLIPSIS] = "\342\200\246", /* … */
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
@@ -53,6 +53,7 @@ typedef enum {
|
||||
BLACK_CIRCLE,
|
||||
ARROW,
|
||||
MDASH,
|
||||
ELLIPSIS,
|
||||
_SPECIAL_GLYPH_MAX
|
||||
} SpecialGlyph;
|
||||
|
||||
|
||||
@@ -77,6 +77,8 @@ basic_sources = files('''
|
||||
fileio-label.h
|
||||
fileio.c
|
||||
fileio.h
|
||||
format-table.c
|
||||
format-table.h
|
||||
format-util.h
|
||||
fs-util.c
|
||||
fs-util.h
|
||||
@@ -127,6 +129,8 @@ basic_sources = files('''
|
||||
nss-util.h
|
||||
ordered-set.c
|
||||
ordered-set.h
|
||||
pager.c
|
||||
pager.h
|
||||
parse-util.c
|
||||
parse-util.h
|
||||
path-util.c
|
||||
|
||||
@@ -0,0 +1,211 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1+ */
|
||||
/***
|
||||
This file is part of systemd.
|
||||
|
||||
Copyright 2010 Lennart Poettering
|
||||
***/
|
||||
|
||||
#include <errno.h>
|
||||
#include <signal.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/prctl.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "copy.h"
|
||||
#include "fd-util.h"
|
||||
#include "locale-util.h"
|
||||
#include "log.h"
|
||||
#include "macro.h"
|
||||
#include "pager.h"
|
||||
#include "process-util.h"
|
||||
#include "signal-util.h"
|
||||
#include "string-util.h"
|
||||
#include "strv.h"
|
||||
#include "terminal-util.h"
|
||||
|
||||
static pid_t pager_pid = 0;
|
||||
|
||||
static int stored_stdout = -1;
|
||||
static int stored_stderr = -1;
|
||||
static bool stdout_redirected = false;
|
||||
static bool stderr_redirected = false;
|
||||
|
||||
_noreturn_ static void pager_fallback(void) {
|
||||
int r;
|
||||
|
||||
r = copy_bytes(STDIN_FILENO, STDOUT_FILENO, (uint64_t) -1, 0);
|
||||
if (r < 0) {
|
||||
log_error_errno(r, "Internal pager failed: %m");
|
||||
_exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
_exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
int pager_open(bool no_pager, bool jump_to_end) {
|
||||
_cleanup_close_pair_ int fd[2] = { -1, -1 };
|
||||
const char *pager;
|
||||
int r;
|
||||
|
||||
if (no_pager)
|
||||
return 0;
|
||||
|
||||
if (pager_pid > 0)
|
||||
return 1;
|
||||
|
||||
if (terminal_is_dumb())
|
||||
return 0;
|
||||
|
||||
if (!is_main_thread())
|
||||
return -EPERM;
|
||||
|
||||
pager = getenv("SYSTEMD_PAGER");
|
||||
if (!pager)
|
||||
pager = getenv("PAGER");
|
||||
|
||||
/* If the pager is explicitly turned off, honour it */
|
||||
if (pager && STR_IN_SET(pager, "", "cat"))
|
||||
return 0;
|
||||
|
||||
/* 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");
|
||||
|
||||
r = safe_fork("(pager)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_LOG, &pager_pid);
|
||||
if (r < 0)
|
||||
return r;
|
||||
if (r == 0) {
|
||||
const char* less_opts, *less_charset;
|
||||
|
||||
/* In the child start the pager */
|
||||
|
||||
(void) dup2(fd[0], STDIN_FILENO);
|
||||
safe_close_pair(fd);
|
||||
|
||||
/* Initialize a good set of less options */
|
||||
less_opts = getenv("SYSTEMD_LESS");
|
||||
if (!less_opts)
|
||||
less_opts = "FRSXMK";
|
||||
if (jump_to_end)
|
||||
less_opts = strjoina(less_opts, " +G");
|
||||
if (setenv("LESS", less_opts, 1) < 0)
|
||||
_exit(EXIT_FAILURE);
|
||||
|
||||
/* Initialize a good charset for less. This is
|
||||
* particularly important if we output UTF-8
|
||||
* characters. */
|
||||
less_charset = getenv("SYSTEMD_LESSCHARSET");
|
||||
if (!less_charset && is_locale_utf8())
|
||||
less_charset = "utf-8";
|
||||
if (less_charset &&
|
||||
setenv("LESSCHARSET", less_charset, 1) < 0)
|
||||
_exit(EXIT_FAILURE);
|
||||
|
||||
if (pager) {
|
||||
execlp(pager, pager, NULL);
|
||||
execl("/bin/sh", "sh", "-c", pager, NULL);
|
||||
}
|
||||
|
||||
/* Debian's alternatives command for pagers is
|
||||
* called 'pager'. Note that we do not call
|
||||
* sensible-pagers here, since that is just a
|
||||
* shell script that implements a logic that
|
||||
* is similar to this one anyway, but is
|
||||
* Debian-specific. */
|
||||
execlp("pager", "pager", NULL);
|
||||
|
||||
execlp("less", "less", NULL);
|
||||
execlp("more", "more", NULL);
|
||||
|
||||
pager_fallback();
|
||||
/* not reached */
|
||||
}
|
||||
|
||||
/* Return in the parent */
|
||||
stored_stdout = fcntl(STDOUT_FILENO, F_DUPFD_CLOEXEC, 3);
|
||||
if (dup2(fd[1], STDOUT_FILENO) < 0) {
|
||||
stored_stdout = safe_close(stored_stdout);
|
||||
return log_error_errno(errno, "Failed to duplicate pager pipe: %m");
|
||||
}
|
||||
stdout_redirected = true;
|
||||
|
||||
stored_stderr = fcntl(STDERR_FILENO, F_DUPFD_CLOEXEC, 3);
|
||||
if (dup2(fd[1], STDERR_FILENO) < 0) {
|
||||
stored_stderr = safe_close(stored_stderr);
|
||||
return log_error_errno(errno, "Failed to duplicate pager pipe: %m");
|
||||
}
|
||||
stderr_redirected = true;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
void pager_close(void) {
|
||||
|
||||
if (pager_pid <= 0)
|
||||
return;
|
||||
|
||||
/* Inform pager that we are done */
|
||||
(void) fflush(stdout);
|
||||
if (stdout_redirected)
|
||||
if (stored_stdout < 0 || dup2(stored_stdout, STDOUT_FILENO) < 0)
|
||||
(void) close(STDOUT_FILENO);
|
||||
stored_stdout = safe_close(stored_stdout);
|
||||
(void) fflush(stderr);
|
||||
if (stderr_redirected)
|
||||
if (stored_stderr < 0 || dup2(stored_stderr, STDERR_FILENO) < 0)
|
||||
(void) close(STDERR_FILENO);
|
||||
stored_stderr = safe_close(stored_stderr);
|
||||
stdout_redirected = stderr_redirected = false;
|
||||
|
||||
(void) kill(pager_pid, SIGCONT);
|
||||
(void) wait_for_terminate(pager_pid, NULL);
|
||||
pager_pid = 0;
|
||||
}
|
||||
|
||||
bool pager_have(void) {
|
||||
return pager_pid > 0;
|
||||
}
|
||||
|
||||
int show_man_page(const char *desc, bool null_stdio) {
|
||||
const char *args[4] = { "man", NULL, NULL, NULL };
|
||||
char *e = NULL;
|
||||
pid_t pid;
|
||||
size_t k;
|
||||
int r;
|
||||
|
||||
k = strlen(desc);
|
||||
|
||||
if (desc[k-1] == ')')
|
||||
e = strrchr(desc, '(');
|
||||
|
||||
if (e) {
|
||||
char *page = NULL, *section = NULL;
|
||||
|
||||
page = strndupa(desc, e - desc);
|
||||
section = strndupa(e + 1, desc + k - e - 2);
|
||||
|
||||
args[1] = section;
|
||||
args[2] = page;
|
||||
} else
|
||||
args[1] = desc;
|
||||
|
||||
r = safe_fork("(man)", FORK_RESET_SIGNALS|FORK_DEATHSIG|(null_stdio ? FORK_NULL_STDIO : 0)|FORK_LOG, &pid);
|
||||
if (r < 0)
|
||||
return r;
|
||||
if (r == 0) {
|
||||
/* Child */
|
||||
execvp(args[0], (char**) args);
|
||||
log_error_errno(errno, "Failed to execute man: %m");
|
||||
_exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
return wait_for_terminate_and_check(NULL, pid, 0);
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1+ */
|
||||
#pragma once
|
||||
|
||||
/***
|
||||
This file is part of systemd.
|
||||
|
||||
Copyright 2010 Lennart Poettering
|
||||
***/
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "macro.h"
|
||||
|
||||
int pager_open(bool no_pager, bool jump_to_end);
|
||||
void pager_close(void);
|
||||
bool pager_have(void) _pure_;
|
||||
|
||||
int show_man_page(const char *page, bool null_stdio);
|
||||
+68
-25
@@ -15,6 +15,7 @@
|
||||
|
||||
#include "alloc-util.h"
|
||||
#include "gunicode.h"
|
||||
#include "locale-util.h"
|
||||
#include "macro.h"
|
||||
#include "string-util.h"
|
||||
#include "terminal-util.h"
|
||||
@@ -452,62 +453,104 @@ bool string_has_cc(const char *p, const char *ok) {
|
||||
}
|
||||
|
||||
static char *ascii_ellipsize_mem(const char *s, size_t old_length, size_t new_length, unsigned percent) {
|
||||
size_t x;
|
||||
size_t x, need_space;
|
||||
char *r;
|
||||
|
||||
assert(s);
|
||||
assert(percent <= 100);
|
||||
assert(new_length >= 3);
|
||||
assert(new_length != (size_t) -1);
|
||||
|
||||
if (old_length <= 3 || old_length <= new_length)
|
||||
if (old_length <= new_length)
|
||||
return strndup(s, old_length);
|
||||
|
||||
r = new0(char, new_length+3);
|
||||
/* Special case short ellipsations */
|
||||
switch (new_length) {
|
||||
|
||||
case 0:
|
||||
return strdup("");
|
||||
|
||||
case 1:
|
||||
if (is_locale_utf8())
|
||||
return strdup("…");
|
||||
else
|
||||
return strdup(".");
|
||||
|
||||
case 2:
|
||||
if (!is_locale_utf8())
|
||||
return strdup("..");
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
/* Calculate how much space the ellipsis will take up. If we are in UTF-8 mode we only need space for one
|
||||
* character ("…"), otherwise for three characters ("..."). Note that in both cases we need 3 bytes of storage,
|
||||
* either for the UTF-8 encoded character or for three ASCII characters. */
|
||||
need_space = is_locale_utf8() ? 1 : 3;
|
||||
|
||||
r = new(char, new_length+3);
|
||||
if (!r)
|
||||
return NULL;
|
||||
|
||||
x = (new_length * percent) / 100;
|
||||
assert(new_length >= need_space);
|
||||
|
||||
if (x > new_length - 3)
|
||||
x = new_length - 3;
|
||||
x = ((new_length - need_space) * percent + 50) / 100;
|
||||
assert(x <= new_length - need_space);
|
||||
|
||||
memcpy(r, s, x);
|
||||
r[x] = 0xe2; /* tri-dot ellipsis: … */
|
||||
r[x+1] = 0x80;
|
||||
r[x+2] = 0xa6;
|
||||
|
||||
if (is_locale_utf8()) {
|
||||
r[x+0] = 0xe2; /* tri-dot ellipsis: … */
|
||||
r[x+1] = 0x80;
|
||||
r[x+2] = 0xa6;
|
||||
} else {
|
||||
r[x+0] = '.';
|
||||
r[x+1] = '.';
|
||||
r[x+2] = '.';
|
||||
}
|
||||
|
||||
memcpy(r + x + 3,
|
||||
s + old_length - (new_length - x - 1),
|
||||
new_length - x - 1);
|
||||
s + old_length - (new_length - x - need_space),
|
||||
new_length - x - need_space + 1);
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
char *ellipsize_mem(const char *s, size_t old_length, size_t new_length, unsigned percent) {
|
||||
size_t x;
|
||||
char *e;
|
||||
size_t x, k, len, len2;
|
||||
const char *i, *j;
|
||||
unsigned k, len, len2;
|
||||
char *e;
|
||||
int r;
|
||||
|
||||
/* Note that 'old_length' refers to bytes in the string, while 'new_length' refers to character cells taken up
|
||||
* on screen. This distinction doesn't matter for ASCII strings, but it does matter for non-ASCII UTF-8
|
||||
* strings.
|
||||
*
|
||||
* Ellipsation is done in a locale-dependent way:
|
||||
* 1. If the string passed in is fully ASCII and the current locale is not UTF-8, three dots are used ("...")
|
||||
* 2. Otherwise, a unicode ellipsis is used ("…")
|
||||
*
|
||||
* In other words: you'll get a unicode ellipsis as soon as either the string contains non-ASCII characters or
|
||||
* the current locale is UTF-8.
|
||||
*/
|
||||
|
||||
assert(s);
|
||||
assert(percent <= 100);
|
||||
|
||||
if (new_length == (size_t) -1)
|
||||
return strndup(s, old_length);
|
||||
|
||||
assert(new_length >= 3);
|
||||
if (new_length == 0)
|
||||
return strdup("");
|
||||
|
||||
/* if no multibyte characters use ascii_ellipsize_mem for speed */
|
||||
/* If no multibyte characters use ascii_ellipsize_mem for speed */
|
||||
if (ascii_is_valid(s))
|
||||
return ascii_ellipsize_mem(s, old_length, new_length, percent);
|
||||
|
||||
if (old_length <= 3 || old_length <= new_length)
|
||||
return strndup(s, old_length);
|
||||
|
||||
x = (new_length * percent) / 100;
|
||||
|
||||
if (x > new_length - 3)
|
||||
x = new_length - 3;
|
||||
x = ((new_length - 1) * percent) / 100;
|
||||
assert(x <= new_length - 1);
|
||||
|
||||
k = 0;
|
||||
for (i = s; k < x && i < s + old_length; i = utf8_next_char(i)) {
|
||||
@@ -552,7 +595,7 @@ char *ellipsize_mem(const char *s, size_t old_length, size_t new_length, unsigne
|
||||
*/
|
||||
|
||||
memcpy(e, s, len);
|
||||
e[len] = 0xe2; /* tri-dot ellipsis: … */
|
||||
e[len + 0] = 0xe2; /* tri-dot ellipsis: … */
|
||||
e[len + 1] = 0x80;
|
||||
e[len + 2] = 0xa6;
|
||||
|
||||
|
||||
@@ -35,6 +35,7 @@
|
||||
#include <string.h>
|
||||
|
||||
#include "alloc-util.h"
|
||||
#include "gunicode.h"
|
||||
#include "hexdecoct.h"
|
||||
#include "macro.h"
|
||||
#include "utf8.h"
|
||||
@@ -414,3 +415,23 @@ size_t utf8_n_codepoints(const char *str) {
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
size_t utf8_console_width(const char *str) {
|
||||
size_t n = 0;
|
||||
|
||||
/* Returns the approximate width a string will take on screen when printed on a character cell
|
||||
* terminal/console. */
|
||||
|
||||
while (*str != 0) {
|
||||
char32_t c;
|
||||
|
||||
if (utf8_encoded_to_unichar(str, &c) < 0)
|
||||
return (size_t) -1;
|
||||
|
||||
str = utf8_next_char(str);
|
||||
|
||||
n += unichar_iswide(c) ? 2 : 1;
|
||||
}
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
@@ -48,3 +48,4 @@ static inline char32_t utf16_surrogate_pair_to_unichar(char16_t lead, char16_t t
|
||||
}
|
||||
|
||||
size_t utf8_n_codepoints(const char *str);
|
||||
size_t utf8_console_width(const char *str);
|
||||
|
||||
@@ -111,6 +111,14 @@ static inline void qsort_safe(void *base, size_t nmemb, size_t size, comparison_
|
||||
qsort_safe((p), (n), sizeof((p)[0]), (__compar_fn_t) _func_); \
|
||||
})
|
||||
|
||||
static inline void qsort_r_safe(void *base, size_t nmemb, size_t size, int (*compar)(const void*, const void*, void*), void *userdata) {
|
||||
if (nmemb <= 1)
|
||||
return;
|
||||
|
||||
assert(base);
|
||||
qsort_r(base, nmemb, size, compar, userdata);
|
||||
}
|
||||
|
||||
/**
|
||||
* Normal memcpy requires src to be nonnull. We do nothing if n is 0.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user