[PATCH 06/10] hal: Clean up scripts in tools

Victor Lowther victor.lowther at gmail.com
Tue Mar 25 18:48:52 PDT 2008


That last commit did now work out like I imagined.
Generalize env testing a little more, and modify the other scripts
to use the new nev testing code.
---
 tools/hal-functions                   |   22 ++++++++++++++++------
 tools/hal-luks-remove                 |    8 ++------
 tools/hal-luks-teardown               |   12 ++----------
 tools/hal-system-killswitch-get-power |    7 +------
 tools/hal-system-killswitch-set-power |    7 +------
 tools/hal-system-lcd-get-brightness   |    8 ++------
 tools/hal-system-lcd-set-brightness   |    9 ++-------
 tools/hal-system-wol-enable           |    7 +------
 tools/hal-system-wol-enabled          |    7 +------
 tools/hal-system-wol-supported        |    7 +------
 10 files changed, 29 insertions(+), 65 deletions(-)

diff --git a/tools/hal-functions b/tools/hal-functions
index b4a1ef9..7742c4b 100755
--- a/tools/hal-functions
+++ b/tools/hal-functions
@@ -23,20 +23,30 @@ hal_check_priv() {
     fi
 }
 
+is_set() { [ -n "$1" ]; }
+
+all_set() {
+	for x in "$@"; do is_set "$x" || return 1; done
+}
+
+any_set() {
+	for x in "$@"; do is_set "$x" && return 0; done
+	return 1
+}
+
 # Check for environment variables.  Seems like every hal script does this.
