.
[coreutils.git] / src / extract-magic
blobe3541594edd6613a645bc1bc97b3731de33b3748
1 #!/usr/bin/perl -w
2 # Derive #define directives from specially formatted `case ...:' statements.
3 use strict;
5 use Getopt::Long;
7 (my $VERSION = '$Revision: 1.3 $ ') =~ tr/[0-9].//cd;
8 (my $ME = $0) =~ s|.*/||;
10 END
12 # Nobody ever checks the status of print()s. That's okay, because
13 # if any do fail, we're guaranteed to get an indicator when we close()
14 # the filehandle.
16 # Close stdout now, and if there were no errors, return happy status.
17 # If stdout has already been closed by the script, though, do nothing.
18 defined fileno STDOUT
19 or return;
20 close STDOUT
21 and return;
23 # Errors closing stdout. Indicate that, and hope stderr is OK.
24 warn "$ME: closing standard output: $!\n";
26 # Don't be so arrogant as to assume that we're the first END handler
27 # defined, and thus the last one invoked. There may be others yet
28 # to come. $? will be passed on to them, and to the final _exit().
30 # If it isn't already an error, make it one (and if it _is_ an error,
31 # preserve the value: it might be important).
32 $? ||= 1;
35 sub usage ($)
37 my ($exit_code) = @_;
38 my $STREAM = ($exit_code == 0 ? *STDOUT : *STDERR);
39 if ($exit_code != 0)
41 print $STREAM "Try `$ME --help' for more information.\n";
43 else
45 print $STREAM <<EOF;
46 Usage: $ME [OPTIONS] FILE
48 FIXME: describe
50 OPTIONS:
52 Derive #define directives from specially formatted `case ...:' statements.
54 --help display this help and exit
55 --version output version information and exit
57 EOF
59 exit $exit_code;
63 GetOptions
65 help => sub { usage 0 },
66 version => sub { print "$ME version $VERSION\n"; exit },
67 ) or usage 1;
69 my $fail = 0;
71 @ARGV < 1
72 and (warn "$ME: missing FILE arguments\n"), $fail = 1;
73 1 < @ARGV
74 and (warn "$ME: too many arguments\n"), $fail = 1;
75 $fail
76 and usage 1;
78 my $file = $ARGV[0];
80 open FH, $file
81 or die "$ME: can't open `$file' for reading: $!\n";
83 # For each line like this:
84 # case S_MAGIC_ROMFS: /* 0x7275 */
85 # emit one like this:
86 # # define S_MAGIC_ROMFS 0x7275
87 # Fail if there is a `case S_MAGIC_.*' line without
88 # a properly formed comment.
90 print <<EOF;
91 /* Define the magic numbers as given by statfs(2).
92 Please send additions to meskes\@debian.org.
93 This file is generated automatically from $file. */
95 #if defined __linux__
96 EOF
98 while (defined (my $line = <FH>))
100 $line =~ /^[ \t]+case S_MAGIC_/
101 or next;
102 $line =~ m!^[ \t]+case (S_MAGIC_\w+): /\* (0x[0-9A-Fa-f]+) \*/$!
103 or (warn "$ME:$file:$.: malformed case S_MAGIC_... line"),
104 $fail = 1, next;
105 my $name = $1;
106 my $value = $2;
107 print "# define $name $value\n";
110 print <<\EOF;
111 #elif defined __GNU__
112 # include <hurd/hurd_types.h>
113 #endif
115 close FH;
117 exit $fail;