genericize.pl revision 135480
1#!/usr/bin/perl -w
2#-
3# Copyright (c) 2004 Dag-Erling Co�dan Sm�rgrav
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9# 1. Redistributions of source code must retain the above copyright
10#    notice, this list of conditions and the following disclaimer
11#    in this position and unchanged.
12# 2. Redistributions in binary form must reproduce the above copyright
13#    notice, this list of conditions and the following disclaimer in the
14#    documentation and/or other materials provided with the distribution.
15# 3. The name of the author may not be used to endorse or promote products
16#    derived from this software without specific prior written permission.
17#
18# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28#
29# $FreeBSD: head/tools/tools/genericize/genericize.pl 135480 2004-09-19 21:21:26Z des $
30#
31
32use strict;
33use Getopt::Std;
34
35sub EMPTY() {}
36
37MAIN:{
38    my %opts;
39    getopts('c', \%opts);
40
41    my %config;
42    my $machine;
43    my $ident;
44
45    while (<>) {
46	chomp();
47	s/\s*(\#.*)?$//;
48	next unless $_;
49	#print("$_\n");
50	my ($keyword, $values) = split(' ', $_, 2);
51	foreach my $value (split(/,\s*/, $values)) {
52	    if ($keyword eq 'machine') {
53		$machine = $value;
54	    } elsif ($keyword eq 'ident') {
55		$ident = $value;
56	    } elsif ($keyword eq 'options' && $value =~ m/(\w+)=(.+)/) {
57		$config{$keyword}->{$1} = $2;
58	    } else {
59		$config{$keyword}->{$value} = \&EMPTY;
60	    }
61	}
62    }
63
64    my $generic = "/usr/src/sys/$machine/conf/GENERIC";
65    local *GENERIC;
66    open(GENERIC, "<", $generic)
67	or die("$generic: $!\n");
68    my $blank = 0;
69    while (<GENERIC>) {
70	my $line = $_;
71	chomp();
72	if ($opts{'c'} && m/^\s*\#/) {
73	    if ($blank) {
74		print "\n";
75		$blank = 0;
76	    }
77	    print $line;
78	    next;
79	}
80	++$blank unless $_;
81	s/\s*(\#.*)?$//;
82	next unless $_;
83	my ($keyword, $value) = split(' ', $_);
84	if ($keyword eq 'machine') {
85	    die("$generic is for $value, not $machine\n")
86		unless ($value eq $machine);
87	} elsif ($keyword eq 'ident') {
88	    $line =~ s/$value/$ident/;
89	} elsif ($keyword eq 'options' && $value =~ m/(\w+)=(.+)/ &&
90	    $config{$keyword}->{$1} != \&EMPTY) {
91	    $value = $1;
92	    if ($config{$keyword}->{$value} ne $2) {
93		my ($old, $new) = ($2, $config{$keyword}->{$value});
94		$line =~ s{=$old}{=$new};
95	    }
96	    delete($config{$keyword}->{$value});
97	    delete($config{$keyword})
98		unless %{$config{$keyword}};
99	} elsif (defined($config{$keyword}->{$value})) {
100	    delete($config{$keyword}->{$value});
101	    delete($config{$keyword})
102		unless %{$config{$keyword}};
103	} else {
104	    next;
105	}
106	if ($blank) {
107	    print "\n";
108	    $blank = 0;
109	}
110	print $line;
111    }
112    close(GENERIC);
113
114    if (%config) {
115	print "\n# Addenda\n";
116	foreach my $keyword (sort(keys(%config))) {
117	    foreach my $value (sort(keys(%{$config{$keyword}}))) {
118		print "$keyword";
119		if (length($keyword) < 7) {
120		    print "\t";
121		} elsif (length($keyword) == 7) {
122		    print " ";
123		}
124		print "\t$value";
125		print "=$config{$keyword}->{$value}"
126		    unless $config{$keyword}->{$value} == \&EMPTY;
127		print "\n";
128	    }
129	}
130    }
131    exit(0);
132}
133