/* get next command; process history log */ void *lex(void *_pcmds, int *agc, byte **agv,byte *initial_cmd,byte *raw_input) { byte *cmd,*p; DISPATCHER_TEXT *pcmds_start,*pcmds; *agc = 0; /* "CMD>" */ if (initial_cmd) rtfs_cs_strcpy(working_buf, initial_cmd, CS_CHARSET_NOT_UNICODE); else rtfs_print_prompt_user((byte *)"CMD> ", working_buf); if (raw_input) rtfs_cs_strcpy(raw_input,working_buf, CS_CHARSET_NOT_UNICODE); pcmds = (DISPATCHER_TEXT *) _pcmds; pcmds_start = pcmds; p = cmd = &working_buf[0]; p = gnext(p, ' '); /* Keep grabbing tokens until there are none left */ while (p) { if (CS_OP_CMP_ASCII(p,'"', CS_CHARSET_NOT_UNICODE)) { /* Quoted string.. find the end quote and terminate */ CS_OP_TERM_STRING(p, CS_CHARSET_NOT_UNICODE); CS_OP_INC_PTR(p, CS_CHARSET_NOT_UNICODE); *agv++ = p; *agc += 1; p = gnext(p,'"'); } else { *agv++ = p; *agc += 1; p = gnext(p,' '); } } { DISPATCHER_TEXT *pcmds_txt; pcmds_txt = (DISPATCHER_TEXT *) pcmds; while (pcmds_txt->cmd) { if (rtfs_cs_strcmp(cmd,pcmds_txt->cmd, CS_CHARSET_NOT_UNICODE) == 0) return ((void *)pcmds_txt); pcmds_txt++; } } /* No match return ??? */ return ((void *)pcmds_start); }
int luaH_findindex (lua_State *L, Table *t, StkId key) { #else static int findindex (lua_State *L, Table *t, StkId key) { #endif /* LUAPLUS_EXTENSIONS */ int i; if (ttisnil(key)) return -1; /* first iteration */ i = arrayindex(key); if (0 < i && i <= t->sizearray) /* is `key' inside array part? */ return i-1; /* yes; that's the index (corrected to C) */ else { Node *n = mainposition(t, key); do { /* check whether `key' is somewhere in the chain */ /* key may be dead already, but it is ok to use it in `next' */ if (luaO_rawequalObj(key2tval(n), key) || (ttype(gkey(n)) == LUA_TDEADKEY && iscollectable(key) && gcvalue(gkey(n)) == gcvalue(key))) { i = cast_int(n - gnode(t, 0)); /* key index in hash table */ /* hash elements are numbered after array ones */ return i + t->sizearray; } else n = gnext(n); } while (n); luaG_runerror(L, "invalid key to " LUA_QL("next")); /* key not found */ return 0; /* to avoid warnings */ } }
/* * main search function */ const Tvalue *kp_table_get(Table *t, const Tvalue *key) { switch (ttype(key)) { case KTAP_TNIL: return ktap_nilobject; case KTAP_TSHRSTR: return kp_table_getstr(t, rawtsvalue(key)); case KTAP_TNUMBER: { ktap_Number n = nvalue(key); int k = (int)n; if ((ktap_Number)k == nvalue(key)) /* index is int? */ return kp_table_getint(t, k); /* use specialized version */ /* else go through */ } default: { Node *n = mainposition(t, key); do { /* check whether `key' is somewhere in the chain */ if (rawequalobj(gkey(n), key)) return gval(n); /* that's it */ else n = gnext(n); } while (n); return ktap_nilobject; } } }
static void setnodevector(ktap_State *ks, Table *t, int size) { int lsize; if (size == 0) { /* no elements to hash part? */ t->node = (Node *)dummynode; /* use common `dummynode' */ lsize = 0; } else { int i; lsize = ceillog2(size); if (lsize > MAXBITS) kp_runerror(ks, "table overflow"); size = twoto(lsize); t->node = kp_malloc(ks, size * sizeof(Node)); for (i = 0; i < size; i++) { Node *n = gnode(t, i); gnext(n) = NULL; setnilvalue(gkey(n)); setnilvalue(gval(n)); } } t->lsizenode = (u8)lsize; t->lastfree = gnode(t, size); /* all positions are free */ }
/* * returns the index of a `key' for table traversals. First goes all * elements in the array part, then elements in the hash part. The * beginning of a traversal is signaled by -1. */ static int findindex(ktap_State *ks, Table *t, StkId key) { int i; if (ttisnil(key)) return -1; /* first iteration */ i = arrayindex(key); if (i > 0 && i <= t->sizearray) /* is `key' inside array part? */ return i - 1; /* yes; that's the index (corrected to C) */ else { Node *n = mainposition(t, key); for (;;) { /* check whether `key' is somewhere in the chain */ /* key may be dead already, but it is ok to use it in `next' */ if (kp_equalobjv(ks, gkey(n), key)) { i = n - gnode(t, 0); /* key index in hash table */ /* hash elements are numbered after array ones */ return i + t->sizearray; } else n = gnext(n); if (n == NULL) /* key not found */ kp_runerror(ks, "invalid key to next"); } } }
static Tvalue *table_newkey(ktap_State *ks, Table *t, const Tvalue *key) { Node *mp; mp = mainposition(t, key); if (!isnil(gval(mp)) || isdummy(mp)) { /* main position is taken? */ Node *othern; Node *n = getfreepos(t); /* get a free place */ if (n == NULL) { /* cannot find a free place? */ rehash(ks, t, key); /* grow table */ /* whatever called 'newkey' take care of TM cache and GC barrier */ return kp_table_set(ks, t, key); /* insert key into grown table */ } othern = mainposition(t, gkey(mp)); if (othern != mp) { /* is colliding node out of its main position? */ /* yes; move colliding node into free position */ while (gnext(othern) != mp) othern = gnext(othern); /* find previous */ gnext(othern) = n; /* redo the chain with `n' in place of `mp' */ *n = *mp; /* copy colliding node into free pos. (mp->next also goes) */ gnext(mp) = NULL; /* now `mp' is free */ setnilvalue(gval(mp)); } else { /* colliding node is in its own main position */ /* new node will go into free position */ gnext(n) = gnext(mp); /* chain new position */ gnext(mp) = n; mp = n; } } setobj(ks, gkey(mp), key); return gval(mp); }
/* ** "Generic" get version. (Not that generic: not valid for integers, ** which may be in array part, nor for floats with integral values.) */ static const TValue *getgeneric (Table *t, const TValue *key) { Node *n = mainpositionTV(t, key); for (;;) { /* check whether 'key' is somewhere in the chain */ if (equalkey(key, n)) return gval(n); /* that's it */ else { int nx = gnext(n); if (nx == 0) return &absentkey; /* not found */ n += nx; } } }
/* * search function for short strings */ const Tvalue *kp_table_getstr(Table *t, Tstring *key) { Node *n = hashstr(t, key); do { /* check whether `key' is somewhere in the chain */ if (ttisshrstring(gkey(n)) && eqshrstr(rawtsvalue(gkey(n)), key)) return gval(n); /* that's it */ else n = gnext(n); } while (n); return ktap_nilobject; }
const Tvalue *kp_table_getint(Table *t, int key) { Node *n; if ((unsigned int)(key - 1) < (unsigned int)t->sizearray) return &t->array[key - 1]; n = hashnum(t, key); do { if (ttisnumber(gkey(n)) && nvalue(gkey(n)) == key) return gval(n); else n = gnext(n); } while (n); return ktap_nilobject; }