| •1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
## |
| 4 |
## Cowsay 3.03 |
| 5 |
## |
| 6 |
## This file is part of cowsay. (c) 1999-2000 Tony Monroe. |
| 7 |
## |
| 8 |
|
| 9 |
use Text::Tabs qw(expand); |
| 10 |
use Text::Wrap qw(wrap fill $columns); |
| 11 |
use File::Basename; |
| 12 |
use Getopt::Std; |
| 13 |
use Cwd; |
| •14 |
use Text::CharWidth qw(mbswidth); |
| •15 |
|
| 16 |
if (${^UTF8LOCALE}) { |
| 17 |
binmode STDIN, ':utf8'; |
| 18 |
binmode STDOUT, ':utf8'; |
| 19 |
require Encode; |
| 20 |
eval { $_ = Encode::decode_utf8($_,1) } for @ARGV; |
| 21 |
} |
| 22 |
|
| 23 |
$version = "3.03"; |
| 24 |
$progname = basename($0); |
| 25 |
$eyes = "oo"; |
| 26 |
$tongue = " "; |
| •27 |
$cowpath = $ENV{'COWPATH'} || '/usr/share/cowsay/cows'; |
| 28 |
@message = (); |
| 29 |
$thoughts = ""; |
| 30 |
|
| 31 |
## Yeah, this is rude, I know. But hopefully it gets around a nasty |
| 32 |
## little version dependency. |
| 33 |
|
| 34 |
$Text::Wrap::initial_tab = 8; |
| 35 |
$Text::Wrap::subsequent_tab = 8; |
| 36 |
$Text::Wrap::tabstop = 8; |
| 37 |
|
| 38 |
## One of these days, we'll get it ported to Windows. Yeah, right. |
| 39 |
|
| 40 |
if (($^O eq "MSWin32") or ($^O eq "Windows_NT")) { ## Many perls, eek! |
| 41 |
$pathsep = ';'; |
| 42 |
} else { |
| 43 |
$pathsep = ':'; |
| 44 |
} |
| 45 |
|
| 46 |
%opts = ( |
| 47 |
'e' => 'oo', |
| 48 |
'f' => 'default.cow', |
| 49 |
'n' => 0, |
| 50 |
'T' => ' ', |
| 51 |
'W' => 40, |
| 52 |
); |
| 53 |
|
| 54 |
getopts('bde:f:ghlLnNpstT:wW:y', \%opts); |
| 55 |
|
| 56 |
&display_usage if $opts{'h'}; |
| 57 |
&list_cowfiles if $opts{'l'}; |
| 58 |
|
| 59 |
$borg = $opts{'b'}; |
| 60 |
$dead = $opts{'d'}; |
| 61 |
$greedy = $opts{'g'}; |
| 62 |
$paranoid = $opts{'p'}; |
| 63 |
$stoned = $opts{'s'}; |
| 64 |
$tired = $opts{'t'}; |
| 65 |
$wired = $opts{'w'}; |
| 66 |
$young = $opts{'y'}; |
| 67 |
$eyes = substr($opts{'e'}, 0, 2); |
| 68 |
$tongue = substr($opts{'T'}, 0, 2); |
| 69 |
$the_cow = ""; |
| 70 |
|
| 71 |
&slurp_input; |
| 72 |
$Text::Wrap::columns = $opts{'W'}; |
| 73 |
@message = ($opts{'n'} ? expand(@message) : |
| 74 |
split("\n", fill("", "", @message))); |
| 75 |
&construct_balloon; |
| 76 |
&construct_face; |
| 77 |
&get_cow; |
| 78 |
print @balloon_lines; |
| 79 |
print $the_cow; |
| 80 |
|
| 81 |
sub list_cowfiles { |
| 82 |
my $basedir; |
| 83 |
my @dirfiles; |
| 84 |
chop($basedir = cwd); |
| 85 |
for my $d (split(/$pathsep/, $cowpath)) { |
| 86 |
print "Cow files in $d:\n"; |
| 87 |
opendir(COWDIR, $d) || die "$0: Cannot open $d\n"; |
| 88 |
for my $file (readdir COWDIR) { |
| 89 |
if ($file =~ s/\.cow$//) { |
| 90 |
push(@dirfiles, $file); |
| 91 |
} |
| 92 |
} |
| 93 |
closedir(COWDIR); |
| 94 |
print wrap("", "", sort @dirfiles), "\n"; |
| 95 |
@dirfiles = (); |
| 96 |
chdir($basedir); |
| 97 |
} |
| 98 |
exit(0); |
| 99 |
} |
| 100 |
|
| 101 |
sub slurp_input { |
| 102 |
unless ($ARGV[0]) { |
| 103 |
chomp(@message = <STDIN>); |
| 104 |
} else { |
| 105 |
&display_usage if $opts{'n'}; |
| 106 |
@message = join(' ', @ARGV); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
sub maxlength { |
| 111 |
my ($l, $m); |
| 112 |
$m = -1; |
| 113 |
for my $i (@_) { |
| •114 |
# $l = mbswidth $i; |
| 115 |
$l = mbswidth $i =~ s/\e\[\d+(?>(;\d+)*)m//gr; |
| 116 |
$m = $l if ($l > $m); |
| •117 |
} |
| 118 |
## maxlength patch from Jeronimo Pellegrini (Closes: #165218) |
| 119 |
if ($m == -1) { |
| 120 |
$m = 0; |
| 121 |
} |
| 122 |
return $m; |
| 123 |
} |
| 124 |
|
| •125 |
sub colstr { |
| 126 |
(my $str, my $columns) = @_; |
| 127 |
$str . ' ' x ($columns - mbswidth $str) |
| 128 |
} |
| 129 |
|
| 130 |
sub construct_balloon { |
| 131 |
my $max = &maxlength(@message); |
| 132 |
my $max2 = $max + 2; ## border space fudge. |
| •133 |
my $format = "%s %s %s\n"; |
| 134 |
my @border; ## up-left, up-right, down-left, down-right, left, right |
| 135 |
if ($0 =~ /think/i) { |
| 136 |
$thoughts = 'o'; |
| 137 |
@border = qw[ ( ) ( ) ( ) ]; |
| 138 |
} elsif (@message < 2) { |
| 139 |
$thoughts = '\\'; |
| 140 |
@border = qw[ < > ]; |
| 141 |
} else { |
| 142 |
$thoughts = '\\'; |
| 143 |
if ($V and $V gt v5.6.0) { # Thanks, perldelta. |
| 144 |
@border = qw[ / \\ \\ / | | ]; |
| 145 |
} else { |
| 146 |
@border = qw[ / \ \ / | | ]; |
| 147 |
} |
| 148 |
} |
| •149 |
## no trailing spaces (#276144) |
| 150 |
push(@balloon_lines, |
| •151 |
" " . ("_" x $max2) . "\n" , |
| •152 |
sprintf($format, $border[0], colstr($message[0], $max), $border[1]), |
| 153 |
(@message < 2 ? "" : |
| 154 |
map { sprintf($format, $border[4], colstr($_, $max), $border[5]) } |
| 155 |
@message[1 .. $#message - 1]), |
| 156 |
(@message < 2 ? "" : |
| •157 |
sprintf($format, $border[2], colstr($message[$#message], $max), $border[3])), |
| •158 |
" " . ("-" x $max2) . "\n" |
| 159 |
); |
| 160 |
} |
| 161 |
|
| 162 |
sub construct_face { |
| 163 |
if ($borg) { $eyes = "=="; } |
| 164 |
if ($dead) { $eyes = "xx"; $tongue = "U "; } |
| 165 |
if ($greedy) { $eyes = "\$\$"; } |
| 166 |
if ($paranoid) { $eyes = "@@"; } |
| 167 |
if ($stoned) { $eyes = "**"; $tongue = "U "; } |
| 168 |
if ($tired) { $eyes = "--"; } |
| 169 |
if ($wired) { $eyes = "OO"; } |
| 170 |
if ($young) { $eyes = ".."; } |
| 171 |
} |
| 172 |
|
| 173 |
sub get_cow { |
| 174 |
## |
| 175 |
## Get a cow from the specified cowfile; otherwise use the default cow |
| 176 |
## which was defined above in $the_cow. |
| 177 |
## |
| 178 |
my $f = $opts{'f'}; |
| 179 |
my $full = ""; |
| 180 |
if ($opts{'f'} =~ m,/,) { |
| 181 |
$full = $opts{'f'}; |
| 182 |
} else { |
| 183 |
for my $d (split(/:/, $cowpath)) { |
| 184 |
if (-f "$d/$f") { |
| 185 |
$full = "$d/$f"; |
| 186 |
last; |
| 187 |
} elsif (-f "$d/$f.cow") { |
| 188 |
$full = "$d/$f.cow"; |
| 189 |
last; |
| 190 |
} |
| 191 |
} |
| 192 |
if ($full eq "") { |
| 193 |
die "$progname: Could not find $f cowfile!\n"; |
| 194 |
} |
| 195 |
} |
| 196 |
do $full; |
| 197 |
die "$progname: $@\n" if $@; |
| 198 |
} |
| 199 |
|
| 200 |
sub display_usage { |
| 201 |
die <<EOF; |
| 202 |
cow{say,think} version $version, (c) 1999 Tony Monroe |
| 203 |
Usage: $progname [-bdgpstwy] [-h] [-e eyes] [-f cowfile] |
| 204 |
[-l] [-n] [-T tongue] [-W wrapcolumn] [message] |
| 205 |
EOF |
| 206 |
} |