ifcfg-rh,cli: merge partical backport of branch 'th/default-dns-options-bgo749648'

- some enhancements and bugfixes to ifcfg-rh utils
- fix and refactoring nmcli value-to-string conversion

(cherry picked from commit ec972ad305)
This commit is contained in:
Thomas Haller 2015-06-05 12:31:08 +02:00
commit 3f2d861614
6 changed files with 422 additions and 433 deletions

View file

@ -7221,7 +7221,7 @@ property_edit_submenu (NmCli *nmc,
case NMC_EDITOR_SUB_CMD_CHANGE:
rl_startup_hook = nmc_rl_set_deftext;
nmc_rl_pre_input_deftext = nmc_setting_get_property_out2in (curr_setting, prop_name, NULL);
nmc_rl_pre_input_deftext = nmc_setting_get_property_parsable (curr_setting, prop_name, NULL);
prop_val_user = nmc_readline (_("Edit '%s' value: "), prop_name);
nmc_property_get_gvalue (curr_setting, prop_name, &prop_g_value);

File diff suppressed because it is too large Load diff

View file

@ -41,9 +41,9 @@ const char **nmc_setting_get_property_allowed_values (NMSetting *setting, const
char *nmc_setting_get_property (NMSetting *setting,
const char *prop,
GError **error);
char *nmc_setting_get_property_out2in (NMSetting *setting,
const char *prop,
GError **error);
char *nmc_setting_get_property_parsable (NMSetting *setting,
const char *prop,
GError **error);
gboolean nmc_setting_set_property (NMSetting *setting,
const char *prop,
const char *val,

View file

@ -36,6 +36,7 @@
#include "shvar.h"
#include "gsystem-local-alloc.h"
#include "nm-core-internal.h"
#include "nm-logging.h"
@ -206,8 +207,8 @@ static const char escapees[] = "\"'\\$~`"; /* must be escaped */
static const char spaces[] = " \t|&;()<>"; /* only require "" */
static const char newlines[] = "\n\r"; /* will be removed */
char *
svEscape (const char *s)
const char *
svEscape (const char *s, char **to_free)
{
char *new;
int i, j, mangle = 0, space = 0, newline = 0;
@ -223,8 +224,10 @@ svEscape (const char *s)
if (strchr (newlines, s[i]))
newline++;
}
if (!mangle && !space && !newline)
return strdup (s);
if (!mangle && !space && !newline) {
*to_free = NULL;
return s;
}
newlen = slen + mangle - newline + 3; /* 3 is extra ""\0 */
new = g_malloc (newlen);
@ -243,6 +246,7 @@ svEscape (const char *s)
new[j++] = '\0';
g_assert (j == slen + mangle - newline + 3);
*to_free = new;
return new;
}
@ -252,6 +256,22 @@ svEscape (const char *s)
*/
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).
* svGetValueFull() will return empty values if that is the value for the @key. */
char *
svGetValueFull (shvarFile *s, const char *key, gboolean verbatim)
{
char *value = NULL;
char *line;
@ -276,12 +296,7 @@ svGetValue (shvarFile *s, const char *key, gboolean verbatim)
}
g_free (keyString);
if (value && value[0]) {
return value;
} else {
g_free (value);
return NULL;
}
return value;
}
/* return TRUE if <key> resolves to any truth value (e.g. "yes", "y", "true")
@ -330,7 +345,7 @@ svGetValueInt64 (shvarFile *s, const char *key, guint base, gint64 min, gint64 m
gint64 result;
int errsv;
tmp = svGetValue (s, key, FALSE);
tmp = svGetValueFull (s, key, FALSE);
if (!tmp) {
errno = 0;
return fallback;
@ -354,20 +369,30 @@ svGetValueInt64 (shvarFile *s, const char *key, guint base, gint64 min, gint64 m
void
svSetValue (shvarFile *s, const char *key, const char *value, gboolean verbatim)
{
char *newval = NULL, *oldval = NULL;
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. */
void
svSetValueFull (shvarFile *s, const char *key, const char *value, gboolean verbatim)
{
gs_free char *newval_free = NULL;
gs_free char *oldval = NULL;
const char *newval;
char *keyValue;
g_return_if_fail (s != NULL);
g_return_if_fail (key != NULL);
/* value may be NULL */
if (value)
newval = verbatim ? g_strdup (value) : svEscape (value);
keyValue = g_strdup_printf ("%s=%s", key, newval ? newval : "");
if (!value || verbatim)
newval = value;
else
newval = svEscape (value, &newval_free);
oldval = svGetValueFull (s, key, FALSE);
oldval = svGetValue (s, key, FALSE);
if (!newval || !newval[0]) {
if (!newval) {
/* delete value */
if (oldval) {
/* delete line */
@ -376,15 +401,15 @@ svSetValue (shvarFile *s, const char *key, const char *value, gboolean verbatim)
g_list_free_1 (s->current);
s->modified = TRUE;
}
g_free (keyValue);
goto end;
return;
}
keyValue = g_strdup_printf ("%s=%s", key, newval);
if (!oldval) {
/* append line */
s->lineList = g_list_append (s->lineList, keyValue);
s->modified = TRUE;
goto end;
return;
}
if (strcmp (oldval, newval) != 0) {
@ -397,11 +422,6 @@ svSetValue (shvarFile *s, const char *key, const char *value, gboolean verbatim)
s->modified = TRUE;
} else
g_free (keyValue);
end:
g_free (newval);
g_free (oldval);
return;
}
/* Write the current contents iff modified. Returns FALSE on error

View file

@ -56,6 +56,7 @@ shvarFile *svOpenFile (const char *name, GError **error);
* be freed by the caller.
*/
char *svGetValue (shvarFile *s, const char *key, gboolean verbatim);
char *svGetValueFull (shvarFile *s, const char *key, gboolean verbatim);
/* return TRUE if <key> resolves to any truth value (e.g. "yes", "y", "true")
* return FALSE if <key> resolves to any non-truth value (e.g. "no", "n", "false")
@ -71,6 +72,7 @@ gint64 svGetValueInt64 (shvarFile *s, const char *key, guint base, gint64 min, g
* to the top of the file.
*/
void svSetValue (shvarFile *s, const char *key, const char *value, gboolean verbatim);
void svSetValueFull (shvarFile *s, const char *key, const char *value, gboolean verbatim);
/* Write the current contents iff modified. Returns FALSE on error
@ -84,8 +86,8 @@ gboolean svWriteFile (shvarFile *s, int mode, GError **error);
/* Close the file descriptor (if open) and free the shvarFile. */
void svCloseFile (shvarFile *s);
/* Return a new escaped string */
char *svEscape (const char *s);
/* Return @s unmodified or an escaped string */
const char *svEscape (const char *s, char **to_free);
/* Unescape a string in-place */
void svUnescape (char *s);

View file

@ -888,20 +888,22 @@ write_wireless_setting (NMConnection *connection,
svSetValue (ifcfg, "ESSID", str->str, TRUE);
g_string_free (str, TRUE);
} else {
const char *tmp_escaped;
/* Printable SSIDs always get quoted */
memset (buf, 0, sizeof (buf));
memcpy (buf, ssid_data, ssid_len);
tmp = svEscape (buf);
tmp_escaped = svEscape (buf, &tmp);
/* svEscape will usually quote the string, but just for consistency,
* if svEscape doesn't quote the ESSID, we quote it ourselves.
*/
if (tmp[0] != '"' && tmp[strlen (tmp) - 1] != '"') {
tmp2 = g_strdup_printf ("\"%s\"", tmp);
if (tmp_escaped[0] != '"' && tmp_escaped[strlen (tmp_escaped) - 1] != '"') {
tmp2 = g_strdup_printf ("\"%s\"", tmp_escaped);
svSetValue (ifcfg, "ESSID", tmp2, TRUE);
g_free (tmp2);
} else
svSetValue (ifcfg, "ESSID", tmp, TRUE);
svSetValue (ifcfg, "ESSID", tmp_escaped, TRUE);
g_free (tmp);
}