shell.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2009-2013 Freie Universität Berlin
3  *
4  * This file is subject to the terms and conditions of the GNU Lesser
5  * General Public License v2.1. See the file LICENSE in the top level
6  * directory for more details.
7  */
8 
47 #ifndef SHELL_H
48 #define SHELL_H
49 
50 #include <stdint.h>
51 #include "periph/pm.h"
52 
53 #include "modules.h"
54 #include "xfa.h"
55 
56 #ifndef __cplusplus
57 #include "flash_utils.h"
58 #endif
59 
60 #ifdef __cplusplus
61 extern "C" {
62 #endif
63 
74 #ifndef CONFIG_SHELL_SHUTDOWN_ON_EXIT
75 /* Some systems (e.g Ubuntu 20.04) close stdin on CTRL-D / EOF
76  * That means we can't just re-start the shell.
77  * Instead terminate RIOT, which is also the behavior a user would
78  * expect from a CLI application.
79  */
80 # if defined(CPU_NATIVE) && !IS_ACTIVE(MODULE_SHELL_LOCK)
81 # define CONFIG_SHELL_SHUTDOWN_ON_EXIT 1
82 # else
83 # define CONFIG_SHELL_SHUTDOWN_ON_EXIT 0
84 # endif
85 #endif
86 
90 #ifndef CONFIG_SHELL_NO_ECHO
91 #define CONFIG_SHELL_NO_ECHO 0
92 #endif
93 
97 #ifndef CONFIG_SHELL_NO_PROMPT
98 #define CONFIG_SHELL_NO_PROMPT 0
99 #endif
100 
119 #define SHELL_DEFAULT_BUFSIZE (128)
120 
128 
138 void shell_pre_command_hook(int argc, char **argv);
139 
150 void shell_post_command_hook(int ret, int argc, char **argv);
151 
176 typedef int (*shell_command_handler_t)(int argc, char **argv);
177 
183 typedef struct shell_command_t {
184  const char *name;
185  const char *desc;
188 
189 #ifndef __cplusplus
195 typedef struct {
196  FLASH_ATTR const char *name;
197  FLASH_ATTR const char *desc;
201 #endif /* __cplusplus */
202 
210 void shell_run_once(const shell_command_t *commands, char *line_buf, int len);
211 
222 static inline void shell_run_forever(const shell_command_t *commands,
223  char *line_buf, int len)
224 {
225  while (1) {
226  shell_run_once(commands, line_buf, len);
227 
229  pm_off();
230  }
231  }
232 }
233 
241 static inline void shell_run(const shell_command_t *commands,
242  char *line_buf, int len)
243 {
244  shell_run_forever(commands, line_buf, len);
245 }
246 
257 int shell_handle_input_line(const shell_command_t *commands, char *line);
258 
271 int shell_parse_file(const shell_command_t *commands,
272  const char *filename, unsigned *line_nr);
273 
274 #ifndef __cplusplus
304 #define SHELL_COMMAND(cmd, help, func) \
305  XFA_USE_CONST(shell_command_xfa_t, shell_commands_xfa_v2); \
306  static FLASH_ATTR const char _xfa_ ## cmd ## _cmd_name[] = #cmd; \
307  static FLASH_ATTR const char _xfa_ ## cmd ## _cmd_desc[] = help; \
308  XFA_CONST(shell_command_xfa_t, shell_commands_xfa_v2, 0) _xfa_ ## cmd ## _cmd = { \
309  .name = _xfa_ ## cmd ## _cmd_name, \
310  .desc = _xfa_ ## cmd ## _cmd_desc, \
311  .handler = &func \
312  };
313 #endif /* __cplusplus */
314 
315 #ifdef __cplusplus
316 }
317 #endif
318 
319 #endif /* SHELL_H */
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:68
#define CONFIG_SHELL_SHUTDOWN_ON_EXIT
Shutdown RIOT on shell exit.
Definition: shell.h:83
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:222
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:176
static void shell_run(const shell_command_t *commands, char *line_buf, int len)
Back-porting alias for shell_run_forever.
Definition: shell.h:241
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:60
Power management interface.
A single command in the list of the supported commands.
Definition: shell.h:183
shell_command_handler_t handler
The callback function.
Definition: shell.h:186
const char * name
Name of the function.
Definition: shell.h:184
const char * desc
Description to print in the "help" command.
Definition: shell.h:185
A single command in the list of the supported commands.
Definition: shell.h:195
FLASH_ATTR const char * name
Name of the function.
Definition: shell.h:196
shell_command_handler_t handler
The callback function.
Definition: shell.h:199
FLASH_ATTR const char * desc
Description to print in the "help" command.
Definition: shell.h:197
Cross File Arrays.