From 8b628e9f2ad95491f8a5f5354fc4101c07b80514 Mon Sep 17 00:00:00 2001 Message-Id: <8b628e9f2ad95491f8a5f5354fc4101c07b80514.1715769027.git.mdw@distorted.org.uk> From: Mark Wooding Date: Thu, 23 Oct 2014 14:02:38 +0100 Subject: [PATCH] buf, buf.1: Allow the temporary file's extension to be set. Organization: Straylight/Edgeware From: Mark Wooding Some programs try to deduce things about their input files from their names, so accommodate them. --- buf | 27 ++++++++++++++++++--------- buf.1 | 16 +++++++++++++++- 2 files changed, 33 insertions(+), 10 deletions(-) diff --git a/buf b/buf index c95cf89..1bb1958 100755 --- a/buf +++ b/buf @@ -1,18 +1,27 @@ #! /bin/sh set -e -case "$#:$1" in - 0:* | 1:-h | 1:--help) - echo "Usage: $0 PROG ARGS..." - echo " Buffer stdin to temp file and pass to PROG with ARGS" - exit 0 - ;; -esac +ext="" +while :; do + case "$#:$1" in + 0:* | *:-h | *:--help) + echo "Usage: $0 [-e EXT] PROG ARGS..." + echo " Buffer stdin to temp file and pass to PROG with ARGS" + exit 0 + ;; + 1:-e) echo >&2 "$0: missing argument"; exit 1 ;; + *:-e) ext=$2; shift 2 ;; + *:-e*) ext=${1#-e}; shift ;; + *:--) shift; break ;; + *:-*) echo >&2 "$0: unknown option \`$1'"; exit 1 ;; + *) break ;; + esac +done tmp=${TMPDIR-/tmp}/buf-$$ mkdir "$tmp" trap 'rm -rf "$tmp"' INT QUIT TERM HUP EXIT -cat >"$tmp/buf" -set +e; "$@" "$tmp/buf"; st=$?; set -e +cat >"$tmp/buf$ext" +set +e; "$@" "$tmp/buf$ext"; st=$?; set -e trap '' INT QUIT TERM HUP EXIT rm -rf "$tmp" exit $st diff --git a/buf.1 b/buf.1 index 5dc4747..65b8a61 100644 --- a/buf.1 +++ b/buf.1 @@ -15,6 +15,8 @@ buf \- buffer stdin to a file .SH SYNOPSIS .B buf +.RB [ \-e +.IR ext ] .I command .RI [ arguments ...] .SH DESCRIPTION @@ -26,11 +28,23 @@ program writes standard input to a temporary file. It then runs .VE and exits with whatever exit status that exited with. .PP +Command line options: +.TP +.B "\-h" +Show very brief help. +.TP +.BI "\-e " ext +Arrange that the temporary file has the suffix +.IR ext . +No +.RB ` . ' +is included unless you provide one yourself. +.PP This is particularly useful for programs which want to seek their standard input, for example PostScript viewers. A typical invocation might be something like .VS -man \-Tps buf | buf gv +man \-Tps buf | buf \-e.ps gv .VE .SH BUGS Doesn't exit correctly if subprogram was killed by a signal. -- [mdw]