summaryrefslogtreecommitdiffstats
path: root/loader/lang.c
blob: 0b8a76f93f95580c14abb979e71bbb6e1f4b5782 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
/*
 * lang.c - determines language, handles translations
 *
 * Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003  Red Hat, Inc.
 * All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Author(s): Erik Troan <ewt@redhat.com>
 *            Matt Wilson <msw@redhat.com>
 *            Michael Fulbright <msf@redhat.com>
 *            Jeremy Katz <katzj@redhat.com>
 */

#include <alloca.h>
#include <errno.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <newt.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <wchar.h>
#include <glib.h>

#include "loader.h"
#include "lang.h"
#include "loadermisc.h"
#include "windows.h"
#include "unpack.h"

#include "../pyanaconda/isys/lang.h"
#include "../pyanaconda/isys/isys.h"
#include "../pyanaconda/isys/log.h"

const char *LANG_DEFAULT = "en_US.UTF-8";

/* boot flags */
extern uint64_t flags;

struct aString {
    unsigned int hash;
    short length;
    char * str;
} ;

struct aString * strings = NULL;
int numStrings = 0, allocedStrings = 0;

static int english = 0;

static char * topLineWelcome = N_("Welcome to %s for %s");
static char * topLineWelcomeRescue = N_("Welcome to %s for %s - Rescue Mode");
static char * bottomHelpLine = N_("  <Tab>/<Alt-Tab> between elements  | <Space> selects | <F12> next screen ");

static int aStringCmp(const void * a, const void * b) {
    const struct aString * first = a;
    const struct aString * second = b;

    if (first->hash < second->hash)
        return -1;
    else if (first->hash == second->hash)
        return 0;

    return 1;
}

char * translateString(char * str) {
    unsigned int sum = 0, xor = 0;
    int len = 0;
    char * chptr;
    struct aString * match;
    struct aString key;

    for (chptr = str; *chptr; chptr++) {
        sum += *chptr;
        xor ^= *chptr;
        len++;
    }

    key.hash = (sum << 16) | ((xor & 0xFF) << 8) | (len & 0xFF);
    match = bsearch(&key, strings, numStrings, sizeof(*strings), aStringCmp);
    if (!match)
        return str;

    return match->str;
}

static struct langInfo * languages = NULL;
static int numLanguages = 0;

static int defaultLanguageIndex(void) {
    int i;
    for (i = 0; i < numLanguages; ++i)
        if (!strcmp(languages[i].lc_all, LANG_DEFAULT))
            return i;
    return -1;
}

static void loadLanguageList(void) {
    char * file = "/etc/lang-table";
    FILE * f;
    char line[256];
    char name[256], key[256], text_supported[256], code[256],
        keyboard[256], timezone[256];
    int lineNum = 0;

    wcwidth(0);
    f = fopen(file, "r");
    if (!f) {
        newtWinMessage(_("Error"), _("OK"), "cannot open %s: %m", file);
        return;
    }

    while (fgets(line, sizeof(line), f)) {
        lineNum++;
        languages = realloc(languages, sizeof(*languages) * (numLanguages + 1));
        if (sscanf(line, "%[^\t]\t%[^\t]\t%[^\t]\t%[^\t]\t%[^\t]\t%[^\t]\n",
                   name, key, text_supported, code, keyboard, timezone) != 6) {
            printf("bad line %d in lang-table", lineNum);
            logMessage(WARNING, "bad line %d in lang-table", lineNum);
        } else {
            languages[numLanguages].lang = strdup(name);
            languages[numLanguages].key = strdup(key);
            languages[numLanguages].text_supported = !strcmp(text_supported, "True");
            languages[numLanguages].lc_all = strdup(code);
            languages[numLanguages++].keyboard = strdup(keyboard);
        }
    }
    fclose(f);
}

int getLangInfo(struct langInfo ** langs) {
    if (!languages)
        loadLanguageList();

    *langs = languages;
    return numLanguages;
}

/**
 * Normalize the value of lang= boot argument.
 *
 * Currently only replaces the trailing .utf8 with .UTF-8.
 *
 * Returns a heap-allocated string.
 */
gchar *normalizeLang(const gchar *s)
{
    if (g_str_has_suffix(s, ".utf8")) {
        gchar *lang = g_strndup(s, strlen(s) - strlen(".utf8"));
        gchar *result = g_strconcat(lang, ".UTF-8", NULL);
        g_free(lang);
        return result;
    }

    return g_strdup(s);
}