-hal_check_env() {
-	# $1 = value of env var.
+env_die () {
+	# $1 = error message.
 	# $2 (optional) = name of env var.
-	[ -n "$1" ] && return
-        echo "org.freedesktop.Hal.Device.UnknownError" >&2
+        echo "org.freedesktop.Hal.Device.$1" >&2
 	if [ -z "$2" ]; then
         	echo "Missing or empty environment variable(s)." >&2
 	else
 		echo "Missing or empty environment variable $2" >&2
 	fi
         echo "This script should be started by hald." >&2
-        return 1
-fi
+        exit 1
+}
 
 hal_call_backend() {
     local PROGRAM
diff --git a/tools/hal-luks-remove b/tools/hal-luks-remove
index 62324a6..0b8f90c 100755
--- a/tools/hal-luks-remove
+++ b/tools/hal-luks-remove
@@ -6,11 +6,7 @@
 # This program is free software; you can redistribute it and/or modify
 # it under the terms of the GNU General Public License version 2.
 
-if [ -z "$HAL_PROP_BLOCK_DEVICE" ] || [ -z "$HAL_PROP_INFO_UDI" ] || [ -z "$HAL_PROP_VOLUME_UUID" ] ; then
-        echo "org.freedesktop.Hal.Device.UnknownError" >&2
-        echo "Missing or empty environment variable(s)." >&2
-        echo "This script should be started by hald." >&2
-        exit 1
-fi
+all_set "$HAL_PROP_BLOCK_DEVICE" "$HAL_PROP_INFO_UDI" 	 \
+	"$HAL_PROP_VOLUME_UUID" || env_die "UnknownError"
 
 hal_exec_backend
diff --git a/tools/hal-luks-teardown b/tools/hal-luks-teardown
index 4158a11..050c7d8 100755
--- a/tools/hal-luks-teardown
+++ b/tools/hal-luks-teardown
@@ -16,17 +16,9 @@ locked_out() {
 	exit 1
 }
 
-unknown_error() {
-        echo "org.freedesktop.Hal.Device.UnknownError" >&2
-        echo "Missing or empty environment variable(s)." >&2
-        echo "This script should be started by hald." >&2
-        exit 1
-}
-
 # Check for environment variables
-if [ -z "$HAL_PROP_BLOCK_DEVICE" ] || [ -z "$HAL_PROP_INFO_UDI" ] || [ -z "$HAL_PROP_VOLUME_UUID" ] ; then
-    unknown_error
-fi
+all_set "$HAL_PROP_BLOCK_DEVICE" "$HAL_PROP_INFO_UDI" \
+	"$HAL_PROP_VOLUME_UUID" || env_die "UnknownError"
 
 # Respect the same locks as Mount()/Unmount() etc.
 if [ -n "$HAL_METHOD_INVOKED_BY_SYSTEMBUS_CONNECTION_NAME" ] ; then
diff --git a/tools/hal-system-killswitch-get-power b/tools/hal-system-killswitch-get-power
index 7f99a8f..19294bd 100755
--- a/tools/hal-system-killswitch-get-power
+++ b/tools/hal-system-killswitch-get-power
@@ -10,12 +10,7 @@
 . hal-functions
 
 # Check for environment variables
-if [ -z "$HAL_PROP_KILLSWITCH_ACCESS_METHOD" ]; then
-        echo "org.freedesktop.Hal.Device.UnknownError" >&2
-	echo "Missing or empty environment variable(s)." >&2
-	echo "This script should be started by hald." >&2
-	exit 1
-fi
+is_set "$HAL_PROP_KILLSWITCH_ACCESS_METHOD" || env_die "UnknownError"
 
 hal_check_priv org.freedesktop.hal.killswitch.$HAL_PROP_KILLSWITCH_TYPE
 hal_exec_backend
diff --git a/tools/hal-system-killswitch-set-power b/tools/hal-system-killswitch-set-power
index 33d8c65..f967b29 100755
--- a/tools/hal-system-killswitch-set-power
+++ b/tools/hal-system-killswitch-set-power
@@ -10,12 +10,7 @@
 . hal-functions
 
 # Check for environment variables
-if [ -z "$HAL_PROP_KILLSWITCH_ACCESS_METHOD" ] ; then
-        echo "org.freedesktop.Hal.Device.UnknownError" >&2
-        echo "Missing or empty environment variable(s)." >&2
-        echo "This script should be started by hald." >&2
-        exit 1
-fi
+is_set "$HAL_PROP_KILLSWITCH_ACCESS_METHOD" || env_die "UnknownError"
 
 # read value for setting power
 read value
diff --git a/tools/hal-system-lcd-get-brightness b/tools/hal-system-lcd-get-brightness
index 76dbd35..c3d0334 100755
--- a/tools/hal-system-lcd-get-brightness
+++ b/tools/hal-system-lcd-get-brightness
@@ -10,12 +10,8 @@
 . hal-functions
 
 # Check for environment variables
-if [ -z "$HAL_PROP_LAPTOP_PANEL_ACCESS_METHOD" -a -z "$HAL_PROP_LINUX_SYSFS_PATH" ]; then
-        echo "org.freedesktop.Hal.Device.UnknownError" >&2
-	echo "Missing or empty environment variable(s)." >&2
-	echo "This script should be started by hald." >&2
-	exit 1
-fi
+any_set "$HAL_PROP_LAPTOP_PANEL_ACCESS_METHOD" "$HAL_PROP_LINUX_SYSFS_PATH" || \
+	env_die "UnknownError"
 
 hal_check_priv org.freedesktop.hal.power-management.lcd-panel
 hal_exec_backend
diff --git a/tools/hal-system-lcd-set-brightness b/tools/hal-system-lcd-set-brightness
index aa7ceb4..206930b 100755
--- a/tools/hal-system-lcd-set-brightness
+++ b/tools/hal-system-lcd-set-brightness
@@ -10,13 +10,8 @@
 . hal-functions
 
 # Check for environment variables
-if [ -z "$HAL_PROP_LAPTOP_PANEL_ACCESS_METHOD" -a -z "$HAL_PROP_LINUX_SYSFS_PATH" ] || 
-   [ -z "$HAL_PROP_LAPTOP_PANEL_NUM_LEVELS" ] ; then
-        echo "org.freedesktop.Hal.Device.UnknownError" >&2
-        echo "Missing or empty environment variable(s)." >&2
-        echo "This script should be started by hald." >&2
-        exit 1
-fi
+any_set "$HAL_PROP_LAPTOP_PANEL_ACCESS_METHOD" "$HAL_PROP_LINUX_SYSFS_PATH" && \
+	all_set "$HAL_PROP_LAPTOP_PANEL_NUM_LEVELS" || env_die "UnknownError"
 
 # read value for set brightness
 read value
diff --git a/tools/hal-system-wol-enable b/tools/hal-system-wol-enable
index b4de461..274db5b 100755
--- a/tools/hal-system-wol-enable
+++ b/tools/hal-system-wol-enable
@@ -9,12 +9,7 @@
 
 . hal-functions
 
-if [ -z "$UDI" -o -z "$HAL_PROP_NET_INTERFACE" ]; then
-    echo "org.freedesktop.Hal.Device.WakeOnLAN.UnknownError" >&2
-    echo "Missing or empty environment variable(s)." >&2
-    echo "This script should be started by hald." >&2
-    exit 1
-fi
+all_set "$UDI" "$HAL_PROP_NET_INTERFACE" || env_die "WakeOnLan.UnknownError"
 
 # read value for setting enabled or disabled
 read enable
diff --git a/tools/hal-system-wol-enabled b/tools/hal-system-wol-enabled
index 4901bc2..691dbed 100755
--- a/tools/hal-system-wol-enabled
+++ b/tools/hal-system-wol-enabled
@@ -9,12 +9,7 @@
 
 . hal-functions
 
-if [ -z "$UDI" -o -z "$HAL_PROP_NET_INTERFACE" ]; then
-    echo "org.freedesktop.Hal.Device.WakeOnLAN.UnknownError" >&2
-    echo "Missing or empty environment variable(s)." >&2
-    echo "This script should be started by hald." >&2
-    exit 1
-fi
+all_set "$UDI" "$HAL_PROP_NET_INTERFACE" || env_die "WakeOnLAN.UnknownError"
 
 hal_check_priv org.freedesktop.hal.wol.enabled
 hal_exec_backend
diff --git a/tools/hal-system-wol-supported b/tools/hal-system-wol-supported
index 4879c3b..4926b78 100755
--- a/tools/hal-system-wol-supported
+++ b/tools/hal-system-wol-supported
@@ -9,12 +9,7 @@
 
 . hal-functions
 
-if [ -z "$UDI" -o -z "$HAL_PROP_NET_INTERFACE" ]; then
-    echo "org.freedesktop.Hal.Device.WakeOnLAN.UnknownError" >&2
-    echo "Missing or empty environment variable(s)." >&2
-    echo "This script should be started by hald." >&2
-    exit 1
-fi
+all_set "$UDI" "$HAL_PROP_NET_INTERFACE" || env_die "WakeOnLAN.UnknownError"
 
 hal_check_priv org.freedesktop.hal.wol.supported
 hal_exec_backend
-- 
1.5.4.3



More information about the hal mailing list