summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBo Ørsted Andresen <bo.andresen@zlin.dk>2007-03-14 07:47:12 +0000
committerBo Ørsted Andresen <bo.andresen@zlin.dk>2007-03-14 07:47:12 +0000
commite2a824c0ed5764652106de7b5b30fdc93a28b530 (patch)
tree1785d4047e2bf9ba553bb10cbdea0ed90f858214
parente4a58fe3f249bf42b347f32faf358aa9830d3e4e (diff)
downloadwgetpaste-e2a824c0ed5764652106de7b5b30fdc93a28b530.tar.gz
Further optimizations for size. Fix bug with verify() counting from 0 instead of 1. Collapse need_stdout() into get_url(). Let get_recipient print ${SERVICE} with _RAW isn't an argument. Add header() and no_support() helper functions to print almost identical messages. Bump to version 2.1 given the number of changes since 2 was released.
-rwxr-xr-xwgetpaste94
1 files changed, 39 insertions, 55 deletions
diff --git a/wgetpaste b/wgetpaste
index 9f4e9b6..269b572 100755
--- a/wgetpaste
+++ b/wgetpaste
@@ -4,7 +4,7 @@
# Author: Bo Ørsted Andresen, bo.andresen@zlin.dk
##########################################################################
-VERSION="2"
+VERSION="2.1"
### helper functions
@@ -14,14 +14,14 @@ die() {
exit 1
}
+no_support() {
+ echo -e "\n${2}\"${SERVICE}\" has no support for setting ${1}." 1>&2
+}
+
# show that an option ${1} is not supported, run function that shows valid options ${3} and die
fail() {
- if [[ "${2}" == "service" ]]; then
- echo "\"$1\" is not a supported $2." 1>&2
- else
- echo "\"$1\" is not a supported $2 for ${SERVICE}: $(get_recipient)." 1>&2
- fi
- echo 1>&2
+ [[ "${2}" == "service" ]] || FOR=" for $(get_recipient)"
+ echo "\"$1\" is not a supported $2${FOR}."$'\n' 1>&2
${3} 1>&2
exit 1
}
@@ -36,6 +36,7 @@ show_url() {
echo "Your ${2}paste can be seen here: ${1}"
}
+# Used for --info and --info-only
INFO_COMMAND="emerge --info"
INFO_ARGS="--ignore-default-opts"
@@ -61,7 +62,7 @@ DEFAULT_EXPIRATION="${DEFAULT_EXPIRATION:-1 month}"
### usage
show_usage() {
- echo "Usage: ${0} [options] [file]"
+ echo "Usage: ${0} [options] [file[s]]"
echo
echo "Options:"
echo " -l, --language LANG set language (defaults to \"${DEFAULT_LANGUAGE}\")"
@@ -98,8 +99,13 @@ SERVICE_URLS_RAW=(http://pastebin.ca http://rafb.net/paste/paste.php http://past
# 4 (base indentation) + max service length + 2 (space + dash)
INDENTATION=10
+header() {
+ [[ "${1}" == "Service" ]] || BY="by $(get_recipient) "
+ echo "${1}s supported ${BY}(case sensitive):"
+}
+
show_services() {
- echo 'Services supported (case sensitive):'
+ header "Service"
for ((i=0; i<${#SERVICES[*]}; i++)); do
echo " ${SERVICES[i]} "$'\e'"[${INDENTATION}G- ${SERVICE_URLS[i]}"
done
@@ -123,7 +129,7 @@ verify() {
if [[ "$(eval "echo \${${1}}")" == "$(eval "echo \${${SERVICE}_${1}S[i]//_/ }")" ]]; then
case "${2}" in
count )
- eval "${1}=\"${i}\""
+ let "i++" && eval "${1}=\"${i}\""
;;
value )
eval "${1}=\${${SERVICE}_${1}_VALUES[i]}"
@@ -139,7 +145,7 @@ verify() {
# rafb
rafb_LANGUAGES=(C C89 C99 C++ C\# Java Pascal Perl PHP PL\/I Python Ruby SQL VB Plain_Text)
-# ca - ordering is important here as their value is set as their number in the order
+# ca - ordering is important here as count is used as value
ca_LANGUAGES=(Plain_Text Asterisk_Configuration C C++ PHP Perl Java VB C\# Ruby Python Pascal \
mIRC PL/I XML SQL Scheme ActionScript Ada Apache_Configuration Assembly_\(NASM\) ASP Bash CSS \
Delphi HTML_4\.0_Strict JavaScript LISP Lua Microprocessor_ASM Objective_C VB\.NET)
@@ -155,14 +161,13 @@ javascript lisp lua matlab mpasm mysql nsis objc ocaml oobas oracle8 pascal perl
qbasic robots ruby scheme smarty sql tcl vb vbnet visualfoxpro xml)
show_languages() {
- echo "Languages supported by ${SERVICE}: $(get_recipient) (case sensitive):"
+ header "Language"
case "${SERVICE}" in
ca | osl | rafb )
show LANGUAGES | sort
;;
* )
- echo 1>&2
- echo "\"${SERVICE}\" has no support for any specific languages." 1>&2
+ no_support language
esac
}
@@ -179,7 +184,7 @@ verify_language() {
verify LANGUAGE value && return 0
;;
* )
- [[ ! ${LANGUAGE_SET} ]] && return 0
+ [[ ${LANGUAGE_SET} ]] || return 0
;;
esac
fail "${LANGUAGE}" "language" "show_languages"
@@ -196,19 +201,16 @@ osl_EXPIRATIONS=(Never 1_day 1_month)
osl_EXPIRATION_VALUES=(f d m)
show_expiration_options() {
- echo "Expiration options supported by ${SERVICE}: $(get_recipient) (case sensisitive):"
+ header "Expiration option"
case "${SERVICE}" in
ca | osl )
show EXPIRATIONS
;;
rafb )
- echo 1>&2
- echo "Pastes on ${SERVICE}: $(get_recipient) expires after 24 hours." 1>&2
- echo "${SERVICE} has no suppport for setting expiration." 1>&2
+ no_support expiration "Pastes on $(get_recipient) expire after 24 hours.\n"
;;
* )
- echo 1>&2
- echo "${SERVICE} has no suppport for setting expiration." 1>&2
+ no_support expiration
;;
esac
}
@@ -222,7 +224,7 @@ verify_expiration_options() {
verify EXPIRATION value && return 0
;;
* )
- [[ ! ${EXPIRATION_SET} ]] && return 0
+ [[ ${EXPIRATION_SET} ]] || return 0
;;
esac
fail "${EXPIRATION}" "expiration option" "show_expiration_options"
@@ -232,8 +234,9 @@ verify_expiration_options() {
# get the url to post to for any given service
get_recipient() {
+ [[ "${1}" == "_RAW" ]] || serv="${SERVICE}: "
for ((i=0; i<${#SERVICES[*]}; i++)); do
- [[ "${SERVICE}" == "${SERVICES[i]}" ]] && eval "echo \"\${SERVICE_URLS${1}[i]}\"" && return 0
+ [[ "${SERVICE}" == "${SERVICES[i]}" ]] && eval "echo ${serv}\"\${SERVICE_URLS${1}[i]}\"" && return 0
done
die "Failed to get url for \"${SERVICE}\"."
}
@@ -247,7 +250,6 @@ warn_size() {
echo "error output from wget if it fails. Alternatively use another pastebin service like e.g. sh."
fi
}
-
case "${SERVICE}" in
rafb )
warn 512000 "512 kb"
@@ -258,7 +260,6 @@ warn_size() {
esac
}
-# POST data
post_data() {
case "${SERVICE}" in
ca )
@@ -279,28 +280,14 @@ post_data() {
esac
}
-# indicate if ${SERVICE} needs stdout output from wget to get the resulting url
-need_stdout() {
- case "${SERVICE}" in
- ca )
- return 0
- ;;
- osl | rafb | sh )
- return 1
- ;;
- * )
- die "\"${SERVICE}\" is not supported by ${FUNCNAME}()."
- ;;
- esac
-}
-
-# get the url
get_url() {
case "${SERVICE}" in
ca )
+ [[ "${1}" == "need_stdout" ]] && return 0
echo "$*" | sed -n 's|^.*content="[0-9]*;\(http://pastebin.ca/[0-9]*\)".*$|\1|p'
;;
osl | rafb | sh )
+ [[ "${1}" == "need_stdout" ]] && return 1
echo "$*" | sed -n 's|^.*Location:\ \(http://[^\ ]\+\).*$|\1|p'
;;
* )
@@ -311,9 +298,8 @@ get_url() {
# verify that the pastebin service didn't return a known error url or print a helpful error message
verify_url() {
- if [[ "${SERVICE}" == "rafb" ]] && [[ "${URL}" == "http://rafb.net/p/toofast.html" ]]; then
+ [[ "${SERVICE}" == "rafb" ]] && [[ "${URL}" == "http://rafb.net/p/toofast.html" ]] && \
die "You must wait at least 10 seconds between each paste! Try again in 10 seconds."
- fi
}
# if possible convert URL to raw
@@ -329,7 +315,7 @@ convert_to_raw() {
RAW_URL="$(echo "${URL}" | sed -e 's|html\?$|txt|')"
;;
* )
- echo "Raw download of pastes is not supported by ${SERVICE}: $(get_recipient)." 1>&2
+ echo "Raw download of pastes is not supported by $(get_recipient)." 1>&2
return 1
;;
esac
@@ -383,7 +369,7 @@ get_filename() {
done
}
-while [[ ! -z "${1}" ]]; do
+while [[ -n "${1}" ]]; do
case "${1}" in
-c | --command )
[[ -z "${2}" ]] && no_argument "${1}"
@@ -548,8 +534,7 @@ case "${SOURCE}" in
echo "define it like this:" 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
+ die "of xclip."
fi
;;
files | stdin )
@@ -557,9 +542,8 @@ case "${SOURCE}" in
if [[ ${#FILES[*]} -gt 1 ]]; then
for ((i=0; i<${#FILES[*]}; i++)); do
file="${FILES[i]}"
- if [[ ! -r "${file}" ]]; then
+ [[ -r "${file}" ]] || \
die "The input source: \"${file}\" is not readable. Please specify a readable input source."
- fi
INPUT="${INPUT}${PS1} cat ${file}"$'\n'"$( < "${file}" )"$'\n\n'
done
else
@@ -600,18 +584,18 @@ RECIPIENT="$(get_recipient "_RAW")"
# paste it
WGET_ARGS="--tries=5 --timeout=60 ${WGET_ARGS}"
-if ! need_stdout && [[ ! ${DEBUG} ]] && [[ -w /dev/null ]]; then
- OUTPUT="$(wget -O /dev/null ${WGET_ARGS} ${RECIPIENT} 2>&1)"
-else
+if get_url need_stdout || [[ ${DEBUG} ]] || [[ ! -w /dev/null ]]; then
OUTPUT="$(wget -O - ${WGET_ARGS} ${RECIPIENT} 2>&1)"
+else
+ OUTPUT="$(wget -O /dev/null ${WGET_ARGS} ${RECIPIENT} 2>&1)"
fi
# clean temporary file if it was created
if [[ -n "${TEMPFILE}" ]] && [[ -f "${TEMPFILE}" ]]; then
- if [[ ! ${DEBUG} ]]; then
- rm "${TEMPFILE}" || echo "Failed to remove temporary file: \"${TEMPFILE}\"." 1>&2
- else
+ if [[ ${DEBUG} ]]; then
echo "Left temporary file: \"${TEMPFILE}\" alone for debugging purposes."
+ else
+ rm "${TEMPFILE}" || echo "Failed to remove temporary file: \"${TEMPFILE}\"." 1>&2
fi
fi