1 | /** |
---|
2 | * @file rtems/shell.h |
---|
3 | * |
---|
4 | * @brief Instantatiate a New Terminal Shell |
---|
5 | */ |
---|
6 | |
---|
7 | /* |
---|
8 | * Author: |
---|
9 | * |
---|
10 | * WORK: fernando.ruiz@ctv.es |
---|
11 | * HOME: correo@fernando-ruiz.com |
---|
12 | * |
---|
13 | * Thanks at: |
---|
14 | * Chris Johns |
---|
15 | */ |
---|
16 | |
---|
17 | #ifndef __RTEMS_SHELL_H__ |
---|
18 | #define __RTEMS_SHELL_H__ |
---|
19 | |
---|
20 | #include <sys/types.h> |
---|
21 | #include <sys/stat.h> |
---|
22 | #include <rtems.h> |
---|
23 | #include <stdio.h> |
---|
24 | #include <termios.h> |
---|
25 | #include <rtems/fs.h> |
---|
26 | #include <rtems/libio.h> |
---|
27 | #include <rtems/chain.h> |
---|
28 | |
---|
29 | #ifdef __cplusplus |
---|
30 | extern "C" { |
---|
31 | #endif |
---|
32 | |
---|
33 | /* |
---|
34 | * Some key labels to define special keys. |
---|
35 | */ |
---|
36 | |
---|
37 | #define RTEMS_SHELL_KEYS_EXTENDED (0x8000) |
---|
38 | #define RTEMS_SHELL_KEYS_NORMAL_MASK (0x00ff) |
---|
39 | #define RTEMS_SHELL_KEYS_INS (0) |
---|
40 | #define RTEMS_SHELL_KEYS_DEL (1) |
---|
41 | #define RTEMS_SHELL_KEYS_UARROW (2) |
---|
42 | #define RTEMS_SHELL_KEYS_DARROW (3) |
---|
43 | #define RTEMS_SHELL_KEYS_LARROW (4) |
---|
44 | #define RTEMS_SHELL_KEYS_RARROW (5) |
---|
45 | #define RTEMS_SHELL_KEYS_HOME (6) |
---|
46 | #define RTEMS_SHELL_KEYS_END (7) |
---|
47 | #define RTEMS_SHELL_KEYS_F1 (8) |
---|
48 | #define RTEMS_SHELL_KEYS_F2 (9) |
---|
49 | #define RTEMS_SHELL_KEYS_F3 (10) |
---|
50 | #define RTEMS_SHELL_KEYS_F4 (11) |
---|
51 | #define RTEMS_SHELL_KEYS_F5 (12) |
---|
52 | #define RTEMS_SHELL_KEYS_F6 (13) |
---|
53 | #define RTEMS_SHELL_KEYS_F7 (14) |
---|
54 | #define RTEMS_SHELL_KEYS_F8 (15) |
---|
55 | #define RTEMS_SHELL_KEYS_F9 (16) |
---|
56 | #define RTEMS_SHELL_KEYS_F10 (17) |
---|
57 | |
---|
58 | typedef bool (*rtems_shell_login_check_t)( |
---|
59 | const char * /* user */, |
---|
60 | const char * /* passphrase */ |
---|
61 | ); |
---|
62 | |
---|
63 | extern bool rtems_shell_login_prompt( |
---|
64 | FILE *in, |
---|
65 | FILE *out, |
---|
66 | const char *device, |
---|
67 | rtems_shell_login_check_t check |
---|
68 | ); |
---|
69 | |
---|
70 | extern bool rtems_shell_login_check( |
---|
71 | const char *user, |
---|
72 | const char *passphrase |
---|
73 | ); |
---|
74 | |
---|
75 | typedef int (*rtems_shell_command_t)(int argc, char **argv); |
---|
76 | |
---|
77 | struct rtems_shell_cmd_tt; |
---|
78 | typedef struct rtems_shell_cmd_tt rtems_shell_cmd_t; |
---|
79 | |
---|
80 | struct rtems_shell_cmd_tt { |
---|
81 | const char *name; |
---|
82 | const char *usage; |
---|
83 | const char *topic; |
---|
84 | rtems_shell_command_t command; |
---|
85 | rtems_shell_cmd_t *alias; |
---|
86 | rtems_shell_cmd_t *next; |
---|
87 | mode_t mode; |
---|
88 | uid_t uid; |
---|
89 | gid_t gid; |
---|
90 | }; |
---|
91 | |
---|
92 | typedef struct { |
---|
93 | const char *name; |
---|
94 | const char *alias; |
---|
95 | } rtems_shell_alias_t; |
---|
96 | |
---|
97 | /* |
---|
98 | * The return value has RTEMS_SHELL_KEYS_EXTENDED set if the key |
---|
99 | * is extended, ie a special key. |
---|
100 | */ |
---|
101 | extern unsigned int rtems_shell_getchar(FILE *in); |
---|
102 | |
---|
103 | extern rtems_shell_cmd_t * rtems_shell_lookup_cmd(const char *cmd); |
---|
104 | |
---|
105 | extern rtems_shell_cmd_t *rtems_shell_add_cmd_struct( |
---|
106 | rtems_shell_cmd_t *shell_cmd |
---|
107 | ); |
---|
108 | |
---|
109 | rtems_shell_cmd_t * rtems_shell_add_cmd( |
---|
110 | const char *cmd, |
---|
111 | const char *topic, |
---|
112 | const char *usage, |
---|
113 | rtems_shell_command_t command |
---|
114 | ); |
---|
115 | |
---|
116 | extern rtems_shell_cmd_t * rtems_shell_alias_cmd( |
---|
117 | const char *cmd, |
---|
118 | const char *alias |
---|
119 | ); |
---|
120 | |
---|
121 | extern int rtems_shell_make_args( |
---|
122 | char *commandLine, |
---|
123 | int *argc_p, |
---|
124 | char **argv_p, |
---|
125 | int max_args |
---|
126 | ); |
---|
127 | |
---|
128 | extern int rtems_shell_cat_file( |
---|
129 | FILE *out, |
---|
130 | const char *name |
---|
131 | ); |
---|
132 | |
---|
133 | extern void rtems_shell_write_file( |
---|
134 | const char *name, |
---|
135 | const char *content |
---|
136 | ); |
---|
137 | |
---|
138 | extern int rtems_shell_script_file( |
---|
139 | int argc, |
---|
140 | char **argv |
---|
141 | ); |
---|
142 | |
---|
143 | /** |
---|
144 | * Initialise the shell creating tasks to login and run the shell |
---|
145 | * sessions. |
---|
146 | * |
---|
147 | * @param task_name Name of the shell task. |
---|
148 | * @param task_stacksize The size of the stack. If 0 the default size is used. |
---|
149 | * @param task_priority The priority the shell runs at. |
---|
150 | * @param forever Repeat logins. |
---|
151 | * @param wait Caller should block until shell exits. |
---|
152 | * @param login_check User login check function, NULL disables login checks. |
---|
153 | * |
---|
154 | */ |
---|
155 | extern rtems_status_code rtems_shell_init( |
---|
156 | const char *task_name, |
---|
157 | size_t task_stacksize, |
---|
158 | rtems_task_priority task_priority, |
---|
159 | const char *devname, |
---|
160 | bool forever, |
---|
161 | bool wait, |
---|
162 | rtems_shell_login_check_t login_check |
---|
163 | ); |
---|
164 | |
---|
165 | /** |
---|
166 | * Run a shell script creating a shell tasks to execute the command under. |
---|
167 | * |
---|
168 | * @param task_name Name of the shell task. |
---|
169 | * @param task_stacksize The size of the stack. If 0 the default size is used. |
---|
170 | * @param task_priority The priority the shell runs at. |
---|
171 | * @param input The file of commands. Can be 'stdin' to use stdin. |
---|
172 | * @param output The output file to write commands to. Can be 'stdout', |
---|
173 | * 'stderr' or '/dev/null'. |
---|
174 | * @param output_append Append the output to the file or truncate the file. |
---|
175 | * Create if it does not exist. |
---|
176 | * @param wait Wait for the script to finish. |
---|
177 | */ |
---|
178 | extern rtems_status_code rtems_shell_script( |
---|
179 | const char *task_name, |
---|
180 | size_t task_stacksize, /* 0 default*/ |
---|
181 | rtems_task_priority task_priority, |
---|
182 | const char *input, |
---|
183 | const char *output, |
---|
184 | bool output_append, |
---|
185 | bool wait, |
---|
186 | bool echo |
---|
187 | ); |
---|
188 | |
---|
189 | /** |
---|
190 | * Private environment associated with each shell instance. |
---|
191 | */ |
---|
192 | typedef struct { |
---|
193 | /** 'S','E','N','V': Shell Environment */ |
---|
194 | rtems_name magic; |
---|
195 | const char *devname; |
---|
196 | const char *taskname; |
---|
197 | bool exit_shell; /* logout */ |
---|
198 | bool forever; /* repeat login */ |
---|
199 | int errorlevel; |
---|
200 | bool echo; |
---|
201 | char cwd[256]; |
---|
202 | const char *input; |
---|
203 | const char *output; |
---|
204 | bool output_append; |
---|
205 | rtems_id wake_on_end; |
---|
206 | rtems_shell_login_check_t login_check; |
---|
207 | |
---|
208 | /** |
---|
209 | * @brief The real and effective UID of the shell task in case no login check |
---|
210 | * is present. |
---|
211 | */ |
---|
212 | uid_t uid; |
---|
213 | |
---|
214 | /** |
---|
215 | * @brief The real and effective GID of the shell task in case no login check |
---|
216 | * is present. |
---|
217 | */ |
---|
218 | gid_t gid; |
---|
219 | } rtems_shell_env_t; |
---|
220 | |
---|
221 | bool rtems_shell_main_loop( |
---|
222 | rtems_shell_env_t *rtems_shell_env |
---|
223 | ); |
---|
224 | |
---|
225 | extern const rtems_shell_env_t rtems_global_shell_env; |
---|
226 | |
---|
227 | rtems_shell_env_t *rtems_shell_get_current_env(void); |
---|
228 | void rtems_shell_dup_current_env(rtems_shell_env_t *); |
---|
229 | |
---|
230 | /* |
---|
231 | * The types of file systems we can mount. We have them broken out |
---|
232 | * out like this so they can be configured by shellconfig.h. The |
---|
233 | * mount command needs special treatment due to some file systems |
---|
234 | * being dependent on the network stack and some not. If we had |
---|
235 | * all possible file systems being included it would force the |
---|
236 | * networking stack into the applcation and this may not be |
---|
237 | * required. |
---|
238 | */ |
---|
239 | struct rtems_shell_filesystems_tt; |
---|
240 | typedef struct rtems_shell_filesystems_tt rtems_shell_filesystems_t; |
---|
241 | |
---|
242 | typedef int (*rtems_shell_filesystems_mounter_t)( |
---|
243 | const char* driver, |
---|
244 | const char* path, |
---|
245 | rtems_shell_filesystems_t* fs, |
---|
246 | rtems_filesystem_options_t options |
---|
247 | ); |
---|
248 | |
---|
249 | struct rtems_shell_filesystems_tt { |
---|
250 | rtems_chain_node link; |
---|
251 | const char *name; |
---|
252 | int driver_needed; |
---|
253 | const rtems_filesystem_operations_table *fs_ops; |
---|
254 | rtems_shell_filesystems_mounter_t mounter; |
---|
255 | }; |
---|
256 | |
---|
257 | /** |
---|
258 | * This method dynamically builds the command line prompt string |
---|
259 | * and places it in @a prompt. |
---|
260 | * |
---|
261 | * @param[in] shell_env is the shell execution environment |
---|
262 | * @param[in] prompt is a pointer to a string buffer area |
---|
263 | * @param[in] size is length of the prompt buffer area |
---|
264 | * |
---|
265 | * @return This method fills in the memory pointed to by @a prompt. |
---|
266 | * |
---|
267 | * @note An application specific implementation can be provided |
---|
268 | * by the user. |
---|
269 | */ |
---|
270 | extern void rtems_shell_get_prompt( |
---|
271 | rtems_shell_env_t *shell_env, |
---|
272 | char *prompt, |
---|
273 | size_t size |
---|
274 | ); |
---|
275 | |
---|
276 | /** |
---|
277 | * Helper for the mount command. |
---|
278 | * |
---|
279 | * @param[in] driver The path to the driver. |
---|
280 | * @param[in] path The path to mount on. |
---|
281 | * @param[in] fs The file system definition. |
---|
282 | * @param[in] options Special file system options. |
---|
283 | */ |
---|
284 | extern int rtems_shell_libc_mounter( |
---|
285 | const char* driver, |
---|
286 | const char* path, |
---|
287 | rtems_shell_filesystems_t* fs, |
---|
288 | rtems_filesystem_options_t options |
---|
289 | ); |
---|
290 | |
---|
291 | /** |
---|
292 | * Add a new file system mount configuration to the mount command. |
---|
293 | * |
---|
294 | * @param[in] fs The file system mount data. |
---|
295 | */ |
---|
296 | extern void rtems_shell_mount_add_fsys(rtems_shell_filesystems_t* fs); |
---|
297 | |
---|
298 | /** |
---|
299 | * Delete file system mount configuration from the mount command. |
---|
300 | * |
---|
301 | * @param[in] fs The file system mount data to remove. |
---|
302 | */ |
---|
303 | extern void rtems_shell_mount_del_fsys(rtems_shell_filesystems_t* fs); |
---|
304 | |
---|
305 | typedef void (*rtems_shell_wait_for_input_notification)( |
---|
306 | int fd, |
---|
307 | int seconds_remaining, |
---|
308 | void *arg |
---|
309 | ); |
---|
310 | |
---|
311 | /** |
---|
312 | * @brief Waits for input. |
---|
313 | * |
---|
314 | * @retval RTEMS_SUCCESSFUL Input detected. |
---|
315 | * @retval RTEMS_TIMEOUT Timeout expired. |
---|
316 | * @retval RTEMS_UNSATISFIED Cannot change or restore termios attributes. |
---|
317 | */ |
---|
318 | extern rtems_status_code rtems_shell_wait_for_input( |
---|
319 | int fd, |
---|
320 | int timeout_in_seconds, |
---|
321 | rtems_shell_wait_for_input_notification notification, |
---|
322 | void *notification_arg |
---|
323 | ); |
---|
324 | |
---|
325 | extern int rtems_shell_main_monitor(int argc, char **argv); |
---|
326 | |
---|
327 | /* |
---|
328 | * Provide these commands for application use, as their implementation |
---|
329 | * is tedious. |
---|
330 | */ |
---|
331 | int rtems_shell_main_mv(int argc, char *argv[]); |
---|
332 | int rtems_shell_main_cp(int argc, char *argv[]); |
---|
333 | int rtems_shell_main_rm(int argc, char *argv[]); |
---|
334 | |
---|
335 | #ifdef __cplusplus |
---|
336 | } |
---|
337 | #endif |
---|
338 | |
---|
339 | #endif |
---|