Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/wait.h>
- #include <unistd.h>
- #include <string.h>
- #define SHELL_RL_BUFSIZE 1024
- #define SHELL_TOK_BUFSIZE 64
- #define SHELL_TOK_DELIM " \t\r\n\a"
- #define MAX_HISTORY_SIZE 100
- #define clear() printf("\033[H\033[J")
- /*
- Function Declarations for builtin shell commands:
- */
- int s_cd(char **args);
- int s_help(char **args);
- int s_exit(char **args);
- int s_history(char **args);
- int s_show_history();
- char *history[MAX_HISTORY_SIZE][10];
- int history_position=0;
- /*
- List of builtin commands, followed by their corresponding functions.
- */
- char *builtin_str[] = {
- "cd",
- "help",
- "exit",
- "history",
- };
- int (*builtin_func[]) (char **) = {
- &s_cd,
- &s_help,
- &s_exit,
- &s_show_history
- };
- int s_show_history(char **args){
- for(int i=1;i<history_position;i++)
- printf("%d. %s\n",i, history[i-1]);
- return 1;
- }
- int s_num_builtins() {
- return sizeof(builtin_str) / sizeof(char *);
- }
- /*
- Builtin function implementations.
- */
- int s_cd(char **args)
- {
- if (args[1] == NULL) {
- fprintf(stderr, "shell: expected argument to \"cd\"\n");
- } else {
- if (chdir(args[1]) != 0) {
- perror("shell");
- }
- }
- return 1;
- }
- int s_help(char **args)
- {
- int i;
- printf("This are the implemented functions : \n");
- for (i = 0; i < s_num_builtins(); i++) {
- printf(" %s\n", builtin_str[i]);
- }
- return 1;
- }
- int s_exit(char **args)
- {
- printf("Successful exit\n");
- return 0;
- }
- int s_history(char **args)
- {
- int i;
- strcpy(history[history_position],args[0]);
- history_position++;
- return 1;
- }
- int s_execute(char **args)
- {
- int i;
- if (args[0] == NULL) {
- // An empty command was entered.
- return 1;
- }
- s_history(args);
- for (i = 0; i < s_num_builtins(); i++) {
- if (strcmp(args[0], builtin_str[i]) == 0) {
- return (*builtin_func[i])(args);
- }
- }
- return s_launch(args);
- }
- int s_launch(char **args)
- {
- pid_t pid, wpid;
- int status;
- pid = fork();
- if (pid == 0) {
- // Child process
- if (execvp(args[0], args) == -1) {
- perror("shell");
- }
- exit(EXIT_FAILURE);
- } else if (pid < 0) {
- // Error forking
- perror("shell");
- } else {
- // Parent process
- do {
- wpid = waitpid(pid, &status, WUNTRACED);
- } while (!WIFEXITED(status) && !WIFSIGNALED(status));
- }
- return 1;
- }
- int s_history_arrow_up(){
- printf("THIS IS A\n");
- int i;
- char his_val[1];
- for(i=1;i<=history_position;i++){
- printf("%d. %s\n",i, history[i-1]);
- }
- return 1;
- }
- char **s_split_line(char *line)
- {
- int bufsize = SHELL_TOK_BUFSIZE, position = 0;
- char **tokens = malloc(bufsize * sizeof(char*));
- char *token;
- if (!tokens) {
- fprintf(stderr, "shell: allocation error\n");
- exit(EXIT_FAILURE);
- }
- token = strtok(line, SHELL_TOK_DELIM);
- while (token != NULL) {
- tokens[position] = token;
- position++;
- if (position >= bufsize) {
- bufsize += SHELL_TOK_BUFSIZE;
- tokens = realloc(tokens, bufsize * sizeof(char*));
- if (!tokens) {
- fprintf(stderr, "shell: allocation error\n");
- exit(EXIT_FAILURE);
- }
- }
- token = strtok(NULL, SHELL_TOK_DELIM);
- }
- tokens[position] = NULL;
- return tokens;
- }
- char *s_read_line(void)
- {
- int bufsize = SHELL_RL_BUFSIZE;
- int position = 0;
- char *buffer = malloc(sizeof(char) * bufsize);
- int c;
- if (!buffer) {
- fprintf(stderr, "shell: allocation error\n");
- exit(EXIT_FAILURE);
- }
- while (1) {
- // Read a character
- c = getchar();
- if (c == '\033') { // if the first value is esc
- getchar(); // skip the [
- switch(getchar()) { // the real value
- case 'A':
- s_history_arrow_up();
- break;
- case 'B':
- printf("THIS IS B\n");
- break;
- }
- }else if (c == EOF || c == '\n') { // If we hit EOF, replace it with a null character and return.
- buffer[position] = '\0';
- return buffer;
- } else {
- buffer[position] = c;
- }
- position++;
- // If we have exceeded the buffer, reallocate.
- if (position >= bufsize) {
- bufsize += SHELL_RL_BUFSIZE;
- buffer = realloc(buffer, bufsize);
- if (!buffer) {
- fprintf(stderr, "shell: allocation error\n");
- exit(EXIT_FAILURE);
- }
- }
- }
- }
- void s_loop(void)
- {
- char *line;
- char **args;
- int status;
- clear();
- printf("Use help command for more information\n");
- do {
- printf("> ");
- line = s_read_line();
- args = s_split_line(line);
- status = s_execute(args);
- free(line);
- free(args);
- } while (status);
- }
- int main(int argc, char **argv)
- {
- // Run command loop.
- s_loop();
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement