xref: /illumos-gate/usr/src/cmd/msgfmt/gnu_msgs.c (revision 2a8bcb4e)
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 #include "gnu_msgfmt.h"
28*7c478bd9Sstevel@tonic-gate #include "../../lib/libc/inc/msgfmt.h"
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate struct messages	*
search_msg(struct catalog * p,const char * id,unsigned int hash_val)31*7c478bd9Sstevel@tonic-gate search_msg(struct catalog *p, const char *id, unsigned int hash_val)
32*7c478bd9Sstevel@tonic-gate {
33*7c478bd9Sstevel@tonic-gate 	unsigned int	i, idx, inc;
34*7c478bd9Sstevel@tonic-gate 	struct messages	*m;
35*7c478bd9Sstevel@tonic-gate 
36*7c478bd9Sstevel@tonic-gate 	idx = hash_val % p->thash_size;
37*7c478bd9Sstevel@tonic-gate 	inc = 1 + (hash_val % (p->thash_size - 2));
38*7c478bd9Sstevel@tonic-gate 	if (!p->thash[idx])
39*7c478bd9Sstevel@tonic-gate 		return (NULL);
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate 	m = p->msg;
42*7c478bd9Sstevel@tonic-gate 	for (m = p->msg; (i = p->thash[idx]) != 0;
43*7c478bd9Sstevel@tonic-gate 		idx = (idx + inc) % p->thash_size) {
44*7c478bd9Sstevel@tonic-gate 		if (strcmp(m[i - 1].id, id) == 0) {
45*7c478bd9Sstevel@tonic-gate 			/* found */
46*7c478bd9Sstevel@tonic-gate 			return (&m[i - 1]);
47*7c478bd9Sstevel@tonic-gate 		}
48*7c478bd9Sstevel@tonic-gate 	}
49*7c478bd9Sstevel@tonic-gate 	return (NULL);
50*7c478bd9Sstevel@tonic-gate }
51*7c478bd9Sstevel@tonic-gate 
52*7c478bd9Sstevel@tonic-gate static int
msg_cmp(struct messages * m1,struct messages * m2)53*7c478bd9Sstevel@tonic-gate msg_cmp(struct messages *m1, struct messages *m2)
54*7c478bd9Sstevel@tonic-gate {
55*7c478bd9Sstevel@tonic-gate 	return (strcmp(m1->id, m2->id));
56*7c478bd9Sstevel@tonic-gate }
57*7c478bd9Sstevel@tonic-gate 
58*7c478bd9Sstevel@tonic-gate void
output_all_gnu_mo_files(void)59*7c478bd9Sstevel@tonic-gate output_all_gnu_mo_files(void)
60*7c478bd9Sstevel@tonic-gate {
61*7c478bd9Sstevel@tonic-gate 	struct catalog	*p, *op;
62*7c478bd9Sstevel@tonic-gate 	struct messages	*m;
63*7c478bd9Sstevel@tonic-gate 	size_t	id_len, str_len, id_off, str_off, ids_top, strs_top;
64*7c478bd9Sstevel@tonic-gate 	unsigned int	*hash_tbl;
65*7c478bd9Sstevel@tonic-gate 	unsigned int	hash_size;
66*7c478bd9Sstevel@tonic-gate 	unsigned int	num = 0, fnum = 0, unum = 0;
67*7c478bd9Sstevel@tonic-gate 	unsigned int	i, idx;
68*7c478bd9Sstevel@tonic-gate 	char	*ids, *strs;
69*7c478bd9Sstevel@tonic-gate 	struct msgtbl	*id_tbl, *str_tbl;
70*7c478bd9Sstevel@tonic-gate 	struct gnu_msg_info	header;
71*7c478bd9Sstevel@tonic-gate 	FILE	*out;
72*7c478bd9Sstevel@tonic-gate 
73*7c478bd9Sstevel@tonic-gate 	p = catalog_head;
74*7c478bd9Sstevel@tonic-gate 
75*7c478bd9Sstevel@tonic-gate 	while (p) {
76*7c478bd9Sstevel@tonic-gate 		num += p->nmsg;
77*7c478bd9Sstevel@tonic-gate 		fnum += p->fnum;
78*7c478bd9Sstevel@tonic-gate 		unum += p->unum;
79*7c478bd9Sstevel@tonic-gate 
80*7c478bd9Sstevel@tonic-gate 
81*7c478bd9Sstevel@tonic-gate 		free(p->thash);
82*7c478bd9Sstevel@tonic-gate 		if (p->nmsg == 0) {
83*7c478bd9Sstevel@tonic-gate 			/*
84*7c478bd9Sstevel@tonic-gate 			 * no message in this file
85*7c478bd9Sstevel@tonic-gate 			 * skip generating a mo
86*7c478bd9Sstevel@tonic-gate 			 */
87*7c478bd9Sstevel@tonic-gate 			goto skip;
88*7c478bd9Sstevel@tonic-gate 		}
89*7c478bd9Sstevel@tonic-gate 
90*7c478bd9Sstevel@tonic-gate 		if (p->header)
91*7c478bd9Sstevel@tonic-gate 			num--;
92*7c478bd9Sstevel@tonic-gate 
93*7c478bd9Sstevel@tonic-gate 		p->msg = (struct messages *)Xrealloc(p->msg,
94*7c478bd9Sstevel@tonic-gate 			sizeof (struct messages) * p->nmsg);
95*7c478bd9Sstevel@tonic-gate 
96*7c478bd9Sstevel@tonic-gate 		/*
97*7c478bd9Sstevel@tonic-gate 		 * Sort the message array
98*7c478bd9Sstevel@tonic-gate 		 */
99*7c478bd9Sstevel@tonic-gate 		qsort(p->msg, p->nmsg, sizeof (struct messages),
100*7c478bd9Sstevel@tonic-gate 			(int (*)(const void *, const void *))msg_cmp);
101*7c478bd9Sstevel@tonic-gate 
102*7c478bd9Sstevel@tonic-gate 
103*7c478bd9Sstevel@tonic-gate 		hash_size = find_prime(p->nmsg);
104*7c478bd9Sstevel@tonic-gate 		hash_tbl = (unsigned int *)Xcalloc(hash_size,
105*7c478bd9Sstevel@tonic-gate 			sizeof (unsigned int));
106*7c478bd9Sstevel@tonic-gate 
107*7c478bd9Sstevel@tonic-gate 
108*7c478bd9Sstevel@tonic-gate 		/* Setting Header info */
109*7c478bd9Sstevel@tonic-gate 		header.magic = GNU_MAGIC;
110*7c478bd9Sstevel@tonic-gate 		header.revision = GNU_REVISION;
111*7c478bd9Sstevel@tonic-gate 		header.num_of_str = p->nmsg;
112*7c478bd9Sstevel@tonic-gate 		header.off_msgid_tbl = sizeof (struct gnu_msg_info);
113*7c478bd9Sstevel@tonic-gate 		header.off_msgstr_tbl = sizeof (struct gnu_msg_info) +
114*7c478bd9Sstevel@tonic-gate 			p->nmsg * sizeof (struct msgtbl);
115*7c478bd9Sstevel@tonic-gate 		header.sz_hashtbl = hash_size;
116*7c478bd9Sstevel@tonic-gate 		header.off_hashtbl = header.off_msgstr_tbl +
117*7c478bd9Sstevel@tonic-gate 			p->nmsg * sizeof (struct msgtbl);
118*7c478bd9Sstevel@tonic-gate 
119*7c478bd9Sstevel@tonic-gate 		m = p->msg;
120*7c478bd9Sstevel@tonic-gate 
121*7c478bd9Sstevel@tonic-gate 		id_len = 0;
122*7c478bd9Sstevel@tonic-gate 		str_len = 0;
123*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < p->nmsg; i++) {
124*7c478bd9Sstevel@tonic-gate 			id_len += m[i].id_len;
125*7c478bd9Sstevel@tonic-gate 			str_len += m[i].str_len;
126*7c478bd9Sstevel@tonic-gate 		}
127*7c478bd9Sstevel@tonic-gate 		ids = (char *)Xmalloc(id_len);
128*7c478bd9Sstevel@tonic-gate 		strs = (char *)Xmalloc(str_len);
129*7c478bd9Sstevel@tonic-gate 		id_tbl = (struct msgtbl *)Xmalloc(sizeof (struct msgtbl) *
130*7c478bd9Sstevel@tonic-gate 			p->nmsg);
131*7c478bd9Sstevel@tonic-gate 		str_tbl = (struct msgtbl *)Xmalloc(sizeof (struct msgtbl) *
132*7c478bd9Sstevel@tonic-gate 			p->nmsg);
133*7c478bd9Sstevel@tonic-gate 		id_off = 0;
134*7c478bd9Sstevel@tonic-gate 		str_off = 0;
135*7c478bd9Sstevel@tonic-gate 		ids_top = header.off_hashtbl +
136*7c478bd9Sstevel@tonic-gate 			sizeof (unsigned int) * hash_size;
137*7c478bd9Sstevel@tonic-gate 		strs_top = ids_top + id_len;
138*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < p->nmsg; i++) {
139*7c478bd9Sstevel@tonic-gate 			/*
140*7c478bd9Sstevel@tonic-gate 			 * Set the hash table
141*7c478bd9Sstevel@tonic-gate 			 */
142*7c478bd9Sstevel@tonic-gate 			idx = get_hash_index(hash_tbl, m[i].hash, hash_size);
143*7c478bd9Sstevel@tonic-gate 			hash_tbl[idx] = i + 1;
144*7c478bd9Sstevel@tonic-gate 
145*7c478bd9Sstevel@tonic-gate 			/*
146*7c478bd9Sstevel@tonic-gate 			 * rearrange msgid and msgstr
147*7c478bd9Sstevel@tonic-gate 			 */
148*7c478bd9Sstevel@tonic-gate 			id_tbl[i].len = m[i].id_len - 1;
149*7c478bd9Sstevel@tonic-gate 			str_tbl[i].len = m[i].str_len - 1;
150*7c478bd9Sstevel@tonic-gate 			id_tbl[i].offset = id_off + ids_top;
151*7c478bd9Sstevel@tonic-gate 			str_tbl[i].offset = str_off + strs_top;
152*7c478bd9Sstevel@tonic-gate 			(void) memcpy(ids + id_off, m[i].id, m[i].id_len);
153*7c478bd9Sstevel@tonic-gate 			(void) memcpy(strs + str_off, m[i].str, m[i].str_len);
154*7c478bd9Sstevel@tonic-gate 			id_off += m[i].id_len;
155*7c478bd9Sstevel@tonic-gate 			str_off += m[i].str_len;
156*7c478bd9Sstevel@tonic-gate 			free(m[i].id);
157*7c478bd9Sstevel@tonic-gate 			free(m[i].str);
158*7c478bd9Sstevel@tonic-gate 		}
159*7c478bd9Sstevel@tonic-gate 
160*7c478bd9Sstevel@tonic-gate 		if ((out = fopen(p->fname, "w")) == NULL) {
161*7c478bd9Sstevel@tonic-gate 			error(gettext(ERR_OPEN_FAILED), p->fname);
162*7c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
163*7c478bd9Sstevel@tonic-gate 		}
164*7c478bd9Sstevel@tonic-gate 
165*7c478bd9Sstevel@tonic-gate 		/* writing header */
166*7c478bd9Sstevel@tonic-gate 		(void) fwrite(&header, sizeof (struct gnu_msg_info),
167*7c478bd9Sstevel@tonic-gate 			1, out);
168*7c478bd9Sstevel@tonic-gate 
169*7c478bd9Sstevel@tonic-gate 		/* writing msgid offset table */
170*7c478bd9Sstevel@tonic-gate 		(void) fwrite(id_tbl, sizeof (struct msgtbl),
171*7c478bd9Sstevel@tonic-gate 			p->nmsg, out);
172*7c478bd9Sstevel@tonic-gate 		/* writing msgstr offset table */
173*7c478bd9Sstevel@tonic-gate 		(void) fwrite(str_tbl, sizeof (struct msgtbl),
174*7c478bd9Sstevel@tonic-gate 			p->nmsg, out);
175*7c478bd9Sstevel@tonic-gate 		/* writing hash table */
176*7c478bd9Sstevel@tonic-gate 		(void) fwrite(hash_tbl, sizeof (unsigned int),
177*7c478bd9Sstevel@tonic-gate 			hash_size, out);
178*7c478bd9Sstevel@tonic-gate 		/* writing msgid table */
179*7c478bd9Sstevel@tonic-gate 		(void) fwrite(ids, id_len, 1, out);
180*7c478bd9Sstevel@tonic-gate 		/* writing msgstr table */
181*7c478bd9Sstevel@tonic-gate 		(void) fwrite(strs, str_len, 1, out);
182*7c478bd9Sstevel@tonic-gate 
183*7c478bd9Sstevel@tonic-gate 		(void) fclose(out);
184*7c478bd9Sstevel@tonic-gate 		free(id_tbl);
185*7c478bd9Sstevel@tonic-gate 		free(str_tbl);
186*7c478bd9Sstevel@tonic-gate 		free(hash_tbl);
187*7c478bd9Sstevel@tonic-gate 		free(ids);
188*7c478bd9Sstevel@tonic-gate 		free(strs);
189*7c478bd9Sstevel@tonic-gate skip:
190*7c478bd9Sstevel@tonic-gate 		free(p->fname);
191*7c478bd9Sstevel@tonic-gate 		free(p->msg);
192*7c478bd9Sstevel@tonic-gate 		op = p->next;
193*7c478bd9Sstevel@tonic-gate 		free(p);
194*7c478bd9Sstevel@tonic-gate 		p = op;
195*7c478bd9Sstevel@tonic-gate 	}
196*7c478bd9Sstevel@tonic-gate 	if (verbose_flag) {
197*7c478bd9Sstevel@tonic-gate 		diag(gettext(DIAG_RESULTS), num, fnum, unum);
198*7c478bd9Sstevel@tonic-gate 	}
199*7c478bd9Sstevel@tonic-gate }
200