From 3770fe80fa49fe8ad6a3c3697b268a18b8c13eb1 Mon Sep 17 00:00:00 2001 Message-Id: <3770fe80fa49fe8ad6a3c3697b268a18b8c13eb1.1715726493.git.mdw@distorted.org.uk> From: Mark Wooding Date: Sat, 5 Feb 2005 10:33:00 +0000 Subject: [PATCH] New scripts. Organization: Straylight/Edgeware From: mdw --- Makefile | 7 +++-- buf | 18 +++++++++++++ buf.1 | 38 +++++++++++++++++++++++++++ create | 23 +++++++++++++++++ create.1 | 48 +++++++++++++++++++++++++++++++++++ debian/control | 16 +++++++++++- debian/inst | 6 +++++ inplace | 44 ++++++++++++++++++++++++++++++++ inplace.1 | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++ z.1 | 2 ++ 10 files changed, 268 insertions(+), 3 deletions(-) create mode 100755 buf create mode 100644 buf.1 create mode 100755 create create mode 100644 create.1 create mode 100755 inplace create mode 100644 inplace.1 diff --git a/Makefile b/Makefile index 514bab2..8137f86 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,9 @@ PACKAGE = nsict-utils VERSION = 1.1.1 -BINSCRIPTS = cdb-assign cdb-list check-sender unfwd splitconf z +BINSCRIPTS = \ + cdb-assign cdb-list check-sender unfwd \ + splitconf z buf create inplace SBINSCRIPTS = shadowfix SCRIPTS = $(BINSCRIPTS) $(SBINSCRIPTS) BINPROGS = not cdb-probe cdb-check-domain gorp locking if-mtu pause @@ -15,7 +17,8 @@ PERLLIBS = MdwOpt.pm LIBS = xtitle.so DISTMAN1 = \ not.1 z.1 cdb-assign.1 cdb-list.1 cdb-probe.1 cdb-check-domain.1 \ - gorp.1 unfwd.1 splitconf.1 locking.1 if-mtu.1 pause.1 + gorp.1 unfwd.1 splitconf.1 locking.1 if-mtu.1 pause.1 \ + buf.1 create.1 inplace.1 MAN1 = $(DISTMAN1) DISTMAN8 = qmail-checkspam.8 MAN8 = $(DISTMAN8) shadowfix.8 diff --git a/buf b/buf new file mode 100755 index 0000000..c95cf89 --- /dev/null +++ b/buf @@ -0,0 +1,18 @@ +#! /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 +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 +trap '' INT QUIT TERM HUP EXIT +rm -rf "$tmp" +exit $st diff --git a/buf.1 b/buf.1 new file mode 100644 index 0000000..0039a90 --- /dev/null +++ b/buf.1 @@ -0,0 +1,38 @@ +.de VS +.sp 1 +.RS +.nf +.ft B +.. +.de VE +.ft R +.fi +.RE +.sp 1 +.. +.TH buf 1 "5 February 2005" "Edgeware tools" +.SH NAME +buf \- buffer stdin to a file +.SH SYNOPSIS +.B buf +.I command +.RI [ arguments ...] +.SH DESCRIPTION +The +.B buf +program writes standard input to a temporary file. It then runs +.VS +.IR "command arguments " ... " stdinfile" +.VE +and exits with whatever exit status that exited with. +.PP +This is particularly useful for programs which want to seek their +standard input, for example PostScript viewers. A typical invokation +might be something like +.VS +man -Tps buf | buf gv +.VE +.SH BUGS +Doesn't exit correctly if subprogram was killed by a signal. +.SH AUTHOR +Mark Wooding (mdw@nsict.org). diff --git a/create b/create new file mode 100755 index 0000000..ae68817 --- /dev/null +++ b/create @@ -0,0 +1,23 @@ +#! /bin/sh + +set -e +case "$#:$1" in + 0:* | 1:-h | 1:--help) + echo "$0 FILE PROG ARGS" + echo " Run PROG with ARGS, writing its stdout to FILE if it succeeds." + echo " Clobbers FILE.new." + exit 0 + ;; + 1:*) + echo >&2 "$0 FILE PROG ARGS" + exit 1 + ;; +esac +file=$1; shift +set +e; "$@" >"$file.new"; st=$?; set -e +if [ $st -eq 0 ]; then + mv "$file.new" "$file" +else + rm "$file.new" +fi +exit $st diff --git a/create.1 b/create.1 new file mode 100644 index 0000000..8ede16a --- /dev/null +++ b/create.1 @@ -0,0 +1,48 @@ +.de VS +.sp 1 +.RS +.nf +.ft B +.. +.de VE +.ft R +.fi +.RE +.sp 1 +.. +.TH create 1 "5 February 2005" "Edgeware tools" +.SH NAME +create \- write new file safely +.SH SYNOPSIS +.B create +.I file +.I program +.RI [ arguments ...] +.SH DESCRIPTION +The +.B create +program +runs +.VS +.IR "program arguments " ... +.VE +redirecting standard output to +.IR file \c +.BR .new . +If +.I program +exits successfully, +.B create +will rename the newly-written +.IB file .new +to simply +.I file +and exit zero; otherwise it deletes +.IB file .new +and exits the same as +.I program +did. +.SH BUGS +Doesn't exit correctly if subprogram was killed by a signal. +.SH AUTHOR +Mark Wooding (mdw@nsict.org). diff --git a/debian/control b/debian/control index f928af9..271d1d4 100644 --- a/debian/control +++ b/debian/control @@ -10,7 +10,7 @@ Package: nsict-utils Architecture: all Section: utils Depends: mdwopt-perl, nsict-cdb, locking, qmail-checkspam, nsict-mail, - if-mtu, shadowfix, zz, gorp, splitconf, xtitle + if-mtu, shadowfix, zz, gorp, splitconf, xtitle, buf, create, inplace Description: Dummy package for convenience. Package: mdwopt-perl @@ -86,3 +86,17 @@ Depends: ${shlibs:Depends} Section: utils Description: Wait for a given time, or until a key is pressed. +Package: buf +Architecture: all +Section: utils +Description: Captures stdin in a temporary file and runs a command. + +Package: create +Architecture: all +Section: utils +Description: Simple script encapsulating atomic file update. + +Package: inplace +Architecture: all +Section: utils +Description: Update files in place safely. diff --git a/debian/inst b/debian/inst index 9793ede..9229323 100644 --- a/debian/inst +++ b/debian/inst @@ -1,4 +1,6 @@ MdwOpt.pm mdwopt-perl /usr/share/perl5 +buf buf /usr/bin +buf.1 buf /usr/share/man/man1 cdb-assign nsict-cdb /usr/bin cdb-assign.1 nsict-cdb /usr/share/man/man1 cdb-check-domain nsict-cdb /usr/bin @@ -9,10 +11,14 @@ cdb-probe nsict-cdb /usr/bin cdb-probe.1 nsict-cdb /usr/share/man/man1 check-sender nsict-mail /usr/bin check-sender.1 nsict-mail /usr/share/man/man1 +create create /usr/bin +create.1 create /usr/share/man/man1 gorp gorp /usr/bin gorp.1 gorp /usr/share/man/man1 if-mtu if-mtu /usr/sbin if-mtu.1 if-mtu /usr/share/man/man1 +inplace inplace /usr/bin +inplace.1 inplace /usr/share/man/man1 locking locking /usr/bin locking.1 locking /usr/share/man/man1 not nsict-mail /usr/bin diff --git a/inplace b/inplace new file mode 100755 index 0000000..5634b2c --- /dev/null +++ b/inplace @@ -0,0 +1,44 @@ +#! /bin/bash + +set -e +usage () { echo >&2 "$0 FILE ... -- PROG ARGS"; exit 1; } +case "$#:$1" in + 0:* | 1:-h | 1:--help) + echo "$0 FILE ... -- PROG ARGS" + echo " For each FILE, run PROG ARGS with stdin from FILE. If all is" + echo " good, replace each FILE with the corresponding output." + echo " Clobbers FILE.old and FILE.new." + exit 0 + ;; +esac +declare -a file +while :; do + [ $# -eq 0 ] && usage + f=$1; shift + [ "$f" = -- ] && break + file=("${file[@]}" "$f") +done +[ $# -eq 0 ] && usage + +cleanup_a () { + for f in "${file[@]}"; do + rm -f "$f.new" "$f.old" + done +} +trap cleanup_a EXIT INT QUIT TERM + +for f in "${file[@]}"; do "$@" <"$f" >"$f.new"; done +for f in "${file[@]}"; do cp "$f" "$f.old"; done + +cleanup_b () { + for f in "${file[@]}"; do + rm -f "$f.new" + mv "$f.old" "$f" + done +} +trap cleanup_b EXIT INT QUIT TERM + +for f in "${file[@]}"; do mv "$f.new" "$f"; done +trap '' EXIT INT QUIT TERM +for f in "${file[@]}"; do rm -f "$f.old"; done +exit 0 diff --git a/inplace.1 b/inplace.1 new file mode 100644 index 0000000..e4fb9ce --- /dev/null +++ b/inplace.1 @@ -0,0 +1,69 @@ +.de VS +.sp 1 +.RS +.nf +.ft B +.. +.de VE +.ft R +.fi +.RE +.sp 1 +.. +.TH inplace 1 "5 February 2005" "Edgeware tools" +.SH NAME +inplace \- update a file in-place +.SH SYNOPSIS +.B inplace +.I file +... +.B \-\- +.I program +.RI [ arguments ...] +.SH DESCRIPTION +The +.B inplace +program rewrites files in place using filters. For each +.I file +it runs +.VS +.IR "program arguments " ... +.VE +with standard input redirected from +.I file +and redirecting standard output to +.IR file \c +.BR .new . +If all these programs are successful, then each +.I file +is copied to +.IB file .old +and each +.IB file .new +renamed to +.IR file ; +the +.IB file .old +files are then deleted. If something went wrong, the +.IB file .new +files are all deleted and the original +.IR file s +are left unmodified. +.I program +exits successfully, +.B inplace +will rename the newly-written +.IB file .new +to simply +.I file +and exit zero; otherwise it deletes +.IB file .new +and exits the same as +.I program +did. +.SH BUGS +Doesn't exit correctly if subprogram was killed by a signal. The update +isn't completely perfect but it's very good; it can leave debris lying +around if strange things happen during the renaming. +.SH AUTHOR +Mark Wooding (mdw@nsict.org). diff --git a/z.1 b/z.1 index 8173170..0e3b196 100644 --- a/z.1 +++ b/z.1 @@ -30,5 +30,7 @@ Nonexistent files cause errors to be reported. The decompressed versions of files are always created with default owner, group and permissions (as set by the current euid, egid and umask settings). +.PP +It doesn't leave file extensions on the decompressed files. .SH AUTHOR Mark Wooding (mdw@nsict.org). -- [mdw]