Root/
Source at commit 6a1f486 created 13 years 3 months ago. By Peter Zotov, Reworked directory structure. | |
---|---|
1 | /* |
2 | * JzBoot: an USB bootloader for JZ series of Ingenic(R) microprocessors. |
3 | * Copyright (C) 2010 Sergey Gridassov <grindars@gmail.com>, |
4 | * Peter Zotov <whitequark@whitequark.org> |
5 | * |
6 | * This program is free software: you can redistribute it and/or modify |
7 | * it under the terms of the GNU General Public License as published by |
8 | * the Free Software Foundation, either version 3 of the License, or |
9 | * (at your option) any later version. |
10 | * |
11 | * This program is distributed in the hope that it will be useful, |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | * GNU General Public License for more details. |
15 | * |
16 | * You should have received a copy of the GNU General Public License |
17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
18 | */ |
19 | |
20 | #include <stdio.h> |
21 | #include <string.h> |
22 | #include <stdlib.h> |
23 | #include <unistd.h> |
24 | #include <errno.h> |
25 | |
26 | #include "shell.h" |
27 | #include "config.h" |
28 | #include "ingenic.h" |
29 | |
30 | static int builtin_help(shell_context_t *ctx, int argc, char *argv[]); |
31 | static int builtin_exit(shell_context_t *ctx, int argc, char *argv[]); |
32 | static int builtin_source(shell_context_t *ctx, int argc, char *argv[]); |
33 | static int builtin_echo(shell_context_t *ctx, int argc, char *argv[]); |
34 | static int builtin_sleep(shell_context_t *ctx, int argc, char *argv[]); |
35 | static int builtin_redetect(shell_context_t *ctx, int argc, char *argv[]); |
36 | static int builtin_rebuildcfg(shell_context_t *ctx, int argc, char *argv[]); |
37 | static int builtin_set(shell_context_t *ctx, int argc, char *argv[]); |
38 | static int builtin_safe(shell_context_t *ctx, int argc, char *argv[]); |
39 | |
40 | const shell_command_t builtin_cmdset[] = { |
41 | { "help", "Display this message", builtin_help, NULL }, |
42 | { "exit", "Batch: stop current script, interactive: end session", builtin_exit, NULL }, |
43 | { "source", "Run specified script", builtin_source, "<FILENAME>" }, |
44 | { "echo", "Output specified string", builtin_echo, "<STRING> ..." }, |
45 | { "sleep", "Sleep a specified amount of time", builtin_sleep, "<MILLISECONDS>" }, |
46 | { "set", "Print or set configuraton variables", builtin_set, "[VARIABLE] [VALUE]" }, |
47 | { "safe", "Run command ignoring errors", builtin_safe, "<COMMAND> [ARG] ..." }, |
48 | |
49 | { "redetect", "Redetect CPU", builtin_redetect, NULL }, |
50 | { "rebuildcfg", "Rebuild firmware configuration data", builtin_rebuildcfg, NULL }, |
51 | |
52 | { NULL, NULL, NULL } |
53 | }; |
54 | |
55 | static int help_maxwidth_function(shell_context_t *ctx, const shell_command_t *cmd, void *arg) { |
56 | int len = strlen(cmd->cmd), *maxlen = arg; |
57 | |
58 | if(cmd->args) |
59 | len += strlen(cmd->args) + 1; |
60 | |
61 | if(len > *maxlen) |
62 | *maxlen = len; |
63 | |
64 | return 0; |
65 | } |
66 | |
67 | static int help_print_function(shell_context_t *ctx, const shell_command_t *cmd, void *arg) { |
68 | int len = strlen(cmd->cmd), *maxlen = arg; |
69 | |
70 | fputs(cmd->cmd, stdout); |
71 | |
72 | if(cmd->args) { |
73 | len += strlen(cmd->args) + 1; |
74 | |
75 | putchar(' '); |
76 | fputs(cmd->args, stdout); |
77 | } |
78 | |
79 | for(int i = 0; i < *maxlen - len; i++) |
80 | putchar(' '); |
81 | |
82 | puts(cmd->description); |
83 | |
84 | return 0; |
85 | } |
86 | |
87 | static int builtin_help(shell_context_t *ctx, int argc, char *argv[]) { |
88 | int maxwidth = 0; |
89 | |
90 | shell_enumerate_commands(ctx, help_maxwidth_function, &maxwidth); |
91 | |
92 | maxwidth += 2; |
93 | |
94 | return shell_enumerate_commands(ctx, help_print_function, &maxwidth); |
95 | } |
96 | |
97 | static int builtin_exit(shell_context_t *ctx, int argc, char *argv[]) { |
98 | shell_exit(ctx, 1); |
99 | |
100 | return 0; |
101 | } |
102 | |
103 | static int builtin_source(shell_context_t *ctx, int argc, char *argv[]) { |
104 | int ret = shell_source(ctx, argv[1]); |
105 | |
106 | if(ret == -1) { |
107 | fprintf(stderr, "Error while sourcing file %s: %s\n", argv[1], strerror(errno)); |
108 | } |
109 | |
110 | shell_exit(ctx, 0); |
111 | |
112 | return ret; |
113 | } |
114 | |
115 | static int builtin_echo(shell_context_t *ctx, int argc, char *argv[]) { |
116 | for(int i = 1; i < argc; i++) { |
117 | fputs(argv[i], stdout); |
118 | |
119 | putchar((i < argc - 1) ? ' ' : '\n'); |
120 | } |
121 | |
122 | return 0; |
123 | } |
124 | |
125 | static int builtin_sleep(shell_context_t *ctx, int argc, char *argv[]) { |
126 | uint32_t ms = atoi(argv[1]); |
127 | |
128 | usleep(ms * 1000); |
129 | |
130 | return 0; |
131 | } |
132 | |
133 | static int builtin_redetect(shell_context_t *ctx, int argc, char *argv[]) { |
134 | if(ingenic_redetect(shell_device(ctx)) == -1) { |
135 | perror("ingenic_redetect"); |
136 | |
137 | return -1; |
138 | } else |
139 | return 0; |
140 | } |
141 | |
142 | |
143 | static int builtin_set(shell_context_t *ctx, int argc, char *argv[]) { |
144 | if(argc == 1) { |
145 | if(cfg_environ) { |
146 | for(int i = 0; cfg_environ[i] != NULL; i++) |
147 | printf("%s\n", cfg_environ[i]); |
148 | } |
149 | |
150 | } else if(argc == 2) { |
151 | cfg_unsetenv(argv[1]); |
152 | |
153 | } else if(argc == 3) { |
154 | cfg_setenv(argv[1], argv[2]); |
155 | } |
156 | |
157 | return 0; |
158 | } |
159 | |
160 | |
161 | static int builtin_rebuildcfg(shell_context_t *ctx, int argc, char *argv[]) { |
162 | return ingenic_rebuild(shell_device(ctx)); |
163 | } |
164 | |
165 | static int builtin_safe(shell_context_t *ctx, int argc, char *argv[]) { |
166 | if(shell_run(ctx, argc - 1, argv + 1) == -1) |
167 | perror("shell_run"); |
168 | |
169 | return 0; |
170 | } |
171 | |
172 |