From f0c1efbf426260c6ed45f3ae83b9a4e2b5a347c4 Mon Sep 17 00:00:00 2001 From: Lubomir Rintel Date: Tue, 24 Apr 2018 13:14:07 +0200 Subject: [PATCH 1/9] all: add and utilize nm_utils_is_separator() It is meant to be rather similar in nature to isblank() or g_ascii_isspace(). Sadly, isblank() is locale dependent while g_ascii_isspace() also considers vertical whitespace as a space. That's no good for configuration files that are strucutured into lines, which happens to be a pretty common case. --- clients/cli/connections.c | 4 ++-- clients/common/nm-meta-setting-desc.c | 2 +- shared/nm-utils/nm-shared-utils.h | 6 ++++++ src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-reader.c | 2 +- 4 files changed, 10 insertions(+), 4 deletions(-) diff --git a/clients/cli/connections.c b/clients/cli/connections.c index 32e3f7f424..c509c6a928 100644 --- a/clients/cli/connections.c +++ b/clients/cli/connections.c @@ -5884,7 +5884,7 @@ _split_cmd (const char *cmd, char **out_arg0, const char **out_argr) if (!cmd) return; - while (NM_IN_SET (cmd[0], ' ', '\t')) + while (nm_utils_is_separator (cmd[0])) cmd++; if (!cmd[0]) return; @@ -5893,7 +5893,7 @@ _split_cmd (const char *cmd, char **out_arg0, const char **out_argr) arg0 = g_strndup (cmd, l); cmd += l; if (cmd[0]) { - while (NM_IN_SET (cmd[0], ' ', '\t')) + while (nm_utils_is_separator (cmd[0])) cmd++; if (cmd[0]) argr = cmd; diff --git a/clients/common/nm-meta-setting-desc.c b/clients/common/nm-meta-setting-desc.c index dcf0eed395..2c8a5159de 100644 --- a/clients/common/nm-meta-setting-desc.c +++ b/clients/common/nm-meta-setting-desc.c @@ -2157,7 +2157,7 @@ _get_fcn_802_1x_phase2_private_key (ARGS_GET_FCN) password = path + strcspn (path, " \t"); \ if (password[0] != '\0') { \ password[0] = '\0'; \ - while (NM_IN_SET (password[0], ' ', '\t')) \ + while (nm_utils_is_separator (password[0])) \ password++; \ } else \ password = password_free = g_strdup (pwd_func (NM_SETTING_802_1X (setting))); \ diff --git a/shared/nm-utils/nm-shared-utils.h b/shared/nm-utils/nm-shared-utils.h index 5ee83eb877..ddb77a429b 100644 --- a/shared/nm-utils/nm-shared-utils.h +++ b/shared/nm-utils/nm-shared-utils.h @@ -191,6 +191,12 @@ void nm_utils_strbuf_append_str (char **buf, gsize *len, const char *str); const char *nm_strquote (char *buf, gsize buf_len, const char *str); +static inline gboolean +nm_utils_is_separator (const char c) +{ + return NM_IN_SET (c, ' ', '\t'); +} + /*****************************************************************************/ const char **nm_utils_strsplit_set (const char *str, const char *delimiters); diff --git a/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-reader.c b/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-reader.c index 7fa473cb6e..11474c3da7 100644 --- a/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-reader.c +++ b/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-reader.c @@ -502,7 +502,7 @@ parse_route_line_is_comment (const char *line) * * initscripts compares: "$line" =~ '^[[:space:]]*(\#.*)?$' */ - while (NM_IN_SET (line[0], ' ', '\t')) + while (nm_utils_is_separator (line[0])) line++; if (NM_IN_SET (line[0], '\0', '#')) return TRUE; From e69d386975be997d3d840de9045e51521ac4474c Mon Sep 17 00:00:00 2001 From: Lubomir Rintel Date: Tue, 24 Apr 2018 11:20:03 +0200 Subject: [PATCH 2/9] all: use the elvis operator wherever possible Coccinelle: @@ expression a, b; @@ -a ? a : b +a ?: b Applied with: spatch --sp-file ternary.cocci --in-place --smpl-spacing --dir . With some manual adjustments on spots that Cocci didn't catch for reasons unknown. Thanks to the marvelous effort of the GNU compiler developer we can now spare a couple of bits that could be used for more important things, like this commit message. Standards commitees yet have to catch up. --- clients/cli/connections.c | 20 +++++++++---------- clients/cli/devices.c | 6 +++--- clients/cli/general.c | 4 ++-- clients/cli/settings.c | 4 ++-- clients/cli/utils.c | 2 +- clients/common/nm-meta-setting-desc.c | 2 +- clients/common/nm-vpn-helpers.c | 2 +- clients/tui/newt/nmt-newt-utils.c | 6 +++--- clients/tui/nmt-connect-connection-list.c | 6 +++--- dispatcher/nm-dispatcher.c | 8 +++----- dispatcher/tests/test-dispatcher-envp.c | 2 +- examples/C/glib/get-ap-info-libnm.c | 2 +- libnm-core/nm-connection.c | 2 +- libnm-core/nm-keyfile-utils.c | 4 ++-- libnm-core/nm-keyfile.c | 8 ++++---- libnm-core/nm-setting-adsl.c | 2 +- libnm-core/nm-utils.c | 6 +++--- libnm-glib/nm-client.c | 8 ++++---- libnm-glib/nm-object.c | 4 ++-- libnm-glib/nm-remote-settings.c | 4 ++-- libnm-util/nm-connection.c | 2 +- libnm-util/nm-setting-adsl.c | 2 +- libnm-util/nm-utils.c | 4 ++-- libnm-util/nm-value-transforms.c | 2 +- libnm/nm-manager.c | 4 ++-- libnm/nm-object.c | 2 +- libnm/nm-remote-settings.c | 4 ++-- libnm/nm-secret-agent-old.c | 2 +- shared/nm-utils/nm-test-utils.h | 4 ++-- src/devices/adsl/nm-atm-manager.c | 2 +- src/devices/adsl/nm-device-adsl.c | 2 +- src/devices/bluetooth/nm-bluez-device.c | 2 +- src/devices/nm-device-bond.c | 4 ++-- src/devices/nm-device.c | 2 +- src/devices/ovs/nm-ovsdb.c | 20 +++++++++---------- src/devices/wifi/nm-device-wifi.c | 4 ++-- src/devices/wwan/nm-modem-broadband.c | 2 +- src/devices/wwan/nm-modem.c | 2 +- src/dhcp/nm-dhcp-client.c | 2 +- src/dhcp/nm-dhcp-listener.c | 2 +- src/dhcp/nm-dhcp-manager.c | 2 +- src/main.c | 2 +- src/nm-config.c | 4 ++-- src/nm-core-utils.c | 10 +++++----- src/nm-core-utils.h | 2 +- src/nm-dispatcher.c | 6 +++--- src/nm-firewall-manager.c | 2 +- src/nm-manager.c | 2 +- src/nm-policy.c | 2 +- src/nm-rfkill-manager.c | 2 +- src/platform/nm-linux-platform.c | 2 +- src/platform/nm-platform.c | 12 +++++------ src/platform/tests/test-common.c | 6 +++--- src/settings/nm-settings-connection.c | 8 ++++---- src/settings/plugins/ibft/nms-ibft-reader.c | 8 ++++---- .../plugins/ifcfg-rh/nms-ifcfg-rh-plugin.c | 2 +- .../plugins/ifcfg-rh/nms-ifcfg-rh-reader.c | 10 +++++----- .../plugins/ifcfg-rh/tests/test-ifcfg-rh.c | 2 +- .../plugins/ifupdown/nms-ifupdown-parser.c | 4 ++-- src/supplicant/nm-supplicant-config.c | 10 +++++----- .../nm-supplicant-settings-verify.c | 4 ++-- src/vpn/nm-vpn-connection.c | 6 +++--- 62 files changed, 139 insertions(+), 141 deletions(-) diff --git a/clients/cli/connections.c b/clients/cli/connections.c index c509c6a928..47e608b7ba 100644 --- a/clients/cli/connections.c +++ b/clients/cli/connections.c @@ -1275,7 +1275,7 @@ nmc_active_connection_details (NMActiveConnection *acon, NmCli *nmc) arr = nmc_dup_fields_array (tmpl, NMC_OF_FLAG_SECTION_PREFIX); set_val_strc (arr, 0, nmc_fields_con_active_details_groups[5]->name); set_val_str (arr, 1, type_str); - set_val_strc (arr, 2, username ? username : get_vpn_data_item (con, VPN_DATA_ITEM_USERNAME)); + set_val_strc (arr, 2, username ?: get_vpn_data_item (con, VPN_DATA_ITEM_USERNAME)); set_val_strc (arr, 3, get_vpn_data_item (con, VPN_DATA_ITEM_GATEWAY)); set_val_str (arr, 4, banner_str); set_val_str (arr, 5, vpn_state_str); @@ -4248,7 +4248,7 @@ nmc_read_connection_properties (NmCli *nmc, return FALSE; if (!*argc && nmc->complete) - complete_property (setting, strv[1], value ? value : "", connection); + complete_property (setting, strv[1], value ?: "", connection); if (!set_property (connection, setting_name, strv[1], value, modifier, error)) return FALSE; @@ -4329,7 +4329,7 @@ nmc_read_connection_properties (NmCli *nmc, return FALSE; if (!*argc && nmc->complete) - complete_option (chosen, value ? value : "", connection); + complete_option (chosen, value ?: "", connection); if (!set_option (nmc, connection, chosen, value, error)) return FALSE; @@ -7014,7 +7014,7 @@ editor_menu_main (NmCli *nmc, NMConnection *connection, const char *connection_t /* in top level - no setting selected yet */ const char *setting_name; NMSetting *setting; - const char *user_arg = cmd_arg_s ? cmd_arg_s : cmd_arg_p; + const char *user_arg = cmd_arg_s ?: cmd_arg_p; setting_name = ask_check_setting (user_arg, valid_settings_main, @@ -7106,7 +7106,7 @@ editor_menu_main (NmCli *nmc, NMConnection *connection, const char *connection_t /* cmd_arg_s != NULL means argument is "setting.property" */ descr_all = !cmd_arg_s && !menu_ctx.curr_setting; - user_s = descr_all ? cmd_arg_p : cmd_arg_s ? cmd_arg_s : NULL; + user_s = descr_all ? cmd_arg_p : cmd_arg_s; if (user_s) { ss = is_setting_valid (connection, valid_settings_main, @@ -7199,7 +7199,7 @@ editor_menu_main (NmCli *nmc, NMConnection *connection, const char *connection_t /* cmd_arg_s != NULL means argument is "setting.property" */ descr_all = !cmd_arg_s && !menu_ctx.curr_setting; - user_s = descr_all ? cmd_arg_p : cmd_arg_s ? cmd_arg_s : NULL; + user_s = descr_all ? cmd_arg_p : cmd_arg_s; if (user_s) { ss = is_setting_valid (connection, valid_settings_main, @@ -7265,7 +7265,7 @@ editor_menu_main (NmCli *nmc, NMConnection *connection, const char *connection_t /* cmd_arg_s != NULL means argument is "setting.property" */ whole_setting = !cmd_arg_s && !menu_ctx.curr_setting; - user_s = whole_setting ? cmd_arg_p : cmd_arg_s ? cmd_arg_s : NULL; + user_s = whole_setting ? cmd_arg_p : cmd_arg_s; if (user_s) { const char *s_name; @@ -7583,7 +7583,7 @@ editor_menu_main (NmCli *nmc, NMConnection *connection, const char *connection_t nmc->editor_prompt_color); } else g_print (_("Invalid configuration option '%s'; allowed [%s]\n"), - cmd_arg_v ? cmd_arg_v : "", "status-line, save-confirmation, show-secrets, prompt-color"); + cmd_arg_v ?: "", "status-line, save-confirmation, show-secrets, prompt-color"); break; @@ -7654,7 +7654,7 @@ editor_init_new_connection (NmCli *nmc, NMConnection *connection, const char *sl g_object_set (s_con, NM_SETTING_CONNECTION_TYPE, NM_SETTING_WIRED_SETTING_NAME, - NM_SETTING_CONNECTION_MASTER, dev_ifname ? dev_ifname : "eth0", + NM_SETTING_CONNECTION_MASTER, dev_ifname ?: "eth0", NM_SETTING_CONNECTION_SLAVE_TYPE, slave_type, NULL); } else { @@ -7675,7 +7675,7 @@ editor_init_new_connection (NmCli *nmc, NMConnection *connection, const char *sl const char *dev_ifname = get_ethernet_device_name (nmc); g_object_set (NM_SETTING_VLAN (base_setting), - NM_SETTING_VLAN_PARENT, dev_ifname ? dev_ifname : "eth0", + NM_SETTING_VLAN_PARENT, dev_ifname ?: "eth0", NULL); } diff --git a/clients/cli/devices.c b/clients/cli/devices.c index 43e10fe185..5ad546a28a 100644 --- a/clients/cli/devices.c +++ b/clients/cli/devices.c @@ -1160,7 +1160,7 @@ show_device_info (NMDevice *device, NmCli *nmc) set_val_strc (arr, 6, nm_device_get_driver (device) ? nm_device_get_driver (device) : _("(unknown)")); set_val_strc (arr, 7, nm_device_get_driver_version (device)); set_val_strc (arr, 8, nm_device_get_firmware_version (device)); - set_val_strc (arr, 9, hwaddr ? hwaddr : _("(unknown)")); + set_val_strc (arr, 9, hwaddr ?: _("(unknown)")); set_val_str (arr, 10, mtu_str); set_val_str (arr, 11, state_str); set_val_str (arr, 12, reason_str); @@ -1268,7 +1268,7 @@ show_device_info (NMDevice *device, NmCli *nmc) } tmpl = (const NMMetaAbstractInfo *const*) nmc_fields_dev_wifi_list; - out_indices = parse_output_fields (section_fld ? section_fld : NMC_FIELDS_DEV_WIFI_LIST_FOR_DEV_LIST, + out_indices = parse_output_fields (section_fld ?: NMC_FIELDS_DEV_WIFI_LIST_FOR_DEV_LIST, tmpl, FALSE, NULL, NULL); arr = nmc_dup_fields_array (tmpl, NMC_OF_FLAG_FIELD_NAMES); g_ptr_array_add (out.output_data, arr); @@ -2906,7 +2906,7 @@ do_device_wifi_connect_network (NmCli *nmc, int argc, char **argv) if (nmc->ask) { ssid_ask = nmc_readline (_("SSID or BSSID: ")); - param_user = ssid_ask ? ssid_ask : ""; + param_user = ssid_ask ?: ""; bssid1_arr = nm_utils_hwaddr_atoba (param_user, ETH_ALEN); } if (!ssid_ask) { diff --git a/clients/cli/general.c b/clients/cli/general.c index 3d3c6eaf68..17c0d79476 100644 --- a/clients/cli/general.c +++ b/clients/cli/general.c @@ -516,8 +516,8 @@ show_nm_status (NmCli *nmc, const char *pretty_header_name, const char *print_fl { gs_free_error GError *error = NULL; const char *fields_str; - const char *fields_all = print_flds ? print_flds : NMC_FIELDS_NM_STATUS_ALL; - const char *fields_common = print_flds ? print_flds : NMC_FIELDS_NM_STATUS_COMMON; + const char *fields_all = print_flds ?: NMC_FIELDS_NM_STATUS_ALL; + const char *fields_common = print_flds ?: NMC_FIELDS_NM_STATUS_COMMON; if (!nmc->required_fields || strcasecmp (nmc->required_fields, "common") == 0) fields_str = fields_common; diff --git a/clients/cli/settings.c b/clients/cli/settings.c index 5c3deeadd3..01142ba262 100644 --- a/clients/cli/settings.c +++ b/clients/cli/settings.c @@ -749,9 +749,9 @@ nmc_setting_get_property_desc (NMSetting *setting, const char *prop) return g_strdup_printf ("%s\n%s\n%s%s%s%s", setting_desc_title, - setting_desc ? setting_desc : "", + setting_desc ?: "", nmcli_nl, nmcli_desc_title, nmcli_nl, - nmcli_desc ? nmcli_desc : ""); + nmcli_desc ?: ""); } /* diff --git a/clients/cli/utils.c b/clients/cli/utils.c index 4caadab2cc..5101ab6892 100644 --- a/clients/cli/utils.c +++ b/clients/cli/utils.c @@ -390,7 +390,7 @@ nmc_terminal_show_progress (const char *str) const char slashes[4] = {'|', '/', '-', '\\'}; nmc_terminal_erase_line (); - g_print ("%c %s", slashes[idx++], str ? str : ""); + g_print ("%c %s", slashes[idx++], str ?: ""); fflush (stdout); if (idx == 4) idx = 0; diff --git a/clients/common/nm-meta-setting-desc.c b/clients/common/nm-meta-setting-desc.c index 2c8a5159de..2988b084b7 100644 --- a/clients/common/nm-meta-setting-desc.c +++ b/clients/common/nm-meta-setting-desc.c @@ -2891,7 +2891,7 @@ dcb_parse_uint_array (const char *val, *iter = g_strstrip (*iter); - num = _nm_utils_ascii_str_to_int64 (*iter, 10, 0, other ? other : max, -1); + num = _nm_utils_ascii_str_to_int64 (*iter, 10, 0, other ?: max, -1); /* If number is greater than 'max' it must equal 'other' */ if ( num == -1 diff --git a/clients/common/nm-vpn-helpers.c b/clients/common/nm-vpn-helpers.c index 15611c45c2..c3237f12b5 100644 --- a/clients/common/nm-vpn-helpers.c +++ b/clients/common/nm-vpn-helpers.c @@ -238,7 +238,7 @@ nm_vpn_openconnect_authenticate_helper (const char *host, * HOST='1.2.3.4' * FINGERPRINT='sha1:32bac90cf09a722e10ecc1942c67fe2ac8c21e2e' */ - strv = g_strsplit_set (output ? output : "", "\r\n", 0); + strv = g_strsplit_set (output ?: "", "\r\n", 0); for (iter = strv; iter && *iter; iter++) { _extract_variable_value (*iter, "COOKIE=", cookie); _extract_variable_value (*iter, "HOST=", gateway); diff --git a/clients/tui/newt/nmt-newt-utils.c b/clients/tui/newt/nmt-newt-utils.c index 112d259966..e886650036 100644 --- a/clients/tui/newt/nmt-newt-utils.c +++ b/clients/tui/newt/nmt-newt-utils.c @@ -65,9 +65,9 @@ nmt_newt_dialog_g_log_handler (const char *log_domain, } full_message = g_strdup_printf ("%s%s%s%s%s", - log_domain ? log_domain : "", + log_domain ?: "", log_domain && level_name ? " " : "", - level_name ? level_name : "", + level_name ?: "", log_domain || level_name ? ": " : "", message); @@ -87,7 +87,7 @@ nmt_newt_dialog_g_log_handler (const char *log_domain, newtGridSetField (grid, 0, 1, NEWT_GRID_COMPONENT, ok, 0, 1, 0, 0, NEWT_ANCHOR_RIGHT, 0); - newtGridWrappedWindow (grid, (char *) (level_name ? level_name : "")); + newtGridWrappedWindow (grid, (char *) (level_name ?: "")); newtGridFree (grid, TRUE); form = newtForm (NULL, NULL, 0); diff --git a/clients/tui/nmt-connect-connection-list.c b/clients/tui/nmt-connect-connection-list.c index cced55fc2b..ae633ec0b7 100644 --- a/clients/tui/nmt-connect-connection-list.c +++ b/clients/tui/nmt-connect-connection-list.c @@ -307,7 +307,7 @@ add_connections_for_aps (NmtConnectDevice *nmtdev, } if (!nmtconn->name) - nmtconn->name = nmtconn->ssid ? nmtconn->ssid : ""; + nmtconn->name = nmtconn->ssid ?: ""; nmtdev->conns = g_slist_prepend (nmtdev->conns, nmtconn); } @@ -376,7 +376,7 @@ append_nmt_devices_for_virtual_devices (GSList *nmt_devices, g_free (name); else { nmtdev = g_slice_new0 (NmtConnectDevice); - nmtdev->name = name ? name : g_strdup ("Unknown"); + nmtdev->name = name ?: g_strdup("Unknown"); nmtdev->sort_order = sort_order; g_hash_table_insert (devices_by_name, nmtdev->name, nmtdev); @@ -533,7 +533,7 @@ nmt_connect_connection_list_rebuild (NmtConnectConnectionList *list) nmtconn->name, (int)(max_width - nmt_newt_text_width (nmtconn->name)), "", strength_col ? " " : "", - strength_col ? strength_col : ""); + strength_col ?: ""); nmt_newt_listbox_append (listbox, row, nmtconn); g_free (row); diff --git a/dispatcher/nm-dispatcher.c b/dispatcher/nm-dispatcher.c index ca11523ab1..899669530a 100644 --- a/dispatcher/nm-dispatcher.c +++ b/dispatcher/nm-dispatcher.c @@ -144,7 +144,7 @@ struct Request { (_request)->request_id, \ (_request)->action, \ (_request)->iface ? " [" : "", \ - (_request)->iface ? (_request)->iface : "", \ + (_request)->iface ?: "", \ (_request)->iface ? "]" : "", \ (_script) ? ", \"" : "", \ (_script) ? (_script)->script : "", \ @@ -297,7 +297,7 @@ complete_request (Request *request) g_variant_builder_add (&results, "(sus)", script->script, script->result, - script->error ? script->error : ""); + script->error ?: ""); } ret = g_variant_new ("(a(sus))", &results); @@ -537,9 +537,7 @@ script_dispatch (ScriptInfo *script) script->dispatched = TRUE; argv[0] = script->script; - argv[1] = request->iface - ? request->iface - : (!strcmp (request->action, NMD_ACTION_HOSTNAME) ? "none" : ""); + argv[1] = request->iface ?: (!strcmp(request->action, NMD_ACTION_HOSTNAME) ? "none" : ""); argv[2] = request->action; argv[3] = NULL; diff --git a/dispatcher/tests/test-dispatcher-envp.c b/dispatcher/tests/test-dispatcher-envp.c index 25cd5e60ce..936b6e93f7 100644 --- a/dispatcher/tests/test-dispatcher-envp.c +++ b/dispatcher/tests/test-dispatcher-envp.c @@ -543,7 +543,7 @@ test_generic (const char *file, const char *override_vpn_ip_iface) device_dhcp4_props, device_dhcp6_props, connectivity_change, - override_vpn_ip_iface ? override_vpn_ip_iface : vpn_ip_iface, + override_vpn_ip_iface ?: vpn_ip_iface, vpn_proxy_props, vpn_ip4_props, vpn_ip6_props, diff --git a/examples/C/glib/get-ap-info-libnm.c b/examples/C/glib/get-ap-info-libnm.c index b312356c76..c11d414651 100644 --- a/examples/C/glib/get-ap-info-libnm.c +++ b/examples/C/glib/get-ap-info-libnm.c @@ -180,7 +180,7 @@ show_wifi_device_info (NMDevice *device) speed /= 1000; printf ("Device: %s ---- Driver: %s ---- Speed: %d Mbit/s ---- Active AP: %s\n", - iface, driver, speed, active_ssid_str ? active_ssid_str : "none"); + iface, driver, speed, active_ssid_str ?: "none"); printf ("=================================================================================\n"); g_free (active_ssid_str); diff --git a/libnm-core/nm-connection.c b/libnm-core/nm-connection.c index 80c21d91b8..790e98b800 100644 --- a/libnm-core/nm-connection.c +++ b/libnm-core/nm-connection.c @@ -1530,7 +1530,7 @@ nm_connection_update_secrets (NMConnection *connection, g_signal_handlers_block_by_func (setting, (GCallback) setting_changed_cb, connection); success_detail = _nm_setting_update_secrets (setting, - setting_dict ? setting_dict : secrets, + setting_dict ?: secrets, error); g_signal_handlers_unblock_by_func (setting, (GCallback) setting_changed_cb, connection); diff --git a/libnm-core/nm-keyfile-utils.c b/libnm-core/nm-keyfile-utils.c index cd937c67a6..5000b58318 100644 --- a/libnm-core/nm-keyfile-utils.c +++ b/libnm-core/nm-keyfile-utils.c @@ -106,7 +106,7 @@ nm_keyfile_plugin_kf_set_##stype##_list (GKeyFile *kf, \ const char *alias; \ \ alias = nm_keyfile_plugin_get_alias_for_setting_name (group); \ - g_key_file_set_##stype##_list (kf, alias ? alias : group, key, list, length); \ + g_key_file_set_##stype##_list (kf, alias ?: group, key, list, length); \ } DEFINE_KF_LIST_WRAPPER(integer, gint*, gint); @@ -170,7 +170,7 @@ nm_keyfile_plugin_kf_set_##stype (GKeyFile *kf, \ const char *alias; \ \ alias = nm_keyfile_plugin_get_alias_for_setting_name (group); \ - g_key_file_set_##stype (kf, alias ? alias : group, key, value); \ + g_key_file_set_##stype (kf, alias ?: group, key, value); \ } DEFINE_KF_WRAPPER(string, gchar*, const gchar*); diff --git a/libnm-core/nm-keyfile.c b/libnm-core/nm-keyfile.c index 3a84bd0113..5c94c35c3e 100644 --- a/libnm-core/nm-keyfile.c +++ b/libnm-core/nm-keyfile.c @@ -112,7 +112,7 @@ setting_alias_parser (KeyfileReaderInfo *info, NMSetting *setting, const char *k if (s) { key_setting_name = nm_keyfile_plugin_get_setting_name_for_alias (s); g_object_set (G_OBJECT (setting), - key, key_setting_name ? key_setting_name : s, + key, key_setting_name ?: s, NULL); g_free (s); } @@ -358,7 +358,7 @@ read_one_ip_address_or_route (KeyfileReaderInfo *info, gs_free char *value = NULL; gs_free char *value_orig = NULL; -#define VALUE_ORIG() (value_orig ? value_orig : (value_orig = nm_keyfile_plugin_kf_get_string (info->keyfile, setting_name, key_name, NULL))) +#define VALUE_ORIG() (value_orig ?: (value_orig = nm_keyfile_plugin_kf_get_string (info->keyfile, setting_name, key_name, NULL))) value = nm_keyfile_plugin_kf_get_string (info->keyfile, setting_name, key_name, NULL); if (!value) @@ -1363,7 +1363,7 @@ parity_parser (KeyfileReaderInfo *info, NMSetting *setting, const char *key) default: handle_warn (info, key, NM_KEYFILE_WARN_SEVERITY_WARN, _("invalid parity value '%s'"), - str_val ? str_val : ""); + str_val ?: ""); return; } @@ -1506,7 +1506,7 @@ setting_alias_writer (KeyfileWriterInfo *info, nm_keyfile_plugin_kf_set_string (info->keyfile, nm_setting_get_name (setting), key, - alias ? alias : str); + alias ?: str); } static void diff --git a/libnm-core/nm-setting-adsl.c b/libnm-core/nm-setting-adsl.c index 8be288b668..c2eb5db419 100644 --- a/libnm-core/nm-setting-adsl.c +++ b/libnm-core/nm-setting-adsl.c @@ -206,7 +206,7 @@ verify (NMSetting *setting, NMConnection *connection, GError **error) NM_CONNECTION_ERROR, NM_CONNECTION_ERROR_INVALID_PROPERTY, _("'%s' is not a valid value for the property"), - priv->protocol ? priv->protocol : "(null)"); + priv->protocol ?: "(null)"); g_prefix_error (error, "%s.%s: ", NM_SETTING_ADSL_SETTING_NAME, NM_SETTING_ADSL_PROTOCOL); return FALSE; } diff --git a/libnm-core/nm-utils.c b/libnm-core/nm-utils.c index f9c3eb1c60..a6d6486fac 100644 --- a/libnm-core/nm-utils.c +++ b/libnm-core/nm-utils.c @@ -4318,7 +4318,7 @@ nm_utils_inet_ntop (int addr_family, gconstpointer addr, char *dst) s = inet_ntop (addr_family, addr, - dst ? dst : _nm_utils_inet_ntop_buffer, + dst ?: _nm_utils_inet_ntop_buffer, addr_family == AF_INET6 ? INET6_ADDRSTRLEN : INET_ADDRSTRLEN); nm_assert (s); return s; @@ -4344,7 +4344,7 @@ nm_utils_inet_ntop (int addr_family, gconstpointer addr, char *dst) const char * nm_utils_inet4_ntop (in_addr_t inaddr, char *dst) { - return inet_ntop (AF_INET, &inaddr, dst ? dst : _nm_utils_inet_ntop_buffer, + return inet_ntop (AF_INET, &inaddr, dst ?: _nm_utils_inet_ntop_buffer, INET_ADDRSTRLEN); } @@ -4370,7 +4370,7 @@ const char * nm_utils_inet6_ntop (const struct in6_addr *in6addr, char *dst) { g_return_val_if_fail (in6addr, NULL); - return inet_ntop (AF_INET6, in6addr, dst ? dst : _nm_utils_inet_ntop_buffer, + return inet_ntop (AF_INET6, in6addr, dst ?: _nm_utils_inet_ntop_buffer, INET6_ADDRSTRLEN); } diff --git a/libnm-glib/nm-client.c b/libnm-glib/nm-client.c index c14d8c9690..0da7b0f4e5 100644 --- a/libnm-glib/nm-client.c +++ b/libnm-glib/nm-client.c @@ -674,7 +674,7 @@ nm_client_activate_connection (NMClient *client, activate_cb, info, NULL, DBUS_TYPE_G_OBJECT_PATH, connection ? nm_connection_get_path (connection) : "/", DBUS_TYPE_G_OBJECT_PATH, device ? nm_object_get_path (NM_OBJECT (device)) : "/", - DBUS_TYPE_G_OBJECT_PATH, specific_object ? specific_object : "/", + DBUS_TYPE_G_OBJECT_PATH, specific_object ?: "/", G_TYPE_INVALID); } @@ -762,7 +762,7 @@ nm_client_add_and_activate_connection (NMClient *client, add_activate_cb, info, NULL, DBUS_TYPE_G_MAP_OF_MAP_OF_VARIANT, hash, DBUS_TYPE_G_OBJECT_PATH, nm_object_get_path (NM_OBJECT (device)), - DBUS_TYPE_G_OBJECT_PATH, specific_object ? specific_object : "/", + DBUS_TYPE_G_OBJECT_PATH, specific_object ?: "/", G_TYPE_INVALID); } else info->idle_id = g_idle_add (activate_nm_not_running, info); @@ -1257,8 +1257,8 @@ nm_client_set_logging (NMClient *client, const char *level, const char *domains, return TRUE; return dbus_g_proxy_call (priv->client_proxy, "SetLogging", error, - G_TYPE_STRING, level ? level : "", - G_TYPE_STRING, domains ? domains : "", + G_TYPE_STRING, level ?: "", + G_TYPE_STRING, domains ?: "", G_TYPE_INVALID, G_TYPE_INVALID); } diff --git a/libnm-glib/nm-object.c b/libnm-glib/nm-object.c index 56db7e3cd6..5956b344ee 100644 --- a/libnm-glib/nm-object.c +++ b/libnm-glib/nm-object.c @@ -1328,7 +1328,7 @@ _nm_object_register_properties (NMObject *object, } pi = g_malloc0 (sizeof (PropertyInfo)); - pi->func = tmp->func ? tmp->func : demarshal_generic; + pi->func = tmp->func ?: demarshal_generic; pi->object_type = tmp->object_type; pi->field = tmp->field; pi->signal_prefix = tmp->signal_prefix; @@ -1572,5 +1572,5 @@ _nm_object_new_proxy (NMObject *self, const char *path, const char *interface) { NMObjectPrivate *priv = NM_OBJECT_GET_PRIVATE (self); - return _nm_dbus_new_proxy_for_connection (priv->connection, path ? path : priv->path, interface); + return _nm_dbus_new_proxy_for_connection (priv->connection, path ?: priv->path, interface); } diff --git a/libnm-glib/nm-remote-settings.c b/libnm-glib/nm-remote-settings.c index be6b7ee3b8..e8a80f6fda 100644 --- a/libnm-glib/nm-remote-settings.c +++ b/libnm-glib/nm-remote-settings.c @@ -576,7 +576,7 @@ fetch_connections_done (DBusGProxy *proxy, && priv->service_running) { g_warning ("%s: error fetching connections: %s.", __func__, - error->message ? error->message : "(unknown)"); + error->message ?: "(unknown)"); } g_clear_error (&error); @@ -964,7 +964,7 @@ nm_remote_settings_save_hostname (NMRemoteSettings *settings, save_hostname_cb, info, g_free, - G_TYPE_STRING, hostname ? hostname : "", + G_TYPE_STRING, hostname ?: "", G_TYPE_INVALID); return TRUE; } diff --git a/libnm-util/nm-connection.c b/libnm-util/nm-connection.c index 726cebdca6..3ddea2d742 100644 --- a/libnm-util/nm-connection.c +++ b/libnm-util/nm-connection.c @@ -1020,7 +1020,7 @@ nm_connection_update_secrets (NMConnection *connection, g_signal_handlers_block_by_func (setting, (GCallback) setting_changed_cb, connection); success_detail = _nm_setting_update_secrets (setting, - setting_hash ? setting_hash : secrets, + setting_hash ?: secrets, error); g_signal_handlers_unblock_by_func (setting, (GCallback) setting_changed_cb, connection); diff --git a/libnm-util/nm-setting-adsl.c b/libnm-util/nm-setting-adsl.c index 5c1775709a..f2305b38df 100644 --- a/libnm-util/nm-setting-adsl.c +++ b/libnm-util/nm-setting-adsl.c @@ -226,7 +226,7 @@ verify (NMSetting *setting, GSList *all_settings, GError **error) NM_SETTING_ADSL_ERROR, NM_SETTING_ADSL_ERROR_INVALID_PROPERTY, _("'%s' is not a valid value for the property"), - priv->protocol ? priv->protocol : "(null)"); + priv->protocol ?: "(null)"); g_prefix_error (error, "%s.%s: ", NM_SETTING_ADSL_SETTING_NAME, NM_SETTING_ADSL_PROTOCOL); return FALSE; } diff --git a/libnm-util/nm-utils.c b/libnm-util/nm-utils.c index 5840491026..54e0cc9a82 100644 --- a/libnm-util/nm-utils.c +++ b/libnm-util/nm-utils.c @@ -2474,7 +2474,7 @@ static char _nm_utils_inet_ntop_buffer[NM_UTILS_INET_ADDRSTRLEN]; const char * nm_utils_inet4_ntop (in_addr_t inaddr, char *dst) { - return inet_ntop (AF_INET, &inaddr, dst ? dst : _nm_utils_inet_ntop_buffer, + return inet_ntop (AF_INET, &inaddr, dst ?: _nm_utils_inet_ntop_buffer, INET_ADDRSTRLEN); } @@ -2502,7 +2502,7 @@ const char * nm_utils_inet6_ntop (const struct in6_addr *in6addr, char *dst) { g_return_val_if_fail (in6addr, NULL); - return inet_ntop (AF_INET6, in6addr, dst ? dst : _nm_utils_inet_ntop_buffer, + return inet_ntop (AF_INET6, in6addr, dst ?: _nm_utils_inet_ntop_buffer, INET6_ADDRSTRLEN); } diff --git a/libnm-util/nm-value-transforms.c b/libnm-util/nm-value-transforms.c index 2d31f12967..13f9eb43bc 100644 --- a/libnm-util/nm-value-transforms.c +++ b/libnm-util/nm-value-transforms.c @@ -104,7 +104,7 @@ _nm_utils_convert_string_list_to_string (const GValue *src_value, GValue *dest_v for (iter = strings; iter; iter = iter->next) { if (iter != strings) g_string_append_c (printable, ','); - g_string_append (printable, iter->data ? iter->data : "(null)"); + g_string_append (printable, iter->data ?: "(null)"); } g_value_take_string (dest_value, g_string_free (printable, FALSE)); diff --git a/libnm/nm-manager.c b/libnm/nm-manager.c index 2b9e0d9695..aac120c6e7 100644 --- a/libnm/nm-manager.c +++ b/libnm/nm-manager.c @@ -1049,7 +1049,7 @@ nm_manager_activate_connection_async (NMManager *manager, nmdbus_manager_call_activate_connection (priv->proxy, connection ? nm_connection_get_path (connection) : "/", device ? nm_object_get_path (NM_OBJECT (device)) : "/", - specific_object ? specific_object : "/", + specific_object ?: "/", cancellable, activate_cb, info); } @@ -1133,7 +1133,7 @@ nm_manager_add_and_activate_connection_async (NMManager *manager, nmdbus_manager_call_add_and_activate_connection (priv->proxy, dict, nm_object_get_path (NM_OBJECT (device)), - specific_object ? specific_object : "/", + specific_object ?: "/", cancellable, add_activate_cb, info); } diff --git a/libnm/nm-object.c b/libnm/nm-object.c index 605da4927e..f162b15dc0 100644 --- a/libnm/nm-object.c +++ b/libnm/nm-object.c @@ -968,7 +968,7 @@ _nm_object_register_properties (NMObject *object, } pi = g_malloc0 (sizeof (PropertyInfo)); - pi->func = tmp->func ? tmp->func : demarshal_generic; + pi->func = tmp->func ?: demarshal_generic; pi->object_type = tmp->object_type; pi->field = tmp->field; pi->signal_prefix = tmp->signal_prefix; diff --git a/libnm/nm-remote-settings.c b/libnm/nm-remote-settings.c index 089a063370..f2b26215c9 100644 --- a/libnm/nm-remote-settings.c +++ b/libnm/nm-remote-settings.c @@ -538,7 +538,7 @@ nm_remote_settings_save_hostname (NMRemoteSettings *settings, priv = NM_REMOTE_SETTINGS_GET_PRIVATE (settings); ret = nmdbus_settings_call_save_hostname_sync (priv->proxy, - hostname ? hostname : "", + hostname ?: "", cancellable, error); if (error && *error) g_dbus_error_strip_remote_error (*error); @@ -583,7 +583,7 @@ nm_remote_settings_save_hostname_async (NMRemoteSettings *settings, g_simple_async_result_set_check_cancellable (simple, cancellable); nmdbus_settings_call_save_hostname (priv->proxy, - hostname ? hostname : "", + hostname ?: "", cancellable, save_hostname_cb, simple); } diff --git a/libnm/nm-secret-agent-old.c b/libnm/nm-secret-agent-old.c index 7491451262..1b9e8915bb 100644 --- a/libnm/nm-secret-agent-old.c +++ b/libnm/nm-secret-agent-old.c @@ -224,7 +224,7 @@ verify_sender (NMSecretAgentOld *self, NM_SECRET_AGENT_ERROR, NM_SECRET_AGENT_ERROR_PERMISSION_DENIED, "Failed to request unix user: (%s) %s.", - remote_error ? remote_error : "", + remote_error ?: "", local->message); g_free (remote_error); g_error_free (local); diff --git a/shared/nm-utils/nm-test-utils.h b/shared/nm-utils/nm-test-utils.h index 244e0ac2cb..6dd337b201 100644 --- a/shared/nm-utils/nm-test-utils.h +++ b/shared/nm-utils/nm-test-utils.h @@ -1138,7 +1138,7 @@ _nmtst_assert_ip4_address (const char *file, int line, in_addr_t addr, const cha char buf[100]; g_error ("%s:%d: Unexpected IPv4 address: expected %s, got %s", - file, line, str_expected ? str_expected : "0.0.0.0", + file, line, str_expected ?: "0.0.0.0", inet_ntop (AF_INET, &addr, buf, sizeof (buf))); } } @@ -1156,7 +1156,7 @@ _nmtst_assert_ip6_address (const char *file, int line, const struct in6_addr *ad char buf[100]; g_error ("%s:%d: Unexpected IPv6 address: expected %s, got %s", - file, line, str_expected ? str_expected : "::", + file, line, str_expected ?: "::", inet_ntop (AF_INET6, addr, buf, sizeof (buf))); } } diff --git a/src/devices/adsl/nm-atm-manager.c b/src/devices/adsl/nm-atm-manager.c index 32c4c386dc..0ff4603df5 100644 --- a/src/devices/adsl/nm-atm-manager.c +++ b/src/devices/adsl/nm-atm-manager.c @@ -236,7 +236,7 @@ handle_uevent (NMUdevClient *client, ifindex = udev_device_get_property_value (device, "IFINDEX"); seqnum = udev_device_get_seqnum (device); nm_log_dbg (LOGD_PLATFORM, "UDEV event: action '%s' subsys '%s' device '%s' (%s); seqnum=%" G_GUINT64_FORMAT, - action, subsys, udev_device_get_sysname (device), ifindex ? ifindex : "unknown", seqnum); + action, subsys, udev_device_get_sysname (device), ifindex ?: "unknown", seqnum); if (!strcmp (action, "add")) adsl_add (self, device); diff --git a/src/devices/adsl/nm-device-adsl.c b/src/devices/adsl/nm-device-adsl.c index 9133137682..5fcc823a20 100644 --- a/src/devices/adsl/nm-device-adsl.c +++ b/src/devices/adsl/nm-device-adsl.c @@ -194,7 +194,7 @@ br2684_assign_vcc (NMDeviceAdsl *self, NMSettingAdsl *s_adsl) _LOGD (LOGD_ADSL, "assigning address %d.%d.%d encapsulation %s", priv->atm_index, addr.sap_addr.vpi, addr.sap_addr.vci, - encapsulation ? encapsulation : "(none)"); + encapsulation ?: "(none)"); err = connect (priv->brfd, (struct sockaddr*) &addr, sizeof (addr)); if (err != 0) { diff --git a/src/devices/bluetooth/nm-bluez-device.c b/src/devices/bluetooth/nm-bluez-device.c index cc9e38c89b..d8e40d6f38 100644 --- a/src/devices/bluetooth/nm-bluez-device.c +++ b/src/devices/bluetooth/nm-bluez-device.c @@ -494,7 +494,7 @@ nm_bluez_device_disconnect (NMBluezDevice *self) priv->path, dbus_iface, "Disconnect", - args ? args : g_variant_new ("()"), + args ?: g_variant_new("()"), NULL, G_DBUS_CALL_FLAGS_NONE, 10000, diff --git a/src/devices/nm-device-bond.c b/src/devices/nm-device-bond.c index 2dd9494a8b..22f7cdde35 100644 --- a/src/devices/nm-device-bond.c +++ b/src/devices/nm-device-bond.c @@ -314,7 +314,7 @@ apply_bonding_config (NMDevice *device) /* Primary */ value = nm_setting_bond_get_option_by_name (s_bond, NM_SETTING_BOND_OPTION_PRIMARY); - set_bond_attr (device, mode, NM_SETTING_BOND_OPTION_PRIMARY, value ? value : ""); + set_bond_attr (device, mode, NM_SETTING_BOND_OPTION_PRIMARY, value ?: ""); /* ARP targets: clear and initialize the list */ contents = nm_platform_sysctl_master_get_option (nm_device_get_platform (device), ifindex, @@ -591,7 +591,7 @@ reapply_connection (NMDevice *device, NMConnection *con_old, NMConnection *con_n /* Primary */ value = nm_setting_bond_get_option_by_name (s_bond, NM_SETTING_BOND_OPTION_PRIMARY); - set_bond_attr (device, mode, NM_SETTING_BOND_OPTION_PRIMARY, value ? value : ""); + set_bond_attr (device, mode, NM_SETTING_BOND_OPTION_PRIMARY, value ?: ""); /* Active slave */ set_simple_option (device, mode, s_bond, NM_SETTING_BOND_OPTION_ACTIVE_SLAVE); diff --git a/src/devices/nm-device.c b/src/devices/nm-device.c index ed63adf548..3980758631 100644 --- a/src/devices/nm-device.c +++ b/src/devices/nm-device.c @@ -1357,7 +1357,7 @@ nm_device_get_ip_iface (NMDevice *self) priv = NM_DEVICE_GET_PRIVATE (self); /* If it's not set, default to iface */ - return priv->ip_iface ? priv->ip_iface : priv->iface; + return priv->ip_iface ?: priv->iface; } int diff --git a/src/devices/ovs/nm-ovsdb.c b/src/devices/ovs/nm-ovsdb.c index 12e7c1f15e..a9cb57034a 100644 --- a/src/devices/ovs/nm-ovsdb.c +++ b/src/devices/ovs/nm-ovsdb.c @@ -363,7 +363,7 @@ _insert_interface (json_t *params, NMConnection *interface) json_pack ("{s:s, s:s, s:{s:s, s:s, s:o, s:[s, [[s, s]]]}, s:s}", "op", "insert", "table", "Interface", "row", "name", nm_connection_get_interface_name (interface), - "type", type ? type : "", + "type", type ?: "", "options", options, "external_ids", "map", "NM.connection.uuid", nm_connection_get_uuid (interface), "uuid-name", "rowInterface")); @@ -891,7 +891,7 @@ ovsdb_got_update (NMOvsdb *self, json_t *msg) _LOGT ("removed an '%s' interface: %s%s%s", ovs_interface->type, ovs_interface->name, ovs_interface->connection_uuid ? ", " : "", - ovs_interface->connection_uuid ? ovs_interface->connection_uuid : ""); + ovs_interface->connection_uuid ?: ""); if (g_strcmp0 (ovs_interface->type, "internal") == 0) { /* Currently the factory only creates NMDevices for * internal interfaces. Ignore the rest. */ @@ -910,14 +910,14 @@ ovsdb_got_update (NMOvsdb *self, json_t *msg) if (old) { _LOGT ("changed an '%s' interface: %s%s%s", type, ovs_interface->name, ovs_interface->connection_uuid ? ", " : "", - ovs_interface->connection_uuid ? ovs_interface->connection_uuid : ""); + ovs_interface->connection_uuid ?: ""); g_signal_emit (self, signals[DEVICE_CHANGED], 0, "ovs-interface", ovs_interface->name); } else { _LOGT ("added an '%s' interface: %s%s%s", ovs_interface->type, ovs_interface->name, ovs_interface->connection_uuid ? ", " : "", - ovs_interface->connection_uuid ? ovs_interface->connection_uuid : ""); + ovs_interface->connection_uuid ?: ""); if (g_strcmp0 (ovs_interface->type, "internal") == 0) { /* Currently the factory only creates NMDevices for * internal interfaces. Ignore the rest. */ @@ -949,7 +949,7 @@ ovsdb_got_update (NMOvsdb *self, json_t *msg) old = FALSE; _LOGT ("removed a port: %s%s%s", ovs_port->name, ovs_port->connection_uuid ? ", " : "", - ovs_port->connection_uuid ? ovs_port->connection_uuid : ""); + ovs_port->connection_uuid ?: ""); g_signal_emit (self, signals[DEVICE_REMOVED], 0, ovs_port->name, NM_DEVICE_TYPE_OVS_PORT); } @@ -965,13 +965,13 @@ ovsdb_got_update (NMOvsdb *self, json_t *msg) if (old) { _LOGT ("changed a port: %s%s%s", ovs_port->name, ovs_port->connection_uuid ? ", " : "", - ovs_port->connection_uuid ? ovs_port->connection_uuid : ""); + ovs_port->connection_uuid ?: ""); g_signal_emit (self, signals[DEVICE_CHANGED], 0, NM_SETTING_OVS_PORT_SETTING_NAME, ovs_port->name); } else { _LOGT ("added a port: %s%s%s", ovs_port->name, ovs_port->connection_uuid ? ", " : "", - ovs_port->connection_uuid ? ovs_port->connection_uuid : ""); + ovs_port->connection_uuid ?: ""); g_signal_emit (self, signals[DEVICE_ADDED], 0, ovs_port->name, NM_DEVICE_TYPE_OVS_PORT); } @@ -999,7 +999,7 @@ ovsdb_got_update (NMOvsdb *self, json_t *msg) old = FALSE; _LOGT ("removed a bridge: %s%s%s", ovs_bridge->name, ovs_bridge->connection_uuid ? ", " : "", - ovs_bridge->connection_uuid ? ovs_bridge->connection_uuid : ""); + ovs_bridge->connection_uuid ?: ""); g_signal_emit (self, signals[DEVICE_REMOVED], 0, ovs_bridge->name, NM_DEVICE_TYPE_OVS_BRIDGE); } @@ -1015,13 +1015,13 @@ ovsdb_got_update (NMOvsdb *self, json_t *msg) if (old) { _LOGT ("changed a bridge: %s%s%s", ovs_bridge->name, ovs_bridge->connection_uuid ? ", " : "", - ovs_bridge->connection_uuid ? ovs_bridge->connection_uuid : ""); + ovs_bridge->connection_uuid ?: ""); g_signal_emit (self, signals[DEVICE_CHANGED], 0, NM_SETTING_OVS_BRIDGE_SETTING_NAME, ovs_bridge->name); } else { _LOGT ("added a bridge: %s%s%s", ovs_bridge->name, ovs_bridge->connection_uuid ? ", " : "", - ovs_bridge->connection_uuid ? ovs_bridge->connection_uuid : ""); + ovs_bridge->connection_uuid ?: ""); g_signal_emit (self, signals[DEVICE_ADDED], 0, ovs_bridge->name, NM_DEVICE_TYPE_OVS_BRIDGE); } diff --git a/src/devices/wifi/nm-device-wifi.c b/src/devices/wifi/nm-device-wifi.c index 1590f16625..90f9455742 100644 --- a/src/devices/wifi/nm-device-wifi.c +++ b/src/devices/wifi/nm-device-wifi.c @@ -2147,9 +2147,9 @@ supplicant_iface_notify_current_bss (NMSupplicantInterface *iface, } _LOGD (LOGD_WIFI, "roamed from BSSID %s (%s) to %s (%s)", - old_bssid ? old_bssid : "(none)", + old_bssid ?: "(none)", old_ssid ? nm_utils_escape_ssid (old_ssid->data, old_ssid->len) : "(none)", - new_bssid ? new_bssid : "(none)", + new_bssid ?: "(none)", new_ssid ? nm_utils_escape_ssid (new_ssid->data, new_ssid->len) : "(none)"); set_current_ap (self, new_ap, TRUE); diff --git a/src/devices/wwan/nm-modem-broadband.c b/src/devices/wwan/nm-modem-broadband.c index a58eb78758..e5678b9635 100644 --- a/src/devices/wwan/nm-modem-broadband.c +++ b/src/devices/wwan/nm-modem-broadband.c @@ -286,7 +286,7 @@ create_gsm_connect_properties (NMConnection *connection) /* Blank APN ("") means the default subscription APN */ str = nm_setting_gsm_get_apn (setting); - mm_simple_connect_properties_set_apn (properties, str ? str : ""); + mm_simple_connect_properties_set_apn (properties, str ?: ""); str = nm_setting_gsm_get_network_id (setting); if (str) diff --git a/src/devices/wwan/nm-modem.c b/src/devices/wwan/nm-modem.c index 61b7247ec8..80336d7209 100644 --- a/src/devices/wwan/nm-modem.c +++ b/src/devices/wwan/nm-modem.c @@ -205,7 +205,7 @@ nm_modem_set_state (NMModem *self, _LOGI ("modem state changed, '%s' --> '%s' (reason: %s)", nm_modem_state_to_string (old_state), nm_modem_state_to_string (new_state), - reason ? reason : "none"); + reason ?: "none"); priv->state = new_state; _notify (self, PROP_STATE); diff --git a/src/dhcp/nm-dhcp-client.c b/src/dhcp/nm-dhcp-client.c index 96c0265388..f407da89ae 100644 --- a/src/dhcp/nm-dhcp-client.c +++ b/src/dhcp/nm-dhcp-client.c @@ -334,7 +334,7 @@ nm_dhcp_client_stop_pid (pid_t pid, const char *iface) g_return_if_fail (pid > 1); - nm_utils_kill_child_sync (pid, SIGTERM, LOGD_DHCP, name ? name : "dhcp-client", NULL, + nm_utils_kill_child_sync (pid, SIGTERM, LOGD_DHCP, name ?: "dhcp-client", NULL, 1000 / 2, 1000 / 20); g_free (name); } diff --git a/src/dhcp/nm-dhcp-listener.c b/src/dhcp/nm-dhcp-listener.c index d7d38e5463..1770ead34f 100644 --- a/src/dhcp/nm-dhcp-listener.c +++ b/src/dhcp/nm-dhcp-listener.c @@ -160,7 +160,7 @@ _method_call_handle (NMDhcpListener *self, pid_str = get_option (options, "pid"); pid = _nm_utils_ascii_str_to_int64 (pid_str, 10, 0, G_MAXINT32, -1); if (pid == -1) { - _LOGW ("dhcp-event: couldn't convert PID '%s' to an integer", pid_str ? pid_str : "(null)"); + _LOGW ("dhcp-event: couldn't convert PID '%s' to an integer", pid_str ?: "(null)"); return; } diff --git a/src/dhcp/nm-dhcp-manager.c b/src/dhcp/nm-dhcp-manager.c index bf22872d4e..aa40e8037d 100644 --- a/src/dhcp/nm-dhcp-manager.c +++ b/src/dhcp/nm-dhcp-manager.c @@ -311,7 +311,7 @@ nm_dhcp_manager_start_ip6 (NMDhcpManager *self, if (send_hostname) { /* Always prefer the explicit dhcp-hostname if given */ - hostname = dhcp_hostname ? dhcp_hostname : priv->default_hostname; + hostname = dhcp_hostname ?: priv->default_hostname; } return client_start (self, AF_INET6, multi_idx, iface, ifindex, hwaddr, uuid, route_table, route_metric, ll_addr, diff --git a/src/main.c b/src/main.c index 714d7bcf46..c1267b275d 100644 --- a/src/main.c +++ b/src/main.c @@ -214,7 +214,7 @@ do_early_setup (int *argc, char **argv[], NMConfigCmdLineOptions *config_cli) _("NetworkManager monitors all network connections and automatically\nchooses the best connection to use. It also allows the user to\nspecify wireless access points which wireless cards in the computer\nshould associate with."))) exit (1); - global_opt.pidfile = global_opt.pidfile ? global_opt.pidfile : g_strdup (NM_DEFAULT_PID_FILE); + global_opt.pidfile = global_opt.pidfile ?: g_strdup(NM_DEFAULT_PID_FILE); } /* diff --git a/src/nm-config.c b/src/nm-config.c index bef05fc78c..d4682ed6da 100644 --- a/src/nm-config.c +++ b/src/nm-config.c @@ -579,7 +579,7 @@ ignore_config_snippet (GKeyFile *keyfile, gboolean is_base_config) const char *e; e = g_getenv ("NM_CONFIG_ENABLE_TAG"); - _nm_config_match_env = g_strdup (e ? e : ""); + _nm_config_match_env = g_strdup (e ?: ""); } /* second, interpret the value as match-spec. */ @@ -2407,7 +2407,7 @@ _set_config_data (NMConfig *self, NMConfigData *new_data, NMConfigChangeFlags re else _LOGI ("signal: %s", nm_config_change_flags_to_string (changes, NULL, 0)); g_signal_emit (self, signals[SIGNAL_CONFIG_CHANGED], 0, - new_data ? new_data : old_data, + new_data ?: old_data, changes, old_data); if (new_data) g_object_unref (old_data); diff --git a/src/nm-core-utils.c b/src/nm-core-utils.c index 548101e7b6..0e3f5cc919 100644 --- a/src/nm-core-utils.c +++ b/src/nm-core-utils.c @@ -2044,8 +2044,8 @@ _log_connection_sort_hashes_fcn (gconstpointer a, gconstpointer b) NMSettingPriority p1, p2; NMSetting *s1, *s2; - s1 = v1->setting ? v1->setting : v1->diff_base_setting; - s2 = v2->setting ? v2->setting : v2->diff_base_setting; + s1 = v1->setting ?: v1->diff_base_setting; + s2 = v2->setting ?: v2->diff_base_setting; g_assert (s1 && s2); @@ -2279,13 +2279,13 @@ nm_utils_log_connection_diff (NMConnection *connection, g_string_printf (str1, "%s.%s", setting_data->name, item->item_name); switch (item->diff_result & (NM_SETTING_DIFF_RESULT_IN_A | NM_SETTING_DIFF_RESULT_IN_B)) { case NM_SETTING_DIFF_RESULT_IN_B: - nm_log (level, domain, NULL, NULL, "%s%"_NM_LOG_ALIGN"s < %s", prefix, str1->str, str_diff ? str_diff : "NULL"); + nm_log (level, domain, NULL, NULL, "%s%"_NM_LOG_ALIGN"s < %s", prefix, str1->str, str_diff ?: "NULL"); break; case NM_SETTING_DIFF_RESULT_IN_A: - nm_log (level, domain, NULL, NULL, "%s%"_NM_LOG_ALIGN"s = %s", prefix, str1->str, str_conn ? str_conn : "NULL"); + nm_log (level, domain, NULL, NULL, "%s%"_NM_LOG_ALIGN"s = %s", prefix, str1->str, str_conn ?: "NULL"); break; default: - nm_log (level, domain, NULL, NULL, "%s%"_NM_LOG_ALIGN"s = %s < %s", prefix, str1->str, str_conn ? str_conn : "NULL", str_diff ? str_diff : "NULL"); + nm_log (level, domain, NULL, NULL, "%s%"_NM_LOG_ALIGN"s = %s < %s", prefix, str1->str, str_conn ?: "NULL", str_diff ?: "NULL"); break; #undef _NM_LOG_ALIGN } diff --git a/src/nm-core-utils.h b/src/nm-core-utils.h index 488eb827d2..a9a70038f9 100644 --- a/src/nm-core-utils.h +++ b/src/nm-core-utils.h @@ -158,7 +158,7 @@ double nm_utils_exp10 (gint16 e); static inline guint32 nm_utils_ip6_route_metric_normalize (guint32 metric) { - return metric ? metric : 1024 /*NM_PLATFORM_ROUTE_METRIC_DEFAULT_IP6*/; + return metric ?: 1024 /*NM_PLATFORM_ROUTE_METRIC_DEFAULT_IP6*/; } static inline guint32 diff --git a/src/nm-dispatcher.c b/src/nm-dispatcher.c index 235134c872..368b781ad1 100644 --- a/src/nm-dispatcher.c +++ b/src/nm-dispatcher.c @@ -547,7 +547,7 @@ _dispatcher_call (NMDispatcherAction action, _LOGD ("(%u) (%s) dispatching action '%s'%s", reqid, - vpn_iface ? vpn_iface : nm_device_get_iface (device), + vpn_iface ?: nm_device_get_iface(device), action_to_string (action), blocking ? " (blocking)" @@ -650,7 +650,7 @@ _dispatcher_call (NMDispatcherAction action, device_dhcp4_props, device_dhcp6_props, connectivity_state_string, - vpn_iface ? vpn_iface : "", + vpn_iface ?: "", &vpn_proxy_props, &vpn_ip4_props, &vpn_ip6_props, @@ -688,7 +688,7 @@ _dispatcher_call (NMDispatcherAction action, device_dhcp4_props, device_dhcp6_props, connectivity_state_string, - vpn_iface ? vpn_iface : "", + vpn_iface ?: "", &vpn_proxy_props, &vpn_ip4_props, &vpn_ip6_props, diff --git a/src/nm-firewall-manager.c b/src/nm-firewall-manager.c index 134a46f7ec..5b5e7cfa20 100644 --- a/src/nm-firewall-manager.c +++ b/src/nm-firewall-manager.c @@ -174,7 +174,7 @@ _cb_info_create (NMFirewallManager *self, if (priv->running || priv->proxy_cancellable) { info->mode_mutable = CB_INFO_MODE_DBUS_WAITING; - info->dbus.arg = g_variant_new ("(ss)", zone ? zone : "", iface); + info->dbus.arg = g_variant_new ("(ss)", zone ?: "", iface); } else info->mode_mutable = CB_INFO_MODE_IDLE; diff --git a/src/nm-manager.c b/src/nm-manager.c index 8c9eb890b9..9f0891a75d 100644 --- a/src/nm-manager.c +++ b/src/nm-manager.c @@ -6977,7 +6977,7 @@ get_property (GObject *object, guint prop_id, if (con) type = nm_connection_get_connection_type (con); } - g_value_set_string (value, type ? type : ""); + g_value_set_string (value, type ?: ""); break; case PROP_ACTIVATING_CONNECTION: nm_dbus_utils_g_value_set_object_path (value, priv->activating_connection); diff --git a/src/nm-policy.c b/src/nm-policy.c index 55b6caf601..61cc001f0b 100644 --- a/src/nm-policy.c +++ b/src/nm-policy.c @@ -2119,7 +2119,7 @@ vpn_connection_retry_after_failure (NMVpnConnection *vpn, NMPolicy *self) &error)) { _LOGW (LOGD_DEVICE, "VPN '%s' reconnect failed: %s", nm_settings_connection_get_id (connection), - error->message ? error->message : "unknown"); + error->message ?: "unknown"); g_clear_error (&error); } } diff --git a/src/nm-rfkill-manager.c b/src/nm-rfkill-manager.c index e655e67cc3..b2909e9d3e 100644 --- a/src/nm-rfkill-manager.c +++ b/src/nm-rfkill-manager.c @@ -296,7 +296,7 @@ add_one_killswitch (NMRfkillManager *self, struct udev_device *device) rfkill_type_to_desc (rtype), ks->path, ks->platform ? "platform " : "", - ks->driver ? ks->driver : ""); + ks->driver ?: ""); } static void diff --git a/src/platform/nm-linux-platform.c b/src/platform/nm-linux-platform.c index c6413d9676..3a671acf74 100644 --- a/src/platform/nm-linux-platform.c +++ b/src/platform/nm-linux-platform.c @@ -6980,7 +6980,7 @@ handle_udev_event (NMUdevClient *udev_client, seqnum = udev_device_get_seqnum (udevice); _LOGD ("UDEV event: action '%s' subsys '%s' device '%s' (%s); seqnum=%" G_GUINT64_FORMAT, action, subsys, udev_device_get_sysname (udevice), - ifindex ? ifindex : "unknown", seqnum); + ifindex ?: "unknown", seqnum); if (NM_IN_STRSET (action, "add", "move")) udev_device_added (platform, udevice); diff --git a/src/platform/nm-platform.c b/src/platform/nm-platform.c index c71bda4d03..727e064bd5 100644 --- a/src/platform/nm-platform.c +++ b/src/platform/nm-platform.c @@ -4882,11 +4882,11 @@ nm_platform_link_to_string (const NMPlatformLink *link, char *buf, gsize len) link->inet6_addr_gen_mode_inv ? " addrgenmode " : "", link->inet6_addr_gen_mode_inv ? nm_platform_link_inet6_addrgenmode2str (_nm_platform_uint8_inv (link->inet6_addr_gen_mode_inv), str_addrmode, sizeof (str_addrmode)) : "", str_addr ? " addr " : "", - str_addr ? str_addr : "", + str_addr ?: "", link->inet6_token.id ? " inet6token " : "", link->inet6_token.id ? nm_utils_inet6_interface_identifier_to_token (link->inet6_token, str_inet6_token) : "", link->driver ? " driver " : "", - link->driver ? link->driver : "", + link->driver ?: "", link->rx_packets, link->rx_bytes, link->tx_packets, link->tx_bytes); g_string_free (str_flags, TRUE); @@ -5298,7 +5298,7 @@ nm_platform_ip4_address_to_string (const NMPlatformIP4Address *address, char *bu str_label[0] = 0; str_lft_p = _lifetime_to_string (address->timestamp, - address->lifetime ? address->lifetime : NM_PLATFORM_LIFETIME_PERMANENT, + address->lifetime ?: NM_PLATFORM_LIFETIME_PERMANENT, now, str_lft, sizeof (str_lft)), str_pref_p = (address->lifetime == address->preferred) ? str_lft_p @@ -5310,7 +5310,7 @@ nm_platform_ip4_address_to_string (const NMPlatformIP4Address *address, char *bu g_snprintf (buf, len, "%s/%d lft %s pref %s%s%s%s%s%s src %s", s_address, address->plen, str_lft_p, str_pref_p, str_time_p, - str_peer ? str_peer : "", + str_peer ?: "", str_dev, _to_string_ifa_flags (address->n_ifa_flags, s_flags, sizeof (s_flags)), str_label, @@ -5405,7 +5405,7 @@ nm_platform_ip6_address_to_string (const NMPlatformIP6Address *address, char *bu _to_string_dev (NULL, address->ifindex, str_dev, sizeof (str_dev)); str_lft_p = _lifetime_to_string (address->timestamp, - address->lifetime ? address->lifetime : NM_PLATFORM_LIFETIME_PERMANENT, + address->lifetime ?: NM_PLATFORM_LIFETIME_PERMANENT, now, str_lft, sizeof (str_lft)), str_pref_p = (address->lifetime == address->preferred) ? str_lft_p @@ -5417,7 +5417,7 @@ nm_platform_ip6_address_to_string (const NMPlatformIP6Address *address, char *bu g_snprintf (buf, len, "%s/%d lft %s pref %s%s%s%s%s src %s", s_address, address->plen, str_lft_p, str_pref_p, str_time_p, - str_peer ? str_peer : "", + str_peer ?: "", str_dev, _to_string_ifa_flags (address->n_ifa_flags, s_flags, sizeof (s_flags)), nmp_utils_ip_config_source_to_string (address->addr_source, s_source, sizeof (s_source))); diff --git a/src/platform/tests/test-common.c b/src/platform/tests/test-common.c index 7885c083b6..42569d5b17 100644 --- a/src/platform/tests/test-common.c +++ b/src/platform/tests/test-common.c @@ -28,7 +28,7 @@ #include "test-common.h" #define SIGNAL_DATA_FMT "'%s-%s' ifindex %d%s%s%s (%d times received)" -#define SIGNAL_DATA_ARG(data) (data)->name, nm_platform_signal_change_type_to_string ((data)->change_type), (data)->ifindex, (data)->ifname ? " ifname '" : "", (data)->ifname ? (data)->ifname : "", (data)->ifname ? "'" : "", (data)->received_count +#define SIGNAL_DATA_ARG(data) (data)->name, nm_platform_signal_change_type_to_string ((data)->change_type), (data)->ifindex, (data)->ifname ? " ifname '" : "", (data)->ifname ?: "", (data)->ifname ? "'" : "", (data)->received_count int NMTSTP_ENV1_IFINDEX = -1; int NMTSTP_ENV1_EX = -1; @@ -1258,7 +1258,7 @@ nmtstp_link_gre_add (NMPlatform *platform, success = !nmtstp_run_command ("ip tunnel add %s mode gre %s local %s remote %s ttl %u tos %02x %s", name, - dev ? dev : "", + dev ?: "", nm_utils_inet4_ntop (lnk->local, NULL), nm_utils_inet4_ntop (lnk->remote, buffer), lnk->ttl, @@ -1568,7 +1568,7 @@ nmtstp_link_vxlan_add (NMPlatform *platform, err = nmtstp_run_command ("ip link add %s type vxlan id %u %s local %s group %s ttl %u tos %02x dstport %u srcport %u %u ageing %u", name, lnk->id, - dev ? dev : "", + dev ?: "", local, remote, lnk->ttl, diff --git a/src/settings/nm-settings-connection.c b/src/settings/nm-settings-connection.c index abc7c524b8..6526abc76f 100644 --- a/src/settings/nm-settings-connection.c +++ b/src/settings/nm-settings-connection.c @@ -135,7 +135,7 @@ G_DEFINE_TYPE_WITH_CODE (NMSettingsConnection, nm_settings_connection, NM_TYPE_D const char *__uuid = (self) ? nm_settings_connection_get_uuid (self) : NULL; \ \ if (self) { \ - g_snprintf (__prefix, sizeof (__prefix), "%s[%p%s%s]", _NMLOG_PREFIX_NAME, self, __uuid ? "," : "", __uuid ? __uuid : ""); \ + g_snprintf (__prefix, sizeof (__prefix), "%s[%p%s%s]", _NMLOG_PREFIX_NAME, self, __uuid ? "," : "", __uuid ?: ""); \ __p_prefix = __prefix; \ } \ _nm_log (__level, _NMLOG_DOMAIN, 0, NULL, __uuid, \ @@ -463,7 +463,7 @@ update_agent_secrets_cache (NMSettingsConnection *self, NMConnection *new) if (priv->agent_secrets) g_object_unref (priv->agent_secrets); - priv->agent_secrets = nm_simple_connection_new_clone (new ? new : NM_CONNECTION (self)); + priv->agent_secrets = nm_simple_connection_new_clone (new ?: NM_CONNECTION(self)); /* Clear out non-system-owned secrets */ nm_connection_clear_secrets_with_flags (priv->agent_secrets, @@ -1883,7 +1883,7 @@ settings_connection_update (NMSettingsConnection *self, * that's sending the update request. You can't make a connection * invisible to yourself. */ - if (!nm_auth_is_subject_in_acl_set_error (tmp ? tmp : NM_CONNECTION (self), + if (!nm_auth_is_subject_in_acl_set_error (tmp ?: NM_CONNECTION(self), subject, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_PERMISSION_DENIED, @@ -1899,7 +1899,7 @@ settings_connection_update (NMSettingsConnection *self, info->new_settings = tmp; permission = get_update_modify_permission (NM_CONNECTION (self), - tmp ? tmp : NM_CONNECTION (self)); + tmp ?: NM_CONNECTION(self)); auth_start (self, context, subject, permission, update_auth_cb, info); return; diff --git a/src/settings/plugins/ibft/nms-ibft-reader.c b/src/settings/plugins/ibft/nms-ibft-reader.c index d26c833473..cf849e2817 100644 --- a/src/settings/plugins/ibft/nms-ibft-reader.c +++ b/src/settings/plugins/ibft/nms-ibft-reader.c @@ -128,7 +128,7 @@ nms_ibft_reader_load_blocks (const char *iscsiadm_path, } g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_FAILED, "iBFT: %s exited with error %d. Message: '%s'", - iscsiadm_path, WEXITSTATUS (status), err ? err : "(none)"); + iscsiadm_path, WEXITSTATUS (status), err ?: "(none)"); goto done; } @@ -392,15 +392,15 @@ connection_setting_add (const GPtrArray *block, id = g_strdup_printf ("iBFT%s%s %s", prefix ? " " : "", - prefix ? prefix : "", + prefix ?: "", iface); uuid = _nm_utils_uuid_generate_from_strings ("ibft", s_hwaddr, s_vlanid ? "V" : "v", - s_vlanid ? s_vlanid : "", + s_vlanid ?: "", s_ip4addr ? "A" : "DHCP", - s_ip4addr ? s_ip4addr : "", + s_ip4addr ?: "", NULL); s_con = nm_setting_connection_new (); diff --git a/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-plugin.c b/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-plugin.c index 8f42bd7e91..c1e00981af 100644 --- a/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-plugin.c +++ b/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-plugin.c @@ -418,7 +418,7 @@ ifcfg_dir_changed (GFileMonitor *monitor, path = g_file_get_path (file); ifcfg_path = utils_detect_ifcfg_path (path, FALSE); - _LOGD ("ifcfg_dir_changed(%s) = %d // %s", path, event_type, ifcfg_path ? ifcfg_path : "(none)"); + _LOGD ("ifcfg_dir_changed(%s) = %d // %s", path, event_type, ifcfg_path ?: "(none)"); if (ifcfg_path) { connection = find_by_path (plugin, ifcfg_path); switch (event_type) { diff --git a/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-reader.c b/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-reader.c index 11474c3da7..722d4e366d 100644 --- a/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-reader.c +++ b/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-reader.c @@ -386,7 +386,7 @@ is_any_ip4_address_defined (shvarFile *ifcfg, int *idx) { int i, ignore, *ret_idx; - ret_idx = idx ? idx : &ignore; + ret_idx = idx ?: &ignore; for (i = -1; i <= 2; i++) { gs_free char *value = NULL; @@ -1847,8 +1847,8 @@ make_ip6_setting (shvarFile *ifcfg, ipv6addr_secondaries = svGetValueStr_cp (ifcfg, "IPV6ADDR_SECONDARIES"); value = g_strjoin (ipv6addr && ipv6addr_secondaries ? " " : NULL, - ipv6addr ? ipv6addr : "", - ipv6addr_secondaries ? ipv6addr_secondaries : "", + ipv6addr ?: "", + ipv6addr_secondaries ?: "", NULL); g_free (ipv6addr); g_free (ipv6addr_secondaries); @@ -4524,9 +4524,9 @@ handle_bond_option (NMSettingBond *s_bond, } } - if (!nm_setting_bond_add_option (s_bond, key, sanitized ? sanitized : value)) + if (!nm_setting_bond_add_option (s_bond, key, sanitized ?: value)) PARSE_WARNING ("invalid bonding option '%s' = %s", - key, sanitized ? sanitized : value); + key, sanitized ?: value); g_free (sanitized); } diff --git a/src/settings/plugins/ifcfg-rh/tests/test-ifcfg-rh.c b/src/settings/plugins/ifcfg-rh/tests/test-ifcfg-rh.c index 4fc0df0313..367de7ccc0 100644 --- a/src/settings/plugins/ifcfg-rh/tests/test-ifcfg-rh.c +++ b/src/settings/plugins/ifcfg-rh/tests/test-ifcfg-rh.c @@ -4590,7 +4590,7 @@ test_write_wired_static_ip6_only_gw (gconstpointer user_data) s_con = (NMSettingConnection *) nm_setting_connection_new (); nm_connection_add_setting (connection, NM_SETTING (s_con)); - id = g_strdup_printf ("Test Write Wired Static IP6 Only With Gateway %s", gateway6 ? gateway6 : "NULL"); + id = g_strdup_printf ("Test Write Wired Static IP6 Only With Gateway %s", gateway6 ?: "NULL"); g_object_set (s_con, NM_SETTING_CONNECTION_ID, id, NM_SETTING_CONNECTION_UUID, nm_utils_uuid_generate_a (), diff --git a/src/settings/plugins/ifupdown/nms-ifupdown-parser.c b/src/settings/plugins/ifupdown/nms-ifupdown-parser.c index 37bd64273e..884c6591a9 100644 --- a/src/settings/plugins/ifupdown/nms-ifupdown-parser.c +++ b/src/settings/plugins/ifupdown/nms-ifupdown-parser.c @@ -338,7 +338,7 @@ update_wireless_security_setting_from_if_block(NMConnection *connection, } g_object_set(wireless_security_setting, - newkey, typed_property_value ? typed_property_value : property_value, + newkey, typed_property_value ?: property_value, NULL); security = TRUE; @@ -386,7 +386,7 @@ update_wireless_security_setting_from_if_block(NMConnection *connection, } g_object_set(wireless_security_setting, - newkey, typed_property_value ? typed_property_value : property_value, + newkey, typed_property_value ?: property_value, NULL); security = TRUE; diff --git a/src/supplicant/nm-supplicant-config.c b/src/supplicant/nm-supplicant-config.c index d8cf28c362..80db5baa1c 100644 --- a/src/supplicant/nm-supplicant-config.c +++ b/src/supplicant/nm-supplicant-config.c @@ -144,7 +144,7 @@ nm_supplicant_config_add_option_with_type (NMSupplicantConfig *self, memset (&buf[0], 0, sizeof (buf)); memcpy (&buf[0], value, len > 254 ? 254 : len); g_set_error (error, NM_SUPPLICANT_ERROR, NM_SUPPLICANT_ERROR_CONFIG, - "key '%s' and/or value '%s' invalid", key, hidden ? hidden : buf); + "key '%s' and/or value '%s' invalid", key, hidden ?: buf); return FALSE; } } @@ -168,7 +168,7 @@ nm_supplicant_config_add_option_with_type (NMSupplicantConfig *self, char buf[255]; memset (&buf[0], 0, sizeof (buf)); memcpy (&buf[0], opt->value, opt->len > 254 ? 254 : opt->len); - nm_log_info (LOGD_SUPPLICANT, "Config: added '%s' value '%s'", key, hidden ? hidden : &buf[0]); + nm_log_info (LOGD_SUPPLICANT, "Config: added '%s' value '%s'", key, hidden ?: &buf[0]); } g_hash_table_insert (priv->config, g_strdup (key), opt); @@ -979,7 +979,7 @@ add_pkcs11_uri_with_pin (NMSupplicantConfig *self, tmp = g_strdup_printf ("%s%s%s", split[0], (pin_qattr ? "?" : ""), - (pin_qattr ? pin_qattr : "")); + (pin_qattr ?: "")); tmp_log = g_strdup_printf ("%s%s%s", split[0], (pin_qattr ? "?" : ""), @@ -1171,7 +1171,7 @@ nm_supplicant_config_add_setting_8021x (NMSupplicantConfig *self, /* CA path */ path = nm_setting_802_1x_get_ca_path (setting); - path = ca_path_override ? ca_path_override : path; + path = ca_path_override ?: path; if (path) { if (!add_string_val (self, path, "ca_path", FALSE, NULL, error)) return FALSE; @@ -1179,7 +1179,7 @@ nm_supplicant_config_add_setting_8021x (NMSupplicantConfig *self, /* Phase2 CA path */ path = nm_setting_802_1x_get_phase2_ca_path (setting); - path = ca_path_override ? ca_path_override : path; + path = ca_path_override ?: path; if (path) { if (!add_string_val (self, path, "ca_path2", FALSE, NULL, error)) return FALSE; diff --git a/src/supplicant/nm-supplicant-settings-verify.c b/src/supplicant/nm-supplicant-settings-verify.c index 1db45dc8a0..317afff9d1 100644 --- a/src/supplicant/nm-supplicant-settings-verify.c +++ b/src/supplicant/nm-supplicant-settings-verify.c @@ -187,7 +187,7 @@ validate_type_bytes (const struct Opt * opt, g_return_val_if_fail (opt != NULL, FALSE); g_return_val_if_fail (value != NULL, FALSE); - check_len = opt->int_high ? opt->int_high : 255; + check_len = opt->int_high ?: 255; if (len > check_len) return FALSE; @@ -204,7 +204,7 @@ validate_type_utf8 (const struct Opt *opt, g_return_val_if_fail (opt != NULL, FALSE); g_return_val_if_fail (value != NULL, FALSE); - check_len = opt->int_high ? opt->int_high : 255; + check_len = opt->int_high ?: 255; /* Note that we deliberately don't validate the UTF-8, because some "UTF-8" fields, such as 8021x.password, do not actually have to be valid UTF-8 */ diff --git a/src/vpn/nm-vpn-connection.c b/src/vpn/nm-vpn-connection.c index 7d6be84d79..ad752c4c1f 100644 --- a/src/vpn/nm-vpn-connection.c +++ b/src/vpn/nm-vpn-connection.c @@ -1022,7 +1022,7 @@ print_vpn_config (NMVpnConnection *self) if (nm_ip4_config_get_num_domains (priv->ip4_config) > 0) dns_domain = (char *) nm_ip4_config_get_domain (priv->ip4_config, 0); - _LOGI ("Data: DNS Domain: '%s'", dns_domain ? dns_domain : "(none)"); + _LOGI ("Data: DNS Domain: '%s'", dns_domain ?: "(none)"); } else _LOGI ("Data: No IPv4 configuration"); @@ -1056,7 +1056,7 @@ print_vpn_config (NMVpnConnection *self) if (nm_ip6_config_get_num_domains (priv->ip6_config) > 0) dns_domain = (char *) nm_ip6_config_get_domain (priv->ip6_config, 0); - _LOGI ("Data: DNS Domain: '%s'", dns_domain ? dns_domain : "(none)"); + _LOGI ("Data: DNS Domain: '%s'", dns_domain ?: "(none)"); } else _LOGI ("Data: No IPv6 configuration"); @@ -2821,7 +2821,7 @@ get_property (GObject *object, guint prop_id, g_value_set_uint (value, _state_to_nm_vpn_state (priv->vpn_state)); break; case PROP_BANNER: - g_value_set_string (value, priv->banner ? priv->banner : ""); + g_value_set_string (value, priv->banner ?: ""); break; case PROP_IP4_CONFIG: nm_dbus_utils_g_value_set_object_path (value, ip_config_valid (priv->vpn_state) ? priv->ip4_config : NULL); From bf7529823ece67f5cb4ecc404e262426e545ce67 Mon Sep 17 00:00:00 2001 From: Lubomir Rintel Date: Tue, 24 Apr 2018 13:24:32 +0200 Subject: [PATCH 3/9] cli: do not leave dangling pointers in a global struct Makes Thomas content and happy.. --- clients/cli/nmcli.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clients/cli/nmcli.c b/clients/cli/nmcli.c index 59d30055d9..cec1930c9b 100644 --- a/clients/cli/nmcli.c +++ b/clients/cli/nmcli.c @@ -607,7 +607,7 @@ nmc_cleanup (NmCli *nmc) if (nmc->pwds_hash) g_hash_table_destroy (nmc->pwds_hash); - g_free (nmc->required_fields); + nm_clear_g_free (&nmc->required_fields); if (nmc->pager_pid > 0) { fclose (stdout); From f70abef5c68c0ebd7b67452358373e76354ff82c Mon Sep 17 00:00:00 2001 From: Lubomir Rintel Date: Tue, 24 Apr 2018 11:13:30 +0200 Subject: [PATCH 4/9] cli: drop a useless comment Thomas doesn't like it and who am I to argue with him. --- clients/cli/nmcli.c | 1 - 1 file changed, 1 deletion(-) diff --git a/clients/cli/nmcli.c b/clients/cli/nmcli.c index cec1930c9b..8700fcdf73 100644 --- a/clients/cli/nmcli.c +++ b/clients/cli/nmcli.c @@ -51,7 +51,6 @@ # define NMCLI_VERSION VERSION #endif -/* Global NmCli object */ NmCli nm_cli; /*****************************************************************************/ From 30d67c99eade7ee9eaafe5fdc83be95392ed0c52 Mon Sep 17 00:00:00 2001 From: Lubomir Rintel Date: Wed, 28 Mar 2018 22:30:44 +0200 Subject: [PATCH 5/9] cli: use static initializer for NmCli It's perhaps but a small improvement here, but will make things a lot more convenient when the color palette will be added. --- clients/cli/nmcli.c | 68 +++++++++++++++++++++------------------------ 1 file changed, 31 insertions(+), 37 deletions(-) diff --git a/clients/cli/nmcli.c b/clients/cli/nmcli.c index 8700fcdf73..3207762da2 100644 --- a/clients/cli/nmcli.c +++ b/clients/cli/nmcli.c @@ -16,7 +16,7 @@ * 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. + * Copyright 2010 - 2018 Red Hat, Inc. */ #include "nm-default.h" @@ -51,7 +51,33 @@ # define NMCLI_VERSION VERSION #endif -NmCli nm_cli; +NmCli nm_cli = { + .client = NULL, + + .return_value = NMC_RESULT_SUCCESS, + + .timeout = -1, + + .secret_agent = NULL, + .pwds_hash = NULL, + .pk_listener = NULL, + + .should_wait = 0, + .nowait_flag = TRUE, + .nmc_config.print_output = NMC_PRINT_NORMAL, + .nmc_config.multiline_output = FALSE, + .mode_specified = FALSE, + .nmc_config.escape_values = TRUE, + .required_fields = NULL, + .ask = FALSE, + .complete = FALSE, + .nmc_config.show_secrets = FALSE, + .nmc_config.use_colors = NMC_USE_COLOR_AUTO, + .nmc_config.in_editor = FALSE, + .editor_status_line = FALSE, + .editor_save_confirmation = TRUE, + .editor_prompt_color = NM_META_TERM_COLOR_NORMAL, +}; /*****************************************************************************/ @@ -382,8 +408,8 @@ process_command_line (NmCli *nmc, int argc, char **argv) complete_fields (argv[0], value); nmc->required_fields = g_strdup (value); nmc->nmc_config_mutable.print_output = NMC_PRINT_TERSE; - /* We want fixed tabular mode here, but just set the mode specified and rely on the initialization - * in nmc_init: in this way we allow use of "-m multiline" to swap the output mode also if placed + /* We want fixed tabular mode here, but just set the mode specified and rely on defaults: + * in this way we allow use of "-m multiline" to swap the output mode also if placed * before the "-g " option (-g may be still more practical and easy to remember than -t -f). */ nmc->mode_specified = TRUE; @@ -557,38 +583,6 @@ nmc_value_transforms_register (void) nmc_convert_bytes_to_string); } -/* Initialize NmCli structure - set default values */ -static void -nmc_init (NmCli *nmc) -{ - nmc->client = NULL; - - nmc->return_value = NMC_RESULT_SUCCESS; - nmc->return_text = g_string_new (_("Success")); - - nmc->timeout = -1; - - nmc->secret_agent = NULL; - nmc->pwds_hash = NULL; - nmc->pk_listener = NULL; - - nmc->should_wait = 0; - nmc->nowait_flag = TRUE; - nmc->nmc_config_mutable.print_output = NMC_PRINT_NORMAL; - nmc->nmc_config_mutable.multiline_output = FALSE; - nmc->mode_specified = FALSE; - nmc->nmc_config_mutable.escape_values = TRUE; - nmc->required_fields = NULL; - nmc->ask = FALSE; - nmc->complete = FALSE; - nmc->nmc_config_mutable.show_secrets = FALSE; - nmc->nmc_config_mutable.use_colors = NMC_USE_COLOR_AUTO; - nmc->nmc_config_mutable.in_editor = FALSE; - nmc->editor_status_line = FALSE; - nmc->editor_save_confirmation = TRUE; - nmc->editor_prompt_color = NM_META_TERM_COLOR_NORMAL; -} - static void nmc_cleanup (NmCli *nmc) { @@ -638,7 +632,7 @@ main (int argc, char *argv[]) nmc_value_transforms_register (); - nmc_init (&nm_cli); + nm_cli.return_text = g_string_new (_("Success")); loop = g_main_loop_new (NULL, FALSE); g_unix_signal_add (SIGTERM, signal_handler, GINT_TO_POINTER (SIGTERM)); From 56a5b2738925fab6300f15246923b6f071732238 Mon Sep 17 00:00:00 2001 From: Lubomir Rintel Date: Wed, 28 Mar 2018 23:08:12 +0200 Subject: [PATCH 6/9] cli: drop --prompt-color It's undocumented, useless, somewhat expensive in volume of code and probably just downright stupid. We'll get a more general way to set colors. Hacking in some code to keep this working wouldn't be too difficult, but it seems entirely pointless. --- clients/cli/connections.c | 53 +++++++++------------------------------ clients/cli/nmcli.c | 1 - clients/cli/nmcli.h | 3 +-- clients/cli/utils.c | 25 +----------------- clients/cli/utils.h | 3 +-- 5 files changed, 15 insertions(+), 70 deletions(-) diff --git a/clients/cli/connections.c b/clients/cli/connections.c index 47e608b7ba..405da31533 100644 --- a/clients/cli/connections.c +++ b/clients/cli/connections.c @@ -14,7 +14,7 @@ * 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. + * Copyright 2010 - 2018 Red Hat, Inc. */ #include "nm-default.h" @@ -4945,15 +4945,7 @@ gen_nmcli_cmds_submenu (const char *text, int state) static char * gen_cmd_nmcli (const char *text, int state) { - const char *words[] = { "status-line", "save-confirmation", "show-secrets", "prompt-color", NULL }; - return nmc_rl_gen_func_basic (text, state, words); -} - -static char * -gen_cmd_nmcli_prompt_color (const char *text, int state) -{ - const char *words[] = { "normal", "black", "red", "green", "yellow", - "blue", "magenta", "cyan", "white", NULL }; + const char *words[] = { "status-line", "save-confirmation", "show-secrets", NULL }; return nmc_rl_gen_func_basic (text, state, words); } @@ -5269,8 +5261,6 @@ get_gen_func_cmd_nmcli (const char *str) return gen_func_bool_values; if (matches (str, "show-secrets")) return gen_func_bool_values; - if (matches (str, "prompt-color")) - return gen_cmd_nmcli_prompt_color; return NULL; } @@ -6480,7 +6470,7 @@ property_edit_submenu (NmCli *nmc, /* Set global variable for use in TAB completion */ nmc_tab_completion.property = prop_name; - prompt = nmc_colorize (nmc->nmc_config.use_colors, nmc->editor_prompt_color, NM_META_TERM_FORMAT_NORMAL, + prompt = nmc_colorize (nmc->nmc_config.use_colors, NM_META_TERM_COLOR_NORMAL, NM_META_TERM_FORMAT_NORMAL, "nmcli %s.%s> ", nm_setting_get_name (curr_setting), prop_name); @@ -6874,7 +6864,7 @@ editor_menu_main (NmCli *nmc, NMConnection *connection, const char *connection_t valid_settings_str = get_valid_options_string (valid_settings_main, valid_settings_slave); g_print (_("You may edit the following settings: %s\n"), valid_settings_str); - menu_ctx.main_prompt = nmc_colorize (nmc->nmc_config.use_colors, nmc->editor_prompt_color, NM_META_TERM_FORMAT_NORMAL, + menu_ctx.main_prompt = nmc_colorize (nmc->nmc_config.use_colors, NM_META_TERM_COLOR_NORMAL, NM_META_TERM_FORMAT_NORMAL, BASE_PROMPT); /* Get remote connection */ @@ -7051,7 +7041,7 @@ editor_menu_main (NmCli *nmc, NMConnection *connection, const char *connection_t nmc_tab_completion.setting = setting; /* Switch to level 1 */ - menu_switch_to_level1 (nmc->nmc_config.use_colors, &menu_ctx, setting, setting_name, nmc->editor_prompt_color); + menu_switch_to_level1 (nmc->nmc_config.use_colors, &menu_ctx, setting, setting_name, NM_META_TERM_COLOR_NORMAL); if (!cmd_arg_s) { g_print (_("You may edit the following properties: %s\n"), menu_ctx.valid_props_str); @@ -7133,7 +7123,7 @@ editor_menu_main (NmCli *nmc, NMConnection *connection, const char *connection_t connection_remove_setting (connection, ss); if (ss == menu_ctx.curr_setting) { /* If we removed the setting we are in, go up */ - menu_switch_to_level0 (nmc->nmc_config.use_colors, &menu_ctx, BASE_PROMPT, nmc->editor_prompt_color); + menu_switch_to_level0 (nmc->nmc_config.use_colors, &menu_ctx, BASE_PROMPT, NM_META_TERM_COLOR_NORMAL); nmc_tab_completion.setting = NULL; /* for TAB completion */ } } else { @@ -7161,7 +7151,7 @@ editor_menu_main (NmCli *nmc, NMConnection *connection, const char *connection_t /* coverity[copy_paste_error] - suppress Coverity COPY_PASTE_ERROR defect */ if (ss == menu_ctx.curr_setting) { /* If we removed the setting we are in, go up */ - menu_switch_to_level0 (nmc->nmc_config.use_colors, &menu_ctx, BASE_PROMPT, nmc->editor_prompt_color); + menu_switch_to_level0 (nmc->nmc_config.use_colors, &menu_ctx, BASE_PROMPT, NM_META_TERM_COLOR_NORMAL); nmc_tab_completion.setting = NULL; /* for TAB completion */ } } else @@ -7517,7 +7507,7 @@ editor_menu_main (NmCli *nmc, NMConnection *connection, const char *connection_t case NMC_EDITOR_MAIN_CMD_BACK: /* Go back (up) an the menu */ if (menu_ctx.level == 1) { - menu_switch_to_level0 (nmc->nmc_config.use_colors, &menu_ctx, BASE_PROMPT, nmc->editor_prompt_color); + menu_switch_to_level0 (nmc->nmc_config.use_colors, &menu_ctx, BASE_PROMPT, NM_META_TERM_COLOR_NORMAL); nmc_tab_completion.setting = NULL; /* for TAB completion */ } break; @@ -7553,37 +7543,18 @@ editor_menu_main (NmCli *nmc, NMConnection *connection, const char *connection_t } else nmc->nmc_config_mutable.show_secrets = bb; } else if (cmd_arg_p && matches (cmd_arg_p, "prompt-color")) { - GError *tmp_err = NULL; - NMMetaTermColor color; - color = nmc_term_color_parse_string (cmd_arg_v ? g_strstrip (cmd_arg_v) : " ", &tmp_err); - if (tmp_err) { - g_print (_("Error: bad color: %s\n"), tmp_err->message); - g_clear_error (&tmp_err); - } else { - nmc->editor_prompt_color = color; - nm_clear_g_free (&menu_ctx.main_prompt); - if (menu_ctx.level == 0) { - menu_ctx.main_prompt = nmc_colorize (nmc->nmc_config.use_colors, nmc->editor_prompt_color, NM_META_TERM_FORMAT_NORMAL, - BASE_PROMPT); - } else { - menu_ctx.main_prompt = nmc_colorize (nmc->nmc_config.use_colors, nmc->editor_prompt_color, NM_META_TERM_FORMAT_NORMAL, - "nmcli %s> ", - nm_setting_get_name (menu_ctx.curr_setting)); - } - } + g_debug ("Ignoring erroneous --prompt-color argument.\n"); } else if (!cmd_arg_p) { g_print (_("Current nmcli configuration:\n")); g_print ("status-line: %s\n" "save-confirmation: %s\n" - "show-secrets: %s\n" - "prompt-color: %d\n", + "show-secrets: %s\n", nmc->editor_status_line ? "yes" : "no", nmc->editor_save_confirmation ? "yes" : "no", - nmc->nmc_config.show_secrets ? "yes" : "no", - nmc->editor_prompt_color); + nmc->nmc_config.show_secrets ? "yes" : "no"); } else g_print (_("Invalid configuration option '%s'; allowed [%s]\n"), - cmd_arg_v ?: "", "status-line, save-confirmation, show-secrets, prompt-color"); + cmd_arg_v ?: "", "status-line, save-confirmation, show-secrets"); break; diff --git a/clients/cli/nmcli.c b/clients/cli/nmcli.c index 3207762da2..1fa9cc6cb5 100644 --- a/clients/cli/nmcli.c +++ b/clients/cli/nmcli.c @@ -76,7 +76,6 @@ NmCli nm_cli = { .nmc_config.in_editor = FALSE, .editor_status_line = FALSE, .editor_save_confirmation = TRUE, - .editor_prompt_color = NM_META_TERM_COLOR_NORMAL, }; /*****************************************************************************/ diff --git a/clients/cli/nmcli.h b/clients/cli/nmcli.h index 0f3d29cda9..768479855a 100644 --- a/clients/cli/nmcli.h +++ b/clients/cli/nmcli.h @@ -14,7 +14,7 @@ * 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. + * Copyright 2010 - 2018 Red Hat, Inc. */ #ifndef NMC_NMCLI_H @@ -143,7 +143,6 @@ typedef struct _NmCli { gboolean complete; /* Autocomplete the command line */ gboolean editor_status_line; /* Whether to display status line in connection editor */ gboolean editor_save_confirmation; /* Whether to ask for confirmation on saving connections with 'autoconnect=yes' */ - NMMetaTermColor editor_prompt_color; /* Color of prompt in connection editor */ } NmCli; extern NmCli nm_cli; diff --git a/clients/cli/utils.c b/clients/cli/utils.c index 5101ab6892..79fb81dd02 100644 --- a/clients/cli/utils.c +++ b/clients/cli/utils.c @@ -15,7 +15,7 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * * Copyright 2010 Lennart Poettering - * Copyright 2010 - 2017 Red Hat, Inc. + * Copyright 2010 - 2018 Red Hat, Inc. */ #include "nm-default.h" @@ -430,29 +430,6 @@ nmc_term_color_sequence (NMMetaTermColor color) } } -/* Parses @str for color as string or number */ -NMMetaTermColor -nmc_term_color_parse_string (const char *str, GError **error) -{ - unsigned long color_int; - static const char *colors[] = { "normal", "black", "red", "green", "yellow", - "blue", "magenta", "cyan", "white", NULL }; - - if (nmc_string_to_uint (str, TRUE, 0, 8, &color_int)) { - return (NMMetaTermColor) color_int; - } else { - const char *color, **p; - int i; - - color = nmc_string_is_valid (str, colors, error); - for (p = colors, i = 0; *p != NULL; p++, i++) { - if (*p == color) - return (NMMetaTermColor) i; - } - return -1; - } -} - const char * nmc_term_format_sequence (NMMetaTermFormat format) { diff --git a/clients/cli/utils.h b/clients/cli/utils.h index c92c4e2e83..09180bf912 100644 --- a/clients/cli/utils.h +++ b/clients/cli/utils.h @@ -14,7 +14,7 @@ * 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. + * Copyright 2010 - 2018 Red Hat, Inc. */ #ifndef NMC_UTILS_H @@ -44,7 +44,6 @@ void nmc_terminal_spawn_pager (const NmcConfig *nmc_config); gboolean nmc_term_use_colors (NmcColorOption color_option); const char *nmc_term_color_sequence (NMMetaTermColor color); const char *nmc_term_format_sequence (NMMetaTermFormat format); -NMMetaTermColor nmc_term_color_parse_string (const char *str, GError **error); char *nmc_colorize (NmcColorOption color_option, NMMetaTermColor color, NMMetaTermFormat format, const char * fmt, ...) _nm_printf (4, 5); void nmc_filter_out_colors_inplace (char *str); char *nmc_filter_out_colors (const char *str); From 9dfe8258409c1a3903f8f82518983f11ccf42a2f Mon Sep 17 00:00:00 2001 From: Lubomir Rintel Date: Thu, 29 Mar 2018 12:25:14 +0200 Subject: [PATCH 7/9] cli: rework enabling and disabling colors This actually makes very little difference at the moment, but will make things more confortable later on, when the logic of enabling/disabling coloring will involve terminal-colors.d(5). Instead of deciding whether to use colors lazily with use_colors(), it's done very early on nmcli initialization and a boolean use_colors field is stored in the NmcConfig instance instead of the raw tristate option of NmcColorOption type (which is now confined to nmcli.c). Wherever the NmcColorOption was used previously, the whole NmcConfig instance is passed around. That might seem pointless (since only the use_colors boolean is actually used at the moment), but will be utilized to pass around the actual color palette in future. --- clients/cli/connections.c | 20 ++++++++--------- clients/cli/devices.c | 4 ++-- clients/cli/general.c | 18 +++++++-------- clients/cli/nmcli.c | 36 ++++++++++++++++++++++++++---- clients/cli/nmcli.h | 8 +------ clients/cli/utils.c | 46 ++++++++++++--------------------------- clients/cli/utils.h | 3 +-- 7 files changed, 69 insertions(+), 66 deletions(-) diff --git a/clients/cli/connections.c b/clients/cli/connections.c index 405da31533..823dd49a7c 100644 --- a/clients/cli/connections.c +++ b/clients/cli/connections.c @@ -6470,7 +6470,7 @@ property_edit_submenu (NmCli *nmc, /* Set global variable for use in TAB completion */ nmc_tab_completion.property = prop_name; - prompt = nmc_colorize (nmc->nmc_config.use_colors, NM_META_TERM_COLOR_NORMAL, NM_META_TERM_FORMAT_NORMAL, + prompt = nmc_colorize (&nmc->nmc_config, NM_META_TERM_COLOR_NORMAL, NM_META_TERM_FORMAT_NORMAL, "nmcli %s.%s> ", nm_setting_get_name (curr_setting), prop_name); @@ -6801,14 +6801,14 @@ typedef struct { } NmcEditorMenuContext; static void -menu_switch_to_level0 (NmcColorOption color_option, +menu_switch_to_level0 (const NmcConfig *nmc_config, NmcEditorMenuContext *menu_ctx, const char *prompt, NMMetaTermColor prompt_color) { menu_ctx->level = 0; g_free (menu_ctx->main_prompt); - menu_ctx->main_prompt = nmc_colorize (color_option, prompt_color, NM_META_TERM_FORMAT_NORMAL, "%s", prompt); + menu_ctx->main_prompt = nmc_colorize (nmc_config, prompt_color, NM_META_TERM_FORMAT_NORMAL, "%s", prompt); menu_ctx->curr_setting = NULL; g_strfreev (menu_ctx->valid_props); menu_ctx->valid_props = NULL; @@ -6817,7 +6817,7 @@ menu_switch_to_level0 (NmcColorOption color_option, } static void -menu_switch_to_level1 (NmcColorOption color_option, +menu_switch_to_level1 (const NmcConfig *nmc_config, NmcEditorMenuContext *menu_ctx, NMSetting *setting, const char *setting_name, @@ -6825,7 +6825,7 @@ menu_switch_to_level1 (NmcColorOption color_option, { menu_ctx->level = 1; g_free (menu_ctx->main_prompt); - menu_ctx->main_prompt = nmc_colorize (color_option, prompt_color, NM_META_TERM_FORMAT_NORMAL, + menu_ctx->main_prompt = nmc_colorize (nmc_config, prompt_color, NM_META_TERM_FORMAT_NORMAL, "nmcli %s> ", setting_name); menu_ctx->curr_setting = setting; g_strfreev (menu_ctx->valid_props); @@ -6864,7 +6864,7 @@ editor_menu_main (NmCli *nmc, NMConnection *connection, const char *connection_t valid_settings_str = get_valid_options_string (valid_settings_main, valid_settings_slave); g_print (_("You may edit the following settings: %s\n"), valid_settings_str); - menu_ctx.main_prompt = nmc_colorize (nmc->nmc_config.use_colors, NM_META_TERM_COLOR_NORMAL, NM_META_TERM_FORMAT_NORMAL, + menu_ctx.main_prompt = nmc_colorize (&nmc->nmc_config, NM_META_TERM_COLOR_NORMAL, NM_META_TERM_FORMAT_NORMAL, BASE_PROMPT); /* Get remote connection */ @@ -7041,7 +7041,7 @@ editor_menu_main (NmCli *nmc, NMConnection *connection, const char *connection_t nmc_tab_completion.setting = setting; /* Switch to level 1 */ - menu_switch_to_level1 (nmc->nmc_config.use_colors, &menu_ctx, setting, setting_name, NM_META_TERM_COLOR_NORMAL); + menu_switch_to_level1 (&nmc->nmc_config, &menu_ctx, setting, setting_name, NM_META_TERM_COLOR_NORMAL); if (!cmd_arg_s) { g_print (_("You may edit the following properties: %s\n"), menu_ctx.valid_props_str); @@ -7123,7 +7123,7 @@ editor_menu_main (NmCli *nmc, NMConnection *connection, const char *connection_t connection_remove_setting (connection, ss); if (ss == menu_ctx.curr_setting) { /* If we removed the setting we are in, go up */ - menu_switch_to_level0 (nmc->nmc_config.use_colors, &menu_ctx, BASE_PROMPT, NM_META_TERM_COLOR_NORMAL); + menu_switch_to_level0 (&nmc->nmc_config, &menu_ctx, BASE_PROMPT, NM_META_TERM_COLOR_NORMAL); nmc_tab_completion.setting = NULL; /* for TAB completion */ } } else { @@ -7151,7 +7151,7 @@ editor_menu_main (NmCli *nmc, NMConnection *connection, const char *connection_t /* coverity[copy_paste_error] - suppress Coverity COPY_PASTE_ERROR defect */ if (ss == menu_ctx.curr_setting) { /* If we removed the setting we are in, go up */ - menu_switch_to_level0 (nmc->nmc_config.use_colors, &menu_ctx, BASE_PROMPT, NM_META_TERM_COLOR_NORMAL); + menu_switch_to_level0 (&nmc->nmc_config, &menu_ctx, BASE_PROMPT, NM_META_TERM_COLOR_NORMAL); nmc_tab_completion.setting = NULL; /* for TAB completion */ } } else @@ -7507,7 +7507,7 @@ editor_menu_main (NmCli *nmc, NMConnection *connection, const char *connection_t case NMC_EDITOR_MAIN_CMD_BACK: /* Go back (up) an the menu */ if (menu_ctx.level == 1) { - menu_switch_to_level0 (nmc->nmc_config.use_colors, &menu_ctx, BASE_PROMPT, NM_META_TERM_COLOR_NORMAL); + menu_switch_to_level0 (&nmc->nmc_config, &menu_ctx, BASE_PROMPT, NM_META_TERM_COLOR_NORMAL); nmc_tab_completion.setting = NULL; /* for TAB completion */ } break; diff --git a/clients/cli/devices.c b/clients/cli/devices.c index 5ad546a28a..607339dde0 100644 --- a/clients/cli/devices.c +++ b/clients/cli/devices.c @@ -14,7 +14,7 @@ * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * - * Copyright 2010 - 2014 Red Hat, Inc. + * Copyright 2010 - 2018 Red Hat, Inc. */ #include "nm-default.h" @@ -2411,7 +2411,7 @@ device_state (NMDevice *device, GParamSpec *pspec, NmCli *nmc) char *str; nmc_device_state_to_color (state, &color, &color_fmt); - str = nmc_colorize (nmc->nmc_config.use_colors, color, color_fmt, "%s: %s\n", + str = nmc_colorize (&nmc->nmc_config, color, color_fmt, "%s: %s\n", nm_device_get_iface (device), nmc_device_state_to_string (state)); diff --git a/clients/cli/general.c b/clients/cli/general.c index 17c0d79476..437ac9d165 100644 --- a/clients/cli/general.c +++ b/clients/cli/general.c @@ -14,7 +14,7 @@ * 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. + * Copyright 2010 - 2018 Red Hat, Inc. */ #include "nm-default.h" @@ -1086,7 +1086,7 @@ networkmanager_running (NMClient *client, GParamSpec *param, NmCli *nmc) char *str; running = nm_client_get_nm_running (client); - str = nmc_colorize (nmc->nmc_config.use_colors, + str = nmc_colorize (&nmc->nmc_config, running ? NM_META_TERM_COLOR_GREEN : NM_META_TERM_COLOR_RED, NM_META_TERM_FORMAT_NORMAL, running ? _("NetworkManager has started") : _("NetworkManager has stopped")); @@ -1128,7 +1128,7 @@ client_connectivity (NMClient *client, GParamSpec *param, NmCli *nmc) char *str; g_object_get (client, NM_CLIENT_CONNECTIVITY, &connectivity, NULL); - str = nmc_colorize (nmc->nmc_config.use_colors, connectivity_to_color (connectivity), NM_META_TERM_FORMAT_NORMAL, + str = nmc_colorize (&nmc->nmc_config, connectivity_to_color (connectivity), NM_META_TERM_FORMAT_NORMAL, _("Connectivity is now '%s'\n"), nm_connectivity_to_string (connectivity)); g_print ("%s", str); g_free (str); @@ -1141,7 +1141,7 @@ client_state (NMClient *client, GParamSpec *param, NmCli *nmc) char *str; g_object_get (client, NM_CLIENT_STATE, &state, NULL); - str = nmc_colorize (nmc->nmc_config.use_colors, state_to_color (state), NM_META_TERM_FORMAT_NORMAL, + str = nmc_colorize (&nmc->nmc_config, state_to_color (state), NM_META_TERM_FORMAT_NORMAL, _("Networkmanager is now in the '%s' state\n"), nm_state_to_string (state)); g_print ("%s", str); @@ -1184,12 +1184,12 @@ device_overview (NmCli *nmc, NMDevice *device) if (!nm_device_get_autoconnect (device)) g_string_append_printf (outbuf, "%s, ", _("autoconnect")); if (nm_device_get_firmware_missing (device)) { - tmp = nmc_colorize (nmc->nmc_config.use_colors, NM_META_TERM_COLOR_RED, NM_META_TERM_FORMAT_NORMAL, _("fw missing")); + tmp = nmc_colorize (&nmc->nmc_config, NM_META_TERM_COLOR_RED, NM_META_TERM_FORMAT_NORMAL, _("fw missing")); g_string_append_printf (outbuf, "%s, ", tmp); g_free (tmp); } if (nm_device_get_nm_plugin_missing (device)) { - tmp = nmc_colorize (nmc->nmc_config.use_colors, NM_META_TERM_COLOR_RED, NM_META_TERM_FORMAT_NORMAL, _("plugin missing")); + tmp = nmc_colorize (&nmc->nmc_config, NM_META_TERM_COLOR_RED, NM_META_TERM_FORMAT_NORMAL, _("plugin missing")); g_string_append_printf (outbuf, "%s, ", tmp); g_free (tmp); } @@ -1315,7 +1315,7 @@ do_overview (NmCli *nmc, int argc, char **argv) state = nm_active_connection_get_state (ac); nmc_active_connection_state_to_color (state, &color); - tmp = nmc_colorize (nmc->nmc_config.use_colors, color, NM_META_TERM_FORMAT_NORMAL, _("%s VPN connection"), + tmp = nmc_colorize (&nmc->nmc_config, color, NM_META_TERM_FORMAT_NORMAL, _("%s VPN connection"), nm_active_connection_get_id (ac)); g_print ("%s\n", tmp); g_free (tmp); @@ -1333,7 +1333,7 @@ do_overview (NmCli *nmc, int argc, char **argv) state = nm_device_get_state (devices[i]); nmc_device_state_to_color (state, &color, &color_fmt); - tmp = nmc_colorize (nmc->nmc_config.use_colors, color, color_fmt, "%s: %s%s%s", + tmp = nmc_colorize (&nmc->nmc_config, color, color_fmt, "%s: %s%s%s", nm_device_get_iface (devices[i]), nmc_device_state_to_string (state), ac ? " to " : "", @@ -1416,7 +1416,7 @@ do_monitor (NmCli *nmc, int argc, char **argv) if (!nm_client_get_nm_running (nmc->client)) { char *str; - str = nmc_colorize (nmc->nmc_config.use_colors, NM_META_TERM_COLOR_RED, NM_META_TERM_FORMAT_NORMAL, + str = nmc_colorize (&nmc->nmc_config, NM_META_TERM_COLOR_RED, NM_META_TERM_FORMAT_NORMAL, _("Networkmanager is not running (waiting for it)\n")); g_print ("%s", str); g_free (str); diff --git a/clients/cli/nmcli.c b/clients/cli/nmcli.c index 1fa9cc6cb5..fe9e1c12fb 100644 --- a/clients/cli/nmcli.c +++ b/clients/cli/nmcli.c @@ -72,7 +72,6 @@ NmCli nm_cli = { .ask = FALSE, .complete = FALSE, .nmc_config.show_secrets = FALSE, - .nmc_config.use_colors = NMC_USE_COLOR_AUTO, .nmc_config.in_editor = FALSE, .editor_status_line = FALSE, .editor_save_confirmation = TRUE, @@ -294,9 +293,36 @@ matches_arg (NmCli *nmc, int *argc, char ***argv, const char *pattern, char **ar return TRUE; } +typedef enum { + NMC_USE_COLOR_AUTO, + NMC_USE_COLOR_YES, + NMC_USE_COLOR_NO, +} NmcColorOption; + +static void +set_colors (NmCli *nmc, NmcColorOption *color_option) +{ + if (*color_option == NMC_USE_COLOR_AUTO) { + if ( g_strcmp0 (g_getenv ("TERM"), "dumb") == 0 + || !isatty (STDOUT_FILENO)) + *color_option = NMC_USE_COLOR_NO; + } + + switch (*color_option) { + case NMC_USE_COLOR_YES: + case NMC_USE_COLOR_AUTO: + nmc->nmc_config_mutable.use_colors = TRUE; + break; + case NMC_USE_COLOR_NO: + nmc->nmc_config_mutable.use_colors = FALSE; + break; + } +} + static gboolean process_command_line (NmCli *nmc, int argc, char **argv) { + NmcColorOption colors = NMC_USE_COLOR_AUTO; char *base; base = strrchr (argv[0], '/'); @@ -376,11 +402,11 @@ process_command_line (NmCli *nmc, int argc, char **argv) if (argc == 1 && nmc->complete) complete_option_with_value (argv[0], value, "yes", "no", "auto", NULL); if (matches (value, "auto")) - nmc->nmc_config_mutable.use_colors = NMC_USE_COLOR_AUTO; + colors = NMC_USE_COLOR_AUTO; else if (matches (value, "yes")) - nmc->nmc_config_mutable.use_colors = NMC_USE_COLOR_YES; + colors = NMC_USE_COLOR_YES; else if (matches (value, "no")) - nmc->nmc_config_mutable.use_colors = NMC_USE_COLOR_NO; + colors = NMC_USE_COLOR_NO; else { g_string_printf (nmc->return_text, _("Error: '%s' is not valid argument for '%s' option."), value, argv[0]); nmc->return_value = NMC_RESULT_ERROR_USER_INPUT; @@ -446,6 +472,8 @@ process_command_line (NmCli *nmc, int argc, char **argv) if (nmc->required_fields) nmc->nmc_config_mutable.overview = FALSE; + set_colors (nmc, &colors); + /* Now run the requested command */ nmc_do_cmd (nmc, nmcli_cmds, *argv, argc, argv); diff --git a/clients/cli/nmcli.h b/clients/cli/nmcli.h index 768479855a..66a1122f83 100644 --- a/clients/cli/nmcli.h +++ b/clients/cli/nmcli.h @@ -97,15 +97,9 @@ struct _NmcOutputField { NMMetaTermFormat color_fmt; /* Use this terminal format to print value */ }; -typedef enum { - NMC_USE_COLOR_AUTO, - NMC_USE_COLOR_YES, - NMC_USE_COLOR_NO, -} NmcColorOption; - typedef struct _NmcConfig { NMCPrintOutput print_output; /* Output mode */ - NmcColorOption use_colors; /* Whether to use colors for output: option '--color' */ + gboolean use_colors; /* Whether to use colors for output: option '--color' */ bool multiline_output; /* Multiline output instead of default tabular */ bool escape_values; /* Whether to escape ':' and '\' in terse tabular mode */ bool in_editor; /* Whether running the editor - nmcli con edit' */ diff --git a/clients/cli/utils.c b/clients/cli/utils.c index 79fb81dd02..f1cd222da1 100644 --- a/clients/cli/utils.c +++ b/clients/cli/utils.c @@ -119,27 +119,8 @@ const NMMetaType nmc_meta_type_generic_info = { /*****************************************************************************/ -static gboolean -use_colors (NmcColorOption color_option) -{ - if (color_option == NMC_USE_COLOR_AUTO) { - static NmcColorOption cached = NMC_USE_COLOR_AUTO; - - if (G_UNLIKELY (cached == NMC_USE_COLOR_AUTO)) { - if ( g_strcmp0 (g_getenv ("TERM"), "dumb") == 0 - || !isatty (STDOUT_FILENO)) - cached = NMC_USE_COLOR_NO; - else - cached = NMC_USE_COLOR_YES; - } - return cached == NMC_USE_COLOR_YES; - } - - return color_option == NMC_USE_COLOR_YES; -} - static const char * -colorize_string (NmcColorOption color_option, +colorize_string (const NmcConfig *nmc_config, NMMetaTermColor color, NMMetaTermFormat color_fmt, const char *str, @@ -147,9 +128,10 @@ colorize_string (NmcColorOption color_option, { const char *out = str; - if ( use_colors (color_option) + if ( nmc_config + && nmc_config->use_colors && (color != NM_META_TERM_COLOR_NORMAL || color_fmt != NM_META_TERM_FORMAT_NORMAL)) { - *out_to_free = nmc_colorize (color_option, color, color_fmt, "%s", str); + *out_to_free = nmc_colorize (nmc_config, color, color_fmt, "%s", str); out = *out_to_free; } @@ -459,7 +441,7 @@ nmc_term_format_sequence (NMMetaTermFormat format) } char * -nmc_colorize (NmcColorOption color_option, NMMetaTermColor color, NMMetaTermFormat format, const char *fmt, ...) +nmc_colorize (const NmcConfig *nmc_config, NMMetaTermColor color, NMMetaTermFormat format, const char *fmt, ...) { va_list args; char *str, *colored; @@ -470,7 +452,7 @@ nmc_colorize (NmcColorOption color_option, NMMetaTermColor color, NMMetaTermForm str = g_strdup_vprintf (fmt, args); va_end (args); - if (!use_colors (color_option)) + if (!nmc_config->use_colors) return str; ansi_color = nmc_term_color_sequence (color); @@ -1351,7 +1333,7 @@ _print_do (const NmcConfig *nmc_config, gs_free char *text_to_free = NULL; const char *text; - text = colorize_string (nmc_config->use_colors, + text = colorize_string (nmc_config, cell->term_color, cell->term_format, lines[i_lines], &text_to_free); if (multiline) { @@ -1482,7 +1464,7 @@ nmc_terminal_spawn_pager (const NmcConfig *nmc_config) if ( nm_cli.nmc_config.in_editor || nm_cli.pager_pid > 0 || nmc_config->print_output == NMC_PRINT_TERSE - || !use_colors (nmc_config->use_colors) + || !nmc_config->use_colors || g_strcmp0 (pager, "") == 0 || getauxval (AT_SECURE)) return; @@ -1553,7 +1535,7 @@ nmc_terminal_spawn_pager (const NmcConfig *nmc_config) /*****************************************************************************/ static const char * -get_value_to_print (NmcColorOption color_option, +get_value_to_print (const NmcConfig *nmc_config, const NmcOutputField *field, gboolean field_name, const char *not_set_str, @@ -1579,7 +1561,7 @@ get_value_to_print (NmcColorOption color_option, } /* colorize the value */ - out = colorize_string (color_option, field->color, field->color_fmt, value, out_to_free); + out = colorize_string (nmc_config, field->color, field->color_fmt, value, out_to_free); if (out && out == free_value) { nm_assert (!*out_to_free); @@ -1673,7 +1655,7 @@ print_required_fields (const NmcConfig *nmc_config, gs_free char *tmp = NULL; val = *p ?: not_set_str; - print_val = colorize_string (nmc_config->use_colors, field_values[idx].color, field_values[idx].color_fmt, + print_val = colorize_string (nmc_config, field_values[idx].color, field_values[idx].color_fmt, val, &val_to_free); tmp = g_strdup_printf ("%s%s%s[%d]:", section_prefix ? (const char*) field_values[0].value : "", @@ -1694,7 +1676,7 @@ print_required_fields (const NmcConfig *nmc_config, /* value is a string */ val = val && *val ? val : not_set_str; - print_val = colorize_string (nmc_config->use_colors, field_values[idx].color, field_values[idx].color_fmt, + print_val = colorize_string (nmc_config, field_values[idx].color, field_values[idx].color_fmt, val, &val_to_free); tmp = g_strdup_printf ("%s%s%s:", section_prefix ? hdr_name : "", @@ -1725,7 +1707,7 @@ print_required_fields (const NmcConfig *nmc_config, idx = g_array_index (indices, int, i); - value = get_value_to_print (nmc_config->use_colors, (NmcOutputField *) field_values+idx, field_names, + value = get_value_to_print (nmc_config, (NmcOutputField *) field_values+idx, field_names, not_set_str, &val_to_free); if (terse) { @@ -1798,7 +1780,7 @@ print_data_prepare_width (GPtrArray *output_data) row = g_ptr_array_index (output_data, j); field_names = row[0].flags & NMC_OF_FLAG_FIELD_NAMES; - value = get_value_to_print (NMC_USE_COLOR_NO, row+i, field_names, "--", &val_to_free); + value = get_value_to_print (NULL, row+i, field_names, "--", &val_to_free); len = nmc_string_screen_width (value, NULL); max_width = len > max_width ? len : max_width; } diff --git a/clients/cli/utils.h b/clients/cli/utils.h index 09180bf912..e73973baec 100644 --- a/clients/cli/utils.h +++ b/clients/cli/utils.h @@ -41,10 +41,9 @@ char *ssid_to_hex (const char *str, gsize len); void nmc_terminal_erase_line (void); void nmc_terminal_show_progress (const char *str); void nmc_terminal_spawn_pager (const NmcConfig *nmc_config); -gboolean nmc_term_use_colors (NmcColorOption color_option); const char *nmc_term_color_sequence (NMMetaTermColor color); const char *nmc_term_format_sequence (NMMetaTermFormat format); -char *nmc_colorize (NmcColorOption color_option, NMMetaTermColor color, NMMetaTermFormat format, const char * fmt, ...) _nm_printf (4, 5); +char *nmc_colorize (const NmcConfig *nmc_config, NMMetaTermColor color, NMMetaTermFormat format, const char * fmt, ...) _nm_printf (4, 5); void nmc_filter_out_colors_inplace (char *str); char *nmc_filter_out_colors (const char *str); char *nmc_get_user_input (const char *ask_str); From 31aa2cfe29beb1bb7371ff36dbbd8baebeeaa06e Mon Sep 17 00:00:00 2001 From: Lubomir Rintel Date: Thu, 29 Mar 2018 12:31:33 +0200 Subject: [PATCH 8/9] cli: use a palette to implement coloring This basically replaces the (NMMetaTermColor, NMMetaTermFormat) combo with NMMetaColor that describes the colored element semantically as opposed to storing the raw attributes. A (currently static) paletted is used to translate the semantic color code to the actual ANSI controle sequence. This matches what terminal-colors.d(5) schemes use, making it convenient to implement customizable palettes. --- clients/cli/common.c | 6 +- clients/cli/connections.c | 45 ++++---- clients/cli/connections.h | 4 +- clients/cli/devices.c | 55 +++++----- clients/cli/devices.h | 4 +- clients/cli/general.c | 78 +++++++------- clients/cli/nmcli.c | 36 +++++++ clients/cli/nmcli.h | 4 +- clients/cli/utils.c | 141 ++++++-------------------- clients/cli/utils.h | 13 +-- clients/common/nm-meta-setting-desc.h | 93 ++++++++--------- 11 files changed, 207 insertions(+), 272 deletions(-) diff --git a/clients/cli/common.c b/clients/cli/common.c index 393a1ee210..738288b690 100644 --- a/clients/cli/common.c +++ b/clients/cli/common.c @@ -16,7 +16,7 @@ * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * - * Copyright 2012 - 2017 Red Hat, Inc. + * Copyright 2012 - 2018 Red Hat, Inc. */ #include "nm-default.h" @@ -119,7 +119,7 @@ _metagen_ip4_config_get_fcn (const NMMetaEnvironment *environment, nm_assert (info->info_type < _NMC_GENERIC_INFO_TYPE_IP4_CONFIG_NUM); - NMC_HANDLE_TERMFORMAT (NM_META_TERM_COLOR_NORMAL); + NMC_HANDLE_COLOR (NM_META_COLOR_NONE); NM_SET_OUT (out_is_default, TRUE); switch (info->info_type) { @@ -202,7 +202,7 @@ _metagen_ip6_config_get_fcn (const NMMetaEnvironment *environment, nm_assert (info->info_type < _NMC_GENERIC_INFO_TYPE_IP6_CONFIG_NUM); - NMC_HANDLE_TERMFORMAT (NM_META_TERM_COLOR_NORMAL); + NMC_HANDLE_COLOR (NM_META_COLOR_NONE); NM_SET_OUT (out_is_default, TRUE); switch (info->info_type) { diff --git a/clients/cli/connections.c b/clients/cli/connections.c index 823dd49a7c..8c4995845f 100644 --- a/clients/cli/connections.c +++ b/clients/cli/connections.c @@ -811,17 +811,17 @@ found: return found; } -void -nmc_active_connection_state_to_color (NMActiveConnectionState state, NMMetaTermColor *color) +NMMetaColor +nmc_active_connection_state_to_color (NMActiveConnectionState state) { - *color = NM_META_TERM_COLOR_NORMAL; - if (state == NM_ACTIVE_CONNECTION_STATE_ACTIVATING) - *color = NM_META_TERM_COLOR_YELLOW; + return NM_META_COLOR_CONNECTION_ACTIVATING; else if (state == NM_ACTIVE_CONNECTION_STATE_ACTIVATED) - *color = NM_META_TERM_COLOR_GREEN; + return NM_META_COLOR_CONNECTION_ACTIVATED; else if (state > NM_ACTIVE_CONNECTION_STATE_ACTIVATED) - *color = NM_META_TERM_COLOR_RED; + return NM_META_COLOR_CONNECTION_DISCONNECTING; + else + return NM_META_COLOR_CONNECTION_UNKNOWN; } /* Essentially a version of nm_setting_connection_get_connection_type() that @@ -864,7 +864,7 @@ fill_output_connection (NMConnection *connection, NMClient *client, NMCPrintOutp const char *ac_state = NULL; NMActiveConnectionState ac_state_int = NM_ACTIVE_CONNECTION_STATE_UNKNOWN; char *ac_dev = NULL; - NMMetaTermColor color; + NMMetaColor color; s_con = nm_connection_get_setting_connection (connection); g_assert (s_con); @@ -893,7 +893,7 @@ fill_output_connection (NMConnection *connection, NMClient *client, NMCPrintOutp arr = nmc_dup_fields_array ((const NMMetaAbstractInfo *const*) nmc_fields_con_show, 0); /* Show active connections in color */ - nmc_active_connection_state_to_color (ac_state_int, &color); + color = nmc_active_connection_state_to_color (ac_state_int); set_val_color_all (arr, color); set_val_strc (arr, 0, nm_setting_connection_get_id (s_con)); @@ -944,7 +944,7 @@ fill_output_connection_for_invisible (NMActiveConnection *ac, NMCPrintOutput pri set_val_strc (arr, 12, ac_path); set_val_strc (arr, 13, NULL); - set_val_color_fmt_all (arr, NM_META_TERM_FORMAT_DIM); + set_val_color_all (arr, NM_META_COLOR_CONNECTION_INVISIBLE); g_ptr_array_add (output_data, arr); } @@ -6470,8 +6470,7 @@ property_edit_submenu (NmCli *nmc, /* Set global variable for use in TAB completion */ nmc_tab_completion.property = prop_name; - prompt = nmc_colorize (&nmc->nmc_config, NM_META_TERM_COLOR_NORMAL, NM_META_TERM_FORMAT_NORMAL, - "nmcli %s.%s> ", + prompt = nmc_colorize (&nmc->nmc_config, NM_META_COLOR_PROMPT, "nmcli %s.%s> ", nm_setting_get_name (curr_setting), prop_name); while (cmd_property_loop) { @@ -6803,12 +6802,11 @@ typedef struct { static void menu_switch_to_level0 (const NmcConfig *nmc_config, NmcEditorMenuContext *menu_ctx, - const char *prompt, - NMMetaTermColor prompt_color) + const char *prompt) { menu_ctx->level = 0; g_free (menu_ctx->main_prompt); - menu_ctx->main_prompt = nmc_colorize (nmc_config, prompt_color, NM_META_TERM_FORMAT_NORMAL, "%s", prompt); + menu_ctx->main_prompt = nmc_colorize (nmc_config, NM_META_COLOR_PROMPT, "%s", prompt); menu_ctx->curr_setting = NULL; g_strfreev (menu_ctx->valid_props); menu_ctx->valid_props = NULL; @@ -6820,13 +6818,11 @@ static void menu_switch_to_level1 (const NmcConfig *nmc_config, NmcEditorMenuContext *menu_ctx, NMSetting *setting, - const char *setting_name, - NMMetaTermColor prompt_color) + const char *setting_name) { menu_ctx->level = 1; g_free (menu_ctx->main_prompt); - menu_ctx->main_prompt = nmc_colorize (nmc_config, prompt_color, NM_META_TERM_FORMAT_NORMAL, - "nmcli %s> ", setting_name); + menu_ctx->main_prompt = nmc_colorize (nmc_config, NM_META_COLOR_PROMPT, "nmcli %s> ", setting_name); menu_ctx->curr_setting = setting; g_strfreev (menu_ctx->valid_props); menu_ctx->valid_props = nmc_setting_get_valid_properties (menu_ctx->curr_setting); @@ -6864,8 +6860,7 @@ editor_menu_main (NmCli *nmc, NMConnection *connection, const char *connection_t valid_settings_str = get_valid_options_string (valid_settings_main, valid_settings_slave); g_print (_("You may edit the following settings: %s\n"), valid_settings_str); - menu_ctx.main_prompt = nmc_colorize (&nmc->nmc_config, NM_META_TERM_COLOR_NORMAL, NM_META_TERM_FORMAT_NORMAL, - BASE_PROMPT); + menu_ctx.main_prompt = nmc_colorize (&nmc->nmc_config, NM_META_COLOR_PROMPT, BASE_PROMPT); /* Get remote connection */ con_tmp = nm_client_get_connection_by_uuid (nmc->client, @@ -7041,7 +7036,7 @@ editor_menu_main (NmCli *nmc, NMConnection *connection, const char *connection_t nmc_tab_completion.setting = setting; /* Switch to level 1 */ - menu_switch_to_level1 (&nmc->nmc_config, &menu_ctx, setting, setting_name, NM_META_TERM_COLOR_NORMAL); + menu_switch_to_level1 (&nmc->nmc_config, &menu_ctx, setting, setting_name); if (!cmd_arg_s) { g_print (_("You may edit the following properties: %s\n"), menu_ctx.valid_props_str); @@ -7123,7 +7118,7 @@ editor_menu_main (NmCli *nmc, NMConnection *connection, const char *connection_t connection_remove_setting (connection, ss); if (ss == menu_ctx.curr_setting) { /* If we removed the setting we are in, go up */ - menu_switch_to_level0 (&nmc->nmc_config, &menu_ctx, BASE_PROMPT, NM_META_TERM_COLOR_NORMAL); + menu_switch_to_level0 (&nmc->nmc_config, &menu_ctx, BASE_PROMPT); nmc_tab_completion.setting = NULL; /* for TAB completion */ } } else { @@ -7151,7 +7146,7 @@ editor_menu_main (NmCli *nmc, NMConnection *connection, const char *connection_t /* coverity[copy_paste_error] - suppress Coverity COPY_PASTE_ERROR defect */ if (ss == menu_ctx.curr_setting) { /* If we removed the setting we are in, go up */ - menu_switch_to_level0 (&nmc->nmc_config, &menu_ctx, BASE_PROMPT, NM_META_TERM_COLOR_NORMAL); + menu_switch_to_level0 (&nmc->nmc_config, &menu_ctx, BASE_PROMPT); nmc_tab_completion.setting = NULL; /* for TAB completion */ } } else @@ -7507,7 +7502,7 @@ editor_menu_main (NmCli *nmc, NMConnection *connection, const char *connection_t case NMC_EDITOR_MAIN_CMD_BACK: /* Go back (up) an the menu */ if (menu_ctx.level == 1) { - menu_switch_to_level0 (&nmc->nmc_config, &menu_ctx, BASE_PROMPT, NM_META_TERM_COLOR_NORMAL); + menu_switch_to_level0 (&nmc->nmc_config, &menu_ctx, BASE_PROMPT); nmc_tab_completion.setting = NULL; /* for TAB completion */ } break; diff --git a/clients/cli/connections.h b/clients/cli/connections.h index 01e78b02ca..591e9cda67 100644 --- a/clients/cli/connections.h +++ b/clients/cli/connections.h @@ -14,7 +14,7 @@ * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * - * (C) Copyright 2010 - 2014 Red Hat, Inc. + * (C) Copyright 2010 - 2018 Red Hat, Inc. */ #ifndef NMC_CONNECTIONS_H @@ -33,7 +33,7 @@ nmc_read_connection_properties (NmCli *nmc, char ***argv, GError **error); -void nmc_active_connection_state_to_color (NMActiveConnectionState state, NMMetaTermColor *color); +NMMetaColor nmc_active_connection_state_to_color (NMActiveConnectionState state); extern const NmcMetaGenericInfo *const nmc_fields_con_show[]; extern const NmcMetaGenericInfo *const nmc_fields_con_active_details_general[]; diff --git a/clients/cli/devices.c b/clients/cli/devices.c index 607339dde0..b99e606a96 100644 --- a/clients/cli/devices.c +++ b/clients/cli/devices.c @@ -707,22 +707,19 @@ sort_access_points (const GPtrArray *aps) return sorted; } -static void -wifi_signal_to_color (guint8 strength, NMMetaTermColor *color, NMMetaTermFormat *color_fmt) +static NMMetaColor +wifi_signal_to_color (guint8 strength) { - *color = NM_META_TERM_COLOR_NORMAL; - *color_fmt = NM_META_TERM_FORMAT_NORMAL; - if (strength > 80) - *color = NM_META_TERM_COLOR_GREEN; + return NM_META_COLOR_WIFI_SIGNAL_EXCELLENT; else if (strength > 55) - *color = NM_META_TERM_COLOR_YELLOW; + return NM_META_COLOR_WIFI_SIGNAL_GOOD; else if (strength > 30) - *color = NM_META_TERM_COLOR_MAGENTA; + return NM_META_COLOR_WIFI_SIGNAL_FAIR; else if (strength > 5) - *color = NM_META_TERM_COLOR_CYAN; + return NM_META_COLOR_WIFI_SIGNAL_POOR; else - *color_fmt = NM_META_TERM_FORMAT_DIM; + return NM_META_COLOR_WIFI_SIGNAL_UNKNOWN; } static char * @@ -795,8 +792,7 @@ fill_output_access_point (gpointer data, gpointer user_data) GString *security_str; char *ap_name; const char *sig_bars; - NMMetaTermColor color; - NMMetaTermFormat color_fmt; + NMMetaColor color; if (info->active_bssid) { const char *current_bssid = nm_access_point_get_bssid (ap); @@ -882,11 +878,10 @@ fill_output_access_point (gpointer data, gpointer user_data) set_val_strc (arr, 16, nm_object_get_path (NM_OBJECT (ap))); /* Set colors */ - wifi_signal_to_color (strength, &color, &color_fmt); + color = wifi_signal_to_color (strength); set_val_color_all (arr, color); - set_val_color_fmt_all (arr, color_fmt); if (active) - arr[15].color = NM_META_TERM_COLOR_GREEN; + arr[15].color = NM_META_COLOR_CONNECTION_ACTIVATED; g_ptr_array_add (info->output_data, arr); @@ -1463,20 +1458,19 @@ show_device_info (NMDevice *device, NmCli *nmc) return TRUE; } -void -nmc_device_state_to_color (NMDeviceState state, NMMetaTermColor *color, NMMetaTermFormat *color_fmt) +NMMetaColor +nmc_device_state_to_color (NMDeviceState state) { - *color = NM_META_TERM_COLOR_NORMAL; - *color_fmt = NM_META_TERM_FORMAT_NORMAL; - if (state <= NM_DEVICE_STATE_UNAVAILABLE) - *color_fmt= NM_META_TERM_FORMAT_DIM; + return NM_META_COLOR_DEVICE_UNAVAILABLE; else if (state == NM_DEVICE_STATE_DISCONNECTED) - *color = NM_META_TERM_COLOR_RED; + return NM_META_COLOR_DEVICE_DISCONNECTED; else if (state >= NM_DEVICE_STATE_PREPARE && state <= NM_DEVICE_STATE_SECONDARIES) - *color = NM_META_TERM_COLOR_YELLOW; + return NM_META_COLOR_DEVICE_ACTIVATING; else if (state == NM_DEVICE_STATE_ACTIVATED) - *color = NM_META_TERM_COLOR_GREEN; + return NM_META_COLOR_DEVICE_ACTIVATED; + + g_return_val_if_reached (NM_META_COLOR_DEVICE_UNKNOWN); } static void @@ -1484,8 +1478,7 @@ fill_output_device_status (NMDevice *device, GPtrArray *output_data) { NMActiveConnection *ac; NMDeviceState state; - NMMetaTermColor color; - NMMetaTermFormat color_fmt; + NMMetaColor color; NmcOutputField *arr = nmc_dup_fields_array ((const NMMetaAbstractInfo *const*) nmc_fields_dev_status, 0); @@ -1493,9 +1486,8 @@ fill_output_device_status (NMDevice *device, GPtrArray *output_data) ac = nm_device_get_active_connection (device); /* Show devices in color */ - nmc_device_state_to_color (state, &color, &color_fmt); + color = nmc_device_state_to_color (state); set_val_color_all (arr, color); - set_val_color_fmt_all (arr, color_fmt); set_val_strc (arr, 0, nm_device_get_iface (device)); set_val_strc (arr, 1, nm_device_get_type_description (device)); @@ -2406,12 +2398,11 @@ static void device_state (NMDevice *device, GParamSpec *pspec, NmCli *nmc) { NMDeviceState state = nm_device_get_state (device); - NMMetaTermColor color; - NMMetaTermFormat color_fmt; + NMMetaColor color; char *str; - nmc_device_state_to_color (state, &color, &color_fmt); - str = nmc_colorize (&nmc->nmc_config, color, color_fmt, "%s: %s\n", + color = nmc_device_state_to_color (state); + str = nmc_colorize (&nmc->nmc_config, color, "%s: %s\n", nm_device_get_iface (device), nmc_device_state_to_string (state)); diff --git a/clients/cli/devices.h b/clients/cli/devices.h index 2c261bc7ab..c78680da34 100644 --- a/clients/cli/devices.h +++ b/clients/cli/devices.h @@ -14,7 +14,7 @@ * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * - * (C) Copyright 2010 Red Hat, Inc. + * (C) Copyright 2010 - 2018 Red Hat, Inc. */ #ifndef NMC_DEVICES_H @@ -32,7 +32,7 @@ void monitor_devices (NmCli *nmc); NMDevice ** nmc_get_devices_sorted (NMClient *client); -void nmc_device_state_to_color (NMDeviceState state, NMMetaTermColor *color, NMMetaTermFormat *color_fmt); +NMMetaColor nmc_device_state_to_color (NMDeviceState state); extern const NmcMetaGenericInfo *const nmc_fields_dev_status[]; extern const NmcMetaGenericInfo *const nmc_fields_dev_show_general[]; diff --git a/clients/cli/general.c b/clients/cli/general.c index 437ac9d165..f1a350c77d 100644 --- a/clients/cli/general.c +++ b/clients/cli/general.c @@ -55,23 +55,26 @@ nm_state_to_string (NMState state) return _(nm_state_to_string_no_l10n (state)); } -static NMMetaTermColor +static NMMetaColor state_to_color (NMState state) { switch (state) { case NM_STATE_CONNECTING: - return NM_META_TERM_COLOR_YELLOW; + return NM_META_COLOR_STATE_CONNECTING; case NM_STATE_CONNECTED_LOCAL: + return NM_META_COLOR_STATE_CONNECTED_LOCAL; case NM_STATE_CONNECTED_SITE: + return NM_META_COLOR_STATE_CONNECTED_SITE; case NM_STATE_CONNECTED_GLOBAL: - return NM_META_TERM_COLOR_GREEN; + return NM_META_COLOR_STATE_CONNECTED_GLOBAL; case NM_STATE_DISCONNECTING: - return NM_META_TERM_COLOR_YELLOW; + return NM_META_COLOR_STATE_DISCONNECTING; case NM_STATE_ASLEEP: + return NM_META_COLOR_STATE_ASLEEP; case NM_STATE_DISCONNECTED: - return NM_META_TERM_COLOR_RED; + return NM_META_COLOR_STATE_DISCONNECTED; default: - return NM_META_TERM_COLOR_NORMAL; + return NM_META_COLOR_STATE_UNKNOWN; } } @@ -90,19 +93,20 @@ nm_connectivity_to_string (NMConnectivityState connectivity) return _(nm_connectivity_to_string_no_l10n (connectivity)); } -static NMMetaTermColor +static NMMetaColor connectivity_to_color (NMConnectivityState connectivity) { switch (connectivity) { case NM_CONNECTIVITY_NONE: - return NM_META_TERM_COLOR_RED; + return NM_META_COLOR_CONNECTIVITY_NONE; case NM_CONNECTIVITY_PORTAL: + return NM_META_COLOR_CONNECTIVITY_PORTAL; case NM_CONNECTIVITY_LIMITED: - return NM_META_TERM_COLOR_YELLOW; + return NM_META_COLOR_CONNECTIVITY_LIMITED; case NM_CONNECTIVITY_FULL: - return NM_META_TERM_COLOR_GREEN; + return NM_META_COLOR_CONNECTIVITY_FULL; default: - return NM_META_TERM_COLOR_NORMAL; + return NM_META_COLOR_CONNECTIVITY_UNKNOWN; } } @@ -155,11 +159,11 @@ NM_UTILS_LOOKUP_STR_DEFINE_STATIC (permission_result_to_string_no_l10n, NMClient NM_UTILS_LOOKUP_ITEM_IGNORE (NM_CLIENT_PERMISSION_RESULT_UNKNOWN), ); -_NM_UTILS_LOOKUP_DEFINE (static, permission_result_to_color, NMClientPermissionResult, NMMetaTermColor, - NM_UTILS_LOOKUP_DEFAULT (NM_META_TERM_COLOR_NORMAL), - NM_UTILS_LOOKUP_ITEM (NM_CLIENT_PERMISSION_RESULT_YES, NM_META_TERM_COLOR_GREEN), - NM_UTILS_LOOKUP_ITEM (NM_CLIENT_PERMISSION_RESULT_NO, NM_META_TERM_COLOR_RED), - NM_UTILS_LOOKUP_ITEM (NM_CLIENT_PERMISSION_RESULT_AUTH, NM_META_TERM_COLOR_YELLOW), +_NM_UTILS_LOOKUP_DEFINE (static, permission_result_to_color, NMClientPermissionResult, NMMetaColor, + NM_UTILS_LOOKUP_DEFAULT (NM_META_COLOR_PERMISSION_UNKNOWN), + NM_UTILS_LOOKUP_ITEM (NM_CLIENT_PERMISSION_RESULT_YES, NM_META_COLOR_PERMISSION_YES), + NM_UTILS_LOOKUP_ITEM (NM_CLIENT_PERMISSION_RESULT_NO, NM_META_COLOR_PERMISSION_NO), + NM_UTILS_LOOKUP_ITEM (NM_CLIENT_PERMISSION_RESULT_AUTH, NM_META_COLOR_PERMISSION_AUTH), NM_UTILS_LOOKUP_ITEM_IGNORE (NM_CLIENT_PERMISSION_RESULT_UNKNOWN), ); @@ -186,26 +190,26 @@ _metagen_general_status_get_fcn (const NMMetaEnvironment *environment, switch (info->info_type) { case NMC_GENERIC_INFO_TYPE_GENERAL_STATUS_RUNNING: - NMC_HANDLE_TERMFORMAT (NM_META_TERM_COLOR_NORMAL); + NMC_HANDLE_COLOR (NM_META_COLOR_NONE); value = N_("running"); goto translate_and_out; case NMC_GENERIC_INFO_TYPE_GENERAL_STATUS_VERSION: - NMC_HANDLE_TERMFORMAT (NM_META_TERM_COLOR_NORMAL); + NMC_HANDLE_COLOR (NM_META_COLOR_NONE); value = nm_client_get_version (nmc->client); goto clone_and_out; case NMC_GENERIC_INFO_TYPE_GENERAL_STATUS_STATE: state = nm_client_get_state (nmc->client); - NMC_HANDLE_TERMFORMAT (state_to_color (state)); + NMC_HANDLE_COLOR (state_to_color (state)); value = nm_state_to_string_no_l10n (state); goto translate_and_out; case NMC_GENERIC_INFO_TYPE_GENERAL_STATUS_STARTUP: v_bool = nm_client_get_startup (nmc->client); - NMC_HANDLE_TERMFORMAT (v_bool ? NM_META_TERM_COLOR_YELLOW : NM_META_TERM_COLOR_GREEN); + NMC_HANDLE_COLOR (v_bool ? NM_META_COLOR_MANAGER_STARTING : NM_META_COLOR_MANAGER_RUNNING); value = v_bool ? N_("starting") : N_("started"); goto translate_and_out; case NMC_GENERIC_INFO_TYPE_GENERAL_STATUS_CONNECTIVITY: connectivity = nm_client_get_connectivity (nmc->client); - NMC_HANDLE_TERMFORMAT (connectivity_to_color (connectivity)); + NMC_HANDLE_COLOR (connectivity_to_color (connectivity)); value = nm_connectivity_to_string_no_l10n (connectivity); goto translate_and_out; case NMC_GENERIC_INFO_TYPE_GENERAL_STATUS_NETWORKING: @@ -234,7 +238,7 @@ _metagen_general_status_get_fcn (const NMMetaEnvironment *environment, g_return_val_if_reached (NULL); enabled_out: - NMC_HANDLE_TERMFORMAT (v_bool ? NM_META_TERM_COLOR_GREEN : NM_META_TERM_COLOR_RED); + NMC_HANDLE_COLOR (v_bool ? NM_META_COLOR_ENABLED : NM_META_COLOR_DISABLED); value = v_bool ? N_("enabled") : N_("disabled"); goto translate_and_out; @@ -293,11 +297,11 @@ _metagen_general_permissions_get_fcn (const NMMetaEnvironment *environment, switch (info->info_type) { case NMC_GENERIC_INFO_TYPE_GENERAL_PERMISSIONS_PERMISSION: - NMC_HANDLE_TERMFORMAT (NM_META_TERM_COLOR_NORMAL); + NMC_HANDLE_COLOR (NM_META_COLOR_NONE); return permission_to_string (perm); case NMC_GENERIC_INFO_TYPE_GENERAL_PERMISSIONS_VALUE: perm_result = nm_client_get_permission_result (nmc->client, perm); - NMC_HANDLE_TERMFORMAT (permission_result_to_color (perm_result)); + NMC_HANDLE_COLOR (permission_result_to_color (perm_result)); s = permission_result_to_string_no_l10n (perm_result); if (get_type == NM_META_ACCESSOR_GET_TYPE_PRETTY) return _(s); @@ -340,7 +344,7 @@ _metagen_general_logging_get_fcn (const NMMetaEnvironment *environment, nm_assert (info->info_type < _NMC_GENERIC_INFO_TYPE_GENERAL_LOGGING_NUM); - NMC_HANDLE_TERMFORMAT (NM_META_TERM_COLOR_NORMAL); + NMC_HANDLE_COLOR (NM_META_COLOR_NONE); if (!d->initialized) { d->initialized = TRUE; @@ -1087,8 +1091,7 @@ networkmanager_running (NMClient *client, GParamSpec *param, NmCli *nmc) running = nm_client_get_nm_running (client); str = nmc_colorize (&nmc->nmc_config, - running ? NM_META_TERM_COLOR_GREEN : NM_META_TERM_COLOR_RED, - NM_META_TERM_FORMAT_NORMAL, + running ? NM_META_COLOR_MANAGER_RUNNING : NM_META_COLOR_MANAGER_STOPPED, running ? _("NetworkManager has started") : _("NetworkManager has stopped")); g_print ("%s\n", str); g_free (str); @@ -1128,7 +1131,7 @@ client_connectivity (NMClient *client, GParamSpec *param, NmCli *nmc) char *str; g_object_get (client, NM_CLIENT_CONNECTIVITY, &connectivity, NULL); - str = nmc_colorize (&nmc->nmc_config, connectivity_to_color (connectivity), NM_META_TERM_FORMAT_NORMAL, + str = nmc_colorize (&nmc->nmc_config, connectivity_to_color (connectivity), _("Connectivity is now '%s'\n"), nm_connectivity_to_string (connectivity)); g_print ("%s", str); g_free (str); @@ -1141,7 +1144,7 @@ client_state (NMClient *client, GParamSpec *param, NmCli *nmc) char *str; g_object_get (client, NM_CLIENT_STATE, &state, NULL); - str = nmc_colorize (&nmc->nmc_config, state_to_color (state), NM_META_TERM_FORMAT_NORMAL, + str = nmc_colorize (&nmc->nmc_config, state_to_color (state), _("Networkmanager is now in the '%s' state\n"), nm_state_to_string (state)); g_print ("%s", str); @@ -1184,12 +1187,12 @@ device_overview (NmCli *nmc, NMDevice *device) if (!nm_device_get_autoconnect (device)) g_string_append_printf (outbuf, "%s, ", _("autoconnect")); if (nm_device_get_firmware_missing (device)) { - tmp = nmc_colorize (&nmc->nmc_config, NM_META_TERM_COLOR_RED, NM_META_TERM_FORMAT_NORMAL, _("fw missing")); + tmp = nmc_colorize (&nmc->nmc_config, NM_META_COLOR_DEVICE_FIRMWARE_MISSING, _("fw missing")); g_string_append_printf (outbuf, "%s, ", tmp); g_free (tmp); } if (nm_device_get_nm_plugin_missing (device)) { - tmp = nmc_colorize (&nmc->nmc_config, NM_META_TERM_COLOR_RED, NM_META_TERM_FORMAT_NORMAL, _("plugin missing")); + tmp = nmc_colorize (&nmc->nmc_config, NM_META_COLOR_DEVICE_PLUGIN_MISSING, _("plugin missing")); g_string_append_printf (outbuf, "%s, ", tmp); g_free (tmp); } @@ -1290,7 +1293,7 @@ do_overview (NmCli *nmc, int argc, char **argv) NMDevice **devices; const GPtrArray *p; NMActiveConnection *ac; - NMMetaTermColor color; + NMMetaColor color; NMDnsEntry *dns; char *tmp; int i; @@ -1314,8 +1317,8 @@ do_overview (NmCli *nmc, int argc, char **argv) continue; state = nm_active_connection_get_state (ac); - nmc_active_connection_state_to_color (state, &color); - tmp = nmc_colorize (&nmc->nmc_config, color, NM_META_TERM_FORMAT_NORMAL, _("%s VPN connection"), + color = nmc_active_connection_state_to_color (state); + tmp = nmc_colorize (&nmc->nmc_config, color, _("%s VPN connection"), nm_active_connection_get_id (ac)); g_print ("%s\n", tmp); g_free (tmp); @@ -1326,14 +1329,13 @@ do_overview (NmCli *nmc, int argc, char **argv) devices = nmc_get_devices_sorted (nmc->client); for (i = 0; devices[i]; i++) { - NMMetaTermFormat color_fmt; NMDeviceState state; ac = nm_device_get_active_connection (devices[i]); state = nm_device_get_state (devices[i]); - nmc_device_state_to_color (state, &color, &color_fmt); - tmp = nmc_colorize (&nmc->nmc_config, color, color_fmt, "%s: %s%s%s", + color = nmc_device_state_to_color (state); + tmp = nmc_colorize (&nmc->nmc_config, color, "%s: %s%s%s", nm_device_get_iface (devices[i]), nmc_device_state_to_string (state), ac ? " to " : "", @@ -1416,7 +1418,7 @@ do_monitor (NmCli *nmc, int argc, char **argv) if (!nm_client_get_nm_running (nmc->client)) { char *str; - str = nmc_colorize (&nmc->nmc_config, NM_META_TERM_COLOR_RED, NM_META_TERM_FORMAT_NORMAL, + str = nmc_colorize (&nmc->nmc_config, NM_META_COLOR_MANAGER_STOPPED, _("Networkmanager is not running (waiting for it)\n")); g_print ("%s", str); g_free (str); diff --git a/clients/cli/nmcli.c b/clients/cli/nmcli.c index fe9e1c12fb..89d474c39b 100644 --- a/clients/cli/nmcli.c +++ b/clients/cli/nmcli.c @@ -73,6 +73,42 @@ NmCli nm_cli = { .complete = FALSE, .nmc_config.show_secrets = FALSE, .nmc_config.in_editor = FALSE, + .nmc_config.palette = { + [NM_META_COLOR_CONNECTION_ACTIVATED] = "32", + [NM_META_COLOR_CONNECTION_ACTIVATING] = "33", + [NM_META_COLOR_CONNECTION_DISCONNECTING] = "31", + [NM_META_COLOR_CONNECTION_INVISIBLE] = "2", + [NM_META_COLOR_CONNECTIVITY_FULL] = "32", + [NM_META_COLOR_CONNECTIVITY_LIMITED] = "33", + [NM_META_COLOR_CONNECTIVITY_NONE] = "31", + [NM_META_COLOR_CONNECTIVITY_PORTAL] = "33", + [NM_META_COLOR_DEVICE_ACTIVATED] = "32", + [NM_META_COLOR_DEVICE_ACTIVATING] = "33", + [NM_META_COLOR_DEVICE_DISCONNECTED] = "31", + [NM_META_COLOR_DEVICE_FIRMWARE_MISSING] = "31", + [NM_META_COLOR_DEVICE_PLUGIN_MISSING] = "31", + [NM_META_COLOR_DEVICE_UNAVAILABLE] = "2", + [NM_META_COLOR_MANAGER_RUNNING] = "32", + [NM_META_COLOR_MANAGER_STARTING] = "33", + [NM_META_COLOR_MANAGER_STOPPED] = "31", + [NM_META_COLOR_PERMISSION_AUTH] = "33", + [NM_META_COLOR_PERMISSION_NO] = "31", + [NM_META_COLOR_PERMISSION_YES] = "32", + [NM_META_COLOR_STATE_ASLEEP] = "31", + [NM_META_COLOR_STATE_CONNECTED_GLOBAL] = "32", + [NM_META_COLOR_STATE_CONNECTED_LOCAL] = "32", + [NM_META_COLOR_STATE_CONNECTED_SITE] = "32", + [NM_META_COLOR_STATE_CONNECTING] = "33", + [NM_META_COLOR_STATE_DISCONNECTED] = "31", + [NM_META_COLOR_STATE_DISCONNECTING] = "33", + [NM_META_COLOR_WIFI_SIGNAL_EXCELLENT] = "32", + [NM_META_COLOR_WIFI_SIGNAL_FAIR] = "35", + [NM_META_COLOR_WIFI_SIGNAL_GOOD] = "33", + [NM_META_COLOR_WIFI_SIGNAL_POOR] = "36", + [NM_META_COLOR_WIFI_SIGNAL_UNKNOWN] = "2", + [NM_META_COLOR_ENABLED] = "32", + [NM_META_COLOR_DISABLED] = "31", + }, .editor_status_line = FALSE, .editor_save_confirmation = TRUE, }; diff --git a/clients/cli/nmcli.h b/clients/cli/nmcli.h index 66a1122f83..55ca1d30ab 100644 --- a/clients/cli/nmcli.h +++ b/clients/cli/nmcli.h @@ -93,8 +93,7 @@ struct _NmcOutputField { gboolean value_is_array; /* Whether value is char** instead of char* */ gboolean free_value; /* Whether to free the value */ NmcOfFlags flags; /* Flags - whether and how to print values/field names/headers */ - NMMetaTermColor color; /* Use this color to print value */ - NMMetaTermFormat color_fmt; /* Use this terminal format to print value */ + NMMetaColor color; /* Use this color to print value */ }; typedef struct _NmcConfig { @@ -105,6 +104,7 @@ typedef struct _NmcConfig { bool in_editor; /* Whether running the editor - nmcli con edit' */ bool show_secrets; /* Whether to display secrets (both input and output): option '--show-secrets' */ bool overview; /* Overview mode (hide default values) */ + const char *palette[_NM_META_COLOR_NUM]; /* Color palette */ } NmcConfig; typedef struct _NmcOutputData { diff --git a/clients/cli/utils.c b/clients/cli/utils.c index f1cd222da1..2c0e3b57d7 100644 --- a/clients/cli/utils.c +++ b/clients/cli/utils.c @@ -86,11 +86,11 @@ _meta_type_nmc_generic_info_get_fcn (const NMMetaAbstractInfo *abstract_info, if (!NM_IN_SET (get_type, NM_META_ACCESSOR_GET_TYPE_PARSABLE, NM_META_ACCESSOR_GET_TYPE_PRETTY, - NM_META_ACCESSOR_GET_TYPE_TERMFORMAT)) + NM_META_ACCESSOR_GET_TYPE_COLOR)) g_return_val_if_reached (NULL); - /* omitting the out_to_free value is only allowed for TERMFORMAT. */ - nm_assert (out_to_free || NM_IN_SET (get_type, NM_META_ACCESSOR_GET_TYPE_TERMFORMAT)); + /* omitting the out_to_free value is only allowed for COLOR. */ + nm_assert (out_to_free || NM_IN_SET (get_type, NM_META_ACCESSOR_GET_TYPE_COLOR)); if (info->get_fcn) { return info->get_fcn (environment, environment_user_data, @@ -103,7 +103,7 @@ _meta_type_nmc_generic_info_get_fcn (const NMMetaAbstractInfo *abstract_info, } if (info->nested) { - NMC_HANDLE_TERMFORMAT (NM_META_TERM_COLOR_NORMAL); + NMC_HANDLE_COLOR (NM_META_COLOR_NONE); return info->name; } @@ -121,17 +121,14 @@ const NMMetaType nmc_meta_type_generic_info = { static const char * colorize_string (const NmcConfig *nmc_config, - NMMetaTermColor color, - NMMetaTermFormat color_fmt, + NMMetaColor color, const char *str, char **out_to_free) { const char *out = str; - if ( nmc_config - && nmc_config->use_colors - && (color != NM_META_TERM_COLOR_NORMAL || color_fmt != NM_META_TERM_FORMAT_NORMAL)) { - *out_to_free = nmc_colorize (nmc_config, color, color_fmt, "%s", str); + if (nmc_config && nmc_config->use_colors) { + *out_to_free = nmc_colorize (nmc_config, color, "%s", str); out = *out_to_free; } @@ -378,89 +375,24 @@ nmc_terminal_show_progress (const char *str) idx = 0; } -const char * -nmc_term_color_sequence (NMMetaTermColor color) -{ - switch (color) { - case NM_META_TERM_COLOR_BLACK: - return "\33[30m"; - break; - case NM_META_TERM_COLOR_RED: - return "\33[31m"; - break; - case NM_META_TERM_COLOR_GREEN: - return "\33[32m"; - break; - case NM_META_TERM_COLOR_YELLOW: - return "\33[33m"; - break; - case NM_META_TERM_COLOR_BLUE: - return "\33[34m"; - break; - case NM_META_TERM_COLOR_MAGENTA: - return "\33[35m"; - break; - case NM_META_TERM_COLOR_CYAN: - return "\33[36m"; - break; - case NM_META_TERM_COLOR_WHITE: - return "\33[37m"; - break; - default: - return ""; - break; - } -} - -const char * -nmc_term_format_sequence (NMMetaTermFormat format) -{ - switch (format) { - case NM_META_TERM_FORMAT_BOLD: - return "\33[1m"; - break; - case NM_META_TERM_FORMAT_DIM: - return "\33[2m"; - break; - case NM_META_TERM_FORMAT_UNDERLINE: - return "\33[4m"; - break; - case NM_META_TERM_FORMAT_BLINK: - return "\33[5m"; - break; - case NM_META_TERM_FORMAT_REVERSE: - return "\33[7m"; - break; - case NM_META_TERM_FORMAT_HIDDEN: - return "\33[8m"; - break; - default: - return ""; - break; - } -} - char * -nmc_colorize (const NmcConfig *nmc_config, NMMetaTermColor color, NMMetaTermFormat format, const char *fmt, ...) +nmc_colorize (const NmcConfig *nmc_config, NMMetaColor color, const char *fmt, ...) { va_list args; char *str, *colored; - const char *ansi_color, *color_end, *ansi_fmt, *format_end; - static const char *end_seq = "\33[0m"; + const char *ansi_seq = NULL; va_start (args, fmt); str = g_strdup_vprintf (fmt, args); va_end (args); - if (!nmc_config->use_colors) + if (nmc_config->use_colors) + ansi_seq = nmc_config->palette[color]; + + if (ansi_seq == NULL) return str; - ansi_color = nmc_term_color_sequence (color); - ansi_fmt = nmc_term_format_sequence (format); - color_end = *ansi_color ? end_seq : ""; - format_end = *ansi_fmt ? end_seq : ""; - - colored = g_strdup_printf ("%s%s%s%s%s", ansi_fmt, ansi_color, str, color_end, format_end); + colored = g_strdup_printf ("\33[%sm%s\33[0m", ansi_seq, str); g_free (str); return colored; } @@ -677,7 +609,7 @@ set_val_arrc (NmcOutputField fields_array[], guint32 idx, const char **value) } void -set_val_color_all (NmcOutputField fields_array[], NMMetaTermColor color) +set_val_color_all (NmcOutputField fields_array[], NMMetaColor color) { int i; @@ -686,16 +618,6 @@ set_val_color_all (NmcOutputField fields_array[], NMMetaTermColor color) } } -void -set_val_color_fmt_all (NmcOutputField fields_array[], NMMetaTermFormat format) -{ - int i; - - for (i = 0; fields_array[i].info; i++) { - fields_array[i].color_fmt = format; - } -} - /* * Free 'value' members in array of NmcOutputField */ @@ -974,8 +896,7 @@ typedef enum { typedef struct { guint row_idx; const PrintDataHeaderCell *header_cell; - NMMetaTermColor term_color; - NMMetaTermFormat term_format; + NMMetaColor color; union { const char *plain; const char *const*strv; @@ -1138,17 +1059,15 @@ _print_fill (const NmcConfig *nmc_config, cell->text_to_free = !!to_free; } - nm_meta_termformat_unpack (nm_meta_abstract_info_get (info, - nmc_meta_environment, - nmc_meta_environment_arg, - target, - NM_META_ACCESSOR_GET_TYPE_TERMFORMAT, - NM_META_ACCESSOR_GET_FLAGS_NONE, - &color_out_flags, - NULL, - NULL), - &cell->term_color, - &cell->term_format); + cell->color = GPOINTER_TO_INT (nm_meta_abstract_info_get (info, + nmc_meta_environment, + nmc_meta_environment_arg, + target, + NM_META_ACCESSOR_GET_TYPE_COLOR, + NM_META_ACCESSOR_GET_FLAGS_NONE, + &color_out_flags, + NULL, + NULL)); if (cell->text_format == PRINT_DATA_CELL_FORMAT_TYPE_PLAIN) { if (pretty && (!cell->text.plain|| !cell->text.plain[0])) { @@ -1333,9 +1252,7 @@ _print_do (const NmcConfig *nmc_config, gs_free char *text_to_free = NULL; const char *text; - text = colorize_string (nmc_config, - cell->term_color, cell->term_format, - lines[i_lines], &text_to_free); + text = colorize_string (nmc_config, cell->color, lines[i_lines], &text_to_free); if (multiline) { gs_free char *prefix = NULL; @@ -1561,7 +1478,7 @@ get_value_to_print (const NmcConfig *nmc_config, } /* colorize the value */ - out = colorize_string (nmc_config, field->color, field->color_fmt, value, out_to_free); + out = colorize_string (nmc_config, field->color, value, out_to_free); if (out && out == free_value) { nm_assert (!*out_to_free); @@ -1655,7 +1572,7 @@ print_required_fields (const NmcConfig *nmc_config, gs_free char *tmp = NULL; val = *p ?: not_set_str; - print_val = colorize_string (nmc_config, field_values[idx].color, field_values[idx].color_fmt, + print_val = colorize_string (nmc_config, field_values[idx].color, val, &val_to_free); tmp = g_strdup_printf ("%s%s%s[%d]:", section_prefix ? (const char*) field_values[0].value : "", @@ -1676,7 +1593,7 @@ print_required_fields (const NmcConfig *nmc_config, /* value is a string */ val = val && *val ? val : not_set_str; - print_val = colorize_string (nmc_config, field_values[idx].color, field_values[idx].color_fmt, + print_val = colorize_string (nmc_config, field_values[idx].color, val, &val_to_free); tmp = g_strdup_printf ("%s%s%s:", section_prefix ? hdr_name : "", diff --git a/clients/cli/utils.h b/clients/cli/utils.h index e73973baec..883e73188c 100644 --- a/clients/cli/utils.h +++ b/clients/cli/utils.h @@ -41,9 +41,7 @@ char *ssid_to_hex (const char *str, gsize len); void nmc_terminal_erase_line (void); void nmc_terminal_show_progress (const char *str); void nmc_terminal_spawn_pager (const NmcConfig *nmc_config); -const char *nmc_term_color_sequence (NMMetaTermColor color); -const char *nmc_term_format_sequence (NMMetaTermFormat format); -char *nmc_colorize (const NmcConfig *nmc_config, NMMetaTermColor color, NMMetaTermFormat format, const char * fmt, ...) _nm_printf (4, 5); +char *nmc_colorize (const NmcConfig *nmc_config, NMMetaColor color, const char * fmt, ...) _nm_printf (3, 4); void nmc_filter_out_colors_inplace (char *str); char *nmc_filter_out_colors (const char *str); char *nmc_get_user_input (const char *ask_str); @@ -56,8 +54,7 @@ void set_val_str (NmcOutputField fields_array[], guint32 index, char *value); void set_val_strc (NmcOutputField fields_array[], guint32 index, const char *value); void set_val_arr (NmcOutputField fields_array[], guint32 index, char **value); void set_val_arrc (NmcOutputField fields_array[], guint32 index, const char **value); -void set_val_color_all (NmcOutputField fields_array[], NMMetaTermColor color); -void set_val_color_fmt_all (NmcOutputField fields_array[], NMMetaTermFormat format); +void set_val_color_all (NmcOutputField fields_array[], NMMetaColor color); void nmc_free_output_field_values (NmcOutputField fields_array[]); GArray *parse_output_fields (const char *fields_str, @@ -126,10 +123,10 @@ typedef enum { } NmcGenericInfoType; -#define NMC_HANDLE_TERMFORMAT(color) \ +#define NMC_HANDLE_COLOR(color) \ G_STMT_START { \ - if (get_type == NM_META_ACCESSOR_GET_TYPE_TERMFORMAT) \ - return nm_meta_termformat_pack ((color), NM_META_TERM_FORMAT_NORMAL); \ + if (get_type == NM_META_ACCESSOR_GET_TYPE_COLOR) \ + return GINT_TO_POINTER (color); \ } G_STMT_END struct _NmcMetaGenericInfo { diff --git a/clients/common/nm-meta-setting-desc.h b/clients/common/nm-meta-setting-desc.h index 3bacaed93d..e7ab6e860a 100644 --- a/clients/common/nm-meta-setting-desc.h +++ b/clients/common/nm-meta-setting-desc.h @@ -14,7 +14,7 @@ * 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. + * Copyright 2010 - 2018 Red Hat, Inc. */ #ifndef __NM_META_SETTING_DESC_H__ @@ -83,31 +83,54 @@ struct _NMDevice; #define NM_META_TEXT_PROMPT_PROXY_METHOD_CHOICES "(" NM_META_TEXT_WORD_NONE "/" NM_META_TEXT_WORD_AUTO ") [" NM_META_TEXT_WORD_NONE "]" typedef enum { - NM_META_TERM_COLOR_NORMAL = 0, - NM_META_TERM_COLOR_BLACK = 1, - NM_META_TERM_COLOR_RED = 2, - NM_META_TERM_COLOR_GREEN = 3, - NM_META_TERM_COLOR_YELLOW = 4, - NM_META_TERM_COLOR_BLUE = 5, - NM_META_TERM_COLOR_MAGENTA = 6, - NM_META_TERM_COLOR_CYAN = 7, - NM_META_TERM_COLOR_WHITE = 8, -} NMMetaTermColor; - -typedef enum { - NM_META_TERM_FORMAT_NORMAL = 0, - NM_META_TERM_FORMAT_BOLD = 1, - NM_META_TERM_FORMAT_DIM = 2, - NM_META_TERM_FORMAT_UNDERLINE = 3, - NM_META_TERM_FORMAT_BLINK = 4, - NM_META_TERM_FORMAT_REVERSE = 5, - NM_META_TERM_FORMAT_HIDDEN = 6, -} NMMetaTermFormat; + NM_META_COLOR_NONE = 0, + NM_META_COLOR_CONNECTION_ACTIVATED, + NM_META_COLOR_CONNECTION_ACTIVATING, + NM_META_COLOR_CONNECTION_DISCONNECTING, + NM_META_COLOR_CONNECTION_INVISIBLE, + NM_META_COLOR_CONNECTION_UNKNOWN, + NM_META_COLOR_CONNECTIVITY_FULL, + NM_META_COLOR_CONNECTIVITY_LIMITED, + NM_META_COLOR_CONNECTIVITY_NONE, + NM_META_COLOR_CONNECTIVITY_PORTAL, + NM_META_COLOR_CONNECTIVITY_UNKNOWN, + NM_META_COLOR_DEVICE_ACTIVATED, + NM_META_COLOR_DEVICE_ACTIVATING, + NM_META_COLOR_DEVICE_DISCONNECTED, + NM_META_COLOR_DEVICE_FIRMWARE_MISSING, + NM_META_COLOR_DEVICE_PLUGIN_MISSING, + NM_META_COLOR_DEVICE_UNAVAILABLE, + NM_META_COLOR_DEVICE_UNKNOWN, + NM_META_COLOR_MANAGER_RUNNING, + NM_META_COLOR_MANAGER_STARTING, + NM_META_COLOR_MANAGER_STOPPED, + NM_META_COLOR_PERMISSION_AUTH, + NM_META_COLOR_PERMISSION_NO, + NM_META_COLOR_PERMISSION_UNKNOWN, + NM_META_COLOR_PERMISSION_YES, + NM_META_COLOR_PROMPT, + NM_META_COLOR_STATE_ASLEEP, + NM_META_COLOR_STATE_CONNECTED_GLOBAL, + NM_META_COLOR_STATE_CONNECTED_LOCAL, + NM_META_COLOR_STATE_CONNECTED_SITE, + NM_META_COLOR_STATE_CONNECTING, + NM_META_COLOR_STATE_DISCONNECTED, + NM_META_COLOR_STATE_DISCONNECTING, + NM_META_COLOR_STATE_UNKNOWN, + NM_META_COLOR_WIFI_SIGNAL_EXCELLENT, + NM_META_COLOR_WIFI_SIGNAL_FAIR, + NM_META_COLOR_WIFI_SIGNAL_GOOD, + NM_META_COLOR_WIFI_SIGNAL_POOR, + NM_META_COLOR_WIFI_SIGNAL_UNKNOWN, + NM_META_COLOR_DISABLED, + NM_META_COLOR_ENABLED, + _NM_META_COLOR_NUM +} NMMetaColor; typedef enum { NM_META_ACCESSOR_GET_TYPE_PRETTY, NM_META_ACCESSOR_GET_TYPE_PARSABLE, - NM_META_ACCESSOR_GET_TYPE_TERMFORMAT, + NM_META_ACCESSOR_GET_TYPE_COLOR, } NMMetaAccessorGetType; typedef enum { @@ -115,32 +138,6 @@ typedef enum { NM_META_ACCESSOR_SETTING_INIT_TYPE_CLI, } NMMetaAccessorSettingInitType; -static inline void -nm_meta_termformat_unpack (gconstpointer value, NMMetaTermColor *out_color, NMMetaTermFormat *out_format) -{ - /* get_fcn() with NM_META_ACCESSOR_GET_TYPE_TERMFORMAT returns a pointer - * that encodes NMMetaTermColor and NMMetaTermFormat. Unpack it. */ - if (!value) { - /* by default, objects that don't support NM_META_ACCESSOR_GET_TYPE_TERMFORMAT - * return NULL. This allows for an explicit fallback value here... */ - NM_SET_OUT (out_color, NM_META_TERM_COLOR_NORMAL); - NM_SET_OUT (out_format, NM_META_TERM_FORMAT_NORMAL); - } else { - NM_SET_OUT (out_color, GPOINTER_TO_UINT (value) & 0xFF); - NM_SET_OUT (out_format, (GPOINTER_TO_UINT (value) & 0xFF00) >> 8); - } -} - -static inline gconstpointer -nm_meta_termformat_pack (NMMetaTermColor color, NMMetaTermFormat format) -{ - /* get_fcn() with NM_META_ACCESSOR_GET_TYPE_TERMFORMAT returns a pointer - * that encodes NMMetaTermColor and NMMetaTermFormat. Pack it. */ - return GUINT_TO_POINTER (((guint) 0x10000) | (((guint) color) & 0xFFu) | ((((guint) format) & 0xFFu) << 8)); -} - -#define NM_META_TERMFORMAT_DEFAULT() (nm_meta_termformat_pack (NM_META_TERM_COLOR_NORMAL, NM_META_TERM_FORMAT_NORMAL)) - typedef enum { NM_META_ACCESSOR_GET_FLAGS_NONE = 0, NM_META_ACCESSOR_GET_FLAGS_ACCEPT_STRV = (1LL << 0), From bcc9e58bfe094cf1d8f865c9ced2ec74385c2192 Mon Sep 17 00:00:00 2001 From: Lubomir Rintel Date: Thu, 29 Mar 2018 12:11:07 +0200 Subject: [PATCH 9/9] cli: allow setting the colors with terminal-colors.d(5) The present version of the specification is somewhat unclear at times, Unclear points were discussed with the maintainers [1] and probably some new version will address those. https://www.spinics.net/lists/util-linux-ng/msg15222.html Until then here's how the implementation copes with ambiguities (after the discussion with util-linux maintainers): 1.) It is unclear whether multiple .schem files should override each other or be merged. We use the overriding behavior -- take the highest priority one and ignore the rest. 2.) We assume "name.schem" is more specific than "@term.schem". 3.) We assume the "Color name" are to be used as aliases for the color sequences and translate them to ANSI escape sequences. 4.) The "Escape sequences" are of no use since the specification pretty much assumes an ANSI terminal and none of the sequences make any sense in ANSI color codes. We don't support them. accept that. 5.) We don't implement TERMINAL_COLORS_DEBUG because it's unspecified what should it do. --- Makefile.am | 1 + clients/cli/connections.c | 2 +- clients/cli/meson.build | 1 + clients/cli/nmcli.c | 281 ++++++++++++++++++++++++++++++++++++-- clients/cli/nmcli.h | 2 + man/nmcli.xml | 272 +++++++++++++++++++++++++++++++++++- 6 files changed, 548 insertions(+), 11 deletions(-) diff --git a/Makefile.am b/Makefile.am index 26a6b8863e..808ed91160 100644 --- a/Makefile.am +++ b/Makefile.am @@ -3514,6 +3514,7 @@ clients_cli_nmcli_CPPFLAGS = \ -I$(srcdir)/clients/cli \ $(clients_cppflags) \ $(SANITIZER_EXEC_CFLAGS) \ + -DSYSCONFDIR=\"$(sysconfdir)\" \ -DG_LOG_DOMAIN=\""nmcli"\" \ -DNETWORKMANAGER_COMPILATION=NM_NETWORKMANAGER_COMPILATION_CLIENT \ -DNMCLI_LOCALEDIR=\"$(datadir)/locale\" diff --git a/clients/cli/connections.c b/clients/cli/connections.c index 8c4995845f..5fb78d467d 100644 --- a/clients/cli/connections.c +++ b/clients/cli/connections.c @@ -7538,7 +7538,7 @@ editor_menu_main (NmCli *nmc, NMConnection *connection, const char *connection_t } else nmc->nmc_config_mutable.show_secrets = bb; } else if (cmd_arg_p && matches (cmd_arg_p, "prompt-color")) { - g_debug ("Ignoring erroneous --prompt-color argument.\n"); + g_debug ("Ignoring erroneous --prompt-color argument. Use terminal-colors.d(5) to set the prompt color.\n"); } else if (!cmd_arg_p) { g_print (_("Current nmcli configuration:\n")); g_print ("status-line: %s\n" diff --git a/clients/cli/meson.build b/clients/cli/meson.build index 65317a0ac9..b7f5e19360 100644 --- a/clients/cli/meson.build +++ b/clients/cli/meson.build @@ -27,6 +27,7 @@ deps = [ ] cflags = clients_cflags + [ + '-DSYSCONFDIR="@0@"'.format(nm_sysconfdir), '-DG_LOG_DOMAIN="@0@"'.format(name), '-DNMCLI_LOCALEDIR="@0@"'.format(nm_localedir) ] diff --git a/clients/cli/nmcli.c b/clients/cli/nmcli.c index 89d474c39b..24fa37c638 100644 --- a/clients/cli/nmcli.c +++ b/clients/cli/nmcli.c @@ -329,22 +329,276 @@ matches_arg (NmCli *nmc, int *argc, char ***argv, const char *pattern, char **ar return TRUE; } +/*************************************************************************************/ + typedef enum { NMC_USE_COLOR_AUTO, NMC_USE_COLOR_YES, NMC_USE_COLOR_NO, } NmcColorOption; -static void -set_colors (NmCli *nmc, NmcColorOption *color_option) +/* Checks whether a particular terminal-colors.d(5) file (.enabled, .disabled or .schem) + * exists. If contents is non-NULL, it returns the content. */ +static gboolean +check_colors_file (NmCli *nmc, NmcColorOption *color_option, + const char *base_dir, const char *name, const char *term, const char *type, + char **contents) { - if (*color_option == NMC_USE_COLOR_AUTO) { - if ( g_strcmp0 (g_getenv ("TERM"), "dumb") == 0 - || !isatty (STDOUT_FILENO)) - *color_option = NMC_USE_COLOR_NO; + char *filename; + gboolean exists; + + filename = g_strdup_printf ("%s/terminal-colors.d/%s%s%s%s%s", + base_dir, + name ? name : "", + term ? "@" : "", term ? term : "", + (name || term) ? "." : "", + type); + if (contents) + exists = g_file_get_contents (filename, contents, NULL, NULL); + else + exists = g_file_test (filename, G_FILE_TEST_EXISTS); + g_free (filename); + + return exists; +} + +static void +check_colors_files_for_term (NmCli *nmc, NmcColorOption *color_option, + const char *base_dir, const char *name, const char *term) +{ + if ( *color_option == NMC_USE_COLOR_AUTO + && check_colors_file (nmc, color_option, base_dir, name, term, "enable", NULL)) { + *color_option = NMC_USE_COLOR_YES; } - switch (*color_option) { + if ( *color_option == NMC_USE_COLOR_AUTO + && check_colors_file (nmc, color_option, base_dir, name, term, "disable", NULL)) { + *color_option = NMC_USE_COLOR_NO; + } + + if (*color_option == NMC_USE_COLOR_NO) { + /* No need to bother any further. */ + return; + } + + if (nmc->palette_buffer == NULL) + check_colors_file (nmc, color_option, base_dir, name, term, "schem", &nmc->palette_buffer); +} + +static void +check_colors_files_for_name (NmCli *nmc, NmcColorOption *color_option, + const char *base_dir, const char *name) +{ + const gchar *term; + + /* Take a shortcut if the directory is not there. */ + if (!g_file_test (base_dir, G_FILE_TEST_EXISTS)) + return; + + term = g_getenv ("TERM"); + if (term) + check_colors_files_for_term (nmc, color_option, base_dir, name, term); + check_colors_files_for_term (nmc, color_option, base_dir, name, NULL); +} + +static void +check_colors_files_for_base_dir (NmCli *nmc, NmcColorOption *color_option, + const char *base_dir) +{ + check_colors_files_for_name (nmc, color_option, base_dir, "nmcli"); + check_colors_files_for_name (nmc, color_option, base_dir, NULL); +} + +static const char * +resolve_color_alias (const char *color) +{ + static const struct { + const char *name; + const char *alias; + } aliases[] = { + { "reset", "0" }, + { "bold", "1" }, + { "white", "1;37" }, + { "halfbright", "2" }, + { "underscore", "4" }, + { "blink", "5" }, + { "reverse", "7" }, + { "black", "30" }, + { "red", "31" }, + { "green", "32" }, + { "brown", "33" }, + { "yellow", "33" }, /* well, yellow */ + { "blue", "34" }, + { "magenta", "35" }, + { "cyan", "36" }, + { "gray", "37" }, + { "darkgray", "90" }, + { "lightred", "91" }, + { "lightgreen", "92" }, + { "lightblue", "94" }, + { "lightmagenta", "95" }, + { "lightcyan", "96" }, + { "lightgray", "97" }, + }; + int i; + + /* Shortcut literal sequences. */ + if (g_ascii_isdigit (*color)) + return color; + + for (i = 0; i < G_N_ELEMENTS (aliases); i++) { + if (strcmp (color, aliases[i].name) == 0) + return aliases[i].alias; + } + + return color; +} + +static gboolean +parse_color_scheme (NmCli *nmc, GError **error) +{ + char *p = nmc->palette_buffer; + const char *name; + const char *color; + const char *map[_NM_META_COLOR_NUM] = { + [NM_META_COLOR_NONE] = NULL, + [NM_META_COLOR_CONNECTION_ACTIVATED] = "connection-activated", + [NM_META_COLOR_CONNECTION_ACTIVATING] = "connection-activating", + [NM_META_COLOR_CONNECTION_DISCONNECTING] = "connection-disconnecting", + [NM_META_COLOR_CONNECTION_INVISIBLE] = "connection-invisible", + [NM_META_COLOR_CONNECTION_UNKNOWN] = "connection-unknown", + [NM_META_COLOR_CONNECTIVITY_FULL] = "connectivity-full", + [NM_META_COLOR_CONNECTIVITY_LIMITED] = "connectivity-limited", + [NM_META_COLOR_CONNECTIVITY_NONE] = "connectivity-none", + [NM_META_COLOR_CONNECTIVITY_PORTAL] = "connectivity-portal", + [NM_META_COLOR_CONNECTIVITY_UNKNOWN] = "connectivity-unknown", + [NM_META_COLOR_DEVICE_ACTIVATED] = "device-activated", + [NM_META_COLOR_DEVICE_ACTIVATING] = "device-activating", + [NM_META_COLOR_DEVICE_DISCONNECTED] = "device-disconnected", + [NM_META_COLOR_DEVICE_FIRMWARE_MISSING] = "device-firmware-missing", + [NM_META_COLOR_DEVICE_PLUGIN_MISSING] = "device-plugin-missing", + [NM_META_COLOR_DEVICE_UNAVAILABLE] = "device-unavailable", + [NM_META_COLOR_DEVICE_UNKNOWN] = "device-unknown", + [NM_META_COLOR_MANAGER_RUNNING] = "manager-running", + [NM_META_COLOR_MANAGER_STARTING] = "manager-starting", + [NM_META_COLOR_MANAGER_STOPPED] = "manager-stopped", + [NM_META_COLOR_PERMISSION_AUTH] = "permission-auth", + [NM_META_COLOR_PERMISSION_NO] = "permission-no", + [NM_META_COLOR_PERMISSION_UNKNOWN] = "permission-unknown", + [NM_META_COLOR_PERMISSION_YES] = "permission-yes", + [NM_META_COLOR_PROMPT] = "prompt", + [NM_META_COLOR_STATE_ASLEEP] = "state-asleep", + [NM_META_COLOR_STATE_CONNECTED_GLOBAL] = "state-connected-global", + [NM_META_COLOR_STATE_CONNECTED_LOCAL] = "state-connected-local", + [NM_META_COLOR_STATE_CONNECTED_SITE] = "state-connected-site", + [NM_META_COLOR_STATE_CONNECTING] = "state-connecting", + [NM_META_COLOR_STATE_DISCONNECTED] = "state-disconnected", + [NM_META_COLOR_STATE_DISCONNECTING] = "state-disconnecting", + [NM_META_COLOR_STATE_UNKNOWN] = "state-unknown", + [NM_META_COLOR_WIFI_SIGNAL_EXCELLENT] = "wifi-signal-excellent", + [NM_META_COLOR_WIFI_SIGNAL_FAIR] = "wifi-signal-fair", + [NM_META_COLOR_WIFI_SIGNAL_GOOD] = "wifi-signal-good", + [NM_META_COLOR_WIFI_SIGNAL_POOR] = "wifi-signal-poor", + [NM_META_COLOR_WIFI_SIGNAL_UNKNOWN] = "wifi-signal-unknown", + [NM_META_COLOR_DISABLED] = "disabled", + [NM_META_COLOR_ENABLED] = "enabled", + }; + int i; + + /* This reads through the raw color scheme file contents, identifying the + * color names and sequences, putting in terminating NULs in place, so that + * pointers into the buffer can readily be used as strings in the palette. */ + while (1) { + /* Leading whitespace. */ + while (nm_utils_is_separator (*p) || *p == '\n') + p++; + + if (*p == '\0') + break; + + /* Comments. */ + if (*p == '#') { + while (*p != '\n' && *p != '\0') + p++; + continue; + } + + /* Color name. */ + name = p; + while (g_ascii_isgraph (*p)) + p++; + if (*p == '\0') { + g_set_error (error, NMCLI_ERROR, 0, + _("Unexpected end of file following '%s'\n"), name); + return FALSE; + } + + /* Separating whitespace. */ + if (!nm_utils_is_separator (*p)) { + *p = '\0'; + g_set_error (error, NMCLI_ERROR, 0, + _("Expected whitespace following '%s'\n"), name); + return FALSE; + } + while (nm_utils_is_separator (*p)) { + *p = '\0'; + p++; + } + + /* Color sequence. */ + color = p; + if (!g_ascii_isgraph (*p)) { + g_set_error (error, NMCLI_ERROR, 0, + _("Expected a value for '%s'\n"), name); + return FALSE; + } + while (g_ascii_isgraph (*p)) + p++; + + /* Trailing whitespace. */ + while (nm_utils_is_separator (*p)) { + *p = '\0'; + p++; + } + if (*p != '\0') { + if (*p != '\n') { + g_set_error (error, NMCLI_ERROR, 0, + _("Expected a line break following '%s'\n"), color); + return FALSE; + } + *p = '\0'; + p++; + } + + /* All good, set the palette entry. */ + for (i = NM_META_COLOR_NONE + 1; i < _NM_META_COLOR_NUM; i++) { + if (strcmp (map[i], name) == 0) { + nmc->nmc_config_mutable.palette[i] = resolve_color_alias (color); + break; + } + } + if (i == _NM_META_COLOR_NUM) + g_debug ("Ignoring an unrecognized color: '%s'\n", name); + } + + return TRUE; +} + +static void +set_colors (NmCli *nmc, NmcColorOption color_option) +{ + GError *error = NULL; + + if (color_option == NMC_USE_COLOR_AUTO) { + if ( g_strcmp0 (g_getenv ("TERM"), "dumb") == 0 + || !isatty (STDOUT_FILENO)) + color_option = NMC_USE_COLOR_NO; + } + + check_colors_files_for_base_dir (nmc, &color_option, g_get_user_config_dir ()); + check_colors_files_for_base_dir (nmc, &color_option, SYSCONFDIR); + + switch (color_option) { case NMC_USE_COLOR_YES: case NMC_USE_COLOR_AUTO: nmc->nmc_config_mutable.use_colors = TRUE; @@ -353,8 +607,17 @@ set_colors (NmCli *nmc, NmcColorOption *color_option) nmc->nmc_config_mutable.use_colors = FALSE; break; } + + if (nmc->nmc_config_mutable.use_colors && nmc->palette_buffer) { + if (!parse_color_scheme (nmc, &error)) { + g_debug ("Error parsing color scheme: %s", error->message); + g_error_free (error); + } + } } +/*************************************************************************************/ + static gboolean process_command_line (NmCli *nmc, int argc, char **argv) { @@ -508,7 +771,7 @@ process_command_line (NmCli *nmc, int argc, char **argv) if (nmc->required_fields) nmc->nmc_config_mutable.overview = FALSE; - set_colors (nmc, &colors); + set_colors (nmc, colors); /* Now run the requested command */ nmc_do_cmd (nmc, nmcli_cmds, *argv, argc, argv); @@ -674,6 +937,8 @@ nmc_cleanup (NmCli *nmc) nmc->pager_pid = 0; } + nm_clear_g_free (&nmc->palette_buffer); + nmc_polkit_agent_fini (nmc); } diff --git a/clients/cli/nmcli.h b/clients/cli/nmcli.h index 55ca1d30ab..1b176a66a9 100644 --- a/clients/cli/nmcli.h +++ b/clients/cli/nmcli.h @@ -137,6 +137,8 @@ typedef struct _NmCli { gboolean complete; /* Autocomplete the command line */ gboolean editor_status_line; /* Whether to display status line in connection editor */ gboolean editor_save_confirmation; /* Whether to ask for confirmation on saving connections with 'autoconnect=yes' */ + + char *palette_buffer; /* Buffer with sequences for terminal-colors.d(5)-based coloring. */ } NmCli; extern NmCli nm_cli; diff --git a/man/nmcli.xml b/man/nmcli.xml index 2619234e11..0787fd8baa 100644 --- a/man/nmcli.xml +++ b/man/nmcli.xml @@ -9,7 +9,7 @@