Skip to content

Commit

Permalink
Fix bug of qtest and extend for completion
Browse files Browse the repository at this point in the history
  • Loading branch information
RinHizakura committed Sep 20, 2020
1 parent 2682633 commit 5e65cd5
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 49 deletions.
75 changes: 55 additions & 20 deletions console.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand All @@ -623,14 +606,66 @@ 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)) {
report(1, "ERROR: Could not open source file '%s'", 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;
}
3 changes: 3 additions & 0 deletions console.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
26 changes: 0 additions & 26 deletions linenoise.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
}
4 changes: 1 addition & 3 deletions linenoise.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
#endif /* __LINENOISE_H */

0 comments on commit 5e65cd5

Please sign in to comment.