summaryrefslogtreecommitdiffstats
path: root/loader2/lang.c
blob: 3291f69dce479ec9b4ed686feb7f4053e39f5411 (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
/*
 * lang.c - determines language, handles translations
 *
 * Erik Troan <ewt@redhat.com>
 * Matt Wilson <msw@redhat.com>
 * Michael Fulbright <msf@redhat.com>
 * Jeremy Katz <katzj@redhat.com>
 *
 * Copyright 1997 - 2002 Red Hat, Inc.
 *
 * This software may be freely redistributed under the terms of the GNU
 * General Public License.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#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 <unistd.h>

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

#include "../isys/stubs.h"
#include "../isys/cpio.h"
#include "../isys/lang.h"

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

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

static char * topLineWelcome = N_("Welcome to %s");
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 void loadLanguageList(int flags) {
    char * file = FL_TESTING(flags) ? "../lang-table" :
                    "/etc/lang-table";
    FILE * f;
    char line[256];
    char name[256], key[256], font[256], map[256], code[256],
         keyboard[256], timezone[256];
    int lineNum = 0;

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

    while (fgets(line, sizeof(line), f)) {
        lineNum++;
        languages = realloc(languages, sizeof(*languages) * (numLanguages + 1));
        if (sscanf(line, "%s %s %s %s %s %s %s\n", name, key, font, map,
                                             code, keyboard, timezone) != 7) {
            logMessage("bad line %d in lang-table", lineNum);
        } else {
            languages[numLanguages].lang = strdup(name);
            languages[numLanguages].key = strdup(key);
            languages[numLanguages].font = strdup(font);
            languages[numLanguages].map = strdup(map);
            languages[numLanguages].lc_all = strdup(code);
            languages[numLanguages].keyboard = strdup(keyboard);
            numLanguages++;
        }
    }
}

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

    *langs = languages;
    return numLanguages;
}

void loadLanguage (char * file, int flags) {
    char filename[200];
    gzFile stream;
    int fd, hash, rc;
    char * key = getenv("LANGKEY");

    if (!key || !strcmp(key, "en_US")) {
        if (strings) {
            free(strings), strings = NULL;
            numStrings = allocedStrings = 0;
        }
        return;
    }

    if (!file) {
        file = filename;
        if (FL_TESTING(flags))
            sprintf(filename, "loader.tr");
        else
            sprintf(filename, "/etc/loader.tr");
    }

    stream = gunzip_open(file);

    if (!stream) {
        newtWinMessage("Error", "OK", "Translation for %s is not available.  "
                       "The Installation will proceed in English.", key);
        return ;
    }
    
    sprintf(filename, "%s.tr", key);

    rc = installCpioFile(stream, filename, "/tmp/translation", 1);
    gunzip_close(stream);

    if (rc || access("/tmp/translation", R_OK)) {
        newtWinMessage("Error", "OK", "Cannot get translation file %s.\n", 
                        filename);
        return;
    }
    
    fd = open("/tmp/translation", O_RDONLY);
    if (fd < 0) {
        newtWinMessage("Error", "OK", "Failed to open /tmp/translation: %s\n", 
                        strerror(errno));
        return;
    }

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

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

    close(fd);
    unlink("/tmp/translation");

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


void setLanguage (char * key, int flags) {
    int i;

    if (!languages) loadLanguageList(flags);

    for (i = 0; i < numLanguages; i++) {
        if (!strcmp(languages[i].key, key)) {
            if (!strcmp(languages[i].font, "None"))
                break;
            setenv("LANG", languages[i].lc_all, 1);
            setenv("LANGKEY", languages[i].key, 1);
            setenv("LC_ALL", languages[i].lc_all, 1);
            setenv("LINGUAS", languages[i].lc_all, 1);
            loadLanguage (NULL, flags);
            if (languages[i].map)
                isysLoadFont(languages[i].map);
            break;
        }
    }
}

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

    /* JKFIXME: I ripped out some of the wacky Kon stuff.  it might need
     * to come back for bterm */
    if (!languages) loadLanguageList(flags);

    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].lc_all, currentLangName))
            current = numLangs;

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

    langs[numLangs] = NULL;

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

    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();

    if (!strncmp(languages[choice].key, "en", 2)) {
        char *buf;
        /* stick with the default (English) */
        unsetenv("LANG");
        unsetenv("LANGKEY");
        unsetenv("LC_ALL");
        unsetenv("LINGUAS");
        if (strings) {
            free(strings), strings = NULL;
            numStrings = allocedStrings = 0;
        }
        buf = sdupprintf(_(topLineWelcome), PRODUCTNAME);
        newtDrawRootText(0, 0, buf);
        free(buf);
        newtPushHelpLine(_(bottomHelpLine));

        return 0;
    }

    /* only set the environment variables when we actually have a way
       to display the language */
    if (strcmp(languages[choice].font, "None")) {
        setenv("LANG", languages[choice].lc_all, 1);
        setenv("LANGKEY", languages[choice].key, 1);
        setenv("LC_ALL", languages[choice].lc_all, 1);
        setenv("LINGUAS", languages[choice].lc_all, 1);
    }
    
    if (strings) {
        free(strings), strings = NULL;
        numStrings = allocedStrings = 0;
    }

    /* load the language only if it is displayable */
    if (!strcmp(languages[choice].font, "None")) {
        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);
    } else {
        loadLanguage (NULL, flags);
    }

    if (languages[choice].map)
        isysLoadFont(languages[choice].map);

    
    buf = sdupprintf(_(topLineWelcome), PRODUCTNAME);
    newtDrawRootText(0, 0, buf);
    free(buf);
    newtPushHelpLine(_(bottomHelpLine));

    return 0;
}

void setKickstartLanguage(struct loaderData_s * loaderData, int argc, 
                          char ** argv, int * flagsPtr) {
    if (argc < 2) {
        logMessage("no argument passed to lang kickstart command");
        return;
    }

    loaderData->lang = argv[1];
    loaderData->lang_set = 1;

}