| 1 |
#! /usr/bin/perl -w |
| 2 |
|
| 3 |
use strict; |
| 4 |
use Getopt::Long; |
| 5 |
|
| 6 |
my $progname = "update-locale"; |
| 7 |
my $locale_file = "/etc/locale.conf"; |
| 8 |
|
| 9 |
my $help = 0; |
| 10 |
my $reset = 0; |
| 11 |
# Kept for compatibility reasons |
| 12 |
my $remove = 0; |
| 13 |
my $no_checks = 0; |
| 14 |
|
| 15 |
GetOptions( |
| 16 |
'reset' => \$reset, |
| 17 |
'remove' => \$remove, |
| 18 |
'locale-file=s' => \$locale_file, |
| 19 |
'no-checks' => \$no_checks, |
| 20 |
'h|help' => \$help, |
| 21 |
); |
| 22 |
|
| 23 |
sub usage |
| 24 |
{ |
| 25 |
my $rc = shift; |
| 26 |
print STDERR "Usage: $progname [OPTIONS] [LANG=locale] [LC_NUMERIC=locale] ... |
| 27 |
Options: |
| 28 |
--help display this message and exit |
| 29 |
--reset ignore variables defined in the locale file |
| 30 |
--locale-file=FILE file containing locale variables |
| 31 |
(Default: /etc/locale.conf) |
| 32 |
--no-checks do not perform sanity checks on locale variables |
| 33 |
"; |
| 34 |
exit $rc; |
| 35 |
} |
| 36 |
|
| 37 |
$help && usage(0); |
| 38 |
|
| 39 |
# Process command-line arguments |
| 40 |
my %arg = (); |
| 41 |
my $content = ''; |
| 42 |
my $mode = 0644; |
| 43 |
if (-r $locale_file) |
| 44 |
{ |
| 45 |
# Keep file mode |
| 46 |
$mode = (stat($locale_file))[2] & 07777; |
| 47 |
# Read current values |
| 48 |
open(IN, "<", $locale_file) |
| 49 |
or die "$progname: Unable to read $locale_file: $!\n"; |
| 50 |
while (<IN>) |
| 51 |
{ |
| 52 |
$content .= $_; |
| 53 |
next unless m/^(\w+)=(.*)/; |
| 54 |
$arg{$1} = $2 unless $reset; |
| 55 |
} |
| 56 |
close(IN) |
| 57 |
or die "$progname: Unable to close $locale_file: $!\n"; |
| 58 |
$content =~ s/^(\s*\w+=)/#$1/mg; |
| 59 |
$content .= "\n" unless $content =~ m/\n$/s; |
| 60 |
} |
| 61 |
else |
| 62 |
{ |
| 63 |
$content = "# File generated by $progname\n"; |
| 64 |
} |
| 65 |
for (@ARGV) |
| 66 |
{ |
| 67 |
if (m/(\w+)=(.*)/) |
| 68 |
{ |
| 69 |
$arg{$1} = $2; |
| 70 |
} |
| 71 |
else |
| 72 |
{ |
| 73 |
delete $arg{$_}; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
my $env = ''; |
| 78 |
my ($key, $value); |
| 79 |
while (($key, $value) = each %arg) |
| 80 |
{ |
| 81 |
$env .= " $key=$value"; |
| 82 |
$content =~ s/^#\s*$key=.*/$key=$value/m or |
| 83 |
$content .= "$key=$value\n"; |
| 84 |
} |
| 85 |
|
| 86 |
# Sanity checks |
| 87 |
if ($no_checks == 0) |
| 88 |
{ |
| 89 |
# Check that this locale does exist |
| 90 |
my $charset = `LANG= LC_CTYPE= LC_NUMERIC= LC_TIME= LC_COLLATE= LC_MONETARY= LC_MESSAGES= LC_PAPER= LC_NAME= LC_ADDRESS= LC_TELEPHONE= LC_MEASUREMENT= LC_IDENTIFICATION= LC_ALL= $env locale charmap 2>&1`; |
| 91 |
die "*** $progname: Error: invalid locale settings: $env\n----------\n$charset----------\n" |
| 92 |
if ($charset =~ m/Cannot set/); |
| 93 |
# If LANGUAGE is set, its first value must be compatible with LC_MESSAGES |
| 94 |
if (defined $arg{LANGUAGE}) |
| 95 |
{ |
| 96 |
my $language = $arg{LANGUAGE}; |
| 97 |
$language =~ s/["']//g; |
| 98 |
$language =~ s/[.:_].*//; |
| 99 |
my $msg = ''; |
| 100 |
my $var = ''; |
| 101 |
for (qw(LANG LC_MESSAGES LC_ALL)) |
| 102 |
{ |
| 103 |
if (defined $arg{$_}) |
| 104 |
{ |
| 105 |
$var = $_; |
| 106 |
$msg = $arg{$_}; |
| 107 |
} |
| 108 |
} |
| 109 |
$msg =~ s/["']//g; |
| 110 |
if ($msg !~ m/^$language/ && $var ne '' |
| 111 |
&& $msg ne 'C' && $msg ne 'POSIX' |
| 112 |
&& $language ne 'C' && $language ne 'POSIX') |
| 113 |
{ |
| 114 |
print "*** $progname: Warning: LANGUAGE ($arg{LANGUAGE}) is not compatible with $var ($msg). Disabling it.\n"; |
| 115 |
$content =~ s/^(\s*LANGUAGE=)/#$1/mg; |
| 116 |
} |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
# Write locale file |
| 121 |
open(OUT, ">", $locale_file) |
| 122 |
or die "$progname: Unable to write $locale_file: $!\n"; |
| 123 |
print OUT $content; |
| 124 |
close(OUT) |
| 125 |
or die "$progname: Unable to close $locale_file: $!\n"; |
| 126 |
chmod($mode, $locale_file) |
| 127 |
or die "$progname: Unable to chmod $locale_file: $!\n"; |
| 128 |
|
| 129 |
1; |