xref: /titanic_44/usr/src/cmd/msgfmt/gnu_msgs.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2001, 2002 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include "gnu_msgfmt.h"
30*7c478bd9Sstevel@tonic-gate #include "../../lib/libc/inc/msgfmt.h"
31*7c478bd9Sstevel@tonic-gate 
32*7c478bd9Sstevel@tonic-gate struct messages	*
search_msg(struct catalog * p,const char * id,unsigned int hash_val)33*7c478bd9Sstevel@tonic-gate search_msg(struct catalog *p, const char *id, unsigned int hash_val)
34*7c478bd9Sstevel@tonic-gate {
35*7c478bd9Sstevel@tonic-gate 	unsigned int	i, idx, inc;
36*7c478bd9Sstevel@tonic-gate 	struct messages	*m;
37*7c478bd9Sstevel@tonic-gate 
38*7c478bd9Sstevel@tonic-gate 	idx = hash_val % p->thash_size;
39*7c478bd9Sstevel@tonic-gate 	inc = 1 + (hash_val % (p->thash_size - 2));
40*7c478bd9Sstevel@tonic-gate 	if (!p->thash[idx])
41*7c478bd9Sstevel@tonic-gate 		return (NULL);
42*7c478bd9Sstevel@tonic-gate 
43*7c478bd9Sstevel@tonic-gate 	m = p->msg;
44*7c478bd9Sstevel@tonic-gate 	for (m = p->msg; (i = p->thash[idx]) != 0;
45*7c478bd9Sstevel@tonic-gate 		idx = (idx + inc) % p->thash_size) {
46*7c478bd9Sstevel@tonic-gate 		if (strcmp(m[i - 1].id, id) == 0) {
47*7c478bd9Sstevel@tonic-gate 			/* found */
48*7c478bd9Sstevel@tonic-gate 			return (&m[i - 1]);
49*7c478bd9Sstevel@tonic-gate 		}
50*7c478bd9Sstevel@tonic-gate 	}
51*7c478bd9Sstevel@tonic-gate 	return (NULL);
52*7c478bd9Sstevel@tonic-gate }
53*7c478bd9Sstevel@tonic-gate 
54*7c478bd9Sstevel@tonic-gate static int
msg_cmp(struct messages * m1,struct messages * m2)55*7c478bd9Sstevel@tonic-gate msg_cmp(struct messages *m1, struct messages *m2)
56*7c478bd9Sstevel@tonic-gate {
57*7c478bd9Sstevel@tonic-gate 	return (strcmp(m1->id, m2->id));
58*7c478bd9Sstevel@tonic-gate }
59*7c478bd9Sstevel@tonic-gate 
60*7c478bd9Sstevel@tonic-gate void
output_all_gnu_mo_files(void)61*7c478bd9Sstevel@tonic-gate output_all_gnu_mo_files(void)
62*7c478bd9Sstevel@tonic-gate {
63*7c478bd9Sstevel@tonic-gate 	struct catalog	*p, *op;
64*7c478bd9Sstevel@tonic-gate 	struct messages	*m;
65*7c478bd9Sstevel@tonic-gate 	size_t	id_len, str_len, id_off, str_off, ids_top, strs_top;
66*7c478bd9Sstevel@tonic-gate 	unsigned int	*hash_tbl;
67*7c478bd9Sstevel@tonic-gate 	unsigned int	hash_size;
68*7c478bd9Sstevel@tonic-gate 	unsigned int	num = 0, fnum = 0, unum = 0;
69*7c478bd9Sstevel@tonic-gate 	unsigned int	i, idx;
70*7c478bd9Sstevel@tonic-gate 	char	*ids, *strs;
71*7c478bd9Sstevel@tonic-gate 	struct msgtbl	*id_tbl, *str_tbl;
72*7c478bd9Sstevel@tonic-gate 	struct gnu_msg_info	header;
73*7c478bd9Sstevel@tonic-gate 	FILE	*out;
74*7c478bd9Sstevel@tonic-gate 
75*7c478bd9Sstevel@tonic-gate 	p = catalog_head;
76*7c478bd9Sstevel@tonic-gate 
77*7c478bd9Sstevel@tonic-gate 	while (p) {
78*7c478bd9Sstevel@tonic-gate 		num += p->nmsg;
79*7c478bd9Sstevel@tonic-gate 		fnum += p->fnum;
80*7c478bd9Sstevel@tonic-gate 		unum += p->unum;
81*7c478bd9Sstevel@tonic-gate 
82*7c478bd9Sstevel@tonic-gate 
83*7c478bd9Sstevel@tonic-gate 		free(p->thash);
84*7c478bd9Sstevel@tonic-gate 		if (p->nmsg == 0) {
85*7c478bd9Sstevel@tonic-gate 			/*
86*7c478bd9Sstevel@tonic-gate 			 * no message in this file
87*7c478bd9Sstevel@tonic-gate 			 * skip generating a mo
88*7c478bd9Sstevel@tonic-gate 			 */
89*7c478bd9Sstevel@tonic-gate 			goto skip;
90*7c478bd9Sstevel@tonic-gate 		}
91*7c478bd9Sstevel@tonic-gate 
92*7c478bd9Sstevel@tonic-gate 		if (p->header)
93*7c478bd9Sstevel@tonic-gate 			num--;
94*7c478bd9Sstevel@tonic-gate 
95*7c478bd9Sstevel@tonic-gate 		p->msg = (struct messages *)Xrealloc(p->msg,
96*7c478bd9Sstevel@tonic-gate 			sizeof (struct messages) * p->nmsg);
97*7c478bd9Sstevel@tonic-gate 
98*7c478bd9Sstevel@tonic-gate 		/*
99*7c478bd9Sstevel@tonic-gate 		 * Sort the message array
100*7c478bd9Sstevel@tonic-gate 		 */
101*7c478bd9Sstevel@tonic-gate 		qsort(p->msg, p->nmsg, sizeof (struct messages),
102*7c478bd9Sstevel@tonic-gate 			(int (*)(const void *, const void *))msg_cmp);
103*7c478bd9Sstevel@tonic-gate 
104*7c478bd9Sstevel@tonic-gate 
105*7c478bd9Sstevel@tonic-gate 		hash_size = find_prime(p->nmsg);
106*7c478bd9Sstevel@tonic-gate 		hash_tbl = (unsigned int *)Xcalloc(hash_size,
107*7c478bd9Sstevel@tonic-gate 			sizeof (unsigned int));
108*7c478bd9Sstevel@tonic-gate 
109*7c478bd9Sstevel@tonic-gate 
110*7c478bd9Sstevel@tonic-gate 		/* Setting Header info */
111*7c478bd9Sstevel@tonic-gate 		header.magic = GNU_MAGIC;
112*7c478bd9Sstevel@tonic-gate 		header.revision = GNU_REVISION;
113*7c478bd9Sstevel@tonic-gate 		header.num_of_str = p->nmsg;
114*7c478bd9Sstevel@tonic-gate 		header.off_msgid_tbl = sizeof (struct gnu_msg_info);
115*7c478bd9Sstevel@tonic-gate 		header.off_msgstr_tbl = sizeof (struct gnu_msg_info) +
116*7c478bd9Sstevel@tonic-gate 			p->nmsg * sizeof (struct msgtbl);
117*7c478bd9Sstevel@tonic-gate 		header.sz_hashtbl = hash_size;
118*7c478bd9Sstevel@tonic-gate 		header.off_hashtbl = header.off_msgstr_tbl +
119*7c478bd9Sstevel@tonic-gate 			p->nmsg * sizeof (struct msgtbl);
120*7c478bd9Sstevel@tonic-gate 
121*7c478bd9Sstevel@tonic-gate 		m = p->msg;
122*7c478bd9Sstevel@tonic-gate 
123*7c478bd9Sstevel@tonic-gate 		id_len = 0;
124*7c478bd9Sstevel@tonic-gate 		str_len = 0;
125*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < p->nmsg; i++) {
126*7c478bd9Sstevel@tonic-gate 			id_len += m[i].id_len;
127*7c478bd9Sstevel@tonic-gate 			str_len += m[i].str_len;
128*7c478bd9Sstevel@tonic-gate 		}
129*7c478bd9Sstevel@tonic-gate 		ids = (char *)Xmalloc(id_len);
130*7c478bd9Sstevel@tonic-gate 		strs = (char *)Xmalloc(str_len);
131*7c478bd9Sstevel@tonic-gate 		id_tbl = (struct msgtbl *)Xmalloc(sizeof (struct msgtbl) *
132*7c478bd9Sstevel@tonic-gate 			p->nmsg);
133*7c478bd9Sstevel@tonic-gate 		str_tbl = (struct msgtbl *)Xmalloc(sizeof (struct msgtbl) *
134*7c478bd9Sstevel@tonic-gate 			p->nmsg);
135*7c478bd9Sstevel@tonic-gate 		id_off = 0;
136*7c478bd9Sstevel@tonic-gate 		str_off = 0;
137*7c478bd9Sstevel@tonic-gate 		ids_top = header.off_hashtbl +
138*7c478bd9Sstevel@tonic-gate 			sizeof (unsigned int) * hash_size;
139*7c478bd9Sstevel@tonic-gate 		strs_top = ids_top + id_len;
140*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < p->nmsg; i++) {
141*7c478bd9Sstevel@tonic-gate 			/*
142*7c478bd9Sstevel@tonic-gate 			 * Set the hash table
143*7c478bd9Sstevel@tonic-gate 			 */
144*7c478bd9Sstevel@tonic-gate 			idx = get_hash_index(hash_tbl, m[i].hash, hash_size);
145*7c478bd9Sstevel@tonic-gate 			hash_tbl[idx] = i + 1;
146*7c478bd9Sstevel@tonic-gate 
147*7c478bd9Sstevel@tonic-gate 			/*
148*7c478bd9Sstevel@tonic-gate 			 * rearrange msgid and msgstr
149*7c478bd9Sstevel@tonic-gate 			 */
150*7c478bd9Sstevel@tonic-gate 			id_tbl[i].len = m[i].id_len - 1;
151*7c478bd9Sstevel@tonic-gate 			str_tbl[i].len = m[i].str_len - 1;
152*7c478bd9Sstevel@tonic-gate 			id_tbl[i].offset = id_off + ids_top;
153*7c478bd9Sstevel@tonic-gate 			str_tbl[i].offset = str_off + strs_top;
154*7c478bd9Sstevel@tonic-gate 			(void) memcpy(ids + id_off, m[i].id, m[i].id_len);
155*7c478bd9Sstevel@tonic-gate 			(void) memcpy(strs + str_off, m[i].str, m[i].str_len);
156*7c478bd9Sstevel@tonic-gate 			id_off += m[i].id_len;
157*7c478bd9Sstevel@tonic-gate 			str_off += m[i].str_len;
158*7c478bd9Sstevel@tonic-gate 			free(m[i].id);
159*7c478bd9Sstevel@tonic-gate 			free(m[i].str);
160*7c478bd9Sstevel@tonic-gate 		}
161*7c478bd9Sstevel@tonic-gate 
162*7c478bd9Sstevel@tonic-gate 		if ((out = fopen(p->fname, "w")) == NULL) {
163*7c478bd9Sstevel@tonic-gate 			error(gettext(ERR_OPEN_FAILED), p->fname);
164*7c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
165*7c478bd9Sstevel@tonic-gate 		}
166*7c478bd9Sstevel@tonic-gate 
167*7c478bd9Sstevel@tonic-gate 		/* writing header */
168*7c478bd9Sstevel@tonic-gate 		(void) fwrite(&header, sizeof (struct gnu_msg_info),
169*7c478bd9Sstevel@tonic-gate 			1, out);
170*7c478bd9Sstevel@tonic-gate 
171*7c478bd9Sstevel@tonic-gate 		/* writing msgid offset table */
172*7c478bd9Sstevel@tonic-gate 		(void) fwrite(id_tbl, sizeof (struct msgtbl),
173*7c478bd9Sstevel@tonic-gate 			p->nmsg, out);
174*7c478bd9Sstevel@tonic-gate 		/* writing msgstr offset table */
175*7c478bd9Sstevel@tonic-gate 		(void) fwrite(str_tbl, sizeof (struct msgtbl),
176*7c478bd9Sstevel@tonic-gate 			p->nmsg, out);
177*7c478bd9Sstevel@tonic-gate 		/* writing hash table */
178*7c478bd9Sstevel@tonic-gate 		(void) fwrite(hash_tbl, sizeof (unsigned int),
179*7c478bd9Sstevel@tonic-gate 			hash_size, out);
180*7c478bd9Sstevel@tonic-gate 		/* writing msgid table */
181*7c478bd9Sstevel@tonic-gate 		(void) fwrite(ids, id_len, 1, out);
182*7c478bd9Sstevel@tonic-gate 		/* writing msgstr table */
183*7c478bd9Sstevel@tonic-gate 		(void) fwrite(strs, str_len, 1, out);
184*7c478bd9Sstevel@tonic-gate 
185*7c478bd9Sstevel@tonic-gate 		(void) fclose(out);
186*7c478bd9Sstevel@tonic-gate 		free(id_tbl);
187*7c478bd9Sstevel@tonic-gate 		free(str_tbl);
188*7c478bd9Sstevel@tonic-gate 		free(hash_tbl);
189*7c478bd9Sstevel@tonic-gate 		free(ids);
190*7c478bd9Sstevel@tonic-gate 		free(strs);
191*7c478bd9Sstevel@tonic-gate skip:
192*7c478bd9Sstevel@tonic-gate 		free(p->fname);
193*7c478bd9Sstevel@tonic-gate 		free(p->msg);
194*7c478bd9Sstevel@tonic-gate 		op = p->next;
195*7c478bd9Sstevel@tonic-gate 		free(p);
196*7c478bd9Sstevel@tonic-gate 		p = op;
197*7c478bd9Sstevel@tonic-gate 	}
198*7c478bd9Sstevel@tonic-gate 	if (verbose_flag) {
199*7c478bd9Sstevel@tonic-gate 		diag(gettext(DIAG_RESULTS), num, fnum, unum);
200*7c478bd9Sstevel@tonic-gate 	}
201*7c478bd9Sstevel@tonic-gate }
202