diff options
Diffstat (limited to 'src/extract-magic')
-rw-r--r-- | src/extract-magic | 73 |
1 files changed, 50 insertions, 23 deletions
diff --git a/src/extract-magic b/src/extract-magic index 2e9e871..73e6d23 100644 --- a/src/extract-magic +++ b/src/extract-magic @@ -1,12 +1,12 @@ #!/usr/bin/perl -w -# Derive #define directives from specially formatted `case ...:' statements. +# Derive #define directives from specially formatted 'case ...:' statements. -# Copyright (C) 2003, 2005 Free Software Foundation, Inc. +# Copyright (C) 2003-2016 Free Software Foundation, Inc. -# This program is free software; you can redistribute it and/or modify +# This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by -# the Free Software Foundation; either version 2, or (at your option) -# any later version. +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of @@ -14,8 +14,7 @@ # GNU General Public License for more details. # You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software Foundation, -# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +# along with this program. If not, see <http://www.gnu.org/licenses/>. use strict; @@ -55,7 +54,7 @@ sub usage ($) my $STREAM = ($exit_code == 0 ? *STDOUT : *STDERR); if ($exit_code != 0) { - print $STREAM "Try `$ME --help' for more information.\n"; + print $STREAM "Try '$ME --help' for more information.\n"; } else { @@ -66,10 +65,15 @@ FIXME: describe OPTIONS: - Derive #define directives from specially formatted `case ...:' statements. + There are two modes of operation, the default, which is to emit + #define directives derived from specially formatted 'case' statements, + and that with --local, which is to emit a static inline function + mapping S_MAGIC_* values to 1, 0, -1, corresponding to known-local, + known-remote/distributed/network and unknown, respectively. - --help display this help and exit - --version output version information and exit + --local emit an is_local_fs_type function + --help display this help and exit + --version output version information and exit EOF } @@ -77,8 +81,12 @@ EOF } { + # The default is to print S_MAGIC_* definitions. + my $emit_magic = 1; + GetOptions ( + local => sub { $emit_magic = 0 }, help => sub { usage 0 }, version => sub { print "$ME version $VERSION\n"; exit }, ) or usage 1; @@ -95,40 +103,59 @@ EOF my $file = $ARGV[0]; open FH, $file - or die "$ME: can't open `$file' for reading: $!\n"; + or die "$ME: can't open '$file' for reading: $!\n"; # For each line like this: # case S_MAGIC_ROMFS: /* 0x7275 */ # emit one like this: # # define S_MAGIC_ROMFS 0x7275 - # Fail if there is a `case S_MAGIC_.*' line without + # Fail if there is a 'case S_MAGIC_.*' line without # a properly formed comment. - print <<EOF; + my $map_comment = <<EOF; +/* Map each S_MAGIC_* value to 1, 0 or -1. + 1 if it is known to be a remote file system type, + 0 if it is known to be a local file system type, or -1 otherwise. */ +EOF + my $magic_comment = <<EOF; /* Define the magic numbers as given by statfs(2). Please send additions to bug-coreutils\@gnu.org and meskes\@debian.org. This file is generated automatically from $file. */ - -#if defined __linux__ EOF + print $emit_magic ? $magic_comment : $map_comment; + + $emit_magic + and print "\n#if defined __linux__\n"; + $emit_magic + or print "static inline int\n" + . "is_local_fs_type (unsigned long int magic)\n" + . "{\n switch (magic)\n {\n"; while (defined (my $line = <FH>)) { $line =~ /^[ \t]+case S_MAGIC_/ - or next; - $line =~ m!^[ \t]+case (S_MAGIC_\w+): /\* (0x[0-9A-Fa-f]+) \*/$! - or (warn "$ME:$file:$.: malformed case S_MAGIC_... line"), - $fail = 1, next; + or next; + $line =~ + m!^[ \t]+case (S_MAGIC_\w+): /\* (0x[0-9A-Fa-f]+) (local|remote) \*/! + or (warn "$ME:$file:$.: malformed case S_MAGIC_... line"), + $fail = 1, next; my $name = $1; - my $value = $2; - print "# define $name $value\n"; + my $magic = $2; + my $local = $3 eq 'local' ? 1 : 0; + print $emit_magic + ? "# define $name $magic\n" + : " case $name: return $local;\n"; } - print <<\EOF; + $emit_magic + and print <<\EOF; #elif defined __GNU__ # include <hurd/hurd_types.h> #endif EOF + $emit_magic + or printf " default: return -1;\n }\n}\n"; + close FH; exit $fail; |