1#! @PERL@
2eval 'exec @PERL@ -S $0 ${1+"$@"}'
3    if 0;
4
5#                                                         -*- Perl -*-
6# info2txt -- Convert a Info document to plain text.
7#
8# Copyright (C) 1997  Motoyuki Kasahara
9#
10# This program is free software; you can redistribute it and/or modify
11# it under the terms of the GNU General Public License as published by
12# the Free Software Foundation; either version 2, or (at your option)
13# any later version.
14#
15# This program is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18# GNU General Public License for more details.
19
20eval 'exec @PERL@ -S $0 ${1+"$@"}'
21    if 0;
22
23# Program name, version and mailing address
24$progname ='info2txt';
25$version = '1.2';
26$mailing_address = '@MAILING_ADDRESS@';
27
28$help = "Usage: $progname [option...] [info-file]
29Options:
30  -h, --help                 display this help, then exit
31  -S, --no-split             don\'t read split info-files
32  -v, --version              display version number, then exit
33
34Report bugs to $mailing_address.
35";
36
37$tryhelp = "try \`$0 --help\' for more information\n";
38
39@option_list = ('-h --help     no-argument',
40		'-S --no-split no-argument',
41                '-v --version  no-argument');
42
43$columns = 70;
44
45#
46# Parse command line options.
47#
48$split_mode = 1;
49&getopt_initialize(@option_list);
50while (($opt, $arg) = &getopt_long) {
51    if ($opt eq '-h') {
52        print $help;
53        exit(0);
54    } elsif ($opt eq '-S') {
55	$split_mode = 0;
56    } elsif ($opt eq '-v') {
57	print "$progname version $version\n\n";
58	print "Copyright (c) 1997, 1998  Motoyuki Kasahara\n\n";
59	print "This is free software; you can redistribute it and/or modify\n";
60	print "it under the terms of the GNU General Public License as published
61 by\n";
62	print "the Free Software Foundation; either version 2, or (at your optio
63n)\n";
64	print "any later version.\n\n";
65	print "This program is distributed in the hope that it will be useful,\n
66";
67	print "but WITHOUT ANY WARRANTY; without even the implied warranty\n";
68	print "of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	See the\n";
69	print "GNU General Public License for more details.\n";
70	exit(0);
71    } else {
72        die $tryhelp;
73    }
74}
75
76die "too many arguments\n$tryhelp" if (2 <= @ARGV);
77
78#
79# Search `info-file-1', `info-file-2', ... when `--no-split' and `-S'
80# are not specified.
81#
82if ($split_mode && @ARGV == 1 && -f "$ARGV[0]-1") {
83    $basename = pop(@ARGV);
84    push(@ARGV, "$basename-1");
85    for ($i = 2; -f "$basename-$i"; $i++) {
86	push(@ARGV, "$basename-$i");
87    }
88}
89
90#
91# Main loop.
92#
93($STATUS_TEXT, $STATUS_DIR_ENTRY, $STATUS_NODE, $STATUS_MENU) = (0..4);
94
95while (<>) {
96    if (/Tag Table:/) {
97	last;
98    } elsif (/^START-INFO-DIR-ENTRY/) {
99	$status = $STATUS_DIR_ENTRY;
100	$dir_entry++;
101    } elsif (/^END-INFO-DIR-ENTRY/) {
102	$status = $STATUS_TEXT;
103	print "\n" if ($dir_entry == 1);
104    } elsif (/^\* Menu:/) {
105	$status = $STATUS_MENU;
106    } elsif (/^\037/) {
107	$status = $STATUS_NODE;
108    } elsif ($status == $STATUS_NODE && /Node: Top,/) {
109	$top_node = 1;
110    } elsif ($status == $STATUS_DIR_ENTRY && $dir_entry == 1) {
111	if (($name, $title) = /^\* ([^:]*): \([^\)]*\)\.[ \t]+(.*)$/) {
112	    $is_title = 1;
113	    $namepad = ($columns - length($name)) / 2;
114	    $titlepad = ($columns - length($title)) / 2;
115	    print "\n";
116	    print ' ' x $namepad if (0 < $namepad);
117	    print "$name\n";
118	    print ' ' x $titlepad if (0 < $titlepad);
119	    print "$title\n";
120	} elsif (s/^[ \t]+// && $is_title) {
121	    chop;
122	    $titlepad = ($columns - length($_)) / 2;
123	    print ' ' x $titlepad if (0 < $titlepad);
124	    print "$_\n";
125	} else {
126	    $is_title = 0;
127	}
128    } else {
129	print if ($status == $STATUS_TEXT && $top_node);
130	$status = $STATUS_TEXT if ($status == $STATUS_NODE);
131    }
132}
133
134# Local Variables:
135# mode: perl
136# End:
137