2 # $NetBSD: msg_xlat.sh,v 1.7 2005/09/10 21:51:12 dsl Exp $
5 # Copyright (c) 2003 The NetBSD Foundation, Inc.
8 # This code is derived from software contributed to The NetBSD Foundation
11 # Redistribution and use in source and binary forms, with or without
12 # modification, are permitted provided that the following conditions
14 # 1. Redistributions of source code must retain the above copyright
15 # notice, this list of conditions and the following disclaimer.
16 # 2. Redistributions in binary form must reproduce the above copyright
17 # notice, this list of conditions and the following disclaimer in the
18 # documentation and/or other materials provided with the distribution.
20 # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 # POSSIBILITY OF SUCH DAMAGE.
35 echo "usage: msg_xlat.sh [-ci] [-d msg_defs.h] [-f fmt_count]" >&2
41 while getopts cd:f
:i f
46 f
) fmt_count
=$OPTARG;;
47 i
) IGNORE_MISSING_TRANSLATIONS
=y
;;
51 shift $
(($OPTIND - 1))
56 msg_long
="((msg)(long)"
64 # save stdin while we read the other files
67 # Read existing list of format arg counts
68 [ -n "$fmt_count" ] && {
69 exec <$fmt_count ||
exit 2
72 eval count_
$name=\
$count
76 # Read header file and set up map of message names to numbers
78 exec <$msg_defs ||
exit 2
80 while read define MSG_name number rest
82 [ -z "$number" -o -n "$rest" ] && continue
83 [ "$define" = "#define" ] ||
continue
84 name
="${MSG_name#MSG_}"
85 [ "$name" = "${MSG_name}" ] && continue
86 msg_number
="${number#$msg_long}"
87 [ "$msg_number" = "$number" ] && continue
88 msg_number
="${msg_number%$close_paren}"
90 eval $MSG_name=$msg_number
91 eval MSGNUM_
$msg_number=\
$MSG_name
92 # eval echo \$$MSG_name \$MSGNUM_$msg_number
95 last_msg_number
="$msg_number"
97 # Read message definition file and set up map of munbers to strings
111 [ "$1" = message
] ||
continue
113 eval number
=\
$MSG_$name
114 [ -z "$number" ] && {
115 echo "ERROR: unknown message \"$name\"" >&2
116 [ -n "$IGNORE_MISSING_TRANSLATIONS" ] || rval
=1
119 l
=${line#*$open_brace}
120 [ "$l" = "$line" ] && continue
124 l
="${line#$open_brace}"
125 [ "$l" = "$line" ] && continue
127 } || msg
="$msg$nl$line"
128 m
="${msg%$close_brace}"
129 [ "$m" = "$msg" ] && {
130 # Allow <tab>*/* comment */ (eg XXX translate)
131 m
="${msg%%$close_brace*$slash[*]*[*]$slash}"
135 # We need the %b to expand the \n that exist in the message file
136 msg
="$(printf "%bz
" "${m#$open_brace}")"
138 eval old
=\"\
$MSGTEXT_$number\"
139 [ -n "$old" -a "$number" != unknown
] && {
140 echo "ERROR: Two translations for message \"$name\"" >&2
141 [ -n "$IGNORE_MISSING_TRANSLATIONS" ] || rval
=1
143 eval MSGTEXT_
$number=\"\
${msg}\"
149 [ -z "$count_fmtargs" -a -z "$fmt_count" ] && continue
153 [ -n "$count_fmtargs" ] && {
157 eval count
=\
${count_$number:-unknown}
158 [ "$count" = $# ] ||
{
159 echo "ERROR: Wrong number of format specifiers in \"$sv_name\", got $#, expected $count" >&2
160 [ -n "$IGNORE_MISSING_TRANSLATIONS" ] || rval
=1
164 [ -n "$count_fmtargs" ] && exit $rval
166 # Output the total number of messages and the offset of each in the file.
167 # Use ascii numbers because generating target-ordered binary numbers
168 # is just a smidgen tricky in the shell.
170 offset
="$(( 8 + $last_msg_number * 8 + 8 ))"
171 printf 'MSGTXTS\0%-7d\0' $last_msg_number
175 msgnum
="$(( $msgnum + 1 ))"
176 [ "$msgnum" -le "$last_msg_number" ]
178 eval msg
=\
${MSGTEXT_$msgnum}
180 eval echo "ERROR: No translation for message \$MSGNUM_$msgnum" >&2
182 [ -n "$IGNORE_MISSING_TRANSLATIONS" ] || rval
=1
185 printf '%-7d\0' $offset
186 offset
="$(( $offset + ${#msg} + 1 ))"
189 # Finally output and null terminate the messages.
193 msgnum
="$(( $msgnum + 1 ))"
194 [ "$msgnum" -le "$last_msg_number" ]
196 eval msg
=\
${MSGTEXT_$msgnum}
197 [ -z "$msg" ] && continue