Rev 3809 | Rev 3813 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3809 | Rev 3812 | ||
---|---|---|---|
1 | /* Copyright (c) 2008, Tim Post <tinkertim@gmail.com> |
1 | /* Copyright (c) 2008, Tim Post <tinkertim@gmail.com> |
2 | * All rights reserved. |
2 | * All rights reserved. |
3 | * Copyright (c) 2008, Jiri Svoboda - All Rights Reserved |
3 | * Copyright (c) 2008, Jiri Svoboda - All Rights Reserved |
4 | * |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions are met: |
6 | * modification, are permitted provided that the following conditions are met: |
7 | * |
7 | * |
8 | * Redistributions of source code must retain the above copyright notice, this |
8 | * Redistributions of source code must retain the above copyright notice, this |
9 | * list of conditions and the following disclaimer. |
9 | * list of conditions and the following disclaimer. |
10 | * |
10 | * |
11 | * Redistributions in binary form must reproduce the above copyright notice, |
11 | * Redistributions in binary form must reproduce the above copyright notice, |
12 | * this list of conditions and the following disclaimer in the documentation |
12 | * this list of conditions and the following disclaimer in the documentation |
13 | * and/or other materials provided with the distribution. |
13 | * and/or other materials provided with the distribution. |
14 | * |
14 | * |
15 | * Neither the name of the original program's authors nor the names of its |
15 | * Neither the name of the original program's authors nor the names of its |
16 | * contributors may be used to endorse or promote products derived from this |
16 | * contributors may be used to endorse or promote products derived from this |
17 | * software without specific prior written permission. |
17 | * software without specific prior written permission. |
18 | * |
18 | * |
19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
29 | * POSSIBILITY OF SUCH DAMAGE. |
29 | * POSSIBILITY OF SUCH DAMAGE. |
30 | */ |
30 | */ |
31 | 31 | ||
32 | #include <stdio.h> |
32 | #include <stdio.h> |
33 | #include <stdlib.h> |
33 | #include <stdlib.h> |
34 | #include <string.h> |
34 | #include <string.h> |
35 | #include <io/stream.h> |
35 | #include <io/stream.h> |
36 | #include <console.h> |
36 | #include <console.h> |
37 | 37 | ||
38 | #include "config.h" |
38 | #include "config.h" |
39 | #include "util.h" |
39 | #include "util.h" |
40 | #include "scli.h" |
40 | #include "scli.h" |
41 | #include "input.h" |
41 | #include "input.h" |
42 | #include "errors.h" |
42 | #include "errors.h" |
43 | #include "exec.h" |
43 | #include "exec.h" |
44 | 44 | ||
45 | extern volatile unsigned int cli_interactive; |
- | |
46 | - | ||
47 | /* Not exposed in input.h */ |
- | |
48 | static void cli_restricted(char *); |
- | |
49 | static void read_line(char *, int); |
45 | static void read_line(char *, int); |
50 | 46 | ||
51 | /* More than a macro than anything */ |
- | |
52 | static void cli_restricted(char *cmd) |
- | |
53 | { |
- | |
54 | printf("%s is not available in %s mode\n", cmd, |
- | |
55 | cli_interactive ? "interactive" : "non-interactive"); |
- | |
56 | - | ||
57 | return; |
- | |
58 | } |
- | |
59 | - | ||
60 | /* Tokenizes input from console, sees if the first word is a built-in, if so |
47 | /* Tokenizes input from console, sees if the first word is a built-in, if so |
61 | * invokes the built-in entry point (a[0]) passing all arguments in a[] to |
48 | * invokes the built-in entry point (a[0]) passing all arguments in a[] to |
62 | * the handler */ |
49 | * the handler */ |
63 | int tok_input(cliuser_t *usr) |
50 | int tok_input(cliuser_t *usr) |
64 | { |
51 | { |
65 | char *cmd[WORD_MAX]; |
52 | char *cmd[WORD_MAX]; |
66 | int n = 0, i = 0; |
53 | int n = 0, i = 0; |
67 | int rc = 0; |
54 | int rc = 0; |
68 | char *tmp; |
55 | char *tmp; |
69 | 56 | ||
70 | if (NULL == usr->line) |
57 | if (NULL == usr->line) |
71 | return CL_EFAIL; |
58 | return CL_EFAIL; |
72 | 59 | ||
73 | tmp = cli_strdup(usr->line); |
60 | tmp = cli_strdup(usr->line); |
74 | 61 | ||
75 | cmd[n] = cli_strtok(tmp, " "); |
62 | cmd[n] = cli_strtok(tmp, " "); |
76 | while (cmd[n] && n < WORD_MAX) { |
63 | while (cmd[n] && n < WORD_MAX) { |
77 | cmd[++n] = cli_strtok(NULL, " "); |
64 | cmd[++n] = cli_strtok(NULL, " "); |
78 | } |
65 | } |
79 | 66 | ||
80 | /* We have rubbish */ |
67 | /* We have rubbish */ |
81 | if (NULL == cmd[0]) { |
68 | if (NULL == cmd[0]) { |
82 | rc = CL_ENOENT; |
69 | rc = CL_ENOENT; |
83 | goto finit; |
70 | goto finit; |
84 | } |
71 | } |
85 | 72 | ||
86 | /* Its a builtin command ? */ |
73 | /* Its a builtin command ? */ |
87 | if ((i = (is_builtin(cmd[0]))) > -1) { |
74 | if ((i = (is_builtin(cmd[0]))) > -1) { |
88 | rc = run_builtin(i, cmd, usr); |
75 | rc = run_builtin(i, cmd, usr); |
89 | goto finit; |
76 | goto finit; |
90 | /* Its a module ? */ |
77 | /* Its a module ? */ |
91 | } else if ((i = (is_module(cmd[0]))) > -1) { |
78 | } else if ((i = (is_module(cmd[0]))) > -1) { |
92 | rc = run_module(i, cmd); |
79 | rc = run_module(i, cmd); |
93 | goto finit; |
80 | goto finit; |
94 | } |
81 | } |
95 | 82 | ||
96 | /* See what try_exec thinks of it */ |
83 | /* See what try_exec thinks of it */ |
97 | rc = try_exec(cmd[0], cmd); |
84 | rc = try_exec(cmd[0], cmd); |
98 | 85 | ||
99 | finit: |
86 | finit: |
100 | if (NULL != usr->line) { |
87 | if (NULL != usr->line) { |
101 | free(usr->line); |
88 | free(usr->line); |
102 | usr->line = (char *) NULL; |
89 | usr->line = (char *) NULL; |
103 | } |
90 | } |
104 | if (NULL != tmp) |
91 | if (NULL != tmp) |
105 | free(tmp); |
92 | free(tmp); |
106 | 93 | ||
107 | return rc; |
94 | return rc; |
108 | } |
95 | } |
109 | 96 | ||
110 | /* Borrowed from Jiri Svoboda's 'cli' uspace app */ |
97 | /* Borrowed from Jiri Svoboda's 'cli' uspace app */ |
111 | static void read_line(char *buffer, int n) |
98 | static void read_line(char *buffer, int n) |
112 | { |
99 | { |
113 | char c; |
100 | char c; |
114 | int chars; |
101 | int chars; |
115 | 102 | ||
116 | chars = 0; |
103 | chars = 0; |
117 | while (chars < n - 1) { |
104 | while (chars < n - 1) { |
118 | c = getchar(); |
105 | c = getchar(); |
119 | if (c < 0) |
106 | if (c < 0) |
120 | return; |
107 | return; |
121 | if (c == '\n') |
108 | if (c == '\n') |
122 | break; |
109 | break; |
123 | if (c == '\b') { |
110 | if (c == '\b') { |
124 | if (chars > 0) { |
111 | if (chars > 0) { |
125 | putchar('\b'); |
112 | putchar('\b'); |
126 | --chars; |
113 | --chars; |
127 | } |
114 | } |
128 | continue; |
115 | continue; |
129 | } |
116 | } |
130 | putchar(c); |
117 | putchar(c); |
131 | buffer[chars++] = c; |
118 | buffer[chars++] = c; |
132 | } |
119 | } |
133 | putchar('\n'); |
120 | putchar('\n'); |
134 | buffer[chars] = '\0'; |
121 | buffer[chars] = '\0'; |
135 | } |
122 | } |
136 | 123 | ||
137 | /* TODO: |
124 | /* TODO: |
138 | * Implement something like editline() / readline(), if even |
125 | * Implement something like editline() / readline(), if even |
139 | * just for command history and making arrows work. */ |
126 | * just for command history and making arrows work. */ |
140 | void get_input(cliuser_t *usr) |
127 | void get_input(cliuser_t *usr) |
141 | { |
128 | { |
142 | char line[INPUT_MAX]; |
129 | char line[INPUT_MAX]; |
143 | size_t len = 0; |
130 | size_t len = 0; |
144 | 131 | ||
145 | console_set_style(STYLE_EMPHASIS); |
132 | console_set_style(STYLE_EMPHASIS); |
146 | printf("%s", usr->prompt); |
133 | printf("%s", usr->prompt); |
147 | console_set_style(STYLE_NORMAL); |
134 | console_set_style(STYLE_NORMAL); |
148 | 135 | ||
149 | read_line(line, INPUT_MAX); |
136 | read_line(line, INPUT_MAX); |
150 | len = strlen(line); |
137 | len = strlen(line); |
151 | /* Make sure we don't have rubbish or a C/R happy user */ |
138 | /* Make sure we don't have rubbish or a C/R happy user */ |
152 | if (len == 0 || line[0] == '\n') |
139 | if (len == 0 || line[0] == '\n') |
153 | return; |
140 | return; |
154 | usr->line = cli_strdup(line); |
141 | usr->line = cli_strdup(line); |
155 | 142 | ||
156 | return; |
143 | return; |
157 | } |
144 | } |
158 | 145 | ||
159 | 146 |