On Tue, Aug 23, 2011 at 05:35:47PM +0200, Seblu wrote:
On Tue, Aug 23, 2011 at 4:58 PM, Lukas Fleischer <archlinux@cryptocrack.de> wrote:
On Tue, Aug 23, 2011 at 03:32:32PM +0200, Sebastien Luttringer wrote: [...]
+# parse options +argv=$(getopt -l 'started,stopped,auto,noauto' -- '' "$@") +(( $? )) && usage
`argv=$(getopt -l 'started,stopped,auto,noauto' -- '' "$@") || usage`.
+eval set -- "$argv" + +# going into script directory +cd /etc/rc.d + +# create an initial daemon list +declare -a daemons=() +while [[ "$1" != -- ]]; do + case "$1" in + --started) + for d in *; do have_daemon "$d" && ! ck_daemon "$d" && daemons+=("$d"); done + ;; + --stopped) + for d in *; do have_daemon "$d" && ck_daemon "$d" && daemons+=("$d"); done + ;; + --auto) + for d in *; do have_daemon "$d" && ! ck_autostart "$d" && daemons+=("$d"); done + ;; + --noauto) + for d in *; do have_daemon "$d" && ck_autostart "$d" && daemons+=("$d"); done + ;;
Oh. I thought of an implicit conjunction over the single filter options here. This is advantageous if you want to list running auto-started daemons (`rc.d list --started --auto`) etc. I would just set some flag when parsing the options and filter the daemon list in a single loop later on (which is faster, also).
+ esac + shift +done + +# remove -- +shift +# get action +action=$1 +shift + +# add daemons +for daemon; do + if ! have_daemon "$daemon"; then + printf "${C_FAIL}:: ${C_DONE}Dameon script ${C_FAIL}${daemon}${C_DONE} does \ +not exist or is not executable.${C_CLEAR}\n" >&2 + exit 2 + fi + daemons+=("$daemon") +done
Same here. Explicitly specified daemons should not be added to the list, but used as input to the filter options (if any). I would suggest following procedure:
Iterate over the daemons specified on the command line if any, "/etc/rc.d/*" otherwise:
* Set some flag to 1 (indicating whether the daemon will be included in the list or not).
* If "--started" or "--stopped" are specified (you stored this in some other flag earlier), possibly reset the flag to 0, depending on what ck_daemon() returns.
* Do the same thing with "--auto" or "--noauto" (and ck_autostart()).
* Add the daemon to some array if the flag is still set to 1.
This removes the need for special treatment in any of the action cases below, also.
What's append if you type rc.d stop with you algo? You stop all your daemon?
Yes, why not? There currently isn't any other simple way to stop all running daemons, so `rc.d stop` (or `rc.d stop --started`) should be perfectly fine. I don't see any reason to use a different logic here. Anyway, even if you don't want to support that, just add a simple oneliner that checks if no options and daemons are specified and the action is != "list". No need for using separate loops that can be refactored into a single location.