File: kenny

package info (click to toggle)
filters 2.45
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 240 kB
  • ctags: 40
  • sloc: lex: 1,869; perl: 941; makefile: 76; ansic: 69; sh: 6
file content (384 lines) | stat: -rwxr-xr-x 8,047 bytes parent folder | download | duplicates (7)
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#!/usr/bin/perl -w
#
# kenny.pl -- translate from and to KennySpeak
# 
# $Revision: 1.7 $
#
# Licensed unter the Artistic License:
# http://www.perl.com/language/misc/Artistic.html
#
# (C) 2001,2002 by Christian Garbs <mitch@cgarbs.de>, http://www.cgarbs.de
#                  Alan Eldridge <alane@geeksrus.net>
#
# KennySpeak invented by Kohan Ikin <syneryder@namesuppressed.com>
#                        http://www.namesuppressed.com/kenny/

#
# $Id: kenny.pl,v 1.7 2002/01/06 22:09:52 mitch Exp $
#

use strict;

#
# This is Perl POD documentation.
# You can generate a nice manpage with "pod2man kenny.pl > kenny.man".
# Or have a look at "perldoc perlpod" for other options
#

=head1 NAME

kenny.pl -- translate from and to KennySpeak

=head1 SYNOPSIS

B<kenny.pl>
S<[ B<-h> ]>
S<[ B<-u> ]>
S<[ B<-k> | B<-d> ]>
S<[ I<file1> ] [ I<file2> ] ...>

=head1 OVERVIEW

kenny.pl translates a given text from or to B<KennySpeak>. KennySpeak
looks like this:

    "Ppfmfp ppmffm mfmppfmpm, fmpmfpmppffm'fpmmpp
     pmpmffpmfpmfmppmpm Pmpmppppppppffm!"

=head1 DESCRIPTION

kenny.pl will read the given filenames, translate them and print the
results on stdout. If you don't give a filename, stdin is used. A F<->
as a filename means stdin.

Without any parameters, kenny.pl will look at the first line of input
and guess which way you want to translate. If kenny.pl guesses wrong,
you must select the translation mode using the B<-k> or B<-d> switch.

=head2 Switches

=over 5

=item B<-h>

This will print a short notice and a quick summary of the available
switches.

=item B<-u>

This will convert German umlauts to Ae, Oe, Ue, ae, oe, ue and ss
so that they can be kennyfied correctly.

=item B<-k>

Kennyfy. This forces the input to be translated into KennySpeak.

=item B<-d>

Dekennyfy. This forces the input to be translated back from
KennySpeak.

=back

=head1 VERSION

This is $Revision: 1.7 $.

=head1 BUGS

The B<-u> switch might not work for charsets other than Latin-1. You
might try to convert the kenny.pl script using B<recode> to change it
from Latin-1 to your preferred charset.

=head1 CREDITS

kenny.pl was written by B<Christian Garbs>. Look on his homepage for
newer versions of kenny.pl. Bug reports or comments about the program
should be sent to him:
       Christian Garbs <F<mitch@cgarbs.de>>
       F<http://www.cgarbs.de/weird.en.html>

KennySpeak was invented by B<Kohan Ikin>. See his homepage for an
online version of the original B<KennyTranslator>:
       Kohan Ikin <F<syneryder@namesuppressed.com>>
       F<http://www.namesuppressed.com/kenny/>

Alan Eldridge <F<alane@geeksrus.net>> patched in a signal handler to
print an appropriate message when kenny.pl is killed.

=head1 COPYRIGHT

kenny.pl is licensed unter the B<Artistic License> which can be found
at F<http://www.perl.com/language/misc/Artistic.html>. This license
has been chosen for keeping compatibility with the original KennySpeak
format.

=cut



######################################################################
#
# CHANGELOG
#
# $Log: kenny.pl,v $
# Revision 1.7  2002/01/06 22:09:52  mitch
# Included patch by Alan Eldridge <alane@geeksrus.net>
# to show a message when kenny.pl is killed.
#
# Revision 1.6  2001/07/20 19:04:49  mitch
# Removed warnings with Perl 5.6. 
# Thanks go to Alan Eldridge <alane@geeksrus.net>
#
# Revision 1.5  2001/07/15 10:27:57  mitch
# Included notice about -u and charsets other than Latin-1.
#
# Revision 1.4  2001/07/14 13:16:30  mitch
# Initial release.
#
#
#
######################################################################



#
# finally{}, the source code!
#



##### Declaration of function prototypes

sub generateKenny();
sub generateDeKenny($);
sub guessDialect($);
sub translate($);
sub addGermanUmlauts($);
sub printHelp();
sub theyKilledKenny();



##### Default values for various option:

my $dialect = 0;  # Translate from or to KennySpeak?
                  # 0=guess, 1=encode, 2=decode

my $umlauts = 0;  # Convert German Umlauts before translation?
                  # TRUE= => Ae, Oe, Ue, ae, ou, ue, ss



##### The KennySpeak translation tables:

