X-Git-Url: http://www.git.stargrave.org/?a=blobdiff_plain;f=bin%2Fbin%2Fdocstringer.pl;h=1b08be3e04dc9a642b96d4f6050ef68ff437a884;hb=HEAD;hp=2de9a0e10331742e336653bac64af3d979dd2926;hpb=88401ec558c64e0110c0de9d0f039573017c80a1;p=dotfiles.git diff --git a/bin/bin/docstringer.pl b/bin/bin/docstringer.pl index 2de9a0e..1e3f1e1 100755 --- a/bin/bin/docstringer.pl +++ b/bin/bin/docstringer.pl @@ -1,7 +1,7 @@ #!/usr/bin/env perl # Simple script for substitute placeholders in Texinfo files with # docstring values found in source code. -# Copyright (C) 2020 Sergey Matveev (stargrave@stargrave.org) +# Copyright (C) 2020-2025 Sergey Matveev # # If you C source code contains: # @@ -18,64 +18,99 @@ use strict; use warnings; +use Carp q{croak}; my $verbose = 0; -if ($ARGV[0] eq "-v") { +if ($ARGV[0] eq q{-v}) { $verbose = 1; shift @ARGV; -}; -my @srcDirs = split / /, $ARGV[0]; -my $docDir = $ARGV[1]; -my $docBuildDir = $ARGV[2]; -my @exts = split / /, defined $ENV{EXTS} ? $ENV{EXTS} : "c h h.in"; - +} +my $outDir = $ARGV[0]; +my @srcDirs = split /:/, $ARGV[1]; +my @docDirs = split /:/, $ARGV[2]; +my @exts = (defined $ENV{EXTS}) ? (split / /, $ENV{EXTS}) : qw{c h h.in}; my %docstrings; foreach my $srcDir (@srcDirs) { - print "src: $srcDir\n" if $verbose; - opendir(my $dir, $srcDir) or die "can not open $srcDir"; + if ($verbose) { + print "src: $srcDir\n"; + } + opendir my $dir, $srcDir or croak "can not open $srcDir"; foreach my $fn (readdir $dir) { - next unless grep { $fn =~ /\.$_$/ } @exts; - open(my $src, "<:encoding(UTF-8)", "$srcDir/$fn") or - die "can not open $srcDir/$fn"; + foreach (@exts) { + if ($fn =~ /[.]$_$/) { + goto ExtIsGood; + } + } + next; + ExtIsGood: + open my $src, q{<:encoding(UTF-8)}, "$srcDir/$fn" or croak "can not open $srcDir/$fn"; my $curEntry; - while(<$src>) { + while (<$src>) { chomp; if (not /^\/\//) { if (defined $curEntry) { undef $curEntry; - }; + } next; - }; - s/^\/\/ ?//; + } + s/^\/\/[ ]?//; if (/^TEXINFO: (.*)$/) { $curEntry = $1; - $docstrings{$curEntry} = ""; - print "\t$fn: $curEntry\n" if $verbose; + $docstrings{$curEntry} = q{}; + if ($verbose) { + print "\t$fn: $curEntry\n"; + } next; - }; - ( $docstrings{$curEntry} .= "$_\n" ) if defined $curEntry; - }; - close $src; - }; + } + if (defined $curEntry) { + $docstrings{$curEntry} .= "$_\n"; + } + } + close $src or croak "$!"; + } closedir $dir; -}; +} -print "doc: $docDir\n" if $verbose; -opendir(my $dir, $docDir) or die "can not open $docDir"; -foreach my $fn (readdir $dir) { - next unless $fn =~ /\.texi$/; - open(my $src, "<:encoding(UTF-8)", "$docDir/$fn") or - die "can not open $docDir/$fn"; - open(my $dst, ">:encoding(UTF-8)", "$docBuildDir/$fn") or - die "can not open $docBuildDir/$fn"; - while(<$src>) { - ( print($dst $_) and next ) unless /^\s*\@DOCSTRING (.*)\@$/; - print "\t$fn: $1\n" if $verbose; - die "unable to find docstring: $1" unless defined $docstrings{$1}; - print $dst $docstrings{$1}; - }; - close $src; - close $dst; -}; -closedir $dir; +foreach my $docDir (@docDirs) { + if ($verbose) { + print "doc: $docDir\n"; + } + opendir my $dir, $docDir or croak "can not open $docDir"; + if ($docDir !~ /[\/]$/) { + $docDir .= q{/}; + } + if ($docDir eq q{./}) { + $docDir = q{}; + } + foreach my $fn (readdir $dir) { + if ($fn !~ /[.]texi$/) { + next; + } + $fn = $docDir . $fn; + open my $src, q{<:encoding(UTF-8)}, $fn or croak "can not open $fn"; + mkdir "$outDir/$docDir"; + open my $dst, q{>:encoding(UTF-8)}, "$outDir/$fn" or + croak "can not open $outDir/$fn"; + my $ref; + while (<$src>) { + if (/^\s*\@DOCSTRING (.*)\@$/) { + $ref = $1; + } else { + print {$dst} $_; + next; + } + if ($verbose) { + print "\t$fn: $ref\n"; + } + if (defined $docstrings{$ref}) { + print {$dst} $docstrings{$ref}; + } else { + croak "unable to find docstring: $ref"; + } + } + close $src or croak "$!"; + close $dst or croak "$!"; + } + closedir $dir; +}