trivial: Split out some CLI functionality for future use

This commit is contained in:
Richard Hughes 2018-05-18 09:53:18 +01:00
parent 3e3ac27216
commit f761640aa1
5 changed files with 130 additions and 70 deletions

View File

@ -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

95
src/fu-util-common.c Normal file
View File

@ -0,0 +1,95 @@
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
*
* Copyright (C) 2017-2018 Richard Hughes <richard@hughsie.com>
*
* 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 <config.h>
#include <stdio.h>
#include <glib/gi18n.h>
#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;
}

32
src/fu-util-common.h Normal file
View File

@ -0,0 +1,32 @@
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
*
* Copyright (C) 2017-2018 Richard Hughes <richard@hughsie.com>
*
* 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 <glib.h>
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__ */

View File

@ -32,7 +32,6 @@
#include <gudev/gudev.h>
#include <json-glib/json-glib.h>
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <libsoup/soup.h>
#include <unistd.h>
@ -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)
{

View File

@ -64,6 +64,7 @@ fwupdmgr = executable(
'fwupdmgr',
sources : [
'fu-util.c',
'fu-util-common.c',
],
include_directories : [
include_directories('..'),