Commit 23298bfc authored by Thomas Haller's avatar Thomas Haller

cli: move utils function from common.h to nm-meta-setting-desc.c

These functions are only used by nm-meta-setting-desc.c. Make them internal.
Unfortunately, they are part of "common.h" which cannot be used without
the rest of nmcli. Still todo.
parent b5c8622a
......@@ -3117,10 +3117,15 @@ if BUILD_NMCLI
bin_PROGRAMS += clients/cli/nmcli
clients_cli_nmcli_SOURCES = \
\
shared/nm-utils/nm-shared-utils.c \
shared/nm-utils/nm-shared-utils.h \
\
shared/nm-meta-setting.c \
shared/nm-meta-setting.h \
\
clients/common/nm-client-utils.c \
clients/common/nm-client-utils.h \
clients/common/nm-meta-setting-desc.c \
clients/common/nm-meta-setting-desc.h \
\
......
This diff is collapsed.
......@@ -30,22 +30,10 @@ gboolean print_ip6_config (NMIPConfig *cfg6, NmCli *nmc, const char *group_prefi
gboolean print_dhcp4_config (NMDhcpConfig *dhcp4, NmCli *nmc, const char *group_prefix, const char *one_field);
gboolean print_dhcp6_config (NMDhcpConfig *dhcp6, NmCli *nmc, const char *group_prefix, const char *one_field);
NMIPAddress *nmc_parse_and_build_address (int family, const char *ip_str, GError **error);
NMIPRoute *nmc_parse_and_build_route (int family, const char *str, GError **error);
const char * nmc_device_state_to_string (NMDeviceState state);
const char * nmc_device_reason_to_string (NMDeviceStateReason reason);
const char * nmc_device_metered_to_string (NMMetered value);
char **
nmc_vlan_parse_priority_maps (const char *priority_map,
NMVlanPriorityMap map_type,
GError **error);
const char *nmc_bond_validate_mode (const char *mode, GError **error);
gboolean nmc_team_check_config (const char *config, char **out_config, GError **error);
gboolean nmc_proxy_check_script (const char *script, char **out_script, GError **error);
NMConnection *nmc_find_connection (const GPtrArray *connections,
const char *filter_type,
const char *filter_val,
......
......@@ -29,6 +29,8 @@
#include <readline/readline.h>
#include <readline/history.h>
#include "nm-client-utils.h"
#include "utils.h"
#include "common.h"
#include "settings.h"
......
......@@ -26,6 +26,8 @@
#include <readline/readline.h>
#include "nm-secret-agent-simple.h"
#include "nm-client-utils.h"
#include "polkit-agent.h"
#include "utils.h"
#include "common.h"
......
......@@ -22,13 +22,15 @@
#include <string.h>
#include <stdlib.h>
#include "nm-common-macros.h"
#include "nm-client-utils.h"
#include "polkit-agent.h"
#include "utils.h"
#include "common.h"
#include "general.h"
#include "common.h"
#include "nm-common-macros.h"
#include "devices.h"
#include "connections.h"
......
......@@ -32,6 +32,8 @@
#include <readline/readline.h>
#include <readline/history.h>
#include "nm-client-utils.h"
#include "polkit-agent.h"
#include "nmcli.h"
#include "utils.h"
......
......@@ -25,9 +25,12 @@
#include <arpa/inet.h>
#include "nm-common-macros.h"
#include "nm-client-utils.h"
#include "nm-vpn-helpers.h"
#include "utils.h"
#include "common.h"
#include "nm-vpn-helpers.h"
/*****************************************************************************/
......
......@@ -27,19 +27,12 @@
#include <netinet/in.h>
#include <arpa/inet.h>
#include "nm-client-utils.h"
#include "utils.h"
#include "common.h"
#include "settings.h"
gboolean
matches (const char *cmd, const char *pattern)
{
size_t len = strlen (cmd);
if (!len || len > strlen (pattern))
return FALSE;
return memcmp (pattern, cmd, len) == 0;
}
static gboolean
parse_global_arg (NmCli *nmc, const char *arg)
{
......@@ -426,137 +419,6 @@ nmc_filter_out_colors (const char *str)
return filtered;
}
/*
* Convert string to signed integer.
* If required, the resulting number is checked to be in the <min,max> range.
*/
gboolean
nmc_string_to_int_base (const char *str,
int base,
gboolean range_check,
long int min,
long int max,
long int *value)
{
char *end;
long int tmp;
errno = 0;
tmp = strtol (str, &end, base);
if (errno || *end != '\0' || (range_check && (tmp < min || tmp > max))) {
return FALSE;
}
*value = tmp;
return TRUE;
}
/*
* Convert string to unsigned integer.
* If required, the resulting number is checked to be in the <min,max> range.
*/
gboolean
nmc_string_to_uint_base (const char *str,
int base,
gboolean range_check,
unsigned long int min,
unsigned long int max,
unsigned long int *value)
{
char *end;
unsigned long int tmp;
errno = 0;
tmp = strtoul (str, &end, base);
if (errno || *end != '\0' || (range_check && (tmp < min || tmp > max))) {
return FALSE;
}
*value = tmp;
return TRUE;
}
gboolean
nmc_string_to_int (const char *str,
gboolean range_check,
long int min,
long int max,
long int *value)
{
return nmc_string_to_int_base (str, 10, range_check, min, max, value);
}
gboolean
nmc_string_to_uint (const char *str,
gboolean range_check,
unsigned long int min,
unsigned long int max,
unsigned long int *value)
{
return nmc_string_to_uint_base (str, 10, range_check, min, max, value);
}
gboolean
nmc_string_to_bool (const char *str, gboolean *val_bool, GError **error)
{
const char *s_true[] = { "true", "yes", "on", NULL };
const char *s_false[] = { "false", "no", "off", NULL };
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
if (g_strcmp0 (str, "o") == 0) {
g_set_error (error, 1, 0,
/* Translators: the first %s is the partial value entered by
* the user, the second %s a list of compatible values.
*/
_("'%s' is ambiguous (%s)"), str, "on x off");
return FALSE;
}
if (nmc_string_is_valid (str, s_true, NULL))
*val_bool = TRUE;
else if (nmc_string_is_valid (str, s_false, NULL))
*val_bool = FALSE;
else {
g_set_error (error, 1, 0,
_("'%s' is not valid; use [%s] or [%s]"),
str, "true, yes, on", "false, no, off");
return FALSE;
}
return TRUE;
}
gboolean
nmc_string_to_tristate (const char *str, NMCTriStateValue *val, GError **error)
{
const char *s_true[] = { "true", "yes", "on", NULL };
const char *s_false[] = { "false", "no", "off", NULL };
const char *s_unknown[] = { "unknown", NULL };
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
if (g_strcmp0 (str, "o") == 0) {
g_set_error (error, 1, 0,
/* Translators: the first %s is the partial value entered by
* the user, the second %s a list of compatible values.
*/
_("'%s' is ambiguous (%s)"), str, "on x off");
return FALSE;
}
if (nmc_string_is_valid (str, s_true, NULL))
*val = NMC_TRI_STATE_YES;
else if (nmc_string_is_valid (str, s_false, NULL))
*val = NMC_TRI_STATE_NO;
else if (nmc_string_is_valid (str, s_unknown, NULL))
*val = NMC_TRI_STATE_UNKNOWN;
else {
g_set_error (error, 1, 0,
_("'%s' is not valid; use [%s], [%s] or [%s]"),
str, "true, yes, on", "false, no, off", "unknown");
return FALSE;
}
return TRUE;
}
/*
* Ask user for input and return the string.
* The caller is responsible for freeing the returned string.
......@@ -619,57 +481,6 @@ nmc_string_to_arg_array (const char *line, const char *delim, gboolean unquote,
return 0;
}
/*
* Check whether 'input' is contained in 'allowed' array. It performs case
* insensitive comparison and supports shortcut strings if they are unique.
* Returns: a pointer to found string in allowed array on success or NULL.
* On failure: error->code : 0 - string not found; 1 - string is ambiguous
*/
const char *
nmc_string_is_valid (const char *input, const char **allowed, GError **error)
{
const char **p;
size_t input_ln, p_len;
gboolean prev_match = FALSE;
const char *ret = NULL;
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
if (!input || !*input)
goto finish;
input_ln = strlen (input);
for (p = allowed; p && *p; p++) {
p_len = strlen (*p);
if (g_ascii_strncasecmp (input, *p, input_ln) == 0) {
if (input_ln == p_len) {
ret = *p;
break;
}
if (!prev_match)
ret = *p;
else {
g_set_error (error, 1, 1, _("'%s' is ambiguous (%s x %s)"),
input, ret, *p);
return NULL;
}
prev_match = TRUE;
}
}
finish:
if (ret == NULL) {
char *valid_vals = g_strjoinv (", ", (char **) allowed);
if (!input || !*input)
g_set_error (error, 1, 0, _("missing name, try one of [%s]"), valid_vals);
else
g_set_error (error, 1, 0, _("'%s' not among [%s]"), input, valid_vals);
g_free (valid_vals);
}
return ret;
}
/*
* Convert string array (char **) to description string in the form of:
* "[string1, string2, ]"
......@@ -697,31 +508,6 @@ nmc_util_strv_for_display (const char *const*strv, gboolean brackets)
return g_string_free (result, FALSE);
}
/*
* Wrapper function for g_strsplit_set() that removes empty strings
* from the vector as they are not useful in most cases.
*/
char **
nmc_strsplit_set (const char *str, const char *delimiter, int max_tokens)
{
char **result;
uint i;
uint j;
result = g_strsplit_set (str, delimiter, max_tokens);
/* remove empty strings */
for (i = 0; result && result[i]; i++) {
if (*(result[i]) == '\0') {
g_free (result[i]);
for (j = i; result[j]; j++)
result[j] = result[j + 1];
i--;
}
}
return result;
}
/*
* Find out how many columns an UTF-8 string occupies on the screen.
*/
......
......@@ -32,43 +32,12 @@ typedef struct {
gboolean found;
} nmc_arg_t;
typedef enum {
NMC_TRI_STATE_NO,
NMC_TRI_STATE_YES,
NMC_TRI_STATE_UNKNOWN,
} NMCTriStateValue;
/* === Functions === */
gboolean matches (const char *cmd, const char *pattern);
int next_arg (NmCli *nmc, int *argc, char ***argv);
gboolean nmc_arg_is_help (const char *arg);
gboolean nmc_arg_is_option (const char *arg, const char *opt_name);
gboolean nmc_parse_args (nmc_arg_t *arg_arr, gboolean last, int *argc, char ***argv, GError **error);
char *ssid_to_hex (const char *str, gsize len);
gboolean nmc_string_to_int_base (const char *str,
int base,
gboolean range_check,
long int min,
long int max,
long int *value);
gboolean nmc_string_to_uint_base (const char *str,
int base,
gboolean range_check,
unsigned long int min,
unsigned long int max,
unsigned long int *value);
gboolean nmc_string_to_int (const char *str,
gboolean range_check,
long int min,
long int max,
long int *value);
gboolean nmc_string_to_uint (const char *str,
gboolean range_check,
unsigned long int min,
unsigned long int max,
unsigned long int *value);
gboolean nmc_string_to_bool (const char *str, gboolean *val_bool, GError **error);
gboolean nmc_string_to_tristate (const char *str, NMCTriStateValue *val, GError **error);
void nmc_terminal_erase_line (void);
void nmc_terminal_show_progress (const char *str);
const char *nmc_term_color_sequence (NmcTermColor color);
......
/* nmcli - command-line tool to control NetworkManager
*
* 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
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Copyright 2010 - 2017 Red Hat, Inc.
*/
#include "nm-default.h"
#include "nm-client-utils.h"
/*
* Convert string to signed integer.
* If required, the resulting number is checked to be in the <min,max> range.
*/
gboolean
nmc_string_to_int_base (const char *str,
int base,
gboolean range_check,
long int min,
long int max,
long int *value)
{
char *end;
long int tmp;
errno = 0;
tmp = strtol (str, &end, base);
if (errno || *end != '\0' || (range_check && (tmp < min || tmp > max))) {
return FALSE;
}
*value = tmp;
return TRUE;
}
/*
* Convert string to unsigned integer.
* If required, the resulting number is checked to be in the <min,max> range.
*/
gboolean
nmc_string_to_uint_base (const char *str,
int base,
gboolean range_check,
unsigned long int min,
unsigned long int max,
unsigned long int *value)
{
char *end;
unsigned long int tmp;
errno = 0;
tmp = strtoul (str, &end, base);
if (errno || *end != '\0' || (range_check && (tmp < min || tmp > max))) {
return FALSE;
}
*value = tmp;
return TRUE;
}
gboolean
nmc_string_to_int (const char *str,
gboolean range_check,
long int min,
long int max,
long int *value)
{
return nmc_string_to_int_base (str, 10, range_check, min, max, value);
}
gboolean
nmc_string_to_uint (const char *str,
gboolean range_check,
unsigned long int min,
unsigned long int max,
unsigned long int *value)
{
return nmc_string_to_uint_base (str, 10, range_check, min, max, value);
}
gboolean
nmc_string_to_bool (const char *str, gboolean *val_bool, GError **error)
{
const char *s_true[] = { "true", "yes", "on", NULL };
const char *s_false[] = { "false", "no", "off", NULL };
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
if (g_strcmp0 (str, "o") == 0) {
g_set_error (error, 1, 0,
/* Translators: the first %s is the partial value entered by
* the user, the second %s a list of compatible values.
*/
_("'%s' is ambiguous (%s)"), str, "on x off");
return FALSE;
}
if (nmc_string_is_valid (str, s_true, NULL))
*val_bool = TRUE;
else if (nmc_string_is_valid (str, s_false, NULL))
*val_bool = FALSE;
else {
g_set_error (error, 1, 0,
_("'%s' is not valid; use [%s] or [%s]"),
str, "true, yes, on", "false, no, off");
return FALSE;
}
return TRUE;
}
gboolean
nmc_string_to_tristate (const char *str, NMCTriStateValue *val, GError **error)
{
const char *s_true[] = { "true", "yes", "on", NULL };
const char *s_false[] = { "false", "no", "off", NULL };
const char *s_unknown[] = { "unknown", NULL };
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
if (g_strcmp0 (str, "o") == 0) {
g_set_error (error, 1, 0,
/* Translators: the first %s is the partial value entered by
* the user, the second %s a list of compatible values.
*/
_("'%s' is ambiguous (%s)"), str, "on x off");
return FALSE;
}
if (nmc_string_is_valid (str, s_true, NULL))
*val = NMC_TRI_STATE_YES;
else if (nmc_string_is_valid (str, s_false, NULL))
*val = NMC_TRI_STATE_NO;
else if (nmc_string_is_valid (str, s_unknown, NULL))
*val = NMC_TRI_STATE_UNKNOWN;
else {
g_set_error (error, 1, 0,
_("'%s' is not valid; use [%s], [%s] or [%s]"),
str, "true, yes, on", "false, no, off", "unknown");
return FALSE;
}
return TRUE;
}
/*
* Check whether 'input' is contained in 'allowed' array. It performs case
* insensitive comparison and supports shortcut strings if they are unique.
* Returns: a pointer to found string in allowed array on success or NULL.
* On failure: error->code : 0 - string not found; 1 - string is ambiguous
*/
const char *
nmc_string_is_valid (const char *input, const char **allowed, GError **error)
{
const char **p;
size_t input_ln, p_len;
gboolean prev_match = FALSE;
const char *ret = NULL;
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
if (!input || !*input)
goto finish;
input_ln = strlen (input);
for (p = allowed; p && *p; p++) {
p_len = strlen (*p);
if (g_ascii_strncasecmp (input, *p, input_ln) == 0) {
if (input_ln == p_len) {
ret = *p;
break;
}
if (!prev_match)
ret = *p;
else {
g_set_error (error, 1, 1, _("'%s' is ambiguous (%s x %s)"),
input, ret, *p);
return NULL;
}
prev_match = TRUE;
}
}
finish:
if (ret == NULL) {
char *valid_vals = g_strjoinv (", ", (char **) allowed);
if (!input || !*input)
g_set_error (error, 1, 0, _("missing name, try one of [%s]"), valid_vals);
else
g_set_error (error, 1, 0, _("'%s' not among [%s]"), input, valid_vals);
g_free (valid_vals);
}
return ret;
}
/*
* Wrapper function for g_strsplit_set() that removes empty strings
* from the vector as they are not useful in most cases.
*/
char **
nmc_strsplit_set (const char *str, const char *delimiter, int max_tokens)
{
char **result;
uint i;
uint j;
result = g_strsplit_set (str, delimiter, max_tokens);
/* remove empty strings */
for (i = 0; result && result[i]; i++) {
if (*(result[i]) == '\0') {
g_free (result[i]);
for (j = i; result[j]; j++)
result[j] = result[j + 1];
i--;
}
}
return result;
}
gboolean
matches (const char *cmd, const char *pattern)
{
size_t len = strlen (cmd);
if (!len || len > strlen (pattern))
return FALSE;
return memcmp (pattern, cmd, len) == 0;
}
/* nmcli - command-line tool to control NetworkManager
*
* 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
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Copyright 2010 - 2017 Red Hat, Inc.
*/
#ifndef __NM_CLIENT_UTILS_H__
#define __NM_CLIENT_UTILS_H__
#include "nm-meta-setting.h"
typedef enum {
NMC_TRI_STATE_NO,
NMC_TRI_STATE_YES,
NMC_TRI_STATE_UNKNOWN,
} NMCTriStateValue;
const char *nmc_string_is_valid (const char *input, const char **allowed, GError **error);
char **nmc_strsplit_set (const char *str, const char *delimiter, int max_tokens);
gboolean nmc_string_to_int_base (const char *str,
int base,
gboolean range_check,
long int min,
long int max,
long int *value);
gboolean nmc_string_to_uint_base (const char *str,
int base,
gboolean range_check,
unsigned long int min,
unsigned long int max,
unsigned long int *value);
gboolean nmc_string_to_int (const char *str,
gboolean range_check,
long int min,
long int max,
long int *value);
gboolean nmc_string_to_uint (const char *str,
gboolean range_check,
unsigned long int min,
unsigned long int max,
unsigned long int *value);
gboolean nmc_string_to_bool (const char *str, gboolean *val_bool, GError **error);
gboolean nmc_string_to_tristate (const char *str, NMCTriStateValue *val, GError **error);
gboolean matches (const char *cmd, const char *pattern);
#endif /* __NM_CLIENT_UTILS_H__ */
This diff is collapsed.
......@@ -115,4 +115,7 @@ struct _NMMetaSettingInfoEditor {
extern const NMMetaSettingInfoEditor nm_meta_setting_infos_editor[_NM_META_SETTING_TYPE_NUM];
/* FIXME: don't expose this function on it's own, at least not from this file. */
const char *nmc_bond_validate_mode (const char *mode, GError **error);
#endif /* __NM_META_SETTING_DESC_H__ */
......@@ -438,7 +438,7 @@ set_property (GObject *object, guint prop_id,
nm_clear_g_free (&priv->keys);
return;
}
g_hash_table_iter_init (&iter, priv->data);
g_hash_table_iter_init (&iter, data);
while (g_hash_table_iter_next (&iter, (gpointer *) &key, (gpointer *) &val)) {
if (!nm_setting_user_check_key (key, NULL))
g_return_if_reached ();
......
......@@ -10,6 +10,7 @@ clients/cli/nmcli.c
clients/cli/polkit-agent.c
clients/cli/settings.c
clients/cli/utils.c
clients/common/nm-client-utils.c
clients/common/nm-meta-setting-desc.c
clients/common/nm-polkit-listener.c
clients/common/nm-secret-agent-simple.c
......
......@@ -62,10 +62,12 @@ gint _nm_utils_ascii_str_to_bool (const char *str,
* error reason. Depending on the usag