1139825Simp#!/usr/bin/perl
226156Sse# -*- perl -*-
326156Sse
426156Sse#   Copyright (C) 2001, 2009, 2011
526156Sse#   Free Software Foundation
626156Sse#
726156Sse# This file is part of the libiberty library.
826156Sse# Libiberty is free software; you can redistribute it and/or
926156Sse# modify it under the terms of the GNU Library General Public
1026156Sse# License as published by the Free Software Foundation; either
1126156Sse# version 2 of the License, or (at your option) any later version.
1226156Sse#
1326156Sse# Libiberty is distributed in the hope that it will be useful,
1426156Sse# but WITHOUT ANY WARRANTY; without even the implied warranty of
1526156Sse# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1626156Sse# Library General Public License for more details.
1726156Sse#
1826156Sse# You should have received a copy of the GNU Library General Public
1926156Sse# License along with libiberty; see the file COPYING.LIB.  If not,
2026156Sse# write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
2126156Sse# Boston, MA 02110-1301, USA.
2226156Sse#
2326156Sse# Originally written by DJ Delorie <dj@redhat.com>
2426156Sse
2526156Sse
2650477Speter
2726156Sse# This program looks for texinfo snippets in source files and other
2826156Sse# files, and builds per-category files with entries sorted in
2945897Speter# alphabetical order.
3045897Speter
3126380Sdfr# The syntax it looks for is lines starting with '@def' in *.c and
3276771Sjhb# other files (see TEXIFILES in Makefile.in).  Entries are terminated
3376771Sjhb# at the next @def* (which begins a new entry) or, for C files, a line
3476771Sjhb# that begins with '*/' without leading spaces (this assumes that the
35151658Sjhb# texinfo snippet is within a C-style /* */ comment).
36151658Sjhb
37166901Spiso# 
38151658Sjhb
3965822Sjhb
4065822Sjhb
4165822Sjhbif ($ARGV[0] eq "-v") {
42151658Sjhb    $verbose = 1;
4372237Sjhb    shift;
4465822Sjhb}
45151658Sjhb
46166901Spiso$srcdir = shift;
4772237Sjhb$outfile = shift;
4872237Sjhb
4972237Sjhbif ($outfile !~ /\S/ || ! -f "$srcdir/Makefile.in" ) {
5072237Sjhb    print STDERR "Usage: gather-docs [-v] srcdir outfile.txi [files with snippets in them ...]\n";
51151658Sjhb    exit 1;
5272237Sjhb}
53151658Sjhb
5472237Sjhb$errors = 0;
55169320Spiso
5672237Sjhbfor $in (@ARGV) {
5765822Sjhb
5872237Sjhb    if (!open(IN, "$srcdir/$in")) {
5972237Sjhb	print STDERR "Cannot open $srcdir/$in for reading: $!\n";
6072237Sjhb	$errors ++;
6172839Sjhb
6272237Sjhb    } else {
6372237Sjhb	$first = 1;
6472237Sjhb	$pertinent = 0;
65151658Sjhb	$man_mode = 0;
66177940Sjhb	$line = 0;
67177940Sjhb
68177940Sjhb	while (<IN>) {
69177940Sjhb	    $line ++;
70177940Sjhb	    $pertinent = 1 if /^\@def[a-z]*[a-wyz] /;
71177940Sjhb	    $pertinent = 0 if /^\*\//;
72177940Sjhb	    next unless $pertinent;
73177940Sjhb
74177940Sjhb	    if (/^\@def[a-z]*[a-wyz] /) {
75177940Sjhb		
76177940Sjhb		($name) = m/[^\(]* ([^\( \t\r\n\@]+) *(\(|\@?$)/;
77177940Sjhb		$name =~ s/[	 ]*\@?$//;
78177940Sjhb		$key = $name;
79177940Sjhb		$key =~ tr/A-Z/a-z/;
80177940Sjhb		$key =~ s/[^a-z0-9]+/ /g;
81177940Sjhb		$name{$key} = $node;
82177940Sjhb		$lines{$key} = '';
83177940Sjhb		$src_file{$key} = $in;
84177940Sjhb		$src_line{$key} = $line;
85177940Sjhb		print "\nReading $in :" if $verbose && $first;
86177940Sjhb		$first = 0;
87177940Sjhb		print " $name" if $verbose;
88177940Sjhb		$node_lines{$key} .= $_;
89177940Sjhb
90177940Sjhb	    } else {
91177940Sjhb		$node_lines{$key} .= $_;
92177940Sjhb	    }
93183052Sjhb
94183052Sjhb	    $pertinent = 0 if /^\@end def/;
95183052Sjhb	}
96183052Sjhb	close (IN);
97183052Sjhb    }
98183052Sjhb}
99183052Sjhb
100183052Sjhbprint "\n" if $verbose;
101183052Sjhbexit $errors if $errors;
102183052Sjhb
10372237Sjhbif (!open (OUT, "> $outfile")) {
104151658Sjhb    print STDERR "Cannot open $outfile for writing: $!\n";
105151658Sjhb    $errors ++;
106151658Sjhb    next;
107151658Sjhb}
108151658Sjhbprint "Writing $outfile\n" if $verbose;
109151658Sjhb
110151658Sjhbprint OUT "\@c Automatically generated from *.c and others (the comments before\n";
111151658Sjhbprint OUT "\@c each entry tell you which file and where in that file).  DO NOT EDIT!\n";
112177940Sjhbprint OUT "\@c Edit the *.c files, configure with --enable-maintainer-mode,\n";
113177940Sjhbprint OUT "\@c run 'make stamp-functions' and gather-docs will build a new copy.\n\n";
114177940Sjhb
115177181Sjhbfor $key (sort keys %name) {
116151658Sjhb    print OUT "\@c $src_file{$key}:$src_line{$key}\n";
117151658Sjhb    print OUT $node_lines{$key};
118168850Snjl    print OUT "\n";
119168850Snjl}
120178092Sjeff
121177181Sjhbif (! print OUT "\n") {
12265822Sjhb    print STDERR "Disk full writing $srcdir/$cat.texi\n";
12365822Sjhb    $errors ++;
124151658Sjhb}
125151658Sjhb
126151658Sjhbclose (OUT);
127151658Sjhb
12872237Sjhbexit $errors;
12972237Sjhb