int exec_word(t_sh *sh, t_tree *root) { pid_t pid; int status; status = 0; if (root->content->len > 1024) return (1); if (exec_is_builtin(root)) return (exec_builtin(sh, root)); if (exec_new_cmd(sh, root) < 0) return (1); if ((pid = fork()) == 0) exec_fork(root); if ((root->cmd.bitset & EX_INPIPE) == EX_INPIPE) { close(root->cmd.fd[0][0]); close(root->cmd.fd[0][1]); } else if ((root->cmd.bitset & EX_INREDIR) == EX_INREDIR) close(root->cmd.fd[0][0]); if ((root->cmd.bitset & EX_WAIT) == EX_WAIT) { waitpid(pid, &status, 0); return (WEXITSTATUS(status)); } return (0); }
int exec_select_execution_other(t_node *node, t_global *global) { int status; t_node *tmp; if (is_builtin(node->content) != -1) { if ((exec_builtin(node, global)) == EXIT_FAILURE) { global->exit = EXIT_FAILURE; return (EXIT_FAILURE); } return (EXIT_CHILD); } else if (my_strsearch(node->content, '$') == 1) { if ((tmp = parsing_func(node->content, global, 1)) == NULL) return (EXIT_CHILD); if ((status = exec_course_tree(tmp, global->env, \ global)) >= EXIT_FAILURE) return (status); return (EXIT_CHILD); } return (EXIT_SUCCESS); }
int exec_env(t_struc *s, char **env2) { int i; int j; int option_i; char **new_env; i = 1; j = 1; option_i = 0; new_env = env2 ? copy_env(env2) : (char **)malloc(sizeof(char *) * 1); if (s->argc == 1) print_env(new_env); else { if (get_env_options(s, &i, &option_i) == -1) return (-1); if (option_i) new_env = (char **)malloc(sizeof(char *) * 1); s->cmd = NULL; new_env = exec_env2(s, i, j, new_env); if (s->cmd) !is_builtin(s) ? exec_cmd(s, new_env) : exec_builtin(s, new_env); else print_env(new_env); } return (0); }
/* * check_builtin() recieves a string vector, * and checks if the first element matches the * name of any builtin commands. If found, it will execute the match's * respective function and return * the result of that function. If not found, it will return (-1). */ int check_builtins(int ac, char **cmd, char **env) { int i; builtin_f exec_builtin; char *builtins_v[] = { "exit", "cd", "env", "help", NULL }; builtin_f builtins_f_v[] = { &builtin_exit, &builtin_cd, &builtin_env, &builtin_help, NULL }; for (i = 0; builtins_v[i] != NULL; i++) { if ((string_compare(cmd[0], builtins_v[i])) == 0) { exec_builtin = builtins_f_v[i]; return (exec_builtin(ac, cmd, env)); } } return (-1); }
int exec_course_branch_execute(t_node *root, t_global *global) { int pid; int signal; int status; if (is_builtin(root->content) != -1) { if ((exec_builtin(root, global)) == EXIT_FAILURE) return (EXIT_FAILURE); return (EXIT_SUCCESS); } if ((pid = fork()) == -1) return (EXIT_FAILURE); if (pid == 0) { start_signal(); if ((status = exec_course_branch_select(root, global)) >= EXIT_FAILURE) return (status); } else { wait(&signal); exec_signal(WTERMSIG(signal)); return((WEXITSTATUS(signal) == 0) ? EXIT_SUCCESS : EXIT_FAILURE); } return (EXIT_SUCCESS); }
int nutt_exec_system(const char *cmdStr) { nutt_dbgmsg2("nutt_exec_system",cmdStr); const char *sep = " "; int argc = 0; char *argv[kMaxCmdLineArgs]; //chop up cmdStr by spaces and store in argv array char *pch = strtok(cmdStr, sep); while ((NULL != pch) && (argc < kMaxCmdLineArgs)) { argv[argc++] = pch; pch = strtok(NULL, sep); } char *cmd = argv[0]; nutt_dbgmsg2("nutt_exec_system",cmd); // exec_builtin(FAR const char *appname, FAR const char **argv, // FAR const char *redirfile, int oflags) //TODO handle redirection etc?? int ret = exec_builtin(cmd, (FAR const char **)&argv, NULL, 0); return 0; }
int main(char** argv, int argc){ while(true){ prompt("badsh> "); exec_builtin(); } return 0; }
static int exec_queries(History *hist) { HistEvent ev; size_t c; SQL_STATEMENT *rs; unsigned int cols; unsigned long long rows; for(c = 0; c < pquery_count; c++) { history(hist, &ev, H_ADD, pqueries[c].query); if(pqueries[c].query[0] == '\\') { exec_builtin(sql_conn, hist, pqueries[c].query); continue; } if(!sql_conn) { printf("[08003] Not connected to database server\n"); continue; } rs = sql_query(sql_conn, pqueries[c].query); if(!rs) { printf("[%s] %s\n", sql_sqlstate(sql_conn), sql_error(sql_conn)); return -1; } cols = sql_stmt_columns(rs); if(!cols) { printf("%qu rows affected.\n", sql_stmt_affected(rs)); sql_stmt_destroy(rs); return 0; } if(pqueries[c].output_mode == 'G') { show_results_long(rs); } else { show_results(rs); } rows = sql_stmt_rows(rs); if(rows == 1) { printf("1 row in set.\n"); } else { printf("%qu rows in set.\n", rows); } sql_stmt_destroy(rs); } return 0; }
static void execute_shell(t_shell *sh, CMD *c, int fd_out, int fd_error) { sh->inside_status = 1; if (c->type == C_ERROR) { exec_error(c, fd_error); change_status(sh, 1); } else if (c->type == C_BUILTIN && !is_extern_builtin(c)) exec_builtin(sh, c, fd_out, fd_error); else sh->inside_status = 0; }
int launch_one_cmd(t_cmd *cmd, t_cmd *next) { int ret; connect_file(cmd); connect_pipe(cmd, next); if ((ret = exec_builtin(cmd)) == -1) { return (launch_fork(cmd)); } cmd->return_val = ret; return (0); }
void exec_cmd(char *str) { char **cmd; str = ft_strdup(parse_vars(str)); if (!(cmd = split_cmd(str)) || !cmd[0]) { free_tab(cmd); return ; } if (!exec_file(cmd) && !exec_builtin(cmd)) exec_path(cmd); free(str); free_tab(cmd); }
int built_or_exec(t_exec *exec) { if (exec->cmd->type == CMD && exec->cmd->ok == 1) if ((execve(exec->cmd->path, exec->cmd->tabx, exec->cmd->env)) < 0) { if (exec->pipe == 0) error_cmd(exec->cmd->tabx[0], exec->fdout, 0); else error_cmd(exec->cmd->tabx[0], exec->fd[1], 0); } if (exec->cmd->type == BUILTIN) exec_builtin(exec); if (exec->cmd->ok == -1 && exec->pipe == 1 && exec->endpipe == 1) error_cmd(exec->cmd->tabx[0], exec->fdout, exec->cmd->error); else if (exec->cmd->ok == -1 && exec->pipe == 1) error_cmd(exec->cmd->tabx[0], exec->fd[1], exec->cmd->error); else if (exec->cmd->ok == -1 && exec->pipe == 0) error_cmd(exec->cmd->tabx[0], exec->fdout, exec->cmd->error); return (0); }
void run_minishell2(t_struc *s, char **tab) { int i; char **tab2; i = 0; while (tab[i]) { tab2 = ft_strdoublesplit(tab[i], ' ', '\t'); if (replace_tild(s, &tab2) == -1) break ; s->argc = get_argc(tab2); s->cmd = tab2[0]; if (!s->cmd) break ; s->argv = tab2; if (!ft_strcmp(s->cmd, "exit")) exec_exit(s); g_ctrl_c = 1; !is_builtin(s) ? exec_cmd(s, s->env) : exec_builtin(s, s->env); free(s->cmd); i++; } }
/* Given a CMD structure, executes it accordingly, printing errors as necessary, * and setting the environment variable ? to reflect the exit status of the last * foreground process. * * @command = CMD to be executed * * Returns: exit status of executed instructions */ int process(CMD* command) { if (!command) return 0; if (signal(SIGINT, handler) == SIG_ERR) perror("Csh"); int pid, status; // See if any background processes have died while (num_background_processes > 0 && (pid = waitpid(-1, &status, WNOHANG)) != 0) { num_background_processes--; } int type = command->type; if (type == SIMPLE || type == SUBCMD) { if (is_builtin(command)) { status = exec_builtin(command); set_status(status); return status; } else { if ((pid = fork()) < 0) { perror("Csh"); return 1; } else if (pid == 0) { // In child exec_stage(command); } else { // In parent (pid > 0) foreground_pid = pid; waitpid(pid, &status, 0); foreground_pid = 0; status = exit_status(status); set_status(status); return status; } } } else if (type == PIPE || type == PIPE_ERR) { return process_pipeline(command); } else if (type == SEP_AND) { status = process(command->left); return (status == 0) ? process(command->right) : status; } else if (type == SEP_OR) { status = process(command->left); return (status == 0) ? status : process(command->right); } else if (type == SEP_BG) { if ((pid = fork()) < 0) { perror("Csh"); return 1; } else if (pid == 0) { // In child CMD* left = command->left; if (is_builtin(left)) { status = exec_builtin(left); _exit(status); } else if (left->type == SIMPLE || left->type == SUBCMD) { exec_stage(left); // Need to call directly so it doesn't fork } else { status = process(left); _exit(status); } } else { // In parent (pid > 0) num_background_processes++; return process(command->right); // Repeat with next process } } else if (type == SEP_END) { status = process(command->left); return (is_builtin(command->left) && status) ? status : process(command->right); } else { perror("Csh"); return 1; } return 0; }
/* Executes a pipeline specified by the given CMD structure. * * @pipeline = CMD to be executed * * Returns: exit status of the pipeline (0 or the first non-zero exit status) */ int process_pipeline(CMD* pipeline) { int num_children = 0; // To keep track of how many we need to wait() for int max_num_children = 4; int* child_pids = malloc(max_num_children * sizeof(int)); // NOTE: This leaks memory for child int fdin = 0; // Input descriptor int fd[2]; // File descriptors for pipe() int pid, status; while (ISPIPELINE(pipeline->type)) { int pipeline_type = (pipeline->type == PIPE) ? 1 : 2; if (pipe(fd) || (pid = fork()) < 0) { perror("Csh"); return 1; } else if (pid == 0) { // In child close(fd[0]); if (fdin != 0) { dup2(fdin, 0); close(fdin); } if (fd[1] != pipeline_type) { // Built-in commands disregard input/output redirections, so if // the next command is a built-in, redirect the read end of the // pipe to /dev/null if (is_builtin(pipeline->right)) { int fd_devnull = open("/dev/null", O_WRONLY); dup2(fd_devnull, fd[1]); close(fd_devnull); } // Replace stdout/stderr with write end of the pipe dup2(fd[1], 1); if (ISREDERR(pipeline->toType)) dup2(fd[1], 2); close(fd[1]); } CMD* left = pipeline->left; if (is_builtin(left)) { status = exec_builtin(left); _exit(status); } else { exec_stage(left); } } else { // In Parent child_pids[num_children++] = pid; if (num_children > 1) { close(fdin); } fdin = fd[0]; close(fd[1]); if (num_children >= max_num_children) { max_num_children *= 2; child_pids = realloc(child_pids, max_num_children * sizeof(int)); } pipeline = pipeline->right; } } int pipeline_status = 0; if (is_builtin(pipeline)) { // Don't fork because it's a built-in and should apply to parent pipeline_status = exec_builtin(pipeline); } else { // Fork last process and proceed as usual if ((pid = fork()) < 0) { perror("Csh"); return 1; } else if (pid == 0) { // In child if (fdin != 0) { dup2(fdin, 0); close(fdin); } exec_stage(pipeline); // Guaranteed not to be a builtin } else { // In parent child_pids[num_children++] = pid; if (num_children > 1) { close(fdin); } } } for (int i = 0; i < num_children; i++) { foreground_pid = child_pids[i]; waitpid(child_pids[i], &status, 0); // want wnohang? status = exit_status(status); if (!pipeline_status && status != 0) { pipeline_status = status; } } foreground_pid = 0; free(child_pids); set_status(pipeline_status); return pipeline_status; }
/* Main function */ int main(int argc, char **argv) { buffer_t *command_line; pipeline_t *pipeline; int pid, status, tmp, fd, ret; command_line = new_command_line(); pipeline = new_pipeline(); current_directory = (char*) malloc(1024*sizeof(char)); exeJobs = (s_exeJobs*)malloc(sizeof(s_exeJobs)); exeJobs->jobs = NULL; exeJobs->count = 0; /* Welcome message */ system("clear"); printf("Welcome to BlueShell!\n\n"); while (go_on) { current_directory = getcwd(current_directory, 1024); printf("%s%s%s ", SHELL_NAME, current_directory, PROMPT); fflush(stdout); ret = read_command_line(command_line); fatal(ret < 0, NULL); /* Parse command line. */ if (parse_command_line(command_line, pipeline) == 0) { if (pipeline->ncommands < 1) continue; /* Try to execute built in command */ if (exec_builtin(pipeline->command[0][0], pipeline->command[0])) continue; /* Running group of process in background */ if(RUN_BACKGROUND(pipeline)) { addJobsBack(pipeline); } /* Running group of process in foreground */ if (RUN_FOREGROUND(pipeline)) { /* More than one command */ if (pipeline->ncommands > 1) { /* Create another process and check for fatal error */ pid = fork(); fatal(pid < 0, "Could not create a new process."); /* If parent */ if (pid > 0) { /* Just wait for child to end */ wait(&status); /* If child */ } else { execute_pipeline(pipeline, NULL, pipeline->ncommands - 1); return EXIT_SUCCESS; } /* Only one command was read */ } else { /* Create another process and check for fatal error */ pid = fork(); fatal(pid < 0, "Could not create a new process."); /* If parent */ if (pid > 0) { /* Just wait for child to end */ addJobsFore(pipeline, pid); wait(&status); /* If child */ } else { /* Redirecting input */ if (REDIRECT_STDIN(pipeline)) { tmp = open(pipeline->file_in, O_RDONLY); fatal(tmp < 0, "Could not redirect input: file not found"); /* Close file descriptor 0 which is 'stdin' */ close(0); /* Make a copy of file descriptor opened for input, thus now it is associated with file descriptor 0 that has been just released */ fd = dup(tmp); fatal(fd < 0, "Could not redirect input."); close(tmp); } /* Redirecting output */ if (REDIRECT_STDOUT(pipeline)) { /* '>' Overwrite if file exists, or create a new one */ if (REDIRECT_STDOUT_WRITE(pipeline)) { tmp = open(pipeline->file_out, O_CREAT | O_TRUNC | O_RDWR, S_IRUSR | S_IWUSR); fatal(tmp < 0, "Could not redirect output: failed to create a file"); /* '>>' Append to existing file */ } else if(REDIRECT_STDOUT_APPEND(pipeline)) { tmp = open (pipeline->file_out, O_CREAT | O_APPEND | O_RDWR, S_IRUSR | S_IWUSR); fatal(tmp < 0, "Could not redirect output: failed to append to file"); } /* Close file descriptor 1 which is 'stdout' */ close(1); /* Make a copy of file descriptor opened for output, thus now it is associated with file descriptor 1 that has been just released */ fd = dup(tmp); fatal(fd < 0, "Could not redirect input."); close(tmp); } /* Execute a program */ execvp(pipeline->command[0][0], pipeline->command[0]); /* If program not found */ printf ("%s: command not found\n", pipeline->command[0][0]); return EXIT_FAILURE; } } /* Running group of process in background */ } else { if (REDIRECT_STDIN(pipeline)) { /* TODO */ } if (REDIRECT_STDOUT(pipeline)) { /* TODO */ } } } } release_command_line(command_line); release_pipeline(pipeline); return EXIT_SUCCESS; }