void loadLanguage(void)
{
    char *filename;
    int fd, hash, rc;
    char * key = getenv("LANGKEY");

    if (strings) {
	free(strings), strings = NULL;
	numStrings = allocedStrings = 0;
    }

    /* english requires no files */
    if (!strcmp(key, "en"))
        return;

    checked_asprintf(&filename, "/tmp/translation/%s.tr", key);

    rc = unpack_archive_file("/etc/loader.tr", "/tmp/translation");
    if (rc != ARCHIVE_OK || access("/tmp/translation", R_OK) == -1) {
        newtWinMessage("Error", "OK", "Cannot get translation file %s.\n", 
                        filename);
        return;
    }

    fd = open(filename, O_RDONLY);
    if (fd < 0) {
        newtWinMessage("Error", "OK", "Failed to open /tmp/translation: %m\n");
        free(filename);
        return;
    }

    while (read(fd, &hash, 4) == 4) {
        if (allocedStrings == numStrings) {
            allocedStrings += 10;
            strings = realloc(strings, sizeof(*strings) * allocedStrings);
        }

        strings[numStrings].hash = ntohl(hash);
        rc = read(fd, &strings[numStrings].length, 2);
        strings[numStrings].length = ntohs(strings[numStrings].length);
        strings[numStrings].str = malloc(strings[numStrings].length + 1);
        rc = read(fd, strings[numStrings].str, strings[numStrings].length);
        strings[numStrings].str[strings[numStrings].length] = '\0';
        numStrings++;
    }

    close(fd);
    free(filename);
    int translation_dir_fd = open("/tmp/translation", O_RDONLY);
    recursiveRemove(translation_dir_fd);
    close(translation_dir_fd);
    rmdir("/tmp/translation");

    qsort(strings, numStrings, sizeof(*strings), aStringCmp);
}


/* give the index of the language to set to -- sets the appropriate
 * lang variables if we have a font.
 *
 * ASSUMPTION: languages exists
 */
static void setLangEnv (int i) {
    if (i > numLanguages)
        return;

    if (!languages[i].text_supported)
        return;
    logMessage(INFO, "setting language to %s", languages[i].lc_all);

    setenv("LANG", languages[i].lc_all, 1);
    setenv("LANGKEY", languages[i].key, 1);
    setenv("LINGUAS", languages[i].lang, 1);
    loadLanguage();
}

/* choice is the index of the chosen language in languages */
static int setupLanguage(int choice, int forced) {
    char * buf;
    char *locale_def = NULL;    /* locale lang name */
    char *locale_charset = NULL;/* locale charset */
    char *locale_mod = NULL;    /* locale variant */
    char *locale_p = NULL;      /* last known locale separator */
    char *locale_i = NULL;      /* string iterator */
    int i;
    pid_t localedef_pid;

    logMessage(DEBUGLVL, "going to set language to %s", languages[choice].lc_all);

    /* Parse locale parts out of locale name.

      Locale examples:
      en
      en_US
      cz_CS.UTF-8
      cz_CS.UTF-8@latin
    */

    locale_p = locale_i = languages[choice].lc_all;
    while (1) {
        // we found new separator
        if (*locale_i == '.' || *locale_i == '@' || *locale_i == '\0') {
            // last separator was annotating charset
            if (*locale_p == '.') locale_charset = strndup(locale_p + 1, locale_i - locale_p - 1);
            // last separator was annotating modifier
            else if (*locale_p == '@') locale_mod = strndup(locale_p + 1, locale_i - locale_p - 1);
            // there was no known separator last time
            else locale_def = strndup(locale_p, locale_i - locale_p);

            // save last known separator
            locale_p = locale_i;

            if(*locale_i == '\0') break; // end of the string
        }

        // test next character
        locale_i++;
    }

    logMessage(DEBUGLVL, "locale %s: base: %s, mod: %s, charset: %s", languages[choice].lc_all, locale_def, locale_mod, locale_charset);

    /* prepare locale name without charset */
    i = strlen(locale_def);
    if (locale_mod) i+= strlen(locale_mod) + 1; /* +1 for the @ char */ 
    locale_p = (char*)calloc(i+1, sizeof(char));
    locale_p = strcpy(locale_p, locale_def);
    if (locale_mod){
        locale_p[strlen(locale_p)] = '@';
        locale_p = strcat(locale_p, locale_mod);
    }

    /* generate locale record */
    logMessage(INFO, "going to prepare locales for %s (locale: %s, charset: %s)", languages[choice].lc_all, locale_p, locale_charset);
    if ((localedef_pid = fork()) == 0) {
        execl("/usr/bin/localedef", "localedef",
              "-i", locale_p,
              "-f", (locale_charset) ? locale_charset : "UTF-8",
              languages[choice].lc_all, NULL);
        _exit(254);
    }

    if (localedef_pid < 0) logMessage(ERROR, "failed forking localedef for %s", languages[choice].lc_all);
    else{
        waitpid(localedef_pid, &i, 0);
        if (WEXITSTATUS(i) != 0) logMessage(ERROR, "failed preparing locales %s [%d]", languages[choice].lc_all, WEXITSTATUS(i));
    }

    /* cleanup */
    if (locale_charset) free(locale_charset);
    if (locale_def) free(locale_def);
    if (locale_mod) free(locale_mod);
    if (locale_p) free(locale_p);

    /* load the language only if it is displayable.  if they're using
     * a serial console or iSeries vioconsole, we hope it's smart enough */
    if (!languages[choice].text_supported && !FL_SERIAL(flags) &&
         !FL_VIRTPCONSOLE(flags) && !isVioConsole()) {
        if (forced == 1) return 0;

	newtWinMessage("Language Unavailable", "OK", 
		       "%s display is unavailable in text mode.  The "
		       "installation will continue in English until the "
		       "display of %s is possible.", languages[choice].lang,
		       languages[choice].lang);
        setLangEnv(english);
	return 0;
    }

    setLangEnv (choice);

    /* clear out top line */
    buf = alloca(81); /* reserve one byte for \0 */
    for (i=0; i < 80; i++)
	buf[i] = ' ';
    buf[80] = 0; /* and set the \0 */
    newtDrawRootText(0, 0, buf);

    char *fmt = FL_RESCUE(flags) ? _(topLineWelcomeRescue) : _(topLineWelcome);
    checked_asprintf(&buf, fmt, getProductName(), getProductArch());

    newtDrawRootText(0, 0, buf);
    free(buf);
    newtPopHelpLine();
    newtPushHelpLine(_(bottomHelpLine));

    return 0;
}

