void exec_pipeline(t_param *param, t_pipeline *pipeline) { t_cmd *cmds; int i; int status; status = 0; pipeline->cmd = my_str_to_wordtab_light(pipeline->line, build_delimiters_tab("|", 0)); cmds = xsmalloc(sizeof(*cmds) * (pipeline->nb_pipe + 2)); variables_substitution(param, pipeline); exec_cmds(param, cmds, pipeline, (i = -1) + 1); while (++i < (pipeline->nb_pipe + 1)) if (cmds[i].pid != PID_BUILTIN) { waitpid(cmds[i].pid, &status, 0); pipeline->status = WEXITSTATUS(status); set_cmd_state(param, 0, status); if (WIFSIGNALED(status) && (!param->forked)) my_printf("%s%d]\n",disp_signal_name(WTERMSIG(status)), cmds[i].pid); } else { pipeline->status = cmds[i].status; set_cmd_state(param, 1, status); } msfree(pipeline->cmd, cmds, 0); }
int main(int ac, char **av, char **envp) { char *cmd; init_env(envp); add_lvl(); signal(SIGINT, sig_handler); while (g_running) { ft_putstr(hash_getset(&g_env, "MSP", "$> ", 4)->value); get_next_line(0, &cmd); exec_cmds(cmd); ft_memdel((void **)&cmd); } hash_del(&g_env, del_hash); (void)ac; (void)av; return (0); }
int main(int argc, char **argv) { char *line; struct commands cmds; int quit = 0; while (!quit) { line = readline("myshell> "); if (!line) return (0); add_history(line); (void) parse_line(line, &cmds); if (exec_cmds(&cmds)) { fprintf(stderr, "Line execution failed.\n"); } free_cmds(&cmds); free(line); } return (0); }
int fc(char ** args){ char * tmp1, * tmp2; char ** cmd_args; tmp1 = args[1]; tmp2 = args[2]; int start, end; if ( debug > 10) printf("fc %s, %s\n", tmp1, tmp2); if (tmp1 == NULL && tmp2 == NULL){ start = cur_cmd; end = cur_cmd; } else if ( tmp2 == NULL ) { if (validate_number(tmp1) != 0){ printf("shellp error - illegal fommand found \n"); return 1; } else { start = atoi(tmp1); end = start; } } else if (validate_number(tmp1) == 0 && validate_number(tmp2) == 0){ start = atoi(tmp1); end = atoi(tmp2); } else { printf("shellp error - xillegal fommand found \n"); return 1; } if ( debug > 10) printf("fc2 %d, %d\n", start, end); if (start < 0 || start > 1023 || end < 0 || end > 1023 || start > end ){ printf("shellp error - illegal fommand found \n"); return 1; } int length = end - start + 1; if (length > num_cmds) { printf("shellp error - too many commands requested \n"); return 1; } start = cur_cmd - 1 - start; if ( start < 0 ) start = SIZE_CMD_STACK+start; end = cur_cmd -1 - end; if ( end < 0 ) end = SIZE_CMD_STACK+start; int thisc = start; for (int i = 0; i < length; i++){ char next_cmdp[1024]; strcpy(next_cmdp,cmd_stack[thisc]); char * cmd; int num_args; if (debug > 10){ int len = num_cmds; for ( int k = 0; k < len; k++ ){ printf("i=%d cmd=%s\n", k, cmd_stack[k]); } } if ( debug > 10) printf("fc3 %s, %d %d\n", next_cmdp, i, length); cmd_args = parseline(next_cmdp, &num_args); cmd = cmd_args[0]; if ( debug > 10) printf("fc4 %s %d %d %d\n", cmd, start, end, thisc); if ( debug > 10) printf("fc4.1 %d %d %d\n", cur_cmd, next_cmd, num_cmds); if (debug > 11) printf("fc5%s:%d\n",cmd,num_args); if (cmd != NULL) { if ( exec_cmds(cmd_args, num_args, false) != 0 ){ exit(1); } } for ( int j = 0; j < 1024; j++ ){ free(cmd_args[j]); } free(cmd_args); thisc--; if ( thisc < 0 ) thisc = SIZE_CMD_STACK - 10; } return 0; }