Batch::Interpreter - interpreter for CMD.EXE batch files
Version 0.01
open my $fh, '<:crlf', $ARGV[0] or die "$ARGV[0]: $!"; my $rc = Batch::Interpreter->new( locale => 'de_DE', # more settings, see below )->run({}, [ <$fh> ], @ARGV);
Create an instance of the class, which can be custimized with the following parameters (values for keys of %settings):
%settings
A Hash with the mapping of emulated filenames to host filenames, the default likely contains the entry NUL => '/dev/null'.
Mapping of emulated mount points (in most cases: drives) to host directories. The entries are scanned in order. Example: 'D:\' => '/mnt/data'.
Mapping of emulated directories to remote names, for prompt $M.
The list of handlers for file extensions. See chapter 'Extension Handlers' for the call interface.
The list of handlers for internal commands, i.e. commands handled by CMD.EXE. See chapter 'Command Handlers' for the call interface.
The list of handlers for external commands, i.e. command line tools. See chapter 'Command Handlers' for the call interface.
The locale, see chapter LOCALES.
State of the ECHO setting, 0 = OFF, 1 = ON.
%ERRORLEVEL%.
State of SETLOCAL ENABLEEXTENSIONS / DISABLEEXTENSIONS, 0 = DISABLE, 1 = ENABLE. .
Extensions are always enabled, but the state variable is maintained correctly.
State of SETLOCAL ENABLEDELAYEDEXPANSION / DISABLEDELAYEDEXPANSION, 0 = DISABLE, 1 = ENABLE.
The values of the environment variables. Due to the case insensitivity all keys are in CAPS, while the real case of the variable names is stored in varcases under the same key.
See vars.
The string for prompt $V and the ver command.
The default drive. Per default a mountpoint for this drive is generated.
The default, if %PATHEXT% is unset.
An optional instance of Term::ReadLine or any other class that supports the ->readline($prompt) method.
Dump parse tree of parsed command before execution.
Print command lines before calling system() for external commands.
Run the interpreter. If given, $lines is an ArrayRef of the lines of the script, else the commands are read from the terminal (as set in the contructor). The arguments of the script can be passed in @arg. Per default the arguments have to be unquoted, like in perl's @ARGV -- in this case a realistically looking quoting is added internally.
$lines
@arg
$attr has the keys
$attr
Set to true, if @arg contains quoted arguments. In this case the values in @arg have to be quoted like under the emulated system. Specifically, they have to look like the return value of the internal function next_token(), i.e. trailing blanks are mandatory for all but the last argument. In this case the arguments can be used as-is.
next_token()
Extension handlers are CodeRefs, that are called with the arguments:
my $ret = $handler->($self, $command, \@arg, $qcommand, \@qarg);
$self is the interpreter instance, $command is the name of the command (i.e. the key the handler was found under in the handler hash), @arg is the array of unquoted command line arguments. $$qcommand and @qarg and $args are the raw forms of $command and @arg, i.e. the quoted command and arguments, which are most likely not needed in the handler. $command is resolved via the %PATH%, while $qcommand is not.
$self
$command
$qcommand
@qarg
$args
%PATH%
The code has to return an empty list (to do nothing) or a list with the name of the interpreter and the arguments that lead to the interpretation of $command (likely including $command itself), which is prepended to @arg and @qarg in the calling code.
Command handlers are CodeRefs, that are called with the arguments:
my $ret = $handler->($self, $command, \@arg, \@qarg, $args);
$self is the interpreter instance, $command is the name of the command (i.e. the key the handler was found under in the handler hash), @arg is the array of unquoted command line arguments. @qarg and $args are the raw forms of @arg, namely the quoted arguments and the unsplit command line, which are most likely not needed in the handler.
The handler can:
If the handler culminates in calling an external program, it will likely be based on the methods $self->unc2sys($path), $self->unc2sys_arg($path) and $self->run_external_command($attr, $exe, @arg), which are documented elsewhere in this document.
Must be 'rewrite'.
Return a new $command to restart the handler search.
The locale class has to implement the following methods:
Format a date for %DATE%, prompt $D, and the date command.
Format a time for the time command.
Format a time for %TIME%, and prompt $T.
Format a file timestamp for the dir command.
Format a file timestamp for for %I in (...) do echo %~tI
Get a localized version of string $key in category $category. The only implemented category is 'message'.
$key
$category
Translates an emulated path into a host path.
Convert path separators from \ to / and apply the filename and mountpoint translation rules, return the result.
Bug: the path is not actually UNC, though that _may_ work through mountpoint translation.
Translates a host path into an emulated path.
Convert path separators from / to \ and apply the filename and mountpoint translation rules, return the result.
Translates an emulated command line argument into a host command line argument.
Paths are found in the argument by crude heuristics, which are subject to change. The actual translation is done with ->unc2sys($arg).
Runs the external program $exe with command line arguments @arg and an optionally changed environment, while optionally translating the input and output text format, and return a result compatible with the Command Handler interface.
$exe
$attr supports the following keys:
Ralf Neubauer, <ralf at strcmp.de>
<ralf at strcmp.de>
Please report any bugs or feature requests to bug-batch-interpreter at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Batch-Interpreter. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
bug-batch-interpreter at rt.cpan.org
You can find documentation for this module with the perldoc command.
perldoc Batch::Interpreter
You can also look for information at:
RT: CPAN's request tracker (report bugs here)
http://rt.cpan.org/NoAuth/Bugs.html?Dist=Batch-Interpreter
AnnoCPAN: Annotated CPAN documentation
http://annocpan.org/dist/Batch-Interpreter
CPAN Ratings
http://cpanratings.perl.org/d/Batch-Interpreter
Search CPAN
http://search.cpan.org/dist/Batch-Interpreter/
Copyright 2016 Ralf Neubauer.
This program is free software; you can redistribute it and/or modify it under the terms of the the Artistic License (2.0). You may obtain a copy of the full license at:
http://www.perlfoundation.org/artistic_license_2_0
Any use, modification, and distribution of the Standard or Modified Versions is governed by this Artistic License. By using, modifying or distributing the Package, you accept this license. Do not use, modify, or distribute the Package, if you do not accept this license.
If your Modified Version has been derived from a Modified Version made by someone other than you, you are nevertheless required to ensure that your Modified Version complies with the requirements of this license.
This license does not grant you the right to use any trademark, service mark, tradename, or logo of the Copyright Holder.
This license includes the non-exclusive, worldwide, free-of-charge patent license to make, have made, use, offer to sell, sell, import and otherwise transfer the Package with respect to any patent claims licensable by the Copyright Holder that are necessarily infringed by the Package. If you institute patent litigation (including a cross-claim or counterclaim) against any party alleging that the Package constitutes direct or contributory patent infringement, then this Artistic License to you shall terminate on the date that such litigation is filed.
Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
To install Batch::Interpreter, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Batch::Interpreter
CPAN shell
perl -MCPAN -e shell install Batch::Interpreter
For more information on module installation, please visit the detailed CPAN module installation guide.