summaryrefslogtreecommitdiff
path: root/import_tools/ysh.pl
blob: ee27a1e405e459ec15496defd7869a968ea10209 (plain)
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
#!/usr/bin/perl -w

$VERSION = '0.35';
$perl_version = '5.10.0';


=head1 NAME

ysh - The YAML Test Shell

=head1 SYNOPSIS

 ysh [options]

=head1 DESCRIPTION

This program is designed to let you play with the YAML.pm module in
an interactive way. When you to type in Perl, you get back YAML. And
vice versa.

By default, every line you type is a one line Perl program, the return
value of which will be displayed as YAML.

To enter multi-line Perl code start the first line with ';' and use as
many lines as needed. Terminate with a line containing just ';'.

To enter YAML text, start with a valid YAML separator/header line
which is typically '---'. Use '===' to indicate that there is no YAML
header. Enter as many lines as needed. Terminate with a line
containing just '...'.

To read in and process an external YAML file, enter '< filename'. The
ysh will also work as a standalone filter. It will read anything on
STDIN as a YAML stream and write the Perl output to STDOUT. You can say
(on most Unix systems):

    cat yaml.file | ysh | less

=head1 COMMAND LINE OPTIONS

=over 4

=item -l

Keep a log of all ysh activity in './ysh.log'. If the log file already
exists, new content will be concatenated to it.

=item -L

Keep a log of all ysh activity in './ysh.log'. If the log file already
exists, it will be deleted first.

=item -r

Test roundtripping. Every piece of Perl code entered will be Dumped,
Loaded, and Dumped again. If the two stores do not match, an error
message will be reported.

=item -R

Same as above, except that a B<confirmation> message will be printed
when the roundtrip succeeds.

=item -i<number>

Specify the number of characters to indent each level. This is the same
as setting $YAML::Indent.

=item -ub

Shortcut for setting '$YAML::UseBlock = 1'. Force multiline scalars to
use 'block' style.

=item -uf

Shortcut for setting '$YAML::UseFold = 1'. Force multiline scalars to
use 'folded' style.

=item -uc

Shortcut for setting '$YAML::UseCode = 1'. Allows subroutine references
to be processed.

=item -nh

Shortcut for setting '$YAML::UseHeader = 0'.

=item -nv

Shortcut for setting '$YAML::UseVersion = 0'.

=item -v

Print the versions of ysh and YAML.pm.

=item -V

In addition to the -v info, print the versions of YAML related modules.

=item -h

Print a help message.

=back

=head2 YSH_OPT

If you don't want to enter your favorite options every time you enter
ysh, you can put the options into the C<YSH_OPT> environment variable.
Do something like this:

    export YSH_OPT='-i3 -uc -L'

=head1 SEE ALSO

L<YAML>

=head1 AUTHOR

Brian Ingerson <ingy@cpan.org>

=head1 COPYRIGHT

Copyright (c) 2001, 2002. Brian Ingerson. All rights reserved.

This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.

See L<http://www.perl.com/perl/misc/Artistic.html>

=cut

use strict;

use Term::ReadLine;
sub Term::ReadLine::Perl::Tie::FIRSTKEY {undef}
use YAML;
use Data::Dumper;
$Data::Dumper::Indent = 1;
use vars qw($prompt);
$prompt = 'ysh > ';
my $round_trip = 0;
my $force = 0;
my $log = 0;
$| = 1;
my @env_args = split /\s+/, ($ENV{YSH_OPT} || '');
my @args = (@env_args, @ARGV);
my $stream = -t STDIN ? '' : join('', <STDIN>);

while (my $arg = shift @args) {
    handle_help(), exit if $arg eq '-h';
    handle_version(), exit if $arg eq '-v';
    handle_Version(), exit if $arg eq '-V';
    $YAML::Indent = $1, next if $arg =~ /^-i(\d+)$/;
    $YAML::UseFold = 1, next if $arg eq '-uf';
    $YAML::UseBlock = 1, next if $arg eq '-ub';
    $YAML::UseCode = 1, next if $arg eq '-uc';
    $YAML::UseHeader = 0, next if $arg eq '-nh';
    $YAML::UseVersion = 0, next if $arg eq '-nv';
    $round_trip = 1, next if $arg eq '-r';
    $round_trip = 2, next if $arg eq '-R';
    $log = 1, next if $arg eq '-l';
    $log = 2, next if $arg eq '-L';
    $force = 1, next if $arg eq '-F';
    warn(<<END), exit; 
Unknown YAML Shell argument: '$arg'.
For help, try: perldoc ysh
END
}

check_install() unless $force;

if ($log) {
    if ($log == 2) {
        open LOGFILE, "> ./ysh.log" or die $!;
    }
    else {
        open LOGFILE, ">> ./ysh.log" or die $!;
    }
    print LOGFILE "\nYAML.pm Version $YAML::VERSION\n";
    print LOGFILE "Begin logging at ", scalar localtime, "\n\n";
}

sub Print {
    print @_;
    print LOGFILE @_ if $log;
}
local $SIG{__WARN__} = sub { Print @_ };

if (not length($stream)) {
    Print <<END;
Welcome to the YAML Test Shell. Type ':help' for more information.

END
}

{
    my $sh;
    {
        local @ENV{qw(HOME EDITOR)};
        local $^W;
        $sh = Term::ReadLine::->new('The YAML Shell');
    }

    sub my_readline {
        print LOGFILE $prompt if $log;
	my $input = $sh->readline($prompt);
	if (not defined $input) {
	    $input = ':exit';
	    Print "\n";
	}
	$input .= "\n";
    }
}

