| 1 |
package Text::Markup::Cmd; |
| 2 |
|
| 3 |
use strict; |
| 4 |
use warnings; |
| 5 |
use Symbol; |
| 6 |
use IPC::Open3; |
| 7 |
use File::Spec; |
| 8 |
use Carp;use constant WIN32 => $^O eq 'MSWin32'; |
| 9 |
use Exporter 'import'; |
| 10 |
our @EXPORT = qw(find_cmd exec_or_die open_pipe WIN32); |
| 11 |
|
| 12 |
sub find_cmd { |
| 13 |
my ($names, @opts) = @_; |
| 14 |
my $cli; |
| 15 |
EXE: { |
| 16 |
for my $exe (@{ $names }) { |
| 17 |
for my $p (File::Spec->path) { |
| 18 |
my $path = File::Spec->catfile($p, $exe); |
| 19 |
next unless -f $path && -x $path; |
| 20 |
$cli = $path; |
| 21 |
last EXE; |
| 22 |
} |
| 23 |
} |
| 24 |
} |
| 25 |
|
| 26 |
unless ($cli) { |
| 27 |
my $list = join(', ', @{ $names }[0..$#$names-1]) . ", or $names->[-1]"; |
| 28 |
Carp::croak( "Cannot find $list in path $ENV{PATH}" ); |
| 29 |
} |
| 30 |
|
| 31 |
# Make sure it looks like it will work. |
| 32 |
exec_or_die("$cli will not execute", $cli, @opts); |
| 33 |
return $cli; |
| 34 |
} |
| 35 |
|
| 36 |
sub exec_or_die { |
| 37 |
my $err = shift; |
| 38 |
my $output = gensym; |
| 39 |
my $pid = open3(undef, $output, $output, @_); |
| 40 |
waitpid $pid, 0; |
| 41 |
return 1 unless $?; |
| 42 |
use Carp; |
| 43 |
local $/; |
| 44 |
Carp::croak( qq{$err\n}, <$output> ); |
| 45 |
} |
| 46 |
|
| 47 |
# Stolen from SVN::Notify. |
| 48 |
sub open_pipe { |
| 49 |
# Ignored; looks like docutils always emits UTF-8. |
| 50 |
if (WIN32) { |
| 51 |
my $cmd = q{"} . join(q{" "}, @_) . q{"|}; |
| 52 |
open my $fh, $cmd or die "Cannot fork: $!\n"; |
| 53 |
return $fh; |
| 54 |
} |
| 55 |
|
| 56 |
my $pid = open my $fh, '-|'; |
| 57 |
die "Cannot fork: $!\n" unless defined $pid; |
| 58 |
|
| 59 |
if ($pid) { |
| 60 |
# Parent process, return the file handle. |
| 61 |
return $fh; |
| 62 |
} else { |
| 63 |
# Child process. Execute the commands. |
| 64 |
exec @_ or die "Cannot exec $_[0]: $!\n"; |
| 65 |
# Not reached. |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
1; |
| 70 |
__END__ |
| 71 |
|
| 72 |
=head1 Name |
| 73 |
|
| 74 |
Text::Markup::Cmd - Tools for external commands |
| 75 |
|
| 76 |
=head1 Synopsis |
| 77 |
|
| 78 |
use Text::Markup::Cmd; |
| 79 |
my $fh = open_pipe(qw(perl -V)); |
| 80 |
|
| 81 |
=head1 Description |
| 82 |
|
| 83 |
Text::Markup::Cmd provides tools for Text::Markup parsers that depend on |
| 84 |
external commands, such as L<Text::Markup::Rest> and |
| 85 |
L<Text::Markup::AsciiDoctor>. Will mainly be of interest to those |
| 86 |
L<adding a new parser|Text::Markup/Add a Parser> with such a dependency. |
| 87 |
|
| 88 |
=head3 Interface |
| 89 |
|
| 90 |
=head2 Exported Functions |
| 91 |
|
| 92 |
=head3 C<WIN32> |
| 93 |
|
| 94 |
my $exe = 'nerble' . (WIN32 ? '.exe' : ''); |
| 95 |
|
| 96 |
Constant indicating whether the current runtime environment (OS) is Windows. |
| 97 |
|
| 98 |
=head3 C<find_cmd> |
| 99 |
|
| 100 |
my $cmd = find_cmd( |
| 101 |
['nerble' . (WIN32 ? '.exe' : ''), 'nerble.rb'], |
| 102 |
'--version', |
| 103 |
); |
| 104 |
|
| 105 |
Searches the path for one or more named commands. Returns the first command |
| 106 |
to be found in the path and which executes with the specified command line |
| 107 |
options without error. The caller must specify OS-appropriate spellings |
| 108 |
of the commands. |
| 109 |
|
| 110 |
=head3 C<exec_or_die> |
| 111 |
|
| 112 |
exec_or_die( |
| 113 |
qq{Missing required Python "docutils" module}, |
| 114 |
$PYTHON, '-c', 'import docutils', |
| 115 |
); |
| 116 |
|
| 117 |
Executes a command and its arguments. Dies with the error argument if the |
| 118 |
command fails. |
| 119 |
|
| 120 |
=head3 C<open_pipe> |
| 121 |
|
| 122 |
my $fh = open_pipe(qw(nerble --as-html input.nerb)); |
| 123 |
|
| 124 |
Executes a command and its arguments and returns a file handle opened to |
| 125 |
its C<STDOUT>. Dies if the command fails. |
| 126 |
|
| 127 |
=head1 Author |
| 128 |
|
| 129 |
David E. Wheeler <[email protected]> |
| 130 |
|
| 131 |
=head1 Copyright and License |
| 132 |
|
| 133 |
Copyright (c) 2012-2025 David E. Wheeler. Some Rights Reserved. |
| 134 |
|
| 135 |
This module is free software; you can redistribute it and/or modify it under |
| 136 |
the same terms as Perl itself. |
| 137 |
|
| 138 |
=cut |