xref: /titanic_50/usr/src/lib/libc/port/i18n/gettext.h (revision b599bd937c305a895426e8c412ca920ce7824850)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5f13ac639Smuffin  * Common Development and Distribution License (the "License").
6f13ac639Smuffin  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21f13ac639Smuffin 
227c478bd9Sstevel@tonic-gate /*
23f13ac639Smuffin  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #ifndef	_LIBC_PORT_I18N_GETTEXT_H
287c478bd9Sstevel@tonic-gate #define	_LIBC_PORT_I18N_GETTEXT_H
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #include <sys/param.h>
317c478bd9Sstevel@tonic-gate #include <iconv.h>
327c478bd9Sstevel@tonic-gate #include <synch.h>
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
357c478bd9Sstevel@tonic-gate extern "C" {
367c478bd9Sstevel@tonic-gate #endif
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate /* Type of MO file */
39f13ac639Smuffin #define	T_MO_MASK		0x07
40f13ac639Smuffin #define	T_SUN_MO		0x01
41f13ac639Smuffin #define	T_GNU_MO		0x02
42f13ac639Smuffin #define	T_ILL_MO		0x04
43f13ac639Smuffin 
44f13ac639Smuffin #define	T_GNU_MASK		0x300
45f13ac639Smuffin #define	T_GNU_SWAPPED		0x100
46f13ac639Smuffin #define	T_GNU_REV1		0x200
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate #define	TP_BINDING	0
497c478bd9Sstevel@tonic-gate #define	TP_CODESET	1
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate /* Msg_g_node->flag */
527c478bd9Sstevel@tonic-gate #define	ST_CHK	0x1			/* header has been checked? */
537c478bd9Sstevel@tonic-gate #define	ST_SWP	0x2			/* reversed endian? */
54f13ac639Smuffin #define	ST_REV1	0x4			/* Revision 1 */
55f13ac639Smuffin 
56f13ac639Smuffin /*
57f13ac639Smuffin  * msg_pack->status:
58f13ac639Smuffin  * interaction between handle_lang() and handle_mo()
59f13ac639Smuffin  */
60f13ac639Smuffin #define	ST_GNU_MSG_FOUND	0x1	/* valid msg found in GNU MO */
61f13ac639Smuffin #define	ST_GNU_MO_FOUND		0x2	/* GNU MO found */
62f13ac639Smuffin #define	ST_SUN_MO_FOUND		0x4	/* Sun MO found */
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate typedef struct domain_binding {
657c478bd9Sstevel@tonic-gate 	char	*domain;	/* domain name */
667c478bd9Sstevel@tonic-gate 	char	*binding;	/* binding directory */
677c478bd9Sstevel@tonic-gate 	char	*codeset;	/* codeset */
687c478bd9Sstevel@tonic-gate 	struct domain_binding	*next;
697c478bd9Sstevel@tonic-gate } Dbinding;
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate /*
727c478bd9Sstevel@tonic-gate  * this structure is used for preserving nlspath templates before
737c478bd9Sstevel@tonic-gate  * passing them to bindtextdomain():
747c478bd9Sstevel@tonic-gate  */
757c478bd9Sstevel@tonic-gate typedef struct nlstmp {
767c478bd9Sstevel@tonic-gate 	char	pathname[MAXPATHLEN];	/* the full pathname to file */
77f13ac639Smuffin 	size_t	len;			/* length of pathname */
787c478bd9Sstevel@tonic-gate 	struct nlstmp	*next;		/* link to the next entry */
797c478bd9Sstevel@tonic-gate } Nlstmp;
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate typedef struct {
827c478bd9Sstevel@tonic-gate 	struct msg_info	*msg_file_info;	/* information of msg file */
837c478bd9Sstevel@tonic-gate 	struct msg_struct	*msg_list;	/* message list */
847c478bd9Sstevel@tonic-gate 	char	*msg_ids;		/* actual message ids */
857c478bd9Sstevel@tonic-gate 	char	*msg_strs;		/* actual message strs */
867c478bd9Sstevel@tonic-gate } Msg_s_node;
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate typedef struct expr	*plural_expr_t;
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate typedef struct {
91f13ac639Smuffin 	unsigned int	len;	/* length of the expanded str of macro */
92f13ac639Smuffin 	const char	*ptr;	/* pointer to the expanded str of macro */
93f13ac639Smuffin } gnu_d_macro_t;
94f13ac639Smuffin 
95f13ac639Smuffin typedef struct {
96f13ac639Smuffin 	struct gnu_msg_info	*msg_file_info;
97f13ac639Smuffin 	struct gnu_msg_rev1_info	*rev1_header;
98f13ac639Smuffin 	size_t	fsize;			/* size of the GNU mo file */
99f13ac639Smuffin 	uint32_t	flag;		/* status */
100f13ac639Smuffin 	uint32_t	num_of_str;	/* number of static msgs */
101f13ac639Smuffin 	uint32_t	num_of_d_str;	/* number of dynamic msgs */
102f13ac639Smuffin 	uint32_t	hash_size;	/* hash table size  */
103f13ac639Smuffin 	uint32_t	*hash_table;	/* hash table */
104f13ac639Smuffin 	struct gnu_msg_ent	*msg_tbl[2]; 	/* msgid/str entries */
105f13ac639Smuffin 	struct gnu_msg_ent	*d_msg[2];	/* dynamic msgid/str entries */
106f13ac639Smuffin 	char	*mchunk;	/* pointer to memory chunk of dynamic strs */
1077c478bd9Sstevel@tonic-gate 	char	*src_encoding;	/* src encoding */
1087c478bd9Sstevel@tonic-gate 	char	*dst_encoding;	/* dst encoding */
109f13ac639Smuffin 	unsigned int	nplurals;	/* number of plural forms */
1107c478bd9Sstevel@tonic-gate 	plural_expr_t	plural;		/* plural expression */
1117c478bd9Sstevel@tonic-gate 	iconv_t	fd;			/* iconv descriptor */
112f13ac639Smuffin 	uint32_t	**conv_msgstr;	/* code-converted msgstr */
1137c478bd9Sstevel@tonic-gate } Msg_g_node;
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate typedef struct msg_node {
116f13ac639Smuffin 	uint32_t	hashid;	/* hashed value of the domain name */
117f13ac639Smuffin 	uint16_t	type;	/* T_SUN_MO, T_GNU_MO, or T_ILL_MO */
118f13ac639Smuffin 	uint16_t	trusted;	/* is this a trusted source? */
1197c478bd9Sstevel@tonic-gate 	char	*path;		/* name of message catalog */
1207c478bd9Sstevel@tonic-gate 	union {
1217c478bd9Sstevel@tonic-gate 		Msg_s_node	*sunmsg;
1227c478bd9Sstevel@tonic-gate 		Msg_g_node	*gnumsg;
1237c478bd9Sstevel@tonic-gate 	} msg;
1247c478bd9Sstevel@tonic-gate 	struct msg_node	*next;	/* link to the next */
1257c478bd9Sstevel@tonic-gate } Msg_node;
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate typedef struct nls_node {
1287c478bd9Sstevel@tonic-gate 	char	*domain;		/* key: domain name */
1297c478bd9Sstevel@tonic-gate 	char	*locale;		/* key: locale name */
1307c478bd9Sstevel@tonic-gate 	char	*nlspath;		/* key: NLSPATH */
1317c478bd9Sstevel@tonic-gate 	char	*ppaths;		/* value: expanded path */
1327c478bd9Sstevel@tonic-gate 	struct nls_node	*next;	/* link to the next */
1337c478bd9Sstevel@tonic-gate } Nls_node;
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate typedef struct {
1367c478bd9Sstevel@tonic-gate 	char	*cur_domain;	/* current domain */
1377c478bd9Sstevel@tonic-gate 	Dbinding	*dbind;		/* domain binding */
138f13ac639Smuffin 	Msg_node	*m_node; 	/* link to the Msg_node cache */
139f13ac639Smuffin 	Nls_node	*n_node; 	/* link to the Nls_node cache */
1407c478bd9Sstevel@tonic-gate 	Msg_node	*c_m_node;	/* link to the current Msg_node */
1417c478bd9Sstevel@tonic-gate 	Nls_node	*c_n_node;	/* link to the current Nls_node */
1427c478bd9Sstevel@tonic-gate } Gettext_t;
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate struct msg_pack {
1457c478bd9Sstevel@tonic-gate 	const char	*msgid1;	/* msgid1 argument */
1467c478bd9Sstevel@tonic-gate 	const char	*msgid2;	/* msgid2 argument */
1477c478bd9Sstevel@tonic-gate 	char	*msgfile;		/* msg catalog file to open */
1487c478bd9Sstevel@tonic-gate 	char	*domain;		/* textdomain name */
1497c478bd9Sstevel@tonic-gate 	char	*binding;		/* binding */
150*b599bd93SRobert Mustacchi 	const char	*locale;	/* locale */
1517c478bd9Sstevel@tonic-gate 	char	*language;		/* LANGUAGE env */
1527c478bd9Sstevel@tonic-gate 	caddr_t	addr;			/* mmap'ed address */
1537c478bd9Sstevel@tonic-gate 	size_t	fsz;			/* file size */
154f13ac639Smuffin 	uint32_t	hash_domain;	/* hash ID of domain */
155f13ac639Smuffin 	uint32_t	domain_len;	/* length of domain */
1567c478bd9Sstevel@tonic-gate 	unsigned int	n;		/* n argument */
1577c478bd9Sstevel@tonic-gate 	int	category;		/* category argument */
1587c478bd9Sstevel@tonic-gate 	int	plural;			/* plural or not */
1597c478bd9Sstevel@tonic-gate 	int	nlsp;			/* nlsp */
1607c478bd9Sstevel@tonic-gate 	int	trusted;		/* trusted msg catalog or not */
161f13ac639Smuffin 	int	status;			/* status */
1627c478bd9Sstevel@tonic-gate };
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate #define	DEFAULT_DOMAIN		"messages"
1657c478bd9Sstevel@tonic-gate #define	DEFAULT_BINDING		_DFLT_LOC_PATH
1667c478bd9Sstevel@tonic-gate #define	MSGFILESUFFIX		".mo"
1677c478bd9Sstevel@tonic-gate #define	MSGFILESUFFIXLEN	(sizeof (MSGFILESUFFIX) - 1)
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate #define	CURRENT_DOMAIN(gt)	(gt)->cur_domain
1707c478bd9Sstevel@tonic-gate #define	FIRSTBIND(gt)	(gt)->dbind
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate #define	DFLTMSG(result, msgid1, msgid2, n, plural) \
1737c478bd9Sstevel@tonic-gate 	result = (plural ? \
1747c478bd9Sstevel@tonic-gate 		((n == 1) ? (char *)msgid1 : (char *)msgid2) : \
1757c478bd9Sstevel@tonic-gate 		(char *)msgid1)
1767c478bd9Sstevel@tonic-gate 
177f13ac639Smuffin #define	ROUND(m, s)	if ((m) % (s)) (m) += ((s) - ((m) % (s)))
178f13ac639Smuffin 
1797c478bd9Sstevel@tonic-gate #define	SWAP(p, ui32) \
1807c478bd9Sstevel@tonic-gate 	(((p)->flag & ST_SWP) ? doswap32(ui32) : (ui32))
1817c478bd9Sstevel@tonic-gate 
182f13ac639Smuffin #define	HASH_TBL(p, ui32)	\
183f13ac639Smuffin 	((((p)->flag & (ST_REV1|ST_SWP)) == ST_SWP) ? \
184f13ac639Smuffin 	    doswap32(ui32) : (ui32))
185f13ac639Smuffin 
1867c478bd9Sstevel@tonic-gate extern const char	*defaultbind;
1877c478bd9Sstevel@tonic-gate extern const char	default_domain[];
1887c478bd9Sstevel@tonic-gate extern Gettext_t	*global_gt;
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate extern char	*_textdomain_u(const char *, char *);
1917c478bd9Sstevel@tonic-gate extern char	*_real_bindtextdomain_u(const char *, const char *, int);
1927c478bd9Sstevel@tonic-gate extern char	*_real_gettext_u(const char *, const char *,
193*b599bd93SRobert Mustacchi     const char *, unsigned long int, int, int, locale_t);
194f13ac639Smuffin extern char	*handle_mo(struct msg_pack *);
1957c478bd9Sstevel@tonic-gate 
196f13ac639Smuffin extern int	gnu_setmsg(Msg_node *, char *, size_t);
197f13ac639Smuffin extern char	*handle_lang(struct msg_pack *);
1987c478bd9Sstevel@tonic-gate extern char	*mk_msgfile(struct msg_pack *);
199f13ac639Smuffin extern Msg_node	*check_cache(struct msg_pack *);
200f13ac639Smuffin extern uint32_t	get_hashid(const char *, uint32_t *);
201f13ac639Smuffin extern uint32_t	doswap32(uint32_t);
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate extern int	plural_expr(plural_expr_t *, const char *);
2047c478bd9Sstevel@tonic-gate extern unsigned int	plural_eval(plural_expr_t, unsigned int);
2057c478bd9Sstevel@tonic-gate 
206f13ac639Smuffin extern char	*gnu_key_2_text(Msg_g_node *, const char *, struct msg_pack *);
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate extern char	*get_codeset(const char *);
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate #ifdef GETTEXT_DEBUG
211f13ac639Smuffin extern void	gprintf(int, const char *, ...);
212f13ac639Smuffin extern void	printgt(Gettext_t *, int);
2137c478bd9Sstevel@tonic-gate extern void	printmp(struct msg_pack *, int);
2147c478bd9Sstevel@tonic-gate extern void	printsunmsg(Msg_s_node *, int);
2157c478bd9Sstevel@tonic-gate extern void	printgnumsg(Msg_g_node *, int);
2167c478bd9Sstevel@tonic-gate extern void	printexpr(plural_expr_t, int);
2177c478bd9Sstevel@tonic-gate extern void	printmnp(Msg_node *, int);
2187c478bd9Sstevel@tonic-gate extern void	printlist(void);
219f13ac639Smuffin extern void	print_rev1_info(Msg_g_node *);
2207c478bd9Sstevel@tonic-gate #endif
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
2237c478bd9Sstevel@tonic-gate }
2247c478bd9Sstevel@tonic-gate #endif
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate #endif	/* !_LIBC_PORT_I18N_GETTEXT_H */
227