diff --git a/po/POTFILES.in b/po/POTFILES.in index 06697261f..20efaab60 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -10,3 +10,4 @@ src/fu-debug.c src/fu-main.c src/fu-progressbar.c src/fu-util.c +src/fu-util-common.c diff --git a/src/fu-util-common.c b/src/fu-util-common.c new file mode 100644 index 000000000..3a73fdf75 --- /dev/null +++ b/src/fu-util-common.c @@ -0,0 +1,95 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- + * + * Copyright (C) 2017-2018 Richard Hughes + * + * Licensed under the GNU General Public License Version 2 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include +#include +#include + +#include "fu-util-common.h" + +void +fu_util_print_data (const gchar *title, const gchar *msg) +{ + gsize title_len; + g_auto(GStrv) lines = NULL; + + if (msg == NULL) + return; + g_print ("%s:", title); + + /* pad */ + title_len = strlen (title) + 1; + lines = g_strsplit (msg, "\n", -1); + for (guint j = 0; lines[j] != NULL; j++) { + for (gsize i = title_len; i < 25; i++) + g_print (" "); + g_print ("%s\n", lines[j]); + title_len = 0; + } +} + +guint +fu_util_prompt_for_number (guint maxnum) +{ + gint retval; + guint answer = 0; + + do { + char buffer[64]; + + /* swallow the \n at end of line too */ + if (!fgets (buffer, sizeof (buffer), stdin)) + break; + if (strlen (buffer) == sizeof (buffer) - 1) + continue; + + /* get a number */ + retval = sscanf (buffer, "%u", &answer); + + /* positive */ + if (retval == 1 && answer <= maxnum) + break; + + /* TRANSLATORS: the user isn't reading the question */ + g_print (_("Please enter a number from 0 to %u: "), maxnum); + } while (TRUE); + return answer; +} + +gboolean +fu_util_prompt_for_boolean (gboolean def) +{ + do { + char buffer[4]; + if (!fgets (buffer, sizeof (buffer), stdin)) + continue; + if (strlen (buffer) == sizeof (buffer) - 1) + continue; + if (g_strcmp0 (buffer, "\n") == 0) + return def; + buffer[0] = g_ascii_toupper (buffer[0]); + if (g_strcmp0 (buffer, "Y\n") == 0) + return TRUE; + if (g_strcmp0 (buffer, "N\n") == 0) + return FALSE; + } while (TRUE); + return FALSE; +} diff --git a/src/fu-util-common.h b/src/fu-util-common.h new file mode 100644 index 000000000..aeb416c00 --- /dev/null +++ b/src/fu-util-common.h @@ -0,0 +1,32 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- + * + * Copyright (C) 2017-2018 Richard Hughes + * + * Licensed under the GNU General Public License Version 2 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef __FU_UTIL_COMMON_H__ +#define __FU_UTIL_COMMON_H__ + +#include + +void fu_util_print_data (const gchar *title, + const gchar *msg); +guint fu_util_prompt_for_number (guint maxnum); +gboolean fu_util_prompt_for_boolean (gboolean def); + +#endif /* __FU_UTIL_COMMON_H__ */ diff --git a/src/fu-util.c b/src/fu-util.c index 11e39e0f0..15a967425 100644 --- a/src/fu-util.c +++ b/src/fu-util.c @@ -32,7 +32,6 @@ #include #include #include -#include #include #include #include @@ -41,6 +40,7 @@ #include "fu-history.h" #include "fu-plugin-private.h" #include "fu-progressbar.h" +#include "fu-util-common.h" #include "fwupd-common-private.h" /* this is only valid in this file */ @@ -196,75 +196,6 @@ fu_util_client_notify_cb (GObject *object, fwupd_client_get_percentage (priv->client)); } -static void -fu_util_print_data (const gchar *title, const gchar *msg) -{ - gsize title_len; - g_auto(GStrv) lines = NULL; - - if (msg == NULL) - return; - g_print ("%s:", title); - - /* pad */ - title_len = strlen (title) + 1; - lines = g_strsplit (msg, "\n", -1); - for (guint j = 0; lines[j] != NULL; j++) { - for (gsize i = title_len; i < 25; i++) - g_print (" "); - g_print ("%s\n", lines[j]); - title_len = 0; - } -} - -static guint -fu_util_prompt_for_number (guint maxnum) -{ - gint retval; - guint answer = 0; - - do { - char buffer[64]; - - /* swallow the \n at end of line too */ - if (!fgets (buffer, sizeof (buffer), stdin)) - break; - if (strlen (buffer) == sizeof (buffer) - 1) - continue; - - /* get a number */ - retval = sscanf (buffer, "%u", &answer); - - /* positive */ - if (retval == 1 && answer <= maxnum) - break; - - /* TRANSLATORS: the user isn't reading the question */ - g_print (_("Please enter a number from 0 to %u: "), maxnum); - } while (TRUE); - return answer; -} - -static gboolean -fu_util_prompt_for_boolean (gboolean def) -{ - do { - char buffer[4]; - if (!fgets (buffer, sizeof (buffer), stdin)) - continue; - if (strlen (buffer) == sizeof (buffer) - 1) - continue; - if (g_strcmp0 (buffer, "\n") == 0) - return def; - buffer[0] = g_ascii_toupper (buffer[0]); - if (g_strcmp0 (buffer, "Y\n") == 0) - return TRUE; - if (g_strcmp0 (buffer, "N\n") == 0) - return FALSE; - } while (TRUE); - return FALSE; -} - static FwupdDevice * fu_util_prompt_for_device (FuUtilPrivate *priv, GError **error) { diff --git a/src/meson.build b/src/meson.build index 306311765..d0577f840 100644 --- a/src/meson.build +++ b/src/meson.build @@ -64,6 +64,7 @@ fwupdmgr = executable( 'fwupdmgr', sources : [ 'fu-util.c', + 'fu-util-common.c', ], include_directories : [ include_directories('..'),