1#!/usr/bin/perl
2# -*- perl -*-
3
4#   Copyright (C) 2001-2024 Free Software Foundation, Inc.
5#
6# This file is part of the libiberty library.
7# Libiberty is free software; you can redistribute it and/or
8# modify it under the terms of the GNU Library General Public
9# License as published by the Free Software Foundation; either
10# version 2 of the License, or (at your option) any later version.
11#
12# Libiberty is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15# Library General Public License for more details.
16#
17# You should have received a copy of the GNU Library General Public
18# License along with libiberty; see the file COPYING.LIB.  If not,
19# write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
20# Boston, MA 02110-1301, USA.
21#
22# Originally written by DJ Delorie <dj@redhat.com>
23
24
25
26# This program looks for texinfo snippets in source files and other
27# files, and builds per-category files with entries sorted in
28# alphabetical order.
29
30# The syntax it looks for is lines starting with '@def' in *.c and
31# other files (see TEXIFILES in Makefile.in).  Entries are terminated
32# at the next @def* (which begins a new entry) or, for C files, a line
33# that begins with '*/' without leading spaces (this assumes that the
34# texinfo snippet is within a C-style /* */ comment).
35
36#
37
38
39
40if ($ARGV[0] eq "-v") {
41    $verbose = 1;
42    shift;
43}
44
45$srcdir = shift;
46$outfile = shift;
47
48if ($outfile !~ /\S/ || ! -f "$srcdir/Makefile.in" ) {
49    print STDERR "Usage: gather-docs [-v] srcdir outfile.txi [files with snippets in them ...]\n";
50    exit 1;
51}
52
53$errors = 0;
54
55for $in (@ARGV) {
56
57    if (!open(IN, "$srcdir/$in")) {
58          print STDERR "Cannot open $srcdir/$in for reading: $!\n";
59          $errors ++;
60
61    } else {
62          $first = 1;
63          $pertinent = 0;
64          $man_mode = 0;
65          $line = 0;
66
67          while (<IN>) {
68              $line ++;
69              $pertinent = 1 if /^\@def[a-z]*[a-wyz] /;
70              $pertinent = 0 if /^\*\//;
71              next unless $pertinent;
72
73              if (/^\@def[a-z]*[a-wyz] /) {
74
75                    ($name) = m/[^\(]* ([^\( \t\r\n\@]+) *(\(|\@?$)/;
76                    $name =~ s/[         ]*\@?$//;
77                    $key = $name;
78                    $key =~ tr/A-Z/a-z/;
79                    $key =~ s/[^a-z0-9]+/ /g;
80                    $name{$key} = $node;
81                    $lines{$key} = '';
82                    $src_file{$key} = $in;
83                    $src_line{$key} = $line;
84                    print "\nReading $in :" if $verbose && $first;
85                    $first = 0;
86                    print " $name" if $verbose;
87                    $node_lines{$key} .= $_;
88
89              } else {
90                    $node_lines{$key} .= $_;
91              }
92
93              $pertinent = 0 if /^\@end def/;
94          }
95          close (IN);
96    }
97}
98
99print "\n" if $verbose;
100exit $errors if $errors;
101
102if (!open (OUT, "> $outfile")) {
103    print STDERR "Cannot open $outfile for writing: $!\n";
104    $errors ++;
105    next;
106}
107print "Writing $outfile\n" if $verbose;
108
109print OUT "\@c Automatically generated from *.c and others (the comments before\n";
110print OUT "\@c each entry tell you which file and where in that file).  DO NOT EDIT!\n";
111print OUT "\@c Edit the *.c files, configure with --enable-maintainer-mode,\n";
112print OUT "\@c run 'make stamp-functions' and gather-docs will build a new copy.\n\n";
113
114for $key (sort keys %name) {
115    print OUT "\@c $src_file{$key}:$src_line{$key}\n";
116    print OUT $node_lines{$key};
117    print OUT "\n";
118}
119
120if (! print OUT "\n") {
121    print STDERR "Disk full writing $srcdir/$cat.texi\n";
122    $errors ++;
123}
124
125close (OUT);
126
127exit $errors;
128