From: Andre Noll Date: Mon, 1 Sep 2014 22:23:41 +0000 (+0200) Subject: gui: Conform to $ret and $result conventions. X-Git-Url: http://git.tuebingen.mpg.de/?p=gsu.git;a=commitdiff_plain;h=2925cf7299f958ed6c52ba1ba9d42a841d86c8f2;ds=sidebyside gui: Conform to $ret and $result conventions. 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. --- diff --git a/common b/common index eaa5751..cd07252 100644 --- a/common +++ b/common @@ -15,6 +15,9 @@ 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 +E_GSU_DIALOG dialog error +E_GSU_MKTEMP mktemp error +E_GSU_MENU_TREE invalid menu tree $gsu_errors " local a b i=0 diff --git a/gui b/gui index 336d154..3c89567 100644 --- 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 g 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