| 1 |
#! /usr/bin/perl |
| 2 |
|
| 3 |
#********************************************************************* |
| 4 |
# |
| 5 |
# ainsl -- AppendIfNoSuchLine written in Perl |
| 6 |
# |
| 7 |
# This script is part of FAI (Fully Automatic Installation) |
| 8 |
# Copyright (C) 2006-2012 Thomas Lange, [email protected] |
| 9 |
# Universitaet zu Koeln |
| 10 |
# |
| 11 |
#********************************************************************* |
| 12 |
# This program is free software; you can redistribute it and/or modify |
| 13 |
# it under the terms of the GNU General Public License as published by |
| 14 |
# the Free Software Foundation; either version 2 of the License, or |
| 15 |
# (at your option) any later version. |
| 16 |
# |
| 17 |
# This program is distributed in the hope that it will be useful, but |
| 18 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 20 |
# General Public License for more details. |
| 21 |
# |
| 22 |
# A copy of the GNU General Public License is available as |
| 23 |
# '/usr/share/common-licences/GPL' in the Debian GNU/Linux distribution |
| 24 |
# or on the World Wide Web at http://www.gnu.org/copyleft/gpl.html. You |
| 25 |
# can also obtain it by writing to the Free Software Foundation, Inc., |
| 26 |
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
| 27 |
#********************************************************************* |
| 28 |
|
| 29 |
use strict; |
| 30 |
|
| 31 |
use Getopt::Std; |
| 32 |
our ($opt_a,$opt_h,$opt_D,$opt_n,$opt_s,$opt_v,$opt_q,$opt_Q,$opt_N); |
| 33 |
|
| 34 |
my $filename; |
| 35 |
my $verbose; |
| 36 |
my $debug; |
| 37 |
|
| 38 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 39 |
sub usage { |
| 40 |
|
| 41 |
my $exitcode = shift; |
| 42 |
|
| 43 |
print << "EOF"; |
| 44 |
ainsl, AppendIfNoSuchLine written in Perl. |
| 45 |
|
| 46 |
Copyright (C) 2006-2012 by Thomas Lange |
| 47 |
|
| 48 |
Usage: ainsl [OPTION] FILE LINE [PATTERN] |
| 49 |
|
| 50 |
-a Autocreate file if not existing. |
| 51 |
-D Create debug output. |
| 52 |
-h Show summary of options. |
| 53 |
-n Print the actions, but do not execute them. |
| 54 |
-Q Quote all metacharacters in pattern. Uses perl\'s \\Q function. |
| 55 |
-q Quote * and + metacharacters in pattern. |
| 56 |
-s Convert blanks in line to '\\s+' regexp. |
| 57 |
-N Don't prepend '\$AINSL_TARGET/' to filename, even if it is set. |
| 58 |
-v Create verbose output. |
| 59 |
|
| 60 |
EOF |
| 61 |
exit $exitcode; |
| 62 |
} |
| 63 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 64 |
sub autocreate { |
| 65 |
|
| 66 |
-f $filename and return; |
| 67 |
|
| 68 |
# create missing parts of a path. Could be replaced by File::Path |
| 69 |
my $path; |
| 70 |
while ($filename =~ m#([^/]+)/#g) { |
| 71 |
$path.="/$1"; |
| 72 |
-d $path and next; |
| 73 |
print "ainsl: create $path\n" if $debug; |
| 74 |
$opt_n and next; |
| 75 |
mkdir $path or die "ainsl: can't create $path $!"; |
| 76 |
} |
| 77 |
|
| 78 |
print "ainsl: create $filename\n" if $verbose; |
| 79 |
$opt_n and return; |
| 80 |
open (FILE,">$filename") or die "ainsl: can't create $filename $!"; |
| 81 |
close (FILE); |
| 82 |
} |
| 83 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 84 |
getopts('aDhsvnQqN') or usage(3); |
| 85 |
|
| 86 |
$opt_h and usage(0); |
| 87 |
$verbose = $opt_v || $ENV{verbose} || 0; |
| 88 |
$debug = $opt_D || $ENV{debug} || 0; |
| 89 |
|
| 90 |
$filename = shift; |
| 91 |
my $line = shift; |
| 92 |
my $optpattern = shift; |
| 93 |
my $found = 0; |
| 94 |
usage(3) unless defined $line; |
| 95 |
|
| 96 |
if (! $opt_N) { |
| 97 |
$ENV{AINSL_TARGET} and $filename = "$ENV{AINSL_TARGET}/$filename"; |
| 98 |
} |
| 99 |
$filename =~ s#//+#/#g; # multiple slashes -> one slash |
| 100 |
|
| 101 |
print "FILE: $filename\nLINE: $line\nPATTERN: ", $optpattern || "[NO PATTERN SPECIFIED]", "\n" if $debug; |
| 102 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 103 |
$opt_a and autocreate; |
| 104 |
|
| 105 |
my $pattern = (defined $optpattern) ? $optpattern : $line; |
| 106 |
# process pattern and line |
| 107 |
# remove ^ and $ in line (only at start and end), but still use it for pattern |
| 108 |
# in no explicit pattern was given |
| 109 |
|
| 110 |
unless (defined $optpattern) { |
| 111 |
# remove special chars ^ and $ from line |
| 112 |
$line =~ s/^\^//; |
| 113 |
$line =~ s/\$$//; |
| 114 |
# escape '(', ')' and '+' if no pattern was given and line is used |
| 115 |
$pattern =~ s/\(/\\(/g; |
| 116 |
$pattern =~ s/\)/\\)/g; |
| 117 |
$pattern =~ s/\+/\\+/g; |
| 118 |
} |
| 119 |
$opt_s and $pattern =~ s/\s+/\\s+/g; |
| 120 |
$opt_q and $pattern =~ s/\*/\\*/g; |
| 121 |
$opt_q and $pattern =~ s/\+/\+/g; |
| 122 |
$pattern="\Q$pattern\E" if $opt_Q; |
| 123 |
|
| 124 |
print "ainsl: newpattern: $pattern\n" if $debug; |
| 125 |
print "ainsl: newline: $line\n" if $debug; |
| 126 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 127 |
# check if pattern already included in file |
| 128 |
-f $filename or die "ainsl: target file $filename does not exist.\n"; |
| 129 |
open (INFILE, "<$filename") or die "ainsl: Can't open $filename $!\n"; |
| 130 |
my $lastline = ""; |
| 131 |
while ($lastline = <INFILE>) { |
| 132 |
if ($lastline =~ /$pattern/o) { |
| 133 |
print "ainsl: Pattern found. Nothing to append.\n" if $debug; |
| 134 |
$found=1; |
| 135 |
last; |
| 136 |
} |
| 137 |
} |
| 138 |
close(INFILE); |
| 139 |
exit 0 if $found; # nothing to append |
| 140 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 141 |
# Append line to file |
| 142 |
print "ainsl: appending to $filename: $line\n" if $verbose; |
| 143 |
exit 0 if $opt_n; |
| 144 |
open (INFILE, ">>$filename") or die "ainsl: can't open $filename for writing. $!"; |
| 145 |
if ($lastline && substr($lastline, -1) ne "\n") { |
| 146 |
print INFILE "\n" or die "ainsl: cannot print to file $filename: $!"; |
| 147 |
} |
| 148 |
print INFILE $line, "\n" or die "ainsl: cannot print to file $filename: $!"; |
| 149 |
close(INFILE) or die "ainsl: error saving file $filename: $!"; |