Skip to content

Commit eb93215

Browse files
committed
shell: Remove shell history initialization function
The shell history initialization function doesn't work in if the shell instance hasn't been created through the shell macro. This removes the function to avoid confusion. Signed-off-by: Måns Ansgariusson <Mansgariusson@gmail.com>
1 parent 8138476 commit eb93215

File tree

4 files changed

+15
-54
lines changed

4 files changed

+15
-54
lines changed

include/zephyr/shell/shell_history.h

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,9 @@ struct shell_history {
3434
K_HEAP_DEFINE(_name##_heap, _size); \
3535
static struct shell_history _name = { \
3636
.heap = &_name##_heap, \
37+
.list = SYS_DLIST_STATIC_INIT(&_name.list), \
3738
}
3839

39-
40-
/**
41-
* @brief Initialize shell history module.
42-
*
43-
* @param history Shell history instance.
44-
*/
45-
void z_shell_history_init(struct shell_history *history);
46-
4740
/**
4841
* @brief Purge shell history.
4942
*

subsys/shell/shell.c

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -158,15 +158,6 @@ static void tab_item_print(const struct shell *sh, const char *option,
158158
z_shell_op_cursor_horiz_move(sh, diff);
159159
}
160160

161-
static void history_init(const struct shell *sh)
162-
{
163-
if (!IS_ENABLED(CONFIG_SHELL_HISTORY)) {
164-
return;
165-
}
166-
167-
z_shell_history_init(sh->history);
168-
}
169-
170161
static void history_purge(const struct shell *sh)
171162
{
172163
if (!IS_ENABLED(CONFIG_SHELL_HISTORY)) {
@@ -1233,8 +1224,6 @@ static int instance_init(const struct shell *sh,
12331224
sh->ctx->selected_cmd = root_cmd_find(CONFIG_SHELL_CMD_ROOT);
12341225
}
12351226

1236-
history_init(sh);
1237-
12381227
k_event_init(&sh->ctx->signal_event);
12391228
k_sem_init(&sh->ctx->lock_sem, 1, 1);
12401229

subsys/shell/shell_history.c

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,6 @@ static bool remove_from_tail(struct shell_history *history)
6868
return true;
6969
}
7070

71-
void z_shell_history_purge(struct shell_history *history)
72-
{
73-
while (remove_from_tail(history)) {
74-
}
75-
}
76-
7771
void z_shell_history_put(struct shell_history *history, uint8_t *line,
7872
size_t len)
7973
{
@@ -96,7 +90,7 @@ void z_shell_history_put(struct shell_history *history, uint8_t *line,
9690
return;
9791
}
9892

99-
for (;;) {
93+
for (;;) {
10094
new = k_heap_alloc(history->heap, total_len, K_NO_WAIT);
10195
if (new) {
10296
/* Got memory, add new item */
@@ -112,8 +106,9 @@ void z_shell_history_put(struct shell_history *history, uint8_t *line,
112106
sys_dlist_prepend(&history->list, &new->dnode);
113107
}
114108

115-
void z_shell_history_init(struct shell_history *history)
109+
void z_shell_history_purge(struct shell_history *history)
116110
{
117-
sys_dlist_init(&history->list);
111+
while (remove_from_tail(history)) {
112+
}
118113
history->current = NULL;
119114
}

tests/subsys/shell/shell_history/src/shell_history_test.c

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ static void init_test_buf(uint8_t *buf, size_t len, uint8_t offset)
2424
}
2525
}
2626

27+
static void reset_history(void *fixture)
28+
{
29+
z_shell_history_purge(&history);
30+
}
2731
/**
2832
* Function tests getting line from history and compares it against expected
2933
* result.
@@ -54,7 +58,6 @@ static void test_get(bool ok, bool up, uint8_t *exp_buf, uint16_t exp_len)
5458
/* Test put line to history and get it.
5559
*
5660
* Test steps:
57-
* - initialize history.
5861
* - put line to the history.
5962
* - read line and verify that it is the one that was put.
6063
*/
@@ -64,15 +67,11 @@ ZTEST(shell_test, test_history_add_get)
6467

6568
init_test_buf(exp_buf, sizeof(exp_buf), 0);
6669

67-
z_shell_history_init(&history);
68-
6970
test_get(false, true, NULL, 0);
7071

7172
z_shell_history_put(&history, exp_buf, 20);
7273

7374
test_get(true, true, exp_buf, 20);
74-
75-
z_shell_history_purge(&history);
7675
}
7776

7877
/* Test verifies that after purging there is no line in the history. */
@@ -82,20 +81,18 @@ ZTEST(shell_test, test_history_purge)
8281

8382
init_test_buf(exp_buf, sizeof(exp_buf), 0);
8483

85-
z_shell_history_init(&history);
84+
8685

8786
z_shell_history_put(&history, exp_buf, 20);
8887
z_shell_history_put(&history, exp_buf, 20);
8988

