Merge pull request 'help' (#16) from help into master

Reviewed-on: #16
This commit is contained in:
Chris Cromer 2021-07-20 01:02:16 -04:00
commit ef8bc8168e
3 changed files with 30 additions and 0 deletions

View File

@ -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 $<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) {
free_string_array(args);
exit(EXIT_SUCCESS);

View File

@ -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.

View File

@ -13,6 +13,7 @@
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <signal.h>
#include <stdlib.h>
#include "loop.h"
@ -23,6 +24,7 @@
* @return Returns 0 on success or an error code on failure.
*/
int main(int argc, char **argv) {
signal(SIGINT, SIG_IGN);
loop();
return EXIT_SUCCESS;
}