Skip to content

Commit

Permalink
Import library and extend qtest
Browse files Browse the repository at this point in the history
qtest support cursor moving

Also suppress pre-commit.hook due to qtest extension

Support auto completion

Support command history

Through UP and DOWN key

Clean dead code

Update read me

Explain usage of command line

Solve conflicts

Modify pitfalls

Indent object

Indent again

Change file name to macro

Change file name into macro
  • Loading branch information
eecheng87 committed Sep 17, 2020
1 parent 2d97587 commit a75eacf
Show file tree
Hide file tree
Showing 9 changed files with 1,479 additions and 22 deletions.
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ $(GIT_HOOKS):
@echo

OBJS := qtest.o report.o console.o harness.o queue.o \
random.o dudect/constant.o dudect/fixture.o dudect/ttest.o
random.o dudect/constant.o dudect/fixture.o dudect/ttest.o \
linenoise.o

deps := $(OBJS:%.o=.%.o.d)

qtest: $(OBJS)
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,12 @@ cmd>
(gdb)
```

## User-friendly command line
Integrate [linenoise](https://github.com/antirez/linenoise) to `qtest`. Contain following user-friendly features:
* Move cursor by right and left key
* Get previous or next command typed before by up and down key
* Auto completion by TAB

## License

`lab0-c`is released under the BSD 2 clause license. Use of this source code is governed by
Expand All @@ -163,3 +169,4 @@ a BSD-style license that can be found in the LICENSE file.
External source code:
* [dudect](https://github.com/oreparaz/dudect): public domain
* [git-good-commit](https://github.com/tommarshall/git-good-commit): MIT License
* [linenoise](https://github.com/antirez/linenoise): BSD 2-Clause "Simplified" License
44 changes: 25 additions & 19 deletions console.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/* Implementation of simple command-line interface */

#include "console.h"

#include <ctype.h>
#include <fcntl.h>
#include <inttypes.h>
Expand All @@ -13,7 +15,6 @@
#include <sys/types.h>
#include <unistd.h>

#include "console.h"
#include "report.h"

/* Some global values */
Expand Down Expand Up @@ -59,6 +60,7 @@ static bool echo = 0;

static bool quit_flag = false;
static char *prompt = "cmd> ";
static bool has_infile = false;

/* Optional function to call as part of exit process */
/* Maximum number of quit functions */
Expand Down Expand Up @@ -160,10 +162,10 @@ static char **parse_args(char *line, int *argcp)
size_t len = strlen(line);
/* First copy into buffer with each substring null-terminated */
char *buf = malloc_or_fail(len + 1, "parse_args");
buf[len] = '\0';
char *src = line;
char *dst = buf;
bool skipping = true;

int c;
int argc = 0;
while ((c = *src++) != '\0') {
Expand Down Expand Up @@ -210,7 +212,6 @@ static bool interpret_cmda(int argc, char *argv[])
{
if (argc == 0)
return true;

/* Try to find matching command */
cmd_ptr next_cmd = cmd_list;
bool ok = true;
Expand Down Expand Up @@ -439,6 +440,7 @@ static bool do_time_cmd(int argc, char *argv[])
static bool push_file(char *fname)
{
int fd = fname ? open(fname, O_RDONLY) : STDIN_FILENO;
has_infile = fname ? true : false;
if (fd < 0)
return false;

Expand Down Expand Up @@ -529,17 +531,6 @@ static char *readline()
return linebuf;
}

/* Determine if there is a complete command line in input buffer */
static bool read_ready()
{
for (int i = 0; buf_stack && i < buf_stack->cnt; i++) {
if (buf_stack->bufptr[i] == '\n')
return true;
}

return false;
}

static bool cmd_done()
{
return !buf_stack || quit_flag;
Expand All @@ -564,10 +555,14 @@ int cmd_select(int nfds,
char *cmdline;
int infd;
fd_set local_readset;
while (!block_flag && read_ready()) {
cmdline = readline();

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())
Expand Down Expand Up @@ -602,9 +597,19 @@ int cmd_select(int nfds,
/* Commandline input available */
FD_CLR(infd, readfds);
result--;
cmdline = readline();
if (cmdline)
interpret_cmd(cmdline);
if (has_infile) {
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 @@ -614,6 +619,7 @@ bool finish_cmd()
bool ok = true;
if (!quit_flag)
ok = ok && do_quit_cmd(0, NULL);
has_infile = false;
return ok && err_cnt == 0;
}

Expand Down
2 changes: 2 additions & 0 deletions console.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#define LAB0_CONSOLE_H
#include <stdbool.h>
#include <sys/select.h>
#include "linenoise.h"
#define HISTORY_FILE ".cmd_history"

/* Implementation of simple command-line interface */

Expand Down
Loading

0 comments on commit a75eacf

Please sign in to comment.