forked from networkupstools/nut
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
common/common.c, include/common.h: introduce minimize_formatting_stri…
…ng() [networkupstools#2450] ...and minimize_formatting_string_staticbuf() for one-shot use-cases. Signed-off-by: Jim Klimov <[email protected]>
- Loading branch information
Showing
2 changed files
with
83 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
@@ -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; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
@@ -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 | ||
|