2 # Simple script for substitute placeholders in Texinfo files with
3 # docstring values found in source code.
4 # Copyright (C) 2020-2023 Sergey Matveev (stargrave@stargrave.org)
6 # If you C source code contains:
10 # // last line of docstring
13 # Then under "SomeKey" you will have the whole docstring (starting from
14 # the line after "TEXINFO", till "last line"). You can include its
15 # contents (excluding comment characters) in your .texi files placing
23 if ($ARGV[0] eq "-v") {
27 my $outDir = $ARGV[0];
28 my @srcDirs = split /:/, $ARGV[1];
29 my @docDirs = split /:/, $ARGV[2];
30 my @exts = split / /, (defined $ENV{EXTS}) ? $ENV{EXTS} : "c h h.in";
34 foreach my $srcDir (@srcDirs) {
35 print "src: $srcDir\n" if $verbose;
36 opendir(my $dir, $srcDir) or die "can not open $srcDir";
37 foreach my $fn (readdir $dir) {
38 next unless grep { $fn =~ /\.$_$/ } @exts;
39 open(my $src, "<:encoding(UTF-8)", "$srcDir/$fn") or
40 die "can not open $srcDir/$fn";
45 if (defined $curEntry) {
51 if (/^TEXINFO: (.*)$/) {
53 $docstrings{$curEntry} = "";
54 print "\t$fn: $curEntry\n" if $verbose;
57 ($docstrings{$curEntry} .= "$_\n") if defined $curEntry;
64 foreach my $docDir (@docDirs) {
65 print "doc: $docDir\n" if $verbose;
66 opendir(my $dir, $docDir) or die "can not open $docDir";
67 ($docDir .= "/") unless $docDir =~ /\/$/;
68 ($docDir = "") if $docDir eq "./";
69 foreach my $fn (readdir $dir) {
70 next unless $fn =~ /\.texi$/;
72 open(my $src, "<:encoding(UTF-8)", $fn) or die "can not open $fn";
73 mkdir "$outDir/$docDir";
74 open(my $dst, ">:encoding(UTF-8)", "$outDir/$fn") or
75 die "can not open $outDir/$fn";
77 (print($dst $_) and next) unless /^\s*\@DOCSTRING (.*)\@$/;
78 print "\t$fn: $1\n" if $verbose;
79 die "unable to find docstring: $1" unless defined $docstrings{$1};
80 print $dst $docstrings{$1};