diff --git a/documentation/RELEASE_NOTES.md b/documentation/RELEASE_NOTES.md index 4b3ecc997..b53572b07 100644 --- a/documentation/RELEASE_NOTES.md +++ b/documentation/RELEASE_NOTES.md @@ -17,6 +17,16 @@ should also be read to understand what has changed since earlier releases. +### epicsEnvShow accepts glob pattern + +The optional argument to epicsEnvShow can now be a glob pattern. + +### New function `epicsStrnGlobMatch()` + +The function `epicsStrnGlobMatch(char* str, size_t len, char* pattern)` +works exactly the same as `epicsStrGlobMatch()` but takes an additional +length arguments which limits the number of characters of `str` to match. + ### Glob pattern allowed in `var` command When used with one argument, the `var` command can be used with a glob pattern diff --git a/modules/libcom/src/misc/epicsString.c b/modules/libcom/src/misc/epicsString.c index e18d9214a..2933fb48e 100644 --- a/modules/libcom/src/misc/epicsString.c +++ b/modules/libcom/src/misc/epicsString.c @@ -259,30 +259,31 @@ size_t epicsStrnLen(const char *s, size_t maxlen) return i; } -int epicsStrGlobMatch(const char *str, const char *pattern) +int epicsStrnGlobMatch(const char *str, size_t len, const char *pattern) { - const char *cp = NULL, *mp = NULL; + const char *mp = NULL; + size_t cp = 0, i = 0; - while ((*str) && (*pattern != '*')) { - if ((*pattern != *str) && (*pattern != '?')) + while ((i < len) && (str[i]) && (*pattern != '*')) { + if ((*pattern != str[i]) && (*pattern != '?')) return 0; pattern++; - str++; + i++; } - while (*str) { + while ((i < len) && str[i]) { if (*pattern == '*') { if (!*++pattern) return 1; mp = pattern; - cp = str+1; + cp = i+1; } - else if ((*pattern == *str) || (*pattern == '?')) { + else if ((*pattern == str[i]) || (*pattern == '?')) { pattern++; - str++; + i++; } else { pattern = mp; - str = cp++; + i = cp++; } } while (*pattern == '*') @@ -290,6 +291,10 @@ int epicsStrGlobMatch(const char *str, const char *pattern) return !*pattern; } +int epicsStrGlobMatch(const char *str, const char *pattern) { + return epicsStrnGlobMatch(str, (size_t)-1, pattern); +} + char * epicsStrtok_r(char *s, const char *delim, char **lasts) { const char *spanp; diff --git a/modules/libcom/src/misc/epicsString.h b/modules/libcom/src/misc/epicsString.h index 854b552c6..76cd722cd 100644 --- a/modules/libcom/src/misc/epicsString.h +++ b/modules/libcom/src/misc/epicsString.h @@ -36,6 +36,7 @@ LIBCOM_API char * epicsStrnDup(const char *s, size_t len); LIBCOM_API int epicsStrPrintEscaped(FILE *fp, const char *s, size_t n); #define epicsStrSnPrintEscaped epicsStrnEscapedFromRaw LIBCOM_API size_t epicsStrnLen(const char *s, size_t maxlen); +LIBCOM_API int epicsStrnGlobMatch(const char *str, size_t len, const char *pattern); LIBCOM_API int epicsStrGlobMatch(const char *str, const char *pattern); LIBCOM_API char * epicsStrtok_r(char *s, const char *delim, char **lasts); LIBCOM_API unsigned int epicsStrHash(const char *str, unsigned int seed); diff --git a/modules/libcom/src/osi/os/WIN32/osdEnv.c b/modules/libcom/src/osi/os/WIN32/osdEnv.c index adc40b58c..18e66cb6b 100644 --- a/modules/libcom/src/osi/os/WIN32/osdEnv.c +++ b/modules/libcom/src/osi/os/WIN32/osdEnv.c @@ -15,11 +15,11 @@ #include #include -#include #include #include #include "epicsStdio.h" +#include "epicsString.h" #include "errlog.h" #include "envDefs.h" #include "osiUnistd.h" @@ -61,18 +61,10 @@ LIBCOM_API void epicsStdCall epicsEnvUnset (const char *name) */ LIBCOM_API void epicsStdCall epicsEnvShow (const char *name) { - if (name == NULL) { - extern char **environ; - char **sp; + char **sp; - for (sp = environ ; (sp != NULL) && (*sp != NULL) ; sp++) + for (sp = environ ; (sp != NULL) && (*sp != NULL) ; sp++) { + if (!name || epicsStrnGlobMatch(*sp, strchr(*sp, '=') - *sp, name)) printf ("%s\n", *sp); } - else { - const char *cp = getenv (name); - if (cp == NULL) - printf ("%s is not an environment variable.\n", name); - else - printf ("%s=%s\n", name, cp); - } } diff --git a/modules/libcom/src/osi/os/default/osdEnv.c b/modules/libcom/src/osi/os/default/osdEnv.c index 7ece253c7..74b5b8041 100644 --- a/modules/libcom/src/osi/os/default/osdEnv.c +++ b/modules/libcom/src/osi/os/default/osdEnv.c @@ -15,11 +15,11 @@ #include #include -#include #include #include #include "epicsStdio.h" +#include "epicsString.h" #include "epicsVersion.h" #include "errlog.h" #include "envDefs.h" @@ -68,18 +68,11 @@ LIBCOM_API void epicsStdCall epicsEnvUnset (const char *name) */ LIBCOM_API void epicsStdCall epicsEnvShow (const char *name) { - if (name == NULL) { - extern char **environ; - char **sp; + extern char **environ; + char **sp; - for (sp = environ ; (sp != NULL) && (*sp != NULL) ; sp++) + for (sp = environ ; (sp != NULL) && (*sp != NULL) ; sp++) { + if (!name || epicsStrnGlobMatch(*sp, strchr(*sp, '=') - *sp, name)) printf ("%s\n", *sp); } - else { - const char *cp = getenv (name); - if (cp == NULL) - printf ("%s is not an environment variable.\n", name); - else - printf ("%s=%s\n", name, cp); - } } diff --git a/modules/libcom/src/osi/os/vxWorks/osdEnv.c b/modules/libcom/src/osi/os/vxWorks/osdEnv.c index d91208f4c..f178e15cf 100644 --- a/modules/libcom/src/osi/os/vxWorks/osdEnv.c +++ b/modules/libcom/src/osi/os/vxWorks/osdEnv.c @@ -16,17 +16,18 @@ /* This is needed for vxWorks 6.8 to prevent an obnoxious compiler warning */ #define _VSB_CONFIG_FILE <../lib/h/config/vsbConfig.h> -#include #include -#include +#include #include #include #include "epicsFindSymbol.h" #include "epicsStdio.h" +#include "epicsString.h" #include "errlog.h" #include "iocsh.h" + /* * Set the value of an environment variable * Leaks memory, but the assumption is that this routine won't be @@ -86,14 +87,12 @@ LIBCOM_API void epicsStdCall epicsEnvUnset (const char *name) */ LIBCOM_API void epicsStdCall epicsEnvShow (const char *name) { - if (name == NULL) { - envShow (0); - } - else { - const char *cp = getenv (name); - if (cp == NULL) - printf ("%s is not an environment variable.\n", name); - else - printf ("%s=%s\n", name, cp); + extern char **ppGlobalEnviron; + char **sp; + + for (sp = environ ; (sp != NULL) && (*sp != NULL) ; sp++) { + if (!**sp) continue; /* skip unset environment variables */ + if (!name || epicsStrnGlobMatch(*sp, strchr(*sp, '=') - *sp, name)) + printf ("%s\n", *sp); } }