From 71f6a3e256eaa6fcf85329d02ef6cb6d145a1939 Mon Sep 17 00:00:00 2001 From: fbt Date: Tue, 10 Feb 2015 16:45:15 +0300 Subject: [PATCH 1/5] Better argument handling; cleanup --- sup | 58 +++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 35 insertions(+), 23 deletions(-) diff --git a/sup b/sup index fe2cb5f..f57b1a9 100755 --- a/sup +++ b/sup @@ -34,7 +34,7 @@ sup.upload() { -F submit="" \ "$cfg_script_url" -sL -A 'zerofiles.org upload script' || { return 1; } - [[ "$flag_rm" ]] && { rm "$file"; } + (( "$flag_rm" )) && { rm "$file"; } return 0 } @@ -57,7 +57,7 @@ sup.scrot() { return 1 } - [[ "$flag_scrot_fullscreen" ]] || { scrot_args+=( '-s' ); } + (( "$flag_scrot_fullscreen" )) || { scrot_args+=( '-s' ); } [[ "$cfg_scrot_delay" ]] && { scrot_args+=( "-d $cfg_scrot_delay" ); } tmp_file="$(sup.mktemp "${cfg_tmp_dir}" ".${cfg_screenshot_ext}")" @@ -92,32 +92,43 @@ sup.mktemp() { echo "${tmp_dir}/${tmp_file_name}" } -sup.getopt() { - local keys +set_argv() { + declare arg opt c + declare -g argv - for i in "$@"; do - case "$i" in - --*) echo "$i";; + while (( $# )); do + unset -v arg opt c - -*) - keys=( $(echo "${i##*-}" | fold -w1) ) + case "$1" in + (--) argv+=( "$1" ); break;; - if [[ "${#keys[@]}" -gt 1 ]]; then - for k in "${keys[@]}"; do - echo "-${k}" - done - else - echo "$i" - fi + (--*) + IFS='=' read arg opt <<< "$1" + argv+=( "$arg" ) + + [[ "$opt" ]] && { + argv+=( "$opt" ) + } ;; - *) echo "$i";; + (-*) + while read -n1 c + do + case "$c" in + -|'') :;; + *) argv+=( "-$c" );; + esac + done <<< "$1" + ;; + + (*) argv+=( "$1" );; esac + shift done } main() { - local args file target flag_private flag_rm flag_scrot flag_scrot_fullscreen cfg_scrot_delay + declare args file target flag_private flag_rm flag_scrot flag_scrot_fullscreen cfg_scrot_delay while [[ "$1" ]]; do case "$1" in @@ -130,19 +141,19 @@ main() { -h|--help|--usage) sup.usage; return;; - --) break;; + --) shift; break;; - *) args+=( "$1" );; + *) break;; esac shift done sup.env - if [[ "$flag_scrot" ]]; then + if (( flag_scrot )); then file="$(sup.scrot)" || { return 1; } else - target="${args[0]}"; [[ "$target" ]] || { sup.usage; exit 1; } + target="$1"; [[ "$target" ]] || { sup.usage; exit 1; } if sup.if_url "$target"; then file=$(sup.mktemp "$cfg_tmp_dir") @@ -159,4 +170,5 @@ main() { sup.upload } -main $(sup.getopt "$@") +set_argv "$@" +main "${argv[@]}" From 30ff0776d60bb2b7b2045b17a0710545c13c85c1 Mon Sep 17 00:00:00 2001 From: fbt Date: Tue, 10 Feb 2015 16:50:10 +0300 Subject: [PATCH 2/5] $# --- sup | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/sup b/sup index cc3d350..f25b4c5 100755 --- a/sup +++ b/sup @@ -153,7 +153,12 @@ main() { if (( flag_scrot )); then file="$(sup.scrot)" || { return 1; } else - target="$1"; [[ "$target" ]] || { sup.usage; exit 1; } + (( $# )) || { + sup.usage + return 1 + } + + target="$1" if sup.if_url "$target"; then file=$(sup.mktemp "$cfg_tmp_dir") From 795078cb2b4bd750682d8e4ce376973a843c547c Mon Sep 17 00:00:00 2001 From: fbt Date: Tue, 10 Feb 2015 16:51:20 +0300 Subject: [PATCH 3/5] use declare instead of local --- sup | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sup b/sup index f25b4c5..cf9bf28 100755 --- a/sup +++ b/sup @@ -49,7 +49,7 @@ sup.mktemp() { } sup.scrot() { - local tmp_file + declare tmp_file [[ "$scrot_exec" ]] || { scrot_exec=$(type -P scrot); } [[ "$scrot_exec" ]] || { @@ -78,7 +78,7 @@ sup.exclude() { sup.if_url() { echo "$1" | grep -oE "$cfg_url_regex" &>/dev/null; } sup.mktemp() { - local tmp_file_name tmp_file_name_extra="$2" tmp_dir="$1" + declare tmp_file_name tmp_file_name_extra="$2" tmp_dir="$1" [[ -d "$tmp_dir" ]] || { sup.err "${tmp_dir} does not exist or is not a directory." From 85cf56afc5b794e8eefd5a8f23c5b6c8607437c1 Mon Sep 17 00:00:00 2001 From: fbt Date: Tue, 10 Feb 2015 16:54:55 +0300 Subject: [PATCH 4/5] Use printf instead of echo. exclude was unused. --- sup | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/sup b/sup index cf9bf28..06e3d95 100755 --- a/sup +++ b/sup @@ -13,11 +13,11 @@ cfg_screenshot_ext='png' [[ -f $HOME/.suprc ]] && { source "$HOME/.suprc"; } -sup.msg() { echo "[sup] $1"; } +sup.msg() { printf '%s\n' "[sup] $1"; } sup.err() { sup.msg "(error) $1" >&2; } sup.usage() { - echo "Usage: ${_self} [-RsF] [-D num] [file/url]" + printf '%s\n' "Usage: ${_self} [-RsF] [-D num] [file/url]" } sup.env() { @@ -45,7 +45,7 @@ sup.mktemp() { return 1 } - echo "$1" + printf '%s\n' "$1" } sup.scrot() { @@ -67,15 +67,10 @@ sup.scrot() { return 1 } - echo "$tmp_file" + printf '%s\n' "$tmp_file" } -sup.exclude() { - [[ "$2" ]] && { echo "$1"; return 1; } - return 0 -} - -sup.if_url() { echo "$1" | grep -oE "$cfg_url_regex" &>/dev/null; } +sup.if_url() { printf '%s\n' "$1" | grep -oE "$cfg_url_regex" &>/dev/null; } sup.mktemp() { declare tmp_file_name tmp_file_name_extra="$2" tmp_dir="$1" @@ -89,7 +84,7 @@ sup.mktemp() { tmp_file_name="${RANDOM}${RANDOM}${tmp_file_name_extra}" done - echo "${tmp_dir}/${tmp_file_name}" + printf '%s\n' "${tmp_dir}/${tmp_file_name}" } set_argv() { From f940ec690596ae3ef08917b2bc313c80b65d9530 Mon Sep 17 00:00:00 2001 From: fbt Date: Tue, 10 Feb 2015 16:58:36 +0300 Subject: [PATCH 5/5] Redundant quoting --- sup | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sup b/sup index 06e3d95..e428b87 100755 --- a/sup +++ b/sup @@ -146,7 +146,7 @@ main() { sup.env if (( flag_scrot )); then - file="$(sup.scrot)" || { return 1; } + file=$(sup.scrot) || { return 1; } else (( $# )) || { sup.usage