This commit is contained in:
Chris Cromer 2021-07-19 22:34:50 -04:00
parent 57dbb56c4c
commit ae5a5b372a
2 changed files with 28 additions and 0 deletions

View File

@ -27,6 +27,10 @@ bool is_builtin(char *command) {
return true; return true;
} }
if (strcmp(command, "help") == 0) {
return true;
}
if (strcmp(command, "cd") == 0) { if (strcmp(command, "cd") == 0) {
return true; return true;
} }
@ -50,6 +54,9 @@ void run_builtin(StringArray *args) {
if (strcmp(args->array[0], "exit") == 0) { if (strcmp(args->array[0], "exit") == 0) {
exit_shell(args); exit_shell(args);
} }
else if (strcmp(args->array[0], "help") == 0) {
help(args);
}
else if (strcmp(args->array[0], "cd") == 0) { else if (strcmp(args->array[0], "cd") == 0) {
change_directory(args); 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 $<variable> = <value> set an environment variable\n");
fprintf(stdout, "environ show all enviornment variables\n");
fprintf(stdout, "echo [$<variable> ...] [comment ...] print comments or variables to stdout\n");
fprintf(stdout, "help show this help message\n");
fprintf(stdout, "cd [dir] [$<variable>] change directory\n");
fprintf(stdout, "exit exit the shell\n");
}
void exit_shell(StringArray *args) { void exit_shell(StringArray *args) {
free_string_array(args); free_string_array(args);
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);

View File

@ -32,6 +32,12 @@ bool is_builtin(char *command);
*/ */
void run_builtin(StringArray *args); 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. * Exit the shell.
* @param args The arguments that were used to call exit. This is used to free the memory before exit. * @param args The arguments that were used to call exit. This is used to free the memory before exit.