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 
9 #pragma once
10 
49 #include <stdint.h>
50 #include "periph/pm.h"
51 
52 #include "modules.h"
53 #include "xfa.h"
54 
55 #ifndef __cplusplus
56 #include "flash_utils.h"
57 #endif
58 
59 #ifdef __cplusplus
60 extern "C" {
61 #endif
62 
73 #ifndef CONFIG_SHELL_SHUTDOWN_ON_EXIT
74 /* Some systems (e.g Ubuntu 20.04) close stdin on CTRL-D / EOF
75  * That means we can't just re-start the shell.
76  * Instead terminate RIOT, which is also the behavior a user would
77  * expect from a CLI application.
78  */
79 # if defined(CPU_NATIVE) && !IS_ACTIVE(MODULE_SHELL_LOCK)
80 # define CONFIG_SHELL_SHUTDOWN_ON_EXIT 1
81 # else
82 # define CONFIG_SHELL_SHUTDOWN_ON_EXIT 0
83 # endif
84 #endif
85 
89 #ifndef CONFIG_SHELL_NO_ECHO
90 #define CONFIG_SHELL_NO_ECHO 0
91 #endif
92 
96 #ifndef CONFIG_SHELL_NO_PROMPT
97 #define CONFIG_SHELL_NO_PROMPT 0
98 #endif
99 
118 #define SHELL_DEFAULT_BUFSIZE (128)
119 
127 
137 void shell_pre_command_hook(int argc, char **argv);
138 
149 void shell_post_command_hook(int ret, int argc, char **argv);
150 
175 typedef int (*shell_command_handler_t)(int argc, char **argv);
176 
182 typedef struct shell_command_t {
183  const char *name;
184  const char *desc;
187 
188 #ifndef __cplusplus
194 typedef struct {
195  FLASH_ATTR const char *name;
196  FLASH_ATTR const char *desc;
200 #endif /* __cplusplus */
201 
209 void shell_run_once(const shell_command_t *commands, char *line_buf, int len);
210 
221 static inline void shell_run_forever(const shell_command_t *commands,
222  char *line_buf, int len)
223 {
224  while (1) {
225  shell_run_once(commands, line_buf, len);
226 
228  pm_off();
229  }
230  }
231 }
232 
240 static inline void shell_run(const shell_command_t *commands,
241  char *line_buf, int len)
242 {
243  shell_run_forever(commands, line_buf, len);
244 }
245 
256 int shell_handle_input_line(const shell_command_t *commands, char *line);
257 
270 int shell_parse_file(const shell_command_t *commands,
271  const char *filename, unsigned *line_nr);
272 
296 int shell_readline(char *buf, size_t size);
297 
298 #ifndef __cplusplus
328 #define SHELL_COMMAND(cmd, help, func) \
329  XFA_USE_CONST(shell_command_xfa_t, shell_commands_xfa_v2); \
330  static FLASH_ATTR const char _xfa_ ## cmd ## _cmd_name[] = #cmd; \
331  static FLASH_ATTR const char _xfa_ ## cmd ## _cmd_desc[] = help; \
332  XFA_CONST(shell_command_xfa_t, shell_commands_xfa_v2, 0) _xfa_ ## cmd ## _cmd = { \
333  .name = _xfa_ ## cmd ## _cmd_name, \
334  .desc = _xfa_ ## cmd ## _cmd_desc, \
335  .handler = &func \
336  };
337 #endif /* __cplusplus */
338 
339 #ifdef __cplusplus
340 }
341 #endif
342 
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:67
#define CONFIG_SHELL_SHUTDOWN_ON_EXIT
Shutdown RIOT on shell exit.
Definition: shell.h:82
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:221
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:175
static void shell_run(const shell_command_t *commands, char *line_buf, int len)
Back-porting alias for shell_run_forever.
Definition: shell.h:240
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:59
Power management interface.
A single command in the list of the supported commands.
Definition: shell.h:182
shell_command_handler_t handler
The callback function.
Definition: shell.h:185
const char * name
Name of the function.
Definition: shell.h:183
const char * desc
Description to print in the "help" command.
Definition: shell.h:184
A single command in the list of the supported commands.
Definition: shell.h:194
FLASH_ATTR const char * name
Name of the function.
Definition: shell.h:195
shell_command_handler_t handler
The callback function.
Definition: shell.h:198
FLASH_ATTR const char * desc
Description to print in the "help" command.
Definition: shell.h:196
Cross File Arrays.