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