static void push_arry_container(Cmdline *cmdline, Token *headref, Token *token) { Token *next = (Token*)utarray_next(cmdline->tokens, token); if (!next || token->end != next->start) return; char *str = token_val(next, VAR_STRING); List *args = token_val(headref, VAR_LIST); /* add index as count of items */ if (str[0] == '[') utarray_push_back(cmdline->arys, &utarray_len(args->items)); }
static void ex_update() { log_msg("EXCMD", "check_update"); if ((ex.state & EX_HIST)) return menu_update(ex.menu, &ex.cmd); int cur = ex.curofs - 1; int pos = compl_cur_pos(); char ch = ex_cmd_curch(); bool root = compl_isroot(); Token *tok = ex_cmd_curtok(); char *str = token_val(tok, VAR_STRING); bool quote = ((str && (*str == '\'' || *str == '\"')) || (tok && tok->quoted)); if (ch == '|' && !quote && (pos < cur || !root)) { menu_restart(ex.menu); ex.state = 0; } else if (ch == '!' && !quote && root && pos == cur) { ex.state &= EX_EXEC; return compl_set_exec(ex.curofs); } menu_update(ex.menu, &ex.cmd); }
char* ex_cmd_curstr() { Token *tok = ex_cmd_curtok(); if (tok) return token_val(tok, VAR_STRING); return ""; }
void* list_arg(List *lst, int argc, char v_type) { if (!lst || utarray_len(lst->items) < argc) return NULL; Token *word = (Token*)utarray_eltptr(lst->items, argc); return token_val(word, v_type); }
void ex_cmdinvert() { int st = compl_root_pos(); int ed = st; while (ex.line[ed++] == ' '); //FIXME: find compl_root_pos for curs position Token *cur = cmdline_tokbtwn(&ex.cmd, st, ed); Token *tok = (Token*)utarray_next(ex.cmd.tokens, cur); char *symb = token_val(tok, VAR_STRING); if (!cur) return; if (symb && *symb == '!') { str_ins(ex.line, "", tok->start, 1); ex.curofs--, ex.curpos--; } else { str_ins(ex.line, "!", cur->end, 0); ex.curofs++, ex.curpos++; } int old = ex.curofs; ex.curofs = 0; menu_rebuild(ex.menu); ex.curofs = old; }
int pipe_parsing(char *str) { char **tab_pipe; int ret; int tab_len; if (!valid_str(str) || !last_verif(str)) return (0); if (in_str(str, token_val("|")) && (tab_pipe = str_to_wordtab(str, token_val("|")))) { tab_len = count_tab(tab_pipe); ret = exec_pipe(tab_pipe, tab_len); free(tab_pipe); } else ret = parse_command_redir(str); return (ret); }
int tokenlist_from_lua(lua_State * L) { const char *s; int tok, t; size_t i, j; halfword p, q, r; r = get_avail(); token_info(r) = 0; token_link(r) = null; p = r; t = lua_type(L, -1); if (t == LUA_TTABLE) { j = lua_rawlen(L, -1); if (j > 0) { for (i = 1; i <= j; i++) { lua_rawgeti(L, -1, (int) i); tok = token_from_lua(L); if (tok >= 0) { store_new_token(tok); } lua_pop(L, 1); }; } return r; } else if (t == LUA_TSTRING) { s = lua_tolstring(L, -1, &j); for (i = 0; i < j; i++) { if (s[i] == 32) { tok = token_val(10, s[i]); } else { int j1 = (int) str2uni((const unsigned char *) (s + i)); i = i + (size_t) (utf8_size(j1) - 1); tok = token_val(12, j1); } store_new_token(tok); } return r; } else { free_avail(r); return null; } }
static void make_new_token(lua_State * L, int cmd, int chr, int cs) { int tok = 0; lua_token *thetok = lua_newuserdata(L, sizeof(lua_token)); thetok->origin = LUA_ORIGIN; fast_get_avail(thetok->token); tok = (cs ? cs_token_flag + cs : token_val(cmd, chr)); set_token_info(thetok->token, tok); lua_get_metatablelua(luatex_token); lua_setmetatable(L, -2); }
static void check_flags(Cmdline *cmdline, Cmdstr *cmd) { Token *fst = (Token*)utarray_front(cmdline->tokens); char *exstr = token_val(fst, VAR_STRING); if (!exstr || cmd_conditional()) return; if (exstr[0] == '!') cmd->exec = true; }
static Token* valid_var(Cmdline *cmdline, Token *token, char ch) { Token *next = (Token*)utarray_next(cmdline->tokens, token); char *str = token_val(next, VAR_STRING); if (!str) return token; if (next->start != token->end) return token; if (!strpbrk(str, EXPANCHARS)) if (strpbrk(str, TOKENCHARS)) return token; next->symb = ch; return next; }
void cmdline_push_var(Cmdline *cmdline, Token *token) { Token *word = token; if (word->quoted) return; if (word->var.v_type == VAR_PAIR) { Pair *p = token_val(word, VAR_PAIR); if (p->scope) word = &p->value; else word = &p->key; } if (word->var.v_type == VAR_STRING) utarray_push_back(cmdline->vars, token); }
static int run_scan_csname(lua_State * L) { unsigned char *s; int t; saved_tex_scanner texstate; save_tex_scanner(texstate); get_next(); t = (cur_cs ? cs_token_flag + cur_cs : token_val(cur_cmd, cur_chr)); if (t >= cs_token_flag && ((s = get_cs_text(t - cs_token_flag)) != (unsigned char *) NULL)) { if (is_active_string(s)) lua_pushstring(L, (char *) (s + 3)); else lua_pushstring(L, (char *) s); } else { lua_pushnil(L); } unsave_tex_scanner(texstate); return 1; }
Token* container_elem(Token *ary, List *args, int fst, int lst) { log_msg("CMDLINE", "access_container"); Token *get = ary; for (int i = fst; i < lst; i++) { List *accessor = list_arg(args, i, VAR_LIST); if (!accessor || utarray_len(accessor->items) > 1) return NULL; int index = -1; if (!str_num(list_arg(accessor, 0, VAR_STRING), &index)) return NULL; List *ret = token_val(get, VAR_LIST); if (!ret) return NULL; get = tok_arg(ret, index); } return get; }
int init_parsing(char **buf) { int i; int ret; char **tab; i = 0; ret = first_parsing(buf, 0); if (ret < 0) return (0); if (!check_parenth(*buf)) return (0); tab = str_to_wordtab(*buf, token_val(SEMICOLON)); if (!tab) return (0); ret = 0; while (tab[i]) ret = logic_parse(tab[i++]); free(tab); return (ret); }
static bool seek_ahead(Cmdline *cmdline, QUEUE *stack, Token *token) { Token *next = (Token*)utarray_next(cmdline->tokens, token); if (!next || stack_head(stack)->var.v_type != VAR_LIST) return false; char *str = token_val(next, VAR_STRING); if (str && str[0] == ':') { push(pair_new(cmdline), stack, token->start); Pair *p = stack_head(stack)->var.vval.v_pair; if (cmdline->line[token->end+1] == ':') p->scope = true; p->key.symb = token->symb; //copy variable symb return true; } return false; }
static int token_from_lua(lua_State * L) { int cmd, chr; int cs = 0; size_t len = lua_rawlen(L, -1); if (len == 3 || len == 2) { lua_rawgeti(L, -1, 1); cmd = (int) lua_tointeger(L, -1); lua_rawgeti(L, -2, 2); chr = (int) lua_tointeger(L, -1); if (len == 3) { lua_rawgeti(L, -3, 3); cs = (int) lua_tointeger(L, -1); } lua_pop(L, (int) len); if (cs == 0) { return token_val(cmd, chr); } else { return cs_token_flag + cs; } } return -1; }
static Token* cmdline_parse(Cmdline *cmdline, Token *word, UT_array *parent) { char ch; bool seek; Cmdstr cmd = {.ed = 0}; if (word) cmd.st = word->start; QUEUE *stack = &cmd.stack; QUEUE_INIT(stack); cmd.args = list_new(cmdline); stack_push(stack, cmd.args); Token *headref = stack_head(stack); utarray_new(cmd.chlds, &chld_icd); check_flags(cmdline, &cmd); int idx = 0; while ((word = (Token*)utarray_next(cmdline->tokens, word))) { char *str = token_val(word, VAR_STRING); if (word->quoted) { push(*word, stack, word->start); pop(stack, cmdline, &idx); continue; } switch(ch = str[0]) { case '(': cmdline->lvl++; word = cmdline_parse(cmdline, word, cmd.chlds); ((Cmdstr*)utarray_back(cmd.chlds))->idx = idx - 1; if (!word) goto breakout; break; case ')': if (cmdline->lvl < 1) break; cmdline->lvl--; cmd.ed = word->start; goto breakout; case '[': push(list_new(cmdline), stack, word->start); stack_head(stack)->start = word->start; break; case ']': if (!valid_arry(cmdline, stack, headref)) break; stack_head(stack)->end = word->end; push_arry_container(cmdline, headref, word); pop(stack, cmdline, &idx); break; case '|': if (cmdline->lvl < 1) { cmd.bar = true; cmd.ed = word->start; goto breakout; } /*FALLTHROUGH*/ case '.': case ':': case ',': break; case '%': case '$': word = valid_var(cmdline, word, ch); case '!': if (valid_exec(cmdline, &cmd, word)) break; /*FALLTHROUGH*/ default: seek = seek_ahead(cmdline, stack, word); push(*word, stack, word->start); if (!seek) pop(stack, cmdline, &idx); } } breakout: while (!QUEUE_EMPTY(stack)) stack_pop(stack); utarray_push_back(parent, &cmd); return word; } void cmdline_build_tokens(Cmdline *cmdline, char *line) { log_msg("CMDSTR", "cmdline_build_tokens"); SWAP_ALLOC_PTR(cmdline->line, strdup(line)); Token *word = NULL; while ((word = (Token*)utarray_next(cmdline->tokens, word))) free(word->var.vval.v_string); utarray_clear(cmdline->tokens); cmdline_tokenize(cmdline); }
List* cmdline_lst(Cmdline *cmd) { Cmdstr *root = (Cmdstr*)utarray_front(cmd->cmds); return token_val(&root->args, VAR_LIST); }