From ae5a5b372a4c43275142c6705ad48d18c39cf69f Mon Sep 17 00:00:00 2001 From: Chris Cromer Date: Mon, 19 Jul 2021 22:34:50 -0400 Subject: [PATCH] add help --- src/builtins.c | 22 ++++++++++++++++++++++ src/include/builtins.h | 6 ++++++ 2 files changed, 28 insertions(+) diff --git a/src/builtins.c b/src/builtins.c index a578edf..cd541ae 100644 --- a/src/builtins.c +++ b/src/builtins.c @@ -27,6 +27,10 @@ bool is_builtin(char *command) { return true; } + if (strcmp(command, "help") == 0) { + return true; + } + if (strcmp(command, "cd") == 0) { return true; } @@ -50,6 +54,9 @@ void run_builtin(StringArray *args) { if (strcmp(args->array[0], "exit") == 0) { exit_shell(args); } + else if (strcmp(args->array[0], "help") == 0) { + help(args); + } else if (strcmp(args->array[0], "cd") == 0) { change_directory(args); } @@ -67,6 +74,21 @@ void run_builtin(StringArray *args) { } } +void help(StringArray *args) { + if (args->size > 1) { + fprintf(stderr, "Too many arguments!\n"); + return; + } + fprintf(stdout, "myshellin help\n"); + fprintf(stdout, "These command are defined internally in myshellin.\n\n"); + fprintf(stdout, "set $ = set an environment variable\n"); + fprintf(stdout, "environ show all enviornment variables\n"); + fprintf(stdout, "echo [$ ...] [comment ...] print comments or variables to stdout\n"); + fprintf(stdout, "help show this help message\n"); + fprintf(stdout, "cd [dir] [$] change directory\n"); + fprintf(stdout, "exit exit the shell\n"); +} + void exit_shell(StringArray *args) { free_string_array(args); exit(EXIT_SUCCESS); diff --git a/src/include/builtins.h b/src/include/builtins.h index 6f4b716..90ad8d1 100644 --- a/src/include/builtins.h +++ b/src/include/builtins.h @@ -32,6 +32,12 @@ bool is_builtin(char *command); */ void run_builtin(StringArray *args); +/** + * Show help message for the shell. + * @param args The arguments passed to help. + */ +void help(StringArray *args); + /** * Exit the shell. * @param args The arguments that were used to call exit. This is used to free the memory before exit.