nmcli.h 7.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* 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.
 *
17
 * Copyright 2010 - 2015 Red Hat, Inc.
18 19 20 21 22
 */

#ifndef NMC_NMCLI_H
#define NMC_NMCLI_H

23 24
#include "NetworkManager.h"
#include "nm-secret-agent-old.h"
25

26 27 28 29 30 31 32
#if WITH_POLKIT_AGENT
#include "nm-polkit-listener.h"
#else
/* polkit agent is not available; define fake NMPolkitListener */
typedef gpointer NMPolkitListener;
#endif

33 34 35 36 37 38
/* nmcli exit codes */
typedef enum {
	/* Indicates successful execution */
	NMC_RESULT_SUCCESS = 0,

	/* Unknown / unspecified error */
39 40 41 42
	NMC_RESULT_ERROR_UNKNOWN = 1,

	/* Wrong invocation of nmcli */
	NMC_RESULT_ERROR_USER_INPUT = 2,
43 44

	/* A timeout expired */
45
	NMC_RESULT_ERROR_TIMEOUT_EXPIRED = 3,
46 47

	/* Error in connection activation */
48
	NMC_RESULT_ERROR_CON_ACTIVATION = 4,
49 50

	/* Error in connection deactivation */
51
	NMC_RESULT_ERROR_CON_DEACTIVATION = 5,
52 53

	/* Error in device disconnect */
54 55
	NMC_RESULT_ERROR_DEV_DISCONNECT = 6,

56 57 58
	/* Error in connection deletion */
	NMC_RESULT_ERROR_CON_DEL = 7,

59
	/* NetworkManager is not running */
60
	NMC_RESULT_ERROR_NM_NOT_RUNNING = 8,
61

62
	/* No more used, keep to preserve API */
63 64 65
	NMC_RESULT_ERROR_VERSIONS_MISMATCH = 9,

	/* Connection/Device/AP not found */
66 67 68 69
	NMC_RESULT_ERROR_NOT_FOUND = 10,

	/* --complete-args signals a file name may follow */
	NMC_RESULT_COMPLETE_FILE = 65,
70 71
} NMCResultCode;

72 73 74 75 76 77 78 79 80 81 82 83
typedef enum {
	NMC_TERM_COLOR_NORMAL  = 0,
	NMC_TERM_COLOR_BLACK   = 1,
	NMC_TERM_COLOR_RED     = 2,
	NMC_TERM_COLOR_GREEN   = 3,
	NMC_TERM_COLOR_YELLOW  = 4,
	NMC_TERM_COLOR_BLUE    = 5,
	NMC_TERM_COLOR_MAGENTA = 6,
	NMC_TERM_COLOR_CYAN    = 7,
	NMC_TERM_COLOR_WHITE   = 8
} NmcTermColor;

84 85 86 87 88 89 90 91 92 93
typedef enum {
	NMC_TERM_FORMAT_NORMAL,
	NMC_TERM_FORMAT_BOLD,
	NMC_TERM_FORMAT_DIM,
	NMC_TERM_FORMAT_UNDERLINE,
	NMC_TERM_FORMAT_BLINK,
	NMC_TERM_FORMAT_REVERSE,
	NMC_TERM_FORMAT_HIDDEN,
} NmcTermFormat;

94 95
typedef enum {
	NMC_PRINT_TERSE = 0,
96 97
	NMC_PRINT_NORMAL = 1,
	NMC_PRINT_PRETTY = 2
98 99
} NMCPrintOutput;

100
/* === Output fields === */
101
/* Flags for NmcOutputField */
102 103 104 105
#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 */
106

107
struct _NMMetaSettingInfoEditor;
108

