~ubuntu-branches/ubuntu/utopic/coreutils/utopic-proposed

« back to all changes in this revision

Viewing changes to src/extract-magic

  • Committer: Package Import Robot
  • Author(s): Colin Watson
  • Date: 2012-11-28 03:03:42 UTC
  • mfrom: (8.3.4 sid)
  • Revision ID: package-import@ubuntu.com-20121128030342-21zanj8354gas5gr
Tags: 8.20-3ubuntu1
* Resynchronise with Debian.  Remaining changes:
  - Make 'uname -i -p' return the real processor/hardware, instead of
    unknown.
  - Build-depend on gettext:any instead of on gettext, so that apt-get can
    properly resolve build-dependencies on the tool when cross-building.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#!/usr/bin/perl -w
2
 
# Derive #define directives from specially formatted `case ...:' statements.
 
2
# Derive #define directives from specially formatted 'case ...:' statements.
3
3
 
4
 
# Copyright (C) 2003, 2005, 2009-2011 Free Software Foundation, Inc.
 
4
# Copyright (C) 2003-2012 Free Software Foundation, Inc.
5
5
 
6
6
# This program is free software: you can redistribute it and/or modify
7
7
# it under the terms of the GNU General Public License as published by
54
54
  my $STREAM = ($exit_code == 0 ? *STDOUT : *STDERR);
55
55
  if ($exit_code != 0)
56
56
    {
57
 
      print $STREAM "Try `$ME --help' for more information.\n";
 
57
      print $STREAM "Try '$ME --help' for more information.\n";
58
58
    }
59
59
  else
60
60
    {
65
65
 
66
66
OPTIONS:
67
67
 
68
 
  Derive #define directives from specially formatted `case ...:' statements.
 
68
  There are two modes of operation, the default, which is to emit
 
69
  #define directives derived from specially formatted 'case' statements,
 
70
  and that with --local, which is to emit a static inline function
 
71
  mapping S_MAGIC_* values to 1, 0, -1, corresponding to known-local,
 
72
  known-remote/distributed/network and unknown, respectively.
69
73
 
70
 
   --help             display this help and exit
71
 
   --version          output version information and exit
 
74
   --local    emit an is_local_fs_type function
 
75
   --help     display this help and exit
 
76
   --version  output version information and exit
72
77
 
73
78
EOF
74
79
    }
76
81
}
77
82
 
78
83
{
 
84
  # The default is to print S_MAGIC_* definitions.
 
85
  my $emit_magic = 1;
 
86
 
79
87
  GetOptions
80
88
    (
 
89
     local => sub { $emit_magic = 0 },
81
90
     help => sub { usage 0 },
82
91
     version => sub { print "$ME version $VERSION\n"; exit },
83
92
    ) or usage 1;
94
103
  my $file = $ARGV[0];
95
104
 
96
105
  open FH, $file
97
 
    or die "$ME: can't open `$file' for reading: $!\n";
 
106
    or die "$ME: can't open '$file' for reading: $!\n";
98
107
 
99
108
  # For each line like this:
100
109
  #   case S_MAGIC_ROMFS: /* 0x7275 */
101
110
  # emit one like this:
102
111
  #   # define S_MAGIC_ROMFS 0x7275
103
 
  # Fail if there is a `case S_MAGIC_.*' line without
 
112
  # Fail if there is a 'case S_MAGIC_.*' line without
104
113
  # a properly formed comment.
105
114
 
106
 
  print <<EOF;
 
115
  my $map_comment = <<EOF;
 
116
/* Map each S_MAGIC_* value to 1, 0 or -1.
 
117
   1 if it is known to be a remote file system type,
 
118
   0 if it is known to be a local file system type, or -1 otherwise.  */
 
119
EOF
 
120
  my $magic_comment = <<EOF;
107
121
/* Define the magic numbers as given by statfs(2).
108
122
   Please send additions to bug-coreutils\@gnu.org and meskes\@debian.org.
109
123
   This file is generated automatically from $file. */
110
 
 
111
 
#if defined __linux__
112
124
EOF
 
125
  print $emit_magic ? $magic_comment : $map_comment;
 
126
 
 
127
  $emit_magic
 
128
    and print "\n#if defined __linux__\n";
 
129
  $emit_magic
 
130
    or print "static inline int\n"
 
131
      . "is_local_fs_type (unsigned long int magic)\n"
 
132
      . "{\n  switch (magic)\n    {\n";
113
133
 
114
134
  while (defined (my $line = <FH>))
115
135
    {
116
136
      $line =~ /^[ \t]+case S_MAGIC_/
117
137
        or next;
118
 
      $line =~ m!^[ \t]+case (S_MAGIC_\w+): /\* (0x[0-9A-Fa-f]+) \*/$!
 
138
      $line =~
 
139
        m!^[ \t]+case (S_MAGIC_\w+): /\* (0x[0-9A-Fa-f]+) (local|remote) \*/!
119
140
        or (warn "$ME:$file:$.: malformed case S_MAGIC_... line"),
120
141
          $fail = 1, next;
121
142
      my $name = $1;
122
 
      my $value = $2;
123
 
      print "# define $name $value\n";
 
143
      my $magic = $2;
 
144
      my $local = $3 eq 'local' ? 1 : 0;
 
145
      print $emit_magic
 
146
        ? "# define $name $magic\n"
 
147
        : "      case $name: return $local;\n";
124
148
    }
125
149
 
126
 
  print <<\EOF;
 
150
  $emit_magic
 
151
    and print <<\EOF;
127
152
#elif defined __GNU__
128
153
# include <hurd/hurd_types.h>
129
154
#endif
130
155
EOF
 
156
  $emit_magic
 
157
    or printf "      default: return -1;\n    }\n}\n";
 
158
 
131
159
  close FH;
132
160
 
133
161
  exit $fail;