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 
20 #ifndef SHELL_H
21 #define SHELL_H
22 
23 #include <stdint.h>
24 #include "periph/pm.h"
25 
26 #include "kernel_defines.h"
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
35 #ifndef CONFIG_SHELL_SHUTDOWN_ON_EXIT
36 /* Some systems (e.g Ubuntu 20.04) close stdin on CTRL-D / EOF
37  * That means we can't just re-start the shell.
38  * Instead terminate RIOT, which is also the behavior a user would
39  * expect from a CLI application.
40  */
41 # ifdef CPU_NATIVE
42 # define CONFIG_SHELL_SHUTDOWN_ON_EXIT 1
43 # endif
44 #endif
45 
49 #define SHELL_DEFAULT_BUFSIZE (128)
50 
57 void shell_post_readline_hook(void);
58 
68 void shell_pre_command_hook(int argc, char **argv);
69 
80 void shell_post_command_hook(int ret, int argc, char **argv);
81 
99 typedef int (*shell_command_handler_t)(int argc, char **argv);
100 
106 typedef struct shell_command_t {
107  const char *name;
108  const char *desc;
111 
119 void shell_run_once(const shell_command_t *commands, char *line_buf, int len);
120 
131 static inline void shell_run_forever(const shell_command_t *commands,
132  char *line_buf, int len)
133 {
134  while (1) {
135  shell_run_once(commands, line_buf, len);
136 
137  if (IS_ACTIVE(CONFIG_SHELL_SHUTDOWN_ON_EXIT)) {
138  pm_off();
139  }
140  }
141 }
142 
150 static inline void shell_run(const shell_command_t *commands,
151  char *line_buf, int len)
152 {
153  shell_run_forever(commands, line_buf, len);
154 }
155 
156 #ifdef __cplusplus
157 }
158 #endif
159 
160 #endif /* SHELL_H */
161 
shell_run_once
void shell_run_once(const shell_command_t *commands, char *line_buf, int len)
Start a shell and exit once EOF is reached.
shell_command_handler_t
int(* shell_command_handler_t)(int argc, char **argv)
Protype of a shell callback handler.
Definition: shell.h:99
pm_off
void pm_off(void)
Turn off MCU completely.
kernel_defines.h
Common macros and compiler attributes/pragmas configuration.
shell_run
static void shell_run(const shell_command_t *commands, char *line_buf, int len)
Back-porting alias for shell_run_forever.
Definition: shell.h:150
shell_command_t::name
const char * name
Name of the function.
Definition: shell.h:107
shell_post_readline_hook
void shell_post_readline_hook(void)
Optional hook after readline has triggered.
shell_post_command_hook
void shell_post_command_hook(int ret, int argc, char **argv)
Optional hook after shell command is called.
shell_pre_command_hook
void shell_pre_command_hook(int argc, char **argv)
Optional hook before shell command is called.
shell_command_t
A single command in the list of the supported commands.
Definition: shell.h:106
shell_run_forever
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:131
shell_command_t
struct shell_command_t shell_command_t
A single command in the list of the supported commands.
IS_ACTIVE
#define IS_ACTIVE(macro)
Allows to verify a macro definition outside the preprocessor.
Definition: kernel_defines.h:177
shell_command_t::desc
const char * desc
Description to print in the "help" command.
Definition: shell.h:108
pm.h
Power management interface.
shell_command_t::handler
shell_command_handler_t handler
The callback function.
Definition: shell.h:109