1da2e3ebdSchin /***********************************************************************
2da2e3ebdSchin * *
3da2e3ebdSchin * This software is part of the ast package *
4*3e14f97fSRoger A. Faulkner * Copyright (c) 1985-2010 AT&T Intellectual Property *
5da2e3ebdSchin * and is licensed under the *
6da2e3ebdSchin * Common Public License, Version 1.0 *
77c2fbfb3SApril Chin * by AT&T Intellectual Property *
8da2e3ebdSchin * *
9da2e3ebdSchin * A copy of the License is available at *
10da2e3ebdSchin * http://www.opensource.org/licenses/cpl1.0.txt *
11da2e3ebdSchin * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
12da2e3ebdSchin * *
13da2e3ebdSchin * Information and Software Systems Research *
14da2e3ebdSchin * AT&T Research *
15da2e3ebdSchin * Florham Park NJ *
16da2e3ebdSchin * *
17da2e3ebdSchin * Glenn Fowler <gsf@research.att.com> *
18da2e3ebdSchin * David Korn <dgk@research.att.com> *
19da2e3ebdSchin * Phong Vo <kpv@research.att.com> *
20da2e3ebdSchin * *
21da2e3ebdSchin ***********************************************************************/
22da2e3ebdSchin #pragma prototyped
23da2e3ebdSchin
24da2e3ebdSchin /*
25da2e3ebdSchin * catopen intercept
26da2e3ebdSchin * the ast catalogs are checked first
27da2e3ebdSchin * the ast mc* and native cat* routines do all the work
28da2e3ebdSchin * catalogs found by mcfind() are converted from utf to ucs
29da2e3ebdSchin *
30da2e3ebdSchin * nl_catd is cast to void*
31da2e3ebdSchin * this is either an Mc_t* (Mc_t.set != 0)
32da2e3ebdSchin * or a Cc_t* where Cc_t.cat is the native nl_catd
33da2e3ebdSchin */
34da2e3ebdSchin
35da2e3ebdSchin #include <ast.h>
36da2e3ebdSchin #include <mc.h>
37da2e3ebdSchin #include <nl_types.h>
38da2e3ebdSchin #include <iconv.h>
39da2e3ebdSchin
40da2e3ebdSchin #ifndef DEBUG_trace
41da2e3ebdSchin #define DEBUG_trace 0
42da2e3ebdSchin #endif
43da2e3ebdSchin
44da2e3ebdSchin #if _lib_catopen
45da2e3ebdSchin
46da2e3ebdSchin #undef nl_catd
47da2e3ebdSchin #undef catopen
48da2e3ebdSchin #undef catgets
49da2e3ebdSchin #undef catclose
50da2e3ebdSchin
51da2e3ebdSchin typedef struct
52da2e3ebdSchin {
53da2e3ebdSchin Mcset_t* set;
54da2e3ebdSchin nl_catd cat;
55da2e3ebdSchin iconv_t cvt;
56da2e3ebdSchin Sfio_t* tmp;
57da2e3ebdSchin } Cc_t;
58da2e3ebdSchin
59da2e3ebdSchin #else
60da2e3ebdSchin
61da2e3ebdSchin #define _ast_nl_catd nl_catd
62da2e3ebdSchin #define _ast_catopen catopen
63da2e3ebdSchin #define _ast_catgets catgets
64da2e3ebdSchin #define _ast_catclose catclose
65da2e3ebdSchin
66da2e3ebdSchin #endif
67da2e3ebdSchin
68da2e3ebdSchin _ast_nl_catd
_ast_catopen(const char * name,int flag)69da2e3ebdSchin _ast_catopen(const char* name, int flag)
70da2e3ebdSchin {
71da2e3ebdSchin Mc_t* mc;
72da2e3ebdSchin char* s;
73da2e3ebdSchin Sfio_t* ip;
74da2e3ebdSchin char path[PATH_MAX];
75da2e3ebdSchin
76da2e3ebdSchin /*
77da2e3ebdSchin * first try the ast catalogs
78da2e3ebdSchin */
79da2e3ebdSchin
80da2e3ebdSchin #if DEBUG_trace
81da2e3ebdSchin sfprintf(sfstderr, "AHA#%d:%s %s\n", __LINE__, __FILE__, name);
82da2e3ebdSchin #endif
83da2e3ebdSchin if ((s = mcfind(path, NiL, name, LC_MESSAGES, flag)) && (ip = sfopen(NiL, s, "r")))
84da2e3ebdSchin {
85da2e3ebdSchin #if DEBUG_trace
86da2e3ebdSchin sfprintf(sfstderr, "AHA#%d:%s %s\n", __LINE__, __FILE__, s);
87da2e3ebdSchin #endif
88da2e3ebdSchin mc = mcopen(ip);
89da2e3ebdSchin sfclose(ip);
90da2e3ebdSchin if (mc)
91da2e3ebdSchin return (_ast_nl_catd)mc;
92da2e3ebdSchin }
93da2e3ebdSchin #if _lib_catopen
94da2e3ebdSchin if (strcmp(setlocale(LC_MESSAGES, NiL), "debug"))
95da2e3ebdSchin {
96da2e3ebdSchin Cc_t* cc;
97da2e3ebdSchin nl_catd d;
98da2e3ebdSchin
99da2e3ebdSchin /*
100da2e3ebdSchin * now the native catalogs
101da2e3ebdSchin */
102da2e3ebdSchin
103da2e3ebdSchin if (s && (d = catopen(s, flag)) != (nl_catd)(-1) || !(s = 0) && (d = catopen(name, flag)) != (nl_catd)(-1))
104da2e3ebdSchin {
105da2e3ebdSchin if (!(cc = newof(0, Cc_t, 1, 0)))
106da2e3ebdSchin {
107da2e3ebdSchin catclose(d);
108da2e3ebdSchin return (_ast_nl_catd)(-1);
109da2e3ebdSchin }
110da2e3ebdSchin cc->cat = d;
111da2e3ebdSchin if ((s || *name == '/') && (ast.locale.set & (1<<AST_LC_MESSAGES)))
112da2e3ebdSchin {
113da2e3ebdSchin if ((cc->cvt = iconv_open("", "utf")) == (iconv_t)(-1) || !(cc->tmp = sfstropen()))
114da2e3ebdSchin {
115da2e3ebdSchin catclose(d);
116da2e3ebdSchin return (_ast_nl_catd)(-1);
117da2e3ebdSchin }
118da2e3ebdSchin }
119da2e3ebdSchin else
120da2e3ebdSchin cc->cvt = (iconv_t)(-1);
121da2e3ebdSchin #if DEBUG_trace
122da2e3ebdSchin sfprintf(sfstderr, "AHA#%d:%s %s %s native %p\n", __LINE__, __FILE__, s, name, cc->cat);
123da2e3ebdSchin #endif
124da2e3ebdSchin return (_ast_nl_catd)cc;
125da2e3ebdSchin }
126da2e3ebdSchin }
127da2e3ebdSchin #endif
128da2e3ebdSchin
129da2e3ebdSchin /*
130da2e3ebdSchin * loser
131da2e3ebdSchin */
132da2e3ebdSchin
133da2e3ebdSchin return (_ast_nl_catd)(-1);
134da2e3ebdSchin }
135da2e3ebdSchin
136da2e3ebdSchin char*
_ast_catgets(_ast_nl_catd cat,int set,int num,const char * msg)137da2e3ebdSchin _ast_catgets(_ast_nl_catd cat, int set, int num, const char* msg)
138da2e3ebdSchin {
139da2e3ebdSchin if (cat == (_ast_nl_catd)(-1))
140da2e3ebdSchin return (char*)msg;
141da2e3ebdSchin #if _lib_catopen
142da2e3ebdSchin if (!((Cc_t*)cat)->set)
143da2e3ebdSchin {
144da2e3ebdSchin char* s;
145da2e3ebdSchin size_t n;
146da2e3ebdSchin
147da2e3ebdSchin msg = (char*)catgets(((Cc_t*)cat)->cat, set, num, msg);
148da2e3ebdSchin if (((Cc_t*)cat)->cvt != (iconv_t)(-1))
149da2e3ebdSchin {
150da2e3ebdSchin s = (char*)msg;
151da2e3ebdSchin n = strlen(s);
152da2e3ebdSchin iconv_write(((Cc_t*)cat)->cvt, ((Cc_t*)cat)->tmp, &s, &n, NiL);
153da2e3ebdSchin if (s = sfstruse(((Cc_t*)cat)->tmp))
154da2e3ebdSchin return s;
155da2e3ebdSchin }
156da2e3ebdSchin return (char*)msg;
157da2e3ebdSchin }
158da2e3ebdSchin #endif
159da2e3ebdSchin return mcget((Mc_t*)cat, set, num, msg);
160da2e3ebdSchin }
161da2e3ebdSchin
162da2e3ebdSchin int
_ast_catclose(_ast_nl_catd cat)163da2e3ebdSchin _ast_catclose(_ast_nl_catd cat)
164da2e3ebdSchin {
165da2e3ebdSchin if (cat == (_ast_nl_catd)(-1))
166da2e3ebdSchin return -1;
167da2e3ebdSchin #if _lib_catopen
168da2e3ebdSchin if (!((Cc_t*)cat)->set)
169da2e3ebdSchin {
170da2e3ebdSchin if (((Cc_t*)cat)->cvt != (iconv_t)(-1))
171da2e3ebdSchin iconv_close(((Cc_t*)cat)->cvt);
172da2e3ebdSchin if (((Cc_t*)cat)->tmp)
173da2e3ebdSchin sfclose(((Cc_t*)cat)->tmp);
174da2e3ebdSchin return catclose(((Cc_t*)cat)->cat);
175da2e3ebdSchin }
176da2e3ebdSchin #endif
177da2e3ebdSchin return mcclose((Mc_t*)cat);
178da2e3ebdSchin }
179