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