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 |
Gavin Howard | bc02129 | 2019-04-09 10:04:01 -0600 | [diff] [blame] | 45 | f="$d/$link" |
Gavin Howard | aea5bdb | 2019-04-09 09:01:15 -0600 | [diff] [blame] | 46 | d=$(dirname "$f") |
| 47 | lsout=$(ls -dl "$f") |
| 48 | link=$(printf '%s' "${lsout#*$arrow}") |
| 49 | done |
| 50 | |
| 51 | printf '%s' "${f##*$d/}" |
Gavin Howard | 5008025 | 2019-02-20 15:27:43 -0700 | [diff] [blame] | 52 | } |
| 53 | |
Gavin Howard | 8237f80 | 2019-04-11 12:23:03 -0600 | [diff] [blame] | 54 | err_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 Howard | 7c715d6 | 2019-02-20 10:41:08 -0700 | [diff] [blame] | 66 | die() { |
| 67 | |
| 68 | local d="$1" |
| 69 | shift |
| 70 | |
| 71 | local msg="$1" |
| 72 | shift |
| 73 | |
| 74 | local name="$1" |
| 75 | shift |
| 76 | |
| 77 | local err="$1" |
| 78 | shift |
| 79 | |
Gavin Howard | 8237f80 | 2019-04-11 12:23:03 -0600 | [diff] [blame] | 80 | str=$(printf '\n%s %s on test:\n\n %s\n' "$d" "$msg" "$name") |
| 81 | |
| 82 | err_exit "$str" "$err" |
Gavin Howard | 7c715d6 | 2019-02-20 10:41:08 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | checkcrash() { |
| 86 | |
| 87 | local error="$1" |
| 88 | shift |
| 89 | |
| 90 | local name="$1" |
| 91 | shift |
| 92 | |
| 93 | if [ "$error" -gt 127 ]; then |
Gavin Howard | 8237f80 | 2019-04-11 12:23:03 -0600 | [diff] [blame] | 94 | die "$d" "crashed ($error)" "$name" "$error" |
Gavin Howard | 7c715d6 | 2019-02-20 10:41:08 -0700 | [diff] [blame] | 95 | fi |
| 96 | } |
| 97 | |
| 98 | checktest() |
| 99 | { |
| 100 | local error="$1" |
| 101 | shift |
| 102 | |
| 103 | local name="$1" |
| 104 | shift |
| 105 | |
| 106 | local out="$1" |
| 107 | shift |
| 108 | |
| 109 | local exebase="$1" |
| 110 | shift |
| 111 | |
| 112 | checkcrash "$error" "$name" |
| 113 | |
| 114 | if [ "$error" -eq 0 ]; then |
| 115 | die "$d" "returned no error" "$name" 127 |
| 116 | fi |
| 117 | |
| 118 | if [ "$error" -eq 100 ]; then |
| 119 | |
| 120 | output=$(cat "$out") |
| 121 | fatal_error="Fatal error" |
| 122 | |
| 123 | if [ "${output##*$fatal_error*}" ]; then |
| 124 | printf "%s\n" "$output" |
| 125 | die "$d" "had memory errors on a non-fatal error" "$name" "$error" |
| 126 | fi |
| 127 | fi |
| 128 | |
| 129 | if [ ! -s "$out" ]; then |
| 130 | die "$d" "produced no error message" "$name" "$error" |
| 131 | fi |
| 132 | |
| 133 | # Display the error messages if not directly running exe. |
| 134 | # This allows the script to print valgrind output. |
| 135 | if [ "$exebase" != "bc" -a "$exebase" != "dc" ]; then |
| 136 | cat "$out" |
| 137 | fi |
| 138 | } |
Gavin Howard | 2425bf2 | 2019-04-08 17:05:45 -0600 | [diff] [blame] | 139 | |
| 140 | substring_replace() { |
| 141 | |
Gavin Howard | aea5bdb | 2019-04-09 09:01:15 -0600 | [diff] [blame] | 142 | local str="$1" |
| 143 | shift |
Gavin Howard | 2425bf2 | 2019-04-08 17:05:45 -0600 | [diff] [blame] | 144 | |
Gavin Howard | aea5bdb | 2019-04-09 09:01:15 -0600 | [diff] [blame] | 145 | local needle="$1" |
| 146 | shift |
| 147 | |
| 148 | local replacement="$1" |
| 149 | shift |
Gavin Howard | 2425bf2 | 2019-04-08 17:05:45 -0600 | [diff] [blame] | 150 | |
| 151 | result=$(printf '%s' "$str" | sed -e "s!$needle!$replacement!g") |
| 152 | |
| 153 | printf '%s\n' "$result" |
| 154 | } |
Gavin Howard | aea5bdb | 2019-04-09 09:01:15 -0600 | [diff] [blame] | 155 | |
| 156 | gen_nlspath() { |
| 157 | |
| 158 | local nlspath="$1" |
| 159 | shift |
| 160 | |
| 161 | local locale="$1" |
| 162 | shift |
| 163 | |
| 164 | local execname="$1" |
| 165 | shift |
| 166 | |
| 167 | local char="@" |
| 168 | local modifier="${locale#*$char}" |
| 169 | local tmplocale="${locale%%$char*}" |
| 170 | |
| 171 | char="." |
| 172 | local charset="${tmplocale#*$char}" |
| 173 | tmplocale="${tmplocale%%$char*}" |
| 174 | |
| 175 | if [ "$charset" = "$tmplocale" ]; then |
| 176 | charset="" |
| 177 | fi |
| 178 | |
| 179 | char="_" |
| 180 | local territory="${tmplocale#*$char}" |
| 181 | local language="${tmplocale%%$char*}" |
| 182 | |
| 183 | if [ "$territory" = "$tmplocale" ]; then |
| 184 | territory="" |
| 185 | fi |
| 186 | |
| 187 | if [ "$language" = "$tmplocale" ]; then |
| 188 | language="" |
| 189 | fi |
| 190 | |
| 191 | local needles="%%:%L:%N:%l:%t:%c" |
| 192 | |
| 193 | needles=$(printf '%s' "$needles" | tr ':' '\n') |
| 194 | |
| 195 | for i in $needles; do |
Stefan Esser | d86c0cd | 2019-04-09 21:40:47 +0200 | [diff] [blame] | 196 | nlspath=$(substring_replace "$nlspath" "$i" "|$i|") |
Gavin Howard | aea5bdb | 2019-04-09 09:01:15 -0600 | [diff] [blame] | 197 | done |
| 198 | |
| 199 | nlspath=$(substring_replace "$nlspath" "%%" "%") |
| 200 | nlspath=$(substring_replace "$nlspath" "%L" "$locale") |
| 201 | nlspath=$(substring_replace "$nlspath" "%N" "$execname") |
| 202 | nlspath=$(substring_replace "$nlspath" "%l" "$language") |
| 203 | nlspath=$(substring_replace "$nlspath" "%t" "$territory") |
| 204 | nlspath=$(substring_replace "$nlspath" "%c" "$charset") |
| 205 | |
Stefan Esser | d86c0cd | 2019-04-09 21:40:47 +0200 | [diff] [blame] | 206 | nlspath=$(printf '%s' "$nlspath" | tr -d '|') |
Gavin Howard | aea5bdb | 2019-04-09 09:01:15 -0600 | [diff] [blame] | 207 | |
Gavin Howard | e0c9d29 | 2019-04-09 10:16:25 -0600 | [diff] [blame] | 208 | printf '%s' "$nlspath" |
Gavin Howard | aea5bdb | 2019-04-09 09:01:15 -0600 | [diff] [blame] | 209 | } |