summaryrefslogtreecommitdiff
path: root/vcat
blob: 0970664fccc1b468b62966f0b67540c71efac841 (plain)
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81

#!/bin/sh -u
#
#  vcat -- Like "cat", but print filenames before file contents
#
#  Copyright (c) 2018 Samuel Lidén Borell <samuel@kodafritt.se>
#
#  Permission is hereby granted, free of charge, to any person obtaining a copy
#  of this software and associated documentation files (the "Software"), to deal
#  in the Software without restriction, including without limitation the rights
#  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#  copies of the Software, and to permit persons to whom the Software is
#  furnished to do so, subject to the following conditions:
#
#  The above copyright notice and this permission notice shall be included in
#  all copies or substantial portions of the Software.
#
#  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
#  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
#  THE SOFTWARE.
#

prefix="---- "
suffix=" ----"
newlines='\n'

print_help() {
   cat <<EOF
usage: $0 [OPTIONS...] FILENAMES...

Prints the contents of the given files similar to "cat", but additionally
prints the filename of the file being printed first.

Options:
    --help                  Show this help.
    -l COUNT                Newlines to print after each file. Default is 1
    -p, --prefix=STRING     Prefix string before filename. Default is "---- "
    -s, --suffix=STRING     Suffix string after filename. Default is " ----"

Note: Options must come before filenames. To print filenames starting
with a "-", delimit the options and filenames with --

EOF
}

process_opts=1
while [ $# != 0 ]; do
    is_option=0
    if [ $process_opts = 1 ]; then
        is_option=1
        case "$1" in
            --) process_opts=0;;
            --help) print_help; exit;;
            --prefix=*) prefix="${1#--prefix=}";;
            --suffix=*) suffix="${1#--suffix=}";;
            -p) shift; prefix="$1";;
            -s) shift; suffix="$1";;
            -l)
                shift;
                count="$1"
                newlines=''
                while [ "$count" -gt 0 ]; do
                    newlines="$newlines\n"
                    count=$((count - 1))
                done
                ;;
            -*) echo "invalid option: $1"; exit 2;;
            *)
                is_option=0;;
        esac
    fi
    if [ $is_option = 0 ]; then
        printf "%s\n" "$prefix$1$suffix"
        cat "$1"
        printf "$newlines"
    fi
    shift
done