wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 1 | /* |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 2 | * (C) Copyright 2000-2003 |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 3 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 4 | * |
| 5 | * See file CREDITS for list of people who contributed to this |
| 6 | * project. |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License as |
| 10 | * published by the Free Software Foundation; either version 2 of |
| 11 | * the License, or (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 21 | * MA 02111-1307 USA |
| 22 | */ |
| 23 | |
| 24 | /* |
| 25 | * Command Processor Table |
| 26 | */ |
| 27 | |
| 28 | #include <common.h> |
| 29 | #include <command.h> |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 30 | |
| 31 | int |
| 32 | do_version (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) |
| 33 | { |
| 34 | extern char version_string[]; |
| 35 | printf ("\n%s\n", version_string); |
| 36 | return 0; |
| 37 | } |
| 38 | |
wdenk | 0d49839 | 2003-07-01 21:06:45 +0000 | [diff] [blame^] | 39 | U_BOOT_CMD( |
| 40 | version, 1, 1, do_version, |
wdenk | b0fce99 | 2003-06-29 21:03:46 +0000 | [diff] [blame] | 41 | "version - print monitor version\n", |
| 42 | NULL |
| 43 | ); |
| 44 | |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 45 | int |
| 46 | do_echo (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) |
| 47 | { |
| 48 | int i, putnl = 1; |
| 49 | |
| 50 | for (i = 1; i < argc; i++) { |
| 51 | char *p = argv[i], c; |
| 52 | |
| 53 | if (i > 1) |
| 54 | putc(' '); |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 55 | while ((c = *p++) != '\0') { |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 56 | if (c == '\\' && *p == 'c') { |
| 57 | putnl = 0; |
| 58 | p++; |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 59 | } else { |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 60 | putc(c); |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 61 | } |
| 62 | } |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | if (putnl) |
| 66 | putc('\n'); |
| 67 | return 0; |
| 68 | } |
| 69 | |
wdenk | 0d49839 | 2003-07-01 21:06:45 +0000 | [diff] [blame^] | 70 | U_BOOT_CMD( |
| 71 | echo, CFG_MAXARGS, 1, do_echo, |
wdenk | b0fce99 | 2003-06-29 21:03:46 +0000 | [diff] [blame] | 72 | "echo - echo args to console\n", |
| 73 | "[args..]\n" |
| 74 | " - echo args to console; \\c suppresses newline\n" |
| 75 | ); |
| 76 | |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 77 | /* |
| 78 | * Use puts() instead of printf() to avoid printf buffer overflow |
| 79 | * for long help messages |
| 80 | */ |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 81 | int do_help (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[]) |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 82 | { |
| 83 | int i; |
| 84 | int rcode = 0; |
| 85 | |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 86 | if (argc == 1) { /*show list of commands */ |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 87 | |
wdenk | 9d2b18a | 2003-06-28 23:11:04 +0000 | [diff] [blame] | 88 | int cmd_items = &__u_boot_cmd_end - |
| 89 | &__u_boot_cmd_start; /* pointer arith! */ |
| 90 | cmd_tbl_t *cmd_array[cmd_items]; |
| 91 | int i, j, swaps; |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 92 | |
wdenk | 9d2b18a | 2003-06-28 23:11:04 +0000 | [diff] [blame] | 93 | /* Make array of commands from .uboot_cmd section */ |
| 94 | cmdtp = &__u_boot_cmd_start; |
| 95 | for (i = 0; i < cmd_items; i++) { |
| 96 | cmd_array[i] = cmdtp++; |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 97 | } |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 98 | |
wdenk | 9d2b18a | 2003-06-28 23:11:04 +0000 | [diff] [blame] | 99 | /* Sort command list (trivial bubble sort) */ |
| 100 | for (i = cmd_items - 1; i > 0; --i) { |
| 101 | swaps = 0; |
| 102 | for (j = 0; j < i; ++j) { |
| 103 | if (strcmp (cmd_array[j]->name, |
| 104 | cmd_array[j + 1]->name) > 0) { |
| 105 | cmd_tbl_t *tmp; |
| 106 | tmp = cmd_array[j]; |
| 107 | cmd_array[j] = cmd_array[j + 1]; |
| 108 | cmd_array[j + 1] = tmp; |
| 109 | ++swaps; |
| 110 | } |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 111 | } |
wdenk | 9d2b18a | 2003-06-28 23:11:04 +0000 | [diff] [blame] | 112 | if (!swaps) |
| 113 | break; |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | /* print short help (usage) */ |
wdenk | 9d2b18a | 2003-06-28 23:11:04 +0000 | [diff] [blame] | 117 | for (i = 0; i < cmd_items; i++) { |
| 118 | const char *usage = cmd_array[i]->usage; |
| 119 | |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 120 | /* allow user abort */ |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 121 | if (ctrlc ()) |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 122 | return 1; |
wdenk | 9d2b18a | 2003-06-28 23:11:04 +0000 | [diff] [blame] | 123 | if (usage == NULL) |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 124 | continue; |
wdenk | 9d2b18a | 2003-06-28 23:11:04 +0000 | [diff] [blame] | 125 | puts (usage); |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 126 | } |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 127 | return 0; |
| 128 | } |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 129 | /* |
| 130 | * command help (long version) |
| 131 | */ |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 132 | for (i = 1; i < argc; ++i) { |
| 133 | if ((cmdtp = find_cmd (argv[i])) != NULL) { |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 134 | #ifdef CFG_LONGHELP |
| 135 | /* found - print (long) help info */ |
| 136 | puts (cmdtp->name); |
| 137 | putc (' '); |
| 138 | if (cmdtp->help) { |
| 139 | puts (cmdtp->help); |
| 140 | } else { |
| 141 | puts ("- No help available.\n"); |
| 142 | rcode = 1; |
| 143 | } |
| 144 | putc ('\n'); |
| 145 | #else /* no long help available */ |
| 146 | if (cmdtp->usage) |
| 147 | puts (cmdtp->usage); |
| 148 | #endif /* CFG_LONGHELP */ |
wdenk | 71f9511 | 2003-06-15 22:40:42 +0000 | [diff] [blame] | 149 | } else { |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 150 | printf ("Unknown command '%s' - try 'help'" |
| 151 | " without arguments for list of all" |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 152 | " known commands\n\n", argv[i] |
| 153 | ); |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 154 | rcode = 1; |
| 155 | } |
| 156 | } |
| 157 | return rcode; |
| 158 | } |
| 159 | |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 160 | |
wdenk | 0d49839 | 2003-07-01 21:06:45 +0000 | [diff] [blame^] | 161 | U_BOOT_CMD( |
| 162 | help, CFG_MAXARGS, 1, do_help, |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 163 | "help - print online help\n", |
| 164 | "[command ...]\n" |
| 165 | " - show help information (for 'command')\n" |
| 166 | "'help' prints online help for the monitor commands.\n\n" |
| 167 | "Without arguments, it prints a short usage message for all commands.\n\n" |
| 168 | "To get detailed help information for specific commands you can type\n" |
| 169 | "'help' with one or more command names as arguments.\n" |
| 170 | ); |
| 171 | |
wdenk | 0d49839 | 2003-07-01 21:06:45 +0000 | [diff] [blame^] | 172 | /* This do not ust the U_BOOT_CMD macro as ? can't be used in symbol names */ |
| 173 | #ifdef CFG_LONGHELP |
| 174 | cmd_tbl_t __u_boot_cmd_question_mark Struct_Section = { |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 175 | "?", CFG_MAXARGS, 1, do_help, |
| 176 | "? - alias for 'help'\n", |
| 177 | NULL |
wdenk | 0d49839 | 2003-07-01 21:06:45 +0000 | [diff] [blame^] | 178 | }; |
| 179 | #else |
| 180 | cmd_tbl_t __u_boot_cmd_question_mark Struct_Section = { |
| 181 | "?", CFG_MAXARGS, 1, do_help, |
| 182 | "? - alias for 'help'\n" |
| 183 | }; |
| 184 | #endif /* CFG_LONGHELP */ |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 185 | |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 186 | /*************************************************************************** |
| 187 | * find command table entry for a command |
| 188 | */ |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 189 | cmd_tbl_t *find_cmd (const char *cmd) |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 190 | { |
| 191 | cmd_tbl_t *cmdtp; |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 192 | cmd_tbl_t *cmdtp_temp = &__u_boot_cmd_start; /*Init value */ |
wdenk | 9d2b18a | 2003-06-28 23:11:04 +0000 | [diff] [blame] | 193 | const char *p; |
| 194 | int len; |
| 195 | int n_found = 0; |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 196 | |
wdenk | 9d2b18a | 2003-06-28 23:11:04 +0000 | [diff] [blame] | 197 | /* |
| 198 | * Some commands allow length modifiers (like "cp.b"); |
| 199 | * compare command name only until first dot. |
| 200 | */ |
| 201 | len = ((p = strchr(cmd, '.')) == NULL) ? strlen (cmd) : (p - cmd); |
| 202 | |
| 203 | for (cmdtp = &__u_boot_cmd_start; |
| 204 | cmdtp != &__u_boot_cmd_end; |
| 205 | cmdtp++) { |
| 206 | if (strncmp (cmd, cmdtp->name, len) == 0) { |
| 207 | if (len == strlen (cmdtp->name)) |
| 208 | return cmdtp; /* full match */ |
| 209 | |
| 210 | cmdtp_temp = cmdtp; /* abbreviated command ? */ |
| 211 | n_found++; |
| 212 | } |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 213 | } |
wdenk | 9d2b18a | 2003-06-28 23:11:04 +0000 | [diff] [blame] | 214 | if (n_found == 1) { /* exactly one match */ |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 215 | return cmdtp_temp; |
wdenk | 9d2b18a | 2003-06-28 23:11:04 +0000 | [diff] [blame] | 216 | } |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 217 | |
wdenk | 9d2b18a | 2003-06-28 23:11:04 +0000 | [diff] [blame] | 218 | return NULL; /* not found or ambiguous command */ |
wdenk | 5dfa25f | 2002-10-19 19:42:10 +0000 | [diff] [blame] | 219 | } |