[arch-projects] [initscripts] simple rc.d bash completion script v2

Auguste Pop auguste at gmail.com
Fri May 20 10:21:32 EDT 2011


this is another version of the bash completion script.

removed an unnecessary echo and allows auto completion for not
well-adopted usage like rc.d status foo.

here's the patch:

commit 47c48b15047ae46dd2e3d56559dad17d57f78c91
Author: Auguste Pop <auguste at gmail.com>
Date:   Fri May 20 22:13:31 2011 +0800

    basic rc.d bash completion

diff --git a/Makefile b/Makefile
index ad8a058..a7f3ad2 100644
--- a/Makefile
+++ b/Makefile
@@ -1,5 +1,5 @@
 VER  := $(shell git describe)
-DIRS := /etc/rc.d /etc/conf.d /etc/rc.d/functions.d /etc/cron.hourly
/sbin /etc/modprobe.d
+DIRS := /etc/rc.d /etc/conf.d /etc/rc.d/functions.d /etc/cron.hourly
/sbin /etc/modprobe.d /etc/bash_completion.d

 minilogd: minilogd.o

@@ -13,6 +13,7 @@ install: minilogd installdirs
 	install -m644 -t $(DESTDIR)/etc/rc.d functions
 	install -m755 -t $(DESTDIR)/etc/rc.d hwclock network netfs
 	install -m755 -t $(DESTDIR)/sbin minilogd rc.d modprobe-blacklist
+	install -m644 bash_completion $(DESTDIR)/etc/bash_completion.d/rc.d
 	ln -s /run/initscripts/modprobe-blacklist.conf
$(DESTDIR)/etc/modprobe.d/arch-blacklist.conf

 clean:
diff --git a/bash_completion b/bash_completion
new file mode 100644
index 0000000..9d766fe
--- /dev/null
+++ b/bash_completion
@@ -0,0 +1,56 @@
+#!/bin/bash
+
+_rc_d_getdaemons ()
+{
+    local -a files=("$1"/*)
+    echo "${files[@]##*/}"
+}
+
+_rc_d_arraydiff ()
+{
+    local -A arr
+
+    for elem in "${!1}"; do
+        arr[$elem]=1
+    done
+    for elem in "${!2}"; do
+        unset arr[$elem]
+    done
+    echo "${!arr[@]}"
+}
+
+_rc_d ()
+{
+    local command commands available started
+    local cur words cword
+
+    commands="list help start stop restart"
+    available=($(_rc_d_getdaemons /etc/rc.d))
+    started=($(_rc_d_getdaemons /run/daemons))
+
+    _get_comp_words_by_ref -n =: cur words cword
+    [[ $cword -gt 1 ]] && command=${words[1]}
+
+    if [[ -z "$command" ]]; then
+        COMPREPLY=($(compgen -W "$commands" -- $cur))
+    else
+        case "$command" in
+            start)
+                COMPREPLY=($(compgen -W "$(_rc_d_arraydiff \
+                    available[@] started[@])" -X "functions*" -- $cur))
+                ;;
+            stop|restart)
+                COMPREPLY=($(compgen -W "${started[*]}" -- $cur))
+                ;;
+            list|help)
+                COMPREPLY=()
+                ;;
+            *)
+                COMPREPLY=($(compgen -W "${available[*]}" \
+                    -X "functions*" --$cur))
+                ;;
+        esac
+    fi
+}
+
+complete -F _rc_d rc.d
-------------- next part --------------
A non-text attachment was scrubbed...
Name: bash_completion.patch
Type: text/x-patch
Size: 2389 bytes
Desc: not available
URL: <http://mailman.archlinux.org/pipermail/arch-projects/attachments/20110520/f4c42bab/attachment.bin>


More information about the arch-projects mailing list