Beispiel #1
0
void mysh_loop(void)
{
    char *line;
    TokenInfo *token_infos;
    int is_running = 1;

    do {
        printf("$ ");
        line = mysh_read_line();

        token_infos = malloc(sizeof(TokenInfo*) * 100);
        int token_count = mysh_get_tokens(line, token_infos);
        int in = STDIN_FILENO;
        int out = -1;
        char **args;
        for (int i=0; i<token_count; i++) {
            TokenInfo token_info = token_infos[i];
            int fd[2];
            pipe (fd);

            if (i == token_count - 1) out = STDOUT_FILENO;

            if (token_info.token_id == TKN_NORMAL) {
                args = malloc(sizeof(char*) * 100);
                args = parse_cmd(token_info.token);
                if (is_next(TKN_PIPE, i, token_count, token_infos)) {
                    out = fd[PIPE_WRITE];
                    mysh_execute(in, out, args);
                    close (fd[PIPE_WRITE]);
                    in = fd[PIPE_READ];
                } else if (is_next(TKN_REDIR_IN, i, token_count, token_infos)) {
                    mysh_execute(open_for_rdir_in(token_infos[i+2].token), out, args);
                    i += 2;
                } else if (is_next(TKN_REDIR_OUT, i, token_count, token_infos)) {
                    printf("%s\n", token_infos[i].token);
                    mysh_execute(in, open_for_rdir_out(token_infos[i+2].token), args);
                    i += 2;
                } else {
                    mysh_execute(in, out, args);
                }
                free(args);
            }
        }

        free(line);
        free(token_infos);
    } while (is_running);
}
Beispiel #2
0
void mysh_loop() {
    char *line;
    struct command *command;
    int status = 1;

    do {   // an infinite loop to handle commands
        mysh_print_promt();
        line = mysh_read_line();   // read one line from terminal
        if (strlen(line) == 0) {
            continue;
        }
        command = mysh_parse_command(line);   // parse line as command structure
        status = mysh_execute_command(command);   // execute the command
        free(line);
    } while (status >= 0);
}