-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen_evil
executable file
·97 lines (89 loc) · 2.39 KB
/
gen_evil
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
#! /bin/sh
# Generate a mean test file for -c and -w modes.
set -e
trap 'test $? = 0 || echo "$0 failed!" >& 2' 0
mode=large
while getopts mts opt
do
case $opt in
s) mode=selftest;;
m) mode=mini;;
t) mode=term;;
*) false || exit
esac
done
shift `expr $OPTIND - 1 || :`
# Retrieve list of characters which needs encoding from the source file.
evil=`grep -F 'wse[]= {"' diffprep.c`
evil=${evil#*'"'}
evil=${evil%'"'*}
test -n "$evil"
# Convert the characters into strings prefixed by a colon.
evil=`printf '%s\n' "$evil" | sed 's/\\\\/:&/g'`
# Add a couple of strings consisting of a different number of SPACE characters.
n=5
while test $n -le 8
do
evil=$evil:`printf '%*s' $n '' | tr -c " " " "`:; evil=${evil%?}
n=`expr $n + 1`
done
# Expand octal escape sequences and convert into strings terminated by a colon
# where the first string is empty.
evil=`printf "$evil:"`
# Replace leading empty string with one single not-evil string.
evil=x$evil
# $1: (possibly empty) sequence of ":"-termnated strings to permute.
# $2: Prefix to prepend to every permutation.
# $3: Suffix to add after every permutation.
permute() {
local first prefix suffix head
test -z "$1" && return
first=${1%%:*}; prefix=; suffix=${1#"$first:"}
if test -z "$suffix"
then
printf %s "$2$first$3"
return
fi
# Emit permute($prefix) || $first || permute($suffix) where "||" means
# concatenation, then moving the first string of $suffix to the end of
# $prefix, until $suffix has become empty at the end of the loop.
while :
do
permute "$prefix" "$2" "$3"
printf %s "$2$first$3"
permute "$suffix" "$2" "$3"
head=${suffix%%:*}
test x"$head" != x"$suffix" || break
suffix=${suffix#"$head:"}
prefix=$prefix$head:
done
}
nl=`printf '\n:'`; nl=${nl%?}
case $mode in
selftest) permute 1:2:3:4: "$nl";;
mini)
# Just print all "evil" strings in two variants prefixed by an
# index number: The string alone and the string prefixed by 7
# spaces. This should trigger errors in the easy cases.
i=1
spc=`printf '%*s' $n '' | tr -c " " " "`:; spc=${evil%?}
while :
do
cur=${evil%%:*}
test x"$cur" != x"$evil" || break
evil=${evil#"$cur:"}
printf %s%s%s%s%s \
"p${i}:" "$cur" "s${i}:" "$spc" "$cur"
i=`expr $i + 1`
done
;;
term)
permute 'x: : :$:' "" "$nl"
;;
large)
# Print all possible permutations of the evil strings (and a
# single good one).
permute "$evil"
;;
*) false || exit
esac