Gavin Howard | 7c715d6 | 2019-02-20 10:41:08 -0700 | [diff] [blame] | 1 | #! /bin/sh |
| 2 | # |
Gavin Howard | 29e00ba | 2020-06-30 09:25:21 -0600 | [diff] [blame^] | 3 | # SPDX-License-Identifier: BSD-2-Clause |
| 4 | # |
Zach van Rijn | 6d2cf3f | 2020-01-14 22:05:02 +0000 | [diff] [blame] | 5 | # Copyright (c) 2018-2020 Gavin D. Howard and contributors. |
Gavin Howard | 7c715d6 | 2019-02-20 10:41:08 -0700 | [diff] [blame] | 6 | # |
Gavin Howard | 7345cb9 | 2019-04-08 14:13:43 -0600 | [diff] [blame] | 7 | # All rights reserved. |
Gavin Howard | 7c715d6 | 2019-02-20 10:41:08 -0700 | [diff] [blame] | 8 | # |
Gavin Howard | 7345cb9 | 2019-04-08 14:13:43 -0600 | [diff] [blame] | 9 | # 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 Howard | 7c715d6 | 2019-02-20 10:41:08 -0700 | [diff] [blame] | 30 | # |
| 31 | |
Gavin Howard | 5008025 | 2019-02-20 15:27:43 -0700 | [diff] [blame] | 32 | readlink() { |
| 33 | |
Gavin Howard | 62421e9 | 2019-04-17 12:56:30 -0600 | [diff] [blame] | 34 | _readlink_f="$1" |
Gavin Howard | 5008025 | 2019-02-20 15:27:43 -0700 | [diff] [blame] | 35 | shift |
| 36 | |
Gavin Howard | 62421e9 | 2019-04-17 12:56:30 -0600 | [diff] [blame] | 37 | _readlink_arrow="-> " |
| 38 | _readlink_d=$(dirname "$_readlink_f") |
Gavin Howard | 5008025 | 2019-02-20 15:27:43 -0700 | [diff] [blame] | 39 | |
Gavin Howard | 62421e9 | 2019-04-17 12:56:30 -0600 | [diff] [blame] | 40 | _readlink_lsout="" |
| 41 | _readlink_link="" |
Gavin Howard | aea5bdb | 2019-04-09 09:01:15 -0600 | [diff] [blame] | 42 | |
Gavin Howard | 62421e9 | 2019-04-17 12:56:30 -0600 | [diff] [blame] | 43 | _readlink_lsout=$(ls -dl "$_readlink_f") |
| 44 | _readlink_link=$(printf '%s' "${_readlink_lsout#*$_readlink_arrow}") |
Gavin Howard | aea5bdb | 2019-04-09 09:01:15 -0600 | [diff] [blame] | 45 | |
Gavin Howard | 62421e9 | 2019-04-17 12:56:30 -0600 | [diff] [blame] | 46 | 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 Howard | aea5bdb | 2019-04-09 09:01:15 -0600 | [diff] [blame] | 51 | done |
| 52 | |
Gavin Howard | 62421e9 | 2019-04-17 12:56:30 -0600 | [diff] [blame] | 53 | printf '%s' "${_readlink_f##*$_readlink_d/}" |
Gavin Howard | 5008025 | 2019-02-20 15:27:43 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Gavin Howard | 8237f80 | 2019-04-11 12:23:03 -0600 | [diff] [blame] | 56 | err_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 Howard | 7c715d6 | 2019-02-20 10:41:08 -0700 | [diff] [blame] | 68 | die() { |
| 69 | |
Gavin Howard | 62421e9 | 2019-04-17 12:56:30 -0600 | [diff] [blame] | 70 | _die_d="$1" |
Gavin Howard | 7c715d6 | 2019-02-20 10:41:08 -0700 | [diff] [blame] | 71 | shift |
| 72 | |
Gavin Howard | 62421e9 | 2019-04-17 12:56:30 -0600 | [diff] [blame] | 73 | _die_msg="$1" |
Gavin Howard | 7c715d6 | 2019-02-20 10:41:08 -0700 | [diff] [blame] | 74 | shift |
| 75 | |
Gavin Howard | 62421e9 | 2019-04-17 12:56:30 -0600 | [diff] [blame] | 76 | _die_name="$1" |
Gavin Howard | 7c715d6 | 2019-02-20 10:41:08 -0700 | [diff] [blame] | 77 | shift |
| 78 | |
Gavin Howard | 62421e9 | 2019-04-17 12:56:30 -0600 | [diff] [blame] | 79 | _die_err="$1" |
Gavin Howard | 7c715d6 | 2019-02-20 10:41:08 -0700 | [diff] [blame] | 80 | shift |
| 81 | |
Gavin Howard | 62421e9 | 2019-04-17 12:56:30 -0600 | [diff] [blame] | 82 | _die_str=$(printf '\n%s %s on test:\n\n %s\n' "$_die_d" "$_die_msg" "$_die_name") |
Gavin Howard | 8237f80 | 2019-04-11 12:23:03 -0600 | [diff] [blame] | 83 | |
Gavin Howard | 62421e9 | 2019-04-17 12:56:30 -0600 | [diff] [blame] | 84 | err_exit "$_die_str" "$_die_err" |
Gavin Howard | 7c715d6 | 2019-02-20 10:41:08 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | checkcrash() { |
| 88 | |
Gavin Howard | 62421e9 | 2019-04-17 12:56:30 -0600 | [diff] [blame] | 89 | _checkcrash_d="$1" |
Gavin Howard | 7c715d6 | 2019-02-20 10:41:08 -0700 | [diff] [blame] | 90 | shift |
| 91 | |
Gavin Howard | 62421e9 | 2019-04-17 12:56:30 -0600 | [diff] [blame] | 92 | _checkcrash_error="$1" |
Gavin Howard | 7c715d6 | 2019-02-20 10:41:08 -0700 | [diff] [blame] | 93 | shift |
| 94 | |
Gavin Howard | 62421e9 | 2019-04-17 12:56:30 -0600 | [diff] [blame] | 95 | _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 Howard | 7c715d6 | 2019-02-20 10:41:08 -0700 | [diff] [blame] | 101 | fi |
| 102 | } |
| 103 | |
| 104 | checktest() |
| 105 | { |
Gavin Howard | 62421e9 | 2019-04-17 12:56:30 -0600 | [diff] [blame] | 106 | _checktest_d="$1" |
Gavin Howard | 7c715d6 | 2019-02-20 10:41:08 -0700 | [diff] [blame] | 107 | shift |
| 108 | |
Gavin Howard | 62421e9 | 2019-04-17 12:56:30 -0600 | [diff] [blame] | 109 | _checktest_error="$1" |
Gavin Howard | 7c715d6 | 2019-02-20 10:41:08 -0700 | [diff] [blame] | 110 | shift |
| 111 | |
Gavin Howard | 62421e9 | 2019-04-17 12:56:30 -0600 | [diff] [blame] | 112 | _checktest_name="$1" |
Gavin Howard | 7c715d6 | 2019-02-20 10:41:08 -0700 | [diff] [blame] | 113 | shift |
| 114 | |
Gavin Howard | 62421e9 | 2019-04-17 12:56:30 -0600 | [diff] [blame] | 115 | _checktest_out="$1" |
Gavin Howard | 7c715d6 | 2019-02-20 10:41:08 -0700 | [diff] [blame] | 116 | shift |
| 117 | |
Gavin Howard | 62421e9 | 2019-04-17 12:56:30 -0600 | [diff] [blame] | 118 | _checktest_exebase="$1" |
| 119 | shift |
Gavin Howard | 7c715d6 | 2019-02-20 10:41:08 -0700 | [diff] [blame] | 120 | |
Gavin Howard | 62421e9 | 2019-04-17 12:56:30 -0600 | [diff] [blame] | 121 | 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 Howard | 7c715d6 | 2019-02-20 10:41:08 -0700 | [diff] [blame] | 125 | fi |
| 126 | |
Gavin Howard | 62421e9 | 2019-04-17 12:56:30 -0600 | [diff] [blame] | 127 | if [ "$_checktest_error" -eq 100 ]; then |
Gavin Howard | 7c715d6 | 2019-02-20 10:41:08 -0700 | [diff] [blame] | 128 | |
Gavin Howard | 62421e9 | 2019-04-17 12:56:30 -0600 | [diff] [blame] | 129 | _checktest_output=$(cat "$_checktest_out") |
| 130 | _checktest_fatal_error="Fatal error" |
Gavin Howard | 7c715d6 | 2019-02-20 10:41:08 -0700 | [diff] [blame] | 131 | |
Gavin Howard | 62421e9 | 2019-04-17 12:56:30 -0600 | [diff] [blame] | 132 | 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 Howard | 7c715d6 | 2019-02-20 10:41:08 -0700 | [diff] [blame] | 136 | fi |
| 137 | fi |
| 138 | |
Gavin Howard | 62421e9 | 2019-04-17 12:56:30 -0600 | [diff] [blame] | 139 | if [ ! -s "$_checktest_out" ]; then |
| 140 | die "$_checktest_d" "produced no error message" "$_checktest_name" "$_checktest_error" |
Gavin Howard | 7c715d6 | 2019-02-20 10:41:08 -0700 | [diff] [blame] | 141 | fi |
| 142 | |
| 143 | # Display the error messages if not directly running exe. |
| 144 | # This allows the script to print valgrind output. |
Gavin Howard | 62421e9 | 2019-04-17 12:56:30 -0600 | [diff] [blame] | 145 | if [ "$_checktest_exebase" != "bc" -a "$_checktest_exebase" != "dc" ]; then |
| 146 | cat "$_checktest_out" |
Gavin Howard | 7c715d6 | 2019-02-20 10:41:08 -0700 | [diff] [blame] | 147 | fi |
| 148 | } |
Gavin Howard | 2425bf2 | 2019-04-08 17:05:45 -0600 | [diff] [blame] | 149 | |
| 150 | substring_replace() { |
| 151 | |
Gavin Howard | 62421e9 | 2019-04-17 12:56:30 -0600 | [diff] [blame] | 152 | _substring_replace_str="$1" |
Gavin Howard | aea5bdb | 2019-04-09 09:01:15 -0600 | [diff] [blame] | 153 | shift |
Gavin Howard | 2425bf2 | 2019-04-08 17:05:45 -0600 | [diff] [blame] | 154 | |
Gavin Howard | 62421e9 | 2019-04-17 12:56:30 -0600 | [diff] [blame] | 155 | _substring_replace_needle="$1" |
Gavin Howard | aea5bdb | 2019-04-09 09:01:15 -0600 | [diff] [blame] | 156 | shift |
| 157 | |
Gavin Howard | 62421e9 | 2019-04-17 12:56:30 -0600 | [diff] [blame] | 158 | _substring_replace_replacement="$1" |
Gavin Howard | aea5bdb | 2019-04-09 09:01:15 -0600 | [diff] [blame] | 159 | shift |
Gavin Howard | 2425bf2 | 2019-04-08 17:05:45 -0600 | [diff] [blame] | 160 | |
Zach van Rijn | 579518c | 2020-01-14 21:56:45 +0000 | [diff] [blame] | 161 | _substring_replace_result=$(printf '%s\n' "$_substring_replace_str" | \ |
Gavin Howard | 62421e9 | 2019-04-17 12:56:30 -0600 | [diff] [blame] | 162 | sed -e "s!$_substring_replace_needle!$_substring_replace_replacement!g") |
Gavin Howard | 2425bf2 | 2019-04-08 17:05:45 -0600 | [diff] [blame] | 163 | |
Zach van Rijn | 579518c | 2020-01-14 21:56:45 +0000 | [diff] [blame] | 164 | printf '%s' "$_substring_replace_result" |
Gavin Howard | 2425bf2 | 2019-04-08 17:05:45 -0600 | [diff] [blame] | 165 | } |
Gavin Howard | aea5bdb | 2019-04-09 09:01:15 -0600 | [diff] [blame] | 166 | |
| 167 | gen_nlspath() { |
| 168 | |
Gavin Howard | 62421e9 | 2019-04-17 12:56:30 -0600 | [diff] [blame] | 169 | _gen_nlspath_nlspath="$1" |
Gavin Howard | aea5bdb | 2019-04-09 09:01:15 -0600 | [diff] [blame] | 170 | shift |
| 171 | |
Gavin Howard | 62421e9 | 2019-04-17 12:56:30 -0600 | [diff] [blame] | 172 | _gen_nlspath_locale="$1" |
Gavin Howard | aea5bdb | 2019-04-09 09:01:15 -0600 | [diff] [blame] | 173 | shift |
| 174 | |
Gavin Howard | 62421e9 | 2019-04-17 12:56:30 -0600 | [diff] [blame] | 175 | _gen_nlspath_execname="$1" |
Gavin Howard | aea5bdb | 2019-04-09 09:01:15 -0600 | [diff] [blame] | 176 | shift |
| 177 | |
Gavin Howard | 62421e9 | 2019-04-17 12:56:30 -0600 | [diff] [blame] | 178 | _gen_nlspath_char="@" |
| 179 | _gen_nlspath_modifier="${_gen_nlspath_locale#*$_gen_nlspath_char}" |
| 180 | _gen_nlspath_tmplocale="${_gen_nlspath_locale%%$_gen_nlspath_char*}" |
Gavin Howard | aea5bdb | 2019-04-09 09:01:15 -0600 | [diff] [blame] | 181 | |
Gavin Howard | 62421e9 | 2019-04-17 12:56:30 -0600 | [diff] [blame] | 182 | _gen_nlspath_char="." |
| 183 | _gen_nlspath_charset="${_gen_nlspath_tmplocale#*$_gen_nlspath_char}" |
| 184 | _gen_nlspath_tmplocale="${_gen_nlspath_tmplocale%%$_gen_nlspath_char*}" |
Gavin Howard | aea5bdb | 2019-04-09 09:01:15 -0600 | [diff] [blame] | 185 | |
Gavin Howard | 62421e9 | 2019-04-17 12:56:30 -0600 | [diff] [blame] | 186 | if [ "$_gen_nlspath_charset" = "$_gen_nlspath_tmplocale" ]; then |
| 187 | _gen_nlspath_charset="" |
Gavin Howard | aea5bdb | 2019-04-09 09:01:15 -0600 | [diff] [blame] | 188 | fi |
| 189 | |
Gavin Howard | 62421e9 | 2019-04-17 12:56:30 -0600 | [diff] [blame] | 190 | _gen_nlspath_char="_" |
| 191 | _gen_nlspath_territory="${_gen_nlspath_tmplocale#*$_gen_nlspath_char}" |
| 192 | _gen_nlspath_language="${_gen_nlspath_tmplocale%%$_gen_nlspath_char*}" |
Gavin Howard | aea5bdb | 2019-04-09 09:01:15 -0600 | [diff] [blame] | 193 | |
Gavin Howard | 62421e9 | 2019-04-17 12:56:30 -0600 | [diff] [blame] | 194 | if [ "$_gen_nlspath_territory" = "$_gen_nlspath_tmplocale" ]; then |
| 195 | _gen_nlspath_territory="" |
Gavin Howard | aea5bdb | 2019-04-09 09:01:15 -0600 | [diff] [blame] | 196 | fi |
| 197 | |
Gavin Howard | 62421e9 | 2019-04-17 12:56:30 -0600 | [diff] [blame] | 198 | if [ "$_gen_nlspath_language" = "$_gen_nlspath_tmplocale" ]; then |
| 199 | _gen_nlspath_language="" |
Gavin Howard | aea5bdb | 2019-04-09 09:01:15 -0600 | [diff] [blame] | 200 | fi |
| 201 | |
Gavin Howard | 62421e9 | 2019-04-17 12:56:30 -0600 | [diff] [blame] | 202 | _gen_nlspath_needles="%%:%L:%N:%l:%t:%c" |
Gavin Howard | aea5bdb | 2019-04-09 09:01:15 -0600 | [diff] [blame] | 203 | |
Gavin Howard | 62421e9 | 2019-04-17 12:56:30 -0600 | [diff] [blame] | 204 | _gen_nlspath_needles=$(printf '%s' "$_gen_nlspath_needles" | tr ':' '\n') |
Gavin Howard | aea5bdb | 2019-04-09 09:01:15 -0600 | [diff] [blame] | 205 | |
Gavin Howard | 62421e9 | 2019-04-17 12:56:30 -0600 | [diff] [blame] | 206 | 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 Howard | aea5bdb | 2019-04-09 09:01:15 -0600 | [diff] [blame] | 208 | done |
| 209 | |
Gavin Howard | 62421e9 | 2019-04-17 12:56:30 -0600 | [diff] [blame] | 210 | _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 Howard | aea5bdb | 2019-04-09 09:01:15 -0600 | [diff] [blame] | 216 | |
Gavin Howard | 62421e9 | 2019-04-17 12:56:30 -0600 | [diff] [blame] | 217 | _gen_nlspath_nlspath=$(printf '%s' "$_gen_nlspath_nlspath" | tr -d '|') |
Gavin Howard | aea5bdb | 2019-04-09 09:01:15 -0600 | [diff] [blame] | 218 | |
Gavin Howard | 62421e9 | 2019-04-17 12:56:30 -0600 | [diff] [blame] | 219 | printf '%s' "$_gen_nlspath_nlspath" |
Gavin Howard | aea5bdb | 2019-04-09 09:01:15 -0600 | [diff] [blame] | 220 | } |