File: validlocale

package info (click to toggle)
glibc 2.24-11%2Bdeb9u4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 225,852 kB
  • sloc: ansic: 996,505; asm: 261,827; sh: 10,484; makefile: 9,856; cpp: 4,169; python: 3,971; perl: 2,254; awk: 1,753; pascal: 1,521; yacc: 291; sed: 80
file content (75 lines) | stat: -rwxr-xr-x 1,773 bytes parent folder | download | duplicates (49)
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
#!/usr/bin/perl -w
#
# Author: Petter Reinholdtsen <pere@hungry.com>
# Date:   2002-02-23
#
# Test if the locale given as argument is a valid locale.  If it
# is not, print on stdout the string to add to /etc/locale.gen to make
# locale-gen generate the locale (if it exists at all).

use POSIX qw(setlocale LC_ALL);

my $debug = 0;

my $defaultcharset = $ENV{"DEFAULTCHARSET"} || "ISO-8859-1";

my $supportedlist = "/usr/share/i18n/SUPPORTED";

unless (defined $ARGV[0]) {
    usage();
    exit 1;
}

my $LANG = $ARGV[0];

my $loc = setlocale(LC_ALL, $LANG);
if ( ! $loc) {
    print STDERR "locale '$LANG' not available\n";

    my ($locale)   = $LANG =~ m/^([^.@]+)/;
    my ($charset)  = $LANG =~ m/^[^.]+\.([^@]+)/;
    my ($modifier) = $LANG =~ m/(@.+)$/;

    $modifier = "" unless defined $modifier;

    # Hm, if charset is missing, how to we pick the correct one to
    # use?  Fetching the value from /usr/share/i18n/SUPPORTED should
    # work on Debian.
    my $codeset = "";
    if (defined $charset) {
       $codeset = '.' . $charset;
    } else {
       $charset = get_default_charset("$locale$modifier");
    }

    # print "L: $locale C: $charset M: $modifier\n";
    print "$locale$codeset$modifier $charset\n";

    exit 1;
} else {
    print STDERR "locale '$LANG' valid and available\n";
    exit 0;
}

sub usage {
    print "Usage: $0 <locale>\n"
}

sub get_default_charset {
    my ($locale) = @_;
    my ($l, $c);
    open(SUPPORTED, "< $supportedlist") || die "Unable to open $supportedlist";
    while (<SUPPORTED>) {
	chomp;
	($l, $c) = split(/\s+/);
	print "Checking '$l' '$c' != '$locale'\n" if $debug;
	last if  ($l eq $locale);
    }
    close(SUPPORTED);

    if ($l eq $locale) {
	return $c;
    } else {
	return $defaultcharset;
    }
}