Builtins

Built-in command registry and implementations.

Header for builtin function registry and declarations.

Defines the t_builtin_fn type and declares builtin functions. Also includes the builtin lookup function.

Author

zweng, pulgamecanica

Typedefs

typedef struct s_shell t_shell
typedef int (*t_builtin_fn)(struct s_shell *shell, int argc, char **argv)

Builtin function type.

Param shell:

Pointer to the central shell state, for accessing variables, jobs, etc.

Param argc:

Argument count (number of elements in argv)

Param argv:

Argument vector (array of strings, with argv[0] being the command name)

Return:

Exit status code (0 for success, non-zero for failure)

Functions

t_builtin_fn builtin_get(const char *name)

Builtin registry.

Builtin registry.

Parameters:
  • name – Name of the builtin function to retrieve

  • name – The name of the builtin to look up.

Returns:

Pointer to the builtin function, or NULL if not found

Returns:

The function pointer or NULL if not found.

int builtin_is_builtin(const char *name)

Check if a name is a builtin.

Parameters:

name – The name to check.

Returns:

1 if the name is a builtin, 0 otherwise.

int builtin_echo(struct s_shell *shell, int argc, char **argv)
int builtin_cd(struct s_shell *shell, int argc, char **argv)
int builtin_exit(struct s_shell *shell, int argc, char **argv)
int builtin_type(struct s_shell *shell, int argc, char **argv)
int builtin_export(struct s_shell *shell, int argc, char **argv)
int builtin_unset(struct s_shell *shell, int argc, char **argv)
int builtin_set(struct s_shell *shell, int argc, char **argv)
int builtin_jobs(struct s_shell *shell, int argc, char **argv)
int builtin_fg(struct s_shell *shell, int argc, char **argv)
int builtin_bg(struct s_shell *shell, int argc, char **argv)
int builtin_history(struct s_shell *shell, int argc, char **argv)

display the command history list

This function displays the command history list in a format similar to bash and ksh.

Parameters:
  • shell – The shell context (not used in this function).

  • argc – The number of arguments passed to the history command.

  • argv – The arguments passed to the history command. If a numeric argument is provided, it specifies how many of the most recent entries to display.

Returns:

int Always returns 0.

builtin_history.c

Implementation of the history builtin command for the 42sh shell.

Author

pulgamecanica

Functions

static void print_entries(HIST_ENTRY **list, int start, int end)

Print the command history list.

Prints all entries of the history

Parameters:
  • list – The list of history entries obtained from readline’s history_list()

  • start – The starting index in the history list to print from

  • end – The ending index in the history list to print to (exclusive)

int builtin_history(struct s_shell *shell, int argc, char **argv)

display the command history list

This function displays the command history list in a format similar to bash and ksh.

Parameters:
  • shell – The shell context (not used in this function).

  • argc – The number of arguments passed to the history command.

  • argv – The arguments passed to the history command. If a numeric argument is provided, it specifies how many of the most recent entries to display.

Returns:

int Always returns 0.