From 5e65cd58410982d049b6d906be58d44120fb3b28 Mon Sep 17 00:00:00 2001 From: RinHizakura Date: Sun, 20 Sep 2020 23:54:14 +0800 Subject: [PATCH] Fix bug of qtest and extend for completion --- console.c | 75 +++++++++++++++++++++++++++++++++++++++-------------- console.h | 3 +++ linenoise.c | 26 ------------------- linenoise.h | 4 +-- 4 files changed, 59 insertions(+), 49 deletions(-) diff --git a/console.c b/console.c index 861c725d8..5a2b9bb7b 100644 --- a/console.c +++ b/console.c @@ -552,19 +552,9 @@ int cmd_select(int nfds, fd_set *exceptfds, struct timeval *timeout) { - char *cmdline; int infd; fd_set local_readset; - while (!has_infile && !block_flag && - (cmdline = linenoise(prompt)) != NULL) { - interpret_cmd(cmdline); - prompt_flag = true; - linenoiseHistoryAdd(cmdline); /* Add to the history. */ - linenoiseHistorySave(HISTORY_FILE); /* Save the history on disk. */ - linenoiseFree(cmdline); - } - if (cmd_done()) return 0; @@ -598,17 +588,10 @@ int cmd_select(int nfds, FD_CLR(infd, readfds); result--; if (has_infile) { + char *cmdline; cmdline = readline(); if (cmdline) interpret_cmd(cmdline); - } else { - while ((cmdline = linenoise(prompt)) != NULL) { - interpret_cmd(cmdline); - linenoiseHistoryAdd(cmdline); /* Add to the history. */ - linenoiseHistorySave( - HISTORY_FILE); /* Save the history on disk. */ - linenoiseFree(cmdline); - } } } return result; @@ -623,6 +606,47 @@ bool finish_cmd() return ok && err_cnt == 0; } +static bool cmd_maybe(char *target, const char *src) +{ + for (int i = 0; i < strlen(src); i++) { + if (target[i] == '\0') + return false; + if (src[i] != target[i]) + return false; + } + return true; +} + +void completion(const char *buf, linenoiseCompletions *lc) +{ + if (strncmp("option ", buf, 7) == 0) { + param_ptr plist = param_list; + + while (plist) { + char str[128] = ""; + // if parameter is too long, now we just ignore it + if (strlen(plist->name) > 120) + continue; + + strcat(str, "option "); + strcat(str, plist->name); + if (cmd_maybe(str, buf)) + linenoiseAddCompletion(lc, str); + + plist = plist->next; + } + return; + } + + cmd_ptr clist = cmd_list; + while (clist) { + if (cmd_maybe(clist->name, buf)) + linenoiseAddCompletion(lc, clist->name); + + clist = clist->next; + } +} + bool run_console(char *infile_name) { if (!push_file(infile_name)) { @@ -630,7 +654,18 @@ bool run_console(char *infile_name) return false; } - while (!cmd_done()) - cmd_select(0, NULL, NULL, NULL, NULL); + if (!has_infile) { + char *cmdline; + while ((cmdline = linenoise(prompt)) != NULL) { + interpret_cmd(cmdline); + linenoiseHistoryAdd(cmdline); /* Add to the history. */ + linenoiseHistorySave(HISTORY_FILE); /* Save the history on disk. */ + linenoiseFree(cmdline); + } + } else { + while (!cmd_done()) + cmd_select(0, NULL, NULL, NULL, NULL); + } + return err_cnt == 0; } diff --git a/console.h b/console.h index c585159d8..a36cd8394 100644 --- a/console.h +++ b/console.h @@ -78,4 +78,7 @@ int cmd_select(int nfds, */ bool run_console(char *infile_name); +/* Callback function to complete command by linenoise */ +void completion(const char *buf, linenoiseCompletions *lc); + #endif /* LAB0_CONSOLE_H */ diff --git a/linenoise.c b/linenoise.c index 8b5e1ac71..41d8cfc7d 100644 --- a/linenoise.c +++ b/linenoise.c @@ -1327,29 +1327,3 @@ int linenoiseHistoryLoad(const char *filename) fclose(fp); return 0; } -int completion_helper(const char *target, const char *cur) -{ - int res = 1; - for (int i = 0, j = 0; i < strlen(cur); i++, j++) { - if (cur[i] != target[j]) - res = 0; - } - return res; -} -void completion(const char *buf, linenoiseCompletions *lc) -{ - if (completion_helper("help", buf)) { - linenoiseAddCompletion(lc, "help"); - } else if (completion_helper("free", buf)) { - linenoiseAddCompletion(lc, "free"); - } else if (completion_helper("ih", buf)) { - linenoiseAddCompletion(lc, "ih"); - } else if (completion_helper("it", buf)) { - linenoiseAddCompletion(lc, "it"); - } else if (completion_helper("new", buf)) { - linenoiseAddCompletion(lc, "new"); - } else if (completion_helper("reverse", buf)) { - linenoiseAddCompletion(lc, "reverse"); - } - /* append more command if you want */ -} \ No newline at end of file diff --git a/linenoise.h b/linenoise.h index 43ea5fe08..382de1dcc 100644 --- a/linenoise.h +++ b/linenoise.h @@ -68,10 +68,8 @@ void linenoiseSetMultiLine(int ml); void linenoisePrintKeyCodes(void); void linenoiseMaskModeEnable(void); void linenoiseMaskModeDisable(void); -void completion(const char *buf, linenoiseCompletions *lc); -int completion_helper(const char *target, const char *cur); #ifdef __cplusplus } #endif -#endif /* __LINENOISE_H */ \ No newline at end of file +#endif /* __LINENOISE_H */