blob: a6cf1c0c63f9d2249626af4bcec7dcb65107c59a [file] [log] [blame]
Gavin Howard7c715d62019-02-20 10:41:08 -07001#! /bin/sh
2#
Gavin Howard29e00ba2020-06-30 09:25:21 -06003# SPDX-License-Identifier: BSD-2-Clause
4#
Zach van Rijn6d2cf3f2020-01-14 22:05:02 +00005# Copyright (c) 2018-2020 Gavin D. Howard and contributors.
Gavin Howard7c715d62019-02-20 10:41:08 -07006#
Gavin Howard7345cb92019-04-08 14:13:43 -06007# All rights reserved.
Gavin Howard7c715d62019-02-20 10:41:08 -07008#
Gavin Howard7345cb92019-04-08 14:13:43 -06009# Redistribution and use in source and binary forms, with or without
10# modification, are permitted provided that the following conditions are met:
11#
12# * Redistributions of source code must retain the above copyright notice, this
13# list of conditions and the following disclaimer.
14#
15# * Redistributions in binary form must reproduce the above copyright notice,
16# this list of conditions and the following disclaimer in the documentation
17# and/or other materials provided with the distribution.
18#
19# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29# POSSIBILITY OF SUCH DAMAGE.
Gavin Howard7c715d62019-02-20 10:41:08 -070030#
31
Gavin Howard50080252019-02-20 15:27:43 -070032readlink() {
33
Gavin Howard62421e92019-04-17 12:56:30 -060034 _readlink_f="$1"
Gavin Howard50080252019-02-20 15:27:43 -070035 shift
36
Gavin Howard62421e92019-04-17 12:56:30 -060037 _readlink_arrow="-> "
38 _readlink_d=$(dirname "$_readlink_f")
Gavin Howard50080252019-02-20 15:27:43 -070039
Gavin Howard62421e92019-04-17 12:56:30 -060040 _readlink_lsout=""
41 _readlink_link=""
Gavin Howardaea5bdb2019-04-09 09:01:15 -060042
Gavin Howard62421e92019-04-17 12:56:30 -060043 _readlink_lsout=$(ls -dl "$_readlink_f")
44 _readlink_link=$(printf '%s' "${_readlink_lsout#*$_readlink_arrow}")
Gavin Howardaea5bdb2019-04-09 09:01:15 -060045
Gavin Howard62421e92019-04-17 12:56:30 -060046 while [ -z "${_readlink_lsout##*$_readlink_arrow*}" ]; do
47 _readlink_f="$_readlink_d/$_readlink_link"
48 _readlink_d=$(dirname "$_readlink_f")
49 _readlink_lsout=$(ls -dl "$_readlink_f")
50 _readlink_link=$(printf '%s' "${_readlink_lsout#*$_readlink_arrow}")
Gavin Howardaea5bdb2019-04-09 09:01:15 -060051 done
52
Gavin Howard62421e92019-04-17 12:56:30 -060053 printf '%s' "${_readlink_f##*$_readlink_d/}"
Gavin Howard50080252019-02-20 15:27:43 -070054}
55
Gavin Howard8237f802019-04-11 12:23:03 -060056err_exit() {
57
58 if [ "$#" -ne 2 ]; then
59 printf 'Invalid number of args to err_exit\n'
60 exit 1
61 fi
62
63 printf '%s\n' "$1"
64 printf '\nexiting...\n'
65 exit "$2"
66}
67
Gavin Howard7c715d62019-02-20 10:41:08 -070068die() {
69
Gavin Howard62421e92019-04-17 12:56:30 -060070 _die_d="$1"
Gavin Howard7c715d62019-02-20 10:41:08 -070071 shift
72
Gavin Howard62421e92019-04-17 12:56:30 -060073 _die_msg="$1"
Gavin Howard7c715d62019-02-20 10:41:08 -070074 shift
75
Gavin Howard62421e92019-04-17 12:56:30 -060076 _die_name="$1"
Gavin Howard7c715d62019-02-20 10:41:08 -070077 shift
78
Gavin Howard62421e92019-04-17 12:56:30 -060079 _die_err="$1"
Gavin Howard7c715d62019-02-20 10:41:08 -070080 shift
81
Gavin Howard62421e92019-04-17 12:56:30 -060082 _die_str=$(printf '\n%s %s on test:\n\n %s\n' "$_die_d" "$_die_msg" "$_die_name")
Gavin Howard8237f802019-04-11 12:23:03 -060083
Gavin Howard62421e92019-04-17 12:56:30 -060084 err_exit "$_die_str" "$_die_err"
Gavin Howard7c715d62019-02-20 10:41:08 -070085}
86
87checkcrash() {
88
Gavin Howard62421e92019-04-17 12:56:30 -060089 _checkcrash_d="$1"
Gavin Howard7c715d62019-02-20 10:41:08 -070090 shift
91
Gavin Howard62421e92019-04-17 12:56:30 -060092 _checkcrash_error="$1"
Gavin Howard7c715d62019-02-20 10:41:08 -070093 shift
94
Gavin Howard62421e92019-04-17 12:56:30 -060095 _checkcrash_name="$1"
96 shift
97
98 if [ "$_checkcrash_error" -gt 127 ]; then
99 die "$_checkcrash_d" "crashed ($_checkcrash_error)" \
100 "$_checkcrash_name" "$_checkcrash_error"
Gavin Howard7c715d62019-02-20 10:41:08 -0700101 fi
102}
103
104checktest()
105{
Gavin Howard62421e92019-04-17 12:56:30 -0600106 _checktest_d="$1"
Gavin Howard7c715d62019-02-20 10:41:08 -0700107 shift
108
Gavin Howard62421e92019-04-17 12:56:30 -0600109 _checktest_error="$1"
Gavin Howard7c715d62019-02-20 10:41:08 -0700110 shift
111
Gavin Howard62421e92019-04-17 12:56:30 -0600112 _checktest_name="$1"
Gavin Howard7c715d62019-02-20 10:41:08 -0700113 shift
114
Gavin Howard62421e92019-04-17 12:56:30 -0600115 _checktest_out="$1"
Gavin Howard7c715d62019-02-20 10:41:08 -0700116 shift
117
Gavin Howard62421e92019-04-17 12:56:30 -0600118 _checktest_exebase="$1"
119 shift
Gavin Howard7c715d62019-02-20 10:41:08 -0700120
Gavin Howard62421e92019-04-17 12:56:30 -0600121 checkcrash "$_checktest_d" "$_checktest_error" "$_checktest_name"
122
123 if [ "$_checktest_error" -eq 0 ]; then
124 die "$_checktest_d" "returned no error" "$_checktest_name" 127
Gavin Howard7c715d62019-02-20 10:41:08 -0700125 fi
126
Gavin Howard62421e92019-04-17 12:56:30 -0600127 if [ "$_checktest_error" -eq 100 ]; then
Gavin Howard7c715d62019-02-20 10:41:08 -0700128
Gavin Howard62421e92019-04-17 12:56:30 -0600129 _checktest_output=$(cat "$_checktest_out")
130 _checktest_fatal_error="Fatal error"
Gavin Howard7c715d62019-02-20 10:41:08 -0700131
Gavin Howard62421e92019-04-17 12:56:30 -0600132 if [ "${_checktest_output##*$_checktest_fatal_error*}" ]; then
133 printf "%s\n" "$_checktest_output"
134 die "$_checktest_d" "had memory errors on a non-fatal error" \
135 "$_checktest_name" "$_checktest_error"
Gavin Howard7c715d62019-02-20 10:41:08 -0700136 fi
137 fi
138
Gavin Howard62421e92019-04-17 12:56:30 -0600139 if [ ! -s "$_checktest_out" ]; then
140 die "$_checktest_d" "produced no error message" "$_checktest_name" "$_checktest_error"
Gavin Howard7c715d62019-02-20 10:41:08 -0700141 fi
142
143 # Display the error messages if not directly running exe.
144 # This allows the script to print valgrind output.
Gavin Howard62421e92019-04-17 12:56:30 -0600145 if [ "$_checktest_exebase" != "bc" -a "$_checktest_exebase" != "dc" ]; then
146 cat "$_checktest_out"
Gavin Howard7c715d62019-02-20 10:41:08 -0700147 fi
148}
Gavin Howard2425bf22019-04-08 17:05:45 -0600149
150substring_replace() {
151
Gavin Howard62421e92019-04-17 12:56:30 -0600152 _substring_replace_str="$1"
Gavin Howardaea5bdb2019-04-09 09:01:15 -0600153 shift
Gavin Howard2425bf22019-04-08 17:05:45 -0600154
Gavin Howard62421e92019-04-17 12:56:30 -0600155 _substring_replace_needle="$1"
Gavin Howardaea5bdb2019-04-09 09:01:15 -0600156 shift
157
Gavin Howard62421e92019-04-17 12:56:30 -0600158 _substring_replace_replacement="$1"
Gavin Howardaea5bdb2019-04-09 09:01:15 -0600159 shift
Gavin Howard2425bf22019-04-08 17:05:45 -0600160
Zach van Rijn579518c2020-01-14 21:56:45 +0000161 _substring_replace_result=$(printf '%s\n' "$_substring_replace_str" | \
Gavin Howard62421e92019-04-17 12:56:30 -0600162 sed -e "s!$_substring_replace_needle!$_substring_replace_replacement!g")
Gavin Howard2425bf22019-04-08 17:05:45 -0600163
Zach van Rijn579518c2020-01-14 21:56:45 +0000164 printf '%s' "$_substring_replace_result"
Gavin Howard2425bf22019-04-08 17:05:45 -0600165}
Gavin Howardaea5bdb2019-04-09 09:01:15 -0600166
167gen_nlspath() {
168
Gavin Howard62421e92019-04-17 12:56:30 -0600169 _gen_nlspath_nlspath="$1"
Gavin Howardaea5bdb2019-04-09 09:01:15 -0600170 shift
171
Gavin Howard62421e92019-04-17 12:56:30 -0600172 _gen_nlspath_locale="$1"
Gavin Howardaea5bdb2019-04-09 09:01:15 -0600173 shift
174
Gavin Howard62421e92019-04-17 12:56:30 -0600175 _gen_nlspath_execname="$1"
Gavin Howardaea5bdb2019-04-09 09:01:15 -0600176 shift
177
Gavin Howard62421e92019-04-17 12:56:30 -0600178 _gen_nlspath_char="@"
179 _gen_nlspath_modifier="${_gen_nlspath_locale#*$_gen_nlspath_char}"
180 _gen_nlspath_tmplocale="${_gen_nlspath_locale%%$_gen_nlspath_char*}"
Gavin Howardaea5bdb2019-04-09 09:01:15 -0600181
Gavin Howard62421e92019-04-17 12:56:30 -0600182 _gen_nlspath_char="."
183 _gen_nlspath_charset="${_gen_nlspath_tmplocale#*$_gen_nlspath_char}"
184 _gen_nlspath_tmplocale="${_gen_nlspath_tmplocale%%$_gen_nlspath_char*}"
Gavin Howardaea5bdb2019-04-09 09:01:15 -0600185
Gavin Howard62421e92019-04-17 12:56:30 -0600186 if [ "$_gen_nlspath_charset" = "$_gen_nlspath_tmplocale" ]; then
187 _gen_nlspath_charset=""
Gavin Howardaea5bdb2019-04-09 09:01:15 -0600188 fi
189
Gavin Howard62421e92019-04-17 12:56:30 -0600190 _gen_nlspath_char="_"
191 _gen_nlspath_territory="${_gen_nlspath_tmplocale#*$_gen_nlspath_char}"
192 _gen_nlspath_language="${_gen_nlspath_tmplocale%%$_gen_nlspath_char*}"
Gavin Howardaea5bdb2019-04-09 09:01:15 -0600193
Gavin Howard62421e92019-04-17 12:56:30 -0600194 if [ "$_gen_nlspath_territory" = "$_gen_nlspath_tmplocale" ]; then
195 _gen_nlspath_territory=""
Gavin Howardaea5bdb2019-04-09 09:01:15 -0600196 fi
197
Gavin Howard62421e92019-04-17 12:56:30 -0600198 if [ "$_gen_nlspath_language" = "$_gen_nlspath_tmplocale" ]; then
199 _gen_nlspath_language=""
Gavin Howardaea5bdb2019-04-09 09:01:15 -0600200 fi
201
Gavin Howard62421e92019-04-17 12:56:30 -0600202 _gen_nlspath_needles="%%:%L:%N:%l:%t:%c"
Gavin Howardaea5bdb2019-04-09 09:01:15 -0600203
Gavin Howard62421e92019-04-17 12:56:30 -0600204 _gen_nlspath_needles=$(printf '%s' "$_gen_nlspath_needles" | tr ':' '\n')
Gavin Howardaea5bdb2019-04-09 09:01:15 -0600205
Gavin Howard62421e92019-04-17 12:56:30 -0600206 for _gen_nlspath_i in $_gen_nlspath_needles; do
207 _gen_nlspath_nlspath=$(substring_replace "$_gen_nlspath_nlspath" "$_gen_nlspath_i" "|$_gen_nlspath_i|")
Gavin Howardaea5bdb2019-04-09 09:01:15 -0600208 done
209
Gavin Howard62421e92019-04-17 12:56:30 -0600210 _gen_nlspath_nlspath=$(substring_replace "$_gen_nlspath_nlspath" "%%" "%")
211 _gen_nlspath_nlspath=$(substring_replace "$_gen_nlspath_nlspath" "%L" "$_gen_nlspath_locale")
212 _gen_nlspath_nlspath=$(substring_replace "$_gen_nlspath_nlspath" "%N" "$_gen_nlspath_execname")
213 _gen_nlspath_nlspath=$(substring_replace "$_gen_nlspath_nlspath" "%l" "$_gen_nlspath_language")
214 _gen_nlspath_nlspath=$(substring_replace "$_gen_nlspath_nlspath" "%t" "$_gen_nlspath_territory")
215 _gen_nlspath_nlspath=$(substring_replace "$_gen_nlspath_nlspath" "%c" "$_gen_nlspath_charset")
Gavin Howardaea5bdb2019-04-09 09:01:15 -0600216
Gavin Howard62421e92019-04-17 12:56:30 -0600217 _gen_nlspath_nlspath=$(printf '%s' "$_gen_nlspath_nlspath" | tr -d '|')
Gavin Howardaea5bdb2019-04-09 09:01:15 -0600218
Gavin Howard62421e92019-04-17 12:56:30 -0600219 printf '%s' "$_gen_nlspath_nlspath"
Gavin Howardaea5bdb2019-04-09 09:01:15 -0600220}