Skip to content

3. Command Structure in Minishell

Ilia Munaev edited this page May 21, 2025 · 2 revisions

Command Structure in Minishell


Command Data (s_cmd)

Each command is stored in a s_cmd struct. This includes:

  • argv: the actual command and its arguments (like ["ls", "-l"])
  • binary: the full path to the program that should be executed (like /bin/ls)
  • next: a pointer to the next command (used when commands are connected with |)
  • minishell: a pointer to the main shell structure, so each command can access global state
  • redirs: a list of redirections for this command
  • origin_head: a pointer to the first command in the pipeline (used for tracking the whole chain)
typedef struct s_cmd
{
	char		**argv;
	char		*binary;
	t_cmd		*next;
	t_mshell	*minishell;
	t_list		*redirs;
	t_cmd	        *origin_head;
}

Redirection Types (e_redir_type)

Minishell supports 4 types of redirection:

  • < (input): reads from a file instead of standard input
  • > (output): writes output to a file (overwriting it)
  • >> (append): adds output to the end of a file
  • << (heredoc): reads input from the user until a specific keyword is entered
typedef enum e_redir_type
{
	R_INPUT,    // "<"
	R_OUTPUT,   // ">"
	R_APPEND,   // ">>"
	R_HEREDOC   // "<<"
} t_redir_type;

Redirection Data (s_redir)

Each redirection in a command is stored as a s_redir struct, which includes:

  • The type of redirection (from the list above)
  • The filename (or delimiter, for heredoc)
  • The file descriptor being redirected (like input or output)
  • A runtime file descriptor used during execution
  • A flag for whether to expand variables in heredoc input
typedef struct s_redir
{
	t_redir_type type;
	char	     *filename;
	int	     target_fd;
	int	     fd;
	int	     expand_in_heredoc;
}	

Clone this wiki locally