From 3ea59e15db1edc319a56495021b3d1e58dd49f30 Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Tue, 11 Apr 2023 11:35:44 +0100 Subject: ash,hush: improvements to tab completion Cosmetic changes. Avoid accessing beyond the end of the command and alias arrays in ash. Code shrink. Change since commit 3165054d2: function old new delta ash_command_name - 153 +153 hush_command_name - 110 +110 complete_cmd_dir_file 876 880 +4 ash_builtin_name 17 - -17 hush_builtin_name 38 - -38 ------------------------------------------------------------------------------ (add/remove: 2/2 grow/shrink: 1/0 up/down: 267/-55) Total: 212 bytes (GitHub issue #301) Signed-off-by: Ron Yorston --- shell/ash.c | 54 +++++++++++++++++++++++++++++++++--------------------- shell/hush.c | 26 ++++++++++++-------------- 2 files changed, 45 insertions(+), 35 deletions(-) diff --git a/shell/ash.c b/shell/ash.c index c77ee8620..9bb8fd805 100644 --- a/shell/ash.c +++ b/shell/ash.c @@ -10286,44 +10286,56 @@ find_builtin(const char *name) static const char * FAST_FUNC ash_command_name(struct exe_state *e) { - if (e->e_type == 0) { - if (/*e->e_index >= 0 &&*/ e->e_index < ARRAY_SIZE(builtintab)) { + int index; + struct tblentry *cmdp; +# if ENABLE_ASH_ALIAS + struct alias *ap; +# endif + + index = e->e_index; + switch (e->e_type) { + case 0: + if (index < ARRAY_SIZE(builtintab)) return builtintab[e->e_index++].name + 1; - } e->e_type++; - e->e_index = 0; - e->e_ptr = cmdtable[0]; - } - if (e->e_type == 1) { - struct tblentry *cmdp = (struct tblentry *)e->e_ptr; - while (e->e_index < CMDTABLESIZE) { + index = 0; + /* e->e_ptr = NULL; */ + /* fall through */ + case 1: + cmdp = (struct tblentry *)e->e_ptr; + for (;;) { while (cmdp) { if (cmdp->cmdtype == CMDFUNCTION) { + e->e_index = index; e->e_ptr = cmdp->next; return cmdp->cmdname; } cmdp = cmdp->next; } - cmdp = cmdtable[++e->e_index]; + if (++index == CMDTABLESIZE) + break; + cmdp = cmdtable[index]; } # if ENABLE_ASH_ALIAS e->e_type++; - e->e_index = 0; - e->e_ptr = atab[0]; -# endif - } -# if ENABLE_ASH_ALIAS - if (e->e_type == 2) { - struct alias *ap = (struct alias *)e->e_ptr; - while (e->e_index < ATABSIZE) { - while (ap) { + index = 0; + e->e_ptr = NULL; + /* fall through */ + case 2: + ap = (struct alias *)e->e_ptr; + for (;;) { + if (ap) { + e->e_index = index; e->e_ptr = ap->next; return ap->name; } - ap = atab[++e->e_index]; + if (++index == ATABSIZE) + break; + ap = atab[index]; } - } # endif + break; + } return NULL; } #endif diff --git a/shell/hush.c b/shell/hush.c index 443bdeccf..04633ab2d 100644 --- a/shell/hush.c +++ b/shell/hush.c @@ -8222,33 +8222,31 @@ static const struct built_in_command *find_builtin(const char *name) #if ENABLE_HUSH_JOB && ENABLE_FEATURE_TAB_COMPLETION static const char * FAST_FUNC hush_command_name(exe_state *e) { - if (e->e_type == 0) { - if (/*e->e_index >= 0 && */ e->e_index < ARRAY_SIZE(bltins1)) { + switch (e->e_type) { + case 0: + if (e->e_index < ARRAY_SIZE(bltins1)) return bltins1[e->e_index++].b_cmd; - } e->e_type++; e->e_index = 0; /* e->e_ptr = NULL; */ - } - if (e->e_type == 1) { - if (e->e_index < ARRAY_SIZE(bltins2)) { + /* fall through */ + case 1: + if (e->e_index < ARRAY_SIZE(bltins2)) return bltins2[e->e_index++].b_cmd; - } # if ENABLE_HUSH_FUNCTIONS e->e_type++; /* e->e_index = 0; */ e->e_ptr = G.top_func; -# endif - } -# if ENABLE_HUSH_FUNCTIONS - if (e->e_type == 2) { - struct function *funcp = (struct function *)e->e_ptr; - while (funcp) { + /* fall through */ + case 2: + if (e->e_ptr) { + struct function *funcp = (struct function *)e->e_ptr; e->e_ptr = funcp->next; return funcp->name; } - } # endif + break; + } return NULL; } #endif -- cgit v1.2.3-55-g6feb