blob: 254e81c5167cacb4bdbc33b72fcc92aeecc7c1cd [file] [log] [blame]
Gavin Howard7c715d62019-02-20 10:41:08 -07001#! /bin/sh
2#
Zach van Rijn6d2cf3f2020-01-14 22:05:02 +00003# Copyright (c) 2018-2020 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 Howard62421e92019-04-17 12:56:30 -060032 _readlink_f="$1"
Gavin Howard50080252019-02-20 15:27:43 -070033 shift
34
Gavin Howard62421e92019-04-17 12:56:30 -060035 _readlink_arrow="-> "
36 _readlink_d=$(dirname "$_readlink_f")
Gavin Howard50080252019-02-20 15:27:43 -070037
Gavin Howard62421e92019-04-17 12:56:30 -060038 _readlink_lsout=""
39 _readlink_link=""
Gavin Howardaea5bdb2019-04-09 09:01:15 -060040
Gavin Howard62421e92019-04-17 12:56:30 -060041 _readlink_lsout=$(ls -dl "$_readlink_f")
42 _readlink_link=$(printf '%s' "${_readlink_lsout#*$_readlink_arrow}")
Gavin Howardaea5bdb2019-04-09 09:01:15 -060043
Gavin Howard62421e92019-04-17 12:56:30 -060044 while [ -z "${_readlink_lsout##*$_readlink_arrow*}" ]; do
45 _readlink_f="$_readlink_d/$_readlink_link"
46 _readlink_d=$(dirname "$_readlink_f")
47 _readlink_lsout=$(ls -dl "$_readlink_f")
48 _readlink_link=$(printf '%s' "${_readlink_lsout#*$_readlink_arrow}")
Gavin Howardaea5bdb2019-04-09 09:01:15 -060049 done
50
Gavin Howard62421e92019-04-17 12:56:30 -060051 printf '%s' "${_readlink_f##*$_readlink_d/}"
Gavin Howard50080252019-02-20 15:27:43 -070052}
53
Gavin Howard8237f802019-04-11 12:23:03 -060054err_exit() {
55
56 if [ "$#" -ne 2 ]; then
57 printf 'Invalid number of args to err_exit\n'
58 exit 1
59 fi
60
61 printf '%s\n' "$1"
62 printf '\nexiting...\n'
63 exit "$2"
64}
65
Gavin Howard7c715d62019-02-20 10:41:08 -070066die() {
67
Gavin Howard62421e92019-04-17 12:56:30 -060068 _die_d="$1"
Gavin Howard7c715d62019-02-20 10:41:08 -070069 shift
70
Gavin Howard62421e92019-04-17 12:56:30 -060071 _die_msg="$1"
Gavin Howard7c715d62019-02-20 10:41:08 -070072 shift
73
Gavin Howard62421e92019-04-17 12:56:30 -060074 _die_name="$1"
Gavin Howard7c715d62019-02-20 10:41:08 -070075 shift
76
Gavin Howard62421e92019-04-17 12:56:30 -060077 _die_err="$1"
Gavin Howard7c715d62019-02-20 10:41:08 -070078 shift
79
Gavin Howard62421e92019-04-17 12:56:30 -060080 _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 -060081
Gavin Howard62421e92019-04-17 12:56:30 -060082 err_exit "$_die_str" "$_die_err"
Gavin Howard7c715d62019-02-20 10:41:08 -070083}
84
85checkcrash() {
86
Gavin Howard62421e92019-04-17 12:56:30 -060087 _checkcrash_d="$1"
Gavin Howard7c715d62019-02-20 10:41:08 -070088 shift
89
Gavin Howard62421e92019-04-17 12:56:30 -060090 _checkcrash_error="$1"
Gavin Howard7c715d62019-02-20 10:41:08 -070091 shift
92
Gavin Howard62421e92019-04-17 12:56:30 -060093 _checkcrash_name="$1"
94 shift
95
96 if [ "$_checkcrash_error" -gt 127 ]; then
97 die "$_checkcrash_d" "crashed ($_checkcrash_error)" \
98 "$_checkcrash_name" "$_checkcrash_error"
Gavin Howard7c715d62019-02-20 10:41:08 -070099 fi
100}
101
102checktest()
103{
Gavin Howard62421e92019-04-17 12:56:30 -0600104 _checktest_d="$1"
Gavin Howard7c715d62019-02-20 10:41:08 -0700105 shift
106
Gavin Howard62421e92019-04-17 12:56:30 -0600107 _checktest_error="$1"
Gavin Howard7c715d62019-02-20 10:41:08 -0700108 shift
109
Gavin Howard62421e92019-04-17 12:56:30 -0600110 _checktest_name="$1"
Gavin Howard7c715d62019-02-20 10:41:08 -0700111 shift
112
Gavin Howard62421e92019-04-17 12:56:30 -0600113 _checktest_out="$1"
Gavin Howard7c715d62019-02-20 10:41:08 -0700114 shift
115
Gavin Howard62421e92019-04-17 12:56:30 -0600116 _checktest_exebase="$1"
117 shift
Gavin Howard7c715d62019-02-20 10:41:08 -0700118
Gavin Howard62421e92019-04-17 12:56:30 -0600119 checkcrash "$_checktest_d" "$_checktest_error" "$_checktest_name"
120
121 if [ "$_checktest_error" -eq 0 ]; then
122 die "$_checktest_d" "returned no error" "$_checktest_name" 127
Gavin Howard7c715d62019-02-20 10:41:08 -0700123 fi
124
Gavin Howard62421e92019-04-17 12:56:30 -0600125 if [ "$_checktest_error" -eq 100 ]; then
Gavin Howard7c715d62019-02-20 10:41:08 -0700126
Gavin Howard62421e92019-04-17 12:56:30 -0600127 _checktest_output=$(cat "$_checktest_out")
128 _checktest_fatal_error="Fatal error"
Gavin Howard7c715d62019-02-20 10:41:08 -0700129
Gavin Howard62421e92019-04-17 12:56:30 -0600130 if [ "${_checktest_output##*$_checktest_fatal_error*}" ]; then
131 printf "%s\n" "$_checktest_output"
132 die "$_checktest_d" "had memory errors on a non-fatal error" \
133 "$_checktest_name" "$_checktest_error"
Gavin Howard7c715d62019-02-20 10:41:08 -0700134 fi
135 fi
136
Gavin Howard62421e92019-04-17 12:56:30 -0600137 if [ ! -s "$_checktest_out" ]; then
138 die "$_checktest_d" "produced no error message" "$_checktest_name" "$_checktest_error"
Gavin Howard7c715d62019-02-20 10:41:08 -0700139 fi
140
141 # Display the error messages if not directly running exe.
142 # This allows the script to print valgrind output.
Gavin Howard62421e92019-04-17 12:56:30 -0600143 if [ "$_checktest_exebase" != "bc" -a "$_checktest_exebase" != "dc" ]; then
144 cat "$_checktest_out"
Gavin Howard7c715d62019-02-20 10:41:08 -0700145 fi
146}
Gavin Howard2425bf22019-04-08 17:05:45 -0600147
148substring_replace() {
149
Gavin Howard62421e92019-04-17 12:56:30 -0600150 _substring_replace_str="$1"
Gavin Howardaea5bdb2019-04-09 09:01:15 -0600151 shift
Gavin Howard2425bf22019-04-08 17:05:45 -0600152
Gavin Howard62421e92019-04-17 12:56:30 -0600153 _substring_replace_needle="$1"
Gavin Howardaea5bdb2019-04-09 09:01:15 -0600154 shift
155
Gavin Howard62421e92019-04-17 12:56:30 -0600156 _substring_replace_replacement="$1"
Gavin Howardaea5bdb2019-04-09 09:01:15 -0600157 shift
Gavin Howard2425bf22019-04-08 17:05:45 -0600158
Zach van Rijn579518c2020-01-14 21:56:45 +0000159 _substring_replace_result=$(printf '%s\n' "$_substring_replace_str" | \
Gavin Howard62421e92019-04-17 12:56:30 -0600160 sed -e "s!$_substring_replace_needle!$_substring_replace_replacement!g")
Gavin Howard2425bf22019-04-08 17:05:45 -0600161
Zach van Rijn579518c2020-01-14 21:56:45 +0000162 printf '%s' "$_substring_replace_result"
Gavin Howard2425bf22019-04-08 17:05:45 -0600163}
Gavin Howardaea5bdb2019-04-09 09:01:15 -0600164
165gen_nlspath() {
166
Gavin Howard62421e92019-04-17 12:56:30 -0600167 _gen_nlspath_nlspath="$1"
Gavin Howardaea5bdb2019-04-09 09:01:15 -0600168 shift
169
Gavin Howard62421e92019-04-17 12:56:30 -0600170 _gen_nlspath_locale="$1"
Gavin Howardaea5bdb2019-04-09 09:01:15 -0600171 shift
172
Gavin Howard62421e92019-04-17 12:56:30 -0600173 _gen_nlspath_execname="$1"
Gavin Howardaea5bdb2019-04-09 09:01:15 -0600174 shift
175
Gavin Howard62421e92019-04-17 12:56:30 -0600176 _gen_nlspath_char="@"
177 _gen_nlspath_modifier="${_gen_nlspath_locale#*$_gen_nlspath_char}"
178 _gen_nlspath_tmplocale="${_gen_nlspath_locale%%$_gen_nlspath_char*}"
Gavin Howardaea5bdb2019-04-09 09:01:15 -0600179
Gavin Howard62421e92019-04-17 12:56:30 -0600180 _gen_nlspath_char="."
181 _gen_nlspath_charset="${_gen_nlspath_tmplocale#*$_gen_nlspath_char}"
182 _gen_nlspath_tmplocale="${_gen_nlspath_tmplocale%%$_gen_nlspath_char*}"
Gavin Howardaea5bdb2019-04-09 09:01:15 -0600183
Gavin Howard62421e92019-04-17 12:56:30 -0600184 if [ "$_gen_nlspath_charset" = "$_gen_nlspath_tmplocale" ]; then
185 _gen_nlspath_charset=""
Gavin Howardaea5bdb2019-04-09 09:01:15 -0600186 fi
187
Gavin Howard62421e92019-04-17 12:56:30 -0600188 _gen_nlspath_char="_"
189 _gen_nlspath_territory="${_gen_nlspath_tmplocale#*$_gen_nlspath_char}"
190 _gen_nlspath_language="${_gen_nlspath_tmplocale%%$_gen_nlspath_char*}"
Gavin Howardaea5bdb2019-04-09 09:01:15 -0600191
Gavin Howard62421e92019-04-17 12:56:30 -0600192 if [ "$_gen_nlspath_territory" = "$_gen_nlspath_tmplocale" ]; then
193 _gen_nlspath_territory=""
Gavin Howardaea5bdb2019-04-09 09:01:15 -0600194 fi
195
Gavin Howard62421e92019-04-17 12:56:30 -0600196 if [ "$_gen_nlspath_language" = "$_gen_nlspath_tmplocale" ]; then
197 _gen_nlspath_language=""
Gavin Howardaea5bdb2019-04-09 09:01:15 -0600198 fi
199
Gavin Howard62421e92019-04-17 12:56:30 -0600200 _gen_nlspath_needles="%%:%L:%N:%l:%t:%c"
Gavin Howardaea5bdb2019-04-09 09:01:15 -0600201
Gavin Howard62421e92019-04-17 12:56:30 -0600202 _gen_nlspath_needles=$(printf '%s' "$_gen_nlspath_needles" | tr ':' '\n')
Gavin Howardaea5bdb2019-04-09 09:01:15 -0600203
Gavin Howard62421e92019-04-17 12:56:30 -0600204 for _gen_nlspath_i in $_gen_nlspath_needles; do
205 _gen_nlspath_nlspath=$(substring_replace "$_gen_nlspath_nlspath" "$_gen_nlspath_i" "|$_gen_nlspath_i|")
Gavin Howardaea5bdb2019-04-09 09:01:15 -0600206 done
207
Gavin Howard62421e92019-04-17 12:56:30 -0600208 _gen_nlspath_nlspath=$(substring_replace "$_gen_nlspath_nlspath" "%%" "%")
209 _gen_nlspath_nlspath=$(substring_replace "$_gen_nlspath_nlspath" "%L" "$_gen_nlspath_locale")
210 _gen_nlspath_nlspath=$(substring_replace "$_gen_nlspath_nlspath" "%N" "$_gen_nlspath_execname")
211 _gen_nlspath_nlspath=$(substring_replace "$_gen_nlspath_nlspath" "%l" "$_gen_nlspath_language")
212 _gen_nlspath_nlspath=$(substring_replace "$_gen_nlspath_nlspath" "%t" "$_gen_nlspath_territory")
213 _gen_nlspath_nlspath=$(substring_replace "$_gen_nlspath_nlspath" "%c" "$_gen_nlspath_charset")
Gavin Howardaea5bdb2019-04-09 09:01:15 -0600214
Gavin Howard62421e92019-04-17 12:56:30 -0600215 _gen_nlspath_nlspath=$(printf '%s' "$_gen_nlspath_nlspath" | tr -d '|')
Gavin Howardaea5bdb2019-04-09 09:01:15 -0600216
Gavin Howard62421e92019-04-17 12:56:30 -0600217 printf '%s' "$_gen_nlspath_nlspath"
Gavin Howardaea5bdb2019-04-09 09:01:15 -0600218}