blob: e73613240337df05ea0b1da79d97ca9bd3a4176e [file] [log] [blame]
Gavin Howard7c715d62019-02-20 10:41:08 -07001#! /bin/sh
2#
Gavin Howard7345cb92019-04-08 14:13:43 -06003# Copyright (c) 2018-2019 Gavin D. Howard and contributors.
Gavin Howard7c715d62019-02-20 10:41:08 -07004#
Gavin Howard7345cb92019-04-08 14:13:43 -06005# All rights reserved.
Gavin Howard7c715d62019-02-20 10:41:08 -07006#
Gavin Howard7345cb92019-04-08 14:13:43 -06007# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions are met:
9#
10# * Redistributions of source code must retain the above copyright notice, this
11# list of conditions and the following disclaimer.
12#
13# * Redistributions in binary form must reproduce the above copyright notice,
14# this list of conditions and the following disclaimer in the documentation
15# and/or other materials provided with the distribution.
16#
17# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27# POSSIBILITY OF SUCH DAMAGE.
Gavin Howard7c715d62019-02-20 10:41:08 -070028#
29
Gavin Howard50080252019-02-20 15:27:43 -070030readlink() {
31
Gavin Howardaea5bdb2019-04-09 09:01:15 -060032 local f="$1"
Gavin Howard50080252019-02-20 15:27:43 -070033 shift
34
Gavin Howardaea5bdb2019-04-09 09:01:15 -060035 local arrow="-> "
36 local d=$(dirname "$f")
Gavin Howard50080252019-02-20 15:27:43 -070037
Gavin Howardaea5bdb2019-04-09 09:01:15 -060038 local lsout=""
39 local link=""
40
41 lsout=$(ls -dl "$f")
42 link=$(printf '%s' "${lsout#*$arrow}")
43
44 while [ -z "${lsout##*$arrow*}" ]; do
45 cd "$d"
46 f="$link"
47 d=$(dirname "$f")
48 lsout=$(ls -dl "$f")
49 link=$(printf '%s' "${lsout#*$arrow}")
50 done
51
52 printf '%s' "${f##*$d/}"
Gavin Howard50080252019-02-20 15:27:43 -070053}
54
Gavin Howardf680e352019-04-01 13:28:48 -060055removeext() {
56
57 local name="$1"
58 shift
59
Gavin Howard013a2302019-04-01 15:00:39 -060060 printf "${name%.*}"
Gavin Howardf680e352019-04-01 13:28:48 -060061}
62
Gavin Howard7c715d62019-02-20 10:41:08 -070063die() {
64
65 local d="$1"
66 shift
67
68 local msg="$1"
69 shift
70
71 local name="$1"
72 shift
73
74 local err="$1"
75 shift
76
77 printf '\n'
78 printf '%s %s on test:\n' "$d" "$msg"
79 printf '\n'
80 printf ' %s\n' "$name"
81 printf '\n'
82 printf 'exiting...\n'
83 exit "$err"
84}
85
86checkcrash() {
87
88 local error="$1"
89 shift
90
91 local name="$1"
92 shift
93
94 if [ "$error" -gt 127 ]; then
95 die "$d" "crashed" "$name" "$error"
96 fi
97}
98
99checktest()
100{
101 local error="$1"
102 shift
103
104 local name="$1"
105 shift
106
107 local out="$1"
108 shift
109
110 local exebase="$1"
111 shift
112
113 checkcrash "$error" "$name"
114
115 if [ "$error" -eq 0 ]; then
116 die "$d" "returned no error" "$name" 127
117 fi
118
119 if [ "$error" -eq 100 ]; then
120
121 output=$(cat "$out")
122 fatal_error="Fatal error"
123
124 if [ "${output##*$fatal_error*}" ]; then
125 printf "%s\n" "$output"
126 die "$d" "had memory errors on a non-fatal error" "$name" "$error"
127 fi
128 fi
129
130 if [ ! -s "$out" ]; then
131 die "$d" "produced no error message" "$name" "$error"
132 fi
133
134 # Display the error messages if not directly running exe.
135 # This allows the script to print valgrind output.
136 if [ "$exebase" != "bc" -a "$exebase" != "dc" ]; then
137 cat "$out"
138 fi
139}
Gavin Howard2425bf22019-04-08 17:05:45 -0600140
141substring_replace() {
142
Gavin Howardaea5bdb2019-04-09 09:01:15 -0600143 local str="$1"
144 shift
Gavin Howard2425bf22019-04-08 17:05:45 -0600145
Gavin Howardaea5bdb2019-04-09 09:01:15 -0600146 local needle="$1"
147 shift
148
149 local replacement="$1"
150 shift
Gavin Howard2425bf22019-04-08 17:05:45 -0600151
152 result=$(printf '%s' "$str" | sed -e "s!$needle!$replacement!g")
153
154 printf '%s\n' "$result"
155}
Gavin Howardaea5bdb2019-04-09 09:01:15 -0600156
157gen_nlspath() {
158
159 local nlspath="$1"
160 shift
161
162 local locale="$1"
163 shift
164
165 local execname="$1"
166 shift
167
168 local char="@"
169 local modifier="${locale#*$char}"
170 local tmplocale="${locale%%$char*}"
171
172 char="."
173 local charset="${tmplocale#*$char}"
174 tmplocale="${tmplocale%%$char*}"
175
176 if [ "$charset" = "$tmplocale" ]; then
177 charset=""
178 fi
179
180 char="_"
181 local territory="${tmplocale#*$char}"
182 local language="${tmplocale%%$char*}"
183
184 if [ "$territory" = "$tmplocale" ]; then
185 territory=""
186 fi
187
188 if [ "$language" = "$tmplocale" ]; then
189 language=""
190 fi
191
192 local needles="%%:%L:%N:%l:%t:%c"
193
194 needles=$(printf '%s' "$needles" | tr ':' '\n')
195
196 for i in $needles; do
197 nlspath=$(substring_replace "$nlspath" "$i" "\n$i\n")
198 done
199
200 nlspath=$(substring_replace "$nlspath" "%%" "%")
201 nlspath=$(substring_replace "$nlspath" "%L" "$locale")
202 nlspath=$(substring_replace "$nlspath" "%N" "$execname")
203 nlspath=$(substring_replace "$nlspath" "%l" "$language")
204 nlspath=$(substring_replace "$nlspath" "%t" "$territory")
205 nlspath=$(substring_replace "$nlspath" "%c" "$charset")
206
207 nlspath=$(printf '%s' "$nlspath" | tr -d '\n')
208
209 printf '%s\n' "$nlspath"
210}