* NEWS: Add a line for 6.5-cvs.
[coreutils.git] / src / extract-magic
blob2e9e871d3a6698d6469b453d5fc1a56f068f664c
1 #!/usr/bin/perl -w
2 # Derive #define directives from specially formatted `case ...:' statements.
4 # Copyright (C) 2003, 2005 Free Software Foundation, Inc.
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 2, or (at your option)
9 # any later version.
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.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software Foundation,
18 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 use strict;
22 use Getopt::Long;
24 (my $VERSION = '$Revision: 1.5 $ ') =~ tr/[0-9].//cd;
25 (my $ME = $0) =~ s|.*/||;
27 END
29 # Nobody ever checks the status of print()s. That's okay, because
30 # if any do fail, we're guaranteed to get an indicator when we close()
31 # the filehandle.
33 # Close stdout now, and if there were no errors, return happy status.
34 # If stdout has already been closed by the script, though, do nothing.
35 defined fileno STDOUT
36 or return;
37 close STDOUT
38 and return;
40 # Errors closing stdout. Indicate that, and hope stderr is OK.
41 warn "$ME: closing standard output: $!\n";
43 # Don't be so arrogant as to assume that we're the first END handler
44 # defined, and thus the last one invoked. There may be others yet
45 # to come. $? will be passed on to them, and to the final _exit().
47 # If it isn't already an error, make it one (and if it _is_ an error,
48 # preserve the value: it might be important).
49 $? ||= 1;
52 sub usage ($)
54 my ($exit_code) = @_;
55 my $STREAM = ($exit_code == 0 ? *STDOUT : *STDERR);
56 if ($exit_code != 0)
58 print $STREAM "Try `$ME --help' for more information.\n";
60 else
62 print $STREAM <<EOF;
63 Usage: $ME [OPTIONS] FILE
65 FIXME: describe
67 OPTIONS:
69 Derive #define directives from specially formatted `case ...:' statements.
71 --help display this help and exit
72 --version output version information and exit
74 EOF
76 exit $exit_code;
80 GetOptions
82 help => sub { usage 0 },
83 version => sub { print "$ME version $VERSION\n"; exit },
84 ) or usage 1;
86 my $fail = 0;
88 @ARGV < 1
89 and (warn "$ME: missing FILE arguments\n"), $fail = 1;
90 1 < @ARGV
91 and (warn "$ME: too many arguments\n"), $fail = 1;
92 $fail
93 and usage 1;
95 my $file = $ARGV[0];
97 open FH, $file
98 or die "$ME: can't open `$file' for reading: $!\n";
100 # For each line like this:
101 # case S_MAGIC_ROMFS: /* 0x7275 */
102 # emit one like this:
103 # # define S_MAGIC_ROMFS 0x7275
104 # Fail if there is a `case S_MAGIC_.*' line without
105 # a properly formed comment.
107 print <<EOF;
108 /* Define the magic numbers as given by statfs(2).
109 Please send additions to bug-coreutils\@gnu.org and meskes\@debian.org.
110 This file is generated automatically from $file. */
112 #if defined __linux__
115 while (defined (my $line = <FH>))
117 $line =~ /^[ \t]+case S_MAGIC_/
118 or next;
119 $line =~ m!^[ \t]+case (S_MAGIC_\w+): /\* (0x[0-9A-Fa-f]+) \*/$!
120 or (warn "$ME:$file:$.: malformed case S_MAGIC_... line"),
121 $fail = 1, next;
122 my $name = $1;
123 my $value = $2;
124 print "# define $name $value\n";
127 print <<\EOF;
128 #elif defined __GNU__
129 # include <hurd/hurd_types.h>
130 #endif
132 close FH;
134 exit $fail;