Commit 1b74e77f authored by Dan Winship's avatar Dan Winship

cli: use g_print/g_printerr rather than printf/fprintf

Use g_print() and g_printerr(), which wrap printf() and
fprintf(stderr,...), but handle conversion from UTF-8 if the locale is
using a different character set.
parent df230fc2
This diff is collapsed.
This diff is collapsed.
......@@ -19,7 +19,6 @@
#include "config.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
......@@ -89,161 +88,147 @@ extern GMainLoop *loop;
static void
usage_general (void)
{
fprintf (stderr,
_("Usage: nmcli general { COMMAND | help }\n\n"
"COMMAND := { status | hostname | permissions | logging }\n\n"
" status\n\n"
" hostname [<hostname>]\n\n"
" permissions\n\n"
" logging [level <log level>] [domains <log domains>]\n\n"));
g_printerr (_("Usage: nmcli general { COMMAND | help }\n\n"
"COMMAND := { status | hostname | permissions | logging }\n\n"
" status\n\n"
" hostname [<hostname>]\n\n"
" permissions\n\n"
" logging [level <log level>] [domains <log domains>]\n\n"));
}
static void
usage_general_status (void)
{
fprintf (stderr,
_("Usage: nmcli general status { help }\n"
"\n"
"Show overall status of NetworkManager.\n"
"'status' is the default action, which means 'nmcli gen' calls 'nmcli gen status'\n\n"));
g_printerr (_("Usage: nmcli general status { help }\n"
"\n"
"Show overall status of NetworkManager.\n"
"'status' is the default action, which means 'nmcli gen' calls 'nmcli gen status'\n\n"));
}
static void
usage_general_hostname (void)
{
fprintf (stderr,
_("Usage: nmcli general hostname { ARGUMENTS | help }\n"
"\n"
"ARGUMENTS := [<hostname>]\n"
"\n"
"Get or change persistent system hostname.\n"
"With no arguments, this prints currently configured hostname. When you pass\n"
"a hostname, NetworkManager will set it as the new persistent system hostname.\n\n"));
g_printerr (_("Usage: nmcli general hostname { ARGUMENTS | help }\n"
"\n"
"ARGUMENTS := [<hostname>]\n"
"\n"
"Get or change persistent system hostname.\n"
"With no arguments, this prints currently configured hostname. When you pass\n"
"a hostname, NetworkManager will set it as the new persistent system hostname.\n\n"));
}
static void
usage_general_permissions (void)
{
fprintf (stderr,
_("Usage: nmcli general permissions { help }\n"
"\n"
"Show caller permissions for authenticated operations.\n\n"));
g_printerr (_("Usage: nmcli general permissions { help }\n"
"\n"
"Show caller permissions for authenticated operations.\n\n"));
}
static void
usage_general_logging (void)
{
fprintf (stderr,
_("Usage: nmcli general logging { ARGUMENTS | help }\n"
"\n"
"ARGUMENTS := [level <log level>] [domains <log domains>]\n"
"\n"
"Get or change NetworkManager logging level and domains.\n"
"Without any argument current logging level and domains are shown. In order to\n"
"change logging state, provide level and/or domain. Please refer to the man page\n"
"for the list of possible logging domains.\n\n"));
g_printerr (_("Usage: nmcli general logging { ARGUMENTS | help }\n"
"\n"
"ARGUMENTS := [level <log level>] [domains <log domains>]\n"
"\n"
"Get or change NetworkManager logging level and domains.\n"
"Without any argument current logging level and domains are shown. In order to\n"
"change logging state, provide level and/or domain. Please refer to the man page\n"
"for the list of possible logging domains.\n\n"));
}
static void
usage_networking (void)
{
fprintf (stderr,
_("Usage: nmcli networking { COMMAND | help }\n\n"
"COMMAND := { [ on | off | connectivity ] }\n\n"
" on\n\n"
" off\n\n"
" connectivity [check]\n\n"));
g_printerr (_("Usage: nmcli networking { COMMAND | help }\n\n"
"COMMAND := { [ on | off | connectivity ] }\n\n"
" on\n\n"
" off\n\n"
" connectivity [check]\n\n"));
}
static void
usage_networking_on (void)
{
fprintf (stderr,
_("Usage: nmcli networking on { help }\n"
"\n"
"Switch networking on.\n\n"));
g_printerr (_("Usage: nmcli networking on { help }\n"
"\n"
"Switch networking on.\n\n"));
}
static void
usage_networking_off (void)
{
fprintf (stderr,
_("Usage: nmcli networking off { help }\n"
"\n"
"Switch networking off.\n\n"));
g_printerr (_("Usage: nmcli networking off { help }\n"
"\n"
"Switch networking off.\n\n"));
}
static void
usage_networking_connectivity (void)
{
fprintf (stderr,
_("Usage: nmcli networking connectivity { ARGUMENTS | help }\n"
"\n"
"ARGUMENTS := [check]\n"
"\n"
"Get network connectivity state.\n"
"The optional 'check' argument makes NetworkManager re-check the connectivity.\n\n"));
g_printerr (_("Usage: nmcli networking connectivity { ARGUMENTS | help }\n"
"\n"
"ARGUMENTS := [check]\n"
"\n"
"Get network connectivity state.\n"
"The optional 'check' argument makes NetworkManager re-check the connectivity.\n\n"));
}
static void
usage_radio (void)
{
fprintf (stderr,
_("Usage: nmcli radio { COMMAND | help }\n\n"
g_printerr (_("Usage: nmcli radio { COMMAND | help }\n\n"
#if WITH_WIMAX
"COMMAND := { all | wifi | wwan | wimax }\n\n"
" all | wifi | wwan | wimax [ on | off ]\n\n"
"COMMAND := { all | wifi | wwan | wimax }\n\n"
" all | wifi | wwan | wimax [ on | off ]\n\n"
#else
"COMMAND := { all | wifi | wwan }\n\n"
" all | wifi | wwan [ on | off ]\n\n"
"COMMAND := { all | wifi | wwan }\n\n"
" all | wifi | wwan [ on | off ]\n\n"
#endif
));
));
}
static void
usage_radio_all (void)
{
fprintf (stderr,
_("Usage: nmcli radio all { ARGUMENTS | help }\n"
"\n"
"ARGUMENTS := [on | off]\n"
"\n"
"Get status of all radio switches, or turn them on/off.\n\n"));
g_printerr (_("Usage: nmcli radio all { ARGUMENTS | help }\n"
"\n"
"ARGUMENTS := [on | off]\n"
"\n"
"Get status of all radio switches, or turn them on/off.\n\n"));
}
static void
usage_radio_wifi (void)
{
fprintf (stderr,
_("Usage: nmcli radio wifi { ARGUMENTS | help }\n"
"\n"
"ARGUMENTS := [on | off]\n"
"\n"
"Get status of Wi-Fi radio switch, or turn it on/off.\n\n"));
g_printerr (_("Usage: nmcli radio wifi { ARGUMENTS | help }\n"
"\n"
"ARGUMENTS := [on | off]\n"
"\n"
"Get status of Wi-Fi radio switch, or turn it on/off.\n\n"));
}
static void
usage_radio_wwan (void)
{
fprintf (stderr,
_("Usage: nmcli radio wwan { ARGUMENTS | help }\n"
"\n"
"ARGUMENTS := [on | off]\n"
"\n"
"Get status of mobile broadband radio switch, or turn it on/off.\n\n"));
g_printerr (_("Usage: nmcli radio wwan { ARGUMENTS | help }\n"
"\n"
"ARGUMENTS := [on | off]\n"
"\n"
"Get status of mobile broadband radio switch, or turn it on/off.\n\n"));
}
#if WITH_WIMAX
static void
usage_radio_wimax (void)
{
fprintf (stderr,
_("Usage: nmcli radio wimax { ARGUMENTS | help }\n"
"\n"
"ARGUMENTS := [on | off]\n"
"\n"
"Get status of WiMAX radio switch, or turn it on/off.\n\n"));
g_printerr (_("Usage: nmcli radio wimax { ARGUMENTS | help }\n"
"\n"
"ARGUMENTS := [on | off]\n"
"\n"
"Get status of WiMAX radio switch, or turn it on/off.\n\n"));
}
#endif
......@@ -620,14 +605,14 @@ do_general (NmCli *nmc, int argc, char **argv)
g_object_get (rem_settings, NM_REMOTE_SETTINGS_HOSTNAME, &hostname, NULL);
if (hostname)
printf ("%s\n", hostname);
g_print ("%s\n", hostname);
g_free (hostname);
} else {
/* hostname provided -> set it */
const char *hostname = *argv;
if (next_arg (&argc, &argv) == 0)
printf ("Warning: ignoring extra garbage after '%s' hostname\n", hostname);
g_print ("Warning: ignoring extra garbage after '%s' hostname\n", hostname);
nmc->should_wait = TRUE;
nm_remote_settings_save_hostname_async (rem_settings, hostname, NULL, save_hostname_cb, nmc);
......
......@@ -78,29 +78,28 @@ nmcli_error_quark (void)
static void
usage (const char *prog_name)
{
fprintf (stderr,
_("Usage: %s [OPTIONS] OBJECT { COMMAND | help }\n"
"\n"
"OPTIONS\n"
" -t[erse] terse output\n"
" -p[retty] pretty output\n"
" -m[ode] tabular|multiline output mode\n"
" -f[ields] <field1,field2,...>|all|common specify fields to output\n"
" -e[scape] yes|no escape columns separators in values\n"
" -n[ocheck] don't check nmcli and NetworkManager versions\n"
" -a[sk] ask for missing parameters\n"
" -w[ait] <seconds> set timeout waiting for finishing operations\n"
" -v[ersion] show program version\n"
" -h[elp] print this help\n"
"\n"
"OBJECT\n"
" g[eneral] NetworkManager's general status and operations\n"
" n[etworking] overall networking control\n"
" r[adio] NetworkManager radio switches\n"
" c[onnection] NetworkManager's connections\n"
" d[evice] devices managed by NetworkManager\n"
"\n"),
prog_name);
g_printerr (_("Usage: %s [OPTIONS] OBJECT { COMMAND | help }\n"
"\n"
"OPTIONS\n"
" -t[erse] terse output\n"
" -p[retty] pretty output\n"
" -m[ode] tabular|multiline output mode\n"
" -f[ields] <field1,field2,...>|all|common specify fields to output\n"
" -e[scape] yes|no escape columns separators in values\n"
" -n[ocheck] don't check nmcli and NetworkManager versions\n"
" -a[sk] ask for missing parameters\n"
" -w[ait] <seconds> set timeout waiting for finishing operations\n"
" -v[ersion] show program version\n"
" -h[elp] print this help\n"
"\n"
"OBJECT\n"
" g[eneral] NetworkManager's general status and operations\n"
" n[etworking] overall networking control\n"
" r[adio] NetworkManager radio switches\n"
" c[onnection] NetworkManager's connections\n"
" d[evice] devices managed by NetworkManager\n"
"\n"),
prog_name);
}
static NMCResultCode
......@@ -248,7 +247,7 @@ parse_command_line (NmCli *nmc, int argc, char **argv)
}
nmc->timeout = (int) timeout;
} else if (matches (opt, "-version") == 0) {
printf (_("nmcli tool, version %s\n"), NMCLI_VERSION);
g_print (_("nmcli tool, version %s\n"), NMCLI_VERSION);
return NMC_RESULT_SUCCESS;
} else if (matches (opt, "-help") == 0) {
usage (base);
......@@ -334,8 +333,8 @@ signal_handling_thread (void *arg) {
} else {
/* We can quit nmcli */
nmc_cleanup_readline ();
printf (_("\nError: nmcli terminated by signal %s (%d)\n"),
strsignal (signo), signo);
g_print (_("\nError: nmcli terminated by signal %s (%d)\n"),
strsignal (signo), signo);
exit (1);
}
break;
......@@ -343,8 +342,8 @@ signal_handling_thread (void *arg) {
case SIGTERM:
nmc_cleanup_readline ();
if (!nmcli_sigquit_internal)
printf (_("\nError: nmcli terminated by signal %s (%d)\n"),
strsignal (signo), signo);
g_print (_("\nError: nmcli terminated by signal %s (%d)\n"),
strsignal (signo), signo);
exit (1);
break;
default:
......@@ -374,14 +373,14 @@ setup_signals (void)
/* Block all signals of interest. */
status = pthread_sigmask (SIG_BLOCK, &signal_set, NULL);
if (status != 0) {
fprintf (stderr, _("Failed to set signal mask: %d\n"), status);
g_printerr (_("Failed to set signal mask: %d\n"), status);
return FALSE;
}
/* Create the signal handling thread. */
status = pthread_create (&signal_thread_id, NULL, signal_handling_thread, NULL);
if (status != 0) {
fprintf (stderr, _("Failed to create signal handling thread: %d\n"), status);
g_printerr (_("Failed to create signal handling thread: %d\n"), status);
return FALSE;
}
......@@ -588,7 +587,7 @@ main (int argc, char *argv[])
/* Print result descripting text */
if (nm_cli.return_value != NMC_RESULT_SUCCESS) {
fprintf (stderr, "%s\n", nm_cli.return_text->str);
g_printerr ("%s\n", nm_cli.return_text->str);
}
g_main_loop_unref (loop);
......
......@@ -19,7 +19,6 @@
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <glib.h>
......@@ -1841,9 +1840,9 @@ wireless_band_channel_changed_cb (GObject *object, GParamSpec *pspec, gpointer u
mode = nm_setting_wireless_get_mode (NM_SETTING_WIRELESS (object));
if (!mode || !*mode || strcmp (mode, NM_SETTING_WIRELESS_MODE_INFRA) == 0) {
printf (_("Warning: %s.%s set to '%s', but it might be ignored in infrastructure mode\n"),
nm_setting_get_name (NM_SETTING (s_wireless)), g_param_spec_get_name (pspec),
value);
g_print (_("Warning: %s.%s set to '%s', but it might be ignored in infrastructure mode\n"),
nm_setting_get_name (NM_SETTING (s_wireless)), g_param_spec_get_name (pspec),
value);
}
}
......@@ -2504,7 +2503,7 @@ nmc_property_connection_set_secondaries (NMSetting *setting, const char *prop, c
con = nmc_find_connection (nm_cli.system_connections,
"uuid", *iter, NULL);
if (!con)
printf (_("Warning: %s is not an UUID of any existing connection profile\n"), *iter);
g_print (_("Warning: %s is not an UUID of any existing connection profile\n"), *iter);
else {
/* Currenly NM only supports VPN connections as secondaries */
if (!nm_connection_is_type (con, NM_SETTING_VPN_SETTING_NAME)) {
......@@ -3804,8 +3803,8 @@ nmc_property_vlan_remove_priority_map (NMSetting *setting,
if (!prio_map)
return FALSE;
if (prio_map[1])
printf (_("Warning: only one mapping at a time is supported; taking the first one (%s)\n"),
prio_map[0]);
g_print (_("Warning: only one mapping at a time is supported; taking the first one (%s)\n"),
prio_map[0]);
ret = nm_setting_vlan_remove_priority_str_by_value (NM_SETTING_VLAN (setting),
map,
prio_map[0]);
......@@ -4276,9 +4275,9 @@ nmc_property_wifi_set_wep_key (NMSetting *setting, const char *prop, const char
}
prev_idx = nm_setting_wireless_security_get_wep_tx_keyidx (NM_SETTING_WIRELESS_SECURITY (setting));
idx = prop[strlen (prop) - 1] - '0';
printf (_("WEP key is guessed to be of '%s'\n"), wep_key_type_to_string (guessed_type));
g_print (_("WEP key is guessed to be of '%s'\n"), wep_key_type_to_string (guessed_type));
if (idx != prev_idx)
printf (_("WEP key index set to '%d'\n"), idx);
g_print (_("WEP key index set to '%d'\n"), idx);
g_object_set (setting, prop, val, NULL);
g_object_set (setting, NM_SETTING_WIRELESS_SECURITY_WEP_KEY_TYPE, guessed_type, NULL);
......@@ -4317,17 +4316,17 @@ nmc_property_wifi_set_wep_key_type (NMSetting *setting, const char *prop, const
key2 = nm_setting_wireless_security_get_wep_key (NM_SETTING_WIRELESS_SECURITY (setting), 2);
key3 = nm_setting_wireless_security_get_wep_key (NM_SETTING_WIRELESS_SECURITY (setting), 3);
if (key0 && !nm_utils_wep_key_valid (key0, type))
printf (_("Warning: '%s' is not compatible with '%s' type, please change or delete the key.\n"),
NM_SETTING_WIRELESS_SECURITY_WEP_KEY0, wep_key_type_to_string (type));
g_print (_("Warning: '%s' is not compatible with '%s' type, please change or delete the key.\n"),
NM_SETTING_WIRELESS_SECURITY_WEP_KEY0, wep_key_type_to_string (type));
if (key1 && !nm_utils_wep_key_valid (key1, type))
printf (_("Warning: '%s' is not compatible with '%s' type, please change or delete the key.\n"),
NM_SETTING_WIRELESS_SECURITY_WEP_KEY1, wep_key_type_to_string (type));
g_print (_("Warning: '%s' is not compatible with '%s' type, please change or delete the key.\n"),
NM_SETTING_WIRELESS_SECURITY_WEP_KEY1, wep_key_type_to_string (type));
if (key2 && !nm_utils_wep_key_valid (key2, type))
printf (_("Warning: '%s' is not compatible with '%s' type, please change or delete the key.\n"),
NM_SETTING_WIRELESS_SECURITY_WEP_KEY2, wep_key_type_to_string (type));
g_print (_("Warning: '%s' is not compatible with '%s' type, please change or delete the key.\n"),
NM_SETTING_WIRELESS_SECURITY_WEP_KEY2, wep_key_type_to_string (type));
if (key3 && !nm_utils_wep_key_valid (key3, type))
printf (_("Warning: '%s' is not compatible with '%s' type, please change or delete the key.\n"),
NM_SETTING_WIRELESS_SECURITY_WEP_KEY3, wep_key_type_to_string (type));
g_print (_("Warning: '%s' is not compatible with '%s' type, please change or delete the key.\n"),
NM_SETTING_WIRELESS_SECURITY_WEP_KEY3, wep_key_type_to_string (type));
g_object_set (setting, prop, type, NULL);
return TRUE;
......@@ -4487,7 +4486,7 @@ dcb_check_feature_enabled (NMSettingDcb *s_dcb, const char *flags_prop)
g_object_get (s_dcb, flags_prop, &flags, NULL);
if (!(flags & NM_SETTING_DCB_FLAG_ENABLE))
printf (_("Warning: changes will have no effect until '%s' includes 1 (enabled)\n\n"), flags_prop);
g_print (_("Warning: changes will have no effect until '%s' includes 1 (enabled)\n\n"), flags_prop);
}
static gboolean
......
......@@ -248,6 +248,9 @@ nmc_ip6_address_as_string (const struct in6_addr *ip, GError **error)
void
nmc_terminal_erase_line (void)
{
/* We intentionally use printf(), not g_print() here, to ensure that
* GLib doesn't mistakenly try to convert the string.
*/
printf ("\33[2K\r");
fflush (stdout);
}
......@@ -264,7 +267,7 @@ nmc_terminal_show_progress (const char *str)
const char slashes[4] = {'|', '/', '-', '\\'};
nmc_terminal_erase_line ();
printf ("%c %s", slashes[idx++], str ? str : "");
g_print ("%c %s", slashes[idx++], str ? str : "");
fflush (stdout);
if (idx == 4)
idx = 0;
......@@ -432,7 +435,7 @@ nmc_get_user_input (const char *ask_str)
size_t line_ln = 0;
ssize_t num;
fprintf (stdout, "%s", ask_str);
g_print ("%s", ask_str);
num = getline (&line, &line_ln, stdin);
/* Remove newline from the string */
......@@ -895,9 +898,9 @@ print_required_fields (NmCli *nmc, const NmcOutputField field_values[])
line = g_strnfill (ML_HEADER_WIDTH, '=');
width1 = strlen (fields.header_name);
width2 = nmc_string_screen_width (fields.header_name, NULL);
printf ("%s\n", line);
printf ("%*s\n", (table_width + width2)/2 + width1 - width2, fields.header_name);
printf ("%s\n", line);
g_print ("%s\n", line);
g_print ("%*s\n", (table_width + width2)/2 + width1 - width2, fields.header_name);
g_print ("%s\n", line);
g_free (line);
}
......@@ -927,8 +930,8 @@ print_required_fields (NmCli *nmc, const NmcOutputField field_values[])
j);
width1 = strlen (tmp);
width2 = nmc_string_screen_width (tmp, NULL);
printf ("%-*s%s\n", terse ? 0 : ML_VALUE_INDENT+width1-width2, tmp,
*p ? *p : not_set_str);
g_print ("%-*s%s\n", terse ? 0 : ML_VALUE_INDENT+width1-width2, tmp,
*p ? *p : not_set_str);
g_free (tmp);
}
} else {
......@@ -942,14 +945,14 @@ print_required_fields (NmCli *nmc, const NmcOutputField field_values[])
_(field_values[idx].name_l10n));
width1 = strlen (tmp);
width2 = nmc_string_screen_width (tmp, NULL);
printf ("%-*s%s\n", terse ? 0 : ML_VALUE_INDENT+width1-width2, tmp,
val ? val : not_set_str);
g_print ("%-*s%s\n", terse ? 0 : ML_VALUE_INDENT+width1-width2, tmp,
val ? val : not_set_str);
g_free (tmp);
}
}
if (pretty) {
line = g_strnfill (ML_HEADER_WIDTH, '-');
printf ("%s\n", line);
g_print ("%s\n", line);
g_free (line);
}
}
......@@ -997,9 +1000,9 @@ print_required_fields (NmCli *nmc, const NmcOutputField field_values[])
line = g_strnfill (table_width, '=');
width1 = strlen (fields.header_name);
width2 = nmc_string_screen_width (fields.header_name, NULL);
printf ("%s\n", line);
printf ("%*s\n", (table_width + width2)/2 + width1 - width2, fields.header_name);
printf ("%s\n", line);
g_print ("%s\n", line);
g_print ("%*s\n", (table_width + width2)/2 + width1 - width2, fields.header_name);
g_print ("%s\n", line);
g_free (line);
}
......@@ -1011,14 +1014,14 @@ print_required_fields (NmCli *nmc, const NmcOutputField field_values[])
g_string_prepend (str, indent_str);
g_free (indent_str);
}
printf ("%s\n", str->str);
g_print ("%s\n", str->str);
}
/* Print horizontal separator */
if (!main_header_only && field_names && pretty) {
if (str->len > 0) {
line = g_strnfill (table_width, '-');
printf ("%s\n", line);
g_print ("%s\n", line);
g_free (line);
}
}
......@@ -1107,9 +1110,8 @@ nmc_versions_match (NmCli *nmc)
if (dot) {
dot = strchr (dot + 1, '.');
if (dot && !strncmp (nm_ver, VERSION, dot-nm_ver)) {
fprintf(stderr,
_("Warning: nmcli (%s) and NetworkManager (%s) versions don't match. Use --nocheck to suppress the warning.\n"),
VERSION, nm_ver);
g_printerr (_("Warning: nmcli (%s) and NetworkManager (%s) versions don't match. Use --nocheck to suppress the warning.\n"),
VERSION, nm_ver);
match = TRUE;
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment