r3447: more include/system/XXX.h include files
[bbaumbach/samba-autobuild/.git] / source4 / 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 #include "system/time.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 key, data;
37
38         lines = file_lines_load(msg_file, &num_lines);
39
40         if (!lines) {
41                 return False;
42         }
43
44         if (tdb_lockall(tdb) != 0) return False;
45
46         /* wipe the db */
47         tdb_traverse(tdb, tdb_traverse_delete_fn, NULL);
48
49         msgid = NULL;
50         
51         for (i=0;i<num_lines;i++) {
52                 if (strncmp(lines[i], "msgid \"", 7) == 0) {
53                         msgid = lines[i] + 7;
54                 }
55                 if (msgid && strncmp(lines[i], "msgstr \"", 8) == 0) {
56                         msgstr = lines[i] + 8;
57                         trim_string(msgid, NULL, "\"");
58                         trim_string(msgstr, NULL, "\"");
59                         if (*msgstr == 0) {
60                                 msgstr = msgid;
61                         }
62                         key.dptr = msgid;
63                         key.dsize = strlen(msgid)+1;
64                         data.dptr = msgstr;
65                         data.dsize = strlen(msgstr)+1;
66                         tdb_store(tdb, key, data, 0);
67                         msgid = NULL;
68                 }
69         }
70
71         file_lines_free(lines);
72         tdb_unlockall(tdb);
73
74         return True;
75 }
76
77
78 /* work out what language to use from locale variables */
79 static const char *get_lang(void)
80 {
81         const char *vars[] = {"LANGUAGE", "LC_ALL", "LC_LANG", "LANG", NULL};
82         int i;
83         char *p;
84
85         for (i=0; vars[i]; i++) {
86                 if ((p = getenv(vars[i]))) {
87                         return p;
88                 }
89         }
90
91         return NULL;
92 }
93
94 /* initialise the message translation subsystem. If the "lang" argument
95    is NULL then get the language from the normal environment variables */
96 BOOL lang_tdb_init(const char *lang)
97 {
98         char *path = NULL;
99         char *msg_path = NULL;
100         struct stat st;
101         static int initialised;
102         time_t loadtime;
103         TALLOC_CTX *mem_ctx;
104
105         /* we only want to init once per process, unless given
106            an override */
107         if (initialised && !lang) return True;
108
109         if (initialised) {
110                 /* we are re-initialising, free up any old init */
111                 if (tdb) {
112                         tdb_close(tdb);
113                         tdb = NULL;
114                 }
115                 SAFE_FREE(current_lang);
116         }
117
118         initialised = 1;
119
120         if (!lang) {
121                 /* no lang given, use environment */
122                 lang = get_lang();
123         }
124
125         /* if no lang then we don't translate */
126         if (!lang) return True;
127
128         mem_ctx = talloc_init("lang_tdb_init");
129         if (!mem_ctx) {
130                 return False;
131         }
132         asprintf(&msg_path, "%s.msg", lib_path(mem_ctx, (const char *)lang));
133         if (stat(msg_path, &st) != 0) {
134                 /* the msg file isn't available */
135                 free(msg_path);
136                 talloc_destroy(mem_ctx);
137                 return False;
138         }
139         
140
141         asprintf(&path, "%s%s.tdb", lock_path(mem_ctx, "lang_"), lang);
142
143         tdb = tdb_open(path, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0644);
144         if (!tdb) {
145                 tdb = tdb_open(path, 0, TDB_DEFAULT, O_RDONLY, 0);
146                 free(path);
147                 free(msg_path);
148                 talloc_destroy(mem_ctx);
149                 if (!tdb) return False;
150                 current_lang = strdup(lang);
151                 return True;
152         }
153
154         free(path);
155         talloc_destroy(mem_ctx);
156
157         loadtime = tdb_fetch_int32(tdb, "/LOADTIME/");
158
159         if (loadtime == -1 || loadtime < st.st_mtime) {
160                 load_msg(msg_path);
161                 tdb_store_int32(tdb, "/LOADTIME/", (int)time(NULL));
162         }
163         free(msg_path);
164
165         current_lang = strdup(lang);
166
167         return True;
168 }
169
170 /* translate a msgid to a message string in the current language 
171    returns a string that must be freed by calling lang_msg_free()
172 */
173 char *lang_msg(const char *msgid)
174 {
175         TDB_DATA key, data;
176
177         lang_tdb_init(NULL);
178
179         if (!tdb) return strdup(msgid);
180
181         key.dptr = strdup(msgid);
182         key.dsize = strlen(msgid)+1;
183         
184         data = tdb_fetch(tdb, key);
185
186         free(key.dptr);
187
188         /* if the message isn't found then we still need to return a pointer
189            that can be freed. Pity. */
190         if (!data.dptr)
191                 return strdup(msgid);
192
193         return data.dptr;
194 }
195
196
197 /* free up a string from lang_msg() */
198 void lang_msg_free(char *msgstr)
199 {
200         free(msgstr);
201 }
202
203
204 /*
205   when the _() translation macro is used there is no obvious place to free
206   the resulting string and there is no easy way to give a static pointer.
207   All we can do is rotate between some static buffers and hope a single d_printf() 
208   doesn't have more calls to _() than the number of buffers 
209 */
210 const char *lang_msg_rotate(const char *msgid)
211 {
212 #define NUM_LANG_BUFS 4
213         const char *msgstr;
214         static pstring bufs[NUM_LANG_BUFS];
215         static int next;
216
217         msgstr = lang_msg(msgid);
218         if (!msgstr) return msgid;
219
220         pstrcpy(bufs[next], msgstr);
221         msgstr = bufs[next];
222
223         next = (next+1) % NUM_LANG_BUFS;
224         
225         return msgstr;
226 }
227
228
229 /* 
230    return the current language - needed for language file mappings 
231 */
232 char *lang_tdb_current(void)
233 {
234         return current_lang;
235 }