=head1 NAME

psh - Perl SHell

=for README
Please read the perlpod manpage! Pay particular attention to the markup
sequences (B<>, C<>, I<>, etc.).

=head1 SYNOPSIS

The Perl Shell documentation has been split into a number of different
manpages:

L<psh|psh>         This overview

L<pshdevel|pshdevel>    Developing for the Perl Shell

L<pshconfig|pshconfig>   Configuring the Perl Shell

L<pshcomplete|pshcomplete> TAB completions in the Perl Shell

=head1 DESCRIPTION

B<psh> is a Perl program which executes a read-eval loop with enough
options so that general behavior reasonably similar to more
traditional shells like 'B<sh>' or 'B<bash>' can be achieved, while still
allowing arbitrary perl expressions to be evaluated.

By default within B<psh>, the Perl B<-w> flag and 'C<use strict>' are not
employed so that the user is not bound by their stipulations. 
They can both be turned on via a command-line flag; or setting
C<$^W = 1> will turn on warnings, and calling 'C<use strict>' will
(almost) do the usual thing if called by the user (see LIMITATIONS, below).

Each line of input is read. B<psh> knows a number of possible
strategies for evaluating the line, such as "send it to C<system()> if it
starts with the name of an executable visible in C<$ENV{PATH}>". (See
below for a complete list.)
Each strategy in turn (from a user-definable
list) examines the command line to see if it can apply, and the first
matching strategy evaluates the line. There is a B<psh> configuration
variable (see below) which controls whether the perl value of the
evaluation is saved and printed after each command.

B<psh> automatically collects several lines of input into a unit
processed as a single line if there are unfinished Perl constructs on
the line. In particular, if there is an unmatched quote, paren, brace,
or square bracket, input is read until these characters match. If an
input line contains the Perl "here document" construct as in
C<E<lt>E<lt>XXX>, (anywhere on the line), then input is read and
accumulated until C<XXX> occurs on a line by itself. Then the
accumulated input is processed as if it were a single line.


=head1 OPTIONS

The command-line arguments to B<psh> are:

 psh [-d [options]] [-w] [-F] [-f RC_FILE] [-c STRING ] [FILE1 FILE2 ....]

They are processed in the following order, regardless of what order
they are specified in:

=over 4

=item * B<-w>

Enables Perl's warning mode. The B<-w> switch runs perl with the
B<-w> switch and "use strict;".

=item * B<-d> [debug options]

The B<-d> option puts B<psh> into "debugging" mode, which prints
diagnostic output. Note that you can also enter/leave this
debugging mode in a running B<psh> via the C<$Psh::debugging> variable.

=item * B<-i>

Only for compatibility reasons and ignored by Perl Shell.

=item * B<-f> I<file>

The B<-f> option specifies a file of commands to be read in and
evaluated before processing begins. If it is not set, and
C<$ENV{HOME}> is set, and the file F<$ENV{HOME}/.pshrc> is present, it
will be used. If B<-r> is not specified and the current directory is
different from C<$ENV{HOME}> and it contains a F<.pshrc> file, that
file will be read and executed in addition to
F<$ENV{HOME}/.pshrc>. 

=item * B<-F>

No pshrc files will be read and executed.

=item * B<-c> I<string>

If the B<-c> flag is present, then commands are read from
C<string>, and then B<psh> exits. In particular, any FILE1
... arguments will be ignored.


=back

If any FILE1 ... arguments are specified on the command line, they
will be read and executed and then B<psh> will exit. Otherwise, B<psh>
will enter an interactive command loop.


=head2 TOKENIZATION

Some evaluation strategies examine the "words" of the input. These are
produced by a tokenizer which behaves very similarly to traditional
shells: words are broken at whitespace, '&' is a metacharacter which
means that it always forms its own word, and backslash and double and
single quotes act as quoting characters, preventing word breaks at
whitespace and the "meta-ness" of &.