9089
z_shell_history_purge(&history);
91-
9290
test_get(false, true, NULL, 0);
9391
}
9492

9593
/* Test browsing history.
9694
*
9795
* Test steps:
98-
* - initialize history.
9996
* - put lines 1,2,3 to history.
10097
* - get in up direction a line and verify that it's the last one added (3).
10198
* - get next line in up direction and verify that it's line 2.
@@ -117,7 +114,7 @@ ZTEST(shell_test, test_history_get_up_and_down)
117114
init_test_buf(exp2_buf, sizeof(exp2_buf), 10);
118115
init_test_buf(exp3_buf, sizeof(exp3_buf), 20);
119116

120-
z_shell_history_init(&history);
117+
121118

122119
z_shell_history_put(&history, exp1_buf, 20);
123120
z_shell_history_put(&history, exp2_buf, 15);
@@ -131,8 +128,6 @@ ZTEST(shell_test, test_history_get_up_and_down)
131128
test_get(true, false, exp2_buf, 15); /* down - 2 */
132129
test_get(true, false, exp3_buf, 20); /* down - 3 */
133130
test_get(false, false, NULL, 0); /* down - nothing */
134-
135-
z_shell_history_purge(&history);
136131
}
137132

138133
/* Function for getting maximal buffer size that can be stored in the history */
@@ -143,7 +138,7 @@ static int get_max_buffer_len(void)
143138
int len = sizeof(buf);
144139
uint16_t out_len;
145140

146-
z_shell_history_init(&history);
141+
147142

148143
do {
149144
z_shell_history_put(&history, buf, len);
@@ -160,7 +155,6 @@ static int get_max_buffer_len(void)
160155
/* Test verifies that line that cannot fit into history buffer is not stored.
161156
*
162157
* Test steps:
163-
* - initialize history.
164158
* - put buffer that is bigger than history overall capacity.
165159
* - verify that history is empty.
166160
* - put short line followed by line that is close to max.
@@ -172,7 +166,7 @@ ZTEST(shell_test, test_too_long_line_not_stored)
172166
int max_len = get_max_buffer_len();
173167

174168
init_test_buf(exp1_buf, sizeof(exp1_buf), 0);
175-
z_shell_history_init(&history);
169+
176170

177171
z_shell_history_put(&history, exp1_buf, max_len + 1);
178172

@@ -185,15 +179,12 @@ ZTEST(shell_test, test_too_long_line_not_stored)
185179
/* Test that long entry evicts older entry. */
186180
test_get(true, true, exp1_buf, max_len - 10);
187181
test_get(false, true, NULL, 0); /* only one entry */
188-
189-
z_shell_history_purge(&history);
190182
}
191183

192184
/* Test verifies that same line as the previous one is not stored in the
193185
* history.
194186
*
195187
* Test steps:
196-
* - initialize history.
197188
* - put same line twice.
198189
* - verify that only one line is in the history.
199190
*/
@@ -202,22 +193,19 @@ ZTEST(shell_test, test_no_duplicates_in_a_row)
202193
uint8_t exp1_buf[HIST_BUF_SIZE];
203194

204195
init_test_buf(exp1_buf, sizeof(exp1_buf), 0);
205-
z_shell_history_init(&history);
196+
206197

207198
z_shell_history_put(&history, exp1_buf, 20);
208199
z_shell_history_put(&history, exp1_buf, 20);
209200

210201
test_get(true, true, exp1_buf, 20);
211202
/* only one line stored. */
212203
test_get(false, true, NULL, 0);
213-
214-
z_shell_history_purge(&history);
215204
}
216205

217206
/* Test storing long lines in the history.
218207
*
219208
* * Test steps:
220-
* - initialize history.
221209
* - Put max length line 1 in history.
222210
* - Verify that it is present.
223211
* - Put max length line 2 in history.
@@ -236,8 +224,6 @@ ZTEST(shell_test, test_storing_long_buffers)
236224
init_test_buf(exp2_buf, sizeof(exp2_buf), 10);
237225
init_test_buf(exp3_buf, sizeof(exp3_buf), 20);
238226

239-
z_shell_history_init(&history);
240-
241227
z_shell_history_put(&history, exp1_buf, max_len);
242228
test_get(true, true, exp1_buf, max_len);
243229
test_get(false, true, NULL, 0); /* only one entry */
@@ -249,8 +235,6 @@ ZTEST(shell_test, test_storing_long_buffers)
249235
z_shell_history_put(&history, exp3_buf, max_len);
250236
test_get(true, true, exp3_buf, max_len);
251237
test_get(false, true, NULL, 0); /* only one entry */
252-
253-
z_shell_history_purge(&history);
254238
}
255239

256-
ZTEST_SUITE(shell_test, NULL, NULL, NULL, NULL, NULL);
240+
ZTEST_SUITE(shell_test, NULL, NULL, NULL, reset_history, NULL);

0 commit comments

Comments
 (0)