/* this is pretty simple.  we want to break down the language specifier
 * into its short form (eg, en_US)
 */
static char * getLangShortForm(char * oldLang) {
    char * lang;
    char * c;
    
    lang = strdup(oldLang);

    c = strchr(lang, '@');
    if (c) {
        *c = '\0';
    }

    c = strchr(lang, '.');
    if (c) {
        *c = '\0';
    }

    return lang;
}

/* return the nick of a language -- eg en_US -> en */
static char * getLangNick(char * oldLang) {
    char * lang;
    char * c;
    
    lang = strdup(oldLang);

    c = strchr(lang, '_');
    if (c) {
        *c = '\0';
    }

    return lang;
}

int setLanguage (const char * key, int forced) {
    int i;

    if (!languages) loadLanguageList();

    for (i = 0; i < numLanguages; i++) {
        if (!strcmp(languages[i].lc_all, key)) {
            return setupLanguage(i, forced | !FL_KICKSTART(flags));
        }
    }

    /* we didn't specify anything that's exactly in the lang-table.  check
     * against short forms and nicks */
    for (i = 0; i < numLanguages; i++) {
        if (!strcmp(getLangShortForm(languages[i].lc_all), key)) {
            return setupLanguage(i, forced | !FL_KICKSTART(flags));
        }
    }

    for (i = 0; i < numLanguages; i++) {
        if (!strcmp(getLangNick(languages[i].lc_all), key)) {
            return setupLanguage(i, forced | !FL_KICKSTART(flags));
        }
    }

    logMessage(ERROR, "unable to set the requested language '%s', "
               "setting the default '%s'", key, LANG_DEFAULT);
    return setupLanguage(defaultLanguageIndex(), forced | !FL_KICKSTART(flags));
}

int chooseLanguage(char ** lang) {
    int choice = 0;
    char ** langs;
    int i;
    int current = -1;
    char * currentLangName = getenv("LANG");
    int numLangs = 0;
    char * langPicked;

    if (!languages) loadLanguageList();

    langs = alloca(sizeof(*langs) * (numLanguages + 1)); 

    for (i = 0; i < numLanguages; i++) {
        if (!strncmp(languages[i].key, "en", 2))
            english = numLangs;
        if (currentLangName &&
            !strcmp(languages[i].lang, currentLangName))
            current = numLangs;

        langs[numLangs++] = languages[i].lang;
    }

    langs[numLangs] = NULL;

    if (current >= 0)
        choice = current;
    else
        choice = english;

    if (!FL_CMDLINE(flags))
        newtWinMenu(_("Choose a Language"),
                    _("What language would you like to use during the "
                      "installation process?"), 40, 5, 5, 8,
                    langs, &choice, _("OK"), NULL);

    langPicked = langs[choice];
    for (i = 0; i < numLanguages; i++) {
        if (!strcmp(langPicked, languages[i].lang)) {
            *lang = languages[i].lc_all;
            choice = i;
            break;
        }
    }

    /* this can't happen */
    if (i == numLanguages) abort();

    return setupLanguage(choice, 0);
}