diff options
author | Thomas Haller <thaller@redhat.com> | 2016-10-09 14:02:19 +0200 |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2016-10-11 11:37:46 +0200 |
commit | b00ee52ab32d7260ac175c989520fbb25c7a32b9 (patch) | |
tree | 0268f67700a164cabb3f1fa53ab95be89114a275 | |
parent | 96846352de0c5110128e82171c6648b7083936bb (diff) | |
download | NetworkManager-b00ee52ab32d7260ac175c989520fbb25c7a32b9.tar.gz |
ifcfg-rh/trivial: move code in shvar.c
-rw-r--r-- | src/settings/plugins/ifcfg-rh/shvar.c | 340 |
1 files changed, 176 insertions, 164 deletions
diff --git a/src/settings/plugins/ifcfg-rh/shvar.c b/src/settings/plugins/ifcfg-rh/shvar.c index 32601384b4..29ec5a1fd8 100644 --- a/src/settings/plugins/ifcfg-rh/shvar.c +++ b/src/settings/plugins/ifcfg-rh/shvar.c @@ -38,6 +38,155 @@ #include "nm-core-internal.h" +/*****************************************************************************/ + +/** + * svParseBoolean: + * @value: the input string + * @fallback: the fallback value + * + * Parses a string and returns the boolean value it contains or, + * in case no valid value is found, the fallback value. Valid values + * are: "yes", "true", "t", "y", "1" and "no", "false", "f", "n", "0". + * + * Returns: the parsed boolean value or @fallback. + */ +gint +svParseBoolean (const char *value, gint fallback) +{ + if (!value) + return fallback; + + if ( !g_ascii_strcasecmp ("yes", value) + || !g_ascii_strcasecmp ("true", value) + || !g_ascii_strcasecmp ("t", value) + || !g_ascii_strcasecmp ("y", value) + || !g_ascii_strcasecmp ("1", value)) + return TRUE; + else if ( !g_ascii_strcasecmp ("no", value) + || !g_ascii_strcasecmp ("false", value) + || !g_ascii_strcasecmp ("f", value) + || !g_ascii_strcasecmp ("n", value) + || !g_ascii_strcasecmp ("0", value)) + return FALSE; + + return fallback; +} + +/*****************************************************************************/ + +/* create a new string with all necessary characters escaped. + * caller must free returned string + */ +static const char escapees[] = "\"'\\$~`"; /* must be escaped */ +static const char spaces[] = " \t|&;()<>"; /* only require "" */ +static const char newlines[] = "\n\r"; /* will be removed */ + +const char * +svEscape (const char *s, char **to_free) +{ + char *new; + int i, j, mangle = 0, space = 0, newline = 0; + int newlen, slen; + + slen = strlen (s); + + for (i = 0; i < slen; i++) { + if (strchr (escapees, s[i])) + mangle++; + if (strchr (spaces, s[i])) + space++; + if (strchr (newlines, s[i])) + newline++; + } + if (!mangle && !space && !newline) { + *to_free = NULL; + return s; + } + + newlen = slen + mangle - newline + 3; /* 3 is extra ""\0 */ + new = g_malloc (newlen); + + j = 0; + new[j++] = '"'; + for (i = 0; i < slen; i++) { + if (strchr (newlines, s[i])) + continue; + if (strchr (escapees, s[i])) { + new[j++] = '\\'; + } + new[j++] = s[i]; + } + new[j++] = '"'; + new[j++] = '\0'; + g_assert (j == slen + mangle - newline + 3); + + *to_free = new; + return new; +} + +/* remove escaped characters in place */ +void +svUnescape (char *s) +{ + size_t len, idx_rd = 0, idx_wr = 0; + char c; + + len = strlen (s); + if (len < 2) { + if (s[0] == '\\') + s[0] = '\0'; + return; + } + + if ((s[0] == '"' || s[0] == '\'') && s[0] == s[len-1]) { + if (len == 2) { + s[0] = '\0'; + return; + } + if (len == 3) { + if (s[1] == '\\') { + s[0] = '\0'; + } else { + s[0] = s[1]; + s[1] = '\0'; + } + return; + } + s[--len] = '\0'; + idx_rd = 1; + } else { + /* seek for the first escape... */ + char *p = strchr (s, '\\'); + + if (!p) + return; + if (p[1] == '\0') { + p[0] = '\0'; + return; + } + idx_wr = idx_rd = (p - s); + } + + /* idx_rd points to the first escape. Walk the string and shift the + * characters from idx_rd to idx_wr. + */ + while ((c = s[idx_rd++])) { + if (c == '\\') { + if (s[idx_rd] == '\0') { + s[idx_wr] = '\0'; + return; + } + s[idx_wr++] = s[idx_rd++]; + continue; + } + s[idx_wr++] = c; + } + s[idx_wr] = '\0'; +} + +/*****************************************************************************/ + /* Open the file <name>, returning a shvarFile on success and NULL on failure. * Add a wrinkle to let the caller specify whether or not to create the file * (actually, return a structure anyway) if it doesn't exist. @@ -136,132 +285,7 @@ svCreateFile (const char *name) return svOpenFileInternal (name, TRUE, NULL); } -/* remove escaped characters in place */ -void -svUnescape (char *s) -{ - size_t len, idx_rd = 0, idx_wr = 0; - char c; - - len = strlen (s); - if (len < 2) { - if (s[0] == '\\') - s[0] = '\0'; - return; - } - - if ((s[0] == '"' || s[0] == '\'') && s[0] == s[len-1]) { - if (len == 2) { - s[0] = '\0'; - return; - } - if (len == 3) { - if (s[1] == '\\') { - s[0] = '\0'; - } else { - s[0] = s[1]; - s[1] = '\0'; - } - return; - } - s[--len] = '\0'; - idx_rd = 1; - } else { - /* seek for the first escape... */ - char *p = strchr (s, '\\'); - - if (!p) - return; - if (p[1] == '\0') { - p[0] = '\0'; - return; - } - idx_wr = idx_rd = (p - s); - } - - /* idx_rd points to the first escape. Walk the string and shift the - * characters from idx_rd to idx_wr. - */ - while ((c = s[idx_rd++])) { - if (c == '\\') { - if (s[idx_rd] == '\0') { - s[idx_wr] = '\0'; - return; - } - s[idx_wr++] = s[idx_rd++]; - continue; - } - s[idx_wr++] = c; - } - s[idx_wr] = '\0'; -} - -/* create a new string with all necessary characters escaped. - * caller must free returned string - */ -static const char escapees[] = "\"'\\$~`"; /* must be escaped */ -static const char spaces[] = " \t|&;()<>"; /* only require "" */ -static const char newlines[] = "\n\r"; /* will be removed */ - -const char * -svEscape (const char *s, char **to_free) -{ - char *new; - int i, j, mangle = 0, space = 0, newline = 0; - int newlen, slen; - - slen = strlen (s); - - for (i = 0; i < slen; i++) { - if (strchr (escapees, s[i])) - mangle++; - if (strchr (spaces, s[i])) - space++; - if (strchr (newlines, s[i])) - newline++; - } - if (!mangle && !space && !newline) { - *to_free = NULL; - return s; - } - - newlen = slen + mangle - newline + 3; /* 3 is extra ""\0 */ - new = g_malloc (newlen); - - j = 0; - new[j++] = '"'; - for (i = 0; i < slen; i++) { - if (strchr (newlines, s[i])) - continue; - if (strchr (escapees, s[i])) { - new[j++] = '\\'; - } - new[j++] = s[i]; - } - new[j++] = '"'; - new[j++] = '\0'; - g_assert (j == slen + mangle - newline + 3); - - *to_free = new; - return new; -} - -/* Get the value associated with the key, and leave the current pointer - * pointing at the line containing the value. The char* returned MUST - * be freed by the caller. - */ -char * -svGetValue (shvarFile *s, const char *key, gboolean verbatim) -{ - char *value; - - value = svGetValueFull (s, key, verbatim); - if (value && !*value) { - g_free (value); - return NULL; - } - return value; -} +/*****************************************************************************/ /* svGetValueFull() is identical to svGetValue() except that * svGetValue() will never return an empty value (but %NULL instead). @@ -292,37 +316,21 @@ svGetValueFull (shvarFile *s, const char *key, gboolean verbatim) return value; } -/** - * svParseBoolean: - * @value: the input string - * @fallback: the fallback value - * - * Parses a string and returns the boolean value it contains or, - * in case no valid value is found, the fallback value. Valid values - * are: "yes", "true", "t", "y", "1" and "no", "false", "f", "n", "0". - * - * Returns: the parsed boolean value or @fallback. +/* Get the value associated with the key, and leave the current pointer + * pointing at the line containing the value. The char* returned MUST + * be freed by the caller. */ -gint -svParseBoolean (const char *value, gint fallback) +char * +svGetValue (shvarFile *s, const char *key, gboolean verbatim) { - if (!value) - return fallback; - - if ( !g_ascii_strcasecmp ("yes", value) - || !g_ascii_strcasecmp ("true", value) - || !g_ascii_strcasecmp ("t", value) - || !g_ascii_strcasecmp ("y", value) - || !g_ascii_strcasecmp ("1", value)) - return TRUE; - else if ( !g_ascii_strcasecmp ("no", value) - || !g_ascii_strcasecmp ("false", value) - || !g_ascii_strcasecmp ("f", value) - || !g_ascii_strcasecmp ("n", value) - || !g_ascii_strcasecmp ("0", value)) - return FALSE; + char *value; - return fallback; + value = svGetValueFull (s, key, verbatim); + if (value && !*value) { + g_free (value); + return NULL; + } + return value; } /* svGetValueBoolean: @@ -375,16 +383,7 @@ svGetValueInt64 (shvarFile *s, const char *key, guint base, gint64 min, gint64 m return result; } -/* Set the variable <key> equal to the value <value>. - * If <key> does not exist, and the <current> pointer is set, append - * the key=value pair after that line. Otherwise, append the pair - * to the bottom of the file. - */ -void -svSetValue (shvarFile *s, const char *key, const char *value, gboolean verbatim) -{ - svSetValueFull (s, key, value && value[0] ? value : NULL, verbatim); -} +/*****************************************************************************/ /* Same as svSetValue() but it preserves empty @value -- contrary to * svSetValue() for which "" effectively means to remove the value. */ @@ -438,6 +437,17 @@ svSetValueFull (shvarFile *s, const char *key, const char *value, gboolean verba g_free (keyValue); } +/* Set the variable <key> equal to the value <value>. + * If <key> does not exist, and the <current> pointer is set, append + * the key=value pair after that line. Otherwise, append the pair + * to the bottom of the file. + */ +void +svSetValue (shvarFile *s, const char *key, const char *value, gboolean verbatim) +{ + svSetValueFull (s, key, value && value[0] ? value : NULL, verbatim); +} + void svSetValueInt64 (shvarFile *s, const char *key, gint64 value) { @@ -454,6 +464,8 @@ svUnsetValue (shvarFile *s, const char *key) svSetValueFull (s, key, NULL, FALSE); } +/*****************************************************************************/ + /* Write the current contents iff modified. Returns FALSE on error * and TRUE on success. Do not write if no values have been modified. * The mode argument is only used if creating the file, not if |