X-Git-Url: http://git.tuebingen.mpg.de/?p=gsu.git;a=blobdiff_plain;f=common;h=374b81e0498defc0d6895d3617fe3970f06a3cd3;hp=fe64cd6b61f690d6ad06da0b2cae747e2ba1f308;hb=6d2ca9b4375fb6103a49b64f9b4feee72dd44843;hpb=77fd8c08604bb7a1c641c4f36bdf4f839d114656 diff --git a/common b/common index fe64cd6..374b81e 100644 --- a/common +++ b/common @@ -14,12 +14,14 @@ 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_INVAL invalid argument E_GSU_MKDIR failed to create directory E_GSU_GETOPTS getopts error E_GSU_DIALOG dialog error E_GSU_MKTEMP mktemp error E_GSU_MENU_TREE invalid menu tree -$gsu_errors +E_GSU_XCMD external command failed +${gsu_errors:-} " local a b i=0 while read a b; do @@ -29,7 +31,7 @@ $gsu_errors #echo "a:$a, b: $b" gsu_error_txt[i]="$b" eval $a=$i - i=$(($i + 1)) + i=$((i + 1)) done << EOF $gsu_errors EOF @@ -39,7 +41,7 @@ EOF gsu_is_a_number() { result="$1" - if (("$1" == "$1")) &> /dev/null; then + if [ "$1" -eq "$1" ] &> /dev/null; then ret=$GSU_SUCCESS else ret=-$E_GSU_NOT_A_NUMBER @@ -66,15 +68,15 @@ gsu_err_msg() local txt="$result" err gsu_is_a_number "$ret" - if (($ret < 0)); then + if ((ret < 0)); then gsu_msg "unknown error ($ret:$txt)" exit 1 fi - if (($result >= 0)); then + if ((result >= 0)); then gsu_msg "unknown error ($result:$txt)" exit 1 fi - err=$((0 - $result)) + err=$((0 - result)) if test -n "$txt"; then txt="$txt: ${gsu_error_txt[$err]}" else @@ -90,3 +92,35 @@ _gsu_setup() 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 +}