adsh - Adventure Shell (Part 01/01)

Deadly Dunc dunc at cs.ubc.ca
Sun Nov 19 09:20:43 AEST 1989


#! /bin/sh
# This is a shell archive, meaning:
# 1. Remove everything above the #! /bin/sh line.
# 2. Save the resulting text in a file.
# 3. Execute the file with /bin/sh (not csh) to create the files:
#	README
#	adsh
# This archive created: Sat Nov 18 14:05:17 1989
export PATH; PATH=/bin:$PATH
echo shar: extracting "'README'" '(906 characters)'
if test -f 'README'
then
	echo shar: will not over-write existing file "'README'"
else
sed 's/^	X//' << \SHAR_EOF > 'README'
	X
	X		    ADSH - Adventure Shell
	X		    ----------------------
	X
	XThis is another 'adventure' type shell, written in perl. It features
	Xaliasing and history substitution similar to csh. 
	X
	XOn startup, it reads your .cshrc file to get your aliases, and you can define
	Xany others in the perl script. For aliases, it recognises the \!* pattern
	X(would be just !* for csh).
	X
	XIf you have a file .adshd in a directory containing a description, it is
	Xprinted out when you do a "look" command.
	X
	XIt has a number of built in commands, and it is easy to define more.
	XAny command it does not handle is passed through to csh.
	X
	XCommands to try:
	X    look
	X    move [<dir>]
	X    north
	X    examine files|dirs|directories|<file>
	X    exit
	X    alias
	X    history	( also !!, !<number>, !<cmd> )
	X    describe
	X
	X
	XThis is fairly basic, and I invite perl hackers to further improve it.
	XPlease send any improvements to <dunc at tsingtao.cs.ubc.ca>
SHAR_EOF
if test 906 -ne "`wc -c < 'README'`"
then
	echo shar: error transmitting "'README'" '(should have been 906 characters)'
fi
fi # end of overwriting check
echo shar: extracting "'adsh'" '(5628 characters)'
if test -f 'adsh'
then
	echo shar: will not over-write existing file "'adsh'"
