xautoconfig()

Mark Hedges hedges at scruzbox.com
Mon Nov 11 21:31:47 CET 2002


#!/usr/bin/perl
#
# (c) Copyright 2002 Scruzbox (Mark Hedges, <hedges at scruzbox.com>).
# Freely use, modify and redistribute
# under terms of GNU General Public License.
#
# Here are some perl routines I wrote that seem to bootstrap
# an appropriate XF86Config-4 file in a FAI finish hook.  You may
# wish to add more video modes.  This is a dirty hack but it
# works okay.  It cuts together the most useful sections
# generated by debconf in the initial installation and by
# `XFree86 -configure`, which usually gets the driver right.
# This generates defaults for PS/2 and ImPS/2 mice, not serial!
# --mark--


$target = "/tmp/target";
$chroot = "chroot $target ";

sub xautoconfig {

   my $xconfigfile = "$target/etc/X11/XF86Config-4";
   my $tempfile    = "/tmp/XF86Config-4";

   my $genfile = "$target/XF86Config.new";  # file generated in CWD, I guess

   # read each section of XF86Config-4 (debconf)
   my $debconf = $xconfigfile;

   # and of the generated file
   system("$chroot /usr/X11R6/bin/XFree86 -configure");

   my %conf;

   readxfconfig(\%conf, $debconf);
   readxfconfig(\%conf, $genfile);

   open OUTFILE, ">$tempfile" || die("WTF?\n");
   print OUTFILE "# Configured by Scruzbox\n";
   print OUTFILE "# You may wish to add larger-screen video modes to the 'Screen' section\n";
   print OUTFILE "#\n#\n#" . `date`;
   print OUTFILE "#\n#\n#\n";

   foreach $file (sort keys %conf) {

      foreach $section (@{$conf{$file}}) {

         my @lines = reverse @{$section};

         my @rev = reverse @lines;

         # $section contains ref to array of lines

         if ($file eq $debconf) {
            # print all except "Monitor" and "Device" sections:
            if ( ($lines[0] eq "Section \"Monitor\"")
              || ($lines[0] eq "Section \"Device\"")   ) {
               next;
            } else {
               my $line;
               while ($line = pop @rev) {
                  # rename things to use other version:
                  $line =~ s/Generic Monitor/Monitor0/;
                  $line =~ s/Generic Video Card/Card0/;

                  # add more video modes here:
                  $line =~ s/\"800x600\" \"640x480\"/\"1024x768\" \"800x600\" \"640x480\"/;

                  print OUTFILE $line . "\n";
                  print OUTFILE "\n" if ($line =~ /EndSection/);
               }
            }
         } elsif ($file eq $genfile) {
            # get "Monitor" and "Device" sections from `XFree86 -configure`:
            if ( ($lines[0] eq "Section \"Monitor\"")
              || ($lines[0] eq "Section \"Device\"")    ) {
               while ($line = pop @rev) {
                  print OUTFILE $line . "\n";
                  print OUTFILE "\n" if ($line =~ /EndSection/);
               }
            }
         }
      }
   }
   close OUTFILE;

   my $bak = $xconfigfile . ".bak";
   system("mv $xconfigfile $bak");
   system("mv $tempfile $xconfigfile");
}


sub readxfconfig {
   my $confref = shift;
   my $file = shift;

   open(XF,"<$file") || die("readxfconfig(): no config file $file\n");

   # an array of lines for each section
   my $newsec = 0;
   my $secname = "";
   my @sec = ();
   my $i = 0;
   while(<XF>) {
      chomp;
      $line = $_;
      next if ((/^#/) || (/^\s+$/));
      if ($line =~ /^EndSection/) {
         push @sec, $line;
         $newsec = 0;
         reverse @sec;
         while (@sec) {
            push @{$confref->{$file}->[$i]}, pop(@sec);
         }
         $i++;
      }
      if ($newsec) {
         push @sec, $line;
      }
      if ($line =~ /^Section "(.*)"$/) {
         $newsec++;
         $secname = $1;
         @sec = ();
         push @sec, $line;
      }
   }
   close(XF);
}




More information about the linux-fai mailing list