static t_hs format_pwd(t_hs pwd, t_hs home) { t_hs rel_path; if (hs_starts_with(pwd, home)) { rel_path = hs_slice(pwd, hs_length(home), hs_length(pwd)); return (hs_concat_char_hs('~', rel_path)); } return (pwd); }
int read_file(const t_hs filename, t_glist_hs *lines) { char *data; int fd; ssize_t file_length; t_hs ths; t_glist_hs new_lines; if ((file_length = get_file_length(filename)) == -1) return (-1); if ((fd = open_file(filename, O_RDONLY)) == -1) return (-1); data = egc_malloc_atomic(file_length + 1); if (read(fd, data, file_length) == -1) { close(fd); return (-1); } close(fd); ths = hs(data); if ((int)hs_length(ths) != file_length) return (-1); new_lines = hs_split(ths, hs("\n")); glist_hs_append_all(lines, &new_lines); return (0); }
static t_parser_result parse_pipeline_end(t_node *first, t_token_list **list_pointer, t_redir *redir) { t_node *list; t_parser_result result; t_token *token; list = node_new(NODE_PIPE, redir); list->children = glist_voidp_new(); glist_voidp_append(&list->children, first); while (*list_pointer) { token = parse_token_type_impl(list_pointer, TOKEN_TYPE_PIPE); if (!token) break ; result = parse_command(list_pointer); if (hs_length(result.error)) return (result); if (!result.node) break ; glist_voidp_append(&list->children, result.node); } return (NODE(list)); }
status hs_replace(hstring *pstr,hstring s1,hstring s2) { /* 初始条件: 串pstr, s1和s2存在,s1是非空串(此函数与串的存储结构无关) */ /* 操作结果: 用s1替换主串pstr中出现的所有与s1相等的不重叠的子串 */ int i=1; /* 从串S的第一个字符起查找串s1 */ if(hs_empty(s1)) /* s1是空串 */ return ERROR; do { i=hs_find(*pstr,s1,i); /* 结果i为从上一个i之后找到的子串T的位置 */ if(i) /* 串pstr中存在串s1 */ { hs_delete(pstr, i, hs_length(s1)); /* 删除该串s1 */ hs_insert(pstr, i, s2); /* 在原串s1的位置插入串s2 */ i += hs_length(s2); /* 在插入的串s2后面继续查找串s1 */ } } while(i); return OK; }
int hs_find(hstring s1,hstring s2,int pos) { /* s2为非空串。若主串s1中第pos个字符之后存在与s2相等的子串, */ /* 则返回第一个这样的子串在s1中的位置,否则返回0 */ int n,m,i; hstring sub; hs_init(&sub); if(pos>0){ n=hs_length(s1); m=hs_length(s2); i=pos; while(i <= (n-m+1)){ hs_substring(&sub, s1, i, m); if(hs_compare(sub, s2)!=0) ++i; else return i; } } return 0; }
void readline_update_cursor(const t_readline *readline) { int new_pos; new_pos = hs_length(readline->line); while (new_pos != readline->cursor_index) { egc_printf("%s", readline->capacity.capacity_cursor_left); new_pos--; } }
int parse_word_or_string(t_token_list **list_pointer, t_hs *result, t_redir *redir) { t_token_result tr; tr = parse_word_or_string_token(list_pointer, redir); if (TOKEN_RESULT_IS_NULL(tr)) { *result = hs(""); return (0); } if (hs_length(tr.error)) { *result = tr.error; return (-1); } *result = parser_get_word(tr.token); return (0); }
/* ** Returns a token, an error or TOKEN_RESULT_NULL. */ t_token_result parse_word_or_string_token(t_token_list **list_ptr, t_redir *redir) { t_token_result result; t_token_list *begin; t_redir begin_redir; begin_redir = *redir; begin = *list_ptr; result = parse_token(list_ptr, redir); if (hs_length(result.error) || !result.token) return (result); if (!parser_is_word_or_string(result.token)) { *redir = begin_redir; *list_ptr = begin; return (TOKEN_RESULT_NULL); } return (result); }
t_parser_result parse_pipeline(t_token_list **list_pointer) { t_parser_result result; t_redir redir; t_node *first; t_hs error; parser_redir_init(&redir); result = parse_command(list_pointer); first = result.node; if (!first) return (result); result = parse_pipeline_end(first, list_pointer, &redir); if (!result.node) return (result); if (glist_voidp_length(&result.node->children) == 1) { return (NODE(first)); } if (hs_length(error = parser_setup_pipeline(result.node))) return (ERROR(error)); return (result); }