| 1 | #!/usr/bin/perl -w
|
|---|
| 2 | # Derive #define directives from specially formatted `case ...:' statements.
|
|---|
| 3 | use strict;
|
|---|
| 4 |
|
|---|
| 5 | use Getopt::Long;
|
|---|
| 6 |
|
|---|
| 7 | (my $VERSION = '$Revision: 1.4 $ ') =~ tr/[0-9].//cd;
|
|---|
| 8 | (my $ME = $0) =~ s|.*/||;
|
|---|
| 9 |
|
|---|
| 10 | END
|
|---|
| 11 | {
|
|---|
| 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.
|
|---|
| 15 | #
|
|---|
| 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;
|
|---|
| 22 |
|
|---|
| 23 | # Errors closing stdout. Indicate that, and hope stderr is OK.
|
|---|
| 24 | warn "$ME: closing standard output: $!\n";
|
|---|
| 25 |
|
|---|
| 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().
|
|---|
| 29 | #
|
|---|
| 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;
|
|---|
| 33 | }
|
|---|
| 34 |
|
|---|
| 35 | sub usage ($)
|
|---|
| 36 | {
|
|---|
| 37 | my ($exit_code) = @_;
|
|---|
| 38 | my $STREAM = ($exit_code == 0 ? *STDOUT : *STDERR);
|
|---|
| 39 | if ($exit_code != 0)
|
|---|
| 40 | {
|
|---|
| 41 | print $STREAM "Try `$ME --help' for more information.\n";
|
|---|
| 42 | }
|
|---|
| 43 | else
|
|---|
| 44 | {
|
|---|
| 45 | print $STREAM <<EOF;
|
|---|
| 46 | Usage: $ME [OPTIONS] FILE
|
|---|
| 47 |
|
|---|
| 48 | FIXME: describe
|
|---|
| 49 |
|
|---|
| 50 | OPTIONS:
|
|---|
| 51 |
|
|---|
| 52 | Derive #define directives from specially formatted `case ...:' statements.
|
|---|
| 53 |
|
|---|
| 54 | --help display this help and exit
|
|---|
| 55 | --version output version information and exit
|
|---|
| 56 |
|
|---|
| 57 | EOF
|
|---|
| 58 | }
|
|---|
| 59 | exit $exit_code;
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | {
|
|---|
| 63 | GetOptions
|
|---|
| 64 | (
|
|---|
| 65 | help => sub { usage 0 },
|
|---|
| 66 | version => sub { print "$ME version $VERSION\n"; exit },
|
|---|
| 67 | ) or usage 1;
|
|---|
| 68 |
|
|---|
| 69 | my $fail = 0;
|
|---|
| 70 |
|
|---|
| 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;
|
|---|
| 77 |
|
|---|
| 78 | my $file = $ARGV[0];
|
|---|
| 79 |
|
|---|
| 80 | open FH, $file
|
|---|
| 81 | or die "$ME: can't open `$file' for reading: $!\n";
|
|---|
| 82 |
|
|---|
| 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.
|
|---|
| 89 |
|
|---|
| 90 | print <<EOF;
|
|---|
| 91 | /* Define the magic numbers as given by statfs(2).
|
|---|
| 92 | Please send additions to bug-coreutils\@gnu.org and meskes\@debian.org.
|
|---|
| 93 | This file is generated automatically from $file. */
|
|---|
| 94 |
|
|---|
| 95 | #if defined __linux__
|
|---|
| 96 | EOF
|
|---|
| 97 |
|
|---|
| 98 | while (defined (my $line = <FH>))
|
|---|
| 99 | {
|
|---|
| 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";
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | print <<\EOF;
|
|---|
| 111 | #elif defined __GNU__
|
|---|
| 112 | # include <hurd/hurd_types.h>
|
|---|
| 113 | #endif
|
|---|
| 114 | EOF
|
|---|
| 115 | close FH;
|
|---|
| 116 |
|
|---|
| 117 | exit $fail;
|
|---|
| 118 | }
|
|---|