my $kenny   = generateKenny();          # encoding table
my $dekenny = generateDeKenny($kenny);  # decoding table



##### Install signal handlers

$SIG{HUP} = \&theyKilledKenny;
$SIG{INT} = \&theyKilledKenny;
$SIG{QUIT} = \&theyKilledKenny;
$SIG{TERM} = \&theyKilledKenny;



##### Parse commandline arguments

# "-h" switch (print help):

if (grep /^-h$/, @ARGV) {
    printHelp();
    exit 0;
}

# "-u" switch (convert German umlauts):

if ((defined $ARGV[0]) && ($ARGV[0] eq "-u")) {
    $umlauts = 1;
    shift @ARGV;
} elsif ((defined $ARGV[1]) && ($ARGV[1] eq "-u")) {
    $umlauts = 1;
    splice @ARGV, 1, 1;
}

# "-k" and "-d" switch (force encoding/decoding):

if (defined $ARGV[0]) {
    if ($ARGV[0] eq "-k") {
        $dialect = 1;
        shift @ARGV;
    } elsif ($ARGV[0] eq "-d") {
        $dialect = 2;
        shift @ARGV;
    }
}



##### add German umlauts to encoding if desired:

if ($umlauts) {
    addGermanUmlauts($kenny);
}



##### Process the given input files (or stdin of none given)

while (my $line=<>) {
    chomp $line;
    $dialect = guessDialect($line) unless $dialect;
    $line = translate($line);
    print "$line\n";
}



##### That's all, folks!

exit 0;


##### Signal handler, if we're kill(1)ed

sub theyKilledKenny()
{
    print "Oh my God! They killed Kenny! You bastards!\n";
    exit 0;
}



##### Guess whether input is already kennyfied:

sub guessDialect($)
{
    my $line = shift;
    $line =~ tr/a-zA-Z//cd;
    if (($line =~ tr/mfpMFP//c) > 0) {
        return 1;
    } else {
        return 2;
    }
}



##### Encode/decode a given line

sub translate($)
{
    my $in  = shift;
    my $out = "";
    if ($dialect == 1)
    {
        $out .= exists $kenny->{$1} ? $kenny->{$1} : $1 while ($in =~ s/^(.)//);
    } else {
        my @chars = split //, $in;
        while (@chars) {
            if ((@chars > 2) and (exists $dekenny->{$chars[0]}->{$chars[1]}->{$chars[2]})) {
                $out .= $dekenny->{$chars[0]}->{$chars[1]}->{$chars[2]};
                shift @chars;
                shift @chars;
                shift @chars;
            } else {
                $out .= shift @chars;
            }
        }
    }
    return $out;
}



##### Generate KennySpeak encoding table

sub generateKenny()
{
    my %kenny;

    # lower case characters

    my ($a, $b, $c) = (0,0,0);
    for my $char ("a".."z") {
	my $foo = $a.$b.$c;
	$foo =~ tr/012/mpf/;
	$kenny{$char} = $foo;
	$c++;
	if ($c == 3) {
	    $c=0;
	    $b++;
	    if ($b == 3) {
		$b=0;
		$a++;
	    }
	}
    }

    # upper case characters

    map { $kenny{uc $_} = ucfirst $kenny{$_} } keys %kenny;

    return \%kenny;
}



##### Generate KennySpeak decoding table

sub generateDeKenny($)
{
    my %dekenny;
    my $kenny = $_[0];
    foreach my $key (keys %{$kenny})
    {
	my ($a, $b, $c) = split //, $kenny->{$key};
	if (! exists $dekenny{$a}) {
	    $dekenny{$a} = {};
	}
	if (! exists $dekenny{$a}->{$b}) {
	    $dekenny{$a}->{$b} = {};
	}
	$dekenny{$a}->{$b}->{$c} = $key;
    }

    return \%dekenny;
}



##### Add German Umlaut conversion to KennySpeak encoding table

sub addGermanUmlauts($)
{
    my $kenny = $_[0];
    $kenny->{""} = $kenny->{"a"} . $kenny->{"e"};
    $kenny->{""} = $kenny->{"o"} . $kenny->{"e"};
    $kenny->{""} = $kenny->{"u"} . $kenny->{"e"};
    $kenny->{""} = $kenny->{"A"} . $kenny->{"e"};
    $kenny->{""} = $kenny->{"O"} . $kenny->{"e"};
    $kenny->{""} = $kenny->{"U"} . $kenny->{"e"};
    $kenny->{""} = $kenny->{"s"} x 2;
}



##### Print short help message

sub printHelp()
{
    print <<'EOF';
kenny.pl $Revision: 1.7 $ by Christian Garbs <mitch@cgarbs.de>
Use "pod2man kenny.pl > kenny.man" to generate the man page.
Usage: kenny.pl [ -h ] [ -u ] [ -k | -d ] [ file1 ] [ file2 ] ...
-h : print this help message
-u : convert German umlauts before translation
-k : force encoding to KennySpeak
-d : force decoding from KennySpeak
EOF
    ;
}