[f40778b] | 1 | /* |
---|
[a2aad55] | 2 | * COPYRIGHT (c) 1989-2013. |
---|
[f40778b] | 3 | * On-Line Applications Research Corporation (OAR). |
---|
| 4 | * |
---|
| 5 | * The license and distribution terms for this file may be |
---|
| 6 | * found in the file LICENSE in this distribution or at |
---|
| 7 | * http://www.rtems.com/license/LICENSE. |
---|
| 8 | */ |
---|
| 9 | |
---|
| 10 | #include <newlib/getopt.h> |
---|
| 11 | #include <stdio.h> |
---|
| 12 | #include <stdlib.h> |
---|
| 13 | #include <string.h> |
---|
| 14 | #include <ctype.h> |
---|
| 15 | |
---|
| 16 | #include "shell.h" |
---|
| 17 | #include "rtems_sched.h" |
---|
| 18 | |
---|
| 19 | /* |
---|
| 20 | * Variables to control global behavior |
---|
| 21 | */ |
---|
| 22 | int verbose = 1; |
---|
| 23 | const char *progname; |
---|
| 24 | const char *scriptname; |
---|
| 25 | |
---|
| 26 | FILE *Script; |
---|
| 27 | int ScriptFileLine = 0; |
---|
| 28 | |
---|
| 29 | /* |
---|
| 30 | * Print program usage message |
---|
| 31 | */ |
---|
| 32 | void usage() |
---|
| 33 | { |
---|
| 34 | fprintf( |
---|
| 35 | stderr, |
---|
| 36 | "Usage: %s [-v] script\n" |
---|
| 37 | "\n" |
---|
| 38 | " -v - enable verbose output\n", |
---|
| 39 | progname |
---|
| 40 | ); |
---|
| 41 | exit( -1 ); |
---|
| 42 | } |
---|
| 43 | |
---|
| 44 | #define RTEMS_SHELL_MAXIMUM_ARGUMENTS (128) |
---|
| 45 | |
---|
| 46 | void ProcessScript( |
---|
| 47 | FILE *script |
---|
| 48 | ) |
---|
| 49 | { |
---|
| 50 | char buffer[512]; |
---|
| 51 | char *cStatus; |
---|
| 52 | char *c; |
---|
| 53 | size_t length; |
---|
| 54 | int argc; |
---|
| 55 | char *argv[RTEMS_SHELL_MAXIMUM_ARGUMENTS]; |
---|
| 56 | rtems_shell_cmd_t *shell_cmd; |
---|
| 57 | |
---|
| 58 | |
---|
| 59 | while ( 1 ) { |
---|
| 60 | cStatus = fgets( buffer, sizeof(buffer), script ); |
---|
| 61 | if ( cStatus == NULL ) |
---|
| 62 | break; |
---|
| 63 | // If the last line does not have a CR, then we don't want to |
---|
| 64 | // arbitrarily clobber an = instead of a \n. |
---|
| 65 | length = strlen(buffer); |
---|
| 66 | if ( buffer[ length - 1] == '\n' ) |
---|
| 67 | buffer[ length - 1] = '\0'; |
---|
| 68 | |
---|
| 69 | if ( verbose ) |
---|
| 70 | printf( "==> %d: %s\n", ++ScriptFileLine, buffer ); |
---|
| 71 | |
---|
| 72 | if ( buffer[0] == '#' ) |
---|
| 73 | continue; |
---|
| 74 | |
---|
| 75 | for ( c = buffer ; *c ; c++ ) { |
---|
| 76 | if (!isblank((int)*c)) |
---|
| 77 | break; |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | |
---|
| 81 | if (!strcmp(c,"bye") || !strcmp(c,"exit")) { |
---|
| 82 | return; |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | if (rtems_shell_make_args(c, &argc, argv, RTEMS_SHELL_MAXIMUM_ARGUMENTS)) { |
---|
| 86 | fprintf(stderr, "Error parsing arguments\n" ); |
---|
| 87 | continue; |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | if ( argc == 0 ) |
---|
| 91 | continue; |
---|
| 92 | |
---|
| 93 | shell_cmd = rtems_shell_lookup_cmd(argv[0]); |
---|
| 94 | if ( !shell_cmd ) { |
---|
| 95 | fprintf(stderr, "%s is unknown command\n", c ); |
---|
| 96 | continue; |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | shell_cmd->command(argc, argv); |
---|
| 100 | } |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | extern "C" { |
---|
| 104 | void add_commands(void); |
---|
| 105 | }; |
---|
| 106 | |
---|
| 107 | int main( |
---|
| 108 | int argc, |
---|
| 109 | char **argv |
---|
| 110 | ) |
---|
| 111 | { |
---|
| 112 | int opt; |
---|
| 113 | progname = argv[0]; |
---|
| 114 | |
---|
| 115 | while ((opt = getopt(argc, argv, "v")) != -1) { |
---|
| 116 | switch (opt) { |
---|
| 117 | case 'v': verbose = 0; break; |
---|
| 118 | default: /* '?' */ |
---|
| 119 | usage(); |
---|
| 120 | } |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | if ( optind >= argc ) { |
---|
| 124 | scriptname = "-"; |
---|
| 125 | } else { |
---|
| 126 | scriptname = argv[ optind ]; |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | if ( !strcmp( scriptname, "-" ) ) { |
---|
| 130 | scriptname = "/dev/stdin"; |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | if ( verbose ) { |
---|
| 134 | printf( |
---|
| 135 | "Script File : %s\n" |
---|
| 136 | "verbose : %d\n", |
---|
| 137 | scriptname, |
---|
| 138 | verbose |
---|
| 139 | ); |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | // |
---|
| 143 | // Initialize the command interpreter |
---|
| 144 | // |
---|
| 145 | rtems_shell_initialize_command_set(); |
---|
| 146 | add_commands(); |
---|
| 147 | |
---|
| 148 | // |
---|
| 149 | // Open the script file |
---|
| 150 | // |
---|
| 151 | Script = fopen( scriptname, "r" ); |
---|
| 152 | if ( !Script ) { |
---|
| 153 | fprintf( stderr, "Unable to open script file (%s)\n", scriptname ); |
---|
| 154 | exit( -1 ); |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | // |
---|
| 158 | // Process the Script |
---|
| 159 | // |
---|
| 160 | ProcessScript( Script ); |
---|
| 161 | |
---|
| 162 | // |
---|
| 163 | // Open the script file |
---|
| 164 | // |
---|
| 165 | (void) fclose( Script ); |
---|
| 166 | |
---|
| 167 | // |
---|
| 168 | // Just in case something throws |
---|
| 169 | // |
---|
| 170 | try { |
---|
| 171 | } catch (...) { |
---|
| 172 | exit(-1); |
---|
| 173 | } |
---|
| 174 | |
---|
| 175 | return 0; |
---|
| 176 | } |
---|