blob: 95af66a9fe679f73027a594c4cfb8d1f3df13a8a [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 Howardf680e352019-04-01 13:28:48 -060054removeext() {
55
56 local name="$1"
57 shift
58
Gavin Howard013a2302019-04-01 15:00:39 -060059 printf "${name%.*}"
Gavin Howardf680e352019-04-01 13:28:48 -060060}
61
Gavin Howard7c715d62019-02-20 10:41:08 -070062die() {
63
64 local d="$1"
65 shift
66
67 local msg="$1"
68 shift
69
70 local name="$1"
71 shift
72
73 local err="$1"
74 shift
75
76 printf '\n'
77 printf '%s %s on test:\n' "$d" "$msg"
78 printf '\n'
79 printf ' %s\n' "$name"
80 printf '\n'
81 printf 'exiting...\n'
82 exit "$err"
83}
84
85checkcrash() {
86
87 local error="$1"
88 shift
89
90 local name="$1"
91 shift
92
93 if [ "$error" -gt 127 ]; then
94 die "$d" "crashed" "$name" "$error"
95 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
196 nlspath=$(substring_replace "$nlspath" "$i" "\n$i\n")
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
206 nlspath=$(printf '%s' "$nlspath" | tr -d '\n')
207
208 printf '%s\n' "$nlspath"
209}