1*0Sstevel@tonic-gate#!/usr/bin/perl 2*0Sstevel@tonic-gate# 3*0Sstevel@tonic-gate# CDDL HEADER START 4*0Sstevel@tonic-gate# 5*0Sstevel@tonic-gate# The contents of this file are subject to the terms of the 6*0Sstevel@tonic-gate# Common Development and Distribution License, Version 1.0 only 7*0Sstevel@tonic-gate# (the "License"). You may not use this file except in compliance 8*0Sstevel@tonic-gate# with the License. 9*0Sstevel@tonic-gate# 10*0Sstevel@tonic-gate# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 11*0Sstevel@tonic-gate# or http://www.opensolaris.org/os/licensing. 12*0Sstevel@tonic-gate# See the License for the specific language governing permissions 13*0Sstevel@tonic-gate# and limitations under the License. 14*0Sstevel@tonic-gate# 15*0Sstevel@tonic-gate# When distributing Covered Code, include this CDDL HEADER in each 16*0Sstevel@tonic-gate# file and include the License file at usr/src/OPENSOLARIS.LICENSE. 17*0Sstevel@tonic-gate# If applicable, add the following below this CDDL HEADER, with the 18*0Sstevel@tonic-gate# fields enclosed by brackets "[]" replaced with your own identifying 19*0Sstevel@tonic-gate# information: Portions Copyright [yyyy] [name of copyright owner] 20*0Sstevel@tonic-gate# 21*0Sstevel@tonic-gate# CDDL HEADER END 22*0Sstevel@tonic-gate# 23*0Sstevel@tonic-gate 24*0Sstevel@tonic-gate# Copyright 2003 Sun Microsystems, Inc. All rights reserved. 25*0Sstevel@tonic-gate# Use is subject to license terms. 26*0Sstevel@tonic-gate# 27*0Sstevel@tonic-gate#ident "%Z%%M% %I% %E% SMI" 28*0Sstevel@tonic-gate 29*0Sstevel@tonic-gate# Given either a list of files containing paths on the command line or 30*0Sstevel@tonic-gate# a set of paths on standard input, validate that the paths actually 31*0Sstevel@tonic-gate# exist, and complain if they do not. This is invoked by nightly to 32*0Sstevel@tonic-gate# verify the contents of various control files used by the ON build 33*0Sstevel@tonic-gate# process. 34*0Sstevel@tonic-gate# 35*0Sstevel@tonic-gate# Command line options: 36*0Sstevel@tonic-gate# 37*0Sstevel@tonic-gate# -m Show the matches (for debug). 38*0Sstevel@tonic-gate# 39*0Sstevel@tonic-gate# -r Allow shell globs in the paths. Unless otherwise 40*0Sstevel@tonic-gate# flagged by a keyword (see -k) or exclusion (see -e), 41*0Sstevel@tonic-gate# it is an error if no files match the expression at 42*0Sstevel@tonic-gate# all. 43*0Sstevel@tonic-gate# 44*0Sstevel@tonic-gate# -s/from/to/ 45*0Sstevel@tonic-gate# Perform a substitution on all of the paths in the 46*0Sstevel@tonic-gate# file. This substitution is performed after stripping 47*0Sstevel@tonic-gate# any in-line comments but before any exclusion matching 48*0Sstevel@tonic-gate# is done. The option may include any legal Perl 49*0Sstevel@tonic-gate# substitution expression and may be repeated to give 50*0Sstevel@tonic-gate# multiple expressions. 51*0Sstevel@tonic-gate# 52*0Sstevel@tonic-gate# -e <pattern> 53*0Sstevel@tonic-gate# Exclude paths matching the given pattern from the 54*0Sstevel@tonic-gate# "must exist" rule. These paths will not be checked. 55*0Sstevel@tonic-gate# Option may include any legal Perl regular expression, 56*0Sstevel@tonic-gate# and may be repeated to give multiple patterns. 57*0Sstevel@tonic-gate# 58*0Sstevel@tonic-gate# -k <keyword> 59*0Sstevel@tonic-gate# Exclude paths if there is either an in-line comment 60*0Sstevel@tonic-gate# containing the given keyword, or the preceding line 61*0Sstevel@tonic-gate# consists of only a comment containing that keyword. 62*0Sstevel@tonic-gate# Option may be repeated to provide multiple keywords. 63*0Sstevel@tonic-gate# 64*0Sstevel@tonic-gate# -b <base> 65*0Sstevel@tonic-gate# Base directory for relative paths tested. 66*0Sstevel@tonic-gate 67*0Sstevel@tonic-gateuse strict; 68*0Sstevel@tonic-gate 69*0Sstevel@tonic-gatemy ($opt_r, $opt_m, @opt_s, @opt_e, @opt_k, $opt_b); 70*0Sstevel@tonic-gatemy ($keywords, @exclude); 71*0Sstevel@tonic-gate 72*0Sstevel@tonic-gatesub usage { 73*0Sstevel@tonic-gate die "usage: $0 [-r] [-m]\n", 74*0Sstevel@tonic-gate "\t[-s/from/to/] [-e <pattern>] [-k <keyword>] [-b <base>]\n", 75*0Sstevel@tonic-gate "\t[files...]\n"; 76*0Sstevel@tonic-gate} 77*0Sstevel@tonic-gate 78*0Sstevel@tonic-gate# process the path list in a given file 79*0Sstevel@tonic-gatesub process_paths { 80*0Sstevel@tonic-gate my ($FILE, $name) = @_; 81*0Sstevel@tonic-gate my ($ignore, $file, $line); 82*0Sstevel@tonic-gate $ignore = 0; 83*0Sstevel@tonic-gate $line = 0; 84*0Sstevel@tonic-gate while (<$FILE>) { 85*0Sstevel@tonic-gate chomp; 86*0Sstevel@tonic-gate $line++; 87*0Sstevel@tonic-gate # Ignore comment lines 88*0Sstevel@tonic-gate if (/^\s*#(.*)$/) { 89*0Sstevel@tonic-gate $ignore = ($1 =~ /$keywords/) if defined $keywords; 90*0Sstevel@tonic-gate next; 91*0Sstevel@tonic-gate } 92*0Sstevel@tonic-gate # Extract path as $1 from line 93*0Sstevel@tonic-gate if (/^\s*([^#]+)#(.*)$/) { 94*0Sstevel@tonic-gate ($ignore = 0, next) if $ignore; 95*0Sstevel@tonic-gate $ignore = ($2 =~ /$keywords/) if defined $keywords; 96*0Sstevel@tonic-gate ($ignore = 0, next) if $ignore; 97*0Sstevel@tonic-gate } elsif (/^\s*([^#]+)$/) { 98*0Sstevel@tonic-gate ($ignore = 0, next) if $ignore; 99*0Sstevel@tonic-gate } else { 100*0Sstevel@tonic-gate # Ignore blank lines 101*0Sstevel@tonic-gate $ignore = 0; 102*0Sstevel@tonic-gate next; 103*0Sstevel@tonic-gate } 104*0Sstevel@tonic-gate # remove any trailing spaces from path 105*0Sstevel@tonic-gate ($file = $1) =~ s/[ ]*$//; 106*0Sstevel@tonic-gate # perform user-supplied substitutions 107*0Sstevel@tonic-gate foreach my $pat (@opt_s) { 108*0Sstevel@tonic-gate eval '$file =~ s' . $pat; 109*0Sstevel@tonic-gate } 110*0Sstevel@tonic-gate # check if the given path is on the 'exclude' list 111*0Sstevel@tonic-gate $ignore = 0; 112*0Sstevel@tonic-gate foreach my $pat (@exclude) { 113*0Sstevel@tonic-gate ($ignore = 1, last) if $file =~ /$pat/; 114*0Sstevel@tonic-gate } 115*0Sstevel@tonic-gate if ($ignore == 0) { 116*0Sstevel@tonic-gate # construct the actual path to the file 117*0Sstevel@tonic-gate my $path = $opt_b . $file; 118*0Sstevel@tonic-gate # Expand any shell globs, if that feature is on. Since 119*0Sstevel@tonic-gate # Perl's glob() is stateful, we use an array assignment 120*0Sstevel@tonic-gate # to get the first match and discard the others. 121*0Sstevel@tonic-gate ($path) = glob($path) if $opt_r; 122*0Sstevel@tonic-gate print "$name:$line: $file\n" unless !$opt_m && -e $path; 123*0Sstevel@tonic-gate print " $path\n" if $opt_m; 124*0Sstevel@tonic-gate } 125*0Sstevel@tonic-gate $ignore = 0; 126*0Sstevel@tonic-gate } 127*0Sstevel@tonic-gate} 128*0Sstevel@tonic-gate 129*0Sstevel@tonic-gatesub next_arg { 130*0Sstevel@tonic-gate my ($arg) = @_; 131*0Sstevel@tonic-gate if ($arg eq "") { 132*0Sstevel@tonic-gate die "$0: missing argument for $_\n" if $#ARGV == -1; 133*0Sstevel@tonic-gate $arg = shift @ARGV; 134*0Sstevel@tonic-gate } 135*0Sstevel@tonic-gate $arg; 136*0Sstevel@tonic-gate} 137*0Sstevel@tonic-gate 138*0Sstevel@tonic-gate# I'd like to use Perl's getopts here, but it doesn't handle repeated 139*0Sstevel@tonic-gate# options, and using comma separators is just too ugly. 140*0Sstevel@tonic-gate# This doesn't handle combined options (as in '-rm'), but I don't care. 141*0Sstevel@tonic-gatemy $arg, $opt_r, $opt_m, @opt_s, @opt_e, @opt_k, $opt_b; 142*0Sstevel@tonic-gatewhile ($#ARGV >= 0) { 143*0Sstevel@tonic-gate $_ = $ARGV[0]; 144*0Sstevel@tonic-gate last if /^[^-]/; 145*0Sstevel@tonic-gate shift @ARGV; 146*0Sstevel@tonic-gate last if /^--$/; 147*0Sstevel@tonic-gate SWITCH: { 148*0Sstevel@tonic-gate /^-r/ && do { $opt_r = 1; last SWITCH; }; 149*0Sstevel@tonic-gate /^-m/ && do { $opt_m = 1; last SWITCH; }; 150*0Sstevel@tonic-gate if (/^-s(.*)$/) { 151*0Sstevel@tonic-gate $arg = next_arg($1); 152*0Sstevel@tonic-gate push @opt_s, $arg; 153*0Sstevel@tonic-gate last SWITCH; 154*0Sstevel@tonic-gate } 155*0Sstevel@tonic-gate if (/^-e(.*)$/) { 156*0Sstevel@tonic-gate $arg = next_arg($1); 157*0Sstevel@tonic-gate push @opt_e, $arg; 158*0Sstevel@tonic-gate last SWITCH; 159*0Sstevel@tonic-gate } 160*0Sstevel@tonic-gate if (/^-k(.*)$/) { 161*0Sstevel@tonic-gate $arg = next_arg($1); 162*0Sstevel@tonic-gate push @opt_k, $arg; 163*0Sstevel@tonic-gate last SWITCH; 164*0Sstevel@tonic-gate } 165*0Sstevel@tonic-gate if (/^-b(.*)$/) { 166*0Sstevel@tonic-gate $opt_b = next_arg($1); 167*0Sstevel@tonic-gate last SWITCH; 168*0Sstevel@tonic-gate } 169*0Sstevel@tonic-gate print "$0: unknown option $_\n"; 170*0Sstevel@tonic-gate usage(); 171*0Sstevel@tonic-gate } 172*0Sstevel@tonic-gate} 173*0Sstevel@tonic-gate 174*0Sstevel@tonic-gate# compile the 'exclude' regexps 175*0Sstevel@tonic-gate@exclude = map qr/$_/x, @opt_e; 176*0Sstevel@tonic-gate# if no keywords are given, then leave $keywords undefined 177*0Sstevel@tonic-gateif (@opt_k) { 178*0Sstevel@tonic-gate # construct a regexp that matches the keywords specified 179*0Sstevel@tonic-gate my $opt_k = join("|", @opt_k); 180*0Sstevel@tonic-gate $keywords = qr/($opt_k)/xo; 181*0Sstevel@tonic-gate} 182*0Sstevel@tonic-gate$opt_b .= "/" if $opt_b =~ /[^\/]$/; 183*0Sstevel@tonic-gate 184*0Sstevel@tonic-gatemy $file; 185*0Sstevel@tonic-gate 186*0Sstevel@tonic-gateif ($#ARGV < 0) { 187*0Sstevel@tonic-gate process_paths(\*STDIN, "standard input"); 188*0Sstevel@tonic-gate} else { 189*0Sstevel@tonic-gate foreach $file (@ARGV) { 190*0Sstevel@tonic-gate if (! -e $file) { 191*0Sstevel@tonic-gate warn "$0: $file doesn't exist\n"; 192*0Sstevel@tonic-gate } elsif (! -f $file) { 193*0Sstevel@tonic-gate warn "$0: $file isn't a regular file\n"; 194*0Sstevel@tonic-gate } elsif (! -T $file) { 195*0Sstevel@tonic-gate warn "$0: $file isn't a text file\n"; 196*0Sstevel@tonic-gate } elsif (open FILE, "<$file") { 197*0Sstevel@tonic-gate process_paths(\*FILE, $file); 198*0Sstevel@tonic-gate } else { 199*0Sstevel@tonic-gate warn "$0: $file: $!\n"; 200*0Sstevel@tonic-gate } 201*0Sstevel@tonic-gate } 202*0Sstevel@tonic-gate} 203*0Sstevel@tonic-gate 204*0Sstevel@tonic-gateexit 0 205