summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBo Ørsted Andresen <bo.andresen@zlin.dk>2007-03-08 11:30:39 +0000
committerBo Ørsted Andresen <bo.andresen@zlin.dk>2007-03-08 11:30:39 +0000
commit119b3f1c01e454a4812b1b1719e301cf79ddb6d4 (patch)
tree4dff962ec71abb82dc260d7b05f35960abc16108
parent02c7415424ce442c97b7f7b3105fa435f4382820 (diff)
downloadwgetpaste-119b3f1c01e454a4812b1b1719e301cf79ddb6d4.tar.gz
Merge optimizations (for size) from the getopts branch.
-rwxr-xr-xwgetpaste119
1 files changed, 50 insertions, 69 deletions
diff --git a/wgetpaste b/wgetpaste
index cb77754..fdd5535 100755
--- a/wgetpaste
+++ b/wgetpaste
@@ -32,24 +32,19 @@ escape() {
}
show_url() {
- # add_to_clipboard() may be defined in /etc/wgetpaste or ~/.wgetpaste and can be used to add
- # ${URL} to your clipboard using xclip, xcut, klipper or whatever your window manager provides
- # for that task.
[[ "$(type -t add_to_clipboard)" == "function" ]] && add_to_clipboard "${1}"
-
echo "Your ${2}paste can be seen here: ${1}"
}
### defaults
# The following defaults can be overridden in either /etc/wgetpaste or ~/.wgetpaste.
-# Only those four variables should be set in those files.
#
# If add_to_clipboard() is defined as a function in one of those files it will be called with
# the url where your paste can be seen as an argument. You may use xclip, xcut, klipper or
# whatever your window manager provides for adding it to your clipboard.
#
-# Likewise if get_from_clipboard() is defined as a funciont in one of those files it will be
+# Likewise if get_from_clipboard() is defined as a funcion in one of those files it will be
# called to retrieve input from your clipboard when --xcut is used.
[[ -f /etc/wgetpaste ]] && . /etc/wgetpaste
[[ -f ~/.wgetpaste ]] && . ~/.wgetpaste
@@ -63,7 +58,8 @@ DEFAULT_EXPIRATION="${DEFAULT_EXPIRATION:-1 month}"
### usage
show_usage() {
- echo "Usage: $0 [options] [file]"
+ [[ -n "$*" ]] && echo "$*"
+ echo "Usage: ${0} [options] [file]"
echo
echo "Options:"
echo " -l, --language LANG set language (defaults to \"${DEFAULT_LANGUAGE}\")"
@@ -90,6 +86,7 @@ show_usage() {
echo
echo "Defaults (DEFAULT_{NICK,SERVICE,LANGUAGE,EXPIRATION}[_\${SERVICE}]) can be"
echo "overridden globally in /etc/wgetpaste or per user in ~/.wgetpaste."
+ [[ -n "$*" ]] && exit 1
}
### services
@@ -102,10 +99,8 @@ INDENTATION=10
show_services() {
echo 'Services supported (case sensitive):'
- let "i = 0"
- for service in ${SERVICES[*]}; do
- echo " ${service} "$'\e'"[${INDENTATION}G- ${SERVICE_URLS[i]}"
- let "i = i + 1"
+ for ((i=0; i<${#SERVICES[*]}; i++)); do
+ echo " ${SERVICES[i]} "$'\e'"[${INDENTATION}G- ${SERVICE_URLS[i]}"
done
}
@@ -153,10 +148,8 @@ show_languages() {
verify_language() {
case "${SERVICE}" in
ca )
- let "i = 0"
- for language in ${ca_LANGUAGES[*]}; do
- let "i = i +1"
- [[ "${LANGUAGE}" == "${language//_/ }" ]] && LANGUAGE="${i}" && return 0
+ for ((i=0; i<${#ca_LANGUAGES[*]}; i++)); do
+ [[ "${LANGUAGE}" == "${ca_LANGUAGES[i]//_/ }" ]] && LANGUAGE="${i}" && return 0
done
;;
rafb )
@@ -165,10 +158,8 @@ verify_language() {
done
;;
osl )
- let "i = 0"
- for language in $(eval "echo \${${SERVICE}_LANGUAGES[*]}"); do
- [[ "${LANGUAGE}" == "${language//_/ }" ]] && LANGUAGE="$(eval "echo \${${SERVICE}_LANGUAGE_VALUES[i]}")" && return 0
- let "i = i + 1"
+ for ((i=0; i<${#osl_LANGUAGES[*]}; i++)); do
+ [[ "${LANGUAGE}" == "${osl_LANGUAGES[i]//_/ }" ]] && LANGUAGE="${osl_LANGUAGE_VALUES[i]}" && return 0
done
;;
* )
@@ -216,10 +207,8 @@ verify_expiration_options() {
done
;;
osl )
- let "i = 0"
- for expiration in $(eval "echo \${${SERVICE}_EXPIRATION_OPTIONS[*]}"); do
- [[ "${EXPIRATION}" == "${expiration//_/ }" ]] && EXPIRATION="$(eval "echo \${${SERVICE}_EXPIRATION_VALUES[i]}")" && return 0
- let "i = i + 1"
+ for ((i=0; i<${#osl_EXPIRATION_OPTIONS[*]}; i++)); do
+ [[ "${EXPIRATION}" == "${osl_EXPIRATION_OPTIONS[i]//_/ }" ]] && EXPIRATION="${osl_EXPIRATION_VALUES[i]}" && return 0
done
;;
* )
@@ -233,37 +222,29 @@ verify_expiration_options() {
# get the url to post to for any given service
get_recipient() {
- let "i = 0"
- if [[ "${1}" == "raw" ]]; then
- for service in ${SERVICES[*]}; do
- [[ "${SERVICE}" == "${service}" ]] && echo "${SERVICE_URLS_RAW[i]}" && return 0
- let "i = i + 1"
- done
- else
- for service in ${SERVICES[*]}; do
- [[ "${SERVICE}" == "${service}" ]] && echo "${SERVICE_URLS[i]}" && return 0
- let "i = i + 1"
- done
- fi
+ for ((i=0; i<${#SERVICES[*]}; i++)); do
+ [[ "${SERVICE}" == "${SERVICES[i]}" ]] && eval "echo \"\${SERVICE_URLS${1}[i]}\"" && return 0
+ done
echo "Failed to get url for \"${SERVICE}\"." 1>&2
exit 1
}
# print a warning if failure is predictable due to the mere size of the paste. sh seems to be the most reliable
-# service in that regard. for now no warning will be printed for that. note that this is only a warning printed.
-# it doesn't abort or anything.
+# service in that regard. note that this is only a warning printed. it doesn't abort or anything.
warn_size() {
- print_warning() {
- echo "Pasting > ${1} often tend to fail with ${SERVICE}. Use --verbose or --debug to see the"
- echo "error output from wget if it fails. Alternatively use another pastebin service like e.g. sh."
+ warn() {
+ if [[ ${SIZE} -gt ${1} ]]; then
+ echo "Pasting > ${2} often tend to fail with ${SERVICE}. Use --verbose or --debug to see the"
+ echo "error output from wget if it fails. Alternatively use another pastebin service like e.g. sh."
+ fi
}
case "${SERVICE}" in
rafb )
- [[ ${SIZE} -gt 512000 ]] && print_warning "512 kb"
+ warn 512000 "512 kb"
;;
ca )
- [[ ${SIZE} -gt 1024000 ]] && print_warning "1 MB"
+ warn 1024000 "1 MB"
;;
esac
}
@@ -293,11 +274,9 @@ post_data() {
# indicate if ${SERVICE} needs stdout output from wget to get the resulting url
need_stdout() {
case "${SERVICE}" in
- # this service needs to use a regex to retrieve the url from stdout
ca )
return 0
;;
- # these services read the reply from Location: which goes to stderr
osl | rafb | sh )
return 1
;;
@@ -324,16 +303,12 @@ get_url() {
esac
}
-# verify that the pastebin service didn't return a known error url such as toofast.html from rafb
+# verify that the pastebin service didn't return a known error url or print a helpful error message
verify_url() {
- case "${SERVICE}" in
- rafb )
- if [[ "${URL}" == "http://rafb.net/p/toofast.html" ]]; then
- echo "You must wait at least 10 seconds between each paste! Try again in 10 seconds." 1>&2
- exit 1
- fi
- ;;
- esac
+ if [[ "${SERVICE}" == "rafb" ]] && [[ "${URL}" == "http://rafb.net/p/toofast.html" ]]; then
+ echo "You must wait at least 10 seconds between each paste! Try again in 10 seconds." 1>&2
+ exit 1
+ fi
}
# if possible convert URL to raw
@@ -360,12 +335,15 @@ convert_to_raw() {
# convert groups of short options to singular short options. convert long options to short options.
for arg in "${@}"; do
- if [[ "${arg}" == --*=* ]]; then
+ if [[ "${arg}" == "--" ]]; then
+ STOP=true
+ ARGS[${#ARGS[*]}]="${arg}"
+ elif [[ "${arg}" == --*=* ]]; then
ARGS[${#ARGS[*]}]="${arg%=*}"
ARGS[${#ARGS[*]}]="${arg#*=}"
elif [[ "${arg}" == --* ]]; then
ARGS[${#ARGS[*]}]="${arg}"
- elif [[ "${arg}" == -* ]]; then
+ elif [[ "${arg}" == -* ]] && [[ ! ${STOP} ]]; then
for short_arg in $(echo "${arg#-}" | sed 's|.| -&|g'); do
ARGS[${#ARGS[*]}]="${short_arg}"
done
@@ -382,7 +360,7 @@ set -- "${ARGS[@]}"
while [[ ! -z "${1}" ]]; do
case "${1}" in
-c | --command )
- [[ -z "${2}" ]] && show_usage 1>&2 && exit 1
+ [[ -z "${2}" ]] && show_usage "${0}: option requires an argument -- c" 1>&2 && exit 1
SOURCE="command"
COMMANDS[${#COMMANDS[*]}]="${2}"
shift 2
@@ -439,7 +417,7 @@ while [[ ! -z "${1}" ]]; do
shift
;;
-s | --service )
- [[ -z "${2}" ]] && show_usage 1>&2 && exit 1
+ [[ -z "${2}" ]] && show_usage "${BASENAME}: option requires an argument -- s" 1>&2 && exit 1
verify_service "${2}"
SERVICE="$(escape "${2}")"
shift 2
@@ -453,19 +431,28 @@ while [[ ! -z "${1}" ]]; do
shift
;;
--version )
- echo "$0, version ${VERSION}"
+ echo "${0}, version ${VERSION}"
exit 0
;;
-x | --xcut )
SOURCE="xcut"
shift
;;
+ -- )
+ if [[ -f "${2}" ]]; then
+ SOURCE="${2}"
+ break
+ else
+ show_usage 1>&2
+ exit 1
+ fi
+ ;;
*)
if [[ -f "${1}" ]]; then
SOURCE="${1}"
shift
else
- show_usage 1>&2
+ echo "${0}: ${1}: No such file found. " 1>&2
exit 1
fi
;;
@@ -544,12 +531,10 @@ case "${SOURCE}" in
else
echo "You need to define get_from_clipboard() in /etc/wgetpaste or ~/.wgetpaste to use" 1>&2
echo "--xcut. If you want to use e.g. xclip simply emerge xclip and define it like this:" 1>&2
- echo 1>&2
- echo -e "get_from_clipboard() {\n xclip -o\n}\n" 1>&2
+ echo -e "\nget_from_clipboard() {\n xclip -o\n}\n" 1>&2
echo "Likewise if you want the resulting url stored in your clipboard using e.g. xclip" 1>&2
echo "define it like this:" 1>&2
- echo 1>&2
- echo -e "add_to_clipboard() {\n xclip \"\$*\"\n}\n" 1>&2
+ echo -e "\nadd_to_clipboard() {\n xclip \"\$*\"\n}\n" 1>&2
echo "You may use whatever your window manager provides to alter your clipboard instead" 1>&2
echo "of xclip." 1>&2
exit 1
@@ -559,8 +544,6 @@ case "${SOURCE}" in
# handle the case where the input source (defaulting to /dev/stdin) isn't readable verbosely
if [[ ! -r "${SOURCE}" ]]; then
echo "The input source: \"${SOURCE}\" is not readable. Please specify a readable input source." 1>&2
- echo 1>&2
- show_usage 1>&2
exit 1
fi
INPUT="$( < "${SOURCE}" )"
@@ -587,7 +570,6 @@ TEMPFILE="$(mktemp /tmp/wgetpaste.XXXXXX)"
if [[ -n "${TEMPFILE}" ]] && [[ -f "${TEMPFILE}" ]]; then
# write paste data to the temporary file
post_data > "${TEMPFILE}" || die "Failed to write to temporary file: \"${TEMPFILE}\"."
-
WGET_ARGS="--post-file=${TEMPFILE}"
else
# fall back to using --post-data if the temporary file could not be created
@@ -596,7 +578,7 @@ else
fi
# set recipient
-RECIPIENT="$(get_recipient "raw")"
+RECIPIENT="$(get_recipient "_RAW")"
# paste it
WGET_ARGS="--tries=5 --timeout=60 ${WGET_ARGS}"
@@ -625,8 +607,7 @@ verify_url
# handle the case when there was no location returned
if [[ -z "${URL}" ]]; then
if [[ ${DEBUG} ]] || [[ ${VERBOSE} ]]; then
- echo "Apparently nothing was received. Perhaps the connection failed." 1>&2
- echo "${OUTPUT}" 1>&2
+ echo "Apparently nothing was received. Perhaps the connection failed."$'\n'"${OUTPUT}" 1>&2
else
echo "Apparently nothing was received. Perhaps the connection failed. Enable --verbose or" 1>&2
echo "--debug to get the output from wget that can help diagnose it correctly." 1>&2