If the description of the strategy does not mention the "words", then
the tokenization is irrelevant to that strategy.

=head2 STANDARD EVALUATION STRATEGIES

B<psh> includes the following evaluation strategies, sorted by the
default order. For adding/removing evaluation
strategies we suggest the usage of the built-in command "strategy"
from within psh.

=over 4

=item * C<comment>

If the first word of the input line begins with a '#' character, ignore
the line.

=item * C<bang>

If the first word of the input line begins with a '!' character, send
everything after the '!' to system().

=item * C<perl>

If the line begins with 'p!', send the everything after the '!' to the
perl interpreter unchanged.

=item * C<brace>

If the first word of the input line begins with a '{' character,
evaluate the entire line as a Perl expression (including the brace).

=item * C<built_in>

If the first word of the input line matches a B<psh> "built-in"
function, call the subroutine associated with that built-in; the
subroutine receives a single argument, which is the remainder of the
input line exactly as entered.

=item * C<perlfunc>

If the first word of the input line matches the name of a defined
Perl subroutine - or - if $Psh::Strategy::Perlfunc::builtins is set a built-in
Perl function (as determined by the %Psh::Strategy::Perlfunc::perl_builtins hash),
pass the line to eval. If C<$Psh::Strategy::Perlfunc::expand_arguments> is
true and the line
contains no parens, or braces or commas (except for {a,b,c} as in
shell brace-expansion), then this strategy tries to
interpret the arguments on the command line in a "shell-like" manner:
strings are literal except for variable expansion, brace expansion,
and glob expansion. 

The idea of this strategy is to allow perl functions, especially
subroutines in main, to be called like "ordinary commands" (i.e.,
executables on disk). Or put another way, the idea is to replace
bash's "shell function" capacity with ordinary Perl subroutines. The
slogan is, "If the command line looks like an ordinary shell command,
interpret it like one, even if the first word is a Perl subroutine."

=item * C<auto_resume> (not enabled by default)

If the input line matches the name of a stopped job then brings that
job to the foreground instead of starting a new programm with that
name.

=item * C<auto_cd> (not enabled by default)

If the input line matches the name of a directory in the current
directory, then change to that directory.

=item * C<perlscript> (not enabled by default)

If (1) the first word of the input line matches the name of a file found in
one of the directories listed in the path (C<$ENV{PATH}>), and (2) that file
starts with C<#!/.../perl>, and (3) that C<perl> is the
same as the Perl under which B<psh> is running, B<psh> will fork and run
the script using the already-loaded Perl interpreter. The idea is to
save the exec half of the fork-exec that the executable strategy would 
do; typically the exec is more expensive. Right now this strategy can
only handle the B<-w> command-line switch on the C<#!> line. Note this 
strategy only makes sense before the "executable" strategy; if it came 
after, it could never trigger.

=item * C<executable>

If the first word of the input line matches the name of an executable
file in the path given by C<$ENV{PATH}>, then pass the line to
system. Perl variable substitution will be done on the line first if
the C<$Psh::executable_expand_arguments> configuration variable is
true and the binary which is executed does not match one of the
regular expresions in C<@Psh::executable_noexpand>

=item * C<fallback_builtin>

If the first word of the input line is a "fallback builtin" provided
for operating systems that do not have common binaries -- such as
"ls", "env", etc, then call the associated subroutine like an ordinary 
builtin. If you want all of these commands to be executed within the
shell, you can move this strategy ahead of executable.

=item * C<eval>

Pass the line to eval, regardless of any condition. This is a catch-all
strategy; strategies placed after it will never be triggered.

=back

=head2 GLOBBING

Globbing is used to expand filenames against patterns. Perl Shell
understands the sh '*' and '?' globbing characters (where * matches
any string and ? matches exactly one character).

In addition, Perl Shell knows the very powerful '**' globbing,
replacing many C<find>s in your daily work. '**' will be replaced
by 'current directories and all sub directories'. For example:

    grep foo lib/**/*.pm

