Skip to content

Commit 33eb0b9

Browse files
sjg20trini
authored andcommittedOct 11, 2023
cli: Add a command to show cmdline history
There is a function for this but it is never used. Showing the history is a useful feature, so add a new 'history' command. Signed-off-by: Simon Glass <[email protected]>
1 parent 0f97e94 commit 33eb0b9

File tree

9 files changed

+153
-3
lines changed

9 files changed

+153
-3
lines changed
 

‎cmd/Kconfig

+7
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,13 @@ config CMD_FWU_METADATA
176176
help
177177
Command to read the metadata and dump it's contents
178178

179+
config CMD_HISTORY
180+
bool "history"
181+
depends on CMDLINE_EDITING
182+
help
183+
Show the command-line history, i.e. a list of commands that are in
184+
the history buffer.
185+
179186
config CMD_LICENSE
180187
bool "license"
181188
select BUILD_BIN2C

‎cmd/Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ obj-$(CONFIG_CMD_FUSE) += fuse.o
9191
obj-$(CONFIG_CMD_FWU_METADATA) += fwu_mdata.o
9292
obj-$(CONFIG_CMD_GETTIME) += gettime.o
9393
obj-$(CONFIG_CMD_GPIO) += gpio.o
94+
obj-$(CONFIG_CMD_HISTORY) += history.o
9495
obj-$(CONFIG_CMD_HVC) += smccc.o
9596
obj-$(CONFIG_CMD_I2C) += i2c.o
9697
obj-$(CONFIG_CMD_IOTRACE) += iotrace.o

‎cmd/history.c

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// SPDX-License-Identifier: GPL-2.0+
2+
/*
3+
* Copyright 2023 Google LLC
4+
* Written by Simon Glass <sjg@chromium.org>
5+
*/
6+
7+
#include <common.h>
8+
#include <command.h>
9+
#include <cli.h>
10+
11+
static int do_history(struct cmd_tbl *cmdtp, int flag, int argc,
12+
char *const argv[])
13+
{
14+
cread_print_hist_list();
15+
16+
return 0;
17+
}
18+
19+
U_BOOT_CMD(
20+
history, CONFIG_SYS_MAXARGS, 1, do_history,
21+
"print command history",
22+
""
23+
);

‎common/cli_readline.c

+1-3
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,7 @@ static char *hist_next(void)
160160
return ret;
161161
}
162162

163-
#ifndef CONFIG_CMDLINE_EDITING
164-
static void cread_print_hist_list(void)
163+
void cread_print_hist_list(void)
165164
{
166165
int i;
167166
unsigned long n;
@@ -179,7 +178,6 @@ static void cread_print_hist_list(void)
179178
i++;
180179
}
181180
}
182-
#endif /* CONFIG_CMDLINE_EDITING */
183181

