Unlike the rest of gsu, the functions of the gui module did not follow
the rule that functions should always set $ret to indicate error or
success, and $result to either the result of the function (if any)
or to an error context string. This commit makes all functions of
the gui module conform to this rule.
This patch also improves the error handling of _gsu_menu(). This
function should distinguish between three cases, according to the
exit code of dialog(1): OK (user selected the OK button), Cancel
(user pressed the escape key or selected the Cancel button), and error
(dialog(1) failed to execute or returned unsuccessfully).
The old code does not distinguish between the last two cases. Hence one
could not exit the application by pressing CTRL+C from a menu. This
patch changes _gsu_menu() to look more carefully at the exit code of
dialog(1) to tell apart the "error" case and the "cancel" case.
E_GSU_EDITOR failed to execute editor
E_GSU_MKDIR failed to create directory
E_GSU_GETOPTS getopts error
E_GSU_EDITOR failed to execute editor
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
"
local a b i=0
$gsu_errors
"
local a b i=0
_get_geometry
g="$result"
result="$(dialog --inputbox "$text" $g "$init" 3>&1 1>&2 2>&3 3>&-)"
_get_geometry
g="$result"
result="$(dialog --inputbox "$text" $g "$init" 3>&1 1>&2 2>&3 3>&-)"
+ if (($? != 0)); then
+ ret=-$E_GSU_DIALOG
+ result='inputbox'
+ return
+ fi
+ ret=$GSU_SUCCESS
- 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
}
# dialog segfaults if message is too long. Hence we always use a temporary file
local tmp="$(mktemp gsu_msgbox.XXXXXXXXXX)"
if (($? != 0)); then
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"
return
fi
echo "$1" > "$tmp"
gsu_textbox "$tmp"
+ rm -f "$tmp" # ignore errors
opts+=" $i $num"
done
result="$(dialog --menu "$gsu_banner_txt ($header)" $opts 3>&1 1>&2 2>&3 3>&-)"
opts+=" $i $num"
done
result="$(dialog --menu "$gsu_banner_txt ($header)" $opts 3>&1 1>&2 2>&3 3>&-)"
+ case $? in
+ 0) ret=$GSU_SUCCESS;;
+ 1) ret=1;; # cancelled
+ *)
+ result="menu error $ret"
+ ret=-$E_GSU_DIALOG
+ esac
local tree="$1" root="${2%/}"
local first TAB=' '
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%%:*}"
[[ -z "$first" ]] && return
line_num="${first%%:*}"
result="$(sed -e "1,${line_num}d;" <<< "$tree" \
| sed -e "/^$TAB\{1,$level\}$_gsu_node_name_pattern/,\$d" \
| sed -e "/^$TAB\{$(($level + 2))\}/d")"
result="$(sed -e "1,${line_num}d;" <<< "$tree" \
| sed -e "/^$TAB\{1,$level\}$_gsu_node_name_pattern/,\$d" \
| sed -e "/^$TAB\{$(($level + 2))\}/d")"
+ if (($? != 0)); then
+ ret=-$E_GSU_MENU_TREE
+ result="sed command for subtree $root failed"
+ return
+ fi
+ ret=$GSU_SUCCESS
local tree="$1" TAB=' '
result="$(grep "^${TAB}${_gsu_node_name_pattern}" <<< "$tree")"
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
while :; do
_gsu_menu "$header" "$subtree"
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"
if [[ "${result%/}" != "$result" ]]; then
old_header="$header"
header="$result"
_get_subtree "$tree" "$header"
_browse "$header" "$tree" "$result"
_browse "$header" "$tree" "$result"
header="$old_header"
continue
fi
header="$old_header"
continue
fi