forked from OpenKore/openkore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.pm
More file actions
62 lines (56 loc) · 1.27 KB
/
Utils.pm
File metadata and controls
62 lines (56 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package Utils;
use strict;
use IPC::Open2;
use POSIX ":sys_wait_h";
##
# checkCommand(file)
# file: an command's filename.
# Returns: the full path to $file, or undef if $file is not a valid command.
#
# Checks whether $file is an executable which is in $PATH or the working directory.
#
# Example:
# checkCommand('gcc'); # Returns "/usr/bin/gcc"
sub checkCommand {
my ($file, $file2) = split / /, $_[0];
$file = $file2 if ($file =~ /ccache/);
return abs_path($file) if (-x $file);
foreach my $dir (split /:+/, $ENV{PATH}) {
if (-x "$dir/$file") {
return "$dir/$file";
}
}
return undef;
}
sub pipeCommand {
my $input = shift;
my ($r, $w);
my $pid = open2($r, $w, @_);
if (!defined $pid) {
return undef;
}
print $w $input;
close $w;
local($/);
my $output = <$r>;
close $r;
waitpid($pid, 0);
return $output;
}
sub syntaxHighlight {
my ($code) = @_;
our $hasSourceHighlight;
if (!defined $hasSourceHighlight) {
$hasSourceHighlight = checkCommand('highlight');
if (!$hasSourceHighlight) {
print STDERR "WARNING: you don't have 'highlight' (http://www.andre-simon.de/) " .
"installed, so syntax highlighting will be disabled.\n";
}
}
if (!$hasSourceHighlight) {
return $code;
} else {
return pipeCommand($code, qw/highlight -S perl -f/);
}
}
1;