summaryrefslogtreecommitdiff
path: root/clients/cli/nmcli.h
blob: 85f536518db465d026b08af19b8e86cada94ef20 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/* 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.
 *
 * Copyright 2010 - 2017 Red Hat, Inc.
 */

#ifndef NMC_NMCLI_H
#define NMC_NMCLI_H

#include "nm-secret-agent-old.h"
#include "nm-meta-setting-desc.h"
#include "nm-polkit-listener.h"

typedef char *(*NmcCompEntryFunc) (const char *, int);

/* 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,

	/* No more used, keep to preserve API */
	NMC_RESULT_ERROR_VERSIONS_MISMATCH = 9,

	/* Connection/Device/AP not found */
	NMC_RESULT_ERROR_NOT_FOUND = 10,

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

typedef enum {
	NMC_PRINT_TERSE = 0,
	NMC_PRINT_NORMAL = 1,
	NMC_PRINT_PRETTY = 2
} NMCPrintOutput;

/* === Output fields === */

typedef enum {
	NMC_OF_FLAG_FIELD_NAMES        = 0x00000001,   /* Print field names instead of values */
	NMC_OF_FLAG_SECTION_PREFIX     = 0x00000002,   /* Use the first value as section prefix for the other field names - just in multiline */
	NMC_OF_FLAG_MAIN_HEADER_ADD    = 0x00000004,   /* Print main header in addition to values/field names */
	NMC_OF_FLAG_MAIN_HEADER_ONLY   = 0x00000008,   /* Print main header only */
} NmcOfFlags;

extern const NMMetaType nmc_meta_type_generic_info;

typedef struct _NmcOutputField NmcOutputField;
typedef struct _NmcMetaGenericInfo NmcMetaGenericInfo;

struct _NmcOutputField {
	const NMMetaAbstractInfo *info;
	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 */
	NmcOfFlags flags;               /* Flags - whether and how to print values/field names/headers */
	NMMetaTermColor color;             /* Use this color to print value */
	NMMetaTermFormat color_fmt;        /* Use this terminal format to print value */
};

typedef enum {
	NMC_USE_COLOR_AUTO,
	NMC_USE_COLOR_YES,
	NMC_USE_COLOR_NO,
} NmcColorOption;

typedef struct _NmcConfig {
	NMCPrintOutput print_output;                      /* Output mode */
	NmcColorOption use_colors;                        /* Whether to use colors for output: option '--color' */
	bool multiline_output;                            /* Multiline output instead of default tabular */
	bool escape_values;                               /* Whether to escape ':' and '\' in terse tabular mode */
	bool in_editor;                                   /* Whether running the editor - nmcli con edit' */
	bool show_secrets;                                /* Whether to display secrets (both input and output): option '--show-secrets' */
} NmcConfig;

typedef struct _NmcOutputData {
	GPtrArray *output_data;                           /* GPtrArray of arrays of NmcOutputField structs - accumulates data for output */
} NmcOutputData;

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

	NMCResultCode return_value;                       /* Return code of nmcli */
	GString *return_text;                             /* Reason text */
	pid_t pager_pid;                                  /* PID of a pager, if one was spawned */

	int timeout;                                      /* Operation timeout */

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

	int should_wait;                                  /* Semaphore indicating whether nmcli should not end or not yet */
	gboolean nowait_flag;                             /* '--nowait' option; used for passing to callbacks */
	gboolean mode_specified;                          /* Whether tabular/multiline mode was specified via '--mode' option */
	union {
		const NmcConfig nmc_config;
		NmcConfig nmc_config_mutable;
	};
	char *required_fields;                            /* Required fields in output: '--fields' option */
	gboolean ask;                                     /* Ask for missing parameters: option '--ask' */
	gboolean complete;                                /* Autocomplete the command line */
	gboolean editor_status_line;                      /* Whether to display status line in connection editor */
	gboolean editor_save_confirmation;                /* Whether to ask for confirmation on saving connections with 'autoconnect=yes' */
	NMMetaTermColor editor_prompt_color;              /* Color of prompt in connection editor */
} NmCli;

extern NmCli nm_cli;

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

extern GMainLoop *loop;

gboolean nmc_seen_sigint (void);
void     nmc_clear_sigint (void);
void     nmc_set_sigquit_internal (void);
void     nmc_exit (void);

void nmc_empty_output_fields (NmcOutputData *output_data);

#define NMC_OUTPUT_DATA_DEFINE_SCOPED(out) \
	gs_unref_array GArray *out##_indices = NULL; \
	nm_auto (nmc_empty_output_fields) NmcOutputData out = { \
		.output_data = g_ptr_array_new_full (20, g_free), \
	}

#endif /* NMC_NMCLI_H */