1#!/usr/bin/perl -w
2# Derive #define directives from specially formatted `case ...:' statements.
3
4# Copyright (C) 2003, 2005, 2009-2010 Free Software Foundation, Inc.
5
6# This program is free software: you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation, either version 3 of the License, or
9# (at your option) any later version.
10
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details.
15
16# You should have received a copy of the GNU General Public License
17# along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19use strict;
20
21use Getopt::Long;
22
23(my $VERSION = '$Revision: 1.5 $ ') =~ tr/[0-9].//cd;
24(my $ME = $0) =~ s|.*/||;
25
26END
27{
28  # Nobody ever checks the status of print()s.  That's okay, because
29  # if any do fail, we're guaranteed to get an indicator when we close()
30  # the filehandle.
31  #
32  # Close stdout now, and if there were no errors, return happy status.
33  # If stdout has already been closed by the script, though, do nothing.
34  defined fileno STDOUT
35    or return;
36  close STDOUT
37    and return;
38
39  # Errors closing stdout.  Indicate that, and hope stderr is OK.
40  warn "$ME: closing standard output: $!\n";
41
42  # Don't be so arrogant as to assume that we're the first END handler
43  # defined, and thus the last one invoked.  There may be others yet
44  # to come.  $? will be passed on to them, and to the final _exit().
45  #
46  # If it isn't already an error, make it one (and if it _is_ an error,
47  # preserve the value: it might be important).
48  $? ||= 1;
49}
50
51sub usage ($)
52{
53  my ($exit_code) = @_;
54  my $STREAM = ($exit_code == 0 ? *STDOUT : *STDERR);
55  if ($exit_code != 0)
56    {
57      print $STREAM "Try `$ME --help' for more information.\n";
58    }
59  else
60    {
61      print $STREAM <<EOF;
62Usage: $ME [OPTIONS] FILE
63
64FIXME: describe
65
66OPTIONS:
67
68  Derive #define directives from specially formatted `case ...:' statements.
69
70   --help             display this help and exit
71   --version          output version information and exit
72
73EOF
74    }
75  exit $exit_code;
76}
77
78{
79  GetOptions
80    (
81     help => sub { usage 0 },
82     version => sub { print "$ME version $VERSION\n"; exit },
83    ) or usage 1;
84
85  my $fail = 0;
86
87  @ARGV < 1
88    and (warn "$ME: missing FILE arguments\n"), $fail = 1;
89  1 < @ARGV
90    and (warn "$ME: too many arguments\n"), $fail = 1;
91  $fail
92    and usage 1;
93
94  my $file = $ARGV[0];
95
96  open FH, $file
97    or die "$ME: can't open `$file' for reading: $!\n";
98
99  # For each line like this:
100  #   case S_MAGIC_ROMFS: /* 0x7275 */
101  # emit one like this:
102  #   # define S_MAGIC_ROMFS 0x7275
103  # Fail if there is a `case S_MAGIC_.*' line without
104  # a properly formed comment.
105
106  print <<EOF;
107/* Define the magic numbers as given by statfs(2).
108   Please send additions to bug-coreutils\@gnu.org and meskes\@debian.org.
109   This file is generated automatically from $file. */
110
111#if defined __linux__
112EOF
113
114  while (defined (my $line = <FH>))
115    {
116      $line =~ /^[ \t]+case S_MAGIC_/
117        or next;
118      $line =~ m!^[ \t]+case (S_MAGIC_\w+): /\* (0x[0-9A-Fa-f]+) \*/$!
119        or (warn "$ME:$file:$.: malformed case S_MAGIC_... line"),
120          $fail = 1, next;
121      my $name = $1;
122      my $value = $2;
123      print "# define $name $value\n";
124    }
125
126  print <<\EOF;
127#elif defined __GNU__
128# include <hurd/hurd_types.h>
129#endif
130EOF
131  close FH;
132
133  exit $fail;
134}
135