xref: /titanic_41/usr/src/cmd/msgfmt/gnu_po.y (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate %{
2*7c478bd9Sstevel@tonic-gate /*
3*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
4*7c478bd9Sstevel@tonic-gate  *
5*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
6*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
7*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
8*7c478bd9Sstevel@tonic-gate  * with the License.
9*7c478bd9Sstevel@tonic-gate  *
10*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
11*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
12*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
13*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
14*7c478bd9Sstevel@tonic-gate  *
15*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
16*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
17*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
18*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
19*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
20*7c478bd9Sstevel@tonic-gate  *
21*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
22*7c478bd9Sstevel@tonic-gate  *
23*7c478bd9Sstevel@tonic-gate  * Copyright (c) 2001 by Sun Microsystems, Inc.
24*7c478bd9Sstevel@tonic-gate  * All rights reserved.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
27*7c478bd9Sstevel@tonic-gate 
28*7c478bd9Sstevel@tonic-gate #include "gnu_msgfmt.h"
29*7c478bd9Sstevel@tonic-gate #include "gnu_lex.h"
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate static int	plural_index;
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate %}
34*7c478bd9Sstevel@tonic-gate 
35*7c478bd9Sstevel@tonic-gate %union {
36*7c478bd9Sstevel@tonic-gate 	char	*str;
37*7c478bd9Sstevel@tonic-gate 	int	num;
38*7c478bd9Sstevel@tonic-gate 	struct entry	msg;
39*7c478bd9Sstevel@tonic-gate 	struct ch	c;
40*7c478bd9Sstevel@tonic-gate }
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate %token	<num> DOMAIN
43*7c478bd9Sstevel@tonic-gate %token	<num> MSGID
44*7c478bd9Sstevel@tonic-gate %token	<num> MSGID_PLURAL
45*7c478bd9Sstevel@tonic-gate %token	<num> MSGSTR
46*7c478bd9Sstevel@tonic-gate %token	<num> NUM
47*7c478bd9Sstevel@tonic-gate %token	<str> STR
48*7c478bd9Sstevel@tonic-gate %token	<str> COMMENT
49*7c478bd9Sstevel@tonic-gate %token	<str> SYMBOL
50*7c478bd9Sstevel@tonic-gate %token	<c>	CHR
51*7c478bd9Sstevel@tonic-gate %type	<msg> message_string plural_messages plural_message
52*7c478bd9Sstevel@tonic-gate %%
53*7c478bd9Sstevel@tonic-gate 
54*7c478bd9Sstevel@tonic-gate start	:
55*7c478bd9Sstevel@tonic-gate 	| start po
56*7c478bd9Sstevel@tonic-gate 	;
57*7c478bd9Sstevel@tonic-gate 
58*7c478bd9Sstevel@tonic-gate po	: comment
59*7c478bd9Sstevel@tonic-gate 	| domain
60*7c478bd9Sstevel@tonic-gate 	| body
61*7c478bd9Sstevel@tonic-gate 	;
62*7c478bd9Sstevel@tonic-gate 
63*7c478bd9Sstevel@tonic-gate domain	: DOMAIN STR
64*7c478bd9Sstevel@tonic-gate 	{
65*7c478bd9Sstevel@tonic-gate 		handle_domain($2);
66*7c478bd9Sstevel@tonic-gate 	}
67*7c478bd9Sstevel@tonic-gate 	;
68*7c478bd9Sstevel@tonic-gate 
69*7c478bd9Sstevel@tonic-gate comment	: COMMENT
70*7c478bd9Sstevel@tonic-gate 	{
71*7c478bd9Sstevel@tonic-gate 		handle_comment($1);
72*7c478bd9Sstevel@tonic-gate 	}
73*7c478bd9Sstevel@tonic-gate 
74*7c478bd9Sstevel@tonic-gate body	: MSGID message_string MSGSTR message_string
75*7c478bd9Sstevel@tonic-gate 	{
76*7c478bd9Sstevel@tonic-gate 		struct entry	och1, och2;
77*7c478bd9Sstevel@tonic-gate 
78*7c478bd9Sstevel@tonic-gate 		och1.no = 1;
79*7c478bd9Sstevel@tonic-gate 		och1.num = $1;
80*7c478bd9Sstevel@tonic-gate 		och1.str = $2.str;
81*7c478bd9Sstevel@tonic-gate 		och1.len = $2.len;
82*7c478bd9Sstevel@tonic-gate 		och1.pos = NULL;
83*7c478bd9Sstevel@tonic-gate 
84*7c478bd9Sstevel@tonic-gate 		och2.no = 1;
85*7c478bd9Sstevel@tonic-gate 		och2.num = $3;
86*7c478bd9Sstevel@tonic-gate 		och2.str = $4.str;
87*7c478bd9Sstevel@tonic-gate 		och2.len = $4.len;
88*7c478bd9Sstevel@tonic-gate 		och2.pos = NULL;
89*7c478bd9Sstevel@tonic-gate 
90*7c478bd9Sstevel@tonic-gate 		handle_message(&och1, &och2);
91*7c478bd9Sstevel@tonic-gate 		clear_state();
92*7c478bd9Sstevel@tonic-gate 	}
93*7c478bd9Sstevel@tonic-gate 	| MSGID message_string MSGID_PLURAL
94*7c478bd9Sstevel@tonic-gate 		message_string {plural_index = 0;} plural_messages
95*7c478bd9Sstevel@tonic-gate 	{
96*7c478bd9Sstevel@tonic-gate 		size_t	len;
97*7c478bd9Sstevel@tonic-gate 		struct entry	och1, och2;
98*7c478bd9Sstevel@tonic-gate 		struct loc	*pos1;
99*7c478bd9Sstevel@tonic-gate 		char	*id_str;
100*7c478bd9Sstevel@tonic-gate 
101*7c478bd9Sstevel@tonic-gate 		len = $2.len + $4.len;
102*7c478bd9Sstevel@tonic-gate 		id_str = (char *)Xmalloc(len);
103*7c478bd9Sstevel@tonic-gate 		(void) memcpy(id_str, $2.str, $2.len);
104*7c478bd9Sstevel@tonic-gate 		(void) memcpy(id_str + $2.len, $4.str, $4.len);
105*7c478bd9Sstevel@tonic-gate 		free($2.str);
106*7c478bd9Sstevel@tonic-gate 		free($4.str);
107*7c478bd9Sstevel@tonic-gate 
108*7c478bd9Sstevel@tonic-gate 		pos1 = (struct loc *)Xmalloc(2 * sizeof (struct loc));
109*7c478bd9Sstevel@tonic-gate 		pos1[0].off = 0;
110*7c478bd9Sstevel@tonic-gate 		pos1[0].len = $2.len;
111*7c478bd9Sstevel@tonic-gate 		pos1[0].num = $1;
112*7c478bd9Sstevel@tonic-gate 		pos1[1].off = $2.len;
113*7c478bd9Sstevel@tonic-gate 		pos1[1].len = $4.len;
114*7c478bd9Sstevel@tonic-gate 		pos1[1].num = $3;
115*7c478bd9Sstevel@tonic-gate 		och1.no = 2;
116*7c478bd9Sstevel@tonic-gate 		och1.num = $1;
117*7c478bd9Sstevel@tonic-gate 		och1.str = id_str;
118*7c478bd9Sstevel@tonic-gate 		och1.len = len;
119*7c478bd9Sstevel@tonic-gate 		och1.pos = pos1;
120*7c478bd9Sstevel@tonic-gate 
121*7c478bd9Sstevel@tonic-gate 		och2 = $6;
122*7c478bd9Sstevel@tonic-gate 		handle_message(&och1, &och2);
123*7c478bd9Sstevel@tonic-gate 		clear_state();
124*7c478bd9Sstevel@tonic-gate 	}
125*7c478bd9Sstevel@tonic-gate 	| MSGID message_string
126*7c478bd9Sstevel@tonic-gate 	{
127*7c478bd9Sstevel@tonic-gate 		error(gettext(ERR_NO_MSGSTR), $1, cur_po);
128*7c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
129*7c478bd9Sstevel@tonic-gate 	}
130*7c478bd9Sstevel@tonic-gate 	| MSGID message_string MSGID_PLURAL message_string
131*7c478bd9Sstevel@tonic-gate 	{
132*7c478bd9Sstevel@tonic-gate 		error(gettext(ERR_NO_MSGSTRS), $1, cur_po);
133*7c478bd9Sstevel@tonic-gate 		/* NOTRECHED */
134*7c478bd9Sstevel@tonic-gate 	}
135*7c478bd9Sstevel@tonic-gate 	| MSGID message_string plural_messages
136*7c478bd9Sstevel@tonic-gate 	{
137*7c478bd9Sstevel@tonic-gate 		error(gettext(ERR_NO_MSGID_PLURAL), $1, cur_po);
138*7c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
139*7c478bd9Sstevel@tonic-gate 	}
140*7c478bd9Sstevel@tonic-gate 	;
141*7c478bd9Sstevel@tonic-gate 
142*7c478bd9Sstevel@tonic-gate message_string	: STR
143*7c478bd9Sstevel@tonic-gate 	{
144*7c478bd9Sstevel@tonic-gate 		$$.str = $1;
145*7c478bd9Sstevel@tonic-gate 		$$.len = strlen($1) + 1;
146*7c478bd9Sstevel@tonic-gate 	}
147*7c478bd9Sstevel@tonic-gate 	| message_string STR
148*7c478bd9Sstevel@tonic-gate 	{
149*7c478bd9Sstevel@tonic-gate 		size_t	len, len1, len2;
150*7c478bd9Sstevel@tonic-gate 		char	*str;
151*7c478bd9Sstevel@tonic-gate 
152*7c478bd9Sstevel@tonic-gate 		/* $1.len includes null-termination */
153*7c478bd9Sstevel@tonic-gate 		len1 = $1.len - 1;
154*7c478bd9Sstevel@tonic-gate 		len2 = strlen($2);
155*7c478bd9Sstevel@tonic-gate 
156*7c478bd9Sstevel@tonic-gate 		/* len doesn't include null-termination */
157*7c478bd9Sstevel@tonic-gate 		len = len1 + len2;
158*7c478bd9Sstevel@tonic-gate 
159*7c478bd9Sstevel@tonic-gate 		str = (char *)Xmalloc(len + 1);
160*7c478bd9Sstevel@tonic-gate 		(void) memcpy(str, $1.str, len1);
161*7c478bd9Sstevel@tonic-gate 		(void) memcpy(str + len1, $2, len2 + 1);
162*7c478bd9Sstevel@tonic-gate 		free($1.str);
163*7c478bd9Sstevel@tonic-gate 		free($2);
164*7c478bd9Sstevel@tonic-gate 		$$.str = str;
165*7c478bd9Sstevel@tonic-gate 		$$.len = len + 1;
166*7c478bd9Sstevel@tonic-gate 	}
167*7c478bd9Sstevel@tonic-gate 	;
168*7c478bd9Sstevel@tonic-gate 
169*7c478bd9Sstevel@tonic-gate plural_messages	: plural_message
170*7c478bd9Sstevel@tonic-gate 	{
171*7c478bd9Sstevel@tonic-gate 		$$ = $1;
172*7c478bd9Sstevel@tonic-gate 	}
173*7c478bd9Sstevel@tonic-gate 	| plural_messages plural_message
174*7c478bd9Sstevel@tonic-gate 	{
175*7c478bd9Sstevel@tonic-gate 		struct loc	*tmp;
176*7c478bd9Sstevel@tonic-gate 		size_t	len;
177*7c478bd9Sstevel@tonic-gate 		char	*plural_str;
178*7c478bd9Sstevel@tonic-gate 		int	no;
179*7c478bd9Sstevel@tonic-gate 
180*7c478bd9Sstevel@tonic-gate 		no = $1.no + 1;
181*7c478bd9Sstevel@tonic-gate 		tmp = (struct loc *)Xrealloc($1.pos,
182*7c478bd9Sstevel@tonic-gate 			no * sizeof (struct loc));
183*7c478bd9Sstevel@tonic-gate 		tmp[no - 1].off = $1.len;
184*7c478bd9Sstevel@tonic-gate 		tmp[no - 1].len = $2.len;
185*7c478bd9Sstevel@tonic-gate 		tmp[no - 1].num = $2.num;
186*7c478bd9Sstevel@tonic-gate 		free($2.pos);
187*7c478bd9Sstevel@tonic-gate 
188*7c478bd9Sstevel@tonic-gate 		len = $1.len + $2.len;
189*7c478bd9Sstevel@tonic-gate 		plural_str = (char *)Xmalloc(len);
190*7c478bd9Sstevel@tonic-gate 		(void) memcpy(plural_str, $1.str, $1.len);
191*7c478bd9Sstevel@tonic-gate 		(void) memcpy(plural_str + $1.len, $2.str, $2.len);
192*7c478bd9Sstevel@tonic-gate 
193*7c478bd9Sstevel@tonic-gate 		$$.no = no;
194*7c478bd9Sstevel@tonic-gate 		$$.num = $1.num;
195*7c478bd9Sstevel@tonic-gate 		$$.str = plural_str;
196*7c478bd9Sstevel@tonic-gate 		$$.len = len;
197*7c478bd9Sstevel@tonic-gate 		$$.pos = tmp;
198*7c478bd9Sstevel@tonic-gate 		free($1.str);
199*7c478bd9Sstevel@tonic-gate 		free($2.str);
200*7c478bd9Sstevel@tonic-gate 	}
201*7c478bd9Sstevel@tonic-gate 	;
202*7c478bd9Sstevel@tonic-gate 
203*7c478bd9Sstevel@tonic-gate plural_message	: MSGSTR '[' NUM ']' message_string
204*7c478bd9Sstevel@tonic-gate 	{
205*7c478bd9Sstevel@tonic-gate 		struct loc	*pos;
206*7c478bd9Sstevel@tonic-gate 		if ($3 != plural_index) {
207*7c478bd9Sstevel@tonic-gate 			error(gettext(ERR_INVALID_PLURALS), $1, cur_po);
208*7c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
209*7c478bd9Sstevel@tonic-gate 		}
210*7c478bd9Sstevel@tonic-gate 		plural_index++;
211*7c478bd9Sstevel@tonic-gate 		pos = (struct loc *)Xmalloc(sizeof (struct loc));
212*7c478bd9Sstevel@tonic-gate 		pos->off = 0;
213*7c478bd9Sstevel@tonic-gate 		pos->len = $5.len;
214*7c478bd9Sstevel@tonic-gate 		pos->num = $1;
215*7c478bd9Sstevel@tonic-gate 		$$.no = 1;
216*7c478bd9Sstevel@tonic-gate 		$$.num = $1;
217*7c478bd9Sstevel@tonic-gate 		$$.str = $5.str;
218*7c478bd9Sstevel@tonic-gate 		$$.len = $5.len;
219*7c478bd9Sstevel@tonic-gate 		$$.pos = pos;
220*7c478bd9Sstevel@tonic-gate 	}
221*7c478bd9Sstevel@tonic-gate 	;
222*7c478bd9Sstevel@tonic-gate %%
223*7c478bd9Sstevel@tonic-gate void
224*7c478bd9Sstevel@tonic-gate yyerror(const char *err)
225*7c478bd9Sstevel@tonic-gate {
226*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
227*7c478bd9Sstevel@tonic-gate 		gettext(ERR_LOCATION), cur_line, cur_po);
228*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s\n", err);
229*7c478bd9Sstevel@tonic-gate 
230*7c478bd9Sstevel@tonic-gate 	exit(1);
231*7c478bd9Sstevel@tonic-gate }
232