/usr/share/texlive/tlpkg/TeXLive/TLConfFile.pm is in texlive-base 2016.20170123-5.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 | # $Id: TLConfFile.pm 41104 2016-05-14 00:31:00Z preining $
# TeXLive::TLConfFile.pm - reading and writing conf files
# Copyright 2010-2014 Norbert Preining
# This file is licensed under the GNU General Public License version 2
# or any later version.
package TeXLive::TLConfFile;
use TeXLive::TLUtils;
my $svnrev = '$Revision: 41104 $';
my $_modulerevision;
if ($svnrev =~ m/: ([0-9]+) /) {
$_modulerevision = $1;
} else {
$_modulerevision = "unknown";
}
sub module_revision {
return $_modulerevision;
}
sub new
{
my $class = shift;
my ($fn, $cc, $sep, $typ) = @_;
my $self = {} ;
$self{'file'} = $fn;
$self{'cc'} = $cc;
$self{'sep'} = $sep;
if (defined($typ)) {
if ($typ eq 'last-win' || $typ eq 'first-win' || $typ eq 'multiple') {
$self{'type'} = $typ;
} else {
printf STDERR "Unknown type of conffile: $typ\n";
printf STDERR "Should be one of: last-win first-win multiple\n";
return;
}
} else {
# default type for backward compatibility is last-win
$self{'type'} = 'last-win';
}
bless $self, $class;
return $self->reparse;
}
sub reparse
{
my $self = shift;
my %config = parse_config_file($self->file, $self->cc, $self->sep);
my $lastkey = undef;
my $lastkeyline = undef;
$self{'keyvalue'} = ();
$self{'confdata'} = \%config;
$self{'changed'} = 0;
my $in_postcomment = 0;
for my $i (0..$config{'lines'}) {
if ($config{$i}{'type'} eq 'comment') {
$lastkey = undef;
$lastkeyline = undef;
$is_postcomment = 0;
} elsif ($config{$i}{'type'} eq 'data') {
$lastkey = $config{$i}{'key'};
$lastkeyline = $i;
$self{'keyvalue'}{$lastkey}{$i}{'value'} = $config{$i}{'value'};
$self{'keyvalue'}{$lastkey}{$i}{'status'} = 'unchanged';
if (defined($config{$i}{'postcomment'})) {
$in_postcomment = 1;
} else {
$in_postcomment = 0;
}
} elsif ($config{$i}{'type'} eq 'empty') {
$lastkey = undef;
$lastkeyline = undef;
$is_postcomment = 0;
} elsif ($config{$i}{'type'} eq 'continuation') {
if (defined($lastkey)) {
if (!$in_postcomment) {
$self{'keyvalue'}{$lastkey}{$lastkeyline}{'value'} .=
$config{$i}{'value'};
}
}
# otherwise we are in a continuation of a comment!!! so nothing to do
} else {
print "-- UNKNOWN TYPE\n";
}
}
return $self;
}
sub file
{
my $self = shift;
return($self{'file'});
}
sub cc
{
my $self = shift;
return($self{'cc'});
}
sub sep
{
my $self = shift;
return($self{'sep'});
}
sub type
{
my $self = shift;
return($self{'type'});
}
sub key_present
{
my ($self, $key) = @_;
return defined($self{'keyvalue'}{$key});
}
sub keys
{
my $self = shift;
return keys(%{$self{'keyvalue'}});
}
sub keyvaluehash
{
my $self = shift;
return \%{$self{'keyvalue'}};
}
sub confdatahash
{
my $self = shift;
return $self{'confdata'};
}
sub by_lnr
{
# order of lines
# first all the line numbers >= 0,
# then the negative line numbers in reverse order
# (negative line numbers refer to new entries in the conffile)
# example:
# line number in order: 0 3 6 7 9 -1 -2 -3
return ($a >= 0 && $b >= 0 ? $a <=> $b : $b <=> $a);
}
sub value
{
my ($self, $key, $value, @restvals) = @_;
my $t = $self->type;
if (defined($value)) {
if (defined($self{'keyvalue'}{$key})) {
my @key_lines = sort by_lnr CORE::keys %{$self{'keyvalue'}{$key}};
if ($t eq 'multiple') {
my @newval = ( $value, @restvals );
my $newlen = $#newval;
# in case of assigning to a multiple value stuff,
# we assign to the first n elements, delete superficial
# or add new ones if necessary
# $value should be a reference to an array of values
my $listp = $self{'keyvalue'}{$key};
my $oldlen = $#key_lines;
my $minlen = ($newlen < $oldlen ? $newlen : $oldlen);
for my $i (0..$minlen) {
if ($listp->{$key_lines[$i]}{'value'} ne $newval[$i]) {
$listp->{$key_lines[$i]}{'value'} = $newval[$i];
if ($listp->{$key_lines[$i]}{'status'} ne 'new') {
$listp->{$key_lines[$i]}{'status'} = 'changed';
}
$self{'changed'} = 1;
}
}
if ($minlen < $oldlen) {
# we are assigning less values to more lines, so we have to
# remove the remaining ones
for my $i (($minlen+1)..$oldlen) {
$listp->{$key_lines[$i]}{'status'} = 'deleted';
}
$self{'changed'} = 1;
}
if ($minlen < $newlen) {
# we have new values
my $ll = $key_lines[$#key_lines];
# if we are adding the first new entry, set line to -1,
# otherwise decrease the line number (already negative
# for new lines)
$ll = ($ll >= 0 ? -1 : $ll-1);
for my $i (($minlen+1)..$newlen) {
$listp->{$ll}{'status'} = 'new';
$listp->{$ll}{'value'} = $newval[$i];
$ll--;
}
$self{'changed'} = 1;
}
} else {
# select element based on first-win or last-win type
my $ll = $key_lines[($t eq 'first-win' ? 0 : $#key_lines)];
print "lastwin = $ll\n";
if ($self{'keyvalue'}{$key}{$ll}{'value'} ne $value) {
$self{'keyvalue'}{$key}{$ll}{'value'} = $value;
# as long as the key/value pair is not new,
# we set its status to changed
if ($self{'keyvalue'}{$key}{$ll}{'status'} ne 'new') {
$self{'keyvalue'}{$key}{$ll}{'status'} = 'changed';
}
$self{'changed'} = 1;
}
}
} else { # all new key
my @newval = ( $value, @restvals );
my $newlen = $#newval;
for my $i (0..$newlen) {
$self{'keyvalue'}{$key}{-($i+1)}{'value'} = $value;
$self{'keyvalue'}{$key}{-($i+1)}{'status'} = 'new';
}
$self{'changed'} = 1;
}
}
if (defined($self{'keyvalue'}{$key})) {
my @key_lines = sort by_lnr CORE::keys %{$self{'keyvalue'}{$key}};
if ($t eq 'first-win') {
return $self{'keyvalue'}{$key}{$key_lines[0]}{'value'};
} elsif ($t eq 'last-win') {
return $self{'keyvalue'}{$key}{$key_lines[$#key_lines]}{'value'};
} elsif ($t eq 'multiple') {
return map { $self{'keyvalue'}{$key}{$_}{'value'} } @key_lines;
} else {
die "That should not happen: wrong type: $!";
}
}
return;
}
sub delete_key
{
my ($self, $key) = @_;
%config = %{$self{'confdata'}};
if (defined($self{'keyvalue'}{$key})) {
for my $l (CORE::keys %{$self{'keyvalue'}{$key}}) {
$self{'keyvalue'}{$key}{$l}{'status'} = 'deleted';
}
$self{'changed'} = 1;
}
}
sub rename_key
{
my ($self, $oldkey, $newkey) = @_;
%config = %{$self{'confdata'}};
for my $i (0..$config{'lines'}) {
if (($config{$i}{'type'} eq 'data') &&
($config{$i}{'key'} eq $oldkey)) {
$config{$i}{'key'} = $newkey;
$self{'changed'} = 1;
}
}
if (defined($self{'keyvalue'}{$oldkey})) {
$self{'keyvalue'}{$newkey} = $self{'keyvalue'}{$oldkey};
delete $self{'keyvalue'}{$oldkey};
$self{'keyvalue'}{$newkey}{'status'} = 'changed';
$self{'changed'} = 1;
}
}
sub is_changed
{
my $self = shift;
return $self{'changed'};
}
sub save
{
my $self = shift;
my $outarg = shift;
my $closeit = 0;
# unless $outarg is defined or we are changed, return immediately
return if (! ( defined($outarg) || $self->is_changed));
#
%config = %{$self{'confdata'}};
#
# determine where to write to
my $out = $outarg;
my $fhout;
if (!defined($out)) {
$out = $config{'file'};
my $dn = TeXLive::TLUtils::dirname($out);
TeXLive::TLUtils::mkdirhier($dn);
if (!open(CFG, ">$out")) {
tlwarn("Cannot write to $out: $!\n");
return 0;
}
$closeit = 1;
$fhout = \*CFG;
} else {
# check what we got there for $out
if (ref($out) eq 'SCALAR') {
# that is a file name
my $dn = TeXLive::TLUtils::dirname($out);
TeXLive::TLUtils::mkdirhier($dn);
if (!open(CFG, ">$out")) {
tlwarn("Cannot write to $out: $!\n");
return 0;
}
$fhout = \*CFG;
$closeit = 1;
} elsif (ref($out) eq 'GLOB') {
# that hopefully is a fh
$fhout = $out;
} else {
tlwarn("Unknown out argument $out\n");
return 0;
}
}
#
# first we write the config file as close as possible to orginal layout,
# and after that we add new key/value pairs
for my $i (0..$config{'lines'}) {
my $is_changed = 0;
if ($config{$i}{'type'} eq 'comment') {
print $fhout "$config{$i}{'value'}";
print $fhout ($config{$i}{'multiline'} ? "\\\n" : "\n");
} elsif ($config{$i}{'type'} eq 'empty') {
print $fhout ($config{$i}{'multiline'} ? "\\\n" : "\n");
} elsif ($config{$i}{'type'} eq 'data') {
# we have to check whether the original data has been changed!!
if ($self{'keyvalue'}{$config{$i}{'key'}}{$i}{'status'} eq 'changed') {
$is_changed = 1;
print $fhout "$config{$i}{'key'} $config{'sep'} $self{'keyvalue'}{$config{$i}{'key'}}{$i}{'value'}";
if (defined($config{$i}{'postcomment'})) {
print $fhout $config{$i}{'postcomment'};
}
print $fhout ($config{$i}{'multiline'} ? "\\\n" : "\n");
} elsif ($self{'keyvalue'}{$config{$i}{'key'}}{$i}{'status'} eq 'deleted') {
$is_changed = 1;
} else {
print $fhout "$config{$i}{'original'}";
print $fhout ($config{$i}{'multiline'} ? "\\\n" : "\n");
}
} elsif ($config{$i}{'type'} eq 'continuation') {
if ($is_changed) {
# ignore continuation lines
} else {
print $fhout "$config{$i}{'value'}";
print $fhout ($config{$i}{'multiline'} ? "\\\n" : "\n");
}
}
}
#
# save new keys
for my $k (CORE::keys %{$self{'keyvalue'}}) {
for my $l (CORE::keys %{$self{'keyvalue'}{$k}}) {
if ($self{'keyvalue'}{$k}{$l}{'status'} eq 'new') {
print $fhout "$k $config{'sep'} $self{'keyvalue'}{$k}{$l}{'value'}\n";
}
}
}
close $fhout if $closeit;
#
# reparse myself
if (!defined($outarg)) {
$self->reparse;
}
}
#
# parse/write config file
# these functions allow reading and writing of config files
# that consists of comments (comment char/string is the second argument)
# and pairs
# \s* key \s* SEP \s* value \s*
# where SEP is the third argument,
# and key does not contain neither white space nor SEP
# and value can be arbitry
#
# continuation lines are allowed
# Furthermore, at least the separator has to be on the same line as the key!!
# Continuations followed by comment lines are invalid!
#
sub parse_config_file {
my ($file, $cc, $sep) = @_;
my @data;
if (!open(CFG, "<$file")) {
@data = ();
} else {
@data = <CFG>;
chomp(@data);
close(CFG);
}
my %config = ();
$config{'file'} = $file;
$config{'cc'} = $cc;
$config{'sep'} = $sep;
my $lines = $#data;
my $cont_running = 0;
for my $l (0..$lines) {
$config{$l}{'original'} = $data[$l];
if ($cont_running) {
if ($data[$l] =~ m/^(.*)\\$/) {
$config{$l}{'type'} = 'continuation';
$config{$l}{'multiline'} = 1;
$config{$l}{'value'} = $1;
next;
} else {
# last line of a continuation
# do nothing, we will finish here
$config{$l}{'type'} = 'continuation';
$config{$l}{'value'} = $data[$l];
$cont_running = 0;
next;
}
}
# ignore continuation after comments, that is the behaviour the
# kpathsea library is using, so we follow it here
if ($data[$l] =~ m/$cc/) {
$data[$l] =~ s/\\$//;
}
# continuation line
if ($data[$l] =~ m/^(.*)\\$/) {
$cont_running = 1;
$config{$l}{'multiline'} = 1;
# remove the continuation marker so that we can do everything
# as normal below
$data[$l] =~ s/\\$//;
# we will continue below
}
# from now on, if $cont_running == 1, then it means that
# we are in the FIRST line of a multi line setting, so evaluate
# it accordingly to get the key if necessary
# empty lines are treated as comments
if ($data[$l] =~ m/^\s*$/) {
$config{$l}{'type'} = 'empty';
next;
}
if ($data[$l] =~ m/^\s*$cc/) {
# save the full line as is into the config hash
$config{$l}{'type'} = 'comment';
$config{$l}{'value'} = $data[$l];
next;
}
# mind that the .*? is making the .* NOT greedy, ie matching as few as
# possible. That way we can get rid of the comments at the end of lines
if ($data[$l] =~ m/^\s*([^\s$sep]+)\s*$sep\s*(.*?)(\s*)?($cc.*)?$/) {
$config{$l}{'type'} = 'data';
$config{$l}{'key'} = $1;
$config{$l}{'value'} = $2;
if (defined($3)) {
my $postcomment = $3;
if (defined($4)) {
$postcomment .= $4;
}
# check that there is actually a comment in the second part of the
# line. Otherwise we might add the continuation lines of that
# line to the value
if ($postcomment =~ m/$cc/) {
$config{$l}{'postcomment'} = $postcomment;
}
}
next;
}
# if we are still here, that means we cannot evaluate the config file
# give a BIG FAT WARNING but save the line as comment and continue
# anyway
warn("WARNING WARNING WARNING\n");
warn("Cannot parse config file $file ($cc, $sep)\n");
warn("The following line (l.$l) seems to be wrong:\n");
warn(">>> $data[$l]\n");
warn("We will treat this line as a comment!\n");
$config{$l}{'type'} = 'comment';
$config{$l}{'value'} = $data[$l];
}
# save the number of lines in the config hash
$config{'lines'} = $lines;
return %config;
}
sub dump_config_data {
my $foo = shift;
my %config = %{$foo};
print "config file name: $config{'file'}\n";
print "config comment char: $config{'cc'}\n";
print "config separator: $config{'sep'}\n";
print "config lines: $config{'lines'}\n";
for my $i (0..$config{'lines'}) {
print "line ", $i+1, ": $config{$i}{'type'}";
if ($config{$i}{'type'} eq 'comment') {
print "\nCOMMNENT = $config{$i}{'value'}\n";
} elsif ($config{$i}{'type'} eq 'data') {
print "\nKEY = $config{$i}{'key'}\nVALUE = $config{$i}{'value'}\n";
} elsif ($config{$i}{'type'} eq 'empty') {
print "\n";
# do nothing
} elsif ($config{$i}{'type'} eq 'continuation') {
print "\nVALUE = $config{$i}{'value'}\n";
} else {
print "-- UNKNOWN TYPE\n";
}
}
}
sub write_config_file {
my $foo = shift;
my %config = %{$foo};
for my $i (0..$config{'lines'}) {
if ($config{$i}{'type'} eq 'comment') {
print "$config{$i}{'value'}";
print ($config{$i}{'multiline'} ? "\\\n" : "\n");
} elsif ($config{$i}{'type'} eq 'data') {
print "$config{$i}{'key'} $config{'sep'} $config{$i}{'value'}";
if ($config{$i}{'multiline'}) {
print "\\";
}
print "\n";
} elsif ($config{$i}{'type'} eq 'empty') {
print ($config{$i}{'multiline'} ? "\\\n" : "\n");
} elsif ($config{$i}{'type'} eq 'continuation') {
print "$config{$i}{'value'}";
print ($config{$i}{'multiline'} ? "\\\n" : "\n");
} else {
print STDERR "-- UNKNOWN TYPE\n";
}
}
}
1;
__END__
=head1 NAME
C<TeXLive::TLConfFile> -- TeX Live Config File Access Module
=head1 SYNOPSIS
use TeXLive::TLConfFile;
$conffile = TeXLive::TLConfFile->new($file_name, $comment_char, $separator, $type);
$conffile->file;
$conffile->cc;
$conffile->sep;
$conffile->type
$conffile->key_present($key);
$conffile->keys;
$conffile->value($key [, $value, ...]);
$conffile->is_changed;
$conffile->save;
$conffile->reparse;
=head1 DESCRIPTION
This module allows parsing, changing, saving of configuration files
of a general style. It also supports three different paradigma
with respect to multiple occurrences of keys: C<first-win> specifies
a configuration file where the first occurrence of a key specifies
the value, C<last-win> specifies that the last wins, and
C<multiple> that all keys are kept.
The configuration files (henceforth conffiles) can contain comments
initiated by the $comment_char defined at instantiation time.
Everything after a $comment_char, as well as empty lines, will be ignored.
The rest should consists of key/value pairs separated by the separator,
defined as well at instantiation time.
Whitespace around the separator, and before and after key and value
are allowed.
Comments can be on the same line as key/value pairs and are also preserved
over changes.
Continuation lines (i.e., lines with last character being a backslash)
are allowed after key/value pairs, but the key and
the separator has to be on the same line.
Continuations are not possible in comments, so a terminal backslash in
a comment will be ignored, and in fact not written out on save.
=head2 Methods
=over 4
=item B<< $conffile = TeXLive::TLConfFile->new($file_name, $comment_char, $separator [, $type]) >>
instantiates a new TLConfFile and returns the object. The file specified
by C<$file_name> does not have to exist, it will be created at save time.
The C<$comment_char> can actually be any regular expression, but
embedding grouping is a bad idea as it will break parsing.
The C<$separator> can also be any regular expression.
The C<$type>, if present, has to be one of C<last-win> (the default),
C<first-win>, or C<multiple>.
=item B<< $conffile->file >>
Returns the location of the configuration file. Not changeable (at the moment).
=item B<< $conffile->cc >>
Returns the comment character.
=item B<< $conffile->sep >>
Returns the separator.
=item B<< $conffile->type >>
Returns the type.
=item B<< $conffile->key_present($key) >>
Returns true (1) if the given key is present in the config file, otherwise
returns false (0).
=item B<< $conffile->keys >>
Returns the list of keys currently set in the config file.
=item B<< $conffile->value($key [, $value, ...]) >>
With one argument, returns the current setting of C<$key>, or undefined
if the key is not set. If the configuration file is of C<multiple>
type a list of keys ordered by occurrence in the file is returned.
With two (or more) arguments changes (or adds) the key/value pair to
the config file and returns the I<new> value.
In case of C<first-win> or C<last-win>, the respective occurrence
of the key is changed, and the others left intact. In this case only
the first C<$value> is used.
In case of C<multiple> the C<$values> are assigned to the keys in the
order of occurrence in the file. If extra values are present, they
are added. If on the contrary less values then already existing
keys are passed, the remaining keys are deleted.
=item B<< $conffile->rename_key($oldkey, $newkey) >>
Renames a key from C<$oldkey> to C<$newkey>. It does not automatically
save the new config file.
=item B<< $conffile->is_changed >>
Returns true (1) if some real change has happened in the configuration file,
that is a value has been changed to something different, or a new
setting has been added.
Note that changing a setting back to the original one will not reset
the changed flag.
=item B<< $conffile->save >>
Saves the config file, preserving as much structure and comments of
the original file as possible.
=item B<< $conffile->reparse >>
Reparses the configuration file.
=back
=head1 EXAMPLES
For parsing a C<texmf.cnf> file you can use
$tmfcnf = TeXLive::TLConfFile->new(".../texmf-dist/web2c", "[#%]", "=");
since the allowed comment characters for texmf.cnf files are # and %.
After that you can query keys:
$tmfcnf->value("TEXMFMAIN");
$tmfcnf->value("trie_size", 900000);
=head1 AUTHORS AND COPYRIGHT
This script and its documentation were written for the TeX Live
distribution (L<http://tug.org/texlive>) and both are licensed under the
GNU General Public License Version 2 or later.
=cut
### Local Variables:
### perl-indent-level: 2
### tab-width: 2
### indent-tabs-mode: nil
### End:
# vim:set tabstop=2 expandtab: #
|