[arch-projects] [INITSCRIPTS][PATCH] rc.d: list [started|stopped|auto|noauto|daemon]
list command can take new arguments auto/noauto. list command can take a list of daemon to list. All kind of arguments can be mixed to obtain the proper output. zsh and bash completion are updated Signed-off-by: Sebastien Luttringer <seblu@seblu.net> --- bash-completion | 2 +- rc.d | 71 ++++++++++++++++++++++++++++++++++++++---------------- zsh-completion | 2 +- 3 files changed, 52 insertions(+), 23 deletions(-) diff --git a/bash-completion b/bash-completion index d78484e..718df6d 100644 --- a/bash-completion +++ b/bash-completion @@ -10,7 +10,7 @@ _rc_d() elif [[ "$prev" == help ]]; then COMPREPLY=() elif [[ "$prev" == list ]]; then - ((COMP_CWORD == 2)) && COMPREPLY=($(compgen -W "started stopped" -- "$cur")) || COMPREPLY=() + COMPREPLY=($(compgen -W "started stopped auto noauto $(cd /etc/rc.d && compgen -f -X 'functions*')" -- "$cur")) elif [[ "$prev" == start ]]; then COMPREPLY=($(comm -23 <(cd /etc/rc.d && compgen -f -X 'functions*' "$cur"|sort) <(cd /run/daemons/ && compgen -f "$cur"|sort))) elif [[ "$prev" =~ stop|restart|reload ]]; then diff --git a/rc.d b/rc.d index 3d34edf..81a07eb 100755 --- a/rc.d +++ b/rc.d @@ -4,11 +4,12 @@ NEED_ROOT=0 # this script can be run without be root . /etc/rc.conf . /etc/rc.d/functions +# print usage and exit usage() { local name=${0##*/} cat >&2 << EOF usage: $name <action> <daemon> [daemon] ... - $name list [started|stopped] + $name list [started|stopped|auto|noauto|daemon] $name help <daemon> is the name of a script in /etc/rc.d @@ -17,40 +18,68 @@ WARNING: initscripts are free to implement or not the above actions. e.g: $name list $name list started + $name list sshd gpm $name help $name start sshd gpm EOF exit 1 } +# list action +list() { + local mode s_status s_auto + local -a daemons + # parse arguments + for p; do + case "$p" in + started) mode=started;; + stopped) mode=stopped;; + auto) mode=auto;; + noauto) mode=noauto;; + *) daemons=(${daemons[@]} "$p");; + esac + done + # if no daemon are specified take all executable file in /etc/rc.d + if ! [[ $daemons ]]; then + cd /etc/rc.d + for d in *; do + [[ -x "$d" && ! -d "$d" ]] && daemons=(${daemons[@]} "$d") + done + fi + # building string to display + for d in "${daemons[@]}"; do + # check if d is a valid daemon name + have_daemon "$d" || continue + # print running / stopped satus + if ! ck_daemon "$d"; then + [[ $mode == stopped ]] && continue + s_status="${C_OTHER}[${C_DONE}STARTED${C_OTHER}]" + else + [[ $mode == started ]] && continue + s_status="${C_OTHER}[${C_FAIL}STOPPED${C_OTHER}]" + fi + # print auto / manual status + if ! ck_autostart "$d"; then + [[ $mode == noauto ]] && continue + s_auto="${C_OTHER}[${C_DONE}AUTO${C_OTHER}]" + else + [[ $mode == auto ]] && continue + s_auto="${C_OTHER}[${C_FAIL} ${C_OTHER}]" + fi + printf "$s_status$s_auto${C_CLEAR} $d\n" + done +} + (( $# < 1 )) && usage declare -i ret=0 case $1 in help) usage - ;; + ;; list) shift - cd /etc/rc.d/ - for d in *; do - have_daemon "$d" || continue - # print running / stopped satus - if ! ck_daemon "$d"; then - [[ "$1" == stopped ]] && continue - printf "${C_OTHER}[${C_DONE}STARTED${C_OTHER}]" - else - [[ "$1" == started ]] && continue - printf "${C_OTHER}[${C_FAIL}STOPPED${C_OTHER}]" - fi - # print auto / manual status - if ! ck_autostart "$d"; then - printf "${C_OTHER}[${C_DONE}AUTO${C_OTHER}]" - else - printf "${C_OTHER}[${C_FAIL} ${C_OTHER}]" - fi - printf " ${C_CLEAR}$d\n" - done + list "$@" ;; *) # check min args count diff --git a/zsh-completion b/zsh-completion index e5c2850..b324e5c 100644 --- a/zsh-completion +++ b/zsh-completion @@ -19,7 +19,7 @@ _rc.d () { _arguments "*: :" ;; list) - _arguments "2: :(started stopped)" + _arguments "*: :(started stopped auto noauto $(echo /etc/rc.d/*(N-*:t)))" ;; start) _arguments "*: :($(comm -23 <(echo /etc/rc.d/*(N-*:t)|tr ' ' '\n') <(echo /run/daemons/*(N:t)|tr ' ' '\n')))" -- Sebastien "Seblu" Luttringer
On Mon, Aug 22, 2011 at 08:21:07PM +0200, Sebastien Luttringer wrote:
list command can take new arguments auto/noauto. list command can take a list of daemon to list.
All kind of arguments can be mixed to obtain the proper output.
zsh and bash completion are updated
Signed-off-by: Sebastien Luttringer <seblu@seblu.net> --- bash-completion | 2 +- rc.d | 71 ++++++++++++++++++++++++++++++++++++++---------------- zsh-completion | 2 +- 3 files changed, 52 insertions(+), 23 deletions(-)
diff --git a/bash-completion b/bash-completion index d78484e..718df6d 100644 --- a/bash-completion +++ b/bash-completion @@ -10,7 +10,7 @@ _rc_d() elif [[ "$prev" == help ]]; then COMPREPLY=() elif [[ "$prev" == list ]]; then - ((COMP_CWORD == 2)) && COMPREPLY=($(compgen -W "started stopped" -- "$cur")) || COMPREPLY=() + COMPREPLY=($(compgen -W "started stopped auto noauto $(cd /etc/rc.d && compgen -f -X 'functions*')" -- "$cur")) elif [[ "$prev" == start ]]; then COMPREPLY=($(comm -23 <(cd /etc/rc.d && compgen -f -X 'functions*' "$cur"|sort) <(cd /run/daemons/ && compgen -f "$cur"|sort))) elif [[ "$prev" =~ stop|restart|reload ]]; then diff --git a/rc.d b/rc.d index 3d34edf..81a07eb 100755 --- a/rc.d +++ b/rc.d @@ -4,11 +4,12 @@ NEED_ROOT=0 # this script can be run without be root . /etc/rc.conf . /etc/rc.d/functions
+# print usage and exit usage() { local name=${0##*/} cat >&2 << EOF usage: $name <action> <daemon> [daemon] ... - $name list [started|stopped] + $name list [started|stopped|auto|noauto|daemon] $name help
<daemon> is the name of a script in /etc/rc.d @@ -17,40 +18,68 @@ WARNING: initscripts are free to implement or not the above actions.
e.g: $name list $name list started + $name list sshd gpm $name help $name start sshd gpm EOF exit 1 }
+# list action +list() { + local mode s_status s_auto + local -a daemons + # parse arguments + for p; do + case "$p" in + started) mode=started;; + stopped) mode=stopped;; + auto) mode=auto;; + noauto) mode=noauto;; + *) daemons=(${daemons[@]} "$p");;
Hm, wouldn't that mean we would never be able to explicitly list daemons called "started", "stopped", "auto" and "noauto"? I know that this is nit-picking, but I would prefer another argument "daemons" or "all", followed by a list of daemons, so we could use: * `rc.d list daemons` to list all daemons (or just `rc.d list`). * `rc.d list started` to list all running daemons. * `rc.d list daemons sshd gpm` to list specific daemons only. * `rc.d list started sshd gpm` to list specific running daemons only. Also, you should not indent mixing tabs and spaces. This breaks indentation for everyone using a different tab width.
+ esac + done + # if no daemon are specified take all executable file in /etc/rc.d + if ! [[ $daemons ]]; then
"[[ ! $daemons ]]". Or any reason to do the negation outside the conditional expression here?
+ cd /etc/rc.d + for d in *; do + [[ -x "$d" && ! -d "$d" ]] && daemons=(${daemons[@]} "$d")
'daemons=("${daemons[@]}" "$d")'. Just to be sure we don't fail here if a daemon ever contains a space (and since you used proper quoting everywhere else).
+ done + fi + # building string to display + for d in "${daemons[@]}"; do + # check if d is a valid daemon name + have_daemon "$d" || continue + # print running / stopped satus + if ! ck_daemon "$d"; then + [[ $mode == stopped ]] && continue + s_status="${C_OTHER}[${C_DONE}STARTED${C_OTHER}]" + else + [[ $mode == started ]] && continue + s_status="${C_OTHER}[${C_FAIL}STOPPED${C_OTHER}]" + fi + # print auto / manual status + if ! ck_autostart "$d"; then + [[ $mode == noauto ]] && continue + s_auto="${C_OTHER}[${C_DONE}AUTO${C_OTHER}]" + else + [[ $mode == auto ]] && continue + s_auto="${C_OTHER}[${C_FAIL} ${C_OTHER}]" + fi + printf "$s_status$s_auto${C_CLEAR} $d\n" + done +} + (( $# < 1 )) && usage
declare -i ret=0 case $1 in help) usage - ;; + ;; list) shift - cd /etc/rc.d/ - for d in *; do - have_daemon "$d" || continue - # print running / stopped satus - if ! ck_daemon "$d"; then - [[ "$1" == stopped ]] && continue - printf "${C_OTHER}[${C_DONE}STARTED${C_OTHER}]" - else - [[ "$1" == started ]] && continue - printf "${C_OTHER}[${C_FAIL}STOPPED${C_OTHER}]" - fi - # print auto / manual status - if ! ck_autostart "$d"; then - printf "${C_OTHER}[${C_DONE}AUTO${C_OTHER}]" - else - printf "${C_OTHER}[${C_FAIL} ${C_OTHER}]" - fi - printf " ${C_CLEAR}$d\n" - done + list "$@" ;; *) # check min args count diff --git a/zsh-completion b/zsh-completion index e5c2850..b324e5c 100644 --- a/zsh-completion +++ b/zsh-completion @@ -19,7 +19,7 @@ _rc.d () { _arguments "*: :" ;; list) - _arguments "2: :(started stopped)" + _arguments "*: :(started stopped auto noauto $(echo /etc/rc.d/*(N-*:t)))"
Whey, another winner of the "Useless use of echo" award [1]! :)
;; start) _arguments "*: :($(comm -23 <(echo /etc/rc.d/*(N-*:t)|tr ' ' '\n') <(echo /run/daemons/*(N:t)|tr ' ' '\n')))" -- Sebastien "Seblu" Luttringer
On Tue, Aug 23, 2011 at 10:01 AM, Lukas Fleischer <archlinux@cryptocrack.de> wrote:
Hm, wouldn't that mean we would never be able to explicitly list daemons called "started", "stopped", "auto" and "noauto"? I know that this is nit-picking, but I would prefer another argument "daemons" or "all", followed by a list of daemons, so we could use:
* `rc.d list daemons` to list all daemons (or just `rc.d list`). * `rc.d list started` to list all running daemons. * `rc.d list daemons sshd gpm` to list specific daemons only. * `rc.d list started sshd gpm` to list specific running daemons only.
Bof. I prefer something like rc.d list \auto to list auto deaemons. It's less complicated.
Also, you should not indent mixing tabs and spaces. This breaks indentation for everyone using a different tab width.
I checked but i don't see mixing tabs and space. The only point where i use space for indentation is in string of usage. Where it's correct to ensure a proper display. Can you be more precise.
+ esac + done + # if no daemon are specified take all executable file in /etc/rc.d + if ! [[ $daemons ]]; then
"[[ ! $daemons ]]". Or any reason to do the negation outside the conditional expression here?
Reason to do it inside ?
+ cd /etc/rc.d + for d in *; do + [[ -x "$d" && ! -d "$d" ]] && daemons=(${daemons[@]} "$d")
'daemons=("${daemons[@]}" "$d")'. Just to be sure we don't fail here if a daemon ever contains a space (and since you used proper quoting everywhere else).
i tested with a daemon with space. np.
--- a/zsh-completion +++ b/zsh-completion @@ -19,7 +19,7 @@ _rc.d () { _arguments "*: :" ;; list) - _arguments "2: :(started stopped)" + _arguments "*: :(started stopped auto noauto $(echo /etc/rc.d/*(N-*:t)))"
Whey, another winner of the "Useless use of echo" award [1]! :) For a weird reason, some daemons are missing without the use of echo here. I doesn't succeed to reproduce it, so i will change this.
Thanks for review! -- Sébastien Luttringer www.seblu.net
On Tue, Aug 23, 2011 at 10:36:27AM +0200, Seblu wrote:
On Tue, Aug 23, 2011 at 10:01 AM, Lukas Fleischer <archlinux@cryptocrack.de> wrote:
Hm, wouldn't that mean we would never be able to explicitly list daemons called "started", "stopped", "auto" and "noauto"? I know that this is nit-picking, but I would prefer another argument "daemons" or "all", followed by a list of daemons, so we could use:
* `rc.d list daemons` to list all daemons (or just `rc.d list`). * `rc.d list started` to list all running daemons. * `rc.d list daemons sshd gpm` to list specific daemons only. * `rc.d list started sshd gpm` to list specific running daemons only.
Bof. I prefer something like rc.d list \auto to list auto deaemons. It's less complicated.
Uh? :) Not sure if you misunderstood me, `rc.d list auto`, as well as `rc.d list noauto` and `rc.d list stopped` will still work with what I suggested. The only difference to your proposal is that there is a separate keyword to use if you want to list specific daemons. You actually already support listing specific running daemons via `rc.d list started sshd gpm`, specific auto daemons via `rc.d list auto sshd gpm` etc., so having another keyword to list *all* daemons felt like the right thing to do here. Anyway, this is/was just a thought.
Also, you should not indent mixing tabs and spaces. This breaks indentation for everyone using a different tab width.
I checked but i don't see mixing tabs and space. The only point where i use space for indentation is in string of usage. Where it's correct to ensure a proper display.
Can you be more precise.
Not talking about the actual indentation itself, but about the mixing of spaces and tabs between the matching patterns in the case statement and the code.
+ esac + done + # if no daemon are specified take all executable file in /etc/rc.d + if ! [[ $daemons ]]; then
"[[ ! $daemons ]]". Or any reason to do the negation outside the conditional expression here?
Reason to do it inside ?
Only a matter of clarity and coding style. If we want to check for a false value explicitly, having the negation inside the conditional expression makes things clearer. "! [[ $daemons ]]" looks like "Check if $daemons is defined. If that is not true, do [...]". "[[ ! $daemons ]]" looks like "If $daemons is undefined, do [...]". If you don't feel like doing it or if you always use negations outside the conditional brackets in initscripts, just keep it :)
+ cd /etc/rc.d + for d in *; do + [[ -x "$d" && ! -d "$d" ]] && daemons=(${daemons[@]} "$d")
'daemons=("${daemons[@]}" "$d")'. Just to be sure we don't fail here if a daemon ever contains a space (and since you used proper quoting everywhere else).
i tested with a daemon with space. np.
---- $ touch "bar foo" foobar $ typeset -a daemons $ for d in *; do daemons=(${daemons[@]} "$d"); done $ IFS=, $ echo "${daemons[*]}" bar,foo,foobar ---- Funny part is that it works correctly if you run it a second time. This is due to the input field separator being set to ",", though. We should not make assumptions on the IFS. Using quotes is the right thing to do here.
--- a/zsh-completion +++ b/zsh-completion @@ -19,7 +19,7 @@ _rc.d () { _arguments "*: :" ;; list) - _arguments "2: :(started stopped)" + _arguments "*: :(started stopped auto noauto $(echo /etc/rc.d/*(N-*:t)))"
Whey, another winner of the "Useless use of echo" award [1]! :) For a weird reason, some daemons are missing without the use of echo here. I doesn't succeed to reproduce it, so i will change this.
Thanks for review!
You're welcome!
On Tue, Aug 23, 2011 at 11:57:20AM +0200, Lukas Fleischer wrote:
On Tue, Aug 23, 2011 at 10:36:27AM +0200, Seblu wrote:
On Tue, Aug 23, 2011 at 10:01 AM, Lukas Fleischer
+ esac + done + # if no daemon are specified take all executable file in /etc/rc.d + if ! [[ $daemons ]]; then
"[[ ! $daemons ]]". Or any reason to do the negation outside the conditional expression here? Reason to do it inside ?
Only a matter of clarity and coding style. If we want to check for a false value explicitly, having the negation inside the conditional expression makes things clearer. "! [[ $daemons ]]" looks like "Check if $daemons is defined. If that is not true, do [...]". "[[ ! $daemons ]]" looks like "If $daemons is undefined, do [...]". If you don't feel like doing it or if you always use negations outside the conditional brackets in initscripts, just keep it :)
If you want to be consistent, we use '[[ -z' everywhere else. '[[ !' isn't wrong, its just unexpected in our codebase.
+ cd /etc/rc.d + for d in *; do + [[ -x "$d" && ! -d "$d" ]] && daemons=(${daemons[@]} "$d")
'daemons=("${daemons[@]}" "$d")'. Just to be sure we don't fail here if a daemon ever contains a space (and since you used proper quoting everywhere else).
i tested with a daemon with space. np.
Generally we use array+=("$another"). There's nothing wrong with this persay, just don't reverse it -- bash is exponentially slower when prepending to an array.
---- <eww zsh>
d
On Tue, Aug 23, 2011 at 12:10 PM, Dave Reisner <d@falconindy.com> wrote:
If you want to be consistent, we use '[[ -z' everywhere else. '[[ !' isn't wrong, its just unexpected in our codebase. ok
Generally we use array+=("$another"). There's nothing wrong with this persay, just don't reverse it -- bash is exponentially slower when prepending to an array.
This was fixed in last version isn't it ? -- Sébastien Luttringer www.seblu.net
On Aug 23, 2011 7:27 AM, "Seblu" <seblu@seblu.net> wrote:
On Tue, Aug 23, 2011 at 12:10 PM, Dave Reisner <d@falconindy.com> wrote:
If you want to be consistent, we use '[[ -z' everywhere else. '[[ !' isn't wrong, its just unexpected in our codebase. ok
Generally we use array+=("$another"). There's nothing wrong with this persay, just don't reverse it -- bash is exponentially slower when prepending to an array.
This was fixed in last version isn't it ?
I guess it is. Reading patches on the bus doesn't work out as well as I'd like it to. D
participants (5)
-
Dave Reisner
-
dave reisner
-
Lukas Fleischer
-
Sebastien Luttringer
-
Seblu