diff options
| author | David M. Syzdek <david@syzdek.net> | 2022-03-16 23:54:32 -0800 |
|---|---|---|
| committer | David M. Syzdek <david@syzdek.net> | 2022-03-16 23:55:18 -0800 |
| commit | 7b39a8de72a20215e9285d824059fa843ae19aed (patch) | |
| tree | e92a8fbd8a6c2277a4fd3bdc170c36660b393fbf | |
| parent | b5fd4adf51b0d1093d73a848bc1249bb9123f5b7 (diff) | |
| download | tinyrad-7b39a8de72a20215e9285d824059fa843ae19aed.tar.gz tinyrad-7b39a8de72a20215e9285d824059fa843ae19aed.tar.bz2 tinyrad-7b39a8de72a20215e9285d824059fa843ae19aed.tar.xz tinyrad-7b39a8de72a20215e9285d824059fa843ae19aed.zip | |
libtinyrad: adding ability to force expansion in tinyrad_strexpand()
| -rw-r--r-- | include/tinyrad_internal.h | 3 | ||||
| -rw-r--r-- | lib/libtinyrad/lstrings.c | 5 | ||||
| -rw-r--r-- | tests/test-str-expand.c | 43 |
3 files changed, 32 insertions, 19 deletions
diff --git a/include/tinyrad_internal.h b/include/tinyrad_internal.h index 445a377..42e647d 100644 --- a/include/tinyrad_internal.h +++ b/include/tinyrad_internal.h | |||
| @@ -219,7 +219,8 @@ _TINYRAD_F int | |||
| 219 | tinyrad_strexpand( | 219 | tinyrad_strexpand( |
| 220 | char * dst, | 220 | char * dst, |
| 221 | const char * restrict src, | 221 | const char * restrict src, |
| 222 | size_t len ); | 222 | size_t len, |
| 223 | int force_expansion ); | ||
| 223 | 224 | ||
| 224 | 225 | ||
| 225 | _TINYRAD_F size_t | 226 | _TINYRAD_F size_t |
diff --git a/lib/libtinyrad/lstrings.c b/lib/libtinyrad/lstrings.c index 63af190..ffce9d1 100644 --- a/lib/libtinyrad/lstrings.c +++ b/lib/libtinyrad/lstrings.c | |||
| @@ -110,7 +110,8 @@ int | |||
| 110 | tinyrad_strexpand( | 110 | tinyrad_strexpand( |
| 111 | char * dst, | 111 | char * dst, |
| 112 | const char * restrict src, | 112 | const char * restrict src, |
| 113 | size_t len ) | 113 | size_t len, |
| 114 | int force_expansion ) | ||
| 114 | { | 115 | { |
| 115 | size_t pos; | 116 | size_t pos; |
| 116 | size_t offset; | 117 | size_t offset; |
| @@ -134,7 +135,7 @@ tinyrad_strexpand( | |||
| 134 | quote = ((src[0] == '\'')||(src[0] == '"')) ? src[0] : 0; | 135 | quote = ((src[0] == '\'')||(src[0] == '"')) ? src[0] : 0; |
| 135 | for(pos = (((quote)) ? 1 : 0); ( ((src[pos])) && (offset < (len-1)) ); pos++) | 136 | for(pos = (((quote)) ? 1 : 0); ( ((src[pos])) && (offset < (len-1)) ); pos++) |
| 136 | { | 137 | { |
| 137 | if (quote != '"') | 138 | if ( (quote != '"') && (force_expansion != TRAD_YES) ) |
| 138 | { | 139 | { |
| 139 | dst[offset++] = src[pos]; | 140 | dst[offset++] = src[pos]; |
| 140 | continue; | 141 | continue; |
diff --git a/tests/test-str-expand.c b/tests/test-str-expand.c index f88e882..0a5eb74 100644 --- a/tests/test-str-expand.c +++ b/tests/test-str-expand.c | |||
| @@ -62,6 +62,8 @@ | |||
| 62 | #undef PROGRAM_NAME | 62 | #undef PROGRAM_NAME |
| 63 | #define PROGRAM_NAME "tinyrad-str-expand" | 63 | #define PROGRAM_NAME "tinyrad-str-expand" |
| 64 | 64 | ||
| 65 | #define TEST_OPT_FORCE 0x0040UL | ||
| 66 | |||
| 65 | 67 | ||
| 66 | ///////////////// | 68 | ///////////////// |
| 67 | // // | 69 | // // |
| @@ -74,6 +76,7 @@ typedef struct my_test_strings | |||
| 74 | { | 76 | { |
| 75 | const char * origin; | 77 | const char * origin; |
| 76 | const char * expand; | 78 | const char * expand; |
| 79 | intptr_t force; | ||
| 77 | } MyTestStr; | 80 | } MyTestStr; |
| 78 | 81 | ||
| 79 | 82 | ||
| @@ -86,20 +89,20 @@ typedef struct my_test_strings | |||
| 86 | 89 | ||
| 87 | static const MyTestStr test_strings[] = | 90 | static const MyTestStr test_strings[] = |
| 88 | { | 91 | { |
| 89 | { "asdfghj", "asdfghj" }, | 92 | { "asdfghj", "asdfghj", TRAD_NO }, |
| 90 | { "'asdfghj'", "asdfghj" }, | 93 | { "'asdfghj'", "asdfghj", TRAD_NO }, |
| 91 | { "\"asdfghj\"", "asdfghj" }, | 94 | { "\"asdfghj\"", "asdfghj", TRAD_NO }, |
| 92 | { "\"\\141\\163\\144\\146\\147\\150\\152\"", "asdfghj" }, | 95 | { "\"\\141\\163\\144\\146\\147\\150\\152\"", "asdfghj", TRAD_NO }, |
| 93 | { "\"\\x61\\x73\\x64\\x66\\x67\\x68\\x6a\"", "asdfghj" }, | 96 | { "\"\\x61\\x73\\x64\\x66\\x67\\x68\\x6a\"", "asdfghj", TRAD_NO }, |
| 94 | { "\"a\\163\\x64f\\147\\x68j\"", "asdfghj" }, | 97 | { "\"a\\163\\x64f\\147\\x68j\"", "asdfghj", TRAD_NO }, |
| 95 | { "\"a%%sdfghj\"", "a%sdfghj" }, | 98 | { "\"a%%sdfghj\"", "a%sdfghj", TRAD_NO }, |
| 96 | { "\"%u:x:%U:%G:test user:%d:/bin/false\"", NULL }, | 99 | { "\"%u:x:%U:%G:test user:%d:/bin/false\"", NULL, TRAD_NO }, |
| 97 | { "\"%g:x:%G:%u,root\"", NULL }, | 100 | { "\"%g:x:%G:%u,root\"", NULL, TRAD_NO }, |
| 98 | { "\"Is %p running successfully?\"", NULL }, | 101 | { "\"Is %p running successfully?\"", NULL, TRAD_NO }, |
| 99 | { "\"Is your email address %u@%D\?\"", NULL }, | 102 | { "\"Is your email address %u@%D\?\"", NULL, TRAD_NO }, |
| 100 | { "\"is your email address %u@%H\?\"", NULL }, | 103 | { "\"is your email address %u@%H\?\"", NULL, TRAD_NO }, |
| 101 | { "\"Is your email address %u@%h.%D\?\"", NULL }, | 104 | { "\"Is your email address %u@%h.%D\?\"", NULL, TRAD_NO }, |
| 102 | { NULL, NULL } | 105 | { NULL, NULL, 0 } |
| 103 | }; | 106 | }; |
| 104 | 107 | ||
| 105 | 108 | ||
| @@ -135,6 +138,7 @@ main( | |||
| 135 | size_t pos; | 138 | size_t pos; |
| 136 | unsigned opts; | 139 | unsigned opts; |
| 137 | char buff[256]; | 140 | char buff[256]; |
| 141 | const MyTestStr * test_str; | ||
| 138 | 142 | ||
| 139 | // getopt options | 143 | // getopt options |
| 140 | static char short_opt[] = "dhVvq"; | 144 | static char short_opt[] = "dhVvq"; |
| @@ -146,6 +150,7 @@ main( | |||
| 146 | {"silent", no_argument, NULL, 'q' }, | 150 | {"silent", no_argument, NULL, 'q' }, |
| 147 | {"version", no_argument, NULL, 'V' }, | 151 | {"version", no_argument, NULL, 'V' }, |
| 148 | {"verbose", no_argument, NULL, 'v' }, | 152 | {"verbose", no_argument, NULL, 'v' }, |
| 153 | {"force", no_argument, NULL, 1 }, | ||
| 149 | { NULL, 0, NULL, 0 } | 154 | { NULL, 0, NULL, 0 } |
| 150 | }; | 155 | }; |
| 151 | 156 | ||
| @@ -161,6 +166,10 @@ main( | |||
| 161 | case 0: /* long options toggles */ | 166 | case 0: /* long options toggles */ |
| 162 | break; | 167 | break; |
| 163 | 168 | ||
| 169 | case 1: | ||
| 170 | opts |= TEST_OPT_FORCE; | ||
| 171 | break; | ||
| 172 | |||
| 164 | case 'd': | 173 | case 'd': |
| 165 | opt = TRAD_DEBUG_ANY; | 174 | opt = TRAD_DEBUG_ANY; |
| 166 | tinyrad_set_option(NULL, TRAD_OPT_DEBUG_LEVEL, &opt); | 175 | tinyrad_set_option(NULL, TRAD_OPT_DEBUG_LEVEL, &opt); |
| @@ -175,6 +184,7 @@ main( | |||
| 175 | printf(" -q, --quiet, --silent do not print messages\n"); | 184 | printf(" -q, --quiet, --silent do not print messages\n"); |
| 176 | printf(" -V, --version print version number and exit\n"); | 185 | printf(" -V, --version print version number and exit\n"); |
| 177 | printf(" -v, --verbose print verbose messages\n"); | 186 | printf(" -v, --verbose print verbose messages\n"); |
| 187 | printf(" --force force expansion of user strings\n"); | ||
| 178 | printf("\n"); | 188 | printf("\n"); |
| 179 | return(0); | 189 | return(0); |
| 180 | 190 | ||
| @@ -209,7 +219,7 @@ main( | |||
| 209 | while (optind < argc) | 219 | while (optind < argc) |
| 210 | { | 220 | { |
| 211 | trutils_verbose(opts, "test string: %s", argv[optind]); | 221 | trutils_verbose(opts, "test string: %s", argv[optind]); |
| 212 | if ((rc = tinyrad_strexpand(buff, argv[optind], sizeof(buff))) != TRAD_SUCCESS) | 222 | if ((rc = tinyrad_strexpand(buff, argv[optind], sizeof(buff), (((opts&TEST_OPT_FORCE)) ? TRAD_YES : TRAD_NO))) != TRAD_SUCCESS) |
| 213 | { | 223 | { |
| 214 | trutils_error(opts, NULL, "tinyrad_strexpand(): %s", tinyrad_strerror(rc)); | 224 | trutils_error(opts, NULL, "tinyrad_strexpand(): %s", tinyrad_strerror(rc)); |
| 215 | return(1); | 225 | return(1); |
| @@ -228,8 +238,9 @@ main( | |||
| 228 | // expand and verify test strings | 238 | // expand and verify test strings |
| 229 | for(pos = 0; ((test_strings[pos].origin)); pos++) | 239 | for(pos = 0; ((test_strings[pos].origin)); pos++) |
| 230 | { | 240 | { |
| 241 | test_str = &test_strings[pos]; | ||
| 231 | trutils_verbose(opts, "test string: %s", test_strings[pos].origin); | 242 | trutils_verbose(opts, "test string: %s", test_strings[pos].origin); |
| 232 | if ((rc = tinyrad_strexpand(buff, test_strings[pos].origin, sizeof(buff))) != TRAD_SUCCESS) | 243 | if ((rc = tinyrad_strexpand(buff, test_str->origin, sizeof(buff), (int)test_str->force)) != TRAD_SUCCESS) |
| 233 | { | 244 | { |
| 234 | trutils_error(opts, NULL, "tinyrad_strexpand(): %s", tinyrad_strerror(rc)); | 245 | trutils_error(opts, NULL, "tinyrad_strexpand(): %s", tinyrad_strerror(rc)); |
| 235 | return(1); | 246 | return(1); |
