Debian Codegraph @

nx-X11/lib/src/xlibi18n/lcFile.c

raw
1 /*
2 *
3 * Copyright IBM Corporation 1993
4 *
5 * All Rights Reserved
6 *
7 * License to use, copy, modify, and distribute this software and its
8 * documentation for any purpose and without fee is hereby granted,
9 * provided that the above copyright notice appear in all copies and that
10 * both that copyright notice and this permission notice appear in
11 * supporting documentation, and that the name of IBM not be
12 * used in advertising or publicity pertaining to distribution of the
13 * software without specific, written prior permission.
14 *
15 * IBM DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
16 * ALL IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS, AND
17 * NONINFRINGEMENT OF THIRD PARTY RIGHTS, IN NO EVENT SHALL
18 * IBM BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
19 * ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
20 * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
21 * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
22 * SOFTWARE.
23 *
24 */
25
26 #ifdef HAVE_CONFIG_H
27 #include <config.h>
28 #endif
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <ctype.h>
32 #include "Xlibint.h"
33 #include "XlcPubI.h"
34 #include <nx-X11/Xos.h>
35 #include <unistd.h>
36
37 /************************************************************************/
38
39 #ifndef HAVE_SETEUID
40 # define seteuid setuid
41 #endif
42 #define iscomment(ch) ((ch) == '#' || (ch) == '\0')
43 #if defined(WIN32)
44 #define isreadable(f) (_XAccessFile(f))
45 #else
46 #define isreadable(f) ((access((f), R_OK) != -1) ? 1 : 0)
47 #endif
48
49 #define LC_PATHDELIM ':'
50
51 #define XLC_BUFSIZE 256
52
53 #include "pathmax.h"
54
55 #define NUM_LOCALEDIR 64
56
57 /* Splits a NUL terminated line into constituents, at colons and newline
58 characters. Leading whitespace is removed from constituents. The
59 constituents are stored at argv[0..argsize-1]. The number of stored
60 constituents (<= argsize) is returned. The line is destructively
61 modified. */
62 static int
63 parse_line(
64 char *line,
65 char **argv,
66 int argsize)
67 {
68 int argc = 0;
69 char *p = line;
70
71 while (argc < argsize) {
72 while (isspace(*p)) {
73 ++p;
74 }
75 if (*p == '\0') {
76 break;
77 }
78 argv[argc++] = p;
79 while (*p != ':' && *p != '\n' && *p != '\0') {
80 ++p;
81 }
82 if (*p == '\0') {
83 break;
84 }
85 *p++ = '\0';
86 }
87
88 return argc;
89 }
90
91 #if defined(WIN32)
92
93 /* this is parse_line but skips drive letters at the beginning of the entry */
94 static int
95 parse_line1(
96 char *line,
97 char **argv,
98 int argsize)
99 {
100 int argc = 0;
101 char *p = line;
102
103 while (argc < argsize) {
104 while (isspace(*p)) {
105 ++p;
106 }
107 if (*p == '\0') {
108 break;
109 }
110 argv[argc++] = p;
111 if (isalpha(*p) && p[1] == ':') {
112 p+= 2; /* skip drive letters */
113 }
114 while (*p != ':' && *p != '\n' && *p != '\0') {
115 ++p;
116 }
117 if (*p == '\0') {
118 break;
119 }
120 *p++ = '\0';
121 }
122
123 return argc;
124 }
125
126 #endif /* WIN32 */
127
128 /* Splits a colon separated list of directories, and returns the constituent
129 paths (without trailing slash). At most argsize constituents are stored
130 at argv[0..argsize-1]. The number of stored constituents is returned. */
131 static int
132 _XlcParsePath(
133 char *path,
134 char **argv,
135 int argsize)
136 {
137 char *p = path;
138 int n, i;
139
140 #if !defined(WIN32)
141 n = parse_line(path, argv, argsize);
142 #else
143 n = parse_line1(path, argv, argsize);
144 #endif
145 for (i = 0; i < n; ++i) {
146 int len;
147 p = argv[i];
148 len = (int) strlen(p);
149 if (len > 0 && p[len - 1] == '/') {
150 /* eliminate trailing slash */
151 p[len - 1] = '\0';
152 }
153 }
154 return n;
155 }
156
157 #ifndef XLOCALEDIR
158 #define XLOCALEDIR "/usr/lib/X11/locale"
159 #endif
160
161 void
162 xlocaledir(
163 char *buf,
164 int buf_len)
165 {
166 char *p = buf;
167 int len = 0;
168
169 #ifndef NO_XLOCALEDIR
170 char *dir;
171 int priv = 1;
172
173 dir = getenv("XLOCALEDIR");
174
175 if (dir) {
176 #ifndef WIN32
177 /*
178 * Only use the user-supplied path if the process isn't privileged.
179 */
180 if (getuid() == geteuid() && getgid() == getegid()) {
181 #if defined(HASSETUGID)
182 priv = issetugid();
183 #elif defined(HASGETRESUID)
184 {
185 uid_t ruid, euid, suid;
186 gid_t rgid, egid, sgid;
187 if ((getresuid(&ruid, &euid, &suid) == 0) &&
188 (getresgid(&rgid, &egid, &sgid) == 0))
189 priv = (euid != suid) || (egid != sgid);
190 }
191 #else
192 /*
193 * If there are saved ID's the process might still be privileged
194 * even though the above test succeeded. If issetugid() and
195 * getresgid() aren't available, test this by trying to set
196 * euid to 0.
197 *
198 * Note: this only protects setuid-root clients. It doesn't
199 * protect other setuid or any setgid clients. If this tradeoff
200 * isn't acceptable, set DisableXLocaleDirEnv to YES in host.def.
201 */
202 unsigned int oldeuid;
203 oldeuid = geteuid();
204 if (seteuid(0) != 0) {
205 priv = 0;
206 } else {
207 if (seteuid(oldeuid) == -1) {
208 /* XXX ouch, couldn't get back to original uid
209 what can we do ??? */
210 _exit(127);
211 }
212 priv = 1;
213 }
214 #endif
215 }
216 #else
217 priv = 0;
218 #endif
219 if (!priv) {
220 len = (int) strlen(dir);
221 strncpy(p, dir, (size_t) buf_len);
222 if (len < buf_len) {
223 p[len++] = LC_PATHDELIM;
224 p += len;
225 }
226 }
227 }
228 #endif /* NO_XLOCALEDIR */
229
230 if (len < buf_len)
231 strncpy(p, XLOCALEDIR, (size_t) (buf_len - len));
232 buf[buf_len-1] = '\0';
233 }
234
235 static void
236 xlocalelibdir(
237 char *buf,
238 int buf_len)
239 {
240 char *p = buf;
241 int len = 0;
242
243 #ifndef NO_XLOCALEDIR
244 char *dir;
245 int priv = 1;
246
247 dir = getenv("XLOCALELIBDIR");
248
249 if (dir) {
250 #ifndef WIN32
251 /*
252 * Only use the user-supplied path if the process isn't privileged.
253 */
254 if (getuid() == geteuid() && getgid() == getegid()) {
255 #if defined(HASSETUGID)
256 priv = issetugid();
257 #elif defined(HASGETRESUID)
258 {
259 uid_t ruid, euid, suid;
260 gid_t rgid, egid, sgid;
261 if ((getresuid(&ruid, &euid, &suid) == 0) &&
262 (getresgid(&rgid, &egid, &sgid) == 0))
263 priv = (euid != suid) || (egid != sgid);
264 }
265 #else
266 /*
267 * If there are saved ID's the process might still be privileged
268 * even though the above test succeeded. If issetugid() and
269 * getresgid() aren't available, test this by trying to set
270 * euid to 0.
271 *
272 * Note: this only protects setuid-root clients. It doesn't
273 * protect other setuid or any setgid clients. If this tradeoff
274 * isn't acceptable, set DisableXLocaleDirEnv to YES in host.def.
275 */
276 unsigned int oldeuid;
277 oldeuid = geteuid();
278 if (seteuid(0) != 0) {
279 priv = 0;
280 } else {
281 if (seteuid(oldeuid) == -1) {
282 /* XXX ouch, couldn't get back to original uid
283 what can we do ??? */
284 _exit(127);
285 }
286 priv = 1;
287 }
288 #endif
289 }
290 #else
291 priv = 0;
292 #endif
293 if (!priv) {
294 len = (int) strlen(dir);
295 strncpy(p, dir, (size_t) buf_len);
296 if (len < buf_len) {
297 p[len++] = LC_PATHDELIM;
298 p += len;
299 }
300 }
301 }
302 #endif /* NO_XLOCALEDIR */
303
304 if (len < buf_len)
305 strncpy(p, XLOCALELIBDIR, (size_t) (buf_len - len));
306 buf[buf_len-1] = '\0';
307 }
308
309 /* Mapping direction */
310 typedef enum {
311 LtoR, /* Map first field to second field */
312 RtoL /* Map second field to first field */
313 } MapDirection;
314
315 static char *
316 resolve_name(
317 const char *lc_name,
318 char *file_name,
319 MapDirection direction)
320 {
321 FILE *fp;
322 char buf[XLC_BUFSIZE], *name = NULL;
323
324 fp = _XFopenFile (file_name, "r");
325 if (fp == NULL)
326 return NULL;
327
328 while (fgets(buf, XLC_BUFSIZE, fp) != NULL) {
329 char *p = buf;
330 int n;
331 char *args[2], *from, *to;
332 while (isspace(*p)) {
333 ++p;
334 }
335 if (iscomment(*p)) {
336 continue;
337 }
338 n = parse_line(p, args, 2); /* get first 2 fields */
339 if (n != 2) {
340 continue;
341 }
342 if (direction == LtoR) {
343 from = args[0], to = args[1]; /* left to right */
344 } else {
345 from = args[1], to = args[0]; /* right to left */
346 }
347 if (! strcmp(from, lc_name)) {
348 name = strdup(to);
349 break;
350 }
351 }
352 fclose(fp);
353 return name;
354 }
355
356 #define c_tolower(ch) ((ch) >= 'A' && (ch) <= 'Z' ? (ch) - 'A' + 'a' : (ch))
357
358 static char *
359 lowercase(
360 char *dst,
361 const char *src)
362 {
363 const char *s;
364 char *t;
365
366 for (s = src, t = dst; *s; ++s, ++t)
367 *t = (char) c_tolower(*s);
368 *t = '\0';
369 return dst;
370 }
371
372 /*
373 * normalize_lcname(): remove any '_' and '-' and convert any character
374 * to lower case after the <language>_<territory> part. If result is identical
375 * to argument, free result and
376 * return NULL.
377 */
378 static char *
379 normalize_lcname (const char *name)
380 {
381 char *p, *ret;
382 const char *tmp = name;
383
384 p = ret = Xmalloc(strlen(name) + 1);
385 if (!p)
386 return NULL;
387
388 if (tmp) {
389 while (*tmp && *tmp != '.' && *tmp != '@')
390 *p++ = *tmp++;
391 while (*tmp) {
392 if (*tmp != '-')
393 *p++ = (char) c_tolower(*tmp);
394 tmp++;
395 }
396 }
397 *p = '\0';
398
399 if (strcmp(ret, name) == 0) {
400 Xfree(ret);
401 return NULL;
402 }
403
404 return ret;
405 }
406
407 /************************************************************************/
408 char *
409 _XlcFileName(
410 XLCd lcd,
411 const char *category)
412 {
413 char *siname;
414 char cat[XLC_BUFSIZE], dir[XLC_BUFSIZE];
415 int i, n;
416 char *args[NUM_LOCALEDIR];
417 char *file_name = NULL;
418
419 if (lcd == (XLCd)NULL)
420 return NULL;
421
422 siname = XLC_PUBLIC(lcd, siname);
423
424 if (category)
425 lowercase(cat, category);
426 else
427 cat[0] = '\0';
428 xlocaledir(dir,XLC_BUFSIZE);
429 n = _XlcParsePath(dir, args, NUM_LOCALEDIR);
430 for (i = 0; i < n; ++i) {
431 char buf[PATH_MAX], *name;
432
433 if (args[i] == NULL)
434 continue;
435
436 name = NULL;
437 if (snprintf(buf, PATH_MAX, "%s/%s.dir", args[i], cat) < PATH_MAX) {
438 name = resolve_name(siname, buf, RtoL);
439 }
440 if (name == NULL) {
441 continue;
442 }
443 if (*name == '/') {
444 /* supposed to be absolute path name */
445 file_name = name;
446 } else {
447 if (snprintf(buf, PATH_MAX, "%s/%s", args[i], name) < PATH_MAX)
448 file_name = strdup(buf);
449 else
450 file_name = NULL;
451 Xfree(name);
452 }
453 if (file_name && isreadable(file_name)) {
454 break;
455 }
456 Xfree(file_name);
457 file_name = NULL;
458 /* Then, try with next dir */
459 }
460 return file_name;
461 }
462
463 /************************************************************************/
464 #ifndef LOCALE_ALIAS
465 #define LOCALE_ALIAS "locale.alias"
466 #endif
467
468 int
469 _XlcResolveLocaleName(
470 const char* lc_name,
471 XLCdPublicPart* pub)
472 {
473 char dir[PATH_MAX], buf[PATH_MAX], *name = NULL;
474 char *dst;
475 int i, n, sinamelen;
476 char *args[NUM_LOCALEDIR];
477 static const char locale_alias[] = LOCALE_ALIAS;
478 char *tmp_siname;
479 char *nlc_name = NULL;
480
481 xlocaledir (dir, PATH_MAX);
482 n = _XlcParsePath(dir, args, NUM_LOCALEDIR);
483 for (i = 0; i < n; ++i) {
484 if (args[i] == NULL)
485 continue;
486
487 if (snprintf (buf, PATH_MAX, "%s/%s", args[i], locale_alias)
488 < PATH_MAX) {
489 name = resolve_name (lc_name, buf, LtoR);
490 if (!name) {
491 if (!nlc_name)
492 nlc_name = normalize_lcname(lc_name);
493 if (nlc_name)
494 name = resolve_name (nlc_name, buf, LtoR);
495 }
496 }
497 if (name != NULL) {
498 break;
499 }
500 }
501 Xfree(nlc_name);
502
503 if (name == NULL) {
504 /* vendor locale name == Xlocale name, no expansion of alias */
505 pub->siname = strdup (lc_name);
506 } else {
507 pub->siname = name;
508 }
509
510 sinamelen = (int) strlen (pub->siname);
511 if (sinamelen == 1 && pub->siname[0] == 'C') {
512 pub->language = pub->siname;
513 pub->territory = pub->codeset = NULL;
514 return 1;
515 }
516
517 /*
518 * pub->siname is in the format <lang>_<terr>.<codeset>, typical would
519 * be "en_US.ISO8859-1", "en_US.utf8", "ru_RU.KOI-8", or ja_JP.SJIS,
520 * although it could be ja.SJIS too.
521 */
522 tmp_siname = Xrealloc (pub->siname, 2 * (sinamelen + 1));
523 if (tmp_siname == NULL) {
524 return 0;
525 }
526 pub->siname = tmp_siname;
527
528 /* language */
529 dst = &pub->siname[sinamelen + 1];
530 strcpy (dst, pub->siname);
531 pub->language = dst;
532
533 /* territory */
534 dst = strchr (dst, '_');
535 if (dst) {
536 *dst = '\0';
537 pub->territory = ++dst;
538 } else
539 dst = &pub->siname[sinamelen + 1];
540
541 /* codeset */
542 dst = strchr (dst, '.');
543 if (dst) {
544 *dst = '\0';
545 pub->codeset = ++dst;
546 }
547
548 return (pub->siname[0] != '\0') ? 1 : 0;
549 }
550
551 /************************************************************************/
552 int
553 _XlcResolveI18NPath(char *buf, int buf_len)
554 {
555 if (buf != NULL) {
556 xlocaledir(buf, buf_len);
557 }
558 return 1;
559 }
560
561 char *
562 _XlcLocaleDirName(char *dir_name, size_t dir_len, const char *lc_name)
563 {
564 char dir[PATH_MAX], buf[PATH_MAX];
565 int i, n;
566 char *args[NUM_LOCALEDIR];
567 static char locale_alias[] = LOCALE_ALIAS;
568 char *target_name = NULL;
569 char *target_dir = NULL;
570 char *nlc_name = NULL;
571 static char* last_dir_name = 0;
572 static size_t last_dir_len = 0;
573 static char* last_lc_name = 0;
574
575 if (last_lc_name != 0 && strcmp (last_lc_name, lc_name) == 0
576 && dir_len >= last_dir_len) {
577 strcpy (dir_name, last_dir_name);
578 return dir_name;
579 }
580
581 xlocaledir (dir, PATH_MAX);
582 n = _XlcParsePath(dir, args, NUM_LOCALEDIR);
583 for (i = 0; i < n; ++i) {
584 char *name = NULL;
585
586 if (args[i] == NULL)
587 continue;
588
589 if (snprintf (buf, PATH_MAX, "%s/%s", args[i], locale_alias)
590 < PATH_MAX) {
591 name = resolve_name(lc_name, buf, LtoR);
592 if (!name) {
593 if (!nlc_name)
594 nlc_name = normalize_lcname(lc_name);
595 if (nlc_name)
596 name = resolve_name (nlc_name, buf, LtoR);
597 }
598 }
599
600 /* look at locale.dir */
601
602 target_dir = args[i];
603 if (snprintf(buf, PATH_MAX, "%s/locale.dir", target_dir) < PATH_MAX) {
604 /* If name is not an alias, use lc_name for locale.dir search */
605 target_name = resolve_name(name ? name : lc_name, buf, RtoL);
606 }
607 Xfree(name);
608 name = NULL;
609 if (target_name != NULL) {
610 char *p = 0;
611 if ((p = strstr(target_name, "/XLC_LOCALE"))) {
612 *p = '\0';
613 break;
614 }
615 Xfree(target_name);
616 target_name = NULL;
617 }
618 }
619 Xfree(nlc_name);
620
621 if (target_name == NULL)
622 /* vendor locale name == Xlocale name, no expansion of alias */
623 snprintf(dir_name, dir_len, "%s/%s", args[0], lc_name);
624 else
625 snprintf(dir_name, dir_len, "%s/%s", target_dir, target_name);
626
627 Xfree(target_name);
628 Xfree (last_dir_name);
629 Xfree (last_lc_name);
630
631 last_dir_len = strlen (dir_name) + 1;
632 last_dir_name = Xmalloc (last_dir_len);
633 strcpy (last_dir_name, dir_name);
634 last_lc_name = strdup (lc_name);
635
636 return dir_name;
637 }
638
639 char *
640 _XlcLocaleLibDirName(char *dir_name, size_t dir_len, const char *lc_name)
641 {
642 char dir[PATH_MAX], buf[PATH_MAX];
643 int i, n;
644 char *args[NUM_LOCALEDIR];
645 static char locale_alias[] = LOCALE_ALIAS;
646 char *target_name = NULL;
647 char *target_dir = NULL;
648 char *nlc_name = NULL;
649 static char* last_dir_name = 0;
650 static size_t last_dir_len = 0;
651 static char* last_lc_name = 0;
652
653 if (last_lc_name != 0 && strcmp (last_lc_name, lc_name) == 0
654 && dir_len >= last_dir_len) {
655 strcpy (dir_name, last_dir_name);
656 return dir_name;
657 }
658
659 xlocalelibdir (dir, PATH_MAX);
660 n = _XlcParsePath(dir, args, NUM_LOCALEDIR);
661 for (i = 0; i < n; ++i) {
662 char *name = NULL;
663
664 if (args[i] == NULL)
665 continue;
666
667 if (snprintf (buf, PATH_MAX, "%s/%s", args[i], locale_alias)
668 < PATH_MAX) {
669 name = resolve_name(lc_name, buf, LtoR);
670 if (!name) {
671 if (!nlc_name)
672 nlc_name = normalize_lcname(lc_name);
673 if (nlc_name)
674 name = resolve_name (nlc_name, buf, LtoR);
675 }
676 }
677
678 /* look at locale.dir */
679
680 target_dir = args[i];
681 if (snprintf(buf, PATH_MAX, "%s/locale.dir", target_dir) < PATH_MAX) {
682 /* If name is not an alias, use lc_name for locale.dir search */
683 target_name = resolve_name(name ? name : lc_name, buf, RtoL);
684 }
685 Xfree(name);
686 name = NULL;
687 if (target_name != NULL) {
688 char *p = 0;
689 if ((p = strstr(target_name, "/XLC_LOCALE"))) {
690 *p = '\0';
691 break;
692 }
693 Xfree(target_name);
694 target_name = NULL;
695 }
696 }
697 Xfree(nlc_name);
698
699 if (target_name == NULL)
700 /* vendor locale name == Xlocale name, no expansion of alias */
701 snprintf(dir_name, dir_len, "%s/%s", args[0], lc_name);
702 else
703 snprintf(dir_name, dir_len, "%s/%s", target_dir, target_name);
704 Xfree(target_name);
705 Xfree (last_dir_name);
706 Xfree (last_lc_name);
707
708 last_dir_len = strlen (dir_name) + 1;
709 last_dir_name = Xmalloc (last_dir_len);
710 strcpy (last_dir_name, dir_name);
711 last_lc_name = strdup (lc_name);
712
713 return dir_name;
714 }