Tizen 2.0 Release
[external/tizen-coreutils.git] / src / extract-magic
1 #!/usr/bin/perl -w
2 # Derive #define directives from specially formatted `case ...:' statements.
3
4 # Copyright (C) 2003, 2005 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 2, or (at your option)
9 # 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, write to the Free Software Foundation,
18 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
20 use strict;
21
22 use Getopt::Long;
23
24 (my $VERSION = '$Revision: 1.5 $ ') =~ tr/[0-9].//cd;
25 (my $ME = $0) =~ s|.*/||;
26
27 END
28 {
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.
32   #
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;
39
40   # Errors closing stdout.  Indicate that, and hope stderr is OK.
41   warn "$ME: closing standard output: $!\n";
42
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().
46   #
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;
50 }
51
52 sub usage ($)
53 {
54   my ($exit_code) = @_;
55   my $STREAM = ($exit_code == 0 ? *STDOUT : *STDERR);
56   if ($exit_code != 0)
57     {
58       print $STREAM "Try `$ME --help' for more information.\n";
59     }
60   else
61     {
62       print $STREAM <<EOF;
63 Usage: $ME [OPTIONS] FILE
64
65 FIXME: describe
66
67 OPTIONS:
68
69   Derive #define directives from specially formatted `case ...:' statements.
70
71    --help             display this help and exit
72    --version          output version information and exit
73
74 EOF
75     }
76   exit $exit_code;
77 }
78
79 {
80   GetOptions
81     (
82      help => sub { usage 0 },
83      version => sub { print "$ME version $VERSION\n"; exit },
84     ) or usage 1;
85
86   my $fail = 0;
87
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;
94
95   my $file = $ARGV[0];
96
97   open FH, $file
98     or die "$ME: can't open `$file' for reading: $!\n";
99
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.
106
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. */
111
112 #if defined __linux__
113 EOF
114
115   while (defined (my $line = <FH>))
116     {
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";
125     }
126
127   print <<\EOF;
128 #elif defined __GNU__
129 # include <hurd/hurd_types.h>
130 #endif
131 EOF
132   close FH;
133
134   exit $fail;
135 }