109 110 111
typedef struct _NmcOutputField {
	const char *name;               /* Field's name */
	int width;                      /* Width in screen columns */
112
	const struct _NmcOutputField *group_list; /* Points to an array with available section field names if this is a section (group) field */
113 114 115 116
	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 */
117
	NmcTermColor color;             /* Use this color to print value */
118
	NmcTermFormat color_fmt;        /* Use this terminal format to print value */
119 120 121 122 123 124 125

	/* in a very particular case NmcOutputField is used in combination with
	 * the @group_list above. That list will go away (and the entire NmcOutputField
	 * should separate formatting-options, setting-metadata and output.
	 *
	 * For now, hack around that by alternatively providing a @setting_info instead
	 * of @group_list. */
126
	const struct _NMMetaSettingInfoEditor *setting_info;
127 128 129 130 131 132 133 134
} 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;

135 136 137 138 139 140
typedef enum {
	NMC_USE_COLOR_AUTO,
	NMC_USE_COLOR_YES,
	NMC_USE_COLOR_NO,
} NmcColorOption;

141
typedef struct _NmcConfig {
142
	NMCPrintOutput print_output;                      /* Output mode */
143
	NmcColorOption use_colors;                        /* Whether to use colors for output: option '--color' */
144 145 146
	gboolean multiline_output;                        /* Multiline output instead of default tabular */
	gboolean escape_values;                           /* Whether to escape ':' and '\' in terse tabular mode */
	gboolean in_editor;                               /* Whether running the editor - nmcli con edit' */
147 148
} NmcConfig;

149 150 151 152 153
typedef struct _NmcOutputData {
	GPtrArray *output_data;                           /* GPtrArray of arrays of NmcOutputField structs - accumulates data for output */
	NmcPrintFields print_fields;                      /* Structure with field indices to print */
} NmcOutputData;

154 155
/* NmCli - main structure */
typedef struct _NmCli {
156
	NMClient *client;                                 /* Pointer to NMClient of libnm */
157

158 159
	NMCResultCode return_value;                       /* Return code of nmcli */
	GString *return_text;                             /* Reason text */
160

161
	int timeout;                                      /* Operation timeout */
162

163
	NMSecretAgentOld *secret_agent;                   /* Secret agent */
164
	GHashTable *pwds_hash;                            /* Hash table with passwords in passwd-file */
165
	NMPolkitListener *pk_listener ;                   /* polkit agent listener */
166

167
	int should_wait;                                  /* Semaphore indicating whether nmcli should not end or not yet */
168
	gboolean nowait_flag;                             /* '--nowait' option; used for passing to callbacks */
169
	gboolean mode_specified;                          /* Whether tabular/multiline mode was specified via '--mode' option */
170 171 172 173
	union {
		const NmcConfig nmc_config;
		NmcConfig nmc_config_mutable;
	};
174
	char *required_fields;                            /* Required fields in output: '--fields' option */
175
	gboolean ask;                                     /* Ask for missing parameters: option '--ask' */
176
	gboolean complete;                                /* Autocomplete the command line */
177
	gboolean show_secrets;                            /* Whether to display secrets (both input and output): option '--show-secrets' */
178
	gboolean editor_status_line;                      /* Whether to display status line in connection editor */
179
	gboolean editor_save_confirmation;                /* Whether to ask for confirmation on saving connections with 'autoconnect=yes' */
180
	gboolean editor_show_secrets;                     /* Whether to display secrets in the editor' */
181
	NmcTermColor editor_prompt_color;                 /* Color of prompt in connection editor */
182 183
} NmCli;

184 185
extern NmCli nm_cli;

186 187 188 189
/* Error quark for GError domain */
#define NMCLI_ERROR (nmcli_error_quark ())
GQuark nmcli_error_quark (void);

190 191
gboolean nmc_seen_sigint (void);
void     nmc_clear_sigint (void);
192
void     nmc_set_sigquit_internal (void);
193
void     nmc_exit (void);
194

195 196 197 198 199 200 201
void nmc_empty_output_fields (NmcOutputData *output_data);

#define NMC_OUTPUT_DATA_DEFINE_SCOPED(out) \
	nm_auto (nmc_empty_output_fields) NmcOutputData out = { \
		.output_data = g_ptr_array_new_full (20, g_free), \
	}

202
#endif /* NMC_NMCLI_H */