Skip to content

Commit

Permalink
common/common.c, include/common.h: introduce minimize_formatting_stri…
Browse files Browse the repository at this point in the history
…ng() [networkupstools#2450]

...and minimize_formatting_string_staticbuf() for one-shot use-cases.

Signed-off-by: Jim Klimov <[email protected]>
  • Loading branch information
jimklimov committed May 31, 2024
1 parent 0d2fd43 commit af1f256
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 2 deletions.
78 changes: 77 additions & 1 deletion common/common.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* common.c - common useful functions
Copyright (C) 2000 Russell Kroll <[email protected]>
Copyright (C) 2021-2022 Jim Klimov <[email protected]>
Copyright (C) 2021-2024 Jim Klimov <[email protected]>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -1284,6 +1284,82 @@ void nut_report_config_flags(void)
);
}

char * minimize_formatting_string(char *buf, size_t buflen, const char *fmt)
{
/* Return the bare-bone formatting string contained in "fmt"
* as "%" characters followed immediately by ASCII letters,
* or null if "fmt" is null (empty string if it has no "%"),
* or "buf" is null, or "buflen" is too short.
* Allows to compare different formatting strings to check
* if they describe the same expected amounts of arguments
* and their types.
* Uses a caller-specified buffer and returns a pointer to
* it, or NULL upon errors.
*/
const char *p;
char *b, inEscape;
size_t i;

if (!fmt || !buf || buflen == 0)
return NULL;

for (b = buf, p = fmt, i = 0, inEscape = 0;
(*p != '\0' && i < buflen);
p++
) {
if (*p == '%') {
if (*(p+1) == '%') {
/* Escaped percent character, not a variable indicator */
p++;
} else {
inEscape = 1;
*b++ = *p;
i++;
}
continue;
}

if (inEscape) {
/* Did we hit a printf format character? */
if (*p == '%')
continue;
if ((*p >= 'a' && *p <= 'z') || (*p >= 'A' && *p <= 'Z')) {
inEscape = 0;
*b++ = *p;
i++;
}
}
}

if (inEscape) {
upsdebugx(1, "%s: error parsing '%s' as a formatting string - "
"got a dangling percent character", __func__, fmt);
}

*b = '\0';
return buf;
}

char * minimize_formatting_string_staticbuf(const char *fmt)
{
/* Return the bare-bone formatting string contained in "fmt"
* as "%" characters followed immediately by ASCII letters,
* or null if "fmt" is null (empty string if it has no "%").
* Allows to compare different formatting strings to check
* if they describe the same expected amounts of arguments
* and their types.
* Returns a pointer to a static buffer; callers should
* xstrdup() and free() the returned value where applicable
* (e.g. if there is a need to compare several strings), or
* just use minimize_formatting_string() with their own
* buffers right away.
*/

static char buf[LARGEBUF];

return minimize_formatting_string(buf, sizeof(buf), fmt);
}

static void vupslog(int priority, const char *fmt, va_list va, int use_strerror)
{
int ret, errno_orig = errno;
Expand Down
7 changes: 6 additions & 1 deletion include/common.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/* common.h - prototypes for the common useful functions
Copyright (C) 2000 Russell Kroll <[email protected]>
Copyright (C)
2000 Russell Kroll <[email protected]>
2020-2024 Jim Klimov <[email protected]>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -365,6 +367,9 @@ void *xcalloc(size_t number, size_t size);
void *xrealloc(void *ptr, size_t size);
char *xstrdup(const char *string);

char *minimize_formatting_string(char *buf, size_t buflen, const char *fmt);
char *minimize_formatting_string_staticbuf(const char *fmt);

/**** REGEX helper methods ****/

/* helper function: version of strcmp that tolerates NULL
Expand Down

0 comments on commit af1f256

Please sign in to comment.