Skip to content

Commit

Permalink
escape kpsewhich arguments on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
xworld21 committed Jan 7, 2024
1 parent 169d04c commit 3a9021e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
4 changes: 3 additions & 1 deletion Makefile.PL
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,11 @@ WriteMakefile(NAME => 'LaTeXML',
'URI' => 0,
'version' => 0,
# Windows terminal handling (see Common::Error)
# Windows argument escaping (see Util::Pathname)
($^O eq 'MSWin32'
? ('Win32::Console' => 0,
'Win32::Console::ANSI' => 0)
'Win32::Console::ANSI' => 0,
'Win32::ShellQuote' => 0)
: ()),
# If we have an "old" version of XML::LibXML,
# we also need XPathContext.
Expand Down
15 changes: 13 additions & 2 deletions lib/LaTeXML/Util/Pathname.pm
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ our @EXPORT = qw( &pathname_find &pathname_findall &pathname_kpsewhich
&pathname_cwd &pathname_chdir &pathname_mkdir &pathname_copy
&pathname_installation);

my $ISWINDOWS;

BEGIN {
$ISWINDOWS = $^O =~ /^(MSWin|NetWare|cygwin)/i;
require Win32::ShellQuote if $ISWINDOWS;
}

# NOTE: For absolute pathnames, the directory component starts with
# whatever File::Spec considers to be the volume, or "/".
#======================================================================
Expand All @@ -54,7 +61,6 @@ our @EXPORT = qw( &pathname_find &pathname_findall &pathname_kpsewhich
### my $SEP = '/'; # [CONSTANT]
# Some indicators that this is not sufficient? (calls to libraries/externals???)
# PRELIMINARY test, probably need to be even more careful
my $ISWINDOWS = $^O =~ /^(MSWin|NetWare|cygwin)/i;
my $SEP = ($ISWINDOWS ? '\\' : '/'); # [CONSTANT]
my $KPATHSEP = ($ISWINDOWS ? ';' : ':'); # [CONSTANT]
my $LITERAL_RE = '(?:literal)(?=:)'; # [CONSTANT]
Expand Down Expand Up @@ -400,7 +406,12 @@ sub pathname_kpsewhich {
# For multiple calls, this is slower in general. But MiKTeX, eg., doesn't use texmf ls-R files!
if ($kpse_toolchain) {
push(@candidates, $kpse_toolchain); }
if ($kpsewhich && open(my $resfh, '-|', $kpsewhich, @candidates)) {
if ($kpsewhich && open(my $resfh, '-|',
# on Windows, ($kpsewhich, @candidates) is joined into a single string, which most binaries
# parse with CommandLineToArgvW, so the arguments must be escaped accordingly
# WARNING: @candidates MUST NOT be empty to guarantee that Perl runs $kpsewhich directly rather
# than through a shell/cmd.exe
$ISWINDOWS ? Win32::ShellQuote::quote_system_list($kpsewhich, @candidates) : ($kpsewhich, @candidates))) {
my $result = <$resfh>; # we only need the first line
{ local $/; <$resfh>; } # discard the rest of the output
close($resfh); # ignore exit status (only one of @candidates exists, usually)
Expand Down

0 comments on commit 3a9021e

Please sign in to comment.