shell.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2009-2013 Freie Universität Berlin
3  * SPDX-License-Identifier: LGPL-2.1-only
4  */
5 
6 #pragma once
7 
46 #include <stdint.h>
47 #include "periph/pm.h"
48 
49 #include "modules.h"
50 #include "xfa.h"
51 
52 #ifndef __cplusplus
53 #include "flash_utils.h"
54 #endif
55 
56 #ifdef __cplusplus
57 extern "C" {
58 #endif
59 
70 #ifndef CONFIG_SHELL_SHUTDOWN_ON_EXIT
71 /* Some systems (e.g Ubuntu 20.04) close stdin on CTRL-D / EOF
72  * That means we can't just re-start the shell.
73  * Instead terminate RIOT, which is also the behavior a user would
74  * expect from a CLI application.
75  */
76 # if defined(CPU_NATIVE) && !IS_ACTIVE(MODULE_SHELL_LOCK)
77 # define CONFIG_SHELL_SHUTDOWN_ON_EXIT 1
78 # else
79 # define CONFIG_SHELL_SHUTDOWN_ON_EXIT 0
80 # endif
81 #endif
82 
86 #ifndef CONFIG_SHELL_NO_ECHO
87 #define CONFIG_SHELL_NO_ECHO 0
88 #endif
89 
93 #ifndef CONFIG_SHELL_NO_PROMPT
94 #define CONFIG_SHELL_NO_PROMPT 0
95 #endif
96 
115 #define SHELL_DEFAULT_BUFSIZE (128)
116 
124 
134 void shell_pre_command_hook(int argc, char **argv);
135 
146 void shell_post_command_hook(int ret, int argc, char **argv);
147 
172 typedef int (*shell_command_handler_t)(int argc, char **argv);
173 
179 typedef struct shell_command_t {
180  const char *name;
181  const char *desc;
184 
185 #ifndef __cplusplus
191 typedef struct {
192  FLASH_ATTR const char *name;
193  FLASH_ATTR const char *desc;
197 #endif /* __cplusplus */
198 
206 void shell_run_once(const shell_command_t *commands, char *line_buf, int len);
207 
218 static inline void shell_run_forever(const shell_command_t *commands,
219  char *line_buf, int len)
220 {
221  while (1) {
222  shell_run_once(commands, line_buf, len);
223 
225  pm_off();
226  }
227  }
228 }
229 
237 static inline void shell_run(const shell_command_t *commands,
238  char *line_buf, int len)
239 {
240  shell_run_forever(commands, line_buf, len);
241 }
242 
253 int shell_handle_input_line(const shell_command_t *commands, char *line);
254 
267 int shell_parse_file(const shell_command_t *commands,
268  const char *filename, unsigned *line_nr);
269 
293 int shell_readline(char *buf, size_t size);
294 
295 #ifndef __cplusplus
325 #define SHELL_COMMAND(cmd, help, func) \
326  XFA_USE_CONST(shell_command_xfa_t, shell_commands_xfa_v2); \
327  static FLASH_ATTR const char _xfa_ ## cmd ## _cmd_name[] = #cmd; \
328  static FLASH_ATTR const char _xfa_ ## cmd ## _cmd_desc[] = help; \
329  XFA_CONST(shell_command_xfa_t, shell_commands_xfa_v2, 0) _xfa_ ## cmd ## _cmd = { \
330  .name = _xfa_ ## cmd ## _cmd_name, \
331  .desc = _xfa_ ## cmd ## _cmd_desc, \
332  .handler = &func \
333  };
334 #endif /* __cplusplus */
335 
336 #ifdef __cplusplus
337 }
338 #endif
339 
Utility functions, macros, and types for read-only memory.
void pm_off(void)
Turn off MCU completely.
#define FLASH_ATTR
C type qualifier required to place a variable in flash.
Definition: flash_utils.h:64
#define CONFIG_SHELL_SHUTDOWN_ON_EXIT
Shutdown RIOT on shell exit.
Definition: shell.h:79
void shell_pre_command_hook(int argc, char **argv)
Optional hook before shell command is called.
int shell_handle_input_line(const shell_command_t *commands, char *line)
Parse and run a line of text as a shell command with arguments.
void shell_run_once(const shell_command_t *commands, char *line_buf, int len)
Start a shell and exit once EOF is reached.
static void shell_run_forever(const shell_command_t *commands, char *line_buf, int len)
Start a shell and restart it if it exits.
Definition: shell.h:218
void shell_post_readline_hook(void)
Optional hook after readline has triggered.
int(* shell_command_handler_t)(int argc, char **argv)
Prototype of a shell callback handler.
Definition: shell.h:172
static void shell_run(const shell_command_t *commands, char *line_buf, int len)
Back-porting alias for shell_run_forever.
Definition: shell.h:237
int shell_readline(char *buf, size_t size)
Read a single line from standard input into a buffer.
void shell_post_command_hook(int ret, int argc, char **argv)
Optional hook after shell command is called.
struct shell_command_t shell_command_t
A single command in the list of the supported commands.
int shell_parse_file(const shell_command_t *commands, const char *filename, unsigned *line_nr)
Read shell commands from a file and run them.
Common macros and compiler attributes/pragmas configuration.
#define IS_ACTIVE(macro)
Allows to verify a macro definition outside the preprocessor.
Definition: modules.h:56
Power management interface.
A single command in the list of the supported commands.
Definition: shell.h:179
shell_command_handler_t handler
The callback function.
Definition: shell.h:182
const char * name
Name of the function.
Definition: shell.h:180
const char * desc
Description to print in the "help" command.
Definition: shell.h:181
A single command in the list of the supported commands.
Definition: shell.h:191
FLASH_ATTR const char * name
Name of the function.
Definition: shell.h:192
shell_command_handler_t handler
The callback function.
Definition: shell.h:195
FLASH_ATTR const char * desc
Description to print in the "help" command.
Definition: shell.h:193
Cross File Arrays.