cli: handle multiline revision strings

This commit is contained in:
Aleksander Morgado
2011-08-20 11:10:09 +02:00
parent fcadc0f841
commit fed5ad5623

View File

@@ -129,6 +129,43 @@ mmcli_modem_shutdown (void)
g_object_unref (ctxt.modem);
}
static gchar *
prefix_newlines (const gchar *prefix,
const gchar *str)
{
GString *prefixed_string = NULL;
const gchar *line_start = str;
const gchar *line_end;
while ((line_end = strchr (line_start, '\n'))) {
gssize line_length;
line_length = line_end - line_start;
if (line_start[line_length - 1] == '\r')
line_length--;
if (line_length > 0) {
if (prefixed_string) {
/* If not the first line, add the prefix */
g_string_append_printf (prefixed_string,
"\n%s", prefix);
} else {
prefixed_string = g_string_new ("");
}
g_string_append_len (prefixed_string,
line_start,
line_length);
}
line_start = line_end + 1;
}
return (prefixed_string ?
g_string_free (prefixed_string, FALSE) :
NULL);
}
static void
get_info_process_reply (gboolean result,
const GError *error,
@@ -136,12 +173,17 @@ get_info_process_reply (gboolean result,
const gchar *model,
const gchar *revision)
{
gchar *prefixed_revision;
if (!result) {
g_printerr ("couldn't get info from modem: '%s'\n",
error ? error->message : "unknown error");
exit (EXIT_FAILURE);
}
prefixed_revision = prefix_newlines (" | ",
revision);
g_print ("\n"
"%s\n"
" -------------------------\n"
@@ -152,7 +194,9 @@ get_info_process_reply (gboolean result,
ctxt.modem_str,
manufacturer,
model,
revision);
prefixed_revision ? prefixed_revision : revision);
g_free (prefixed_revision);
}
static void