summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@sbcglobal.net>2016-06-08 22:30:19 -0400
committerLuke Shumaker <lukeshu@sbcglobal.net>2016-06-08 23:03:05 -0400
commit8c67cad1f2bbcaabe23586643d670ba08f3d2a05 (patch)
tree812d0cba8c0d157d8c22b67093c8f6c8a3bcd099
parent482ff021e313131af25eb6c8d70bd4d414c32105 (diff)
Clean up. Mostly have error handling be a mode.
You probably want to ignore whitespace change when viewing this diff.
-rw-r--r--common.sh.in22
-rw-r--r--ediff.sh.in66
-rw-r--r--emacsmail.sh.in58
-rw-r--r--emacsterm.sh.in48
4 files changed, 94 insertions, 100 deletions
diff --git a/common.sh.in b/common.sh.in
index 614f462..b2b4768 100644
--- a/common.sh.in
+++ b/common.sh.in
@@ -1,6 +1,6 @@
# begin common.sh {{{
-# Copyright (C) 2013-2014 Luke Shumaker <lukeshu@sbcglobal.net>
+# Copyright (C) 2013-2014, 2016 Luke Shumaker <lukeshu@sbcglobal.net>
#
# This file is not considered part of GNU Emacs.
#
@@ -17,6 +17,9 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
+unset IFS
+IFS=$' \t\n'
+
if type gettext &>/dev/null; then
_() { gettext "$@"; }
else
@@ -48,10 +51,6 @@ emacs_quote() {
printf -- '"%s" ' "${args[@]}" # wrap them in quotes, return
}
-bash_quote() {
- printf -- '%q ' "$@"
-}
-
version() {
print '%s (Emacs utils) %s, %s' \
"${0##*/}" @VERSION@ "$(emacsclient --version)"
@@ -106,4 +105,17 @@ emacs_usage() {
emacsclient --help | grep -E '^(\s|-)'
}
+
+next() {
+ local mode=$1
+ shift
+ case "$mode" in
+ error) print "Try \``%q --help'' for more information" "$0" >&2; return 1;;
+ usage) usage; return 0;;
+ version) version; return 0;;
+ normal) exec -- "$@";;
+ *) error 'Internal error. The programmer writing this tool screwed up.'; exit 1;;
+ esac
+}
+
# }}} end common.sh
diff --git a/ediff.sh.in b/ediff.sh.in
index 164e6da..569721e 100644
--- a/ediff.sh.in
+++ b/ediff.sh.in
@@ -1,6 +1,6 @@
#!@bash@
-# Copyright (C) 2014 Luke Shumaker <lukeshu@sbcglobal.net>
+# Copyright (C) 2014, 2016 Luke Shumaker <lukeshu@sbcglobal.net>
#
# This file is not considered part of GNU Emacs.
#
@@ -21,7 +21,7 @@ m4_include(common.sh.in)
usage() {
print 'Usage: %q [OPTIONS] FILE_A FILE_B' "$0"
- print 'Usage: %q -3 [OPTIONS] FILE_A FILE_B FILE_C' "$0"
+ print ' or: %q -3 [OPTIONS] FILE_A FILE_B FILE_C' "$0"
print "Use Emacs' ediff-mode to compare two files"
echo
print 'The following OPTIONS are accepted:'
@@ -34,46 +34,40 @@ main() {
declare -a flags=()
declare -a files=()
declare -i cnt=2
- declare error=false
- declare mode=normal
declare cmd=ediff
+ declare mode=normal
emacs_getopt_init
declare args
- args="$(emacs_getopt 3r recursive "$@")" || error=true
- eval set -- "$args"
- while true; do
- case "$1" in
- -V|--version) shift; mode=version;;
- -H|--help) shift; mode=usage;;
- -r|--recursive) shift; cmd=edirs;;
- -3) shift; cnt=3;;
- --) shift; break;;
- *)
- if [[ $1 =~ $emacs_getopt_2 ]]; then
- flags+=("$1" "$2"); shift 2
- else
- flags+=("$1"); shift 1
- fi
- ;;
- esac
- done
- files=("$@")
- if [[ $mode == normal ]]; then
- [[ ${#files[@]} = ${cnt} ]] || error=true
- fi
-
- if $error; then
- usage >&2
- return 1
+ if ! args="$(emacs_getopt 3r recursive "$@")"; then
+ mode=error
+ else
+ eval set -- "$args"
+ while true; do
+ case "$1" in
+ -V|--version) shift; mode=version;;
+ -H|--help) shift; mode=usage;;
+ -r|--recursive) shift; cmd=edirs;;
+ -3) shift; cnt=3;;
+ --) shift; break;;
+ *)
+ if [[ $1 =~ $emacs_getopt_2 ]]; then
+ flags+=("$1" "$2"); shift 2
+ else
+ flags+=("$1"); shift 1
+ fi
+ ;;
+ esac
+ done
+ files=("$@")
+ if [[ $mode == normal ]]; then
+ [[ ${#files[@]} = ${cnt} ]] || mode=error
+ fi
fi
- case "$mode" in
- usage) usage; return 0;;
- version) version; return 0;;
- esac
- emacsclient "${flags[@]}" --eval \
- "(${cmd}${cnt#2} $(emacs_quote "${files[@]}"))"
+ next "$mode" \
+ emacsclient "${flags[@]}" --eval \
+ "(${cmd}${cnt#2} $(emacs_quote "${files[@]}"))"
}
main "$@"
diff --git a/emacsmail.sh.in b/emacsmail.sh.in
index f3b1fce..f1a3e1c 100644
--- a/emacsmail.sh.in
+++ b/emacsmail.sh.in
@@ -1,6 +1,6 @@
#!@bash@
-# Copyright (C) 2014 Luke Shumaker <lukeshu@sbcglobal.net>
+# Copyright (C) 2014, 2016 Luke Shumaker <lukeshu@sbcglobal.net>
#
# This file is not considered part of GNU Emacs.
#
@@ -32,43 +32,37 @@ usage() {
main() {
declare -a flags=()
- declare error=false
declare mode=normal
emacs_getopt_init
declare args
- args="$(emacs_getopt '' '' "$@")" || error=true
- eval set -- "$args"
- while true; do
- case "$1" in
- -V|--version) shift; mode=version;;
- -H|--help) shift; mode=usage;;
- --) shift; break;;
- *)
- if [[ $1 =~ $emacs_getopt_2 ]]; then
- flags+=("$1" "$2"); shift 2
- else
- flags+=("$1"); shift 1
- fi
- ;;
- esac
- done
- urls=("$@")
- if [[ $mode == normal ]]; then
- [[ ${#urls[@]} = 1 ]] || error=true
+ if ! args="$(emacs_getopt '' '' "$@")"; then
+ mode=error
+ else
+ eval set -- "$args"
+ while true; do
+ case "$1" in
+ -V|--version) shift; mode=version;;
+ -H|--help) shift; mode=usage;;
+ --) shift; break;;
+ *)
+ if [[ $1 =~ $emacs_getopt_2 ]]; then
+ flags+=("$1" "$2"); shift 2
+ else
+ flags+=("$1"); shift 1
+ fi
+ ;;
+ esac
+ done
+ urls=("$@")
+ if [[ $mode == normal ]]; then
+ [[ ${#urls[@]} = 1 ]] || mode=error
+ fi
fi
- if $error; then
- usage >&2
- return 1
- fi
- case "$mode" in
- usage) usage; return 0;;
- version) version; return 0;;
- esac
-
- emacsclient "${flags[@]}" --eval \
- "(browse-url-mail $(emacs_quote "${urls[@]}"))"
+ next "$mode" \
+ emacsclient "${flags[@]}" --eval \
+ "(browse-url-mail $(emacs_quote "${urls[@]}"))"
}
main "$@"
diff --git a/emacsterm.sh.in b/emacsterm.sh.in
index 3340bea..6ecbed3 100644
--- a/emacsterm.sh.in
+++ b/emacsterm.sh.in
@@ -1,6 +1,6 @@
#!@bash@
-# Copyright (C) 2011, 2013-2014 Luke Shumaker <lukeshu@sbcglobal.net>
+# Copyright (C) 2011, 2013-2014, 2016 Luke Shumaker <lukeshu@sbcglobal.net>
#
# This file is not considered part of GNU Emacs.
#
@@ -33,13 +33,15 @@ usage() {
main() {
+ declare mode
declare mimic
declare cmd
declare -a flags
parse "$@"
- emacsclient "${flags[@]}" --eval \
- "(term $(emacs_quote "${cmd}"))"
+ next \
+ emacsclient "${flags[@]}" --eval \
+ "(term $(emacs_quote "${cmd}"))"
}
# Sets the 'global' variables:
@@ -48,15 +50,14 @@ main() {
# - cmd
# - flags (array)
parse() {
+ mode=normal
mimic=rxvt
cmd=
flags=()
-
- local mode=normal
- local error=false
+
emacs_getopt_init
- while [[ $# -gt 0 ]]; do
+ while [[ $# -gt 0 ]] && [ "$mode" != error ]; do
case "$1" in
-V|--version) shift; mode=version;;
-H|--help) shift; mode=usage;;
@@ -69,25 +70,18 @@ parse() {
esac
done
- if $error; then
- usage >&2
- exit 1
- fi
- case "$mode" in
- usage) usage; exit 0;;
- version) version; exit 0;;
- esac
-
- if [[ -z "$cmd" ]]; then
- # This matches rxvt's behavior.
- # This is simpler than xterm's behavior.
- cmd="${cmd:-${SHELL:-/bin/sh}}"
- fi
+ # This matches rxvt's behavior.
+ # This is simpler than xterm's behavior.
+ cmd="${cmd:-${SHELL:-/bin/sh}}"
}
# Return status is the number of things to shift
parse_emacs_getopt() {
declare -a flag
+ # Because we don't want options to get combined, we run getopt
+ # with : stripped, so that we can check that it returned 2
+ # arguments: the flag itself, and '--'. If there were
+ # combined flags, then it will return more than 2 arguments.
if eval "flag=($(getopt -a \
-n "$0" \
-o "${emacs_getopt_o//:/}" \
@@ -103,12 +97,12 @@ parse_emacs_getopt() {
return 2
else
# Missing the required 2nd part
- getopt -a \
+ getopt -Q -a \
-n "$0" \
-o "$emacs_getopt_o" \
-l "$emacs_getopt_l" \
- -- "$flag" >/dev/null
- error=true
+ -- "$flag"
+ mode=error
return 1
fi
else
@@ -119,8 +113,8 @@ parse_emacs_getopt() {
else
# getopt either didn't work, or did combined flags
# Have getopt display its own error message:
- getopt -a -n "$0" -o '' -l '' -- "$1" >/dev/null
- error=true
+ getopt -Q -a -n "$0" -o '' -l '' -- "$1"
+ mode=error
return 1
fi
}
@@ -153,7 +147,7 @@ parse_shell() {
else
shift
error "extra arguments: %s" "$*"
- error=true
+ mode=error
fi
}