common: command: Expose a generic helper to auto-complete sub commands
[pandora-u-boot.git] / include / command.h
1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3  * (C) Copyright 2000-2009
4  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5  */
6
7 /*
8  *  Definitions for Command Processor
9  */
10 #ifndef __COMMAND_H
11 #define __COMMAND_H
12
13 #include <linker_lists.h>
14
15 #ifndef NULL
16 #define NULL    0
17 #endif
18
19 /* Default to a width of 8 characters for help message command width */
20 #ifndef CONFIG_SYS_HELP_CMD_WIDTH
21 #define CONFIG_SYS_HELP_CMD_WIDTH       8
22 #endif
23
24 #ifndef __ASSEMBLY__
25 /*
26  * Monitor Command Table
27  */
28
29 struct cmd_tbl_s {
30         char            *name;          /* Command Name                 */
31         int             maxargs;        /* maximum number of arguments  */
32         int             repeatable;     /* autorepeat allowed?          */
33                                         /* Implementation function      */
34         int             (*cmd)(struct cmd_tbl_s *, int, int, char * const []);
35         char            *usage;         /* Usage message        (short) */
36 #ifdef  CONFIG_SYS_LONGHELP
37         char            *help;          /* Help  message        (long)  */
38 #endif
39 #ifdef CONFIG_AUTO_COMPLETE
40         /* do auto completion on the arguments */
41         int             (*complete)(int argc, char * const argv[], char last_char, int maxv, char *cmdv[]);
42 #endif
43 };
44
45 typedef struct cmd_tbl_s        cmd_tbl_t;
46
47
48 #if defined(CONFIG_CMD_RUN)
49 extern int do_run(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
50 #endif
51
52 /* common/command.c */
53 int _do_help (cmd_tbl_t *cmd_start, int cmd_items, cmd_tbl_t * cmdtp, int
54               flag, int argc, char * const argv[]);
55 cmd_tbl_t *find_cmd(const char *cmd);
56 cmd_tbl_t *find_cmd_tbl (const char *cmd, cmd_tbl_t *table, int table_len);
57 int complete_subcmdv(cmd_tbl_t *cmdtp, int count, int argc,
58                      char * const argv[], char last_char, int maxv,
59                      char *cmdv[]);
60
61 extern int cmd_usage(const cmd_tbl_t *cmdtp);
62
63 #ifdef CONFIG_AUTO_COMPLETE
64 extern int var_complete(int argc, char * const argv[], char last_char, int maxv, char *cmdv[]);
65 extern int cmd_auto_complete(const char *const prompt, char *buf, int *np, int *colp);
66 #endif
67
68 /**
69  * cmd_process_error() - report and process a possible error
70  *
71  * @cmdtp: Command which caused the error
72  * @err: Error code (0 if none, -ve for error, like -EIO)
73  * @return 0 (CMD_RET_SUCCESS) if there is not error,
74  *         1 (CMD_RET_FAILURE) if an error is found
75  *         -1 (CMD_RET_USAGE) if 'usage' error is found
76  */
77 int cmd_process_error(cmd_tbl_t *cmdtp, int err);
78
79 /*
80  * Monitor Command
81  *
82  * All commands use a common argument format:
83  *
84  * void function (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
85  */
86
87 #if defined(CONFIG_CMD_MEMORY) || \
88         defined(CONFIG_CMD_I2C) || \
89         defined(CONFIG_CMD_ITEST) || \
90         defined(CONFIG_CMD_PCI)
91 #define CMD_DATA_SIZE
92 extern int cmd_get_data_size(char* arg, int default_size);
93 #endif
94
95 #ifdef CONFIG_CMD_BOOTD
96 extern int do_bootd(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
97 #endif
98 #ifdef CONFIG_CMD_BOOTM
99 extern int do_bootm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
100 extern int bootm_maybe_autostart(cmd_tbl_t *cmdtp, const char *cmd);
101 #else
102 static inline int bootm_maybe_autostart(cmd_tbl_t *cmdtp, const char *cmd)
103 {
104         return 0;
105 }
106 #endif
107
108 extern int do_bootz(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
109
110 extern int do_booti(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
111
112 extern int common_diskboot(cmd_tbl_t *cmdtp, const char *intf, int argc,
113                            char *const argv[]);
114
115 extern int do_reset(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
116 extern int do_poweroff(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
117
118 extern unsigned long do_go_exec(ulong (*entry)(int, char * const []), int argc,
119                                 char * const argv[]);
120 /*
121  * Error codes that commands return to cmd_process(). We use the standard 0
122  * and 1 for success and failure, but add one more case - failure with a
123  * request to call cmd_usage(). But the cmd_process() function handles
124  * CMD_RET_USAGE itself and after calling cmd_usage() it will return 1.
125  * This is just a convenience for commands to avoid them having to call
126  * cmd_usage() all over the place.
127  */
128 enum command_ret_t {
129         CMD_RET_SUCCESS,        /* 0 = Success */
130         CMD_RET_FAILURE,        /* 1 = Failure */
131         CMD_RET_USAGE = -1,     /* Failure, please report 'usage' error */
132 };
133
134 /**
135  * Process a command with arguments. We look up the command and execute it
136  * if valid. Otherwise we print a usage message.
137  *
138  * @param flag          Some flags normally 0 (see CMD_FLAG_.. above)
139  * @param argc          Number of arguments (arg 0 must be the command text)
140  * @param argv          Arguments
141  * @param repeatable    This function sets this to 0 if the command is not
142  *                      repeatable. If the command is repeatable, the value
143  *                      is left unchanged.
144  * @param ticks         If ticks is not null, this function set it to the
145  *                      number of ticks the command took to complete.
146  * @return 0 if the command succeeded, 1 if it failed
147  */
148 int cmd_process(int flag, int argc, char * const argv[],
149                                int *repeatable, unsigned long *ticks);
150
151 void fixup_cmdtable(cmd_tbl_t *cmdtp, int size);
152
153 /**
154  * board_run_command() - Fallback function to execute a command
155  *
156  * When no command line features are enabled in U-Boot, this function is
157  * called to execute a command. Typically the function can look at the
158  * command and perform a few very specific tasks, such as booting the
159  * system in a particular way.
160  *
161  * This function is only used when CONFIG_CMDLINE is not enabled.
162  *
163  * In normal situations this function should not return, since U-Boot will
164  * simply hang.
165  *
166  * @cmdline:    Command line string to execute
167  * @return 0 if OK, 1 for error
168  */
169 int board_run_command(const char *cmdline);
170 #endif  /* __ASSEMBLY__ */
171
172 /*
173  * Command Flags:
174  */
175 #define CMD_FLAG_REPEAT         0x0001  /* repeat last command          */
176 #define CMD_FLAG_BOOTD          0x0002  /* command is from bootd        */
177 #define CMD_FLAG_ENV            0x0004  /* command is from the environment */
178
179 #ifdef CONFIG_AUTO_COMPLETE
180 # define _CMD_COMPLETE(x) x,
181 #else
182 # define _CMD_COMPLETE(x)
183 #endif
184 #ifdef CONFIG_SYS_LONGHELP
185 # define _CMD_HELP(x) x,
186 #else
187 # define _CMD_HELP(x)
188 #endif
189
190 #ifdef CONFIG_CMDLINE
191 #define U_BOOT_CMD_MKENT_COMPLETE(_name, _maxargs, _rep, _cmd,          \
192                                 _usage, _help, _comp)                   \
193                 { #_name, _maxargs, _rep, _cmd, _usage,                 \
194                         _CMD_HELP(_help) _CMD_COMPLETE(_comp) }
195
196 #define U_BOOT_CMD_COMPLETE(_name, _maxargs, _rep, _cmd, _usage, _help, _comp) \
197         ll_entry_declare(cmd_tbl_t, _name, cmd) =                       \
198                 U_BOOT_CMD_MKENT_COMPLETE(_name, _maxargs, _rep, _cmd,  \
199                                                 _usage, _help, _comp);
200
201 #else
202 #define U_BOOT_SUBCMD_START(name)       static cmd_tbl_t name[] = {};
203 #define U_BOOT_SUBCMD_END
204
205 #define _CMD_REMOVE(_name, _cmd)                                        \
206         int __remove_ ## _name(void)                                    \
207         {                                                               \
208                 if (0)                                                  \
209                         _cmd(NULL, 0, 0, NULL);                         \
210                 return 0;                                               \
211         }
212 #define U_BOOT_CMD_MKENT_COMPLETE(_name, _maxargs, _rep, _cmd, _usage,  \
213                                   _help, _comp)                         \
214                 { #_name, _maxargs, _rep, 0 ? _cmd : NULL, _usage,      \
215                         _CMD_HELP(_help) _CMD_COMPLETE(_comp) }
216
217 #define U_BOOT_CMD_COMPLETE(_name, _maxargs, _rep, _cmd, _usage, _help, \
218                             _comp)                              \
219         _CMD_REMOVE(sub_ ## _name, _cmd)
220
221 #endif /* CONFIG_CMDLINE */
222
223 #define U_BOOT_CMD(_name, _maxargs, _rep, _cmd, _usage, _help)          \
224         U_BOOT_CMD_COMPLETE(_name, _maxargs, _rep, _cmd, _usage, _help, NULL)
225
226 #define U_BOOT_CMD_MKENT(_name, _maxargs, _rep, _cmd, _usage, _help)    \
227         U_BOOT_CMD_MKENT_COMPLETE(_name, _maxargs, _rep, _cmd,          \
228                                         _usage, _help, NULL)
229
230 #endif  /* __COMMAND_H */