1181344Sdfr#!/usr/bin/perl -w
2181344Sdfr#-
3208291Suqs# Copyright (c) 2003 Dag-Erling Sm��rgrav
4181344Sdfr# All rights reserved.
5181344Sdfr#
6181344Sdfr# Redistribution and use in source and binary forms, with or without
7181344Sdfr# modification, are permitted provided that the following conditions
8181344Sdfr# are met:
9181344Sdfr# 1. Redistributions of source code must retain the above copyright
10181344Sdfr#    notice, this list of conditions and the following disclaimer
11181344Sdfr#    in this position and unchanged.
12181344Sdfr# 2. Redistributions in binary form must reproduce the above copyright
13181344Sdfr#    notice, this list of conditions and the following disclaimer in the
14181344Sdfr#    documentation and/or other materials provided with the distribution.
15181344Sdfr# 3. The name of the author may not be used to endorse or promote products
16181344Sdfr#    derived from this software without specific prior written permission.
17181344Sdfr#
18181344Sdfr# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19181344Sdfr# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20181344Sdfr# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21181344Sdfr# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22181344Sdfr# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23181344Sdfr# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24181344Sdfr# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25181344Sdfr# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26181344Sdfr# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27203026Sgavin# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28181344Sdfr#
29181344Sdfr#
30181344Sdfr
31181344Sdfruse strict;
32181344Sdfruse Getopt::Std;
33181344Sdfr
34181344Sdfrour $opt_b;
35181344Sdfrour $opt_v;
36181344Sdfr
37181344Sdfrsub hcomp($)
38181344Sdfr{
39181344Sdfr    my $fn = shift;
40181344Sdfr
41181344Sdfr    local *FILE;
42181344Sdfr    my $header;
43236668Sjoel
44181344Sdfr    warn("$fn\n")
45181344Sdfr	if ($opt_v);
46181344Sdfr
47181344Sdfr    open(FILE, "<", $fn)
48181344Sdfr	or die("$fn: $!\n");
49181344Sdfr    $header = join('', <FILE>);
50181344Sdfr    close(FILE);
51181344Sdfr
52181344Sdfr    # Remove comments
53181344Sdfr    $header =~ s|/\*.*?\*/||gs;
54181344Sdfr    $header =~ s|//.*$||gm;
55181344Sdfr
56181344Sdfr    # Collapse preprocessor directives
57181344Sdfr    while ($header =~ s|(\n\#.*?)\\\n|$1|gs) {
58181344Sdfr	# nothing
59181344Sdfr    }
60181344Sdfr
61181344Sdfr    # Remove superfluous whitespace
62181344Sdfr    $header =~ s|^\s+||s;
63203025Sgavin    $header =~ s|^\s+||gm;
64181344Sdfr    $header =~ s|\s+$||s;
65181344Sdfr    $header =~ s|\s+$||gm;
66181344Sdfr    $header =~ s|\n+|\n|gs;
67181344Sdfr    $header =~ s|[ \t]+| |gm;
68181344Sdfr
69    open(FILE, ">", "$fn.new")
70	or die("$fn.new: $!\n");
71    print(FILE $header);
72    close(FILE);
73
74    rename($fn, "$fn.$opt_b")
75	if defined($opt_b);
76    rename("$fn.new", $fn);
77}
78
79sub usage()
80{
81    print(STDERR "usage: hcomp [-b ext] file ...\n");
82    exit(1);
83}
84
85MAIN:{
86    my %opts;
87    getopts('b:v')
88	or usage();
89    foreach (@ARGV) {
90	hcomp($_);
91    }
92}
93