xref: /titanic_52/usr/src/lib/libc/port/i18n/gettext.h (revision c7158ae983f5a04c4a998f468ecefba6d23ba721)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #ifndef	_LIBC_PORT_I18N_GETTEXT_H
28 #define	_LIBC_PORT_I18N_GETTEXT_H
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 #include <sys/param.h>
33 #include <iconv.h>
34 #include <synch.h>
35 
36 #ifdef	__cplusplus
37 extern "C" {
38 #endif
39 
40 /* Type of MO file */
41 #define	T_SUN_MO	0x1
42 #define	T_GNU_MO	0x2
43 #define	T_GNU_SWAPPED_MO	0x4
44 #define	T_ILL_MO	0x8
45 
46 #define	TP_BINDING	0
47 #define	TP_CODESET	1
48 
49 /* Msg_g_node->flag */
50 #define	ST_CHK	0x1			/* header has been checked? */
51 #define	ST_SWP	0x2			/* reversed endian? */
52 
53 typedef struct domain_binding {
54 	char	*domain;	/* domain name */
55 	char	*binding;	/* binding directory */
56 	char	*codeset;	/* codeset */
57 	struct domain_binding	*next;
58 } Dbinding;
59 
60 /*
61  * this structure is used for preserving nlspath templates before
62  * passing them to bindtextdomain():
63  */
64 typedef struct nlstmp {
65 	char	pathname[MAXPATHLEN];	/* the full pathname to file */
66 	struct nlstmp	*next;		/* link to the next entry */
67 } Nlstmp;
68 
69 typedef struct {
70 	struct msg_info	*msg_file_info;	/* information of msg file */
71 	struct msg_struct	*msg_list;	/* message list */
72 	char	*msg_ids;		/* actual message ids */
73 	char	*msg_strs;		/* actual message strs */
74 } Msg_s_node;
75 
76 typedef struct expr	*plural_expr_t;
77 
78 typedef struct {
79 	struct gnu_msg_info	*msg_file_info;	/* information of msg file */
80 	unsigned int	*hash_table; /* hash table */
81 	char	*src_encoding;	/* src encoding */
82 	char	*dst_encoding;	/* dst encoding */
83 	int	flag;				/* status */
84 	int	nplurals;			/* number of plural forms */
85 	plural_expr_t	plural;	/* plural expression */
86 	iconv_t	fd;				/* iconv descriptor */
87 	char	**conv_msgstr;	/* code-converted msgstr */
88 } Msg_g_node;
89 
90 typedef struct msg_node {
91 	int	type;		/* T_SUN_MO, T_GNU_MO, or T_ILL_MO */
92 	int	trusted;	/* is this a trusted source? */
93 	char	*path;	/* name of message catalog */
94 	union {
95 		Msg_s_node	*sunmsg;
96 		Msg_g_node	*gnumsg;
97 	} msg;
98 	struct msg_node	*next;	/* link to the next */
99 } Msg_node;
100 
101 typedef struct nls_node {
102 	char	*domain;		/* key: domain name */
103 	char	*locale;		/* key: locale name */
104 	char	*nlspath;		/* key: NLSPATH */
105 	char	*ppaths;		/* value: expanded path */
106 	struct nls_node	*next;	/* link to the next */
107 } Nls_node;
108 
109 typedef struct cache_node {
110 	unsigned int	hashid;	/* hashed valued for the locale name */
111 	Msg_node	*m_node;	/* link to Msg_node */
112 	Msg_node	*m_last;	/* link to the last Msg_node */
113 	Nls_node	*n_node;	/* link to Nls_node */
114 	Nls_node	*n_last;	/* link to the last Nls_node */
115 	struct cache_node	*next;	/* link to the next */
116 } Cache_node;
117 
118 typedef struct {
119 	char	*cur_domain;	/* current domain */
120 	Dbinding	*dbind;		/* domain binding */
121 	Cache_node	*c_node;	/* link to the cache node */
122 	Cache_node	*c_last;	/* link to the last cache node */
123 	Msg_node	*c_m_node;	/* link to the current Msg_node */
124 	Nls_node	*c_n_node;	/* link to the current Nls_node */
125 } Gettext_t;
126 
127 struct msg_pack {
128 	const char	*msgid1;	/* msgid1 argument */
129 	const char	*msgid2;	/* msgid2 argument */
130 	char	*msgfile;		/* msg catalog file to open */
131 	char	*domain;		/* textdomain name */
132 	char	*binding;		/* binding */
133 	char	*locale;		/* locale */
134 	char	*language;		/* LANGUAGE env */
135 	caddr_t	addr;			/* mmap'ed address */
136 	size_t	fsz;			/* file size */
137 	size_t	msgfile_len;	/* length of msgfile */
138 	size_t	domain_len;		/* length of domain */
139 	size_t	locale_len;		/* length of locale */
140 
141 	unsigned int	n;		/* n argument */
142 	int	category;			/* category argument */
143 	int	plural;				/* plural or not */
144 	unsigned int	hash_locale;	/* hash ID of locale */
145 	int	nlsp;				/* nlsp */
146 	int	trusted;			/* trusted msg catalog or not */
147 };
148 
149 struct cache_pack {
150 	int	cacheline;
151 	Msg_node	*mnp;
152 	Cache_node	*cnp;
153 	Cache_node	*node_hash;
154 };
155 
156 #define	DEFAULT_DOMAIN		"messages"
157 #define	DEFAULT_DOMAIN_LEN	(sizeof (DEFAULT_DOMAIN) - 1)
158 #define	DEFAULT_BINDING		_DFLT_LOC_PATH
159 #define	MSGFILESUFFIX		".mo"
160 #define	MSGFILESUFFIXLEN	(sizeof (MSGFILESUFFIX) - 1)
161 
162 #define	CURRENT_DOMAIN(gt)	(gt)->cur_domain
163 #define	FIRSTBIND(gt)	(gt)->dbind
164 
165 #define	DFLTMSG(result, msgid1, msgid2, n, plural) \
166 	result = (plural ? \
167 		((n == 1) ? (char *)msgid1 : (char *)msgid2) : \
168 		(char *)msgid1)
169 
170 #define	SWAP(p, ui32) \
171 	(((p)->flag & ST_SWP) ? doswap32(ui32) : (ui32))
172 
173 extern const char	*defaultbind;
174 extern const char	default_domain[];
175 extern Gettext_t	*global_gt;
176 
177 extern char *_textdomain_u(const char *, char *);
178 extern char *_real_bindtextdomain_u(const char *, const char *, int);
179 extern char *_real_gettext_u(const char *, const char *,
180 	const char *, unsigned long int, int, int);
181 
182 extern int	setmsg(Msg_node *, char *, size_t);
183 extern char *handle_lang(struct cache_pack *, struct msg_pack *);
184 extern char *mk_msgfile(struct msg_pack *);
185 extern int check_cache(struct cache_pack *, struct msg_pack *);
186 extern void connect_entry(struct cache_pack *);
187 extern int connect_invalid_entry(struct cache_pack *, struct msg_pack *);
188 extern Msg_node *create_mnp(struct msg_pack *);
189 extern Cache_node *create_cnp(Msg_node *, struct msg_pack *);
190 extern void free_mnp_mp(Msg_node *, struct msg_pack *);
191 extern unsigned int	get_hashid(const char *, size_t *);
192 extern unsigned int	doswap32(unsigned int);
193 
194 extern int	plural_expr(plural_expr_t *, const char *);
195 extern unsigned int	plural_eval(plural_expr_t, unsigned int);
196 
197 extern char *gnu_key_2_text(Msg_g_node *, const char *,
198 	struct msg_pack *);
199 
200 extern char *get_codeset(const char *);
201 
202 #ifdef GETTEXT_DEBUG
203 extern void	printmp(struct msg_pack *, int);
204 extern void	printsunmsg(Msg_s_node *, int);
205 extern void	printgnumsg(Msg_g_node *, int);
206 extern void	printexpr(plural_expr_t, int);
207 extern void	printmnp(Msg_node *, int);
208 extern void	printcnp(Cache_node *, int);
209 extern void	printcp(struct cache_pack *, int);
210 extern void	printlist(void);
211 #endif
212 
213 #ifdef	__cplusplus
214 }
215 #endif
216 
217 #endif	/* !_LIBC_PORT_I18N_GETTEXT_H */
218