NetworkManager/cli/src/nmcli.h
Jiří Klimeš e6870789b5 cli: enhance printing to align tabular output properly and not to waste space
Until now we have used a static width defined for each column for tabular
output. Even if this worked in most cases, it was not optimal, because by
using too wide columns we wasted space, and in case of a too narrow column the
alignment broke. So, we need to know the longest string in a column to be able
to align columns in the tabular output. Thus, the printing has to be postponed
till we have all data available, and can find the widest column. This value is
then used for aligning while printing the data.

Arrays of NmcOutputField (rows) are inserted into output_data array. When all
data have been added, print_data() can be used to print the whole output_data
array with proper alignment.

A single row can be printed using print_required_fields().

Also, output flags are redone to better match the new output_data array.
The flags are needed for every row (in tabular output); they are stored in
the first field (NmcOutputField) for the whole row.

Addapted set_val_str() and set_val_arr() to set value type (char * x char **).
Added set_val_strc(), set_val_arrc() for const values that should not be freed.

output_data takes ownership of the data added to it and takes care of freeing
the memory.

See e.g.
https://bugzilla.gnome.org/show_bug.cgi?id=699503
2013-05-31 09:38:03 +02:00

121 lines
5 KiB
C

/* nmcli - command-line tool to control NetworkManager
*
* 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.
*
* (C) Copyright 2010 - 2012 Red Hat, Inc.
*/
#ifndef NMC_NMCLI_H
#define NMC_NMCLI_H
#include <glib.h>
#include <nm-client.h>
#include <nm-remote-settings.h>
/* nmcli exit codes */
typedef enum {
/* Indicates successful execution */
NMC_RESULT_SUCCESS = 0,
/* Unknown / unspecified error */
NMC_RESULT_ERROR_UNKNOWN = 1,
/* Wrong invocation of nmcli */
NMC_RESULT_ERROR_USER_INPUT = 2,
/* A timeout expired */
NMC_RESULT_ERROR_TIMEOUT_EXPIRED = 3,
/* Error in connection activation */
NMC_RESULT_ERROR_CON_ACTIVATION = 4,
/* Error in connection deactivation */
NMC_RESULT_ERROR_CON_DEACTIVATION = 5,
/* Error in device disconnect */
NMC_RESULT_ERROR_DEV_DISCONNECT = 6,
/* Error in connection deletion */
NMC_RESULT_ERROR_CON_DEL = 7,
/* NetworkManager is not running */
NMC_RESULT_ERROR_NM_NOT_RUNNING = 8,
/* nmcli and NetworkManager versions mismatch */
NMC_RESULT_ERROR_VERSIONS_MISMATCH = 9
} NMCResultCode;
typedef enum {
NMC_PRINT_TERSE = 0,
NMC_PRINT_NORMAL = 1,
NMC_PRINT_PRETTY = 2
} NMCPrintOutput;
/* === Output fields === */
/* Flags for NmcOutputField */
#define NMC_OF_FLAG_FIELD_NAMES 0x00000001 /* Print field names instead of values */
#define NMC_OF_FLAG_SECTION_PREFIX 0x00000002 /* Use the first value as section prefix for the other field names - just in multiline */
#define NMC_OF_FLAG_MAIN_HEADER_ADD 0x00000004 /* Print main header in addition to values/field names */
#define NMC_OF_FLAG_MAIN_HEADER_ONLY 0x00000008 /* Print main header only */
typedef struct {
const char *name; /* Field's name */
const char *name_l10n; /* Field's name for translation */
int width; /* Width in screen columns */
void *value; /* Value of current field - char* or char** (NULL-terminated array) */
gboolean value_is_array; /* Whether value is char ** instead of char* */
gboolean free_value; /* Whether to free the value */
guint32 flags; /* Flags - whether and how to print values/field names/headers */
} NmcOutputField;
typedef struct {
GArray *indices; /* Array of field indices to the array of allowed fields */
char *header_name; /* Name of the output */
int indent; /* Indent by this number of spaces */
} NmcPrintFields;
/* NmCli - main structure */
typedef struct _NmCli {
NMClient *client; /* Pointer to NMClient of libnm-glib */
NMClient *(*get_client) (struct _NmCli *nmc); /* Pointer to function for creating NMClient */
NMCResultCode return_value; /* Return code of nmcli */
GString *return_text; /* Reason text */
int timeout; /* Operation timeout */
NMRemoteSettings *system_settings; /* System settings */
gboolean system_settings_running; /* Is system settings service running? */
GSList *system_connections; /* List of system connections */
gboolean should_wait; /* Indication that nmcli should not end yet */
gboolean nowait_flag; /* '--nowait' option; used for passing to callbacks */
NMCPrintOutput print_output; /* Output mode */
gboolean multiline_output; /* Multiline output instead of default tabular */
gboolean mode_specified; /* Whether tabular/multiline mode was specified via '--mode' option */
gboolean escape_values; /* Whether to escape ':' and '\' in terse tabular mode */
char *required_fields; /* Required fields in output: '--fields' option */
GPtrArray *output_data; /* GPtrArray of arrays of NmcOutputField structs - accumulates data for output */
NmcPrintFields print_fields; /* Structure with field indices to print */
gboolean nocheck_ver; /* Don't check nmcli and NM versions: option '--nocheck' */
gboolean ask; /* Ask for missing parameters: option '--ask' */
} NmCli;
/* Error quark for GError domain */
#define NMCLI_ERROR (nmcli_error_quark ())
GQuark nmcli_error_quark (void);
#endif /* NMC_NMCLI_H */