Package core

Class CommandHandler

java.lang.Object
core.CommandHandler

public class CommandHandler extends Object
Central class responsible for parsing user input, handling command execution (builtins and external), managing pipelines, redirection, and maintaining the current working directory.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructs a CommandHandler, initializing all built-in commands, the command history, and setting the current working directory to the user's directory.
  • Method Summary

    Modifier and Type
    Method
    Description
    getCommand(String[] tokens, String rawInput, File redirectFile, File stderrRedirectFile)
    Returns the appropriate Command instance (builtin or external) for the given tokens.
    Returns the history of commands.
    handleCommand(String input, Path currentDirectory)
    Parses and executes a single shell command or pipeline, handling redirection and updating the current directory as needed.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • CommandHandler

      public CommandHandler()
      Constructs a CommandHandler, initializing all built-in commands, the command history, and setting the current working directory to the user's directory.
  • Method Details

    • handleCommand

      public Path handleCommand(String input, Path currentDirectory)
      Parses and executes a single shell command or pipeline, handling redirection and updating the current directory as needed.
      Parameters:
      input - The command line input string.
      currentDirectory - The current working directory.
      Returns:
      The updated current directory after command execution.
    • getCommand

      public Command getCommand(String[] tokens, String rawInput, File redirectFile, File stderrRedirectFile)
      Returns the appropriate Command instance (builtin or external) for the given tokens.
      Parameters:
      tokens - The command and its arguments.
      rawInput - The raw input string.
      redirectFile - Output redirection file, if any.
      stderrRedirectFile - Error redirection file, if any.
      Returns:
      The Command instance to execute.
    • getHistory

      public List<String> getHistory()
      Returns the history of commands.
      Returns:
      The list of previous commands entered by the user.