[ci] Test Tcl bindings for dragonfly/freebsd
[xapian.git] / xapian-applications / omega / gen-mimemap
blob19cac6fd6d975bf20133799594cad8cf6d46f818
1 #!/usr/bin/perl
2 use strict;
3 use warnings;
4 my $copyright = <<'EOF';
5 /* Copyright (C) 2012,2013,2015,2016,2017 Olly Betts
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to
9 * deal in the Software without restriction, including without limitation the
10 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
11 * sell copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23 * IN THE SOFTWARE.
25 EOF
27 use Tokeniseise;
29 my $srcdir = shift @ARGV;
31 -d 'docs/inc' or mkdir 'docs/inc' or die $!;
33 my $hdr = Tokeniseise->new('mimemap.h', 'Map extension to MIME Content-Type', $copyright, 'OMEGA_INCLUDED_MIMEMAP_H', 'mime_type', 2);
34 $hdr->append('static const char * const default_mime_map[] = {');
35 my $max_ext_len = 0;
36 my $max_mimetype_len = 0;
37 my %mime_table;
38 while (<STDIN>) {
39 s/#.*//;
40 next if /^\s*$/;
41 my ($ext, $mimetype) = split /\s+/;
42 my $enum = uc $mimetype;
43 $enum =~ s![-+/.]!_!g;
44 $hdr->add($ext, $enum);
45 $max_ext_len = length($ext) if length($ext) > $max_ext_len;
46 if (!exists $mime_table{$mimetype}) {
47 $hdr->append(" \"$mimetype\",");
48 if (length($mimetype) > $max_mimetype_len) {
49 $max_mimetype_len = length($mimetype) if length($mimetype);
51 $mime_table{$mimetype} = [];
53 push @{$mime_table{$mimetype}}, $ext;
55 $hdr->append('};');
56 $hdr->append('');
57 $hdr->append("const size_t MAX_BUILTIN_MIMEMAP_EXTENSION_LEN = $max_ext_len;");
59 $hdr->write();
61 open IGNORED, '>', 'docs/inc/ignored.rst' or die $!;
62 if (exists $mime_table{'ignore'}) {
63 foreach my $ext (sort @{$mime_table{'ignore'}}) {
64 print IGNORED " - $ext\n";
67 close IGNORED or die $!;
69 open MIMETYPES, '>', 'docs/inc/mimetypes.rst' or die $!;
70 sub mimetype_order {
71 my ($a, $b) = @_;
72 $a =~ s!^text/!0/!;
73 $a =~ s!^application/!1/!;
74 $a =~ s!^image/!2/!;
75 $a =~ s!^message/!3/!;
76 $b =~ s!^text/!0/!;
77 $b =~ s!^application/!1/!;
78 $b =~ s!^image/!2/!;
79 $b =~ s!^message/!3/!;
80 return $a cmp $b;
82 my $fmt = "\% -${max_mimetype_len}s \% -${max_ext_len}s\n";
83 my $drule = '='x$max_mimetype_len . ' ' . '='x${max_ext_len} . "\n";
84 my $srule = '-'x$max_mimetype_len . ' ' . '-'x${max_ext_len} . "\n";
85 print MIMETYPES $drule;
86 printf MIMETYPES $fmt, 'MIME Type', 'Extensions';
87 print MIMETYPES $drule;
88 my $first = 1;
89 foreach my $mimetype (sort {mimetype_order($a, $b)} keys %mime_table) {
90 if ($mimetype eq 'ignore') {
91 # Handled above.
92 next;
93 } elsif ($mimetype eq 'skip') {
94 # FIXME: Currently nothing is mapped to this by default.
95 next;
97 if ($first) {
98 $first = 0;
99 } else {
100 print MIMETYPES $srule;
102 my @exts = @{$mime_table{$mimetype}};
103 printf MIMETYPES $fmt, $mimetype, shift @exts;
104 for (@exts) {
105 printf MIMETYPES $fmt, '', $_;
108 print MIMETYPES $drule;
109 close MIMETYPES or die $!;