will search for foo in all *.pm files which are somewhere
(recursivly) within the lib directory.


=head2 REDIRECTS

The standard output may be redirected to a file with

    command > file

and the standard input may be taken from a file with

    command < file

File descriptors other than 0 and 1 may be specified in an rc-like
syntax.

To redirect standard error to a file use:

    command >[2] file

(this is 'command 2> file' in sh-derivatives! sh-syntax is not
supported)

To redirect both, standard output and standard error use:

   command >[all] file

It's also possible to redirect to opened Perl filehandles. If you e.g.
opened a handle "FOO" for writing you may use:

   command >[=FOO]

to write to that filehandle.

=head2 PIPELINES

Pipelines are used to construct processing chains.

    cat a.txt b.txt | wc -l

This is the same as in other shells - standard output of the first
command will be standard input of the second command.

To redirect different file descriptors, use e.g.

    command |[5] command2

to redirect file descriptor 5 to standard input of command.

It is also possible to redirect to a different filedescriptor than
standard input for the right-hand command:

    command |[1=5] command2

will redirect standard output from the first command to a newly opened
stream on file descriptor 5 for command. Thus, 'command | command' is
only a short hand version of 'command |[1=0] command'.

An alias is provided for piping standard error and standard output at
the same time:

    command |[all] command2

will pipe both to command2 and is so an easier to remember version of

    command >[2=1] | command2


=head2 MANIFEST FILTERS

A manifest filter is a chunk of code that causes the creation of a
filter process. They are handy for creating simple one-time filters
because they don't require creating a program file, setting permissions
and so on.

There are three kinds of manifest filters: quick, grep and substitution.

A quick filter consists of a block of code surrounded by curly braces,
with a trailing 'C<q>' modifier. The Perl Shell turns this into a line-by-line
filter. For the code in the braces, C<$_> will contain the line as it was
read from input (including any end-of-line character). The filter block
should

     ls | { print ++$i, ": $_"; }q

A grep filter consists of a block of code surrounded by curly braces,
with a trailing 'C<g>' modifier. The Perl Shell turns this into a line-by-line
filter. Only those lines for which the code in the braces returns a true
value will be printed. For the code in the braces, C<@_> will contain the
results of splitting C<$_> with the pattern C<\s+>.

     netstat | { $_[1]>2; }g

A substitution filter consists of a perl-style s/// operator instance.
The Perl Shell will turn this into a line-by-line filter that performs the
substitution on each line, and then prints the line. For example:

    ls | s/a/b/

A substitution filter is logically equivalent to a block filter containing
the substitution and a statement to print the resulting line. The example
above is equivalent to:

    ls | { s/a/b/; print; }q 


=head2 BUILT-IN FUNCTIONS

A list of built in functions is available from within C<psh> using
the "help" command.

For details about the implementation of built-ins, please see the
B<pshdevel> manpage.

=head2 PSH FUNCTIONS

=over 4

=item C<&Psh::evl>

This function takes a string, evaluates it as if it were a line of
B<psh> input, and returns the value. Useful in loops like:
 
 C<psh$ for $file (glob $pat) { Psh::evl("ls -ld $file"); }>

=item C<&Psh::is_number>

Returns true if its first argument is a number. Intended for use in
filter subroutines placed in $Psh::echo. For example,
C<$Psh::echo = \&Psh::is_number;> will cause only numeric return
values to be printed.

=item C<&Psh::Util::print_debug, print_error, print_out, print_warning>

These four functions are called whenever B<psh> wants to produce B<-d>-mode 
output, error messages, normal output, and warnings,
respectively. They could conceivably be redefined to implement logging 
or similar facilities.

=back

There are other functions in the Psh:: package, but they are probably
not useful except internally to B<psh>.

=head1 LIMITATIONS

