blob: c3718180c28a6e70ed729b2e69274a6948bfe778 [file] [log] [blame]
Gavin Howard7c715d62019-02-20 10:41:08 -07001#! /bin/sh
2#
Gavin Howard7345cb92019-04-08 14:13:43 -06003# Copyright (c) 2018-2019 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 Howardaea5bdb2019-04-09 09:01:15 -060032 local f="$1"
Gavin Howard50080252019-02-20 15:27:43 -070033 shift
34
Gavin Howardaea5bdb2019-04-09 09:01:15 -060035 local arrow="-> "
36 local d=$(dirname "$f")
Gavin Howard50080252019-02-20 15:27:43 -070037
Gavin Howardaea5bdb2019-04-09 09:01:15 -060038 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 Howardbc021292019-04-09 10:04:01 -060045 f="$d/$link"
Gavin Howardaea5bdb2019-04-09 09:01:15 -060046 d=$(dirname "$f")
47 lsout=$(ls -dl "$f")
48 link=$(printf '%s' "${lsout#*$arrow}")
49 done
50
51 printf '%s' "${f##*$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
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 Howard8237f802019-04-11 12:23:03 -060080 str=$(printf '\n%s %s on test:\n\n %s\n' "$d" "$msg" "$name")
81
82 err_exit "$str" "$err"
Gavin Howard7c715d62019-02-20 10:41:08 -070083}
84
85checkcrash() {
86
87 local error="$1"
88 shift
89
90 local name="$1"
91 shift
92
93 if [ "$error" -gt 127 ]; then
Gavin Howard8237f802019-04-11 12:23:03 -060094 die "$d" "crashed ($error)" "$name" "$error"
Gavin Howard7c715d62019-02-20 10:41:08 -070095 fi
96}
97
98checktest()
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 Howard2425bf22019-04-08 17:05:45 -0600139
140substring_replace() {
141
Gavin Howardaea5bdb2019-04-09 09:01:15 -0600142 local str="$1"
143 shift
Gavin Howard2425bf22019-04-08 17:05:45 -0600144
Gavin Howardaea5bdb2019-04-09 09:01:15 -0600145 local needle="$1"
146 shift
147
148 local replacement="$1"
149 shift
Gavin Howard2425bf22019-04-08 17:05:45 -0600150
151 result=$(printf '%s' "$str" | sed -e "s!$needle!$replacement!g")
152
153 printf '%s\n' "$result"
154}
Gavin Howardaea5bdb2019-04-09 09:01:15 -0600155
156gen_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 Esserd86c0cd2019-04-09 21:40:47 +0200196 nlspath=$(substring_replace "$nlspath" "$i" "|$i|")
Gavin Howardaea5bdb2019-04-09 09:01:15 -0600197 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 Esserd86c0cd2019-04-09 21:40:47 +0200206 nlspath=$(printf '%s' "$nlspath" | tr -d '|')
Gavin Howardaea5bdb2019-04-09 09:01:15 -0600207
Gavin Howarde0c9d292019-04-09 10:16:25 -0600208 printf '%s' "$nlspath"
Gavin Howardaea5bdb2019-04-09 09:01:15 -0600209}