| 1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
use strict; |
| 4 |
use warnings; |
| 5 |
|
| 6 |
use File::Basename; |
| 7 |
|
| 8 |
my %options; |
| 9 |
my $dir; |
| 10 |
my $parts; |
| 11 |
my $formats; |
| 12 |
my $file; |
| 13 |
|
| 14 |
sub expandrw { |
| 15 |
my $read = shift; |
| 16 |
my $write = shift; |
| 17 |
my $type = shift; |
| 18 |
|
| 19 |
my $res = ""; |
| 20 |
if ( ( $read eq 'r' ) || ( $write eq 'w' ) ) { |
| 21 |
$res .= |
| 22 |
" <listitem>\n <para role=\"fmtcapsitem\">\n "; |
| 23 |
if ( ( $read eq 'r' ) && ( $write eq 'w' ) ) { |
| 24 |
$res .= "read and write $type"; |
| 25 |
} |
| 26 |
elsif ( $read eq 'r' ) { |
| 27 |
$res .= "read $type"; |
| 28 |
} |
| 29 |
elsif ( $write eq 'w' ) { |
| 30 |
$res .= "write $type"; |
| 31 |
} |
| 32 |
$res .= "\n </para>\n </listitem>\n"; |
| 33 |
} |
| 34 |
$res; |
| 35 |
} |
| 36 |
|
| 37 |
sub expandoptions { |
| 38 |
my $opts = shift; |
| 39 |
my $res = |
| 40 |
" <para role=\"fmtcapshdr\">\n This format can...\n <itemizedlist>\n"; |
| 41 |
|
| 42 |
$res .= |
| 43 |
expandrw( substr( $opts, 0, 1 ), substr( $opts, 1, 1 ), 'waypoints' ); |
| 44 |
$res .= expandrw( substr( $opts, 2, 1 ), substr( $opts, 3, 1 ), 'tracks' ); |
| 45 |
$res .= expandrw( substr( $opts, 4, 1 ), substr( $opts, 5, 1 ), 'routes' ); |
| 46 |
|
| 47 |
$res .= " </itemizedlist></para>\n"; |
| 48 |
|
| 49 |
$res; |
| 50 |
} |
| 51 |
|
| 52 |
sub expandsuboptions { |
| 53 |
my $f = shift; |
| 54 |
my $res; |
| 55 |
my $olist = $options{$f}; |
| 56 |
|
| 57 |
# If no options, don't clutter things. |
| 58 |
if ( not defined($olist) ) { return; } |
| 59 |
|
| 60 |
# Comma separate the human-readable variation. |
| 61 |
$olist =~ s/> </>, </g; |
| 62 |
|
| 63 |
$res .= "<para>This format has the following options: "; |
| 64 |
$res .= $olist; |
| 65 |
$res .= ".</para>"; |
| 66 |
|
| 67 |
$res; |
| 68 |
} |
| 69 |
|
| 70 |
sub include { |
| 71 |
my $name = shift; |
| 72 |
my $dir2 = shift; |
| 73 |
|
| 74 |
my $name2 = $name; |
| 75 |
$name2 =~ s/-/_/g; |
| 76 |
my $d2 = $dir2; |
| 77 |
$d2 =~ s:/.*::; |
| 78 |
$name2 = $d2 . '_' . $name2; |
| 79 |
|
| 80 |
#print $parts qq(<!ENTITY inc_$name2 SYSTEM "../$dir2/$name.xml">\n); |
| 81 |
#print $file "\&inc_$name2;\n"; |
| 82 |
if ( !-e "$dir/$dir2/$name.xml" ) { |
| 83 |
open my $tmp, '>:encoding(UTF-8)', "$dir/$dir2/$name.xml" or die $!; |
| 84 |
print $tmp "\n"; |
| 85 |
close $tmp; |
| 86 |
} |
| 87 |
open my $tmp2, '<:encoding(UTF-8)', "$dir/$dir2/$name.xml" or die $!; |
| 88 |
print $file qq(<!-- BEGIN inclusion of $dir/$dir2/$name.xml -->\n); |
| 89 |
while (<$tmp2>) { |
| 90 |
print $file $_; |
| 91 |
} |
| 92 |
print $file qq(<!-- END inclusion of $dir/$dir2/$name.xml -->\n); |
| 93 |
close $tmp2; |
| 94 |
} |
| 95 |
|
| 96 |
sub includef { |
| 97 |
my $name = shift; |
| 98 |
|
| 99 |
my $name2 = $name; |
| 100 |
$name2 =~ s/-/_/g; |
| 101 |
print $parts qq(<!ENTITY inc_$name2 SYSTEM "$name.xml">\n); |
| 102 |
print $formats "\&inc_$name2;\n"; |
| 103 |
} |
| 104 |
|
| 105 |
$dir = dirname($0); |
| 106 |
|
| 107 |
qx(mkdir -p $dir/autogen); |
| 108 |
if ( $? != 0 ) { |
| 109 |
die "error creating autogen directory: $?"; |
| 110 |
} |
| 111 |
open $parts, '>:encoding(UTF-8)', "$dir/autogen/_parts.xml" or die $!; |
| 112 |
print $parts qq(<!-- This document is automatically generated. -->\n); |
| 113 |
print $parts qq(<!ENTITY formats SYSTEM "_formats.xml">\n); |
| 114 |
print $parts qq(<!ENTITY filters SYSTEM "_filters.xml">\n); |
| 115 |
|
| 116 |
open $formats, '>:encoding(UTF-8)', "$dir/autogen/_formats.xml" or die $!; |
| 117 |
print $formats qq(<!-- This document is automatically generated. -->\n); |
| 118 |
|
| 119 |
my @formats = qx(./gpsbabel -^3); |
| 120 |
if ( $? != 0 ) { |
| 121 |
die "error running gpsbabel: $?"; |
| 122 |
} |
| 123 |
|
| 124 |
my $going = 0; |
| 125 |
my $dooptions = 0; |
| 126 |
|
| 127 |
# Prescan the argument list for options. |
| 128 |
|
| 129 |
my $fmt; |
| 130 |
my $skipping; |
| 131 |
|
| 132 |
for (@formats) { |
| 133 |
chomp; |
| 134 |
s/\&/\&/g; |
| 135 |
s/</\</g; |
| 136 |
s/>/\>/g; |
| 137 |
my @line = split "\t"; |
| 138 |
next if ( scalar(@line) < 1 ); |
| 139 |
|
| 140 |
if ( ( $line[0] eq 'file' ) || ( $line[0] eq 'serial' ) ) { |
| 141 |
$fmt = $line[2]; |
| 142 |
|
| 143 |
# Pennance for earlier sins. Rename magellan -> magellan1 here. |
| 144 |
if ( $line[4] eq "Magellan serial protocol" ) { |
| 145 |
$fmt = "magellan1"; |
| 146 |
} |
| 147 |
$skipping = 0; |
| 148 |
} |
| 149 |
if ( ( $line[0] eq 'internal' ) |
| 150 |
|| ( defined( $line[5] ) && ( $line[5] eq 'xcsv' ) ) ) |
| 151 |
{ |
| 152 |
$skipping = 1; |
| 153 |
} |
| 154 |
if ( $line[0] eq 'option' && $skipping == 0 ) { |
| 155 |
my $optname = $line[2]; |
| 156 |
|
| 157 |
$options{$fmt} .= |
| 158 |
"<link linkend=\"fmt_${fmt}_o_${optname}\">$optname</link> "; |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
my $id; |
| 163 |
my %fmts; |
| 164 |
|
| 165 |
for (@formats) { |
| 166 |
chomp; |
| 167 |
s/\&/\&/g; |
| 168 |
s/</\</g; |
| 169 |
s/>/\>/g; |
| 170 |
my @line = split "\t"; |
| 171 |
next if ( scalar(@line) < 1 ); |
| 172 |
|
| 173 |
if ( $line[0] eq 'internal' ) { |
| 174 |
if ($going) { |
| 175 |
print $file "</section>\n"; |
| 176 |
close $file; |
| 177 |
$going = 0; |
| 178 |
} |
| 179 |
if ( $line[5] eq 'xcsv' ) { |
| 180 |
$line[0] = 'file'; |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
if ( ( $line[0] eq 'file' ) || ( $line[0] eq 'serial' ) ) { |
| 185 |
if ($going) { |
| 186 |
print $file "</section>\n"; |
| 187 |
close $file; |
| 188 |
} |
| 189 |
$id = $line[2]; |
| 190 |
if ( $fmts{$id} ) { |
| 191 |
$id .= $fmts{$id}++; |
| 192 |
} |
| 193 |
else { |
| 194 |
$fmts{$id} = 1; |
| 195 |
} |
| 196 |
includef( 'fmt_' . $id ); |
| 197 |
open $file, '>:encoding(UTF-8)', "$dir/autogen/fmt_$id.xml" or die $!; |
| 198 |
print $file <<END; |
| 199 |
<!-- This document is automatically generated. --> |
| 200 |
<section xmlns="http://docbook.org/ns/docbook" version="5.0" xml:id="fmt_$id"> |
| 201 |
<title>$line[4] ($line[2])</title> |
| 202 |
END |
| 203 |
print $file expandoptions( $line[1] ); |
| 204 |
print $file expandsuboptions($id); |
| 205 |
$going = 1; |
| 206 |
$dooptions = 1; |
| 207 |
|
| 208 |
if ( defined( $line[5] ) && ( $line[5] ne $line[2] ) ) { |
| 209 |
print $file <<END; |
| 210 |
<para> |
| 211 |
This format is derived from the <link linkend="fmt_$line[5]">$line[5]</link> |
| 212 |
format, so it has all of the same options as that format. |
| 213 |
</para> |
| 214 |
END |
| 215 |
if ( $line[5] eq 'xcsv' ) { |
| 216 |
$dooptions = 0; |
| 217 |
} |
| 218 |
} |
| 219 |
include( $id, "formats" ); |
| 220 |
} |
| 221 |
elsif ( $going && $dooptions && ( $line[0] eq 'option' ) ) { |
| 222 |
my $nid = 'fmt_' . $id . '_o_' . $line[2]; |
| 223 |
print $file <<END; |
| 224 |
<section xml:id="$nid"> |
| 225 |
<title><option>$line[2]</option> option</title> |
| 226 |
<para> |
| 227 |
$line[3]. |
| 228 |
</para> |
| 229 |
END |
| 230 |
include( $id . '-' . $line[2], "formats/options" ); |
| 231 |
print $file <<END; |
| 232 |
</section> |
| 233 |
END |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
if ($going) { |
| 238 |
print $file "</section>\n"; |
| 239 |
close $file; |
| 240 |
$going = 0; |
| 241 |
} |
| 242 |
|
| 243 |
close $formats; |
| 244 |
open $formats, '>:encoding(UTF-8)', "$dir/autogen/_filters.xml" or die $!; |
| 245 |
print $formats qq(<!-- This document is automatically generated. -->\n); |
| 246 |
|
| 247 |
my @filters = qx(./gpsbabel -%1); |
| 248 |
if ( $? != 0 ) { |
| 249 |
die "error running gpsbabel: $?"; |
| 250 |
} |
| 251 |
|
| 252 |
$going = 0; |
| 253 |
|
| 254 |
for (@filters) { |
| 255 |
chomp; |
| 256 |
s/\&/\&/g; |
| 257 |
s/</\</g; |
| 258 |
s/>/\>/g; |
| 259 |
my @line = split "\t"; |
| 260 |
|
| 261 |
if ( $going && ( $line[0] eq 'option' ) ) { |
| 262 |
print $file <<END; |
| 263 |
<section xml:id="flt_$line[1]_o_$line[2]"> |
| 264 |
<title>$line[2] option</title> |
| 265 |
<para> |
| 266 |
$line[3]. |
| 267 |
</para> |
| 268 |
END |
| 269 |
include( $line[1] . '-' . $line[2], "filters/options" ); |
| 270 |
print $file <<END; |
| 271 |
</section> |
| 272 |
END |
| 273 |
} |
| 274 |
else { |
| 275 |
if ($going) { |
| 276 |
print $file "</section>\n"; |
| 277 |
close $file; |
| 278 |
} |
| 279 |
includef( 'filter_' . $line[0] ); |
| 280 |
open $file, '>:encoding(UTF-8)', "$dir/autogen/filter_$line[0].xml" or die $!; |
| 281 |
print $file <<END; |
| 282 |
<!-- This document is automatically generated. --> |
| 283 |
<section xmlns="http://docbook.org/ns/docbook" version="5.0" xml:id="filter_$line[0]"> |
| 284 |
<title>$line[1] ($line[0])</title> |
| 285 |
END |
| 286 |
include( $line[0], "filters" ); |
| 287 |
$going = $line[0]; |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
if ($going) { |
| 292 |
print $file "</section>\n"; |
| 293 |
close $file; |
| 294 |
$going = 0; |
| 295 |
} |
| 296 |
close $formats; |
| 297 |
close $parts; |