]>
Commit | Line | Data |
---|---|---|
83d290c5 | 1 | /* SPDX-License-Identifier: GPL-2.0+ */ |
18d66533 SG |
2 | /* |
3 | * (C) Copyright 2014 Google, Inc | |
4 | * Simon Glass <[email protected]> | |
18d66533 SG |
5 | */ |
6 | ||
7 | #ifndef __CLI_H | |
8 | #define __CLI_H | |
9 | ||
b08e9d4b | 10 | #include <stdbool.h> |
be5c2edd | 11 | #include <linux/types.h> |
b08e9d4b SG |
12 | |
13 | /** | |
14 | * struct cli_ch_state - state information for reading cmdline characters | |
15 | * | |
16 | * @esc_len: Number of escape characters read so far | |
17 | * @esc_save: Escape characters collected so far | |
32bab0ea SG |
18 | * @emit_upto: Next index to emit from esc_save |
19 | * @emitting: true if emitting from esc_save | |
b08e9d4b SG |
20 | */ |
21 | struct cli_ch_state { | |
22 | int esc_len; | |
23 | char esc_save[8]; | |
24 | int emit_upto; | |
32bab0ea | 25 | bool emitting; |
b08e9d4b SG |
26 | }; |
27 | ||
be5c2edd SG |
28 | /** |
29 | * struct cli_line_state - state of the line editor | |
30 | * | |
31 | * @num: Current cursor position, where 0 is the start | |
32 | * @eol_num: Number of characters in the buffer | |
33 | * @insert: true if in 'insert' mode | |
8fc041fe | 34 | * @history: true if history should be accessible |
39ee3216 SG |
35 | * @cmd_complete: true if tab completion should be enabled (requires @prompt to |
36 | * be set) | |
e5509ce8 SG |
37 | * @buf: Buffer containing line |
38 | * @prompt: Prompt for the line | |
be5c2edd SG |
39 | */ |
40 | struct cli_line_state { | |
41 | uint num; | |
42 | uint eol_num; | |
e5509ce8 | 43 | uint len; |
be5c2edd | 44 | bool insert; |
8fc041fe | 45 | bool history; |
3b487bf5 | 46 | bool cmd_complete; |
e5509ce8 SG |
47 | char *buf; |
48 | const char *prompt; | |
be5c2edd SG |
49 | }; |
50 | ||
18d66533 SG |
51 | /** |
52 | * Go into the command loop | |
53 | * | |
54 | * This will return if we get a timeout waiting for a command. See | |
55 | * CONFIG_BOOT_RETRY_TIME. | |
56 | */ | |
c1bb2cd0 | 57 | void cli_simple_loop(void); |
18d66533 SG |
58 | |
59 | /** | |
60 | * cli_simple_run_command() - Execute a command with the simple CLI | |
61 | * | |
62 | * @cmd: String containing the command to execute | |
63 | * @flag Flag value - see CMD_FLAG_... | |
185f812c | 64 | * Return: 1 - command executed, repeatable |
18d66533 SG |
65 | * 0 - command executed but not repeatable, interrupted commands are |
66 | * always considered not repeatable | |
67 | * -1 - not executed (unrecognized, bootd recursion or too many args) | |
68 | * (If cmd is NULL or "" or longer than CONFIG_SYS_CBSIZE-1 it is | |
69 | * considered unrecognized) | |
70 | */ | |
71 | int cli_simple_run_command(const char *cmd, int flag); | |
72 | ||
a06be2d0 HG |
73 | /** |
74 | * cli_simple_process_macros() - Expand $() and ${} format env. variables | |
75 | * | |
76 | * @param input Input string possible containing $() / ${} vars | |
77 | * @param output Output string with $() / ${} vars expanded | |
1a62d64c | 78 | * @param max_size Maximum size of @output (including terminator) |
185f812c | 79 | * Return: 0 if OK, -ENOSPC if we ran out of space in @output |
a06be2d0 | 80 | */ |
1a62d64c | 81 | int cli_simple_process_macros(const char *input, char *output, int max_size); |
a06be2d0 | 82 | |
18d66533 SG |
83 | /** |
84 | * cli_simple_run_command_list() - Execute a list of command | |
85 | * | |
86 | * The commands should be separated by ; or \n and will be executed | |
87 | * by the built-in parser. | |
88 | * | |
89 | * This function cannot take a const char * for the command, since if it | |
90 | * finds newlines in the string, it replaces them with \0. | |
91 | * | |
92 | * @param cmd String containing list of commands | |
93 | * @param flag Execution flags (CMD_FLAG_...) | |
185f812c | 94 | * Return: 0 on success, or != 0 on error. |
18d66533 SG |
95 | */ |
96 | int cli_simple_run_command_list(char *cmd, int flag); | |
97 | ||
98 | /** | |
99 | * cli_readline() - read a line into the console_buffer | |
100 | * | |
101 | * This is a convenience function which calls cli_readline_into_buffer(). | |
102 | * | |
103 | * @prompt: Prompt to display | |
185f812c | 104 | * Return: command line length excluding terminator, or -ve on error |
18d66533 | 105 | */ |
e1bf824d | 106 | int cli_readline(const char *const prompt); |
18d66533 SG |
107 | |
108 | /** | |
109 | * readline_into_buffer() - read a line into a buffer | |
110 | * | |
111 | * Display the prompt, then read a command line into @buffer. The | |
112 | * maximum line length is CONFIG_SYS_CBSIZE including a \0 terminator, which | |
113 | * will always be added. | |
114 | * | |
115 | * The command is echoed as it is typed. Command editing is supported if | |
116 | * CONFIG_CMDLINE_EDITING is defined. Tab auto-complete is supported if | |
117 | * CONFIG_AUTO_COMPLETE is defined. If CONFIG_BOOT_RETRY_TIME is defined, | |
118 | * then a timeout will be applied. | |
119 | * | |
120 | * If CONFIG_BOOT_RETRY_TIME is defined and retry_time >= 0, | |
121 | * time out when time goes past endtime (timebase time in ticks). | |
122 | * | |
123 | * @prompt: Prompt to display | |
124 | * @buffer: Place to put the line that is entered | |
be0169f0 SG |
125 | * @timeout: Timeout in seconds, 0 if none |
126 | * Return: command line length excluding terminator, or -ve on error: if the | |
18d66533 SG |
127 | * timeout is exceeded (either CONFIG_BOOT_RETRY_TIME or the timeout |
128 | * parameter), then -2 is returned. If a break is detected (Ctrl-C) then | |
129 | * -1 is returned. | |
130 | */ | |
e1bf824d SG |
131 | int cli_readline_into_buffer(const char *const prompt, char *buffer, |
132 | int timeout); | |
18d66533 SG |
133 | |
134 | /** | |
135 | * parse_line() - split a command line down into separate arguments | |
136 | * | |
137 | * The argv[] array is filled with pointers into @line, and each argument | |
138 | * is terminated by \0 (i.e. @line is changed in the process unless there | |
139 | * is only one argument). | |
140 | * | |
141 | * #argv is terminated by a NULL after the last argument pointer. | |
142 | * | |
143 | * At most CONFIG_SYS_MAXARGS arguments are permited - if there are more | |
144 | * than that then an error is printed, and this function returns | |
145 | * CONFIG_SYS_MAXARGS, with argv[] set up to that point. | |
146 | * | |
147 | * @line: Command line to parse | |
148 | * @args: Array to hold arguments | |
185f812c | 149 | * Return: number of arguments |
18d66533 | 150 | */ |
e1bf824d | 151 | int cli_simple_parse_line(char *line, char *argv[]); |
18d66533 | 152 | |
0f925822 | 153 | #if CONFIG_IS_ENABLED(OF_CONTROL) |
affb2156 SG |
154 | /** |
155 | * cli_process_fdt() - process the boot command from the FDT | |
156 | * | |
157 | * If bootcmmd is defined in the /config node of the FDT, we use that | |
158 | * as the boot command. Further, if bootsecure is set to 1 (in the same | |
159 | * node) then we return true, indicating that the command should be executed | |
160 | * as securely as possible, avoiding the CLI parser. | |
161 | * | |
162 | * @cmdp: On entry, the command that will be executed if the FDT does | |
163 | * not have a command. Returns the command to execute after | |
164 | * checking the FDT. | |
185f812c | 165 | * Return: true to execute securely, else false |
affb2156 SG |
166 | */ |
167 | bool cli_process_fdt(const char **cmdp); | |
168 | ||
169 | /** cli_secure_boot_cmd() - execute a command as securely as possible | |
170 | * | |
171 | * This avoids using the parser, thus executing the command with the | |
172 | * smallest amount of code. Parameters are not supported. | |
173 | */ | |
174 | void cli_secure_boot_cmd(const char *cmd); | |
175 | #else | |
176 | static inline bool cli_process_fdt(const char **cmdp) | |
177 | { | |
178 | return false; | |
179 | } | |
180 | ||
181 | static inline void cli_secure_boot_cmd(const char *cmd) | |
182 | { | |
183 | } | |
184 | #endif /* CONFIG_OF_CONTROL */ | |
185 | ||
c1bb2cd0 SG |
186 | /** |
187 | * Go into the command loop | |
188 | * | |
189 | * This will return if we get a timeout waiting for a command, but only for | |
190 | * the simple parser (not hush). See CONFIG_BOOT_RETRY_TIME. | |
191 | */ | |
192 | void cli_loop(void); | |
193 | ||
194 | /** Set up the command line interpreter ready for action */ | |
195 | void cli_init(void); | |
196 | ||
6493ccc7 | 197 | #define endtick(seconds) (get_ticks() + (uint64_t)(seconds) * get_tbclk()) |
b08e9d4b SG |
198 | #define CTL_CH(c) ((c) - 'a' + 1) |
199 | ||
200 | /** | |
201 | * cli_ch_init() - Set up the initial state to process input characters | |
202 | * | |
203 | * @cch: State to set up | |
204 | */ | |
205 | void cli_ch_init(struct cli_ch_state *cch); | |
206 | ||
207 | /** | |
208 | * cli_ch_process() - Process an input character | |
209 | * | |
210 | * When @ichar is 0, this function returns any characters from an invalid escape | |
211 | * sequence which are still pending in the buffer | |
212 | * | |
213 | * Otherwise it processes the input character. If it is an escape character, | |
214 | * then an escape sequence is started and the function returns 0. If we are in | |
215 | * the middle of an escape sequence, the character is processed and may result | |
216 | * in returning 0 (if more characters are needed) or a valid character (if | |
217 | * @ichar finishes the sequence). | |
218 | * | |
219 | * If @ichar is a valid character and there is no escape sequence in progress, | |
220 | * then it is returned as is. | |
221 | * | |
222 | * If the Enter key is pressed, '\n' is returned. | |
223 | * | |
224 | * Usage should be like this:: | |
225 | * | |
226 | * struct cli_ch_state cch; | |
227 | * | |
228 | * cli_ch_init(cch); | |
229 | * do | |
230 | * { | |
231 | * int ichar, ch; | |
232 | * | |
233 | * ichar = cli_ch_process(cch, 0); | |
234 | * if (!ichar) { | |
235 | * ch = getchar(); | |
236 | * ichar = cli_ch_process(cch, ch); | |
237 | * } | |
238 | * (handle the ichar character) | |
239 | * } while (!done) | |
240 | * | |
241 | * If tstc() is used to look for keypresses, this function can be called with | |
242 | * @ichar set to -ETIMEDOUT if there is no character after 5-10ms. This allows | |
243 | * the ambgiuity between the Escape key and the arrow keys (which generate an | |
244 | * escape character followed by other characters) to be resolved. | |
245 | * | |
246 | * @cch: Current state | |
247 | * @ichar: Input character to process, or 0 if none, or -ETIMEDOUT if no | |
248 | * character has been received within a small number of milliseconds (this | |
249 | * cancels any existing escape sequence and allows pressing the Escape key to | |
250 | * work) | |
251 | * Returns: Resulting input character after processing, 0 if none, '\e' if | |
252 | * an existing escape sequence was cancelled | |
253 | */ | |
254 | int cli_ch_process(struct cli_ch_state *cch, int ichar); | |
6493ccc7 | 255 | |
e5509ce8 SG |
256 | /** |
257 | * cread_line_process_ch() - Process a character for line input | |
258 | * | |
259 | * @cls: CLI line state | |
260 | * @ichar: Character to process | |
261 | * Return: 0 if input is complete, with line in cls->buf, -EINTR if input was | |
262 | * cancelled with Ctrl-C, -EAGAIN if more characters are needed | |
263 | */ | |
264 | int cread_line_process_ch(struct cli_line_state *cls, char ichar); | |
265 | ||
39ee3216 SG |
266 | /** |
267 | * cli_cread_init() - Set up a new cread struct | |
268 | * | |
269 | * Sets up a new cread state, with history and cmd_complete set to false | |
270 | * | |
271 | * After calling this, you can use cread_line_process_ch() to process characters | |
272 | * received from the user. | |
273 | * | |
274 | * @cls: CLI line state | |
275 | * @buf: Text buffer containing the initial text | |
276 | * @buf_size: Buffer size, including nul terminator | |
277 | */ | |
278 | void cli_cread_init(struct cli_line_state *cls, char *buf, uint buf_size); | |
279 | ||
33eb0b9e SG |
280 | /** cread_print_hist_list() - Print the command-line history list */ |
281 | void cread_print_hist_list(void); | |
282 | ||
18d66533 | 283 | #endif |