Skip to content

Commit d47a700

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 <[email protected]>
1 parent 9d319ee commit d47a700

File tree

4 files changed

+10
-58
lines changed

4 files changed

+10
-58
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: 5 additions & 30 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,19 +81,16 @@ ZTEST(shell_test, test_history_purge)
8281

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

85-
z_shell_history_init(&history);
8684
z_shell_history_put(&history, exp_buf, 20);
8785
z_shell_history_put(&history, exp_buf, 20);
8886

8987
z_shell_history_purge(&history);
90-
9188
test_get(false, true, NULL, 0);
9289
}
9390

9491
/* Test browsing history.
9592
*
9693
* Test steps:
97-
* - initialize history.
9894
* - put lines 1,2,3 to history.
9995
* - get in up direction a line and verify that it's the last one added (3).
10096
* - get next line in up direction and verify that it's line 2.
@@ -116,7 +112,6 @@ ZTEST(shell_test, test_history_get_up_and_down)
116112
init_test_buf(exp2_buf, sizeof(exp2_buf), 10);
117113
init_test_buf(exp3_buf, sizeof(exp3_buf), 20);
118114

119-
z_shell_history_init(&history);
120115
z_shell_history_put(&history, exp1_buf, 20);
121116
z_shell_history_put(&history, exp2_buf, 15);
122117
z_shell_history_put(&history, exp3_buf, 20);
@@ -129,8 +124,6 @@ ZTEST(shell_test, test_history_get_up_and_down)
129124
test_get(true, false, exp2_buf, 15); /* down - 2 */
130125
test_get(true, false, exp3_buf, 20); /* down - 3 */
131126
test_get(false, false, NULL, 0); /* down - nothing */
132-
133-
z_shell_history_purge(&history);
134127
}
135128

136129
/* Function for getting maximal buffer size that can be stored in the history */
@@ -141,7 +134,6 @@ static int get_max_buffer_len(void)
141134
int len = sizeof(buf);
142135
uint16_t out_len;
143136

144-
z_shell_history_init(&history);
145137
do {
146138
z_shell_history_put(&history, buf, len);
147139
out_len = sizeof(out_buf);
@@ -157,7 +149,6 @@ static int get_max_buffer_len(void)
157149
/* Test verifies that line that cannot fit into history buffer is not stored.
158150
*
159151
* Test steps:
160-
* - initialize history.
161152
* - put buffer that is bigger than history overall capacity.
162153
* - verify that history is empty.
163154
* - put short line followed by line that is close to max.
@@ -169,7 +160,6 @@ ZTEST(shell_test, test_too_long_line_not_stored)
169160
int max_len = get_max_buffer_len();
170161

171162
init_test_buf(exp1_buf, sizeof(exp1_buf), 0);
172-
z_shell_history_init(&history);
173163

174164
z_shell_history_put(&history, exp1_buf, max_len + 1);
175165

@@ -182,15 +172,12 @@ ZTEST(shell_test, test_too_long_line_not_stored)
182172
/* Test that long entry evicts older entry. */
183173
test_get(true, true, exp1_buf, max_len - 10);
184174
test_get(false, true, NULL, 0); /* only one entry */
185-
186-
z_shell_history_purge(&history);
187175
}
188176

189177
/* Test verifies that same line as the previous one is not stored in the
190178
* history.
191179
*
192180
* Test steps:
193-
* - initialize history.
194181
* - put same line twice.
195182
* - verify that only one line is in the history.
196183
*/
@@ -199,22 +186,18 @@ ZTEST(shell_test, test_no_duplicates_in_a_row)
199186
uint8_t exp1_buf[HIST_BUF_SIZE];
200187

201188
init_test_buf(exp1_buf, sizeof(exp1_buf), 0);
202-
z_shell_history_init(&history);
203189

204190
z_shell_history_put(&history, exp1_buf, 20);
205191
z_shell_history_put(&history, exp1_buf, 20);
206192

207193
test_get(true, true, exp1_buf, 20);
208194
/* only one line stored. */
209195
test_get(false, true, NULL, 0);
210-
211-
z_shell_history_purge(&history);
212196
}
213197

214198
/* Test storing long lines in the history.
215199
*
216200
* * Test steps:
217-
* - initialize history.
218201
* - Put max length line 1 in history.
219202
* - Verify that it is present.
220203
* - Put max length line 2 in history.
@@ -233,8 +216,6 @@ ZTEST(shell_test, test_storing_long_buffers)
233216
init_test_buf(exp2_buf, sizeof(exp2_buf), 10);
234217
init_test_buf(exp3_buf, sizeof(exp3_buf), 20);
235218

236-
z_shell_history_init(&history);
237-
238219
z_shell_history_put(&history, exp1_buf, max_len);
239220
test_get(true, true, exp1_buf, max_len);
240221
test_get(false, true, NULL, 0); /* only one entry */
@@ -246,8 +227,6 @@ ZTEST(shell_test, test_storing_long_buffers)
246227
z_shell_history_put(&history, exp3_buf, max_len);
247228
test_get(true, true, exp3_buf, max_len);
248229
test_get(false, true, NULL, 0); /* only one entry */
249-
250-
z_shell_history_purge(&history);
251230
}
252231

253232
ZTEST(shell_test, test_circle_through_history)
@@ -256,8 +235,6 @@ ZTEST(shell_test, test_circle_through_history)
256235
uint8_t exp2_buf[HIST_BUF_SIZE];
257236
uint8_t exp3_buf[HIST_BUF_SIZE];
258237

259-
z_shell_history_init(&history);
260-
261238
init_test_buf(exp1_buf, sizeof(exp1_buf), 0);
262239
init_test_buf(exp2_buf, sizeof(exp2_buf), 10);
263240
init_test_buf(exp3_buf, sizeof(exp3_buf), 20);
@@ -273,8 +250,6 @@ ZTEST(shell_test, test_circle_through_history)
273250
test_get(true, true, exp3_buf, 20); /* up - 3*/
274251
test_get(true, true, exp2_buf, 15); /* up - 2*/
275252
test_get(true, true, exp1_buf, 20); /* up - 1*/
276-
277-
z_shell_history_purge(&history);
278253
}
279254

280-
ZTEST_SUITE(shell_test, NULL, NULL, NULL, NULL, NULL);
255+
ZTEST_SUITE(shell_test, NULL, NULL, NULL, reset_history, NULL);

0 commit comments

Comments
 (0)