More const fixes for compiler warnings from the waf build.
[kai/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 3 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, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "includes.h"
21 #include "system/filesys.h"
22 #include "intl/lang_tdb.h"
23
24 static TDB_CONTEXT *tdb;
25
26 /* the currently selected language */
27 static char *current_lang;
28
29
30 /* load a msg file into the tdb */
31 static bool load_msg(const char *msg_file)
32 {
33         char **lines;
34         int num_lines, i;
35         char *msgid, *msgstr;
36         TDB_DATA data;
37
38         lines = file_lines_load(msg_file, &num_lines, 0, NULL);
39
40         if (!lines) {
41                 return False;
42         }
43
44         if (tdb_lockall(tdb) != 0) {
45                 TALLOC_FREE(lines);
46                 return False;
47         }
48
49         /* wipe the db */
50         tdb_wipe_all(tdb);
51
52         msgid = NULL;
53         
54         for (i=0;i<num_lines;i++) {
55                 if (strncmp(lines[i], "msgid \"", 7) == 0) {
56                         msgid = lines[i] + 7;
57                 }
58                 if (msgid && strncmp(lines[i], "msgstr \"", 8) == 0) {
59                         msgstr = lines[i] + 8;
60                         trim_char(msgid, '\0', '\"');
61                         trim_char(msgstr, '\0', '\"');
62                         if (*msgstr == 0) {
63                                 msgstr = msgid;
64                         }
65                         all_string_sub(msgid, "\\n", "\n", 0);
66                         all_string_sub(msgstr, "\\n", "\n", 0);
67                         data = string_term_tdb_data(msgstr);
68                         tdb_store_bystring(tdb, msgid, data, 0);
69                         msgid = NULL;
70                 }
71         }
72
73         TALLOC_FREE(lines);
74         tdb_unlockall(tdb);
75
76         return True;
77 }
78
79
80 /* work out what language to use from locale variables */
81 static const char *get_lang(void)
82 {
83         const char *vars[] = {"LANGUAGE", "LC_ALL", "LC_LANG", "LANG", NULL};
84         int i;
85         char *p;
86
87         for (i=0; vars[i]; i++) {
88                 if ((p = getenv(vars[i]))) {
89                         return p;
90                 }
91         }
92
93         return NULL;
94 }
95
96 /* initialise the message translation subsystem. If the "lang" argument
97    is NULL then get the language from the normal environment variables */
98 bool lang_tdb_init(const char *lang)
99 {
100         char *path = NULL;
101         char *msg_path = NULL;
102         struct stat st;
103         static int initialised;
104         time_t loadtime;
105         bool result = False;
106
107         /* we only want to init once per process, unless given
108            an override */
109         if (initialised && !lang) 
110                 return True;
111
112         if (initialised) {
113                 /* we are re-initialising, free up any old init */
114                 if (tdb) {
115                         tdb_close(tdb);
116                         tdb = NULL;
117                 }
118                 SAFE_FREE(current_lang);
119         }
120
121         initialised = 1;
122
123         if (!lang) {
124                 /* no lang given, use environment */
125                 lang = get_lang();
126         }
127
128         /* if no lang then we don't translate */
129         if (!lang) 
130                 return True;
131
132         if (asprintf(&msg_path, "%s.msg",
133                      data_path((const char *)lang)) == -1) {
134                 DEBUG(0, ("asprintf failed\n"));
135                 goto done;
136         }
137         if (stat(msg_path, &st) != 0) {
138                 /* the msg file isn't available */
139                 DEBUG(10, ("lang_tdb_init: %s: %s\n", msg_path, 
140                            strerror(errno)));
141                 goto done;
142         }
143         
144         if (asprintf(&path, "%s%s.tdb", lock_path("lang_"), lang) == -1) {
145                 DEBUG(0, ("asprintf failed\n"));
146                 goto done;
147         }
148
149         DEBUG(10, ("lang_tdb_init: loading %s\n", path));
150
151         tdb = tdb_open_log(path, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0644);
152         if (!tdb) {
153                 tdb = tdb_open_log(path, 0, TDB_DEFAULT, O_RDONLY, 0);
154                 if (!tdb) {
155                         DEBUG(10, ("lang_tdb_init: %s: %s\n", path,
156                                    strerror(errno)));
157                         goto done;
158                 }
159                 current_lang = SMB_STRDUP(lang);
160                 result = True;
161                 goto done;
162         }
163
164         loadtime = tdb_fetch_int32(tdb, "/LOADTIME/");
165
166         if (loadtime == -1 || loadtime < st.st_mtime) {
167                 load_msg(msg_path);
168                 tdb_store_int32(tdb, "/LOADTIME/", (int)time(NULL));
169         }
170
171         current_lang = SMB_STRDUP(lang);
172         result = True;
173
174  done:
175         SAFE_FREE(msg_path);
176         SAFE_FREE(path);
177
178         return result;
179 }
180
181 /* translate a msgid to a message string in the current language 
182    returns a string that must be freed by calling lang_msg_free()
183 */
184 const char *lang_msg(const char *msgid)
185 {
186         TDB_DATA data;
187         const char *p;
188         char *q, *msgid_quoted;
189         int count;
190
191         lang_tdb_init(NULL);
192
193         if (!tdb) return msgid;
194
195         /* Due to the way quotes in msgids are escaped in the msg file we
196            must replace " with \" before doing a lookup in the tdb. */
197
198         count = 0;
199
200         for(p = msgid; *p; p++) {
201                 if (*p == '\"')
202                         count++;
203         }
204
205         if (!(msgid_quoted = (char *)SMB_MALLOC(strlen(msgid) + count + 1)))
206                 return msgid;
207
208         /* string_sub() is unsuitable here as it replaces some punctuation
209            chars with underscores. */
210
211         for(p = msgid, q = msgid_quoted; *p; p++) {
212                 if (*p == '\"') {
213                         *q = '\\';
214                         q++;
215                 }
216                 *q = *p;
217                 q++;
218         }
219
220         *q = 0;
221
222         data = tdb_fetch_bystring(tdb, msgid_quoted);
223
224         free(msgid_quoted);
225
226         /* if the message isn't found then we still need to return a pointer
227            that can be freed. Pity. */
228         if (!data.dptr)
229                 return SMB_STRDUP(msgid);
230
231         return (const char *)data.dptr;
232 }
233
234
235 /* free up a string from lang_msg() */
236 void lang_msg_free(const char *msgstr)
237 {
238         if (!tdb) return;
239         free(discard_const_p(void, msgstr));
240 }
241
242 /* 
243    return the current language - needed for language file mappings 
244 */
245 char *lang_tdb_current(void)
246 {
247         return current_lang;
248 }