1 | /* |
---|
2 | * COPYRIGHT (c) 1989-2013. |
---|
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 | int sc; |
---|
52 | char *cStatus; |
---|
53 | char *c; |
---|
54 | size_t length; |
---|
55 | int argc; |
---|
56 | char *argv[RTEMS_SHELL_MAXIMUM_ARGUMENTS]; |
---|
57 | rtems_shell_cmd_t *shell_cmd; |
---|
58 | |
---|
59 | |
---|
60 | while ( 1 ) { |
---|
61 | cStatus = fgets( buffer, sizeof(buffer), script ); |
---|
62 | if ( cStatus == NULL ) |
---|
63 | break; |
---|
64 | // If the last line does not have a CR, then we don't want to |
---|
65 | // arbitrarily clobber an = instead of a \n. |
---|
66 | length = strlen(buffer); |
---|
67 | if ( buffer[ length - 1] == '\n' ) |
---|
68 | buffer[ length - 1] = '\0'; |
---|
69 | |
---|
70 | if ( verbose ) |
---|
71 | printf( "==> %d: %s\n", ++ScriptFileLine, buffer ); |
---|
72 | |
---|
73 | if ( buffer[0] == '#' ) |
---|
74 | continue; |
---|
75 | |
---|
76 | for ( c = buffer ; *c ; c++ ) { |
---|
77 | if (!isblank((int)*c)) |
---|
78 | break; |
---|
79 | } |
---|
80 | |
---|
81 | |
---|
82 | if (!strcmp(c,"bye") || !strcmp(c,"exit")) { |
---|
83 | exit( 0 ); |
---|
84 | } |
---|
85 | |
---|
86 | if (rtems_shell_make_args(c, &argc, argv, RTEMS_SHELL_MAXIMUM_ARGUMENTS)) { |
---|
87 | fprintf(stderr, "Error parsing arguments\n" ); |
---|
88 | continue; |
---|
89 | } |
---|
90 | |
---|
91 | if ( argc == 0 ) |
---|
92 | continue; |
---|
93 | |
---|
94 | shell_cmd = rtems_shell_lookup_cmd(argv[0]); |
---|
95 | if ( !shell_cmd ) { |
---|
96 | fprintf(stderr, "%s is unknown command\n", c ); |
---|
97 | exit( 1 ); |
---|
98 | } |
---|
99 | |
---|
100 | sc = shell_cmd->command(argc, argv); |
---|
101 | if ( sc != 0 ) { |
---|
102 | fprintf( stderr, "ERROR: Command %s returned %d\n", argv[0], sc ); |
---|
103 | exit( sc ); |
---|
104 | } |
---|
105 | } |
---|
106 | } |
---|
107 | |
---|
108 | extern "C" { |
---|
109 | void add_commands(void); |
---|
110 | }; |
---|
111 | |
---|
112 | int main( |
---|
113 | int argc, |
---|
114 | char **argv |
---|
115 | ) |
---|
116 | { |
---|
117 | int opt; |
---|
118 | progname = argv[0]; |
---|
119 | |
---|
120 | while ((opt = getopt(argc, argv, "v")) != -1) { |
---|
121 | switch (opt) { |
---|
122 | case 'v': verbose = 0; break; |
---|
123 | default: /* '?' */ |
---|
124 | usage(); |
---|
125 | } |
---|
126 | } |
---|
127 | |
---|
128 | if ( optind >= argc ) { |
---|
129 | scriptname = "-"; |
---|
130 | } else { |
---|
131 | scriptname = argv[ optind ]; |
---|
132 | } |
---|
133 | |
---|
134 | if ( !strcmp( scriptname, "-" ) ) { |
---|
135 | scriptname = "/dev/stdin"; |
---|
136 | } |
---|
137 | |
---|
138 | if ( verbose ) { |
---|
139 | printf( |
---|
140 | "Script File : %s\n" |
---|
141 | "verbose : %d\n", |
---|
142 | scriptname, |
---|
143 | verbose |
---|
144 | ); |
---|
145 | } |
---|
146 | |
---|
147 | // |
---|
148 | // Initialize the command interpreter |
---|
149 | // |
---|
150 | rtems_shell_initialize_command_set(); |
---|
151 | add_commands(); |
---|
152 | |
---|
153 | // |
---|
154 | // Open the script file |
---|
155 | // |
---|
156 | Script = fopen( scriptname, "r" ); |
---|
157 | if ( !Script ) { |
---|
158 | fprintf( stderr, "Unable to open script file (%s)\n", scriptname ); |
---|
159 | exit( -1 ); |
---|
160 | } |
---|
161 | |
---|
162 | // |
---|
163 | // Process the Script |
---|
164 | // |
---|
165 | ProcessScript( Script ); |
---|
166 | |
---|
167 | // |
---|
168 | // Open the script file |
---|
169 | // |
---|
170 | (void) fclose( Script ); |
---|
171 | |
---|
172 | // |
---|
173 | // Just in case something throws |
---|
174 | // |
---|
175 | try { |
---|
176 | } catch (...) { |
---|
177 | exit(-1); |
---|
178 | } |
---|
179 | |
---|
180 | return 0; |
---|
181 | } |
---|