diff --git a/docs/man/Makefile.am b/docs/man/Makefile.am index 976e2f35c7..3b62b95261 100644 --- a/docs/man/Makefile.am +++ b/docs/man/Makefile.am @@ -200,6 +200,7 @@ SRC_DEV_PAGES = \ nutscan_display_parsable.txt \ nutscan_init_ip_ranges.txt \ nutscan_free_ip_ranges.txt \ + nutscan_stringify_ip_ranges.txt \ nutscan_add_ip_range.txt \ nutscan_ip_ranges_iter_init.txt \ nutscan_ip_ranges_iter_inc.txt \ @@ -323,6 +324,7 @@ MAN3_DEV_PAGES = \ nutscan_display_parsable.3 \ nutscan_init_ip_ranges.3 \ nutscan_free_ip_ranges.3 \ + nutscan_stringify_ip_ranges.3 \ nutscan_add_ip_range.3 \ nutscan_ip_ranges_iter_init.3 \ nutscan_ip_ranges_iter_inc.3 \ @@ -415,6 +417,7 @@ HTML_DEV_MANS = \ nutscan_display_parsable.html \ nutscan_init_ip_ranges.html \ nutscan_free_ip_ranges.html \ + nutscan_stringify_ip_ranges.html \ nutscan_add_ip_range.html \ nutscan_ip_ranges_iter_init.html \ nutscan_ip_ranges_iter_inc.html \ diff --git a/docs/man/nutscan_add_ip_range.txt b/docs/man/nutscan_add_ip_range.txt index 1d27810c1b..8270c6cc69 100644 --- a/docs/man/nutscan_add_ip_range.txt +++ b/docs/man/nutscan_add_ip_range.txt @@ -71,6 +71,7 @@ SEE ALSO -------- linkman:nutscan_init_ip_ranges[3], linkman:nutscan_free_ip_ranges[3], +linkman:nutscan_stringify_ip_ranges[3], linkman:nutscan_cidr_to_ip[3], linkman:nutscan_ip_ranges_iter_init[3], linkman:nutscan_ip_ranges_iter_inc[3] diff --git a/docs/man/nutscan_free_ip_ranges.txt b/docs/man/nutscan_free_ip_ranges.txt index f9baa2848c..567e89dcfe 100644 --- a/docs/man/nutscan_free_ip_ranges.txt +++ b/docs/man/nutscan_free_ip_ranges.txt @@ -37,6 +37,7 @@ SEE ALSO -------- linkman:nutscan_init_ip_ranges[3], linkman:nutscan_add_ip_range[3], +linkman:nutscan_stringify_ip_ranges[3], linkman:nutscan_cidr_to_ip[3], linkman:nutscan_ip_ranges_iter_init[3], linkman:nutscan_ip_ranges_iter_inc[3] diff --git a/docs/man/nutscan_init_ip_ranges.txt b/docs/man/nutscan_init_ip_ranges.txt index 2eabdaa872..73094aaefc 100644 --- a/docs/man/nutscan_init_ip_ranges.txt +++ b/docs/man/nutscan_init_ip_ranges.txt @@ -38,6 +38,7 @@ SEE ALSO -------- linkman:nutscan_free_ip_ranges[3], linkman:nutscan_add_ip_range[3], +linkman:nutscan_stringify_ip_ranges[3], linkman:nutscan_cidr_to_ip[3], linkman:nutscan_ip_ranges_iter_init[3], linkman:nutscan_ip_ranges_iter_inc[3] diff --git a/docs/man/nutscan_stringify_ip_ranges.txt b/docs/man/nutscan_stringify_ip_ranges.txt new file mode 100644 index 0000000000..cc14793f10 --- /dev/null +++ b/docs/man/nutscan_stringify_ip_ranges.txt @@ -0,0 +1,39 @@ +NUTSCAN_STRINGIFY_IP_RANGES(3) +============================== + +NAME +---- + +nutscan_stringify_ip_ranges - Collect contents of a `nutscan_ip_range_list_t` +structure into a string buffer that can be further printed into logs. + +SYNOPSIS +-------- + + #include + + const char * nutscan_stringify_ip_ranges(nutscan_ip_range_list_t *irl); + +DESCRIPTION +----------- + +The *nutscan_stringify_ip_ranges()* function can walk a `nutscan_ip_range_list_t` +structure to report its contents: count of list items, and a comma-separated +listing with each item as a single token (if `start_ip==end_ip` in that range) +or a range as `start_ip .. end_ip`. + +Returns a pointer to internal statically allocated buffer which would be +overwritten by subsequent calls, but does not have to be freed by caller. + +NOTES +----- + +Technically, the function is currently defined in 'nutscan-ip.h' file. + +SEE ALSO +-------- + +linkman:nutscan_free_ip_ranges[3], linkman:nutscan_add_ip_range[3], +linkman:nutscan_cidr_to_ip[3], +linkman:nutscan_ip_ranges_iter_init[3], +linkman:nutscan_ip_ranges_iter_inc[3] diff --git a/docs/nut.dict b/docs/nut.dict index 2f15275041..6bb3967ff3 100644 --- a/docs/nut.dict +++ b/docs/nut.dict @@ -1,4 +1,4 @@ -personal_ws-1.1 en 3185 utf-8 +personal_ws-1.1 en 3186 utf-8 AAC AAS ABI @@ -2831,6 +2831,7 @@ strcpy strdup strerror strftime +stringify strlen strnlen strptime diff --git a/tools/nut-scanner/nutscan-ip.c b/tools/nut-scanner/nutscan-ip.c index 22a7002f29..0d73b7f039 100644 --- a/tools/nut-scanner/nutscan-ip.c +++ b/tools/nut-scanner/nutscan-ip.c @@ -189,6 +189,54 @@ size_t nutscan_add_ip_range(nutscan_ip_range_list_t *irl, char * start_ip, char return irl->ip_ranges_count; } +const char * nutscan_stringify_ip_ranges(nutscan_ip_range_list_t *irl) +{ + static char buf[LARGEBUF - 64]; /* Leave some space for upsdebugx() prefixes */ + size_t len = 0; + + memset(buf, 0, sizeof(buf)); + len += snprintf(buf + len, sizeof(buf) - len, + "(%" PRIuSIZE ")[", + (irl ? irl->ip_ranges_count : 0)); + + if (irl && irl->ip_ranges && irl->ip_ranges_count) { + nutscan_ip_range_t *p; + size_t j; + + for ( + j = 0, p = irl->ip_ranges; + p && len < sizeof(buf) - 6; + p = p->next, j++ + ) { + if (j) { + buf[len++] = ','; + buf[len++] = ' '; + } + + if (len > sizeof(buf) - 6) { + /* Too little left, but enough for this */ + buf[len++] = '.'; + buf[len++] = '.'; + buf[len++] = '.'; + break; + } + + if (p->start_ip == p->end_ip || !strcmp(p->start_ip, p->end_ip)) { + len += snprintf(buf + len, sizeof(buf) - len, + "%s", p->start_ip); + } else { + len += snprintf(buf + len, sizeof(buf) - len, + "%s .. %s", p->start_ip, p->end_ip); + } + } + } + + if (len < sizeof(buf) - 1) + buf[len++] = ']'; + + return buf; +} + /* Return the first ip or NULL if error */ char * nutscan_ip_ranges_iter_init(nutscan_ip_range_list_iter_t *irliter, const nutscan_ip_range_list_t *irl) { diff --git a/tools/nut-scanner/nutscan-ip.h b/tools/nut-scanner/nutscan-ip.h index 82b2035980..a585d8d3ca 100644 --- a/tools/nut-scanner/nutscan-ip.h +++ b/tools/nut-scanner/nutscan-ip.h @@ -87,6 +87,10 @@ nutscan_ip_range_list_t *nutscan_init_ip_ranges(nutscan_ip_range_list_t *irl); */ void nutscan_free_ip_ranges(nutscan_ip_range_list_t *irl); +/* Prints contents of irl into a groovy-like string, + * using a static buffer (rewritten by each call) */ +const char * nutscan_stringify_ip_ranges(nutscan_ip_range_list_t *irl); + size_t nutscan_add_ip_range(nutscan_ip_range_list_t *irl, char * start_ip, char * end_ip); /* Iterator over given nutscan_ip_range_list_t structure