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 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
|
#!/usr/bin/perl
=pod
Copyright (c) <2013> <Anthony Persaud> L<http://modernistik.com>
MIT License
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
=cut
use Data::Dumper;
use File::Basename;
$Data::Dumper::Purity = 1;
use vars qw(%_GLOBALS $ans @_HISTORY $VERSION);
$VERSION = 0.5;
_glob_init();
print "\nPerl-Dev [" . ($0) . " - $VERSION]\n\n";
if ( -e $_GLOBALS{cfg} ) {
do "$_GLOBALS{cfg}";
print "Loaded $_GLOBALS{cfg} ok.\n";
}
print "Use 'our' instead of 'my' for persistent variables.\n";
print "Type ':?' for help.\n\n";
if ( scalar @ARGV ) {
$_GLOBALS{input} = join ' ', @ARGV;
print "\t[AutoExe] $_GLOBALS{input}\n";
exec_src();
}
#Main look for the shell
while (1) {
$_GLOBALS{input} = '';
$_GLOBALS{input} = prompt();
if ( $_GLOBALS{debug} ) {
print "Input: $_GLOBALS{input}\n";
}
if ( $_GLOBALS{input} =~ s/^:// ) {
parse_scmd( $_GLOBALS{input} );
}
elsif ( $_GLOBALS{input} =~ s/( )\\ *?$/$1/ ) {
push @{ $_GLOBALS{code} }, $_GLOBALS{input} . "\n";
print "[Appending] " . join( '', @{ $_GLOBALS{code} } ) . "\n"
if ( $_GLOBALS{debug} );
$_GLOBALS{p_type} = 1;
}
else {
exec_src();
$_GLOBALS{p_type} = 0;
}
}
sub _glob_init {
%_GLOBALS = (
input => '',
scmd => '',
p_type => 0,
code => '',
debug => 0,
feed => '',
regex => qr//,
ans => '',
dir => dirname($0),
script => basename($0),
cfg => dirname($0) . '/.pdev'
);
}
sub exec_src {
push @{ $_GLOBALS{code} }, $_GLOBALS{input};
my $_exe = join '', @{ $_GLOBALS{code} };
next if ( $_exe eq '' );
print "\t[EXE] $_exe\n" if ( $_GLOBALS{debug} );
$_exe .= ';' if ( $_exe !~ /;$/ );
push @_HISTORY, $_exe;
my @_return = ( eval $_exe );
$_GLOBALS{ans} = $ans = join ' ', @_return;
print "\n\t[ANS] ", $ans, "\n" if ($ans);
print_err($@);
@{ $_GLOBALS{code} } = ();
$_GLOBALS{scmd} = '';
}
sub print_err {
my @err = @_;
print( "\n\t[ERR] ", @err, "\n" )
if ( join( '', @err ) =~ /\w+/ ); #if errors
}
sub prompt {
my $in = '';
if ( $_GLOBALS{p_type} > 0 ) { print " ..> "; }
#else { print '[{', $_GLOBALS{hist}++, '}> ';}
else { print 'pdev> '; }
chomp( $in = <STDIN> );
return $in;
}
sub parse_scmd {
my $scmd = join '', @_;
my @tokens = split ' ', $scmd;
my $type = lc( shift @tokens );
$_GLOBALS{scmd} = $type;
print "Type: $type\nTokens: ", join( ' ', @tokens ), "\n"
if ( $_GLOBALS{debug} );
if ( $type eq 'dump' ) { _dump( $type, @tokens ); }
elsif ( $type eq 'pop' ) { _pop( $type, @tokens ); }
elsif ( $type =~ /\d+/ ) { _num( $type, @tokens ); }
elsif ( $type eq 'hist' ) { _hist( $type, @tokens ); }
elsif ( $type eq 'run' ) { _run( $type, @tokens ); }
elsif ( $type eq 'feed' ) { _feed( $type, @tokens ); }
elsif ( $type eq 'rgx' ) { _rgx( $type, @tokens ); }
elsif ( $type eq 'rgxtest' ) { _rgxtest( $type, @tokens ); }
elsif ( $type eq 'debug' ) { _debug( $type, @tokens ); }
elsif ( $type eq 'rst' ) { _rst( $type, @tokens ); }
elsif ( $type eq 'output' ) { _output( $type, @tokens ); }
elsif ( $type eq 'sys' ) { _sys( $type, @tokens ); }
elsif ( $type eq 'env' ) { _env( $type, @tokens ); }
elsif ( $type eq 'help' || $type eq '?' ) { _help( $type, @tokens ); }
else { print "Unknown special command: $type\n"; }
}
#------------------------------------------------------------------------------#
# Special Command Functions #
#------------------------------------------------------------------------------#
sub _output {
my $__type = shift;
my $file = shift || 'pdev.output';
if ( open FILE, ">$file" ) {
for my $line (@_HISTORY) {
print "$line\n" if ( $_GLOBALS{debug} );
print FILE $line . "\n";
}
close FILE;
print "[Output Saved] $file\n";
}
else { print "[Err] Could not open output file $file: $!\n"; }
}
sub _env {
my $__type = shift;
my @__tokens = @_;
print "[GLOBALS]\n";
for my $key ( keys %_GLOBALS ) {
printf( "\t%10s: %s\n", $key, $_GLOBALS{$key} );
}
}
sub _sys {
my $__type = shift;
my @__tokens = @_;
my $__sys_cmd = join ' ', @__tokens;
$_GLOBALS{input} = 'qx{' . $__sys_cmd . '}';
if ( $__sys_cmd eq '' ) {
print "[Err] - no system command given $__sys_cmd\n";
}
else { exec_src(); }
}
sub _rst {
%_GLOBALS = ();
$_GLOBALS{ans} = $ans = '';
@_HISTORY = ();
_glob_init();
print "[Env Reset] - ok\n";
}
sub _debug {
my $type = shift;
my $switch = lc(shift);
if ( $switch eq 'off' ) {
$_GLOBALS{debug} = 0;
print "debugging is off\n";
}
elsif ( $switch eq 'on' ) {
$_GLOBALS{debug} = 1;
print "debugging is on\n";
}
else { print "Choose either 'on' or 'off'. ex. :debug off\n"; }
}
sub _dump {
my $__type = shift;
my @__tokens = @_;
for my $__var (@__tokens) {
print "Dump: $__var\n" if ( $_GLOBALS{debug} );
print Data::Dumper->Dump( [ eval("$__var") ], ["$__var"] ) . "\n";
}
}
sub _pop {
my $__type = shift;
my @__tokens = @_;
if ( scalar @__tokens == 0 ) {
@__tokens = ($#_HISTORY);
}
for my $t (@__tokens) {
print "[Removed History] $_HISTORY[$t]\n";
$_HISTORY[$t] = undef;
}
@_HISTORY = grep { $_ ne undef } @_HISTORY;
}
sub _num {
my $__type = shift;
my @__tokens = @_;
$__type = ( $__type + 0 ) || 0;
$_GLOBALS{input} = $_HISTORY[$__type];
if ( $_GLOBALS{input} eq '' ) {
print "\n\t[Err] - no command history at $__type\n";
}
else { exec_src(); }
}
sub _hist {
my $__type = shift;
my @__tokens = @_;
print "[History]\n";
for my $line ( 0 .. $#_HISTORY ) {
print "\t[$line] " . $_HISTORY[$line] . "\n";
}
}
sub _run {
my $__type = shift;
my @__tokens = @_;
print "[Running Feed]\n";
$_GLOBALS{input} = $_GLOBALS{feed};
exec_src();
}
sub _feed {
my $__type = shift;
my @__tokens = @_;
$_GLOBALS{feed} = '';
print "[Feeding] until :end or :run\n";
my $_curr = $_GLOBALS{input} = '';
until ( $_curr =~ /^:end/i || $_curr =~ /^:run/i ) {
$_GLOBALS{input} .= $_curr . "\n";
print "\t[...> ";
chomp( $_curr = <STDIN> );
}
$_GLOBALS{feed} = $_GLOBALS{input};
print "[Feed Stored]\n";
if ( $_curr =~ /^:run/i ) {
_run(); #runs feed automatically
}
}
sub _rgx {
my $__type = shift;
my @__tokens = @_;
my $rgx = join '', @__tokens;
$_GLOBALS{regex} = eval $rgx;
print "[regex] $_GLOBALS{regex}\n";
}
sub _rgxtest {
my $__type = shift;
my @__tokens = @_;
my $__test_str = join '', @__tokens;
print "Regex Stored: $_GLOBALS{regex}\n" if ( $_GLOBALS{debug} );
print "Test String : $__test_str\n" if ( $_GLOBALS{debug} );
if ( $__test_str =~ $_GLOBALS{regex} ) {
print "\n[REGEX MATCH] OK.\n\n";
print "Prematch : $`\n";
print "Match : $&\n";
print "Postmatch : $'\n";
print "Lastmatch : $+\n";
print "[Captured Matches]\n";
for my $m ( 1 .. 9 ) {
print "\t[\$$m] : " . ${$m} . "\n" if ( ${$m} ne '' );
}
}
else {
print "\n[REGEX MATCH] NONE.\n\n";
}
}
sub _help {
my $__type = shift;
my @__tokens = @_;
print q{
[Special Commands]
All special commands must be be prefixed with ':', for example ':help'.
dump - Data::Dump of a variable
:dump $var
debug - turns debugging 'on' or 'off' (use 'on' or 'off' as parameters)
:debug 'on' #turns debuggin on
rgx - store regular expression
:rgx qr/\w+_(\w+)/i
env - prints the current environment variables
rgxtest - tests the regex in rgx against text
:rgxtest my_test
feed - starts storing line-by-line perl code and stores it.
end - use to end the feeding process of feed.
run - executes the code stored by feed.
pop - removes the last code line that has been stored.
If number is given, it removes that entry from the history.
:pop 3 4 #removes entry 3 and 4 from history.
[num] - runs the command in history denoted by num.
:1 #runs command in history 1
hist - lists command history.
rst - resets all internal variables (cleans the shell)
sys - run a system command using ``;
log - outputs history information to file.
:out script.txt
help - this help information
? - same thing as 'help'
[Special Variables]
[Do not use any variables with underscore '__' (double underscore)]
$ans - contains the results of the return value of last operation
pdev> 4+5
[ANS] 9
pdev> $ans + 1
[ANS] 10
@_HISTORY - contains the listing of history commands
%_GLOBALS - contains the global environment variables
[Special Files]
.pdev - if this file is located in the current directory, pdev will
'eval' the file. This is usefule to load regularly used
libraries, variables or configurations.
[Extras]
1. Use 'our' instead of 'my' for persistent variables.
2. You can also auto execute command line strings before entering the shell:
pdev.pl print(5+4);
Perl-Dev [U:\pdev.pl]
[AutoExe] print(5+4)
9
pdev>
Anthony Persaud L<http://modernistik.com>
Copyright (c) <2003-2013> <Anthony Persaud>
MIT License
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
};
}
|