aboutsummaryrefslogtreecommitdiff
path: root/bin/chn.mi5
blob: dfc1000cdda6222953188f52b5c8b9ee5582ad00 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# Repeat a command to filter input several times
self=chn

# Check arguments.
if [ "$#" -lt 2 ] ; then
    printf >&2 '%s: Need a count and a program name\n' "$self"
    exit 2
fi

# Shift off the repetition count.
c=$1
shift

# Check the repetition count looks sane. Zero is fine!
if [ "$c" -lt 0 ] ; then
    printf >&2 '%s: Nonsensical negative count\n' "$self"
    exit 2
fi

# If the count is zero, just run the input straight through!
if [ "$c" -eq 0 ] ; then
    cat
    exit
fi

<%
include(`include/mktd.m4')
%>

# Define and create input and output files
if=$td/if of=$td/of
touch -- "$if" "$of"

# Iterate through the count
while [ "${n=1}" -le "$c" ] ; do

    # Start a subshell so we can deal with FDs cleanly
    (
        # If this isn't the first iteration, our input comes from $if
        [ "$n" -eq 1 ] ||
            exec <"$if"

        # If this isn't the last iteration, our output goes to $of
        [ "$n" -eq "$c" ] ||
            exec >"$of"

        # Run the command with the descriptors above; if the command fails, the
        # subshell will exit, which will in turn exit the program
        "$@"
    ) || exit

    # Copy the output file over the input one
    cp -- "$of" "$if"

    # Increment the counter for the next while loop run
    n=$((n+1))
done