Files
mars-smart/readconfig.pl
2026-04-21 12:00:30 +02:00

267 lines
5.5 KiB
Perl

# SMArT
#
# Configuration file code
#
# Copyright 2001 Wilmer van der Gaast (lintux@lintux.cx)
#
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
#
my( @info, @conf, @rawconf, $l );
$info[1] = 'Volume';
$info[2] = 'Server name';
$info[3] = 'Internal network';
$info[4] = 'IPX device';
$info[5] = 'Device flags';
$info[6] = 'Version spoofing';
$info[7] = 'Password handling';
$info[8] = 'Login/security flags';
$info[9] = 'creat() modes';
$info[10] = 'Guest group';
$info[11] = 'Guest user';
$info[12] = 'Supervisor login';
$info[13] = 'User login';
$info[15] = 'Automatic login mapping';
$info[16] = 'Startup tests';
$info[17] = 'Bindery/user flags';
$info[18] = 'Queue flags';
$info[21] = 'Print queue';
$info[22] = 'Print server';
$info[30] = 'Burst mode';
$info[40] = 'Volume cache';
$info[41] = 'Share/lock files';
$info[42] = 'Spool dir';
$info[45] = 'Bindery files';
$info[46] = 'Attribute files';
$info[47] = 'Trustee files';
$info[50] = 'Conversion tables';
$info[60] = 'MAX_CONNECTIONS';
$info[61] = 'MAX_NW_VOLS';
$info[63] = 'MAX_DIR_BASE_ENTRIES';
$info[68] = 'USE_MMAP';
$info[69] = 'HANDLE_ALL_SAP_TYPS';
$info[70] = 'NETWORK_SERIAL_NMBR';
$info[71] = 'NETWORK_APPL_NMBR';
$info[80] = 'max_dir_search_handles';
$info[100] = 'Debug IPX kernel';
$info[101] = 'Debug NWSERV';
$info[102] = 'Debug NCPSERV';
$info[103] = 'Debug NWCONN';
$info[104] = 'Debug NWCLIENT';
$info[105] = 'Debug NWBIND';
$info[106] = 'Debug NWROUTED';
$info[200] = 'Daemon mode';
$info[201] = 'Log file';
$info[202] = 'Log mode';
$info[210] = 'Shutdown timing';
$info[211] = 'Shutdown warnings';
$info[300] = 'Routing info';
$info[301] = 'Routing log';
$info[302] = 'Routing log flags';
$info[310] = 'Watchdogs';
$info[400] = 'nwserv.stations file';
$info[401] = 'Reply to nearest server requests';
$info[402] = 'Reply to connect requests';
open( CONF, '<' . $mars_config ) or die "Could not open $mars_config: $!";
@conf = ();
@rawconf = ();
while( $l = <CONF> )
{
push( @rawconf, $l );
my $x = $l;
$x =~ s/[\r\n]//g;
$x =~ s/[\t ]+/ /g;
$x =~ s/#.*//;
$x =~ s/^[\t ]*//;
$x =~ s/[\t ]*$//;
if( $x ne '' )
{
push( @conf, $x );
}
}
close( CONF );
sortconfig();
sub sortconfig()
{
@conf = sort( { $a cmp $b } @conf );
@conf = sort( { ( split( ' ', $a ) )[0] <=> ( split( ' ', $b ) )[0] } @conf );
}
sub getconfigline( $ )
{
my( @c, $c );
@c = getconfig( @_ );
$c = $c[0];
$c =~ s/^[0-9]* //;
return( $c );
}
sub getconfig( $ )
{
my( @c );
@c = grep( /^$_[0] /i, @conf );
return( @c );
}
sub addconfigline( $ )
{
unshift( @conf, $_[0] );
}
sub delconfigline( $ )
{
@conf = grep( !/^$_[0] /i, grep( !/^$_[0]$/i, @conf ) );
}
sub normalize_line( $ )
{
my $x = $_[0];
$x =~ s/[\r\n]//g;
$x =~ s/[\t ]+/ /g;
$x =~ s/#.*//;
$x =~ s/^[\t ]*//;
$x =~ s/[\t ]*$//;
return( $x );
}
sub section_of_line( $ )
{
my $x = normalize_line( $_[0] );
if( $x =~ /^([0-9]+)\b/ )
{
return( $1 );
}
return( '' );
}
sub writeconfig_compact()
{
my( $i, $l );
sortconfig();
open( CONF, '>' . $mars_config ) or die "Could not write $mars_config: $!";
$l = ( getconfig( '1 SYS' ) )[0];
delconfigline( '1 SYS' );
addconfigline( $l );
foreach $i ( @conf )
{
$l = $i;
$l =~ s/ .*//;
printf( CONF "%-50s # %s\n", $i, $info[$l] );
}
close( CONF );
}
sub writeconfig_preserve_layout()
{
my( %secmap, %written, @sections, $sec, $line, $sysline );
sortconfig();
# Keep SYS as the first entry in section 1.
$sysline = ( grep( /^1 SYS /, @conf ) )[0];
if( defined( $sysline ) )
{
@conf = grep( $_ ne $sysline, @conf );
unshift( @conf, $sysline );
}
foreach $line ( @conf )
{
$sec = section_of_line( $line );
if( $sec ne '' )
{
push( @{ $secmap{$sec} }, $line );
}
}
open( CONF, '>' . $mars_config ) or die "Could not write $mars_config: $!";
foreach $line ( @rawconf )
{
$sec = section_of_line( $line );
# Preserve comments and blank lines exactly as they were.
if( $sec eq '' )
{
print CONF $line;
next;
}
# Replace each section only once, at its first occurrence.
if( ! $written{$sec} )
{
if( defined( $secmap{$sec} ) )
{
foreach my $entry ( @{ $secmap{$sec} } )
{
print CONF $entry . "\n";
}
}
$written{$sec} = 1;
}
# Skip the original config line because the updated section
# has already been emitted.
}
# Append sections that did not exist in the original file.
@sections = sort { $a <=> $b } keys( %secmap );
foreach $sec ( @sections )
{
next if $written{$sec};
print CONF "\n";
foreach my $entry ( @{ $secmap{$sec} } )
{
print CONF $entry . "\n";
}
$written{$sec} = 1;
}
close( CONF );
}
sub writeconfig()
{
if( defined( $smart_compact_nwservconf ) && $smart_compact_nwservconf )
{
writeconfig_compact();
}
else
{
writeconfig_preserve_layout();
}
}