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