1 | /** |
---|
2 | * @file rtems/shell.h |
---|
3 | * |
---|
4 | * 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 | * $Id$ |
---|
17 | */ |
---|
18 | |
---|
19 | #ifndef __RTEMS_SHELL_H__ |
---|
20 | #define __RTEMS_SHELL_H__ |
---|
21 | |
---|
22 | #include <rtems.h> |
---|
23 | #include <stdio.h> |
---|
24 | #include <termios.h> |
---|
25 | #include <rtems/fs.h> |
---|
26 | #include <rtems/libio.h> |
---|
27 | |
---|
28 | #ifdef __cplusplus |
---|
29 | extern "C" { |
---|
30 | #endif |
---|
31 | |
---|
32 | /* |
---|
33 | * Some key labels to define special keys. |
---|
34 | */ |
---|
35 | |
---|
36 | #define RTEMS_SHELL_KEYS_EXTENDED (0x8000) |
---|
37 | #define RTEMS_SHELL_KEYS_NORMAL_MASK (0x00ff) |
---|
38 | #define RTEMS_SHELL_KEYS_INS (0) |
---|
39 | #define RTEMS_SHELL_KEYS_DEL (1) |
---|
40 | #define RTEMS_SHELL_KEYS_UARROW (2) |
---|
41 | #define RTEMS_SHELL_KEYS_DARROW (3) |
---|
42 | #define RTEMS_SHELL_KEYS_LARROW (4) |
---|
43 | #define RTEMS_SHELL_KEYS_RARROW (5) |
---|
44 | #define RTEMS_SHELL_KEYS_HOME (6) |
---|
45 | #define RTEMS_SHELL_KEYS_END (7) |
---|
46 | #define RTEMS_SHELL_KEYS_F1 (8) |
---|
47 | #define RTEMS_SHELL_KEYS_F2 (9) |
---|
48 | #define RTEMS_SHELL_KEYS_F3 (10) |
---|
49 | #define RTEMS_SHELL_KEYS_F4 (11) |
---|
50 | #define RTEMS_SHELL_KEYS_F5 (12) |
---|
51 | #define RTEMS_SHELL_KEYS_F6 (13) |
---|
52 | #define RTEMS_SHELL_KEYS_F7 (14) |
---|
53 | #define RTEMS_SHELL_KEYS_F8 (15) |
---|
54 | #define RTEMS_SHELL_KEYS_F9 (16) |
---|
55 | #define RTEMS_SHELL_KEYS_F10 (17) |
---|
56 | |
---|
57 | typedef int (*rtems_shell_command_t)(int argc, char **argv); |
---|
58 | |
---|
59 | struct rtems_shell_cmd_tt; |
---|
60 | typedef struct rtems_shell_cmd_tt rtems_shell_cmd_t; |
---|
61 | |
---|
62 | struct rtems_shell_cmd_tt { |
---|
63 | const char *name; |
---|
64 | const char *usage; |
---|
65 | const char *topic; |
---|
66 | rtems_shell_command_t command; |
---|
67 | rtems_shell_cmd_t *alias; |
---|
68 | rtems_shell_cmd_t *next; |
---|
69 | }; |
---|
70 | |
---|
71 | typedef struct { |
---|
72 | const char *name; |
---|
73 | const char *alias; |
---|
74 | } rtems_shell_alias_t; |
---|
75 | |
---|
76 | /* |
---|
77 | * The return value has RTEMS_SHELL_KEYS_EXTENDED set if the key |
---|
78 | * is extended, ie a special key. |
---|
79 | */ |
---|
80 | unsigned int rtems_shell_getchar(FILE *in); |
---|
81 | |
---|
82 | rtems_shell_cmd_t * rtems_shell_lookup_cmd(const char *cmd); |
---|
83 | |
---|
84 | rtems_shell_cmd_t *rtems_shell_add_cmd_struct( |
---|
85 | rtems_shell_cmd_t *shell_cmd |
---|
86 | ); |
---|
87 | |
---|
88 | rtems_shell_cmd_t * rtems_shell_add_cmd( |
---|
89 | const char *cmd, |
---|
90 | const char *topic, |
---|
91 | const char *usage, |
---|
92 | rtems_shell_command_t command |
---|
93 | ); |
---|
94 | |
---|
95 | rtems_shell_cmd_t * rtems_shell_alias_cmd( |
---|
96 | const char *cmd, |
---|
97 | const char *alias |
---|
98 | ); |
---|
99 | |
---|
100 | int rtems_shell_make_args( |
---|
101 | char *commandLine, |
---|
102 | int *argc_p, |
---|
103 | char **argv_p, |
---|
104 | int max_args |
---|
105 | ); |
---|
106 | |
---|
107 | int rtems_shell_scanline( |
---|
108 | char *line, |
---|
109 | int size, |
---|
110 | FILE *in, |
---|
111 | FILE *out |
---|
112 | ); |
---|
113 | |
---|
114 | int rtems_shell_cat_file( |
---|
115 | FILE *out, |
---|
116 | const char *name |
---|
117 | ); |
---|
118 | |
---|
119 | void rtems_shell_write_file( |
---|
120 | const char *name, |
---|
121 | const char *content |
---|
122 | ); |
---|
123 | |
---|
124 | int rtems_shell_script_file( |
---|
125 | int argc, |
---|
126 | char **argv |
---|
127 | ); |
---|
128 | |
---|
129 | /** |
---|
130 | * Initialise the shell creating tasks to login and run the shell |
---|
131 | * sessions. |
---|
132 | * |
---|
133 | * @param task_name Name of the shell task. |
---|
134 | * @param task_stacksize The size of the stack. If 0 the default size is used. |
---|
135 | * @param task_priority The priority the shell runs at. |
---|
136 | * @param forever Repeat logins. |
---|
137 | * @param wait Caller should block until shell exits. |
---|
138 | * @param login Demand user login. |
---|
139 | * |
---|
140 | */ |
---|
141 | rtems_status_code rtems_shell_init( |
---|
142 | const char *task_name, |
---|
143 | size_t task_stacksize, /* 0 default*/ |
---|
144 | rtems_task_priority task_priority, |
---|
145 | const char *devname, |
---|
146 | bool forever, |
---|
147 | bool wait, |
---|
148 | bool login |
---|
149 | ); |
---|
150 | |
---|
151 | /** |
---|
152 | * Run a shell script creating a shell tasks to execute the command under. |
---|
153 | * |
---|
154 | * @param task_name Name of the shell task. |
---|
155 | * @param task_stacksize The size of the stack. If 0 the default size is used. |
---|
156 | * @param task_priority The priority the shell runs at. |
---|
157 | * @param input The file of commands. Can be 'stdin' to use stdin. |
---|
158 | * @param output The output file to write commands to. Can be 'stdout', |
---|
159 | * 'stderr' or '/dev/null'. |
---|
160 | * @param output_append Append the output to the file or truncate the file. |
---|
161 | * Create if it does not exist. |
---|
162 | * @param wait Wait for the script to finish. |
---|
163 | */ |
---|
164 | rtems_status_code rtems_shell_script( |
---|
165 | const char *task_name, |
---|
166 | size_t task_stacksize, /* 0 default*/ |
---|
167 | rtems_task_priority task_priority, |
---|
168 | const char *input, |
---|
169 | const char *output, |
---|
170 | bool output_append, |
---|
171 | bool wait, |
---|
172 | bool echo |
---|
173 | ); |
---|
174 | |
---|
175 | /* |
---|
176 | * Things that are useful to external entities developing commands and plugging |
---|
177 | * them in. |
---|
178 | */ |
---|
179 | int rtems_shell_str2int(const char * s); |
---|
180 | |
---|
181 | typedef struct { |
---|
182 | rtems_name magic; /* 'S','E','N','V': Shell Environment */ |
---|
183 | const char *devname; |
---|
184 | const char *taskname; |
---|
185 | /* user extensions */ |
---|
186 | bool exit_shell; /* logout */ |
---|
187 | bool forever ; /* repeat login */ |
---|
188 | int errorlevel; |
---|
189 | bool echo; |
---|
190 | char cwd[256]; |
---|
191 | const char* input; |
---|
192 | const char* output; |
---|
193 | bool output_append; |
---|
194 | rtems_id wake_on_end; |
---|
195 | bool login; |
---|
196 | } rtems_shell_env_t; |
---|
197 | |
---|
198 | bool rtems_shell_main_loop( |
---|
199 | rtems_shell_env_t *rtems_shell_env |
---|
200 | ); |
---|
201 | |
---|
202 | extern rtems_shell_env_t rtems_global_shell_env; |
---|
203 | extern rtems_shell_env_t *rtems_current_shell_env; |
---|
204 | |
---|
205 | /* |
---|
206 | * The types of file systems we can mount. We have them broken out |
---|
207 | * out like this so they can be configured by shellconfig.h. The |
---|
208 | * mount command needs special treatment due to some file systems |
---|
209 | * being dependent on the network stack and some not. If we had |
---|
210 | * all possible file systems being included it would force the |
---|
211 | * networking stack into the applcation and this may not be |
---|
212 | * required. |
---|
213 | */ |
---|
214 | struct rtems_shell_filesystems_tt; |
---|
215 | typedef struct rtems_shell_filesystems_tt rtems_shell_filesystems_t; |
---|
216 | |
---|
217 | typedef int (*rtems_shell_filesystems_mounter_t)( |
---|
218 | const char* driver, |
---|
219 | const char* path, |
---|
220 | rtems_shell_filesystems_t* fs, |
---|
221 | rtems_filesystem_options_t options |
---|
222 | ); |
---|
223 | |
---|
224 | struct rtems_shell_filesystems_tt { |
---|
225 | const char* name; |
---|
226 | int driver_needed; |
---|
227 | rtems_filesystem_operations_table* fs_ops; |
---|
228 | rtems_shell_filesystems_mounter_t mounter; |
---|
229 | }; |
---|
230 | |
---|
231 | /** |
---|
232 | * This method dynamically builds the command line prompt string |
---|
233 | * and places it in @a prompt. |
---|
234 | * |
---|
235 | * @param[in] shell_env is the shell execution environment |
---|
236 | * @param[in] prompt is a pointer to a string buffer area |
---|
237 | * @param[in] size is length of the prompt buffer area |
---|
238 | * |
---|
239 | * @return This method fills in the memory pointed to by @a prompt. |
---|
240 | * |
---|
241 | * @note An application specific implementation can be provided |
---|
242 | * by the user. |
---|
243 | */ |
---|
244 | void rtems_shell_get_prompt( |
---|
245 | rtems_shell_env_t *shell_env, |
---|
246 | char *prompt, |
---|
247 | size_t size |
---|
248 | ); |
---|
249 | |
---|
250 | #ifdef __cplusplus |
---|
251 | } |
---|
252 | #endif |
---|
253 | |
---|
254 | #endif |
---|