diff --git a/Makefile b/Makefile index 10e2278..45c957f 100644 --- a/Makefile +++ b/Makefile @@ -46,10 +46,14 @@ standalone/vimpager: vimpager vimpager-version.txt ${SRC:=.uu} inc/* Makefile @echo building $@ @${MKPATH} ${@D} @sed \ - -e '/^ *\. .*inc\/prologue.sh"$$/{' \ + -e '/^\. .*inc\/prologue.sh/{' \ -e 'r inc/prologue.sh' \ -e d \ -e '}' \ + -e '/^\. .*inc\/platform.sh/{' \ + -e 'r inc/platform.sh' \ + -e d \ + -e '}' \ -e 's/^\( *\)# EXTRACT BUNDLED SCRIPTS HERE$$/\1extract_bundled_scripts/' \ -e 's|^version=.*|version="'"`cat vimpager-version.txt`"' (standalone, shell=\$$(command -v \$$POSIX_SHELL))"|' \ -e 's!^\( *\)runtime=.*$$!\1runtime='\''\$$tmp/runtime'\''!' \ @@ -75,7 +79,7 @@ standalone/vimcat: vimcat autoload/vimcat.vim vimcat-version.txt inc/prologue.sh -e '/^ *--cmd "set rtp^=\$$runtime" \\$$/d' \ -e '/call vimcat#Init/i\'"$$nl"'--cmd "$$silent source $$0" \\' \ -e 's/vimcat#\([^ ]*\)(/\1(/g' \ - -e '/^ *\. .*inc\/prologue.sh"$$/{' \ + -e '/^\. .*inc\/prologue.sh/{' \ -e 'r inc/prologue.sh' \ -e d \ -e '}' \ @@ -95,7 +99,7 @@ vimcat.uu: vimcat vimcat-version.txt @printf "\t(cat <<'EOF') | do_uudecode > bin/vimcat\n" >> $@ @sed \ -e 's|^version=.*|version="'"`cat vimcat-version.txt`"' (bundled, shell=\$$(command -v \$$POSIX_SHELL))"|' \ - -e '/^ *\. .*inc\/prologue.sh"$$/{' \ + -e '/^\. .*inc\/prologue.sh/{' \ -e 'r inc/prologue.sh' \ -e d \ -e '}' \ @@ -184,6 +188,7 @@ install: docs vimpager.configured vimcat.configured sed -e '1{ s|.*|#!'"$$POSIX_SHELL"'|; }' \ -e 's|\$$POSIX_SHELL|'"$$POSIX_SHELL|" \ -e '/^ *\. .*inc\/prologue.sh"$$/d' \ + -e 's/^ *\. .*inc\/platform.sh"$$/'"`scripts/find_platform`"/ \ -e 's|^version=.*|version="'"`cat $<-version.txt`"' (configured, shell='"$$POSIX_SHELL"')"|' \ -e '/^# FIND REAL PARENT DIRECTORY$$/,/^# END OF FIND REAL PARENT DIRECTORY$$/d' \ -e 's!^\( *\)runtime=.*!\1runtime='\''${PREFIX}/share/vimpager'\''!' \ diff --git a/inc/platform.sh b/inc/platform.sh new file mode 100644 index 0000000..84badae --- /dev/null +++ b/inc/platform.sh @@ -0,0 +1,73 @@ +#!/bin/sh + +# This file is used to set some variables acording to the system we are +# running on. It assumes a POSIX compatible shell. + +# Detect the operating system. +case $(uname -s) in + Linux) linux=1 ;; + SunOS) solaris=1 ;; + Darwin) osx=1; bsd=1 ;; + CYGWIN*) cygwin=1; win32=1 ;; + MINGW*) msys=1; win32=1 ;; + MSYS*) msys=1; win32=1 ;; + OpenBSD) openbsd=1; bsd=1 ;; + FreeBSD) freebsd=1; bsd=1 ;; + NetBSD) netbsd=1; bsd=1 ;; + *) bsd=1 ;; +esac + +# Find a suitble awk executable. +if [ -n "$AWK" ] && command -v "$AWK" >/dev/null; then + : +elif command -v gawk >/dev/null; then + AWK=gawk +elif command -v nawk >/dev/null; then + AWK=nawk +elif command -v mawk >/dev/null; then + AWK=mawk +elif [ -x /usr/xpg4/bin/awk ]; then + AWK=/usr/xpg4/bin/awk +elif command -v awk >/dev/null; then + AWK=awk +else + echo "ERROR: No awk found!" >&2 + exit 1 +fi + +# Find a suitable sed executable. +if [ -n "$SED" ] && command -v "$SED" >/dev/null; then + : +elif command -v gsed >/dev/null; then + SED=gsed +elif [ -x /usr/xpg4/bin/sed ]; then + SED=/usr/xpg4/bin/sed +elif command -v sed >/dev/null; then + SED=sed +else + echo "ERROR: No sed found!" >&2 + exit 1 +fi + +# Find a suitable head executable. +if command -v ghead >/dev/null; then + HEAD=ghead +else + HEAD=head +fi + +# Check the syntax for head. +if [ "$HEAD_SYNTAX" = new -o "$HEAD_SYNTAX" = old ]; then + : +else + if [ "$(echo xx | "$HEAD" -n 1 2>/dev/null)" = xx ]; then + HEAD_SYNTAX=new + else + if ! "$HEAD" -1 -- "$0" >/dev/null 2>&1; then + HEAD_NO_DOUBLE_DASH=1 + fi + HEAD_SYNTAX=old + fi +fi + +# vim: sw=4 et tw=0: diff --git a/scripts/find_platform b/scripts/find_platform new file mode 100755 index 0000000..b5ed12a --- /dev/null +++ b/scripts/find_platform @@ -0,0 +1,30 @@ +#!/bin/sh + +. inc/platform.sh + +# The backslash has to be put here 4 times: foctor two to get past the initial +# shell quoting, another factor two because it is used in the replacement +# string for sed. This yields one backslash that can escape the single quote +# in the configured vimpager script. +AWK=$(echo "$AWK" | sed "s/'/'\\\\''/g") +SED=$(echo "$SED" | sed "s/'/'\\\\''/g") + +# The final sed is used to escape special characters for the sed command in +# the makefile, where the output will be used. +echo \ + ${bsd:+bsd=$bsd} \ + ${cygwin:+cygwin=$cygwin} \ + ${freebsd:+freebsd=$freebsd} \ + ${linux:+linux=$linux} \ + ${msys:+msys=$msys} \ + ${netbsd:+netbsd=$netbsd} \ + ${openbsd:+openbsd=$openbsd} \ + ${osx:+osx=$osx} \ + ${solaris:+solaris=$solaris} \ + ${win32:+win32=$win32} \ + "AWK='$AWK'" \ + "SED='$SED'" \ + ${HEAD_SYNTAX:+HEAD_SYNTAX=$HEAD_SYNTAX} \ + | sed -e 's/\\/\\\\/g' -e 's/|/\|/g' + +# vim: sw=4 et tw=0: diff --git a/vimpager b/vimpager index ed92df5..0085adf 100755 --- a/vimpager +++ b/vimpager @@ -27,7 +27,8 @@ done project_dir=`dirname "$link"` # END OF FIND REAL PARENT DIRECTORY -. "$project_dir/inc/prologue.sh" +. "$project_dir/inc/prologue.sh" # Hopefully we're now POSIX. +. "$project_dir/inc/platform.sh" [ -n "$ZSH_VERSION" ] && emulate -R sh 2>/dev/null # force zsh into full POSIX @@ -36,19 +37,6 @@ runtime='$project_dir' vimcat='$project_dir/vimcat' system_vimpagerrc='$project_dir/vimpagerrc' -case "$(uname -s)" in - Linux) linux=1 ;; - SunOS) solaris=1 ;; - Darwin) osx=1; bsd=1 ;; - CYGWIN*) cygwin=1; win32=1 ;; - MINGW*) msys=1; win32=1 ;; - MSYS*) msys=1; win32=1 ;; - OpenBSD) openbsd=1; bsd=1 ;; - FreeBSD) freebsd=1; bsd=1 ;; - NetBSD) netbsd=1; bsd=1 ;; - *) bsd=1 ;; -esac - main() { # if no args and no stdin, display usage if [ $# -eq 0 -a -t 0 ]; then @@ -219,12 +207,12 @@ main() { # squeeze blank lines if option was specified, Ubuntu man with /usr/bin/pager does this if [ "${squeeze_blank_lines:-0}" -eq 1 ]; then - sed -e '/^[ ]*$/{ - N - /^[ ]*\n[ ]*$/D - }' < "${tempfile:-$file}" > "$tmp/$filename.work" - tempfile=$tmp/$filename - mv -f -- "$tempfile.work" "$tempfile" + sed -e '/^[ ]*$/{ + N + /^[ ]*\n[ ]*$/D + }' < "${tempfile:-$file}" > "$tmp/$filename.work" + tempfile=$tmp/$filename + mv -f -- "$tempfile.work" "$tempfile" fi # dumb man detection when the pstree heuristic fails @@ -639,38 +627,12 @@ Press ',h' for a summary of keystrokes in the program. EOF } -if command -v gawk >/dev/null; then - _awk=gawk -elif command -v nawk >/dev/null; then - _awk=nawk -elif command -v mawk >/dev/null; then - _awk=mawk -elif [ -x /usr/xpg4/bin/awk ]; then - _awk=/usr/xpg4/bin/awk -elif command -v awk >/dev/null; then - _awk=awk -else - echo "ERROR: No awk found!" >&2 - quit 1 -fi - awk() { - command "$_awk" "$@" + command "$AWK" "$@" } -if command -v gsed >/dev/null; then - _sed=gsed -elif [ -x /usr/xpg4/bin/sed ]; then - _sed=/usr/xpg4/bin/sed -elif command -v sed >/dev/null; then - _sed=sed -else - echo "ERROR: No sed found!" >&2 - quit 1 -fi - sed() { - command "$_sed" "$@" + command "$SED" "$@" } @@ -724,20 +686,6 @@ awk_grep_E_q() { ' "$@" } -if command -v ghead >/dev/null; then - _head=ghead -else - _head=head -fi - -if [ "$(echo xx | head -n 1 2>/dev/null)" = "xx" ]; then - _head_syntax=new -else - if ! head -1 -- "$0" >/dev/null 2>&1; then - _head_no_double_dash=1 - fi -fi - head() { _lines= case "$1" in @@ -747,13 +695,13 @@ head() { esac if [ -z "$_lines" ]; then - command "$_head" "$@" - elif [ "$_head_syntax" = "new" ]; then - command "$_head" -n $_lines -- "$@" - elif [ -z "$_head_no_double_dash" ]; then - command "$_head" -$_lines -- "$@" + command "$HEAD" "$@" + elif [ "$HEAD_SYNTAX" = "new" ]; then + command "$HEAD" -n $_lines -- "$@" + elif [ -z "$HEAD_NO_DOUBLE_DASH" ]; then + command "$HEAD" -$_lines -- "$@" else - command "$_head" -$_lines "$@" + command "$HEAD" -$_lines "$@" fi }