Merge pull request #13201 from yuwata/networkctl-merge-table_add_cell

networkctl cleanups and improvements
This commit is contained in:
Lennart Poettering
2019-07-29 19:32:14 +02:00
committed by GitHub
6 changed files with 477 additions and 204 deletions
+168 -1
View File
@@ -9,6 +9,7 @@
#include "format-table.h"
#include "format-util.h"
#include "gunicode.h"
#include "in-addr-util.h"
#include "memory-util.h"
#include "pager.h"
#include "parse-util.h"
@@ -77,13 +78,18 @@ typedef struct TableData {
uint64_t size;
char string[0];
int int_val;
int8_t int8;
int16_t int16;
int32_t int32;
int64_t int64;
unsigned uint_val;
uint8_t uint8;
uint16_t uint16;
uint32_t uint32;
uint64_t uint64;
int percent; /* we use 'int' as datatype for percent values in order to match the result of parse_percent() */
int ifindex;
union in_addr_union address;
/* … add more here as we start supporting more cell data types … */
};
} TableData;
@@ -246,12 +252,26 @@ static size_t table_data_size(TableDataType type, const void *data) {
case TABLE_UINT32:
return sizeof(uint32_t);
case TABLE_INT16:
case TABLE_UINT16:
return sizeof(uint16_t);
case TABLE_INT8:
case TABLE_UINT8:
return sizeof(uint8_t);
case TABLE_INT:
case TABLE_UINT:
case TABLE_PERCENT:
case TABLE_IFINDEX:
return sizeof(int);
case TABLE_IN_ADDR:
return sizeof(struct in_addr);
case TABLE_IN6_ADDR:
return sizeof(struct in6_addr);
default:
assert_not_reached("Uh? Unexpected cell type");
}
@@ -697,14 +717,19 @@ int table_add_many_internal(Table *t, TableDataType first_type, ...) {
uint64_t size;
usec_t usec;
int int_val;
int8_t int8;
int16_t int16;
int32_t int32;
int64_t int64;
unsigned uint_val;
uint8_t uint8;
uint16_t uint16;
uint32_t uint32;
uint64_t uint64;
int percent;
int ifindex;
bool b;
union in_addr_union address;
} buffer;
switch (type) {
@@ -742,6 +767,24 @@ int table_add_many_internal(Table *t, TableDataType first_type, ...) {
data = &buffer.int_val;
break;
case TABLE_INT8: {
int x = va_arg(ap, int);
assert(x >= INT8_MIN && x <= INT8_MAX);
buffer.int8 = x;
data = &buffer.int8;
break;
}
case TABLE_INT16: {
int x = va_arg(ap, int);
assert(x >= INT16_MIN && x <= INT16_MAX);
buffer.int16 = x;
data = &buffer.int16;
break;
}
case TABLE_INT32:
buffer.int32 = va_arg(ap, int32_t);
data = &buffer.int32;
@@ -757,6 +800,24 @@ int table_add_many_internal(Table *t, TableDataType first_type, ...) {
data = &buffer.uint_val;
break;
case TABLE_UINT8: {
unsigned x = va_arg(ap, unsigned);
assert(x <= UINT8_MAX);
buffer.uint8 = x;
data = &buffer.uint8;
break;
}
case TABLE_UINT16: {
unsigned x = va_arg(ap, unsigned);
assert(x <= UINT16_MAX);
buffer.uint16 = x;
data = &buffer.uint16;
break;
}
case TABLE_UINT32:
buffer.uint32 = va_arg(ap, uint32_t);
data = &buffer.uint32;
@@ -777,6 +838,16 @@ int table_add_many_internal(Table *t, TableDataType first_type, ...) {
data = &buffer.ifindex;
break;
case TABLE_IN_ADDR:
buffer.address = *va_arg(ap, union in_addr_union *);
data = &buffer.address.in;
break;
case TABLE_IN6_ADDR:
buffer.address = *va_arg(ap, union in_addr_union *);
data = &buffer.address.in6;
break;
case TABLE_SET_MINIMUM_WIDTH: {
size_t w = va_arg(ap, size_t);
@@ -955,6 +1026,12 @@ static int cell_data_compare(TableData *a, size_t index_a, TableData *b, size_t
case TABLE_INT:
return CMP(a->int_val, b->int_val);
case TABLE_INT8:
return CMP(a->int8, b->int8);
case TABLE_INT16:
return CMP(a->int16, b->int16);
case TABLE_INT32:
return CMP(a->int32, b->int32);
@@ -964,6 +1041,12 @@ static int cell_data_compare(TableData *a, size_t index_a, TableData *b, size_t
case TABLE_UINT:
return CMP(a->uint_val, b->uint_val);
case TABLE_UINT8:
return CMP(a->uint8, b->uint8);
case TABLE_UINT16:
return CMP(a->uint16, b->uint16);
case TABLE_UINT32:
return CMP(a->uint32, b->uint32);
@@ -976,6 +1059,12 @@ static int cell_data_compare(TableData *a, size_t index_a, TableData *b, size_t
case TABLE_IFINDEX:
return CMP(a->ifindex, b->ifindex);
case TABLE_IN_ADDR:
return CMP(a->address.in.s_addr, b->address.in.s_addr);
case TABLE_IN6_ADDR:
return memcmp(&a->address.in6, &b->address.in6, FAMILY_ADDRESS_SIZE(AF_INET6));
default:
;
}
@@ -1129,6 +1218,30 @@ static const char *table_data_format(TableData *d) {
break;
}
case TABLE_INT8: {
_cleanup_free_ char *p;
p = new(char, DECIMAL_STR_WIDTH(d->int8) + 1);
if (!p)
return NULL;
sprintf(p, "%" PRIi8, d->int8);
d->formatted = TAKE_PTR(p);
break;
}
case TABLE_INT16: {
_cleanup_free_ char *p;
p = new(char, DECIMAL_STR_WIDTH(d->int16) + 1);
if (!p)
return NULL;
sprintf(p, "%" PRIi16, d->int16);
d->formatted = TAKE_PTR(p);
break;
}
case TABLE_INT32: {
_cleanup_free_ char *p;
@@ -1165,6 +1278,30 @@ static const char *table_data_format(TableData *d) {
break;
}
case TABLE_UINT8: {
_cleanup_free_ char *p;
p = new(char, DECIMAL_STR_WIDTH(d->uint8) + 1);
if (!p)
return NULL;
sprintf(p, "%" PRIu8, d->uint8);
d->formatted = TAKE_PTR(p);
break;
}
case TABLE_UINT16: {
_cleanup_free_ char *p;
p = new(char, DECIMAL_STR_WIDTH(d->uint16) + 1);
if (!p)
return NULL;
sprintf(p, "%" PRIu16, d->uint16);
d->formatted = TAKE_PTR(p);
break;
}
case TABLE_UINT32: {
_cleanup_free_ char *p;
@@ -1202,7 +1339,7 @@ static const char *table_data_format(TableData *d) {
}
case TABLE_IFINDEX: {
_cleanup_free_ char *p;
_cleanup_free_ char *p = NULL;
char name[IF_NAMESIZE + 1];
if (format_ifname(d->ifindex, name)) {
@@ -1218,6 +1355,18 @@ static const char *table_data_format(TableData *d) {
break;
}
case TABLE_IN_ADDR:
case TABLE_IN6_ADDR: {
_cleanup_free_ char *p = NULL;
if (in_addr_to_string(d->type == TABLE_IN_ADDR ? AF_INET : AF_INET6,
&d->address, &p) < 0)
return NULL;
d->formatted = TAKE_PTR(p);
break;
}
default:
assert_not_reached("Unexpected type?");
}
@@ -1728,6 +1877,12 @@ static int table_data_to_json(TableData *d, JsonVariant **ret) {
case TABLE_INT:
return json_variant_new_integer(ret, d->int_val);
case TABLE_INT8:
return json_variant_new_integer(ret, d->int8);
case TABLE_INT16:
return json_variant_new_integer(ret, d->int16);
case TABLE_INT32:
return json_variant_new_integer(ret, d->int32);
@@ -1737,6 +1892,12 @@ static int table_data_to_json(TableData *d, JsonVariant **ret) {
case TABLE_UINT:
return json_variant_new_unsigned(ret, d->uint_val);
case TABLE_UINT8:
return json_variant_new_unsigned(ret, d->uint8);
case TABLE_UINT16:
return json_variant_new_unsigned(ret, d->uint16);
case TABLE_UINT32:
return json_variant_new_unsigned(ret, d->uint32);
@@ -1749,6 +1910,12 @@ static int table_data_to_json(TableData *d, JsonVariant **ret) {
case TABLE_IFINDEX:
return json_variant_new_integer(ret, d->ifindex);
case TABLE_IN_ADDR:
return json_variant_new_array_bytes(ret, &d->address, FAMILY_ADDRESS_SIZE(AF_INET));
case TABLE_IN6_ADDR:
return json_variant_new_array_bytes(ret, &d->address, FAMILY_ADDRESS_SIZE(AF_INET6));
default:
return -EINVAL;
}
+6
View File
@@ -20,13 +20,19 @@ typedef enum TableDataType {
TABLE_SIZE,
TABLE_BPS,
TABLE_INT,
TABLE_INT8,
TABLE_INT16,
TABLE_INT32,
TABLE_INT64,
TABLE_UINT,
TABLE_UINT8,
TABLE_UINT16,
TABLE_UINT32,
TABLE_UINT64,
TABLE_PERCENT,
TABLE_IFINDEX,
TABLE_IN_ADDR, /* Takes a union in_addr_union (or a struct in_addr) */
TABLE_IN6_ADDR, /* Takes a union in_addr_union (or a struct in6_addr) */
_TABLE_DATA_TYPE_MAX,
/* The following are not really data types, but commands for table_add_cell_many() to make changes to