Commit bcbea6fe authored by Thomas Haller's avatar Thomas Haller

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.
parent 64e0e241
......@@ -4046,149 +4046,132 @@ wireless_connection_from_ifcfg (const char *file,
}
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) {
PARSE_WARNING ("Auto-negotiation option missing");
return;
}
if (g_str_equal (value, "off"))
*out_autoneg = FALSE;
else if (g_str_equal (value, "on"))
*out_autoneg = TRUE;
else
PARSE_WARNING ("Auto-negotiation unknown value: %s", value);
}
gs_free const char **words = NULL;
guint i;
static void
parse_ethtool_option_speed (const char *value, guint32 *out_speed)
{
if (!value) {
PARSE_WARNING ("Speed option missing");
words = nm_utils_strsplit_set (value, "\t \n");
if (!words)
return;
}
*out_speed = _nm_utils_ascii_str_to_int64 (value, 10, 0, G_MAXUINT32, 0);
if (errno)
PARSE_WARNING ("Speed value '%s' is invalid", value);
}
for (i = 0; words[i]; ) {
const char *opt = words[i];
const char *opt_val = words[++i];
static void
parse_ethtool_option_duplex (const char *value, const char **out_duplex)
{
if (!value) {
PARSE_WARNING ("Duplex option missing");
return;
}
if (g_str_equal (value, "half"))
*out_duplex = "half";
else if (g_str_equal (value, "full"))
*out_duplex = "full";
else
PARSE_WARNING ("Duplex unknown value: %s", value);
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;
else if (nm_streq (opt_val, "on"))
*out_autoneg = TRUE;
else
PARSE_WARNING ("Auto-negotiation 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 (nm_streq (opt, "speed")) {
guint32 speed;
if (!value) {
PARSE_WARNING ("Wake-on-LAN options missing");
return;
}
if (!opt_val) {
PARSE_WARNING ("Speed option missing");
break;
}
i++;
for (; *value; value++) {
switch (*value) {
case 'p':
wol_flags |= NM_SETTING_WIRED_WAKE_ON_LAN_PHY;
break;
case 'u':
wol_flags |= NM_SETTING_WIRED_WAKE_ON_LAN_UNICAST;
break;
case 'm':
wol_flags |= NM_SETTING_WIRED_WAKE_ON_LAN_MULTICAST;
break;
case 'b':
wol_flags |= NM_SETTING_WIRED_WAKE_ON_LAN_BROADCAST;
break;
case 'a':
wol_flags |= NM_SETTING_WIRED_WAKE_ON_LAN_ARP;
break;
case 'g':
wol_flags |= NM_SETTING_WIRED_WAKE_ON_LAN_MAGIC;
break;
case 's':
break;
case 'd':
wol_flags = NM_SETTING_WIRED_WAKE_ON_LAN_NONE;
break;
default:
PARSE_WARNING ("unrecognized Wake-on-LAN option '%c'", *value);
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_flags = wol_flags;
}
if (nm_streq (opt, "duplex")) {
if (!opt_val) {
PARSE_WARNING ("Duplex option missing");
break;
}
i++;
static void parse_ethtool_option_sopass (const char *value, char **out_password)
{
if (!value) {
PARSE_WARNING ("Wake-on-LAN password missing");
return;
}
if (nm_streq (opt_val, "half"))
*out_duplex = "half";
else if (nm_streq (opt_val, "full"))
*out_duplex = "full";
else
PARSE_WARNING ("Duplex unknown value: %s", opt_val);
continue;
}
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;
}
if (nm_streq (opt, "wol")) {
NMSettingWiredWakeOnLan wol_flags = NM_SETTING_WIRED_WAKE_ON_LAN_NONE;
*out_password = g_strdup (value);
}
if (!opt_val) {
PARSE_WARNING ("Wake-on-LAN options missing");
break;
}
i++;
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;
for (; *opt_val; opt_val++) {
switch (*opt_val) {
case 'p':
wol_flags |= NM_SETTING_WIRED_WAKE_ON_LAN_PHY;
break;
case 'u':
wol_flags |= NM_SETTING_WIRED_WAKE_ON_LAN_UNICAST;
break;
case 'm':
wol_flags |= NM_SETTING_WIRED_WAKE_ON_LAN_MULTICAST;
break;
case 'b':
wol_flags |= NM_SETTING_WIRED_WAKE_ON_LAN_BROADCAST;
break;
case 'a':
wol_flags |= NM_SETTING_WIRED_WAKE_ON_LAN_ARP;
break;
case 'g':
wol_flags |= NM_SETTING_WIRED_WAKE_ON_LAN_MAGIC;
break;
case 's':
break;
case 'd':
wol_flags = NM_SETTING_WIRED_WAKE_ON_LAN_NONE;
break;
default:
PARSE_WARNING ("unrecognized Wake-on-LAN option '%c'", *opt_val);
}
}
words = nm_utils_strsplit_set (value, "\t ");
if (!words)
return;
*out_flags = wol_flags;
continue;
}
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 */
if (nm_streq (opt, "sopass")) {
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;
}
if (iter[0])
iter++;
/* Silently skip unknown options */
}
}
......@@ -4196,8 +4179,10 @@ static void
parse_ethtool_options (shvarFile *ifcfg, NMSettingWired *s_wired, const char *value)
{
NMSettingWiredWakeOnLan wol_flags = NM_SETTING_WIRED_WAKE_ON_LAN_DEFAULT;
gs_free char *wol_password = NULL, *wol_value = NULL;
gboolean ignore_wol_password = FALSE, autoneg = FALSE;
gs_free char *wol_password = NULL;
gs_free char *wol_value = NULL;
gboolean ignore_wol_password = FALSE;
gboolean autoneg = FALSE;
guint32 speed = 0;
const char *duplex = NULL;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment