r17345: Some C++ warnings
[jra/samba/.git] / source3 / intl / lang_tdb.c
1 /* 
2    Unix SMB/CIFS implementation.
3    tdb based replacement for gettext 
4    Copyright (C) Andrew Tridgell 2001
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "includes.h"
22
23 static TDB_CONTEXT *tdb;
24
25 /* the currently selected language */
26 static char *current_lang;
27
28
29 /* load a msg file into the tdb */
30 static BOOL load_msg(const char *msg_file)
31 {
32         char **lines;
33         int num_lines, i;
34         char *msgid, *msgstr;
35         TDB_DATA key, data;
36
37         lines = file_lines_load(msg_file, &num_lines,0);
38
39         if (!lines) {
40                 return False;
41         }
42
43         if (tdb_lockall(tdb) != 0) {
44                 file_lines_free(lines);
45                 return False;
46         }
47
48         /* wipe the db */
49         tdb_traverse(tdb, tdb_traverse_delete_fn, NULL);
50
51         msgid = NULL;
52         
53         for (i=0;i<num_lines;i++) {
54                 if (strncmp(lines[i], "msgid \"", 7) == 0) {
55                         msgid = lines[i] + 7;
56                 }
57                 if (msgid && strncmp(lines[i], "msgstr \"", 8) == 0) {
58                         msgstr = lines[i] + 8;
59                         trim_char(msgid, '\0', '\"');
60                         trim_char(msgstr, '\0', '\"');
61                         if (*msgstr == 0) {
62                                 msgstr = msgid;
63                         }
64                         all_string_sub(msgid, "\\n", "\n", 0);
65                         all_string_sub(msgstr, "\\n", "\n", 0);
66                         key.dptr = msgid;
67                         key.dsize = strlen(msgid)+1;
68                         data.dptr = msgstr;
69                         data.dsize = strlen(msgstr)+1;
70                         tdb_store(tdb, key, data, 0);
71                         msgid = NULL;
72                 }
73         }
74
75         file_lines_free(lines);
76         tdb_unlockall(tdb);
77
78         return True;
79 }
80
81
82 /* work out what language to use from locale variables */
83 static const char *get_lang(void)
84 {
85         const char *vars[] = {"LANGUAGE", "LC_ALL", "LC_LANG", "LANG", NULL};
86         int i;
87         char *p;
88
89         for (i=0; vars[i]; i++) {
90                 if ((p = getenv(vars[i]))) {
91                         return p;
92                 }
93         }
94
95         return NULL;
96 }
97
98 /* initialise the message translation subsystem. If the "lang" argument
99    is NULL then get the language from the normal environment variables */
100 BOOL lang_tdb_init(const char *lang)
101 {
102         char *path = NULL;
103         char *msg_path = NULL;
104         struct stat st;
105         static int initialised;
106         time_t loadtime;
107         BOOL result = False;
108
109         /* we only want to init once per process, unless given
110            an override */
111         if (initialised && !lang) 
112                 return True;
113
114         if (initialised) {
115                 /* we are re-initialising, free up any old init */
116                 if (tdb) {
117                         tdb_close(tdb);
118                         tdb = NULL;
119                 }
120                 SAFE_FREE(current_lang);
121         }
122
123         initialised = 1;
124
125         if (!lang) {
126                 /* no lang given, use environment */
127                 lang = get_lang();
128         }
129
130         /* if no lang then we don't translate */
131         if (!lang) 
132                 return True;
133
134         asprintf(&msg_path, "%s.msg", lib_path((const char *)lang));
135         if (stat(msg_path, &st) != 0) {
136                 /* the msg file isn't available */
137                 DEBUG(10, ("lang_tdb_init: %s: %s\n", msg_path, 
138                            strerror(errno)));
139                 goto done;
140         }
141         
142         asprintf(&path, "%s%s.tdb", lock_path("lang_"), lang);
143
144         DEBUG(10, ("lang_tdb_init: loading %s\n", path));
145
146         tdb = tdb_open_log(path, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0644);
147         if (!tdb) {
148                 tdb = tdb_open_log(path, 0, TDB_DEFAULT, O_RDONLY, 0);
149                 if (!tdb) {
150                         DEBUG(10, ("lang_tdb_init: %s: %s\n", path,
151                                    strerror(errno)));
152                         goto done;
153                 }
154                 current_lang = SMB_STRDUP(lang);
155                 result = True;
156                 goto done;
157         }
158
159         loadtime = tdb_fetch_int32(tdb, "/LOADTIME/");
160
161         if (loadtime == -1 || loadtime < st.st_mtime) {
162                 load_msg(msg_path);
163                 tdb_store_int32(tdb, "/LOADTIME/", (int)time(NULL));
164         }
165
166         current_lang = SMB_STRDUP(lang);
167         result = True;
168
169  done:
170         SAFE_FREE(msg_path);
171         SAFE_FREE(path);
172
173         return result;
174 }
175
176 /* translate a msgid to a message string in the current language 
177    returns a string that must be freed by calling lang_msg_free()
178 */
179 const char *lang_msg(const char *msgid)
180 {
181         TDB_DATA key, data;
182         const char *p;
183         char *q, *msgid_quoted;
184         int count;
185
186         lang_tdb_init(NULL);
187
188         if (!tdb) return msgid;
189
190         /* Due to the way quotes in msgids are escaped in the msg file we
191            must replace " with \" before doing a lookup in the tdb. */
192
193         count = 0;
194
195         for(p = msgid; *p; p++) {
196                 if (*p == '\"')
197                         count++;
198         }
199
200         if (!(msgid_quoted = (char *)SMB_MALLOC(strlen(msgid) + count + 1)))
201                 return msgid;
202
203         /* string_sub() is unsuitable here as it replaces some punctuation
204            chars with underscores. */
205
206         for(p = msgid, q = msgid_quoted; *p; p++) {
207                 if (*p == '\"') {
208                         *q = '\\';
209                         q++;
210                 }
211                 *q = *p;
212                 q++;
213         }
214
215         *q = 0;
216
217         key.dptr = (char *)msgid_quoted;
218         key.dsize = strlen(msgid_quoted)+1;
219         
220         data = tdb_fetch(tdb, key);
221
222         free(msgid_quoted);
223
224         /* if the message isn't found then we still need to return a pointer
225            that can be freed. Pity. */
226         if (!data.dptr)
227                 return SMB_STRDUP(msgid);
228
229         return (const char *)data.dptr;
230 }
231
232
233 /* free up a string from lang_msg() */
234 void lang_msg_free(const char *msgstr)
235 {
236         if (!tdb) return;
237         free((void *)msgstr);
238 }
239
240
241 /*
242   when the _() translation macro is used there is no obvious place to free
243   the resulting string and there is no easy way to give a static pointer.
244   All we can do is rotate between some static buffers and hope a single d_printf() 
245   doesn't have more calls to _() than the number of buffers 
246 */
247 const char *lang_msg_rotate(const char *msgid)
248 {
249 #define NUM_LANG_BUFS 16
250         char *msgstr;
251         static pstring bufs[NUM_LANG_BUFS];
252         static int next;
253
254         msgstr = (char *)lang_msg(msgid);
255         if (!msgstr) return msgid;
256
257         pstrcpy(bufs[next], msgstr);
258         msgstr = bufs[next];
259
260         next = (next+1) % NUM_LANG_BUFS;
261         
262         return msgstr;
263 }
264
265
266 /* 
267    return the current language - needed for language file mappings 
268 */
269 char *lang_tdb_current(void)
270 {
271         return current_lang;
272 }