Loading...
Searching...
No Matches
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
28#ifndef SHELL_H
29#define SHELL_H
30
31#include <stdint.h>
32#include "periph/pm.h"
33
34#include "modules.h"
35#include "xfa.h"
36
37#ifndef __cplusplus
38#include "flash_utils.h"
39#endif
40
41#ifdef __cplusplus
42extern "C" {
43#endif
44
55#ifndef CONFIG_SHELL_SHUTDOWN_ON_EXIT
56/* Some systems (e.g Ubuntu 20.04) close stdin on CTRL-D / EOF
57 * That means we can't just re-start the shell.
58 * Instead terminate RIOT, which is also the behavior a user would
59 * expect from a CLI application.
60 */
61# if defined(CPU_NATIVE) && !IS_ACTIVE(MODULE_SHELL_LOCK)
62# define CONFIG_SHELL_SHUTDOWN_ON_EXIT 1
63# else
64# define CONFIG_SHELL_SHUTDOWN_ON_EXIT 0
65# endif
66#endif
67
71#ifndef CONFIG_SHELL_NO_ECHO
72#define CONFIG_SHELL_NO_ECHO 0
73#endif
74
78#ifndef CONFIG_SHELL_NO_PROMPT
79#define CONFIG_SHELL_NO_PROMPT 0
80#endif
81
100#define SHELL_DEFAULT_BUFSIZE (128)
101
109
119void shell_pre_command_hook(int argc, char **argv);
120
131void shell_post_command_hook(int ret, int argc, char **argv);
132
157typedef int (*shell_command_handler_t)(int argc, char **argv);
158
169
170#ifndef __cplusplus
182#endif /* __cplusplus */
183
191void shell_run_once(const shell_command_t *commands, char *line_buf, int len);
192
203static inline void shell_run_forever(const shell_command_t *commands,
204 char *line_buf, int len)
205{
206 while (1) {
207 shell_run_once(commands, line_buf, len);
208
210 pm_off();
211 }
212 }
213}
214
222static inline void shell_run(const shell_command_t *commands,
223 char *line_buf, int len)
224{
225 shell_run_forever(commands, line_buf, len);
226}
227
238int shell_handle_input_line(const shell_command_t *commands, char *line);
239
253 const char *filename, unsigned *line_nr);
254
255#ifndef __cplusplus
285#define SHELL_COMMAND(cmd, help, func) \
286 XFA_USE_CONST(shell_command_xfa_t*, shell_commands_xfa); \
287 static FLASH_ATTR const char _xfa_ ## cmd ## _cmd_name[] = #cmd; \
288 static FLASH_ATTR const char _xfa_ ## cmd ## _cmd_desc[] = help; \
289 static const shell_command_xfa_t _xfa_ ## cmd ## _cmd = { \
290 .name = _xfa_ ## cmd ## _cmd_name, \
291 .desc = _xfa_ ## cmd ## _cmd_desc, \
292 .handler = &func \
293 }; \
294 XFA_ADD_PTR(shell_commands_xfa, cmd, cmd, &_xfa_ ## cmd ## _cmd)
295#endif /* __cplusplus */
296
297#ifdef __cplusplus
298}
299#endif
300
301#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:64
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:203
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:157
static void shell_run(const shell_command_t *commands, char *line_buf, int len)
Back-porting alias for shell_run_forever.
Definition shell.h:222
void shell_post_command_hook(int ret, int argc, char **argv)
Optional hook after shell command is called.
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:164
shell_command_handler_t handler
The callback function.
Definition shell.h:167
const char * name
Name of the function.
Definition shell.h:165
const char * desc
Description to print in the "help" command.
Definition shell.h:166
A single command in the list of the supported commands.
Definition shell.h:176
FLASH_ATTR const char * name
Name of the function.
Definition shell.h:177
shell_command_handler_t handler
The callback function.
Definition shell.h:180
FLASH_ATTR const char * desc
Description to print in the "help" command.
Definition shell.h:178
Cross File Arrays.