examples/conf/confconfer.pl untested

filter: Statementstmt 0.0% 0 / 41 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 0.0% 0 / 3 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 68 CC 3 SCAR 20.8
CC
3
Cov
0%
fillOut
12.0
BEGIN
2.0
BEGIN
2.0
CRAP 8.0
1 #!/usr/bin/perl -wT
2  
3 # $Id: confconfer.pl,v 1.3 2007/09/28 06:28:20 pauldoom Exp $
4  
5 ##
6 # Copyright (c) 2007 Paul M. Hirsch (paul@voltagenoir.org).
7 # All rights reserved.
8 #
9 # This program is free software; you can redistribute it and/or modify it under
10 # the same terms as Perl itself.
11 ##
12 use strict;
13  
14 use vars qw($VERSION);
15 $VERSION = substr(q$Revision: 1.3 $, 10, -1);
16  
17 my $usage =<<"EOU";
18 >>> confconfer.pl - Fill out a sample config template to make a real config <<<
19 Usage: confconfer.pl INPUTFILE > OUTPUTFILE
20  
21 1) Takes single argument which must be a plain text file containing one or
22 more replacement variables as input.
23 2) Prompts for the value of each unique replacement variable.
24 3) Spits out the input with the replacement variables replaced.
25  
26 Replacement variables are specified with the syntax: __VARIABLENAME__
27 For example, if this script encountered the line:
28  
29 <Directory "__DOCUMENTROOT__/junk">
30  
31 and the user entered:
32  
33 /var/toad/master
34  
35 confconfer would spit out the line:
36  
37 <Directory "/var/toad/master/junk">
38
39 This usage text is longer than the code itself.
40 EOU
41  
42 my %v = ();
43 my $i = '';
44 my $o = '';
45  
46 ($ARGV[0]) or die "FATAL: You must specify an input template file\n$usage\n";
47 (-f $ARGV[0]) or die "FATAL: Missing or bad template file \"$ARGV[0]\": $!\n";
48 print STDERR "Opening \"$ARGV[0]\"...\n";
49 open(TF, "$ARGV[0]") or die "FATAL: Unable to open template file \"$ARGV[0]\": $!\n";
50 # Read in input and stow it
51 print STDERR "Reading in template...\n";
52 while ($_ = <TF>) {
53 $i .= $_;
54 }
55 close(TF);
56  
57 # Fill in the vars
58 print STDERR "Gathering replacement values from user (that means you):\n";
59 while ($i =~ s/^(.*?)__([^\s]+)__//s) {
60 ($1) && ($o .= $1);
61 $o .= fillOut($2);
62 }
63 $o .= $i; # Add remainder
64  
65 # Out goes the output
66 print STDERR "Sending output to STDOUT...";
67 print STDOUT $o;
68 print STDERR "Done.\n";
69 exit 0;
70  
71 # Check for template var, prompting for input if needed. Returns template
72 # var value
73 sub fillOut {
74 my $vn = shift;
75 unless (exists($v{$vn})) {
76 # Get velue, and don't accept empty...
77 my $val = '';
78 while ($val =~ /^\s*$/) {
79 print STDERR "Enter the value for \"$vn\" : ";
80 $| = 1; # Flush to STDOUT
81 my $in = <STDIN>;
82 if (!defined($in) || $in =~ /^\s*$/) {
83 print STDERR "Empty? Please try again\n";
84 } else {
85 $in =~ /^\s*(.+?)\s*$/; # Pull in value, sans surrounding white
86 $val = $1;
87 }
88 }
89 $v{$vn} = $val;
90 }
91  
92 return $v{$vn};
93 }
94
95