Due to limitations of the Win32 type of operating system there's no
job control available on those systems.

The loop inside B<psh> will clobber I<$1> and other Perl-builtin variables 
because it uses matches to implement some of its special
functions.

Right now, job control simply assumes that the POSIX interface is
fully implemented. There should be a way to turn job control off if
this is not the case.

The "exit status" of programs invoked in the foreground by the
"executable" strategy (or even the "bang" strategy) isn't available
from within B<psh>.

Note that since expressions like 'use foo' return undef when sent to
eval(), it is not possible to use that return value as indication of
an error. Instead, we use the heuristic that there was no error unless
the special Perl variable '$@' is non-empty. Note that the side
effects of 'use foo' as a B<psh> command line appear to be exactly as
expected. 

=head1 REQUIREMENTS

psh needs several optional Perl modules to offer full functionality:

=over 4

=item Term::ReadLine::Gnu or Term::ReadLine::Perl for readline support
(command history, special editing chars etc.).

=item Term::Size or Term::ReadKey to offer the ability to change the
environment variables LINES and COLUMNS when the terminal window size
changes while running as standard shell

=item BSD::Resource is necessary for the ulimit builtin

=back


=head1 OTHER PERL SHELLS


=head2 Larry Walls' Perl Shell

Larry Wall exhibits the simple Perl shell C<while (E<lt>E<gt>) { eval; print $@; }> on
page 161 of the Camel Book (2nd Edition).


=head2 lpsh

