File: guards.in

package info (click to toggle)
quilt 0.60-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,856 kB
  • sloc: sh: 5,425; perl: 1,312; lisp: 430; makefile: 414
file content (271 lines) | stat: -rw-r--r-- 6,365 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#!@PERL@ -w

#  This script is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License version 2 as
#  published by the Free Software Foundation.
#
#  See the COPYING and AUTHORS files for more details.

#
# Guards:
#
# +xxx   include if xxx is defined
# -xxx   exclude if xxx is defined
# +!xxx  include if xxx is not defined
# -!xxx  exclude if xxx is not defined
#

use FileHandle;
use Getopt::Long;
use strict;

# Prototypes
sub files_in($$);
sub parse($$);
sub help();

sub slashme($) {
    my ($dir) = @_;
    $dir =~ s#([^/])$#$&/#; # append a slash if necessary
    if ($dir eq './') {
	return '';
    } else {
	return $dir;
    }
}

# Generate a list of files in a directory
#
sub files_in($$) {
    my ($dir, $path) = @_;
    my $dh = new FileHandle;
    my (@files, $file);


    opendir $dh, length("$dir$path") ? "$dir$path" : '.'
	or die "$dir$path: $!\n";
    while ($file = readdir($dh)) {
	next if $file =~ /^(\.|\.\.|\.#.*|CVS|.*~)$/;
	if (-d "$dir$path$file") {
		@files = (@files, files_in($dir, "$path$file/"));
	} else {
		#print "[$path$file]\n";
		push @files, "$path$file";
	}
    }
    closedir $dh;
    return @files;
}

# Parse a configuration file
# Callback called with ($patch, @guards) arguments
#
sub parse($$) {
    my ($fh, $callback) = @_;

    my $line = "";

    while (<$fh>) {
	chomp;
	s/(^|\s+)#.*//;
	if (s/\\$/ /) {
		$line .= $_;
		next;
	}
	$line .= $_;
        my @guards = ();
	foreach my $token (split /[\s\t\n]+/, $line) {
	    next if $token eq "";
	    if ($token =~ /^[-+]/) {
		push @guards, $token;
	    } else {
		#print "[" . join(",", @guards) . "] $token\n";
		&$callback($token, @guards);
	    }
	}
	$line = "";
    }
}

# Command line options
#
my ($dir, $config, $default, $check, $list, $invert_match) =
   (  '',     '-',        1,      0,     0,             0);
my @path;

# Help text
#
sub help() {
    print "$0 - select from a list of files guarded by conditions\n";
    print "SYNOPSIS: $0 [--prefix=dir] [--path=dir1:dir2:...]\n" .
	"	[--default=0|1] [--check|--list] [--config=file] " .
							"symbol ...\n\n" .
	"	(Default values: --path='" . join(':', @path) . "', " .
		"--default=$default)\n";
    exit 0;
}

# Parse command line options
#
Getopt::Long::Configure ("bundling");
eval {
    unless (GetOptions (
	'd|prefix=s' => \$dir,
	'c|config=s' => \$config,
	'C|check' => \$check,
	'l|list' => \$list,
	'p|path=s' => \@path,
	'D|default=i' => \$default,
	'v|invert-match' => \$invert_match,
	'h|help' => sub { help(); exit 0; })) {
	help();
	exit 1;
    }
};
if ($@) {
    print "$@";
    help();
    exit 1;
}

@path = ('.')
    unless (@path);
@path = split(/:/, join(':', @path));

my $fh = ($config eq '-') ? \*STDIN : new FileHandle($config)
    or die "$config: $!\n";

$dir = slashme($dir);

if ($check) {
    # Check for duplicate files, or for files that are not referenced by
    # the specification.

    my $problems = 0;
    my @files;

    foreach (@path) {
	@files = (@files, files_in($dir, slashme($_)));
    }
    my %files = map { $_ => 0 } @files;

    parse($fh, sub {
	my ($patch, @guards) = @_;
	if (exists $files{$patch}) {
	    $files{$patch}++;
	} else {
	    print "Not found: $dir$patch\n";
	    $problems++;
	}});

    $fh->close();

    my ($file, $ref);
    while (($file, $ref) = each %files) {
	next if $ref == 1;

	if ($ref == 0) {
	    print "Unused: $file\n" if $ref == 0;
	    $problems++;
	}
	if ($ref > 1) {
	    print "Warning: multiple uses: $file\n" if $ref > 1;
	    # This is not an error if the entries are mutually exclusive...
	}
    }
    exit $problems ? 1 : 0;

} elsif ($list) {
    parse($fh, sub {
	my ($patch, @guards) = @_;
	print "$dir$patch\n";
	});
} else {
    # Generate a list of patches to apply.

    my %symbols = map { $_ => 1 } @ARGV;

    parse($fh, sub {
	my ($patch, @guards) = @_;

	my $selected;
	if (@guards) {
	    # If the first guard is -xxx, the patch is included by default;
	    # if it is -xxx, the patch is excluded by default.
	    $selected = ($guards[0] =~ /^-/);

	    foreach (@guards) {
		/^([-+])(!?)(.*)?/
		    or die "Bad guard '$_'\n";

		# Check if the guard matches
		if (($2 eq '!' && !exists $symbols{$3}) ||
		    ($2 eq ''  && ( $3 eq '' || exists $symbols{$3}))) {
		    # Include or exclude
		    $selected = ($1 eq '+');
		}
	    }
	} else {
	    # If there are no guards, use the specified default result.
	    $selected = $default;
	}

	print "$dir$patch\n"
	    if $selected ^ $invert_match;
	});

    $fh->close();

    exit 0;
}

__END__

=head1 NAME

guards - select from a list of files guarded by conditions

=head1 SYNOPSIS

F<guards> [--prefix=F<dir>] [--path=F<dir2:dir2:...>] [--default=I<0>|I<1>]
	  [-v|--invert-match] [--list|--check] [--config=F<file>]
	  I<symbol> ...

=head1 DESCRIPTION

The script reads a configuration file that may contain so-called guards, file
names, and comments, and writes those file names that satisfy all guards to
standard output. The script takes a list of symbols as its arguments. Each line
in the configuration file is processed separately. Lines may start with a
number of guards. The following guards are defined:

=over

+I<xxx> Include the file(s) on this line if the symbol I<xxx> is defined.

-I<xxx> Exclude the file(s) on this line if the symbol I<xxx> is defined.

+!I<xxx> Include the file(s) on this line if the symbol I<xxx> is not defined.

-!I<xxx> Exclude the file(s) on this line if the symbol I<xxx> is not defined.

- Exclude this file. Used to avoid spurious I<--check> messages.

=back

The guards are processed left to right. The last guard that matches determines
if the file is included. If no guard is specified, the I<--default>
setting determines if the file is included.

If no configuration file is specified, the script reads from standard input.

The I<--check> option is used to compare the specification file against the
file system. If files are referenced in the specification that do not exist, or
if files are not enlisted in the specification file warnings are printed. The
I<--path> option can be used to specify which directory or directories to scan.
Multiple directories are separated by a colon (C<:>) character. The
I<--prefix> option specifies the location of the files.

=head1 AUTHOR

Andreas Gruenbacher <agruen@suse.de> (SuSE Linux AG)