From 743b1b558876cbf4739083acdd569e69fb7174d7 Mon Sep 17 00:00:00 2001 From: Lucas Hoffmann Date: Tue, 19 Apr 2016 13:05:13 +0200 Subject: [PATCH 1/6] Handle system detection like shell detection. --- Makefile | 5 +++++ inc/platform.sh | 20 ++++++++++++++++++++ scripts/find_platform | 17 +++++++++++++++++ vimpager | 28 ++++++++-------------------- 4 files changed, 50 insertions(+), 20 deletions(-) create mode 100644 inc/platform.sh create mode 100755 scripts/find_platform diff --git a/Makefile b/Makefile index 10e2278..90cdb9e 100644 --- a/Makefile +++ b/Makefile @@ -50,6 +50,10 @@ standalone/vimpager: vimpager vimpager-version.txt ${SRC:=.uu} inc/* Makefile -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'\''!' \ @@ -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..9cc7aba --- /dev/null +++ b/inc/platform.sh @@ -0,0 +1,20 @@ +#!/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 + +# vim: sw=4 et tw=0: diff --git a/scripts/find_platform b/scripts/find_platform new file mode 100755 index 0000000..319157f --- /dev/null +++ b/scripts/find_platform @@ -0,0 +1,17 @@ +#!/bin/sh + +. inc/platform.sh + +echo \ + bsd=$bsd \ + cygwin=$cygwin \ + freebsd=$freebsd \ + linux=$linux \ + msys=$msys \ + netbsd=$netbsd \ + openbsd=$openbsd \ + osx=$osx \ + solaris=$solaris \ + win32=$win32 \ + +# vim: sw=4 et tw=0: diff --git a/vimpager b/vimpager index ed92df5..4e5e78f 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 From 8823a5b8f42eb558ae6e1cb4f401f72187d1297a Mon Sep 17 00:00:00 2001 From: Lucas Hoffmann Date: Sun, 24 Apr 2016 22:27:13 +0200 Subject: [PATCH 2/6] Move finding logic to platform file. --- inc/platform.sh | 32 ++++++++++++++++++++++++++++++++ scripts/find_platform | 2 ++ vimpager | 30 ++---------------------------- 3 files changed, 36 insertions(+), 28 deletions(-) diff --git a/inc/platform.sh b/inc/platform.sh index 9cc7aba..93f54f5 100644 --- a/inc/platform.sh +++ b/inc/platform.sh @@ -17,4 +17,36 @@ case $(uname -s) in *) 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 + # vim: sw=4 et tw=0: diff --git a/scripts/find_platform b/scripts/find_platform index 319157f..c9ae02f 100755 --- a/scripts/find_platform +++ b/scripts/find_platform @@ -13,5 +13,7 @@ echo \ osx=$osx \ solaris=$solaris \ win32=$win32 \ + AWK=$AWK \ + SED=$SED \ # vim: sw=4 et tw=0: diff --git a/vimpager b/vimpager index 4e5e78f..77a046a 100755 --- a/vimpager +++ b/vimpager @@ -627,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" "$@" } From 272c8e3ecb51bce9e845bcefed8891f987f7c887 Mon Sep 17 00:00:00 2001 From: Lucas Hoffmann Date: Sun, 24 Apr 2016 22:30:50 +0200 Subject: [PATCH 3/6] Move syntax checks to platform file. --- inc/platform.sh | 21 +++++++++++++++++++++ scripts/find_platform | 2 ++ vimpager | 26 ++++++-------------------- 3 files changed, 29 insertions(+), 20 deletions(-) diff --git a/inc/platform.sh b/inc/platform.sh index 93f54f5..84badae 100644 --- a/inc/platform.sh +++ b/inc/platform.sh @@ -49,4 +49,25 @@ else 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 index c9ae02f..86623cd 100755 --- a/scripts/find_platform +++ b/scripts/find_platform @@ -15,5 +15,7 @@ echo \ win32=$win32 \ AWK=$AWK \ SED=$SED \ + SED=$SED \ + HEAD_SYNTAX=$HEAD_SYNTAX \ # vim: sw=4 et tw=0: diff --git a/vimpager b/vimpager index 77a046a..0085adf 100755 --- a/vimpager +++ b/vimpager @@ -686,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 @@ -709,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 } From f5c4fefc00c68494735cb2344e8be2de4219b834 Mon Sep 17 00:00:00 2001 From: Lucas Hoffmann Date: Sun, 24 Apr 2016 23:34:13 +0200 Subject: [PATCH 4/6] Fix and improve configuring code in makefile and script. --- scripts/find_platform | 36 ++++++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/scripts/find_platform b/scripts/find_platform index 86623cd..82f5bac 100755 --- a/scripts/find_platform +++ b/scripts/find_platform @@ -2,20 +2,28 @@ . inc/platform.sh +# The backslash has to be put here 8 times: foctor two to get past the initial +# shell quoting, another factor two because it is used in the replacement +# string for sed, another factor two because the output of belows echo will be +# used in the replacement text in the sed call in the makefile. 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") + echo \ - bsd=$bsd \ - cygwin=$cygwin \ - freebsd=$freebsd \ - linux=$linux \ - msys=$msys \ - netbsd=$netbsd \ - openbsd=$openbsd \ - osx=$osx \ - solaris=$solaris \ - win32=$win32 \ - AWK=$AWK \ - SED=$SED \ - SED=$SED \ - HEAD_SYNTAX=$HEAD_SYNTAX \ + ${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} \ # vim: sw=4 et tw=0: From f8243171c40394858a3fd067a6a750990a6db5f6 Mon Sep 17 00:00:00 2001 From: Lucas Hoffmann Date: Mon, 25 Apr 2016 10:57:53 +0200 Subject: [PATCH 5/6] Improve escaping in find_platform. --- scripts/find_platform | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/scripts/find_platform b/scripts/find_platform index 82f5bac..b5ed12a 100755 --- a/scripts/find_platform +++ b/scripts/find_platform @@ -2,15 +2,15 @@ . inc/platform.sh -# The backslash has to be put here 8 times: foctor two to get past the initial +# 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, another factor two because the output of belows echo will be -# used in the replacement text in the sed call in the makefile. 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") +# 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} \ @@ -25,5 +25,6 @@ echo \ "AWK='$AWK'" \ "SED='$SED'" \ ${HEAD_SYNTAX:+HEAD_SYNTAX=$HEAD_SYNTAX} \ + | sed -e 's/\\/\\\\/g' -e 's/|/\|/g' # vim: sw=4 et tw=0: From a3070a3484e2ae6d3c25f445cd63fb2889f6362f Mon Sep 17 00:00:00 2001 From: Lucas Hoffmann Date: Tue, 14 Feb 2017 19:34:49 +0100 Subject: [PATCH 6/6] Simplify sed search patterns in makefile This simplifies some search patterns that are used in the makefile to copy sourced scripts into the "compiled" versions. The simplified patterns should be more robust to minor comment or quoting changes. --- Makefile | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index 90cdb9e..45c957f 100644 --- a/Makefile +++ b/Makefile @@ -46,11 +46,11 @@ 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 '/^\. .*inc\/platform.sh/{' \ -e 'r inc/platform.sh' \ -e d \ -e '}' \ @@ -79,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 '}' \ @@ -99,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 '}' \