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.,
|
||||
* 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
|
||||
@@ -62,23 +62,26 @@ typedef enum {
|
||||
} NMCPrintOutput;
|
||||
|
||||
/* === Output fields === */
|
||||
/* Flags for NmcOutputField */
|
||||
#define NMC_OF_FLAG_ARRAY 0x00000001 /* 'value' is an NULL-terminated array rather then a single string */
|
||||
|
||||
typedef struct {
|
||||
const char *name; /* Field's name */
|
||||
const char *name_l10n; /* Field's name for translation */
|
||||
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 */
|
||||
} NmcOutputField;
|
||||
|
||||
/* Flags for NmcPrintFields */
|
||||
#define NMC_PF_FLAG_MULTILINE 0x00000001 /* Multiline output instead of tabular */
|
||||
#define NMC_PF_FLAG_TERSE 0x00000002 /* Terse output mode */
|
||||
#define NMC_PF_FLAG_PRETTY 0x00000004 /* Pretty output mode */
|
||||
#define NMC_PF_FLAG_MAIN_HEADER_ADD 0x00000008 /* Print main header in addition to values/field names */
|
||||
#define NMC_PF_FLAG_MAIN_HEADER_ONLY 0x00000010 /* Print main header only */
|
||||
#define NMC_PF_FLAG_FIELD_NAMES 0x00000020 /* Print field names instead of values */
|
||||
#define NMC_PF_FLAG_ESCAPE 0x00000040 /* Escape column separator and '\' */
|
||||
#define NMC_PF_FLAG_SECTION_PREFIX 0x00000080 /* Use the first value as section prefix for the other field names - just in multiline */
|
||||
#define NMC_PF_FLAG_MULTILINE 0x00000001 /* Multiline output instead of tabular */
|
||||
#define NMC_PF_FLAG_TERSE 0x00000002 /* Terse output mode */
|
||||
#define NMC_PF_FLAG_PRETTY 0x00000004 /* Pretty output mode */
|
||||
#define NMC_PF_FLAG_MAIN_HEADER_ADD 0x00000008 /* Print main header in addition to values/field names */
|
||||
#define NMC_PF_FLAG_MAIN_HEADER_ONLY 0x00000010 /* Print main header only */
|
||||
#define NMC_PF_FLAG_FIELD_NAMES 0x00000020 /* Print field names instead of values */
|
||||
#define NMC_PF_FLAG_ESCAPE 0x00000040 /* Escape column separator and '\' */
|
||||
#define NMC_PF_FLAG_SECTION_PREFIX 0x00000080 /* Use the first value as section prefix for the other field names - just in multiline */
|
||||
|
||||
typedef struct {
|
||||
GArray *indices; /* Array of field indices to the array of allowed fields */
|
||||
|
@@ -14,7 +14,7 @@
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 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 */
|
||||
@@ -96,6 +96,20 @@ nmc_string_screen_width (const char *start, const char *end)
|
||||
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'.
|
||||
* IN: 'field_str': comma-separated fields names
|
||||
@@ -172,9 +186,8 @@ print_fields (const NmcPrintFields fields, const NmcOutputField field_values[])
|
||||
int table_width = 0;
|
||||
char *line = NULL;
|
||||
char *indent_str;
|
||||
const char *value;
|
||||
const char *not_set_str = _("not set");
|
||||
int i, idx;
|
||||
int i;
|
||||
gboolean multiline = fields.flags & NMC_PF_FLAG_MULTILINE;
|
||||
gboolean terse = fields.flags & NMC_PF_FLAG_TERSE;
|
||||
gboolean pretty = fields.flags & NMC_PF_FLAG_PRETTY;
|
||||
@@ -213,14 +226,39 @@ print_fields (const NmcPrintFields fields, const NmcOutputField field_values[])
|
||||
if (!main_header_only && !field_names) {
|
||||
for (i = 0; i < fields.indices->len; i++) {
|
||||
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 */
|
||||
continue;
|
||||
tmp = g_strdup_printf ("%s%s%s:", section_prefix ? field_values[0].value : "",
|
||||
section_prefix ? "." : "",
|
||||
_(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);
|
||||
g_free (tmp);
|
||||
|
||||
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 ? "." : "",
|
||||
_(field_values[idx].name_l10n));
|
||||
printf ("%-*s%s\n", terse ? 0 : ML_VALUE_INDENT, tmp, val ? val : not_set_str);
|
||||
g_free (tmp);
|
||||
}
|
||||
}
|
||||
if (pretty) {
|
||||
line = g_strnfill (ML_HEADER_WIDTH, '-');
|
||||
@@ -235,11 +273,16 @@ print_fields (const NmcPrintFields fields, const NmcOutputField field_values[])
|
||||
str = g_string_new (NULL);
|
||||
|
||||
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)
|
||||
value = _(field_values[idx].name_l10n);
|
||||
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 (escape) {
|
||||
const char *p = value;
|
||||
@@ -250,18 +293,19 @@ print_fields (const NmcPrintFields fields, const NmcOutputField field_values[])
|
||||
p++;
|
||||
}
|
||||
}
|
||||
else
|
||||
else
|
||||
g_string_append_printf (str, "%s", value);
|
||||
g_string_append_c (str, ':'); /* Column separator */
|
||||
} else {
|
||||
width1 = strlen (value);
|
||||
width2 = nmc_string_screen_width (value, NULL); /* Width of the string (in screen colums) */
|
||||
if (strlen (value) == 0)
|
||||
value = "--";
|
||||
g_string_append_printf (str, "%-*s", field_values[idx].width + width1 - width2, value);
|
||||
g_string_append_printf (str, "%-*s", field_values[idx].width + width1 - width2, strlen (value) > 0 ? value : "--");
|
||||
g_string_append_c (str, ' '); /* Column separator */
|
||||
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 */
|
||||
|
@@ -14,7 +14,7 @@
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 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
|
||||
@@ -29,6 +29,8 @@ int matches (const char *cmd, const char *pattern);
|
||||
int next_arg (int *argc, char ***argv);
|
||||
char *ssid_to_printable (const char *str, gsize len);
|
||||
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);
|
||||
gboolean nmc_terse_option_check (NMCPrintOutput print_output, const char *fields, GError **error);
|
||||
void print_fields (const NmcPrintFields fields, const NmcOutputField field_values[]);
|
||||
|
Reference in New Issue
Block a user