ifcfg-rh: refactor parsing in parse_ethtool_option() to not call helper functions
Parsing can be complicated enough. It's simpler to just work top-to-bottom, without calling various helper functions. This was, you can see all the code in one place, without need to jump to the helper function to see what it is doing. In general, a static function that is only called once, does sometimes not simplify but obfuscate the code.
This commit is contained in:
@@ -4046,63 +4046,84 @@ wireless_connection_from_ifcfg (const char *file,
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
parse_ethtool_option_autoneg (const char *value, gboolean *out_autoneg)
|
parse_ethtool_option (const char *value,
|
||||||
|
NMSettingWiredWakeOnLan *out_flags,
|
||||||
|
char **out_password,
|
||||||
|
gboolean *out_autoneg,
|
||||||
|
guint32 *out_speed,
|
||||||
|
const char **out_duplex)
|
||||||
{
|
{
|
||||||
if (!value) {
|
gs_free const char **words = NULL;
|
||||||
PARSE_WARNING ("Auto-negotiation option missing");
|
guint i;
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (g_str_equal (value, "off"))
|
words = nm_utils_strsplit_set (value, "\t \n");
|
||||||
|
if (!words)
|
||||||
|
return;
|
||||||
|
|
||||||
|
for (i = 0; words[i]; ) {
|
||||||
|
const char *opt = words[i];
|
||||||
|
const char *opt_val = words[++i];
|
||||||
|
|
||||||
|
if (nm_streq (opt, "autoneg")) {
|
||||||
|
if (!opt_val) {
|
||||||
|
PARSE_WARNING ("Auto-negotiation option missing");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
|
||||||
|
if (nm_streq (opt_val, "off"))
|
||||||
*out_autoneg = FALSE;
|
*out_autoneg = FALSE;
|
||||||
else if (g_str_equal (value, "on"))
|
else if (nm_streq (opt_val, "on"))
|
||||||
*out_autoneg = TRUE;
|
*out_autoneg = TRUE;
|
||||||
else
|
else
|
||||||
PARSE_WARNING ("Auto-negotiation unknown value: %s", value);
|
PARSE_WARNING ("Auto-negotiation unknown value: %s", opt_val);
|
||||||
}
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
static void
|
if (nm_streq (opt, "speed")) {
|
||||||
parse_ethtool_option_speed (const char *value, guint32 *out_speed)
|
guint32 speed;
|
||||||
{
|
|
||||||
if (!value) {
|
if (!opt_val) {
|
||||||
PARSE_WARNING ("Speed option missing");
|
PARSE_WARNING ("Speed option missing");
|
||||||
return;
|
break;
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
|
||||||
|
speed = _nm_utils_ascii_str_to_int64 (opt_val, 10, 0, G_MAXUINT32, 0);
|
||||||
|
if (errno == 0)
|
||||||
|
*out_speed = speed;
|
||||||
|
else
|
||||||
|
PARSE_WARNING ("Speed value '%s' is invalid", opt_val);
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
*out_speed = _nm_utils_ascii_str_to_int64 (value, 10, 0, G_MAXUINT32, 0);
|
if (nm_streq (opt, "duplex")) {
|
||||||
if (errno)
|
if (!opt_val) {
|
||||||
PARSE_WARNING ("Speed value '%s' is invalid", value);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
parse_ethtool_option_duplex (const char *value, const char **out_duplex)
|
|
||||||
{
|
|
||||||
if (!value) {
|
|
||||||
PARSE_WARNING ("Duplex option missing");
|
PARSE_WARNING ("Duplex option missing");
|
||||||
return;
|
break;
|
||||||
}
|
}
|
||||||
|
i++;
|
||||||
|
|
||||||
if (g_str_equal (value, "half"))
|
if (nm_streq (opt_val, "half"))
|
||||||
*out_duplex = "half";
|
*out_duplex = "half";
|
||||||
else if (g_str_equal (value, "full"))
|
else if (nm_streq (opt_val, "full"))
|
||||||
*out_duplex = "full";
|
*out_duplex = "full";
|
||||||
else
|
else
|
||||||
PARSE_WARNING ("Duplex unknown value: %s", value);
|
PARSE_WARNING ("Duplex unknown value: %s", opt_val);
|
||||||
|
continue;
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
parse_ethtool_option_wol (const char *value, NMSettingWiredWakeOnLan *out_flags)
|
|
||||||
{
|
|
||||||
NMSettingWiredWakeOnLan wol_flags = NM_SETTING_WIRED_WAKE_ON_LAN_NONE;
|
|
||||||
|
|
||||||
if (!value) {
|
|
||||||
PARSE_WARNING ("Wake-on-LAN options missing");
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for (; *value; value++) {
|
if (nm_streq (opt, "wol")) {
|
||||||
switch (*value) {
|
NMSettingWiredWakeOnLan wol_flags = NM_SETTING_WIRED_WAKE_ON_LAN_NONE;
|
||||||
|
|
||||||
|
if (!opt_val) {
|
||||||
|
PARSE_WARNING ("Wake-on-LAN options missing");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
|
||||||
|
for (; *opt_val; opt_val++) {
|
||||||
|
switch (*opt_val) {
|
||||||
case 'p':
|
case 'p':
|
||||||
wol_flags |= NM_SETTING_WIRED_WAKE_ON_LAN_PHY;
|
wol_flags |= NM_SETTING_WIRED_WAKE_ON_LAN_PHY;
|
||||||
break;
|
break;
|
||||||
@@ -4127,68 +4148,30 @@ parse_ethtool_option_wol (const char *value, NMSettingWiredWakeOnLan *out_flags)
|
|||||||
wol_flags = NM_SETTING_WIRED_WAKE_ON_LAN_NONE;
|
wol_flags = NM_SETTING_WIRED_WAKE_ON_LAN_NONE;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
PARSE_WARNING ("unrecognized Wake-on-LAN option '%c'", *value);
|
PARSE_WARNING ("unrecognized Wake-on-LAN option '%c'", *opt_val);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
*out_flags = wol_flags;
|
*out_flags = wol_flags;
|
||||||
}
|
|
||||||
|
|
||||||
static void parse_ethtool_option_sopass (const char *value, char **out_password)
|
|
||||||
{
|
|
||||||
if (!value) {
|
|
||||||
PARSE_WARNING ("Wake-on-LAN password missing");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
g_clear_pointer (out_password, g_free);
|
|
||||||
if (!nm_utils_hwaddr_valid (value, ETH_ALEN)) {
|
|
||||||
PARSE_WARNING ("Wake-on-LAN password '%s' is invalid", value);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
*out_password = g_strdup (value);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
parse_ethtool_option (const char *value,
|
|
||||||
NMSettingWiredWakeOnLan *out_flags,
|
|
||||||
char **out_password,
|
|
||||||
gboolean *out_autoneg,
|
|
||||||
guint32 *out_speed,
|
|
||||||
const char **out_duplex)
|
|
||||||
{
|
|
||||||
gs_free const char **words = NULL;
|
|
||||||
const char *const *iter;
|
|
||||||
const char *opt_val, *opt;
|
|
||||||
|
|
||||||
words = nm_utils_strsplit_set (value, "\t ");
|
|
||||||
if (!words)
|
|
||||||
return;
|
|
||||||
|
|
||||||
iter = words;
|
|
||||||
|
|
||||||
while (iter[0]) {
|
|
||||||
opt = iter++[0];
|
|
||||||
opt_val = iter[0];
|
|
||||||
|
|
||||||
if (nm_streq (opt, "autoneg"))
|
|
||||||
parse_ethtool_option_autoneg (opt_val, out_autoneg);
|
|
||||||
else if (nm_streq (opt, "speed"))
|
|
||||||
parse_ethtool_option_speed (opt_val, out_speed);
|
|
||||||
else if (nm_streq (opt, "duplex"))
|
|
||||||
parse_ethtool_option_duplex (opt_val, out_duplex);
|
|
||||||
else if (nm_streq (opt, "wol"))
|
|
||||||
parse_ethtool_option_wol (opt_val, out_flags);
|
|
||||||
else if (nm_streq (opt, "sopass"))
|
|
||||||
parse_ethtool_option_sopass (opt_val, out_password);
|
|
||||||
else {
|
|
||||||
/* Silently skip unknown options */
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (iter[0])
|
if (nm_streq (opt, "sopass")) {
|
||||||
iter++;
|
if (!opt_val) {
|
||||||
|
PARSE_WARNING ("Wake-on-LAN password missing");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
|
||||||
|
if (nm_utils_hwaddr_valid (opt_val, ETH_ALEN)) {
|
||||||
|
g_clear_pointer (out_password, g_free);
|
||||||
|
*out_password = g_strdup (opt_val);
|
||||||
|
} else
|
||||||
|
PARSE_WARNING ("Wake-on-LAN password '%s' is invalid", opt_val);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Silently skip unknown options */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -4196,8 +4179,10 @@ static void
|
|||||||
parse_ethtool_options (shvarFile *ifcfg, NMSettingWired *s_wired, const char *value)
|
parse_ethtool_options (shvarFile *ifcfg, NMSettingWired *s_wired, const char *value)
|
||||||
{
|
{
|
||||||
NMSettingWiredWakeOnLan wol_flags = NM_SETTING_WIRED_WAKE_ON_LAN_DEFAULT;
|
NMSettingWiredWakeOnLan wol_flags = NM_SETTING_WIRED_WAKE_ON_LAN_DEFAULT;
|
||||||
gs_free char *wol_password = NULL, *wol_value = NULL;
|
gs_free char *wol_password = NULL;
|
||||||
gboolean ignore_wol_password = FALSE, autoneg = FALSE;
|
gs_free char *wol_value = NULL;
|
||||||
|
gboolean ignore_wol_password = FALSE;
|
||||||
|
gboolean autoneg = FALSE;
|
||||||
guint32 speed = 0;
|
guint32 speed = 0;
|
||||||
const char *duplex = NULL;
|
const char *duplex = NULL;
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user