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