2 # Simple script for substitute placeholders in Texinfo files with
3 # docstring values found in source code.
4 # Copyright (C) 2020-2025 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
24 if ($ARGV[0] eq q{-v}) {
28 my $outDir = $ARGV[0];
29 my @srcDirs = split /:/, $ARGV[1];
30 my @docDirs = split /:/, $ARGV[2];
31 my @exts = (defined $ENV{EXTS}) ? (split / /, $ENV{EXTS}) : qw{c h h.in};
34 foreach my $srcDir (@srcDirs) {
36 print "src: $srcDir\n";
38 opendir my $dir, $srcDir or croak "can not open $srcDir";
39 foreach my $fn (readdir $dir) {
41 if ($fn =~ /[.]$_$/) {
47 open my $src, q{<:encoding(UTF-8)}, "$srcDir/$fn" or croak "can not open $srcDir/$fn";
52 if (defined $curEntry) {
58 if (/^TEXINFO: (.*)$/) {
60 $docstrings{$curEntry} = q{};
62 print "\t$fn: $curEntry\n";
66 if (defined $curEntry) {
67 $docstrings{$curEntry} .= "$_\n";
70 close $src or croak "$!";
75 foreach my $docDir (@docDirs) {
77 print "doc: $docDir\n";
79 opendir my $dir, $docDir or croak "can not open $docDir";
80 if ($docDir !~ /[\/]$/) {
83 if ($docDir eq q{./}) {
86 foreach my $fn (readdir $dir) {
87 if ($fn !~ /[.]texi$/) {
91 open my $src, q{<:encoding(UTF-8)}, $fn or croak "can not open $fn";
92 mkdir "$outDir/$docDir";
93 open my $dst, q{>:encoding(UTF-8)}, "$outDir/$fn" or
94 croak "can not open $outDir/$fn";
97 if (/^\s*\@DOCSTRING (.*)\@$/) {
104 print "\t$fn: $ref\n";
106 if (defined $docstrings{$ref}) {
107 print {$dst} $docstrings{$ref};
109 croak "unable to find docstring: $ref";
112 close $src or croak "$!";
113 close $dst or croak "$!";