184182
#define BEGINNING_OF_LINE() { \
185183
while (num) { \

‎doc/usage/cmd/history.rst

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
.. SPDX-License-Identifier: GPL-2.0+:
2+
3+
history command
4+
===============
5+
6+
Synopis
7+
-------
8+
9+
::
10+
11+
history
12+
13+
Description
14+
-----------
15+
16+
The *history* command shows a list of previously entered commands on the
17+
command line. When U-Boot starts, this it is initially empty. Each new command
18+
entered is added to the list.
19+
20+
Normally these commands can be accessed by pressing the `up arrow` and
21+
`down arrow` keys, which cycle through the list. The `history` command provides
22+
a simple way to view the list.
23+
24+
Example
25+
-------
26+
27+
This example shows entering three commands, then `history`. Note that `history`
28+
itself is added to the list.
29+
30+
::
31+
32+
=> bootflow scan -l
33+
Scanning for bootflows in all bootdevs
34+
Seq Method State Uclass Part Name Filename
35+
--- ----------- ------ -------- ---- ------------------------ ----------------
36+
Scanning global bootmeth 'firmware0':
37+
Hunting with: simple_bus
38+
Found 2 extension board(s).
39+
Scanning bootdev 'mmc2.bootdev':
40+
Scanning bootdev 'mmc1.bootdev':
41+
0 extlinux ready mmc 1 mmc1.bootdev.part_1 /extlinux/extlinux.conf
42+
No more bootdevs
43+
--- ----------- ------ -------- ---- ------------------------ ----------------
44+
(1 bootflow, 1 valid)
45+
=> bootflow select 0
46+
=> bootflow info
47+
Name: mmc1.bootdev.part_1
48+
Device: mmc1.bootdev
49+
Block dev: mmc1.blk
50+
Method: extlinux
51+
State: ready
52+
Partition: 1
53+
Subdir: (none)
54+
Filename: /extlinux/extlinux.conf
55+
Buffer: aebdea0
56+
Size: 253 (595 bytes)
57+
OS: Fedora-Workstation-armhfp-31-1.9 (5.3.7-301.fc31.armv7hl)
58+
Cmdline: (none)
59+
Logo: (none)
60+
FDT: <NULL>
61+
Error: 0
62+
=> history
63+
bootflow scan -l
64+
bootflow select 0
65+
bootflow info
66+
history
67+
=>

‎doc/usage/index.rst

+1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ Shell commands
6767
cmd/fwu_mdata
6868
cmd/gpio
6969
cmd/gpt
70+
cmd/history
7071
cmd/host
7172
cmd/imxtract
7273
cmd/load

‎include/cli.h

+3
Original file line numberDiff line numberDiff line change
@@ -229,4 +229,7 @@ void cli_ch_init(struct cli_ch_state *cch);
229229
*/
230230
int cli_ch_process(struct cli_ch_state *cch, int ichar);
231231

232+
/** cread_print_hist_list() - Print the command-line history list */
233+
void cread_print_hist_list(void);
234+
232235
#endif

‎test/cmd/Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ obj-$(CONFIG_CMD_ADDRMAP) += addrmap.o
1414
obj-$(CONFIG_CMD_BDI) += bdinfo.o
1515
obj-$(CONFIG_CMD_FDT) += fdt.o
1616
obj-$(CONFIG_CONSOLE_TRUETYPE) += font.o
17+
obj-$(CONFIG_CMD_HISTORY) += history.o
1718
obj-$(CONFIG_CMD_LOADM) += loadm.o
1819
obj-$(CONFIG_CMD_MEM_SEARCH) += mem_search.o
1920
ifdef CONFIG_CMD_PCI

‎test/cmd/history.c

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// SPDX-License-Identifier: GPL-2.0+
2+
/*
3+
* Tests for history command
4+
*
5+
* Copyright 2023 Google LLC
6+
* Written by Simon Glass <sjg@chromium.org>
7+
*/
8+
9+
#include <common.h>
10+
#include <cli.h>
11+
#include <command.h>
12+
#include <test/lib.h>
13+
#include <test/test.h>
14+
#include <test/ut.h>
15+
16+
static int lib_test_history(struct unit_test_state *uts)
17+
{
18+
static const char cmd1[] = "setenv fred hello";
19+
static const char cmd2[] = "print fred";
20+
21+
/* running commands directly does not add to history */
22+
ut_assertok(run_command(cmd1, 0));
23+
ut_assert_console_end();
24+
ut_assertok(run_command("history", 0));
25+
ut_assert_console_end();
26+
27+
/* enter commands via the console */
28+
console_in_puts(cmd1);
29+
console_in_puts("\n");
30+
ut_asserteq(strlen(cmd1), cli_readline(""));
31+
ut_assert_nextline(cmd1);
32+
33+
console_in_puts(cmd2);
34+
console_in_puts("\n");
35+
ut_asserteq(strlen(cmd2), cli_readline(""));
36+
ut_assert_nextline(cmd2);
37+
38+
ut_assertok(run_command("print fred", 0));
39+
ut_assert_nextline("fred=hello");
40+
ut_assert_console_end();
41+
42+
ut_assertok(run_command("history", 0));
43+
ut_assert_nextline(cmd1);
44+
ut_assert_nextline(cmd2);
45+
ut_assert_console_end();
46+
47+
return 0;
48+
}
49+
LIB_TEST(lib_test_history, UT_TESTF_CONSOLE_REC);

0 commit comments

Comments
 (0)
Please sign in to comment.