xref: /titanic_50/usr/src/tools/scripts/validate_flg.pl (revision fb9f9b975cb9214fec5dab37d461199adab9b964)
17c478bd9Sstevel@tonic-gate#!/usr/bin/perl
27c478bd9Sstevel@tonic-gate#
37c478bd9Sstevel@tonic-gate# CDDL HEADER START
47c478bd9Sstevel@tonic-gate#
57c478bd9Sstevel@tonic-gate# The contents of this file are subject to the terms of the
67c478bd9Sstevel@tonic-gate# Common Development and Distribution License, Version 1.0 only
77c478bd9Sstevel@tonic-gate# (the "License").  You may not use this file except in compliance
87c478bd9Sstevel@tonic-gate# with the License.
97c478bd9Sstevel@tonic-gate#
107c478bd9Sstevel@tonic-gate# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
117c478bd9Sstevel@tonic-gate# or http://www.opensolaris.org/os/licensing.
127c478bd9Sstevel@tonic-gate# See the License for the specific language governing permissions
137c478bd9Sstevel@tonic-gate# and limitations under the License.
147c478bd9Sstevel@tonic-gate#
157c478bd9Sstevel@tonic-gate# When distributing Covered Code, include this CDDL HEADER in each
167c478bd9Sstevel@tonic-gate# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
177c478bd9Sstevel@tonic-gate# If applicable, add the following below this CDDL HEADER, with the
187c478bd9Sstevel@tonic-gate# fields enclosed by brackets "[]" replaced with your own identifying
197c478bd9Sstevel@tonic-gate# information: Portions Copyright [yyyy] [name of copyright owner]
207c478bd9Sstevel@tonic-gate#
217c478bd9Sstevel@tonic-gate# CDDL HEADER END
227c478bd9Sstevel@tonic-gate#
237c478bd9Sstevel@tonic-gate
24*fb9f9b97Skupfer# Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
257c478bd9Sstevel@tonic-gate# Use is subject to license terms.
267c478bd9Sstevel@tonic-gate#
277c478bd9Sstevel@tonic-gate#ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate
297c478bd9Sstevel@tonic-gateuse strict;
307c478bd9Sstevel@tonic-gateuse File::Find ();
317c478bd9Sstevel@tonic-gaterequire v5.6.1;
327c478bd9Sstevel@tonic-gate
337c478bd9Sstevel@tonic-gateuse vars qw/$f_flg *name *dir @execlist $basedir/;
347c478bd9Sstevel@tonic-gate*name   = *File::Find::name;
357c478bd9Sstevel@tonic-gate*dir    = *File::Find::dir;
367c478bd9Sstevel@tonic-gate
377c478bd9Sstevel@tonic-gate# Use the same mechanism as def.dir.flp to determine if there are any
387c478bd9Sstevel@tonic-gate# SCCS files matching the pattern supplied for a "find_files"
397c478bd9Sstevel@tonic-gate# statement.
407c478bd9Sstevel@tonic-gatesub sccs_empty {
417c478bd9Sstevel@tonic-gate    my ($pat, $dir) = @_;
427c478bd9Sstevel@tonic-gate    return 0 if $f_flg;
437c478bd9Sstevel@tonic-gate    my $foo = `find $dir -name "$pat" -print | grep /SCCS/s.`;
447c478bd9Sstevel@tonic-gate    $foo eq "";
457c478bd9Sstevel@tonic-gate}
467c478bd9Sstevel@tonic-gate
477c478bd9Sstevel@tonic-gate# Not pretty, but simple enough to work for the known cases.
487c478bd9Sstevel@tonic-gate# Does not bother with curly braces or fancy substitutions.
497c478bd9Sstevel@tonic-gatesub expand {
507c478bd9Sstevel@tonic-gate    my ($str) = @_;
517c478bd9Sstevel@tonic-gate    while ($str =~ /\$(\w+)/) {
527c478bd9Sstevel@tonic-gate	my $newstr = $ENV{$1};
537c478bd9Sstevel@tonic-gate	$str =~ s/\$$1/$newstr/g;
547c478bd9Sstevel@tonic-gate    }
557c478bd9Sstevel@tonic-gate    $str;
567c478bd9Sstevel@tonic-gate}
577c478bd9Sstevel@tonic-gate
587c478bd9Sstevel@tonic-gate# Process a single inc.flg or req.flg file.
597c478bd9Sstevel@tonic-gatesub process_file {
607c478bd9Sstevel@tonic-gate    my ($fname, $incpath) = @_;
617c478bd9Sstevel@tonic-gate    my ($dname, $isincflg);
627c478bd9Sstevel@tonic-gate    my ($expfile, $newpath, $line, $cont, $firstline, $text);
637c478bd9Sstevel@tonic-gate
647c478bd9Sstevel@tonic-gate    $dname = $fname;
657c478bd9Sstevel@tonic-gate    $dname =~ s+/[^/]*$++;
667c478bd9Sstevel@tonic-gate
677c478bd9Sstevel@tonic-gate    $isincflg = $fname =~ /inc.flg$/;
687c478bd9Sstevel@tonic-gate
697c478bd9Sstevel@tonic-gate    if (defined $incpath) {
707c478bd9Sstevel@tonic-gate	$newpath = "$incpath, from $fname:";
717c478bd9Sstevel@tonic-gate    } else {
727c478bd9Sstevel@tonic-gate	$newpath = "from $fname:";
737c478bd9Sstevel@tonic-gate    }
747c478bd9Sstevel@tonic-gate
757c478bd9Sstevel@tonic-gate    if (open INC, "<$fname") {
767c478bd9Sstevel@tonic-gate	$line = 0;
777c478bd9Sstevel@tonic-gate	$cont = 0;
787c478bd9Sstevel@tonic-gate	while (<INC>) {
797c478bd9Sstevel@tonic-gate	    chomp;
807c478bd9Sstevel@tonic-gate	    $line++;
817c478bd9Sstevel@tonic-gate	    ( $cont = 0, next ) if /^\s*#/ || /^\s*$/;
827c478bd9Sstevel@tonic-gate	    if ($cont) {
837c478bd9Sstevel@tonic-gate		$text = $text . $_;
847c478bd9Sstevel@tonic-gate	    } else {
857c478bd9Sstevel@tonic-gate		$firstline = $line;
867c478bd9Sstevel@tonic-gate		$text = $_;
877c478bd9Sstevel@tonic-gate	    }
887c478bd9Sstevel@tonic-gate	    if (/\\$/) {
897c478bd9Sstevel@tonic-gate		$cont = 1;
907c478bd9Sstevel@tonic-gate		$text =~ s/\\$//;
917c478bd9Sstevel@tonic-gate		next;
927c478bd9Sstevel@tonic-gate	    }
937c478bd9Sstevel@tonic-gate	    $cont = 0;
947c478bd9Sstevel@tonic-gate	    if ($text =~ /\s*echo_file\s+(\S+)/) {
957c478bd9Sstevel@tonic-gate		$expfile = expand($1);
967c478bd9Sstevel@tonic-gate		warn "$fname:$firstline: $1 isn't a file\n" if ! -f $expfile;
977c478bd9Sstevel@tonic-gate	    } elsif ($text =~ /\s*find_files\s+['"]([^'"]+)['"]\s+(.*)/) {
987c478bd9Sstevel@tonic-gate		foreach my $dir (split(/\s+/, "$2")) {
997c478bd9Sstevel@tonic-gate		    $expfile = expand($dir);
1007c478bd9Sstevel@tonic-gate		    if (! -d $expfile) {
1017c478bd9Sstevel@tonic-gate			warn "$fname:$firstline: $dir isn't a directory\n";
1027c478bd9Sstevel@tonic-gate		    } elsif ($isincflg && $expfile eq $dname) {
1037c478bd9Sstevel@tonic-gate			warn "$fname:$firstline: $dir is unnecessary\n";
1047c478bd9Sstevel@tonic-gate		    } elsif (sccs_empty($1, $expfile)) {
1057c478bd9Sstevel@tonic-gate			warn "$fname:$firstline: $dir has no SCCS objects ",
1067c478bd9Sstevel@tonic-gate				"with '$1'\n";
1077c478bd9Sstevel@tonic-gate		    }
1087c478bd9Sstevel@tonic-gate		}
1097c478bd9Sstevel@tonic-gate	    } elsif ($text =~ /\s*exec_file\s+(\S+)/) {
1107c478bd9Sstevel@tonic-gate		$expfile = expand($1);
1117c478bd9Sstevel@tonic-gate		if (-f $expfile) {
1127c478bd9Sstevel@tonic-gate		    push @execlist, $expfile, "$newpath:$firstline";
1137c478bd9Sstevel@tonic-gate		} else {
1147c478bd9Sstevel@tonic-gate		    warn "$fname:$firstline: $1 isn't a file\n";
1157c478bd9Sstevel@tonic-gate		    warn "included $incpath\n" if defined $incpath;
1167c478bd9Sstevel@tonic-gate		}
1177c478bd9Sstevel@tonic-gate	    } else {
1187c478bd9Sstevel@tonic-gate		warn "$0: $fname:$firstline: unknown entry: $text\n";
1197c478bd9Sstevel@tonic-gate		warn "included $incpath\n" if defined $incpath;
1207c478bd9Sstevel@tonic-gate	    }
1217c478bd9Sstevel@tonic-gate	}
1227c478bd9Sstevel@tonic-gate	close INC;
1237c478bd9Sstevel@tonic-gate    } else {
1247c478bd9Sstevel@tonic-gate	warn "$0: $fname: $!\n";
1257c478bd9Sstevel@tonic-gate    }
1267c478bd9Sstevel@tonic-gate}
1277c478bd9Sstevel@tonic-gate
1287c478bd9Sstevel@tonic-gatesub wanted {
1297c478bd9Sstevel@tonic-gate    process_file($_, undef) if /\/(inc|req)\.flg$/ && -f $_;
1307c478bd9Sstevel@tonic-gate}
1317c478bd9Sstevel@tonic-gate
1327c478bd9Sstevel@tonic-gate$f_flg = $ARGV[0] eq "-f";
1337c478bd9Sstevel@tonic-gateshift @ARGV if $f_flg;
1347c478bd9Sstevel@tonic-gate
135*fb9f9b97Skupfer$basedir = "usr";
1367c478bd9Sstevel@tonic-gateif ($#ARGV == 0) {
1377c478bd9Sstevel@tonic-gate    $basedir = shift @ARGV;
1387c478bd9Sstevel@tonic-gate} elsif ($#ARGV > 0) {
1397c478bd9Sstevel@tonic-gate    die "$0: unexpected arguments\n";
1407c478bd9Sstevel@tonic-gate}
1417c478bd9Sstevel@tonic-gate
1427c478bd9Sstevel@tonic-gatedie "$0: \$CODEMGR_WS must be set\n" if $ENV{CODEMGR_WS} eq "";
1437c478bd9Sstevel@tonic-gatechdir $ENV{CODEMGR_WS} or die "$0: chdir $ENV{CODEMGR_WS}: $!\n";
1447c478bd9Sstevel@tonic-gate
1457c478bd9Sstevel@tonic-gateFile::Find::find({wanted => \&wanted, no_chdir => 1}, $basedir);
1467c478bd9Sstevel@tonic-gate
1477c478bd9Sstevel@tonic-gate# After passing through the tree, process all of the included files.
1487c478bd9Sstevel@tonic-gate# There aren't many of these, so don't bother trying to optimize the
1497c478bd9Sstevel@tonic-gate# traversal.  Just do them all.
1507c478bd9Sstevel@tonic-gatewhile (@execlist) {
1517c478bd9Sstevel@tonic-gate    my $file = shift @execlist;
1527c478bd9Sstevel@tonic-gate    my $incpath = shift @execlist;
1537c478bd9Sstevel@tonic-gate    process_file($file, $incpath);
1547c478bd9Sstevel@tonic-gate}
1557c478bd9Sstevel@tonic-gate
1567c478bd9Sstevel@tonic-gateexit 0;
157