Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

{ls,ins,rm}mod: update help, man and {bash,fish,zsh} shell completion #138

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,22 @@ noarch_pkgconfig_DATA = tools/kmod.pc

bashcompletiondir=@bashcompletiondir@
dist_bashcompletion_DATA = \
shell-completion/bash/kmod
shell-completion/bash/insmod \
shell-completion/bash/kmod \
shell-completion/bash/lsmod \
shell-completion/bash/rmmod

fishcompletiondir=@fishcompletiondir@
dist_fishcompletion_DATA = \
shell-completion/fish/insmod.fish \
shell-completion/fish/lsmod.fish \
shell-completion/fish/rmmod.fish

zshcompletiondir=@zshcompletiondir@
dist_zshcompletion_DATA = \
shell-completion/zsh/_insmod \
shell-completion/zsh/_lsmod \
shell-completion/zsh/_rmmod

install-exec-hook:
if BUILD_TOOLS
Expand Down Expand Up @@ -338,7 +353,9 @@ EXTRA_DIST += testsuite/rootfs-pristine

AM_DISTCHECK_CONFIGURE_FLAGS=--enable-gtk-doc --sysconfdir=/etc \
--with-zlib --with-zstd --with-xz --with-openssl \
--with-bashcompletiondir=$$dc_install_base/$(bashcompletiondir)
--with-bashcompletiondir=$$dc_install_base/$(bashcompletiondir) \
--with-fishcompletiondir=$$dc_install_base/$(fishcompletiondir) \
--with-zshcompletiondir=$$dc_install_base/$(zshcompletiondir)

distclean-local: $(DISTCLEAN_LOCAL_HOOKS)

Expand Down
16 changes: 16 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,22 @@ AC_ARG_WITH([bashcompletiondir],
])])
AC_SUBST([bashcompletiondir], [$with_bashcompletiondir])

AC_ARG_WITH([fishcompletiondir],
AS_HELP_STRING([--with-fishcompletiondir=DIR], [Fish completions directory]),
[],
[AS_IF([$($PKG_CONFIG --exists fish)], [
with_fishcompletiondir=$($PKG_CONFIG --variable=completionsdir fish)
] , [
with_fishcompletiondir=${datadir}/fish/vendor_functions.d
])])
AC_SUBST([fishcompletiondir], [$with_fishcompletiondir])

AC_ARG_WITH([zshcompletiondir],
AS_HELP_STRING([--with-zshcompletiondir=DIR], [Zsh completions directory]),
[],
[with_zshcompletiondir=${datadir}/zsh/site-functions])
AC_SUBST([zshcompletiondir], [$with_zshcompletiondir])

#####################################################################
# --enable-
#####################################################################
Expand Down
49 changes: 40 additions & 9 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -191,16 +191,47 @@ if moduledir == ''
endif
cdata.set_quoted('MODULE_DIRECTORY', moduledir)

bashcompletiondir = get_option('bashcompletiondir')
if bashcompletiondir == ''
bashcompletion = dependency('bash-completion', required : false)
if bashcompletion.found()
bashcompletiondir = bashcompletion.get_variable(pkgconfig : 'completionsdir')
else
bashcompletiondir = join_paths(get_option('prefix'), get_option('datadir'),
'bash-completion/completions')
_completiondirs = [
['bashcompletiondir', 'bash-completion', 'bash-completion/completions', 'shell-completion/bash/@0@'],
['fishcompletiondir', 'fish', 'fish/vendor_functions.d', 'shell-completion/fish/@[email protected]'],
['zshcompletiondir', '', 'zsh/site-functions', 'shell-completion/zsh/_@0@'],
]

foreach tuple : _completiondirs
dir_option = tuple[0]
pkg_dep = tuple[1]
def_path = tuple[2]
ins_path = tuple[3]

completiondir = get_option(dir_option)
if completiondir == ''
completion = dependency(pkg_dep, required : false)
if completion.found()
completiondir = completion.get_variable(pkgconfig : 'completionsdir')
else
completiondir = join_paths(get_option('prefix'), get_option('datadir'),
def_path)
endif
endif
endif

_completions = [
'insmod',
'lsmod',
'rmmod',
]

if completiondir != 'no'
foreach comp : _completions
install_data(
files(ins_path.format(comp)),
install_dir : completiondir,
)
endforeach
endif

# NEEDED solely for bash/kmod below
set_variable(dir_option, completiondir)
endforeach

