From cc5474d7efe6845e2f89ac915d6e1289b26044b2 Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Wed, 25 Jan 2012 15:25:12 +0100 Subject: merge functions and shell helper into main script This makes the main script able to run alone again. Signed-off-by: Florian Pritz --- Makefile | 17 +++++++---------- fb-helper.sh.in | 24 ------------------------ fb.in | 53 ++++++++++++++++++++++++++++++++++++++++++++--------- functions | 9 --------- 4 files changed, 51 insertions(+), 52 deletions(-) delete mode 100644 fb-helper.sh.in delete mode 100644 functions diff --git a/Makefile b/Makefile index f91b94e..3370c1d 100644 --- a/Makefile +++ b/Makefile @@ -7,23 +7,19 @@ CC=cc CFLAGS?=-O2 -std=c99 -Wall -Wextra -pedantic LIBCURL:=$(shell pkg-config --silence-errors --libs --cflags libcurl) +ifdef LIBCURL all: fb.1 fb fb-helper +else +all: fb.1 fb +endif fb: fb.in @[ -n "$(VERSION)" ] || (echo "Error: version detection failed"; exit 1) sed 's|@VERSION@|$(VERSION)|; s|@LIBDIR@|$(MY_LIBDIR)|' $< > $@ chmod 755 $@ -ifdef LIBCURL fb-helper: fb-helper.c $(CC) $(CFLAGS) $(LIBCURL) -DVERSION=\"$(VERSION)\" -o $@ $< -else -fb-helper: fb-helper.sh.in - @echo "libcurl not found. using shell helper..." - @[ -n "$(VERSION)" ] || (echo "Error: version detection failed"; exit 1) - sed 's|@VERSION@|$(VERSION)|; s|@LIBDIR@|$(MY_LIBDIR)|' $< > $@ - chmod 755 $@ -endif fb.1: fb.pod pod2man -c "" $< $@ @@ -34,8 +30,9 @@ clean: install: all install -Dm755 fb $(DESTDIR)$(BINDIR)/fb +ifdef LIBCURL install -Dm755 fb-helper $(DESTDIR)$(MY_LIBDIR)/fb-helper - install -Dm755 functions $(DESTDIR)$(MY_LIBDIR)/functions +endif install -Dm644 fb.1 $(DESTDIR)$(MANDIR)/man1/fb.1 uninstall: @@ -46,7 +43,7 @@ uninstall: dist: all @[ -n "$(VERSION)" ] || (echo "Error: version detection failed"; exit 1) mkdir -p dist/fb-$(VERSION) - cp -a fb-helper.c fb{,.in} fb.pod fb.1 functions COPYING Makefile dist/fb-$(VERSION) + cp -a fb-helper.c fb{,.in} fb.pod fb.1 COPYING Makefile dist/fb-$(VERSION) sed -i 's/^VERSION:=.*$$/VERSION:='$(VERSION)'/' dist/fb-$(VERSION)/Makefile cd dist; tar -czf fb-$(VERSION).tar.gz fb-$(VERSION) diff --git a/fb-helper.sh.in b/fb-helper.sh.in deleted file mode 100644 index 0c023fb..0000000 --- a/fb-helper.sh.in +++ /dev/null @@ -1,24 +0,0 @@ -#!/bin/sh - -mode=$1 -url=$2 -file=$3 - -LIBDIR="@LIBDIR@" - -source "$LIBDIR/functions" - -USERAGENT="fb-client/shell-@VERSION@" -CURLOPTS="-n -L -A $USERAGENT" - -if [ "$mode" = "u" ]; then - basefilename=`basename -- "$file"` - if [ `$STAT -- "$file"` -eq "0" ] || echo "$basefilename" | grep -F -q ","; then - base64fn=`echo "$basefilename" | base64 -w0 ` - curl $CURLOPTS -F "file=@-" -F "filename=$base64fn" "$url" < "$file" -o /dev/stdout - else - curl $CURLOPTS -F "file=@$file" "$url" -o /dev/stdout - fi -else - curl $CURLOPTS "$url" -fi diff --git a/fb.in b/fb.in index 2a67f83..3287deb 100644 --- a/fb.in +++ b/fb.in @@ -24,7 +24,42 @@ CLIPBOARD="" EXITCODE=0 LIBDIR="@LIBDIR@" -source "$LIBDIR/functions" +# the calling conventions for stat(1) are highly system dependent +STAT='stat -c %s' # GNU stat(1) is the default since most people have it +case "`uname -s`" in + *BSD) STAT='stat -f %z';; + Minix) STAT='stat -size';; +esac + +request_helper() { + mode=$1 + url=$2 + file=$3 + + # if available use the external helper, else fall back to calling curl + if [ -x "$LIBDIR/fb-helper" ]; then + $LIBDIR/fb-helper "$mode" "$url" "$file" + else + USERAGENT="fb-client/shell-$VERSION" + CURLOPTS="-n -L -A $USERAGENT" + + if [ "$mode" = "d" ]; then + CURLOPTS="$CURLOPTS -s" + fi + + if [ "$mode" = "u" ]; then + basefilename=`basename -- "$file"` + if [ `$STAT -- "$file"` -eq "0" ] || echo "$basefilename" | grep -F -q ","; then + base64fn=`echo "$basefilename" | base64 -w0 ` + curl $CURLOPTS -F "file=@-" -F "filename=$base64fn" "$url" < "$file" -o /dev/stdout + else + curl $CURLOPTS -F "file=@$file" "$url" -o /dev/stdout + fi + else + curl $CURLOPTS "$url" + fi + fi +} require_executable() { if ! type $1 >/dev/null; then @@ -90,14 +125,14 @@ do_upload() { TMPFILE=`mktemp "$TMPDIR/data.XXXXXX"` if [ `$STAT -- "$file"` -gt "$WARNSIZE" ]; then - WARNSIZE=`$LIBDIR/fb-helper d "$PASTEBIN/file/get_max_size"` + WARNSIZE=`request_helper d "$PASTEBIN/file/get_max_size"` if [ `$STAT -- "$file"` -gt "$WARNSIZE" ]; then echo "Warning: Your upload is too big and would be rejected. Maximum size is: $WARNSIZE bytes. Skipping..." >&2 return 1 fi fi - $LIBDIR/fb-helper u "$PASTEBIN/file/do_upload" "$file" > $TMPFILE || return 1 + request_helper u "$PASTEBIN/file/do_upload" "$file" > $TMPFILE || return 1 sed '$d' $TMPFILE >&2 URL=`tail -1 $TMPFILE`"$EXTENSION" @@ -177,20 +212,20 @@ if [ "$DELETE" ] || [ "$GET" ]; then for i in "$@"; do i=$(id_from_arg "$i") if [ "$DELETE" ]; then - $LIBDIR/fb-helper d "$PASTEBIN/file/delete/$i" || EXITCODE=1 + request_helper d "$PASTEBIN/file/delete/$i" || EXITCODE=1 elif [ "$GET" ]; then if [ "$COMPRESS" = "1" ]; then - $LIBDIR/fb-helper d "$PASTEBIN/$i" | gzip -cd || EXITCODE=1 + request_helper d "$PASTEBIN/$i" | gzip -cd || EXITCODE=1 elif [ "$COMPRESS" = "2" ]; then - $LIBDIR/fb-helper d "$PASTEBIN/$i" | xz -cd || EXITCODE=1 + request_helper d "$PASTEBIN/$i" | xz -cd || EXITCODE=1 else - $LIBDIR/fb-helper d "$PASTEBIN/$i" || EXITCODE=1 + request_helper d "$PASTEBIN/$i" || EXITCODE=1 fi fi done elif [ "$DISPLAYHISTORY" ]; then - $LIBDIR/fb-helper d "$PASTEBIN/file/upload_history" || EXITCODE=1 + request_helper d "$PASTEBIN/file/upload_history" || EXITCODE=1 elif [ $# -eq 0 ]; then if [ "$TAR" ]; then @@ -218,7 +253,7 @@ else for i in "$@"; do if is_url "$i"; then cd $TMPDIR - if ! $LIBDIR/fb-helper d "$i" > "`basename "$i"`"; then + if ! request_helper d "$i" > "`basename "$i"`"; then EXITCODE=1 continue fi diff --git a/functions b/functions deleted file mode 100644 index 1dc76d3..0000000 --- a/functions +++ /dev/null @@ -1,9 +0,0 @@ -#!/bin/sh - -# the calling conventions for stat(1) are highly system dependent -STAT='stat -c %s' # GNU stat(1) is the default since most people have it -case "`uname -s`" in - *BSD) STAT='stat -f %z';; - Minix) STAT='stat -size';; -esac - -- cgit v1.2.3-24-g4f1b