xref: /openbmc/u-boot/common/command.c (revision 5a75e121)
1 /*
2  * (C) Copyright 2000-2009
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>
30 
31 /*
32  * Use puts() instead of printf() to avoid printf buffer overflow
33  * for long help messages
34  */
35 
36 int _do_help (cmd_tbl_t *cmd_start, int cmd_items, cmd_tbl_t * cmdtp, int
37 	      flag, int argc, char * const argv[])
38 {
39 	int i;
40 	int rcode = 0;
41 
42 	if (argc == 1) {	/*show list of commands */
43 		cmd_tbl_t *cmd_array[cmd_items];
44 		int i, j, swaps;
45 
46 		/* Make array of commands from .uboot_cmd section */
47 		cmdtp = cmd_start;
48 		for (i = 0; i < cmd_items; i++) {
49 			cmd_array[i] = cmdtp++;
50 		}
51 
52 		/* Sort command list (trivial bubble sort) */
53 		for (i = cmd_items - 1; i > 0; --i) {
54 			swaps = 0;
55 			for (j = 0; j < i; ++j) {
56 				if (strcmp (cmd_array[j]->name,
57 					    cmd_array[j + 1]->name) > 0) {
58 					cmd_tbl_t *tmp;
59 					tmp = cmd_array[j];
60 					cmd_array[j] = cmd_array[j + 1];
61 					cmd_array[j + 1] = tmp;
62 					++swaps;
63 				}
64 			}
65 			if (!swaps)
66 				break;
67 		}
68 
69 		/* print short help (usage) */
70 		for (i = 0; i < cmd_items; i++) {
71 			const char *usage = cmd_array[i]->usage;
72 
73 			/* allow user abort */
74 			if (ctrlc ())
75 				return 1;
76 			if (usage == NULL)
77 				continue;
78 			printf("%-*s- %s\n", CONFIG_SYS_HELP_CMD_WIDTH,
79 			       cmd_array[i]->name, usage);
80 		}
81 		return 0;
82 	}
83 	/*
84 	 * command help (long version)
85 	 */
86 	for (i = 1; i < argc; ++i) {
87 		if ((cmdtp = find_cmd_tbl (argv[i], cmd_start, cmd_items )) != NULL) {
88 			rcode |= cmd_usage(cmdtp);
89 		} else {
90 			printf ("Unknown command '%s' - try 'help'"
91 				" without arguments for list of all"
92 				" known commands\n\n", argv[i]
93 					);
94 			rcode = 1;
95 		}
96 	}
97 	return rcode;
98 }
99 
100 /***************************************************************************
101  * find command table entry for a command
102  */
103 cmd_tbl_t *find_cmd_tbl (const char *cmd, cmd_tbl_t *table, int table_len)
104 {
105 	cmd_tbl_t *cmdtp;
106 	cmd_tbl_t *cmdtp_temp = table;	/*Init value */
107 	const char *p;
108 	int len;
109 	int n_found = 0;
110 
111 	/*
112 	 * Some commands allow length modifiers (like "cp.b");
113 	 * compare command name only until first dot.
114 	 */
115 	len = ((p = strchr(cmd, '.')) == NULL) ? strlen (cmd) : (p - cmd);
116 
117 	for (cmdtp = table;
118 	     cmdtp != table + table_len;
119 	     cmdtp++) {
120 		if (strncmp (cmd, cmdtp->name, len) == 0) {
121 			if (len == strlen (cmdtp->name))
122 				return cmdtp;	/* full match */
123 
124 			cmdtp_temp = cmdtp;	/* abbreviated command ? */
125 			n_found++;
126 		}
127 	}
128 	if (n_found == 1) {			/* exactly one match */
129 		return cmdtp_temp;
130 	}
131 
132 	return NULL;	/* not found or ambiguous command */
133 }
134 
135 cmd_tbl_t *find_cmd (const char *cmd)
136 {
137 	int len = &__u_boot_cmd_end - &__u_boot_cmd_start;
138 	return find_cmd_tbl(cmd, &__u_boot_cmd_start, len);
139 }
140 
141 int cmd_usage(cmd_tbl_t *cmdtp)
142 {
143 	printf("%s - %s\n\n", cmdtp->name, cmdtp->usage);
144 
145 #ifdef	CONFIG_SYS_LONGHELP
146 	printf("Usage:\n%s ", cmdtp->name);
147 
148 	if (!cmdtp->help) {
149 		puts ("- No additional help available.\n");
150 		return 1;
151 	}
152 
153 	puts (cmdtp->help);
154 	putc ('\n');
155 #endif	/* CONFIG_SYS_LONGHELP */
156 	return 1;
157 }
158 
159 #ifdef CONFIG_AUTO_COMPLETE
160 
161 int var_complete(int argc, char * const argv[], char last_char, int maxv, char *cmdv[])
162 {
163 #if 0 /* need to reimplement */
164 	static char tmp_buf[512];
165 	int space;
166 
167 	space = last_char == '\0' || last_char == ' ' || last_char == '\t';
168 
169 	if (space && argc == 1)
170 		return env_complete("", maxv, cmdv, sizeof(tmp_buf), tmp_buf);
171 
172 	if (!space && argc == 2)
173 		return env_complete(argv[1], maxv, cmdv, sizeof(tmp_buf), tmp_buf);
174 #endif
175 	return 0;
176 }
177 
178 static void install_auto_complete_handler(const char *cmd,
179 		int (*complete)(int argc, char * const argv[], char last_char, int maxv, char *cmdv[]))
180 {
181 	cmd_tbl_t *cmdtp;
182 
183 	cmdtp = find_cmd(cmd);
184 	if (cmdtp == NULL)
185 		return;
186 
187 	cmdtp->complete = complete;
188 }
189 
190 void install_auto_complete(void)
191 {
192 #if defined(CONFIG_CMD_EDITENV)
193 	install_auto_complete_handler("editenv", var_complete);
194 #endif
195 	install_auto_complete_handler("printenv", var_complete);
196 	install_auto_complete_handler("setenv", var_complete);
197 #if defined(CONFIG_CMD_RUN)
198 	install_auto_complete_handler("run", var_complete);
199 #endif
200 }
201 
202 /*************************************************************************************/
203 
204 static int complete_cmdv(int argc, char * const argv[], char last_char, int maxv, char *cmdv[])
205 {
206 	cmd_tbl_t *cmdtp;
207 	const char *p;
208 	int len, clen;
209 	int n_found = 0;
210 	const char *cmd;
211 
212 	/* sanity? */
213 	if (maxv < 2)
214 		return -2;
215 
216 	cmdv[0] = NULL;
217 
218 	if (argc == 0) {
219 		/* output full list of commands */
220 		for (cmdtp = &__u_boot_cmd_start; cmdtp != &__u_boot_cmd_end; cmdtp++) {
221 			if (n_found >= maxv - 2) {
222 				cmdv[n_found++] = "...";
223 				break;
224 			}
225 			cmdv[n_found++] = cmdtp->name;
226 		}
227 		cmdv[n_found] = NULL;
228 		return n_found;
229 	}
230 
231 	/* more than one arg or one but the start of the next */
232 	if (argc > 1 || (last_char == '\0' || last_char == ' ' || last_char == '\t')) {
233 		cmdtp = find_cmd(argv[0]);
234 		if (cmdtp == NULL || cmdtp->complete == NULL) {
235 			cmdv[0] = NULL;
236 			return 0;
237 		}
238 		return (*cmdtp->complete)(argc, argv, last_char, maxv, cmdv);
239 	}
240 
241 	cmd = argv[0];
242 	/*
243 	 * Some commands allow length modifiers (like "cp.b");
244 	 * compare command name only until first dot.
245 	 */
246 	p = strchr(cmd, '.');
247 	if (p == NULL)
248 		len = strlen(cmd);
249 	else
250 		len = p - cmd;
251 
252 	/* return the partial matches */
253 	for (cmdtp = &__u_boot_cmd_start; cmdtp != &__u_boot_cmd_end; cmdtp++) {
254 
255 		clen = strlen(cmdtp->name);
256 		if (clen < len)
257 			continue;
258 
259 		if (memcmp(cmd, cmdtp->name, len) != 0)
260 			continue;
261 
262 		/* too many! */
263 		if (n_found >= maxv - 2) {
264 			cmdv[n_found++] = "...";
265 			break;
266 		}
267 
268 		cmdv[n_found++] = cmdtp->name;
269 	}
270 
271 	cmdv[n_found] = NULL;
272 	return n_found;
273 }
274 
275 static int make_argv(char *s, int argvsz, char *argv[])
276 {
277 	int argc = 0;
278 
279 	/* split into argv */
280 	while (argc < argvsz - 1) {
281 
282 		/* skip any white space */
283 		while ((*s == ' ') || (*s == '\t'))
284 			++s;
285 
286 		if (*s == '\0')	/* end of s, no more args	*/
287 			break;
288 
289 		argv[argc++] = s;	/* begin of argument string	*/
290 
291 		/* find end of string */
292 		while (*s && (*s != ' ') && (*s != '\t'))
293 			++s;
294 
295 		if (*s == '\0')		/* end of s, no more args	*/
296 			break;
297 
298 		*s++ = '\0';		/* terminate current arg	 */
299 	}
300 	argv[argc] = NULL;
301 
302 	return argc;
303 }
304 
305 static void print_argv(const char *banner, const char *leader, const char *sep, int linemax, char * const argv[])
306 {
307 	int ll = leader != NULL ? strlen(leader) : 0;
308 	int sl = sep != NULL ? strlen(sep) : 0;
309 	int len, i;
310 
311 	if (banner) {
312 		puts("\n");
313 		puts(banner);
314 	}
315 
316 	i = linemax;	/* force leader and newline */
317 	while (*argv != NULL) {
318 		len = strlen(*argv) + sl;
319 		if (i + len >= linemax) {
320 			puts("\n");
321 			if (leader)
322 				puts(leader);
323 			i = ll - sl;
324 		} else if (sep)
325 			puts(sep);
326 		puts(*argv++);
327 		i += len;
328 	}
329 	printf("\n");
330 }
331 
332 static int find_common_prefix(char * const argv[])
333 {
334 	int i, len;
335 	char *anchor, *s, *t;
336 
337 	if (*argv == NULL)
338 		return 0;
339 
340 	/* begin with max */
341 	anchor = *argv++;
342 	len = strlen(anchor);
343 	while ((t = *argv++) != NULL) {
344 		s = anchor;
345 		for (i = 0; i < len; i++, t++, s++) {
346 			if (*t != *s)
347 				break;
348 		}
349 		len = s - anchor;
350 	}
351 	return len;
352 }
353 
354 static char tmp_buf[CONFIG_SYS_CBSIZE];	/* copy of console I/O buffer	*/
355 
356 int cmd_auto_complete(const char *const prompt, char *buf, int *np, int *colp)
357 {
358 	int n = *np, col = *colp;
359 	char *argv[CONFIG_SYS_MAXARGS + 1];		/* NULL terminated	*/
360 	char *cmdv[20];
361 	char *s, *t;
362 	const char *sep;
363 	int i, j, k, len, seplen, argc;
364 	int cnt;
365 	char last_char;
366 
367 	if (strcmp(prompt, CONFIG_SYS_PROMPT) != 0)
368 		return 0;	/* not in normal console */
369 
370 	cnt = strlen(buf);
371 	if (cnt >= 1)
372 		last_char = buf[cnt - 1];
373 	else
374 		last_char = '\0';
375 
376 	/* copy to secondary buffer which will be affected */
377 	strcpy(tmp_buf, buf);
378 
379 	/* separate into argv */
380 	argc = make_argv(tmp_buf, sizeof(argv)/sizeof(argv[0]), argv);
381 
382 	/* do the completion and return the possible completions */
383 	i = complete_cmdv(argc, argv, last_char, sizeof(cmdv)/sizeof(cmdv[0]), cmdv);
384 
385 	/* no match; bell and out */
386 	if (i == 0) {
387 		if (argc > 1)	/* allow tab for non command */
388 			return 0;
389 		putc('\a');
390 		return 1;
391 	}
392 
393 	s = NULL;
394 	len = 0;
395 	sep = NULL;
396 	seplen = 0;
397 	if (i == 1) { /* one match; perfect */
398 		k = strlen(argv[argc - 1]);
399 		s = cmdv[0] + k;
400 		len = strlen(s);
401 		sep = " ";
402 		seplen = 1;
403 	} else if (i > 1 && (j = find_common_prefix(cmdv)) != 0) {	/* more */
404 		k = strlen(argv[argc - 1]);
405 		j -= k;
406 		if (j > 0) {
407 			s = cmdv[0] + k;
408 			len = j;
409 		}
410 	}
411 
412 	if (s != NULL) {
413 		k = len + seplen;
414 		/* make sure it fits */
415 		if (n + k >= CONFIG_SYS_CBSIZE - 2) {
416 			putc('\a');
417 			return 1;
418 		}
419 
420 		t = buf + cnt;
421 		for (i = 0; i < len; i++)
422 			*t++ = *s++;
423 		if (sep != NULL)
424 			for (i = 0; i < seplen; i++)
425 				*t++ = sep[i];
426 		*t = '\0';
427 		n += k;
428 		col += k;
429 		puts(t - k);
430 		if (sep == NULL)
431 			putc('\a');
432 		*np = n;
433 		*colp = col;
434 	} else {
435 		print_argv(NULL, "  ", " ", 78, cmdv);
436 
437 		puts(prompt);
438 		puts(buf);
439 	}
440 	return 1;
441 }
442 
443 #endif
444 
445 #ifdef CMD_DATA_SIZE
446 int cmd_get_data_size(char* arg, int default_size)
447 {
448 	/* Check for a size specification .b, .w or .l.
449 	 */
450 	int len = strlen(arg);
451 	if (len > 2 && arg[len-2] == '.') {
452 		switch(arg[len-1]) {
453 		case 'b':
454 			return 1;
455 		case 'w':
456 			return 2;
457 		case 'l':
458 			return 4;
459 		case 's':
460 			return -2;
461 		default:
462 			return -1;
463 		}
464 	}
465 	return default_size;
466 }
467 #endif
468 
469 #if !defined(CONFIG_RELOC_FIXUP_WORKS)
470 DECLARE_GLOBAL_DATA_PTR;
471 
472 void fixup_cmdtable(cmd_tbl_t *cmdtp, int size)
473 {
474 	int	i;
475 
476 	if (gd->reloc_off == 0)
477 		return;
478 
479 	for (i = 0; i < size; i++) {
480 		ulong addr;
481 
482 		addr = (ulong) (cmdtp->cmd) + gd->reloc_off;
483 #if DEBUG_COMMANDS
484 		printf("Command \"%s\": 0x%08lx => 0x%08lx\n",
485 		       cmdtp->name, (ulong) (cmdtp->cmd), addr);
486 #endif
487 		cmdtp->cmd =
488 			(int (*)(struct cmd_tbl_s *, int, int, char * const []))addr;
489 		addr = (ulong)(cmdtp->name) + gd->reloc_off;
490 		cmdtp->name = (char *)addr;
491 		if (cmdtp->usage) {
492 			addr = (ulong)(cmdtp->usage) + gd->reloc_off;
493 			cmdtp->usage = (char *)addr;
494 		}
495 #ifdef	CONFIG_SYS_LONGHELP
496 		if (cmdtp->help) {
497 			addr = (ulong)(cmdtp->help) + gd->reloc_off;
498 			cmdtp->help = (char *)addr;
499 		}
500 #endif
501 		cmdtp++;
502 	}
503 }
504 #endif
505