if bashcompletiondir != 'no'
install_data(
Expand Down
12 changes: 12 additions & 0 deletions meson_options.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@ option(
description : 'Bash completions directory. Use "no" to disable.',
)

option(
'fishcompletiondir',
type : 'string',
description : 'Fish completions directory. Use "no" to disable.',
)

option(
'zshcompletiondir',
type : 'string',
description : 'Zsh completions directory. Use "no" to disable.',
)

evelikov marked this conversation as resolved.
Show resolved Hide resolved
# Compression options
option(
'zstd',
Expand Down
35 changes: 35 additions & 0 deletions shell-completion/bash/insmod
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# insmod(8) completion -*- shell-script -*-
# SPDX-License-Identifier: LGPL-2.1-or-later
#
# SPDX-FileCopyrightText: 2024 Emil Velikov <[email protected]>
#
# Formatted using:
# shfmt --language-dialect bash --indent 4 --func-next-line

_insmod()
{
# long/short opt pairs
local -A opts=(
['force']='f'
['syslog']='s'
['verbose']='v'
['version']='V'
['help']='h'
)

local cur="${COMP_WORDS[COMP_CWORD]}"

if [[ $cur == --* ]]; then
COMPREPLY=($(compgen -P '--' -W "${!opts[*]}" -- "${cur:2}"))
elif [[ $cur == -* ]]; then
if (( ${#cur} != 2 )); then
COMPREPLY=($(compgen -P '--' -W "${!opts[*]}" -- "${cur:2}"))
fi
COMPREPLY+=($(compgen -P '-' -W "${opts[*]}" -- "${cur:1}"))
else
# TODO: match only one file
_filedir '@(ko?(.gz|.xz|.zst))'
# TODO: add module parameter completion, based of modinfo
fi
} &&
complete -F _insmod insmod
30 changes: 30 additions & 0 deletions shell-completion/bash/lsmod
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# lsmod(8) completion -*- shell-script -*-
# SPDX-License-Identifier: LGPL-2.1-or-later
#
# SPDX-FileCopyrightText: 2024 Emil Velikov <[email protected]>
#
# Formatted using:
# shfmt --language-dialect bash --indent 4 --func-next-line

_lsmod()
{
# long/short opt pairs
local -A opts=(
['syslog']='s'
['verbose']='v'
['version']='V'
['help']='h'
)

local cur="${COMP_WORDS[COMP_CWORD]}"

if [[ $cur == --* ]]; then
COMPREPLY=($(compgen -P '--' -W "${!opts[*]}" -- "${cur:2}"))
elif [[ $cur == -* ]]; then
if (( ${#cur} != 2 )); then
COMPREPLY=($(compgen -P '--' -W "${!opts[*]}" -- "${cur:2}"))
fi
COMPREPLY+=($(compgen -P '-' -W "${opts[*]}" -- "${cur:1}"))
fi
} &&
complete -F _lsmod lsmod
34 changes: 34 additions & 0 deletions shell-completion/bash/rmmod
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# rmmod(8) completion -*- shell-script -*-
# SPDX-License-Identifier: LGPL-2.1-or-later
#
# SPDX-FileCopyrightText: 2024 Emil Velikov <[email protected]>
#
# Formatted using:
# shfmt --language-dialect bash --indent 4 --func-next-line

_rmmod()
{
# long/short opt pairs
local -A opts=(
['force']='f'
['syslog']='s'
['verbose']='v'
['version']='V'
['help']='h'
)

local cur="${COMP_WORDS[COMP_CWORD]}"

if [[ $cur == --* ]]; then
COMPREPLY=($(compgen -P '--' -W "${!opts[*]}" -- "${cur:2}"))
elif [[ $cur == -* ]]; then
if (( ${#cur} != 2 )); then
COMPREPLY=($(compgen -P '--' -W "${!opts[*]}" -- "${cur:2}"))
fi
COMPREPLY+=($(compgen -P '-' -W "${opts[*]}" -- "${cur:1}"))
else
local -a modules=($(lsmod | cut -f1 -d' '))
COMPREPLY=($(compgen -W "${modules[*]}" -- "${cur}"))
fi
} &&
complete -F _rmmod rmmod
21 changes: 21 additions & 0 deletions shell-completion/fish/insmod.fish
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# insmod(8) completion -*- shell-script -*-
# SPDX-License-Identifier: LGPL-2.1-or-later
#
# SPDX-FileCopyrightText: 2024 Emil Velikov <[email protected]>

# globally disable file completions
complete -c insmod -f

complete -c insmod -s f -l force -d "DANGEROUS: forces a module load, may cause data corruption and crash your machine"
complete -c insmod -s s -l syslog -d 'print to syslog, not stderr'
complete -c insmod -s v -l verbose -d 'enables more messages'
complete -c insmod -s V -l version -d 'show version'
complete -c insmod -s h -l help -d 'show this help'

# provide an exclusive (x) list of required (r) answers (a), keeping (k) the
# matches at the top.
# TODO: match only one file
# BUG: fish lists everything, even when only the given suffix is requested. Plus
# we need to explicitly keep them sorted.
complete -c insmod -x -ra "(__fish_complete_suffix .ko .ko.gz .ko.xz .ko.zst)" -k
# TODO: add module parameter completion, based of modinfo
12 changes: 12 additions & 0 deletions shell-completion/fish/lsmod.fish
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# lsmod(8) completion -*- shell-script -*-
# SPDX-License-Identifier: LGPL-2.1-or-later
#
# SPDX-FileCopyrightText: 2024 Emil Velikov <[email protected]>

# globally disable file completions
complete -c lsmod -f

complete -c lsmod -s s -l syslog -d 'print to syslog, not stderr'
complete -c lsmod -s v -l verbose -d 'enables more messages'
complete -c lsmod -s V -l version -d 'show version'
complete -c lsmod -s h -l help -d 'show this help'
16 changes: 16 additions & 0 deletions shell-completion/fish/rmmod.fish
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# rmmod(8) completion -*- shell-script -*-
# SPDX-License-Identifier: LGPL-2.1-or-later
#
# SPDX-FileCopyrightText: 2024 Emil Velikov <[email protected]>

# globally disable file completions
complete -c rmmod -f

complete -c rmmod -s f -l force -d "DANGEROUS: forces a module unload and may crash your machine"
complete -c rmmod -s s -l syslog -d 'print to syslog, not stderr'
complete -c rmmod -s v -l verbose -d 'enables more messages'
complete -c rmmod -s V -l version -d 'show version'
complete -c rmmod -s h -l help -d 'show this help'

# provide an exclusive (x) list of required (r) answers (a)
complete -c rmmod -x -ra "(lsmod | cut -f1 -d' ')"
14 changes: 14 additions & 0 deletions shell-completion/zsh/_insmod
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#compdef insmod

# insmod(8) completion -*- shell-script -*-
# SPDX-License-Identifier: LGPL-2.1-or-later
#
# SPDX-FileCopyrightText: 2024 Emil Velikov <[email protected]>

_arguments \
{-f,--force}'[DANGEROUS: forces a module load, may cause data corruption and crash your machine]' \
{-s,--syslog}'[print to syslog, not stderr]' \
{-v,--verbose}'[enables more messages]' \
{-V,--version}'[show version]' \
{-h,--help}'[show this help]' \
'1::module:_files -g "*.ko(|.gz|.xz|.zst)"'
12 changes: 12 additions & 0 deletions shell-completion/zsh/_lsmod
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#compdef lsmod

# lsmod(8) completion -*- shell-script -*-
# SPDX-License-Identifier: LGPL-2.1-or-later
#
# SPDX-FileCopyrightText: 2024 Emil Velikov <[email protected]>

_arguments \
{-s,--syslog}'[print to syslog, not stderr]' \
{-v,--verbose}'[enables more messages]' \
{-V,--version}'[show version]' \
{-h,--help}'[show this help]'
21 changes: 21 additions & 0 deletions shell-completion/zsh/_rmmod
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#compdef rmmod

# rmmod(8) completion -*- shell-script -*-
# SPDX-License-Identifier: LGPL-2.1-or-later
#
# SPDX-FileCopyrightText: 2024 Emil Velikov <[email protected]>

(( $+functions[_rmmod_modules] )) || _rmmod_modules()
{
local -a _modules
_modules=(${${(f)"$(_call_program modules lsmod)"}[2,-1]%% *})
_values 'modules' "$_modules[@]"
}

_arguments \
{-f,--force}'[DANGEROUS: forces a module unload and may crash your machine]' \
{-s,--syslog}'[print to syslog, not stderr]' \
{-v,--verbose}'[enables more messages]' \
{-V,--version}'[show version]' \
{-h,--help}'[show this help]' \
'*::modules:_rmmod_modules'