-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathv
More file actions
166 lines (158 loc) · 3.36 KB
/
v
File metadata and controls
166 lines (158 loc) · 3.36 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# $Id: v,v 1.10 2011/06/28 12:16:19 phil Exp $
function v {
usage () {
echo "usage: s=file v [-dhnV] [-l lines]" 1>&2
echo " v [-dhnV] [-l lines] file ..." 1>&2
}
myprint () {
typeset len=$(wc -l "$1" | awk '{print $1}')
if [ "$len" -gt "$max" ]
then
if [ $linenr -eq 1 ]
then
if [ $qp -eq 1 ]
then
qpc -d "$1" | less -N
else
less -N "$1"
fi
else
if [ $qp -eq 1 ]
then
qpc -d "$1" | less
else
less "$1"
fi
fi
else
if [ $linenr -eq 1 ]
then
if [ $qp -eq 1 ]
then
qpc -d "$1" | cat -n
else
cat -n "$1"
fi
else
if [ $qp -eq 1 ]
then
qpc -d "$1" | cat
else
cat "$1"
fi
fi
fi
}
typeset max=$(($LINES * 3 / 4))
typeset linenr=0
typeset qp=0
typeset id='$Id: v,v 1.10 2011/06/28 12:16:19 phil Exp $'
# I'm doing this by hand and not with getopt(1)
# because file arguments could be filenames with spaces.
# With getopt(1) these filenames will not survive intact
# after the getopt(1) call.
while [[ $# > 0 && "$1" = \-* ]]
do
typeset op=$1
op=`echo "X$op" | sed -e 's/^X-//'`
if [[ "$op" != +([hndVl0-9]) ]]
then
usage
unset -f usage myprint
return 1
fi
while [[ "$op" = +([hndVl0-9]) ]]
do
if [[ "$op" = h* ]]
then
echo "Prints files with cat or less depending" 1>&2
echo "on the number of lines." 1>&2
echo "Default: use cat if #lines <= 3/4 * \$LINES" 1>&2
echo 1>&2
echo "Several files can be given as the last" 1>&2
echo "commandline arguments or one file can be" 1>&2
echo "specified in the variable s." 1>&2
echo "Only one of both variants is used." 1>&2
echo "Commandline arguments take precedence over \$s." 1>&2
echo "For example, both commands print the same file:" 1>&2
echo " $ v ~/notes/openbsd" 1>&2
echo " $ s=~/notes/openbsd v" 1>&2
echo 1>&2
usage
echo 1>&2
echo "options:" 1>&2
echo " -h print this help" 1>&2
echo " -d decode quoted-printables" 1>&2
echo " -l #lines number of lines when to use less" 1>&2
echo " -n print line numbers" 1>&2
echo " -V prints version and exits" 1>&2
unset -f usage myprint
return 0
elif [[ "$op" = n* ]]
then
linenr=1
op=`echo "$op" | sed -e 's/^n//'`
elif [[ "$op" = d* ]]
then
qp=1
op=`echo "$op" | sed -e 's/^d//'`
elif [[ "$op" = l* ]]
then
typeset ltmp
if [[ "$op" = "l" ]]
then
shift
ltmp="$1"
op=`echo "$op" | sed -e 's/^l//'`
else
ltmp=$(echo "$op" | sed -E 's/^l[[:space:]]*//')
op=$(echo "$op" | sed -E 's/^l[[:space:]]*[[:digit:]]*//')
fi
if [[ "$ltmp" != +([0-9]) ]]
then
usage
unset -f usage myprint
return 1
fi
max="$ltmp"
elif [[ "$op" = V* ]]
then
echo "$id" | sed -e 's/^.Id://' -e 's/\$$//'
unset -f usage myprint
return 0
else
usage
unset -f usage myprint
return 1
fi
done
shift
done
if [ $# -gt 0 ]
then
i=0
while [ $# -gt 0 ]
do
if [ -r "$1" ]
then
myprint "$1"
else
echo "v: $1: No such file or directory" 1>&2
fi
shift
done
elif [ "$s" ]
then
if [ -r "$s" ]
then
myprint "$s"
else
echo "v: $s: No such file or directory" 1>&2
fi
else
usage
unset -f usage myprint
return 1
fi
unset -f usage myprint
}