#
# ShellExecutables.rb
#
#   $Author: hiroya $
#   $Date: 2000/11/23 01:53:10 $
#   Copyright (C) 2000 Hiroya KUBO
#

module ShellExecutables

  def getShellExecutables
    if(@shell_executables == nil)
      @shell_executables = {
	'ls'      => ls,
	'cd'      => cd,
	'pwd'     => pwd,
      }
    end
    return @shell_executables
  end

  def cd
    return Proc.new{|argv|
      begin
	if(argv.size == 0)
	  @dir.setPath(@dir.getHomeDirectory)
	elsif(argv.size == 1)
	  @dir.setPath(argv[0])
	else
	  print("cd: Too many arguments.\n")
	end
      rescue NamingException
	print($!.getValue+"\n")
      end
    }
  end

  def pwd
    return Proc.new{|argv|
      print(@dir.getPath+"\n")
    }
  end

  def ls
    return Proc.new{|argv|
      if(argv.size == 0)
	argv.push("")
      end

      begin
	i = 0
	for arg in argv
	  if(argv.size > 1)
	    i += 1
	    if(i > 1)
	      print "\n"
	    end
	    print(arg+":\n")
	  end

	  ret = []
	  @dir.foreach(@dir.getContextualName(arg)) do |filename|
	    if(@dir.directory?(filename))
	      ret.push(filename+@dir.getSeparator)
	    else
	      ret.push(filename)
	    end
	  end

	  ret.sort.each do |n|
	    print n,"\n"
	  end

	end
      rescue NamingException
	print($!.getValue+"\n")
      end
    }
  end

end
