static void handle_else_tilde(char *home, char **new_s, char **s) { char *tmp; int i; i = 0; while (**s && !ft_strchr("'\"", **s)) { if (ft_strchr(" \t<|>", **s) && *(*s + 1) == '~' && ft_strchr("/ \t<|>", *(*s + 2))) { i++; (*s)++; tmp = ft_strndup(*s - i, i); tilde_new_str(home, new_s, tmp, 0); tilde_new_str(home, new_s, NULL, 1); ft_strdel(&tmp); i = -1; } i++; (*s)++; } tmp = ft_strndup(*s - i, i); tilde_new_str(home, new_s, tmp, 0); ft_strdel(&tmp); }
static int put_next_line(char **line, char *buf, t_line *elem, int ret) { size_t i; TMP2 = ft_strjoin(TMP, buf); free(TMP); TMP = ft_strdup(TMP2); free(TMP2); i = 0; while (TMP[i] != '\n' && TMP[i]) i++; if (TMP[i] == '\n') { *line = ft_strndup(TMP, i); TMP2 = ft_strdup(TMP); free(TMP); TMP = ft_strsub(TMP2, i + 1, ft_strlen(TMP2)); free(TMP2); return (ret < BUFF_SIZE && !TMP ? 0 : 1); } if (TMP[i] == '\0' && ret < BUFF_SIZE) { *line = ft_strndup(TMP, i); ft_bzero(TMP, ft_strlen(TMP)); return (i ? 1 : 0); } return (0); }
char *ft_read_line(char *ptr, int *retour) { static int cmpt = 0; int cmpt2; printf("%d", cmpt); ft_putstr(ptr); cmpt2 = 0; *retour = 1; while (ptr[cmpt] != '\0') { if (ptr[cmpt] == '\n') { return (ft_strndup(&ptr[cmpt - cmpt2], cmpt2)); } cmpt2++; cmpt++; } if (ptr[cmpt] == '\0') { *retour = 0; return (ft_strndup(&ptr[cmpt - cmpt2], cmpt2)); } *retour = -1; return (NULL); }
static char *ft_search_path(char *str, int i) { int end; char *path; char *tmp; path = NULL; while (i > 0 && ft_isspace(str[i]) && str[i - 1] != '\\') i--; while (i > 0 && (!ft_isspace(str[i]) || (ft_isspace(str[i]) && str[i - 1] == '\\'))) i--; end = (i > 0 ? ++i : i); while (str[end] && (!ft_isspace(str[end]) || (ft_isspace(str[end]) && str[end - 1] == '\\'))) end++; path = ft_strnrchr(str + i, '/', end - i); if (str[i] == '/') path = ft_strndup(str + i, path - (str + i) + 1); else { tmp = ft_strndup(str + i, path - (str + i) + 1); path = ft_strjoin("./", path ? tmp : ""); ft_strdel(&tmp); } return (path); }
static int ft_fill_line(char **line, char **buf, int ret) { int n; if (ret == -1) *buf = NULL; if (ret == -1) return (-1); if (ret == 0 && *buf[0] == '\0') { *buf = NULL; *line = ft_strdup("\0"); return (0); } n = ft_strcchr(*buf, '\n'); if (n > -1) { *line = ft_strndup(*buf, n); *buf += n + 1; return (1); } else { *line = ft_strndup(*buf, ft_strlen(*buf)); ft_strclr(*buf); return (1); } }
static char *ft_get_name(char *str, int i) { char *name; char *tmp; int end; name = NULL; while (i > 0 && ft_isspace(str[i]) && str[i - 1] != '\\') i--; while (i > 0 && (!ft_isspace(str[i]) || (ft_isspace(str[i]) && str[i - 1] == '\\'))) i--; end = (i > 0 ? ++i : i); while (str[end] && (!ft_isspace(str[end]) || (ft_isspace(str[end]) && str[end - 1] == '\\'))) end++; tmp = ft_strnrchr(str + i, '/', end - i); if (!tmp) name = ft_strndup(str + i, end - i); else { i = tmp - str; name = ft_strndup(str + i + 1, end - i); } return (name); }
static int endl_in_remain(char **line, char **remain, int fd, char *buf) { if (**line) *line = ft_strjoin(ft_strdup(*line), ft_strndup(remain[fd], ft_strchr(remain[fd], '\n') - remain[fd])); else *line = ft_strndup(remain[fd], ft_strchr(remain[fd], '\n') - remain[fd]); remain[fd] = ft_strjoin(ft_strdup(ft_strchr(remain[fd], '\n') + 1), ft_strdup(buf)); return (*line && remain[fd] ? 1 : -1); }
static void ft_other(t_lexer **root, char *str, int i, int j) { ft_add_lexer(root, ft_strndup(str, i), 0, 5); if (str[i] && str[i + 1] && str[i] == str[i + 1] && g_t[j].type <= 7) { j++; ft_add_lexer(root, ft_strndup(str + i++, 2), g_t[j].type, g_t[j].rank); } else if (str[i] && g_t[j].type > 0) ft_add_lexer(root, ft_strndup(str + i, 1), g_t[j].type, g_t[j].rank); if (str[i] && *(str + i + 1)) ft_lexer(root, str + i + 1); }
char *basename(char const *file_path_full) { size_t size; size = ft_strlen(file_path_full); while (file_path_full[size] != '.' && size != 0) size--; if (file_path_full[size] == '.' && size != 0 && file_path_full[size - 1] != '/') return (ft_strndup(file_path_full, size)); else return (ft_strndup(file_path_full, size)); }
char *ft_getenv(char **env, char *elem) { char *uppername; char *ptr; char *value; int i; int len; i = -1; value = NULL; len = ft_strlen(elem); uppername = ft_strdup(elem); ft_strupper(uppername); while (env[++i]) { if (!ft_strncmp(env[i], elem, len) || !ft_strncmp(env[i], uppername, len)) { if ((ptr = ft_strchr(env[i], '='))) value = ft_strndup(ptr + 1, ft_strlen(ptr)); } } free(uppername); return (value); }
static int get_env_variable(t_expander *exp, char *s) { t_env_list *env_var; int len; t_bsh *const bsh = get_shell_data(); len = 0; while (ft_isalnum(*(s + len)) || *(s + len) == '_') len++; if (!len) return (append(exp)); if (!(s = ft_strndup(exp->tmp + 1, len))) return (MALLOC_FAIL); if ((env_var = ft_getenv(bsh->env.env_list, s))) { exp->buffer_len += ft_strlen(env_var->value); while (exp->buffer_len >= exp->buffer_size) if (realloc_exp_buffer(exp) == MALLOC_FAIL) { ft_strdel(&s); return (MALLOC_FAIL); } ft_strcat(exp->buffer, env_var->value); } ft_strdel(&s); exp->tmp += len; return (0); }
t_cmd *shell_parser_redirection(t_cmd *cmd, char **p, char *end) { int tok; char *q; char *eq; char *f; while (shell_parser_helper_strings_whitespaces(p, end, "12<>")) { if ((**p == '1' || **p == '2') && (*p)[1] != '>' && (*p)[1] != '<') break ; tok = tokenizer(p, end, 0, 0); tokenizer(p, end, &q, &eq); if ((f = ft_strndup(q, eq - q + 1)) && tok == '<') cmd = shell_parser_new_redirection_cmd(cmd, f, M_READ, 0); if (tok == '>') cmd = shell_parser_new_redirection_cmd(cmd, f, M_WRITE_TRUNC, 1); if (tok == '+') cmd = shell_parser_new_redirection_cmd(cmd, f, M_WRITE_APPEND, 1); if (tok == '=') cmd = shell_parser_new_redirection_cmd(cmd, f, M_READ_APPEND, 0); if (tok == '*') cmd = shell_parser_new_redirection_cmd(cmd, f, M_WRITE_TRUNC, 2); if (tok == '/') cmd = shell_parser_new_redirection_cmd(cmd, f, M_WRITE_APPEND, 2); } return (cmd); }
char *ft_ltoa_base(long long n, long long base) { char *str; char *ret; int x; x = 0; if (!(str = (char *)ft_memalloc(sizeof(char) * 100))) return (NULL); while (n / base) { if (base > 10 && ft_abs(n % base) > 9) str[x] = ft_abs(n % base) - 10 + 'a'; else str[x] = ft_abs(n % base) + '0'; n = n / base; x++; } if (base > 10 && ft_abs(n % base) > 9) str[x] = ft_abs(n % base) - 10 + 'a'; else str[x] = ft_abs(n % base) + '0'; if (n < 0 && base == 10) str[++x] = '-'; ret = ft_strndup(str, x + 2); free(str); return (ft_strrev(ret)); }
int ft_gnl(const int fd, char **line) { static char **remain = NULL; char *endl; int exitval; char *tmp; int ret; if (init_gnl(fd, line, &remain)) return (-1); tmp = remain[fd]; if ((endl = (tmp == NULL ? NULL : ft_strchr(tmp, '\n'))) == NULL) { exitval = rec_gnl(fd, line, remain, (tmp != NULL ? ft_strlen(tmp) : 0)); if (tmp != NULL) { ft_memcpy(*line, tmp, ft_strlen(tmp)); free(tmp); } return (exitval); } ret = endl - remain[fd]; *line = ft_strndup(remain[fd], ret); remain[fd] = ft_strdup(endl + 1); if (tmp != NULL) free(tmp); return (ret + 1); }
char *get_v(char *env, int start) { char *tmp; tmp = env + (start + 1); return (ft_strndup(tmp, ft_strlen(tmp))); }
int get_next_line(int const fd, char **line) { char buf[BUFF_SIZE + 1]; int ret; static t_list *list = NULL; t_list *begin; char *l; if (fd < 0 || line == NULL || read(fd, buf, 0) < 0) return (-1); begin = list; list = ft_findfd(&begin, fd); while (!ft_strchr(list->content, '\n') && (ret = read(fd, buf, BUFF_SIZE))) list->content = ft_freejoin(list->content, buf, ret); ret = 0; while (((char *)list->content)[ret] && ((char *)list->content)[ret] != '\n') ++ret; *line = ft_strndup(list->content, ret); if (((char *)list->content)[ret] == '\n') ++ret; l = list->content; list->content = ft_strdup(list->content + ret); free(l); list = begin; return (ret ? 1 : 0); }
char *ft_strtrim(char const *s) { char *str; size_t len; size_t n; if (!s) return (NULL); n = 0; while (*s == ' ' || *s == '\n' || *s == '\t') s++; if (*s == '\0') { str = (char*)malloc(sizeof(*str)); str[0] = '\0'; return (str); } len = ft_strlen(s); while (s[len - 1] == ' ' || s[len - 1] == '\n' || s[len - 1] == '\t') len--; str = ft_strndup(s, len); if (!str) return (NULL); str[len] = '\0'; return (str); }
t_list *ft_lstsplit(char const *s, char c) { t_list *lst_begin; char *str; char *tmp; int x; if (!s) return (NULL); str = (char *)s; lst_begin = NULL; while (*str) { x = 0; while (*str == c && *str) str++; if (!*str) break ; while (str[x] != c && str[x]) x++; tmp = ft_strndup(str, x); ft_lstaddend(&lst_begin, ft_lstnew(tmp, ft_strlen(tmp) * sizeof(char))); str = &str[x]; } return (lst_begin); }
static int parse_key_four(int i, size_t key, t_cmds **cmd, t_shell *shell) { if (key == 19) { if ((*cmd)->cp == -1) { (*cmd)->cp = 0; (*cmd)->txt_cp = ft_strnew(1); (*cmd)->txt_cp[0] = (*cmd)->cmd[(*cmd)->pointer]; } else { (*cmd)->cp = -1; edit_cmd_cp(*cmd); } } else if (key == 24) { if (!(*cmd)->txt_cp && (*cmd)->up && (*cmd)->up->txt_cp) (*cmd)->txt_cp = (*cmd)->up->txt_cp; while ((*cmd)->txt_cp && (*cmd)->txt_cp[++i]) process_key(*cmd, (*cmd)->txt_cp[i],\ ft_strndup((*cmd)->txt_cp + i, 1), shell); } else return (parse_key_five(key, cmd, shell)); return (1); }
t_cmd *shell_parser_exec(char **p_input, char *end, int *res) { char *new_cmd; char *new_cmd_end; int tok; t_exec_cmd *cmd; t_cmd *ret; ret = shell_parser_new_exec_cmd(); cmd = (t_exec_cmd *)ret; cmd->argv = NULL; ret = shell_parser_redirection(ret, p_input, end); while (*p_input < end) { if ((tok = tokenizer(p_input, end, &new_cmd, &new_cmd_end)) == 0) break ; if (tok != 'a' && (*res = 0)) return (NULL); ft_lstadd_back(&cmd->argv, ft_lstnew( ft_strndup(new_cmd, new_cmd_end - new_cmd), new_cmd_end - new_cmd + 1)); ret = shell_parser_redirection(ret, p_input, end); } return (ret); }
int get_next_line(int const fd_const, char **line) { static t_gnl gnl[64] = {{{0}, 0, 0, 0}}; t_norme n; if (!(n.n = NULL) && (!line || fd_const < 0)) return (-1); n.f = fd_const % 64; *line = NULL; while (!n.n) { if (gnl[n.f].i == gnl[n.f].r && !(gnl[n.f].i = 0)) { if ((gnl[n.f].r = read(fd_const, gnl[n.f].buf, BUFF_SIZE)) <= 0) { n.t = gnl[n.f].r; gnl[n.f].r = 0; return (*line ? 1 : n.t); } } n.n = ft_memchr(&gnl[n.f].buf[gnl[n.f].i], 10, gnl[n.f].r - gnl[n.f].i); n.l = (n.n) ? n.n - &gnl[n.f].buf[gnl[n.f].i] : gnl[n.f].r - gnl[n.f].i; *line = (*line) ? ft_strnjoin(*line, &gnl[n.f].buf[gnl[n.f].i], n.l, &gnl[n.f].s) : ft_strndup(&gnl[n.f].buf[gnl[n.f].i], n.l, &gnl[n.f].s); gnl[n.f].i = (n.n) ? gnl[n.f].i + n.l + 1 : gnl[n.f].r; } return (1); }
int get_next_line(const int fd, char **line) { char *temp; char *ptr; int ret; static t_file file; if (file.fd != fd) { file.fd = fd; ft_memdel((void**)&file.data); } if ((ret = get_buff(fd, &file.data)) == -1) return (ret); if (file.data == NULL) return (0); if (ft_strlen(file.data) == 0) return (0); ptr = ft_strchr(file.data, '\n'); *line = ft_strndup(file.data, ptr - file.data); temp = ft_strsub(file.data, ptr - file.data + 1, ft_strlen(file.data)); ft_memdel((void**)&file.data); file.data = temp; return (1); }
void execute_back_quote(t_var *var, char **arg) { pid_t father; int status; int fd[2]; int saved_fds[2]; save_std_fds(saved_fds); pipe(fd); father = fork(); if (!father) { close(fd[0]); dup2(fd[1], 1); var->line.s = ft_strndup((*arg) + 1, ft_strlen(*arg) - 2); var->list = NULL; proceed_to_execution(var); exit(0); } else { waitpid(father, &status, 0); close(fd[1]); fill_argument(fd[0], arg); close(fd[0]); restore_std_fds(saved_fds); } }
char *ft_con_nothing(char **format, int d, char *(*f_tab[S_TAB_FLAG])(char *, int, char *)) { char *str; char *fr; d = 0; while (1) { if (*(*format - d) == '%') break ; ++d; } fr = *format - d + 1; while ((**format == ' ' || ft_isdigit(**format) || **format == '.' || **format == '-' || **format == 'h' || **format == 'l' || **format == 'z' || **format == 'j') && **format && **format != '%') ++*format; str = ft_strndup(*format, 1); if (f_tab[DIGIT]) str = f_tab[DIGIT](fr, 0, str); if (f_tab[MINUS]) str = f_tab[MINUS](fr, d, str); if (f_tab[ZERO]) str = f_tab[ZERO](fr, d, str); g_nbr += ft_strlen(str); return (str); }
char *format_path(char *path) { int i; char *tmp; if (path) { i = ft_strlen(path) - 1; if (path[i] == '/') return (NULL); else { tmp = ft_strdup(path); while (tmp[i] != '/' && i > 0) i--; if (i == 0) tmp = ft_strdup("./"); else tmp = ft_add_slash(ft_strndup(path, i)); return (tmp); } } else return (NULL); }
void lex_quote(t_var *var, char **buf, t_list **alst) { char *s; t_list *tmp; int i; i = 1; LN_QUOTE = **buf; s = *buf; while (*(*buf + i) && !is_valid_quote(var, *buf, i)) i++; if (!*(*buf + i)) missing_quote_loop(var, s, alst); else { tmp = ((*(*buf + i) == LN_QUOTE) && (i >= 1) ? \ ft_lstnew((void *)ft_strndup(s, i + 1), sizeof(char *)) : NULL); while (*(*buf + i) != LN_QUOTE) i++; i++; *buf += i; ft_lstadd_last(alst, tmp); } LN_QUOTE = '\0'; }
int asm_syntax_param(char **str, int line, t_btcode *btcode, int j) { char *tmp; int i; int token; tmp = *str; token = 0; i = asm_syntax2_param(*str, tmp, line); while (tmp[i] && (ft_isalnum(tmp[i]) || (tmp[i] == '-' && ++token))) i++; if (token > 1) asm_put_error_line(*str, line); token = 0; asm_set_cmd_param(btcode, ft_strndup(*str, i), j); while (tmp[i] && (tmp[i] == ' ' || tmp[i] == '\t')) i++; if (tmp[i] != ',' && tmp[i] != '\0') asm_put_error_line(*str, line); if (tmp[i] == ',' && ++token) i++; while (tmp[i] && (tmp[i] == ' ' || tmp[i] == '\t')) i++; if (token && tmp[i] == '\0') asm_put_error_line("ENDLINE", line); return (i); }
int check_for_closing_quote(t_var *var, char **tmp, t_list **alst) { int i; char *del[2]; t_list *el; i = 0; while (LN_S[i] && !is_valid_quote(var, LN_S, i)) i++; if (!LN_S[i]) return (0); PROMPT_LEN = PROMPT_LEN_VALUE; i++; del[0] = *tmp; *tmp = ft_strjoin(*tmp, "\n"); ft_strdel(&del[0]); del[0] = *tmp; del[1] = ft_strndup(LN_S, i - 1); *tmp = ft_strjoin(*tmp, del[1]); ft_strdel(&del[0]); ft_strdel(&del[1]); el = ft_lstnew((void *)ft_strdup(*tmp), sizeof(char *)); ft_strdel(tmp); LN_S += i; ft_lstadd_last(alst, el); move_to_next_line(var); return (1); }
static int ft_join_keep(char **line, char *buf) { char *p; char *tmp; char *tmp2; if ((p = ft_strchr(buf, '\n'))) { tmp = ft_strndup(buf, (p - buf)); tmp2 = ft_strjoin(*line, tmp); free(*line); *line = ft_strsub(tmp2, 0, ft_strlen(tmp2)); free(tmp2); p++; ft_strcpy(buf, p); free(tmp); return (1); } else { tmp = ft_strjoin(*line, buf); free(*line); *line = ft_strsub(tmp, 0, ft_strlen(tmp)); free(tmp); ft_strclr(buf); return (0); } }
static char *get_newdir(t_params *p, char *buf) { char *new_dir; char *tmp; if (!ft_strncmp(buf, "./", 2)) new_dir = ft_strjoin(p->pwd, ft_strchr(buf, '/')); else if (buf[0] == '/') new_dir = ft_strdup(buf); else if (!ft_strcmp(buf, "-")) new_dir = ft_strdup(p->oldpwd); else if (!ft_strcmp(buf, "..")) new_dir = ft_strlen(p->pwd) - ft_strlen(ft_strrchr(p->pwd, '/')) ? ft_strndup(p->pwd, ft_strlen(p->pwd) - ft_strlen(ft_strrchr(p->pwd, '/')) - 1) : ft_strdup("/"); else { tmp = p->pwd[ft_strlen(p->pwd) - 1] != '/' ? ft_strjoin(p->pwd, "/") : NULL; new_dir = tmp != NULL ? ft_strjoin(tmp, buf) : ft_strjoin(p->pwd, buf); ft_strdel(&tmp); } return (new_dir); }