From fe764f4205a43258f51ebd5576af92427fa53466 Mon Sep 17 00:00:00 2001 From: Finlay Davidson Date: Thu, 23 Dec 2021 02:06:26 +0100 Subject: [PATCH] Add bash-completion support Loosely based on bash-completion's sudo support --- GNUmakefile | 10 +++++++--- doas.completion | 19 +++++++++++++++++++ 2 files changed, 26 insertions(+), 3 deletions(-) create mode 100644 doas.completion diff --git a/GNUmakefile b/GNUmakefile index 2eef88e..22dbc2d 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -1,7 +1,8 @@ -PROG= doas -MAN= doas.1 doas.conf.5 +PROG= doas +MAN= doas.1 doas.conf.5 +COMPLETION= doas.completion -SRCS= parse.y doas.c env.c +SRCS= parse.y doas.c env.c include config.mk @@ -19,17 +20,20 @@ install: ${PROG} ${MAN} mkdir -p -m 0755 ${DESTDIR}${BINDIR} mkdir -p -m 0755 ${DESTDIR}${MANDIR}/man1 mkdir -p -m 0755 ${DESTDIR}${MANDIR}/man5 + mkdir -p -m 0755 ${DESTDIR}/share/bash-completion/completions cp -f ${PROG} ${DESTDIR}${BINDIR} chown ${BINOWN}:${BINGRP} ${DESTDIR}${BINDIR}/${PROG} chmod ${BINMODE} ${DESTDIR}${BINDIR}/${PROG} cp -f doas.1 ${DESTDIR}${MANDIR}/man1 cp -f doas.conf.5 ${DESTDIR}${MANDIR}/man5 + cp -f doas.completion ${DESTDIR}/share/bash-completion/completions/doas uninstall: rm -f ${DESTDIR}${BINDIR}/${PROG} rm -f ${DESTDIR}${PAMDIR}/doas rm -f ${DESTDIR}${MANDIR}/man1/doas.1 rm -f ${DESTDIR}${MANDIR}/man5/doas.conf.5 + rm -f ${DESTDIR}/share/bash-completion/completions/doas clean: rm -f ${PROG} ${OBJS} ${OBJS:.o=.d} parse.c diff --git a/doas.completion b/doas.completion new file mode 100644 index 0000000..374eb40 --- /dev/null +++ b/doas.completion @@ -0,0 +1,19 @@ +# bash completion for doas(1) -*- shell-script -*- + +_doas() { + local cur prev words cword split + _init_completion -s || return + + for ((i = 1; i <= cword; i++)); do + if [[ ${words[i]} != -* ]]; then + local PATH=$PATH:/sbin:/usr/sbin:/usr/local/sbin + local root_command=${words[i]} + _command_offset $i + return + fi + + [[ ${words[i]} == -u ]] || [[ ${words[i]} == -C ]] && ((i++)) + done +} && complete -F _doas doas + +# ex: filetype=sh