1/* SPDX-License-Identifier: GPL-2.0+ */
2/*
3 * The 'exception' command can be used for testing exception handling.
4 *
5 * Copyright (c) 2018, Heinrich Schuchardt <xypron.glpk@gmx.de>
6 */
7
8#include <command.h>
9
10static int do_exception(struct cmd_tbl *cmdtp, int flag, int argc,
11			char *const argv[])
12{
13	struct cmd_tbl *cp;
14
15	if (argc != 2)
16		return CMD_RET_USAGE;
17
18	/* drop sub-command parameter */
19	argc--;
20	argv++;
21
22	cp = find_cmd_tbl(argv[0], cmd_sub, ARRAY_SIZE(cmd_sub));
23
24	if (cp)
25		return cp->cmd(cmdtp, flag, argc, argv);
26
27	return CMD_RET_USAGE;
28}
29
30static int exception_complete(int argc, char *const argv[], char last_char,
31			      int maxv, char *cmdv[])
32{
33	int len = 0;
34	int i = 0;
35	struct cmd_tbl *cmdtp;
36
37	switch (argc) {
38	case 1:
39		break;
40	case 2:
41		len = strlen(argv[1]);
42		break;
43	default:
44		return 0;
45	}
46	for (cmdtp = cmd_sub; cmdtp != cmd_sub + ARRAY_SIZE(cmd_sub); cmdtp++) {
47		if (i >= maxv - 1)
48			return i;
49		if (!strncmp(argv[1], cmdtp->name, len))
50			cmdv[i++] = cmdtp->name;
51	}
52	cmdv[i] = NULL;
53	return i;
54}
55
56U_BOOT_CMD_COMPLETE(
57	exception, 2, 0, do_exception,
58	"Forces an exception to occur",
59	exception_help_text, exception_complete
60);
61