Lee Eakin E<lt>F<leakin@dfw.nostrum.com>E<gt> has written the Fancy Poor Man's Perl SHell
(called lpsh for Lee's Perl Shell), a simple Perl shell that he has used for a number of
years now (it is derived from Larry Wall's Perl Shell).  He has added some numeric
conversion functions because he often uses it as a calculator.

He has placed it on the web at C<http://www.dfw.nostrum.com/~leakin/psh> (for the
code) and C<http://www.dfw.nostrum.com/~leakin/psh.README> for a short explanation
of the code and a reference to the main Perl Shell site.


=head2 Perl Debugger Shell

Rich Graves E<lt>F<rcgraves@brandeis.edu>E<gt> posted a comment to the original
psh-0.001 announcement on C<http://freshmeat.net>, which contained this
gem that leverages the Perl debugger: C<perl -d -e 1>;


=head2 perlsh

Hiroo Hayashi E<lt>F<hiroo.hayashi@computer.org>E<gt> includes B<perlsh>, a 
``one-line perl evaluator with line editing function and variable name
completion function'' as an example with his Term::ReadLine::Gnu Perl module.


=head2 PSH.pm

In an example of convergent evolution, at C<http://jenda.krynicky.cz/>
there is a Perl shell module called F<PSH.pm> which is quite similar
to this B<psh>. It is designed to provide a command line that can be called
inside some other program via C<PSH::prompt();>, but a small file
F<psh.pl> is also included that uses PSH to provide a standalone
shell. Perhaps some merger of these efforts would be beneficial to all?


=head2 SoftList

Some versions of the Perl faq mention an interactive Perl shell called
SoftList, which can still be found at
C<http://www.mit.edu/afs/sipb/contrib/perl/SoftList/>. It predates
Term::Readline and was apparently last touched in 1993, so it seems to
be obsolescent.


=head2 timtosh

Tim Newsome, E<lt>F<nuisance@cmu.edu>E<gt>, has developed a shell he calls
B<timtosh> (There Is More Than One SHell). Per his web site
(C<http://www.wiw.org/~drz/timtosh>),
it is a shell written entirely in Perl. The goal is to get a shell which you
can extend in Perl and can do some other niceties related to
Perl (like perl re file matching). As of 1999-12-13 (Perl Shell 0.004 release date),
Tim says B<timtosh> ``is focused quite differently than B<psh> is, but is currently
still waiting for a rewrite of the command line parsing.
(It has been for almost a year now)''.


=head2 vbsh

Tom Christiansen and Nathan Torkington's book B<Perl Cookbook>, published by
O'Reilly in 1998 (ISBN 1-56592-243-3) has "Example 15-4. vbsh" on page 531 for
section 15.11 (Editing Input). It stands for Very Bad SHell.


=head2 Comparison of perl shells

As an aid to comparing/contrasting these different shells, here is a
brief table indicating whether or not each has certain features.

  Key to features:
    PE : Perl evaluation of Perl expressions
    SHE: shell-like evaluation of shell-like expressions, including 
         'exec'ing executables searched for in PATH
    CLE: command-line editing
    JC : job control
    PL : pipelines 

  Key to symbols:
    * : feature present
    - : feature absent
    ? : don't know 

  The shells:

=for table
 @<<<<<<<<<<<<<<<<<<<  @|||  @|||  @|||  @|||  @|||

=for table-head

    Shell Name         PE    SHE   CLE    JC    PL

=for table-body

 psh (this one)         *     *     *     *     *            
 Larry Wall shell       *     -     -     -     -
 lpsh                   *     -     *     -     -
 Perl debugger shell    *     -     *     -     -
 perlsh                 *     -     *     -     -
 Krynicky PSH.pm        *     *     ?     -     ?
 SoftList               *     ?     *     ?     ?
 timtosh                -     *     *     *     *
 vbsh                   ?     ?     ?     ?     -

=head1 FILES

F<psh> - The Perl Shell executable script.

F<.pshrc> - The user's Perl Shell `profile'. May be in C<$HOME> or the
current directory; if both are present, both will be read in the order
mentioned.


=head1 COPYRIGHT

Copyright (C) 1999-2003 Gregor N. Purdy. All rights reserved.
This script is free software. It may be copied or modified according
to the same terms as Perl itself.

 E<lt>F<gregor@focusresearch.com>E<gt>

=head1 CREDITS

The following people have contributed to the development of C<psh>:

=over 4

=item Prodigious Contributors

Markus Peter E<lt>F<warp@spin.de>E<gt> added job and signal handling,
globbing, redirection, pipelines, parts of completion code, Win32 port,
i18n code, some bash compatibility builtins and environment variables
and some more minor updates.

Glen Whitney E<lt>F<gwhitney@post.harvard.edu>E<gt> added evaluation
strategies, improved interrupt/job handling, C<&Psh::evl>, C<$Psh::echo>,
more extensive documentation, and other more minor features.

Omer Shenker E<lt>F<oshenker@iname.com>E<gt> added file locking, Win32
code, login shell handling, various bits of documentation, and other minor
features and updates.

Hiroo Hayashi E<lt>F<hiroo.hayashi@computer.org>E<gt> added the current,
bash compatible support for programmable completions and some small fixes.
We also have to thank him for the existence of the Term::ReadLine::Gnu module.

=item ReadLine Support

Code examples showing how to apply the Term::ReadLine package were contributed
by Billy Naylor E<lt>F<billy.naylor@eu.net>E<gt> (in his C<pash.pl> program,
which is his own Perl shell).

=item Symbol Table Dumping

Billy Naylor E<lt>F<billy.naylor@eu.net>E<gt> also had an example of a symbol
table printing function that was used as the starting point for the C<psh>
function C<psh::symbols()>. The C<psh> version adds the ability to specify a
package name, and it also filters out some special variables. The implementation
technique is also different from Billy's.

=item Prompt String Variables

Matthew D. Allen E<lt>F<s2mdalle@titan.vcu.edu>E<gt> contributed an enhanced
prompt string handling routine that emulates the C<bash> prompt variables. This
was expanded into the form now present.

=item Typo Spotting

Allan Kelly E<lt>F<akelly@holyrood.ed.ac.uk>E<gt> found some problems with the
generated documentation.

=back


=cut
