1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
|
#! /bin/sh
# $NetBSD: msg_xlat.sh,v 1.6 2021/10/11 18:46:34 rillig Exp $
#-
# Copyright (c) 2003 The NetBSD Foundation, Inc.
# All rights reserved.
#
# This code is derived from software contributed to The NetBSD Foundation
# by David Laight.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
PROG=$(basename "$0")
usage()
{
echo "Usage: $PROG [-ci] [-d msg_defs.h] [-f fmt_count]" >&2
exit 1
}
error()
{
echo "$PROG: ERROR $@" >&2
}
IGNORE_MISSING_TRANSLATIONS=false
count_fmtargs=false
msg_defs=msg_defs.h
while getopts cd:f:i f
do
case "$f" in
c) count_fmtargs=true;;
d) msg_defs=$OPTARG;;
f) fmt_count=$OPTARG;;
i) IGNORE_MISSING_TRANSLATIONS=true;;
*) usage;;
esac
done
shift $(($OPTIND - 1))
if [ "$#" -ne 0 ]; then usage; fi
nl='
'
msg_long='((msg)(long)'
close_paren=')'
open_brace='{'
close_brace='}'
slash=/
rval=0
# save stdin while we read the other files
exec 3<&0
# Read existing list of format arg counts
if [ -n "$fmt_count" ]; then
exec <$fmt_count || exit 2
while read name count
do
eval count_$name=\$count
done
fi
# Read header file and set up map of message names to numbers
exec <$msg_defs || exit 2
while read define MSG_name number rest
do
if [ -z "$number" ] || [ -n "$rest" ]; then continue; fi
if [ "$define" != "#define" ]; then continue; fi
name="${MSG_name#MSG_}"
if [ "$name" = "${MSG_name}" ]; then continue; fi
msg_number="${number#$msg_long}"
if [ "$msg_number" = "$number" ]; then continue; fi
msg_number="${msg_number%$close_paren}"
eval $MSG_name=$msg_number
eval MSGNUM_$msg_number=\$MSG_name
# eval echo \$$MSG_name \$MSGNUM_$msg_number
done
last_msg_number="$msg_number"
# Read message definition file and set up map of numbers to strings
exec <&3 3<&-
name=
msg=
while
IFS=
read -r line
do
if [ -z "$name" ]; then
IFS=" "
set -- $line
if [ "$1" != message ]; then continue; fi
name="$2"
eval number=\$MSG_$name
if [ -z "$number" ]; then
error "unknown message \"$name\""
$IGNORE_MISSING_TRANSLATIONS || rval=1
number=unknown
fi
l=${line#*$open_brace}
if [ "$l" = "$line" ]; then continue; fi
line="{$l"
fi
if [ -z "$msg" ]; then
l=${line#$open_brace}
if [ "$l" = "$line" ]; then continue; fi
msg="$line"
else
msg="$msg$nl$line"
fi
m=${msg%$close_brace}
if [ "$m" = "$msg" ]; then
# Allow <tab>*/* comment */ (eg XXX translate)
m=${msg%%$close_brace*$slash[*]*[*]$slash}
if [ "$m" = "$msg" ]; then continue; fi
fi
# We need the %b to expand the \n that exist in the message file
msg=$(printf %bz "${m#$open_brace}")
msg=${msg%z}
eval old=\"\$MSGTEXT_$number\"
if [ -n "$old" ] && [ "$number" != unknown ]; then
error "Two translations for message \"$name\""
$IGNORE_MISSING_TRANSLATIONS || rval=1
fi
eval MSGTEXT_$number=\"\${msg}\"
# echo $number $msg
sv_name="$name"
sv_msg="$msg"
name=
msg=
if ! $count_fmtargs && [ -z "$fmt_count" ]; then continue; fi
IFS=%
set -- $sv_msg
# For our purposes, empty messages are the same as words without %
if [ $# -eq 0 ]; then set -- x; fi
if $count_fmtargs; then
echo $number $#
continue
fi
eval count=\${count_$number:-unknown}
if [ "$count" -ne $# ]; then
error "Wrong number of format specifiers in \"$sv_name\", got $#, expected $count"
$IGNORE_MISSING_TRANSLATIONS || rval=1
fi
done
unset IFS
if $count_fmtargs; then exit $rval; fi
# Output the total number of messages and the offset of each in the file.
# Use ascii numbers because generating target-ordered binary numbers
# is just a smidgen tricky in the shell.
offset=$(( 8 + $last_msg_number * 8 + 8 ))
printf 'MSGTXTS\0%-7d\0' $last_msg_number
msgnum=0
while
msgnum=$(( $msgnum + 1 ))
[ "$msgnum" -le "$last_msg_number" ]
do
eval msg=\${MSGTEXT_$msgnum}
if [ -z "$msg" ]; then
eval error "No translation for message \$MSGNUM_$msgnum"
printf '%-7d\0' 0
$IGNORE_MISSING_TRANSLATIONS || rval=1
continue
fi
printf '%-7d\0' $offset
offset=$(( $offset + ${#msg} + 1 ))
done
# Finally output and null terminate the messages.
msgnum=0
while
msgnum=$(( $msgnum + 1 ))
[ "$msgnum" -le "$last_msg_number" ]
do
eval msg=\${MSGTEXT_$msgnum}
if [ -z "$msg" ]; then continue; fi
printf '%s\0' "$msg"
done
exit $rval
|