| 1 |
lange |
3997 |
#! /usr/bin/perl |
| 2 |
|
|
|
| 3 |
|
|
# $Id$ |
| 4 |
|
|
#********************************************************************* |
| 5 |
|
|
# |
| 6 |
|
|
# ainsl -- AppendIfNoSuchLine written in Perl |
| 7 |
|
|
# |
| 8 |
|
|
# This script is part of FAI (Fully Automatic Installation) |
| 9 |
lange |
4496 |
# Copyright (C) 2006-2007 Thomas Lange, lange@informatik.uni-koeln.de |
| 10 |
lange |
3997 |
# Universitaet zu Koeln |
| 11 |
|
|
# |
| 12 |
|
|
#********************************************************************* |
| 13 |
|
|
# This program is free software; you can redistribute it and/or modify |
| 14 |
|
|
# it under the terms of the GNU General Public License as published by |
| 15 |
|
|
# the Free Software Foundation; either version 2 of the License, or |
| 16 |
|
|
# (at your option) any later version. |
| 17 |
|
|
# |
| 18 |
|
|
# This program is distributed in the hope that it will be useful, but |
| 19 |
|
|
# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 |
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 21 |
|
|
# General Public License for more details. |
| 22 |
|
|
# |
| 23 |
|
|
# A copy of the GNU General Public License is available as |
| 24 |
|
|
# '/usr/share/common-licences/GPL' in the Debian GNU/Linux distribution |
| 25 |
|
|
# or on the World Wide Web at http://www.gnu.org/copyleft/gpl.html. You |
| 26 |
|
|
# can also obtain it by writing to the Free Software Foundation, Inc., |
| 27 |
lange |
4677 |
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
| 28 |
lange |
3997 |
#********************************************************************* |
| 29 |
|
|
|
| 30 |
lange |
4496 |
my $version = "Version 1.1, 5-august-2007"; |
| 31 |
lange |
3997 |
|
| 32 |
|
|
use Getopt::Std; |
| 33 |
lange |
4009 |
our ($opt_a,$opt_h,$opt_D,$opt_n,$opt_s,$opt_v); |
| 34 |
lange |
3997 |
|
| 35 |
|
|
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 36 |
|
|
sub usage { |
| 37 |
|
|
|
| 38 |
|
|
print << "EOF"; |
| 39 |
|
|
ainsl, AppendIfNoSuchLine written in Perl. $version |
| 40 |
|
|
|
| 41 |
lange |
4496 |
Copyright (C) 2006-2007 by Thomas Lange |
| 42 |
lange |
3997 |
|
| 43 |
|
|
Usage: ainsl [OPTION] FILE LINE [PATTERN] |
| 44 |
|
|
|
| 45 |
lange |
4009 |
-a Autocreate file if not existing. |
| 46 |
lange |
3997 |
-D Create debug output. |
| 47 |
|
|
-h Show summary of options. |
| 48 |
|
|
-n Print the actions, but do not execute them. |
| 49 |
lange |
4009 |
-s Convert blanks in line to '\\s+' regexp |
| 50 |
lange |
3997 |
-v Create verbose output. |
| 51 |
|
|
|
| 52 |
|
|
Report bugs to <lange\@informatik.uni-koeln.de>. |
| 53 |
|
|
EOF |
| 54 |
|
|
exit 0; |
| 55 |
|
|
} |
| 56 |
|
|
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 57 |
lange |
4009 |
sub autocreate { |
| 58 |
lange |
3997 |
|
| 59 |
lange |
4009 |
-f $filename && return; |
| 60 |
|
|
print "ainsl: create $filename\n" if $verbose; |
| 61 |
|
|
$opt_n && return; |
| 62 |
|
|
open (FILE,">$filename") || die "ainsl: can't create $filename $!"; |
| 63 |
|
|
close (FILE); |
| 64 |
|
|
} |
| 65 |
|
|
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 66 |
lange |
4135 |
getopts('aDhsvn') || usage; |
| 67 |
lange |
4009 |
|
| 68 |
lange |
3997 |
$opt_h && usage; |
| 69 |
|
|
$verbose = $opt_v || $ENV{verbose} || 0; |
| 70 |
|
|
$debug = $opt_D || $ENV{debug} || 0; |
| 71 |
|
|
|
| 72 |
|
|
$filename = shift; |
| 73 |
|
|
$line = shift; |
| 74 |
|
|
$optpattern = shift; |
| 75 |
|
|
$found = 0; |
| 76 |
|
|
usage() unless defined $line; |
| 77 |
|
|
|
| 78 |
lange |
4000 |
print "FILE: $filename\nLINE: $line\nPATTERN $pattern\n" if $debug; |
| 79 |
lange |
3997 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 80 |
lange |
4009 |
$opt_a && autocreate; |
| 81 |
lange |
3997 |
|
| 82 |
|
|
$pattern = (defined $optpattern) ? $optpattern: $line; |
| 83 |
|
|
# process pattern and line |
| 84 |
lange |
4000 |
$opt_s && $pattern=~ s/\s+/\\s+/g; |
| 85 |
lange |
3997 |
|
| 86 |
|
|
# remove ^ and $ in line (only at start and end), but still use it for pattern |
| 87 |
|
|
# in no explicit pattern was given |
| 88 |
|
|
unless (defined $optpattern) { |
| 89 |
lange |
4000 |
# remove special chars ^ and $ from line |
| 90 |
lange |
3997 |
$line =~ s/^\^//; |
| 91 |
|
|
$line =~ s/\$$//; |
| 92 |
lange |
4000 |
# escape '(' and ')' if no pattern was given and line is used |
| 93 |
|
|
$pattern =~s/\(/\\(/g; |
| 94 |
|
|
$pattern =~s/\)/\\)/g; |
| 95 |
lange |
4496 |
$pattern =~s/\+/\\+/g; |
| 96 |
lange |
3997 |
} |
| 97 |
|
|
|
| 98 |
lange |
4009 |
print "ainsl: newpattern: $pattern\n" if $debug; |
| 99 |
|
|
print "ainsl: newline: $line\n" if $debug; |
| 100 |
lange |
3997 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 101 |
|
|
# check if pattern already included in file |
| 102 |
|
|
open (INFILE, "<$filename") or die "Can't open $filename $!"; |
| 103 |
|
|
while (<INFILE>) { |
| 104 |
|
|
if (/$pattern/o) { |
| 105 |
lange |
4009 |
print "aisnl: Pattern found. Nothing to append.\n" if $debug; |
| 106 |
lange |
3997 |
$found=1; |
| 107 |
|
|
last; |
| 108 |
|
|
} |
| 109 |
|
|
} |
| 110 |
|
|
close(INFILE); |
| 111 |
|
|
exit 0 if $found; # nothing to append |
| 112 |
|
|
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 113 |
|
|
# Append line to file |
| 114 |
lange |
4009 |
print "ainsl: appending to $filename: $line\n" if $verbose; |
| 115 |
lange |
3997 |
exit 0 if $opt_n; |
| 116 |
lange |
4009 |
open (INFILE, ">>$filename") or die "ainsl: can't open $filename for writing. $!"; |
| 117 |
lange |
3997 |
print INFILE $line,"\n"; |
| 118 |
|
|
close(INFILE); |