gui: Conform to $ret and $result conventions.
[gsu.git] / gui
diff --git a/gui b/gui
index 336d154..3c89567 100644 (file)
--- a/gui
+++ b/gui
@@ -29,15 +29,25 @@ gsu_inputbox()
        _get_geometry
        g="$result"
        result="$(dialog --inputbox "$text" $g "$init" 3>&1 1>&2 2>&3 3>&-)"
-       ret="$?"
+       if (($? != 0)); then
+               ret=-$E_GSU_DIALOG
+               result='inputbox'
+               return
+       fi
+       ret=$GSU_SUCCESS
 }
 
 gsu_textbox()
 {
-       local file="$1"
+       local file="$1"
 
        _get_geometry
-       dialog --textbox "$file" $result
+       g="$result"
+
+       ret=-$E_GSU_DIALOG
+       result='textbox'
+       dialog --textbox "$file" $g || return
+       ret=$GSU_SUCCESS
 }
 
 # dialog segfaults if message is too long. Hence we always use a temporary file
@@ -46,12 +56,13 @@ gsu_msgbox()
        local tmp="$(mktemp gsu_msgbox.XXXXXXXXXX)"
 
        if (($? != 0)); then
-               dialog --msgbox "mktemp error" 0 0
+               ret=-$E_GSU_MKTEMP
+               result='temp file for textbox'
                return
        fi
        echo "$1" > "$tmp"
        gsu_textbox "$tmp"
-       rm -f "$tmp"
+       rm -f "$tmp" # ignore errors
 }
 
 _gsu_menu()
@@ -67,7 +78,13 @@ _gsu_menu()
                opts+=" $i $num"
        done
        result="$(dialog --menu "$gsu_banner_txt ($header)" $opts 3>&1 1>&2 2>&3 3>&-)"
-       ret="$?"
+       case $? in
+       0) ret=$GSU_SUCCESS;;
+       1) ret=1;; # cancelled
+       *)
+               result="menu error $ret"
+               ret=-$E_GSU_DIALOG
+       esac
 }
 
 _get_level()
@@ -81,7 +98,9 @@ _get_subtree()
        local tree="$1" root="${2%/}"
        local first TAB='       '
 
-       first="$(grep -n "$TAB\{1,\}$root/" <<< "$tree")"
+       ret=-$E_GSU_MENU_TREE
+       result="subtree grep failed"
+       first="$(grep -n "$TAB\{1,\}$root/" <<< "$tree")" || return
        [[ -z "$first" ]] && return
 
        line_num="${first%%:*}"
@@ -92,7 +111,12 @@ _get_subtree()
        result="$(sed -e "1,${line_num}d;" <<< "$tree" \
                | sed -e "/^$TAB\{1,$level\}$_gsu_node_name_pattern/,\$d" \
                | sed -e "/^$TAB\{$(($level + 2))\}/d")"
-       ret="$level"
+       if (($? != 0)); then
+               ret=-$E_GSU_MENU_TREE
+               result="sed command for subtree $root failed"
+               return
+       fi
+       ret=$GSU_SUCCESS
 }
 
 _get_root_nodes()
@@ -100,6 +124,12 @@ _get_root_nodes()
        local tree="$1" TAB='   '
 
        result="$(grep "^${TAB}${_gsu_node_name_pattern}" <<< "$tree")"
+       if (($? != 0)); then
+               ret=-$E_GSU_MENU_TREE
+               result="root node grep failed"
+               return
+       fi
+       ret=$GSU_SUCCESS
 }
 
 _browse()
@@ -109,13 +139,15 @@ _browse()
 
        while :; do
                _gsu_menu "$header" "$subtree"
-               (($ret != 0)) && return
-               [[ -z "$result" ]] && return
+               (($ret < 0)) && return
+               [[ -z "$result" ]] && return # menu was cancelled
                if [[ "${result%/}" != "$result" ]]; then
                        old_header="$header"
                        header="$result"
                        _get_subtree "$tree" "$header"
+                       (($ret < 0)) && return
                        _browse "$header" "$tree" "$result"
+                       (($ret < 0)) && return
                        header="$old_header"
                        continue
                fi