t_grp *create_n_process_group(t_sh *shell, char *lign) { char **cmd_line; t_grp *res; t_cmd *tmp_cmd; int i; i = 0; if ((res = malloc(1 * sizeof(t_grp))) == NULL) return (NULL); res->line = lign; init_stdfd_t_def_val(&(res->fd), 0, 1, 2); res->pid.sid = shell->pid.sid; res->pid.pgid = -1; res->cmds = NULL; res->flags = 0; parse_redirection(res, lign); if ((cmd_line = str_to_wordtab(res->line, "|", 1)) != NULL) while (cmd_line[i] != NULL) { tmp_cmd = create_n_cmd(shell, cmd_line[i]); res->cmds = (t_cmd**)add_ptr_t_tab((void**)res->cmds, (void*)tmp_cmd); i++; } free(cmd_line); return (res); }
/* parse program arguments (builtins are also parsed this way first) */ static void parse_prog(cmds* cmd, prog_args* prog) { switch(lookahead.kind) { /* end of program arguments? */ case AMP: prog->background=true; case SEP: case END: case STROKE: return; /* redirections? */ case IN: case OUT: parse_redirection(prog); break; /* argument? */ case IDE: argv_add(prog,get_ide()); break; default: raise_error(PARSER_INVALID_STATE); } /* parse next program argument */ scan(); parse_prog(cmd,prog); }
int main(int argc, char** argv) { //.......... int ret = MYSHELL_CMD_OK; cmd mycmd; char* readlineptr, * tampon; struct passwd* infos; char str[1024]; char hostname[256]; char workingdirectory[256]; rl_bind_key ('\t', rl_complete);/*initialisation de l'auto complétion*/ using_history(); //.......... while(ret != MYSHELL_FCT_EXIT) { infos=getpwuid(getuid()); gethostname(hostname, 256); getcwd(workingdirectory, 256); sprintf(str, "\n{myshell}%s@%s:%s$ ", infos->pw_name, hostname, workingdirectory); readlineptr = readline(str); if (strstr(readlineptr, "exit") != NULL) { ret = MYSHELL_FCT_EXIT; } else { tampon = (char *)malloc((strlen(readlineptr) + 1) * sizeof(char)); if (tampon == NULL) { exit(-1); } strcpy(tampon, readlineptr); add_history(tampon); //.......... parse_membres(readlineptr, &mycmd); parse_args(&mycmd); parse_redirection(&mycmd); exec_commande(&mycmd); //.......... //aff_membres(&mycmd); //aff_args(&mycmd); //aff_redirection(&mycmd); free(readlineptr); free_membres(&mycmd); free_args(&mycmd); free_redirection(&mycmd); //.......... } } clear_history(); //.......... return 0; }
struct watchman_trigger_command *w_build_trigger_from_def( w_root_t *root, json_t *trig, char **errmsg) { struct watchman_trigger_command *cmd; json_t *ele, *query; json_int_t jint; const char *name = NULL; cmd = calloc(1, sizeof(*cmd)); if (!cmd) { *errmsg = strdup("no memory"); return NULL; } cmd->definition = trig; json_incref(cmd->definition); query = json_pack("{s:O}", "expression", json_object_get(cmd->definition, "expression")); cmd->query = w_query_parse(query, errmsg); json_decref(query); if (!cmd->query) { w_trigger_command_free(cmd); return NULL; } json_unpack(trig, "{s:s}", "name", &name); if (!name) { *errmsg = strdup("invalid or missing name"); w_trigger_command_free(cmd); return NULL; } cmd->triggername = w_string_new(name); cmd->command = json_object_get(trig, "command"); if (cmd->command) { json_incref(cmd->command); } if (!cmd->command || !json_is_array(cmd->command) || !json_array_size(cmd->command)) { *errmsg = strdup("invalid command array"); w_trigger_command_free(cmd); return NULL; } json_unpack(trig, "{s:b}", "append_files", &cmd->append_files); ele = json_object_get(trig, "stdin"); if (!ele) { cmd->stdin_style = input_dev_null; } else if (json_is_array(ele)) { cmd->stdin_style = input_json; if (!parse_field_list(ele, &cmd->field_list, errmsg)) { w_trigger_command_free(cmd); return NULL; } } else if (json_is_string(ele)) { const char *str = json_string_value(ele); if (!strcmp(str, "/dev/null")) { cmd->stdin_style = input_dev_null; } else if (!strcmp(str, "NAME_PER_LINE")) { cmd->stdin_style = input_name_list; } else { ignore_result(asprintf(errmsg, "invalid stdin value %s", str)); w_trigger_command_free(cmd); return NULL; } } else { *errmsg = strdup("invalid value for stdin"); w_trigger_command_free(cmd); return NULL; } jint = 0; // unlimited unless specified json_unpack(trig, "{s:I}", "max_files_stdin", &jint); if (jint < 0) { *errmsg = strdup("max_files_stdin must be >= 0"); w_trigger_command_free(cmd); return NULL; } cmd->max_files_stdin = jint; json_unpack(trig, "{s:s}", "stdout", &cmd->stdout_name); json_unpack(trig, "{s:s}", "stderr", &cmd->stderr_name); if (!parse_redirection(&cmd->stdout_name, &cmd->stdout_flags, "stdout", errmsg)) { w_trigger_command_free(cmd); return NULL; } if (!parse_redirection(&cmd->stderr_name, &cmd->stderr_flags, "stderr", errmsg)) { w_trigger_command_free(cmd); return NULL; } // Copy current environment cmd->envht = w_envp_make_ht(); // Set some standard vars w_envp_set(cmd->envht, "WATCHMAN_ROOT", root->root_path); w_envp_set_cstring(cmd->envht, "WATCHMAN_SOCK", get_sock_name()); w_envp_set(cmd->envht, "WATCHMAN_TRIGGER", cmd->triggername); return cmd; }