Gavin Howard | 7c715d6 | 2019-02-20 10:41:08 -0700 | [diff] [blame] | 1 | #! /bin/sh |
| 2 | # |
Gavin Howard | 7345cb9 | 2019-04-08 14:13:43 -0600 | [diff] [blame] | 3 | # Copyright (c) 2018-2019 Gavin D. Howard and contributors. |
Gavin Howard | 7c715d6 | 2019-02-20 10:41:08 -0700 | [diff] [blame] | 4 | # |
Gavin Howard | 7345cb9 | 2019-04-08 14:13:43 -0600 | [diff] [blame] | 5 | # All rights reserved. |
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 | # 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 Howard | 7c715d6 | 2019-02-20 10:41:08 -0700 | [diff] [blame] | 28 | # |
| 29 | |
Gavin Howard | 5008025 | 2019-02-20 15:27:43 -0700 | [diff] [blame] | 30 | readlink() { |
| 31 | |
Gavin Howard | aea5bdb | 2019-04-09 09:01:15 -0600 | [diff] [blame] | 32 | local f="$1" |
Gavin Howard | 5008025 | 2019-02-20 15:27:43 -0700 | [diff] [blame] | 33 | shift |
| 34 | |
Gavin Howard | aea5bdb | 2019-04-09 09:01:15 -0600 | [diff] [blame] | 35 | local arrow="-> " |
| 36 | local d=$(dirname "$f") |
Gavin Howard | 5008025 | 2019-02-20 15:27:43 -0700 | [diff] [blame] | 37 | |
Gavin Howard | aea5bdb | 2019-04-09 09:01:15 -0600 | [diff] [blame] | 38 | 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 Howard | 5008025 | 2019-02-20 15:27:43 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Gavin Howard | f680e35 | 2019-04-01 13:28:48 -0600 | [diff] [blame] | 55 | removeext() { |
| 56 | |
| 57 | local name="$1" |
| 58 | shift |
| 59 | |
Gavin Howard | 013a230 | 2019-04-01 15:00:39 -0600 | [diff] [blame] | 60 | printf "${name%.*}" |
Gavin Howard | f680e35 | 2019-04-01 13:28:48 -0600 | [diff] [blame] | 61 | } |
| 62 | |
Gavin Howard | 7c715d6 | 2019-02-20 10:41:08 -0700 | [diff] [blame] | 63 | die() { |
| 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 | |
| 86 | checkcrash() { |
| 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 | |
| 99 | checktest() |
| 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 Howard | 2425bf2 | 2019-04-08 17:05:45 -0600 | [diff] [blame] | 140 | |
| 141 | substring_replace() { |
| 142 | |
Gavin Howard | aea5bdb | 2019-04-09 09:01:15 -0600 | [diff] [blame] | 143 | local str="$1" |
| 144 | shift |
Gavin Howard | 2425bf2 | 2019-04-08 17:05:45 -0600 | [diff] [blame] | 145 | |
Gavin Howard | aea5bdb | 2019-04-09 09:01:15 -0600 | [diff] [blame] | 146 | local needle="$1" |
| 147 | shift |
| 148 | |
| 149 | local replacement="$1" |
| 150 | shift |
Gavin Howard | 2425bf2 | 2019-04-08 17:05:45 -0600 | [diff] [blame] | 151 | |
| 152 | result=$(printf '%s' "$str" | sed -e "s!$needle!$replacement!g") |
| 153 | |
| 154 | printf '%s\n' "$result" |
| 155 | } |
Gavin Howard | aea5bdb | 2019-04-09 09:01:15 -0600 | [diff] [blame] | 156 | |
| 157 | gen_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 | } |