cli: allow field values to be null-terminated string arrays
Field values can now be string arrays. print_fields() recognizes the format and prints values accordingly. Setter functions was added to facilitate setting string vs. array: set_val_str(), set_val_arr()
This commit is contained in:
@@ -14,7 +14,7 @@
|
|||||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||||
*
|
*
|
||||||
* (C) Copyright 2010 - 2011 Red Hat, Inc.
|
* (C) Copyright 2010 - 2012 Red Hat, Inc.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef NMC_NMCLI_H
|
#ifndef NMC_NMCLI_H
|
||||||
@@ -62,11 +62,14 @@ typedef enum {
|
|||||||
} NMCPrintOutput;
|
} NMCPrintOutput;
|
||||||
|
|
||||||
/* === Output fields === */
|
/* === Output fields === */
|
||||||
|
/* Flags for NmcOutputField */
|
||||||
|
#define NMC_OF_FLAG_ARRAY 0x00000001 /* 'value' is an NULL-terminated array rather then a single string */
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
const char *name; /* Field's name */
|
const char *name; /* Field's name */
|
||||||
const char *name_l10n; /* Field's name for translation */
|
const char *name_l10n; /* Field's name for translation */
|
||||||
int width; /* Width in screen columns */
|
int width; /* Width in screen columns */
|
||||||
const char *value; /* Value of current field */
|
const void *value; /* Value of current field - char* or char** */
|
||||||
guint32 flags; /* Flags */
|
guint32 flags; /* Flags */
|
||||||
} NmcOutputField;
|
} NmcOutputField;
|
||||||
|
|
||||||
|
@@ -14,7 +14,7 @@
|
|||||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||||
*
|
*
|
||||||
* (C) Copyright 2010 - 2011 Red Hat, Inc.
|
* (C) Copyright 2010 - 2012 Red Hat, Inc.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* Generated configuration file */
|
/* Generated configuration file */
|
||||||
@@ -96,6 +96,20 @@ nmc_string_screen_width (const char *start, const char *end)
|
|||||||
return width;
|
return width;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
set_val_str (NmcOutputField fields_array[], guint32 idx, const char *value)
|
||||||
|
{
|
||||||
|
fields_array[idx].flags = 0;
|
||||||
|
fields_array[idx].value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
set_val_arr (NmcOutputField fields_array[], guint32 idx, const char **value)
|
||||||
|
{
|
||||||
|
fields_array[idx].flags = NMC_OF_FLAG_ARRAY;
|
||||||
|
fields_array[idx].value = value;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Parse comma separated fields in 'fields_str' according to 'fields_array'.
|
* Parse comma separated fields in 'fields_str' according to 'fields_array'.
|
||||||
* IN: 'field_str': comma-separated fields names
|
* IN: 'field_str': comma-separated fields names
|
||||||
@@ -172,9 +186,8 @@ print_fields (const NmcPrintFields fields, const NmcOutputField field_values[])
|
|||||||
int table_width = 0;
|
int table_width = 0;
|
||||||
char *line = NULL;
|
char *line = NULL;
|
||||||
char *indent_str;
|
char *indent_str;
|
||||||
const char *value;
|
|
||||||
const char *not_set_str = _("not set");
|
const char *not_set_str = _("not set");
|
||||||
int i, idx;
|
int i;
|
||||||
gboolean multiline = fields.flags & NMC_PF_FLAG_MULTILINE;
|
gboolean multiline = fields.flags & NMC_PF_FLAG_MULTILINE;
|
||||||
gboolean terse = fields.flags & NMC_PF_FLAG_TERSE;
|
gboolean terse = fields.flags & NMC_PF_FLAG_TERSE;
|
||||||
gboolean pretty = fields.flags & NMC_PF_FLAG_PRETTY;
|
gboolean pretty = fields.flags & NMC_PF_FLAG_PRETTY;
|
||||||
@@ -213,15 +226,40 @@ print_fields (const NmcPrintFields fields, const NmcOutputField field_values[])
|
|||||||
if (!main_header_only && !field_names) {
|
if (!main_header_only && !field_names) {
|
||||||
for (i = 0; i < fields.indices->len; i++) {
|
for (i = 0; i < fields.indices->len; i++) {
|
||||||
char *tmp;
|
char *tmp;
|
||||||
idx = g_array_index (fields.indices, int, i);
|
int idx = g_array_index (fields.indices, int, i);
|
||||||
|
guint32 value_is_array = field_values[idx].flags & NMC_OF_FLAG_ARRAY;
|
||||||
|
|
||||||
|
/* section prefix can't be an array */
|
||||||
|
g_assert (!value_is_array || !section_prefix || idx != 0);
|
||||||
|
|
||||||
if (section_prefix && idx == 0) /* The first field is section prefix */
|
if (section_prefix && idx == 0) /* The first field is section prefix */
|
||||||
continue;
|
continue;
|
||||||
tmp = g_strdup_printf ("%s%s%s:", section_prefix ? field_values[0].value : "",
|
|
||||||
|
if (value_is_array) {
|
||||||
|
/* value is a null-terminated string array */
|
||||||
|
const char **p;
|
||||||
|
int j;
|
||||||
|
|
||||||
|
for (p = (const char **) field_values[idx].value, j = 1; p && *p; p++, j++) {
|
||||||
|
tmp = g_strdup_printf ("%s%s%s[%d]:", section_prefix ? (const char*) field_values[0].value : "",
|
||||||
|
section_prefix ? "." : "",
|
||||||
|
_(field_values[idx].name_l10n),
|
||||||
|
j);
|
||||||
|
printf ("%-*s%s\n", terse ? 0 : ML_VALUE_INDENT, tmp, *p ? *p : not_set_str);
|
||||||
|
g_free (tmp);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
/* value is a string */
|
||||||
|
const char *hdr_name = (const char*) field_values[0].value;
|
||||||
|
const char *val = (const char*) field_values[idx].value;
|
||||||
|
|
||||||
|
tmp = g_strdup_printf ("%s%s%s:", section_prefix ? hdr_name : "",
|
||||||
section_prefix ? "." : "",
|
section_prefix ? "." : "",
|
||||||
_(field_values[idx].name_l10n));
|
_(field_values[idx].name_l10n));
|
||||||
printf ("%-*s%s\n", terse ? 0 : ML_VALUE_INDENT, tmp, field_values[idx].value ? field_values[idx].value : not_set_str);
|
printf ("%-*s%s\n", terse ? 0 : ML_VALUE_INDENT, tmp, val ? val : not_set_str);
|
||||||
g_free (tmp);
|
g_free (tmp);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if (pretty) {
|
if (pretty) {
|
||||||
line = g_strnfill (ML_HEADER_WIDTH, '-');
|
line = g_strnfill (ML_HEADER_WIDTH, '-');
|
||||||
printf ("%s\n", line);
|
printf ("%s\n", line);
|
||||||
@@ -235,11 +273,16 @@ print_fields (const NmcPrintFields fields, const NmcOutputField field_values[])
|
|||||||
str = g_string_new (NULL);
|
str = g_string_new (NULL);
|
||||||
|
|
||||||
for (i = 0; i < fields.indices->len; i++) {
|
for (i = 0; i < fields.indices->len; i++) {
|
||||||
idx = g_array_index (fields.indices, int, i);
|
int idx = g_array_index (fields.indices, int, i);
|
||||||
|
guint32 value_is_array = field_values[idx].flags & NMC_OF_FLAG_ARRAY;
|
||||||
|
char *value;
|
||||||
if (field_names)
|
if (field_names)
|
||||||
value = _(field_values[idx].name_l10n);
|
value = _(field_values[idx].name_l10n);
|
||||||
else
|
else
|
||||||
value = field_values[idx].value ? field_values[idx].value : not_set_str;
|
value = field_values[idx].value ?
|
||||||
|
(value_is_array ? g_strjoinv (" | ", (char **) field_values[idx].value) : (char *) field_values[idx].value) :
|
||||||
|
(char *) not_set_str;
|
||||||
|
|
||||||
if (terse) {
|
if (terse) {
|
||||||
if (escape) {
|
if (escape) {
|
||||||
const char *p = value;
|
const char *p = value;
|
||||||
@@ -256,12 +299,13 @@ print_fields (const NmcPrintFields fields, const NmcOutputField field_values[])
|
|||||||
} else {
|
} else {
|
||||||
width1 = strlen (value);
|
width1 = strlen (value);
|
||||||
width2 = nmc_string_screen_width (value, NULL); /* Width of the string (in screen colums) */
|
width2 = nmc_string_screen_width (value, NULL); /* Width of the string (in screen colums) */
|
||||||
if (strlen (value) == 0)
|
g_string_append_printf (str, "%-*s", field_values[idx].width + width1 - width2, strlen (value) > 0 ? value : "--");
|
||||||
value = "--";
|
|
||||||
g_string_append_printf (str, "%-*s", field_values[idx].width + width1 - width2, value);
|
|
||||||
g_string_append_c (str, ' '); /* Column separator */
|
g_string_append_c (str, ' '); /* Column separator */
|
||||||
table_width += field_values[idx].width + width1 - width2 + 1;
|
table_width += field_values[idx].width + width1 - width2 + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (value_is_array && field_values[idx].value)
|
||||||
|
g_free (value);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Print the main table header */
|
/* Print the main table header */
|
||||||
|
@@ -14,7 +14,7 @@
|
|||||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||||
*
|
*
|
||||||
* (C) Copyright 2010 - 2011 Red Hat, Inc.
|
* (C) Copyright 2010 - 2012 Red Hat, Inc.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef NMC_UTILS_H
|
#ifndef NMC_UTILS_H
|
||||||
@@ -29,6 +29,8 @@ int matches (const char *cmd, const char *pattern);
|
|||||||
int next_arg (int *argc, char ***argv);
|
int next_arg (int *argc, char ***argv);
|
||||||
char *ssid_to_printable (const char *str, gsize len);
|
char *ssid_to_printable (const char *str, gsize len);
|
||||||
int nmc_string_screen_width (const char *start, const char *end);
|
int nmc_string_screen_width (const char *start, const char *end);
|
||||||
|
void set_val_str (NmcOutputField fields_array[], guint32 index, const char *value);
|
||||||
|
void set_val_arr (NmcOutputField fields_array[], guint32 index, const char **value);
|
||||||
GArray *parse_output_fields (const char *fields_str, const NmcOutputField fields_array[], GError **error);
|
GArray *parse_output_fields (const char *fields_str, const NmcOutputField fields_array[], GError **error);
|
||||||
gboolean nmc_terse_option_check (NMCPrintOutput print_output, const char *fields, GError **error);
|
gboolean nmc_terse_option_check (NMCPrintOutput print_output, const char *fields, GError **error);
|
||||||
void print_fields (const NmcPrintFields fields, const NmcOutputField field_values[]);
|
void print_fields (const NmcPrintFields fields, const NmcOutputField field_values[]);
|
||||||
|
Reference in New Issue
Block a user