3 # SPDX-License-Identifier: BSD-2-Clause
5 # Copyright (c) 2018-2021 Gavin D. Howard and contributors.
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions are met:
10 # * Redistributions of source code must retain the above copyright notice, this
11 # list of conditions and the following disclaimer.
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.
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.
30 # This script is NOT meant to be run! It is meant to be sourced by other
33 # Reads and follows a link until it finds a real file. This is here because the
34 # readlink utility is not part of the POSIX standard. Sigh...
35 # @param f The link to find the original file for.
42 _readlink_d=$(dirname "$_readlink_f")
47 _readlink_lsout=$(ls -dl "$_readlink_f")
48 _readlink_link=$(printf '%s' "${_readlink_lsout#*$_readlink_arrow}")
50 while [ -z "${_readlink_lsout##*$_readlink_arrow*}" ]; do
51 _readlink_f="$_readlink_d/$_readlink_link"
52 _readlink_d=$(dirname "$_readlink_f")
53 _readlink_lsout=$(ls -dl "$_readlink_f")
54 _readlink_link=$(printf '%s' "${_readlink_lsout#*$_readlink_arrow}")
57 printf '%s' "${_readlink_f##*$_readlink_d/}"
60 # Quick function for exiting with an error.
61 # @param 1 A message to print.
62 # @param 2 The exit code to use.
65 if [ "$#" -ne 2 ]; then
66 printf 'Invalid number of args to err_exit\n'
74 # Check the return code on a test and exit with a fail if it's non-zero.
75 # @param d The calculator under test.
76 # @param err The return code.
77 # @param name The name of the test.
80 _checktest_retcode_d="$1"
83 _checktest_retcode_err="$1"
86 _checktest_retcode_name="$1"
89 if [ "$_checktest_retcode_err" -ne 0 ]; then
91 err_exit "$_checktest_retcode_d failed test '$_checktest_retcode_name' with error code $_checktest_retcode_err" 1
95 # Check the result of a test. First, it checks the error code using
96 # checktest_retcode(). Then it checks the output against the expected output
97 # and fails if it doesn't match.
98 # @param d The calculator under test.
99 # @param err The error code.
100 # @param name The name of the test.
101 # @param test_path The path to the test.
102 # @param results_name The path to the file with the expected result.
114 _checktest_test_path="$1"
117 _checktest_results_name="$1"
120 checktest_retcode "$_checktest_d" "$_checktest_err" "$_checktest_name"
122 _checktest_diff=$(diff "$_checktest_test_path" "$_checktest_results_name")
126 if [ "$_checktest_err" -ne 0 ]; then
128 printf '%s\n' "$_checktest_diff"
129 err_exit "$_checktest_d failed test $_checktest_name" 1
133 # Die. With a message.
134 # @param d The calculator under test.
135 # @param msg The message to print.
136 # @param name The name of the test.
137 # @param err The return code from the test.
152 _die_str=$(printf '\n%s %s on test:\n\n %s\n' "$_die_d" "$_die_msg" "$_die_name")
154 err_exit "$_die_str" "$_die_err"
157 # Check that a test did not crash and die if it did.
158 # @param d The calculator under test.
159 # @param error The error code.
160 # @param name The name of the test.
166 _checkcrash_error="$1"
169 _checkcrash_name="$1"
173 if [ "$_checkcrash_error" -gt 127 ]; then
174 die "$_checkcrash_d" "crashed ($_checkcrash_error)" \
175 "$_checkcrash_name" "$_checkcrash_error"
179 # Check that a test had an error or crash.
180 # @param d The calculator under test.
181 # @param error The error code.
182 # @param name The name of the test.
183 # @param out The file that the test results were output to.
184 # @param exebase The name of the executable.
190 _checkerrtest_error="$1"
193 _checkerrtest_name="$1"
196 _checkerrtest_out="$1"
199 _checkerrtest_exebase="$1"
202 checkcrash "$_checkerrtest_d" "$_checkerrtest_error" "$_checkerrtest_name"
204 if [ "$_checkerrtest_error" -eq 0 ]; then
205 die "$_checkerrtest_d" "returned no error" "$_checkerrtest_name" 127
208 # This is to check for memory errors with Valgrind, which is told to return
209 # 100 on memory errors.
210 if [ "$_checkerrtest_error" -eq 100 ]; then
212 _checkerrtest_output=$(cat "$_checkerrtest_out")
213 _checkerrtest_fatal_error="Fatal error"
215 if [ "${_checkerrtest_output##*$_checkerrtest_fatal_error*}" ]; then
216 printf "%s\n" "$_checkerrtest_output"
217 die "$_checkerrtest_d" "had memory errors on a non-fatal error" \
218 "$_checkerrtest_name" "$_checkerrtest_error"
222 if [ ! -s "$_checkerrtest_out" ]; then
223 die "$_checkerrtest_d" "produced no error message" "$_checkerrtest_name" "$_checkerrtest_error"
226 # To display error messages, uncomment this line. This is useful when
228 #cat "$_checkerrtest_out"
231 # Replace a substring in a string with another. This function is the *real*
232 # workhorse behind configure.sh's generation of a Makefile.
234 # This function uses a sed call that uses exclamation points `!` as delimiters.
235 # As a result, needle can never contain an exclamation point. Oh well.
237 # @param str The string that will have any of the needle replaced by
239 # @param needle The needle to replace in str with replacement.
240 # @param replacement The replacement for needle in str.
241 substring_replace() {
243 _substring_replace_str="$1"
246 _substring_replace_needle="$1"
249 _substring_replace_replacement="$1"
252 _substring_replace_result=$(printf '%s\n' "$_substring_replace_str" | \
253 sed -e "s!$_substring_replace_needle!$_substring_replace_replacement!g")
255 printf '%s' "$_substring_replace_result"
258 # Generates an NLS path based on the locale and executable name.
260 # This is a monstrosity for a reason.
262 # @param nlspath The $NLSPATH
263 # @param locale The locale.
264 # @param execname The name of the executable.
267 _gen_nlspath_nlspath="$1"
270 _gen_nlspath_locale="$1"
273 _gen_nlspath_execname="$1"
276 # Split the locale into its modifier and other parts.
277 _gen_nlspath_char="@"
278 _gen_nlspath_modifier="${_gen_nlspath_locale#*$_gen_nlspath_char}"
279 _gen_nlspath_tmplocale="${_gen_nlspath_locale%%$_gen_nlspath_char*}"
281 # Split the locale into charset and other parts.
282 _gen_nlspath_char="."
283 _gen_nlspath_charset="${_gen_nlspath_tmplocale#*$_gen_nlspath_char}"
284 _gen_nlspath_tmplocale="${_gen_nlspath_tmplocale%%$_gen_nlspath_char*}"
286 # Check for an empty charset.
287 if [ "$_gen_nlspath_charset" = "$_gen_nlspath_tmplocale" ]; then
288 _gen_nlspath_charset=""
291 # Split the locale into territory and language.
292 _gen_nlspath_char="_"
293 _gen_nlspath_territory="${_gen_nlspath_tmplocale#*$_gen_nlspath_char}"
294 _gen_nlspath_language="${_gen_nlspath_tmplocale%%$_gen_nlspath_char*}"
296 # Check for empty territory and language.
297 if [ "$_gen_nlspath_territory" = "$_gen_nlspath_tmplocale" ]; then
298 _gen_nlspath_territory=""
301 if [ "$_gen_nlspath_language" = "$_gen_nlspath_tmplocale" ]; then
302 _gen_nlspath_language=""
305 # Prepare to replace the format specifiers. This is done by wrapping the in
306 # pipe characters. It just makes it easier to split them later.
307 _gen_nlspath_needles="%%:%L:%N:%l:%t:%c"
309 _gen_nlspath_needles=$(printf '%s' "$_gen_nlspath_needles" | tr ':' '\n')
311 for _gen_nlspath_i in $_gen_nlspath_needles; do
312 _gen_nlspath_nlspath=$(substring_replace "$_gen_nlspath_nlspath" "$_gen_nlspath_i" "|$_gen_nlspath_i|")
315 # Replace all the format specifiers.
316 _gen_nlspath_nlspath=$(substring_replace "$_gen_nlspath_nlspath" "%%" "%")
317 _gen_nlspath_nlspath=$(substring_replace "$_gen_nlspath_nlspath" "%L" "$_gen_nlspath_locale")
318 _gen_nlspath_nlspath=$(substring_replace "$_gen_nlspath_nlspath" "%N" "$_gen_nlspath_execname")
319 _gen_nlspath_nlspath=$(substring_replace "$_gen_nlspath_nlspath" "%l" "$_gen_nlspath_language")
320 _gen_nlspath_nlspath=$(substring_replace "$_gen_nlspath_nlspath" "%t" "$_gen_nlspath_territory")
321 _gen_nlspath_nlspath=$(substring_replace "$_gen_nlspath_nlspath" "%c" "$_gen_nlspath_charset")
323 # Get rid of pipe characters.
324 _gen_nlspath_nlspath=$(printf '%s' "$_gen_nlspath_nlspath" | tr -d '|')
327 printf '%s' "$_gen_nlspath_nlspath"