189857Sobrien#!/usr/bin/perl
289857Sobrien# -*- perl -*-
389857Sobrien
489857Sobrien#   Copyright (C) 2001
589857Sobrien#   Free Software Foundation
689857Sobrien#
789857Sobrien# This file is part of the libiberty library.
889857Sobrien# Libiberty is free software; you can redistribute it and/or
989857Sobrien# modify it under the terms of the GNU Library General Public
1089857Sobrien# License as published by the Free Software Foundation; either
1189857Sobrien# version 2 of the License, or (at your option) any later version.
1289857Sobrien#
1389857Sobrien# Libiberty is distributed in the hope that it will be useful,
1489857Sobrien# but WITHOUT ANY WARRANTY; without even the implied warranty of
1589857Sobrien# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1689857Sobrien# Library General Public License for more details.
1789857Sobrien#
1889857Sobrien# You should have received a copy of the GNU Library General Public
1989857Sobrien# License along with libiberty; see the file COPYING.LIB.  If not,
20218822Sdim# write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
21218822Sdim# Boston, MA 02110-1301, USA.
2289857Sobrien#
2389857Sobrien# Originally written by DJ Delorie <dj@redhat.com>
2489857Sobrien
2589857Sobrien
2689857Sobrien
2789857Sobrien# This program looks for texinfo snippets in source files and other
2889857Sobrien# files, and builds per-category files with entries sorted in
2989857Sobrien# alphabetical order.
3089857Sobrien
3189857Sobrien# The syntax it looks for is lines starting with '@def' in *.c and
3289857Sobrien# other files (see TEXIFILES in Makefile.in).  Entries are terminated
3389857Sobrien# at the next @def* (which begins a new entry) or, for C files, a line
3489857Sobrien# that begins with '*/' without leading spaces (this assumes that the
3589857Sobrien# texinfo snippet is within a C-style /* */ comment).
3689857Sobrien
3789857Sobrien# 
3889857Sobrien
3989857Sobrien
4089857Sobrien
4189857Sobrienif ($ARGV[0] eq "-v") {
4289857Sobrien    $verbose = 1;
4389857Sobrien    shift;
4489857Sobrien}
4589857Sobrien
4689857Sobrien$srcdir = shift;
4789857Sobrien$outfile = shift;
4889857Sobrien
4989857Sobrienif ($outfile !~ /\S/ || ! -f "$srcdir/Makefile.in" ) {
5089857Sobrien    print STDERR "Usage: gather-docs [-v] srcdir outfile.txi [files with snippets in them ...]\n";
5189857Sobrien    exit 1;
5289857Sobrien}
5389857Sobrien
5489857Sobrien$errors = 0;
5589857Sobrien
5689857Sobrienfor $in (@ARGV) {
5789857Sobrien
5889857Sobrien    if (!open(IN, "$srcdir/$in")) {
5989857Sobrien	print STDERR "Cannot open $srcdir/$in for reading: $!\n";
6089857Sobrien	$errors ++;
6189857Sobrien
6289857Sobrien    } else {
6389857Sobrien	$first = 1;
6489857Sobrien	$pertinent = 0;
6589857Sobrien	$man_mode = 0;
6689857Sobrien	$line = 0;
6789857Sobrien
6889857Sobrien	while (<IN>) {
6989857Sobrien	    $line ++;
7089857Sobrien	    $pertinent = 1 if /^\@def[a-z]*[a-wyz] /;
7189857Sobrien	    $pertinent = 0 if /^\*\//;
7289857Sobrien	    next unless $pertinent;
7389857Sobrien
7489857Sobrien	    if (/^\@def[a-z]*[a-wyz] /) {
7589857Sobrien		
7689857Sobrien		($name) = m/[^\(]* ([^\( \t\r\n]+) *\(/;
7789857Sobrien		$name =~ s/[ 	]*$//;
7889857Sobrien		$key = $name;
7989857Sobrien		$key =~ tr/A-Z/a-z/;
8089857Sobrien		$key =~ s/[^a-z0-9]+/ /g;
8189857Sobrien		$name{$key} = $node;
8289857Sobrien		$lines{$key} = '';
8389857Sobrien		$src_file{$key} = $in;
8489857Sobrien		$src_line{$key} = $line;
8589857Sobrien		print "\nReading $in :" if $verbose && $first;
8689857Sobrien		$first = 0;
8789857Sobrien		print " $name" if $verbose;
8889857Sobrien		$node_lines{$key} .= $_;
8989857Sobrien
9089857Sobrien	    } else {
9189857Sobrien		$node_lines{$key} .= $_;
9289857Sobrien	    }
9389857Sobrien
9489857Sobrien	    $pertinent = 0 if /^\@end def/;
9589857Sobrien	}
9689857Sobrien	close (IN);
9789857Sobrien    }
9889857Sobrien}
9989857Sobrien
10089857Sobrienprint "\n" if $verbose;
10189857Sobrienexit $errors if $errors;
10289857Sobrien
10389857Sobrienif (!open (OUT, "> $outfile")) {
10489857Sobrien    print STDERR "Cannot open $outfile for writing: $!\n";
10589857Sobrien    $errors ++;
10689857Sobrien    next;
10789857Sobrien}
10889857Sobrienprint "Writing $outfile\n" if $verbose;
10989857Sobrien
11089857Sobrienprint OUT "\@c Automatically generated from *.c and others (the comments before\n";
11189857Sobrienprint OUT "\@c each entry tell you which file and where in that file).  DO NOT EDIT!\n";
11289857Sobrienprint OUT "\@c Edit the *.c files, configure with --enable-maintainer-mode,\n";
11389857Sobrienprint OUT "\@c and let gather-docs build you a new copy.\n\n";
11489857Sobrien
11589857Sobrienfor $key (sort keys %name) {
11689857Sobrien    print OUT "\@c $src_file{$key}:$src_line{$key}\n";
11789857Sobrien    print OUT $node_lines{$key};
11889857Sobrien    print OUT "\n";
11989857Sobrien}
12089857Sobrien
12189857Sobrienif (! print OUT "\n") {
12289857Sobrien    print STDERR "Disk full writing $srcdir/$cat.texi\n";
12389857Sobrien    $errors ++;
12489857Sobrien}
12589857Sobrien
12689857Sobrienclose (OUT);
12789857Sobrien
12889857Sobrienexit $errors;
129