Skip to content

Commit

Permalink
Updated with builtin command support!
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephen Brennan committed Jan 16, 2015
1 parent 540a381 commit 45a0026
Showing 1 changed file with 100 additions and 5 deletions.
105 changes: 100 additions & 5 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,87 @@
#include <stdio.h>
#include <string.h>

/*
Function Declarations for builtin shell commands:
*/
int lsh_cd(char **args);
int lsh_help(char **args);
int lsh_exit(char **args);

/*
List of builtin commands, followed by their corresponding functions.
*/
char *builtin_str[] = {
"cd",
"help",
"exit"
};

int (*builtin_func[]) (char **) = {
&lsh_cd,
&lsh_help,
&lsh_exit
};

int lsh_num_builtins() {
return sizeof(builtin_str) / sizeof(char *);
}

/*
Builtin function implementations.
*/

/**
@brief Bultin command: change directory.
@param args List of args. args[0] is "cd". args[1] is the directory.
@return Always returns 1, to continue executing.
*/
int lsh_cd(char **args)
{
if (args[1] == NULL) {
fprintf(stderr, "lsh: expected argument to \"cd\"\n");
} else {
if (chdir(args[1]) != 0) {
perror("lsh");
}
}
return 1;
}

/**
@brief Builtin command: print help.
@param args List of args. Not examined.
@return Always returns 1, to continue executing.
*/
int lsh_help(char **args)
{
int i;
printf("Stephen Brennan's LSH\n");
printf("Type program names and arguments, and hit enter.\n");
printf("The following are built in:\n");

for (i = 0; i < lsh_num_builtins(); i++) {
printf(" %s\n", builtin_str[i]);
}

printf("Use the man command for information on other programs.\n");
return 1;
}

/**
@brief Builtin command: exit.
@param args List of args. Not examined.
@return Always returns 0, to terminate execution.
*/
int lsh_exit(char **args)
{
return 0;
}

/**
@brief Launch a program and wait for it to terminate.
@param args Null terminated list of arguments (including program).
@return 0
@return Always returns 1, to continue execution.
*/
int lsh_launch(char **args)
{
Expand All @@ -45,7 +121,25 @@ int lsh_launch(char **args)
} while (!WIFEXITED(status) && !WIFSIGNALED(status));
}

return 0;
return 1;
}

/**
@brief Execute shell built-in or launch program.
@param args Null terminated list of arguments.
@return 1 if the shell should continue running, 0 if it should terminate
*/
int lsh_execute(char **args)
{
int i;

for (i = 0; i < lsh_num_builtins(); i++) {
if (strcmp(args[0], builtin_str[i]) == 0) {
return (*builtin_func[i])(args);
}
}

return lsh_launch(args);
}

#define LSH_RL_BUFSIZE 1024
Expand Down Expand Up @@ -146,16 +240,17 @@ void lsh_loop(void)
{
char *line;
char **args;
int status;

while (1) {
do {
printf("> ");
line = lsh_read_line();
args = lsh_split_line(line);
lsh_launch(args);
status = lsh_execute(args);

free(line);
lsh_free_args(args);
}
} while (status);
}

/**
Expand Down

0 comments on commit 45a0026

Please sign in to comment.