1118425Sdes#!/usr/bin/perl -w
2118425Sdes#-
3228953Suqs# Copyright (c) 2003 Dag-Erling Co��dan Sm��rgrav
4118425Sdes# All rights reserved.
5118425Sdes#
6118425Sdes# Redistribution and use in source and binary forms, with or without
7118425Sdes# modification, are permitted provided that the following conditions
8118425Sdes# are met:
9118425Sdes# 1. Redistributions of source code must retain the above copyright
10118425Sdes#    notice, this list of conditions and the following disclaimer
11118425Sdes#    in this position and unchanged.
12118425Sdes# 2. Redistributions in binary form must reproduce the above copyright
13118425Sdes#    notice, this list of conditions and the following disclaimer in the
14118425Sdes#    documentation and/or other materials provided with the distribution.
15118425Sdes# 3. The name of the author may not be used to endorse or promote products
16118425Sdes#    derived from this software without specific prior written permission.
17118425Sdes#
18118425Sdes# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19118425Sdes# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20118425Sdes# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21118425Sdes# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22118425Sdes# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23118425Sdes# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24118425Sdes# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25118425Sdes# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26118425Sdes# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27118425Sdes# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28118425Sdes#
29118425Sdes# $FreeBSD$
30118425Sdes#
31118425Sdes
32118425Sdesuse strict;
33118425Sdesuse Getopt::Std;
34118425Sdes
35118425Sdesour $opt_b;
36118425Sdesour $opt_v;
37118425Sdes
38118425Sdessub hcomp($)
39118425Sdes{
40118425Sdes    my $fn = shift;
41118425Sdes
42118425Sdes    local *FILE;
43118425Sdes    my $header;
44118425Sdes
45118425Sdes    warn("$fn\n")
46118425Sdes	if ($opt_v);
47118425Sdes
48118425Sdes    open(FILE, "<", $fn)
49118425Sdes	or die("$fn: $!\n");
50118425Sdes    $header = join('', <FILE>);
51118425Sdes    close(FILE);
52118425Sdes
53118425Sdes    # Remove comments
54118425Sdes    $header =~ s|/\*.*?\*/||gs;
55118425Sdes    $header =~ s|//.*$||gm;
56118425Sdes
57118425Sdes    # Collapse preprocessor directives
58118425Sdes    while ($header =~ s|(\n\#.*?)\\\n|$1|gs) {
59118425Sdes	# nothing
60118425Sdes    }
61118425Sdes
62118425Sdes    # Remove superfluous whitespace
63118425Sdes    $header =~ s|^\s+||s;
64118425Sdes    $header =~ s|^\s+||gm;
65118425Sdes    $header =~ s|\s+$||s;
66118425Sdes    $header =~ s|\s+$||gm;
67118425Sdes    $header =~ s|\n+|\n|gs;
68118425Sdes    $header =~ s|[ \t]+| |gm;
69118425Sdes
70118425Sdes    open(FILE, ">", "$fn.new")
71118425Sdes	or die("$fn.new: $!\n");
72118425Sdes    print(FILE $header);
73118425Sdes    close(FILE);
74118425Sdes
75118425Sdes    rename($fn, "$fn.$opt_b")
76118425Sdes	if defined($opt_b);
77118425Sdes    rename("$fn.new", $fn);
78118425Sdes}
79118425Sdes
80118425Sdessub usage()
81118425Sdes{
82118425Sdes    print(STDERR "usage: hcomp [-b ext] file ...\n");
83118425Sdes    exit(1);
84118425Sdes}
85118425Sdes
86118425SdesMAIN:{
87118425Sdes    my %opts;
88118425Sdes    getopts('b:v')
89118425Sdes	or usage();
90118425Sdes    foreach (@ARGV) {
91118425Sdes	hcomp($_);
92118425Sdes    }
93118425Sdes}
94