t_exec *set_max_and_out(t_exec *exec, t_dlist *lcmd, int n, int *max) { int redir; int stop; t_node *tmp; t_node *lredir; redir = 0; stop = 0; if ((tmp = cursor_to_n(lcmd, n)) == NULL) return (exec); while (tmp != NULL && stop == 0) { if (tmp->token == SEMICOLON || tmp->token == 0) stop = 1; if (tmp->token == RCHEV || tmp->token == RDCHEV) { exec = is_redir(exec, lcmd, n, tmp->token); redir = 1; lredir = tmp; } stop = set_stop_max_and_out(redir, tmp->token, stop); tmp = tmp->next; n += 1; } *max = end_max_and_out(redir, lredir, n, stop); return (exec); }
char *skip_quotes_nb_arg(char *str, size_t *i, t_cmd *cmd) { size_t start; if (!verif_empty_quote(str, i)) { return (NULL); } start = *i; while (str[*i] && !ft_isspace2(str[*i]) && !is_sep(i, str, 0, cmd)) { if (is_redir(i, str, 0, cmd) && ft_isdigit(str[*i])) { break ; } if (!is_escaped_char(str, *i) && is_quote_open(str[*i])) { get_pos_after_quote(i, str); } (*i)++; } if (start != *i) return ((void*)1); return (NULL); }
static void skip_quotes_replace_string(t_cmd *cmd, char **str, size_t *i) { while ((*str)[*i] && !ft_isspace2((*str)[*i]) && !is_sep(i, *str, 0, cmd)) { if ((*str)[*i] == '\\') { if ((*str)[*i + 1]) *str = delete_char(*str, *i + 1); } else { if ((is_redir(i, *str, 0, cmd) || is_aggr(i, *str, 0)) && ft_isdigit((*str)[*i])) { (*i)++; break ; } if (is_quote_open((*str)[*i]) && !is_escaped_char(*str, *i)) join_inside_quote(i, str); } if ((*str)[*i]) (*i)++; } }
int get_redirection(char *s, char **ifn, char **ofn, int *ofatt) { /* * Gets the redirection info from the command line and copies the * file names into ifn and ofn removing them from the command line. * The names are allocated here and passed back to the caller, on * malloc() failure, -1 is returned. These names are trimmed, * meaning they do not contain any leading or trailing whitespaces. * * Converts remaining command line into a series of null terminated * strings defined by the pipe char '|'. Each string corresponds * to a single executable command. A double null terminates the * command strings. * * Check for, but do not implement, output append redirect. * * Return number of command strings found. * */ int num = 1; int ch; char *dp = s; char *sp = s; assert(s); assert(ifn); assert(ofn); assert(ofatt); /* find and remove all the redirections first */ while ((ch = *dp++ = *sp++) != 0) switch (ch) { case '"': /* No redirects inside quotes */ /* case '\'': single quotes don't quote ska*/ { char *p; int len; /* If there is no closing quote, then go to end of line. */ if ((p = strchr(sp, ch)) == 0) { p = sp + strlen(sp) - 1; } /* closing quote found, move that area */ /* need memmove() because both areas overlap each other */ memmove(dp, sp, len = p - sp + 1); dp += len; sp += len; } break; case '<': case '>': { /* MS-DOS ignores multiple redirection symbols and uses the last */ /* redirection, so we'll emulate that and not check */ char **op = (ch == '<') ? ifn : ofn; char *p; if ((ch == '>') && (*sp == '>')) /* Append request ? */ { *ofatt = O_CREAT | O_APPEND | O_WRONLY; sp++; } p = sp = ltrimcl(sp); while (*sp && !is_redir(*sp) && !isargdelim(*sp)) ++sp; free(*op); /* ignore any previous one */ ch = *sp; *sp = '\0'; if ((*op = strdup(p)) == 0) { /* out of mem */ error_out_of_memory(); return -1; } *sp = ch; --dp; /* ignore the already copied '<' or '>' */ } break; case '|': dp[-1] = '\0'; /* overwrite the already copied '|' */ ++num; break; } /* end switch */ return num; }