[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Complex Editfiles Examples

From: Systems Administrator
Subject: Re: Complex Editfiles Examples
Date: Thu, 4 Dec 2003 15:24:07 +1100 (EST)

On Thu, 4 Dec 2003, Jamie Wilkinson wrote:

> This one time, at band camp, Russell Adams wrote:
> >Egad man!! I'm impressed. ;]
> >
> >I setup a similar construct last night to edit Gentoo's
> >/etc/make.conf, but its not nearly as complex.
> >
> >I'll post something when I've got the limits thing figured out.
> My best editfiles is the one for /etc/ssh/sshd_config, most of my other
> configuration file edits are based on that.
> I keep replicating the same construct over and over though, so I'm now
> contemplating writing some M4 macros to take care of the mundane
> parts... autoengine anyone?

        I've got something like that, except it uses Perl.  I've included
it below.  You'll need Text::Template for this.  Basically, have your
config file as normal, but with a .pt on the end.  When you run
"templateconv <filename>" (where filename can be with or without the .pt),
it will convert the .pt file into the other by going through and running
the Perl code in between <? and ?>

        If you want to add something to the output, use

        $OUT .= 'foo';

        If you want to output different files based on command line
options, use -D option=1, and then access the %defines hash.  The default
comment sign is "#", but for eg. /etc/named.conf you can use the -d switch
to set it to ';'.


#!/usr/bin/perl -I /usr/lib/perl5/site_perl/5.005

use     Text::Template;
use     Getopt::Long;

Getopt::Long::Configure('bundling', 'no_ignore_case');
        'd=s', 'D|define=s' => \%defines,
if(! $ARGH{'d'}) { $ARGH{'d'} = "#"; }

if($#ARGV < 0) { die "Error: List files to be processed on the command
line\n"; }

foreach $file (@ARGV) {
        if($file !~ /\.pt$/) { $file .= '.pt'; }
        if(! -e $file) { die "File $file does not exist\n"; }
        $targetfile = $file; $targetfile =~ s/\.pt$//;
        $template = new Text::Template(
                TYPE => FILE,
                SOURCE => $file,
                DELIMITERS => ['<?', '?>'],
        ) or die "Can't create template: $Text::Template::ERROR\n";

        $text = $template->fill_in(
                BROKEN => \&callback

        print "Output: $targetfile\n";
        open(FILE, ">$targetfile") or do { warn "Error: $!\n"; next};
        print FILE "$ARGH{'d'} Do not edit this file -- it is generated
with templateconv\n";
        print FILE $text;

exit $templateretval ? $templateretval : 0;

sub     callback {
        my(%hash) = @_;
        die "Error at $hash{'lineno'}: " . $hash{'error'} . "\n";

Tim Nelson
Systems Administrator
Sunet Internet
Tel: +61 3 5241 1155
Fax: +61 3 5241 6187
Email: address@hidden

reply via email to

[Prev in Thread] Current Thread [Next in Thread]