]> git.tuebingen.mpg.de Git - gsu.git/blobdiff - misc/gsu/common
Split gsu.
[gsu.git] / misc / gsu / common
diff --git a/misc/gsu/common b/misc/gsu/common
new file mode 100644 (file)
index 0000000..63b2ab6
--- /dev/null
@@ -0,0 +1,95 @@
+#!/bin/bash
+# (C) 2006-2011 Andre Noll
+
+_gsu_init_errors()
+{
+       gsu_errors="
+GSU_SUCCESS                    success
+E_GSU_BAD_COMMAND              invalid command
+E_GSU_NOT_A_NUMBER             not a number
+E_GSU_BAD_CONFIG_VAR           invalid config variable
+E_GSU_NEED_VALUE               value required but not given
+E_GSU_BAD_BOOL                 bad value for boolian option
+E_GSU_BAD_OPTION_TYPE          invalid option type
+E_GSU_BAD_ARG_COUNT            invalid number of arguments
+E_GSU_EDITOR                   failed to execute editor
+E_GSU_MKDIR                    failed to create directory
+E_GSU_GETOPTS                  getopts error
+$gsu_errors
+"
+       local a b i=0
+       while read a b; do
+               if test -z "$a"; then
+                       continue
+               fi
+               #echo "a:$a,  b: $b"
+               gsu_error_txt[i]="$b"
+               eval $a=$i
+               i=$(($i + 1))
+       done << EOF
+       $gsu_errors
+EOF
+}
+export -f _gsu_init_errors
+
+# check if $1 is a number
+gsu_is_a_number()
+{
+       result="$1"
+       if test "$1" -eq "$1" &> /dev/null; then
+               ret=$GSU_SUCCESS
+       else
+               ret=-$E_GSU_NOT_A_NUMBER
+       fi
+}
+export -f gsu_is_a_number
+
+gsu_short_msg()
+{
+       echo "$1" 1>&2
+}
+export -f gsu_short_msg
+
+gsu_msg()
+{
+       gsu_short_msg "$_gsu_self: $1"
+}
+export -f gsu_msg
+
+gsu_date_msg()
+{
+       gsu_short_msg "$_gsu_self $(date): $1"
+}
+export -f gsu_date_msg
+
+gsu_err_msg()
+{
+       local txt="$result" err
+
+       gsu_is_a_number "$ret"
+       if test $ret -lt 0; then
+               gsu_msg "unknown error ($ret:$txt)"
+               exit 1
+       fi
+       if test $result -ge 0; then
+               gsu_msg "unknown error ($result:$txt)"
+               exit 1
+       fi
+       err=$((0 - $result))
+       if test -n "$txt"; then
+               txt="$txt: ${gsu_error_txt[$err]}"
+       else
+               txt="${gsu_error_txt[$err]}"
+       fi
+       gsu_msg "$txt"
+}
+export -f gsu_err_msg
+
+_gsu_setup()
+{
+       _gsu_self="$(basename $0)"
+       gsu_name="${gsu_name:=$_gsu_self}"
+       gsu_config_var_prefix="${gsu_config_var_prefix:=$gsu_name}"
+       _gsu_init_errors
+}
+export -f _gsu_setup