Commit Graph

127 Commits

Author SHA1 Message Date
Thomas Haller
606612ea59 all: add "libnm/nm-default-client.h" as replacement for "nm-default.h" 2021-02-09 12:38:17 +01:00
Thomas Haller
977ea352a0 all: update deprecated SPDX license identifiers
These SPDX license identifiers are deprecated ([1]). Update them.

[1] https://spdx.org/licenses/

  sed \
     -e '1 s%^/\* SPDX-License-Identifier: \(GPL-2.0\|LGPL-2.1\)+ \*/$%/* SPDX-License-Identifier: \1-or-later */%' \
     -e '1,2 s%^\(--\|#\|//\) SPDX-License-Identifier: \(GPL-2.0\|LGPL-2.1\)+$%\1 SPDX-License-Identifier: \2-or-later%' \
     -i \
     $(git grep -l SPDX-License-Identifier -- \
         ':(exclude)shared/c-*/' \
         ':(exclude)shared/n-*/' \
         ':(exclude)shared/systemd/src' \
         ':(exclude)src/systemd/src')
2021-01-05 09:46:21 +01:00
Thomas Haller
4e966a7f36 cli: introduce NmcColorPalette struct instead of plain array
We do have types in C. Use them.
2020-10-09 09:52:10 +02:00
Thomas Haller
88071abb43 all: unify comment style for SPDX-License-Identifier tag
Our coding style recommends C style comments (/* */) instead of C++
(//). Also, systemd (which we partly fork) uses C style comments for
the SPDX-License-Identifier.

Unify the style.

  $ sed -i '1 s#// SPDX-License-Identifier: \([^ ]\+\)$#/* SPDX-License-Identifier: \1 */#' -- $(git ls-files -- '*.[hc]' '*.[hc]pp')
2020-09-29 16:50:53 +02:00
Thomas Haller
740b092fda format: replace tabs for indentation in code comments
sed -i \
     -e 's/^'$'\t'' \*/     */g' \
     -e 's/^'$'\t\t'' \*/         */g' \
     -e 's/^'$'\t\t\t'' \*/             */g' \
     -e 's/^'$'\t\t\t\t'' \*/                 */g' \
     -e 's/^'$'\t\t\t\t\t'' \*/                     */g' \
     -e 's/^'$'\t\t\t\t\t\t'' \*/                         */g' \
     -e 's/^'$'\t\t\t\t\t\t\t'' \*/                             */g' \
     $(git ls-files -- '*.[hc]')
2020-09-28 16:07:52 +02:00
Antonio Cardace
328fb90f3e all: reformat all with new clang-format style
Run:

    ./contrib/scripts/nm-code-format.sh -i
    ./contrib/scripts/nm-code-format.sh -i

Yes, it needs to run twice because the first run doesn't yet produce the
final result.

Signed-off-by: Antonio Cardace <acardace@redhat.com>
2020-09-28 16:07:51 +02:00
Thomas Haller
fc582312f7 cli: let _print_fill() hide option if NM_META_ACCESSOR_GET_OUT_FLAGS_HIDE is set
Regardless, whether the option is also currently the default.

(cherry picked from commit 69eca25391)
2020-04-20 08:54:55 +02:00
Thomas Haller
c5d45848dd cli: mark argv argument for command line parsing as const
It's bad style to pass the argv argument around and mutate it.
We shouldn't mutate it, and not assume that it stays around after
the function returns to the caller (meaning, we should clone the
array if we intend to use it later).

Add const specifier.
2020-04-10 10:27:27 +02:00
Thomas Haller
30b8bb476a cli: avoid using nm_cli global variable in print_data() 2020-04-04 19:28:41 +02:00
Thomas Haller
dbf697c759 cli: avoid passing full NmCli global variable to nm_cli_spawn_pager()
We should not use global variables, and we should minimize the state
that we pass around. Instead of requiring the full NmCli struct in
nm_cli_spawn_pager(), pass only the necessary data.

This reduces our use of global variables.
2020-04-04 19:28:41 +02:00
Thomas Haller
7627173c0e cli: make nmc_meta_environment_arg const pointer
Of course, we later pass the point on, where we need to cast the constness away
again. This is more a reminder that we aren't suppost to change the variable.
2020-04-04 19:28:41 +02:00
Thomas Haller
3d2b982fb7 cli: fix out of bounds access in _print_fill()
cols_len might be larger than header_row->len. That is when
the cols has entries that are not leaf entries (which currently
I think is never the case).

Fix it to use the right variable for the length of the row.
2020-03-16 13:44:12 +01:00
Thomas Haller
5bef7d7453 cli: minor cleanup dropping unnecessary local variables 2020-03-16 13:44:12 +01:00
Thomas Haller
3cc99c9f8c cli: return typed PrintDataCol array from _output_selection_parse()
It makes debugging and understanding the code slightly simpler, if we
have a pointer of correct type, instead of returning a GArray. We don't
need the GArray at this point anymore.
2020-03-16 13:44:09 +01:00
Thomas Haller
652de3b8d2 cli: use nm_utils_bin2hexstr_full() in ssid_to_hex()
We already have an implementation for converting a binary
array to hex. And, it doesn't require a GString for constructing
the output that has an known length.
2020-03-16 13:40:51 +01:00
Thomas Haller
3b69f02164 all: unify format of our Copyright source code comments
```bash

readarray -d '' FILES < <(
  git ls-files -z \
    ':(exclude)po' \
    ':(exclude)shared/c-rbtree' \
    ':(exclude)shared/c-list' \
    ':(exclude)shared/c-siphash' \
    ':(exclude)shared/c-stdaux' \
    ':(exclude)shared/n-acd' \
    ':(exclude)shared/n-dhcp4' \
    ':(exclude)src/systemd/src' \
    ':(exclude)shared/systemd/src' \
    ':(exclude)m4' \
    ':(exclude)COPYING*'
  )

sed \
  -e 's/^\(--\|#\| \*\) *\(([cC]) *\)\?Copyright \+\(\(([cC])\) \+\)\?\(\(20\|19\)[0-9][0-9]\) *[-–] *\(\(20\|19\)[0-9][0-9]\) \+\([^ ].*\)$/\1 C1pyright#\5 - \7#\9/' \
  -e 's/^\(--\|#\| \*\) *\(([cC]) *\)\?Copyright \+\(\(([cC])\) \+\)\?\(\(20\|19\)[0-9][0-9]\) *[,] *\(\(20\|19\)[0-9][0-9]\) \+\([^ ].*\)$/\1 C2pyright#\5, \7#\9/' \
  -e 's/^\(--\|#\| \*\) *\(([cC]) *\)\?Copyright \+\(\(([cC])\) \+\)\?\(\(20\|19\)[0-9][0-9]\) \+\([^ ].*\)$/\1 C3pyright#\5#\7/' \
  -e 's/^Copyright \(\(20\|19\)[0-9][0-9]\) \+\([^ ].*\)$/C4pyright#\1#\3/' \
  -i \
  "${FILES[@]}"

echo ">>> untouched Copyright lines"
git grep Copyright "${FILES[@]}"

echo ">>> Copyright lines with unusual extra"
git grep '\<C[0-9]pyright#' "${FILES[@]}" | grep -i reserved

sed \
  -e 's/\<C[0-9]pyright#\([^#]*\)#\(.*\)$/Copyright (C) \1 \2/' \
  -i \
  "${FILES[@]}"

```

https://gitlab.freedesktop.org/NetworkManager/NetworkManager/merge_requests/298
2019-10-02 17:03:52 +02:00
Thomas Haller
abff46cacf all: manually drop code comments with file description 2019-10-01 07:50:52 +02:00
Lubomir Rintel
24028a2246 all: SPDX header conversion
$ find * -type f |xargs perl contrib/scripts/spdx.pl
  $ git rm contrib/scripts/spdx.pl
2019-09-10 11:19:56 +02:00
Thomas Haller
87d16e935c cli: drop unnecessary NULL sentinel from nmc_complete_strings()
Since commit 62b939de4e ('cli: add nmc_complete_strv() which takes a
string array for completion that may contain NULL'), the sentinel is
no longer needed.
2019-04-15 20:50:50 +02:00
Thomas Haller
84f2037648 shared: add flags argument to nm_utils_strsplit_set()
It will be useful to extend nm_utils_strsplit_set() with various
flavors and subtly different behaviors. Add a flags argument to
support these.
2019-04-10 15:05:57 +02:00
Thomas Haller
9beed4f661 all: replace strerror() calls with nm_strerror_native() 2019-02-12 08:50:28 +01:00
Thomas Haller
047998f80a all: cache errno in local variable before using it 2019-02-12 08:50:28 +01:00
Thomas Haller
a3370af3a8 all: drop unnecessary includes of <errno.h> and <string.h>
"nm-macros-interal.h" already includes <errno.h> and <string.h>.
No need to include it everywhere else too.
2019-02-12 08:50:28 +01:00
Beniamino Galvani
a985efaf93 cli: fix memory leaks 2018-11-02 14:56:40 +01:00
Thomas Haller
99d3b6930b cli: don't use global variable nm_cli in nmc_terminal_spawn_pager()
print_required_fields() still accesses the global variable.
We can only move the uses of globals up the call-stack, one
bit at a time.
2018-10-10 09:55:45 +02:00
luz.paz
f985b6944a docs: misc. typos
Found via `codespell -q 3 --skip="*.po"`

https://github.com/NetworkManager/NetworkManager/pull/203
2018-09-15 09:08:03 +02:00
Beniamino Galvani
6a51d393b2 shared: add @allow_escaping argument to @nm_utils_strsplit_set 2018-08-11 09:41:07 +02:00
Thomas Haller
582ee91145 cli: add functionality to hide properties from output
Historically, nmcli printed all fields during operations like
`nmcli connection show "$PROFILE"`. As we supported more and
more options, this resulted in a verbose output, of most properties
just being the default values.

To counter that, we added the '-overview' option. When given,
it would hide options that are set at their default. This option
was not the default, to preserve established behavior.

However, for new options, we can afford to hide them. Add a mechanism,
that property getters can mark their value to be hidden. At the moment,
there is no way to show these properties. However, we could add a
'-verbose' option, with the opposite meaning of '-overview'. Anyway,
that does not seem necessary at the moment.

Hiding properties from output is only acceptable for new properties
(otherwise we badly change behavior), and if the properties are set
at their default values (otherwise, we hide important information).
2018-08-10 10:38:19 +02:00
Thomas Haller
982c74abd7 cli: remove unused argument from nm_meta_selection_create_parse_list() 2018-07-25 17:08:37 +02:00
Thomas Haller
41810aad48 cli: don't explicitly unset out_to_free argument in NMMetaType.get_nested()
The virtual function NMMetaType.get_nested() has only one caller:
nm_meta_abstract_info_get_nested(). That caller makes sure to
always pass in an @out_to_free argument, and that it is initialized
to NULL.
2018-07-25 17:08:37 +02:00
Thomas Haller
07ea00074b cli: drop local variable multiline in _print_do()
The local variable was just a copy of the (unchanging)
configuration nmc_config->multiline_output.

It is complicated enough to understand how nmc_config->print_output
and nmc_config->multiline_output affects nmc_print(). Don't use an
alias when referencing to nmc_config->multiline_output because it
hides where the value is used and where it causes a difference.
2018-07-25 17:08:37 +02:00
Thomas Haller
66e5f13307 cli: simplify tracking of parent column in PrintDataCol structure
PrintDataCol contains a reference to the parent structure, for which
it was created. Previously, that was expressed via the "parent_idx"
field, which is an index into the list of all PrintDataCol entries.

That is inconvenient. Resolve the index to the actual pointer.

Note that during _output_selection_append() we still need to use
the index instead of resolving the pointer right away. That is because
_output_selection_append() grows the GArray into which the parent_idx
pointers to. So, obtaining the real pointer at that point would result
in using a dangling pointer later on.

Introduce a new step _output_selection_complete() which converts the
index into the actual pointer.
2018-07-25 17:08:37 +02:00
Thomas Haller
ac07cbb98b cli: minor cleanup explicitly checking print_output
The print-output knows three modes: NORMAL, PRETTY, TERSE.
Never compare whether a mode is != a particular mode.

Instead, always explicitly check for the modes for which we enable a
particular behavior.

The effect is, that we always do something when the mode is set to a
particular value. We don't ever do something, if the mode is not set to
a particular value. Consistently opt-in is easier to understand.
2018-07-25 17:08:37 +02:00
Thomas Haller
8f037a11f4 cli: minor cleanup evaluating print_output
The print-output knows three modes: NORMAL, PRETTY, TERSE.
Instead of using local variables "pretty" and "terse",
check for the output mode directly.

Note how we have tree modes, so mapping them to two boolean
variables is confusing. Especially at one place where we did:
  pretty = (nmc_config->print_output != NMC_PRINT_TERSE);
while at other places:
  pretty = (nmc_config->print_output == NMC_PRINT_PRETTY);
2018-07-25 17:08:37 +02:00
Thomas Haller
e912ca7622 cli: minor cleanup in _print_data_cell_clear_text()
It wasn't entirely clear what this was doing.

Like before, free and clear the text pointers.
However, also always reset the text_format back to PLAIN.
2018-07-25 17:08:37 +02:00
Thomas Haller
9c51ebc298 cli: fix skipping columns with all default-values
The header-cell is the header for a column with possibly many
rows.

We must not set:
  header_cell->skip = nmc_config->overview && is_default;
for a particular cell, because it does not take into accound
the neighbouring rows. It is only correct, if there is only
one row.

Invert the logical meaning and rename "strip" to "to_print".
Each cell then can opt-in, whether it needs to be printed.
Only if no cell opts-in, it will be skipped.

Fixes: 9a19bbcb2f
2018-07-25 17:08:37 +02:00
Thomas Haller
ba350a3495 cli: add additional user-data argument to get_fcn()
The function nmc_print() receives a list of "targets". These are essentially
the rows that should be printed (while the "fields" list represents the columns).

When filling the cells with values, it calles repeatedly get_fcn() on the
column descriptors (fields), by passing each row (target).

The caller must be well aware that the fields and targets are
compatible. For example, in some cases the targets are NMDevice
instances and the target type must correspond to what get_fcn()
expects.

Add another user-data pointer that is passed on along with the
targets. That is useful, if we have a list of targets/rows, but
pass in additional data that applies to all rows alike.

It is still unused.
2018-07-09 15:43:55 +02:00
Thomas Haller
4e7ae3da6e cli: fix printing multi-line output when there is nothing to print
When a property getter returns an empty/missing strv-array, in multi-line
mode we should not print any lines.

To get that right, we must mark the cell as STRV type, even if there is no value
provided.

Previously, with text_out_flags having NM_META_ACCESSOR_GET_OUT_FLAGS_STRV
and value being NULL, we would not set
  cell->text_format = PRINT_DATA_CELL_FORMAT_TYPE_STRV;
and thus, later on the value would be treated as a missing (plain)
string.
2018-06-15 11:48:16 +02:00
Lubomir Rintel
e6abc96e13 cli/utils: make next_arg() recognize arguments that are not in "--option" form
This is going to make it possible to parse and complete argument lists in one go:

  -nmc_complete_strings (*argv, "ifname", "bssid", NULL);
  -next_arg (nmc, &argc, &argv, NULL);
  -if (strcmp (*argv, "ifname") == 0)
  -...
  -else if (strcmp (*argv, "bssid") == 0)
  -...
  +option = next_arg (nmc, &argc, &argv, "ifname", "bssid", NULL)
  +switch (option) {
  +case 1:
  +...
  +case 2:
  +...

Beautiful.
2018-06-13 14:44:18 +02:00
Thomas Haller
a1b25a47b0 cli: rework printing of nmcli connection for multiple active connections
The output of `nmcli connection show` contains also information about
whether the profile is currently active, for example the device and
the current (activation) state.

Even when a profile can be activated only once (without supporting
mutiple activations at the same time), there are moments when a
connection is activating and still deactivating on another device.
NetworkManager ensures in the case with single activations that
a profile cannot be in state "activated" multiple times. But that
doesn't mean, that one profile cannot have multiple active connection
which reference it. That was already handled wrongly before, because
`nmcli connection show` would only search the first matching
active-connection. That is, it would arbitrarily pick an active
connection in case there were multiple and only show activation
state about one.
Furthermore, we will soon also add the possibility, that a profile can be
active multiple times (at the same time). Especially then, we need to
extend the output format to show all the devices on which the profile is
currently active.

Rework printing the connection list to use nmc_print(), and fix various
issues.

- as discussed, a profile may have multiple active connections at each time.
  There are only two possibilities: if a profile is active multiple
  times, show a line for each activation, or otherwise, show the
  information about multiple activations combined in one line, e.g. by
  printing "DEVICE eth0,eth1". This patch, does the former.
  We will now print a line for each active connection, to show
  all the devices and activation states in multiple lines.
  Yes, this may result in the same profile being printed multiple times.
  That is a change in behavior, and inconvenient if you do something
  like

     for UUID in $(nmcli connection show | awk '{print$2}'); do ...

  However, above is anyway wrong because it assumes that there are no
  spaces in the connection name. The proper way to do this is like

     for UUID in $(nmcli -g UUID connection show); do ...

  In the latter case, whenever a user selects a subset of fields
  (--fields, --get) which don't print information about active connections,
  these multiple lines are combined. So, above still works as expected,
  never returning duplicate UUIDs.

- if a user has no permissions to see a connection, we previously
  would print "<invisible> $NAME". No longer do this but just print
  the ID was it is reported by the active-connection. If the goal
  of this was to prevent users from accidentally access the non-existing
  connection by $NAME, then this was a bad solution, because a script
  would instead try to access "<invisible> $NAME". This is now solved
  better by hiding the active connection if the user selects "-g NAME".

- the --order option now sorts according to how the fields are shown.
  For example, with --terse mode, it will evaluate type "802-11-wireless"
  but with pretty mode it will consider "wifi". This may change the
  ordering in which connections are shown. Also, for sorting the name,
  we use g_utf8_collate() because it's unicode.
2018-06-01 16:03:23 +02:00
Thomas Haller
e1bbc2e155 cli: avoid bogus line seprator in pretty-output
This bug resulted in spurious lines with "--pretty --mode tabular",
whenever nmc_print() was called with multiple rows.

Currently, the only case where this was visible was with:

  $ nmcli --pretty general permissions

(note that "--mode tabular" is the default).

Fixes: 16299e5ac0
2018-05-25 17:36:47 +02:00
Thomas Haller
fc475f25f2 cli: use macro for list of argument for NmcMetaGenericInfo.get_fcn()
The reasons are:

- I want to locate all implmenetations of the get_fcn() handler. By
  consistently using this macro, you can just grep for the macro and
  find them all.

- all implementations should follow the same style. This macro
  enforces the same names for arguments and avoids copy&paste.

- if we are going to add or change an argument, it becomes easier.
  That's because we can easily identify all implementation and can
  change arguments in one place.
2018-05-14 17:53:03 +02:00
Lubomir Rintel
31aa2cfe29 cli: use a palette to implement coloring
This basically replaces the (NMMetaTermColor, NMMetaTermFormat) combo
with NMMetaColor that describes the colored element semantically as
opposed to storing the raw attributes.

A (currently static) paletted is used to translate the semantic color
code to the actual ANSI controle sequence. This matches what
terminal-colors.d(5) schemes use, making it convenient to implement
customizable palettes.
2018-05-10 14:36:58 +02:00
Lubomir Rintel
9dfe825840 cli: rework enabling and disabling colors
This actually makes very little difference at the moment, but will make
things more confortable later on, when the logic of enabling/disabling
coloring will involve terminal-colors.d(5).

Instead of deciding whether to use colors lazily with use_colors(), it's
done very early on nmcli initialization and a boolean use_colors field
is stored in the NmcConfig instance instead of the raw tristate option
of NmcColorOption type (which is now confined to nmcli.c).

Wherever the NmcColorOption was used previously, the whole NmcConfig
instance is passed around. That might seem pointless (since only the
use_colors boolean is actually used at the moment), but will be utilized
to pass around the actual color palette in future.
2018-05-10 14:36:58 +02:00
Lubomir Rintel
56a5b27389 cli: drop --prompt-color
It's undocumented, useless, somewhat expensive in volume of code and
probably just downright stupid. We'll get a more general way to set
colors.

Hacking in some code to keep this working wouldn't be too difficult, but
it seems entirely pointless.
2018-05-10 14:36:58 +02:00
Lubomir Rintel
e69d386975 all: use the elvis operator wherever possible
Coccinelle:

  @@
  expression a, b;
  @@
  -a ? a : b
  +a ?: b

Applied with:

  spatch --sp-file ternary.cocci --in-place --smpl-spacing --dir .

With some manual adjustments on spots that Cocci didn't catch for
reasons unknown.

Thanks to the marvelous effort of the GNU compiler developer we can now
spare a couple of bits that could be used for more important things,
like this commit message. Standards commitees yet have to catch up.
2018-05-10 14:36:58 +02:00
Beniamino Galvani
1b5925ce88 all: remove consecutive empty lines
Normalize coding style by removing consecutive empty lines from C
sources and headers.

https://github.com/NetworkManager/NetworkManager/pull/108
2018-04-30 16:24:52 +02:00
Beniamino Galvani
9a19bbcb2f nmcli: add overview option to skip default values in output
Add a new 'overview' command line option to make the output more
compact and display only properties that have non-default
values. Currently the option has only effect for the "connection show
$CON" sub-command.

 $ nmcli -o connection show wifi-home
 connection.id:                          wifi-home
 connection.uuid:                        8308c425-f2a7-4021-9afc-37bde7253c6d
 connection.type:                        802-11-wireless
 connection.timestamp:                   1519264421
 connection.permissions:                 user:me
 802-11-wireless.ssid:                   home
 802-11-wireless.mode:                   infrastructure
 802-11-wireless-security.key-mgmt:      wpa-psk
 802-11-wireless-security.auth-alg:      open
 ipv4.method:                            auto
 ipv6.method:                            auto

https://bugzilla.redhat.com/show_bug.cgi?id=1434527
2018-04-13 17:02:55 +02:00
Beniamino Galvani
1f2319dcb9 clients: change nm_meta_abstract_info_get() to report defaults
Return a boolean to indicate whether the value is the default one, so
that the caller can choose to hide it.
2018-04-13 17:02:55 +02:00
Thomas Haller
28da0154fc all: drop trailing spaces 2018-02-07 13:32:04 +01:00