genericize.pl revision 134494
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 134494 2004-08-29 19:45:50Z des $
30#
31
32use strict;
33use Getopt::Std;
34
35MAIN:{
36    my %opts;
37    getopts('c', \%opts);
38
39    my %config;
40    my $machine;
41    my $ident;
42
43    while (<>) {
44	chomp();
45	s/\s*(\#.*)?$//;
46	next unless $_;
47	#print("$_\n");
48	my ($keyword, $values) = split(' ', $_, 2);
49	foreach my $value (split(/,\s*/, $values)) {
50	    if ($keyword eq 'machine') {
51		$machine = $value;
52	    } elsif ($keyword eq 'ident') {
53		$ident = $value;
54	    } else {
55		$config{$keyword}->{$value} = 1;
56	    }
57	    #print "$keyword $value\n";
58	}
59    }
60
61    my $generic = "/usr/src/sys/$machine/conf/GENERIC";
62    local *GENERIC;
63    open(GENERIC, "<", $generic)
64	or die("$generic: $!\n");
65    my $blank = 0;
66    while (<GENERIC>) {
67	my $line = $_;
68	chomp();
69	if ($opts{'c'} && m/^\#/) {
70	    if ($blank) {
71		print "\n";
72		$blank = 0;
73	    }
74	    print $line;
75	    next;
76	}
77	++$blank unless $_;
78	s/\s*(\#.*)?$//;
79	next unless $_;
80	my ($keyword, $value) = split(' ', $_);
81	if ($keyword eq 'machine') {
82	    die("$generic is for $value, not $machine\n")
83		unless ($value eq $machine);
84	} elsif ($keyword eq 'ident') {
85	    $line =~ s/$value/$ident/;
86	} elsif ($config{$keyword}->{$value}) {
87	    delete($config{$keyword}->{$value});
88	    delete($config{$keyword})
89		unless %{$config{$keyword}};
90	} else {
91	    next;
92	}
93	if ($blank) {
94	    print "\n";
95	    $blank = 0;
96	}
97	print $line;
98    }
99    close(GENERIC);
100
101    if (%config) {
102	print "\n# Addenda\n";
103	foreach my $keyword (sort(keys(%config))) {
104	    foreach my $value (sort(keys(%{$config{$keyword}}))) {
105		print "$keyword";
106		if (length($keyword) < 7) {
107		    print "\t";
108		} elsif (length($keyword) == 7) {
109		    print " ";
110		}
111		print "\t$value\n";
112	    }
113	}
114    }
115    exit(0);
116}
117