subcommand: Make help text variables private.
[gsu.git] / common
diff --git a/common b/common
index bd8276b2a96954eef6ef05dfe4cbad5ccf2c1a62..ec7b8e2cfd816f1faadc6b2ffd8c45c2da37fc28 100644 (file)
--- a/common
+++ b/common
@@ -1,5 +1,6 @@
 #!/bin/bash
-# (C) 2006-2011 Andre Noll
+# Copyright (C) 2006 Andre Noll
+# Licensed under the LGPL, version 3. See COPYING and COPYING.LESSER.
 
 _gsu_init_errors()
 {
@@ -15,7 +16,10 @@ 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
+E_GSU_DIALOG                   dialog error
+E_GSU_MKTEMP                   mktemp error
+E_GSU_MENU_TREE                        invalid menu tree
+${gsu_errors:-}
 "
        local a b i=0
        while read a b; do
@@ -35,7 +39,7 @@ EOF
 gsu_is_a_number()
 {
        result="$1"
-       if test "$1" -eq "$1" &> /dev/null; then
+       if [ "$1" -eq "$1" ] &> /dev/null; then
                ret=$GSU_SUCCESS
        else
                ret=-$E_GSU_NOT_A_NUMBER
@@ -49,12 +53,12 @@ gsu_short_msg()
 
 gsu_msg()
 {
-       gsu_short_msg "$_gsu_self: $1"
+       gsu_short_msg "$gsu_name: $1"
 }
 
 gsu_date_msg()
 {
-       gsu_short_msg "$_gsu_self $(date): $1"
+       gsu_short_msg "$gsu_name $(date): $1"
 }
 
 gsu_err_msg()
@@ -62,11 +66,11 @@ gsu_err_msg()
        local txt="$result" err
 
        gsu_is_a_number "$ret"
-       if test $ret -lt 0; then
+       if (($ret < 0)); then
                gsu_msg "unknown error ($ret:$txt)"
                exit 1
        fi
-       if test $result -ge 0; then
+       if (($result >= 0)); then
                gsu_msg "unknown error ($result:$txt)"
                exit 1
        fi
@@ -81,9 +85,40 @@ gsu_err_msg()
 
 _gsu_setup()
 {
-       _gsu_self="$(basename $0)"
-       gsu_name="${gsu_name:=$_gsu_self}"
+       gsu_name="${gsu_name:-${0##*/}}"
        gsu_config_var_prefix="${gsu_config_var_prefix:=$gsu_name}"
        gsu_banner_txt="${gsu_banner_txt:-set \$gsu_banner_txt to customize this message}"
        _gsu_init_errors
 }
+
+# We'd love to use mktemp -t here, but on Linux -t is deprecated in favor of
+# --tempdir, which is not supported on *BSD. Hence we have to implement our own
+# logic for -t.
+#
+# The second parameter to this function is optional. It is ignored if the
+# template ($1) is an absolute path. Conversely, if the template is a relative
+# path and a second parameter is given, $2 is assumed to be the directory in
+# which the temporary file should be created.
+gsu_make_tempfile()
+{
+       local template="$1"
+       local dir
+
+       if [[ "${template:0:1}" != '/' ]]; then # relative path
+               if (($# > 1)); then
+                       dir="$2"
+               elif [[ -n "${TMPDIR:-}" ]]; then
+                       dir="$TMPDIR"
+               else
+                       dir="/tmp"
+               fi
+               template="$dir/$template"
+       fi
+       result="$(mktemp "$template")"
+       if (($? != 0)); then
+               ret=-$E_GSU_MKTEMP
+               result="template: $template"
+               return
+       fi
+       ret=$GSU_SUCCESS
+}