if (length($stream)) {
    my @objects;
    eval { @objects = YAML::Load($stream) };
    if ($@) {
        print STDERR $@;
    }
    else {
        print STDOUT Data::Dumper::Dumper(@objects);
    }
    exit 0;
}
 
while ($_ = my_readline()) {
    print LOGFILE $_ if $log;
    next if /^\s*$/;
    exec('ysh', @ARGV) if /^\/$/;
    handle_command($_),next if /^:/;
    handle_file($1),next if /^<\s*(\S+)\s*$/;
    handle_yaml($_),next if /^--\S/;
    handle_yaml(''),next if /^===$/;
    handle_perl($_,1),next if /^;/;
    handle_perl($_,0),next;
    Print "Unknown command. Type ':help' for instructions.\n";
}

sub handle_file {
    my ($file) = @_;
    my @objects;
    eval { @objects = YAML::LoadFile($file) };
    if ($@) {
        Print $@;
    }
    else {
        Print Data::Dumper::Dumper(@objects);
    }
}
    
sub handle_perl {
    my ($perl, $multi) = @_;
    my (@objects, $yaml, $yaml2);
    local $prompt = 'perl> ';
    my $line = '';
    if ($multi) {
        while ($line !~ /^;$/) {
            $line = my_readline();
            print LOGFILE $line if $log;
            $perl .= $line;
        }
    }
    @objects = eval "no strict;$perl";
    Print("Bad Perl expression:\n$@"), return if $@;
    eval { $yaml = Dump(@objects) };
    $@ =~ s/^ at.*\Z//sm if $@;
    Print("Dump failed:\n$@"), return if $@;
    Print $yaml;
    if ($round_trip) {
        {
            local $SIG{__WARN__} = sub {};
            eval { $yaml2 = Dump(Load($yaml)) };
        }
        $@ =~ s/^ at.*\Z//sm if $@;
        Print("Load failed:\n$@"), return if $@;
        if ($yaml eq $yaml2) {
            if ($round_trip > 1) {
                Print "\nData roundtripped OK!!!\n";
            }
        }
        else {
            Print "================\n";
            Print "after roundtrip:\n";
            Print "================\n";
            # $yaml2 =~ s/ /_/g;  #
            # $yaml2 =~ s/\n/+/g; #
            # Print $yaml2, "\n"; #
            Print $yaml2;
            Print "=========================\n";
            Print "Data did NOT roundtrip...\n";
        }
    }
}

sub handle_yaml {
    my $yaml = shift;
    my $line = $yaml;
    my (@objects);
    local $prompt = 'yaml> ';
    $line = my_readline();
    print LOGFILE $line if $log;
    $line = '' unless defined $line;
    while ($line !~ /^\.{3}$/) {
        $yaml .= $line;
        $line = my_readline();
        print LOGFILE $line if $log;
        last unless defined $line;
    }
    $yaml =~ s/\^{2,8}/\t/g;
    eval { @objects = Load($yaml) };
    $@ =~ s/^ at.*\Z//sm if $@;
    $@ =~ s/^/  /gm if $@;
    Print("YAML Load Failed:\n$@"), return if $@;
    Print Data::Dumper::Dumper(@objects);
}

sub handle_command {
    my $line = shift;
    chomp $line;
    my ($cmd, $args);
    if ($line =~ /^:(\w+)\s*(.*)$/) {
        $cmd = $1;
        $args = $2;
        exit if $cmd =~ /^(exit|q(uit)?)$/;
        handle_help(),return if $cmd eq 'help';
        print `clear`,return if $cmd =~ /^c(lear)?$/;
    }
    Print "Invalid command\n";
}

sub handle_help {
    Print <<END;
                      Welcome to the YAML Test Shell.

   When you to type in Perl, you get back YAML. And vice versa.

   By default, every line you type is a one line Perl program, the
   return value of which will be displayed as YAML.

   To enter multi-line Perl code start the first line with ';' and use
   as many lines as needed. Terminate with a line containing just ';'.

   To enter YAML text, start with a valid YAML separator/header line
   which is typically '---'. Use '===' to indicate that there is no YAML
   header. Enter as many lines as needed. Terminate with a line
   containing just '...'.

   Shell Commands:             (Begin with ':')
      :exit or :q(uit) - leave the shell
      :help - get this help screen

END
}

sub check_install {
    if (-f "./YAML.pm" && -f "./pm_to_blib" &&
        -M "./YAML.pm" <  -M "./pm_to_blib"
       ) {
        die "You need to 'make install'!\n";
    }
}

sub handle_version {
    print STDERR <<END;

ysh: '$main::VERSION'
YAML: '$YAML::VERSION'

END
}

sub handle_Version {
    my $TRP = get_version('Term::ReadLine::Perl');
    my $TRG = get_version('Term::ReadLine::Gnu');
    my $POE = get_version('POE');
    my $TO = get_version('Time::Object');

    print STDERR <<END;

ysh: '$main::VERSION'
YAML: '$YAML::VERSION'
perl: '$main::perl_version'
Data::Dumper: '$Data::Dumper::VERSION'
Term::ReadLine::Perl: '$TRP'
Term::ReadLine::Gnu: '$TRG'
POE: '$POE'
Time::Object: '$TO'

END
}

sub get_version {
    my ($module) = @_;
    my $version;
    eval "no strict; use $module; \$version = \$${module}::VERSION";
    #$version = "$@" if $@;
    $version = "not installed" if $@;
    return $version;
}

1;