xref: /titanic_50/usr/src/lib/libast/common/misc/translate.c (revision 67e3a03ed4a2813074d36330f062ed6e593a4937)
1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *           Copyright (c) 1985-2007 AT&T Knowledge Ventures            *
5 *                      and is licensed under the                       *
6 *                  Common Public License, Version 1.0                  *
7 *                      by AT&T Knowledge Ventures                      *
8 *                                                                      *
9 *                A copy of the License is available at                 *
10 *            http://www.opensource.org/licenses/cpl1.0.txt             *
11 *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
12 *                                                                      *
13 *              Information and Software Systems Research               *
14 *                            AT&T Research                             *
15 *                           Florham Park NJ                            *
16 *                                                                      *
17 *                 Glenn Fowler <gsf@research.att.com>                  *
18 *                  David Korn <dgk@research.att.com>                   *
19 *                   Phong Vo <kpv@research.att.com>                    *
20 *                                                                      *
21 ***********************************************************************/
22 #pragma prototyped
23 
24 /*
25  * AT&T Research and SCO
26  * ast i18n message translation
27  */
28 
29 #include "lclib.h"
30 
31 #include <cdt.h>
32 #include <error.h>
33 #include <mc.h>
34 #include <nl_types.h>
35 
36 #ifndef DEBUG_trace
37 #define DEBUG_trace		0
38 #endif
39 
40 #define NOCAT			((nl_catd)-1)
41 #define GAP			100
42 
43 typedef	struct
44 {
45 	Dtlink_t	link;		/* dictionary link		*/
46 	Dt_t*		messages;	/* message dictionary handle	*/
47 	nl_catd		cat;		/* message catalog handle	*/
48 	int		debug;		/* special debug locale		*/
49 	const char*	locale;		/* message catalog locale	*/
50 	char		name[1];	/* catalog name			*/
51 } Catalog_t;
52 
53 typedef struct
54 {
55 	Dtlink_t	link;		/* dictionary link		*/
56 	Catalog_t*	cat;		/* current catalog pointer	*/
57 	int		set;		/* set number			*/
58 	int		seq;		/* sequence number		*/
59 	char		text[1];	/* message text			*/
60 } Message_t;
61 
62 typedef struct
63 {
64 	Sfio_t*		sp;		/* temp string stream		*/
65 	int		off;		/* string base offset		*/
66 } Temp_t;
67 
68 typedef struct
69 {
70 	Dtdisc_t	message_disc;	/* message dict discipline	*/
71 	Dtdisc_t	catalog_disc;	/* catalog dict discipline	*/
72 	Dt_t*		catalogs;	/* catalog dictionary handle	*/
73 	Sfio_t*		tmp;		/* temporary string stream	*/
74 	const char*	debug;		/* debug locale name		*/
75 	int		error;		/* no dictionaries!		*/
76 	char		null[1];	/* null string			*/
77 } State_t;
78 
79 static State_t	state =
80 {
81 	{	offsetof(Message_t, text),	0,	0	},
82 	{	offsetof(Catalog_t, name),	0,	0	},
83 };
84 
85 static int
86 tempget(Sfio_t* sp)
87 {
88 	if (sfstrtell(sp) > sfstrsize(sp) / 2)
89 		sfstrseek(sp, 0, SEEK_SET);
90 	return sfstrtell(sp);
91 }
92 
93 static char*
94 tempuse(Sfio_t* sp, int off)
95 {
96 	sfputc(sp, 0);
97 	return sfstrbase(sp) + off;
98 }
99 
100 /*
101  * add msg to dict
102  */
103 
104 static int
105 entry(Dt_t* dict, int set, int seq, const char* msg)
106 {
107 	Message_t*	mp;
108 
109 	if (!(mp = newof(0, Message_t, 1, strlen(msg))))
110 		return 0;
111 	strcpy(mp->text, msg);
112 	mp->set = set;
113 	mp->seq = seq;
114 	if (!dtinsert(dict, mp))
115 	{
116 		free(mp);
117 		return 0;
118 	}
119 #if DEBUG_trace > 1
120 sfprintf(sfstderr, "AHA#%d:%s set %d seq %d msg `%s'\n", __LINE__, __FILE__, set, seq, msg);
121 #endif
122 	return 1;
123 }
124 
125 /*
126  * find catalog in locale and return catopen() descriptor
127  */
128 
129 static nl_catd
130 find(const char* locale, const char* catalog)
131 {
132 	char		path[PATH_MAX];
133 #if DEBUG_trace
134 	const char*	ocatalog = catalog;
135 #endif
136 
137 	if (mcfind(path, locale, catalog, LC_MESSAGES, 0))
138 		catalog = (const char*)path;
139 #if DEBUG_trace
140 sfprintf(sfstderr, "AHA#%d:%s %s %s %s\n", __LINE__, __FILE__, locale, ocatalog, catalog);
141 #endif
142 	return catopen(catalog, NL_CAT_LOCALE);
143 }
144 
145 /*
146  * initialize the catalog s by loading in the default locale messages
147  */
148 
149 static Catalog_t*
150 init(register char* s)
151 {
152 	register Catalog_t*	cp;
153 	register char*		u;
154 	register int		n;
155 	register int		m;
156 	nl_catd			d;
157 
158 	/*
159 	 * insert into the catalog dictionary
160 	 */
161 
162 	if (!(cp = newof(0, Catalog_t, 1, strlen(s))))
163 		return 0;
164 	strcpy(cp->name, s);
165 	if (!dtinsert(state.catalogs, cp))
166 	{
167 		free(cp);
168 		return 0;
169 	}
170 	cp->cat = NOCAT;
171 
172 	/*
173 	 * locate the default locale catalog
174 	 */
175 
176 	u = setlocale(LC_MESSAGES, NiL);
177 	setlocale(LC_MESSAGES, "C");
178 	if ((d = find("C", s)) != NOCAT)
179 	{
180 		/*
181 		 * load the default locale messages
182 		 * this assumes one mesage set for ast (AST_MESSAGE_SET)
183 		 * different packages can share the same message catalog
184 		 * name by using different message set numbers
185 		 * see <mc.h> mcindex()
186 		 *
187 		 * this method requires a scan of each catalog, and the
188 		 * catalogs do not advertize the max message number, so
189 		 * we assume there are no messages after a gap of GAP
190 		 * missing messages
191 		 */
192 
193 		if (cp->messages = dtopen(&state.message_disc, Dtset))
194 		{
195 			n = m = 0;
196 			for (;;)
197 			{
198 				n++;
199 				if ((s = catgets(d, AST_MESSAGE_SET, n, state.null)) != state.null && entry(cp->messages, AST_MESSAGE_SET, n, s))
200 					m = n;
201 				else if ((n - m) > GAP)
202 					break;
203 			}
204 			if (!m)
205 			{
206 				dtclose(cp->messages);
207 				cp->messages = 0;
208 			}
209 		}
210 		catclose(d);
211 	}
212 	setlocale(LC_MESSAGES, u);
213 	return cp;
214 }
215 
216 /*
217  * return the C locale message pointer for msg in cat
218  * cat may be a : separated list of candidate names
219  */
220 
221 static Message_t*
222 match(const char* cat, const char* msg)
223 {
224 	register char*	s;
225 	register char*	t;
226 	Catalog_t*	cp;
227 	Message_t*	mp;
228 	size_t		n;
229 
230 	char		buf[1024];
231 
232 	s = (char*)cat;
233 	for (;;)
234 	{
235 		if (t = strchr(s, ':'))
236 		{
237 			if (s == (char*)cat)
238 			{
239 				if ((n = strlen(s)) >= sizeof(buf))
240 					n = sizeof(buf) - 1;
241 				s = (char*)memcpy(buf, s, n);
242 				s[n] = 0;
243 				t = strchr(s, ':');
244 			}
245 			*t = 0;
246 		}
247 		if (*s && ((cp = (Catalog_t*)dtmatch(state.catalogs, s)) || (cp = init(s))) && cp->messages && (mp = (Message_t*)dtmatch(cp->messages, msg)))
248 		{
249 			mp->cat = cp;
250 			return mp;
251 		}
252 		if (!t)
253 			break;
254 		s = t + 1;
255 	}
256 	return 0;
257 }
258 
259 /*
260  * translate() is called with four arguments:
261  *
262  *	loc	the LC_MESSAGES locale name
263  *	cmd	the calling command name
264  *	cat	the catalog name, possibly a : separated list
265  *		"libFOO"	FOO library messages
266  *		"libshell"	ksh command messages
267  *		"SCRIPT"	script SCRIPT application messages
268  *	msg	message text to be translated
269  *
270  * the translated message text is returned on success
271  * otherwise the original msg is returned
272  *
273  * The first time translate() is called (for a non-C locale)
274  * it creates the state.catalogs dictionary. A dictionary entry
275  * (Catalog_t) is made each time translate() is called with a new
276  * cmd:cat argument.
277  *
278  * The X/Open interface catgets() is used to obtain a translated
279  * message. Its arguments include the message catalog name
280  * and the set/sequence numbers within the catalog. An additional
281  * dictionary, with entries of type Message_t, is needed for
282  * mapping untranslated message strings to the set/sequence numbers
283  * needed by catgets().  A separate Message_t dictionary is maintained
284  * for each Catalog_t.
285  */
286 
287 char*
288 translate(const char* loc, const char* cmd, const char* cat, const char* msg)
289 {
290 	register char*	r;
291 	char*		t;
292 	int		p;
293 	int		oerrno;
294 	Catalog_t*	cp;
295 	Message_t*	mp;
296 
297 	oerrno = errno;
298 	r = (char*)msg;
299 
300 	/*
301 	 * quick out
302 	 */
303 
304 	if (!cmd && !cat)
305 		goto done;
306 	if (cmd && (t = strrchr(cmd, '/')))
307 		cmd = (const char*)(t + 1);
308 
309 	/*
310 	 * initialize the catalogs dictionary
311 	 */
312 
313 	if (!state.catalogs)
314 	{
315 		if (state.error)
316 			goto done;
317 		if (!(state.tmp = sfstropen()))
318 		{
319 			state.error = 1;
320 			goto done;
321 		}
322 		if (!(state.catalogs = dtopen(&state.catalog_disc, Dtset)))
323 		{
324 			sfclose(state.tmp);
325 			state.error = 1;
326 			goto done;
327 		}
328 		if (streq(loc, "debug"))
329 			state.debug = loc;
330 	}
331 
332 	/*
333 	 * get the message
334 	 * or do we have to spell it out for you
335 	 */
336 
337 	if ((!cmd || !(mp = match(cmd, msg))) &&
338 	    (!cat || !(mp = match(cat, msg))) &&
339 	    (!error_info.catalog || !(mp = match(error_info.catalog, msg))) &&
340 	    (!ast.id || !(mp = match(ast.id, msg))) ||
341 	     !(cp = mp->cat))
342 	{
343 #if DEBUG_trace > 1
344 sfprintf(sfstderr, "AHA#%d:%s cmd %s cat %s:%s id %s msg `%s'\n", __LINE__, __FILE__, cmd, cat, error_info.catalog, ast.id, msg);
345 #endif
346 		goto done;
347 	}
348 
349 	/*
350 	 * adjust for the current locale
351 	 */
352 
353 #if DEBUG_trace
354 sfprintf(sfstderr, "AHA#%d:%s cp->locale `%s' %p loc `%s' %p\n", __LINE__, __FILE__, cp->locale, cp->locale, loc, loc);
355 #endif
356 	if (cp->locale != loc)
357 	{
358 		cp->locale = loc;
359 		if (cp->cat != NOCAT)
360 			catclose(cp->cat);
361 		if ((cp->cat = find(cp->locale, cp->name)) == NOCAT)
362 			cp->debug = streq(cp->locale, "debug");
363 		else
364 			cp->debug = 0;
365 #if DEBUG_trace
366 sfprintf(sfstderr, "AHA#%d:%s cp->cat %p cp->debug %d NOCAT %p\n", __LINE__, __FILE__, cp->cat, cp->debug, NOCAT);
367 #endif
368 	}
369 	if (cp->cat == NOCAT)
370 	{
371 		if (cp->debug)
372 		{
373 			p = tempget(state.tmp);
374 			sfprintf(state.tmp, "(%s,%d,%d)", cp->name, mp->set, mp->seq);
375 			r = tempuse(state.tmp, p);
376 		}
377 		else if (ast.locale.set & AST_LC_debug)
378 		{
379 			p = tempget(state.tmp);
380 			sfprintf(state.tmp, "(%s,%d,%d)%s", cp->name, mp->set, mp->seq, r);
381 			r = tempuse(state.tmp, p);
382 		}
383 		goto done;
384 	}
385 
386 	/*
387 	 * get the translated message
388 	 */
389 
390 	r = catgets(cp->cat, mp->set, mp->seq, msg);
391 	if (ast.locale.set & AST_LC_translate)
392 		sfprintf(sfstderr, "translate locale=%s catalog=%s set=%d seq=%d \"%s\" => \"%s\"\n", cp->locale, cp->name, mp->set, mp->seq, msg, r == (char*)msg ? "NOPE" : r);
393 	if (r != (char*)msg)
394 	{
395 		if (streq(r, (char*)msg))
396 			r = (char*)msg;
397 		else if (strcmp(fmtfmt(r), fmtfmt(msg)))
398 		{
399 			sfprintf(sfstderr, "locale %s catalog %s message %d.%d \"%s\" does not match \"%s\"\n", cp->locale, cp->name, mp->set, mp->seq, r, msg);
400 			r = (char*)msg;
401 		}
402 	}
403 	if (ast.locale.set & AST_LC_debug)
404 	{
405 		p = tempget(state.tmp);
406 		sfprintf(state.tmp, "(%s,%d,%d)%s", cp->name, mp->set, mp->seq, r);
407 		r = tempuse(state.tmp, p);
408 	}
409  done:
410 	if (r == (char*)msg && loc == state.debug)
411 	{
412 		p = tempget(state.tmp);
413 		sfprintf(state.tmp, "(%s,%s,%s,\"%s\")", loc, cmd, cat, r);
414 		r = tempuse(state.tmp, p);
415 	}
416 	errno = oerrno;
417 	return r;
418 }
419