else
sed 's/^	X//' << \SHAR_EOF > 'adsh'
	X#!/cs/public/bin/perl
	X
	X#
	X# routines
	X#
	Xsub look
	X  {
	X    local( $cwd, @dirs, $ls, $f, $d );
	X    system "clear";
	X    chop($cwd = `pwd`);
	X    @dirs = split( "/", $cwd );
	X    if($cwd eq "/")
	X      {
	X	print "You are at the top of the filesystem.\n";
	X      }
	X    else
	X      {
	X	print "You are in a directory called \"", $dirs[$#dirs], "\"\n";
	X	print "To the north is a directory called \"", $dirs[$#dirs-1], "\"\n";
	X      }
	X    if( -f ".adshd" )
	X      {
	X	open(in, ".adshd" );
	X	while(<in>) { print; }
	X      }
	X    chop($ls = `/bin/ls`);
	X    $f = 0; $d = 0;
	X    foreach $file (split( " ", $ls ))
	X      {
	X	$f++ if -f $file;
	X	$d++ if -d $file;
	X      }
	X    print "  There are some directories here.\n" if $d;
	X    print "  You notice some files lying on the ground.\n" if $f;
	X  }
	X
	Xsub describe
	X  {
	X    open(out, ">.adshd" );
	X    while(<>) { print out; }
	X  }
	X
	Xsub move
	X  {
	X    if( $#argv == $[ )
	X      {
	X	chdir $ENV{"HOME"};
	X      }
	X    else
	X      {
	X	$argv[1] =~ s/~/$ENV{"HOME"}/;
	X	chdir $argv[1];
	X      }
	X    chop($cwd = `pwd`);
	X    print "You are in a directory called \"", $cwd, "\"\n";
	X    if( -f ".adshd" )
	X      {
	X	open(in, ".adshd" );
	X	while(<in>) { print; }
	X      }
	X  }
	X
	Xsub examine
	X  {
	X    if( $#argv == $[ )
	X      {
	X	print "  Examine what?\n";
	X      }
	X    elsif( $argv[1] eq "files" )
	X      {
	X	chop($ls = `/bin/ls`);
	X	foreach $file (split( " ", $ls ))
	X	  {
	X	    if( -f $file )
	X	      {
	X		print "  There is a";
	X		print " file" if -f $file;
	X		print " link" if -l $file;
	X		print " called \"", $file, "\" here.\n";
	X	      }
	X	  }
	X      }
	X    elsif( $argv[1] eq "directories" || $argv[1] eq "dirs" )
	X      {
	X	chop($ls = `/bin/ls`);
	X	foreach $file (split( " ", $ls ))
	X	  {
	X	    if( -d $file )
	X	      {
	X		print "  There is a";
	X		print " directory" if -d $file;
	X		print " link" if -l $file;
	X		print " called \"", $file, "\" here.\n";
	X	      }
	X	  }
	X      }
	X    elsif( -e $argv[1])
	X      {
	X	print "  ", $argv[1], " is a";
	X	print " file" if -f $argv[1];
	X	print " directory" if -d $argv[1];
	X	print " link" if -l $argv[1];
	X	print "\n";
	X      }
	X    else
	X      {
	X	print "  I see no ", $argv[1], " here.\n";
	X      }
	X  }
	X
	Xsub die
	X  {
	X    system "mv $inventory/* .";
	X    print "\nYou died by committing suicide.\n\n";
	X    print "You scored a total of ", $cmd_count, " points.\n";
	X    exit 0;
	X  }
	Xsub alias
	X  {
	X    local( $c );
	X
	X    if( $#argv == $[ )
	X      {
	X	for( $c=0; $c <= $#aliases; $c++ )
	X	  {
	X	    print $aliases[$c], "\t", $expand[$c], "\n";
	X	  }
	X      }
	X    else
	X      {
	X	print "unable to set aliases yet.\n";
	X      }
	X  }
	X
	Xsub history
	X  {
	X    local( $c );
	X
	X    for( $c=0; $c <= $#history; $c++ )
	X      {
	X	print $hist_count[$c], ": ", $history[$c], "\n";
	X      }
	X  }
	X
	Xsub expand
	X  {
	X    local( $c, $line );
	X
	X    for( $c=0; $c <= $#aliases; $c++ )
	X      {
	X	if( $aliases[$c] eq $_[0] )
	X	  {
	X	    $line = $expand[$c];
	X	    if( $line =~ /\\\!\*/ )
	X	      {
	X		$line =~ s/\\\!\*/@_[1..$#_]/g;
	X	      }
	X	    else
	X	      {
	X		$line = join( " ", $line, @_[1..$#_] )
	X		    unless $line =~ /;/;
	X	      }
	X
	X	    do parse( $line );
	X	    return 1;
	X	  }
	X      }
	X    return 0;
	X  }
	X
	Xsub parse
	X  {
	X    local( $string ) = shift( @_ );
	X    local( @lines, $line, @argv );
	X
	X    @lines = split(/[ \t\n]*;[ \t\n]*/, $string );
	X    foreach $line (@lines)
	X      {
	X	@argv = split(/[ \t\n]+/, $line);
	X#	print "argv = ", join(", ", @argv), "\n";
	X	next if do expand( @argv );
	Xfoo:      {
	X	    if($argv[0] eq "exit")	{ do die(); }
	X	    if($argv[0] eq "look")	{ do look(); last foo; }
	X	    if($argv[0] eq "move")	{ do move(); last foo; }
	X	    if($argv[0] eq "north")	{ chdir ".."; last foo; }
	X	    if($argv[0] eq "examine")	{ do examine(); last foo; }
	X	    if($argv[0] eq "alias")	{ do alias(); last foo; }
	X	    if($argv[0] eq "history")	{ do history(); last foo; }
	X	    if($argv[0] eq "describe")	{ do describe(); last foo; }
	X	    system "csh", "-c", join(" ", @argv);
	X	  }
	X      }
	X  }
	X
	Xsub read_cshrc
	X  {
	X    local( $expand );
	X    open( in, $ENV{"HOME"}."/.cshrc" );
	X    while(<in>)
	X      {
	X	chop; split;
	X	shift @_ if $_[0] eq "";
	X	if( $_[0] eq "alias")
	X	  {
	X	    push( @aliases, $_[1] );
	X	    $expand = join(" ", @_[2..$#_]);
	X	    $expand = substr($expand, 1, length($expand) - 2)
	X		if $expand =~ /^\'/;
	X	    push( @expand, $expand );
	X	  }
	X      }
	X    close(in);
	X  }
	X#
	X# main code
	X#
	X$inventory = $ENV{"HOME"}."/.adshi";
	X$cmd_count = 0;
	X$prompt = "> ";
	X at aliases = (	"cd",
	X		"up",
	X		"q",
	X		"quit",
	X		"inv",
	X		"inventory",
	X		"get",
	X		"drop"
	X);
	X at expand = (	"move",
	X		"north",
	X		"exit",
	X		"exit",
	X		"inventory",
	X		"echo \"You are carrying:\" ; echo \"\"; ls $inventory",
	X		"mv \\!* $inventory",
	X		"mv $inventory/\\!* ."
	X);
	X$ENV{"SHELL"} = $0;
	X
	Xselect( stdout); $| = 1;
	Xdo read_cshrc();
	Xmkdir( $inventory, 0700) unless -d $inventory;
	Xdo look();
	Xprint $prompt;
	Xwhile(<>)
	X  {
	X    chop;
	X
	X    if( /^\!(\d+)/ )
	X      {
	X	eval "\$x = $1";
	X	if( $x < $hist_count[0] || $x > $hist_count[$#hist_count] )
	X	  {
	X	    print "History available: ", 
	X		$hist_count[0], " - ", $hist_count[$#hist_count], "\n";
	X	    next;
	X	  }
	X	for( $c=0; $c<=$#hist_count; $c++ )
	X	  {
	X	    $_ = $history[$c], last if $hist_count[$c] eq $x;
	X	  }
	X	print $_, "\n";
	X      }
	X    if( /^\!(\w+)/ )
	X      {
	X	$match ="";
	X	for( $c=$#hist_count; $c>=0; $c-- )
	X	  {
	X	    $match = $history[$c], last 
	X		if $1 eq substr($history[$c], 0, length($1));
	X	  }
	X	if( $match eq "")
	X	  {
	X	    print "no match\n";
	X	    next;
	X	  }
	X	$_ = $match;
	X	print $_, "\n";
	X      }
	X    $_ = $history[$#history],  print $_, "\n" if $_ eq "!!";
	X
	X    push( @history, $_ );
	X    push( @hist_count, ++$cmd_count );
	X    if( $#histrory > 20 )
	X      {
	X	shift( @history );
	X	shift( @hist_count );
	X      }
	X    do parse( $_ );
	X  }
	Xcontinue
	X  {
	X    print $prompt;
	X  }
SHAR_EOF
if test 5628 -ne "`wc -c < 'adsh'`"
then
	echo shar: error transmitting "'adsh'" '(should have been 5628 characters)'
fi
chmod +x 'adsh'
fi # end of overwriting check
#	End of shell archive
exit 0



More information about the Alt.sources mailing list