198b9484cSchristos#!/usr/bin/perl 298b9484cSchristos# -*- perl -*- 398b9484cSchristos 4*5173eb0aSchristos# Copyright (C) 2001-2024 Free Software Foundation, Inc. 598b9484cSchristos# 698b9484cSchristos# This file is part of the libiberty library. 798b9484cSchristos# Libiberty is free software; you can redistribute it and/or 898b9484cSchristos# modify it under the terms of the GNU Library General Public 998b9484cSchristos# License as published by the Free Software Foundation; either 1098b9484cSchristos# version 2 of the License, or (at your option) any later version. 1198b9484cSchristos# 1298b9484cSchristos# Libiberty is distributed in the hope that it will be useful, 1398b9484cSchristos# but WITHOUT ANY WARRANTY; without even the implied warranty of 1498b9484cSchristos# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 1598b9484cSchristos# Library General Public License for more details. 1698b9484cSchristos# 1798b9484cSchristos# You should have received a copy of the GNU Library General Public 1898b9484cSchristos# License along with libiberty; see the file COPYING.LIB. If not, 1998b9484cSchristos# write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor, 2098b9484cSchristos# Boston, MA 02110-1301, USA. 2198b9484cSchristos# 2298b9484cSchristos# Originally written by DJ Delorie <dj@redhat.com> 2398b9484cSchristos 2498b9484cSchristos 2598b9484cSchristos 2698b9484cSchristos# This program looks for texinfo snippets in source files and other 2798b9484cSchristos# files, and builds per-category files with entries sorted in 2898b9484cSchristos# alphabetical order. 2998b9484cSchristos 3098b9484cSchristos# The syntax it looks for is lines starting with '@def' in *.c and 3198b9484cSchristos# other files (see TEXIFILES in Makefile.in). Entries are terminated 3298b9484cSchristos# at the next @def* (which begins a new entry) or, for C files, a line 3398b9484cSchristos# that begins with '*/' without leading spaces (this assumes that the 3498b9484cSchristos# texinfo snippet is within a C-style /* */ comment). 3598b9484cSchristos 3698b9484cSchristos# 3798b9484cSchristos 3898b9484cSchristos 3998b9484cSchristos 4098b9484cSchristosif ($ARGV[0] eq "-v") { 4198b9484cSchristos $verbose = 1; 4298b9484cSchristos shift; 4398b9484cSchristos} 4498b9484cSchristos 4598b9484cSchristos$srcdir = shift; 4698b9484cSchristos$outfile = shift; 4798b9484cSchristos 4898b9484cSchristosif ($outfile !~ /\S/ || ! -f "$srcdir/Makefile.in" ) { 4998b9484cSchristos print STDERR "Usage: gather-docs [-v] srcdir outfile.txi [files with snippets in them ...]\n"; 5098b9484cSchristos exit 1; 5198b9484cSchristos} 5298b9484cSchristos 5398b9484cSchristos$errors = 0; 5498b9484cSchristos 5598b9484cSchristosfor $in (@ARGV) { 5698b9484cSchristos 5798b9484cSchristos if (!open(IN, "$srcdir/$in")) { 5898b9484cSchristos print STDERR "Cannot open $srcdir/$in for reading: $!\n"; 5998b9484cSchristos $errors ++; 6098b9484cSchristos 6198b9484cSchristos } else { 6298b9484cSchristos $first = 1; 6398b9484cSchristos $pertinent = 0; 6498b9484cSchristos $man_mode = 0; 6598b9484cSchristos $line = 0; 6698b9484cSchristos 6798b9484cSchristos while (<IN>) { 6898b9484cSchristos $line ++; 6998b9484cSchristos $pertinent = 1 if /^\@def[a-z]*[a-wyz] /; 7098b9484cSchristos $pertinent = 0 if /^\*\//; 7198b9484cSchristos next unless $pertinent; 7298b9484cSchristos 7398b9484cSchristos if (/^\@def[a-z]*[a-wyz] /) { 7498b9484cSchristos 7598b9484cSchristos ($name) = m/[^\(]* ([^\( \t\r\n\@]+) *(\(|\@?$)/; 7698b9484cSchristos $name =~ s/[ ]*\@?$//; 7798b9484cSchristos $key = $name; 7898b9484cSchristos $key =~ tr/A-Z/a-z/; 7998b9484cSchristos $key =~ s/[^a-z0-9]+/ /g; 8098b9484cSchristos $name{$key} = $node; 8198b9484cSchristos $lines{$key} = ''; 8298b9484cSchristos $src_file{$key} = $in; 8398b9484cSchristos $src_line{$key} = $line; 8498b9484cSchristos print "\nReading $in :" if $verbose && $first; 8598b9484cSchristos $first = 0; 8698b9484cSchristos print " $name" if $verbose; 8798b9484cSchristos $node_lines{$key} .= $_; 8898b9484cSchristos 8998b9484cSchristos } else { 9098b9484cSchristos $node_lines{$key} .= $_; 9198b9484cSchristos } 9298b9484cSchristos 9398b9484cSchristos $pertinent = 0 if /^\@end def/; 9498b9484cSchristos } 9598b9484cSchristos close (IN); 9698b9484cSchristos } 9798b9484cSchristos} 9898b9484cSchristos 9998b9484cSchristosprint "\n" if $verbose; 10098b9484cSchristosexit $errors if $errors; 10198b9484cSchristos 10298b9484cSchristosif (!open (OUT, "> $outfile")) { 10398b9484cSchristos print STDERR "Cannot open $outfile for writing: $!\n"; 10498b9484cSchristos $errors ++; 10598b9484cSchristos next; 10698b9484cSchristos} 10798b9484cSchristosprint "Writing $outfile\n" if $verbose; 10898b9484cSchristos 10998b9484cSchristosprint OUT "\@c Automatically generated from *.c and others (the comments before\n"; 11098b9484cSchristosprint OUT "\@c each entry tell you which file and where in that file). DO NOT EDIT!\n"; 11198b9484cSchristosprint OUT "\@c Edit the *.c files, configure with --enable-maintainer-mode,\n"; 11298b9484cSchristosprint OUT "\@c run 'make stamp-functions' and gather-docs will build a new copy.\n\n"; 11398b9484cSchristos 11498b9484cSchristosfor $key (sort keys %name) { 11598b9484cSchristos print OUT "\@c $src_file{$key}:$src_line{$key}\n"; 11698b9484cSchristos print OUT $node_lines{$key}; 11798b9484cSchristos print OUT "\n"; 11898b9484cSchristos} 11998b9484cSchristos 12098b9484cSchristosif (! print OUT "\n") { 12198b9484cSchristos print STDERR "Disk full writing $srcdir/$cat.texi\n"; 12298b9484cSchristos $errors ++; 12398b9484cSchristos} 12498b9484cSchristos 12598b9484cSchristosclose (OUT); 12698b9484cSchristos 12798b9484cSchristosexit $errors; 128