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