File: common.sh

package info (click to toggle)
mmh 0.4-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 4,976 kB
  • sloc: ansic: 26,132; sh: 6,871; makefile: 680; awk: 74
file content (123 lines) | stat: -rw-r--r-- 2,259 bytes parent folder | download | duplicates (2)
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
# Common helper routines for test shell scripts
# -- intended to be sourced by them

tmpfile="`mktemp`"
trap '
	rm -f "$tmpfile"
	exit "$failed"
' 0 1 2 15
failed=0


test_skip()
{
	echo "$0: $1"
	failed=120
	exit
}

# portable implementation of 'which' utility
findprog()
{
	FOUND=
	PROG="$1"
	IFS_SAVE="$IFS"
	IFS=:
	for D in $PATH; do
		if [ -z "$D" ]; then
			D=.
		fi
		if [ -f "$D/$PROG" ] && [ -x "$D/$PROG" ]; then
			printf '%s\n' "$D/$PROG"
			break
		fi
	done
	IFS="$IFS_SAVE"
}

require_prog()
{
	if [ -z "$(findprog $1)" ]; then
		test_skip "missing $1"
	fi
}

require_locale()
{
	for locale in "$@"; do
		if locale -a | grep -i "$locale" >/dev/null; then
			return
		fi
	done
	test_skip "no suitable locale available"
}

# Do a best guess at FQDN
mh_hostname()
{
	hostname -f 2>/dev/null || uname -n
}

# Some stuff for doing silly progress indicators
progress_update()
{
	test -t 1 || return 0   # suppress progress meter if non-interactive
	this="$1"
	first="$2"
	last="$3"
	range="$(expr $last - $first ||:)"
	prog="$(expr $this - $first ||:)"
	# this automatically rounds to nearest integer
	perc="$(expr 100 \* $prog / $range ||:)"
	# note \r so next update will overwrite
	printf "%3d%%\r" $perc
}

progress_done()
{
	test -t 1 || return 0   # suppress progress meter if non-interactive
	printf "100%%\n"
}


#### Replace generated Content-ID headers with static value
replace_contentid()
{
	sed "/^Content-ID/s/:.*/: <TESTID>/" "$@"
}


#### Filter that squeezes blank lines, partially emulating GNU cat -s,
#### but sufficient for our purpose.
#### From http://www-rohan.sdsu.edu/doc/sed.html, compiled by Eric Pement.
squeeze_lines()
{
	sed '/^$/N;/\n$/D'
}

#### Filter that converts non-breakable space U+00A0 to an ASCII space.
prepare_space()
{
	sed 's/'"`printf '\\302\\240'`"'/ /g'
}


# first argument:	command line to run
# second argument:	"normspace" to normalize the whitespace
# stdin:		expected output
runandcheck()
{
	if [ "$2" = "normspace" ]; then
		eval "$1" 2>&1 | prepare_space >"$tmpfile"
		diff="`prepare_space | diff -ub - "$tmpfile" || :`"
	else
		eval "$1" >"$tmpfile" 2>&1
		diff="`diff -u - "$tmpfile"`"
	fi

	if [ "$diff" ]; then
		echo "$0: $1 failed"
		echo "$diff"
		failed=`expr "${failed:-0}" + 1`
	fi
}